platform/upstream/elfutils.git
4 years agodebuginfod test: assert curl & rpm2cpio during test prologue
Frank Ch. Eigler [Mon, 3 Feb 2020 19:11:53 +0000 (14:11 -0500)]
debuginfod test: assert curl & rpm2cpio during test prologue

On debian & ubuntu hosts, rpm2cpio is not available by default,
and heck, curl might not be either.  Check for both these programs
during tests/run-debuginfod-find.sh startup, else exit-77 (skip).

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
4 years agoPR24393: debuginfod configury: tolerate CXX= configure invocations
Frank Ch. Eigler [Mon, 3 Feb 2020 11:29:31 +0000 (06:29 -0500)]
PR24393: debuginfod configury: tolerate CXX= configure invocations

Previous code was fragile with respect to this case and needlessly
tolerant with non-c++11 compilers.  New configury makes more muscular
assertion about this, to catch bad $CXX settings or poor language
support.

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
4 years agodebuginfod: diagnostics verbosity tweak
Frank Ch. Eigler [Wed, 22 Jan 2020 20:27:59 +0000 (15:27 -0500)]
debuginfod: diagnostics verbosity tweak

marxin reported "skipping hat= .... empty comp_dir" messages
were too chatty for the journnal.

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
4 years agoempty commit for irker test
Frank Ch. Eigler [Wed, 22 Jan 2020 18:22:31 +0000 (13:22 -0500)]
empty commit for irker test

4 years agoPR25394 cont'd: debuginfod testsuite fix for -USR1 timing
Frank Ch. Eigler [Mon, 20 Jan 2020 20:37:33 +0000 (15:37 -0500)]
PR25394 cont'd: debuginfod testsuite fix for -USR1 timing

If a SIGUSR1 is sent before the initial traversal, it no longer
results in an extra traversal.  That's a sensible effect.  The
test case just needs to wait before the kill -USR1.

4 years agoPR25394 cont'd: debuginfod timing fix for fts-traversal thread
Frank Ch. Eigler [Mon, 20 Jan 2020 20:37:33 +0000 (15:37 -0500)]
PR25394 cont'd: debuginfod timing fix for fts-traversal thread

The new code neglected to set the last_rescan timestamp, leading
to overly frequent rescanning.

4 years agoPR25394 followup#2: debuginfod casting fixes
Frank Ch. Eigler [Mon, 20 Jan 2020 19:44:15 +0000 (14:44 -0500)]
PR25394 followup#2: debuginfod casting fixes

Buildbot still reports type warnings in time_t arithmetic.
Explicit (long)er cast pushed as obvious ... or is it? :-)

4 years agoPR25394 followup: debuginfod casting fixes
Frank Ch. Eigler [Mon, 20 Jan 2020 19:44:15 +0000 (14:44 -0500)]
PR25394 followup: debuginfod casting fixes

Buildbot reports type warnings in time_t arithmetic.
Explicit (long) cast pushed as obvious.

4 years agoPR25394: debuginfod mutex between grooming and scanning
Frank Ch. Eigler [Mon, 20 Jan 2020 01:33:32 +0000 (20:33 -0500)]
PR25394: debuginfod mutex between grooming and scanning

Extended the work-queue concept with "idlers" - other threads that
block on the work queue until it becomes empty (rather than normal
consumers that block on it until it becomes non-empty).  Use this
facility for the groomer thread to avoid working at the same time as
the scanner threads.  Use this for the fts traversal thread for
similar reasons.  One user-visible effect: response to SIGUSR1 and
SIGUSR2 will wait until the work queue runs empty, but the man page
was unspecific so does not need changing.

It's not obvious how to test this with a tests/ dataset so small that
scanning takes negligible time, so the former races are very tight.

P.S. We also evaluated using sqlite level transactions to isolate the
scanner thread groups-of-operations from the groomer.  These
experiments failed to produce a nominally concurrent debuginfod,
having triggered "database locked" type errors.  So we remain
single-threaded (fully serialized) at the sqlite API level.

4 years agoPR25415: fix debuginfod SEGV for empty search path list
Frank Ch. Eigler [Sat, 18 Jan 2020 20:32:40 +0000 (15:32 -0500)]
PR25415: fix debuginfod SEGV for empty search path list

Prevent triggering undefined behaviour of fts(3), which causes
a SEGV on fedora glibc 2.29.

4 years agonm: Fix nm --external sysv format output.
Mark Wielaard [Thu, 16 Jan 2020 22:33:52 +0000 (23:33 +0100)]
nm: Fix nm --external sysv format output.

Partial revert of commit 66f4c37d497bdde040a33f299b12163f044b1bf2.
If index zero wasn't a real symbol it has already been filtered out
in show_symbols so don't skip it in show_symbols_sysv.

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

Reported-by: Enzo Matsumiya <ematsumiya@suse.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agodoc: Fix DEBUGINFOD_PROGRESS description to just mention output on stderr.
Mark Wielaard [Fri, 10 Jan 2020 14:46:29 +0000 (15:46 +0100)]
doc: Fix DEBUGINFOD_PROGRESS description to just mention output on stderr.

An earlier variant of the default progress function could write to any
file. Which is still in the documentation. But the actual implementation
just uses stderr. Fix the documentation to match.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agolibasm.h: Don't include libebl.h. Define an opaque Ebl handle.
Mark Wielaard [Wed, 8 Jan 2020 14:04:50 +0000 (15:04 +0100)]
libasm.h: Don't include libebl.h. Define an opaque Ebl handle.

Using libasm isn't really usable without a way to create an Ebl handle.
But we don't support libebl.h (and libebl itself). Just define the
Ebl handle as an opaque struct. Code that uses it needs to figure out
how to instantiate one itself (they cannot in any supportable way...)

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoelfutils.spec: typo fix
Frank Ch. Eigler [Sun, 12 Jan 2020 16:04:57 +0000 (11:04 -0500)]
elfutils.spec: typo fix

Removed dangling %endif.

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
4 years agoelfutils.spec: dependency typo fix
Frank Ch. Eigler [Sun, 12 Jan 2020 16:00:20 +0000 (11:00 -0500)]
elfutils.spec: dependency typo fix

Commit da0959cafda4 introduced a dependency on "debuginfod-client",
whereas it's really "elfutils-debuginfod-client".

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
4 years agodebuginfod: extracted-from-archive file cache
Frank Ch. Eigler [Fri, 27 Dec 2019 00:06:46 +0000 (19:06 -0500)]
debuginfod: extracted-from-archive file cache

