Commit graph

272 commits

Author SHA1 Message Date
Christos Margiolis 440dbf8a4b readelf: fix -wR option, memory leaks, and -wf minor bug
Reviewed by:	markj
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D38419

(cherry picked from commit cec2d0b1c7)
2023-03-20 08:52:57 -04:00
Mark Johnston dc1ba2a432 libdwarf: Add some constants from DWARF 5
This is not exhaustive - DWARF 5 has some new enumeration types not
implemented here - but I think I caught all the ones that are extended
in DWARF 5, plus the new compilation unit type (DW_UT_*), needed when
parsing .debug_info headers.

These were useful when extending libdwarf/ctfconvert/readelf to handle
DWARF generated by gcc 12, which is version 5 by default.

Reviewed by:	emaste
MFC after:	3 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D38273

(cherry picked from commit 7bb441c866)
2023-02-28 09:05:00 -05:00
Mitchell Horne 6b9f6aeac6 elftoolchain: update the list of riscv relocations
A few have been added, a couple removed.

Reviewed by:	jrtc27, imp, emaste
MFC after:	3 days
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D38175

(cherry picked from commit 537c166b76)
2023-02-06 15:34:37 -04:00
Ed Maste f61fca7409 readelf: match GNU readelf formatting
GNU readelf emits a blank line before, and uses single quotes around the
symbol table name, in each "Symbol table 'name' contains ## entries"
header.

Reviewed by:	markj
MFC after:	3 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D37746

(cherry picked from commit 766145637d)
2023-01-08 21:31:15 -05:00
John Baldwin 382ffc8786 x86: Add a NT_X86_SEGBASES register set.
This register set contains the values of the fsbase and gsbase
registers.  Note that these registers can already be controlled
individually via ptrace(2) via MD operations, so the main reason for
adding this is to include these register values in core dumps.  In
particular, this will enable looking up the value of TLS variables
from core dumps in gdb.

The value of NT_X86_SEGBASES was chosen to match the value of
NT_386_TLS on Linux.  The notes serve similar purposes, but FreeBSD
will never dump a note equivalent to NT_386_TLS (which dumps a single
segment descriptor rather than a pair of addresses) and picking a
currently-unused value in the NT_X86_* range could result in a future
conflict.

Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D34650

(cherry picked from commit 931983ee08)
2022-05-13 09:45:19 -07:00
John Baldwin e0b7d19d44 arm,arm64: Add a NT_ARM_TLS read-only register set.
This register set exposes the per-thread TLS register.  It matches the
layout used by Linux on arm64.  Linux does not implement this note for
32-bit arm.

Reviewed by:	andrew, markj
Sponsored by:	University of Cambridge, Google, Inc.
Differential Revision:	https://reviews.freebsd.org/D34595

(cherry picked from commit b2cb74c22c)
2022-05-12 17:39:00 -07:00
Ed Maste d69b681240 readelf: add Arm address mask note type NT_ARM_ADDR_MASK
Reviewed by:	andrew
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D34346

(cherry picked from commit 817e68d8e6)
2022-02-28 19:24:44 -05:00
Ed Maste 98a0af7b6b readelf: add newer Linux core arm note types
Sponsored by:	The FreeBSD Foundation

(cherry picked from commit 089eb4ead8)
2022-02-22 14:54:08 -05:00
Mark Johnston 40d6b2a362 exec: Remove the stack gap implementation
ASLR stack randomization will reappear in a forthcoming commit.  Rather
than inserting a random gap into the stack mapping, the entire stack
mapping itself will be randomized in the same way that other mappings
are when ASLR is enabled.

No functional change intended, as the stack gap implementation is
currently disabled by default.

Reviewed by:	kib
Sponsored by:	The FreeBSD Foundation

(cherry picked from commit 758d98debe)
2022-01-31 09:48:57 -05:00
Ed Maste cb4d27f48e readelf: add Go Build ID ELF note support
Reviewed by:	markj
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D33379

(cherry picked from commit ca457394fc)
2022-01-04 21:04:19 -05:00
Ed Maste 8d91740f96 strip/objcopy: handle empty file as unknown
Previously strip reported a somewhat cryptic error for empty files:

    strip: elf_begin() failed: Invalid argument

Add a special case to treat empty files as with an unknown file format.
This is consistent with llvm-strip.  GNU strip produces no output which
does not seem like useful behaviour (but it does exit with status 1).

Reported by:	andrew
Reviewed by:	markj
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D32648

(cherry picked from commit 179219ea04)
2021-11-08 08:54:23 -05:00
Chris Rees 96e101bec9 elftoolchain: stop leaving tempfiles on error
Temporary files were not cleaned up, resulting in $TMPDIR or even
the current directory becoming littered with ecp.* files.

