1 //===-- RecordStreamer.cpp - Record asm defined and used symbols ----------===// 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 #include "RecordStreamer.h" 10 #include "llvm/IR/Mangler.h" 11 #include "llvm/IR/Module.h" 12 #include "llvm/MC/MCContext.h" 13 #include "llvm/MC/MCSymbol.h" 14 15 using namespace llvm; 16 17 void RecordStreamer::markDefined(const MCSymbol &Symbol) { 18 State &S = Symbols[Symbol.getName()]; 19 switch (S) { 20 case DefinedGlobal: 21 case Global: 22 S = DefinedGlobal; 23 break; 24 case NeverSeen: 25 case Defined: 26 case Used: 27 S = Defined; 28 break; 29 case DefinedWeak: 30 break; 31 case UndefinedWeak: 32 S = DefinedWeak; 33 } 34 } 35 36 void RecordStreamer::markGlobal(const MCSymbol &Symbol, 37 MCSymbolAttr Attribute) { 38 State &S = Symbols[Symbol.getName()]; 39 switch (S) { 40 case DefinedGlobal: 41 case Defined: 42 S = (Attribute == MCSA_Weak) ? DefinedWeak : DefinedGlobal; 43 break; 44 45 case NeverSeen: 46 case Global: 47 case Used: 48 S = (Attribute == MCSA_Weak) ? UndefinedWeak : Global; 49 break; 50 case UndefinedWeak: 51 case DefinedWeak: 52 break; 53 } 54 } 55 56 void RecordStreamer::markUsed(const MCSymbol &Symbol) { 57 State &S = Symbols[Symbol.getName()]; 58 switch (S) { 59 case DefinedGlobal: 60 case Defined: 61 case Global: 62 case DefinedWeak: 63 case UndefinedWeak: 64 break; 65 66 case NeverSeen: 67 case Used: 68 S = Used; 69 break; 70 } 71 } 72 73 void RecordStreamer::visitUsedSymbol(const MCSymbol &Sym) { markUsed(Sym); } 74 75 RecordStreamer::RecordStreamer(MCContext &Context, const Module &M) 76 : MCStreamer(Context), M(M) {} 77 78 RecordStreamer::const_iterator RecordStreamer::begin() { 79 return Symbols.begin(); 80 } 81 82 RecordStreamer::const_iterator RecordStreamer::end() { return Symbols.end(); } 83 84 void RecordStreamer::emitInstruction(const MCInst &Inst, 85 const MCSubtargetInfo &STI) { 86 MCStreamer::emitInstruction(Inst, STI); 87 } 88 89 void RecordStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) { 90 MCStreamer::emitLabel(Symbol); 91 markDefined(*Symbol); 92 } 93 94 void RecordStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) { 95 markDefined(*Symbol); 96 MCStreamer::emitAssignment(Symbol, Value); 97 } 98 99 bool RecordStreamer::emitSymbolAttribute(MCSymbol *Symbol, 100 MCSymbolAttr Attribute) { 101 if (Attribute == MCSA_Global || Attribute == MCSA_Weak) 102 markGlobal(*Symbol, Attribute); 103 if (Attribute == MCSA_LazyReference) 104 markUsed(*Symbol); 105 return true; 106 } 107 108 void RecordStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol, 109 uint64_t Size, unsigned ByteAlignment, 110 SMLoc Loc) { 111 markDefined(*Symbol); 112 } 113 114 void RecordStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 115 unsigned ByteAlignment) { 116 markDefined(*Symbol); 117 } 118 119 RecordStreamer::State RecordStreamer::getSymbolState(const MCSymbol *Sym) { 120 auto SI = Symbols.find(Sym->getName()); 121 if (SI == Symbols.end()) 122 return NeverSeen; 123 return SI->second; 124 } 125 126 void RecordStreamer::emitELFSymverDirective(const MCSymbol *OriginalSym, 127 StringRef Name, 128 bool KeepOriginalSym) { 129 SymverAliasMap[OriginalSym].push_back(Name); 130 } 131 132 iterator_range<RecordStreamer::const_symver_iterator> 133 RecordStreamer::symverAliases() { 134 return {SymverAliasMap.begin(), SymverAliasMap.end()}; 135 } 136 137 void RecordStreamer::flushSymverDirectives() { 138 // Mapping from mangled name to GV. 139 StringMap<const GlobalValue *> MangledNameMap; 140 // The name in the assembler will be mangled, but the name in the IR 141 // might not, so we first compute a mapping from mangled name to GV. 142 Mangler Mang; 143 SmallString<64> MangledName; 144 for (const GlobalValue &GV : M.global_values()) { 145 if (!GV.hasName()) 146 continue; 147 MangledName.clear(); 148 MangledName.reserve(GV.getName().size() + 1); 149 Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false); 150 MangledNameMap[MangledName] = &GV; 151 } 152 153 // Walk all the recorded .symver aliases, and set up the binding 154 // for each alias. 155 for (auto &Symver : SymverAliasMap) { 156 const MCSymbol *Aliasee = Symver.first; 157 MCSymbolAttr Attr = MCSA_Invalid; 158 bool IsDefined = false; 159 160 // First check if the aliasee binding was recorded in the asm. 161 RecordStreamer::State state = getSymbolState(Aliasee); 162 switch (state) { 163 case RecordStreamer::Global: 164 case RecordStreamer::DefinedGlobal: 165 Attr = MCSA_Global; 166 break; 167 case RecordStreamer::UndefinedWeak: 168 case RecordStreamer::DefinedWeak: 169 Attr = MCSA_Weak; 170 break; 171 default: 172 break; 173 } 174 175 switch (state) { 176 case RecordStreamer::Defined: 177 case RecordStreamer::DefinedGlobal: 178 case RecordStreamer::DefinedWeak: 179 IsDefined = true; 180 break; 181 case RecordStreamer::NeverSeen: 182 case RecordStreamer::Global: 183 case RecordStreamer::Used: 184 case RecordStreamer::UndefinedWeak: 185 break; 186 } 187 188 if (Attr == MCSA_Invalid || !IsDefined) { 189 const GlobalValue *GV = M.getNamedValue(Aliasee->getName()); 190 if (!GV) { 191 auto MI = MangledNameMap.find(Aliasee->getName()); 192 if (MI != MangledNameMap.end()) 193 GV = MI->second; 194 } 195 if (GV) { 196 // If we don't have a symbol attribute from assembly, then check if 197 // the aliasee was defined in the IR. 198 if (Attr == MCSA_Invalid) { 199 if (GV->hasExternalLinkage()) 200 Attr = MCSA_Global; 201 else if (GV->hasLocalLinkage()) 202 Attr = MCSA_Local; 203 else if (GV->isWeakForLinker()) 204 Attr = MCSA_Weak; 205 } 206 IsDefined = IsDefined || !GV->isDeclarationForLinker(); 207 } 208 } 209 210 // Set the detected binding on each alias with this aliasee. 211 for (auto AliasName : Symver.second) { 212 std::pair<StringRef, StringRef> Split = AliasName.split("@@@"); 213 SmallString<128> NewName; 214 if (!Split.second.empty() && !Split.second.startswith("@")) { 215 // Special processing for "@@@" according 216 // https://sourceware.org/binutils/docs/as/Symver.html 217 const char *Separator = IsDefined ? "@@" : "@"; 218 AliasName = 219 (Split.first + Separator + Split.second).toStringRef(NewName); 220 } 221 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName); 222 // TODO: Handle "@@@". Depending on SymbolAttribute value it needs to be 223 // converted into @ or @@. 224 const MCExpr *Value = MCSymbolRefExpr::create(Aliasee, getContext()); 225 if (IsDefined) 226 markDefined(*Alias); 227 // Don't use EmitAssignment override as it always marks alias as defined. 228 MCStreamer::emitAssignment(Alias, Value); 229 if (Attr != MCSA_Invalid) 230 emitSymbolAttribute(Alias, Attr); 231 } 232 } 233 } 234