blob: e010785c44672969abd155bc6b6902db5060c327 [file] [log] [blame]
Jim Grosbache0934be2012-01-16 23:50:58 +00001//===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
Eli Benderskya66a1852012-01-16 08:56:09 +00002//
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 ELF support for the MC-JIT runtime dynamic linker.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dyld"
15#include "llvm/ADT/OwningPtr.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/IntervalMap.h"
Eli Bendersky76463fd2012-01-22 07:05:02 +000019#include "RuntimeDyldELF.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000020#include "llvm/Object/ObjectFile.h"
21#include "llvm/Support/ELF.h"
22#include "llvm/ADT/Triple.h"
Preston Gurd689ff9c2012-04-16 22:12:58 +000023#include "llvm/Object/ELF.h"
24#include "JITRegistrar.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000025using namespace llvm;
26using namespace llvm::object;
27
Preston Gurd689ff9c2012-04-16 22:12:58 +000028namespace {
29
30template<support::endianness target_endianness, bool is64Bits>
31class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> {
32 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
33
34 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
35 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
36 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
37 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
38
39 typedef typename ELFObjectFile<target_endianness, is64Bits>::
40 Elf_Ehdr Elf_Ehdr;
41
42 typedef typename ELFDataTypeTypedefHelper<
43 target_endianness, is64Bits>::value_type addr_type;
44
45protected:
46 // This duplicates the 'Data' member in the 'Binary' base class
47 // but it is necessary to workaround a bug in gcc 4.2
48 MemoryBuffer *InputData;
49
50public:
51 DyldELFObject(MemoryBuffer *Object, error_code &ec);
52
53 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
54 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
55
56 const MemoryBuffer& getBuffer() const { return *InputData; }
57
58 // Methods for type inquiry through isa, cast, and dyn_cast
59 static inline bool classof(const Binary *v) {
60 return (isa<ELFObjectFile<target_endianness, is64Bits> >(v)
61 && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v)));
62 }
63 static inline bool classof(
64 const ELFObjectFile<target_endianness, is64Bits> *v) {
65 return v->isDyldType();
66 }
67 static inline bool classof(const DyldELFObject *v) {
68 return true;
69 }
70};
71
72template<support::endianness target_endianness, bool is64Bits>
73class ELFObjectImage : public ObjectImage {
74 protected:
75 DyldELFObject<target_endianness, is64Bits> *DyldObj;
76 bool Registered;
77
78 public:
79 ELFObjectImage(DyldELFObject<target_endianness, is64Bits> *Obj)
80 : ObjectImage(Obj),
81 DyldObj(Obj),
82 Registered(false) {}
83
84 virtual ~ELFObjectImage() {
85 if (Registered)
86 deregisterWithDebugger();
87 }
88
89 // Subclasses can override these methods to update the image with loaded
90 // addresses for sections and common symbols
91 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
92 {
93 DyldObj->updateSectionAddress(Sec, Addr);
94 }
95
96 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
97 {
98 DyldObj->updateSymbolAddress(Sym, Addr);
99 }
100
101 virtual void registerWithDebugger()
102 {
103 JITRegistrar::getGDBRegistrar().registerObject(DyldObj->getBuffer());
104 Registered = true;
105 }
106 virtual void deregisterWithDebugger()
107 {
108 JITRegistrar::getGDBRegistrar().deregisterObject(DyldObj->getBuffer());
109 }
110};
111
112template<support::endianness target_endianness, bool is64Bits>
113DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Object,
114 error_code &ec)
115 : ELFObjectFile<target_endianness, is64Bits>(Object, ec),
116 InputData(Object) {
117 this->isDyldELFObject = true;
118}
119
120template<support::endianness target_endianness, bool is64Bits>
121void DyldELFObject<target_endianness, is64Bits>::updateSectionAddress(
122 const SectionRef &Sec,
123 uint64_t Addr) {
124 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
125 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
126 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
127
128 // This assumes the address passed in matches the target address bitness
129 // The template-based type cast handles everything else.
130 shdr->sh_addr = static_cast<addr_type>(Addr);
131}
132
133template<support::endianness target_endianness, bool is64Bits>
134void DyldELFObject<target_endianness, is64Bits>::updateSymbolAddress(
135 const SymbolRef &SymRef,
136 uint64_t Addr) {
137
138 Elf_Sym *sym = const_cast<Elf_Sym*>(
139 ELFObjectFile<target_endianness, is64Bits>::
140 getSymbol(SymRef.getRawDataRefImpl()));
141
142 // This assumes the address passed in matches the target address bitness
143 // The template-based type cast handles everything else.
144 sym->st_value = static_cast<addr_type>(Addr);
145}
146
147} // namespace
148
149
Eli Benderskya66a1852012-01-16 08:56:09 +0000150namespace llvm {
151
Preston Gurd689ff9c2012-04-16 22:12:58 +0000152ObjectImage *RuntimeDyldELF::createObjectImage(
153 const MemoryBuffer *ConstInputBuffer) {
154 MemoryBuffer *InputBuffer = const_cast<MemoryBuffer*>(ConstInputBuffer);
155 std::pair<unsigned char, unsigned char> Ident = getElfArchType(InputBuffer);
156 error_code ec;
157
158 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
159 DyldELFObject<support::little, false> *Obj =
160 new DyldELFObject<support::little, false>(InputBuffer, ec);
161 return new ELFObjectImage<support::little, false>(Obj);
162 }
163 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
164 DyldELFObject<support::big, false> *Obj =
165 new DyldELFObject<support::big, false>(InputBuffer, ec);
166 return new ELFObjectImage<support::big, false>(Obj);
167 }
168 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
169 DyldELFObject<support::big, true> *Obj =
170 new DyldELFObject<support::big, true>(InputBuffer, ec);
171 return new ELFObjectImage<support::big, true>(Obj);
172 }
173 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
174 DyldELFObject<support::little, true> *Obj =
175 new DyldELFObject<support::little, true>(InputBuffer, ec);
176 return new ELFObjectImage<support::little, true>(Obj);
177 }
178 else
179 llvm_unreachable("Unexpected ELF format");
180}
181
182void RuntimeDyldELF::handleObjectLoaded(ObjectImage *Obj)
183{
184 Obj->registerWithDebugger();
185 // Save the loaded object. It will deregister itself when deleted
186 LoadedObject = Obj;
187}
188
189RuntimeDyldELF::~RuntimeDyldELF() {
190 if (LoadedObject)
191 delete LoadedObject;
192}
Eli Benderskya66a1852012-01-16 08:56:09 +0000193
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000194void RuntimeDyldELF::resolveX86_64Relocation(uint8_t *LocalAddress,
195 uint64_t FinalAddress,
196 uint64_t Value,
197 uint32_t Type,
198 int64_t Addend) {
199 switch (Type) {
200 default:
201 llvm_unreachable("Relocation type not implemented yet!");
202 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000203 case ELF::R_X86_64_64: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000204 uint64_t *Target = (uint64_t*)(LocalAddress);
205 *Target = Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000206 break;
207 }
208 case ELF::R_X86_64_32:
209 case ELF::R_X86_64_32S: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000210 Value += Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000211 // FIXME: Handle the possibility of this assertion failing
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000212 assert((Type == ELF::R_X86_64_32 && !(Value & 0xFFFFFFFF00000000ULL)) ||
213 (Type == ELF::R_X86_64_32S &&
Eli Bendersky92238222012-01-16 09:31:10 +0000214 (Value & 0xFFFFFFFF00000000ULL) == 0xFFFFFFFF00000000ULL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000215 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000216 uint32_t *Target = reinterpret_cast<uint32_t*>(LocalAddress);
Eli Benderskya66a1852012-01-16 08:56:09 +0000217 *Target = TruncatedAddr;
218 break;
219 }
220 case ELF::R_X86_64_PC32: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000221 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
222 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
223 assert(RealOffset <= 214783647 && RealOffset >= -214783648);
224 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Eli Benderskya66a1852012-01-16 08:56:09 +0000225 *Placeholder = TruncOffset;
226 break;
227 }
228 }
229}
230
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000231void RuntimeDyldELF::resolveX86Relocation(uint8_t *LocalAddress,
232 uint32_t FinalAddress,
233 uint32_t Value,
234 uint32_t Type,
235 int32_t Addend) {
236 switch (Type) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000237 case ELF::R_386_32: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000238 uint32_t *Target = (uint32_t*)(LocalAddress);
Preston Gurdc68dda82012-04-12 20:13:57 +0000239 uint32_t Placeholder = *Target;
240 *Target = Placeholder + Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000241 break;
242 }
243 case ELF::R_386_PC32: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000244 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
245 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Eli Benderskya66a1852012-01-16 08:56:09 +0000246 *Placeholder = RealOffset;
247 break;
248 }
249 default:
250 // There are other relocation types, but it appears these are the
251 // only ones currently used by the LLVM ELF object writer
Craig Topper85814382012-02-07 05:05:23 +0000252 llvm_unreachable("Relocation type not implemented yet!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000253 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000254 }
255}
256
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000257void RuntimeDyldELF::resolveARMRelocation(uint8_t *LocalAddress,
258 uint32_t FinalAddress,
259 uint32_t Value,
260 uint32_t Type,
261 int32_t Addend) {
262 // TODO: Add Thumb relocations.
263 uint32_t* TargetPtr = (uint32_t*)LocalAddress;
264 Value += Addend;
265
266 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " << LocalAddress
267 << " FinalAddress: " << format("%p",FinalAddress)
268 << " Value: " << format("%x",Value)
269 << " Type: " << format("%x",Type)
270 << " Addend: " << format("%x",Addend)
271 << "\n");
272
273 switch(Type) {
274 default:
275 llvm_unreachable("Not implemented relocation type!");
276
277 // Just write 32bit value to relocation address
278 case ELF::R_ARM_ABS32 :
279 *TargetPtr = Value;
280 break;
281
282 // Write first 16 bit of 32 bit value to the mov instruction.
283 // Last 4 bit should be shifted.
284 case ELF::R_ARM_MOVW_ABS_NC :
285 Value = Value & 0xFFFF;
286 *TargetPtr |= Value & 0xFFF;
287 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
288 break;
289
290 // Write last 16 bit of 32 bit value to the mov instruction.
291 // Last 4 bit should be shifted.
292 case ELF::R_ARM_MOVT_ABS :
293 Value = (Value >> 16) & 0xFFFF;
294 *TargetPtr |= Value & 0xFFF;
295 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
296 break;
297
298 // Write 24 bit relative value to the branch instruction.
299 case ELF::R_ARM_PC24 : // Fall through.
300 case ELF::R_ARM_CALL : // Fall through.
301 case ELF::R_ARM_JUMP24 :
302 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
303 RelValue = (RelValue & 0x03FFFFFC) >> 2;
304 *TargetPtr &= 0xFF000000;
305 *TargetPtr |= RelValue;
306 break;
307 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000308}
309
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000310void RuntimeDyldELF::resolveRelocation(uint8_t *LocalAddress,
311 uint64_t FinalAddress,
312 uint64_t Value,
313 uint32_t Type,
314 int64_t Addend) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000315 switch (Arch) {
316 case Triple::x86_64:
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000317 resolveX86_64Relocation(LocalAddress, FinalAddress, Value, Type, Addend);
Eli Benderskya66a1852012-01-16 08:56:09 +0000318 break;
319 case Triple::x86:
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000320 resolveX86Relocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
321 (uint32_t)(Value & 0xffffffffL), Type,
322 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000323 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000324 case Triple::arm: // Fall through.
325 case Triple::thumb:
326 resolveARMRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
327 (uint32_t)(Value & 0xffffffffL), Type,
328 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000329 break;
Craig Topper85814382012-02-07 05:05:23 +0000330 default: llvm_unreachable("Unsupported CPU type!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000331 }
332}
333
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000334void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000335 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000336 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000337 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000338 StubMap &Stubs) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000339
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000340 uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL);
341 intptr_t Addend = (intptr_t)Rel.AdditionalInfo;
342 RelocationValueRef Value;
343 StringRef TargetName;
344 const SymbolRef &Symbol = Rel.Symbol;
345 Symbol.getName(TargetName);
346 DEBUG(dbgs() << "\t\tRelType: " << RelType
347 << " Addend: " << Addend
348 << " TargetName: " << TargetName
349 << "\n");
350 // First look the symbol in object file symbols.
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000351 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000352 if (lsi != Symbols.end()) {
353 Value.SectionID = lsi->second.first;
354 Value.Addend = lsi->second.second;
355 } else {
356 // Second look the symbol in global symbol table.
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000357 SymbolTableMap::const_iterator gsi =
358 GlobalSymbolTable.find(TargetName.data());
359 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000360 Value.SectionID = gsi->second.first;
361 Value.Addend = gsi->second.second;
362 } else {
363 SymbolRef::Type SymType;
364 Symbol.getType(SymType);
365 switch (SymType) {
366 case SymbolRef::ST_Debug: {
367 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
368 // and can be changed by another developers. Maybe best way is add
369 // a new symbol type ST_Section to SymbolRef and use it.
370 section_iterator si = Obj.end_sections();
371 Symbol.getSection(si);
372 if (si == Obj.end_sections())
373 llvm_unreachable("Symbol section not found, bad object file format!");
374 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Preston Gurd689ff9c2012-04-16 22:12:58 +0000375 Value.SectionID = findOrEmitSection(Obj, (*si), true, ObjSectionToID);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000376 Value.Addend = Addend;
377 break;
378 }
379 case SymbolRef::ST_Unknown: {
380 Value.SymbolName = TargetName.data();
381 Value.Addend = Addend;
382 break;
383 }
384 default:
385 llvm_unreachable("Unresolved symbol type!");
386 break;
387 }
388 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000389 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000390 DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID
391 << " Rel.Offset: " << Rel.Offset
392 << "\n");
393 if (Arch == Triple::arm &&
394 (RelType == ELF::R_ARM_PC24 ||
395 RelType == ELF::R_ARM_CALL ||
396 RelType == ELF::R_ARM_JUMP24)) {
397 // This is an ARM branch relocation, need to use a stub function.
398 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
399 SectionEntry &Section = Sections[Rel.SectionID];
400 uint8_t *Target = Section.Address + Rel.Offset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000401
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000402 // Look up for existing stub.
403 StubMap::const_iterator i = Stubs.find(Value);
404 if (i != Stubs.end()) {
Danil Malyshevab427332012-04-17 20:10:16 +0000405 resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000406 i->second, RelType, 0);
407 DEBUG(dbgs() << " Stub function found\n");
408 } else {
409 // Create a new stub function.
410 DEBUG(dbgs() << " Create a new stub function\n");
411 Stubs[Value] = Section.StubOffset;
412 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
413 Section.StubOffset);
Eli Bendersky6d15e872012-04-30 10:06:27 +0000414 addRelocation(Value, Rel.SectionID,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000415 StubTargetAddr - Section.Address, ELF::R_ARM_ABS32);
Danil Malyshevab427332012-04-17 20:10:16 +0000416 resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000417 Section.StubOffset, RelType, 0);
418 Section.StubOffset += getMaxStubSize();
419 }
420 } else
Eli Bendersky6d15e872012-04-30 10:06:27 +0000421 addRelocation(Value, Rel.SectionID, Rel.Offset, RelType);
Jim Grosbach61425c02012-01-16 22:26:39 +0000422}
423
Eli Benderskya66a1852012-01-16 08:56:09 +0000424bool RuntimeDyldELF::isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
425 StringRef Magic = InputBuffer->getBuffer().slice(0, ELF::EI_NIDENT);
426 return (memcmp(Magic.data(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
427}
428} // namespace llvm