Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit c67a9ef

Browse files
committed
Fix asm printing of associated sections.
Make MCSectionELF::AssociatedSection be a link to a symbol, because that's how it works in the assembly, and use it in the asm printer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@297769 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 1526258 commit c67a9ef

File tree

8 files changed

+48
-30
lines changed

8 files changed

+48
-30
lines changed

include/llvm/MC/MCContext.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ namespace llvm {
262262
unsigned EntrySize,
263263
const MCSymbolELF *Group,
264264
unsigned UniqueID,
265-
const MCSectionELF *Associated);
265+
const MCSymbolELF *Associated);
266266

267267
public:
268268
explicit MCContext(const MCAsmInfo *MAI, const MCRegisterInfo *MRI,
@@ -393,12 +393,12 @@ namespace llvm {
393393
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
394394
unsigned Flags, unsigned EntrySize,
395395
const Twine &Group, unsigned UniqueID,
396-
const MCSectionELF *Associated);
396+
const MCSymbolELF *Associated);
397397

398398
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
399399
unsigned Flags, unsigned EntrySize,
400400
const MCSymbolELF *Group, unsigned UniqueID,
401-
const MCSectionELF *Associated);
401+
const MCSymbolELF *Associated);
402402

403403
/// Get a section with the provided group identifier. This section is
404404
/// named by concatenating \p Prefix with '.' then \p Suffix. The \p Type
@@ -411,7 +411,7 @@ namespace llvm {
411411
MCSectionELF *createELFRelSection(const Twine &Name, unsigned Type,
412412
unsigned Flags, unsigned EntrySize,
413413
const MCSymbolELF *Group,
414-
const MCSectionELF *Associated);
414+
const MCSectionELF *RelInfoSection);
415415

416416
void renameELFSection(MCSectionELF *Section, StringRef Name);
417417

include/llvm/MC/MCSectionELF.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ class MCSectionELF final : public MCSection {
4545

4646
const MCSymbolELF *Group;
4747

48-
/// Depending on the type of the section this is sh_link or sh_info.
49-
const MCSectionELF *Associated;
48+
/// sh_info for SHF_LINK_ORDER (can be null).
49+
const MCSymbol *AssociatedSymbol;
5050

5151
private:
5252
friend class MCContext;
5353

5454
MCSectionELF(StringRef Section, unsigned type, unsigned flags, SectionKind K,
5555
unsigned entrySize, const MCSymbolELF *group, unsigned UniqueID,
56-
MCSymbol *Begin, const MCSectionELF *Associated)
56+
MCSymbol *Begin, const MCSymbolELF *AssociatedSymbol)
5757
: MCSection(SV_ELF, K, Begin), SectionName(Section), Type(type),
5858
Flags(flags), UniqueID(UniqueID), EntrySize(entrySize), Group(group),
59-
Associated(Associated) {
59+
AssociatedSymbol(AssociatedSymbol) {
6060
if (Group)
6161
Group->setIsSignature();
6262
}
@@ -86,7 +86,8 @@ class MCSectionELF final : public MCSection {
8686
bool isUnique() const { return UniqueID != ~0U; }
8787
unsigned getUniqueID() const { return UniqueID; }
8888

89-
const MCSectionELF *getAssociatedSection() const { return Associated; }
89+
const MCSection *getAssociatedSection() const { return &AssociatedSymbol->getSection(); }
90+
const MCSymbol *getAssociatedSymbol() const { return AssociatedSymbol; }
9091

9192
static bool classof(const MCSection *S) {
9293
return S->getVariant() == SV_ELF;

lib/MC/ELFObjectWriter.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,8 +1156,8 @@ void ELFObjectWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
11561156
case ELF::SHT_RELA: {
11571157
sh_link = SymbolTableIndex;
11581158
assert(sh_link && ".symtab not found");
1159-
const MCSectionELF *InfoSection = Section.getAssociatedSection();
1160-
sh_info = SectionIndexMap.lookup(InfoSection);
1159+
const MCSection *InfoSection = Section.getAssociatedSection();
1160+
sh_info = SectionIndexMap.lookup(cast<MCSectionELF>(InfoSection));
11611161
break;
11621162
}
11631163

@@ -1177,8 +1177,11 @@ void ELFObjectWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
11771177
break;
11781178
}
11791179

1180-
if (Section.getFlags() & ELF::SHF_LINK_ORDER)
1181-
sh_link = SectionIndexMap.lookup(Section.getAssociatedSection());
1180+
if (Section.getFlags() & ELF::SHF_LINK_ORDER) {
1181+
const MCSymbol *Sym = Section.getAssociatedSymbol();
1182+
const MCSectionELF *Sec = cast<MCSectionELF>(&Sym->getSection());
1183+
sh_link = SectionIndexMap.lookup(Sec);
1184+
}
11821185

11831186
WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()),
11841187
Section.getType(), Section.getFlags(), 0, Offset, Size,
@@ -1302,7 +1305,8 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm,
13021305
// Remember the offset into the file for this section.
13031306
uint64_t SecStart = getStream().tell();
13041307

1305-
writeRelocations(Asm, *RelSection->getAssociatedSection());
1308+
writeRelocations(Asm,
1309+
cast<MCSectionELF>(*RelSection->getAssociatedSection()));
13061310

13071311
uint64_t SecEnd = getStream().tell();
13081312
SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);

