blob: f32e3871b8282c2456addf8f6ee33fb141f0dc94 [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/IntervalMap.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/OwningPtr.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000022#include "llvm/ADT/Triple.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/ExecutionEngine/ObjectBuffer.h"
24#include "llvm/ExecutionEngine/ObjectImage.h"
Preston Gurd689ff9c2012-04-16 22:12:58 +000025#include "llvm/Object/ELF.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Object/ObjectFile.h"
27#include "llvm/Support/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.
Amara Emerson098d6d52012-11-16 11:11:59 +0000293 case ELF::R_ARM_TARGET1 :
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000294 case ELF::R_ARM_ABS32 :
Tim Northover565ebde2012-10-03 16:29:42 +0000295 *TargetPtr += Value;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000296 break;
297
298 // Write first 16 bit of 32 bit value to the mov instruction.
299 // Last 4 bit should be shifted.
300 case ELF::R_ARM_MOVW_ABS_NC :
Tim Northover565ebde2012-10-03 16:29:42 +0000301 // We are not expecting any other addend in the relocation address.
302 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
303 // non-contiguous fields.
304 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000305 Value = Value & 0xFFFF;
306 *TargetPtr |= Value & 0xFFF;
307 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
308 break;
309
310 // Write last 16 bit of 32 bit value to the mov instruction.
311 // Last 4 bit should be shifted.
312 case ELF::R_ARM_MOVT_ABS :
Tim Northover565ebde2012-10-03 16:29:42 +0000313 // We are not expecting any other addend in the relocation address.
314 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
315 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000316 Value = (Value >> 16) & 0xFFFF;
317 *TargetPtr |= Value & 0xFFF;
318 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
319 break;
320
321 // Write 24 bit relative value to the branch instruction.
322 case ELF::R_ARM_PC24 : // Fall through.
323 case ELF::R_ARM_CALL : // Fall through.
324 case ELF::R_ARM_JUMP24 :
325 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
326 RelValue = (RelValue & 0x03FFFFFC) >> 2;
327 *TargetPtr &= 0xFF000000;
328 *TargetPtr |= RelValue;
329 break;
330 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000331}
332
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000333void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
334 uint64_t Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000335 uint32_t Value,
336 uint32_t Type,
337 int32_t Addend) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000338 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000339 Value += Addend;
340
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000341 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
342 << Section.Address + Offset
343 << " FinalAddress: "
344 << format("%p",Section.LoadAddress + Offset)
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000345 << " Value: " << format("%x",Value)
346 << " Type: " << format("%x",Type)
347 << " Addend: " << format("%x",Addend)
348 << "\n");
349
350 switch(Type) {
351 default:
352 llvm_unreachable("Not implemented relocation type!");
353 break;
354 case ELF::R_MIPS_32:
355 *TargetPtr = Value + (*TargetPtr);
356 break;
357 case ELF::R_MIPS_26:
358 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
359 break;
360 case ELF::R_MIPS_HI16:
361 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
362 Value += ((*TargetPtr) & 0x0000ffff) << 16;
363 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
364 (((Value + 0x8000) >> 16) & 0xffff);
365 break;
366 case ELF::R_MIPS_LO16:
367 Value += ((*TargetPtr) & 0x0000ffff);
368 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
369 break;
370 }
371}
372
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000373// Return the .TOC. section address to R_PPC64_TOC relocations.
374uint64_t RuntimeDyldELF::findPPC64TOC() const {
375 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
376 // order. The TOC starts where the first of these sections starts.
377 SectionList::const_iterator it = Sections.begin();
378 SectionList::const_iterator ite = Sections.end();
379 for (; it != ite; ++it) {
380 if (it->Name == ".got" ||
381 it->Name == ".toc" ||
382 it->Name == ".tocbss" ||
383 it->Name == ".plt")
384 break;
385 }
386 if (it == ite) {
387 // This may happen for
388 // * references to TOC base base (sym@toc, .odp relocation) without
389 // a .toc directive.
390 // In this case just use the first section (which is usually
391 // the .odp) since the code won't reference the .toc base
392 // directly.
393 it = Sections.begin();
394 }
395 assert (it != ite);
396 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
397 // thus permitting a full 64 Kbytes segment.
398 return it->LoadAddress + 0x8000;
399}
400
401// Returns the sections and offset associated with the ODP entry referenced
402// by Symbol.
403void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
404 ObjSectionToIDMap &LocalSections,
405 RelocationValueRef &Rel) {
406 // Get the ELF symbol value (st_value) to compare with Relocation offset in
407 // .opd entries
408
409 error_code err;
410 for (section_iterator si = Obj.begin_sections(),
411 se = Obj.end_sections(); si != se; si.increment(err)) {
412 StringRef SectionName;
413 check(si->getName(SectionName));
414 if (SectionName != ".opd")
415 continue;
416
417 for (relocation_iterator i = si->begin_relocations(),
418 e = si->end_relocations(); i != e;) {
419 check(err);
420
421 // The R_PPC64_ADDR64 relocation indicates the first field
422 // of a .opd entry
423 uint64_t TypeFunc;
424 check(i->getType(TypeFunc));
425 if (TypeFunc != ELF::R_PPC64_ADDR64) {
426 i.increment(err);
427 continue;
428 }
429
430 SymbolRef TargetSymbol;
431 uint64_t TargetSymbolOffset;
432 int64_t TargetAdditionalInfo;
433 check(i->getSymbol(TargetSymbol));
434 check(i->getOffset(TargetSymbolOffset));
435 check(i->getAdditionalInfo(TargetAdditionalInfo));
436
437 i = i.increment(err);
438 if (i == e)
439 break;
440 check(err);
441
442 // Just check if following relocation is a R_PPC64_TOC
443 uint64_t TypeTOC;
444 check(i->getType(TypeTOC));
445 if (TypeTOC != ELF::R_PPC64_TOC)
446 continue;
447
448 // Finally compares the Symbol value and the target symbol offset
449 // to check if this .opd entry refers to the symbol the relocation
450 // points to.
451 if (Rel.Addend != (intptr_t)TargetSymbolOffset)
452 continue;
453
454 section_iterator tsi(Obj.end_sections());
455 check(TargetSymbol.getSection(tsi));
456 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
457 Rel.Addend = (intptr_t)TargetAdditionalInfo;
458 return;
459 }
460 }
461 llvm_unreachable("Attempting to get address of ODP entry!");
462}
463
464// Relocation masks following the #lo(value), #hi(value), #higher(value),
465// and #highest(value) macros defined in section 4.5.1. Relocation Types
466// in PPC-elf64abi document.
467//
468static inline
469uint16_t applyPPClo (uint64_t value)
470{
471 return value & 0xffff;
472}
473
474static inline
475uint16_t applyPPChi (uint64_t value)
476{
477 return (value >> 16) & 0xffff;
478}
479
480static inline
481uint16_t applyPPChigher (uint64_t value)
482{
483 return (value >> 32) & 0xffff;
484}
485
486static inline
487uint16_t applyPPChighest (uint64_t value)
488{
489 return (value >> 48) & 0xffff;
490}
491
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000492void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
493 uint64_t Offset,
494 uint64_t Value,
495 uint32_t Type,
496 int64_t Addend) {
497 uint8_t* LocalAddress = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000498 switch (Type) {
499 default:
500 llvm_unreachable("Relocation type not implemented yet!");
501 break;
502 case ELF::R_PPC64_ADDR16_LO :
503 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
504 break;
505 case ELF::R_PPC64_ADDR16_HI :
506 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
507 break;
508 case ELF::R_PPC64_ADDR16_HIGHER :
509 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
510 break;
511 case ELF::R_PPC64_ADDR16_HIGHEST :
512 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
513 break;
514 case ELF::R_PPC64_ADDR14 : {
515 assert(((Value + Addend) & 3) == 0);
516 // Preserve the AA/LK bits in the branch instruction
517 uint8_t aalk = *(LocalAddress+3);
518 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
519 } break;
520 case ELF::R_PPC64_REL24 : {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000521 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000522 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
523 if (SignExtend32<24>(delta) != delta)
524 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
525 // Generates a 'bl <address>' instruction
526 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
527 } break;
528 case ELF::R_PPC64_ADDR64 :
529 writeInt64BE(LocalAddress, Value + Addend);
530 break;
531 case ELF::R_PPC64_TOC :
532 writeInt64BE(LocalAddress, findPPC64TOC());
533 break;
534 case ELF::R_PPC64_TOC16 : {
535 uint64_t TOCStart = findPPC64TOC();
536 Value = applyPPClo((Value + Addend) - TOCStart);
537 writeInt16BE(LocalAddress, applyPPClo(Value));
538 } break;
539 case ELF::R_PPC64_TOC16_DS : {
540 uint64_t TOCStart = findPPC64TOC();
541 Value = ((Value + Addend) - TOCStart);
542 writeInt16BE(LocalAddress, applyPPClo(Value));
543 } break;
544 }
545}
546
547
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000548void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
549 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000550 uint64_t Value,
551 uint32_t Type,
552 int64_t Addend) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000553 switch (Arch) {
554 case Triple::x86_64:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000555 resolveX86_64Relocation(Section, Offset, Value, Type, Addend);
Eli Benderskya66a1852012-01-16 08:56:09 +0000556 break;
557 case Triple::x86:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000558 resolveX86Relocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000559 (uint32_t)(Value & 0xffffffffL), Type,
560 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000561 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000562 case Triple::arm: // Fall through.
563 case Triple::thumb:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000564 resolveARMRelocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000565 (uint32_t)(Value & 0xffffffffL), Type,
566 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000567 break;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000568 case Triple::mips: // Fall through.
569 case Triple::mipsel:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000570 resolveMIPSRelocation(Section, Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000571 (uint32_t)(Value & 0xffffffffL), Type,
572 (uint32_t)(Addend & 0xffffffffL));
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000573 break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000574 case Triple::ppc64:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000575 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000576 break;
Craig Topper85814382012-02-07 05:05:23 +0000577 default: llvm_unreachable("Unsupported CPU type!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000578 }
579}
580
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000581void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000582 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000583 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000584 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000585 StubMap &Stubs) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000586
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000587 uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL);
588 intptr_t Addend = (intptr_t)Rel.AdditionalInfo;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000589 const SymbolRef &Symbol = Rel.Symbol;
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000590
591 // Obtain the symbol name which is referenced in the relocation
592 StringRef TargetName;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000593 Symbol.getName(TargetName);
594 DEBUG(dbgs() << "\t\tRelType: " << RelType
595 << " Addend: " << Addend
596 << " TargetName: " << TargetName
597 << "\n");
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000598 RelocationValueRef Value;
599 // First search for the symbol in the local symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000600 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000601 SymbolRef::Type SymType;
602 Symbol.getType(SymType);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000603 if (lsi != Symbols.end()) {
604 Value.SectionID = lsi->second.first;
605 Value.Addend = lsi->second.second;
606 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000607 // Search for the symbol in the global symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000608 SymbolTableMap::const_iterator gsi =
609 GlobalSymbolTable.find(TargetName.data());
610 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000611 Value.SectionID = gsi->second.first;
612 Value.Addend = gsi->second.second;
613 } else {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000614 switch (SymType) {
615 case SymbolRef::ST_Debug: {
616 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
617 // and can be changed by another developers. Maybe best way is add
618 // a new symbol type ST_Section to SymbolRef and use it.
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000619 section_iterator si(Obj.end_sections());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000620 Symbol.getSection(si);
621 if (si == Obj.end_sections())
622 llvm_unreachable("Symbol section not found, bad object file format!");
623 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000624 // Default to 'true' in case isText fails (though it never does).
625 bool isCode = true;
626 si->isText(isCode);
627 Value.SectionID = findOrEmitSection(Obj,
628 (*si),
629 isCode,
630 ObjSectionToID);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000631 Value.Addend = Addend;
632 break;
633 }
634 case SymbolRef::ST_Unknown: {
635 Value.SymbolName = TargetName.data();
636 Value.Addend = Addend;
637 break;
638 }
639 default:
640 llvm_unreachable("Unresolved symbol type!");
641 break;
642 }
643 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000644 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000645 DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID
646 << " Rel.Offset: " << Rel.Offset
647 << "\n");
648 if (Arch == Triple::arm &&
649 (RelType == ELF::R_ARM_PC24 ||
650 RelType == ELF::R_ARM_CALL ||
651 RelType == ELF::R_ARM_JUMP24)) {
652 // This is an ARM branch relocation, need to use a stub function.
653 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
654 SectionEntry &Section = Sections[Rel.SectionID];
Eli Benderskya66a1852012-01-16 08:56:09 +0000655
Eric Christopherbf261f12012-10-23 17:19:15 +0000656 // Look for an existing stub.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000657 StubMap::const_iterator i = Stubs.find(Value);
658 if (i != Stubs.end()) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000659 resolveRelocation(Section, Rel.Offset,
660 (uint64_t)Section.Address + i->second, RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000661 DEBUG(dbgs() << " Stub function found\n");
662 } else {
663 // Create a new stub function.
664 DEBUG(dbgs() << " Create a new stub function\n");
665 Stubs[Value] = Section.StubOffset;
666 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
667 Section.StubOffset);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000668 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
669 ELF::R_ARM_ABS32, Value.Addend);
670 if (Value.SymbolName)
671 addRelocationForSymbol(RE, Value.SymbolName);
672 else
673 addRelocationForSection(RE, Value.SectionID);
674
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000675 resolveRelocation(Section, Rel.Offset,
676 (uint64_t)Section.Address + Section.StubOffset,
677 RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000678 Section.StubOffset += getMaxStubSize();
679 }
Akira Hatanakaade04742012-12-03 23:12:19 +0000680 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
681 RelType == ELF::R_MIPS_26) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000682 // This is an Mips branch relocation, need to use a stub function.
683 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
684 SectionEntry &Section = Sections[Rel.SectionID];
685 uint8_t *Target = Section.Address + Rel.Offset;
686 uint32_t *TargetAddress = (uint32_t *)Target;
687
688 // Extract the addend from the instruction.
689 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
690
691 Value.Addend += Addend;
692
693 // Look up for existing stub.
694 StubMap::const_iterator i = Stubs.find(Value);
695 if (i != Stubs.end()) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000696 resolveRelocation(Section, Rel.Offset,
697 (uint64_t)Section.Address + i->second, RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000698 DEBUG(dbgs() << " Stub function found\n");
699 } else {
700 // Create a new stub function.
701 DEBUG(dbgs() << " Create a new stub function\n");
702 Stubs[Value] = Section.StubOffset;
703 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
704 Section.StubOffset);
705
706 // Creating Hi and Lo relocations for the filled stub instructions.
707 RelocationEntry REHi(Rel.SectionID,
708 StubTargetAddr - Section.Address,
709 ELF::R_MIPS_HI16, Value.Addend);
710 RelocationEntry RELo(Rel.SectionID,
711 StubTargetAddr - Section.Address + 4,
712 ELF::R_MIPS_LO16, Value.Addend);
713
714 if (Value.SymbolName) {
715 addRelocationForSymbol(REHi, Value.SymbolName);
716 addRelocationForSymbol(RELo, Value.SymbolName);
717 } else {
718 addRelocationForSection(REHi, Value.SectionID);
719 addRelocationForSection(RELo, Value.SectionID);
720 }
721
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000722 resolveRelocation(Section, Rel.Offset,
723 (uint64_t)Section.Address + Section.StubOffset,
724 RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000725 Section.StubOffset += getMaxStubSize();
726 }
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000727 } else if (Arch == Triple::ppc64) {
728 if (RelType == ELF::R_PPC64_REL24) {
729 // A PPC branch relocation will need a stub function if the target is
730 // an external symbol (Symbol::ST_Unknown) or if the target address
731 // is not within the signed 24-bits branch address.
732 SectionEntry &Section = Sections[Rel.SectionID];
733 uint8_t *Target = Section.Address + Rel.Offset;
734 bool RangeOverflow = false;
735 if (SymType != SymbolRef::ST_Unknown) {
736 // A function call may points to the .opd entry, so the final symbol value
737 // in calculated based in the relocation values in .opd section.
738 findOPDEntrySection(Obj, ObjSectionToID, Value);
739 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
740 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
741 // If it is within 24-bits branch range, just set the branch target
742 if (SignExtend32<24>(delta) == delta) {
743 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
744 if (Value.SymbolName)
745 addRelocationForSymbol(RE, Value.SymbolName);
746 else
747 addRelocationForSection(RE, Value.SectionID);
748 } else {
749 RangeOverflow = true;
750 }
751 }
752 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
753 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
754 // larger than 24-bits.
755 StubMap::const_iterator i = Stubs.find(Value);
756 if (i != Stubs.end()) {
757 // Symbol function stub already created, just relocate to it
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000758 resolveRelocation(Section, Rel.Offset,
759 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000760 DEBUG(dbgs() << " Stub function found\n");
761 } else {
762 // Create a new stub function.
763 DEBUG(dbgs() << " Create a new stub function\n");
764 Stubs[Value] = Section.StubOffset;
765 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
766 Section.StubOffset);
767 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
768 ELF::R_PPC64_ADDR64, Value.Addend);
769
770 // Generates the 64-bits address loads as exemplified in section
771 // 4.5.1 in PPC64 ELF ABI.
772 RelocationEntry REhst(Rel.SectionID,
773 StubTargetAddr - Section.Address + 2,
774 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
775 RelocationEntry REhr(Rel.SectionID,
776 StubTargetAddr - Section.Address + 6,
777 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
778 RelocationEntry REh(Rel.SectionID,
779 StubTargetAddr - Section.Address + 14,
780 ELF::R_PPC64_ADDR16_HI, Value.Addend);
781 RelocationEntry REl(Rel.SectionID,
782 StubTargetAddr - Section.Address + 18,
783 ELF::R_PPC64_ADDR16_LO, Value.Addend);
784
785 if (Value.SymbolName) {
786 addRelocationForSymbol(REhst, Value.SymbolName);
787 addRelocationForSymbol(REhr, Value.SymbolName);
788 addRelocationForSymbol(REh, Value.SymbolName);
789 addRelocationForSymbol(REl, Value.SymbolName);
790 } else {
791 addRelocationForSection(REhst, Value.SectionID);
792 addRelocationForSection(REhr, Value.SectionID);
793 addRelocationForSection(REh, Value.SectionID);
794 addRelocationForSection(REl, Value.SectionID);
795 }
796
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000797 resolveRelocation(Section, Rel.Offset,
798 (uint64_t)Section.Address + Section.StubOffset,
799 RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000800 if (SymType == SymbolRef::ST_Unknown)
801 // Restore the TOC for external calls
802 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
803 Section.StubOffset += getMaxStubSize();
804 }
805 }
806 } else {
807 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
808 // Extra check to avoid relocation againt empty symbols (usually
809 // the R_PPC64_TOC).
810 if (Value.SymbolName && !TargetName.empty())
811 addRelocationForSymbol(RE, Value.SymbolName);
812 else
813 addRelocationForSection(RE, Value.SectionID);
814 }
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000815 } else {
816 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
817 if (Value.SymbolName)
818 addRelocationForSymbol(RE, Value.SymbolName);
819 else
820 addRelocationForSection(RE, Value.SectionID);
821 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000822}
823
Tim Northoverf00677d2012-10-29 10:47:04 +0000824unsigned RuntimeDyldELF::getCommonSymbolAlignment(const SymbolRef &Sym) {
825 // In ELF, the value of an SHN_COMMON symbol is its alignment requirement.
826 uint64_t Align;
827 Check(Sym.getValue(Align));
828 return Align;
829}
830
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000831bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
832 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
833 return false;
834 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
Eli Benderskya66a1852012-01-16 08:56:09 +0000835}
836} // namespace llvm