1 //===- SymbolTable.cpp ----------------------------------------------------===// 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 "SymbolTable.h" 10 #include "ConcatOutputSection.h" 11 #include "Config.h" 12 #include "InputFiles.h" 13 #include "InputSection.h" 14 #include "Symbols.h" 15 #include "SyntheticSections.h" 16 #include "lld/Common/ErrorHandler.h" 17 #include "lld/Common/Memory.h" 18 19 using namespace llvm; 20 using namespace lld; 21 using namespace lld::macho; 22 23 Symbol *SymbolTable::find(CachedHashStringRef cachedName) { 24 auto it = symMap.find(cachedName); 25 if (it == symMap.end()) 26 return nullptr; 27 return symVector[it->second]; 28 } 29 30 std::pair<Symbol *, bool> SymbolTable::insert(StringRef name, 31 const InputFile *file) { 32 auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()}); 33 34 Symbol *sym; 35 if (!p.second) { 36 // Name already present in the symbol table. 37 sym = symVector[p.first->second]; 38 } else { 39 // Name is a new symbol. 40 sym = reinterpret_cast<Symbol *>(make<SymbolUnion>()); 41 symVector.push_back(sym); 42 } 43 44 sym->isUsedInRegularObj |= !file || isa<ObjFile>(file); 45 return {sym, p.second}; 46 } 47 48 Defined *SymbolTable::addDefined(StringRef name, InputFile *file, 49 InputSection *isec, uint64_t value, 50 uint64_t size, bool isWeakDef, 51 bool isPrivateExtern, bool isThumb, 52 bool isReferencedDynamically, bool noDeadStrip, 53 bool isWeakDefCanBeHidden) { 54 Symbol *s; 55 bool wasInserted; 56 bool overridesWeakDef = false; 57 std::tie(s, wasInserted) = insert(name, file); 58 59 assert(!isWeakDef || (isa<BitcodeFile>(file) && !isec) || 60 (isa<ObjFile>(file) && file == isec->getFile())); 61 62 if (!wasInserted) { 63 if (auto *defined = dyn_cast<Defined>(s)) { 64 if (isWeakDef) { 65 // See further comment in createDefined() in InputFiles.cpp 66 if (defined->isWeakDef()) { 67 defined->privateExtern &= isPrivateExtern; 68 defined->weakDefCanBeHidden &= isWeakDefCanBeHidden; 69 defined->referencedDynamically |= isReferencedDynamically; 70 defined->noDeadStrip |= noDeadStrip; 71 } 72 // FIXME: Handle this for bitcode files. 73 if (auto concatIsec = dyn_cast_or_null<ConcatInputSection>(isec)) 74 concatIsec->wasCoalesced = true; 75 return defined; 76 } 77 78 if (defined->isWeakDef()) { 79 // FIXME: Handle this for bitcode files. 80 if (auto concatIsec = 81 dyn_cast_or_null<ConcatInputSection>(defined->isec)) { 82 concatIsec->wasCoalesced = true; 83 concatIsec->symbols.erase(llvm::find(concatIsec->symbols, defined)); 84 } 85 } else { 86 error("duplicate symbol: " + name + "\n>>> defined in " + 87 toString(defined->getFile()) + "\n>>> defined in " + 88 toString(file)); 89 } 90 91 } else if (auto *dysym = dyn_cast<DylibSymbol>(s)) { 92 overridesWeakDef = !isWeakDef && dysym->isWeakDef(); 93 dysym->unreference(); 94 } 95 // Defined symbols take priority over other types of symbols, so in case 96 // of a name conflict, we fall through to the replaceSymbol() call below. 97 } 98 99 Defined *defined = replaceSymbol<Defined>( 100 s, name, file, isec, value, size, isWeakDef, /*isExternal=*/true, 101 isPrivateExtern, isThumb, isReferencedDynamically, noDeadStrip, 102 overridesWeakDef, isWeakDefCanBeHidden); 103 return defined; 104 } 105 106 Symbol *SymbolTable::addUndefined(StringRef name, InputFile *file, 107 bool isWeakRef) { 108 Symbol *s; 109 bool wasInserted; 110 std::tie(s, wasInserted) = insert(name, file); 111 112 RefState refState = isWeakRef ? RefState::Weak : RefState::Strong; 113 114 if (wasInserted) 115 replaceSymbol<Undefined>(s, name, file, refState); 116 else if (auto *lazy = dyn_cast<LazySymbol>(s)) 117 lazy->fetchArchiveMember(); 118 else if (auto *dynsym = dyn_cast<DylibSymbol>(s)) 119 dynsym->reference(refState); 120 else if (auto *undefined = dyn_cast<Undefined>(s)) 121 undefined->refState = std::max(undefined->refState, refState); 122 return s; 123 } 124 125 Symbol *SymbolTable::addCommon(StringRef name, InputFile *file, uint64_t size, 126 uint32_t align, bool isPrivateExtern) { 127 Symbol *s; 128 bool wasInserted; 129 std::tie(s, wasInserted) = insert(name, file); 130 131 if (!wasInserted) { 132 if (auto *common = dyn_cast<CommonSymbol>(s)) { 133 if (size < common->size) 134 return s; 135 } else if (isa<Defined>(s)) { 136 return s; 137 } 138 // Common symbols take priority over all non-Defined symbols, so in case of 139 // a name conflict, we fall through to the replaceSymbol() call below. 140 } 141 142 replaceSymbol<CommonSymbol>(s, name, file, size, align, isPrivateExtern); 143 return s; 144 } 145 146 Symbol *SymbolTable::addDylib(StringRef name, DylibFile *file, bool isWeakDef, 147 bool isTlv) { 148 Symbol *s; 149 bool wasInserted; 150 std::tie(s, wasInserted) = insert(name, file); 151 152 RefState refState = RefState::Unreferenced; 153 if (!wasInserted) { 154 if (auto *defined = dyn_cast<Defined>(s)) { 155 if (isWeakDef && !defined->isWeakDef()) 156 defined->overridesWeakDef = true; 157 } else if (auto *undefined = dyn_cast<Undefined>(s)) { 158 refState = undefined->refState; 159 } else if (auto *dysym = dyn_cast<DylibSymbol>(s)) { 160 refState = dysym->getRefState(); 161 } 162 } 163 164 bool isDynamicLookup = file == nullptr; 165 if (wasInserted || isa<Undefined>(s) || 166 (isa<DylibSymbol>(s) && 167 ((!isWeakDef && s->isWeakDef()) || 168 (!isDynamicLookup && cast<DylibSymbol>(s)->isDynamicLookup())))) { 169 if (auto *dynsym = dyn_cast<DylibSymbol>(s)) 170 dynsym->unreference(); 171 replaceSymbol<DylibSymbol>(s, file, name, isWeakDef, refState, isTlv); 172 } 173 174 return s; 175 } 176 177 Symbol *SymbolTable::addDynamicLookup(StringRef name) { 178 return addDylib(name, /*file=*/nullptr, /*isWeakDef=*/false, /*isTlv=*/false); 179 } 180 181 Symbol *SymbolTable::addLazy(StringRef name, ArchiveFile *file, 182 const object::Archive::Symbol &sym) { 183 Symbol *s; 184 bool wasInserted; 185 std::tie(s, wasInserted) = insert(name, file); 186 187 if (wasInserted) 188 replaceSymbol<LazySymbol>(s, file, sym); 189 else if (isa<Undefined>(s) || (isa<DylibSymbol>(s) && s->isWeakDef())) 190 file->fetch(sym); 191 return s; 192 } 193 194 Defined *SymbolTable::addSynthetic(StringRef name, InputSection *isec, 195 uint64_t value, bool isPrivateExtern, 196 bool includeInSymtab, 197 bool referencedDynamically) { 198 Defined *s = 199 addDefined(name, nullptr, isec, value, /*size=*/0, 200 /*isWeakDef=*/false, isPrivateExtern, 201 /*isThumb=*/false, referencedDynamically, 202 /*noDeadStrip=*/false, /*isWeakDefCanBeHidden=*/false); 203 s->includeInSymtab = includeInSymtab; 204 return s; 205 } 206 207 enum class Boundary { 208 Start, 209 End, 210 }; 211 212 static Defined *createBoundarySymbol(const Undefined &sym) { 213 return symtab->addSynthetic( 214 sym.getName(), /*isec=*/nullptr, /*value=*/-1, /*isPrivateExtern=*/true, 215 /*includeInSymtab=*/false, /*referencedDynamically=*/false); 216 } 217 218 static void handleSectionBoundarySymbol(const Undefined &sym, StringRef segSect, 219 Boundary which) { 220 StringRef segName, sectName; 221 std::tie(segName, sectName) = segSect.split('$'); 222 223 // Attach the symbol to any InputSection that will end up in the right 224 // OutputSection -- it doesn't matter which one we pick. 225 // Don't bother looking through inputSections for a matching 226 // ConcatInputSection -- we need to create ConcatInputSection for 227 // non-existing sections anyways, and that codepath works even if we should 228 // already have a ConcatInputSection with the right name. 229 230 OutputSection *osec = nullptr; 231 // This looks for __TEXT,__cstring etc. 232 for (SyntheticSection *ssec : syntheticSections) 233 if (ssec->segname == segName && ssec->name == sectName) { 234 osec = ssec->isec->parent; 235 break; 236 } 237 238 if (!osec) { 239 ConcatInputSection *isec = make<ConcatInputSection>(segName, sectName); 240 241 // This runs after markLive() and is only called for Undefineds that are 242 // live. Marking the isec live ensures an OutputSection is created that the 243 // start/end symbol can refer to. 244 assert(sym.isLive()); 245 isec->live = true; 246 247 // This runs after gatherInputSections(), so need to explicitly set parent 248 // and add to inputSections. 249 osec = isec->parent = ConcatOutputSection::getOrCreateForInput(isec); 250 inputSections.push_back(isec); 251 } 252 253 if (which == Boundary::Start) 254 osec->sectionStartSymbols.push_back(createBoundarySymbol(sym)); 255 else 256 osec->sectionEndSymbols.push_back(createBoundarySymbol(sym)); 257 } 258 259 static void handleSegmentBoundarySymbol(const Undefined &sym, StringRef segName, 260 Boundary which) { 261 OutputSegment *seg = getOrCreateOutputSegment(segName); 262 if (which == Boundary::Start) 263 seg->segmentStartSymbols.push_back(createBoundarySymbol(sym)); 264 else 265 seg->segmentEndSymbols.push_back(createBoundarySymbol(sym)); 266 } 267 268 void lld::macho::treatUndefinedSymbol(const Undefined &sym, StringRef source) { 269 // Handle start/end symbols. 270 StringRef name = sym.getName(); 271 if (name.consume_front("section$start$")) 272 return handleSectionBoundarySymbol(sym, name, Boundary::Start); 273 if (name.consume_front("section$end$")) 274 return handleSectionBoundarySymbol(sym, name, Boundary::End); 275 if (name.consume_front("segment$start$")) 276 return handleSegmentBoundarySymbol(sym, name, Boundary::Start); 277 if (name.consume_front("segment$end$")) 278 return handleSegmentBoundarySymbol(sym, name, Boundary::End); 279 280 // Handle -U. 281 if (config->explicitDynamicLookups.count(sym.getName())) { 282 symtab->addDynamicLookup(sym.getName()); 283 return; 284 } 285 286 // Handle -undefined. 287 auto message = [source, &sym]() { 288 std::string message = "undefined symbol"; 289 if (config->archMultiple) 290 message += (" for arch " + getArchitectureName(config->arch())).str(); 291 message += ": " + toString(sym); 292 if (!source.empty()) 293 message += "\n>>> referenced by " + source.str(); 294 else 295 message += "\n>>> referenced by " + toString(sym.getFile()); 296 return message; 297 }; 298 switch (config->undefinedSymbolTreatment) { 299 case UndefinedSymbolTreatment::error: 300 error(message()); 301 break; 302 case UndefinedSymbolTreatment::warning: 303 warn(message()); 304 LLVM_FALLTHROUGH; 305 case UndefinedSymbolTreatment::dynamic_lookup: 306 case UndefinedSymbolTreatment::suppress: 307 symtab->addDynamicLookup(sym.getName()); 308 break; 309 case UndefinedSymbolTreatment::unknown: 310 llvm_unreachable("unknown -undefined TREATMENT"); 311 } 312 } 313 314 SymbolTable *macho::symtab; 315