llvm.org GIT mirror llvm / df3edb6
Return ErrorOr from getSection. This also improves the logic of what is an error: * getSection(uint_32): only return an error if the index is out of bounds. The index 0 corresponds to a perfectly valid entry. * getSection(Elf_Sym): Returns null for symbols that normally don't have sections and error for out of bound indexes. In many places this just moves the report_fatal_error up the stack, but those can then be fixed in smaller patches. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241156 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
9 changed file(s) with 144 addition(s) and 75 deletion(s). Raw diff Collapse all Expand all
356356 uintX_t getStringTableIndex() const;
357357 ELF::Elf64_Word getExtendedSymbolTableIndex(const Elf_Sym *symb) const;
358358 const Elf_Ehdr *getHeader() const { return Header; }
359 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
360 const Elf_Shdr *getSection(uint32_t Index) const;
359 ErrorOr getSection(const Elf_Sym *symb) const;
360 ErrorOr getSection(uint32_t Index) const;
361361 const Elf_Sym *getSymbol(uint32_t index) const;
362362
363363 ErrorOr getStaticSymbolName(const Elf_Sym *Symb) const;
464464 }
465465
466466 template
467 const typename ELFFile::Elf_Shdr *
467 ErrorOr::Elf_Shdr *>
468468 ELFFile::getSection(const Elf_Sym *symb) const {
469 if (symb->st_shndx == ELF::SHN_XINDEX)
469 uint32_t Index = symb->st_shndx;
470 if (Index == ELF::SHN_XINDEX)
470471 return getSection(ExtendedSymbolTable.lookup(symb));
471 if (symb->st_shndx >= ELF::SHN_LORESERVE)
472 if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
472473 return nullptr;
473474 return getSection(symb->st_shndx);
474475 }
531532 ELFFile::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const {
532533 if (!Sec->sh_link)
533534 return std::make_pair(nullptr, nullptr);
534 const Elf_Shdr *SymTable = getSection(Sec->sh_link);
535 ErrorOr SymTableOrErr = getSection(Sec->sh_link);
536 if (std::error_code EC = SymTableOrErr.getError())
537 report_fatal_error(EC.message());
538 const Elf_Shdr *SymTable = *SymTableOrErr;
535539 return std::make_pair(
536540 SymTable, getEntry(SymTable, Rel->getSymbol(isMips64EL())));
537541 }
614618 return;
615619 }
616620 dot_symtab_sec = &Sec;
617 ErrorOr<StringRef> SymtabOrErr = getStringTable(getSection(Sec.sh_link));
621 ErrorOr<const Elf_Shdr *> SectionOrErr = getSection(Sec.sh_link);
622 if ((EC = SectionOrErr.getError()))
623 return;
624 ErrorOr SymtabOrErr = getStringTable(*SectionOrErr);
618625 if ((EC = SymtabOrErr.getError()))
619626 return;
620627 DotStrtab = *SymtabOrErr;
628635 DynSymRegion.Addr = base() + Sec.sh_offset;
629636 DynSymRegion.Size = Sec.sh_size;
630637 DynSymRegion.EntSize = Sec.sh_entsize;
631 const Elf_Shdr *DynStr = getSection(Sec.sh_link);
638 ErrorOr DynStrOrErr = getSection(Sec.sh_link);
639 if ((EC = DynStrOrErr.getError()))
640 return;
641 const Elf_Shdr *DynStr = *DynStrOrErr;
632642 DynStrRegion.Addr = base() + DynStr->sh_offset;
633643 DynStrRegion.Size = DynStr->sh_size;
634644 DynStrRegion.EntSize = DynStr->sh_entsize;
672682 }
673683
674684 // Get string table sections.
675 ErrorOr SymtabOrErr =
676 getStringTable(getSection(getStringTableIndex()));
685 ErrorOr StrTabSecOrErr = getSection(getStringTableIndex());
686 if ((EC = StrTabSecOrErr.getError()))
687 return;
688
689 ErrorOr SymtabOrErr = getStringTable(*StrTabSecOrErr);
677690 if ((EC = SymtabOrErr.getError()))
678691 return;
679692 DotShstrtab = *SymtabOrErr;
824837 template
825838 template
826839 const T *ELFFile::getEntry(uint32_t Section, uint32_t Entry) const {
827 return getEntry(getSection(Section), Entry);
840 ErrorOr Sec = getSection(Section);
841 if (std::error_code EC = Sec.getError())
842 report_fatal_error(EC.message());
843 return getEntry(*Sec, Entry);
828844 }
829845
830846 template
836852 }
837853
838854 template
839 const typename ELFFile::Elf_Shdr *
840 ELFFile::getSection(uint32_t index) const {
841 if (index == 0)
842 return nullptr;
843 if (!SectionHeaderTable || index >= getNumSections())
844 // FIXME: Proper error handling.
845 report_fatal_error("Invalid section index!");
855 ErrorOr::Elf_Shdr *>
856 ELFFile::getSection(uint32_t Index) const {
857 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
858 if (Index >= getNumSections())
859 return object_error::invalid_section_index;
846860
847861 return reinterpret_cast(
848 reinterpret_cast(SectionHeaderTable)
849 + (index * Header->e_shentsize));
862 reinterpret_cast(SectionHeaderTable) +
863 (Index * Header->e_shentsize));
850864 }
851865
852866 template
240240
241241 /// \brief Get the relocation section that contains \a Rel.
242242 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
243 return EF.getSection(Rel.d.a);
243 ErrorOr Sec = EF.getSection(Rel.d.a);
244 if (std::error_code EC = Sec.getError())
245 report_fatal_error(EC.message());
246 return *Sec;
244247 }
245248
246249 const Elf_Rel *getRel(DataRefImpl Rel) const;
405408 const Elf_Ehdr *Header = EF.getHeader();
406409
407410 if (Header->e_type == ELF::ET_REL) {
408 const typename ELFFile::Elf_Shdr * Section = EF.getSection(ESym);
409 if (Section != nullptr)
411 ErrorOr SectionOrErr = EF.getSection(ESym);
412 if (std::error_code EC = SectionOrErr.getError())
413 return EC;
414 const Elf_Shdr *Section = *SectionOrErr;
415 if (Section)
410416 Result += Section->sh_addr;
411417 }
412418
510516 template
511517 section_iterator
512518 ELFObjectFile::getSymbolSection(const Elf_Sym *ESym) const {
513 const Elf_Shdr *ESec = EF.getSection(ESym);
519 ErrorOr ESecOrErr = EF.getSection(ESym);
520 if (std::error_code EC = ESecOrErr.getError())
521 report_fatal_error(EC.message());
522
523 const Elf_Shdr *ESec = *ESecOrErr;
514524 if (!ESec)
515525 return section_end();
516 else {
517 DataRefImpl Sec;
518 Sec.p = reinterpret_cast(ESec);
519 return section_iterator(SectionRef(Sec, this));
520 }
526
527 DataRefImpl Sec;
528 Sec.p = reinterpret_cast(ESec);
529 return section_iterator(SectionRef(Sec, this));
521530 }
522531
523532 template
628637 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
629638 return section_end();
630639
631 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
632 return section_iterator(SectionRef(toDRI(R), this));
640 ErrorOr R = EF.getSection(EShdr->sh_info);
641 if (std::error_code EC = R.getError())
642 report_fatal_error(EC.message());
643 return section_iterator(SectionRef(toDRI(*R), this));
633644 }
634645
635646 // Relocations
650661 if (!symbolIdx)
651662 return symbol_end();
652663
653 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
664 ErrorOr SymSecOrErr = EF.getSection(sec->sh_link);
665 if (std::error_code EC = SymSecOrErr.getError())
666 report_fatal_error(EC.message());
667 const Elf_Shdr *SymSec = *SymSecOrErr;
654668
655669 DataRefImpl SymbolData;
656670 switch (SymSec->sh_type) {
675689
676690 if (Header->e_type == ELF::ET_REL) {
677691 const Elf_Shdr *RelocationSec = getRelSection(Rel);
678 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
679 return ROffset + RelocatedSec->sh_addr;
692 ErrorOr RelocatedSec =
693 EF.getSection(RelocationSec->sh_info);
694 if (std::error_code EC = RelocatedSec.getError())
695 return EC;
696 return ROffset + (*RelocatedSec)->sh_addr;
680697 }
681698 return ROffset;
682699 }
2727 parse_failed,
2828 unexpected_eof,
2929 string_table_non_null_end,
30 invalid_section_index,
3031 bitcode_section_not_found,
3132 macho_small_load_command,
3233 macho_load_segment_too_many_sections,
4242 return "The end of the file was unexpectedly encountered";
4343 case object_error::string_table_non_null_end:
4444 return "String table must end with a null terminator";
45 case object_error::invalid_section_index:
46 return "Invalid section index";
4547 case object_error::bitcode_section_not_found:
4648 return "Bitcode section not found in object file";
4749 case object_error::macho_small_load_command:
3939
4040 RUN: not llvm-readobj -t %p/Inputs/invalid-section-index.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-INDEX %s
4141
42 INVALID-SECTION-INDEX: Invalid section index!
42 INVALID-SECTION-INDEX: Invalid section index
4343
4444 RUN: not llvm-readobj -s %p/Inputs/invalid-section-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-SIZE %s
4545 INVALID-SECTION-SIZE: Invalid section header size
318318 typedef typename ELFObjectFile::Elf_Shdr Elf_Shdr;
319319 const ELFFile &EF = *Obj->getELFFile();
320320
321 const Elf_Shdr *sec = EF.getSection(Rel.d.a);
322 const Elf_Shdr *SymTab = EF.getSection(sec->sh_link);
321 ErrorOr SecOrErr = EF.getSection(Rel.d.a);
322 if (std::error_code EC = SecOrErr.getError())
323 return EC;
324 const Elf_Shdr *Sec = *SecOrErr;
325 ErrorOr SymTabOrErr = EF.getSection(Sec->sh_link);
326 if (std::error_code EC = SymTabOrErr.getError())
327 return EC;
328 const Elf_Shdr *SymTab = *SymTabOrErr;
323329 assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
324330 SymTab->sh_type == ELF::SHT_DYNSYM);
325 const Elf_Shdr *StrTabSec = EF.getSection(SymTab->sh_link);
326 ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(StrTabSec);
331 ErrorOr<const Elf_Shdr *> StrTabSec = EF.getSection(SymTab->sh_link);
332 if (std::error_code EC = StrTabSec.getError())
333 return EC;
334 ErrorOr StrTabOrErr = EF.getStringTable(*StrTabSec);
327335 if (std::error_code EC = StrTabOrErr.getError())
328336 return EC;
329337 StringRef StrTab = *StrTabOrErr;
331339 StringRef res;
332340 int64_t addend = 0;
333341 uint16_t symbol_index = 0;
334 switch (sec->sh_type) {
342 switch (Sec->sh_type) {
335343 default:
336344 return object_error::parse_failed;
337345 case ELF::SHT_REL: {
348356 }
349357 }
350358 const Elf_Sym *symb =
351 EF.template getEntry(sec->sh_link, symbol_index);
359 EF.template getEntry(Sec->sh_link, symbol_index);
352360 StringRef Target;
353 const Elf_Shdr *SymSec = EF.getSection(symb);
361 ErrorOr SymSec = EF.getSection(symb);
362 if (std::error_code EC = SymSec.getError())
363 return EC;
354364 if (symb->getType() == ELF::STT_SECTION) {
355 ErrorOr SecName = EF.getSectionName(SymSec);
365 ErrorOr SecName = EF.getSectionName(*SymSec);
356366 if (std::error_code EC = SecName.getError())
357367 return EC;
358368 Target = *SecName;
376376 std::pair Symbol =
377377 ELF->getRelocationSymbol(&Sec, &RelA);
378378
379 return ELF->getSection(Symbol.second);
379 ErrorOr Ret = ELF->getSection(Symbol.second);
380 if (std::error_code EC = Ret.getError())
381 report_fatal_error(EC.message());
382 return *Ret;
380383 }
381384 }
382385 }
161161 else {
162162 if (SectionIndex == SHN_XINDEX)
163163 SectionIndex = Obj.getExtendedSymbolTableIndex(&*Symbol);
164 const typename ELFO::Elf_Shdr *Sec = Obj.getSection(SectionIndex);
165 SectionName = errorOrDefault(Obj.getSectionName(Sec));
164 ErrorOr Sec = Obj.getSection(SectionIndex);
165 if (!error(Sec.getError()))
166 SectionName = errorOrDefault(Obj.getSectionName(*Sec));
166167 }
167168 }
168169
643644 if (opts::SectionSymbols) {
644645 ListScope D(W, "Symbols");
645646 for (const typename ELFO::Elf_Sym &Sym : Obj->symbols()) {
646 if (Obj->getSection(&Sym) == &Sec)
647 ErrorOr SymSec = Obj->getSection(&Sym);
648 if (!SymSec)
649 continue;
650 if (*SymSec == &Sec)
647651 printSymbol(&Sym, false);
648652 }
649653 }
745749 std::pair Sym =
746750 Obj->getRelocationSymbol(Sec, &Rel);
747751 if (Sym.second && Sym.second->getType() == ELF::STT_SECTION) {
748 const Elf_Shdr *Sec = Obj->getSection(Sym.second);
749 ErrorOr SecName = Obj->getSectionName(Sec);
750 if (SecName)
751 TargetName = SecName.get();
752 ErrorOr Sec = Obj->getSection(Sym.second);
753 if (!error(Sec.getError())) {
754 ErrorOr SecName = Obj->getSectionName(*Sec);
755 if (SecName)
756 TargetName = SecName.get();
757 }
752758 } else if (Sym.first) {
753759 const Elf_Shdr *SymTable = Sym.first;
754 const Elf_Shdr *StrTableSec = Obj->getSection(SymTable->sh_link);
755 ErrorOr StrTableOrErr = Obj->getStringTable(StrTableSec);
756 if (!error(StrTableOrErr.getError()))
757 TargetName = errorOrDefault(Sym.second->getName(*StrTableOrErr));
760 ErrorOr StrTableSec = Obj->getSection(SymTable->sh_link);
761 if (!error(StrTableSec.getError())) {
762 ErrorOr StrTableOrErr = Obj->getStringTable(*StrTableSec);
763 if (!error(StrTableOrErr.getError()))
764 TargetName = errorOrDefault(Sym.second->getName(*StrTableOrErr));
765 }
758766 }
759767
760768 if (opts::ExpandRelocs) {
156156 return EC;
157157 S.Name = NameOrErr.get();
158158
159 const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
159 ErrorOr ShdrOrErr = Obj.getSection(&*Sym);
160 if (std::error_code EC = ShdrOrErr.getError())
161 return EC;
162 const Elf_Shdr *Shdr = *ShdrOrErr;
160163 if (!Shdr)
161164 return obj2yaml_error::success;
162165
182185 return obj2yaml_error::success;
183186
184187 const Elf_Shdr *SymTab = NamePair.first;
185 const Elf_Shdr *StrTabSec = Obj.getSection(SymTab->sh_link);
186 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(StrTabSec);
188 ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
189 if (std::error_code EC = StrTabSec.getError())
190 return EC;
191 ErrorOr StrTabOrErr = Obj.getStringTable(*StrTabSec);
187192 if (std::error_code EC = StrTabOrErr.getError())
188193 return EC;
189194 StringRef StrTab = *StrTabOrErr;
210215 S.Name = NameOrErr.get();
211216
212217 if (Shdr->sh_link != ELF::SHN_UNDEF) {
213 if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) {
214 NameOrErr = Obj.getSectionName(LinkSection);
215 if (std::error_code EC = NameOrErr.getError())
216 return EC;
217 S.Link = NameOrErr.get();
218 }
218 ErrorOr LinkSection = Obj.getSection(Shdr->sh_link);
219 if (std::error_code EC = LinkSection.getError())
220 return EC;
221 NameOrErr = Obj.getSectionName(*LinkSection);
222 if (std::error_code EC = NameOrErr.getError())
223 return EC;
224 S.Link = NameOrErr.get();
219225 }
220226
221227 return obj2yaml_error::success;
228234 if (std::error_code EC = dumpCommonSection(Shdr, S))
229235 return EC;
230236
231 if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) {
232 ErrorOr NameOrErr = Obj.getSectionName(InfoSection);
233 if (std::error_code EC = NameOrErr.getError())
234 return EC;
235 S.Info = NameOrErr.get();
236 }
237 ErrorOr InfoSection = Obj.getSection(Shdr->sh_info);
238 if (std::error_code EC = InfoSection.getError())
239 return EC;
240
241 ErrorOr NameOrErr = Obj.getSectionName(*InfoSection);
242 if (std::error_code EC = NameOrErr.getError())
243 return EC;
244 S.Info = NameOrErr.get();
237245
238246 return obj2yaml_error::success;
239247 }
303311 return EC;
304312 // Get sh_info which is the signature.
305313 const Elf_Sym *symbol = Obj.getSymbol(Shdr->sh_info);
306 const Elf_Shdr *symtab = Obj.getSection(Shdr->sh_link);
307 const Elf_Shdr *StrTabSec = Obj.getSection(symtab->sh_link);
308 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(StrTabSec);
314 ErrorOr<const Elf_Shdr *> Symtab = Obj.getSection(Shdr->sh_link);
315 if (std::error_code EC = Symtab.getError())
316 return EC;
317 ErrorOr StrTabSec = Obj.getSection((*Symtab)->sh_link);
318 if (std::error_code EC = StrTabSec.getError())
319 return EC;
320 ErrorOr StrTabOrErr = Obj.getStringTable(*StrTabSec);
309321 if (std::error_code EC = StrTabOrErr.getError())
310322 return EC;
311323 StringRef StrTab = *StrTabOrErr;
324336 if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
325337 s.sectionNameOrType = "GRP_COMDAT";
326338 } else {
327 const Elf_Shdr *sHdr = Obj.getSection(groupMembers[i]);
328 ErrorOr<StringRef> sectionName = Obj.getSectionName(sHdr);
339 ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
340 if (std::error_code EC = sHdr.getError())
341 return EC;
342 ErrorOr sectionName = Obj.getSectionName(*sHdr);
329343 if (std::error_code ec = sectionName.getError())
330344 return ec;
331345 s.sectionNameOrType = *sectionName;