Commit Graph

54 Commits

Author SHA1 Message Date
zhanghaibo 94edba6285 use gnu99 std and fix potential overflow
Signed-off-by: zhanghaibo <zhanghaibo0@huawei.com>
Change-Id: Ied6da97755c78f320ad43213077af6e674bb9d24
2023-04-10 18:06:21 +08:00
Martin Liska adc201f819 Come up with startswith function.
New function in system.h that returns true if a string has a given
prefix, false otherwise.  Use it in place of strncmp.

Signed-off-by: Martin Liška <mliska@suse.cz>
2021-05-12 11:56:57 +02:00
Mark Wielaard 52536d7d21 libdwfl: Add ZSTD support.
Newer kernels might be compressed using ZSTD add support to libdwfl open
so we can can automatically read ZSTD compressed files and kernel images.

The support is very similar to the bzip2 and lzma support, but slightly
different. With a bit more macros it could maybe have used the gzip.c
USE_INFLATE code path. But I felt that the many macros didn't really help
understand the code. So the unzip routine has a slightly different code
path for ZSTD.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
2020-09-21 15:17:00 +02:00
Mark Wielaard eff30a6dab libdwfl: read_address should use increasing address in intuit_kernel_bounds
In kernels from 4.14 up to 4.19 in /proc/kallsyms there are special
__entry_SYSCALL_64_trampoline symbols. The problem is that they come
after the last kernel address, but before the module addresses.
And they are (much) smaller than the start address we found. This
confuses intuit_kernel_bounds and makes it fail.

Make sure to check read_address returns an increasing address when
searching for the end.

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

Reported-by: Vitaly Chikunov <vt@altlinux.org>
Signed-off-by: Mark Wielaard <mark@klomp.org>
2020-06-28 15:27:25 +02:00
Mark Wielaard 6e36936878 libdwfl: When we find a compressed image, use that, don't search for others
We try to find a compressed vmlinux image ending with either .gz, bz2 or
xz. Stop searching if we find one. Otherwise we will leak a file descriptor
for an earlier one we opened.

Signed-off-by: Mark Wielaard <mark@klomp.org>
2020-06-19 19:03:14 +02:00
Mark Wielaard 268e805de0 libdwfl: Fix some GCC10 -Wnull-dereference issues.
GCC10 on some arches will warn about possible NULL derefences.
In the libdwfl linux-kernel-modules.c cases it might be caught already
by earlier calls to get_release (). But it is hard to see that will
really always happen. So do an explicit NULL check just in case.

Signed-off-by: Mark Wielaard <mark@klomp.org>
2020-02-06 21:29:55 +01:00
Mark Wielaard 4a90cb1114 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>
2019-12-09 20:20:01 +01:00
Mark Wielaard 92ded1f11c libdwfl: Initialize notes early in intuit_kernel_bounds.
We fake initialization of notes with an empty asm statement.  But
it is simpler and less confusing to just initialize notes just
before the fopen.

