Skip to content

Commit 13a79bb

Browse files
committed
Change how we handle section symbols on ELF.
On ELF every section can have a corresponding section symbol. When in an assembly file we have .quad .text the '.text' refers to that symbol. The way we used to handle them is to leave .text an undefined symbol until the very end when the object writer would map them to the actual section symbol. The problem with that is that anything before the end would see an undefined symbol. This could result in bad diagnostics (test/MC/AArch64/label-arithmetic-diags-elf.s), or incorrect results when using the asm streamer (est/MC/Mips/expansion-jal-sym-pic.s). Fixing this will also allow using the section symbol earlier for setting sh_link of SHF_METADATA sections. This patch includes a few hacks to avoid changing our behaviour when handling conflicts between section symbols and other symbols. I reported pr31850 to track that. llvm-svn: 293936
1 parent bb8dcc6 commit 13a79bb

18 files changed

+103
-140
lines changed

llvm/include/llvm/MC/MCContext.h

+10-30
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ namespace llvm {
8383
/// Bindings of names to symbols.
8484
SymbolTable Symbols;
8585

86-
/// Sections can have a corresponding symbol. This maps one to the
87-
/// other.
88-
DenseMap<const MCSection *, MCSymbol *> SectionSymbols;
89-
9086
/// A mapping from a local label number and an instance count to a symbol.
9187
/// For example, in the assembly
9288
/// 1:
@@ -232,6 +228,13 @@ namespace llvm {
232228
MCSymbol *getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
233229
unsigned Instance);
234230

231+
MCSectionELF *createELFSectionImpl(StringRef Section, unsigned Type,
232+
unsigned Flags, SectionKind K,
233+
unsigned EntrySize,
234+
const MCSymbolELF *Group,
235+
unsigned UniqueID,
236+
const MCSectionELF *Associated);
237+
235238
public:
236239
explicit MCContext(const MCAsmInfo *MAI, const MCRegisterInfo *MRI,
237240
const MCObjectFileInfo *MOFI,
@@ -288,8 +291,6 @@ namespace llvm {
288291
/// \param Name - The symbol name, which must be unique across all symbols.
289292
MCSymbol *getOrCreateSymbol(const Twine &Name);
290293

291-
MCSymbolELF *getOrCreateSectionSymbol(const MCSectionELF &Section);
292-
293294
/// Gets a symbol that will be defined to the final stack offset of a local
294295
/// variable after codegen.
295296
///
@@ -340,43 +341,22 @@ namespace llvm {
340341

341342
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
342343
unsigned Flags) {
343-
return getELFSection(Section, Type, Flags, nullptr);
344-
}
345-
346-
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
347-
unsigned Flags, const char *BeginSymName) {
348-
return getELFSection(Section, Type, Flags, 0, "", BeginSymName);
344+
return getELFSection(Section, Type, Flags, 0, "");
349345
}
350346

351347
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
352348
unsigned Flags, unsigned EntrySize,
353349
const Twine &Group) {
354-
return getELFSection(Section, Type, Flags, EntrySize, Group, nullptr);
355-
}
356-
357-
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
358-
unsigned Flags, unsigned EntrySize,
359-
const Twine &Group, const char *BeginSymName) {
360-
return getELFSection(Section, Type, Flags, EntrySize, Group, ~0,
361-
BeginSymName);
362-
}
363-
364-
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
365-
unsigned Flags, unsigned EntrySize,
366-
const Twine &Group, unsigned UniqueID) {
367-
return getELFSection(Section, Type, Flags, EntrySize, Group, UniqueID,
368-
nullptr);
350+
return getELFSection(Section, Type, Flags, EntrySize, Group, ~0);
369351
}
370352

371353
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
372354
unsigned Flags, unsigned EntrySize,
373-
const Twine &Group, unsigned UniqueID,
374-
const char *BeginSymName);
355+
const Twine &Group, unsigned UniqueID);
375356

376357
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
377358
unsigned Flags, unsigned EntrySize,
378359
const MCSymbolELF *Group, unsigned UniqueID,
379-
const char *BeginSymName,
380360
const MCSectionELF *Associated);
381361

382362
/// Get a section with the provided group identifier. This section is

llvm/lib/MC/ELFObjectWriter.cpp