This happened with error and even sometimes on success!

Approved by:		dim
MFC after:		4 weeks
Accepted upstream:	https://sourceforge.net/p/elftoolchain/code/3918/
Differential Revision:	https://reviews.freebsd.org/D28651

(cherry picked from commit 5ac70383c8)
2021-11-08 08:54:23 -05:00
Andrew Turner 3a98aae1e5 Teach the readelf about arm64 gnu properties
On arm64 binaries can be tagged as using BTI or PAC. Add support to
decode these to the elftoolchain readelf.

To simplify the code use a table based method to find the flag
description table.

Reviewed by:	emaste
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D32315

(cherry picked from commit aacbf3fb84)
2021-10-25 14:46:44 +01:00
Ed Maste 04676c953a readelf: document that -u / --unwind is not yet implemented
ELF tool chain readelf accepts -u / --unwind but just ignores the
option.  This was previously undocumented, which could be confusing for
someone encountering `readelf -u` (in a script or GNU readelf example).

Reported by:	markj (in D32003)
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation

(cherry picked from commit deef4b8ce8)
2021-09-23 20:57:10 -04:00
Ed Maste ac8994cf5b readelf: include notes (-n) and unwind (-u) in --all/-a
This matches the GNU and LLVM versions of readelf.

As markj noted in the review -u is not actually implemented yet and has
no effect.  The option is accepted and just ignored.

Reported by:	andrew
Reviewed by:	andrew, markj
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D32003

(cherry picked from commit f161abf9f2)
2021-09-23 20:57:10 -04:00
Ed Maste f4c3c0189f readelf: return error in case of invalid file
GNU readelf exits with an error for a number of invalid file cases.
Previously ELF Tool Chain readelf always exited with 0.  Now we exit 1
upon detecting an error with one or more input files, but in any case
all of them are processed.

This should catch common failure cases.  We still do not report an error
for some types of malformed ELF files, but this is consistent with GNU
readelf.

PR:		252727
Reviewed by:	jkoshy, markj
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D29377

(cherry picked from commit ea444392bb)
2021-04-23 11:37:49 -04:00
Jessica Clarke a20427cc03 elftoolchain: Support building on Arm-based Macs
Currently macOS and DragonFlyBSD get their own special case and only
handle x86. Since all the FreeBSD cases should be general enough for
macOS and DragonFlyBSD (and the x86 ones are identical to the existing
ones) we can just delete the special cases and reuse the FreeBSD ones.

Note that upstream has since removed all the architecture-specific
checks in this file, with the only code relevant to us being an
endianness check that uses the generic compiler-provided macros. Thus
this patch will not be upstreamed, and will be dropped in a future
vendor import.

(cherry picked from commit 839fdcfc0c)
2021-03-27 02:10:29 +00:00
Alex Richardson ab0e7589d2 readelf: Fix printing NT_FREEBSD_ARCH_TAG
Looking at lib/csu/arm/crt1_s.S, this should be a string and therefore the
restriction to 4 characters seems wrong.
Found whle updating https://reviews.llvm.org/D74393.

Reviewed By:	emaste
Differential Revision: https://reviews.freebsd.org/D28470

(cherry picked from commit 2a39919364)
2021-03-17 09:51:39 +00:00
Ed Maste 207f428971 readelf: do not trucate section name with -W
PR:		246015
Reviewed by:	cem
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D28461

(cherry picked from commit 847dfd2803)
2021-02-18 14:31:28 -05:00
Ed Maste 5e9e7ad9a2 readelf: decode LA48 and ASG_DISABLE feature flags
Sponsored by:	The FreeBSD Foundation

(cherry picked from commit e79b51e2b2)
2021-02-09 21:52:47 -05:00
Ed Maste 67b94fc58e addr2line: fix allocation leak in error path
CID:		1437677
Reported by:	Coverity Scan
Sponsored by:	The FreeBSD Foundation
2020-12-03 14:41:11 +00:00
Ed Maste f4d6ed9a5c addr2line: rework check_range conditions
Simplify logic and reduce indentation for DW_AT_low_pc case.

Reviewed by:	Tiger Gao, markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D27426
2020-12-02 17:22:29 +00:00
Ed Maste a36179937e addr2line: add label checks when DW_AT_range and DW_AT_low_pc cannot be used
Check label's ranges for address we want to translate if a CU doesn't
have usable DW_AT_range or DW_AT_low_pc.

Use more appropriate names: "struct CU" -> "struct range"

Developed as part of upstream ELF Tool Chain bug report
https://sourceforge.net/p/elftoolchain/tickets/552/ although this does
not address the specific case reported there.

