1 //===----- LinkGraphLayer.cpp - Add LinkGraphs to an ExecutionSession -----===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/LinkGraphLayer.h"
11 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
12 #include "llvm/ExecutionEngine/Orc/Shared/MachOObjectFormat.h"
13 #include "llvm/ExecutionEngine/Orc/Shared/ObjectFormats.h"
15 #define DEBUG_TYPE "orc"
18 using namespace llvm::jitlink
;
19 using namespace llvm::orc
;
23 bool hasInitializerSection(LinkGraph
&G
) {
24 bool IsMachO
= G
.getTargetTriple().isOSBinFormatMachO();
25 bool IsElf
= G
.getTargetTriple().isOSBinFormatELF();
26 if (!IsMachO
&& !IsElf
)
29 for (auto &Sec
: G
.sections()) {
30 if (IsMachO
&& isMachOInitializerSection(Sec
.getName()))
32 if (IsElf
&& isELFInitializerSection(Sec
.getName()))
39 } // end anonymous namespace
43 LinkGraphLayer::~LinkGraphLayer() = default;
45 MaterializationUnit::Interface
LinkGraphLayer::getInterface(LinkGraph
&G
) {
47 MaterializationUnit::Interface LGI
;
49 auto AddSymbol
= [&](Symbol
*Sym
) {
50 // Skip local symbols.
51 if (Sym
->getScope() == Scope::Local
)
53 assert(Sym
->hasName() && "Anonymous non-local symbol?");
55 LGI
.SymbolFlags
[Sym
->getName()] = getJITSymbolFlagsForSymbol(*Sym
);
58 for (auto *Sym
: G
.defined_symbols())
60 for (auto *Sym
: G
.absolute_symbols())
63 if (hasInitializerSection(G
)) {
64 std::string InitSymString
;
66 raw_string_ostream(InitSymString
)
67 << "$." << G
.getName() << ".__inits" << Counter
++;
69 LGI
.InitSymbol
= ES
.intern(InitSymString
);
75 JITSymbolFlags
LinkGraphLayer::getJITSymbolFlagsForSymbol(Symbol
&Sym
) {
78 if (Sym
.getLinkage() == Linkage::Weak
)
79 Flags
|= JITSymbolFlags::Weak
;
81 if (Sym
.getScope() == Scope::Default
)
82 Flags
|= JITSymbolFlags::Exported
;
83 else if (Sym
.getScope() == Scope::SideEffectsOnly
)
84 Flags
|= JITSymbolFlags::MaterializationSideEffectsOnly
;
87 Flags
|= JITSymbolFlags::Callable
;
92 StringRef
LinkGraphMaterializationUnit::getName() const { return G
->getName(); }
94 void LinkGraphMaterializationUnit::discard(const JITDylib
&JD
,
95 const SymbolStringPtr
&Name
) {
96 for (auto *Sym
: G
->defined_symbols())
97 if (Sym
->getName() == Name
) {
98 assert(Sym
->getLinkage() == Linkage::Weak
&&
99 "Discarding non-weak definition");
100 G
->makeExternal(*Sym
);
105 } // namespace llvm::orc