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 | |
| 14 | #ifndef LLVM_RUNTIME_DYLD_IMPL_H |
| 15 | #define LLVM_RUNTIME_DYLD_IMPL_H |
| 16 | |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringMap.h" |
| 20 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/ObjectImage.h" |
| 22 | #include "llvm/ExecutionEngine/RuntimeDyld.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 "llvm/Support/raw_ostream.h" |
| 31 | #include "llvm/Support/system_error.h" |
| 32 | #include <map> |
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 | |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 39 | class ObjectBuffer; |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 40 | class Twine; |
| 41 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 42 | /// SectionEntry - represents a section emitted into memory by the dynamic |
| 43 | /// linker. |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 44 | class SectionEntry { |
| 45 | public: |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 46 | /// Name - section name. |
| 47 | StringRef Name; |
| 48 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 49 | /// Address - address in the linker's memory where the section resides. |
| 50 | uint8_t *Address; |
| 51 | |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 52 | /// Size - section size. Doesn't include the stubs. |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 53 | size_t Size; |
| 54 | |
| 55 | /// LoadAddress - the address of the section in the target process's memory. |
| 56 | /// Used for situations in which JIT-ed code is being executed in the address |
| 57 | /// space of a separate process. If the code executes in the same address |
| 58 | /// space where it was JIT-ed, this just equals Address. |
| 59 | uint64_t LoadAddress; |
| 60 | |
| 61 | /// StubOffset - used for architectures with stub functions for far |
| 62 | /// relocations (like ARM). |
| 63 | uintptr_t StubOffset; |
| 64 | |
| 65 | /// ObjAddress - address of the section in the in-memory object file. Used |
| 66 | /// for calculating relocations in some object formats (like MachO). |
| 67 | uintptr_t ObjAddress; |
| 68 | |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 69 | SectionEntry(StringRef name, uint8_t *address, size_t size, |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 70 | uintptr_t objAddress) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 71 | : Name(name), Address(address), Size(size), |
| 72 | LoadAddress((uintptr_t)address), StubOffset(size), |
| 73 | ObjAddress(objAddress) {} |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 76 | /// RelocationEntry - used to represent relocations internally in the dynamic |
| 77 | /// linker. |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 78 | class RelocationEntry { |
| 79 | public: |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 80 | /// SectionID - the section this relocation points to. |
| 81 | unsigned SectionID; |
| 82 | |
| 83 | /// Offset - offset into the section. |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 84 | uint64_t Offset; |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 85 | |
| 86 | /// RelType - relocation type. |
| 87 | uint32_t RelType; |
| 88 | |
| 89 | /// Addend - the relocation addend encoded in the instruction itself. Also |
| 90 | /// used to make a relocation section relative instead of symbol relative. |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 91 | int64_t Addend; |
| 92 | |
| 93 | /// SymOffset - Section offset of the relocation entry's symbol (used for GOT |
| 94 | /// lookup). |
| 95 | uint64_t SymOffset; |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 96 | |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 97 | /// True if this is a PCRel relocation (MachO specific). |
| 98 | bool IsPCRel; |
| 99 | |
| 100 | /// The size of this relocation (MachO specific). |
| 101 | unsigned Size; |
| 102 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 103 | RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 104 | : SectionID(id), Offset(offset), RelType(type), Addend(addend), |
| 105 | SymOffset(0), IsPCRel(false), Size(0) {} |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 106 | |
| 107 | RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend, |
| 108 | uint64_t symoffset) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 109 | : SectionID(id), Offset(offset), RelType(type), Addend(addend), |
| 110 | SymOffset(symoffset), IsPCRel(false), Size(0) {} |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 111 | |
| 112 | RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend, |
| 113 | bool IsPCRel, unsigned Size) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 114 | : SectionID(id), Offset(offset), RelType(type), Addend(addend), |
| 115 | SymOffset(0), IsPCRel(IsPCRel), Size(Size) {} |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 118 | class RelocationValueRef { |
| 119 | public: |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 120 | unsigned SectionID; |
| 121 | uint64_t Offset; |
| 122 | int64_t Addend; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 123 | const char *SymbolName; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 124 | RelocationValueRef() : SectionID(0), Offset(0), Addend(0), SymbolName(0) {} |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 125 | |
| 126 | inline bool operator==(const RelocationValueRef &Other) const { |
Benjamin Kramer | 5a71250 | 2013-08-20 09:27:31 +0000 | [diff] [blame] | 127 | return SectionID == Other.SectionID && Offset == Other.Offset && |
| 128 | Addend == Other.Addend && SymbolName == Other.SymbolName; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 129 | } |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 130 | inline bool operator<(const RelocationValueRef &Other) const { |
Benjamin Kramer | 5a71250 | 2013-08-20 09:27:31 +0000 | [diff] [blame] | 131 | if (SectionID != Other.SectionID) |
| 132 | return SectionID < Other.SectionID; |
| 133 | if (Offset != Other.Offset) |
| 134 | return Offset < Other.Offset; |
| 135 | if (Addend != Other.Addend) |
| 136 | return Addend < Other.Addend; |
| 137 | return SymbolName < Other.SymbolName; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 138 | } |
| 139 | }; |
| 140 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 141 | class RuntimeDyldImpl { |
| 142 | protected: |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 143 | // The MemoryManager to load objects into. |
| 144 | RTDyldMemoryManager *MemMgr; |
| 145 | |
Eli Bendersky | 32d5488 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 146 | // A list of all sections emitted by the dynamic linker. These sections are |
| 147 | // 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] | 148 | typedef SmallVector<SectionEntry, 64> SectionList; |
| 149 | SectionList Sections; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 150 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 151 | typedef unsigned SID; // Type for SectionIDs |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 152 | #define RTDYLD_INVALID_SECTION_ID ((SID)(-1)) |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 153 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 154 | // Keep a map of sections from object file to the SectionID which |
| 155 | // references it. |
| 156 | typedef std::map<SectionRef, unsigned> ObjSectionToIDMap; |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 157 | |
Eli Bendersky | fc07908 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 158 | // A global symbol table for symbols from all loaded modules. Maps the |
| 159 | // symbol name to a (SectionID, offset in section) pair. |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 160 | typedef std::pair<unsigned, uintptr_t> SymbolLoc; |
Eli Bendersky | fc07908 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 161 | typedef StringMap<SymbolLoc> SymbolTableMap; |
| 162 | SymbolTableMap GlobalSymbolTable; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 163 | |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 164 | // Pair representing the size and alignment requirement for a common symbol. |
| 165 | typedef std::pair<unsigned, unsigned> CommonSymbolInfo; |
| 166 | // Keep a map of common symbols to their info pairs |
| 167 | typedef std::map<SymbolRef, CommonSymbolInfo> CommonSymbolMap; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 168 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 169 | // For each symbol, keep a list of relocations based on it. Anytime |
| 170 | // its address is reassigned (the JIT re-compiled the function, e.g.), |
| 171 | // the relocations get re-resolved. |
| 172 | // The symbol (or section) the relocation is sourced from is the Key |
| 173 | // in the relocation list where it's stored. |
| 174 | typedef SmallVector<RelocationEntry, 64> RelocationList; |
| 175 | // Relocations to sections already loaded. Indexed by SectionID which is the |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 176 | // 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] | 177 | // SectionID/Offset in the relocation itself. |
| 178 | DenseMap<unsigned, RelocationList> Relocations; |
Eli Bendersky | b92e1cf | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 179 | |
| 180 | // Relocations to external symbols that are not yet resolved. Symbols are |
| 181 | // external when they aren't found in the global symbol table of all loaded |
| 182 | // modules. This map is indexed by symbol name. |
| 183 | StringMap<RelocationList> ExternalSymbolRelocations; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 184 | |
| 185 | typedef std::map<RelocationValueRef, uintptr_t> StubMap; |
| 186 | |
| 187 | Triple::ArchType Arch; |
Andrew Kaylor | 33c5b1b | 2013-10-15 20:44:55 +0000 | [diff] [blame] | 188 | bool IsTargetLittleEndian; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 189 | |
Lang Hames | 868d4b3 | 2014-03-20 21:06:46 +0000 | [diff] [blame] | 190 | // True if all sections should be passed to the memory manager, false if only |
| 191 | // sections containing relocations should be. Defaults to 'false'. |
| 192 | bool ProcessAllSections; |
| 193 | |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 194 | // This mutex prevents simultaneously loading objects from two different |
| 195 | // threads. This keeps us from having to protect individual data structures |
| 196 | // and guarantees that section allocation requests to the memory manager |
| 197 | // won't be interleaved between modules. It is also used in mapSectionAddress |
| 198 | // and resolveRelocations to protect write access to internal data structures. |
| 199 | // |
| 200 | // loadObject may be called on the same thread during the handling of of |
| 201 | // processRelocations, and that's OK. The handling of the relocation lists |
| 202 | // is written in such a way as to work correctly if new elements are added to |
| 203 | // the end of the list while the list is being processed. |
| 204 | sys::Mutex lock; |
| 205 | |
Andrew Kaylor | 2ba21c5 | 2013-10-15 21:32:56 +0000 | [diff] [blame] | 206 | virtual unsigned getMaxStubSize() = 0; |
| 207 | virtual unsigned getStubAlignment() = 0; |
Richard Sandiford | ca04408 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 208 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 209 | bool HasError; |
| 210 | std::string ErrorStr; |
| 211 | |
| 212 | // Set the error state and record an error string. |
| 213 | bool Error(const Twine &Msg) { |
| 214 | ErrorStr = Msg.str(); |
| 215 | HasError = true; |
| 216 | return true; |
| 217 | } |
| 218 | |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 219 | uint64_t getSectionLoadAddress(unsigned SectionID) { |
| 220 | return Sections[SectionID].LoadAddress; |
| 221 | } |
| 222 | |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 223 | uint8_t *getSectionAddress(unsigned SectionID) { |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 224 | return (uint8_t *)Sections[SectionID].Address; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 225 | } |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 226 | |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 227 | void writeInt16BE(uint8_t *Addr, uint16_t Value) { |
Andrew Kaylor | 33c5b1b | 2013-10-15 20:44:55 +0000 | [diff] [blame] | 228 | if (IsTargetLittleEndian) |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 229 | Value = sys::SwapByteOrder(Value); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 230 | *Addr = (Value >> 8) & 0xFF; |
| 231 | *(Addr + 1) = Value & 0xFF; |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void writeInt32BE(uint8_t *Addr, uint32_t Value) { |
Andrew Kaylor | 33c5b1b | 2013-10-15 20:44:55 +0000 | [diff] [blame] | 235 | if (IsTargetLittleEndian) |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 236 | Value = sys::SwapByteOrder(Value); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 237 | *Addr = (Value >> 24) & 0xFF; |
| 238 | *(Addr + 1) = (Value >> 16) & 0xFF; |
| 239 | *(Addr + 2) = (Value >> 8) & 0xFF; |
| 240 | *(Addr + 3) = Value & 0xFF; |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void writeInt64BE(uint8_t *Addr, uint64_t Value) { |
Andrew Kaylor | 33c5b1b | 2013-10-15 20:44:55 +0000 | [diff] [blame] | 244 | if (IsTargetLittleEndian) |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 245 | Value = sys::SwapByteOrder(Value); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 246 | *Addr = (Value >> 56) & 0xFF; |
| 247 | *(Addr + 1) = (Value >> 48) & 0xFF; |
| 248 | *(Addr + 2) = (Value >> 40) & 0xFF; |
| 249 | *(Addr + 3) = (Value >> 32) & 0xFF; |
| 250 | *(Addr + 4) = (Value >> 24) & 0xFF; |
| 251 | *(Addr + 5) = (Value >> 16) & 0xFF; |
| 252 | *(Addr + 6) = (Value >> 8) & 0xFF; |
| 253 | *(Addr + 7) = Value & 0xFF; |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 256 | /// \brief Given the common symbols discovered in the object file, emit a |
| 257 | /// new section for them and update the symbol mappings in the object and |
| 258 | /// symbol table. |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 259 | void emitCommonSymbols(ObjectImage &Obj, const CommonSymbolMap &CommonSymbols, |
| 260 | uint64_t TotalSize, SymbolTableMap &SymbolTable); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 261 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 262 | /// \brief Emits section data from the object file to the MemoryManager. |
| 263 | /// \param IsCode if it's true then allocateCodeSection() will be |
Eli Bendersky | 0e2ac5b | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 264 | /// used for emits, else allocateDataSection() will be used. |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 265 | /// \return SectionID. |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 266 | unsigned emitSection(ObjectImage &Obj, const SectionRef &Section, |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 267 | bool IsCode); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 268 | |
| 269 | /// \brief Find Section in LocalSections. If the secton is not found - emit |
| 270 | /// it and store in LocalSections. |
| 271 | /// \param IsCode if it's true then allocateCodeSection() will be |
| 272 | /// used for emmits, else allocateDataSection() will be used. |
| 273 | /// \return SectionID. |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 274 | unsigned findOrEmitSection(ObjectImage &Obj, const SectionRef &Section, |
| 275 | bool IsCode, ObjSectionToIDMap &LocalSections); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 276 | |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 277 | // \brief Add a relocation entry that uses the given section. |
| 278 | void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID); |
| 279 | |
| 280 | // \brief Add a relocation entry that uses the given symbol. This symbol may |
| 281 | // be found in the global symbol table, or it may be external. |
| 282 | void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 283 | |
| 284 | /// \brief Emits long jump instruction to Addr. |
| 285 | /// \return Pointer to the memory area for emitting target address. |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 286 | uint8_t *createStubFunction(uint8_t *Addr); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 287 | |
| 288 | /// \brief Resolves relocations from Relocs list with address from Value. |
| 289 | void resolveRelocationList(const RelocationList &Relocs, uint64_t Value); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 290 | |
| 291 | /// \brief A object file specific relocation resolver |
Rafael Espindola | b39478e | 2013-04-29 19:33:51 +0000 | [diff] [blame] | 292 | /// \param RE The relocation to be resolved |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 293 | /// \param Value Target symbol address to apply the relocation action |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 294 | virtual void resolveRelocation(const RelocationEntry &RE, uint64_t Value) = 0; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 295 | |
Juergen Ributzka | 838282e | 2014-03-21 20:38:46 +0000 | [diff] [blame^] | 296 | /// \brief Parses one or more object file relocations (some object files use |
| 297 | /// relocation pairs) and stores it to Relocations or SymbolRelocations |
| 298 | /// (this depends on the object file type). |
| 299 | /// \return Iterator to the next relocation that needs to be parsed. |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 300 | virtual relocation_iterator |
| 301 | processRelocationRef(unsigned SectionID, relocation_iterator RelI, |
| 302 | ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID, |
| 303 | const SymbolTableMap &Symbols, StubMap &Stubs) = 0; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 304 | |
Eli Bendersky | b92e1cf | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 305 | /// \brief Resolve relocations to external symbols. |
| 306 | void resolveExternalSymbols(); |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 307 | |
| 308 | /// \brief Update GOT entries for external symbols. |
| 309 | // The base class does nothing. ELF overrides this. |
| 310 | virtual void updateGOTEntries(StringRef Name, uint64_t Addr) {} |
| 311 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 312 | // \brief Compute an upper bound of the memory that is required to load all |
| 313 | // sections |
| 314 | void computeTotalAllocSize(ObjectImage &Obj, uint64_t &CodeSize, |
| 315 | uint64_t &DataSizeRO, uint64_t &DataSizeRW); |
| 316 | |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 317 | // \brief Compute the stub buffer size required for a section |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 318 | unsigned computeSectionStubBufSize(ObjectImage &Obj, |
| 319 | const SectionRef &Section); |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 320 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 321 | public: |
Lang Hames | 868d4b3 | 2014-03-20 21:06:46 +0000 | [diff] [blame] | 322 | RuntimeDyldImpl(RTDyldMemoryManager *mm) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 323 | : MemMgr(mm), ProcessAllSections(false), HasError(false) {} |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 324 | |
| 325 | virtual ~RuntimeDyldImpl(); |
| 326 | |
Lang Hames | 868d4b3 | 2014-03-20 21:06:46 +0000 | [diff] [blame] | 327 | void setProcessAllSections(bool ProcessAllSections) { |
| 328 | this->ProcessAllSections = ProcessAllSections; |
| 329 | } |
| 330 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 331 | ObjectImage *loadObject(ObjectImage *InputObject); |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 332 | |
| 333 | void *getSymbolAddress(StringRef Name) { |
| 334 | // FIXME: Just look up as a function for now. Overly simple of course. |
| 335 | // Work in progress. |
Yaron Keren | c980288 | 2013-10-19 09:04:26 +0000 | [diff] [blame] | 336 | SymbolTableMap::const_iterator pos = GlobalSymbolTable.find(Name); |
| 337 | if (pos == GlobalSymbolTable.end()) |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 338 | return 0; |
Yaron Keren | c980288 | 2013-10-19 09:04:26 +0000 | [diff] [blame] | 339 | SymbolLoc Loc = pos->second; |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 340 | return getSectionAddress(Loc.first) + Loc.second; |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 343 | uint64_t getSymbolLoadAddress(StringRef Name) { |
| 344 | // FIXME: Just look up as a function for now. Overly simple of course. |
| 345 | // Work in progress. |
Yaron Keren | c980288 | 2013-10-19 09:04:26 +0000 | [diff] [blame] | 346 | SymbolTableMap::const_iterator pos = GlobalSymbolTable.find(Name); |
| 347 | if (pos == GlobalSymbolTable.end()) |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 348 | return 0; |
Yaron Keren | c980288 | 2013-10-19 09:04:26 +0000 | [diff] [blame] | 349 | SymbolLoc Loc = pos->second; |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 350 | return getSectionLoadAddress(Loc.first) + Loc.second; |
| 351 | } |
| 352 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 353 | void resolveRelocations(); |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 354 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 355 | void reassignSectionAddress(unsigned SectionID, uint64_t Addr); |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 356 | |
Jim Grosbach | 6d61397 | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 357 | void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress); |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 358 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 359 | // Is the linker in an error state? |
| 360 | bool hasError() { return HasError; } |
| 361 | |
| 362 | // Mark the error condition as handled and continue. |
| 363 | void clearError() { HasError = false; } |
| 364 | |
| 365 | // Get the error message. |
| 366 | StringRef getErrorString() { return ErrorStr; } |
| 367 | |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 368 | virtual bool isCompatibleFormat(const ObjectBuffer *Buffer) const = 0; |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 369 | virtual bool isCompatibleFile(const ObjectFile *Obj) const = 0; |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 370 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 371 | virtual void registerEHFrames(); |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 372 | |
Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 373 | virtual void deregisterEHFrames(); |
| 374 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 375 | virtual void finalizeLoad(ObjSectionToIDMap &SectionMap) {} |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 376 | }; |
| 377 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 378 | } // end namespace llvm |
| 379 | |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 380 | #endif |