|
Packit |
032894 |
/* Decompression support for libdwfl: zlib (gzip), bzlib (bzip2) or lzma (xz).
|
|
Packit |
032894 |
Copyright (C) 2009, 2016 Red Hat, Inc.
|
|
Packit |
032894 |
This file is part of elfutils.
|
|
Packit |
032894 |
|
|
Packit |
032894 |
This file is free software; you can redistribute it and/or modify
|
|
Packit |
032894 |
it under the terms of either
|
|
Packit |
032894 |
|
|
Packit |
032894 |
* the GNU Lesser General Public License as published by the Free
|
|
Packit |
032894 |
Software Foundation; either version 3 of the License, or (at
|
|
Packit |
032894 |
your option) any later version
|
|
Packit |
032894 |
|
|
Packit |
032894 |
or
|
|
Packit |
032894 |
|
|
Packit |
032894 |
* the GNU General Public License as published by the Free
|
|
Packit |
032894 |
Software Foundation; either version 2 of the License, or (at
|
|
Packit |
032894 |
your option) any later version
|
|
Packit |
032894 |
|
|
Packit |
032894 |
or both in parallel, as here.
|
|
Packit |
032894 |
|
|
Packit |
032894 |
elfutils is distributed in the hope that it will be useful, but
|
|
Packit |
032894 |
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
032894 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
032894 |
General Public License for more details.
|
|
Packit |
032894 |
|
|
Packit |
032894 |
You should have received copies of the GNU General Public License and
|
|
Packit |
032894 |
the GNU Lesser General Public License along with this program. If
|
|
Packit |
032894 |
not, see <http://www.gnu.org/licenses/>. */
|
|
Packit |
032894 |
|
|
Packit |
032894 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
032894 |
# include <config.h>
|
|
Packit |
032894 |
#endif
|
|
Packit |
032894 |
|
|
Packit |
032894 |
#include "../libelf/libelfP.h"
|
|
Packit |
032894 |
#undef _
|
|
Packit |
032894 |
#include "libdwflP.h"
|
|
Packit |
032894 |
|
|
Packit |
032894 |
#include <unistd.h>
|
|
Packit |
032894 |
|
|
Packit |
032894 |
#if !USE_BZLIB
|
|
Packit |
032894 |
# define __libdw_bunzip2(...) DWFL_E_BADELF
|
|
Packit |
032894 |
#endif
|
|
Packit |
032894 |
|
|
Packit |
032894 |
#if !USE_LZMA
|
|
Packit |
032894 |
# define __libdw_unlzma(...) DWFL_E_BADELF
|
|
Packit |
032894 |
#endif
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* Consumes and replaces *ELF only on success. */
|
|
Packit |
032894 |
static Dwfl_Error
|
|
Packit |
032894 |
decompress (int fd __attribute__ ((unused)), Elf **elf)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
Dwfl_Error error = DWFL_E_BADELF;
|
|
Packit |
032894 |
void *buffer = NULL;
|
|
Packit |
032894 |
size_t size = 0;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
const off_t offset = (*elf)->start_offset;
|
|
Packit |
032894 |
void *const mapped = ((*elf)->map_address == NULL ? NULL
|
|
Packit |
032894 |
: (*elf)->map_address + offset);
|
|
Packit |
032894 |
const size_t mapped_size = (*elf)->maximum_size;
|
|
Packit |
032894 |
if (mapped_size == 0)
|
|
Packit |
032894 |
return error;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
error = __libdw_gunzip (fd, offset, mapped, mapped_size, &buffer, &size);
|
|
Packit |
032894 |
if (error == DWFL_E_BADELF)
|
|
Packit |
032894 |
error = __libdw_bunzip2 (fd, offset, mapped, mapped_size, &buffer, &size);
|
|
Packit |
032894 |
if (error == DWFL_E_BADELF)
|
|
Packit |
032894 |
error = __libdw_unlzma (fd, offset, mapped, mapped_size, &buffer, &size);
|
|
Packit |
032894 |
|
|
Packit |
032894 |
if (error == DWFL_E_NOERROR)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
if (unlikely (size == 0))
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
error = DWFL_E_BADELF;
|
|
Packit |
032894 |
free (buffer);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
else
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
Elf *memelf = elf_memory (buffer, size);
|
|
Packit |
032894 |
if (memelf == NULL)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
error = DWFL_E_LIBELF;
|
|
Packit |
032894 |
free (buffer);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
else
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
memelf->flags |= ELF_F_MALLOCED;
|
|
Packit |
032894 |
elf_end (*elf);
|
|
Packit |
032894 |
*elf = memelf;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
else
|
|
Packit |
032894 |
free (buffer);
|
|
Packit |
032894 |
|
|
Packit |
032894 |
return error;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
static Dwfl_Error
|
|
Packit |
032894 |
what_kind (int fd, Elf **elfp, Elf_Kind *kind, bool *may_close_fd)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
Dwfl_Error error = DWFL_E_NOERROR;
|
|
Packit |
032894 |
*kind = elf_kind (*elfp);
|
|
Packit |
032894 |
if (unlikely (*kind == ELF_K_NONE))
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
if (unlikely (*elfp == NULL))
|
|
Packit |
032894 |
error = DWFL_E_LIBELF;
|
|
Packit |
032894 |
else
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
error = decompress (fd, elfp);
|
|
Packit |
032894 |
if (error == DWFL_E_NOERROR)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
*may_close_fd = true;
|
|
Packit |
032894 |
*kind = elf_kind (*elfp);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
return error;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
static Dwfl_Error
|
|
Packit |
032894 |
libdw_open_elf (int *fdp, Elf **elfp, bool close_on_fail, bool archive_ok,
|
|
Packit |
032894 |
bool never_close_fd, bool bad_elf_ok)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
bool may_close_fd = false;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
Elf *elf = elf_begin (*fdp, ELF_C_READ_MMAP_PRIVATE, NULL);
|
|
Packit |
032894 |
|
|
Packit |
032894 |
Elf_Kind kind;
|
|
Packit |
032894 |
Dwfl_Error error = what_kind (*fdp, &elf, &kind, &may_close_fd);
|
|
Packit |
032894 |
if (error == DWFL_E_BADELF)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
/* It's not an ELF file or a compressed file.
|
|
Packit |
032894 |
See if it's an image with a header preceding the real file. */
|
|
Packit |
032894 |
|
|
Packit |
032894 |
off_t offset = elf->start_offset;
|
|
Packit |
032894 |
error = __libdw_image_header (*fdp, &offset,
|
|
Packit |
032894 |
(elf->map_address == NULL ? NULL
|
|
Packit |
032894 |
: elf->map_address + offset),
|
|
Packit |
032894 |
elf->maximum_size);
|
|
Packit |
032894 |
if (error == DWFL_E_NOERROR)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
/* Pure evil. libelf needs some better interfaces. */
|
|
Packit |
032894 |
elf->kind = ELF_K_AR;
|
|
Packit |
032894 |
elf->state.ar.elf_ar_hdr.ar_name = "libdwfl is faking you out";
|
|
Packit |
032894 |
elf->state.ar.elf_ar_hdr.ar_size = elf->maximum_size - offset;
|
|
Packit |
032894 |
elf->state.ar.offset = offset - sizeof (struct ar_hdr);
|
|
Packit |
032894 |
Elf *subelf = elf_begin (-1, ELF_C_READ_MMAP_PRIVATE, elf);
|
|
Packit |
032894 |
elf->kind = ELF_K_NONE;
|
|
Packit |
032894 |
if (unlikely (subelf == NULL))
|
|
Packit |
032894 |
error = DWFL_E_LIBELF;
|
|
Packit |
032894 |
else
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
subelf->parent = NULL;
|
|
Packit |
032894 |
subelf->flags |= elf->flags & (ELF_F_MMAPPED | ELF_F_MALLOCED);
|
|
Packit |
032894 |
elf->flags &= ~(ELF_F_MMAPPED | ELF_F_MALLOCED);
|
|
Packit |
032894 |
elf_end (elf);
|
|
Packit |
032894 |
elf = subelf;
|
|
Packit |
032894 |
error = what_kind (*fdp, &elf, &kind, &may_close_fd);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
if (error == DWFL_E_NOERROR
|
|
Packit |
032894 |
&& kind != ELF_K_ELF
|
|
Packit |
032894 |
&& !(archive_ok && kind == ELF_K_AR))
|
|
Packit |
032894 |
error = DWFL_E_BADELF;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* This basically means, we keep a ELF_K_NONE Elf handle and return it. */
|
|
Packit |
032894 |
if (bad_elf_ok && error == DWFL_E_BADELF)
|
|
Packit |
032894 |
error = DWFL_E_NOERROR;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
if (error != DWFL_E_NOERROR)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
elf_end (elf);
|
|
Packit |
032894 |
elf = NULL;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
if (! never_close_fd
|
|
Packit |
032894 |
&& error == DWFL_E_NOERROR ? may_close_fd : close_on_fail)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
close (*fdp);
|
|
Packit |
032894 |
*fdp = -1;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
*elfp = elf;
|
|
Packit |
032894 |
return error;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
Dwfl_Error internal_function
|
|
Packit |
032894 |
__libdw_open_file (int *fdp, Elf **elfp, bool close_on_fail, bool archive_ok)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
return libdw_open_elf (fdp, elfp, close_on_fail, archive_ok, false, false);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
Dwfl_Error internal_function
|
|
Packit |
032894 |
__libdw_open_elf (int fd, Elf **elfp)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
return libdw_open_elf (&fd, elfp, false, true, true, true);
|
|
Packit |
032894 |
}
|