LLVM  13.0.0git
Mangling.cpp
Go to the documentation of this file.
1 //===----------- Mangling.cpp -- Name Mangling Utilities for ORC ----------===//
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 
10 #include "llvm/IR/Constants.h"
11 #include "llvm/IR/Mangler.h"
12 #include "llvm/Object/MachO.h"
13 #include "llvm/Object/ObjectFile.h"
14 #include "llvm/Support/Debug.h"
15 
16 #define DEBUG_TYPE "orc"
17 
18 namespace llvm {
19 namespace orc {
20 
22  : ES(ES), DL(DL) {}
23 
25  std::string MangledName;
26  {
27  raw_string_ostream MangledNameStream(MangledName);
28  Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
29  }
30  return ES.intern(MangledName);
31 }
32 
36  SymbolNameToDefinitionMap *SymbolToDefinition) {
37  if (GVs.empty())
38  return;
39 
40  MangleAndInterner Mangle(ES, GVs[0]->getParent()->getDataLayout());
41  for (auto *G : GVs) {
42  assert(G && "GVs cannot contain null elements");
43  if (!G->hasName() || G->isDeclaration() || G->hasLocalLinkage() ||
44  G->hasAvailableExternallyLinkage() || G->hasAppendingLinkage())
45  continue;
46 
47  if (G->isThreadLocal() && MO.EmulatedTLS) {
48  auto *GV = cast<GlobalVariable>(G);
49 
50  auto Flags = JITSymbolFlags::fromGlobalValue(*GV);
51 
52  auto EmuTLSV = Mangle(("__emutls_v." + GV->getName()).str());
53  SymbolFlags[EmuTLSV] = Flags;
54  if (SymbolToDefinition)
55  (*SymbolToDefinition)[EmuTLSV] = GV;
56 
57  // If this GV has a non-zero initializer we'll need to emit an
58  // __emutls.t symbol too.
59  if (GV->hasInitializer()) {
60  const auto *InitVal = GV->getInitializer();
61 
62  // Skip zero-initializers.
63  if (isa<ConstantAggregateZero>(InitVal))
64  continue;
65  const auto *InitIntValue = dyn_cast<ConstantInt>(InitVal);
66  if (InitIntValue && InitIntValue->isZero())
67  continue;
68 
69  auto EmuTLST = Mangle(("__emutls_t." + GV->getName()).str());
70  SymbolFlags[EmuTLST] = Flags;
71  if (SymbolToDefinition)
72  (*SymbolToDefinition)[EmuTLST] = GV;
73  }
74  continue;
75  }
76 
77  // Otherwise we just need a normal linker mangling.
78  auto MangledName = Mangle(G->getName());
80  if (SymbolToDefinition)
81  (*SymbolToDefinition)[MangledName] = G;
82  }
83 }
84 
87  auto Obj = object::ObjectFile::createObjectFile(ObjBuffer);
88 
89  if (!Obj)
90  return Obj.takeError();
91 
92  bool IsMachO = isa<object::MachOObjectFile>(Obj->get());
93 
95  for (auto &Sym : (*Obj)->symbols()) {
96  Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
97  if (!SymFlagsOrErr)
98  // TODO: Test this error.
99  return SymFlagsOrErr.takeError();
100 
101  // Skip symbols not defined in this object file.
102  if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
103  continue;
104 
105  // Skip symbols that are not global.
106  if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
107  continue;
108 
109  // Skip symbols that have type SF_File.
110  if (auto SymType = Sym.getType()) {
111  if (*SymType == object::SymbolRef::ST_File)
112  continue;
113  } else
114  return SymType.takeError();
115 
116  auto Name = Sym.getName();
117  if (!Name)
118  return Name.takeError();
119  auto InternedName = ES.intern(*Name);
120  auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
121  if (!SymFlags)
122  return SymFlags.takeError();
123 
124  // Strip the 'exported' flag from MachO linker-private symbols.
125  if (IsMachO && Name->startswith("l"))
126  *SymFlags &= ~JITSymbolFlags::Exported;
127 
128  SymbolFlags[InternedName] = std::move(*SymFlags);
129  }
130 
131  SymbolStringPtr InitSymbol;
132 
133  if (IsMachO) {
134  auto &MachOObj = cast<object::MachOObjectFile>(*Obj->get());
135  for (auto &Sec : MachOObj.sections()) {
136  auto SecType = MachOObj.getSectionType(Sec);
138  size_t Counter = 0;
139  while (true) {
140  std::string InitSymString;
141  raw_string_ostream(InitSymString)
142  << "$." << ObjBuffer.getBufferIdentifier() << ".__inits."
143  << Counter++;
144  InitSymbol = ES.intern(InitSymString);
145  if (SymbolFlags.count(InitSymbol))
146  continue;
147  SymbolFlags[InitSymbol] =
149  break;
150  }
151  break;
152  }
153  }
154  }
155 
156  return std::make_pair(std::move(SymbolFlags), std::move(InitSymbol));
157 }
158 
159 } // End namespace orc.
160 } // End namespace llvm.
llvm::orc::IRSymbolMapper::add
static void add(ExecutionSession &ES, const ManglingOptions &MO, ArrayRef< GlobalValue * > GVs, SymbolFlagsMap &SymbolFlags, SymbolNameToDefinitionMap *SymbolToDefinition=nullptr)
Add mangled symbols for the given GlobalValues to SymbolFlags.
Definition: Mangling.cpp:33
llvm
Definition: AllocatorList.h:23
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:614
llvm::orc::IRSymbolMapper::SymbolNameToDefinitionMap
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition: Mangling.h:45
llvm::orc::SymbolStringPtr
Pointer to a pooled string representing a symbol name.
Definition: SymbolStringPool.h:50
llvm::orc::MangleAndInterner::MangleAndInterner
MangleAndInterner(ExecutionSession &ES, const DataLayout &DL)
Definition: Mangling.cpp:21
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::object::SymbolRef::ST_File
@ ST_File
Definition: ObjectFile.h:175
llvm::orc::getObjectSymbolInfo
Expected< std::pair< SymbolFlagsMap, SymbolStringPtr > > getObjectSymbolInfo(ExecutionSession &ES, MemoryBufferRef ObjBuffer)
Returns a SymbolFlagsMap for the object file represented by the given buffer, or an error if the buff...
Definition: Mangling.cpp:86
llvm::JITSymbolFlags
Flags for symbols in the JIT.
Definition: JITSymbol.h:74
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::MachO::SymbolFlags
SymbolFlags
Symbol flags.
Definition: Symbol.h:25
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
MachO.h
Constants.h
llvm::JITSymbolFlags::MaterializationSideEffectsOnly
@ MaterializationSideEffectsOnly
Definition: JITSymbol.h:87
llvm::Mangler::getNameWithPrefix
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:114
llvm::orc::ExecutionSession::intern
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1276
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
llvm::MachO::SECTION_TYPE
@ SECTION_TYPE
Definition: MachO.h:112
llvm::MachO::S_MOD_INIT_FUNC_POINTERS
@ S_MOD_INIT_FUNC_POINTERS
S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for initialization.
Definition: MachO.h:145
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::DenseMap< SymbolStringPtr, JITSymbolFlags >
llvm::object::BasicSymbolRef::SF_Undefined
@ SF_Undefined
Definition: SymbolicFile.h:108
llvm::object::ObjectFile::createObjectFile
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:184
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ObjectFile.h
Mangler.h
llvm::orc::IRSymbolMapper::ManglingOptions::EmulatedTLS
bool EmulatedTLS
Definition: Mangling.h:42
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::orc::MangleAndInterner::operator()
SymbolStringPtr operator()(StringRef Name)
Definition: Mangling.cpp:24
getParent
static const Function * getParent(const Value *V)
Definition: BasicAliasAnalysis.cpp:767
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::orc::MangleAndInterner
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition: Mangling.h:26
llvm::orc::IRSymbolMapper::ManglingOptions
Definition: Mangling.h:41
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
Mangling.h
llvm::orc::ExecutionSession
An ExecutionSession represents a running JIT program.
Definition: Core.h:1252
llvm::sampleprof::SecType
SecType
Definition: SampleProf.h:118
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
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
llvm::object::BasicSymbolRef::SF_Global
@ SF_Global
Definition: SymbolicFile.h:109
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::MemoryBufferRef::getBufferIdentifier
StringRef getBufferIdentifier() const
Definition: MemoryBufferRef.h:33
Debug.h