aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/ELFObjectWriter.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-08-16MCSymbol: Remove AMDGPU-specific Kind::TargetCommonFangrui Song1-4/+4
The SymContentsTargetCommon kind introduced by https://reviews.llvm.org/D61493 lackes significant and should be treated as a regular common symbol with a different section index. Update ELFObjectWriter to respect the specified section index. The new representation also works with Hexagon's SHN_HEXAGON_SCOMMON.
2025-08-03MCSymbolELF: Migrate away from classofFangrui Song1-3/+2
The object file format specific derived classes are used in context where the type is statically known. We don't use isa/dyn_cast and we want to eliminate MCSymbol::Kind in the base class.
2025-08-03MCSymbolELF: Migrate away from classofFangrui Song1-13/+14
The object file format specific derived classes are used in context where the type is statically known. We don't use isa/dyn_cast and we want to eliminate MCSymbol::Kind in the base class.
2025-07-26MC: Allocate initial fragment and define section symbol in changeSectionFangrui Song1-14/+1
Reland #150574 with a MCStreamer::changeSection change: In Mach-O, DWARF sections use Begin as a temporary label, requiring a label definition, unlike section symbols in other file formats. (Tested by dec978036ef1037753e7de5b78c978e71c49217b) --- 13a79bbfe583e1d8cc85d241b580907260065eb8 (2017) introduced fragment creation in MCContext for createELFSectionImpl, which was inappropriate. Fragments should only be created when using MCSteramer, not during `MCContext::get*Section` calls. `initMachOMCObjectFileInfo` defines multiple sections, some of which may not be used by the code generator. This caused symbol names matching these sections to be incorrectly marked as undefined (see https://reviews.llvm.org/D55173). The fragment code was later replicated in other file formats, such as WebAssembly (see https://reviews.llvm.org/D46561), XCOFF, and GOFF. This patch fixes the problem by moving initial fragment allocation from MCContext::createSection to MCStreamer::changeSection. While MCContext still creates a section symbol, the symbol is not attached to the initial fragment. In addition, * Move `emitLabel`/`setFragment` from `switchSection*` and overridden changeSection to `MCObjectStreamer::changeSection` for consistency. * De-virtualize `switchSectionNoPrint`. * test/CodeGen/XCore/section-name.ll now passes. XCore doesn't support MCObjectStreamer. I don't think the MCAsmStreamer output behavior change matters. Pull Request: https://github.com/llvm/llvm-project/pull/150574
2025-07-25Revert "MC: Allocate initial fragment and define section symbol in ↵dyung1-1/+14
changeSection" (#150736) Reverts llvm/llvm-project#150574 This is causing a test failure on AArch64 MacOS bots: https://lab.llvm.org/buildbot/#/builders/190/builds/24187
2025-07-25MCSectionELF: Remove classofFangrui Song1-6/+8
The object file format specific derived classes are used in context like MCStreamer and MCObjectTargetWriter where the type is statically known. We don't use isa/dyn_cast and we want to eliminate MCSection::SectionVariant in the base class.
2025-07-24MC: Allocate initial fragment and define section symbol in changeSectionFangrui Song1-14/+1
13a79bbfe583e1d8cc85d241b580907260065eb8 (2017) introduced fragment creation in MCContext for createELFSectionImpl, which was inappropriate. Fragments should only be created when using MCSteramer, not during `MCContext::get*Section` calls. `initMachOMCObjectFileInfo` defines multiple sections, some of which may not be used by the code generator. This caused symbol names matching these sections to be incorrectly marked as undefined (see https://reviews.llvm.org/D55173). The fragment code was later replicated in other file formats, such as WebAssembly (see https://reviews.llvm.org/D46561), XCOFF, and GOFF. This patch fixes the problem by moving initial fragment allocation from MCContext::createSection to MCStreamer::changeSection. While MCContext still creates a section symbol, the symbol is not attached to the initial fragment. In addition, move `emitLabel`/`setFragment` from `switchSection*` and overridden changeSection to `MCObjectStreamer::changeSection` for consistency. * test/CodeGen/XCore/section-name.ll now passes. XCore doesn't support MCObjectStreamer. I don't think the MCAsmStreamer output behavior change matters. Pull Request: https://github.com/llvm/llvm-project/pull/150574
2025-07-05MC: Remove llvm/MC/MCFixupKindInfo.hFangrui Song1-1/+0
The file used to define `MCFixupKindInfo`, a simple structure, which is now in MCAsmBackend.h.
2025-07-03MCAssembler: Optimize PCRel fixupsFangrui Song1-3/+1
* MCAssembler::evaluateFixup sets MCFixup::PCRel. * ELFObjectWriter retrieves the bit from the MCFixup argument.
2025-07-02ELFObjectWriter: Optimize isInSymtabFangrui Song1-22/+16
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 Song1-9/+19
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 Song1-1/+0
... 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 Song1-19/+9
2025-06-27MC: Reduce MCSymbolRefExpr::VK_None usesFangrui Song1-2/+3
2025-06-01MC: Clear some members in resetFangrui Song1-0/+2
2025-05-26MC: Allow .set to reassign non-MCConstantExpr expressionsFangrui Song1-2/+1
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 Song1-28/+21
2025-05-25MC: Rework .weakrefFangrui Song1-22/+20
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 Song1-1/+1
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 Song1-7/+0
2025-05-24ELFObjectWriter: Remove the MCContext argument from getRelocTypeFangrui Song1-1/+1
Additionally, swap MCFixup/MCValue order to match addReloc/recordRelocation.
2025-05-24MCObjectTargetWriter: Add getContext/reportError and use it for ELFFangrui Song1-5/+10
Prepare for removing MCContext from getRelocType functions.
2025-05-24ELFObjectWriter: Remove MCAssembler * argumentsFangrui Song1-22/+19
2025-05-24MCObjectWriter: Remove the MCAssembler argument from writeObjectFangrui Song1-7/+7
2025-05-24ELFObjectWriter: Simplify useSectionSymbol and writeSymbolFangrui Song1-14/+12
2025-05-24MC: Simplify recordRelocationFangrui Song1-8/+7
* 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 Song1-13/+15
2025-05-24MCObjectwriter: Add member variable MCAssembler * and simplify codeFangrui Song1-10/+9
2025-05-18MC: Move R_PPC64_TOC case to PowerPCAsmBackendFangrui Song1-8/+0
2025-05-18MC: Generalize RISCV/LoongArch handleAddSubRelocations and AVR ↵Fangrui Song1-6/+2
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 Song1-2/+2
2025-05-18ELFObjectWriter: SimplifyFangrui Song1-40/+37
2025-04-18MCFixup: Add isRelocation/isRelocRelocation helpersFangrui Song1-2/+2
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 Song1-3/+2
... 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 Song1-2/+1
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 Song1-3/+9
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 Song1-28/+20
2025-04-12Revert "ELFObjectWriter: Disable STT_SECTION adjustment for .reloc"Fangrui Song1-2/+3
This reverts commit 1c5961c0481b9c7421d38e3141d3c5a1e6084234. Inadvertently pushed.
2025-04-12ELFObjectWriter: Disable STT_SECTION adjustment for .relocFangrui Song1-3/+2
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 Song1-61/+29
2025-04-05[MC] Replace getSymA()->getSymbol() with getAddSym. NFCFangrui Song1-6/+5
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 Song1-0/+6
ELFObjectWriter::recordRelocation
2025-03-23MCValue: Simplify code with getSubSymFangrui Song1-2/+2
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 Song1-9/+0
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 Song1-15/+0
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 Song1-42/+0
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 Song1-5/+0
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 Song1-4/+0
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 Song1-52/+10
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 Song1-0/+89
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.