llvm.org GIT mirror llvm / 3c4c933
Pass a symbol table to getRelocationSymbol instead of returning one. This removes a report_fatal_error from library and avoids checking a section property for every section entry. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@246656 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
4 changed file(s) with 50 addition(s) and 45 deletion(s). Raw diff Collapse all Expand all
8787
8888 /// \brief Get the symbol table section and symbol for a given relocation.
8989 template
90 std::pair
91 getRelocationSymbol(const Elf_Shdr *RelSec, const RelT *Rel) const;
90 const Elf_Sym *getRelocationSymbol(const RelT *Rel,
91 const Elf_Shdr *SymTab) const;
9292
9393 ELFFile(StringRef Object, std::error_code &EC);
9494
289289
290290 template
291291 template
292 std::pair::Elf_Shdr *,
293 const typename ELFFile::Elf_Sym *>
294 ELFFile::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const {
295 if (!Sec->sh_link)
296 return std::make_pair(nullptr, nullptr);
297 ErrorOr SymTableOrErr = getSection(Sec->sh_link);
298 if (std::error_code EC = SymTableOrErr.getError())
299 report_fatal_error(EC.message());
300 const Elf_Shdr *SymTable = *SymTableOrErr;
301 return std::make_pair(
302 SymTable, getEntry(SymTable, Rel->getSymbol(isMips64EL())));
292 const typename ELFFile::Elf_Sym *
293 ELFFile::getRelocationSymbol(const RelT *Rel,
294 const Elf_Shdr *SymTab) const {
295 uint32_t Index = Rel->getSymbol(isMips64EL());
296 if (Index == 0)
297 return nullptr;
298 return getEntry(SymTab, Index);
303299 }
304300
305301 template
374374 if (Sec.sh_type != ELF::SHT_REL || Sec.sh_info != IndexSectionIndex)
375375 continue;
376376
377 ErrorOr SymTabOrErr = ELF->getSection(Sec.sh_link);
378 error(SymTabOrErr.getError());
379 const Elf_Shdr *SymTab = *SymTabOrErr;
380
377381 for (const Elf_Rel &R : ELF->rels(&Sec)) {
378382 if (R.r_offset != static_cast(IndexTableOffset))
379383 continue;
383387 RelA.r_info = R.r_info;
384388 RelA.r_addend = 0;
385389
386 std::pair Symbol =
387 ELF->getRelocationSymbol(&Sec, &RelA);
390 const Elf_Sym *Symbol = ELF->getRelocationSymbol(&RelA, SymTab);
388391
389392 ErrorOr Ret =
390 ELF->getSection(Symbol.second, Symbol.first, ShndxTable);
393 ELF->getSection(Symbol, SymTab, ShndxTable);
391394 if (std::error_code EC = Ret.getError())
392395 report_fatal_error(EC.message());
393396 return *Ret;
9999 StringRef StrTable, bool IsDynamic);
100100
101101 void printRelocations(const Elf_Shdr *Sec);
102 void printRelocation(const Elf_Shdr *Sec, Elf_Rela Rel);
102 void printRelocation(Elf_Rela Rel, const Elf_Shdr *SymTab);
103103 void printValue(uint64_t Type, uint64_t Value);
104104
105105 const Elf_Rela *dyn_rela_begin() const;
10941094
10951095 template
10961096 void ELFDumper::printRelocations(const Elf_Shdr *Sec) {
1097 ErrorOr SymTabOrErr = Obj->getSection(Sec->sh_link);
1098 error(SymTabOrErr.getError());
1099 const Elf_Shdr *SymTab = *SymTabOrErr;
1100
10971101 switch (Sec->sh_type) {
10981102 case ELF::SHT_REL:
10991103 for (const Elf_Rel &R : Obj->rels(Sec)) {
11011105 Rela.r_offset = R.r_offset;
11021106 Rela.r_info = R.r_info;
11031107 Rela.r_addend = 0;
1104 printRelocation(Sec, Rela);
1108 printRelocation(Rela, SymTab);
11051109 }
11061110 break;
11071111 case ELF::SHT_RELA:
11081112 for (const Elf_Rela &R : Obj->relas(Sec))
1109 printRelocation(Sec, R);
1113 printRelocation(R, SymTab);
11101114 break;
11111115 }
11121116 }
11131117
11141118 template
1115 void ELFDumper::printRelocation(const Elf_Shdr *Sec, Elf_Rela Rel) {
1119 void ELFDumper::printRelocation(Elf_Rela Rel, const Elf_Shdr *SymTab) {
11161120 SmallString<32> RelocName;
11171121 Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
11181122 StringRef TargetName;
1119 std::pair Sym =
1120 Obj->getRelocationSymbol(Sec, &Rel);
1121 if (Sym.second && Sym.second->getType() == ELF::STT_SECTION) {
1122 ErrorOr Sec =
1123 Obj->getSection(Sym.second, Sym.first, ShndxTable);
1123 const Elf_Sym *Sym = Obj->getRelocationSymbol(&Rel, SymTab);
1124 if (Sym && Sym->getType() == ELF::STT_SECTION) {
1125 ErrorOr Sec = Obj->getSection(Sym, SymTab, ShndxTable);
11241126 error(Sec.getError());
11251127 ErrorOr SecName = Obj->getSectionName(*Sec);
11261128 if (SecName)
11271129 TargetName = SecName.get();
1128 } else if (Sym.first) {
1129 const Elf_Shdr *SymTable = Sym.first;
1130 ErrorOr StrTableOrErr = Obj->getStringTableForSymtab(*SymTable);
1130 } else if (Sym) {
1131 ErrorOr StrTableOrErr = Obj->getStringTableForSymtab(*SymTab);
11311132 error(StrTableOrErr.getError());
1132 TargetName = errorOrDefault(Sym.second->getName(*StrTableOrErr));
1133 TargetName = errorOrDefault(Sym->getName(*StrTableOrErr));
11331134 }
11341135
11351136 if (opts::ExpandRelocs) {
17661767 ErrorOr SymTableOrErr =
17671768 Obj->getSection(PLTRelShdr->sh_link);
17681769 error(SymTableOrErr.getError());
1769 ErrorOr StrTable = Obj->getStringTableForSymtab(**SymTableOrErr);
1770 const Elf_Shdr *SymTable = *SymTableOrErr;
1771 ErrorOr StrTable = Obj->getStringTableForSymtab(*SymTable);
17701772 error(StrTable.getError());
17711773
17721774 const GOTEntry *PLTBegin = makeGOTIter(*PLT, 0);
17881790 for (const Elf_Rel *RI = Obj->rel_begin(PLTRelShdr),
17891791 *RE = Obj->rel_end(PLTRelShdr);
17901792 RI != RE && It != PLTEnd; ++RI, ++It) {
1791 const Elf_Sym *Sym =
1792 Obj->getRelocationSymbol(&*PLTRelShdr, &*RI).second;
1793 const Elf_Sym *Sym = Obj->getRelocationSymbol(&*RI, SymTable);
17931794 printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, *StrTable, Sym);
17941795 }
17951796 break;
17971798 for (const Elf_Rela *RI = Obj->rela_begin(PLTRelShdr),
17981799 *RE = Obj->rela_end(PLTRelShdr);
17991800 RI != RE && It != PLTEnd; ++RI, ++It) {
1800 const Elf_Sym *Sym =
1801 Obj->getRelocationSymbol(&*PLTRelShdr, &*RI).second;
1801 const Elf_Sym *Sym = Obj->getRelocationSymbol(&*RI, SymTable);
18021802 printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, *StrTable, Sym);
18031803 }
18041804 break;
3333 std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr,
3434 ELFYAML::RelocationSection &S);
3535 template
36 std::error_code dumpRelocation(const Elf_Shdr *Shdr, const RelT *Rel,
36 std::error_code dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
3737 ELFYAML::Relocation &R);
3838
3939 ErrorOr dumpRelSection(const Elf_Shdr *Shdr);
200200
201201 template
202202 template
203 std::error_code ELFDumper::dumpRelocation(const Elf_Shdr *Shdr,
204 const RelT *Rel,
203 std::error_code ELFDumper::dumpRelocation(const RelT *Rel,
204 const Elf_Shdr *SymTab,
205205 ELFYAML::Relocation &R) {
206206 R.Type = Rel->getType(Obj.isMips64EL());
207207 R.Offset = Rel->r_offset;
208208 R.Addend = 0;
209209
210 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
211 if (!NamePair.first)
212 return obj2yaml_error::success;
213
214 const Elf_Shdr *SymTab = NamePair.first;
210 const Elf_Sym *Sym = Obj.getRelocationSymbol(Rel, SymTab);
215211 ErrorOr StrTabSec = Obj.getSection(SymTab->sh_link);
216212 if (std::error_code EC = StrTabSec.getError())
217213 return EC;
220216 return EC;
221217 StringRef StrTab = *StrTabOrErr;
222218
223 ErrorOr NameOrErr = NamePair.second->getName(StrTab);
219 ErrorOr NameOrErr = Sym->getName(StrTab);
224220 if (std::error_code EC = NameOrErr.getError())
225221 return EC;
226222 R.Symbol = NameOrErr.get();
282278 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
283279 return EC;
284280
281 ErrorOr SymTabOrErr = Obj.getSection(Shdr->sh_link);
282 if (std::error_code EC = SymTabOrErr.getError())
283 return EC;
284 const Elf_Shdr *SymTab = *SymTabOrErr;
285
285286 for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) {
286287 ELFYAML::Relocation R;
287 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
288 if (std::error_code EC = dumpRelocation(&*RI, SymTab, R))
288289 return EC;
289290 S->Relocations.push_back(R);
290291 }
301302 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
302303 return EC;
303304
305 ErrorOr SymTabOrErr = Obj.getSection(Shdr->sh_link);
306 if (std::error_code EC = SymTabOrErr.getError())
307 return EC;
308 const Elf_Shdr *SymTab = *SymTabOrErr;
309
304310 for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE;
305311 ++RI) {
306312 ELFYAML::Relocation R;
307 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
313 if (std::error_code EC = dumpRelocation(&*RI, SymTab, R))
308314 return EC;
309315 R.Addend = RI->r_addend;
310316 S->Relocations.push_back(R);