16
16
#include " llvm/ADT/ArrayRef.h"
17
17
#include " llvm/Object/ELFObjectFile.h"
18
18
#include " llvm/Object/ELFYAML.h"
19
+ #include " llvm/Object/StringTableBuilder.h"
19
20
#include " llvm/Support/ELF.h"
20
21
#include " llvm/Support/MemoryBuffer.h"
21
22
#include " llvm/Support/YAMLTraits.h"
22
23
#include " llvm/Support/raw_ostream.h"
23
24
24
25
using namespace llvm ;
25
26
26
- // There is similar code in yaml2coff, but with some slight COFF-specific
27
- // variations like different initial state. Might be able to deduplicate
28
- // some day, but also want to make sure that the Mach-O use case is served.
29
- //
30
- // This class has a deliberately small interface, since a lot of
31
- // implementation variation is possible.
32
- //
33
- // TODO: Use the StringTable builder from lib/Object instead, since it
34
- // will deduplicate suffixes.
35
- namespace {
36
- class StringTableBuilder {
37
- // / \brief Indices of strings currently present in `Buf`.
38
- StringMap<unsigned > StringIndices;
39
- // / \brief The contents of the string table as we build it.
40
- std::string Buf;
41
- public:
42
- StringTableBuilder () {
43
- Buf.push_back (' \0 ' );
44
- }
45
- // / \returns Index of string in string table.
46
- unsigned addString (StringRef S) {
47
- StringMapEntry<unsigned > &Entry = StringIndices.GetOrCreateValue (S);
48
- unsigned &I = Entry.getValue ();
49
- if (I != 0 )
50
- return I;
51
- I = Buf.size ();
52
- Buf.append (S.begin (), S.end ());
53
- Buf.push_back (' \0 ' );
54
- return I;
55
- }
56
- size_t size () const {
57
- return Buf.size ();
58
- }
59
- void writeToStream (raw_ostream &OS) {
60
- OS.write (Buf.data (), Buf.size ());
61
- }
62
- };
63
- } // end anonymous namespace
64
-
65
27
// This class is used to build up a contiguous binary blob while keeping
66
28
// track of an offset in the output (which notionally begins at
67
29
// `InitialOffset`).
@@ -226,9 +188,13 @@ bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
226
188
zero (SHeader);
227
189
SHeaders.push_back (SHeader);
228
190
191
+ for (const auto &Sec : Doc.Sections )
192
+ DotShStrtab.add (Sec->Name );
193
+ DotShStrtab.finalize ();
194
+
229
195
for (const auto &Sec : Doc.Sections ) {
230
196
zero (SHeader);
231
- SHeader.sh_name = DotShStrtab.addString (Sec->Name );
197
+ SHeader.sh_name = DotShStrtab.getOffset (Sec->Name );
232
198
SHeader.sh_type = Sec->Type ;
233
199
SHeader.sh_flags = Sec->Flags ;
234
200
SHeader.sh_addr = Sec->Address ;
@@ -273,7 +239,7 @@ template <class ELFT>
273
239
void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
274
240
ContiguousBlobAccumulator &CBA) {
275
241
zero (SHeader);
276
- SHeader.sh_name = DotShStrtab.addString ( StringRef ( " .symtab" ) );
242
+ SHeader.sh_name = DotShStrtab.getOffset ( " .symtab" );
277
243
SHeader.sh_type = ELF::SHT_SYMTAB;
278
244
SHeader.sh_link = getDotStrTabSecNo ();
279
245
// One greater than symbol table index of the last local symbol.
@@ -287,6 +253,16 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
287
253
zero (Sym);
288
254
Syms.push_back (Sym);
289
255
}
256
+
257
+ // Add symbol names to .strtab.
258
+ for (const auto &Sym : Doc.Symbols .Local )
259
+ DotStrtab.add (Sym.Name );
260
+ for (const auto &Sym : Doc.Symbols .Global )
261
+ DotStrtab.add (Sym.Name );
262
+ for (const auto &Sym : Doc.Symbols .Weak )
263
+ DotStrtab.add (Sym.Name );
264
+ DotStrtab.finalize ();
265
+
290
266
addSymbols (Doc.Symbols .Local , Syms, ELF::STB_LOCAL);
291
267
addSymbols (Doc.Symbols .Global , Syms, ELF::STB_GLOBAL);
292
268
addSymbols (Doc.Symbols .Weak , Syms, ELF::STB_WEAK);
@@ -301,10 +277,10 @@ void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
301
277
StringTableBuilder &STB,
302
278
ContiguousBlobAccumulator &CBA) {
303
279
zero (SHeader);
304
- SHeader.sh_name = DotShStrtab.addString (Name);
280
+ SHeader.sh_name = DotShStrtab.getOffset (Name);
305
281
SHeader.sh_type = ELF::SHT_STRTAB;
306
- STB. writeToStream ( CBA.getOSAndAlignedOffset (SHeader.sh_offset ));
307
- SHeader.sh_size = STB.size ();
282
+ CBA.getOSAndAlignedOffset (SHeader.sh_offset ) << STB. data ( );
283
+ SHeader.sh_size = STB.data (). size ();
308
284
SHeader.sh_addralign = 1 ;
309
285
}
310
286
@@ -316,7 +292,7 @@ void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
316
292
Elf_Sym Symbol;
317
293
zero (Symbol);
318
294
if (!Sym.Name .empty ())
319
- Symbol.st_name = DotStrtab.addString (Sym.Name );
295
+ Symbol.st_name = DotStrtab.getOffset (Sym.Name );
320
296
Symbol.setBindingAndType (SymbolBinding, Sym.Type );
321
297
if (!Sym.Section .empty ()) {
322
298
unsigned Index;
@@ -445,6 +421,12 @@ int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
445
421
Header.e_ehsize + Header.e_shentsize * Header.e_shnum ;
446
422
ContiguousBlobAccumulator CBA (SectionContentBeginOffset);
447
423
424
+ // Doc might not contain .symtab, .strtab and .shstrtab sections,
425
+ // but we will emit them, so make sure to add them to ShStrTabSHeader.
426
+ State.DotShStrtab .add (" .symtab" );
427
+ State.DotShStrtab .add (" .strtab" );
428
+ State.DotShStrtab .add (" .shstrtab" );
429
+
448
430
std::vector<Elf_Shdr> SHeaders;
449
431
if (!State.initSectionHeaders (SHeaders, CBA))
450
432
return 1 ;
0 commit comments