llvm.org GIT mirror llvm / 068463b
[yaml2obj][ELF] Make symbol table top-level key. Although in reality the symbol table in ELF resides in a section, the standard requires that there be no more than one SHT_SYMTAB. To enforce this constraint, it is cleaner to group all the symbols under a top-level `Symbols` key on the object file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@184627 91177308-0d34-0410-b5e6-96231b3b80d8 Sean Silva 7 years ago
5 changed file(s) with 40 addition(s) and 43 deletion(s). Raw diff Collapse all Expand all
7171 object::yaml::BinaryRef Content;
7272 StringRef Link;
7373 llvm::yaml::Hex64 AddressAlign;
74 // For SHT_SYMTAB; should be empty otherwise.
75 LocalGlobalWeakSymbols Symbols;
7674 };
7775 struct Object {
7876 FileHeader Header;
7977 std::vector
Sections;
78 // Although in reality the symbols reside in a section, it is a lot
79 // cleaner and nicer if we read them from the YAML as a separate
80 // top-level key, which automatically ensures that invariants like there
81 // being a single SHT_SYMTAB section are upheld.
82 LocalGlobalWeakSymbols Symbols;
8083 };
8184
8285 } // end namespace ELFYAML
243243 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
244244 ECase(SHT_NULL)
245245 ECase(SHT_PROGBITS)
246 ECase(SHT_SYMTAB)
246 // No SHT_SYMTAB. Use the top-level `Symbols` key instead.
247 // FIXME: Issue a diagnostic with this information.
247248 ECase(SHT_STRTAB)
248249 ECase(SHT_RELA)
249250 ECase(SHT_HASH)
325326 IO.mapOptional("Content", Section.Content);
326327 IO.mapOptional("Link", Section.Link);
327328 IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
328 // TODO: Error if `Type` is SHT_SYMTAB and this is not present, or if
329 // `Type` is *not* SHT_SYMTAB and this *is* present. (By SHT_SYMTAB I
330 // also mean SHT_DYNSYM, but for simplicity right now we just do
331 // SHT_SYMTAB). Want to be able to share the predicate with consumers of
332 // this structure.
333 IO.mapOptional("Symbols", Section.Symbols);
334329 }
335330
336331 void MappingTraits::mapping(IO &IO, ELFYAML::Object &Object) {
337332 IO.mapRequired("FileHeader", Object.Header);
338333 IO.mapOptional("Sections", Object.Sections);
334 IO.mapOptional("Symbols", Object.Symbols);
339335 }
340336
341337 } // end namespace yaml
99 Type: SHT_PROGBITS
1010 Flags: [ SHF_ALLOC, SHF_WRITE ]
1111 Content: "DEADBEEF"
12 - Name: .symtab
13 Type: SHT_SYMTAB
14 Symbols:
15 Local:
16 - Name: local_symbol
17 Type: STT_OBJECT
18 Section: .data
19 Global:
20 - Name: global_symbol
21 Type: STT_OBJECT
22 Section: .data
23 Weak:
24 - Name: weak_symbol
25 Type: STT_OBJECT
26 Section: .data
12 Symbols:
13 Local:
14 - Name: local_symbol
15 Type: STT_OBJECT
16 Section: .data
17 Global:
18 - Name: global_symbol
19 Type: STT_OBJECT
20 Section: .data
21 Weak:
22 - Name: weak_symbol
23 Type: STT_OBJECT
24 Section: .data
2725
2826 # CHECK: Symbol {
2927 # CHECK: Name: (0)
1515 # This YAML file is a valid relocatable object that,
1616 # when linked and run on x86_64, will go into an
1717 # infloop.
18 - Name: .symtab
19 Type: SHT_SYMTAB
20 Symbols:
21 Global:
22 - Name: main
23 Type: STT_FUNC
24 Section: .text
25 Value: 0x1
26 Size: 2
27 - Name: undefined_symbol
18 Symbols:
19 Global:
20 - Name: main
21 Type: STT_FUNC
22 Section: .text
23 Value: 0x1
24 Size: 2
25 - Name: undefined_symbol
2826
2927 # CHECK: Symbols [
3028 # CHECK-NEXT: Symbol {
215215 typename object::ELFObjectFile::Elf_Shdr &SHeader) {
216216
217217 typedef typename object::ELFObjectFile::Elf_Sym Elf_Sym;
218 // TODO: Ensure that a manually specified `Link` field is diagnosed as an
219 // error for SHT_SYMTAB.
218 SHeader.sh_type = ELF::SHT_SYMTAB;
220219 SHeader.sh_link = State.getDotStrTabSecNo();
221220 // One greater than symbol table index of the last local symbol.
222221 SHeader.sh_info = Symbols.Local.size() + 1;
271270 // Immediately following the ELF header.
272271 Header.e_shoff = sizeof(Header);
273272 const std::vector &Sections = Doc.Sections;
274 // "+ 3" for
273 // "+ 4" for
275274 // - SHT_NULL entry (placed first, i.e. 0'th entry)
275 // - symbol table (.symtab) (placed third to last)
276276 // - string table (.strtab) (placed second to last)
277277 // - section header string table. (placed last)
278 Header.e_shnum = Sections.size() + 3;
278 Header.e_shnum = Sections.size() + 4;
279279 // Place section header string table last.
280280 Header.e_shstrndx = Header.e_shnum - 1;
281281 const unsigned DotStrtabSecNo = Header.e_shnum - 2;
333333 SHeader.sh_info = 0;
334334 SHeader.sh_addralign = Sec.AddressAlign;
335335 SHeader.sh_entsize = 0;
336 // XXX: Really ugly right now. Should not be writing to `CBA` above
337 // (and setting sh_offset and sh_size) when going through this branch
338 // here.
339 if (Sec.Type == ELFYAML::ELF_SHT(SHT_SYMTAB))
340 handleSymtabSectionHeader(Sec.Symbols, State, SHeader);
341336 SHeaders.push_back(SHeader);
342337 }
338
339 // .symtab section.
340 Elf_Shdr SymtabSHeader;
341 zero(SymtabSHeader);
342 SymtabSHeader.sh_name = SHStrTab.addString(StringRef(".symtab"));
343 handleSymtabSectionHeader(Doc.Symbols, State, SymtabSHeader);
344 SHeaders.push_back(SymtabSHeader);
343345
344346 // .strtab string table header.
345347 Elf_Shdr DotStrTabSHeader;