llvm.org GIT mirror llvm / cabe7eb
Revert r366052 "[obj2yaml] - Rework tool's error reporting logic for ELF target." Seems it broke LLD: http://lab.llvm.org:8011/builders/sanitizer-windows/builds/48434 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@366053 91177308-0d34-0410-b5e6-96231b3b80d8 George Rimar a month ago
6 changed file(s) with 200 addition(s) and 215 deletion(s). Raw diff Collapse all Expand all
354354 }
355355
356356 template
357 inline Expected
358 getSymbol(typename ELFT::SymRange Symbols, uint32_t Index) {
359 if (Index >= Symbols.size())
360 // TODO: this error is untested.
361 return createError("invalid symbol index");
362 return &Symbols[Index];
363 }
364
365 template
357366 Expected
358367 ELFFile::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
359 auto SymsOrErr = symbols(Sec);
360 if (!SymsOrErr)
361 return SymsOrErr.takeError();
362
363 Elf_Sym_Range Symbols = *SymsOrErr;
364 if (Index >= Symbols.size())
365 return createError("unable to get symbol from section " +
366 getSecIndexForError(this, Sec) +
367 ": invalid symbol index (" + Twine(Index) + ")");
368 return &Symbols[Index];
368 auto SymtabOrErr = symbols(Sec);
369 if (!SymtabOrErr)
370 return SymtabOrErr.takeError();
371 return object::getSymbol(*SymtabOrErr, Index);
369372 }
370373
371374 template
0 ## Checks that the tool is able to read section groups from ELF.
11
2 # RUN: yaml2obj --docnum=1 %s > %t1.o
2 # RUN: yaml2obj %s > %t1.o
33 # RUN: llvm-readobj --elf-section-groups %t1.o | FileCheck %s -check-prefix=OBJ
44 # RUN: obj2yaml %t1.o | FileCheck %s --check-prefix YAML
55
4545 - Name: signature
4646 Type: STT_OBJECT
4747 Section: .rodata
48
49 ## Check obj2yaml report an error when sh_info field of
50 ## group section contains invalid (too large) signature symbol index.
51
52 # RUN: yaml2obj --docnum=2 %s > %t2.o
53 # RUN: not obj2yaml %t2.o 2>&1 | FileCheck %s --check-prefix ERR
54
55 --- !ELF
56 FileHeader:
57 Class: ELFCLASS64
58 Data: ELFDATA2LSB
59 Type: ET_REL
60 Machine: EM_X86_64
61 Sections:
62 - Name: .group
63 Type: SHT_GROUP
64 Link: .symtab
65 Info: 0xFF
66 Members:
67 - SectionOrType: GRP_COMDAT
68
69 # ERR: Error reading file: {{.*}}2.o: unable to get symbol from section [index 2]: invalid symbol index (255)
5050 ## shn_xindex.o contains a symbol with st_shndx == SHN_XINDEX.
5151 ## We do not support it at this moment.
5252 # RUN: not obj2yaml %S/Inputs/shn_xindex.o 2>&1 | FileCheck %s --check-prefix=ERR
53 # ERR: Error reading file: {{.*}}shn_xindex.o: SHN_XINDEX symbols are not supported
53 # ERR: Error reading file: {{.*}}shn_xindex.o: Feature not yet implemented.
4343 const object::ELFFile &Obj;
4444 ArrayRef ShndxTable;
4545
46 Error dumpSymbols(const Elf_Shdr *Symtab,
47 std::vector &Symbols);
48 Error dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
49 StringRef StrTable, ELFYAML::Symbol &S);
50 Error dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
51 Error dumpCommonRelocationSection(const Elf_Shdr *Shdr,
52 ELFYAML::RelocationSection &S);
46 std::error_code dumpSymbols(const Elf_Shdr *Symtab,
47 std::vector &Symbols);
48 std::error_code dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
49 StringRef StrTable, ELFYAML::Symbol &S);
50 std::error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
51 std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr,
52 ELFYAML::RelocationSection &S);
5353 template
54 Error dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
55 ELFYAML::Relocation &R);
56
57 Expected dumpDynamicSection(const Elf_Shdr *Shdr);
58 Expected dumpRelocSection(const Elf_Shdr *Shdr);
59 Expected
54 std::error_code dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
55 ELFYAML::Relocation &R);
56
57 ErrorOr dumpDynamicSection(const Elf_Shdr *Shdr);
58 ErrorOr dumpRelocSection(const Elf_Shdr *Shdr);
59 ErrorOr
6060 dumpContentSection(const Elf_Shdr *Shdr);
61 Expected dumpNoBitsSection(const Elf_Shdr *Shdr);
62 Expected dumpVerdefSection(const Elf_Shdr *Shdr);
63 Expected dumpSymverSection(const Elf_Shdr *Shdr);
64 Expected dumpVerneedSection(const Elf_Shdr *Shdr);
65 Expected dumpGroup(const Elf_Shdr *Shdr);
66 Expected dumpMipsABIFlags(const Elf_Shdr *Shdr);
61 ErrorOr dumpNoBitsSection(const Elf_Shdr *Shdr);
62 ErrorOr dumpVerdefSection(const Elf_Shdr *Shdr);
63 ErrorOr dumpSymverSection(const Elf_Shdr *Shdr);
64 ErrorOr dumpVerneedSection(const Elf_Shdr *Shdr);
65 ErrorOr dumpGroup(const Elf_Shdr *Shdr);
66 ErrorOr dumpMipsABIFlags(const Elf_Shdr *Shdr);
6767
6868 public:
6969 ELFDumper(const object::ELFFile &O);
70 Expected dump();
70 ErrorOr dump();
7171 };
7272
7373 }
133133 return Name;
134134 }
135135
136 template Expected ELFDumper::dump() {
136 template ErrorOr ELFDumper::dump() {
137137 auto Y = make_unique();
138138
139139 // Dump header. We do not dump SHEntSize, SHOffset, SHNum and SHStrNdx field.
151151 // Dump sections
152152 auto SectionsOrErr = Obj.sections();
153153 if (!SectionsOrErr)
154 return SectionsOrErr.takeError();
154 return errorToErrorCode(SectionsOrErr.takeError());
155155 Sections = *SectionsOrErr;
156156 SectionNames.resize(Sections.size());
157157
159159 // to access the deduplicated symbol names that we also create here.
160160 for (const Elf_Shdr &Sec : Sections) {
161161 if (Sec.sh_type == ELF::SHT_SYMTAB)
162 if (Error E = dumpSymbols(&Sec, Y->Symbols))
163 return std::move(E);
162 if (auto EC = dumpSymbols(&Sec, Y->Symbols))
163 return EC;
164164 if (Sec.sh_type == ELF::SHT_DYNSYM)
165 if (Error E = dumpSymbols(&Sec, Y->DynamicSymbols))
166 return std::move(E);
165 if (auto EC = dumpSymbols(&Sec, Y->DynamicSymbols))
166 return EC;
167167 }
168168
169169 for (const Elf_Shdr &Sec : Sections) {
170170 switch (Sec.sh_type) {
171171 case ELF::SHT_DYNAMIC: {
172 Expected SecOrErr = dumpDynamicSection(&Sec);
173 if (!SecOrErr)
174 return SecOrErr.takeError();
175 Y->Sections.emplace_back(*SecOrErr);
172 ErrorOr S = dumpDynamicSection(&Sec);
173 if (std::error_code EC = S.getError())
174 return EC;
175 Y->Sections.push_back(std::unique_ptr(S.get()));
176176 break;
177177 }
178178 case ELF::SHT_NULL:
184184 case ELF::SHT_SYMTAB_SHNDX: {
185185 auto TableOrErr = Obj.getSHNDXTable(Sec);
186186 if (!TableOrErr)
187 return TableOrErr.takeError();
187 return errorToErrorCode(TableOrErr.takeError());
188188 ShndxTable = *TableOrErr;
189189 break;
190190 }
191191 case ELF::SHT_REL:
192192 case ELF::SHT_RELA: {
193 Expected SecOrErr = dumpRelocSection(&Sec);
194 if (!SecOrErr)
195 return SecOrErr.takeError();
196 Y->Sections.emplace_back(*SecOrErr);
193 ErrorOr S = dumpRelocSection(&Sec);
194 if (std::error_code EC = S.getError())
195 return EC;
196 Y->Sections.push_back(std::unique_ptr(S.get()));
197197 break;
198198 }
199199 case ELF::SHT_GROUP: {
200 Expected GroupOrErr = dumpGroup(&Sec);
201 if (!GroupOrErr)
202 return GroupOrErr.takeError();
203 Y->Sections.emplace_back(*GroupOrErr);
200 ErrorOr G = dumpGroup(&Sec);
201 if (std::error_code EC = G.getError())
202 return EC;
203 Y->Sections.push_back(std::unique_ptr(G.get()));
204204 break;
205205 }
206206 case ELF::SHT_MIPS_ABIFLAGS: {
207 Expected SecOrErr = dumpMipsABIFlags(&Sec);
208 if (!SecOrErr)
209 return SecOrErr.takeError();
210 Y->Sections.emplace_back(*SecOrErr);
207 ErrorOr G = dumpMipsABIFlags(&Sec);
208 if (std::error_code EC = G.getError())
209 return EC;
210 Y->Sections.push_back(std::unique_ptr(G.get()));
211211 break;
212212 }
213213 case ELF::SHT_NOBITS: {
214 Expected SecOrErr = dumpNoBitsSection(&Sec);
215 if (!SecOrErr)
216 return SecOrErr.takeError();
217 Y->Sections.emplace_back(*SecOrErr);
214 ErrorOr S = dumpNoBitsSection(&Sec);
215 if (std::error_code EC = S.getError())
216 return EC;
217 Y->Sections.push_back(std::unique_ptr(S.get()));
218218 break;
219219 }
220220 case ELF::SHT_GNU_verdef: {
221 Expected SecOrErr = dumpVerdefSection(&Sec);
222 if (!SecOrErr)
223 return SecOrErr.takeError();
224 Y->Sections.emplace_back(*SecOrErr);
221 ErrorOr S = dumpVerdefSection(&Sec);
222 if (std::error_code EC = S.getError())
223 return EC;
224 Y->Sections.push_back(std::unique_ptr(S.get()));
225225 break;
226226 }
227227 case ELF::SHT_GNU_versym: {
228 Expected SecOrErr = dumpSymverSection(&Sec);
229 if (!SecOrErr)
230 return SecOrErr.takeError();
231 Y->Sections.emplace_back(*SecOrErr);
228 ErrorOr S = dumpSymverSection(&Sec);
229 if (std::error_code EC = S.getError())
230 return EC;
231 Y->Sections.push_back(std::unique_ptr(S.get()));
232232 break;
233233 }
234234 case ELF::SHT_GNU_verneed: {
235 Expected SecOrErr = dumpVerneedSection(&Sec);
236 if (!SecOrErr)
237 return SecOrErr.takeError();
238 Y->Sections.emplace_back(*SecOrErr);
235 ErrorOr S = dumpVerneedSection(&Sec);
236 if (std::error_code EC = S.getError())
237 return EC;
238 Y->Sections.push_back(std::unique_ptr(S.get()));
239239 break;
240240 }
241241 default: {
242 Expected SecOrErr =
243 dumpContentSection(&Sec);
244 if (!SecOrErr)
245 return SecOrErr.takeError();
246 Y->Sections.emplace_back(*SecOrErr);
242 ErrorOr S = dumpContentSection(&Sec);
243 if (std::error_code EC = S.getError())
244 return EC;
245 Y->Sections.push_back(std::unique_ptr(S.get()));
247246 }
248247 }
249248 }
252251 }
253252
254253 template
255 Error ELFDumper::dumpSymbols(const Elf_Shdr *Symtab,
254 std::error_code
255 ELFDumper::dumpSymbols(const Elf_Shdr *Symtab,
256256 std::vector &Symbols) {
257257 if (!Symtab)
258 return Error::success();
258 return std::error_code();
259259
260260 auto StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
261261 if (!StrTableOrErr)
262 return StrTableOrErr.takeError();
262 return errorToErrorCode(StrTableOrErr.takeError());
263263 StringRef StrTable = *StrTableOrErr;
264264
265265 auto SymtabOrErr = Obj.symbols(Symtab);
266266 if (!SymtabOrErr)
267 return SymtabOrErr.takeError();
267 return errorToErrorCode(SymtabOrErr.takeError());
268268
269269 if (Symtab->sh_type == ELF::SHT_SYMTAB) {
270270 SymTable = *SymtabOrErr;
278278 Symbols.push_back(S);
279279 }
280280
281 return Error::success();
282 }
283
284 template
285 Error ELFDumper::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
286 StringRef StrTable, ELFYAML::Symbol &S) {
281 return std::error_code();
282 }
283
284 template
285 std::error_code
286 ELFDumper::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
287 StringRef StrTable, ELFYAML::Symbol &S) {
287288 S.Type = Sym->getType();
288289 S.Value = Sym->st_value;
289290 S.Size = Sym->st_size;
293294 Expected SymbolNameOrErr =
294295 getUniquedSymbolName(Sym, StrTable, SymTab);
295296 if (!SymbolNameOrErr)
296 return SymbolNameOrErr.takeError();
297 return errorToErrorCode(SymbolNameOrErr.takeError());
297298 S.Name = SymbolNameOrErr.get();
298299
299300 if (Sym->st_shndx >= ELF::SHN_LORESERVE) {
300301 if (Sym->st_shndx == ELF::SHN_XINDEX)
301 return createStringError(obj2yaml_error::not_implemented,
302 "SHN_XINDEX symbols are not supported");
302 return obj2yaml_error::not_implemented;
303303 S.Index = (ELFYAML::ELF_SHN)Sym->st_shndx;
304 return Error::success();
304 return obj2yaml_error::success;
305305 }
306306
307307 auto ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable);
308308 if (!ShdrOrErr)
309 return ShdrOrErr.takeError();
309 return errorToErrorCode(ShdrOrErr.takeError());
310310 const Elf_Shdr *Shdr = *ShdrOrErr;
311311 if (!Shdr)
312 return Error::success();
312 return obj2yaml_error::success;
313313
314314 auto NameOrErr = getUniquedSectionName(Shdr);
315315 if (!NameOrErr)
316 return NameOrErr.takeError();
316 return errorToErrorCode(NameOrErr.takeError());
317317 S.Section = NameOrErr.get();
318318
319 return Error::success();
319 return obj2yaml_error::success;
320320 }
321321
322322 template
323323 template
324 Error ELFDumper::dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
325 ELFYAML::Relocation &R) {
324 std::error_code ELFDumper::dumpRelocation(const RelT *Rel,
325 const Elf_Shdr *SymTab,
326 ELFYAML::Relocation &R) {
326327 R.Type = Rel->getType(Obj.isMips64EL());
327328 R.Offset = Rel->r_offset;
328329 R.Addend = 0;
329330
330331 auto SymOrErr = Obj.getRelocationSymbol(Rel, SymTab);
331332 if (!SymOrErr)
332 return SymOrErr.takeError();
333 return errorToErrorCode(SymOrErr.takeError());
333334 const Elf_Sym *Sym = *SymOrErr;
334335 auto StrTabSec = Obj.getSection(SymTab->sh_link);
335336 if (!StrTabSec)
336 return StrTabSec.takeError();
337 return errorToErrorCode(StrTabSec.takeError());
337338 auto StrTabOrErr = Obj.getStringTable(*StrTabSec);
338339 if (!StrTabOrErr)
339 return StrTabOrErr.takeError();
340 return errorToErrorCode(StrTabOrErr.takeError());
340341 StringRef StrTab = *StrTabOrErr;
341342
342343 if (Sym) {
343344 Expected NameOrErr = getUniquedSymbolName(Sym, StrTab, SymTab);
344345 if (!NameOrErr)
345 return NameOrErr.takeError();
346 return errorToErrorCode(NameOrErr.takeError());
346347 R.Symbol = NameOrErr.get();
347348 } else {
348349 // We have some edge cases of relocations without a symbol associated,
352353 R.Symbol = "";
353354 }
354355
355 return Error::success();
356 }
357
358 template
359 Error ELFDumper::dumpCommonSection(const Elf_Shdr *Shdr,
360 ELFYAML::Section &S) {
356 return obj2yaml_error::success;
357 }
358
359 template
360 std::error_code ELFDumper::dumpCommonSection(const Elf_Shdr *Shdr,
361 ELFYAML::Section &S) {
361362 // Dump fields. We do not dump the ShOffset field. When not explicitly
362363 // set, the value is set by yaml2obj automatically.
363364 S.Type = Shdr->sh_type;
370371
371372 auto NameOrErr = getUniquedSectionName(Shdr);
372373 if (!NameOrErr)
373 return NameOrErr.takeError();
374 return errorToErrorCode(NameOrErr.takeError());
374375 S.Name = NameOrErr.get();
375376
376377 if (Shdr->sh_link != ELF::SHN_UNDEF) {
377378 auto LinkSection = Obj.getSection(Shdr->sh_link);
378379 if (LinkSection.takeError())
379 return LinkSection.takeError();
380 return errorToErrorCode(LinkSection.takeError());
380381 NameOrErr = getUniquedSectionName(*LinkSection);
381382 if (!NameOrErr)
382 return NameOrErr.takeError();
383 return errorToErrorCode(NameOrErr.takeError());
383384 S.Link = NameOrErr.get();
384385 }
385386
386 return Error::success();
387 }
388
389 template
390 Error ELFDumper::dumpCommonRelocationSection(
391 const Elf_Shdr *Shdr, ELFYAML::RelocationSection &S) {
392 if (Error E = dumpCommonSection(Shdr, S))
393 return E;
387 return obj2yaml_error::success;
388 }
389
390 template
391 std::error_code
392 ELFDumper::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
393 ELFYAML::RelocationSection &S) {
394 if (std::error_code EC = dumpCommonSection(Shdr, S))
395 return EC;
394396
395397 auto InfoSection = Obj.getSection(Shdr->sh_info);
396398 if (!InfoSection)
397 return InfoSection.takeError();
399 return errorToErrorCode(InfoSection.takeError());
398400
399401 auto NameOrErr = getUniquedSectionName(*InfoSection);
400402 if (!NameOrErr)
401 return NameOrErr.takeError();
403 return errorToErrorCode(NameOrErr.takeError());
402404 S.RelocatableSec = NameOrErr.get();
403405
404 return Error::success();
405 }
406
407 template
408 Expected
406 return obj2yaml_error::success;
407 }
408
409 template
410 ErrorOr
409411 ELFDumper::dumpDynamicSection(const Elf_Shdr *Shdr) {
410412 auto S = make_unique();
411 if (Error E = dumpCommonSection(Shdr, *S))
412 return std::move(E);
413 if (std::error_code EC = dumpCommonSection(Shdr, *S))
414 return EC;
413415
414416 auto DynTagsOrErr = Obj.template getSectionContentsAsArray(Shdr);
415417 if (!DynTagsOrErr)
416 return DynTagsOrErr.takeError();
418 return errorToErrorCode(DynTagsOrErr.takeError());
417419
418420 for (const Elf_Dyn &Dyn : *DynTagsOrErr)
419421 S->Entries.push_back({(ELFYAML::ELF_DYNTAG)Dyn.getTag(), Dyn.getVal()});
422424 }
423425
424426 template
425 Expected
427 ErrorOr
426428 ELFDumper::dumpRelocSection(const Elf_Shdr *Shdr) {
427429 auto S = make_unique();
428 if (auto E = dumpCommonRelocationSection(Shdr, *S))
429 return std::move(E);
430 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
431 return EC;
430432
431433 auto SymTabOrErr = Obj.getSection(Shdr->sh_link);
432434 if (!SymTabOrErr)
433 return SymTabOrErr.takeError();
435 return errorToErrorCode(SymTabOrErr.takeError());
434436 const Elf_Shdr *SymTab = *SymTabOrErr;
435437
436438 if (Shdr->sh_type == ELF::SHT_REL) {
437439 auto Rels = Obj.rels(Shdr);
438440 if (!Rels)
439 return Rels.takeError();
441 return errorToErrorCode(Rels.takeError());
440442 for (const Elf_Rel &Rel : *Rels) {
441443 ELFYAML::Relocation R;
442 if (Error E = dumpRelocation(&Rel, SymTab, R))
443 return std::move(E);
444 if (std::error_code EC = dumpRelocation(&Rel, SymTab, R))
445 return EC;
444446 S->Relocations.push_back(R);
445447 }
446448 } else {
447449 auto Rels = Obj.relas(Shdr);
448450 if (!Rels)
449 return Rels.takeError();
451 return errorToErrorCode(Rels.takeError());
450452 for (const Elf_Rela &Rel : *Rels) {
451453 ELFYAML::Relocation R;
452 if (Error E = dumpRelocation(&Rel, SymTab, R))
453 return std::move(E);
454 if (std::error_code EC = dumpRelocation(&Rel, SymTab, R))
455 return EC;
454456 R.Addend = Rel.r_addend;
455457 S->Relocations.push_back(R);
456458 }
460462 }
461463
462464 template
463 Expected
465 ErrorOr
464466 ELFDumper::dumpContentSection(const Elf_Shdr *Shdr) {
465467 auto S = make_unique();
466 if (Error E = dumpCommonSection(Shdr, *S))
467 return std::move(E);
468
469 if (std::error_code EC = dumpCommonSection(Shdr, *S))
470 return EC;
468471
469472 auto ContentOrErr = Obj.getSectionContents(Shdr);
470473 if (!ContentOrErr)
471 return ContentOrErr.takeError();
474 return errorToErrorCode(ContentOrErr.takeError());
472475 ArrayRef Content = *ContentOrErr;
473476 if (!Content.empty())
474477 S->Content = yaml::BinaryRef(Content);
478481 }
479482
480483 template
481 Expected
484 ErrorOr
482485 ELFDumper::dumpNoBitsSection(const Elf_Shdr *Shdr) {
483486 auto S = make_unique();
484 if (Error E = dumpCommonSection(Shdr, *S))
485 return std::move(E);
487
488 if (std::error_code EC = dumpCommonSection(Shdr, *S))
489 return EC;
486490 S->Size = Shdr->sh_size;
487491
488492 return S.release();
489493 }
490494
491495 template
492 Expected
496 ErrorOr
493497 ELFDumper::dumpVerdefSection(const Elf_Shdr *Shdr) {
494498 typedef typename ELFT::Verdef Elf_Verdef;
495499 typedef typename ELFT::Verdaux Elf_Verdaux;
496500
497501 auto S = make_unique();
498 if (Error E = dumpCommonSection(Shdr, *S))
499 return std::move(E);
502 if (std::error_code EC = dumpCommonSection(Shdr, *S))
503 return EC;
500504
501505 S->Info = Shdr->sh_info;
502506
503507 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link);
504508 if (!StringTableShdrOrErr)
505 return StringTableShdrOrErr.takeError();
509 return errorToErrorCode(StringTableShdrOrErr.takeError());
506510
507511 auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr);
508512 if (!StringTableOrErr)
509 return StringTableOrErr.takeError();
513 return errorToErrorCode(StringTableOrErr.takeError());
510514
511515 auto Contents = Obj.getSectionContents(Shdr);
512516 if (!Contents)
513 return Contents.takeError();
517 return errorToErrorCode(Contents.takeError());
514518
515519 llvm::ArrayRef Data = *Contents;
516520 const uint8_t *Buf = Data.data();
539543 }
540544
541545 template
542 Expected
546 ErrorOr
543547 ELFDumper::dumpSymverSection(const Elf_Shdr *Shdr) {
544548 typedef typename ELFT::Half Elf_Half;
545549
546550 auto S = make_unique();
547 if (Error E = dumpCommonSection(Shdr, *S))
548 return std::move(E);
551 if (std::error_code EC = dumpCommonSection(Shdr, *S))
552 return EC;
549553
550554 auto VersionsOrErr = Obj.template getSectionContentsAsArray(Shdr);
551555 if (!VersionsOrErr)
552 return VersionsOrErr.takeError();
556 return errorToErrorCode(VersionsOrErr.takeError());
553557 for (const Elf_Half &E : *VersionsOrErr)
554558 S->Entries.push_back(E);
555559
557561 }
558562
559563 template
560 Expected
564 ErrorOr
561565 ELFDumper::dumpVerneedSection(const Elf_Shdr *Shdr) {
562566 typedef typename ELFT::Verneed Elf_Verneed;
563567 typedef typename ELFT::Vernaux Elf_Vernaux;
564568
565569 auto S = make_unique();
566 if (Error E = dumpCommonSection(Shdr, *S))
567 return std::move(E);
570 if (std::error_code EC = dumpCommonSection(Shdr, *S))
571 return EC;
568572
569573 S->Info = Shdr->sh_info;
570574
571575 auto Contents = Obj.getSectionContents(Shdr);
572576 if (!Contents)
573 return Contents.takeError();
577 return errorToErrorCode(Contents.takeError());
574578
575579 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link);
576580 if (!StringTableShdrOrErr)
577 return StringTableShdrOrErr.takeError();
581 return errorToErrorCode(StringTableShdrOrErr.takeError());
578582
579583 auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr);
580584 if (!StringTableOrErr)
581 return StringTableOrErr.takeError();
585 return errorToErrorCode(StringTableOrErr.takeError());
582586
583587 llvm::ArrayRef Data = *Contents;
584588 const uint8_t *Buf = Data.data();
614618 }
615619
616620 template
617 Expected ELFDumper::dumpGroup(const Elf_Shdr *Shdr) {
621 ErrorOr ELFDumper::dumpGroup(const Elf_Shdr *Shdr) {
618622 auto S = make_unique();
619 if (Error E = dumpCommonSection(Shdr, *S))
620 return std::move(E);
623 if (std::error_code EC = dumpCommonSection(Shdr, *S))
624 return EC;
621625
622626 auto SymtabOrErr = Obj.getSection(Shdr->sh_link);
623627 if (!SymtabOrErr)
624 return SymtabOrErr.takeError();
628 return errorToErrorCode(SymtabOrErr.takeError());
625629 // Get symbol with index sh_info which name is the signature of the group.
626630 const Elf_Shdr *Symtab = *SymtabOrErr;
627631 auto SymOrErr = Obj.getSymbol(Symtab, Shdr->sh_info);
628632 if (!SymOrErr)
629 return SymOrErr.takeError();
633 return errorToErrorCode(SymOrErr.takeError());
630634 auto StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
631635 if (!StrTabOrErr)
632 return StrTabOrErr.takeError();
636 return errorToErrorCode(StrTabOrErr.takeError());
633637
634638 Expected SymbolName =
635639 getUniquedSymbolName(*SymOrErr, *StrTabOrErr, Symtab);
636640 if (!SymbolName)
637 return SymbolName.takeError();
641 return errorToErrorCode(SymbolName.takeError());
638642 S->Signature = *SymbolName;
639643
640644 auto MembersOrErr = Obj.template getSectionContentsAsArray(Shdr);
641645 if (!MembersOrErr)
642 return MembersOrErr.takeError();
646 return errorToErrorCode(MembersOrErr.takeError());
643647
644648 for (Elf_Word Member : *MembersOrErr) {
645649 if (Member == llvm::ELF::GRP_COMDAT) {
649653
650654 auto SHdrOrErr = Obj.getSection(Member);
651655 if (!SHdrOrErr)
652 return SHdrOrErr.takeError();
656 return errorToErrorCode(SHdrOrErr.takeError());
653657 auto NameOrErr = getUniquedSectionName(*SHdrOrErr);
654658 if (!NameOrErr)
655 return NameOrErr.takeError();
659 return errorToErrorCode(NameOrErr.takeError());
656660 S->Members.push_back({*NameOrErr});
657661 }
658662 return S.release();
659663 }
660664
661665 template
662 Expected
666 ErrorOr
663667 ELFDumper::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
664668 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
665669 "Section type is not SHT_MIPS_ABIFLAGS");
666670 auto S = make_unique();
667 if (Error E = dumpCommonSection(Shdr, *S))
668 return std::move(E);
671 if (std::error_code EC = dumpCommonSection(Shdr, *S))
672 return EC;
669673
670674 auto ContentOrErr = Obj.getSectionContents(Shdr);
671675 if (!ContentOrErr)
672 return ContentOrErr.takeError();
676 return errorToErrorCode(ContentOrErr.takeError());
673677
674678 auto *Flags = reinterpret_cast *>(
675679 ContentOrErr.get().data());
688692 }
689693
690694 template
691 static Error elf2yaml(raw_ostream &Out, const object::ELFFile &Obj) {
695 static std::error_code elf2yaml(raw_ostream &Out,
696 const object::ELFFile &Obj) {
692697 ELFDumper Dumper(Obj);
693 Expected YAMLOrErr = Dumper.dump();
694 if (!YAMLOrErr)
695 return YAMLOrErr.takeError();
698 ErrorOr YAMLOrErr = Dumper.dump();
699 if (std::error_code EC = YAMLOrErr.getError())
700 return EC;
696701
697702 std::unique_ptr YAML(YAMLOrErr.get());
698703 yaml::Output Yout(Out);
699704 Yout << *YAML;
700705
701 return Error::success();
702 }
703
704 Error elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
706 return std::error_code();
707 }
708
709 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
705710 if (const auto *ELFObj = dyn_cast(&Obj))
706711 return elf2yaml(Out, *ELFObj->getELFFile());
707712
714719 if (const auto *ELFObj = dyn_cast(&Obj))
715720 return elf2yaml(Out, *ELFObj->getELFFile());
716721
717 llvm_unreachable("unknown ELF file format");
718 }
722 return obj2yaml_error::unsupported_obj_file_format;
723 }
1616 using namespace llvm;
1717 using namespace llvm::object;
1818
19 static Error dumpObject(const ObjectFile &Obj) {
19 static std::error_code dumpObject(const ObjectFile &Obj) {
2020 if (Obj.isCOFF())
21 return errorCodeToError(coff2yaml(outs(), cast(Obj)));
21 return coff2yaml(outs(), cast(Obj));
2222
2323 if (Obj.isXCOFF())
24 return errorCodeToError(xcoff2yaml(outs(), cast(Obj)));
24 return xcoff2yaml(outs(), cast(Obj));
2525
2626 if (Obj.isELF())
2727 return elf2yaml(outs(), Obj);
28 if (Obj.isWasm())
29 return wasm2yaml(outs(), cast(Obj));
2830
29 if (Obj.isWasm())
30 return errorCodeToError(wasm2yaml(outs(), cast(Obj)));
31
32 return errorCodeToError(obj2yaml_error::unsupported_obj_file_format);
31 return obj2yaml_error::unsupported_obj_file_format;
3332 }
3433
3534 static Error dumpInput(StringRef File) {
4443 return errorCodeToError(macho2yaml(outs(), Binary));
4544 // TODO: If this is an archive, then burst it and dump each entry
4645 if (ObjectFile *Obj = dyn_cast(&Binary))
47 return dumpObject(*Obj);
46 return errorCodeToError(dumpObject(*Obj));
4847 if (MinidumpFile *Minidump = dyn_cast(&Binary))
4948 return minidump2yaml(outs(), *Minidump);
5049
2020
2121 std::error_code coff2yaml(llvm::raw_ostream &Out,
2222 const llvm::object::COFFObjectFile &Obj);
23 llvm::Error elf2yaml(llvm::raw_ostream &Out,
23 std::error_code elf2yaml(llvm::raw_ostream &Out,
2424 const llvm::object::ObjectFile &Obj);
2525 std::error_code macho2yaml(llvm::raw_ostream &Out,
2626 const llvm::object::Binary &Obj);