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" |
Tim Northover | f00677d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MathExtras.h" |
Eli Bendersky | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 21 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | using namespace llvm::object; |
| 24 | |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 25 | // Empty out-of-line virtual destructor as the key function. |
| 26 | RTDyldMemoryManager::~RTDyldMemoryManager() {} |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 27 | RuntimeDyldImpl::~RuntimeDyldImpl() {} |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 28 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 29 | namespace llvm { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 30 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 31 | // Resolve the relocations for all symbols we currently know about. |
| 32 | void RuntimeDyldImpl::resolveRelocations() { |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 33 | // First, resolve relocations associated with external symbols. |
Eli Bendersky | 37bc5a2 | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 34 | resolveExternalSymbols(); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 35 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 36 | // Just iterate over the sections we have and resolve all the relocations |
| 37 | // in them. Gross overkill, but it gets the job done. |
| 38 | for (int i = 0, e = Sections.size(); i != e; ++i) { |
Andrew Kaylor | 2898988 | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 39 | uint64_t Addr = Sections[i].LoadAddress; |
| 40 | DEBUG(dbgs() << "Resolving relocations Section #" << i |
| 41 | << "\t" << format("%p", (uint8_t *)Addr) |
| 42 | << "\n"); |
| 43 | resolveRelocationList(Relocations[i], Addr); |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 44 | } |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Jim Grosbach | e940c1b | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 47 | void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress, |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 48 | uint64_t TargetAddress) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 49 | for (unsigned i = 0, e = Sections.size(); i != e; ++i) { |
| 50 | if (Sections[i].Address == LocalAddress) { |
| 51 | reassignSectionAddress(i, TargetAddress); |
| 52 | return; |
| 53 | } |
| 54 | } |
| 55 | llvm_unreachable("Attempting to remap address of unknown section!"); |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 58 | // Subclasses can implement this method to create specialized image instances. |
Sylvestre Ledru | c8e41c5 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 59 | // The caller owns the pointer that is returned. |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 60 | ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) { |
| 61 | return new ObjectImageCommon(InputBuffer); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 64 | ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 65 | OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer)); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 66 | if (!obj) |
| 67 | report_fatal_error("Unable to create object image from memory buffer!"); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 68 | |
| 69 | Arch = (Triple::ArchType)obj->getArch(); |
| 70 | |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 71 | // Symbols found in this object |
| 72 | StringMap<SymbolLoc> LocalSymbols; |
| 73 | // Used sections from the object file |
| 74 | ObjSectionToIDMap LocalSections; |
| 75 | |
Tim Northover | f00677d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 76 | // Common symbols requiring allocation, with their sizes and alignments |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 77 | CommonSymbolMap CommonSymbols; |
Tim Northover | f00677d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 78 | // Maximum required total memory to allocate all common symbols |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 79 | uint64_t CommonSize = 0; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 80 | |
| 81 | error_code err; |
| 82 | // Parse symbols |
| 83 | DEBUG(dbgs() << "Parse symbols:\n"); |
| 84 | for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols(); |
| 85 | i != e; i.increment(err)) { |
| 86 | Check(err); |
| 87 | object::SymbolRef::Type SymType; |
| 88 | StringRef Name; |
| 89 | Check(i->getType(SymType)); |
| 90 | Check(i->getName(Name)); |
| 91 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 92 | uint32_t flags; |
| 93 | Check(i->getFlags(flags)); |
| 94 | |
| 95 | bool isCommon = flags & SymbolRef::SF_Common; |
| 96 | if (isCommon) { |
| 97 | // Add the common symbols to a list. We'll allocate them all below. |
Tim Northover | f00677d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 98 | uint64_t Align = getCommonSymbolAlignment(*i); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 99 | uint64_t Size = 0; |
| 100 | Check(i->getSize(Size)); |
Tim Northover | f00677d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 101 | CommonSize += Size + Align; |
| 102 | CommonSymbols[*i] = CommonSymbolInfo(Size, Align); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 103 | } else { |
| 104 | if (SymType == object::SymbolRef::ST_Function || |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 105 | SymType == object::SymbolRef::ST_Data || |
| 106 | SymType == object::SymbolRef::ST_Unknown) { |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 107 | uint64_t FileOffset; |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 108 | StringRef SectionData; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 109 | section_iterator si = obj->end_sections(); |
| 110 | Check(i->getFileOffset(FileOffset)); |
| 111 | Check(i->getSection(si)); |
| 112 | if (si == obj->end_sections()) continue; |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 113 | Check(si->getContents(SectionData)); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 114 | const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() + |
| 115 | (uintptr_t)FileOffset; |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 116 | uintptr_t SectOffset = (uintptr_t)(SymPtr - |
| 117 | (const uint8_t*)SectionData.begin()); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 118 | unsigned SectionID = |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 119 | findOrEmitSection(*obj, |
| 120 | *si, |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 121 | SymType == object::SymbolRef::ST_Function, |
| 122 | LocalSections); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 123 | LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset); |
| 124 | DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset) |
| 125 | << " flags: " << flags |
| 126 | << " SID: " << SectionID |
| 127 | << " Offset: " << format("%p", SectOffset)); |
Amara Emerson | 098d6d5 | 2012-11-16 11:11:59 +0000 | [diff] [blame^] | 128 | GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 129 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 130 | } |
| 131 | DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n"); |
| 132 | } |
| 133 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 134 | // Allocate common symbols |
| 135 | if (CommonSize != 0) |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 136 | emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 137 | |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 138 | // Parse and process relocations |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 139 | DEBUG(dbgs() << "Parse relocations:\n"); |
| 140 | for (section_iterator si = obj->begin_sections(), |
| 141 | se = obj->end_sections(); si != se; si.increment(err)) { |
| 142 | Check(err); |
| 143 | bool isFirstRelocation = true; |
| 144 | unsigned SectionID = 0; |
| 145 | StubMap Stubs; |
| 146 | |
| 147 | for (relocation_iterator i = si->begin_relocations(), |
| 148 | e = si->end_relocations(); i != e; i.increment(err)) { |
| 149 | Check(err); |
| 150 | |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 151 | // If it's the first relocation in this section, find its SectionID |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 152 | if (isFirstRelocation) { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 153 | SectionID = findOrEmitSection(*obj, *si, true, LocalSections); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 154 | DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n"); |
| 155 | isFirstRelocation = false; |
| 156 | } |
| 157 | |
| 158 | ObjRelocationInfo RI; |
| 159 | RI.SectionID = SectionID; |
| 160 | Check(i->getAdditionalInfo(RI.AdditionalInfo)); |
| 161 | Check(i->getOffset(RI.Offset)); |
| 162 | Check(i->getSymbol(RI.Symbol)); |
| 163 | Check(i->getType(RI.Type)); |
| 164 | |
| 165 | DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo |
| 166 | << " Offset: " << format("%p", (uintptr_t)RI.Offset) |
| 167 | << " Type: " << (uint32_t)(RI.Type & 0xffffffffL) |
| 168 | << "\n"); |
| 169 | processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs); |
| 170 | } |
| 171 | } |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 172 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 173 | return obj.take(); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 176 | void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj, |
| 177 | const CommonSymbolMap &CommonSymbols, |
| 178 | uint64_t TotalSize, |
| 179 | SymbolTableMap &SymbolTable) { |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 180 | // Allocate memory for the section |
| 181 | unsigned SectionID = Sections.size(); |
| 182 | uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*), |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 183 | SectionID, false); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 184 | if (!Addr) |
| 185 | report_fatal_error("Unable to allocate memory for common symbols!"); |
| 186 | uint64_t Offset = 0; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 187 | Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, TotalSize, 0)); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 188 | memset(Addr, 0, TotalSize); |
| 189 | |
| 190 | DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID |
| 191 | << " new addr: " << format("%p", Addr) |
| 192 | << " DataSize: " << TotalSize |
| 193 | << "\n"); |
| 194 | |
| 195 | // Assign the address of each symbol |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 196 | for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(), |
| 197 | itEnd = CommonSymbols.end(); it != itEnd; it++) { |
Tim Northover | f00677d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 198 | uint64_t Size = it->second.first; |
| 199 | uint64_t Align = it->second.second; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 200 | StringRef Name; |
| 201 | it->first.getName(Name); |
Tim Northover | f00677d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 202 | if (Align) { |
| 203 | // This symbol has an alignment requirement. |
| 204 | uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align); |
| 205 | Addr += AlignOffset; |
| 206 | Offset += AlignOffset; |
| 207 | DEBUG(dbgs() << "Allocating common symbol " << Name << " address " << |
Andrew Kaylor | 5fc8c7c | 2012-11-01 19:49:21 +0000 | [diff] [blame] | 208 | format("%p\n", Addr)); |
Tim Northover | f00677d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 209 | } |
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); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 212 | Offset += Size; |
| 213 | Addr += Size; |
| 214 | } |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 217 | unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj, |
| 218 | const SectionRef &Section, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 219 | bool IsCode) { |
| 220 | |
| 221 | unsigned StubBufSize = 0, |
| 222 | StubSize = getMaxStubSize(); |
| 223 | error_code err; |
| 224 | if (StubSize > 0) { |
| 225 | for (relocation_iterator i = Section.begin_relocations(), |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 226 | e = Section.end_relocations(); i != e; i.increment(err), Check(err)) |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 227 | StubBufSize += StubSize; |
| 228 | } |
| 229 | StringRef data; |
| 230 | uint64_t Alignment64; |
| 231 | Check(Section.getContents(data)); |
| 232 | Check(Section.getAlignment(Alignment64)); |
| 233 | |
| 234 | unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 235 | bool IsRequired; |
| 236 | bool IsVirtual; |
| 237 | bool IsZeroInit; |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 238 | bool IsReadOnly; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 239 | uint64_t DataSize; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 240 | StringRef Name; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 241 | Check(Section.isRequiredForExecution(IsRequired)); |
| 242 | Check(Section.isVirtual(IsVirtual)); |
| 243 | Check(Section.isZeroInit(IsZeroInit)); |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 244 | Check(Section.isReadOnlyData(IsReadOnly)); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 245 | Check(Section.getSize(DataSize)); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 246 | Check(Section.getName(Name)); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 247 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 248 | unsigned Allocate; |
| 249 | unsigned SectionID = Sections.size(); |
| 250 | uint8_t *Addr; |
| 251 | const char *pData = 0; |
| 252 | |
| 253 | // Some sections, such as debug info, don't need to be loaded for execution. |
| 254 | // Leave those where they are. |
| 255 | if (IsRequired) { |
| 256 | Allocate = DataSize + StubBufSize; |
| 257 | Addr = IsCode |
| 258 | ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID) |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 259 | : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, IsReadOnly); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 260 | if (!Addr) |
| 261 | report_fatal_error("Unable to allocate section memory!"); |
| 262 | |
| 263 | // Virtual sections have no data in the object image, so leave pData = 0 |
| 264 | if (!IsVirtual) |
| 265 | pData = data.data(); |
| 266 | |
| 267 | // Zero-initialize or copy the data from the image |
| 268 | if (IsZeroInit || IsVirtual) |
| 269 | memset(Addr, 0, DataSize); |
| 270 | else |
| 271 | memcpy(Addr, pData, DataSize); |
| 272 | |
| 273 | DEBUG(dbgs() << "emitSection SectionID: " << SectionID |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 274 | << " Name: " << Name |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 275 | << " obj addr: " << format("%p", pData) |
| 276 | << " new addr: " << format("%p", Addr) |
| 277 | << " DataSize: " << DataSize |
| 278 | << " StubBufSize: " << StubBufSize |
| 279 | << " Allocate: " << Allocate |
| 280 | << "\n"); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 281 | Obj.updateSectionAddress(Section, (uint64_t)Addr); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 282 | } |
| 283 | else { |
| 284 | // 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] | 285 | // to handle later processing (and by 'handle' I mean don't do anything |
| 286 | // with these sections). |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 287 | Allocate = 0; |
| 288 | Addr = 0; |
| 289 | DEBUG(dbgs() << "emitSection SectionID: " << SectionID |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 290 | << " Name: " << Name |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 291 | << " obj addr: " << format("%p", data.data()) |
| 292 | << " new addr: 0" |
| 293 | << " DataSize: " << DataSize |
| 294 | << " StubBufSize: " << StubBufSize |
| 295 | << " Allocate: " << Allocate |
| 296 | << "\n"); |
| 297 | } |
| 298 | |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 299 | Sections.push_back(SectionEntry(Name, Addr, Allocate, DataSize, |
| 300 | (uintptr_t)pData)); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 301 | return SectionID; |
| 302 | } |
| 303 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 304 | unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj, |
| 305 | const SectionRef &Section, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 306 | bool IsCode, |
| 307 | ObjSectionToIDMap &LocalSections) { |
| 308 | |
| 309 | unsigned SectionID = 0; |
| 310 | ObjSectionToIDMap::iterator i = LocalSections.find(Section); |
| 311 | if (i != LocalSections.end()) |
| 312 | SectionID = i->second; |
| 313 | else { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 314 | SectionID = emitSection(Obj, Section, IsCode); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 315 | LocalSections[Section] = SectionID; |
| 316 | } |
| 317 | return SectionID; |
| 318 | } |
| 319 | |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 320 | void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE, |
| 321 | unsigned SectionID) { |
| 322 | Relocations[SectionID].push_back(RE); |
| 323 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 324 | |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 325 | void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE, |
| 326 | StringRef SymbolName) { |
| 327 | // Relocation by symbol. If the symbol is found in the global symbol table, |
| 328 | // create an appropriate section relocation. Otherwise, add it to |
| 329 | // ExternalSymbolRelocations. |
| 330 | SymbolTableMap::const_iterator Loc = |
| 331 | GlobalSymbolTable.find(SymbolName); |
| 332 | if (Loc == GlobalSymbolTable.end()) { |
| 333 | ExternalSymbolRelocations[SymbolName].push_back(RE); |
Eli Bendersky | 37bc5a2 | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 334 | } else { |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 335 | // Copy the RE since we want to modify its addend. |
| 336 | RelocationEntry RECopy = RE; |
| 337 | RECopy.Addend += Loc->second.second; |
| 338 | Relocations[Loc->second.first].push_back(RECopy); |
Eli Bendersky | 37bc5a2 | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 339 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 343 | if (Arch == Triple::arm) { |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 344 | // TODO: There is only ARM far stub now. We should add the Thumb stub, |
| 345 | // and stubs for branches Thumb - ARM and ARM - Thumb. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 346 | uint32_t *StubAddr = (uint32_t*)Addr; |
| 347 | *StubAddr = 0xe51ff004; // ldr pc,<label> |
| 348 | return (uint8_t*)++StubAddr; |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 349 | } else if (Arch == Triple::mipsel) { |
| 350 | uint32_t *StubAddr = (uint32_t*)Addr; |
| 351 | // 0: 3c190000 lui t9,%hi(addr). |
| 352 | // 4: 27390000 addiu t9,t9,%lo(addr). |
| 353 | // 8: 03200008 jr t9. |
| 354 | // c: 00000000 nop. |
| 355 | const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000; |
| 356 | const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0; |
| 357 | |
| 358 | *StubAddr = LuiT9Instr; |
| 359 | StubAddr++; |
| 360 | *StubAddr = AdduiT9Instr; |
| 361 | StubAddr++; |
| 362 | *StubAddr = JrT9Instr; |
| 363 | StubAddr++; |
| 364 | *StubAddr = NopInstr; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 365 | return Addr; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 366 | } else if (Arch == Triple::ppc64) { |
| 367 | // PowerPC64 stub: the address points to a function descriptor |
| 368 | // instead of the function itself. Load the function address |
| 369 | // on r11 and sets it to control register. Also loads the function |
| 370 | // TOC in r2 and environment pointer to r11. |
| 371 | writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr) |
| 372 | writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr) |
| 373 | writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32 |
| 374 | writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr) |
| 375 | writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr) |
| 376 | writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1) |
| 377 | writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12) |
| 378 | writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12) |
| 379 | writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11 |
| 380 | writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2) |
| 381 | writeInt32BE(Addr+40, 0x4E800420); // bctr |
Andrew Kaylor | 5fc8c7c | 2012-11-01 19:49:21 +0000 | [diff] [blame] | 382 | |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 383 | return Addr; |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 384 | } |
| 385 | return Addr; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // Assign an address to a symbol name and resolve all the relocations |
| 389 | // associated with it. |
| 390 | void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID, |
| 391 | uint64_t Addr) { |
| 392 | // The address to use for relocation resolution is not |
| 393 | // the address of the local section buffer. We must be doing |
Andrew Kaylor | 2898988 | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 394 | // a remote execution environment of some sort. Relocations can't |
| 395 | // be applied until all the sections have been moved. The client must |
| 396 | // trigger this with a call to MCJIT::finalize() or |
| 397 | // RuntimeDyld::resolveRelocations(). |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 398 | // |
| 399 | // Addr is a uint64_t because we can't assume the pointer width |
| 400 | // of the target is the same as that of the host. Just use a generic |
| 401 | // "big enough" type. |
| 402 | Sections[SectionID].LoadAddress = Addr; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | void RuntimeDyldImpl::resolveRelocationEntry(const RelocationEntry &RE, |
| 406 | uint64_t Value) { |
Eric Christopher | a7ca3c2 | 2012-10-12 02:04:47 +0000 | [diff] [blame] | 407 | // Ignore relocations for sections that were not loaded |
| 408 | if (Sections[RE.SectionID].Address != 0) { |
Eric Christopher | a7ca3c2 | 2012-10-12 02:04:47 +0000 | [diff] [blame] | 409 | DEBUG(dbgs() << "\tSectionID: " << RE.SectionID |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 410 | << " + " << RE.Offset << " (" |
| 411 | << format("%p", Sections[RE.SectionID].Address + RE.Offset) << ")" |
Eric Christopher | a7ca3c2 | 2012-10-12 02:04:47 +0000 | [diff] [blame] | 412 | << " RelType: " << RE.RelType |
| 413 | << " Addend: " << RE.Addend |
| 414 | << "\n"); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 415 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 416 | resolveRelocation(Sections[RE.SectionID], RE.Offset, |
Eric Christopher | a7ca3c2 | 2012-10-12 02:04:47 +0000 | [diff] [blame] | 417 | Value, RE.RelType, RE.Addend); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 418 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, |
| 422 | uint64_t Value) { |
| 423 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
| 424 | resolveRelocationEntry(Relocs[i], Value); |
| 425 | } |
| 426 | } |
| 427 | |
Eli Bendersky | 37bc5a2 | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 428 | void RuntimeDyldImpl::resolveExternalSymbols() { |
| 429 | StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(), |
| 430 | e = ExternalSymbolRelocations.end(); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 431 | for (; i != e; i++) { |
| 432 | StringRef Name = i->first(); |
| 433 | RelocationList &Relocs = i->second; |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 434 | SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name); |
| 435 | if (Loc == GlobalSymbolTable.end()) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 436 | // This is an external symbol, try to get it address from |
| 437 | // MemoryManager. |
| 438 | uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(), |
| 439 | true); |
| 440 | DEBUG(dbgs() << "Resolving relocations Name: " << Name |
| 441 | << "\t" << format("%p", Addr) |
| 442 | << "\n"); |
| 443 | resolveRelocationList(Relocs, (uintptr_t)Addr); |
| 444 | } else { |
Eli Bendersky | 37bc5a2 | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 445 | report_fatal_error("Expected external symbol"); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 451 | //===----------------------------------------------------------------------===// |
| 452 | // RuntimeDyld class implementation |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 453 | RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) { |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 454 | // FIXME: There's a potential issue lurking here if a single instance of |
| 455 | // RuntimeDyld is used to load multiple objects. The current implementation |
| 456 | // associates a single memory manager with a RuntimeDyld instance. Even |
| 457 | // though the public class spawns a new 'impl' instance for each load, |
| 458 | // they share a single memory manager. This can become a problem when page |
| 459 | // permissions are applied. |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 460 | Dyld = 0; |
| 461 | MM = mm; |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | RuntimeDyld::~RuntimeDyld() { |
| 465 | delete Dyld; |
| 466 | } |
| 467 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 468 | ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 469 | if (!Dyld) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 470 | sys::LLVMFileType type = sys::IdentifyFileType( |
| 471 | InputBuffer->getBufferStart(), |
| 472 | static_cast<unsigned>(InputBuffer->getBufferSize())); |
| 473 | switch (type) { |
| 474 | case sys::ELF_Relocatable_FileType: |
| 475 | case sys::ELF_Executable_FileType: |
| 476 | case sys::ELF_SharedObject_FileType: |
| 477 | case sys::ELF_Core_FileType: |
| 478 | Dyld = new RuntimeDyldELF(MM); |
| 479 | break; |
| 480 | case sys::Mach_O_Object_FileType: |
| 481 | case sys::Mach_O_Executable_FileType: |
| 482 | case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: |
| 483 | case sys::Mach_O_Core_FileType: |
| 484 | case sys::Mach_O_PreloadExecutable_FileType: |
| 485 | case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: |
| 486 | case sys::Mach_O_DynamicLinker_FileType: |
| 487 | case sys::Mach_O_Bundle_FileType: |
| 488 | case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: |
| 489 | case sys::Mach_O_DSYMCompanion_FileType: |
| 490 | Dyld = new RuntimeDyldMachO(MM); |
| 491 | break; |
| 492 | case sys::Unknown_FileType: |
| 493 | case sys::Bitcode_FileType: |
| 494 | case sys::Archive_FileType: |
| 495 | case sys::COFF_FileType: |
| 496 | report_fatal_error("Incompatible object format!"); |
| 497 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 498 | } else { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 499 | if (!Dyld->isCompatibleFormat(InputBuffer)) |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 500 | report_fatal_error("Incompatible object format!"); |
| 501 | } |
| 502 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 503 | return Dyld->loadObject(InputBuffer); |
| 504 | } |
| 505 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 506 | void *RuntimeDyld::getSymbolAddress(StringRef Name) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 507 | return Dyld->getSymbolAddress(Name); |
| 508 | } |
| 509 | |
Jim Grosbach | 35ed842 | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 510 | uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) { |
| 511 | return Dyld->getSymbolLoadAddress(Name); |
| 512 | } |
| 513 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 514 | void RuntimeDyld::resolveRelocations() { |
| 515 | Dyld->resolveRelocations(); |
| 516 | } |
| 517 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 518 | void RuntimeDyld::reassignSectionAddress(unsigned SectionID, |
| 519 | uint64_t Addr) { |
| 520 | Dyld->reassignSectionAddress(SectionID, Addr); |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Jim Grosbach | e940c1b | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 523 | void RuntimeDyld::mapSectionAddress(const void *LocalAddress, |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 524 | uint64_t TargetAddress) { |
| 525 | Dyld->mapSectionAddress(LocalAddress, TargetAddress); |
| 526 | } |
| 527 | |
Jim Grosbach | 91dde15 | 2011-03-22 18:22:27 +0000 | [diff] [blame] | 528 | StringRef RuntimeDyld::getErrorString() { |
Jim Grosbach | b3eecaf | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 529 | return Dyld->getErrorString(); |
| 530 | } |
| 531 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 532 | } // end namespace llvm |