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