platform/upstream/elfutils.git
9 years agoDefine MODVERSION in a more reproducable way.
Mark Wielaard [Sun, 31 May 2015 21:03:30 +0000 (23:03 +0200)]
Define MODVERSION in a more reproducable way.

We define MODVERSION to make sure the libebl backend modules match the
elfutils build when we dlopen them. Using hostname and date makes it very
hard to create reproducable builds. Define them a little more reproducable
using LIBEBL_SUBDIR, eu_version and ac_cv_build.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoFix sanitize-undefined config check. Don't ALLOW_UNALIGNED when checking.
Mark Wielaard [Sun, 31 May 2015 20:37:19 +0000 (22:37 +0200)]
Fix sanitize-undefined config check. Don't ALLOW_UNALIGNED when checking.

The -fsanitize=undefined check need to use AC_LINK_IFELSE to make sure
libubsan is installed. When doing undefined checking we should disable
ALLOW_UNALIGNED memory accesses to catch issues where we do unaligned
accesses by accident.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Don't assume auxv or r_debug data is properly aligned in link_map.
Mark Wielaard [Sun, 31 May 2015 18:49:23 +0000 (20:49 +0200)]
libdwfl: Don't assume auxv or r_debug data is properly aligned in link_map.

core file data isn't guaranteed to be alligned properly. Use
read_(4|8)ubyte_unaligned_noncvt to read values, types and addresses.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibelf: Fix possible unbounded stack usage in load_shdr_wrlock.
Mark Wielaard [Sun, 31 May 2015 14:09:01 +0000 (16:09 +0200)]
libelf: Fix possible unbounded stack usage in load_shdr_wrlock.

When a copy needs to be made of the shdrs, allocate with malloc and free
after conversion instead of calling alloca.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibelf: Fix possible unbounded stack usage in getphdr_wrlock.
Mark Wielaard [Sun, 31 May 2015 14:05:34 +0000 (16:05 +0200)]
libelf: Fix possible unbounded stack usage in getphdr_wrlock.

When a copy needs to be made of the phdrs, allocate with malloc and free
after conversion instead of calling alloca.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibelf: Fix unbounded stack usage in elf_getarsym for !ALLOW_UNALIGNED case.
Mark Wielaard [Sun, 31 May 2015 13:58:20 +0000 (15:58 +0200)]
libelf: Fix unbounded stack usage in elf_getarsym for !ALLOW_UNALIGNED case.

The number of entries in the index can be large, don't use alloca to
read in temporary data, use malloc (which is freed after out).

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoelfcmp: Don't call memcmp with possible NULL d_buf.
Mark Wielaard [Sat, 30 May 2015 21:55:50 +0000 (23:55 +0200)]
elfcmp: Don't call memcmp with possible NULL d_buf.

When d_size is zero d_buf might be NULL.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibelf: Don't call memmove with possible NULL buffer.
Mark Wielaard [Sat, 30 May 2015 21:50:14 +0000 (23:50 +0200)]
libelf: Don't call memmove with possible NULL buffer.

When size is zero the buffer src and dest buffers might be NULL.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibelf: Don't call mempcpy with possible NULL d_buf.
Mark Wielaard [Sat, 30 May 2015 21:43:11 +0000 (23:43 +0200)]
libelf: Don't call mempcpy with possible NULL d_buf.

When d_size is zero d_buf might be NULL. last_position doesn't need to be
updated in that case.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Check file_name is not NULL before calling canonicalize_file_name.
Mark Wielaard [Sat, 30 May 2015 21:38:28 +0000 (23:38 +0200)]
libdwfl: Check file_name is not NULL before calling canonicalize_file_name.

canonicalize_file_name is marked as taking a nonnull argument.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agotests: Always print reason when skipping test.
Mark Wielaard [Sat, 30 May 2015 21:00:40 +0000 (23:00 +0200)]
tests: Always print reason when skipping test.

Make sure the reason a test is SKIPPED is added to the test-suite.log.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoconfigure: Add check for bunzip2 and maintainer-mode tools.
Mark Wielaard [Sat, 30 May 2015 20:50:37 +0000 (22:50 +0200)]
configure: Add check for bunzip2 and maintainer-mode tools.

Without bunzip2 make check cannot be run. To generate all files in
maintainer-mode flex and bison are needed. Otherwise check that
libdw/known-dwarf.h is already generated.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdw: Fix overflow in read_encoded_value for the DW_EH_PE_indirect case.
Mark Wielaard [Wed, 27 May 2015 12:05:15 +0000 (14:05 +0200)]
libdw: Fix overflow in read_encoded_value for the DW_EH_PE_indirect case.

If we are going to dereference a pointer there should be at least enough
data to hold a pointer. Found by afl-fuzz.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdw: Fix overflow in read_encoded_value for the DW_EH_PE_indirect case.
Mark Wielaard [Wed, 27 May 2015 12:05:15 +0000 (14:05 +0200)]
libdw: Fix overflow in read_encoded_value for the DW_EH_PE_indirect case.