Signed-off-by: Mark Wielaard <mark@klomp.org>
2019-02-28 22:19:03 +01:00
Mark Wielaard 5199e15870 Recognize and parse GNU Property notes.
GNU Property notes are different from normal notes because they use
variable alignment/padding of their fields. They are 8 byte aligned,
but use 4 byte fields. The name is aligned at 4 bytes and padded so
that, the desc is aligned at 8 bytes. The whole note is padded to
8 bytes again. For normal notes all fields are both 4 bytes wide and
4 bytes aligned.

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

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

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

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

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
2018-10-29 00:57:57 +01:00
Ulf Hermann e88787f9cd Don't look for kernel version if not running on linux
We don't want to use it, even if it exists.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
Signed-off-by: Mark Wielaard <mark@klomp.org>
2017-05-02 23:24:53 +02:00
Ulf Hermann 5dd27ffbb3 Include sys/types.h before fts.h
The bad fts not only needs to be included before config.h, but also
requires various special types without including sys/types.h.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
2017-04-25 23:50:46 +02:00
Ulf Hermann b3248e684c Check for existence of mempcpy
If it doesn't exist, provide a definition based on memcpy.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
2017-02-17 10:45:39 +01:00
Ulf Hermann cb379c4d78 Always use the same method to query the system page size
This makes it easier to write a replacement for it on systems where
sysconf(3) doesn't exist.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
2017-02-15 15:32:45 +01:00
Mark Wielaard ee225020a8 Only workaround fts.h if we have a bad version that doesn't handle LFS.
Older versions of glibc included an fts implementation that didn't have
Large File System support. We worked around that in linux-kernel-modules.c
by including it early before config.h and then redefining some symbols
to get the 64-bit versions. This is somewhat fragile and not necessary
with newer glibc. If possible we want the 64bit fts version always.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-11-23 20:51:35 +01:00
Ravi Bangoria 7802e6e57d libdwfl: Check for kernel debuginfo file without .debug extension as well
Elfutils, looking for kernel debuginfo file, tries to find it at
various places. If elfutils finds /boot/vmlinu*x* file, it checks
for debufginfo section. If debuginfo is not present, it saves it as
'main elf' and continue looking for debuginfo file having .debug
extension i.e. vmlinux-RELEASE.debug.

'Ubuntu on powerpc' installs kernel as /boot/vmlinux and installs
debuginfo without any extension as /usr/lib/debug/boot/vmlinux-RELEASE
and hence, elfutils is not able to find the debuginfo file.

Here is the launchpad bug for the same:
  https://bugs.launchpad.net/ubuntu/+source/systemtap/+bug/1537125

This patch adds functionality to search for a kernel or debuginfo file
both with and without .debug extension.

Signed-off-by: Ravi Bangoria <ravi.bangoria@linux.vnet.ibm.com>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-02-22 20:46:30 +01:00
Mark Wielaard dd131168e2 Simplify code and build now that zlib support is no longer optional.
Now that we always require zlib support we don't need to conditionally
build or compile code that depends on it.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2016-01-08 13:35:48 +01:00
Josh Stone 3425454a10 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.

Signed-off-by: Josh Stone <jistone@redhat.com>
2015-10-09 10:10:37 -07:00
Chih-Hung Hsieh a3a76e3615 Move 4 libdwfl nested functions.
Now they should compile with clang.

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.

Signed-off-by: Chih-Hung Hsieh <chh@google.com>
2015-10-05 16:49:16 +02:00
Mark Wielaard 08e5ae6fc8 libdwfl: Don't stack allocate alternate_name in dwfl_linux_kernel_find_elf.
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2015-05-27 23:04:31 +02:00
Mark Wielaard 251f1459aa libdwfl: Minimize stack usage in dwfl_linux_kernel_report_offline.
Don't stack allocate module name. Also fixes a latent bug (if the module
file didn't have a suffix - which is very unlikely) and an inefficiency.
We only need to substitue chars up to the suffix.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2015-05-27 23:04:31 +02:00
Mark Wielaard 9d29ed2989 libdwfl: Handle LZMA .ko.xz compressed kernel modules.
Linux kernel modules can not just be compressed with gz and bz2, but also
with xz.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2014-08-15 20:29:20 +02:00
Mark Wielaard df85bf9902 libdwfl: Move dwz alt multi file searching to find_debuginfo callback.
Don't hard code the Dwarf dwz alt multi file search but allow the user
to override it through the standard Dwfl_Callbacks. Also move ownership
completely to the user of dwarf_setalt by removing free_alt from Dwarf
and adding alt, fd and elf fields to Dwfl_Module. Add a relative .dwz
file test case.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2014-05-02 17:00:48 +02:00
Mark Wielaard 641a4f62f1 libdwfl: Correct nested asprintf result check in report_kernel_archive.
Because of wrongly placed parens the result of only one asprintf call
was checked correctly. Causing dwfl_linux_kernel_report_offline to return
ENOMEM. Rewrite nested if unlikely check into separate if statements to
make clear what is actually being checked and what the actual unlikely
condition is.

