summaryrefslogtreecommitdiff
path: root/llvm/lib/MC/ELFObjectWriter.cpp
AgeCommit message (Collapse)Author
2025-07-03MCAssembler: Optimize PCRel fixupsFangrui Song
* MCAssembler::evaluateFixup sets MCFixup::PCRel. * ELFObjectWriter retrieves the bit from the MCFixup argument.
2025-07-02ELFObjectWriter: Optimize isInSymtabFangrui Song
Drop `OWriter.Renames.count(&Symbol)` from the fast path (Used||Signature). Place the two equated symbol (isVariable()) conditions together.
2025-07-02Revert "ELFObjectWriter: Optimize isInSymtab"Fangrui Song
This reverts commit 1108cf64196a056aa350baba98e3fab6d7529a59. Caused a regression for a weird but interesting case (STT_SECTION symbol as group signature). We no longer define `sec` ``` .section sec,"ax" .section .foo,"axG",@progbits,sec nop ``` Fix #146581
2025-06-30MC: Merge MCFragment.h into MCSection.hFangrui Song
... due to their close relationship. MCSection's inline functions (e.g. iterator) access MCFragment, and we want MCFragment's inline functions to access MCSection similarly (#146307). Pull Request: https://github.com/llvm/llvm-project/pull/146315
2025-06-28ELFObjectWriter: Optimize isInSymtabFangrui Song
2025-06-27MC: Reduce MCSymbolRefExpr::VK_None usesFangrui Song
2025-06-01MC: Clear some members in resetFangrui Song
2025-05-26MC: Allow .set to reassign non-MCConstantExpr expressionsFangrui Song
GNU Assembler supports symbol reassignment via .set, .equ, or =. However, LLVM's integrated assembler only allows reassignment for MCConstantExpr cases, as it struggles with scenarios like: ``` .data .set x, 0 .long x // reference the first instance x = .-.data .long x // reference the second instance .set x,.-.data .long x // reference the third instance ``` Between two assignments binds, we cannot ensure that a reference binds to the earlier assignment. We use MCSymbol::IsUsed and other conditions to reject potentially unsafe reassignments, but certain MCConstantExpr uses could be unsafe as well. This patch enables reassignment by cloning the symbol upon reassignment and updating the symbol table. Existing references to the original symbol remain unchanged, and the original symbol is excluded from the emitted symbol table.
2025-05-25ELFObjectWriter: Simplify STT_SECTION adjustmentFangrui Song
2025-05-25MC: Rework .weakrefFangrui Song
Use a variable symbol without any specifier instead of VK_WEAKREF. Add code in ELFObjectWriter::executePostLayoutBinding to check whether the target should be made an undefined weak symbol. This change fixes several issues: * Unreferenced `.weakref alias, target` no longer creates an undefined `target`. * When `alias` is already defined, report an error instead of crashing. .weakref is specific to ELF. llvm-ml has reused the VK_WEAKREF name for a different concept. wasm incorrectly copied the ELF implementation. Remove it.
2025-05-24MCELFObjectTargetWriter::needsRelocateWithSymbol: Remove MCSymbol argumentFangrui Song
Replace MCSymbol argument with MCValue::AddSym. The minor difference in .weakref handling is negligible, as our implementation may not fully align with GAS, and .weakref is not used in practice.
2025-05-24ELFObjectWriter: Move Thumb-specific condition to ARMELFObjectWriterFangrui Song
2025-05-24ELFObjectWriter: Remove the MCContext argument from getRelocTypeFangrui Song
Additionally, swap MCFixup/MCValue order to match addReloc/recordRelocation.
2025-05-24MCObjectTargetWriter: Add getContext/reportError and use it for ELFFangrui Song
Prepare for removing MCContext from getRelocType functions.
2025-05-24ELFObjectWriter: Remove MCAssembler * argumentsFangrui Song
2025-05-24MCObjectWriter: Remove the MCAssembler argument from writeObjectFangrui Song
2025-05-24ELFObjectWriter: Simplify useSectionSymbol and writeSymbolFangrui Song
2025-05-24MC: Simplify recordRelocationFangrui Song
* Remove the MCAssembler * argument. Change subclasses to use MCAssembler *MCObjectWriter::Asm. * Remove pure specifier and add an empty implementation * Change MCFragment * to MCFragment &
2025-05-24MCObjectwriter: Add getContext and simplify codeFangrui Song
2025-05-24MCObjectwriter: Add member variable MCAssembler * and simplify codeFangrui Song
2025-05-18MC: Move R_PPC64_TOC case to PowerPCAsmBackendFangrui Song
2025-05-18MC: Generalize RISCV/LoongArch handleAddSubRelocations and AVR ↵Fangrui Song
shouldForceRelocation Introduce MCAsmBackend::addReloc to manage relocation appending. The default implementation uses shouldForceRelocation to check unresolved fixups and calls recordRelocation to append a relocation when needed. RISCV and LoongArch override addReloc to handle ADD/SUB relocations, with potential support for RELAX relocations in the future. AVR overrides addReloc to customize shouldForceRelocation behavior (#121498). applyFixup is moved into evaluateFixup.
2025-05-18ELFObjectWriter: Simplify R_PPC64_TOC special case. NFCFangrui Song
2025-05-18ELFObjectWriter: SimplifyFangrui Song
2025-04-18MCFixup: Add isRelocation/isRelocRelocation helpersFangrui Song
Add two helper functions to simplify checks for relocation types, replacing direct comparisons with FirstRelocationKind and FirstLiteralRelocationKind. Note: Some targets haven't utilized isRelocation yet. Also, update RelaxFixupKind to use 0 as the sentinel value.
2025-04-16ELFObjectWriter: Disable STT_SECTION adjustment for .relocFangrui Song
... to match GNU Assembler. This generalizes the SHT_LLVM_CALL_GRAPH_PROFILE special case (which uses .reloc with BFD_RELOC_NONE https://reviews.llvm.org/D104080). Targets that want STT_SECTION adjustment cannot use FirstLiteralRelocationKind derived fixup kinds. Depends on the fix of #135521 Pull Request: https://github.com/llvm/llvm-project/pull/135519
2025-04-15[LoongArch] Use FirstRelocationKind to remove ↵Fangrui Song
ELFObjectWriter::recordRelocation special case The current implementation of R_LARCH_SUB{8,16,32,64} and TLS relocation types relies on fixup kinds FirstLiteralRelocationKind + offset (originally intended for .reloc directives). While this is clever and prevents switch cases like ``` case fixup_...sub8: return ELF::R_LARCH_SUB8; ``` it needs revision. GNU Assembler treats .reloc directives differently from standard relocations, notably by skipping * Skipping STT_SECTION adjustments (when a referenced symbol is local and satisfies certain conditions, it can be redirected to a section symbol). * Skipping STT_TLS symbol type setting for TLS relocations. Encode relocatin type t with FirstRelocationKind+t instead of FirstLiteralRelocationKind+t. The new value is less than FirstLiteralRelocationKind and will not be treated as a .reloc directive. Close #135521
2025-04-12ELFObjectWriter: Make .reloc test genericFangrui Song
Move `Fixup.getKind() >= FirstLiteralRelocationKind` from target hooks to ELFObjectWriter::recordRelocation. Currently, getRelocType cannot be skipped for LoongArch due to #135519
2025-04-12ELFObjectWriter: Simplify STT_SECTION adjustmentFangrui Song
2025-04-12Revert "ELFObjectWriter: Disable STT_SECTION adjustment for .reloc"Fangrui Song
This reverts commit 1c5961c0481b9c7421d38e3141d3c5a1e6084234. Inadvertently pushed.
2025-04-12ELFObjectWriter: Disable STT_SECTION adjustment for .relocFangrui Song
to match GNU Assembler. This generalizes the SHT_LLVM_CALL_GRAPH_PROFILE (which uses BFD_RELOC_NONE https://reviews.llvm.org/D104080) special case.
2025-04-12ELFObjectWriter: Simplify STT_SECTION adjustment. NFCFangrui Song
2025-04-05[MC] Replace getSymA()->getSymbol() with getAddSym. NFCFangrui Song
We will replace the MCSymbolRefExpr member in MCValue with MCSymbol. This change reduces dependence on MCSymbolRefExpr.
2025-03-29[MC] Move ELF-specific handleAddSubRelocations to ↵Fangrui Song
ELFObjectWriter::recordRelocation
2025-03-23MCValue: Simplify code with getSubSymFangrui Song
MCValue::SymB is a MCSymbolRefExpr *, which might become MCSymbol * in the future. Simplify some code that uses MCValue::SymB.
2025-03-23[MC] Move isMemtag test to AArch64Fangrui Song
And introduce MCValue::getAddSym & MCValue::getSubSym to simplify code. We do not utilize the MCSymbol argument of needsRelocateWithSymbol as it will go away in the future.
2025-03-23ELFObjectWriter: Remove relocation specifier test from shouldRelocateWithSymbolFangrui Song
It's the decision of backend needsRelocateWithSymbol whether the STT_SECTION adjustment should be suppressed. test/MC/AArch64/data-directive-specifier.s demonstrates how to test this property.
2025-03-22[MC] Remove ELFObjectWriter::fixSymbolsInTLSFixupsFangrui Song
Finish the migration started by eea7d32bd262bb5f61790c42ebaa147aa26c3979. STT_TLS setting has been moved to backend getRelocType. 75f5a4f0dc7d96134cca86543ef3f86ef218ce77 migrated the last target, VE.
2025-03-22Move SystemZ-specific MCSymbolRefExpr::VariantKind to SystemZMCExpr::SpecifierFangrui Song
Similar to previous migration done for other targets (PowerPC, X86, ARM, etc). Switch from the confusing VariantKind to Specifier, which aligns with Arm and IBM AIX's documentation. In addition, rename *MCExpr::getKind, which confusingly shadows the base class getKind. In the future, relocation specifiers should be encoded as part of SystemZMCExpr instead of MCSymbolRefExpr.
2025-03-20Move X86-specific MCSymbolRefExpr::VariantKind to X86MCExpr::SpecifierFangrui Song
Move target-specific members outside of MCSymbolRefExpr::VariantKind (a legacy interface I am eliminating). Most changes are mechanic, except: * ELFObjectWriter::shouldRelocateWithSymbol * The legacy generic code uses `ELFObjectWriter::fixSymbolsInTLSFixups` to set `STT_TLS` (and use an unnecessary expression walk). The better way is to do this in `getRelocType`, which I have done for AArch64, PowerPC, and RISC-V. In the future, we should encode expressions with a relocation specifier as X86MCExpr and use MCValue::RefKind to hold the specifier of the relocatable expression. https://maskray.me/blog/2025-03-16-relocation-generation-in-assemblers While here, rename "Modifier' to "Specifier": > "Relocation modifier", though concise, suggests adjustments happen during the linker's relocation step rather than the assembler's expression evaluation. I landed on "relocation specifier" as the winner. It's clear, aligns with Arm and IBM’s usage, and fits the assembler's role seamlessly. Pull Request: https://github.com/llvm/llvm-project/pull/132149
2025-03-12Move PowerPC-specific MCSymbolRefExpr::VariantKind to PPCMCExprFangrui Song
Most changes are mechanic, except: * ELFObjectWriter::shouldRelocateWithSymbol: .TOC.@tocbase does not register the undefined symbol. Move the handling into the Sym->isUndefined() code path. * ELFObjectWriter::fixSymbolsInTLSFixups's VK_PPC* cases are moved to PPCELFObjectWriter::getRelocType. We should do similar refactoring for other targets and eventually remove fixSymbolsInTLSFixups. In the future, we should classify PPCMCExpr similar to AArch64MCExpr.
2025-03-12[MC] Move fixSymbolsInTLSFixups to ELFObjectWriterFangrui Song
so that we only need to do it once during recordRelocation. In the future, we should change fixSymbolsInTLSFixups to apply to MCValue instead of MCExpr, similar to GNU assembler.
2025-02-07[llvm][ELF] Separate out .dwo bytes written in stats (#126165)Arthur Eubanks
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 Hirata
2024-11-15[MC] Remove unused includes (NFC) (#116317)Kazu Hirata
Identified with misc-include-cleaner.
2024-09-23[llvm][ELF] Add ELF header/section header table size statistics (#109345)Arthur Eubanks
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 Eubanks
More consistent casing + more accurate names.
2024-08-23[MIPS] Optimize sortRelocs for o32Fangrui Song
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 Song
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 Eubanks
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, ```