Skip to content

Commit 029644e

Browse files
author
Georgii Rymar
committed
[yaml2obj] - Refine how we set the sh_link field. NFCI.
This refactors the logic that sets the `sh_link` field. With this patch we set it in a single place for all sections. Differential revision: https://reviews.llvm.org/D95354
1 parent db92d47 commit 029644e

File tree

1 file changed

+42
-77
lines changed

1 file changed

+42
-77
lines changed

llvm/lib/ObjectYAML/ELFEmitter.cpp

Lines changed: 42 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,17 @@ ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
383383
if (DocSections.count(SecName))
384384
continue;
385385

386-
std::unique_ptr<ELFYAML::Chunk> Sec = std::make_unique<ELFYAML::Section>(
386+
std::unique_ptr<ELFYAML::Section> Sec = std::make_unique<ELFYAML::Section>(
387387
ELFYAML::Chunk::ChunkKind::RawContent, true /*IsImplicit*/);
388388
Sec->Name = SecName;
389389

390+
if (SecName == ".dynsym")
391+
Sec->Type = ELF::SHT_DYNSYM;
392+
else if (SecName == ".symtab")
393+
Sec->Type = ELF::SHT_SYMTAB;
394+
else
395+
Sec->Type = ELF::SHT_STRTAB;
396+
390397
// When the section header table is explicitly defined at the end of the
391398
// sections list, it is reasonable to assume that the user wants to reorder
392399
// section headers, but still wants to place the section header table after
@@ -682,6 +689,29 @@ static uint64_t writeContent(ContiguousBlobAccumulator &CBA,
682689
return *Size;
683690
}
684691

692+
static StringRef getDefaultLinkSec(unsigned SecType) {
693+
switch (SecType) {
694+
case ELF::SHT_REL:
695+
case ELF::SHT_RELA:
696+
case ELF::SHT_GROUP:
697+
case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
698+
case ELF::SHT_LLVM_ADDRSIG:
699+
return ".symtab";
700+
case ELF::SHT_GNU_versym:
701+
case ELF::SHT_HASH:
702+
case ELF::SHT_GNU_HASH:
703+
return ".dynsym";
704+
case ELF::SHT_DYNSYM:
705+
case ELF::SHT_GNU_verdef:
706+
case ELF::SHT_GNU_verneed:
707+
return ".dynstr";
708+
case ELF::SHT_SYMTAB:
709+
return ".strtab";
710+
default:
711+
return "";
712+
}
713+
}
714+
685715
template <class ELFT>
686716
void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
687717
ContiguousBlobAccumulator &CBA) {
@@ -721,11 +751,21 @@ void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
721751
if (IsFirstUndefSection && Sec->IsImplicit)
722752
continue;
723753

754+
Elf_Shdr &SHeader = SHeaders[SN2I.get(Sec->Name)];
755+
if (Sec->Link) {
756+
SHeader.sh_link = toSectionIndex(*Sec->Link, Sec->Name);
757+
} else {
758+
StringRef LinkSec = getDefaultLinkSec(Sec->Type);
759+
unsigned Link = 0;
760+
if (!LinkSec.empty() && !ExcludedSectionHeaders.count(LinkSec) &&
761+
SN2I.lookup(LinkSec, Link))
762+
SHeader.sh_link = Link;
763+
}
764+
724765
// We have a few sections like string or symbol tables that are usually
725766
// added implicitly to the end. However, if they are explicitly specified
726767
// in the YAML, we need to write them here. This ensures the file offset
727768
// remains correct.
728-
Elf_Shdr &SHeader = SHeaders[SN2I.get(Sec->Name)];
729769
if (initImplicitHeader(CBA, SHeader, Sec->Name,
730770
Sec->IsImplicit ? nullptr : Sec))
731771
continue;
@@ -747,9 +787,6 @@ void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
747787

748788
assignSectionAddress(SHeader, Sec);
749789

750-
if (Sec->Link)
751-
SHeader.sh_link = toSectionIndex(*Sec->Link, Sec->Name);
752-
753790
if (Sec->EntSize)
754791
SHeader.sh_entsize = *Sec->EntSize;
755792
else
@@ -917,34 +954,13 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
917954
}
918955
}
919956

920-
zero(SHeader);
921957
SHeader.sh_name = getSectionNameOffset(IsStatic ? ".symtab" : ".dynsym");
922958

923959
if (YAMLSec)
924960
SHeader.sh_type = YAMLSec->Type;
925961
else
926962
SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
927963

928-
if (RawSec && RawSec->Link) {
929-
// If the Link field is explicitly defined in the document,
930-
// we should use it.
931-
SHeader.sh_link = toSectionIndex(*RawSec->Link, RawSec->Name);
932-
} else {
933-
// When we describe the .dynsym section in the document explicitly, it is
934-
// allowed to omit the "DynamicSymbols" tag. In this case .dynstr is not
935-
// added implicitly and we should be able to leave the Link zeroed if
936-
// .dynstr is not defined.
937-
unsigned Link = 0;
938-
if (IsStatic) {
939-
if (!ExcludedSectionHeaders.count(".strtab"))
940-
Link = SN2I.get(".strtab");
941-
} else {
942-
if (!ExcludedSectionHeaders.count(".dynstr"))
943-
SN2I.lookup(".dynstr", Link);
944-
}
945-
SHeader.sh_link = Link;
946-
}
947-
948964
if (YAMLSec && YAMLSec->Flags)
949965
SHeader.sh_flags = *YAMLSec->Flags;
950966
else if (!IsStatic)
@@ -981,7 +997,6 @@ void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
981997
StringTableBuilder &STB,
982998
ContiguousBlobAccumulator &CBA,
983999
ELFYAML::Section *YAMLSec) {
984-
zero(SHeader);
9851000
SHeader.sh_name = getSectionNameOffset(Name);
9861001
SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB;
9871002
SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
@@ -1043,7 +1058,6 @@ template <class ELFT>
10431058
void ELFState<ELFT>::initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
10441059
ContiguousBlobAccumulator &CBA,
10451060
ELFYAML::Section *YAMLSec) {
1046-
zero(SHeader);
10471061
SHeader.sh_name = getSectionNameOffset(ELFYAML::dropUniqueSuffix(Name));
10481062
SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_PROGBITS;
10491063
SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1;
@@ -1083,9 +1097,6 @@ void ELFState<ELFT>::initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
10831097
else if (Name == ".debug_str")
10841098
SHeader.sh_flags = ELF::SHF_MERGE | ELF::SHF_STRINGS;
10851099

1086-
if (YAMLSec && YAMLSec->Link)
1087-
SHeader.sh_link = toSectionIndex(*YAMLSec->Link, Name);
1088-
10891100
assignSectionAddress(SHeader, YAMLSec);
10901101
}
10911102

