aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/ELFObjectWriter.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-02-07[llvm][ELF] Separate out .dwo bytes written in stats (#126165)Arthur Eubanks1-2/+7
So we can distinguish between debug info sections written to .dwo files and those written to the object file.
2025-01-19[MC] Avoid repeated hash lookups (NFC) (#123502)Kazu Hirata1-1/+2
2024-11-15[MC] Remove unused includes (NFC) (#116317)Kazu Hirata1-6/+0
Identified with misc-include-cleaner.
2024-09-23[llvm][ELF] Add ELF header/section header table size statistics (#109345)Arthur Eubanks1-0/+7
Followup to #102363. This makes the `elf-object-writer.*Bytes` stats sum up to `assembler.ObjectBytes`.
2024-09-19[NFC][ELF] Rename some ELFWriter methods (#109332)Arthur Eubanks1-31/+34
More consistent casing + more accurate names.
2024-08-23[MIPS] Optimize sortRelocs for o32Fangrui Song1-2/+2
The o32 ABI specifies: > Each relocation type of R_MIPS_HI16 must have an associated R_MIPS_LO16 entry immediately following it in the list of relocations. [...] the addend AHL is computed as (AHI << 16) + (short)ALO In practice, the high-part and low-part relocations may not be adjacent in assembly files, requiring the assembler to reorder relocations. http://reviews.llvm.org/D19718 performed the reordering, but did not optimize for the common case where a %lo immediately follows its matching %hi. The quadratic time complexity could make sections with many relocations very slow to process. This patch implements the fast path, simplifies the code, and makes the behavior more similar to GNU assembler (for the .rel.mips_hilo_8b test). We also remove `OriginalSymbol`, removing overhead for other targets. Fix #104562 Pull Request: https://github.com/llvm/llvm-project/pull/104723
2024-08-18[MC] Remove ELFRelocationEntry::OriginalAddendFangrui Song1-11/+6
For MIPS's o32 ABI (REL), https://reviews.llvm.org/D19718 introduced `OriginalAddend` to find the matching R_MIPS_LO16 relocation for R_MIPS_GOT16 when STT_SECTION conversion is applicable. lw $2, %lo(local1) lui $2, %got(local1) However, we could just store the original `Addend` in `ELFRelocationEntry` and remove `OriginalAddend`. Note: The relocation ordering algorithm in https://reviews.llvm.org/D19718 is inefficient (#104562), which will be addressed by another patch.
2024-08-08[llvm][ELF] Add statistics on various section sizes (#102363)Arthur Eubanks1-2/+54
Useful with other infrastructure that consume LLVM statistics to get an idea of distribution of section sizes. The breakdown of various section types is subject to change, this is just an initial go at gather some sort of stats. Example stats compiling X86ISelLowering.cpp (-g1): ``` "elf-object-writer.AllocROBytes": 308268, "elf-object-writer.AllocRWBytes": 6240, "elf-object-writer.AllocTextBytes": 1659203, "elf-object-writer.DebugBytes": 3180386, "elf-object-writer.OtherBytes": 5862, "elf-object-writer.RelocationBytes": 2623440, "elf-object-writer.StrtabBytes": 228599, "elf-object-writer.SymtabBytes": 120336, "elf-object-writer.UnwindBytes": 85216, ```
2024-08-05[MC,ARM] Move SHF_ARM_PUECODE change for .text to ARMTargetELFStreamer::finishFangrui Song1-2/+0
and remove MCELFObjectWriter::addTargetSectionFlags.
2024-07-23ELFObjectWriter: Remove unneeded subclassesFangrui Song1-65/+35
2024-07-22MCAssembler: Move FileNames and CompilerVersion to MCObjectWriterFangrui Song1-1/+1
2024-07-22MCAssembler: Move Symvers to ELFObjectWriterFangrui Song1-1/+2
Similar to c473e75adeaf2998e4fb444b0bdbf2dd19312e50
2024-07-22MCAssmembler: Move ELFHeaderEFlags to ELFObjectWriterFangrui Song1-1/+2
Now that MCELFStreamer can access ELFObjectWriter (commit 70c52b62c5669993e341664a63bfbe5245e32884), we can move ELFHeaderEFlags there.
2024-07-22[MC] Export llvm::ELFObjectWriterFangrui Song1-64/+13
Similar to commit 28fcafb50274be2520117eacb0a886adafefe59d (2011) for MachObjectWriter and commit 9539a7796094ff5fb59d9c685140ea2e214b945c for WinCOFFObjectWriter. MCELFStreamer can now access ELFObjectWriter directly without adding ELF-specific markGnuAbi (https://reviews.llvm.org/D97976) and setOverrideABIVersion to MCObjectWriter. A few member variables have to be made public since we cannot use a friend declaration for ELFWriter.
2024-07-08[MC,ELF] Extract CREL encoder codeFangrui Song1-39/+10
The extracted ELFObjectWriter.cpp code will be reused by llvm-objcopy support (#97521).
2024-07-03[MC][ELF] Eliminate some hash maps from ELFObjectWriter (#97421)Alexis Engelke1-72/+48
Remove some maps. Mostly cleanup, only a slight performance win. - Replace SectionIndexMap with layout order: The section layout order is only used in MachO, so we can repurpose the field as section table index. - Store section offsets in MCSectionELF: No need for a map, and especially not a std::map. Direct access to the underlying (and easily modifyable) data structure is always faster. - Improve storage of groups: There's no point in having a DenseMap, the number of sections and groups are reasonably small to use vectors.
2024-07-01[MC] Remove unused MCAsmLayout declarations and includesFangrui Song1-1/+0
2024-07-01MCExpr::evaluateKnownAbsolute: replace the MCAsmLayout parameter with ↵Fangrui Song1-1/+1
MCAssembler and add a comment.
2024-07-01[MC] Remove the MCAsmLayout parameter from ↵Fangrui Song1-4/+2
MCObjectWriter::executePostLayoutBinding
2024-07-01[MC,llvm-readobj,yaml2obj] Support CREL relocation formatFangrui Song1-29/+88
CREL is a compact relocation format for the ELF object file format. This patch adds integrated assembler support (using the RELA form) available with `llvm-mc -filetype=obj -crel a.s -o a.o`. A dependent patch will add `clang -c -Wa,--crel,--allow-experimental-crel`. Also add llvm-readobj support (for both REL and RELA forms) to facilitate testing the assembler. Additionally, yaml2obj gains support for the RELA form to aid testing with llvm-readobj. We temporarily assign the section type code 0x40000020 from the generic range to `SHT_CREL`. We avoided using `SHT_LLVM_` or `SHT_GNU_` to avoid code churn and maintain broader applicability for interested psABIs. Similarly, `DT_CREL` is temporarily 0x40000026. LLVM will change the code and break compatibility. This is not an issue if all relocatable files using CREL are regenerated (aka no prebuilt relocatable files). Link: https://discourse.llvm.org/t/rfc-crel-a-compact-relocation-format-for-elf/77600 Pull Request: https://github.com/llvm/llvm-project/pull/91280
2024-07-01ELFObjectWriter: Use DenseMap+SmallVector. NFCFangrui Song1-2/+2
2024-07-01[MC] Remove the MCAsmLayout parameter from ELFObjectWriterFangrui Song1-29/+27
2024-07-01[MC] Remove the MCAsmLayout parameter from ↵Fangrui Song1-14/+13
MCObjectWriter::{writeObject,writeSectionData}
2024-06-30[MC] Remove the MCAsmLayout parameter from MCObjectWriter::recordRelocationFangrui Song1-7/+6
2024-06-30[MC] Start merging MCAsmLayout into MCAssemblerFangrui Song1-6/+5
Follow-up to 10c894cffd0f4bef21b54a43b5780240532e44cf. MCAsmLayout, introduced by ac8a95498a99eb16dff9d3d0186616645d200b6e (2010), provides APIs to compute fragment/symbol/section offsets. The separate class is cumbersome and passing it around has overhead. Let's remove it as the underlying implementation is tightly coupled with MCAsmLayout anyway. Some forwarders are added to ease migration.
2024-06-29[MC] Simplify isSymbolRefDifferenceFullyResolvedImpl overloads. NFCFangrui Song1-2/+1
The base implementation is simple. Just inline it.
2024-06-23[MC] Move ELFWriter::createMemtagRelocs to AArch64TargetELFStreamer::finishFangrui Song1-21/+0
Follow-up to https://reviews.llvm.org/D128958 * Move target-specific code away from the generic ELFWriter. * All sections should have been created before MCAssembler::layout. * Remove one `registerSection` use, which should be considered private to MCAssembler.
2024-06-23Revert "[MC] Move ELFWriter::createMemtagRelocs to ↵Fangrui Song1-0/+21
AArch64ELFStreamer::finishImpl" This reverts commit 9d63506ddc6d60e220d967eb11779114075d401d. There is a heap-use-after-free.
2024-06-23[MC] Move ELFWriter::createMemtagRelocs to AArch64ELFStreamer::finishImplFangrui Song1-21/+0
Follow-up to https://reviews.llvm.org/D128958 * Move target-specific code away from the generic ELFWriter. * All sections should have been created before MCAssembler::layout. * Remove one `registerSection` use, which should be considered private to MCAssembler.
2024-04-26[MC] Rename temporary symbols of empty name to ".L0 " (#89693)Fangrui Song1-0/+6
Temporary symbols generated for .eh_frame and .debug_line have an empty name, which appear in .symtab in the presence of RISC-V style linker relaxation and will not be discarded by ld/objcopy --discard-locals (-X). In contrast, GNU assembler's riscv port assigns a fake name ".L0 " (with a trailing space) to these symbols so that will be discarded by ld/objcopy --discard-locals. This patch matches the GNU behavior. Since Clang's RISC-V targets pass -X to ld, and GNU ld defaults to -X for RISC-V targets, these ".L0 " symbols will be discarded after linking by default, as expected by users. The llvm-symbolizer special case for RISC-V `SF_FormatSpecific` symbols https://reviews.llvm.org/D98669 needs to be adjusted. Note: `"":` in assembly currently crashes. Note: bolt tests used /usr/bin/clang before llvmorg-19-init-9532-g59bfc3106874. The revert llvmorg-19-init-9531-g28b55342e1a8 actually broke bolt/test/RISCV/fake-label-no-entry.c
2024-04-25Revert "[MC] Rename temporary symbols of empty name to ".L0 " (#89693)"Amir Ayupov1-6/+0
This reverts commit 96c45a7fa12619c3abd6b81effe4c80f0916b78b. Broke BOLT builders and all pre-merge testing: https://lab.llvm.org/buildbot/#/builders/244/builds/28097
2024-04-24[MC] Rename temporary symbols of empty name to ".L0 " (#89693)Fangrui Song1-0/+6
Temporary symbols generated for .eh_frame and .debug_line have an empty name, which appear in .symtab in the presence of RISC-V style linker relaxation and will not be discarded by ld/objcopy --discard-locals (-X). In contrast, GNU assembler's riscv port assigns a fake name ".L0 " (with a trailing space) to these symbols so that will be discarded by ld/objcopy --discard-locals. This patch matches the GNU behavior. Since Clang's RISC-V targets pass -X to ld, and GNU ld defaults to -X for RISC-V targets, these ".L0 " symbols will be discarded after linking by default, as expected by users. The llvm-symbolizer special case for RISC-V `SF_FormatSpecific` symbols https://reviews.llvm.org/D98669 needs to be adjusted. Note: `"":` in assembly currently crashes.
2024-04-25Revert "[MC] Rename temporary symbols of empty name to ".L0 "" (#90002)Mehdi Amini1-6/+0
Reverts llvm/llvm-project#89693 This broke the premerge bot (bolt tests failing)
2024-04-24[MC] Rename temporary symbols of empty name to ".L0 " (#89693)Fangrui Song1-0/+6
Temporary symbols generated for .eh_frame and .debug_line have an empty name, which appear in .symtab in the presence of RISC-V style linker relaxation and will not be discarded by ld/objcopy --discard-locals (-X). In contrast, GNU assembler's riscv port assigns a fake name ".L0 " (with a trailing space) to these symbols so that will be discarded by ld/objcopy --discard-locals. This patch matches the GNU behavior. Since Clang's RISC-V targets pass -X to ld, and GNU ld defaults to -X for RISC-V targets, these ".L0 " symbols will be discarded after linking by default, as expected by users. The llvm-symbolizer special case for RISC-V `SF_FormatSpecific` symbols https://reviews.llvm.org/D98669 needs to be adjusted. Note: `"":` in assembly currently crashes.
2024-03-27[MC] Simplify ELFObjectWriter. NFCFangrui Song1-19/+17
And fix `if (hasRelocationAddend())` to `usesRela` to properly treat SHT_LLVM_CALL_GRAPH_PROFILE as SHT_REL. The incorrect does not cause a problem because the synthesized SHT_LLVM_CALL_GRAPH_PROFILE has zero addends.
2024-03-23[MC] Refactor writeRelocations. NFCFangrui Song1-27/+33
MIPS is different and should better off use separate code.
2024-03-23[MC] Remove unnecessary reversal of relocations. NFCFangrui Song1-8/+1
Commit f44db24e1fd948c75c87aea017646f16553d3361 (2015) enabled this simplication.
2024-03-06[MC] Move CompressDebugSections/RelaxELFRelocations from ↵Fangrui Song1-5/+4
TargetOptions/MCAsmInfo to MCTargetOptions The convention is for such MC-specific options to reside in MCTargetOptions. However, CompressDebugSections/RelaxELFRelocations do not follow the convention: `CompressDebugSections` is defined in both TargetOptions and MCAsmInfo and there is forwarding complexity. Move the option to MCTargetOptions and hereby simplify the code. Rename the misleading RelaxELFRelocations to X86RelaxRelocations. llvm-mc -relax-relocations and llc -x86-relax-relocations can now be unified.
2024-01-21[AMDGPU] Add an asm directive to track code_object_version (#76267)Emma Pilkington1-1/+10
Named '.amdhsa_code_object_version'. This directive sets the e_ident[ABIVERSION] in the ELF header, and should be used as the assumed COV for the rest of the asm file. This commit also weakens the --amdhsa-code-object-version CL flag. Previously, the CL flag took precedence over the IR flag. Now the IR flag/asm directive take precedence over the CL flag. This is implemented by merging a few COV-checking functions in AMDGPUBaseInfo.h.
2023-12-11[llvm] Use StringRef::{starts,ends}_with (NFC) (#74956)Kazu Hirata1-6/+6
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-11-17[MC] Fix compression header size check in ELF writerFangrui Song1-1/+1
This is #66888 with a test. For MC we only use a zstd test, as zlib has a lot of versions/forks with different speed/size tradeoff, which would make the test more brittle. If clang/test/Misc/cc1as-compress.s turns out to be brittle, we could make the string longer.
2023-11-06Revert "Fix compression header size check in ELF writer (#66888)"Hans Wennborg1-1/+1
This broke lit tests in zstd enabled builds, see comment on the PR. > The test had 32-bit and 64-bit header sizes the wrong way around. This reverts commit c5ecf5a130f087f493802800f3565c7bb75c238a.
2023-11-05Fix compression header size check in ELF writer (#66888)myxoid1-1/+1
The test had 32-bit and 64-bit header sizes the wrong way around.
2023-10-12Use llvm::endianness::{big,little,native} (NFC)Kazu Hirata1-4/+5
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-08-29[MC,AArch64] Suppress local symbol to STT_SECTION conversion for GOT relocationsFangrui Song1-5/+5
Assemblers change certain relocations referencing a local symbol to reference the section symbol instead. This conversion is disabled for many conditions (`shouldRelocateWithSymbol`), e.g. TLS symbol, for most targets (including AArch32, x86, PowerPC, and RISC-V) GOT-generating relocations. However, AArch64 encodes the GOT-generating intent in MCValue::RefKind instead of MCSymbolRef::Kind (see commit 0999cbd0b9ed8aa893cce10d681dec6d54b200ad (2014)), therefore not affected by the code `case MCSymbolRefExpr::VK_GOT:`. As GNU ld and ld.lld create GOT entries based on the symbol, ignoring addend, the two ldr instructions will share the same GOT entry, which is not expected: ``` ldr x1, [x1, :got_lo12:x] // converted to .data+0 ldr x1, [x1, :got_lo12:y] // converted to .data+4 .data // .globl x, y would suppress STT_SECTION conversion x: .zero 4 y: .long 42 ``` This patch changes AArch64 to suppress local symbol to STT_SECTION conversion for GOT relocations, matching most other targets. x and y will use different GOT entries, which IMO is the most sensable behavior. With this change, the ABI decision on https://github.com/ARM-software/abi-aa/issues/217 will only affect relocations explicitly referencing STT_SECTION symbols, e.g. ``` ldr x1, [x1, :got_lo12:(.data+0)] ldr x1, [x1, :got_lo12:(.data+4)] // I consider this unreasonable uses ``` IMO all reasonable use cases are unaffected. Link: https://github.com/llvm/llvm-project/issues/63418 GNU assembler PR: https://sourceware.org/bugzilla/show_bug.cgi?id=30788 Reviewed By: peter.smith Differential Revision: https://reviews.llvm.org/D158577
2023-06-29Revert "[MC,x86-32] Remove a gold<2.34 workaround"Arthur Eubanks1-0/+6
This reverts commit a699921baa91e6c2979ec0f0482430c57f51761d. Seems to cause miscompiles (https://crbug.com/1459232), following up with author.
2023-06-25[llvm] Add missing StringExtras.h includesElliot Goodrich1-0/+1
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-22[MC,x86-32] Remove a gold<2.34 workaroundFangrui Song1-6/+0
This workaround appears to apply with gold<2.34 -O2/-O3 (linker -O2, not compiler driver -O2). This used to be more visible as we used -Wl,-O3 in CMake, but the option is generally not recommended and has been removed by d63016a86548e8231002a760bbe9eb817cd1eb00 (Dec 2021). This finishes a workaround removal work started by D64327 (2019). Link: https://github.com/llvm/llvm-project/issues/45269
2023-04-23[Coverity] Fix uninitialized scalar members in MCAkshay Khadse1-3/+3
This change fixes static code analysis errors Reviewed By: pengfei Differential Revision: https://reviews.llvm.org/D148814
2023-02-10[NFC][TargetParser] Replace uses of llvm/Support/Host.hArchibald Elliott1-1/+1
The forwarding header is left in place because of its use in `polly/lib/External/isl/interface/extract_interface.cc`, but I have added a GCC warning about the fact it is deprecated, because it is used in `isl` from where it is included by Polly.