Skip to content

Commit 22eb026

Browse files
committedNov 14, 2016
[ELF] Convert StringTableSection to input section
Differential revision: https://reviews.llvm.org/D26549 llvm-svn: 286799
1 parent e3d8026 commit 22eb026

File tree

5 files changed

+114
-95
lines changed

5 files changed

+114
-95
lines changed
 

‎lld/ELF/OutputSections.cpp

Lines changed: 23 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,15 @@ template <class ELFT> void DynamicSection<ELFT>::addEntries() {
415415
// Add strings to .dynstr early so that .dynstr's size will be
416416
// fixed early.
417417
for (StringRef S : Config->AuxiliaryList)
418-
Add({DT_AUXILIARY, Out<ELFT>::DynStrTab->addString(S)});
418+
Add({DT_AUXILIARY, In<ELFT>::DynStrTab->addString(S)});
419419
if (!Config->RPath.empty())
420420
Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
421-
Out<ELFT>::DynStrTab->addString(Config->RPath)});
421+
In<ELFT>::DynStrTab->addString(Config->RPath)});
422422
for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles())
423423
if (F->isNeeded())
424-
Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
424+
Add({DT_NEEDED, In<ELFT>::DynStrTab->addString(F->getSoName())});
425425
if (!Config->SoName.empty())
426-
Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
426+
Add({DT_SONAME, In<ELFT>::DynStrTab->addString(Config->SoName)});
427427

