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