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