blob: 1ebcaf7ba822e536008960e602826cc8cb884024 [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
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +000033static inline
34error_code check(error_code Err) {
35 if (Err) {
36 report_fatal_error(Err.message());
37 }
38 return Err;
39}
40
Preston Gurd689ff9c2012-04-16 22:12:58 +000041template<support::endianness target_endianness, bool is64Bits>
42class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> {
43 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
44
45 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
46 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
47 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
48 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
49
Michael J. Spencer2c38a662012-09-10 19:04:02 +000050 typedef Elf_Ehdr_Impl<target_endianness, is64Bits> Elf_Ehdr;
Preston Gurd689ff9c2012-04-16 22:12:58 +000051
52 typedef typename ELFDataTypeTypedefHelper<
53 target_endianness, is64Bits>::value_type addr_type;
54
Preston Gurd689ff9c2012-04-16 22:12:58 +000055public:
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000056 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
Preston Gurd689ff9c2012-04-16 22:12:58 +000057
58 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
59 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
60
Andrew Kaylor2e319872012-07-27 17:52:42 +000061 // Methods for type inquiry through isa, cast and dyn_cast
Preston Gurd689ff9c2012-04-16 22:12:58 +000062 static inline bool classof(const Binary *v) {
63 return (isa<ELFObjectFile<target_endianness, is64Bits> >(v)
64 && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v)));
65 }
66 static inline bool classof(
67 const ELFObjectFile<target_endianness, is64Bits> *v) {
68 return v->isDyldType();
69 }
Preston Gurd689ff9c2012-04-16 22:12:58 +000070};
71
72template<support::endianness target_endianness, bool is64Bits>
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000073class ELFObjectImage : public ObjectImageCommon {
Preston Gurd689ff9c2012-04-16 22:12:58 +000074 protected:
75 DyldELFObject<target_endianness, is64Bits> *DyldObj;
76 bool Registered;
77
78 public:
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000079 ELFObjectImage(ObjectBuffer *Input,
80 DyldELFObject<target_endianness, is64Bits> *Obj)
81 : ObjectImageCommon(Input, Obj),
Preston Gurd689ff9c2012-04-16 22:12:58 +000082 DyldObj(Obj),
83 Registered(false) {}
84
85 virtual ~ELFObjectImage() {
86 if (Registered)
87 deregisterWithDebugger();
88 }
89
90 // Subclasses can override these methods to update the image with loaded
91 // addresses for sections and common symbols
92 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
93 {
94 DyldObj->updateSectionAddress(Sec, Addr);
95 }
96
97 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
98 {
99 DyldObj->updateSymbolAddress(Sym, Addr);
100 }
101
102 virtual void registerWithDebugger()
103 {
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000104 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000105 Registered = true;
106 }
107 virtual void deregisterWithDebugger()
108 {
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000109 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000110 }
111};
112
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000113// The MemoryBuffer passed into this constructor is just a wrapper around the
114// actual memory. Ultimately, the Binary parent class will take ownership of
115// this MemoryBuffer object but not the underlying memory.
Preston Gurd689ff9c2012-04-16 22:12:58 +0000116template<support::endianness target_endianness, bool is64Bits>
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000117DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Wrapper,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000118 error_code &ec)
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000119 : ELFObjectFile<target_endianness, is64Bits>(Wrapper, ec) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000120 this->isDyldELFObject = true;
121}
122
123template<support::endianness target_endianness, bool is64Bits>
124void DyldELFObject<target_endianness, is64Bits>::updateSectionAddress(
125 const SectionRef &Sec,
126 uint64_t Addr) {
127 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
128 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
129 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
130
131 // This assumes the address passed in matches the target address bitness
132 // The template-based type cast handles everything else.
133 shdr->sh_addr = static_cast<addr_type>(Addr);
134}
135
136template<support::endianness target_endianness, bool is64Bits>
137void DyldELFObject<target_endianness, is64Bits>::updateSymbolAddress(
138 const SymbolRef &SymRef,
139 uint64_t Addr) {
140
141 Elf_Sym *sym = const_cast<Elf_Sym*>(
142 ELFObjectFile<target_endianness, is64Bits>::
143 getSymbol(SymRef.getRawDataRefImpl()));
144
145 // This assumes the address passed in matches the target address bitness
146 // The template-based type cast handles everything else.
147 sym->st_value = static_cast<addr_type>(Addr);
148}
149
150} // namespace
151
152
Eli Benderskya66a1852012-01-16 08:56:09 +0000153namespace llvm {
154
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000155ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
156 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
157 llvm_unreachable("Unexpected ELF object size");
158 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
159 (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
160 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000161 error_code ec;
162
163 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
164 DyldELFObject<support::little, false> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000165 new DyldELFObject<support::little, false>(Buffer->getMemBuffer(), ec);
166 return new ELFObjectImage<support::little, false>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000167 }
168 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
169 DyldELFObject<support::big, false> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000170 new DyldELFObject<support::big, false>(Buffer->getMemBuffer(), ec);
171 return new ELFObjectImage<support::big, false>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000172 }
173 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
174 DyldELFObject<support::big, true> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000175 new DyldELFObject<support::big, true>(Buffer->getMemBuffer(), ec);
176 return new ELFObjectImage<support::big, true>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000177 }
178 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
179 DyldELFObject<support::little, true> *Obj =
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000180 new DyldELFObject<support::little, true>(Buffer->getMemBuffer(), ec);
181 return new ELFObjectImage<support::little, true>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000182 }
183 else
184 llvm_unreachable("Unexpected ELF format");
185}
186
Preston Gurd689ff9c2012-04-16 22:12:58 +0000187RuntimeDyldELF::~RuntimeDyldELF() {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000188}
Eli Benderskya66a1852012-01-16 08:56:09 +0000189
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000190void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
191 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000192 uint64_t Value,
193 uint32_t Type,
194 int64_t Addend) {
195 switch (Type) {
196 default:
197 llvm_unreachable("Relocation type not implemented yet!");
198 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000199 case ELF::R_X86_64_64: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000200 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000201 *Target = Value + Addend;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000202 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend))
203 << " at " << format("%p\n",Target));
Eli Benderskya66a1852012-01-16 08:56:09 +0000204 break;
205 }
206 case ELF::R_X86_64_32:
207 case ELF::R_X86_64_32S: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000208 Value += Addend;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000209 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
210 (Type == ELF::R_X86_64_32S &&
211 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Benderskya66a1852012-01-16 08:56:09 +0000212 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000213 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
Eli Benderskya66a1852012-01-16 08:56:09 +0000214 *Target = TruncatedAddr;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000215 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr)
216 << " at " << format("%p\n",Target));
Eli Benderskya66a1852012-01-16 08:56:09 +0000217 break;
218 }
219 case ELF::R_X86_64_PC32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000220 // Get the placeholder value from the generated object since
221 // a previous relocation attempt may have overwritten the loaded version
222 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
223 + Offset);
224 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
225 uint64_t FinalAddress = Section.LoadAddress + Offset;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000226 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000227 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000228 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000229 *Target = TruncOffset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000230 break;
231 }
232 }
233}
234
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000235void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
236 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000237 uint32_t Value,
238 uint32_t Type,
239 int32_t Addend) {
240 switch (Type) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000241 case ELF::R_386_32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000242 // Get the placeholder value from the generated object since
243 // a previous relocation attempt may have overwritten the loaded version
244 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
245 + Offset);
246 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
247 *Target = *Placeholder + Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000248 break;
249 }
250 case ELF::R_386_PC32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000251 // Get the placeholder value from the generated object since
252 // a previous relocation attempt may have overwritten the loaded version
253 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
254 + Offset);
255 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
256 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000257 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000258 *Target = RealOffset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000259 break;
260 }
261 default:
262 // There are other relocation types, but it appears these are the
Andrew Kaylore2e73bd2012-07-27 18:39:47 +0000263 // only ones currently used by the LLVM ELF object writer
Craig Topper85814382012-02-07 05:05:23 +0000264 llvm_unreachable("Relocation type not implemented yet!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000265 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000266 }
267}
268
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000269void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
270 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000271 uint32_t Value,
272 uint32_t Type,
273 int32_t Addend) {
274 // TODO: Add Thumb relocations.
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000275 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
276 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000277 Value += Addend;
278
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000279 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
280 << Section.Address + Offset
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000281 << " FinalAddress: " << format("%p",FinalAddress)
282 << " Value: " << format("%x",Value)
283 << " Type: " << format("%x",Type)
284 << " Addend: " << format("%x",Addend)
285 << "\n");
286
287 switch(Type) {
288 default:
289 llvm_unreachable("Not implemented relocation type!");
290
Tim Northover565ebde2012-10-03 16:29:42 +0000291 // Write a 32bit value to relocation address, taking into account the
292 // implicit addend encoded in the target.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000293 case ELF::R_ARM_ABS32 :
Tim Northover565ebde2012-10-03 16:29:42 +0000294 *TargetPtr += Value;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000295 break;
296
297 // Write first 16 bit of 32 bit value to the mov instruction.
298 // Last 4 bit should be shifted.
299 case ELF::R_ARM_MOVW_ABS_NC :
Tim Northover565ebde2012-10-03 16:29:42 +0000300 // We are not expecting any other addend in the relocation address.
301 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
302 // non-contiguous fields.
303 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000304 Value = Value & 0xFFFF;
305 *TargetPtr |= Value & 0xFFF;
306 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
307 break;
308
309 // Write last 16 bit of 32 bit value to the mov instruction.
310 // Last 4 bit should be shifted.
311 case ELF::R_ARM_MOVT_ABS :
Tim Northover565ebde2012-10-03 16:29:42 +0000312 // We are not expecting any other addend in the relocation address.
313 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
314 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000315 Value = (Value >> 16) & 0xFFFF;
316 *TargetPtr |= Value & 0xFFF;
317 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
318 break;
319
320 // Write 24 bit relative value to the branch instruction.
321 case ELF::R_ARM_PC24 : // Fall through.
322 case ELF::R_ARM_CALL : // Fall through.
323 case ELF::R_ARM_JUMP24 :
324 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
325 RelValue = (RelValue & 0x03FFFFFC) >> 2;
326 *TargetPtr &= 0xFF000000;
327 *TargetPtr |= RelValue;
328 break;
329 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000330}
331
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000332void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
333 uint64_t Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000334 uint32_t Value,
335 uint32_t Type,
336 int32_t Addend) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000337 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000338 Value += Addend;
339
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000340 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
341 << Section.Address + Offset
342 << " FinalAddress: "
343 << format("%p",Section.LoadAddress + Offset)
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000344 << " Value: " << format("%x",Value)
345 << " Type: " << format("%x",Type)
346 << " Addend: " << format("%x",Addend)
347 << "\n");
348
349 switch(Type) {
350 default:
351 llvm_unreachable("Not implemented relocation type!");
352 break;
353 case ELF::R_MIPS_32:
354 *TargetPtr = Value + (*TargetPtr);
355 break;
356 case ELF::R_MIPS_26:
357 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
358 break;
359 case ELF::R_MIPS_HI16:
360 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
361 Value += ((*TargetPtr) & 0x0000ffff) << 16;
362 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
363 (((Value + 0x8000) >> 16) & 0xffff);
364 break;
365 case ELF::R_MIPS_LO16:
366 Value += ((*TargetPtr) & 0x0000ffff);
367 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
368 break;
369 }
370}
371
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000372// Return the .TOC. section address to R_PPC64_TOC relocations.
373uint64_t RuntimeDyldELF::findPPC64TOC() const {
374 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
375 // order. The TOC starts where the first of these sections starts.
376 SectionList::const_iterator it = Sections.begin();
377 SectionList::const_iterator ite = Sections.end();
378 for (; it != ite; ++it) {
379 if (it->Name == ".got" ||
380 it->Name == ".toc" ||
381 it->Name == ".tocbss" ||
382 it->Name == ".plt")
383 break;
384 }
385 if (it == ite) {
386 // This may happen for
387 // * references to TOC base base (sym@toc, .odp relocation) without
388 // a .toc directive.
389 // In this case just use the first section (which is usually
390 // the .odp) since the code won't reference the .toc base
391 // directly.
392 it = Sections.begin();
393 }
394 assert (it != ite);
395 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
396 // thus permitting a full 64 Kbytes segment.
397 return it->LoadAddress + 0x8000;
398}
399
400// Returns the sections and offset associated with the ODP entry referenced
401// by Symbol.
402void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
403 ObjSectionToIDMap &LocalSections,
404 RelocationValueRef &Rel) {
405 // Get the ELF symbol value (st_value) to compare with Relocation offset in
406 // .opd entries
407
408 error_code err;
409 for (section_iterator si = Obj.begin_sections(),
410 se = Obj.end_sections(); si != se; si.increment(err)) {
411 StringRef SectionName;
412 check(si->getName(SectionName));
413 if (SectionName != ".opd")
414 continue;
415
416 for (relocation_iterator i = si->begin_relocations(),
417 e = si->end_relocations(); i != e;) {
418 check(err);
419
420 // The R_PPC64_ADDR64 relocation indicates the first field
421 // of a .opd entry
422 uint64_t TypeFunc;
423 check(i->getType(TypeFunc));
424 if (TypeFunc != ELF::R_PPC64_ADDR64) {
425 i.increment(err);
426 continue;
427 }
428
429 SymbolRef TargetSymbol;
430 uint64_t TargetSymbolOffset;
431 int64_t TargetAdditionalInfo;
432 check(i->getSymbol(TargetSymbol));
433 check(i->getOffset(TargetSymbolOffset));
434 check(i->getAdditionalInfo(TargetAdditionalInfo));
435
436 i = i.increment(err);
437 if (i == e)
438 break;
439 check(err);
440
441 // Just check if following relocation is a R_PPC64_TOC
442 uint64_t TypeTOC;
443 check(i->getType(TypeTOC));
444 if (TypeTOC != ELF::R_PPC64_TOC)
445 continue;
446
447 // Finally compares the Symbol value and the target symbol offset
448 // to check if this .opd entry refers to the symbol the relocation
449 // points to.
450 if (Rel.Addend != (intptr_t)TargetSymbolOffset)
451 continue;
452
453 section_iterator tsi(Obj.end_sections());
454 check(TargetSymbol.getSection(tsi));
455 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
456 Rel.Addend = (intptr_t)TargetAdditionalInfo;
457 return;
458 }
459 }
460 llvm_unreachable("Attempting to get address of ODP entry!");
461}
462
463// Relocation masks following the #lo(value), #hi(value), #higher(value),
464// and #highest(value) macros defined in section 4.5.1. Relocation Types
465// in PPC-elf64abi document.
466//
467static inline
468uint16_t applyPPClo (uint64_t value)
469{
470 return value & 0xffff;
471}
472
473static inline
474uint16_t applyPPChi (uint64_t value)
475{
476 return (value >> 16) & 0xffff;
477}
478
479static inline
480uint16_t applyPPChigher (uint64_t value)
481{
482 return (value >> 32) & 0xffff;
483}
484
485static inline
486uint16_t applyPPChighest (uint64_t value)
487{
488 return (value >> 48) & 0xffff;
489}
490
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000491void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
492 uint64_t Offset,
493 uint64_t Value,
494 uint32_t Type,
495 int64_t Addend) {
496 uint8_t* LocalAddress = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000497 switch (Type) {
498 default:
499 llvm_unreachable("Relocation type not implemented yet!");
500 break;
501 case ELF::R_PPC64_ADDR16_LO :
502 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
503 break;
504 case ELF::R_PPC64_ADDR16_HI :
505 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
506 break;
507 case ELF::R_PPC64_ADDR16_HIGHER :
508 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
509 break;
510 case ELF::R_PPC64_ADDR16_HIGHEST :
511 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
512 break;
513 case ELF::R_PPC64_ADDR14 : {
514 assert(((Value + Addend) & 3) == 0);
515 // Preserve the AA/LK bits in the branch instruction
516 uint8_t aalk = *(LocalAddress+3);
517 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
518 } break;
519 case ELF::R_PPC64_REL24 : {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000520 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000521 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
522 if (SignExtend32<24>(delta) != delta)
523 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
524 // Generates a 'bl <address>' instruction
525 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
526 } break;
527 case ELF::R_PPC64_ADDR64 :
528 writeInt64BE(LocalAddress, Value + Addend);
529 break;
530 case ELF::R_PPC64_TOC :
531 writeInt64BE(LocalAddress, findPPC64TOC());
532 break;
533 case ELF::R_PPC64_TOC16 : {
534 uint64_t TOCStart = findPPC64TOC();
535 Value = applyPPClo((Value + Addend) - TOCStart);
536 writeInt16BE(LocalAddress, applyPPClo(Value));
537 } break;
538 case ELF::R_PPC64_TOC16_DS : {
539 uint64_t TOCStart = findPPC64TOC();
540 Value = ((Value + Addend) - TOCStart);
541 writeInt16BE(LocalAddress, applyPPClo(Value));
542 } break;
543 }
544}
545
546
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000547void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
548 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000549 uint64_t Value,
550 uint32_t Type,
551 int64_t Addend) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000552 switch (Arch) {
553 case Triple::x86_64:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000554 resolveX86_64Relocation(Section, Offset, Value, Type, Addend);
Eli Benderskya66a1852012-01-16 08:56:09 +0000555 break;
556 case Triple::x86:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000557 resolveX86Relocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000558 (uint32_t)(Value & 0xffffffffL), Type,
559 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000560 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000561 case Triple::arm: // Fall through.
562 case Triple::thumb:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000563 resolveARMRelocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000564 (uint32_t)(Value & 0xffffffffL), Type,
565 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000566 break;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000567 case Triple::mips: // Fall through.
568 case Triple::mipsel:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000569 resolveMIPSRelocation(Section, Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000570 (uint32_t)(Value & 0xffffffffL), Type,
571 (uint32_t)(Addend & 0xffffffffL));
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000572 break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000573 case Triple::ppc64:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000574 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000575 break;
Craig Topper85814382012-02-07 05:05:23 +0000576 default: llvm_unreachable("Unsupported CPU type!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000577 }
578}
579
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000580void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000581 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000582 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000583 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000584 StubMap &Stubs) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000585
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000586 uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL);
587 intptr_t Addend = (intptr_t)Rel.AdditionalInfo;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000588 const SymbolRef &Symbol = Rel.Symbol;
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000589
590 // Obtain the symbol name which is referenced in the relocation
591 StringRef TargetName;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000592 Symbol.getName(TargetName);
593 DEBUG(dbgs() << "\t\tRelType: " << RelType
594 << " Addend: " << Addend
595 << " TargetName: " << TargetName
596 << "\n");
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000597 RelocationValueRef Value;
598 // First search for the symbol in the local symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000599 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000600 SymbolRef::Type SymType;
601 Symbol.getType(SymType);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000602 if (lsi != Symbols.end()) {
603 Value.SectionID = lsi->second.first;
604 Value.Addend = lsi->second.second;
605 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000606 // Search for the symbol in the global symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000607 SymbolTableMap::const_iterator gsi =
608 GlobalSymbolTable.find(TargetName.data());
609 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000610 Value.SectionID = gsi->second.first;
611 Value.Addend = gsi->second.second;
612 } else {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000613 switch (SymType) {
614 case SymbolRef::ST_Debug: {
615 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
616 // and can be changed by another developers. Maybe best way is add
617 // a new symbol type ST_Section to SymbolRef and use it.
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000618 section_iterator si(Obj.end_sections());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000619 Symbol.getSection(si);
620 if (si == Obj.end_sections())
621 llvm_unreachable("Symbol section not found, bad object file format!");
622 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000623 // Default to 'true' in case isText fails (though it never does).
624 bool isCode = true;
625 si->isText(isCode);
626 Value.SectionID = findOrEmitSection(Obj,
627 (*si),
628 isCode,
629 ObjSectionToID);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000630 Value.Addend = Addend;
631 break;
632 }
633 case SymbolRef::ST_Unknown: {
634 Value.SymbolName = TargetName.data();
635 Value.Addend = Addend;
636 break;
637 }
638 default:
639 llvm_unreachable("Unresolved symbol type!");
640 break;
641 }
642 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000643 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000644 DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID
645 << " Rel.Offset: " << Rel.Offset
646 << "\n");
647 if (Arch == Triple::arm &&
648 (RelType == ELF::R_ARM_PC24 ||
649 RelType == ELF::R_ARM_CALL ||
650 RelType == ELF::R_ARM_JUMP24)) {
651 // This is an ARM branch relocation, need to use a stub function.
652 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
653 SectionEntry &Section = Sections[Rel.SectionID];
Eli Benderskya66a1852012-01-16 08:56:09 +0000654
Eric Christopherbf261f12012-10-23 17:19:15 +0000655 // Look for an existing stub.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000656 StubMap::const_iterator i = Stubs.find(Value);
657 if (i != Stubs.end()) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000658 resolveRelocation(Section, Rel.Offset,
659 (uint64_t)Section.Address + i->second, RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000660 DEBUG(dbgs() << " Stub function found\n");
661 } else {
662 // Create a new stub function.
663 DEBUG(dbgs() << " Create a new stub function\n");
664 Stubs[Value] = Section.StubOffset;
665 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
666 Section.StubOffset);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000667 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
668 ELF::R_ARM_ABS32, Value.Addend);
669 if (Value.SymbolName)
670 addRelocationForSymbol(RE, Value.SymbolName);
671 else
672 addRelocationForSection(RE, Value.SectionID);
673
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000674 resolveRelocation(Section, Rel.Offset,
675 (uint64_t)Section.Address + Section.StubOffset,
676 RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000677 Section.StubOffset += getMaxStubSize();
678 }
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000679 } else if (Arch == Triple::mipsel && RelType == ELF::R_MIPS_26) {
680 // This is an Mips branch relocation, need to use a stub function.
681 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
682 SectionEntry &Section = Sections[Rel.SectionID];
683 uint8_t *Target = Section.Address + Rel.Offset;
684 uint32_t *TargetAddress = (uint32_t *)Target;
685
686 // Extract the addend from the instruction.
687 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
688
689 Value.Addend += Addend;
690
691 // Look up for existing stub.
692 StubMap::const_iterator i = Stubs.find(Value);
693 if (i != Stubs.end()) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000694 resolveRelocation(Section, Rel.Offset,
695 (uint64_t)Section.Address + i->second, RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000696 DEBUG(dbgs() << " Stub function found\n");
697 } else {
698 // Create a new stub function.
699 DEBUG(dbgs() << " Create a new stub function\n");
700 Stubs[Value] = Section.StubOffset;
701 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
702 Section.StubOffset);
703
704 // Creating Hi and Lo relocations for the filled stub instructions.
705 RelocationEntry REHi(Rel.SectionID,
706 StubTargetAddr - Section.Address,
707 ELF::R_MIPS_HI16, Value.Addend);
708 RelocationEntry RELo(Rel.SectionID,
709 StubTargetAddr - Section.Address + 4,
710 ELF::R_MIPS_LO16, Value.Addend);
711
712 if (Value.SymbolName) {
713 addRelocationForSymbol(REHi, Value.SymbolName);
714 addRelocationForSymbol(RELo, Value.SymbolName);
715 } else {
716 addRelocationForSection(REHi, Value.SectionID);
717 addRelocationForSection(RELo, Value.SectionID);
718 }
719
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000720 resolveRelocation(Section, Rel.Offset,
721 (uint64_t)Section.Address + Section.StubOffset,
722 RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000723 Section.StubOffset += getMaxStubSize();
724 }
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000725 } else if (Arch == Triple::ppc64) {
726 if (RelType == ELF::R_PPC64_REL24) {
727 // A PPC branch relocation will need a stub function if the target is
728 // an external symbol (Symbol::ST_Unknown) or if the target address
729 // is not within the signed 24-bits branch address.
730 SectionEntry &Section = Sections[Rel.SectionID];
731 uint8_t *Target = Section.Address + Rel.Offset;
732 bool RangeOverflow = false;
733 if (SymType != SymbolRef::ST_Unknown) {
734 // A function call may points to the .opd entry, so the final symbol value
735 // in calculated based in the relocation values in .opd section.
736 findOPDEntrySection(Obj, ObjSectionToID, Value);
737 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
738 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
739 // If it is within 24-bits branch range, just set the branch target
740 if (SignExtend32<24>(delta) == delta) {
741 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
742 if (Value.SymbolName)
743 addRelocationForSymbol(RE, Value.SymbolName);
744 else
745 addRelocationForSection(RE, Value.SectionID);
746 } else {
747 RangeOverflow = true;
748 }
749 }
750 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
751 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
752 // larger than 24-bits.
753 StubMap::const_iterator i = Stubs.find(Value);
754 if (i != Stubs.end()) {
755 // Symbol function stub already created, just relocate to it
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000756 resolveRelocation(Section, Rel.Offset,
757 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000758 DEBUG(dbgs() << " Stub function found\n");
759 } else {
760 // Create a new stub function.
761 DEBUG(dbgs() << " Create a new stub function\n");
762 Stubs[Value] = Section.StubOffset;
763 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
764 Section.StubOffset);
765 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
766 ELF::R_PPC64_ADDR64, Value.Addend);
767
768 // Generates the 64-bits address loads as exemplified in section
769 // 4.5.1 in PPC64 ELF ABI.
770 RelocationEntry REhst(Rel.SectionID,
771 StubTargetAddr - Section.Address + 2,
772 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
773 RelocationEntry REhr(Rel.SectionID,
774 StubTargetAddr - Section.Address + 6,
775 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
776 RelocationEntry REh(Rel.SectionID,
777 StubTargetAddr - Section.Address + 14,
778 ELF::R_PPC64_ADDR16_HI, Value.Addend);
779 RelocationEntry REl(Rel.SectionID,
780 StubTargetAddr - Section.Address + 18,
781 ELF::R_PPC64_ADDR16_LO, Value.Addend);
782
783 if (Value.SymbolName) {
784 addRelocationForSymbol(REhst, Value.SymbolName);
785 addRelocationForSymbol(REhr, Value.SymbolName);
786 addRelocationForSymbol(REh, Value.SymbolName);
787 addRelocationForSymbol(REl, Value.SymbolName);
788 } else {
789 addRelocationForSection(REhst, Value.SectionID);
790 addRelocationForSection(REhr, Value.SectionID);
791 addRelocationForSection(REh, Value.SectionID);
792 addRelocationForSection(REl, Value.SectionID);
793 }
794
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000795 resolveRelocation(Section, Rel.Offset,
796 (uint64_t)Section.Address + Section.StubOffset,
797 RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000798 if (SymType == SymbolRef::ST_Unknown)
799 // Restore the TOC for external calls
800 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
801 Section.StubOffset += getMaxStubSize();
802 }
803 }
804 } else {
805 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
806 // Extra check to avoid relocation againt empty symbols (usually
807 // the R_PPC64_TOC).
808 if (Value.SymbolName && !TargetName.empty())
809 addRelocationForSymbol(RE, Value.SymbolName);
810 else
811 addRelocationForSection(RE, Value.SectionID);
812 }
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000813 } else {
814 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
815 if (Value.SymbolName)
816 addRelocationForSymbol(RE, Value.SymbolName);
817 else
818 addRelocationForSection(RE, Value.SectionID);
819 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000820}
821
Tim Northoverf00677d2012-10-29 10:47:04 +0000822unsigned RuntimeDyldELF::getCommonSymbolAlignment(const SymbolRef &Sym) {
823 // In ELF, the value of an SHN_COMMON symbol is its alignment requirement.
824 uint64_t Align;
825 Check(Sym.getValue(Align));
826 return Align;
827}
828
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000829bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
830 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
831 return false;
832 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
Eli Benderskya66a1852012-01-16 08:56:09 +0000833}
834} // namespace llvm