blob: 1a2552deed95ed5d0826b140f4eec9f7bd884284 [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 {
Tim Northover94bc73d2012-10-29 10:47:04 +000023
Eli Bendersky058d6472012-01-22 07:05:02 +000024class RuntimeDyldELF : public RuntimeDyldImpl {
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000025
Juergen Ributzka7608dc02014-03-21 20:28:42 +000026 void resolveRelocation(const SectionEntry &Section, uint64_t Offset,
27 uint64_t Value, uint32_t Type, int64_t Addend,
Petar Jovanovic97202832015-05-28 13:48:41 +000028 uint64_t SymOffset = 0, SID SectionID = 0);
Rafael Espindolaf1f1c622013-04-29 17:24:34 +000029
Juergen Ributzka7608dc02014-03-21 20:28:42 +000030 void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset,
31 uint64_t Value, uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +000032 uint64_t SymOffset);
Eli Bendersky058d6472012-01-22 07:05:02 +000033
Juergen Ributzka7608dc02014-03-21 20:28:42 +000034 void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset,
35 uint32_t Value, uint32_t Type, int32_t Addend);
Eli Bendersky058d6472012-01-22 07:05:02 +000036
Juergen Ributzka7608dc02014-03-21 20:28:42 +000037 void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset,
38 uint64_t Value, uint32_t Type, int64_t Addend);
Tim Northoverfa1b2f82013-05-04 20:13:59 +000039
Juergen Ributzka7608dc02014-03-21 20:28:42 +000040 void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset,
41 uint32_t Value, uint32_t Type, int32_t Addend);
Eli Bendersky058d6472012-01-22 07:05:02 +000042
Juergen Ributzka7608dc02014-03-21 20:28:42 +000043 void resolveMIPSRelocation(const SectionEntry &Section, uint64_t Offset,
44 uint32_t Value, uint32_t Type, int32_t Addend);
Akira Hatanaka111174b2012-08-17 21:28:04 +000045
Juergen Ributzka7608dc02014-03-21 20:28:42 +000046 void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset,
47 uint64_t Value, uint32_t Type, int64_t Addend);
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000048
Juergen Ributzka7608dc02014-03-21 20:28:42 +000049 void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset,
50 uint64_t Value, uint32_t Type, int64_t Addend);
Eli Bendersky058d6472012-01-22 07:05:02 +000051
Petar Jovanovic97202832015-05-28 13:48:41 +000052 void resolveMIPS64Relocation(const SectionEntry &Section, uint64_t Offset,
53 uint64_t Value, uint32_t Type, int64_t Addend,
54 uint64_t SymOffset, SID SectionID);
55
56 int64_t evaluateMIPS64Relocation(const SectionEntry &Section,
57 uint64_t Offset, uint64_t Value,
58 uint32_t Type, int64_t Addend,
59 uint64_t SymOffset, SID SectionID);
60
61 void applyMIPS64Relocation(uint8_t *TargetPtr, int64_t CalculatedValue,
62 uint32_t Type);
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
Petar Jovanovic97202832015-05-28 13:48:41 +000069 else if (IsMipsO32ABI)
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000070 return 16;
71 else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
72 return 44;
73 else if (Arch == Triple::x86_64)
74 return 6; // 2-byte jmp instruction + 32-bit relative address
75 else if (Arch == Triple::systemz)
76 return 16;
77 else
78 return 0;
79 }
80
Craig Topperb51ff602014-03-08 07:51:20 +000081 unsigned getStubAlignment() override {
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000082 if (Arch == Triple::systemz)
83 return 8;
84 else
85 return 1;
86 }
87
Petar Jovanovic97202832015-05-28 13:48:41 +000088 void setMipsABI(const ObjectFile &Obj) override;
89
Rafael Espindola3dc0d052015-06-19 20:58:43 +000090 void findPPC64TOCSection(const ELFObjectFileBase &Obj,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000091 ObjSectionToIDMap &LocalSections,
Ulrich Weigand8f1f87c2014-06-27 10:32:14 +000092 RelocationValueRef &Rel);
Rafael Espindola3dc0d052015-06-19 20:58:43 +000093 void findOPDEntrySection(const ELFObjectFileBase &Obj,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000094 ObjSectionToIDMap &LocalSections,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000095 RelocationValueRef &Rel);
96
Andrew Kaylor4612fed2013-08-19 23:27:43 +000097 size_t getGOTEntrySize();
98
Keno Fischer02628de2015-04-14 02:10:35 +000099 SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000100
Keno Fischer02628de2015-04-14 02:10:35 +0000101 // Allocate no GOT entries for use in the given section.
102 uint64_t allocateGOTEntries(unsigned SectionID, unsigned no);
103
104 // Resolve the relvative address of GOTOffset in Section ID and place
105 // it at the given Offset
106 void resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset,
107 uint64_t GOTOffset);
108
109 // For a GOT entry referenced from SectionID, compute a relocation entry
110 // that will place the final resolved value in the GOT slot
111 RelocationEntry computeGOTOffsetRE(unsigned SectionID,
112 uint64_t GOTOffset,
113 uint64_t SymbolOffset,
114 unsigned Type);
115
Keno Fischere6892c82015-05-01 20:21:45 +0000116 // Compute the address in memory where we can find the placeholder
117 void *computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const;
118
119 // Split out common case for createing the RelocationEntry for when the relocation requires
120 // no particular advanced processing.
121 void processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value);
122
Keno Fischer02628de2015-04-14 02:10:35 +0000123 // The tentative ID for the GOT section
124 unsigned GOTSectionID;
125
126 // Records the current number of allocated slots in the GOT
127 // (This would be equivalent to GOTEntries.size() were it not for relocations
128 // that consume more than one slot)
129 unsigned CurrentGOTIndex;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000130
Petar Jovanovic97202832015-05-28 13:48:41 +0000131 // A map from section to a GOT section that has entries for section's GOT
132 // relocations. (Mips64 specific)
133 DenseMap<SID, SID> SectionToGOTMap;
134
135 // A map to avoid duplicate got entries (Mips64 specific)
136 StringMap<uint64_t> GOTSymbolOffsets;
137
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000138 // When a module is loaded we save the SectionID of the EH frame section
139 // in a table until we receive a request to register all unregistered
140 // EH frame sections with the memory manager.
141 SmallVector<SID, 2> UnregisteredEHFrameSections;
Andrew Kaylorc442a762013-10-16 00:14:21 +0000142 SmallVector<SID, 2> RegisteredEHFrameSections;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000143
Eli Bendersky058d6472012-01-22 07:05:02 +0000144public:
Lang Hames633fe142015-03-30 03:37:06 +0000145 RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
146 RuntimeDyld::SymbolResolver &Resolver);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000147 ~RuntimeDyldELF() override;
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000148
149 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
150 loadObject(const object::ObjectFile &O) override;
Preston Gurdcc31af92012-04-16 22:12:58 +0000151
Craig Topperb51ff602014-03-08 07:51:20 +0000152 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000153 relocation_iterator
154 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000155 const ObjectFile &Obj,
156 ObjSectionToIDMap &ObjSectionToID,
Lang Hamesa5cd9502014-11-27 05:40:13 +0000157 StubMap &Stubs) override;
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000158 bool isCompatibleFile(const object::ObjectFile &Obj) const override;
Craig Topperb51ff602014-03-08 07:51:20 +0000159 void registerEHFrames() override;
160 void deregisterEHFrames() override;
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000161 void finalizeLoad(const ObjectFile &Obj,
Lang Hames36072da2014-05-12 21:39:59 +0000162 ObjSectionToIDMap &SectionMap) override;
Eli Bendersky058d6472012-01-22 07:05:02 +0000163};
164
165} // end namespace llvm
166
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000167#endif