[MCJIT] Fix the ARM BR24 relocation in RuntimeDyldMachO.
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / RuntimeDyldMachO.h
1 //===-- RuntimeDyldMachO.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 // MachO support for MC-JIT runtime dynamic linker.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_RUNTIME_DYLD_MACHO_H
15 #define LLVM_RUNTIME_DYLD_MACHO_H
16
17 #include "ObjectImageCommon.h"
18 #include "RuntimeDyldImpl.h"
19 #include "llvm/Object/MachO.h"
20 #include "llvm/Support/Format.h"
21
22 #define DEBUG_TYPE "dyld"
23
24 using namespace llvm;
25 using namespace llvm::object;
26
27 namespace llvm {
28 class RuntimeDyldMachO : public RuntimeDyldImpl {
29 protected:
30   struct SectionOffsetPair {
31     unsigned SectionID;
32     uint64_t Offset;
33   };
34
35   struct EHFrameRelatedSections {
36     EHFrameRelatedSections()
37         : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
38           TextSID(RTDYLD_INVALID_SECTION_ID),
39           ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
40
41     EHFrameRelatedSections(SID EH, SID T, SID Ex)
42         : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
43     SID EHFrameSID;
44     SID TextSID;
45     SID ExceptTabSID;
46   };
47
48   // When a module is loaded we save the SectionID of the EH frame section
49   // in a table until we receive a request to register all unregistered
50   // EH frame sections with the memory manager.
51   SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections;
52
53   RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
54
55   /// Extract the addend encoded in the instruction.
56   int64_t decodeAddend(uint8_t *LocalAddress, unsigned NumBytes,
57                        MachO::RelocationInfoType RelType) const;
58
59   /// Construct a RelocationValueRef representing the relocation target.
60   /// For Symbols in known sections, this will return a RelocationValueRef
61   /// representing a (SectionID, Offset) pair.
62   /// For Symbols whose section is not known, this will return a
63   /// (SymbolName, Offset) pair, where the Offset is taken from the instruction
64   /// immediate (held in RE.Addend).
65   /// In both cases the Addend field is *NOT* fixed up to be PC-relative. That
66   /// should be done by the caller where appropriate by calling makePCRel on
67   /// the RelocationValueRef.
68   RelocationValueRef getRelocationValueRef(ObjectImage &ObjImg,
69                                            const relocation_iterator &RI,
70                                            const RelocationEntry &RE,
71                                            ObjSectionToIDMap &ObjSectionToID,
72                                            const SymbolTableMap &Symbols);
73
74   /// Make the RelocationValueRef addend PC-relative.
75   void makeValueAddendPCRel(RelocationValueRef &Value, ObjectImage &ObjImg,
76                             const relocation_iterator &RI,
77                             unsigned OffsetToNextPC);
78
79   /// Dump information about the relocation entry (RE) and resolved value.
80   void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const;
81
82 public:
83   /// Create an ObjectImage from the given ObjectBuffer.
84   static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) {
85     return new ObjectImageCommon(InputBuffer);
86   }
87
88   /// Create an ObjectImage from the given ObjectFile.
89   static ObjectImage *
90   createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) {
91     return new ObjectImageCommon(std::move(InputObject));
92   }
93
94   /// Create a RuntimeDyldMachO instance for the given target architecture.
95   static std::unique_ptr<RuntimeDyldMachO> create(Triple::ArchType Arch,
96                                                   RTDyldMemoryManager *mm);
97
98   /// Write the least significant 'Size' bytes in 'Value' out at the address
99   /// pointed to by Addr. Check for overflow.
100   bool writeBytesUnaligned(uint8_t *Addr, uint64_t Value, unsigned Size);
101
102   SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
103
104   bool isCompatibleFormat(const ObjectBuffer *Buffer) const override;
105   bool isCompatibleFile(const object::ObjectFile *Obj) const override;
106   void registerEHFrames() override;
107 };
108
109 /// RuntimeDyldMachOTarget - Templated base class for generic MachO linker
110 /// algorithms and data structures.
111 ///
112 /// Concrete, target specific sub-classes can be accessed via the impl()
113 /// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously
114 /// Recurring Template Idiom). Concrete subclasses for each target
115 /// can be found in ./Targets.
116 template <typename Impl>
117 class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO {
118 private:
119   Impl &impl() { return static_cast<Impl &>(*this); }
120   const Impl &impl() const { return static_cast<const Impl &>(*this); }
121
122 protected:
123
124   /// Parse the given relocation, which must be a non-scattered, and
125   /// return a RelocationEntry representing the information. The 'Addend' field
126   /// will contain the unmodified instruction immediate.
127   RelocationEntry getBasicRelocationEntry(unsigned SectionID,
128                                           ObjectImage &ObjImg,
129                                           const relocation_iterator &RI) const {
130     const MachOObjectFile &Obj =
131       static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
132     MachO::any_relocation_info RelInfo =
133       Obj.getRelocation(RI->getRawDataRefImpl());
134
135     const SectionEntry &Section = Sections[SectionID];
136     bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
137     unsigned Size = Obj.getAnyRelocationLength(RelInfo);
138     uint64_t Offset;
139     RI->getOffset(Offset);
140     uint8_t *LocalAddress = Section.Address + Offset;
141     unsigned NumBytes = 1 << Size;
142     MachO::RelocationInfoType RelType =
143       static_cast<MachO::RelocationInfoType>(Obj.getAnyRelocationType(RelInfo));
144     int64_t Addend = impl().decodeAddend(LocalAddress, NumBytes, RelType);
145
146     return RelocationEntry(SectionID, Offset, RelType, Addend, IsPCRel, Size);
147   }
148
149 public:
150   RuntimeDyldMachOCRTPBase(RTDyldMemoryManager *mm) : RuntimeDyldMachO(mm) {}
151
152   void finalizeLoad(ObjectImage &ObjImg, ObjSectionToIDMap &SectionMap) {
153     unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
154     unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
155     unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
156     ObjSectionToIDMap::iterator i, e;
157
158     for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
159       const SectionRef &Section = i->first;
160       StringRef Name;
161       Section.getName(Name);
162       if (Name == "__eh_frame")
163         EHFrameSID = i->second;
164       else if (Name == "__text")
165         TextSID = i->second;
166       else if (Name == "__gcc_except_tab")
167         ExceptTabSID = i->second;
168       else
169         impl().finalizeSection(ObjImg, i->second, Section);
170     }
171     UnregisteredEHFrameSections.push_back(
172         EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
173   }
174 };
175
176 } // end namespace llvm
177
178 #undef DEBUG_TYPE
179
180 #endif