428428
// Set DT_FLAGS and DT_FLAGS_1.
429429
uint32_t DtFlags = 0;
@@ -455,7 +455,7 @@ template <class ELFT> void DynamicSection<ELFT>::finalize() {
455455
if (this->Size)
456456
return; // Already finalized.
457457

458-
this->Link = Out<ELFT>::DynStrTab->SectionIndex;
458+
this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
459459

460460
if (Out<ELFT>::RelaDyn->hasRelocs()) {
461461
bool IsRela = Config->Rela;
@@ -483,8 +483,8 @@ template <class ELFT> void DynamicSection<ELFT>::finalize() {
483483

484484
Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
485485
Add({DT_SYMENT, sizeof(Elf_Sym)});
486-
Add({DT_STRTAB, Out<ELFT>::DynStrTab});
487-
Add({DT_STRSZ, Out<ELFT>::DynStrTab->Size});
486+
Add({DT_STRTAB, In<ELFT>::DynStrTab});
487+
Add({DT_STRSZ, In<ELFT>::DynStrTab->getSize()});
488488
if (Out<ELFT>::GnuHashTab)
489489
Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
490490
if (Out<ELFT>::HashTab)
@@ -650,6 +650,15 @@ template <class ELFT> void OutputSection<ELFT>::finalize() {
650650
this->Link = D->OutSec->SectionIndex;
651651
}
652652
}
653+
654+
// Recalculate input section offsets if we own any synthetic section
655+
for (auto *SS : In<ELFT>::SyntheticSections)
656+
if (SS && this == SS->OutSec) {
657+
this->Size = 0;
658+
assignOffsets();
659+
break;
660+
}
661+
653662
if (Type != SHT_RELA && Type != SHT_REL)
654663
return;
655664
this->Link = Out<ELFT>::SymTab->SectionIndex;
@@ -1051,40 +1060,6 @@ template <class ELFT> void MergeOutputSection<ELFT>::finalizePieces() {
10511060
Sec->finalizePieces();
10521061
}
10531062

1054-
template <class ELFT>
1055-
StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
1056-
: OutputSectionBase(Name, SHT_STRTAB, Dynamic ? (uintX_t)SHF_ALLOC : 0),
1057-
Dynamic(Dynamic) {
1058-
// ELF string tables start with a NUL byte, so 1.
1059-
this->Size = 1;
1060-
}
1061-
1062-
// Adds a string to the string table. If HashIt is true we hash and check for
1063-
// duplicates. It is optional because the name of global symbols are already
1064-
// uniqued and hashing them again has a big cost for a small value: uniquing
1065-
// them with some other string that happens to be the same.
1066-
template <class ELFT>
1067-
unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1068-
if (HashIt) {
1069-
auto R = StringMap.insert(std::make_pair(S, this->Size));
1070-
if (!R.second)
1071-
return R.first->second;
1072-
}
1073-
unsigned Ret = this->Size;
1074-
this->Size = this->Size + S.size() + 1;
1075-
Strings.push_back(S);
1076-
return Ret;
1077-
}
1078-
1079-
template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
1080-
// ELF string tables start with NUL byte, so advance the pointer by one.
1081-
++Buf;
1082-
for (StringRef S : Strings) {
1083-
memcpy(Buf, S.data(), S.size());
1084-
Buf += S.size() + 1;
1085-
}
1086-
}
1087-
10881063
template <class ELFT>
10891064
typename ELFT::uint DynamicReloc<ELFT>::getOffset() const {
10901065
if (OutputSec)
@@ -1148,7 +1123,7 @@ template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
11481123
return; // Already finalized.
11491124

11501125
this->Size = getNumSymbols() * sizeof(Elf_Sym);
1151-
this->Link = StrTabSec.SectionIndex;
1126+
this->Link = StrTabSec.OutSec->SectionIndex;
11521127
this->Info = NumLocals + 1;
11531128

11541129
if (Config->Relocatable) {
@@ -1300,12 +1275,12 @@ static StringRef getFileDefName() {
13001275
}
13011276

13021277
template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() {
1303-
FileDefNameOff = Out<ELFT>::DynStrTab->addString(getFileDefName());
1278+
FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName());
13041279
for (VersionDefinition &V : Config->VersionDefinitions)
1305-
V.NameOff = Out<ELFT>::DynStrTab->addString(V.Name);
1280+
V.NameOff = In<ELFT>::DynStrTab->addString(V.Name);
13061281

13071282
this->Size = (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
1308-
this->Link = Out<ELFT>::DynStrTab->SectionIndex;
1283+
this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
13091284

13101285
// sh_info should be set to the number of definitions. This fact is missed in
13111286
// documentation, but confirmed by binutils community:
@@ -1389,13 +1364,13 @@ void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
13891364
// to create one by adding it to our needed list and creating a dynstr entry
13901365
// for the soname.
13911366
if (F->VerdefMap.empty())
1392-
Needed.push_back({F, Out<ELFT>::DynStrTab->addString(F->getSoName())});
1367+
Needed.push_back({F, In<ELFT>::DynStrTab->addString(F->getSoName())});
13931368
typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
13941369
// If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
13951370
// prepare to create one by allocating a version identifier and creating a
13961371
// dynstr entry for the version name.
13971372
if (NV.Index == 0) {
1398-
NV.StrTab = Out<ELFT>::DynStrTab->addString(
1373+
NV.StrTab = In<ELFT>::DynStrTab->addString(
13991374
SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name);
14001375
NV.Index = NextIndex++;
14011376
}
@@ -1438,7 +1413,7 @@ template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
14381413
}
14391414

14401415
template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
1441-
this->Link = Out<ELFT>::DynStrTab->SectionIndex;
1416+
this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
14421417
this->Info = Needed.size();
14431418
unsigned Size = Needed.size() * sizeof(Elf_Verneed);
14441419
for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
@@ -1590,11 +1565,6 @@ template class MergeOutputSection<ELF32BE>;
15901565
template class MergeOutputSection<ELF64LE>;
15911566
template class MergeOutputSection<ELF64BE>;
15921567

1593-
template class StringTableSection<ELF32LE>;
1594-
template class StringTableSection<ELF32BE>;
1595-
template class StringTableSection<ELF64LE>;
1596-
template class StringTableSection<ELF64BE>;
1597-
15981568
template class SymbolTableSection<ELF32LE>;
15991569
template class SymbolTableSection<ELF32BE>;
16001570
template class SymbolTableSection<ELF64LE>;

‎lld/ELF/OutputSections.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class OutputSectionBase {
5454
Plt,
5555
Regular,
5656
Reloc,
57-
StrTable,
5857
SymTable,
5958
VersDef,
6059
VersNeed,
@@ -424,26 +423,6 @@ template <class ELFT> class EhOutputSection final : public OutputSectionBase {
424423
llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
425424
};
426425

427-
template <class ELFT>
428-
class StringTableSection final : public OutputSectionBase {
429-
430-
public:
431-
typedef typename ELFT::uint uintX_t;
432-
StringTableSection(StringRef Name, bool Dynamic);
433-
unsigned addString(StringRef S, bool HashIt = true);
434-
void writeTo(uint8_t *Buf) override;
435-
bool isDynamic() const { return Dynamic; }
436-
Kind getKind() const override { return StrTable; }
437-
static bool classof(const OutputSectionBase *B) {
438-
return B->getKind() == StrTable;
439-
}
440-
441-
private:
442-
const bool Dynamic;
443-
llvm::DenseMap<StringRef, unsigned> StringMap;
444-
std::vector<StringRef> Strings;
445-
};
446-
447426
template <class ELFT> class HashTableSection final : public OutputSectionBase {
448427
typedef typename ELFT::Word Elf_Word;
449428

@@ -600,9 +579,6 @@ template <class ELFT> struct Out {
600579
static PltSection<ELFT> *Plt;
601580
static RelocationSection<ELFT> *RelaDyn;
602581
static RelocationSection<ELFT> *RelaPlt;
603-
static StringTableSection<ELFT> *DynStrTab;
604-
static StringTableSection<ELFT> *ShStrTab;
605-
static StringTableSection<ELFT> *StrTab;
606582
static SymbolTableSection<ELFT> *DynSymTab;
607583
static SymbolTableSection<ELFT> *SymTab;
608584
static VersionDefinitionSection<ELFT> *VerDef;
@@ -664,9 +640,6 @@ template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
664640
template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
665641
template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
666642
template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
667-
template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
668-
template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
669-
template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
670643
template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
671644
template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
672645
template <class ELFT> VersionDefinitionSection<ELFT> *Out<ELFT>::VerDef;

‎lld/ELF/SyntheticSections.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,38 @@ template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
642642
}
643643
}
644644

645+
template <class ELFT>
646+
StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
647+
: SyntheticSection<ELFT>(Dynamic ? (uintX_t)SHF_ALLOC : 0, SHT_STRTAB, 1,
648+
Name),
649+
Dynamic(Dynamic) {}
650+
651+
// Adds a string to the string table. If HashIt is true we hash and check for
652+
// duplicates. It is optional because the name of global symbols are already
653+
// uniqued and hashing them again has a big cost for a small value: uniquing
654+
// them with some other string that happens to be the same.
655+
template <class ELFT>
656+
unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
657+
if (HashIt) {
658+
auto R = StringMap.insert(std::make_pair(S, this->Size));
659+
if (!R.second)
660+
return R.first->second;
661+
}
662+
unsigned Ret = this->Size;
663+
this->Size = this->Size + S.size() + 1;
664+
Strings.push_back(S);
665+
return Ret;
666+
}
667+
668+
template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
669+
// ELF string tables start with NUL byte, so advance the pointer by one.
670+
++Buf;
671+
for (StringRef S : Strings) {
672+
memcpy(Buf, S.data(), S.size());
673+
Buf += S.size() + 1;
674+
}
675+
}
676+
645677
template InputSection<ELF32LE> *elf::createCommonSection();
646678
template InputSection<ELF32BE> *elf::createCommonSection();
647679
template InputSection<ELF64LE> *elf::createCommonSection();
@@ -711,3 +743,8 @@ template class elf::GotPltSection<ELF32LE>;
711743
template class elf::GotPltSection<ELF32BE>;
712744
template class elf::GotPltSection<ELF64LE>;
713745
template class elf::GotPltSection<ELF64BE>;
746+
747+
template class elf::StringTableSection<ELF32LE>;
748+
template class elf::StringTableSection<ELF32BE>;
749+
template class elf::StringTableSection<ELF64LE>;
750+
template class elf::StringTableSection<ELF64BE>;

‎lld/ELF/SyntheticSections.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,26 @@ class GotPltSection final : public SyntheticSection<ELFT> {
218218
std::vector<const SymbolBody *> Entries;
219219
};
220220

221+
template <class ELFT>
222+
class StringTableSection final : public SyntheticSection<ELFT> {
223+
public:
224+
typedef typename ELFT::uint uintX_t;
225+
StringTableSection(StringRef Name, bool Dynamic);
226+
unsigned addString(StringRef S, bool HashIt = true);
227+
void writeTo(uint8_t *Buf) override;
228+
size_t getSize() const override { return Size; }
229+
bool isDynamic() const { return Dynamic; }
230+
231+
private:
232+
const bool Dynamic;
233+
234+
// ELF string tables start with a NUL byte, so 1.
235+
uintX_t Size = 1;
236+
237+
llvm::DenseMap<StringRef, unsigned> StringMap;
238+
std::vector<StringRef> Strings;
239+
};
240+
221241
template <class ELFT> InputSection<ELFT> *createCommonSection();
222242
template <class ELFT> InputSection<ELFT> *createInterpSection();
223243
template <class ELFT> MergeInputSection<ELFT> *createCommentSection();
@@ -226,22 +246,35 @@ template <class ELFT> MergeInputSection<ELFT> *createCommentSection();
226246
template <class ELFT> struct In {
227247
static BuildIdSection<ELFT> *BuildId;
228248
static InputSection<ELFT> *Common;
249+
static StringTableSection<ELFT> *DynStrTab;
229250
static GotSection<ELFT> *Got;
230251
static GotPltSection<ELFT> *GotPlt;
231252
static InputSection<ELFT> *Interp;
232253
static MipsAbiFlagsSection<ELFT> *MipsAbiFlags;
233254
static MipsOptionsSection<ELFT> *MipsOptions;
234255
static MipsReginfoSection<ELFT> *MipsReginfo;
256+
static StringTableSection<ELFT> *ShStrTab;
257+
static StringTableSection<ELFT> *StrTab;
258+
259+
// Contains list of sections, which size is not known when
260+
// createSections() is called. This list is used when output
261+
// sections are being finalized to calculate their size correctly.
262+
static std::vector<SyntheticSection<ELFT> *> SyntheticSections;
235263
};
236264

237265
template <class ELFT> BuildIdSection<ELFT> *In<ELFT>::BuildId;
238266
template <class ELFT> InputSection<ELFT> *In<ELFT>::Common;
267+
template <class ELFT> StringTableSection<ELFT> *In<ELFT>::DynStrTab;
239268
template <class ELFT> GotSection<ELFT> *In<ELFT>::Got;
240269
template <class ELFT> GotPltSection<ELFT> *In<ELFT>::GotPlt;
241270
template <class ELFT> InputSection<ELFT> *In<ELFT>::Interp;
242271
template <class ELFT> MipsAbiFlagsSection<ELFT> *In<ELFT>::MipsAbiFlags;
243272
template <class ELFT> MipsOptionsSection<ELFT> *In<ELFT>::MipsOptions;
244273
template <class ELFT> MipsReginfoSection<ELFT> *In<ELFT>::MipsReginfo;
274+
template <class ELFT> StringTableSection<ELFT> *In<ELFT>::ShStrTab;
275+
template <class ELFT> StringTableSection<ELFT> *In<ELFT>::StrTab;
276+
template <class ELFT>
277+
std::vector<SyntheticSection<ELFT> *> In<ELFT>::SyntheticSections;
245278

246279
} // namespace elf
247280
} // namespace lld

0 commit comments

Comments
 (0)
Please sign in to comment.