platform/upstream/elfutils.git
5 years agolibelf: Fix possible resource leak in elf[32|64]_updatefile.
Mark Wielaard [Wed, 6 Mar 2019 18:56:54 +0000 (19:56 +0100)]
libelf: Fix possible resource leak in elf[32|64]_updatefile.

When we cannot allocate enough memory to convert the data in
updatemmap we should free the scns before returning an error.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agotests: Remove tgkill define from backtrace.c
Mark Wielaard [Mon, 4 Mar 2019 19:10:35 +0000 (20:10 +0100)]
tests: Remove tgkill define from backtrace.c

tgkill is never used in the test and it might conflict with newer
glibc which might define tgkill themselves (just like gettid):
https://sourceware.org/bugzilla/show_bug.cgi?id=6399

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Remove unused __elf_xfctstof.
Mark Wielaard [Sat, 23 Feb 2019 19:44:24 +0000 (20:44 +0100)]
libelf: Remove unused __elf_xfctstof.

__elf_xfctstof is only used in case the memory size and file size of ELF
data structures are different. This is never the case.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Remove unused internal __elf[32|64]_msize functions.
Mark Wielaard [Thu, 21 Feb 2019 23:28:01 +0000 (00:28 +0100)]
libelf: Remove unused internal __elf[32|64]_msize functions.

