RuntimeDyld: add COFF i386 support
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / Targets / RuntimeDyldCOFFI386.h
1 //===--- RuntimeDyldCOFFI386.h --- COFF/X86_64 specific code ---*- C++ --*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // COFF x86 support for MC-JIT runtime dynamic linker.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFI386_H
15 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFI386_H
16
17 #include "llvm/Object/COFF.h"
18 #include "llvm/Support/COFF.h"
19 #include "../RuntimeDyldCOFF.h"
20
21 #define DEBUG_TYPE "dyld"
22
23 namespace llvm {
24
25 class RuntimeDyldCOFFI386 : public RuntimeDyldCOFF {
26 public:
27   RuntimeDyldCOFFI386(RuntimeDyld::MemoryManager &MM,
28                       RuntimeDyld::SymbolResolver &Resolver)
29       : RuntimeDyldCOFF(MM, Resolver) {}
30
31   unsigned getMaxStubSize() override {
32     return 8; // 2-byte jmp instruction + 32-bit relative address + 2 byte pad
33   }
34
35   unsigned getStubAlignment() override { return 1; }
36
37   relocation_iterator processRelocationRef(unsigned SectionID,
38                                            relocation_iterator RelI,
39                                            const ObjectFile &Obj,
40                                            ObjSectionToIDMap &ObjSectionToID,
41                                            StubMap &Stubs) override {
42     auto Symbol = RelI->getSymbol();
43     if (Symbol == Obj.symbol_end())
44       report_fatal_error("Unknown symbol in relocation");
45
46     ErrorOr<StringRef> TargetNameOrErr = Symbol->getName();
47     if (auto EC = TargetNameOrErr.getError())
48       report_fatal_error(EC.message());
49     StringRef TargetName = *TargetNameOrErr;
50
51     auto Section = *Symbol->getSection();
52
53     uint64_t RelType = RelI->getType();
54     uint64_t Offset = RelI->getOffset();
55
56 #if !defined(NDEBUG)
57     SmallString<32> RelTypeName;
58     RelI->getTypeName(RelTypeName);
59 #endif
60     DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
61                  << " RelType: " << RelTypeName << " TargetName: " << TargetName
62                  << "\n");
63
64     unsigned TargetSectionID = -1;
65     if (Section == Obj.section_end()) {
66       RelocationEntry RE(SectionID, Offset, RelType, 0, -1, 0, 0, 0, false, 0);
67       addRelocationForSymbol(RE, TargetName);
68     } else {
69       TargetSectionID =
70           findOrEmitSection(Obj, *Section, Section->isText(), ObjSectionToID);
71
72       switch (RelType) {
73       case COFF::IMAGE_REL_I386_ABSOLUTE:
74         // This relocation is ignored.
75         break;
76       case COFF::IMAGE_REL_I386_DIR32:
77       case COFF::IMAGE_REL_I386_DIR32NB:
78       case COFF::IMAGE_REL_I386_REL32: {
79         RelocationEntry RE =
80             RelocationEntry(SectionID, Offset, RelType, 0, TargetSectionID,
81                             getSymbolOffset(*Symbol), 0, 0, false, 0);
82         addRelocationForSection(RE, TargetSectionID);
83         break;
84       }
85       case COFF::IMAGE_REL_I386_SECTION: {
86         RelocationEntry RE =
87             RelocationEntry(TargetSectionID, Offset, RelType, 0);
88         addRelocationForSection(RE, TargetSectionID);
89         break;
90       }
91       case COFF::IMAGE_REL_I386_SECREL: {
92         RelocationEntry RE = RelocationEntry(SectionID, Offset, RelType,
93                                              getSymbolOffset(*Symbol));
94         addRelocationForSection(RE, TargetSectionID);
95         break;
96       }
97       default:
98         llvm_unreachable("unsupported relocation type");
99       }
100
101     }
102
103     return ++RelI;
104   }
105
106   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
107     const auto Section = Sections[RE.SectionID];
108     uint8_t *Target = Section.Address + RE.Offset;
109
110     switch (RE.RelType) {
111     case COFF::IMAGE_REL_I386_ABSOLUTE:
112       // This relocation is ignored.
113       break;
114     case COFF::IMAGE_REL_I386_DIR32: {
115       // The target's 32-bit VA.
116       uint64_t Result =
117           RE.Sections.SectionA == static_cast<uint32_t>(-1)
118               ? Value
119               : Sections[RE.Sections.SectionA].LoadAddress + RE.Addend;
120       assert(static_cast<int32_t>(Result) <= INT32_MAX &&
121              "relocation overflow");
122       assert(static_cast<int32_t>(Result) >= INT32_MIN &&
123              "relocation underflow");
124       DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
125                    << " RelType: IMAGE_REL_I386_DIR32"
126                    << " TargetSection: " << RE.Sections.SectionA
127                    << " Value: " << format("0x%08" PRIx32, Result) << '\n');
128       writeBytesUnaligned(Result, Target, 4);
129       break;
130     }
131     case COFF::IMAGE_REL_I386_DIR32NB: {
132       // The target's 32-bit RVA.
133       // NOTE: use Section[0].LoadAddress as an approximation of ImageBase
134       uint64_t Result = Sections[RE.Sections.SectionA].LoadAddress + RE.Addend -
135                         Sections[0].LoadAddress;
136       assert(static_cast<int32_t>(Result) <= INT32_MAX &&
137              "relocation overflow");
138       assert(static_cast<int32_t>(Result) >= INT32_MIN &&
139              "relocation underflow");
140       DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
141                    << " RelType: IMAGE_REL_I386_DIR32NB"
142                    << " TargetSection: " << RE.Sections.SectionA
143                    << " Value: " << format("0x%08" PRIx32, Result) << '\n');
144       writeBytesUnaligned(Result, Target, 4);
145       break;
146     }
147     case COFF::IMAGE_REL_I386_REL32: {
148       // 32-bit relative displacement to the target.
149       uint64_t Result = Sections[RE.Sections.SectionA].LoadAddress -
150                         Section.LoadAddress + RE.Addend - 4 - RE.Offset;
151       assert(static_cast<int32_t>(Result) <= INT32_MAX &&
152              "relocation overflow");
153       assert(static_cast<int32_t>(Result) >= INT32_MIN &&
154              "relocation underflow");
155       DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
156                    << " RelType: IMAGE_REL_I386_REL32"
157                    << " TargetSection: " << RE.Sections.SectionA
158                    << " Value: " << format("0x%08" PRIx32, Result) << '\n');
159       writeBytesUnaligned(Result, Target, 4);
160       break;
161     }
162     case COFF::IMAGE_REL_I386_SECTION:
163       // 16-bit section index of the section that contains the target.
164       assert(static_cast<int16_t>(RE.SectionID) <= INT16_MAX &&
165              "relocation overflow");
166       assert(static_cast<int16_t>(RE.SectionID) >= INT16_MIN &&
167              "relocation underflow");
168       DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
169                    << " RelType: IMAGE_REL_I386_SECTION Value: " << RE.SectionID
170                    << '\n');
171       writeBytesUnaligned(RE.SectionID, Target, 2);
172       break;
173     case COFF::IMAGE_REL_I386_SECREL:
174       // 32-bit offset of the target from the beginning of its section.
175       assert(static_cast<int32_t>(RE.Addend) <= INT32_MAX &&
176              "relocation overflow");
177       assert(static_cast<int32_t>(RE.Addend) >= INT32_MIN &&
178              "relocation underflow");
179       DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
180                    << " RelType: IMAGE_REL_I386_SECREL Value: " << RE.Addend
181                    << '\n');
182       writeBytesUnaligned(RE.Addend, Target, 2);
183       break;
184     default:
185       llvm_unreachable("unsupported relocation type");
186     }
187   }
188
189   void registerEHFrames() override {}
190   void deregisterEHFrames() override {}
191
192   void finalizeLoad(const ObjectFile &Obj,
193                     ObjSectionToIDMap &SectionMap) override {}
194 };
195
196 }
197
198 #endif
199