Skip to content

[MC,COFF] Change how we handle section symbols #96459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions llvm/include/llvm/MC/MCContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ class MCContext {
MCSymbol *getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
unsigned Instance);

template <typename Symbol>
Symbol *getOrCreateSectionSymbol(StringRef Section);

MCSectionELF *createELFSectionImpl(StringRef Section, unsigned Type,
unsigned Flags, unsigned EntrySize,
const MCSymbolELF *Group, bool IsComdat,
Expand Down Expand Up @@ -604,11 +607,9 @@ class MCContext {

MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics,
StringRef COMDATSymName, int Selection,
unsigned UniqueID = GenericSectionID,
const char *BeginSymName = nullptr);
unsigned UniqueID = GenericSectionID);

MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics,
const char *BeginSymName = nullptr);
MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics);

/// Gets or creates a section equivalent to Sec that is associated with the
/// section containing KeySym. For example, to create a debug info section
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/MC/MCWinCOFFStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MCWinCOFFStreamer : public MCObjectStreamer {
/// \{

void initSections(bool NoExecStack, const MCSubtargetInfo &STI) override;
void changeSection(MCSection *Section, uint32_t Subsection = 0) override;
void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
void emitAssemblerFlag(MCAssemblerFlag Flag) override;
void emitThumbFunc(MCSymbol *Func) override;
Expand Down
63 changes: 36 additions & 27 deletions llvm/lib/MC/MCContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,27 @@ MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
}

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()) {
R = cast<Symbol>(Sym);
} else {
SymEntry.second.Used = true;
R = new (&SymEntry, *this) Symbol(&SymEntry, /*isTemporary=*/false);
if (!Sym)
SymEntry.second.Symbol = R;
}
return R;
}

MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
SmallString<128> NameSV;
StringRef NameRef = Name.toStringRef(NameSV);
Expand Down Expand Up @@ -497,22 +518,7 @@ MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
const MCSymbolELF *Group,
bool Comdat, unsigned UniqueID,
const MCSymbolELF *LinkedToSym) {
MCSymbolELF *R;
MCSymbolTableEntry &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()) {
R = cast<MCSymbolELF>(Sym);
} else {
SymEntry.second.Used = true;
R = new (&SymEntry, *this) MCSymbolELF(&SymEntry, /*isTemporary*/ false);
if (!Sym)
SymEntry.second.Symbol = R;
}
auto *R = getOrCreateSectionSymbol<MCSymbolELF>(Section);
R->setBinding(ELF::STB_LOCAL);
R->setType(ELF::STT_SECTION);

Expand Down Expand Up @@ -681,12 +687,19 @@ MCSectionGOFF *MCContext::getGOFFSection(StringRef Section, SectionKind Kind,
MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
unsigned Characteristics,
StringRef COMDATSymName, int Selection,
unsigned UniqueID,
const char *BeginSymName) {
unsigned UniqueID) {
MCSymbol *COMDATSymbol = nullptr;
if (!COMDATSymName.empty()) {
COMDATSymbol = getOrCreateSymbol(COMDATSymName);
COMDATSymName = COMDATSymbol->getName();
// A non-associative COMDAT is considered to define the COMDAT symbol. Check
// the redefinition error.
if (Selection != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE && COMDATSymbol &&
COMDATSymbol->isDefined() &&
(!COMDATSymbol->isInSection() ||
cast<MCSectionCOFF>(COMDATSymbol->getSection()).getCOMDATSymbol() !=
COMDATSymbol))
reportError(SMLoc(), "invalid symbol redefinition");
}

// Do the lookup, if we have a hit, return it.
Expand All @@ -696,23 +709,19 @@ MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
if (!IterBool.second)
return Iter->second;

MCSymbol *Begin = nullptr;
if (BeginSymName)
Begin = createTempSymbol(BeginSymName, false);

StringRef CachedName = Iter->first.SectionName;
MCSymbol *Begin = getOrCreateSectionSymbol<MCSymbolCOFF>(Section);
MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
CachedName, Characteristics, COMDATSymbol, Selection, Begin);
Iter->second = Result;
allocInitialFragment(*Result);
auto *F = allocInitialFragment(*Result);
Begin->setFragment(F);
return Result;
}

MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
unsigned Characteristics,
const char *BeginSymName) {
return getCOFFSection(Section, Characteristics, "", 0, GenericSectionID,
BeginSymName);
unsigned Characteristics) {
return getCOFFSection(Section, Characteristics, "", 0, GenericSectionID);
}

MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
Expand Down
35 changes: 15 additions & 20 deletions llvm/lib/MC/MCObjectFileInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,24 +611,21 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
COFF::IMAGE_SCN_MEM_READ));

DwarfAbbrevSection = Ctx->getCOFFSection(
".debug_abbrev",
COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ,
"section_abbrev");
".debug_abbrev", COFF::IMAGE_SCN_MEM_DISCARDABLE |
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ);
DwarfInfoSection = Ctx->getCOFFSection(
".debug_info",
COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ,
"section_info");
".debug_info", COFF::IMAGE_SCN_MEM_DISCARDABLE |
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ);
DwarfLineSection = Ctx->getCOFFSection(
".debug_line", COFF::IMAGE_SCN_MEM_DISCARDABLE |
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ);
DwarfLineStrSection = Ctx->getCOFFSection(
".debug_line_str",
COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ,
"section_line_str");
".debug_line_str", COFF::IMAGE_SCN_MEM_DISCARDABLE |
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ);
DwarfFrameSection = Ctx->getCOFFSection(
".debug_frame", COFF::IMAGE_SCN_MEM_DISCARDABLE |
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
Expand Down Expand Up @@ -738,19 +735,17 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ);
DwarfAccelNamesSection = Ctx->getCOFFSection(
".apple_names",
COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ,
"names_begin");
".apple_names", COFF::IMAGE_SCN_MEM_DISCARDABLE |
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ);
DwarfAccelNamespaceSection = Ctx->getCOFFSection(
".apple_namespaces", COFF::IMAGE_SCN_MEM_DISCARDABLE |
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ);
DwarfAccelTypesSection = Ctx->getCOFFSection(
".apple_types",
COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ,
"types_begin");
".apple_types", COFF::IMAGE_SCN_MEM_DISCARDABLE |
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ);
DwarfAccelObjCSection = Ctx->getCOFFSection(
".apple_objc", COFF::IMAGE_SCN_MEM_DISCARDABLE |
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/MC/MCWinCOFFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ void MCWinCOFFStreamer::initSections(bool NoExecStack,
switchSection(getContext().getObjectFileInfo()->getTextSection());
}

void MCWinCOFFStreamer::changeSection(MCSection *Section, uint32_t Subsection) {
changeSectionImpl(Section, Subsection);
// Ensure that the first and the second symbols relative to the section are
// the section symbol and the COMDAT symbol.
getAssembler().registerSymbol(*Section->getBeginSymbol());
if (auto *Sym = cast<MCSectionCOFF>(Section)->getCOMDATSymbol())
getAssembler().registerSymbol(*Sym);
}

void MCWinCOFFStreamer::emitLabel(MCSymbol *S, SMLoc Loc) {
auto *Symbol = cast<MCSymbolCOFF>(S);
MCObjectStreamer::emitLabel(Symbol, Loc);
Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/MC/WinCOFFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ void WinCOFFWriter::defineSection(const MCSectionCOFF &MCSec,
COFFSection *Section = createSection(MCSec.getName());
COFFSymbol *Symbol = createSymbol(MCSec.getName());
Section->Symbol = Symbol;
SymbolMap[MCSec.getBeginSymbol()] = Symbol;
Symbol->Section = Section;
Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;

Expand Down Expand Up @@ -398,15 +399,18 @@ COFFSymbol *WinCOFFWriter::getLinkedSymbol(const MCSymbol &Symbol) {
/// and creates the associated COFF symbol staging object.
void WinCOFFWriter::DefineSymbol(const MCSymbol &MCSym, MCAssembler &Assembler,
const MCAsmLayout &Layout) {
COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
COFFSection *Sec = nullptr;
MCSectionCOFF *MCSec = nullptr;
if (Base && Base->getFragment()) {
Sec = SectionMap[Base->getFragment()->getParent()];
if (Sym->Section && Sym->Section != Sec)
report_fatal_error("conflicting sections for symbol");
MCSec = cast<MCSectionCOFF>(Base->getFragment()->getParent());
Sec = SectionMap[MCSec];
}

if (Mode == NonDwoOnly && MCSec && isDwoSection(*MCSec))
return;

COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
COFFSymbol *Local = nullptr;
if (cast<MCSymbolCOFF>(MCSym).getWeakExternalCharacteristics()) {
Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/DebugInfo/X86/InlinedFnLocalVar.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
; RUN: llc --try-experimental-debuginfo-iterators -mtriple i686-pc-cygwin -O2 %s -o - | FileCheck %s
; Check struct X for dead variable xyz from inlined function foo.

; CHECK: Lsection_info
; CHECK: .section .debug_info,"dr"
; CHECK: DW_TAG_structure_type
; CHECK-NEXT: info_string

Expand Down
2 changes: 1 addition & 1 deletion llvm/test/DebugInfo/X86/ref_addr_relocation.ll
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
; Make sure this is relocatable.
; and test that we don't create the labels to emit a correct COFF relocation
; ELF-ASM: .quad .debug_info+[[TYPE]] # DW_AT_type
; COFF-ASM: .secrel32 .Lsection_info+[[TYPE]] # DW_AT_type
; COFF-ASM: .secrel32 .debug_info+[[TYPE]] # DW_AT_type
; DARWIN-ASM2: .quad [[TYPE]] ## DW_AT_type
; DARWIN-ASM4: .long [[TYPE]] ## DW_AT_type
; CHECK-NOT: DW_TAG_structure_type
Expand Down
1 change: 0 additions & 1 deletion llvm/test/ExecutionEngine/RuntimeDyld/X86/COFF_x86_64.s
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


.section section,"rx"
section:
.long 0
Lreloc:
.long 0
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/MC/COFF/section-comdat-conflict.s
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: not --crash llvm-mc -triple i386-pc-win32 -filetype=obj < %s 2>&1 | FileCheck %s
// RUN: not llvm-mc -triple i386-pc-win32 -filetype=obj < %s 2>&1 | FileCheck %s

// CHECK: conflicting sections for symbol
// CHECK: <unknown>:0: error: invalid symbol redefinition

.section .xyz
.global bar
Expand Down
8 changes: 4 additions & 4 deletions llvm/test/MC/COFF/section-comdat.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
.section assocSec, "dr", discard, "assocSym"
.global assocSym
assocSym:
.long 1
.long assocSec

.section secName, "dr", discard, "Symbol1"
.globl Symbol1
Symbol1:
.long 1
.long assocSym

.section secName, "dr", one_only, "Symbol2"
.globl Symbol2
Expand Down Expand Up @@ -68,10 +68,10 @@ Symbol8:
# CHECK-NEXT: [ 4](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .bss
# CHECK-NEXT: AUX scnlen 0x0 nreloc 0 nlnno 0 checksum 0x0 assoc 3 comdat 0
# CHECK-NEXT: [ 6](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 assocSec
# CHECK-NEXT: AUX scnlen 0x4 nreloc 0 nlnno 0 checksum 0xb8bc6765 assoc 4 comdat 2
# CHECK-NEXT: AUX scnlen 0x4 nreloc 1 nlnno 0 checksum 0x0 assoc 4 comdat 2
# CHECK-NEXT: [ 8](sec 4)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 assocSym
# CHECK-NEXT: [ 9](sec 5)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 secName
# CHECK-NEXT: AUX scnlen 0x4 nreloc 0 nlnno 0 checksum 0xb8bc6765 assoc 5 comdat 2
# CHECK-NEXT: AUX scnlen 0x4 nreloc 1 nlnno 0 checksum 0x0 assoc 5 comdat 2
# CHECK-NEXT: [11](sec 5)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 Symbol1
# CHECK-NEXT: [12](sec 6)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 secName
# CHECK-NEXT: AUX scnlen 0x4 nreloc 0 nlnno 0 checksum 0xb8bc6765 assoc 6 comdat 1
Expand Down
7 changes: 7 additions & 0 deletions llvm/test/MC/COFF/section-sym-err.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# RUN: not llvm-mc -filetype=obj -triple x86_64-windows-gnu %s -o %t.o 2>&1 | FileCheck %s --implicit-check-not=error:

.section foo0,"xr",discard,foo1
foo0:
foo1:

# CHECK: error: symbol 'foo0' is already defined
Loading