Jim Grosbach | 06594e1 | 2012-01-16 23:50:58 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT --*- C++ -*-===// |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Interface for the implementations of runtime dynamic linker facilities. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 14 | #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H |
| 15 | #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 16 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringMap.h" |
| 19 | #include "llvm/ADT/Triple.h" |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 20 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 22 | #include "llvm/ExecutionEngine/RuntimeDyldChecker.h" |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 23 | #include "llvm/Object/ObjectFile.h" |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/ErrorHandling.h" |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Format.h" |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Host.h" |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Mutex.h" |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 29 | #include "llvm/Support/SwapByteOrder.h" |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 30 | #include <map> |
Keno Fischer | eb59d46 | 2015-12-03 21:27:59 +0000 | [diff] [blame] | 31 | #include <unordered_map> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 32 | #include <system_error> |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 35 | using namespace llvm::object; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 36 | |
| 37 | namespace llvm { |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 38 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 39 | class Twine; |
| 40 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame^] | 41 | #define UNIMPLEMENTED_RELOC(RelType) \ |
| 42 | case RelType: \ |
| 43 | return make_error<RuntimeDyldError>("Unimplemented relocation: " #RelType) |
| 44 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 45 | /// SectionEntry - represents a section emitted into memory by the dynamic |
| 46 | /// linker. |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 47 | class SectionEntry { |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 48 | /// Name - section name. |
Lang Hames | ccc588e | 2015-04-14 17:13:10 +0000 | [diff] [blame] | 49 | std::string Name; |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 50 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 51 | /// Address - address in the linker's memory where the section resides. |
| 52 | uint8_t *Address; |
| 53 | |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 54 | /// Size - section size. Doesn't include the stubs. |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 55 | size_t Size; |
| 56 | |
| 57 | /// LoadAddress - the address of the section in the target process's memory. |
| 58 | /// Used for situations in which JIT-ed code is being executed in the address |
| 59 | /// space of a separate process. If the code executes in the same address |
| 60 | /// space where it was JIT-ed, this just equals Address. |
| 61 | uint64_t LoadAddress; |
| 62 | |
| 63 | /// StubOffset - used for architectures with stub functions for far |
| 64 | /// relocations (like ARM). |
| 65 | uintptr_t StubOffset; |
| 66 | |
Sanjoy Das | 8082592 | 2015-11-23 21:47:46 +0000 | [diff] [blame] | 67 | /// The total amount of space allocated for this section. This includes the |
| 68 | /// section size and the maximum amount of space that the stubs can occupy. |
| 69 | size_t AllocationSize; |
| 70 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 71 | /// ObjAddress - address of the section in the in-memory object file. Used |
| 72 | /// for calculating relocations in some object formats (like MachO). |
| 73 | uintptr_t ObjAddress; |
| 74 | |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 75 | public: |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 76 | SectionEntry(StringRef name, uint8_t *address, size_t size, |
Sanjoy Das | 8082592 | 2015-11-23 21:47:46 +0000 | [diff] [blame] | 77 | size_t allocationSize, uintptr_t objAddress) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 78 | : Name(name), Address(address), Size(size), |
Lang Hames | 0717c3de | 2014-08-27 17:48:07 +0000 | [diff] [blame] | 79 | LoadAddress(reinterpret_cast<uintptr_t>(address)), StubOffset(size), |
Sanjoy Das | 5abfbb9 | 2015-11-23 22:59:36 +0000 | [diff] [blame] | 80 | AllocationSize(allocationSize), ObjAddress(objAddress) { |
| 81 | // AllocationSize is used only in asserts, prevent an "unused private field" |
| 82 | // warning: |
| 83 | (void)AllocationSize; |
| 84 | } |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 85 | |
| 86 | StringRef getName() const { return Name; } |
| 87 | |
| 88 | uint8_t *getAddress() const { return Address; } |
| 89 | |
| 90 | /// \brief Return the address of this section with an offset. |
| 91 | uint8_t *getAddressWithOffset(unsigned OffsetBytes) const { |
Sanjoy Das | 8082592 | 2015-11-23 21:47:46 +0000 | [diff] [blame] | 92 | assert(OffsetBytes <= AllocationSize && "Offset out of bounds!"); |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 93 | return Address + OffsetBytes; |
| 94 | } |
| 95 | |
| 96 | size_t getSize() const { return Size; } |
| 97 | |
| 98 | uint64_t getLoadAddress() const { return LoadAddress; } |
| 99 | void setLoadAddress(uint64_t LA) { LoadAddress = LA; } |
| 100 | |
| 101 | /// \brief Return the load address of this section with an offset. |
| 102 | uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const { |
Sanjoy Das | 8082592 | 2015-11-23 21:47:46 +0000 | [diff] [blame] | 103 | assert(OffsetBytes <= AllocationSize && "Offset out of bounds!"); |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 104 | return LoadAddress + OffsetBytes; |
| 105 | } |
| 106 | |
| 107 | uintptr_t getStubOffset() const { return StubOffset; } |
| 108 | |
Sanjoy Das | 8082592 | 2015-11-23 21:47:46 +0000 | [diff] [blame] | 109 | void advanceStubOffset(unsigned StubSize) { |
| 110 | StubOffset += StubSize; |
| 111 | assert(StubOffset <= AllocationSize && "Not enough space allocated!"); |
| 112 | } |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 113 | |
| 114 | uintptr_t getObjAddress() const { return ObjAddress; } |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 117 | /// RelocationEntry - used to represent relocations internally in the dynamic |
| 118 | /// linker. |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 119 | class RelocationEntry { |
| 120 | public: |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 121 | /// SectionID - the section this relocation points to. |
| 122 | unsigned SectionID; |
| 123 | |
| 124 | /// Offset - offset into the section. |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 125 | uint64_t Offset; |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 126 | |
| 127 | /// RelType - relocation type. |
| 128 | uint32_t RelType; |
| 129 | |
| 130 | /// Addend - the relocation addend encoded in the instruction itself. Also |
| 131 | /// used to make a relocation section relative instead of symbol relative. |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 132 | int64_t Addend; |
| 133 | |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 134 | struct SectionPair { |
| 135 | uint32_t SectionA; |
| 136 | uint32_t SectionB; |
| 137 | }; |
| 138 | |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 139 | /// SymOffset - Section offset of the relocation entry's symbol (used for GOT |
| 140 | /// lookup). |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 141 | union { |
| 142 | uint64_t SymOffset; |
| 143 | SectionPair Sections; |
| 144 | }; |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 145 | |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 146 | /// True if this is a PCRel relocation (MachO specific). |
| 147 | bool IsPCRel; |
| 148 | |
| 149 | /// The size of this relocation (MachO specific). |
| 150 | unsigned Size; |
| 151 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 152 | RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 153 | : SectionID(id), Offset(offset), RelType(type), Addend(addend), |
| 154 | SymOffset(0), IsPCRel(false), Size(0) {} |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 155 | |
| 156 | RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend, |
| 157 | uint64_t symoffset) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 158 | : SectionID(id), Offset(offset), RelType(type), Addend(addend), |
| 159 | SymOffset(symoffset), IsPCRel(false), Size(0) {} |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 160 | |
| 161 | RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend, |
| 162 | bool IsPCRel, unsigned Size) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 163 | : SectionID(id), Offset(offset), RelType(type), Addend(addend), |
| 164 | SymOffset(0), IsPCRel(IsPCRel), Size(Size) {} |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 165 | |
| 166 | RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend, |
| 167 | unsigned SectionA, uint64_t SectionAOffset, unsigned SectionB, |
| 168 | uint64_t SectionBOffset, bool IsPCRel, unsigned Size) |
| 169 | : SectionID(id), Offset(offset), RelType(type), |
| 170 | Addend(SectionAOffset - SectionBOffset + addend), IsPCRel(IsPCRel), |
| 171 | Size(Size) { |
| 172 | Sections.SectionA = SectionA; |
| 173 | Sections.SectionB = SectionB; |
| 174 | } |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 177 | class RelocationValueRef { |
| 178 | public: |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 179 | unsigned SectionID; |
| 180 | uint64_t Offset; |
| 181 | int64_t Addend; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 182 | const char *SymbolName; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 183 | RelocationValueRef() : SectionID(0), Offset(0), Addend(0), |
| 184 | SymbolName(nullptr) {} |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 185 | |
| 186 | inline bool operator==(const RelocationValueRef &Other) const { |
Benjamin Kramer | 5a71250 | 2013-08-20 09:27:31 +0000 | [diff] [blame] | 187 | return SectionID == Other.SectionID && Offset == Other.Offset && |
| 188 | Addend == Other.Addend && SymbolName == Other.SymbolName; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 189 | } |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 190 | inline bool operator<(const RelocationValueRef &Other) const { |
Benjamin Kramer | 5a71250 | 2013-08-20 09:27:31 +0000 | [diff] [blame] | 191 | if (SectionID != Other.SectionID) |
| 192 | return SectionID < Other.SectionID; |
| 193 | if (Offset != Other.Offset) |
| 194 | return Offset < Other.Offset; |
| 195 | if (Addend != Other.Addend) |
| 196 | return Addend < Other.Addend; |
| 197 | return SymbolName < Other.SymbolName; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 198 | } |
| 199 | }; |
| 200 | |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 201 | /// @brief Symbol info for RuntimeDyld. |
| 202 | class SymbolTableEntry : public JITSymbolBase { |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 203 | public: |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 204 | SymbolTableEntry() |
| 205 | : JITSymbolBase(JITSymbolFlags::None), Offset(0), SectionID(0) {} |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 206 | |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 207 | SymbolTableEntry(unsigned SectionID, uint64_t Offset, JITSymbolFlags Flags) |
| 208 | : JITSymbolBase(Flags), Offset(Offset), SectionID(SectionID) {} |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 209 | |
| 210 | unsigned getSectionID() const { return SectionID; } |
| 211 | uint64_t getOffset() const { return Offset; } |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 212 | |
| 213 | private: |
| 214 | uint64_t Offset; |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 215 | unsigned SectionID; |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 218 | typedef StringMap<SymbolTableEntry> RTDyldSymbolTable; |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 219 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 220 | class RuntimeDyldImpl { |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 221 | friend class RuntimeDyld::LoadedObjectInfo; |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 222 | friend class RuntimeDyldCheckerImpl; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 223 | protected: |
Lang Hames | a32d71b | 2015-10-18 01:41:37 +0000 | [diff] [blame] | 224 | static const unsigned AbsoluteSymbolSection = ~0U; |
| 225 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 226 | // The MemoryManager to load objects into. |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 227 | RuntimeDyld::MemoryManager &MemMgr; |
| 228 | |
| 229 | // The symbol resolver to use for external symbols. |
| 230 | RuntimeDyld::SymbolResolver &Resolver; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 231 | |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 232 | // Attached RuntimeDyldChecker instance. Null if no instance attached. |
| 233 | RuntimeDyldCheckerImpl *Checker; |
| 234 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 235 | // A list of all sections emitted by the dynamic linker. These sections are |
| 236 | // referenced in the code by means of their index in this list - SectionID. |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 237 | typedef SmallVector<SectionEntry, 64> SectionList; |
| 238 | SectionList Sections; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 239 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 240 | typedef unsigned SID; // Type for SectionIDs |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 241 | #define RTDYLD_INVALID_SECTION_ID ((RuntimeDyldImpl::SID)(-1)) |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 242 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 243 | // Keep a map of sections from object file to the SectionID which |
| 244 | // references it. |
| 245 | typedef std::map<SectionRef, unsigned> ObjSectionToIDMap; |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 246 | |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 247 | // A global symbol table for symbols from all loaded modules. |
| 248 | RTDyldSymbolTable GlobalSymbolTable; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 249 | |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 250 | // Keep a map of common symbols to their info pairs |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 251 | typedef std::vector<SymbolRef> CommonSymbolList; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 252 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 253 | // For each symbol, keep a list of relocations based on it. Anytime |
| 254 | // its address is reassigned (the JIT re-compiled the function, e.g.), |
| 255 | // the relocations get re-resolved. |
| 256 | // The symbol (or section) the relocation is sourced from is the Key |
| 257 | // in the relocation list where it's stored. |
| 258 | typedef SmallVector<RelocationEntry, 64> RelocationList; |
| 259 | // Relocations to sections already loaded. Indexed by SectionID which is the |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 260 | // source of the address. The target where the address will be written is |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 261 | // SectionID/Offset in the relocation itself. |
Keno Fischer | eb59d46 | 2015-12-03 21:27:59 +0000 | [diff] [blame] | 262 | std::unordered_map<unsigned, RelocationList> Relocations; |
Eli Bendersky | b92e1cf | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 263 | |
| 264 | // Relocations to external symbols that are not yet resolved. Symbols are |
| 265 | // external when they aren't found in the global symbol table of all loaded |
| 266 | // modules. This map is indexed by symbol name. |
| 267 | StringMap<RelocationList> ExternalSymbolRelocations; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 268 | |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 269 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 270 | typedef std::map<RelocationValueRef, uintptr_t> StubMap; |
| 271 | |
| 272 | Triple::ArchType Arch; |
Andrew Kaylor | 33c5b1b | 2013-10-15 20:44:55 +0000 | [diff] [blame] | 273 | bool IsTargetLittleEndian; |
Petar Jovanovic | 9720283 | 2015-05-28 13:48:41 +0000 | [diff] [blame] | 274 | bool IsMipsO32ABI; |
| 275 | bool IsMipsN64ABI; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 276 | |
Lang Hames | 868d4b3 | 2014-03-20 21:06:46 +0000 | [diff] [blame] | 277 | // True if all sections should be passed to the memory manager, false if only |
| 278 | // sections containing relocations should be. Defaults to 'false'. |
| 279 | bool ProcessAllSections; |
| 280 | |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 281 | // This mutex prevents simultaneously loading objects from two different |
| 282 | // threads. This keeps us from having to protect individual data structures |
| 283 | // and guarantees that section allocation requests to the memory manager |
| 284 | // won't be interleaved between modules. It is also used in mapSectionAddress |
| 285 | // and resolveRelocations to protect write access to internal data structures. |
| 286 | // |
| 287 | // loadObject may be called on the same thread during the handling of of |
| 288 | // processRelocations, and that's OK. The handling of the relocation lists |
| 289 | // is written in such a way as to work correctly if new elements are added to |
| 290 | // the end of the list while the list is being processed. |
| 291 | sys::Mutex lock; |
| 292 | |
Andrew Kaylor | 2ba21c5 | 2013-10-15 21:32:56 +0000 | [diff] [blame] | 293 | virtual unsigned getMaxStubSize() = 0; |
| 294 | virtual unsigned getStubAlignment() = 0; |
Richard Sandiford | ca04408 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 295 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 296 | bool HasError; |
| 297 | std::string ErrorStr; |
| 298 | |
Lang Hames | 3e930a3 | 2014-09-05 18:00:16 +0000 | [diff] [blame] | 299 | uint64_t getSectionLoadAddress(unsigned SectionID) const { |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 300 | return Sections[SectionID].getLoadAddress(); |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Lang Hames | 3e930a3 | 2014-09-05 18:00:16 +0000 | [diff] [blame] | 303 | uint8_t *getSectionAddress(unsigned SectionID) const { |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 304 | return Sections[SectionID].getAddress(); |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 305 | } |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 306 | |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 307 | void writeInt16BE(uint8_t *Addr, uint16_t Value) { |
Andrew Kaylor | 33c5b1b | 2013-10-15 20:44:55 +0000 | [diff] [blame] | 308 | if (IsTargetLittleEndian) |
Artyom Skrobov | 9aea843 | 2014-06-14 13:18:07 +0000 | [diff] [blame] | 309 | sys::swapByteOrder(Value); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 310 | *Addr = (Value >> 8) & 0xFF; |
| 311 | *(Addr + 1) = Value & 0xFF; |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void writeInt32BE(uint8_t *Addr, uint32_t Value) { |
Andrew Kaylor | 33c5b1b | 2013-10-15 20:44:55 +0000 | [diff] [blame] | 315 | if (IsTargetLittleEndian) |
Artyom Skrobov | 9aea843 | 2014-06-14 13:18:07 +0000 | [diff] [blame] | 316 | sys::swapByteOrder(Value); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 317 | *Addr = (Value >> 24) & 0xFF; |
| 318 | *(Addr + 1) = (Value >> 16) & 0xFF; |
| 319 | *(Addr + 2) = (Value >> 8) & 0xFF; |
| 320 | *(Addr + 3) = Value & 0xFF; |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void writeInt64BE(uint8_t *Addr, uint64_t Value) { |
Andrew Kaylor | 33c5b1b | 2013-10-15 20:44:55 +0000 | [diff] [blame] | 324 | if (IsTargetLittleEndian) |
Artyom Skrobov | 9aea843 | 2014-06-14 13:18:07 +0000 | [diff] [blame] | 325 | sys::swapByteOrder(Value); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 326 | *Addr = (Value >> 56) & 0xFF; |
| 327 | *(Addr + 1) = (Value >> 48) & 0xFF; |
| 328 | *(Addr + 2) = (Value >> 40) & 0xFF; |
| 329 | *(Addr + 3) = (Value >> 32) & 0xFF; |
| 330 | *(Addr + 4) = (Value >> 24) & 0xFF; |
| 331 | *(Addr + 5) = (Value >> 16) & 0xFF; |
| 332 | *(Addr + 6) = (Value >> 8) & 0xFF; |
| 333 | *(Addr + 7) = Value & 0xFF; |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Petar Jovanovic | 9720283 | 2015-05-28 13:48:41 +0000 | [diff] [blame] | 336 | virtual void setMipsABI(const ObjectFile &Obj) { |
| 337 | IsMipsO32ABI = false; |
| 338 | IsMipsN64ABI = false; |
| 339 | } |
| 340 | |
Lang Hames | e1287c0 | 2014-08-29 23:17:47 +0000 | [diff] [blame] | 341 | /// Endian-aware read Read the least significant Size bytes from Src. |
| 342 | uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const; |
| 343 | |
| 344 | /// Endian-aware write. Write the least significant Size bytes from Value to |
| 345 | /// Dst. |
| 346 | void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const; |
| 347 | |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 348 | /// \brief Given the common symbols discovered in the object file, emit a |
| 349 | /// new section for them and update the symbol mappings in the object and |
| 350 | /// symbol table. |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame^] | 351 | Error emitCommonSymbols(const ObjectFile &Obj, |
| 352 | CommonSymbolList &CommonSymbols); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 353 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 354 | /// \brief Emits section data from the object file to the MemoryManager. |
| 355 | /// \param IsCode if it's true then allocateCodeSection() will be |
Eli Bendersky | 0e2ac5b | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 356 | /// used for emits, else allocateDataSection() will be used. |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 357 | /// \return SectionID. |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame^] | 358 | Expected<unsigned> emitSection(const ObjectFile &Obj, |
| 359 | const SectionRef &Section, |
| 360 | bool IsCode); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 361 | |
| 362 | /// \brief Find Section in LocalSections. If the secton is not found - emit |
| 363 | /// it and store in LocalSections. |
| 364 | /// \param IsCode if it's true then allocateCodeSection() will be |
| 365 | /// used for emmits, else allocateDataSection() will be used. |
| 366 | /// \return SectionID. |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame^] | 367 | Expected<unsigned> findOrEmitSection(const ObjectFile &Obj, |
| 368 | const SectionRef &Section, bool IsCode, |
| 369 | ObjSectionToIDMap &LocalSections); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 370 | |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 371 | // \brief Add a relocation entry that uses the given section. |
| 372 | void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID); |
| 373 | |
| 374 | // \brief Add a relocation entry that uses the given symbol. This symbol may |
| 375 | // be found in the global symbol table, or it may be external. |
| 376 | void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 377 | |
| 378 | /// \brief Emits long jump instruction to Addr. |
| 379 | /// \return Pointer to the memory area for emitting target address. |
Ulrich Weigand | 752b5c9 | 2014-07-20 23:53:14 +0000 | [diff] [blame] | 380 | uint8_t *createStubFunction(uint8_t *Addr, unsigned AbiVariant = 0); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 381 | |
| 382 | /// \brief Resolves relocations from Relocs list with address from Value. |
| 383 | void resolveRelocationList(const RelocationList &Relocs, uint64_t Value); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 384 | |
| 385 | /// \brief A object file specific relocation resolver |
Rafael Espindola | b39478e | 2013-04-29 19:33:51 +0000 | [diff] [blame] | 386 | /// \param RE The relocation to be resolved |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 387 | /// \param Value Target symbol address to apply the relocation action |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 388 | virtual void resolveRelocation(const RelocationEntry &RE, uint64_t Value) = 0; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 389 | |
Juergen Ributzka | 838282e | 2014-03-21 20:38:46 +0000 | [diff] [blame] | 390 | /// \brief Parses one or more object file relocations (some object files use |
| 391 | /// relocation pairs) and stores it to Relocations or SymbolRelocations |
| 392 | /// (this depends on the object file type). |
| 393 | /// \return Iterator to the next relocation that needs to be parsed. |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame^] | 394 | virtual Expected<relocation_iterator> |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 395 | processRelocationRef(unsigned SectionID, relocation_iterator RelI, |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 396 | const ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID, |
Lang Hames | a5cd950 | 2014-11-27 05:40:13 +0000 | [diff] [blame] | 397 | StubMap &Stubs) = 0; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 398 | |
Eli Bendersky | b92e1cf | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 399 | /// \brief Resolve relocations to external symbols. |
| 400 | void resolveExternalSymbols(); |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 401 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 402 | // \brief Compute an upper bound of the memory that is required to load all |
| 403 | // sections |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame^] | 404 | Error computeTotalAllocSize(const ObjectFile &Obj, |
| 405 | uint64_t &CodeSize, uint32_t &CodeAlign, |
| 406 | uint64_t &RODataSize, uint32_t &RODataAlign, |
| 407 | uint64_t &RWDataSize, uint32_t &RWDataAlign); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 408 | |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 409 | // \brief Compute the stub buffer size required for a section |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 410 | unsigned computeSectionStubBufSize(const ObjectFile &Obj, |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 411 | const SectionRef &Section); |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 412 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 413 | // \brief Implementation of the generic part of the loadObject algorithm. |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame^] | 414 | Expected<ObjSectionToIDMap> loadObjectImpl(const object::ObjectFile &Obj); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 415 | |
Sanjoy Das | d5658b0 | 2015-11-23 21:47:51 +0000 | [diff] [blame] | 416 | // \brief Return true if the relocation R may require allocating a stub. |
| 417 | virtual bool relocationNeedsStub(const RelocationRef &R) const { |
| 418 | return true; // Conservative answer |
| 419 | } |
| 420 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 421 | public: |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 422 | RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr, |
| 423 | RuntimeDyld::SymbolResolver &Resolver) |
| 424 | : MemMgr(MemMgr), Resolver(Resolver), Checker(nullptr), |
| 425 | ProcessAllSections(false), HasError(false) { |
Lang Hames | e1c1138 | 2014-06-27 20:20:57 +0000 | [diff] [blame] | 426 | } |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 427 | |
| 428 | virtual ~RuntimeDyldImpl(); |
| 429 | |
Lang Hames | 868d4b3 | 2014-03-20 21:06:46 +0000 | [diff] [blame] | 430 | void setProcessAllSections(bool ProcessAllSections) { |
| 431 | this->ProcessAllSections = ProcessAllSections; |
| 432 | } |
| 433 | |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 434 | void setRuntimeDyldChecker(RuntimeDyldCheckerImpl *Checker) { |
| 435 | this->Checker = Checker; |
| 436 | } |
| 437 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 438 | virtual std::unique_ptr<RuntimeDyld::LoadedObjectInfo> |
| 439 | loadObject(const object::ObjectFile &Obj) = 0; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 440 | |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 441 | uint8_t* getSymbolLocalAddress(StringRef Name) const { |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 442 | // FIXME: Just look up as a function for now. Overly simple of course. |
| 443 | // Work in progress. |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 444 | RTDyldSymbolTable::const_iterator pos = GlobalSymbolTable.find(Name); |
Yaron Keren | c980288 | 2013-10-19 09:04:26 +0000 | [diff] [blame] | 445 | if (pos == GlobalSymbolTable.end()) |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 446 | return nullptr; |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 447 | const auto &SymInfo = pos->second; |
Lang Hames | a32d71b | 2015-10-18 01:41:37 +0000 | [diff] [blame] | 448 | // Absolute symbols do not have a local address. |
| 449 | if (SymInfo.getSectionID() == AbsoluteSymbolSection) |
| 450 | return nullptr; |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 451 | return getSectionAddress(SymInfo.getSectionID()) + SymInfo.getOffset(); |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 454 | RuntimeDyld::SymbolInfo getSymbol(StringRef Name) const { |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 455 | // FIXME: Just look up as a function for now. Overly simple of course. |
| 456 | // Work in progress. |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 457 | RTDyldSymbolTable::const_iterator pos = GlobalSymbolTable.find(Name); |
Yaron Keren | c980288 | 2013-10-19 09:04:26 +0000 | [diff] [blame] | 458 | if (pos == GlobalSymbolTable.end()) |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 459 | return nullptr; |
| 460 | const auto &SymEntry = pos->second; |
Lang Hames | a32d71b | 2015-10-18 01:41:37 +0000 | [diff] [blame] | 461 | uint64_t SectionAddr = 0; |
| 462 | if (SymEntry.getSectionID() != AbsoluteSymbolSection) |
| 463 | SectionAddr = getSectionLoadAddress(SymEntry.getSectionID()); |
| 464 | uint64_t TargetAddr = SectionAddr + SymEntry.getOffset(); |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 465 | return RuntimeDyld::SymbolInfo(TargetAddr, SymEntry.getFlags()); |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 468 | void resolveRelocations(); |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 469 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 470 | void reassignSectionAddress(unsigned SectionID, uint64_t Addr); |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 471 | |
Jim Grosbach | 6d61397 | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 472 | void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress); |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 473 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 474 | // Is the linker in an error state? |
| 475 | bool hasError() { return HasError; } |
| 476 | |
| 477 | // Mark the error condition as handled and continue. |
| 478 | void clearError() { HasError = false; } |
| 479 | |
| 480 | // Get the error message. |
| 481 | StringRef getErrorString() { return ErrorStr; } |
| 482 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 483 | virtual bool isCompatibleFile(const ObjectFile &Obj) const = 0; |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 484 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 485 | virtual void registerEHFrames(); |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 486 | |
Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 487 | virtual void deregisterEHFrames(); |
| 488 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame^] | 489 | virtual Error finalizeLoad(const ObjectFile &ObjImg, |
| 490 | ObjSectionToIDMap &SectionMap) { |
| 491 | return Error::success(); |
| 492 | } |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 493 | }; |
| 494 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 495 | } // end namespace llvm |
| 496 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 497 | #endif |