1 //===------ utils/elf2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/DenseSet.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Object/ELFObjectFile.h"
13 #include "llvm/ObjectYAML/ELFYAML.h"
14 #include "llvm/Support/DataExtractor.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/YAMLTraits.h"
24 typedef object::Elf_Sym_Impl
<ELFT
> Elf_Sym
;
25 typedef typename
ELFT::Dyn Elf_Dyn
;
26 typedef typename
ELFT::Shdr Elf_Shdr
;
27 typedef typename
ELFT::Word Elf_Word
;
28 typedef typename
ELFT::Rel Elf_Rel
;
29 typedef typename
ELFT::Rela Elf_Rela
;
31 ArrayRef
<Elf_Shdr
> Sections
;
32 ArrayRef
<Elf_Sym
> SymTable
;
34 DenseMap
<StringRef
, uint32_t> UsedSectionNames
;
35 std::vector
<std::string
> SectionNames
;
37 DenseMap
<StringRef
, uint32_t> UsedSymbolNames
;
38 std::vector
<std::string
> SymbolNames
;
40 Expected
<StringRef
> getUniquedSectionName(const Elf_Shdr
*Sec
);
41 Expected
<StringRef
> getUniquedSymbolName(const Elf_Sym
*Sym
,
43 const Elf_Shdr
*SymTab
);
44 Expected
<StringRef
> getSymbolName(uint32_t SymtabNdx
, uint32_t SymbolNdx
);
46 const object::ELFFile
<ELFT
> &Obj
;
47 ArrayRef
<Elf_Word
> ShndxTable
;
49 Error
dumpSymbols(const Elf_Shdr
*Symtab
,
50 std::vector
<ELFYAML::Symbol
> &Symbols
);
51 Error
dumpSymbol(const Elf_Sym
*Sym
, const Elf_Shdr
*SymTab
,
52 StringRef StrTable
, ELFYAML::Symbol
&S
);
53 Error
dumpCommonSection(const Elf_Shdr
*Shdr
, ELFYAML::Section
&S
);
54 Error
dumpCommonRelocationSection(const Elf_Shdr
*Shdr
,
55 ELFYAML::RelocationSection
&S
);
57 Error
dumpRelocation(const RelT
*Rel
, const Elf_Shdr
*SymTab
,
58 ELFYAML::Relocation
&R
);
60 Expected
<ELFYAML::AddrsigSection
*> dumpAddrsigSection(const Elf_Shdr
*Shdr
);
61 Expected
<ELFYAML::DynamicSection
*> dumpDynamicSection(const Elf_Shdr
*Shdr
);
62 Expected
<ELFYAML::RelocationSection
*> dumpRelocSection(const Elf_Shdr
*Shdr
);
63 Expected
<ELFYAML::RawContentSection
*>
64 dumpContentSection(const Elf_Shdr
*Shdr
);
65 Expected
<ELFYAML::SymtabShndxSection
*>
66 dumpSymtabShndxSection(const Elf_Shdr
*Shdr
);
67 Expected
<ELFYAML::NoBitsSection
*> dumpNoBitsSection(const Elf_Shdr
*Shdr
);
68 Expected
<ELFYAML::HashSection
*> dumpHashSection(const Elf_Shdr
*Shdr
);
69 Expected
<ELFYAML::VerdefSection
*> dumpVerdefSection(const Elf_Shdr
*Shdr
);
70 Expected
<ELFYAML::SymverSection
*> dumpSymverSection(const Elf_Shdr
*Shdr
);
71 Expected
<ELFYAML::VerneedSection
*> dumpVerneedSection(const Elf_Shdr
*Shdr
);
72 Expected
<ELFYAML::Group
*> dumpGroup(const Elf_Shdr
*Shdr
);
73 Expected
<ELFYAML::MipsABIFlags
*> dumpMipsABIFlags(const Elf_Shdr
*Shdr
);
74 Expected
<ELFYAML::StackSizesSection
*>
75 dumpStackSizesSection(const Elf_Shdr
*Shdr
);
77 Expected
<ELFYAML::Section
*> dumpSpecialSection(const Elf_Shdr
*Shdr
);
80 ELFDumper(const object::ELFFile
<ELFT
> &O
);
81 Expected
<ELFYAML::Object
*> dump();
87 ELFDumper
<ELFT
>::ELFDumper(const object::ELFFile
<ELFT
> &O
)
92 ELFDumper
<ELFT
>::getUniquedSectionName(const Elf_Shdr
*Sec
) {
93 unsigned SecIndex
= Sec
- &Sections
[0];
94 assert(&Sections
[SecIndex
] == Sec
);
95 if (!SectionNames
[SecIndex
].empty())
96 return SectionNames
[SecIndex
];
98 auto NameOrErr
= Obj
.getSectionName(Sec
);
101 StringRef Name
= *NameOrErr
;
102 std::string
&Ret
= SectionNames
[SecIndex
];
104 auto It
= UsedSectionNames
.insert({Name
, 0});
106 Ret
= (Name
+ " [" + Twine(++It
.first
->second
) + "]").str();
112 template <class ELFT
>
114 ELFDumper
<ELFT
>::getUniquedSymbolName(const Elf_Sym
*Sym
, StringRef StrTable
,
115 const Elf_Shdr
*SymTab
) {
116 Expected
<StringRef
> SymbolNameOrErr
= Sym
->getName(StrTable
);
117 if (!SymbolNameOrErr
)
118 return SymbolNameOrErr
;
119 StringRef Name
= *SymbolNameOrErr
;
120 if (Name
.empty() && Sym
->getType() == ELF::STT_SECTION
) {
121 auto ShdrOrErr
= Obj
.getSection(Sym
, SymTab
, ShndxTable
);
123 return ShdrOrErr
.takeError();
124 return getUniquedSectionName(*ShdrOrErr
);
127 // Symbols in .symtab can have duplicate names. For example, it is a common
128 // situation for local symbols in a relocatable object. Here we assign unique
129 // suffixes for such symbols so that we can differentiate them.
130 if (SymTab
->sh_type
== ELF::SHT_SYMTAB
) {
131 unsigned Index
= Sym
- SymTable
.data();
132 if (!SymbolNames
[Index
].empty())
133 return SymbolNames
[Index
];
135 auto It
= UsedSymbolNames
.insert({Name
, 0});
138 (Name
+ " [" + Twine(++It
.first
->second
) + "]").str();
140 SymbolNames
[Index
] = Name
;
141 return SymbolNames
[Index
];
147 template <class ELFT
> Expected
<ELFYAML::Object
*> ELFDumper
<ELFT
>::dump() {
148 auto Y
= std::make_unique
<ELFYAML::Object
>();
150 // Dump header. We do not dump SHEntSize, SHOff, SHNum and SHStrNdx fields.
151 // When not explicitly set, the values are set by yaml2obj automatically
152 // and there is no need to dump them here.
153 Y
->Header
.Class
= ELFYAML::ELF_ELFCLASS(Obj
.getHeader()->getFileClass());
154 Y
->Header
.Data
= ELFYAML::ELF_ELFDATA(Obj
.getHeader()->getDataEncoding());
155 Y
->Header
.OSABI
= Obj
.getHeader()->e_ident
[ELF::EI_OSABI
];
156 Y
->Header
.ABIVersion
= Obj
.getHeader()->e_ident
[ELF::EI_ABIVERSION
];
157 Y
->Header
.Type
= Obj
.getHeader()->e_type
;
158 Y
->Header
.Machine
= Obj
.getHeader()->e_machine
;
159 Y
->Header
.Flags
= Obj
.getHeader()->e_flags
;
160 Y
->Header
.Entry
= Obj
.getHeader()->e_entry
;
163 auto SectionsOrErr
= Obj
.sections();
165 return SectionsOrErr
.takeError();
166 Sections
= *SectionsOrErr
;
167 SectionNames
.resize(Sections
.size());
169 // Dump symbols. We need to do this early because other sections might want
170 // to access the deduplicated symbol names that we also create here.
171 const Elf_Shdr
*SymTab
= nullptr;
172 const Elf_Shdr
*SymTabShndx
= nullptr;
173 const Elf_Shdr
*DynSymTab
= nullptr;
175 for (const Elf_Shdr
&Sec
: Sections
) {
176 if (Sec
.sh_type
== ELF::SHT_SYMTAB
) {
178 } else if (Sec
.sh_type
== ELF::SHT_DYNSYM
) {
180 } else if (Sec
.sh_type
== ELF::SHT_SYMTAB_SHNDX
) {
181 // ABI allows us to have one SHT_SYMTAB_SHNDX for each symbol table.
182 // We only support having the SHT_SYMTAB_SHNDX for SHT_SYMTAB now.
184 return createStringError(obj2yaml_error::not_implemented
,
185 "multiple SHT_SYMTAB_SHNDX sections are not supported");
190 // We need to locate the SHT_SYMTAB_SHNDX section early, because it might be
191 // needed for dumping symbols.
193 if (!SymTab
|| SymTabShndx
->sh_link
!= SymTab
- Sections
.begin())
194 return createStringError(
195 obj2yaml_error::not_implemented
,
196 "only SHT_SYMTAB_SHNDX associated with SHT_SYMTAB are supported");
198 auto TableOrErr
= Obj
.getSHNDXTable(*SymTabShndx
);
200 return TableOrErr
.takeError();
201 ShndxTable
= *TableOrErr
;
204 if (Error E
= dumpSymbols(SymTab
, Y
->Symbols
))
207 if (Error E
= dumpSymbols(DynSymTab
, Y
->DynamicSymbols
))
210 for (const Elf_Shdr
&Sec
: Sections
) {
211 switch (Sec
.sh_type
) {
212 case ELF::SHT_DYNAMIC
: {
213 Expected
<ELFYAML::DynamicSection
*> SecOrErr
= dumpDynamicSection(&Sec
);
215 return SecOrErr
.takeError();
216 Y
->Sections
.emplace_back(*SecOrErr
);
219 case ELF::SHT_STRTAB
:
220 case ELF::SHT_SYMTAB
:
221 case ELF::SHT_DYNSYM
:
222 // Do not dump these sections.
224 case ELF::SHT_SYMTAB_SHNDX
: {
225 Expected
<ELFYAML::SymtabShndxSection
*> SecOrErr
=
226 dumpSymtabShndxSection(&Sec
);
228 return SecOrErr
.takeError();
229 Y
->Sections
.emplace_back(*SecOrErr
);
233 case ELF::SHT_RELA
: {
234 Expected
<ELFYAML::RelocationSection
*> SecOrErr
= dumpRelocSection(&Sec
);
236 return SecOrErr
.takeError();
237 Y
->Sections
.emplace_back(*SecOrErr
);
240 case ELF::SHT_GROUP
: {
241 Expected
<ELFYAML::Group
*> GroupOrErr
= dumpGroup(&Sec
);
243 return GroupOrErr
.takeError();
244 Y
->Sections
.emplace_back(*GroupOrErr
);
247 case ELF::SHT_MIPS_ABIFLAGS
: {
248 Expected
<ELFYAML::MipsABIFlags
*> SecOrErr
= dumpMipsABIFlags(&Sec
);
250 return SecOrErr
.takeError();
251 Y
->Sections
.emplace_back(*SecOrErr
);
254 case ELF::SHT_NOBITS
: {
255 Expected
<ELFYAML::NoBitsSection
*> SecOrErr
= dumpNoBitsSection(&Sec
);
257 return SecOrErr
.takeError();
258 Y
->Sections
.emplace_back(*SecOrErr
);
261 case ELF::SHT_HASH
: {
262 Expected
<ELFYAML::HashSection
*> SecOrErr
= dumpHashSection(&Sec
);
264 return SecOrErr
.takeError();
265 Y
->Sections
.emplace_back(*SecOrErr
);
268 case ELF::SHT_GNU_verdef
: {
269 Expected
<ELFYAML::VerdefSection
*> SecOrErr
= dumpVerdefSection(&Sec
);
271 return SecOrErr
.takeError();
272 Y
->Sections
.emplace_back(*SecOrErr
);
275 case ELF::SHT_GNU_versym
: {
276 Expected
<ELFYAML::SymverSection
*> SecOrErr
= dumpSymverSection(&Sec
);
278 return SecOrErr
.takeError();
279 Y
->Sections
.emplace_back(*SecOrErr
);
282 case ELF::SHT_GNU_verneed
: {
283 Expected
<ELFYAML::VerneedSection
*> SecOrErr
= dumpVerneedSection(&Sec
);
285 return SecOrErr
.takeError();
286 Y
->Sections
.emplace_back(*SecOrErr
);
289 case ELF::SHT_LLVM_ADDRSIG
: {
290 Expected
<ELFYAML::AddrsigSection
*> SecOrErr
= dumpAddrsigSection(&Sec
);
292 return SecOrErr
.takeError();
293 Y
->Sections
.emplace_back(*SecOrErr
);
296 case ELF::SHT_NULL
: {
297 // We only dump the SHT_NULL section at index 0 when it
298 // has at least one non-null field, because yaml2obj
299 // normally creates the zero section at index 0 implicitly.
300 if (&Sec
== &Sections
[0]) {
301 const uint8_t *Begin
= reinterpret_cast<const uint8_t *>(&Sec
);
302 const uint8_t *End
= Begin
+ sizeof(Elf_Shdr
);
303 if (std::find_if(Begin
, End
, [](uint8_t V
) { return V
!= 0; }) == End
)
309 // Recognize some special SHT_PROGBITS sections by name.
310 if (Sec
.sh_type
== ELF::SHT_PROGBITS
) {
311 Expected
<ELFYAML::Section
*> SpecialSecOrErr
= dumpSpecialSection(&Sec
);
312 if (!SpecialSecOrErr
)
313 return SpecialSecOrErr
.takeError();
314 if (*SpecialSecOrErr
) {
315 Y
->Sections
.emplace_back(*SpecialSecOrErr
);
320 Expected
<ELFYAML::RawContentSection
*> SecOrErr
=
321 dumpContentSection(&Sec
);
323 return SecOrErr
.takeError();
324 Y
->Sections
.emplace_back(*SecOrErr
);
332 template <class ELFT
>
333 Error ELFDumper
<ELFT
>::dumpSymbols(const Elf_Shdr
*Symtab
,
334 std::vector
<ELFYAML::Symbol
> &Symbols
) {
336 return Error::success();
338 auto StrTableOrErr
= Obj
.getStringTableForSymtab(*Symtab
);
340 return StrTableOrErr
.takeError();
341 StringRef StrTable
= *StrTableOrErr
;
343 auto SymtabOrErr
= Obj
.symbols(Symtab
);
345 return SymtabOrErr
.takeError();
347 if (Symtab
->sh_type
== ELF::SHT_SYMTAB
) {
348 SymTable
= *SymtabOrErr
;
349 SymbolNames
.resize(SymTable
.size());
352 for (const auto &Sym
: (*SymtabOrErr
).drop_front()) {
354 if (auto EC
= dumpSymbol(&Sym
, Symtab
, StrTable
, S
))
356 Symbols
.push_back(S
);
359 return Error::success();
362 template <class ELFT
>
363 Error ELFDumper
<ELFT
>::dumpSymbol(const Elf_Sym
*Sym
, const Elf_Shdr
*SymTab
,
364 StringRef StrTable
, ELFYAML::Symbol
&S
) {
365 S
.Type
= Sym
->getType();
366 S
.Value
= Sym
->st_value
;
367 S
.Size
= Sym
->st_size
;
368 S
.Other
= Sym
->st_other
;
369 S
.Binding
= Sym
->getBinding();
371 Expected
<StringRef
> SymbolNameOrErr
=
372 getUniquedSymbolName(Sym
, StrTable
, SymTab
);
373 if (!SymbolNameOrErr
)
374 return SymbolNameOrErr
.takeError();
375 S
.Name
= SymbolNameOrErr
.get();
377 if (Sym
->st_shndx
>= ELF::SHN_LORESERVE
) {
378 S
.Index
= (ELFYAML::ELF_SHN
)Sym
->st_shndx
;
379 return Error::success();
382 auto ShdrOrErr
= Obj
.getSection(Sym
, SymTab
, ShndxTable
);
384 return ShdrOrErr
.takeError();
385 const Elf_Shdr
*Shdr
= *ShdrOrErr
;
387 return Error::success();
389 auto NameOrErr
= getUniquedSectionName(Shdr
);
391 return NameOrErr
.takeError();
392 S
.Section
= NameOrErr
.get();
394 return Error::success();
397 template <class ELFT
>
398 template <class RelT
>
399 Error ELFDumper
<ELFT
>::dumpRelocation(const RelT
*Rel
, const Elf_Shdr
*SymTab
,
400 ELFYAML::Relocation
&R
) {
401 R
.Type
= Rel
->getType(Obj
.isMips64EL());
402 R
.Offset
= Rel
->r_offset
;
405 auto SymOrErr
= Obj
.getRelocationSymbol(Rel
, SymTab
);
407 return SymOrErr
.takeError();
408 const Elf_Sym
*Sym
= *SymOrErr
;
409 auto StrTabSec
= Obj
.getSection(SymTab
->sh_link
);
411 return StrTabSec
.takeError();
412 auto StrTabOrErr
= Obj
.getStringTable(*StrTabSec
);
414 return StrTabOrErr
.takeError();
415 StringRef StrTab
= *StrTabOrErr
;
418 Expected
<StringRef
> NameOrErr
= getUniquedSymbolName(Sym
, StrTab
, SymTab
);
420 return NameOrErr
.takeError();
421 R
.Symbol
= NameOrErr
.get();
423 // We have some edge cases of relocations without a symbol associated,
424 // e.g. an object containing the invalid (according to the System V
425 // ABI) R_X86_64_NONE reloc. Create a symbol with an empty name instead
430 return Error::success();
433 template <class ELFT
>
434 Error ELFDumper
<ELFT
>::dumpCommonSection(const Elf_Shdr
*Shdr
,
435 ELFYAML::Section
&S
) {
436 // Dump fields. We do not dump the ShOffset field. When not explicitly
437 // set, the value is set by yaml2obj automatically.
438 S
.Type
= Shdr
->sh_type
;
440 S
.Flags
= static_cast<ELFYAML::ELF_SHF
>(Shdr
->sh_flags
);
441 S
.Address
= Shdr
->sh_addr
;
442 S
.AddressAlign
= Shdr
->sh_addralign
;
443 if (Shdr
->sh_entsize
)
444 S
.EntSize
= static_cast<llvm::yaml::Hex64
>(Shdr
->sh_entsize
);
446 auto NameOrErr
= getUniquedSectionName(Shdr
);
448 return NameOrErr
.takeError();
449 S
.Name
= NameOrErr
.get();
451 if (Shdr
->sh_link
!= ELF::SHN_UNDEF
) {
452 auto LinkSection
= Obj
.getSection(Shdr
->sh_link
);
454 return make_error
<StringError
>(
455 "unable to resolve sh_link reference in section '" + S
.Name
+
456 "': " + toString(LinkSection
.takeError()),
457 inconvertibleErrorCode());
459 NameOrErr
= getUniquedSectionName(*LinkSection
);
461 return NameOrErr
.takeError();
462 S
.Link
= NameOrErr
.get();
465 return Error::success();
468 template <class ELFT
>
469 Expected
<ELFYAML::Section
*>
470 ELFDumper
<ELFT
>::dumpSpecialSection(const Elf_Shdr
*Shdr
) {
471 auto NameOrErr
= getUniquedSectionName(Shdr
);
473 return NameOrErr
.takeError();
475 if (ELFYAML::StackSizesSection::nameMatches(*NameOrErr
))
476 return dumpStackSizesSection(Shdr
);
480 template <class ELFT
>
481 Error ELFDumper
<ELFT
>::dumpCommonRelocationSection(
482 const Elf_Shdr
*Shdr
, ELFYAML::RelocationSection
&S
) {
483 if (Error E
= dumpCommonSection(Shdr
, S
))
486 auto InfoSection
= Obj
.getSection(Shdr
->sh_info
);
488 return InfoSection
.takeError();
490 auto NameOrErr
= getUniquedSectionName(*InfoSection
);
492 return NameOrErr
.takeError();
493 S
.RelocatableSec
= NameOrErr
.get();
495 return Error::success();
498 template <class ELFT
>
499 Expected
<ELFYAML::StackSizesSection
*>
500 ELFDumper
<ELFT
>::dumpStackSizesSection(const Elf_Shdr
*Shdr
) {
501 auto S
= std::make_unique
<ELFYAML::StackSizesSection
>();
502 if (Error E
= dumpCommonSection(Shdr
, *S
))
505 auto ContentOrErr
= Obj
.getSectionContents(Shdr
);
507 return ContentOrErr
.takeError();
509 ArrayRef
<uint8_t> Content
= *ContentOrErr
;
510 DataExtractor
Data(Content
, Obj
.isLE(), ELFT::Is64Bits
? 8 : 4);
512 std::vector
<ELFYAML::StackSizeEntry
> Entries
;
513 DataExtractor::Cursor
Cur(0);
514 while (Cur
&& Cur
.tell() < Content
.size()) {
515 uint64_t Address
= Data
.getAddress(Cur
);
516 uint64_t Size
= Data
.getULEB128(Cur
);
517 Entries
.push_back({Address
, Size
});
520 if (Content
.empty() || !Cur
) {
521 // If .stack_sizes cannot be decoded, we dump it as an array of bytes.
522 consumeError(Cur
.takeError());
523 S
->Content
= yaml::BinaryRef(Content
);
525 S
->Entries
= std::move(Entries
);
531 template <class ELFT
>
532 Expected
<ELFYAML::AddrsigSection
*>
533 ELFDumper
<ELFT
>::dumpAddrsigSection(const Elf_Shdr
*Shdr
) {
534 auto S
= std::make_unique
<ELFYAML::AddrsigSection
>();
535 if (Error E
= dumpCommonSection(Shdr
, *S
))
538 auto ContentOrErr
= Obj
.getSectionContents(Shdr
);
540 return ContentOrErr
.takeError();
542 ArrayRef
<uint8_t> Content
= *ContentOrErr
;
543 DataExtractor::Cursor
Cur(0);
544 DataExtractor
Data(Content
, Obj
.isLE(), /*AddressSize=*/0);
545 std::vector
<ELFYAML::AddrsigSymbol
> Symbols
;
546 while (Cur
&& Cur
.tell() < Content
.size()) {
547 uint64_t SymNdx
= Data
.getULEB128(Cur
);
551 Expected
<StringRef
> SymbolName
= getSymbolName(Shdr
->sh_link
, SymNdx
);
552 if (!SymbolName
|| SymbolName
->empty()) {
553 consumeError(SymbolName
.takeError());
554 Symbols
.emplace_back(SymNdx
);
558 Symbols
.emplace_back(*SymbolName
);
562 S
->Symbols
= std::move(Symbols
);
566 consumeError(Cur
.takeError());
567 S
->Content
= yaml::BinaryRef(Content
);
571 template <class ELFT
>
572 Expected
<ELFYAML::DynamicSection
*>
573 ELFDumper
<ELFT
>::dumpDynamicSection(const Elf_Shdr
*Shdr
) {
574 auto S
= std::make_unique
<ELFYAML::DynamicSection
>();
575 if (Error E
= dumpCommonSection(Shdr
, *S
))
578 auto DynTagsOrErr
= Obj
.template getSectionContentsAsArray
<Elf_Dyn
>(Shdr
);
580 return DynTagsOrErr
.takeError();
582 for (const Elf_Dyn
&Dyn
: *DynTagsOrErr
)
583 S
->Entries
.push_back({(ELFYAML::ELF_DYNTAG
)Dyn
.getTag(), Dyn
.getVal()});
588 template <class ELFT
>
589 Expected
<ELFYAML::RelocationSection
*>
590 ELFDumper
<ELFT
>::dumpRelocSection(const Elf_Shdr
*Shdr
) {
591 auto S
= std::make_unique
<ELFYAML::RelocationSection
>();
592 if (auto E
= dumpCommonRelocationSection(Shdr
, *S
))
595 auto SymTabOrErr
= Obj
.getSection(Shdr
->sh_link
);
597 return SymTabOrErr
.takeError();
598 const Elf_Shdr
*SymTab
= *SymTabOrErr
;
600 if (Shdr
->sh_type
== ELF::SHT_REL
) {
601 auto Rels
= Obj
.rels(Shdr
);
603 return Rels
.takeError();
604 for (const Elf_Rel
&Rel
: *Rels
) {
605 ELFYAML::Relocation R
;
606 if (Error E
= dumpRelocation(&Rel
, SymTab
, R
))
608 S
->Relocations
.push_back(R
);
611 auto Rels
= Obj
.relas(Shdr
);
613 return Rels
.takeError();
614 for (const Elf_Rela
&Rel
: *Rels
) {
615 ELFYAML::Relocation R
;
616 if (Error E
= dumpRelocation(&Rel
, SymTab
, R
))
618 R
.Addend
= Rel
.r_addend
;
619 S
->Relocations
.push_back(R
);
626 template <class ELFT
>
627 Expected
<ELFYAML::RawContentSection
*>
628 ELFDumper
<ELFT
>::dumpContentSection(const Elf_Shdr
*Shdr
) {
629 auto S
= std::make_unique
<ELFYAML::RawContentSection
>();
630 if (Error E
= dumpCommonSection(Shdr
, *S
))
633 unsigned SecIndex
= Shdr
- &Sections
[0];
634 if (SecIndex
!= 0 || Shdr
->sh_type
!= ELF::SHT_NULL
) {
635 auto ContentOrErr
= Obj
.getSectionContents(Shdr
);
637 return ContentOrErr
.takeError();
638 ArrayRef
<uint8_t> Content
= *ContentOrErr
;
639 if (!Content
.empty())
640 S
->Content
= yaml::BinaryRef(Content
);
642 S
->Size
= static_cast<llvm::yaml::Hex64
>(Shdr
->sh_size
);
646 S
->Info
= static_cast<llvm::yaml::Hex64
>(Shdr
->sh_info
);
650 template <class ELFT
>
651 Expected
<ELFYAML::SymtabShndxSection
*>
652 ELFDumper
<ELFT
>::dumpSymtabShndxSection(const Elf_Shdr
*Shdr
) {
653 auto S
= std::make_unique
<ELFYAML::SymtabShndxSection
>();
654 if (Error E
= dumpCommonSection(Shdr
, *S
))
657 auto EntriesOrErr
= Obj
.template getSectionContentsAsArray
<Elf_Word
>(Shdr
);
659 return EntriesOrErr
.takeError();
660 for (const Elf_Word
&E
: *EntriesOrErr
)
661 S
->Entries
.push_back(E
);
665 template <class ELFT
>
666 Expected
<ELFYAML::NoBitsSection
*>
667 ELFDumper
<ELFT
>::dumpNoBitsSection(const Elf_Shdr
*Shdr
) {
668 auto S
= std::make_unique
<ELFYAML::NoBitsSection
>();
669 if (Error E
= dumpCommonSection(Shdr
, *S
))
671 S
->Size
= Shdr
->sh_size
;
676 template <class ELFT
>
677 Expected
<ELFYAML::HashSection
*>
678 ELFDumper
<ELFT
>::dumpHashSection(const Elf_Shdr
*Shdr
) {
679 auto S
= std::make_unique
<ELFYAML::HashSection
>();
680 if (Error E
= dumpCommonSection(Shdr
, *S
))
683 auto ContentOrErr
= Obj
.getSectionContents(Shdr
);
685 return ContentOrErr
.takeError();
687 ArrayRef
<uint8_t> Content
= *ContentOrErr
;
688 if (Content
.size() % 4 != 0 || Content
.size() < 8) {
689 S
->Content
= yaml::BinaryRef(Content
);
693 DataExtractor::Cursor
Cur(0);
694 DataExtractor
Data(Content
, Obj
.isLE(), /*AddressSize=*/0);
695 uint32_t NBucket
= Data
.getU32(Cur
);
696 uint32_t NChain
= Data
.getU32(Cur
);
697 if (Content
.size() != (2 + NBucket
+ NChain
) * 4) {
698 S
->Content
= yaml::BinaryRef(Content
);
701 llvm_unreachable("entries were not read correctly");
704 S
->Bucket
.emplace(NBucket
);
705 for (uint32_t &V
: *S
->Bucket
)
706 V
= Data
.getU32(Cur
);
708 S
->Chain
.emplace(NChain
);
709 for (uint32_t &V
: *S
->Chain
)
710 V
= Data
.getU32(Cur
);
714 llvm_unreachable("entries were not read correctly");
717 template <class ELFT
>
718 Expected
<ELFYAML::VerdefSection
*>
719 ELFDumper
<ELFT
>::dumpVerdefSection(const Elf_Shdr
*Shdr
) {
720 typedef typename
ELFT::Verdef Elf_Verdef
;
721 typedef typename
ELFT::Verdaux Elf_Verdaux
;
723 auto S
= std::make_unique
<ELFYAML::VerdefSection
>();
724 if (Error E
= dumpCommonSection(Shdr
, *S
))
727 S
->Info
= Shdr
->sh_info
;
729 auto StringTableShdrOrErr
= Obj
.getSection(Shdr
->sh_link
);
730 if (!StringTableShdrOrErr
)
731 return StringTableShdrOrErr
.takeError();
733 auto StringTableOrErr
= Obj
.getStringTable(*StringTableShdrOrErr
);
734 if (!StringTableOrErr
)
735 return StringTableOrErr
.takeError();
737 auto Contents
= Obj
.getSectionContents(Shdr
);
739 return Contents
.takeError();
741 llvm::ArrayRef
<uint8_t> Data
= *Contents
;
742 const uint8_t *Buf
= Data
.data();
744 const Elf_Verdef
*Verdef
= reinterpret_cast<const Elf_Verdef
*>(Buf
);
745 ELFYAML::VerdefEntry Entry
;
746 Entry
.Version
= Verdef
->vd_version
;
747 Entry
.Flags
= Verdef
->vd_flags
;
748 Entry
.VersionNdx
= Verdef
->vd_ndx
;
749 Entry
.Hash
= Verdef
->vd_hash
;
751 const uint8_t *BufAux
= Buf
+ Verdef
->vd_aux
;
753 const Elf_Verdaux
*Verdaux
=
754 reinterpret_cast<const Elf_Verdaux
*>(BufAux
);
755 Entry
.VerNames
.push_back(
756 StringTableOrErr
->drop_front(Verdaux
->vda_name
).data());
757 BufAux
= Verdaux
->vda_next
? BufAux
+ Verdaux
->vda_next
: nullptr;
760 S
->Entries
.push_back(Entry
);
761 Buf
= Verdef
->vd_next
? Buf
+ Verdef
->vd_next
: nullptr;
767 template <class ELFT
>
768 Expected
<ELFYAML::SymverSection
*>
769 ELFDumper
<ELFT
>::dumpSymverSection(const Elf_Shdr
*Shdr
) {
770 typedef typename
ELFT::Half Elf_Half
;
772 auto S
= std::make_unique
<ELFYAML::SymverSection
>();
773 if (Error E
= dumpCommonSection(Shdr
, *S
))
776 auto VersionsOrErr
= Obj
.template getSectionContentsAsArray
<Elf_Half
>(Shdr
);
778 return VersionsOrErr
.takeError();
779 for (const Elf_Half
&E
: *VersionsOrErr
)
780 S
->Entries
.push_back(E
);
785 template <class ELFT
>
786 Expected
<ELFYAML::VerneedSection
*>
787 ELFDumper
<ELFT
>::dumpVerneedSection(const Elf_Shdr
*Shdr
) {
788 typedef typename
ELFT::Verneed Elf_Verneed
;
789 typedef typename
ELFT::Vernaux Elf_Vernaux
;
791 auto S
= std::make_unique
<ELFYAML::VerneedSection
>();
792 if (Error E
= dumpCommonSection(Shdr
, *S
))
795 S
->Info
= Shdr
->sh_info
;
797 auto Contents
= Obj
.getSectionContents(Shdr
);
799 return Contents
.takeError();
801 auto StringTableShdrOrErr
= Obj
.getSection(Shdr
->sh_link
);
802 if (!StringTableShdrOrErr
)
803 return StringTableShdrOrErr
.takeError();
805 auto StringTableOrErr
= Obj
.getStringTable(*StringTableShdrOrErr
);
806 if (!StringTableOrErr
)
807 return StringTableOrErr
.takeError();
809 llvm::ArrayRef
<uint8_t> Data
= *Contents
;
810 const uint8_t *Buf
= Data
.data();
812 const Elf_Verneed
*Verneed
= reinterpret_cast<const Elf_Verneed
*>(Buf
);
814 ELFYAML::VerneedEntry Entry
;
815 Entry
.Version
= Verneed
->vn_version
;
817 StringRef(StringTableOrErr
->drop_front(Verneed
->vn_file
).data());
819 const uint8_t *BufAux
= Buf
+ Verneed
->vn_aux
;
821 const Elf_Vernaux
*Vernaux
=
822 reinterpret_cast<const Elf_Vernaux
*>(BufAux
);
824 ELFYAML::VernauxEntry Aux
;
825 Aux
.Hash
= Vernaux
->vna_hash
;
826 Aux
.Flags
= Vernaux
->vna_flags
;
827 Aux
.Other
= Vernaux
->vna_other
;
829 StringRef(StringTableOrErr
->drop_front(Vernaux
->vna_name
).data());
831 Entry
.AuxV
.push_back(Aux
);
832 BufAux
= Vernaux
->vna_next
? BufAux
+ Vernaux
->vna_next
: nullptr;
835 S
->VerneedV
.push_back(Entry
);
836 Buf
= Verneed
->vn_next
? Buf
+ Verneed
->vn_next
: nullptr;
842 template <class ELFT
>
843 Expected
<StringRef
> ELFDumper
<ELFT
>::getSymbolName(uint32_t SymtabNdx
,
844 uint32_t SymbolNdx
) {
845 auto SymtabOrErr
= Obj
.getSection(SymtabNdx
);
847 return SymtabOrErr
.takeError();
849 const Elf_Shdr
*Symtab
= *SymtabOrErr
;
850 auto SymOrErr
= Obj
.getSymbol(Symtab
, SymbolNdx
);
852 return SymOrErr
.takeError();
854 auto StrTabOrErr
= Obj
.getStringTableForSymtab(*Symtab
);
856 return StrTabOrErr
.takeError();
857 return getUniquedSymbolName(*SymOrErr
, *StrTabOrErr
, Symtab
);
860 template <class ELFT
>
861 Expected
<ELFYAML::Group
*> ELFDumper
<ELFT
>::dumpGroup(const Elf_Shdr
*Shdr
) {
862 auto S
= std::make_unique
<ELFYAML::Group
>();
863 if (Error E
= dumpCommonSection(Shdr
, *S
))
866 // Get symbol with index sh_info. This symbol's name is the signature of the group.
867 Expected
<StringRef
> SymbolName
= getSymbolName(Shdr
->sh_link
, Shdr
->sh_info
);
869 return SymbolName
.takeError();
870 S
->Signature
= *SymbolName
;
872 auto MembersOrErr
= Obj
.template getSectionContentsAsArray
<Elf_Word
>(Shdr
);
874 return MembersOrErr
.takeError();
876 for (Elf_Word Member
: *MembersOrErr
) {
877 if (Member
== llvm::ELF::GRP_COMDAT
) {
878 S
->Members
.push_back({"GRP_COMDAT"});
882 auto SHdrOrErr
= Obj
.getSection(Member
);
884 return SHdrOrErr
.takeError();
885 auto NameOrErr
= getUniquedSectionName(*SHdrOrErr
);
887 return NameOrErr
.takeError();
888 S
->Members
.push_back({*NameOrErr
});
893 template <class ELFT
>
894 Expected
<ELFYAML::MipsABIFlags
*>
895 ELFDumper
<ELFT
>::dumpMipsABIFlags(const Elf_Shdr
*Shdr
) {
896 assert(Shdr
->sh_type
== ELF::SHT_MIPS_ABIFLAGS
&&
897 "Section type is not SHT_MIPS_ABIFLAGS");
898 auto S
= std::make_unique
<ELFYAML::MipsABIFlags
>();
899 if (Error E
= dumpCommonSection(Shdr
, *S
))
902 auto ContentOrErr
= Obj
.getSectionContents(Shdr
);
904 return ContentOrErr
.takeError();
906 auto *Flags
= reinterpret_cast<const object::Elf_Mips_ABIFlags
<ELFT
> *>(
907 ContentOrErr
.get().data());
908 S
->Version
= Flags
->version
;
909 S
->ISALevel
= Flags
->isa_level
;
910 S
->ISARevision
= Flags
->isa_rev
;
911 S
->GPRSize
= Flags
->gpr_size
;
912 S
->CPR1Size
= Flags
->cpr1_size
;
913 S
->CPR2Size
= Flags
->cpr2_size
;
914 S
->FpABI
= Flags
->fp_abi
;
915 S
->ISAExtension
= Flags
->isa_ext
;
916 S
->ASEs
= Flags
->ases
;
917 S
->Flags1
= Flags
->flags1
;
918 S
->Flags2
= Flags
->flags2
;
922 template <class ELFT
>
923 static Error
elf2yaml(raw_ostream
&Out
, const object::ELFFile
<ELFT
> &Obj
) {
924 ELFDumper
<ELFT
> Dumper(Obj
);
925 Expected
<ELFYAML::Object
*> YAMLOrErr
= Dumper
.dump();
927 return YAMLOrErr
.takeError();
929 std::unique_ptr
<ELFYAML::Object
> YAML(YAMLOrErr
.get());
930 yaml::Output
Yout(Out
);
933 return Error::success();
936 Error
elf2yaml(raw_ostream
&Out
, const object::ObjectFile
&Obj
) {
937 if (const auto *ELFObj
= dyn_cast
<object::ELF32LEObjectFile
>(&Obj
))
938 return elf2yaml(Out
, *ELFObj
->getELFFile());
940 if (const auto *ELFObj
= dyn_cast
<object::ELF32BEObjectFile
>(&Obj
))
941 return elf2yaml(Out
, *ELFObj
->getELFFile());
943 if (const auto *ELFObj
= dyn_cast
<object::ELF64LEObjectFile
>(&Obj
))
944 return elf2yaml(Out
, *ELFObj
->getELFFile());
946 if (const auto *ELFObj
= dyn_cast
<object::ELF64BEObjectFile
>(&Obj
))
947 return elf2yaml(Out
, *ELFObj
->getELFFile());
949 llvm_unreachable("unknown ELF file format");