blob: 7f3a790d4b972cc36b74852b563919926acc52fc [file] [log] [blame]
Jim Grosbach06594e12012-01-16 23:50:58 +00001//===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
Eli Bendersky4c647582012-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 Kayloradc70562012-10-02 21:18:39 +000015#include "RuntimeDyldELF.h"
16#include "JITRegistrar.h"
17#include "ObjectImageCommon.h"
Eli Bendersky4c647582012-01-16 08:56:09 +000018#include "llvm/ADT/IntervalMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/OwningPtr.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
Eli Bendersky4c647582012-01-16 08:56:09 +000022#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/ExecutionEngine/ObjectBuffer.h"
24#include "llvm/ExecutionEngine/ObjectImage.h"
Michael J. Spencer126973b2013-08-08 22:27:13 +000025#include "llvm/Object/ELFObjectFile.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Object/ObjectFile.h"
27#include "llvm/Support/ELF.h"
Lang Hames173c69f2014-01-08 04:09:09 +000028#include "llvm/Support/MemoryBuffer.h"
29
Eli Bendersky4c647582012-01-16 08:56:09 +000030using namespace llvm;
31using namespace llvm::object;
32
Preston Gurdcc31af92012-04-16 22:12:58 +000033namespace {
34
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000035static inline
36error_code check(error_code Err) {
37 if (Err) {
38 report_fatal_error(Err.message());
39 }
40 return Err;
41}
42
Michael J. Spencer1a791612013-01-15 07:44:25 +000043template<class ELFT>
Michael J. Spencerbae14ce2013-01-04 20:36:28 +000044class DyldELFObject
Michael J. Spencer1a791612013-01-15 07:44:25 +000045 : public ELFObjectFile<ELFT> {
Rafael Espindola035b4162013-04-17 21:20:55 +000046 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Preston Gurdcc31af92012-04-16 22:12:58 +000047
Michael J. Spencer1a791612013-01-15 07:44:25 +000048 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
49 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
Michael J. Spencerbae14ce2013-01-04 20:36:28 +000050 typedef
Michael J. Spencer1a791612013-01-15 07:44:25 +000051 Elf_Rel_Impl<ELFT, false> Elf_Rel;
Michael J. Spencerbae14ce2013-01-04 20:36:28 +000052 typedef
Michael J. Spencer1a791612013-01-15 07:44:25 +000053 Elf_Rel_Impl<ELFT, true> Elf_Rela;
Preston Gurdcc31af92012-04-16 22:12:58 +000054
Michael J. Spencer1a791612013-01-15 07:44:25 +000055 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
Preston Gurdcc31af92012-04-16 22:12:58 +000056
57 typedef typename ELFDataTypeTypedefHelper<
Michael J. Spencer1a791612013-01-15 07:44:25 +000058 ELFT>::value_type addr_type;
Preston Gurdcc31af92012-04-16 22:12:58 +000059
Preston Gurdcc31af92012-04-16 22:12:58 +000060public:
Andrew Kayloradc70562012-10-02 21:18:39 +000061 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
Preston Gurdcc31af92012-04-16 22:12:58 +000062
63 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
64 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
65
Andrew Kaylor5c010902012-07-27 17:52:42 +000066 // Methods for type inquiry through isa, cast and dyn_cast
Preston Gurdcc31af92012-04-16 22:12:58 +000067 static inline bool classof(const Binary *v) {
Michael J. Spencer1a791612013-01-15 07:44:25 +000068 return (isa<ELFObjectFile<ELFT> >(v)
Michael J. Spencerbae14ce2013-01-04 20:36:28 +000069 && classof(cast<ELFObjectFile
Michael J. Spencer1a791612013-01-15 07:44:25 +000070 <ELFT> >(v)));
Preston Gurdcc31af92012-04-16 22:12:58 +000071 }
72 static inline bool classof(
Michael J. Spencer1a791612013-01-15 07:44:25 +000073 const ELFObjectFile<ELFT> *v) {
Preston Gurdcc31af92012-04-16 22:12:58 +000074 return v->isDyldType();
75 }
Preston Gurdcc31af92012-04-16 22:12:58 +000076};
77
Michael J. Spencer1a791612013-01-15 07:44:25 +000078template<class ELFT>
Andrew Kayloradc70562012-10-02 21:18:39 +000079class ELFObjectImage : public ObjectImageCommon {
Preston Gurdcc31af92012-04-16 22:12:58 +000080 protected:
Michael J. Spencer1a791612013-01-15 07:44:25 +000081 DyldELFObject<ELFT> *DyldObj;
Preston Gurdcc31af92012-04-16 22:12:58 +000082 bool Registered;
83
84 public:
Andrew Kayloradc70562012-10-02 21:18:39 +000085 ELFObjectImage(ObjectBuffer *Input,
Michael J. Spencer1a791612013-01-15 07:44:25 +000086 DyldELFObject<ELFT> *Obj)
Andrew Kayloradc70562012-10-02 21:18:39 +000087 : ObjectImageCommon(Input, Obj),
Preston Gurdcc31af92012-04-16 22:12:58 +000088 DyldObj(Obj),
89 Registered(false) {}
90
91 virtual ~ELFObjectImage() {
92 if (Registered)
93 deregisterWithDebugger();
94 }
95
96 // Subclasses can override these methods to update the image with loaded
97 // addresses for sections and common symbols
98 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
99 {
100 DyldObj->updateSectionAddress(Sec, Addr);
101 }
102
103 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
104 {
105 DyldObj->updateSymbolAddress(Sym, Addr);
106 }
107
108 virtual void registerWithDebugger()
109 {
Andrew Kayloradc70562012-10-02 21:18:39 +0000110 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
Preston Gurdcc31af92012-04-16 22:12:58 +0000111 Registered = true;
112 }
113 virtual void deregisterWithDebugger()
114 {
Andrew Kayloradc70562012-10-02 21:18:39 +0000115 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
Preston Gurdcc31af92012-04-16 22:12:58 +0000116 }
117};
118
Andrew Kayloradc70562012-10-02 21:18:39 +0000119// The MemoryBuffer passed into this constructor is just a wrapper around the
120// actual memory. Ultimately, the Binary parent class will take ownership of
121// this MemoryBuffer object but not the underlying memory.
Michael J. Spencer1a791612013-01-15 07:44:25 +0000122template<class ELFT>
123DyldELFObject<ELFT>::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
124 : ELFObjectFile<ELFT>(Wrapper, ec) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000125 this->isDyldELFObject = true;
126}
127
Michael J. Spencer1a791612013-01-15 07:44:25 +0000128template<class ELFT>
129void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
130 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000131 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
132 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
133 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
134
135 // This assumes the address passed in matches the target address bitness
136 // The template-based type cast handles everything else.
137 shdr->sh_addr = static_cast<addr_type>(Addr);
138}
139
Michael J. Spencer1a791612013-01-15 07:44:25 +0000140template<class ELFT>
141void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
142 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +0000143
144 Elf_Sym *sym = const_cast<Elf_Sym*>(
Michael J. Spencer1a791612013-01-15 07:44:25 +0000145 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
Preston Gurdcc31af92012-04-16 22:12:58 +0000146
147 // This assumes the address passed in matches the target address bitness
148 // The template-based type cast handles everything else.
149 sym->st_value = static_cast<addr_type>(Addr);
150}
151
152} // namespace
153
Eli Bendersky4c647582012-01-16 08:56:09 +0000154namespace llvm {
155
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000156void RuntimeDyldELF::registerEHFrames() {
157 if (!MemMgr)
158 return;
159 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
160 SID EHFrameSID = UnregisteredEHFrameSections[i];
161 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
162 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
163 size_t EHFrameSize = Sections[EHFrameSID].Size;
164 MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
Andrew Kaylorc442a762013-10-16 00:14:21 +0000165 RegisteredEHFrameSections.push_back(EHFrameSID);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000166 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000167 UnregisteredEHFrameSections.clear();
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000168}
169
Andrew Kaylorc442a762013-10-16 00:14:21 +0000170void RuntimeDyldELF::deregisterEHFrames() {
171 if (!MemMgr)
172 return;
173 for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
174 SID EHFrameSID = RegisteredEHFrameSections[i];
175 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
176 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
177 size_t EHFrameSize = Sections[EHFrameSID].Size;
178 MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
179 }
180 RegisteredEHFrameSections.clear();
181}
182
Lang Hames173c69f2014-01-08 04:09:09 +0000183ObjectImage *RuntimeDyldELF::createObjectImageFromFile(object::ObjectFile *ObjFile) {
184 if (!ObjFile)
185 return NULL;
186
187 error_code ec;
188 MemoryBuffer* Buffer = MemoryBuffer::getMemBuffer(ObjFile->getData(),
189 "",
190 false);
191
192 if (ObjFile->getBytesInAddress() == 4 && ObjFile->isLittleEndian()) {
193 DyldELFObject<ELFType<support::little, 2, false> > *Obj =
194 new DyldELFObject<ELFType<support::little, 2, false> >(Buffer, ec);
195 return new ELFObjectImage<ELFType<support::little, 2, false> >(NULL, Obj);
196 }
197 else if (ObjFile->getBytesInAddress() == 4 && !ObjFile->isLittleEndian()) {
198 DyldELFObject<ELFType<support::big, 2, false> > *Obj =
199 new DyldELFObject<ELFType<support::big, 2, false> >(Buffer, ec);
200 return new ELFObjectImage<ELFType<support::big, 2, false> >(NULL, Obj);
201 }
202 else if (ObjFile->getBytesInAddress() == 8 && !ObjFile->isLittleEndian()) {
203 DyldELFObject<ELFType<support::big, 2, true> > *Obj =
204 new DyldELFObject<ELFType<support::big, 2, true> >(Buffer, ec);
205 return new ELFObjectImage<ELFType<support::big, 2, true> >(NULL, Obj);
206 }
207 else if (ObjFile->getBytesInAddress() == 8 && ObjFile->isLittleEndian()) {
208 DyldELFObject<ELFType<support::little, 2, true> > *Obj =
209 new DyldELFObject<ELFType<support::little, 2, true> >(Buffer, ec);
210 return new ELFObjectImage<ELFType<support::little, 2, true> >(NULL, Obj);
211 }
212 else
213 llvm_unreachable("Unexpected ELF format");
214}
215
Andrew Kayloradc70562012-10-02 21:18:39 +0000216ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
217 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
218 llvm_unreachable("Unexpected ELF object size");
219 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
220 (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
221 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
Preston Gurdcc31af92012-04-16 22:12:58 +0000222 error_code ec;
223
224 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
Michael J. Spencer1a791612013-01-15 07:44:25 +0000225 DyldELFObject<ELFType<support::little, 4, false> > *Obj =
226 new DyldELFObject<ELFType<support::little, 4, false> >(
227 Buffer->getMemBuffer(), ec);
228 return new ELFObjectImage<ELFType<support::little, 4, false> >(Buffer, Obj);
Preston Gurdcc31af92012-04-16 22:12:58 +0000229 }
230 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
Michael J. Spencer1a791612013-01-15 07:44:25 +0000231 DyldELFObject<ELFType<support::big, 4, false> > *Obj =
232 new DyldELFObject<ELFType<support::big, 4, false> >(
233 Buffer->getMemBuffer(), ec);
234 return new ELFObjectImage<ELFType<support::big, 4, false> >(Buffer, Obj);
Preston Gurdcc31af92012-04-16 22:12:58 +0000235 }
236 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
Michael J. Spencer1a791612013-01-15 07:44:25 +0000237 DyldELFObject<ELFType<support::big, 8, true> > *Obj =
238 new DyldELFObject<ELFType<support::big, 8, true> >(
239 Buffer->getMemBuffer(), ec);
240 return new ELFObjectImage<ELFType<support::big, 8, true> >(Buffer, Obj);
Preston Gurdcc31af92012-04-16 22:12:58 +0000241 }
242 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
Michael J. Spencer1a791612013-01-15 07:44:25 +0000243 DyldELFObject<ELFType<support::little, 8, true> > *Obj =
244 new DyldELFObject<ELFType<support::little, 8, true> >(
245 Buffer->getMemBuffer(), ec);
246 return new ELFObjectImage<ELFType<support::little, 8, true> >(Buffer, Obj);
Preston Gurdcc31af92012-04-16 22:12:58 +0000247 }
248 else
249 llvm_unreachable("Unexpected ELF format");
250}
251
Preston Gurdcc31af92012-04-16 22:12:58 +0000252RuntimeDyldELF::~RuntimeDyldELF() {
Preston Gurdcc31af92012-04-16 22:12:58 +0000253}
Eli Bendersky4c647582012-01-16 08:56:09 +0000254
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000255void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
256 uint64_t Offset,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000257 uint64_t Value,
258 uint32_t Type,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000259 int64_t Addend,
260 uint64_t SymOffset) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000261 switch (Type) {
262 default:
263 llvm_unreachable("Relocation type not implemented yet!");
264 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000265 case ELF::R_X86_64_64: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000266 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000267 *Target = Value + Addend;
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000268 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend))
269 << " at " << format("%p\n",Target));
Eli Bendersky4c647582012-01-16 08:56:09 +0000270 break;
271 }
272 case ELF::R_X86_64_32:
273 case ELF::R_X86_64_32S: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000274 Value += Addend;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000275 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000276 (Type == ELF::R_X86_64_32S &&
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000277 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Bendersky4c647582012-01-16 08:56:09 +0000278 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000279 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
Eli Bendersky4c647582012-01-16 08:56:09 +0000280 *Target = TruncatedAddr;
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000281 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr)
282 << " at " << format("%p\n",Target));
Eli Bendersky4c647582012-01-16 08:56:09 +0000283 break;
284 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000285 case ELF::R_X86_64_GOTPCREL: {
286 // findGOTEntry returns the 'G + GOT' part of the relocation calculation
287 // based on the load/target address of the GOT (not the current/local addr).
288 uint64_t GOTAddr = findGOTEntry(Value, SymOffset);
289 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
290 uint64_t FinalAddress = Section.LoadAddress + Offset;
291 // The processRelocationRef method combines the symbol offset and the addend
292 // and in most cases that's what we want. For this relocation type, we need
293 // the raw addend, so we subtract the symbol offset to get it.
294 int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress;
295 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
296 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
297 *Target = TruncOffset;
298 break;
299 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000300 case ELF::R_X86_64_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000301 // Get the placeholder value from the generated object since
302 // a previous relocation attempt may have overwritten the loaded version
303 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
304 + Offset);
305 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
306 uint64_t FinalAddress = Section.LoadAddress + Offset;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000307 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000308 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000309 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000310 *Target = TruncOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000311 break;
312 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000313 case ELF::R_X86_64_PC64: {
314 // Get the placeholder value from the generated object since
315 // a previous relocation attempt may have overwritten the loaded version
316 uint64_t *Placeholder = reinterpret_cast<uint64_t*>(Section.ObjAddress
317 + Offset);
318 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
319 uint64_t FinalAddress = Section.LoadAddress + Offset;
320 *Target = *Placeholder + Value + Addend - FinalAddress;
321 break;
322 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000323 }
324}
325
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000326void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
327 uint64_t Offset,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000328 uint32_t Value,
329 uint32_t Type,
330 int32_t Addend) {
331 switch (Type) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000332 case ELF::R_386_32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000333 // Get the placeholder value from the generated object since
334 // a previous relocation attempt may have overwritten the loaded version
335 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
336 + Offset);
337 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
338 *Target = *Placeholder + Value + Addend;
Eli Bendersky4c647582012-01-16 08:56:09 +0000339 break;
340 }
341 case ELF::R_386_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000342 // Get the placeholder value from the generated object since
343 // a previous relocation attempt may have overwritten the loaded version
344 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
345 + Offset);
346 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
347 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000348 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000349 *Target = RealOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000350 break;
351 }
352 default:
353 // There are other relocation types, but it appears these are the
Andrew Kaylor782d5c42012-07-27 18:39:47 +0000354 // only ones currently used by the LLVM ELF object writer
Craig Toppera2886c22012-02-07 05:05:23 +0000355 llvm_unreachable("Relocation type not implemented yet!");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000356 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000357 }
358}
359
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000360void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
361 uint64_t Offset,
362 uint64_t Value,
363 uint32_t Type,
364 int64_t Addend) {
365 uint32_t *TargetPtr = reinterpret_cast<uint32_t*>(Section.Address + Offset);
366 uint64_t FinalAddress = Section.LoadAddress + Offset;
367
368 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
369 << format("%llx", Section.Address + Offset)
370 << " FinalAddress: 0x" << format("%llx",FinalAddress)
371 << " Value: 0x" << format("%llx",Value)
372 << " Type: 0x" << format("%x",Type)
373 << " Addend: 0x" << format("%llx",Addend)
374 << "\n");
375
376 switch (Type) {
377 default:
378 llvm_unreachable("Relocation type not implemented yet!");
379 break;
Tim Northoverb23d8db2013-05-04 20:14:14 +0000380 case ELF::R_AARCH64_ABS64: {
381 uint64_t *TargetPtr = reinterpret_cast<uint64_t*>(Section.Address + Offset);
382 *TargetPtr = Value + Addend;
383 break;
384 }
Tim Northover5959ea32013-05-19 15:39:03 +0000385 case ELF::R_AARCH64_PREL32: {
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000386 uint64_t Result = Value + Addend - FinalAddress;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000387 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000388 static_cast<int64_t>(Result) <= UINT32_MAX);
389 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
390 break;
391 }
Tim Northover37cde972013-05-04 20:14:09 +0000392 case ELF::R_AARCH64_CALL26: // fallthrough
393 case ELF::R_AARCH64_JUMP26: {
394 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
395 // calculation.
396 uint64_t BranchImm = Value + Addend - FinalAddress;
397
398 // "Check that -2^27 <= result < 2^27".
Michael J. Spencer126973b2013-08-08 22:27:13 +0000399 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
Tim Northover37cde972013-05-04 20:14:09 +0000400 static_cast<int64_t>(BranchImm) < (1LL << 27));
Tim Northover5959ea32013-05-19 15:39:03 +0000401
402 // AArch64 code is emitted with .rela relocations. The data already in any
403 // bits affected by the relocation on entry is garbage.
404 *TargetPtr &= 0xfc000000U;
Tim Northover37cde972013-05-04 20:14:09 +0000405 // Immediate goes in bits 25:0 of B and BL.
406 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
407 break;
408 }
Tim Northover4d01c1e2013-05-04 20:14:04 +0000409 case ELF::R_AARCH64_MOVW_UABS_G3: {
410 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000411
412 // AArch64 code is emitted with .rela relocations. The data already in any
413 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000414 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000415 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
416 *TargetPtr |= Result >> (48 - 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000417 // Shift must be "lsl #48", in bits 22:21
418 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000419 break;
420 }
421 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
422 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000423
Tim Northover5959ea32013-05-19 15:39:03 +0000424 // AArch64 code is emitted with .rela relocations. The data already in any
425 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000426 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000427 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
428 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000429 // Shift must be "lsl #32", in bits 22:21
430 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000431 break;
432 }
433 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
434 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000435
436 // AArch64 code is emitted with .rela relocations. The data already in any
437 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000438 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000439 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
440 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000441 // Shift must be "lsl #16", in bits 22:2
442 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000443 break;
444 }
445 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
446 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000447
448 // AArch64 code is emitted with .rela relocations. The data already in any
449 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000450 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000451 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
452 *TargetPtr |= ((Result & 0xffffU) << 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000453 // Shift must be "lsl #0", in bits 22:21.
454 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000455 break;
456 }
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000457 }
458}
459
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000460void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
461 uint64_t Offset,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000462 uint32_t Value,
463 uint32_t Type,
464 int32_t Addend) {
465 // TODO: Add Thumb relocations.
Tim Northover3b684d82013-05-28 19:48:19 +0000466 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress +
467 Offset);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000468 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
469 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000470 Value += Addend;
471
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000472 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
473 << Section.Address + Offset
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000474 << " FinalAddress: " << format("%p",FinalAddress)
475 << " Value: " << format("%x",Value)
476 << " Type: " << format("%x",Type)
477 << " Addend: " << format("%x",Addend)
478 << "\n");
479
480 switch(Type) {
481 default:
482 llvm_unreachable("Not implemented relocation type!");
483
Renato Golin8cea6e82014-01-29 11:50:56 +0000484 case ELF::R_ARM_NONE:
485 break;
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000486 // Write a 32bit value to relocation address, taking into account the
Tim Northover471cbb72012-10-03 16:29:42 +0000487 // implicit addend encoded in the target.
Renato Golin8cea6e82014-01-29 11:50:56 +0000488 case ELF::R_ARM_PREL31:
Tim Northover3b684d82013-05-28 19:48:19 +0000489 case ELF::R_ARM_TARGET1:
490 case ELF::R_ARM_ABS32:
491 *TargetPtr = *Placeholder + Value;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000492 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000493 // Write first 16 bit of 32 bit value to the mov instruction.
494 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000495 case ELF::R_ARM_MOVW_ABS_NC:
Tim Northover471cbb72012-10-03 16:29:42 +0000496 // We are not expecting any other addend in the relocation address.
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000497 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover471cbb72012-10-03 16:29:42 +0000498 // non-contiguous fields.
Tim Northover3b684d82013-05-28 19:48:19 +0000499 assert((*Placeholder & 0x000F0FFF) == 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000500 Value = Value & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000501 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000502 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
503 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000504 // Write last 16 bit of 32 bit value to the mov instruction.
505 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000506 case ELF::R_ARM_MOVT_ABS:
Tim Northover471cbb72012-10-03 16:29:42 +0000507 // We are not expecting any other addend in the relocation address.
508 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
Tim Northover3b684d82013-05-28 19:48:19 +0000509 assert((*Placeholder & 0x000F0FFF) == 0);
510
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000511 Value = (Value >> 16) & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000512 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000513 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
514 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000515 // Write 24 bit relative value to the branch instruction.
516 case ELF::R_ARM_PC24 : // Fall through.
517 case ELF::R_ARM_CALL : // Fall through.
Tim Northover3b684d82013-05-28 19:48:19 +0000518 case ELF::R_ARM_JUMP24: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000519 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
520 RelValue = (RelValue & 0x03FFFFFC) >> 2;
Tim Northover3b684d82013-05-28 19:48:19 +0000521 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000522 *TargetPtr &= 0xFF000000;
523 *TargetPtr |= RelValue;
524 break;
525 }
Tim Northover3b684d82013-05-28 19:48:19 +0000526 case ELF::R_ARM_PRIVATE_0:
527 // This relocation is reserved by the ARM ELF ABI for internal use. We
528 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
529 // in the stubs created during JIT (which can't put an addend into the
530 // original object file).
531 *TargetPtr = Value;
532 break;
533 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000534}
535
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000536void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
537 uint64_t Offset,
Akira Hatanaka11dfbe12012-08-20 17:53:24 +0000538 uint32_t Value,
539 uint32_t Type,
540 int32_t Addend) {
Akira Hatanaka2e236242013-07-24 01:58:40 +0000541 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress +
542 Offset);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000543 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000544 Value += Addend;
545
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000546 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
547 << Section.Address + Offset
548 << " FinalAddress: "
549 << format("%p",Section.LoadAddress + Offset)
Akira Hatanaka111174b2012-08-17 21:28:04 +0000550 << " Value: " << format("%x",Value)
551 << " Type: " << format("%x",Type)
552 << " Addend: " << format("%x",Addend)
553 << "\n");
554
555 switch(Type) {
556 default:
557 llvm_unreachable("Not implemented relocation type!");
558 break;
559 case ELF::R_MIPS_32:
Akira Hatanaka2e236242013-07-24 01:58:40 +0000560 *TargetPtr = Value + (*Placeholder);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000561 break;
562 case ELF::R_MIPS_26:
Akira Hatanaka2e236242013-07-24 01:58:40 +0000563 *TargetPtr = ((*Placeholder) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000564 break;
565 case ELF::R_MIPS_HI16:
566 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanaka2e236242013-07-24 01:58:40 +0000567 Value += ((*Placeholder) & 0x0000ffff) << 16;
568 *TargetPtr = ((*Placeholder) & 0xffff0000) |
569 (((Value + 0x8000) >> 16) & 0xffff);
570 break;
571 case ELF::R_MIPS_LO16:
572 Value += ((*Placeholder) & 0x0000ffff);
573 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
574 break;
575 case ELF::R_MIPS_UNUSED1:
576 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
577 // are used for internal JIT purpose. These relocations are similar to
578 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
579 // account.
Akira Hatanaka111174b2012-08-17 21:28:04 +0000580 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
581 (((Value + 0x8000) >> 16) & 0xffff);
582 break;
Akira Hatanaka2e236242013-07-24 01:58:40 +0000583 case ELF::R_MIPS_UNUSED2:
Akira Hatanaka111174b2012-08-17 21:28:04 +0000584 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
585 break;
586 }
587}
588
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000589// Return the .TOC. section address to R_PPC64_TOC relocations.
590uint64_t RuntimeDyldELF::findPPC64TOC() const {
591 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
592 // order. The TOC starts where the first of these sections starts.
593 SectionList::const_iterator it = Sections.begin();
594 SectionList::const_iterator ite = Sections.end();
595 for (; it != ite; ++it) {
596 if (it->Name == ".got" ||
597 it->Name == ".toc" ||
598 it->Name == ".tocbss" ||
599 it->Name == ".plt")
600 break;
601 }
602 if (it == ite) {
603 // This may happen for
604 // * references to TOC base base (sym@toc, .odp relocation) without
605 // a .toc directive.
606 // In this case just use the first section (which is usually
607 // the .odp) since the code won't reference the .toc base
608 // directly.
609 it = Sections.begin();
610 }
611 assert (it != ite);
612 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
613 // thus permitting a full 64 Kbytes segment.
614 return it->LoadAddress + 0x8000;
615}
616
617// Returns the sections and offset associated with the ODP entry referenced
618// by Symbol.
619void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
620 ObjSectionToIDMap &LocalSections,
621 RelocationValueRef &Rel) {
622 // Get the ELF symbol value (st_value) to compare with Relocation offset in
623 // .opd entries
624
625 error_code err;
626 for (section_iterator si = Obj.begin_sections(),
627 se = Obj.end_sections(); si != se; si.increment(err)) {
Rafael Espindolaa61f1e92013-06-03 19:37:34 +0000628 section_iterator RelSecI = si->getRelocatedSection();
629 if (RelSecI == Obj.end_sections())
630 continue;
631
632 StringRef RelSectionName;
633 check(RelSecI->getName(RelSectionName));
634 if (RelSectionName != ".opd")
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000635 continue;
636
637 for (relocation_iterator i = si->begin_relocations(),
638 e = si->end_relocations(); i != e;) {
639 check(err);
640
641 // The R_PPC64_ADDR64 relocation indicates the first field
642 // of a .opd entry
643 uint64_t TypeFunc;
644 check(i->getType(TypeFunc));
645 if (TypeFunc != ELF::R_PPC64_ADDR64) {
646 i.increment(err);
647 continue;
648 }
649
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000650 uint64_t TargetSymbolOffset;
Rafael Espindola806f0062013-06-05 01:33:53 +0000651 symbol_iterator TargetSymbol = i->getSymbol();
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000652 check(i->getOffset(TargetSymbolOffset));
Rafael Espindola0d15f732013-05-09 03:39:05 +0000653 int64_t Addend;
654 check(getELFRelocationAddend(*i, Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000655
656 i = i.increment(err);
657 if (i == e)
658 break;
659 check(err);
660
661 // Just check if following relocation is a R_PPC64_TOC
662 uint64_t TypeTOC;
663 check(i->getType(TypeTOC));
664 if (TypeTOC != ELF::R_PPC64_TOC)
665 continue;
666
667 // Finally compares the Symbol value and the target symbol offset
668 // to check if this .opd entry refers to the symbol the relocation
669 // points to.
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000670 if (Rel.Addend != (int64_t)TargetSymbolOffset)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000671 continue;
672
673 section_iterator tsi(Obj.end_sections());
Rafael Espindola806f0062013-06-05 01:33:53 +0000674 check(TargetSymbol->getSection(tsi));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000675 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
Rafael Espindola0d15f732013-05-09 03:39:05 +0000676 Rel.Addend = (intptr_t)Addend;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000677 return;
678 }
679 }
680 llvm_unreachable("Attempting to get address of ODP entry!");
681}
682
683// Relocation masks following the #lo(value), #hi(value), #higher(value),
684// and #highest(value) macros defined in section 4.5.1. Relocation Types
685// in PPC-elf64abi document.
686//
687static inline
688uint16_t applyPPClo (uint64_t value)
689{
690 return value & 0xffff;
691}
692
693static inline
694uint16_t applyPPChi (uint64_t value)
695{
696 return (value >> 16) & 0xffff;
697}
698
699static inline
700uint16_t applyPPChigher (uint64_t value)
701{
702 return (value >> 32) & 0xffff;
703}
704
705static inline
706uint16_t applyPPChighest (uint64_t value)
707{
708 return (value >> 48) & 0xffff;
709}
710
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000711void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
712 uint64_t Offset,
713 uint64_t Value,
714 uint32_t Type,
715 int64_t Addend) {
716 uint8_t* LocalAddress = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000717 switch (Type) {
718 default:
719 llvm_unreachable("Relocation type not implemented yet!");
720 break;
721 case ELF::R_PPC64_ADDR16_LO :
722 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
723 break;
724 case ELF::R_PPC64_ADDR16_HI :
725 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
726 break;
727 case ELF::R_PPC64_ADDR16_HIGHER :
728 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
729 break;
730 case ELF::R_PPC64_ADDR16_HIGHEST :
731 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
732 break;
733 case ELF::R_PPC64_ADDR14 : {
734 assert(((Value + Addend) & 3) == 0);
735 // Preserve the AA/LK bits in the branch instruction
736 uint8_t aalk = *(LocalAddress+3);
737 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
738 } break;
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000739 case ELF::R_PPC64_ADDR32 : {
740 int32_t Result = static_cast<int32_t>(Value + Addend);
741 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000742 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000743 writeInt32BE(LocalAddress, Result);
744 } break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000745 case ELF::R_PPC64_REL24 : {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000746 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000747 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
748 if (SignExtend32<24>(delta) != delta)
749 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
750 // Generates a 'bl <address>' instruction
751 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
752 } break;
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000753 case ELF::R_PPC64_REL32 : {
754 uint64_t FinalAddress = (Section.LoadAddress + Offset);
755 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
756 if (SignExtend32<32>(delta) != delta)
757 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
758 writeInt32BE(LocalAddress, delta);
759 } break;
Adhemerval Zanellae8bd03d2013-05-06 17:21:23 +0000760 case ELF::R_PPC64_REL64: {
761 uint64_t FinalAddress = (Section.LoadAddress + Offset);
762 uint64_t Delta = Value - FinalAddress + Addend;
763 writeInt64BE(LocalAddress, Delta);
764 } break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000765 case ELF::R_PPC64_ADDR64 :
766 writeInt64BE(LocalAddress, Value + Addend);
767 break;
768 case ELF::R_PPC64_TOC :
769 writeInt64BE(LocalAddress, findPPC64TOC());
770 break;
771 case ELF::R_PPC64_TOC16 : {
772 uint64_t TOCStart = findPPC64TOC();
773 Value = applyPPClo((Value + Addend) - TOCStart);
774 writeInt16BE(LocalAddress, applyPPClo(Value));
775 } break;
776 case ELF::R_PPC64_TOC16_DS : {
777 uint64_t TOCStart = findPPC64TOC();
778 Value = ((Value + Addend) - TOCStart);
779 writeInt16BE(LocalAddress, applyPPClo(Value));
780 } break;
781 }
782}
783
Richard Sandifordca044082013-05-03 14:15:35 +0000784void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
785 uint64_t Offset,
786 uint64_t Value,
787 uint32_t Type,
788 int64_t Addend) {
789 uint8_t *LocalAddress = Section.Address + Offset;
790 switch (Type) {
791 default:
792 llvm_unreachable("Relocation type not implemented yet!");
793 break;
794 case ELF::R_390_PC16DBL:
795 case ELF::R_390_PLT16DBL: {
796 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
797 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
798 writeInt16BE(LocalAddress, Delta / 2);
799 break;
800 }
801 case ELF::R_390_PC32DBL:
802 case ELF::R_390_PLT32DBL: {
803 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
804 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
805 writeInt32BE(LocalAddress, Delta / 2);
806 break;
807 }
808 case ELF::R_390_PC32: {
809 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
810 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
811 writeInt32BE(LocalAddress, Delta);
812 break;
813 }
814 case ELF::R_390_64:
815 writeInt64BE(LocalAddress, Value + Addend);
816 break;
817 }
818}
819
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000820// The target location for the relocation is described by RE.SectionID and
821// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
822// SectionEntry has three members describing its location.
823// SectionEntry::Address is the address at which the section has been loaded
824// into memory in the current (host) process. SectionEntry::LoadAddress is the
825// address that the section will have in the target process.
826// SectionEntry::ObjAddress is the address of the bits for this section in the
827// original emitted object image (also in the current address space).
828//
829// Relocations will be applied as if the section were loaded at
830// SectionEntry::LoadAddress, but they will be applied at an address based
831// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
832// Target memory contents if they are required for value calculations.
833//
834// The Value parameter here is the load address of the symbol for the
835// relocation to be applied. For relocations which refer to symbols in the
836// current object Value will be the LoadAddress of the section in which
837// the symbol resides (RE.Addend provides additional information about the
838// symbol location). For external symbols, Value will be the address of the
839// symbol in the target address space.
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000840void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000841 uint64_t Value) {
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000842 const SectionEntry &Section = Sections[RE.SectionID];
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000843 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
844 RE.SymOffset);
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000845}
846
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000847void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
848 uint64_t Offset,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000849 uint64_t Value,
850 uint32_t Type,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000851 int64_t Addend,
852 uint64_t SymOffset) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000853 switch (Arch) {
854 case Triple::x86_64:
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000855 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
Eli Bendersky4c647582012-01-16 08:56:09 +0000856 break;
857 case Triple::x86:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000858 resolveX86Relocation(Section, Offset,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000859 (uint32_t)(Value & 0xffffffffL), Type,
860 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000861 break;
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000862 case Triple::aarch64:
863 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
864 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000865 case Triple::arm: // Fall through.
866 case Triple::thumb:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000867 resolveARMRelocation(Section, Offset,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000868 (uint32_t)(Value & 0xffffffffL), Type,
869 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000870 break;
Akira Hatanaka111174b2012-08-17 21:28:04 +0000871 case Triple::mips: // Fall through.
872 case Triple::mipsel:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000873 resolveMIPSRelocation(Section, Offset,
Akira Hatanaka11dfbe12012-08-20 17:53:24 +0000874 (uint32_t)(Value & 0xffffffffL), Type,
875 (uint32_t)(Addend & 0xffffffffL));
Akira Hatanaka111174b2012-08-17 21:28:04 +0000876 break;
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000877 case Triple::ppc64: // Fall through.
878 case Triple::ppc64le:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000879 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000880 break;
Richard Sandifordca044082013-05-03 14:15:35 +0000881 case Triple::systemz:
882 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
883 break;
Craig Toppera2886c22012-02-07 05:05:23 +0000884 default: llvm_unreachable("Unsupported CPU type!");
Eli Bendersky4c647582012-01-16 08:56:09 +0000885 }
886}
887
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000888void RuntimeDyldELF::processRelocationRef(unsigned SectionID,
Rafael Espindola37008942013-04-29 19:03:21 +0000889 RelocationRef RelI,
Preston Gurdcc31af92012-04-16 22:12:58 +0000890 ObjectImage &Obj,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000891 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyfc079082012-05-01 06:58:59 +0000892 const SymbolTableMap &Symbols,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000893 StubMap &Stubs) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000894 uint64_t RelType;
Rafael Espindola37008942013-04-29 19:03:21 +0000895 Check(RelI.getType(RelType));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000896 int64_t Addend;
Rafael Espindola0d15f732013-05-09 03:39:05 +0000897 Check(getELFRelocationAddend(RelI, Addend));
Rafael Espindola806f0062013-06-05 01:33:53 +0000898 symbol_iterator Symbol = RelI.getSymbol();
Eli Bendersky667b8792012-05-01 10:41:12 +0000899
900 // Obtain the symbol name which is referenced in the relocation
901 StringRef TargetName;
Rafael Espindola75954472013-06-05 02:55:01 +0000902 if (Symbol != Obj.end_symbols())
903 Symbol->getName(TargetName);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000904 DEBUG(dbgs() << "\t\tRelType: " << RelType
905 << " Addend: " << Addend
906 << " TargetName: " << TargetName
907 << "\n");
Eli Bendersky667b8792012-05-01 10:41:12 +0000908 RelocationValueRef Value;
909 // First search for the symbol in the local symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000910 SymbolTableMap::const_iterator lsi = Symbols.end();
911 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
912 if (Symbol != Obj.end_symbols()) {
913 lsi = Symbols.find(TargetName.data());
914 Symbol->getType(SymType);
915 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000916 if (lsi != Symbols.end()) {
917 Value.SectionID = lsi->second.first;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000918 Value.Offset = lsi->second.second;
Ulrich Weigand78e97652013-04-05 13:29:04 +0000919 Value.Addend = lsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000920 } else {
Eli Bendersky667b8792012-05-01 10:41:12 +0000921 // Search for the symbol in the global symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000922 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end();
923 if (Symbol != Obj.end_symbols())
924 gsi = GlobalSymbolTable.find(TargetName.data());
Eli Benderskyfc079082012-05-01 06:58:59 +0000925 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000926 Value.SectionID = gsi->second.first;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000927 Value.Offset = gsi->second.second;
Ulrich Weigand78e97652013-04-05 13:29:04 +0000928 Value.Addend = gsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000929 } else {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000930 switch (SymType) {
931 case SymbolRef::ST_Debug: {
932 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
933 // and can be changed by another developers. Maybe best way is add
934 // a new symbol type ST_Section to SymbolRef and use it.
Eli Bendersky667b8792012-05-01 10:41:12 +0000935 section_iterator si(Obj.end_sections());
Rafael Espindola806f0062013-06-05 01:33:53 +0000936 Symbol->getSection(si);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000937 if (si == Obj.end_sections())
938 llvm_unreachable("Symbol section not found, bad object file format!");
939 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Andrew Kaylor47328722012-10-12 23:53:16 +0000940 // Default to 'true' in case isText fails (though it never does).
941 bool isCode = true;
942 si->isText(isCode);
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000943 Value.SectionID = findOrEmitSection(Obj,
944 (*si),
945 isCode,
Andrew Kaylor47328722012-10-12 23:53:16 +0000946 ObjSectionToID);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000947 Value.Addend = Addend;
948 break;
949 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000950 case SymbolRef::ST_Data:
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000951 case SymbolRef::ST_Unknown: {
952 Value.SymbolName = TargetName.data();
953 Value.Addend = Addend;
Richard Mittonad6d3492013-08-16 18:54:26 +0000954
955 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
956 // will manifest here as a NULL symbol name.
957 // We can set this as a valid (but empty) symbol name, and rely
958 // on addRelocationForSymbol to handle this.
959 if (!Value.SymbolName)
960 Value.SymbolName = "";
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000961 break;
962 }
963 default:
964 llvm_unreachable("Unresolved symbol type!");
965 break;
966 }
967 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000968 }
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000969 uint64_t Offset;
Rafael Espindola37008942013-04-29 19:03:21 +0000970 Check(RelI.getOffset(Offset));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000971
972 DEBUG(dbgs() << "\t\tSectionID: " << SectionID
973 << " Offset: " << Offset
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000974 << "\n");
Tim Northover37cde972013-05-04 20:14:09 +0000975 if (Arch == Triple::aarch64 &&
976 (RelType == ELF::R_AARCH64_CALL26 ||
977 RelType == ELF::R_AARCH64_JUMP26)) {
978 // This is an AArch64 branch relocation, need to use a stub function.
979 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
980 SectionEntry &Section = Sections[SectionID];
981
982 // Look for an existing stub.
983 StubMap::const_iterator i = Stubs.find(Value);
984 if (i != Stubs.end()) {
985 resolveRelocation(Section, Offset,
986 (uint64_t)Section.Address + i->second, RelType, 0);
987 DEBUG(dbgs() << " Stub function found\n");
988 } else {
989 // Create a new stub function.
990 DEBUG(dbgs() << " Create a new stub function\n");
991 Stubs[Value] = Section.StubOffset;
992 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
993 Section.StubOffset);
994
995 RelocationEntry REmovz_g3(SectionID,
996 StubTargetAddr - Section.Address,
997 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
998 RelocationEntry REmovk_g2(SectionID,
999 StubTargetAddr - Section.Address + 4,
1000 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
1001 RelocationEntry REmovk_g1(SectionID,
1002 StubTargetAddr - Section.Address + 8,
1003 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
1004 RelocationEntry REmovk_g0(SectionID,
1005 StubTargetAddr - Section.Address + 12,
1006 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
1007
1008 if (Value.SymbolName) {
1009 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1010 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1011 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1012 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1013 } else {
1014 addRelocationForSection(REmovz_g3, Value.SectionID);
1015 addRelocationForSection(REmovk_g2, Value.SectionID);
1016 addRelocationForSection(REmovk_g1, Value.SectionID);
1017 addRelocationForSection(REmovk_g0, Value.SectionID);
1018 }
1019 resolveRelocation(Section, Offset,
1020 (uint64_t)Section.Address + Section.StubOffset,
1021 RelType, 0);
1022 Section.StubOffset += getMaxStubSize();
1023 }
1024 } else if (Arch == Triple::arm &&
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001025 (RelType == ELF::R_ARM_PC24 ||
1026 RelType == ELF::R_ARM_CALL ||
1027 RelType == ELF::R_ARM_JUMP24)) {
1028 // This is an ARM branch relocation, need to use a stub function.
1029 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001030 SectionEntry &Section = Sections[SectionID];
Eli Bendersky4c647582012-01-16 08:56:09 +00001031
Eric Christopherc33f6222012-10-23 17:19:15 +00001032 // Look for an existing stub.
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001033 StubMap::const_iterator i = Stubs.find(Value);
1034 if (i != Stubs.end()) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001035 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001036 (uint64_t)Section.Address + i->second, RelType, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001037 DEBUG(dbgs() << " Stub function found\n");
1038 } else {
1039 // Create a new stub function.
1040 DEBUG(dbgs() << " Create a new stub function\n");
1041 Stubs[Value] = Section.StubOffset;
1042 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1043 Section.StubOffset);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001044 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Tim Northover3b684d82013-05-28 19:48:19 +00001045 ELF::R_ARM_PRIVATE_0, Value.Addend);
Eli Bendersky667b8792012-05-01 10:41:12 +00001046 if (Value.SymbolName)
1047 addRelocationForSymbol(RE, Value.SymbolName);
1048 else
1049 addRelocationForSection(RE, Value.SectionID);
1050
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001051 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001052 (uint64_t)Section.Address + Section.StubOffset,
1053 RelType, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001054 Section.StubOffset += getMaxStubSize();
1055 }
Akira Hatanakaa667aad2012-12-03 23:12:19 +00001056 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
1057 RelType == ELF::R_MIPS_26) {
Akira Hatanaka111174b2012-08-17 21:28:04 +00001058 // This is an Mips branch relocation, need to use a stub function.
1059 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001060 SectionEntry &Section = Sections[SectionID];
1061 uint8_t *Target = Section.Address + Offset;
Akira Hatanaka111174b2012-08-17 21:28:04 +00001062 uint32_t *TargetAddress = (uint32_t *)Target;
1063
1064 // Extract the addend from the instruction.
1065 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
1066
1067 Value.Addend += Addend;
1068
1069 // Look up for existing stub.
1070 StubMap::const_iterator i = Stubs.find(Value);
1071 if (i != Stubs.end()) {
Petar Jovanovic45115f82013-11-19 21:56:00 +00001072 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1073 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001074 DEBUG(dbgs() << " Stub function found\n");
1075 } else {
1076 // Create a new stub function.
1077 DEBUG(dbgs() << " Create a new stub function\n");
1078 Stubs[Value] = Section.StubOffset;
1079 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1080 Section.StubOffset);
1081
1082 // Creating Hi and Lo relocations for the filled stub instructions.
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001083 RelocationEntry REHi(SectionID,
Akira Hatanaka111174b2012-08-17 21:28:04 +00001084 StubTargetAddr - Section.Address,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001085 ELF::R_MIPS_UNUSED1, Value.Addend);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001086 RelocationEntry RELo(SectionID,
Akira Hatanaka111174b2012-08-17 21:28:04 +00001087 StubTargetAddr - Section.Address + 4,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001088 ELF::R_MIPS_UNUSED2, Value.Addend);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001089
1090 if (Value.SymbolName) {
1091 addRelocationForSymbol(REHi, Value.SymbolName);
1092 addRelocationForSymbol(RELo, Value.SymbolName);
1093 } else {
1094 addRelocationForSection(REHi, Value.SectionID);
1095 addRelocationForSection(RELo, Value.SectionID);
1096 }
1097
Petar Jovanovic45115f82013-11-19 21:56:00 +00001098 RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset);
1099 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001100 Section.StubOffset += getMaxStubSize();
1101 }
Bill Schmidt0a9170d2013-07-26 01:35:43 +00001102 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001103 if (RelType == ELF::R_PPC64_REL24) {
1104 // A PPC branch relocation will need a stub function if the target is
1105 // an external symbol (Symbol::ST_Unknown) or if the target address
1106 // is not within the signed 24-bits branch address.
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001107 SectionEntry &Section = Sections[SectionID];
1108 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001109 bool RangeOverflow = false;
1110 if (SymType != SymbolRef::ST_Unknown) {
1111 // A function call may points to the .opd entry, so the final symbol value
1112 // in calculated based in the relocation values in .opd section.
1113 findOPDEntrySection(Obj, ObjSectionToID, Value);
1114 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
1115 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
1116 // If it is within 24-bits branch range, just set the branch target
1117 if (SignExtend32<24>(delta) == delta) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001118 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001119 if (Value.SymbolName)
1120 addRelocationForSymbol(RE, Value.SymbolName);
1121 else
1122 addRelocationForSection(RE, Value.SectionID);
1123 } else {
1124 RangeOverflow = true;
1125 }
1126 }
1127 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
1128 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1129 // larger than 24-bits.
1130 StubMap::const_iterator i = Stubs.find(Value);
1131 if (i != Stubs.end()) {
1132 // Symbol function stub already created, just relocate to it
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001133 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001134 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001135 DEBUG(dbgs() << " Stub function found\n");
1136 } else {
1137 // Create a new stub function.
1138 DEBUG(dbgs() << " Create a new stub function\n");
1139 Stubs[Value] = Section.StubOffset;
1140 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
1141 Section.StubOffset);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001142 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001143 ELF::R_PPC64_ADDR64, Value.Addend);
1144
1145 // Generates the 64-bits address loads as exemplified in section
1146 // 4.5.1 in PPC64 ELF ABI.
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001147 RelocationEntry REhst(SectionID,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001148 StubTargetAddr - Section.Address + 2,
1149 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001150 RelocationEntry REhr(SectionID,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001151 StubTargetAddr - Section.Address + 6,
1152 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001153 RelocationEntry REh(SectionID,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001154 StubTargetAddr - Section.Address + 14,
1155 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001156 RelocationEntry REl(SectionID,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001157 StubTargetAddr - Section.Address + 18,
1158 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1159
1160 if (Value.SymbolName) {
1161 addRelocationForSymbol(REhst, Value.SymbolName);
1162 addRelocationForSymbol(REhr, Value.SymbolName);
1163 addRelocationForSymbol(REh, Value.SymbolName);
1164 addRelocationForSymbol(REl, Value.SymbolName);
1165 } else {
1166 addRelocationForSection(REhst, Value.SectionID);
1167 addRelocationForSection(REhr, Value.SectionID);
1168 addRelocationForSection(REh, Value.SectionID);
1169 addRelocationForSection(REl, Value.SectionID);
1170 }
1171
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001172 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001173 (uint64_t)Section.Address + Section.StubOffset,
1174 RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001175 if (SymType == SymbolRef::ST_Unknown)
1176 // Restore the TOC for external calls
1177 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
1178 Section.StubOffset += getMaxStubSize();
1179 }
1180 }
1181 } else {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001182 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001183 // Extra check to avoid relocation againt empty symbols (usually
1184 // the R_PPC64_TOC).
Richard Mittonad6d3492013-08-16 18:54:26 +00001185 if (SymType != SymbolRef::ST_Unknown && TargetName.empty())
1186 Value.SymbolName = NULL;
1187
1188 if (Value.SymbolName)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001189 addRelocationForSymbol(RE, Value.SymbolName);
1190 else
1191 addRelocationForSection(RE, Value.SectionID);
1192 }
Richard Sandifordca044082013-05-03 14:15:35 +00001193 } else if (Arch == Triple::systemz &&
1194 (RelType == ELF::R_390_PLT32DBL ||
1195 RelType == ELF::R_390_GOTENT)) {
1196 // Create function stubs for both PLT and GOT references, regardless of
1197 // whether the GOT reference is to data or code. The stub contains the
1198 // full address of the symbol, as needed by GOT references, and the
1199 // executable part only adds an overhead of 8 bytes.
1200 //
1201 // We could try to conserve space by allocating the code and data
1202 // parts of the stub separately. However, as things stand, we allocate
1203 // a stub for every relocation, so using a GOT in JIT code should be
1204 // no less space efficient than using an explicit constant pool.
1205 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1206 SectionEntry &Section = Sections[SectionID];
1207
1208 // Look for an existing stub.
1209 StubMap::const_iterator i = Stubs.find(Value);
1210 uintptr_t StubAddress;
1211 if (i != Stubs.end()) {
1212 StubAddress = uintptr_t(Section.Address) + i->second;
1213 DEBUG(dbgs() << " Stub function found\n");
1214 } else {
1215 // Create a new stub function.
1216 DEBUG(dbgs() << " Create a new stub function\n");
1217
1218 uintptr_t BaseAddress = uintptr_t(Section.Address);
1219 uintptr_t StubAlignment = getStubAlignment();
1220 StubAddress = (BaseAddress + Section.StubOffset +
1221 StubAlignment - 1) & -StubAlignment;
1222 unsigned StubOffset = StubAddress - BaseAddress;
1223
1224 Stubs[Value] = StubOffset;
1225 createStubFunction((uint8_t *)StubAddress);
1226 RelocationEntry RE(SectionID, StubOffset + 8,
1227 ELF::R_390_64, Value.Addend - Addend);
1228 if (Value.SymbolName)
1229 addRelocationForSymbol(RE, Value.SymbolName);
1230 else
1231 addRelocationForSection(RE, Value.SectionID);
1232 Section.StubOffset = StubOffset + getMaxStubSize();
1233 }
1234
1235 if (RelType == ELF::R_390_GOTENT)
1236 resolveRelocation(Section, Offset, StubAddress + 8,
1237 ELF::R_390_PC32DBL, Addend);
1238 else
1239 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001240 } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) {
1241 // The way the PLT relocations normally work is that the linker allocates the
1242 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
1243 // entry will then jump to an address provided by the GOT. On first call, the
1244 // GOT address will point back into PLT code that resolves the symbol. After
1245 // the first call, the GOT entry points to the actual function.
1246 //
1247 // For local functions we're ignoring all of that here and just replacing
1248 // the PLT32 relocation type with PC32, which will translate the relocation
1249 // into a PC-relative call directly to the function. For external symbols we
1250 // can't be sure the function will be within 2^32 bytes of the call site, so
1251 // we need to create a stub, which calls into the GOT. This case is
1252 // equivalent to the usual PLT implementation except that we use the stub
1253 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1254 // rather than allocating a PLT section.
1255 if (Value.SymbolName) {
1256 // This is a call to an external function.
1257 // Look for an existing stub.
1258 SectionEntry &Section = Sections[SectionID];
1259 StubMap::const_iterator i = Stubs.find(Value);
1260 uintptr_t StubAddress;
1261 if (i != Stubs.end()) {
1262 StubAddress = uintptr_t(Section.Address) + i->second;
1263 DEBUG(dbgs() << " Stub function found\n");
1264 } else {
1265 // Create a new stub function (equivalent to a PLT entry).
1266 DEBUG(dbgs() << " Create a new stub function\n");
1267
1268 uintptr_t BaseAddress = uintptr_t(Section.Address);
1269 uintptr_t StubAlignment = getStubAlignment();
1270 StubAddress = (BaseAddress + Section.StubOffset +
1271 StubAlignment - 1) & -StubAlignment;
1272 unsigned StubOffset = StubAddress - BaseAddress;
1273 Stubs[Value] = StubOffset;
1274 createStubFunction((uint8_t *)StubAddress);
1275
1276 // Create a GOT entry for the external function.
1277 GOTEntries.push_back(Value);
1278
1279 // Make our stub function a relative call to the GOT entry.
1280 RelocationEntry RE(SectionID, StubOffset + 2,
1281 ELF::R_X86_64_GOTPCREL, -4);
1282 addRelocationForSymbol(RE, Value.SymbolName);
1283
1284 // Bump our stub offset counter
1285 Section.StubOffset = StubOffset + getMaxStubSize();
1286 }
1287
1288 // Make the target call a call into the stub table.
1289 resolveRelocation(Section, Offset, StubAddress,
1290 ELF::R_X86_64_PC32, Addend);
1291 } else {
1292 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
1293 Value.Offset);
1294 addRelocationForSection(RE, Value.SectionID);
1295 }
Eli Bendersky667b8792012-05-01 10:41:12 +00001296 } else {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001297 if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) {
1298 GOTEntries.push_back(Value);
1299 }
1300 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
Eli Bendersky667b8792012-05-01 10:41:12 +00001301 if (Value.SymbolName)
1302 addRelocationForSymbol(RE, Value.SymbolName);
1303 else
1304 addRelocationForSection(RE, Value.SectionID);
1305 }
Jim Grosbacheff0a402012-01-16 22:26:39 +00001306}
1307
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001308void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001309
1310 SmallVectorImpl<std::pair<SID, GOTRelocations> >::iterator it;
1311 SmallVectorImpl<std::pair<SID, GOTRelocations> >::iterator end = GOTs.end();
1312
1313 for (it = GOTs.begin(); it != end; ++it) {
1314 GOTRelocations &GOTEntries = it->second;
1315 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
1316 if (GOTEntries[i].SymbolName != 0 && GOTEntries[i].SymbolName == Name) {
1317 GOTEntries[i].Offset = Addr;
1318 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001319 }
1320 }
1321}
1322
1323size_t RuntimeDyldELF::getGOTEntrySize() {
1324 // We don't use the GOT in all of these cases, but it's essentially free
1325 // to put them all here.
1326 size_t Result = 0;
1327 switch (Arch) {
1328 case Triple::x86_64:
1329 case Triple::aarch64:
1330 case Triple::ppc64:
1331 case Triple::ppc64le:
1332 case Triple::systemz:
1333 Result = sizeof(uint64_t);
1334 break;
1335 case Triple::x86:
1336 case Triple::arm:
1337 case Triple::thumb:
1338 case Triple::mips:
1339 case Triple::mipsel:
1340 Result = sizeof(uint32_t);
1341 break;
1342 default: llvm_unreachable("Unsupported CPU type!");
1343 }
1344 return Result;
1345}
1346
1347uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress,
1348 uint64_t Offset) {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001349
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001350 const size_t GOTEntrySize = getGOTEntrySize();
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001351
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001352 SmallVectorImpl<std::pair<SID, GOTRelocations> >::const_iterator it;
1353 SmallVectorImpl<std::pair<SID, GOTRelocations> >::const_iterator end = GOTs.end();
1354
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001355 int GOTIndex = -1;
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001356 for (it = GOTs.begin(); it != end; ++it) {
1357 SID GOTSectionID = it->first;
1358 const GOTRelocations &GOTEntries = it->second;
1359
1360 // Find the matching entry in our vector.
1361 uint64_t SymbolOffset = 0;
1362 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
1363 if (GOTEntries[i].SymbolName == 0) {
1364 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress &&
1365 GOTEntries[i].Offset == Offset) {
1366 GOTIndex = i;
1367 SymbolOffset = GOTEntries[i].Offset;
1368 break;
1369 }
1370 } else {
1371 // GOT entries for external symbols use the addend as the address when
1372 // the external symbol has been resolved.
1373 if (GOTEntries[i].Offset == LoadAddress) {
1374 GOTIndex = i;
1375 // Don't use the Addend here. The relocation handler will use it.
1376 break;
1377 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001378 }
1379 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001380
1381 if (GOTIndex != -1) {
1382 if (GOTEntrySize == sizeof(uint64_t)) {
1383 uint64_t *LocalGOTAddr = (uint64_t*)getSectionAddress(GOTSectionID);
1384 // Fill in this entry with the address of the symbol being referenced.
1385 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset;
1386 } else {
1387 uint32_t *LocalGOTAddr = (uint32_t*)getSectionAddress(GOTSectionID);
1388 // Fill in this entry with the address of the symbol being referenced.
1389 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset);
1390 }
1391
1392 // Calculate the load address of this entry
1393 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize);
1394 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001395 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001396
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001397 assert(GOTIndex != -1 && "Unable to find requested GOT entry.");
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001398 return 0;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001399}
1400
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001401void RuntimeDyldELF::finalizeLoad(ObjSectionToIDMap &SectionMap) {
1402 // If necessary, allocate the global offset table
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001403 if (MemMgr) {
1404 // Allocate the GOT if necessary
1405 size_t numGOTEntries = GOTEntries.size();
1406 if (numGOTEntries != 0) {
1407 // Allocate memory for the section
1408 unsigned SectionID = Sections.size();
1409 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1410 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(),
1411 SectionID, ".got", false);
1412 if (!Addr)
1413 report_fatal_error("Unable to allocate memory for GOT!");
1414
1415 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1416 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1417 // For now, initialize all GOT entries to zero. We'll fill them in as
1418 // needed when GOT-based relocations are applied.
1419 memset(Addr, 0, TotalSize);
1420 }
1421 }
1422 else {
1423 report_fatal_error("Unable to allocate memory for GOT!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001424 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001425
1426 // Look for and record the EH frame section.
1427 ObjSectionToIDMap::iterator i, e;
1428 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1429 const SectionRef &Section = i->first;
1430 StringRef Name;
1431 Section.getName(Name);
1432 if (Name == ".eh_frame") {
1433 UnregisteredEHFrameSections.push_back(i->second);
1434 break;
1435 }
1436 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001437}
1438
Andrew Kayloradc70562012-10-02 21:18:39 +00001439bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
1440 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
1441 return false;
1442 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
Eli Bendersky4c647582012-01-16 08:56:09 +00001443}
Lang Hames173c69f2014-01-08 04:09:09 +00001444
1445bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile *Obj) const {
1446 return Obj->isELF();
1447}
1448
Eli Bendersky4c647582012-01-16 08:56:09 +00001449} // namespace llvm