X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FExecutionEngine%2FRuntimeDyld%2FRuntimeDyldMachO.h;h=c8642826d4e7b8c9bce4e80c7f44f89bddc82e19;hb=be406de2251080a4a4c12c5bdec2eb1b92d44347;hp=b5fa0df006dfae19c21b37a789adda902c8b0c1c;hpb=76463fdeb603e1d89b05f094bfd6fe73b90d0b61;p=oota-llvm.git diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h index b5fa0df006d..c8642826d4e 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h @@ -14,177 +14,140 @@ #ifndef LLVM_RUNTIME_DYLD_MACHO_H #define LLVM_RUNTIME_DYLD_MACHO_H -#include "llvm/ADT/IndexedMap.h" -#include "llvm/Object/MachOObject.h" -#include "llvm/Support/Format.h" +#include "ObjectImageCommon.h" #include "RuntimeDyldImpl.h" +#include "llvm/Object/MachO.h" +#include "llvm/Support/Format.h" + +#define DEBUG_TYPE "dyld" using namespace llvm; using namespace llvm::object; - namespace llvm { class RuntimeDyldMachO : public RuntimeDyldImpl { - - // For each symbol, keep a list of relocations based on it. Anytime - // its address is reassigned (the JIT re-compiled the function, e.g.), - // the relocations get re-resolved. - // The symbol (or section) the relocation is sourced from is the Key - // in the relocation list where it's stored. - struct RelocationEntry { - unsigned SectionID; // Section the relocation is contained in. - uint64_t Offset; // Offset into the section for the relocation. - uint32_t Data; // Second word of the raw macho relocation entry. - int64_t Addend; // Addend encoded in the instruction itself, if any, - // plus the offset into the source section for - // the symbol once the relocation is resolvable. - - RelocationEntry(unsigned id, uint64_t offset, uint32_t data, int64_t addend) - : SectionID(id), Offset(offset), Data(data), Addend(addend) {} +protected: + struct SectionOffsetPair { + unsigned SectionID; + uint64_t Offset; }; - typedef SmallVector RelocationList; - // Relocations to sections already loaded. Indexed by SectionID which is the - // source of the address. The target where the address will be writen is - // SectionID/Offset in the relocation itself. - IndexedMap Relocations; - // Relocations to symbols that are not yet resolved. Must be external - // relocations by definition. Indexed by symbol name. - StringMap UnresolvedRelocations; - - bool resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel, - unsigned Type, unsigned Size, int64_t Addend); - bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel, - unsigned Type, unsigned Size, int64_t Addend); - bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel, - unsigned Type, unsigned Size, int64_t Addend); - - bool loadSegment32(const MachOObject *Obj, - const MachOObject::LoadCommandInfo *SegmentLCI, - const InMemoryStruct &SymtabLC); - bool loadSegment64(const MachOObject *Obj, - const MachOObject::LoadCommandInfo *SegmentLCI, - const InMemoryStruct &SymtabLC); - bool processSymbols32(const MachOObject *Obj, - SmallVectorImpl &SectionMap, - SmallVectorImpl &SymbolNames, - const InMemoryStruct &SymtabLC); - bool processSymbols64(const MachOObject *Obj, - SmallVectorImpl &SectionMap, - SmallVectorImpl &SymbolNames, - const InMemoryStruct &SymtabLC); - - void resolveSymbol(StringRef Name); -public: - RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} + struct EHFrameRelatedSections { + EHFrameRelatedSections() + : EHFrameSID(RTDYLD_INVALID_SECTION_ID), + TextSID(RTDYLD_INVALID_SECTION_ID), + ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {} + EHFrameRelatedSections(SID EH, SID T, SID Ex) + : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {} + SID EHFrameSID; + SID TextSID; + SID ExceptTabSID; + }; - bool loadObject(MemoryBuffer *InputBuffer); + // When a module is loaded we save the SectionID of the EH frame section + // in a table until we receive a request to register all unregistered + // EH frame sections with the memory manager. + SmallVector UnregisteredEHFrameSections; - void reassignSectionAddress(unsigned SectionID, uint64_t Addr); + RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} - static bool isKnownFormat(const MemoryBuffer *InputBuffer); + /// Parse the given relocation, which must be a non-scattered, and + /// return a RelocationEntry representing the information. The 'Addend' field + /// will contain the unmodified instruction immediate. + RelocationEntry getBasicRelocationEntry(unsigned SectionID, ObjectImage &Obj, + const relocation_iterator &RI) const; + + /// Construct a RelocationValueRef representing the relocation target. + /// For Symbols in known sections, this will return a RelocationValueRef + /// representing a (SectionID, Offset) pair. + /// For Symbols whose section is not known, this will return a + /// (SymbolName, Offset) pair, where the Offset is taken from the instruction + /// immediate (held in RE.Addend). + /// In both cases the Addend field is *NOT* fixed up to be PC-relative. That + /// should be done by the caller where appropriate by calling makePCRel on + /// the RelocationValueRef. + RelocationValueRef getRelocationValueRef(ObjectImage &ObjImg, + const relocation_iterator &RI, + const RelocationEntry &RE, + ObjSectionToIDMap &ObjSectionToID, + const SymbolTableMap &Symbols); + + /// Make the RelocationValueRef addend PC-relative. + void makeValueAddendPCRel(RelocationValueRef &Value, ObjectImage &ObjImg, + const relocation_iterator &RI); + + /// Dump information about the relocation entry (RE) and resolved value. + void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const; - bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const { - return isKnownFormat(InputBuffer); +public: + /// Create an ObjectImage from the given ObjectBuffer. + static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) { + return new ObjectImageCommon(InputBuffer); } -}; - -} // end namespace llvm - -#endif - -//===-- RuntimeDyldMachO.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-=// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// MachO support for MC-JIT runtime dynamic linker. -// -//===----------------------------------------------------------------------===// -#ifndef LLVM_RUNTIME_DYLD_MACHO_H -#define LLVM_RUNTIME_DYLD_MACHO_H + /// Create an ObjectImage from the given ObjectFile. + static ObjectImage * + createObjectImageFromFile(std::unique_ptr InputObject) { + return new ObjectImageCommon(std::move(InputObject)); + } -#include "llvm/ADT/IndexedMap.h" -#include "llvm/Object/MachOObject.h" -#include "llvm/Support/Format.h" -#include "RuntimeDyldImpl.h" + /// Create a RuntimeDyldMachO instance for the given target architecture. + static std::unique_ptr create(Triple::ArchType Arch, + RTDyldMemoryManager *mm); -using namespace llvm; -using namespace llvm::object; + /// Write the least significant 'Size' bytes in 'Value' out at the address + /// pointed to by Addr. Check for overflow. + bool writeBytesUnaligned(uint8_t *Addr, uint64_t Value, unsigned Size); + SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; } -namespace llvm { -class RuntimeDyldMachO : public RuntimeDyldImpl { + bool isCompatibleFormat(const ObjectBuffer *Buffer) const override; + bool isCompatibleFile(const object::ObjectFile *Obj) const override; + void registerEHFrames() override; +}; - // For each symbol, keep a list of relocations based on it. Anytime - // its address is reassigned (the JIT re-compiled the function, e.g.), - // the relocations get re-resolved. - // The symbol (or section) the relocation is sourced from is the Key - // in the relocation list where it's stored. - struct RelocationEntry { - unsigned SectionID; // Section the relocation is contained in. - uint64_t Offset; // Offset into the section for the relocation. - uint32_t Data; // Second word of the raw macho relocation entry. - int64_t Addend; // Addend encoded in the instruction itself, if any, - // plus the offset into the source section for - // the symbol once the relocation is resolvable. - - RelocationEntry(unsigned id, uint64_t offset, uint32_t data, int64_t addend) - : SectionID(id), Offset(offset), Data(data), Addend(addend) {} - }; - typedef SmallVector RelocationList; - // Relocations to sections already loaded. Indexed by SectionID which is the - // source of the address. The target where the address will be writen is - // SectionID/Offset in the relocation itself. - IndexedMap Relocations; - // Relocations to symbols that are not yet resolved. Must be external - // relocations by definition. Indexed by symbol name. - StringMap UnresolvedRelocations; - - bool resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel, - unsigned Type, unsigned Size, int64_t Addend); - bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel, - unsigned Type, unsigned Size, int64_t Addend); - bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel, - unsigned Type, unsigned Size, int64_t Addend); - - bool loadSegment32(const MachOObject *Obj, - const MachOObject::LoadCommandInfo *SegmentLCI, - const InMemoryStruct &SymtabLC); - bool loadSegment64(const MachOObject *Obj, - const MachOObject::LoadCommandInfo *SegmentLCI, - const InMemoryStruct &SymtabLC); - bool processSymbols32(const MachOObject *Obj, - SmallVectorImpl &SectionMap, - SmallVectorImpl &SymbolNames, - const InMemoryStruct &SymtabLC); - bool processSymbols64(const MachOObject *Obj, - SmallVectorImpl &SectionMap, - SmallVectorImpl &SymbolNames, - const InMemoryStruct &SymtabLC); - - void resolveSymbol(StringRef Name); +/// RuntimeDyldMachOTarget - Templated base class for generic MachO linker +/// algorithms and data structures. +/// +/// Concrete, target specific sub-classes can be accessed via the impl() +/// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously +/// Recurring Template Idiom). Concrete subclasses for each target +/// can be found in ./Targets. +template +class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO { +private: + Impl &impl() { return static_cast(*this); } + const Impl &impl() const { return static_cast(*this); } public: - RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} - - bool loadObject(MemoryBuffer *InputBuffer); - - void reassignSectionAddress(unsigned SectionID, uint64_t Addr); - - static bool isKnownFormat(const MemoryBuffer *InputBuffer); - - bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const { - return isKnownFormat(InputBuffer); + RuntimeDyldMachOCRTPBase(RTDyldMemoryManager *mm) : RuntimeDyldMachO(mm) {} + + void finalizeLoad(ObjectImage &ObjImg, ObjSectionToIDMap &SectionMap) { + unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID; + unsigned TextSID = RTDYLD_INVALID_SECTION_ID; + unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID; + ObjSectionToIDMap::iterator i, e; + + for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) { + const SectionRef &Section = i->first; + StringRef Name; + Section.getName(Name); + if (Name == "__eh_frame") + EHFrameSID = i->second; + else if (Name == "__text") + TextSID = i->second; + else if (Name == "__gcc_except_tab") + ExceptTabSID = i->second; + else + impl().finalizeSection(ObjImg, i->second, Section); + } + UnregisteredEHFrameSections.push_back( + EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID)); } }; } // end namespace llvm -#endif +#undef DEBUG_TYPE +#endif