If we are going to dereference a pointer there should be at least enough
data to hold a pointer. Found by afl-fuzz.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Check mod is not NULL in check_module.
Mark Wielaard [Sat, 23 May 2015 22:07:33 +0000 (00:07 +0200)]
libdwfl: Check mod is not NULL in check_module.

check_module is called from dwfl_module_relocate_address and from
dwfl_module_address_section. Both could take a NULL Dwfl_Module if
an earlier error had occured. Make check_module return immediately
indicating an issue in that case.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoconfig: Add -Wstack-usage=262144 (256K) warning check.
Mark Wielaard [Sat, 23 May 2015 15:50:09 +0000 (17:50 +0200)]
config: Add -Wstack-usage=262144 (256K) warning check.

All library code now builds with this warning and -Werror enabled.
Add exceptions for most of the src tools (ldgeneric, readelf, nm, size,
strip, elflint, findtextrel, elfcmp objdump, ranlib, ar and unstrip).

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdw: Limit stack usage of temporary dir, file and line lists.
Mark Wielaard [Fri, 22 May 2015 21:33:56 +0000 (23:33 +0200)]
libdw: Limit stack usage of temporary dir, file and line lists.

Limit stack usage of lines with MAX_STACK_LINES, files with MAX_STACK_LINES
and dirs with MAX_STACK_DIRS. Calculate number of dirs needed first, then
create dirarray directly, without needing the next field. Free not stack
allocated lines and files at the end.

Use preallocated stack arrays for all instead of calling alloca to help
compiler see actual stack usage.

The current stack allocation limits are 4096 lines, 1024 files in 256 dirs
per CU. For GCC 4.8 libstdc++.so has on average 512 lines per CU.  With
these settings read_srclines uses max 228K of stack on x86_64.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Don't allocate phdrs and dyn on stack in link_map.
Mark Wielaard [Fri, 22 May 2015 16:31:58 +0000 (18:31 +0200)]
libdwfl: Don't allocate phdrs and dyn on stack in link_map.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Don't allocate phdrs and dyn on stack in dwfl_segment_report_module.
Mark Wielaard [Fri, 22 May 2015 16:01:34 +0000 (18:01 +0200)]
libdwfl: Don't allocate phdrs and dyn on stack in dwfl_segment_report_module.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: find_debuginfo_in_patch don't alloca/strdupa strings of unknown size.
Mark Wielaard [Fri, 22 May 2015 14:01:02 +0000 (16:01 +0200)]
libdwfl: find_debuginfo_in_patch don't alloca/strdupa strings of unknown size.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Reject very short or really large build-ids.
Mark Wielaard [Fri, 22 May 2015 12:18:11 +0000 (14:18 +0200)]
libdwfl: Reject very short or really large build-ids.

We cannot handle build-ids less than at least 3 or more than 64 bytes.
Very big build-ids, or very large debug search paths might have blown
up the stack.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Don't allocate all phdrs on the stack in elf_from_remote_memory.
Mark Wielaard [Tue, 19 May 2015 11:39:39 +0000 (13:39 +0200)]
libdwfl: Don't allocate all phdrs on the stack in elf_from_remote_memory.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Don't stack allocate alternate_name in dwfl_linux_kernel_find_elf.
Mark Wielaard [Tue, 19 May 2015 10:58:49 +0000 (12:58 +0200)]
libdwfl: Don't stack allocate alternate_name in dwfl_linux_kernel_find_elf.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Minimize stack usage in dwfl_linux_kernel_report_offline.
Mark Wielaard [Tue, 19 May 2015 10:40:30 +0000 (12:40 +0200)]
libdwfl: Minimize stack usage in dwfl_linux_kernel_report_offline.

Don't stack allocate module name. Also fixes a latent bug (if the module
file didn't have a suffix - which is very unlikely) and an inefficiency.
We only need to substitue chars up to the suffix.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdw: Fix possibly unbounded stack usage in __libdw_intern_expression.
Mark Wielaard [Tue, 19 May 2015 09:52:58 +0000 (11:52 +0200)]
libdw: Fix possibly unbounded stack usage in __libdw_intern_expression.

Create a stack allocated array to hold locs. Allocate locs bigger than
the array with malloc and free them when done.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Fix unbounded stack usage in find_prelink_address_sync.
Mark Wielaard [Mon, 18 May 2015 19:26:24 +0000 (21:26 +0200)]
libdwfl: Fix unbounded stack usage in find_prelink_address_sync.

Allocate phdr and shdr unions with malloc, not alloca. Free after use.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Fix possible unbounded stack usage in cache_sections.
Mark Wielaard [Mon, 18 May 2015 14:23:06 +0000 (16:23 +0200)]
libdwfl: Fix possible unbounded stack usage in cache_sections.

For modules with lots of sections cache_sections could blow up the stack
theoretically. Don't use alloca, but use malloc with explicit free.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibebl: Don't blow up stack when processing large NT_GNU_ABI_TAG.
Mark Wielaard [Sun, 17 May 2015 18:07:56 +0000 (20:07 +0200)]
libebl: Don't blow up stack when processing large NT_GNU_ABI_TAG.

