LLVM 17.0.0git
ModuleSymbolTable.cpp
Go to the documentation of this file.
1//===- ModuleSymbolTable.cpp - symbol table for in-memory IR --------------===//
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// This class represents a symbol table built from in-memory IR. It provides
10// access to GlobalValues and should only be used if such access is required
11// (e.g. in the LTO implementation).
12//
13//===----------------------------------------------------------------------===//
14
16#include "RecordStreamer.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/Triple.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/GlobalAlias.h"
23#include "llvm/IR/GlobalValue.h"
25#include "llvm/IR/InlineAsm.h"
26#include "llvm/IR/Module.h"
27#include "llvm/MC/MCAsmInfo.h"
28#include "llvm/MC/MCContext.h"
29#include "llvm/MC/MCInstrInfo.h"
35#include "llvm/MC/MCSymbol.h"
42#include "llvm/Support/SMLoc.h"
45#include <algorithm>
46#include <cassert>
47#include <cstdint>
48#include <memory>
49#include <string>
50
51using namespace llvm;
52using namespace object;
53
55 if (FirstMod)
56 assert(FirstMod->getTargetTriple() == M->getTargetTriple());
57 else
58 FirstMod = M;
59
60 for (GlobalValue &GV : M->global_values())
61 SymTab.push_back(&GV);
62
64 SymTab.push_back(new (AsmSymbols.Allocate())
65 AsmSymbol(std::string(Name), Flags));
66 });
67}
68
69static void
72 StringRef InlineAsm = M.getModuleInlineAsm();
73 if (InlineAsm.empty())
74 return;
75
76 std::string Err;
77 const Triple TT(M.getTargetTriple());
78 const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
79 assert(T && T->hasMCAsmParser());
80
81 std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
82 if (!MRI)
83 return;
84
85 MCTargetOptions MCOptions;
86 std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str(), MCOptions));
87 if (!MAI)
88 return;
89
90 std::unique_ptr<MCSubtargetInfo> STI(
91 T->createMCSubtargetInfo(TT.str(), "", ""));
92 if (!STI)
93 return;
94
95 std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
96 if (!MCII)
97 return;
98
99 std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
101 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
102
103 MCContext MCCtx(TT, MAI.get(), MRI.get(), STI.get(), &SrcMgr);
104 std::unique_ptr<MCObjectFileInfo> MOFI(
105 T->createMCObjectFileInfo(MCCtx, /*PIC=*/false));
106 MOFI->setSDKVersion(M.getSDKVersion());
107 MCCtx.setObjectFileInfo(MOFI.get());
108 RecordStreamer Streamer(MCCtx, M);
109 T->createNullTargetStreamer(Streamer);
110
111 std::unique_ptr<MCAsmParser> Parser(
112 createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI));
113
114 std::unique_ptr<MCTargetAsmParser> TAP(
115 T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
116 if (!TAP)
117 return;
118
119 // Module-level inline asm is assumed to use At&t syntax (see
120 // AsmPrinter::doInitialization()).
121 Parser->setAssemblerDialect(InlineAsm::AD_ATT);
122
123 Parser->setTargetParser(*TAP);
124 if (Parser->Run(false))
125 return;
126
127 Init(Streamer);
128}
129
131 const Module &M,
133 initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
134 Streamer.flushSymverDirectives();
135
136 for (auto &KV : Streamer) {
137 StringRef Key = KV.first();
138 RecordStreamer::State Value = KV.second;
139 // FIXME: For now we just assume that all asm symbols are executable.
141 switch (Value) {
143 llvm_unreachable("NeverSeen should have been replaced earlier");
146 break;
148 break;
153 break;
157 break;
161 }
163 }
164 });
165}
166
168 const Module &M, function_ref<void(StringRef, StringRef)> AsmSymver) {
169 initializeRecordStreamer(M, [&](RecordStreamer &Streamer) {
170 for (auto &KV : Streamer.symverAliases())
171 for (auto &Alias : KV.second)
172 AsmSymver(KV.first->getName(), Alias);
173 });
174}
175
177 if (S.is<AsmSymbol *>()) {
178 OS << S.get<AsmSymbol *>()->first;
179 return;
180 }
181
182 auto *GV = S.get<GlobalValue *>();
183 if (GV->hasDLLImportStorageClass())
184 OS << "__imp_";
185
186 Mang.getNameWithPrefix(OS, GV, false);
187}
188
190 if (S.is<AsmSymbol *>())
191 return S.get<AsmSymbol *>()->second;
192
193 auto *GV = S.get<GlobalValue *>();
194
196 if (GV->isDeclarationForLinker())
198 else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage())
200 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
201 if (GVar->isConstant())
203 }
204 if (const GlobalObject *GO = GV->getAliaseeObject())
205 if (isa<Function>(GO) || isa<GlobalIFunc>(GO))
207 if (isa<GlobalAlias>(GV))
209 if (GV->hasPrivateLinkage())
211 if (!GV->hasLocalLinkage())
213 if (GV->hasCommonLinkage())
215 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
216 GV->hasExternalWeakLinkage())
218
219 if (GV->getName().startswith("llvm."))
221 else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
222 if (Var->getSection() == "llvm.metadata")
224 }
225
226 return Res;
227}
unsigned const MachineRegisterInfo * MRI
This file defines the StringMap class.
This header is deprecated in favour of llvm/TargetParser/Triple.h.
for(auto &MBB :MF)
std::string Name
static void initializeRecordStreamer(const Module &M, function_ref< void(RecordStreamer &)> Init)
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:369
Context object for machine code objects.
Definition: MCContext.h:76
void setObjectFileInfo(const MCObjectFileInfo *Mofi)
Definition: MCContext.h:444
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:119
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
static void CollectAsmSymvers(const Module &M, function_ref< void(StringRef, StringRef)> AsmSymver)
Parse inline ASM and collect the symvers directives that are defined in the current module.
std::pair< std::string, uint32_t > AsmSymbol
void printSymbolName(raw_ostream &OS, Symbol S) const
uint32_t getSymbolFlags(Symbol S) const
static void CollectAsmSymbols(const Module &M, function_ref< void(StringRef, object::BasicSymbolRef::Flags)> AsmSymbol)
Parse inline ASM and collect the symbols that are defined or referenced in the current module.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:258
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
T get() const
Returns the value of the specified pointer type.
Definition: PointerUnion.h:155
bool is() const
Test if the Union currently holds the type matching T.
Definition: PointerUnion.h:150
iterator_range< const_symver_iterator > symverAliases()
Represents a location in source code.
Definition: SMLoc.h:23
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:144
T * Allocate(size_t num=1)
Allocate space for an array of objects without constructing them.
Definition: Allocator.h:432
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
LLVM Value Representation.
Definition: Value.h:74
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#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
SourceMgr SrcMgr
Definition: Error.cpp:24
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, unsigned CB=0)
Create an MCAsmParser instance for parsing assembly similar to gas syntax.
Definition: AsmParser.cpp:6440
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.