blob: 414957c356e48528dae3d7638097d5e11b9812fc [file] [log] [blame]
Jim Grosbache0934be2012-01-16 23:50:58 +00001//===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
Eli Benderskya66a1852012-01-16 08:56:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implementation of ELF support for the MC-JIT runtime dynamic linker.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dyld"
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000015#include "RuntimeDyldELF.h"
16#include "JITRegistrar.h"
17#include "ObjectImageCommon.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000018#include "llvm/ADT/OwningPtr.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/IntervalMap.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000022#include "llvm/Object/ObjectFile.h"
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000023#include "llvm/ExecutionEngine/ObjectImage.h"
24#include "llvm/ExecutionEngine/ObjectBuffer.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000025#include "llvm/Support/ELF.h"
26#include "llvm/ADT/Triple.h"
Preston Gurd689ff9c2012-04-16 22:12:58 +000027#include "llvm/Object/ELF.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000028using namespace llvm;
29using namespace llvm::object;
30
Preston Gurd689ff9c2012-04-16 22:12:58 +000031namespace {
32
33template<support::endianness target_endianness, bool is64Bits>
34class 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
Michael J. Spencer2c38a662012-09-10 19:04:02 +000042 typedef Elf_Ehdr_Impl<target_endianness, is64Bits> Elf_Ehdr;
Preston Gurd689ff9c2012-04-16 22:12:58 +000043
44 typedef typename ELFDataTypeTypedefHelper<
45 target_endianness, is64Bits>::value_type addr_type;
46
Preston Gurd689ff9c2012-04-16 22:12:58 +000047public:
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000048 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
Preston Gurd689ff9c2012-04-16 22:12:58 +000049
50 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
51 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
52
Andrew Kaylor2e319872012-07-27 17:52:42 +000053 // Methods for type inquiry through isa, cast and dyn_cast
Preston Gurd689ff9c2012-04-16 22:12:58 +000054 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 }
Preston Gurd689ff9c2012-04-16 22:12:58 +000062};
63
64template<support::endianness target_endianness, bool is64Bits>
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000065class ELFObjectImage : public ObjectImageCommon {
Preston Gurd689ff9c2012-04-16 22:12:58 +000066 protected:
67 DyldELFObject<target_endianness, is64Bits> *DyldObj;
68 bool Registered;
69
70 public:
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000071 ELFObjectImage(ObjectBuffer *Input,
72 DyldELFObject<target_endianness, is64Bits> *Obj)
73 : ObjectImageCommon(Input, Obj),
Preston Gurd689ff9c2012-04-16 22:12:58 +000074 DyldObj(Obj),
75 Registered(false) {}
76
77 virtual ~ELFObjectImage() {
78 if (Registered)
79 deregisterWithDebugger();
80 }
81
82 // Subclasses can override these methods to update the image with loaded
83 // addresses for sections and common symbols
84 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
85 {
86 DyldObj->updateSectionAddress(Sec, Addr);
87 }
88
89 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
90 {
91 DyldObj->updateSymbolAddress(Sym, Addr);
92 }
93
94 virtual void registerWithDebugger()
95 {
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000096 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +000097 Registered = true;
98 }
99 virtual void deregisterWithDebugger()
100 {
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000101 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000102 }
103};
104
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000105// The MemoryBuffer passed into this constructor is just a wrapper around the
106// actual memory. Ultimately, the Binary parent class will take ownership of
107// this MemoryBuffer object but not the underlying memory.
Preston Gurd689ff9c2012-04-16 22:12:58 +0000108template<support::endianness target_endianness, bool is64Bits>
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000109DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Wrapper,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000110 error_code &ec)
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000111 : ELFObjectFile<target_endianness, is64Bits>(Wrapper, ec) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000112 this->isDyldELFObject = true;
113}
114
115template<support::endianness target_endianness, bool is64Bits>
116void DyldELFObject<target_endianness, is64Bits>::updateSectionAddress(
117 const SectionRef &Sec,
118 uint64_t Addr) {
119 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
120 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
121 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
122
123 // This assumes the address passed in matches the target address bitness
124 // The template-based type cast handles everything else.
125 shdr->sh_addr = static_cast<addr_type>(Addr);
126}
127
128template<support::endianness target_endianness, bool is64Bits>
129void DyldELFObject<target_endianness, is64Bits>::updateSymbolAddress(
130 const SymbolRef &SymRef,
131 uint64_t Addr) {
132
133 Elf_Sym *sym = const_cast<Elf_Sym*>(
134 ELFObjectFile<target_endianness, is64Bits>::
135 getSymbol(SymRef.getRawDataRefImpl()));
136
137 // This assumes the address passed in matches the target address bitness
138 // The template-based type cast handles everything else.
139 sym->st_value = static_cast<addr_type>(Addr);
140}
141
142} // namespace
143
144
Eli Benderskya66a1852012-01-16 08:56:09 +0000145namespace llvm {
146
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000147ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
148 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
149 llvm_unreachable("Unexpected ELF object size");
150 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
151 (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
152 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000153 error_code ec;
154
155 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
156 DyldELFObject<support::little, false> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000157 new DyldELFObject<support::little, false>(Buffer->getMemBuffer(), ec);
158 return new ELFObjectImage<support::little, false>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000159 }
160 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
161 DyldELFObject<support::big, false> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000162 new DyldELFObject<support::big, false>(Buffer->getMemBuffer(), ec);
163 return new ELFObjectImage<support::big, false>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000164 }
165 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
166 DyldELFObject<support::big, true> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000167 new DyldELFObject<support::big, true>(Buffer->getMemBuffer(), ec);
168 return new ELFObjectImage<support::big, true>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000169 }
170 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
171 DyldELFObject<support::little, true> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000172 new DyldELFObject<support::little, true>(Buffer->getMemBuffer(), ec);
173 return new ELFObjectImage<support::little, true>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000174 }
175 else
176 llvm_unreachable("Unexpected ELF format");
177}
178
Preston Gurd689ff9c2012-04-16 22:12:58 +0000179RuntimeDyldELF::~RuntimeDyldELF() {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000180}
Eli Benderskya66a1852012-01-16 08:56:09 +0000181
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000182void RuntimeDyldELF::resolveX86_64Relocation(uint8_t *LocalAddress,
183 uint64_t FinalAddress,
184 uint64_t Value,
185 uint32_t Type,
186 int64_t Addend) {
187 switch (Type) {
188 default:
189 llvm_unreachable("Relocation type not implemented yet!");
190 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000191 case ELF::R_X86_64_64: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000192 uint64_t *Target = (uint64_t*)(LocalAddress);
193 *Target = Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000194 break;
195 }
196 case ELF::R_X86_64_32:
197 case ELF::R_X86_64_32S: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000198 Value += Addend;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000199 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
200 (Type == ELF::R_X86_64_32S &&
201 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Benderskya66a1852012-01-16 08:56:09 +0000202 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000203 uint32_t *Target = reinterpret_cast<uint32_t*>(LocalAddress);
Eli Benderskya66a1852012-01-16 08:56:09 +0000204 *Target = TruncatedAddr;
205 break;
206 }
207 case ELF::R_X86_64_PC32: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000208 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
209 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000210 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000211 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Eli Benderskya66a1852012-01-16 08:56:09 +0000212 *Placeholder = TruncOffset;
213 break;
214 }
215 }
216}
217
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000218void RuntimeDyldELF::resolveX86Relocation(uint8_t *LocalAddress,
219 uint32_t FinalAddress,
220 uint32_t Value,
221 uint32_t Type,
222 int32_t Addend) {
223 switch (Type) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000224 case ELF::R_386_32: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000225 uint32_t *Target = (uint32_t*)(LocalAddress);
Preston Gurdc68dda82012-04-12 20:13:57 +0000226 uint32_t Placeholder = *Target;
227 *Target = Placeholder + Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000228 break;
229 }
230 case ELF::R_386_PC32: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000231 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
232 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Eli Benderskya66a1852012-01-16 08:56:09 +0000233 *Placeholder = RealOffset;
234 break;
235 }
236 default:
237 // There are other relocation types, but it appears these are the
Andrew Kaylore2e73bd2012-07-27 18:39:47 +0000238 // only ones currently used by the LLVM ELF object writer
Craig Topper85814382012-02-07 05:05:23 +0000239 llvm_unreachable("Relocation type not implemented yet!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000240 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000241 }
242}
243
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000244void RuntimeDyldELF::resolveARMRelocation(uint8_t *LocalAddress,
245 uint32_t FinalAddress,
246 uint32_t Value,
247 uint32_t Type,
248 int32_t Addend) {
249 // TODO: Add Thumb relocations.
250 uint32_t* TargetPtr = (uint32_t*)LocalAddress;
251 Value += Addend;
252
253 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " << LocalAddress
254 << " FinalAddress: " << format("%p",FinalAddress)
255 << " Value: " << format("%x",Value)
256 << " Type: " << format("%x",Type)
257 << " Addend: " << format("%x",Addend)
258 << "\n");
259
260 switch(Type) {
261 default:
262 llvm_unreachable("Not implemented relocation type!");
263
Tim Northover565ebde2012-10-03 16:29:42 +0000264 // Write a 32bit value to relocation address, taking into account the
265 // implicit addend encoded in the target.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000266 case ELF::R_ARM_ABS32 :
Tim Northover565ebde2012-10-03 16:29:42 +0000267 *TargetPtr += Value;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000268 break;
269
270 // Write first 16 bit of 32 bit value to the mov instruction.
271 // Last 4 bit should be shifted.
272 case ELF::R_ARM_MOVW_ABS_NC :
Tim Northover565ebde2012-10-03 16:29:42 +0000273 // We are not expecting any other addend in the relocation address.
274 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
275 // non-contiguous fields.
276 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000277 Value = Value & 0xFFFF;
278 *TargetPtr |= Value & 0xFFF;
279 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
280 break;
281
282 // Write last 16 bit of 32 bit value to the mov instruction.
283 // Last 4 bit should be shifted.
284 case ELF::R_ARM_MOVT_ABS :
Tim Northover565ebde2012-10-03 16:29:42 +0000285 // We are not expecting any other addend in the relocation address.
286 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
287 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000288 Value = (Value >> 16) & 0xFFFF;
289 *TargetPtr |= Value & 0xFFF;
290 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
291 break;
292
293 // Write 24 bit relative value to the branch instruction.
294 case ELF::R_ARM_PC24 : // Fall through.
295 case ELF::R_ARM_CALL : // Fall through.
296 case ELF::R_ARM_JUMP24 :
297 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
298 RelValue = (RelValue & 0x03FFFFFC) >> 2;
299 *TargetPtr &= 0xFF000000;
300 *TargetPtr |= RelValue;
301 break;
302 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000303}
304
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000305void RuntimeDyldELF::resolveMIPSRelocation(uint8_t *LocalAddress,
Akira Hatanakab862f092012-08-20 17:53:24 +0000306 uint32_t FinalAddress,
307 uint32_t Value,
308 uint32_t Type,
309 int32_t Addend) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000310 uint32_t* TargetPtr = (uint32_t*)LocalAddress;
311 Value += Addend;
312
313 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: " << LocalAddress
314 << " FinalAddress: " << format("%p",FinalAddress)
315 << " Value: " << format("%x",Value)
316 << " Type: " << format("%x",Type)
317 << " Addend: " << format("%x",Addend)
318 << "\n");
319
320 switch(Type) {
321 default:
322 llvm_unreachable("Not implemented relocation type!");
323 break;
324 case ELF::R_MIPS_32:
325 *TargetPtr = Value + (*TargetPtr);
326 break;
327 case ELF::R_MIPS_26:
328 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
329 break;
330 case ELF::R_MIPS_HI16:
331 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
332 Value += ((*TargetPtr) & 0x0000ffff) << 16;
333 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
334 (((Value + 0x8000) >> 16) & 0xffff);
335 break;
336 case ELF::R_MIPS_LO16:
337 Value += ((*TargetPtr) & 0x0000ffff);
338 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
339 break;
340 }
341}
342
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000343void RuntimeDyldELF::resolveRelocation(uint8_t *LocalAddress,
344 uint64_t FinalAddress,
345 uint64_t Value,
346 uint32_t Type,
347 int64_t Addend) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000348 switch (Arch) {
349 case Triple::x86_64:
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000350 resolveX86_64Relocation(LocalAddress, FinalAddress, Value, Type, Addend);
Eli Benderskya66a1852012-01-16 08:56:09 +0000351 break;
352 case Triple::x86:
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000353 resolveX86Relocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
354 (uint32_t)(Value & 0xffffffffL), Type,
355 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000356 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000357 case Triple::arm: // Fall through.
358 case Triple::thumb:
359 resolveARMRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
360 (uint32_t)(Value & 0xffffffffL), Type,
361 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000362 break;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000363 case Triple::mips: // Fall through.
364 case Triple::mipsel:
365 resolveMIPSRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
Akira Hatanakab862f092012-08-20 17:53:24 +0000366 (uint32_t)(Value & 0xffffffffL), Type,
367 (uint32_t)(Addend & 0xffffffffL));
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000368 break;
Craig Topper85814382012-02-07 05:05:23 +0000369 default: llvm_unreachable("Unsupported CPU type!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000370 }
371}
372
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000373void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000374 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000375 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000376 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000377 StubMap &Stubs) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000378
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000379 uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL);
380 intptr_t Addend = (intptr_t)Rel.AdditionalInfo;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000381 const SymbolRef &Symbol = Rel.Symbol;
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000382
383 // Obtain the symbol name which is referenced in the relocation
384 StringRef TargetName;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000385 Symbol.getName(TargetName);
386 DEBUG(dbgs() << "\t\tRelType: " << RelType
387 << " Addend: " << Addend
388 << " TargetName: " << TargetName
389 << "\n");
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000390 RelocationValueRef Value;
391 // First search for the symbol in the local symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000392 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000393 if (lsi != Symbols.end()) {
394 Value.SectionID = lsi->second.first;
395 Value.Addend = lsi->second.second;
396 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000397 // Search for the symbol in the global symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000398 SymbolTableMap::const_iterator gsi =
399 GlobalSymbolTable.find(TargetName.data());
400 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000401 Value.SectionID = gsi->second.first;
402 Value.Addend = gsi->second.second;
403 } else {
404 SymbolRef::Type SymType;
405 Symbol.getType(SymType);
406 switch (SymType) {
407 case SymbolRef::ST_Debug: {
408 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
409 // and can be changed by another developers. Maybe best way is add
410 // a new symbol type ST_Section to SymbolRef and use it.
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000411 section_iterator si(Obj.end_sections());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000412 Symbol.getSection(si);
413 if (si == Obj.end_sections())
414 llvm_unreachable("Symbol section not found, bad object file format!");
415 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Preston Gurd689ff9c2012-04-16 22:12:58 +0000416 Value.SectionID = findOrEmitSection(Obj, (*si), true, ObjSectionToID);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000417 Value.Addend = Addend;
418 break;
419 }
420 case SymbolRef::ST_Unknown: {
421 Value.SymbolName = TargetName.data();
422 Value.Addend = Addend;
423 break;
424 }
425 default:
426 llvm_unreachable("Unresolved symbol type!");
427 break;
428 }
429 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000430 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000431 DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID
432 << " Rel.Offset: " << Rel.Offset
433 << "\n");
434 if (Arch == Triple::arm &&
435 (RelType == ELF::R_ARM_PC24 ||
436 RelType == ELF::R_ARM_CALL ||
437 RelType == ELF::R_ARM_JUMP24)) {
438 // This is an ARM branch relocation, need to use a stub function.
439 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
440 SectionEntry &Section = Sections[Rel.SectionID];
441 uint8_t *Target = Section.Address + Rel.Offset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000442
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000443 // Look up for existing stub.
444 StubMap::const_iterator i = Stubs.find(Value);
445 if (i != Stubs.end()) {
Danil Malyshevab427332012-04-17 20:10:16 +0000446 resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000447 i->second, RelType, 0);
448 DEBUG(dbgs() << " Stub function found\n");
449 } else {
450 // Create a new stub function.
451 DEBUG(dbgs() << " Create a new stub function\n");
452 Stubs[Value] = Section.StubOffset;
453 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
454 Section.StubOffset);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000455 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
456 ELF::R_ARM_ABS32, Value.Addend);
457 if (Value.SymbolName)
458 addRelocationForSymbol(RE, Value.SymbolName);
459 else
460 addRelocationForSection(RE, Value.SectionID);
461
Danil Malyshevab427332012-04-17 20:10:16 +0000462 resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000463 Section.StubOffset, RelType, 0);
464 Section.StubOffset += getMaxStubSize();
465 }
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000466 } else if (Arch == Triple::mipsel && RelType == ELF::R_MIPS_26) {
467 // This is an Mips branch relocation, need to use a stub function.
468 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
469 SectionEntry &Section = Sections[Rel.SectionID];
470 uint8_t *Target = Section.Address + Rel.Offset;
471 uint32_t *TargetAddress = (uint32_t *)Target;
472
473 // Extract the addend from the instruction.
474 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
475
476 Value.Addend += Addend;
477
478 // Look up for existing stub.
479 StubMap::const_iterator i = Stubs.find(Value);
480 if (i != Stubs.end()) {
481 resolveRelocation(Target, (uint64_t)Target,
482 (uint64_t)Section.Address +
483 i->second, RelType, 0);
484 DEBUG(dbgs() << " Stub function found\n");
485 } else {
486 // Create a new stub function.
487 DEBUG(dbgs() << " Create a new stub function\n");
488 Stubs[Value] = Section.StubOffset;
489 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
490 Section.StubOffset);
491
492 // Creating Hi and Lo relocations for the filled stub instructions.
493 RelocationEntry REHi(Rel.SectionID,
494 StubTargetAddr - Section.Address,
495 ELF::R_MIPS_HI16, Value.Addend);
496 RelocationEntry RELo(Rel.SectionID,
497 StubTargetAddr - Section.Address + 4,
498 ELF::R_MIPS_LO16, Value.Addend);
499
500 if (Value.SymbolName) {
501 addRelocationForSymbol(REHi, Value.SymbolName);
502 addRelocationForSymbol(RELo, Value.SymbolName);
503 } else {
504 addRelocationForSection(REHi, Value.SectionID);
505 addRelocationForSection(RELo, Value.SectionID);
506 }
507
508 resolveRelocation(Target, (uint64_t)Target,
509 (uint64_t)Section.Address +
510 Section.StubOffset, RelType, 0);
511 Section.StubOffset += getMaxStubSize();
512 }
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000513 } else {
514 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
515 if (Value.SymbolName)
516 addRelocationForSymbol(RE, Value.SymbolName);
517 else
518 addRelocationForSection(RE, Value.SectionID);
519 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000520}
521
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000522bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
523 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
524 return false;
525 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
Eli Benderskya66a1852012-01-16 08:56:09 +0000526}
527} // namespace llvm