Reported against systemtap "build-id difficulties with hand-built kernels"
https://sourceware.org/bugzilla/show_bug.cgi?id=16358

Reported-by: Crestez Dan Leonard <lcrestez@ixiacom.com>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2013-12-21 17:03:11 +01:00
Mark Wielaard 16fa414aff libdwfl/linux-kernel-modules.c (report_kernel): Pass add_p_vaddr as true.
On some architectures (e.g. x86_64) the vmlinux is ET_EXEC, while on
others (e.g. ppc64) it is ET_DYN.  In both cases the phdr p_vaddr will
be non-zero.  We want the image to be placed as if it was ET_DYN, so
pass true for add_p_vaddr which will do the right thing (in combination
with a zero base) in either case.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2013-08-26 15:59:28 +02:00
Jan Kratochvil 904aec2c2f Add parameter add_p_vaddr to dwfl_report_elf.
libdwfl/
	* dwfl_report_elf.c (__libdwfl_report_elf): Add parameter add_p_vaddr.
	Set it to true for ET_EXEC and ET_CORE.  Provide alternative
	setup of START and BIAS if !ADD_P_VADDR.  Set END from BIAS, not BASE.
	(dwfl_report_elf): Add parameter add_p_vaddr.  Pass it down.  Add
	NEW_VERSION.
	(_compat_without_add_p_vaddr_dwfl_report_elf) <SHARED>: New, with
	COMPAT_VERSION.
	* libdwfl.h (dwfl_report_elf): Add parameter add_p_vaddr.  Describe it.
	* libdwflP.h (__libdwfl_report_elf): Add parameter add_p_vaddr.
	* link_map.c (report_r_debug): Use true add_p_vaddr for dwfl_report_elf.
	* linux-kernel-modules.c (report_kernel): Use false add_p_vaddr for
	dwfl_report_elf.
	* offline.c (process_elf): Use true add_p_vaddr for dwfl_report_elf.

tests/
	* dwfl-report-elf-align.c: Use false add_p_vaddr for dwfl_report_elf.

Signed-off-by: Jan Kratochvil <jan.kratochvil@redhat.com>
2013-05-05 19:08:50 +02:00
Mark Wielaard 76c54597ca Fix two failure condition checks in libdwfl/linux-kernel-modules.c.
In report_kernel () if find_kernel_elf () fails then fname hasn't been
allocated or already freed. Don't free it again.

Brackets around unlikely expression in report_kernel_archive were incorrect
making the check always succeed.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
2012-12-11 22:37:45 +01:00
Mark Wielaard de2ed97f33 Update name, license and contributor policy.
* Change name from "Red Hat elfutils" to "elfutils".
* Update license of standalone tools and test from GPLv2 to GPLv3+.
* Change license of libraries from GPLv2+exception to GPLv2/LGPLv3+.
* Add Developer Certificate of Origin based contributor policy.

top-level:

- COPYING: Upgraded from GPLv2 to GPLv3.
- CONTRIBUTING, COPYING-GPLv2, COPYING-LGPLv3: New files.
- NEWS: Added note about new contribution and license policy.
- Makefile.am: Updated to GPLv3, added new files to EXTRA_DIST.
- configure.ac: Update to GPLv3, changed AC_INIT name to 'elfutils'.

backends, lib, libasm, libcpu, libdw, libdwfl, libebl, libelf:

- All files updated to GPLv2/LGPLv3+. Except some very small files
  (<5 lines) which didn't have any headers at all before, the linker
  .maps files and the libcpu/defs files which only contain data and
  libelf/elf.h which comes from glibc and is under LGPLv2+.

config:

