122 Commits

Author SHA1 Message Date
Kazu Hirata
54dad9e269
[ObjCopy] Remove unused includes (NFC) (#116534)
Identified with misc-include-cleaner.
2024-11-17 08:39:13 -08:00
Kazu Hirata
e4e3ff5adc
[llvm] Use std::optional::value_or (NFC) (#109568) 2024-09-22 01:00:24 -07:00
Sam Clegg
2a6136e552
[llvm-objcopy][WebAssembly] Allow --strip-debug to operate on relocatable files. (#102978)
This change is enough to allow `--strip-debug` to work on object files,
without breaking the relocation information or symbol table.

A more complete version of this change would instead reconstruct the
symbol table and relocation sections, but that is much larger change.

Bug: #102002
2024-08-19 21:52:17 -07:00
Eleanor Bonnici
2b2f4ae0fb
[llvm-objcopy] Add --change-section-address (#98664)
--change-section address and its alias --adjust-section-vma allows
modification
of section addresses in a relocatable file. This used to be used, for
example,
in Fiasco microkernel.

On a relocatable file this option behaves the same as GNU objcopy, apart
from
the fact that it does not issue any warnings, for example, when an
argument is
not used.
GNU objcopy does not produce an error when passed an executable file but
the
usecase for this is not clear, and the behaviour is inconsistent. The
idea of
GNU objcopy --change-section-address is that the option should change
both LMA
and VMA in an executable file. Since this patch does not implement
executable
file support, only VMA is changed.
2024-07-30 10:57:57 +01:00
serge-sans-paille
fb5a38bb49
[llvm-objcopy] Add verification of added .note section format
Also add a --no-verify-note-sections flag to make it possible to add
invalid sections if needs be.

Pull Request: https://github.com/llvm/llvm-project/pull/90458
2024-07-11 17:59:19 +02:00
Pranav Kant
e142a55686
[llvm-objcopy] Remove references for empty section groups (#98106)
Otherwise, llvm-objcopy fails with use-after-free when built under
sanitizers. Simple repro: run the test
`ELF/remove-section-in-group.test` under asan. This is due to symbol
table references to empty section groups that must be removed.
2024-07-09 14:48:09 -07:00
Fangrui Song
2abe53a17f Revert "[llvm-objcopy] Remove empty SHT_GROUP sections (#97141)"
This reverts commit 359c64f314ad568e78ee9a3723260286e3425c2d.

This caused heap-use-after-free. See #98106.
2024-07-08 23:47:28 -07:00
Dmitriy Chestnykh
359c64f314
[llvm-objcopy] Remove empty SHT_GROUP sections (#97141)
Currently `llvm-objcopy/llvm-strip` in `--strip-debug` mode doesn't
remove such sections. This behavior can lead to incompatibilities with
GNU binutils (for examples ld.bfd before https://sourceware.org/PR20520
cannot process the object file contains empty .group section).
The ELF object that contains group section with `.debug_*` sections
inside can be obtained by `gcc -g3`.
Fix #97139
2024-07-08 11:50:23 -07:00
Fangrui Song
9bb4cd5977
[llvm-objcopy] Support CREL
llvm-objcopy may modify the symbol table and need to rewrite
relocations. For CREL, while we can reuse the decoder from #91280, we
need an encoder to support CREL.

Since MC/ELFObjectWriter.cpp has an existing encoder, and MC is at a
lower layer than Object, extract the encoder to a new header file
llvm/MC/MCELFExtras.h.

Link: https://discourse.llvm.org/t/rfc-crel-a-compact-relocation-format-for-elf/77600

Pull Request: https://github.com/llvm/llvm-project/pull/97521
2024-07-08 09:32:08 -07:00
Eleanor Bonnici
3320036370
[llvm-objcopy] Add change-section-lma *+/-offset (#95431)
llvm-objcopy did not support change-section-lma argument.

This patch adds support for a use case of change-section-lma, that is
shifting load address of all sections by the same offset. This seems to
be the only practical use case of change-section-lma, found in other
software such as Zephyr RTOS's build system.

This is an option that could possibly be supported in some other than
ELF formats, however this change only implements it for ELF. When used
with other formats an error message is raised.

In comparison, the behavior of GNU objcopy is inconsistent. For some ELF
files it behaves the same as described above. For others, it copies the
file without modifying the p_paddr fields when it would be expected. In
some experiments it modifies arbitrary fields in section or program
headers. It is unclear what exactly determines this.
The executable file generated by yaml2obj in this test is not parsable
by GNU objcopy. With Machine set to EM_AARCH64, the file can be parsed
and the first test in the test file completes with 0 exit code. However,
the result is rather arbitrary. AArch64 GNU objcopy subtracts 0x1000
from p_filesz and p_memsz of the first LOAD section and 0x1000 from
p_offset of the second LOAD section. It does not look meaningful.
2024-07-08 17:17:09 +01:00
Braden Helmer
79ce70b803
[NFC] Mitigate pointless copies (#95052)
Fixes #95036 #95033 #94933 #94930
2024-06-11 09:54:00 +01:00
Fangrui Song
07942987b5 [llvm-objcopy] Add --compress-sections
--compress-sections is similar to --compress-debug-sections but applies
to arbitrary sections.

* `--compress-sections <section>=none`: decompress sections
* `--compress-sections <section>=[zlib|zstd]`: compress sections with zlib/zstd

Like `--remove-section`, the pattern is by default a glob, but a regex
when --regex is specified.

For `--remove-section` like options, `!` prevents matches and is not
dependent on ordering (see `ELF/wildcard-syntax.test`). Since
`--compress-sections a=zlib --compress-sections a=none` naturally allows
overriding, having an order-independent `!` would be confusing.
Therefore, `!` is disallowed.

Sections within a segment are effectively immutable. Report an error for
an attempt to (de)compress them. `SHF_ALLOC` sections in a relocatable
file can be compressed, but linkers usually reject them.

Note: Before this patch, a compressed relocation section is recognized
as a `RelocationSectionBase` as well and `removeSections` `!ToRemove(*ToRelSec)`
may incorrectly interpret a `CompressedSections` as `RelocationSectionBase`,
leading to ubsan failure for the new test. Fix this by setting
`OriginalFlags` in CompressedSection::CompressedSection.

Link: https://discourse.llvm.org/t/rfc-compress-arbitrary-sections-with-ld-lld-compress-sections/71674

Pull Request: https://github.com/llvm/llvm-project/pull/85036
2024-04-15 00:48:51 -07:00
Mitch Phillips
be8bc3cf43 Revert "[llvm-objcopy] Add --compress-sections"
This reverts commit 9e3b64b9f95aadf57568576712902a272fe66503.

Reason: Broke the UBSan buildbot. See the comments in the pull request
(https://github.com/llvm/llvm-project/pull/85036) for more information.
2024-04-05 11:42:52 +02:00
Fangrui Song
9e3b64b9f9 [llvm-objcopy] Add --compress-sections
--compress-sections is similar to --compress-debug-sections but applies
to arbitrary sections.

* `--compress-sections <section>=none`: decompress sections
* `--compress-sections <section>=[zlib|zstd]`: compress sections with zlib/zstd

Like `--remove-section`, the pattern is by default a glob, but a regex
when --regex is specified.

For `--remove-section` like options, `!` prevents matches and is not
dependent on ordering (see `ELF/wildcard-syntax.test`). Since
`--compress-sections a=zlib --compress-sections a=none` naturally allows
overriding, having an order-independent `!` would be confusing.
Therefore, `!` is disallowed.

Sections within a segment are effectively immutable. Report an error for
an attempt to (de)compress them. `SHF_ALLOC` sections in a relocatable
file can be compressed, but linkers usually reject them.

Link: https://discourse.llvm.org/t/rfc-compress-arbitrary-sections-with-ld-lld-compress-sections/71674

Pull Request: https://github.com/llvm/llvm-project/pull/85036
2024-04-04 09:33:18 -07:00
Fangrui Song
2763353891
[Object,ELFType] Rename TargetEndianness to Endianness (#86604)
`TargetEndianness` is long and unwieldy. "Target" in the name is confusing. Rename it to "Endianness".

I cannot find noticeable out-of-tree users of `TargetEndianness`, but
keep `TargetEndianness` to make this patch safer. `TargetEndianness`
will be removed by a subsequent change.
2024-03-28 09:10:34 -07:00
Ilia Kuklin
4946cc37f4
[llvm-objcopy] Add --skip-symbol and --skip-symbols options (#80873)
Add --skip-symbol and --skip-symbols options that allow to skip symbols
when executing other options that can change the symbol's name, binding
or visibility, similar to an existing option --keep-symbol that keeps a
symbol from being removed by other options.
2024-03-21 17:05:35 +05:00
Fangrui Song
122d368b2b
[llvm-objcopy] --[de]compress-debug-sections: don't compress SHF_ALLOC sections, only decompress .debug sections
Simplify --[de]compress-debug-sections to make it easier to add custom section [de]compression.
Change the following two behaviors to match GNU objcopy.

* --compress-debug-sections compresses SHF_ALLOC sections while GNU
  doesn't.
* --decompress-debug-sections decompresses non-debug sections while GNU
  doesn't.

Pull Request: https://github.com/llvm/llvm-project/pull/84885
2024-03-13 10:13:21 -07:00
Justin Lebar
fab2bb8bfd
Add llvm::min/max_element and use it in llvm/ and mlir/ directories. (#84678)
For some reason this was missing from STLExtras.
2024-03-10 20:00:13 -07:00
Ilia Kuklin
07d8a457ad
[llvm-objcopy] Add --set-symbol-visibility and --set-symbols-visibility options (#80872)
Add options --set-symbol-visibility and --set-symbols-visibility to
manually change the visibility of symbols.

There is already an option to set the visibility of newly added symbols
via --add-symbol and --new-symbol-visibility. This option will allow to
change the visibility of already existing symbols.
2024-02-28 17:38:26 +05:00
Fangrui Song
ef28379022
[llvm-objcopy] Fix file offsets when PT_INTERP/PT_LOAD offsets are equal (#80562)
(#79887) When the offset of a PT_INTERP segment equals the offset of a
PT_LOAD segment, we consider that the parent of the PT_LOAD segment is
the PT_INTERP segment. In `layoutSegments`, we place both segments to be
after the current `Offset`, ignoring the PT_LOAD alignment.

This scenario is possible with fixed section addresses, but doesn't
happen with default linker layouts (.interp precedes other sections and
is part of a PT_LOAD segment containing the ELF header and program
headers).

```
% cat a.s
.globl _start; _start: ret
.rodata; .byte 0
.tdata; .balign 4096; .byte 0
% clang -fuse-ld=lld a.s -o a -nostdlib -no-pie -z separate-loadable-segments -Wl,-Ttext=0x201000,--section-start=.interp=0x202000,--section-start=.rodata=0x202020,-z,nognustack
% llvm-objcopy a a2
% llvm-readelf -l a2   # incorrect offset(PT_LOAD)
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  PHDR           0x000040 0x0000000000200040 0x0000000000200040 0x0001c0 0x0001c0 R   0x8
  INTERP         0x001001 0x0000000000202000 0x0000000000202000 0x00001c 0x00001c R   0x1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x000000 0x0000000000200000 0x0000000000200000 0x000200 0x000200 R   0x1000
  LOAD           0x001000 0x0000000000201000 0x0000000000201000 0x000001 0x000001 R E 0x1000
//// incorrect offset
  LOAD           0x001001 0x0000000000202000 0x0000000000202000 0x000021 0x000021 R   0x1000
  LOAD           0x002000 0x0000000000203000 0x0000000000203000 0x000001 0x001000 RW  0x1000
  TLS            0x002000 0x0000000000203000 0x0000000000203000 0x000001 0x000001 R   0x1000
  GNU_RELRO      0x002000 0x0000000000203000 0x0000000000203000 0x000001 0x001000 R   0x1000
```

The same issue occurs for PT_TLS/PT_GNU_RELRO if we PT_TLS's alignment
is smaller and we place the PT_LOAD after PT_TLS/PT_GNU_RELRO segments
(not linker default, but possible with a `PHDRS` linker script command).

Fix #79887: when two segments have the same offset, order the one with a
larger alignment first. In the previous case, the PT_LOAD segment will
go before the PT_INTERP segment. In case of equal alignments, it doesn't
matter which segment is treated as the parent segment.
2024-02-20 09:26:04 -08:00
Maksim Panchenko
2cbe5a33a5 [llvm-objcopy] Fix the build again after 7ddc320 2024-02-09 12:26:12 -08:00
Jon Roelofs
5afbed1968
[llvm-objcopy] Fix the build after 7ddc32052546abd41656d2e670f3902b1bf805a7. NFCI 2024-02-09 08:49:45 -08:00
quic-areg
7ddc320525
[llvm-objcopy] Support SREC output format (#75874)
Adds a new output target "srec" to write SREC files from ELF inputs.

https://en.wikipedia.org/wiki/SREC_(file_format)
2024-02-09 16:15:23 +00:00
Yi Kong
1b87ebce92
[llvm-objcopy] Add --remove-symbol-prefix (#79415) 2024-02-07 17:38:09 +09:00
Felix Kellenbenz
11ca56eaf1
[llvm-objcopy] Don't remove .gnu_debuglink section when using --strip-all (#78919)
This fixes the issue mentioned here:
https://github.com/llvm/llvm-project/issues/57407
It prevents `llvm-objcopy` from removing the `.gnu _debuglink` section
when used with the `--strip-all` flag. Since `--strip-all` is the
default of `llvm-strip` the patch also prevents `llvm-strip` from
removing the `.gnu_debuglink` section.
2024-01-24 08:12:16 +00:00
Kazu Hirata
01c5b5c104 [ObjCopy] Use StringRef::consume_front (NFC) 2024-01-19 18:54:06 -08:00
quic-akaryaki
535520c663
[llvm-objcopy] --gap-fill and 0-size sections (#75837)
In the change that added `--gap-fill`, the condition to choose the
sections to write in `BinaryWriter::write()` did not exclude zero-size
sections. However, zero-size sections did not have correct offsets
assigned in `BinaryWriter::finalize()`. The result is either a failed
assertion, or memory corruption due to writing to the buffer beyond its
size.
To fix this, exclude zero-size sections from writing. Also, add a zero-size
section to the test, which would trigger the problem.
2023-12-19 15:30:29 -06:00
quic-akaryaki
4070dffd34
[llvm-objcopy] Add --gap-fill and --pad-to options (#65815)
`--gap-fill <value>` fills the gaps between sections with a specified
8-bit value, instead of zero.
`--pad-to <address>` pads the output binary up to the specified load
address, using the 8-bit value from `--gap-fill` or zero.

These options are only supported for ELF input and binary output.
2023-12-14 16:28:34 -06:00
Abhina Sree
ec41462d7a
[SystemZ][z/OS] Add missing strnlen function for z/OS to fix build failures (#75339)
This patch adds strnlen to the zOSSupport.h file to fix build failures in multiple files.
2023-12-13 13:13:53 -05:00
Kazu Hirata
586ecdf205
[llvm] Use StringRef::{starts,ends}_with (NFC) (#74956)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-11 21:01:36 -08:00
Ilia Kuklin
bdcb841aa7
[objcopy] Return an error in case of an invalid regex (#74319)
As of now, llvm-objcopy silently ignores a provided regex if it doesn't
compile.

This patch adds returning an error saying that a regex couldn't be
compiled, along with the compilation error message.

---------

Co-authored-by: James Henderson <46713263+jh7370@users.noreply.github.com>
2023-12-08 01:16:52 +05:00
Kazu Hirata
92c2529ccd [llvm] Stop including vector (NFC)
Identified with clangd.
2023-12-03 22:32:21 -08:00
Dan Zimmerman
0807846a9e
[objcopy] Implement --weaken, --weaken-symbol(s) flags for Mach-O Object Files (#70560)
This PR implements --weaken, --weaken-symbol(s)  flags in llvm-objcopy for Mach-O.

Test plan: ninja check-all
2023-11-06 01:20:52 -08:00
Kazu Hirata
4a0ccfa865 Use llvm::endianness::{big,little,native} (NFC)
Note that llvm::support::endianness has been renamed to
llvm::endianness while becoming an enum class as opposed to an
enum. This patch replaces support::{big,little,native} with
llvm::endianness::{big,little,native}.
2023-10-12 21:21:45 -07:00
Thurston Dang
bfa7d10b9f
[MachO] Fix unaligned load in extractSections (#68741)
A recent change to ubsan

(792674400f)
exposed an unaligned load in MachOReader (see
https://lab.llvm.org/buildbot/#/builders/85/builds/19482 for an
example).

This patch fixes it by dropping the alignment.
2023-10-10 13:48:13 -07:00
Mikael Holmen
336b7a256b [ObjCopy] Fix warning in conditional expression [NFC]
Without the fix gcc warned with
 ../lib/ObjCopy/ELF/ELFObjcopy.cpp: In function 'uint64_t getSectionFlagsPreserveMask(uint64_t, uint64_t, uint16_t)':
 ../lib/ObjCopy/ELF/ELFObjcopy.cpp:106:31: warning: enumeral and non-enumeral type in conditional expression [-Wextra]
   106 |       ~(EMachine == EM_X86_64 ? ELF::SHF_X86_64_LARGE : 0UL);
       |         ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2023-09-29 12:44:54 +02:00
Gregory Alfonso
40dc8e6889 [NFC] Use const references to avoid copying objects in for-loops
Differential Revision: https://reviews.llvm.org/D139487
2023-09-27 13:39:30 -07:00
zhijian
f740bcb370 [AIX] supporting -X options for llvm-ranlib in AIX OS
Summary:

llvm-ar is symlinked as llvm-ranlib and will act as ranlib when invoked in that mode. llvm-ar since [[ 4f2cfbe531 | compiler/llvm-project@4f2cfbe ]] supports the -X options, but doesn't seem to accept them when running as llvm-ranlib.

In AIX OS , according to https://www.ibm.com/docs/en/aix/7.2?topic=r-ranlib-command

-X mode 	Specifies the type of object file ranlib should examine. The mode must be one of the following:

32
    Processes only 32-bit object files
64
    Processes only 64-bit object files
32_64, any
    Processes both 32-bit and 64-bit object files

The default is to process 32-bit object files (ignore 64-bit objects). The mode can also be set with the OBJECT_MODE environment variable. For example, OBJECT_MODE=64 causes ranlib to process any 64-bit objects and ignore 32-bit objects. The -X flag overrides the OBJECT_MODE variable.

Reviewers: James Henderson, MaskRay, Stephen Peckham
Differential Revision: https://reviews.llvm.org/D142660
2023-08-22 09:41:33 -04:00
Derek Schuff
1b21067cf2 [WebAssembly][Objcopy] Write output section headers identically to inputs
Previously when objcopy generated section headers, it padded the LEB
that encodes the section size out to 5 bytes, matching the behavior of
clang. This is correct, but results in a binary that differs from the
input. This can sometimes have undesirable consequences (e.g. breaking
source maps).

This change makes the object reader remember the size of the LEB
encoding in the section header, so that llvm-objcopy can reproduce it
exactly. For sections not read from an object file (e.g. that
llvm-objcopy is adding itself), pad to 5 bytes.

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D155535
2023-07-27 15:43:51 -07:00
Thomas Köppe
6f1395a1fe [llvm-objcopy] --set-section-flags: allow "large" to add SHF_X86_64_LARGE
Currently, objcopy cannot set the new flag SHF_X86_64_LARGE. This change introduces the named flag "large" which translates to that section flag.

An "invalid argument" error is produced if a user attempts to set the flag on an architecture other than X86_64.

Reviewed By: jhenderson, MaskRay

Differential Revision: https://reviews.llvm.org/D153262
2023-07-25 09:47:02 -07:00
Thomas Köppe
b617669229 [llvm-objcopy] Remove unused internal helper function template makeStringError. NFC
Reviewed By: jhenderson, MaskRay

Differential Revision: https://reviews.llvm.org/D153671
2023-07-15 11:22:54 -07:00
Elliot Goodrich
b0abd4893f [llvm] Add missing StringExtras.h includes
In preparation for removing the `#include "llvm/ADT/StringExtras.h"`
from the header to source file of `llvm/Support/Error.h`, first add in
all the missing includes that were previously included transitively
through this header.
2023-06-25 15:42:22 +01:00
Alexey Karyakin
c973123fdc [llvm-objcopy] -O binary: do not align physical addresses
llvm-objcopy should not insert padding before a section if its
physical addresses is not aligned to section's alignment. This
behavior will match GNU objcopy and is important for embedded images
where the physical address is used to store the initial data image.
The loader typically will copy this image using a start symbol
created by the linker. If llvm-objcopy inserts padding before such a
section, the symbol address will not match the location in the image.

This commit refines the change in https://reviews.llvm.org/D128961
which intended to align sections which type changed from NOBITS and
their offset may not be aligned. However, it affected all sections.

Fix https://github.com/llvm/llvm-project/issues/62636

Reviewed By: jhenderson, MaskRay

Differential Revision: https://reviews.llvm.org/D150276
2023-06-20 15:45:33 -07:00
Krzysztof Parzyszek
ebc757d3b6 Revert "[llvm-objcopy] -O binary: do not align physical addresses"
This reverts commit eb1442d0f73c76cfb5051d133f858fe760d189cf.

The test tools/llvm-objcopy/ELF/binary-paddr.test fails on
ppc64be-clang-test-suite:
https://lab.llvm.org/buildbot#builders/231/builds/13120

Reverting at author's request.
2023-06-20 14:46:55 -07:00
Alexey Karyakin
eb1442d0f7 [llvm-objcopy] -O binary: do not align physical addresses
llvm-objcopy should not insert padding before a section if its
physical addresses is not aligned to section's alignment. This
behavior will match GNU objcopy and is important for embedded images
where the physical address is used to store the initial data image.
The loader typically will copy this image using a start symbol
created by the linker. If llvm-objcopy inserts padding before such a
section, the symbol address will not match the location in the image.

This commit refines the change in https://reviews.llvm.org/D128961
which intended to align sections which type changed from NOBITS and
their offset may not be aligned. However, it affected all sections.

Fix https://github.com/llvm/llvm-project/issues/62636

Reviewed By: jhenderson, MaskRay

Differential Revision: https://reviews.llvm.org/D150276
2023-06-20 13:28:40 -07:00
David Blaikie
f3df0cf3b0 Remove unnecessary copy 2023-06-07 18:02:58 +00:00
Andrew Ng
744e589caa [llvm-objcopy][ELF] Preserve sh_link to .symtab when applicable
This change to llvm-objcopy preserves the ELF section sh_link to .symtab
so long as none of the symbol table indices have been changed.
Previously, any invocation of llvm-objcopy including a "no-op" would
clear any section sh_link to .symtab.

Differential Revision: https://reviews.llvm.org/D150859
2023-06-06 14:35:04 +01:00
Mike Hommey
57dc16fbe3
[llvm] Strip stabs symbols in Mach-O when stripping debug info 2023-06-03 09:31:42 +02:00
Saleem Abdulrasool
00e6d0e9ac ObjCopy: support --dump-section on COFF
Add support for --dump-section on COFF files. This is helpful for
extracting specific content from an object file on Windows.

Differential Revision: https://reviews.llvm.org/D150305
Reviewed By: @alexander-shaposhnikov, @jhenderson, @hjyamauchi
2023-05-12 14:42:06 -07:00
Moshe Berman
bc6e10c9ef [ELF][llvm-objcopy] Reject duplicate SHT_SYMTAB sections
The gABI prohibits multiple SH_SYMTAB sections. As a result,
llvm-objcopy was crashing in SymbolTableSection::removeSymbols(). This
patch fixes the issue by emitting an error if multiple SH_SYMTAB
sections are encountered when building an ELF object.

Fixes: https://github.com/llvm/llvm-project/issues/60448

Differential Revision: https://reviews.llvm.org/D143508
2023-02-17 17:59:20 +00:00