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" |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 15 | #include "ObjectImageCommon.h" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 16 | #include "RuntimeDyldImpl.h" |
Eli Bendersky | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 17 | #include "RuntimeDyldELF.h" |
| 18 | #include "RuntimeDyldMachO.h" |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
Eli Bendersky | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 20 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | using namespace llvm::object; |
| 23 | |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 24 | // Empty out-of-line virtual destructor as the key function. |
| 25 | RTDyldMemoryManager::~RTDyldMemoryManager() {} |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 26 | RuntimeDyldImpl::~RuntimeDyldImpl() {} |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 27 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 28 | namespace llvm { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 29 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | // Helper for extensive error checking in debug builds. |
| 32 | error_code Check(error_code Err) { |
| 33 | if (Err) { |
| 34 | report_fatal_error(Err.message()); |
| 35 | } |
| 36 | return Err; |
| 37 | } |
| 38 | } // end anonymous namespace |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 39 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 40 | // Resolve the relocations for all symbols we currently know about. |
| 41 | void RuntimeDyldImpl::resolveRelocations() { |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 42 | // First, resolve relocations associated with external symbols. |
Eli Bendersky | 37bc5a2 | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 43 | resolveExternalSymbols(); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 44 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 45 | // Just iterate over the sections we have and resolve all the relocations |
| 46 | // in them. Gross overkill, but it gets the job done. |
| 47 | for (int i = 0, e = Sections.size(); i != e; ++i) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 48 | reassignSectionAddress(i, Sections[i].LoadAddress); |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 49 | } |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Jim Grosbach | e940c1b | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 52 | void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress, |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 53 | uint64_t TargetAddress) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 54 | for (unsigned i = 0, e = Sections.size(); i != e; ++i) { |
| 55 | if (Sections[i].Address == LocalAddress) { |
| 56 | reassignSectionAddress(i, TargetAddress); |
| 57 | return; |
| 58 | } |
| 59 | } |
| 60 | llvm_unreachable("Attempting to remap address of unknown section!"); |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 63 | // Subclasses can implement this method to create specialized image instances. |
Sylvestre Ledru | c8e41c5 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 64 | // The caller owns the pointer that is returned. |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 65 | ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) { |
| 66 | return new ObjectImageCommon(InputBuffer); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 69 | ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 70 | OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer)); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 71 | if (!obj) |
| 72 | report_fatal_error("Unable to create object image from memory buffer!"); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 73 | |
| 74 | Arch = (Triple::ArchType)obj->getArch(); |
| 75 | |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 76 | // Symbols found in this object |
| 77 | StringMap<SymbolLoc> LocalSymbols; |
| 78 | // Used sections from the object file |
| 79 | ObjSectionToIDMap LocalSections; |
| 80 | |
| 81 | // Common symbols requiring allocation, and the total size required to |
| 82 | // allocate all common symbols. |
| 83 | CommonSymbolMap CommonSymbols; |
| 84 | uint64_t CommonSize = 0; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 85 | |
| 86 | error_code err; |
| 87 | // Parse symbols |
| 88 | DEBUG(dbgs() << "Parse symbols:\n"); |
| 89 | for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols(); |
| 90 | i != e; i.increment(err)) { |
| 91 | Check(err); |
| 92 | object::SymbolRef::Type SymType; |
| 93 | StringRef Name; |
| 94 | Check(i->getType(SymType)); |
| 95 | Check(i->getName(Name)); |
| 96 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 97 | uint32_t flags; |
| 98 | Check(i->getFlags(flags)); |
| 99 | |
| 100 | bool isCommon = flags & SymbolRef::SF_Common; |
| 101 | if (isCommon) { |
| 102 | // Add the common symbols to a list. We'll allocate them all below. |
| 103 | uint64_t Size = 0; |
| 104 | Check(i->getSize(Size)); |
| 105 | CommonSize += Size; |
| 106 | CommonSymbols[*i] = Size; |
| 107 | } else { |
| 108 | if (SymType == object::SymbolRef::ST_Function || |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 109 | SymType == object::SymbolRef::ST_Data || |
| 110 | SymType == object::SymbolRef::ST_Unknown) { |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 111 | uint64_t FileOffset; |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 112 | StringRef SectionData; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 113 | section_iterator si = obj->end_sections(); |
| 114 | Check(i->getFileOffset(FileOffset)); |
| 115 | Check(i->getSection(si)); |
| 116 | if (si == obj->end_sections()) continue; |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 117 | Check(si->getContents(SectionData)); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 118 | const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() + |
| 119 | (uintptr_t)FileOffset; |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 120 | uintptr_t SectOffset = (uintptr_t)(SymPtr - |
| 121 | (const uint8_t*)SectionData.begin()); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 122 | unsigned SectionID = |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 123 | findOrEmitSection(*obj, |
| 124 | *si, |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 125 | SymType == object::SymbolRef::ST_Function, |
| 126 | LocalSections); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 127 | LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset); |
| 128 | DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset) |
| 129 | << " flags: " << flags |
| 130 | << " SID: " << SectionID |
| 131 | << " Offset: " << format("%p", SectOffset)); |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 132 | bool isGlobal = flags & SymbolRef::SF_Global; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 133 | if (isGlobal) |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 134 | GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 135 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 136 | } |
| 137 | DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n"); |
| 138 | } |
| 139 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 140 | // Allocate common symbols |
| 141 | if (CommonSize != 0) |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 142 | emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 143 | |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 144 | // Parse and process relocations |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 145 | DEBUG(dbgs() << "Parse relocations:\n"); |
| 146 | for (section_iterator si = obj->begin_sections(), |
| 147 | se = obj->end_sections(); si != se; si.increment(err)) { |
| 148 | Check(err); |
| 149 | bool isFirstRelocation = true; |
| 150 | unsigned SectionID = 0; |
| 151 | StubMap Stubs; |
| 152 | |
| 153 | for (relocation_iterator i = si->begin_relocations(), |
| 154 | e = si->end_relocations(); i != e; i.increment(err)) { |
| 155 | Check(err); |
| 156 | |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 157 | // If it's the first relocation in this section, find its SectionID |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 158 | if (isFirstRelocation) { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 159 | SectionID = findOrEmitSection(*obj, *si, true, LocalSections); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 160 | DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n"); |
| 161 | isFirstRelocation = false; |
| 162 | } |
| 163 | |
| 164 | ObjRelocationInfo RI; |
| 165 | RI.SectionID = SectionID; |
| 166 | Check(i->getAdditionalInfo(RI.AdditionalInfo)); |
| 167 | Check(i->getOffset(RI.Offset)); |
| 168 | Check(i->getSymbol(RI.Symbol)); |
| 169 | Check(i->getType(RI.Type)); |
| 170 | |
| 171 | DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo |
| 172 | << " Offset: " << format("%p", (uintptr_t)RI.Offset) |
| 173 | << " Type: " << (uint32_t)(RI.Type & 0xffffffffL) |
| 174 | << "\n"); |
| 175 | processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs); |
| 176 | } |
| 177 | } |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 178 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 179 | return obj.take(); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 182 | void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj, |
| 183 | const CommonSymbolMap &CommonSymbols, |
| 184 | uint64_t TotalSize, |
| 185 | SymbolTableMap &SymbolTable) { |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 186 | // Allocate memory for the section |
| 187 | unsigned SectionID = Sections.size(); |
| 188 | uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*), |
| 189 | SectionID); |
| 190 | if (!Addr) |
| 191 | report_fatal_error("Unable to allocate memory for common symbols!"); |
| 192 | uint64_t Offset = 0; |
| 193 | Sections.push_back(SectionEntry(Addr, TotalSize, TotalSize, 0)); |
| 194 | memset(Addr, 0, TotalSize); |
| 195 | |
| 196 | DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID |
| 197 | << " new addr: " << format("%p", Addr) |
| 198 | << " DataSize: " << TotalSize |
| 199 | << "\n"); |
| 200 | |
| 201 | // Assign the address of each symbol |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 202 | for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(), |
| 203 | itEnd = CommonSymbols.end(); it != itEnd; it++) { |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 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); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 207 | SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset); |
| 208 | uint64_t Size = it->second; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 209 | Offset += Size; |
| 210 | Addr += Size; |
| 211 | } |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 214 | unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj, |
| 215 | const SectionRef &Section, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 216 | bool IsCode) { |
| 217 | |
| 218 | unsigned StubBufSize = 0, |
| 219 | StubSize = getMaxStubSize(); |
| 220 | error_code err; |
| 221 | if (StubSize > 0) { |
| 222 | for (relocation_iterator i = Section.begin_relocations(), |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 223 | e = Section.end_relocations(); i != e; i.increment(err), Check(err)) |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 224 | StubBufSize += StubSize; |
| 225 | } |
| 226 | StringRef data; |
| 227 | uint64_t Alignment64; |
| 228 | Check(Section.getContents(data)); |
| 229 | Check(Section.getAlignment(Alignment64)); |
| 230 | |
| 231 | unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 232 | bool IsRequired; |
| 233 | bool IsVirtual; |
| 234 | bool IsZeroInit; |
| 235 | uint64_t DataSize; |
| 236 | Check(Section.isRequiredForExecution(IsRequired)); |
| 237 | Check(Section.isVirtual(IsVirtual)); |
| 238 | Check(Section.isZeroInit(IsZeroInit)); |
| 239 | Check(Section.getSize(DataSize)); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 240 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 241 | unsigned Allocate; |
| 242 | unsigned SectionID = Sections.size(); |
| 243 | uint8_t *Addr; |
| 244 | const char *pData = 0; |
| 245 | |
| 246 | // Some sections, such as debug info, don't need to be loaded for execution. |
| 247 | // Leave those where they are. |
| 248 | if (IsRequired) { |
| 249 | Allocate = DataSize + StubBufSize; |
| 250 | Addr = IsCode |
| 251 | ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID) |
| 252 | : MemMgr->allocateDataSection(Allocate, Alignment, SectionID); |
| 253 | if (!Addr) |
| 254 | report_fatal_error("Unable to allocate section memory!"); |
| 255 | |
| 256 | // Virtual sections have no data in the object image, so leave pData = 0 |
| 257 | if (!IsVirtual) |
| 258 | pData = data.data(); |
| 259 | |
| 260 | // Zero-initialize or copy the data from the image |
| 261 | if (IsZeroInit || IsVirtual) |
| 262 | memset(Addr, 0, DataSize); |
| 263 | else |
| 264 | memcpy(Addr, pData, DataSize); |
| 265 | |
| 266 | DEBUG(dbgs() << "emitSection SectionID: " << SectionID |
| 267 | << " obj addr: " << format("%p", pData) |
| 268 | << " new addr: " << format("%p", Addr) |
| 269 | << " DataSize: " << DataSize |
| 270 | << " StubBufSize: " << StubBufSize |
| 271 | << " Allocate: " << Allocate |
| 272 | << "\n"); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 273 | Obj.updateSectionAddress(Section, (uint64_t)Addr); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 274 | } |
| 275 | else { |
| 276 | // 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] | 277 | // to handle later processing (and by 'handle' I mean don't do anything |
| 278 | // with these sections). |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 279 | Allocate = 0; |
| 280 | Addr = 0; |
| 281 | DEBUG(dbgs() << "emitSection SectionID: " << SectionID |
| 282 | << " obj addr: " << format("%p", data.data()) |
| 283 | << " new addr: 0" |
| 284 | << " DataSize: " << DataSize |
| 285 | << " StubBufSize: " << StubBufSize |
| 286 | << " Allocate: " << Allocate |
| 287 | << "\n"); |
| 288 | } |
| 289 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 290 | Sections.push_back(SectionEntry(Addr, Allocate, DataSize,(uintptr_t)pData)); |
| 291 | return SectionID; |
| 292 | } |
| 293 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 294 | unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj, |
| 295 | const SectionRef &Section, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 296 | bool IsCode, |
| 297 | ObjSectionToIDMap &LocalSections) { |
| 298 | |
| 299 | unsigned SectionID = 0; |
| 300 | ObjSectionToIDMap::iterator i = LocalSections.find(Section); |
| 301 | if (i != LocalSections.end()) |
| 302 | SectionID = i->second; |
| 303 | else { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 304 | SectionID = emitSection(Obj, Section, IsCode); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 305 | LocalSections[Section] = SectionID; |
| 306 | } |
| 307 | return SectionID; |
| 308 | } |
| 309 | |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 310 | void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE, |
| 311 | unsigned SectionID) { |
| 312 | Relocations[SectionID].push_back(RE); |
| 313 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 314 | |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 315 | void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE, |
| 316 | StringRef SymbolName) { |
| 317 | // Relocation by symbol. If the symbol is found in the global symbol table, |
| 318 | // create an appropriate section relocation. Otherwise, add it to |
| 319 | // ExternalSymbolRelocations. |
| 320 | SymbolTableMap::const_iterator Loc = |
| 321 | GlobalSymbolTable.find(SymbolName); |
| 322 | if (Loc == GlobalSymbolTable.end()) { |
| 323 | ExternalSymbolRelocations[SymbolName].push_back(RE); |
Eli Bendersky | 37bc5a2 | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 324 | } else { |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 325 | // Copy the RE since we want to modify its addend. |
| 326 | RelocationEntry RECopy = RE; |
| 327 | RECopy.Addend += Loc->second.second; |
| 328 | Relocations[Loc->second.first].push_back(RECopy); |
Eli Bendersky | 37bc5a2 | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 329 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 333 | if (Arch == Triple::arm) { |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 334 | // TODO: There is only ARM far stub now. We should add the Thumb stub, |
| 335 | // and stubs for branches Thumb - ARM and ARM - Thumb. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 336 | uint32_t *StubAddr = (uint32_t*)Addr; |
| 337 | *StubAddr = 0xe51ff004; // ldr pc,<label> |
| 338 | return (uint8_t*)++StubAddr; |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 339 | } else if (Arch == Triple::mipsel) { |
| 340 | uint32_t *StubAddr = (uint32_t*)Addr; |
| 341 | // 0: 3c190000 lui t9,%hi(addr). |
| 342 | // 4: 27390000 addiu t9,t9,%lo(addr). |
| 343 | // 8: 03200008 jr t9. |
| 344 | // c: 00000000 nop. |
| 345 | const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000; |
| 346 | const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0; |
| 347 | |
| 348 | *StubAddr = LuiT9Instr; |
| 349 | StubAddr++; |
| 350 | *StubAddr = AdduiT9Instr; |
| 351 | StubAddr++; |
| 352 | *StubAddr = JrT9Instr; |
| 353 | StubAddr++; |
| 354 | *StubAddr = NopInstr; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 355 | return Addr; |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 356 | } |
| 357 | return Addr; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 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) { |
Eric Christopher | a7ca3c2 | 2012-10-12 02:04:47 +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) << ")" |
| 386 | << " RelType: " << RE.RelType |
| 387 | << " Addend: " << RE.Addend |
| 388 | << "\n"); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 389 | |
Eric Christopher | a7ca3c2 | 2012-10-12 02:04:47 +0000 | [diff] [blame] | 390 | resolveRelocation(Target, Sections[RE.SectionID].LoadAddress + RE.Offset, |
| 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 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 436 | ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *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 | 35ed842 | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 478 | uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) { |
| 479 | return Dyld->getSymbolLoadAddress(Name); |
| 480 | } |
| 481 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 482 | void RuntimeDyld::resolveRelocations() { |
| 483 | Dyld->resolveRelocations(); |
| 484 | } |
| 485 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 486 | void RuntimeDyld::reassignSectionAddress(unsigned SectionID, |
| 487 | uint64_t Addr) { |
| 488 | Dyld->reassignSectionAddress(SectionID, Addr); |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Jim Grosbach | e940c1b | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 491 | void RuntimeDyld::mapSectionAddress(const void *LocalAddress, |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 492 | uint64_t TargetAddress) { |
| 493 | Dyld->mapSectionAddress(LocalAddress, TargetAddress); |
| 494 | } |
| 495 | |
Jim Grosbach | 91dde15 | 2011-03-22 18:22:27 +0000 | [diff] [blame] | 496 | StringRef RuntimeDyld::getErrorString() { |
Jim Grosbach | b3eecaf | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 497 | return Dyld->getErrorString(); |
| 498 | } |
| 499 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 500 | } // end namespace llvm |