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 15 #include "llvm/Object/ModuleSymbolTable.h" 16 #include "RecordStreamer.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SmallString.h" 19 #include "llvm/ADT/StringMap.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/ADT/Triple.h" 22 #include "llvm/IR/Function.h" 23 #include "llvm/IR/GlobalAlias.h" 24 #include "llvm/IR/GlobalValue.h" 25 #include "llvm/IR/GlobalVariable.h" 26 #include "llvm/IR/InlineAsm.h" 27 #include "llvm/IR/Module.h" 28 #include "llvm/MC/MCAsmInfo.h" 29 #include "llvm/MC/MCContext.h" 30 #include "llvm/MC/MCDirectives.h" 31 #include "llvm/MC/MCInstrInfo.h" 32 #include "llvm/MC/MCObjectFileInfo.h" 33 #include "llvm/MC/MCParser/MCAsmParser.h" 34 #include "llvm/MC/MCParser/MCTargetAsmParser.h" 35 #include "llvm/MC/MCRegisterInfo.h" 36 #include "llvm/MC/MCSubtargetInfo.h" 37 #include "llvm/MC/MCSymbol.h" 38 #include "llvm/MC/MCTargetOptions.h" 39 #include "llvm/Object/SymbolicFile.h" 40 #include "llvm/Support/Casting.h" 41 #include "llvm/Support/CodeGen.h" 42 #include "llvm/Support/ErrorHandling.h" 43 #include "llvm/Support/MemoryBuffer.h" 44 #include "llvm/Support/SMLoc.h" 45 #include "llvm/Support/SourceMgr.h" 46 #include "llvm/Support/TargetRegistry.h" 47 #include "llvm/Support/raw_ostream.h" 48 #include <algorithm> 49 #include <cassert> 50 #include <cstdint> 51 #include <memory> 52 #include <string> 53 54 using namespace llvm; 55 using namespace object; 56 57 void ModuleSymbolTable::addModule(Module *M) { 58 if (FirstMod) 59 assert(FirstMod->getTargetTriple() == M->getTargetTriple()); 60 else 61 FirstMod = M; 62 63 for (GlobalValue &GV : M->global_values()) 64 SymTab.push_back(&GV); 65 66 CollectAsmSymbols(*M, [this](StringRef Name, BasicSymbolRef::Flags Flags) { 67 SymTab.push_back(new (AsmSymbols.Allocate()) 68 AsmSymbol(std::string(Name), Flags)); 69 }); 70 } 71 72 static void 73 initializeRecordStreamer(const Module &M, 74 function_ref<void(RecordStreamer &)> Init) { 75 StringRef InlineAsm = M.getModuleInlineAsm(); 76 if (InlineAsm.empty()) 77 return; 78 79 std::string Err; 80 const Triple TT(M.getTargetTriple()); 81 const Target *T = TargetRegistry::lookupTarget(TT.str(), Err); 82 assert(T && T->hasMCAsmParser()); 83 84 std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str())); 85 if (!MRI) 86 return; 87 88 MCTargetOptions MCOptions; 89 std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str(), MCOptions)); 90 if (!MAI) 91 return; 92 93 std::unique_ptr<MCSubtargetInfo> STI( 94 T->createMCSubtargetInfo(TT.str(), "", "")); 95 if (!STI) 96 return; 97 98 std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo()); 99 if (!MCII) 100 return; 101 102 MCObjectFileInfo MOFI; 103 MCContext MCCtx(MAI.get(), MRI.get(), &MOFI); 104 MOFI.InitMCObjectFileInfo(TT, /*PIC*/ false, MCCtx); 105 MOFI.setSDKVersion(M.getSDKVersion()); 106 RecordStreamer Streamer(MCCtx, M); 107 T->createNullTargetStreamer(Streamer); 108 109 std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm)); 110 SourceMgr SrcMgr; 111 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc()); 112 std::unique_ptr<MCAsmParser> Parser( 113 createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI)); 114 115 std::unique_ptr<MCTargetAsmParser> TAP( 116 T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions)); 117 if (!TAP) 118 return; 119 120 // Module-level inline asm is assumed to use At&t syntax (see 121 // AsmPrinter::doInitialization()). 122 Parser->setAssemblerDialect(InlineAsm::AD_ATT); 123 124 Parser->setTargetParser(*TAP); 125 if (Parser->Run(false)) 126 return; 127 128 Init(Streamer); 129 } 130 131 void ModuleSymbolTable::CollectAsmSymbols( 132 const Module &M, 133 function_ref<void(StringRef, BasicSymbolRef::Flags)> AsmSymbol) { 134 initializeRecordStreamer(M, [&](RecordStreamer &Streamer) { 135 Streamer.flushSymverDirectives(); 136 137 for (auto &KV : Streamer) { 138 StringRef Key = KV.first(); 139 RecordStreamer::State Value = KV.second; 140 // FIXME: For now we just assume that all asm symbols are executable. 141 uint32_t Res = BasicSymbolRef::SF_Executable; 142 switch (Value) { 143 case RecordStreamer::NeverSeen: 144 llvm_unreachable("NeverSeen should have been replaced earlier"); 145 case RecordStreamer::DefinedGlobal: 146 Res |= BasicSymbolRef::SF_Global; 147 break; 148 case RecordStreamer::Defined: 149 break; 150 case RecordStreamer::Global: 151 case RecordStreamer::Used: 152 Res |= BasicSymbolRef::SF_Undefined; 153 Res |= BasicSymbolRef::SF_Global; 154 break; 155 case RecordStreamer::DefinedWeak: 156 Res |= BasicSymbolRef::SF_Weak; 157 Res |= BasicSymbolRef::SF_Global; 158 break; 159 case RecordStreamer::UndefinedWeak: 160 Res |= BasicSymbolRef::SF_Weak; 161 Res |= BasicSymbolRef::SF_Undefined; 162 } 163 AsmSymbol(Key, BasicSymbolRef::Flags(Res)); 164 } 165 }); 166 } 167 168 void ModuleSymbolTable::CollectAsmSymvers( 169 const Module &M, function_ref<void(StringRef, StringRef)> AsmSymver) { 170 initializeRecordStreamer(M, [&](RecordStreamer &Streamer) { 171 for (auto &KV : Streamer.symverAliases()) 172 for (auto &Alias : KV.second) 173 AsmSymver(KV.first->getName(), Alias); 174 }); 175 } 176 177 void ModuleSymbolTable::printSymbolName(raw_ostream &OS, Symbol S) const { 178 if (S.is<AsmSymbol *>()) { 179 OS << S.get<AsmSymbol *>()->first; 180 return; 181 } 182 183 auto *GV = S.get<GlobalValue *>(); 184 if (GV->hasDLLImportStorageClass()) 185 OS << "__imp_"; 186 187 Mang.getNameWithPrefix(OS, GV, false); 188 } 189 190 uint32_t ModuleSymbolTable::getSymbolFlags(Symbol S) const { 191 if (S.is<AsmSymbol *>()) 192 return S.get<AsmSymbol *>()->second; 193 194 auto *GV = S.get<GlobalValue *>(); 195 196 uint32_t Res = BasicSymbolRef::SF_None; 197 if (GV->isDeclarationForLinker()) 198 Res |= BasicSymbolRef::SF_Undefined; 199 else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage()) 200 Res |= BasicSymbolRef::SF_Hidden; 201 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) { 202 if (GVar->isConstant()) 203 Res |= BasicSymbolRef::SF_Const; 204 } 205 if (dyn_cast_or_null<Function>(GV->getBaseObject())) 206 Res |= BasicSymbolRef::SF_Executable; 207 if (isa<GlobalAlias>(GV)) 208 Res |= BasicSymbolRef::SF_Indirect; 209 if (GV->hasPrivateLinkage()) 210 Res |= BasicSymbolRef::SF_FormatSpecific; 211 if (!GV->hasLocalLinkage()) 212 Res |= BasicSymbolRef::SF_Global; 213 if (GV->hasCommonLinkage()) 214 Res |= BasicSymbolRef::SF_Common; 215 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() || 216 GV->hasExternalWeakLinkage()) 217 Res |= BasicSymbolRef::SF_Weak; 218 219 if (GV->getName().startswith("llvm.")) 220 Res |= BasicSymbolRef::SF_FormatSpecific; 221 else if (auto *Var = dyn_cast<GlobalVariable>(GV)) { 222 if (Var->getSection() == "llvm.metadata") 223 Res |= BasicSymbolRef::SF_FormatSpecific; 224 } 225 226 return Res; 227 } 228