RuntimeDyldCOFFX86_64.h revision 283625
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];
65283625Sdim    uint8_t *Target = Section.Address + 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: {
75283625Sdim      uint32_t *TargetAddress = (uint32_t *)Target;
76283625Sdim      uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
77283625Sdim      // Delta is the distance from the start of the reloc to the end of the
78283625Sdim      // instruction with the reloc.
79283625Sdim      uint64_t Delta = 4 + (RE.RelType - COFF::IMAGE_REL_AMD64_REL32);
80283625Sdim      Value -= FinalAddress + Delta;
81283625Sdim      uint64_t Result = Value + RE.Addend;
82283625Sdim      assert(((int64_t)Result <= INT32_MAX) && "Relocation overflow");
83283625Sdim      assert(((int64_t)Result >= INT32_MIN) && "Relocation underflow");
84283625Sdim      *TargetAddress = Result;
85283625Sdim      break;
86283625Sdim    }
87283625Sdim
88283625Sdim    case COFF::IMAGE_REL_AMD64_ADDR32NB: {
89283625Sdim      // Note ADDR32NB requires a well-established notion of
90283625Sdim      // image base. This address must be less than or equal
91283625Sdim      // to every section's load address, and all sections must be
92283625Sdim      // within a 32 bit offset from the base.
93283625Sdim      //
94283625Sdim      // For now we just set these to zero.
95283625Sdim      uint32_t *TargetAddress = (uint32_t *)Target;
96283625Sdim      *TargetAddress = 0;
97283625Sdim      break;
98283625Sdim    }
99283625Sdim
100283625Sdim    case COFF::IMAGE_REL_AMD64_ADDR64: {
101283625Sdim      uint64_t *TargetAddress = (uint64_t *)Target;
102283625Sdim      *TargetAddress = Value + RE.Addend;
103283625Sdim      break;
104283625Sdim    }
105283625Sdim
106283625Sdim    default:
107283625Sdim      llvm_unreachable("Relocation type not implemented yet!");
108283625Sdim      break;
109283625Sdim    }
110283625Sdim  }
111283625Sdim
112283625Sdim  relocation_iterator processRelocationRef(unsigned SectionID,
113283625Sdim                                           relocation_iterator RelI,
114283625Sdim                                           const ObjectFile &Obj,
115283625Sdim                                           ObjSectionToIDMap &ObjSectionToID,
116283625Sdim                                           StubMap &Stubs) override {
117283625Sdim    // If possible, find the symbol referred to in the relocation,
118283625Sdim    // and the section that contains it.
119283625Sdim    symbol_iterator Symbol = RelI->getSymbol();
120283625Sdim    if (Symbol == Obj.symbol_end())
121283625Sdim      report_fatal_error("Unknown symbol in relocation");
122283625Sdim    section_iterator SecI(Obj.section_end());
123283625Sdim    Symbol->getSection(SecI);
124283625Sdim    // If there is no section, this must be an external reference.
125283625Sdim    const bool IsExtern = SecI == Obj.section_end();
126283625Sdim
127283625Sdim    // Determine the Addend used to adjust the relocation value.
128283625Sdim    uint64_t RelType;
129283625Sdim    Check(RelI->getType(RelType));
130283625Sdim    uint64_t Offset;
131283625Sdim    Check(RelI->getOffset(Offset));
132283625Sdim    uint64_t Addend = 0;
133283625Sdim    SectionEntry &Section = Sections[SectionID];
134283625Sdim    uintptr_t ObjTarget = Section.ObjAddress + Offset;
135283625Sdim
136283625Sdim    switch (RelType) {
137283625Sdim
138283625Sdim    case COFF::IMAGE_REL_AMD64_REL32:
139283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_1:
140283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_2:
141283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_3:
142283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_4:
143283625Sdim    case COFF::IMAGE_REL_AMD64_REL32_5:
144283625Sdim    case COFF::IMAGE_REL_AMD64_ADDR32NB: {
145283625Sdim      uint32_t *Displacement = (uint32_t *)ObjTarget;
146283625Sdim      Addend = *Displacement;
147283625Sdim      break;
148283625Sdim    }
149283625Sdim
150283625Sdim    case COFF::IMAGE_REL_AMD64_ADDR64: {
151283625Sdim      uint64_t *Displacement = (uint64_t *)ObjTarget;
152283625Sdim      Addend = *Displacement;
153283625Sdim      break;
154283625Sdim    }
155283625Sdim
156283625Sdim    default:
157283625Sdim      break;
158283625Sdim    }
159283625Sdim
160283625Sdim    StringRef TargetName;
161283625Sdim    Symbol->getName(TargetName);
162283625Sdim
163283625Sdim    DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
164283625Sdim                 << " RelType: " << RelType << " TargetName: " << TargetName
165283625Sdim                 << " Addend " << Addend << "\n");
166283625Sdim
167283625Sdim    if (IsExtern) {
168283625Sdim      RelocationEntry RE(SectionID, Offset, RelType, Addend);
169283625Sdim      addRelocationForSymbol(RE, TargetName);
170283625Sdim    } else {
171283625Sdim      bool IsCode = SecI->isText();
172283625Sdim      unsigned TargetSectionID =
173283625Sdim          findOrEmitSection(Obj, *SecI, IsCode, ObjSectionToID);
174283625Sdim      uint64_t TargetOffset = getSymbolOffset(*Symbol);
175283625Sdim      RelocationEntry RE(SectionID, Offset, RelType, TargetOffset + Addend);
176283625Sdim      addRelocationForSection(RE, TargetSectionID);
177283625Sdim    }
178283625Sdim
179283625Sdim    return ++RelI;
180283625Sdim  }
181283625Sdim
182283625Sdim  unsigned getStubAlignment() override { return 1; }
183283625Sdim  void registerEHFrames() override {
184283625Sdim    for (auto const &EHFrameSID : UnregisteredEHFrameSections) {
185283625Sdim      uint8_t *EHFrameAddr = Sections[EHFrameSID].Address;
186283625Sdim      uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress;
187283625Sdim      size_t EHFrameSize = Sections[EHFrameSID].Size;
188283625Sdim      MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
189283625Sdim      RegisteredEHFrameSections.push_back(EHFrameSID);
190283625Sdim    }
191283625Sdim    UnregisteredEHFrameSections.clear();
192283625Sdim  }
193283625Sdim  void deregisterEHFrames() override {
194283625Sdim    // Stub
195283625Sdim  }
196283625Sdim  void finalizeLoad(const ObjectFile &Obj,
197283625Sdim                    ObjSectionToIDMap &SectionMap) override {
198283625Sdim    // Look for and record the EH frame section IDs.
199283625Sdim    for (const auto &SectionPair : SectionMap) {
200283625Sdim      const SectionRef &Section = SectionPair.first;
201283625Sdim      StringRef Name;
202283625Sdim      Check(Section.getName(Name));
203283625Sdim      // Note unwind info is split across .pdata and .xdata, so this
204283625Sdim      // may not be sufficiently general for all users.
205283625Sdim      if (Name == ".xdata") {
206283625Sdim        UnregisteredEHFrameSections.push_back(SectionPair.second);
207283625Sdim      }
208283625Sdim    }
209283625Sdim  }
210283625Sdim};
211283625Sdim
212283625Sdim} // end namespace llvm
213283625Sdim
214283625Sdim#undef DEBUG_TYPE
215283625Sdim
216283625Sdim#endif
217