llvm.org GIT mirror llvm / 14d1db9
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 Hans Wennborg 5 years ago
3 changed file(s) with 35 addition(s) and 53 deletion(s). Raw diff Collapse all Expand all
6565 # CHECK: }
6666 # CHECK: Section {
6767 # CHECK-NEXT: Index: 1
68 # CHECK-NEXT: Name: .text (1)
68 # CHECK-NEXT: Name: .text (16)
6969 # CHECK: }
7070 # CHECK-NEXT: Section {
7171 # CHECK-NEXT: Index: 2
72 # CHECK-NEXT: Name: .rel.text (7)
72 # CHECK-NEXT: Name: .rel.text (1)
7373 # CHECK-NEXT: Type: SHT_REL (0x9)
7474 # CHECK-NEXT: Flags [ (0x0)
7575 # CHECK-NEXT: ]
8383 # CHECK-NEXT: }
8484 # CHECK-NEXT: Section {
8585 # CHECK-NEXT: Index: 3
86 # CHECK-NEXT: Name: .rela.text (17)
86 # CHECK-NEXT: Name: .rela.text (11)
8787 # CHECK-NEXT: Type: SHT_RELA (0x4)
8888 # CHECK-NEXT: Flags [ (0x0)
8989 # CHECK-NEXT: ]
9797 # CHECK-NEXT: }
9898 # CHECK-NEXT: Section {
9999 # CHECK-NEXT: Index: 4
100 # CHECK-NEXT: Name: .symtab (28)
100 # CHECK-NEXT: Name: .symtab (40)
101101 # CHECK: }
102102 # CHECK-NEXT: Section {
103103 # CHECK-NEXT: Index: 5
104 # CHECK-NEXT: Name: .strtab (36)
104 # CHECK-NEXT: Name: .strtab (32)
105105 # CHECK: }
106106 # CHECK: Relocations [
107107 # CHECK-NEXT: Section (2) .rel.text {
3434 # CHECK-NEXT: )
3535 #
3636 # CHECK: Section {
37 # CHECK: Name: .symtab (7)
37 # CHECK: Name: .symtab (25)
3838 # CHECK: Type: SHT_SYMTAB (0x2)
3939 # CHECK: }
4040 # CHECK: Section {
41 # CHECK: Name: .strtab (15)
41 # CHECK: Name: .strtab (17)
4242 # CHECK: Type: SHT_STRTAB (0x3)
4343 # CHECK: }
4444 # CHECK: Section {
45 # CHECK: Name: .shstrtab (23)
45 # CHECK: Name: .shstrtab (7)
4646 # CHECK: Type: SHT_STRTAB (0x3)
4747 # CHECK: }
1515 #include "llvm/ADT/ArrayRef.h"
1616 #include "llvm/Object/ELFObjectFile.h"
1717 #include "llvm/Object/ELFYAML.h"
18 #include "llvm/Object/StringTableBuilder.h"
1819 #include "llvm/Support/ELF.h"
1920 #include "llvm/Support/MemoryBuffer.h"
2021 #include "llvm/Support/YAMLTraits.h"
2122 #include "llvm/Support/raw_ostream.h"
2223
2324 using namespace llvm;
24
25 // There is similar code in yaml2coff, but with some slight COFF-specific
26 // variations like different initial state. Might be able to deduplicate
27 // some day, but also want to make sure that the Mach-O use case is served.
28 //
29 // This class has a deliberately small interface, since a lot of
30 // implementation variation is possible.
31 //
32 // TODO: Use the StringTable builder from lib/Object instead, since it
33 // will deduplicate suffixes.
34 namespace {
35 class StringTableBuilder {
36 /// \brief Indices of strings currently present in `Buf`.
37 StringMap StringIndices;
38 /// \brief The contents of the string table as we build it.
39 std::string Buf;
40 public:
41 StringTableBuilder() {
42 Buf.push_back('\0');
43 }
44 /// \returns Index of string in string table.
45 unsigned addString(StringRef S) {
46 StringMapEntry &Entry = StringIndices.GetOrCreateValue(S);
47 unsigned &I = Entry.getValue();
48 if (I != 0)
49 return I;
50 I = Buf.size();
51 Buf.append(S.begin(), S.end());
52 Buf.push_back('\0');
53 return I;
54 }
55 size_t size() const {
56 return Buf.size();
57 }
58 void writeToStream(raw_ostream &OS) {
59 OS.write(Buf.data(), Buf.size());
60 }
61 };
62 } // end anonymous namespace
6325
6426 // This class is used to build up a contiguous binary blob while keeping
6527 // track of an offset in the output (which notionally begins at
225187 zero(SHeader);
226188 SHeaders.push_back(SHeader);
227189
190 for (const auto &Sec : Doc.Sections)
191 DotShStrtab.add(Sec->Name);
192 DotShStrtab.finalize();
193
228194 for (const auto &Sec : Doc.Sections) {
229195 zero(SHeader);
230 SHeader.sh_name = DotShStrtab.addString(Sec->Name);
196 SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
231197 SHeader.sh_type = Sec->Type;
232198 SHeader.sh_flags = Sec->Flags;
233199 SHeader.sh_addr = Sec->Address;
272238 void ELFState::initSymtabSectionHeader(Elf_Shdr &SHeader,
273239 ContiguousBlobAccumulator &CBA) {
274240 zero(SHeader);
275 SHeader.sh_name = DotShStrtab.addString(StringRef(".symtab"));
241 SHeader.sh_name = DotShStrtab.getOffset(".symtab");
276242 SHeader.sh_type = ELF::SHT_SYMTAB;
277243 SHeader.sh_link = getDotStrTabSecNo();
278244 // One greater than symbol table index of the last local symbol.
286252 zero(Sym);
287253 Syms.push_back(Sym);
288254 }
255
256 // Add symbol names to .strtab.
257 for (const auto &Sym : Doc.Symbols.Local)
258 DotStrtab.add(Sym.Name);
259 for (const auto &Sym : Doc.Symbols.Global)
260 DotStrtab.add(Sym.Name);
261 for (const auto &Sym : Doc.Symbols.Weak)
262 DotStrtab.add(Sym.Name);
263 DotStrtab.finalize();
264
289265 addSymbols(Doc.Symbols.Local, Syms, ELF::STB_LOCAL);
290266 addSymbols(Doc.Symbols.Global, Syms, ELF::STB_GLOBAL);
291267 addSymbols(Doc.Symbols.Weak, Syms, ELF::STB_WEAK);
300276 StringTableBuilder &STB,
301277 ContiguousBlobAccumulator &CBA) {
302278 zero(SHeader);
303 SHeader.sh_name = DotShStrtab.addString(Name);
279 SHeader.sh_name = DotShStrtab.getOffset(Name);
304280 SHeader.sh_type = ELF::SHT_STRTAB;
305 STB.writeToStream(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
306 SHeader.sh_size = STB.size();
281 CBA.getOSAndAlignedOffset(SHeader.sh_offset) << STB.data();
282 SHeader.sh_size = STB.data().size();
307283 SHeader.sh_addralign = 1;
308284 }
309285
315291 Elf_Sym Symbol;
316292 zero(Symbol);
317293 if (!Sym.Name.empty())
318 Symbol.st_name = DotStrtab.addString(Sym.Name);
294 Symbol.st_name = DotStrtab.getOffset(Sym.Name);
319295 Symbol.setBindingAndType(SymbolBinding, Sym.Type);
320296 if (!Sym.Section.empty()) {
321297 unsigned Index;
443419 const size_t SectionContentBeginOffset =
444420 Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
445421 ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
422
423 // Doc might not contain .symtab, .strtab and .shstrtab sections,
424 // but we will emit them, so make sure to add them to ShStrTabSHeader.
425 State.DotShStrtab.add(".symtab");
426 State.DotShStrtab.add(".strtab");
427 State.DotShStrtab.add(".shstrtab");
446428
447429 std::vector SHeaders;
448430 if(!State.initSectionHeaders(SHeaders, CBA))