LLVM  4.0.0
ModuleSymbolTable.cpp
Go to the documentation of this file.
1 //===- ModuleSymbolTable.cpp - symbol table for in-memory IR ----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class represents a symbol table built from in-memory IR. It provides
11 // access to GlobalValues and should only be used if such access is required
12 // (e.g. in the LTO implementation).
13 //
14 //===----------------------------------------------------------------------===//
15 
17 #include "RecordStreamer.h"
18 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/IR/GVMaterializer.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Mangler.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCInstrInfo.h"
30 #include "llvm/MC/MCRegisterInfo.h"
32 #include "llvm/Object/ObjectFile.h"
34 #include "llvm/Support/SourceMgr.h"
37 using namespace llvm;
38 using namespace object;
39 
41  if (FirstMod)
42  assert(FirstMod->getTargetTriple() == M->getTargetTriple());
43  else
44  FirstMod = M;
45 
46  for (Function &F : *M)
47  SymTab.push_back(&F);
48  for (GlobalVariable &GV : M->globals())
49  SymTab.push_back(&GV);
50  for (GlobalAlias &GA : M->aliases())
51  SymTab.push_back(&GA);
52 
53  CollectAsmSymbols(Triple(M->getTargetTriple()), M->getModuleInlineAsm(),
55  SymTab.push_back(new (AsmSymbols.Allocate())
56  AsmSymbol(Name, Flags));
57  });
58 }
59 
61  const Triple &TT, StringRef InlineAsm,
63  if (InlineAsm.empty())
64  return;
65 
66  std::string Err;
67  const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
68  assert(T && T->hasMCAsmParser());
69 
70  std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
71  if (!MRI)
72  return;
73 
74  std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str()));
75  if (!MAI)
76  return;
77 
78  std::unique_ptr<MCSubtargetInfo> STI(
79  T->createMCSubtargetInfo(TT.str(), "", ""));
80  if (!STI)
81  return;
82 
83  std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
84  if (!MCII)
85  return;
86 
87  MCObjectFileInfo MOFI;
88  MCContext MCCtx(MAI.get(), MRI.get(), &MOFI);
89  MOFI.InitMCObjectFileInfo(TT, /*PIC*/ false, CodeModel::Default, MCCtx);
90  RecordStreamer Streamer(MCCtx);
91  T->createNullTargetStreamer(Streamer);
92 
93  std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
95  SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
96  std::unique_ptr<MCAsmParser> Parser(
97  createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI));
98 
99  MCTargetOptions MCOptions;
100  std::unique_ptr<MCTargetAsmParser> TAP(
101  T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
102  if (!TAP)
103  return;
104 
105  Parser->setTargetParser(*TAP);
106  if (Parser->Run(false))
107  return;
108 
109  for (auto &KV : Streamer) {
110  StringRef Key = KV.first();
111  RecordStreamer::State Value = KV.second;
112  // FIXME: For now we just assume that all asm symbols are executable.
114  switch (Value) {
116  llvm_unreachable("NeverSeen should have been replaced earlier");
119  break;
121  break;
126  break;
130  break;
134  }
135  AsmSymbol(Key, BasicSymbolRef::Flags(Res));
136  }
137 }
138 
140  if (S.is<AsmSymbol *>()) {
141  OS << S.get<AsmSymbol *>()->first;
142  return;
143  }
144 
145  auto *GV = S.get<GlobalValue *>();
146  if (GV->hasDLLImportStorageClass())
147  OS << "__imp_";
148 
149  Mang.getNameWithPrefix(OS, GV, false);
150 }
151 
153  if (S.is<AsmSymbol *>())
154  return S.get<AsmSymbol *>()->second;
155 
156  auto *GV = S.get<GlobalValue *>();
157 
159  if (GV->isDeclarationForLinker())
161  else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage())
163  if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
164  if (GVar->isConstant())
166  }
167  if (dyn_cast_or_null<Function>(GV->getBaseObject()))
169  if (isa<GlobalAlias>(GV))
171  if (GV->hasPrivateLinkage())
173  if (!GV->hasLocalLinkage())
175  if (GV->hasCommonLinkage())
177  if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
178  GV->hasExternalWeakLinkage())
180 
181  if (GV->getName().startswith("llvm."))
183  else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
184  if (Var->getSection() == "llvm.metadata")
186  }
187 
188  return Res;
189 }
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
std::pair< std::string, uint32_t > AsmSymbol
SourceMgr SrcMgr
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
MCRegisterInfo * createMCRegInfo(StringRef TT) const
createMCRegInfo - Create a MCRegisterInfo implementation.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:83
const std::string & str() const
Definition: Triple.h:339
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:218
void InitMCObjectFileInfo(const Triple &TT, bool PIC, CodeModel::Model CM, MCContext &ctx)
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
MCSubtargetInfo * createMCSubtargetInfo(StringRef TheTriple, StringRef CPU, StringRef Features) const
createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
struct fuzzer::@269 Flags
Context object for machine code objects.
Definition: MCContext.h:51
#define F(x, y, z)
Definition: MD5.cpp:51
MCTargetAsmParser * createMCAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser, const MCInstrInfo &MII, const MCTargetOptions &Options) const
createMCAsmParser - Create a target specific assembly parser.
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:118
MCAsmInfo * createMCAsmInfo(const MCRegisterInfo &MRI, StringRef TheTriple) const
createMCAsmInfo - Create a MCAsmInfo implementation for the specified target triple.
unsigned const MachineRegisterInfo * MRI
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &)
Create an MCAsmParser instance.
Definition: AsmParser.cpp:5520
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:35
bool hasMCAsmParser() const
hasMCAsmParser - Check if this target supports assembly parsing.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
T get() const
Returns the value of the specified pointer type.
Definition: PointerUnion.h:135
Module.h This file contains the declarations for the Module class.
MCTargetStreamer * createNullTargetStreamer(MCStreamer &S) const
Target - Wrapper for Target specific information.
uint32_t getSymbolFlags(Symbol S) const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:130
void printSymbolName(raw_ostream &OS, Symbol S) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void CollectAsmSymbols(const Triple &TheTriple, StringRef InlineAsm, function_ref< void(StringRef, object::BasicSymbolRef::Flags)> AsmSymbol)
Parse inline ASM and collect the symbols that are defined or referenced in the current module...
LLVM Value Representation.
Definition: Value.h:71
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
Represents a location in source code.
Definition: SMLoc.h:24
int is() const
Test if the Union currently holds the type matching T.
Definition: PointerUnion.h:123
A discriminated union of two pointer types, with the discriminator in the low bit of the pointer...
Definition: PointerUnion.h:87