blob: 1073c6fc5225d2d3aef4e3e881576bd9c055ddd4 [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"
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000015#include "RuntimeDyldELF.h"
16#include "JITRegistrar.h"
17#include "ObjectImageCommon.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000018#include "llvm/ADT/OwningPtr.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/IntervalMap.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000022#include "llvm/Object/ObjectFile.h"
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000023#include "llvm/ExecutionEngine/ObjectImage.h"
24#include "llvm/ExecutionEngine/ObjectBuffer.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000025#include "llvm/Support/ELF.h"
26#include "llvm/ADT/Triple.h"
Preston Gurd689ff9c2012-04-16 22:12:58 +000027#include "llvm/Object/ELF.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000028using namespace llvm;
29using namespace llvm::object;
30
Preston Gurd689ff9c2012-04-16 22:12:58 +000031namespace {
32
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +000033static inline
34error_code check(error_code Err) {
35 if (Err) {
36 report_fatal_error(Err.message());
37 }
38 return Err;
39}
40
Preston Gurd689ff9c2012-04-16 22:12:58 +000041template<support::endianness target_endianness, bool is64Bits>
42class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> {
43 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
44
45 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
46 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
47 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
48 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
49
Michael J. Spencer2c38a662012-09-10 19:04:02 +000050 typedef Elf_Ehdr_Impl<target_endianness, is64Bits> Elf_Ehdr;
Preston Gurd689ff9c2012-04-16 22:12:58 +000051
52 typedef typename ELFDataTypeTypedefHelper<
53 target_endianness, is64Bits>::value_type addr_type;
54
Preston Gurd689ff9c2012-04-16 22:12:58 +000055public:
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000056 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
Preston Gurd689ff9c2012-04-16 22:12:58 +000057
58 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
59 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
60
Andrew Kaylor2e319872012-07-27 17:52:42 +000061 // Methods for type inquiry through isa, cast and dyn_cast
Preston Gurd689ff9c2012-04-16 22:12:58 +000062 static inline bool classof(const Binary *v) {
63 return (isa<ELFObjectFile<target_endianness, is64Bits> >(v)
64 && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v)));
65 }
66 static inline bool classof(
67 const ELFObjectFile<target_endianness, is64Bits> *v) {
68 return v->isDyldType();
69 }
Preston Gurd689ff9c2012-04-16 22:12:58 +000070};
71
72template<support::endianness target_endianness, bool is64Bits>
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000073class ELFObjectImage : public ObjectImageCommon {
Preston Gurd689ff9c2012-04-16 22:12:58 +000074 protected:
75 DyldELFObject<target_endianness, is64Bits> *DyldObj;
76 bool Registered;
77
78 public:
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000079 ELFObjectImage(ObjectBuffer *Input,
80 DyldELFObject<target_endianness, is64Bits> *Obj)
81 : ObjectImageCommon(Input, Obj),
Preston Gurd689ff9c2012-04-16 22:12:58 +000082 DyldObj(Obj),
83 Registered(false) {}
84
85 virtual ~ELFObjectImage() {
86 if (Registered)
87 deregisterWithDebugger();
88 }
89
90 // Subclasses can override these methods to update the image with loaded
91 // addresses for sections and common symbols
92 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
93 {
94 DyldObj->updateSectionAddress(Sec, Addr);
95 }
96
97 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
98 {
99 DyldObj->updateSymbolAddress(Sym, Addr);
100 }
101
102 virtual void registerWithDebugger()
103 {
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000104 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000105 Registered = true;
106 }
107 virtual void deregisterWithDebugger()
108 {
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000109 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000110 }
111};
112
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000113// The MemoryBuffer passed into this constructor is just a wrapper around the
114// actual memory. Ultimately, the Binary parent class will take ownership of
115// this MemoryBuffer object but not the underlying memory.
Preston Gurd689ff9c2012-04-16 22:12:58 +0000116template<support::endianness target_endianness, bool is64Bits>
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000117DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Wrapper,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000118 error_code &ec)
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000119 : ELFObjectFile<target_endianness, is64Bits>(Wrapper, ec) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000120 this->isDyldELFObject = true;
121}
122
123template<support::endianness target_endianness, bool is64Bits>
124void DyldELFObject<target_endianness, is64Bits>::updateSectionAddress(
125 const SectionRef &Sec,
126 uint64_t Addr) {
127 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
128 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
129 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
130
131 // This assumes the address passed in matches the target address bitness
132 // The template-based type cast handles everything else.
133 shdr->sh_addr = static_cast<addr_type>(Addr);
134}
135
136template<support::endianness target_endianness, bool is64Bits>
137void DyldELFObject<target_endianness, is64Bits>::updateSymbolAddress(
138 const SymbolRef &SymRef,
139 uint64_t Addr) {
140
141 Elf_Sym *sym = const_cast<Elf_Sym*>(
142 ELFObjectFile<target_endianness, is64Bits>::
143 getSymbol(SymRef.getRawDataRefImpl()));
144
145 // This assumes the address passed in matches the target address bitness
146 // The template-based type cast handles everything else.
147 sym->st_value = static_cast<addr_type>(Addr);
148}
149
150} // namespace
151
152
Eli Benderskya66a1852012-01-16 08:56:09 +0000153namespace llvm {
154
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000155ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
156 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
157 llvm_unreachable("Unexpected ELF object size");
158 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
159 (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
160 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000161 error_code ec;
162
163 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
164 DyldELFObject<support::little, false> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000165 new DyldELFObject<support::little, false>(Buffer->getMemBuffer(), ec);
166 return new ELFObjectImage<support::little, false>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000167 }
168 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
169 DyldELFObject<support::big, false> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000170 new DyldELFObject<support::big, false>(Buffer->getMemBuffer(), ec);
171 return new ELFObjectImage<support::big, false>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000172 }
173 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
174 DyldELFObject<support::big, true> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000175 new DyldELFObject<support::big, true>(Buffer->getMemBuffer(), ec);
176 return new ELFObjectImage<support::big, true>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000177 }
178 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
179 DyldELFObject<support::little, true> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000180 new DyldELFObject<support::little, true>(Buffer->getMemBuffer(), ec);
181 return new ELFObjectImage<support::little, true>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000182 }
183 else
184 llvm_unreachable("Unexpected ELF format");
185}
186
Preston Gurd689ff9c2012-04-16 22:12:58 +0000187RuntimeDyldELF::~RuntimeDyldELF() {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000188}
Eli Benderskya66a1852012-01-16 08:56:09 +0000189
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000190void RuntimeDyldELF::resolveX86_64Relocation(uint8_t *LocalAddress,
191 uint64_t FinalAddress,
192 uint64_t Value,
193 uint32_t Type,
194 int64_t Addend) {
195 switch (Type) {
196 default:
197 llvm_unreachable("Relocation type not implemented yet!");
198 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000199 case ELF::R_X86_64_64: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000200 uint64_t *Target = (uint64_t*)(LocalAddress);
201 *Target = Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000202 break;
203 }
204 case ELF::R_X86_64_32:
205 case ELF::R_X86_64_32S: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000206 Value += Addend;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000207 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
208 (Type == ELF::R_X86_64_32S &&
209 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Benderskya66a1852012-01-16 08:56:09 +0000210 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000211 uint32_t *Target = reinterpret_cast<uint32_t*>(LocalAddress);
Eli Benderskya66a1852012-01-16 08:56:09 +0000212 *Target = TruncatedAddr;
213 break;
214 }
215 case ELF::R_X86_64_PC32: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000216 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
217 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000218 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000219 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Eli Benderskya66a1852012-01-16 08:56:09 +0000220 *Placeholder = TruncOffset;
221 break;
222 }
223 }
224}
225
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000226void RuntimeDyldELF::resolveX86Relocation(uint8_t *LocalAddress,
227 uint32_t FinalAddress,
228 uint32_t Value,
229 uint32_t Type,
230 int32_t Addend) {
231 switch (Type) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000232 case ELF::R_386_32: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000233 uint32_t *Target = (uint32_t*)(LocalAddress);
Preston Gurdc68dda82012-04-12 20:13:57 +0000234 uint32_t Placeholder = *Target;
235 *Target = Placeholder + Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000236 break;
237 }
238 case ELF::R_386_PC32: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000239 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
240 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Eli Benderskya66a1852012-01-16 08:56:09 +0000241 *Placeholder = RealOffset;
242 break;
243 }
244 default:
245 // There are other relocation types, but it appears these are the
Andrew Kaylore2e73bd2012-07-27 18:39:47 +0000246 // only ones currently used by the LLVM ELF object writer
Craig Topper85814382012-02-07 05:05:23 +0000247 llvm_unreachable("Relocation type not implemented yet!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000248 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000249 }
250}
251
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000252void RuntimeDyldELF::resolveARMRelocation(uint8_t *LocalAddress,
253 uint32_t FinalAddress,
254 uint32_t Value,
255 uint32_t Type,
256 int32_t Addend) {
257 // TODO: Add Thumb relocations.
258 uint32_t* TargetPtr = (uint32_t*)LocalAddress;
259 Value += Addend;
260
261 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " << LocalAddress
262 << " FinalAddress: " << format("%p",FinalAddress)
263 << " Value: " << format("%x",Value)
264 << " Type: " << format("%x",Type)
265 << " Addend: " << format("%x",Addend)
266 << "\n");
267
268 switch(Type) {
269 default:
270 llvm_unreachable("Not implemented relocation type!");
271
Tim Northover565ebde2012-10-03 16:29:42 +0000272 // Write a 32bit value to relocation address, taking into account the
273 // implicit addend encoded in the target.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000274 case ELF::R_ARM_ABS32 :
Tim Northover565ebde2012-10-03 16:29:42 +0000275 *TargetPtr += Value;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000276 break;
277
278 // Write first 16 bit of 32 bit value to the mov instruction.
279 // Last 4 bit should be shifted.
280 case ELF::R_ARM_MOVW_ABS_NC :
Tim Northover565ebde2012-10-03 16:29:42 +0000281 // We are not expecting any other addend in the relocation address.
282 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
283 // non-contiguous fields.
284 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000285 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 :
Tim Northover565ebde2012-10-03 16:29:42 +0000293 // We are not expecting any other addend in the relocation address.
294 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
295 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000296 Value = (Value >> 16) & 0xFFFF;
297 *TargetPtr |= Value & 0xFFF;
298 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
299 break;
300
301 // Write 24 bit relative value to the branch instruction.
302 case ELF::R_ARM_PC24 : // Fall through.
303 case ELF::R_ARM_CALL : // Fall through.
304 case ELF::R_ARM_JUMP24 :
305 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
306 RelValue = (RelValue & 0x03FFFFFC) >> 2;
307 *TargetPtr &= 0xFF000000;
308 *TargetPtr |= RelValue;
309 break;
310 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000311}
312
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000313void RuntimeDyldELF::resolveMIPSRelocation(uint8_t *LocalAddress,
Akira Hatanakab862f092012-08-20 17:53:24 +0000314 uint32_t FinalAddress,
315 uint32_t Value,
316 uint32_t Type,
317 int32_t Addend) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000318 uint32_t* TargetPtr = (uint32_t*)LocalAddress;
319 Value += Addend;
320
321 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: " << LocalAddress
322 << " FinalAddress: " << format("%p",FinalAddress)
323 << " Value: " << format("%x",Value)
324 << " Type: " << format("%x",Type)
325 << " Addend: " << format("%x",Addend)
326 << "\n");
327
328 switch(Type) {
329 default:
330 llvm_unreachable("Not implemented relocation type!");
331 break;
332 case ELF::R_MIPS_32:
333 *TargetPtr = Value + (*TargetPtr);
334 break;
335 case ELF::R_MIPS_26:
336 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
337 break;
338 case ELF::R_MIPS_HI16:
339 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
340 Value += ((*TargetPtr) & 0x0000ffff) << 16;
341 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
342 (((Value + 0x8000) >> 16) & 0xffff);
343 break;
344 case ELF::R_MIPS_LO16:
345 Value += ((*TargetPtr) & 0x0000ffff);
346 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
347 break;
348 }
349}
350
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000351// Return the .TOC. section address to R_PPC64_TOC relocations.
352uint64_t RuntimeDyldELF::findPPC64TOC() const {
353 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
354 // order. The TOC starts where the first of these sections starts.
355 SectionList::const_iterator it = Sections.begin();
356 SectionList::const_iterator ite = Sections.end();
357 for (; it != ite; ++it) {
358 if (it->Name == ".got" ||
359 it->Name == ".toc" ||
360 it->Name == ".tocbss" ||
361 it->Name == ".plt")
362 break;
363 }
364 if (it == ite) {
365 // This may happen for
366 // * references to TOC base base (sym@toc, .odp relocation) without
367 // a .toc directive.
368 // In this case just use the first section (which is usually
369 // the .odp) since the code won't reference the .toc base
370 // directly.
371 it = Sections.begin();
372 }
373 assert (it != ite);
374 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
375 // thus permitting a full 64 Kbytes segment.
376 return it->LoadAddress + 0x8000;
377}
378
379// Returns the sections and offset associated with the ODP entry referenced
380// by Symbol.
381void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
382 ObjSectionToIDMap &LocalSections,
383 RelocationValueRef &Rel) {
384 // Get the ELF symbol value (st_value) to compare with Relocation offset in
385 // .opd entries
386
387 error_code err;
388 for (section_iterator si = Obj.begin_sections(),
389 se = Obj.end_sections(); si != se; si.increment(err)) {
390 StringRef SectionName;
391 check(si->getName(SectionName));
392 if (SectionName != ".opd")
393 continue;
394
395 for (relocation_iterator i = si->begin_relocations(),
396 e = si->end_relocations(); i != e;) {
397 check(err);
398
399 // The R_PPC64_ADDR64 relocation indicates the first field
400 // of a .opd entry
401 uint64_t TypeFunc;
402 check(i->getType(TypeFunc));
403 if (TypeFunc != ELF::R_PPC64_ADDR64) {
404 i.increment(err);
405 continue;
406 }
407
408 SymbolRef TargetSymbol;
409 uint64_t TargetSymbolOffset;
410 int64_t TargetAdditionalInfo;
411 check(i->getSymbol(TargetSymbol));
412 check(i->getOffset(TargetSymbolOffset));
413 check(i->getAdditionalInfo(TargetAdditionalInfo));
414
415 i = i.increment(err);
416 if (i == e)
417 break;
418 check(err);
419
420 // Just check if following relocation is a R_PPC64_TOC
421 uint64_t TypeTOC;
422 check(i->getType(TypeTOC));
423 if (TypeTOC != ELF::R_PPC64_TOC)
424 continue;
425
426 // Finally compares the Symbol value and the target symbol offset
427 // to check if this .opd entry refers to the symbol the relocation
428 // points to.
429 if (Rel.Addend != (intptr_t)TargetSymbolOffset)
430 continue;
431
432 section_iterator tsi(Obj.end_sections());
433 check(TargetSymbol.getSection(tsi));
434 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
435 Rel.Addend = (intptr_t)TargetAdditionalInfo;
436 return;
437 }
438 }
439 llvm_unreachable("Attempting to get address of ODP entry!");
440}
441
442// Relocation masks following the #lo(value), #hi(value), #higher(value),
443// and #highest(value) macros defined in section 4.5.1. Relocation Types
444// in PPC-elf64abi document.
445//
446static inline
447uint16_t applyPPClo (uint64_t value)
448{
449 return value & 0xffff;
450}
451
452static inline
453uint16_t applyPPChi (uint64_t value)
454{
455 return (value >> 16) & 0xffff;
456}
457
458static inline
459uint16_t applyPPChigher (uint64_t value)
460{
461 return (value >> 32) & 0xffff;
462}
463
464static inline
465uint16_t applyPPChighest (uint64_t value)
466{
467 return (value >> 48) & 0xffff;
468}
469
470void RuntimeDyldELF::resolvePPC64Relocation(uint8_t *LocalAddress,
471 uint64_t FinalAddress,
472 uint64_t Value,
473 uint32_t Type,
474 int64_t Addend) {
475 switch (Type) {
476 default:
477 llvm_unreachable("Relocation type not implemented yet!");
478 break;
479 case ELF::R_PPC64_ADDR16_LO :
480 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
481 break;
482 case ELF::R_PPC64_ADDR16_HI :
483 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
484 break;
485 case ELF::R_PPC64_ADDR16_HIGHER :
486 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
487 break;
488 case ELF::R_PPC64_ADDR16_HIGHEST :
489 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
490 break;
491 case ELF::R_PPC64_ADDR14 : {
492 assert(((Value + Addend) & 3) == 0);
493 // Preserve the AA/LK bits in the branch instruction
494 uint8_t aalk = *(LocalAddress+3);
495 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
496 } break;
497 case ELF::R_PPC64_REL24 : {
498 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
499 if (SignExtend32<24>(delta) != delta)
500 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
501 // Generates a 'bl <address>' instruction
502 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
503 } break;
504 case ELF::R_PPC64_ADDR64 :
505 writeInt64BE(LocalAddress, Value + Addend);
506 break;
507 case ELF::R_PPC64_TOC :
508 writeInt64BE(LocalAddress, findPPC64TOC());
509 break;
510 case ELF::R_PPC64_TOC16 : {
511 uint64_t TOCStart = findPPC64TOC();
512 Value = applyPPClo((Value + Addend) - TOCStart);
513 writeInt16BE(LocalAddress, applyPPClo(Value));
514 } break;
515 case ELF::R_PPC64_TOC16_DS : {
516 uint64_t TOCStart = findPPC64TOC();
517 Value = ((Value + Addend) - TOCStart);
518 writeInt16BE(LocalAddress, applyPPClo(Value));
519 } break;
520 }
521}
522
523
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000524void RuntimeDyldELF::resolveRelocation(uint8_t *LocalAddress,
525 uint64_t FinalAddress,
526 uint64_t Value,
527 uint32_t Type,
528 int64_t Addend) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000529 switch (Arch) {
530 case Triple::x86_64:
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000531 resolveX86_64Relocation(LocalAddress, FinalAddress, Value, Type, Addend);
Eli Benderskya66a1852012-01-16 08:56:09 +0000532 break;
533 case Triple::x86:
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000534 resolveX86Relocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
535 (uint32_t)(Value & 0xffffffffL), Type,
536 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000537 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000538 case Triple::arm: // Fall through.
539 case Triple::thumb:
540 resolveARMRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
541 (uint32_t)(Value & 0xffffffffL), Type,
542 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000543 break;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000544 case Triple::mips: // Fall through.
545 case Triple::mipsel:
546 resolveMIPSRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
Akira Hatanakab862f092012-08-20 17:53:24 +0000547 (uint32_t)(Value & 0xffffffffL), Type,
548 (uint32_t)(Addend & 0xffffffffL));
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000549 break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000550 case Triple::ppc64:
551 resolvePPC64Relocation(LocalAddress, FinalAddress, Value, Type, Addend);
552 break;
Craig Topper85814382012-02-07 05:05:23 +0000553 default: llvm_unreachable("Unsupported CPU type!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000554 }
555}
556
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000557void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000558 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000559 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000560 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000561 StubMap &Stubs) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000562
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000563 uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL);
564 intptr_t Addend = (intptr_t)Rel.AdditionalInfo;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000565 const SymbolRef &Symbol = Rel.Symbol;
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000566
567 // Obtain the symbol name which is referenced in the relocation
568 StringRef TargetName;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000569 Symbol.getName(TargetName);
570 DEBUG(dbgs() << "\t\tRelType: " << RelType
571 << " Addend: " << Addend
572 << " TargetName: " << TargetName
573 << "\n");
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000574 RelocationValueRef Value;
575 // First search for the symbol in the local symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000576 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000577 SymbolRef::Type SymType;
578 Symbol.getType(SymType);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000579 if (lsi != Symbols.end()) {
580 Value.SectionID = lsi->second.first;
581 Value.Addend = lsi->second.second;
582 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000583 // Search for the symbol in the global symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000584 SymbolTableMap::const_iterator gsi =
585 GlobalSymbolTable.find(TargetName.data());
586 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000587 Value.SectionID = gsi->second.first;
588 Value.Addend = gsi->second.second;
589 } else {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000590 switch (SymType) {
591 case SymbolRef::ST_Debug: {
592 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
593 // and can be changed by another developers. Maybe best way is add
594 // a new symbol type ST_Section to SymbolRef and use it.
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000595 section_iterator si(Obj.end_sections());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000596 Symbol.getSection(si);
597 if (si == Obj.end_sections())
598 llvm_unreachable("Symbol section not found, bad object file format!");
599 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000600 // Default to 'true' in case isText fails (though it never does).
601 bool isCode = true;
602 si->isText(isCode);
603 Value.SectionID = findOrEmitSection(Obj,
604 (*si),
605 isCode,
606 ObjSectionToID);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000607 Value.Addend = Addend;
608 break;
609 }
610 case SymbolRef::ST_Unknown: {
611 Value.SymbolName = TargetName.data();
612 Value.Addend = Addend;
613 break;
614 }
615 default:
616 llvm_unreachable("Unresolved symbol type!");
617 break;
618 }
619 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000620 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000621 DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID
622 << " Rel.Offset: " << Rel.Offset
623 << "\n");
624 if (Arch == Triple::arm &&
625 (RelType == ELF::R_ARM_PC24 ||
626 RelType == ELF::R_ARM_CALL ||
627 RelType == ELF::R_ARM_JUMP24)) {
628 // This is an ARM branch relocation, need to use a stub function.
629 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
630 SectionEntry &Section = Sections[Rel.SectionID];
631 uint8_t *Target = Section.Address + Rel.Offset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000632
Eric Christopherbf261f12012-10-23 17:19:15 +0000633 // Look for an existing stub.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000634 StubMap::const_iterator i = Stubs.find(Value);
635 if (i != Stubs.end()) {
Danil Malyshevab427332012-04-17 20:10:16 +0000636 resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000637 i->second, RelType, 0);
638 DEBUG(dbgs() << " Stub function found\n");
639 } else {
640 // Create a new stub function.
641 DEBUG(dbgs() << " Create a new stub function\n");
642 Stubs[Value] = Section.StubOffset;
643 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
644 Section.StubOffset);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000645 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
646 ELF::R_ARM_ABS32, Value.Addend);
647 if (Value.SymbolName)
648 addRelocationForSymbol(RE, Value.SymbolName);
649 else
650 addRelocationForSection(RE, Value.SectionID);
651
Danil Malyshevab427332012-04-17 20:10:16 +0000652 resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000653 Section.StubOffset, RelType, 0);
654 Section.StubOffset += getMaxStubSize();
655 }
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000656 } else if (Arch == Triple::mipsel && RelType == ELF::R_MIPS_26) {
657 // This is an Mips branch relocation, need to use a stub function.
658 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
659 SectionEntry &Section = Sections[Rel.SectionID];
660 uint8_t *Target = Section.Address + Rel.Offset;
661 uint32_t *TargetAddress = (uint32_t *)Target;
662
663 // Extract the addend from the instruction.
664 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
665
666 Value.Addend += Addend;
667
668 // Look up for existing stub.
669 StubMap::const_iterator i = Stubs.find(Value);
670 if (i != Stubs.end()) {
671 resolveRelocation(Target, (uint64_t)Target,
672 (uint64_t)Section.Address +
673 i->second, RelType, 0);
674 DEBUG(dbgs() << " Stub function found\n");
675 } else {
676 // Create a new stub function.
677 DEBUG(dbgs() << " Create a new stub function\n");
678 Stubs[Value] = Section.StubOffset;
679 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
680 Section.StubOffset);
681
682 // Creating Hi and Lo relocations for the filled stub instructions.
683 RelocationEntry REHi(Rel.SectionID,
684 StubTargetAddr - Section.Address,
685 ELF::R_MIPS_HI16, Value.Addend);
686 RelocationEntry RELo(Rel.SectionID,
687 StubTargetAddr - Section.Address + 4,
688 ELF::R_MIPS_LO16, Value.Addend);
689
690 if (Value.SymbolName) {
691 addRelocationForSymbol(REHi, Value.SymbolName);
692 addRelocationForSymbol(RELo, Value.SymbolName);
693 } else {
694 addRelocationForSection(REHi, Value.SectionID);
695 addRelocationForSection(RELo, Value.SectionID);
696 }
697
698 resolveRelocation(Target, (uint64_t)Target,
699 (uint64_t)Section.Address +
700 Section.StubOffset, RelType, 0);
701 Section.StubOffset += getMaxStubSize();
702 }
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000703 } else if (Arch == Triple::ppc64) {
704 if (RelType == ELF::R_PPC64_REL24) {
705 // A PPC branch relocation will need a stub function if the target is
706 // an external symbol (Symbol::ST_Unknown) or if the target address
707 // is not within the signed 24-bits branch address.
708 SectionEntry &Section = Sections[Rel.SectionID];
709 uint8_t *Target = Section.Address + Rel.Offset;
710 bool RangeOverflow = false;
711 if (SymType != SymbolRef::ST_Unknown) {
712 // A function call may points to the .opd entry, so the final symbol value
713 // in calculated based in the relocation values in .opd section.
714 findOPDEntrySection(Obj, ObjSectionToID, Value);
715 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
716 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
717 // If it is within 24-bits branch range, just set the branch target
718 if (SignExtend32<24>(delta) == delta) {
719 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
720 if (Value.SymbolName)
721 addRelocationForSymbol(RE, Value.SymbolName);
722 else
723 addRelocationForSection(RE, Value.SectionID);
724 } else {
725 RangeOverflow = true;
726 }
727 }
728 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
729 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
730 // larger than 24-bits.
731 StubMap::const_iterator i = Stubs.find(Value);
732 if (i != Stubs.end()) {
733 // Symbol function stub already created, just relocate to it
734 resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address
735 + i->second, RelType, 0);
736 DEBUG(dbgs() << " Stub function found\n");
737 } else {
738 // Create a new stub function.
739 DEBUG(dbgs() << " Create a new stub function\n");
740 Stubs[Value] = Section.StubOffset;
741 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
742 Section.StubOffset);
743 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
744 ELF::R_PPC64_ADDR64, Value.Addend);
745
746 // Generates the 64-bits address loads as exemplified in section
747 // 4.5.1 in PPC64 ELF ABI.
748 RelocationEntry REhst(Rel.SectionID,
749 StubTargetAddr - Section.Address + 2,
750 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
751 RelocationEntry REhr(Rel.SectionID,
752 StubTargetAddr - Section.Address + 6,
753 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
754 RelocationEntry REh(Rel.SectionID,
755 StubTargetAddr - Section.Address + 14,
756 ELF::R_PPC64_ADDR16_HI, Value.Addend);
757 RelocationEntry REl(Rel.SectionID,
758 StubTargetAddr - Section.Address + 18,
759 ELF::R_PPC64_ADDR16_LO, Value.Addend);
760
761 if (Value.SymbolName) {
762 addRelocationForSymbol(REhst, Value.SymbolName);
763 addRelocationForSymbol(REhr, Value.SymbolName);
764 addRelocationForSymbol(REh, Value.SymbolName);
765 addRelocationForSymbol(REl, Value.SymbolName);
766 } else {
767 addRelocationForSection(REhst, Value.SectionID);
768 addRelocationForSection(REhr, Value.SectionID);
769 addRelocationForSection(REh, Value.SectionID);
770 addRelocationForSection(REl, Value.SectionID);
771 }
772
773 resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address
774 + Section.StubOffset, RelType, 0);
775 if (SymType == SymbolRef::ST_Unknown)
776 // Restore the TOC for external calls
777 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
778 Section.StubOffset += getMaxStubSize();
779 }
780 }
781 } else {
782 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
783 // Extra check to avoid relocation againt empty symbols (usually
784 // the R_PPC64_TOC).
785 if (Value.SymbolName && !TargetName.empty())
786 addRelocationForSymbol(RE, Value.SymbolName);
787 else
788 addRelocationForSection(RE, Value.SectionID);
789 }
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000790 } else {
791 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
792 if (Value.SymbolName)
793 addRelocationForSymbol(RE, Value.SymbolName);
794 else
795 addRelocationForSection(RE, Value.SectionID);
796 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000797}
798
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000799bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
800 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
801 return false;
802 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
Eli Benderskya66a1852012-01-16 08:56:09 +0000803}
804} // namespace llvm