aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/MCContext.cpp
diff options
context:
space:
mode:
authorFangrui Song <i@maskray.me>2025-07-26 00:05:10 -0700
committerFangrui Song <i@maskray.me>2025-07-26 00:05:10 -0700
commitb22e22ebfa546cc31e166dd5ca4cc808aa057c9b (patch)
tree68e6826bb41b6ae37a1e415b72c31dc6e3647299 /llvm/lib/MC/MCContext.cpp
parent9475ed84909463ad594602dd76583f4ff12efcd0 (diff)
downloadllvm-b22e22ebfa546cc31e166dd5ca4cc808aa057c9b.zip
llvm-b22e22ebfa546cc31e166dd5ca4cc808aa057c9b.tar.gz
llvm-b22e22ebfa546cc31e166dd5ca4cc808aa057c9b.tar.bz2
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
Diffstat (limited to 'llvm/lib/MC/MCContext.cpp')
-rw-r--r--llvm/lib/MC/MCContext.cpp50
1 files changed, 9 insertions, 41 deletions
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index cf1ef31..39bf628 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -200,16 +200,6 @@ MCInst *MCContext::createMCInst() {
return new (MCInstAllocator.Allocate()) MCInst;
}
-// Allocate the initial MCFragment for the begin symbol.
-MCFragment *MCContext::allocInitialFragment(MCSection &Sec) {
- assert(!Sec.curFragList()->Head);
- auto *F = allocFragment<MCFragment>();
- F->setParent(&Sec);
- Sec.curFragList()->Head = F;
- Sec.curFragList()->Tail = F;
- return F;
-}
-
//===----------------------------------------------------------------------===//
// Symbol Manipulation
//===----------------------------------------------------------------------===//
@@ -443,17 +433,19 @@ MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
}
+// Create a section symbol, with a distinct one for each section of the same.
+// The first symbol is used for assembly code references.
template <typename Symbol>
Symbol *MCContext::getOrCreateSectionSymbol(StringRef Section) {
Symbol *R;
auto &SymEntry = getSymbolTableEntry(Section);
MCSymbol *Sym = SymEntry.second.Symbol;
- // A section symbol can not redefine regular symbols. There may be multiple
- // sections with the same name, in which case the first such section wins.
if (Sym && Sym->isDefined() &&
(!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
reportError(SMLoc(), "invalid symbol redefinition");
- if (Sym && Sym->isUndefined()) {
+ // Use the symbol's index to track if it has been used as a section symbol.
+ // Set to -1 to catch potential bugs if misused as a symbol index.
+ if (Sym && Sym->getIndex() != -1u) {
R = cast<Symbol>(Sym);
} else {
SymEntry.second.Used = true;
@@ -461,6 +453,8 @@ Symbol *MCContext::getOrCreateSectionSymbol(StringRef Section) {
if (!Sym)
SymEntry.second.Symbol = R;
}
+ // Mark as section symbol.
+ R->setIndex(-1u);
return R;
}
@@ -568,7 +562,6 @@ MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
MCSectionMachO(Segment, Name.substr(Name.size() - Section.size()),
TypeAndAttributes, Reserved2, Kind, Begin);
R.first->second = Ret;
- allocInitialFragment(*Ret);
return Ret;
}
@@ -579,15 +572,8 @@ MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
bool Comdat, unsigned UniqueID,
const MCSymbolELF *LinkedToSym) {
auto *R = getOrCreateSectionSymbol<MCSymbolELF>(Section);
- R->setBinding(ELF::STB_LOCAL);
- R->setType(ELF::STT_SECTION);
-
- auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
+ return new (ELFAllocator.Allocate()) MCSectionELF(
Section, Type, Flags, EntrySize, Group, Comdat, UniqueID, R, LinkedToSym);
-
- auto *F = allocInitialFragment(*Ret);
- R->setFragment(F);
- return Ret;
}
MCSectionELF *
@@ -743,7 +729,6 @@ MCSectionGOFF *MCContext::getGOFFSection(SectionKind Kind, StringRef Name,
MCSectionGOFF(CachedName, Kind, IsVirtual, Attributes,
static_cast<MCSectionGOFF *>(Parent));
Iter->second = GOFFSection;
- allocInitialFragment(*GOFFSection);
return GOFFSection;
}
@@ -798,8 +783,7 @@ MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
CachedName, Characteristics, COMDATSymbol, Selection, UniqueID, Begin);
Iter->second = Result;
- auto *F = allocInitialFragment(*Result);
- Begin->setFragment(F);
+ Begin->setFragment(&Result->getDummyFragment());
return Result;
}
@@ -870,8 +854,6 @@ MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
MCSectionWasm(CachedName, Kind, Flags, GroupSym, UniqueID, Begin);
Entry.second = Result;
- auto *F = allocInitialFragment(*Result);
- Begin->setFragment(F);
return Result;
}
@@ -927,24 +909,11 @@ MCSectionXCOFF *MCContext::getXCOFFSection(
MultiSymbolsAllowed);
Entry.second = Result;
-
- auto *F = allocInitialFragment(*Result);
-
- // We might miss calculating the symbols difference as absolute value before
- // adding fixups when symbol_A without the fragment set is the csect itself
- // and symbol_B is in it.
- // TODO: Currently we only set the fragment for XMC_PR csects and DWARF
- // sections because we don't have other cases that hit this problem yet.
- if (IsDwarfSec || CsectProp->MappingClass == XCOFF::XMC_PR)
- QualName->setFragment(F);
-
return Result;
}
MCSectionSPIRV *MCContext::getSPIRVSection() {
MCSectionSPIRV *Result = new (SPIRVAllocator.Allocate()) MCSectionSPIRV();
-
- allocInitialFragment(*Result);
return Result;
}
@@ -964,7 +933,6 @@ MCSectionDXContainer *MCContext::getDXContainerSection(StringRef Section,
new (DXCAllocator.Allocate()) MCSectionDXContainer(Name, K, nullptr);
// The first fragment will store the header
- allocInitialFragment(*MapIt->second);
return MapIt->second;
}