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.
Dodji Seketeli [Thu, 13 Aug 2015 13:59:41 +0000 (15:59 +0200)]
Fix finding split debug info files not located by the build-id mechanism
[This bug has been found by using elfutils in the context of
libabigail. The initial bug report is
https://sourceware.org/bugzilla/show_bug.cgi?id=18792. The
interesting comments start at is
https://sourceware.org/bugzilla/show_bug.cgi?id=18792#c4]
suppose we have a debug info file that is located at a
/prefix1/usr/lib/debug/prefix2/libfoo.so. Suppose also that the debug
info describes a binary that is located at /prefix1/prefix2/libfoo.so
Suppose the debug_link property inside the binary
/prefix1/prefix2/libfoo.so correctly contains the string "libfoo.so"
that designates the name of the debug info file.
The problem is, when find_debuginfo_in_path() is called with its
file_name parameter set to "/prefix1/prefix2/libfoo.so" and
mod->dwfl->callbacks->debuginfo_path set to
"/prefix1/lib/debug/", it fails to locate the debug
info file libfoo.so under "/prefix1/usr/lib/debug/prefix2/".
This patch fixes the issue by making find_debuginfo_in_path() try all
the sub-strings of "/prefix1/prefix2/libfoo.so "under"
"/prefix1/usr/lib/debug/", to find libfoo.so. That is, it tries, in
order:
- /prefix1/usr/lib/debug/prefix1/prefix2/libfoo.so
- /prefix1/usr/lib/debug/prefix2/libfoo.so <-- and boom, it finds it!
Note that the patch tries the variations between the two candidates
above too.
The patch uses a goto. I dislike gotos like anyone else, but then
here, not using this would imply a bigger change of the logic of that
function. So I am proposing the scheme based on the goto instead.
* libdwfl/find-debuginfo.c (find_debuginfo_in_path): Try to locate
the debug info file named debuglink_file under
mod->dwfl->callbacks->debuginfo_path, by looking at the set of
sub-trees under mod->dwfl->callbacks->debuginfo_path which is
common to the set of non-absolute parent trees of file_name.
Mark Wielaard [Tue, 4 Aug 2015 10:20:20 +0000 (12:20 +0200)]
spec: Provide default-yama-scope.
When yama is enabled in the kernel it might be used to filter any user
space access which requires PTRACE_MODE_ATTACH like ptrace attach, access
to /proc/PID/{mem,personality,stack,syscall}, and the syscalls
process_vm_readv and process_vm_writev which are used for interprocess
services, communication and introspection (like synchronisation, signaling,
debugging, tracing and profiling) of processes.
These are precisely the things that libdw dwfl and ebl backends rely on.
So make sure they don't mysteriously fail in such cases by providing the
default yama scope sysctl value.
This is implemented as a separate subpackage that just provides this
functionality so other packages that don't directly rely on elfutils-libs
can also just Requires: default-yama-scope to function properly.
https://bugzilla.redhat.com/show_bug.cgi?id=1209492#c69
Mark Wielaard [Wed, 29 Jul 2015 15:51:27 +0000 (17:51 +0200)]
unstrip: Handle debuginfo files with missing SHF_INFO_LINK section flags.
With GCC 5 there might be a .rela.plt section with SHF_INFO_LINK set.
Buggy binutils objdump might strip it from the section in the debug file.
Ignore such differences for relocation sections and put the flag back
if necessary.
Also improve the error message a little by only discarding the already
matched sections if there is an prelink undo section. Otherwise we will
report all sections as not matching if the file wasn't prelinked instead
of just the non-matching sections.
New testfiles generated by gcc5 and binutils objdump added.
Fill the .gitignore with more generated files, such as:
- test executables
- results of the automake testing framework (*.log and *.trs)
- sources generated by flex and bison
- generated *.mnemonics files in libcpu
- the helper libcpu/i386_gendis tool
- *.so.1 symlinks
Also, anchor existing filenames to their location when
builddir==srcdir.
Pino Toscano [Sat, 27 Jun 2015 17:23:01 +0000 (19:23 +0200)]
tests: skip run-deleted.sh when dwfl_linux_proc_attach is not implemented
If the current OS does not implement dwfl_linux_proc_attach (which
currently only Linux does) then skip this test, as "stack" uses that
API for attaching to a running process.
Pino Toscano [Fri, 26 Jun 2015 18:36:01 +0000 (20:36 +0200)]
Reduce scope of some includes
Use some includes only according to the #ifdef block of the respective
code, or matching the fact they are Linux-only. This way, includes
potentially unportable are not unconditionally used.
Mark Wielaard [Thu, 18 Jun 2015 21:43:39 +0000 (23:43 +0200)]
findtextrel: Don't leak memory in process_file.
When a files do contain textrels we fail to release the segments searched
and valgrind will show:
2,560 bytes in 16 blocks are definitely lost in loss record 1 of 1
at 0x40281B5: malloc (in vgpreload_memcheck-x86-linux.so)
by 0x804AE63: process_file (findtextrel.c:322)
by 0x804909B: main (findtextrel.c:149)
Mark Wielaard [Thu, 18 Jun 2015 09:07:41 +0000 (11:07 +0200)]
nm: Fix typo in size check to determine whether we stack allocated memory.
We allocate GElf_SymX entries, which are larger than plain GElf_Sym structs.
The check to see whether we could use stack allocation used the correct
sizeof (GElf_SymX), but the check to see if we needed to free was using
the incorrect sizeof (GElf_Sym). Which could cause us to leak memory.
Mark Wielaard [Thu, 18 Jun 2015 09:00:51 +0000 (11:00 +0200)]
strings: Handle failure of getting section name on bogus section data.
If there is something wrong with getting the section data it is likely
we won't be able to get the actual section name because the file is
somehow corrupted. Try to get the name, but handle failure gracefully.
Mark Wielaard [Thu, 18 Jun 2015 08:41:42 +0000 (10:41 +0200)]
libdw: Not leak memory on error in dwarf_getpubnames.
When there is an error with the first entry we might already have allocated
the memory but not yet set cnt to 1. Just always free the memory on error.
free (NULL) is a nop anyway and doesn't matter on a failure path.