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 | |
Jim Grosbach | 6a85a05 | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "dyld" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 16 | #include "JITRegistrar.h" |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 17 | #include "ObjectImageCommon.h" |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 18 | #include "RuntimeDyldELF.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "RuntimeDyldImpl.h" |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 20 | #include "RuntimeDyldMachO.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "llvm/Object/ELF.h" |
Rafael Espindola | 8de8607 | 2013-06-11 18:01:14 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.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 | 086f708 | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 29 | // Empty out-of-line virtual destructor as the key function. |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 30 | RuntimeDyldImpl::~RuntimeDyldImpl() {} |
Chandler Carruth | 086f708 | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 31 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 32 | // Pin the JITRegistrar's and ObjectImage*'s vtables to this file. |
| 33 | void JITRegistrar::anchor() {} |
| 34 | void ObjectImage::anchor() {} |
| 35 | void ObjectImageCommon::anchor() {} |
| 36 | |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 37 | namespace llvm { |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 38 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 39 | void RuntimeDyldImpl::registerEHFrames() { |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 42 | void RuntimeDyldImpl::deregisterEHFrames() { |
| 43 | } |
| 44 | |
Jim Grosbach | 733d305 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 45 | // Resolve the relocations for all symbols we currently know about. |
| 46 | void RuntimeDyldImpl::resolveRelocations() { |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 47 | MutexGuard locked(lock); |
| 48 | |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 49 | // First, resolve relocations associated with external symbols. |
Eli Bendersky | b92e1cf | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 50 | resolveExternalSymbols(); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 51 | |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 52 | // Just iterate over the sections we have and resolve all the relocations |
| 53 | // in them. Gross overkill, but it gets the job done. |
| 54 | for (int i = 0, e = Sections.size(); i != e; ++i) { |
Andrew Kaylor | 5f3a998 | 2013-08-19 19:38:06 +0000 | [diff] [blame] | 55 | // The Section here (Sections[i]) refers to the section in which the |
| 56 | // symbol for the relocation is located. The SectionID in the relocation |
| 57 | // entry provides the section to which the relocation will be applied. |
Andrew Kaylor | a714efc | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 58 | uint64_t Addr = Sections[i].LoadAddress; |
| 59 | DEBUG(dbgs() << "Resolving relocations Section #" << i |
| 60 | << "\t" << format("%p", (uint8_t *)Addr) |
| 61 | << "\n"); |
| 62 | resolveRelocationList(Relocations[i], Addr); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 63 | Relocations.erase(i); |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 64 | } |
Jim Grosbach | 733d305 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Jim Grosbach | 6d61397 | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 67 | void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress, |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 68 | uint64_t TargetAddress) { |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 69 | MutexGuard locked(lock); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 70 | for (unsigned i = 0, e = Sections.size(); i != e; ++i) { |
| 71 | if (Sections[i].Address == LocalAddress) { |
| 72 | reassignSectionAddress(i, TargetAddress); |
| 73 | return; |
| 74 | } |
| 75 | } |
| 76 | llvm_unreachable("Attempting to remap address of unknown section!"); |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Eli Bendersky | 0e2ac5b | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 79 | // Subclasses can implement this method to create specialized image instances. |
Sylvestre Ledru | 35521e2 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 80 | // The caller owns the pointer that is returned. |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 81 | ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) { |
| 82 | return new ObjectImageCommon(InputBuffer); |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 85 | ObjectImage *RuntimeDyldImpl::createObjectImageFromFile(ObjectFile *InputObject) { |
| 86 | return new ObjectImageCommon(InputObject); |
| 87 | } |
| 88 | |
| 89 | ObjectImage *RuntimeDyldImpl::loadObject(ObjectFile *InputObject) { |
| 90 | return loadObject(createObjectImageFromFile(InputObject)); |
| 91 | } |
| 92 | |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 93 | ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) { |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 94 | return loadObject(createObjectImage(InputBuffer)); |
| 95 | } |
| 96 | |
| 97 | ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) { |
Andrew Kaylor | 4fba049 | 2013-10-21 17:42:06 +0000 | [diff] [blame] | 98 | MutexGuard locked(lock); |
| 99 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 100 | OwningPtr<ObjectImage> obj(InputObject); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 101 | if (!obj) |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 102 | return NULL; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 103 | |
Andrew Kaylor | 33c5b1b | 2013-10-15 20:44:55 +0000 | [diff] [blame] | 104 | // Save information about our target |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 105 | Arch = (Triple::ArchType)obj->getArch(); |
Andrew Kaylor | 33c5b1b | 2013-10-15 20:44:55 +0000 | [diff] [blame] | 106 | IsTargetLittleEndian = obj->getObjectFile()->isLittleEndian(); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 107 | |
Eli Bendersky | fc07908 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 108 | // Symbols found in this object |
| 109 | StringMap<SymbolLoc> LocalSymbols; |
| 110 | // Used sections from the object file |
| 111 | ObjSectionToIDMap LocalSections; |
| 112 | |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 113 | // Common symbols requiring allocation, with their sizes and alignments |
Eli Bendersky | fc07908 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 114 | CommonSymbolMap CommonSymbols; |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 115 | // Maximum required total memory to allocate all common symbols |
Eli Bendersky | fc07908 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 116 | uint64_t CommonSize = 0; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 117 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 118 | // Parse symbols |
| 119 | DEBUG(dbgs() << "Parse symbols:\n"); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 120 | for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols(); i != e; |
| 121 | ++i) { |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 122 | object::SymbolRef::Type SymType; |
| 123 | StringRef Name; |
| 124 | Check(i->getType(SymType)); |
| 125 | Check(i->getName(Name)); |
| 126 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 127 | uint32_t flags = i->getFlags(); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 128 | |
| 129 | bool isCommon = flags & SymbolRef::SF_Common; |
| 130 | if (isCommon) { |
| 131 | // Add the common symbols to a list. We'll allocate them all below. |
Rafael Espindola | e4dd2e0 | 2013-04-29 22:24:22 +0000 | [diff] [blame] | 132 | uint32_t Align; |
| 133 | Check(i->getAlignment(Align)); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 134 | uint64_t Size = 0; |
| 135 | Check(i->getSize(Size)); |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 136 | CommonSize += Size + Align; |
| 137 | CommonSymbols[*i] = CommonSymbolInfo(Size, Align); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 138 | } else { |
| 139 | if (SymType == object::SymbolRef::ST_Function || |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 140 | SymType == object::SymbolRef::ST_Data || |
| 141 | SymType == object::SymbolRef::ST_Unknown) { |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 142 | uint64_t FileOffset; |
Eli Bendersky | 0e2ac5b | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 143 | StringRef SectionData; |
Tim Northover | d05e6b5 | 2012-12-17 17:59:35 +0000 | [diff] [blame] | 144 | bool IsCode; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 145 | section_iterator si = obj->end_sections(); |
| 146 | Check(i->getFileOffset(FileOffset)); |
| 147 | Check(i->getSection(si)); |
| 148 | if (si == obj->end_sections()) continue; |
Eli Bendersky | 0e2ac5b | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 149 | Check(si->getContents(SectionData)); |
Tim Northover | d05e6b5 | 2012-12-17 17:59:35 +0000 | [diff] [blame] | 150 | Check(si->isText(IsCode)); |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 151 | const uint8_t* SymPtr = (const uint8_t*)InputObject->getData().data() + |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 152 | (uintptr_t)FileOffset; |
Eli Bendersky | 0e2ac5b | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 153 | uintptr_t SectOffset = (uintptr_t)(SymPtr - |
| 154 | (const uint8_t*)SectionData.begin()); |
Tim Northover | d05e6b5 | 2012-12-17 17:59:35 +0000 | [diff] [blame] | 155 | unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 156 | LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset); |
| 157 | DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset) |
| 158 | << " flags: " << flags |
| 159 | << " SID: " << SectionID |
| 160 | << " Offset: " << format("%p", SectOffset)); |
Amara Emerson | c958bf3 | 2012-11-16 11:11:59 +0000 | [diff] [blame] | 161 | GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 162 | } |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 163 | } |
| 164 | DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n"); |
| 165 | } |
| 166 | |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 167 | // Allocate common symbols |
| 168 | if (CommonSize != 0) |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 169 | emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 170 | |
Eli Bendersky | 0e2ac5b | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 171 | // Parse and process relocations |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 172 | DEBUG(dbgs() << "Parse relocations:\n"); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 173 | for (section_iterator si = obj->begin_sections(), se = obj->end_sections(); |
| 174 | si != se; ++si) { |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 175 | bool isFirstRelocation = true; |
| 176 | unsigned SectionID = 0; |
| 177 | StubMap Stubs; |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 178 | section_iterator RelocatedSection = si->getRelocatedSection(); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 179 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame^] | 180 | for (relocation_iterator i = si->relocation_begin(), |
| 181 | e = si->relocation_end(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 182 | i != e; ++i) { |
Eli Bendersky | 0e2ac5b | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 183 | // If it's the first relocation in this section, find its SectionID |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 184 | if (isFirstRelocation) { |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 185 | SectionID = |
| 186 | findOrEmitSection(*obj, *RelocatedSection, true, LocalSections); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 187 | DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n"); |
| 188 | isFirstRelocation = false; |
| 189 | } |
| 190 | |
Rafael Espindola | 3700894 | 2013-04-29 19:03:21 +0000 | [diff] [blame] | 191 | processRelocationRef(SectionID, *i, *obj, LocalSections, LocalSymbols, |
NAKAMURA Takumi | 87e0880 | 2013-12-07 11:21:42 +0000 | [diff] [blame] | 192 | Stubs); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 195 | |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 196 | // Give the subclasses a chance to tie-up any loose ends. |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 197 | finalizeLoad(LocalSections); |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 198 | |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 199 | return obj.take(); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 202 | void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj, |
| 203 | const CommonSymbolMap &CommonSymbols, |
| 204 | uint64_t TotalSize, |
| 205 | SymbolTableMap &SymbolTable) { |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 206 | // Allocate memory for the section |
| 207 | unsigned SectionID = Sections.size(); |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 208 | uint8_t *Addr = MemMgr->allocateDataSection( |
| 209 | TotalSize, sizeof(void*), SectionID, StringRef(), false); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 210 | if (!Addr) |
| 211 | report_fatal_error("Unable to allocate memory for common symbols!"); |
| 212 | uint64_t Offset = 0; |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 213 | Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0)); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 214 | memset(Addr, 0, TotalSize); |
| 215 | |
| 216 | DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID |
| 217 | << " new addr: " << format("%p", Addr) |
| 218 | << " DataSize: " << TotalSize |
| 219 | << "\n"); |
| 220 | |
| 221 | // Assign the address of each symbol |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 222 | for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(), |
| 223 | itEnd = CommonSymbols.end(); it != itEnd; it++) { |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 224 | uint64_t Size = it->second.first; |
| 225 | uint64_t Align = it->second.second; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 226 | StringRef Name; |
| 227 | it->first.getName(Name); |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 228 | if (Align) { |
| 229 | // This symbol has an alignment requirement. |
| 230 | uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align); |
| 231 | Addr += AlignOffset; |
| 232 | Offset += AlignOffset; |
| 233 | DEBUG(dbgs() << "Allocating common symbol " << Name << " address " << |
Andrew Kaylor | 0eece8d | 2012-11-01 19:49:21 +0000 | [diff] [blame] | 234 | format("%p\n", Addr)); |
Tim Northover | 94bc73d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 235 | } |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 236 | Obj.updateSymbolAddress(it->first, (uint64_t)Addr); |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 237 | SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 238 | Offset += Size; |
| 239 | Addr += Size; |
| 240 | } |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 243 | unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj, |
| 244 | const SectionRef &Section, |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 245 | bool IsCode) { |
| 246 | |
| 247 | unsigned StubBufSize = 0, |
| 248 | StubSize = getMaxStubSize(); |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 249 | const ObjectFile *ObjFile = Obj.getObjectFile(); |
| 250 | // FIXME: this is an inefficient way to handle this. We should computed the |
| 251 | // necessary section allocation size in loadObject by walking all the sections |
| 252 | // once. |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 253 | if (StubSize > 0) { |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame^] | 254 | for (section_iterator SI = ObjFile->section_begin(), |
| 255 | SE = ObjFile->section_end(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 256 | SI != SE; ++SI) { |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 257 | section_iterator RelSecI = SI->getRelocatedSection(); |
| 258 | if (!(RelSecI == Section)) |
| 259 | continue; |
| 260 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame^] | 261 | for (relocation_iterator I = SI->relocation_begin(), |
| 262 | E = SI->relocation_end(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 263 | I != E; ++I) { |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 264 | StubBufSize += StubSize; |
| 265 | } |
| 266 | } |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 267 | } |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 268 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 269 | StringRef data; |
| 270 | uint64_t Alignment64; |
| 271 | Check(Section.getContents(data)); |
| 272 | Check(Section.getAlignment(Alignment64)); |
| 273 | |
| 274 | unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 275 | bool IsRequired; |
| 276 | bool IsVirtual; |
| 277 | bool IsZeroInit; |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 278 | bool IsReadOnly; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 279 | uint64_t DataSize; |
Andrew Kaylor | 877b931 | 2013-10-16 00:32:24 +0000 | [diff] [blame] | 280 | unsigned PaddingSize = 0; |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 281 | StringRef Name; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 282 | Check(Section.isRequiredForExecution(IsRequired)); |
| 283 | Check(Section.isVirtual(IsVirtual)); |
| 284 | Check(Section.isZeroInit(IsZeroInit)); |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 285 | Check(Section.isReadOnlyData(IsReadOnly)); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 286 | Check(Section.getSize(DataSize)); |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 287 | Check(Section.getName(Name)); |
Richard Sandiford | ca04408 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 288 | if (StubSize > 0) { |
| 289 | unsigned StubAlignment = getStubAlignment(); |
| 290 | unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment); |
| 291 | if (StubAlignment > EndAlignment) |
| 292 | StubBufSize += StubAlignment - EndAlignment; |
| 293 | } |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 294 | |
Andrew Kaylor | 877b931 | 2013-10-16 00:32:24 +0000 | [diff] [blame] | 295 | // The .eh_frame section (at least on Linux) needs an extra four bytes padded |
| 296 | // with zeroes added at the end. For MachO objects, this section has a |
| 297 | // slightly different name, so this won't have any effect for MachO objects. |
| 298 | if (Name == ".eh_frame") |
| 299 | PaddingSize = 4; |
| 300 | |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 301 | unsigned Allocate; |
| 302 | unsigned SectionID = Sections.size(); |
| 303 | uint8_t *Addr; |
| 304 | const char *pData = 0; |
| 305 | |
| 306 | // Some sections, such as debug info, don't need to be loaded for execution. |
| 307 | // Leave those where they are. |
| 308 | if (IsRequired) { |
Andrew Kaylor | 877b931 | 2013-10-16 00:32:24 +0000 | [diff] [blame] | 309 | Allocate = DataSize + PaddingSize + StubBufSize; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 310 | Addr = IsCode |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 311 | ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID, Name) |
| 312 | : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, Name, |
| 313 | IsReadOnly); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 314 | if (!Addr) |
| 315 | report_fatal_error("Unable to allocate section memory!"); |
| 316 | |
| 317 | // Virtual sections have no data in the object image, so leave pData = 0 |
| 318 | if (!IsVirtual) |
| 319 | pData = data.data(); |
| 320 | |
| 321 | // Zero-initialize or copy the data from the image |
| 322 | if (IsZeroInit || IsVirtual) |
| 323 | memset(Addr, 0, DataSize); |
| 324 | else |
| 325 | memcpy(Addr, pData, DataSize); |
| 326 | |
Andrew Kaylor | 877b931 | 2013-10-16 00:32:24 +0000 | [diff] [blame] | 327 | // Fill in any extra bytes we allocated for padding |
| 328 | if (PaddingSize != 0) { |
| 329 | memset(Addr + DataSize, 0, PaddingSize); |
| 330 | // Update the DataSize variable so that the stub offset is set correctly. |
| 331 | DataSize += PaddingSize; |
| 332 | } |
| 333 | |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 334 | DEBUG(dbgs() << "emitSection SectionID: " << SectionID |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 335 | << " Name: " << Name |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 336 | << " obj addr: " << format("%p", pData) |
| 337 | << " new addr: " << format("%p", Addr) |
| 338 | << " DataSize: " << DataSize |
| 339 | << " StubBufSize: " << StubBufSize |
| 340 | << " Allocate: " << Allocate |
| 341 | << "\n"); |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 342 | Obj.updateSectionAddress(Section, (uint64_t)Addr); |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 343 | } |
| 344 | else { |
| 345 | // 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] | 346 | // to handle later processing (and by 'handle' I mean don't do anything |
| 347 | // with these sections). |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 348 | Allocate = 0; |
| 349 | Addr = 0; |
| 350 | DEBUG(dbgs() << "emitSection SectionID: " << SectionID |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 351 | << " Name: " << Name |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 352 | << " obj addr: " << format("%p", data.data()) |
| 353 | << " new addr: 0" |
| 354 | << " DataSize: " << DataSize |
| 355 | << " StubBufSize: " << StubBufSize |
| 356 | << " Allocate: " << Allocate |
| 357 | << "\n"); |
| 358 | } |
| 359 | |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 360 | Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData)); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 361 | return SectionID; |
| 362 | } |
| 363 | |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 364 | unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj, |
| 365 | const SectionRef &Section, |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 366 | bool IsCode, |
| 367 | ObjSectionToIDMap &LocalSections) { |
| 368 | |
| 369 | unsigned SectionID = 0; |
| 370 | ObjSectionToIDMap::iterator i = LocalSections.find(Section); |
| 371 | if (i != LocalSections.end()) |
| 372 | SectionID = i->second; |
| 373 | else { |
Preston Gurd | cc31af9 | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 374 | SectionID = emitSection(Obj, Section, IsCode); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 375 | LocalSections[Section] = SectionID; |
| 376 | } |
| 377 | return SectionID; |
| 378 | } |
| 379 | |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 380 | void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE, |
| 381 | unsigned SectionID) { |
| 382 | Relocations[SectionID].push_back(RE); |
| 383 | } |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 384 | |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 385 | void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE, |
| 386 | StringRef SymbolName) { |
| 387 | // Relocation by symbol. If the symbol is found in the global symbol table, |
| 388 | // create an appropriate section relocation. Otherwise, add it to |
| 389 | // ExternalSymbolRelocations. |
| 390 | SymbolTableMap::const_iterator Loc = |
| 391 | GlobalSymbolTable.find(SymbolName); |
| 392 | if (Loc == GlobalSymbolTable.end()) { |
| 393 | ExternalSymbolRelocations[SymbolName].push_back(RE); |
Eli Bendersky | b92e1cf | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 394 | } else { |
Eli Bendersky | 667b879 | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 395 | // Copy the RE since we want to modify its addend. |
| 396 | RelocationEntry RECopy = RE; |
| 397 | RECopy.Addend += Loc->second.second; |
| 398 | Relocations[Loc->second.first].push_back(RECopy); |
Eli Bendersky | b92e1cf | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 399 | } |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) { |
Tim Northover | 37cde97 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 403 | if (Arch == Triple::aarch64) { |
| 404 | // This stub has to be able to access the full address space, |
| 405 | // since symbol lookup won't necessarily find a handy, in-range, |
| 406 | // PLT stub for functions which could be anywhere. |
| 407 | uint32_t *StubAddr = (uint32_t*)Addr; |
| 408 | |
| 409 | // Stub can use ip0 (== x16) to calculate address |
| 410 | *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr> |
| 411 | StubAddr++; |
| 412 | *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr> |
| 413 | StubAddr++; |
| 414 | *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr> |
| 415 | StubAddr++; |
| 416 | *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr> |
| 417 | StubAddr++; |
| 418 | *StubAddr = 0xd61f0200; // br ip0 |
| 419 | |
| 420 | return Addr; |
| 421 | } else if (Arch == Triple::arm) { |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 422 | // TODO: There is only ARM far stub now. We should add the Thumb stub, |
| 423 | // and stubs for branches Thumb - ARM and ARM - Thumb. |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 424 | uint32_t *StubAddr = (uint32_t*)Addr; |
| 425 | *StubAddr = 0xe51ff004; // ldr pc,<label> |
| 426 | return (uint8_t*)++StubAddr; |
NAKAMURA Takumi | f97efd9 | 2012-12-04 00:08:14 +0000 | [diff] [blame] | 427 | } else if (Arch == Triple::mipsel || Arch == Triple::mips) { |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 428 | uint32_t *StubAddr = (uint32_t*)Addr; |
| 429 | // 0: 3c190000 lui t9,%hi(addr). |
| 430 | // 4: 27390000 addiu t9,t9,%lo(addr). |
| 431 | // 8: 03200008 jr t9. |
| 432 | // c: 00000000 nop. |
| 433 | const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000; |
| 434 | const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0; |
| 435 | |
| 436 | *StubAddr = LuiT9Instr; |
| 437 | StubAddr++; |
| 438 | *StubAddr = AdduiT9Instr; |
| 439 | StubAddr++; |
| 440 | *StubAddr = JrT9Instr; |
| 441 | StubAddr++; |
| 442 | *StubAddr = NopInstr; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 443 | return Addr; |
Bill Schmidt | 0a9170d | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 444 | } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) { |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 445 | // PowerPC64 stub: the address points to a function descriptor |
| 446 | // instead of the function itself. Load the function address |
| 447 | // on r11 and sets it to control register. Also loads the function |
| 448 | // TOC in r2 and environment pointer to r11. |
| 449 | writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr) |
| 450 | writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr) |
| 451 | writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32 |
| 452 | writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr) |
| 453 | writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr) |
| 454 | writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1) |
| 455 | writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12) |
| 456 | writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12) |
| 457 | writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11 |
| 458 | writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2) |
| 459 | writeInt32BE(Addr+40, 0x4E800420); // bctr |
Andrew Kaylor | 0eece8d | 2012-11-01 19:49:21 +0000 | [diff] [blame] | 460 | |
Adhemerval Zanella | 5fc11b3 | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 461 | return Addr; |
Richard Sandiford | ca04408 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 462 | } else if (Arch == Triple::systemz) { |
| 463 | writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8 |
| 464 | writeInt16BE(Addr+2, 0x0000); |
| 465 | writeInt16BE(Addr+4, 0x0004); |
| 466 | writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1 |
| 467 | // 8-byte address stored at Addr + 8 |
| 468 | return Addr; |
Andrew Kaylor | 4612fed | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 469 | } else if (Arch == Triple::x86_64) { |
| 470 | *Addr = 0xFF; // jmp |
| 471 | *(Addr+1) = 0x25; // rip |
| 472 | // 32-bit PC-relative address of the GOT entry will be stored at Addr+2 |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 473 | } |
| 474 | return Addr; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | // Assign an address to a symbol name and resolve all the relocations |
| 478 | // associated with it. |
| 479 | void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID, |
| 480 | uint64_t Addr) { |
| 481 | // The address to use for relocation resolution is not |
| 482 | // the address of the local section buffer. We must be doing |
Andrew Kaylor | a714efc | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 483 | // a remote execution environment of some sort. Relocations can't |
| 484 | // be applied until all the sections have been moved. The client must |
| 485 | // trigger this with a call to MCJIT::finalize() or |
| 486 | // RuntimeDyld::resolveRelocations(). |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 487 | // |
| 488 | // Addr is a uint64_t because we can't assume the pointer width |
| 489 | // of the target is the same as that of the host. Just use a generic |
| 490 | // "big enough" type. |
| 491 | Sections[SectionID].LoadAddress = Addr; |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 494 | void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, |
| 495 | uint64_t Value) { |
| 496 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
Rafael Espindola | f1f1c62 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 497 | const RelocationEntry &RE = Relocs[i]; |
| 498 | // Ignore relocations for sections that were not loaded |
| 499 | if (Sections[RE.SectionID].Address == 0) |
| 500 | continue; |
| 501 | resolveRelocation(RE, Value); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Eli Bendersky | b92e1cf | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 505 | void RuntimeDyldImpl::resolveExternalSymbols() { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 506 | while(!ExternalSymbolRelocations.empty()) { |
| 507 | StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(); |
| 508 | |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 509 | StringRef Name = i->first(); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 510 | if (Name.size() == 0) { |
| 511 | // This is an absolute symbol, use an address of zero. |
| 512 | DEBUG(dbgs() << "Resolving absolute relocations." << "\n"); |
Andrew Kaylor | cfb4a99 | 2013-11-11 19:55:10 +0000 | [diff] [blame] | 513 | RelocationList &Relocs = i->second; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 514 | resolveRelocationList(Relocs, 0); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 515 | } else { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 516 | uint64_t Addr = 0; |
| 517 | SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name); |
| 518 | if (Loc == GlobalSymbolTable.end()) { |
| 519 | // This is an external symbol, try to get its address from |
| 520 | // MemoryManager. |
| 521 | Addr = MemMgr->getSymbolAddress(Name.data()); |
Andrew Kaylor | cfb4a99 | 2013-11-11 19:55:10 +0000 | [diff] [blame] | 522 | // The call to getSymbolAddress may have caused additional modules to |
| 523 | // be loaded, which may have added new entries to the |
| 524 | // ExternalSymbolRelocations map. Consquently, we need to update our |
| 525 | // iterator. This is also why retrieval of the relocation list |
| 526 | // associated with this symbol is deferred until below this point. |
| 527 | // New entries may have been added to the relocation list. |
| 528 | i = ExternalSymbolRelocations.find(Name); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 529 | } else { |
| 530 | // We found the symbol in our global table. It was probably in a |
| 531 | // Module that we loaded previously. |
Yaron Keren | c980288 | 2013-10-19 09:04:26 +0000 | [diff] [blame] | 532 | SymbolLoc SymLoc = Loc->second; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 533 | Addr = getSectionLoadAddress(SymLoc.first) + SymLoc.second; |
| 534 | } |
| 535 | |
| 536 | // FIXME: Implement error handling that doesn't kill the host program! |
| 537 | if (!Addr) |
| 538 | report_fatal_error("Program used external function '" + Name + |
| 539 | "' which could not be resolved!"); |
| 540 | |
| 541 | updateGOTEntries(Name, Addr); |
| 542 | DEBUG(dbgs() << "Resolving relocations Name: " << Name |
| 543 | << "\t" << format("0x%lx", Addr) |
| 544 | << "\n"); |
Andrew Kaylor | cfb4a99 | 2013-11-11 19:55:10 +0000 | [diff] [blame] | 545 | // This list may have been updated when we called getSymbolAddress, so |
| 546 | // don't change this code to get the list earlier. |
| 547 | RelocationList &Relocs = i->second; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 548 | resolveRelocationList(Relocs, Addr); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 549 | } |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 550 | |
Andrew Kaylor | cfb4a99 | 2013-11-11 19:55:10 +0000 | [diff] [blame] | 551 | ExternalSymbolRelocations.erase(i); |
Danil Malyshev | 70d22cc | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 556 | //===----------------------------------------------------------------------===// |
| 557 | // RuntimeDyld class implementation |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 558 | RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) { |
Andrew Kaylor | a342cb9 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 559 | // FIXME: There's a potential issue lurking here if a single instance of |
| 560 | // RuntimeDyld is used to load multiple objects. The current implementation |
| 561 | // associates a single memory manager with a RuntimeDyld instance. Even |
| 562 | // though the public class spawns a new 'impl' instance for each load, |
| 563 | // they share a single memory manager. This can become a problem when page |
| 564 | // permissions are applied. |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 565 | Dyld = 0; |
| 566 | MM = mm; |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | RuntimeDyld::~RuntimeDyld() { |
| 570 | delete Dyld; |
| 571 | } |
| 572 | |
Lang Hames | 173c69f | 2014-01-08 04:09:09 +0000 | [diff] [blame] | 573 | ObjectImage *RuntimeDyld::loadObject(ObjectFile *InputObject) { |
| 574 | if (!Dyld) { |
| 575 | if (InputObject->isELF()) |
| 576 | Dyld = new RuntimeDyldELF(MM); |
| 577 | else if (InputObject->isMachO()) |
| 578 | Dyld = new RuntimeDyldMachO(MM); |
| 579 | else |
| 580 | report_fatal_error("Incompatible object format!"); |
| 581 | } else { |
| 582 | if (!Dyld->isCompatibleFile(InputObject)) |
| 583 | report_fatal_error("Incompatible object format!"); |
| 584 | } |
| 585 | |
| 586 | return Dyld->loadObject(InputObject); |
| 587 | } |
| 588 | |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 589 | ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) { |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 590 | if (!Dyld) { |
Rafael Espindola | 8de8607 | 2013-06-11 18:01:14 +0000 | [diff] [blame] | 591 | sys::fs::file_magic Type = |
| 592 | sys::fs::identify_magic(InputBuffer->getBuffer()); |
| 593 | switch (Type) { |
| 594 | case sys::fs::file_magic::elf_relocatable: |
| 595 | case sys::fs::file_magic::elf_executable: |
| 596 | case sys::fs::file_magic::elf_shared_object: |
| 597 | case sys::fs::file_magic::elf_core: |
| 598 | Dyld = new RuntimeDyldELF(MM); |
| 599 | break; |
| 600 | case sys::fs::file_magic::macho_object: |
| 601 | case sys::fs::file_magic::macho_executable: |
| 602 | case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: |
| 603 | case sys::fs::file_magic::macho_core: |
| 604 | case sys::fs::file_magic::macho_preload_executable: |
| 605 | case sys::fs::file_magic::macho_dynamically_linked_shared_lib: |
| 606 | case sys::fs::file_magic::macho_dynamic_linker: |
| 607 | case sys::fs::file_magic::macho_bundle: |
| 608 | case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: |
| 609 | case sys::fs::file_magic::macho_dsym_companion: |
| 610 | Dyld = new RuntimeDyldMachO(MM); |
| 611 | break; |
| 612 | case sys::fs::file_magic::unknown: |
| 613 | case sys::fs::file_magic::bitcode: |
| 614 | case sys::fs::file_magic::archive: |
| 615 | case sys::fs::file_magic::coff_object: |
Rui Ueyama | e448f9e | 2013-11-15 21:22:02 +0000 | [diff] [blame] | 616 | case sys::fs::file_magic::coff_import_library: |
Rafael Espindola | 8de8607 | 2013-06-11 18:01:14 +0000 | [diff] [blame] | 617 | case sys::fs::file_magic::pecoff_executable: |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 618 | case sys::fs::file_magic::macho_universal_binary: |
Rui Ueyama | fc149a6 | 2013-10-15 22:45:38 +0000 | [diff] [blame] | 619 | case sys::fs::file_magic::windows_resource: |
Rafael Espindola | 8de8607 | 2013-06-11 18:01:14 +0000 | [diff] [blame] | 620 | report_fatal_error("Incompatible object format!"); |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 621 | } |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 622 | } else { |
Eli Bendersky | 4c64758 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 623 | if (!Dyld->isCompatibleFormat(InputBuffer)) |
Danil Malyshev | 72510f2 | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 624 | report_fatal_error("Incompatible object format!"); |
| 625 | } |
| 626 | |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 627 | return Dyld->loadObject(InputBuffer); |
| 628 | } |
| 629 | |
Jim Grosbach | 18b81c5 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 630 | void *RuntimeDyld::getSymbolAddress(StringRef Name) { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 631 | if (!Dyld) |
| 632 | return NULL; |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 633 | return Dyld->getSymbolAddress(Name); |
| 634 | } |
| 635 | |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 636 | uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 637 | if (!Dyld) |
| 638 | return 0; |
Jim Grosbach | dc1123f | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 639 | return Dyld->getSymbolLoadAddress(Name); |
| 640 | } |
| 641 | |
Jim Grosbach | 733d305 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 642 | void RuntimeDyld::resolveRelocations() { |
| 643 | Dyld->resolveRelocations(); |
| 644 | } |
| 645 | |
Jim Grosbach | eff0a40 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 646 | void RuntimeDyld::reassignSectionAddress(unsigned SectionID, |
| 647 | uint64_t Addr) { |
| 648 | Dyld->reassignSectionAddress(SectionID, Addr); |
Jim Grosbach | 733d305 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Jim Grosbach | 6d61397 | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 651 | void RuntimeDyld::mapSectionAddress(const void *LocalAddress, |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 652 | uint64_t TargetAddress) { |
| 653 | Dyld->mapSectionAddress(LocalAddress, TargetAddress); |
| 654 | } |
| 655 | |
Jim Grosbach | 5cc5eef | 2011-03-22 18:22:27 +0000 | [diff] [blame] | 656 | StringRef RuntimeDyld::getErrorString() { |
Jim Grosbach | 40411cc | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 657 | return Dyld->getErrorString(); |
| 658 | } |
| 659 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 660 | void RuntimeDyld::registerEHFrames() { |
Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 661 | if (Dyld) |
| 662 | Dyld->registerEHFrames(); |
| 663 | } |
| 664 | |
| 665 | void RuntimeDyld::deregisterEHFrames() { |
| 666 | if (Dyld) |
| 667 | Dyld->deregisterEHFrames(); |
Rafael Espindola | fa5942b | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Jim Grosbach | f016b0a | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 670 | } // end namespace llvm |