LLVM  16.0.0git
JITSymbol.cpp
Go to the documentation of this file.
1 //===----------- JITSymbol.cpp - JITSymbol class implementation -----------===//
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 // JITSymbol class implementation plus helper functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/GlobalAlias.h"
16 #include "llvm/IR/GlobalValue.h"
18 #include "llvm/Object/ObjectFile.h"
19 
20 using namespace llvm;
21 
23  assert(GV.hasName() && "Can't get flags for anonymous symbol");
24 
26  if (GV.hasWeakLinkage() || GV.hasLinkOnceLinkage())
27  Flags |= JITSymbolFlags::Weak;
28  if (GV.hasCommonLinkage())
29  Flags |= JITSymbolFlags::Common;
30  if (!GV.hasLocalLinkage() && !GV.hasHiddenVisibility())
31  Flags |= JITSymbolFlags::Exported;
32 
33  if (isa<Function>(GV))
34  Flags |= JITSymbolFlags::Callable;
35  else if (isa<GlobalAlias>(GV) &&
36  isa<Function>(cast<GlobalAlias>(GV).getAliasee()))
37  Flags |= JITSymbolFlags::Callable;
38 
39  // Check for a linker-private-global-prefix on the symbol name, in which
40  // case it must be marked as non-exported.
41  if (auto *M = GV.getParent()) {
42  const auto &DL = M->getDataLayout();
43  StringRef LPGP = DL.getLinkerPrivateGlobalPrefix();
44  if (!LPGP.empty() && GV.getName().front() == '\01' &&
45  GV.getName().substr(1).startswith(LPGP))
46  Flags &= ~JITSymbolFlags::Exported;
47  }
48 
49  return Flags;
50 }
51 
54  auto L = S->linkage();
56  Flags |= JITSymbolFlags::Weak;
58  Flags |= JITSymbolFlags::Common;
60  Flags |= JITSymbolFlags::Exported;
61 
62  if (isa<FunctionSummary>(S))
63  Flags |= JITSymbolFlags::Callable;
64 
65  return Flags;
66 }
67 
70  Expected<uint32_t> SymbolFlagsOrErr = Symbol.getFlags();
71  if (!SymbolFlagsOrErr)
72  // TODO: Test this error.
73  return SymbolFlagsOrErr.takeError();
74 
76  if (*SymbolFlagsOrErr & object::BasicSymbolRef::SF_Weak)
77  Flags |= JITSymbolFlags::Weak;
78  if (*SymbolFlagsOrErr & object::BasicSymbolRef::SF_Common)
79  Flags |= JITSymbolFlags::Common;
80  if (*SymbolFlagsOrErr & object::BasicSymbolRef::SF_Exported)
81  Flags |= JITSymbolFlags::Exported;
82 
83  auto SymbolType = Symbol.getType();
84  if (!SymbolType)
85  return SymbolType.takeError();
86 
88  Flags |= JITSymbolFlags::Callable;
89 
90  return Flags;
91 }
92 
95  Expected<uint32_t> SymbolFlagsOrErr = Symbol.getFlags();
96  if (!SymbolFlagsOrErr)
97  // TODO: Actually report errors helpfully.
98  report_fatal_error(SymbolFlagsOrErr.takeError());
99  ARMJITSymbolFlags Flags;
100  if (*SymbolFlagsOrErr & object::BasicSymbolRef::SF_Thumb)
101  Flags |= ARMJITSymbolFlags::Thumb;
102  return Flags;
103 }
104 
105 /// Performs lookup by, for each symbol, first calling
106 /// findSymbolInLogicalDylib and if that fails calling
107 /// findSymbol.
109  OnResolvedFunction OnResolved) {
111  for (auto &Symbol : Symbols) {
112  std::string SymName = Symbol.str();
113  if (auto Sym = findSymbolInLogicalDylib(SymName)) {
114  if (auto AddrOrErr = Sym.getAddress())
115  Result[Symbol] = JITEvaluatedSymbol(*AddrOrErr, Sym.getFlags());
116  else {
117  OnResolved(AddrOrErr.takeError());
118  return;
119  }
120  } else if (auto Err = Sym.takeError()) {
121  OnResolved(std::move(Err));
122  return;
123  } else {
124  // findSymbolInLogicalDylib failed. Lets try findSymbol.
125  if (auto Sym = findSymbol(SymName)) {
126  if (auto AddrOrErr = Sym.getAddress())
127  Result[Symbol] = JITEvaluatedSymbol(*AddrOrErr, Sym.getFlags());
128  else {
129  OnResolved(AddrOrErr.takeError());
130  return;
131  }
132  } else if (auto Err = Sym.takeError()) {
133  OnResolved(std::move(Err));
134  return;
135  } else {
136  OnResolved(make_error<StringError>("Symbol not found: " + Symbol,
138  return;
139  }
140  }
141  }
142 
143  OnResolved(std::move(Result));
144 }
145 
146 /// Performs flags lookup by calling findSymbolInLogicalDylib and
147 /// returning the flags value for that symbol.
151 
152  for (auto &Symbol : Symbols) {
153  std::string SymName = Symbol.str();
154  if (auto Sym = findSymbolInLogicalDylib(SymName)) {
155  // If there's an existing def but it is not strong, then the caller is
156  // responsible for it.
157  if (!Sym.getFlags().isStrong())
158  Result.insert(Symbol);
159  } else if (auto Err = Sym.takeError())
160  return std::move(Err);
161  else {
162  // If there is no existing definition then the caller is responsible for
163  // it.
164  Result.insert(Symbol);
165  }
166  }
167 
168  return std::move(Result);
169 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
JITSymbol.h
llvm::GlobalValue::hasCommonLinkage
bool hasCommonLinkage() const
Definition: GlobalValue.h:521
llvm::unique_function
unique_function is a type-erasing functor similar to std::function.
Definition: FunctionExtras.h:56
llvm::StringRef::front
char front() const
front - Get the first character in the string.
Definition: StringRef.h:140
llvm::object::BasicSymbolRef::SF_Common
@ SF_Common
Definition: SymbolicFile.h:114
llvm::LegacyJITSymbolResolver::lookup
void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) final
Performs lookup by, for each symbol, first calling findSymbolInLogicalDylib and if that fails calling...
Definition: JITSymbol.cpp:108
llvm::GlobalValue::hasWeakLinkage
bool hasWeakLinkage() const
Definition: GlobalValue.h:511
llvm::Value::hasName
bool hasName() const
Definition: Value.h:261
llvm::XCOFF::SymbolType
SymbolType
Definition: XCOFF.h:225
llvm::JITSymbolFlags::fromSummary
static JITSymbolFlags fromSummary(GlobalValueSummary *S)
Construct a JITSymbolFlags value based on the flags of the given global value summary.
Definition: JITSymbol.cpp:52
llvm::object::BasicSymbolRef::SF_Exported
@ SF_Exported
Definition: SymbolicFile.h:116
llvm::GlobalValue::hasHiddenVisibility
bool hasHiddenVisibility() const
Definition: GlobalValue.h:244
llvm::StringRef::substr
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:561
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::object::BasicSymbolRef::SF_Weak
@ SF_Weak
Definition: SymbolicFile.h:112
llvm::JITSymbolFlags::Common
@ Common
Definition: JITSymbol.h:83
llvm::LegacyJITSymbolResolver::getResponsibilitySet
Expected< LookupSet > getResponsibilitySet(const LookupSet &Symbols) final
Performs flags lookup by calling findSymbolInLogicalDylib and returning the flags value for that symb...
Definition: JITSymbol.cpp:149
llvm::JITSymbolFlags
Flags for symbols in the JIT.
Definition: JITSymbol.h:74
ModuleSummaryIndex.h
llvm::JITEvaluatedSymbol
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:229
llvm::ARMJITSymbolFlags
ARM-specific JIT symbol flags.
Definition: JITSymbol.h:211
llvm::GlobalValue::isCommonLinkage
static bool isCommonLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:404
llvm::JITSymbolResolver::LookupSet
std::set< StringRef > LookupSet
Definition: JITSymbol.h:373
llvm::GlobalValueSummary
Function and variable summary information to aid decisions and implementation of importing.
Definition: ModuleSummaryIndex.h:289
GlobalValue.h
llvm::StringRef::startswith
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:256
llvm::LegacyJITSymbolResolver::findSymbolInLogicalDylib
virtual JITSymbol findSymbolInLogicalDylib(const std::string &Name)=0
This method returns the address of the specified symbol if it exists within the logical dynamic libra...
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:145
llvm::StringRef::empty
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::object::SymbolRef::ST_Function
@ ST_Function
Definition: ObjectFile.h:177
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:644
llvm::ARMJITSymbolFlags::Thumb
@ Thumb
Definition: JITSymbol.h:217
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::JITSymbolFlags::Callable
@ Callable
Definition: JITSymbol.h:86
llvm::object::BasicSymbolRef::SF_Thumb
@ SF_Thumb
Definition: SymbolicFile.h:119
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ObjectFile.h
llvm::GlobalValue::isExternalLinkage
static bool isExternalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:365
llvm::GlobalValue::hasLocalLinkage
bool hasLocalLinkage() const
Definition: GlobalValue.h:517
llvm::GlobalValue::isWeakLinkage
static bool isWeakLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:386
llvm::JITSymbolFlags::Weak
@ Weak
Definition: JITSymbol.h:82
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:308
llvm::GlobalValue::isLinkOnceLinkage
static bool isLinkOnceLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:377
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:79
Function.h
llvm::JITSymbolFlags::fromGlobalValue
static JITSymbolFlags fromGlobalValue(const GlobalValue &GV)
Construct a JITSymbolFlags value based on the flags of the given global value.
Definition: JITSymbol.cpp:22
GlobalAlias.h
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:83
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
llvm::ARMJITSymbolFlags::fromObjectSymbol
static ARMJITSymbolFlags fromObjectSymbol(const object::SymbolRef &Symbol)
Definition: JITSymbol.cpp:94
llvm::JITSymbolFlags::fromObjectSymbol
static Expected< JITSymbolFlags > fromObjectSymbol(const object::SymbolRef &Symbol)
Construct a JITSymbolFlags value based on the flags of the given libobject symbol.
Definition: JITSymbol.cpp:69
llvm::JITSymbolResolver::LookupResult
std::map< StringRef, JITEvaluatedSymbol > LookupResult
Definition: JITSymbol.h:374
llvm::GlobalValue::hasLinkOnceLinkage
bool hasLinkOnceLinkage() const
Definition: GlobalValue.h:504
llvm::object::SymbolRef
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:167
llvm::JITSymbolFlags::None
@ None
Definition: JITSymbol.h:80
llvm::GlobalValue::isExternalWeakLinkage
static bool isExternalWeakLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:401
llvm::JITSymbolFlags::Exported
@ Exported
Definition: JITSymbol.h:85
llvm::LegacyJITSymbolResolver::findSymbol
virtual JITSymbol findSymbol(const std::string &Name)=0
This method returns the address of the specified function or variable.