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