Normally an NT_GNU_ABI_TAG is large, just 4 words (16 bytes).
Only use stack allocated conversion buf for small (max 16 words) notes.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibebl: Use static number for array stack allocation in openbackend.
Mark Wielaard [Sun, 17 May 2015 16:49:10 +0000 (18:49 +0200)]
libebl: Use static number for array stack allocation in openbackend.

Help the compiler see we stack allocate a small array.  Add assert to check
no machine prefix is ever larger than 16. Current maximum number is 7.
This prevents gcc warning about possible unbounded stack usage with
-Wstack-usage.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibelf: Fix possible unbounded stack usage in updatefile.
Mark Wielaard [Sun, 17 May 2015 08:30:57 +0000 (10:30 +0200)]
libelf: Fix possible unbounded stack usage in updatefile.

Allocate shdr_data and scns with malloc, not alloca. Free after writing
section headers.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibelf: Fix possible unbounded stack usage in updatemmap.
Mark Wielaard [Sat, 16 May 2015 20:41:36 +0000 (22:41 +0200)]
libelf: Fix possible unbounded stack usage in updatemmap.

Allocate temporary shdr storage with malloc, not alloca. Free after
writing section headers.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibelf: Fix possible unbounded stack usage in elf_getarsym.
Mark Wielaard [Sat, 16 May 2015 19:30:41 +0000 (21:30 +0200)]
libelf: Fix possible unbounded stack usage in elf_getarsym.

The number of entries in the index can be large, don't use alloca to
read in temporary data, use malloc (and free after out).

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoaddr2line: Set scopes to NULL after free in handle_address.
Mark Wielaard [Thu, 21 May 2015 14:05:18 +0000 (16:05 +0200)]
addr2line: Set scopes to NULL after free in handle_address.

If dwarf_getscopes_die () fails then scopes might not be reset and we
could cause a double free.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoaddr2line: Add --pretty-print option to show all information on one line.
Mark Wielaard [Wed, 20 May 2015 12:09:46 +0000 (14:09 +0200)]
addr2line: Add --pretty-print option to show all information on one line.

This adds an option --pretty-print to eu-addr2line to show all information
on one line and all inlines on a line of their own. This mimics the same
option from binutils addr2line, but without the short option variant -p.
Since we already use -p to select the process.

Example output:

eu-addr2line --pretty-print -s -i -f -C -p$(pidof firefox) 0x00007f368c6f8915
mozilla::ReentrantMonitor::Wait(unsigned int) at ReentrantMonitor.h:92
 (inlined by) mozilla::ReentrantMonitorAutoEnter::Wait(unsigned int) at ReentrantMonitor.h:190

A couple of tests were added to check the output matches that of
binutils addr2line.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoaddr2line: Add demangler support.
Mark Wielaard [Wed, 20 May 2015 09:52:27 +0000 (11:52 +0200)]
addr2line: Add demangler support.

Makes the -C, --demangle option visible and implements it (ignoring the
demangle style argument). Adds a new test with sample output.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoaddr2line: Add -a, --address. Print address before for each entry.
Mark Wielaard [Tue, 19 May 2015 14:21:27 +0000 (16:21 +0200)]
addr2line: Add -a, --address. Print address before for each entry.

Adds test cases with sample output.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoaddr2line: Always parse addresses as hex numbers.
Mark Wielaard [Tue, 19 May 2015 21:08:00 +0000 (23:08 +0200)]
addr2line: Always parse addresses as hex numbers.

We would sometimes interpret input addresses as decimal or octal.
That could be confusing and isn't what binutils addr2line does.
Be consistent and always treat input addresses as hex.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoaddr2line: Split options in input and output format groups.
Mark Wielaard [Wed, 20 May 2015 10:10:47 +0000 (12:10 +0200)]
addr2line: Split options in input and output format groups.

This makes it clear that -j, --section=NAME is about how input is treated,
not how the output is formatted.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibelf: Use posix_fallocate instead of ftruncate to extend ELF file.
Mark Wielaard [Thu, 14 May 2015 10:34:26 +0000 (12:34 +0200)]
libelf: Use posix_fallocate instead of ftruncate to extend ELF file.

This fixes an obscure SIGBUS error when using ELF_C_WRITE_MMAP on an ELF
file that needs extending when the underlying file system is (nearly) full.

Use posix_fallocate to make sure the file content is really there. Using
ftruncate might mean the file is extended, but space isn't allocated yet.
This might cause a SIGBUS once we write into the mmapped space and the disk
is full.

Using fallocate might fail on some file systems. posix_fallocate is
required to extend the file and allocate enough space even if the
underlying filesystem would normally return EOPNOTSUPP or the kernel
doesn't implement the fallocate syscall. Also posix_fallocate has been in
glibc since 2.1.94, while support for fallocate was only added in 2.10
and kernel 2.6.23.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoconfigure: Add --enable-sanitize-undefined.
Mark Wielaard [Thu, 21 May 2015 21:36:51 +0000 (23:36 +0200)]
configure: Add --enable-sanitize-undefined.

Now that we are -fsanitize=undefined clean make sure we keep it that way.

We do have to disable alignment checking. Because we might explicitly
use unaligned accesses (see ALLOW_UNALIGNED).

