LLVM 20.0.0git
RuntimeDyldCOFF.cpp
Go to the documentation of this file.
1//===-- RuntimeDyldCOFF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Implementation of COFF support for the MC-JIT runtime dynamic linker.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RuntimeDyldCOFF.h"
21
22using namespace llvm;
23using namespace llvm::object;
24
25#define DEBUG_TYPE "dyld"
26
27namespace {
28
29class LoadedCOFFObjectInfo final
30 : public LoadedObjectInfoHelper<LoadedCOFFObjectInfo,
31 RuntimeDyld::LoadedObjectInfo> {
32public:
33 LoadedCOFFObjectInfo(
34 RuntimeDyldImpl &RTDyld,
36 : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
37
39 getObjectForDebug(const ObjectFile &Obj) const override {
41 }
42};
43}
44
45namespace llvm {
46
47std::unique_ptr<RuntimeDyldCOFF>
51 switch (Arch) {
52 default: llvm_unreachable("Unsupported target for RuntimeDyldCOFF.");
53 case Triple::x86:
54 return std::make_unique<RuntimeDyldCOFFI386>(MemMgr, Resolver);
55 case Triple::thumb:
56 return std::make_unique<RuntimeDyldCOFFThumb>(MemMgr, Resolver);
57 case Triple::x86_64:
58 return std::make_unique<RuntimeDyldCOFFX86_64>(MemMgr, Resolver);
59 case Triple::aarch64:
60 return std::make_unique<RuntimeDyldCOFFAArch64>(MemMgr, Resolver);
61 }
62}
63
64std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
66 if (auto ObjSectionToIDOrErr = loadObjectImpl(O)) {
67 return std::make_unique<LoadedCOFFObjectInfo>(*this, *ObjSectionToIDOrErr);
68 } else {
69 HasError = true;
71 logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream);
72 return nullptr;
73 }
74}
75
77 // The value in a relocatable COFF object is the offset.
78 return cantFail(Sym.getValue());
79}
80
83 bool SetSectionIDMinus1) {
84 LLVM_DEBUG(dbgs() << "Getting DLLImport entry for " << Name << "... ");
85 assert(Name.starts_with(getImportSymbolPrefix()) &&
86 "Not a DLLImport symbol?");
88 Reloc.SymbolName = Name.data();
89 auto I = Stubs.find(Reloc);
90 if (I != Stubs.end()) {
91 LLVM_DEBUG(dbgs() << format("{0:x8}", I->second) << "\n");
92 return I->second;
93 }
94
95 assert(SectionID < Sections.size() && "SectionID out of range");
96 auto &Sec = Sections[SectionID];
97 auto EntryOffset = alignTo(Sec.getStubOffset(), PointerSize);
98 Sec.advanceStubOffset(EntryOffset + PointerSize - Sec.getStubOffset());
99 Stubs[Reloc] = EntryOffset;
100
101 RelocationEntry RE(SectionID, EntryOffset, PointerReloc, 0, false,
102 Log2_64(PointerSize));
103 // Hack to tell I386/Thumb resolveRelocation that this isn't section relative.
104 if (SetSectionIDMinus1)
105 RE.Sections.SectionA = -1;
107
108 LLVM_DEBUG({
109 dbgs() << "Creating entry at "
110 << formatv("{0:x16} + {1:x8} ( {2:x16} )", Sec.getLoadAddress(),
111 EntryOffset, Sec.getLoadAddress() + EntryOffset)
112 << "\n";
113 });
114 return EntryOffset;
115}
116
118 return Obj.isCOFF();
119}
120
122 const RelocationRef &R) const {
123 object::symbol_iterator Symbol = R.getSymbol();
124 Expected<StringRef> TargetNameOrErr = Symbol->getName();
125 if (!TargetNameOrErr)
126 return false;
127
128 return TargetNameOrErr->starts_with(getImportSymbolPrefix());
129}
130
131} // namespace llvm
#define LLVM_DEBUG(...)
Definition: Debug.h:106
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Tagged union holding either a T or a Error.
Definition: Error.h:481
Symbol resolution interface.
Definition: JITSymbol.h:371
RelocationEntry - used to represent relocations internally in the dynamic linker.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2203
uint64_t getSymbolOffset(const SymbolRef &Sym)
static constexpr StringRef getImportSymbolPrefix()
std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &Obj) override
static std::unique_ptr< RuntimeDyldCOFF > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
bool relocationNeedsDLLImportStub(const RelocationRef &R) const override
bool isCompatibleFile(const object::ObjectFile &Obj) const override
uint64_t getDLLImportOffset(unsigned SectionID, StubMap &Stubs, StringRef Name, bool SetSectionIDMinus1=false)
std::map< RelocationValueRef, uintptr_t > StubMap
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
RuntimeDyld::MemoryManager & MemMgr
Triple::ArchType Arch
Expected< ObjSectionToIDMap > loadObjectImpl(const object::ObjectFile &Obj)
std::map< object::SectionRef, unsigned > ObjSectionToIDMap
Definition: RuntimeDyld.h:73
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
bool isCOFF() const
Definition: Binary.h:131
This class is the base class for all object file types.
Definition: ObjectFile.h:229
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:168
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:65
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1697
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:346
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:756
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155