Commit Graph

244 Commits

Author SHA1 Message Date
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
Ed Maste
57d7e4cc2c elfcopy: set ELF OS/ABI field when converting from binary
PR:		228934
Submitted by:	Tiger Gao <tig@freebsdfoundation.org>
Reviewed by:	markj, jkoshy
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D23504
2020-02-12 15:59:59 +00:00
Mark Johnston
85642eee46 readelf: Don't leak memory when dwarf_get_fde_info_for_all_regs() fails.
CID:		1292493
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-02-04 21:18:56 +00:00
Mark Johnston
37fa1df2d1 readelf: Fix the check for an error from realloc().
Use err() instead of errx() while here, since realloc() sets errno.

CID:		1401326
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-02-04 21:18:37 +00:00
Mark Johnston
dfa169049b readelf: Fix a double close of the input file.
The caller of dump_object() is responsible for opening the file, let it
be responsible for closing too.

CID:		1411588
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-02-04 21:18:16 +00:00
Mark Johnston
b7fc41b3ca size: Avoid returning a stack pointer from xlatetom().
The callers only check whether the returned pointer is non-NULL, so this
was harmless in practice, but change the return value to guard against
the issue.

CID:		1411597
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-02-04 21:17:59 +00:00
Mark Johnston
640ff6ed84 elfcopy: Avoid leaking dst's fd when we fail to copy a file.
We should really create the output file in the same directory as the
destination file so that rename() works.  This will be done in a future
change as part of some work to run in capability mode.

CID:		1262523
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-02-04 21:16:56 +00:00
Mark Johnston
f2530c80db elfcopy: Handle multiple data descriptors properly.
The code clearly meant to resize the buffer in the case where a section
was backed by multiple data descriptors.

In practice this shouldn't have been a problem since libelf would return
a single data descriptor for each section in a newly opened file.

CID:		1262522
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-02-04 21:16:41 +00:00
Mark Johnston
334f09a65b libelftc: Add a missing check for an error from vector_str_init().
While here consistently use the same spelling for such checks.

CID:		1376769
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-02-04 21:16:16 +00:00
Mark Johnston
a3c35da61b libelftc: Fix memory leaks in the C++ demanglers.
CID:		1262518, 1262519, 1262520, 1262529
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-02-04 21:15:52 +00:00
Mark Johnston
ab3b51df28 libdwarf: Fix a memory leak in _dwarf_frame_section_init().
If frame length validation failed we would leak memory.

CID:		1193366
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-02-04 21:15:29 +00:00
Mark Johnston
513e76aaf8 libdwarf: Make an out-pointer of _dwarf_abbrev_add() mandatory.
All callers pass a non-NULL pointer, and otherwise it was possible to
leak memory if the abbrev was not added to a CU.

CID:		1193365
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-02-04 21:15:11 +00:00
Mark Johnston
e5551befe8 libdwarf: Fix a possible memory leak in dwarf_add_AT_location_expr().
CID:		1193364
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-02-04 21:14:53 +00:00
Mark Johnston
75bd29add2 libdwarf: Remove unnecessary NULL checks.
All callers of _dwarf_add_expr() and _dwarf_expr_into_block() pass a
non-NULL expr pointer, and these functions assume that expr is non-NULL
anyway.

CID:		1193305, 1193306
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2020-02-04 21:14:34 +00:00
Mark Johnston
35beedae40 addr2line: Avoid a name collision.
The RB_ macros define functions with a parameter named head, and gcc
warns about this.

MFC with:	r357450
2020-02-03 19:08:35 +00:00
Mark Johnston
af84329100 addr2line: Cache CU DIEs upon a successful address lookup.
Previously, addr2line would sequentially search all CUs for each input
address.  For some uses, notably syzkaller's code coverage map generator,
this was extremely slow.  Add a CU cache into which entries are added
following a successful lookup, and search the cache before falling back
to a scan.  When translating a large number of addresses this yields
slightly better performance than GNU addr2line.

