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"
18#include "llvm/ADT/STLExtras.h"
22
23using namespace llvm;
24using namespace llvm::object;
25
26#define DEBUG_TYPE "dyld"
27
28namespace {
29
30class LoadedCOFFObjectInfo final
31 : public LoadedObjectInfoHelper<LoadedCOFFObjectInfo,
32 RuntimeDyld::LoadedObjectInfo> {
33public:
34 LoadedCOFFObjectInfo(
35 RuntimeDyldImpl &RTDyld,
37 : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
38
40 getObjectForDebug(const ObjectFile &Obj) const override {
42 }
43};
44}
45
46namespace llvm {
47
48std::unique_ptr<RuntimeDyldCOFF>
52 switch (Arch) {
53 default: llvm_unreachable("Unsupported target for RuntimeDyldCOFF.");
54 case Triple::x86:
55 return std::make_unique<RuntimeDyldCOFFI386>(MemMgr, Resolver);
56 case Triple::thumb:
57 return std::make_unique<RuntimeDyldCOFFThumb>(MemMgr, Resolver);
58 case Triple::x86_64:
59 return std::make_unique<RuntimeDyldCOFFX86_64>(MemMgr, Resolver);
60 case Triple::aarch64:
61 return std::make_unique<RuntimeDyldCOFFAArch64>(MemMgr, Resolver);
62 }
63}
64
65std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
67 if (auto ObjSectionToIDOrErr = loadObjectImpl(O)) {
68 return std::make_unique<LoadedCOFFObjectInfo>(*this, *ObjSectionToIDOrErr);
69 } else {
70 HasError = true;
72 logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream);
73 return nullptr;
74 }
75}
76
78 // The value in a relocatable COFF object is the offset.
79 return cantFail(Sym.getValue());
80}
81
84 bool SetSectionIDMinus1) {
85 LLVM_DEBUG(dbgs() << "Getting DLLImport entry for " << Name << "... ");
86 assert(Name.starts_with(getImportSymbolPrefix()) &&
87 "Not a DLLImport symbol?");
89 Reloc.SymbolName = Name.data();
90 auto I = Stubs.find(Reloc);
91 if (I != Stubs.end()) {
92 LLVM_DEBUG(dbgs() << format("{0:x8}", I->second) << "\n");
93 return I->second;
94 }
95
96 assert(SectionID < Sections.size() && "SectionID out of range");
97 auto &Sec = Sections[SectionID];
98 auto EntryOffset = alignTo(Sec.getStubOffset(), PointerSize);
99 Sec.advanceStubOffset(EntryOffset + PointerSize - Sec.getStubOffset());
100 Stubs[Reloc] = EntryOffset;
101
102 RelocationEntry RE(SectionID, EntryOffset, PointerReloc, 0, false,
103 Log2_64(PointerSize));
104 // Hack to tell I386/Thumb resolveRelocation that this isn't section relative.
105 if (SetSectionIDMinus1)
106 RE.Sections.SectionA = -1;
108
109 LLVM_DEBUG({
110 dbgs() << "Creating entry at "
111 << formatv("{0:x16} + {1:x8} ( {2:x16} )", Sec.getLoadAddress(),
112 EntryOffset, Sec.getLoadAddress() + EntryOffset)
113 << "\n";
114 });
115 return EntryOffset;
116}
117
119 return Obj.isCOFF();
120}
121
122} // namespace llvm
#define LLVM_DEBUG(X)
Definition: Debug.h:101
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())
This file contains some templates that are useful if you are working with the STL at all.
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:2212
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 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:50
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 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:1680
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
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
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