1283625Sdim//===-- RuntimeDyldCOFFX86_64.h --- COFF/X86_64 specific code ---*- C++ -*-===//
2283625Sdim//
3283625Sdim//                     The LLVM Compiler Infrastructure
4283625Sdim//
5283625Sdim// This file is distributed under the University of Illinois Open Source
6283625Sdim// License. See LICENSE.TXT for details.
7283625Sdim//
8283625Sdim//===----------------------------------------------------------------------===//
9283625Sdim//
10283625Sdim// COFF x86_x64 support for MC-JIT runtime dynamic linker.
11283625Sdim//
12283625Sdim//===----------------------------------------------------------------------===//
13283625Sdim
14283625Sdim#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFF86_64_H
15283625Sdim#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFF86_64_H
16283625Sdim
17283625Sdim#include "llvm/Object/COFF.h"
18283625Sdim#include "llvm/Support/COFF.h"
19283625Sdim#include "../RuntimeDyldCOFF.h"
20283625Sdim
21283625Sdim#define DEBUG_TYPE "dyld"
22283625Sdim
23283625Sdimnamespace llvm {
24283625Sdim
25283625Sdimclass RuntimeDyldCOFFX86_64 : public RuntimeDyldCOFF {
26283625Sdim
27283625Sdimprivate:
28283625Sdim  // When a module is loaded we save the SectionID of the unwind
29283625Sdim  // sections in a table until we receive a request to register all
30283625Sdim  // unregisteredEH frame sections with the memory manager.
31283625Sdim  SmallVector<SID, 2> UnregisteredEHFrameSections;
32283625Sdim  SmallVector<SID, 2> RegisteredEHFrameSections;
33283625Sdim
34283625Sdimpublic:
35283625Sdim  RuntimeDyldCOFFX86_64(RuntimeDyld::MemoryManager &MM,
36283625Sdim                        RuntimeDyld::SymbolResolver &Resolver)
37283625Sdim    : RuntimeDyldCOFF(MM, Resolver) {}
38283625Sdim
39283625Sdim  unsigned getMaxStubSize() override {
40283625Sdim    return 6; // 2-byte jmp instruction + 32-bit relative address
41283625Sdim  }
42283625Sdim
43283625Sdim  // The target location for the relocation is described by RE.SectionID and
44283625Sdim  // RE.Offset.  RE.SectionID can be used to find the SectionEntry.  Each
45283625Sdim  // SectionEntry has three members describing its location.
46283625Sdim  // SectionEntry::Address is the address at which the section has been loaded
47283625Sdim  // into memory in the current (host) process.  SectionEntry::LoadAddress is
48283625Sdim  // the address that the section will have in the target process.
49283625Sdim  // SectionEntry::ObjAddress is the address of the bits for this section in the
50283625Sdim  // original emitted object image (also in the current address space).
51283625Sdim  //
52283625Sdim  // Relocations will be applied as if the section were loaded at
53283625Sdim  // SectionEntry::LoadAddress, but they will be applied at an address based
54283625Sdim  // on SectionEntry::Address.  SectionEntry::ObjAddress will be used to refer
55283625Sdim  // to Target memory contents if they are required for value calculations.
56283625Sdim  //
57283625Sdim  // The Value parameter here is the load address of the symbol for the
58283625Sdim  // relocation to be applied.  For relocations which refer to symbols in the
59283625Sdim  // current object Value will be the LoadAddress of the section in which
60283625Sdim  // the symbol resides (RE.Addend provides additional information about the
61283625Sdim  // symbol location).  For external symbols, Value will be the address of the
62283625Sdim  // symbol in the target address space.
63283625Sdim  void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
64283625Sdim    const SectionEntry &Section = Sections[RE.SectionID];
65296417Sdim    uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
66283625Sdim
67283625Sdim    switch (RE.RelType) {
68283625Sdim
69283625Sdim    case COFF::IMAGE_REL_AMD64_REL32:
70283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_1:
71283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_2:
72283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_3:
73283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_4:
74283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_5: {
75296417Sdim      uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
76283625Sdim      // Delta is the distance from the start of the reloc to the end of the
77283625Sdim      // instruction with the reloc.
78283625Sdim      uint64_t Delta = 4 + (RE.RelType - COFF::IMAGE_REL_AMD64_REL32);
79283625Sdim      Value -= FinalAddress + Delta;
80283625Sdim      uint64_t Result = Value + RE.Addend;
81283625Sdim      assert(((int64_t)Result <= INT32_MAX) && "Relocation overflow");
82283625Sdim      assert(((int64_t)Result >= INT32_MIN) && "Relocation underflow");
83296417Sdim      writeBytesUnaligned(Result, Target, 4);
84283625Sdim      break;
85283625Sdim    }
86283625Sdim
87283625Sdim    case COFF::IMAGE_REL_AMD64_ADDR32NB: {
88283625Sdim      // Note ADDR32NB requires a well-established notion of
89283625Sdim      // image base. This address must be less than or equal
90283625Sdim      // to every section's load address, and all sections must be
91283625Sdim      // within a 32 bit offset from the base.
92283625Sdim      //
93283625Sdim      // For now we just set these to zero.
94296417Sdim      writeBytesUnaligned(0, Target, 4);
95283625Sdim      break;
96283625Sdim    }
97283625Sdim
98283625Sdim    case COFF::IMAGE_REL_AMD64_ADDR64: {
99296417Sdim      writeBytesUnaligned(Value + RE.Addend, Target, 8);
100283625Sdim      break;
101283625Sdim    }
102283625Sdim
103283625Sdim    default:
104283625Sdim      llvm_unreachable("Relocation type not implemented yet!");
105283625Sdim      break;
106283625Sdim    }
107283625Sdim  }
108283625Sdim
109283625Sdim  relocation_iterator processRelocationRef(unsigned SectionID,
110283625Sdim                                           relocation_iterator RelI,
111283625Sdim                                           const ObjectFile &Obj,
112283625Sdim                                           ObjSectionToIDMap &ObjSectionToID,
113283625Sdim                                           StubMap &Stubs) override {
114283625Sdim    // If possible, find the symbol referred to in the relocation,
115283625Sdim    // and the section that contains it.
116283625Sdim    symbol_iterator Symbol = RelI->getSymbol();
117283625Sdim    if (Symbol == Obj.symbol_end())
118283625Sdim      report_fatal_error("Unknown symbol in relocation");
119296417Sdim    section_iterator SecI = *Symbol->getSection();
120283625Sdim    // If there is no section, this must be an external reference.
121283625Sdim    const bool IsExtern = SecI == Obj.section_end();
122283625Sdim
123283625Sdim    // Determine the Addend used to adjust the relocation value.
124285181Sdim    uint64_t RelType = RelI->getType();
125285181Sdim    uint64_t Offset = RelI->getOffset();
126283625Sdim    uint64_t Addend = 0;
127283625Sdim    SectionEntry &Section = Sections[SectionID];
128296417Sdim    uintptr_t ObjTarget = Section.getObjAddress() + Offset;
129283625Sdim
130283625Sdim    switch (RelType) {
131283625Sdim
132283625Sdim    case COFF::IMAGE_REL_AMD64_REL32:
133283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_1:
134283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_2:
135283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_3:
136283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_4:
137283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_5:
138283625Sdim    case COFF::IMAGE_REL_AMD64_ADDR32NB: {
139296417Sdim      uint8_t *Displacement = (uint8_t *)ObjTarget;
140296417Sdim      Addend = readBytesUnaligned(Displacement, 4);
141283625Sdim      break;
142283625Sdim    }
143283625Sdim
144283625Sdim    case COFF::IMAGE_REL_AMD64_ADDR64: {
145296417Sdim      uint8_t *Displacement = (uint8_t *)ObjTarget;
146296417Sdim      Addend = readBytesUnaligned(Displacement, 8);
147283625Sdim      break;
148283625Sdim    }
149283625Sdim
150283625Sdim    default:
151283625Sdim      break;
152283625Sdim    }
153283625Sdim
154285181Sdim    ErrorOr<StringRef> TargetNameOrErr = Symbol->getName();
155285181Sdim    if (std::error_code EC = TargetNameOrErr.getError())
156285181Sdim      report_fatal_error(EC.message());
157285181Sdim    StringRef TargetName = *TargetNameOrErr;
158283625Sdim
159283625Sdim    DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
160283625Sdim                 << " RelType: " << RelType << " TargetName: " << TargetName
161283625Sdim                 << " Addend " << Addend << "\n");
162283625Sdim
163283625Sdim    if (IsExtern) {
164283625Sdim      RelocationEntry RE(SectionID, Offset, RelType, Addend);
165283625Sdim      addRelocationForSymbol(RE, TargetName);
166283625Sdim    } else {
167283625Sdim      bool IsCode = SecI->isText();
168283625Sdim      unsigned TargetSectionID =
169283625Sdim          findOrEmitSection(Obj, *SecI, IsCode, ObjSectionToID);
170283625Sdim      uint64_t TargetOffset = getSymbolOffset(*Symbol);
171283625Sdim      RelocationEntry RE(SectionID, Offset, RelType, TargetOffset + Addend);
172283625Sdim      addRelocationForSection(RE, TargetSectionID);
173283625Sdim    }
174283625Sdim
175283625Sdim    return ++RelI;
176283625Sdim  }
177283625Sdim
178283625Sdim  unsigned getStubAlignment() override { return 1; }
179283625Sdim  void registerEHFrames() override {
180283625Sdim    for (auto const &EHFrameSID : UnregisteredEHFrameSections) {
181296417Sdim      uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
182296417Sdim      uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
183296417Sdim      size_t EHFrameSize = Sections[EHFrameSID].getSize();
184283625Sdim      MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
185283625Sdim      RegisteredEHFrameSections.push_back(EHFrameSID);
186283625Sdim    }
187283625Sdim    UnregisteredEHFrameSections.clear();
188283625Sdim  }
189283625Sdim  void deregisterEHFrames() override {
190283625Sdim    // Stub
191283625Sdim  }
192283625Sdim  void finalizeLoad(const ObjectFile &Obj,
193283625Sdim                    ObjSectionToIDMap &SectionMap) override {
194283625Sdim    // Look for and record the EH frame section IDs.
195283625Sdim    for (const auto &SectionPair : SectionMap) {
196283625Sdim      const SectionRef &Section = SectionPair.first;
197283625Sdim      StringRef Name;
198283625Sdim      Check(Section.getName(Name));
199283625Sdim      // Note unwind info is split across .pdata and .xdata, so this
200283625Sdim      // may not be sufficiently general for all users.
201283625Sdim      if (Name == ".xdata") {
202283625Sdim        UnregisteredEHFrameSections.push_back(SectionPair.second);
203283625Sdim      }
204283625Sdim    }
205283625Sdim  }
206283625Sdim};
207283625Sdim
208283625Sdim} // end namespace llvm
209283625Sdim
210283625Sdim#undef DEBUG_TYPE
211283625Sdim
212283625Sdim#endif
213