Commit Graph

149 Commits

Author SHA1 Message Date
Mateusz Guzik
248b5d08b3 strings: unbreak the build after r340746
Discussed with:	oshogbo
Sponsored by:	The FreeBSD Foundation
2018-11-21 22:37:49 +00:00
Mariusz Zaborski
4f486ad6f6 strings: fix style nits
Reviewed by:	cem, emaste, Joseph Koshy <jkoshy@users.sourceforge.net>
Differential Revision:	https://reviews.freebsd.org/D18036
2018-11-21 21:48:02 +00:00
Maxim Sobolev
65caaa0ed6 Fix CU: output of the --debug-dump=decodedline, the problem there
is that both file name and current directory is recorded, however
file name sometimes already contains absolute path. In which case
prefixing it with directory name results in an invalid pathname.

Only append directory name if the file name does not start with '/'.
This seems to DTRT.

Approved by:	emaste
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D18290
2018-11-21 21:46:06 +00:00
Ed Maste
7ee126159f Revert r340385, strings capability mode
This needs to be reworked for bootstrapping.
2018-11-13 01:30:31 +00:00
Ed Maste
672b07ce30 strings: enter capability mode when operating on stdin
Reviewed by:	oshogbo
MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
2018-11-12 20:38:43 +00:00
Ed Maste
e74f411d47 Define NT_FREEBSD_FEATURE_CTL ELF note type
This ELF note will be used to allow binaries to opt out of, or in to,
upcoming vulnerability mitigation and other features.

Committing the definition and readelf change separately to allow
independent MFC.

MFC after:	3 days
Sponsored by:	The FreeBSD Foundation
2018-11-02 19:02:03 +00:00
Ed Maste
e37480d938 readelf: decode R_MIPS_HIGHER and R_MIPS_HIGHEST relocation types
Sponsored by:	The FreeBSD Foundation
2018-11-02 18:40:01 +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
aee4c74cdd objcopy: restore behaviour required by GCC's build
In r339350 filter_reloc() was removed, to fix the case of stripping
statically linked binaries with relocations (which may come from ifunc
use, for example).  As a side effect this changed the behaviour when
stripping object files - the output was broken both before and after
r339350, in different ways.  Unfortunately GCC's build process relies
on the previous behaviour, so:

- Revert r339350, restoring filter_reloc().
- Fix an unitialized variable use (commited as r3638 in ELF Tool Chain).
- Change filter_reloc() to omit relocations referencing removed
  symbols, while retaining relocations with no symbol reference.
- Retain the entire relocation section if it references the dynamic
  symbol table (fix from kaiw in D17596).

PR:		232176
Reported by:	antoine
Reviewed by:	kaiw
MFC with:	r339350
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D17596
2018-10-20 17:27:53 +00:00
Ed Maste
c9504239e8 elfcopy: delete filter_reloc, it is broken and unnecessary
elfcopy contained logic to filter individual relocations in STRIP_ALL
mode.  However, this is not valid; relocations emitted by the linker are
required, unless they apply to an entire section being removed (which is
handled by other logic in elfcopy).

Note that filter_reloc was also buggy: for RELA relocation sections it
operated on uninitialized rel.r_info resulting in invalid operation.

The logic most likely needs to be inverted: instead of removing
relocations because their associated symbols are being removed, we must
keep symbols referenced by relocations.  That said, in practice we do
not encounter this code path today: objects being stripped are either
dynamically linked binaries which retain .dynsym, or static binaries
with no relocations.

Just remove filter_reloc.  This fixes certain cases including statically
linked binaries containing ifuncs.  Stripping binaries with relocations
referencing removed symbols was already broken, and after this change
may still be broken in a different way.

PR:		232176
Reviewed by:	kaiw, kib, markj
Approved by:	re (rgrimes)
MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D17519
2018-10-13 21:26:07 +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
ce0c6340ec readelf: display NT_GNU_PROPERTY_TYPE_0 note name
NT_GNU_PROPERTY_TYPE_0 in a .note.gnu.property section "contains a
program property note which describes special handling requirements
for linker and run-time loader." (from the System V Application Binary
Interface - Linux Extensions")

Intel CET uses two processor-specific program properties in
NT_GNU_PROPERTY_TYPE_0: GNU_PROPERTY_X86_FEATURE_1_IBT to indicate that
all executable sections are compatible with Indirect Branch Tracking,
and GNU_PROPERTY_X86_FEATURE_1_SHSTK to indicate that sections are
compatible with shadow stack.

A later change should add decoding of the individual properties.

MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2018-08-10 10:37:25 +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
Mark Johnston
b027d6545b Pass the right sizes to malloc() and realloc().
Reported by:	scan-build, via Mark Millard
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D16180
2018-07-12 18:48:53 +00:00
Mark Johnston
4a0f8b339e Add DW_LANG_* definitions from DWARF 4 and 5.
Reviewed by:	emaste
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D15712
2018-06-09 14:50:38 +00:00
Phil Shafer
48d41ef0fb Handle thread-local storage (TLS) segments correctly when
copying (objcopy) and displaying (readelf) them.

PR:		227552
Submitted by:	kaiw (maintainer)
Reported by:	jachmann@unitix.org
Reviewed by:	phil
MFC after:	1 day
2018-05-14 05:21:18 +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
Conrad Meyer
48fc14c0aa elftoolchain nm(1): Initialize allocated memory before use
In out of memory scenarios (where one of these allocations failed but
other(s) did not), nm(1) could reference the uninitialized value of these
allocations (undefined behavior).

Always initialize any successful allocations as the most expedient
resolution of the issue.  However, I would encourage upstream elftoolchain
contributors to clean up the error path to just abort immediately, rather
than proceeding sloppily when one allocation fails.

Reported by:	Coverity
Sponsored by:	Dell EMC Isilon
2018-03-16 22:25:33 +00:00
Ed Maste
aadb68849f elfcopy: copy raw (untranslated) contents to binary output
Previously elfcopy used elf_getdata to obtain data from ELF sections
being copied to binary output, but elf_getdata returns data that has
been translated - that is, data is in host byte order. When the host and
target differ in endianness (e.g., converting a big-endian MIPS ELF
object to binary on an x86 host) this resulted in byte-swapped data in
certain sections such as .dynamic.

Instead use elf_rawdata to keep data in the original, target endianness.

Reported by:	Hiroki Mori <yamori83@yahoo.co.jp>, Bill Yuan
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2018-01-02 14:07:55 +00:00
Ed Maste
7003cfb386 readelf: report byte size for DT_PREINIT_ARRAYSZ
Sponsored by:	The FreeBSD Foundation
2017-12-26 18:10:34 +00:00
John Baldwin
9fb35c8d7b Add missing newline after unknown MIPS-specific dynamic entries.
Reviewed by:	emaste
Sponsored by:	DARPA / AFRL
Differential Revision:	https://reviews.freebsd.org/D12384
2017-09-15 22:56:39 +00:00
John Baldwin
369bd05bc6 Recognize NT_PTLWPINFO and NT_ARM_VFP in FreeBSD ELF cores.
Reviewed by:	emaste
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D12371
2017-09-14 16:41: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
7582669623 readelf: correct printing of DT_FILTER and DT_AUXILIARY values
Previously these were shown only for MIPS objects.

Obtained from:	ELF Tool Chain r3564
MFC after:	1 week
MFC with:	r321045
Sponsored by:	The FreeBSD Foundation
2017-07-16 16:22:52 +00:00
Ed Maste
d62ecb4ecd readelf: fix printing of DT_FILTER and some other DT_* values
Some non-processor-specific DT_* values overlap the range DT_LOPROC to
DT_HIPROC.  Handle common ones first, then the processor-specific ones.

Obtained from:	ELF Tool Chain r3563
Sponsored by:	The FreeBSD Foundation
2017-07-16 12:30:39 +00:00
Ed Maste
18f4c9db68 Update to ELF Tool Chain snapshot at r3561
This update is primarily bug fixes in C++ symbol demangling, including:

- rvalue reference
- builtin type auto and decltype(auto)
- revamped support for function return types
- formatting fixes
- omit void when its the only param
- ref-qualifiers and others in function types
- type qualifiers in pointer-to-member function types
- incorrect handling regarding CV-qualifiers in function types
- ref-qualifier found in nested-name
- properly handle <name> ::= <substitute><template-args>
- make sure that nested function name is not a substitute candidate
- correctly handle expression in template args
- skip unknown substitution abbreviations

MFC after:	4 days
2017-06-25 22:39:28 +00:00
Ed Maste
9257ada0a4 nm: document 'r' symbol type
PR:		219245
MFC after:	3 days
Sponsored by:	The FreeBSD Foundation
2017-05-22 01:10:12 +00:00
Ed Maste
42a4fda284 revert r308465: c++filt: flush output after newline
The ELF Tool Chain update to r3520 uses setvbuf to set line buffering.

Sponsored by:	The FreeBSD Foundation
2017-05-01 01:56:11 +00:00
Ed Maste
fa903e5725 elfcopy: allow empty symbol list files
Reported by:	bz
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D10441
2017-04-24 14:51:53 +00:00
Ed Maste
431bcfcdbd elfcopy: document --strip-symbols=filename in the man page
Reported by:	bz
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2017-04-20 13:57:53 +00:00
Ed Maste
e9f3f88adf readelf: dump MIPS abiflags section
This is not fully fleshed out but is sufficient to dump the
SHT_MIPS_ABIFLAGS used in FreeBSD.

