Jim Grosbach | e0934be | 2012-01-16 23:50:58 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===// |
Jim Grosbach | 6e56331 | 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 | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "dyld" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 15 | #include "RuntimeDyldImpl.h" |
Eli Bendersky | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 16 | #include "RuntimeDyldELF.h" |
| 17 | #include "RuntimeDyldMachO.h" |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Eli Bendersky | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 19 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | using namespace llvm::object; |
| 22 | |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 23 | // Empty out-of-line virtual destructor as the key function. |
| 24 | RTDyldMemoryManager::~RTDyldMemoryManager() {} |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 25 | RuntimeDyldImpl::~RuntimeDyldImpl() {} |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 26 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 27 | namespace llvm { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 28 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 29 | namespace { |
| 30 | // Helper for extensive error checking in debug builds. |
| 31 | error_code Check(error_code Err) { |
| 32 | if (Err) { |
| 33 | report_fatal_error(Err.message()); |
| 34 | } |
| 35 | return Err; |
| 36 | } |
| 37 | } // end anonymous namespace |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 38 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 39 | // Resolve the relocations for all symbols we currently know about. |
| 40 | void RuntimeDyldImpl::resolveRelocations() { |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 41 | // First, resolve relocations associated with external symbols. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 42 | resolveSymbols(); |
| 43 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 44 | // Just iterate over the sections we have and resolve all the relocations |
| 45 | // in them. Gross overkill, but it gets the job done. |
| 46 | for (int i = 0, e = Sections.size(); i != e; ++i) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 47 | reassignSectionAddress(i, Sections[i].LoadAddress); |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 48 | } |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 51 | void RuntimeDyldImpl::mapSectionAddress(void *LocalAddress, |
| 52 | uint64_t TargetAddress) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 53 | for (unsigned i = 0, e = Sections.size(); i != e; ++i) { |
| 54 | if (Sections[i].Address == LocalAddress) { |
| 55 | reassignSectionAddress(i, TargetAddress); |
| 56 | return; |
| 57 | } |
| 58 | } |
| 59 | llvm_unreachable("Attempting to remap address of unknown section!"); |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame^] | 62 | // Subclasses can implement this method to create specialized image instances. |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 63 | // The caller owns the the pointer that is returned. |
| 64 | ObjectImage *RuntimeDyldImpl::createObjectImage(const MemoryBuffer *InputBuffer) { |
| 65 | ObjectFile *ObjFile = ObjectFile::createObjectFile(const_cast<MemoryBuffer*> |
| 66 | (InputBuffer)); |
| 67 | ObjectImage *Obj = new ObjectImage(ObjFile); |
| 68 | return Obj; |
| 69 | } |
| 70 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 71 | bool RuntimeDyldImpl::loadObject(const MemoryBuffer *InputBuffer) { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 72 | OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer)); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 73 | if (!obj) |
| 74 | report_fatal_error("Unable to create object image from memory buffer!"); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 75 | |
| 76 | Arch = (Triple::ArchType)obj->getArch(); |
| 77 | |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame^] | 78 | LocalSymbolMap LocalSymbols; // Functions and data symbols from the |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 79 | // object file. |
| 80 | ObjSectionToIDMap LocalSections; // Used sections from the object file |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 81 | CommonSymbolMap CommonSymbols; // Common symbols requiring allocation |
| 82 | uint64_t CommonSize = 0; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 83 | |
| 84 | error_code err; |
| 85 | // Parse symbols |
| 86 | DEBUG(dbgs() << "Parse symbols:\n"); |
| 87 | for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols(); |
| 88 | i != e; i.increment(err)) { |
| 89 | Check(err); |
| 90 | object::SymbolRef::Type SymType; |
| 91 | StringRef Name; |
| 92 | Check(i->getType(SymType)); |
| 93 | Check(i->getName(Name)); |
| 94 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 95 | uint32_t flags; |
| 96 | Check(i->getFlags(flags)); |
| 97 | |
| 98 | bool isCommon = flags & SymbolRef::SF_Common; |
| 99 | if (isCommon) { |
| 100 | // Add the common symbols to a list. We'll allocate them all below. |
| 101 | uint64_t Size = 0; |
| 102 | Check(i->getSize(Size)); |
| 103 | CommonSize += Size; |
| 104 | CommonSymbols[*i] = Size; |
| 105 | } else { |
| 106 | if (SymType == object::SymbolRef::ST_Function || |
| 107 | SymType == object::SymbolRef::ST_Data) { |
| 108 | uint64_t FileOffset; |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame^] | 109 | StringRef SectionData; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 110 | section_iterator si = obj->end_sections(); |
| 111 | Check(i->getFileOffset(FileOffset)); |
| 112 | Check(i->getSection(si)); |
| 113 | if (si == obj->end_sections()) continue; |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame^] | 114 | Check(si->getContents(SectionData)); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 115 | const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() + |
| 116 | (uintptr_t)FileOffset; |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame^] | 117 | uintptr_t SectOffset = (uintptr_t)(SymPtr - |
| 118 | (const uint8_t*)SectionData.begin()); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 119 | unsigned SectionID = |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 120 | findOrEmitSection(*obj, |
| 121 | *si, |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 122 | SymType == object::SymbolRef::ST_Function, |
| 123 | LocalSections); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 124 | LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset); |
| 125 | DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset) |
| 126 | << " flags: " << flags |
| 127 | << " SID: " << SectionID |
| 128 | << " Offset: " << format("%p", SectOffset)); |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame^] | 129 | bool isGlobal = flags & SymbolRef::SF_Global; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 130 | if (isGlobal) |
| 131 | SymbolTable[Name] = SymbolLoc(SectionID, SectOffset); |
| 132 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 133 | } |
| 134 | DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n"); |
| 135 | } |
| 136 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 137 | // Allocate common symbols |
| 138 | if (CommonSize != 0) |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 139 | emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 140 | |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame^] | 141 | // Parse and process relocations |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 142 | DEBUG(dbgs() << "Parse relocations:\n"); |
| 143 | for (section_iterator si = obj->begin_sections(), |
| 144 | se = obj->end_sections(); si != se; si.increment(err)) { |
| 145 | Check(err); |
| 146 | bool isFirstRelocation = true; |
| 147 | unsigned SectionID = 0; |
| 148 | StubMap Stubs; |
| 149 | |
| 150 | for (relocation_iterator i = si->begin_relocations(), |
| 151 | e = si->end_relocations(); i != e; i.increment(err)) { |
| 152 | Check(err); |
| 153 | |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame^] | 154 | // If it's the first relocation in this section, find its SectionID |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 155 | if (isFirstRelocation) { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 156 | SectionID = findOrEmitSection(*obj, *si, true, LocalSections); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 157 | DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n"); |
| 158 | isFirstRelocation = false; |
| 159 | } |
| 160 | |
| 161 | ObjRelocationInfo RI; |
| 162 | RI.SectionID = SectionID; |
| 163 | Check(i->getAdditionalInfo(RI.AdditionalInfo)); |
| 164 | Check(i->getOffset(RI.Offset)); |
| 165 | Check(i->getSymbol(RI.Symbol)); |
| 166 | Check(i->getType(RI.Type)); |
| 167 | |
| 168 | DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo |
| 169 | << " Offset: " << format("%p", (uintptr_t)RI.Offset) |
| 170 | << " Type: " << (uint32_t)(RI.Type & 0xffffffffL) |
| 171 | << "\n"); |
| 172 | processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs); |
| 173 | } |
| 174 | } |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 175 | |
| 176 | handleObjectLoaded(obj.take()); |
| 177 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 181 | unsigned RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj, |
| 182 | const CommonSymbolMap &Map, |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 183 | uint64_t TotalSize, |
| 184 | LocalSymbolMap &LocalSymbols) { |
| 185 | // Allocate memory for the section |
| 186 | unsigned SectionID = Sections.size(); |
| 187 | uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*), |
| 188 | SectionID); |
| 189 | if (!Addr) |
| 190 | report_fatal_error("Unable to allocate memory for common symbols!"); |
| 191 | uint64_t Offset = 0; |
| 192 | Sections.push_back(SectionEntry(Addr, TotalSize, TotalSize, 0)); |
| 193 | memset(Addr, 0, TotalSize); |
| 194 | |
| 195 | DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID |
| 196 | << " new addr: " << format("%p", Addr) |
| 197 | << " DataSize: " << TotalSize |
| 198 | << "\n"); |
| 199 | |
| 200 | // Assign the address of each symbol |
| 201 | for (CommonSymbolMap::const_iterator it = Map.begin(), itEnd = Map.end(); |
| 202 | it != itEnd; it++) { |
| 203 | uint64_t Size = it->second; |
| 204 | StringRef Name; |
| 205 | it->first.getName(Name); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 206 | Obj.updateSymbolAddress(it->first, (uint64_t)Addr); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 207 | LocalSymbols[Name.data()] = SymbolLoc(SectionID, Offset); |
| 208 | Offset += Size; |
| 209 | Addr += Size; |
| 210 | } |
| 211 | |
| 212 | return SectionID; |
| 213 | } |
| 214 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 215 | unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj, |
| 216 | const SectionRef &Section, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 217 | bool IsCode) { |
| 218 | |
| 219 | unsigned StubBufSize = 0, |
| 220 | StubSize = getMaxStubSize(); |
| 221 | error_code err; |
| 222 | if (StubSize > 0) { |
| 223 | for (relocation_iterator i = Section.begin_relocations(), |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 224 | e = Section.end_relocations(); i != e; i.increment(err), Check(err)) |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 225 | StubBufSize += StubSize; |
| 226 | } |
| 227 | StringRef data; |
| 228 | uint64_t Alignment64; |
| 229 | Check(Section.getContents(data)); |
| 230 | Check(Section.getAlignment(Alignment64)); |
| 231 | |
| 232 | unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 233 | bool IsRequired; |
| 234 | bool IsVirtual; |
| 235 | bool IsZeroInit; |
| 236 | uint64_t DataSize; |
| 237 | Check(Section.isRequiredForExecution(IsRequired)); |
| 238 | Check(Section.isVirtual(IsVirtual)); |
| 239 | Check(Section.isZeroInit(IsZeroInit)); |
| 240 | Check(Section.getSize(DataSize)); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 241 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 242 | unsigned Allocate; |
| 243 | unsigned SectionID = Sections.size(); |
| 244 | uint8_t *Addr; |
| 245 | const char *pData = 0; |
| 246 | |
| 247 | // Some sections, such as debug info, don't need to be loaded for execution. |
| 248 | // Leave those where they are. |
| 249 | if (IsRequired) { |
| 250 | Allocate = DataSize + StubBufSize; |
| 251 | Addr = IsCode |
| 252 | ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID) |
| 253 | : MemMgr->allocateDataSection(Allocate, Alignment, SectionID); |
| 254 | if (!Addr) |
| 255 | report_fatal_error("Unable to allocate section memory!"); |
| 256 | |
| 257 | // Virtual sections have no data in the object image, so leave pData = 0 |
| 258 | if (!IsVirtual) |
| 259 | pData = data.data(); |
| 260 | |
| 261 | // Zero-initialize or copy the data from the image |
| 262 | if (IsZeroInit || IsVirtual) |
| 263 | memset(Addr, 0, DataSize); |
| 264 | else |
| 265 | memcpy(Addr, pData, DataSize); |
| 266 | |
| 267 | DEBUG(dbgs() << "emitSection SectionID: " << SectionID |
| 268 | << " obj addr: " << format("%p", pData) |
| 269 | << " new addr: " << format("%p", Addr) |
| 270 | << " DataSize: " << DataSize |
| 271 | << " StubBufSize: " << StubBufSize |
| 272 | << " Allocate: " << Allocate |
| 273 | << "\n"); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 274 | Obj.updateSectionAddress(Section, (uint64_t)Addr); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 275 | } |
| 276 | else { |
| 277 | // Even if we didn't load the section, we need to record an entry for it |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame^] | 278 | // to handle later processing (and by 'handle' I mean don't do anything |
| 279 | // with these sections). |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 280 | Allocate = 0; |
| 281 | Addr = 0; |
| 282 | DEBUG(dbgs() << "emitSection SectionID: " << SectionID |
| 283 | << " obj addr: " << format("%p", data.data()) |
| 284 | << " new addr: 0" |
| 285 | << " DataSize: " << DataSize |
| 286 | << " StubBufSize: " << StubBufSize |
| 287 | << " Allocate: " << Allocate |
| 288 | << "\n"); |
| 289 | } |
| 290 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 291 | Sections.push_back(SectionEntry(Addr, Allocate, DataSize,(uintptr_t)pData)); |
| 292 | return SectionID; |
| 293 | } |
| 294 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 295 | unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj, |
| 296 | const SectionRef &Section, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 297 | bool IsCode, |
| 298 | ObjSectionToIDMap &LocalSections) { |
| 299 | |
| 300 | unsigned SectionID = 0; |
| 301 | ObjSectionToIDMap::iterator i = LocalSections.find(Section); |
| 302 | if (i != LocalSections.end()) |
| 303 | SectionID = i->second; |
| 304 | else { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 305 | SectionID = emitSection(Obj, Section, IsCode); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 306 | LocalSections[Section] = SectionID; |
| 307 | } |
| 308 | return SectionID; |
| 309 | } |
| 310 | |
| 311 | void RuntimeDyldImpl::AddRelocation(const RelocationValueRef &Value, |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame^] | 312 | unsigned SectionID, uintptr_t Offset, |
| 313 | uint32_t RelType) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 314 | DEBUG(dbgs() << "AddRelocation SymNamePtr: " << format("%p", Value.SymbolName) |
| 315 | << " SID: " << Value.SectionID |
| 316 | << " Addend: " << format("%p", Value.Addend) |
| 317 | << " Offset: " << format("%p", Offset) |
| 318 | << " RelType: " << format("%x", RelType) |
| 319 | << "\n"); |
| 320 | |
| 321 | if (Value.SymbolName == 0) { |
| 322 | Relocations[Value.SectionID].push_back(RelocationEntry( |
| 323 | SectionID, |
| 324 | Offset, |
| 325 | RelType, |
| 326 | Value.Addend)); |
| 327 | } else |
| 328 | SymbolRelocations[Value.SymbolName].push_back(RelocationEntry( |
| 329 | SectionID, |
| 330 | Offset, |
| 331 | RelType, |
| 332 | Value.Addend)); |
| 333 | } |
| 334 | |
| 335 | uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) { |
| 336 | // TODO: There is only ARM far stub now. We should add the Thumb stub, |
| 337 | // and stubs for branches Thumb - ARM and ARM - Thumb. |
| 338 | if (Arch == Triple::arm) { |
| 339 | uint32_t *StubAddr = (uint32_t*)Addr; |
| 340 | *StubAddr = 0xe51ff004; // ldr pc,<label> |
| 341 | return (uint8_t*)++StubAddr; |
| 342 | } |
| 343 | else |
| 344 | return Addr; |
| 345 | } |
| 346 | |
| 347 | // Assign an address to a symbol name and resolve all the relocations |
| 348 | // associated with it. |
| 349 | void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID, |
| 350 | uint64_t Addr) { |
| 351 | // The address to use for relocation resolution is not |
| 352 | // the address of the local section buffer. We must be doing |
| 353 | // a remote execution environment of some sort. Re-apply any |
| 354 | // relocations referencing this section with the given address. |
| 355 | // |
| 356 | // Addr is a uint64_t because we can't assume the pointer width |
| 357 | // of the target is the same as that of the host. Just use a generic |
| 358 | // "big enough" type. |
| 359 | Sections[SectionID].LoadAddress = Addr; |
| 360 | DEBUG(dbgs() << "Resolving relocations Section #" << SectionID |
| 361 | << "\t" << format("%p", (uint8_t *)Addr) |
| 362 | << "\n"); |
| 363 | resolveRelocationList(Relocations[SectionID], Addr); |
| 364 | } |
| 365 | |
| 366 | void RuntimeDyldImpl::resolveRelocationEntry(const RelocationEntry &RE, |
| 367 | uint64_t Value) { |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 368 | // Ignore relocations for sections that were not loaded |
| 369 | if (Sections[RE.SectionID].Address != 0) { |
| 370 | uint8_t *Target = Sections[RE.SectionID].Address + RE.Offset; |
| 371 | DEBUG(dbgs() << "\tSectionID: " << RE.SectionID |
| 372 | << " + " << RE.Offset << " (" << format("%p", Target) << ")" |
| 373 | << " Data: " << RE.Data |
| 374 | << " Addend: " << RE.Addend |
| 375 | << "\n"); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 376 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 377 | resolveRelocation(Target, Sections[RE.SectionID].LoadAddress + RE.Offset, |
| 378 | Value, RE.Data, RE.Addend); |
| 379 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, |
| 383 | uint64_t Value) { |
| 384 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
| 385 | resolveRelocationEntry(Relocs[i], Value); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // resolveSymbols - Resolve any relocations to the specified symbols if |
| 390 | // we know where it lives. |
| 391 | void RuntimeDyldImpl::resolveSymbols() { |
| 392 | StringMap<RelocationList>::iterator i = SymbolRelocations.begin(), |
| 393 | e = SymbolRelocations.end(); |
| 394 | for (; i != e; i++) { |
| 395 | StringRef Name = i->first(); |
| 396 | RelocationList &Relocs = i->second; |
| 397 | StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name); |
| 398 | if (Loc == SymbolTable.end()) { |
| 399 | // This is an external symbol, try to get it address from |
| 400 | // MemoryManager. |
| 401 | uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(), |
| 402 | true); |
| 403 | DEBUG(dbgs() << "Resolving relocations Name: " << Name |
| 404 | << "\t" << format("%p", Addr) |
| 405 | << "\n"); |
| 406 | resolveRelocationList(Relocs, (uintptr_t)Addr); |
| 407 | } else { |
| 408 | // Change the relocation to be section relative rather than symbol |
| 409 | // relative and move it to the resolved relocation list. |
| 410 | DEBUG(dbgs() << "Resolving symbol '" << Name << "'\n"); |
| 411 | for (int i = 0, e = Relocs.size(); i != e; ++i) { |
| 412 | RelocationEntry Entry = Relocs[i]; |
| 413 | Entry.Addend += Loc->second.second; |
| 414 | Relocations[Loc->second.first].push_back(Entry); |
| 415 | } |
| 416 | Relocs.clear(); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 422 | //===----------------------------------------------------------------------===// |
| 423 | // RuntimeDyld class implementation |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 424 | RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) { |
| 425 | Dyld = 0; |
| 426 | MM = mm; |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | RuntimeDyld::~RuntimeDyld() { |
| 430 | delete Dyld; |
| 431 | } |
| 432 | |
| 433 | bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 434 | if (!Dyld) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 435 | sys::LLVMFileType type = sys::IdentifyFileType( |
| 436 | InputBuffer->getBufferStart(), |
| 437 | static_cast<unsigned>(InputBuffer->getBufferSize())); |
| 438 | switch (type) { |
| 439 | case sys::ELF_Relocatable_FileType: |
| 440 | case sys::ELF_Executable_FileType: |
| 441 | case sys::ELF_SharedObject_FileType: |
| 442 | case sys::ELF_Core_FileType: |
| 443 | Dyld = new RuntimeDyldELF(MM); |
| 444 | break; |
| 445 | case sys::Mach_O_Object_FileType: |
| 446 | case sys::Mach_O_Executable_FileType: |
| 447 | case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: |
| 448 | case sys::Mach_O_Core_FileType: |
| 449 | case sys::Mach_O_PreloadExecutable_FileType: |
| 450 | case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: |
| 451 | case sys::Mach_O_DynamicLinker_FileType: |
| 452 | case sys::Mach_O_Bundle_FileType: |
| 453 | case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: |
| 454 | case sys::Mach_O_DSYMCompanion_FileType: |
| 455 | Dyld = new RuntimeDyldMachO(MM); |
| 456 | break; |
| 457 | case sys::Unknown_FileType: |
| 458 | case sys::Bitcode_FileType: |
| 459 | case sys::Archive_FileType: |
| 460 | case sys::COFF_FileType: |
| 461 | report_fatal_error("Incompatible object format!"); |
| 462 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 463 | } else { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 464 | if (!Dyld->isCompatibleFormat(InputBuffer)) |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 465 | report_fatal_error("Incompatible object format!"); |
| 466 | } |
| 467 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 468 | return Dyld->loadObject(InputBuffer); |
| 469 | } |
| 470 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 471 | void *RuntimeDyld::getSymbolAddress(StringRef Name) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 472 | return Dyld->getSymbolAddress(Name); |
| 473 | } |
| 474 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 475 | void RuntimeDyld::resolveRelocations() { |
| 476 | Dyld->resolveRelocations(); |
| 477 | } |
| 478 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 479 | void RuntimeDyld::reassignSectionAddress(unsigned SectionID, |
| 480 | uint64_t Addr) { |
| 481 | Dyld->reassignSectionAddress(SectionID, Addr); |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 484 | void RuntimeDyld::mapSectionAddress(void *LocalAddress, |
| 485 | uint64_t TargetAddress) { |
| 486 | Dyld->mapSectionAddress(LocalAddress, TargetAddress); |
| 487 | } |
| 488 | |
Jim Grosbach | 91dde15 | 2011-03-22 18:22:27 +0000 | [diff] [blame] | 489 | StringRef RuntimeDyld::getErrorString() { |
Jim Grosbach | b3eecaf | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 490 | return Dyld->getErrorString(); |
| 491 | } |
| 492 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 493 | } // end namespace llvm |