Add EH support to the MCJIT.
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / RuntimeDyldELF.cpp
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
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
65   static inline bool classof(const Binary *v) {
66     return (isa<ELFObjectFile<ELFT> >(v)
67             && classof(cast<ELFObjectFile
68                 <ELFT> >(v)));
69   }
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:
83     ELFObjectImage(ObjectBuffer *Input,
84                  DyldELFObject<ELFT> *Obj)
85     : ObjectImageCommon(Input, Obj),
86       DyldObj(Obj),
87       Registered(false) {}
88
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
96     virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
97     {
98       DyldObj->updateSectionAddress(Sec, Addr);
99     }
100
101     virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
102     {
103       DyldObj->updateSymbolAddress(Sym, Addr);
104     }
105
106     virtual void registerWithDebugger()
107     {
108       JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
109       Registered = true;
110     }
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>
121 DyldELFObject<ELFT>::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
122   : ELFObjectFile<ELFT>(Wrapper, ec) {
123   this->isDyldELFObject = true;
124 }
125
126 template<class ELFT>
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>
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
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
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
198 RuntimeDyldELF::~RuntimeDyldELF() {
199 }
200
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
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
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: { // test-shift.ll (.eh_frame)
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     // Immediate goes in bits 25:0 of B and BL.
322     *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
323     break;
324   }
325   case ELF::R_AARCH64_MOVW_UABS_G3: {
326     uint64_t Result = Value + Addend;
327     // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
328     *TargetPtr |= Result >> (48 - 5);
329     // Shift is "lsl #48", in bits 22:21
330     *TargetPtr |= 3 << 21;
331     break;
332   }
333   case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
334     uint64_t Result = Value + Addend;
335     // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
336     *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
337     // Shift is "lsl #32", in bits 22:21
338     *TargetPtr |= 2 << 21;
339     break;
340   }
341   case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
342     uint64_t Result = Value + Addend;
343     // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
344     *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
345     // Shift is "lsl #16", in bits 22:21
346     *TargetPtr |= 1 << 21;
347     break;
348   }
349   case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
350     uint64_t Result = Value + Addend;
351     // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
352     *TargetPtr |= ((Result & 0xffffU) << 5);
353     // Shift is "lsl #0", in bits 22:21. No action needed.
354     break;
355   }
356   }
357 }
358
359 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
360                                           uint64_t Offset,
361                                           uint32_t Value,
362                                           uint32_t Type,
363                                           int32_t Addend) {
364   // TODO: Add Thumb relocations.
365   uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
366   uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
367   Value += Addend;
368
369   DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
370                << Section.Address + Offset
371                << " FinalAddress: " << format("%p",FinalAddress)
372                << " Value: " << format("%x",Value)
373                << " Type: " << format("%x",Type)
374                << " Addend: " << format("%x",Addend)
375                << "\n");
376
377   switch(Type) {
378   default:
379     llvm_unreachable("Not implemented relocation type!");
380
381   // Write a 32bit value to relocation address, taking into account the
382   // implicit addend encoded in the target.
383   case ELF::R_ARM_TARGET1 :
384   case ELF::R_ARM_ABS32 :
385     *TargetPtr += Value;
386     break;
387
388   // Write first 16 bit of 32 bit value to the mov instruction.
389   // Last 4 bit should be shifted.
390   case ELF::R_ARM_MOVW_ABS_NC :
391     // We are not expecting any other addend in the relocation address.
392     // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
393     // non-contiguous fields.
394     assert((*TargetPtr & 0x000F0FFF) == 0);
395     Value = Value & 0xFFFF;
396     *TargetPtr |= Value & 0xFFF;
397     *TargetPtr |= ((Value >> 12) & 0xF) << 16;
398     break;
399
400   // Write last 16 bit of 32 bit value to the mov instruction.
401   // Last 4 bit should be shifted.
402   case ELF::R_ARM_MOVT_ABS :
403     // We are not expecting any other addend in the relocation address.
404     // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
405     assert((*TargetPtr & 0x000F0FFF) == 0);
406     Value = (Value >> 16) & 0xFFFF;
407     *TargetPtr |= Value & 0xFFF;
408     *TargetPtr |= ((Value >> 12) & 0xF) << 16;
409     break;
410
411   // Write 24 bit relative value to the branch instruction.
412   case ELF::R_ARM_PC24 :    // Fall through.
413   case ELF::R_ARM_CALL :    // Fall through.
414   case ELF::R_ARM_JUMP24 :
415     int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
416     RelValue = (RelValue & 0x03FFFFFC) >> 2;
417     *TargetPtr &= 0xFF000000;
418     *TargetPtr |= RelValue;
419     break;
420   }
421 }
422
423 void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
424                                            uint64_t Offset,
425                                            uint32_t Value,
426                                            uint32_t Type,
427                                            int32_t Addend) {
428   uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
429   Value += Addend;
430
431   DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
432                << Section.Address + Offset
433                << " FinalAddress: "
434                << format("%p",Section.LoadAddress + Offset)
435                << " Value: " << format("%x",Value)
436                << " Type: " << format("%x",Type)
437                << " Addend: " << format("%x",Addend)
438                << "\n");
439
440   switch(Type) {
441   default:
442     llvm_unreachable("Not implemented relocation type!");
443     break;
444   case ELF::R_MIPS_32:
445     *TargetPtr = Value + (*TargetPtr);
446     break;
447   case ELF::R_MIPS_26:
448     *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
449     break;
450   case ELF::R_MIPS_HI16:
451     // Get the higher 16-bits. Also add 1 if bit 15 is 1.
452     Value += ((*TargetPtr) & 0x0000ffff) << 16;
453     *TargetPtr = ((*TargetPtr) & 0xffff0000) |
454                  (((Value + 0x8000) >> 16) & 0xffff);
455     break;
456    case ELF::R_MIPS_LO16:
457     Value += ((*TargetPtr) & 0x0000ffff);
458     *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
459     break;
460    }
461 }
462
463 // Return the .TOC. section address to R_PPC64_TOC relocations.
464 uint64_t RuntimeDyldELF::findPPC64TOC() const {
465   // The TOC consists of sections .got, .toc, .tocbss, .plt in that
466   // order. The TOC starts where the first of these sections starts.
467   SectionList::const_iterator it = Sections.begin();
468   SectionList::const_iterator ite = Sections.end();
469   for (; it != ite; ++it) {
470     if (it->Name == ".got" ||
471         it->Name == ".toc" ||
472         it->Name == ".tocbss" ||
473         it->Name == ".plt")
474       break;
475   }
476   if (it == ite) {
477     // This may happen for
478     // * references to TOC base base (sym@toc, .odp relocation) without
479     // a .toc directive.
480     // In this case just use the first section (which is usually
481     // the .odp) since the code won't reference the .toc base
482     // directly.
483     it = Sections.begin();
484   }
485   assert (it != ite);
486   // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
487   // thus permitting a full 64 Kbytes segment.
488   return it->LoadAddress + 0x8000;
489 }
490
491 // Returns the sections and offset associated with the ODP entry referenced
492 // by Symbol.
493 void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
494                                          ObjSectionToIDMap &LocalSections,
495                                          RelocationValueRef &Rel) {
496   // Get the ELF symbol value (st_value) to compare with Relocation offset in
497   // .opd entries
498
499   error_code err;
500   for (section_iterator si = Obj.begin_sections(),
501      se = Obj.end_sections(); si != se; si.increment(err)) {
502     StringRef SectionName;
503     check(si->getName(SectionName));
504     if (SectionName != ".opd")
505       continue;
506
507     for (relocation_iterator i = si->begin_relocations(),
508          e = si->end_relocations(); i != e;) {
509       check(err);
510
511       // The R_PPC64_ADDR64 relocation indicates the first field
512       // of a .opd entry
513       uint64_t TypeFunc;
514       check(i->getType(TypeFunc));
515       if (TypeFunc != ELF::R_PPC64_ADDR64) {
516         i.increment(err);
517         continue;
518       }
519
520       SymbolRef TargetSymbol;
521       uint64_t TargetSymbolOffset;
522       int64_t TargetAdditionalInfo;
523       check(i->getSymbol(TargetSymbol));
524       check(i->getOffset(TargetSymbolOffset));
525       check(i->getAdditionalInfo(TargetAdditionalInfo));
526
527       i = i.increment(err);
528       if (i == e)
529         break;
530       check(err);
531
532       // Just check if following relocation is a R_PPC64_TOC
533       uint64_t TypeTOC;
534       check(i->getType(TypeTOC));
535       if (TypeTOC != ELF::R_PPC64_TOC)
536         continue;
537
538       // Finally compares the Symbol value and the target symbol offset
539       // to check if this .opd entry refers to the symbol the relocation
540       // points to.
541       if (Rel.Addend != (intptr_t)TargetSymbolOffset)
542         continue;
543
544       section_iterator tsi(Obj.end_sections());
545       check(TargetSymbol.getSection(tsi));
546       Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
547       Rel.Addend = (intptr_t)TargetAdditionalInfo;
548       return;
549     }
550   }
551   llvm_unreachable("Attempting to get address of ODP entry!");
552 }
553
554 // Relocation masks following the #lo(value), #hi(value), #higher(value),
555 // and #highest(value) macros defined in section 4.5.1. Relocation Types
556 // in PPC-elf64abi document.
557 //
558 static inline
559 uint16_t applyPPClo (uint64_t value)
560 {
561   return value & 0xffff;
562 }
563
564 static inline
565 uint16_t applyPPChi (uint64_t value)
566 {
567   return (value >> 16) & 0xffff;
568 }
569
570 static inline
571 uint16_t applyPPChigher (uint64_t value)
572 {
573   return (value >> 32) & 0xffff;
574 }
575
576 static inline
577 uint16_t applyPPChighest (uint64_t value)
578 {
579   return (value >> 48) & 0xffff;
580 }
581
582 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
583                                             uint64_t Offset,
584                                             uint64_t Value,
585                                             uint32_t Type,
586                                             int64_t Addend) {
587   uint8_t* LocalAddress = Section.Address + Offset;
588   switch (Type) {
589   default:
590     llvm_unreachable("Relocation type not implemented yet!");
591   break;
592   case ELF::R_PPC64_ADDR16_LO :
593     writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
594     break;
595   case ELF::R_PPC64_ADDR16_HI :
596     writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
597     break;
598   case ELF::R_PPC64_ADDR16_HIGHER :
599     writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
600     break;
601   case ELF::R_PPC64_ADDR16_HIGHEST :
602     writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
603     break;
604   case ELF::R_PPC64_ADDR14 : {
605     assert(((Value + Addend) & 3) == 0);
606     // Preserve the AA/LK bits in the branch instruction
607     uint8_t aalk = *(LocalAddress+3);
608     writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
609   } break;
610   case ELF::R_PPC64_ADDR32 : {
611     int32_t Result = static_cast<int32_t>(Value + Addend);
612     if (SignExtend32<32>(Result) != Result)
613       llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
614     writeInt32BE(LocalAddress, Result);
615   } break;
616   case ELF::R_PPC64_REL24 : {
617     uint64_t FinalAddress = (Section.LoadAddress + Offset);
618     int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
619     if (SignExtend32<24>(delta) != delta)
620       llvm_unreachable("Relocation R_PPC64_REL24 overflow");
621     // Generates a 'bl <address>' instruction
622     writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
623   } break;
624   case ELF::R_PPC64_REL32 : {
625     uint64_t FinalAddress = (Section.LoadAddress + Offset);
626     int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
627     if (SignExtend32<32>(delta) != delta)
628       llvm_unreachable("Relocation R_PPC64_REL32 overflow");
629     writeInt32BE(LocalAddress, delta);
630   } break;
631   case ELF::R_PPC64_ADDR64 :
632     writeInt64BE(LocalAddress, Value + Addend);
633     break;
634   case ELF::R_PPC64_TOC :
635     writeInt64BE(LocalAddress, findPPC64TOC());
636     break;
637   case ELF::R_PPC64_TOC16 : {
638     uint64_t TOCStart = findPPC64TOC();
639     Value = applyPPClo((Value + Addend) - TOCStart);
640     writeInt16BE(LocalAddress, applyPPClo(Value));
641   } break;
642   case ELF::R_PPC64_TOC16_DS : {
643     uint64_t TOCStart = findPPC64TOC();
644     Value = ((Value + Addend) - TOCStart);
645     writeInt16BE(LocalAddress, applyPPClo(Value));
646   } break;
647   }
648 }
649
650 void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
651                                               uint64_t Offset,
652                                               uint64_t Value,
653                                               uint32_t Type,
654                                               int64_t Addend) {
655   uint8_t *LocalAddress = Section.Address + Offset;
656   switch (Type) {
657   default:
658     llvm_unreachable("Relocation type not implemented yet!");
659     break;
660   case ELF::R_390_PC16DBL:
661   case ELF::R_390_PLT16DBL: {
662     int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
663     assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
664     writeInt16BE(LocalAddress, Delta / 2);
665     break;
666   }
667   case ELF::R_390_PC32DBL:
668   case ELF::R_390_PLT32DBL: {
669     int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
670     assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
671     writeInt32BE(LocalAddress, Delta / 2);
672     break;
673   }
674   case ELF::R_390_PC32: {
675     int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
676     assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
677     writeInt32BE(LocalAddress, Delta);
678     break;
679   }
680   case ELF::R_390_64:
681     writeInt64BE(LocalAddress, Value + Addend);
682     break;
683   }
684 }
685
686 void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
687                                        uint64_t Value) {
688   const SectionEntry &Section = Sections[RE.SectionID];
689   return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend);
690 }
691
692 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
693                                        uint64_t Offset,
694                                        uint64_t Value,
695                                        uint32_t Type,
696                                        int64_t Addend) {
697   switch (Arch) {
698   case Triple::x86_64:
699     resolveX86_64Relocation(Section, Offset, Value, Type, Addend);
700     break;
701   case Triple::x86:
702     resolveX86Relocation(Section, Offset,
703                          (uint32_t)(Value & 0xffffffffL), Type,
704                          (uint32_t)(Addend & 0xffffffffL));
705     break;
706   case Triple::aarch64:
707     resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
708     break;
709   case Triple::arm:    // Fall through.
710   case Triple::thumb:
711     resolveARMRelocation(Section, Offset,
712                          (uint32_t)(Value & 0xffffffffL), Type,
713                          (uint32_t)(Addend & 0xffffffffL));
714     break;
715   case Triple::mips:    // Fall through.
716   case Triple::mipsel:
717     resolveMIPSRelocation(Section, Offset,
718                           (uint32_t)(Value & 0xffffffffL), Type,
719                           (uint32_t)(Addend & 0xffffffffL));
720     break;
721   case Triple::ppc64:
722     resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
723     break;
724   case Triple::systemz:
725     resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
726     break;
727   default: llvm_unreachable("Unsupported CPU type!");
728   }
729 }
730
731 void RuntimeDyldELF::processRelocationRef(unsigned SectionID,
732                                           RelocationRef RelI,
733                                           ObjectImage &Obj,
734                                           ObjSectionToIDMap &ObjSectionToID,
735                                           const SymbolTableMap &Symbols,
736                                           StubMap &Stubs) {
737   uint64_t RelType;
738   Check(RelI.getType(RelType));
739   int64_t Addend;
740   Check(RelI.getAdditionalInfo(Addend));
741   SymbolRef Symbol;
742   Check(RelI.getSymbol(Symbol));
743
744   // Obtain the symbol name which is referenced in the relocation
745   StringRef TargetName;
746   Symbol.getName(TargetName);
747   DEBUG(dbgs() << "\t\tRelType: " << RelType
748                << " Addend: " << Addend
749                << " TargetName: " << TargetName
750                << "\n");
751   RelocationValueRef Value;
752   // First search for the symbol in the local symbol table
753   SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
754   SymbolRef::Type SymType;
755   Symbol.getType(SymType);
756   if (lsi != Symbols.end()) {
757     Value.SectionID = lsi->second.first;
758     Value.Addend = lsi->second.second + Addend;
759   } else {
760     // Search for the symbol in the global symbol table
761     SymbolTableMap::const_iterator gsi =
762         GlobalSymbolTable.find(TargetName.data());
763     if (gsi != GlobalSymbolTable.end()) {
764       Value.SectionID = gsi->second.first;
765       Value.Addend = gsi->second.second + Addend;
766     } else {
767       switch (SymType) {
768         case SymbolRef::ST_Debug: {
769           // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
770           // and can be changed by another developers. Maybe best way is add
771           // a new symbol type ST_Section to SymbolRef and use it.
772           section_iterator si(Obj.end_sections());
773           Symbol.getSection(si);
774           if (si == Obj.end_sections())
775             llvm_unreachable("Symbol section not found, bad object file format!");
776           DEBUG(dbgs() << "\t\tThis is section symbol\n");
777           // Default to 'true' in case isText fails (though it never does).
778           bool isCode = true;
779           si->isText(isCode);
780           Value.SectionID = findOrEmitSection(Obj,
781                                               (*si),
782                                               isCode,
783                                               ObjSectionToID);
784           Value.Addend = Addend;
785           break;
786         }
787         case SymbolRef::ST_Unknown: {
788           Value.SymbolName = TargetName.data();
789           Value.Addend = Addend;
790           break;
791         }
792         default:
793           llvm_unreachable("Unresolved symbol type!");
794           break;
795       }
796     }
797   }
798   uint64_t Offset;
799   Check(RelI.getOffset(Offset));
800
801   DEBUG(dbgs() << "\t\tSectionID: " << SectionID
802                << " Offset: " << Offset
803                << "\n");
804   if (Arch == Triple::aarch64 &&
805       (RelType == ELF::R_AARCH64_CALL26 ||
806        RelType == ELF::R_AARCH64_JUMP26)) {
807     // This is an AArch64 branch relocation, need to use a stub function.
808     DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
809     SectionEntry &Section = Sections[SectionID];
810
811     // Look for an existing stub.
812     StubMap::const_iterator i = Stubs.find(Value);
813     if (i != Stubs.end()) {
814         resolveRelocation(Section, Offset,
815                           (uint64_t)Section.Address + i->second, RelType, 0);
816       DEBUG(dbgs() << " Stub function found\n");
817     } else {
818       // Create a new stub function.
819       DEBUG(dbgs() << " Create a new stub function\n");
820       Stubs[Value] = Section.StubOffset;
821       uint8_t *StubTargetAddr = createStubFunction(Section.Address +
822                                                    Section.StubOffset);
823
824       RelocationEntry REmovz_g3(SectionID,
825                                 StubTargetAddr - Section.Address,
826                                 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
827       RelocationEntry REmovk_g2(SectionID,
828                                 StubTargetAddr - Section.Address + 4,
829                                 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
830       RelocationEntry REmovk_g1(SectionID,
831                                 StubTargetAddr - Section.Address + 8,
832                                 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
833       RelocationEntry REmovk_g0(SectionID,
834                                 StubTargetAddr - Section.Address + 12,
835                                 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
836
837       if (Value.SymbolName) {
838         addRelocationForSymbol(REmovz_g3, Value.SymbolName);
839         addRelocationForSymbol(REmovk_g2, Value.SymbolName);
840         addRelocationForSymbol(REmovk_g1, Value.SymbolName);
841         addRelocationForSymbol(REmovk_g0, Value.SymbolName);
842       } else {
843         addRelocationForSection(REmovz_g3, Value.SectionID);
844         addRelocationForSection(REmovk_g2, Value.SectionID);
845         addRelocationForSection(REmovk_g1, Value.SectionID);
846         addRelocationForSection(REmovk_g0, Value.SectionID);
847       }
848       resolveRelocation(Section, Offset,
849                         (uint64_t)Section.Address + Section.StubOffset,
850                         RelType, 0);
851       Section.StubOffset += getMaxStubSize();
852     }
853   } else if (Arch == Triple::arm &&
854       (RelType == ELF::R_ARM_PC24 ||
855        RelType == ELF::R_ARM_CALL ||
856        RelType == ELF::R_ARM_JUMP24)) {
857     // This is an ARM branch relocation, need to use a stub function.
858     DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
859     SectionEntry &Section = Sections[SectionID];
860
861     // Look for an existing stub.
862     StubMap::const_iterator i = Stubs.find(Value);
863     if (i != Stubs.end()) {
864         resolveRelocation(Section, Offset,
865                           (uint64_t)Section.Address + i->second, RelType, 0);
866       DEBUG(dbgs() << " Stub function found\n");
867     } else {
868       // Create a new stub function.
869       DEBUG(dbgs() << " Create a new stub function\n");
870       Stubs[Value] = Section.StubOffset;
871       uint8_t *StubTargetAddr = createStubFunction(Section.Address +
872                                                    Section.StubOffset);
873       RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
874                          ELF::R_ARM_ABS32, Value.Addend);
875       if (Value.SymbolName)
876         addRelocationForSymbol(RE, Value.SymbolName);
877       else
878         addRelocationForSection(RE, Value.SectionID);
879
880       resolveRelocation(Section, Offset,
881                         (uint64_t)Section.Address + Section.StubOffset,
882                         RelType, 0);
883       Section.StubOffset += getMaxStubSize();
884     }
885   } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
886              RelType == ELF::R_MIPS_26) {
887     // This is an Mips branch relocation, need to use a stub function.
888     DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
889     SectionEntry &Section = Sections[SectionID];
890     uint8_t *Target = Section.Address + Offset;
891     uint32_t *TargetAddress = (uint32_t *)Target;
892
893     // Extract the addend from the instruction.
894     uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
895
896     Value.Addend += Addend;
897
898     //  Look up for existing stub.
899     StubMap::const_iterator i = Stubs.find(Value);
900     if (i != Stubs.end()) {
901       resolveRelocation(Section, Offset,
902                         (uint64_t)Section.Address + i->second, RelType, 0);
903       DEBUG(dbgs() << " Stub function found\n");
904     } else {
905       // Create a new stub function.
906       DEBUG(dbgs() << " Create a new stub function\n");
907       Stubs[Value] = Section.StubOffset;
908       uint8_t *StubTargetAddr = createStubFunction(Section.Address +
909                                                    Section.StubOffset);
910
911       // Creating Hi and Lo relocations for the filled stub instructions.
912       RelocationEntry REHi(SectionID,
913                            StubTargetAddr - Section.Address,
914                            ELF::R_MIPS_HI16, Value.Addend);
915       RelocationEntry RELo(SectionID,
916                            StubTargetAddr - Section.Address + 4,
917                            ELF::R_MIPS_LO16, Value.Addend);
918
919       if (Value.SymbolName) {
920         addRelocationForSymbol(REHi, Value.SymbolName);
921         addRelocationForSymbol(RELo, Value.SymbolName);
922       } else {
923         addRelocationForSection(REHi, Value.SectionID);
924         addRelocationForSection(RELo, Value.SectionID);
925       }
926
927       resolveRelocation(Section, Offset,
928                         (uint64_t)Section.Address + Section.StubOffset,
929                         RelType, 0);
930       Section.StubOffset += getMaxStubSize();
931     }
932   } else if (Arch == Triple::ppc64) {
933     if (RelType == ELF::R_PPC64_REL24) {
934       // A PPC branch relocation will need a stub function if the target is
935       // an external symbol (Symbol::ST_Unknown) or if the target address
936       // is not within the signed 24-bits branch address.
937       SectionEntry &Section = Sections[SectionID];
938       uint8_t *Target = Section.Address + Offset;
939       bool RangeOverflow = false;
940       if (SymType != SymbolRef::ST_Unknown) {
941         // A function call may points to the .opd entry, so the final symbol value
942         // in calculated based in the relocation values in .opd section.
943         findOPDEntrySection(Obj, ObjSectionToID, Value);
944         uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
945         int32_t delta = static_cast<int32_t>(Target - RelocTarget);
946         // If it is within 24-bits branch range, just set the branch target
947         if (SignExtend32<24>(delta) == delta) {
948           RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
949           if (Value.SymbolName)
950             addRelocationForSymbol(RE, Value.SymbolName);
951           else
952             addRelocationForSection(RE, Value.SectionID);
953         } else {
954           RangeOverflow = true;
955         }
956       }
957       if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
958         // It is an external symbol (SymbolRef::ST_Unknown) or within a range
959         // larger than 24-bits.
960         StubMap::const_iterator i = Stubs.find(Value);
961         if (i != Stubs.end()) {
962           // Symbol function stub already created, just relocate to it
963           resolveRelocation(Section, Offset,
964                             (uint64_t)Section.Address + i->second, RelType, 0);
965           DEBUG(dbgs() << " Stub function found\n");
966         } else {
967           // Create a new stub function.
968           DEBUG(dbgs() << " Create a new stub function\n");
969           Stubs[Value] = Section.StubOffset;
970           uint8_t *StubTargetAddr = createStubFunction(Section.Address +
971                                                        Section.StubOffset);
972           RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
973                              ELF::R_PPC64_ADDR64, Value.Addend);
974
975           // Generates the 64-bits address loads as exemplified in section
976           // 4.5.1 in PPC64 ELF ABI.
977           RelocationEntry REhst(SectionID,
978                                 StubTargetAddr - Section.Address + 2,
979                                 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
980           RelocationEntry REhr(SectionID,
981                                StubTargetAddr - Section.Address + 6,
982                                ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
983           RelocationEntry REh(SectionID,
984                               StubTargetAddr - Section.Address + 14,
985                               ELF::R_PPC64_ADDR16_HI, Value.Addend);
986           RelocationEntry REl(SectionID,
987                               StubTargetAddr - Section.Address + 18,
988                               ELF::R_PPC64_ADDR16_LO, Value.Addend);
989
990           if (Value.SymbolName) {
991             addRelocationForSymbol(REhst, Value.SymbolName);
992             addRelocationForSymbol(REhr,  Value.SymbolName);
993             addRelocationForSymbol(REh,   Value.SymbolName);
994             addRelocationForSymbol(REl,   Value.SymbolName);
995           } else {
996             addRelocationForSection(REhst, Value.SectionID);
997             addRelocationForSection(REhr,  Value.SectionID);
998             addRelocationForSection(REh,   Value.SectionID);
999             addRelocationForSection(REl,   Value.SectionID);
1000           }
1001
1002           resolveRelocation(Section, Offset,
1003                             (uint64_t)Section.Address + Section.StubOffset,
1004                             RelType, 0);
1005           if (SymType == SymbolRef::ST_Unknown)
1006             // Restore the TOC for external calls
1007             writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
1008           Section.StubOffset += getMaxStubSize();
1009         }
1010       }
1011     } else {
1012       RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1013       // Extra check to avoid relocation againt empty symbols (usually
1014       // the R_PPC64_TOC).
1015       if (Value.SymbolName && !TargetName.empty())
1016         addRelocationForSymbol(RE, Value.SymbolName);
1017       else
1018         addRelocationForSection(RE, Value.SectionID);
1019     }
1020   } else if (Arch == Triple::systemz &&
1021              (RelType == ELF::R_390_PLT32DBL ||
1022               RelType == ELF::R_390_GOTENT)) {
1023     // Create function stubs for both PLT and GOT references, regardless of
1024     // whether the GOT reference is to data or code.  The stub contains the
1025     // full address of the symbol, as needed by GOT references, and the
1026     // executable part only adds an overhead of 8 bytes.
1027     //
1028     // We could try to conserve space by allocating the code and data
1029     // parts of the stub separately.  However, as things stand, we allocate
1030     // a stub for every relocation, so using a GOT in JIT code should be
1031     // no less space efficient than using an explicit constant pool.
1032     DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1033     SectionEntry &Section = Sections[SectionID];
1034
1035     // Look for an existing stub.
1036     StubMap::const_iterator i = Stubs.find(Value);
1037     uintptr_t StubAddress;
1038     if (i != Stubs.end()) {
1039       StubAddress = uintptr_t(Section.Address) + i->second;
1040       DEBUG(dbgs() << " Stub function found\n");
1041     } else {
1042       // Create a new stub function.
1043       DEBUG(dbgs() << " Create a new stub function\n");
1044
1045       uintptr_t BaseAddress = uintptr_t(Section.Address);
1046       uintptr_t StubAlignment = getStubAlignment();
1047       StubAddress = (BaseAddress + Section.StubOffset +
1048                      StubAlignment - 1) & -StubAlignment;
1049       unsigned StubOffset = StubAddress - BaseAddress;
1050
1051       Stubs[Value] = StubOffset;
1052       createStubFunction((uint8_t *)StubAddress);
1053       RelocationEntry RE(SectionID, StubOffset + 8,
1054                          ELF::R_390_64, Value.Addend - Addend);
1055       if (Value.SymbolName)
1056         addRelocationForSymbol(RE, Value.SymbolName);
1057       else
1058         addRelocationForSection(RE, Value.SectionID);
1059       Section.StubOffset = StubOffset + getMaxStubSize();
1060     }
1061
1062     if (RelType == ELF::R_390_GOTENT)
1063       resolveRelocation(Section, Offset, StubAddress + 8,
1064                         ELF::R_390_PC32DBL, Addend);
1065     else
1066       resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
1067   } else {
1068     RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1069     if (Value.SymbolName)
1070       addRelocationForSymbol(RE, Value.SymbolName);
1071     else
1072       addRelocationForSection(RE, Value.SectionID);
1073   }
1074 }
1075
1076 bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1077   if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1078     return false;
1079   return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
1080 }
1081 } // namespace llvm