Rename DISTCHECK_CONFIGURE_FLAGS to AM_DISTCHECK_CONFIGURE_FLAGS, so the
user can override. And add --enable-sanitize-undefined.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibelf: If e_phnum is zero then set e_phoff also to zero.
Mark Wielaard [Wed, 13 May 2015 13:21:57 +0000 (15:21 +0200)]
libelf: If e_phnum is zero then set e_phoff also to zero.

If phnum is zero make sure e_phoff is also zero and not some random value.
That would cause trouble in update_file. This could happen when ELF_F_LAYOUT
is set and the user copied over a ehdr from a bogus ELF file where the phdrs
are unreadable. In that case trying to write out the new ELF image would
crash trying to follow the bogus e_phdr value.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agostrip: Harden against bogus input files. Don't leak tmp debug file on error.
Mark Wielaard [Tue, 12 May 2015 13:59:04 +0000 (15:59 +0200)]
strip: Harden against bogus input files. Don't leak tmp debug file on error.

There were various places where a bogus/unexpected input file would cause
eu-strip to crash. Also on an unexpected error eu-strip would leak the temp
debug file it was writing.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c34

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibelf: More checking of valid sh_addralign values.
Mark Wielaard [Tue, 12 May 2015 13:28:06 +0000 (15:28 +0200)]
libelf: More checking of valid sh_addralign values.