Those functions were intended for ELF versions where the memory and
file sizes of data structures are different. They were never used
because libelf depends on the file and memory sizes being equal
(otherwise using mmap wouldn't work).

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: There is just one ELF version.
Mark Wielaard [Sun, 3 Feb 2019 18:05:13 +0000 (19:05 +0100)]
libelf: There is just one ELF version.

Remove (partially defined out) code and data structures dealing with
multiple ELF versions. There hasn't been a new ELF version in the
last 20 years. Simplify the code a bit by just assuming there will
only be one version (EV_CURRENT == 1).

Simplifies elf_version, gets rid of __libelf_version_initialized.
Removes one (or more) array (version) dimension from various tables
and accessor functions (__elf_xfctstom, shtype_map, __libelf_data_type,
__libelf_type_aligns and __libelf_type_sizes).

Signed-off-by: Mark Wielaard <mark@klomp.org>`
5 years agolibdwfl: Initialize notes early in intuit_kernel_bounds.
Mark Wielaard [Sun, 3 Feb 2019 18:03:26 +0000 (19:03 +0100)]
libdwfl: Initialize notes early in intuit_kernel_bounds.

We fake initialization of notes with an empty asm statement.  But
it is simpler and less confusing to just initialize notes just
before the fopen.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Handle zero size decompressed data.
Mark Wielaard [Tue, 19 Feb 2019 15:27:40 +0000 (16:27 +0100)]
libelf: Handle zero size decompressed data.

This is a corner case that will most likely never occur in practice,
but we have several testcases that compress and decompress zero sized
data. In that case during decompression we might malloc a buffer of
size zero. It is allowed for malloc to return NULL in that case. But
we do need a non-NULL buffer to return and set as result. So make sure
to always at least allocate one byte. Also make sure that we don't
allocate a zero sized conversion buffer for the data.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agos390: elflint should check if _GLOBAL_OFFSET_TABLE_ points to .got. elfutils-0.176
Mark Wielaard [Fri, 15 Feb 2019 13:39:57 +0000 (14:39 +0100)]
s390: elflint should check if _GLOBAL_OFFSET_TABLE_ points to .got.

The _GLOBAL_OFFSET_TABLE_ symbol might point to the DT_PLTGOT,
which is in the .got section, even if the symbol itself is
associated with the .got.plt section.

See https://sourceware.org/ml/binutils/2018-07/msg00200.html

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoPrepare for 0.176
Mark Wielaard [Thu, 14 Feb 2019 12:29:39 +0000 (13:29 +0100)]
Prepare for 0.176

Set version to 0.176.
Update NEWS and elfutils.spec.in.
Update GPG-KEY.
Regenerate po/*.po files.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Make sure ar_size is terminated when reading ar long names.
Mark Wielaard [Thu, 14 Feb 2019 10:47:59 +0000 (11:47 +0100)]
libelf: Make sure ar_size is terminated when reading ar long names.

The ar_size is given as a fixed size decimal string, right padded with
spaces.  Make sure we read it properly even if there is no terminating
space. Also sanity check len early if we can.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibdw: Check there is enough space for CU 64bit length, version and type.
Mark Wielaard [Fri, 1 Feb 2019 13:03:38 +0000 (14:03 +0100)]
libdw: Check there is enough space for CU 64bit length, version and type.

We only checked we could read the initial length and after knowing the
version and type whether the unit header was the right size. Also check
there are at least enough bytes to read the 64bit length, version and
unit type bytes.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoreadelf: Check there is enough data to read DWARF line opcodes arguments.
Mark Wielaard [Fri, 1 Feb 2019 08:08:14 +0000 (09:08 +0100)]
readelf: Check there is enough data to read DWARF line opcodes arguments.

When reading the debug_line opcode arguments we have to make sure there
is enough data to read the arguments (if there are any(.

The similar code in dwarf_getsrclines already had these checks.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoconfigure: Add new --enable-install-elfh option.
Mark Wielaard [Fri, 18 Jan 2019 13:18:22 +0000 (14:18 +0100)]
configure: Add new --enable-install-elfh option.

We explicitly test (with system-elf-libelf) that our include headers
work with the system elf.h header. But it might be helpful to install
the elf.h file for a private install. Our elf.h header really is just
a copy of the latest glibc elf.h. But it might be newer and include
more constants than the system installed elf.h.

Add a new configure option --enable-install-elfh to install elf.h.
But warn when it is enabled for the default /usr or /usr/local prefix
because it might clash with the glibc/system elf.h header in that case.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agotests: Remove assert (errno == 0) from tests.
Mark Wielaard [Thu, 31 Jan 2019 22:18:25 +0000 (23:18 +0100)]
tests: Remove assert (errno == 0) from tests.

When a function fails it might set errno. But it isn't a guarantee
that if a function succeeds that it sets errno to zero.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agotests: parse inode in /proc/pid/maps/correctly in run-backtrace-data.sh
Yonghong Song [Tue, 29 Jan 2019 21:33:03 +0000 (13:33 -0800)]
tests: parse inode in /proc/pid/maps/correctly in run-backtrace-data.sh

The backtrace-data.c parsed the inode in /proc/pid/maps with
format "%*x".
This caused failure if inode is big. For example,
  7f269223d000-7f269226b000 r-xp 00000000 00:50 10224326387095067468       /home/...

The error likes below:
  -bash-4.4$ cat run-backtrace-data.sh.log
  backtrace-data: /home/engshare/elfutils/0.174/src/elfutils-0.174/tests/backtrace-data.c:110:
    maps_lookup: Assertion `errno == 0' failed.
  /home/engshare/elfutils/0.174/src/elfutils-0.174/tests/test-subr.sh: line 84:
    3123578 Aborted                 (core dumped)
    LD_LIBRARY_PATH="${built_library_path}${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH" $VALGRIND_CMD "$@"
  data: no main
  -bash-4.4$
The reason is errno is ERANGE.

Fix the test with inode format string "%*u" as inode here is presented
as decimal numbers.

Suggested-by: Mark Wielaard <mark@klomp.org>
Signed-off-by: Yonghong Song <yhs@fb.com>
5 years agolibebl: Check GNU property note data padding fits inside note.
Mark Wielaard [Tue, 29 Jan 2019 23:04:11 +0000 (00:04 +0100)]
libebl: Check GNU property note data padding fits inside note.

The GNU property note data is padded. Make sure the extra padding
still fits in the note description.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibdwfl: parse inode in /proc/pid/maps correctly
Yonghong Song [Fri, 25 Jan 2019 21:20:08 +0000 (13:20 -0800)]
libdwfl: parse inode in /proc/pid/maps correctly

The inode number in /proc/pid/maps is displayed as "unsigned long"
type.

In one of our x64 system, we have inode number exceeding valid "long"
type range, which caused the following test failure:
   FAIL: dwfl-bug-fd-leak
   FAIL: run-backtrace-dwarf.sh
   FAIL: vdsosyms

The offending map entry:
  7f269246b000-7f269246c000 rw-p 0002e000 00:50 10224326387095067468 /home/...

This patch changed sscanf inode number type from PRIi64 to PRIu64
and fixed the problem.

Signed-off-by: Yonghong Song <yhs@fb.com>
5 years agotests: Use separate files for strip outputs
Ulf Hermann [Thu, 17 Jan 2019 14:00:46 +0000 (15:00 +0100)]
tests: Use separate files for strip outputs

This way we can verify that strip actually created all of them.
Also, check that strip, unstrip, elflint actually succeed in
run-strip-test-many.sh. This exposed the fact that the generated
testfile in run-strip-test-many.sh is considered as illformed by
strip -g.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
5 years agostrip: Fix check test for SHN_XINDEX symbol.
Mark Wielaard [Thu, 24 Jan 2019 15:00:49 +0000 (16:00 +0100)]
strip: Fix check test for SHN_XINDEX symbol.

The check for whether a symbol used the extended section table was
wrong causing the run-strip-test-many.sh testcase to declare the
testfile was an illformed file.

Fixing this exposed a strict elfutils check for the '.shstrtab'
section having this exact name and a SHT_STRTAB type. This might
be a little too strict, but easily worked around by changing the
name of the "old" shstrtab section in the addsections program.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoreadelf: Don't go past end of line data reading unknown opcode parameters.
Mark Wielaard [Tue, 22 Jan 2019 14:55:18 +0000 (15:55 +0100)]
readelf: Don't go past end of line data reading unknown opcode parameters.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibdw: Check terminating NUL byte in dwarf_getsrclines for dir/file table.
Mark Wielaard [Sun, 20 Jan 2019 21:10:18 +0000 (22:10 +0100)]
libdw: Check terminating NUL byte in dwarf_getsrclines for dir/file table.

For DWARF version < 5 the .debug_line directory and file tables consist
of a terminating NUL byte after all strings. The code used to just skip
this without checking it actually existed. This could case a spurious
read past the end of data.

Fix the same issue in readelf.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibdwfl: Sanity check partial core file dyn data read.
Mark Wielaard [Sun, 20 Jan 2019 22:05:56 +0000 (23:05 +0100)]
libdwfl: Sanity check partial core file dyn data read.

When reading the dyn data from the core file check if we got everything,
or just part of the data.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibebl: Check NT_PLATFORM core notes contain a zero terminated string.
Mark Wielaard [Wed, 16 Jan 2019 14:41:31 +0000 (15:41 +0100)]
libebl: Check NT_PLATFORM core notes contain a zero terminated string.

Most strings in core notes are fixed size. But NT_PLATFORM contains just
a variable length string. Check that it is actually zero terminated
before passing to readelf to print.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Correct overflow check in note_xlate.
Mark Wielaard [Wed, 16 Jan 2019 11:25:57 +0000 (12:25 +0100)]
libelf: Correct overflow check in note_xlate.

We want to make sure the note_len doesn't overflow and becomes shorter
than the note header. But the namesz and descsz checks got the note header
size wrong). Replace the wrong constant (8) with a sizeof cvt_Nhdr (12).

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibebl: Check GNU property note pr_datasz fits inside note description.
Mark Wielaard [Wed, 16 Jan 2019 10:57:35 +0000 (11:57 +0100)]
libebl: Check GNU property note pr_datasz fits inside note description.

Before printing the data values, make sure pr_datasz doesn't go beyond
the end of the note description data.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoSkip run-readelf-compressed.sh test if built without bzip2
Ulf Hermann [Sun, 13 Jan 2019 22:14:35 +0000 (23:14 +0100)]
Skip run-readelf-compressed.sh test if built without bzip2

Obviously, we cannot read the compressed ELF file if no bzip2 support is
present.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
5 years agoRISC-V: Add untested 32-bit core file support.
Jim Wilson [Thu, 27 Dec 2018 23:27:02 +0000 (15:27 -0800)]
RISC-V: Add untested 32-bit core file support.

Adds 32-bit support exactly the same way that the sparc backend handles
32- and 64-bit core file support.  The 64-bit core file support was tested
and still works same as before.

Signed-off-by: Jim Wilson <jimw@sifive.com>
5 years agoRISC-V: Add initial return value location support.
Jim Wilson [Thu, 27 Dec 2018 23:26:11 +0000 (15:26 -0800)]
RISC-V: Add initial return value location support.