-16
Original file line numberDiff line numberDiff line change
@@ -371,22 +371,6 @@ uint64_t ELFObjectWriter::SymbolValue(const MCSymbol &Sym,
371371

372372
void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
373373
const MCAsmLayout &Layout) {
374-
// Section symbols are used as definitions for undefined symbols with matching
375-
// names. If there are multiple sections with the same name, the first one is
376-
// used.
377-
for (const MCSection &Sec : Asm) {
378-
const MCSymbol *Begin = Sec.getBeginSymbol();
379-
if (!Begin)
380-
continue;
381-
382-
const MCSymbol *Alias = Asm.getContext().lookupSymbol(Begin->getName());
383-
if (!Alias || !Alias->isUndefined())
384-
continue;
385-
386-
Renames.insert(
387-
std::make_pair(cast<MCSymbolELF>(Alias), cast<MCSymbolELF>(Begin)));
388-
}
389-
390374
// The presence of symbol versions causes undefined symbols and
391375
// versions declared with @@@ to be renamed.
392376
for (const MCSymbol &A : Asm.symbols()) {

llvm/lib/MC/MCContext.cpp

+42-31
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ void MCContext::reset() {
8080
MCSubtargetAllocator.DestroyAll();
8181
UsedNames.clear();
8282
Symbols.clear();
83-
SectionSymbols.clear();
8483
Allocator.Reset();
8584
Instances.clear();
8685
CompilationDir.clear();
@@ -124,18 +123,6 @@ MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
124123
return Sym;
125124
}
126125

127-
MCSymbolELF *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
128-
MCSymbol *&Sym = SectionSymbols[&Section];
129-
if (Sym)
130-
return cast<MCSymbolELF>(Sym);
131-
132-
StringRef Name = Section.getSectionName();
133-
auto NameIter = UsedNames.insert(std::make_pair(Name, false)).first;
134-
Sym = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
135-
136-
return cast<MCSymbolELF>(Sym);
137-
}
138-
139126
MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
140127
unsigned Idx) {
141128
return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
@@ -316,6 +303,38 @@ void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
316303
const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
317304
}
318305

306+
MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
307+
unsigned Flags, SectionKind K,
308+
unsigned EntrySize,
309+
const MCSymbolELF *Group,
310+
unsigned UniqueID,
311+
const MCSectionELF *Associated) {
312+
313+
MCSymbolELF *R;
314+
MCSymbol *&Sym = Symbols[Section];
315+
if (Sym && Sym->isUndefined()) {
316+
R = cast<MCSymbolELF>(Sym);
317+
} else {
318+
auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
319+
R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
320+
if (!Sym)
321+
Sym = R;
322+
}
323+
R->setBinding(ELF::STB_LOCAL);
324+
R->setType(ELF::STT_SECTION);
325+
R->setRedefinable(true);
326+
327+
auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
328+
Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
329+
330+
auto *F = new MCDataFragment();
331+
Ret->getFragmentList().insert(Ret->begin(), F);
332+
F->setParent(Ret);
333+
R->setFragment(F);
334+
335+
return Ret;
336+
}
337+
319338
MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
320339
unsigned Flags, unsigned EntrySize,
321340
const MCSymbolELF *Group,
@@ -325,9 +344,9 @@ MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
325344
std::tie(I, Inserted) =
326345
ELFRelSecNames.insert(std::make_pair(Name.str(), true));
327346

328-
return new (ELFAllocator.Allocate())
329-
MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
330-
EntrySize, Group, true, nullptr, Associated);
347+
return createELFSectionImpl(I->getKey(), Type, Flags,
348+
SectionKind::getReadOnly(), EntrySize, Group,
349+
true, Associated);
331350
}
332351

333352
MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
@@ -339,21 +358,19 @@ MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
339358

340359
MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
341360
unsigned Flags, unsigned EntrySize,
342-
const Twine &Group, unsigned UniqueID,
343-
const char *BeginSymName) {
361+
const Twine &Group, unsigned UniqueID) {
344362
MCSymbolELF *GroupSym = nullptr;
345363
if (!Group.isTriviallyEmpty() && !Group.str().empty())
346364
GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
347365

348366
return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
349-
BeginSymName, nullptr);
367+
nullptr);
350368
}
351369