There were two issues with bogus sh_addralign values. First we would only
check the individual Elf_Data d_align values were powerof2. But not the
actual shdr addralign value. This would cause an issue if the shdr addralign
was bigger than all of the individual d_align values. Then we could write
out a bogus (! powerof2) shdr addralign value for the sections. Secondly
when reading in the Elf_Data we would set the d_align value to the value
of the shdr addralign value. But we would not check it was valid at all.
In practice there are ELF files with incorrect sh_addralign values (they
are a powerof2, but aren't aligned to the ELF image offset). We would try
to fix that up in elf_update by adding extra padding. But this could bloat
the ELF image a lot for large alignment values. So for too large alignments
that are bigger than the offset in the ELF file clamp them to the offset
value. This could lead us to reject to write out the data again when the
offset was not a powerof2. But this will only happen for aligment values
bigger than 64. Which are uncommon in practice.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibelf: Make sure shdrs are valid before storing extended phnum in newphdr.
Mark Wielaard [Tue, 12 May 2015 13:16:35 +0000 (15:16 +0200)]
libelf: Make sure shdrs are valid before storing extended phnum in newphdr.

Creating phdr with more than PN_XNUM phnum requires a valid section zero
shdr to store the extended value. Make sure the shdrs are valid. Also fix
the error when count was too big to store by setting ELF_E_INVALID_INDEX
before failing.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agodwarf_begin_elf: new error for compression failure
Jonathan Lebon [Mon, 11 May 2015 19:38:14 +0000 (15:38 -0400)]
dwarf_begin_elf: new error for compression failure

In order to provide better feedback to users, a distinction is made
between finding no dwarf info (E_NO_DWARF) and finding dwarf info but
which is compressed and failed to decompress (E_COMPRESSED_ERROR). This
can occur for example if elfutils was built without zlib support, or if
the compressed section is invalid.

Signed-off-by: Jonathan Lebon <jlebon@redhat.com>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agodwarf_begin_elf: decouple section searching from reading
Jonathan Lebon [Mon, 11 May 2015 19:38:13 +0000 (15:38 -0400)]
dwarf_begin_elf: decouple section searching from reading

To help legibility, we separate the section name matching from the
actual section reading. This also allows us to remove duplicate code in
cases of sections appearing twice or empty section data. There are no
changes in functionality. The indentation will be fixed in the next
commit, in the interest of keeping this commit easier to read.

Signed-off-by: Jonathan Lebon <jlebon@redhat.com>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoAlways call gelf_fsize with EV_CURRENT as argument.
Mark Wielaard [Fri, 8 May 2015 15:56:32 +0000 (17:56 +0200)]
Always call gelf_fsize with EV_CURRENT as argument.

Don't trust the elf version given by the file. It could be completely
bogus. In which case gelf_fsize just returns zero. Which could cause
divide by zero errors.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c34

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Only intern CU when not EOF marker and cuoff points to a DIE.
Mark Wielaard [Thu, 7 May 2015 16:35:48 +0000 (18:35 +0200)]
libdwfl: Only intern CU when not EOF marker and cuoff points to a DIE.

We need to check the cuoff points to a real Dwarf_Die before trying to
intern the cu with tsearch. Otherwise bogus keys might end up in the
search tree with NULL cus. That will cause crashes in compare_cukey
during next insertion or deletion of cus. We also don't want to insert
the EOF marker and unconditionally tdestroy the lazy_cu_root. The EOF
could be caused by bad DWARF from a bogus agranges entry.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c30

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoelflint: Check gnu_hash has enough data and bitmask_words is not zero.
Mark Wielaard [Wed, 6 May 2015 16:02:10 +0000 (18:02 +0200)]
elflint: Check gnu_hash has enough data and bitmask_words is not zero.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c31

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoelflint: Add sanity checks to check_attributes.
Mark Wielaard [Wed, 6 May 2015 15:38:18 +0000 (17:38 +0200)]
elflint: Add sanity checks to check_attributes.

This is similar to commit 9644aa for readelf print_attributes.
Bail out when the vendor name isn't terminated and add overflow check
for subsection_len.

Note that readelf does handle non-gnu attributes, while elflint doesn't.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoelflint: Use Use Elf64_Word for shdr->sh_info cnt.
Mark Wielaard [Wed, 6 May 2015 14:01:55 +0000 (16:01 +0200)]
elflint: Use Use Elf64_Word for shdr->sh_info cnt.

On 32bit using int might overflow.
https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c31

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoelflint: Stop checking section when 2nd hash function shift too big.
Mark Wielaard [Wed, 6 May 2015 11:09:23 +0000 (13:09 +0200)]
elflint: Stop checking section when 2nd hash function shift too big.

Nothing good comes from trying to continue with a bogus hash function.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c31

9 years agolibdw: Detect line number overflow in dwarf_getsrclines on 32bit.
Mark Wielaard [Wed, 6 May 2015 10:55:21 +0000 (12:55 +0200)]
libdw: Detect line number overflow in dwarf_getsrclines on 32bit.

We do check whether the values we store for the line fit our data
representation in add_new_line, but on 32bit systems we would fail
to notice line overflowing.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c32

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agonm: Handle dwarf_linesrc returning NULL.
Mark Wielaard [Wed, 6 May 2015 10:45:49 +0000 (12:45 +0200)]
nm: Handle dwarf_linesrc returning NULL.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#32

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdw: dwarf_getaranges check there is enough data before reading.
Mark Wielaard [Tue, 5 May 2015 08:16:42 +0000 (10:16 +0200)]
libdw: dwarf_getaranges check there is enough data before reading.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c30

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Bounds check Dwarf_Fileinfo file number in dwfl_lineinfo.
Mark Wielaard [Tue, 5 May 2015 08:05:01 +0000 (10:05 +0200)]
libdwfl: Bounds check Dwarf_Fileinfo file number in dwfl_lineinfo.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c30

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Make dwfl_error.c C99-kosher.
Roland McGrath [Wed, 6 May 2015 20:49:47 +0000 (13:49 -0700)]
libdwfl: Make dwfl_error.c C99-kosher.

Signed-off-by: Roland McGrath <roland@hack.frob.com>
9 years agolibelf: Make sure version xlate dest buffer is fully defined.
Mark Wielaard [Sat, 3 Jan 2015 22:02:17 +0000 (23:02 +0100)]
libelf: Make sure version xlate dest buffer is fully defined.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c16
contains an example of usage of undefined memory when version section
data needs to be translated, but the version xlate functions detect they
cannot fully transform the section data. To make sure the dest buffer
data is completely defined this patch makes sure all data is moved
from src to dest first. This is somewhat inefficient since normally
all data will be fully converted. But the translation functions have
no way to indicate only partial data was converted.

Reported-by: Alexander Cherepanov <cherepan@mccme.ru>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoReplace assert_perror with assert
Max Filippov [Mon, 4 May 2015 17:17:52 +0000 (20:17 +0300)]
Replace assert_perror with assert

assert_perror is a GNU extension, it's not provided by uClibc and it's
only used in tests. Replace it with assert.

Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
9 years agoReplace futimes with futimens.
Max Filippov [Mon, 4 May 2015 17:17:51 +0000 (20:17 +0300)]
Replace futimes with futimens.

futimes is not standard function, and it's not available in uClibc.
Use futimens which is POSIX.

Suggested-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
9 years agoDrop mtrace calls.
Max Filippov [Sun, 3 May 2015 23:25:40 +0000 (02:25 +0300)]
Drop mtrace calls.

mtrace is a GNU extension not supported by uClibc and it's believed to
not be really used. Drop mtrace() calls and #include <mcheck.h>.

Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
9 years agoLink against standalone argp library
Anthony G. Basile [Sun, 3 May 2015 23:25:39 +0000 (02:25 +0300)]
Link against standalone argp library

argp is not part of POSIX standards and not provided by uClibc or
some other standard C libraries.  However, it is possible to link
against a breakout argp library.  One is provided at

    http://www.lysator.liu.se/~nisse/misc/

This patch tests if libc provides argp otherwise it adds the linker
flag -largp where needed in the build system.

Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
9 years agoconfigure: fix enable_demangler setting
Max Filippov [Sun, 3 May 2015 18:14:09 +0000 (21:14 +0300)]
configure: fix enable_demangler setting

Add missing comma to AS_IF, otherwise enable_demangler is always set to
'no'. Canonicalize tests.

Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
9 years agotests: Split run-stack tests to support missing demangler.
Mark Wielaard [Fri, 1 May 2015 08:31:36 +0000 (10:31 +0200)]
tests: Split run-stack tests to support missing demangler.

SKIP tests that need a demangler if it is not there.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoconfigure: print feature settings overview
Mark Wielaard [Fri, 1 May 2015 07:23:57 +0000 (09:23 +0200)]
configure: print feature settings overview

elfutils can be configured in various ways. But we expect certain
settings for the features that we explicitly support. Print an
overview of all settings, plus their recommended setting, at the
end of the configure run.

=====================================================================
        elfutils: 0.161 (eu_version: 161)
=====================================================================

    Prefix                             : /opt/local/install/elfutils
    Program prefix ("eu-" recommended) : eu-
    Source code location               : /home/mark/src/elfutils
    Maintainer mode                    : yes
    libebl modules subdirectory        : elfutils
    build arch                         : x86_64-unknown-linux-gnu

  RECOMMENDED FEATURES (should all be yes)
    gzip support                       : yes
    bzip2 support                      : yes
    lzma/xz support                    : yes
    libstdc++ demangle support         : yes
    File textrel check                 : yes
    Symbol versioning                  : yes

  NOT RECOMMENDED FEATURES (should all be no)
    Experimental thread safety         : no

  OTHER FEATURES
    Deterministic archives by default  : false
    Native language support            : yes

  EXTRA TEST FEATURES (used with make check)
    debug branch prediction            : no
    gprof support                      : no
    gcov support                       : no
    run all tests under valgrind       : no
    use rpath in tests                 : no
    test biarch                        : yes

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoUpdate elf.h from glibc.
Mark Wielaard [Tue, 28 Apr 2015 12:41:12 +0000 (14:41 +0200)]
Update elf.h from glibc.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoreadelf: Fix cie_offset calculation comparison on 32bit.
Mark Wielaard [Wed, 22 Apr 2015 10:47:46 +0000 (12:47 +0200)]
readelf: Fix cie_offset calculation comparison on 32bit.

gcc -fsanitize=undefined pointed out that on 32bit systems the calculation
to match the cie_offset to the cie_id could be undefined because a cie_id
could be an unsigned 64bit value while ptrdiff_t is only 32bits. Correct
the calculation to use 64bit values.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdw: Undefined behavior in get_sleb128_step.
Mark Wielaard [Wed, 22 Apr 2015 10:28:30 +0000 (12:28 +0200)]
libdw: Undefined behavior in get_sleb128_step.

gcc -fsanitize=undefined pointed out that for too big sleb128 values we
could shift into the sign bit. So for sleb128 values that have to fit
in a (signed) int64_t variable reduce the max number of steps by one.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c29

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoreadelf: Check all offsets used in print_gdb_index_section against d_size.
Mark Wielaard [Wed, 22 Apr 2015 09:44:32 +0000 (11:44 +0200)]
readelf: Check all offsets used in print_gdb_index_section against d_size.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c29

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoreadelf: Always try to print some debug section information.
Mark Wielaard [Fri, 17 Apr 2015 20:24:41 +0000 (22:24 +0200)]
readelf: Always try to print some debug section information.

Even if we cannot create a proper Dwarf dbg we can still print the
information of various debug sections. All all debug print section
functions already check first they can access the appropriate data.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoreadelf: Add overflow checking to print_gdb_index_section dataend checks.
Mark Wielaard [Fri, 17 Apr 2015 18:03:44 +0000 (20:03 +0200)]
readelf: Add overflow checking to print_gdb_index_section dataend checks.

https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c29

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoAllow disabling symbol versioning at configure time
Max Filippov [Thu, 23 Apr 2015 18:46:59 +0000 (20:46 +0200)]
Allow disabling symbol versioning at configure time

Due to missing symbol versioning support in uClibc calls to versioned
functions that internally call different version of themselves results
in infinite recursion.

Introduce macro SYMBOL_VERSIONING and use it instead of plain SHARED to
decide whether symbol versioning is needed. Control this macro
definition with new configure option --disable-symbol-versioning.

Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdw: Don't overflow stack with user defined macro attributes array.
Mark Wielaard [Tue, 21 Apr 2015 13:46:01 +0000 (15:46 +0200)]
libdw: Don't overflow stack with user defined macro attributes array.

In theory user defined debug macros can have an arbitrary number of
arguments. Don't allocate them all on stack. If there are more than
8 (arbitrary number, but no sane macro should have more arguments),
then dynamically allocate and free the attributes.

Found by gcc -fsanitize=undefined. Which pointed out the nforms could
be zero, creating an empty vla (which could cause undefined behavior).

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agosrc: Remove __DATE__ from internal error messages.
Mark Wielaard [Tue, 14 Apr 2015 09:59:36 +0000 (11:59 +0200)]
src: Remove __DATE__ from internal error messages.

The date didn't really add much information (it is the day on which the
code happened to be compiled) and it prevents creating reproducible builds.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoconfigure: Add explicit checks for all GNU99 extensions used.
Mark Wielaard [Tue, 14 Apr 2015 08:18:37 +0000 (10:18 +0200)]
configure: Add explicit checks for all GNU99 extensions used.

