Skip to content

Commit 53187f1

Browse files
committed
ELFObjectWriter: Simplify
* Delete unused ELFSymbolData::operator< * Inline createStringTable * Fix a comment * Change align to return uint64_t
1 parent 3643828 commit 53187f1

File tree

1 file changed

+13
-44
lines changed

1 file changed

+13
-44
lines changed

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,6 @@ struct ELFWriter {
119119
StringRef Name;
120120
uint32_t SectionIndex;
121121
uint32_t Order;
122-
123-
// Support lexicographic sorting.
124-
bool operator<(const ELFSymbolData &RHS) const {
125-
unsigned LHSType = Symbol->getType();
126-
unsigned RHSType = RHS.Symbol->getType();
127-
if (LHSType == ELF::STT_SECTION && RHSType != ELF::STT_SECTION)
128-
return false;
129-
if (LHSType != ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
130-
return true;
131-
if (LHSType == ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
132-
return SectionIndex < RHS.SectionIndex;
133-
return Name < RHS.Name;
134-
}
135122
};
136123

137124
/// @}
@@ -157,7 +144,7 @@ struct ELFWriter {
157144
bool is64Bit() const;
158145
bool hasRelocationAddend() const;
159146

160-
void align(unsigned Alignment);
147+
uint64_t align(unsigned Alignment);
161148

162149
bool maybeWriteCompression(uint64_t Size,
163150
SmallVectorImpl<char> &CompressedContents,
@@ -207,8 +194,6 @@ struct ELFWriter {
207194
MCSectionELF *createRelocationSection(MCContext &Ctx,
208195
const MCSectionELF &Sec);
209196

210-
const MCSectionELF *createStringTable(MCContext &Ctx);
211-
212197
void writeSectionHeader(const MCAsmLayout &Layout,
213198
const SectionIndexMapTy &SectionIndexMap,
214199
const SectionOffsetsTy &SectionOffsets);
@@ -337,9 +322,10 @@ class ELFDwoObjectWriter : public ELFObjectWriter {
337322

338323
} // end anonymous namespace
339324

340-
void ELFWriter::align(unsigned Alignment) {
341-
uint64_t Padding = offsetToAlignment(W.OS.tell(), Align(Alignment));
342-
W.OS.write_zeros(Padding);
325+
uint64_t ELFWriter::align(unsigned Alignment) {
326+
uint64_t Offset = W.OS.tell(), NewOffset = alignTo(Offset, Alignment);
327+
W.OS.write_zeros(NewOffset - Offset);
328+
return NewOffset;
343329
}
344330

345331
unsigned ELFWriter::addToSectionTable(const MCSectionELF *Sec) {
@@ -458,7 +444,7 @@ void ELFWriter::writeHeader(const MCAssembler &Asm) {
458444
// e_shnum = # of section header ents
459445
W.write<uint16_t>(0);
460446

461-
// e_shstrndx = Section # of '.shstrtab'
447+
// e_shstrndx = Section # of '.strtab'
462448
assert(StringTableIndex < ELF::SHN_LORESERVE);
463449
W.write<uint16_t>(StringTableIndex);
464450
}
@@ -619,8 +605,7 @@ void ELFWriter::computeSymbolTable(
619605
SymtabSection->setAlignment(is64Bit() ? Align(8) : Align(4));
620606
SymbolTableIndex = addToSectionTable(SymtabSection);
621607

622-
align(SymtabSection->getAlignment());
623-
uint64_t SecStart = W.OS.tell();
608+
uint64_t SecStart = align(SymtabSection->getAlignment());
624609

625610
// The first entry is the undefined symbol entry.
626611
Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
@@ -985,12 +970,6 @@ void ELFWriter::writeRelocations(const MCAssembler &Asm,
985970
}
986971
}
987972

988-
const MCSectionELF *ELFWriter::createStringTable(MCContext &Ctx) {
989-
const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1];
990-
StrTabBuilder.write(W.OS);
991-
return StrtabSection;
992-
}
993-
994973
void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
995974
uint32_t GroupSymbolIndex, uint64_t Offset,
996975
uint64_t Size, const MCSectionELF &Section) {
@@ -1105,10 +1084,8 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
11051084
if (Mode == DwoOnly && !isDwoSection(Section))
11061085
continue;
11071086

1108-
align(Section.getAlignment());
1109-
11101087
// Remember the offset into the file for this section.
1111-
uint64_t SecStart = W.OS.tell();
1088+
const uint64_t SecStart = align(Section.getAlignment());
11121089

11131090
const MCSymbolELF *SignatureSymbol = Section.getGroup();
11141091
writeSectionData(Asm, Section, Layout);
@@ -1151,10 +1128,8 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
11511128
}
11521129

11531130
for (MCSectionELF *Group : Groups) {
1154-
align(Group->getAlignment());
1155-
11561131
// Remember the offset into the file for this section.
1157-
uint64_t SecStart = W.OS.tell();
1132+
const uint64_t SecStart = align(Group->getAlignment());
11581133

11591134
const MCSymbol *SignatureSymbol = Group->getGroup();
11601135
assert(SignatureSymbol);
@@ -1185,10 +1160,8 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
11851160
SectionOffsets);
11861161

11871162
for (MCSectionELF *RelSection : Relocations) {
1188-
align(RelSection->getAlignment());
1189-
11901163
// Remember the offset into the file for this section.
1191-
uint64_t SecStart = W.OS.tell();
1164+
const uint64_t SecStart = align(RelSection->getAlignment());
11921165

11931166
writeRelocations(Asm,
11941167
cast<MCSectionELF>(*RelSection->getLinkedToSection()));
@@ -1218,15 +1191,11 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
12181191

12191192
{
12201193
uint64_t SecStart = W.OS.tell();
1221-
const MCSectionELF *Sec = createStringTable(Ctx);
1222-
uint64_t SecEnd = W.OS.tell();
1223-
SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);
1194+
StrTabBuilder.write(W.OS);
1195+
SectionOffsets[StrtabSection] = std::make_pair(SecStart, W.OS.tell());
12241196
}
12251197

1226-
uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1227-
align(NaturalAlignment);
1228-
1229-
const uint64_t SectionHeaderOffset = W.OS.tell();
1198+
const uint64_t SectionHeaderOffset = align(is64Bit() ? 8 : 4);
12301199

12311200
// ... then the section header table ...
12321201
writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);

0 commit comments

Comments
 (0)