352370
MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
353371
unsigned Flags, unsigned EntrySize,
354372
const MCSymbolELF *GroupSym,
355373
unsigned UniqueID,
356-
const char *BeginSymName,
357374
const MCSectionELF *Associated) {
358375
StringRef Group = "";
359376
if (GroupSym)
@@ -375,22 +392,16 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
375392
else
376393
Kind = SectionKind::getReadOnly();
377394

378-
MCSymbol *Begin = nullptr;
379-
if (BeginSymName)
380-
Begin = createTempSymbol(BeginSymName, false);
381-
382-
MCSectionELF *Result = new (ELFAllocator.Allocate())
383-
MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID,
384-
Begin, Associated);
395+
MCSectionELF *Result = createELFSectionImpl(
396+
CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
385397
Entry.second = Result;
386398
return Result;
387399
}
388400

389401
MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
390-
MCSectionELF *Result = new (ELFAllocator.Allocate())
391-
MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4,
392-
Group, ~0, nullptr, nullptr);
393-
return Result;
402+
return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
403+
SectionKind::getReadOnly(), 4, Group, ~0,
404+
nullptr);
394405
}
395406

396407
MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,

llvm/lib/MC/MCELFStreamer.cpp

+1-10
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,7 @@ void MCELFStreamer::ChangeSection(MCSection *Section,
148148
Asm.registerSymbol(*Grp);
149149

150150
this->MCObjectStreamer::ChangeSection(Section, Subsection);
151-
MCContext &Ctx = getContext();
152-
auto *Begin = cast_or_null<MCSymbolELF>(Section->getBeginSymbol());
153-
if (!Begin) {
154-
Begin = Ctx.getOrCreateSectionSymbol(*SectionELF);
155-
Section->setBeginSymbol(Begin);
156-
}
157-
if (Begin->isUndefined()) {
158-
Asm.registerSymbol(*Begin);
159-
Begin->setType(ELF::STT_SECTION);
160-
}
151+
Asm.registerSymbol(*Section->getBeginSymbol());
161152
}
162153

163154
void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {

llvm/lib/MC/MCObjectFileInfo.cpp

+13-15
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,9 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T) {
506506
COFFDebugTypesSection = nullptr;
507507

508508
// Debug Info Sections.
509-
DwarfAbbrevSection = Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
510-
"section_abbrev");
511-
DwarfInfoSection =
512-
Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0, "section_info");
509+
DwarfAbbrevSection =
510+
Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0);
511+
DwarfInfoSection = Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0);
513512
DwarfLineSection = Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0);
514513
DwarfFrameSection = Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0);
515514
DwarfPubNamesSection =
@@ -527,21 +526,21 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T) {
527526
DwarfARangesSection =
528527
Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0);
529528
DwarfRangesSection =
530-
Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0, "debug_range");
531-
DwarfMacinfoSection = Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS,
532-
0, "debug_macinfo");
529+
Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0);
530+
DwarfMacinfoSection =
531+
Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0);
533532

534533
// DWARF5 Experimental Debug Info
535534

536535
// Accelerator Tables
537536
DwarfAccelNamesSection =
538-
Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0, "names_begin");
537+
Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0);
539538
DwarfAccelObjCSection =
540-
Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0, "objc_begin");
541-
DwarfAccelNamespaceSection = Ctx->getELFSection(
542-
".apple_namespaces", ELF::SHT_PROGBITS, 0, "namespac_begin");
539+
Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0);
540+
DwarfAccelNamespaceSection =
541+
Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0);
543542
DwarfAccelTypesSection =
544-
Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0, "types_begin");
543+
Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0);
545544

546545
// Fission Sections
547546
DwarfInfoDWOSection =
@@ -556,11 +555,10 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T) {
556555
DwarfLineDWOSection =
557556
Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0);
558557
DwarfLocDWOSection =
559-
Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0, "skel_loc");
558+
Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0);
560559
DwarfStrOffDWOSection =
561560
Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0);
562-
DwarfAddrSection =
563-
Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0, "addr_sec");
561+
DwarfAddrSection = Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0);
564562

565563
// DWP Sections
566564
DwarfCUIndexSection =

llvm/lib/MC/MCParser/AsmParser.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -4417,6 +4417,7 @@ bool AsmParser::parseDirectiveComm(bool IsLocal) {
44174417
return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
44184418
"alignment, can't be less than zero");
44194419

4420+
Sym->redefineIfPossible();
44204421
if (!Sym->isUndefined())
44214422
return Error(IDLoc, "invalid symbol redefinition");
44224423