Some compilers (clang) claim to support -std=gnu99 but don't actually
implement all extensions we use in the code. Producing really hard to
parse errors. Add explicit checks for some of the other language
extensions we use, Nested Functions and Arrays of Variable Length,
to the configure check to catch such issues early.

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

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Correct off-by-one check in segment.c insert.
Mark Wielaard [Thu, 2 Apr 2015 11:39:03 +0000 (13:39 +0200)]
libdwfl: Correct off-by-one check in segment.c insert.

Caught by running run-backtrace-core-x32.sh under valgrind.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdwfl: Special case core_file_read_eagerly for small ELF images.
Mark Wielaard [Tue, 31 Mar 2015 09:33:53 +0000 (11:33 +0200)]
libdwfl: Special case core_file_read_eagerly for small ELF images.

Small ELF images, like linux-gate or linux-vdso, might be available in the
core file, but not on disk, even if we have a build-id. If the whole image
is small enough try to read them in from the core file to make sure symbols
and unwind information are always available for them. We would already map
them in if the core file was opened with ELF_C_READ_MMAP.

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

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoDo not reject type units in dwarf_getsrcfiles and dwarf_getsrclines
Petr Machata [Wed, 1 Apr 2015 19:44:32 +0000 (21:44 +0200)]
Do not reject type units in dwarf_getsrcfiles and dwarf_getsrclines