Started with the aarch64 support and modified it for RISC-V.  The
flattened structure support hasn't been written yet, but the rest of
it should be correct for the LP64D ABI.  We have potentially 6 different
ABIs to support, so this requires checking elf header flags in riscv_init
when setting the hook.

Signed-off-by: Jim Wilson <jimw@sifive.com>
5 years agoRISC-V: Improve riscv64 core file support.
Jim Wilson [Thu, 27 Dec 2018 23:25:49 +0000 (15:25 -0800)]
RISC-V: Improve riscv64 core file support.

This fixes two problems.  The offset for x1 is changed from 1 to 8 because
this is a byte offset not a register skip count.  Support for reading the
PC value is added.  This requires changing the testsuite to match the new
readelf output for coredumps.

Signed-off-by: Jim Wilson <jimw@sifive.com>
5 years agolibebl: Fix reading GNU_PROPERTY_STACK_SIZE reading from 32bit notes.
Mark Wielaard [Sun, 2 Dec 2018 23:03:39 +0000 (00:03 +0100)]
libebl: Fix reading GNU_PROPERTY_STACK_SIZE reading from 32bit notes.

When reading a GNU_PROPERTY_STACK_SIZE we need to use the proper data
type. GElf_Addr is 64bit always and when reading a 32bit size part of
it would not be initialized. Use either Elf32_Addr or Elf64_Addr to
read and print the data.

Add 32bit and 64bit, little and big endian testcases.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agotests: Improve backtrace-data SKIP message.
Mark Wielaard [Wed, 28 Nov 2018 12:58:31 +0000 (13:58 +0100)]
tests: Improve backtrace-data SKIP message.

The backtrace-data testcase is x86_64 linux only because it uses its
own set_initial_registers and scans its own /proc/pid/maps file.
The SKIP message it gave on other platforms was misleading. It said
"Unwinding not supported for this architecture". Change it to
"x86_64 linux only test" to be less confusing.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibdwfl: Fix relocation overlap sanity check.
Mark Wielaard [Fri, 23 Nov 2018 20:10:27 +0000 (21:10 +0100)]
libdwfl: Fix relocation overlap sanity check.

We would not relocate when the relocation section data or the target
section date would overlap with one of the ELF headers. This is only
really necessary if the data comes directly from the mmapped file.
Otherwise there is no real overlap and the relocations can be safely
applied.

One particular thing we got wrong with the original sanity check was
when the relocation data or target data section was compressed. In
that case it could happen we overestimated the size (because the Shdr
would have been updated to show the uncompressed data size). But
uncompressed data is always malloced and so cannot overlap with the
mmapped Elf header structures.

When building with CFLAGS="-g -Og" this showed up as a failure in
run-strip-reloc.sh for strip-compressed.o. Where the .debug_loc
section decompressed would "overlap" with the shdrs at the end of
the file and so wouldn't get relocations applied.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibdw: Enable building with -Og.
Mark Wielaard [Fri, 23 Nov 2018 19:50:51 +0000 (20:50 +0100)]
libdw: Enable building with -Og.

When building with -Og gcc is unable to see that __libdw_dieabbrev ()
will initialize what the second argument points to when no error occurs
as called by dwarf_child and dwarf_getattrs. Causing an possibly
uninitialized error. Just initialize readp/die_addr to NULL, which is
the value we would return if an error occurs anyway.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agotests: Call test_cleanup in backtrace-subr.sh check_unsupported.
Mark Wielaard [Wed, 21 Nov 2018 19:07:14 +0000 (20:07 +0100)]
tests: Call test_cleanup in backtrace-subr.sh check_unsupported.

We want to make sure all (temporary) test files are cleaned up even when
we exit 77 to skip the testcase.

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

Tested-by: Kurt Roeckx <kurt@roeckx.be>
Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Get alignment correct when calling conversion functions.
Mark Wielaard [Sat, 17 Nov 2018 23:21:49 +0000 (00:21 +0100)]
libelf: Get alignment correct when calling conversion functions.

When writing out data that needs to be converted we have to make sure
the conversion function is called on correctly aligned buffers. When
using mmap this might mean we have to convert into a temporarily buffer
if the user wants to write out the section at a location that is not
correctly aligned for the section type.

Older gas would generate misaligned ELF notes for the .version
directive. When copying over such notes using mmap from files with
a different endianness using mmap we would get the alignment of the
conversion destination wrong.

The new testcase would fail with configure --enable-sanitize-undefined
on little endian systems. The GCC undefinited sanitizer caught a similar
issue with testfile1 on big endian systems.

gelf_xlate.h:47:1: runtime error: member access within misaligned address
0x7f8145d770d5 for type 'struct Elf32_Nhdr', which requires 4 byte alignment

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoAdd -Wtrampolines to CFLAGS.
Mark Wielaard [Mon, 19 Nov 2018 10:46:46 +0000 (11:46 +0100)]
Add -Wtrampolines to CFLAGS.

elfutils uses nested functions a lot. This is fine unless one takes the
address of such a nested function. When taking the address of a nested
function a trampoline is generated that on some systems require the stack
to be executable. That is bad. We never want to generate such trampolines.
Add -Wtrampolines to CLAGS to make sure we don't.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoconfig/upload-release.sh: Need to make before make dist.
Mark Wielaard [Fri, 16 Nov 2018 11:52:00 +0000 (12:52 +0100)]
config/upload-release.sh: Need to make before make dist.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoUpdate upload-release.sh script and po files. elfutils-0.175
Mark Wielaard [Fri, 16 Nov 2018 11:44:53 +0000 (12:44 +0100)]
Update upload-release.sh script and po files.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Mark both fsize and msize with const attribute.
Mark Wielaard [Fri, 16 Nov 2018 08:27:00 +0000 (09:27 +0100)]
libelf: Mark both fsize and msize with const attribute.

GCC9 -Wmissing-attributes pointed out that although we alias the fsize
and msize functions only fsize was marked as const. Fix by also marking
the msize definition as const.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibebl: Don't update w, t and len unnecessarily in ebl_object_note_type_name.
Mark Wielaard [Thu, 15 Nov 2018 14:53:41 +0000 (15:53 +0100)]
libebl: Don't update w, t and len unnecessarily in ebl_object_note_type_name.

Harmless, but useless.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoPrepare for 0.175
Mark Wielaard [Wed, 14 Nov 2018 12:12:24 +0000 (13:12 +0100)]
Prepare for 0.175

