Commit Graph

262 Commits

Author SHA1 Message Date
Andreas Schwab 9b29c2a136 libelf: Sync elf.h from glibc
Signed-off-by: Andreas Schwab <schwab@suse.de>
2018-04-23 13:51:26 +02:00
Mark Wielaard 88f3d2daa1 libelf: Sync elf.h from glibc.
Signed-off-by: Mark Wielaard <mark@klomp.org>
2018-02-17 00:23:19 +01:00
Joshua Watt 555e15ebe8 Use fallthrough attribute.
Use __attribute__ ((fallthrough)) to indicate switch case fall through
instead of a comment. This ensures that the fallthrough warning is not
triggered even if the file is pre-processed (hence stripping the
comments) before it is compiled.

The actual fallback implementation is hidden behind a FALLBACK macro in
case the compiler doesn't support it.

Finally, the -Wimplict-fallthrough warning was upgraded to only allow
the attribute to satisfy it; a comment alone is no longer sufficient.

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
2018-02-10 03:19:26 +01:00
Mark Wielaard 4f977868ea libelf: Don't error out when sanity checking e_shoff if scncnt is zero.
We won't use the e_shoff value in that case because we will set
elf->state.elf[64|32].scns.cnt to zero to indicate not to read
any section header data from the file.

Signed-off-by: Mark Wielaard <mark@klomp.org>
2017-10-13 16:22:06 +02:00
Mark Wielaard fef9e11b30 libelf: Add ELF_E_INVALID_ELF error value.
Add ELF_E_INVALID_ELF which is set when the ELF file data is bad.
This is different from ELF_E_INVALID_FILE which is set when the file
could not be read.

Signed-off-by: Mark Wielaard <mark@klomp.org>
2017-10-13 16:22:06 +02:00
Ulf Hermann 17d7194d29 Make sure packed structs follow the gcc memory layout
gcc defaults to using struct layouts that follow the native conventions,
even if __attribute__((packed)) is given. In order to get the layout we
expect, we need to tell gcc to always use the gcc struct layout, at
least for packed structs. To do this, we can use the gcc_struct
attribute.

This is important, not only for porting to windows, but also potentially
for other platforms, as the bugs resulting from struct layout
differences are rather subtle and hard to find.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
2017-09-20 21:40:31 +02:00
Ulf Hermann 1127470a3e Check if gcc complains about __attribute__ (visibility(..))
If so, define attribute_hidden to be empty. Also, use attribute_hidden
in all places where we hide symbols. If this attribute is missing, it
simply means that we cannot hide private symbols in the binary using
attributes. This disables some optimizations and may increase the risk
of symbol name clashes with other libraries, but is not fatal.

However, we still employ linker version scripts to explicitly define
the exported symbols. This serves much of the same purpose. Also, as
all our symbols are prefixed with the library name, and "__" for
private ones, the chance of clashes is low anyway.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
2017-08-18 22:32:44 +02:00
Ulf Hermann 07737584e7 Check for -z,defs, -z,relro, -fPIC, -fPIE before using them
Those flags are not available on all platforms, and omitting them when
not available will not cause any harm. In particular:

-z,defs disallows undefined symbols in object files. This option is
unsupported if the target binary format enforces the same condition
already. Furthermore it is only a compile time sanity check. When it is
omitted, the same binary is produced.

-z,relro instructs the loader to mark sections read-only after loading
the library, where possible. This is a hardening mechanism. If it is
unavailable, the functionality of the code is not affected in any way.

-fPIC instructs the compiler to produce position independent code. While
this is preferable to relocatable code, relocatable code also works and
may even be faster. Relocatable code might just be loaded into memory
multiple times for different processes.

-fPIE is the same thing as -fPIC for executables rather than shared
libraries.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
2017-08-18 22:29:46 +02:00
Mark Wielaard 6d2e7e7100 libelf: Sync elf.h from glibc.
Add new powerpc note descriptors.

Signed-off-by: Mark Wielaard <mark@klomp.org>
2017-08-15 22:43:01 +02:00
Gustavo Romero 55a471f5fe ppc64: Add HTM SPRs support to readelf
Since POWER8, PowerPC 64 supports Hardware Transactional Memory, which has
three special purpose registers associated to it: tfhar, tfiar, and texasr.
This commit add HTM SPRs set as known note type so it's possible to use
'readelf --notes' to inspect the HTM SPRs in a coredump file generated in
such a machines.

