Mark Wielaard [Tue, 1 Dec 2015 13:55:07 +0000 (14:55 +0100)]
elfcompress: New utility.
Usage: elfcompress [OPTION...] FILE...
Compress or decompress sections in an ELF file.
-f, --force Force compression of section even if it would
become larger
-n, --name=SECTION SECTION name to (de)compress, SECTION is an
extended wildcard pattern (defaults to
'.?(z)debug*')
-o, --output=FILE Place (de)compressed output into FILE
-p, --permissive Relax a few rules to handle slightly broken ELF
files
-q, --quiet Be silent when a section cannot be compressed
-t, --type=TYPE What type of compression to apply. TYPE can be
'none' (decompress), 'zlib' (ELF ZLIB compression,
the default, 'zlib-gabi' is an alias) or
'zlib-gnu' (.zdebug GNU style compression, 'gnu'
is an alias)
-v, --verbose Print a message for each section being
(de)compressed
-?, --help Give this help list
--usage Give a short usage message
-V, --version Print program version
Mark Wielaard [Fri, 18 Dec 2015 14:51:37 +0000 (15:51 +0100)]
elflint: Recognize and check SHF_COMPRESSED section flag.
SHF_COMPRESSED is a valid section flag, it can optionally be on any special
section, but it cannot be used on NOBITS sections or together with SHF_ALLOC.
A section that has SHF_COMPRESSED set must have a valid Chdr.
Mark Wielaard [Wed, 30 Dec 2015 00:39:29 +0000 (01:39 +0100)]
libelf: Make elf_strptr index correctly into compressed section data.
elf_strptr indexes into the section data. This is defined as index into
the uncompressed data of the section. If the section is compressed make
sure the uncompressed data is available, but don't really decompress the
section header (elf_getdata will still return compressed data).
Mark Wielaard [Tue, 20 Oct 2015 23:30:44 +0000 (01:30 +0200)]
readelf: Handle compressed sections and extend -z to cover -x and -p.
When printing a (non-string, non-data) section use uncompressed data when
possible. For dumping hex and string sections (-x and -p) -z will dump the
uncompressed data (otherwise the compressed data is dumped).
-z, --decompress Show compression information for compressed
sections (when used with -S); decompress section
before dumping data (when used with -p or -x)
Includes test cases for ET_REL files using compressed relocation (target)
debug sections to test libdwfl transparent uncompression of sections.
Mark Wielaard [Wed, 30 Dec 2015 13:39:18 +0000 (14:39 +0100)]
libdwfl: Use elf_compress[_gnu] to decompress string, symbol and reloc data.
This makes usage of the libdwfl symbol functions work out of the box even
when some sections (string, symbol or xndx) are compressed. For ET_REL
files this makes relocations just work by making sure the target section
is decompressed first before relocations are applied.
Mark Wielaard [Wed, 30 Dec 2015 13:18:40 +0000 (14:18 +0100)]
libdw: Use elf_compress[_gnu] to uncompress debug section data.
libdw used its own code for decompressing GNU style .zdebug sections.
Using elf_compress and elf_compress_gnu instead makes it possible to
transparently use either GNU style or ELF style compressed data.
Mark Wielaard [Thu, 19 Nov 2015 23:12:37 +0000 (00:12 +0100)]
libelf: Add elf32_getchdr, elf64_getchdr and gelf_getchdr.
Elf_Data of a compressed section has type ELF_T_CHDR. This type can be
xlated to the file or memory representation. This will make sure the Chdr
is in the correct endianess. The compressed data following the Chdr isn't
translated.
Mark Wielaard [Thu, 31 Dec 2015 22:16:28 +0000 (23:16 +0100)]
elflint: Add _edata and _end to the list of possibly dangling gnuld symbols.
gnuld might keep dangling/unused symbols around (see sourceware PR13621).
Add _edata (end of initialized data address) and _end (end of uninitialized
data address) to that list. They might have been added by gnuld even if
there is no such [un]initialized data. Also add the double underscore
variants which are used on some architectures.
Mark Wielaard [Thu, 31 Dec 2015 22:03:20 +0000 (23:03 +0100)]
tests: elfstrmerge warn about STT_SECTION symbol for shstrhndx.
Old linkers might have created an STT_SECTION symbol for the section
header string table section, which isn't actually used. For now just
warn about such symbols. If such a symbol would actually really be used
(but why?) then we would have to handle it by removing it and rewriting
the symbol table.
This is a testsuite only change, but includes an extra test with files
that have such STT_SECTION symbols to make sure it will be handled in
case we "upgrade" the elfstrmerge test to a real utility.
libdwfl: make the unwinder aware of the backend defined ra_offset.
Ebl backends can define an offset that must be applied to the value of
the "return address register" defined in the CFI ABI information.
This patch makes the unwinder to use that offset when reading the RA
CFI register.
Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
Some backends need to fine-tune the return address as fetched from the
corresponding CFI register. This patch adds a new backend attribute
ra_offset and the corresponding ebl_ra_offset getter function.
Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
Mark Wielaard [Wed, 2 Dec 2015 16:27:18 +0000 (17:27 +0100)]
tests: Use valgrind --leak-check=full.
When --enable-valgrind is given run the testsuite with --leak-check=full
treating any memory leak as a failure. Also disable valgrind for those
tests that trigger a valgrind bug.
https://bugs.kde.org/show_bug.cgi?id=327427
Mark Wielaard [Wed, 2 Dec 2015 16:07:40 +0000 (17:07 +0100)]
libdw: Don't leak duplicate FDEs.
Although it isn't supposed to happen there could be FDEs that cover the
same address range. Don't leak such FDEs and use an existing FDE for
consistency.
Mark Wielaard [Wed, 2 Dec 2015 15:57:44 +0000 (16:57 +0100)]
nm: If the file is ET_REL and we need Dwarf use libdwfl to get it relocated.
If we need Dwarf and the file is ET_REL use the same trick as in readelf
to get a relocated Dwarf. Otherwise lots of references in the debug_info
will come out as zero.
This also explains the "bogus" Dwarf seen that caused the memory leak in
the local_root that was fixed previously.
Mark Wielaard [Tue, 1 Dec 2015 23:21:07 +0000 (00:21 +0100)]
libdw: Don't use a FDE if it doesn't actually cover a real code range.
If the FDE end <= start then it doesn't actually cover a valid code range.
Don't use and cache such FDEs (it will cause memory leaks if there is
another FDE that has the same start address and a valid code range).
Such FDEs have been seen in the backtrace.ppc.exec testfile.
Mark Wielaard [Tue, 1 Dec 2015 22:11:55 +0000 (23:11 +0100)]
libdwfl: Fix memory leak in link_map.c dwfl_link_map_report.
The phdrs buffer could come from the core file or through the executable.
dwfl_link_map_report would leak the buffer if it came from the executable.
Track where the buffer came from and free appropriately.
Mark Wielaard [Tue, 1 Dec 2015 21:16:00 +0000 (22:16 +0100)]
libdwfl: Don't leak core Elf and core file descriptor from argp-std.
Our argp helper would leak the core Elf and core file descriptor when
given by the user. Store both in the Dwfl as a new Dwfl_User_Core so
they get cleaned up by dwfl_end.
Mark Wielaard [Tue, 1 Dec 2015 14:21:55 +0000 (15:21 +0100)]
libdw: Don't leak fake_loc_cu.
The fake_loc_cu is used when synthesizing attributes for Dwarf_Ops that
came from a location list entry in dwarf_getlocation_attr. Make sure
we remove the loc cache stored in it when disposing of the fake_loc_cu.
Mark Wielaard [Thu, 19 Nov 2015 11:37:08 +0000 (12:37 +0100)]
libdw: Make sure Ebl is always freed from cfi frame cache.
libdwfl sets the Dwfl_Module Ebl for the eh_cfi and dwarf_cfi cache to
save a bit of memory. It also calls ebl_closebackend on the ebl to free
it. The Dwarf_CFI never frees the Ebl in the cache, even when it opened
one itself. This means that if only libdw calls are used to access the
Dwarf_CFI the Ebl might be leaked.
Always destroy the Dwarf_CFI cache Ebl in __libdw_destroy_frame_cache.
And in __libdwfl_module_free clear the Dwarf_CFI Ebl if it is the
Dwfl_Module Ebl before calling dwarf_cfi_end and dwarf_end.
Dmitry V. Levin [Mon, 16 Nov 2015 15:11:23 +0000 (18:11 +0300)]
libdw: initialize state early in read_srclines
Starting with commit f8443bd09f8a8d3d84a63e5ce206a218e57dff7a,
we might jump to "out" on error before initialization of "state".
Initialize "state" early to fix this issue.
Mark Wielaard [Fri, 16 Oct 2015 19:19:05 +0000 (21:19 +0200)]
elflint: Don't check shdr or TLS symbol offsets against debuginfo phdrs.
In debuginfo files the phdrs file offsets match those of the original ELF
file. So the section header file offsets and the TLS symbol value offset
in debuginfo files cannot be sanity checked against the phdrs file offset.
The issue can be triggered with run-strip-strmerge.sh test when configured
with --enable-gcov. Which will insert a .tbss section and TLS symbols that
trigger the above checks.
Mark Wielaard [Fri, 16 Oct 2015 11:15:01 +0000 (13:15 +0200)]
elflint: Reject mergable string sections as relocation target.
Mergable sections can be the target of a relocation, but only if they
contain items of the same size. So only reject mergable string sections
as target of a relocation section.
Mark Wielaard [Fri, 16 Oct 2015 10:26:27 +0000 (12:26 +0200)]
ppc: ppc_check_special_symbol): Also allow _SDA_BASE_ in .data section.
The Small Data Area Base normally points to .sdata, in which case we
check it is at an offset of 0x8000. It might however fall in the
.data section, in which case we cannot check the offset. The size always
should be zero.
Josh Stone [Sat, 10 Oct 2015 00:32:52 +0000 (17:32 -0700)]
libelf: Use int64_t for offsets in libelf.h
Some systems don't have loff_t, like FreeBSD where off_t always supports
large files. We need a standardized 64-bit signed type for the public
header, without depending on configuration... OK, just use int64_t.
Jose E. Marchesi [Tue, 13 Oct 2015 16:39:15 +0000 (18:39 +0200)]
Do not rely on ebl_check_special_section when checking debuginfo files.
The test run-strip-strmerge.sh is failing in sparc targets. This is
because it does an `elflint -d' in a debuginfo file, and this fails
with:
section [21] '.plt' is both executable and writable
elflint uses the ebl_check_special_section hook (implemented only by
the alpha and sparc backends) in order to determine whether an
executable+writable section must be flagged or not. In both cases the
hook will fail analyzing eu-strip debuginfo files (as they should,
because debuginfo files are broken ;).
This patch just modifies elflint to not use the hook when -d is
specified in the command-line. This fixes the test in sparc-* and
sparc64-* targets (and presumably in alpha* targets too).
Additionaly, the patch checks that the type of WE sections in
debuginfo files is NOBITS.
Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
Mark Wielaard [Tue, 13 Oct 2015 14:02:10 +0000 (16:02 +0200)]
elflint: Check relro flags are a subset of the load segment.
If the RELRO segment doesn't fully overlap with the load segment then the
load segment might have more flags sets. This happens for example on sparc
when the .plt, which is executable, is also part of the load segment that
RELRO covers.
Mark Wielaard [Mon, 5 Oct 2015 15:32:29 +0000 (17:32 +0200)]
libelf: Only use posix_fallocate when using mmap. Ignore unsupported errors.
Don't use posix_fallocate when not using mmap. It is enough to ftruncate
the file to the right size. pwrite will report an error if there is no
disk space left. And on file systems that don't support fallocate it
might duplicate writes in that case. When using posix_fallocate do ignore
most errors. Other libc implementations don't guarantee the call actually
works always and even with glibc there might be an unexpected error from
the fallback code when the file system doesn't support fallocate. That is
pretty nasty since we might get a SIGBUS in that case when writing to the
mmapped memory. But the chance of that happening is very small. And will
normally never happen with glibc. So only report an error when
posix_fallocate reports ENOSPC.
Mark Wielaard [Wed, 7 Oct 2015 11:43:32 +0000 (13:43 +0200)]
config: Use ARFLAGS 'cr'.
By default automake uses ARFLAGS cru. 'u' is an optimization that
makes sure files are only added to the archive when they are newer
than the version already in the .a file. This optimization doesn't
work when ar is deterministic by default because then it doesn't
record timestamps in the archive. Deterministic ar is the default
now on various distributions causing multiple warnings like:
ar: `u' modifier ignored since `D' is the default (see `U')
Josh Stone [Fri, 9 Oct 2015 17:10:37 +0000 (10:10 -0700)]
Trust AC_SYS_LARGEFILE to provide large file support
AC_SYS_LARGEFILE defines _FILE_OFFSET_BITS in config.h if needed for
LFS, and this automatically maps things like open to open64. But quite
a few places used explicit 64-bit names, which won't work on platforms
like FreeBSD where off_t is always 64-bit and there are no foo64 names.
It's better to just trust that AC_SYS_LARGEFILE is doing it correctly.
But we can verify this too, as some file could easily forget to include
config.h. The new tests/run-lfs-symbols.sh checks all build targets
against lfs-symbols (taken from lintian) to make sure everything was
implicitly mapped to 64-bit variants when _FILE_OFFSET_BITS is set.
Mark Wielaard [Sun, 4 Oct 2015 21:47:47 +0000 (23:47 +0200)]
strip: Only sanity check symbols to not discarded sections for debug file.
We can only sanity check the symbols to discarded sections when creating a
debug file. Otherwise we won't keep track of debug_data. This can trigger
erroneously when there are relocation symbols in the symtab for a .debug
section but we are not putting those symbols in a separate .debug file.
Adjust run-strip-test.sh to always run in-place strip size check even
for ET_REL files. Before this fix the sanity check would trigger for
some ET_REL files because they contained such relocation symbols to
.debug sections (but debug_data would be NULL because -f wasn't used).
Josh Stone [Mon, 5 Oct 2015 18:04:15 +0000 (11:04 -0700)]
Improve AM_SILENT_RULES coverage
Note, elfutils does not explicitly enable AM_SILENT_RULES. It's only
available starting from automake 1.11, but starting from automake 1.13
silent rules are always generated, defaulting to verbose. $(AM_V_foo)
additions should be no-ops on systems that don't support silent rules.
To be silent, use "./configure --enable-silent-rules" or "make V=0".
sparc: fix the extraction of relocation IDs from r_type fields.
This patch adds support for a RELOC_TYPE_ID transform macros that
backends can use before including common-reloc.c. The sparc backend
uses this in order to extract the relocation IDs from r_type fields.
In this target the most significative 24 bits of r_type are used to
store an additional addend in some relocation types.
Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
Mark Wielaard [Fri, 2 Oct 2015 14:31:36 +0000 (16:31 +0200)]
unstrip: Update sh_info when SH_INFO_LINK_P not just when SHF_INFO_LINK set
SHF_INFO_LINK is not consistently set when sh_info is actually a section
index reference. Use SH_INFO_LINK_P to check whether to update the sh_info
value. SH_INFO_LINK_P also checks the section type to know whether or not
sh_info is meant as section index.
Found by run-strip-strmerge.sh test with older binutils.
Mark Wielaard [Tue, 11 Aug 2015 19:38:38 +0000 (21:38 +0200)]
Handle merged strtab/shstrtab string tables in strip and unstrip.
ELF files can share the section header string table (e_shstrndx) with
the symtab .strtab section. That might in some cases save a bit of space
since symbols and sections might share some (sub)strings. To handle that
eu-strip just needs to not unconditionally remove the .shstrtab section
(it will be properly marked as used/unused as needed). eu-unstrip needs
to make sure the section names are added to the strtab if it decides to
rewrite that section. Also makes sure that eu-strip won't move around
a SHT_NOBITS section that has SHF_ALLOC set. Although it is allowed to
move such sections around, there is no benefit. And some tools might
expect no allocated section to move around, not even a nobits section.
It also makes it harder to do "roundtripping" sanity checks that make
sure splitting a file with eu-strip and then reconstructed with eu-unstrip
produce the same ELF file (as is done in the new run-strip-strmerge.sh).
Introduces a somewhat large test generator elfstrmerge.c that will
hopefully turn into a more generic string table merger program.
Mark Wielaard [Tue, 29 Sep 2015 15:04:11 +0000 (17:04 +0200)]
libelf: Always update e_version and e_shentsize in elf_update.
When e_version is EV_NONE we should set it to EV_CURRENT like we do for
the EI_VERSION and like we set EI_DATA to the correct byte order when set
to ELFDATANONE. Likewise we should always set e_shentsize like we do for
e_phentsize, not just when ELF_F_LAYOUT isn't set.
Add a new elfshphehdr testcase to check the above.
Used local variables are passed to new file scope functions
as constant parameters, or pointers, or embedded in a
'state' structure.
One simple function "report" is changed to a macro.
It triggers a gcc false positive -Werror=maybe-uninitialized,
so the local variables are explicitly initialized.
Mark Wielaard [Tue, 22 Sep 2015 12:20:45 +0000 (14:20 +0200)]
Update dl-hash.h from glibc.
Our dl-hash.h implementation originally came from, or was written at the
same time as, the glibc implementation. At some point (around 9 years ago)
they diverged and the elfutils version got an updated copyright header.
The glibc version saw various updates/optimizations. Just treat the file
like we do for elf.h and copy it whenever the glibc version is updated.
Mark Wielaard [Tue, 15 Sep 2015 08:55:10 +0000 (10:55 +0200)]
libdw: Don't reassign result pointer in dwarf_peel_type.
GCC6 will warn about the reassignement of the nonnull result pointer.
The reassignment is indeed a little questionable. The compiler cannot
see that the pointer will not actually be reassigned since the function
will just return the same pointer value except when the dwarf_formref_die
function fails. In which case we don't use the result anymore. So the
compiler has to pessimistically assume the pointer will need to be
reloaded in the loop every time. Help the compiler generate slightly
better code by just checking whether the function fails directly instead
of reusing the pointer value for this.
Mark Wielaard [Mon, 7 Sep 2015 10:52:12 +0000 (12:52 +0200)]
libdwfl: Make dwfl_linux_proc_attach work even without any Dwfl_Modules.
dwfl_linux_proc_attach depended on a Dwfl_Module with the correct
ELF header already being available. That isn't really necessary since
when we attach we have the main exe ELF file available. Just use that
to make dwfl_linux_proc_attach always work.
Mark Wielaard [Thu, 3 Sep 2015 10:50:54 +0000 (12:50 +0200)]
backends: Use ebl->class consistently in sparc backend.
sparc_init would check the ELF class to register to correct core_note
handler. But sparc_register_info would check the ELF machine to determine
whether the register set was 32 or 64 bits. This caused some confusion
and assertion failures in readelf when printing core notes where the
sparc machine and class didn't seem to match up. Check the ELF class
in both sparc_unit and sparc_register_info to give consistent results.
Mark Wielaard [Thu, 3 Sep 2015 08:50:58 +0000 (10:50 +0200)]
readelf: handle_core_item large right shift triggers undefined behaviour.
The problem is this:
int n = ffs (w);
w >>= n;
The intent is to shift away up to (and including) the first least
significant bit in w. But w is an unsigned int, so 32 bits. And the
least significant bit could be bit 32 (ffs counts from 1). Unfortunately
a right shift equal to (or larger than) the length in bits of the left
hand operand is undefined behaviour. We expect w to be zero afterwards.
Which would terminate the while loop in the function. But since it is
undefined behaviour anything can happen. In this case, what will actually
happen is that w is unchanged, causing an infinite loop...
gcc -fsanitize=undefined will catch and warn about this when w = 0x80000000
Mark Wielaard [Tue, 11 Aug 2015 22:11:26 +0000 (00:11 +0200)]
elflint: Add gnuld check when a NOBITS section falls inside a segment.
gnuld has a really bad bug where it can place a NOBITS section inside
a PT_LOAD segment. Normally that would not work. But it also makes sure
that the contents of the file is all zeros. So in practice it is actually
a PROGBITS section with all zero data. Except that other tools will think
there is an unused gap in the ELF file after the NOBITS section.
Recognize and check this pattern in elflint when --gnu is given.
Mark Wielaard [Fri, 14 Aug 2015 13:50:12 +0000 (15:50 +0200)]
tests: Add a testcase for dwfl debuginfo-path with separate bin/debug dirs.
Reuse the run-addr2line-i-test.sh testfile-inlines testfile, but
first strip the debuginfo and put the binary and separate debuginfo
file in different roots. Check that --debuginfo-path still finds
the debug file.