Set version to 0.175
Update NEWS and elfutils.spec.in.
Regenerate po/*.po files.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoelflint: Allow PT_GNU_EH_FRAME segment to match SHT_X86_64_UNWIND section.
Mark Wielaard [Sat, 10 Nov 2018 22:33:03 +0000 (23:33 +0100)]
elflint: Allow PT_GNU_EH_FRAME segment to match SHT_X86_64_UNWIND section.

The gold linker might generate an .eh_frame_hdr with a SHT_X86_64_UNWIND
type instead of a SHT_PROGBITS type.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Correctly setup alignment of SHF_COMPRESSED section data.
Mark Wielaard [Tue, 13 Nov 2018 20:18:09 +0000 (21:18 +0100)]
libelf: Correctly setup alignment of SHF_COMPRESSED section data.

We didn't set the alignment of SHF_COMPRESSED sections correctly.
Those sections start with an Elf(32|64)_Chdr. Make sure sh_addralign
is setup to be able to read such a struct directly. Likewise don't
trust the alignment set on any SHF_COMPRESSED section, but always
make the (raw) compressed data aligned correctly for the reading the
Elf(32|64)_Chdr directly.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agostrip: Also handle gnu compressed debug sections with --reloc-debug-sections
Mark Wielaard [Fri, 9 Nov 2018 14:19:07 +0000 (15:19 +0100)]
strip: Also handle gnu compressed debug sections with --reloc-debug-sections

Check whether a section was gnu compressed and decompress it first
before trying to resolve relocations. Recompress it afterwards.

This found a bug in elf_compress_gnu which would use the "raw" file
contents even if the user had just created the section (copying over
the section from the original input file).

Add compressed ET_REL tests to run-strip-reloc.sh testcase.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoHandle GNU Build Attribute ELF Notes.
Mark Wielaard [Mon, 12 Nov 2018 22:34:24 +0000 (23:34 +0100)]
Handle GNU Build Attribute ELF Notes.

GNU Build Attribute ELF Notes are generated by the GCC annobin plugin
and described at https://fedoraproject.org/wiki/Toolchain/Watermark

Unfortunately the constants aren't yet described in the standard glibc
elf.h so they have been added to the elfutils specific elf-knowledge.h.

The notes abuse the name owner field to encode some data not in the
description. This makes it a bit hard to parse. We have to match the
note owner name prefix (to "GA") to be sure the type is valid. We also
cannot rely on the owner name being a valid C string since the attribute
name and value can contain zero (terminators). So pass around namesz
to the ebl note parsing functions.

eu-elflint will recognize and eu-readelf -n will now show the notes:

Note section [27] '.gnu.build.attributes' of 56080 bytes at offset 0x114564:
  Owner          Data size  Type
  GA                    16  GNU Build Attribute OPEN
    Address Range: 0x2f30f - 0x2f30f
    VERSION: "3p8"
  GA                     0  GNU Build Attribute OPEN
    TOOL: "gcc 8.2.1 20180801"
  GA                     0  GNU Build Attribute OPEN
    "GOW": 45
  GA                     0  GNU Build Attribute OPEN
    STACK_PROT: 0
  GA                     0  GNU Build Attribute OPEN
    "stack_clash": TRUE
  GA                     0  GNU Build Attribute OPEN
    "cf_protection": 0
  GA                     0  GNU Build Attribute OPEN
    "GLIBCXX_ASSERTIONS": TRUE
  GA                     0  GNU Build Attribute OPEN
    "FORTIFY": 0
  GA                     0  GNU Build Attribute OPEN
    PIC: 3
  GA                     0  GNU Build Attribute OPEN
    SHORT_ENUM: FALSE
  GA                     0  GNU Build Attribute OPEN
    ABI: c001100000012
  GA                     0  GNU Build Attribute OPEN
    "stack_realign": FALSE

A new test was added to run-readelf -n for the existing annobin file.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoRecognize NT_VERSION notes.
Mark Wielaard [Sun, 11 Nov 2018 22:50:41 +0000 (23:50 +0100)]
Recognize NT_VERSION notes.

NT_VERSION notes are emitted by the gas .version directive.
They have an empty description and (ab)use the owner name to store the
version data string.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibcpu: Recognize bpf jump variants BPF_JLT, BPF_JLE, BPF_JSLT and BPF_JSLE
Mark Wielaard [Sun, 4 Nov 2018 20:34:38 +0000 (21:34 +0100)]
libcpu: Recognize bpf jump variants BPF_JLT, BPF_JLE, BPF_JSLT and BPF_JSLE

Linux kernel 4.13 introduced 4 more jump class variants.

  commit 92b31a9af73b3a3fc801899335d6c47966351830
  Author: Daniel Borkmann <daniel@iogearbox.net>
  Date:   Thu Aug 10 01:39:55 2017 +0200

    bpf: add BPF_J{LT,LE,SLT,SLE} instructions

For conditional jumping on unsigned and signed < and <= between a register
and another register or immediate.

Add these new constants to bpf.h, recognize them in bpf_disasm and update
the testfile-bpf-dis1.expect file.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibdwelf: New function dwelf_elf_begin.
Mark Wielaard [Sun, 21 Oct 2018 21:41:32 +0000 (23:41 +0200)]
libdwelf: New function dwelf_elf_begin.

This introduces a new function dwelf_elf_begin which creates a (read-only)
ELF handle from a possibly compressed file handle or a file that start
with a linux kernel header. This can be used in eu-readelf to (re)open a
(pure) ELF.

eu-readelf uses libdwfl to relocate addresses in the original file in
case it is ET_REL. But to show the "raw" data it might need to (re)open
the file. Which could fail if the file was compressed. And produced an
obscure error message: "cannot create EBL handle".

This rewrites __libdw_open_file a little so that the given file handle
will never be closed (whether on success or failure) and introduces a
new internal function __libdw_open_elf that dwelf_elf_begin wraps.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agobackends: Add x86_64 section_type_name for SHT_X86_64_UNWIND.
Mark Wielaard [Tue, 6 Nov 2018 11:01:25 +0000 (12:01 +0100)]
backends: Add x86_64 section_type_name for SHT_X86_64_UNWIND.

Makes sure that eu-readelf and eu-elflint recognize and show the
x86_64 specific section type correctly.

Signed-off-by: Mark Wielaard <mark@klomp.org>
Tested-by: Milian Wolff <milian.wolff@kdab.com>
5 years agoAlso find CFI in sections of type SHT_X86_64_UNWIND
Milian Wolff [Mon, 29 Oct 2018 15:21:26 +0000 (16:21 +0100)]
Also find CFI in sections of type SHT_X86_64_UNWIND

On my system with g++ (GCC) 8.2.1 20180831 with GNU gold (GNU Binutils
2.31.1) 1.16, the .eh_frame section does not have type PROGBITS
but rather is using X86_64_UNWIND nowadays:

```
$ echo "int main(){ return 0; }" > test.c
$ gcc test.c
$ readelf --sections a.out | grep .eh_frame
  [14] .eh_frame         X86_64_UNWIND    0000000000000670  00000670
  [15] .eh_frame_hdr     X86_64_UNWIND    0000000000000724  00000724
```

Without this patch, libdw refuses to use the available unwind
information, leading to broken backtraces while unwinding. With the
patch applied, unwinding works once more in such situations.

Signed-off-by: Milian Wolff <milian.wolff@kdab.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>
Tested-by: Milian Wolff <milian.wolff@kdab.com>
5 years agolibelf: Explicitly update section data after (de)compression.
Mark Wielaard [Fri, 9 Nov 2018 08:18:22 +0000 (08:18 +0000)]
libelf: Explicitly update section data after (de)compression.

We need to explictly trigger a section data reload after updating the
ELF section rawdata to make sure it gets written out to disk on an
elf_update. Doing this showed one bug/inefficiently when the underlying
file has a different endianness. In that case for debug sections we
would convert by allocating a new buffer and just copying over the
raw data into a new buffer. This is not really necessary and would
hide any relocations done on the rawdata by libdwfl.

Added a couple of new ppc64 big endian testfiles that show the issue.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agostrip: Add --reloc-debug-sections-only option.
Mark Wielaard [Thu, 25 Oct 2018 11:35:25 +0000 (13:35 +0200)]
strip: Add --reloc-debug-sections-only option.

This option does the same thing as --reloc-debug-sections without doing
any other strip operation. This is useful when you want to remove the
debug section relocations in a separate ET_REL debug file that was created
without --reloc-debug-sections, or for a file (like the linux debug vmlinux)
that you don't want to strip, but for which the debug section relocations
can be resolved already.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agostrip: Extract code to update shdrstrndx into new common function.
Mark Wielaard [Thu, 25 Oct 2018 14:55:18 +0000 (16:55 +0200)]
strip: Extract code to update shdrstrndx into new common function.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agostrip: Split out debug section relocation into separate helper functions.
Mark Wielaard [Thu, 25 Oct 2018 11:30:19 +0000 (13:30 +0200)]
strip: Split out debug section relocation into separate helper functions.

Extract a couple of helper functions out of handle_elf (secndx_name,
get_xndxdata and remove_debug_relocations) so they can be reused more
easily in the future.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agostrip: Always copy over any phdrs if there are any.
Mark Wielaard [Thu, 25 Oct 2018 10:37:04 +0000 (12:37 +0200)]
strip: Always copy over any phdrs if there are any.

Ignore the type of ELF file, just copy over any phdrs if the original
file contained any. Also refuse to move around any allocated sections
based on whether there are any phdrs instead of on ELF file type.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agounstrip: Add ELF_CHECK to make sure gelf_getehdr () doesn't return NULL.
Mark Wielaard [Fri, 2 Nov 2018 22:17:05 +0000 (23:17 +0100)]
unstrip: Add ELF_CHECK to make sure gelf_getehdr () doesn't return NULL.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoRecognize and parse GNU Property notes.
Mark Wielaard [Mon, 15 Oct 2018 21:35:47 +0000 (23:35 +0200)]
Recognize and parse GNU Property notes.

GNU Property notes are different from normal notes because they use
variable alignment/padding of their fields. They are 8 byte aligned,
but use 4 byte fields. The name is aligned at 4 bytes and padded so
that, the desc is aligned at 8 bytes. The whole note is padded to
8 bytes again. For normal notes all fields are both 4 bytes wide and
4 bytes aligned.

To recognize these new kind of ELF Notes a new Elf_Type is introduced,
ELF_T_NHDR8. This type is used in the xlate functions to determine
how to align and pad the various fields. Since the fields themselves
can now have different alignments we will have to keep track of the
current alignement and use either NOTE_ALIGN4 or NOTE_ALIGN8 to
determine the padding.

To set the correct Elf_Type on the Elf_Data we use either the section
sh_addralign or the segment p_align values. Assuming 8 means the
section or segment contains the new style notes, otherwise normal
notes.

When we cannot determine the "alignment" directly, like when parsing
special kernel sys files, we check the name "GNU" and type
"GNU_PROPERTY_TYPE_0" fields.

ebl_object_note now parses the new NT_GNU_PROPERTY_TYPE_0 and can
extract the GNU_PROPERTY_STACK_SIZE, GNU_PROPERTY_NO_COPY_ON_PROTECTED
and GNU_PROPERTY_X86_FEATURE_1_AND types GNU_PROPERTY_X86_FEATURE_1_IBT
and GNU_PROPERTY_X86_FEATURE_1_SHSTK.

Tests are added for extracting the note from sections or segments
as set by gcc -fcf-protection.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoaddr2line: Use elf_getshdrstrndx not Ehdr field to print section name.
Mark Wielaard [Wed, 24 Oct 2018 14:15:04 +0000 (16:15 +0200)]
addr2line: Use elf_getshdrstrndx not Ehdr field to print section name.

Using the Ehdr field directly doesn't work when there are a large number
of sections.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoreadelf: Use shstrndx to lookup section names.
Mark Wielaard [Wed, 24 Oct 2018 13:59:32 +0000 (15:59 +0200)]
readelf: Use shstrndx to lookup section names.

The function section_name would use the Ehdr e_shstrndx field to find the
index of the section index string table directly. But it should use
elf_getshdrstrndx. Adjust all callers.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agobackends: ppc use define instead of const for size of dwarf_regs array.
Mark Wielaard [Sat, 20 Oct 2018 11:20:30 +0000 (13:20 +0200)]
backends: ppc use define instead of const for size of dwarf_regs array.

The size of the dwarf_regs is a constant, but when building without
optimizations the compiler doesn't see that and will warn that it
cannot proof the stack size is bounded. Use a define instead of a
const, so the compiler will use a constant expression everywhere.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoreadelf: Make sure readp is smaller than cieend in print_debug_frame_section.
Mark Wielaard [Tue, 16 Oct 2018 12:22:33 +0000 (14:22 +0200)]
readelf: Make sure readp is smaller than cieend in print_debug_frame_section.

We could end up with a negative length in a call to memchr.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoreadelf: Make sure readp is smaller than cieend in print_debug_frame_section.
Mark Wielaard [Tue, 16 Oct 2018 12:22:33 +0000 (14:22 +0200)]
readelf: Make sure readp is smaller than cieend in print_debug_frame_section.

We could end up with a negative length in a call to memchr.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoCheck sh_entsize is not zero.
Mark Wielaard [Fri, 19 Oct 2018 13:01:29 +0000 (15:01 +0200)]
Check sh_entsize is not zero.

There were some recent bug reports where we trusted the ELF section header
to be sane and divided the sh_size by the sh_entsize to get the number of
objects in the section. This would cause a divide by zero if the file was
corrupt and the sh_entsize was zero. Add checks for any such code.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agosize: Handle recursive ELF ar files.
Mark Wielaard [Thu, 18 Oct 2018 21:15:48 +0000 (23:15 +0200)]
size: Handle recursive ELF ar files.

eu-size didn't handle an ELF ar file that contained an ar file itself
correctly. handle_ar would recursively call itself but close the ELF
file before returning. Only close the ELF file at the top-level.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoarlib: Check that sh_entsize isn't zero.
Mark Wielaard [Thu, 18 Oct 2018 17:01:52 +0000 (19:01 +0200)]
arlib: Check that sh_entsize isn't zero.

A bogus ELF file could have sh_entsize as zero. Don't divide by zero,
but just assume there are no symbols in the section.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoar: Assume epoch if ar_date is bogus.
Mark Wielaard [Sun, 14 Oct 2018 15:29:51 +0000 (17:29 +0200)]
ar: Assume epoch if ar_date is bogus.

If the ar header contains a bogus ar_date then in verbose mode we would
get a NULL pointer from localtime. Just assume the entry was created
during the epoch.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agofindtextrel: Check that sh_entsize isn't zero.
Mark Wielaard [Sun, 14 Oct 2018 14:58:51 +0000 (16:58 +0200)]
findtextrel: Check that sh_entsize isn't zero.

A bogus ELF file could have sh_entsize as zero. Don't divide by zero,
but just assume there are no entries in the section.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibdwfl: Sanity check partial core file data reads.
Mark Wielaard [Sun, 14 Oct 2018 14:45:48 +0000 (16:45 +0200)]
libdwfl: Sanity check partial core file data reads.

There were two issues when reading note data from a core file.
We didn't check if the data we already had in a buffer was big
enough. And if we did get the data, we should check if we got
everything, or just a part of the data.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoreadelf: Handle multiple .debug_macro sections and decode header flag.
Mark Wielaard [Sat, 13 Oct 2018 13:08:16 +0000 (15:08 +0200)]
readelf: Handle multiple .debug_macro sections and decode header flag.

In object files there could be multiple .debug_macro sections.
These are COMDAT sections used as imports. Note that the output for
DW_MACRO_import isn't ideal since the offset is printed against the
start of the .debug_macro section, but it doesn't show which one.
We currently don't have that information and no interface yet for
libdw users.

Also decode the macro header flag byte for convenience.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agounstrip: Renumber the group section indexes.
Mark Wielaard [Sat, 13 Oct 2018 08:27:47 +0000 (10:27 +0200)]
unstrip: Renumber the group section indexes.

When unstripping we might need to renumber the group section indexes.
Just like we do when stripping.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agostrip, unstrip: Handle SHT_GROUP correctly.
Mark Wielaard [Sat, 13 Oct 2018 08:27:47 +0000 (10:27 +0200)]
strip, unstrip: Handle SHT_GROUP correctly.

The usage of annobin in Fedora showed a couple of bugs when using
eu-strip and eu-unstrip on ET_REL files that contain multiple group
sections.

When stripping we should not remove the SHF_GROUP flag from sections
even if the group section itself might be removed. Either the section
itself gets removed, and so the flag doesn't matter. Or it gets moved
together with the group section into the debug file, and then it still
needs to have the flag set. Also we would "renumber" the section group
flag field (which isn't a section index, and so shouldn't be changed).

Often the group sections have the exact same name (".group"), flags
(none) and sometimes the same sizes. Which makes matching them hard.
Extract the group signature and compare those when comparing two
group sections.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoHandle ADD/SUB relocations
Andreas Schwab [Tue, 2 Oct 2018 12:46:51 +0000 (14:46 +0200)]
Handle ADD/SUB relocations

This adds support for ADD and SUB relocations as seen on RISC-V.

Signed-off-by: Andreas Schwab <schwab@suse.de>
5 years agotests: backtrace-dwarf.c improve error handling in test framework.
Mark Wielaard [Fri, 21 Sep 2018 11:47:45 +0000 (13:47 +0200)]
tests: backtrace-dwarf.c improve error handling in test framework.

To debug https://sourceware.org/bugzilla/show_bug.cgi?id=23673
clean up the test framework so we know what exactly failed.

Suggested-by: Dmitry V. Levin <ldv@sourceware.org>
Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoPrepare for 0.174 elfutils-0.174
Mark Wielaard [Fri, 14 Sep 2018 10:08:15 +0000 (12:08 +0200)]
Prepare for 0.174

Set version to 0.174.
Mention new functionality in NEWS.
Update po/*.po files.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibdwfl: Document core memory and remote memory ELF shdrs reading.
Mark Wielaard [Sat, 4 Aug 2018 20:36:48 +0000 (22:36 +0200)]
libdwfl: Document core memory and remote memory ELF shdrs reading.

There are two places, dwfl_segment_report_module and elf_from_remote_memory
in libdwfl where we use the Ehdr e_shnum directly. Document why this is fine.
Getting the shdrs in those two places is really just a nice bonus and if there
are more than 0xff00 then it is unlikely we will get them all anyway.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoreadelf: Use elf_getshdrnum in print_shdr and print_phdr.
Mark Wielaard [Sat, 4 Aug 2018 20:30:15 +0000 (22:30 +0200)]
readelf: Use elf_getshdrnum in print_shdr and print_phdr.

print_shdr didn't print the correct number of sections if there were
more than SHN_LORESERVE sections. print_phdr wouldn't match up the
(allocated) sections and segements if there were more than SHN_LORESERVE
sections in the ELF file.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agostrip,unstrip: Use and set shdrstrndx consistently.
Mark Wielaard [Sat, 4 Aug 2018 18:29:25 +0000 (20:29 +0200)]
strip,unstrip: Use and set shdrstrndx consistently.

In various places in strip we used e_shstrndx instead of shdrstrndx and we
didn't setup the shdrstrndx for the debug file. In unstrip we forgot to copy
the shdrstrndx in case the -o output option was used.

Added a new testcase that adds many sections to a testfile and runs strip, elflint,
unstrip and elfcmp.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibdw: dwarf_begin_elf should use elf_getshdrstrndx to get section names.
Mark Wielaard [Sat, 4 Aug 2018 20:34:32 +0000 (22:34 +0200)]
libdw: dwarf_begin_elf should use elf_getshdrstrndx to get section names.

dwarf_begin_elf used the Ehdr e_shstrndx to get the shdr string table
section. This does not work for ELF files with more than SHN_LORESERVE
sections. Use elf_getshdrstrndx, and don't pass around the ehdr.

Add a simple testcase that fails before the patch because dwarf_begin
return an error.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoelflint: Use shnum and shstrndx instead of ehdr field directly.
Mark Wielaard [Wed, 12 Sep 2018 21:38:47 +0000 (23:38 +0200)]
elflint: Use shnum and shstrndx instead of ehdr field directly.

We already got the right shnum and shstrndx. But were still using
e_shnum in one check for ELFCLASS64 (it was correct for ELFCLASS32).
And when getting section names in check_symtab we still used
e_shstrndx in two places.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Fix shnum and section zero handling.
Mark Wielaard [Wed, 12 Sep 2018 21:38:28 +0000 (23:38 +0200)]
libelf: Fix shnum and section zero handling.

For ELF files with more than SHN_LOWRESERVE sections we always need
section zero to store the section number (it doesn't just fit in the
Ehdr e_shnum field). Make sure to create it if it doesn't exist yet
in elf_getscn. Also fix handling on shnum in updatefile for the mmap
case (we already got this correct for the non-mmap case).

This adds a new test run-copymany-sections.sh which is like
run-copyadd-sections.sh but tries to add two times 65535 sections.
It makes sure libelf can copy the whole file and elfcmp checks they
are the same. It doesn't use mmap for addsections since that doesn't
work yet. ELF_C_RDWR_MMAP needs mremap which will fail since it needs
too much space and the original mmap cannot move.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoelfcmp: Get, check and shdrstrndx for section names.
Mark Wielaard [Thu, 13 Sep 2018 11:52:46 +0000 (13:52 +0200)]
elfcmp: Get, check and shdrstrndx for section names.

elfcmp would use the Ehdr e_shstrndx field to find the shdr string
index table. Use elf_getshdrstrndx instead to be able to handle ELF
files with more than SHN_LORESERVE sections.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibebl: Use elf_getshdrstrndx in ebl_section_strip_p.
Mark Wielaard [Wed, 1 Aug 2018 14:51:03 +0000 (16:51 +0200)]
libebl: Use elf_getshdrstrndx in ebl_section_strip_p.

The ebl_section_strip_p function used the Ehdr e_shstrndx field
to get at the name of the (debug) sections. This is not correct
if there are more than SHN_LORESERVE sections. Use elf_getshdrstrndx
to get at the shstrtab section. And drop the Ehdr argument that isn't
necessary anymore.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agobackends: Use elf_getshdrstrndx to find .odp section in ppc64_init
Mark Wielaard [Sat, 4 Aug 2018 20:32:56 +0000 (22:32 +0200)]
backends: Use elf_getshdrstrndx to find .odp section in ppc64_init

The .odp section is found by name. But ppc64_init used the e_shstrndx
Ehdr field for that. This is wrong if the file contains more than
SHN_LORESERVE sections. Use elf_getshdrstrndx instead to find the
shstrtab section.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agobackends: Always use elf_getshdrstrndx in check_special_symbol.
Mark Wielaard [Sat, 4 Aug 2018 18:36:09 +0000 (20:36 +0200)]
backends: Always use elf_getshdrstrndx in check_special_symbol.

The check_special_symbol backend functions used the Ehdr e_shstrndx
field to get at the name of sections. This is not correct if there
are more than SHN_LORESERVE sections. Always use elf_getshdrstrndx
to get the shstrtab section. And drop the Ehdr argument that isn't
necessary anymore.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Fix some issues with ELF_C_RDWR_MMAP.
Mark Wielaard [Sun, 12 Aug 2018 13:35:18 +0000 (15:35 +0200)]
libelf: Fix some issues with ELF_C_RDWR_MMAP.

When ELF_C_RDWR_MMAP is used libelf might have to write overlapping memory
when moving the section data or headers. Make sure to use memmove, not
memcpy. Also the size of the underlying file might have to change. That
means we will have to also extend the mmap region with mremap. Since we
are using direct pointers into the mmapped area we cannot move the mmap,
only extend it. This might still fail if there is not enough free memory
available to extend the mmap region.

Two new test programs have been added. elfcopy which copies a whole elf
file (using either ELF_C_WRITE or ELF_C_WRITE_MMAP). And addsections which
adds new sections to an existing ELF file (using either ELF_C_RDWR or
ELF_C_RDWR_MMAP). The newly added test will fail under valgrind without
the fixes.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agotests: Add section attribute to defeat -freorder-blocks-and-partition.
Mark Wielaard [Tue, 11 Sep 2018 10:58:14 +0000 (12:58 +0200)]
tests: Add section attribute to defeat -freorder-blocks-and-partition.

GCC could partition main into an cold and hot block causing our symbol
lookup to fail in the backtrace-dwarf testcase. Add a section attribute
on "main" so that it will be kept together.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agostrip: Handle mixed allocated/non-allocated sections.
Mark Wielaard [Sun, 29 Jul 2018 22:27:52 +0000 (00:27 +0200)]
strip: Handle mixed allocated/non-allocated sections.

Normally in non-ET_REL files all allocated sections come before
all non-allocated sections. eu-strip relies on this when stripping
a file and calculating the file offsets. But recently on Fedora
there are non-allocated .gnu.build.attributes NOTE sections in
the middle of the allocated sections, with a sh_offset field that
is larger then the next section. This confuses eu-strip so much that
it might corrupt the stripped file.

Work around this by calculating the sh_offset fields in two phases
when detecting mixed allocated/non-allocated sections. First handle
the allocated ones, then use the offset after the last allocated
section to calculate the offsets of the non-allocated sections left
in the stripped file.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibdw: Check end of attributes list consistently.
Mark Wielaard [Sat, 18 Aug 2018 17:51:27 +0000 (19:51 +0200)]
libdw: Check end of attributes list consistently.

dwarf_child (__libdw_find_attr), dwarf_getabbrevattr[_data] and
dwarf_getattrs all assume the end of the attribute list is when
both the name (code) and form of the attribute are zero.

dwarf_getabbrev (__libdw_getabbrev) and dwarf_hasattr assume the
end of the attribute list is when either the name (code) or the
form of the attribute is zero.

The DWARF spec says: "The series of attribute specifications ends
with an entry containing 0 for the name and 0 for the form." So
the first check is correct.

Make sure dwarf_getabbrev and dwarf_hasattr use the same check.
This is important since all other functions expect dwarf_getabbrev
(__libdw_getabbrev) to have done a data sanity check of the attribute.
So if the ending condition is different it could cause a crash.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibdw, readelf: Make sure there is enough data to read full aranges header.
Mark Wielaard [Sat, 18 Aug 2018 11:27:48 +0000 (13:27 +0200)]
libdw, readelf: Make sure there is enough data to read full aranges header.

dwarf_getaranges didn't check if there was enough data left to read both
the address and segment size. readelf didn't check there was enough data
left to read the segment size.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoelflint: Fix check_sysv_hash[64] sanity checks to not overflow.
Mark Wielaard [Sat, 18 Aug 2018 11:17:45 +0000 (13:17 +0200)]
elflint: Fix check_sysv_hash[64] sanity checks to not overflow.

The sanity checks for how many words were needed in the section could
overflow causing errors. Fix the checks.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Return error if elf_compress_gnu is used on SHF_COMPRESSED section.
Mark Wielaard [Sat, 18 Aug 2018 10:42:16 +0000 (12:42 +0200)]
libelf: Return error if elf_compress_gnu is used on SHF_COMPRESSED section.

Compressing a section that is already compressed is fine, but useless.
But it isn't possible to gnu compress (or decompress) a SHF_COMPRESSED
section since there is no state kept that would tell if the section was
first GNU compressed or first gabi compressed. Calling elf_compress_gnu
on a section and then calling elf_compress on it to decompress it twice
could cause a crash (the other way around is fine). Just disallow it.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Fix documentation of elf_getshdrstrndx and elf_getshstrndx.
Mark Wielaard [Fri, 27 Jul 2018 12:43:15 +0000 (14:43 +0200)]
libelf: Fix documentation of elf_getshdrstrndx and elf_getshstrndx.

Because of copy/paste error part of the documentation for
elf_getshdrstrndx and elf_getshstrndx was actually for
elf_getshdrnum and elf_getshnum. Fix it to refer to the proper
ehdr field and deprecated function name.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agounstrip: Also check sh_size in compare_unalloc_sections.
Mark Wielaard [Tue, 24 Jul 2018 21:34:19 +0000 (23:34 +0200)]
unstrip: Also check sh_size in compare_unalloc_sections.

compare_unalloc_sections only checked sh_flags and the section names.
This would cause stripped/debug section mismatches when there were
multiple sections with the same name and flags. Fix this by also checking
the size of the section matches.

Add a testcase that has two ".group" sections created on i386 with the
gcc annobin plugin.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agounstrip: Handle SHT_GROUP sections in ET_REL files.
Mark Wielaard [Sat, 21 Jul 2018 21:40:11 +0000 (23:40 +0200)]
unstrip: Handle SHT_GROUP sections in ET_REL files.

SHT_GROUP sections are put in both the stripped and debug file.
Handle correcting the symbol table/name entry of the group only once.

The testfile was generated with the gcc annobin plugin.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agoelfcompress: Don't rewrite file if no section data needs to be updated.
Mark Wielaard [Sat, 21 Jul 2018 15:07:12 +0000 (17:07 +0200)]
elfcompress: Don't rewrite file if no section data needs to be updated.

If the input and output file are the same and no section needs to
be updated we really don't need to rewrite the file.

Check whether any matching section is already compressed or decompressed.
Skip the section if it doesn't need to be changed. If no section data
needs updating end with success without rewriting/updating file.

With --force the file will still always be updated/rewritten even if
no section data needs to be (de)compressed.

Acked-by: Igor Gnatenko <ignatenkobrain@fedoraproject.org>
Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agobackends: add abi_cfi and set_initial_registers_tid callbacks for M68K
Andreas Schwab [Sat, 21 Jul 2018 21:42:07 +0000 (23:42 +0200)]
backends: add abi_cfi and set_initial_registers_tid callbacks for M68K

This fixes all testsuite failures.

Signed-off-by: Andreas Schwab <schwab@linux-m68k.org>
5 years agoelfcompress: Swap fchmod and fchown calls on new file.
Mark Wielaard [Sat, 21 Jul 2018 14:10:25 +0000 (16:10 +0200)]
elfcompress: Swap fchmod and fchown calls on new file.

Calling fchmod with a suid bit on a file might silently fail or the suid
bit might be slilently cleared by a call to fchown if already set. Swap
the calls so that the owner is set first and then set the suid bit.

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

Reported-and-tested-by: Igor Gnatenko <ignatenkobrain@fedoraproject.org>
Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agotests: Add core and strip tests for RISC-V
Andreas Schwab [Thu, 19 Jul 2018 10:41:02 +0000 (12:41 +0200)]
tests: Add core and strip tests for RISC-V

Signed-off-by: Andreas Schwab <schwab@suse.de>
5 years agoFix typo in riscv_register_info
Andreas Schwab [Thu, 19 Jul 2018 09:54:59 +0000 (11:54 +0200)]
Fix typo in riscv_register_info

Signed-off-by: Andreas Schwab <schwab@suse.de>
5 years agoDon't include sys/uio.h if we don't need it
Ulf Hermann [Tue, 17 Jul 2018 11:40:26 +0000 (13:40 +0200)]
Don't include sys/uio.h if we don't need it

The PID attaching only works on linux.