- elfutils.spec.in: Add new License: headers and new %doc files.
- Update all license headers to GPLv2/LGPLv3+ for files used by libs.

src, tests:

- All files updated to GPLv3+. Except for the test bz2 data files, the
  linker maps and script files and some very small files (<5 lines)
  that don't have any headers.

Signed-off-by: Richard Fontana <rfontana@redhat.com>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2012-06-05 23:12:05 +02:00
Mark Wielaard dbb490fad0 Fix libdwfl compile issue in case none of the compression libraries are there.
* linux-kernel-modules.c (vmlinux_suffixes): Guard definition
    by check for zlib, bzlib or lzma defines to check it isn't empty.
    (try_kernel_name): Use same guard for use of vmlinux_suffixes.
2011-04-11 17:24:16 +02:00
Roland McGrath 6ecdead8c0 libdwfl: Search for Linux kernel binaries with compression file name suffixes. 2011-02-11 12:29:45 -08:00
Roland McGrath 81cfccae4f libdwfl: Fix Linux kernel module search when dwfl_build_id_find_elf closes fd on success. 2010-06-30 00:45:58 -07:00
Roland McGrath be9c4d7683 Fix /proc/kallsyms grovelling. 2010-05-19 21:24:27 -07:00
Roland McGrath a4b1a95434 Make -k/-K prune "source" subdirectory. 2009-01-08 20:53:26 -08:00
Roland McGrath 705364b2bf Fix nits in .ko.{gz,bz2} matching. 2009-01-06 02:05:48 -08:00
Roland McGrath 60205c9622 libdwfl -k/-K match compressed .ko files 2009-01-06 00:10:23 -08:00
Roland McGrath 1d8bb25cac src/
(find_symbol): Likewise.
	Convert plain number, or handle strings like "(section)+offset"
	or "symbol+offset".
2008-08-07 08:39:41 +00:00
Roland McGrath 0fa20e84d7 libdwfl/
2008-05-06  Roland McGrath  <roland@frob.com>

	* linux-kernel-modules.c (dwfl_linux_kernel_report_offline): Use
	FTS_LOGICAL here too.
	(dwfl_linux_kernel_find_elf): Likewise.
2008-05-07 18:37:14 +00:00
Roland McGrath 9897d66b4e propagate from branch 'com.redhat.elfutils' (head bef2134618ae8468c403066e350769ec46e303b7)
to branch 'com.redhat.elfutils.roland.pending' (head 0a8081a6b3306c7fc090f01691f4c561b841d4f0)
2008-04-29 01:49:50 +00:00
Roland McGrath e4c22ea004 2007-10-23 Roland McGrath <roland@redhat.com>
* linux-kernel-modules.c (report_kernel_archive): Reorder the kernel
	module to appear first.
2007-10-23 13:07:39 +00:00
Ulrich Drepper b597dfad92 merge of '92c36bfdbc6468d1711c043b530e0dfe5abb6dec'
and 'c22c8c43f8f68b0bffd4d5ccdb2282c958268742'
2007-10-16 05:21:27 +00:00
Ulrich Drepper 3fc3d7bd6b Build fixes for uninitialized variables.
Add some branch prediction.
2007-10-04 18:40:28 +00:00
Roland McGrath 59ea7f33f7 src/
2007-10-04  Roland McGrath  <roland@redhat.com>

	* readelf.c (print_archive_index): New variable.
	(options, parse_opt): Accept -c/--archive-index to set it.
	(dump_archive_index): New function.
	(process_file): Take new arg WILL_PRINT_ARCHIVE_INDEX.
	Call dump_archive_index on archives if set.
	(main): Update caller.
	(any_control_option): Give it file scope, moved out of ...
	(parse_opt): ... here.

tests/
2007-10-04  Roland McGrath  <roland@redhat.com>

	* run-readelf-test4.sh: New file.
	* Makefile.am (TESTS, EXTRA_DIST): Add it.