@@ -1229,12 +1240,6 @@ void ELFState<ELFT>::writeSectionContent(
12291240
Section.Type == llvm::ELF::SHT_RELA) &&
12301241
"Section type is not SHT_REL nor SHT_RELA");
12311242

1232-
// For relocation section set link to .symtab by default.
1233-
unsigned Link = 0;
1234-
if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
1235-
SN2I.lookup(".symtab", Link))
1236-
SHeader.sh_link = Link;
1237-
12381243
if (!Section.RelocatableSec.empty())
12391244
SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name);
12401245

@@ -1307,11 +1312,6 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
13071312
assert(Section.Type == llvm::ELF::SHT_GROUP &&
13081313
"Section type is not SHT_GROUP");
13091314

1310-
unsigned Link = 0;
1311-
if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
1312-
SN2I.lookup(".symtab", Link))
1313-
SHeader.sh_link = Link;
1314-
13151315
if (Section.Signature)
13161316
SHeader.sh_info =
13171317
toSymbolIndex(*Section.Signature, Section.Name, /*IsDynamic=*/false);
@@ -1334,11 +1334,6 @@ template <class ELFT>
13341334
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
13351335
const ELFYAML::SymverSection &Section,
13361336
ContiguousBlobAccumulator &CBA) {
1337-
unsigned Link = 0;
1338-
if (!Section.Link && !ExcludedSectionHeaders.count(".dynsym") &&
1339-
SN2I.lookup(".dynsym", Link))
1340-
SHeader.sh_link = Link;
1341-
13421337
if (!Section.Entries)
13431338
return;
13441339

@@ -1444,11 +1439,6 @@ template <class ELFT>
14441439
void ELFState<ELFT>::writeSectionContent(
14451440
Elf_Shdr &SHeader, const ELFYAML::CallGraphProfileSection &Section,
14461441
ContiguousBlobAccumulator &CBA) {
1447-
unsigned Link = 0;
1448-
if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
1449-
SN2I.lookup(".symtab", Link))
1450-
SHeader.sh_link = Link;
1451-
14521442
if (!Section.Entries)
14531443
return;
14541444

@@ -1467,11 +1457,6 @@ template <class ELFT>
14671457
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
14681458
const ELFYAML::HashSection &Section,
14691459
ContiguousBlobAccumulator &CBA) {
1470-
unsigned Link = 0;
1471-
if (!Section.Link && !ExcludedSectionHeaders.count(".dynsym") &&
1472-
SN2I.lookup(".dynsym", Link))
1473-
SHeader.sh_link = Link;
1474-
14751460
if (!Section.Bucket)
14761461
return;
14771462

@@ -1503,11 +1488,6 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
15031488
else if (Section.Entries)
15041489
SHeader.sh_info = Section.Entries->size();
15051490

1506-
unsigned Link = 0;
1507-
if (!Section.Link && !ExcludedSectionHeaders.count(".dynstr") &&
1508-
SN2I.lookup(".dynstr", Link))
1509-
SHeader.sh_link = Link;
1510-
15111491
if (!Section.Entries)
15121492
return;
15131493

@@ -1553,11 +1533,6 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
15531533
else if (Section.VerneedV)
15541534
SHeader.sh_info = Section.VerneedV->size();
15551535

1556-
unsigned Link = 0;
1557-
if (!Section.Link && !ExcludedSectionHeaders.count(".dynstr") &&
1558-
SN2I.lookup(".dynstr", Link))
1559-
SHeader.sh_link = Link;
1560-
15611536
if (!Section.VerneedV)
15621537
return;
15631538

@@ -1657,11 +1632,6 @@ template <class ELFT>
16571632
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
16581633
const ELFYAML::AddrsigSection &Section,
16591634
ContiguousBlobAccumulator &CBA) {
1660-
unsigned Link = 0;
1661-
if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
1662-
SN2I.lookup(".symtab", Link))
1663-
SHeader.sh_link = Link;
1664-
16651635
if (!Section.Symbols)
16661636
return;
16671637

@@ -1718,11 +1688,6 @@ template <class ELFT>
17181688
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
17191689
const ELFYAML::GnuHashSection &Section,
17201690
ContiguousBlobAccumulator &CBA) {
1721-
unsigned Link = 0;
1722-
if (!Section.Link && !ExcludedSectionHeaders.count(".dynsym") &&
1723-
SN2I.lookup(".dynsym", Link))
1724-
SHeader.sh_link = Link;
1725-
17261691
if (!Section.HashBuckets)
17271692
return;
17281693

0 commit comments

Comments
 (0)