blob: cb3b5129f23fc15cc51860b0c248dae46fe73f34 [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
Tim Northover85829bb2013-05-04 20:13:59 +0000280void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
281 uint64_t Offset,
282 uint64_t Value,
283 uint32_t Type,
284 int64_t Addend) {
285 uint32_t *TargetPtr = reinterpret_cast<uint32_t*>(Section.Address + Offset);
286 uint64_t FinalAddress = Section.LoadAddress + Offset;
287
288 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
289 << format("%llx", Section.Address + Offset)
290 << " FinalAddress: 0x" << format("%llx",FinalAddress)
291 << " Value: 0x" << format("%llx",Value)
292 << " Type: 0x" << format("%x",Type)
293 << " Addend: 0x" << format("%llx",Addend)
294 << "\n");
295
296 switch (Type) {
297 default:
298 llvm_unreachable("Relocation type not implemented yet!");
299 break;
Tim Northoverd52eaae2013-05-04 20:14:14 +0000300 case ELF::R_AARCH64_ABS64: {
301 uint64_t *TargetPtr = reinterpret_cast<uint64_t*>(Section.Address + Offset);
302 *TargetPtr = Value + Addend;
303 break;
304 }
Tim Northover675b9e92013-05-19 15:39:03 +0000305 case ELF::R_AARCH64_PREL32: {
Tim Northover85829bb2013-05-04 20:13:59 +0000306 uint64_t Result = Value + Addend - FinalAddress;
307 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
308 static_cast<int64_t>(Result) <= UINT32_MAX);
309 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
310 break;
311 }
Tim Northover4a9b6b72013-05-04 20:14:09 +0000312 case ELF::R_AARCH64_CALL26: // fallthrough
313 case ELF::R_AARCH64_JUMP26: {
314 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
315 // calculation.
316 uint64_t BranchImm = Value + Addend - FinalAddress;
317
318 // "Check that -2^27 <= result < 2^27".
319 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
320 static_cast<int64_t>(BranchImm) < (1LL << 27));
Tim Northover675b9e92013-05-19 15:39:03 +0000321
322 // AArch64 code is emitted with .rela relocations. The data already in any
323 // bits affected by the relocation on entry is garbage.
324 *TargetPtr &= 0xfc000000U;
Tim Northover4a9b6b72013-05-04 20:14:09 +0000325 // Immediate goes in bits 25:0 of B and BL.
326 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
327 break;
328 }
Tim Northover654c2d62013-05-04 20:14:04 +0000329 case ELF::R_AARCH64_MOVW_UABS_G3: {
330 uint64_t Result = Value + Addend;
Tim Northover675b9e92013-05-19 15:39:03 +0000331
332 // AArch64 code is emitted with .rela relocations. The data already in any
333 // bits affected by the relocation on entry is garbage.
334 *TargetPtr &= 0xff80001fU;
Tim Northover654c2d62013-05-04 20:14:04 +0000335 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
336 *TargetPtr |= Result >> (48 - 5);
Tim Northover6711fc22013-07-01 19:23:10 +0000337 // Shift must be "lsl #48", in bits 22:21
338 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
Tim Northover654c2d62013-05-04 20:14:04 +0000339 break;
340 }
341 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
342 uint64_t Result = Value + Addend;
Tim Northover675b9e92013-05-19 15:39:03 +0000343
344
345 // AArch64 code is emitted with .rela relocations. The data already in any
346 // bits affected by the relocation on entry is garbage.
347 *TargetPtr &= 0xff80001fU;
Tim Northover654c2d62013-05-04 20:14:04 +0000348 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
349 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
Tim Northover6711fc22013-07-01 19:23:10 +0000350 // Shift must be "lsl #32", in bits 22:21
351 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
Tim Northover654c2d62013-05-04 20:14:04 +0000352 break;
353 }
354 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
355 uint64_t Result = Value + Addend;
Tim Northover675b9e92013-05-19 15:39:03 +0000356
357 // AArch64 code is emitted with .rela relocations. The data already in any
358 // bits affected by the relocation on entry is garbage.
359 *TargetPtr &= 0xff80001fU;
Tim Northover654c2d62013-05-04 20:14:04 +0000360 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
361 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
Tim Northover6711fc22013-07-01 19:23:10 +0000362 // Shift must be "lsl #16", in bits 22:2
363 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
Tim Northover654c2d62013-05-04 20:14:04 +0000364 break;
365 }
366 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
367 uint64_t Result = Value + Addend;
Tim Northover675b9e92013-05-19 15:39:03 +0000368
369 // AArch64 code is emitted with .rela relocations. The data already in any
370 // bits affected by the relocation on entry is garbage.
371 *TargetPtr &= 0xff80001fU;
Tim Northover654c2d62013-05-04 20:14:04 +0000372 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
373 *TargetPtr |= ((Result & 0xffffU) << 5);
Tim Northover6711fc22013-07-01 19:23:10 +0000374 // Shift must be "lsl #0", in bits 22:21.
375 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
Tim Northover654c2d62013-05-04 20:14:04 +0000376 break;
377 }
Tim Northover85829bb2013-05-04 20:13:59 +0000378 }
379}
380
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000381void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
382 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000383 uint32_t Value,
384 uint32_t Type,
385 int32_t Addend) {
386 // TODO: Add Thumb relocations.
Tim Northovere274b472013-05-28 19:48:19 +0000387 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress +
388 Offset);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000389 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
390 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000391 Value += Addend;
392
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000393 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
394 << Section.Address + Offset
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000395 << " FinalAddress: " << format("%p",FinalAddress)
396 << " Value: " << format("%x",Value)
397 << " Type: " << format("%x",Type)
398 << " Addend: " << format("%x",Addend)
399 << "\n");
400
401 switch(Type) {
402 default:
403 llvm_unreachable("Not implemented relocation type!");
404
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000405 // Write a 32bit value to relocation address, taking into account the
Tim Northover565ebde2012-10-03 16:29:42 +0000406 // implicit addend encoded in the target.
Tim Northovere274b472013-05-28 19:48:19 +0000407 case ELF::R_ARM_TARGET1:
408 case ELF::R_ARM_ABS32:
409 *TargetPtr = *Placeholder + Value;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000410 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000411 // Write first 16 bit of 32 bit value to the mov instruction.
412 // Last 4 bit should be shifted.
Tim Northovere274b472013-05-28 19:48:19 +0000413 case ELF::R_ARM_MOVW_ABS_NC:
Tim Northover565ebde2012-10-03 16:29:42 +0000414 // We are not expecting any other addend in the relocation address.
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000415 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover565ebde2012-10-03 16:29:42 +0000416 // non-contiguous fields.
Tim Northovere274b472013-05-28 19:48:19 +0000417 assert((*Placeholder & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000418 Value = Value & 0xFFFF;
Tim Northovere274b472013-05-28 19:48:19 +0000419 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000420 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
421 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000422 // Write last 16 bit of 32 bit value to the mov instruction.
423 // Last 4 bit should be shifted.
Tim Northovere274b472013-05-28 19:48:19 +0000424 case ELF::R_ARM_MOVT_ABS:
Tim Northover565ebde2012-10-03 16:29:42 +0000425 // We are not expecting any other addend in the relocation address.
426 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
Tim Northovere274b472013-05-28 19:48:19 +0000427 assert((*Placeholder & 0x000F0FFF) == 0);
428
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000429 Value = (Value >> 16) & 0xFFFF;
Tim Northovere274b472013-05-28 19:48:19 +0000430 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000431 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
432 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000433 // Write 24 bit relative value to the branch instruction.
434 case ELF::R_ARM_PC24 : // Fall through.
435 case ELF::R_ARM_CALL : // Fall through.
Tim Northovere274b472013-05-28 19:48:19 +0000436 case ELF::R_ARM_JUMP24: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000437 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
438 RelValue = (RelValue & 0x03FFFFFC) >> 2;
Tim Northovere274b472013-05-28 19:48:19 +0000439 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000440 *TargetPtr &= 0xFF000000;
441 *TargetPtr |= RelValue;
442 break;
443 }
Tim Northovere274b472013-05-28 19:48:19 +0000444 case ELF::R_ARM_PRIVATE_0:
445 // This relocation is reserved by the ARM ELF ABI for internal use. We
446 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
447 // in the stubs created during JIT (which can't put an addend into the
448 // original object file).
449 *TargetPtr = Value;
450 break;
451 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000452}
453
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000454void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
455 uint64_t Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000456 uint32_t Value,
457 uint32_t Type,
458 int32_t Addend) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000459 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000460 Value += Addend;
461
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000462 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
463 << Section.Address + Offset
464 << " FinalAddress: "
465 << format("%p",Section.LoadAddress + Offset)
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000466 << " Value: " << format("%x",Value)
467 << " Type: " << format("%x",Type)
468 << " Addend: " << format("%x",Addend)
469 << "\n");
470
471 switch(Type) {
472 default:
473 llvm_unreachable("Not implemented relocation type!");
474 break;
475 case ELF::R_MIPS_32:
476 *TargetPtr = Value + (*TargetPtr);
477 break;
478 case ELF::R_MIPS_26:
479 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
480 break;
481 case ELF::R_MIPS_HI16:
482 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
483 Value += ((*TargetPtr) & 0x0000ffff) << 16;
484 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
485 (((Value + 0x8000) >> 16) & 0xffff);
486 break;
487 case ELF::R_MIPS_LO16:
488 Value += ((*TargetPtr) & 0x0000ffff);
489 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
490 break;
491 }
492}
493
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000494// Return the .TOC. section address to R_PPC64_TOC relocations.
495uint64_t RuntimeDyldELF::findPPC64TOC() const {
496 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
497 // order. The TOC starts where the first of these sections starts.
498 SectionList::const_iterator it = Sections.begin();
499 SectionList::const_iterator ite = Sections.end();
500 for (; it != ite; ++it) {
501 if (it->Name == ".got" ||
502 it->Name == ".toc" ||
503 it->Name == ".tocbss" ||
504 it->Name == ".plt")
505 break;
506 }
507 if (it == ite) {
508 // This may happen for
509 // * references to TOC base base (sym@toc, .odp relocation) without
510 // a .toc directive.
511 // In this case just use the first section (which is usually
512 // the .odp) since the code won't reference the .toc base
513 // directly.
514 it = Sections.begin();
515 }
516 assert (it != ite);
517 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
518 // thus permitting a full 64 Kbytes segment.
519 return it->LoadAddress + 0x8000;
520}
521
522// Returns the sections and offset associated with the ODP entry referenced
523// by Symbol.
524void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
525 ObjSectionToIDMap &LocalSections,
526 RelocationValueRef &Rel) {
527 // Get the ELF symbol value (st_value) to compare with Relocation offset in
528 // .opd entries
529
530 error_code err;
531 for (section_iterator si = Obj.begin_sections(),
532 se = Obj.end_sections(); si != se; si.increment(err)) {
Rafael Espindola15e5c462013-06-03 19:37:34 +0000533 section_iterator RelSecI = si->getRelocatedSection();
534 if (RelSecI == Obj.end_sections())
535 continue;
536
537 StringRef RelSectionName;
538 check(RelSecI->getName(RelSectionName));
539 if (RelSectionName != ".opd")
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000540 continue;
541
542 for (relocation_iterator i = si->begin_relocations(),
543 e = si->end_relocations(); i != e;) {
544 check(err);
545
546 // The R_PPC64_ADDR64 relocation indicates the first field
547 // of a .opd entry
548 uint64_t TypeFunc;
549 check(i->getType(TypeFunc));
550 if (TypeFunc != ELF::R_PPC64_ADDR64) {
551 i.increment(err);
552 continue;
553 }
554
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000555 uint64_t TargetSymbolOffset;
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000556 symbol_iterator TargetSymbol = i->getSymbol();
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000557 check(i->getOffset(TargetSymbolOffset));
Rafael Espindola167957f2013-05-09 03:39:05 +0000558 int64_t Addend;
559 check(getELFRelocationAddend(*i, Addend));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000560
561 i = i.increment(err);
562 if (i == e)
563 break;
564 check(err);
565
566 // Just check if following relocation is a R_PPC64_TOC
567 uint64_t TypeTOC;
568 check(i->getType(TypeTOC));
569 if (TypeTOC != ELF::R_PPC64_TOC)
570 continue;
571
572 // Finally compares the Symbol value and the target symbol offset
573 // to check if this .opd entry refers to the symbol the relocation
574 // points to.
575 if (Rel.Addend != (intptr_t)TargetSymbolOffset)
576 continue;
577
578 section_iterator tsi(Obj.end_sections());
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000579 check(TargetSymbol->getSection(tsi));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000580 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
Rafael Espindola167957f2013-05-09 03:39:05 +0000581 Rel.Addend = (intptr_t)Addend;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000582 return;
583 }
584 }
585 llvm_unreachable("Attempting to get address of ODP entry!");
586}
587
588// Relocation masks following the #lo(value), #hi(value), #higher(value),
589// and #highest(value) macros defined in section 4.5.1. Relocation Types
590// in PPC-elf64abi document.
591//
592static inline
593uint16_t applyPPClo (uint64_t value)
594{
595 return value & 0xffff;
596}
597
598static inline
599uint16_t applyPPChi (uint64_t value)
600{
601 return (value >> 16) & 0xffff;
602}
603
604static inline
605uint16_t applyPPChigher (uint64_t value)
606{
607 return (value >> 32) & 0xffff;
608}
609
610static inline
611uint16_t applyPPChighest (uint64_t value)
612{
613 return (value >> 48) & 0xffff;
614}
615
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000616void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
617 uint64_t Offset,
618 uint64_t Value,
619 uint32_t Type,
620 int64_t Addend) {
621 uint8_t* LocalAddress = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000622 switch (Type) {
623 default:
624 llvm_unreachable("Relocation type not implemented yet!");
625 break;
626 case ELF::R_PPC64_ADDR16_LO :
627 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
628 break;
629 case ELF::R_PPC64_ADDR16_HI :
630 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
631 break;
632 case ELF::R_PPC64_ADDR16_HIGHER :
633 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
634 break;
635 case ELF::R_PPC64_ADDR16_HIGHEST :
636 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
637 break;
638 case ELF::R_PPC64_ADDR14 : {
639 assert(((Value + Addend) & 3) == 0);
640 // Preserve the AA/LK bits in the branch instruction
641 uint8_t aalk = *(LocalAddress+3);
642 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
643 } break;
Adhemerval Zanella7b449882013-01-04 19:08:13 +0000644 case ELF::R_PPC64_ADDR32 : {
645 int32_t Result = static_cast<int32_t>(Value + Addend);
646 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanellaa1db5de2013-01-09 17:08:15 +0000647 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella7b449882013-01-04 19:08:13 +0000648 writeInt32BE(LocalAddress, Result);
649 } break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000650 case ELF::R_PPC64_REL24 : {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000651 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000652 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
653 if (SignExtend32<24>(delta) != delta)
654 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
655 // Generates a 'bl <address>' instruction
656 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
657 } break;
Adhemerval Zanellaa1db5de2013-01-09 17:08:15 +0000658 case ELF::R_PPC64_REL32 : {
659 uint64_t FinalAddress = (Section.LoadAddress + Offset);
660 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
661 if (SignExtend32<32>(delta) != delta)
662 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
663 writeInt32BE(LocalAddress, delta);
664 } break;
Adhemerval Zanellaf51d7e72013-05-06 17:21:23 +0000665 case ELF::R_PPC64_REL64: {
666 uint64_t FinalAddress = (Section.LoadAddress + Offset);
667 uint64_t Delta = Value - FinalAddress + Addend;
668 writeInt64BE(LocalAddress, Delta);
669 } break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000670 case ELF::R_PPC64_ADDR64 :
671 writeInt64BE(LocalAddress, Value + Addend);
672 break;
673 case ELF::R_PPC64_TOC :
674 writeInt64BE(LocalAddress, findPPC64TOC());
675 break;
676 case ELF::R_PPC64_TOC16 : {
677 uint64_t TOCStart = findPPC64TOC();
678 Value = applyPPClo((Value + Addend) - TOCStart);
679 writeInt16BE(LocalAddress, applyPPClo(Value));
680 } break;
681 case ELF::R_PPC64_TOC16_DS : {
682 uint64_t TOCStart = findPPC64TOC();
683 Value = ((Value + Addend) - TOCStart);
684 writeInt16BE(LocalAddress, applyPPClo(Value));
685 } break;
686 }
687}
688
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000689void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
690 uint64_t Offset,
691 uint64_t Value,
692 uint32_t Type,
693 int64_t Addend) {
694 uint8_t *LocalAddress = Section.Address + Offset;
695 switch (Type) {
696 default:
697 llvm_unreachable("Relocation type not implemented yet!");
698 break;
699 case ELF::R_390_PC16DBL:
700 case ELF::R_390_PLT16DBL: {
701 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
702 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
703 writeInt16BE(LocalAddress, Delta / 2);
704 break;
705 }
706 case ELF::R_390_PC32DBL:
707 case ELF::R_390_PLT32DBL: {
708 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
709 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
710 writeInt32BE(LocalAddress, Delta / 2);
711 break;
712 }
713 case ELF::R_390_PC32: {
714 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
715 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
716 writeInt32BE(LocalAddress, Delta);
717 break;
718 }
719 case ELF::R_390_64:
720 writeInt64BE(LocalAddress, Value + Addend);
721 break;
722 }
723}
724
Rafael Espindola87b50172013-04-29 17:24:34 +0000725void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
726 uint64_t Value) {
727 const SectionEntry &Section = Sections[RE.SectionID];
728 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend);
729}
730
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000731void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
732 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000733 uint64_t Value,
734 uint32_t Type,
735 int64_t Addend) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000736 switch (Arch) {
737 case Triple::x86_64:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000738 resolveX86_64Relocation(Section, Offset, Value, Type, Addend);
Eli Benderskya66a1852012-01-16 08:56:09 +0000739 break;
740 case Triple::x86:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000741 resolveX86Relocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000742 (uint32_t)(Value & 0xffffffffL), Type,
743 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000744 break;
Tim Northover85829bb2013-05-04 20:13:59 +0000745 case Triple::aarch64:
746 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
747 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000748 case Triple::arm: // Fall through.
749 case Triple::thumb:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000750 resolveARMRelocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000751 (uint32_t)(Value & 0xffffffffL), Type,
752 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000753 break;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000754 case Triple::mips: // Fall through.
755 case Triple::mipsel:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000756 resolveMIPSRelocation(Section, Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000757 (uint32_t)(Value & 0xffffffffL), Type,
758 (uint32_t)(Addend & 0xffffffffL));
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000759 break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000760 case Triple::ppc64:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000761 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000762 break;
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000763 case Triple::systemz:
764 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
765 break;
Craig Topper85814382012-02-07 05:05:23 +0000766 default: llvm_unreachable("Unsupported CPU type!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000767 }
768}
769
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000770void RuntimeDyldELF::processRelocationRef(unsigned SectionID,
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000771 RelocationRef RelI,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000772 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000773 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000774 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000775 StubMap &Stubs) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000776 uint64_t RelType;
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000777 Check(RelI.getType(RelType));
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000778 int64_t Addend;
Rafael Espindola167957f2013-05-09 03:39:05 +0000779 Check(getELFRelocationAddend(RelI, Addend));
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000780 symbol_iterator Symbol = RelI.getSymbol();
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000781
782 // Obtain the symbol name which is referenced in the relocation
783 StringRef TargetName;
Rafael Espindola0962b162013-06-05 02:55:01 +0000784 if (Symbol != Obj.end_symbols())
785 Symbol->getName(TargetName);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000786 DEBUG(dbgs() << "\t\tRelType: " << RelType
787 << " Addend: " << Addend
788 << " TargetName: " << TargetName
789 << "\n");
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000790 RelocationValueRef Value;
791 // First search for the symbol in the local symbol table
Rafael Espindola0962b162013-06-05 02:55:01 +0000792 SymbolTableMap::const_iterator lsi = Symbols.end();
793 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
794 if (Symbol != Obj.end_symbols()) {
795 lsi = Symbols.find(TargetName.data());
796 Symbol->getType(SymType);
797 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000798 if (lsi != Symbols.end()) {
799 Value.SectionID = lsi->second.first;
Ulrich Weigand03f018a2013-04-05 13:29:04 +0000800 Value.Addend = lsi->second.second + Addend;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000801 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000802 // Search for the symbol in the global symbol table
Rafael Espindola0962b162013-06-05 02:55:01 +0000803 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end();
804 if (Symbol != Obj.end_symbols())
805 gsi = GlobalSymbolTable.find(TargetName.data());
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000806 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000807 Value.SectionID = gsi->second.first;
Ulrich Weigand03f018a2013-04-05 13:29:04 +0000808 Value.Addend = gsi->second.second + Addend;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000809 } else {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000810 switch (SymType) {
811 case SymbolRef::ST_Debug: {
812 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
813 // and can be changed by another developers. Maybe best way is add
814 // a new symbol type ST_Section to SymbolRef and use it.
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000815 section_iterator si(Obj.end_sections());
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000816 Symbol->getSection(si);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000817 if (si == Obj.end_sections())
818 llvm_unreachable("Symbol section not found, bad object file format!");
819 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000820 // Default to 'true' in case isText fails (though it never does).
821 bool isCode = true;
822 si->isText(isCode);
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000823 Value.SectionID = findOrEmitSection(Obj,
824 (*si),
825 isCode,
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000826 ObjSectionToID);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000827 Value.Addend = Addend;
828 break;
829 }
830 case SymbolRef::ST_Unknown: {
831 Value.SymbolName = TargetName.data();
832 Value.Addend = Addend;
833 break;
834 }
835 default:
836 llvm_unreachable("Unresolved symbol type!");
837 break;
838 }
839 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000840 }
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000841 uint64_t Offset;
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000842 Check(RelI.getOffset(Offset));
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000843
844 DEBUG(dbgs() << "\t\tSectionID: " << SectionID
845 << " Offset: " << Offset
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000846 << "\n");
Tim Northover4a9b6b72013-05-04 20:14:09 +0000847 if (Arch == Triple::aarch64 &&
848 (RelType == ELF::R_AARCH64_CALL26 ||
849 RelType == ELF::R_AARCH64_JUMP26)) {
850 // This is an AArch64 branch relocation, need to use a stub function.
851 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
852 SectionEntry &Section = Sections[SectionID];
853
854 // Look for an existing stub.
855 StubMap::const_iterator i = Stubs.find(Value);
856 if (i != Stubs.end()) {
857 resolveRelocation(Section, Offset,
858 (uint64_t)Section.Address + i->second, RelType, 0);
859 DEBUG(dbgs() << " Stub function found\n");
860 } else {
861 // Create a new stub function.
862 DEBUG(dbgs() << " Create a new stub function\n");
863 Stubs[Value] = Section.StubOffset;
864 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
865 Section.StubOffset);
866
867 RelocationEntry REmovz_g3(SectionID,
868 StubTargetAddr - Section.Address,
869 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
870 RelocationEntry REmovk_g2(SectionID,
871 StubTargetAddr - Section.Address + 4,
872 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
873 RelocationEntry REmovk_g1(SectionID,
874 StubTargetAddr - Section.Address + 8,
875 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
876 RelocationEntry REmovk_g0(SectionID,
877 StubTargetAddr - Section.Address + 12,
878 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
879
880 if (Value.SymbolName) {
881 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
882 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
883 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
884 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
885 } else {
886 addRelocationForSection(REmovz_g3, Value.SectionID);
887 addRelocationForSection(REmovk_g2, Value.SectionID);
888 addRelocationForSection(REmovk_g1, Value.SectionID);
889 addRelocationForSection(REmovk_g0, Value.SectionID);
890 }
891 resolveRelocation(Section, Offset,
892 (uint64_t)Section.Address + Section.StubOffset,
893 RelType, 0);
894 Section.StubOffset += getMaxStubSize();
895 }
896 } else if (Arch == Triple::arm &&
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000897 (RelType == ELF::R_ARM_PC24 ||
898 RelType == ELF::R_ARM_CALL ||
899 RelType == ELF::R_ARM_JUMP24)) {
900 // This is an ARM branch relocation, need to use a stub function.
901 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000902 SectionEntry &Section = Sections[SectionID];
Eli Benderskya66a1852012-01-16 08:56:09 +0000903
Eric Christopherbf261f12012-10-23 17:19:15 +0000904 // Look for an existing stub.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000905 StubMap::const_iterator i = Stubs.find(Value);
906 if (i != Stubs.end()) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000907 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000908 (uint64_t)Section.Address + i->second, RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000909 DEBUG(dbgs() << " Stub function found\n");
910 } else {
911 // Create a new stub function.
912 DEBUG(dbgs() << " Create a new stub function\n");
913 Stubs[Value] = Section.StubOffset;
914 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
915 Section.StubOffset);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000916 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Tim Northovere274b472013-05-28 19:48:19 +0000917 ELF::R_ARM_PRIVATE_0, Value.Addend);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000918 if (Value.SymbolName)
919 addRelocationForSymbol(RE, Value.SymbolName);
920 else
921 addRelocationForSection(RE, Value.SectionID);
922
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000923 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000924 (uint64_t)Section.Address + Section.StubOffset,
925 RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000926 Section.StubOffset += getMaxStubSize();
927 }
Akira Hatanakaade04742012-12-03 23:12:19 +0000928 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
929 RelType == ELF::R_MIPS_26) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000930 // This is an Mips branch relocation, need to use a stub function.
931 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000932 SectionEntry &Section = Sections[SectionID];
933 uint8_t *Target = Section.Address + Offset;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000934 uint32_t *TargetAddress = (uint32_t *)Target;
935
936 // Extract the addend from the instruction.
937 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
938
939 Value.Addend += Addend;
940
941 // Look up for existing stub.
942 StubMap::const_iterator i = Stubs.find(Value);
943 if (i != Stubs.end()) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000944 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000945 (uint64_t)Section.Address + i->second, RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000946 DEBUG(dbgs() << " Stub function found\n");
947 } else {
948 // Create a new stub function.
949 DEBUG(dbgs() << " Create a new stub function\n");
950 Stubs[Value] = Section.StubOffset;
951 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
952 Section.StubOffset);
953
954 // Creating Hi and Lo relocations for the filled stub instructions.
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000955 RelocationEntry REHi(SectionID,
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000956 StubTargetAddr - Section.Address,
957 ELF::R_MIPS_HI16, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000958 RelocationEntry RELo(SectionID,
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000959 StubTargetAddr - Section.Address + 4,
960 ELF::R_MIPS_LO16, Value.Addend);
961
962 if (Value.SymbolName) {
963 addRelocationForSymbol(REHi, Value.SymbolName);
964 addRelocationForSymbol(RELo, Value.SymbolName);
965 } else {
966 addRelocationForSection(REHi, Value.SectionID);
967 addRelocationForSection(RELo, Value.SectionID);
968 }
969
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000970 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000971 (uint64_t)Section.Address + Section.StubOffset,
972 RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000973 Section.StubOffset += getMaxStubSize();
974 }
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000975 } else if (Arch == Triple::ppc64) {
976 if (RelType == ELF::R_PPC64_REL24) {
977 // A PPC branch relocation will need a stub function if the target is
978 // an external symbol (Symbol::ST_Unknown) or if the target address
979 // is not within the signed 24-bits branch address.
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000980 SectionEntry &Section = Sections[SectionID];
981 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000982 bool RangeOverflow = false;
983 if (SymType != SymbolRef::ST_Unknown) {
984 // A function call may points to the .opd entry, so the final symbol value
985 // in calculated based in the relocation values in .opd section.
986 findOPDEntrySection(Obj, ObjSectionToID, Value);
987 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
988 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
989 // If it is within 24-bits branch range, just set the branch target
990 if (SignExtend32<24>(delta) == delta) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000991 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000992 if (Value.SymbolName)
993 addRelocationForSymbol(RE, Value.SymbolName);
994 else
995 addRelocationForSection(RE, Value.SectionID);
996 } else {
997 RangeOverflow = true;
998 }
999 }
1000 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
1001 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1002 // larger than 24-bits.
1003 StubMap::const_iterator i = Stubs.find(Value);
1004 if (i != Stubs.end()) {
1005 // Symbol function stub already created, just relocate to it
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001006 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +00001007 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001008 DEBUG(dbgs() << " Stub function found\n");
1009 } else {
1010 // Create a new stub function.
1011 DEBUG(dbgs() << " Create a new stub function\n");
1012 Stubs[Value] = Section.StubOffset;
1013 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1014 Section.StubOffset);
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001015 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001016 ELF::R_PPC64_ADDR64, Value.Addend);
1017
1018 // Generates the 64-bits address loads as exemplified in section
1019 // 4.5.1 in PPC64 ELF ABI.
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001020 RelocationEntry REhst(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001021 StubTargetAddr - Section.Address + 2,
1022 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001023 RelocationEntry REhr(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001024 StubTargetAddr - Section.Address + 6,
1025 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001026 RelocationEntry REh(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001027 StubTargetAddr - Section.Address + 14,
1028 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001029 RelocationEntry REl(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001030 StubTargetAddr - Section.Address + 18,
1031 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1032
1033 if (Value.SymbolName) {
1034 addRelocationForSymbol(REhst, Value.SymbolName);
1035 addRelocationForSymbol(REhr, Value.SymbolName);
1036 addRelocationForSymbol(REh, Value.SymbolName);
1037 addRelocationForSymbol(REl, Value.SymbolName);
1038 } else {
1039 addRelocationForSection(REhst, Value.SectionID);
1040 addRelocationForSection(REhr, Value.SectionID);
1041 addRelocationForSection(REh, Value.SectionID);
1042 addRelocationForSection(REl, Value.SectionID);
1043 }
1044
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001045 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +00001046 (uint64_t)Section.Address + Section.StubOffset,
1047 RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001048 if (SymType == SymbolRef::ST_Unknown)
1049 // Restore the TOC for external calls
1050 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
1051 Section.StubOffset += getMaxStubSize();
1052 }
1053 }
1054 } else {
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001055 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001056 // Extra check to avoid relocation againt empty symbols (usually
1057 // the R_PPC64_TOC).
1058 if (Value.SymbolName && !TargetName.empty())
1059 addRelocationForSymbol(RE, Value.SymbolName);
1060 else
1061 addRelocationForSection(RE, Value.SectionID);
1062 }
Richard Sandiford6fc2ad62013-05-03 14:15:35 +00001063 } else if (Arch == Triple::systemz &&
1064 (RelType == ELF::R_390_PLT32DBL ||
1065 RelType == ELF::R_390_GOTENT)) {
1066 // Create function stubs for both PLT and GOT references, regardless of
1067 // whether the GOT reference is to data or code. The stub contains the
1068 // full address of the symbol, as needed by GOT references, and the
1069 // executable part only adds an overhead of 8 bytes.
1070 //
1071 // We could try to conserve space by allocating the code and data
1072 // parts of the stub separately. However, as things stand, we allocate
1073 // a stub for every relocation, so using a GOT in JIT code should be
1074 // no less space efficient than using an explicit constant pool.
1075 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1076 SectionEntry &Section = Sections[SectionID];
1077
1078 // Look for an existing stub.
1079 StubMap::const_iterator i = Stubs.find(Value);
1080 uintptr_t StubAddress;
1081 if (i != Stubs.end()) {
1082 StubAddress = uintptr_t(Section.Address) + i->second;
1083 DEBUG(dbgs() << " Stub function found\n");
1084 } else {
1085 // Create a new stub function.
1086 DEBUG(dbgs() << " Create a new stub function\n");
1087
1088 uintptr_t BaseAddress = uintptr_t(Section.Address);
1089 uintptr_t StubAlignment = getStubAlignment();
1090 StubAddress = (BaseAddress + Section.StubOffset +
1091 StubAlignment - 1) & -StubAlignment;
1092 unsigned StubOffset = StubAddress - BaseAddress;
1093
1094 Stubs[Value] = StubOffset;
1095 createStubFunction((uint8_t *)StubAddress);
1096 RelocationEntry RE(SectionID, StubOffset + 8,
1097 ELF::R_390_64, Value.Addend - Addend);
1098 if (Value.SymbolName)
1099 addRelocationForSymbol(RE, Value.SymbolName);
1100 else
1101 addRelocationForSection(RE, Value.SectionID);
1102 Section.StubOffset = StubOffset + getMaxStubSize();
1103 }
1104
1105 if (RelType == ELF::R_390_GOTENT)
1106 resolveRelocation(Section, Offset, StubAddress + 8,
1107 ELF::R_390_PC32DBL, Addend);
1108 else
1109 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Eli Benderskyc201e6e2012-05-01 10:41:12 +00001110 } else {
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001111 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Eli Benderskyc201e6e2012-05-01 10:41:12 +00001112 if (Value.SymbolName)
1113 addRelocationForSymbol(RE, Value.SymbolName);
1114 else
1115 addRelocationForSection(RE, Value.SectionID);
1116 }
Jim Grosbach61425c02012-01-16 22:26:39 +00001117}
1118
Andrew Kaylor3f23cef2012-10-02 21:18:39 +00001119bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1120 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1121 return false;
1122 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
Eli Benderskya66a1852012-01-16 08:56:09 +00001123}
1124} // namespace llvm