blob: 73b074ccd8246bc5a37db165d699cec6a1ebc5e0 [file] [log] [blame]
Rafael Espindolabeee25e2015-08-14 14:12:54 +00001//===- Symbols.h ------------------------------------------------*- C++ -*-===//
Michael J. Spencer84487f12015-07-24 21:03:07 +00002//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
10// All symbols are handled as SymbolBodies regardless of their types.
11// This file defines various types of SymbolBodies.
12//
13// File-scope symbols in ELF objects are the only exception of SymbolBody
14// instantiation. We will never create SymbolBodies for them for performance
15// reason. They are often represented as nullptrs. This is fine for symbol
16// resolution because the symbol table naturally cares only about
17// externally-visible symbols. For relocations, you have to deal with both
18// local and non-local functions, and we have two different functions
19// where we need them.
20//
21//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
23#ifndef LLD_ELF_SYMBOLS_H
24#define LLD_ELF_SYMBOLS_H
25
Rafael Espindola9d06ab62015-09-22 00:01:39 +000026#include "InputSection.h"
Rafael Espindola832b93f2015-08-24 20:06:32 +000027
Michael J. Spencer84487f12015-07-24 21:03:07 +000028#include "lld/Core/LLVM.h"
Michael J. Spencer1b348a62015-09-04 22:28:10 +000029#include "llvm/Object/Archive.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000030#include "llvm/Object/ELF.h"
31
32namespace lld {
33namespace elf2 {
34
Michael J. Spencer1b348a62015-09-04 22:28:10 +000035class ArchiveFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000036class InputFile;
37class SymbolBody;
Michael J. Spencercdae0a42015-07-28 22:58:25 +000038template <class ELFT> class ObjectFile;
Michael J. Spencer658dccd2015-09-18 22:13:25 +000039template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000040template <class ELFT> class OutputSectionBase;
Rui Ueyama35da9b62015-10-11 20:59:12 +000041template <class ELFT> class SharedFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000042
Rui Ueyama9ea49c72015-10-07 23:46:11 +000043// Initializes global objects defined in this file.
44// Called at the beginning of main().
45void initSymbols();
46
Rui Ueyamaa4a628f2016-01-13 18:55:39 +000047// Returns a demangled C++ symbol name. If Name is not a mangled
48// name or the system does not provide __cxa_demangle function,
49// it returns the unmodified string.
50std::string demangle(StringRef Name);
51
Michael J. Spencer84487f12015-07-24 21:03:07 +000052// A real symbol object, SymbolBody, is usually accessed indirectly
53// through a Symbol. There's always one Symbol for each symbol name.
54// The resolver updates SymbolBody pointers as it resolves symbols.
55struct Symbol {
Michael J. Spencer84487f12015-07-24 21:03:07 +000056 SymbolBody *Body;
57};
58
59// The base class for real symbol classes.
60class SymbolBody {
61public:
62 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000063 DefinedFirst,
64 DefinedRegularKind = DefinedFirst,
Rafael Espindola84aff152015-09-25 21:20:23 +000065 SharedKind,
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000066 DefinedElfLast = SharedKind,
Rafael Espindola11191912015-12-24 16:23:37 +000067 DefinedCommonKind,
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000068 DefinedSyntheticKind,
69 DefinedLast = DefinedSyntheticKind,
Rafael Espindola5d7593b2015-12-22 23:00:50 +000070 UndefinedElfKind,
Rafael Espindola84aff152015-09-25 21:20:23 +000071 UndefinedKind,
72 LazyKind
Michael J. Spencer84487f12015-07-24 21:03:07 +000073 };
74
Michael J. Spencercdae0a42015-07-28 22:58:25 +000075 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000076
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000077 bool isWeak() const { return IsWeak; }
Rafael Espindola5d7593b2015-12-22 23:00:50 +000078 bool isUndefined() const {
79 return SymbolKind == UndefinedKind || SymbolKind == UndefinedElfKind;
80 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000081 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000082 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000083 bool isLazy() const { return SymbolKind == LazyKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +000084 bool isShared() const { return SymbolKind == SharedKind; }
85 bool isUsedInRegularObj() const { return IsUsedInRegularObj; }
Rafael Espindola05a3dd22015-09-22 23:38:23 +000086 bool isUsedInDynamicReloc() const { return IsUsedInDynamicReloc; }
87 void setUsedInDynamicReloc() { IsUsedInDynamicReloc = true; }
Rui Ueyama61805ec2015-12-17 00:12:03 +000088 bool isTls() const { return IsTls; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +000089
Michael J. Spencer84487f12015-07-24 21:03:07 +000090 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000091 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000092
Rui Ueyama8f2c4da2015-10-21 18:13:47 +000093 uint8_t getVisibility() const { return Visibility; }
Rafael Espindola78471f02015-09-01 23:12:52 +000094
Rui Ueyamaa02bba62015-12-17 00:12:04 +000095 unsigned DynamicSymbolTableIndex = 0;
George Rimar90cd0a82015-12-01 19:20:26 +000096 uint32_t GlobalDynIndex = -1;
Rui Ueyama49c68a72015-10-09 00:42:06 +000097 uint32_t GotIndex = -1;
George Rimar648a2c32015-10-20 08:54:27 +000098 uint32_t GotPltIndex = -1;
Rui Ueyama49c68a72015-10-09 00:42:06 +000099 uint32_t PltIndex = -1;
George Rimar90cd0a82015-12-01 19:20:26 +0000100 bool hasGlobalDynIndex() { return GlobalDynIndex != uint32_t(-1); }
Rafael Espindola5c2310c2015-09-18 14:40:19 +0000101 bool isInGot() const { return GotIndex != -1U; }
George Rimar648a2c32015-10-20 08:54:27 +0000102 bool isInGotPlt() const { return GotPltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000103 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000104
Michael J. Spencer84487f12015-07-24 21:03:07 +0000105 // A SymbolBody has a backreference to a Symbol. Originally they are
106 // doubly-linked. A backreference will never change. But the pointer
107 // in the Symbol may be mutated by the resolver. If you have a
108 // pointer P to a SymbolBody and are not sure whether the resolver
109 // has chosen the object among other objects having the same name,
110 // you can access P->Backref->Body to get the resolver's result.
111 void setBackref(Symbol *P) { Backref = P; }
Rui Ueyamae66e0012015-10-07 23:20:23 +0000112 SymbolBody *repl() { return Backref ? Backref->Body : this; }
Rui Ueyamadeb15402016-01-07 17:20:07 +0000113 Symbol *getSymbol() { return Backref; }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000114
115 // Decides which symbol should "win" in the symbol table, this or
116 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
117 // they are duplicate (conflicting) symbols.
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000118 template <class ELFT> int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000119
120protected:
Igor Kudrin65bddea2015-10-09 09:58:39 +0000121 SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
Rui Ueyama61805ec2015-12-17 00:12:03 +0000122 bool IsTls)
123 : SymbolKind(K), IsWeak(IsWeak), Visibility(Visibility), IsTls(IsTls),
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000124 Name(Name) {
Rafael Espindola18173d42015-09-08 15:50:05 +0000125 IsUsedInRegularObj = K != SharedKind && K != LazyKind;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000126 IsUsedInDynamicReloc = 0;
Rafael Espindola18173d42015-09-08 15:50:05 +0000127 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000128
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000129 const unsigned SymbolKind : 8;
Rafael Espindola8614c562015-10-06 14:33:58 +0000130 unsigned IsWeak : 1;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000131 unsigned Visibility : 2;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000132
133 // True if the symbol was used for linking and thus need to be
134 // added to the output file's symbol table. It is usually true,
135 // but if it is a shared symbol that were not referenced by anyone,
136 // it can be false.
Rafael Espindola18173d42015-09-08 15:50:05 +0000137 unsigned IsUsedInRegularObj : 1;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000138
139 // If true, the symbol is added to .dynsym symbol table.
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000140 unsigned IsUsedInDynamicReloc : 1;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000141
Rui Ueyama61805ec2015-12-17 00:12:03 +0000142 unsigned IsTls : 1;
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000143 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000144 Symbol *Backref = nullptr;
145};
146
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000147// The base class for any defined symbols.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000148class Defined : public SymbolBody {
149public:
150 Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility, bool IsTls);
151 static bool classof(const SymbolBody *S) { return S->isDefined(); }
152};
153
154// Any defined symbol from an ELF file.
155template <class ELFT> class DefinedElf : public Defined {
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000156protected:
Rafael Espindola88dddf92015-12-21 21:07:31 +0000157 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000158
159public:
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000160 DefinedElf(Kind K, StringRef N, const Elf_Sym &Sym)
161 : Defined(K, N, Sym.getBinding() == llvm::ELF::STB_WEAK,
162 Sym.getVisibility(), Sym.getType() == llvm::ELF::STT_TLS),
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000163 Sym(Sym) {}
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000164
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000165 const Elf_Sym &Sym;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000166 static bool classof(const SymbolBody *S) {
167 return S->kind() <= DefinedElfLast;
168 }
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000169};
170
Rafael Espindola11191912015-12-24 16:23:37 +0000171class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000172public:
Rafael Espindola11191912015-12-24 16:23:37 +0000173 DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, bool IsWeak,
174 uint8_t Visibility);
Rafael Espindola51d46902015-08-28 21:26:51 +0000175
176 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000177 return S->kind() == SymbolBody::DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000178 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000179
180 // The output offset of this common symbol in the output bss. Computed by the
181 // writer.
Rui Ueyamae57c4872016-01-05 16:35:43 +0000182 uint64_t OffsetInBss;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000183
184 // The maximum alignment we have seen for this symbol.
Rafael Espindola11191912015-12-24 16:23:37 +0000185 uint64_t MaxAlignment;
186
187 uint64_t Size;
Rafael Espindola51d46902015-08-28 21:26:51 +0000188};
189
Michael J. Spencer84487f12015-07-24 21:03:07 +0000190// Regular defined symbols read from object file symbol tables.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000191template <class ELFT> class DefinedRegular : public DefinedElf<ELFT> {
Rafael Espindola88dddf92015-12-21 21:07:31 +0000192 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000193
Michael J. Spencer84487f12015-07-24 21:03:07 +0000194public:
Rafael Espindolac159c962015-10-19 21:00:02 +0000195 DefinedRegular(StringRef N, const Elf_Sym &Sym,
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000196 InputSectionBase<ELFT> *Section)
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000197 : DefinedElf<ELFT>(SymbolBody::DefinedRegularKind, N, Sym),
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000198 Section(Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000199
200 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000201 return S->kind() == SymbolBody::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000202 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000203
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000204 // If this is null, the symbol is absolute.
205 InputSectionBase<ELFT> *Section;
Rafael Espindolab13df652015-08-11 17:33:02 +0000206};
207
Rui Ueyama8b75b9a2015-12-17 00:48:16 +0000208// DefinedSynthetic is a class to represent linker-generated ELF symbols.
209// The difference from the regular symbol is that DefinedSynthetic symbols
210// don't belong to any input files or sections. Thus, its constructor
211// takes an output section to calculate output VA, etc.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000212template <class ELFT> class DefinedSynthetic : public Defined {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000213public:
Rafael Espindola88dddf92015-12-21 21:07:31 +0000214 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000215 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
216 DefinedSynthetic(StringRef N, uintX_t Value,
217 OutputSectionBase<ELFT> &Section);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000218
219 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000220 return S->kind() == SymbolBody::DefinedSyntheticKind;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000221 }
222
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000223 uintX_t Value;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000224 const OutputSectionBase<ELFT> &Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000225};
226
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000227// Undefined symbol.
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000228class Undefined : public SymbolBody {
229 typedef SymbolBody::Kind Kind;
230 bool CanKeepUndefined;
231
232protected:
233 Undefined(Kind K, StringRef N, bool IsWeak, uint8_t Visibility, bool IsTls);
234
235public:
236 Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
237 bool CanKeepUndefined);
238
239 static bool classof(const SymbolBody *S) { return S->isUndefined(); }
240
241 bool canKeepUndefined() const { return CanKeepUndefined; }
242};
243
244template <class ELFT> class UndefinedElf : public Undefined {
Rafael Espindola88dddf92015-12-21 21:07:31 +0000245 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000246
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000247public:
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000248 UndefinedElf(StringRef N, const Elf_Sym &Sym);
249 const Elf_Sym &Sym;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000250
251 static bool classof(const SymbolBody *S) {
252 return S->kind() == SymbolBody::UndefinedElfKind;
253 }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000254};
255
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000256template <class ELFT> class SharedSymbol : public DefinedElf<ELFT> {
Rafael Espindola88dddf92015-12-21 21:07:31 +0000257 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
George Rimarbc590fe2015-10-28 16:48:58 +0000258 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola18173d42015-09-08 15:50:05 +0000259
260public:
261 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000262 return S->kind() == SymbolBody::SharedKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000263 }
264
Rui Ueyama35da9b62015-10-11 20:59:12 +0000265 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym)
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000266 : DefinedElf<ELFT>(SymbolBody::SharedKind, Name, Sym), File(F) {}
Rui Ueyama35da9b62015-10-11 20:59:12 +0000267
268 SharedFile<ELFT> *File;
George Rimarbc590fe2015-10-28 16:48:58 +0000269
Rui Ueyamabb936062015-12-17 01:14:23 +0000270 // True if the linker has to generate a copy relocation for this shared
Rui Ueyamae57c4872016-01-05 16:35:43 +0000271 // symbol. OffsetInBss is significant only when NeedsCopy is true.
Rui Ueyamabb936062015-12-17 01:14:23 +0000272 bool NeedsCopy = false;
Rui Ueyamae57c4872016-01-05 16:35:43 +0000273 uintX_t OffsetInBss = 0;
Rafael Espindola18173d42015-09-08 15:50:05 +0000274};
275
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000276// This class represents a symbol defined in an archive file. It is
277// created from an archive file header, and it knows how to load an
278// object file from an archive to replace itself with a defined
279// symbol. If the resolver finds both Undefined and Lazy for
280// the same name, it will ask the Lazy to load a file.
281class Lazy : public SymbolBody {
282public:
283 Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
Igor Kudrin65bddea2015-10-09 09:58:39 +0000284 : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT, false),
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000285 File(F), Sym(S) {}
286
287 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
288
289 // Returns an object file for this symbol, or a nullptr if the file
290 // was already returned.
291 std::unique_ptr<InputFile> getMember();
292
Rafael Espindola8614c562015-10-06 14:33:58 +0000293 void setWeak() { IsWeak = true; }
294 void setUsedInRegularObj() { IsUsedInRegularObj = true; }
295
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000296private:
297 ArchiveFile *File;
298 const llvm::object::Archive::Symbol Sym;
299};
300
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000301// Some linker-generated symbols need to be created as
302// DefinedRegular symbols, so they need Elf_Sym symbols.
303// Here we allocate such Elf_Sym symbols statically.
304template <class ELFT> struct ElfSym {
305 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
306
Rafael Espindola65e80b92016-01-19 21:19:52 +0000307 // Used to represent an undefined symbol which we don't want to add to the
308 // output file's symbol table. It has weak binding and can be substituted.
309 static Elf_Sym Ignored;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000310
311 // The content for _end and end symbols.
312 static Elf_Sym End;
313
314 // The content for _gp symbol for MIPS target.
315 static Elf_Sym MipsGp;
316
317 // __rel_iplt_start/__rel_iplt_end for signaling
318 // where R_[*]_IRELATIVE relocations do live.
319 static Elf_Sym RelaIpltStart;
320 static Elf_Sym RelaIpltEnd;
321};
322
Rafael Espindola65e80b92016-01-19 21:19:52 +0000323template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::Ignored;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000324template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::End;
325template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::MipsGp;
326template <class ELFT>
327typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::RelaIpltStart;
328template <class ELFT> typename ElfSym<ELFT>::Elf_Sym ElfSym<ELFT>::RelaIpltEnd;
329
Michael J. Spencer84487f12015-07-24 21:03:07 +0000330} // namespace elf2
331} // namespace lld
332
333#endif