blob: efefacf632d3ec6b50238f2efdae0e81d183ee57 [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 }
62 static inline bool classof(const DyldELFObject *v) {
63 return true;
64 }
65};
66
67template<support::endianness target_endianness, bool is64Bits>
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000068class ELFObjectImage : public ObjectImageCommon {
Preston Gurd689ff9c2012-04-16 22:12:58 +000069 protected:
70 DyldELFObject<target_endianness, is64Bits> *DyldObj;
71 bool Registered;
72
73 public:
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000074 ELFObjectImage(ObjectBuffer *Input,
75 DyldELFObject<target_endianness, is64Bits> *Obj)
76 : ObjectImageCommon(Input, Obj),
Preston Gurd689ff9c2012-04-16 22:12:58 +000077 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 {
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000099 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000100 Registered = true;
101 }
102 virtual void deregisterWithDebugger()
103 {
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000104 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000105 }
106};
107
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000108// 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.
Preston Gurd689ff9c2012-04-16 22:12:58 +0000111template<support::endianness target_endianness, bool is64Bits>
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000112DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Wrapper,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000113 error_code &ec)
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000114 : ELFObjectFile<target_endianness, is64Bits>(Wrapper, ec) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000115 this->isDyldELFObject = true;
116}
117
118template<support::endianness target_endianness, bool is64Bits>
119void 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
131template<support::endianness target_endianness, bool is64Bits>
132void 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
Eli Benderskya66a1852012-01-16 08:56:09 +0000148namespace llvm {
149
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000150ObjectImage *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]);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000156 error_code ec;
157
158 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
159 DyldELFObject<support::little, false> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000160 new DyldELFObject<support::little, false>(Buffer->getMemBuffer(), ec);
161 return new ELFObjectImage<support::little, false>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000162 }
163 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
164 DyldELFObject<support::big, false> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000165 new DyldELFObject<support::big, false>(Buffer->getMemBuffer(), ec);
166 return new ELFObjectImage<support::big, false>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000167 }
168 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
169 DyldELFObject<support::big, true> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000170 new DyldELFObject<support::big, true>(Buffer->getMemBuffer(), ec);
171 return new ELFObjectImage<support::big, true>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000172 }
173 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
174 DyldELFObject<support::little, true> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000175 new DyldELFObject<support::little, true>(Buffer->getMemBuffer(), ec);
176 return new ELFObjectImage<support::little, true>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000177 }
178 else
179 llvm_unreachable("Unexpected ELF format");
180}
181
Preston Gurd689ff9c2012-04-16 22:12:58 +0000182RuntimeDyldELF::~RuntimeDyldELF() {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000183}
Eli Benderskya66a1852012-01-16 08:56:09 +0000184
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000185void 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;
Eli Benderskya66a1852012-01-16 08:56:09 +0000194 case ELF::R_X86_64_64: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000195 uint64_t *Target = (uint64_t*)(LocalAddress);
196 *Target = Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000197 break;
198 }
199 case ELF::R_X86_64_32:
200 case ELF::R_X86_64_32S: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000201 Value += Addend;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000202 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)));
Eli Benderskya66a1852012-01-16 08:56:09 +0000205 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000206 uint32_t *Target = reinterpret_cast<uint32_t*>(LocalAddress);
Eli Benderskya66a1852012-01-16 08:56:09 +0000207 *Target = TruncatedAddr;
208 break;
209 }
210 case ELF::R_X86_64_PC32: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000211 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
212 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000213 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000214 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Eli Benderskya66a1852012-01-16 08:56:09 +0000215 *Placeholder = TruncOffset;
216 break;
217 }
218 }
219}
220
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000221void RuntimeDyldELF::resolveX86Relocation(uint8_t *LocalAddress,
222 uint32_t FinalAddress,
223 uint32_t Value,
224 uint32_t Type,
225 int32_t Addend) {
226 switch (Type) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000227 case ELF::R_386_32: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000228 uint32_t *Target = (uint32_t*)(LocalAddress);
Preston Gurdc68dda82012-04-12 20:13:57 +0000229 uint32_t Placeholder = *Target;
230 *Target = Placeholder + Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000231 break;
232 }
233 case ELF::R_386_PC32: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000234 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress);
235 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Eli Benderskya66a1852012-01-16 08:56:09 +0000236 *Placeholder = RealOffset;
237 break;
238 }
239 default:
240 // There are other relocation types, but it appears these are the
Andrew Kaylore2e73bd2012-07-27 18:39:47 +0000241 // only ones currently used by the LLVM ELF object writer
Craig Topper85814382012-02-07 05:05:23 +0000242 llvm_unreachable("Relocation type not implemented yet!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000243 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000244 }
245}
246
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000247void 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
Tim Northover565ebde2012-10-03 16:29:42 +0000267 // Write a 32bit value to relocation address, taking into account the
268 // implicit addend encoded in the target.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000269 case ELF::R_ARM_ABS32 :
Tim Northover565ebde2012-10-03 16:29:42 +0000270 *TargetPtr += Value;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000271 break;
272
273 // Write first 16 bit of 32 bit value to the mov instruction.
274 // Last 4 bit should be shifted.
275 case ELF::R_ARM_MOVW_ABS_NC :
Tim Northover565ebde2012-10-03 16:29:42 +0000276 // We are not expecting any other addend in the relocation address.
277 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
278 // non-contiguous fields.
279 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000280 Value = Value & 0xFFFF;
281 *TargetPtr |= Value & 0xFFF;
282 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
283 break;
284
285 // Write last 16 bit of 32 bit value to the mov instruction.
286 // Last 4 bit should be shifted.
287 case ELF::R_ARM_MOVT_ABS :
Tim Northover565ebde2012-10-03 16:29:42 +0000288 // We are not expecting any other addend in the relocation address.
289 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
290 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000291 Value = (Value >> 16) & 0xFFFF;
292 *TargetPtr |= Value & 0xFFF;
293 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
294 break;
295
296 // Write 24 bit relative value to the branch instruction.
297 case ELF::R_ARM_PC24 : // Fall through.
298 case ELF::R_ARM_CALL : // Fall through.
299 case ELF::R_ARM_JUMP24 :
300 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
301 RelValue = (RelValue & 0x03FFFFFC) >> 2;
302 *TargetPtr &= 0xFF000000;
303 *TargetPtr |= RelValue;
304 break;
305 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000306}
307
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000308void RuntimeDyldELF::resolveMIPSRelocation(uint8_t *LocalAddress,
Akira Hatanakab862f092012-08-20 17:53:24 +0000309 uint32_t FinalAddress,
310 uint32_t Value,
311 uint32_t Type,
312 int32_t Addend) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000313 uint32_t* TargetPtr = (uint32_t*)LocalAddress;
314 Value += Addend;
315
316 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: " << LocalAddress
317 << " FinalAddress: " << format("%p",FinalAddress)
318 << " Value: " << format("%x",Value)
319 << " Type: " << format("%x",Type)
320 << " Addend: " << format("%x",Addend)
321 << "\n");
322
323 switch(Type) {
324 default:
325 llvm_unreachable("Not implemented relocation type!");
326 break;
327 case ELF::R_MIPS_32:
328 *TargetPtr = Value + (*TargetPtr);
329 break;
330 case ELF::R_MIPS_26:
331 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
332 break;
333 case ELF::R_MIPS_HI16:
334 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
335 Value += ((*TargetPtr) & 0x0000ffff) << 16;
336 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
337 (((Value + 0x8000) >> 16) & 0xffff);
338 break;
339 case ELF::R_MIPS_LO16:
340 Value += ((*TargetPtr) & 0x0000ffff);
341 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
342 break;
343 }
344}
345
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000346void RuntimeDyldELF::resolveRelocation(uint8_t *LocalAddress,
347 uint64_t FinalAddress,
348 uint64_t Value,
349 uint32_t Type,
350 int64_t Addend) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000351 switch (Arch) {
352 case Triple::x86_64:
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000353 resolveX86_64Relocation(LocalAddress, FinalAddress, Value, Type, Addend);
Eli Benderskya66a1852012-01-16 08:56:09 +0000354 break;
355 case Triple::x86:
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000356 resolveX86Relocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
357 (uint32_t)(Value & 0xffffffffL), Type,
358 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000359 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000360 case Triple::arm: // Fall through.
361 case Triple::thumb:
362 resolveARMRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
363 (uint32_t)(Value & 0xffffffffL), Type,
364 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000365 break;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000366 case Triple::mips: // Fall through.
367 case Triple::mipsel:
368 resolveMIPSRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL),
Akira Hatanakab862f092012-08-20 17:53:24 +0000369 (uint32_t)(Value & 0xffffffffL), Type,
370 (uint32_t)(Addend & 0xffffffffL));
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000371 break;
Craig Topper85814382012-02-07 05:05:23 +0000372 default: llvm_unreachable("Unsupported CPU type!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000373 }
374}
375
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000376void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000377 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000378 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000379 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000380 StubMap &Stubs) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000381
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000382 uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL);
383 intptr_t Addend = (intptr_t)Rel.AdditionalInfo;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000384 const SymbolRef &Symbol = Rel.Symbol;
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000385
386 // Obtain the symbol name which is referenced in the relocation
387 StringRef TargetName;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000388 Symbol.getName(TargetName);
389 DEBUG(dbgs() << "\t\tRelType: " << RelType
390 << " Addend: " << Addend
391 << " TargetName: " << TargetName
392 << "\n");
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000393 RelocationValueRef Value;
394 // First search for the symbol in the local symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000395 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000396 if (lsi != Symbols.end()) {
397 Value.SectionID = lsi->second.first;
398 Value.Addend = lsi->second.second;
399 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000400 // Search for the symbol in the global symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000401 SymbolTableMap::const_iterator gsi =
402 GlobalSymbolTable.find(TargetName.data());
403 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000404 Value.SectionID = gsi->second.first;
405 Value.Addend = gsi->second.second;
406 } else {
407 SymbolRef::Type SymType;
408 Symbol.getType(SymType);
409 switch (SymType) {
410 case SymbolRef::ST_Debug: {
411 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
412 // and can be changed by another developers. Maybe best way is add
413 // a new symbol type ST_Section to SymbolRef and use it.
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000414 section_iterator si(Obj.end_sections());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000415 Symbol.getSection(si);
416 if (si == Obj.end_sections())
417 llvm_unreachable("Symbol section not found, bad object file format!");
418 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Preston Gurd689ff9c2012-04-16 22:12:58 +0000419 Value.SectionID = findOrEmitSection(Obj, (*si), true, ObjSectionToID);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000420 Value.Addend = Addend;
421 break;
422 }
423 case SymbolRef::ST_Unknown: {
424 Value.SymbolName = TargetName.data();
425 Value.Addend = Addend;
426 break;
427 }
428 default:
429 llvm_unreachable("Unresolved symbol type!");
430 break;
431 }
432 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000433 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000434 DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID
435 << " Rel.Offset: " << Rel.Offset
436 << "\n");
437 if (Arch == Triple::arm &&
438 (RelType == ELF::R_ARM_PC24 ||
439 RelType == ELF::R_ARM_CALL ||
440 RelType == ELF::R_ARM_JUMP24)) {
441 // This is an ARM branch relocation, need to use a stub function.
442 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
443 SectionEntry &Section = Sections[Rel.SectionID];
444 uint8_t *Target = Section.Address + Rel.Offset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000445
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000446 // Look up for existing stub.
447 StubMap::const_iterator i = Stubs.find(Value);
448 if (i != Stubs.end()) {
Danil Malyshevab427332012-04-17 20:10:16 +0000449 resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000450 i->second, RelType, 0);
451 DEBUG(dbgs() << " Stub function found\n");
452 } else {
453 // Create a new stub function.
454 DEBUG(dbgs() << " Create a new stub function\n");
455 Stubs[Value] = Section.StubOffset;
456 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
457 Section.StubOffset);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000458 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
459 ELF::R_ARM_ABS32, Value.Addend);
460 if (Value.SymbolName)
461 addRelocationForSymbol(RE, Value.SymbolName);
462 else
463 addRelocationForSection(RE, Value.SectionID);
464
Danil Malyshevab427332012-04-17 20:10:16 +0000465 resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address +
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000466 Section.StubOffset, RelType, 0);
467 Section.StubOffset += getMaxStubSize();
468 }
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000469 } else if (Arch == Triple::mipsel && RelType == ELF::R_MIPS_26) {
470 // This is an Mips branch relocation, need to use a stub function.
471 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
472 SectionEntry &Section = Sections[Rel.SectionID];
473 uint8_t *Target = Section.Address + Rel.Offset;
474 uint32_t *TargetAddress = (uint32_t *)Target;
475
476 // Extract the addend from the instruction.
477 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
478
479 Value.Addend += Addend;
480
481 // Look up for existing stub.
482 StubMap::const_iterator i = Stubs.find(Value);
483 if (i != Stubs.end()) {
484 resolveRelocation(Target, (uint64_t)Target,
485 (uint64_t)Section.Address +
486 i->second, RelType, 0);
487 DEBUG(dbgs() << " Stub function found\n");
488 } else {
489 // Create a new stub function.
490 DEBUG(dbgs() << " Create a new stub function\n");
491 Stubs[Value] = Section.StubOffset;
492 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
493 Section.StubOffset);
494
495 // Creating Hi and Lo relocations for the filled stub instructions.
496 RelocationEntry REHi(Rel.SectionID,
497 StubTargetAddr - Section.Address,
498 ELF::R_MIPS_HI16, Value.Addend);
499 RelocationEntry RELo(Rel.SectionID,
500 StubTargetAddr - Section.Address + 4,
501 ELF::R_MIPS_LO16, Value.Addend);
502
503 if (Value.SymbolName) {
504 addRelocationForSymbol(REHi, Value.SymbolName);
505 addRelocationForSymbol(RELo, Value.SymbolName);
506 } else {
507 addRelocationForSection(REHi, Value.SectionID);
508 addRelocationForSection(RELo, Value.SectionID);
509 }
510
511 resolveRelocation(Target, (uint64_t)Target,
512 (uint64_t)Section.Address +
513 Section.StubOffset, RelType, 0);
514 Section.StubOffset += getMaxStubSize();
515 }
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000516 } else {
517 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
518 if (Value.SymbolName)
519 addRelocationForSymbol(RE, Value.SymbolName);
520 else
521 addRelocationForSection(RE, Value.SectionID);
522 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000523}
524
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000525bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
526 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
527 return false;
528 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
Eli Benderskya66a1852012-01-16 08:56:09 +0000529}
530} // namespace llvm