blob: f37bd0bbaea657eccdb78aade0daef32259624e9 [file] [log] [blame]
Eli Bendersky058d6472012-01-22 07:05:02 +00001//===-- RuntimeDyldELF.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-===//
2//
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// ELF support for MC-JIT runtime dynamic linker.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
15#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
Eli Bendersky058d6472012-01-22 07:05:02 +000016
17#include "RuntimeDyldImpl.h"
Andrew Kaylor480dcb32013-10-05 01:52:09 +000018#include "llvm/ADT/DenseMap.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000019
20using namespace llvm;
21
Eli Bendersky058d6472012-01-22 07:05:02 +000022namespace llvm {
Reid Klecknerdafc5d72016-07-06 16:56:42 +000023namespace object {
24class ELFObjectFileBase;
25}
Tim Northover94bc73d2012-10-29 10:47:04 +000026
Eli Bendersky058d6472012-01-22 07:05:02 +000027class RuntimeDyldELF : public RuntimeDyldImpl {
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000028
Juergen Ributzka7608dc02014-03-21 20:28:42 +000029 void resolveRelocation(const SectionEntry &Section, uint64_t Offset,
30 uint64_t Value, uint32_t Type, int64_t Addend,
Petar Jovanovic97202832015-05-28 13:48:41 +000031 uint64_t SymOffset = 0, SID SectionID = 0);
Rafael Espindolaf1f1c622013-04-29 17:24:34 +000032
Juergen Ributzka7608dc02014-03-21 20:28:42 +000033 void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset,
34 uint64_t Value, uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +000035 uint64_t SymOffset);
Eli Bendersky058d6472012-01-22 07:05:02 +000036
Juergen Ributzka7608dc02014-03-21 20:28:42 +000037 void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset,
38 uint32_t Value, uint32_t Type, int32_t Addend);
Eli Bendersky058d6472012-01-22 07:05:02 +000039
Juergen Ributzka7608dc02014-03-21 20:28:42 +000040 void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset,
41 uint64_t Value, uint32_t Type, int64_t Addend);
Tim Northoverfa1b2f82013-05-04 20:13:59 +000042
Eugene Leviantbe2d68f2017-01-09 09:56:31 +000043 bool resolveAArch64ShortBranch(unsigned SectionID, relocation_iterator RelI,
44 const RelocationValueRef &Value);
45
Eugene Leviant3e582c82017-02-06 15:31:28 +000046 void resolveAArch64Branch(unsigned SectionID, const RelocationValueRef &Value,
47 relocation_iterator RelI, StubMap &Stubs);
48
Juergen Ributzka7608dc02014-03-21 20:28:42 +000049 void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset,
50 uint32_t Value, uint32_t Type, int32_t Addend);
Eli Bendersky058d6472012-01-22 07:05:02 +000051
Hal Finkel23cdeee2015-08-04 15:29:00 +000052 void resolvePPC32Relocation(const SectionEntry &Section, uint64_t Offset,
53 uint64_t Value, uint32_t Type, int64_t Addend);
54
Juergen Ributzka7608dc02014-03-21 20:28:42 +000055 void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset,
56 uint64_t Value, uint32_t Type, int64_t Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000057
Juergen Ributzka7608dc02014-03-21 20:28:42 +000058 void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset,
59 uint64_t Value, uint32_t Type, int64_t Addend);
Eli Bendersky058d6472012-01-22 07:05:02 +000060
Alexei Starovoitov4198f2a2017-05-03 17:30:56 +000061 void resolveBPFRelocation(const SectionEntry &Section, uint64_t Offset,
62 uint64_t Value, uint32_t Type, int64_t Addend);
63
Craig Topperb51ff602014-03-08 07:51:20 +000064 unsigned getMaxStubSize() override {
Tim Northovere19bed72014-07-23 12:32:47 +000065 if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be)
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000066 return 20; // movz; movk; movk; movk; br
67 if (Arch == Triple::arm || Arch == Triple::thumb)
68 return 8; // 32-bit instruction and 32-bit address
Nitesh Jain757f74c2017-10-22 09:47:41 +000069 else if (IsMipsO32ABI || IsMipsN32ABI)
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000070 return 16;
Nitesh Jain757f74c2017-10-22 09:47:41 +000071 else if (IsMipsN64ABI)
72 return 32;
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000073 else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
74 return 44;
75 else if (Arch == Triple::x86_64)
76 return 6; // 2-byte jmp instruction + 32-bit relative address
77 else if (Arch == Triple::systemz)
78 return 16;
79 else
80 return 0;
81 }
82
Craig Topperb51ff602014-03-08 07:51:20 +000083 unsigned getStubAlignment() override {
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000084 if (Arch == Triple::systemz)
85 return 8;
86 else
87 return 1;
88 }
89
Petar Jovanovic97202832015-05-28 13:48:41 +000090 void setMipsABI(const ObjectFile &Obj) override;
91
Lang Hames89595312016-04-27 20:24:48 +000092 Error findPPC64TOCSection(const ELFObjectFileBase &Obj,
93 ObjSectionToIDMap &LocalSections,
94 RelocationValueRef &Rel);
95 Error findOPDEntrySection(const ELFObjectFileBase &Obj,
96 ObjSectionToIDMap &LocalSections,
97 RelocationValueRef &Rel);
Simon Dardisc97cfb62016-12-13 11:39:18 +000098protected:
Eugene Leviant3e582c82017-02-06 15:31:28 +000099 size_t getGOTEntrySize() override;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000100
Simon Dardisc97cfb62016-12-13 11:39:18 +0000101private:
Keno Fischer02628de2015-04-14 02:10:35 +0000102 SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000103
Keno Fischer02628de2015-04-14 02:10:35 +0000104 // Allocate no GOT entries for use in the given section.
Eugene Leviant3e582c82017-02-06 15:31:28 +0000105 uint64_t allocateGOTEntries(unsigned no);
106
107 // Find GOT entry corresponding to relocation or create new one.
108 uint64_t findOrAllocGOTEntry(const RelocationValueRef &Value,
109 unsigned GOTRelType);
Keno Fischer02628de2015-04-14 02:10:35 +0000110
111 // Resolve the relvative address of GOTOffset in Section ID and place
112 // it at the given Offset
113 void resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset,
Eugene Leviant3e582c82017-02-06 15:31:28 +0000114 uint64_t GOTOffset, uint32_t Type);
Keno Fischer02628de2015-04-14 02:10:35 +0000115
116 // For a GOT entry referenced from SectionID, compute a relocation entry
117 // that will place the final resolved value in the GOT slot
Eugene Leviant3e582c82017-02-06 15:31:28 +0000118 RelocationEntry computeGOTOffsetRE(uint64_t GOTOffset, uint64_t SymbolOffset,
Keno Fischer02628de2015-04-14 02:10:35 +0000119 unsigned Type);
120
Keno Fischere6892c82015-05-01 20:21:45 +0000121 // Compute the address in memory where we can find the placeholder
122 void *computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const;
123
124 // Split out common case for createing the RelocationEntry for when the relocation requires
125 // no particular advanced processing.
126 void processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value);
127
Petar Jovanovicd22164d2015-08-13 15:12:49 +0000128 // Return matching *LO16 relocation (Mips specific)
129 uint32_t getMatchingLoRelocation(uint32_t RelType,
130 bool IsLocal = false) const;
131
Keno Fischer02628de2015-04-14 02:10:35 +0000132 // The tentative ID for the GOT section
133 unsigned GOTSectionID;
134
135 // Records the current number of allocated slots in the GOT
136 // (This would be equivalent to GOTEntries.size() were it not for relocations
137 // that consume more than one slot)
138 unsigned CurrentGOTIndex;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000139
Simon Dardisc97cfb62016-12-13 11:39:18 +0000140protected:
Petar Jovanovic97202832015-05-28 13:48:41 +0000141 // A map from section to a GOT section that has entries for section's GOT
142 // relocations. (Mips64 specific)
143 DenseMap<SID, SID> SectionToGOTMap;
144
Simon Dardisc97cfb62016-12-13 11:39:18 +0000145private:
Petar Jovanovic97202832015-05-28 13:48:41 +0000146 // A map to avoid duplicate got entries (Mips64 specific)
147 StringMap<uint64_t> GOTSymbolOffsets;
148
Petar Jovanovicd22164d2015-08-13 15:12:49 +0000149 // *HI16 relocations will be added for resolving when we find matching
150 // *LO16 part. (Mips specific)
151 SmallVector<std::pair<RelocationValueRef, RelocationEntry>, 8> PendingRelocs;
152
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000153 // When a module is loaded we save the SectionID of the EH frame section
154 // in a table until we receive a request to register all unregistered
155 // EH frame sections with the memory manager.
156 SmallVector<SID, 2> UnregisteredEHFrameSections;
157
Eugene Leviant3e582c82017-02-06 15:31:28 +0000158 // Map between GOT relocation value and corresponding GOT offset
159 std::map<RelocationValueRef, uint64_t> GOTOffsetMap;
160
161 bool relocationNeedsGot(const RelocationRef &R) const override;
Sanjoy Dasd5658b02015-11-23 21:47:51 +0000162 bool relocationNeedsStub(const RelocationRef &R) const override;
163
Eli Bendersky058d6472012-01-22 07:05:02 +0000164public:
Lang Hames633fe142015-03-30 03:37:06 +0000165 RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
Lang Hamesad4a9112016-08-01 20:49:11 +0000166 JITSymbolResolver &Resolver);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000167 ~RuntimeDyldELF() override;
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000168
Simon Dardisc97cfb62016-12-13 11:39:18 +0000169 static std::unique_ptr<RuntimeDyldELF>
170 create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr,
171 JITSymbolResolver &Resolver);
172
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000173 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
174 loadObject(const object::ObjectFile &O) override;
Preston Gurdcc31af92012-04-16 22:12:58 +0000175
Craig Topperb51ff602014-03-08 07:51:20 +0000176 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
Lang Hames89595312016-04-27 20:24:48 +0000177 Expected<relocation_iterator>
Juergen Ributzka046709f2014-03-21 07:26:41 +0000178 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000179 const ObjectFile &Obj,
180 ObjSectionToIDMap &ObjSectionToID,
Lang Hamesa5cd9502014-11-27 05:40:13 +0000181 StubMap &Stubs) override;
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000182 bool isCompatibleFile(const object::ObjectFile &Obj) const override;
Craig Topperb51ff602014-03-08 07:51:20 +0000183 void registerEHFrames() override;
Lang Hames89595312016-04-27 20:24:48 +0000184 Error finalizeLoad(const ObjectFile &Obj,
185 ObjSectionToIDMap &SectionMap) override;
Eli Bendersky058d6472012-01-22 07:05:02 +0000186};
187
188} // end namespace llvm
189
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000190#endif