blob: 2773bf1e191c4ff35c5926cc4885a2140c1b25b5 [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
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000041template<class ELFT>
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000042class DyldELFObject
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000043 : public ELFObjectFile<ELFT> {
Rafael Espindola43239072013-04-17 21:20:55 +000044 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Preston Gurd689ff9c2012-04-16 22:12:58 +000045
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000046 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
47 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000048 typedef
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000049 Elf_Rel_Impl<ELFT, false> Elf_Rel;
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000050 typedef
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000051 Elf_Rel_Impl<ELFT, true> Elf_Rela;
Preston Gurd689ff9c2012-04-16 22:12:58 +000052
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000053 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
Preston Gurd689ff9c2012-04-16 22:12:58 +000054
55 typedef typename ELFDataTypeTypedefHelper<
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000056 ELFT>::value_type addr_type;
Preston Gurd689ff9c2012-04-16 22:12:58 +000057
Preston Gurd689ff9c2012-04-16 22:12:58 +000058public:
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000059 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
Preston Gurd689ff9c2012-04-16 22:12:58 +000060
61 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
62 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
63
Andrew Kaylor2e319872012-07-27 17:52:42 +000064 // Methods for type inquiry through isa, cast and dyn_cast
Preston Gurd689ff9c2012-04-16 22:12:58 +000065 static inline bool classof(const Binary *v) {
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000066 return (isa<ELFObjectFile<ELFT> >(v)
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000067 && classof(cast<ELFObjectFile
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000068 <ELFT> >(v)));
Preston Gurd689ff9c2012-04-16 22:12:58 +000069 }
70 static inline bool classof(
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000071 const ELFObjectFile<ELFT> *v) {
Preston Gurd689ff9c2012-04-16 22:12:58 +000072 return v->isDyldType();
73 }
Preston Gurd689ff9c2012-04-16 22:12:58 +000074};
75
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000076template<class ELFT>
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000077class ELFObjectImage : public ObjectImageCommon {
Preston Gurd689ff9c2012-04-16 22:12:58 +000078 protected:
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000079 DyldELFObject<ELFT> *DyldObj;
Preston Gurd689ff9c2012-04-16 22:12:58 +000080 bool Registered;
81
82 public:
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000083 ELFObjectImage(ObjectBuffer *Input,
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000084 DyldELFObject<ELFT> *Obj)
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000085 : ObjectImageCommon(Input, Obj),
Preston Gurd689ff9c2012-04-16 22:12:58 +000086 DyldObj(Obj),
87 Registered(false) {}
88
89 virtual ~ELFObjectImage() {
90 if (Registered)
91 deregisterWithDebugger();
92 }
93
94 // Subclasses can override these methods to update the image with loaded
95 // addresses for sections and common symbols
96 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
97 {
98 DyldObj->updateSectionAddress(Sec, Addr);
99 }
100
101 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
102 {
103 DyldObj->updateSymbolAddress(Sym, Addr);
104 }
105
106 virtual void registerWithDebugger()
107 {
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000108 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000109 Registered = true;
110 }
111 virtual void deregisterWithDebugger()
112 {
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000113 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000114 }
115};
116
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000117// The MemoryBuffer passed into this constructor is just a wrapper around the
118// actual memory. Ultimately, the Binary parent class will take ownership of
119// this MemoryBuffer object but not the underlying memory.
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000120template<class ELFT>
121DyldELFObject<ELFT>::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
122 : ELFObjectFile<ELFT>(Wrapper, ec) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000123 this->isDyldELFObject = true;
124}
125
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000126template<class ELFT>
127void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
128 uint64_t Addr) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000129 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
130 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
131 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
132
133 // This assumes the address passed in matches the target address bitness
134 // The template-based type cast handles everything else.
135 shdr->sh_addr = static_cast<addr_type>(Addr);
136}
137
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000138template<class ELFT>
139void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
140 uint64_t Addr) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000141
142 Elf_Sym *sym = const_cast<Elf_Sym*>(
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000143 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
Preston Gurd689ff9c2012-04-16 22:12:58 +0000144
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
Eli Benderskya66a1852012-01-16 08:56:09 +0000152namespace llvm {
153
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000154StringRef RuntimeDyldELF::getEHFrameSection() {
155 for (int i = 0, e = Sections.size(); i != e; ++i) {
156 if (Sections[i].Name == ".eh_frame")
157 return StringRef((const char*)Sections[i].Address, Sections[i].Size);
158 }
159 return StringRef();
160}
161
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000162ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
163 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
164 llvm_unreachable("Unexpected ELF object size");
165 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
166 (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
167 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000168 error_code ec;
169
170 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000171 DyldELFObject<ELFType<support::little, 4, false> > *Obj =
172 new DyldELFObject<ELFType<support::little, 4, false> >(
173 Buffer->getMemBuffer(), ec);
174 return new ELFObjectImage<ELFType<support::little, 4, false> >(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000175 }
176 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000177 DyldELFObject<ELFType<support::big, 4, false> > *Obj =
178 new DyldELFObject<ELFType<support::big, 4, false> >(
179 Buffer->getMemBuffer(), ec);
180 return new ELFObjectImage<ELFType<support::big, 4, false> >(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000181 }
182 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000183 DyldELFObject<ELFType<support::big, 8, true> > *Obj =
184 new DyldELFObject<ELFType<support::big, 8, true> >(
185 Buffer->getMemBuffer(), ec);
186 return new ELFObjectImage<ELFType<support::big, 8, true> >(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000187 }
188 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000189 DyldELFObject<ELFType<support::little, 8, true> > *Obj =
190 new DyldELFObject<ELFType<support::little, 8, true> >(
191 Buffer->getMemBuffer(), ec);
192 return new ELFObjectImage<ELFType<support::little, 8, true> >(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000193 }
194 else
195 llvm_unreachable("Unexpected ELF format");
196}
197
Preston Gurd689ff9c2012-04-16 22:12:58 +0000198RuntimeDyldELF::~RuntimeDyldELF() {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000199}
Eli Benderskya66a1852012-01-16 08:56:09 +0000200
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000201void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
202 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000203 uint64_t Value,
204 uint32_t Type,
205 int64_t Addend) {
206 switch (Type) {
207 default:
208 llvm_unreachable("Relocation type not implemented yet!");
209 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000210 case ELF::R_X86_64_64: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000211 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000212 *Target = Value + Addend;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000213 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend))
214 << " at " << format("%p\n",Target));
Eli Benderskya66a1852012-01-16 08:56:09 +0000215 break;
216 }
217 case ELF::R_X86_64_32:
218 case ELF::R_X86_64_32S: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000219 Value += Addend;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000220 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000221 (Type == ELF::R_X86_64_32S &&
Andrew Kaylord83a5472012-07-27 20:30:12 +0000222 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Benderskya66a1852012-01-16 08:56:09 +0000223 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000224 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
Eli Benderskya66a1852012-01-16 08:56:09 +0000225 *Target = TruncatedAddr;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000226 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr)
227 << " at " << format("%p\n",Target));
Eli Benderskya66a1852012-01-16 08:56:09 +0000228 break;
229 }
230 case ELF::R_X86_64_PC32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000231 // Get the placeholder value from the generated object since
232 // a previous relocation attempt may have overwritten the loaded version
233 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
234 + Offset);
235 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
236 uint64_t FinalAddress = Section.LoadAddress + Offset;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000237 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000238 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000239 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000240 *Target = TruncOffset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000241 break;
242 }
243 }
244}
245
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000246void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
247 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000248 uint32_t Value,
249 uint32_t Type,
250 int32_t Addend) {
251 switch (Type) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000252 case ELF::R_386_32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000253 // Get the placeholder value from the generated object since
254 // a previous relocation attempt may have overwritten the loaded version
255 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
256 + Offset);
257 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
258 *Target = *Placeholder + Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000259 break;
260 }
261 case ELF::R_386_PC32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000262 // Get the placeholder value from the generated object since
263 // a previous relocation attempt may have overwritten the loaded version
264 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
265 + Offset);
266 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
267 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000268 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000269 *Target = RealOffset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000270 break;
271 }
272 default:
273 // There are other relocation types, but it appears these are the
Andrew Kaylore2e73bd2012-07-27 18:39:47 +0000274 // only ones currently used by the LLVM ELF object writer
Craig Topper85814382012-02-07 05:05:23 +0000275 llvm_unreachable("Relocation type not implemented yet!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000276 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000277 }
278}
279
David Tweed91c623d2013-05-17 14:31:59 +0000280// FIXME: PR16013: this routine needs modification to handle repeated relocations.
Tim Northover85829bb2013-05-04 20:13:59 +0000281void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
282 uint64_t Offset,
283 uint64_t Value,
284 uint32_t Type,
285 int64_t Addend) {
286 uint32_t *TargetPtr = reinterpret_cast<uint32_t*>(Section.Address + Offset);
287 uint64_t FinalAddress = Section.LoadAddress + Offset;
288
289 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
290 << format("%llx", Section.Address + Offset)
291 << " FinalAddress: 0x" << format("%llx",FinalAddress)
292 << " Value: 0x" << format("%llx",Value)
293 << " Type: 0x" << format("%x",Type)
294 << " Addend: 0x" << format("%llx",Addend)
295 << "\n");
296
297 switch (Type) {
298 default:
299 llvm_unreachable("Relocation type not implemented yet!");
300 break;
Tim Northoverd52eaae2013-05-04 20:14:14 +0000301 case ELF::R_AARCH64_ABS64: {
302 uint64_t *TargetPtr = reinterpret_cast<uint64_t*>(Section.Address + Offset);
303 *TargetPtr = Value + Addend;
304 break;
305 }
Tim Northover85829bb2013-05-04 20:13:59 +0000306 case ELF::R_AARCH64_PREL32: { // test-shift.ll (.eh_frame)
307 uint64_t Result = Value + Addend - FinalAddress;
308 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
309 static_cast<int64_t>(Result) <= UINT32_MAX);
310 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
311 break;
312 }
Tim Northover4a9b6b72013-05-04 20:14:09 +0000313 case ELF::R_AARCH64_CALL26: // fallthrough
314 case ELF::R_AARCH64_JUMP26: {
315 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
316 // calculation.
317 uint64_t BranchImm = Value + Addend - FinalAddress;
318
319 // "Check that -2^27 <= result < 2^27".
320 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
321 static_cast<int64_t>(BranchImm) < (1LL << 27));
322 // Immediate goes in bits 25:0 of B and BL.
323 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
324 break;
325 }
Tim Northover654c2d62013-05-04 20:14:04 +0000326 case ELF::R_AARCH64_MOVW_UABS_G3: {
327 uint64_t Result = Value + Addend;
328 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
329 *TargetPtr |= Result >> (48 - 5);
330 // Shift is "lsl #48", in bits 22:21
331 *TargetPtr |= 3 << 21;
332 break;
333 }
334 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
335 uint64_t Result = Value + Addend;
336 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
337 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
338 // Shift is "lsl #32", in bits 22:21
339 *TargetPtr |= 2 << 21;
340 break;
341 }
342 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
343 uint64_t Result = Value + Addend;
344 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
345 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
346 // Shift is "lsl #16", in bits 22:21
347 *TargetPtr |= 1 << 21;
348 break;
349 }
350 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
351 uint64_t Result = Value + Addend;
352 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
353 *TargetPtr |= ((Result & 0xffffU) << 5);
354 // Shift is "lsl #0", in bits 22:21. No action needed.
355 break;
356 }
Tim Northover85829bb2013-05-04 20:13:59 +0000357 }
358}
359
David Tweed91c623d2013-05-17 14:31:59 +0000360// FIXME: PR16013: this routine needs modification to handle repeated relocations.
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000361void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
362 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000363 uint32_t Value,
364 uint32_t Type,
365 int32_t Addend) {
366 // TODO: Add Thumb relocations.
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000367 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
368 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000369 Value += Addend;
370
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000371 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
372 << Section.Address + Offset
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000373 << " FinalAddress: " << format("%p",FinalAddress)
374 << " Value: " << format("%x",Value)
375 << " Type: " << format("%x",Type)
376 << " Addend: " << format("%x",Addend)
377 << "\n");
378
379 switch(Type) {
380 default:
381 llvm_unreachable("Not implemented relocation type!");
382
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000383 // Write a 32bit value to relocation address, taking into account the
Tim Northover565ebde2012-10-03 16:29:42 +0000384 // implicit addend encoded in the target.
Amara Emerson098d6d52012-11-16 11:11:59 +0000385 case ELF::R_ARM_TARGET1 :
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000386 case ELF::R_ARM_ABS32 :
Tim Northover565ebde2012-10-03 16:29:42 +0000387 *TargetPtr += Value;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000388 break;
389
390 // Write first 16 bit of 32 bit value to the mov instruction.
391 // Last 4 bit should be shifted.
392 case ELF::R_ARM_MOVW_ABS_NC :
Tim Northover565ebde2012-10-03 16:29:42 +0000393 // We are not expecting any other addend in the relocation address.
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000394 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover565ebde2012-10-03 16:29:42 +0000395 // non-contiguous fields.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000396 Value = Value & 0xFFFF;
David Tweed91c623d2013-05-17 14:31:59 +0000397 *TargetPtr &= ~0x000F0FFF; // Not really right; see FIXME at top.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000398 *TargetPtr |= Value & 0xFFF;
399 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
400 break;
401
402 // Write last 16 bit of 32 bit value to the mov instruction.
403 // Last 4 bit should be shifted.
404 case ELF::R_ARM_MOVT_ABS :
Tim Northover565ebde2012-10-03 16:29:42 +0000405 // We are not expecting any other addend in the relocation address.
406 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000407 Value = (Value >> 16) & 0xFFFF;
David Tweed91c623d2013-05-17 14:31:59 +0000408 *TargetPtr &= ~0x000F0FFF; // Not really right; see FIXME at top.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000409 *TargetPtr |= Value & 0xFFF;
410 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
411 break;
412
413 // Write 24 bit relative value to the branch instruction.
414 case ELF::R_ARM_PC24 : // Fall through.
415 case ELF::R_ARM_CALL : // Fall through.
416 case ELF::R_ARM_JUMP24 :
417 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
418 RelValue = (RelValue & 0x03FFFFFC) >> 2;
419 *TargetPtr &= 0xFF000000;
420 *TargetPtr |= RelValue;
421 break;
422 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000423}
424
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000425void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
426 uint64_t Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000427 uint32_t Value,
428 uint32_t Type,
429 int32_t Addend) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000430 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000431 Value += Addend;
432
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000433 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
434 << Section.Address + Offset
435 << " FinalAddress: "
436 << format("%p",Section.LoadAddress + Offset)
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000437 << " Value: " << format("%x",Value)
438 << " Type: " << format("%x",Type)
439 << " Addend: " << format("%x",Addend)
440 << "\n");
441
442 switch(Type) {
443 default:
444 llvm_unreachable("Not implemented relocation type!");
445 break;
446 case ELF::R_MIPS_32:
447 *TargetPtr = Value + (*TargetPtr);
448 break;
449 case ELF::R_MIPS_26:
450 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
451 break;
452 case ELF::R_MIPS_HI16:
453 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
454 Value += ((*TargetPtr) & 0x0000ffff) << 16;
455 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
456 (((Value + 0x8000) >> 16) & 0xffff);
457 break;
458 case ELF::R_MIPS_LO16:
459 Value += ((*TargetPtr) & 0x0000ffff);
460 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
461 break;
462 }
463}
464
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000465// Return the .TOC. section address to R_PPC64_TOC relocations.
466uint64_t RuntimeDyldELF::findPPC64TOC() const {
467 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
468 // order. The TOC starts where the first of these sections starts.
469 SectionList::const_iterator it = Sections.begin();
470 SectionList::const_iterator ite = Sections.end();
471 for (; it != ite; ++it) {
472 if (it->Name == ".got" ||
473 it->Name == ".toc" ||
474 it->Name == ".tocbss" ||
475 it->Name == ".plt")
476 break;
477 }
478 if (it == ite) {
479 // This may happen for
480 // * references to TOC base base (sym@toc, .odp relocation) without
481 // a .toc directive.
482 // In this case just use the first section (which is usually
483 // the .odp) since the code won't reference the .toc base
484 // directly.
485 it = Sections.begin();
486 }
487 assert (it != ite);
488 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
489 // thus permitting a full 64 Kbytes segment.
490 return it->LoadAddress + 0x8000;
491}
492
493// Returns the sections and offset associated with the ODP entry referenced
494// by Symbol.
495void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
496 ObjSectionToIDMap &LocalSections,
497 RelocationValueRef &Rel) {
498 // Get the ELF symbol value (st_value) to compare with Relocation offset in
499 // .opd entries
500
501 error_code err;
502 for (section_iterator si = Obj.begin_sections(),
503 se = Obj.end_sections(); si != se; si.increment(err)) {
504 StringRef SectionName;
505 check(si->getName(SectionName));
506 if (SectionName != ".opd")
507 continue;
508
509 for (relocation_iterator i = si->begin_relocations(),
510 e = si->end_relocations(); i != e;) {
511 check(err);
512
513 // The R_PPC64_ADDR64 relocation indicates the first field
514 // of a .opd entry
515 uint64_t TypeFunc;
516 check(i->getType(TypeFunc));
517 if (TypeFunc != ELF::R_PPC64_ADDR64) {
518 i.increment(err);
519 continue;
520 }
521
522 SymbolRef TargetSymbol;
523 uint64_t TargetSymbolOffset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000524 check(i->getSymbol(TargetSymbol));
525 check(i->getOffset(TargetSymbolOffset));
Rafael Espindola167957f2013-05-09 03:39:05 +0000526 int64_t Addend;
527 check(getELFRelocationAddend(*i, Addend));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000528
529 i = i.increment(err);
530 if (i == e)
531 break;
532 check(err);
533
534 // Just check if following relocation is a R_PPC64_TOC
535 uint64_t TypeTOC;
536 check(i->getType(TypeTOC));
537 if (TypeTOC != ELF::R_PPC64_TOC)
538 continue;
539
540 // Finally compares the Symbol value and the target symbol offset
541 // to check if this .opd entry refers to the symbol the relocation
542 // points to.
543 if (Rel.Addend != (intptr_t)TargetSymbolOffset)
544 continue;
545
546 section_iterator tsi(Obj.end_sections());
547 check(TargetSymbol.getSection(tsi));
548 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
Rafael Espindola167957f2013-05-09 03:39:05 +0000549 Rel.Addend = (intptr_t)Addend;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000550 return;
551 }
552 }
553 llvm_unreachable("Attempting to get address of ODP entry!");
554}
555
556// Relocation masks following the #lo(value), #hi(value), #higher(value),
557// and #highest(value) macros defined in section 4.5.1. Relocation Types
558// in PPC-elf64abi document.
559//
560static inline
561uint16_t applyPPClo (uint64_t value)
562{
563 return value & 0xffff;
564}
565
566static inline
567uint16_t applyPPChi (uint64_t value)
568{
569 return (value >> 16) & 0xffff;
570}
571
572static inline
573uint16_t applyPPChigher (uint64_t value)
574{
575 return (value >> 32) & 0xffff;
576}
577
578static inline
579uint16_t applyPPChighest (uint64_t value)
580{
581 return (value >> 48) & 0xffff;
582}
583
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000584void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
585 uint64_t Offset,
586 uint64_t Value,
587 uint32_t Type,
588 int64_t Addend) {
589 uint8_t* LocalAddress = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000590 switch (Type) {
591 default:
592 llvm_unreachable("Relocation type not implemented yet!");
593 break;
594 case ELF::R_PPC64_ADDR16_LO :
595 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
596 break;
597 case ELF::R_PPC64_ADDR16_HI :
598 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
599 break;
600 case ELF::R_PPC64_ADDR16_HIGHER :
601 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
602 break;
603 case ELF::R_PPC64_ADDR16_HIGHEST :
604 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
605 break;
606 case ELF::R_PPC64_ADDR14 : {
607 assert(((Value + Addend) & 3) == 0);
608 // Preserve the AA/LK bits in the branch instruction
609 uint8_t aalk = *(LocalAddress+3);
610 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
611 } break;
Adhemerval Zanella7b449882013-01-04 19:08:13 +0000612 case ELF::R_PPC64_ADDR32 : {
613 int32_t Result = static_cast<int32_t>(Value + Addend);
614 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanellaa1db5de2013-01-09 17:08:15 +0000615 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella7b449882013-01-04 19:08:13 +0000616 writeInt32BE(LocalAddress, Result);
617 } break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000618 case ELF::R_PPC64_REL24 : {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000619 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000620 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
621 if (SignExtend32<24>(delta) != delta)
622 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
623 // Generates a 'bl <address>' instruction
624 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
625 } break;
Adhemerval Zanellaa1db5de2013-01-09 17:08:15 +0000626 case ELF::R_PPC64_REL32 : {
627 uint64_t FinalAddress = (Section.LoadAddress + Offset);
628 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
629 if (SignExtend32<32>(delta) != delta)
630 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
631 writeInt32BE(LocalAddress, delta);
632 } break;
Adhemerval Zanellaf51d7e72013-05-06 17:21:23 +0000633 case ELF::R_PPC64_REL64: {
634 uint64_t FinalAddress = (Section.LoadAddress + Offset);
635 uint64_t Delta = Value - FinalAddress + Addend;
636 writeInt64BE(LocalAddress, Delta);
637 } break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000638 case ELF::R_PPC64_ADDR64 :
639 writeInt64BE(LocalAddress, Value + Addend);
640 break;
641 case ELF::R_PPC64_TOC :
642 writeInt64BE(LocalAddress, findPPC64TOC());
643 break;
644 case ELF::R_PPC64_TOC16 : {
645 uint64_t TOCStart = findPPC64TOC();
646 Value = applyPPClo((Value + Addend) - TOCStart);
647 writeInt16BE(LocalAddress, applyPPClo(Value));
648 } break;
649 case ELF::R_PPC64_TOC16_DS : {
650 uint64_t TOCStart = findPPC64TOC();
651 Value = ((Value + Addend) - TOCStart);
652 writeInt16BE(LocalAddress, applyPPClo(Value));
653 } break;
654 }
655}
656
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000657void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
658 uint64_t Offset,
659 uint64_t Value,
660 uint32_t Type,
661 int64_t Addend) {
662 uint8_t *LocalAddress = Section.Address + Offset;
663 switch (Type) {
664 default:
665 llvm_unreachable("Relocation type not implemented yet!");
666 break;
667 case ELF::R_390_PC16DBL:
668 case ELF::R_390_PLT16DBL: {
669 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
670 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
671 writeInt16BE(LocalAddress, Delta / 2);
672 break;
673 }
674 case ELF::R_390_PC32DBL:
675 case ELF::R_390_PLT32DBL: {
676 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
677 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
678 writeInt32BE(LocalAddress, Delta / 2);
679 break;
680 }
681 case ELF::R_390_PC32: {
682 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
683 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
684 writeInt32BE(LocalAddress, Delta);
685 break;
686 }
687 case ELF::R_390_64:
688 writeInt64BE(LocalAddress, Value + Addend);
689 break;
690 }
691}
692
Rafael Espindola87b50172013-04-29 17:24:34 +0000693void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
694 uint64_t Value) {
695 const SectionEntry &Section = Sections[RE.SectionID];
696 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend);
697}
698
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000699void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
700 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000701 uint64_t Value,
702 uint32_t Type,
703 int64_t Addend) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000704 switch (Arch) {
705 case Triple::x86_64:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000706 resolveX86_64Relocation(Section, Offset, Value, Type, Addend);
Eli Benderskya66a1852012-01-16 08:56:09 +0000707 break;
708 case Triple::x86:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000709 resolveX86Relocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000710 (uint32_t)(Value & 0xffffffffL), Type,
711 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000712 break;
Tim Northover85829bb2013-05-04 20:13:59 +0000713 case Triple::aarch64:
714 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
715 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000716 case Triple::arm: // Fall through.
717 case Triple::thumb:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000718 resolveARMRelocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000719 (uint32_t)(Value & 0xffffffffL), Type,
720 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000721 break;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000722 case Triple::mips: // Fall through.
723 case Triple::mipsel:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000724 resolveMIPSRelocation(Section, Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000725 (uint32_t)(Value & 0xffffffffL), Type,
726 (uint32_t)(Addend & 0xffffffffL));
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000727 break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000728 case Triple::ppc64:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000729 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000730 break;
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000731 case Triple::systemz:
732 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
733 break;
Craig Topper85814382012-02-07 05:05:23 +0000734 default: llvm_unreachable("Unsupported CPU type!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000735 }
736}
737
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000738void RuntimeDyldELF::processRelocationRef(unsigned SectionID,
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000739 RelocationRef RelI,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000740 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000741 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000742 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000743 StubMap &Stubs) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000744 uint64_t RelType;
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000745 Check(RelI.getType(RelType));
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000746 int64_t Addend;
Rafael Espindola167957f2013-05-09 03:39:05 +0000747 Check(getELFRelocationAddend(RelI, Addend));
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000748 SymbolRef Symbol;
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000749 Check(RelI.getSymbol(Symbol));
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000750
751 // Obtain the symbol name which is referenced in the relocation
752 StringRef TargetName;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000753 Symbol.getName(TargetName);
754 DEBUG(dbgs() << "\t\tRelType: " << RelType
755 << " Addend: " << Addend
756 << " TargetName: " << TargetName
757 << "\n");
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000758 RelocationValueRef Value;
759 // First search for the symbol in the local symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000760 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000761 SymbolRef::Type SymType;
762 Symbol.getType(SymType);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000763 if (lsi != Symbols.end()) {
764 Value.SectionID = lsi->second.first;
Ulrich Weigand03f018a2013-04-05 13:29:04 +0000765 Value.Addend = lsi->second.second + Addend;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000766 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000767 // Search for the symbol in the global symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000768 SymbolTableMap::const_iterator gsi =
769 GlobalSymbolTable.find(TargetName.data());
770 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000771 Value.SectionID = gsi->second.first;
Ulrich Weigand03f018a2013-04-05 13:29:04 +0000772 Value.Addend = gsi->second.second + Addend;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000773 } else {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000774 switch (SymType) {
775 case SymbolRef::ST_Debug: {
776 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
777 // and can be changed by another developers. Maybe best way is add
778 // a new symbol type ST_Section to SymbolRef and use it.
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000779 section_iterator si(Obj.end_sections());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000780 Symbol.getSection(si);
781 if (si == Obj.end_sections())
782 llvm_unreachable("Symbol section not found, bad object file format!");
783 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000784 // Default to 'true' in case isText fails (though it never does).
785 bool isCode = true;
786 si->isText(isCode);
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000787 Value.SectionID = findOrEmitSection(Obj,
788 (*si),
789 isCode,
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000790 ObjSectionToID);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000791 Value.Addend = Addend;
792 break;
793 }
794 case SymbolRef::ST_Unknown: {
795 Value.SymbolName = TargetName.data();
796 Value.Addend = Addend;
797 break;
798 }
799 default:
800 llvm_unreachable("Unresolved symbol type!");
801 break;
802 }
803 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000804 }
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000805 uint64_t Offset;
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000806 Check(RelI.getOffset(Offset));
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000807
808 DEBUG(dbgs() << "\t\tSectionID: " << SectionID
809 << " Offset: " << Offset
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000810 << "\n");
Tim Northover4a9b6b72013-05-04 20:14:09 +0000811 if (Arch == Triple::aarch64 &&
812 (RelType == ELF::R_AARCH64_CALL26 ||
813 RelType == ELF::R_AARCH64_JUMP26)) {
814 // This is an AArch64 branch relocation, need to use a stub function.
815 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
816 SectionEntry &Section = Sections[SectionID];
817
818 // Look for an existing stub.
819 StubMap::const_iterator i = Stubs.find(Value);
820 if (i != Stubs.end()) {
821 resolveRelocation(Section, Offset,
822 (uint64_t)Section.Address + i->second, RelType, 0);
823 DEBUG(dbgs() << " Stub function found\n");
824 } else {
825 // Create a new stub function.
826 DEBUG(dbgs() << " Create a new stub function\n");
827 Stubs[Value] = Section.StubOffset;
828 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
829 Section.StubOffset);
830
831 RelocationEntry REmovz_g3(SectionID,
832 StubTargetAddr - Section.Address,
833 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
834 RelocationEntry REmovk_g2(SectionID,
835 StubTargetAddr - Section.Address + 4,
836 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
837 RelocationEntry REmovk_g1(SectionID,
838 StubTargetAddr - Section.Address + 8,
839 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
840 RelocationEntry REmovk_g0(SectionID,
841 StubTargetAddr - Section.Address + 12,
842 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
843
844 if (Value.SymbolName) {
845 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
846 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
847 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
848 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
849 } else {
850 addRelocationForSection(REmovz_g3, Value.SectionID);
851 addRelocationForSection(REmovk_g2, Value.SectionID);
852 addRelocationForSection(REmovk_g1, Value.SectionID);
853 addRelocationForSection(REmovk_g0, Value.SectionID);
854 }
855 resolveRelocation(Section, Offset,
856 (uint64_t)Section.Address + Section.StubOffset,
857 RelType, 0);
858 Section.StubOffset += getMaxStubSize();
859 }
860 } else if (Arch == Triple::arm &&
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000861 (RelType == ELF::R_ARM_PC24 ||
862 RelType == ELF::R_ARM_CALL ||
863 RelType == ELF::R_ARM_JUMP24)) {
864 // This is an ARM branch relocation, need to use a stub function.
865 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000866 SectionEntry &Section = Sections[SectionID];
Eli Benderskya66a1852012-01-16 08:56:09 +0000867
Eric Christopherbf261f12012-10-23 17:19:15 +0000868 // Look for an existing stub.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000869 StubMap::const_iterator i = Stubs.find(Value);
870 if (i != Stubs.end()) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000871 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000872 (uint64_t)Section.Address + i->second, RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000873 DEBUG(dbgs() << " Stub function found\n");
874 } else {
875 // Create a new stub function.
876 DEBUG(dbgs() << " Create a new stub function\n");
877 Stubs[Value] = Section.StubOffset;
878 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
879 Section.StubOffset);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000880 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000881 ELF::R_ARM_ABS32, Value.Addend);
882 if (Value.SymbolName)
883 addRelocationForSymbol(RE, Value.SymbolName);
884 else
885 addRelocationForSection(RE, Value.SectionID);
886
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000887 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000888 (uint64_t)Section.Address + Section.StubOffset,
889 RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000890 Section.StubOffset += getMaxStubSize();
891 }
Akira Hatanakaade04742012-12-03 23:12:19 +0000892 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
893 RelType == ELF::R_MIPS_26) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000894 // This is an Mips branch relocation, need to use a stub function.
895 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000896 SectionEntry &Section = Sections[SectionID];
897 uint8_t *Target = Section.Address + Offset;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000898 uint32_t *TargetAddress = (uint32_t *)Target;
899
900 // Extract the addend from the instruction.
901 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
902
903 Value.Addend += Addend;
904
905 // Look up for existing stub.
906 StubMap::const_iterator i = Stubs.find(Value);
907 if (i != Stubs.end()) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000908 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000909 (uint64_t)Section.Address + i->second, RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000910 DEBUG(dbgs() << " Stub function found\n");
911 } else {
912 // Create a new stub function.
913 DEBUG(dbgs() << " Create a new stub function\n");
914 Stubs[Value] = Section.StubOffset;
915 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
916 Section.StubOffset);
917
918 // Creating Hi and Lo relocations for the filled stub instructions.
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000919 RelocationEntry REHi(SectionID,
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000920 StubTargetAddr - Section.Address,
921 ELF::R_MIPS_HI16, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000922 RelocationEntry RELo(SectionID,
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000923 StubTargetAddr - Section.Address + 4,
924 ELF::R_MIPS_LO16, Value.Addend);
925
926 if (Value.SymbolName) {
927 addRelocationForSymbol(REHi, Value.SymbolName);
928 addRelocationForSymbol(RELo, Value.SymbolName);
929 } else {
930 addRelocationForSection(REHi, Value.SectionID);
931 addRelocationForSection(RELo, Value.SectionID);
932 }
933
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000934 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000935 (uint64_t)Section.Address + Section.StubOffset,
936 RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000937 Section.StubOffset += getMaxStubSize();
938 }
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000939 } else if (Arch == Triple::ppc64) {
940 if (RelType == ELF::R_PPC64_REL24) {
941 // A PPC branch relocation will need a stub function if the target is
942 // an external symbol (Symbol::ST_Unknown) or if the target address
943 // is not within the signed 24-bits branch address.
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000944 SectionEntry &Section = Sections[SectionID];
945 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000946 bool RangeOverflow = false;
947 if (SymType != SymbolRef::ST_Unknown) {
948 // A function call may points to the .opd entry, so the final symbol value
949 // in calculated based in the relocation values in .opd section.
950 findOPDEntrySection(Obj, ObjSectionToID, Value);
951 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
952 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
953 // If it is within 24-bits branch range, just set the branch target
954 if (SignExtend32<24>(delta) == delta) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000955 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000956 if (Value.SymbolName)
957 addRelocationForSymbol(RE, Value.SymbolName);
958 else
959 addRelocationForSection(RE, Value.SectionID);
960 } else {
961 RangeOverflow = true;
962 }
963 }
964 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
965 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
966 // larger than 24-bits.
967 StubMap::const_iterator i = Stubs.find(Value);
968 if (i != Stubs.end()) {
969 // Symbol function stub already created, just relocate to it
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000970 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000971 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000972 DEBUG(dbgs() << " Stub function found\n");
973 } else {
974 // Create a new stub function.
975 DEBUG(dbgs() << " Create a new stub function\n");
976 Stubs[Value] = Section.StubOffset;
977 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
978 Section.StubOffset);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000979 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000980 ELF::R_PPC64_ADDR64, Value.Addend);
981
982 // Generates the 64-bits address loads as exemplified in section
983 // 4.5.1 in PPC64 ELF ABI.
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000984 RelocationEntry REhst(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000985 StubTargetAddr - Section.Address + 2,
986 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000987 RelocationEntry REhr(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000988 StubTargetAddr - Section.Address + 6,
989 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000990 RelocationEntry REh(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000991 StubTargetAddr - Section.Address + 14,
992 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000993 RelocationEntry REl(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000994 StubTargetAddr - Section.Address + 18,
995 ELF::R_PPC64_ADDR16_LO, Value.Addend);
996
997 if (Value.SymbolName) {
998 addRelocationForSymbol(REhst, Value.SymbolName);
999 addRelocationForSymbol(REhr, Value.SymbolName);
1000 addRelocationForSymbol(REh, Value.SymbolName);
1001 addRelocationForSymbol(REl, Value.SymbolName);
1002 } else {
1003 addRelocationForSection(REhst, Value.SectionID);
1004 addRelocationForSection(REhr, Value.SectionID);
1005 addRelocationForSection(REh, Value.SectionID);
1006 addRelocationForSection(REl, Value.SectionID);
1007 }
1008
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001009 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +00001010 (uint64_t)Section.Address + Section.StubOffset,
1011 RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001012 if (SymType == SymbolRef::ST_Unknown)
1013 // Restore the TOC for external calls
1014 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
1015 Section.StubOffset += getMaxStubSize();
1016 }
1017 }
1018 } else {
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001019 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001020 // Extra check to avoid relocation againt empty symbols (usually
1021 // the R_PPC64_TOC).
1022 if (Value.SymbolName && !TargetName.empty())
1023 addRelocationForSymbol(RE, Value.SymbolName);
1024 else
1025 addRelocationForSection(RE, Value.SectionID);
1026 }
Richard Sandiford6fc2ad62013-05-03 14:15:35 +00001027 } else if (Arch == Triple::systemz &&
1028 (RelType == ELF::R_390_PLT32DBL ||
1029 RelType == ELF::R_390_GOTENT)) {
1030 // Create function stubs for both PLT and GOT references, regardless of
1031 // whether the GOT reference is to data or code. The stub contains the
1032 // full address of the symbol, as needed by GOT references, and the
1033 // executable part only adds an overhead of 8 bytes.
1034 //
1035 // We could try to conserve space by allocating the code and data
1036 // parts of the stub separately. However, as things stand, we allocate
1037 // a stub for every relocation, so using a GOT in JIT code should be
1038 // no less space efficient than using an explicit constant pool.
1039 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1040 SectionEntry &Section = Sections[SectionID];
1041
1042 // Look for an existing stub.
1043 StubMap::const_iterator i = Stubs.find(Value);
1044 uintptr_t StubAddress;
1045 if (i != Stubs.end()) {
1046 StubAddress = uintptr_t(Section.Address) + i->second;
1047 DEBUG(dbgs() << " Stub function found\n");
1048 } else {
1049 // Create a new stub function.
1050 DEBUG(dbgs() << " Create a new stub function\n");
1051
1052 uintptr_t BaseAddress = uintptr_t(Section.Address);
1053 uintptr_t StubAlignment = getStubAlignment();
1054 StubAddress = (BaseAddress + Section.StubOffset +
1055 StubAlignment - 1) & -StubAlignment;
1056 unsigned StubOffset = StubAddress - BaseAddress;
1057
1058 Stubs[Value] = StubOffset;
1059 createStubFunction((uint8_t *)StubAddress);
1060 RelocationEntry RE(SectionID, StubOffset + 8,
1061 ELF::R_390_64, Value.Addend - Addend);
1062 if (Value.SymbolName)
1063 addRelocationForSymbol(RE, Value.SymbolName);
1064 else
1065 addRelocationForSection(RE, Value.SectionID);
1066 Section.StubOffset = StubOffset + getMaxStubSize();
1067 }
1068
1069 if (RelType == ELF::R_390_GOTENT)
1070 resolveRelocation(Section, Offset, StubAddress + 8,
1071 ELF::R_390_PC32DBL, Addend);
1072 else
1073 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Eli Benderskyc201e6e2012-05-01 10:41:12 +00001074 } else {
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001075 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Eli Benderskyc201e6e2012-05-01 10:41:12 +00001076 if (Value.SymbolName)
1077 addRelocationForSymbol(RE, Value.SymbolName);
1078 else
1079 addRelocationForSection(RE, Value.SectionID);
1080 }
Jim Grosbach61425c02012-01-16 22:26:39 +00001081}
1082
Andrew Kaylor3f23cef2012-10-02 21:18:39 +00001083bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1084 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1085 return false;
1086 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
Eli Benderskya66a1852012-01-16 08:56:09 +00001087}
1088} // namespace llvm