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