1 //===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
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 ELF support for the MC-JIT runtime dynamic linker.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "dyld"
15 #include "RuntimeDyldELF.h"
16 #include "JITRegistrar.h"
17 #include "ObjectImageCommon.h"
18 #include "llvm/ADT/IntervalMap.h"
19 #include "llvm/ADT/OwningPtr.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/ExecutionEngine/ObjectBuffer.h"
24 #include "llvm/ExecutionEngine/ObjectImage.h"
25 #include "llvm/Object/ELF.h"
26 #include "llvm/Object/ObjectFile.h"
27 #include "llvm/Support/ELF.h"
28 using namespace llvm;
29 using namespace llvm::object;
30
31 namespace {
32
33 static inline
check(error_code Err)34 error_code check(error_code Err) {
35 if (Err) {
36 report_fatal_error(Err.message());
37 }
38 return Err;
39 }
40
41 template<class ELFT>
42 class DyldELFObject
43 : public ELFObjectFile<ELFT> {
44 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
45
46 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
47 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
48 typedef
49 Elf_Rel_Impl<ELFT, false> Elf_Rel;
50 typedef
51 Elf_Rel_Impl<ELFT, true> Elf_Rela;
52
53 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
54
55 typedef typename ELFDataTypeTypedefHelper<
56 ELFT>::value_type addr_type;
57
58 public:
59 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
60
61 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
62 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
63
64 // Methods for type inquiry through isa, cast and dyn_cast
classof(const Binary * v)65 static inline bool classof(const Binary *v) {
66 return (isa<ELFObjectFile<ELFT> >(v)
67 && classof(cast<ELFObjectFile
68 <ELFT> >(v)));
69 }
classof(const ELFObjectFile<ELFT> * v)70 static inline bool classof(
71 const ELFObjectFile<ELFT> *v) {
72 return v->isDyldType();
73 }
74 };
75
76 template<class ELFT>
77 class ELFObjectImage : public ObjectImageCommon {
78 protected:
79 DyldELFObject<ELFT> *DyldObj;
80 bool Registered;
81
82 public:
ELFObjectImage(ObjectBuffer * Input,DyldELFObject<ELFT> * Obj)83 ELFObjectImage(ObjectBuffer *Input,
84 DyldELFObject<ELFT> *Obj)
85 : ObjectImageCommon(Input, Obj),
86 DyldObj(Obj),
87 Registered(false) {}
88
~ELFObjectImage()89 virtual ~ELFObjectImage() {
90 if (Registered)
91 deregisterWithDebugger();
92 }
93
94 // Subclasses can override these methods to update the image with loaded
95 // addresses for sections and common symbols
updateSectionAddress(const SectionRef & Sec,uint64_t Addr)96 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
97 {
98 DyldObj->updateSectionAddress(Sec, Addr);
99 }
100
updateSymbolAddress(const SymbolRef & Sym,uint64_t Addr)101 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
102 {
103 DyldObj->updateSymbolAddress(Sym, Addr);
104 }
105
registerWithDebugger()106 virtual void registerWithDebugger()
107 {
108 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
109 Registered = true;
110 }
deregisterWithDebugger()111 virtual void deregisterWithDebugger()
112 {
113 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
114 }
115 };
116
117 // The MemoryBuffer passed into this constructor is just a wrapper around the
118 // actual memory. Ultimately, the Binary parent class will take ownership of
119 // this MemoryBuffer object but not the underlying memory.
120 template<class ELFT>
DyldELFObject(MemoryBuffer * Wrapper,error_code & ec)121 DyldELFObject<ELFT>::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
122 : ELFObjectFile<ELFT>(Wrapper, ec) {
123 this->isDyldELFObject = true;
124 }
125
126 template<class ELFT>
updateSectionAddress(const SectionRef & Sec,uint64_t Addr)127 void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
128 uint64_t Addr) {
129 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
130 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
131 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
132
133 // This assumes the address passed in matches the target address bitness
134 // The template-based type cast handles everything else.
135 shdr->sh_addr = static_cast<addr_type>(Addr);
136 }
137
138 template<class ELFT>
updateSymbolAddress(const SymbolRef & SymRef,uint64_t Addr)139 void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
140 uint64_t Addr) {
141
142 Elf_Sym *sym = const_cast<Elf_Sym*>(
143 ELFObjectFile<ELFT>::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 namespace llvm {
153
getEHFrameSection()154 StringRef RuntimeDyldELF::getEHFrameSection() {
155 for (int i = 0, e = Sections.size(); i != e; ++i) {
156 if (Sections[i].Name == ".eh_frame")
157 return StringRef((const char*)Sections[i].Address, Sections[i].Size);
158 }
159 return StringRef();
160 }
161
createObjectImage(ObjectBuffer * Buffer)162 ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
163 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
164 llvm_unreachable("Unexpected ELF object size");
165 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
166 (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
167 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
168 error_code ec;
169
170 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
171 DyldELFObject<ELFType<support::little, 4, false> > *Obj =
172 new DyldELFObject<ELFType<support::little, 4, false> >(
173 Buffer->getMemBuffer(), ec);
174 return new ELFObjectImage<ELFType<support::little, 4, false> >(Buffer, Obj);
175 }
176 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
177 DyldELFObject<ELFType<support::big, 4, false> > *Obj =
178 new DyldELFObject<ELFType<support::big, 4, false> >(
179 Buffer->getMemBuffer(), ec);
180 return new ELFObjectImage<ELFType<support::big, 4, false> >(Buffer, Obj);
181 }
182 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
183 DyldELFObject<ELFType<support::big, 8, true> > *Obj =
184 new DyldELFObject<ELFType<support::big, 8, true> >(
185 Buffer->getMemBuffer(), ec);
186 return new ELFObjectImage<ELFType<support::big, 8, true> >(Buffer, Obj);
187 }
188 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
189 DyldELFObject<ELFType<support::little, 8, true> > *Obj =
190 new DyldELFObject<ELFType<support::little, 8, true> >(
191 Buffer->getMemBuffer(), ec);
192 return new ELFObjectImage<ELFType<support::little, 8, true> >(Buffer, Obj);
193 }
194 else
195 llvm_unreachable("Unexpected ELF format");
196 }
197
~RuntimeDyldELF()198 RuntimeDyldELF::~RuntimeDyldELF() {
199 }
200
resolveX86_64Relocation(const SectionEntry & Section,uint64_t Offset,uint64_t Value,uint32_t Type,int64_t Addend)201 void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
202 uint64_t Offset,
203 uint64_t Value,
204 uint32_t Type,
205 int64_t Addend) {
206 switch (Type) {
207 default:
208 llvm_unreachable("Relocation type not implemented yet!");
209 break;
210 case ELF::R_X86_64_64: {
211 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
212 *Target = Value + Addend;
213 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend))
214 << " at " << format("%p\n",Target));
215 break;
216 }
217 case ELF::R_X86_64_32:
218 case ELF::R_X86_64_32S: {
219 Value += Addend;
220 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
221 (Type == ELF::R_X86_64_32S &&
222 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
223 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
224 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
225 *Target = TruncatedAddr;
226 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr)
227 << " at " << format("%p\n",Target));
228 break;
229 }
230 case ELF::R_X86_64_PC32: {
231 // Get the placeholder value from the generated object since
232 // a previous relocation attempt may have overwritten the loaded version
233 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
234 + Offset);
235 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
236 uint64_t FinalAddress = Section.LoadAddress + Offset;
237 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
238 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
239 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
240 *Target = TruncOffset;
241 break;
242 }
243 }
244 }
245
resolveX86Relocation(const SectionEntry & Section,uint64_t Offset,uint32_t Value,uint32_t Type,int32_t Addend)246 void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
247 uint64_t Offset,
248 uint32_t Value,
249 uint32_t Type,
250 int32_t Addend) {
251 switch (Type) {
252 case ELF::R_386_32: {
253 // Get the placeholder value from the generated object since
254 // a previous relocation attempt may have overwritten the loaded version
255 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
256 + Offset);
257 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
258 *Target = *Placeholder + Value + Addend;
259 break;
260 }
261 case ELF::R_386_PC32: {
262 // Get the placeholder value from the generated object since
263 // a previous relocation attempt may have overwritten the loaded version
264 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
265 + Offset);
266 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
267 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
268 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
269 *Target = RealOffset;
270 break;
271 }
272 default:
273 // There are other relocation types, but it appears these are the
274 // only ones currently used by the LLVM ELF object writer
275 llvm_unreachable("Relocation type not implemented yet!");
276 break;
277 }
278 }
279
resolveAArch64Relocation(const SectionEntry & Section,uint64_t Offset,uint64_t Value,uint32_t Type,int64_t Addend)280 void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
281 uint64_t Offset,
282 uint64_t Value,
283 uint32_t Type,
284 int64_t Addend) {
285 uint32_t *TargetPtr = reinterpret_cast<uint32_t*>(Section.Address + Offset);
286 uint64_t FinalAddress = Section.LoadAddress + Offset;
287
288 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
289 << format("%llx", Section.Address + Offset)
290 << " FinalAddress: 0x" << format("%llx",FinalAddress)
291 << " Value: 0x" << format("%llx",Value)
292 << " Type: 0x" << format("%x",Type)
293 << " Addend: 0x" << format("%llx",Addend)
294 << "\n");
295
296 switch (Type) {
297 default:
298 llvm_unreachable("Relocation type not implemented yet!");
299 break;
300 case ELF::R_AARCH64_ABS64: {
301 uint64_t *TargetPtr = reinterpret_cast<uint64_t*>(Section.Address + Offset);
302 *TargetPtr = Value + Addend;
303 break;
304 }
305 case ELF::R_AARCH64_PREL32: {
306 uint64_t Result = Value + Addend - FinalAddress;
307 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
308 static_cast<int64_t>(Result) <= UINT32_MAX);
309 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
310 break;
311 }
312 case ELF::R_AARCH64_CALL26: // fallthrough
313 case ELF::R_AARCH64_JUMP26: {
314 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
315 // calculation.
316 uint64_t BranchImm = Value + Addend - FinalAddress;
317
318 // "Check that -2^27 <= result < 2^27".
319 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
320 static_cast<int64_t>(BranchImm) < (1LL << 27));
321
322 // AArch64 code is emitted with .rela relocations. The data already in any
323 // bits affected by the relocation on entry is garbage.
324 *TargetPtr &= 0xfc000000U;
325 // Immediate goes in bits 25:0 of B and BL.
326 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
327 break;
328 }
329 case ELF::R_AARCH64_MOVW_UABS_G3: {
330 uint64_t Result = Value + Addend;
331
332 // AArch64 code is emitted with .rela relocations. The data already in any
333 // bits affected by the relocation on entry is garbage.
334 *TargetPtr &= 0xffe0001fU;
335 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
336 *TargetPtr |= Result >> (48 - 5);
337 // Shift must be "lsl #48", in bits 22:21
338 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
339 break;
340 }
341 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
342 uint64_t Result = Value + Addend;
343
344
345 // AArch64 code is emitted with .rela relocations. The data already in any
346 // bits affected by the relocation on entry is garbage.
347 *TargetPtr &= 0xffe0001fU;
348 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
349 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
350 // Shift must be "lsl #32", in bits 22:21
351 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
352 break;
353 }
354 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
355 uint64_t Result = Value + Addend;
356
357 // AArch64 code is emitted with .rela relocations. The data already in any
358 // bits affected by the relocation on entry is garbage.
359 *TargetPtr &= 0xffe0001fU;
360 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
361 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
362 // Shift must be "lsl #16", in bits 22:2
363 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
364 break;
365 }
366 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
367 uint64_t Result = Value + Addend;
368
369 // AArch64 code is emitted with .rela relocations. The data already in any
370 // bits affected by the relocation on entry is garbage.
371 *TargetPtr &= 0xffe0001fU;
372 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
373 *TargetPtr |= ((Result & 0xffffU) << 5);
374 // Shift must be "lsl #0", in bits 22:21.
375 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
376 break;
377 }
378 }
379 }
380
resolveARMRelocation(const SectionEntry & Section,uint64_t Offset,uint32_t Value,uint32_t Type,int32_t Addend)381 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
382 uint64_t Offset,
383 uint32_t Value,
384 uint32_t Type,
385 int32_t Addend) {
386 // TODO: Add Thumb relocations.
387 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress +
388 Offset);
389 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
390 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
391 Value += Addend;
392
393 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
394 << Section.Address + Offset
395 << " FinalAddress: " << format("%p",FinalAddress)
396 << " Value: " << format("%x",Value)
397 << " Type: " << format("%x",Type)
398 << " Addend: " << format("%x",Addend)
399 << "\n");
400
401 switch(Type) {
402 default:
403 llvm_unreachable("Not implemented relocation type!");
404
405 // Write a 32bit value to relocation address, taking into account the
406 // implicit addend encoded in the target.
407 case ELF::R_ARM_TARGET1:
408 case ELF::R_ARM_ABS32:
409 *TargetPtr = *Placeholder + Value;
410 break;
411 // Write first 16 bit of 32 bit value to the mov instruction.
412 // Last 4 bit should be shifted.
413 case ELF::R_ARM_MOVW_ABS_NC:
414 // We are not expecting any other addend in the relocation address.
415 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
416 // non-contiguous fields.
417 assert((*Placeholder & 0x000F0FFF) == 0);
418 Value = Value & 0xFFFF;
419 *TargetPtr = *Placeholder | (Value & 0xFFF);
420 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
421 break;
422 // Write last 16 bit of 32 bit value to the mov instruction.
423 // Last 4 bit should be shifted.
424 case ELF::R_ARM_MOVT_ABS:
425 // We are not expecting any other addend in the relocation address.
426 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
427 assert((*Placeholder & 0x000F0FFF) == 0);
428
429 Value = (Value >> 16) & 0xFFFF;
430 *TargetPtr = *Placeholder | (Value & 0xFFF);
431 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
432 break;
433 // Write 24 bit relative value to the branch instruction.
434 case ELF::R_ARM_PC24 : // Fall through.
435 case ELF::R_ARM_CALL : // Fall through.
436 case ELF::R_ARM_JUMP24: {
437 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
438 RelValue = (RelValue & 0x03FFFFFC) >> 2;
439 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
440 *TargetPtr &= 0xFF000000;
441 *TargetPtr |= RelValue;
442 break;
443 }
444 case ELF::R_ARM_PRIVATE_0:
445 // This relocation is reserved by the ARM ELF ABI for internal use. We
446 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
447 // in the stubs created during JIT (which can't put an addend into the
448 // original object file).
449 *TargetPtr = Value;
450 break;
451 }
452 }
453
resolveMIPSRelocation(const SectionEntry & Section,uint64_t Offset,uint32_t Value,uint32_t Type,int32_t Addend)454 void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
455 uint64_t Offset,
456 uint32_t Value,
457 uint32_t Type,
458 int32_t Addend) {
459 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress +
460 Offset);
461 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
462 Value += Addend;
463
464 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
465 << Section.Address + Offset
466 << " FinalAddress: "
467 << format("%p",Section.LoadAddress + Offset)
468 << " Value: " << format("%x",Value)
469 << " Type: " << format("%x",Type)
470 << " Addend: " << format("%x",Addend)
471 << "\n");
472
473 switch(Type) {
474 default:
475 llvm_unreachable("Not implemented relocation type!");
476 break;
477 case ELF::R_MIPS_32:
478 *TargetPtr = Value + (*Placeholder);
479 break;
480 case ELF::R_MIPS_26:
481 *TargetPtr = ((*Placeholder) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
482 break;
483 case ELF::R_MIPS_HI16:
484 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
485 Value += ((*Placeholder) & 0x0000ffff) << 16;
486 *TargetPtr = ((*Placeholder) & 0xffff0000) |
487 (((Value + 0x8000) >> 16) & 0xffff);
488 break;
489 case ELF::R_MIPS_LO16:
490 Value += ((*Placeholder) & 0x0000ffff);
491 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
492 break;
493 case ELF::R_MIPS_UNUSED1:
494 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
495 // are used for internal JIT purpose. These relocations are similar to
496 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
497 // account.
498 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
499 (((Value + 0x8000) >> 16) & 0xffff);
500 break;
501 case ELF::R_MIPS_UNUSED2:
502 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
503 break;
504 }
505 }
506
507 // Return the .TOC. section address to R_PPC64_TOC relocations.
findPPC64TOC() const508 uint64_t RuntimeDyldELF::findPPC64TOC() const {
509 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
510 // order. The TOC starts where the first of these sections starts.
511 SectionList::const_iterator it = Sections.begin();
512 SectionList::const_iterator ite = Sections.end();
513 for (; it != ite; ++it) {
514 if (it->Name == ".got" ||
515 it->Name == ".toc" ||
516 it->Name == ".tocbss" ||
517 it->Name == ".plt")
518 break;
519 }
520 if (it == ite) {
521 // This may happen for
522 // * references to TOC base base (sym@toc, .odp relocation) without
523 // a .toc directive.
524 // In this case just use the first section (which is usually
525 // the .odp) since the code won't reference the .toc base
526 // directly.
527 it = Sections.begin();
528 }
529 assert (it != ite);
530 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
531 // thus permitting a full 64 Kbytes segment.
532 return it->LoadAddress + 0x8000;
533 }
534
535 // Returns the sections and offset associated with the ODP entry referenced
536 // by Symbol.
findOPDEntrySection(ObjectImage & Obj,ObjSectionToIDMap & LocalSections,RelocationValueRef & Rel)537 void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
538 ObjSectionToIDMap &LocalSections,
539 RelocationValueRef &Rel) {
540 // Get the ELF symbol value (st_value) to compare with Relocation offset in
541 // .opd entries
542
543 error_code err;
544 for (section_iterator si = Obj.begin_sections(),
545 se = Obj.end_sections(); si != se; si.increment(err)) {
546 section_iterator RelSecI = si->getRelocatedSection();
547 if (RelSecI == Obj.end_sections())
548 continue;
549
550 StringRef RelSectionName;
551 check(RelSecI->getName(RelSectionName));
552 if (RelSectionName != ".opd")
553 continue;
554
555 for (relocation_iterator i = si->begin_relocations(),
556 e = si->end_relocations(); i != e;) {
557 check(err);
558
559 // The R_PPC64_ADDR64 relocation indicates the first field
560 // of a .opd entry
561 uint64_t TypeFunc;
562 check(i->getType(TypeFunc));
563 if (TypeFunc != ELF::R_PPC64_ADDR64) {
564 i.increment(err);
565 continue;
566 }
567
568 uint64_t TargetSymbolOffset;
569 symbol_iterator TargetSymbol = i->getSymbol();
570 check(i->getOffset(TargetSymbolOffset));
571 int64_t Addend;
572 check(getELFRelocationAddend(*i, Addend));
573
574 i = i.increment(err);
575 if (i == e)
576 break;
577 check(err);
578
579 // Just check if following relocation is a R_PPC64_TOC
580 uint64_t TypeTOC;
581 check(i->getType(TypeTOC));
582 if (TypeTOC != ELF::R_PPC64_TOC)
583 continue;
584
585 // Finally compares the Symbol value and the target symbol offset
586 // to check if this .opd entry refers to the symbol the relocation
587 // points to.
588 if (Rel.Addend != (intptr_t)TargetSymbolOffset)
589 continue;
590
591 section_iterator tsi(Obj.end_sections());
592 check(TargetSymbol->getSection(tsi));
593 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
594 Rel.Addend = (intptr_t)Addend;
595 return;
596 }
597 }
598 llvm_unreachable("Attempting to get address of ODP entry!");
599 }
600
601 // Relocation masks following the #lo(value), #hi(value), #higher(value),
602 // and #highest(value) macros defined in section 4.5.1. Relocation Types
603 // in PPC-elf64abi document.
604 //
605 static inline
applyPPClo(uint64_t value)606 uint16_t applyPPClo (uint64_t value)
607 {
608 return value & 0xffff;
609 }
610
611 static inline
applyPPChi(uint64_t value)612 uint16_t applyPPChi (uint64_t value)
613 {
614 return (value >> 16) & 0xffff;
615 }
616
617 static inline
applyPPChigher(uint64_t value)618 uint16_t applyPPChigher (uint64_t value)
619 {
620 return (value >> 32) & 0xffff;
621 }
622
623 static inline
applyPPChighest(uint64_t value)624 uint16_t applyPPChighest (uint64_t value)
625 {
626 return (value >> 48) & 0xffff;
627 }
628
resolvePPC64Relocation(const SectionEntry & Section,uint64_t Offset,uint64_t Value,uint32_t Type,int64_t Addend)629 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
630 uint64_t Offset,
631 uint64_t Value,
632 uint32_t Type,
633 int64_t Addend) {
634 uint8_t* LocalAddress = Section.Address + Offset;
635 switch (Type) {
636 default:
637 llvm_unreachable("Relocation type not implemented yet!");
638 break;
639 case ELF::R_PPC64_ADDR16_LO :
640 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
641 break;
642 case ELF::R_PPC64_ADDR16_HI :
643 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
644 break;
645 case ELF::R_PPC64_ADDR16_HIGHER :
646 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
647 break;
648 case ELF::R_PPC64_ADDR16_HIGHEST :
649 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
650 break;
651 case ELF::R_PPC64_ADDR14 : {
652 assert(((Value + Addend) & 3) == 0);
653 // Preserve the AA/LK bits in the branch instruction
654 uint8_t aalk = *(LocalAddress+3);
655 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
656 } break;
657 case ELF::R_PPC64_ADDR32 : {
658 int32_t Result = static_cast<int32_t>(Value + Addend);
659 if (SignExtend32<32>(Result) != Result)
660 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
661 writeInt32BE(LocalAddress, Result);
662 } break;
663 case ELF::R_PPC64_REL24 : {
664 uint64_t FinalAddress = (Section.LoadAddress + Offset);
665 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
666 if (SignExtend32<24>(delta) != delta)
667 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
668 // Generates a 'bl <address>' instruction
669 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
670 } break;
671 case ELF::R_PPC64_REL32 : {
672 uint64_t FinalAddress = (Section.LoadAddress + Offset);
673 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
674 if (SignExtend32<32>(delta) != delta)
675 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
676 writeInt32BE(LocalAddress, delta);
677 } break;
678 case ELF::R_PPC64_REL64: {
679 uint64_t FinalAddress = (Section.LoadAddress + Offset);
680 uint64_t Delta = Value - FinalAddress + Addend;
681 writeInt64BE(LocalAddress, Delta);
682 } break;
683 case ELF::R_PPC64_ADDR64 :
684 writeInt64BE(LocalAddress, Value + Addend);
685 break;
686 case ELF::R_PPC64_TOC :
687 writeInt64BE(LocalAddress, findPPC64TOC());
688 break;
689 case ELF::R_PPC64_TOC16 : {
690 uint64_t TOCStart = findPPC64TOC();
691 Value = applyPPClo((Value + Addend) - TOCStart);
692 writeInt16BE(LocalAddress, applyPPClo(Value));
693 } break;
694 case ELF::R_PPC64_TOC16_DS : {
695 uint64_t TOCStart = findPPC64TOC();
696 Value = ((Value + Addend) - TOCStart);
697 writeInt16BE(LocalAddress, applyPPClo(Value));
698 } break;
699 }
700 }
701
resolveSystemZRelocation(const SectionEntry & Section,uint64_t Offset,uint64_t Value,uint32_t Type,int64_t Addend)702 void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
703 uint64_t Offset,
704 uint64_t Value,
705 uint32_t Type,
706 int64_t Addend) {
707 uint8_t *LocalAddress = Section.Address + Offset;
708 switch (Type) {
709 default:
710 llvm_unreachable("Relocation type not implemented yet!");
711 break;
712 case ELF::R_390_PC16DBL:
713 case ELF::R_390_PLT16DBL: {
714 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
715 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
716 writeInt16BE(LocalAddress, Delta / 2);
717 break;
718 }
719 case ELF::R_390_PC32DBL:
720 case ELF::R_390_PLT32DBL: {
721 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
722 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
723 writeInt32BE(LocalAddress, Delta / 2);
724 break;
725 }
726 case ELF::R_390_PC32: {
727 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
728 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
729 writeInt32BE(LocalAddress, Delta);
730 break;
731 }
732 case ELF::R_390_64:
733 writeInt64BE(LocalAddress, Value + Addend);
734 break;
735 }
736 }
737
resolveRelocation(const RelocationEntry & RE,uint64_t Value)738 void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
739 uint64_t Value) {
740 const SectionEntry &Section = Sections[RE.SectionID];
741 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend);
742 }
743
resolveRelocation(const SectionEntry & Section,uint64_t Offset,uint64_t Value,uint32_t Type,int64_t Addend)744 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
745 uint64_t Offset,
746 uint64_t Value,
747 uint32_t Type,
748 int64_t Addend) {
749 switch (Arch) {
750 case Triple::x86_64:
751 resolveX86_64Relocation(Section, Offset, Value, Type, Addend);
752 break;
753 case Triple::x86:
754 resolveX86Relocation(Section, Offset,
755 (uint32_t)(Value & 0xffffffffL), Type,
756 (uint32_t)(Addend & 0xffffffffL));
757 break;
758 case Triple::aarch64:
759 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
760 break;
761 case Triple::arm: // Fall through.
762 case Triple::thumb:
763 resolveARMRelocation(Section, Offset,
764 (uint32_t)(Value & 0xffffffffL), Type,
765 (uint32_t)(Addend & 0xffffffffL));
766 break;
767 case Triple::mips: // Fall through.
768 case Triple::mipsel:
769 resolveMIPSRelocation(Section, Offset,
770 (uint32_t)(Value & 0xffffffffL), Type,
771 (uint32_t)(Addend & 0xffffffffL));
772 break;
773 case Triple::ppc64: // Fall through.
774 case Triple::ppc64le:
775 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
776 break;
777 case Triple::systemz:
778 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
779 break;
780 default: llvm_unreachable("Unsupported CPU type!");
781 }
782 }
783
processRelocationRef(unsigned SectionID,RelocationRef RelI,ObjectImage & Obj,ObjSectionToIDMap & ObjSectionToID,const SymbolTableMap & Symbols,StubMap & Stubs)784 void RuntimeDyldELF::processRelocationRef(unsigned SectionID,
785 RelocationRef RelI,
786 ObjectImage &Obj,
787 ObjSectionToIDMap &ObjSectionToID,
788 const SymbolTableMap &Symbols,
789 StubMap &Stubs) {
790 uint64_t RelType;
791 Check(RelI.getType(RelType));
792 int64_t Addend;
793 Check(getELFRelocationAddend(RelI, Addend));
794 symbol_iterator Symbol = RelI.getSymbol();
795
796 // Obtain the symbol name which is referenced in the relocation
797 StringRef TargetName;
798 if (Symbol != Obj.end_symbols())
799 Symbol->getName(TargetName);
800 DEBUG(dbgs() << "\t\tRelType: " << RelType
801 << " Addend: " << Addend
802 << " TargetName: " << TargetName
803 << "\n");
804 RelocationValueRef Value;
805 // First search for the symbol in the local symbol table
806 SymbolTableMap::const_iterator lsi = Symbols.end();
807 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
808 if (Symbol != Obj.end_symbols()) {
809 lsi = Symbols.find(TargetName.data());
810 Symbol->getType(SymType);
811 }
812 if (lsi != Symbols.end()) {
813 Value.SectionID = lsi->second.first;
814 Value.Addend = lsi->second.second + Addend;
815 } else {
816 // Search for the symbol in the global symbol table
817 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end();
818 if (Symbol != Obj.end_symbols())
819 gsi = GlobalSymbolTable.find(TargetName.data());
820 if (gsi != GlobalSymbolTable.end()) {
821 Value.SectionID = gsi->second.first;
822 Value.Addend = gsi->second.second + Addend;
823 } else {
824 switch (SymType) {
825 case SymbolRef::ST_Debug: {
826 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
827 // and can be changed by another developers. Maybe best way is add
828 // a new symbol type ST_Section to SymbolRef and use it.
829 section_iterator si(Obj.end_sections());
830 Symbol->getSection(si);
831 if (si == Obj.end_sections())
832 llvm_unreachable("Symbol section not found, bad object file format!");
833 DEBUG(dbgs() << "\t\tThis is section symbol\n");
834 // Default to 'true' in case isText fails (though it never does).
835 bool isCode = true;
836 si->isText(isCode);
837 Value.SectionID = findOrEmitSection(Obj,
838 (*si),
839 isCode,
840 ObjSectionToID);
841 Value.Addend = Addend;
842 break;
843 }
844 case SymbolRef::ST_Unknown: {
845 Value.SymbolName = TargetName.data();
846 Value.Addend = Addend;
847 break;
848 }
849 default:
850 llvm_unreachable("Unresolved symbol type!");
851 break;
852 }
853 }
854 }
855 uint64_t Offset;
856 Check(RelI.getOffset(Offset));
857
858 DEBUG(dbgs() << "\t\tSectionID: " << SectionID
859 << " Offset: " << Offset
860 << "\n");
861 if (Arch == Triple::aarch64 &&
862 (RelType == ELF::R_AARCH64_CALL26 ||
863 RelType == ELF::R_AARCH64_JUMP26)) {
864 // This is an AArch64 branch relocation, need to use a stub function.
865 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
866 SectionEntry &Section = Sections[SectionID];
867
868 // Look for an existing stub.
869 StubMap::const_iterator i = Stubs.find(Value);
870 if (i != Stubs.end()) {
871 resolveRelocation(Section, Offset,
872 (uint64_t)Section.Address + i->second, RelType, 0);
873 DEBUG(dbgs() << " Stub function found\n");
874 } else {
875 // Create a new stub function.
876 DEBUG(dbgs() << " Create a new stub function\n");
877 Stubs[Value] = Section.StubOffset;
878 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
879 Section.StubOffset);
880
881 RelocationEntry REmovz_g3(SectionID,
882 StubTargetAddr - Section.Address,
883 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
884 RelocationEntry REmovk_g2(SectionID,
885 StubTargetAddr - Section.Address + 4,
886 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
887 RelocationEntry REmovk_g1(SectionID,
888 StubTargetAddr - Section.Address + 8,
889 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
890 RelocationEntry REmovk_g0(SectionID,
891 StubTargetAddr - Section.Address + 12,
892 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
893
894 if (Value.SymbolName) {
895 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
896 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
897 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
898 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
899 } else {
900 addRelocationForSection(REmovz_g3, Value.SectionID);
901 addRelocationForSection(REmovk_g2, Value.SectionID);
902 addRelocationForSection(REmovk_g1, Value.SectionID);
903 addRelocationForSection(REmovk_g0, Value.SectionID);
904 }
905 resolveRelocation(Section, Offset,
906 (uint64_t)Section.Address + Section.StubOffset,
907 RelType, 0);
908 Section.StubOffset += getMaxStubSize();
909 }
910 } else if (Arch == Triple::arm &&
911 (RelType == ELF::R_ARM_PC24 ||
912 RelType == ELF::R_ARM_CALL ||
913 RelType == ELF::R_ARM_JUMP24)) {
914 // This is an ARM branch relocation, need to use a stub function.
915 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
916 SectionEntry &Section = Sections[SectionID];
917
918 // Look for an existing stub.
919 StubMap::const_iterator i = Stubs.find(Value);
920 if (i != Stubs.end()) {
921 resolveRelocation(Section, Offset,
922 (uint64_t)Section.Address + i->second, RelType, 0);
923 DEBUG(dbgs() << " Stub function found\n");
924 } else {
925 // Create a new stub function.
926 DEBUG(dbgs() << " Create a new stub function\n");
927 Stubs[Value] = Section.StubOffset;
928 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
929 Section.StubOffset);
930 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
931 ELF::R_ARM_PRIVATE_0, Value.Addend);
932 if (Value.SymbolName)
933 addRelocationForSymbol(RE, Value.SymbolName);
934 else
935 addRelocationForSection(RE, Value.SectionID);
936
937 resolveRelocation(Section, Offset,
938 (uint64_t)Section.Address + Section.StubOffset,
939 RelType, 0);
940 Section.StubOffset += getMaxStubSize();
941 }
942 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
943 RelType == ELF::R_MIPS_26) {
944 // This is an Mips branch relocation, need to use a stub function.
945 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
946 SectionEntry &Section = Sections[SectionID];
947 uint8_t *Target = Section.Address + Offset;
948 uint32_t *TargetAddress = (uint32_t *)Target;
949
950 // Extract the addend from the instruction.
951 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
952
953 Value.Addend += Addend;
954
955 // Look up for existing stub.
956 StubMap::const_iterator i = Stubs.find(Value);
957 if (i != Stubs.end()) {
958 resolveRelocation(Section, Offset,
959 (uint64_t)Section.Address + i->second, RelType, 0);
960 DEBUG(dbgs() << " Stub function found\n");
961 } else {
962 // Create a new stub function.
963 DEBUG(dbgs() << " Create a new stub function\n");
964 Stubs[Value] = Section.StubOffset;
965 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
966 Section.StubOffset);
967
968 // Creating Hi and Lo relocations for the filled stub instructions.
969 RelocationEntry REHi(SectionID,
970 StubTargetAddr - Section.Address,
971 ELF::R_MIPS_UNUSED1, Value.Addend);
972 RelocationEntry RELo(SectionID,
973 StubTargetAddr - Section.Address + 4,
974 ELF::R_MIPS_UNUSED2, Value.Addend);
975
976 if (Value.SymbolName) {
977 addRelocationForSymbol(REHi, Value.SymbolName);
978 addRelocationForSymbol(RELo, Value.SymbolName);
979 } else {
980 addRelocationForSection(REHi, Value.SectionID);
981 addRelocationForSection(RELo, Value.SectionID);
982 }
983
984 resolveRelocation(Section, Offset,
985 (uint64_t)Section.Address + Section.StubOffset,
986 RelType, 0);
987 Section.StubOffset += getMaxStubSize();
988 }
989 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
990 if (RelType == ELF::R_PPC64_REL24) {
991 // A PPC branch relocation will need a stub function if the target is
992 // an external symbol (Symbol::ST_Unknown) or if the target address
993 // is not within the signed 24-bits branch address.
994 SectionEntry &Section = Sections[SectionID];
995 uint8_t *Target = Section.Address + Offset;
996 bool RangeOverflow = false;
997 if (SymType != SymbolRef::ST_Unknown) {
998 // A function call may points to the .opd entry, so the final symbol value
999 // in calculated based in the relocation values in .opd section.
1000 findOPDEntrySection(Obj, ObjSectionToID, Value);
1001 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
1002 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
1003 // If it is within 24-bits branch range, just set the branch target
1004 if (SignExtend32<24>(delta) == delta) {
1005 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1006 if (Value.SymbolName)
1007 addRelocationForSymbol(RE, Value.SymbolName);
1008 else
1009 addRelocationForSection(RE, Value.SectionID);
1010 } else {
1011 RangeOverflow = true;
1012 }
1013 }
1014 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
1015 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1016 // larger than 24-bits.
1017 StubMap::const_iterator i = Stubs.find(Value);
1018 if (i != Stubs.end()) {
1019 // Symbol function stub already created, just relocate to it
1020 resolveRelocation(Section, Offset,
1021 (uint64_t)Section.Address + i->second, RelType, 0);
1022 DEBUG(dbgs() << " Stub function found\n");
1023 } else {
1024 // Create a new stub function.
1025 DEBUG(dbgs() << " Create a new stub function\n");
1026 Stubs[Value] = Section.StubOffset;
1027 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1028 Section.StubOffset);
1029 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
1030 ELF::R_PPC64_ADDR64, Value.Addend);
1031
1032 // Generates the 64-bits address loads as exemplified in section
1033 // 4.5.1 in PPC64 ELF ABI.
1034 RelocationEntry REhst(SectionID,
1035 StubTargetAddr - Section.Address + 2,
1036 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
1037 RelocationEntry REhr(SectionID,
1038 StubTargetAddr - Section.Address + 6,
1039 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
1040 RelocationEntry REh(SectionID,
1041 StubTargetAddr - Section.Address + 14,
1042 ELF::R_PPC64_ADDR16_HI, Value.Addend);
1043 RelocationEntry REl(SectionID,
1044 StubTargetAddr - Section.Address + 18,
1045 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1046
1047 if (Value.SymbolName) {
1048 addRelocationForSymbol(REhst, Value.SymbolName);
1049 addRelocationForSymbol(REhr, Value.SymbolName);
1050 addRelocationForSymbol(REh, Value.SymbolName);
1051 addRelocationForSymbol(REl, Value.SymbolName);
1052 } else {
1053 addRelocationForSection(REhst, Value.SectionID);
1054 addRelocationForSection(REhr, Value.SectionID);
1055 addRelocationForSection(REh, Value.SectionID);
1056 addRelocationForSection(REl, Value.SectionID);
1057 }
1058
1059 resolveRelocation(Section, Offset,
1060 (uint64_t)Section.Address + Section.StubOffset,
1061 RelType, 0);
1062 if (SymType == SymbolRef::ST_Unknown)
1063 // Restore the TOC for external calls
1064 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
1065 Section.StubOffset += getMaxStubSize();
1066 }
1067 }
1068 } else {
1069 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1070 // Extra check to avoid relocation againt empty symbols (usually
1071 // the R_PPC64_TOC).
1072 if (Value.SymbolName && !TargetName.empty())
1073 addRelocationForSymbol(RE, Value.SymbolName);
1074 else
1075 addRelocationForSection(RE, Value.SectionID);
1076 }
1077 } else if (Arch == Triple::systemz &&
1078 (RelType == ELF::R_390_PLT32DBL ||
1079 RelType == ELF::R_390_GOTENT)) {
1080 // Create function stubs for both PLT and GOT references, regardless of
1081 // whether the GOT reference is to data or code. The stub contains the
1082 // full address of the symbol, as needed by GOT references, and the
1083 // executable part only adds an overhead of 8 bytes.
1084 //
1085 // We could try to conserve space by allocating the code and data
1086 // parts of the stub separately. However, as things stand, we allocate
1087 // a stub for every relocation, so using a GOT in JIT code should be
1088 // no less space efficient than using an explicit constant pool.
1089 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1090 SectionEntry &Section = Sections[SectionID];
1091
1092 // Look for an existing stub.
1093 StubMap::const_iterator i = Stubs.find(Value);
1094 uintptr_t StubAddress;
1095 if (i != Stubs.end()) {
1096 StubAddress = uintptr_t(Section.Address) + i->second;
1097 DEBUG(dbgs() << " Stub function found\n");
1098 } else {
1099 // Create a new stub function.
1100 DEBUG(dbgs() << " Create a new stub function\n");
1101
1102 uintptr_t BaseAddress = uintptr_t(Section.Address);
1103 uintptr_t StubAlignment = getStubAlignment();
1104 StubAddress = (BaseAddress + Section.StubOffset +
1105 StubAlignment - 1) & -StubAlignment;
1106 unsigned StubOffset = StubAddress - BaseAddress;
1107
1108 Stubs[Value] = StubOffset;
1109 createStubFunction((uint8_t *)StubAddress);
1110 RelocationEntry RE(SectionID, StubOffset + 8,
1111 ELF::R_390_64, Value.Addend - Addend);
1112 if (Value.SymbolName)
1113 addRelocationForSymbol(RE, Value.SymbolName);
1114 else
1115 addRelocationForSection(RE, Value.SectionID);
1116 Section.StubOffset = StubOffset + getMaxStubSize();
1117 }
1118
1119 if (RelType == ELF::R_390_GOTENT)
1120 resolveRelocation(Section, Offset, StubAddress + 8,
1121 ELF::R_390_PC32DBL, Addend);
1122 else
1123 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
1124 } else {
1125 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1126 if (Value.SymbolName)
1127 addRelocationForSymbol(RE, Value.SymbolName);
1128 else
1129 addRelocationForSection(RE, Value.SectionID);
1130 }
1131 }
1132
isCompatibleFormat(const ObjectBuffer * Buffer) const1133 bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1134 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1135 return false;
1136 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
1137 }
1138 } // namespace llvm
1139