xref: /freebsd/contrib/llvm-project/lld/ELF/Symbols.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
21*bdd1243dSDimitry 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 
43*bdd1243dSDimitry Andric void printTraceSymbol(const Symbol &sym, StringRef name);
44*bdd1243dSDimitry Andric 
45*bdd1243dSDimitry Andric enum {
46*bdd1243dSDimitry Andric   NEEDS_GOT = 1 << 0,
47*bdd1243dSDimitry Andric   NEEDS_PLT = 1 << 1,
48*bdd1243dSDimitry Andric   HAS_DIRECT_RELOC = 1 << 2,
49*bdd1243dSDimitry Andric   // True if this symbol needs a canonical PLT entry, or (during
50*bdd1243dSDimitry Andric   // postScanRelocations) a copy relocation.
51*bdd1243dSDimitry Andric   NEEDS_COPY = 1 << 3,
52*bdd1243dSDimitry Andric   NEEDS_TLSDESC = 1 << 4,
53*bdd1243dSDimitry Andric   NEEDS_TLSGD = 1 << 5,
54*bdd1243dSDimitry Andric   NEEDS_TLSGD_TO_IE = 1 << 6,
55*bdd1243dSDimitry Andric   NEEDS_GOT_DTPREL = 1 << 7,
56*bdd1243dSDimitry Andric   NEEDS_TLSIE = 1 << 8,
57*bdd1243dSDimitry Andric };
58*bdd1243dSDimitry 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 
68*bdd1243dSDimitry 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 
87*bdd1243dSDimitry Andric   // The default copy constructor is deleted due to atomic flags. Define one for
88*bdd1243dSDimitry Andric   // places where no atomic is needed.
89*bdd1243dSDimitry Andric   Symbol(const Symbol &o) { memcpy(this, &o, sizeof(o)); }
90*bdd1243dSDimitry 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.
115*bdd1243dSDimitry 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 
164*bdd1243dSDimitry Andric   // Symbol visibility. This is the computed minimum visibility of all
165*bdd1243dSDimitry Andric   // observed non-DSO symbols.
166*bdd1243dSDimitry Andric   uint8_t visibility() const { return stOther & 3; }
167*bdd1243dSDimitry Andric   void setVisibility(uint8_t visibility) {
168*bdd1243dSDimitry Andric     stOther = (stOther & ~3) | visibility;
169*bdd1243dSDimitry 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 
205*bdd1243dSDimitry Andric   uint32_t getGotIdx() const { return symAux[auxIdx].gotIdx; }
206*bdd1243dSDimitry Andric   uint32_t getPltIdx() const { return symAux[auxIdx].pltIdx; }
207*bdd1243dSDimitry Andric   uint32_t getTlsDescIdx() const { return symAux[auxIdx].tlsDescIdx; }
208*bdd1243dSDimitry 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);
237*bdd1243dSDimitry Andric   void resolve(const Undefined &other);
238*bdd1243dSDimitry Andric   void resolve(const CommonSymbol &other);
239*bdd1243dSDimitry Andric   void resolve(const Defined &other);
240*bdd1243dSDimitry Andric   void resolve(const LazyObject &other);
241*bdd1243dSDimitry 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),
25781ad6265SDimitry Andric         binding(binding), stOther(stOther), symbolKind(k),
258*bdd1243dSDimitry Andric         exportDynamic(false) {}
259*bdd1243dSDimitry Andric 
260*bdd1243dSDimitry Andric   void overwrite(Symbol &sym, Kind k) const {
261*bdd1243dSDimitry Andric     if (sym.traced)
262*bdd1243dSDimitry Andric       printTraceSymbol(*this, sym.getName());
263*bdd1243dSDimitry Andric     sym.file = file;
264*bdd1243dSDimitry Andric     sym.type = type;
265*bdd1243dSDimitry Andric     sym.binding = binding;
266*bdd1243dSDimitry Andric     sym.stOther = (stOther & ~3) | sym.visibility();
267*bdd1243dSDimitry Andric     sym.symbolKind = k;
268*bdd1243dSDimitry 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 
2820b57cec5SDimitry Andric   // True if a call to this symbol needs to be followed by a restore of the
2830b57cec5SDimitry Andric   // PPC64 toc pointer.
284480093f4SDimitry Andric   uint8_t needsTocRestore : 1;
2850b57cec5SDimitry Andric 
28681ad6265SDimitry Andric   // True if this symbol is defined by a symbol assignment or wrapped by --wrap.
28781ad6265SDimitry Andric   //
28881ad6265SDimitry Andric   // LTO shouldn't inline the symbol because it doesn't know the final content
28981ad6265SDimitry Andric   // of the symbol.
290480093f4SDimitry Andric   uint8_t scriptDefined : 1;
2910b57cec5SDimitry Andric 
292*bdd1243dSDimitry Andric   // True if defined in a DSO as protected visibility.
293*bdd1243dSDimitry Andric   uint8_t dsoProtected : 1;
2940eae32dcSDimitry Andric 
2950eae32dcSDimitry Andric   // Temporary flags used to communicate which symbol entries need PLT and GOT
2960eae32dcSDimitry Andric   // entries during postScanRelocations();
297*bdd1243dSDimitry Andric   std::atomic<uint16_t> flags;
2980eae32dcSDimitry Andric 
29981ad6265SDimitry Andric   // A symAux index used to access GOT/PLT entry indexes. This is allocated in
30081ad6265SDimitry Andric   // postScanRelocations().
301*bdd1243dSDimitry Andric   uint32_t auxIdx;
302*bdd1243dSDimitry Andric   uint32_t dynsymIndex;
30381ad6265SDimitry Andric 
30481ad6265SDimitry Andric   // This field is a index to the symbol's version definition.
305*bdd1243dSDimitry Andric   uint16_t verdefIndex;
30681ad6265SDimitry Andric 
30781ad6265SDimitry Andric   // Version definition index.
30881ad6265SDimitry Andric   uint16_t versionId;
30981ad6265SDimitry Andric 
310*bdd1243dSDimitry Andric   void setFlags(uint16_t bits) {
311*bdd1243dSDimitry Andric     flags.fetch_or(bits, std::memory_order_relaxed);
312*bdd1243dSDimitry Andric   }
313*bdd1243dSDimitry Andric   bool hasFlag(uint16_t bit) const {
314*bdd1243dSDimitry Andric     assert(bit && (bit & (bit - 1)) == 0 && "bit must be a power of 2");
315*bdd1243dSDimitry Andric     return flags.load(std::memory_order_relaxed) & bit;
316*bdd1243dSDimitry Andric   }
317*bdd1243dSDimitry Andric 
31804eeddc0SDimitry Andric   bool needsDynReloc() const {
319*bdd1243dSDimitry Andric     return flags.load(std::memory_order_relaxed) &
320*bdd1243dSDimitry Andric            (NEEDS_COPY | NEEDS_GOT | NEEDS_PLT | NEEDS_TLSDESC | NEEDS_TLSGD |
321*bdd1243dSDimitry Andric             NEEDS_TLSGD_TO_IE | NEEDS_GOT_DTPREL | NEEDS_TLSIE);
32204eeddc0SDimitry Andric   }
32304eeddc0SDimitry Andric   void allocateAux() {
324*bdd1243dSDimitry Andric     assert(auxIdx == 0);
32504eeddc0SDimitry Andric     auxIdx = symAux.size();
32604eeddc0SDimitry Andric     symAux.emplace_back();
32704eeddc0SDimitry Andric   }
32804eeddc0SDimitry Andric 
3290b57cec5SDimitry Andric   bool isSection() const { return type == llvm::ELF::STT_SECTION; }
3300b57cec5SDimitry Andric   bool isTls() const { return type == llvm::ELF::STT_TLS; }
3310b57cec5SDimitry Andric   bool isFunc() const { return type == llvm::ELF::STT_FUNC; }
3320b57cec5SDimitry Andric   bool isGnuIFunc() const { return type == llvm::ELF::STT_GNU_IFUNC; }
3330b57cec5SDimitry Andric   bool isObject() const { return type == llvm::ELF::STT_OBJECT; }
3340b57cec5SDimitry Andric   bool isFile() const { return type == llvm::ELF::STT_FILE; }
3350b57cec5SDimitry Andric };
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric // Represents a symbol that is defined in the current output file.
3380b57cec5SDimitry Andric class Defined : public Symbol {
3390b57cec5SDimitry Andric public:
34004eeddc0SDimitry Andric   Defined(InputFile *file, StringRef name, uint8_t binding, uint8_t stOther,
3410b57cec5SDimitry Andric           uint8_t type, uint64_t value, uint64_t size, SectionBase *section)
3420b57cec5SDimitry Andric       : Symbol(DefinedKind, file, name, binding, stOther, type), value(value),
34381ad6265SDimitry Andric         size(size), section(section) {
34481ad6265SDimitry Andric     exportDynamic = config->exportDynamic;
34581ad6265SDimitry Andric   }
346*bdd1243dSDimitry Andric   void overwrite(Symbol &sym) const {
347*bdd1243dSDimitry Andric     Symbol::overwrite(sym, DefinedKind);
348*bdd1243dSDimitry Andric     sym.verdefIndex = -1;
349*bdd1243dSDimitry Andric     auto &s = static_cast<Defined &>(sym);
350*bdd1243dSDimitry Andric     s.value = value;
351*bdd1243dSDimitry Andric     s.size = size;
352*bdd1243dSDimitry Andric     s.section = section;
353*bdd1243dSDimitry Andric   }
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   static bool classof(const Symbol *s) { return s->isDefined(); }
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   uint64_t value;
3580b57cec5SDimitry Andric   uint64_t size;
3590b57cec5SDimitry Andric   SectionBase *section;
3600b57cec5SDimitry Andric };
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric // Represents a common symbol.
3630b57cec5SDimitry Andric //
3640b57cec5SDimitry Andric // On Unix, it is traditionally allowed to write variable definitions
3650b57cec5SDimitry Andric // without initialization expressions (such as "int foo;") to header
3660b57cec5SDimitry Andric // files. Such definition is called "tentative definition".
3670b57cec5SDimitry Andric //
3680b57cec5SDimitry Andric // Using tentative definition is usually considered a bad practice
3690b57cec5SDimitry Andric // because you should write only declarations (such as "extern int
3700b57cec5SDimitry Andric // foo;") to header files. Nevertheless, the linker and the compiler
3710b57cec5SDimitry Andric // have to do something to support bad code by allowing duplicate
3720b57cec5SDimitry Andric // definitions for this particular case.
3730b57cec5SDimitry Andric //
3740b57cec5SDimitry Andric // Common symbols represent variable definitions without initializations.
375480093f4SDimitry Andric // The compiler creates common symbols when it sees variable definitions
3760b57cec5SDimitry Andric // without initialization (you can suppress this behavior and let the
3770b57cec5SDimitry Andric // compiler create a regular defined symbol by -fno-common).
3780b57cec5SDimitry Andric //
3790b57cec5SDimitry Andric // The linker allows common symbols to be replaced by regular defined
3800b57cec5SDimitry Andric // symbols. If there are remaining common symbols after name resolution is
3810b57cec5SDimitry Andric // complete, they are converted to regular defined symbols in a .bss
3820b57cec5SDimitry Andric // section. (Therefore, the later passes don't see any CommonSymbols.)
3830b57cec5SDimitry Andric class CommonSymbol : public Symbol {
3840b57cec5SDimitry Andric public:
38504eeddc0SDimitry Andric   CommonSymbol(InputFile *file, StringRef name, uint8_t binding,
3860b57cec5SDimitry Andric                uint8_t stOther, uint8_t type, uint64_t alignment, uint64_t size)
3870b57cec5SDimitry Andric       : Symbol(CommonKind, file, name, binding, stOther, type),
38881ad6265SDimitry Andric         alignment(alignment), size(size) {
38981ad6265SDimitry Andric     exportDynamic = config->exportDynamic;
39081ad6265SDimitry Andric   }
391*bdd1243dSDimitry Andric   void overwrite(Symbol &sym) const {
392*bdd1243dSDimitry Andric     Symbol::overwrite(sym, CommonKind);
393*bdd1243dSDimitry Andric     auto &s = static_cast<CommonSymbol &>(sym);
394*bdd1243dSDimitry Andric     s.alignment = alignment;
395*bdd1243dSDimitry Andric     s.size = size;
396*bdd1243dSDimitry Andric   }
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric   static bool classof(const Symbol *s) { return s->isCommon(); }
3990b57cec5SDimitry Andric 
4000b57cec5SDimitry Andric   uint32_t alignment;
4010b57cec5SDimitry Andric   uint64_t size;
4020b57cec5SDimitry Andric };
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric class Undefined : public Symbol {
4050b57cec5SDimitry Andric public:
40604eeddc0SDimitry Andric   Undefined(InputFile *file, StringRef name, uint8_t binding, uint8_t stOther,
4070b57cec5SDimitry Andric             uint8_t type, uint32_t discardedSecIdx = 0)
4080b57cec5SDimitry Andric       : Symbol(UndefinedKind, file, name, binding, stOther, type),
4090b57cec5SDimitry Andric         discardedSecIdx(discardedSecIdx) {}
410*bdd1243dSDimitry Andric   void overwrite(Symbol &sym) const {
411*bdd1243dSDimitry Andric     Symbol::overwrite(sym, UndefinedKind);
412*bdd1243dSDimitry Andric     auto &s = static_cast<Undefined &>(sym);
413*bdd1243dSDimitry Andric     s.discardedSecIdx = discardedSecIdx;
414*bdd1243dSDimitry Andric     s.nonPrevailing = nonPrevailing;
415*bdd1243dSDimitry Andric   }
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric   static bool classof(const Symbol *s) { return s->kind() == UndefinedKind; }
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   // The section index if in a discarded section, 0 otherwise.
4200b57cec5SDimitry Andric   uint32_t discardedSecIdx;
42181ad6265SDimitry Andric   bool nonPrevailing = false;
4220b57cec5SDimitry Andric };
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric class SharedSymbol : public Symbol {
4250b57cec5SDimitry Andric public:
4260b57cec5SDimitry Andric   static bool classof(const Symbol *s) { return s->kind() == SharedKind; }
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric   SharedSymbol(InputFile &file, StringRef name, uint8_t binding,
4290b57cec5SDimitry Andric                uint8_t stOther, uint8_t type, uint64_t value, uint64_t size,
43081ad6265SDimitry Andric                uint32_t alignment)
4310b57cec5SDimitry Andric       : Symbol(SharedKind, &file, name, binding, stOther, type), value(value),
4320b57cec5SDimitry Andric         size(size), alignment(alignment) {
43381ad6265SDimitry Andric     exportDynamic = true;
434*bdd1243dSDimitry Andric     dsoProtected = visibility() == llvm::ELF::STV_PROTECTED;
4350b57cec5SDimitry Andric     // GNU ifunc is a mechanism to allow user-supplied functions to
4360b57cec5SDimitry Andric     // resolve PLT slot values at load-time. This is contrary to the
4370b57cec5SDimitry Andric     // regular symbol resolution scheme in which symbols are resolved just
4380b57cec5SDimitry Andric     // by name. Using this hook, you can program how symbols are solved
4390b57cec5SDimitry Andric     // for you program. For example, you can make "memcpy" to be resolved
4400b57cec5SDimitry Andric     // to a SSE-enabled version of memcpy only when a machine running the
4410b57cec5SDimitry Andric     // program supports the SSE instruction set.
4420b57cec5SDimitry Andric     //
4430b57cec5SDimitry Andric     // Naturally, such symbols should always be called through their PLT
4440b57cec5SDimitry Andric     // slots. What GNU ifunc symbols point to are resolver functions, and
4450b57cec5SDimitry Andric     // calling them directly doesn't make sense (unless you are writing a
4460b57cec5SDimitry Andric     // loader).
4470b57cec5SDimitry Andric     //
4480b57cec5SDimitry Andric     // For DSO symbols, we always call them through PLT slots anyway.
4490b57cec5SDimitry Andric     // So there's no difference between GNU ifunc and regular function
4500b57cec5SDimitry Andric     // symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC.
4510b57cec5SDimitry Andric     if (this->type == llvm::ELF::STT_GNU_IFUNC)
4520b57cec5SDimitry Andric       this->type = llvm::ELF::STT_FUNC;
4530b57cec5SDimitry Andric   }
454*bdd1243dSDimitry Andric   void overwrite(Symbol &sym) const {
455*bdd1243dSDimitry Andric     Symbol::overwrite(sym, SharedKind);
456*bdd1243dSDimitry Andric     auto &s = static_cast<SharedSymbol &>(sym);
457*bdd1243dSDimitry Andric     s.dsoProtected = dsoProtected;
458*bdd1243dSDimitry Andric     s.value = value;
459*bdd1243dSDimitry Andric     s.size = size;
460*bdd1243dSDimitry Andric     s.alignment = alignment;
461*bdd1243dSDimitry Andric   }
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric   uint64_t value; // st_value
4640b57cec5SDimitry Andric   uint64_t size;  // st_size
4650b57cec5SDimitry Andric   uint32_t alignment;
4660b57cec5SDimitry Andric };
4670b57cec5SDimitry Andric 
46881ad6265SDimitry Andric // LazyObject symbols represent symbols in object files between --start-lib and
46981ad6265SDimitry Andric // --end-lib options. LLD also handles traditional archives as if all the files
47081ad6265SDimitry Andric // in the archive are surrounded by --start-lib and --end-lib.
4710b57cec5SDimitry Andric //
4720b57cec5SDimitry Andric // A special complication is the handling of weak undefined symbols. They should
4730b57cec5SDimitry Andric // not load a file, but we have to remember we have seen both the weak undefined
4740b57cec5SDimitry Andric // and the lazy. We represent that with a lazy symbol with a weak binding. This
4750b57cec5SDimitry Andric // means that code looking for undefined symbols normally also has to take lazy
4760b57cec5SDimitry Andric // symbols into consideration.
4770b57cec5SDimitry Andric class LazyObject : public Symbol {
4780b57cec5SDimitry Andric public:
47981ad6265SDimitry Andric   LazyObject(InputFile &file)
48081ad6265SDimitry Andric       : Symbol(LazyObjectKind, &file, {}, llvm::ELF::STB_GLOBAL,
48181ad6265SDimitry Andric                llvm::ELF::STV_DEFAULT, llvm::ELF::STT_NOTYPE) {}
482*bdd1243dSDimitry Andric   void overwrite(Symbol &sym) const { Symbol::overwrite(sym, LazyObjectKind); }
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric   static bool classof(const Symbol *s) { return s->kind() == LazyObjectKind; }
4850b57cec5SDimitry Andric };
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric // Some linker-generated symbols need to be created as
4880b57cec5SDimitry Andric // Defined symbols.
4890b57cec5SDimitry Andric struct ElfSym {
4900b57cec5SDimitry Andric   // __bss_start
4910b57cec5SDimitry Andric   static Defined *bss;
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric   // etext and _etext
4940b57cec5SDimitry Andric   static Defined *etext1;
4950b57cec5SDimitry Andric   static Defined *etext2;
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric   // edata and _edata
4980b57cec5SDimitry Andric   static Defined *edata1;
4990b57cec5SDimitry Andric   static Defined *edata2;
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric   // end and _end
5020b57cec5SDimitry Andric   static Defined *end1;
5030b57cec5SDimitry Andric   static Defined *end2;
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric   // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
5060b57cec5SDimitry Andric   // be at some offset from the base of the .got section, usually 0 or
5070b57cec5SDimitry Andric   // the end of the .got.
5080b57cec5SDimitry Andric   static Defined *globalOffsetTable;
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric   // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
5110b57cec5SDimitry Andric   static Defined *mipsGp;
5120b57cec5SDimitry Andric   static Defined *mipsGpDisp;
5130b57cec5SDimitry Andric   static Defined *mipsLocalGp;
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric   // __rel{,a}_iplt_{start,end} symbols.
5160b57cec5SDimitry Andric   static Defined *relaIpltStart;
5170b57cec5SDimitry Andric   static Defined *relaIpltEnd;
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   // _TLS_MODULE_BASE_ on targets that support TLSDESC.
5200b57cec5SDimitry Andric   static Defined *tlsModuleBase;
5210b57cec5SDimitry Andric };
5220b57cec5SDimitry Andric 
5230b57cec5SDimitry Andric // A buffer class that is large enough to hold any Symbol-derived
5240b57cec5SDimitry Andric // object. We allocate memory using this class and instantiate a symbol
5250b57cec5SDimitry Andric // using the placement new.
52681ad6265SDimitry Andric 
52781ad6265SDimitry Andric // It is important to keep the size of SymbolUnion small for performance and
52881ad6265SDimitry Andric // memory usage reasons. 64 bytes is a soft limit based on the size of Defined
52981ad6265SDimitry Andric // on a 64-bit system. This is enforced by a static_assert in Symbols.cpp.
5300b57cec5SDimitry Andric union SymbolUnion {
5310b57cec5SDimitry Andric   alignas(Defined) char a[sizeof(Defined)];
5320b57cec5SDimitry Andric   alignas(CommonSymbol) char b[sizeof(CommonSymbol)];
5330b57cec5SDimitry Andric   alignas(Undefined) char c[sizeof(Undefined)];
5340b57cec5SDimitry Andric   alignas(SharedSymbol) char d[sizeof(SharedSymbol)];
53581ad6265SDimitry Andric   alignas(LazyObject) char e[sizeof(LazyObject)];
5360b57cec5SDimitry Andric };
5370b57cec5SDimitry Andric 
5380eae32dcSDimitry Andric template <typename... T> Defined *makeDefined(T &&...args) {
539*bdd1243dSDimitry Andric   auto *sym = getSpecificAllocSingleton<SymbolUnion>().Allocate();
540*bdd1243dSDimitry Andric   memset(sym, 0, sizeof(Symbol));
541*bdd1243dSDimitry Andric   auto &s = *new (reinterpret_cast<Defined *>(sym)) Defined(std::forward<T>(args)...);
542*bdd1243dSDimitry Andric   return &s;
5430eae32dcSDimitry Andric }
5440eae32dcSDimitry Andric 
54581ad6265SDimitry Andric void reportDuplicate(const Symbol &sym, const InputFile *newFile,
54681ad6265SDimitry Andric                      InputSectionBase *errSec, uint64_t errOffset);
5470b57cec5SDimitry Andric void maybeWarnUnorderableSymbol(const Symbol *sym);
548480093f4SDimitry Andric bool computeIsPreemptible(const Symbol &sym);
549349cc55cSDimitry Andric 
5500b57cec5SDimitry Andric } // namespace elf
5510b57cec5SDimitry Andric } // namespace lld
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric #endif
554