718 Commits

Author SHA1 Message Date
Fangrui Song
aa96e20dce MCSymbol: Remove AMDGPU-specific Kind::TargetCommon
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-16 15:39:33 -07:00
Fangrui Song
b51ff2705f MCSymbolELF: Migrate away from classof
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-03 16:05:35 -07:00
Fangrui Song
e2d761df32 MCSymbolELF: Migrate away from classof
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-03 15:05:22 -07:00
Fangrui Song
b22e22ebfa MC: Allocate initial fragment and define section symbol in changeSection
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-26 00:05:10 -07:00
dyung
cb37916a25
Revert "MC: Allocate initial fragment and define section symbol in 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-25 20:21:59 -07:00
Fangrui Song
2571924ad6 MCSectionELF: Remove classof
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-25 09:50:19 -07:00
Fangrui Song
1955a01d63
MC: Allocate initial fragment and define section symbol in changeSection
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-24 23:37:48 -07:00
Fangrui Song
244e053b6c MC: Remove llvm/MC/MCFixupKindInfo.h
The file used to define `MCFixupKindInfo`, a simple structure,
which is now in MCAsmBackend.h.
2025-07-05 11:24:11 -07:00
Fangrui Song
9234d07752 MCAssembler: Optimize PCRel fixups
* MCAssembler::evaluateFixup sets MCFixup::PCRel.
* ELFObjectWriter retrieves the bit from the MCFixup argument.
2025-07-03 00:01:53 -07:00
Fangrui Song
a25887077b ELFObjectWriter: Optimize isInSymtab
Drop `OWriter.Renames.count(&Symbol)` from the fast path
(Used||Signature).
Place the two equated symbol (isVariable()) conditions together.
2025-07-02 19:35:18 -07:00
Fangrui Song
9262ac3ee4 Revert "ELFObjectWriter: Optimize isInSymtab"
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-07-02 00:08:42 -07:00
Fangrui Song
04395be630
MC: Merge MCFragment.h into MCSection.h
... 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-30 09:41:53 -07:00
Fangrui Song
1108cf6419 ELFObjectWriter: Optimize isInSymtab 2025-06-28 17:36:14 -07:00
Fangrui Song
5aa3e6baa0 MC: Reduce MCSymbolRefExpr::VK_None uses 2025-06-27 21:46:36 -07:00
Fangrui Song
2a673078b2 MC: Clear some members in reset 2025-06-01 21:20:25 -07:00
Fangrui Song
e015626f18 MC: Allow .set to reassign non-MCConstantExpr expressions
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-26 21:58:18 -07:00
Fangrui Song
fe518e77d3 ELFObjectWriter: Simplify STT_SECTION adjustment 2025-05-25 21:41:02 -07:00
Fangrui Song
95756e67c2 MC: Rework .weakref
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-25 21:09:55 -07:00
Fangrui Song
e3e949cf5b MCELFObjectTargetWriter::needsRelocateWithSymbol: Remove MCSymbol argument
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-24 23:55:26 -07:00
Fangrui Song
63adf07555 ELFObjectWriter: Move Thumb-specific condition to ARMELFObjectWriter 2025-05-24 22:31:39 -07:00
Fangrui Song
fe32806d67 ELFObjectWriter: Remove the MCContext argument from getRelocType
Additionally, swap MCFixup/MCValue order to match addReloc/recordRelocation.
2025-05-24 21:48:11 -07:00
Fangrui Song
b65760bc7f MCObjectTargetWriter: Add getContext/reportError and use it for ELF
Prepare for removing MCContext from getRelocType functions.
2025-05-24 20:43:48 -07:00
Fangrui Song
7687261637 ELFObjectWriter: Remove MCAssembler * arguments 2025-05-24 20:01:00 -07:00
Fangrui Song
7ff0cf6138 MCObjectWriter: Remove the MCAssembler argument from writeObject 2025-05-24 12:55:52 -07:00
Fangrui Song
adaf170e08 ELFObjectWriter: Simplify useSectionSymbol and writeSymbol 2025-05-24 11:03:54 -07:00
Fangrui Song
e373f7a452 MC: Simplify recordRelocation
* 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-24 09:54:03 -07:00
Fangrui Song
2849b1282e MCObjectwriter: Add getContext and simplify code 2025-05-24 09:26:30 -07:00
Fangrui Song
a8433b88fa MCObjectwriter: Add member variable MCAssembler * and simplify code 2025-05-24 00:11:32 -07:00
Fangrui Song
6e857b49cc MC: Move R_PPC64_TOC case to PowerPCAsmBackend 2025-05-18 22:32:07 -07:00
Fangrui Song
c512d95186 MC: Generalize RISCV/LoongArch handleAddSubRelocations and AVR 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-18 18:08:35 -07:00
Fangrui Song
ec7a509165 ELFObjectWriter: Simplify R_PPC64_TOC special case. NFC 2025-05-18 14:56:55 -07:00
Fangrui Song
9d3ef8a66f ELFObjectWriter: Simplify 2025-05-18 14:18:20 -07:00
Fangrui Song
f9bd89b7ac MCFixup: Add isRelocation/isRelocRelocation helpers
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-18 18:18:31 -07:00
Fangrui Song
a630ef71e8
ELFObjectWriter: Disable STT_SECTION adjustment for .reloc
... 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-16 00:07:16 -07:00
Fangrui Song
ac4712482e [LoongArch] Use FirstRelocationKind to remove 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-15 23:55:49 -07:00
Fangrui Song
19730e37ec ELFObjectWriter: Make .reloc test generic
Move `Fixup.getKind() >= FirstLiteralRelocationKind` from target hooks
to ELFObjectWriter::recordRelocation.