Submitted by:	Tiger Gao <tig@freebsdfoundation.org>
MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D23782
2020-11-27 21:38:03 +00:00
Ed Maste 65c207758a addr2line: swap if conditions for diff reduction in upcoming change
No functional change intended.
2020-11-19 21:10:36 +00:00
Dimitry Andric 1e4896b176 When elftoolchain's objcopy (or strip) is rewriting a file in-place,
make it create the temporary file in the same directory as the source
file by default, instead of always using $TMPDIR or /tmp. If creating
that file fails because the directory is not writable, also fallback to
$TMPDIR or /tmp.

This has also been submitted upstream as:
https://sourceforge.net/p/elftoolchain/tickets/597/

Reported by:	cem
PR:		250872
MFC after:	2 weeks
2020-11-18 18:40:58 +00:00
Dimitry Andric c2bffd0a97 Merge elftoolchain r3877 (by jkoshy):
Incorporate fixes from Dimitry Andric:

  - Use a BUFFER_GROW() macro to avoid rounding errors in capacity
    calculations.
  - Fix a bug introduced in [r3531].
  - Fix handling of nested template parameters.

  Ticket:	#581

This should fix a number of assertions on elftoolchain's cxxfilt, and
allow it to correctly demangle several names that it could not handle
before.

Obtained from:	https://sourceforge.net/p/elftoolchain/code/3877/
PR:		250702
MFC after:	3 days
2020-11-04 11:02:05 +00:00
Brandon Bergren d4fba56212 Fix 32-bit build after r367209
Fix build on systems with a 32-bit size_t.

Since it's being passed as a pointer, a 64-bit write to it will overflow.

MFC with:	r367209
2020-10-31 22:14:37 +00:00
Ed Maste e128bd0ff9 readelf: Add -z decompression support
Compatible with GNU readelf, -z decompresses sections displayed by
-x or -p.

ELF Tool Chain ticket #555
https://sourceforge.net/p/elftoolchain/tickets/555/

Submitted by:	Tiger Gao <tig@FreeBSDFoundation.org>
Reviewed by:	markj
MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
Differential Revision:    https://reviews.freebsd.org/D26909
2020-10-31 15:27:45 +00:00
Ed Maste 573456a931 libelf: add compression header support
GNU and Oracle libelf implementations added support for section
compression, intended to reduce the size of DWARF debug info (which
might be an order of magnitude larger than the code).

There are two compressed ELF section formats:

1. Old GNU - sections are renmaed to start with 'z'.  Section contains
   a magic number, uncompressed size, and compressed data.

2. Oracle and New GNU - compressed sections use the SHF_COMPRESSED flag.
   The compression header contains the compression type, uncompressed
   size, and uncompressed alignment.

The second style is preferred and this change implements only that one.

Submitted by:	Tiger Gao <tig@FreeBSDFoundation.org>
Reviewed by:	markj
MFC after:	2 weeks
Relnotes:	Yes
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D24566
2020-10-23 16:35:23 +00:00
Brandon Bergren d9052fccdf [PowerPC64LE] PPC64LE support for libelf.
Fix native detection when building on powerpc64le.

I will be submitting this and r361104 upstream shortly.

Sponsored by:	Tag1 Consulting, Inc.
2020-09-09 00:06:35 +00:00
Mark Johnston f690eff983 libdwarf: Hide SHT_NOBITS sections.
gnu_debuglink external debug files will contain an .eh_frame section of
type SHT_NOBITS.  libdwarf does not handle such sections (or rather, it
expects all debug sections to not have type SHT_NOBITS).  Avoid loading
SHT_NOBITS sections, to be consistent with SGI libdwarf's handling of
this case.

PR:		239516
Diagnosed by:	Paco Pascal <me@pacopascal.com>
Reviewed by:	emaste (previous version)
Event:		July 2020 Bugathon
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D25626
2020-07-20 18:22:38 +00:00
Ed Maste 796bf3133b readelf: add more DT_FLAGS_1 flags
Reference:
https://docs.oracle.com/cd/E36784_01/html/E36857/chapter6-42444.html

> DF_1_SINGLETON  Singleton symbols exist.
> DF_1_STUB       Object is a stub.
> DF_1_PIE        Object is a position-independent executable.

