xref: /aosp_15_r20/external/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // Implementation of the MC-JIT runtime dynamic linker.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "llvm/ExecutionEngine/RuntimeDyld.h"
15*9880d681SAndroid Build Coastguard Worker #include "RuntimeDyldCheckerImpl.h"
16*9880d681SAndroid Build Coastguard Worker #include "RuntimeDyldCOFF.h"
17*9880d681SAndroid Build Coastguard Worker #include "RuntimeDyldELF.h"
18*9880d681SAndroid Build Coastguard Worker #include "RuntimeDyldImpl.h"
19*9880d681SAndroid Build Coastguard Worker #include "RuntimeDyldMachO.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Object/ELFObjectFile.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Object/COFF.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ManagedStatic.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MathExtras.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MutexGuard.h"
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker using namespace llvm;
27*9880d681SAndroid Build Coastguard Worker using namespace llvm::object;
28*9880d681SAndroid Build Coastguard Worker 
29*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "dyld"
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker namespace {
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker enum RuntimeDyldErrorCode {
34*9880d681SAndroid Build Coastguard Worker   GenericRTDyldError = 1
35*9880d681SAndroid Build Coastguard Worker };
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker // FIXME: This class is only here to support the transition to llvm::Error. It
38*9880d681SAndroid Build Coastguard Worker // will be removed once this transition is complete. Clients should prefer to
39*9880d681SAndroid Build Coastguard Worker // deal with the Error value directly, rather than converting to error_code.
40*9880d681SAndroid Build Coastguard Worker class RuntimeDyldErrorCategory : public std::error_category {
41*9880d681SAndroid Build Coastguard Worker public:
name() const42*9880d681SAndroid Build Coastguard Worker   const char *name() const LLVM_NOEXCEPT override { return "runtimedyld"; }
43*9880d681SAndroid Build Coastguard Worker 
message(int Condition) const44*9880d681SAndroid Build Coastguard Worker   std::string message(int Condition) const override {
45*9880d681SAndroid Build Coastguard Worker     switch (static_cast<RuntimeDyldErrorCode>(Condition)) {
46*9880d681SAndroid Build Coastguard Worker       case GenericRTDyldError: return "Generic RuntimeDyld error";
47*9880d681SAndroid Build Coastguard Worker     }
48*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("Unrecognized RuntimeDyldErrorCode");
49*9880d681SAndroid Build Coastguard Worker   }
50*9880d681SAndroid Build Coastguard Worker };
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker static ManagedStatic<RuntimeDyldErrorCategory> RTDyldErrorCategory;
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker }
55*9880d681SAndroid Build Coastguard Worker 
56*9880d681SAndroid Build Coastguard Worker char RuntimeDyldError::ID = 0;
57*9880d681SAndroid Build Coastguard Worker 
log(raw_ostream & OS) const58*9880d681SAndroid Build Coastguard Worker void RuntimeDyldError::log(raw_ostream &OS) const {
59*9880d681SAndroid Build Coastguard Worker   OS << ErrMsg << "\n";
60*9880d681SAndroid Build Coastguard Worker }
61*9880d681SAndroid Build Coastguard Worker 
convertToErrorCode() const62*9880d681SAndroid Build Coastguard Worker std::error_code RuntimeDyldError::convertToErrorCode() const {
63*9880d681SAndroid Build Coastguard Worker   return std::error_code(GenericRTDyldError, *RTDyldErrorCategory);
64*9880d681SAndroid Build Coastguard Worker }
65*9880d681SAndroid Build Coastguard Worker 
66*9880d681SAndroid Build Coastguard Worker // Empty out-of-line virtual destructor as the key function.
~RuntimeDyldImpl()67*9880d681SAndroid Build Coastguard Worker RuntimeDyldImpl::~RuntimeDyldImpl() {}
68*9880d681SAndroid Build Coastguard Worker 
69*9880d681SAndroid Build Coastguard Worker // Pin LoadedObjectInfo's vtables to this file.
anchor()70*9880d681SAndroid Build Coastguard Worker void RuntimeDyld::LoadedObjectInfo::anchor() {}
71*9880d681SAndroid Build Coastguard Worker 
72*9880d681SAndroid Build Coastguard Worker namespace llvm {
73*9880d681SAndroid Build Coastguard Worker 
registerEHFrames()74*9880d681SAndroid Build Coastguard Worker void RuntimeDyldImpl::registerEHFrames() {}
75*9880d681SAndroid Build Coastguard Worker 
deregisterEHFrames()76*9880d681SAndroid Build Coastguard Worker void RuntimeDyldImpl::deregisterEHFrames() {}
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
dumpSectionMemory(const SectionEntry & S,StringRef State)79*9880d681SAndroid Build Coastguard Worker static void dumpSectionMemory(const SectionEntry &S, StringRef State) {
80*9880d681SAndroid Build Coastguard Worker   dbgs() << "----- Contents of section " << S.getName() << " " << State
81*9880d681SAndroid Build Coastguard Worker          << " -----";
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker   if (S.getAddress() == nullptr) {
84*9880d681SAndroid Build Coastguard Worker     dbgs() << "\n          <section not emitted>\n";
85*9880d681SAndroid Build Coastguard Worker     return;
86*9880d681SAndroid Build Coastguard Worker   }
87*9880d681SAndroid Build Coastguard Worker 
88*9880d681SAndroid Build Coastguard Worker   const unsigned ColsPerRow = 16;
89*9880d681SAndroid Build Coastguard Worker 
90*9880d681SAndroid Build Coastguard Worker   uint8_t *DataAddr = S.getAddress();
91*9880d681SAndroid Build Coastguard Worker   uint64_t LoadAddr = S.getLoadAddress();
92*9880d681SAndroid Build Coastguard Worker 
93*9880d681SAndroid Build Coastguard Worker   unsigned StartPadding = LoadAddr & (ColsPerRow - 1);
94*9880d681SAndroid Build Coastguard Worker   unsigned BytesRemaining = S.getSize();
95*9880d681SAndroid Build Coastguard Worker 
96*9880d681SAndroid Build Coastguard Worker   if (StartPadding) {
97*9880d681SAndroid Build Coastguard Worker     dbgs() << "\n" << format("0x%016" PRIx64,
98*9880d681SAndroid Build Coastguard Worker                              LoadAddr & ~(uint64_t)(ColsPerRow - 1)) << ":";
99*9880d681SAndroid Build Coastguard Worker     while (StartPadding--)
100*9880d681SAndroid Build Coastguard Worker       dbgs() << "   ";
101*9880d681SAndroid Build Coastguard Worker   }
102*9880d681SAndroid Build Coastguard Worker 
103*9880d681SAndroid Build Coastguard Worker   while (BytesRemaining > 0) {
104*9880d681SAndroid Build Coastguard Worker     if ((LoadAddr & (ColsPerRow - 1)) == 0)
105*9880d681SAndroid Build Coastguard Worker       dbgs() << "\n" << format("0x%016" PRIx64, LoadAddr) << ":";
106*9880d681SAndroid Build Coastguard Worker 
107*9880d681SAndroid Build Coastguard Worker     dbgs() << " " << format("%02x", *DataAddr);
108*9880d681SAndroid Build Coastguard Worker 
109*9880d681SAndroid Build Coastguard Worker     ++DataAddr;
110*9880d681SAndroid Build Coastguard Worker     ++LoadAddr;
111*9880d681SAndroid Build Coastguard Worker     --BytesRemaining;
112*9880d681SAndroid Build Coastguard Worker   }
113*9880d681SAndroid Build Coastguard Worker 
114*9880d681SAndroid Build Coastguard Worker   dbgs() << "\n";
115*9880d681SAndroid Build Coastguard Worker }
116*9880d681SAndroid Build Coastguard Worker #endif
117*9880d681SAndroid Build Coastguard Worker 
118*9880d681SAndroid Build Coastguard Worker // Resolve the relocations for all symbols we currently know about.
resolveRelocations()119*9880d681SAndroid Build Coastguard Worker void RuntimeDyldImpl::resolveRelocations() {
120*9880d681SAndroid Build Coastguard Worker   MutexGuard locked(lock);
121*9880d681SAndroid Build Coastguard Worker 
122*9880d681SAndroid Build Coastguard Worker   // Print out the sections prior to relocation.
123*9880d681SAndroid Build Coastguard Worker   DEBUG(
124*9880d681SAndroid Build Coastguard Worker     for (int i = 0, e = Sections.size(); i != e; ++i)
125*9880d681SAndroid Build Coastguard Worker       dumpSectionMemory(Sections[i], "before relocations");
126*9880d681SAndroid Build Coastguard Worker   );
127*9880d681SAndroid Build Coastguard Worker 
128*9880d681SAndroid Build Coastguard Worker   // First, resolve relocations associated with external symbols.
129*9880d681SAndroid Build Coastguard Worker   resolveExternalSymbols();
130*9880d681SAndroid Build Coastguard Worker 
131*9880d681SAndroid Build Coastguard Worker   // Iterate over all outstanding relocations
132*9880d681SAndroid Build Coastguard Worker   for (auto it = Relocations.begin(), e = Relocations.end(); it != e; ++it) {
133*9880d681SAndroid Build Coastguard Worker     // The Section here (Sections[i]) refers to the section in which the
134*9880d681SAndroid Build Coastguard Worker     // symbol for the relocation is located.  The SectionID in the relocation
135*9880d681SAndroid Build Coastguard Worker     // entry provides the section to which the relocation will be applied.
136*9880d681SAndroid Build Coastguard Worker     int Idx = it->first;
137*9880d681SAndroid Build Coastguard Worker     uint64_t Addr = Sections[Idx].getLoadAddress();
138*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t"
139*9880d681SAndroid Build Coastguard Worker                  << format("%p", (uintptr_t)Addr) << "\n");
140*9880d681SAndroid Build Coastguard Worker     resolveRelocationList(it->second, Addr);
141*9880d681SAndroid Build Coastguard Worker   }
142*9880d681SAndroid Build Coastguard Worker   Relocations.clear();
143*9880d681SAndroid Build Coastguard Worker 
144*9880d681SAndroid Build Coastguard Worker   // Print out sections after relocation.
145*9880d681SAndroid Build Coastguard Worker   DEBUG(
146*9880d681SAndroid Build Coastguard Worker     for (int i = 0, e = Sections.size(); i != e; ++i)
147*9880d681SAndroid Build Coastguard Worker       dumpSectionMemory(Sections[i], "after relocations");
148*9880d681SAndroid Build Coastguard Worker   );
149*9880d681SAndroid Build Coastguard Worker 
150*9880d681SAndroid Build Coastguard Worker }
151*9880d681SAndroid Build Coastguard Worker 
mapSectionAddress(const void * LocalAddress,uint64_t TargetAddress)152*9880d681SAndroid Build Coastguard Worker void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
153*9880d681SAndroid Build Coastguard Worker                                         uint64_t TargetAddress) {
154*9880d681SAndroid Build Coastguard Worker   MutexGuard locked(lock);
155*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
156*9880d681SAndroid Build Coastguard Worker     if (Sections[i].getAddress() == LocalAddress) {
157*9880d681SAndroid Build Coastguard Worker       reassignSectionAddress(i, TargetAddress);
158*9880d681SAndroid Build Coastguard Worker       return;
159*9880d681SAndroid Build Coastguard Worker     }
160*9880d681SAndroid Build Coastguard Worker   }
161*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("Attempting to remap address of unknown section!");
162*9880d681SAndroid Build Coastguard Worker }
163*9880d681SAndroid Build Coastguard Worker 
getOffset(const SymbolRef & Sym,SectionRef Sec,uint64_t & Result)164*9880d681SAndroid Build Coastguard Worker static Error getOffset(const SymbolRef &Sym, SectionRef Sec,
165*9880d681SAndroid Build Coastguard Worker                        uint64_t &Result) {
166*9880d681SAndroid Build Coastguard Worker   Expected<uint64_t> AddressOrErr = Sym.getAddress();
167*9880d681SAndroid Build Coastguard Worker   if (!AddressOrErr)
168*9880d681SAndroid Build Coastguard Worker     return AddressOrErr.takeError();
169*9880d681SAndroid Build Coastguard Worker   Result = *AddressOrErr - Sec.getAddress();
170*9880d681SAndroid Build Coastguard Worker   return Error::success();
171*9880d681SAndroid Build Coastguard Worker }
172*9880d681SAndroid Build Coastguard Worker 
173*9880d681SAndroid Build Coastguard Worker Expected<RuntimeDyldImpl::ObjSectionToIDMap>
loadObjectImpl(const object::ObjectFile & Obj)174*9880d681SAndroid Build Coastguard Worker RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) {
175*9880d681SAndroid Build Coastguard Worker   MutexGuard locked(lock);
176*9880d681SAndroid Build Coastguard Worker 
177*9880d681SAndroid Build Coastguard Worker   // Save information about our target
178*9880d681SAndroid Build Coastguard Worker   Arch = (Triple::ArchType)Obj.getArch();
179*9880d681SAndroid Build Coastguard Worker   IsTargetLittleEndian = Obj.isLittleEndian();
180*9880d681SAndroid Build Coastguard Worker   setMipsABI(Obj);
181*9880d681SAndroid Build Coastguard Worker 
182*9880d681SAndroid Build Coastguard Worker   // Compute the memory size required to load all sections to be loaded
183*9880d681SAndroid Build Coastguard Worker   // and pass this information to the memory manager
184*9880d681SAndroid Build Coastguard Worker   if (MemMgr.needsToReserveAllocationSpace()) {
185*9880d681SAndroid Build Coastguard Worker     uint64_t CodeSize = 0, RODataSize = 0, RWDataSize = 0;
186*9880d681SAndroid Build Coastguard Worker     uint32_t CodeAlign = 1, RODataAlign = 1, RWDataAlign = 1;
187*9880d681SAndroid Build Coastguard Worker     if (auto Err = computeTotalAllocSize(Obj,
188*9880d681SAndroid Build Coastguard Worker                                          CodeSize, CodeAlign,
189*9880d681SAndroid Build Coastguard Worker                                          RODataSize, RODataAlign,
190*9880d681SAndroid Build Coastguard Worker                                          RWDataSize, RWDataAlign))
191*9880d681SAndroid Build Coastguard Worker       return std::move(Err);
192*9880d681SAndroid Build Coastguard Worker     MemMgr.reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
193*9880d681SAndroid Build Coastguard Worker                                   RWDataSize, RWDataAlign);
194*9880d681SAndroid Build Coastguard Worker   }
195*9880d681SAndroid Build Coastguard Worker 
196*9880d681SAndroid Build Coastguard Worker   // Used sections from the object file
197*9880d681SAndroid Build Coastguard Worker   ObjSectionToIDMap LocalSections;
198*9880d681SAndroid Build Coastguard Worker 
199*9880d681SAndroid Build Coastguard Worker   // Common symbols requiring allocation, with their sizes and alignments
200*9880d681SAndroid Build Coastguard Worker   CommonSymbolList CommonSymbols;
201*9880d681SAndroid Build Coastguard Worker 
202*9880d681SAndroid Build Coastguard Worker   // Parse symbols
203*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Parse symbols:\n");
204*9880d681SAndroid Build Coastguard Worker   for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
205*9880d681SAndroid Build Coastguard Worker        ++I) {
206*9880d681SAndroid Build Coastguard Worker     uint32_t Flags = I->getFlags();
207*9880d681SAndroid Build Coastguard Worker 
208*9880d681SAndroid Build Coastguard Worker     if (Flags & SymbolRef::SF_Common)
209*9880d681SAndroid Build Coastguard Worker       CommonSymbols.push_back(*I);
210*9880d681SAndroid Build Coastguard Worker     else {
211*9880d681SAndroid Build Coastguard Worker 
212*9880d681SAndroid Build Coastguard Worker       // Get the symbol type.
213*9880d681SAndroid Build Coastguard Worker       object::SymbolRef::Type SymType;
214*9880d681SAndroid Build Coastguard Worker       if (auto SymTypeOrErr = I->getType())
215*9880d681SAndroid Build Coastguard Worker         SymType =  *SymTypeOrErr;
216*9880d681SAndroid Build Coastguard Worker       else
217*9880d681SAndroid Build Coastguard Worker         return SymTypeOrErr.takeError();
218*9880d681SAndroid Build Coastguard Worker 
219*9880d681SAndroid Build Coastguard Worker       // Get symbol name.
220*9880d681SAndroid Build Coastguard Worker       StringRef Name;
221*9880d681SAndroid Build Coastguard Worker       if (auto NameOrErr = I->getName())
222*9880d681SAndroid Build Coastguard Worker         Name = *NameOrErr;
223*9880d681SAndroid Build Coastguard Worker       else
224*9880d681SAndroid Build Coastguard Worker         return NameOrErr.takeError();
225*9880d681SAndroid Build Coastguard Worker 
226*9880d681SAndroid Build Coastguard Worker       // Compute JIT symbol flags.
227*9880d681SAndroid Build Coastguard Worker       JITSymbolFlags RTDyldSymFlags = JITSymbolFlags::None;
228*9880d681SAndroid Build Coastguard Worker       if (Flags & SymbolRef::SF_Weak)
229*9880d681SAndroid Build Coastguard Worker         RTDyldSymFlags |= JITSymbolFlags::Weak;
230*9880d681SAndroid Build Coastguard Worker       if (Flags & SymbolRef::SF_Exported)
231*9880d681SAndroid Build Coastguard Worker         RTDyldSymFlags |= JITSymbolFlags::Exported;
232*9880d681SAndroid Build Coastguard Worker 
233*9880d681SAndroid Build Coastguard Worker       if (Flags & SymbolRef::SF_Absolute &&
234*9880d681SAndroid Build Coastguard Worker           SymType != object::SymbolRef::ST_File) {
235*9880d681SAndroid Build Coastguard Worker         uint64_t Addr = 0;
236*9880d681SAndroid Build Coastguard Worker         if (auto AddrOrErr = I->getAddress())
237*9880d681SAndroid Build Coastguard Worker           Addr = *AddrOrErr;
238*9880d681SAndroid Build Coastguard Worker         else
239*9880d681SAndroid Build Coastguard Worker           return AddrOrErr.takeError();
240*9880d681SAndroid Build Coastguard Worker 
241*9880d681SAndroid Build Coastguard Worker         unsigned SectionID = AbsoluteSymbolSection;
242*9880d681SAndroid Build Coastguard Worker 
243*9880d681SAndroid Build Coastguard Worker         DEBUG(dbgs() << "\tType: " << SymType << " (absolute) Name: " << Name
244*9880d681SAndroid Build Coastguard Worker                      << " SID: " << SectionID << " Offset: "
245*9880d681SAndroid Build Coastguard Worker                      << format("%p", (uintptr_t)Addr)
246*9880d681SAndroid Build Coastguard Worker                      << " flags: " << Flags << "\n");
247*9880d681SAndroid Build Coastguard Worker         GlobalSymbolTable[Name] =
248*9880d681SAndroid Build Coastguard Worker           SymbolTableEntry(SectionID, Addr, RTDyldSymFlags);
249*9880d681SAndroid Build Coastguard Worker       } else if (SymType == object::SymbolRef::ST_Function ||
250*9880d681SAndroid Build Coastguard Worker                  SymType == object::SymbolRef::ST_Data ||
251*9880d681SAndroid Build Coastguard Worker                  SymType == object::SymbolRef::ST_Unknown ||
252*9880d681SAndroid Build Coastguard Worker                  SymType == object::SymbolRef::ST_Other) {
253*9880d681SAndroid Build Coastguard Worker 
254*9880d681SAndroid Build Coastguard Worker         section_iterator SI = Obj.section_end();
255*9880d681SAndroid Build Coastguard Worker         if (auto SIOrErr = I->getSection())
256*9880d681SAndroid Build Coastguard Worker           SI = *SIOrErr;
257*9880d681SAndroid Build Coastguard Worker         else
258*9880d681SAndroid Build Coastguard Worker           return SIOrErr.takeError();
259*9880d681SAndroid Build Coastguard Worker 
260*9880d681SAndroid Build Coastguard Worker         if (SI == Obj.section_end())
261*9880d681SAndroid Build Coastguard Worker           continue;
262*9880d681SAndroid Build Coastguard Worker 
263*9880d681SAndroid Build Coastguard Worker         // Get symbol offset.
264*9880d681SAndroid Build Coastguard Worker         uint64_t SectOffset;
265*9880d681SAndroid Build Coastguard Worker         if (auto Err = getOffset(*I, *SI, SectOffset))
266*9880d681SAndroid Build Coastguard Worker           return std::move(Err);
267*9880d681SAndroid Build Coastguard Worker 
268*9880d681SAndroid Build Coastguard Worker         bool IsCode = SI->isText();
269*9880d681SAndroid Build Coastguard Worker         unsigned SectionID;
270*9880d681SAndroid Build Coastguard Worker         if (auto SectionIDOrErr = findOrEmitSection(Obj, *SI, IsCode,
271*9880d681SAndroid Build Coastguard Worker                                                     LocalSections))
272*9880d681SAndroid Build Coastguard Worker           SectionID = *SectionIDOrErr;
273*9880d681SAndroid Build Coastguard Worker         else
274*9880d681SAndroid Build Coastguard Worker           return SectionIDOrErr.takeError();
275*9880d681SAndroid Build Coastguard Worker 
276*9880d681SAndroid Build Coastguard Worker         DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name
277*9880d681SAndroid Build Coastguard Worker                      << " SID: " << SectionID << " Offset: "
278*9880d681SAndroid Build Coastguard Worker                      << format("%p", (uintptr_t)SectOffset)
279*9880d681SAndroid Build Coastguard Worker                      << " flags: " << Flags << "\n");
280*9880d681SAndroid Build Coastguard Worker         GlobalSymbolTable[Name] =
281*9880d681SAndroid Build Coastguard Worker           SymbolTableEntry(SectionID, SectOffset, RTDyldSymFlags);
282*9880d681SAndroid Build Coastguard Worker       }
283*9880d681SAndroid Build Coastguard Worker     }
284*9880d681SAndroid Build Coastguard Worker   }
285*9880d681SAndroid Build Coastguard Worker 
286*9880d681SAndroid Build Coastguard Worker   // Allocate common symbols
287*9880d681SAndroid Build Coastguard Worker   if (auto Err = emitCommonSymbols(Obj, CommonSymbols))
288*9880d681SAndroid Build Coastguard Worker     return std::move(Err);
289*9880d681SAndroid Build Coastguard Worker 
290*9880d681SAndroid Build Coastguard Worker   // Parse and process relocations
291*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Parse relocations:\n");
292*9880d681SAndroid Build Coastguard Worker   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
293*9880d681SAndroid Build Coastguard Worker        SI != SE; ++SI) {
294*9880d681SAndroid Build Coastguard Worker     StubMap Stubs;
295*9880d681SAndroid Build Coastguard Worker     section_iterator RelocatedSection = SI->getRelocatedSection();
296*9880d681SAndroid Build Coastguard Worker 
297*9880d681SAndroid Build Coastguard Worker     if (RelocatedSection == SE)
298*9880d681SAndroid Build Coastguard Worker       continue;
299*9880d681SAndroid Build Coastguard Worker 
300*9880d681SAndroid Build Coastguard Worker     relocation_iterator I = SI->relocation_begin();
301*9880d681SAndroid Build Coastguard Worker     relocation_iterator E = SI->relocation_end();
302*9880d681SAndroid Build Coastguard Worker 
303*9880d681SAndroid Build Coastguard Worker     if (I == E && !ProcessAllSections)
304*9880d681SAndroid Build Coastguard Worker       continue;
305*9880d681SAndroid Build Coastguard Worker 
306*9880d681SAndroid Build Coastguard Worker     bool IsCode = RelocatedSection->isText();
307*9880d681SAndroid Build Coastguard Worker     unsigned SectionID = 0;
308*9880d681SAndroid Build Coastguard Worker     if (auto SectionIDOrErr = findOrEmitSection(Obj, *RelocatedSection, IsCode,
309*9880d681SAndroid Build Coastguard Worker                                                 LocalSections))
310*9880d681SAndroid Build Coastguard Worker       SectionID = *SectionIDOrErr;
311*9880d681SAndroid Build Coastguard Worker     else
312*9880d681SAndroid Build Coastguard Worker       return SectionIDOrErr.takeError();
313*9880d681SAndroid Build Coastguard Worker 
314*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
315*9880d681SAndroid Build Coastguard Worker 
316*9880d681SAndroid Build Coastguard Worker     for (; I != E;)
317*9880d681SAndroid Build Coastguard Worker       if (auto IOrErr = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs))
318*9880d681SAndroid Build Coastguard Worker         I = *IOrErr;
319*9880d681SAndroid Build Coastguard Worker       else
320*9880d681SAndroid Build Coastguard Worker         return IOrErr.takeError();
321*9880d681SAndroid Build Coastguard Worker 
322*9880d681SAndroid Build Coastguard Worker     // If there is an attached checker, notify it about the stubs for this
323*9880d681SAndroid Build Coastguard Worker     // section so that they can be verified.
324*9880d681SAndroid Build Coastguard Worker     if (Checker)
325*9880d681SAndroid Build Coastguard Worker       Checker->registerStubMap(Obj.getFileName(), SectionID, Stubs);
326*9880d681SAndroid Build Coastguard Worker   }
327*9880d681SAndroid Build Coastguard Worker 
328*9880d681SAndroid Build Coastguard Worker   // Give the subclasses a chance to tie-up any loose ends.
329*9880d681SAndroid Build Coastguard Worker   if (auto Err = finalizeLoad(Obj, LocalSections))
330*9880d681SAndroid Build Coastguard Worker     return std::move(Err);
331*9880d681SAndroid Build Coastguard Worker 
332*9880d681SAndroid Build Coastguard Worker //   for (auto E : LocalSections)
333*9880d681SAndroid Build Coastguard Worker //     llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n";
334*9880d681SAndroid Build Coastguard Worker 
335*9880d681SAndroid Build Coastguard Worker   return LocalSections;
336*9880d681SAndroid Build Coastguard Worker }
337*9880d681SAndroid Build Coastguard Worker 
338*9880d681SAndroid Build Coastguard Worker // A helper method for computeTotalAllocSize.
339*9880d681SAndroid Build Coastguard Worker // Computes the memory size required to allocate sections with the given sizes,
340*9880d681SAndroid Build Coastguard Worker // assuming that all sections are allocated with the given alignment
341*9880d681SAndroid Build Coastguard Worker static uint64_t
computeAllocationSizeForSections(std::vector<uint64_t> & SectionSizes,uint64_t Alignment)342*9880d681SAndroid Build Coastguard Worker computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
343*9880d681SAndroid Build Coastguard Worker                                  uint64_t Alignment) {
344*9880d681SAndroid Build Coastguard Worker   uint64_t TotalSize = 0;
345*9880d681SAndroid Build Coastguard Worker   for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
346*9880d681SAndroid Build Coastguard Worker     uint64_t AlignedSize =
347*9880d681SAndroid Build Coastguard Worker         (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
348*9880d681SAndroid Build Coastguard Worker     TotalSize += AlignedSize;
349*9880d681SAndroid Build Coastguard Worker   }
350*9880d681SAndroid Build Coastguard Worker   return TotalSize;
351*9880d681SAndroid Build Coastguard Worker }
352*9880d681SAndroid Build Coastguard Worker 
isRequiredForExecution(const SectionRef Section)353*9880d681SAndroid Build Coastguard Worker static bool isRequiredForExecution(const SectionRef Section) {
354*9880d681SAndroid Build Coastguard Worker   const ObjectFile *Obj = Section.getObject();
355*9880d681SAndroid Build Coastguard Worker   if (isa<object::ELFObjectFileBase>(Obj))
356*9880d681SAndroid Build Coastguard Worker     return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC;
357*9880d681SAndroid Build Coastguard Worker   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) {
358*9880d681SAndroid Build Coastguard Worker     const coff_section *CoffSection = COFFObj->getCOFFSection(Section);
359*9880d681SAndroid Build Coastguard Worker     // Avoid loading zero-sized COFF sections.
360*9880d681SAndroid Build Coastguard Worker     // In PE files, VirtualSize gives the section size, and SizeOfRawData
361*9880d681SAndroid Build Coastguard Worker     // may be zero for sections with content. In Obj files, SizeOfRawData
362*9880d681SAndroid Build Coastguard Worker     // gives the section size, and VirtualSize is always zero. Hence
363*9880d681SAndroid Build Coastguard Worker     // the need to check for both cases below.
364*9880d681SAndroid Build Coastguard Worker     bool HasContent =
365*9880d681SAndroid Build Coastguard Worker         (CoffSection->VirtualSize > 0) || (CoffSection->SizeOfRawData > 0);
366*9880d681SAndroid Build Coastguard Worker     bool IsDiscardable =
367*9880d681SAndroid Build Coastguard Worker         CoffSection->Characteristics &
368*9880d681SAndroid Build Coastguard Worker         (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_LNK_INFO);
369*9880d681SAndroid Build Coastguard Worker     return HasContent && !IsDiscardable;
370*9880d681SAndroid Build Coastguard Worker   }
371*9880d681SAndroid Build Coastguard Worker 
372*9880d681SAndroid Build Coastguard Worker   assert(isa<MachOObjectFile>(Obj));
373*9880d681SAndroid Build Coastguard Worker   return true;
374*9880d681SAndroid Build Coastguard Worker }
375*9880d681SAndroid Build Coastguard Worker 
isReadOnlyData(const SectionRef Section)376*9880d681SAndroid Build Coastguard Worker static bool isReadOnlyData(const SectionRef Section) {
377*9880d681SAndroid Build Coastguard Worker   const ObjectFile *Obj = Section.getObject();
378*9880d681SAndroid Build Coastguard Worker   if (isa<object::ELFObjectFileBase>(Obj))
379*9880d681SAndroid Build Coastguard Worker     return !(ELFSectionRef(Section).getFlags() &
380*9880d681SAndroid Build Coastguard Worker              (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
381*9880d681SAndroid Build Coastguard Worker   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
382*9880d681SAndroid Build Coastguard Worker     return ((COFFObj->getCOFFSection(Section)->Characteristics &
383*9880d681SAndroid Build Coastguard Worker              (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
384*9880d681SAndroid Build Coastguard Worker              | COFF::IMAGE_SCN_MEM_READ
385*9880d681SAndroid Build Coastguard Worker              | COFF::IMAGE_SCN_MEM_WRITE))
386*9880d681SAndroid Build Coastguard Worker              ==
387*9880d681SAndroid Build Coastguard Worker              (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
388*9880d681SAndroid Build Coastguard Worker              | COFF::IMAGE_SCN_MEM_READ));
389*9880d681SAndroid Build Coastguard Worker 
390*9880d681SAndroid Build Coastguard Worker   assert(isa<MachOObjectFile>(Obj));
391*9880d681SAndroid Build Coastguard Worker   return false;
392*9880d681SAndroid Build Coastguard Worker }
393*9880d681SAndroid Build Coastguard Worker 
isZeroInit(const SectionRef Section)394*9880d681SAndroid Build Coastguard Worker static bool isZeroInit(const SectionRef Section) {
395*9880d681SAndroid Build Coastguard Worker   const ObjectFile *Obj = Section.getObject();
396*9880d681SAndroid Build Coastguard Worker   if (isa<object::ELFObjectFileBase>(Obj))
397*9880d681SAndroid Build Coastguard Worker     return ELFSectionRef(Section).getType() == ELF::SHT_NOBITS;
398*9880d681SAndroid Build Coastguard Worker   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
399*9880d681SAndroid Build Coastguard Worker     return COFFObj->getCOFFSection(Section)->Characteristics &
400*9880d681SAndroid Build Coastguard Worker             COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
401*9880d681SAndroid Build Coastguard Worker 
402*9880d681SAndroid Build Coastguard Worker   auto *MachO = cast<MachOObjectFile>(Obj);
403*9880d681SAndroid Build Coastguard Worker   unsigned SectionType = MachO->getSectionType(Section);
404*9880d681SAndroid Build Coastguard Worker   return SectionType == MachO::S_ZEROFILL ||
405*9880d681SAndroid Build Coastguard Worker          SectionType == MachO::S_GB_ZEROFILL;
406*9880d681SAndroid Build Coastguard Worker }
407*9880d681SAndroid Build Coastguard Worker 
408*9880d681SAndroid Build Coastguard Worker // Compute an upper bound of the memory size that is required to load all
409*9880d681SAndroid Build Coastguard Worker // sections
computeTotalAllocSize(const ObjectFile & Obj,uint64_t & CodeSize,uint32_t & CodeAlign,uint64_t & RODataSize,uint32_t & RODataAlign,uint64_t & RWDataSize,uint32_t & RWDataAlign)410*9880d681SAndroid Build Coastguard Worker Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj,
411*9880d681SAndroid Build Coastguard Worker                                              uint64_t &CodeSize,
412*9880d681SAndroid Build Coastguard Worker                                              uint32_t &CodeAlign,
413*9880d681SAndroid Build Coastguard Worker                                              uint64_t &RODataSize,
414*9880d681SAndroid Build Coastguard Worker                                              uint32_t &RODataAlign,
415*9880d681SAndroid Build Coastguard Worker                                              uint64_t &RWDataSize,
416*9880d681SAndroid Build Coastguard Worker                                              uint32_t &RWDataAlign) {
417*9880d681SAndroid Build Coastguard Worker   // Compute the size of all sections required for execution
418*9880d681SAndroid Build Coastguard Worker   std::vector<uint64_t> CodeSectionSizes;
419*9880d681SAndroid Build Coastguard Worker   std::vector<uint64_t> ROSectionSizes;
420*9880d681SAndroid Build Coastguard Worker   std::vector<uint64_t> RWSectionSizes;
421*9880d681SAndroid Build Coastguard Worker 
422*9880d681SAndroid Build Coastguard Worker   // Collect sizes of all sections to be loaded;
423*9880d681SAndroid Build Coastguard Worker   // also determine the max alignment of all sections
424*9880d681SAndroid Build Coastguard Worker   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
425*9880d681SAndroid Build Coastguard Worker        SI != SE; ++SI) {
426*9880d681SAndroid Build Coastguard Worker     const SectionRef &Section = *SI;
427*9880d681SAndroid Build Coastguard Worker 
428*9880d681SAndroid Build Coastguard Worker     bool IsRequired = isRequiredForExecution(Section);
429*9880d681SAndroid Build Coastguard Worker 
430*9880d681SAndroid Build Coastguard Worker     // Consider only the sections that are required to be loaded for execution
431*9880d681SAndroid Build Coastguard Worker     if (IsRequired) {
432*9880d681SAndroid Build Coastguard Worker       uint64_t DataSize = Section.getSize();
433*9880d681SAndroid Build Coastguard Worker       uint64_t Alignment64 = Section.getAlignment();
434*9880d681SAndroid Build Coastguard Worker       unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
435*9880d681SAndroid Build Coastguard Worker       bool IsCode = Section.isText();
436*9880d681SAndroid Build Coastguard Worker       bool IsReadOnly = isReadOnlyData(Section);
437*9880d681SAndroid Build Coastguard Worker 
438*9880d681SAndroid Build Coastguard Worker       StringRef Name;
439*9880d681SAndroid Build Coastguard Worker       if (auto EC = Section.getName(Name))
440*9880d681SAndroid Build Coastguard Worker         return errorCodeToError(EC);
441*9880d681SAndroid Build Coastguard Worker 
442*9880d681SAndroid Build Coastguard Worker       uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
443*9880d681SAndroid Build Coastguard Worker       uint64_t SectionSize = DataSize + StubBufSize;
444*9880d681SAndroid Build Coastguard Worker 
445*9880d681SAndroid Build Coastguard Worker       // The .eh_frame section (at least on Linux) needs an extra four bytes
446*9880d681SAndroid Build Coastguard Worker       // padded
447*9880d681SAndroid Build Coastguard Worker       // with zeroes added at the end.  For MachO objects, this section has a
448*9880d681SAndroid Build Coastguard Worker       // slightly different name, so this won't have any effect for MachO
449*9880d681SAndroid Build Coastguard Worker       // objects.
450*9880d681SAndroid Build Coastguard Worker       if (Name == ".eh_frame")
451*9880d681SAndroid Build Coastguard Worker         SectionSize += 4;
452*9880d681SAndroid Build Coastguard Worker 
453*9880d681SAndroid Build Coastguard Worker       if (!SectionSize)
454*9880d681SAndroid Build Coastguard Worker         SectionSize = 1;
455*9880d681SAndroid Build Coastguard Worker 
456*9880d681SAndroid Build Coastguard Worker       if (IsCode) {
457*9880d681SAndroid Build Coastguard Worker         CodeAlign = std::max(CodeAlign, Alignment);
458*9880d681SAndroid Build Coastguard Worker         CodeSectionSizes.push_back(SectionSize);
459*9880d681SAndroid Build Coastguard Worker       } else if (IsReadOnly) {
460*9880d681SAndroid Build Coastguard Worker         RODataAlign = std::max(RODataAlign, Alignment);
461*9880d681SAndroid Build Coastguard Worker         ROSectionSizes.push_back(SectionSize);
462*9880d681SAndroid Build Coastguard Worker       } else {
463*9880d681SAndroid Build Coastguard Worker         RWDataAlign = std::max(RWDataAlign, Alignment);
464*9880d681SAndroid Build Coastguard Worker         RWSectionSizes.push_back(SectionSize);
465*9880d681SAndroid Build Coastguard Worker       }
466*9880d681SAndroid Build Coastguard Worker     }
467*9880d681SAndroid Build Coastguard Worker   }
468*9880d681SAndroid Build Coastguard Worker 
469*9880d681SAndroid Build Coastguard Worker   // Compute the size of all common symbols
470*9880d681SAndroid Build Coastguard Worker   uint64_t CommonSize = 0;
471*9880d681SAndroid Build Coastguard Worker   uint32_t CommonAlign = 1;
472*9880d681SAndroid Build Coastguard Worker   for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
473*9880d681SAndroid Build Coastguard Worker        ++I) {
474*9880d681SAndroid Build Coastguard Worker     uint32_t Flags = I->getFlags();
475*9880d681SAndroid Build Coastguard Worker     if (Flags & SymbolRef::SF_Common) {
476*9880d681SAndroid Build Coastguard Worker       // Add the common symbols to a list.  We'll allocate them all below.
477*9880d681SAndroid Build Coastguard Worker       uint64_t Size = I->getCommonSize();
478*9880d681SAndroid Build Coastguard Worker       uint32_t Align = I->getAlignment();
479*9880d681SAndroid Build Coastguard Worker       // If this is the first common symbol, use its alignment as the alignment
480*9880d681SAndroid Build Coastguard Worker       // for the common symbols section.
481*9880d681SAndroid Build Coastguard Worker       if (CommonSize == 0)
482*9880d681SAndroid Build Coastguard Worker         CommonAlign = Align;
483*9880d681SAndroid Build Coastguard Worker       CommonSize = alignTo(CommonSize, Align) + Size;
484*9880d681SAndroid Build Coastguard Worker     }
485*9880d681SAndroid Build Coastguard Worker   }
486*9880d681SAndroid Build Coastguard Worker   if (CommonSize != 0) {
487*9880d681SAndroid Build Coastguard Worker     RWSectionSizes.push_back(CommonSize);
488*9880d681SAndroid Build Coastguard Worker     RWDataAlign = std::max(RWDataAlign, CommonAlign);
489*9880d681SAndroid Build Coastguard Worker   }
490*9880d681SAndroid Build Coastguard Worker 
491*9880d681SAndroid Build Coastguard Worker   // Compute the required allocation space for each different type of sections
492*9880d681SAndroid Build Coastguard Worker   // (code, read-only data, read-write data) assuming that all sections are
493*9880d681SAndroid Build Coastguard Worker   // allocated with the max alignment. Note that we cannot compute with the
494*9880d681SAndroid Build Coastguard Worker   // individual alignments of the sections, because then the required size
495*9880d681SAndroid Build Coastguard Worker   // depends on the order, in which the sections are allocated.
496*9880d681SAndroid Build Coastguard Worker   CodeSize = computeAllocationSizeForSections(CodeSectionSizes, CodeAlign);
497*9880d681SAndroid Build Coastguard Worker   RODataSize = computeAllocationSizeForSections(ROSectionSizes, RODataAlign);
498*9880d681SAndroid Build Coastguard Worker   RWDataSize = computeAllocationSizeForSections(RWSectionSizes, RWDataAlign);
499*9880d681SAndroid Build Coastguard Worker 
500*9880d681SAndroid Build Coastguard Worker   return Error::success();
501*9880d681SAndroid Build Coastguard Worker }
502*9880d681SAndroid Build Coastguard Worker 
503*9880d681SAndroid Build Coastguard Worker // compute stub buffer size for the given section
computeSectionStubBufSize(const ObjectFile & Obj,const SectionRef & Section)504*9880d681SAndroid Build Coastguard Worker unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj,
505*9880d681SAndroid Build Coastguard Worker                                                     const SectionRef &Section) {
506*9880d681SAndroid Build Coastguard Worker   unsigned StubSize = getMaxStubSize();
507*9880d681SAndroid Build Coastguard Worker   if (StubSize == 0) {
508*9880d681SAndroid Build Coastguard Worker     return 0;
509*9880d681SAndroid Build Coastguard Worker   }
510*9880d681SAndroid Build Coastguard Worker   // FIXME: this is an inefficient way to handle this. We should computed the
511*9880d681SAndroid Build Coastguard Worker   // necessary section allocation size in loadObject by walking all the sections
512*9880d681SAndroid Build Coastguard Worker   // once.
513*9880d681SAndroid Build Coastguard Worker   unsigned StubBufSize = 0;
514*9880d681SAndroid Build Coastguard Worker   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
515*9880d681SAndroid Build Coastguard Worker        SI != SE; ++SI) {
516*9880d681SAndroid Build Coastguard Worker     section_iterator RelSecI = SI->getRelocatedSection();
517*9880d681SAndroid Build Coastguard Worker     if (!(RelSecI == Section))
518*9880d681SAndroid Build Coastguard Worker       continue;
519*9880d681SAndroid Build Coastguard Worker 
520*9880d681SAndroid Build Coastguard Worker     for (const RelocationRef &Reloc : SI->relocations())
521*9880d681SAndroid Build Coastguard Worker       if (relocationNeedsStub(Reloc))
522*9880d681SAndroid Build Coastguard Worker         StubBufSize += StubSize;
523*9880d681SAndroid Build Coastguard Worker   }
524*9880d681SAndroid Build Coastguard Worker 
525*9880d681SAndroid Build Coastguard Worker   // Get section data size and alignment
526*9880d681SAndroid Build Coastguard Worker   uint64_t DataSize = Section.getSize();
527*9880d681SAndroid Build Coastguard Worker   uint64_t Alignment64 = Section.getAlignment();
528*9880d681SAndroid Build Coastguard Worker 
529*9880d681SAndroid Build Coastguard Worker   // Add stubbuf size alignment
530*9880d681SAndroid Build Coastguard Worker   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
531*9880d681SAndroid Build Coastguard Worker   unsigned StubAlignment = getStubAlignment();
532*9880d681SAndroid Build Coastguard Worker   unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
533*9880d681SAndroid Build Coastguard Worker   if (StubAlignment > EndAlignment)
534*9880d681SAndroid Build Coastguard Worker     StubBufSize += StubAlignment - EndAlignment;
535*9880d681SAndroid Build Coastguard Worker   return StubBufSize;
536*9880d681SAndroid Build Coastguard Worker }
537*9880d681SAndroid Build Coastguard Worker 
readBytesUnaligned(uint8_t * Src,unsigned Size) const538*9880d681SAndroid Build Coastguard Worker uint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src,
539*9880d681SAndroid Build Coastguard Worker                                              unsigned Size) const {
540*9880d681SAndroid Build Coastguard Worker   uint64_t Result = 0;
541*9880d681SAndroid Build Coastguard Worker   if (IsTargetLittleEndian) {
542*9880d681SAndroid Build Coastguard Worker     Src += Size - 1;
543*9880d681SAndroid Build Coastguard Worker     while (Size--)
544*9880d681SAndroid Build Coastguard Worker       Result = (Result << 8) | *Src--;
545*9880d681SAndroid Build Coastguard Worker   } else
546*9880d681SAndroid Build Coastguard Worker     while (Size--)
547*9880d681SAndroid Build Coastguard Worker       Result = (Result << 8) | *Src++;
548*9880d681SAndroid Build Coastguard Worker 
549*9880d681SAndroid Build Coastguard Worker   return Result;
550*9880d681SAndroid Build Coastguard Worker }
551*9880d681SAndroid Build Coastguard Worker 
writeBytesUnaligned(uint64_t Value,uint8_t * Dst,unsigned Size) const552*9880d681SAndroid Build Coastguard Worker void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst,
553*9880d681SAndroid Build Coastguard Worker                                           unsigned Size) const {
554*9880d681SAndroid Build Coastguard Worker   if (IsTargetLittleEndian) {
555*9880d681SAndroid Build Coastguard Worker     while (Size--) {
556*9880d681SAndroid Build Coastguard Worker       *Dst++ = Value & 0xFF;
557*9880d681SAndroid Build Coastguard Worker       Value >>= 8;
558*9880d681SAndroid Build Coastguard Worker     }
559*9880d681SAndroid Build Coastguard Worker   } else {
560*9880d681SAndroid Build Coastguard Worker     Dst += Size - 1;
561*9880d681SAndroid Build Coastguard Worker     while (Size--) {
562*9880d681SAndroid Build Coastguard Worker       *Dst-- = Value & 0xFF;
563*9880d681SAndroid Build Coastguard Worker       Value >>= 8;
564*9880d681SAndroid Build Coastguard Worker     }
565*9880d681SAndroid Build Coastguard Worker   }
566*9880d681SAndroid Build Coastguard Worker }
567*9880d681SAndroid Build Coastguard Worker 
emitCommonSymbols(const ObjectFile & Obj,CommonSymbolList & CommonSymbols)568*9880d681SAndroid Build Coastguard Worker Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj,
569*9880d681SAndroid Build Coastguard Worker                                          CommonSymbolList &CommonSymbols) {
570*9880d681SAndroid Build Coastguard Worker   if (CommonSymbols.empty())
571*9880d681SAndroid Build Coastguard Worker     return Error::success();
572*9880d681SAndroid Build Coastguard Worker 
573*9880d681SAndroid Build Coastguard Worker   uint64_t CommonSize = 0;
574*9880d681SAndroid Build Coastguard Worker   uint32_t CommonAlign = CommonSymbols.begin()->getAlignment();
575*9880d681SAndroid Build Coastguard Worker   CommonSymbolList SymbolsToAllocate;
576*9880d681SAndroid Build Coastguard Worker 
577*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Processing common symbols...\n");
578*9880d681SAndroid Build Coastguard Worker 
579*9880d681SAndroid Build Coastguard Worker   for (const auto &Sym : CommonSymbols) {
580*9880d681SAndroid Build Coastguard Worker     StringRef Name;
581*9880d681SAndroid Build Coastguard Worker     if (auto NameOrErr = Sym.getName())
582*9880d681SAndroid Build Coastguard Worker       Name = *NameOrErr;
583*9880d681SAndroid Build Coastguard Worker     else
584*9880d681SAndroid Build Coastguard Worker       return NameOrErr.takeError();
585*9880d681SAndroid Build Coastguard Worker 
586*9880d681SAndroid Build Coastguard Worker     // Skip common symbols already elsewhere.
587*9880d681SAndroid Build Coastguard Worker     if (GlobalSymbolTable.count(Name) ||
588*9880d681SAndroid Build Coastguard Worker         Resolver.findSymbolInLogicalDylib(Name)) {
589*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "\tSkipping already emitted common symbol '" << Name
590*9880d681SAndroid Build Coastguard Worker                    << "'\n");
591*9880d681SAndroid Build Coastguard Worker       continue;
592*9880d681SAndroid Build Coastguard Worker     }
593*9880d681SAndroid Build Coastguard Worker 
594*9880d681SAndroid Build Coastguard Worker     uint32_t Align = Sym.getAlignment();
595*9880d681SAndroid Build Coastguard Worker     uint64_t Size = Sym.getCommonSize();
596*9880d681SAndroid Build Coastguard Worker 
597*9880d681SAndroid Build Coastguard Worker     CommonSize = alignTo(CommonSize, Align) + Size;
598*9880d681SAndroid Build Coastguard Worker 
599*9880d681SAndroid Build Coastguard Worker     SymbolsToAllocate.push_back(Sym);
600*9880d681SAndroid Build Coastguard Worker   }
601*9880d681SAndroid Build Coastguard Worker 
602*9880d681SAndroid Build Coastguard Worker   // Allocate memory for the section
603*9880d681SAndroid Build Coastguard Worker   unsigned SectionID = Sections.size();
604*9880d681SAndroid Build Coastguard Worker   uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign, SectionID,
605*9880d681SAndroid Build Coastguard Worker                                              "<common symbols>", false);
606*9880d681SAndroid Build Coastguard Worker   if (!Addr)
607*9880d681SAndroid Build Coastguard Worker     report_fatal_error("Unable to allocate memory for common symbols!");
608*9880d681SAndroid Build Coastguard Worker   uint64_t Offset = 0;
609*9880d681SAndroid Build Coastguard Worker   Sections.push_back(
610*9880d681SAndroid Build Coastguard Worker       SectionEntry("<common symbols>", Addr, CommonSize, CommonSize, 0));
611*9880d681SAndroid Build Coastguard Worker   memset(Addr, 0, CommonSize);
612*9880d681SAndroid Build Coastguard Worker 
613*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: "
614*9880d681SAndroid Build Coastguard Worker                << format("%p", Addr) << " DataSize: " << CommonSize << "\n");
615*9880d681SAndroid Build Coastguard Worker 
616*9880d681SAndroid Build Coastguard Worker   // Assign the address of each symbol
617*9880d681SAndroid Build Coastguard Worker   for (auto &Sym : SymbolsToAllocate) {
618*9880d681SAndroid Build Coastguard Worker     uint32_t Align = Sym.getAlignment();
619*9880d681SAndroid Build Coastguard Worker     uint64_t Size = Sym.getCommonSize();
620*9880d681SAndroid Build Coastguard Worker     StringRef Name;
621*9880d681SAndroid Build Coastguard Worker     if (auto NameOrErr = Sym.getName())
622*9880d681SAndroid Build Coastguard Worker       Name = *NameOrErr;
623*9880d681SAndroid Build Coastguard Worker     else
624*9880d681SAndroid Build Coastguard Worker       return NameOrErr.takeError();
625*9880d681SAndroid Build Coastguard Worker     if (Align) {
626*9880d681SAndroid Build Coastguard Worker       // This symbol has an alignment requirement.
627*9880d681SAndroid Build Coastguard Worker       uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
628*9880d681SAndroid Build Coastguard Worker       Addr += AlignOffset;
629*9880d681SAndroid Build Coastguard Worker       Offset += AlignOffset;
630*9880d681SAndroid Build Coastguard Worker     }
631*9880d681SAndroid Build Coastguard Worker     uint32_t Flags = Sym.getFlags();
632*9880d681SAndroid Build Coastguard Worker     JITSymbolFlags RTDyldSymFlags = JITSymbolFlags::None;
633*9880d681SAndroid Build Coastguard Worker     if (Flags & SymbolRef::SF_Weak)
634*9880d681SAndroid Build Coastguard Worker       RTDyldSymFlags |= JITSymbolFlags::Weak;
635*9880d681SAndroid Build Coastguard Worker     if (Flags & SymbolRef::SF_Exported)
636*9880d681SAndroid Build Coastguard Worker       RTDyldSymFlags |= JITSymbolFlags::Exported;
637*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
638*9880d681SAndroid Build Coastguard Worker                  << format("%p", Addr) << "\n");
639*9880d681SAndroid Build Coastguard Worker     GlobalSymbolTable[Name] =
640*9880d681SAndroid Build Coastguard Worker       SymbolTableEntry(SectionID, Offset, RTDyldSymFlags);
641*9880d681SAndroid Build Coastguard Worker     Offset += Size;
642*9880d681SAndroid Build Coastguard Worker     Addr += Size;
643*9880d681SAndroid Build Coastguard Worker   }
644*9880d681SAndroid Build Coastguard Worker 
645*9880d681SAndroid Build Coastguard Worker   if (Checker)
646*9880d681SAndroid Build Coastguard Worker     Checker->registerSection(Obj.getFileName(), SectionID);
647*9880d681SAndroid Build Coastguard Worker 
648*9880d681SAndroid Build Coastguard Worker   return Error::success();
649*9880d681SAndroid Build Coastguard Worker }
650*9880d681SAndroid Build Coastguard Worker 
651*9880d681SAndroid Build Coastguard Worker Expected<unsigned>
emitSection(const ObjectFile & Obj,const SectionRef & Section,bool IsCode)652*9880d681SAndroid Build Coastguard Worker RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
653*9880d681SAndroid Build Coastguard Worker                              const SectionRef &Section,
654*9880d681SAndroid Build Coastguard Worker                              bool IsCode) {
655*9880d681SAndroid Build Coastguard Worker   StringRef data;
656*9880d681SAndroid Build Coastguard Worker   uint64_t Alignment64 = Section.getAlignment();
657*9880d681SAndroid Build Coastguard Worker 
658*9880d681SAndroid Build Coastguard Worker   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
659*9880d681SAndroid Build Coastguard Worker   unsigned PaddingSize = 0;
660*9880d681SAndroid Build Coastguard Worker   unsigned StubBufSize = 0;
661*9880d681SAndroid Build Coastguard Worker   bool IsRequired = isRequiredForExecution(Section);
662*9880d681SAndroid Build Coastguard Worker   bool IsVirtual = Section.isVirtual();
663*9880d681SAndroid Build Coastguard Worker   bool IsZeroInit = isZeroInit(Section);
664*9880d681SAndroid Build Coastguard Worker   bool IsReadOnly = isReadOnlyData(Section);
665*9880d681SAndroid Build Coastguard Worker   uint64_t DataSize = Section.getSize();
666*9880d681SAndroid Build Coastguard Worker 
667*9880d681SAndroid Build Coastguard Worker   StringRef Name;
668*9880d681SAndroid Build Coastguard Worker   if (auto EC = Section.getName(Name))
669*9880d681SAndroid Build Coastguard Worker     return errorCodeToError(EC);
670*9880d681SAndroid Build Coastguard Worker 
671*9880d681SAndroid Build Coastguard Worker   StubBufSize = computeSectionStubBufSize(Obj, Section);
672*9880d681SAndroid Build Coastguard Worker 
673*9880d681SAndroid Build Coastguard Worker   // The .eh_frame section (at least on Linux) needs an extra four bytes padded
674*9880d681SAndroid Build Coastguard Worker   // with zeroes added at the end.  For MachO objects, this section has a
675*9880d681SAndroid Build Coastguard Worker   // slightly different name, so this won't have any effect for MachO objects.
676*9880d681SAndroid Build Coastguard Worker   if (Name == ".eh_frame")
677*9880d681SAndroid Build Coastguard Worker     PaddingSize = 4;
678*9880d681SAndroid Build Coastguard Worker 
679*9880d681SAndroid Build Coastguard Worker   uintptr_t Allocate;
680*9880d681SAndroid Build Coastguard Worker   unsigned SectionID = Sections.size();
681*9880d681SAndroid Build Coastguard Worker   uint8_t *Addr;
682*9880d681SAndroid Build Coastguard Worker   const char *pData = nullptr;
683*9880d681SAndroid Build Coastguard Worker 
684*9880d681SAndroid Build Coastguard Worker   // If this section contains any bits (i.e. isn't a virtual or bss section),
685*9880d681SAndroid Build Coastguard Worker   // grab a reference to them.
686*9880d681SAndroid Build Coastguard Worker   if (!IsVirtual && !IsZeroInit) {
687*9880d681SAndroid Build Coastguard Worker     // In either case, set the location of the unrelocated section in memory,
688*9880d681SAndroid Build Coastguard Worker     // since we still process relocations for it even if we're not applying them.
689*9880d681SAndroid Build Coastguard Worker     if (auto EC = Section.getContents(data))
690*9880d681SAndroid Build Coastguard Worker       return errorCodeToError(EC);
691*9880d681SAndroid Build Coastguard Worker     pData = data.data();
692*9880d681SAndroid Build Coastguard Worker   }
693*9880d681SAndroid Build Coastguard Worker 
694*9880d681SAndroid Build Coastguard Worker   // Code section alignment needs to be at least as high as stub alignment or
695*9880d681SAndroid Build Coastguard Worker   // padding calculations may by incorrect when the section is remapped to a
696*9880d681SAndroid Build Coastguard Worker   // higher alignment.
697*9880d681SAndroid Build Coastguard Worker   if (IsCode)
698*9880d681SAndroid Build Coastguard Worker     Alignment = std::max(Alignment, getStubAlignment());
699*9880d681SAndroid Build Coastguard Worker 
700*9880d681SAndroid Build Coastguard Worker   // Some sections, such as debug info, don't need to be loaded for execution.
701*9880d681SAndroid Build Coastguard Worker   // Leave those where they are.
702*9880d681SAndroid Build Coastguard Worker   if (IsRequired) {
703*9880d681SAndroid Build Coastguard Worker     Allocate = DataSize + PaddingSize + StubBufSize;
704*9880d681SAndroid Build Coastguard Worker     if (!Allocate)
705*9880d681SAndroid Build Coastguard Worker       Allocate = 1;
706*9880d681SAndroid Build Coastguard Worker     Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID,
707*9880d681SAndroid Build Coastguard Worker                                                Name)
708*9880d681SAndroid Build Coastguard Worker                   : MemMgr.allocateDataSection(Allocate, Alignment, SectionID,
709*9880d681SAndroid Build Coastguard Worker                                                Name, IsReadOnly);
710*9880d681SAndroid Build Coastguard Worker     if (!Addr)
711*9880d681SAndroid Build Coastguard Worker       report_fatal_error("Unable to allocate section memory!");
712*9880d681SAndroid Build Coastguard Worker 
713*9880d681SAndroid Build Coastguard Worker     // Zero-initialize or copy the data from the image
714*9880d681SAndroid Build Coastguard Worker     if (IsZeroInit || IsVirtual)
715*9880d681SAndroid Build Coastguard Worker       memset(Addr, 0, DataSize);
716*9880d681SAndroid Build Coastguard Worker     else
717*9880d681SAndroid Build Coastguard Worker       memcpy(Addr, pData, DataSize);
718*9880d681SAndroid Build Coastguard Worker 
719*9880d681SAndroid Build Coastguard Worker     // Fill in any extra bytes we allocated for padding
720*9880d681SAndroid Build Coastguard Worker     if (PaddingSize != 0) {
721*9880d681SAndroid Build Coastguard Worker       memset(Addr + DataSize, 0, PaddingSize);
722*9880d681SAndroid Build Coastguard Worker       // Update the DataSize variable so that the stub offset is set correctly.
723*9880d681SAndroid Build Coastguard Worker       DataSize += PaddingSize;
724*9880d681SAndroid Build Coastguard Worker     }
725*9880d681SAndroid Build Coastguard Worker 
726*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
727*9880d681SAndroid Build Coastguard Worker                  << " obj addr: " << format("%p", pData)
728*9880d681SAndroid Build Coastguard Worker                  << " new addr: " << format("%p", Addr)
729*9880d681SAndroid Build Coastguard Worker                  << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
730*9880d681SAndroid Build Coastguard Worker                  << " Allocate: " << Allocate << "\n");
731*9880d681SAndroid Build Coastguard Worker   } else {
732*9880d681SAndroid Build Coastguard Worker     // Even if we didn't load the section, we need to record an entry for it
733*9880d681SAndroid Build Coastguard Worker     // to handle later processing (and by 'handle' I mean don't do anything
734*9880d681SAndroid Build Coastguard Worker     // with these sections).
735*9880d681SAndroid Build Coastguard Worker     Allocate = 0;
736*9880d681SAndroid Build Coastguard Worker     Addr = nullptr;
737*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
738*9880d681SAndroid Build Coastguard Worker                  << " obj addr: " << format("%p", data.data()) << " new addr: 0"
739*9880d681SAndroid Build Coastguard Worker                  << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
740*9880d681SAndroid Build Coastguard Worker                  << " Allocate: " << Allocate << "\n");
741*9880d681SAndroid Build Coastguard Worker   }
742*9880d681SAndroid Build Coastguard Worker 
743*9880d681SAndroid Build Coastguard Worker   Sections.push_back(
744*9880d681SAndroid Build Coastguard Worker       SectionEntry(Name, Addr, DataSize, Allocate, (uintptr_t)pData));
745*9880d681SAndroid Build Coastguard Worker 
746*9880d681SAndroid Build Coastguard Worker   if (Checker)
747*9880d681SAndroid Build Coastguard Worker     Checker->registerSection(Obj.getFileName(), SectionID);
748*9880d681SAndroid Build Coastguard Worker 
749*9880d681SAndroid Build Coastguard Worker   return SectionID;
750*9880d681SAndroid Build Coastguard Worker }
751*9880d681SAndroid Build Coastguard Worker 
752*9880d681SAndroid Build Coastguard Worker Expected<unsigned>
findOrEmitSection(const ObjectFile & Obj,const SectionRef & Section,bool IsCode,ObjSectionToIDMap & LocalSections)753*9880d681SAndroid Build Coastguard Worker RuntimeDyldImpl::findOrEmitSection(const ObjectFile &Obj,
754*9880d681SAndroid Build Coastguard Worker                                    const SectionRef &Section,
755*9880d681SAndroid Build Coastguard Worker                                    bool IsCode,
756*9880d681SAndroid Build Coastguard Worker                                    ObjSectionToIDMap &LocalSections) {
757*9880d681SAndroid Build Coastguard Worker 
758*9880d681SAndroid Build Coastguard Worker   unsigned SectionID = 0;
759*9880d681SAndroid Build Coastguard Worker   ObjSectionToIDMap::iterator i = LocalSections.find(Section);
760*9880d681SAndroid Build Coastguard Worker   if (i != LocalSections.end())
761*9880d681SAndroid Build Coastguard Worker     SectionID = i->second;
762*9880d681SAndroid Build Coastguard Worker   else {
763*9880d681SAndroid Build Coastguard Worker     if (auto SectionIDOrErr = emitSection(Obj, Section, IsCode))
764*9880d681SAndroid Build Coastguard Worker       SectionID = *SectionIDOrErr;
765*9880d681SAndroid Build Coastguard Worker     else
766*9880d681SAndroid Build Coastguard Worker       return SectionIDOrErr.takeError();
767*9880d681SAndroid Build Coastguard Worker     LocalSections[Section] = SectionID;
768*9880d681SAndroid Build Coastguard Worker   }
769*9880d681SAndroid Build Coastguard Worker   return SectionID;
770*9880d681SAndroid Build Coastguard Worker }
771*9880d681SAndroid Build Coastguard Worker 
addRelocationForSection(const RelocationEntry & RE,unsigned SectionID)772*9880d681SAndroid Build Coastguard Worker void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
773*9880d681SAndroid Build Coastguard Worker                                               unsigned SectionID) {
774*9880d681SAndroid Build Coastguard Worker   Relocations[SectionID].push_back(RE);
775*9880d681SAndroid Build Coastguard Worker }
776*9880d681SAndroid Build Coastguard Worker 
addRelocationForSymbol(const RelocationEntry & RE,StringRef SymbolName)777*9880d681SAndroid Build Coastguard Worker void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
778*9880d681SAndroid Build Coastguard Worker                                              StringRef SymbolName) {
779*9880d681SAndroid Build Coastguard Worker   // Relocation by symbol.  If the symbol is found in the global symbol table,
780*9880d681SAndroid Build Coastguard Worker   // create an appropriate section relocation.  Otherwise, add it to
781*9880d681SAndroid Build Coastguard Worker   // ExternalSymbolRelocations.
782*9880d681SAndroid Build Coastguard Worker   RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
783*9880d681SAndroid Build Coastguard Worker   if (Loc == GlobalSymbolTable.end()) {
784*9880d681SAndroid Build Coastguard Worker     ExternalSymbolRelocations[SymbolName].push_back(RE);
785*9880d681SAndroid Build Coastguard Worker   } else {
786*9880d681SAndroid Build Coastguard Worker     // Copy the RE since we want to modify its addend.
787*9880d681SAndroid Build Coastguard Worker     RelocationEntry RECopy = RE;
788*9880d681SAndroid Build Coastguard Worker     const auto &SymInfo = Loc->second;
789*9880d681SAndroid Build Coastguard Worker     RECopy.Addend += SymInfo.getOffset();
790*9880d681SAndroid Build Coastguard Worker     Relocations[SymInfo.getSectionID()].push_back(RECopy);
791*9880d681SAndroid Build Coastguard Worker   }
792*9880d681SAndroid Build Coastguard Worker }
793*9880d681SAndroid Build Coastguard Worker 
createStubFunction(uint8_t * Addr,unsigned AbiVariant)794*9880d681SAndroid Build Coastguard Worker uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
795*9880d681SAndroid Build Coastguard Worker                                              unsigned AbiVariant) {
796*9880d681SAndroid Build Coastguard Worker   if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) {
797*9880d681SAndroid Build Coastguard Worker     // This stub has to be able to access the full address space,
798*9880d681SAndroid Build Coastguard Worker     // since symbol lookup won't necessarily find a handy, in-range,
799*9880d681SAndroid Build Coastguard Worker     // PLT stub for functions which could be anywhere.
800*9880d681SAndroid Build Coastguard Worker     // Stub can use ip0 (== x16) to calculate address
801*9880d681SAndroid Build Coastguard Worker     writeBytesUnaligned(0xd2e00010, Addr,    4); // movz ip0, #:abs_g3:<addr>
802*9880d681SAndroid Build Coastguard Worker     writeBytesUnaligned(0xf2c00010, Addr+4,  4); // movk ip0, #:abs_g2_nc:<addr>
803*9880d681SAndroid Build Coastguard Worker     writeBytesUnaligned(0xf2a00010, Addr+8,  4); // movk ip0, #:abs_g1_nc:<addr>
804*9880d681SAndroid Build Coastguard Worker     writeBytesUnaligned(0xf2800010, Addr+12, 4); // movk ip0, #:abs_g0_nc:<addr>
805*9880d681SAndroid Build Coastguard Worker     writeBytesUnaligned(0xd61f0200, Addr+16, 4); // br ip0
806*9880d681SAndroid Build Coastguard Worker 
807*9880d681SAndroid Build Coastguard Worker     return Addr;
808*9880d681SAndroid Build Coastguard Worker   } else if (Arch == Triple::arm || Arch == Triple::armeb) {
809*9880d681SAndroid Build Coastguard Worker     // TODO: There is only ARM far stub now. We should add the Thumb stub,
810*9880d681SAndroid Build Coastguard Worker     // and stubs for branches Thumb - ARM and ARM - Thumb.
811*9880d681SAndroid Build Coastguard Worker     writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc,<label>
812*9880d681SAndroid Build Coastguard Worker     return Addr + 4;
813*9880d681SAndroid Build Coastguard Worker   } else if (IsMipsO32ABI) {
814*9880d681SAndroid Build Coastguard Worker     // 0:   3c190000        lui     t9,%hi(addr).
815*9880d681SAndroid Build Coastguard Worker     // 4:   27390000        addiu   t9,t9,%lo(addr).
816*9880d681SAndroid Build Coastguard Worker     // 8:   03200008        jr      t9.
817*9880d681SAndroid Build Coastguard Worker     // c:   00000000        nop.
818*9880d681SAndroid Build Coastguard Worker     const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
819*9880d681SAndroid Build Coastguard Worker     const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
820*9880d681SAndroid Build Coastguard Worker 
821*9880d681SAndroid Build Coastguard Worker     writeBytesUnaligned(LuiT9Instr, Addr, 4);
822*9880d681SAndroid Build Coastguard Worker     writeBytesUnaligned(AdduiT9Instr, Addr+4, 4);
823*9880d681SAndroid Build Coastguard Worker     writeBytesUnaligned(JrT9Instr, Addr+8, 4);
824*9880d681SAndroid Build Coastguard Worker     writeBytesUnaligned(NopInstr, Addr+12, 4);
825*9880d681SAndroid Build Coastguard Worker     return Addr;
826*9880d681SAndroid Build Coastguard Worker   } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
827*9880d681SAndroid Build Coastguard Worker     // Depending on which version of the ELF ABI is in use, we need to
828*9880d681SAndroid Build Coastguard Worker     // generate one of two variants of the stub.  They both start with
829*9880d681SAndroid Build Coastguard Worker     // the same sequence to load the target address into r12.
830*9880d681SAndroid Build Coastguard Worker     writeInt32BE(Addr,    0x3D800000); // lis   r12, highest(addr)
831*9880d681SAndroid Build Coastguard Worker     writeInt32BE(Addr+4,  0x618C0000); // ori   r12, higher(addr)
832*9880d681SAndroid Build Coastguard Worker     writeInt32BE(Addr+8,  0x798C07C6); // sldi  r12, r12, 32
833*9880d681SAndroid Build Coastguard Worker     writeInt32BE(Addr+12, 0x658C0000); // oris  r12, r12, h(addr)
834*9880d681SAndroid Build Coastguard Worker     writeInt32BE(Addr+16, 0x618C0000); // ori   r12, r12, l(addr)
835*9880d681SAndroid Build Coastguard Worker     if (AbiVariant == 2) {
836*9880d681SAndroid Build Coastguard Worker       // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
837*9880d681SAndroid Build Coastguard Worker       // The address is already in r12 as required by the ABI.  Branch to it.
838*9880d681SAndroid Build Coastguard Worker       writeInt32BE(Addr+20, 0xF8410018); // std   r2,  24(r1)
839*9880d681SAndroid Build Coastguard Worker       writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12
840*9880d681SAndroid Build Coastguard Worker       writeInt32BE(Addr+28, 0x4E800420); // bctr
841*9880d681SAndroid Build Coastguard Worker     } else {
842*9880d681SAndroid Build Coastguard Worker       // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
843*9880d681SAndroid Build Coastguard Worker       // Load the function address on r11 and sets it to control register. Also
844*9880d681SAndroid Build Coastguard Worker       // loads the function TOC in r2 and environment pointer to r11.
845*9880d681SAndroid Build Coastguard Worker       writeInt32BE(Addr+20, 0xF8410028); // std   r2,  40(r1)
846*9880d681SAndroid Build Coastguard Worker       writeInt32BE(Addr+24, 0xE96C0000); // ld    r11, 0(r12)
847*9880d681SAndroid Build Coastguard Worker       writeInt32BE(Addr+28, 0xE84C0008); // ld    r2,  0(r12)
848*9880d681SAndroid Build Coastguard Worker       writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
849*9880d681SAndroid Build Coastguard Worker       writeInt32BE(Addr+36, 0xE96C0010); // ld    r11, 16(r2)
850*9880d681SAndroid Build Coastguard Worker       writeInt32BE(Addr+40, 0x4E800420); // bctr
851*9880d681SAndroid Build Coastguard Worker     }
852*9880d681SAndroid Build Coastguard Worker     return Addr;
853*9880d681SAndroid Build Coastguard Worker   } else if (Arch == Triple::systemz) {
854*9880d681SAndroid Build Coastguard Worker     writeInt16BE(Addr,    0xC418);     // lgrl %r1,.+8
855*9880d681SAndroid Build Coastguard Worker     writeInt16BE(Addr+2,  0x0000);
856*9880d681SAndroid Build Coastguard Worker     writeInt16BE(Addr+4,  0x0004);
857*9880d681SAndroid Build Coastguard Worker     writeInt16BE(Addr+6,  0x07F1);     // brc 15,%r1
858*9880d681SAndroid Build Coastguard Worker     // 8-byte address stored at Addr + 8
859*9880d681SAndroid Build Coastguard Worker     return Addr;
860*9880d681SAndroid Build Coastguard Worker   } else if (Arch == Triple::x86_64) {
861*9880d681SAndroid Build Coastguard Worker     *Addr      = 0xFF; // jmp
862*9880d681SAndroid Build Coastguard Worker     *(Addr+1)  = 0x25; // rip
863*9880d681SAndroid Build Coastguard Worker     // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
864*9880d681SAndroid Build Coastguard Worker   } else if (Arch == Triple::x86) {
865*9880d681SAndroid Build Coastguard Worker     *Addr      = 0xE9; // 32-bit pc-relative jump.
866*9880d681SAndroid Build Coastguard Worker   }
867*9880d681SAndroid Build Coastguard Worker   return Addr;
868*9880d681SAndroid Build Coastguard Worker }
869*9880d681SAndroid Build Coastguard Worker 
870*9880d681SAndroid Build Coastguard Worker // Assign an address to a symbol name and resolve all the relocations
871*9880d681SAndroid Build Coastguard Worker // associated with it.
reassignSectionAddress(unsigned SectionID,uint64_t Addr)872*9880d681SAndroid Build Coastguard Worker void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
873*9880d681SAndroid Build Coastguard Worker                                              uint64_t Addr) {
874*9880d681SAndroid Build Coastguard Worker   // The address to use for relocation resolution is not
875*9880d681SAndroid Build Coastguard Worker   // the address of the local section buffer. We must be doing
876*9880d681SAndroid Build Coastguard Worker   // a remote execution environment of some sort. Relocations can't
877*9880d681SAndroid Build Coastguard Worker   // be applied until all the sections have been moved.  The client must
878*9880d681SAndroid Build Coastguard Worker   // trigger this with a call to MCJIT::finalize() or
879*9880d681SAndroid Build Coastguard Worker   // RuntimeDyld::resolveRelocations().
880*9880d681SAndroid Build Coastguard Worker   //
881*9880d681SAndroid Build Coastguard Worker   // Addr is a uint64_t because we can't assume the pointer width
882*9880d681SAndroid Build Coastguard Worker   // of the target is the same as that of the host. Just use a generic
883*9880d681SAndroid Build Coastguard Worker   // "big enough" type.
884*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Reassigning address for section " << SectionID << " ("
885*9880d681SAndroid Build Coastguard Worker                << Sections[SectionID].getName() << "): "
886*9880d681SAndroid Build Coastguard Worker                << format("0x%016" PRIx64, Sections[SectionID].getLoadAddress())
887*9880d681SAndroid Build Coastguard Worker                << " -> " << format("0x%016" PRIx64, Addr) << "\n");
888*9880d681SAndroid Build Coastguard Worker   Sections[SectionID].setLoadAddress(Addr);
889*9880d681SAndroid Build Coastguard Worker }
890*9880d681SAndroid Build Coastguard Worker 
resolveRelocationList(const RelocationList & Relocs,uint64_t Value)891*9880d681SAndroid Build Coastguard Worker void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
892*9880d681SAndroid Build Coastguard Worker                                             uint64_t Value) {
893*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
894*9880d681SAndroid Build Coastguard Worker     const RelocationEntry &RE = Relocs[i];
895*9880d681SAndroid Build Coastguard Worker     // Ignore relocations for sections that were not loaded
896*9880d681SAndroid Build Coastguard Worker     if (Sections[RE.SectionID].getAddress() == nullptr)
897*9880d681SAndroid Build Coastguard Worker       continue;
898*9880d681SAndroid Build Coastguard Worker     resolveRelocation(RE, Value);
899*9880d681SAndroid Build Coastguard Worker   }
900*9880d681SAndroid Build Coastguard Worker }
901*9880d681SAndroid Build Coastguard Worker 
resolveExternalSymbols()902*9880d681SAndroid Build Coastguard Worker void RuntimeDyldImpl::resolveExternalSymbols() {
903*9880d681SAndroid Build Coastguard Worker   while (!ExternalSymbolRelocations.empty()) {
904*9880d681SAndroid Build Coastguard Worker     StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
905*9880d681SAndroid Build Coastguard Worker 
906*9880d681SAndroid Build Coastguard Worker     StringRef Name = i->first();
907*9880d681SAndroid Build Coastguard Worker     if (Name.size() == 0) {
908*9880d681SAndroid Build Coastguard Worker       // This is an absolute symbol, use an address of zero.
909*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "Resolving absolute relocations."
910*9880d681SAndroid Build Coastguard Worker                    << "\n");
911*9880d681SAndroid Build Coastguard Worker       RelocationList &Relocs = i->second;
912*9880d681SAndroid Build Coastguard Worker       resolveRelocationList(Relocs, 0);
913*9880d681SAndroid Build Coastguard Worker     } else {
914*9880d681SAndroid Build Coastguard Worker       uint64_t Addr = 0;
915*9880d681SAndroid Build Coastguard Worker       RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name);
916*9880d681SAndroid Build Coastguard Worker       if (Loc == GlobalSymbolTable.end()) {
917*9880d681SAndroid Build Coastguard Worker         // This is an external symbol, try to get its address from the symbol
918*9880d681SAndroid Build Coastguard Worker         // resolver.
919*9880d681SAndroid Build Coastguard Worker         // First search for the symbol in this logical dylib.
920*9880d681SAndroid Build Coastguard Worker         Addr = Resolver.findSymbolInLogicalDylib(Name.data()).getAddress();
921*9880d681SAndroid Build Coastguard Worker         // If that fails, try searching for an external symbol.
922*9880d681SAndroid Build Coastguard Worker         if (!Addr)
923*9880d681SAndroid Build Coastguard Worker           Addr = Resolver.findSymbol(Name.data()).getAddress();
924*9880d681SAndroid Build Coastguard Worker         // The call to getSymbolAddress may have caused additional modules to
925*9880d681SAndroid Build Coastguard Worker         // be loaded, which may have added new entries to the
926*9880d681SAndroid Build Coastguard Worker         // ExternalSymbolRelocations map.  Consquently, we need to update our
927*9880d681SAndroid Build Coastguard Worker         // iterator.  This is also why retrieval of the relocation list
928*9880d681SAndroid Build Coastguard Worker         // associated with this symbol is deferred until below this point.
929*9880d681SAndroid Build Coastguard Worker         // New entries may have been added to the relocation list.
930*9880d681SAndroid Build Coastguard Worker         i = ExternalSymbolRelocations.find(Name);
931*9880d681SAndroid Build Coastguard Worker       } else {
932*9880d681SAndroid Build Coastguard Worker         // We found the symbol in our global table.  It was probably in a
933*9880d681SAndroid Build Coastguard Worker         // Module that we loaded previously.
934*9880d681SAndroid Build Coastguard Worker         const auto &SymInfo = Loc->second;
935*9880d681SAndroid Build Coastguard Worker         Addr = getSectionLoadAddress(SymInfo.getSectionID()) +
936*9880d681SAndroid Build Coastguard Worker                SymInfo.getOffset();
937*9880d681SAndroid Build Coastguard Worker       }
938*9880d681SAndroid Build Coastguard Worker 
939*9880d681SAndroid Build Coastguard Worker       // FIXME: Implement error handling that doesn't kill the host program!
940*9880d681SAndroid Build Coastguard Worker       if (!Addr)
941*9880d681SAndroid Build Coastguard Worker         report_fatal_error("Program used external function '" + Name +
942*9880d681SAndroid Build Coastguard Worker                            "' which could not be resolved!");
943*9880d681SAndroid Build Coastguard Worker 
944*9880d681SAndroid Build Coastguard Worker       // If Resolver returned UINT64_MAX, the client wants to handle this symbol
945*9880d681SAndroid Build Coastguard Worker       // manually and we shouldn't resolve its relocations.
946*9880d681SAndroid Build Coastguard Worker       if (Addr != UINT64_MAX) {
947*9880d681SAndroid Build Coastguard Worker         DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
948*9880d681SAndroid Build Coastguard Worker                      << format("0x%lx", Addr) << "\n");
949*9880d681SAndroid Build Coastguard Worker         // This list may have been updated when we called getSymbolAddress, so
950*9880d681SAndroid Build Coastguard Worker         // don't change this code to get the list earlier.
951*9880d681SAndroid Build Coastguard Worker         RelocationList &Relocs = i->second;
952*9880d681SAndroid Build Coastguard Worker         resolveRelocationList(Relocs, Addr);
953*9880d681SAndroid Build Coastguard Worker       }
954*9880d681SAndroid Build Coastguard Worker     }
955*9880d681SAndroid Build Coastguard Worker 
956*9880d681SAndroid Build Coastguard Worker     ExternalSymbolRelocations.erase(i);
957*9880d681SAndroid Build Coastguard Worker   }
958*9880d681SAndroid Build Coastguard Worker }
959*9880d681SAndroid Build Coastguard Worker 
960*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
961*9880d681SAndroid Build Coastguard Worker // RuntimeDyld class implementation
962*9880d681SAndroid Build Coastguard Worker 
getSectionLoadAddress(const object::SectionRef & Sec) const963*9880d681SAndroid Build Coastguard Worker uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress(
964*9880d681SAndroid Build Coastguard Worker                                           const object::SectionRef &Sec) const {
965*9880d681SAndroid Build Coastguard Worker 
966*9880d681SAndroid Build Coastguard Worker   auto I = ObjSecToIDMap.find(Sec);
967*9880d681SAndroid Build Coastguard Worker   if (I != ObjSecToIDMap.end())
968*9880d681SAndroid Build Coastguard Worker     return RTDyld.Sections[I->second].getLoadAddress();
969*9880d681SAndroid Build Coastguard Worker 
970*9880d681SAndroid Build Coastguard Worker   return 0;
971*9880d681SAndroid Build Coastguard Worker }
972*9880d681SAndroid Build Coastguard Worker 
anchor()973*9880d681SAndroid Build Coastguard Worker void RuntimeDyld::MemoryManager::anchor() {}
anchor()974*9880d681SAndroid Build Coastguard Worker void RuntimeDyld::SymbolResolver::anchor() {}
975*9880d681SAndroid Build Coastguard Worker 
RuntimeDyld(RuntimeDyld::MemoryManager & MemMgr,RuntimeDyld::SymbolResolver & Resolver)976*9880d681SAndroid Build Coastguard Worker RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr,
977*9880d681SAndroid Build Coastguard Worker                          RuntimeDyld::SymbolResolver &Resolver)
978*9880d681SAndroid Build Coastguard Worker     : MemMgr(MemMgr), Resolver(Resolver) {
979*9880d681SAndroid Build Coastguard Worker   // FIXME: There's a potential issue lurking here if a single instance of
980*9880d681SAndroid Build Coastguard Worker   // RuntimeDyld is used to load multiple objects.  The current implementation
981*9880d681SAndroid Build Coastguard Worker   // associates a single memory manager with a RuntimeDyld instance.  Even
982*9880d681SAndroid Build Coastguard Worker   // though the public class spawns a new 'impl' instance for each load,
983*9880d681SAndroid Build Coastguard Worker   // they share a single memory manager.  This can become a problem when page
984*9880d681SAndroid Build Coastguard Worker   // permissions are applied.
985*9880d681SAndroid Build Coastguard Worker   Dyld = nullptr;
986*9880d681SAndroid Build Coastguard Worker   ProcessAllSections = false;
987*9880d681SAndroid Build Coastguard Worker   Checker = nullptr;
988*9880d681SAndroid Build Coastguard Worker }
989*9880d681SAndroid Build Coastguard Worker 
~RuntimeDyld()990*9880d681SAndroid Build Coastguard Worker RuntimeDyld::~RuntimeDyld() {}
991*9880d681SAndroid Build Coastguard Worker 
992*9880d681SAndroid Build Coastguard Worker static std::unique_ptr<RuntimeDyldCOFF>
createRuntimeDyldCOFF(Triple::ArchType Arch,RuntimeDyld::MemoryManager & MM,RuntimeDyld::SymbolResolver & Resolver,bool ProcessAllSections,RuntimeDyldCheckerImpl * Checker)993*9880d681SAndroid Build Coastguard Worker createRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
994*9880d681SAndroid Build Coastguard Worker                       RuntimeDyld::SymbolResolver &Resolver,
995*9880d681SAndroid Build Coastguard Worker                       bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
996*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<RuntimeDyldCOFF> Dyld =
997*9880d681SAndroid Build Coastguard Worker     RuntimeDyldCOFF::create(Arch, MM, Resolver);
998*9880d681SAndroid Build Coastguard Worker   Dyld->setProcessAllSections(ProcessAllSections);
999*9880d681SAndroid Build Coastguard Worker   Dyld->setRuntimeDyldChecker(Checker);
1000*9880d681SAndroid Build Coastguard Worker   return Dyld;
1001*9880d681SAndroid Build Coastguard Worker }
1002*9880d681SAndroid Build Coastguard Worker 
1003*9880d681SAndroid Build Coastguard Worker static std::unique_ptr<RuntimeDyldELF>
createRuntimeDyldELF(RuntimeDyld::MemoryManager & MM,RuntimeDyld::SymbolResolver & Resolver,bool ProcessAllSections,RuntimeDyldCheckerImpl * Checker)1004*9880d681SAndroid Build Coastguard Worker createRuntimeDyldELF(RuntimeDyld::MemoryManager &MM,
1005*9880d681SAndroid Build Coastguard Worker                      RuntimeDyld::SymbolResolver &Resolver,
1006*9880d681SAndroid Build Coastguard Worker                      bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
1007*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<RuntimeDyldELF> Dyld(new RuntimeDyldELF(MM, Resolver));
1008*9880d681SAndroid Build Coastguard Worker   Dyld->setProcessAllSections(ProcessAllSections);
1009*9880d681SAndroid Build Coastguard Worker   Dyld->setRuntimeDyldChecker(Checker);
1010*9880d681SAndroid Build Coastguard Worker   return Dyld;
1011*9880d681SAndroid Build Coastguard Worker }
1012*9880d681SAndroid Build Coastguard Worker 
1013*9880d681SAndroid Build Coastguard Worker static std::unique_ptr<RuntimeDyldMachO>
createRuntimeDyldMachO(Triple::ArchType Arch,RuntimeDyld::MemoryManager & MM,RuntimeDyld::SymbolResolver & Resolver,bool ProcessAllSections,RuntimeDyldCheckerImpl * Checker)1014*9880d681SAndroid Build Coastguard Worker createRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
1015*9880d681SAndroid Build Coastguard Worker                        RuntimeDyld::SymbolResolver &Resolver,
1016*9880d681SAndroid Build Coastguard Worker                        bool ProcessAllSections,
1017*9880d681SAndroid Build Coastguard Worker                        RuntimeDyldCheckerImpl *Checker) {
1018*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<RuntimeDyldMachO> Dyld =
1019*9880d681SAndroid Build Coastguard Worker     RuntimeDyldMachO::create(Arch, MM, Resolver);
1020*9880d681SAndroid Build Coastguard Worker   Dyld->setProcessAllSections(ProcessAllSections);
1021*9880d681SAndroid Build Coastguard Worker   Dyld->setRuntimeDyldChecker(Checker);
1022*9880d681SAndroid Build Coastguard Worker   return Dyld;
1023*9880d681SAndroid Build Coastguard Worker }
1024*9880d681SAndroid Build Coastguard Worker 
1025*9880d681SAndroid Build Coastguard Worker std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
loadObject(const ObjectFile & Obj)1026*9880d681SAndroid Build Coastguard Worker RuntimeDyld::loadObject(const ObjectFile &Obj) {
1027*9880d681SAndroid Build Coastguard Worker   if (!Dyld) {
1028*9880d681SAndroid Build Coastguard Worker     if (Obj.isELF())
1029*9880d681SAndroid Build Coastguard Worker       Dyld = createRuntimeDyldELF(MemMgr, Resolver, ProcessAllSections, Checker);
1030*9880d681SAndroid Build Coastguard Worker     else if (Obj.isMachO())
1031*9880d681SAndroid Build Coastguard Worker       Dyld = createRuntimeDyldMachO(
1032*9880d681SAndroid Build Coastguard Worker                static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
1033*9880d681SAndroid Build Coastguard Worker                ProcessAllSections, Checker);
1034*9880d681SAndroid Build Coastguard Worker     else if (Obj.isCOFF())
1035*9880d681SAndroid Build Coastguard Worker       Dyld = createRuntimeDyldCOFF(
1036*9880d681SAndroid Build Coastguard Worker                static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
1037*9880d681SAndroid Build Coastguard Worker                ProcessAllSections, Checker);
1038*9880d681SAndroid Build Coastguard Worker     else
1039*9880d681SAndroid Build Coastguard Worker       report_fatal_error("Incompatible object format!");
1040*9880d681SAndroid Build Coastguard Worker   }
1041*9880d681SAndroid Build Coastguard Worker 
1042*9880d681SAndroid Build Coastguard Worker   if (!Dyld->isCompatibleFile(Obj))
1043*9880d681SAndroid Build Coastguard Worker     report_fatal_error("Incompatible object format!");
1044*9880d681SAndroid Build Coastguard Worker 
1045*9880d681SAndroid Build Coastguard Worker   auto LoadedObjInfo = Dyld->loadObject(Obj);
1046*9880d681SAndroid Build Coastguard Worker   MemMgr.notifyObjectLoaded(*this, Obj);
1047*9880d681SAndroid Build Coastguard Worker   return LoadedObjInfo;
1048*9880d681SAndroid Build Coastguard Worker }
1049*9880d681SAndroid Build Coastguard Worker 
getSymbolLocalAddress(StringRef Name) const1050*9880d681SAndroid Build Coastguard Worker void *RuntimeDyld::getSymbolLocalAddress(StringRef Name) const {
1051*9880d681SAndroid Build Coastguard Worker   if (!Dyld)
1052*9880d681SAndroid Build Coastguard Worker     return nullptr;
1053*9880d681SAndroid Build Coastguard Worker   return Dyld->getSymbolLocalAddress(Name);
1054*9880d681SAndroid Build Coastguard Worker }
1055*9880d681SAndroid Build Coastguard Worker 
getSymbol(StringRef Name) const1056*9880d681SAndroid Build Coastguard Worker RuntimeDyld::SymbolInfo RuntimeDyld::getSymbol(StringRef Name) const {
1057*9880d681SAndroid Build Coastguard Worker   if (!Dyld)
1058*9880d681SAndroid Build Coastguard Worker     return nullptr;
1059*9880d681SAndroid Build Coastguard Worker   return Dyld->getSymbol(Name);
1060*9880d681SAndroid Build Coastguard Worker }
1061*9880d681SAndroid Build Coastguard Worker 
resolveRelocations()1062*9880d681SAndroid Build Coastguard Worker void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
1063*9880d681SAndroid Build Coastguard Worker 
reassignSectionAddress(unsigned SectionID,uint64_t Addr)1064*9880d681SAndroid Build Coastguard Worker void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
1065*9880d681SAndroid Build Coastguard Worker   Dyld->reassignSectionAddress(SectionID, Addr);
1066*9880d681SAndroid Build Coastguard Worker }
1067*9880d681SAndroid Build Coastguard Worker 
mapSectionAddress(const void * LocalAddress,uint64_t TargetAddress)1068*9880d681SAndroid Build Coastguard Worker void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
1069*9880d681SAndroid Build Coastguard Worker                                     uint64_t TargetAddress) {
1070*9880d681SAndroid Build Coastguard Worker   Dyld->mapSectionAddress(LocalAddress, TargetAddress);
1071*9880d681SAndroid Build Coastguard Worker }
1072*9880d681SAndroid Build Coastguard Worker 
hasError()1073*9880d681SAndroid Build Coastguard Worker bool RuntimeDyld::hasError() { return Dyld->hasError(); }
1074*9880d681SAndroid Build Coastguard Worker 
getErrorString()1075*9880d681SAndroid Build Coastguard Worker StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
1076*9880d681SAndroid Build Coastguard Worker 
finalizeWithMemoryManagerLocking()1077*9880d681SAndroid Build Coastguard Worker void RuntimeDyld::finalizeWithMemoryManagerLocking() {
1078*9880d681SAndroid Build Coastguard Worker   bool MemoryFinalizationLocked = MemMgr.FinalizationLocked;
1079*9880d681SAndroid Build Coastguard Worker   MemMgr.FinalizationLocked = true;
1080*9880d681SAndroid Build Coastguard Worker   resolveRelocations();
1081*9880d681SAndroid Build Coastguard Worker   registerEHFrames();
1082*9880d681SAndroid Build Coastguard Worker   if (!MemoryFinalizationLocked) {
1083*9880d681SAndroid Build Coastguard Worker     MemMgr.finalizeMemory();
1084*9880d681SAndroid Build Coastguard Worker     MemMgr.FinalizationLocked = false;
1085*9880d681SAndroid Build Coastguard Worker   }
1086*9880d681SAndroid Build Coastguard Worker }
1087*9880d681SAndroid Build Coastguard Worker 
registerEHFrames()1088*9880d681SAndroid Build Coastguard Worker void RuntimeDyld::registerEHFrames() {
1089*9880d681SAndroid Build Coastguard Worker   if (Dyld)
1090*9880d681SAndroid Build Coastguard Worker     Dyld->registerEHFrames();
1091*9880d681SAndroid Build Coastguard Worker }
1092*9880d681SAndroid Build Coastguard Worker 
deregisterEHFrames()1093*9880d681SAndroid Build Coastguard Worker void RuntimeDyld::deregisterEHFrames() {
1094*9880d681SAndroid Build Coastguard Worker   if (Dyld)
1095*9880d681SAndroid Build Coastguard Worker     Dyld->deregisterEHFrames();
1096*9880d681SAndroid Build Coastguard Worker }
1097*9880d681SAndroid Build Coastguard Worker 
1098*9880d681SAndroid Build Coastguard Worker } // end namespace llvm
1099