llvm/lib/MC/MCSymbolELF.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ enum {
4242

4343
void MCSymbolELF::setBinding(unsigned Binding) const {
4444
setIsBindingSet();
45+
if (getType() == ELF::STT_SECTION && Binding != ELF::STB_LOCAL)
46+
setType(ELF::STT_NOTYPE);
4547
unsigned Val;
4648
switch (Binding) {
4749
default:
@@ -93,6 +95,8 @@ unsigned MCSymbolELF::getBinding() const {
9395

9496
void MCSymbolELF::setType(unsigned Type) const {
9597
unsigned Val;
98+
if (Type == ELF::STT_SECTION && getBinding() != ELF::STB_LOCAL)
99+
return;
96100
switch (Type) {
97101
default:
98102
llvm_unreachable("Unsupported Binding");

llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1138,9 +1138,8 @@ inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix,
11381138
const MCSymbolELF *Group = FnSection.getGroup();
11391139
if (Group)
11401140
Flags |= ELF::SHF_GROUP;
1141-
MCSectionELF *EHSection =
1142-
getContext().getELFSection(EHSecName, Type, Flags, 0, Group,
1143-
FnSection.getUniqueID(), nullptr, &FnSection);
1141+
MCSectionELF *EHSection = getContext().getELFSection(
1142+
EHSecName, Type, Flags, 0, Group, FnSection.getUniqueID(), &FnSection);
11441143

11451144
assert(EHSection && "Failed to get the required EH section");
11461145

llvm/test/DebugInfo/Generic/2010-06-29-InlinedFnLocalVar.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; RUN: %llc_dwarf -O2 %s -o - | FileCheck %s
22
; Check struct X for dead variable xyz from inlined function foo.
33

4-
; CHECK: section_info
4+
; CHECK: .section .debug_info,"",@progbits
55
; CHECK: DW_TAG_structure_type
66
; CHECK-NEXT: DW_AT_name
77

llvm/test/DebugInfo/Generic/array.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata) nounwind readnone
2525
!7 = distinct !DILexicalBlock(line: 3, column: 12, file: !14, scope: !0)
2626
!8 = !DICompositeType(tag: DW_TAG_array_type, align: 32, file: !14, scope: !2, baseType: !5, elements: !9)
2727
!9 = !{!10}
28-
;CHECK: section_info:
28+
;CHECK: .section .debug_info,"",@progbits
2929
;CHECK: DW_TAG_subrange_type
3030
;CHECK-NEXT: DW_AT_type
3131
;CHECK-NOT: DW_AT_lower_bound

llvm/test/DebugInfo/Mips/InlinedFnLocalVar.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; RUN: llc -mtriple mips-linux-gnu -O2 %s -o - | FileCheck %s
22
; Check struct X for dead variable xyz from inlined function foo.
33

4-
; CHECK: section_info
4+
; CHECK: .section .debug_info,"",@progbits
55
; CHECK: DW_TAG_structure_type
66
; CHECK-NEXT: info_string
77

llvm/test/DebugInfo/PowerPC/tls-fission.ll

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
; DW_OP_GNU_push_tls_address
1414
; CHECK-NEXT: .byte 224
1515
; check that the expected TLS address description is the first thing in the debug_addr section
16-
; CHECK: debug_addr
17-
; CHECK-NEXT: .Laddr_sec:
16+
; CHECK: .section .debug_addr,"",@progbits
1817
; CHECK-NEXT: .quad tls@DTPREL+32768
1918

2019
source_filename = "test/DebugInfo/PowerPC/tls-fission.ll"

llvm/test/DebugInfo/X86/ref_addr_relocation.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
; CHECK: DW_TAG_variable
4747
; Make sure this is relocatable.
4848
; and test that we don't create the labels to emit a correct COFF relocation
49-
; ELF-ASM: .quad .Lsection_info+[[TYPE]] # DW_AT_type
49+
; ELF-ASM: .quad .debug_info+[[TYPE]] # DW_AT_type
5050
; COFF-ASM: .secrel32 .Lsection_info+[[TYPE]] # DW_AT_type
5151
; DARWIN-ASM2: .quad [[TYPE]] ## DW_AT_type
5252
; DARWIN-ASM4: .long [[TYPE]] ## DW_AT_type

0 commit comments

Comments
 (0)