Commit graph

45 commits

Author SHA1 Message Date
Jessica Clarke 839fdcfc0c 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.

Reviewed by:	dim
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D29352
2021-03-20 17:58:10 +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
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 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 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
Mark Johnston 8c953901bf libelf: Use a red-black tree to manage the section list.
The tree is indexed by section number.  This speeds up elf_getscn()
and its callers, which previously had to traverse a linked list. In
particular, since .shstrtab is often the last section in a file,
elf_strptr() would have to traverse the entire list.

PR:		234949
Reviewed by:	emaste
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D20443
2019-06-04 18:26:29 +00:00
John Baldwin 2e43efd0bb Drop "All rights reserved" from my copyright statements.
Reviewed by:	rgrimes
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D19485
2019-03-06 22:11:45 +00:00
Ed Maste ae500c1ff8 Update to ELF Tool Chain r3668
Highlights:
- Make sure that only TLS sections are sorted into TLS segment.
- Fixed multiple errors in "Section to Segment mapping".
- Man page updates
- ar improvements
- elfcopy: avoid filter_reloc uninitialized variable for rela
- elfcopy: avoid stripping relocations from static binaries
- readelf: avoid printing directory in front of absolute path
- readelf: add NT_FREEBSD_FEATURE_CTL FreeBSD note type
- test improvements

NOTES:

Some of these changes originated in FreeBSD and simply reduce diffs
between contrib and vendor.

ELF Tool Chain ar is not (currently) used in FreeBSD, and there are
improvements in both FreeBSD and ELF Tool Chain ar that are not in
the other.

Sponsored by:	The FreeBSD Foundation
2019-01-10 14:35:23 +00:00
Ed Maste 447a8c25fe libelf: also test for 64-bit ELF in _libelf_is_mips64el
Although _libelf_is_mips64el is only called in contexts where we've
already checked that e_class is ELFCLASS64 but this may change in the
future.  Add a safety belt so that we don't access an invalid e_ehdr64
union member if it does.

Reported by:	jkoshy (in review D17380)
2018-10-20 18:47:45 +00:00
Ed Maste 1c45d770a7 libelf: correct mips64el test to use ELF header
libelf maintains two views of endianness: e_byteorder, and
e_ident[EI_DATA] in the ELF header itself.  e_byteorder is not always
kept in sync, so use the ELF header endianness to test for mips64el.

PR:		231790
Bisected by:	sbruno
Reviewed by:	jhb
Approved by:	re (kib)
MFC with:	r338478
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D17380
2018-10-02 15:08:41 +00:00
John Baldwin eb81f38a62 Fix objcopy for little-endian MIPS64 objects.
MIPS64 does not store the 'r_info' field of a relocation table entry as
a 64-bit value consisting of a 32-bit symbol index in the high 32 bits
and a 32-bit type in the low 32 bits as on other architectures.  Instead,
the 64-bit 'r_info' field is really a 32-bit symbol index followed by four
individual byte type fields.  For big-endian MIPS64, treating this as a
64-bit integer happens to be compatible with the layout expected by other
architectures (symbol index in upper 32-bits of resulting "native" 64-bit
integer).  However, for little-endian MIPS64 the parsed 64-bit integer
contains the symbol index in the low 32 bits and the 4 individual byte
type fields in the upper 32-bits (but as if the upper 32-bits were
byte-swapped).

To cope, add two helper routines in gelf_getrel.c to translate between the
correct native 'r_info' value and the value obtained after the normal
byte-swap translation.  Use these routines in gelf_getrel(), gelf_getrela(),
gelf_update_rel(), and gelf_update_rela().  This fixes 'readelf -r' on
little-endian MIPS64 objects which was previously decoding incorrect
relocations as well as 'objcopy: invalid symbox index' warnings from
objcopy when extracting debug symbols from kernel modules.

Even with this fixed, objcopy was still crashing when trying to extract
debug symbols from little-endian MIPS64 modules.  The workaround in
gelf_*rel*() depends on the current ELF object having a valid ELF header
so that the 'e_machine' field can be compared against EM_MIPS.  objcopy
was parsing the relocation entries to possibly rewrite the 'r_info' fields
in the update_relocs() function before writing the initial ELF header to
the destination object file.  Move the initial write of the ELF header
earlier before copy_contents() so that update_relocs() uses the correct
symbol index values.