Signed-off-by: Gustavo Romero <gromero@linux.vnet.ibm.com>
2017-07-25 12:20:42 +02:00
Ulf Hermann 8abf0b5b58 Unify linking of libasm, libelf, libdw, backends
Link them all with -z,defs,-z,relro,--no-undefined, provide complete
dependencies for the link steps, and add libeu.a to each one. libeu.a
contains useful library functionality that each of them might use. The
linker will strip unneeded symbols, so linking it in won't hurt even if
none of the functions are used.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
2017-07-24 11:01:09 +02:00
Ulf Hermann 575198c29a Avoid double-including config.h
config.h doesn't have include guards, so including it twice is bad. We
deal with this by checking for PACKAGE_NAME, but only in some places.
Once we start using gnulib, we will need to include config.h before any
gnulib-generated headers. This is problematic if we include it
transitively through our own private headers.

In order to set a clear rule about inclusion of config.h, it is now
included in every .c file as first header, but not in any header. This
will definitely avoid double-inclusion and satisfy the condition that it
has to be included before gnulib headers. It comes at the price of
adding some redundancy, but there is no clean way to avoid this.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
2017-05-02 13:28:54 +02:00
Ulf Hermann fd9e7345b4 Use F_GETFD rather than F_GETFL to check validity of file descriptor
F_GETFD is both cheaper and easier to port, and otherwise has the same
effect here.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
2017-05-02 13:18:40 +02:00
Ulf Hermann c73a00d82b Make __attribute__ conditional in all installed headers
__attribute__ is a GNU extension. If we want to link against the
libraries using a different compiler, it needs to be disabled. It was
already disabled in libdw.h, and this patch extends this to the other
headers. We move the defines to libelf.h as that is included in all
the others.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
2017-05-02 13:07:28 +02:00
Ulf Hermann a6c6fb4ae4 On elf_update, remember when we mmap()
Otherwise we skip the munmap() later. This leaks resources.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
2017-04-28 00:14:56 +02:00
Mark Wielaard 7114c513fb libelf: Initialize n to zero in elf_getarsym.
When building with gcc -Os it seems we can inline read_number_entries
but if that function fails then n will not be initialized. GCC seems not
to realize that in that case n won't be used at all. Explicitly initialize
n to zero to prevent a spurious error: 'n' may be used uninitialized in
this function [-Werror=maybe-uninitialized] in that case.

https://sourceware.org/bugzilla/show_bug.cgi?id=21011

Signed-off-by: Mark Wielaard <mark@klomp.org>
2017-04-19 17:11:08 +02:00
Mark Wielaard a3bf8f0852 libelf: Always update last_offset in updatefile and updatemmap.
When ELF section data was used, but not updated or marked as dirty and
there also existed non-dirty sections and some padding was needed between
the sections (possibly because of alignment) then elf_update might write
"fill" over some of the existing data. This happened because in that case
the last_position was not updated correctly.

Includes a new testcase fillfile that fails before this patch by showing
fill instead of the expected data in some section data. It succeeds with
this patch.

https://sourceware.org/bugzilla/show_bug.cgi?id=21199

