llvm.org GIT mirror llvm / 96a1f00
Simplify the search for which segment has a virtual address. NFC. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242672 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
1 changed file(s) with 12 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
1515
1616 #include "llvm/ADT/ArrayRef.h"
1717 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/IntervalMap.h"
1918 #include "llvm/ADT/PointerIntPair.h"
2019 #include "llvm/ADT/SmallVector.h"
2120 #include "llvm/ADT/StringSwitch.h"
725724 }
726725
727726 template
728 void ELFFile::scanDynamicTable() {
729 // Build load-address to file-offset map.
730 typedef IntervalMap<
731 uintX_t, uintptr_t,
732 IntervalMapImpl::NodeSizer::LeafSize,
733 IntervalMapHalfOpenInfo> LoadMapT;
734 typename LoadMapT::Allocator Alloc;
735 // Allocate the IntervalMap on the heap to work around MSVC bug where the
736 // stack doesn't get realigned despite LoadMap having alignment 8 (PR24113).
737 std::unique_ptr LoadMap(new LoadMapT(Alloc));
738
727 static bool compareAddr(uint64_t VAddr, const Elf_Phdr_Impl *Phdr) {
728 return VAddr < Phdr->p_vaddr;
729 }
730
731 template void ELFFile::scanDynamicTable() {
732 SmallVector LoadSegments;
739733 for (const Elf_Phdr &Phdr : program_headers()) {
740734 if (Phdr.p_type == ELF::PT_DYNAMIC) {
741735 DynamicRegion.Addr = base() + Phdr.p_offset;
745739 }
746740 if (Phdr.p_type != ELF::PT_LOAD || Phdr.p_filesz == 0)
747741 continue;
748 LoadMap->insert(Phdr.p_vaddr, Phdr.p_vaddr + Phdr.p_filesz, Phdr.p_offset);
742 LoadSegments.push_back(&Phdr);
749743 }
750744
751745 auto toMappedAddr = [&](uint64_t VAddr) -> const uint8_t * {
752 auto I = LoadMap->find(VAddr);
753 if (I == LoadMap->end())
746 const Elf_Phdr **I = std::upper_bound(
747 LoadSegments.begin(), LoadSegments.end(), VAddr, compareAddr);
748 if (I == LoadSegments.begin())
754749 return nullptr;
755 return this->base() + I.value() + (VAddr - I.start());
750 --I;
751 return this->base() + (*I)->p_offset + (VAddr - (*I)->p_vaddr);
756752 };
757753
758754 for (Elf_Dyn_Iter DynI = dynamic_table_begin(), DynE = dynamic_table_end();