History log of /freebsd-current/contrib/elftoolchain/elfcopy/sections.c
Revision Date Author Comments
# 05ab6549 20-Mar-2020 Ed Maste <emaste@FreeBSD.org>

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


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

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


# 4133f236 08-Nov-2019 Ed Maste <emaste@FreeBSD.org>

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] https://repo.or.cz/w/nasm.git/commit/1f0cb0f2c1ba632c0fab02424928cfb756a9160c
[3] https://sourceforge.net/p/elftoolchain/tickets/485/

PR: 198611
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D2292


# 0070b575 04-Jul-2019 Mark Johnston <markj@FreeBSD.org>

elfcopy: Clear errors after fetching the shstrtab index.

Otherwise a future call to elf_errno() will return a non-zero value.
update_shdr(), for example, treats any errors associated with the ELF
descriptor as fatal. Clear the error per the first example in
elf_errmsg.3.

Convert to elf_getshdrstrndx() while here since elf_getshstrndx() is
apparently deprecated.

Reported by: royger
Reviewed by: emaste
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D20852


# c8b057f4 26-Jun-2019 Mark Johnston <markj@FreeBSD.org>

elfcopy: Provide a size hint when creating the section string table.

Use the input file's .shstrtab size as the hint if it exists. This
gives a small performance improvement when processing files with
many sections.

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


# 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


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

elfcopy: Use elf_getscn() instead of iterating over all sections.

When removing a section, we would loop over all sections looking for
a corresponding relocation section. With r348652 it is much faster
to just use elf_getscn().

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


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

elfcopy: Optimize for insertions at the end of the section list.

This is the common case when strip(1) is creating the output file.
The change provides a significant speedup when running on ELF files with
many sections.

PR: 234949
Reviewed by: emaste
MFC after: 1 week
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D20444


# aee4c74c 20-Oct-2018 Ed Maste <emaste@FreeBSD.org>

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


# c9504239 13-Oct-2018 Ed Maste <emaste@FreeBSD.org>

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


# b027d654 12-Jul-2018 Mark Johnston <markj@FreeBSD.org>

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


# 48d41ef0 13-May-2018 Phil Shafer <phil@FreeBSD.org>

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


# d938d64e 15-Apr-2016 Ed Maste <emaste@FreeBSD.org>

elfcopy: fix symbol table handling when sections come after symtab/strtab

Fix a symbol table handling bug in elfcopy: elfcopy puts .symtab,
.strtab and .shstrtab sections in the end of the output object. If
the input objects have more sections after any of these 3 sections,
the section table will be reordered, and in that case the section
symbols should be regenerated for relocations.

The bug is triggered since newer clang puts .strtab section in the
beginning of the object produced.

Ticket: #525

Reported by: royger
Obtained from: ELF Tool Chain r3443


# f5e9c916 04-Mar-2016 Ed Maste <emaste@FreeBSD.org>

elfcopy: fail if debug link target is empty

An empty debug link target previously returned a confusing and incorrect
error like "objcopy: fread failed: No error: 0". Now, return an explicit
error.

GNU objcopy allows an empty file as the debug link target. However,
that case is nonsensical so diverging from GNU behaviour is fine.

Reviewed by: bdrewery
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D5541


# 619ba3b4 13-Jun-2015 Ed Maste <emaste@FreeBSD.org>

elfcopy: Handle objects without a ".shstrtab" section string table

As of LLVM revision 238073, LLVM stores symbols and section names in
the same string table. From the upstream commit mesage:

With the scheme of naming sections like ".text.foo" where foo is a
symbol, there is a big potential saving in using a single one.

This is a cherry-pick of ELF Tool Chain revision 3225.

Sponsored by: The FreeBSD Foundation


# 2b39d4f6 14-Jan-2015 Ed Maste <emaste@FreeBSD.org>

elfcopy: Avoid divide-by-0 on section alignment 0

According to ELF ABI, alignment 0 and 1 has the same meaning: the
section has no alignment constraints.

PR: 196715
Sponsored by: The FreeBSD Foundation


# 9ef62fdb 22-Dec-2014 Ed Maste <emaste@FreeBSD.org>

Set up default shstrtab entries at shstrtab initialization

Instead of waiting until the addition of the first non-default entry.
This fixes a segfault when strip(1) is asked to remove every section from
an object file.

Upstream elftoolchain ticket 463

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


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

Include section name in strip warning message