blob: a56bd0cb3ef600ae73bea0c834d78358c52350f5 [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
14#ifndef LLVM_RUNTIME_DYLD_ELF_H
15#define LLVM_RUNTIME_DYLD_ELF_H
16
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
24namespace {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000025// Helper for extensive error checking in debug builds.
26error_code Check(error_code Err) {
27 if (Err) {
28 report_fatal_error(Err.message());
Tim Northover94bc73d2012-10-29 10:47:04 +000029 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +000030 return Err;
31}
Tim Northover94bc73d2012-10-29 10:47:04 +000032} // end anonymous namespace
33
Eli Bendersky058d6472012-01-22 07:05:02 +000034class RuntimeDyldELF : public RuntimeDyldImpl {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000035 void resolveRelocation(const SectionEntry &Section, uint64_t Offset,
36 uint64_t Value, uint32_t Type, int64_t Addend,
37 uint64_t SymOffset = 0);
Rafael Espindolaf1f1c622013-04-29 17:24:34 +000038
Juergen Ributzka7608dc02014-03-21 20:28:42 +000039 void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset,
40 uint64_t Value, uint32_t Type, int64_t Addend,
Andrew Kaylor4612fed2013-08-19 23:27:43 +000041 uint64_t SymOffset);
Eli Bendersky058d6472012-01-22 07:05:02 +000042
Juergen Ributzka7608dc02014-03-21 20:28:42 +000043 void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset,
44 uint32_t Value, uint32_t Type, int32_t Addend);
Eli Bendersky058d6472012-01-22 07:05:02 +000045
Juergen Ributzka7608dc02014-03-21 20:28:42 +000046 void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset,
47 uint64_t Value, uint32_t Type, int64_t Addend);
Tim Northoverfa1b2f82013-05-04 20:13:59 +000048
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
Juergen Ributzka7608dc02014-03-21 20:28:42 +000052 void resolveMIPSRelocation(const SectionEntry &Section, uint64_t Offset,
53 uint32_t Value, uint32_t Type, int32_t Addend);
Akira Hatanaka111174b2012-08-17 21:28:04 +000054
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
Craig Topperb51ff602014-03-08 07:51:20 +000061 unsigned getMaxStubSize() override {
James Molloybd2ffa02014-04-30 10:15:41 +000062 if (Arch == Triple::aarch64 || Arch == Triple::arm64 ||
63 Arch == Triple::aarch64_be || Arch == Triple::arm64_be)
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000064 return 20; // movz; movk; movk; movk; br
65 if (Arch == Triple::arm || Arch == Triple::thumb)
66 return 8; // 32-bit instruction and 32-bit address
67 else if (Arch == Triple::mipsel || Arch == Triple::mips)
68 return 16;
69 else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
70 return 44;
71 else if (Arch == Triple::x86_64)
72 return 6; // 2-byte jmp instruction + 32-bit relative address
73 else if (Arch == Triple::systemz)
74 return 16;
75 else
76 return 0;
77 }
78
Craig Topperb51ff602014-03-08 07:51:20 +000079 unsigned getStubAlignment() override {
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000080 if (Arch == Triple::systemz)
81 return 8;
82 else
83 return 1;
84 }
85
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000086 uint64_t findPPC64TOC() const;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000087 void findOPDEntrySection(ObjectImage &Obj, ObjSectionToIDMap &LocalSections,
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000088 RelocationValueRef &Rel);
89
Andrew Kaylor4612fed2013-08-19 23:27:43 +000090 uint64_t findGOTEntry(uint64_t LoadAddr, uint64_t Offset);
91 size_t getGOTEntrySize();
92
Craig Topperb51ff602014-03-08 07:51:20 +000093 void updateGOTEntries(StringRef Name, uint64_t Addr) override;
Andrew Kaylor4612fed2013-08-19 23:27:43 +000094
Alp Tokercb402912014-01-24 17:20:08 +000095 // Relocation entries for symbols whose position-independent offset is
Andrew Kaylor480dcb32013-10-05 01:52:09 +000096 // updated in a global offset table.
Andrew Kaylor480dcb32013-10-05 01:52:09 +000097 typedef SmallVector<RelocationValueRef, 2> GOTRelocations;
98 GOTRelocations GOTEntries; // List of entries requiring finalization.
99 SmallVector<std::pair<SID, GOTRelocations>, 8> GOTs; // Allocated tables.
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000100
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000101 // When a module is loaded we save the SectionID of the EH frame section
102 // in a table until we receive a request to register all unregistered
103 // EH frame sections with the memory manager.
104 SmallVector<SID, 2> UnregisteredEHFrameSections;
Andrew Kaylorc442a762013-10-16 00:14:21 +0000105 SmallVector<SID, 2> RegisteredEHFrameSections;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000106
Eli Bendersky058d6472012-01-22 07:05:02 +0000107public:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000108 RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
Preston Gurdcc31af92012-04-16 22:12:58 +0000109
Craig Topperb51ff602014-03-08 07:51:20 +0000110 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000111 relocation_iterator
112 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
113 ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID,
114 const SymbolTableMap &Symbols, StubMap &Stubs) override;
Craig Topperb51ff602014-03-08 07:51:20 +0000115 bool isCompatibleFormat(const ObjectBuffer *Buffer) const override;
116 bool isCompatibleFile(const object::ObjectFile *Buffer) const override;
Craig Topperb51ff602014-03-08 07:51:20 +0000117 void registerEHFrames() override;
118 void deregisterEHFrames() override;
119 void finalizeLoad(ObjSectionToIDMap &SectionMap) override;
Preston Gurdcc31af92012-04-16 22:12:58 +0000120 virtual ~RuntimeDyldELF();
Lang Hames951b2352014-03-08 18:45:12 +0000121
122 static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer);
David Blaikie7a1e7752014-04-29 21:52:46 +0000123 static ObjectImage *createObjectImageFromFile(std::unique_ptr<object::ObjectFile> Obj);
Eli Bendersky058d6472012-01-22 07:05:02 +0000124};
125
126} // end namespace llvm
127
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000128#endif