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