Skip to content

Commit dea5ff1

Browse files
committed
Add support for allowing us to create uniquely identified "COMDAT" or "ELF
Group" sections while lowering. In particular, for ELF sections this is useful for creating function-specific groups that get merged into the same named section. Also use const Twine& instead of StringRef for the getELF functions while we're here. Differential Revision: http://reviews.llvm.org/D21743 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@274336 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 84607f6 commit dea5ff1

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

include/llvm/MC/MCContext.h

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -339,48 +339,56 @@ namespace llvm {
339339
BeginSymName);
340340
}
341341

342-
MCSectionELF *getELFSection(StringRef Section, unsigned Type,
342+
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
343343
unsigned Flags) {
344344
return getELFSection(Section, Type, Flags, nullptr);
345345
}
346346

347-
MCSectionELF *getELFSection(StringRef Section, unsigned Type,
347+
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
348348
unsigned Flags, const char *BeginSymName) {
349349
return getELFSection(Section, Type, Flags, 0, "", BeginSymName);
350350
}
351351

352-
MCSectionELF *getELFSection(StringRef Section, unsigned Type,
352+
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
353353
unsigned Flags, unsigned EntrySize,
354-
StringRef Group) {
354+
const Twine &Group) {
355355
return getELFSection(Section, Type, Flags, EntrySize, Group, nullptr);
356356
}
357357

358-
MCSectionELF *getELFSection(StringRef Section, unsigned Type,
358+
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
359359
unsigned Flags, unsigned EntrySize,
360-
StringRef Group, const char *BeginSymName) {
360+
const Twine &Group, const char *BeginSymName) {
361361
return getELFSection(Section, Type, Flags, EntrySize, Group, ~0,
362362
BeginSymName);
363363
}
364364

365-
MCSectionELF *getELFSection(StringRef Section, unsigned Type,
365+
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
366366
unsigned Flags, unsigned EntrySize,
367-
StringRef Group, unsigned UniqueID) {
367+
const Twine &Group, unsigned UniqueID) {
368368
return getELFSection(Section, Type, Flags, EntrySize, Group, UniqueID,
369369
nullptr);
370370
}
371371

372-
MCSectionELF *getELFSection(StringRef Section, unsigned Type,
372+
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
373373
unsigned Flags, unsigned EntrySize,
374-
StringRef Group, unsigned UniqueID,
374+
const Twine &Group, unsigned UniqueID,
375375
const char *BeginSymName);
376376

377-
MCSectionELF *getELFSection(StringRef Section, unsigned Type,
377+
MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
378378
unsigned Flags, unsigned EntrySize,
379379
const MCSymbolELF *Group, unsigned UniqueID,
380380
const char *BeginSymName,
381381
const MCSectionELF *Associated);
382382

383-
MCSectionELF *createELFRelSection(StringRef Name, unsigned Type,
383+
/// Get a section with the provided group identifier. This section is
384+
/// named by concatenating \p Prefix with '.' then \p Suffix. The \p Type
385+
/// describes the type of the section and \p Flags are used to further
386+
/// configure this named section.
387+
MCSectionELF *getELFNamedSection(const Twine &Prefix, const Twine &Suffix,
388+
unsigned Type, unsigned Flags,
389+
unsigned EntrySize = 0);
390+
391+
MCSectionELF *createELFRelSection(const Twine &Name, unsigned Type,
384392
unsigned Flags, unsigned EntrySize,
385393
const MCSymbolELF *Group,
386394
const MCSectionELF *Associated);

lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,9 @@ void TargetLoweringObjectFileELF::emitPersonalityValue(
6969
cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
7070
Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
7171
Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
72-
StringRef Prefix = ".data.";
73-
NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end());
7472
unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
75-
MCSection *Sec = getContext().getELFSection(NameData, ELF::SHT_PROGBITS,
76-
Flags, 0, Label->getName());
73+
MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(),
74+
ELF::SHT_PROGBITS, Flags, 0);
7775
unsigned Size = DL.getPointerSize();
7876
Streamer.SwitchSection(Sec);
7977
Streamer.EmitValueToAlignment(DL.getPointerABIAlignment());

lib/MC/MCContext.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,32 +309,40 @@ void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
309309
const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
310310
}
311311

312-
MCSectionELF *MCContext::createELFRelSection(StringRef Name, unsigned Type,
312+
MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
313313
unsigned Flags, unsigned EntrySize,
314314
const MCSymbolELF *Group,
315315
const MCSectionELF *Associated) {
316316
StringMap<bool>::iterator I;
317317
bool Inserted;
318-
std::tie(I, Inserted) = ELFRelSecNames.insert(std::make_pair(Name, true));
318+
std::tie(I, Inserted) =
319+
ELFRelSecNames.insert(std::make_pair(Name.str(), true));
319320

320321
return new (ELFAllocator.Allocate())
321322
MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
322323
EntrySize, Group, true, nullptr, Associated);
323324
}
324325

325-
MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
326+
MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
327+
const Twine &Suffix, unsigned Type,
328+
unsigned Flags,
329+
unsigned EntrySize) {
330+
return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
331+
}
332+
333+
MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
326334
unsigned Flags, unsigned EntrySize,
327-
StringRef Group, unsigned UniqueID,
335+
const Twine &Group, unsigned UniqueID,
328336
const char *BeginSymName) {
329337
MCSymbolELF *GroupSym = nullptr;
330-
if (!Group.empty())
338+
if (!Group.isTriviallyEmpty() && !Group.str().empty())
331339
GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
332340

333341
return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
334342
BeginSymName, nullptr);
335343
}
336344

337-
MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
345+
MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
338346
unsigned Flags, unsigned EntrySize,
339347
const MCSymbolELF *GroupSym,
340348
unsigned UniqueID,
@@ -345,7 +353,7 @@ MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
345353
Group = GroupSym->getName();
346354
// Do the lookup, if we have a hit, return it.
347355
auto IterBool = ELFUniquingMap.insert(
348-
std::make_pair(ELFSectionKey{Section, Group, UniqueID}, nullptr));
356+
std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
349357
auto &Entry = *IterBool.first;
350358
if (!IterBool.second)
351359
return Entry.second;

0 commit comments

Comments
 (0)