lib/MC/MCContext.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
317317
unsigned EntrySize,
318318
const MCSymbolELF *Group,
319319
unsigned UniqueID,
320-
const MCSectionELF *Associated) {
320+
const MCSymbolELF *Associated) {
321321
MCSymbolELF *R;
322322
MCSymbol *&Sym = Symbols[Section];
323323
// A section symbol can not redefine regular symbols. There may be multiple
@@ -350,15 +350,15 @@ MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
350350
MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
351351
unsigned Flags, unsigned EntrySize,
352352
const MCSymbolELF *Group,
353-
const MCSectionELF *Associated) {
353+
const MCSectionELF *RelInfoSection) {
354354
StringMap<bool>::iterator I;
355355
bool Inserted;
356356
std::tie(I, Inserted) =
357357
RelSecNames.insert(std::make_pair(Name.str(), true));
358358

359-
return createELFSectionImpl(I->getKey(), Type, Flags,
360-
SectionKind::getReadOnly(), EntrySize, Group,
361-
true, Associated);
359+
return createELFSectionImpl(
360+
I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
361+
true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
362362
}
363363

364364
MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
@@ -371,7 +371,7 @@ MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
371371
MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
372372
unsigned Flags, unsigned EntrySize,
373373
const Twine &Group, unsigned UniqueID,
374-
const MCSectionELF *Associated) {
374+
const MCSymbolELF *Associated) {
375375
MCSymbolELF *GroupSym = nullptr;
376376
if (!Group.isTriviallyEmpty() && !Group.str().empty())
377377
GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
@@ -384,7 +384,7 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
384384
unsigned Flags, unsigned EntrySize,
385385
const MCSymbolELF *GroupSym,
386386
unsigned UniqueID,
387-
const MCSectionELF *Associated) {
387+
const MCSymbolELF *Associated) {
388388
StringRef Group = "";
389389
if (GroupSym)
390390
Group = GroupSym->getName();

lib/MC/MCParser/ELFAsmParser.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class ELFAsmParser : public MCAsmParserExtension {
157157
bool maybeParseSectionType(StringRef &TypeName);
158158
bool parseMergeSize(int64_t &Size);
159159
bool parseGroup(StringRef &GroupName);
160-
bool parseMetadataSym(MCSectionELF *&Associated);
160+
bool parseMetadataSym(MCSymbolELF *&Associated);
161161
bool maybeParseUniqueID(int64_t &UniqueID);
162162
};
163163

@@ -432,18 +432,17 @@ bool ELFAsmParser::parseGroup(StringRef &GroupName) {
432432
return false;
433433
}
434434

435-
bool ELFAsmParser::parseMetadataSym(MCSectionELF *&Associated) {
435+
bool ELFAsmParser::parseMetadataSym(MCSymbolELF *&Associated) {
436436
MCAsmLexer &L = getLexer();
437437
if (L.isNot(AsmToken::Comma))
438438
return TokError("expected metadata symbol");
439439
Lex();
440440
StringRef Name;
441441
if (getParser().parseIdentifier(Name))
442442
return true;
443-
MCSymbol *Sym = getContext().lookupSymbol(Name);
444-
if (!Sym || !Sym->isInSection())
443+
Associated = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
444+
if (!Associated || !Associated->isInSection())
445445
return TokError("symbol is not in a section: " + Name);
446-
Associated = cast<MCSectionELF>(&Sym->getSection());
447446
return false;
448447
}
449448

@@ -482,7 +481,7 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
482481
const MCExpr *Subsection = nullptr;
483482
bool UseLastGroup = false;
484483
StringRef UniqueStr;
485-
MCSectionELF *Associated = nullptr;
484+
MCSymbolELF *Associated = nullptr;
486485
int64_t UniqueID = ~0;
487486

488487
// Set the defaults first.
@@ -597,8 +596,9 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
597596
}
598597
}
599598