Note that this change should really go upstream.  The binutils readelf
source has a similar hack for MIPS64EL though I implemented this version
from scratch using the MIPS64 ABI PDF as a reference.

Discussed with:	jkoshy
Reviewed by:	emaste, imp
Approved by:	re (gjb, kib)
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D15734
2018-09-05 20:51:53 +00:00
Ed Maste bd6313b66e libelf: reload section headers after update with ELF_C_WRITE
These issues (PR 218860 and PR 218861) are still undergoing additional
discussion in the upstream context and there may be additional changes
to come.

PR:		218860
Submitted by:	Eric McCorkle
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D10486
2018-08-01 15:55:14 +00:00
Ed Maste 715d1396d6 Update ELF Tool Chain to r3614
MFC after:	1 week
Relnotes:	Yes
Sponsored by:	The FreeBSD Foundation
2018-04-27 13:59:24 +00:00
Ruslan Bukin ca20f8ec29 o Replace __riscv__ with __riscv
o Replace __riscv64 with (__riscv && __riscv_xlen == 64)

This is required to support new GCC 7.1 compiler.
This is compatible with current GCC 6.1 compiler.

RISC-V is extensible ISA and the idea here is to have built-in define
per each extension, so together with __riscv we will have some subset
of these as well (depending on -march string passed to compiler):

__riscv_compressed
__riscv_atomic
__riscv_mul
__riscv_div
__riscv_muldiv
__riscv_fdiv
__riscv_fsqrt
__riscv_float_abi_soft
__riscv_float_abi_single
__riscv_float_abi_double
__riscv_cmodel_medlow
__riscv_cmodel_medany
__riscv_cmodel_pic
__riscv_xlen

Reviewed by:	ngie
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D11901
2017-08-07 14:09:57 +00:00
Ed Maste bee2765cc1 Update ELF Tool Chain to upstream r3520
Highlights of changes between r3490 and r3520:

- Improve C++ demangling
- Improve compatibility with Binutils tools wrt. error messages
- Handle additional types/sections/etc. in readelf and elfdump
- addr2line, cxxfilt: use setvbuf to set line buffering for filter use

PR:		218395
MFC after:	2 weeks
Relnotes:	Yes
Sponsored by:	The FreeBSD Foundation
2017-04-17 23:56:48 +00:00
Ed Maste f2911fa4a3 libelf: add an assert that msz is non-zero
Reported by:	Coverity
CID:		976023
2017-04-10 19:22:15 +00:00
Conrad Meyer 93326017f6 gelf_getphdr: Allow extended indices
Needed for 'readelf -l' of extended phnum files.  (Parity with GNU
binutils.)

Reviewed by:	no one, unfortunately
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D8703
2016-12-16 01:42:51 +00:00
Conrad Meyer 1d1bfbbb38 libelf: Fix extended numbering detection
Extended numbering is used for any of these fields overflowing.

Reviewed by:	emaste@
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D8701
2016-12-16 01:39:06 +00:00
Ed Maste b6d812d2dd Update to ELF Tool Chain r3490
Improvements include:
 * readelf: report all relocation types in rel/rela for MIPS N64
 * readelf: add ELFOSABI_ARM_AEABI
 * elfdump: add ELFOSABI_ARM_AEABI and ELFOSABI_ARM
 * Add recent RISC-V relocations
 * elfcopy: use elftc_timestamp, to support SOURCE_DATE_EPOCH

Sponsored by:	The FreeBSD Foundation
2016-08-31 15:05:04 +00:00
Ed Maste b6b6f9cc7c Update to ELF Tool Chain r3475
Improvements include:

 * Add support for reporting and handling a number of new constants in
   various tools, including:
    * CloudABI OSABI
    * DT_TLSDESC_*
    * i386, MIPS, SPARC and amd64 relocations

 * C++ demangler bug fixes

 * Man page updates

 * Improved input validation in several tools

