History log of /freebsd-current/contrib/elftoolchain/elfcopy/main.c
Revision Date Author Comments
# 179219ea 25-Oct-2021 Ed Maste <emaste@FreeBSD.org>

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


# 5ac70383 15-Feb-2021 Chris Rees <crees@FreeBSD.org>

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


# 1e4896b1 18-Nov-2020 Dimitry Andric <dim@FreeBSD.org>

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


# 57d7e4cc 12-Feb-2020 Ed Maste <emaste@FreeBSD.org>

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


# 640ff6ed 04-Feb-2020 Mark Johnston <markj@FreeBSD.org>

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


# bbce1017 07-Aug-2019 Mark Johnston <markj@FreeBSD.org>

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


# 36d78189 29-Jul-2019 Mark Johnston <markj@FreeBSD.org>

Merge r3778 and r3779 from ELFToolchain.

Modify strip(1) to not accept multiple input files when an output
file is specified. There is no good way to handle this combination,
and the change is compatible with binutils.

MFC after: 1 week
Sponsored by: The FreeBSD Foundation


# bc589b72 04-Jun-2019 Mark Johnston <markj@FreeBSD.org>

elfcopy: Use libelftc's string table routines to build .shstrtab.

This replaces some hand-rolled routines and is substantially faster
since libelftc uses a hash table for lookups and insertions, whereas
elfcopy would perform a linear scan of the table.

PR: 234949
Reviewed by: emaste
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D20473


# 5405b282 30-May-2019 Mark Johnston <markj@FreeBSD.org>

elfcopy: Remove an unneeded memset.

Reviewed by: emaste, trasz
MFC after: 1 week
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D20445


# eb81f38a 05-Sep-2018 John Baldwin <jhb@FreeBSD.org>

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


# fa903e57 24-Apr-2017 Ed Maste <emaste@FreeBSD.org>

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


# 9bfb310c 30-Mar-2017 Ed Maste <emaste@FreeBSD.org>

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


# d2972ce0 22-Oct-2016 Ed Maste <emaste@FreeBSD.org>

elfcopy: select mode by the end of the program name

The mode of operation (elfcopy, mcs, or strip) is chosen based on the
program name. Broaden this to allow a substring match at the end of the
name to allow prefixes - for example, bsdstrip or aarch64-freebsd-strip.

This improves use of these tools as drop-in replacements for GNU objcopy
and strip, which are often built with a limited set of supported targets
and installed with a target prefix for cross tools.

Reviewed by: dim
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D1663


# 94bb24b3 05-Jun-2016 Bryan Drewery <bdrewery@FreeBSD.org>

Use the in-tree sys/elf_common.h to build libelftc.

This fixes build failures on older releases that lack various
definitions such as EM_AARCH64 (which was unfixed before this).

Revert all of the recent compatibility changes that worked around this
problem.

This uses the same method of using the in-tree header as lib/libelf,
lib/libdwarf and usr.bin/readelf.

Reviewed by: emaste
Sponsored by: EMC / Isilon Storage Division
Differential Revision: https://reviews.freebsd.org/D6734


# bcf9fc28 21-May-2016 Ed Maste <emaste@FreeBSD.org>

elftoolchain: backwards compatability for ELFOSABI_CLOUDABI definition

It is not provided by sys/elf_common.h on older releases or -current
before March 2015.

Reported by: Jenkins


# 3884d6f8 16-Feb-2016 Ed Maste <emaste@FreeBSD.org>

Allow elfcopy to convert between two non-ELF formats

If the output object is not an ELF file, choose an arbitrary ELF format
for the intermediate file. srec, ihex and binary formats are independent
of class, endianness and machine type so these choices do not affect the
output.

ELF Tool Chain ticket #517

Reviewed by: kai
Obtained from: ELF Tool Chain r3411


# 272a972b 28-Jan-2015 Ed Maste <emaste@FreeBSD.org>

Preserve hard & symbolic links when modifying source file

Strip is often used to modify existing files, rather than creating new
files. If the existing file has hard links or is a symbolic link, act as
if editing the file in place and preserve the links.

Reported by: luigi
Reviewed by: imp, rpaulo
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D1682


# a356a1f5 17-Dec-2014 Ed Maste <emaste@FreeBSD.org>

Do not strip all when stripping an explicit symbol

When requested to strip specific symbols (-N flag) the default should be
to strip nothing (other than the requested symbols). This is consistent
with binutils strip(1).

PR: 196038
Reviewed by: imp
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D1327