summaryrefslogtreecommitdiff
path: root/llvm/lib/ObjCopy/ELF/ELFObject.cpp
AgeCommit message (Collapse)Author
2025-11-15[llvm] Use llvm::copy (NFC) (#168182)Kazu Hirata
Identified with llvm-use-ranges.
2025-08-29[llvm] Support building with c++23 (#154372)Kyle Krüger
closes #154331 This PR addresses all minimum changes needed to compile LLVM and MLIR with the c++23 standard. It is a work in progress and to be reviewed for better methods of handling the parts of the build broken by c++23.
2025-04-26[llvm] Use llvm::copy (NFC) (#137470)Kazu Hirata
2025-01-23Reapply "[llvm-objcopy][ELF] Add an option to remove notes (#118739)"Igor Kudrin
This fixes "unused-local-typedef" warnings in 9324e6a7a5. This adds an option `--remove-note=[name/]type` to selectively delete notes in ELF files, where `type` is the numeric value of the note type and `name` is the name of the originator. The name can be omitted, in which case all notes of the specified type will be removed. For now, only `SHT_NOTE` sections that are not associated with segments are handled. The implementation can be extended later as needed. RFC: https://discourse.llvm.org/t/rfc-llvm-objcopy-feature-for-editing-notes/83491
2025-01-23Revert "[llvm-objcopy][ELF] Add an option to remove notes (#118739)"Igor Kudrin
This reverts commit 9324e6a7a5c5adc5b5c38c3e3cbecd7e1e98876a.
2025-01-23[llvm-objcopy][ELF] Add an option to remove notes (#118739)Igor Kudrin
This adds an option `--remove-note=[name/]type` to selectively delete notes in ELF files, where `type` is the numeric value of the note type and `name` is the name of the originator. The name can be omitted, in which case all notes of the specified type will be removed. For now, only `SHT_NOTE` sections that are not associated with segments are handled. The implementation can be extended later as needed. RFC: https://discourse.llvm.org/t/rfc-llvm-objcopy-feature-for-editing-notes/83491
2024-11-17[ObjCopy] Remove unused includes (NFC) (#116534)Kazu Hirata
Identified with misc-include-cleaner.
2024-07-09[llvm-objcopy] Remove references for empty section groups (#98106)Pranav Kant
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-08Revert "[llvm-objcopy] Remove empty SHT_GROUP sections (#97141)"Fangrui Song
This reverts commit 359c64f314ad568e78ee9a3723260286e3425c2d. This caused heap-use-after-free. See #98106.
2024-07-08[llvm-objcopy] Remove empty SHT_GROUP sections (#97141)Dmitriy Chestnykh
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[llvm-objcopy] Support CRELFangrui Song
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-04-15[llvm-objcopy] Add --compress-sectionsFangrui Song
--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-03-28[Object,ELFType] Rename TargetEndianness to Endianness (#86604)Fangrui Song
`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-02-20[llvm-objcopy] Fix file offsets when PT_INTERP/PT_LOAD offsets are equal ↵Fangrui Song
(#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-09[llvm-objcopy] Fix the build again after 7ddc320Maksim Panchenko
2024-02-09[llvm-objcopy] Fix the build after 7ddc32052546abd41656d2e670f3902b1bf805a7. ↵Jon Roelofs
NFCI
2024-02-09[llvm-objcopy] Support SREC output format (#75874)quic-areg
Adds a new output target "srec" to write SREC files from ELF inputs. https://en.wikipedia.org/wiki/SREC_(file_format)
2023-12-19[llvm-objcopy] --gap-fill and 0-size sections (#75837)quic-akaryaki
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-14[llvm-objcopy] Add --gap-fill and --pad-to options (#65815)quic-akaryaki
`--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-10-12Use llvm::endianness::{big,little,native} (NFC)Kazu Hirata
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-09-27[NFC] Use const references to avoid copying objects in for-loopsGregory Alfonso
Differential Revision: https://reviews.llvm.org/D139487
2023-06-20[llvm-objcopy] -O binary: do not align physical addressesAlexey Karyakin
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-20Revert "[llvm-objcopy] -O binary: do not align physical addresses"Krzysztof Parzyszek
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[llvm-objcopy] -O binary: do not align physical addressesAlexey Karyakin
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-07Remove unnecessary copyDavid Blaikie
2023-06-06[llvm-objcopy][ELF] Preserve sh_link to .symtab when applicableAndrew Ng
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-02-17[ELF][llvm-objcopy] Reject duplicate SHT_SYMTAB sectionsMoshe Berman
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
2022-12-16[LLVM][objcopy] Fix update-section.test on 32 bit platformsDavid Spickett
This used %zu to print a uint64_t type. z is for size_t so on 32 bit we tried to treat it as a 32 bit number. Use PRIu64 instead to print as 64 bit everywhere.
2022-12-10[ObjCopy] llvm::Optional => std::optionalFangrui Song
2022-10-12[llvm-objcopy] Support --decompress-debug-sections when zlib is disabledFangrui Song
When zlib is disabled at build time, the diagnostic `LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress` for --decompress-debug-sections may be inaccurate: if zstd is enabled, we should still support zstd decompression. It's not useful to test zlib and zstd. Just remove the diagnostic and add a new one before `compression::decompress`. This fixes compress-debug-sections-zstd.test Reviewed By: mariusz-sikora-at-amd, jhenderson, phosek Differential Revision: https://reviews.llvm.org/D135744
2022-09-22[llvm-objcopy] --compress-debug-sections: remove tail padding for ELFCLASS32Fangrui Song
For an ELFCLASS32 object, a compressed section due to --compress-debug-sections={zlib,zstd} has a tail padding of 12 zero bytes. zlib happily allows this while zstd is rigid and reports `error: '{{.*}}': failed to decompress section '.debug_foo': Src size is incorrect`. Cole Kissane reported the problem. Reviewed By: jhenderson, phosek Differential Revision: https://reviews.llvm.org/D134385
2022-09-08[Support] Rename DebugCompressionType::Z to ZlibFangrui Song
"Z" was so named when we had both gABI ELFCOMPRESS_ZLIB and the legacy .zdebug support. Now we have just one zlib format, we should use the more descriptive name.
2022-09-08[llvm-objcopy] Support --{,de}compress-debug-sections for zstdFangrui Song
Also, add ELFCOMPRESS_ZSTD (2) from the approved generic-abi proposal: https://groups.google.com/g/generic-abi/c/satyPkuMisk ("Add new ch_type value: ELFCOMPRESS_ZSTD") Link: https://discourse.llvm.org/t/rfc-zstandard-as-a-second-compression-method-to-llvm/63399 ("[RFC] Zstandard as a second compression method to LLVM") Reviewed By: jhenderson, dblaikie Differential Revision: https://reviews.llvm.org/D130458
2022-09-08Revert "[Support] Add ↵Nikita Popov
llvm::compression::{getReasonIfUnsupported,compress,decompress}" This reverts commit 19dc3cff0f771bb8933136ef68e782553e920d04. This reverts commit 5b19a1f8e88da9ec92b995bfee90043795c2c252. This reverts commit 9397648ac8ad192f7e6e6a8e6894c27bf7e024e9. This reverts commit 10842b44759f987777b08e7714ef77da2526473a. Breaks the GCC build, as reported here: https://reviews.llvm.org/D130506#3776415
2022-09-07[llvm-objcopy] Support --{,de}compress-debug-sections for zstdFangrui Song
Also, add ELFCOMPRESS_ZSTD (2) from the approved generic-abi proposal: https://groups.google.com/g/generic-abi/c/satyPkuMisk ("Add new ch_type value: ELFCOMPRESS_ZSTD") Link: https://discourse.llvm.org/t/rfc-zstandard-as-a-second-compression-method-to-llvm/63399 ("[RFC] Zstandard as a second compression method to LLVM") Reviewed By: jhenderson, dblaikie Differential Revision: https://reviews.llvm.org/D130458
2022-07-29Revert D130458 "[llvm-objcopy] Support --{,de}compress-debug-sections for zstd"Fangrui Song
This reverts commit c26dc2904b95b3685d883e760e84046ea6c33d7f. The new Zstd dispatch has an ongoing design discussion related to https://reviews.llvm.org/D130516#3688123 . Revert for now before it is resolved.
2022-07-28[llvm-objcopy] Support --{,de}compress-debug-sections for zstdFangrui Song
Also, add ELFCOMPRESS_ZSTD (2) from the approved generic-abi proposal: https://groups.google.com/g/generic-abi/c/satyPkuMisk ("Add new ch_type value: ELFCOMPRESS_ZSTD") Link: https://discourse.llvm.org/t/rfc-zstandard-as-a-second-compression-method-to-llvm/63399 ("[RFC] Zstandard as a second compression method to LLVM") Differential Revision: https://reviews.llvm.org/D130458
2022-07-25[llvm-objcopy] Remove getDecompressedSizeAndAlignment. NFCFangrui Song
2022-07-24[llvm-objcopy] --compress-debug-sections: fix uninitialized ch_reserved for ↵Fangrui Song
Elf64_Chdr ch_reserved is uninitialized and the output is not deterministic. Fix it. Rewrite and improve compress-debug-sections-zlib.test.
2022-07-24[llvm-objcopy] Remove remnant .zdebug codeFangrui Song
2022-07-24[MC] Delete dead zlib-gnu code and simplify writeSectionDataFangrui Song
2022-07-13[Support] Change compression::zlib::{compress,uncompress} to use uint8_t *Fangrui Song
It's more natural to use uint8_t * (std::byte needs C++17 and llvm has too much uint8_t *) and most callers use uint8_t * instead of char *. The functions are recently moved into `llvm::compression::zlib::`, so downstream projects need to make adaption anyway.
2022-07-08[NFC] Refactor llvm::zlib namespaceCole Kissane
* Refactor compression namespaces across the project, making way for a possible introduction of alternatives to zlib compression. Changes are as follows: * Relocate the `llvm::zlib` namespace to `llvm::compression::zlib`. Reviewed By: MaskRay, leonardchan, phosek Differential Revision: https://reviews.llvm.org/D128953
2022-07-04[llvm-objcopy] -O binary: align sh_offset for section changed from SHT_NOBITSFangrui Song
For a SHT_NOBITS section like .bss, its sh_offset is typically not aligned by sh_addralign. If it is converted to SHT_PROGBITS by `--set-section-flags .bss=alloc,contents`, we should conceptually align it when computing the output size for -O binary. Otherwise the output size may be smaller than GNU objcopy produced output. * binary-no-paddr.test has a case with non-sensical p_paddr=1 which has a changed behavior. Update it. Close https://github.com/llvm/llvm-project/issues/55246 Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D128961
2022-06-29[llvm-objcopy] Remove support for legacy .zdebug sectionsFangrui Song
clang 14 removed -gz=zlib-gnu support and ld.lld removed linker input support for zlib-gnu in D126793. Now let's remove zlib-gnu from llvm-objcopy. * .zdebug* sections are no longer recognized as debug sections. --strip* don't remove them. They are copied like other opaque sections * --decompress-debug-sections does not uncompress .zdebug* sections * --compress-debug-sections=zlib-gnu is not supported It is very rare but in case a user has object files using .zdebug . They can use llvm-objcopy<15 or GNU objcopy for uncompression. --compress-debug-sections=zlib-gnu is unlikely ever used by anyone, so I do not add a custom diagnostic. Differential Revision: https://reviews.llvm.org/D128688
2022-06-05[llvm] Convert for_each to range-based for loops (NFC)Kazu Hirata
2022-05-25[llvm-objcopy][ObjectYAML][mips] Add MIPS specific ELF section indexesAnubhab Ghosh
This fixes https://github.com/llvm/llvm-project/issues/53998 and displays correct information in obj2yaml for SHN_MIPS_* sections according to https://refspecs.linuxfoundation.org/elf/mipsabi.pdf Reviewed By: jhenderson, MaskRay Differential Revision: https://reviews.llvm.org/D123902
2022-03-28Apply clang-tidy fixes for modernize-use-equals-default in ELFObject.cpp (NFC)Kazu Hirata
2022-03-14[llvm-objcopy] Simplify CompressedSection creation. NFCFangrui Song
Remove Expected<CompressedSection> factory functions in favor of constructors now that zlib::compress returns void (D121512). Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D121644
2022-03-14[Support] Change zlib::compress to return voidFangrui Song
With a sufficiently large output buffer, the only failure is Z_MEM_ERROR. Check it and call the noreturn report_bad_alloc_error if applicable. resize_for_overwrite may call report_bad_alloc_error as well. Now that there is no other error type, we can replace the return type with void and simplify call sites. Reviewed By: ikudrin Differential Revision: https://reviews.llvm.org/D121512