llvm.org GIT mirror llvm / 1c12cc0
Remove Elf_Rela_Iter and Elf_Rel_Iter. Use just the pointers and check for invalid relocation sections. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242700 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
5 changed file(s) with 83 addition(s) and 70 deletion(s). Raw diff Collapse all Expand all
141141 typedef Elf_Hash_Impl Elf_Hash;
142142 typedef ELFEntityIterator Elf_Dyn_Iter;
143143 typedef iterator_range Elf_Dyn_Range;
144 typedef ELFEntityIterator Elf_Rela_Iter;
145 typedef ELFEntityIterator Elf_Rel_Iter;
146144 typedef iterator_range Elf_Shdr_Range;
147145
148146 /// \brief Archive files are 2 byte aligned, so we need this for
310308 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
311309 }
312310
313 Elf_Rela_Iter dyn_rela_begin() const {
314 if (DynRelaRegion.Addr)
315 return Elf_Rela_Iter(DynRelaRegion.EntSize,
316 (const char *)DynRelaRegion.Addr);
317 return Elf_Rela_Iter(0, nullptr);
318 }
319
320 Elf_Rela_Iter dyn_rela_end() const {
321 if (DynRelaRegion.Addr)
322 return Elf_Rela_Iter(
323 DynRelaRegion.EntSize,
324 (const char *)DynRelaRegion.Addr + DynRelaRegion.Size);
325 return Elf_Rela_Iter(0, nullptr);
326 }
327
328 Elf_Rela_Iter rela_begin(const Elf_Shdr *sec) const {
329 return Elf_Rela_Iter(sec->sh_entsize,
330 (const char *)(base() + sec->sh_offset));
331 }
332
333 Elf_Rela_Iter rela_end(const Elf_Shdr *sec) const {
334 return Elf_Rela_Iter(
335 sec->sh_entsize,
336 (const char *)(base() + sec->sh_offset + sec->sh_size));
337 }
338
339 Elf_Rel_Iter rel_begin(const Elf_Shdr *sec) const {
340 return Elf_Rel_Iter(sec->sh_entsize,
341 (const char *)(base() + sec->sh_offset));
342 }
343
344 Elf_Rel_Iter rel_end(const Elf_Shdr *sec) const {
345 return Elf_Rel_Iter(sec->sh_entsize,
346 (const char *)(base() + sec->sh_offset + sec->sh_size));
311 const Elf_Rela *dyn_rela_begin() const {
312 if (DynRelaRegion.Size && DynRelaRegion.EntSize != sizeof(Elf_Rela))
313 report_fatal_error("Invalid relocation entry size");
314 return reinterpret_cast(DynRelaRegion.Addr);
315 }
316
317 const Elf_Rela *dyn_rela_end() const {
318 uint64_t Size = DynRelaRegion.Size;
319 if (Size % sizeof(Elf_Rela))
320 report_fatal_error("Invalid relocation table size");
321 return dyn_rela_begin() + Size / sizeof(Elf_Rela);
322 }
323
324 typedef iterator_range Elf_Rela_Range;
325
326 Elf_Rela_Range dyn_relas() const {
327 return make_range(dyn_rela_begin(), dyn_rela_end());
328 }
329
330 const Elf_Rela *rela_begin(const Elf_Shdr *sec) const {
331 if (sec->sh_entsize != sizeof(Elf_Rela))
332 report_fatal_error("Invalid relocation entry size");
333 return reinterpret_cast(base() + sec->sh_offset);
334 }
335
336 const Elf_Rela *rela_end(const Elf_Shdr *sec) const {
337 uint64_t Size = sec->sh_size;
338 if (Size % sizeof(Elf_Rela))
339 report_fatal_error("Invalid relocation table size");
340 return rela_begin(sec) + Size / sizeof(Elf_Rela);
341 }
342
343 Elf_Rela_Range relas(const Elf_Shdr *Sec) const {
344 return make_range(rela_begin(Sec), rela_end(Sec));
345 }
346
347 const Elf_Rel *rel_begin(const Elf_Shdr *sec) const {
348 if (sec->sh_entsize != sizeof(Elf_Rel))
349 report_fatal_error("Invalid relocation entry size");
350 return reinterpret_cast(base() + sec->sh_offset);
351 }
352
353 const Elf_Rel *rel_end(const Elf_Shdr *sec) const {
354 uint64_t Size = sec->sh_size;
355 if (Size % sizeof(Elf_Rel))
356 report_fatal_error("Invalid relocation table size");
357 return rel_begin(sec) + Size / sizeof(Elf_Rel);
358 }
359
360 typedef iterator_range Elf_Rel_Range;
361 Elf_Rel_Range rels(const Elf_Shdr *Sec) const {
362 return make_range(rel_begin(Sec), rel_end(Sec));
347363 }
348364
349365 /// \brief Iterate over program header table.
4242 RUN: FileCheck --check-prefix=VIRTADDR %s
4343
4444 VIRTADDR: Virtual address is not in any segment
45
46
47 RUN: not llvm-readobj -dyn-relocations \
48 RUN: %p/Inputs/corrupt-invalid-relocation-size.elf.x86-64 2>&1 | \
49 RUN: FileCheck --check-prefix=RELOC %s
50
51 RELOC: Invalid relocation entry size
309309
310310 typedef typename object::ELFFile::Elf_Sym Elf_Sym;
311311 typedef typename object::ELFFile::Elf_Shdr Elf_Shdr;
312
313 typedef typename object::ELFFile::Elf_Rel_Iter Elf_Rel_iterator;
312 typedef typename object::ELFFile::Elf_Rel Elf_Rel;
314313
315314 static const size_t IndexTableEntrySize;
316315
365364
366365 for (const Elf_Shdr &Sec : ELF->sections()) {
367366 if (Sec.sh_type == ELF::SHT_REL && Sec.sh_info == IndexSectionIndex) {
368 for (Elf_Rel_iterator RI = ELF->rel_begin(&Sec), RE = ELF->rel_end(&Sec);
369 RI != RE; ++RI) {
370 if (RI->r_offset == static_cast(IndexTableOffset)) {
367 for (const Elf_Rel &R : ELF->rels(&Sec)) {
368 if (R.r_offset == static_cast(IndexTableOffset)) {
371369 typename object::ELFFile::Elf_Rela RelA;
372 RelA.r_offset = RI->r_offset;
373 RelA.r_info = RI->r_info;
370 RelA.r_offset = R.r_offset;
371 RelA.r_info = R.r_info;
374372 RelA.r_addend = 0;
375373
376374 std::pair Symbol =
688688 void ELFDumper::printDynamicRelocations() {
689689 W.startLine() << "Dynamic Relocations {\n";
690690 W.indent();
691 for (typename ELFO::Elf_Rela_Iter RelI = Obj->dyn_rela_begin(),
692 RelE = Obj->dyn_rela_end();
693 RelI != RelE; ++RelI) {
691 for (const typename ELFO::Elf_Rela &Rel : Obj->dyn_relas()) {
694692 SmallString<32> RelocName;
695 Obj->getRelocationTypeName(RelI->getType(Obj->isMips64EL()), RelocName);
693 Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
696694 StringRef SymbolName;
697 uint32_t SymIndex = RelI->getSymbol(Obj->isMips64EL());
695 uint32_t SymIndex = Rel.getSymbol(Obj->isMips64EL());
698696 const typename ELFO::Elf_Sym *Sym = Obj->dynamic_symbol_begin() + SymIndex;
699697 SymbolName = errorOrDefault(Obj->getSymbolName(Sym, true));
700698 if (opts::ExpandRelocs) {
701699 DictScope Group(W, "Relocation");
702 W.printHex("Offset", RelI->r_offset);
703 W.printNumber("Type", RelocName, (int)RelI->getType(Obj->isMips64EL()));
700 W.printHex("Offset", Rel.r_offset);
701 W.printNumber("Type", RelocName, (int)Rel.getType(Obj->isMips64EL()));
704702 W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
705 W.printHex("Addend", RelI->r_addend);
703 W.printHex("Addend", Rel.r_addend);
706704 }
707705 else {
708706 raw_ostream& OS = W.startLine();
709 OS << W.hex(RelI->r_offset)
710 << " " << RelocName
711 << " " << (SymbolName.size() > 0 ? SymbolName : "-")
712 << " " << W.hex(RelI->r_addend)
713 << "\n";
707 OS << W.hex(Rel.r_offset) << " " << RelocName << " "
708 << (SymbolName.size() > 0 ? SymbolName : "-") << " "
709 << W.hex(Rel.r_addend) << "\n";
714710 }
715711 }
716712 W.unindent();
721717 void ELFDumper::printRelocations(const Elf_Shdr *Sec) {
722718 switch (Sec->sh_type) {
723719 case ELF::SHT_REL:
724 for (typename ELFO::Elf_Rel_Iter RI = Obj->rel_begin(Sec),
725 RE = Obj->rel_end(Sec);
726 RI != RE; ++RI) {
720 for (const typename ELFO::Elf_Rel &R : Obj->rels(Sec)) {
727721 typename ELFO::Elf_Rela Rela;
728 Rela.r_offset = RI->r_offset;
729 Rela.r_info = RI->r_info;
722 Rela.r_offset = R.r_offset;
723 Rela.r_info = R.r_info;
730724 Rela.r_addend = 0;
731725 printRelocation(Sec, Rela);
732726 }
733727 break;
734728 case ELF::SHT_RELA:
735 for (typename ELFO::Elf_Rela_Iter RI = Obj->rela_begin(Sec),
736 RE = Obj->rela_end(Sec);
737 RI != RE; ++RI) {
738 printRelocation(Sec, *RI);
739 }
729 for (const typename ELFO::Elf_Rela &R : Obj->relas(Sec))
730 printRelocation(Sec, R);
740731 break;
741732 }
742733 }
13701361
13711362 switch (PLTRelShdr->sh_type) {
13721363 case ELF::SHT_REL:
1373 for (typename ObjectFile::Elf_Rel_Iter RI = Obj->rel_begin(PLTRelShdr),
1374 RE = Obj->rel_end(PLTRelShdr);
1364 for (const typename ObjectFile::Elf_Rel *RI = Obj->rel_begin(PLTRelShdr),
1365 *RE = Obj->rel_end(PLTRelShdr);
13751366 RI != RE && It != PLTEnd; ++RI, ++It) {
13761367 const Elf_Sym *Sym =
13771368 Obj->getRelocationSymbol(&*PLTRelShdr, &*RI).second;
13791370 }
13801371 break;
13811372 case ELF::SHT_RELA:
1382 for (typename ObjectFile::Elf_Rela_Iter RI = Obj->rela_begin(PLTRelShdr),
1383 RE = Obj->rela_end(PLTRelShdr);
1373 for (const typename ObjectFile::Elf_Rela
1374 *RI = Obj->rela_begin(PLTRelShdr),
1375 *RE = Obj->rela_end(PLTRelShdr);
13841376 RI != RE && It != PLTEnd; ++RI, ++It) {
13851377 const Elf_Sym *Sym =
13861378 Obj->getRelocationSymbol(&*PLTRelShdr, &*RI).second;