10b57cec5SDimitry Andric //===- Symbols.h ------------------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file defines various types of Symbols. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #ifndef LLD_ELF_SYMBOLS_H 140b57cec5SDimitry Andric #define LLD_ELF_SYMBOLS_H 150b57cec5SDimitry Andric 1681ad6265SDimitry Andric #include "Config.h" 170b57cec5SDimitry Andric #include "lld/Common/LLVM.h" 180eae32dcSDimitry Andric #include "lld/Common/Memory.h" 195ffd83dbSDimitry Andric #include "llvm/ADT/DenseMap.h" 200b57cec5SDimitry Andric #include "llvm/Object/ELF.h" 21bdd1243dSDimitry Andric #include "llvm/Support/Compiler.h" 22349cc55cSDimitry Andric #include <tuple> 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric namespace lld { 2581ad6265SDimitry Andric namespace elf { 2681ad6265SDimitry Andric class Symbol; 2781ad6265SDimitry Andric } 285ffd83dbSDimitry Andric // Returns a string representation for a symbol for diagnostics. 2985868e8aSDimitry Andric std::string toString(const elf::Symbol &); 3085868e8aSDimitry Andric 310b57cec5SDimitry Andric namespace elf { 320b57cec5SDimitry Andric class CommonSymbol; 330b57cec5SDimitry Andric class Defined; 3481ad6265SDimitry Andric class OutputSection; 3581ad6265SDimitry Andric class SectionBase; 3681ad6265SDimitry Andric class InputSectionBase; 370b57cec5SDimitry Andric class SharedSymbol; 380b57cec5SDimitry Andric class Symbol; 390b57cec5SDimitry Andric class Undefined; 407a6dacacSDimitry Andric class LazySymbol; 4181ad6265SDimitry Andric class InputFile; 420b57cec5SDimitry Andric 43bdd1243dSDimitry Andric void printTraceSymbol(const Symbol &sym, StringRef name); 44bdd1243dSDimitry Andric 45bdd1243dSDimitry Andric enum { 46bdd1243dSDimitry Andric NEEDS_GOT = 1 << 0, 47bdd1243dSDimitry Andric NEEDS_PLT = 1 << 1, 48bdd1243dSDimitry Andric HAS_DIRECT_RELOC = 1 << 2, 49bdd1243dSDimitry Andric // True if this symbol needs a canonical PLT entry, or (during 50bdd1243dSDimitry Andric // postScanRelocations) a copy relocation. 51bdd1243dSDimitry Andric NEEDS_COPY = 1 << 3, 52bdd1243dSDimitry Andric NEEDS_TLSDESC = 1 << 4, 53bdd1243dSDimitry Andric NEEDS_TLSGD = 1 << 5, 54bdd1243dSDimitry Andric NEEDS_TLSGD_TO_IE = 1 << 6, 55bdd1243dSDimitry Andric NEEDS_GOT_DTPREL = 1 << 7, 56bdd1243dSDimitry Andric NEEDS_TLSIE = 1 << 8, 57bdd1243dSDimitry Andric }; 58bdd1243dSDimitry Andric 5904eeddc0SDimitry Andric // Some index properties of a symbol are stored separately in this auxiliary 6004eeddc0SDimitry Andric // struct to decrease sizeof(SymbolUnion) in the majority of cases. 6104eeddc0SDimitry Andric struct SymbolAux { 6204eeddc0SDimitry Andric uint32_t gotIdx = -1; 6304eeddc0SDimitry Andric uint32_t pltIdx = -1; 6404eeddc0SDimitry Andric uint32_t tlsDescIdx = -1; 6504eeddc0SDimitry Andric uint32_t tlsGdIdx = -1; 660b57cec5SDimitry Andric }; 670b57cec5SDimitry Andric 68bdd1243dSDimitry Andric LLVM_LIBRARY_VISIBILITY extern SmallVector<SymbolAux, 0> symAux; 6904eeddc0SDimitry Andric 700b57cec5SDimitry Andric // The base class for real symbol classes. 710b57cec5SDimitry Andric class Symbol { 720b57cec5SDimitry Andric public: 730b57cec5SDimitry Andric enum Kind { 740b57cec5SDimitry Andric PlaceholderKind, 750b57cec5SDimitry Andric DefinedKind, 760b57cec5SDimitry Andric CommonKind, 770b57cec5SDimitry Andric SharedKind, 780b57cec5SDimitry Andric UndefinedKind, 797a6dacacSDimitry Andric LazyKind, 800b57cec5SDimitry Andric }; 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric Kind kind() const { return static_cast<Kind>(symbolKind); } 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric // The file from which this symbol was created. 850b57cec5SDimitry Andric InputFile *file; 860b57cec5SDimitry Andric 87bdd1243dSDimitry Andric // The default copy constructor is deleted due to atomic flags. Define one for 88bdd1243dSDimitry Andric // places where no atomic is needed. 89bdd1243dSDimitry Andric Symbol(const Symbol &o) { memcpy(this, &o, sizeof(o)); } 90bdd1243dSDimitry Andric 910b57cec5SDimitry Andric protected: 920b57cec5SDimitry Andric const char *nameData; 9304eeddc0SDimitry Andric // 32-bit size saves space. 9404eeddc0SDimitry Andric uint32_t nameSize; 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric public: 9781ad6265SDimitry Andric // The next three fields have the same meaning as the ELF symbol attributes. 9881ad6265SDimitry Andric // type and binding are placed in this order to optimize generating st_info, 9981ad6265SDimitry Andric // which is defined as (binding << 4) + (type & 0xf), on a little-endian 10081ad6265SDimitry Andric // system. 10181ad6265SDimitry Andric uint8_t type : 4; // symbol type 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric // Symbol binding. This is not overwritten by replace() to track 1040b57cec5SDimitry Andric // changes during resolution. In particular: 1050b57cec5SDimitry Andric // - An undefined weak is still weak when it resolves to a shared library. 1064824e7fdSDimitry Andric // - An undefined weak will not extract archive members, but we have to 1070b57cec5SDimitry Andric // remember it is weak. 10881ad6265SDimitry Andric uint8_t binding : 4; 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric uint8_t stOther; // st_other field value 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric uint8_t symbolKind; 1130b57cec5SDimitry Andric 11481ad6265SDimitry Andric // The partition whose dynamic symbol table contains this symbol's definition. 115bdd1243dSDimitry Andric uint8_t partition; 1160b57cec5SDimitry Andric 11781ad6265SDimitry Andric // True if this symbol is preemptible at load time. 118*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 11981ad6265SDimitry Andric uint8_t isPreemptible : 1; 12081ad6265SDimitry Andric 1210b57cec5SDimitry Andric // True if the symbol was used for linking and thus need to be added to the 1220b57cec5SDimitry Andric // output file's symbol table. This is true for all symbols except for 1230b57cec5SDimitry Andric // unreferenced DSO symbols, lazy (archive) symbols, and bitcode symbols that 1240b57cec5SDimitry Andric // are unreferenced except by other bitcode objects. 125*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 126480093f4SDimitry Andric uint8_t isUsedInRegularObj : 1; 1270b57cec5SDimitry Andric 12881ad6265SDimitry Andric // True if an undefined or shared symbol is used from a live section. 12981ad6265SDimitry Andric // 13081ad6265SDimitry Andric // NOTE: In Writer.cpp the field is used to mark local defined symbols 13181ad6265SDimitry Andric // which are referenced by relocations when -r or --emit-relocs is given. 132*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 13381ad6265SDimitry Andric uint8_t used : 1; 13481ad6265SDimitry Andric 13585868e8aSDimitry Andric // Used by a Defined symbol with protected or default visibility, to record 13685868e8aSDimitry Andric // whether it is required to be exported into .dynsym. This is set when any of 13785868e8aSDimitry Andric // the following conditions hold: 13885868e8aSDimitry Andric // 13981ad6265SDimitry Andric // - If there is an interposable symbol from a DSO. Note: We also do this for 14081ad6265SDimitry Andric // STV_PROTECTED symbols which can't be interposed (to match BFD behavior). 14185868e8aSDimitry Andric // - If -shared or --export-dynamic is specified, any symbol in an object 14285868e8aSDimitry Andric // file/bitcode sets this property, unless suppressed by LTO 14385868e8aSDimitry Andric // canBeOmittedFromSymbolTable(). 144*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 145480093f4SDimitry Andric uint8_t exportDynamic : 1; 14685868e8aSDimitry Andric 14785868e8aSDimitry Andric // True if the symbol is in the --dynamic-list file. A Defined symbol with 14885868e8aSDimitry Andric // protected or default visibility with this property is required to be 14985868e8aSDimitry Andric // exported into .dynsym. 150*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 151480093f4SDimitry Andric uint8_t inDynamicList : 1; 1520b57cec5SDimitry Andric 153e8d8bef9SDimitry Andric // Used to track if there has been at least one undefined reference to the 154e8d8bef9SDimitry Andric // symbol. For Undefined and SharedSymbol, the binding may change to STB_WEAK 155e8d8bef9SDimitry Andric // if the first undefined reference from a non-shared object is weak. 156*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 157480093f4SDimitry Andric uint8_t referenced : 1; 1580b57cec5SDimitry Andric 15981ad6265SDimitry Andric // Used to track if this symbol will be referenced after wrapping is performed 16081ad6265SDimitry Andric // (i.e. this will be true for foo if __real_foo is referenced, and will be 16181ad6265SDimitry Andric // true for __wrap_foo if foo is referenced). 162*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 16381ad6265SDimitry Andric uint8_t referencedAfterWrap : 1; 16481ad6265SDimitry Andric 1650b57cec5SDimitry Andric // True if this symbol is specified by --trace-symbol option. 166*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 167480093f4SDimitry Andric uint8_t traced : 1; 1680b57cec5SDimitry Andric 16904eeddc0SDimitry Andric // True if the name contains '@'. 170*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 17104eeddc0SDimitry Andric uint8_t hasVersionSuffix : 1; 17204eeddc0SDimitry Andric 173bdd1243dSDimitry Andric // Symbol visibility. This is the computed minimum visibility of all 174bdd1243dSDimitry Andric // observed non-DSO symbols. 175bdd1243dSDimitry Andric uint8_t visibility() const { return stOther & 3; } 176bdd1243dSDimitry Andric void setVisibility(uint8_t visibility) { 177bdd1243dSDimitry Andric stOther = (stOther & ~3) | visibility; 178bdd1243dSDimitry Andric } 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric bool includeInDynsym() const; 1810b57cec5SDimitry Andric uint8_t computeBinding() const; 18281ad6265SDimitry Andric bool isGlobal() const { return binding == llvm::ELF::STB_GLOBAL; } 1830b57cec5SDimitry Andric bool isWeak() const { return binding == llvm::ELF::STB_WEAK; } 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric bool isUndefined() const { return symbolKind == UndefinedKind; } 1860b57cec5SDimitry Andric bool isCommon() const { return symbolKind == CommonKind; } 1870b57cec5SDimitry Andric bool isDefined() const { return symbolKind == DefinedKind; } 1880b57cec5SDimitry Andric bool isShared() const { return symbolKind == SharedKind; } 1890b57cec5SDimitry Andric bool isPlaceholder() const { return symbolKind == PlaceholderKind; } 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric bool isLocal() const { return binding == llvm::ELF::STB_LOCAL; } 1920b57cec5SDimitry Andric 1937a6dacacSDimitry Andric bool isLazy() const { return symbolKind == LazyKind; } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric // True if this is an undefined weak symbol. This only works once 1960b57cec5SDimitry Andric // all input files have been added. 197349cc55cSDimitry Andric bool isUndefWeak() const { return isWeak() && isUndefined(); } 1980b57cec5SDimitry Andric 19904eeddc0SDimitry Andric StringRef getName() const { return {nameData, nameSize}; } 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric void setName(StringRef s) { 2020b57cec5SDimitry Andric nameData = s.data(); 2030b57cec5SDimitry Andric nameSize = s.size(); 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric void parseSymbolVersion(); 2070b57cec5SDimitry Andric 208e8d8bef9SDimitry Andric // Get the NUL-terminated version suffix ("", "@...", or "@@..."). 209e8d8bef9SDimitry Andric // 210e8d8bef9SDimitry Andric // For @@, the name has been truncated by insert(). For @, the name has been 211e8d8bef9SDimitry Andric // truncated by Symbol::parseSymbolVersion(). 21204eeddc0SDimitry Andric const char *getVersionSuffix() const { return nameData + nameSize; } 21304eeddc0SDimitry Andric 214bdd1243dSDimitry Andric uint32_t getGotIdx() const { return symAux[auxIdx].gotIdx; } 215bdd1243dSDimitry Andric uint32_t getPltIdx() const { return symAux[auxIdx].pltIdx; } 216bdd1243dSDimitry Andric uint32_t getTlsDescIdx() const { return symAux[auxIdx].tlsDescIdx; } 217bdd1243dSDimitry Andric uint32_t getTlsGdIdx() const { return symAux[auxIdx].tlsGdIdx; } 218e8d8bef9SDimitry Andric 21904eeddc0SDimitry Andric bool isInGot() const { return getGotIdx() != uint32_t(-1); } 22004eeddc0SDimitry Andric bool isInPlt() const { return getPltIdx() != uint32_t(-1); } 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric uint64_t getVA(int64_t addend = 0) const; 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric uint64_t getGotOffset() const; 2250b57cec5SDimitry Andric uint64_t getGotVA() const; 2260b57cec5SDimitry Andric uint64_t getGotPltOffset() const; 2270b57cec5SDimitry Andric uint64_t getGotPltVA() const; 2280b57cec5SDimitry Andric uint64_t getPltVA() const; 2290b57cec5SDimitry Andric uint64_t getSize() const; 2300b57cec5SDimitry Andric OutputSection *getOutputSection() const; 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric // The following two functions are used for symbol resolution. 2330b57cec5SDimitry Andric // 2340b57cec5SDimitry Andric // You are expected to call mergeProperties for all symbols in input 2350b57cec5SDimitry Andric // files so that attributes that are attached to names rather than 2360b57cec5SDimitry Andric // indivisual symbol (such as visibility) are merged together. 2370b57cec5SDimitry Andric // 2380b57cec5SDimitry Andric // Every time you read a new symbol from an input, you are supposed 2390b57cec5SDimitry Andric // to call resolve() with the new symbol. That function replaces 2400b57cec5SDimitry Andric // "this" object as a result of name resolution if the new symbol is 2410b57cec5SDimitry Andric // more appropriate to be included in the output. 2420b57cec5SDimitry Andric // 2430b57cec5SDimitry Andric // For example, if "this" is an undefined symbol and a new symbol is 2440b57cec5SDimitry Andric // a defined symbol, "this" is replaced with the new symbol. 2450b57cec5SDimitry Andric void mergeProperties(const Symbol &other); 246bdd1243dSDimitry Andric void resolve(const Undefined &other); 247bdd1243dSDimitry Andric void resolve(const CommonSymbol &other); 248bdd1243dSDimitry Andric void resolve(const Defined &other); 2497a6dacacSDimitry Andric void resolve(const LazySymbol &other); 250bdd1243dSDimitry Andric void resolve(const SharedSymbol &other); 2510b57cec5SDimitry Andric 2524824e7fdSDimitry Andric // If this is a lazy symbol, extract an input file and add the symbol 2530b57cec5SDimitry Andric // in the file to the symbol table. Calling this function on 2540b57cec5SDimitry Andric // non-lazy object causes a runtime error. 2554824e7fdSDimitry Andric void extract() const; 2560b57cec5SDimitry Andric 25781ad6265SDimitry Andric void checkDuplicate(const Defined &other) const; 2580b57cec5SDimitry Andric 259fe6060f1SDimitry Andric private: 26081ad6265SDimitry Andric bool shouldReplace(const Defined &other) const; 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric protected: 26304eeddc0SDimitry Andric Symbol(Kind k, InputFile *file, StringRef name, uint8_t binding, 2640b57cec5SDimitry Andric uint8_t stOther, uint8_t type) 26581ad6265SDimitry Andric : file(file), nameData(name.data()), nameSize(name.size()), type(type), 2665f757f3fSDimitry Andric binding(binding), stOther(stOther), symbolKind(k), exportDynamic(false), 2675f757f3fSDimitry Andric archSpecificBit(false) {} 268bdd1243dSDimitry Andric 269bdd1243dSDimitry Andric void overwrite(Symbol &sym, Kind k) const { 270bdd1243dSDimitry Andric if (sym.traced) 271bdd1243dSDimitry Andric printTraceSymbol(*this, sym.getName()); 272bdd1243dSDimitry Andric sym.file = file; 273bdd1243dSDimitry Andric sym.type = type; 274bdd1243dSDimitry Andric sym.binding = binding; 275bdd1243dSDimitry Andric sym.stOther = (stOther & ~3) | sym.visibility(); 276bdd1243dSDimitry Andric sym.symbolKind = k; 277bdd1243dSDimitry Andric } 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric public: 2800b57cec5SDimitry Andric // True if this symbol is in the Iplt sub-section of the Plt and the Igot 2810b57cec5SDimitry Andric // sub-section of the .got.plt or .got. 282*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 283480093f4SDimitry Andric uint8_t isInIplt : 1; 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric // True if this symbol needs a GOT entry and its GOT entry is actually in 2860b57cec5SDimitry Andric // Igot. This will be true only for certain non-preemptible ifuncs. 287*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 288480093f4SDimitry Andric uint8_t gotInIgot : 1; 2890b57cec5SDimitry Andric 2900eae32dcSDimitry Andric // True if defined relative to a section discarded by ICF. 291*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 2920eae32dcSDimitry Andric uint8_t folded : 1; 2930eae32dcSDimitry Andric 2945f757f3fSDimitry Andric // Allow reuse of a bit between architecture-exclusive symbol flags. 2955f757f3fSDimitry Andric // - needsTocRestore(): On PPC64, true if a call to this symbol needs to be 2965f757f3fSDimitry Andric // followed by a restore of the toc pointer. 2975f757f3fSDimitry Andric // - isTagged(): On AArch64, true if the symbol needs special relocation and 2985f757f3fSDimitry Andric // metadata semantics because it's tagged, under the AArch64 MemtagABI. 299*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 3005f757f3fSDimitry Andric uint8_t archSpecificBit : 1; 3015f757f3fSDimitry Andric bool needsTocRestore() const { return archSpecificBit; } 3025f757f3fSDimitry Andric bool isTagged() const { return archSpecificBit; } 3035f757f3fSDimitry Andric void setNeedsTocRestore(bool v) { archSpecificBit = v; } 3045f757f3fSDimitry Andric void setIsTagged(bool v) { 3055f757f3fSDimitry Andric archSpecificBit = v; 3065f757f3fSDimitry Andric } 3070b57cec5SDimitry Andric 30881ad6265SDimitry Andric // True if this symbol is defined by a symbol assignment or wrapped by --wrap. 30981ad6265SDimitry Andric // 31081ad6265SDimitry Andric // LTO shouldn't inline the symbol because it doesn't know the final content 31181ad6265SDimitry Andric // of the symbol. 312*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 313480093f4SDimitry Andric uint8_t scriptDefined : 1; 3140b57cec5SDimitry Andric 3157a6dacacSDimitry Andric // True if defined in a DSO. There may also be a definition in a relocatable 3167a6dacacSDimitry Andric // object file. 317*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 3187a6dacacSDimitry Andric uint8_t dsoDefined : 1; 3197a6dacacSDimitry Andric 320bdd1243dSDimitry Andric // True if defined in a DSO as protected visibility. 321*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 322bdd1243dSDimitry Andric uint8_t dsoProtected : 1; 3230eae32dcSDimitry Andric 3240eae32dcSDimitry Andric // Temporary flags used to communicate which symbol entries need PLT and GOT 3250eae32dcSDimitry Andric // entries during postScanRelocations(); 326bdd1243dSDimitry Andric std::atomic<uint16_t> flags; 3270eae32dcSDimitry Andric 32881ad6265SDimitry Andric // A symAux index used to access GOT/PLT entry indexes. This is allocated in 32981ad6265SDimitry Andric // postScanRelocations(). 330bdd1243dSDimitry Andric uint32_t auxIdx; 331bdd1243dSDimitry Andric uint32_t dynsymIndex; 33281ad6265SDimitry Andric 3335f757f3fSDimitry Andric // If `file` is SharedFile (for SharedSymbol or copy-relocated Defined), this 3345f757f3fSDimitry Andric // represents the Verdef index within the input DSO, which will be converted 3355f757f3fSDimitry Andric // to a Verneed index in the output. Otherwise, this represents the Verdef 3365f757f3fSDimitry Andric // index (VER_NDX_LOCAL, VER_NDX_GLOBAL, or a named version). 33781ad6265SDimitry Andric uint16_t versionId; 338*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 3395f757f3fSDimitry Andric uint8_t versionScriptAssigned : 1; 34081ad6265SDimitry Andric 3417a6dacacSDimitry Andric // True if targeted by a range extension thunk. 342*0fca6ea1SDimitry Andric LLVM_PREFERRED_TYPE(bool) 3437a6dacacSDimitry Andric uint8_t thunkAccessed : 1; 3447a6dacacSDimitry Andric 345bdd1243dSDimitry Andric void setFlags(uint16_t bits) { 346bdd1243dSDimitry Andric flags.fetch_or(bits, std::memory_order_relaxed); 347bdd1243dSDimitry Andric } 348bdd1243dSDimitry Andric bool hasFlag(uint16_t bit) const { 349bdd1243dSDimitry Andric assert(bit && (bit & (bit - 1)) == 0 && "bit must be a power of 2"); 350bdd1243dSDimitry Andric return flags.load(std::memory_order_relaxed) & bit; 351bdd1243dSDimitry Andric } 352bdd1243dSDimitry Andric 35304eeddc0SDimitry Andric bool needsDynReloc() const { 354bdd1243dSDimitry Andric return flags.load(std::memory_order_relaxed) & 355bdd1243dSDimitry Andric (NEEDS_COPY | NEEDS_GOT | NEEDS_PLT | NEEDS_TLSDESC | NEEDS_TLSGD | 356bdd1243dSDimitry Andric NEEDS_TLSGD_TO_IE | NEEDS_GOT_DTPREL | NEEDS_TLSIE); 35704eeddc0SDimitry Andric } 35804eeddc0SDimitry Andric void allocateAux() { 359bdd1243dSDimitry Andric assert(auxIdx == 0); 36004eeddc0SDimitry Andric auxIdx = symAux.size(); 36104eeddc0SDimitry Andric symAux.emplace_back(); 36204eeddc0SDimitry Andric } 36304eeddc0SDimitry Andric 3640b57cec5SDimitry Andric bool isSection() const { return type == llvm::ELF::STT_SECTION; } 3650b57cec5SDimitry Andric bool isTls() const { return type == llvm::ELF::STT_TLS; } 3660b57cec5SDimitry Andric bool isFunc() const { return type == llvm::ELF::STT_FUNC; } 3670b57cec5SDimitry Andric bool isGnuIFunc() const { return type == llvm::ELF::STT_GNU_IFUNC; } 3680b57cec5SDimitry Andric bool isObject() const { return type == llvm::ELF::STT_OBJECT; } 3690b57cec5SDimitry Andric bool isFile() const { return type == llvm::ELF::STT_FILE; } 3700b57cec5SDimitry Andric }; 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric // Represents a symbol that is defined in the current output file. 3730b57cec5SDimitry Andric class Defined : public Symbol { 3740b57cec5SDimitry Andric public: 37504eeddc0SDimitry Andric Defined(InputFile *file, StringRef name, uint8_t binding, uint8_t stOther, 3760b57cec5SDimitry Andric uint8_t type, uint64_t value, uint64_t size, SectionBase *section) 3770b57cec5SDimitry Andric : Symbol(DefinedKind, file, name, binding, stOther, type), value(value), 37881ad6265SDimitry Andric size(size), section(section) { 37981ad6265SDimitry Andric exportDynamic = config->exportDynamic; 38081ad6265SDimitry Andric } 3815f757f3fSDimitry Andric void overwrite(Symbol &sym) const; 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric static bool classof(const Symbol *s) { return s->isDefined(); } 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric uint64_t value; 3860b57cec5SDimitry Andric uint64_t size; 3870b57cec5SDimitry Andric SectionBase *section; 3880b57cec5SDimitry Andric }; 3890b57cec5SDimitry Andric 3900b57cec5SDimitry Andric // Represents a common symbol. 3910b57cec5SDimitry Andric // 3920b57cec5SDimitry Andric // On Unix, it is traditionally allowed to write variable definitions 3930b57cec5SDimitry Andric // without initialization expressions (such as "int foo;") to header 3940b57cec5SDimitry Andric // files. Such definition is called "tentative definition". 3950b57cec5SDimitry Andric // 3960b57cec5SDimitry Andric // Using tentative definition is usually considered a bad practice 3970b57cec5SDimitry Andric // because you should write only declarations (such as "extern int 3980b57cec5SDimitry Andric // foo;") to header files. Nevertheless, the linker and the compiler 3990b57cec5SDimitry Andric // have to do something to support bad code by allowing duplicate 4000b57cec5SDimitry Andric // definitions for this particular case. 4010b57cec5SDimitry Andric // 4020b57cec5SDimitry Andric // Common symbols represent variable definitions without initializations. 403480093f4SDimitry Andric // The compiler creates common symbols when it sees variable definitions 4040b57cec5SDimitry Andric // without initialization (you can suppress this behavior and let the 4050b57cec5SDimitry Andric // compiler create a regular defined symbol by -fno-common). 4060b57cec5SDimitry Andric // 4070b57cec5SDimitry Andric // The linker allows common symbols to be replaced by regular defined 4080b57cec5SDimitry Andric // symbols. If there are remaining common symbols after name resolution is 4090b57cec5SDimitry Andric // complete, they are converted to regular defined symbols in a .bss 4100b57cec5SDimitry Andric // section. (Therefore, the later passes don't see any CommonSymbols.) 4110b57cec5SDimitry Andric class CommonSymbol : public Symbol { 4120b57cec5SDimitry Andric public: 41304eeddc0SDimitry Andric CommonSymbol(InputFile *file, StringRef name, uint8_t binding, 4140b57cec5SDimitry Andric uint8_t stOther, uint8_t type, uint64_t alignment, uint64_t size) 4150b57cec5SDimitry Andric : Symbol(CommonKind, file, name, binding, stOther, type), 41681ad6265SDimitry Andric alignment(alignment), size(size) { 41781ad6265SDimitry Andric exportDynamic = config->exportDynamic; 41881ad6265SDimitry Andric } 419bdd1243dSDimitry Andric void overwrite(Symbol &sym) const { 420bdd1243dSDimitry Andric Symbol::overwrite(sym, CommonKind); 421bdd1243dSDimitry Andric auto &s = static_cast<CommonSymbol &>(sym); 422bdd1243dSDimitry Andric s.alignment = alignment; 423bdd1243dSDimitry Andric s.size = size; 424bdd1243dSDimitry Andric } 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric static bool classof(const Symbol *s) { return s->isCommon(); } 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric uint32_t alignment; 4290b57cec5SDimitry Andric uint64_t size; 4300b57cec5SDimitry Andric }; 4310b57cec5SDimitry Andric 4320b57cec5SDimitry Andric class Undefined : public Symbol { 4330b57cec5SDimitry Andric public: 43404eeddc0SDimitry Andric Undefined(InputFile *file, StringRef name, uint8_t binding, uint8_t stOther, 4350b57cec5SDimitry Andric uint8_t type, uint32_t discardedSecIdx = 0) 4360b57cec5SDimitry Andric : Symbol(UndefinedKind, file, name, binding, stOther, type), 4370b57cec5SDimitry Andric discardedSecIdx(discardedSecIdx) {} 438bdd1243dSDimitry Andric void overwrite(Symbol &sym) const { 439bdd1243dSDimitry Andric Symbol::overwrite(sym, UndefinedKind); 440bdd1243dSDimitry Andric auto &s = static_cast<Undefined &>(sym); 441bdd1243dSDimitry Andric s.discardedSecIdx = discardedSecIdx; 442bdd1243dSDimitry Andric s.nonPrevailing = nonPrevailing; 443bdd1243dSDimitry Andric } 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric static bool classof(const Symbol *s) { return s->kind() == UndefinedKind; } 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric // The section index if in a discarded section, 0 otherwise. 4480b57cec5SDimitry Andric uint32_t discardedSecIdx; 44981ad6265SDimitry Andric bool nonPrevailing = false; 4500b57cec5SDimitry Andric }; 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric class SharedSymbol : public Symbol { 4530b57cec5SDimitry Andric public: 4540b57cec5SDimitry Andric static bool classof(const Symbol *s) { return s->kind() == SharedKind; } 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric SharedSymbol(InputFile &file, StringRef name, uint8_t binding, 4570b57cec5SDimitry Andric uint8_t stOther, uint8_t type, uint64_t value, uint64_t size, 45881ad6265SDimitry Andric uint32_t alignment) 4590b57cec5SDimitry Andric : Symbol(SharedKind, &file, name, binding, stOther, type), value(value), 4600b57cec5SDimitry Andric size(size), alignment(alignment) { 46181ad6265SDimitry Andric exportDynamic = true; 462bdd1243dSDimitry Andric dsoProtected = visibility() == llvm::ELF::STV_PROTECTED; 4630b57cec5SDimitry Andric // GNU ifunc is a mechanism to allow user-supplied functions to 4640b57cec5SDimitry Andric // resolve PLT slot values at load-time. This is contrary to the 4650b57cec5SDimitry Andric // regular symbol resolution scheme in which symbols are resolved just 4660b57cec5SDimitry Andric // by name. Using this hook, you can program how symbols are solved 4670b57cec5SDimitry Andric // for you program. For example, you can make "memcpy" to be resolved 4680b57cec5SDimitry Andric // to a SSE-enabled version of memcpy only when a machine running the 4690b57cec5SDimitry Andric // program supports the SSE instruction set. 4700b57cec5SDimitry Andric // 4710b57cec5SDimitry Andric // Naturally, such symbols should always be called through their PLT 4720b57cec5SDimitry Andric // slots. What GNU ifunc symbols point to are resolver functions, and 4730b57cec5SDimitry Andric // calling them directly doesn't make sense (unless you are writing a 4740b57cec5SDimitry Andric // loader). 4750b57cec5SDimitry Andric // 4760b57cec5SDimitry Andric // For DSO symbols, we always call them through PLT slots anyway. 4770b57cec5SDimitry Andric // So there's no difference between GNU ifunc and regular function 4780b57cec5SDimitry Andric // symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC. 4790b57cec5SDimitry Andric if (this->type == llvm::ELF::STT_GNU_IFUNC) 4800b57cec5SDimitry Andric this->type = llvm::ELF::STT_FUNC; 4810b57cec5SDimitry Andric } 482bdd1243dSDimitry Andric void overwrite(Symbol &sym) const { 483bdd1243dSDimitry Andric Symbol::overwrite(sym, SharedKind); 484bdd1243dSDimitry Andric auto &s = static_cast<SharedSymbol &>(sym); 485bdd1243dSDimitry Andric s.dsoProtected = dsoProtected; 486bdd1243dSDimitry Andric s.value = value; 487bdd1243dSDimitry Andric s.size = size; 488bdd1243dSDimitry Andric s.alignment = alignment; 489bdd1243dSDimitry Andric } 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric uint64_t value; // st_value 4920b57cec5SDimitry Andric uint64_t size; // st_size 4930b57cec5SDimitry Andric uint32_t alignment; 4940b57cec5SDimitry Andric }; 4950b57cec5SDimitry Andric 4967a6dacacSDimitry Andric // LazySymbol symbols represent symbols in object files between --start-lib and 49781ad6265SDimitry Andric // --end-lib options. LLD also handles traditional archives as if all the files 49881ad6265SDimitry Andric // in the archive are surrounded by --start-lib and --end-lib. 4990b57cec5SDimitry Andric // 5000b57cec5SDimitry Andric // A special complication is the handling of weak undefined symbols. They should 5010b57cec5SDimitry Andric // not load a file, but we have to remember we have seen both the weak undefined 5020b57cec5SDimitry Andric // and the lazy. We represent that with a lazy symbol with a weak binding. This 5030b57cec5SDimitry Andric // means that code looking for undefined symbols normally also has to take lazy 5040b57cec5SDimitry Andric // symbols into consideration. 5057a6dacacSDimitry Andric class LazySymbol : public Symbol { 5060b57cec5SDimitry Andric public: 5077a6dacacSDimitry Andric LazySymbol(InputFile &file) 5087a6dacacSDimitry Andric : Symbol(LazyKind, &file, {}, llvm::ELF::STB_GLOBAL, 50981ad6265SDimitry Andric llvm::ELF::STV_DEFAULT, llvm::ELF::STT_NOTYPE) {} 5107a6dacacSDimitry Andric void overwrite(Symbol &sym) const { Symbol::overwrite(sym, LazyKind); } 5110b57cec5SDimitry Andric 5127a6dacacSDimitry Andric static bool classof(const Symbol *s) { return s->kind() == LazyKind; } 5130b57cec5SDimitry Andric }; 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric // Some linker-generated symbols need to be created as 5160b57cec5SDimitry Andric // Defined symbols. 5170b57cec5SDimitry Andric struct ElfSym { 5180b57cec5SDimitry Andric // __bss_start 5190b57cec5SDimitry Andric static Defined *bss; 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric // etext and _etext 5220b57cec5SDimitry Andric static Defined *etext1; 5230b57cec5SDimitry Andric static Defined *etext2; 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric // edata and _edata 5260b57cec5SDimitry Andric static Defined *edata1; 5270b57cec5SDimitry Andric static Defined *edata2; 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric // end and _end 5300b57cec5SDimitry Andric static Defined *end1; 5310b57cec5SDimitry Andric static Defined *end2; 5320b57cec5SDimitry Andric 5330b57cec5SDimitry Andric // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to 5340b57cec5SDimitry Andric // be at some offset from the base of the .got section, usually 0 or 5350b57cec5SDimitry Andric // the end of the .got. 5360b57cec5SDimitry Andric static Defined *globalOffsetTable; 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS. 5390b57cec5SDimitry Andric static Defined *mipsGp; 5400b57cec5SDimitry Andric static Defined *mipsGpDisp; 5410b57cec5SDimitry Andric static Defined *mipsLocalGp; 5420b57cec5SDimitry Andric 54306c3fb27SDimitry Andric // __global_pointer$ for RISC-V. 54406c3fb27SDimitry Andric static Defined *riscvGlobalPointer; 54506c3fb27SDimitry Andric 5460b57cec5SDimitry Andric // __rel{,a}_iplt_{start,end} symbols. 5470b57cec5SDimitry Andric static Defined *relaIpltStart; 5480b57cec5SDimitry Andric static Defined *relaIpltEnd; 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric // _TLS_MODULE_BASE_ on targets that support TLSDESC. 5510b57cec5SDimitry Andric static Defined *tlsModuleBase; 5520b57cec5SDimitry Andric }; 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric // A buffer class that is large enough to hold any Symbol-derived 5550b57cec5SDimitry Andric // object. We allocate memory using this class and instantiate a symbol 5560b57cec5SDimitry Andric // using the placement new. 55781ad6265SDimitry Andric 55881ad6265SDimitry Andric // It is important to keep the size of SymbolUnion small for performance and 55981ad6265SDimitry Andric // memory usage reasons. 64 bytes is a soft limit based on the size of Defined 56081ad6265SDimitry Andric // on a 64-bit system. This is enforced by a static_assert in Symbols.cpp. 5610b57cec5SDimitry Andric union SymbolUnion { 5620b57cec5SDimitry Andric alignas(Defined) char a[sizeof(Defined)]; 5630b57cec5SDimitry Andric alignas(CommonSymbol) char b[sizeof(CommonSymbol)]; 5640b57cec5SDimitry Andric alignas(Undefined) char c[sizeof(Undefined)]; 5650b57cec5SDimitry Andric alignas(SharedSymbol) char d[sizeof(SharedSymbol)]; 5667a6dacacSDimitry Andric alignas(LazySymbol) char e[sizeof(LazySymbol)]; 5670b57cec5SDimitry Andric }; 5680b57cec5SDimitry Andric 5690eae32dcSDimitry Andric template <typename... T> Defined *makeDefined(T &&...args) { 570bdd1243dSDimitry Andric auto *sym = getSpecificAllocSingleton<SymbolUnion>().Allocate(); 571bdd1243dSDimitry Andric memset(sym, 0, sizeof(Symbol)); 572bdd1243dSDimitry Andric auto &s = *new (reinterpret_cast<Defined *>(sym)) Defined(std::forward<T>(args)...); 573bdd1243dSDimitry Andric return &s; 5740eae32dcSDimitry Andric } 5750eae32dcSDimitry Andric 57681ad6265SDimitry Andric void reportDuplicate(const Symbol &sym, const InputFile *newFile, 57781ad6265SDimitry Andric InputSectionBase *errSec, uint64_t errOffset); 5780b57cec5SDimitry Andric void maybeWarnUnorderableSymbol(const Symbol *sym); 579480093f4SDimitry Andric bool computeIsPreemptible(const Symbol &sym); 580349cc55cSDimitry Andric 5810b57cec5SDimitry Andric } // namespace elf 5820b57cec5SDimitry Andric } // namespace lld 5830b57cec5SDimitry Andric 5840b57cec5SDimitry Andric #endif 585