From: Lang Hames Date: Tue, 28 Jul 2015 17:52:11 +0000 (+0000) Subject: [RuntimeDyld] Make LoadedObjectInfo::getLoadedSectionAddress take a SectionRef X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=ce8287db09f12375b81f0a10404900bb140ca33b;p=oota-llvm.git [RuntimeDyld] Make LoadedObjectInfo::getLoadedSectionAddress take a SectionRef rather than a string section name. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243456 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/DebugInfo/DIContext.h b/include/llvm/DebugInfo/DIContext.h index 871e60c56b1..db9ba3b3642 100644 --- a/include/llvm/DebugInfo/DIContext.h +++ b/include/llvm/DebugInfo/DIContext.h @@ -145,12 +145,12 @@ public: /// Obtain the Load Address of a section by Name. /// - /// Calculate the address of the section identified by the passed in Name. + /// Calculate the address of the given section. /// The section need not be present in the local address space. The addresses - /// need to be consistent with the addresses used to query the DIContext and + /// needs to be consistent with the addresses used to query the DIContext and /// the output of this function should be deterministic, i.e. repeated calls with - /// the same Name should give the same address. - virtual uint64_t getSectionLoadAddress(StringRef Name) const = 0; + /// the same Sec should give the same address. + virtual uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const = 0; /// If conveniently available, return the content of the given Section. /// @@ -162,7 +162,8 @@ public: /// local (unrelocated) object file and applied on the fly. Note that this method /// is used purely for optimzation purposes in the common case of JITting in the /// local address space, so returning false should always be correct. - virtual bool getLoadedSectionContents(StringRef Name, StringRef &Data) const { + virtual bool getLoadedSectionContents(const object::SectionRef &Sec, + StringRef &Data) const { return false; } diff --git a/include/llvm/ExecutionEngine/RuntimeDyld.h b/include/llvm/ExecutionEngine/RuntimeDyld.h index 2b6e98287b1..17c5fbd5649 100644 --- a/include/llvm/ExecutionEngine/RuntimeDyld.h +++ b/include/llvm/ExecutionEngine/RuntimeDyld.h @@ -17,8 +17,10 @@ #include "JITSymbolFlags.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Object/ObjectFile.h" #include "llvm/Support/Memory.h" #include "llvm/DebugInfo/DIContext.h" +#include #include namespace llvm { @@ -59,26 +61,27 @@ public: class LoadedObjectInfo : public llvm::LoadedObjectInfo { friend class RuntimeDyldImpl; public: - LoadedObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx, - unsigned EndIdx) - : RTDyld(RTDyld), BeginIdx(BeginIdx), EndIdx(EndIdx) { } + typedef std::map ObjSectionToIDMap; + + LoadedObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap) + : RTDyld(RTDyld), ObjSecToIDMap(ObjSecToIDMap) { } virtual object::OwningBinary getObjectForDebug(const object::ObjectFile &Obj) const = 0; - uint64_t getSectionLoadAddress(StringRef Name) const; + uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const; protected: virtual void anchor(); RuntimeDyldImpl &RTDyld; - unsigned BeginIdx, EndIdx; + ObjSectionToIDMap ObjSecToIDMap; }; template struct LoadedObjectInfoHelper : LoadedObjectInfo { - LoadedObjectInfoHelper(RuntimeDyldImpl &RTDyld, unsigned BeginIdx, - unsigned EndIdx) - : LoadedObjectInfo(RTDyld, BeginIdx, EndIdx) {} + LoadedObjectInfoHelper(RuntimeDyldImpl &RTDyld, + LoadedObjectInfo::ObjSectionToIDMap ObjSecToIDMap) + : LoadedObjectInfo(RTDyld, std::move(ObjSecToIDMap)) {} std::unique_ptr clone() const override { return llvm::make_unique(static_cast(*this)); } diff --git a/include/llvm/Object/SymbolicFile.h b/include/llvm/Object/SymbolicFile.h index 537997ac631..6b10ceb263a 100644 --- a/include/llvm/Object/SymbolicFile.h +++ b/include/llvm/Object/SymbolicFile.h @@ -15,6 +15,7 @@ #define LLVM_OBJECT_SYMBOLICFILE_H #include "llvm/Object/Binary.h" +#include "llvm/Support/Format.h" namespace llvm { namespace object { @@ -29,6 +30,12 @@ union DataRefImpl { DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); } }; +template +OStream& operator<<(OStream &OS, const DataRefImpl &D) { + OS << "(" << format("0x%x8", D.p) << " (" << format("0x%x8", D.d.a) << ", " << format("0x%x8", D.d.b) << "))"; + return OS; +} + inline bool operator==(const DataRefImpl &a, const DataRefImpl &b) { // Check bitwise identical. This is the only legal way to compare a union w/o // knowing which member is in use. diff --git a/lib/DebugInfo/DWARF/DWARFContext.cpp b/lib/DebugInfo/DWARF/DWARFContext.cpp index 96bcf15e0af..4858f3633d0 100644 --- a/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -556,10 +556,11 @@ DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj, continue; StringRef data; + section_iterator RelocatedSection = Section.getRelocatedSection(); // Try to obtain an already relocated version of this section. // Else use the unrelocated section from the object file. We'll have to // apply relocations ourselves later. - if (!L || !L->getLoadedSectionContents(name,data)) + if (!L || !L->getLoadedSectionContents(*RelocatedSection,data)) Section.getContents(data); name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes. @@ -623,7 +624,6 @@ DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj, TypesDWOSections[Section].Data = data; } - section_iterator RelocatedSection = Section.getRelocatedSection(); if (RelocatedSection == Obj.section_end()) continue; @@ -634,7 +634,7 @@ DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj, // If the section we're relocating was relocated already by the JIT, // then we used the relocated version above, so we do not need to process // relocations for it now. - if (L && L->getLoadedSectionContents(RelSecName,RelSecData)) + if (L && L->getLoadedSectionContents(*RelocatedSection,RelSecData)) continue; RelSecName = RelSecName.substr( @@ -704,7 +704,10 @@ DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj, // we need to perform the same computation. StringRef SecName; RSec->getName(SecName); - SectionLoadAddress = L->getSectionLoadAddress(SecName); +// llvm::dbgs() << "Name: '" << SecName +// << "', RSec: " << RSec->getRawDataRefImpl() +// << ", Section: " << Section.getRawDataRefImpl() << "\n"; + SectionLoadAddress = L->getSectionLoadAddress(*RSec); if (SectionLoadAddress != 0) SymAddr += SectionLoadAddress - RSec->getAddress(); } diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index 93287a3a4e7..24495b21d45 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -122,14 +122,10 @@ static std::error_code getOffset(const SymbolRef &Sym, SectionRef Sec, return std::error_code(); } -std::pair +RuntimeDyldImpl::ObjSectionToIDMap RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { MutexGuard locked(lock); - // Grab the first Section ID. We'll use this later to construct the underlying - // range for the returned LoadedObjectInfo. - unsigned SectionsAddedBeginIdx = Sections.size(); - // Save information about our target Arch = (Triple::ArchType)Obj.getArch(); IsTargetLittleEndian = Obj.isLittleEndian(); @@ -231,9 +227,10 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { // Give the subclasses a chance to tie-up any loose ends. finalizeLoad(Obj, LocalSections); - unsigned SectionsAddedEndIdx = Sections.size(); +// for (auto E : LocalSections) +// llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n"; - return std::make_pair(SectionsAddedBeginIdx, SectionsAddedEndIdx); + return LocalSections; } // A helper method for computeTotalAllocSize. @@ -818,10 +815,19 @@ void RuntimeDyldImpl::resolveExternalSymbols() { // RuntimeDyld class implementation uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress( - StringRef SectionName) const { - for (unsigned I = BeginIdx; I != EndIdx; ++I) - if (RTDyld.Sections[I].Name == SectionName) - return RTDyld.Sections[I].LoadAddress; + const object::SectionRef &Sec) const { + +// llvm::dbgs() << "Searching for " << Sec.getRawDataRefImpl() << " in:\n"; +// for (auto E : ObjSecToIDMap) +// llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n"; + + auto I = ObjSecToIDMap.find(Sec); + if (I != ObjSecToIDMap.end()) { +// llvm::dbgs() << "Found ID " << I->second << " for Sec: " << Sec.getRawDataRefImpl() << ", LoadAddress = " << RTDyld.Sections[I->second].LoadAddress << "\n"; + return RTDyld.Sections[I->second].LoadAddress; + } else { +// llvm::dbgs() << "Not found.\n"; + } return 0; } diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldCOFF.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldCOFF.cpp index 1dacc1393f2..e2979a6140f 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldCOFF.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldCOFF.cpp @@ -27,9 +27,8 @@ namespace { class LoadedCOFFObjectInfo : public RuntimeDyld::LoadedObjectInfoHelper { public: - LoadedCOFFObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx, - unsigned EndIdx) - : LoadedObjectInfoHelper(RTDyld, BeginIdx, EndIdx) {} + LoadedCOFFObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap) + : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {} OwningBinary getObjectForDebug(const ObjectFile &Obj) const override { @@ -55,10 +54,7 @@ llvm::RuntimeDyldCOFF::create(Triple::ArchType Arch, std::unique_ptr RuntimeDyldCOFF::loadObject(const object::ObjectFile &O) { - unsigned SectionStartIdx, SectionEndIdx; - std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O); - return llvm::make_unique(*this, SectionStartIdx, - SectionEndIdx); + return llvm::make_unique(*this, loadObjectImpl(O)); } uint64_t RuntimeDyldCOFF::getSymbolOffset(const SymbolRef &Sym) { diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp index 3787950b3b0..38890aea8b3 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp @@ -107,9 +107,8 @@ void DyldELFObject::updateSymbolAddress(const SymbolRef &SymRef, class LoadedELFObjectInfo : public RuntimeDyld::LoadedObjectInfoHelper { public: - LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx, - unsigned EndIdx) - : LoadedObjectInfoHelper(RTDyld, BeginIdx, EndIdx) {} + LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap) + : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {} OwningBinary getObjectForDebug(const ObjectFile &Obj) const override; @@ -118,6 +117,7 @@ public: template std::unique_ptr> createRTDyldELFObject(MemoryBufferRef Buffer, + const ObjectFile &SourceObject, const LoadedELFObjectInfo &L, std::error_code &ec) { typedef typename ELFFile::Elf_Shdr Elf_Shdr; @@ -127,6 +127,7 @@ createRTDyldELFObject(MemoryBufferRef Buffer, llvm::make_unique>(Buffer, ec); // Iterate over all sections in the object. + auto SI = SourceObject.section_begin(); for (const auto &Sec : Obj->sections()) { StringRef SectionName; Sec.getName(SectionName); @@ -135,12 +136,13 @@ createRTDyldELFObject(MemoryBufferRef Buffer, Elf_Shdr *shdr = const_cast( reinterpret_cast(ShdrRef.p)); - if (uint64_t SecLoadAddr = L.getSectionLoadAddress(SectionName)) { + if (uint64_t SecLoadAddr = L.getSectionLoadAddress(*SI)) { // This assumes that the address passed in matches the target address // bitness. The template-based type cast handles everything else. shdr->sh_addr = static_cast(SecLoadAddr); } } + ++SI; } return Obj; @@ -158,16 +160,20 @@ OwningBinary createELFDebugObject(const ObjectFile &Obj, std::unique_ptr DebugObj; if (Obj.getBytesInAddress() == 4 && Obj.isLittleEndian()) { typedef ELFType ELF32LE; - DebugObj = createRTDyldELFObject(Buffer->getMemBufferRef(), L, ec); + DebugObj = createRTDyldELFObject(Buffer->getMemBufferRef(), Obj, L, + ec); } else if (Obj.getBytesInAddress() == 4 && !Obj.isLittleEndian()) { typedef ELFType ELF32BE; - DebugObj = createRTDyldELFObject(Buffer->getMemBufferRef(), L, ec); + DebugObj = createRTDyldELFObject(Buffer->getMemBufferRef(), Obj, L, + ec); } else if (Obj.getBytesInAddress() == 8 && !Obj.isLittleEndian()) { typedef ELFType ELF64BE; - DebugObj = createRTDyldELFObject(Buffer->getMemBufferRef(), L, ec); + DebugObj = createRTDyldELFObject(Buffer->getMemBufferRef(), Obj, L, + ec); } else if (Obj.getBytesInAddress() == 8 && Obj.isLittleEndian()) { typedef ELFType ELF64LE; - DebugObj = createRTDyldELFObject(Buffer->getMemBufferRef(), L, ec); + DebugObj = createRTDyldELFObject(Buffer->getMemBufferRef(), Obj, L, + ec); } else llvm_unreachable("Unexpected ELF format"); @@ -215,10 +221,7 @@ void RuntimeDyldELF::deregisterEHFrames() { std::unique_ptr RuntimeDyldELF::loadObject(const object::ObjectFile &O) { - unsigned SectionStartIdx, SectionEndIdx; - std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O); - return llvm::make_unique(*this, SectionStartIdx, - SectionEndIdx); + return llvm::make_unique(*this, loadObjectImpl(O)); } void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section, diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h index e085a9296e8..518df12b67e 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h @@ -378,7 +378,7 @@ protected: const SectionRef &Section); // \brief Implementation of the generic part of the loadObject algorithm. - std::pair loadObjectImpl(const object::ObjectFile &Obj); + ObjSectionToIDMap loadObjectImpl(const object::ObjectFile &Obj); public: RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr, diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp index c61b1c2fb27..c9bd11a1e1d 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp @@ -29,9 +29,9 @@ namespace { class LoadedMachOObjectInfo : public RuntimeDyld::LoadedObjectInfoHelper { public: - LoadedMachOObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx, - unsigned EndIdx) - : LoadedObjectInfoHelper(RTDyld, BeginIdx, EndIdx) {} + LoadedMachOObjectInfo(RuntimeDyldImpl &RTDyld, + ObjSectionToIDMap ObjSecToIDMap) + : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {} OwningBinary getObjectForDebug(const ObjectFile &Obj) const override { @@ -334,10 +334,7 @@ RuntimeDyldMachO::create(Triple::ArchType Arch, std::unique_ptr RuntimeDyldMachO::loadObject(const object::ObjectFile &O) { - unsigned SectionStartIdx, SectionEndIdx; - std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O); - return llvm::make_unique(*this, SectionStartIdx, - SectionEndIdx); + return llvm::make_unique(*this, loadObjectImpl(O)); } } // end namespace llvm diff --git a/tools/llvm-rtdyld/llvm-rtdyld.cpp b/tools/llvm-rtdyld/llvm-rtdyld.cpp index 86f66f89b15..51d68bf3040 100644 --- a/tools/llvm-rtdyld/llvm-rtdyld.cpp +++ b/tools/llvm-rtdyld/llvm-rtdyld.cpp @@ -277,6 +277,7 @@ static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) { if (UseDebugObj) { DebugObj = LoadedObjInfo->getObjectForDebug(Obj); SymbolObj = DebugObj.getBinary(); + LoadedObjInfo.release(); } } @@ -308,7 +309,7 @@ static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) { StringRef SecName; Sec->getName(SecName); uint64_t SectionLoadAddress = - LoadedObjInfo->getSectionLoadAddress(SecName); + LoadedObjInfo->getSectionLoadAddress(*Sec); if (SectionLoadAddress != 0) Addr += SectionLoadAddress - Sec->getAddress(); }