Signed-off-by: Mark Wielaard <mark@klomp.org>
2017-04-04 00:03:27 +02:00
Mark Wielaard 8dcc4bf791 libelf: Check compression ratio before trying to allocate output buffer.
The maximum compression factor (http://www.zlib.net/zlib_tech.html) is
1032:1. Add a sanity check for that before trying to allocate lots of
memory and trying to decompress lots of bogus data.

https://sourceware.org/bugzilla/show_bug.cgi?id=21301

Signed-off-by: Mark Wielaard <mark@klomp.org>
2017-04-03 23:53:10 +02:00
Akihiko Odaki 0d0f8450ff libelf: gelf_newehdr and gelf_newehdr should return void *.
unsigned long int is not always capable to have pointer in some cases
(LLP64, for example). Return a void pointer instead. Other libelf
implementations will also make this change (or already have).
Also update the documentation to state what is created and that NULL
is returned on error (don't document that the returned value is a
pointer to the actual header created).

Signed-off-by: Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-12-07 15:19:08 +01:00
Mark Wielaard 09ec02ec7f libelf: Sanity check offset and size before trying to malloc and read data.
Bad sh_off or sh_size could trigger a bad malloc or read. Sanity check
the header values first before trying to malloc a huge buffer or reading
any data that will certainly fail.

https://bugzilla.redhat.com/show_bug.cgi?id=1387584

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-11-10 12:11:00 +01:00
Mark Wielaard 191000fded libelf: Always set ELF maxsize when reading an ELF file for sanity checks.
There are various sanity checks that depend on knowing the file size
of the underlying ELF file which we only used when mmapping the ELF file.
Although we probably won't crash if we use pread to try to read from
the file, we still might return completely bogus data structures. This
could cause us to malloc insane amounts of memory.

Always try to get the maxsize when unknown in elf_begin.c (read_file).

https://bugzilla.redhat.com/show_bug.cgi?id=1388057

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-11-10 12:11:00 +01:00
Akihiko Odaki a24d52ac20 Do not depend on some non-POSIX features.
Define/open code memrchr, rawmemchr, powerof2 and TEMP_FAILURE_RETRY if
not available through system headers.

Signed-off-by: Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-10-13 11:24:03 +02:00
Akihiko Odaki 60b2bf1b08 lib: Provide MAX and MIN in system.h
This change also creates a new header file libeu.h to provide the
prototypes for the function of libeu. That hides the definition of function
crc32, which can conflict with zlib, from libelf. It also prevents mistakes
to refer those functions from a component which doesn't link with libeu,
such as libelf.

Signed-off-by: Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
2016-10-12 15:43:14 +02:00
Mark Wielaard e93f2d801f libelf: Fix memory leak in elf_compress for mmapped ELF files.
The testcase added to run-strip-reloc.sh for strip-compressed.o showed
a memory leak when ran under valgrind (configure --enable-valgrind).

For a mmapped ELF file when existing section data was compressed
elf_end would fail to release the new compressed data buffer assigned
to rawdata_base. For non-mapped files rawdata_base is always freed.
For decompressed data rawdata_base is released together with zdata_base.

Use the Elf_Scn flag ELF_T_MALLOCED to track whether rawdata_base
points to malloced memory and free it in elf_end even for mmapped
ELF files.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-08-15 09:58:12 +02:00
Mark Wielaard e6ca75ddcf Remove eu-ld and unused code.
Nobody has hacked on eu-ld in a very long time. It didn't really work.
And we didn't install it by default in the spec file. Remove sources,
the build rules and any (now) unused code.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-08-03 17:26:06 +02:00
Mark Wielaard 8b5f017ddf libelf: Allow updating phdrs for any e_type.
elf[32|64]_updatenull would sanity check the e_type before allowing to
update the phdrs. This prevents creating an ET_REL file with phdrs. It
also prevents creating any vendor specific ELF file having phdrs. We
only check this when updating/writing out the file. But we would just
read such files. Don't prevent people from creating unexpected ELF files.
elflint will warn for such files.

While writing a new testcase for this another bug was found that
prevented updating a just created phdr because elf_getphdrnum would
sanity check the phdr offset in the file (which doesn't exist yet).
Fix that by only doing such a sanity check if the phdrs haven't been
read in or created yet.

This second bug should have been found by the existing elfshphehdr
test, but that test contained a typo checking elf_getphdrnum.
It tested that the called failed when there were no phdrs, but then
elf_getphdrnum should simply succeed and return zero.

https://bugzilla.redhat.com/show_bug.cgi?id=1352232

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-07-11 09:53:29 +02:00
John Ogness 96e140f668 libelf: find 1st section instead of assuming
When getting section headers it is assumed that the first section
is on the first section list. However, it is possible that the
first section list only contains the zeroth section, in which
case either illegal memory access occurs or elf_nextscn()
erroneously returns NULL.

With this patch, checks are added to avoid the illegal memory
access and (if available) the second section list is looked at
to find the first section.

A new test emptyfile is added that tests adding a section to
and "empty" ELF file 32/64 class with ELF_C_RDWR[_MMAP].

Signed-off-by: John Ogness <john.ogness@linutronix.de>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-06-28 20:19:24 +02:00
Richard Henderson 4c746f2501 Sync elf.h from glibc
Add lots of new EM_* definitions.
Add R_BPF_* definitions.

Signed-off-by: Richard Henderson <rth@redhat.com>
2016-06-28 20:08:52 +02:00
Mark Wielaard f7f45ed580 libelf: Fix memory leak when zlib deflateInit fails in elf_compress.
On error we return NULL, not out_buf. So make sure to not leak it.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-04-14 21:08:30 +02:00
Mark Wielaard 7249c8e4b5 libelf: Don't leak memory when out of memory in updatemmap.
We forgot to free scns when returning ELF_E_NOMEM.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-02-22 12:14:44 +01:00
Mark Wielaard 2f289fa9e3 Add new i386 and x86_64 relocation types.
Update elf.h from glibc and recognize R_386_GOT32X, R_X86_64_GOTPCRELX
and R_x86_64_REX_GOTPCRELX as non-dynamic relocations.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-02-19 21:13:23 +01:00
Mark Wielaard 1899704b87 Add NT_ARM_SYSTEM_CALL aarch64 syscall regset.
Linux kernel 3.18 added the NT_ARM_SYSTEM_CALL regset for aarch64.
Recognize and print this new core item.

https://bugzilla.redhat.com/show_bug.cgi?id=1285613

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-02-15 23:51:31 +01:00
Mark Wielaard 22035605b9 libelf: elf_getdata should not adjust alignment for SHT_NOBITS sections.
In commit c0748e "libelf: More checking of valid sh_addralign values." we
adjusted bogus alignment of data buffers if they were greater than the
offset of the data in the file. This works OK, except when there is no
data in the file. So make sure to not adjust any NOBITS sections.

Also adds a test that shows the issue and makes sure elflint is called
with --gnu in run-strip-test.sh.

https://bugzilla.redhat.com/show_bug.cgi?id=1303845

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-02-10 11:27:18 +01:00
Chih-Hung Hsieh 203f0a3eec Move nested functions in elf_compress.c and elf_strptr.c.
* elf_compress.c (__libelf_compress): do_deflate_cleanup
* elf_strptr.c (elf_strptr): get_zdata

Signed-off-by: Chih-Hung Hsieh <chh@google.com>
2016-01-23 19:55:41 +01:00
Mark Wielaard 344ca0775d libelf: Add ELF compression types and defines to libelf.h for older glibc.
Older glibc elf.h might not define the new ELF compression defines and
types. If not just define them in libelf.h directly to make the libelf
headers work on older glibc systems.

Also include a testcase to check the libelf headers build against the
system elf.h.

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=810885

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-01-14 14:26:22 +01:00
Mark Wielaard 519c13c9c7 libelf: Make elf_strptr index correctly into compressed section data.
elf_strptr indexes into the section data. This is defined as index into
the uncompressed data of the section. If the section is compressed make
sure the uncompressed data is available, but don't really decompress the
section header (elf_getdata will still return compressed data).

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-01-06 14:27:10 +01:00
Mark Wielaard 272018bba1 libelf: Add elf_compress and elf_compress_gnu.
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-01-06 14:27:10 +01:00
Mark Wielaard 5ddb50af4a libelf: Add elf32_getchdr, elf64_getchdr and gelf_getchdr.
Elf_Data of a compressed section has type ELF_T_CHDR. This type can be
xlated to the file or memory representation. This will make sure the Chdr
is in the correct endianess. The compressed data following the Chdr isn't
translated.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-01-06 14:27:10 +01:00
Mark Wielaard 528cb45fc9 Make zlib mandatory.
Explicitly link libelf and libdw with -lz.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2015-10-26 13:52:31 +01:00
Mark Wielaard 5eb3e901f9 elf.h: Update from glibc. Add section compression constants and structures.
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2015-10-22 23:25:55 +02:00
Jose E. Marchesi 1ed7195ea8 libelf: use the right size when preading in[0].sh_size
Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
2015-10-22 23:09:56 +02:00
Chih-Hung Hsieh 239d859169 Move nested functions in libelf.
* Move nested functions to file scope
  in libelf/elf_begin.c and elf32_updatefile.c
  to compile with clang.

Signed-off-by: Chih-Hung Hsieh <chh@google.com>
2015-10-22 22:43:42 +02:00
Josh Stone b1d0b0fc7e libelf: Use int64_t for offsets in libelf.h
Some systems don't have loff_t, like FreeBSD where off_t always supports
large files.  We need a standardized 64-bit signed type for the public
header, without depending on configuration... OK, just use int64_t.

Signed-off-by: Josh Stone <jistone@redhat.com>
2015-10-14 09:29:27 -07:00
Mark Wielaard ae1e85ea0b libelf: Only use posix_fallocate when using mmap. Ignore unsupported errors.
Don't use posix_fallocate when not using mmap. It is enough to ftruncate
the file to the right size. pwrite will report an error if there is no
disk space left. And on file systems that don't support fallocate it
might duplicate writes in that case. When using posix_fallocate do ignore
most errors. Other libc implementations don't guarantee the call actually
works always and even with glibc there might be an unexpected error from
the fallback code when the file system doesn't support fallocate. That is
pretty nasty since we might get a SIGBUS in that case when writing to the
mmapped memory. But the chance of that happening is very small.  And will
normally never happen with glibc. So only report an error when
posix_fallocate reports ENOSPC.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2015-10-12 12:44:49 +02:00
Josh Stone 3425454a10 Trust AC_SYS_LARGEFILE to provide large file support
AC_SYS_LARGEFILE defines _FILE_OFFSET_BITS in config.h if needed for
LFS, and this automatically maps things like open to open64.  But quite
a few places used explicit 64-bit names, which won't work on platforms
like FreeBSD where off_t is always 64-bit and there are no foo64 names.
It's better to just trust that AC_SYS_LARGEFILE is doing it correctly.

But we can verify this too, as some file could easily forget to include
config.h.  The new tests/run-lfs-symbols.sh checks all build targets
against lfs-symbols (taken from lintian) to make sure everything was
implicitly mapped to 64-bit variants when _FILE_OFFSET_BITS is set.

Signed-off-by: Josh Stone <jistone@redhat.com>
2015-10-09 10:10:37 -07:00
Chih-Hung Hsieh 7eff36d5da Do without union of variable length arrays.
Prepare to compile with clang.

A union like
  { T32 a32[n]; T64 a64[n]; } u;
is expanded to
  size_t nbytes = n * MAX(sizeof(T32), sizeof(T64));
  void *data = malloc(nbytes);
  T32 (*a32)[n] = data;
  T64 (*a64)[n] = data;

Signed-off-by: Chih-Hung Hsieh <chh@google.com>
2015-10-07 22:44:41 +02:00
Josh Stone daee4714ee Improve AM_SILENT_RULES coverage
Note, elfutils does not explicitly enable AM_SILENT_RULES.  It's only
available starting from automake 1.11, but starting from automake 1.13
silent rules are always generated, defaulting to verbose.  $(AM_V_foo)
additions should be no-ops on systems that don't support silent rules.

To be silent, use "./configure --enable-silent-rules" or "make V=0".

Signed-off-by: Josh Stone <jistone@redhat.com>
2015-10-06 17:50:10 -07:00
Jose E. Marchesi 6a20e20ea9 Use -fPIC instead of -fpic when generating PIC code.
This avoids relocation overflows in sparc/sparc64 targets while
linking,  where the reachable data using -fpic is only 4kb.

Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
2015-10-06 23:54:34 +02:00
Mark Wielaard 3adda3ce2a libelf: Always update e_version and e_shentsize in elf_update.
When e_version is EV_NONE we should set it to EV_CURRENT like we do for
the EI_VERSION and like we set EI_DATA to the correct byte order when set
to ELFDATANONE.  Likewise we should always set e_shentsize like we do for
e_phentsize, not just when ELF_F_LAYOUT isn't set.

Add a new elfshphehdr testcase to check the above.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2015-10-05 21:50:51 +02:00
Mark Wielaard d8698e55cb Properly mark all internal function definitions.
Since we banned old style function definitions GCC is able to diagnose
function definitions that don't match the function declaration:

  elf32_getehdr.c:78: error: conflicting types for ‘__elf64_getehdr_wrlock’
  libelfP.h:498: note: previous declaration of ‘__elf64_getehdr_wrlock’

This happens on i386 because there internal functions are marked with:

  # define internal_function __attribute__ ((regparm (3), stdcall))

Make sure all internal function declarations and definitions are marked
with internal_function.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2015-09-23 20:49:54 +02:00