1e0934bee3a4f40731169bc42b15a39ce39978175Jim Grosbach//===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
26e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//
36e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//                     The LLVM Compiler Infrastructure
46e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//
56e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach// This file is distributed under the University of Illinois Open Source
66e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach// License. See LICENSE.TXT for details.
76e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//
86e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//===----------------------------------------------------------------------===//
96e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//
106e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach// Implementation of the MC-JIT runtime dynamic linker.
116e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//
126e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//===----------------------------------------------------------------------===//
136e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
14d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ExecutionEngine/RuntimeDyld.h"
1537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "RuntimeDyldCheckerImpl.h"
164c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar#include "RuntimeDyldCOFF.h"
1776463fdeb603e1d89b05f094bfd6fe73b90d0b61Eli Bendersky#include "RuntimeDyldELF.h"
18d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "RuntimeDyldImpl.h"
1976463fdeb603e1d89b05f094bfd6fe73b90d0b61Eli Bendersky#include "RuntimeDyldMachO.h"
20ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/Object/ELFObjectFile.h"
214c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar#include "llvm/Object/COFF.h"
22f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover#include "llvm/Support/MathExtras.h"
236169453ba37ac353655f2475f336e66f31276752Andrew Kaylor#include "llvm/Support/MutexGuard.h"
2476463fdeb603e1d89b05f094bfd6fe73b90d0b61Eli Bendersky
256e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbachusing namespace llvm;
266e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbachusing namespace llvm::object;
276e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
28dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define DEBUG_TYPE "dyld"
29dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
3053c5e7b2e7e36d70bd943cb2d576121435bd5b3fChandler Carruth// Empty out-of-line virtual destructor as the key function.
31cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil MalyshevRuntimeDyldImpl::~RuntimeDyldImpl() {}
3253c5e7b2e7e36d70bd943cb2d576121435bd5b3fChandler Carruth
33ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines// Pin LoadedObjectInfo's vtables to this file.
34ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid RuntimeDyld::LoadedObjectInfo::anchor() {}
35354362524a72b3fa43a6c09380b7ae3b2380cbbaJuergen Ributzka
366e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbachnamespace llvm {
376e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
3836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvoid RuntimeDyldImpl::registerEHFrames() {}
39a2e40fbd624916c187a95ed76939ca7f02ed3e53Rafael Espindola
4036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvoid RuntimeDyldImpl::deregisterEHFrames() {}
4143507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor
4237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#ifndef NDEBUG
4337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstatic void dumpSectionMemory(const SectionEntry &S, StringRef State) {
4437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  dbgs() << "----- Contents of section " << S.Name << " " << State << " -----";
4537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
4637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (S.Address == nullptr) {
4737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    dbgs() << "\n          <section not emitted>\n";
4837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return;
4937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
5037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
5137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  const unsigned ColsPerRow = 16;
5237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
5337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  uint8_t *DataAddr = S.Address;
5437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  uint64_t LoadAddr = S.LoadAddress;
5537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
5637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  unsigned StartPadding = LoadAddr & (ColsPerRow - 1);
5737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  unsigned BytesRemaining = S.Size;
5837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
5937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (StartPadding) {
602c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar    dbgs() << "\n" << format("0x%016" PRIx64,
612c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                             LoadAddr & ~(uint64_t)(ColsPerRow - 1)) << ":";
6237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    while (StartPadding--)
6337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      dbgs() << "   ";
6437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
6537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
6637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  while (BytesRemaining > 0) {
6737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if ((LoadAddr & (ColsPerRow - 1)) == 0)
6837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      dbgs() << "\n" << format("0x%016" PRIx64, LoadAddr) << ":";
6937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
7037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    dbgs() << " " << format("%02x", *DataAddr);
7137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
7237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    ++DataAddr;
7337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    ++LoadAddr;
7437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    --BytesRemaining;
7537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
7637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
7737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  dbgs() << "\n";
7837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
7937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#endif
8037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
81f8c1c8465ff097ad5b87331b6d9a2576167f1402Jim Grosbach// Resolve the relocations for all symbols we currently know about.
82f8c1c8465ff097ad5b87331b6d9a2576167f1402Jim Grosbachvoid RuntimeDyldImpl::resolveRelocations() {
836169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  MutexGuard locked(lock);
846169453ba37ac353655f2475f336e66f31276752Andrew Kaylor
85c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  // First, resolve relocations associated with external symbols.
8637bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  resolveExternalSymbols();
870e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
8861425c0a7f4e3608a85f7bbf254cd052a15b7446Jim Grosbach  // Just iterate over the sections we have and resolve all the relocations
8961425c0a7f4e3608a85f7bbf254cd052a15b7446Jim Grosbach  // in them. Gross overkill, but it gets the job done.
9061425c0a7f4e3608a85f7bbf254cd052a15b7446Jim Grosbach  for (int i = 0, e = Sections.size(); i != e; ++i) {
9132bd10b1a33df2cc4d067a16901d56665f4ba085Andrew Kaylor    // The Section here (Sections[i]) refers to the section in which the
9232bd10b1a33df2cc4d067a16901d56665f4ba085Andrew Kaylor    // symbol for the relocation is located.  The SectionID in the relocation
9332bd10b1a33df2cc4d067a16901d56665f4ba085Andrew Kaylor    // entry provides the section to which the relocation will be applied.
9428989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor    uint64_t Addr = Sections[i].LoadAddress;
9536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    DEBUG(dbgs() << "Resolving relocations Section #" << i << "\t"
962c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                 << format("%p", (uintptr_t)Addr) << "\n");
9737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    DEBUG(dumpSectionMemory(Sections[i], "before relocations"));
9828989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor    resolveRelocationList(Relocations[i], Addr);
9937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    DEBUG(dumpSectionMemory(Sections[i], "after relocations"));
1008e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor    Relocations.erase(i);
10161425c0a7f4e3608a85f7bbf254cd052a15b7446Jim Grosbach  }
102f8c1c8465ff097ad5b87331b6d9a2576167f1402Jim Grosbach}
103f8c1c8465ff097ad5b87331b6d9a2576167f1402Jim Grosbach
104e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbachvoid RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
105020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach                                        uint64_t TargetAddress) {
1066169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  MutexGuard locked(lock);
1070e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
1080e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev    if (Sections[i].Address == LocalAddress) {
1090e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev      reassignSectionAddress(i, TargetAddress);
1100e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev      return;
1110e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev    }
1120e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  }
1130e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  llvm_unreachable("Attempting to remap address of unknown section!");
1140e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev}
1150e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
116c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesstatic std::error_code getOffset(const SymbolRef &Sym, uint64_t &Result) {
117dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  uint64_t Address;
118c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  if (std::error_code EC = Sym.getAddress(Address))
119dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return EC;
120dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
121dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (Address == UnknownAddressOrSize) {
122dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Result = UnknownAddressOrSize;
123dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return object_error::success;
124dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
125dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
126dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  const ObjectFile *Obj = Sym.getObject();
127dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  section_iterator SecI(Obj->section_begin());
128c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  if (std::error_code EC = Sym.getSection(SecI))
129dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return EC;
130dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
13137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (SecI == Obj->section_end()) {
13237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Result = UnknownAddressOrSize;
13337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return object_error::success;
13437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
135dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
13637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  uint64_t SectionAddress = SecI->getAddress();
137dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Result = Address - SectionAddress;
138dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  return object_error::success;
139dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
140dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
141ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstd::pair<unsigned, unsigned>
142ebe69fe11e48d322045d5949c83283927a0d790bStephen HinesRuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) {
1436169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  MutexGuard locked(lock);
1446169453ba37ac353655f2475f336e66f31276752Andrew Kaylor
145ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // Grab the first Section ID. We'll use this later to construct the underlying
146ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // range for the returned LoadedObjectInfo.
147ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  unsigned SectionsAddedBeginIdx = Sections.size();
1480e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
149ab950f5f334d30a7b5bfb1e009846dfb0b47f61cAndrew Kaylor  // Save information about our target
150ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Arch = (Triple::ArchType)Obj.getArch();
151ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  IsTargetLittleEndian = Obj.isLittleEndian();
15236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
15336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // Compute the memory size required to load all sections to be loaded
15436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // and pass this information to the memory manager
1552c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar  if (MemMgr.needsToReserveAllocationSpace()) {
15636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    uint64_t CodeSize = 0, DataSizeRO = 0, DataSizeRW = 0;
157ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    computeTotalAllocSize(Obj, CodeSize, DataSizeRO, DataSizeRW);
1582c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar    MemMgr.reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
15936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
1600e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
161d98c9e918c9750d965c1efe9efcc9e13feacbe13Eli Bendersky  // Used sections from the object file
162d98c9e918c9750d965c1efe9efcc9e13feacbe13Eli Bendersky  ObjSectionToIDMap LocalSections;
163d98c9e918c9750d965c1efe9efcc9e13feacbe13Eli Bendersky
164f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover  // Common symbols requiring allocation, with their sizes and alignments
165ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  CommonSymbolList CommonSymbols;
1660e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
1670e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // Parse symbols
1680e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  DEBUG(dbgs() << "Parse symbols:\n");
169ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
17036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines       ++I) {
17136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    uint32_t Flags = I->getFlags();
172c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
17336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    bool IsCommon = Flags & SymbolRef::SF_Common;
174ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (IsCommon)
175ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      CommonSymbols.push_back(*I);
176ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    else {
177ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      object::SymbolRef::Type SymType;
178ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      Check(I->getType(SymType));
179ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
180c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd      if (SymType == object::SymbolRef::ST_Function ||
181b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka          SymType == object::SymbolRef::ST_Data ||
182b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka          SymType == object::SymbolRef::ST_Unknown) {
183ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
184ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        StringRef Name;
185dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        uint64_t SectOffset;
186ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        Check(I->getName(Name));
187dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        Check(getOffset(*I, SectOffset));
188ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        section_iterator SI = Obj.section_end();
18936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        Check(I->getSection(SI));
190ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        if (SI == Obj.section_end())
19136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines          continue;
192ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        StringRef SectionData;
19336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        Check(SI->getContents(SectionData));
19437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines        bool IsCode = SI->isText();
19536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        unsigned SectionID =
196ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines            findOrEmitSection(Obj, *SI, IsCode, LocalSections);
197ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name
198ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                     << " SID: " << SectionID << " Offset: "
199ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                     << format("%p", (uintptr_t)SectOffset)
200ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                     << " flags: " << Flags << "\n");
2014c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar        JITSymbolFlags RTDyldSymFlags = JITSymbolFlags::None;
2024c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar        if (Flags & SymbolRef::SF_Weak)
2034c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar          RTDyldSymFlags |= JITSymbolFlags::Weak;
2044c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar        if (Flags & SymbolRef::SF_Exported)
2054c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar          RTDyldSymFlags |= JITSymbolFlags::Exported;
2064c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar        GlobalSymbolTable[Name] =
2074c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar          SymbolTableEntry(SectionID, SectOffset, RTDyldSymFlags);
208c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd      }
2090e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev    }
2100e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  }
2110e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
212c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  // Allocate common symbols
213ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  emitCommonSymbols(Obj, CommonSymbols);
214c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
2155fe019835c269ccbfe185276269bc53b3f9a7a86Eli Bendersky  // Parse and process relocations
2160e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  DEBUG(dbgs() << "Parse relocations:\n");
217ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
21836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines       SI != SE; ++SI) {
2190e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev    unsigned SectionID = 0;
2200e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev    StubMap Stubs;
22136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    section_iterator RelocatedSection = SI->getRelocatedSection();
2220e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
223ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (RelocatedSection == SE)
224ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      continue;
225ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
226dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    relocation_iterator I = SI->relocation_begin();
227dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    relocation_iterator E = SI->relocation_end();
228dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
229dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (I == E && !ProcessAllSections)
23036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      continue;
23136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
23237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    bool IsCode = RelocatedSection->isText();
23336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    SectionID =
234ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        findOrEmitSection(Obj, *RelocatedSection, IsCode, LocalSections);
23536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
23636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
237dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    for (; I != E;)
238ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      I = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs);
23937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
24037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // If there is an attached checker, notify it about the stubs for this
24137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // section so that they can be verified.
24237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (Checker)
243ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      Checker->registerStubMap(Obj.getFileName(), SectionID, Stubs);
2440e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  }
245689ff9c00f4f3dcf3491778bcdbda79e19e2285dPreston Gurd
246ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  // Give the subclasses a chance to tie-up any loose ends.
247ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  finalizeLoad(Obj, LocalSections);
248ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
249ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  unsigned SectionsAddedEndIdx = Sections.size();
250ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor
251ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return std::make_pair(SectionsAddedBeginIdx, SectionsAddedEndIdx);
25236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
25336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
25436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// A helper method for computeTotalAllocSize.
25536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// Computes the memory size required to allocate sections with the given sizes,
25636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// assuming that all sections are allocated with the given alignment
25736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic uint64_t
25836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinescomputeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
25936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                 uint64_t Alignment) {
26036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  uint64_t TotalSize = 0;
26136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
26236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    uint64_t AlignedSize =
26336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
26436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    TotalSize += AlignedSize;
26536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
26636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  return TotalSize;
26736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
26836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
269ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstatic bool isRequiredForExecution(const SectionRef &Section) {
270ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  const ObjectFile *Obj = Section.getObject();
271ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  if (auto *ELFObj = dyn_cast<object::ELFObjectFileBase>(Obj))
272ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return ELFObj->getSectionFlags(Section) & ELF::SHF_ALLOC;
2734c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) {
2744c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    const coff_section *CoffSection = COFFObj->getCOFFSection(Section);
2754c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    // Avoid loading zero-sized COFF sections.
2764c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    // In PE files, VirtualSize gives the section size, and SizeOfRawData
2774c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    // may be zero for sections with content. In Obj files, SizeOfRawData
2784c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    // gives the section size, and VirtualSize is always zero. Hence
2794c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    // the need to check for both cases below.
2804c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    bool HasContent = (CoffSection->VirtualSize > 0)
2814c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar      || (CoffSection->SizeOfRawData > 0);
2824c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    bool IsDiscardable = CoffSection->Characteristics &
2834c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar      (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_LNK_INFO);
2844c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    return HasContent && !IsDiscardable;
2854c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  }
2864c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar
287ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  assert(isa<MachOObjectFile>(Obj));
288ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return true;
289ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines }
290ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
291ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstatic bool isReadOnlyData(const SectionRef &Section) {
292ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  const ObjectFile *Obj = Section.getObject();
293ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  if (auto *ELFObj = dyn_cast<object::ELFObjectFileBase>(Obj))
294ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return !(ELFObj->getSectionFlags(Section) &
295ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines             (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
2964c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
2974c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    return ((COFFObj->getCOFFSection(Section)->Characteristics &
2984c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar             (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
2994c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar             | COFF::IMAGE_SCN_MEM_READ
3004c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar             | COFF::IMAGE_SCN_MEM_WRITE))
3014c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar             ==
3024c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar             (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
3034c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar             | COFF::IMAGE_SCN_MEM_READ));
3044c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar
305ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  assert(isa<MachOObjectFile>(Obj));
306ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return false;
307ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
308ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
309ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstatic bool isZeroInit(const SectionRef &Section) {
310ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  const ObjectFile *Obj = Section.getObject();
311ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  if (auto *ELFObj = dyn_cast<object::ELFObjectFileBase>(Obj))
312ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return ELFObj->getSectionType(Section) == ELF::SHT_NOBITS;
3134c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
3144c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    return COFFObj->getCOFFSection(Section)->Characteristics &
3154c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar            COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
316ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
317ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  auto *MachO = cast<MachOObjectFile>(Obj);
318ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  unsigned SectionType = MachO->getSectionType(Section);
319ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return SectionType == MachO::S_ZEROFILL ||
320ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines         SectionType == MachO::S_GB_ZEROFILL;
321ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
322ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
32336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// Compute an upper bound of the memory size that is required to load all
32436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// sections
325ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj,
32636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                            uint64_t &CodeSize,
32736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                            uint64_t &DataSizeRO,
32836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                            uint64_t &DataSizeRW) {
32936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // Compute the size of all sections required for execution
33036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::vector<uint64_t> CodeSectionSizes;
33136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::vector<uint64_t> ROSectionSizes;
33236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::vector<uint64_t> RWSectionSizes;
33336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  uint64_t MaxAlignment = sizeof(void *);
33436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
33536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // Collect sizes of all sections to be loaded;
33636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // also determine the max alignment of all sections
337ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
33836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines       SI != SE; ++SI) {
33936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    const SectionRef &Section = *SI;
34036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
341ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    bool IsRequired = isRequiredForExecution(Section);
34236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
34336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // Consider only the sections that are required to be loaded for execution
34436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (IsRequired) {
34536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      StringRef Name;
34637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      uint64_t DataSize = Section.getSize();
34737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      uint64_t Alignment64 = Section.getAlignment();
34837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      bool IsCode = Section.isText();
349ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      bool IsReadOnly = isReadOnlyData(Section);
35036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      Check(Section.getName(Name));
35136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
35236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
35336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
35436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      uint64_t SectionSize = DataSize + StubBufSize;
35536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
35636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      // The .eh_frame section (at least on Linux) needs an extra four bytes
35736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      // padded
35836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      // with zeroes added at the end.  For MachO objects, this section has a
35936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      // slightly different name, so this won't have any effect for MachO
36036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      // objects.
36136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      if (Name == ".eh_frame")
36236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        SectionSize += 4;
36336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
3642c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar      if (!SectionSize)
3652c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar        SectionSize = 1;
3662c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar
3672c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar      if (IsCode) {
3682c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar        CodeSectionSizes.push_back(SectionSize);
3692c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar      } else if (IsReadOnly) {
3702c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar        ROSectionSizes.push_back(SectionSize);
3712c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar      } else {
3722c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar        RWSectionSizes.push_back(SectionSize);
3732c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar      }
3742c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar
3752c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar      // update the max alignment
3762c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar      if (Alignment > MaxAlignment) {
3772c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar        MaxAlignment = Alignment;
37836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      }
37936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
38036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
38136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
38236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // Compute the size of all common symbols
38336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  uint64_t CommonSize = 0;
384ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
38536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines       ++I) {
38636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    uint32_t Flags = I->getFlags();
38736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (Flags & SymbolRef::SF_Common) {
38836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      // Add the common symbols to a list.  We'll allocate them all below.
38936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      uint64_t Size = 0;
39036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      Check(I->getSize(Size));
39136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      CommonSize += Size;
39236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
39336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
39436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (CommonSize != 0) {
39536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    RWSectionSizes.push_back(CommonSize);
39636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
39736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
39836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // Compute the required allocation space for each different type of sections
39936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // (code, read-only data, read-write data) assuming that all sections are
40036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // allocated with the max alignment. Note that we cannot compute with the
40136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // individual alignments of the sections, because then the required size
40236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // depends on the order, in which the sections are allocated.
40336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  CodeSize = computeAllocationSizeForSections(CodeSectionSizes, MaxAlignment);
40436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  DataSizeRO = computeAllocationSizeForSections(ROSectionSizes, MaxAlignment);
40536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  DataSizeRW = computeAllocationSizeForSections(RWSectionSizes, MaxAlignment);
40636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
40736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
40836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// compute stub buffer size for the given section
409ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesunsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj,
41036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                                    const SectionRef &Section) {
41136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  unsigned StubSize = getMaxStubSize();
41236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (StubSize == 0) {
41336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    return 0;
41436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
41536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // FIXME: this is an inefficient way to handle this. We should computed the
41636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // necessary section allocation size in loadObject by walking all the sections
41736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // once.
41836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  unsigned StubBufSize = 0;
419ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
42036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines       SI != SE; ++SI) {
42136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    section_iterator RelSecI = SI->getRelocatedSection();
42236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (!(RelSecI == Section))
42336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      continue;
42436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
42536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    for (const RelocationRef &Reloc : SI->relocations()) {
42636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      (void)Reloc;
42736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      StubBufSize += StubSize;
42836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    }
42936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
43036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
43136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // Get section data size and alignment
43237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  uint64_t DataSize = Section.getSize();
43337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  uint64_t Alignment64 = Section.getAlignment();
43436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
43536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  // Add stubbuf size alignment
43636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
43736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  unsigned StubAlignment = getStubAlignment();
43836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
43936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (StubAlignment > EndAlignment)
44036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    StubBufSize += StubAlignment - EndAlignment;
44136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  return StubBufSize;
442020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach}
443020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
44437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesuint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src,
44537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                             unsigned Size) const {
44637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  uint64_t Result = 0;
44737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (IsTargetLittleEndian) {
44837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Src += Size - 1;
44937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    while (Size--)
45037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      Result = (Result << 8) | *Src--;
45137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  } else
45237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    while (Size--)
45337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      Result = (Result << 8) | *Src++;
45437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
45537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  return Result;
45637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
45737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
45837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesvoid RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst,
45937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                          unsigned Size) const {
46037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (IsTargetLittleEndian) {
46137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    while (Size--) {
46237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      *Dst++ = Value & 0xFF;
46337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      Value >>= 8;
46437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    }
46537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  } else {
46637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Dst += Size - 1;
46737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    while (Size--) {
46837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      *Dst-- = Value & 0xFF;
46937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      Value >>= 8;
47037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    }
47137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
47237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
47337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
474ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj,
475ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                                        CommonSymbolList &CommonSymbols) {
476ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  if (CommonSymbols.empty())
477ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return;
478ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
479ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  uint64_t CommonSize = 0;
480ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  CommonSymbolList SymbolsToAllocate;
481ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
482ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  DEBUG(dbgs() << "Processing common symbols...\n");
483ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
484ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  for (const auto &Sym : CommonSymbols) {
485ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    StringRef Name;
486ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    Check(Sym.getName(Name));
487ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
488ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    // Skip common symbols already elsewhere.
489ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (GlobalSymbolTable.count(Name) ||
4902c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar        Resolver.findSymbolInLogicalDylib(Name)) {
491ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      DEBUG(dbgs() << "\tSkipping already emitted common symbol '" << Name
492ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                   << "'\n");
493ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      continue;
494ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    }
495ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
496ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    uint32_t Align = 0;
497ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    uint64_t Size = 0;
498ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    Check(Sym.getAlignment(Align));
499ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    Check(Sym.getSize(Size));
500ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
501ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    CommonSize += Align + Size;
502ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    SymbolsToAllocate.push_back(Sym);
503ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
504ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
505c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  // Allocate memory for the section
506c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  unsigned SectionID = Sections.size();
5072c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar  uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, sizeof(void *),
5082c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                                             SectionID, StringRef(), false);
509c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  if (!Addr)
510c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd    report_fatal_error("Unable to allocate memory for common symbols!");
511c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  uint64_t Offset = 0;
512ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Sections.push_back(SectionEntry("<common symbols>", Addr, CommonSize, 0));
513ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  memset(Addr, 0, CommonSize);
514c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
51536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: "
516ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines               << format("%p", Addr) << " DataSize: " << CommonSize << "\n");
517c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
518c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  // Assign the address of each symbol
519ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  for (auto &Sym : SymbolsToAllocate) {
520ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    uint32_t Align;
521ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    uint64_t Size;
522c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd    StringRef Name;
523ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    Check(Sym.getAlignment(Align));
524ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    Check(Sym.getSize(Size));
525ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    Check(Sym.getName(Name));
526f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover    if (Align) {
527f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover      // This symbol has an alignment requirement.
528f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover      uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
529f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover      Addr += AlignOffset;
530f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover      Offset += AlignOffset;
531f00677d74f1be5b4c7c73e1681a64c9062f4c7eeTim Northover    }
532ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    uint32_t Flags = Sym.getFlags();
5334c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    JITSymbolFlags RTDyldSymFlags = JITSymbolFlags::None;
5344c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    if (Flags & SymbolRef::SF_Weak)
5354c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar      RTDyldSymFlags |= JITSymbolFlags::Weak;
5364c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    if (Flags & SymbolRef::SF_Exported)
5374c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar      RTDyldSymFlags |= JITSymbolFlags::Exported;
538ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
539ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                 << format("%p", Addr) << "\n");
5404c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    GlobalSymbolTable[Name] =
5414c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar      SymbolTableEntry(SectionID, Offset, RTDyldSymFlags);
542c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd    Offset += Size;
543c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd    Addr += Size;
544c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  }
545c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd}
546c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
547ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesunsigned RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
54836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                      const SectionRef &Section, bool IsCode) {
5497486d92a6c949a193bb75c0ffa0170eeb2fabb80Rafael Espindola
5500e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  StringRef data;
55137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  uint64_t Alignment64 = Section.getAlignment();
5520e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
5530e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
55428a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor  unsigned PaddingSize = 0;
55536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  unsigned StubBufSize = 0;
5566e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  StringRef Name;
557ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  bool IsRequired = isRequiredForExecution(Section);
55837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  bool IsVirtual = Section.isVirtual();
559ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  bool IsZeroInit = isZeroInit(Section);
560ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  bool IsReadOnly = isReadOnlyData(Section);
56137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  uint64_t DataSize = Section.getSize();
5626e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella  Check(Section.getName(Name));
56336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
56436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  StubBufSize = computeSectionStubBufSize(Obj, Section);
565c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
56628a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor  // The .eh_frame section (at least on Linux) needs an extra four bytes padded
56728a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor  // with zeroes added at the end.  For MachO objects, this section has a
56828a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor  // slightly different name, so this won't have any effect for MachO objects.
56928a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor  if (Name == ".eh_frame")
57028a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor    PaddingSize = 4;
57128a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor
57236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  uintptr_t Allocate;
5730e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  unsigned SectionID = Sections.size();
574c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  uint8_t *Addr;
575dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  const char *pData = nullptr;
576c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
577c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  // Some sections, such as debug info, don't need to be loaded for execution.
578c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  // Leave those where they are.
579c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  if (IsRequired) {
5804c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    Check(Section.getContents(data));
58128a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor    Allocate = DataSize + PaddingSize + StubBufSize;
5822c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar    if (!Allocate)
5832c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar      Allocate = 1;
5842c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar    Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID,
5852c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                                               Name)
5862c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                  : MemMgr.allocateDataSection(Allocate, Alignment, SectionID,
5872c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                                               Name, IsReadOnly);
588c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd    if (!Addr)
589c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd      report_fatal_error("Unable to allocate section memory!");
590c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
591c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd    // Virtual sections have no data in the object image, so leave pData = 0
592c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd    if (!IsVirtual)
593c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd      pData = data.data();
594c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
595c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd    // Zero-initialize or copy the data from the image
596c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd    if (IsZeroInit || IsVirtual)
597c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd      memset(Addr, 0, DataSize);
598c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd    else
599c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd      memcpy(Addr, pData, DataSize);
600c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
60128a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor    // Fill in any extra bytes we allocated for padding
60228a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor    if (PaddingSize != 0) {
60328a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor      memset(Addr + DataSize, 0, PaddingSize);
60428a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor      // Update the DataSize variable so that the stub offset is set correctly.
60528a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor      DataSize += PaddingSize;
60628a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor    }
60728a765542c7a57821fc4c06454c1326e2f66a1d9Andrew Kaylor
60836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
609c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd                 << " obj addr: " << format("%p", pData)
610c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd                 << " new addr: " << format("%p", Addr)
61136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
61236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                 << " Allocate: " << Allocate << "\n");
61336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  } else {
614c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd    // Even if we didn't load the section, we need to record an entry for it
6155fe019835c269ccbfe185276269bc53b3f9a7a86Eli Bendersky    // to handle later processing (and by 'handle' I mean don't do anything
6165fe019835c269ccbfe185276269bc53b3f9a7a86Eli Bendersky    // with these sections).
617c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd    Allocate = 0;
618dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Addr = nullptr;
61936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
62036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                 << " obj addr: " << format("%p", data.data()) << " new addr: 0"
62136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
62236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                 << " Allocate: " << Allocate << "\n");
623c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd  }
624c68dda815e64fb2fb463318d1eaa304e22199d50Preston Gurd
625a2e40fbd624916c187a95ed76939ca7f02ed3e53Rafael Espindola  Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
62637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
62737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (Checker)
628ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    Checker->registerSection(Obj.getFileName(), SectionID);
62937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
6300e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  return SectionID;
6310e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev}
6320e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
633ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesunsigned RuntimeDyldImpl::findOrEmitSection(const ObjectFile &Obj,
634689ff9c00f4f3dcf3491778bcdbda79e19e2285dPreston Gurd                                            const SectionRef &Section,
6350e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev                                            bool IsCode,
6360e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev                                            ObjSectionToIDMap &LocalSections) {
6370e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
6380e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  unsigned SectionID = 0;
6390e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  ObjSectionToIDMap::iterator i = LocalSections.find(Section);
6400e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  if (i != LocalSections.end())
6410e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev    SectionID = i->second;
6420e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  else {
643689ff9c00f4f3dcf3491778bcdbda79e19e2285dPreston Gurd    SectionID = emitSection(Obj, Section, IsCode);
6440e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev    LocalSections[Section] = SectionID;
6450e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  }
6460e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  return SectionID;
6470e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev}
6480e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
649c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Benderskyvoid RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
650c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky                                              unsigned SectionID) {
651c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  Relocations[SectionID].push_back(RE);
652c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky}
6530e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
654c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Benderskyvoid RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
655c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky                                             StringRef SymbolName) {
656c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  // Relocation by symbol.  If the symbol is found in the global symbol table,
657c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  // create an appropriate section relocation.  Otherwise, add it to
658c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  // ExternalSymbolRelocations.
659ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
660c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky  if (Loc == GlobalSymbolTable.end()) {
661c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky    ExternalSymbolRelocations[SymbolName].push_back(RE);
66237bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  } else {
663c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky    // Copy the RE since we want to modify its addend.
664c201e6eaf165c83f0092c43b371e509fa8eaf4ccEli Bendersky    RelocationEntry RECopy = RE;
665ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    const auto &SymInfo = Loc->second;
666ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    RECopy.Addend += SymInfo.getOffset();
667ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    Relocations[SymInfo.getSectionID()].push_back(RECopy);
66837bc5a200092f1b41da799c75da70258015e43a4Eli Bendersky  }
6690e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev}
6700e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
67137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesuint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
67237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                             unsigned AbiVariant) {
67337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) {
6744a9b6b798d5ea335f5f29d278db83b830ae4a564Tim Northover    // This stub has to be able to access the full address space,
6754a9b6b798d5ea335f5f29d278db83b830ae4a564Tim Northover    // since symbol lookup won't necessarily find a handy, in-range,
6764a9b6b798d5ea335f5f29d278db83b830ae4a564Tim Northover    // PLT stub for functions which could be anywhere.
6774a9b6b798d5ea335f5f29d278db83b830ae4a564Tim Northover    // Stub can use ip0 (== x16) to calculate address
67837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    writeBytesUnaligned(0xd2e00010, Addr,    4); // movz ip0, #:abs_g3:<addr>
67937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    writeBytesUnaligned(0xf2c00010, Addr+4,  4); // movk ip0, #:abs_g2_nc:<addr>
68037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    writeBytesUnaligned(0xf2a00010, Addr+8,  4); // movk ip0, #:abs_g1_nc:<addr>
68137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    writeBytesUnaligned(0xf2800010, Addr+12, 4); // movk ip0, #:abs_g0_nc:<addr>
68237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    writeBytesUnaligned(0xd61f0200, Addr+16, 4); // br ip0
6834a9b6b798d5ea335f5f29d278db83b830ae4a564Tim Northover
6844a9b6b798d5ea335f5f29d278db83b830ae4a564Tim Northover    return Addr;
68536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  } else if (Arch == Triple::arm || Arch == Triple::armeb) {
686b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka    // TODO: There is only ARM far stub now. We should add the Thumb stub,
687b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka    // and stubs for branches Thumb - ARM and ARM - Thumb.
68837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc,<label>
68937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return Addr + 4;
6904af1ca5229a149c1d8e5e89bc2f13209da33221cNAKAMURA Takumi  } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
691b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka    // 0:   3c190000        lui     t9,%hi(addr).
692b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka    // 4:   27390000        addiu   t9,t9,%lo(addr).
693b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka    // 8:   03200008        jr      t9.
694b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka    // c:   00000000        nop.
695b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka    const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
696b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka    const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
697b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka
69837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    writeBytesUnaligned(LuiT9Instr, Addr, 4);
69937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    writeBytesUnaligned(AdduiT9Instr, Addr+4, 4);
70037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    writeBytesUnaligned(JrT9Instr, Addr+8, 4);
70137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    writeBytesUnaligned(NopInstr, Addr+12, 4);
7020e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev    return Addr;
703f38cc38fa647d4e72c053c39bbe0cdec1342535fBill Schmidt  } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
70437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // Depending on which version of the ELF ABI is in use, we need to
70537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // generate one of two variants of the stub.  They both start with
70637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    // the same sequence to load the target address into r12.
7076e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    writeInt32BE(Addr,    0x3D800000); // lis   r12, highest(addr)
7086e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    writeInt32BE(Addr+4,  0x618C0000); // ori   r12, higher(addr)
7096e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    writeInt32BE(Addr+8,  0x798C07C6); // sldi  r12, r12, 32
7106e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    writeInt32BE(Addr+12, 0x658C0000); // oris  r12, r12, h(addr)
7116e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    writeInt32BE(Addr+16, 0x618C0000); // ori   r12, r12, l(addr)
71237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    if (AbiVariant == 2) {
71337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
71437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      // The address is already in r12 as required by the ABI.  Branch to it.
71537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      writeInt32BE(Addr+20, 0xF8410018); // std   r2,  24(r1)
71637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12
71737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      writeInt32BE(Addr+28, 0x4E800420); // bctr
71837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    } else {
71937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
72037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      // Load the function address on r11 and sets it to control register. Also
72137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      // loads the function TOC in r2 and environment pointer to r11.
72237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      writeInt32BE(Addr+20, 0xF8410028); // std   r2,  40(r1)
72337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      writeInt32BE(Addr+24, 0xE96C0000); // ld    r11, 0(r12)
72437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      writeInt32BE(Addr+28, 0xE84C0008); // ld    r2,  0(r12)
72537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
72637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      writeInt32BE(Addr+36, 0xE96C0010); // ld    r11, 16(r2)
72737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      writeInt32BE(Addr+40, 0x4E800420); // bctr
72837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    }
7296e8946c5280ad6071d21013651745a9478f063edAdhemerval Zanella    return Addr;
7306fc2ad62e23673ef829c9c4bbf62743d30928a5bRichard Sandiford  } else if (Arch == Triple::systemz) {
7316fc2ad62e23673ef829c9c4bbf62743d30928a5bRichard Sandiford    writeInt16BE(Addr,    0xC418);     // lgrl %r1,.+8
7326fc2ad62e23673ef829c9c4bbf62743d30928a5bRichard Sandiford    writeInt16BE(Addr+2,  0x0000);
7336fc2ad62e23673ef829c9c4bbf62743d30928a5bRichard Sandiford    writeInt16BE(Addr+4,  0x0004);
7346fc2ad62e23673ef829c9c4bbf62743d30928a5bRichard Sandiford    writeInt16BE(Addr+6,  0x07F1);     // brc 15,%r1
7356fc2ad62e23673ef829c9c4bbf62743d30928a5bRichard Sandiford    // 8-byte address stored at Addr + 8
7366fc2ad62e23673ef829c9c4bbf62743d30928a5bRichard Sandiford    return Addr;
737ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor  } else if (Arch == Triple::x86_64) {
738ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor    *Addr      = 0xFF; // jmp
739ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor    *(Addr+1)  = 0x25; // rip
740ff9fa05905be716435460b5f6d32689041bf895bAndrew Kaylor    // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
741dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  } else if (Arch == Triple::x86) {
742dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    *Addr      = 0xE9; // 32-bit pc-relative jump.
743b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka  }
744b889e0cd2fea4afee623d5be603b912b955a2ecaAkira Hatanaka  return Addr;
7450e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev}
7460e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
7470e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev// Assign an address to a symbol name and resolve all the relocations
7480e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev// associated with it.
7490e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevvoid RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
7500e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev                                             uint64_t Addr) {
7510e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // The address to use for relocation resolution is not
7520e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // the address of the local section buffer. We must be doing
75328989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor  // a remote execution environment of some sort. Relocations can't
75428989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor  // be applied until all the sections have been moved.  The client must
75528989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor  // trigger this with a call to MCJIT::finalize() or
75628989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor  // RuntimeDyld::resolveRelocations().
7570e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  //
7580e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // Addr is a uint64_t because we can't assume the pointer width
7590e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // of the target is the same as that of the host. Just use a generic
7600e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  // "big enough" type.
76137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  DEBUG(dbgs() << "Reassigning address for section "
76237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines               << SectionID << " (" << Sections[SectionID].Name << "): "
76337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines               << format("0x%016" PRIx64, Sections[SectionID].LoadAddress) << " -> "
76437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines               << format("0x%016" PRIx64, Addr) << "\n");
7650e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  Sections[SectionID].LoadAddress = Addr;
7660e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev}
7670e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
7680e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshevvoid RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
7690e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev                                            uint64_t Value) {
7700e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
77187b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola    const RelocationEntry &RE = Relocs[i];
77287b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola    // Ignore relocations for sections that were not loaded
773dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (Sections[RE.SectionID].Address == nullptr)
77487b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola      continue;
77587b5017139e9d8ac9b046b3284a9cc68c76185d6Rafael Espindola    resolveRelocation(RE, Value);
7760e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  }
7770e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev}
7780e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
77937bc5a200092f1b41da799c75da70258015e43a4Eli Benderskyvoid RuntimeDyldImpl::resolveExternalSymbols() {
78036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  while (!ExternalSymbolRelocations.empty()) {
7818e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor    StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
7828e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor
7830e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev    StringRef Name = i->first();
7848e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor    if (Name.size() == 0) {
7858e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor      // This is an absolute symbol, use an address of zero.
78636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      DEBUG(dbgs() << "Resolving absolute relocations."
78736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                   << "\n");
788559d409633ce22574dcab56d4f600b6eb1304652Andrew Kaylor      RelocationList &Relocs = i->second;
7898e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor      resolveRelocationList(Relocs, 0);
7908e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor    } else {
7918e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor      uint64_t Addr = 0;
792ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name);
7938e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor      if (Loc == GlobalSymbolTable.end()) {
7942c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar        // This is an external symbol, try to get its address from the symbol
7952c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar        // resolver.
7962c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar        Addr = Resolver.findSymbol(Name.data()).getAddress();
79736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        // The call to getSymbolAddress may have caused additional modules to
79836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        // be loaded, which may have added new entries to the
79936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        // ExternalSymbolRelocations map.  Consquently, we need to update our
80036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        // iterator.  This is also why retrieval of the relocation list
80136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        // associated with this symbol is deferred until below this point.
80236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        // New entries may have been added to the relocation list.
80336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        i = ExternalSymbolRelocations.find(Name);
80429fe150bff0f167e85e1b44efe344bf28cb7fe0fAndrew Kaylor      } else {
8058e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor        // We found the symbol in our global table.  It was probably in a
8068e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor        // Module that we loaded previously.
807ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        const auto &SymInfo = Loc->second;
808ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        Addr = getSectionLoadAddress(SymInfo.getSectionID()) +
809ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines               SymInfo.getOffset();
8107b170500dcfce130c1e5af1c9150014e69e56819Andrew Kaylor      }
8118e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor
8128e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor      // FIXME: Implement error handling that doesn't kill the host program!
8138e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor      if (!Addr)
8148e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor        report_fatal_error("Program used external function '" + Name +
81536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                           "' which could not be resolved!");
8168e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor
81736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
81836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                   << format("0x%lx", Addr) << "\n");
819559d409633ce22574dcab56d4f600b6eb1304652Andrew Kaylor      // This list may have been updated when we called getSymbolAddress, so
820559d409633ce22574dcab56d4f600b6eb1304652Andrew Kaylor      // don't change this code to get the list earlier.
821559d409633ce22574dcab56d4f600b6eb1304652Andrew Kaylor      RelocationList &Relocs = i->second;
8228e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor      resolveRelocationList(Relocs, Addr);
8230e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev    }
8248e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor
825559d409633ce22574dcab56d4f600b6eb1304652Andrew Kaylor    ExternalSymbolRelocations.erase(i);
8260e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev  }
8270e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev}
8280e4fa5ff365fccff46870b7d5d8d4d1d46e77986Danil Malyshev
8296e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//===----------------------------------------------------------------------===//
8306e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach// RuntimeDyld class implementation
831ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
832ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesuint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress(
833ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                                                  StringRef SectionName) const {
834ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  for (unsigned I = BeginIdx; I != EndIdx; ++I)
835ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (RTDyld.Sections[I].Name == SectionName)
836ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      return RTDyld.Sections[I].LoadAddress;
837ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
838ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return 0;
839ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
840ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
8412c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainarvoid RuntimeDyld::MemoryManager::anchor() {}
8422c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainarvoid RuntimeDyld::SymbolResolver::anchor() {}
8432c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar
8442c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga NainarRuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr,
8452c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                         RuntimeDyld::SymbolResolver &Resolver)
8462c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar    : MemMgr(MemMgr), Resolver(Resolver) {
84753608a34ce3f0969e9fb01eaa983422761011e03Andrew Kaylor  // FIXME: There's a potential issue lurking here if a single instance of
84853608a34ce3f0969e9fb01eaa983422761011e03Andrew Kaylor  // RuntimeDyld is used to load multiple objects.  The current implementation
84953608a34ce3f0969e9fb01eaa983422761011e03Andrew Kaylor  // associates a single memory manager with a RuntimeDyld instance.  Even
85053608a34ce3f0969e9fb01eaa983422761011e03Andrew Kaylor  // though the public class spawns a new 'impl' instance for each load,
85153608a34ce3f0969e9fb01eaa983422761011e03Andrew Kaylor  // they share a single memory manager.  This can become a problem when page
85253608a34ce3f0969e9fb01eaa983422761011e03Andrew Kaylor  // permissions are applied.
853dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Dyld = nullptr;
85436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ProcessAllSections = false;
85537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Checker = nullptr;
85636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
85736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
85837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesRuntimeDyld::~RuntimeDyld() {}
85936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
8604c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainarstatic std::unique_ptr<RuntimeDyldCOFF>
8612c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga NainarcreateRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
8622c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                      RuntimeDyld::SymbolResolver &Resolver,
8634c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                      bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
8642c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar  std::unique_ptr<RuntimeDyldCOFF> Dyld =
8652c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar    RuntimeDyldCOFF::create(Arch, MM, Resolver);
8664c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  Dyld->setProcessAllSections(ProcessAllSections);
8674c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  Dyld->setRuntimeDyldChecker(Checker);
8684c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  return Dyld;
8694c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar}
8704c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar
87136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic std::unique_ptr<RuntimeDyldELF>
8722c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga NainarcreateRuntimeDyldELF(RuntimeDyld::MemoryManager &MM,
8732c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                     RuntimeDyld::SymbolResolver &Resolver,
8742c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                     bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
8752c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar  std::unique_ptr<RuntimeDyldELF> Dyld(new RuntimeDyldELF(MM, Resolver));
87636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Dyld->setProcessAllSections(ProcessAllSections);
87737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Dyld->setRuntimeDyldChecker(Checker);
87836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  return Dyld;
8796e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach}
8806e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
88136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic std::unique_ptr<RuntimeDyldMachO>
8822c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga NainarcreateRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
8832c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                       RuntimeDyld::SymbolResolver &Resolver,
8842c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                       bool ProcessAllSections,
8852c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar                       RuntimeDyldCheckerImpl *Checker) {
8862c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar  std::unique_ptr<RuntimeDyldMachO> Dyld =
8872c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar    RuntimeDyldMachO::create(Arch, MM, Resolver);
88836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Dyld->setProcessAllSections(ProcessAllSections);
88937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Dyld->setRuntimeDyldChecker(Checker);
89036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  return Dyld;
89136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
89236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
893ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstd::unique_ptr<RuntimeDyld::LoadedObjectInfo>
894ebe69fe11e48d322045d5949c83283927a0d790bStephen HinesRuntimeDyld::loadObject(const ObjectFile &Obj) {
895ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  if (!Dyld) {
896ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (Obj.isELF())
8972c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar      Dyld = createRuntimeDyldELF(MemMgr, Resolver, ProcessAllSections, Checker);
898ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    else if (Obj.isMachO())
89937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      Dyld = createRuntimeDyldMachO(
9002c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar               static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
901ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines               ProcessAllSections, Checker);
9024c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    else if (Obj.isCOFF())
9034c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar      Dyld = createRuntimeDyldCOFF(
9042c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar               static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
9054c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar               ProcessAllSections, Checker);
906ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    else
907ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      report_fatal_error("Incompatible object format!");
908cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev  }
909cf852dc49bfadb79a27455f8773dbfda2b96f4f6Danil Malyshev
910ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  if (!Dyld->isCompatibleFile(Obj))
91136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    report_fatal_error("Incompatible object format!");
91236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
913ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return Dyld->loadObject(Obj);
9146e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach}
9156e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
9164c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainarvoid *RuntimeDyld::getSymbolLocalAddress(StringRef Name) const {
9178e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  if (!Dyld)
918dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return nullptr;
9194c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  return Dyld->getSymbolLocalAddress(Name);
9206e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach}
9216e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
9224c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga NainarRuntimeDyld::SymbolInfo RuntimeDyld::getSymbol(StringRef Name) const {
9238e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  if (!Dyld)
9244c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    return nullptr;
9254c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  return Dyld->getSymbol(Name);
926ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
927ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
92836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvoid RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
929f8c1c8465ff097ad5b87331b6d9a2576167f1402Jim Grosbach
93036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvoid RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
93161425c0a7f4e3608a85f7bbf254cd052a15b7446Jim Grosbach  Dyld->reassignSectionAddress(SectionID, Addr);
932f8c1c8465ff097ad5b87331b6d9a2576167f1402Jim Grosbach}
933f8c1c8465ff097ad5b87331b6d9a2576167f1402Jim Grosbach
934e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbachvoid RuntimeDyld::mapSectionAddress(const void *LocalAddress,
935020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach                                    uint64_t TargetAddress) {
936020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  Dyld->mapSectionAddress(LocalAddress, TargetAddress);
937020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach}
938020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
93936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesbool RuntimeDyld::hasError() { return Dyld->hasError(); }
94036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
94136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesStringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
942b3eecaf19e81f0cccffdeff940afbfd1a3754af2Jim Grosbach
943528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylorvoid RuntimeDyld::registerEHFrames() {
94443507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor  if (Dyld)
94543507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor    Dyld->registerEHFrames();
94643507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor}
94743507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor
94843507d026bef31100cb0c35614bcf419029a265bAndrew Kaylorvoid RuntimeDyld::deregisterEHFrames() {
94943507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor  if (Dyld)
95043507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor    Dyld->deregisterEHFrames();
951a2e40fbd624916c187a95ed76939ca7f02ed3e53Rafael Espindola}
952a2e40fbd624916c187a95ed76939ca7f02ed3e53Rafael Espindola
9536e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach} // end namespace llvm
954