Add a facility to service webapi and dwz/altdebug requests that
resolve to archives via a $TMPDIR file cache.  This permits
instantaneous dwz resolution during -debuginfo rpm scanning, and also
instantanous duplicate webapi requests.  The cache is limited both in
number of entries and in storage space.  Heuristics provide
serviceable defaults.

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
4 years agodebuginfod: print U-A: and X-F-F: request headers
Frank Ch. Eigler [Sat, 11 Jan 2020 21:05:46 +0000 (16:05 -0500)]
debuginfod: print U-A: and X-F-F: request headers

For an incoming webapi request, print two headers that should assist
in the administration of a debuginfod service.  At fweimer's
suggestion, added a bit of filtering so the text is more reliably
parseable.

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
4 years agodebuginfod: rework threading model for file/archive scanning
Frank Ch. Eigler [Wed, 1 Jan 2020 01:06:30 +0000 (20:06 -0500)]
debuginfod: rework threading model for file/archive scanning

We switch from a thread per supplied PATH, with a semaphore based
concurrency control, to a fixed number of worker threads collecting
the result of a plain directory traversal being put into a work queue.
This allows maximal continuous concurrency, even if the PATH
directories are dramatically differently sized.  There is no more need
to use concurrency-motivated subdirectory wildcards for PATH entries:
just a single top level directory will work fast.  doc & tests incl.

4 years agodebuginfod: Use DEBUGINFOD_TIMEOUT as seconds to get at least 100K.
Mark Wielaard [Thu, 2 Jan 2020 16:02:42 +0000 (17:02 +0100)]
debuginfod: Use DEBUGINFOD_TIMEOUT as seconds to get at least 100K.

Use just one timeout using CURLOPT_LOW_SPEED_TIME (default 90 seconds)
and CURLOPT_LOW_SPEED_LIMIT (100K).

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agodebuginfod: pass a distro-summary User-Agent request header
Frank Ch. Eigler [Mon, 6 Jan 2020 09:29:21 +0000 (04:29 -0500)]
debuginfod: pass a distro-summary User-Agent request header

It may be useful for a debuginfod server operator to know what kinds
of clients make webapi requests.  This is mainly as a
telemetry/diagnostic (though the data cannot be really trusted).  It
may also be useful to automate downloading of distro packages to a
debuginfod server in the case of an unknown hex buildid.  doc/testing
not affected as these are diagnostics.

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
4 years agoDo not install libdebuginfod.pc unless debuginfod is enabled
Dmitry V. Levin [Wed, 1 Jan 2020 23:42:10 +0000 (02:42 +0300)]
Do not install libdebuginfod.pc unless debuginfod is enabled

Fixes: 288f6b199 ("debuginfod 1/2: client side")
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
4 years agodebuginfod server: support .deb/.ddeb archives
Frank Ch. Eigler [Sat, 30 Nov 2019 15:46:44 +0000 (10:46 -0500)]
debuginfod server: support .deb/.ddeb archives

Add support for scanning .deb / .ddeb files, enabled with a new
command line option "-U".  Using a synthetic .deb/.ddeb from a Ubuntu
18 machine, extend the debuginfod testsuite with some .deb processing,
if the dpkg-deb binary is installed.

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
4 years agodebuginfod: usability tweaks, incl. $DEBUGINFOD_PROGRESS client support
Frank Ch. Eigler [Wed, 4 Dec 2019 20:51:12 +0000 (15:51 -0500)]
debuginfod: usability tweaks, incl. $DEBUGINFOD_PROGRESS client support

This facility allows a default progress-printing function to be
installed if the given environment variable is set.  Some larger usage
experience (systemtap fetching kernels) indicates the default timeout
is too short, so forked it into a connection timeout (default short)
and a transfer timeout (default unlimited).

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
4 years agolibdwfl: remove broken coalescing logic in dwfl_report_segment
Omar Sandoval [Thu, 12 Dec 2019 01:29:44 +0000 (17:29 -0800)]
libdwfl: remove broken coalescing logic in dwfl_report_segment

dwfl_report_segment has some logic that detects when a segment is
contiguous with the previously reported segment, in which case it's
supposed to coalesce them. However, in this case, it actually returns
without updating the segment array at all. As far as I can tell, this
has always been broken. It appears that no one uses the coalescing logic
anyways, as they pass IDENT as NULL. Let's just get rid of the logic and
add a test case.

Signed-off-by: Omar Sandoval <osandov@fb.com>
4 years agolibcpu: Compile i386_lex.c with -Wno-implicit-fallthrough
Omar Sandoval [Thu, 12 Dec 2019 00:23:28 +0000 (16:23 -0800)]
libcpu: Compile i386_lex.c with -Wno-implicit-fallthrough

elfutils is compiled with -Wimplicit-fallthrough=5, so the fallthrough
comment in i386_lex.c (generated by flex) doesn't prevent the implicit
fallthrough warning. Add -Wno-implicit-fallthrough to i386_lex_CFLAGS.

Signed-off-by: Omar Sandoval <osandov@fb.com>
4 years agoconfigure: Fix -D_FORTIFY_SOURCE=2 check when CFLAGS contains -Wno-error
Omar Sandoval [Thu, 12 Dec 2019 00:23:27 +0000 (16:23 -0800)]
configure: Fix -D_FORTIFY_SOURCE=2 check when CFLAGS contains -Wno-error

If CFLAGS contains -Wno-error, then the check for -D_FORTIFY_SOURCE=2
won't fail when appropriate. E.g., compiling with:

  ./configure CFLAGS='-Wno-error -O0' &&

Results in a flood of "_FORTIFY_SOURCE requires compiling with
optimization (-O)" warnings.

Make sure we add -Werror after the user-defined CFLAGS.

Signed-off-by: Omar Sandoval <osandov@fb.com>
4 years agodebuginfod: Check the DEBUGINFOD_URLS environment variable early in client.
Mark Wielaard [Mon, 9 Dec 2019 18:38:19 +0000 (19:38 +0100)]
debuginfod: Check the DEBUGINFOD_URLS environment variable early in client.

If the debuginfod-client isn't configured we should do as little
as possible. Simply return early with ENOSYS if no servers are
configured. This means we won't check