Signed-off-by: Petr Machata <pmachata@redhat.com>
9 years agotests: Add run-strip-test10.sh
H.J. Lu [Wed, 25 Mar 2015 16:19:08 +0000 (09:19 -0700)]
tests: Add run-strip-test10.sh

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
9 years agotests: Add run-strip-test9.sh
H.J. Lu [Wed, 25 Mar 2015 15:16:56 +0000 (08:16 -0700)]
tests: Add run-strip-test9.sh

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
9 years agotests: Add run-backtrace-core-x32.sh
H.J. Lu [Wed, 25 Mar 2015 14:22:43 +0000 (07:22 -0700)]
tests: Add run-backtrace-core-x32.sh

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
9 years agotests: Add testfile-x32 to run-addrcfi.sh
H.J. Lu [Wed, 25 Mar 2015 13:44:09 +0000 (06:44 -0700)]
tests: Add testfile-x32 to run-addrcfi.sh

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
9 years agotests: Add testfile-x32-core to run-allregs.sh
H.J. Lu [Wed, 25 Mar 2015 12:57:09 +0000 (05:57 -0700)]
tests: Add testfile-x32-core to run-allregs.sh

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
9 years agotests: Add testfile-x32-core.bz2 to run-readelf-mixed-corenote.sh
H.J. Lu [Wed, 25 Mar 2015 12:44:57 +0000 (05:44 -0700)]
tests: Add testfile-x32-core.bz2 to run-readelf-mixed-corenote.sh

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
9 years agobackends: Add x32_corenote.c
H.J. Lu [Thu, 12 Mar 2015 21:51:42 +0000 (14:51 -0700)]
backends: Add x32_corenote.c

This patch adds x32_corenote.c to support x32 coredump.  X32 coredump is
a hybrid between ia32 coredump and x86-64 coredump.  The exact formats
are described in bfd/hosts/x86-64linux.h in GNU binutils source tree.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
9 years agoPublish known-dwarf.h
Petr Machata [Wed, 18 Mar 2015 18:42:02 +0000 (19:42 +0100)]
Publish known-dwarf.h

- Make the macro names properly scoped (DWARF_ALL_KNOWN_* and
  DWARF_ONE_KNOWN_*).

- Simplify the calling convention: don't keep track of the comment
  associated with the enumerator, always use the non-_DESC
  DWARF_ONE_KNOWN_* callback.

- Install known-dwarf.h alongside libdw.h and others.

Signed-off-by: Petr Machata <pmachata@redhat.com>
9 years agolibelf: Don't extend ELF file size with SHT_NOBITS sh_offset.
Mark Wielaard [Mon, 23 Mar 2015 21:19:12 +0000 (22:19 +0100)]
libelf: Don't extend ELF file size with SHT_NOBITS sh_offset.

Don't explicitly extend the file size for SHT_NOBITS sections. Since
that could cause a size beyond any actual file content it will cause
issues when the underlying ELF file has been mmapped or will extend
the file size to increase (writing fill bytes) when not mmapped. The
sh_offset value is essentially meaningless for SHT_NOBITS. gabi says
that a NOBITS section sh_offset member locates the "conceptual
placement" in the file. But it doesn't say this cannot be beyond the
enf of the file. When ELF_F_LAYOUT is set we should trust sh_offset
as given is what is wanted for an SHT_NOBITS section without extending
the file size.

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

Buggy binutils ld could generate files where SHT_NOBITS sections have
sh_offset outside the file.
https://sourceware.org/bugzilla/show_bug.cgi?id=12921

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agobackends: aarch64 can have _GLOBAL_OFFSET_TABLE_ point anywhere in .got.
Mark Wielaard [Mon, 23 Mar 2015 14:08:27 +0000 (15:08 +0100)]
backends: aarch64 can have _GLOBAL_OFFSET_TABLE_ point anywhere in .got.

When building with relro aarch64 splits .got in a writable and read-only
part. _GLOBAL_OFFSET_TABLE_ points to the first writable symbol, not the
start of .got.

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

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoAdd forgotten files to EXTRA_DIST
Petr Machata [Wed, 18 Mar 2015 18:35:16 +0000 (19:35 +0100)]
Add forgotten files to EXTRA_DIST

