- IndexedMap<RelocationList> Relocations;
- // Relocations to symbols that are not yet resolved. Must be external
- // relocations by definition. Indexed by symbol name.
- StringMap<RelocationList> 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<macho::SymtabLoadCommand> &SymtabLC);
- bool loadSegment64(const MachOObject *Obj,
- const MachOObject::LoadCommandInfo *SegmentLCI,
- const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
- bool processSymbols32(const MachOObject *Obj,
- SmallVectorImpl<unsigned> &SectionMap,
- SmallVectorImpl<StringRef> &SymbolNames,
- const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
- bool processSymbols64(const MachOObject *Obj,
- SmallVectorImpl<unsigned> &SectionMap,
- SmallVectorImpl<StringRef> &SymbolNames,
- const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
-
- void resolveSymbol(StringRef Name);
+ DenseMap<unsigned, RelocationList> Relocations;
+
+ // Relocations to external symbols that are not yet resolved. Symbols are
+ // external when they aren't found in the global symbol table of all loaded
+ // modules. This map is indexed by symbol name.
+ StringMap<RelocationList> ExternalSymbolRelocations;
+
+
+ typedef std::map<RelocationValueRef, uintptr_t> StubMap;
+
+ Triple::ArchType Arch;
+ bool IsTargetLittleEndian;
+ bool IsMipsO32ABI;
+ bool IsMipsN64ABI;
+
+ // True if all sections should be passed to the memory manager, false if only
+ // sections containing relocations should be. Defaults to 'false'.
+ bool ProcessAllSections;
+
+ // This mutex prevents simultaneously loading objects from two different
+ // threads. This keeps us from having to protect individual data structures
+ // and guarantees that section allocation requests to the memory manager
+ // won't be interleaved between modules. It is also used in mapSectionAddress
+ // and resolveRelocations to protect write access to internal data structures.
+ //
+ // loadObject may be called on the same thread during the handling of of
+ // processRelocations, and that's OK. The handling of the relocation lists
+ // is written in such a way as to work correctly if new elements are added to
+ // the end of the list while the list is being processed.
+ sys::Mutex lock;
+
+ virtual unsigned getMaxStubSize() = 0;
+ virtual unsigned getStubAlignment() = 0;
+
+ bool HasError;
+ std::string ErrorStr;
+
+ // Set the error state and record an error string.
+ bool Error(const Twine &Msg) {
+ ErrorStr = Msg.str();
+ HasError = true;
+ return true;
+ }
+
+ uint64_t getSectionLoadAddress(unsigned SectionID) const {
+ return Sections[SectionID].getLoadAddress();
+ }
+
+ uint8_t *getSectionAddress(unsigned SectionID) const {
+ return Sections[SectionID].getAddress();
+ }
+
+ void writeInt16BE(uint8_t *Addr, uint16_t Value) {
+ if (IsTargetLittleEndian)
+ sys::swapByteOrder(Value);
+ *Addr = (Value >> 8) & 0xFF;
+ *(Addr + 1) = Value & 0xFF;
+ }
+
+ void writeInt32BE(uint8_t *Addr, uint32_t Value) {
+ if (IsTargetLittleEndian)
+ sys::swapByteOrder(Value);
+ *Addr = (Value >> 24) & 0xFF;
+ *(Addr + 1) = (Value >> 16) & 0xFF;
+ *(Addr + 2) = (Value >> 8) & 0xFF;
+ *(Addr + 3) = Value & 0xFF;
+ }
+
+ void writeInt64BE(uint8_t *Addr, uint64_t Value) {
+ if (IsTargetLittleEndian)
+ sys::swapByteOrder(Value);
+ *Addr = (Value >> 56) & 0xFF;
+ *(Addr + 1) = (Value >> 48) & 0xFF;
+ *(Addr + 2) = (Value >> 40) & 0xFF;
+ *(Addr + 3) = (Value >> 32) & 0xFF;
+ *(Addr + 4) = (Value >> 24) & 0xFF;
+ *(Addr + 5) = (Value >> 16) & 0xFF;
+ *(Addr + 6) = (Value >> 8) & 0xFF;
+ *(Addr + 7) = Value & 0xFF;
+ }
+
+ virtual void setMipsABI(const ObjectFile &Obj) {
+ IsMipsO32ABI = false;
+ IsMipsN64ABI = false;
+ }
+
+ /// Endian-aware read Read the least significant Size bytes from Src.
+ uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const;
+
+ /// Endian-aware write. Write the least significant Size bytes from Value to
+ /// Dst.
+ void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const;
+
+ /// \brief Given the common symbols discovered in the object file, emit a
+ /// new section for them and update the symbol mappings in the object and
+ /// symbol table.
+ void emitCommonSymbols(const ObjectFile &Obj, CommonSymbolList &CommonSymbols);
+
+ /// \brief Emits section data from the object file to the MemoryManager.
+ /// \param IsCode if it's true then allocateCodeSection() will be
+ /// used for emits, else allocateDataSection() will be used.
+ /// \return SectionID.
+ unsigned emitSection(const ObjectFile &Obj, const SectionRef &Section,
+ bool IsCode);
+
+ /// \brief Find Section in LocalSections. If the secton is not found - emit
+ /// it and store in LocalSections.
+ /// \param IsCode if it's true then allocateCodeSection() will be
+ /// used for emmits, else allocateDataSection() will be used.
+ /// \return SectionID.
+ unsigned findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section,
+ bool IsCode, ObjSectionToIDMap &LocalSections);
+
+ // \brief Add a relocation entry that uses the given section.
+ void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID);
+
+ // \brief Add a relocation entry that uses the given symbol. This symbol may
+ // be found in the global symbol table, or it may be external.
+ void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName);
+
+ /// \brief Emits long jump instruction to Addr.
+ /// \return Pointer to the memory area for emitting target address.
+ uint8_t *createStubFunction(uint8_t *Addr, unsigned AbiVariant = 0);
+
+ /// \brief Resolves relocations from Relocs list with address from Value.
+ void resolveRelocationList(const RelocationList &Relocs, uint64_t Value);
+
+ /// \brief A object file specific relocation resolver
+ /// \param RE The relocation to be resolved
+ /// \param Value Target symbol address to apply the relocation action
+ virtual void resolveRelocation(const RelocationEntry &RE, uint64_t Value) = 0;
+
+ /// \brief Parses one or more object file relocations (some object files use
+ /// relocation pairs) and stores it to Relocations or SymbolRelocations
+ /// (this depends on the object file type).
+ /// \return Iterator to the next relocation that needs to be parsed.
+ virtual relocation_iterator
+ processRelocationRef(unsigned SectionID, relocation_iterator RelI,
+ const ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID,
+ StubMap &Stubs) = 0;
+
+ /// \brief Resolve relocations to external symbols.
+ void resolveExternalSymbols();
+
+ // \brief Compute an upper bound of the memory that is required to load all
+ // sections
+ void computeTotalAllocSize(const ObjectFile &Obj, uint64_t &CodeSize,
+ uint64_t &DataSizeRO, uint64_t &DataSizeRW);
+
+ // \brief Compute the stub buffer size required for a section
+ unsigned computeSectionStubBufSize(const ObjectFile &Obj,
+ const SectionRef &Section);
+
+ // \brief Implementation of the generic part of the loadObject algorithm.
+ ObjSectionToIDMap loadObjectImpl(const object::ObjectFile &Obj);
+
+ // \brief Return true if the relocation R may require allocating a stub.
+ virtual bool relocationNeedsStub(const RelocationRef &R) const {
+ return true; // Conservative answer
+ }