2007-10-04 08:50:09 +00:00
Roland McGrath 099dd52727 missed testdata files 2007-08-13 22:58:36 +00:00
Roland McGrath 9aa8ef7fbb src/
2007-05-18  Roland McGrath  <roland@redhat.com>

	* unstrip.c (copy_elided_sections): Match up non-NOBITS sections with
	stripped file, so as not to duplicate a section copied in both.

	* strip.c (handle_elf): Keep SHT_NOTE section copies in the debug file.

tests/
2007-05-18  Roland McGrath  <roland@redhat.com>

	* run-strip-test4.sh (stripped, debugfile): Use new reference files.
	* testfile37.bz2: New data file.
	* testfile37.debug.bz2: New data file.
	* run-unstrip-test2.sh: New file.
	* Makefile.am (TESTS, EXTRA_DIST): Add them.
2007-05-18 08:59:43 +00:00
Ulrich Drepper 6258e7486e propagate from branch 'com.redhat.elfutils.roland.pending' (head e0c7abd450c9e49093cfae30af8a22782a74a403)
to branch 'com.redhat.elfutils' (head 2c784d50eee72e33972c333138a3a28df304da63)
2007-03-13 06:22:40 +00:00
Ulrich Drepper aa915fd3d7 propagate from branch 'com.redhat.elfutils.roland.pending' (head c44dcfac5b545aecb173fede31f34cb003be0173)
to branch 'com.redhat.elfutils' (head 4196d4e01486bdeb0c0632291881d1c6d7163fab)
2007-02-05 07:25:33 +00:00
Roland McGrath 44865b9e1e 2007-01-10 Roland McGrath <roland@redhat.com>
* linux-kernel-modules.c (report_kernel): Check asprintf return value
	directly instead of via side effect, to silence warn_unused_result.
	(dwfl_linux_kernel_report_offline): Likewise.
	(dwfl_linux_kernel_find_elf): Likewise.
	(dwfl_linux_kernel_module_section_address): Likewise.
	* find-debuginfo.c (try_open): Likewise.
2007-01-11 05:06:16 +00:00
Roland McGrath a1cee0760d libdwfl/
2006-08-14  Roland McGrath  <roland@redhat.com>

	* linux-kernel-modules.c (try_kernel_name): If the call to
	dwfl_standard_find_debuginfo produces no results, try it again
	with NULL as DEBUGLINK_FILE to try *FNAME with .debug suffix.

	* find-debuginfo.c (DEFAULT_DEBUGINFO_PATH): Macro moved ...
	* libdwflP.h: ... to here.
	* linux-kernel-modules.c (try_kernel_name): Skip manual open if it
	repeats the first thing dwfl_standard_find_debuginfo will try.

	* linux-kernel-modules.c (MODULE_SECT_NAME_LEN): New macro.
	(dwfl_linux_kernel_module_section_address): If a /sys file is missing
	and the section name is >= MODULE_SECT_NAME_LEN, try truncating the
	section name.
2006-08-15 01:37:23 +00:00
Ulrich Drepper 1e9ef50681 Fix FSF address. No exception for libdwarf. 2006-04-04 22:29:06 +00:00
Ulrich Drepper 361df7da6d propagate from branch 'com.redhat.elfutils.roland.pending' (head 4f8fc821345feef58624f0aa5b470d4827577d8c)
to branch 'com.redhat.elfutils' (head 76e26cb54695fd3b21ee8fb5be3036bd68200633)
2006-04-04 21:38:57 +00:00
Roland McGrath 2e53b9950e 2005-11-21 Roland McGrath <roland@redhat.com>
* linux-kernel-modules.c (dwfl_linux_kernel_module_section_address):
	Don't leak malloc'd file name.
	If a /sys/.../sections file is missing and starts with ".init",
	try the variant with "_init" too; catches PPC64 kernel braindamage.
2005-11-21 23:40:54 +00:00