llvm.org GIT mirror llvm / 63830bc
[ELF] Convert ELF.h to Expected<T>. This has two advantages: 1) We slowly move away from ErrorOr to the new handling interface, in the hope of having an uniform error handling in LLVM, eventually. 2) We're starting to have *meaningful* error messages for invalid object ELF files, rather than a generic "parse error". At some point we should include also the offset to improve the quality of the diagnostic. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@287081 91177308-0d34-0410-b5e6-96231b3b80d8 Davide Italiano 3 years ago
8 changed file(s) with 321 addition(s) and 276 deletion(s). Raw diff Collapse all Expand all
3030 (uint8_t)ELF::ELFDATANONE);
3131 return std::make_pair((uint8_t)Object[ELF::EI_CLASS],
3232 (uint8_t)Object[ELF::EI_DATA]);
33 }
34
35 static inline Error createError(StringRef Err) {
36 return make_error(Err, object_error::parse_failed);
3337 }
3438
3539 template
7478 }
7579
7680 template
77 ErrorOr getEntry(uint32_t Section, uint32_t Entry) const;
81 Expected getEntry(uint32_t Section, uint32_t Entry) const;
7882 template
79 ErrorOr getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
80
81 ErrorOr getStringTable(const Elf_Shdr *Section) const;
82 ErrorOr getStringTableForSymtab(const Elf_Shdr &Section) const;
83 ErrorOr getStringTableForSymtab(const Elf_Shdr &Section,
83 Expected getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
84
85 Expected getStringTable(const Elf_Shdr *Section) const;
86 Expected getStringTableForSymtab(const Elf_Shdr &Section) const;
87 Expected getStringTableForSymtab(const Elf_Shdr &Section,
88 Elf_Shdr_Range Sections) const;
89
90 Expected> getSHNDXTable(const Elf_Shdr &Section) const;
91 Expected> getSHNDXTable(const Elf_Shdr &Section,
8492 Elf_Shdr_Range Sections) const;
85
86 ErrorOr> getSHNDXTable(const Elf_Shdr &Section) const;
87 ErrorOr> getSHNDXTable(const Elf_Shdr &Section,
88 Elf_Shdr_Range Sections) const;
8993
9094 void VerifyStrTab(const Elf_Shdr *sh) const;
9195
9498 SmallVectorImpl &Result) const;
9599
96100 /// \brief Get the symbol for a given relocation.
97 ErrorOr getRelocationSymbol(const Elf_Rel *Rel,
98 const Elf_Shdr *SymTab) const;
101 Expected getRelocationSymbol(const Elf_Rel *Rel,
102 const Elf_Shdr *SymTab) const;
99103
100104 ELFFile(StringRef Object);
101105
109113 getHeader()->getDataEncoding() == ELF::ELFDATA2LSB;
110114 }
111115
112 ErrorOr sections() const;
113
114 ErrorOr symbols(const Elf_Shdr *Sec) const {
116 Expected sections() const;
117
118 Expected symbols(const Elf_Shdr *Sec) const {
115119 if (!Sec)
116120 return makeArrayRef(nullptr, nullptr);
117121 return getSectionContentsAsArray(Sec);
118122 }
119123
120 ErrorOr relas(const Elf_Shdr *Sec) const {
124 Expected relas(const Elf_Shdr *Sec) const {
121125 return getSectionContentsAsArray(Sec);
122126 }
123127
124 ErrorOr rels(const Elf_Shdr *Sec) const {
128 Expected rels(const Elf_Shdr *Sec) const {
125129 return getSectionContentsAsArray(Sec);
126130 }
127131
128132 /// \brief Iterate over program header table.
129 ErrorOr program_headers() const {
133 Expected program_headers() const {
130134 if (getHeader()->e_phnum && getHeader()->e_phentsize != sizeof(Elf_Phdr))
131 return object_error::parse_failed;
135 return createError("invalid e_phentsize");
132136 auto *Begin =
133137 reinterpret_cast(base() + getHeader()->e_phoff);
134138 return makeArrayRef(Begin, Begin + getHeader()->e_phnum);
135139 }
136140
137 ErrorOr getSectionStringTable(Elf_Shdr_Range Sections) const;
138 ErrorOr getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms,
139 ArrayRef ShndxTable) const;
140 ErrorOr getSection(const Elf_Sym *Sym,
141 const Elf_Shdr *SymTab,
142 ArrayRef ShndxTable) const;
143 ErrorOr getSection(const Elf_Sym *Sym, Elf_Sym_Range Symtab,
144 ArrayRef ShndxTable) const;
145 ErrorOr getSection(uint32_t Index) const;
146
147 ErrorOr getSymbol(const Elf_Shdr *Sec,
148 uint32_t Index) const;
149
150 ErrorOr getSectionName(const Elf_Shdr *Section) const;
151 ErrorOr getSectionName(const Elf_Shdr *Section,
152 StringRef DotShstrtab) const;
141 Expected getSectionStringTable(Elf_Shdr_Range Sections) const;
142 Expected getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms,
143 ArrayRef ShndxTable) const;
144 Expected getSection(const Elf_Sym *Sym,
145 const Elf_Shdr *SymTab,
146 ArrayRef ShndxTable) const;
147 Expected getSection(const Elf_Sym *Sym,
148 Elf_Sym_Range Symtab,
149 ArrayRef ShndxTable) const;
150 Expected getSection(uint32_t Index) const;
151
152 Expected getSymbol(const Elf_Shdr *Sec,
153 uint32_t Index) const;
154
155 Expected getSectionName(const Elf_Shdr *Section) const;
156 Expected getSectionName(const Elf_Shdr *Section,
157 StringRef DotShstrtab) const;
153158 template
154 ErrorOr> getSectionContentsAsArray(const Elf_Shdr *Sec) const;
155 ErrorOr > getSectionContents(const Elf_Shdr *Sec) const;
159 Expected> getSectionContentsAsArray(const Elf_Shdr *Sec) const;
160 Expected> getSectionContents(const Elf_Shdr *Sec) const;
156161 };
157162
158163 typedef ELFFile> ELF32LEFile;
161166 typedef ELFFile> ELF64BEFile;
162167
163168 template
164 inline ErrorOr
169 inline Expected
165170 getSection(typename ELFT::ShdrRange Sections, uint32_t Index) {
166171 if (Index >= Sections.size())
167 return object_error::invalid_section_index;
172 return createError("invalid section index");
168173 return &Sections[Index];
169174 }
170175
171176 template
172 inline ErrorOr
177 inline Expected
173178 getExtendedSymbolTableIndex(const typename ELFT::Sym *Sym,
174179 const typename ELFT::Sym *FirstSym,
175180 ArrayRef ShndxTable) {
176181 assert(Sym->st_shndx == ELF::SHN_XINDEX);
177182 unsigned Index = Sym - FirstSym;
178183 if (Index >= ShndxTable.size())
179 return object_error::parse_failed;
184 return createError("index past the end of the symbol table");
185
180186 // The size of the table was checked in getSHNDXTable.
181187 return ShndxTable[Index];
182188 }
183189
184190 template
185 ErrorOr
191 Expected
186192 ELFFile::getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms,
187193 ArrayRef ShndxTable) const {
188194 uint32_t Index = Sym->st_shndx;
189195 if (Index == ELF::SHN_XINDEX) {
190196 auto ErrorOrIndex = object::getExtendedSymbolTableIndex(
191197 Sym, Syms.begin(), ShndxTable);
192 if (std::error_code EC = ErrorOrIndex.getError())
193 return EC;
198 if (!ErrorOrIndex)
199 return ErrorOrIndex.takeError();
194200 return *ErrorOrIndex;
195201 }
196202 if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
199205 }
200206
201207 template
202 ErrorOr
208 Expected
203209 ELFFile::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
204210 ArrayRef ShndxTable) const {
205211 auto SymsOrErr = symbols(SymTab);
206 if (std::error_code EC = SymsOrErr.getError())
207 return EC;
212 if (!SymsOrErr)
213 return SymsOrErr.takeError();
208214 return getSection(Sym, *SymsOrErr, ShndxTable);
209215 }
210216
211217 template
212 ErrorOr
218 Expected
213219 ELFFile::getSection(const Elf_Sym *Sym, Elf_Sym_Range Symbols,
214220 ArrayRef ShndxTable) const {
215 ErrorOr IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable);
216 if (std::error_code EC = IndexOrErr.getError())
217 return EC;
221 auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable);
222 if (!IndexOrErr)
223 return IndexOrErr.takeError();
218224 uint32_t Index = *IndexOrErr;
219225 if (Index == 0)
220226 return nullptr;
221227 auto SectionsOrErr = sections();
222 if (std::error_code EC = SectionsOrErr.getError())
223 return EC;
228 if (!SectionsOrErr)
229 return SectionsOrErr.takeError();
224230 return object::getSection(*SectionsOrErr, Index);
225231 }
226232
227233 template
228 inline ErrorOr
234 inline Expected
229235 getSymbol(typename ELFT::SymRange Symbols, uint32_t Index) {
230236 if (Index >= Symbols.size())
231 return object_error::invalid_symbol_index;
237 return createError("invalid symbol index");
232238 return &Symbols[Index];
233239 }
234240
235241 template
236 ErrorOr
242 Expected
237243 ELFFile::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
238244 auto SymtabOrErr = symbols(Sec);
239 if (std::error_code EC = SymtabOrErr.getError())
240 return EC;
245 if (!SymtabOrErr)
246 return SymtabOrErr.takeError();
241247 return object::getSymbol(*SymtabOrErr, Index);
242248 }
243249
244250 template
245251 template
246 ErrorOr>
252 Expected>
247253 ELFFile::getSectionContentsAsArray(const Elf_Shdr *Sec) const {
248254 if (Sec->sh_entsize != sizeof(T) && sizeof(T) != 1)
249 return object_error::parse_failed;
255 return createError("invalid sh_entsize");
250256
251257 uintX_t Offset = Sec->sh_offset;
252258 uintX_t Size = Sec->sh_size;
253259
254260 if (Size % sizeof(T))
255 return object_error::parse_failed;
261 return createError("size is not a multiple of sh_entsize");
256262 if ((std::numeric_limits::max() - Offset < Size) ||
257263 Offset + Size > Buf.size())
258 return object_error::parse_failed;
264 return createError("invalid section offset");
259265
260266 const T *Start = reinterpret_cast(base() + Offset);
261267 return makeArrayRef(Start, Size / sizeof(T));
262268 }
263269
264270 template
265 ErrorOr>
271 Expected>
266272 ELFFile::getSectionContents(const Elf_Shdr *Sec) const {
267273 return getSectionContentsAsArray(Sec);
268274 }
304310 }
305311
306312 template
307 ErrorOr
313 Expected
308314 ELFFile::getRelocationSymbol(const Elf_Rel *Rel,
309315 const Elf_Shdr *SymTab) const {
310316 uint32_t Index = Rel->getSymbol(isMips64EL());
314320 }
315321
316322 template
317 ErrorOr
323 Expected
318324 ELFFile::getSectionStringTable(Elf_Shdr_Range Sections) const {
319325 uint32_t Index = getHeader()->e_shstrndx;
320326 if (Index == ELF::SHN_XINDEX)
323329 if (!Index) // no section string table.
324330 return "";
325331 if (Index >= Sections.size())
326 return object_error::parse_failed;
332 return createError("invalid section index");
327333 return getStringTable(&Sections[Index]);
328334 }
329335
338344 }
339345
340346 template
341 ErrorOr ELFFile::sections() const {
347 Expected ELFFile::sections() const {
342348 const uintX_t SectionTableOffset = getHeader()->e_shoff;
343349 if (SectionTableOffset == 0)
344350 return ArrayRef();
345351
346 // Invalid section header entry size (e_shentsize) in ELF header
347352 if (getHeader()->e_shentsize != sizeof(Elf_Shdr))
348 return object_error::parse_failed;
353 return createError(
354 "invalid section header entry size (e_shentsize) in ELF header");
349355
350356 const uint64_t FileSize = Buf.size();
351357
352 // Section header table goes past end of file!
353358 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
354 return object_error::parse_failed;
359 return createError("section header table goes past the end of the file");
355360
356361 // Invalid address alignment of section headers
357362 if (SectionTableOffset & (alignof(Elf_Shdr) - 1))
358 return object_error::parse_failed;
363 return createError("invalid alignment of section headers");
359364
360365 const Elf_Shdr *First =
361366 reinterpret_cast(base() + SectionTableOffset);
364369 if (NumSections == 0)
365370 NumSections = First->sh_size;
366371
367 // Section table goes past end of file!
368372 if (NumSections > UINT64_MAX / sizeof(Elf_Shdr))
369 return object_error::parse_failed;
373 return createError("section table goes past the end of file");
370374
371375 const uint64_t SectionTableSize = NumSections * sizeof(Elf_Shdr);
372376
373377 // Section table goes past end of file!
374378 if (SectionTableOffset + SectionTableSize > FileSize)
375 return object_error::parse_failed;
379 return createError("section table goes past the end of file");
376380
377381 return makeArrayRef(First, NumSections);
378382 }
379383
380384 template
381385 template
382 ErrorOr ELFFile::getEntry(uint32_t Section,
383 uint32_t Entry) const {
384 ErrorOr Sec = getSection(Section);
385 if (std::error_code EC = Sec.getError())
386 return EC;
387 return getEntry(*Sec, Entry);
386 Expected ELFFile::getEntry(uint32_t Section,
387 uint32_t Entry) const {
388 auto SecOrErr = getSection(Section);
389 if (!SecOrErr)
390 return SecOrErr.takeError();
391 return getEntry(*SecOrErr, Entry);
388392 }
389393
390394 template
391395 template
392 ErrorOr ELFFile::getEntry(const Elf_Shdr *Section,
393 uint32_t Entry) const {
396 Expected ELFFile::getEntry(const Elf_Shdr *Section,
397 uint32_t Entry) const {
394398 if (sizeof(T) != Section->sh_entsize)
395 return object_error::parse_failed;
399 return createError("invalid sh_entsize");
396400 size_t Pos = Section->sh_offset + Entry * sizeof(T);
397401 if (Pos + sizeof(T) > Buf.size())
398 return object_error::parse_failed;
402 return createError("invalid section offset");
399403 return reinterpret_cast(base() + Pos);
400404 }
401405
402406 template
403 ErrorOr
407 Expected
404408 ELFFile::getSection(uint32_t Index) const {
405409 auto TableOrErr = sections();
406 if (std::error_code EC = TableOrErr.getError())
407 return EC;
410 if (!TableOrErr)
411 return TableOrErr.takeError();
408412 return object::getSection(*TableOrErr, Index);
409413 }
410414
411415 template
412 ErrorOr
416 Expected
413417 ELFFile::getStringTable(const Elf_Shdr *Section) const {
414418 if (Section->sh_type != ELF::SHT_STRTAB)
415 return object_error::parse_failed;
419 return createError("invalid sh_type for string table, expected SHT_STRTAB");
416420 auto V = getSectionContentsAsArray(Section);
417 if (std::error_code EC = V.getError())
418 return EC;
421 if (!V)
422 return V.takeError();
419423 ArrayRef Data = *V;
420424 if (Data.empty())
421 return object_error::parse_failed;
425 return createError("empty string table");
422426 if (Data.back() != '\0')
423 return object_error::string_table_non_null_end;
427 return createError("string table non-null terminated");
424428 return StringRef(Data.begin(), Data.size());
425429 }
426430
427431 template
428 ErrorOr>
432 Expected>
429433 ELFFile::getSHNDXTable(const Elf_Shdr &Section) const {
430434 auto SectionsOrErr = sections();
431 if (std::error_code EC = SectionsOrErr.getError())
432 return EC;
435 if (!SectionsOrErr)
436 return SectionsOrErr.takeError();
433437 return getSHNDXTable(Section, *SectionsOrErr);
434438 }
435439
436440 template
437 ErrorOr>
441 Expected>
438442 ELFFile::getSHNDXTable(const Elf_Shdr &Section,
439443 Elf_Shdr_Range Sections) const {
440444 assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
441445 auto VOrErr = getSectionContentsAsArray(&Section);
442 if (std::error_code EC = VOrErr.getError())
443 return EC;
446 if (!VOrErr)
447 return VOrErr.takeError();
444448 ArrayRef V = *VOrErr;
445 ErrorOr SymTableOrErr =
446 object::getSection(Sections, Section.sh_link);
447 if (std::error_code EC = SymTableOrErr.getError())
448 return EC;
449 auto SymTableOrErr = object::getSection(Sections, Section.sh_link);
450 if (!SymTableOrErr)
451 return SymTableOrErr.takeError();
449452 const Elf_Shdr &SymTable = **SymTableOrErr;
450453 if (SymTable.sh_type != ELF::SHT_SYMTAB &&
451454 SymTable.sh_type != ELF::SHT_DYNSYM)
452 return object_error::parse_failed;
455 return createError("invalid sh_type");
453456 if (V.size() != (SymTable.sh_size / sizeof(Elf_Sym)))
454 return object_error::parse_failed;
457 return createError("invalid section contents size");
455458 return V;
456459 }
457460
458461 template
459 ErrorOr
462 Expected
460463 ELFFile::getStringTableForSymtab(const Elf_Shdr &Sec) const {
461464 auto SectionsOrErr = sections();
462 if (std::error_code EC = SectionsOrErr.getError())
463 return EC;
465 if (!SectionsOrErr)
466 return SectionsOrErr.takeError();
464467 return getStringTableForSymtab(Sec, *SectionsOrErr);
465468 }
466469
467470 template
468 ErrorOr
471 Expected
469472 ELFFile::getStringTableForSymtab(const Elf_Shdr &Sec,
470473 Elf_Shdr_Range Sections) const {
471474
472475 if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
473 return object_error::parse_failed;
474 ErrorOr SectionOrErr =
475 object::getSection(Sections, Sec.sh_link);
476 if (std::error_code EC = SectionOrErr.getError())
477 return EC;
476 return createError(
477 "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM");
478 auto SectionOrErr = object::getSection(Sections, Sec.sh_link);
479 if (!SectionOrErr)
480 return SectionOrErr.takeError();
478481 return getStringTable(*SectionOrErr);
479482 }
480483
481484 template
482 ErrorOr
485 Expected
483486 ELFFile::getSectionName(const Elf_Shdr *Section) const {
484487 auto SectionsOrErr = sections();
485 if (std::error_code EC = SectionsOrErr.getError())
486 return EC;
487 ErrorOr Table = getSectionStringTable(*SectionsOrErr);
488 if (std::error_code EC = Table.getError())
489 return EC;
488 if (!SectionsOrErr)
489 return SectionsOrErr.takeError();
490 auto Table = getSectionStringTable(*SectionsOrErr);
491 if (!Table)
492 return Table.takeError();
490493 return getSectionName(Section, *Table);
491494 }
492495
493496 template
494 ErrorOr ELFFile::getSectionName(const Elf_Shdr *Section,
495 StringRef DotShstrtab) const {
497 Expected ELFFile::getSectionName(const Elf_Shdr *Section,
498 StringRef DotShstrtab) const {
496499 uint32_t Offset = Section->sh_name;
497500 if (Offset == 0)
498501 return StringRef();
499502 if (Offset >= DotShstrtab.size())
500 return object_error::parse_failed;
503 return createError("invalid string offset");
501504 return StringRef(DotShstrtab.data() + Offset);
502505 }
503506
254254
255255 /// \brief Get the relocation section that contains \a Rel.
256256 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
257 return *EF.getSection(Rel.d.a);
257 auto RelSecOrErr = EF.getSection(Rel.d.a);
258 if (!RelSecOrErr)
259 report_fatal_error(errorToErrorCode(RelSecOrErr.takeError()).message());
260 return *RelSecOrErr;
258261 }
259262
260263 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
267270 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
268271 SymTable->sh_type == ELF::SHT_DYNSYM);
269272
270 uintptr_t SHT = reinterpret_cast((*EF.sections()).begin());
273 auto SectionsOrErr = EF.sections();
274 if (!SectionsOrErr) {
275 DRI.d.a = 0;
276 DRI.d.b = 0;
277 return DRI;
278 }
279 uintptr_t SHT = reinterpret_cast((*SectionsOrErr).begin());
271280 unsigned SymTableIndex =
272281 (reinterpret_cast(SymTable) - SHT) / sizeof(Elf_Shdr);
273282
317326
318327 const Elf_Sym *getSymbol(DataRefImpl Sym) const {
319328 auto Ret = EF.template getEntry(Sym.d.a, Sym.d.b);
320 if (std::error_code EC = Ret.getError())
321 report_fatal_error(EC.message());
329 if (!Ret)
330 report_fatal_error(errorToErrorCode(Ret.takeError()).message());
322331 return *Ret;
323332 }
324333
372381 template
373382 Expected ELFObjectFile::getSymbolName(DataRefImpl Sym) const {
374383 const Elf_Sym *ESym = getSymbol(Sym);
375 const Elf_Shdr *SymTableSec = *EF.getSection(Sym.d.a);
376 const Elf_Shdr *StringTableSec = *EF.getSection(SymTableSec->sh_link);
377 StringRef SymTable = *EF.getStringTable(StringTableSec);
378 return ESym->getName(SymTable);
384 auto SymTabOrErr = EF.getSection(Sym.d.a);
385 if (!SymTabOrErr)
386 return SymTabOrErr.takeError();
387 const Elf_Shdr *SymTableSec = *SymTabOrErr;
388 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
389 if (!StrTabOrErr)
390 return StrTabOrErr.takeError();
391 const Elf_Shdr *StringTableSec = *StrTabOrErr;
392 auto SymStrTabOrErr = EF.getStringTable(StringTableSec);
393 if (!SymStrTabOrErr)
394 return SymStrTabOrErr.takeError();
395 return ESym->getName(*SymStrTabOrErr);
379396 }
380397
381398 template
422439 }
423440
424441 const Elf_Ehdr *Header = EF.getHeader();
425 const Elf_Shdr *SymTab = *EF.getSection(Symb.d.a);
442 auto SymTabOrErr = EF.getSection(Symb.d.a);
443 if (!SymTabOrErr)
444 return SymTabOrErr.takeError();
445 const Elf_Shdr *SymTab = *SymTabOrErr;
426446
427447 if (Header->e_type == ELF::ET_REL) {
428 ErrorOr SectionOrErr =
429 EF.getSection(ESym, SymTab, ShndxTable);
430 if (std::error_code EC = SectionOrErr.getError())
431 return errorCodeToError(EC);
448 auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable);
449 if (!SectionOrErr)
450 return SectionOrErr.takeError();
432451 const Elf_Shdr *Section = *SectionOrErr;
433452 if (Section)
434453 Result += Section->sh_addr;
508527 if (ESym->st_shndx == ELF::SHN_ABS)
509528 Result |= SymbolRef::SF_Absolute;
510529
511 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
512 ESym == (*EF.symbols(DotSymtabSec)).begin() ||
513 ESym == (*EF.symbols(DotDynSymSec)).begin())
530 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
531 Result |= SymbolRef::SF_FormatSpecific;
532
533 auto DotSymtabSecSyms = EF.symbols(DotSymtabSec);
534 if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
535 Result |= SymbolRef::SF_FormatSpecific;
536 auto DotDynSymSecSyms = EF.symbols(DotDynSymSec);
537 if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
514538 Result |= SymbolRef::SF_FormatSpecific;
515539
516540 if (EF.getHeader()->e_machine == ELF::EM_ARM) {
546570 Expected
547571 ELFObjectFile::getSymbolSection(const Elf_Sym *ESym,
548572 const Elf_Shdr *SymTab) const {
549 ErrorOr ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
550 if (std::error_code EC = ESecOrErr.getError())
551 return errorCodeToError(EC);
573 auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
574 if (!ESecOrErr)
575 return ESecOrErr.takeError();
552576
553577 const Elf_Shdr *ESec = *ESecOrErr;
554578 if (!ESec)
563587 Expected
564588 ELFObjectFile::getSymbolSection(DataRefImpl Symb) const {
565589 const Elf_Sym *Sym = getSymbol(Symb);
566 const Elf_Shdr *SymTab = *EF.getSection(Symb.d.a);
590 auto SymTabOrErr = EF.getSection(Symb.d.a);
591 if (!SymTabOrErr)
592 return SymTabOrErr.takeError();
593 const Elf_Shdr *SymTab = *SymTabOrErr;
567594 return getSymbolSection(Sym, SymTab);
568595 }
569596
576603 template
577604 std::error_code ELFObjectFile::getSectionName(DataRefImpl Sec,
578605 StringRef &Result) const {
579 ErrorOr Name = EF.getSectionName(&*getSection(Sec));
606 auto Name = EF.getSectionName(&*getSection(Sec));
580607 if (!Name)
581 return Name.getError();
608 return errorToErrorCode(Name.takeError());
582609 Result = *Name;
583610 return std::error_code();
584611 }
640667 relocation_iterator
641668 ELFObjectFile::section_rel_begin(DataRefImpl Sec) const {
642669 DataRefImpl RelData;
643 uintptr_t SHT = reinterpret_cast((*EF.sections()).begin());
670 auto SectionsOrErr = EF.sections();
671 if (!SectionsOrErr)
672 return relocation_iterator(RelocationRef());
673 uintptr_t SHT = reinterpret_cast((*SectionsOrErr).begin());
644674 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
645675 RelData.d.b = 0;
646676 return relocation_iterator(RelocationRef(RelData, this));
657687 const Elf_Shdr *RelSec = getRelSection(RelData);
658688
659689 // Error check sh_link here so that getRelocationSymbol can just use it.
660 ErrorOr SymSecOrErr = EF.getSection(RelSec->sh_link);
661 if (std::error_code EC = SymSecOrErr.getError())
662 report_fatal_error(EC.message());
690 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
691 if (!SymSecOrErr)
692 report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message());
663693
664694 RelData.d.b += S->sh_size / S->sh_entsize;
665695 return relocation_iterator(RelocationRef(RelData, this));
676706 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
677707 return section_end();
678708
679 ErrorOr R = EF.getSection(EShdr->sh_info);
680 if (std::error_code EC = R.getError())
681 report_fatal_error(EC.message());
709 auto R = EF.getSection(EShdr->sh_info);
710 if (!R)
711 report_fatal_error(errorToErrorCode(R.takeError()).message());
682712 return section_iterator(SectionRef(toDRI(*R), this));
683713 }
684714
752782 ELFObjectFile::getRel(DataRefImpl Rel) const {
753783 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
754784 auto Ret = EF.template getEntry(Rel.d.a, Rel.d.b);
755 if (std::error_code EC = Ret.getError())
756 report_fatal_error(EC.message());
785 if (!Ret)
786 report_fatal_error(errorToErrorCode(Ret.takeError()).message());
757787 return *Ret;
758788 }
759789
762792 ELFObjectFile::getRela(DataRefImpl Rela) const {
763793 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
764794 auto Ret = EF.template getEntry(Rela.d.a, Rela.d.b);
765 if (std::error_code EC = Ret.getError())
766 report_fatal_error(EC.message());
795 if (!Ret)
796 report_fatal_error(errorToErrorCode(Ret.takeError()).message());
767797 return *Ret;
768798 }
769799
774804 Object),
775805 EF(Data.getBuffer()) {
776806 auto SectionsOrErr = EF.sections();
777 if ((EC = SectionsOrErr.getError()))
807 if (!SectionsOrErr) {
808 EC = errorToErrorCode(SectionsOrErr.takeError());
778809 return;
810 }
779811 for (const Elf_Shdr &Sec : *SectionsOrErr) {
780812 switch (Sec.sh_type) {
781813 case ELF::SHT_DYNSYM: {
797829 break;
798830 }
799831 case ELF::SHT_SYMTAB_SHNDX: {
800 ErrorOr> TableOrErr = EF.getSHNDXTable(Sec);
801 if ((EC = TableOrErr.getError()))
832 auto TableOrErr = EF.getSHNDXTable(Sec);
833 if (!TableOrErr) {
834 EC = errorToErrorCode(TableOrErr.takeError());
802835 return;
836 }
803837 ShndxTable = *TableOrErr;
804838 break;
805839 }
837871
838872 template
839873 section_iterator ELFObjectFile::section_begin() const {
840 return section_iterator(SectionRef(toDRI((*EF.sections()).begin()), this));
874 auto SectionsOrErr = EF.sections();
875 if (!SectionsOrErr)
876 return section_iterator(SectionRef());
877 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
841878 }
842879
843880 template
844881 section_iterator ELFObjectFile::section_end() const {
845 return section_iterator(SectionRef(toDRI((*EF.sections()).end()), this));
882 auto SectionsOrErr = EF.sections();
883 if (!SectionsOrErr)
884 return section_iterator(SectionRef());
885 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
846886 }
847887
848888 template
11 RUN: not llvm-readobj %p/Inputs/corrupt.elf-x86-64 -sections \
22 RUN: 2>&1 | FileCheck --check-prefix=SECNAME %s
33
4 SECNAME: Error reading file: Invalid data was encountered while parsing the file.
4 SECNAME: invalid string offset
55
66
77 // Section data offset past end of file.
88 RUN: not llvm-readobj %p/Inputs/corrupt.elf-x86-64 -sections -section-data \
99 RUN: 2>&1 | FileCheck --check-prefix=SECDATA %s
1010
11 SECDATA: Error reading file: Invalid data was encountered while parsing the file.
11 SECDATA: invalid section offset
1212
1313
1414 // Symbol name offset overflows string table.
1515 RUN: not llvm-readobj %p/Inputs/corrupt.elf-x86-64 -symbols \
1616 RUN: 2>&1 | FileCheck --check-prefix=SYMNAME %s
1717
18 SYMNAME: Error reading file: Invalid data was encountered while parsing the file.
18 SYMNAME: invalid string offset
1919
2020
2121 // Version index in .gnu.version overflows the version map.
3535 RUN: %p/Inputs/corrupt-invalid-phentsize.elf.x86-64 2>&1 | \
3636 RUN: FileCheck --check-prefix=PHENTSIZE %s
3737
38 PHENTSIZE: Invalid data was encountered while parsing the file.
38 PHENTSIZE: invalid e_phentsize
3939
4040 RUN: not llvm-readobj -dynamic-table \
4141 RUN: %p/Inputs/corrupt-invalid-virtual-addr.elf.x86-64 2>&1 | \
44 CHECK: Invalid data was encountered while parsing the file
55
66 RUN: not llvm-objdump -s %p/Inputs/invalid-strtab-non-null.elf 2>&1 | FileCheck --check-prefix=NON-NULL %s
7 NON-NULL: String table must end with a null terminator
7 NON-NULL: Invalid data was encountered while parsing the file
88
99 Test the sh_entsize are invalid
1010 RUN: llvm-readobj -s %p/Inputs/invalid-sh_entsize.elf | FileCheck --check-prefix=SECTION %s
3535 SECTION-NEXT: EntrySize: 32
3636
3737 RUN: not llvm-readobj -t %p/Inputs/invalid-sh_entsize.elf 2>&1 | FileCheck --check-prefix=INVALID-SYM-SIZE %s
38 INVALID-SYM-SIZE: Invalid data was encountered while parsing the file
38 INVALID-SYM-SIZE: invalid sh_entsize
3939
4040 RUN: not llvm-readobj --dyn-symbols %p/Inputs/invalid-sh_entsize.elf 2>&1 | FileCheck --check-prefix=INVALID-DYNSYM-SIZE %s
4141 INVALID-DYNSYM-SIZE: Invalid entity size
4242
4343 RUN: not llvm-readobj -t %p/Inputs/invalid-section-index.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-INDEX %s
44 INVALID-SECTION-INDEX: Invalid section index
44 INVALID-SECTION-INDEX: invalid section index
4545
4646 RUN: not llvm-readobj -s %p/Inputs/invalid-section-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-SIZE %s
4747 INVALID-SECTION-SIZE: Invalid data was encountered while parsing the file
4848
4949
5050 RUN: not llvm-readobj -t %p/Inputs/invalid-symbol-table-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SYMTAB-SIZE %s
51 INVALID-SYMTAB-SIZE: Invalid data was encountered while parsing the file
51 INVALID-SYMTAB-SIZE: size is not a multiple of sh_entsize
5252
5353
5454 RUN: not llvm-readobj -t %p/Inputs/invalid-xindex-size.elf 2>&1 | FileCheck --check-prefix=INVALID-XINDEX-SIZE %s
5555 INVALID-XINDEX-SIZE: Invalid data was encountered while parsing the file.
5656
5757 RUN: not llvm-readobj -t %p/Inputs/invalid-e_shnum.elf 2>&1 | FileCheck --check-prefix=INVALID-SH-NUM %s
58 INVALID-SH-NUM: Invalid data was encountered while parsing the file.
58 INVALID-SH-NUM: invalid e_phentsize
5959
6060 RUN: not llvm-readobj -t %p/Inputs/invalid-ext-symtab-index.elf-x86-64 2>&1 | \
6161 RUN: FileCheck --check-prefix=INVALID-EXT-SYMTAB-INDEX %s
62 INVALID-EXT-SYMTAB-INDEX: Invalid data was encountered while parsing the file.
62 INVALID-EXT-SYMTAB-INDEX: index past the end of the symbol table
6363
6464 RUN: not llvm-readobj -r %p/Inputs/invalid-relocation-sec-sh_offset.elf-i386 2>&1 | \
6565 RUN: FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s
6666 RUN: not llvm-readobj -r %p/Inputs/invalid-relocation-sec-sh_offset.elf-x86-64 2>&1 | \
6767 RUN: FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s
68 INVALID-RELOC-SH-OFFSET: Invalid data was encountered while parsing the file
68 INVALID-RELOC-SH-OFFSET: invalid section offset
6969
7070 RUN: not llvm-readobj -t %p/Inputs/invalid-sections-address-alignment.x86-64 2>&1 | \
7171 RUN: FileCheck --check-prefix=INVALID-SEC-ADDRESS-ALIGNMENT %s
7373
7474 RUN: not llvm-readobj -t %p/Inputs/invalid-section-size2.elf 2>&1 | \
7575 RUN: FileCheck --check-prefix=INVALID-SECTION-SIZE2 %s
76 INVALID-SECTION-SIZE2: Invalid data was encountered while parsing the file.
76 INVALID-SECTION-SIZE2: invalid section offset
7777
7878 RUN: not llvm-readobj -t %p/Inputs/invalid-sections-num.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-NUM %s
7979 INVALID-SECTION-NUM: Invalid data was encountered while parsing the file.
8080
8181 RUN: not llvm-readobj -r %p/Inputs/invalid-rel-sym.elf 2>&1 | FileCheck --check-prefix=INVALID-REL-SYM %s
82 INVALID-REL-SYM: Invalid data was encountered while parsing the file.
82 INVALID-REL-SYM: invalid section offset
2424 typedef ELFFile ELFO;
2525 outs() << "Program Header:\n";
2626 auto ProgramHeaderOrError = o->program_headers();
27 if (std::error_code EC = ProgramHeaderOrError.getError())
28 report_fatal_error(EC.message());
27 if (!ProgramHeaderOrError)
28 report_fatal_error(
29 errorToErrorCode(ProgramHeaderOrError.takeError()).message());
2930 for (const typename ELFO::Elf_Phdr &Phdr : *ProgramHeaderOrError) {
3031 switch (Phdr.p_type) {
3132 case ELF::PT_LOAD:
607607
608608 const ELFFile &EF = *Obj->getELFFile();
609609
610 ErrorOr SecOrErr = EF.getSection(Rel.d.a);
611 if (std::error_code EC = SecOrErr.getError())
612 return EC;
610 auto SecOrErr = EF.getSection(Rel.d.a);
611 if (!SecOrErr)
612 return errorToErrorCode(SecOrErr.takeError());
613613 const Elf_Shdr *Sec = *SecOrErr;
614 ErrorOr SymTabOrErr = EF.getSection(Sec->sh_link);
615 if (std::error_code EC = SymTabOrErr.getError())
616 return EC;
614 auto SymTabOrErr = EF.getSection(Sec->sh_link);
615 if (!SymTabOrErr)
616 return errorToErrorCode(SymTabOrErr.takeError());
617617 const Elf_Shdr *SymTab = *SymTabOrErr;
618618 assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
619619 SymTab->sh_type == ELF::SHT_DYNSYM);
620 ErrorOr StrTabSec = EF.getSection(SymTab->sh_link);
621 if (std::error_code EC = StrTabSec.getError())
622 return EC;
623 ErrorOr StrTabOrErr = EF.getStringTable(*StrTabSec);
624 if (std::error_code EC = StrTabOrErr.getError())
625 return EC;
620 auto StrTabSec = EF.getSection(SymTab->sh_link);
621 if (!StrTabSec)
622 return errorToErrorCode(StrTabSec.takeError());
623 auto StrTabOrErr = EF.getStringTable(*StrTabSec);
624 if (!StrTabOrErr)
625 return errorToErrorCode(StrTabOrErr.takeError());
626626 StringRef StrTab = *StrTabOrErr;
627627 uint8_t type = RelRef.getType();
628628 StringRef res;
648648 if (!SymSI)
649649 return errorToErrorCode(SymSI.takeError());
650650 const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl());
651 ErrorOr SecName = EF.getSectionName(SymSec);
652 if (std::error_code EC = SecName.getError())
653 return EC;
651 auto SecName = EF.getSectionName(SymSec);
652 if (!SecName)
653 return errorToErrorCode(SecName.takeError());
654654 Target = *SecName;
655655 } else {
656656 Expected SymName = symb->getName(StrTab);
348348 ErrorOr
349349 PrinterContext::FunctionAtAddress(unsigned Section,
350350 uint64_t Address) const {
351 ErrorOr StrTableOrErr = ELF->getStringTableForSymtab(*Symtab);
352 error(StrTableOrErr.getError());
351 auto StrTableOrErr = ELF->getStringTableForSymtab(*Symtab);
352 if (!StrTableOrErr)
353 error(StrTableOrErr.takeError());
353354 StringRef StrTable = *StrTableOrErr;
354355
355356 for (const Elf_Sym &Sym : unwrapOrError(ELF->symbols(Symtab)))
382383 if (Sec.sh_type != ELF::SHT_REL || Sec.sh_info != IndexSectionIndex)
383384 continue;
384385
385 ErrorOr SymTabOrErr = ELF->getSection(Sec.sh_link);
386 error(SymTabOrErr.getError());
386 auto SymTabOrErr = ELF->getSection(Sec.sh_link);
387 if (!SymTabOrErr)
388 error(SymTabOrErr.takeError());
387389 const Elf_Shdr *SymTab = *SymTabOrErr;
388390
389391 for (const Elf_Rel &R : unwrapOrError(ELF->rels(&Sec))) {
398400 const Elf_Sym *Symbol =
399401 unwrapOrError(ELF->getRelocationSymbol(&RelA, SymTab));
400402
401 ErrorOr Ret =
402 ELF->getSection(Symbol, SymTab, ShndxTable);
403 if (std::error_code EC = Ret.getError())
404 report_fatal_error(EC.message());
403 auto Ret = ELF->getSection(Symbol, SymTab, ShndxTable);
404 if (!Ret)
405 report_fatal_error(errorToErrorCode(Ret.takeError()).message());
405406 return *Ret;
406407 }
407408 }
412413 void PrinterContext::PrintExceptionTable(const Elf_Shdr *IT,
413414 const Elf_Shdr *EHT,
414415 uint64_t TableEntryOffset) const {
415 ErrorOr > Contents = ELF->getSectionContents(EHT);
416 Expected> Contents = ELF->getSectionContents(EHT);
416417 if (!Contents)
417418 return;
418419
479480 template
480481 void PrinterContext::PrintIndexTable(unsigned SectionIndex,
481482 const Elf_Shdr *IT) const {
482 ErrorOr > Contents = ELF->getSectionContents(IT);
483 Expected> Contents = ELF->getSectionContents(IT);
483484 if (!Contents)
484485 return;
485486
532533 const Elf_Shdr *EHT =
533534 FindExceptionTable(SectionIndex, Entry * IndexTableEntrySize + 4);
534535
535 if (ErrorOr Name = ELF->getSectionName(EHT))
536 if (auto Name = ELF->getSectionName(EHT))
536537 SW.printString("ExceptionHandlingTable", *Name);
537538
538539 uint64_t TableEntryOffset = PREL31(Word1, IT->sh_addr);
553554 DictScope UIT(SW, "UnwindIndexTable");
554555
555556 SW.printNumber("SectionIndex", SectionIndex);
556 if (ErrorOr SectionName = ELF->getSectionName(&Sec))
557 if (auto SectionName = ELF->getSectionName(&Sec))
557558 SW.printString("SectionName", *SectionName);
558559 SW.printHex("SectionOffset", Sec.sh_offset);
559560
7474
7575 // Dump sections
7676 auto SectionsOrErr = Obj.sections();
77 if (std::error_code EC = SectionsOrErr.getError())
78 return EC;
77 if (!SectionsOrErr)
78 return errorToErrorCode(SectionsOrErr.takeError());
7979 for (const Elf_Shdr &Sec : *SectionsOrErr) {
8080 switch (Sec.sh_type) {
8181 case ELF::SHT_NULL:
8787 Symtab = &Sec;
8888 break;
8989 case ELF::SHT_SYMTAB_SHNDX: {
90 ErrorOr> TableOrErr = Obj.getSHNDXTable(Sec);
91 if (std::error_code EC = TableOrErr.getError())
92 return EC;
90 auto TableOrErr = Obj.getSHNDXTable(Sec);
91 if (!TableOrErr)
92 return errorToErrorCode(TableOrErr.takeError());
9393 ShndxTable = *TableOrErr;
9494 break;
9595 }
138138 }
139139
140140 // Dump symbols
141 ErrorOr StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
142 if (std::error_code EC = StrTableOrErr.getError())
143 return EC;
141 auto StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
142 if (!StrTableOrErr)
143 return errorToErrorCode(StrTableOrErr.takeError());
144144 StringRef StrTable = *StrTableOrErr;
145145
146146 bool IsFirstSym = true;
147147 auto SymtabOrErr = Obj.symbols(Symtab);
148 if (std::error_code EC = SymtabOrErr.getError())
149 return EC;
148 if (!SymtabOrErr)
149 return errorToErrorCode(SymtabOrErr.takeError());
150150 for (const Elf_Sym &Sym : *SymtabOrErr) {
151151 if (IsFirstSym) {
152152 IsFirstSym = false;
191191 return errorToErrorCode(SymbolNameOrErr.takeError());
192192 S.Name = SymbolNameOrErr.get();
193193
194 ErrorOr ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable);
195 if (std::error_code EC = ShdrOrErr.getError())
196 return EC;
194 auto ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable);
195 if (!ShdrOrErr)
196 return errorToErrorCode(ShdrOrErr.takeError());
197197 const Elf_Shdr *Shdr = *ShdrOrErr;
198198 if (!Shdr)
199199 return obj2yaml_error::success;
200200
201 ErrorOr NameOrErr = Obj.getSectionName(Shdr);
202 if (std::error_code EC = NameOrErr.getError())
203 return EC;
201 auto NameOrErr = Obj.getSectionName(Shdr);
202 if (!NameOrErr)
203 return errorToErrorCode(NameOrErr.takeError());
204204 S.Section = NameOrErr.get();
205205
206206 return obj2yaml_error::success;
216216 R.Addend = 0;
217217
218218 auto SymOrErr = Obj.getRelocationSymbol(Rel, SymTab);
219 if (std::error_code EC = SymOrErr.getError())
220 return EC;
219 if (!SymOrErr)
220 return errorToErrorCode(SymOrErr.takeError());
221221 const Elf_Sym *Sym = *SymOrErr;
222 ErrorOr StrTabSec = Obj.getSection(SymTab->sh_link);
223 if (std::error_code EC = StrTabSec.getError())
224 return EC;
225 ErrorOr StrTabOrErr = Obj.getStringTable(*StrTabSec);
226 if (std::error_code EC = StrTabOrErr.getError())
227 return EC;
222 auto StrTabSec = Obj.getSection(SymTab->sh_link);
223 if (!StrTabSec)
224 return errorToErrorCode(StrTabSec.takeError());
225 auto StrTabOrErr = Obj.getStringTable(*StrTabSec);
226 if (!StrTabOrErr)
227 return errorToErrorCode(StrTabOrErr.takeError());
228228 StringRef StrTab = *StrTabOrErr;
229229
230230 Expected NameOrErr = Sym->getName(StrTab);
243243 S.Address = Shdr->sh_addr;
244244 S.AddressAlign = Shdr->sh_addralign;
245245
246 ErrorOr NameOrErr = Obj.getSectionName(Shdr);
247 if (std::error_code EC = NameOrErr.getError())
248 return EC;
246 auto NameOrErr = Obj.getSectionName(Shdr);
247 if (!NameOrErr)
248 return errorToErrorCode(NameOrErr.takeError());
249249 S.Name = NameOrErr.get();
250250
251251 if (Shdr->sh_link != ELF::SHN_UNDEF) {
252 ErrorOr LinkSection = Obj.getSection(Shdr->sh_link);
253 if (std::error_code EC = LinkSection.getError())
254 return EC;
252 auto LinkSection = Obj.getSection(Shdr->sh_link);
253 if (LinkSection.takeError())
254 return errorToErrorCode(LinkSection.takeError());
255255 NameOrErr = Obj.getSectionName(*LinkSection);
256 if (std::error_code EC = NameOrErr.getError())
257 return EC;
256 if (!NameOrErr)
257 return errorToErrorCode(NameOrErr.takeError());
258258 S.Link = NameOrErr.get();
259259 }
260260
268268 if (std::error_code EC = dumpCommonSection(Shdr, S))
269269 return EC;
270270
271 ErrorOr InfoSection = Obj.getSection(Shdr->sh_info);
272 if (std::error_code EC = InfoSection.getError())
273 return EC;
274
275 ErrorOr NameOrErr = Obj.getSectionName(*InfoSection);
276 if (std::error_code EC = NameOrErr.getError())
277 return EC;
271 auto InfoSection = Obj.getSection(Shdr->sh_info);
272 if (!InfoSection)
273 return errorToErrorCode(InfoSection.takeError());
274
275 auto NameOrErr = Obj.getSectionName(*InfoSection);
276 if (!NameOrErr)
277 return errorToErrorCode(NameOrErr.takeError());
278278 S.Info = NameOrErr.get();
279279
280280 return obj2yaml_error::success;
289289 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
290290 return EC;
291291
292 ErrorOr SymTabOrErr = Obj.getSection(Shdr->sh_link);
293 if (std::error_code EC = SymTabOrErr.getError())
294 return EC;
292 auto SymTabOrErr = Obj.getSection(Shdr->sh_link);
293 if (!SymTabOrErr)
294 return errorToErrorCode(SymTabOrErr.takeError());
295295 const Elf_Shdr *SymTab = *SymTabOrErr;
296296
297297 auto Rels = Obj.rels(Shdr);
298 if (std::error_code EC = Rels.getError())
299 return EC;
298 if (!Rels)
299 return errorToErrorCode(Rels.takeError());
300300 for (const Elf_Rel &Rel : *Rels) {
301301 ELFYAML::Relocation R;
302302 if (std::error_code EC = dumpRelocation(&Rel, SymTab, R))
316316 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
317317 return EC;
318318
319 ErrorOr SymTabOrErr = Obj.getSection(Shdr->sh_link);
320 if (std::error_code EC = SymTabOrErr.getError())
321 return EC;
319 auto SymTabOrErr = Obj.getSection(Shdr->sh_link);
320 if (!SymTabOrErr)
321 return errorToErrorCode(SymTabOrErr.takeError());
322322 const Elf_Shdr *SymTab = *SymTabOrErr;
323323
324324 auto Rels = Obj.relas(Shdr);
325 if (std::error_code EC = Rels.getError())
326 return EC;
325 if (!Rels)
326 return errorToErrorCode(Rels.takeError());
327327 for (const Elf_Rela &Rel : *Rels) {
328328 ELFYAML::Relocation R;
329329 if (std::error_code EC = dumpRelocation(&Rel, SymTab, R))
343343 if (std::error_code EC = dumpCommonSection(Shdr, *S))
344344 return EC;
345345
346 ErrorOr> ContentOrErr = Obj.getSectionContents(Shdr);
347 if (std::error_code EC = ContentOrErr.getError())
348 return EC;
346 auto ContentOrErr = Obj.getSectionContents(Shdr);
347 if (!ContentOrErr)
348 return errorToErrorCode(ContentOrErr.takeError());
349349 S->Content = yaml::BinaryRef(ContentOrErr.get());
350350 S->Size = S->Content.binary_size();
351351
371371 if (std::error_code EC = dumpCommonSection(Shdr, *S))
372372 return EC;
373373 // Get sh_info which is the signature.
374 ErrorOr SymtabOrErr = Obj.getSection(Shdr->sh_link);
375 if (std::error_code EC = SymtabOrErr.getError())
376 return EC;
374 auto SymtabOrErr = Obj.getSection(Shdr->sh_link);
375 if (!SymtabOrErr)
376 return errorToErrorCode(SymtabOrErr.takeError());
377377 const Elf_Shdr *Symtab = *SymtabOrErr;
378 ErrorOr SymOrErr = Obj.getSymbol(Symtab, Shdr->sh_info);
379 if (std::error_code EC = SymOrErr.getError())
380 return EC;
378 auto SymOrErr = Obj.getSymbol(Symtab, Shdr->sh_info);
379 if (!SymOrErr)
380 return errorToErrorCode(SymOrErr.takeError());
381381 const Elf_Sym *symbol = *SymOrErr;
382 ErrorOr StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
383 if (std::error_code EC = StrTabOrErr.getError())
384 return EC;
382 auto StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
383 if (!StrTabOrErr)
384 return errorToErrorCode(StrTabOrErr.takeError());
385385 StringRef StrTab = *StrTabOrErr;
386386 auto sectionContents = Obj.getSectionContents(Shdr);
387 if (std::error_code ec = sectionContents.getError())
388 return ec;
387 if (!sectionContents)
388 return errorToErrorCode(sectionContents.takeError());
389389 Expected symbolName = symbol->getName(StrTab);
390390 if (!symbolName)
391391 return errorToErrorCode(symbolName.takeError());
398398 if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
399399 s.sectionNameOrType = "GRP_COMDAT";
400400 } else {
401 ErrorOr sHdr = Obj.getSection(groupMembers[i]);
402 if (std::error_code EC = sHdr.getError())
403 return EC;
404 ErrorOr sectionName = Obj.getSectionName(*sHdr);
405 if (std::error_code ec = sectionName.getError())
406 return ec;
401 auto sHdr = Obj.getSection(groupMembers[i]);
402 if (!sHdr)
403 return errorToErrorCode(sHdr.takeError());
404 auto sectionName = Obj.getSectionName(*sHdr);
405 if (!sectionName)
406 return errorToErrorCode(sectionName.takeError());
407407 s.sectionNameOrType = *sectionName;
408408 }
409409 S->Members.push_back(s);
420420 if (std::error_code EC = dumpCommonSection(Shdr, *S))
421421 return EC;
422422
423 ErrorOr> ContentOrErr = Obj.getSectionContents(Shdr);
424 if (std::error_code EC = ContentOrErr.getError())
425 return EC;
423 auto ContentOrErr = Obj.getSectionContents(Shdr);
424 if (!ContentOrErr)
425 return errorToErrorCode(ContentOrErr.takeError());
426426
427427 auto *Flags = reinterpret_cast *>(
428428 ContentOrErr.get().data());