This does change the behavior of the debuginfod_find calls slightly.
Previously we would setup and check the cache if the given build-id
was valid. Which might have provided a result if an earlier client
had run with the same cache and valid server URLs which knew about
that particular build-id. Now we don't return any cached results
unless at least one server is configured.

This prevents selinux errors when the library is used in a confined
setup.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoMake sure to always build with build-ids.
Mark Wielaard [Fri, 6 Dec 2019 16:20:00 +0000 (17:20 +0100)]
Make sure to always build with build-ids.

We really need build-ids for various things.  If the system compiler
doesn't generate build-ids warn and generate them anyway for both the
binaries and the tests.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agotests: Run strip under testrun in run-debuginfod-find.sh
Mark Wielaard [Fri, 6 Dec 2019 14:48:00 +0000 (15:48 +0100)]
tests: Run strip under testrun in run-debuginfod-find.sh

Otherwise strip might run against the system libelf which might be too
old or missing.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agotests: Add break to avoid implicit-fallthrough warning
Mark Wielaard [Fri, 6 Dec 2019 14:39:38 +0000 (15:39 +0100)]
tests: Add break to avoid implicit-fallthrough warning

For some reason gcc might fail to recognize the assert (0) will never
return and emit an implicit-fallthrough warning. Just add a break to
silence it.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoconfig: Update elfutils.spec with explicit sub-package requires.
Mark Wielaard [Thu, 5 Dec 2019 14:24:49 +0000 (15:24 +0100)]
config: Update elfutils.spec with explicit sub-package requires.

Consistently require version-release subpackages so that everything
is updated together. Technically this isn't always required (it is
for elfutils-libelf and elfutils-libs). But this makes things more
consistent and prevents multilib version mismatches.

Also update a Summary and description to match the current Fedora
text.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agotests: Run elfcompress under testrun in run-elfclassify.sh
Mark Wielaard [Thu, 5 Dec 2019 13:22:56 +0000 (14:22 +0100)]
tests: Run elfcompress under testrun in run-elfclassify.sh

Otherwise elfcompress might run against the system libelf which might
be too old or missing.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agodebuginfod: Fix implicit conversion from 'CURLcode' to 'CURLMcode'
Mark Wielaard [Tue, 3 Dec 2019 23:39:26 +0000 (00:39 +0100)]
debuginfod: Fix implicit conversion from 'CURLcode' to 'CURLMcode'

GCC10 warns when converting the value of one enum type into another:

