blob: 80231a7c04091d4926a204093906e0b5ebda4a05 [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
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000154ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
155 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
156 llvm_unreachable("Unexpected ELF object size");
157 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
158 (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
159 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000160 error_code ec;
161
162 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000163 DyldELFObject<ELFType<support::little, 4, false> > *Obj =
164 new DyldELFObject<ELFType<support::little, 4, false> >(
165 Buffer->getMemBuffer(), ec);
166 return new ELFObjectImage<ELFType<support::little, 4, false> >(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000167 }
168 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000169 DyldELFObject<ELFType<support::big, 4, false> > *Obj =
170 new DyldELFObject<ELFType<support::big, 4, false> >(
171 Buffer->getMemBuffer(), ec);
172 return new ELFObjectImage<ELFType<support::big, 4, false> >(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000173 }
174 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000175 DyldELFObject<ELFType<support::big, 8, true> > *Obj =
176 new DyldELFObject<ELFType<support::big, 8, true> >(
177 Buffer->getMemBuffer(), ec);
178 return new ELFObjectImage<ELFType<support::big, 8, true> >(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000179 }
180 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
Michael J. Spencerac97f5c2013-01-15 07:44:25 +0000181 DyldELFObject<ELFType<support::little, 8, true> > *Obj =
182 new DyldELFObject<ELFType<support::little, 8, true> >(
183 Buffer->getMemBuffer(), ec);
184 return new ELFObjectImage<ELFType<support::little, 8, true> >(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000185 }
186 else
187 llvm_unreachable("Unexpected ELF format");
188}
189
Preston Gurd689ff9c2012-04-16 22:12:58 +0000190RuntimeDyldELF::~RuntimeDyldELF() {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000191}
Eli Benderskya66a1852012-01-16 08:56:09 +0000192
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000193void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
194 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000195 uint64_t Value,
196 uint32_t Type,
197 int64_t Addend) {
198 switch (Type) {
199 default:
200 llvm_unreachable("Relocation type not implemented yet!");
201 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000202 case ELF::R_X86_64_64: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000203 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000204 *Target = Value + Addend;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000205 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend))
206 << " at " << format("%p\n",Target));
Eli Benderskya66a1852012-01-16 08:56:09 +0000207 break;
208 }
209 case ELF::R_X86_64_32:
210 case ELF::R_X86_64_32S: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000211 Value += Addend;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000212 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000213 (Type == ELF::R_X86_64_32S &&
Andrew Kaylord83a5472012-07-27 20:30:12 +0000214 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Benderskya66a1852012-01-16 08:56:09 +0000215 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000216 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
Eli Benderskya66a1852012-01-16 08:56:09 +0000217 *Target = TruncatedAddr;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000218 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr)
219 << " at " << format("%p\n",Target));
Eli Benderskya66a1852012-01-16 08:56:09 +0000220 break;
221 }
222 case ELF::R_X86_64_PC32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000223 // Get the placeholder value from the generated object since
224 // a previous relocation attempt may have overwritten the loaded version
225 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
226 + Offset);
227 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
228 uint64_t FinalAddress = Section.LoadAddress + Offset;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000229 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000230 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000231 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000232 *Target = TruncOffset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000233 break;
234 }
235 }
236}
237
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000238void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
239 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000240 uint32_t Value,
241 uint32_t Type,
242 int32_t Addend) {
243 switch (Type) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000244 case ELF::R_386_32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000245 // Get the placeholder value from the generated object since
246 // a previous relocation attempt may have overwritten the loaded version
247 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
248 + Offset);
249 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
250 *Target = *Placeholder + Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000251 break;
252 }
253 case ELF::R_386_PC32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000254 // Get the placeholder value from the generated object since
255 // a previous relocation attempt may have overwritten the loaded version
256 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
257 + Offset);
258 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
259 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000260 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000261 *Target = RealOffset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000262 break;
263 }
264 default:
265 // There are other relocation types, but it appears these are the
Andrew Kaylore2e73bd2012-07-27 18:39:47 +0000266 // only ones currently used by the LLVM ELF object writer
Craig Topper85814382012-02-07 05:05:23 +0000267 llvm_unreachable("Relocation type not implemented yet!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000268 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000269 }
270}
271
Tim Northover85829bb2013-05-04 20:13:59 +0000272void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
273 uint64_t Offset,
274 uint64_t Value,
275 uint32_t Type,
276 int64_t Addend) {
277 uint32_t *TargetPtr = reinterpret_cast<uint32_t*>(Section.Address + Offset);
278 uint64_t FinalAddress = Section.LoadAddress + Offset;
279
280 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
281 << format("%llx", Section.Address + Offset)
282 << " FinalAddress: 0x" << format("%llx",FinalAddress)
283 << " Value: 0x" << format("%llx",Value)
284 << " Type: 0x" << format("%x",Type)
285 << " Addend: 0x" << format("%llx",Addend)
286 << "\n");
287
288 switch (Type) {
289 default:
290 llvm_unreachable("Relocation type not implemented yet!");
291 break;
Tim Northoverd52eaae2013-05-04 20:14:14 +0000292 case ELF::R_AARCH64_ABS64: {
293 uint64_t *TargetPtr = reinterpret_cast<uint64_t*>(Section.Address + Offset);
294 *TargetPtr = Value + Addend;
295 break;
296 }
Tim Northover85829bb2013-05-04 20:13:59 +0000297 case ELF::R_AARCH64_PREL32: { // test-shift.ll (.eh_frame)
298 uint64_t Result = Value + Addend - FinalAddress;
299 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
300 static_cast<int64_t>(Result) <= UINT32_MAX);
301 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
302 break;
303 }
Tim Northover4a9b6b72013-05-04 20:14:09 +0000304 case ELF::R_AARCH64_CALL26: // fallthrough
305 case ELF::R_AARCH64_JUMP26: {
306 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
307 // calculation.
308 uint64_t BranchImm = Value + Addend - FinalAddress;
309
310 // "Check that -2^27 <= result < 2^27".
311 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
312 static_cast<int64_t>(BranchImm) < (1LL << 27));
313 // Immediate goes in bits 25:0 of B and BL.
314 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
315 break;
316 }
Tim Northover654c2d62013-05-04 20:14:04 +0000317 case ELF::R_AARCH64_MOVW_UABS_G3: {
318 uint64_t Result = Value + Addend;
319 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
320 *TargetPtr |= Result >> (48 - 5);
321 // Shift is "lsl #48", in bits 22:21
322 *TargetPtr |= 3 << 21;
323 break;
324 }
325 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
326 uint64_t Result = Value + Addend;
327 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
328 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
329 // Shift is "lsl #32", in bits 22:21
330 *TargetPtr |= 2 << 21;
331 break;
332 }
333 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
334 uint64_t Result = Value + Addend;
335 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
336 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
337 // Shift is "lsl #16", in bits 22:21
338 *TargetPtr |= 1 << 21;
339 break;
340 }
341 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
342 uint64_t Result = Value + Addend;
343 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
344 *TargetPtr |= ((Result & 0xffffU) << 5);
345 // Shift is "lsl #0", in bits 22:21. No action needed.
346 break;
347 }
Tim Northover85829bb2013-05-04 20:13:59 +0000348 }
349}
350
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000351void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
352 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000353 uint32_t Value,
354 uint32_t Type,
355 int32_t Addend) {
356 // TODO: Add Thumb relocations.
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000357 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
358 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000359 Value += Addend;
360
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000361 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
362 << Section.Address + Offset
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000363 << " FinalAddress: " << format("%p",FinalAddress)
364 << " Value: " << format("%x",Value)
365 << " Type: " << format("%x",Type)
366 << " Addend: " << format("%x",Addend)
367 << "\n");
368
369 switch(Type) {
370 default:
371 llvm_unreachable("Not implemented relocation type!");
372
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000373 // Write a 32bit value to relocation address, taking into account the
Tim Northover565ebde2012-10-03 16:29:42 +0000374 // implicit addend encoded in the target.
Amara Emerson098d6d52012-11-16 11:11:59 +0000375 case ELF::R_ARM_TARGET1 :
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000376 case ELF::R_ARM_ABS32 :
Tim Northover565ebde2012-10-03 16:29:42 +0000377 *TargetPtr += Value;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000378 break;
379
380 // Write first 16 bit of 32 bit value to the mov instruction.
381 // Last 4 bit should be shifted.
382 case ELF::R_ARM_MOVW_ABS_NC :
Tim Northover565ebde2012-10-03 16:29:42 +0000383 // We are not expecting any other addend in the relocation address.
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000384 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover565ebde2012-10-03 16:29:42 +0000385 // non-contiguous fields.
386 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000387 Value = Value & 0xFFFF;
388 *TargetPtr |= Value & 0xFFF;
389 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
390 break;
391
392 // Write last 16 bit of 32 bit value to the mov instruction.
393 // Last 4 bit should be shifted.
394 case ELF::R_ARM_MOVT_ABS :
Tim Northover565ebde2012-10-03 16:29:42 +0000395 // We are not expecting any other addend in the relocation address.
396 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
397 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000398 Value = (Value >> 16) & 0xFFFF;
399 *TargetPtr |= Value & 0xFFF;
400 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
401 break;
402
403 // Write 24 bit relative value to the branch instruction.
404 case ELF::R_ARM_PC24 : // Fall through.
405 case ELF::R_ARM_CALL : // Fall through.
406 case ELF::R_ARM_JUMP24 :
407 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
408 RelValue = (RelValue & 0x03FFFFFC) >> 2;
409 *TargetPtr &= 0xFF000000;
410 *TargetPtr |= RelValue;
411 break;
412 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000413}
414
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000415void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
416 uint64_t Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000417 uint32_t Value,
418 uint32_t Type,
419 int32_t Addend) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000420 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000421 Value += Addend;
422
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000423 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
424 << Section.Address + Offset
425 << " FinalAddress: "
426 << format("%p",Section.LoadAddress + Offset)
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000427 << " Value: " << format("%x",Value)
428 << " Type: " << format("%x",Type)
429 << " Addend: " << format("%x",Addend)
430 << "\n");
431
432 switch(Type) {
433 default:
434 llvm_unreachable("Not implemented relocation type!");
435 break;
436 case ELF::R_MIPS_32:
437 *TargetPtr = Value + (*TargetPtr);
438 break;
439 case ELF::R_MIPS_26:
440 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
441 break;
442 case ELF::R_MIPS_HI16:
443 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
444 Value += ((*TargetPtr) & 0x0000ffff) << 16;
445 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
446 (((Value + 0x8000) >> 16) & 0xffff);
447 break;
448 case ELF::R_MIPS_LO16:
449 Value += ((*TargetPtr) & 0x0000ffff);
450 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
451 break;
452 }
453}
454
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000455// Return the .TOC. section address to R_PPC64_TOC relocations.
456uint64_t RuntimeDyldELF::findPPC64TOC() const {
457 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
458 // order. The TOC starts where the first of these sections starts.
459 SectionList::const_iterator it = Sections.begin();
460 SectionList::const_iterator ite = Sections.end();
461 for (; it != ite; ++it) {
462 if (it->Name == ".got" ||
463 it->Name == ".toc" ||
464 it->Name == ".tocbss" ||
465 it->Name == ".plt")
466 break;
467 }
468 if (it == ite) {
469 // This may happen for
470 // * references to TOC base base (sym@toc, .odp relocation) without
471 // a .toc directive.
472 // In this case just use the first section (which is usually
473 // the .odp) since the code won't reference the .toc base
474 // directly.
475 it = Sections.begin();
476 }
477 assert (it != ite);
478 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
479 // thus permitting a full 64 Kbytes segment.
480 return it->LoadAddress + 0x8000;
481}
482
483// Returns the sections and offset associated with the ODP entry referenced
484// by Symbol.
485void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
486 ObjSectionToIDMap &LocalSections,
487 RelocationValueRef &Rel) {
488 // Get the ELF symbol value (st_value) to compare with Relocation offset in
489 // .opd entries
490
491 error_code err;
492 for (section_iterator si = Obj.begin_sections(),
493 se = Obj.end_sections(); si != se; si.increment(err)) {
494 StringRef SectionName;
495 check(si->getName(SectionName));
496 if (SectionName != ".opd")
497 continue;
498
499 for (relocation_iterator i = si->begin_relocations(),
500 e = si->end_relocations(); i != e;) {
501 check(err);
502
503 // The R_PPC64_ADDR64 relocation indicates the first field
504 // of a .opd entry
505 uint64_t TypeFunc;
506 check(i->getType(TypeFunc));
507 if (TypeFunc != ELF::R_PPC64_ADDR64) {
508 i.increment(err);
509 continue;
510 }
511
512 SymbolRef TargetSymbol;
513 uint64_t TargetSymbolOffset;
514 int64_t TargetAdditionalInfo;
515 check(i->getSymbol(TargetSymbol));
516 check(i->getOffset(TargetSymbolOffset));
517 check(i->getAdditionalInfo(TargetAdditionalInfo));
518
519 i = i.increment(err);
520 if (i == e)
521 break;
522 check(err);
523
524 // Just check if following relocation is a R_PPC64_TOC
525 uint64_t TypeTOC;
526 check(i->getType(TypeTOC));
527 if (TypeTOC != ELF::R_PPC64_TOC)
528 continue;
529
530 // Finally compares the Symbol value and the target symbol offset
531 // to check if this .opd entry refers to the symbol the relocation
532 // points to.
533 if (Rel.Addend != (intptr_t)TargetSymbolOffset)
534 continue;
535
536 section_iterator tsi(Obj.end_sections());
537 check(TargetSymbol.getSection(tsi));
538 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
539 Rel.Addend = (intptr_t)TargetAdditionalInfo;
540 return;
541 }
542 }
543 llvm_unreachable("Attempting to get address of ODP entry!");
544}
545
546// Relocation masks following the #lo(value), #hi(value), #higher(value),
547// and #highest(value) macros defined in section 4.5.1. Relocation Types
548// in PPC-elf64abi document.
549//
550static inline
551uint16_t applyPPClo (uint64_t value)
552{
553 return value & 0xffff;
554}
555
556static inline
557uint16_t applyPPChi (uint64_t value)
558{
559 return (value >> 16) & 0xffff;
560}
561
562static inline
563uint16_t applyPPChigher (uint64_t value)
564{
565 return (value >> 32) & 0xffff;
566}
567
568static inline
569uint16_t applyPPChighest (uint64_t value)
570{
571 return (value >> 48) & 0xffff;
572}
573
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000574void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
575 uint64_t Offset,
576 uint64_t Value,
577 uint32_t Type,
578 int64_t Addend) {
579 uint8_t* LocalAddress = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000580 switch (Type) {
581 default:
582 llvm_unreachable("Relocation type not implemented yet!");
583 break;
584 case ELF::R_PPC64_ADDR16_LO :
585 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
586 break;
587 case ELF::R_PPC64_ADDR16_HI :
588 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
589 break;
590 case ELF::R_PPC64_ADDR16_HIGHER :
591 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
592 break;
593 case ELF::R_PPC64_ADDR16_HIGHEST :
594 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
595 break;
596 case ELF::R_PPC64_ADDR14 : {
597 assert(((Value + Addend) & 3) == 0);
598 // Preserve the AA/LK bits in the branch instruction
599 uint8_t aalk = *(LocalAddress+3);
600 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
601 } break;
Adhemerval Zanella7b449882013-01-04 19:08:13 +0000602 case ELF::R_PPC64_ADDR32 : {
603 int32_t Result = static_cast<int32_t>(Value + Addend);
604 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanellaa1db5de2013-01-09 17:08:15 +0000605 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella7b449882013-01-04 19:08:13 +0000606 writeInt32BE(LocalAddress, Result);
607 } break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000608 case ELF::R_PPC64_REL24 : {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000609 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000610 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
611 if (SignExtend32<24>(delta) != delta)
612 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
613 // Generates a 'bl <address>' instruction
614 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
615 } break;
Adhemerval Zanellaa1db5de2013-01-09 17:08:15 +0000616 case ELF::R_PPC64_REL32 : {
617 uint64_t FinalAddress = (Section.LoadAddress + Offset);
618 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
619 if (SignExtend32<32>(delta) != delta)
620 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
621 writeInt32BE(LocalAddress, delta);
622 } break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000623 case ELF::R_PPC64_ADDR64 :
624 writeInt64BE(LocalAddress, Value + Addend);
625 break;
626 case ELF::R_PPC64_TOC :
627 writeInt64BE(LocalAddress, findPPC64TOC());
628 break;
629 case ELF::R_PPC64_TOC16 : {
630 uint64_t TOCStart = findPPC64TOC();
631 Value = applyPPClo((Value + Addend) - TOCStart);
632 writeInt16BE(LocalAddress, applyPPClo(Value));
633 } break;
634 case ELF::R_PPC64_TOC16_DS : {
635 uint64_t TOCStart = findPPC64TOC();
636 Value = ((Value + Addend) - TOCStart);
637 writeInt16BE(LocalAddress, applyPPClo(Value));
638 } break;
639 }
640}
641
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000642void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
643 uint64_t Offset,
644 uint64_t Value,
645 uint32_t Type,
646 int64_t Addend) {
647 uint8_t *LocalAddress = Section.Address + Offset;
648 switch (Type) {
649 default:
650 llvm_unreachable("Relocation type not implemented yet!");
651 break;
652 case ELF::R_390_PC16DBL:
653 case ELF::R_390_PLT16DBL: {
654 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
655 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
656 writeInt16BE(LocalAddress, Delta / 2);
657 break;
658 }
659 case ELF::R_390_PC32DBL:
660 case ELF::R_390_PLT32DBL: {
661 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
662 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
663 writeInt32BE(LocalAddress, Delta / 2);
664 break;
665 }
666 case ELF::R_390_PC32: {
667 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
668 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
669 writeInt32BE(LocalAddress, Delta);
670 break;
671 }
672 case ELF::R_390_64:
673 writeInt64BE(LocalAddress, Value + Addend);
674 break;
675 }
676}
677
Rafael Espindola87b50172013-04-29 17:24:34 +0000678void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
679 uint64_t Value) {
680 const SectionEntry &Section = Sections[RE.SectionID];
681 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend);
682}
683
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000684void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
685 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000686 uint64_t Value,
687 uint32_t Type,
688 int64_t Addend) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000689 switch (Arch) {
690 case Triple::x86_64:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000691 resolveX86_64Relocation(Section, Offset, Value, Type, Addend);
Eli Benderskya66a1852012-01-16 08:56:09 +0000692 break;
693 case Triple::x86:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000694 resolveX86Relocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000695 (uint32_t)(Value & 0xffffffffL), Type,
696 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000697 break;
Tim Northover85829bb2013-05-04 20:13:59 +0000698 case Triple::aarch64:
699 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
700 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000701 case Triple::arm: // Fall through.
702 case Triple::thumb:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000703 resolveARMRelocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000704 (uint32_t)(Value & 0xffffffffL), Type,
705 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000706 break;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000707 case Triple::mips: // Fall through.
708 case Triple::mipsel:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000709 resolveMIPSRelocation(Section, Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000710 (uint32_t)(Value & 0xffffffffL), Type,
711 (uint32_t)(Addend & 0xffffffffL));
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000712 break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000713 case Triple::ppc64:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000714 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000715 break;
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000716 case Triple::systemz:
717 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
718 break;
Craig Topper85814382012-02-07 05:05:23 +0000719 default: llvm_unreachable("Unsupported CPU type!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000720 }
721}
722
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000723void RuntimeDyldELF::processRelocationRef(unsigned SectionID,
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000724 RelocationRef RelI,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000725 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000726 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000727 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000728 StubMap &Stubs) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000729 uint64_t RelType;
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000730 Check(RelI.getType(RelType));
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000731 int64_t Addend;
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000732 Check(RelI.getAdditionalInfo(Addend));
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000733 SymbolRef Symbol;
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000734 Check(RelI.getSymbol(Symbol));
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000735
736 // Obtain the symbol name which is referenced in the relocation
737 StringRef TargetName;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000738 Symbol.getName(TargetName);
739 DEBUG(dbgs() << "\t\tRelType: " << RelType
740 << " Addend: " << Addend
741 << " TargetName: " << TargetName
742 << "\n");
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000743 RelocationValueRef Value;
744 // First search for the symbol in the local symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000745 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000746 SymbolRef::Type SymType;
747 Symbol.getType(SymType);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000748 if (lsi != Symbols.end()) {
749 Value.SectionID = lsi->second.first;
Ulrich Weigand03f018a2013-04-05 13:29:04 +0000750 Value.Addend = lsi->second.second + Addend;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000751 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000752 // Search for the symbol in the global symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000753 SymbolTableMap::const_iterator gsi =
754 GlobalSymbolTable.find(TargetName.data());
755 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000756 Value.SectionID = gsi->second.first;
Ulrich Weigand03f018a2013-04-05 13:29:04 +0000757 Value.Addend = gsi->second.second + Addend;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000758 } else {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000759 switch (SymType) {
760 case SymbolRef::ST_Debug: {
761 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
762 // and can be changed by another developers. Maybe best way is add
763 // a new symbol type ST_Section to SymbolRef and use it.
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000764 section_iterator si(Obj.end_sections());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000765 Symbol.getSection(si);
766 if (si == Obj.end_sections())
767 llvm_unreachable("Symbol section not found, bad object file format!");
768 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000769 // Default to 'true' in case isText fails (though it never does).
770 bool isCode = true;
771 si->isText(isCode);
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000772 Value.SectionID = findOrEmitSection(Obj,
773 (*si),
774 isCode,
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000775 ObjSectionToID);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000776 Value.Addend = Addend;
777 break;
778 }
779 case SymbolRef::ST_Unknown: {
780 Value.SymbolName = TargetName.data();
781 Value.Addend = Addend;
782 break;
783 }
784 default:
785 llvm_unreachable("Unresolved symbol type!");
786 break;
787 }
788 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000789 }
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000790 uint64_t Offset;
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000791 Check(RelI.getOffset(Offset));
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000792
793 DEBUG(dbgs() << "\t\tSectionID: " << SectionID
794 << " Offset: " << Offset
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000795 << "\n");
Tim Northover4a9b6b72013-05-04 20:14:09 +0000796 if (Arch == Triple::aarch64 &&
797 (RelType == ELF::R_AARCH64_CALL26 ||
798 RelType == ELF::R_AARCH64_JUMP26)) {
799 // This is an AArch64 branch relocation, need to use a stub function.
800 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
801 SectionEntry &Section = Sections[SectionID];
802
803 // Look for an existing stub.
804 StubMap::const_iterator i = Stubs.find(Value);
805 if (i != Stubs.end()) {
806 resolveRelocation(Section, Offset,
807 (uint64_t)Section.Address + i->second, RelType, 0);
808 DEBUG(dbgs() << " Stub function found\n");
809 } else {
810 // Create a new stub function.
811 DEBUG(dbgs() << " Create a new stub function\n");
812 Stubs[Value] = Section.StubOffset;
813 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
814 Section.StubOffset);
815
816 RelocationEntry REmovz_g3(SectionID,
817 StubTargetAddr - Section.Address,
818 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
819 RelocationEntry REmovk_g2(SectionID,
820 StubTargetAddr - Section.Address + 4,
821 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
822 RelocationEntry REmovk_g1(SectionID,
823 StubTargetAddr - Section.Address + 8,
824 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
825 RelocationEntry REmovk_g0(SectionID,
826 StubTargetAddr - Section.Address + 12,
827 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
828
829 if (Value.SymbolName) {
830 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
831 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
832 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
833 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
834 } else {
835 addRelocationForSection(REmovz_g3, Value.SectionID);
836 addRelocationForSection(REmovk_g2, Value.SectionID);
837 addRelocationForSection(REmovk_g1, Value.SectionID);
838 addRelocationForSection(REmovk_g0, Value.SectionID);
839 }
840 resolveRelocation(Section, Offset,
841 (uint64_t)Section.Address + Section.StubOffset,
842 RelType, 0);
843 Section.StubOffset += getMaxStubSize();
844 }
845 } else if (Arch == Triple::arm &&
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000846 (RelType == ELF::R_ARM_PC24 ||
847 RelType == ELF::R_ARM_CALL ||
848 RelType == ELF::R_ARM_JUMP24)) {
849 // This is an ARM branch relocation, need to use a stub function.
850 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000851 SectionEntry &Section = Sections[SectionID];
Eli Benderskya66a1852012-01-16 08:56:09 +0000852
Eric Christopherbf261f12012-10-23 17:19:15 +0000853 // Look for an existing stub.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000854 StubMap::const_iterator i = Stubs.find(Value);
855 if (i != Stubs.end()) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000856 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000857 (uint64_t)Section.Address + i->second, RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000858 DEBUG(dbgs() << " Stub function found\n");
859 } else {
860 // Create a new stub function.
861 DEBUG(dbgs() << " Create a new stub function\n");
862 Stubs[Value] = Section.StubOffset;
863 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
864 Section.StubOffset);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000865 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000866 ELF::R_ARM_ABS32, Value.Addend);
867 if (Value.SymbolName)
868 addRelocationForSymbol(RE, Value.SymbolName);
869 else
870 addRelocationForSection(RE, Value.SectionID);
871
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000872 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000873 (uint64_t)Section.Address + Section.StubOffset,
874 RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000875 Section.StubOffset += getMaxStubSize();
876 }
Akira Hatanakaade04742012-12-03 23:12:19 +0000877 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
878 RelType == ELF::R_MIPS_26) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000879 // This is an Mips branch relocation, need to use a stub function.
880 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000881 SectionEntry &Section = Sections[SectionID];
882 uint8_t *Target = Section.Address + Offset;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000883 uint32_t *TargetAddress = (uint32_t *)Target;
884
885 // Extract the addend from the instruction.
886 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
887
888 Value.Addend += Addend;
889
890 // Look up for existing stub.
891 StubMap::const_iterator i = Stubs.find(Value);
892 if (i != Stubs.end()) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000893 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000894 (uint64_t)Section.Address + i->second, RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000895 DEBUG(dbgs() << " Stub function found\n");
896 } else {
897 // Create a new stub function.
898 DEBUG(dbgs() << " Create a new stub function\n");
899 Stubs[Value] = Section.StubOffset;
900 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
901 Section.StubOffset);
902
903 // Creating Hi and Lo relocations for the filled stub instructions.
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000904 RelocationEntry REHi(SectionID,
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000905 StubTargetAddr - Section.Address,
906 ELF::R_MIPS_HI16, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000907 RelocationEntry RELo(SectionID,
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000908 StubTargetAddr - Section.Address + 4,
909 ELF::R_MIPS_LO16, Value.Addend);
910
911 if (Value.SymbolName) {
912 addRelocationForSymbol(REHi, Value.SymbolName);
913 addRelocationForSymbol(RELo, Value.SymbolName);
914 } else {
915 addRelocationForSection(REHi, Value.SectionID);
916 addRelocationForSection(RELo, Value.SectionID);
917 }
918
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000919 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000920 (uint64_t)Section.Address + Section.StubOffset,
921 RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000922 Section.StubOffset += getMaxStubSize();
923 }
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000924 } else if (Arch == Triple::ppc64) {
925 if (RelType == ELF::R_PPC64_REL24) {
926 // A PPC branch relocation will need a stub function if the target is
927 // an external symbol (Symbol::ST_Unknown) or if the target address
928 // is not within the signed 24-bits branch address.
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000929 SectionEntry &Section = Sections[SectionID];
930 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000931 bool RangeOverflow = false;
932 if (SymType != SymbolRef::ST_Unknown) {
933 // A function call may points to the .opd entry, so the final symbol value
934 // in calculated based in the relocation values in .opd section.
935 findOPDEntrySection(Obj, ObjSectionToID, Value);
936 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
937 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
938 // If it is within 24-bits branch range, just set the branch target
939 if (SignExtend32<24>(delta) == delta) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000940 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000941 if (Value.SymbolName)
942 addRelocationForSymbol(RE, Value.SymbolName);
943 else
944 addRelocationForSection(RE, Value.SectionID);
945 } else {
946 RangeOverflow = true;
947 }
948 }
949 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
950 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
951 // larger than 24-bits.
952 StubMap::const_iterator i = Stubs.find(Value);
953 if (i != Stubs.end()) {
954 // Symbol function stub already created, just relocate to it
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000955 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000956 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000957 DEBUG(dbgs() << " Stub function found\n");
958 } else {
959 // Create a new stub function.
960 DEBUG(dbgs() << " Create a new stub function\n");
961 Stubs[Value] = Section.StubOffset;
962 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
963 Section.StubOffset);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000964 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000965 ELF::R_PPC64_ADDR64, Value.Addend);
966
967 // Generates the 64-bits address loads as exemplified in section
968 // 4.5.1 in PPC64 ELF ABI.
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000969 RelocationEntry REhst(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000970 StubTargetAddr - Section.Address + 2,
971 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000972 RelocationEntry REhr(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000973 StubTargetAddr - Section.Address + 6,
974 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000975 RelocationEntry REh(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000976 StubTargetAddr - Section.Address + 14,
977 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000978 RelocationEntry REl(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000979 StubTargetAddr - Section.Address + 18,
980 ELF::R_PPC64_ADDR16_LO, Value.Addend);
981
982 if (Value.SymbolName) {
983 addRelocationForSymbol(REhst, Value.SymbolName);
984 addRelocationForSymbol(REhr, Value.SymbolName);
985 addRelocationForSymbol(REh, Value.SymbolName);
986 addRelocationForSymbol(REl, Value.SymbolName);
987 } else {
988 addRelocationForSection(REhst, Value.SectionID);
989 addRelocationForSection(REhr, Value.SectionID);
990 addRelocationForSection(REh, Value.SectionID);
991 addRelocationForSection(REl, Value.SectionID);
992 }
993
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000994 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000995 (uint64_t)Section.Address + Section.StubOffset,
996 RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000997 if (SymType == SymbolRef::ST_Unknown)
998 // Restore the TOC for external calls
999 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
1000 Section.StubOffset += getMaxStubSize();
1001 }
1002 }
1003 } else {
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001004 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001005 // Extra check to avoid relocation againt empty symbols (usually
1006 // the R_PPC64_TOC).
1007 if (Value.SymbolName && !TargetName.empty())
1008 addRelocationForSymbol(RE, Value.SymbolName);
1009 else
1010 addRelocationForSection(RE, Value.SectionID);
1011 }
Richard Sandiford6fc2ad62013-05-03 14:15:35 +00001012 } else if (Arch == Triple::systemz &&
1013 (RelType == ELF::R_390_PLT32DBL ||
1014 RelType == ELF::R_390_GOTENT)) {
1015 // Create function stubs for both PLT and GOT references, regardless of
1016 // whether the GOT reference is to data or code. The stub contains the
1017 // full address of the symbol, as needed by GOT references, and the
1018 // executable part only adds an overhead of 8 bytes.
1019 //
1020 // We could try to conserve space by allocating the code and data
1021 // parts of the stub separately. However, as things stand, we allocate
1022 // a stub for every relocation, so using a GOT in JIT code should be
1023 // no less space efficient than using an explicit constant pool.
1024 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1025 SectionEntry &Section = Sections[SectionID];
1026
1027 // Look for an existing stub.
1028 StubMap::const_iterator i = Stubs.find(Value);
1029 uintptr_t StubAddress;
1030 if (i != Stubs.end()) {
1031 StubAddress = uintptr_t(Section.Address) + i->second;
1032 DEBUG(dbgs() << " Stub function found\n");
1033 } else {
1034 // Create a new stub function.
1035 DEBUG(dbgs() << " Create a new stub function\n");
1036
1037 uintptr_t BaseAddress = uintptr_t(Section.Address);
1038 uintptr_t StubAlignment = getStubAlignment();
1039 StubAddress = (BaseAddress + Section.StubOffset +
1040 StubAlignment - 1) & -StubAlignment;
1041 unsigned StubOffset = StubAddress - BaseAddress;
1042
1043 Stubs[Value] = StubOffset;
1044 createStubFunction((uint8_t *)StubAddress);
1045 RelocationEntry RE(SectionID, StubOffset + 8,
1046 ELF::R_390_64, Value.Addend - Addend);
1047 if (Value.SymbolName)
1048 addRelocationForSymbol(RE, Value.SymbolName);
1049 else
1050 addRelocationForSection(RE, Value.SectionID);
1051 Section.StubOffset = StubOffset + getMaxStubSize();
1052 }
1053
1054 if (RelType == ELF::R_390_GOTENT)
1055 resolveRelocation(Section, Offset, StubAddress + 8,
1056 ELF::R_390_PC32DBL, Addend);
1057 else
1058 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Eli Benderskyc201e6e2012-05-01 10:41:12 +00001059 } else {
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001060 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Eli Benderskyc201e6e2012-05-01 10:41:12 +00001061 if (Value.SymbolName)
1062 addRelocationForSymbol(RE, Value.SymbolName);
1063 else
1064 addRelocationForSection(RE, Value.SectionID);
1065 }
Jim Grosbach61425c02012-01-16 22:26:39 +00001066}
1067
Andrew Kaylor3f23cef2012-10-02 21:18:39 +00001068bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1069 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1070 return false;
1071 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
Eli Benderskya66a1852012-01-16 08:56:09 +00001072}
1073} // namespace llvm