@@ -74,14 +74,6 @@ class NameToIdxMap {
74
74
Idx = I->getValue ();
75
75
return false ;
76
76
}
77
- // / asserts if name is not present in the map
78
- unsigned get (StringRef Name) const {
79
- unsigned Idx = 0 ;
80
- auto missing = lookup (Name, Idx);
81
- assert (!missing && " Expected section not found in index" );
82
- return Idx;
83
- }
84
- unsigned size () const { return Map.size (); }
85
77
};
86
78
} // end anonymous namespace
87
79
@@ -152,21 +144,19 @@ class ELFState {
152
144
ContiguousBlobAccumulator &CBA);
153
145
154
146
// - SHT_NULL entry (placed first, i.e. 0'th entry)
155
- // - symbol table (.symtab) (defaults to third to last)
156
- // - string table (.strtab) (defaults to second to last)
157
- // - section header string table (.shstrtab) (defaults to last)
158
- unsigned getDotSymTabSecNo () const { return SN2I. get ( " .symtab " ) ; }
159
- unsigned getDotStrTabSecNo () const { return SN2I. get ( " .strtab " ) ; }
160
- unsigned getDotShStrTabSecNo () const { return SN2I. get ( " .shstrtab " ) ; }
161
- unsigned getSectionCount () const { return SN2I. size () + 1 ; }
147
+ // - symbol table (.symtab) (placed third to last)
148
+ // - string table (.strtab) (placed second to last)
149
+ // - section header string table (.shstrtab) (placed last)
150
+ unsigned getDotSymTabSecNo () const { return Doc. Sections . size () + 1 ; }
151
+ unsigned getDotStrTabSecNo () const { return Doc. Sections . size () + 2 ; }
152
+ unsigned getDotShStrTabSecNo () const { return Doc. Sections . size () + 3 ; }
153
+ unsigned getSectionCount () const { return Doc. Sections . size () + 4 ; }
162
154
163
155
ELFState (const ELFYAML::Object &D) : Doc(D) {}
164
156
165
157
public:
166
158
static int writeELF (raw_ostream &OS, const ELFYAML::Object &Doc);
167
159
};
168
-
169
- static const char * const ImplicitSecNames[] = {" .symtab" , " .strtab" , " .shstrtab" };
170
160
} // end anonymous namespace
171
161
172
162
template <class ELFT >
@@ -221,6 +211,10 @@ bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
221
211
zero (SHeader);
222
212
SHeaders.push_back (SHeader);
223
213
214
+ for (const auto &Sec : Doc.Sections )
215
+ DotShStrtab.add (Sec->Name );
216
+ DotShStrtab.finalize ();
217
+
224
218
for (const auto &Sec : Doc.Sections ) {
225
219
zero (SHeader);
226
220
SHeader.sh_name = DotShStrtab.getOffset (Sec->Name );
@@ -553,9 +547,12 @@ bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
553
547
}
554
548
555
549
template <class ELFT > bool ELFState<ELFT>::buildSectionIndex() {
550
+ SN2I.addName (" .symtab" , getDotSymTabSecNo ());
551
+ SN2I.addName (" .strtab" , getDotStrTabSecNo ());
552
+ SN2I.addName (" .shstrtab" , getDotShStrTabSecNo ());
553
+
556
554
for (unsigned i = 0 , e = Doc.Sections .size (); i != e; ++i) {
557
555
StringRef Name = Doc.Sections [i]->Name ;
558
- DotShStrtab.add (Name);
559
556
if (Name.empty ())
560
557
continue ;
561
558
// "+ 1" to take into account the SHT_NULL entry.
@@ -565,17 +562,6 @@ template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
565
562
return false ;
566
563
}
567
564
}
568
-
569
- auto SecNo = 1 + Doc.Sections .size ();
570
- // Add special sections after input sections, if necessary.
571
- for (const auto &Name : ImplicitSecNames)
572
- if (!SN2I.addName (Name, SecNo)) {
573
- // Account for this section, since it wasn't in the Doc
574
- ++SecNo;
575
- DotShStrtab.add (Name);
576
- }
577
-
578
- DotShStrtab.finalize ();
579
565
return true ;
580
566
}
581
567
@@ -622,23 +608,32 @@ int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
622
608
Header.e_shentsize * Header.e_shnum ;
623
609
ContiguousBlobAccumulator CBA (SectionContentBeginOffset);
624
610
611
+ // Doc might not contain .symtab, .strtab and .shstrtab sections,
612
+ // but we will emit them, so make sure to add them to ShStrTabSHeader.
613
+ State.DotShStrtab .add (" .symtab" );
614
+ State.DotShStrtab .add (" .strtab" );
615
+ State.DotShStrtab .add (" .shstrtab" );
616
+
625
617
std::vector<Elf_Shdr> SHeaders;
626
- SHeaders.reserve (State.SN2I .size ());
627
618
if (!State.initSectionHeaders (SHeaders, CBA))
628
619
return 1 ;
629
620
630
- // Populate SHeaders with implicit sections not present in the Doc
631
- for (const auto &Name : ImplicitSecNames)
632
- if (State.SN2I .get (Name) >= SHeaders.size ())
633
- SHeaders.push_back ({});
634
-
635
- // Initialize the implicit sections
636
- auto Index = State.SN2I .get (" .symtab" );
637
- State.initSymtabSectionHeader (SHeaders[Index], CBA);
638
- Index = State.SN2I .get (" .strtab" );
639
- State.initStrtabSectionHeader (SHeaders[Index], " .strtab" , State.DotStrtab , CBA);
640
- Index = State.SN2I .get (" .shstrtab" );
641
- State.initStrtabSectionHeader (SHeaders[Index], " .shstrtab" , State.DotShStrtab , CBA);
621
+ // .symtab section.
622
+ Elf_Shdr SymtabSHeader;
623
+ State.initSymtabSectionHeader (SymtabSHeader, CBA);
624
+ SHeaders.push_back (SymtabSHeader);
625
+
626
+ // .strtab string table header.
627
+ Elf_Shdr DotStrTabSHeader;
628
+ State.initStrtabSectionHeader (DotStrTabSHeader, " .strtab" , State.DotStrtab ,
629
+ CBA);
630
+ SHeaders.push_back (DotStrTabSHeader);
631
+
632
+ // .shstrtab string table header.
633
+ Elf_Shdr ShStrTabSHeader;
634
+ State.initStrtabSectionHeader (ShStrTabSHeader, " .shstrtab" , State.DotShStrtab ,
635
+ CBA);
636
+ SHeaders.push_back (ShStrTabSHeader);
642
637
643
638
// Now we can decide segment offsets
644
639
State.setProgramHeaderLayout (PHeaders, SHeaders);
0 commit comments