llvm.org GIT mirror llvm / 70b7545
Recommit r366052 "[obj2yaml] - Rework tool's error reporting logic for ELF target." No changes, LLD code was updated in r366057. Original commit message: ELF.h contains two getSymbol methods which seems to be used only from obj2yaml. One of these methods calls another, which in turn contains untested error message which doesn't provide enough information. Problem is that after improving only just that message, obj2yaml will not show it, ("Error reading file: yaml: Invalid data was encountered while parsing the file" message will be shown instead), because internal errors handling of tool is based on ErrorOr<> class which stores a error code and as a result can only show a predefined error string, what actually isn't very useful. In this patch, I rework obj2yaml's error reporting system for ELF targets to use Error Expected<> classes. Also, I improve the error message produced by getSymbol for demonstration of the new functionality. Differential revision: https://reviews.llvm.org/D64631 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@366058 91177308-0d34-0410-b5e6-96231b3b80d8 George Rimar a month ago
6 changed file(s) with 214 addition(s) and 199 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
366357 Expected
367358 ELFFile::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
368 auto SymtabOrErr = symbols(Sec);
369 if (!SymtabOrErr)
370 return SymtabOrErr.takeError();
371 return object::getSymbol(*SymtabOrErr, Index);
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];
372369 }
373370
374371 template
0 ## Checks that the tool is able to read section groups from ELF.
11
2 # RUN: yaml2obj %s > %t1.o
2 # RUN: yaml2obj --docnum=1 %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: Feature not yet implemented.
53 # ERR: Error reading file: {{.*}}shn_xindex.o: SHN_XINDEX symbols are not supported
4343 const object::ELFFile &Obj;
4444 ArrayRef ShndxTable;
4545
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);
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);
5353 template
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
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
6060 dumpContentSection(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);
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);
6767
6868 public:
6969 ELFDumper(const object::ELFFile &O);
70 ErrorOr dump();
70 Expected dump();
7171 };
7272
7373 }
133133 return Name;
134134 }
135135
136 template ErrorOr ELFDumper::dump() {
136 template Expected 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 errorToErrorCode(SectionsOrErr.takeError());
154 return 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 (auto EC = dumpSymbols(&Sec, Y->Symbols))
163 return EC;
162 if (Error E = dumpSymbols(&Sec, Y->Symbols))
163 return std::move(E);
164164 if (Sec.sh_type == ELF::SHT_DYNSYM)
165 if (auto EC = dumpSymbols(&Sec, Y->DynamicSymbols))
166 return EC;
165 if (Error E = dumpSymbols(&Sec, Y->DynamicSymbols))
166 return std::move(E);
167167 }
168168
169169 for (const Elf_Shdr &Sec : Sections) {
170170 switch (Sec.sh_type) {
171171 case ELF::SHT_DYNAMIC: {
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()));
172 Expected SecOrErr = dumpDynamicSection(&Sec);
173 if (!SecOrErr)
174 return SecOrErr.takeError();
175 Y->Sections.emplace_back(*SecOrErr);
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 errorToErrorCode(TableOrErr.takeError());
187 return TableOrErr.takeError();
188188 ShndxTable = *TableOrErr;
189189 break;
190190 }
191191 case ELF::SHT_REL:
192192 case ELF::SHT_RELA: {
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()));
193 Expected SecOrErr = dumpRelocSection(&Sec);
194 if (!SecOrErr)
195 return SecOrErr.takeError();
196 Y->Sections.emplace_back(*SecOrErr);
197197 break;
198198 }
199199 case ELF::SHT_GROUP: {
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()));
200 Expected GroupOrErr = dumpGroup(&Sec);
201 if (!GroupOrErr)
202 return GroupOrErr.takeError();
203 Y->Sections.emplace_back(*GroupOrErr);
204204 break;
205205 }
206206 case ELF::SHT_MIPS_ABIFLAGS: {
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()));
207 Expected SecOrErr = dumpMipsABIFlags(&Sec);
208 if (!SecOrErr)
209 return SecOrErr.takeError();
210 Y->Sections.emplace_back(*SecOrErr);
211211 break;
212212 }
213213 case ELF::SHT_NOBITS: {
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()));
214 Expected SecOrErr = dumpNoBitsSection(&Sec);
215 if (!SecOrErr)
216 return SecOrErr.takeError();
217 Y->Sections.emplace_back(*SecOrErr);
218218 break;
219219 }
220220 case ELF::SHT_GNU_verdef: {
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()));
221 Expected SecOrErr = dumpVerdefSection(&Sec);
222 if (!SecOrErr)
223 return SecOrErr.takeError();
224 Y->Sections.emplace_back(*SecOrErr);
225225 break;
226226 }
227227 case ELF::SHT_GNU_versym: {
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()));
228 Expected SecOrErr = dumpSymverSection(&Sec);
229 if (!SecOrErr)
230 return SecOrErr.takeError();
231 Y->Sections.emplace_back(*SecOrErr);
232232 break;
233233 }
234234 case ELF::SHT_GNU_verneed: {
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()));
235 Expected SecOrErr = dumpVerneedSection(&Sec);
236 if (!SecOrErr)
237 return SecOrErr.takeError();
238 Y->Sections.emplace_back(*SecOrErr);
239239 break;
240240 }
241241 default: {
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()));
242 Expected SecOrErr =
243 dumpContentSection(&Sec);
244 if (!SecOrErr)
245 return SecOrErr.takeError();
246 Y->Sections.emplace_back(*SecOrErr);
246247 }
247248 }
248249 }
251252 }
252253
253254 template
254 std::error_code
255 ELFDumper::dumpSymbols(const Elf_Shdr *Symtab,
255 Error ELFDumper::dumpSymbols(const Elf_Shdr *Symtab,
256256 std::vector &Symbols) {
257257 if (!Symtab)
258 return std::error_code();
258 return Error::success();
259259
260260 auto StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
261261 if (!StrTableOrErr)
262 return errorToErrorCode(StrTableOrErr.takeError());
262 return StrTableOrErr.takeError();
263263 StringRef StrTable = *StrTableOrErr;
264264
265265 auto SymtabOrErr = Obj.symbols(Symtab);
266266 if (!SymtabOrErr)
267 return errorToErrorCode(SymtabOrErr.takeError());
267 return SymtabOrErr.takeError();
268268
269269 if (Symtab->sh_type == ELF::SHT_SYMTAB) {
270270 SymTable = *SymtabOrErr;
278278 Symbols.push_back(S);
279279 }
280280
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) {
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) {
288287 S.Type = Sym->getType();
289288 S.Value = Sym->st_value;
290289 S.Size = Sym->st_size;
294293 Expected SymbolNameOrErr =
295294 getUniquedSymbolName(Sym, StrTable, SymTab);
296295 if (!SymbolNameOrErr)
297 return errorToErrorCode(SymbolNameOrErr.takeError());
296 return SymbolNameOrErr.takeError();
298297 S.Name = SymbolNameOrErr.get();
299298
300299 if (Sym->st_shndx >= ELF::SHN_LORESERVE) {
301300 if (Sym->st_shndx == ELF::SHN_XINDEX)
302 return obj2yaml_error::not_implemented;
301 return createStringError(obj2yaml_error::not_implemented,
302 "SHN_XINDEX symbols are not supported");
303303 S.Index = (ELFYAML::ELF_SHN)Sym->st_shndx;
304 return obj2yaml_error::success;
304 return Error::success();
305305 }
306306
307307 auto ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable);
308308 if (!ShdrOrErr)
309 return errorToErrorCode(ShdrOrErr.takeError());
309 return ShdrOrErr.takeError();
310310 const Elf_Shdr *Shdr = *ShdrOrErr;
311311 if (!Shdr)
312 return obj2yaml_error::success;
312 return Error::success();
313313
314314 auto NameOrErr = getUniquedSectionName(Shdr);
315315 if (!NameOrErr)
316 return errorToErrorCode(NameOrErr.takeError());
316 return NameOrErr.takeError();
317317 S.Section = NameOrErr.get();
318318
319 return obj2yaml_error::success;
319 return Error::success();
320320 }
321321
322322 template
323323 template
324 std::error_code ELFDumper::dumpRelocation(const RelT *Rel,
325 const Elf_Shdr *SymTab,
326 ELFYAML::Relocation &R) {
324 Error ELFDumper::dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
325 ELFYAML::Relocation &R) {
327326 R.Type = Rel->getType(Obj.isMips64EL());
328327 R.Offset = Rel->r_offset;
329328 R.Addend = 0;
330329
331330 auto SymOrErr = Obj.getRelocationSymbol(Rel, SymTab);
332331 if (!SymOrErr)
333 return errorToErrorCode(SymOrErr.takeError());
332 return SymOrErr.takeError();
334333 const Elf_Sym *Sym = *SymOrErr;
335334 auto StrTabSec = Obj.getSection(SymTab->sh_link);
336335 if (!StrTabSec)
337 return errorToErrorCode(StrTabSec.takeError());
336 return StrTabSec.takeError();
338337 auto StrTabOrErr = Obj.getStringTable(*StrTabSec);
339338 if (!StrTabOrErr)
340 return errorToErrorCode(StrTabOrErr.takeError());
339 return StrTabOrErr.takeError();
341340 StringRef StrTab = *StrTabOrErr;
342341
343342 if (Sym) {
344343 Expected NameOrErr = getUniquedSymbolName(Sym, StrTab, SymTab);
345344 if (!NameOrErr)
346 return errorToErrorCode(NameOrErr.takeError());
345 return NameOrErr.takeError();
347346 R.Symbol = NameOrErr.get();
348347 } else {
349348 // We have some edge cases of relocations without a symbol associated,
353352 R.Symbol = "";
354353 }
355354
356 return obj2yaml_error::success;
357 }
358
359 template
360 std::error_code ELFDumper::dumpCommonSection(const Elf_Shdr *Shdr,
361 ELFYAML::Section &S) {
355 return Error::success();
356 }
357
358 template
359 Error ELFDumper::dumpCommonSection(const Elf_Shdr *Shdr,
360 ELFYAML::Section &S) {
362361 // Dump fields. We do not dump the ShOffset field. When not explicitly
363362 // set, the value is set by yaml2obj automatically.
364363 S.Type = Shdr->sh_type;
371370
372371 auto NameOrErr = getUniquedSectionName(Shdr);
373372 if (!NameOrErr)
374 return errorToErrorCode(NameOrErr.takeError());
373 return NameOrErr.takeError();
375374 S.Name = NameOrErr.get();
376375
377376 if (Shdr->sh_link != ELF::SHN_UNDEF) {
378377 auto LinkSection = Obj.getSection(Shdr->sh_link);
379378 if (LinkSection.takeError())
380 return errorToErrorCode(LinkSection.takeError());
379 return LinkSection.takeError();
381380 NameOrErr = getUniquedSectionName(*LinkSection);
382381 if (!NameOrErr)
383 return errorToErrorCode(NameOrErr.takeError());
382 return NameOrErr.takeError();
384383 S.Link = NameOrErr.get();
385384 }
386385
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;
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;
396394
397395 auto InfoSection = Obj.getSection(Shdr->sh_info);
398396 if (!InfoSection)
399 return errorToErrorCode(InfoSection.takeError());
397 return InfoSection.takeError();
400398
401399 auto NameOrErr = getUniquedSectionName(*InfoSection);
402400 if (!NameOrErr)
403 return errorToErrorCode(NameOrErr.takeError());
401 return NameOrErr.takeError();
404402 S.RelocatableSec = NameOrErr.get();
405403
406 return obj2yaml_error::success;
407 }
408
409 template
410 ErrorOr
404 return Error::success();
405 }
406
407 template
408 Expected
411409 ELFDumper::dumpDynamicSection(const Elf_Shdr *Shdr) {
412410 auto S = make_unique();
413 if (std::error_code EC = dumpCommonSection(Shdr, *S))
414 return EC;
411 if (Error E = dumpCommonSection(Shdr, *S))
412 return std::move(E);
415413
416414 auto DynTagsOrErr = Obj.template getSectionContentsAsArray(Shdr);
417415 if (!DynTagsOrErr)
418 return errorToErrorCode(DynTagsOrErr.takeError());
416 return DynTagsOrErr.takeError();
419417
420418 for (const Elf_Dyn &Dyn : *DynTagsOrErr)
421419 S->Entries.push_back({(ELFYAML::ELF_DYNTAG)Dyn.getTag(), Dyn.getVal()});
424422 }
425423
426424 template
427 ErrorOr
425 Expected
428426 ELFDumper::dumpRelocSection(const Elf_Shdr *Shdr) {
429427 auto S = make_unique();
430 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
431 return EC;
428 if (auto E = dumpCommonRelocationSection(Shdr, *S))
429 return std::move(E);
432430
433431 auto SymTabOrErr = Obj.getSection(Shdr->sh_link);
434432 if (!SymTabOrErr)
435 return errorToErrorCode(SymTabOrErr.takeError());
433 return SymTabOrErr.takeError();
436434 const Elf_Shdr *SymTab = *SymTabOrErr;
437435
438436 if (Shdr->sh_type == ELF::SHT_REL) {
439437 auto Rels = Obj.rels(Shdr);
440438 if (!Rels)
441 return errorToErrorCode(Rels.takeError());
439 return Rels.takeError();
442440 for (const Elf_Rel &Rel : *Rels) {
443441 ELFYAML::Relocation R;
444 if (std::error_code EC = dumpRelocation(&Rel, SymTab, R))
445 return EC;
442 if (Error E = dumpRelocation(&Rel, SymTab, R))
443 return std::move(E);
446444 S->Relocations.push_back(R);
447445 }
448446 } else {
449447 auto Rels = Obj.relas(Shdr);
450448 if (!Rels)
451 return errorToErrorCode(Rels.takeError());
449 return Rels.takeError();
452450 for (const Elf_Rela &Rel : *Rels) {
453451 ELFYAML::Relocation R;
454 if (std::error_code EC = dumpRelocation(&Rel, SymTab, R))
455 return EC;
452 if (Error E = dumpRelocation(&Rel, SymTab, R))
453 return std::move(E);
456454 R.Addend = Rel.r_addend;
457455 S->Relocations.push_back(R);
458456 }
462460 }
463461
464462 template
465 ErrorOr
463 Expected
466464 ELFDumper::dumpContentSection(const Elf_Shdr *Shdr) {
467465 auto S = make_unique();
468
469 if (std::error_code EC = dumpCommonSection(Shdr, *S))
470 return EC;
466 if (Error E = dumpCommonSection(Shdr, *S))
467 return std::move(E);
471468
472469 auto ContentOrErr = Obj.getSectionContents(Shdr);
473470 if (!ContentOrErr)
474 return errorToErrorCode(ContentOrErr.takeError());
471 return ContentOrErr.takeError();
475472 ArrayRef Content = *ContentOrErr;
476473 if (!Content.empty())
477474 S->Content = yaml::BinaryRef(Content);
481478 }
482479
483480 template
484 ErrorOr
481 Expected
485482 ELFDumper::dumpNoBitsSection(const Elf_Shdr *Shdr) {
486483 auto S = make_unique();
487
488 if (std::error_code EC = dumpCommonSection(Shdr, *S))
489 return EC;
484 if (Error E = dumpCommonSection(Shdr, *S))
485 return std::move(E);
490486 S->Size = Shdr->sh_size;
491487
492488 return S.release();
493489 }
494490
495491 template
496 ErrorOr
492 Expected
497493 ELFDumper::dumpVerdefSection(const Elf_Shdr *Shdr) {
498494 typedef typename ELFT::Verdef Elf_Verdef;
499495 typedef typename ELFT::Verdaux Elf_Verdaux;
500496
501497 auto S = make_unique();
502 if (std::error_code EC = dumpCommonSection(Shdr, *S))
503 return EC;
498 if (Error E = dumpCommonSection(Shdr, *S))
499 return std::move(E);
504500
505501 S->Info = Shdr->sh_info;
506502
507503 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link);
508504 if (!StringTableShdrOrErr)
509 return errorToErrorCode(StringTableShdrOrErr.takeError());
505 return StringTableShdrOrErr.takeError();
510506
511507 auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr);
512508 if (!StringTableOrErr)
513 return errorToErrorCode(StringTableOrErr.takeError());
509 return StringTableOrErr.takeError();
514510
515511 auto Contents = Obj.getSectionContents(Shdr);
516512 if (!Contents)
517 return errorToErrorCode(Contents.takeError());
513 return Contents.takeError();
518514
519515 llvm::ArrayRef Data = *Contents;
520516 const uint8_t *Buf = Data.data();
543539 }
544540
545541 template
546 ErrorOr
542 Expected
547543 ELFDumper::dumpSymverSection(const Elf_Shdr *Shdr) {
548544 typedef typename ELFT::Half Elf_Half;
549545
550546 auto S = make_unique();
551 if (std::error_code EC = dumpCommonSection(Shdr, *S))
552 return EC;
547 if (Error E = dumpCommonSection(Shdr, *S))
548 return std::move(E);
553549
554550 auto VersionsOrErr = Obj.template getSectionContentsAsArray(Shdr);
555551 if (!VersionsOrErr)
556 return errorToErrorCode(VersionsOrErr.takeError());
552 return VersionsOrErr.takeError();
557553 for (const Elf_Half &E : *VersionsOrErr)
558554 S->Entries.push_back(E);
559555
561557 }
562558
563559 template
564 ErrorOr
560 Expected
565561 ELFDumper::dumpVerneedSection(const Elf_Shdr *Shdr) {
566562 typedef typename ELFT::Verneed Elf_Verneed;
567563 typedef typename ELFT::Vernaux Elf_Vernaux;
568564
569565 auto S = make_unique();
570 if (std::error_code EC = dumpCommonSection(Shdr, *S))
571 return EC;
566 if (Error E = dumpCommonSection(Shdr, *S))
567 return std::move(E);
572568
573569 S->Info = Shdr->sh_info;
574570
575571 auto Contents = Obj.getSectionContents(Shdr);
576572 if (!Contents)
577 return errorToErrorCode(Contents.takeError());
573 return Contents.takeError();
578574
579575 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link);
580576 if (!StringTableShdrOrErr)
581 return errorToErrorCode(StringTableShdrOrErr.takeError());
577 return StringTableShdrOrErr.takeError();
582578
583579 auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr);
584580 if (!StringTableOrErr)
585 return errorToErrorCode(StringTableOrErr.takeError());
581 return StringTableOrErr.takeError();
586582
587583 llvm::ArrayRef Data = *Contents;
588584 const uint8_t *Buf = Data.data();
618614 }
619615
620616 template
621 ErrorOr ELFDumper::dumpGroup(const Elf_Shdr *Shdr) {
617 Expected ELFDumper::dumpGroup(const Elf_Shdr *Shdr) {
622618 auto S = make_unique();
623 if (std::error_code EC = dumpCommonSection(Shdr, *S))
624 return EC;
619 if (Error E = dumpCommonSection(Shdr, *S))
620 return std::move(E);
625621
626622 auto SymtabOrErr = Obj.getSection(Shdr->sh_link);
627623 if (!SymtabOrErr)
628 return errorToErrorCode(SymtabOrErr.takeError());
624 return SymtabOrErr.takeError();
629625 // Get symbol with index sh_info which name is the signature of the group.
630626 const Elf_Shdr *Symtab = *SymtabOrErr;
631627 auto SymOrErr = Obj.getSymbol(Symtab, Shdr->sh_info);
632628 if (!SymOrErr)
633 return errorToErrorCode(SymOrErr.takeError());
629 return SymOrErr.takeError();
634630 auto StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
635631 if (!StrTabOrErr)
636 return errorToErrorCode(StrTabOrErr.takeError());
632 return StrTabOrErr.takeError();
637633
638634 Expected SymbolName =
639635 getUniquedSymbolName(*SymOrErr, *StrTabOrErr, Symtab);
640636 if (!SymbolName)
641 return errorToErrorCode(SymbolName.takeError());
637 return SymbolName.takeError();
642638 S->Signature = *SymbolName;
643639
644640 auto MembersOrErr = Obj.template getSectionContentsAsArray(Shdr);
645641 if (!MembersOrErr)
646 return errorToErrorCode(MembersOrErr.takeError());
642 return MembersOrErr.takeError();
647643
648644 for (Elf_Word Member : *MembersOrErr) {
649645 if (Member == llvm::ELF::GRP_COMDAT) {
653649
654650 auto SHdrOrErr = Obj.getSection(Member);
655651 if (!SHdrOrErr)
656 return errorToErrorCode(SHdrOrErr.takeError());
652 return SHdrOrErr.takeError();
657653 auto NameOrErr = getUniquedSectionName(*SHdrOrErr);
658654 if (!NameOrErr)
659 return errorToErrorCode(NameOrErr.takeError());
655 return NameOrErr.takeError();
660656 S->Members.push_back({*NameOrErr});
661657 }
662658 return S.release();
663659 }
664660
665661 template
666 ErrorOr
662 Expected
667663 ELFDumper::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
668664 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
669665 "Section type is not SHT_MIPS_ABIFLAGS");
670666 auto S = make_unique();
671 if (std::error_code EC = dumpCommonSection(Shdr, *S))
672 return EC;
667 if (Error E = dumpCommonSection(Shdr, *S))
668 return std::move(E);
673669
674670 auto ContentOrErr = Obj.getSectionContents(Shdr);
675671 if (!ContentOrErr)
676 return errorToErrorCode(ContentOrErr.takeError());
672 return ContentOrErr.takeError();
677673
678674 auto *Flags = reinterpret_cast *>(
679675 ContentOrErr.get().data());
692688 }
693689
694690 template
695 static std::error_code elf2yaml(raw_ostream &Out,
696 const object::ELFFile &Obj) {
691 static Error elf2yaml(raw_ostream &Out, const object::ELFFile &Obj) {
697692 ELFDumper Dumper(Obj);
698 ErrorOr YAMLOrErr = Dumper.dump();
699 if (std::error_code EC = YAMLOrErr.getError())
700 return EC;
693 Expected YAMLOrErr = Dumper.dump();
694 if (!YAMLOrErr)
695 return YAMLOrErr.takeError();
701696
702697 std::unique_ptr YAML(YAMLOrErr.get());
703698 yaml::Output Yout(Out);
704699 Yout << *YAML;
705700
706 return std::error_code();
707 }
708
709 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
701 return Error::success();
702 }
703
704 Error elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
710705 if (const auto *ELFObj = dyn_cast(&Obj))
711706 return elf2yaml(Out, *ELFObj->getELFFile());
712707
719714 if (const auto *ELFObj = dyn_cast(&Obj))
720715 return elf2yaml(Out, *ELFObj->getELFFile());
721716
722 return obj2yaml_error::unsupported_obj_file_format;
723 }
717 llvm_unreachable("unknown ELF file format");
718 }
1616 using namespace llvm;
1717 using namespace llvm::object;
1818
19 static std::error_code dumpObject(const ObjectFile &Obj) {
19 static Error dumpObject(const ObjectFile &Obj) {
2020 if (Obj.isCOFF())
21 return coff2yaml(outs(), cast(Obj));
21 return errorCodeToError(coff2yaml(outs(), cast(Obj)));
2222
2323 if (Obj.isXCOFF())
24 return xcoff2yaml(outs(), cast(Obj));
24 return errorCodeToError(xcoff2yaml(outs(), cast(Obj)));
2525
2626 if (Obj.isELF())
2727 return elf2yaml(outs(), Obj);
28
2829 if (Obj.isWasm())
29 return wasm2yaml(outs(), cast(Obj));
30 return errorCodeToError(wasm2yaml(outs(), cast(Obj)));
3031
31 return obj2yaml_error::unsupported_obj_file_format;
32 return errorCodeToError(obj2yaml_error::unsupported_obj_file_format);
3233 }
3334
3435 static Error dumpInput(StringRef File) {
4344 return errorCodeToError(macho2yaml(outs(), Binary));
4445 // TODO: If this is an archive, then burst it and dump each entry
4546 if (ObjectFile *Obj = dyn_cast(&Binary))
46 return errorCodeToError(dumpObject(*Obj));
47 return dumpObject(*Obj);
4748 if (MinidumpFile *Minidump = dyn_cast(&Binary))
4849 return minidump2yaml(outs(), *Minidump);
4950
2020
2121 std::error_code coff2yaml(llvm::raw_ostream &Out,
2222 const llvm::object::COFFObjectFile &Obj);
23 std::error_code elf2yaml(llvm::raw_ostream &Out,
23 llvm::Error 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);