debuginfod-client.c:530:24: error: implicit conversion from ‘CURLcode’
                                   to ‘CURLMcode’ [-Werror=enum-conversion]
  530 |               curl_res = curl_easy_getinfo(target_handle,
      |                        ^

libcurl has different error code enums. The "easy" interfaces return
a CURLcode error. The "multi" interface functions return a CURLMcode.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agolibdwfl: Find and handle compressed vmlinuz image.
Mark Wielaard [Thu, 5 Dec 2019 14:03:54 +0000 (15:03 +0100)]
libdwfl: Find and handle compressed vmlinuz image.

Both the dwfl_linux_kernel_find_elf callback and the
dwfl_linux_kernel_report_offline reporting function only handled
vmlinix images possibly compressed with .gz, .bz2 or .xz extension.
They did not find or handle the much more common vmlinuz compressed
images.

It is not completely clear why we didn't up to now. Support for
compressed ELF files was added in 2009 and the code was updated to
to try to find the .gz, .bz2 or .xz extension variants in 2011.
But not the vmlinuz named variant.

Reported-by: Aaron Merey <amerey@redhat.com>
Tested-by: Frank Ch. Eigler <fche@redhat.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoconfig: Add an explicit Requires elfutils-debuginfod-client for debuginfod.
Mark Wielaard [Thu, 28 Nov 2019 15:17:52 +0000 (16:17 +0100)]
config: Add an explicit Requires elfutils-debuginfod-client for debuginfod.

elfutils-debuginfod depends indirectly already on the client library.
Make that explicit so they are always updated in sync. Which isn't
technically required, but makes things clearer and simpler on updates.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoconfig: versioned libdebuginfod libraries should not be in -devel package.
Mark Wielaard [Wed, 27 Nov 2019 02:21:49 +0000 (03:21 +0100)]
config: versioned libdebuginfod libraries should not be in -devel package.

The versioned library (links) should be in the runtime package.
The debuginfod-client-devel package should just contain the .so
(symlink). This isn't fatal, just sloppy. ldconfig will correct
the missing symlinks when it was missing.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoelfutils.spec.in: Sync with fedora spec, remove rhel/fedora specifics. elfutils-0.178 upstream/0.178
Mark Wielaard [Tue, 26 Nov 2019 22:34:27 +0000 (23:34 +0100)]
elfutils.spec.in: Sync with fedora spec, remove rhel/fedora specifics.

This makes the sample elfutils.spec file more like the fedora
elfutils.spec but with all fedora and rhel specifics removed.
In particular it introduces a new elfutils-libs subpackage and
updates the requires accordingly.

Other cleanups are removal of Group tags, remove dot at end of
Summary tags, add post/postun ldconfig for elfutils-libs and
elfutils debuginfod-client subpackages, remove default
defattr(-,root,root) for file lists and order binaries by name.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoPrepare for 0.178
Mark Wielaard [Tue, 26 Nov 2019 08:52:24 +0000 (09:52 +0100)]
Prepare for 0.178

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoFix BUILD_STATIC build and enable gcov for debuginfod.
Mark Wielaard [Tue, 26 Nov 2019 07:56:44 +0000 (08:56 +0100)]
Fix BUILD_STATIC build and enable gcov for debuginfod.

libdw now always needs -lpthread and -ld even when BUILD_STATIC.
BUILD_STATIC is only used when doing a gcov enabled build.
Enable gcov coverage also for debuginfod.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoelfutils.spec: Add BuildRequires: curl
Mark Wielaard [Mon, 25 Nov 2019 22:20:35 +0000 (23:20 +0100)]
elfutils.spec: Add BuildRequires: curl

The run-debuginfod-find.sh now relies on curl to fetch the metrics.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agodebuginfod: Tweak groom cycle for memory freeing
Frank Ch. Eigler [Mon, 25 Nov 2019 21:47:18 +0000 (16:47 -0500)]
debuginfod: Tweak groom cycle for memory freeing

Use the sqlite3_db_release_memory() call periodically.  It should
have no effect except hopefully smaller server memory usage.

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
4 years agotests: Don't sleep in run-debuginfod-find.sh, but wait till ready.
Mark Wielaard [Sun, 24 Nov 2019 16:38:18 +0000 (17:38 +0100)]
tests: Don't sleep in run-debuginfod-find.sh, but wait till ready.

Wait for the debuginfod server to finish and use the metrics to see
when a server is ready for the next test instead of sleeping. Also
remove DEBUGINFOD_TEST_WEBAPI_SLEEP.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agodebuginfod: Add found_{executable,debuginfo,sourcerefs}_total metrics.
Mark Wielaard [Sun, 24 Nov 2019 13:22:17 +0000 (14:22 +0100)]
debuginfod: Add found_{executable,debuginfo,sourcerefs}_total metrics.

Keeps metrics of how many executables, debuginfo and sourcerefs were
found in total for file and rpm scanners.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agotests: Use 127.0.0.1 in run-debuginfod-find.sh instead of localhost.
Mark Wielaard [Sat, 23 Nov 2019 13:09:42 +0000 (14:09 +0100)]
tests: Use 127.0.0.1 in run-debuginfod-find.sh instead of localhost.

The way curl does hostname lookups in multi_curl mode takes a very long
time under valgrind. Replace all localhosti hostnames with 127.0.0.1 so
no lookups are necessary.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agodebuginfod: add client context
Mark Wielaard [Sun, 17 Nov 2019 21:17:26 +0000 (22:17 +0100)]
debuginfod: add client context

Add a mandatory debuginfod_begin()/_end() call pair to manage a client
object that represents persistent but non-global state.  From libdwfl,
dlopen the debuginfod.so client library early on.  This hopefully
makes sure that the code (and the libcurl.so dependency) is loaded
before the program goes into multi-threaded mode.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agodebuginfod 5: add /metrics endpoint
Frank Ch. Eigler [Thu, 7 Nov 2019 09:00:16 +0000 (04:00 -0500)]
debuginfod 5: add /metrics endpoint

This webapi extensions allows admins to hook up debuginfod to a
prometheus-compatible monitoring system for general situational
statistics.  The metrics are simple enough that local curl requests
can give a user a sense of what's going on.  The metrics are
documented as unstable with respect to future versions.

4 years agodebuginfod 4: symbolic link traversal mode
Frank Ch. Eigler [Wed, 6 Nov 2019 23:53:31 +0000 (18:53 -0500)]
debuginfod 4: symbolic link traversal mode

In order to support file/rpm archives that are organized via symlink
trees, add an "-L" option to debuginfod, meaning about the same as for
find(1) or ls(1): to traverse rather than ignore symlinks.

4 years agodebuginfod 3/3: client interruptability
Frank Ch. Eigler [Mon, 4 Nov 2019 21:33:09 +0000 (16:33 -0500)]
debuginfod 3/3: client interruptability

For interactive clients such as gdb, interruptibility is important for
usability during longer downloads.  This patchset adds a
download-progress callback function to the debuginfod client library,
with which a caller app can interrupt a download as well as be
notified of its quantitative progress.

4 years agodebuginfod 2/2: server side
Frank Ch. Eigler [Mon, 28 Oct 2019 17:29:26 +0000 (13:29 -0400)]
debuginfod 2/2: server side

Add the server to the debuginfod/ subdirectory.  This is a highly
multithreaded c++11 program (still buildable on rhel7's gcc 4.8,
which is only partly c++11 compliant).  Includes an initial suite
of tests, man pages, and a sample systemd service.

Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
Signed-off-by: Aaron Merey <amerey@redhat.com>
4 years agodebuginfod 1/2: client side
Aaron Merey [Mon, 28 Oct 2019 17:29:26 +0000 (13:29 -0400)]
debuginfod 1/2: client side

Introduce the debuginfod/ subdirectory, containing the client for a
new debuginfo-over-http service, in shared-library and command-line
forms.  Two functions in libdwfl make calls into the client library to
fetch elf/dwarf files by buildid, as a fallback.  Instead of normal
dynamic linking (thus pulling in a variety of curl dependencies),
the libdwfl hooks use dlopen/dlsym.  Server & tests coming in patch 2.

Signed-off-by: Aaron Merey <amerey@redhat.com>
Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
4 years agorun-large-elf-file.sh: skip if free memory information is not available
Andreas Schwab [Thu, 14 Nov 2019 15:50:26 +0000 (16:50 +0100)]
run-large-elf-file.sh: skip if free memory information is not available

4 years agodwelf_elf_e_machine_string: Clear errno before calling strtol
Andreas Schwab [Thu, 14 Nov 2019 13:54:58 +0000 (14:54 +0100)]
dwelf_elf_e_machine_string: Clear errno before calling strtol

Avoid spurious failure if errno is modified by any other library call in
the test.

Signed-off-by: Andreas Schwab <schwab@suse.de>
4 years agolibdw: Introduce libdw_unalloc to stop Dwarf_Abbrev leaks.
Mark Wielaard [Sun, 10 Nov 2019 23:15:55 +0000 (00:15 +0100)]
libdw: Introduce libdw_unalloc to stop Dwarf_Abbrev leaks.

In the case of reading an invalid abbrev or when reading an abbrev
concurrently the Dwarf_Abbrev just created might leak because it isn't
needed after all. Introduce libdw_unalloc and libdw_typed_unalloc to
unallocate such Dwarf_Abbrevs so they don't leak.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agolibdw: Rewrite the memory handler to be more robust.
Jonathon Anderson [Sun, 25 Aug 2019 15:07:00 +0000 (10:07 -0500)]
libdw: Rewrite the memory handler to be more robust.

Pthread's thread-local variables are highly limited, which makes
it difficult to use many Dwarfs. This replaces that with a
less efficient (or elegant) but more robust method.

Signed-off-by: Jonathon Anderson <jma14@rice.edu>
4 years agoAdd configure options for Valgrind annotations.
Jonathon Anderson [Sun, 25 Aug 2019 14:53:50 +0000 (09:53 -0500)]
Add configure options for Valgrind annotations.

Signed-off-by: Jonathon Anderson <jma14@rice.edu>
4 years agolibdw: Add and use a concurrent version of the dynamic-size hash table.
Srđan Milaković [Mon, 4 Nov 2019 16:39:35 +0000 (10:39 -0600)]
libdw: Add and use a concurrent version of the dynamic-size hash table.

Signed-off-by: Srđan Milaković <sm108@rice.edu>
Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agolibdw: Don't free uninitialized Dwarf_Abbrev_Hash's of "fake" CUs.
Jonathon Anderson [Fri, 1 Nov 2019 13:14:05 +0000 (08:14 -0500)]
libdw: Don't free uninitialized Dwarf_Abbrev_Hash's of "fake" CUs.

fake_{loc,loclists,addr}_cu are Dwarf_CUs that are created separate from
all the others, so their contents are minimal and mostly initialized by
a calloc. On dwarf_end however, they are freed through the same code path
as all the others, so they call DAH_free like all the others. This changes
that so that these three are exempt from DAH and split-DWARF matters, and
swaps the calloc for a malloc so Memcheck will catch any others.

Signed-off-by: Jonathon Anderson <jma14@rice.edu>
4 years agolibdwfl: only use thread->unwound for initial frame
Omar Sandoval [Mon, 7 Oct 2019 09:05:36 +0000 (02:05 -0700)]
libdwfl: only use thread->unwound for initial frame

thread->unwound is only used for set_initial_registers (via
dwfl_thread_state_registers, dwfl_thread_state_register_pc, and a
special case in core_set_initial_registers). At that point,
thread->unwound is always the initial frame, so there's no need to
update it as we unwind the stack. Let's set it to NULL after we do the
initial setup. This simplifies the next change.

Signed-off-by: Omar Sandoval <osandov@fb.com>
4 years agolibdwfl: don't bother freeing frames outside of dwfl_thread_getframes
Omar Sandoval [Mon, 7 Oct 2019 09:05:35 +0000 (02:05 -0700)]
libdwfl: don't bother freeing frames outside of dwfl_thread_getframes

dwfl_thread_getframes always frees the state before returning, so
dwfl_getthreads and getthread don't need to do it. The only place
we allocate the state is from dwfl_thread_getframes and we always
free it before returning from that function.

Signed-off-by: Omar Sandoval <osandov@fb.com>
4 years agounstrip: Check symbol strings are terminated.
Mark Wielaard [Sat, 26 Oct 2019 20:54:49 +0000 (22:54 +0200)]
unstrip: Check symbol strings are terminated.

A corrupt ELF file could contain a .strtab section that wasn't
properly zero terminated. If so we could add a non-terminated string
to the dwelf_strtab functions, which could then crash because they
would read past the .strtab section data.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agolibcpu: Fix bounds checks and replace asserts with errors.
Mark Wielaard [Sat, 19 Oct 2019 12:01:30 +0000 (14:01 +0200)]
libcpu: Fix bounds checks and replace asserts with errors.

Add a missing bounds check, fix an off-by-one bounds check and replace
asserts with error messages.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agounstrip: Don't try to write extra bogus versym data.
Mark Wielaard [Sat, 19 Oct 2019 12:37:46 +0000 (14:37 +0200)]
unstrip: Don't try to write extra bogus versym data.

If the sh_entsize of the symver section was bogus (bigger than necessary)
then some bogus data would be written out (except that then fails because
pwrite would probably fail). Fix that by ignoring the bogus sh_entsize
and use the actual symver data size.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agounstrip: Add sanity check for bogus sh_offset of allocated sections.
Mark Wielaard [Sun, 20 Oct 2019 15:26:29 +0000 (17:26 +0200)]
unstrip: Add sanity check for bogus sh_offset of allocated sections.

unstrip tries to preserve any allocated section offset in an
executable or shared library. If the offset is extremely large that
would cause the disk to fill up because we will write out a file with
lots of padding to put the section contents at that particular
offset. Add a sanity check that makes sure we just error out if there
is such a bogus offset by checking that no offset is larger than the
original ELF file size.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agounstrip: Add various checks for bad input data.
Mark Wielaard [Mon, 21 Oct 2019 08:55:32 +0000 (10:55 +0200)]
unstrip: Add various checks for bad input data.

There were various ways to crash eu-unstrip with bad ELF input
data. Add various tests against bad data and allocate some structures
on the heap instead of on the stack.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agolibdw: Rewrite the memory handler to be thread-safe.
Jonathon Anderson [Sun, 25 Aug 2019 15:07:00 +0000 (10:07 -0500)]
libdw: Rewrite the memory handler to be thread-safe.

Signed-off-by: Jonathon Anderson <jma14@rice.edu>
4 years agoAdd some supporting framework for C11-style atomics.
Jonathon Anderson [Sun, 25 Aug 2019 15:01:51 +0000 (10:01 -0500)]
Add some supporting framework for C11-style atomics.

Uses the stdatomic.h provided by FreeBSD when GCC doesn't (ie. GCC < 4.9)

Signed-off-by: Jonathon Anderson <jma14@rice.edu>
Signed-off-by: Srđan Milaković <sm108@rice.edu>
4 years agoelflint, readelf: enhance error diagnostics
Dmitry V. Levin [Mon, 30 Sep 2019 11:39:50 +0000 (14:39 +0300)]
elflint, readelf: enhance error diagnostics

When an input file cannot be opened, include its name
into the error diagnostics.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
4 years agoreadelf: Add --dyn-sym option.
Mark Wielaard [Mon, 2 Sep 2019 22:03:22 +0000 (00:03 +0200)]
readelf: Add --dyn-sym option.

It is already possible to select the symbol table to print by name,
using --symbols=SECTION. This allows printing the dynamic symbol table
with --symbols=.dynsym. binutils readelf allows printing just the
dynamic symbol table by type using --dyn-sym. Add the same option
and document it. Also add a testcase to show --symbols=.dynsym and
--dyn-sym produce the same output.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agolibebl: Don't install libebl.a, libebl.h and remove backends from spec.
Mark Wielaard [Thu, 29 Aug 2019 21:34:11 +0000 (23:34 +0200)]
libebl: Don't install libebl.a, libebl.h and remove backends from spec.

All archive members from libebl.a are now in libdw.a. We don't generate
separate backend shared libraries anymore. So remove them from the
elfutils.spec file.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agolibcpu: Make sure left shifts are done in unsigned context.
Mark Wielaard [Sat, 7 Sep 2019 19:45:26 +0000 (21:45 +0200)]
libcpu: Make sure left shifts are done in unsigned context.

Use UINT64_C (0) instead of INT64_C (0) to make sure the calculation
is done on unsigned values. Otherwise the gcc undefined sanitizer will
warn:

libcpu/riscv_disasm.c:457:57: runtime error: left shift of negative value -1

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agotests: Add new riscv64 test files to EXTRA_DIST
Mark Wielaard [Sat, 7 Sep 2019 19:35:10 +0000 (21:35 +0200)]
tests: Add new riscv64 test files to EXTRA_DIST

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoFix whitespaces in RISC-V disasm code
Ulrich Drepper [Sat, 7 Sep 2019 09:06:13 +0000 (11:06 +0200)]
Fix whitespaces in RISC-V disasm code

4 years agoImplement RISC-V disassembler
Ulrich Drepper [Fri, 6 Sep 2019 21:49:54 +0000 (23:49 +0200)]
Implement RISC-V disassembler

4 years agoconfig: Fix spec file, add manpages and new GFDL license.
Mark Wielaard [Tue, 27 Aug 2019 22:33:41 +0000 (00:33 +0200)]
config: Fix spec file, add manpages and new GFDL license.

Now that we have manpages lets also package them. Rename COPYING to
COPYING-GFDL to make it not clash with the top-level COPYING file.
Also fix up the spec file so it can be used to create a srpm again.
Add eu-stack to the file list.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoreadelf: Actually dump hex or strings when -p or -x get section number.
Mark Wielaard [Tue, 27 Aug 2019 14:09:39 +0000 (16:09 +0200)]
readelf: Actually dump hex or strings when -p or -x get section number.

The readelf code did parse section numbers, but then failed to actually
dump the section found. Fixed by actually calling the dump function
(either the hex or string variant). Add testcase for readelf -x num.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agonm: Fix latent memory leak in show_symbols.
Mark Wielaard [Thu, 29 Aug 2019 15:46:52 +0000 (17:46 +0200)]
nm: Fix latent memory leak in show_symbols.

If there are just a handful of symbols then memory for them is
allocated on the stack, otherwise the memory is malloced. So before
freeing the memory we need to check the number of entries to know if
the memory was heap allocated or not. But since not all entries might
be used we might have decreased the number of entries to the number
we will actually show. Remember the original symbol entries to not
have a memory leak.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agotests: Use smaller self test files.
Mark Wielaard [Thu, 29 Aug 2019 14:21:35 +0000 (16:21 +0200)]
tests: Use smaller self test files.

Don't use the largest executables/libraries to reduce the make check time.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoDon't use dlopen() for libebl modules
Omar Sandoval [Mon, 26 Aug 2019 17:51:46 +0000 (10:51 -0700)]
Don't use dlopen() for libebl modules

Currently, architecture-specific code for libebl exists in separate
libebl_$ARCH.so libraries which libebl loads with dlopen() at runtime.
This makes it impossible to have standalone, statically-linked binaries
which use libdwfl if they depend on any architecture-specific
functionality. Additionally, when these libraries cannot be found, the
failure modes are non-obvious. So, let's get rid of libebl_$arch.so and
move it all into libdw.so/libdw.a, which simplifies things considerably.

Signed-off-by: Omar Sandoval <osandov@fb.com>
4 years agolibasm: Link against libebl_pic.a.
Mark Wielaard [Wed, 28 Aug 2019 21:55:24 +0000 (23:55 +0200)]
libasm: Link against libebl_pic.a.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoAdd PIC and non-PIC variants of libcpu and libebl
Omar Sandoval [Mon, 26 Aug 2019 17:51:45 +0000 (10:51 -0700)]
Add PIC and non-PIC variants of libcpu and libebl

Currently, libcpu and libebl are always compiled as PIC. An upcoming
change will add the objects from libcpu.a and libebl.a to libdw.a, which
should not be PIC unless configured that way. So, make libcpu.a and
libebl.a non-PIC and add libcpu_pic.a and libebl_pic.a.

Signed-off-by: Omar Sandoval <osandov@fb.com>
4 years agolibcpu: merge libcpu_{i386,x86_64,bpf} into one library
Omar Sandoval [Mon, 26 Aug 2019 17:51:44 +0000 (10:51 -0700)]
libcpu: merge libcpu_{i386,x86_64,bpf} into one library

In preparation for combining the libebl backend modules, combine all of
the libcpu backends into libcpu.a.

Signed-off-by: Omar Sandoval <osandov@fb.com>
4 years agolibebl: remove unnecessary variable in Makefile.am
Omar Sandoval [Mon, 26 Aug 2019 17:51:43 +0000 (10:51 -0700)]
libebl: remove unnecessary variable in Makefile.am

gen_SOURCES is assigned to libebl_a_SOURCES and never used again. Get
rid of it.

Signed-off-by: Omar Sandoval <osandov@fb.com>
4 years agoAdd elfclassify to .gitignore
Omar Sandoval [Mon, 26 Aug 2019 17:58:31 +0000 (10:58 -0700)]
Add elfclassify to .gitignore

The tool was just added, but it's missing the ignore entry.

Signed-off-by: Omar Sandoval <osandov@fb.com>
4 years agoBegin adding some docs to elfutils
Ben Woodard [Fri, 23 Aug 2019 20:42:06 +0000 (13:42 -0700)]
Begin adding some docs to elfutils

- Added doc/README
- Updated doc/ChangeLog
- Added a eu-readelf manpage based on the one from binutils.
- Added a brand new manpage for eu-elfclassify the new utility.
- Add some new files in the doc directory and sync makefile with upstream.
- Reenable the compilation of doc directory.
- Disable sgml file building
- Build man pages the automake way

Since V1
- Put man pages in the proper directories.
- Added copy of Gnu Free Documentation License
- Modified eu-readelf.1 man page to match the supported options.

Signed-off-by: Ben Woodard <woodard@redhat.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agolibdw: fix latent bug in dwarf_getcfi.c not setting default_same_value.
Jonathon Anderson [Sun, 25 Aug 2019 15:07:00 +0000 (10:07 -0500)]
libdw: fix latent bug in dwarf_getcfi.c not setting default_same_value.

Signed-off-by: Jonathon Anderson <jma14@rice.edu>
4 years agoreadelf: Add optional "SECTION" argument for --notes.
Mark Wielaard [Wed, 21 Aug 2019 10:28:56 +0000 (12:28 +0200)]
readelf: Add optional "SECTION" argument for --notes.

There are multiple sections that can contain ELF Notes. It is sometimes
nice to just list the notes from a specific section.

  -n, --notes[=SECTION]      Display the ELF notes

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoconfig/elfutils.spec.in: package eu-elfclassify
Dmitry V. Levin [Wed, 14 Aug 2019 20:45:56 +0000 (23:45 +0300)]
config/elfutils.spec.in: package eu-elfclassify

Package the new tool introduced by commit elfutils-0.177~1.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
4 years agoPrepare for 0.177 elfutils-0.177
Mark Wielaard [Tue, 13 Aug 2019 21:53:56 +0000 (23:53 +0200)]
Prepare for 0.177

Set version to 0.177.
Update NEWS and elfutils.spec.in.
Use git --get user.name and user.email for spec changelog.
Regenerate po/*.po files.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agoelfclassify: New tool to analyze ELF objects.
Florian Weimer [Tue, 13 Aug 2019 11:27:15 +0000 (13:27 +0200)]
elfclassify: New tool to analyze ELF objects.

Usage: elfclassify [OPTION...] FILE...
Determine the type of an ELF file.

All of the classification options must apply at the same time to a
particular file.  Classification options can be negated using a
"--not-" prefix.

Since modern ELF does not clearly distinguish between programs and
dynamic shared objects, you should normally use either --executable or
--shared to identify the primary purpose of a file.  Only one of the
--shared and --executable checks can pass for a file.

If you want to know whether an ELF object might a program or a shared
library (but could be both), then use --program or --library. Some ELF
files will classify as both a program and a library.

If you just want to know whether an ELF file is loadable (as program
or library) use --loadable.  Note that files that only contain
(separate) debug information (--debug-only) are never --loadable (even
though they might contain program headers).  Linux kernel modules are
also not --loadable (in the normal sense).

Without any of the --print options, the program exits with status 0 if
the requested checks pass for all input files, with 1 if a check fails
for any file, and 2 if there is an environmental issue (such as a file
read error or a memory allocation error).

When printing file names, the program exits with status 0 even if no
file names are printed, and exits with status 2 if there is an
environmental issue.

On usage error (e.g. a bad option was given), the program exits with a
status code larger than 2.

The --quiet or -q oose_filestion suppresses some error warning output,
but doesn't change the exit status.

Classification options
  --core                 File is an ELF core dump file
  --debug-only           File is a debug only ELF file (separate .debug,
                         .dwo or dwz multi-file)
  --elf                  File looks like an ELF object or archive/static
                         library (default)
  --elf-archive          File is an ELF archive or static library
  --elf-file             File is an regular ELF object (not an
                         archive/static library)
  --executable           File is (primarily) an ELF program executable (not
                         primarily a DSO)
  --library              File is an ELF shared object (DSO) (might also be
                         an executable)
  --linux-kernel-module  File is a linux kernel module
  --loadable             File is a loadable ELF object (program or shared
                         object)
  --program              File is an ELF program executable (might also be a
                         DSO)
  --shared               File is (primarily) an ELF shared object (DSO)
                         (not primarily an executable)
  --unstripped           File is an ELF file with symbol table or .debug_*
                         sections and can be stripped further

Input flags
  -f, --file             Only classify regular (not symlink nor special
                         device) files
  --no-stdin             Do not read files from standard input (default)
  --stdin                Also read file names to process from standard
                         input, separated by newlines
  --stdin0               Also read file names to process from standard
                         input, separated by ASCII NUL bytes
  -z, --compressed       Try to open compressed files or embedded (kernel)
                         ELF images

Output flags
  --matching             If printing file names, print matching files
                         (default)
  --no-print             Do not output file names
  --not-matching         If printing file names, print files that do not
                         match
  --print                Output names of files, separated by newline
  --print0               Output names of files, separated by ASCII NUL

Additional flags
  -q, --quiet            Suppress some error output (counterpart to
                         --verbose)
  -v, --verbose          Output additional information (can be specified
                         multiple times)

  -?, --help             Give this help list
      --usage            Give a short usage message
  -V, --version          Print program version

Report bugs to https://sourceware.org/bugzilla.

Signed-off-by: Florian Weimer <fweimer@redhat.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agolibdwelf: Make dwelf_elf_begin return NULL only when there is an error.
Mark Wielaard [Sun, 11 Aug 2019 22:43:22 +0000 (00:43 +0200)]
libdwelf: Make dwelf_elf_begin return NULL only when there is an error.

dwelf_elf_begin was slightly different from elf_begin in case the file
turned out to not be an ELF file. elf_begin would return an Elf handle
with ELF_K_NONE. But dwelf_elf_begin would return NULL. This made it
impossible to tell the difference between a file or decompression error
and a (decompressed) file not being an ELF file.

Since dwelf_elf_begin could still return different kinds of ELF files
(ELF_K_ELF or ELF_K_AR - and theoretically ELF_K_COFF) this was not
really useful anyway. So make it so that dwelf_elf_begin always returns
an Elf handle unless there was a real error reading or decompressing
the file. Otherwise return NULL to make clear there was a real error.

Make sure that the decompression function returns DWFL_E_BADELF only
when the file isn't compressed. In which case the Elf handle won't
be replaced and can be returned (as ELF_K_NONE).

Add a new version to dwelf_elf_begin so programs can rely on it
returning NULL only for real errors.

Signed-off-by: Mark Wielaard <mark@klomp.org>
4 years agolibdwfl: Fix fd leak/closing wrong fd after dwfl_core_file_report()
Omar Sandoval [Mon, 5 Aug 2019 10:40:05 +0000 (03:40 -0700)]
libdwfl: Fix fd leak/closing wrong fd after dwfl_core_file_report()

dwfl_segment_report_module() (used only by dwfl_core_file_report())
opens a file descriptor and/or an Elf handle, reports a module, and
assigns mod->main.elf. However, it doesn't assign mod->main.fd, so it is
left as 0. This causes two problems:

1. We leak the file descriptor for the module.
2. When we free the module, we close file descriptor 0 (stdin).

Fix it by assigning mod->main.fd.

Signed-off-by: Omar Sandoval <osandov@fb.com>
5 years agoAdd backend support for C-SKY
Mao Han [Tue, 16 Jul 2019 11:14:24 +0000 (19:14 +0800)]
Add backend support for C-SKY

C-SKY V2 ABI manual:
https://github.com/c-sky/csky-doc/blob/master/C-SKY_V2_CPU_Applications_Binary_Interface_Standards_Manual.pdf
C-SKY architecture user guide:
https://github.com/c-sky/csky-doc/blob/master/CSKY%20Architecture%20user_guide.pdf

Signed-off-by: Mao Han <han_mao@c-sky.com>
5 years agolibdwelf: Add dwelf_elf_e_machine_string and use it in readelf.
Mark Wielaard [Fri, 28 Jun 2019 17:21:59 +0000 (19:21 +0200)]
libdwelf: Add dwelf_elf_e_machine_string and use it in readelf.

To print eh human readable description of the ELF e_machine header field
we used the ebl name. But this is not set for most EM constants. Introduce
a new function dwelf_elf_e_machine_string that does work for all known
EM values. Use that in eu-readelf to print a string representation of the
e_machine value.

Since this was the only usage of ebl->name, remove that from struct ebl.

Also add a testcase that makes sure dwelf_elf_e_machine_string works for
all EM values in the libelf/elf.h header so we will immediately notice
when a new value appears.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agotests: Add 2GB to mem_needed running run-large-elf-file.sh with valgrind
Mark Wielaard [Mon, 1 Jul 2019 15:08:43 +0000 (17:08 +0200)]
tests: Add 2GB to mem_needed running run-large-elf-file.sh with valgrind

Running under valgrind just requires even more free memory.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Fix some 32bit offset/size issues that break updating 4G+ files.
Mark Wielaard [Sun, 24 Feb 2019 22:51:16 +0000 (23:51 +0100)]
libelf: Fix some 32bit offset/size issues that break updating 4G+ files.

Some years ago, in commit b1d0b0fc "libelf: Use int64_t for offsets in
libelf.h", we changed the public interface to use 64bit offsets/sizes.
This wasn't really a API change, before we relied on loff_t always
being 64bits on all platforms.

We didn't change the implementation to use the int64_t type though.
That was a little confusing, since the function definitions used a
different type, int64_t, from the function implementations, off_t.
Since we always build with _FILE_OFFSET_BITS=64 this should be fine.
But it was a bit sloppy and confusing.

Worse is that we got the translation of offset/sizes wrong in a
couple of places when translating to ELF types. In various places
we would use Elf32_Word or Elf64_Word. But both are 32bit (unsigned)
types! As is GElf_Word. Elf32_Off is 32bits and Elf64_Off is 64bits.
But we were not using those consistently.

This patch introduces comments for the usage of [G]Elf(32|64)Word in
libelf that are correct. And introduces Elf(32|64)_SizeWord in
elf32_updatenull.c where we want to make a difference between sizes
and offsets (the ELF variants are both unsigned, while int64_t/loff_t
is signed).

It also includes a new run-large-elf-file.sh test that creates a
large ELF files (one 64bit, little endian, rel and another big endian,
non-rel) and runs eu-strip, eu-elflint, eu-unstrip and eu-elfcmp.
Before this patch, that test case fails and creates corrupt ELF files.

The test is guarded by some checks that try to make sure there is
enough disk space and memory available on the machine. The test is
skipped otherwise.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agostack: Fix error message when dwfl_core_file_attach fails.
Mark Wielaard [Mon, 24 Jun 2019 22:21:42 +0000 (00:21 +0200)]
stack: Fix error message when dwfl_core_file_attach fails.

Reported-by: Mao Han <han_mao@c-sky.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agostrip don't mmap debug output file.
Mark Wielaard [Mon, 4 Mar 2019 19:04:18 +0000 (20:04 +0100)]
strip don't mmap debug output file.

Using ELF_C_WRITE_MMAP sometimes causes unexpected errors when disk
space is low. When writing out the file, the output file is first
extended so that it covers the whole file/mmap size. But it might
be that the file system allowed the extension as a sparse file. In
that case writing to the file through the mmap might still fail and
produce a SIGBUS if the disk is full. This is confusing to the user.

Using ELF_C_WRITE will produce "normal" errors when the file cannot
be written out. It also seems to use less memory because the debug
file is created from scratch. So the memory is first read into the
ELF data structure buffers, then written out as a whole. In this case
the mmap output buffer is just overhead.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agotests: elfcopy --reverse-offs should only swap sections next to each other.
Mark Wielaard [Mon, 3 Jun 2019 16:00:20 +0000 (18:00 +0200)]
tests: elfcopy --reverse-offs should only swap sections next to each other.

The run-reverse-sections-self.sh (which really should have been called
"swap-sections") could fail if the ELF file had sections that were not
directly next to each other swapped. Add a check to make sure the swapped
sections are actually directly next to each other.

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibebl: Try harder to find backend library in bin and lib origin paths.
Mark Wielaard [Sat, 11 May 2019 14:55:01 +0000 (16:55 +0200)]
libebl: Try harder to find backend library in bin and lib origin paths.

eblopenbackend tries to find libraries based on the $ORIGIN/../$LIB/
path. But depending on whether the system is multilib or multiarch
this doesn't always work. On multilib systems $LIB is always just one
directory deep (it is either .../lib or .../lib64) but on multiarch
systems it can be multiple directories deep (.../lib/x86_64-linux-gnu).
This means that on multiarch systems $ORIGIN/../$LIB only works for
binaries (where origin is .../bin/), but not for libraries.

Most of the time it still works because of RPATH which is tried afterwards.
But RPATH processing does not always work reliable.

So try multiple paths first. The first time using the $ORIGIN as if it
came from an executable (in bin/) and then using the $ORIGIN as if it
came from an library (in lib[64]/ or lib/<arch>/). So first time using
../$LIB and second time just with the elfutils EBL_SUBDIR.

The first is what we do now and always work on multilib systems. The
second try works when loading relative to a library whether on a multilib
or multiarch system.

Then we use the same fallback (not using any path) we used already
(to take advantage of any RPATH or LD_LIBRARY_PATH setting).

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
5 years agolibelf: Mark shdr_flags dirty if offset or size changes during update.
Mark Wielaard [Sat, 11 May 2019 22:37:45 +0000 (00:37 +0200)]
libelf: Mark shdr_flags dirty if offset or size changes during update.

We forgot to mark the shdr_flags dirty when only the sh_size or
sh_offset changed during elf_update (). This meant that if there were
no other shdr changes we only wrote out the section data, but didn't
write out the shdr table to the file.

Add a testcase that puts some sections in the reverse order and then
writes out the resulting file again without doing any other
updates. This would show the issue after write out of the
(re-reversed) ELF file (the .shstrtab section offset would be wrong
causing all section names to be garbage). Also run a self test.

Signed-off-by: Mark Wielaard <mark@klomp.org>