blob: a438dcd87991159f11341374cd2adf833b273521 [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"
Michael J. Spencer081a1942013-08-08 22:27:13 +000025#include "llvm/Object/ELFObjectFile.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,
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000205 int64_t Addend,
206 uint64_t SymOffset) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000207 switch (Type) {
208 default:
209 llvm_unreachable("Relocation type not implemented yet!");
210 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000211 case ELF::R_X86_64_64: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000212 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000213 *Target = Value + Addend;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000214 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend))
215 << " at " << format("%p\n",Target));
Eli Benderskya66a1852012-01-16 08:56:09 +0000216 break;
217 }
218 case ELF::R_X86_64_32:
219 case ELF::R_X86_64_32S: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000220 Value += Addend;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000221 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000222 (Type == ELF::R_X86_64_32S &&
Andrew Kaylord83a5472012-07-27 20:30:12 +0000223 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Benderskya66a1852012-01-16 08:56:09 +0000224 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000225 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
Eli Benderskya66a1852012-01-16 08:56:09 +0000226 *Target = TruncatedAddr;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000227 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr)
228 << " at " << format("%p\n",Target));
Eli Benderskya66a1852012-01-16 08:56:09 +0000229 break;
230 }
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000231 case ELF::R_X86_64_GOTPCREL: {
232 // findGOTEntry returns the 'G + GOT' part of the relocation calculation
233 // based on the load/target address of the GOT (not the current/local addr).
234 uint64_t GOTAddr = findGOTEntry(Value, SymOffset);
235 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
236 uint64_t FinalAddress = Section.LoadAddress + Offset;
237 // The processRelocationRef method combines the symbol offset and the addend
238 // and in most cases that's what we want. For this relocation type, we need
239 // the raw addend, so we subtract the symbol offset to get it.
240 int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress;
241 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
242 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
243 *Target = TruncOffset;
244 break;
245 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000246 case ELF::R_X86_64_PC32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000247 // Get the placeholder value from the generated object since
248 // a previous relocation attempt may have overwritten the loaded version
249 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
250 + Offset);
251 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
252 uint64_t FinalAddress = Section.LoadAddress + Offset;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000253 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000254 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000255 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000256 *Target = TruncOffset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000257 break;
258 }
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000259 case ELF::R_X86_64_PC64: {
260 // Get the placeholder value from the generated object since
261 // a previous relocation attempt may have overwritten the loaded version
262 uint64_t *Placeholder = reinterpret_cast<uint64_t*>(Section.ObjAddress
263 + Offset);
264 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
265 uint64_t FinalAddress = Section.LoadAddress + Offset;
266 *Target = *Placeholder + Value + Addend - FinalAddress;
267 break;
268 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000269 }
270}
271
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000272void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
273 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000274 uint32_t Value,
275 uint32_t Type,
276 int32_t Addend) {
277 switch (Type) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000278 case ELF::R_386_32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000279 // Get the placeholder value from the generated object since
280 // a previous relocation attempt may have overwritten the loaded version
281 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
282 + Offset);
283 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
284 *Target = *Placeholder + Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000285 break;
286 }
287 case ELF::R_386_PC32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000288 // Get the placeholder value from the generated object since
289 // a previous relocation attempt may have overwritten the loaded version
290 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
291 + Offset);
292 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
293 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000294 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000295 *Target = RealOffset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000296 break;
297 }
298 default:
299 // There are other relocation types, but it appears these are the
Andrew Kaylore2e73bd2012-07-27 18:39:47 +0000300 // only ones currently used by the LLVM ELF object writer
Craig Topper85814382012-02-07 05:05:23 +0000301 llvm_unreachable("Relocation type not implemented yet!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000302 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000303 }
304}
305
Tim Northover85829bb2013-05-04 20:13:59 +0000306void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
307 uint64_t Offset,
308 uint64_t Value,
309 uint32_t Type,
310 int64_t Addend) {
311 uint32_t *TargetPtr = reinterpret_cast<uint32_t*>(Section.Address + Offset);
312 uint64_t FinalAddress = Section.LoadAddress + Offset;
313
314 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
315 << format("%llx", Section.Address + Offset)
316 << " FinalAddress: 0x" << format("%llx",FinalAddress)
317 << " Value: 0x" << format("%llx",Value)
318 << " Type: 0x" << format("%x",Type)
319 << " Addend: 0x" << format("%llx",Addend)
320 << "\n");
321
322 switch (Type) {
323 default:
324 llvm_unreachable("Relocation type not implemented yet!");
325 break;
Tim Northoverd52eaae2013-05-04 20:14:14 +0000326 case ELF::R_AARCH64_ABS64: {
327 uint64_t *TargetPtr = reinterpret_cast<uint64_t*>(Section.Address + Offset);
328 *TargetPtr = Value + Addend;
329 break;
330 }
Tim Northover675b9e92013-05-19 15:39:03 +0000331 case ELF::R_AARCH64_PREL32: {
Tim Northover85829bb2013-05-04 20:13:59 +0000332 uint64_t Result = Value + Addend - FinalAddress;
Michael J. Spencer081a1942013-08-08 22:27:13 +0000333 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
Tim Northover85829bb2013-05-04 20:13:59 +0000334 static_cast<int64_t>(Result) <= UINT32_MAX);
335 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
336 break;
337 }
Tim Northover4a9b6b72013-05-04 20:14:09 +0000338 case ELF::R_AARCH64_CALL26: // fallthrough
339 case ELF::R_AARCH64_JUMP26: {
340 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
341 // calculation.
342 uint64_t BranchImm = Value + Addend - FinalAddress;
343
344 // "Check that -2^27 <= result < 2^27".
Michael J. Spencer081a1942013-08-08 22:27:13 +0000345 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
Tim Northover4a9b6b72013-05-04 20:14:09 +0000346 static_cast<int64_t>(BranchImm) < (1LL << 27));
Tim Northover675b9e92013-05-19 15:39:03 +0000347
348 // AArch64 code is emitted with .rela relocations. The data already in any
349 // bits affected by the relocation on entry is garbage.
350 *TargetPtr &= 0xfc000000U;
Tim Northover4a9b6b72013-05-04 20:14:09 +0000351 // Immediate goes in bits 25:0 of B and BL.
352 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
353 break;
354 }
Tim Northover654c2d62013-05-04 20:14:04 +0000355 case ELF::R_AARCH64_MOVW_UABS_G3: {
356 uint64_t Result = Value + Addend;
Tim Northover675b9e92013-05-19 15:39:03 +0000357
358 // AArch64 code is emitted with .rela relocations. The data already in any
359 // bits affected by the relocation on entry is garbage.
Tim Northover107b2f22013-07-25 12:42:52 +0000360 *TargetPtr &= 0xffe0001fU;
Tim Northover654c2d62013-05-04 20:14:04 +0000361 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
362 *TargetPtr |= Result >> (48 - 5);
Tim Northover6711fc22013-07-01 19:23:10 +0000363 // Shift must be "lsl #48", in bits 22:21
364 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
Tim Northover654c2d62013-05-04 20:14:04 +0000365 break;
366 }
367 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
368 uint64_t Result = Value + Addend;
Tim Northover675b9e92013-05-19 15:39:03 +0000369
Tim Northover675b9e92013-05-19 15:39:03 +0000370 // AArch64 code is emitted with .rela relocations. The data already in any
371 // bits affected by the relocation on entry is garbage.
Tim Northover107b2f22013-07-25 12:42:52 +0000372 *TargetPtr &= 0xffe0001fU;
Tim Northover654c2d62013-05-04 20:14:04 +0000373 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
374 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
Tim Northover6711fc22013-07-01 19:23:10 +0000375 // Shift must be "lsl #32", in bits 22:21
376 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
Tim Northover654c2d62013-05-04 20:14:04 +0000377 break;
378 }
379 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
380 uint64_t Result = Value + Addend;
Tim Northover675b9e92013-05-19 15:39:03 +0000381
382 // AArch64 code is emitted with .rela relocations. The data already in any
383 // bits affected by the relocation on entry is garbage.
Tim Northover107b2f22013-07-25 12:42:52 +0000384 *TargetPtr &= 0xffe0001fU;
Tim Northover654c2d62013-05-04 20:14:04 +0000385 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
386 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
Tim Northover6711fc22013-07-01 19:23:10 +0000387 // Shift must be "lsl #16", in bits 22:2
388 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
Tim Northover654c2d62013-05-04 20:14:04 +0000389 break;
390 }
391 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
392 uint64_t Result = Value + Addend;
Tim Northover675b9e92013-05-19 15:39:03 +0000393
394 // AArch64 code is emitted with .rela relocations. The data already in any
395 // bits affected by the relocation on entry is garbage.
Tim Northover107b2f22013-07-25 12:42:52 +0000396 *TargetPtr &= 0xffe0001fU;
Tim Northover654c2d62013-05-04 20:14:04 +0000397 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
398 *TargetPtr |= ((Result & 0xffffU) << 5);
Tim Northover6711fc22013-07-01 19:23:10 +0000399 // Shift must be "lsl #0", in bits 22:21.
400 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
Tim Northover654c2d62013-05-04 20:14:04 +0000401 break;
402 }
Tim Northover85829bb2013-05-04 20:13:59 +0000403 }
404}
405
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000406void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
407 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000408 uint32_t Value,
409 uint32_t Type,
410 int32_t Addend) {
411 // TODO: Add Thumb relocations.
Tim Northovere274b472013-05-28 19:48:19 +0000412 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress +
413 Offset);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000414 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
415 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000416 Value += Addend;
417
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000418 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
419 << Section.Address + Offset
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000420 << " FinalAddress: " << format("%p",FinalAddress)
421 << " Value: " << format("%x",Value)
422 << " Type: " << format("%x",Type)
423 << " Addend: " << format("%x",Addend)
424 << "\n");
425
426 switch(Type) {
427 default:
428 llvm_unreachable("Not implemented relocation type!");
429
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000430 // Write a 32bit value to relocation address, taking into account the
Tim Northover565ebde2012-10-03 16:29:42 +0000431 // implicit addend encoded in the target.
Tim Northovere274b472013-05-28 19:48:19 +0000432 case ELF::R_ARM_TARGET1:
433 case ELF::R_ARM_ABS32:
434 *TargetPtr = *Placeholder + Value;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000435 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000436 // Write first 16 bit of 32 bit value to the mov instruction.
437 // Last 4 bit should be shifted.
Tim Northovere274b472013-05-28 19:48:19 +0000438 case ELF::R_ARM_MOVW_ABS_NC:
Tim Northover565ebde2012-10-03 16:29:42 +0000439 // We are not expecting any other addend in the relocation address.
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000440 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover565ebde2012-10-03 16:29:42 +0000441 // non-contiguous fields.
Tim Northovere274b472013-05-28 19:48:19 +0000442 assert((*Placeholder & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000443 Value = Value & 0xFFFF;
Tim Northovere274b472013-05-28 19:48:19 +0000444 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000445 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
446 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000447 // Write last 16 bit of 32 bit value to the mov instruction.
448 // Last 4 bit should be shifted.
Tim Northovere274b472013-05-28 19:48:19 +0000449 case ELF::R_ARM_MOVT_ABS:
Tim Northover565ebde2012-10-03 16:29:42 +0000450 // We are not expecting any other addend in the relocation address.
451 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
Tim Northovere274b472013-05-28 19:48:19 +0000452 assert((*Placeholder & 0x000F0FFF) == 0);
453
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000454 Value = (Value >> 16) & 0xFFFF;
Tim Northovere274b472013-05-28 19:48:19 +0000455 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000456 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
457 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000458 // Write 24 bit relative value to the branch instruction.
459 case ELF::R_ARM_PC24 : // Fall through.
460 case ELF::R_ARM_CALL : // Fall through.
Tim Northovere274b472013-05-28 19:48:19 +0000461 case ELF::R_ARM_JUMP24: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000462 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
463 RelValue = (RelValue & 0x03FFFFFC) >> 2;
Tim Northovere274b472013-05-28 19:48:19 +0000464 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000465 *TargetPtr &= 0xFF000000;
466 *TargetPtr |= RelValue;
467 break;
468 }
Tim Northovere274b472013-05-28 19:48:19 +0000469 case ELF::R_ARM_PRIVATE_0:
470 // This relocation is reserved by the ARM ELF ABI for internal use. We
471 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
472 // in the stubs created during JIT (which can't put an addend into the
473 // original object file).
474 *TargetPtr = Value;
475 break;
476 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000477}
478
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000479void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
480 uint64_t Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000481 uint32_t Value,
482 uint32_t Type,
483 int32_t Addend) {
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +0000484 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress +
485 Offset);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000486 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000487 Value += Addend;
488
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000489 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
490 << Section.Address + Offset
491 << " FinalAddress: "
492 << format("%p",Section.LoadAddress + Offset)
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000493 << " Value: " << format("%x",Value)
494 << " Type: " << format("%x",Type)
495 << " Addend: " << format("%x",Addend)
496 << "\n");
497
498 switch(Type) {
499 default:
500 llvm_unreachable("Not implemented relocation type!");
501 break;
502 case ELF::R_MIPS_32:
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +0000503 *TargetPtr = Value + (*Placeholder);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000504 break;
505 case ELF::R_MIPS_26:
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +0000506 *TargetPtr = ((*Placeholder) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000507 break;
508 case ELF::R_MIPS_HI16:
509 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +0000510 Value += ((*Placeholder) & 0x0000ffff) << 16;
511 *TargetPtr = ((*Placeholder) & 0xffff0000) |
512 (((Value + 0x8000) >> 16) & 0xffff);
513 break;
514 case ELF::R_MIPS_LO16:
515 Value += ((*Placeholder) & 0x0000ffff);
516 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
517 break;
518 case ELF::R_MIPS_UNUSED1:
519 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
520 // are used for internal JIT purpose. These relocations are similar to
521 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
522 // account.
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000523 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
524 (((Value + 0x8000) >> 16) & 0xffff);
525 break;
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +0000526 case ELF::R_MIPS_UNUSED2:
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000527 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
528 break;
529 }
530}
531
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000532// Return the .TOC. section address to R_PPC64_TOC relocations.
533uint64_t RuntimeDyldELF::findPPC64TOC() const {
534 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
535 // order. The TOC starts where the first of these sections starts.
536 SectionList::const_iterator it = Sections.begin();
537 SectionList::const_iterator ite = Sections.end();
538 for (; it != ite; ++it) {
539 if (it->Name == ".got" ||
540 it->Name == ".toc" ||
541 it->Name == ".tocbss" ||
542 it->Name == ".plt")
543 break;
544 }
545 if (it == ite) {
546 // This may happen for
547 // * references to TOC base base (sym@toc, .odp relocation) without
548 // a .toc directive.
549 // In this case just use the first section (which is usually
550 // the .odp) since the code won't reference the .toc base
551 // directly.
552 it = Sections.begin();
553 }
554 assert (it != ite);
555 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
556 // thus permitting a full 64 Kbytes segment.
557 return it->LoadAddress + 0x8000;
558}
559
560// Returns the sections and offset associated with the ODP entry referenced
561// by Symbol.
562void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
563 ObjSectionToIDMap &LocalSections,
564 RelocationValueRef &Rel) {
565 // Get the ELF symbol value (st_value) to compare with Relocation offset in
566 // .opd entries
567
568 error_code err;
569 for (section_iterator si = Obj.begin_sections(),
570 se = Obj.end_sections(); si != se; si.increment(err)) {
Rafael Espindola15e5c462013-06-03 19:37:34 +0000571 section_iterator RelSecI = si->getRelocatedSection();
572 if (RelSecI == Obj.end_sections())
573 continue;
574
575 StringRef RelSectionName;
576 check(RelSecI->getName(RelSectionName));
577 if (RelSectionName != ".opd")
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000578 continue;
579
580 for (relocation_iterator i = si->begin_relocations(),
581 e = si->end_relocations(); i != e;) {
582 check(err);
583
584 // The R_PPC64_ADDR64 relocation indicates the first field
585 // of a .opd entry
586 uint64_t TypeFunc;
587 check(i->getType(TypeFunc));
588 if (TypeFunc != ELF::R_PPC64_ADDR64) {
589 i.increment(err);
590 continue;
591 }
592
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000593 uint64_t TargetSymbolOffset;
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000594 symbol_iterator TargetSymbol = i->getSymbol();
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000595 check(i->getOffset(TargetSymbolOffset));
Rafael Espindola167957f2013-05-09 03:39:05 +0000596 int64_t Addend;
597 check(getELFRelocationAddend(*i, Addend));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000598
599 i = i.increment(err);
600 if (i == e)
601 break;
602 check(err);
603
604 // Just check if following relocation is a R_PPC64_TOC
605 uint64_t TypeTOC;
606 check(i->getType(TypeTOC));
607 if (TypeTOC != ELF::R_PPC64_TOC)
608 continue;
609
610 // Finally compares the Symbol value and the target symbol offset
611 // to check if this .opd entry refers to the symbol the relocation
612 // points to.
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000613 if (Rel.Addend != (int64_t)TargetSymbolOffset)
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000614 continue;
615
616 section_iterator tsi(Obj.end_sections());
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000617 check(TargetSymbol->getSection(tsi));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000618 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
Rafael Espindola167957f2013-05-09 03:39:05 +0000619 Rel.Addend = (intptr_t)Addend;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000620 return;
621 }
622 }
623 llvm_unreachable("Attempting to get address of ODP entry!");
624}
625
626// Relocation masks following the #lo(value), #hi(value), #higher(value),
627// and #highest(value) macros defined in section 4.5.1. Relocation Types
628// in PPC-elf64abi document.
629//
630static inline
631uint16_t applyPPClo (uint64_t value)
632{
633 return value & 0xffff;
634}
635
636static inline
637uint16_t applyPPChi (uint64_t value)
638{
639 return (value >> 16) & 0xffff;
640}
641
642static inline
643uint16_t applyPPChigher (uint64_t value)
644{
645 return (value >> 32) & 0xffff;
646}
647
648static inline
649uint16_t applyPPChighest (uint64_t value)
650{
651 return (value >> 48) & 0xffff;
652}
653
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000654void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
655 uint64_t Offset,
656 uint64_t Value,
657 uint32_t Type,
658 int64_t Addend) {
659 uint8_t* LocalAddress = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000660 switch (Type) {
661 default:
662 llvm_unreachable("Relocation type not implemented yet!");
663 break;
664 case ELF::R_PPC64_ADDR16_LO :
665 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
666 break;
667 case ELF::R_PPC64_ADDR16_HI :
668 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
669 break;
670 case ELF::R_PPC64_ADDR16_HIGHER :
671 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
672 break;
673 case ELF::R_PPC64_ADDR16_HIGHEST :
674 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
675 break;
676 case ELF::R_PPC64_ADDR14 : {
677 assert(((Value + Addend) & 3) == 0);
678 // Preserve the AA/LK bits in the branch instruction
679 uint8_t aalk = *(LocalAddress+3);
680 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
681 } break;
Adhemerval Zanella7b449882013-01-04 19:08:13 +0000682 case ELF::R_PPC64_ADDR32 : {
683 int32_t Result = static_cast<int32_t>(Value + Addend);
684 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanellaa1db5de2013-01-09 17:08:15 +0000685 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella7b449882013-01-04 19:08:13 +0000686 writeInt32BE(LocalAddress, Result);
687 } break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000688 case ELF::R_PPC64_REL24 : {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000689 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000690 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
691 if (SignExtend32<24>(delta) != delta)
692 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
693 // Generates a 'bl <address>' instruction
694 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
695 } break;
Adhemerval Zanellaa1db5de2013-01-09 17:08:15 +0000696 case ELF::R_PPC64_REL32 : {
697 uint64_t FinalAddress = (Section.LoadAddress + Offset);
698 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
699 if (SignExtend32<32>(delta) != delta)
700 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
701 writeInt32BE(LocalAddress, delta);
702 } break;
Adhemerval Zanellaf51d7e72013-05-06 17:21:23 +0000703 case ELF::R_PPC64_REL64: {
704 uint64_t FinalAddress = (Section.LoadAddress + Offset);
705 uint64_t Delta = Value - FinalAddress + Addend;
706 writeInt64BE(LocalAddress, Delta);
707 } break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000708 case ELF::R_PPC64_ADDR64 :
709 writeInt64BE(LocalAddress, Value + Addend);
710 break;
711 case ELF::R_PPC64_TOC :
712 writeInt64BE(LocalAddress, findPPC64TOC());
713 break;
714 case ELF::R_PPC64_TOC16 : {
715 uint64_t TOCStart = findPPC64TOC();
716 Value = applyPPClo((Value + Addend) - TOCStart);
717 writeInt16BE(LocalAddress, applyPPClo(Value));
718 } break;
719 case ELF::R_PPC64_TOC16_DS : {
720 uint64_t TOCStart = findPPC64TOC();
721 Value = ((Value + Addend) - TOCStart);
722 writeInt16BE(LocalAddress, applyPPClo(Value));
723 } break;
724 }
725}
726
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000727void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
728 uint64_t Offset,
729 uint64_t Value,
730 uint32_t Type,
731 int64_t Addend) {
732 uint8_t *LocalAddress = Section.Address + Offset;
733 switch (Type) {
734 default:
735 llvm_unreachable("Relocation type not implemented yet!");
736 break;
737 case ELF::R_390_PC16DBL:
738 case ELF::R_390_PLT16DBL: {
739 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
740 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
741 writeInt16BE(LocalAddress, Delta / 2);
742 break;
743 }
744 case ELF::R_390_PC32DBL:
745 case ELF::R_390_PLT32DBL: {
746 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
747 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
748 writeInt32BE(LocalAddress, Delta / 2);
749 break;
750 }
751 case ELF::R_390_PC32: {
752 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
753 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
754 writeInt32BE(LocalAddress, Delta);
755 break;
756 }
757 case ELF::R_390_64:
758 writeInt64BE(LocalAddress, Value + Addend);
759 break;
760 }
761}
762
Andrew Kaylor32bd10b2013-08-19 19:38:06 +0000763// The target location for the relocation is described by RE.SectionID and
764// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
765// SectionEntry has three members describing its location.
766// SectionEntry::Address is the address at which the section has been loaded
767// into memory in the current (host) process. SectionEntry::LoadAddress is the
768// address that the section will have in the target process.
769// SectionEntry::ObjAddress is the address of the bits for this section in the
770// original emitted object image (also in the current address space).
771//
772// Relocations will be applied as if the section were loaded at
773// SectionEntry::LoadAddress, but they will be applied at an address based
774// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
775// Target memory contents if they are required for value calculations.
776//
777// The Value parameter here is the load address of the symbol for the
778// relocation to be applied. For relocations which refer to symbols in the
779// current object Value will be the LoadAddress of the section in which
780// the symbol resides (RE.Addend provides additional information about the
781// symbol location). For external symbols, Value will be the address of the
782// symbol in the target address space.
Rafael Espindola87b50172013-04-29 17:24:34 +0000783void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
Andrew Kaylor32bd10b2013-08-19 19:38:06 +0000784 uint64_t Value) {
Rafael Espindola87b50172013-04-29 17:24:34 +0000785 const SectionEntry &Section = Sections[RE.SectionID];
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000786 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
787 RE.SymOffset);
Rafael Espindola87b50172013-04-29 17:24:34 +0000788}
789
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000790void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
791 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000792 uint64_t Value,
793 uint32_t Type,
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000794 int64_t Addend,
795 uint64_t SymOffset) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000796 switch (Arch) {
797 case Triple::x86_64:
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000798 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
Eli Benderskya66a1852012-01-16 08:56:09 +0000799 break;
800 case Triple::x86:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000801 resolveX86Relocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000802 (uint32_t)(Value & 0xffffffffL), Type,
803 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000804 break;
Tim Northover85829bb2013-05-04 20:13:59 +0000805 case Triple::aarch64:
806 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
807 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000808 case Triple::arm: // Fall through.
809 case Triple::thumb:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000810 resolveARMRelocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000811 (uint32_t)(Value & 0xffffffffL), Type,
812 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000813 break;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000814 case Triple::mips: // Fall through.
815 case Triple::mipsel:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000816 resolveMIPSRelocation(Section, Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000817 (uint32_t)(Value & 0xffffffffL), Type,
818 (uint32_t)(Addend & 0xffffffffL));
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000819 break;
Bill Schmidtf38cc382013-07-26 01:35:43 +0000820 case Triple::ppc64: // Fall through.
821 case Triple::ppc64le:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000822 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000823 break;
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000824 case Triple::systemz:
825 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
826 break;
Craig Topper85814382012-02-07 05:05:23 +0000827 default: llvm_unreachable("Unsupported CPU type!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000828 }
829}
830
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000831void RuntimeDyldELF::processRelocationRef(unsigned SectionID,
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000832 RelocationRef RelI,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000833 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000834 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000835 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000836 StubMap &Stubs) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000837 uint64_t RelType;
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000838 Check(RelI.getType(RelType));
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000839 int64_t Addend;
Rafael Espindola167957f2013-05-09 03:39:05 +0000840 Check(getELFRelocationAddend(RelI, Addend));
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000841 symbol_iterator Symbol = RelI.getSymbol();
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000842
843 // Obtain the symbol name which is referenced in the relocation
844 StringRef TargetName;
Rafael Espindola0962b162013-06-05 02:55:01 +0000845 if (Symbol != Obj.end_symbols())
846 Symbol->getName(TargetName);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000847 DEBUG(dbgs() << "\t\tRelType: " << RelType
848 << " Addend: " << Addend
849 << " TargetName: " << TargetName
850 << "\n");
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000851 RelocationValueRef Value;
852 // First search for the symbol in the local symbol table
Rafael Espindola0962b162013-06-05 02:55:01 +0000853 SymbolTableMap::const_iterator lsi = Symbols.end();
854 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
855 if (Symbol != Obj.end_symbols()) {
856 lsi = Symbols.find(TargetName.data());
857 Symbol->getType(SymType);
858 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000859 if (lsi != Symbols.end()) {
860 Value.SectionID = lsi->second.first;
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000861 Value.Offset = lsi->second.second;
Ulrich Weigand03f018a2013-04-05 13:29:04 +0000862 Value.Addend = lsi->second.second + Addend;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000863 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000864 // Search for the symbol in the global symbol table
Rafael Espindola0962b162013-06-05 02:55:01 +0000865 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end();
866 if (Symbol != Obj.end_symbols())
867 gsi = GlobalSymbolTable.find(TargetName.data());
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000868 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000869 Value.SectionID = gsi->second.first;
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000870 Value.Offset = gsi->second.second;
Ulrich Weigand03f018a2013-04-05 13:29:04 +0000871 Value.Addend = gsi->second.second + Addend;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000872 } else {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000873 switch (SymType) {
874 case SymbolRef::ST_Debug: {
875 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
876 // and can be changed by another developers. Maybe best way is add
877 // a new symbol type ST_Section to SymbolRef and use it.
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000878 section_iterator si(Obj.end_sections());
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000879 Symbol->getSection(si);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000880 if (si == Obj.end_sections())
881 llvm_unreachable("Symbol section not found, bad object file format!");
882 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000883 // Default to 'true' in case isText fails (though it never does).
884 bool isCode = true;
885 si->isText(isCode);
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000886 Value.SectionID = findOrEmitSection(Obj,
887 (*si),
888 isCode,
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000889 ObjSectionToID);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000890 Value.Addend = Addend;
891 break;
892 }
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000893 case SymbolRef::ST_Data:
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000894 case SymbolRef::ST_Unknown: {
895 Value.SymbolName = TargetName.data();
896 Value.Addend = Addend;
Richard Mittonb0f79292013-08-16 18:54:26 +0000897
898 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
899 // will manifest here as a NULL symbol name.
900 // We can set this as a valid (but empty) symbol name, and rely
901 // on addRelocationForSymbol to handle this.
902 if (!Value.SymbolName)
903 Value.SymbolName = "";
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000904 break;
905 }
906 default:
907 llvm_unreachable("Unresolved symbol type!");
908 break;
909 }
910 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000911 }
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000912 uint64_t Offset;
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000913 Check(RelI.getOffset(Offset));
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000914
915 DEBUG(dbgs() << "\t\tSectionID: " << SectionID
916 << " Offset: " << Offset
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000917 << "\n");
Tim Northover4a9b6b72013-05-04 20:14:09 +0000918 if (Arch == Triple::aarch64 &&
919 (RelType == ELF::R_AARCH64_CALL26 ||
920 RelType == ELF::R_AARCH64_JUMP26)) {
921 // This is an AArch64 branch relocation, need to use a stub function.
922 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
923 SectionEntry &Section = Sections[SectionID];
924
925 // Look for an existing stub.
926 StubMap::const_iterator i = Stubs.find(Value);
927 if (i != Stubs.end()) {
928 resolveRelocation(Section, Offset,
929 (uint64_t)Section.Address + i->second, RelType, 0);
930 DEBUG(dbgs() << " Stub function found\n");
931 } else {
932 // Create a new stub function.
933 DEBUG(dbgs() << " Create a new stub function\n");
934 Stubs[Value] = Section.StubOffset;
935 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
936 Section.StubOffset);
937
938 RelocationEntry REmovz_g3(SectionID,
939 StubTargetAddr - Section.Address,
940 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
941 RelocationEntry REmovk_g2(SectionID,
942 StubTargetAddr - Section.Address + 4,
943 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
944 RelocationEntry REmovk_g1(SectionID,
945 StubTargetAddr - Section.Address + 8,
946 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
947 RelocationEntry REmovk_g0(SectionID,
948 StubTargetAddr - Section.Address + 12,
949 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
950
951 if (Value.SymbolName) {
952 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
953 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
954 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
955 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
956 } else {
957 addRelocationForSection(REmovz_g3, Value.SectionID);
958 addRelocationForSection(REmovk_g2, Value.SectionID);
959 addRelocationForSection(REmovk_g1, Value.SectionID);
960 addRelocationForSection(REmovk_g0, Value.SectionID);
961 }
962 resolveRelocation(Section, Offset,
963 (uint64_t)Section.Address + Section.StubOffset,
964 RelType, 0);
965 Section.StubOffset += getMaxStubSize();
966 }
967 } else if (Arch == Triple::arm &&
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000968 (RelType == ELF::R_ARM_PC24 ||
969 RelType == ELF::R_ARM_CALL ||
970 RelType == ELF::R_ARM_JUMP24)) {
971 // This is an ARM branch relocation, need to use a stub function.
972 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000973 SectionEntry &Section = Sections[SectionID];
Eli Benderskya66a1852012-01-16 08:56:09 +0000974
Eric Christopherbf261f12012-10-23 17:19:15 +0000975 // Look for an existing stub.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000976 StubMap::const_iterator i = Stubs.find(Value);
977 if (i != Stubs.end()) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000978 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000979 (uint64_t)Section.Address + i->second, RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000980 DEBUG(dbgs() << " Stub function found\n");
981 } else {
982 // Create a new stub function.
983 DEBUG(dbgs() << " Create a new stub function\n");
984 Stubs[Value] = Section.StubOffset;
985 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
986 Section.StubOffset);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000987 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Tim Northovere274b472013-05-28 19:48:19 +0000988 ELF::R_ARM_PRIVATE_0, Value.Addend);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000989 if (Value.SymbolName)
990 addRelocationForSymbol(RE, Value.SymbolName);
991 else
992 addRelocationForSection(RE, Value.SectionID);
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);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000997 Section.StubOffset += getMaxStubSize();
998 }
Akira Hatanakaade04742012-12-03 23:12:19 +0000999 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
1000 RelType == ELF::R_MIPS_26) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001001 // This is an Mips branch relocation, need to use a stub function.
1002 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001003 SectionEntry &Section = Sections[SectionID];
1004 uint8_t *Target = Section.Address + Offset;
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001005 uint32_t *TargetAddress = (uint32_t *)Target;
1006
1007 // Extract the addend from the instruction.
1008 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
1009
1010 Value.Addend += Addend;
1011
1012 // Look up for existing stub.
1013 StubMap::const_iterator i = Stubs.find(Value);
1014 if (i != Stubs.end()) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001015 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +00001016 (uint64_t)Section.Address + i->second, RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001017 DEBUG(dbgs() << " Stub function found\n");
1018 } else {
1019 // Create a new stub function.
1020 DEBUG(dbgs() << " Create a new stub function\n");
1021 Stubs[Value] = Section.StubOffset;
1022 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1023 Section.StubOffset);
1024
1025 // Creating Hi and Lo relocations for the filled stub instructions.
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001026 RelocationEntry REHi(SectionID,
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001027 StubTargetAddr - Section.Address,
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +00001028 ELF::R_MIPS_UNUSED1, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001029 RelocationEntry RELo(SectionID,
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001030 StubTargetAddr - Section.Address + 4,
Akira Hatanaka3af1c9d2013-07-24 01:58:40 +00001031 ELF::R_MIPS_UNUSED2, Value.Addend);
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001032
1033 if (Value.SymbolName) {
1034 addRelocationForSymbol(REHi, Value.SymbolName);
1035 addRelocationForSymbol(RELo, Value.SymbolName);
1036 } else {
1037 addRelocationForSection(REHi, Value.SectionID);
1038 addRelocationForSection(RELo, Value.SectionID);
1039 }
1040
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001041 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +00001042 (uint64_t)Section.Address + Section.StubOffset,
1043 RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +00001044 Section.StubOffset += getMaxStubSize();
1045 }
Bill Schmidtf38cc382013-07-26 01:35:43 +00001046 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001047 if (RelType == ELF::R_PPC64_REL24) {
1048 // A PPC branch relocation will need a stub function if the target is
1049 // an external symbol (Symbol::ST_Unknown) or if the target address
1050 // is not within the signed 24-bits branch address.
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001051 SectionEntry &Section = Sections[SectionID];
1052 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001053 bool RangeOverflow = false;
1054 if (SymType != SymbolRef::ST_Unknown) {
1055 // A function call may points to the .opd entry, so the final symbol value
1056 // in calculated based in the relocation values in .opd section.
1057 findOPDEntrySection(Obj, ObjSectionToID, Value);
1058 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
1059 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
1060 // If it is within 24-bits branch range, just set the branch target
1061 if (SignExtend32<24>(delta) == delta) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001062 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001063 if (Value.SymbolName)
1064 addRelocationForSymbol(RE, Value.SymbolName);
1065 else
1066 addRelocationForSection(RE, Value.SectionID);
1067 } else {
1068 RangeOverflow = true;
1069 }
1070 }
1071 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
1072 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1073 // larger than 24-bits.
1074 StubMap::const_iterator i = Stubs.find(Value);
1075 if (i != Stubs.end()) {
1076 // Symbol function stub already created, just relocate to it
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001077 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +00001078 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001079 DEBUG(dbgs() << " Stub function found\n");
1080 } else {
1081 // Create a new stub function.
1082 DEBUG(dbgs() << " Create a new stub function\n");
1083 Stubs[Value] = Section.StubOffset;
1084 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1085 Section.StubOffset);
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001086 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001087 ELF::R_PPC64_ADDR64, Value.Addend);
1088
1089 // Generates the 64-bits address loads as exemplified in section
1090 // 4.5.1 in PPC64 ELF ABI.
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001091 RelocationEntry REhst(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001092 StubTargetAddr - Section.Address + 2,
1093 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001094 RelocationEntry REhr(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001095 StubTargetAddr - Section.Address + 6,
1096 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001097 RelocationEntry REh(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001098 StubTargetAddr - Section.Address + 14,
1099 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001100 RelocationEntry REl(SectionID,
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001101 StubTargetAddr - Section.Address + 18,
1102 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1103
1104 if (Value.SymbolName) {
1105 addRelocationForSymbol(REhst, Value.SymbolName);
1106 addRelocationForSymbol(REhr, Value.SymbolName);
1107 addRelocationForSymbol(REh, Value.SymbolName);
1108 addRelocationForSymbol(REl, Value.SymbolName);
1109 } else {
1110 addRelocationForSection(REhst, Value.SectionID);
1111 addRelocationForSection(REhr, Value.SectionID);
1112 addRelocationForSection(REh, Value.SectionID);
1113 addRelocationForSection(REl, Value.SectionID);
1114 }
1115
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001116 resolveRelocation(Section, Offset,
Andrew Kaylora307a1c2012-11-02 19:45:23 +00001117 (uint64_t)Section.Address + Section.StubOffset,
1118 RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001119 if (SymType == SymbolRef::ST_Unknown)
1120 // Restore the TOC for external calls
1121 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
1122 Section.StubOffset += getMaxStubSize();
1123 }
1124 }
1125 } else {
Rafael Espindolaefa91f62013-04-29 14:44:23 +00001126 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001127 // Extra check to avoid relocation againt empty symbols (usually
1128 // the R_PPC64_TOC).
Richard Mittonb0f79292013-08-16 18:54:26 +00001129 if (SymType != SymbolRef::ST_Unknown && TargetName.empty())
1130 Value.SymbolName = NULL;
1131
1132 if (Value.SymbolName)
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +00001133 addRelocationForSymbol(RE, Value.SymbolName);
1134 else
1135 addRelocationForSection(RE, Value.SectionID);
1136 }
Richard Sandiford6fc2ad62013-05-03 14:15:35 +00001137 } else if (Arch == Triple::systemz &&
1138 (RelType == ELF::R_390_PLT32DBL ||
1139 RelType == ELF::R_390_GOTENT)) {
1140 // Create function stubs for both PLT and GOT references, regardless of
1141 // whether the GOT reference is to data or code. The stub contains the
1142 // full address of the symbol, as needed by GOT references, and the
1143 // executable part only adds an overhead of 8 bytes.
1144 //
1145 // We could try to conserve space by allocating the code and data
1146 // parts of the stub separately. However, as things stand, we allocate
1147 // a stub for every relocation, so using a GOT in JIT code should be
1148 // no less space efficient than using an explicit constant pool.
1149 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1150 SectionEntry &Section = Sections[SectionID];
1151
1152 // Look for an existing stub.
1153 StubMap::const_iterator i = Stubs.find(Value);
1154 uintptr_t StubAddress;
1155 if (i != Stubs.end()) {
1156 StubAddress = uintptr_t(Section.Address) + i->second;
1157 DEBUG(dbgs() << " Stub function found\n");
1158 } else {
1159 // Create a new stub function.
1160 DEBUG(dbgs() << " Create a new stub function\n");
1161
1162 uintptr_t BaseAddress = uintptr_t(Section.Address);
1163 uintptr_t StubAlignment = getStubAlignment();
1164 StubAddress = (BaseAddress + Section.StubOffset +
1165 StubAlignment - 1) & -StubAlignment;
1166 unsigned StubOffset = StubAddress - BaseAddress;
1167
1168 Stubs[Value] = StubOffset;
1169 createStubFunction((uint8_t *)StubAddress);
1170 RelocationEntry RE(SectionID, StubOffset + 8,
1171 ELF::R_390_64, Value.Addend - Addend);
1172 if (Value.SymbolName)
1173 addRelocationForSymbol(RE, Value.SymbolName);
1174 else
1175 addRelocationForSection(RE, Value.SectionID);
1176 Section.StubOffset = StubOffset + getMaxStubSize();
1177 }
1178
1179 if (RelType == ELF::R_390_GOTENT)
1180 resolveRelocation(Section, Offset, StubAddress + 8,
1181 ELF::R_390_PC32DBL, Addend);
1182 else
1183 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001184 } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) {
1185 // The way the PLT relocations normally work is that the linker allocates the
1186 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
1187 // entry will then jump to an address provided by the GOT. On first call, the
1188 // GOT address will point back into PLT code that resolves the symbol. After
1189 // the first call, the GOT entry points to the actual function.
1190 //
1191 // For local functions we're ignoring all of that here and just replacing
1192 // the PLT32 relocation type with PC32, which will translate the relocation
1193 // into a PC-relative call directly to the function. For external symbols we
1194 // can't be sure the function will be within 2^32 bytes of the call site, so
1195 // we need to create a stub, which calls into the GOT. This case is
1196 // equivalent to the usual PLT implementation except that we use the stub
1197 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1198 // rather than allocating a PLT section.
1199 if (Value.SymbolName) {
1200 // This is a call to an external function.
1201 // Look for an existing stub.
1202 SectionEntry &Section = Sections[SectionID];
1203 StubMap::const_iterator i = Stubs.find(Value);
1204 uintptr_t StubAddress;
1205 if (i != Stubs.end()) {
1206 StubAddress = uintptr_t(Section.Address) + i->second;
1207 DEBUG(dbgs() << " Stub function found\n");
1208 } else {
1209 // Create a new stub function (equivalent to a PLT entry).
1210 DEBUG(dbgs() << " Create a new stub function\n");
1211
1212 uintptr_t BaseAddress = uintptr_t(Section.Address);
1213 uintptr_t StubAlignment = getStubAlignment();
1214 StubAddress = (BaseAddress + Section.StubOffset +
1215 StubAlignment - 1) & -StubAlignment;
1216 unsigned StubOffset = StubAddress - BaseAddress;
1217 Stubs[Value] = StubOffset;
1218 createStubFunction((uint8_t *)StubAddress);
1219
1220 // Create a GOT entry for the external function.
1221 GOTEntries.push_back(Value);
1222
1223 // Make our stub function a relative call to the GOT entry.
1224 RelocationEntry RE(SectionID, StubOffset + 2,
1225 ELF::R_X86_64_GOTPCREL, -4);
1226 addRelocationForSymbol(RE, Value.SymbolName);
1227
1228 // Bump our stub offset counter
1229 Section.StubOffset = StubOffset + getMaxStubSize();
1230 }
1231
1232 // Make the target call a call into the stub table.
1233 resolveRelocation(Section, Offset, StubAddress,
1234 ELF::R_X86_64_PC32, Addend);
1235 } else {
1236 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
1237 Value.Offset);
1238 addRelocationForSection(RE, Value.SectionID);
1239 }
Eli Benderskyc201e6e2012-05-01 10:41:12 +00001240 } else {
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001241 if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) {
1242 GOTEntries.push_back(Value);
1243 }
1244 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
Eli Benderskyc201e6e2012-05-01 10:41:12 +00001245 if (Value.SymbolName)
1246 addRelocationForSymbol(RE, Value.SymbolName);
1247 else
1248 addRelocationForSection(RE, Value.SectionID);
1249 }
Jim Grosbach61425c02012-01-16 22:26:39 +00001250}
1251
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001252void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) {
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001253
1254 SmallVectorImpl<std::pair<SID, GOTRelocations> >::iterator it;
1255 SmallVectorImpl<std::pair<SID, GOTRelocations> >::iterator end = GOTs.end();
1256
1257 for (it = GOTs.begin(); it != end; ++it) {
1258 GOTRelocations &GOTEntries = it->second;
1259 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
1260 if (GOTEntries[i].SymbolName != 0 && GOTEntries[i].SymbolName == Name) {
1261 GOTEntries[i].Offset = Addr;
1262 }
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001263 }
1264 }
1265}
1266
1267size_t RuntimeDyldELF::getGOTEntrySize() {
1268 // We don't use the GOT in all of these cases, but it's essentially free
1269 // to put them all here.
1270 size_t Result = 0;
1271 switch (Arch) {
1272 case Triple::x86_64:
1273 case Triple::aarch64:
1274 case Triple::ppc64:
1275 case Triple::ppc64le:
1276 case Triple::systemz:
1277 Result = sizeof(uint64_t);
1278 break;
1279 case Triple::x86:
1280 case Triple::arm:
1281 case Triple::thumb:
1282 case Triple::mips:
1283 case Triple::mipsel:
1284 Result = sizeof(uint32_t);
1285 break;
1286 default: llvm_unreachable("Unsupported CPU type!");
1287 }
1288 return Result;
1289}
1290
1291uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress,
1292 uint64_t Offset) {
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001293
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001294 const size_t GOTEntrySize = getGOTEntrySize();
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001295
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001296 SmallVectorImpl<std::pair<SID, GOTRelocations> >::const_iterator it;
1297 SmallVectorImpl<std::pair<SID, GOTRelocations> >::const_iterator end = GOTs.end();
1298
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001299 int GOTIndex = -1;
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001300 for (it = GOTs.begin(); it != end; ++it) {
1301 SID GOTSectionID = it->first;
1302 const GOTRelocations &GOTEntries = it->second;
1303
1304 // Find the matching entry in our vector.
1305 uint64_t SymbolOffset = 0;
1306 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
1307 if (GOTEntries[i].SymbolName == 0) {
1308 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress &&
1309 GOTEntries[i].Offset == Offset) {
1310 GOTIndex = i;
1311 SymbolOffset = GOTEntries[i].Offset;
1312 break;
1313 }
1314 } else {
1315 // GOT entries for external symbols use the addend as the address when
1316 // the external symbol has been resolved.
1317 if (GOTEntries[i].Offset == LoadAddress) {
1318 GOTIndex = i;
1319 // Don't use the Addend here. The relocation handler will use it.
1320 break;
1321 }
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001322 }
1323 }
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001324
1325 if (GOTIndex != -1) {
1326 if (GOTEntrySize == sizeof(uint64_t)) {
1327 uint64_t *LocalGOTAddr = (uint64_t*)getSectionAddress(GOTSectionID);
1328 // Fill in this entry with the address of the symbol being referenced.
1329 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset;
1330 } else {
1331 uint32_t *LocalGOTAddr = (uint32_t*)getSectionAddress(GOTSectionID);
1332 // Fill in this entry with the address of the symbol being referenced.
1333 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset);
1334 }
1335
1336 // Calculate the load address of this entry
1337 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize);
1338 }
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001339 }
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001340
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001341 assert(GOTIndex != -1 && "Unable to find requested GOT entry.");
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001342 return 0;
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001343}
1344
1345void RuntimeDyldELF::finalizeLoad() {
Andrew Kaylor9cffedb2013-10-05 01:52:09 +00001346 if (MemMgr) {
1347 // Allocate the GOT if necessary
1348 size_t numGOTEntries = GOTEntries.size();
1349 if (numGOTEntries != 0) {
1350 // Allocate memory for the section
1351 unsigned SectionID = Sections.size();
1352 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1353 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1354 SectionID, ".got", false);
1355 if (!Addr)
1356 report_fatal_error("Unable to allocate memory for GOT!");
1357
1358 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1359 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1360 // For now, initialize all GOT entries to zero. We'll fill them in as
1361 // needed when GOT-based relocations are applied.
1362 memset(Addr, 0, TotalSize);
1363 }
1364 }
1365 else {
1366 report_fatal_error("Unable to allocate memory for GOT!");
Andrew Kaylorff9fa052013-08-19 23:27:43 +00001367 }
1368}
1369
Andrew Kaylor3f23cef2012-10-02 21:18:39 +00001370bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1371 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1372 return false;
1373 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
Eli Benderskya66a1852012-01-16 08:56:09 +00001374}
1375} // namespace llvm