Sponsored by:	The FreeBSD Foundation
2020-05-31 15:31:47 +00:00
Justin Hibbits 4f8d098652 elftoolchain: Add powerpc64 definition to elftoolchain config
powerpc is already in place, but powerpc64 is needed separately.
2020-05-16 03:52:30 +00:00
Ed Maste e982f6f935 readelf: simplify Xen string note printing
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D24140
2020-03-21 00:08:33 +00:00
Mark Johnston c364ccf9ce libelftc: Fix a memory leak in cpp_demangle_read_type().
CID:		1305425
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-03-20 16:25:06 +00:00
Mark Johnston d5e7add6b5 readelf: Fix another instance of the leak mentioned in r357542.
CID:		1292493
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-03-20 16:24:53 +00:00
Mark Johnston c37c6b38b6 readelf: Avoid leaking ELF descriptors for archive entries.
CID:		1292494
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-03-20 16:24:40 +00:00
Mark Johnston 388586bcd8 libpe: Avoid a potential use-after-free in pe_update_symtab().
This function appears to be unused within FreeBSD and ELFToolChain.

CID:		1418982
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-03-20 16:24:23 +00:00
Mark Johnston a9d65c5fae libdwarf: Make an out-pointer assignment unconditional.
There is no reason for diep to ever be NULL, since in that case we would
simply be leaking memory.

CID:		1418801
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-03-20 16:24:06 +00:00
Ed Maste 05ab65497e objcopy: add new sections also when there is no .shstrtab
Previously objcopy (elfcopy) --add-sections inserted new sections before
.shstrtab, but omitted them if there was no .shstrtab.

Now, after processing existing sections add new sections if they were
not yet added.

PR:		241437
Reported by:	arrowd
Submitted by:	Tiger Gao <tig@FreeBSDFoundation.org>
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D23571
2020-03-20 15:50:37 +00:00
Ed Maste 6c37d6032e readelf: print GNU Build-ID
Sponsored by:	The FreeBSD Foundation
2020-03-06 17:24:51 +00:00
Ed Maste ff0f134bb1 readelf: decode and print Xen ELF note strings
Sponsored by:	The FreeBSD Foundation
2020-03-06 15:58:52 +00:00
Ed Maste 4d8a9faf17 readelf: add XEN_ELFNOTE_PHYS32_ENTRY note
See r336469 for details.

Sponsored by:	The FreeBSD Foundation
2020-03-06 15:26:15 +00:00
Ed Maste 2f7242ed33 libelf: rationalize error handling in ELF note conversion
Previously _libelf_cvt_NOTE_tom (to host) returned false if a note's
namesz + descsz exceeded the buffer size, while _libelf_cvt_NOTE_tof
(to file) silently truncated.  Return false in the latter case too.

Sponsored by:	The FreeBSD Foundation
2020-03-05 20:53:43 +00:00
Ed Maste 89839cad79 readelf: simplify namesz / descsz checks
MFC after:	3 days
Sponsored by:	The FreeBSD Foundation
2020-03-04 20:41:45 +00:00
Ed Maste 721ac29c0c readelf: check note namesz and descsz
Previously corrupt note namesz or descsz (perhaps caused by readelf's
current lack of endian support for notes) resulted in a crash.  Check
that namesz and descsz do not extend beyond the end of the buffer before
trying to access name and desc data.

Reported by:	jhb
MFC after:	3 days
Sponsored by:	The FreeBSD Foundation
2020-03-04 20:29:49 +00:00
Ed Maste d06e23f9d9 Reserve WXNEEDED ELF feature control flag
This will be used to tag binaries that require W+X mappings, in advance
of the ability to prevent W^X in mmap/mprotect.

There is still some discussion about the flag's name, but the ABI won't
change even if the name does (as kib pointed out in the review).

Reviewed by:	csjp, kib
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D23909
2020-03-04 18:21:30 +00:00
Ed Maste 8e7e7da5ec readelf: add PROTMAX_DISABLE and STKGAP_DISABLE
From r349609 (PROTMAX_DISABLE) and r354790 (STKGAP_DISABLE).  Commited
upstream (in a slightly different form) as r3831.

Sponsored by:	The FreeBSD Foundation
2020-03-01 20:25:57 +00:00
Ed Maste fe91dd0138 addr2line: use stdbool.h header for bool
Presumably a bool definition is obtained via header contamination on
FreeBSD-CURRENT.  Found while trying to upstream FreeBSD addr2line
changes - the FreeBSD 11.2 CI build failed there.

Reported by:	Cirrus-CI, upstream ELF Tool Chain
MFC with:	r357844
Sponsored by:	The FreeBSD Foundation
2020-02-13 16:17:05 +00:00
Ed Maste 6c4a4f1bc2 addr2line: Handle DW_AT_ranges in compile units
Based on original submission by Marat Radchenko in ELF Tool Chain
ticket #545, rebased and updated by Tiger Gao.

PR:		217736
Submitted by:	Marat Radchenko <marat@slonopotamus.org>
Submitted by:	Tiger Gao <tig@freebsdfoundation.org>
Reviewed by:	markj
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D23501
2020-02-12 21:52:10 +00:00