@@ -383,10 +383,17 @@ ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
383
383
if (DocSections.count (SecName))
384
384
continue ;
385
385
386
- std::unique_ptr<ELFYAML::Chunk > Sec = std::make_unique<ELFYAML::Section>(
386
+ std::unique_ptr<ELFYAML::Section > Sec = std::make_unique<ELFYAML::Section>(
387
387
ELFYAML::Chunk::ChunkKind::RawContent, true /* IsImplicit*/ );
388
388
Sec->Name = SecName;
389
389
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
+
390
397
// When the section header table is explicitly defined at the end of the
391
398
// sections list, it is reasonable to assume that the user wants to reorder
392
399
// section headers, but still wants to place the section header table after
@@ -682,6 +689,29 @@ static uint64_t writeContent(ContiguousBlobAccumulator &CBA,
682
689
return *Size;
683
690
}
684
691
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
+
685
715
template <class ELFT >
686
716
void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
687
717
ContiguousBlobAccumulator &CBA) {
@@ -721,11 +751,21 @@ void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
721
751
if (IsFirstUndefSection && Sec->IsImplicit )
722
752
continue ;
723
753
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
+
724
765
// We have a few sections like string or symbol tables that are usually
725
766
// added implicitly to the end. However, if they are explicitly specified
726
767
// in the YAML, we need to write them here. This ensures the file offset
727
768
// remains correct.
728
- Elf_Shdr &SHeader = SHeaders[SN2I.get (Sec->Name )];
729
769
if (initImplicitHeader (CBA, SHeader, Sec->Name ,
730
770
Sec->IsImplicit ? nullptr : Sec))
731
771
continue ;
@@ -747,9 +787,6 @@ void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
747
787
748
788
assignSectionAddress (SHeader, Sec);
749
789
750
- if (Sec->Link )
751
- SHeader.sh_link = toSectionIndex (*Sec->Link , Sec->Name );
752
-
753
790
if (Sec->EntSize )
754
791
SHeader.sh_entsize = *Sec->EntSize ;
755
792
else
@@ -917,34 +954,13 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
917
954
}
918
955
}
919
956
920
- zero (SHeader);
921
957
SHeader.sh_name = getSectionNameOffset (IsStatic ? " .symtab" : " .dynsym" );
922
958
923
959
if (YAMLSec)
924
960
SHeader.sh_type = YAMLSec->Type ;
925
961
else
926
962
SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
927
963
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
-
948
964
if (YAMLSec && YAMLSec->Flags )
949
965
SHeader.sh_flags = *YAMLSec->Flags ;
950
966
else if (!IsStatic)
@@ -981,7 +997,6 @@ void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
981
997
StringTableBuilder &STB,
982
998
ContiguousBlobAccumulator &CBA,
983
999
ELFYAML::Section *YAMLSec) {
984
- zero (SHeader);
985
1000
SHeader.sh_name = getSectionNameOffset (Name);
986
1001
SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB;
987
1002
SHeader.sh_addralign = YAMLSec ? (uint64_t )YAMLSec->AddressAlign : 1 ;
@@ -1043,7 +1058,6 @@ template <class ELFT>
1043
1058
void ELFState<ELFT>::initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
1044
1059
ContiguousBlobAccumulator &CBA,
1045
1060
ELFYAML::Section *YAMLSec) {
1046
- zero (SHeader);
1047
1061
SHeader.sh_name = getSectionNameOffset (ELFYAML::dropUniqueSuffix (Name));
1048
1062
SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_PROGBITS;
1049
1063
SHeader.sh_addralign = YAMLSec ? (uint64_t )YAMLSec->AddressAlign : 1 ;
@@ -1083,9 +1097,6 @@ void ELFState<ELFT>::initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
1083
1097
else if (Name == " .debug_str" )
1084
1098
SHeader.sh_flags = ELF::SHF_MERGE | ELF::SHF_STRINGS;
1085
1099
1086
- if (YAMLSec && YAMLSec->Link )
1087
- SHeader.sh_link = toSectionIndex (*YAMLSec->Link , Name);
1088
-
1089
1100
assignSectionAddress (SHeader, YAMLSec);
1090
1101
}
1091
1102
@@ -1229,12 +1240,6 @@ void ELFState<ELFT>::writeSectionContent(
1229
1240
Section.Type == llvm::ELF::SHT_RELA) &&
1230
1241
" Section type is not SHT_REL nor SHT_RELA" );
1231
1242
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
-
1238
1243
if (!Section.RelocatableSec .empty ())
1239
1244
SHeader.sh_info = toSectionIndex (Section.RelocatableSec , Section.Name );
1240
1245
@@ -1307,11 +1312,6 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1307
1312
assert (Section.Type == llvm::ELF::SHT_GROUP &&
1308
1313
" Section type is not SHT_GROUP" );
1309
1314
1310
- unsigned Link = 0 ;
1311
- if (!Section.Link && !ExcludedSectionHeaders.count (" .symtab" ) &&
1312
- SN2I.lookup (" .symtab" , Link))
1313
- SHeader.sh_link = Link;
1314
-
1315
1315
if (Section.Signature )
1316
1316
SHeader.sh_info =
1317
1317
toSymbolIndex (*Section.Signature , Section.Name , /* IsDynamic=*/ false );
@@ -1334,11 +1334,6 @@ template <class ELFT>
1334
1334
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1335
1335
const ELFYAML::SymverSection &Section,
1336
1336
ContiguousBlobAccumulator &CBA) {
1337
- unsigned Link = 0 ;
1338
- if (!Section.Link && !ExcludedSectionHeaders.count (" .dynsym" ) &&
1339
- SN2I.lookup (" .dynsym" , Link))
1340
- SHeader.sh_link = Link;
1341
-
1342
1337
if (!Section.Entries )
1343
1338
return ;
1344
1339
@@ -1444,11 +1439,6 @@ template <class ELFT>
1444
1439
void ELFState<ELFT>::writeSectionContent(
1445
1440
Elf_Shdr &SHeader, const ELFYAML::CallGraphProfileSection &Section,
1446
1441
ContiguousBlobAccumulator &CBA) {
1447
- unsigned Link = 0 ;
1448
- if (!Section.Link && !ExcludedSectionHeaders.count (" .symtab" ) &&
1449
- SN2I.lookup (" .symtab" , Link))
1450
- SHeader.sh_link = Link;
1451
-
1452
1442
if (!Section.Entries )
1453
1443
return ;
1454
1444
@@ -1467,11 +1457,6 @@ template <class ELFT>
1467
1457
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1468
1458
const ELFYAML::HashSection &Section,
1469
1459
ContiguousBlobAccumulator &CBA) {
1470
- unsigned Link = 0 ;
1471
- if (!Section.Link && !ExcludedSectionHeaders.count (" .dynsym" ) &&
1472
- SN2I.lookup (" .dynsym" , Link))
1473
- SHeader.sh_link = Link;
1474
-
1475
1460
if (!Section.Bucket )
1476
1461
return ;
1477
1462
@@ -1503,11 +1488,6 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1503
1488
else if (Section.Entries )
1504
1489
SHeader.sh_info = Section.Entries ->size ();
1505
1490
1506
- unsigned Link = 0 ;
1507
- if (!Section.Link && !ExcludedSectionHeaders.count (" .dynstr" ) &&
1508
- SN2I.lookup (" .dynstr" , Link))
1509
- SHeader.sh_link = Link;
1510
-
1511
1491
if (!Section.Entries )
1512
1492
return ;
1513
1493
@@ -1553,11 +1533,6 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1553
1533
else if (Section.VerneedV )
1554
1534
SHeader.sh_info = Section.VerneedV ->size ();
1555
1535
1556
- unsigned Link = 0 ;
1557
- if (!Section.Link && !ExcludedSectionHeaders.count (" .dynstr" ) &&
1558
- SN2I.lookup (" .dynstr" , Link))
1559
- SHeader.sh_link = Link;
1560
-
1561
1536
if (!Section.VerneedV )
1562
1537
return ;
1563
1538
@@ -1657,11 +1632,6 @@ template <class ELFT>
1657
1632
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1658
1633
const ELFYAML::AddrsigSection &Section,
1659
1634
ContiguousBlobAccumulator &CBA) {
1660
- unsigned Link = 0 ;
1661
- if (!Section.Link && !ExcludedSectionHeaders.count (" .symtab" ) &&
1662
- SN2I.lookup (" .symtab" , Link))
1663
- SHeader.sh_link = Link;
1664
-
1665
1635
if (!Section.Symbols )
1666
1636
return ;
1667
1637
@@ -1718,11 +1688,6 @@ template <class ELFT>
1718
1688
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
1719
1689
const ELFYAML::GnuHashSection &Section,
1720
1690
ContiguousBlobAccumulator &CBA) {
1721
- unsigned Link = 0 ;
1722
- if (!Section.Link && !ExcludedSectionHeaders.count (" .dynsym" ) &&
1723
- SN2I.lookup (" .dynsym" , Link))
1724
- SHeader.sh_link = Link;
1725
-
1726
1691
if (!Section.HashBuckets )
1727
1692
return ;
1728
1693
0 commit comments