This update also reduces diffs against upstream as a number of fixes
included in upstream were previously cherry-picked into FreeBSD.

Sponsored by:	The FreeBSD Foundation
2016-05-20 17:24:34 +00:00
Ed Maste a87342e849 libelf: correct byte count in cross-endian note translation
MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
2016-03-11 22:37:12 +00:00
Ed Maste 839529caa9 Update ELF Tool Chain to upstream rev 3400
Some notable improvements include:

readelf:
- Add AArch64 relocation definitions.
- Report value of unknown relocation types.

elfcopy:
- Consider symbols with STB_GNU_UNIQUE binding as global symbols.
- Fixed support for VMA adjustment for loadable sections found
  in relocatable objects.
- Handle nameless global symbols.
- Improve wildcard matching for !-prefixed symbols.
- Add PE/COFF support.

elfdump:
- Improve section type reporting.
- Add MIPS-specific section types.

This update also includes a significant number of bug fixes.

PR:		207091 [exp-run]
Sponsored by:	The FreeBSD Foundation
2016-02-12 20:54:02 +00:00
Ruslan Bukin 4f0a4502a1 Add config for RISC-V ISA.
Reviewed by:	emaste
Sponsored by:	DARPA, AFRL
Sponsored by:	HEIF5
Differential Revision:	https://reviews.freebsd.org/D5046
2016-01-24 15:12:49 +00:00
Ed Maste 95fd7f2615 Update to ELF Tool Chain r3272
Highlights (not already in the FreeBSD tree):
 - addr2line: Speed up and support searching inlined functions
 - addr2line: Support -i, -a, -p options
 - readelf: Add some ARM relocation types
 - readelf, libelf: Avoid reading beyond end of buffer/file

Relnotes:	Yes
Sponsored by:	The FreeBSD Foundation
2015-12-11 20:28:27 +00:00
Ed Maste b00fe64f4a Update to ELF Tool Chain r3197
Highlights:
 - Fix man page markup, whitespace, and typos
 - Fix sh_info of SHT_GROUP section to point to the correct string
 - Improve validation in readelf and elfcopy/strip
 - Handle DWARF 4's DW_AT_high_pc in addr2line

Sponsored by:	The FreeBSD Foundation
2015-05-14 19:48:15 +00:00
Ed Maste 67d97fe724 Update elftoolchain to upstream revision 3179
Some notable changes:
- libdwarf: Fixed DWARF4 line section
- elfcopy: Implement --localize-hidden
- nm: handle object name referenced by DW_AT_specification
- elfcopy: Add --strip-dwo and --extract-dwo options for split DWARF
- readelf: add remaining arm64 dynamic relocation names
- nm: Avoid integer overflow in value comparison

Relnotes:	Yes
Sponsored by:	The FreeBSD Foundation
2015-04-01 01:08:01 +00:00
Ed Maste 71a0c925ce Update elftoolchain to upstream revision 3163
Most of our changes have now been committed upstream, so this change is
largely bookkeeping.

Sponsored by:	The FreeBSD Foundation
2015-02-17 15:19:58 +00:00
Ed Maste 8ae6c16d82 Verify that section header offset is not past EOF
MFC After:	1 week
Sponsored by:	The FreeBSD Foundation
2015-01-16 15:16:19 +00:00
Ed Maste a737d64c08 libelf: Add arm64 config
Upstream elftoolchain ticket: #470
Submitted by:	Andrew Turner
Sponsored by:	The FreeBSD Foundation
2015-01-07 19:34:44 +00:00
Ed Maste bc5438c566 libelf: Return an error instead of asserting on an invalid ar file
Upstream elftoolchain ticket 467
http://sourceforge.net/p/elftoolchain/tickets/467/

Reported by:	Alexander Cherepanov
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
2015-01-02 02:47:47 +00:00
Ed Maste 47fc54d8b0 libelf: Rearrange size test to prevent integer overflow
Sponsored by:	The FreeBSD Foundation
2014-12-31 01:48:23 +00:00
Ed Maste 6db8a9f3a5 Check for multiplication integer overflow in CHECK_EHDR
The initial fix in r276374 is valid only for 64-bit objects. Revert it
and return an error in CHECK_EHDR if the multiplication would overflow.

