Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 14d1db9

Browse files
committed
Use the new StringTableBuilder in yaml2elf
http://reviews.llvm.org/D3574 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@207694 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 6843898 commit 14d1db9

File tree

3 files changed

+35
-53
lines changed

3 files changed

+35
-53
lines changed

test/Object/yaml2obj-elf-rel.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ Symbols:
6666
# CHECK: }
6767
# CHECK: Section {
6868
# CHECK-NEXT: Index: 1
69-
# CHECK-NEXT: Name: .text (1)
69+
# CHECK-NEXT: Name: .text (16)
7070
# CHECK: }
7171
# CHECK-NEXT: Section {
7272
# CHECK-NEXT: Index: 2
73-
# CHECK-NEXT: Name: .rel.text (7)
73+
# CHECK-NEXT: Name: .rel.text (1)
7474
# CHECK-NEXT: Type: SHT_REL (0x9)
7575
# CHECK-NEXT: Flags [ (0x0)
7676
# CHECK-NEXT: ]
@@ -84,7 +84,7 @@ Symbols:
8484
# CHECK-NEXT: }
8585
# CHECK-NEXT: Section {
8686
# CHECK-NEXT: Index: 3
87-
# CHECK-NEXT: Name: .rela.text (17)
87+
# CHECK-NEXT: Name: .rela.text (11)
8888
# CHECK-NEXT: Type: SHT_RELA (0x4)
8989
# CHECK-NEXT: Flags [ (0x0)
9090
# CHECK-NEXT: ]
@@ -98,11 +98,11 @@ Symbols:
9898
# CHECK-NEXT: }
9999
# CHECK-NEXT: Section {
100100
# CHECK-NEXT: Index: 4
101-
# CHECK-NEXT: Name: .symtab (28)
101+
# CHECK-NEXT: Name: .symtab (40)
102102
# CHECK: }
103103
# CHECK-NEXT: Section {
104104
# CHECK-NEXT: Index: 5
105-
# CHECK-NEXT: Name: .strtab (36)
105+
# CHECK-NEXT: Name: .strtab (32)
106106
# CHECK: }
107107
# CHECK: Relocations [
108108
# CHECK-NEXT: Section (2) .rel.text {

test/Object/yaml2obj-elf-section-basic.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ Sections:
3535
# CHECK-NEXT: )
3636
#
3737
# CHECK: Section {
38-
# CHECK: Name: .symtab (7)
38+
# CHECK: Name: .symtab (25)
3939
# CHECK: Type: SHT_SYMTAB (0x2)
4040
# CHECK: }
4141
# CHECK: Section {
42-
# CHECK: Name: .strtab (15)
42+
# CHECK: Name: .strtab (17)
4343
# CHECK: Type: SHT_STRTAB (0x3)
4444
# CHECK: }
4545
# CHECK: Section {
46-
# CHECK: Name: .shstrtab (23)
46+
# CHECK: Name: .shstrtab (7)
4747
# CHECK: Type: SHT_STRTAB (0x3)
4848
# CHECK: }

tools/yaml2obj/yaml2elf.cpp

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,14 @@
1616
#include "llvm/ADT/ArrayRef.h"
1717
#include "llvm/Object/ELFObjectFile.h"
1818
#include "llvm/Object/ELFYAML.h"
19+
#include "llvm/Object/StringTableBuilder.h"
1920
#include "llvm/Support/ELF.h"
2021
#include "llvm/Support/MemoryBuffer.h"
2122
#include "llvm/Support/YAMLTraits.h"
2223
#include "llvm/Support/raw_ostream.h"
2324

2425
using namespace llvm;
2526

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-
6527
// This class is used to build up a contiguous binary blob while keeping
6628
// track of an offset in the output (which notionally begins at
6729
// `InitialOffset`).
@@ -226,9 +188,13 @@ bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
226188
zero(SHeader);
227189
SHeaders.push_back(SHeader);
228190

191+
for (const auto &Sec : Doc.Sections)
192+
DotShStrtab.add(Sec->Name);
193+
DotShStrtab.finalize();
194+
229195
for (const auto &Sec : Doc.Sections) {
230196
zero(SHeader);
231-
SHeader.sh_name = DotShStrtab.addString(Sec->Name);
197+
SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
232198
SHeader.sh_type = Sec->Type;
233199
SHeader.sh_flags = Sec->Flags;
234200
SHeader.sh_addr = Sec->Address;
@@ -273,7 +239,7 @@ template <class ELFT>
273239
void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
274240
ContiguousBlobAccumulator &CBA) {
275241
zero(SHeader);
276-
SHeader.sh_name = DotShStrtab.addString(StringRef(".symtab"));
242+
SHeader.sh_name = DotShStrtab.getOffset(".symtab");
277243
SHeader.sh_type = ELF::SHT_SYMTAB;
278244
SHeader.sh_link = getDotStrTabSecNo();
279245
// One greater than symbol table index of the last local symbol.
@@ -287,6 +253,16 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
287253
zero(Sym);
288254
Syms.push_back(Sym);
289255
}
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+
290266
addSymbols(Doc.Symbols.Local, Syms, ELF::STB_LOCAL);
291267
addSymbols(Doc.Symbols.Global, Syms, ELF::STB_GLOBAL);
292268
addSymbols(Doc.Symbols.Weak, Syms, ELF::STB_WEAK);
@@ -301,10 +277,10 @@ void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
301277
StringTableBuilder &STB,
302278
ContiguousBlobAccumulator &CBA) {
303279
zero(SHeader);
304-
SHeader.sh_name = DotShStrtab.addString(Name);
280+
SHeader.sh_name = DotShStrtab.getOffset(Name);
305281
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();
308284
SHeader.sh_addralign = 1;
309285
}
310286

@@ -316,7 +292,7 @@ void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
316292
Elf_Sym Symbol;
317293
zero(Symbol);
318294
if (!Sym.Name.empty())
319-
Symbol.st_name = DotStrtab.addString(Sym.Name);
295+
Symbol.st_name = DotStrtab.getOffset(Sym.Name);
320296
Symbol.setBindingAndType(SymbolBinding, Sym.Type);
321297
if (!Sym.Section.empty()) {
322298
unsigned Index;
@@ -445,6 +421,12 @@ int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
445421
Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
446422
ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
447423

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+
448430
std::vector<Elf_Shdr> SHeaders;
449431
if(!State.initSectionHeaders(SHeaders, CBA))
450432
return 1;

0 commit comments

Comments
 (0)