1 //===- Symbols.h ------------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines various types of Symbols.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLD_ELF_SYMBOLS_H
15 #define LLD_ELF_SYMBOLS_H
17 #include "InputSection.h"
18 #include "lld/Common/LLVM.h"
19 #include "lld/Common/Strings.h"
20 #include "llvm/Object/Archive.h"
21 #include "llvm/Object/ELF.h"
31 template <class ELFT> class ObjFile;
33 template <class ELFT> class SharedFile;
35 // This is a StringRef-like container that doesn't run strlen().
37 // ELF string tables contain a lot of null-terminated strings. Most of them
38 // are not necessary for the linker because they are names of local symbols,
39 // and the linker doesn't use local symbol names for name resolution. So, we
40 // use this class to represents strings read from string tables.
42 StringRefZ(const char *S) : Data(S), Size(-1) {}
43 StringRefZ(StringRef S) : Data(S.data()), Size(S.size()) {}
49 // The base class for real symbol classes.
60 Kind kind() const { return static_cast<Kind>(SymbolKind); }
62 // The file from which this symbol was created.
67 mutable uint32_t NameSize;
70 uint32_t DynsymIndex = 0;
71 uint32_t GotIndex = -1;
72 uint32_t PltIndex = -1;
73 uint32_t GlobalDynIndex = -1;
75 // This field is a index to the symbol's version definition.
76 uint32_t VerdefIndex = -1;
78 // Version definition index.
81 // Symbol binding. This is not overwritten by replaceSymbol to track
82 // changes during resolution. In particular:
83 // - An undefined weak is still weak when it resolves to a shared library.
84 // - An undefined weak will not fetch archive members, but we have to
85 // remember it is weak.
88 // The following fields have the same meaning as the ELF symbol attributes.
89 uint8_t Type; // symbol type
90 uint8_t StOther; // st_other field value
92 const uint8_t SymbolKind;
94 // Symbol visibility. This is the computed minimum visibility of all
95 // observed non-DSO symbols.
96 unsigned Visibility : 2;
98 // True if the symbol was used for linking and thus need to be added to the
99 // output file's symbol table. This is true for all symbols except for
100 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
101 // by other bitcode objects.
102 unsigned IsUsedInRegularObj : 1;
104 // If this flag is true and the symbol has protected or default visibility, it
105 // will appear in .dynsym. This flag is set by interposable DSO symbols in
106 // executables, by most symbols in DSOs and executables built with
107 // --export-dynamic, and by dynamic lists.
108 unsigned ExportDynamic : 1;
110 // False if LTO shouldn't inline whatever this symbol points to. If a symbol
111 // is overwritten after LTO, LTO shouldn't inline the symbol because it
112 // doesn't know the final contents of the symbol.
113 unsigned CanInline : 1;
115 // True if this symbol is specified by --trace-symbol option.
118 bool includeInDynsym() const;
119 uint8_t computeBinding() const;
120 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
122 bool isUndefined() const { return SymbolKind == UndefinedKind; }
123 bool isDefined() const { return SymbolKind == DefinedKind; }
124 bool isShared() const { return SymbolKind == SharedKind; }
125 bool isLocal() const { return Binding == llvm::ELF::STB_LOCAL; }
127 bool isLazy() const {
128 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
131 // True if this is an undefined weak symbol.
132 bool isUndefWeak() const { return isWeak() && isUndefined(); }
134 StringRef getName() const {
135 if (NameSize == (uint32_t)-1)
136 NameSize = strlen(NameData);
137 return {NameData, NameSize};
140 void parseSymbolVersion();
142 bool isInGot() const { return GotIndex != -1U; }
143 bool isInPlt() const { return PltIndex != -1U; }
145 uint64_t getVA(int64_t Addend = 0) const;
147 uint64_t getGotOffset() const;
148 uint64_t getGotVA() const;
149 uint64_t getGotPltOffset() const;
150 uint64_t getGotPltVA() const;
151 uint64_t getPltVA() const;
152 uint64_t getPltOffset() const;
153 uint64_t getSize() const;
154 OutputSection *getOutputSection() const;
157 Symbol(Kind K, InputFile *File, StringRefZ Name, uint8_t Binding,
158 uint8_t StOther, uint8_t Type)
159 : File(File), NameData(Name.Data), NameSize(Name.Size), Binding(Binding),
160 Type(Type), StOther(StOther), SymbolKind(K), NeedsPltAddr(false),
161 IsInIplt(false), IsInIgot(false), IsPreemptible(false),
162 Used(!Config->GcSections), NeedsTocRestore(false) {}
165 // True the symbol should point to its PLT entry.
166 // For SharedSymbol only.
167 unsigned NeedsPltAddr : 1;
169 // True if this symbol is in the Iplt sub-section of the Plt.
170 unsigned IsInIplt : 1;
172 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
173 unsigned IsInIgot : 1;
175 // True if this symbol is preemptible at load time.
176 unsigned IsPreemptible : 1;
178 // True if an undefined or shared symbol is used from a live section.
181 // True if a call to this symbol needs to be followed by a restore of the
182 // PPC64 toc pointer.
183 unsigned NeedsTocRestore : 1;
185 // The Type field may also have this value. It means that we have not yet seen
186 // a non-Lazy symbol with this name, so we don't know what its type is. The
187 // Type field is normally set to this value for Lazy symbols unless we saw a
188 // weak undefined symbol first, in which case we need to remember the original
189 // symbol's type in order to check for TLS mismatches.
190 enum { UnknownType = 255 };
192 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
193 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
194 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
195 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
196 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
197 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
200 // Represents a symbol that is defined in the current output file.
201 class Defined : public Symbol {
203 Defined(InputFile *File, StringRefZ Name, uint8_t Binding, uint8_t StOther,
204 uint8_t Type, uint64_t Value, uint64_t Size, SectionBase *Section)
205 : Symbol(DefinedKind, File, Name, Binding, StOther, Type), Value(Value),
206 Size(Size), Section(Section) {}
208 static bool classof(const Symbol *S) { return S->isDefined(); }
212 SectionBase *Section;
215 class Undefined : public Symbol {
217 Undefined(InputFile *File, StringRefZ Name, uint8_t Binding, uint8_t StOther,
219 : Symbol(UndefinedKind, File, Name, Binding, StOther, Type) {}
221 static bool classof(const Symbol *S) { return S->kind() == UndefinedKind; }
224 class SharedSymbol : public Symbol {
226 static bool classof(const Symbol *S) { return S->kind() == SharedKind; }
228 SharedSymbol(InputFile &File, StringRef Name, uint8_t Binding,
229 uint8_t StOther, uint8_t Type, uint64_t Value, uint64_t Size,
230 uint32_t Alignment, uint32_t VerdefIndex)
231 : Symbol(SharedKind, &File, Name, Binding, StOther, Type),
232 Alignment(Alignment), Value(Value), Size(Size) {
233 this->VerdefIndex = VerdefIndex;
234 // GNU ifunc is a mechanism to allow user-supplied functions to
235 // resolve PLT slot values at load-time. This is contrary to the
236 // regular symbol resolution scheme in which symbols are resolved just
237 // by name. Using this hook, you can program how symbols are solved
238 // for you program. For example, you can make "memcpy" to be resolved
239 // to a SSE-enabled version of memcpy only when a machine running the
240 // program supports the SSE instruction set.
242 // Naturally, such symbols should always be called through their PLT
243 // slots. What GNU ifunc symbols point to are resolver functions, and
244 // calling them directly doesn't make sense (unless you are writing a
247 // For DSO symbols, we always call them through PLT slots anyway.
248 // So there's no difference between GNU ifunc and regular function
249 // symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC.
250 if (this->Type == llvm::ELF::STT_GNU_IFUNC)
251 this->Type = llvm::ELF::STT_FUNC;
254 template <class ELFT> SharedFile<ELFT> &getFile() const {
255 return *cast<SharedFile<ELFT>>(File);
260 uint64_t Value; // st_value
261 uint64_t Size; // st_size
264 // LazyArchive and LazyObject represent a symbols that is not yet in the link,
265 // but we know where to find it if needed. If the resolver finds both Undefined
266 // and Lazy for the same name, it will ask the Lazy to load a file.
268 // A special complication is the handling of weak undefined symbols. They should
269 // not load a file, but we have to remember we have seen both the weak undefined
270 // and the lazy. We represent that with a lazy symbol with a weak binding. This
271 // means that code looking for undefined symbols normally also has to take lazy
272 // symbols into consideration.
274 // This class represents a symbol defined in an archive file. It is
275 // created from an archive file header, and it knows how to load an
276 // object file from an archive to replace itself with a defined
278 class LazyArchive : public Symbol {
280 LazyArchive(InputFile &File, uint8_t Type,
281 const llvm::object::Archive::Symbol S)
282 : Symbol(LazyArchiveKind, &File, S.getName(), llvm::ELF::STB_GLOBAL,
283 llvm::ELF::STV_DEFAULT, Type),
286 static bool classof(const Symbol *S) { return S->kind() == LazyArchiveKind; }
291 const llvm::object::Archive::Symbol Sym;
294 // LazyObject symbols represents symbols in object files between
295 // --start-lib and --end-lib options.
296 class LazyObject : public Symbol {
298 LazyObject(InputFile &File, uint8_t Type, StringRef Name)
299 : Symbol(LazyObjectKind, &File, Name, llvm::ELF::STB_GLOBAL,
300 llvm::ELF::STV_DEFAULT, Type) {}
302 static bool classof(const Symbol *S) { return S->kind() == LazyObjectKind; }
305 // Some linker-generated symbols need to be created as
312 static Defined *Etext1;
313 static Defined *Etext2;
316 static Defined *Edata1;
317 static Defined *Edata2;
320 static Defined *End1;
321 static Defined *End2;
323 // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
324 // be at some offset from the base of the .got section, usually 0 or
325 // the end of the .got.
326 static Defined *GlobalOffsetTable;
328 // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
329 static Defined *MipsGp;
330 static Defined *MipsGpDisp;
331 static Defined *MipsLocalGp;
333 // __rela_iplt_end or __rel_iplt_end
334 static Defined *RelaIpltEnd;
337 // A buffer class that is large enough to hold any Symbol-derived
338 // object. We allocate memory using this class and instantiate a symbol
339 // using the placement new.
341 alignas(Defined) char A[sizeof(Defined)];
342 alignas(Undefined) char C[sizeof(Undefined)];
343 alignas(SharedSymbol) char D[sizeof(SharedSymbol)];
344 alignas(LazyArchive) char E[sizeof(LazyArchive)];
345 alignas(LazyObject) char F[sizeof(LazyObject)];
348 void printTraceSymbol(Symbol *Sym);
350 template <typename T, typename... ArgT>
351 void replaceSymbol(Symbol *S, ArgT &&... Arg) {
352 static_assert(std::is_trivially_destructible<T>(),
353 "Symbol types must be trivially destructible");
354 static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
355 static_assert(alignof(T) <= alignof(SymbolUnion),
356 "SymbolUnion not aligned enough");
357 assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
362 new (S) T(std::forward<ArgT>(Arg)...);
364 S->VersionId = Sym.VersionId;
365 S->Visibility = Sym.Visibility;
366 S->IsUsedInRegularObj = Sym.IsUsedInRegularObj;
367 S->ExportDynamic = Sym.ExportDynamic;
368 S->CanInline = Sym.CanInline;
369 S->Traced = Sym.Traced;
371 // Print out a log message if --trace-symbol was specified.
372 // This is for debugging.
377 void warnUnorderableSymbol(const Symbol *Sym);
380 std::string toString(const elf::Symbol &B);