The original buffer overflow issue was found with the security/afl
fuzzer and has upstream elftoolchain ticket 462. The 32-bit object issue
with r276374 found by antoine@ during an i386 exp-run.

Sponsored by:	The FreeBSD Foundation
2014-12-30 22:04:24 +00:00
Ed Maste 3a6f20c19a libelf: Do not read past end of buffer
Previously a corrupt ELF file could read beyond the end of e_rawfile.

Upstream elftoolchain ticket 462.  Found via the security/afl fuzzer.

Sponsored by:	The FreeBSD Foundation
2014-12-29 20:23:42 +00:00
Ed Maste 4a9f54ac69 Restore r276252 mdoc fix
It was accidentally reverted in the elftoolchain update (r276371).

Sponsored by:	The FreeBSD Foundation
2014-12-29 19:27:10 +00:00
Ed Maste b4e9f2392c Update elftoolchain to upstream rev 3130
This includes a number of libdwarf improvements (particularly DWARF4
related) and updates to elftoolchain tools such as strip(1). It also
includes a large number of miscellaneous fixes (memory leaks, sign and
cast warnings, integer overflow and underflow, etc.).

This is a merge of r276167,276170-276172 from the
projects/elftoolchain-update-r3130 branch.

Sponsored by:	The FreeBSD Foundation
2014-12-29 19:16:40 +00:00
Baptiste Daroussin e66fe60d56 mdoc fixes 2014-12-26 20:50:40 +00:00
Ed Maste cf781b2e16 Update elftoolchain to upstream rev 3130
This brings a number of fixes to elfcopy/strip and DWARF4 improvements.

Sponsored by:	The FreeBSD Foundation
2014-12-24 03:13:16 +00:00
Ed Maste ccbdcd03f2 libelf: Fix cross-endian ELF note file / memory conversion
The namesz and descsz variables need to be used in native endianness.
The sizes are in native order after swapping in the file to memory case,
and before swapping in the memory to file case.

This issue was identified for r273443, but the change was applied to the
wrong case. Revert r273443 to fix the to-memory case, and apply the
equivalent change to the to-file case.

Sponsored by:	DARPA, AFRL
Reviewed by:	adrian, brooks, marcel
Differential Revision: https://reviews.freebsd.org/D1257
2014-12-02 22:35:43 +00:00
Marcel Moolenaar c10185837c Fix the conversion macro for .note sections, broken in the case
the ELF file's byte order is not the native byte order.  The
bug is that the variables holding the name and description size
are used (natively) after having been byte-swapped.  The fix is
to calculate sz from them just prior to byte-swapping.

Approved by:	jkoshy@
Obtained from:	Juniper Networks, Inc.
2014-10-22 01:04:16 +00:00
Marcel Moolenaar 5da7d44acb GCC for PowerPC does not align .note sections to 4 bytes. When
running ctfmerge on its objects, libelf asserts as it expects
.note sections to be 4-byte aligned.  Change that expectation.

Approved by:	jkoshy@
Obtained from:	Juniper Networks, Inc.
2014-10-22 00:58:50 +00:00
Kai Wang ee3d625d72 Reapply r221569, r233401, r233524 and r255105: Add support for a few
ARM/MIPS ELF section types in _libelf_xlate_shtype().
2014-01-25 13:53:46 +00:00
Kai Wang 35b270102d Apply r241720 by ed:
Fix warnings found by -Wmising-variable-declarations.
2014-01-18 10:13:04 +00:00
Kai Wang 4e5c697507 Use FreeBSD's ELF headers instead of the elfdefinitions.h header which
comes with elftoolchain. This version of libelf doesn't need to be
portable; using FreeBSD's own ELF headers will avoid conflicts and
make integration easier.
2014-01-16 21:47:27 +00:00
Kai Wang 2de3b87a12 Copy libelf, libdwarf and common files from vendor/ to contrib/. 2014-01-15 22:30:48 +00:00