Fix coding style violations in 162135 and 162136.
[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 "llvm/ADT/OwningPtr.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/IntervalMap.h"
19 #include "RuntimeDyldELF.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/ELF.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/Object/ELF.h"
24 #include "JITRegistrar.h"
25 using namespace llvm;
26 using namespace llvm::object;
27
28 namespace {
29
30 template<support::endianness target_endianness, bool is64Bits>
31 class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> {
32   LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
33
34   typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
35   typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
36   typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
37   typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
38
39   typedef typename ELFObjectFile<target_endianness, is64Bits>::
40     Elf_Ehdr Elf_Ehdr;
41
42   typedef typename ELFDataTypeTypedefHelper<
43           target_endianness, is64Bits>::value_type addr_type;
44
45 protected:
46   // This duplicates the 'Data' member in the 'Binary' base class
47   // but it is necessary to workaround a bug in gcc 4.2
48   MemoryBuffer *InputData;
49
50 public:
51   DyldELFObject(MemoryBuffer *Object, error_code &ec);
52
53   void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
54   void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
55
56   const MemoryBuffer& getBuffer() const { return *InputData; }
57
58   // Methods for type inquiry through isa, cast and dyn_cast
59   static inline bool classof(const Binary *v) {
60     return (isa<ELFObjectFile<target_endianness, is64Bits> >(v)
61             && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v)));
62   }
63   static inline bool classof(
64       const ELFObjectFile<target_endianness, is64Bits> *v) {
65     return v->isDyldType();
66   }
67   static inline bool classof(const DyldELFObject *v) {
68     return true;
69   }
70 };
71
72 template<support::endianness target_endianness, bool is64Bits>
73 class ELFObjectImage : public ObjectImage {
74   protected:
75     DyldELFObject<target_endianness, is64Bits> *DyldObj;
76     bool Registered;
77
78   public:
79     ELFObjectImage(DyldELFObject<target_endianness, is64Bits> *Obj)
80     : ObjectImage(Obj),
81       DyldObj(Obj),
82       Registered(false) {}
83
84     virtual ~ELFObjectImage() {
85       if (Registered)
86         deregisterWithDebugger();
87     }
88
89     // Subclasses can override these methods to update the image with loaded
90     // addresses for sections and common symbols
91     virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
92     {
93       DyldObj->updateSectionAddress(Sec, Addr);
94     }
95
96     virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
97     {
98       DyldObj->updateSymbolAddress(Sym, Addr);
99     }
100
101     virtual void registerWithDebugger()
102     {
103       JITRegistrar::getGDBRegistrar().registerObject(DyldObj->getBuffer());
104       Registered = true;
105     }
106     virtual void deregisterWithDebugger()
107     {
108       JITRegistrar::getGDBRegistrar().deregisterObject(DyldObj->getBuffer());
109     }
110 };
111
112 template<support::endianness target_endianness, bool is64Bits>
113 DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Object,
114                                                           error_code &ec)
115   : ELFObjectFile<target_endianness, is64Bits>(Object, ec),
116     InputData(Object) {
117   this->isDyldELFObject = true;
118 }
119
120 template<support::endianness target_endianness, bool is64Bits>
121 void DyldELFObject<target_endianness, is64Bits>::updateSectionAddress(
122                                                        const SectionRef &Sec,
123                                                        uint64_t Addr) {
124   DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
125   Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
126                           reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
127
128   // This assumes the address passed in matches the target address bitness
129   // The template-based type cast handles everything else.
130   shdr->sh_addr = static_cast<addr_type>(Addr);
131 }
132
133 template<support::endianness target_endianness, bool is64Bits>
134 void DyldELFObject<target_endianness, is64Bits>::updateSymbolAddress(
135                                                        const SymbolRef &SymRef,
136                                                        uint64_t Addr) {
137
138   Elf_Sym *sym = const_cast<Elf_Sym*>(
139                                  ELFObjectFile<target_endianness, is64Bits>::
140                                    getSymbol(SymRef.getRawDataRefImpl()));
141
142   // This assumes the address passed in matches the target address bitness
143   // The template-based type cast handles everything else.
144   sym->st_value = static_cast<addr_type>(Addr);
145 }
146
147 } // namespace
148
149
150 namespace llvm {
151
152 ObjectImage *RuntimeDyldELF::createObjectImage(
153                                          const MemoryBuffer *ConstInputBuffer) {
154   MemoryBuffer *InputBuffer = const_cast<MemoryBuffer*>(ConstInputBuffer);
155   std::pair<unsigned char, unsigned char> Ident = getElfArchType(InputBuffer);
156   error_code ec;
157
158   if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
159     DyldELFObject<support::little, false> *Obj =
160            new DyldELFObject<support::little, false>(InputBuffer, ec);
161     return new ELFObjectImage<support::little, false>(Obj);
162   }
163   else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
164     DyldELFObject<support::big, false> *Obj =
165            new DyldELFObject<support::big, false>(InputBuffer, ec);
166     return new ELFObjectImage<support::big, false>(Obj);
167   }
168   else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
169     DyldELFObject<support::big, true> *Obj =
170            new DyldELFObject<support::big, true>(InputBuffer, ec);
171     return new ELFObjectImage<support::big, true>(Obj);
172   }
173   else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
174     DyldELFObject<support::little, true> *Obj =
175            new DyldELFObject<support::little, true>(InputBuffer, ec);
176     return new ELFObjectImage<support::little, true>(Obj);
177   }
178   else
179     llvm_unreachable("Unexpected ELF format");
180 }
181
182 void RuntimeDyldELF::handleObjectLoaded(ObjectImage *Obj)
183 {
184   Obj->registerWithDebugger();
185   // Save the loaded object.  It will deregister itself when deleted
186   LoadedObject = Obj;
187 }
188
189 RuntimeDyldELF::~RuntimeDyldELF() {
190   if (LoadedObject)
191     delete LoadedObject;
192 }
193
194 void RuntimeDyldELF::resolveX86_64Relocation(uint8_t *LocalAddress,
195                                              uint64_t FinalAddress,
196                                              uint64_t Value,
197                                              uint32_t Type,
198                                              int64_t Addend) {
199   switch (Type) {
200   default:
201     llvm_unreachable("Relocation type not implemented yet!");
202   break;
203   case ELF::R_X86_64_64: {
204     uint64_t *Target = (uint64_t*)(LocalAddress);
205     *Target = Value + Addend;
206     break;
207   }
208   case ELF::R_X86_64_32:
209   case ELF::R_X86_64_32S: {
210     Value += Addend;
211     assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
212            (Type == ELF::R_X86_64_32S && 
213              ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
214     uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
215     uint32_t *Target = reinterpret_cast<uint32_t*>(LocalAddress);
216     *Target = TruncatedAddr;
217     break;
218   }
219   case ELF::R_X86_64_PC32: {
220     uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
221     int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
222     assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
223     int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
224     *Placeholder = TruncOffset;
225     break;
226   }
227   }
228 }
229
230 void RuntimeDyldELF::resolveX86Relocation(uint8_t *LocalAddress,
231                                           uint32_t FinalAddress,
232                                           uint32_t Value,
233                                           uint32_t Type,
234                                           int32_t Addend) {
235   switch (Type) {
236   case ELF::R_386_32: {
237     uint32_t *Target = (uint32_t*)(LocalAddress);
238     uint32_t Placeholder = *Target;
239     *Target = Placeholder + Value + Addend;
240     break;
241   }
242   case ELF::R_386_PC32: {
243     uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
244     uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
245     *Placeholder = RealOffset;
246     break;
247     }
248     default:
249       // There are other relocation types, but it appears these are the
250       // only ones currently used by the LLVM ELF object writer
251       llvm_unreachable("Relocation type not implemented yet!");
252       break;
253   }
254 }
255
256 void RuntimeDyldELF::resolveARMRelocation(uint8_t *LocalAddress,
257                                           uint32_t FinalAddress,
258                                           uint32_t Value,
259                                           uint32_t Type,
260                                           int32_t Addend) {
261   // TODO: Add Thumb relocations.
262   uint32_t* TargetPtr = (uint32_t*)LocalAddress;
263   Value += Addend;
264
265   DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " << LocalAddress
266                << " FinalAddress: " << format("%p",FinalAddress)
267                << " Value: " << format("%x",Value)
268                << " Type: " << format("%x",Type)
269                << " Addend: " << format("%x",Addend)
270                << "\n");
271
272   switch(Type) {
273   default:
274     llvm_unreachable("Not implemented relocation type!");
275
276   // Just write 32bit value to relocation address
277   case ELF::R_ARM_ABS32 :
278     *TargetPtr = Value;
279     break;
280
281   // Write first 16 bit of 32 bit value to the mov instruction.
282   // Last 4 bit should be shifted.
283   case ELF::R_ARM_MOVW_ABS_NC :
284     Value = Value & 0xFFFF;
285     *TargetPtr |= Value & 0xFFF;
286     *TargetPtr |= ((Value >> 12) & 0xF) << 16;
287     break;
288
289   // Write last 16 bit of 32 bit value to the mov instruction.
290   // Last 4 bit should be shifted.
291   case ELF::R_ARM_MOVT_ABS :
292     Value = (Value >> 16) & 0xFFFF;
293     *TargetPtr |= Value & 0xFFF;
294     *TargetPtr |= ((Value >> 12) & 0xF) << 16;
295     break;
296
297   // Write 24 bit relative value to the branch instruction.
298   case ELF::R_ARM_PC24 :    // Fall through.
299   case ELF::R_ARM_CALL :    // Fall through.
300   case ELF::R_ARM_JUMP24 :
301     int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
302     RelValue = (RelValue & 0x03FFFFFC) >> 2;
303     *TargetPtr &= 0xFF000000;
304     *TargetPtr |= RelValue;
305     break;
306   }
307 }
308
309 void RuntimeDyldELF::resolveMIPSRelocation(uint8_t *LocalAddress,
310                                            uint32_t FinalAddress,
311                                            uint32_t Value,
312                                            uint32_t Type,
313                                            int32_t Addend) {
314   uint32_t* TargetPtr = (uint32_t*)LocalAddress;
315   Value += Addend;
316
317   DEBUG(dbgs() << "resolveMipselocation, LocalAddress: " << LocalAddress
318                << " FinalAddress: " << format("%p",FinalAddress)
319                << " Value: " << format("%x",Value)
320                << " Type: " << format("%x",Type)
321                << " Addend: " << format("%x",Addend)
322                << "\n");
323
324   switch(Type) {
325   default:
326     llvm_unreachable("Not implemented relocation type!");
327     break;
328   case ELF::R_MIPS_32:
329     *TargetPtr = Value + (*TargetPtr);
330     break;
331   case ELF::R_MIPS_26:
332     *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
333     break;
334   case ELF::R_MIPS_HI16:
335     // Get the higher 16-bits. Also add 1 if bit 15 is 1.
336     Value += ((*TargetPtr) & 0x0000ffff) << 16;
337     *TargetPtr = ((*TargetPtr) & 0xffff0000) |
338                  (((Value + 0x8000) >> 16) & 0xffff);
339     break;
340    case ELF::R_MIPS_LO16:
341     Value += ((*TargetPtr) & 0x0000ffff);
342     *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
343     break;
344    }
345 }
346
347 void RuntimeDyldELF::resolveRelocation(uint8_t *LocalAddress,
348                                        uint64_t FinalAddress,
349                                        uint64_t Value,
350                                        uint32_t Type,
351                                        int64_t Addend) {
352   switch (Arch) {
353   case Triple::x86_64:
354     resolveX86_64Relocation(LocalAddress, FinalAddress, Value, Type, Addend);
355     break;
356   case Triple::x86:
357     resolveX86Relocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
358                          (uint32_t)(Value & 0xffffffffL), Type,
359                          (uint32_t)(Addend & 0xffffffffL));
360     break;
361   case Triple::arm:    // Fall through.
362   case Triple::thumb:
363     resolveARMRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
364                          (uint32_t)(Value & 0xffffffffL), Type,
365                          (uint32_t)(Addend & 0xffffffffL));
366     break;
367   case Triple::mips:    // Fall through.
368   case Triple::mipsel:
369     resolveMIPSRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
370                           (uint32_t)(Value & 0xffffffffL), Type,
371                           (uint32_t)(Addend & 0xffffffffL));
372     break;
373   default: llvm_unreachable("Unsupported CPU type!");
374   }
375 }
376
377 void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel,
378                                           ObjectImage &Obj,
379                                           ObjSectionToIDMap &ObjSectionToID,
380                                           const SymbolTableMap &Symbols,
381                                           StubMap &Stubs) {
382
383   uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL);
384   intptr_t Addend = (intptr_t)Rel.AdditionalInfo;
385   const SymbolRef &Symbol = Rel.Symbol;
386
387   // Obtain the symbol name which is referenced in the relocation
388   StringRef TargetName;
389   Symbol.getName(TargetName);
390   DEBUG(dbgs() << "\t\tRelType: " << RelType
391                << " Addend: " << Addend
392                << " TargetName: " << TargetName
393                << "\n");
394   RelocationValueRef Value;
395   // First search for the symbol in the local symbol table
396   SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
397   if (lsi != Symbols.end()) {
398     Value.SectionID = lsi->second.first;
399     Value.Addend = lsi->second.second;
400   } else {
401     // Search for the symbol in the global symbol table
402     SymbolTableMap::const_iterator gsi =
403         GlobalSymbolTable.find(TargetName.data());
404     if (gsi != GlobalSymbolTable.end()) {
405       Value.SectionID = gsi->second.first;
406       Value.Addend = gsi->second.second;
407     } else {
408       SymbolRef::Type SymType;
409       Symbol.getType(SymType);
410       switch (SymType) {
411         case SymbolRef::ST_Debug: {
412           // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
413           // and can be changed by another developers. Maybe best way is add
414           // a new symbol type ST_Section to SymbolRef and use it.
415           section_iterator si(Obj.end_sections());
416           Symbol.getSection(si);
417           if (si == Obj.end_sections())
418             llvm_unreachable("Symbol section not found, bad object file format!");
419           DEBUG(dbgs() << "\t\tThis is section symbol\n");
420           Value.SectionID = findOrEmitSection(Obj, (*si), true, ObjSectionToID);
421           Value.Addend = Addend;
422           break;
423         }
424         case SymbolRef::ST_Unknown: {
425           Value.SymbolName = TargetName.data();
426           Value.Addend = Addend;
427           break;
428         }
429         default:
430           llvm_unreachable("Unresolved symbol type!");
431           break;
432       }
433     }
434   }
435   DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID
436                << " Rel.Offset: " << Rel.Offset
437                << "\n");
438   if (Arch == Triple::arm &&
439       (RelType == ELF::R_ARM_PC24 ||
440        RelType == ELF::R_ARM_CALL ||
441        RelType == ELF::R_ARM_JUMP24)) {
442     // This is an ARM branch relocation, need to use a stub function.
443     DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
444     SectionEntry &Section = Sections[Rel.SectionID];
445     uint8_t *Target = Section.Address + Rel.Offset;
446
447     //  Look up for existing stub.
448     StubMap::const_iterator i = Stubs.find(Value);
449     if (i != Stubs.end()) {
450       resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
451                         i->second, RelType, 0);
452       DEBUG(dbgs() << " Stub function found\n");
453     } else {
454       // Create a new stub function.
455       DEBUG(dbgs() << " Create a new stub function\n");
456       Stubs[Value] = Section.StubOffset;
457       uint8_t *StubTargetAddr = createStubFunction(Section.Address +
458                                                    Section.StubOffset);
459       RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
460                          ELF::R_ARM_ABS32, Value.Addend);
461       if (Value.SymbolName)
462         addRelocationForSymbol(RE, Value.SymbolName);
463       else
464         addRelocationForSection(RE, Value.SectionID);
465
466       resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
467                         Section.StubOffset, RelType, 0);
468       Section.StubOffset += getMaxStubSize();
469     }
470   } else if (Arch == Triple::mipsel && RelType == ELF::R_MIPS_26) {
471     // This is an Mips branch relocation, need to use a stub function.
472     DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
473     SectionEntry &Section = Sections[Rel.SectionID];
474     uint8_t *Target = Section.Address + Rel.Offset;
475     uint32_t *TargetAddress = (uint32_t *)Target;
476
477     // Extract the addend from the instruction.
478     uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
479
480     Value.Addend += Addend;
481
482     //  Look up for existing stub.
483     StubMap::const_iterator i = Stubs.find(Value);
484     if (i != Stubs.end()) {
485       resolveRelocation(Target, (uint64_t)Target,
486                         (uint64_t)Section.Address +
487                         i->second, RelType, 0);
488       DEBUG(dbgs() << " Stub function found\n");
489     } else {
490       // Create a new stub function.
491       DEBUG(dbgs() << " Create a new stub function\n");
492       Stubs[Value] = Section.StubOffset;
493       uint8_t *StubTargetAddr = createStubFunction(Section.Address +
494                                                    Section.StubOffset);
495
496       // Creating Hi and Lo relocations for the filled stub instructions.
497       RelocationEntry REHi(Rel.SectionID,
498                            StubTargetAddr - Section.Address,
499                            ELF::R_MIPS_HI16, Value.Addend);
500       RelocationEntry RELo(Rel.SectionID,
501                            StubTargetAddr - Section.Address + 4,
502                            ELF::R_MIPS_LO16, Value.Addend);
503
504       if (Value.SymbolName) {
505         addRelocationForSymbol(REHi, Value.SymbolName);
506         addRelocationForSymbol(RELo, Value.SymbolName);
507       } else {
508         addRelocationForSection(REHi, Value.SectionID);
509         addRelocationForSection(RELo, Value.SectionID);
510       }
511
512       resolveRelocation(Target, (uint64_t)Target,
513                         (uint64_t)Section.Address +
514                         Section.StubOffset, RelType, 0);
515       Section.StubOffset += getMaxStubSize();
516     }
517   } else {
518     RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
519     if (Value.SymbolName)
520       addRelocationForSymbol(RE, Value.SymbolName);
521     else
522       addRelocationForSection(RE, Value.SectionID);
523   }
524 }
525
526 bool RuntimeDyldELF::isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
527   StringRef Magic = InputBuffer->getBuffer().slice(0, ELF::EI_NIDENT);
528   return (memcmp(Magic.data(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
529 }
530 } // namespace llvm