Currently, getRelocType cannot be skipped for LoongArch due to #135519
2025-04-12 23:02:24 -07:00
Fangrui Song
734660b6dd ELFObjectWriter: Simplify STT_SECTION adjustment 2025-04-12 21:34:57 -07:00
Fangrui Song
3ff634dee8 Revert "ELFObjectWriter: Disable STT_SECTION adjustment for .reloc"
This reverts commit 1c5961c0481b9c7421d38e3141d3c5a1e6084234.
Inadvertently pushed.
2025-04-12 21:02:27 -07:00
Fangrui Song
1c5961c048 ELFObjectWriter: Disable STT_SECTION adjustment for .reloc
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-12 21:00:25 -07:00
Fangrui Song
329a675006 ELFObjectWriter: Simplify STT_SECTION adjustment. NFC 2025-04-12 20:54:05 -07:00
Fangrui Song
7ccdc3d5ca [MC] Replace getSymA()->getSymbol() with getAddSym. NFC
We will replace the MCSymbolRefExpr member in MCValue with MCSymbol.
This change reduces dependence on MCSymbolRefExpr.
2025-04-05 13:16:25 -07:00
Fangrui Song
db603a09da [MC] Move ELF-specific handleAddSubRelocations to ELFObjectWriter::recordRelocation 2025-03-29 19:08:07 -07:00
Fangrui Song
b73e144bdf MCValue: Simplify code with getSubSym
MCValue::SymB is a MCSymbolRefExpr *, which might become MCSymbol * in
the future. Simplify some code that uses MCValue::SymB.
2025-03-23 12:13:13 -07:00
Fangrui Song
83c3ec1b07 [MC] Move isMemtag test to AArch64
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-23 11:59:21 -07:00
Fangrui Song
c39d393038 ELFObjectWriter: Remove relocation specifier test from shouldRelocateWithSymbol
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-23 11:50:58 -07:00
Fangrui Song
8553fafff0 [MC] Remove ELFObjectWriter::fixSymbolsInTLSFixups
Finish the migration started by
eea7d32bd262bb5f61790c42ebaa147aa26c3979.
STT_TLS setting has been moved to backend getRelocType.
75f5a4f0dc7d96134cca86543ef3f86ef218ce77 migrated the last target, VE.
2025-03-22 20:10:31 -07:00
Fangrui Song
b19b6d9fab Move SystemZ-specific MCSymbolRefExpr::VariantKind to SystemZMCExpr::Specifier
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-22 18:05:40 -07:00
Fangrui Song
c177dbe484
Move X86-specific MCSymbolRefExpr::VariantKind to X86MCExpr::Specifier
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-20 20:28:49 -07:00
Fangrui Song
5d5f16204f Move PowerPC-specific MCSymbolRefExpr::VariantKind to PPCMCExpr
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 23:00:03 -07:00
Fangrui Song
eea7d32bd2 [MC] Move fixSymbolsInTLSFixups to ELFObjectWriter
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-03-12 19:49:52 -07:00