600-
MCSection *ELFSection = getContext().getELFSection(
601-
SectionName, Type, Flags, Size, GroupName, UniqueID, Associated);
599+
MCSection *ELFSection =
600+
getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
601+
UniqueID, Associated);
602602
getStreamer().SwitchSection(ELFSection, Subsection);
603603

604604
if (getContext().getGenDwarfForAssembly()) {

lib/MC/MCSectionELF.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
103103
OS << 'S';
104104
if (Flags & ELF::SHF_TLS)
105105
OS << 'T';
106+
if (Flags & ELF::SHF_LINK_ORDER)
107+
OS << 'm';
106108

107109
// If there are target-specific flags, print them.
108110
Triple::ArchType Arch = T.getArch();
@@ -160,6 +162,12 @@ void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
160162
OS << ",comdat";
161163
}
162164

165+
if (Flags & ELF::SHF_LINK_ORDER) {
166+
assert(AssociatedSymbol);
167+
OS << ",";
168+
printName(OS, AssociatedSymbol->getName());
169+
}
170+
163171
if (isUnique())
164172
OS << ",unique," << UniqueID;
165173

lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,8 @@ inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix,
11391139
if (Group)
11401140
Flags |= ELF::SHF_GROUP;
11411141
MCSectionELF *EHSection = getContext().getELFSection(
1142-
EHSecName, Type, Flags, 0, Group, FnSection.getUniqueID(), &FnSection);
1142+
EHSecName, Type, Flags, 0, Group, FnSection.getUniqueID(),
1143+
static_cast<const MCSymbolELF *>(&Fn));
11431144

11441145
assert(EHSection && "Failed to get the required EH section");
11451146

test/MC/ELF/section.s

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | llvm-readobj -s | FileCheck %s
2+
// RUN: llvm-mc -filetype=asm -triple x86_64-pc-linux-gnu %s -o - | FileCheck %s --check-prefix=ASM
23

34
// Test that these names are accepted.
45

@@ -165,6 +166,9 @@ bar:
165166
.section .shf_metadata1,"am",@progbits,.Lshf_metadata_target2_1
166167
.section .shf_metadata2,"am",@progbits,.Lshf_metadata_target2_2
167168
.section .shf_metadata3,"am",@progbits,.shf_metadata_target1
169+
// ASM: .section .shf_metadata1,"am",@progbits,.Lshf_metadata_target2_1
170+
// ASM: .section .shf_metadata2,"am",@progbits,.Lshf_metadata_target2_2
171+
// ASM: .section .shf_metadata3,"am",@progbits,.shf_metadata_target1
168172

169173
// CHECK: Section {
170174
// CHECK: Index: 22

0 commit comments

Comments
 (0)