Garbage-collect an unused hash table which appears to have been intended
for the same purpose.  A hash table doesn't seem particularly suitable
since each CU spans a range of addresses.

Submitted by:	Tiger Gao <tig@freebsdfoundation.org>
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D23418
2020-02-03 16:41:40 +00:00
Ed Maste
d003e0d7fe Update ELF Tool Chain to upstream r3769
This contains many small bugfixes and documentation improvements.

Sponsored by:	The FreeBSD Foundation
2019-12-05 13:20:15 +00:00
Justin Hibbits
12f7c1e8de ELF toolchain: Add PowerPC VMX and VSX note decode to readelf
Summary: A follow-on to r276634, which added the VMX note to userland cores,
and r334538 for VSX notes.

Copied from note_type_linux_core().

Reviewed by:	emaste
Differential Revision:	https://reviews.freebsd.org/D22404
2019-11-19 02:11:00 +00:00
Ed Maste
4133f23624 elfcopy/strip: Ensure sections have required alignment on output
Object files may specify insufficient alignment on certain sections, for
example due to a bug in NASM[1].  When we detect that case in elfcopy or
strip, emit a warning and increase the alignment to the minimum
required.

The NASM bug was fixed in 2015[2], but we might as well have this fixup
(and warning) in elfcopy in case we encounter such a file for any other
reason.

This might be reworked somewhat upstream - see ELF Tool Chain
ticket 485[3].

[1] https://bugzilla.nasm.us/show_bug.cgi?id=3392307
[2] 1f0cb0f2c1
[3] https://sourceforge.net/p/elftoolchain/tickets/485/

PR:		198611
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D2292
2019-11-08 14:59:41 +00:00
Mark Johnston
052ad61b7e Capsicumize nm(1).
Reviewed by:	emaste
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D21107
2019-09-30 17:27:59 +00:00
Mark Johnston
8d3d724796 nm: Adjust argc and argv in get_opt().
This refactoring makes it slightly easier to capsicumize nm(1).

Discussed with:	emaste
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2019-09-30 17:26:42 +00:00
Mark Johnston
65bc027257 Capsicumize c++filt(1).
Reviewed by:	emaste
Discussed with:	oshogbo
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D21106
2019-09-30 16:10:42 +00:00
Aleksandr Rybalko
164e7901a0 Initialize baseaddr to suppres warning.
Pointy hat to:	ray
2019-09-29 23:47:23 +00:00
Aleksandr Rybalko
0705bb9de5 ections into expected offset in binary format.
Calculate binary file offset using address field, bacause software know only offset to known data, not where to load segment.
With that patch, kernel .data section can have any alignment/offset - kernel boor fine.

PR:		235391
Reviewed by:	markj
MFC after:	1 month
Differential Revision:	D21827
2019-09-29 22:34:01 +00:00
Mark Johnston
bbce101753 Merge r3780 from elftoolchain.
Adjust argc and argv by optind before using them.  This slightly
simplifies the code.  No functional change intended.

MFC after:	3 days
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D21079
2019-08-07 16:22:25 +00:00
Mark Johnston
1966967151 readelf: Close input files when done with them.
The low fd limit used by poudriere exposed an odd failure mode in
cap_fileargs (used by readelf as of r350516).  In particular, when
the limit was hit, both the main process and casper service would
block on their shared socket, waiting forever for the other to send a
message.

Reported by:	zeising
MFC after:	3 days
Sponsored by:	The FreeBSD Foundation
2019-08-07 03:14:45 +00:00
Mark Johnston
1033464925 Capsicumize size(1).
Reviewed by:	oshogbo
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D21105
2019-08-01 18:57:37 +00:00
Mark Johnston
802c2095b5 Capsicumize readelf(1).
Reviewed by:	oshogbo
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D21108
2019-08-01 18:57:08 +00:00
Mark Johnston
1d954fed61 Capsicumize addr2line(1).
Reviewed by:	oshogbo
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D21104
2019-08-01 18:56:32 +00:00