Signed-off-by: Petr Machata <pmachata@redhat.com>
9 years agoFix -Wimplicit warnings.
Mark Wielaard [Fri, 13 Mar 2015 22:51:40 +0000 (23:51 +0100)]
Fix -Wimplicit warnings.

9 years agotests: Use PTRACE_GETREGS/PTRACE_SETREGS to update rip
H.J. Lu [Thu, 12 Mar 2015 19:14:43 +0000 (12:14 -0700)]
tests: Use PTRACE_GETREGS/PTRACE_SETREGS to update rip

Use PTRACE_POKEUSER to update rip is unsupported for x32.  We should use
PTRACE_GETREGS/PTRACE_SETREGS to update rip.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
9 years agobackends: COPY relocations can also happen in ET_DYN.
Mark Wielaard [Mon, 9 Mar 2015 21:43:08 +0000 (22:43 +0100)]
backends: COPY relocations can also happen in ET_DYN.

In particular COPY relocations might happen in PIE executables,
which are ET_DYN.

Found on Fedora Rawhide with run-elflint-self.sh.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoelflint: Correct gnu_symbias usage in compare_hash_gnu_hash.
Mark Wielaard [Mon, 9 Mar 2015 15:41:48 +0000 (16:41 +0100)]
elflint: Correct gnu_symbias usage in compare_hash_gnu_hash.

commit 0a545e "elflint: Add various low-level checks." introduced a bug
in the handling of the gnu_symbias. The symbol in the bucket should first
be compared to STN_UNDEF before applying the bias. And the used symbol
index should have the bias added when compared against plain hash symbol.

Found on Debian wheezy with run-elflint-self.sh.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agoDrop two useless variables from known-dwarf.awk
Petr Machata [Fri, 20 Feb 2015 19:05:29 +0000 (20:05 +0100)]
Drop two useless variables from known-dwarf.awk

Signed-off-by: Petr Machata <pmachata@redhat.com>
9 years agoSupport STT_ARM_TFUNC symbol type in ebl_symbol_type_name hook
Petr Machata [Tue, 24 Feb 2015 00:08:25 +0000 (01:08 +0100)]
Support STT_ARM_TFUNC symbol type in ebl_symbol_type_name hook

Signed-off-by: Petr Machata <pmachata@redhat.com>
9 years agolibelf: Make __libelf_set_data_list_rdlock an internal_function.
Mark Wielaard [Wed, 18 Feb 2015 19:51:40 +0000 (20:51 +0100)]
libelf: Make __libelf_set_data_list_rdlock an internal_function.

__libelf_set_data_list_rdlock from elf_getdata.c is marked as an
internal_function in the implementation, but not in libelfP.h when it
is declared.  Add internal_function to the declaration. This broke
the i686 build.

Reported-by: Alexander Cherepanov <ch3root@openwall.com>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agotests: Use PRId64 for printing loff_t.
Mark Wielaard [Wed, 18 Feb 2015 23:43:59 +0000 (00:43 +0100)]
tests: Use PRId64 for printing loff_t.

loff_t is always 64bits.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdw: Add DWARF5/GCC5 DW_LANG_Fortran03 and DW_LANG_Fortran08 support.
Mark Wielaard [Mon, 9 Feb 2015 15:47:08 +0000 (16:47 +0100)]
libdw: Add DWARF5/GCC5 DW_LANG_Fortran03 and DW_LANG_Fortran08 support.

GCC5 adds support for the new Fortran 2003 and 2008 standard DWARF5
language codes. Add them to dwarf.h and handle them in dwarf_aggregate_size.

http://dwarfstd.org/ShowIssue.php?issue=141121.1

Also documented at https://fedorahosted.org/elfutils/wiki/DwarfExtensions

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agolibdw: Add support for DWARF5/GCC5 DW_TAG_atomic_type.
Mark Wielaard [Mon, 9 Feb 2015 15:24:04 +0000 (16:24 +0100)]
libdw: Add support for DWARF5/GCC5 DW_TAG_atomic_type.

GCC5 adds support for the new C11 _Atomic DWARF5 DW_TAG_atomic_type
qualifier. Add it to dwarf.h and handle it in dwarf_peel_type.

http://dwarfstd.org/ShowIssue.php?issue=131112.1

Also documented at https://fedorahosted.org/elfutils/wiki/DwarfExtensions

Signed-off-by: Mark Wielaard <mjw@redhat.com>
9 years agotests: ensure backtrace.c exec_dump sets jmp
Josh Stone [Wed, 11 Feb 2015 22:25:37 +0000 (14:25 -0800)]
tests: ensure backtrace.c exec_dump sets jmp

With CFLAGS='-Og -g', F21 gcc hits -Werror=maybe-uninitialized on jmp in
the call "prepare_thread (pid2, jmp)".  It's fine with -O2/-O0.

The only way this could be unset is if the loop before ran to
completion, and there's already an assert against this case.

This patch initializes jmp anyway to convince gcc it's ok.

Signed-off-by: Josh Stone <jistone@redhat.com>