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; 4081ad6265SDimitry Andric class LazyObject; 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, 790b57cec5SDimitry Andric LazyObjectKind, 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. 11881ad6265SDimitry Andric uint8_t isPreemptible : 1; 11981ad6265SDimitry Andric 1200b57cec5SDimitry Andric // True if the symbol was used for linking and thus need to be added to the 1210b57cec5SDimitry Andric // output file's symbol table. This is true for all symbols except for 1220b57cec5SDimitry Andric // unreferenced DSO symbols, lazy (archive) symbols, and bitcode symbols that 1230b57cec5SDimitry Andric // are unreferenced except by other bitcode objects. 124480093f4SDimitry Andric uint8_t isUsedInRegularObj : 1; 1250b57cec5SDimitry Andric 12681ad6265SDimitry Andric // True if an undefined or shared symbol is used from a live section. 12781ad6265SDimitry Andric // 12881ad6265SDimitry Andric // NOTE: In Writer.cpp the field is used to mark local defined symbols 12981ad6265SDimitry Andric // which are referenced by relocations when -r or --emit-relocs is given. 13081ad6265SDimitry Andric uint8_t used : 1; 13181ad6265SDimitry Andric 13285868e8aSDimitry Andric // Used by a Defined symbol with protected or default visibility, to record 13385868e8aSDimitry Andric // whether it is required to be exported into .dynsym. This is set when any of 13485868e8aSDimitry Andric // the following conditions hold: 13585868e8aSDimitry Andric // 13681ad6265SDimitry Andric // - If there is an interposable symbol from a DSO. Note: We also do this for 13781ad6265SDimitry Andric // STV_PROTECTED symbols which can't be interposed (to match BFD behavior). 13885868e8aSDimitry Andric // - If -shared or --export-dynamic is specified, any symbol in an object 13985868e8aSDimitry Andric // file/bitcode sets this property, unless suppressed by LTO 14085868e8aSDimitry Andric // canBeOmittedFromSymbolTable(). 141480093f4SDimitry Andric uint8_t exportDynamic : 1; 14285868e8aSDimitry Andric 14385868e8aSDimitry Andric // True if the symbol is in the --dynamic-list file. A Defined symbol with 14485868e8aSDimitry Andric // protected or default visibility with this property is required to be 14585868e8aSDimitry Andric // exported into .dynsym. 146480093f4SDimitry Andric uint8_t inDynamicList : 1; 1470b57cec5SDimitry Andric 148e8d8bef9SDimitry Andric // Used to track if there has been at least one undefined reference to the 149e8d8bef9SDimitry Andric // symbol. For Undefined and SharedSymbol, the binding may change to STB_WEAK 150e8d8bef9SDimitry Andric // if the first undefined reference from a non-shared object is weak. 151480093f4SDimitry Andric uint8_t referenced : 1; 1520b57cec5SDimitry Andric 15381ad6265SDimitry Andric // Used to track if this symbol will be referenced after wrapping is performed 15481ad6265SDimitry Andric // (i.e. this will be true for foo if __real_foo is referenced, and will be 15581ad6265SDimitry Andric // true for __wrap_foo if foo is referenced). 15681ad6265SDimitry Andric uint8_t referencedAfterWrap : 1; 15781ad6265SDimitry Andric 1580b57cec5SDimitry Andric // True if this symbol is specified by --trace-symbol option. 159480093f4SDimitry Andric uint8_t traced : 1; 1600b57cec5SDimitry Andric 16104eeddc0SDimitry Andric // True if the name contains '@'. 16204eeddc0SDimitry Andric uint8_t hasVersionSuffix : 1; 16304eeddc0SDimitry Andric 164bdd1243dSDimitry Andric // Symbol visibility. This is the computed minimum visibility of all 165bdd1243dSDimitry Andric // observed non-DSO symbols. 166bdd1243dSDimitry Andric uint8_t visibility() const { return stOther & 3; } 167bdd1243dSDimitry Andric void setVisibility(uint8_t visibility) { 168bdd1243dSDimitry Andric stOther = (stOther & ~3) | visibility; 169bdd1243dSDimitry Andric } 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric bool includeInDynsym() const; 1720b57cec5SDimitry Andric uint8_t computeBinding() const; 17381ad6265SDimitry Andric bool isGlobal() const { return binding == llvm::ELF::STB_GLOBAL; } 1740b57cec5SDimitry Andric bool isWeak() const { return binding == llvm::ELF::STB_WEAK; } 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric bool isUndefined() const { return symbolKind == UndefinedKind; } 1770b57cec5SDimitry Andric bool isCommon() const { return symbolKind == CommonKind; } 1780b57cec5SDimitry Andric bool isDefined() const { return symbolKind == DefinedKind; } 1790b57cec5SDimitry Andric bool isShared() const { return symbolKind == SharedKind; } 1800b57cec5SDimitry Andric bool isPlaceholder() const { return symbolKind == PlaceholderKind; } 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric bool isLocal() const { return binding == llvm::ELF::STB_LOCAL; } 1830b57cec5SDimitry Andric 18481ad6265SDimitry Andric bool isLazy() const { return symbolKind == LazyObjectKind; } 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric // True if this is an undefined weak symbol. This only works once 1870b57cec5SDimitry Andric // all input files have been added. 188349cc55cSDimitry Andric bool isUndefWeak() const { return isWeak() && isUndefined(); } 1890b57cec5SDimitry Andric 19004eeddc0SDimitry Andric StringRef getName() const { return {nameData, nameSize}; } 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric void setName(StringRef s) { 1930b57cec5SDimitry Andric nameData = s.data(); 1940b57cec5SDimitry Andric nameSize = s.size(); 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric void parseSymbolVersion(); 1980b57cec5SDimitry Andric 199e8d8bef9SDimitry Andric // Get the NUL-terminated version suffix ("", "@...", or "@@..."). 200e8d8bef9SDimitry Andric // 201e8d8bef9SDimitry Andric // For @@, the name has been truncated by insert(). For @, the name has been 202e8d8bef9SDimitry Andric // truncated by Symbol::parseSymbolVersion(). 20304eeddc0SDimitry Andric const char *getVersionSuffix() const { return nameData + nameSize; } 20404eeddc0SDimitry Andric 205bdd1243dSDimitry Andric uint32_t getGotIdx() const { return symAux[auxIdx].gotIdx; } 206bdd1243dSDimitry Andric uint32_t getPltIdx() const { return symAux[auxIdx].pltIdx; } 207bdd1243dSDimitry Andric uint32_t getTlsDescIdx() const { return symAux[auxIdx].tlsDescIdx; } 208bdd1243dSDimitry Andric uint32_t getTlsGdIdx() const { return symAux[auxIdx].tlsGdIdx; } 209e8d8bef9SDimitry Andric 21004eeddc0SDimitry Andric bool isInGot() const { return getGotIdx() != uint32_t(-1); } 21104eeddc0SDimitry Andric bool isInPlt() const { return getPltIdx() != uint32_t(-1); } 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric uint64_t getVA(int64_t addend = 0) const; 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric uint64_t getGotOffset() const; 2160b57cec5SDimitry Andric uint64_t getGotVA() const; 2170b57cec5SDimitry Andric uint64_t getGotPltOffset() const; 2180b57cec5SDimitry Andric uint64_t getGotPltVA() const; 2190b57cec5SDimitry Andric uint64_t getPltVA() const; 2200b57cec5SDimitry Andric uint64_t getSize() const; 2210b57cec5SDimitry Andric OutputSection *getOutputSection() const; 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric // The following two functions are used for symbol resolution. 2240b57cec5SDimitry Andric // 2250b57cec5SDimitry Andric // You are expected to call mergeProperties for all symbols in input 2260b57cec5SDimitry Andric // files so that attributes that are attached to names rather than 2270b57cec5SDimitry Andric // indivisual symbol (such as visibility) are merged together. 2280b57cec5SDimitry Andric // 2290b57cec5SDimitry Andric // Every time you read a new symbol from an input, you are supposed 2300b57cec5SDimitry Andric // to call resolve() with the new symbol. That function replaces 2310b57cec5SDimitry Andric // "this" object as a result of name resolution if the new symbol is 2320b57cec5SDimitry Andric // more appropriate to be included in the output. 2330b57cec5SDimitry Andric // 2340b57cec5SDimitry Andric // For example, if "this" is an undefined symbol and a new symbol is 2350b57cec5SDimitry Andric // a defined symbol, "this" is replaced with the new symbol. 2360b57cec5SDimitry Andric void mergeProperties(const Symbol &other); 237bdd1243dSDimitry Andric void resolve(const Undefined &other); 238bdd1243dSDimitry Andric void resolve(const CommonSymbol &other); 239bdd1243dSDimitry Andric void resolve(const Defined &other); 240bdd1243dSDimitry Andric void resolve(const LazyObject &other); 241bdd1243dSDimitry Andric void resolve(const SharedSymbol &other); 2420b57cec5SDimitry Andric 2434824e7fdSDimitry Andric // If this is a lazy symbol, extract an input file and add the symbol 2440b57cec5SDimitry Andric // in the file to the symbol table. Calling this function on 2450b57cec5SDimitry Andric // non-lazy object causes a runtime error. 2464824e7fdSDimitry Andric void extract() const; 2470b57cec5SDimitry Andric 24881ad6265SDimitry Andric void checkDuplicate(const Defined &other) const; 2490b57cec5SDimitry Andric 250fe6060f1SDimitry Andric private: 25181ad6265SDimitry Andric bool shouldReplace(const Defined &other) const; 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric protected: 25404eeddc0SDimitry Andric Symbol(Kind k, InputFile *file, StringRef name, uint8_t binding, 2550b57cec5SDimitry Andric uint8_t stOther, uint8_t type) 25681ad6265SDimitry Andric : file(file), nameData(name.data()), nameSize(name.size()), type(type), 257*5f757f3fSDimitry Andric binding(binding), stOther(stOther), symbolKind(k), exportDynamic(false), 258*5f757f3fSDimitry Andric archSpecificBit(false) {} 259bdd1243dSDimitry Andric 260bdd1243dSDimitry Andric void overwrite(Symbol &sym, Kind k) const { 261bdd1243dSDimitry Andric if (sym.traced) 262bdd1243dSDimitry Andric printTraceSymbol(*this, sym.getName()); 263bdd1243dSDimitry Andric sym.file = file; 264bdd1243dSDimitry Andric sym.type = type; 265bdd1243dSDimitry Andric sym.binding = binding; 266bdd1243dSDimitry Andric sym.stOther = (stOther & ~3) | sym.visibility(); 267bdd1243dSDimitry Andric sym.symbolKind = k; 268bdd1243dSDimitry Andric } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric public: 2710b57cec5SDimitry Andric // True if this symbol is in the Iplt sub-section of the Plt and the Igot 2720b57cec5SDimitry Andric // sub-section of the .got.plt or .got. 273480093f4SDimitry Andric uint8_t isInIplt : 1; 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric // True if this symbol needs a GOT entry and its GOT entry is actually in 2760b57cec5SDimitry Andric // Igot. This will be true only for certain non-preemptible ifuncs. 277480093f4SDimitry Andric uint8_t gotInIgot : 1; 2780b57cec5SDimitry Andric 2790eae32dcSDimitry Andric // True if defined relative to a section discarded by ICF. 2800eae32dcSDimitry Andric uint8_t folded : 1; 2810eae32dcSDimitry Andric 282*5f757f3fSDimitry Andric // Allow reuse of a bit between architecture-exclusive symbol flags. 283*5f757f3fSDimitry Andric // - needsTocRestore(): On PPC64, true if a call to this symbol needs to be 284*5f757f3fSDimitry Andric // followed by a restore of the toc pointer. 285*5f757f3fSDimitry Andric // - isTagged(): On AArch64, true if the symbol needs special relocation and 286*5f757f3fSDimitry Andric // metadata semantics because it's tagged, under the AArch64 MemtagABI. 287*5f757f3fSDimitry Andric uint8_t archSpecificBit : 1; 288*5f757f3fSDimitry Andric bool needsTocRestore() const { return archSpecificBit; } 289*5f757f3fSDimitry Andric bool isTagged() const { return archSpecificBit; } 290*5f757f3fSDimitry Andric void setNeedsTocRestore(bool v) { archSpecificBit = v; } 291*5f757f3fSDimitry Andric void setIsTagged(bool v) { 292*5f757f3fSDimitry Andric archSpecificBit = v; 293*5f757f3fSDimitry Andric } 2940b57cec5SDimitry Andric 29581ad6265SDimitry Andric // True if this symbol is defined by a symbol assignment or wrapped by --wrap. 29681ad6265SDimitry Andric // 29781ad6265SDimitry Andric // LTO shouldn't inline the symbol because it doesn't know the final content 29881ad6265SDimitry Andric // of the symbol. 299480093f4SDimitry Andric uint8_t scriptDefined : 1; 3000b57cec5SDimitry Andric 301bdd1243dSDimitry Andric // True if defined in a DSO as protected visibility. 302bdd1243dSDimitry Andric uint8_t dsoProtected : 1; 3030eae32dcSDimitry Andric 30406c3fb27SDimitry Andric // True if targeted by a range extension thunk. 30506c3fb27SDimitry Andric uint8_t thunkAccessed : 1; 30606c3fb27SDimitry Andric 3070eae32dcSDimitry Andric // Temporary flags used to communicate which symbol entries need PLT and GOT 3080eae32dcSDimitry Andric // entries during postScanRelocations(); 309bdd1243dSDimitry Andric std::atomic<uint16_t> flags; 3100eae32dcSDimitry Andric 31181ad6265SDimitry Andric // A symAux index used to access GOT/PLT entry indexes. This is allocated in 31281ad6265SDimitry Andric // postScanRelocations(). 313bdd1243dSDimitry Andric uint32_t auxIdx; 314bdd1243dSDimitry Andric uint32_t dynsymIndex; 31581ad6265SDimitry Andric 316*5f757f3fSDimitry Andric // If `file` is SharedFile (for SharedSymbol or copy-relocated Defined), this 317*5f757f3fSDimitry Andric // represents the Verdef index within the input DSO, which will be converted 318*5f757f3fSDimitry Andric // to a Verneed index in the output. Otherwise, this represents the Verdef 319*5f757f3fSDimitry Andric // index (VER_NDX_LOCAL, VER_NDX_GLOBAL, or a named version). 32081ad6265SDimitry Andric uint16_t versionId; 321*5f757f3fSDimitry Andric uint8_t versionScriptAssigned : 1; 32281ad6265SDimitry Andric 323bdd1243dSDimitry Andric void setFlags(uint16_t bits) { 324bdd1243dSDimitry Andric flags.fetch_or(bits, std::memory_order_relaxed); 325bdd1243dSDimitry Andric } 326bdd1243dSDimitry Andric bool hasFlag(uint16_t bit) const { 327bdd1243dSDimitry Andric assert(bit && (bit & (bit - 1)) == 0 && "bit must be a power of 2"); 328bdd1243dSDimitry Andric return flags.load(std::memory_order_relaxed) & bit; 329bdd1243dSDimitry Andric } 330bdd1243dSDimitry Andric 33104eeddc0SDimitry Andric bool needsDynReloc() const { 332bdd1243dSDimitry Andric return flags.load(std::memory_order_relaxed) & 333bdd1243dSDimitry Andric (NEEDS_COPY | NEEDS_GOT | NEEDS_PLT | NEEDS_TLSDESC | NEEDS_TLSGD | 334bdd1243dSDimitry Andric NEEDS_TLSGD_TO_IE | NEEDS_GOT_DTPREL | NEEDS_TLSIE); 33504eeddc0SDimitry Andric } 33604eeddc0SDimitry Andric void allocateAux() { 337bdd1243dSDimitry Andric assert(auxIdx == 0); 33804eeddc0SDimitry Andric auxIdx = symAux.size(); 33904eeddc0SDimitry Andric symAux.emplace_back(); 34004eeddc0SDimitry Andric } 34104eeddc0SDimitry Andric 3420b57cec5SDimitry Andric bool isSection() const { return type == llvm::ELF::STT_SECTION; } 3430b57cec5SDimitry Andric bool isTls() const { return type == llvm::ELF::STT_TLS; } 3440b57cec5SDimitry Andric bool isFunc() const { return type == llvm::ELF::STT_FUNC; } 3450b57cec5SDimitry Andric bool isGnuIFunc() const { return type == llvm::ELF::STT_GNU_IFUNC; } 3460b57cec5SDimitry Andric bool isObject() const { return type == llvm::ELF::STT_OBJECT; } 3470b57cec5SDimitry Andric bool isFile() const { return type == llvm::ELF::STT_FILE; } 3480b57cec5SDimitry Andric }; 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric // Represents a symbol that is defined in the current output file. 3510b57cec5SDimitry Andric class Defined : public Symbol { 3520b57cec5SDimitry Andric public: 35304eeddc0SDimitry Andric Defined(InputFile *file, StringRef name, uint8_t binding, uint8_t stOther, 3540b57cec5SDimitry Andric uint8_t type, uint64_t value, uint64_t size, SectionBase *section) 3550b57cec5SDimitry Andric : Symbol(DefinedKind, file, name, binding, stOther, type), value(value), 35681ad6265SDimitry Andric size(size), section(section) { 35781ad6265SDimitry Andric exportDynamic = config->exportDynamic; 35881ad6265SDimitry Andric } 359*5f757f3fSDimitry Andric void overwrite(Symbol &sym) const; 3600b57cec5SDimitry Andric 3610b57cec5SDimitry Andric static bool classof(const Symbol *s) { return s->isDefined(); } 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric uint64_t value; 3640b57cec5SDimitry Andric uint64_t size; 3650b57cec5SDimitry Andric SectionBase *section; 3660b57cec5SDimitry Andric }; 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric // Represents a common symbol. 3690b57cec5SDimitry Andric // 3700b57cec5SDimitry Andric // On Unix, it is traditionally allowed to write variable definitions 3710b57cec5SDimitry Andric // without initialization expressions (such as "int foo;") to header 3720b57cec5SDimitry Andric // files. Such definition is called "tentative definition". 3730b57cec5SDimitry Andric // 3740b57cec5SDimitry Andric // Using tentative definition is usually considered a bad practice 3750b57cec5SDimitry Andric // because you should write only declarations (such as "extern int 3760b57cec5SDimitry Andric // foo;") to header files. Nevertheless, the linker and the compiler 3770b57cec5SDimitry Andric // have to do something to support bad code by allowing duplicate 3780b57cec5SDimitry Andric // definitions for this particular case. 3790b57cec5SDimitry Andric // 3800b57cec5SDimitry Andric // Common symbols represent variable definitions without initializations. 381480093f4SDimitry Andric // The compiler creates common symbols when it sees variable definitions 3820b57cec5SDimitry Andric // without initialization (you can suppress this behavior and let the 3830b57cec5SDimitry Andric // compiler create a regular defined symbol by -fno-common). 3840b57cec5SDimitry Andric // 3850b57cec5SDimitry Andric // The linker allows common symbols to be replaced by regular defined 3860b57cec5SDimitry Andric // symbols. If there are remaining common symbols after name resolution is 3870b57cec5SDimitry Andric // complete, they are converted to regular defined symbols in a .bss 3880b57cec5SDimitry Andric // section. (Therefore, the later passes don't see any CommonSymbols.) 3890b57cec5SDimitry Andric class CommonSymbol : public Symbol { 3900b57cec5SDimitry Andric public: 39104eeddc0SDimitry Andric CommonSymbol(InputFile *file, StringRef name, uint8_t binding, 3920b57cec5SDimitry Andric uint8_t stOther, uint8_t type, uint64_t alignment, uint64_t size) 3930b57cec5SDimitry Andric : Symbol(CommonKind, file, name, binding, stOther, type), 39481ad6265SDimitry Andric alignment(alignment), size(size) { 39581ad6265SDimitry Andric exportDynamic = config->exportDynamic; 39681ad6265SDimitry Andric } 397bdd1243dSDimitry Andric void overwrite(Symbol &sym) const { 398bdd1243dSDimitry Andric Symbol::overwrite(sym, CommonKind); 399bdd1243dSDimitry Andric auto &s = static_cast<CommonSymbol &>(sym); 400bdd1243dSDimitry Andric s.alignment = alignment; 401bdd1243dSDimitry Andric s.size = size; 402bdd1243dSDimitry Andric } 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric static bool classof(const Symbol *s) { return s->isCommon(); } 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andric uint32_t alignment; 4070b57cec5SDimitry Andric uint64_t size; 4080b57cec5SDimitry Andric }; 4090b57cec5SDimitry Andric 4100b57cec5SDimitry Andric class Undefined : public Symbol { 4110b57cec5SDimitry Andric public: 41204eeddc0SDimitry Andric Undefined(InputFile *file, StringRef name, uint8_t binding, uint8_t stOther, 4130b57cec5SDimitry Andric uint8_t type, uint32_t discardedSecIdx = 0) 4140b57cec5SDimitry Andric : Symbol(UndefinedKind, file, name, binding, stOther, type), 4150b57cec5SDimitry Andric discardedSecIdx(discardedSecIdx) {} 416bdd1243dSDimitry Andric void overwrite(Symbol &sym) const { 417bdd1243dSDimitry Andric Symbol::overwrite(sym, UndefinedKind); 418bdd1243dSDimitry Andric auto &s = static_cast<Undefined &>(sym); 419bdd1243dSDimitry Andric s.discardedSecIdx = discardedSecIdx; 420bdd1243dSDimitry Andric s.nonPrevailing = nonPrevailing; 421bdd1243dSDimitry Andric } 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric static bool classof(const Symbol *s) { return s->kind() == UndefinedKind; } 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric // The section index if in a discarded section, 0 otherwise. 4260b57cec5SDimitry Andric uint32_t discardedSecIdx; 42781ad6265SDimitry Andric bool nonPrevailing = false; 4280b57cec5SDimitry Andric }; 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric class SharedSymbol : public Symbol { 4310b57cec5SDimitry Andric public: 4320b57cec5SDimitry Andric static bool classof(const Symbol *s) { return s->kind() == SharedKind; } 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric SharedSymbol(InputFile &file, StringRef name, uint8_t binding, 4350b57cec5SDimitry Andric uint8_t stOther, uint8_t type, uint64_t value, uint64_t size, 43681ad6265SDimitry Andric uint32_t alignment) 4370b57cec5SDimitry Andric : Symbol(SharedKind, &file, name, binding, stOther, type), value(value), 4380b57cec5SDimitry Andric size(size), alignment(alignment) { 43981ad6265SDimitry Andric exportDynamic = true; 440bdd1243dSDimitry Andric dsoProtected = visibility() == llvm::ELF::STV_PROTECTED; 4410b57cec5SDimitry Andric // GNU ifunc is a mechanism to allow user-supplied functions to 4420b57cec5SDimitry Andric // resolve PLT slot values at load-time. This is contrary to the 4430b57cec5SDimitry Andric // regular symbol resolution scheme in which symbols are resolved just 4440b57cec5SDimitry Andric // by name. Using this hook, you can program how symbols are solved 4450b57cec5SDimitry Andric // for you program. For example, you can make "memcpy" to be resolved 4460b57cec5SDimitry Andric // to a SSE-enabled version of memcpy only when a machine running the 4470b57cec5SDimitry Andric // program supports the SSE instruction set. 4480b57cec5SDimitry Andric // 4490b57cec5SDimitry Andric // Naturally, such symbols should always be called through their PLT 4500b57cec5SDimitry Andric // slots. What GNU ifunc symbols point to are resolver functions, and 4510b57cec5SDimitry Andric // calling them directly doesn't make sense (unless you are writing a 4520b57cec5SDimitry Andric // loader). 4530b57cec5SDimitry Andric // 4540b57cec5SDimitry Andric // For DSO symbols, we always call them through PLT slots anyway. 4550b57cec5SDimitry Andric // So there's no difference between GNU ifunc and regular function 4560b57cec5SDimitry Andric // symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC. 4570b57cec5SDimitry Andric if (this->type == llvm::ELF::STT_GNU_IFUNC) 4580b57cec5SDimitry Andric this->type = llvm::ELF::STT_FUNC; 4590b57cec5SDimitry Andric } 460bdd1243dSDimitry Andric void overwrite(Symbol &sym) const { 461bdd1243dSDimitry Andric Symbol::overwrite(sym, SharedKind); 462bdd1243dSDimitry Andric auto &s = static_cast<SharedSymbol &>(sym); 463bdd1243dSDimitry Andric s.dsoProtected = dsoProtected; 464bdd1243dSDimitry Andric s.value = value; 465bdd1243dSDimitry Andric s.size = size; 466bdd1243dSDimitry Andric s.alignment = alignment; 467bdd1243dSDimitry Andric } 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric uint64_t value; // st_value 4700b57cec5SDimitry Andric uint64_t size; // st_size 4710b57cec5SDimitry Andric uint32_t alignment; 4720b57cec5SDimitry Andric }; 4730b57cec5SDimitry Andric 47481ad6265SDimitry Andric // LazyObject symbols represent symbols in object files between --start-lib and 47581ad6265SDimitry Andric // --end-lib options. LLD also handles traditional archives as if all the files 47681ad6265SDimitry Andric // in the archive are surrounded by --start-lib and --end-lib. 4770b57cec5SDimitry Andric // 4780b57cec5SDimitry Andric // A special complication is the handling of weak undefined symbols. They should 4790b57cec5SDimitry Andric // not load a file, but we have to remember we have seen both the weak undefined 4800b57cec5SDimitry Andric // and the lazy. We represent that with a lazy symbol with a weak binding. This 4810b57cec5SDimitry Andric // means that code looking for undefined symbols normally also has to take lazy 4820b57cec5SDimitry Andric // symbols into consideration. 4830b57cec5SDimitry Andric class LazyObject : public Symbol { 4840b57cec5SDimitry Andric public: 48581ad6265SDimitry Andric LazyObject(InputFile &file) 48681ad6265SDimitry Andric : Symbol(LazyObjectKind, &file, {}, llvm::ELF::STB_GLOBAL, 48781ad6265SDimitry Andric llvm::ELF::STV_DEFAULT, llvm::ELF::STT_NOTYPE) {} 488bdd1243dSDimitry Andric void overwrite(Symbol &sym) const { Symbol::overwrite(sym, LazyObjectKind); } 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric static bool classof(const Symbol *s) { return s->kind() == LazyObjectKind; } 4910b57cec5SDimitry Andric }; 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric // Some linker-generated symbols need to be created as 4940b57cec5SDimitry Andric // Defined symbols. 4950b57cec5SDimitry Andric struct ElfSym { 4960b57cec5SDimitry Andric // __bss_start 4970b57cec5SDimitry Andric static Defined *bss; 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric // etext and _etext 5000b57cec5SDimitry Andric static Defined *etext1; 5010b57cec5SDimitry Andric static Defined *etext2; 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric // edata and _edata 5040b57cec5SDimitry Andric static Defined *edata1; 5050b57cec5SDimitry Andric static Defined *edata2; 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric // end and _end 5080b57cec5SDimitry Andric static Defined *end1; 5090b57cec5SDimitry Andric static Defined *end2; 5100b57cec5SDimitry Andric 5110b57cec5SDimitry Andric // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to 5120b57cec5SDimitry Andric // be at some offset from the base of the .got section, usually 0 or 5130b57cec5SDimitry Andric // the end of the .got. 5140b57cec5SDimitry Andric static Defined *globalOffsetTable; 5150b57cec5SDimitry Andric 5160b57cec5SDimitry Andric // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS. 5170b57cec5SDimitry Andric static Defined *mipsGp; 5180b57cec5SDimitry Andric static Defined *mipsGpDisp; 5190b57cec5SDimitry Andric static Defined *mipsLocalGp; 5200b57cec5SDimitry Andric 52106c3fb27SDimitry Andric // __global_pointer$ for RISC-V. 52206c3fb27SDimitry Andric static Defined *riscvGlobalPointer; 52306c3fb27SDimitry Andric 5240b57cec5SDimitry Andric // __rel{,a}_iplt_{start,end} symbols. 5250b57cec5SDimitry Andric static Defined *relaIpltStart; 5260b57cec5SDimitry Andric static Defined *relaIpltEnd; 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric // _TLS_MODULE_BASE_ on targets that support TLSDESC. 5290b57cec5SDimitry Andric static Defined *tlsModuleBase; 5300b57cec5SDimitry Andric }; 5310b57cec5SDimitry Andric 5320b57cec5SDimitry Andric // A buffer class that is large enough to hold any Symbol-derived 5330b57cec5SDimitry Andric // object. We allocate memory using this class and instantiate a symbol 5340b57cec5SDimitry Andric // using the placement new. 53581ad6265SDimitry Andric 53681ad6265SDimitry Andric // It is important to keep the size of SymbolUnion small for performance and 53781ad6265SDimitry Andric // memory usage reasons. 64 bytes is a soft limit based on the size of Defined 53881ad6265SDimitry Andric // on a 64-bit system. This is enforced by a static_assert in Symbols.cpp. 5390b57cec5SDimitry Andric union SymbolUnion { 5400b57cec5SDimitry Andric alignas(Defined) char a[sizeof(Defined)]; 5410b57cec5SDimitry Andric alignas(CommonSymbol) char b[sizeof(CommonSymbol)]; 5420b57cec5SDimitry Andric alignas(Undefined) char c[sizeof(Undefined)]; 5430b57cec5SDimitry Andric alignas(SharedSymbol) char d[sizeof(SharedSymbol)]; 54481ad6265SDimitry Andric alignas(LazyObject) char e[sizeof(LazyObject)]; 5450b57cec5SDimitry Andric }; 5460b57cec5SDimitry Andric 5470eae32dcSDimitry Andric template <typename... T> Defined *makeDefined(T &&...args) { 548bdd1243dSDimitry Andric auto *sym = getSpecificAllocSingleton<SymbolUnion>().Allocate(); 549bdd1243dSDimitry Andric memset(sym, 0, sizeof(Symbol)); 550bdd1243dSDimitry Andric auto &s = *new (reinterpret_cast<Defined *>(sym)) Defined(std::forward<T>(args)...); 551bdd1243dSDimitry Andric return &s; 5520eae32dcSDimitry Andric } 5530eae32dcSDimitry Andric 55481ad6265SDimitry Andric void reportDuplicate(const Symbol &sym, const InputFile *newFile, 55581ad6265SDimitry Andric InputSectionBase *errSec, uint64_t errOffset); 5560b57cec5SDimitry Andric void maybeWarnUnorderableSymbol(const Symbol *sym); 557480093f4SDimitry Andric bool computeIsPreemptible(const Symbol &sym); 558349cc55cSDimitry Andric 5590b57cec5SDimitry Andric } // namespace elf 5600b57cec5SDimitry Andric } // namespace lld 5610b57cec5SDimitry Andric 5620b57cec5SDimitry Andric #endif 563