aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-objcopy/ELF
AgeCommit message (Collapse)AuthorFilesLines
2022-02-17[llvm-objcopy][NFC] Move core implementation of llvm-objcopy into separate ↵Alexey Lapshin5-4850/+0
library. This patch moves core implementation of llvm-objcopy into Object library (http://lists.llvm.org/pipermail/llvm-dev/2020-September/145075.html). The functionality for parsing input options is left inside tools/llvm-objcopy. The interface of ObjCopy library: ObjCopy/ELF/ELFObjcopy.h ``` Error executeObjcopyOnIHex(const CopyConfig &Config, MemoryBuffer &In, Buffer &Out); Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In, Buffer &Out); Error executeObjcopyOnBinary(const CopyConfig &Config, object::ELFObjectFileBase &In, Buffer &Out); ``` ObjCopy/COFF/COFFObjcopy.h ``` Error executeObjcopyOnBinary(const CopyConfig &Config, object::COFFObjectFile &In, Buffer &Out); ``` ObjCopy/MachO/MachOObjcopy.h ``` Error executeObjcopyOnBinary(const CopyConfig &Config, object::MachOObjectFile &In, Buffer &Out); ``` ObjCopy/wasm/WasmObjcopy.h ``` Error executeObjcopyOnBinary(const CopyConfig &Config, object::WasmObjectFile &In, Buffer &Out); ``` Differential Revision: https://reviews.llvm.org/D88827
2022-02-03[llvm-objcopy][COFF] Implement --update-sectionAlex Brachet1-1/+1
Reviewed By: jhenderson, MaskRay Differential Revision: https://reviews.llvm.org/D118189
2022-01-19[llvm-objcopy] Preserve ARM and AArch64 mapping symbolsIgor Kudrin1-6/+44
Mapping symbols are required by ARM/AArch64 ELF ABI. They help to disassemble files correctly and are also used in linkers. Nonetheless, for executable files, the symbols can be stripped to better resemble the behavior of GNU's objcopy. Differential Revision: https://reviews.llvm.org/D117233
2022-01-08[llvm] Remove redundant member initialization (NFC)Kazu Hirata1-4/+2
Identified with readability-redundant-member-init.
2022-01-03Revert "[llvm] Remove redundant member initialization (NFC)"Kazu Hirata1-2/+4
This reverts commit fd4808887ee47f3ec8a030e9211169ef4fb094c3. This patch causes gcc to issue a lot of warnings like: warning: base class ‘class llvm::MCParsedAsmOperand’ should be explicitly initialized in the copy constructor [-Wextra]
2022-01-01[llvm] Remove redundant member initialization (NFC)Kazu Hirata1-4/+2
Identified with readability-redundant-member-init.
2021-12-14[llvm-objcopy] Fix handling of MIPS64 little endian filesSimon Atanasyan2-16/+29
MIPS64 little endian target has a "special" encoding of `r_info` relocation record field. Instead of one 64-bit little endian number, it is a little endian 32-bit number followed by a 32-bit big endian number. For correct reading and writing such fields we must provide information about target machine into the corresponding routine. This patch does this for the `llvm-objcopy` tool and fix handling of MIPS64 little endian files. The bug was reported in the issue #52647. Differential Revision: https://reviews.llvm.org/D115635
2021-11-16[llvm-objcopy] Add --update-sectionLeonard Chan3-15/+89
This is another attempt at D59351 which attempted to add --update-section, but with some heuristics for adjusting segment/section offsets/sizes in the event the data copied into the section is larger than the original size of the section. We are opting to not support this case. GNU's objcopy was able to do this because the linker and objcopy are tightly coupled enough that segment reformatting was simpler. This is not the case with llvm-objcopy and lld where they like to be separated. This will attempt to copy data into the section without changing any other properties of the parent segment (if the section is part of one). Differential Revision: https://reviews.llvm.org/D112116
2021-09-29[llvm-objcopy] Rename relocation sections together with their targets.Igor Kudrin1-1/+19
As for now, llvm-objcopy renames only sections that are specified explicitly in --rename-section, while GNU objcopy keeps names of relocation sections in sync with their targets. For example: > readelf -S test.o ... [ 1] .foo PROGBITS [ 2] .rela.foo RELA > objcopy --rename-section .foo=.bar test.o gnu.o > readelf -S gnu.o ... [ 1] .bar PROGBITS [ 2] .rela.bar RELA > llvm-objcopy --rename-section .foo=.bar test.o llvm.o > readelf -S llvm.o ... [ 1] .bar PROGBITS [ 2] .rela.foo RELA This patch makes llvm-objcopy to match the behavior of GNU objcopy better. Differential Revision: https://reviews.llvm.org/D110352
2021-09-24[llvm-objcopy][NFC] Add a helper method RelocationSectionBase::getNamePrefix()Igor Kudrin3-15/+17
Refactor handleArgs() to use that method. Differential Revision: https://reviews.llvm.org/D110350
2021-09-08[llvm-objcopy][NFC] Refactor CopyConfig structure - categorize options.Alexey Lapshin2-11/+23
This patch continues refactoring done by D99055. It puts format specific options into the correponding CopyConfig structures. Differential Revision: https://reviews.llvm.org/D102277
2021-08-12[llvm-objcopy][ELF] Avoid reordering section headersIgor Kudrin3-54/+76
As for now, llvm-objcopy sorts section headers according to the offsets of the sections in the input file. That can corrupt section references in the dynamic symbol table because it is a loadable section and as such is not updated by the tool. Even though the section references are not required for loading the binary correctly, they are still handy for a user who analyzes the file. While the patch removes global reordering of section headers, it layouts the sections in the same way as before, i.e. according to their original offsets. All that helps the output file to resemble the input better. Note that the patch removes sorting SHT_GROUP sections to the start of the list, which was introduced in D62620 in order to ensure that they come before the group members, along with the corresponding test. The original issue was caused by the sorting of section headers, so dropping the sorting also resolves the issue. Differential Revision: https://reviews.llvm.org/D107653
2021-08-09[llvm-objcopy][ELF][NFC] Remove unneeded methods of ObjectIgor Kudrin1-12/+4
The patch removes mutable accessor methods for sections and segments. As for now, const variants of them are not used because all callers have mutable access to an instance of Object. On the other hand, they do not actually modify the sets, so it looks better to keep only const ones. Differential Revision: https://reviews.llvm.org/D107652
2021-08-03[llvm-objcopy] IHexELFBuilder::addDataSections - fix evaluation ordering ↵Simon Pilgrim1-5/+8
static analyzer warning As detailed on https://pvs-studio.com/en/blog/posts/cpp/0771/ and raised on D62583, the SecNo++ increment is not guaranteed to occur before the second use of SecNo in the same addSection() call. This patch pulls out the increment (just for clarity) and replaces the second use of SecNo with a constant zero value (we're using stable_sort so the value isn't critical). Differential Revision: https://reviews.llvm.org/D107273
2021-07-26[llvm-objcopy] Fix section group flag read/write when operating on a ↵Fangrui Song1-2/+3
cross-endian object file
2021-07-26[llvm-objcopy] Drop GRP_COMDAT if the group signature is localizedFangrui Song1-0/+6
See [GRP_COMDAT group with STB_LOCAL signature](https://groups.google.com/g/generic-abi/c/2X6mR-s2zoc) objcopy PR: https://sourceware.org/bugzilla/show_bug.cgi?id=27931 GRP_COMDAT deduplication is purely based on the signature symbol name in ld.lld/GNU ld/gold. The local/global status is not part of the equation. If the signature symbol is localized by --localize-hidden or --keep-global-symbol, the intention is likely to make the group fully localized. Drop GRP_COMDAT to suppress deduplication. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D106782
2021-06-22[llvm-objcopy] Fix some namespace style issuesFangrui Song2-31/+31
https://llvm.org/docs/CodingStandards.html#use-namespace-qualifiers-to-implement-previously-declared-functions Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D104693
2021-06-16[llvm-objcopy] Make ihex writer similar to binary writerFangrui Song1-19/+2
There is no need to differentiate whether `UseSegments` is true or false. Unifying the cases makes the behavior closer to BinaryWriter. This improves compatibility with objcopy because SHF_ALLOC sections not in a PT_LOAD will not be skipped. Such cases are usually erroneous input, though. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D104186
2021-06-12llvm-objcopy: fix section size truncation/extension when dumping sectionsDavid Blaikie1-1/+1
Since this only comes up with inputs containing sections at least 4GB large (I guess I could use a bzero section or something, so the input file doesn't have to be 4GB, but even then the output file would have to be 4GB, right?) I've skipped testing this. If there's a nice way to test this without needing 4GB inputs or output files. The subtlety here is demonstrated by this code: struct t { operator uint64_t(); }; static_assert(std::is_same_v<int, decltype(std::declval<bool>() ? 0 : std::declval<t>())>); static_assert(std::is_same_v<uint64_t, decltype(std::declval<bool>() ? 0 : std::declval<uint64_t>())>); Because of this difference, the original source code was getting an int type (truncating the actual size) and then extending it again, resulting in bogus values (I haven't thought through this hard enough to explain why the resulting value was 0xffff... - sign extension, possible UB, but in any case it's the wrong answer - in this particular case I was looking at that resulted in a size so large that we couldn't open a file large enough to write to and ended up with a rather vague: error: 'file_name.o': Invalid argument
2021-06-12[llvm-objcopy] Exclude empty sections in IHexWriter outputIan McIntyre1-1/+2
IHexWriter was evaluating a section's physical address when deciding if that section should be written to an output. This approach does not account for a zero-sized section that has the same physical address as a sized section. The behavior varies from GNU objcopy, and may result in a HEX file that does not include all program sections. The IHexWriter now excludes zero-sized sections when deciding what should be written to the output. This affects the contents of the writer's `Sections` collection; we will not try to insert multiple sections that could have the same physical address. The behavior seems consistent with GNU objcopy, which always excludes empty sections, no matter the address. The new test case evaluates the IHexWriter behavior when provided a variety of empty sections that overlap or append a filled section. See the input file's comments for more information. Given that test input, and the change to the IHexWriter, GNU objcopy and llvm-objcopy produce the same output. Reviewed By: jhenderson, MaskRay, evgeny777 Differential Revision: https://reviews.llvm.org/D101332
2021-05-31[llvm-objcopy][NFC] Refactor CopyConfig structure - remove lazy options ↵Alexey Lapshin2-23/+60
processing. During reviewing D102277 it was decided to remove lazy options processing from llvm-objcopy CopyConfig structure. This patch transforms processing of ELF lazy options into the in-place processing. Differential Revision: https://reviews.llvm.org/D103260
2021-05-20[llvm-objcopy] Refactor CopyConfig structure.Alexey Lapshin5-170/+36
This patch prepares llvm-objcopy to move its implementation into a separate library. To make it possible it is necessary to minimize internal dependencies. Differential Revision: https://reviews.llvm.org/D99055
2021-05-12Fixed llvm-objcopy to add correct symbol table for ELF with program headers.Alex Orlov1-0/+4
This fixes the following bugs: https://bugs.llvm.org/show_bug.cgi?id=43935 Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D102258
2021-05-05[llvm-objcopy][ELF] --only-keep-debug: set offset/size of segments with no ↵Fangrui Song1-10/+11
sections to zero PR50160: we currently ignore non-PT_PHDR segments with no sections, not accounting for its p_offset and p_filesz: this can cause an out-of-bounds write in `writeSegmentData` if the p_offset+p_filesz is larger than the total file size. This can be fixed by setting p_offset=p_filesz=0. The logic nicely unifies with the logic added in D90897. Reviewed By: jhenderson, rupprecht Differential Revision: https://reviews.llvm.org/D101560
2021-03-18[llvm-objcopy] remove split dwo file creation from executeObjcopyOnBinary.Alexey Lapshin1-36/+11
This patch removes creation of the resulting file from the executeObjcopyOnBinary() function. For the most use cases, the executeObjcopyOnBinary receives output file as a parameter - raw_ostream &Out. The splitting .dwo file is implemented differently: file containg .dwo tables is created inside executeObjcopyOnBinary(). When objcopy functionality would be moved into separate library, current implementation will become inconvenient. The goal of that refactoring is to separate concerns: It might be convenient to to do dwo tables splitting but to create resulting file differently. Differential Revision: https://reviews.llvm.org/D98582
2021-03-10[llvm-objcopy][NFC] replace class Buffer/MemBuffer/FileBuffer with streams.Alexey Lapshin4-67/+107
During D88827 it was requested to remove the local implementation of Memory/File Buffers: // TODO: refactor the buffer classes in LLVM to enable us to use them here // directly. This patch uses raw_ostream instead of Buffers. Generally, using streams could allow us to reduce memory usages. No need to load all data into the memory - the data could be streamed through a smaller buffer. Thus, this patch uses raw_ostream as an interface for output data: Error executeObjcopyOnBinary(CopyConfig &Config, object::Binary &In, raw_ostream &Out); Note 1. This patch does not change the implementation of Writers so that data would be directly stored into raw_ostream. This is assumed to be done later. Note 2. It would be better if Writers would be implemented in a such way that data could be streamed without seeking/updating. If that would be inconvenient then raw_ostream could be replaced with raw_pwrite_stream to have a possibility to seek back and update file headers. This is assumed to be done later if necessary. Note 3. Current FileOutputBuffer allows using a memory-mapped file. The raw_fd_ostream (which could be used if data should be stored in the file) does not allow us to use a memory-mapped file. Memory map functionality could be implemented for raw_fd_ostream: It is possible to add resize() method into raw_ostream. class raw_ostream { void resize(uint64_t size); } That method, implemented for raw_fd_ostream, could create a memory-mapped file. The streamed data would be written into that memory file then. Thus we would be able to use memory-mapped files with raw_fd_ostream. This is assumed to be done later if necessary. Differential Revision: https://reviews.llvm.org/D91028
2021-03-08[llvm-objcopy][MachO] Add support for --keep-undefinedAlexander Shaposhnikov1-1/+1
This diff introduces --keep-undefined in llvm-objcopy/llvm-strip for Mach-O which makes the tools preserve undefined symbols. Test plan: make check-all Differential revision: https://reviews.llvm.org/D97040
2021-03-05[llvm-objcopy] Fix crash for binary input files with non-ascii namesJames Henderson1-2/+3
The code was using the standard isalnum function which doesn't handle values outside the non-ascii range. Switching to using llvm::isAlnum instead ensures we don't provoke undefined behaviour, which can in some cases result in crashes. Reviewed by: MaskRay Differential Revision: https://reviews.llvm.org/D97663
2021-03-04[llvm-objcopy/strip] Fix off-by-one error in SYMTAB_SHNDX need checkJames Henderson1-1/+3
The check for whether an extended symbol index table was required dropped the first SHN_LORESERVE sections from the sections array before checking whether the remaining sections had symbols. Unfortunately, the null section header is not present in this list, so the check was skipping the first section that might be important. If that section contained a symbol, and no subsequent ones did, the .symtab_shndx section would not be emitted, leading to a corrupt object. Also consolidate and expand test coverage in the area to cover this bug and other aspects of the SYMTAB_SHNDX section. Reviewed by: alexshap, MaskRay Differential Revision: https://reviews.llvm.org/D97661
2021-02-15[llvm-objcopy] Delete --build-id-link-{dir,input,output}Fangrui Song1-115/+0
The few options are niche. They solved a problem which was traditionally solved with more shell commands (`llvm-readelf -n` fetches the Build ID. Then `ln` is used to hard link the file to a directory derived from the Build ID.) Due to limitation, they are no longer used by Fuchsia and they don't appear to be used elsewhere (checked with Google Search and Debian Code Search). So delete them without a transition period. Announcement: https://lists.llvm.org/pipermail/llvm-dev/2021-February/148446.html Differential Revision: https://reviews.llvm.org/D96310
2021-02-01[llvm-objcopy] -O binary: consider SHT_NOBITS sections to be emptyPatrick Oppenlander1-1/+1
This is consistent with BFD objcopy. Previously llvm objcopy would allocate space for SHT_NOBITS sections often resulting in enormous binary files. New test case (binary-paddr.test %t6). Reviewed By: jhenderson, MaskRay Differential Revision: https://reviews.llvm.org/D95569
2021-01-19[llvm] Use llvm::any_of (NFC)Kazu Hirata1-2/+2
2020-12-04[lib/Object, tools] - Make ELFObjectFile::getELFFile return reference.Georgii Rymar2-5/+5
We always have an object, so we don't have to return a pointer. Differential revision: https://reviews.llvm.org/D92560
2020-11-11[llvm-objcopy][ELF] Try fixing non-determinism of Segment::firstSectionFangrui Song2-3/+5
2020-11-11[llvm-objcopy] --only-keep-debug: place zero-size segment according to its ↵Fangrui Song1-2/+9
parent segment Alternative to D74755. sectionWithinSegment() treats an empty section as having a size of 1. Due to the rule, an empty .tdata will not be attributed to an empty PT_TLS. (The empty p_align=64 PT_TLS is for Android Bionic's TCB compatibility (ELF-TLS). See https://reviews.llvm.org/D62055#1507426) Currently --only-keep-debug will not layout a segment with no section (layoutSegmentsForOnlyKeepDebug()), thus p_offset of PT_TLS can go past the end of the file. The strange p_offset can trigger validation errors for subsequent tools, e.g. llvm-objcopy errors when reading back the separate debug file (readProgramHeaders()). This patch places such an empty segment according to its parent segment. This special cases works for the empty PT_TLS used in Android. For a non-empty segment, it should have at least one non-empty section and will be handled by the normal code. Note, p_memsz PT_LOAD is rejected by both Linux and FreeBSD. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D90897
2020-11-04[llvm-objcopy] Make --set-section-flags work with --add-sectionFangrui Song1-10/+11
This matches behavior GNU objcopy and can simplify clang-offload-bundler (which currently works around the issue by invoking llvm-objcopy twice). Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D90438
2020-10-06[llvm-objcopy][NFC] fix style issues reported by clang-format.Alexey Lapshin3-32/+32
2020-10-02[llvm-objcopy][NFC] refactor error handling. part 3.Alexey Lapshin3-434/+756
Remove usages of special error reporting functions(error(), reportError()). Errors are reported as Expected<>/Error returning values. This part is for ELF subfolder of llvm-objcopy. Testing: check-all. Differential Revision: https://reviews.llvm.org/D87987
2020-09-15[lib/Object] - Refine interface of ELFFile<ELFT>. NFCI.Georgii Rymar1-15/+15
`ELFFile<ELFT>` has many methods that take pointers, though they assume that arguments are never null and hence could take references instead. This patch performs such clean-up. Differential revision: https://reviews.llvm.org/D87385
2020-07-05[llvm-objcopy] Fix crash when removing symbol table at same time as adding a ↵Georgy Komarov3-21/+35
symbol This patch resolves crash that occurs when user wanted to remove all symbols and add a brand new one using: ``` llvm-objcopy -R .symtab --add-symbol foo=1234 in.o out.o ``` Before these changes the symbol table internally being null when adding new symbols. For now we will regenerate symtab in this case. This fixes: https://bugs.llvm.org/show_bug.cgi?id=43930 Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D82935
2020-07-02Fix typo and check commit access.Sameer Arora1-2/+2
2020-06-29[llvm-objcopy] Emit error if removing symtab referenced by group sectionGeorgy Komarov1-14/+26
SHT_GROUP sections contain a reference to a symbol indicating their "signature" symbol. The symbol table containing this symbol is referred to by the group section's sh_link field. If llvm-objcopy is instructed to remove the symbol table, it will emit an error. This fixes https://bugs.llvm.org/show_bug.cgi?id=46153. Reviewed By: jhenderson, Higuoxing Differential Revision: https://reviews.llvm.org/D82274
2020-06-24[Hexagon][llvm-objcopy] Add missing check for SHN_HEXAGON_SCOMMON_1Sid Manning1-0/+1
Differential Revision: https://reviews.llvm.org/D82484
2020-06-05[llvm-objcopy] Reorder --dump-section before --remove-section for ELFSameer Arora1-11/+12
Reorder `DumpSection` under `handleArgs` in file `ELFObjcopy.cpp`. `DumpSection` is placed before `replaceAndRemoveSections` and is therefore now the first operation under `handleArgs`. Thus, it is now performed before both `add` and `remove` section operations. Change for the MachO format at D81123. Together fixes https://bugs.llvm.org/show_bug.cgi?id=44283. Reviewed By: alexshap, jhenderson, MaskRay Differential Revision: https://reviews.llvm.org/D81097
2020-05-29[llvm-objcopy][ELF] Fix removing a group member.Igor Kudrin2-1/+12
When a group member is removed, the corresponding record in the SHT_GROUP section has to be deleted. This fixes PR46064. Differential Revision: https://reviews.llvm.org/D80568
2020-05-29[llvm-objcopy][ELF] Fix removing SHT_GROUP sections.Igor Kudrin2-0/+12
When a SHT_GROUP section is removed, but other sections of the group are kept, the SHF_GROUP flag of these sections should be dropped, otherwise the resulting ELF file will be malformed. Differential Revision: https://reviews.llvm.org/D80511
2020-05-28SymbolicFile.h - removed unused FileSystem.h include. NFC.Simon Pilgrim1-1/+1
Exposes a number of implicit dependencies that needs fixing in source files and XCOFFObjectFile.h.
2020-05-26[llvm-objcopy][MachO] Add support for removing Swift symbolsAlexander Shaposhnikov1-1/+3
cctools strip has the option "-T" which removes Swift symbols. This diff implements this option in llvm-strip for MachO. Test plan: make check-all Differential revision: https://reviews.llvm.org/D80099
2020-05-05[llvm-objcopy][ELF] Allow --dump-section to dump an empty non-SHT_NOBITS sectionFangrui Song1-1/+1
This is the ELF part of D75949. GNU objcopy from binutils 2.35 onwards will support an empty non-SHT_NOBITS section as well https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=e052e2ba295a65b6ea80cbc3f90495beca299c42 Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D79339
2020-05-03[llvm-objcopy] Avoid invalid Sec.Offset after D79229Fangrui Song1-4/+4
To avoid undefined behavior caught by -fsanitize=undefined on binary-paddr.test void SectionWriter::visit(const Section &Sec) { if (Sec.Type != SHT_NOBITS) // Sec.Contents is empty while Sec.Offset may be out of bound llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset); }