Reviewed by:	imp
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D8241
2017-04-19 14:43:51 +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
Ed Maste
9bfb310cf3 elfcopy: remove temporary ELF file when converting from binary
Previously a command like

  objcopy --input-target binary --output-target elf64-x86-64-freebsd \
    binary_file object.o

would leave a temporary file behind.

ELF Tool Chain ticket #543

Reported by:	Roger Marquis
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
2017-03-30 17:37:12 +00:00
Justin Hibbits
9569e13dce Add elf*-powerpc-freebsd targets to the elftoolchain target list
FreeBSD uses the full target triple when generating embedded rootfs images
(MFS_IMAGE= make option).  Without this change objcopy errors out with:

objcopy: elf64-poewrpc-freebsd: invalid target name

MFC after:	2 weeks
2017-02-08 03:21:29 +00:00
Ed Maste
080b710889 strings: avoid unnecessary trip through handle_file for stdin
Sponsored by:	The FreeBSD Foundation
2017-01-25 20:59:06 +00:00
Ed Maste
c83bcfbd2e readelf: add S390 relocation types
From https://refspecs.linuxfoundation.org/ELF/zSeries/lzsabi0_zSeries.html

Reviewed by:	bz
MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D9149
2017-01-11 22:54:04 +00:00
Ed Maste
0fbbe5bf87 readelf: add more PPC64 relocation types found in LLVM
MFC after:	2 weeks
MFC with:	r311941
Sponsored by:	The FreeBSD Foundation
2017-01-11 21:28:22 +00:00
Ed Maste
dd9d62b416 readelf: add PPC64 relocation types
Reported by:	Mark Millard
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D9146
2017-01-11 21:18:14 +00:00
Mark Johnston
2ef476c9a2 Follow DW_AT_specification when looking up DW_AT_type attributes.
dwarf_attrval_*() will search the parent DIE referenced by a
DW_AT_abstract_origin attribute for the value of the DW_AT_type attribute.
Do the same thing for the DW_AT_specification attributes in variable
definitions emitted by GCC 6.2, and ensure that we return an error rather
than crashing if neither DW_AT_abstract_origin or DW_AT_specification is
found when looking for the value of the DW_AT_type attribute.

PR:		215350, 215395
Reviewed by:	emaste
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D8920
2016-12-28 21:54:33 +00:00
Ed Maste
d0aa56451e elfcopy: fix PE object section name corruption and crash
Fixed a bug that the PE object section names are generated incorrectly
using the section name table found in the original input ELF object
instead of the intermediate ELF object.

Ticket:		#541

Do not try to copy section content from a NULL d_buf when creating
uninitialized data COFF section for PE object.

Ticket:		#540

Obtained from:	ELF Tool Chain r3507, r3508
MFC after:	1 week
2016-12-27 17:31:07 +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
3ca2845d13 libelftc: add elf{32,64}-tradlittlemips target emulation names
Sponsored by:	The FreeBSD Foundation
2016-12-10 01:40:10 +00:00
Conrad Meyer
7dc45d65e3 readelf(1) -S: Include zero index and match binutils' no-name
Include the SHN_UNDEF (zero) index special section in extended-attribute
ELF files, like GNU binutils' readelf.

Additionally, print "<no-name>" for sections without names, like GNU
binutils.

Reviewed by:	kaiw@
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D8707
2016-12-07 18:33:40 +00:00
Conrad Meyer
714935be5a readelf(1): Add support for extended program header numbers
Add support for extended program header numbers to elftoolchain
'readelf -h'.

Reviewed by:	emaste
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D8702
2016-12-05 03:11:52 +00:00
Ed Maste
a402c1e54f c++filt: flush output after newline
Some tools spawn c++filt and pass it a single line at a time for
demangling. This is akin to r276689 for addr2line.

Sponsored by:	The FreeBSD Foundation
2016-11-09 15:04:29 +00:00
Ed Maste
adb25d1e1a libelftc: add elf{32,64}-tradbigmips target emulation names
Reported by:	theraven
Sponsored by:	The FreeBSD Foundation
2016-11-07 22:41:52 +00:00
Ed Maste
f8d647b44d strings: fix exit status if a file before the last one fails
Previously a command like "strings f1 f2 f3" reported the exit status
based only on processing the last file.

As with GNU strings, report an error exit status if an error was
encountered processing any of the files. While here simplify the
exit status handling to just success (0) / failure (1).

Reviewed by:	brooks
MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D8334
2016-10-26 17:07:53 +00:00