Jim Grosbach | 06594e1 | 2012-01-16 23:50:58 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===// |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +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 | // Implementation of the MC-JIT runtime dynamic linker. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 15 | #include "RuntimeDyldCheckerImpl.h" |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 16 | #include "RuntimeDyldCOFF.h" |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 17 | #include "RuntimeDyldELF.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "RuntimeDyldImpl.h" |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 19 | #include "RuntimeDyldMachO.h" |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 20 | #include "llvm/Object/ELFObjectFile.h" |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 21 | #include "llvm/Object/COFF.h" |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ManagedStatic.h" |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MutexGuard.h" |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 25 | |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | using namespace llvm::object; |
| 28 | |
Chandler Carruth | f58e376 | 2014-04-22 03:04:17 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "dyld" |
| 30 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | enum RuntimeDyldErrorCode { |
| 34 | GenericRTDyldError = 1 |
| 35 | }; |
| 36 | |
| 37 | class RuntimeDyldErrorCategory : public std::error_category { |
| 38 | public: |
| 39 | const char *name() const LLVM_NOEXCEPT override { return "runtimedyld"; } |
| 40 | |
| 41 | std::string message(int Condition) const override { |
| 42 | switch (static_cast<RuntimeDyldErrorCode>(Condition)) { |
| 43 | case GenericRTDyldError: return "Generic RuntimeDyld error"; |
| 44 | } |
| 45 | llvm_unreachable("Unrecognized RuntimeDyldErrorCode"); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | static ManagedStatic<RuntimeDyldErrorCategory> RTDyldErrorCategory; |
| 50 | |
| 51 | } |
| 52 | |
| 53 | char RuntimeDyldError::ID = 0; |
| 54 | |
| 55 | void RuntimeDyldError::log(raw_ostream &OS) const { |
| 56 | OS << ErrMsg << "\n"; |
| 57 | } |
| 58 | |
| 59 | std::error_code RuntimeDyldError::convertToErrorCode() const { |
| 60 | return std::error_code(GenericRTDyldError, *RTDyldErrorCategory); |
| 61 | } |
| 62 | |
Chandler Carruth | 086f708 | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 63 | // Empty out-of-line virtual destructor as the key function. |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 64 | RuntimeDyldImpl::~RuntimeDyldImpl() {} |
Chandler Carruth | 086f708 | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 65 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 66 | // Pin LoadedObjectInfo's vtables to this file. |
| 67 | void RuntimeDyld::LoadedObjectInfo::anchor() {} |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 68 | |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 69 | namespace llvm { |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 70 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 71 | void RuntimeDyldImpl::registerEHFrames() {} |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 72 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 73 | void RuntimeDyldImpl::deregisterEHFrames() {} |
Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 74 | |
Benjamin Kramer | 01e1789 | 2014-08-26 14:22:05 +0000 | [diff] [blame] | 75 | #ifndef NDEBUG |
Lang Hames | f4b3b67 | 2014-08-25 22:19:14 +0000 | [diff] [blame] | 76 | static void dumpSectionMemory(const SectionEntry &S, StringRef State) { |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 77 | dbgs() << "----- Contents of section " << S.getName() << " " << State |
| 78 | << " -----"; |
Lang Hames | f4b3b67 | 2014-08-25 22:19:14 +0000 | [diff] [blame] | 79 | |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 80 | if (S.getAddress() == nullptr) { |
Lang Hames | 24f0c24 | 2014-10-01 21:57:47 +0000 | [diff] [blame] | 81 | dbgs() << "\n <section not emitted>\n"; |
| 82 | return; |
| 83 | } |
| 84 | |
Lang Hames | f4b3b67 | 2014-08-25 22:19:14 +0000 | [diff] [blame] | 85 | const unsigned ColsPerRow = 16; |
Lang Hames | 86b08f0 | 2014-08-25 18:37:38 +0000 | [diff] [blame] | 86 | |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 87 | uint8_t *DataAddr = S.getAddress(); |
| 88 | uint64_t LoadAddr = S.getLoadAddress(); |
Lang Hames | 86b08f0 | 2014-08-25 18:37:38 +0000 | [diff] [blame] | 89 | |
Lang Hames | 8d4d026 | 2014-09-18 16:43:24 +0000 | [diff] [blame] | 90 | unsigned StartPadding = LoadAddr & (ColsPerRow - 1); |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 91 | unsigned BytesRemaining = S.getSize(); |
Lang Hames | 86b08f0 | 2014-08-25 18:37:38 +0000 | [diff] [blame] | 92 | |
| 93 | if (StartPadding) { |
Alexei Starovoitov | 36df1ca | 2015-03-30 05:15:57 +0000 | [diff] [blame] | 94 | dbgs() << "\n" << format("0x%016" PRIx64, |
| 95 | LoadAddr & ~(uint64_t)(ColsPerRow - 1)) << ":"; |
Lang Hames | 86b08f0 | 2014-08-25 18:37:38 +0000 | [diff] [blame] | 96 | while (StartPadding--) |
| 97 | dbgs() << " "; |
| 98 | } |
| 99 | |
| 100 | while (BytesRemaining > 0) { |
Lang Hames | f4b3b67 | 2014-08-25 22:19:14 +0000 | [diff] [blame] | 101 | if ((LoadAddr & (ColsPerRow - 1)) == 0) |
Lang Hames | c5cafbb | 2014-08-28 04:25:17 +0000 | [diff] [blame] | 102 | dbgs() << "\n" << format("0x%016" PRIx64, LoadAddr) << ":"; |
Lang Hames | 86b08f0 | 2014-08-25 18:37:38 +0000 | [diff] [blame] | 103 | |
| 104 | dbgs() << " " << format("%02x", *DataAddr); |
| 105 | |
| 106 | ++DataAddr; |
| 107 | ++LoadAddr; |
| 108 | --BytesRemaining; |
| 109 | } |
| 110 | |
| 111 | dbgs() << "\n"; |
| 112 | } |
Benjamin Kramer | 01e1789 | 2014-08-26 14:22:05 +0000 | [diff] [blame] | 113 | #endif |
Lang Hames | 86b08f0 | 2014-08-25 18:37:38 +0000 | [diff] [blame] | 114 | |
Jim Grosbach | 733d305 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 115 | // Resolve the relocations for all symbols we currently know about. |
| 116 | void RuntimeDyldImpl::resolveRelocations() { |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 117 | MutexGuard locked(lock); |
| 118 | |
Lang Hames | 79fce47 | 2015-09-10 20:44:36 +0000 | [diff] [blame] | 119 | // Print out the sections prior to relocation. |
| 120 | DEBUG( |
| 121 | for (int i = 0, e = Sections.size(); i != e; ++i) |
| 122 | dumpSectionMemory(Sections[i], "before relocations"); |
| 123 | ); |
| 124 | |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 125 | // First, resolve relocations associated with external symbols. |
Eli Bendersky | b92e1cf | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 126 | resolveExternalSymbols(); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 127 | |
Keno Fischer | 2cd66e9 | 2015-10-10 05:37:02 +0000 | [diff] [blame] | 128 | // Iterate over all outstanding relocations |
| 129 | for (auto it = Relocations.begin(), e = Relocations.end(); it != e; ++it) { |
Andrew Kaylor | 5f3a998 | 2013-08-19 19:38:06 +0000 | [diff] [blame] | 130 | // The Section here (Sections[i]) refers to the section in which the |
| 131 | // symbol for the relocation is located. The SectionID in the relocation |
| 132 | // entry provides the section to which the relocation will be applied. |
Keno Fischer | eb59d46 | 2015-12-03 21:27:59 +0000 | [diff] [blame] | 133 | int Idx = it->first; |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 134 | uint64_t Addr = Sections[Idx].getLoadAddress(); |
Keno Fischer | 2cd66e9 | 2015-10-10 05:37:02 +0000 | [diff] [blame] | 135 | DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t" |
Alexei Starovoitov | 36df1ca | 2015-03-30 05:15:57 +0000 | [diff] [blame] | 136 | << format("%p", (uintptr_t)Addr) << "\n"); |
Keno Fischer | eb59d46 | 2015-12-03 21:27:59 +0000 | [diff] [blame] | 137 | resolveRelocationList(it->second, Addr); |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 138 | } |
Keno Fischer | 2cd66e9 | 2015-10-10 05:37:02 +0000 | [diff] [blame] | 139 | Relocations.clear(); |
Lang Hames | 79fce47 | 2015-09-10 20:44:36 +0000 | [diff] [blame] | 140 | |
| 141 | // Print out sections after relocation. |
| 142 | DEBUG( |
| 143 | for (int i = 0, e = Sections.size(); i != e; ++i) |
| 144 | dumpSectionMemory(Sections[i], "after relocations"); |
| 145 | ); |
| 146 | |
Jim Grosbach | 733d305 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Jim Grosbach | 6d61397 | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 149 | void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress, |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 150 | uint64_t TargetAddress) { |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 151 | MutexGuard locked(lock); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 152 | for (unsigned i = 0, e = Sections.size(); i != e; ++i) { |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 153 | if (Sections[i].getAddress() == LocalAddress) { |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 154 | reassignSectionAddress(i, TargetAddress); |
| 155 | return; |
| 156 | } |
| 157 | } |
| 158 | llvm_unreachable("Attempting to remap address of unknown section!"); |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 161 | static Error getOffset(const SymbolRef &Sym, SectionRef Sec, |
| 162 | uint64_t &Result) { |
Rafael Espindola | ed067c4 | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 163 | ErrorOr<uint64_t> AddressOrErr = Sym.getAddress(); |
| 164 | if (std::error_code EC = AddressOrErr.getError()) |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 165 | return errorCodeToError(EC); |
Rafael Espindola | ef888a4 | 2015-07-07 16:45:55 +0000 | [diff] [blame] | 166 | Result = *AddressOrErr - Sec.getAddress(); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 167 | return Error::success(); |
Rafael Espindola | 6956b1a | 2014-04-21 13:45:32 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 170 | Expected<RuntimeDyldImpl::ObjSectionToIDMap> |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 171 | RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 172 | MutexGuard locked(lock); |
| 173 | |
Andrew Kaylor | 33c5b1b | 2013-10-15 20:44:55 +0000 | [diff] [blame] | 174 | // Save information about our target |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 175 | Arch = (Triple::ArchType)Obj.getArch(); |
| 176 | IsTargetLittleEndian = Obj.isLittleEndian(); |
Petar Jovanovic | 9720283 | 2015-05-28 13:48:41 +0000 | [diff] [blame] | 177 | setMipsABI(Obj); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 178 | |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 179 | // Compute the memory size required to load all sections to be loaded |
| 180 | // and pass this information to the memory manager |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 181 | if (MemMgr.needsToReserveAllocationSpace()) { |
Lang Hames | b2b7a3c | 2016-01-10 18:51:50 +0000 | [diff] [blame] | 182 | uint64_t CodeSize = 0, RODataSize = 0, RWDataSize = 0; |
| 183 | uint32_t CodeAlign = 1, RODataAlign = 1, RWDataAlign = 1; |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 184 | if (auto Err = computeTotalAllocSize(Obj, |
| 185 | CodeSize, CodeAlign, |
| 186 | RODataSize, RODataAlign, |
| 187 | RWDataSize, RWDataAlign)) |
| 188 | return std::move(Err); |
Lang Hames | b2b7a3c | 2016-01-10 18:51:50 +0000 | [diff] [blame] | 189 | MemMgr.reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign, |
| 190 | RWDataSize, RWDataAlign); |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 191 | } |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 192 | |
Eli Bendersky | fc07908 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 193 | // Used sections from the object file |
| 194 | ObjSectionToIDMap LocalSections; |
| 195 | |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 196 | // Common symbols requiring allocation, with their sizes and alignments |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 197 | CommonSymbolList CommonSymbols; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 198 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 199 | // Parse symbols |
| 200 | DEBUG(dbgs() << "Parse symbols:\n"); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 201 | for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E; |
Jim Grosbach | 36f025e | 2014-04-21 19:23:59 +0000 | [diff] [blame] | 202 | ++I) { |
Jim Grosbach | 36f025e | 2014-04-21 19:23:59 +0000 | [diff] [blame] | 203 | uint32_t Flags = I->getFlags(); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 204 | |
Lang Hames | a32d71b | 2015-10-18 01:41:37 +0000 | [diff] [blame] | 205 | if (Flags & SymbolRef::SF_Common) |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 206 | CommonSymbols.push_back(*I); |
| 207 | else { |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 208 | |
| 209 | // Get the symbol type. |
| 210 | object::SymbolRef::Type SymType; |
| 211 | if (auto SymTypeOrErr = I->getType()) |
| 212 | SymType = *SymTypeOrErr; |
| 213 | else |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame^] | 214 | return SymTypeOrErr.takeError(); |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 215 | |
Lang Hames | a32d71b | 2015-10-18 01:41:37 +0000 | [diff] [blame] | 216 | // Get symbol name. |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 217 | StringRef Name; |
| 218 | if (auto NameOrErr = I->getName()) |
| 219 | Name = *NameOrErr; |
| 220 | else |
| 221 | return NameOrErr.takeError(); |
| 222 | |
Lang Hames | a32d71b | 2015-10-18 01:41:37 +0000 | [diff] [blame] | 223 | // Compute JIT symbol flags. |
| 224 | JITSymbolFlags RTDyldSymFlags = JITSymbolFlags::None; |
| 225 | if (Flags & SymbolRef::SF_Weak) |
| 226 | RTDyldSymFlags |= JITSymbolFlags::Weak; |
| 227 | if (Flags & SymbolRef::SF_Exported) |
| 228 | RTDyldSymFlags |= JITSymbolFlags::Exported; |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 229 | |
Keno Fischer | ddad187 | 2015-10-21 20:22:04 +0000 | [diff] [blame] | 230 | if (Flags & SymbolRef::SF_Absolute && |
| 231 | SymType != object::SymbolRef::ST_File) { |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 232 | uint64_t Addr = 0; |
| 233 | if (auto AddrOrErr = I->getAddress()) |
| 234 | Addr = *AddrOrErr; |
| 235 | else |
| 236 | return errorCodeToError(AddrOrErr.getError()); |
| 237 | |
Lang Hames | a32d71b | 2015-10-18 01:41:37 +0000 | [diff] [blame] | 238 | unsigned SectionID = AbsoluteSymbolSection; |
| 239 | |
| 240 | DEBUG(dbgs() << "\tType: " << SymType << " (absolute) Name: " << Name |
| 241 | << " SID: " << SectionID << " Offset: " |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 242 | << format("%p", (uintptr_t)Addr) |
Lang Hames | a32d71b | 2015-10-18 01:41:37 +0000 | [diff] [blame] | 243 | << " flags: " << Flags << "\n"); |
| 244 | GlobalSymbolTable[Name] = |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 245 | SymbolTableEntry(SectionID, Addr, RTDyldSymFlags); |
Lang Hames | a32d71b | 2015-10-18 01:41:37 +0000 | [diff] [blame] | 246 | } else if (SymType == object::SymbolRef::ST_Function || |
| 247 | SymType == object::SymbolRef::ST_Data || |
| 248 | SymType == object::SymbolRef::ST_Unknown || |
| 249 | SymType == object::SymbolRef::ST_Other) { |
| 250 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 251 | section_iterator SI = Obj.section_end(); |
| 252 | if (auto SIOrErr = I->getSection()) |
| 253 | SI = *SIOrErr; |
| 254 | else |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame^] | 255 | return SIOrErr.takeError(); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 256 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 257 | if (SI == Obj.section_end()) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 258 | continue; |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 259 | |
Lang Hames | a32d71b | 2015-10-18 01:41:37 +0000 | [diff] [blame] | 260 | // Get symbol offset. |
Rafael Espindola | ef888a4 | 2015-07-07 16:45:55 +0000 | [diff] [blame] | 261 | uint64_t SectOffset; |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 262 | if (auto Err = getOffset(*I, *SI, SectOffset)) |
| 263 | return std::move(Err); |
| 264 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 265 | bool IsCode = SI->isText(); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 266 | unsigned SectionID; |
| 267 | if (auto SectionIDOrErr = findOrEmitSection(Obj, *SI, IsCode, |
| 268 | LocalSections)) |
| 269 | SectionID = *SectionIDOrErr; |
| 270 | else |
| 271 | return SectionIDOrErr.takeError(); |
Lang Hames | a32d71b | 2015-10-18 01:41:37 +0000 | [diff] [blame] | 272 | |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 273 | DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name |
| 274 | << " SID: " << SectionID << " Offset: " |
| 275 | << format("%p", (uintptr_t)SectOffset) |
| 276 | << " flags: " << Flags << "\n"); |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 277 | GlobalSymbolTable[Name] = |
| 278 | SymbolTableEntry(SectionID, SectOffset, RTDyldSymFlags); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 279 | } |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 280 | } |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 283 | // Allocate common symbols |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 284 | if (auto Err = emitCommonSymbols(Obj, CommonSymbols)) |
| 285 | return std::move(Err); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 286 | |
Eli Bendersky | 0e2ac5b | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 287 | // Parse and process relocations |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 288 | DEBUG(dbgs() << "Parse relocations:\n"); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 289 | for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); |
Jim Grosbach | 36f025e | 2014-04-21 19:23:59 +0000 | [diff] [blame] | 290 | SI != SE; ++SI) { |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 291 | StubMap Stubs; |
Jim Grosbach | 36f025e | 2014-04-21 19:23:59 +0000 | [diff] [blame] | 292 | section_iterator RelocatedSection = SI->getRelocatedSection(); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 293 | |
Rafael Espindola | 127b6c3 | 2015-02-17 20:07:28 +0000 | [diff] [blame] | 294 | if (RelocatedSection == SE) |
| 295 | continue; |
| 296 | |
Jim Grosbach | 36f025e | 2014-04-21 19:23:59 +0000 | [diff] [blame] | 297 | relocation_iterator I = SI->relocation_begin(); |
| 298 | relocation_iterator E = SI->relocation_end(); |
Rafael Espindola | 77314aa | 2014-04-03 22:42:22 +0000 | [diff] [blame] | 299 | |
| 300 | if (I == E && !ProcessAllSections) |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 301 | continue; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 302 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 303 | bool IsCode = RelocatedSection->isText(); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 304 | unsigned SectionID = 0; |
| 305 | if (auto SectionIDOrErr = findOrEmitSection(Obj, *RelocatedSection, IsCode, |
| 306 | LocalSections)) |
| 307 | SectionID = *SectionIDOrErr; |
| 308 | else |
| 309 | return SectionIDOrErr.takeError(); |
| 310 | |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 311 | DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n"); |
| 312 | |
Rafael Espindola | 77314aa | 2014-04-03 22:42:22 +0000 | [diff] [blame] | 313 | for (; I != E;) |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 314 | if (auto IOrErr = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs)) |
| 315 | I = *IOrErr; |
| 316 | else |
| 317 | return IOrErr.takeError(); |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 318 | |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 319 | // If there is an attached checker, notify it about the stubs for this |
| 320 | // section so that they can be verified. |
| 321 | if (Checker) |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 322 | Checker->registerStubMap(Obj.getFileName(), SectionID, Stubs); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 323 | } |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 324 | |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 325 | // Give the subclasses a chance to tie-up any loose ends. |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 326 | if (auto Err = finalizeLoad(Obj, LocalSections)) |
| 327 | return std::move(Err); |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 328 | |
Lang Hames | 2e88f4f | 2015-07-28 17:52:11 +0000 | [diff] [blame] | 329 | // for (auto E : LocalSections) |
| 330 | // llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n"; |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 331 | |
Lang Hames | 2e88f4f | 2015-07-28 17:52:11 +0000 | [diff] [blame] | 332 | return LocalSections; |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // A helper method for computeTotalAllocSize. |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 336 | // Computes the memory size required to allocate sections with the given sizes, |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 337 | // assuming that all sections are allocated with the given alignment |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 338 | static uint64_t |
| 339 | computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes, |
| 340 | uint64_t Alignment) { |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 341 | uint64_t TotalSize = 0; |
Jim Grosbach | 36f025e | 2014-04-21 19:23:59 +0000 | [diff] [blame] | 342 | for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) { |
| 343 | uint64_t AlignedSize = |
| 344 | (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment; |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 345 | TotalSize += AlignedSize; |
| 346 | } |
| 347 | return TotalSize; |
| 348 | } |
| 349 | |
Rafael Espindola | edd5f84 | 2015-06-26 12:44:10 +0000 | [diff] [blame] | 350 | static bool isRequiredForExecution(const SectionRef Section) { |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 351 | const ObjectFile *Obj = Section.getObject(); |
Rafael Espindola | edd5f84 | 2015-06-26 12:44:10 +0000 | [diff] [blame] | 352 | if (isa<object::ELFObjectFileBase>(Obj)) |
| 353 | return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC; |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 354 | if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) { |
| 355 | const coff_section *CoffSection = COFFObj->getCOFFSection(Section); |
| 356 | // Avoid loading zero-sized COFF sections. |
| 357 | // In PE files, VirtualSize gives the section size, and SizeOfRawData |
| 358 | // may be zero for sections with content. In Obj files, SizeOfRawData |
| 359 | // gives the section size, and VirtualSize is always zero. Hence |
| 360 | // the need to check for both cases below. |
| 361 | bool HasContent = (CoffSection->VirtualSize > 0) |
| 362 | || (CoffSection->SizeOfRawData > 0); |
| 363 | bool IsDiscardable = CoffSection->Characteristics & |
| 364 | (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_LNK_INFO); |
| 365 | return HasContent && !IsDiscardable; |
| 366 | } |
| 367 | |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 368 | assert(isa<MachOObjectFile>(Obj)); |
| 369 | return true; |
Rafael Espindola | edd5f84 | 2015-06-26 12:44:10 +0000 | [diff] [blame] | 370 | } |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 371 | |
Rafael Espindola | edd5f84 | 2015-06-26 12:44:10 +0000 | [diff] [blame] | 372 | static bool isReadOnlyData(const SectionRef Section) { |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 373 | const ObjectFile *Obj = Section.getObject(); |
Rafael Espindola | edd5f84 | 2015-06-26 12:44:10 +0000 | [diff] [blame] | 374 | if (isa<object::ELFObjectFileBase>(Obj)) |
| 375 | return !(ELFSectionRef(Section).getFlags() & |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 376 | (ELF::SHF_WRITE | ELF::SHF_EXECINSTR)); |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 377 | if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) |
| 378 | return ((COFFObj->getCOFFSection(Section)->Characteristics & |
| 379 | (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
| 380 | | COFF::IMAGE_SCN_MEM_READ |
| 381 | | COFF::IMAGE_SCN_MEM_WRITE)) |
| 382 | == |
| 383 | (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
| 384 | | COFF::IMAGE_SCN_MEM_READ)); |
| 385 | |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 386 | assert(isa<MachOObjectFile>(Obj)); |
| 387 | return false; |
| 388 | } |
| 389 | |
Rafael Espindola | 41401e9 | 2015-06-26 12:33:37 +0000 | [diff] [blame] | 390 | static bool isZeroInit(const SectionRef Section) { |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 391 | const ObjectFile *Obj = Section.getObject(); |
Rafael Espindola | 41401e9 | 2015-06-26 12:33:37 +0000 | [diff] [blame] | 392 | if (isa<object::ELFObjectFileBase>(Obj)) |
| 393 | return ELFSectionRef(Section).getType() == ELF::SHT_NOBITS; |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 394 | if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) |
| 395 | return COFFObj->getCOFFSection(Section)->Characteristics & |
| 396 | COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 397 | |
| 398 | auto *MachO = cast<MachOObjectFile>(Obj); |
| 399 | unsigned SectionType = MachO->getSectionType(Section); |
| 400 | return SectionType == MachO::S_ZEROFILL || |
| 401 | SectionType == MachO::S_GB_ZEROFILL; |
| 402 | } |
| 403 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 404 | // Compute an upper bound of the memory size that is required to load all |
| 405 | // sections |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 406 | Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj, |
| 407 | uint64_t &CodeSize, |
| 408 | uint32_t &CodeAlign, |
| 409 | uint64_t &RODataSize, |
| 410 | uint32_t &RODataAlign, |
| 411 | uint64_t &RWDataSize, |
| 412 | uint32_t &RWDataAlign) { |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 413 | // Compute the size of all sections required for execution |
| 414 | std::vector<uint64_t> CodeSectionSizes; |
| 415 | std::vector<uint64_t> ROSectionSizes; |
| 416 | std::vector<uint64_t> RWSectionSizes; |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 417 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 418 | // Collect sizes of all sections to be loaded; |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 419 | // also determine the max alignment of all sections |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 420 | for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); |
Jim Grosbach | 36f025e | 2014-04-21 19:23:59 +0000 | [diff] [blame] | 421 | SI != SE; ++SI) { |
| 422 | const SectionRef &Section = *SI; |
| 423 | |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 424 | bool IsRequired = isRequiredForExecution(Section); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 425 | |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 426 | // Consider only the sections that are required to be loaded for execution |
| 427 | if (IsRequired) { |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 428 | uint64_t DataSize = Section.getSize(); |
| 429 | uint64_t Alignment64 = Section.getAlignment(); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 430 | unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 431 | bool IsCode = Section.isText(); |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 432 | bool IsReadOnly = isReadOnlyData(Section); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 433 | |
| 434 | StringRef Name; |
| 435 | if (auto EC = Section.getName(Name)) |
| 436 | return errorCodeToError(EC); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 437 | |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 438 | uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section); |
| 439 | uint64_t SectionSize = DataSize + StubBufSize; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 440 | |
| 441 | // The .eh_frame section (at least on Linux) needs an extra four bytes |
| 442 | // padded |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 443 | // with zeroes added at the end. For MachO objects, this section has a |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 444 | // slightly different name, so this won't have any effect for MachO |
| 445 | // objects. |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 446 | if (Name == ".eh_frame") |
| 447 | SectionSize += 4; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 448 | |
Lang Hames | 2be1bbe | 2015-04-07 06:27:56 +0000 | [diff] [blame] | 449 | if (!SectionSize) |
| 450 | SectionSize = 1; |
| 451 | |
| 452 | if (IsCode) { |
Lang Hames | b2b7a3c | 2016-01-10 18:51:50 +0000 | [diff] [blame] | 453 | CodeAlign = std::max(CodeAlign, Alignment); |
Lang Hames | 2be1bbe | 2015-04-07 06:27:56 +0000 | [diff] [blame] | 454 | CodeSectionSizes.push_back(SectionSize); |
| 455 | } else if (IsReadOnly) { |
Lang Hames | b2b7a3c | 2016-01-10 18:51:50 +0000 | [diff] [blame] | 456 | RODataAlign = std::max(RODataAlign, Alignment); |
Lang Hames | 2be1bbe | 2015-04-07 06:27:56 +0000 | [diff] [blame] | 457 | ROSectionSizes.push_back(SectionSize); |
| 458 | } else { |
Lang Hames | b2b7a3c | 2016-01-10 18:51:50 +0000 | [diff] [blame] | 459 | RWDataAlign = std::max(RWDataAlign, Alignment); |
Lang Hames | 2be1bbe | 2015-04-07 06:27:56 +0000 | [diff] [blame] | 460 | RWSectionSizes.push_back(SectionSize); |
| 461 | } |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | // Compute the size of all common symbols |
| 466 | uint64_t CommonSize = 0; |
Lang Hames | 543e0dc | 2016-04-21 20:08:06 +0000 | [diff] [blame] | 467 | uint32_t CommonAlign = 1; |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 468 | for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 469 | ++I) { |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 470 | uint32_t Flags = I->getFlags(); |
| 471 | if (Flags & SymbolRef::SF_Common) { |
| 472 | // Add the common symbols to a list. We'll allocate them all below. |
Rafael Espindola | d7a32ea | 2015-06-24 10:20:30 +0000 | [diff] [blame] | 473 | uint64_t Size = I->getCommonSize(); |
Lang Hames | 543e0dc | 2016-04-21 20:08:06 +0000 | [diff] [blame] | 474 | uint32_t Align = I->getAlignment(); |
| 475 | // If this is the first common symbol, use its alignment as the alignment |
| 476 | // for the common symbols section. |
| 477 | if (CommonSize == 0) |
| 478 | CommonAlign = Align; |
| 479 | CommonSize = alignTo(CommonSize, Align) + Size; |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | if (CommonSize != 0) { |
| 483 | RWSectionSizes.push_back(CommonSize); |
Lang Hames | 543e0dc | 2016-04-21 20:08:06 +0000 | [diff] [blame] | 484 | RWDataAlign = std::max(RWDataAlign, CommonAlign); |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 487 | // Compute the required allocation space for each different type of sections |
| 488 | // (code, read-only data, read-write data) assuming that all sections are |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 489 | // allocated with the max alignment. Note that we cannot compute with the |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 490 | // individual alignments of the sections, because then the required size |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 491 | // depends on the order, in which the sections are allocated. |
Lang Hames | b2b7a3c | 2016-01-10 18:51:50 +0000 | [diff] [blame] | 492 | CodeSize = computeAllocationSizeForSections(CodeSectionSizes, CodeAlign); |
| 493 | RODataSize = computeAllocationSizeForSections(ROSectionSizes, RODataAlign); |
| 494 | RWDataSize = computeAllocationSizeForSections(RWSectionSizes, RWDataAlign); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 495 | |
| 496 | return Error::success(); |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // compute stub buffer size for the given section |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 500 | unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj, |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 501 | const SectionRef &Section) { |
| 502 | unsigned StubSize = getMaxStubSize(); |
| 503 | if (StubSize == 0) { |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 504 | return 0; |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 505 | } |
| 506 | // FIXME: this is an inefficient way to handle this. We should computed the |
| 507 | // necessary section allocation size in loadObject by walking all the sections |
| 508 | // once. |
| 509 | unsigned StubBufSize = 0; |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 510 | for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); |
Jim Grosbach | 36f025e | 2014-04-21 19:23:59 +0000 | [diff] [blame] | 511 | SI != SE; ++SI) { |
| 512 | section_iterator RelSecI = SI->getRelocatedSection(); |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 513 | if (!(RelSecI == Section)) |
| 514 | continue; |
| 515 | |
Sanjoy Das | d5658b0 | 2015-11-23 21:47:51 +0000 | [diff] [blame] | 516 | for (const RelocationRef &Reloc : SI->relocations()) |
| 517 | if (relocationNeedsStub(Reloc)) |
| 518 | StubBufSize += StubSize; |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 519 | } |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 520 | |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 521 | // Get section data size and alignment |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 522 | uint64_t DataSize = Section.getSize(); |
| 523 | uint64_t Alignment64 = Section.getAlignment(); |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 524 | |
| 525 | // Add stubbuf size alignment |
| 526 | unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; |
| 527 | unsigned StubAlignment = getStubAlignment(); |
| 528 | unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment); |
| 529 | if (StubAlignment > EndAlignment) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 530 | StubBufSize += StubAlignment - EndAlignment; |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 531 | return StubBufSize; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Lang Hames | e1287c0 | 2014-08-29 23:17:47 +0000 | [diff] [blame] | 534 | uint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src, |
| 535 | unsigned Size) const { |
| 536 | uint64_t Result = 0; |
Lang Hames | 69abd72 | 2014-09-07 02:05:26 +0000 | [diff] [blame] | 537 | if (IsTargetLittleEndian) { |
| 538 | Src += Size - 1; |
| 539 | while (Size--) |
| 540 | Result = (Result << 8) | *Src--; |
| 541 | } else |
| 542 | while (Size--) |
| 543 | Result = (Result << 8) | *Src++; |
Lang Hames | e1287c0 | 2014-08-29 23:17:47 +0000 | [diff] [blame] | 544 | |
| 545 | return Result; |
| 546 | } |
| 547 | |
| 548 | void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst, |
| 549 | unsigned Size) const { |
Lang Hames | 69abd72 | 2014-09-07 02:05:26 +0000 | [diff] [blame] | 550 | if (IsTargetLittleEndian) { |
| 551 | while (Size--) { |
| 552 | *Dst++ = Value & 0xFF; |
| 553 | Value >>= 8; |
| 554 | } |
Lang Hames | e1287c0 | 2014-08-29 23:17:47 +0000 | [diff] [blame] | 555 | } else { |
Lang Hames | 69abd72 | 2014-09-07 02:05:26 +0000 | [diff] [blame] | 556 | Dst += Size - 1; |
| 557 | while (Size--) { |
| 558 | *Dst-- = Value & 0xFF; |
| 559 | Value >>= 8; |
| 560 | } |
Lang Hames | e1287c0 | 2014-08-29 23:17:47 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 564 | Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj, |
| 565 | CommonSymbolList &CommonSymbols) { |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 566 | if (CommonSymbols.empty()) |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 567 | return Error::success(); |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 568 | |
| 569 | uint64_t CommonSize = 0; |
Lang Hames | 543e0dc | 2016-04-21 20:08:06 +0000 | [diff] [blame] | 570 | uint32_t CommonAlign = CommonSymbols.begin()->getAlignment(); |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 571 | CommonSymbolList SymbolsToAllocate; |
| 572 | |
| 573 | DEBUG(dbgs() << "Processing common symbols...\n"); |
| 574 | |
| 575 | for (const auto &Sym : CommonSymbols) { |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 576 | StringRef Name; |
| 577 | if (auto NameOrErr = Sym.getName()) |
| 578 | Name = *NameOrErr; |
| 579 | else |
| 580 | return NameOrErr.takeError(); |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 581 | |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 582 | // Skip common symbols already elsewhere. |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 583 | if (GlobalSymbolTable.count(Name) || |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 584 | Resolver.findSymbolInLogicalDylib(Name)) { |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 585 | DEBUG(dbgs() << "\tSkipping already emitted common symbol '" << Name |
| 586 | << "'\n"); |
| 587 | continue; |
| 588 | } |
| 589 | |
Rafael Espindola | a4d22472 | 2015-05-31 23:52:50 +0000 | [diff] [blame] | 590 | uint32_t Align = Sym.getAlignment(); |
Rafael Espindola | d7a32ea | 2015-06-24 10:20:30 +0000 | [diff] [blame] | 591 | uint64_t Size = Sym.getCommonSize(); |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 592 | |
Lang Hames | 543e0dc | 2016-04-21 20:08:06 +0000 | [diff] [blame] | 593 | CommonSize = alignTo(CommonSize, Align) + Size; |
| 594 | |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 595 | SymbolsToAllocate.push_back(Sym); |
| 596 | } |
| 597 | |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 598 | // Allocate memory for the section |
| 599 | unsigned SectionID = Sections.size(); |
Lang Hames | 543e0dc | 2016-04-21 20:08:06 +0000 | [diff] [blame] | 600 | uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign, |
| 601 | SectionID, "<common symbols>", |
| 602 | false); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 603 | if (!Addr) |
| 604 | report_fatal_error("Unable to allocate memory for common symbols!"); |
| 605 | uint64_t Offset = 0; |
Sanjoy Das | 8082592 | 2015-11-23 21:47:46 +0000 | [diff] [blame] | 606 | Sections.push_back( |
| 607 | SectionEntry("<common symbols>", Addr, CommonSize, CommonSize, 0)); |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 608 | memset(Addr, 0, CommonSize); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 609 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 610 | DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: " |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 611 | << format("%p", Addr) << " DataSize: " << CommonSize << "\n"); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 612 | |
| 613 | // Assign the address of each symbol |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 614 | for (auto &Sym : SymbolsToAllocate) { |
Rafael Espindola | a4d22472 | 2015-05-31 23:52:50 +0000 | [diff] [blame] | 615 | uint32_t Align = Sym.getAlignment(); |
Rafael Espindola | d7a32ea | 2015-06-24 10:20:30 +0000 | [diff] [blame] | 616 | uint64_t Size = Sym.getCommonSize(); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 617 | StringRef Name; |
| 618 | if (auto NameOrErr = Sym.getName()) |
| 619 | Name = *NameOrErr; |
| 620 | else |
| 621 | return NameOrErr.takeError(); |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 622 | if (Align) { |
| 623 | // This symbol has an alignment requirement. |
| 624 | uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align); |
| 625 | Addr += AlignOffset; |
| 626 | Offset += AlignOffset; |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 627 | } |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 628 | uint32_t Flags = Sym.getFlags(); |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 629 | JITSymbolFlags RTDyldSymFlags = JITSymbolFlags::None; |
| 630 | if (Flags & SymbolRef::SF_Weak) |
| 631 | RTDyldSymFlags |= JITSymbolFlags::Weak; |
| 632 | if (Flags & SymbolRef::SF_Exported) |
| 633 | RTDyldSymFlags |= JITSymbolFlags::Exported; |
Lang Hames | 2996895 | 2015-01-17 00:55:05 +0000 | [diff] [blame] | 634 | DEBUG(dbgs() << "Allocating common symbol " << Name << " address " |
| 635 | << format("%p", Addr) << "\n"); |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 636 | GlobalSymbolTable[Name] = |
| 637 | SymbolTableEntry(SectionID, Offset, RTDyldSymFlags); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 638 | Offset += Size; |
| 639 | Addr += Size; |
| 640 | } |
Petar Jovanovic | d22164d | 2015-08-13 15:12:49 +0000 | [diff] [blame] | 641 | |
| 642 | if (Checker) |
| 643 | Checker->registerSection(Obj.getFileName(), SectionID); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 644 | |
| 645 | return Error::success(); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 648 | Expected<unsigned> |
| 649 | RuntimeDyldImpl::emitSection(const ObjectFile &Obj, |
| 650 | const SectionRef &Section, |
| 651 | bool IsCode) { |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 652 | StringRef data; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 653 | uint64_t Alignment64 = Section.getAlignment(); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 654 | |
| 655 | unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; |
Andrew Kaylor | 877b931 | 2013-10-16 00:32:24 +0000 | [diff] [blame] | 656 | unsigned PaddingSize = 0; |
Lang Hames | 937ec54 | 2014-02-12 21:30:07 +0000 | [diff] [blame] | 657 | unsigned StubBufSize = 0; |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 658 | bool IsRequired = isRequiredForExecution(Section); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 659 | bool IsVirtual = Section.isVirtual(); |
Rafael Espindola | 0e77a94 | 2014-12-10 20:46:55 +0000 | [diff] [blame] | 660 | bool IsZeroInit = isZeroInit(Section); |
| 661 | bool IsReadOnly = isReadOnlyData(Section); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 662 | uint64_t DataSize = Section.getSize(); |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 663 | |
| 664 | StringRef Name; |
| 665 | if (auto EC = Section.getName(Name)) |
| 666 | return errorCodeToError(EC); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 667 | |
| 668 | StubBufSize = computeSectionStubBufSize(Obj, Section); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 669 | |
Andrew Kaylor | 877b931 | 2013-10-16 00:32:24 +0000 | [diff] [blame] | 670 | // The .eh_frame section (at least on Linux) needs an extra four bytes padded |
| 671 | // with zeroes added at the end. For MachO objects, this section has a |
| 672 | // slightly different name, so this won't have any effect for MachO objects. |
| 673 | if (Name == ".eh_frame") |
| 674 | PaddingSize = 4; |
| 675 | |
Lang Hames | d410017 | 2014-02-11 05:28:24 +0000 | [diff] [blame] | 676 | uintptr_t Allocate; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 677 | unsigned SectionID = Sections.size(); |
| 678 | uint8_t *Addr; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 679 | const char *pData = nullptr; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 680 | |
Lang Hames | 86a4593 | 2015-10-15 06:41:45 +0000 | [diff] [blame] | 681 | // If this section contains any bits (i.e. isn't a virtual or bss section), |
| 682 | // grab a reference to them. |
| 683 | if (!IsVirtual && !IsZeroInit) { |
| 684 | // In either case, set the location of the unrelocated section in memory, |
| 685 | // since we still process relocations for it even if we're not applying them. |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 686 | if (auto EC = Section.getContents(data)) |
| 687 | return errorCodeToError(EC); |
Keno Fischer | e6892c8 | 2015-05-01 20:21:45 +0000 | [diff] [blame] | 688 | pData = data.data(); |
Lang Hames | 86a4593 | 2015-10-15 06:41:45 +0000 | [diff] [blame] | 689 | } |
Keno Fischer | e6892c8 | 2015-05-01 20:21:45 +0000 | [diff] [blame] | 690 | |
Lang Hames | 7e66c6c | 2015-08-14 06:26:42 +0000 | [diff] [blame] | 691 | // Code section alignment needs to be at least as high as stub alignment or |
| 692 | // padding calculations may by incorrect when the section is remapped to a |
| 693 | // higher alignment. |
| 694 | if (IsCode) |
| 695 | Alignment = std::max(Alignment, getStubAlignment()); |
| 696 | |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 697 | // Some sections, such as debug info, don't need to be loaded for execution. |
| 698 | // Leave those where they are. |
| 699 | if (IsRequired) { |
Andrew Kaylor | 877b931 | 2013-10-16 00:32:24 +0000 | [diff] [blame] | 700 | Allocate = DataSize + PaddingSize + StubBufSize; |
Lang Hames | 2be1bbe | 2015-04-07 06:27:56 +0000 | [diff] [blame] | 701 | if (!Allocate) |
| 702 | Allocate = 1; |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 703 | Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID, |
| 704 | Name) |
| 705 | : MemMgr.allocateDataSection(Allocate, Alignment, SectionID, |
| 706 | Name, IsReadOnly); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 707 | if (!Addr) |
| 708 | report_fatal_error("Unable to allocate section memory!"); |
| 709 | |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 710 | // Zero-initialize or copy the data from the image |
| 711 | if (IsZeroInit || IsVirtual) |
| 712 | memset(Addr, 0, DataSize); |
| 713 | else |
| 714 | memcpy(Addr, pData, DataSize); |
| 715 | |
Andrew Kaylor | 877b931 | 2013-10-16 00:32:24 +0000 | [diff] [blame] | 716 | // Fill in any extra bytes we allocated for padding |
| 717 | if (PaddingSize != 0) { |
| 718 | memset(Addr + DataSize, 0, PaddingSize); |
| 719 | // Update the DataSize variable so that the stub offset is set correctly. |
| 720 | DataSize += PaddingSize; |
| 721 | } |
| 722 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 723 | DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 724 | << " obj addr: " << format("%p", pData) |
| 725 | << " new addr: " << format("%p", Addr) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 726 | << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize |
| 727 | << " Allocate: " << Allocate << "\n"); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 728 | } else { |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 729 | // Even if we didn't load the section, we need to record an entry for it |
Eli Bendersky | 0e2ac5b | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 730 | // to handle later processing (and by 'handle' I mean don't do anything |
| 731 | // with these sections). |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 732 | Allocate = 0; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 733 | Addr = nullptr; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 734 | DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name |
| 735 | << " obj addr: " << format("%p", data.data()) << " new addr: 0" |
| 736 | << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize |
| 737 | << " Allocate: " << Allocate << "\n"); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Sanjoy Das | 8082592 | 2015-11-23 21:47:46 +0000 | [diff] [blame] | 740 | Sections.push_back( |
| 741 | SectionEntry(Name, Addr, DataSize, Allocate, (uintptr_t)pData)); |
Lang Hames | 587ee6a | 2014-09-03 05:01:46 +0000 | [diff] [blame] | 742 | |
| 743 | if (Checker) |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 744 | Checker->registerSection(Obj.getFileName(), SectionID); |
Lang Hames | 587ee6a | 2014-09-03 05:01:46 +0000 | [diff] [blame] | 745 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 746 | return SectionID; |
| 747 | } |
| 748 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 749 | Expected<unsigned> |
| 750 | RuntimeDyldImpl::findOrEmitSection(const ObjectFile &Obj, |
| 751 | const SectionRef &Section, |
| 752 | bool IsCode, |
| 753 | ObjSectionToIDMap &LocalSections) { |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 754 | |
| 755 | unsigned SectionID = 0; |
| 756 | ObjSectionToIDMap::iterator i = LocalSections.find(Section); |
| 757 | if (i != LocalSections.end()) |
| 758 | SectionID = i->second; |
| 759 | else { |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 760 | if (auto SectionIDOrErr = emitSection(Obj, Section, IsCode)) |
| 761 | SectionID = *SectionIDOrErr; |
| 762 | else |
| 763 | return SectionIDOrErr.takeError(); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 764 | LocalSections[Section] = SectionID; |
| 765 | } |
| 766 | return SectionID; |
| 767 | } |
| 768 | |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 769 | void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE, |
| 770 | unsigned SectionID) { |
| 771 | Relocations[SectionID].push_back(RE); |
| 772 | } |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 773 | |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 774 | void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE, |
| 775 | StringRef SymbolName) { |
| 776 | // Relocation by symbol. If the symbol is found in the global symbol table, |
| 777 | // create an appropriate section relocation. Otherwise, add it to |
| 778 | // ExternalSymbolRelocations. |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 779 | RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(SymbolName); |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 780 | if (Loc == GlobalSymbolTable.end()) { |
| 781 | ExternalSymbolRelocations[SymbolName].push_back(RE); |
Eli Bendersky | b92e1cf | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 782 | } else { |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 783 | // Copy the RE since we want to modify its addend. |
| 784 | RelocationEntry RECopy = RE; |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 785 | const auto &SymInfo = Loc->second; |
| 786 | RECopy.Addend += SymInfo.getOffset(); |
| 787 | Relocations[SymInfo.getSectionID()].push_back(RECopy); |
Eli Bendersky | b92e1cf | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 788 | } |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Ulrich Weigand | 752b5c9 | 2014-07-20 23:53:14 +0000 | [diff] [blame] | 791 | uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr, |
| 792 | unsigned AbiVariant) { |
Tim Northover | e19bed7 | 2014-07-23 12:32:47 +0000 | [diff] [blame] | 793 | if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) { |
Tim Northover | 37cde97 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 794 | // This stub has to be able to access the full address space, |
| 795 | // since symbol lookup won't necessarily find a handy, in-range, |
| 796 | // PLT stub for functions which could be anywhere. |
Tim Northover | 37cde97 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 797 | // Stub can use ip0 (== x16) to calculate address |
Daniel Sanders | 66e799f | 2014-11-06 09:53:05 +0000 | [diff] [blame] | 798 | writeBytesUnaligned(0xd2e00010, Addr, 4); // movz ip0, #:abs_g3:<addr> |
| 799 | writeBytesUnaligned(0xf2c00010, Addr+4, 4); // movk ip0, #:abs_g2_nc:<addr> |
| 800 | writeBytesUnaligned(0xf2a00010, Addr+8, 4); // movk ip0, #:abs_g1_nc:<addr> |
| 801 | writeBytesUnaligned(0xf2800010, Addr+12, 4); // movk ip0, #:abs_g0_nc:<addr> |
| 802 | writeBytesUnaligned(0xd61f0200, Addr+16, 4); // br ip0 |
Tim Northover | 37cde97 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 803 | |
| 804 | return Addr; |
Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 805 | } else if (Arch == Triple::arm || Arch == Triple::armeb) { |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 806 | // TODO: There is only ARM far stub now. We should add the Thumb stub, |
| 807 | // and stubs for branches Thumb - ARM and ARM - Thumb. |
Daniel Sanders | 66e799f | 2014-11-06 09:53:05 +0000 | [diff] [blame] | 808 | writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc,<label> |
| 809 | return Addr + 4; |
Petar Jovanovic | 9720283 | 2015-05-28 13:48:41 +0000 | [diff] [blame] | 810 | } else if (IsMipsO32ABI) { |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 811 | // 0: 3c190000 lui t9,%hi(addr). |
| 812 | // 4: 27390000 addiu t9,t9,%lo(addr). |
| 813 | // 8: 03200008 jr t9. |
| 814 | // c: 00000000 nop. |
| 815 | const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000; |
| 816 | const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0; |
| 817 | |
Daniel Sanders | 66e799f | 2014-11-06 09:53:05 +0000 | [diff] [blame] | 818 | writeBytesUnaligned(LuiT9Instr, Addr, 4); |
| 819 | writeBytesUnaligned(AdduiT9Instr, Addr+4, 4); |
| 820 | writeBytesUnaligned(JrT9Instr, Addr+8, 4); |
| 821 | writeBytesUnaligned(NopInstr, Addr+12, 4); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 822 | return Addr; |
Bill Schmidt | 0a9170d | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 823 | } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) { |
Ulrich Weigand | 752b5c9 | 2014-07-20 23:53:14 +0000 | [diff] [blame] | 824 | // Depending on which version of the ELF ABI is in use, we need to |
| 825 | // generate one of two variants of the stub. They both start with |
| 826 | // the same sequence to load the target address into r12. |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 827 | writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr) |
| 828 | writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr) |
| 829 | writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32 |
| 830 | writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr) |
| 831 | writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr) |
Ulrich Weigand | 752b5c9 | 2014-07-20 23:53:14 +0000 | [diff] [blame] | 832 | if (AbiVariant == 2) { |
| 833 | // PowerPC64 stub ELFv2 ABI: The address points to the function itself. |
| 834 | // The address is already in r12 as required by the ABI. Branch to it. |
| 835 | writeInt32BE(Addr+20, 0xF8410018); // std r2, 24(r1) |
| 836 | writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12 |
| 837 | writeInt32BE(Addr+28, 0x4E800420); // bctr |
| 838 | } else { |
| 839 | // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor. |
| 840 | // Load the function address on r11 and sets it to control register. Also |
| 841 | // loads the function TOC in r2 and environment pointer to r11. |
| 842 | writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1) |
| 843 | writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12) |
| 844 | writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12) |
| 845 | writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11 |
| 846 | writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2) |
| 847 | writeInt32BE(Addr+40, 0x4E800420); // bctr |
| 848 | } |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 849 | return Addr; |
Richard Sandiford | ca04408 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 850 | } else if (Arch == Triple::systemz) { |
| 851 | writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8 |
| 852 | writeInt16BE(Addr+2, 0x0000); |
| 853 | writeInt16BE(Addr+4, 0x0004); |
| 854 | writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1 |
| 855 | // 8-byte address stored at Addr + 8 |
| 856 | return Addr; |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 857 | } else if (Arch == Triple::x86_64) { |
| 858 | *Addr = 0xFF; // jmp |
| 859 | *(Addr+1) = 0x25; // rip |
| 860 | // 32-bit PC-relative address of the GOT entry will be stored at Addr+2 |
Lang Hames | 36072da | 2014-05-12 21:39:59 +0000 | [diff] [blame] | 861 | } else if (Arch == Triple::x86) { |
| 862 | *Addr = 0xE9; // 32-bit pc-relative jump. |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 863 | } |
| 864 | return Addr; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | // Assign an address to a symbol name and resolve all the relocations |
| 868 | // associated with it. |
| 869 | void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID, |
| 870 | uint64_t Addr) { |
| 871 | // The address to use for relocation resolution is not |
| 872 | // the address of the local section buffer. We must be doing |
Andrew Kaylor | a714efc | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 873 | // a remote execution environment of some sort. Relocations can't |
| 874 | // be applied until all the sections have been moved. The client must |
| 875 | // trigger this with a call to MCJIT::finalize() or |
| 876 | // RuntimeDyld::resolveRelocations(). |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 877 | // |
| 878 | // Addr is a uint64_t because we can't assume the pointer width |
| 879 | // of the target is the same as that of the host. Just use a generic |
| 880 | // "big enough" type. |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 881 | DEBUG(dbgs() << "Reassigning address for section " << SectionID << " (" |
| 882 | << Sections[SectionID].getName() << "): " |
| 883 | << format("0x%016" PRIx64, Sections[SectionID].getLoadAddress()) |
| 884 | << " -> " << format("0x%016" PRIx64, Addr) << "\n"); |
| 885 | Sections[SectionID].setLoadAddress(Addr); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 888 | void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, |
| 889 | uint64_t Value) { |
| 890 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 891 | const RelocationEntry &RE = Relocs[i]; |
| 892 | // Ignore relocations for sections that were not loaded |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 893 | if (Sections[RE.SectionID].getAddress() == nullptr) |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 894 | continue; |
| 895 | resolveRelocation(RE, Value); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
Eli Bendersky | b92e1cf | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 899 | void RuntimeDyldImpl::resolveExternalSymbols() { |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 900 | while (!ExternalSymbolRelocations.empty()) { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 901 | StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(); |
| 902 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 903 | StringRef Name = i->first(); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 904 | if (Name.size() == 0) { |
| 905 | // This is an absolute symbol, use an address of zero. |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 906 | DEBUG(dbgs() << "Resolving absolute relocations." |
| 907 | << "\n"); |
Andrew Kaylor | cfb4a99 | 2013-11-11 19:55:10 +0000 | [diff] [blame] | 908 | RelocationList &Relocs = i->second; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 909 | resolveRelocationList(Relocs, 0); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 910 | } else { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 911 | uint64_t Addr = 0; |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 912 | RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 913 | if (Loc == GlobalSymbolTable.end()) { |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 914 | // This is an external symbol, try to get its address from the symbol |
| 915 | // resolver. |
| 916 | Addr = Resolver.findSymbol(Name.data()).getAddress(); |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 917 | // The call to getSymbolAddress may have caused additional modules to |
| 918 | // be loaded, which may have added new entries to the |
| 919 | // ExternalSymbolRelocations map. Consquently, we need to update our |
| 920 | // iterator. This is also why retrieval of the relocation list |
| 921 | // associated with this symbol is deferred until below this point. |
| 922 | // New entries may have been added to the relocation list. |
| 923 | i = ExternalSymbolRelocations.find(Name); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 924 | } else { |
| 925 | // We found the symbol in our global table. It was probably in a |
| 926 | // Module that we loaded previously. |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 927 | const auto &SymInfo = Loc->second; |
| 928 | Addr = getSectionLoadAddress(SymInfo.getSectionID()) + |
| 929 | SymInfo.getOffset(); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | // FIXME: Implement error handling that doesn't kill the host program! |
| 933 | if (!Addr) |
| 934 | report_fatal_error("Program used external function '" + Name + |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 935 | "' which could not be resolved!"); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 936 | |
Lang Hames | 78937c2 | 2015-07-04 01:35:26 +0000 | [diff] [blame] | 937 | // If Resolver returned UINT64_MAX, the client wants to handle this symbol |
| 938 | // manually and we shouldn't resolve its relocations. |
| 939 | if (Addr != UINT64_MAX) { |
| 940 | DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t" |
| 941 | << format("0x%lx", Addr) << "\n"); |
| 942 | // This list may have been updated when we called getSymbolAddress, so |
| 943 | // don't change this code to get the list earlier. |
| 944 | RelocationList &Relocs = i->second; |
| 945 | resolveRelocationList(Relocs, Addr); |
| 946 | } |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 947 | } |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 948 | |
Andrew Kaylor | cfb4a99 | 2013-11-11 19:55:10 +0000 | [diff] [blame] | 949 | ExternalSymbolRelocations.erase(i); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 953 | //===----------------------------------------------------------------------===// |
| 954 | // RuntimeDyld class implementation |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 955 | |
| 956 | uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress( |
Lang Hames | 2e88f4f | 2015-07-28 17:52:11 +0000 | [diff] [blame] | 957 | const object::SectionRef &Sec) const { |
| 958 | |
Lang Hames | 2e88f4f | 2015-07-28 17:52:11 +0000 | [diff] [blame] | 959 | auto I = ObjSecToIDMap.find(Sec); |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 960 | if (I != ObjSecToIDMap.end()) |
| 961 | return RTDyld.Sections[I->second].getLoadAddress(); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 962 | |
| 963 | return 0; |
| 964 | } |
| 965 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 966 | void RuntimeDyld::MemoryManager::anchor() {} |
| 967 | void RuntimeDyld::SymbolResolver::anchor() {} |
| 968 | |
| 969 | RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr, |
| 970 | RuntimeDyld::SymbolResolver &Resolver) |
| 971 | : MemMgr(MemMgr), Resolver(Resolver) { |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 972 | // FIXME: There's a potential issue lurking here if a single instance of |
| 973 | // RuntimeDyld is used to load multiple objects. The current implementation |
| 974 | // associates a single memory manager with a RuntimeDyld instance. Even |
| 975 | // though the public class spawns a new 'impl' instance for each load, |
| 976 | // they share a single memory manager. This can become a problem when page |
| 977 | // permissions are applied. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 978 | Dyld = nullptr; |
Lang Hames | 868d4b3 | 2014-03-20 21:06:46 +0000 | [diff] [blame] | 979 | ProcessAllSections = false; |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 980 | Checker = nullptr; |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 981 | } |
| 982 | |
David Blaikie | 847d2a0 | 2014-09-04 18:37:29 +0000 | [diff] [blame] | 983 | RuntimeDyld::~RuntimeDyld() {} |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 984 | |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 985 | static std::unique_ptr<RuntimeDyldCOFF> |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 986 | createRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, |
| 987 | RuntimeDyld::SymbolResolver &Resolver, |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 988 | bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) { |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 989 | std::unique_ptr<RuntimeDyldCOFF> Dyld = |
| 990 | RuntimeDyldCOFF::create(Arch, MM, Resolver); |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 991 | Dyld->setProcessAllSections(ProcessAllSections); |
| 992 | Dyld->setRuntimeDyldChecker(Checker); |
| 993 | return Dyld; |
| 994 | } |
| 995 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 996 | static std::unique_ptr<RuntimeDyldELF> |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 997 | createRuntimeDyldELF(RuntimeDyld::MemoryManager &MM, |
| 998 | RuntimeDyld::SymbolResolver &Resolver, |
| 999 | bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) { |
| 1000 | std::unique_ptr<RuntimeDyldELF> Dyld(new RuntimeDyldELF(MM, Resolver)); |
Lang Hames | 868d4b3 | 2014-03-20 21:06:46 +0000 | [diff] [blame] | 1001 | Dyld->setProcessAllSections(ProcessAllSections); |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 1002 | Dyld->setRuntimeDyldChecker(Checker); |
Lang Hames | 868d4b3 | 2014-03-20 21:06:46 +0000 | [diff] [blame] | 1003 | return Dyld; |
| 1004 | } |
| 1005 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 1006 | static std::unique_ptr<RuntimeDyldMachO> |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 1007 | createRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, |
| 1008 | RuntimeDyld::SymbolResolver &Resolver, |
| 1009 | bool ProcessAllSections, |
| 1010 | RuntimeDyldCheckerImpl *Checker) { |
| 1011 | std::unique_ptr<RuntimeDyldMachO> Dyld = |
| 1012 | RuntimeDyldMachO::create(Arch, MM, Resolver); |
Lang Hames | 868d4b3 | 2014-03-20 21:06:46 +0000 | [diff] [blame] | 1013 | Dyld->setProcessAllSections(ProcessAllSections); |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 1014 | Dyld->setRuntimeDyldChecker(Checker); |
Lang Hames | 868d4b3 | 2014-03-20 21:06:46 +0000 | [diff] [blame] | 1015 | return Dyld; |
| 1016 | } |
| 1017 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 1018 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> |
| 1019 | RuntimeDyld::loadObject(const ObjectFile &Obj) { |
| 1020 | if (!Dyld) { |
| 1021 | if (Obj.isELF()) |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 1022 | Dyld = createRuntimeDyldELF(MemMgr, Resolver, ProcessAllSections, Checker); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 1023 | else if (Obj.isMachO()) |
Aaron Ballman | 9fb4114 | 2014-11-26 15:27:39 +0000 | [diff] [blame] | 1024 | Dyld = createRuntimeDyldMachO( |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 1025 | static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver, |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 1026 | ProcessAllSections, Checker); |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 1027 | else if (Obj.isCOFF()) |
| 1028 | Dyld = createRuntimeDyldCOFF( |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 1029 | static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver, |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 1030 | ProcessAllSections, Checker); |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 1031 | else |
| 1032 | report_fatal_error("Incompatible object format!"); |
Aaron Ballman | 9fb4114 | 2014-11-26 15:27:39 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 1035 | if (!Dyld->isCompatibleFile(Obj)) |
Aaron Ballman | 9fb4114 | 2014-11-26 15:27:39 +0000 | [diff] [blame] | 1036 | report_fatal_error("Incompatible object format!"); |
| 1037 | |
Lang Hames | b093429 | 2016-01-10 23:59:41 +0000 | [diff] [blame] | 1038 | auto LoadedObjInfo = Dyld->loadObject(Obj); |
| 1039 | MemMgr.notifyObjectLoaded(*this, Obj); |
| 1040 | return LoadedObjInfo; |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 1043 | void *RuntimeDyld::getSymbolLocalAddress(StringRef Name) const { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 1044 | if (!Dyld) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1045 | return nullptr; |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 1046 | return Dyld->getSymbolLocalAddress(Name); |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 1049 | RuntimeDyld::SymbolInfo RuntimeDyld::getSymbol(StringRef Name) const { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 1050 | if (!Dyld) |
Lang Hames | b118603 | 2015-03-11 00:43:26 +0000 | [diff] [blame] | 1051 | return nullptr; |
| 1052 | return Dyld->getSymbol(Name); |
Lang Hames | 6bfd398 | 2015-01-16 23:13:56 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 1055 | void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); } |
Jim Grosbach | 733d305 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 1056 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 1057 | void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) { |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 1058 | Dyld->reassignSectionAddress(SectionID, Addr); |
Jim Grosbach | 733d305 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Jim Grosbach | 6d61397 | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 1061 | void RuntimeDyld::mapSectionAddress(const void *LocalAddress, |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 1062 | uint64_t TargetAddress) { |
| 1063 | Dyld->mapSectionAddress(LocalAddress, TargetAddress); |
| 1064 | } |
| 1065 | |
Juergen Ributzka | 6ff29a7 | 2014-03-26 18:19:27 +0000 | [diff] [blame] | 1066 | bool RuntimeDyld::hasError() { return Dyld->hasError(); } |
| 1067 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 1068 | StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); } |
Jim Grosbach | 40411cc | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 1069 | |
Lang Hames | 859d73c | 2016-01-09 19:50:40 +0000 | [diff] [blame] | 1070 | void RuntimeDyld::finalizeWithMemoryManagerLocking() { |
| 1071 | bool MemoryFinalizationLocked = MemMgr.FinalizationLocked; |
| 1072 | MemMgr.FinalizationLocked = true; |
| 1073 | resolveRelocations(); |
| 1074 | registerEHFrames(); |
| 1075 | if (!MemoryFinalizationLocked) { |
| 1076 | MemMgr.finalizeMemory(); |
| 1077 | MemMgr.FinalizationLocked = false; |
| 1078 | } |
| 1079 | } |
| 1080 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 1081 | void RuntimeDyld::registerEHFrames() { |
Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 1082 | if (Dyld) |
| 1083 | Dyld->registerEHFrames(); |
| 1084 | } |
| 1085 | |
| 1086 | void RuntimeDyld::deregisterEHFrames() { |
| 1087 | if (Dyld) |
| 1088 | Dyld->deregisterEHFrames(); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 1091 | } // end namespace llvm |