blob: 6381c0495c384cd603fb1736518b5f48805d9b03 [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
Andrew Kayloradc70562012-10-02 21:18:39 +000014#include "RuntimeDyldELF.h"
Eli Bendersky4c647582012-01-16 08:56:09 +000015#include "llvm/ADT/IntervalMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringRef.h"
Eli Bendersky4c647582012-01-16 08:56:09 +000018#include "llvm/ADT/Triple.h"
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000019#include "llvm/MC/MCStreamer.h"
Michael J. Spencer126973b2013-08-08 22:27:13 +000020#include "llvm/Object/ELFObjectFile.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Object/ObjectFile.h"
22#include "llvm/Support/ELF.h"
Alexey Samsonova8d2f812014-08-27 23:06:08 +000023#include "llvm/Support/Endian.h"
Lang Hames173c69f2014-01-08 04:09:09 +000024#include "llvm/Support/MemoryBuffer.h"
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000025#include "llvm/Support/TargetRegistry.h"
Lang Hames173c69f2014-01-08 04:09:09 +000026
Eli Bendersky4c647582012-01-16 08:56:09 +000027using namespace llvm;
28using namespace llvm::object;
29
Chandler Carruthf58e3762014-04-22 03:04:17 +000030#define DEBUG_TYPE "dyld"
31
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000032static inline std::error_code check(std::error_code Err) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000033 if (Err) {
34 report_fatal_error(Err.message());
35 }
36 return Err;
37}
38
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000039namespace {
40
Juergen Ributzka7608dc02014-03-21 20:28:42 +000041template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> {
Rafael Espindola035b4162013-04-17 21:20:55 +000042 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Preston Gurdcc31af92012-04-16 22:12:58 +000043
Michael J. Spencer1a791612013-01-15 07:44:25 +000044 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
45 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000046 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
47 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
Preston Gurdcc31af92012-04-16 22:12:58 +000048
Michael J. Spencer1a791612013-01-15 07:44:25 +000049 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
Preston Gurdcc31af92012-04-16 22:12:58 +000050
Juergen Ributzka7608dc02014-03-21 20:28:42 +000051 typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type;
Preston Gurdcc31af92012-04-16 22:12:58 +000052
Preston Gurdcc31af92012-04-16 22:12:58 +000053public:
Rafael Espindola48af1c22014-08-19 18:44:46 +000054 DyldELFObject(MemoryBufferRef Wrapper, std::error_code &ec);
Preston Gurdcc31af92012-04-16 22:12:58 +000055
56 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000057
58 void updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr);
Preston Gurdcc31af92012-04-16 22:12:58 +000059
Andrew Kaylor5c010902012-07-27 17:52:42 +000060 // Methods for type inquiry through isa, cast and dyn_cast
Preston Gurdcc31af92012-04-16 22:12:58 +000061 static inline bool classof(const Binary *v) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000062 return (isa<ELFObjectFile<ELFT>>(v) &&
63 classof(cast<ELFObjectFile<ELFT>>(v)));
Preston Gurdcc31af92012-04-16 22:12:58 +000064 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +000065 static inline bool classof(const ELFObjectFile<ELFT> *v) {
Preston Gurdcc31af92012-04-16 22:12:58 +000066 return v->isDyldType();
67 }
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000068
Preston Gurdcc31af92012-04-16 22:12:58 +000069};
70
Preston Gurdcc31af92012-04-16 22:12:58 +000071
Preston Gurdcc31af92012-04-16 22:12:58 +000072
Andrew Kayloradc70562012-10-02 21:18:39 +000073// The MemoryBuffer passed into this constructor is just a wrapper around the
74// actual memory. Ultimately, the Binary parent class will take ownership of
75// this MemoryBuffer object but not the underlying memory.
Juergen Ributzka7608dc02014-03-21 20:28:42 +000076template <class ELFT>
Rafael Espindola48af1c22014-08-19 18:44:46 +000077DyldELFObject<ELFT>::DyldELFObject(MemoryBufferRef Wrapper, std::error_code &EC)
78 : ELFObjectFile<ELFT>(Wrapper, EC) {
Preston Gurdcc31af92012-04-16 22:12:58 +000079 this->isDyldELFObject = true;
80}
81
Juergen Ributzka7608dc02014-03-21 20:28:42 +000082template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +000083void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
84 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +000085 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
Juergen Ributzka7608dc02014-03-21 20:28:42 +000086 Elf_Shdr *shdr =
87 const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
Preston Gurdcc31af92012-04-16 22:12:58 +000088
89 // This assumes the address passed in matches the target address bitness
90 // The template-based type cast handles everything else.
91 shdr->sh_addr = static_cast<addr_type>(Addr);
92}
93
Juergen Ributzka7608dc02014-03-21 20:28:42 +000094template <class ELFT>
Michael J. Spencer1a791612013-01-15 07:44:25 +000095void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
96 uint64_t Addr) {
Preston Gurdcc31af92012-04-16 22:12:58 +000097
Juergen Ributzka7608dc02014-03-21 20:28:42 +000098 Elf_Sym *sym = const_cast<Elf_Sym *>(
99 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
Preston Gurdcc31af92012-04-16 22:12:58 +0000100
101 // This assumes the address passed in matches the target address bitness
102 // The template-based type cast handles everything else.
103 sym->st_value = static_cast<addr_type>(Addr);
104}
105
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000106class LoadedELFObjectInfo : public RuntimeDyld::LoadedObjectInfo {
107public:
108 LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
109 unsigned EndIdx)
110 : RuntimeDyld::LoadedObjectInfo(RTDyld, BeginIdx, EndIdx) {}
111
112 OwningBinary<ObjectFile>
113 getObjectForDebug(const ObjectFile &Obj) const override;
114};
115
116template <typename ELFT>
117std::unique_ptr<DyldELFObject<ELFT>>
118createRTDyldELFObject(MemoryBufferRef Buffer,
119 const LoadedELFObjectInfo &L,
120 std::error_code &ec) {
121 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
122 typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type;
123
124 std::unique_ptr<DyldELFObject<ELFT>> Obj =
125 llvm::make_unique<DyldELFObject<ELFT>>(Buffer, ec);
126
127 // Iterate over all sections in the object.
128 for (const auto &Sec : Obj->sections()) {
129 StringRef SectionName;
130 Sec.getName(SectionName);
131 if (SectionName != "") {
132 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
133 Elf_Shdr *shdr = const_cast<Elf_Shdr *>(
134 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
135
136 if (uint64_t SecLoadAddr = L.getSectionLoadAddress(SectionName)) {
137 // This assumes that the address passed in matches the target address
138 // bitness. The template-based type cast handles everything else.
139 shdr->sh_addr = static_cast<addr_type>(SecLoadAddr);
140 }
141 }
142 }
143
144 return Obj;
145}
146
147OwningBinary<ObjectFile> createELFDebugObject(const ObjectFile &Obj,
148 const LoadedELFObjectInfo &L) {
149 assert(Obj.isELF() && "Not an ELF object file.");
150
151 std::unique_ptr<MemoryBuffer> Buffer =
152 MemoryBuffer::getMemBufferCopy(Obj.getData(), Obj.getFileName());
153
154 std::error_code ec;
155
156 std::unique_ptr<ObjectFile> DebugObj;
157 if (Obj.getBytesInAddress() == 4 && Obj.isLittleEndian()) {
158 typedef ELFType<support::little, 2, false> ELF32LE;
159 DebugObj = createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), L, ec);
160 } else if (Obj.getBytesInAddress() == 4 && !Obj.isLittleEndian()) {
161 typedef ELFType<support::big, 2, false> ELF32BE;
162 DebugObj = createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), L, ec);
163 } else if (Obj.getBytesInAddress() == 8 && !Obj.isLittleEndian()) {
164 typedef ELFType<support::big, 2, true> ELF64BE;
165 DebugObj = createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), L, ec);
166 } else if (Obj.getBytesInAddress() == 8 && Obj.isLittleEndian()) {
167 typedef ELFType<support::little, 2, true> ELF64LE;
168 DebugObj = createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), L, ec);
169 } else
170 llvm_unreachable("Unexpected ELF format");
171
172 assert(!ec && "Could not construct copy ELF object file");
173
174 return OwningBinary<ObjectFile>(std::move(DebugObj), std::move(Buffer));
175}
176
177OwningBinary<ObjectFile>
178LoadedELFObjectInfo::getObjectForDebug(const ObjectFile &Obj) const {
179 return createELFDebugObject(Obj, *this);
180}
181
Preston Gurdcc31af92012-04-16 22:12:58 +0000182} // namespace
183
Eli Bendersky4c647582012-01-16 08:56:09 +0000184namespace llvm {
185
Lang Hames633fe142015-03-30 03:37:06 +0000186RuntimeDyldELF::RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
187 RuntimeDyld::SymbolResolver &Resolver)
188 : RuntimeDyldImpl(MemMgr, Resolver) {}
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000189RuntimeDyldELF::~RuntimeDyldELF() {}
190
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000191void RuntimeDyldELF::registerEHFrames() {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000192 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
193 SID EHFrameSID = UnregisteredEHFrameSections[i];
194 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
195 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
196 size_t EHFrameSize = Sections[EHFrameSID].Size;
Lang Hames633fe142015-03-30 03:37:06 +0000197 MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
Andrew Kaylorc442a762013-10-16 00:14:21 +0000198 RegisteredEHFrameSections.push_back(EHFrameSID);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000199 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000200 UnregisteredEHFrameSections.clear();
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000201}
202
Andrew Kaylorc442a762013-10-16 00:14:21 +0000203void RuntimeDyldELF::deregisterEHFrames() {
Andrew Kaylorc442a762013-10-16 00:14:21 +0000204 for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
205 SID EHFrameSID = RegisteredEHFrameSections[i];
206 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
207 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
208 size_t EHFrameSize = Sections[EHFrameSID].Size;
Lang Hames633fe142015-03-30 03:37:06 +0000209 MemMgr.deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
Andrew Kaylorc442a762013-10-16 00:14:21 +0000210 }
211 RegisteredEHFrameSections.clear();
212}
213
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000214std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
215RuntimeDyldELF::loadObject(const object::ObjectFile &O) {
216 unsigned SectionStartIdx, SectionEndIdx;
217 std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
218 return llvm::make_unique<LoadedELFObjectInfo>(*this, SectionStartIdx,
219 SectionEndIdx);
Lang Hames173c69f2014-01-08 04:09:09 +0000220}
221
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000222void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000223 uint64_t Offset, uint64_t Value,
224 uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000225 uint64_t SymOffset) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000226 switch (Type) {
227 default:
228 llvm_unreachable("Relocation type not implemented yet!");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000229 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000230 case ELF::R_X86_64_64: {
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000231 support::ulittle64_t::ref(Section.Address + Offset) = Value + Addend;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000232 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000233 << format("%p\n", Section.Address + Offset));
Eli Bendersky4c647582012-01-16 08:56:09 +0000234 break;
235 }
236 case ELF::R_X86_64_32:
237 case ELF::R_X86_64_32S: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000238 Value += Addend;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000239 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000240 (Type == ELF::R_X86_64_32S &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000241 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Bendersky4c647582012-01-16 08:56:09 +0000242 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000243 support::ulittle32_t::ref(Section.Address + Offset) = TruncatedAddr;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000244 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000245 << format("%p\n", Section.Address + Offset));
Eli Bendersky4c647582012-01-16 08:56:09 +0000246 break;
247 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000248 case ELF::R_X86_64_GOTPCREL: {
249 // findGOTEntry returns the 'G + GOT' part of the relocation calculation
250 // based on the load/target address of the GOT (not the current/local addr).
251 uint64_t GOTAddr = findGOTEntry(Value, SymOffset);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000252 uint64_t FinalAddress = Section.LoadAddress + Offset;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000253 // The processRelocationRef method combines the symbol offset and the addend
254 // and in most cases that's what we want. For this relocation type, we need
255 // the raw addend, so we subtract the symbol offset to get it.
256 int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress;
257 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
258 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000259 support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000260 break;
261 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000262 case ELF::R_X86_64_PC32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000263 // Get the placeholder value from the generated object since
264 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000265 support::ulittle32_t::ref Placeholder(
266 (void *)(Section.ObjAddress + Offset));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000267 uint64_t FinalAddress = Section.LoadAddress + Offset;
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000268 int64_t RealOffset = Placeholder + Value + Addend - FinalAddress;
Andrew Kaylor8e87a752012-07-27 20:30:12 +0000269 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000270 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000271 support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000272 break;
273 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000274 case ELF::R_X86_64_PC64: {
275 // Get the placeholder value from the generated object since
276 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000277 support::ulittle64_t::ref Placeholder(
278 (void *)(Section.ObjAddress + Offset));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000279 uint64_t FinalAddress = Section.LoadAddress + Offset;
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000280 support::ulittle64_t::ref(Section.Address + Offset) =
281 Placeholder + Value + Addend - FinalAddress;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000282 break;
283 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000284 }
285}
286
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000287void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000288 uint64_t Offset, uint32_t Value,
289 uint32_t Type, int32_t Addend) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000290 switch (Type) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000291 case ELF::R_386_32: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000292 // Get the placeholder value from the generated object since
293 // a previous relocation attempt may have overwritten the loaded version
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000294 support::ulittle32_t::ref Placeholder(
295 (void *)(Section.ObjAddress + Offset));
296 support::ulittle32_t::ref(Section.Address + Offset) =
297 Placeholder + Value + Addend;
Eli Bendersky4c647582012-01-16 08:56:09 +0000298 break;
299 }
300 case ELF::R_386_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
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000303 support::ulittle32_t::ref Placeholder(
304 (void *)(Section.ObjAddress + Offset));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000305 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Alexey Samsonova8d2f812014-08-27 23:06:08 +0000306 uint32_t RealOffset = Placeholder + Value + Addend - FinalAddress;
307 support::ulittle32_t::ref(Section.Address + Offset) = RealOffset;
Eli Bendersky4c647582012-01-16 08:56:09 +0000308 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000309 }
310 default:
311 // There are other relocation types, but it appears these are the
312 // only ones currently used by the LLVM ELF object writer
313 llvm_unreachable("Relocation type not implemented yet!");
314 break;
Eli Bendersky4c647582012-01-16 08:56:09 +0000315 }
316}
317
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000318void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000319 uint64_t Offset, uint64_t Value,
320 uint32_t Type, int64_t Addend) {
321 uint32_t *TargetPtr = reinterpret_cast<uint32_t *>(Section.Address + Offset);
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000322 uint64_t FinalAddress = Section.LoadAddress + Offset;
323
324 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
325 << format("%llx", Section.Address + Offset)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000326 << " FinalAddress: 0x" << format("%llx", FinalAddress)
327 << " Value: 0x" << format("%llx", Value) << " Type: 0x"
328 << format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000329 << "\n");
330
331 switch (Type) {
332 default:
333 llvm_unreachable("Relocation type not implemented yet!");
334 break;
Tim Northoverb23d8db2013-05-04 20:14:14 +0000335 case ELF::R_AARCH64_ABS64: {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000336 uint64_t *TargetPtr =
337 reinterpret_cast<uint64_t *>(Section.Address + Offset);
Tim Northoverb23d8db2013-05-04 20:14:14 +0000338 *TargetPtr = Value + Addend;
339 break;
340 }
Tim Northover5959ea32013-05-19 15:39:03 +0000341 case ELF::R_AARCH64_PREL32: {
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000342 uint64_t Result = Value + Addend - FinalAddress;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000343 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000344 static_cast<int64_t>(Result) <= UINT32_MAX);
345 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
346 break;
347 }
Tim Northover37cde972013-05-04 20:14:09 +0000348 case ELF::R_AARCH64_CALL26: // fallthrough
349 case ELF::R_AARCH64_JUMP26: {
350 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
351 // calculation.
352 uint64_t BranchImm = Value + Addend - FinalAddress;
353
354 // "Check that -2^27 <= result < 2^27".
Michael J. Spencer126973b2013-08-08 22:27:13 +0000355 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
Tim Northover37cde972013-05-04 20:14:09 +0000356 static_cast<int64_t>(BranchImm) < (1LL << 27));
Tim Northover5959ea32013-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.
360 *TargetPtr &= 0xfc000000U;
Tim Northover37cde972013-05-04 20:14:09 +0000361 // Immediate goes in bits 25:0 of B and BL.
362 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2;
363 break;
364 }
Tim Northover4d01c1e2013-05-04 20:14:04 +0000365 case ELF::R_AARCH64_MOVW_UABS_G3: {
366 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000367
368 // AArch64 code is emitted with .rela relocations. The data already in any
369 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000370 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000371 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
372 *TargetPtr |= Result >> (48 - 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000373 // Shift must be "lsl #48", in bits 22:21
374 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000375 break;
376 }
377 case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
378 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000379
Tim Northover5959ea32013-05-19 15:39:03 +0000380 // AArch64 code is emitted with .rela relocations. The data already in any
381 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000382 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000383 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
384 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000385 // Shift must be "lsl #32", in bits 22:21
386 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000387 break;
388 }
389 case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
390 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000391
392 // AArch64 code is emitted with .rela relocations. The data already in any
393 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000394 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000395 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
396 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
Tim Northover8625fd82013-07-01 19:23:10 +0000397 // Shift must be "lsl #16", in bits 22:2
398 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000399 break;
400 }
401 case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
402 uint64_t Result = Value + Addend;
Tim Northover5959ea32013-05-19 15:39:03 +0000403
404 // AArch64 code is emitted with .rela relocations. The data already in any
405 // bits affected by the relocation on entry is garbage.
Tim Northoverca8a0072013-07-25 12:42:52 +0000406 *TargetPtr &= 0xffe0001fU;
Tim Northover4d01c1e2013-05-04 20:14:04 +0000407 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction
408 *TargetPtr |= ((Result & 0xffffU) << 5);
Tim Northover8625fd82013-07-01 19:23:10 +0000409 // Shift must be "lsl #0", in bits 22:21.
410 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
Tim Northover4d01c1e2013-05-04 20:14:04 +0000411 break;
412 }
Bradley Smith9d808492014-02-11 12:59:09 +0000413 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
414 // Operation: Page(S+A) - Page(P)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000415 uint64_t Result =
416 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
Bradley Smith9d808492014-02-11 12:59:09 +0000417
418 // Check that -2^32 <= X < 2^32
419 assert(static_cast<int64_t>(Result) >= (-1LL << 32) &&
420 static_cast<int64_t>(Result) < (1LL << 32) &&
421 "overflow check failed for relocation");
422
423 // AArch64 code is emitted with .rela relocations. The data already in any
424 // bits affected by the relocation on entry is garbage.
425 *TargetPtr &= 0x9f00001fU;
426 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
427 // from bits 32:12 of X.
428 *TargetPtr |= ((Result & 0x3000U) << (29 - 12));
429 *TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5));
430 break;
431 }
432 case ELF::R_AARCH64_LDST32_ABS_LO12_NC: {
433 // Operation: S + A
434 uint64_t Result = Value + Addend;
435
436 // AArch64 code is emitted with .rela relocations. The data already in any
437 // bits affected by the relocation on entry is garbage.
438 *TargetPtr &= 0xffc003ffU;
439 // Immediate goes in bits 21:10 of LD/ST instruction, taken
440 // from bits 11:2 of X
441 *TargetPtr |= ((Result & 0xffc) << (10 - 2));
442 break;
443 }
444 case ELF::R_AARCH64_LDST64_ABS_LO12_NC: {
445 // Operation: S + A
446 uint64_t Result = Value + Addend;
447
448 // AArch64 code is emitted with .rela relocations. The data already in any
449 // bits affected by the relocation on entry is garbage.
450 *TargetPtr &= 0xffc003ffU;
451 // Immediate goes in bits 21:10 of LD/ST instruction, taken
452 // from bits 11:3 of X
453 *TargetPtr |= ((Result & 0xff8) << (10 - 3));
454 break;
455 }
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000456 }
457}
458
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000459void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000460 uint64_t Offset, uint32_t Value,
461 uint32_t Type, int32_t Addend) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000462 // TODO: Add Thumb relocations.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000463 uint32_t *Placeholder =
464 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
465 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000466 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000467 Value += Addend;
468
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000469 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
470 << Section.Address + Offset
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000471 << " FinalAddress: " << format("%p", FinalAddress) << " Value: "
472 << format("%x", Value) << " Type: " << format("%x", Type)
473 << " Addend: " << format("%x", Addend) << "\n");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000474
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000475 switch (Type) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000476 default:
477 llvm_unreachable("Not implemented relocation type!");
478
Renato Golin8cea6e82014-01-29 11:50:56 +0000479 case ELF::R_ARM_NONE:
480 break;
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000481 // Write a 32bit value to relocation address, taking into account the
Tim Northover471cbb72012-10-03 16:29:42 +0000482 // implicit addend encoded in the target.
Renato Golin8cea6e82014-01-29 11:50:56 +0000483 case ELF::R_ARM_PREL31:
Tim Northover3b684d82013-05-28 19:48:19 +0000484 case ELF::R_ARM_TARGET1:
485 case ELF::R_ARM_ABS32:
486 *TargetPtr = *Placeholder + Value;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000487 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000488 // Write first 16 bit of 32 bit value to the mov instruction.
489 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000490 case ELF::R_ARM_MOVW_ABS_NC:
Tim Northover471cbb72012-10-03 16:29:42 +0000491 // We are not expecting any other addend in the relocation address.
Michael J. Spencerbae14ce2013-01-04 20:36:28 +0000492 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover471cbb72012-10-03 16:29:42 +0000493 // non-contiguous fields.
Tim Northover3b684d82013-05-28 19:48:19 +0000494 assert((*Placeholder & 0x000F0FFF) == 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000495 Value = Value & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000496 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000497 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
498 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000499 // Write last 16 bit of 32 bit value to the mov instruction.
500 // Last 4 bit should be shifted.
Tim Northover3b684d82013-05-28 19:48:19 +0000501 case ELF::R_ARM_MOVT_ABS:
Tim Northover471cbb72012-10-03 16:29:42 +0000502 // We are not expecting any other addend in the relocation address.
503 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
Tim Northover3b684d82013-05-28 19:48:19 +0000504 assert((*Placeholder & 0x000F0FFF) == 0);
505
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000506 Value = (Value >> 16) & 0xFFFF;
Tim Northover3b684d82013-05-28 19:48:19 +0000507 *TargetPtr = *Placeholder | (Value & 0xFFF);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000508 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
509 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000510 // Write 24 bit relative value to the branch instruction.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000511 case ELF::R_ARM_PC24: // Fall through.
512 case ELF::R_ARM_CALL: // Fall through.
Tim Northover3b684d82013-05-28 19:48:19 +0000513 case ELF::R_ARM_JUMP24: {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000514 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
515 RelValue = (RelValue & 0x03FFFFFC) >> 2;
Tim Northover3b684d82013-05-28 19:48:19 +0000516 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000517 *TargetPtr &= 0xFF000000;
518 *TargetPtr |= RelValue;
519 break;
520 }
Tim Northover3b684d82013-05-28 19:48:19 +0000521 case ELF::R_ARM_PRIVATE_0:
522 // This relocation is reserved by the ARM ELF ABI for internal use. We
523 // appropriate it here to act as an R_ARM_ABS32 without any addend for use
524 // in the stubs created during JIT (which can't put an addend into the
525 // original object file).
526 *TargetPtr = Value;
527 break;
528 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000529}
530
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000531void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000532 uint64_t Offset, uint32_t Value,
533 uint32_t Type, int32_t Addend) {
534 uint32_t *Placeholder =
535 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset);
536 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000537 Value += Addend;
538
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000539 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000540 << Section.Address + Offset << " FinalAddress: "
541 << format("%p", Section.LoadAddress + Offset) << " Value: "
542 << format("%x", Value) << " Type: " << format("%x", Type)
543 << " Addend: " << format("%x", Addend) << "\n");
Akira Hatanaka111174b2012-08-17 21:28:04 +0000544
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000545 switch (Type) {
Akira Hatanaka111174b2012-08-17 21:28:04 +0000546 default:
547 llvm_unreachable("Not implemented relocation type!");
548 break;
549 case ELF::R_MIPS_32:
Akira Hatanaka2e236242013-07-24 01:58:40 +0000550 *TargetPtr = Value + (*Placeholder);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000551 break;
552 case ELF::R_MIPS_26:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000553 *TargetPtr = ((*Placeholder) & 0xfc000000) | ((Value & 0x0fffffff) >> 2);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000554 break;
555 case ELF::R_MIPS_HI16:
556 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanaka2e236242013-07-24 01:58:40 +0000557 Value += ((*Placeholder) & 0x0000ffff) << 16;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000558 *TargetPtr =
559 ((*Placeholder) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka2e236242013-07-24 01:58:40 +0000560 break;
561 case ELF::R_MIPS_LO16:
562 Value += ((*Placeholder) & 0x0000ffff);
563 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
564 break;
565 case ELF::R_MIPS_UNUSED1:
566 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
567 // are used for internal JIT purpose. These relocations are similar to
568 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
569 // account.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000570 *TargetPtr =
571 ((*TargetPtr) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff);
Akira Hatanaka111174b2012-08-17 21:28:04 +0000572 break;
Akira Hatanaka2e236242013-07-24 01:58:40 +0000573 case ELF::R_MIPS_UNUSED2:
Akira Hatanaka111174b2012-08-17 21:28:04 +0000574 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
575 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000576 }
Akira Hatanaka111174b2012-08-17 21:28:04 +0000577}
578
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000579// Return the .TOC. section and offset.
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000580void RuntimeDyldELF::findPPC64TOCSection(const ObjectFile &Obj,
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000581 ObjSectionToIDMap &LocalSections,
582 RelocationValueRef &Rel) {
583 // Set a default SectionID in case we do not find a TOC section below.
584 // This may happen for references to TOC base base (sym@toc, .odp
585 // relocation) without a .toc directive. In this case just use the
586 // first section (which is usually the .odp) since the code won't
587 // reference the .toc base directly.
588 Rel.SymbolName = NULL;
589 Rel.SectionID = 0;
590
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000591 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
592 // order. The TOC starts where the first of these sections starts.
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000593 for (section_iterator si = Obj.section_begin(), se = Obj.section_end();
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000594 si != se; ++si) {
595
596 StringRef SectionName;
597 check(si->getName(SectionName));
598
599 if (SectionName == ".got"
600 || SectionName == ".toc"
601 || SectionName == ".tocbss"
602 || SectionName == ".plt") {
603 Rel.SectionID = findOrEmitSection(Obj, *si, false, LocalSections);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000604 break;
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000605 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000606 }
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000607
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000608 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
609 // thus permitting a full 64 Kbytes segment.
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +0000610 Rel.Addend = 0x8000;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000611}
612
613// Returns the sections and offset associated with the ODP entry referenced
614// by Symbol.
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000615void RuntimeDyldELF::findOPDEntrySection(const ObjectFile &Obj,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000616 ObjSectionToIDMap &LocalSections,
617 RelocationValueRef &Rel) {
618 // Get the ELF symbol value (st_value) to compare with Relocation offset in
619 // .opd entries
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000620 for (section_iterator si = Obj.section_begin(), se = Obj.section_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000621 si != se; ++si) {
Rafael Espindolaa61f1e92013-06-03 19:37:34 +0000622 section_iterator RelSecI = si->getRelocatedSection();
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000623 if (RelSecI == Obj.section_end())
Rafael Espindolaa61f1e92013-06-03 19:37:34 +0000624 continue;
625
626 StringRef RelSectionName;
627 check(RelSecI->getName(RelSectionName));
628 if (RelSectionName != ".opd")
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000629 continue;
630
Rafael Espindolab5155a52014-02-10 20:24:04 +0000631 for (relocation_iterator i = si->relocation_begin(),
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000632 e = si->relocation_end();
633 i != e;) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000634 // The R_PPC64_ADDR64 relocation indicates the first field
635 // of a .opd entry
636 uint64_t TypeFunc;
637 check(i->getType(TypeFunc));
638 if (TypeFunc != ELF::R_PPC64_ADDR64) {
Rafael Espindola5e812af2014-01-30 02:49:50 +0000639 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000640 continue;
641 }
642
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000643 uint64_t TargetSymbolOffset;
Rafael Espindola806f0062013-06-05 01:33:53 +0000644 symbol_iterator TargetSymbol = i->getSymbol();
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000645 check(i->getOffset(TargetSymbolOffset));
Rafael Espindola0d15f732013-05-09 03:39:05 +0000646 int64_t Addend;
647 check(getELFRelocationAddend(*i, Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000648
Rafael Espindola5e812af2014-01-30 02:49:50 +0000649 ++i;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000650 if (i == e)
651 break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000652
653 // Just check if following relocation is a R_PPC64_TOC
654 uint64_t TypeTOC;
655 check(i->getType(TypeTOC));
656 if (TypeTOC != ELF::R_PPC64_TOC)
657 continue;
658
659 // Finally compares the Symbol value and the target symbol offset
660 // to check if this .opd entry refers to the symbol the relocation
661 // points to.
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000662 if (Rel.Addend != (int64_t)TargetSymbolOffset)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000663 continue;
664
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000665 section_iterator tsi(Obj.section_end());
Rafael Espindola806f0062013-06-05 01:33:53 +0000666 check(TargetSymbol->getSection(tsi));
Rafael Espindola80291272014-10-08 15:28:58 +0000667 bool IsCode = tsi->isText();
Lang Hames9b2dc932014-02-18 21:46:39 +0000668 Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
Rafael Espindola0d15f732013-05-09 03:39:05 +0000669 Rel.Addend = (intptr_t)Addend;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000670 return;
671 }
672 }
673 llvm_unreachable("Attempting to get address of ODP entry!");
674}
675
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000676// Relocation masks following the #lo(value), #hi(value), #ha(value),
677// #higher(value), #highera(value), #highest(value), and #highesta(value)
678// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
679// document.
680
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000681static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000682
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000683static inline uint16_t applyPPChi(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000684 return (value >> 16) & 0xffff;
685}
686
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000687static inline uint16_t applyPPCha (uint64_t value) {
688 return ((value + 0x8000) >> 16) & 0xffff;
689}
690
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000691static inline uint16_t applyPPChigher(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000692 return (value >> 32) & 0xffff;
693}
694
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000695static inline uint16_t applyPPChighera (uint64_t value) {
696 return ((value + 0x8000) >> 32) & 0xffff;
697}
698
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000699static inline uint16_t applyPPChighest(uint64_t value) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000700 return (value >> 48) & 0xffff;
701}
702
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000703static inline uint16_t applyPPChighesta (uint64_t value) {
704 return ((value + 0x8000) >> 48) & 0xffff;
705}
706
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000707void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000708 uint64_t Offset, uint64_t Value,
709 uint32_t Type, int64_t Addend) {
710 uint8_t *LocalAddress = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000711 switch (Type) {
712 default:
713 llvm_unreachable("Relocation type not implemented yet!");
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000714 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000715 case ELF::R_PPC64_ADDR16:
716 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
717 break;
718 case ELF::R_PPC64_ADDR16_DS:
719 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
720 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000721 case ELF::R_PPC64_ADDR16_LO:
722 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000723 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000724 case ELF::R_PPC64_ADDR16_LO_DS:
725 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
726 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000727 case ELF::R_PPC64_ADDR16_HI:
728 writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000729 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000730 case ELF::R_PPC64_ADDR16_HA:
731 writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
732 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000733 case ELF::R_PPC64_ADDR16_HIGHER:
734 writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000735 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000736 case ELF::R_PPC64_ADDR16_HIGHERA:
737 writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
738 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000739 case ELF::R_PPC64_ADDR16_HIGHEST:
740 writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
741 break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000742 case ELF::R_PPC64_ADDR16_HIGHESTA:
743 writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
744 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000745 case ELF::R_PPC64_ADDR14: {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000746 assert(((Value + Addend) & 3) == 0);
747 // Preserve the AA/LK bits in the branch instruction
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000748 uint8_t aalk = *(LocalAddress + 3);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000749 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
750 } break;
Ulrich Weiganddbc8e1a2014-06-20 17:51:47 +0000751 case ELF::R_PPC64_REL16_LO: {
752 uint64_t FinalAddress = (Section.LoadAddress + Offset);
753 uint64_t Delta = Value - FinalAddress + Addend;
754 writeInt16BE(LocalAddress, applyPPClo(Delta));
755 } break;
756 case ELF::R_PPC64_REL16_HI: {
757 uint64_t FinalAddress = (Section.LoadAddress + Offset);
758 uint64_t Delta = Value - FinalAddress + Addend;
759 writeInt16BE(LocalAddress, applyPPChi(Delta));
760 } break;
761 case ELF::R_PPC64_REL16_HA: {
762 uint64_t FinalAddress = (Section.LoadAddress + Offset);
763 uint64_t Delta = Value - FinalAddress + Addend;
764 writeInt16BE(LocalAddress, applyPPCha(Delta));
765 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000766 case ELF::R_PPC64_ADDR32: {
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000767 int32_t Result = static_cast<int32_t>(Value + Addend);
768 if (SignExtend32<32>(Result) != Result)
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000769 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
Adhemerval Zanella9b0b7812013-01-04 19:08:13 +0000770 writeInt32BE(LocalAddress, Result);
771 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000772 case ELF::R_PPC64_REL24: {
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000773 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000774 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
775 if (SignExtend32<24>(delta) != delta)
776 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
777 // Generates a 'bl <address>' instruction
778 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
779 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000780 case ELF::R_PPC64_REL32: {
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000781 uint64_t FinalAddress = (Section.LoadAddress + Offset);
782 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
783 if (SignExtend32<32>(delta) != delta)
784 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
785 writeInt32BE(LocalAddress, delta);
786 } break;
Adhemerval Zanellae8bd03d2013-05-06 17:21:23 +0000787 case ELF::R_PPC64_REL64: {
788 uint64_t FinalAddress = (Section.LoadAddress + Offset);
789 uint64_t Delta = Value - FinalAddress + Addend;
790 writeInt64BE(LocalAddress, Delta);
791 } break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000792 case ELF::R_PPC64_ADDR64:
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000793 writeInt64BE(LocalAddress, Value + Addend);
794 break;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000795 }
796}
797
Richard Sandifordca044082013-05-03 14:15:35 +0000798void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000799 uint64_t Offset, uint64_t Value,
800 uint32_t Type, int64_t Addend) {
Richard Sandifordca044082013-05-03 14:15:35 +0000801 uint8_t *LocalAddress = Section.Address + Offset;
802 switch (Type) {
803 default:
804 llvm_unreachable("Relocation type not implemented yet!");
805 break;
806 case ELF::R_390_PC16DBL:
807 case ELF::R_390_PLT16DBL: {
808 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
809 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
810 writeInt16BE(LocalAddress, Delta / 2);
811 break;
812 }
813 case ELF::R_390_PC32DBL:
814 case ELF::R_390_PLT32DBL: {
815 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
816 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
817 writeInt32BE(LocalAddress, Delta / 2);
818 break;
819 }
820 case ELF::R_390_PC32: {
821 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset);
822 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
823 writeInt32BE(LocalAddress, Delta);
824 break;
825 }
826 case ELF::R_390_64:
827 writeInt64BE(LocalAddress, Value + Addend);
828 break;
829 }
830}
831
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000832// The target location for the relocation is described by RE.SectionID and
833// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
834// SectionEntry has three members describing its location.
835// SectionEntry::Address is the address at which the section has been loaded
836// into memory in the current (host) process. SectionEntry::LoadAddress is the
837// address that the section will have in the target process.
838// SectionEntry::ObjAddress is the address of the bits for this section in the
839// original emitted object image (also in the current address space).
840//
841// Relocations will be applied as if the section were loaded at
842// SectionEntry::LoadAddress, but they will be applied at an address based
843// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
844// Target memory contents if they are required for value calculations.
845//
846// The Value parameter here is the load address of the symbol for the
847// relocation to be applied. For relocations which refer to symbols in the
848// current object Value will be the LoadAddress of the section in which
849// the symbol resides (RE.Addend provides additional information about the
850// symbol location). For external symbols, Value will be the address of the
851// symbol in the target address space.
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000852void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000853 uint64_t Value) {
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000854 const SectionEntry &Section = Sections[RE.SectionID];
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000855 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
856 RE.SymOffset);
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000857}
858
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000859void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000860 uint64_t Offset, uint64_t Value,
861 uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000862 uint64_t SymOffset) {
Eli Bendersky4c647582012-01-16 08:56:09 +0000863 switch (Arch) {
864 case Triple::x86_64:
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000865 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
Eli Bendersky4c647582012-01-16 08:56:09 +0000866 break;
867 case Triple::x86:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000868 resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000869 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000870 break;
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000871 case Triple::aarch64:
Christian Pirker99974c72014-03-26 14:57:32 +0000872 case Triple::aarch64_be:
Tim Northoverfa1b2f82013-05-04 20:13:59 +0000873 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
874 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000875 case Triple::arm: // Fall through.
Christian Pirker2a111602014-03-28 14:35:30 +0000876 case Triple::armeb:
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000877 case Triple::thumb:
Christian Pirker2a111602014-03-28 14:35:30 +0000878 case Triple::thumbeb:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000879 resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000880 (uint32_t)(Addend & 0xffffffffL));
Eli Bendersky4c647582012-01-16 08:56:09 +0000881 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000882 case Triple::mips: // Fall through.
Akira Hatanaka111174b2012-08-17 21:28:04 +0000883 case Triple::mipsel:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000884 resolveMIPSRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL),
885 Type, (uint32_t)(Addend & 0xffffffffL));
Akira Hatanaka111174b2012-08-17 21:28:04 +0000886 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000887 case Triple::ppc64: // Fall through.
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000888 case Triple::ppc64le:
Andrew Kaylorfb05a502012-11-02 19:45:23 +0000889 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000890 break;
Richard Sandifordca044082013-05-03 14:15:35 +0000891 case Triple::systemz:
892 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
893 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000894 default:
895 llvm_unreachable("Unsupported CPU type!");
Eli Bendersky4c647582012-01-16 08:56:09 +0000896 }
897}
898
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000899relocation_iterator RuntimeDyldELF::processRelocationRef(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000900 unsigned SectionID, relocation_iterator RelI,
901 const ObjectFile &Obj,
Lang Hamesa5cd9502014-11-27 05:40:13 +0000902 ObjSectionToIDMap &ObjSectionToID,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000903 StubMap &Stubs) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000904 uint64_t RelType;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000905 Check(RelI->getType(RelType));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000906 int64_t Addend;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000907 Check(getELFRelocationAddend(*RelI, Addend));
908 symbol_iterator Symbol = RelI->getSymbol();
Eli Bendersky667b8792012-05-01 10:41:12 +0000909
910 // Obtain the symbol name which is referenced in the relocation
911 StringRef TargetName;
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000912 if (Symbol != Obj.symbol_end())
Rafael Espindola75954472013-06-05 02:55:01 +0000913 Symbol->getName(TargetName);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000914 DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
915 << " TargetName: " << TargetName << "\n");
Eli Bendersky667b8792012-05-01 10:41:12 +0000916 RelocationValueRef Value;
917 // First search for the symbol in the local symbol table
Rafael Espindola75954472013-06-05 02:55:01 +0000918 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
Lang Hamesa5cd9502014-11-27 05:40:13 +0000919
920 // Search for the symbol in the global symbol table
Lang Hames6bfd3982015-01-16 23:13:56 +0000921 RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end();
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000922 if (Symbol != Obj.symbol_end()) {
Lang Hamesa5cd9502014-11-27 05:40:13 +0000923 gsi = GlobalSymbolTable.find(TargetName.data());
Rafael Espindola75954472013-06-05 02:55:01 +0000924 Symbol->getType(SymType);
925 }
Lang Hamesa5cd9502014-11-27 05:40:13 +0000926 if (gsi != GlobalSymbolTable.end()) {
Lang Hames6bfd3982015-01-16 23:13:56 +0000927 const auto &SymInfo = gsi->second;
928 Value.SectionID = SymInfo.getSectionID();
929 Value.Offset = SymInfo.getOffset();
930 Value.Addend = SymInfo.getOffset() + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000931 } else {
Lang Hamesa5cd9502014-11-27 05:40:13 +0000932 switch (SymType) {
933 case SymbolRef::ST_Debug: {
934 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
935 // and can be changed by another developers. Maybe best way is add
936 // a new symbol type ST_Section to SymbolRef and use it.
937 section_iterator si(Obj.section_end());
938 Symbol->getSection(si);
939 if (si == Obj.section_end())
940 llvm_unreachable("Symbol section not found, bad object file format!");
941 DEBUG(dbgs() << "\t\tThis is section symbol\n");
942 bool isCode = si->isText();
943 Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID);
944 Value.Addend = Addend;
945 break;
946 }
947 case SymbolRef::ST_Data:
948 case SymbolRef::ST_Unknown: {
949 Value.SymbolName = TargetName.data();
950 Value.Addend = Addend;
Richard Mittonad6d3492013-08-16 18:54:26 +0000951
Lang Hamesa5cd9502014-11-27 05:40:13 +0000952 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
953 // will manifest here as a NULL symbol name.
954 // We can set this as a valid (but empty) symbol name, and rely
955 // on addRelocationForSymbol to handle this.
956 if (!Value.SymbolName)
957 Value.SymbolName = "";
958 break;
959 }
960 default:
961 llvm_unreachable("Unresolved symbol type!");
962 break;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000963 }
Eli Bendersky4c647582012-01-16 08:56:09 +0000964 }
Lang Hamesa5cd9502014-11-27 05:40:13 +0000965
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000966 uint64_t Offset;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000967 Check(RelI->getOffset(Offset));
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000968
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000969 DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000970 << "\n");
Tim Northovere19bed72014-07-23 12:32:47 +0000971 if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be) &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000972 (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26)) {
Tim Northover37cde972013-05-04 20:14:09 +0000973 // This is an AArch64 branch relocation, need to use a stub function.
974 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
975 SectionEntry &Section = Sections[SectionID];
976
977 // Look for an existing stub.
978 StubMap::const_iterator i = Stubs.find(Value);
979 if (i != Stubs.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000980 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
981 RelType, 0);
Tim Northover37cde972013-05-04 20:14:09 +0000982 DEBUG(dbgs() << " Stub function found\n");
983 } else {
984 // Create a new stub function.
985 DEBUG(dbgs() << " Create a new stub function\n");
986 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000987 uint8_t *StubTargetAddr =
988 createStubFunction(Section.Address + Section.StubOffset);
Tim Northover37cde972013-05-04 20:14:09 +0000989
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000990 RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.Address,
Lang Hames9a891052014-09-07 04:13:13 +0000991 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000992 RelocationEntry REmovk_g2(SectionID, StubTargetAddr - Section.Address + 4,
Lang Hames9a891052014-09-07 04:13:13 +0000993 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000994 RelocationEntry REmovk_g1(SectionID, StubTargetAddr - Section.Address + 8,
Lang Hames9a891052014-09-07 04:13:13 +0000995 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
Tim Northover37cde972013-05-04 20:14:09 +0000996 RelocationEntry REmovk_g0(SectionID,
997 StubTargetAddr - Section.Address + 12,
998 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
999
1000 if (Value.SymbolName) {
1001 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1002 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1003 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1004 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1005 } else {
1006 addRelocationForSection(REmovz_g3, Value.SectionID);
1007 addRelocationForSection(REmovk_g2, Value.SectionID);
1008 addRelocationForSection(REmovk_g1, Value.SectionID);
1009 addRelocationForSection(REmovk_g0, Value.SectionID);
1010 }
1011 resolveRelocation(Section, Offset,
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001012 (uint64_t)Section.Address + Section.StubOffset, RelType,
1013 0);
Tim Northover37cde972013-05-04 20:14:09 +00001014 Section.StubOffset += getMaxStubSize();
1015 }
1016 } else if (Arch == Triple::arm &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001017 (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1018 RelType == ELF::R_ARM_JUMP24)) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001019 // This is an ARM branch relocation, need to use a stub function.
1020 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001021 SectionEntry &Section = Sections[SectionID];
Eli Bendersky4c647582012-01-16 08:56:09 +00001022
Eric Christopherc33f6222012-10-23 17:19:15 +00001023 // Look for an existing stub.
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001024 StubMap::const_iterator i = Stubs.find(Value);
1025 if (i != Stubs.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001026 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second,
1027 RelType, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001028 DEBUG(dbgs() << " Stub function found\n");
1029 } else {
1030 // Create a new stub function.
1031 DEBUG(dbgs() << " Create a new stub function\n");
1032 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001033 uint8_t *StubTargetAddr =
1034 createStubFunction(Section.Address + Section.StubOffset);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001035 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Tim Northover3b684d82013-05-28 19:48:19 +00001036 ELF::R_ARM_PRIVATE_0, Value.Addend);
Eli Bendersky667b8792012-05-01 10:41:12 +00001037 if (Value.SymbolName)
1038 addRelocationForSymbol(RE, Value.SymbolName);
1039 else
1040 addRelocationForSection(RE, Value.SectionID);
1041
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001042 resolveRelocation(Section, Offset,
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001043 (uint64_t)Section.Address + Section.StubOffset, RelType,
1044 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +00001045 Section.StubOffset += getMaxStubSize();
1046 }
Akira Hatanakaa667aad2012-12-03 23:12:19 +00001047 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
1048 RelType == ELF::R_MIPS_26) {
Akira Hatanaka111174b2012-08-17 21:28:04 +00001049 // This is an Mips branch relocation, need to use a stub function.
1050 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001051 SectionEntry &Section = Sections[SectionID];
1052 uint8_t *Target = Section.Address + Offset;
Akira Hatanaka111174b2012-08-17 21:28:04 +00001053 uint32_t *TargetAddress = (uint32_t *)Target;
1054
1055 // Extract the addend from the instruction.
1056 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
1057
1058 Value.Addend += Addend;
1059
1060 // Look up for existing stub.
1061 StubMap::const_iterator i = Stubs.find(Value);
1062 if (i != Stubs.end()) {
Petar Jovanovic45115f82013-11-19 21:56:00 +00001063 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1064 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001065 DEBUG(dbgs() << " Stub function found\n");
1066 } else {
1067 // Create a new stub function.
1068 DEBUG(dbgs() << " Create a new stub function\n");
1069 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001070 uint8_t *StubTargetAddr =
1071 createStubFunction(Section.Address + Section.StubOffset);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001072
1073 // Creating Hi and Lo relocations for the filled stub instructions.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001074 RelocationEntry REHi(SectionID, StubTargetAddr - Section.Address,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001075 ELF::R_MIPS_UNUSED1, Value.Addend);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001076 RelocationEntry RELo(SectionID, StubTargetAddr - Section.Address + 4,
Akira Hatanaka2e236242013-07-24 01:58:40 +00001077 ELF::R_MIPS_UNUSED2, Value.Addend);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001078
1079 if (Value.SymbolName) {
1080 addRelocationForSymbol(REHi, Value.SymbolName);
1081 addRelocationForSymbol(RELo, Value.SymbolName);
1082 } else {
1083 addRelocationForSection(REHi, Value.SectionID);
1084 addRelocationForSection(RELo, Value.SectionID);
1085 }
1086
Petar Jovanovic45115f82013-11-19 21:56:00 +00001087 RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset);
1088 addRelocationForSection(RE, SectionID);
Akira Hatanaka111174b2012-08-17 21:28:04 +00001089 Section.StubOffset += getMaxStubSize();
1090 }
Bill Schmidt0a9170d2013-07-26 01:35:43 +00001091 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001092 if (RelType == ELF::R_PPC64_REL24) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001093 // Determine ABI variant in use for this object.
1094 unsigned AbiVariant;
Lang Hamesb5c7b1f2014-11-26 16:54:40 +00001095 Obj.getPlatformFlags(AbiVariant);
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001096 AbiVariant &= ELF::EF_PPC64_ABI;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001097 // A PPC branch relocation will need a stub function if the target is
1098 // an external symbol (Symbol::ST_Unknown) or if the target address
1099 // is not within the signed 24-bits branch address.
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001100 SectionEntry &Section = Sections[SectionID];
1101 uint8_t *Target = Section.Address + Offset;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001102 bool RangeOverflow = false;
1103 if (SymType != SymbolRef::ST_Unknown) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001104 if (AbiVariant != 2) {
1105 // In the ELFv1 ABI, a function call may point to the .opd entry,
1106 // so the final symbol value is calculated based on the relocation
1107 // values in the .opd section.
1108 findOPDEntrySection(Obj, ObjSectionToID, Value);
1109 } else {
1110 // In the ELFv2 ABI, a function symbol may provide a local entry
1111 // point, which must be used for direct calls.
1112 uint8_t SymOther;
1113 Symbol->getOther(SymOther);
1114 Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
1115 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001116 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
1117 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
1118 // If it is within 24-bits branch range, just set the branch target
1119 if (SignExtend32<24>(delta) == delta) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001120 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001121 if (Value.SymbolName)
1122 addRelocationForSymbol(RE, Value.SymbolName);
1123 else
1124 addRelocationForSection(RE, Value.SectionID);
1125 } else {
1126 RangeOverflow = true;
1127 }
1128 }
David Blaikiedc3f01e2015-03-09 01:57:13 +00001129 if (SymType == SymbolRef::ST_Unknown || RangeOverflow) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001130 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
1131 // larger than 24-bits.
1132 StubMap::const_iterator i = Stubs.find(Value);
1133 if (i != Stubs.end()) {
1134 // Symbol function stub already created, just relocate to it
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001135 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001136 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001137 DEBUG(dbgs() << " Stub function found\n");
1138 } else {
1139 // Create a new stub function.
1140 DEBUG(dbgs() << " Create a new stub function\n");
1141 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001142 uint8_t *StubTargetAddr =
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001143 createStubFunction(Section.Address + Section.StubOffset,
1144 AbiVariant);
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001145 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001146 ELF::R_PPC64_ADDR64, Value.Addend);
1147
1148 // Generates the 64-bits address loads as exemplified in section
Ulrich Weigand32626012014-06-20 18:17:56 +00001149 // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to
1150 // apply to the low part of the instructions, so we have to update
1151 // the offset according to the target endianness.
1152 uint64_t StubRelocOffset = StubTargetAddr - Section.Address;
1153 if (!IsTargetLittleEndian)
1154 StubRelocOffset += 2;
1155
1156 RelocationEntry REhst(SectionID, StubRelocOffset + 0,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001157 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001158 RelocationEntry REhr(SectionID, StubRelocOffset + 4,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001159 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001160 RelocationEntry REh(SectionID, StubRelocOffset + 12,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001161 ELF::R_PPC64_ADDR16_HI, Value.Addend);
Ulrich Weigand32626012014-06-20 18:17:56 +00001162 RelocationEntry REl(SectionID, StubRelocOffset + 16,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001163 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1164
1165 if (Value.SymbolName) {
1166 addRelocationForSymbol(REhst, Value.SymbolName);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001167 addRelocationForSymbol(REhr, Value.SymbolName);
1168 addRelocationForSymbol(REh, Value.SymbolName);
1169 addRelocationForSymbol(REl, Value.SymbolName);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001170 } else {
1171 addRelocationForSection(REhst, Value.SectionID);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001172 addRelocationForSection(REhr, Value.SectionID);
1173 addRelocationForSection(REh, Value.SectionID);
1174 addRelocationForSection(REl, Value.SectionID);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001175 }
1176
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001177 resolveRelocation(Section, Offset,
Andrew Kaylorfb05a502012-11-02 19:45:23 +00001178 (uint64_t)Section.Address + Section.StubOffset,
1179 RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001180 Section.StubOffset += getMaxStubSize();
1181 }
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001182 if (SymType == SymbolRef::ST_Unknown) {
Ulrich Weigandfa84ac92014-03-11 15:26:27 +00001183 // Restore the TOC for external calls
Ulrich Weigand752b5c92014-07-20 23:53:14 +00001184 if (AbiVariant == 2)
1185 writeInt32BE(Target + 4, 0xE8410018); // ld r2,28(r1)
1186 else
1187 writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
1188 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001189 }
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +00001190 } else if (RelType == ELF::R_PPC64_TOC16 ||
1191 RelType == ELF::R_PPC64_TOC16_DS ||
1192 RelType == ELF::R_PPC64_TOC16_LO ||
1193 RelType == ELF::R_PPC64_TOC16_LO_DS ||
1194 RelType == ELF::R_PPC64_TOC16_HI ||
1195 RelType == ELF::R_PPC64_TOC16_HA) {
1196 // These relocations are supposed to subtract the TOC address from
1197 // the final value. This does not fit cleanly into the RuntimeDyld
1198 // scheme, since there may be *two* sections involved in determining
1199 // the relocation value (the section of the symbol refered to by the
1200 // relocation, and the TOC section associated with the current module).
1201 //
1202 // Fortunately, these relocations are currently only ever generated
1203 // refering to symbols that themselves reside in the TOC, which means
1204 // that the two sections are actually the same. Thus they cancel out
1205 // and we can immediately resolve the relocation right now.
1206 switch (RelType) {
1207 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break;
1208 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break;
1209 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break;
1210 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break;
1211 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break;
1212 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break;
1213 default: llvm_unreachable("Wrong relocation type.");
1214 }
1215
1216 RelocationValueRef TOCValue;
1217 findPPC64TOCSection(Obj, ObjSectionToID, TOCValue);
1218 if (Value.SymbolName || Value.SectionID != TOCValue.SectionID)
1219 llvm_unreachable("Unsupported TOC relocation.");
1220 Value.Addend -= TOCValue.Addend;
1221 resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001222 } else {
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +00001223 // There are two ways to refer to the TOC address directly: either
1224 // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
1225 // ignored), or via any relocation that refers to the magic ".TOC."
1226 // symbols (in which case the addend is respected).
1227 if (RelType == ELF::R_PPC64_TOC) {
1228 RelType = ELF::R_PPC64_ADDR64;
1229 findPPC64TOCSection(Obj, ObjSectionToID, Value);
1230 } else if (TargetName == ".TOC.") {
1231 findPPC64TOCSection(Obj, ObjSectionToID, Value);
1232 Value.Addend += Addend;
1233 }
1234
Rafael Espindola4d4a48d2013-04-29 14:44:23 +00001235 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
Richard Mittonad6d3492013-08-16 18:54:26 +00001236
1237 if (Value.SymbolName)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +00001238 addRelocationForSymbol(RE, Value.SymbolName);
1239 else
1240 addRelocationForSection(RE, Value.SectionID);
1241 }
Richard Sandifordca044082013-05-03 14:15:35 +00001242 } else if (Arch == Triple::systemz &&
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001243 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
Richard Sandifordca044082013-05-03 14:15:35 +00001244 // Create function stubs for both PLT and GOT references, regardless of
1245 // whether the GOT reference is to data or code. The stub contains the
1246 // full address of the symbol, as needed by GOT references, and the
1247 // executable part only adds an overhead of 8 bytes.
1248 //
1249 // We could try to conserve space by allocating the code and data
1250 // parts of the stub separately. However, as things stand, we allocate
1251 // a stub for every relocation, so using a GOT in JIT code should be
1252 // no less space efficient than using an explicit constant pool.
1253 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1254 SectionEntry &Section = Sections[SectionID];
1255
1256 // Look for an existing stub.
1257 StubMap::const_iterator i = Stubs.find(Value);
1258 uintptr_t StubAddress;
1259 if (i != Stubs.end()) {
1260 StubAddress = uintptr_t(Section.Address) + i->second;
1261 DEBUG(dbgs() << " Stub function found\n");
1262 } else {
1263 // Create a new stub function.
1264 DEBUG(dbgs() << " Create a new stub function\n");
1265
1266 uintptr_t BaseAddress = uintptr_t(Section.Address);
1267 uintptr_t StubAlignment = getStubAlignment();
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001268 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1269 -StubAlignment;
Richard Sandifordca044082013-05-03 14:15:35 +00001270 unsigned StubOffset = StubAddress - BaseAddress;
1271
1272 Stubs[Value] = StubOffset;
1273 createStubFunction((uint8_t *)StubAddress);
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001274 RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
Lang Hames40e200e2014-08-25 23:33:48 +00001275 Value.Offset);
Richard Sandifordca044082013-05-03 14:15:35 +00001276 if (Value.SymbolName)
1277 addRelocationForSymbol(RE, Value.SymbolName);
1278 else
1279 addRelocationForSection(RE, Value.SectionID);
1280 Section.StubOffset = StubOffset + getMaxStubSize();
1281 }
1282
1283 if (RelType == ELF::R_390_GOTENT)
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001284 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1285 Addend);
Richard Sandifordca044082013-05-03 14:15:35 +00001286 else
1287 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001288 } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001289 // The way the PLT relocations normally work is that the linker allocates
1290 // the
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001291 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001292 // entry will then jump to an address provided by the GOT. On first call,
1293 // the
1294 // GOT address will point back into PLT code that resolves the symbol. After
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001295 // the first call, the GOT entry points to the actual function.
1296 //
1297 // For local functions we're ignoring all of that here and just replacing
1298 // the PLT32 relocation type with PC32, which will translate the relocation
1299 // into a PC-relative call directly to the function. For external symbols we
1300 // can't be sure the function will be within 2^32 bytes of the call site, so
1301 // we need to create a stub, which calls into the GOT. This case is
1302 // equivalent to the usual PLT implementation except that we use the stub
1303 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1304 // rather than allocating a PLT section.
1305 if (Value.SymbolName) {
1306 // This is a call to an external function.
1307 // Look for an existing stub.
1308 SectionEntry &Section = Sections[SectionID];
1309 StubMap::const_iterator i = Stubs.find(Value);
1310 uintptr_t StubAddress;
1311 if (i != Stubs.end()) {
1312 StubAddress = uintptr_t(Section.Address) + i->second;
1313 DEBUG(dbgs() << " Stub function found\n");
1314 } else {
1315 // Create a new stub function (equivalent to a PLT entry).
1316 DEBUG(dbgs() << " Create a new stub function\n");
1317
1318 uintptr_t BaseAddress = uintptr_t(Section.Address);
1319 uintptr_t StubAlignment = getStubAlignment();
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001320 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
1321 -StubAlignment;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001322 unsigned StubOffset = StubAddress - BaseAddress;
1323 Stubs[Value] = StubOffset;
1324 createStubFunction((uint8_t *)StubAddress);
1325
1326 // Create a GOT entry for the external function.
1327 GOTEntries.push_back(Value);
1328
1329 // Make our stub function a relative call to the GOT entry.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001330 RelocationEntry RE(SectionID, StubOffset + 2, ELF::R_X86_64_GOTPCREL,
1331 -4);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001332 addRelocationForSymbol(RE, Value.SymbolName);
1333
1334 // Bump our stub offset counter
1335 Section.StubOffset = StubOffset + getMaxStubSize();
1336 }
1337
1338 // Make the target call a call into the stub table.
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001339 resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1340 Addend);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001341 } else {
1342 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
1343 Value.Offset);
1344 addRelocationForSection(RE, Value.SectionID);
1345 }
Eli Bendersky667b8792012-05-01 10:41:12 +00001346 } else {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001347 if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) {
1348 GOTEntries.push_back(Value);
1349 }
1350 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
Eli Bendersky667b8792012-05-01 10:41:12 +00001351 if (Value.SymbolName)
1352 addRelocationForSymbol(RE, Value.SymbolName);
1353 else
1354 addRelocationForSection(RE, Value.SectionID);
1355 }
Juergen Ributzka046709f2014-03-21 07:26:41 +00001356 return ++RelI;
Jim Grosbacheff0a402012-01-16 22:26:39 +00001357}
1358
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001359void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001360
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001361 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator it;
1362 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator end = GOTs.end();
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001363
1364 for (it = GOTs.begin(); it != end; ++it) {
1365 GOTRelocations &GOTEntries = it->second;
1366 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
Craig Topper353eda42014-04-24 06:44:33 +00001367 if (GOTEntries[i].SymbolName != nullptr &&
1368 GOTEntries[i].SymbolName == Name) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001369 GOTEntries[i].Offset = Addr;
1370 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001371 }
1372 }
1373}
1374
1375size_t RuntimeDyldELF::getGOTEntrySize() {
1376 // We don't use the GOT in all of these cases, but it's essentially free
1377 // to put them all here.
1378 size_t Result = 0;
1379 switch (Arch) {
1380 case Triple::x86_64:
1381 case Triple::aarch64:
James Molloybd2ffa02014-04-30 10:15:41 +00001382 case Triple::aarch64_be:
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001383 case Triple::ppc64:
1384 case Triple::ppc64le:
1385 case Triple::systemz:
1386 Result = sizeof(uint64_t);
1387 break;
1388 case Triple::x86:
1389 case Triple::arm:
1390 case Triple::thumb:
1391 case Triple::mips:
1392 case Triple::mipsel:
1393 Result = sizeof(uint32_t);
1394 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001395 default:
1396 llvm_unreachable("Unsupported CPU type!");
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001397 }
1398 return Result;
1399}
1400
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001401uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress, uint64_t Offset) {
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001402
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001403 const size_t GOTEntrySize = getGOTEntrySize();
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001404
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001405 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator it;
1406 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator end =
1407 GOTs.end();
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001408
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001409 int GOTIndex = -1;
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001410 for (it = GOTs.begin(); it != end; ++it) {
1411 SID GOTSectionID = it->first;
1412 const GOTRelocations &GOTEntries = it->second;
1413
1414 // Find the matching entry in our vector.
1415 uint64_t SymbolOffset = 0;
1416 for (int i = 0, e = GOTEntries.size(); i != e; ++i) {
Craig Topper353eda42014-04-24 06:44:33 +00001417 if (!GOTEntries[i].SymbolName) {
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001418 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress &&
1419 GOTEntries[i].Offset == Offset) {
1420 GOTIndex = i;
1421 SymbolOffset = GOTEntries[i].Offset;
1422 break;
1423 }
1424 } else {
1425 // GOT entries for external symbols use the addend as the address when
1426 // the external symbol has been resolved.
1427 if (GOTEntries[i].Offset == LoadAddress) {
1428 GOTIndex = i;
1429 // Don't use the Addend here. The relocation handler will use it.
1430 break;
1431 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001432 }
1433 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001434
1435 if (GOTIndex != -1) {
1436 if (GOTEntrySize == sizeof(uint64_t)) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001437 uint64_t *LocalGOTAddr = (uint64_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001438 // Fill in this entry with the address of the symbol being referenced.
1439 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset;
1440 } else {
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001441 uint32_t *LocalGOTAddr = (uint32_t *)getSectionAddress(GOTSectionID);
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001442 // Fill in this entry with the address of the symbol being referenced.
1443 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset);
1444 }
1445
1446 // Calculate the load address of this entry
1447 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize);
1448 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001449 }
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001450
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001451 assert(GOTIndex != -1 && "Unable to find requested GOT entry.");
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001452 return 0;
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001453}
1454
Lang Hamesb5c7b1f2014-11-26 16:54:40 +00001455void RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj,
Lang Hames36072da2014-05-12 21:39:59 +00001456 ObjSectionToIDMap &SectionMap) {
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001457 // If necessary, allocate the global offset table
Lang Hames633fe142015-03-30 03:37:06 +00001458 size_t numGOTEntries = GOTEntries.size();
1459 if (numGOTEntries != 0) {
1460 // Allocate memory for the section
1461 unsigned SectionID = Sections.size();
1462 size_t TotalSize = numGOTEntries * getGOTEntrySize();
1463 uint8_t *Addr = MemMgr.allocateDataSection(TotalSize, getGOTEntrySize(),
1464 SectionID, ".got", false);
1465 if (!Addr)
1466 report_fatal_error("Unable to allocate memory for GOT!");
Andrew Kaylor480dcb32013-10-05 01:52:09 +00001467
Lang Hames633fe142015-03-30 03:37:06 +00001468 GOTs.push_back(std::make_pair(SectionID, GOTEntries));
1469 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0));
1470 // For now, initialize all GOT entries to zero. We'll fill them in as
1471 // needed when GOT-based relocations are applied.
1472 memset(Addr, 0, TotalSize);
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001473 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001474
1475 // Look for and record the EH frame section.
1476 ObjSectionToIDMap::iterator i, e;
1477 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1478 const SectionRef &Section = i->first;
1479 StringRef Name;
1480 Section.getName(Name);
1481 if (Name == ".eh_frame") {
1482 UnregisteredEHFrameSections.push_back(i->second);
1483 break;
1484 }
1485 }
Andrew Kaylor4612fed2013-08-19 23:27:43 +00001486}
1487
Lang Hamesb5c7b1f2014-11-26 16:54:40 +00001488bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile &Obj) const {
1489 return Obj.isELF();
Lang Hames173c69f2014-01-08 04:09:09 +00001490}
1491
Eli Bendersky4c647582012-01-16 08:56:09 +00001492} // namespace llvm