blob: 2ce4fe86070f6af53073d9e97c4b7945879b1b3c [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
Michael J. Spencer84487f12015-07-24 21:03:07 +000047// A real symbol object, SymbolBody, is usually accessed indirectly
48// through a Symbol. There's always one Symbol for each symbol name.
49// The resolver updates SymbolBody pointers as it resolves symbols.
50struct Symbol {
Michael J. Spencer84487f12015-07-24 21:03:07 +000051 SymbolBody *Body;
52};
53
54// The base class for real symbol classes.
55class SymbolBody {
56public:
57 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000058 DefinedFirst,
59 DefinedRegularKind = DefinedFirst,
60 DefinedAbsoluteKind,
61 DefinedCommonKind,
Rafael Espindola84aff152015-09-25 21:20:23 +000062 SharedKind,
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000063 DefinedElfLast = SharedKind,
64 DefinedSyntheticKind,
65 DefinedLast = DefinedSyntheticKind,
Rafael Espindola5d7593b2015-12-22 23:00:50 +000066 UndefinedElfKind,
Rafael Espindola84aff152015-09-25 21:20:23 +000067 UndefinedKind,
68 LazyKind
Michael J. Spencer84487f12015-07-24 21:03:07 +000069 };
70
Michael J. Spencercdae0a42015-07-28 22:58:25 +000071 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000072
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000073 bool isWeak() const { return IsWeak; }
Rafael Espindola5d7593b2015-12-22 23:00:50 +000074 bool isUndefined() const {
75 return SymbolKind == UndefinedKind || SymbolKind == UndefinedElfKind;
76 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000077 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +000078 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000079 bool isLazy() const { return SymbolKind == LazyKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +000080 bool isShared() const { return SymbolKind == SharedKind; }
81 bool isUsedInRegularObj() const { return IsUsedInRegularObj; }
Rafael Espindola05a3dd22015-09-22 23:38:23 +000082 bool isUsedInDynamicReloc() const { return IsUsedInDynamicReloc; }
83 void setUsedInDynamicReloc() { IsUsedInDynamicReloc = true; }
Rui Ueyama61805ec2015-12-17 00:12:03 +000084 bool isTls() const { return IsTls; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +000085
Michael J. Spencer84487f12015-07-24 21:03:07 +000086 // Returns the symbol name.
Michael J. Spencercdae0a42015-07-28 22:58:25 +000087 StringRef getName() const { return Name; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000088
Rui Ueyama8f2c4da2015-10-21 18:13:47 +000089 uint8_t getVisibility() const { return Visibility; }
Rafael Espindola78471f02015-09-01 23:12:52 +000090
Rui Ueyamaa02bba62015-12-17 00:12:04 +000091 unsigned DynamicSymbolTableIndex = 0;
George Rimar90cd0a82015-12-01 19:20:26 +000092 uint32_t GlobalDynIndex = -1;
Rui Ueyama49c68a72015-10-09 00:42:06 +000093 uint32_t GotIndex = -1;
George Rimar648a2c32015-10-20 08:54:27 +000094 uint32_t GotPltIndex = -1;
Rui Ueyama49c68a72015-10-09 00:42:06 +000095 uint32_t PltIndex = -1;
George Rimar90cd0a82015-12-01 19:20:26 +000096 bool hasGlobalDynIndex() { return GlobalDynIndex != uint32_t(-1); }
Rafael Espindola5c2310c2015-09-18 14:40:19 +000097 bool isInGot() const { return GotIndex != -1U; }
George Rimar648a2c32015-10-20 08:54:27 +000098 bool isInGotPlt() const { return GotPltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +000099 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000100
Michael J. Spencer84487f12015-07-24 21:03:07 +0000101 // A SymbolBody has a backreference to a Symbol. Originally they are
102 // doubly-linked. A backreference will never change. But the pointer
103 // in the Symbol may be mutated by the resolver. If you have a
104 // pointer P to a SymbolBody and are not sure whether the resolver
105 // has chosen the object among other objects having the same name,
106 // you can access P->Backref->Body to get the resolver's result.
107 void setBackref(Symbol *P) { Backref = P; }
Rui Ueyamae66e0012015-10-07 23:20:23 +0000108 SymbolBody *repl() { return Backref ? Backref->Body : this; }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000109
110 // Decides which symbol should "win" in the symbol table, this or
111 // the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
112 // they are duplicate (conflicting) symbols.
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000113 template <class ELFT> int compare(SymbolBody *Other);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000114
115protected:
Igor Kudrin65bddea2015-10-09 09:58:39 +0000116 SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
Rui Ueyama61805ec2015-12-17 00:12:03 +0000117 bool IsTls)
118 : SymbolKind(K), IsWeak(IsWeak), Visibility(Visibility), IsTls(IsTls),
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000119 Name(Name) {
Rafael Espindola18173d42015-09-08 15:50:05 +0000120 IsUsedInRegularObj = K != SharedKind && K != LazyKind;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000121 IsUsedInDynamicReloc = 0;
Rafael Espindola18173d42015-09-08 15:50:05 +0000122 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000123
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000124 const unsigned SymbolKind : 8;
Rafael Espindola8614c562015-10-06 14:33:58 +0000125 unsigned IsWeak : 1;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000126 unsigned Visibility : 2;
Rafael Espindola18173d42015-09-08 15:50:05 +0000127 unsigned IsUsedInRegularObj : 1;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000128 unsigned IsUsedInDynamicReloc : 1;
Rui Ueyama61805ec2015-12-17 00:12:03 +0000129 unsigned IsTls : 1;
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000130 StringRef Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000131 Symbol *Backref = nullptr;
132};
133
Rui Ueyama34f29242015-10-13 19:51:57 +0000134// The base class for any defined symbols, including absolute symbols, etc.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000135class Defined : public SymbolBody {
136public:
137 Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility, bool IsTls);
138 static bool classof(const SymbolBody *S) { return S->isDefined(); }
139};
140
141// Any defined symbol from an ELF file.
142template <class ELFT> class DefinedElf : public Defined {
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000143protected:
Rafael Espindola88dddf92015-12-21 21:07:31 +0000144 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000145
146public:
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000147 DefinedElf(Kind K, StringRef N, const Elf_Sym &Sym)
148 : Defined(K, N, Sym.getBinding() == llvm::ELF::STB_WEAK,
149 Sym.getVisibility(), Sym.getType() == llvm::ELF::STT_TLS),
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000150 Sym(Sym) {}
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000151
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000152 const Elf_Sym &Sym;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000153 static bool classof(const SymbolBody *S) {
154 return S->kind() <= DefinedElfLast;
155 }
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000156};
157
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000158template <class ELFT> class DefinedAbsolute : public DefinedElf<ELFT> {
Rafael Espindola88dddf92015-12-21 21:07:31 +0000159 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola0e0c1902015-08-27 12:40:06 +0000160
161public:
Rafael Espindolad27adc42e2015-09-24 13:34:01 +0000162 static Elf_Sym IgnoreUndef;
163
Igor Kudrinb044af52015-11-20 02:32:35 +0000164 // The following symbols must be added early to reserve their places
165 // in symbol tables. The value of the symbols are set when all sections
166 // are finalized and their addresses are determined.
167
168 // The content for _end and end symbols.
169 static Elf_Sym End;
170
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000171 // The content for _gp symbol for MIPS target.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000172 static Elf_Sym MipsGp;
173
George Rimara07ff662015-12-21 10:12:06 +0000174 // __rel_iplt_start/__rel_iplt_end for signaling
175 // where R_[*]_IRELATIVE relocations do live.
176 static Elf_Sym RelaIpltStart;
177 static Elf_Sym RelaIpltEnd;
178
Rui Ueyama294b1362015-09-30 02:06:17 +0000179 DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000180 : DefinedElf<ELFT>(SymbolBody::DefinedAbsoluteKind, N, Sym) {}
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000181
182 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000183 return S->kind() == SymbolBody::DefinedAbsoluteKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000184 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000185};
186
Rafael Espindolad27adc42e2015-09-24 13:34:01 +0000187template <class ELFT>
188typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::IgnoreUndef;
189
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000190template <class ELFT>
Igor Kudrinb044af52015-11-20 02:32:35 +0000191typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::End;
192
193template <class ELFT>
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000194typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::MipsGp;
195
George Rimara07ff662015-12-21 10:12:06 +0000196template <class ELFT>
197typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::RelaIpltStart;
198
199template <class ELFT>
200typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::RelaIpltEnd;
201
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000202template <class ELFT> class DefinedCommon : public DefinedElf<ELFT> {
Rafael Espindola88dddf92015-12-21 21:07:31 +0000203 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola51d46902015-08-28 21:26:51 +0000204
205public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000206 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama294b1362015-09-30 02:06:17 +0000207 DefinedCommon(StringRef N, const Elf_Sym &Sym)
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000208 : DefinedElf<ELFT>(SymbolBody::DefinedCommonKind, N, Sym) {
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000209 MaxAlignment = Sym.st_value;
210 }
Rafael Espindola51d46902015-08-28 21:26:51 +0000211
212 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000213 return S->kind() == SymbolBody::DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000214 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000215
216 // The output offset of this common symbol in the output bss. Computed by the
217 // writer.
218 uintX_t OffsetInBSS;
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000219
220 // The maximum alignment we have seen for this symbol.
221 uintX_t MaxAlignment;
Rafael Espindola51d46902015-08-28 21:26:51 +0000222};
223
Michael J. Spencer84487f12015-07-24 21:03:07 +0000224// Regular defined symbols read from object file symbol tables.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000225template <class ELFT> class DefinedRegular : public DefinedElf<ELFT> {
Rafael Espindola88dddf92015-12-21 21:07:31 +0000226 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolac44d17a2015-08-14 15:10:49 +0000227
Michael J. Spencer84487f12015-07-24 21:03:07 +0000228public:
Rafael Espindolac159c962015-10-19 21:00:02 +0000229 DefinedRegular(StringRef N, const Elf_Sym &Sym,
230 InputSectionBase<ELFT> &Section)
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000231 : DefinedElf<ELFT>(SymbolBody::DefinedRegularKind, N, Sym),
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000232 Section(Section) {}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000233
234 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000235 return S->kind() == SymbolBody::DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000236 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000237
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000238 InputSectionBase<ELFT> &Section;
Rafael Espindolab13df652015-08-11 17:33:02 +0000239};
240
Rui Ueyama8b75b9a2015-12-17 00:48:16 +0000241// DefinedSynthetic is a class to represent linker-generated ELF symbols.
242// The difference from the regular symbol is that DefinedSynthetic symbols
243// don't belong to any input files or sections. Thus, its constructor
244// takes an output section to calculate output VA, etc.
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000245template <class ELFT> class DefinedSynthetic : public Defined {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000246public:
Rafael Espindola88dddf92015-12-21 21:07:31 +0000247 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000248 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
249 DefinedSynthetic(StringRef N, uintX_t Value,
250 OutputSectionBase<ELFT> &Section);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000251
252 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000253 return S->kind() == SymbolBody::DefinedSyntheticKind;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000254 }
255
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000256 uintX_t Value;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000257 const OutputSectionBase<ELFT> &Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000258};
259
Rafael Espindolaf7d45f02015-08-31 01:46:20 +0000260// Undefined symbol.
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000261class Undefined : public SymbolBody {
262 typedef SymbolBody::Kind Kind;
263 bool CanKeepUndefined;
264
265protected:
266 Undefined(Kind K, StringRef N, bool IsWeak, uint8_t Visibility, bool IsTls);
267
268public:
269 Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
270 bool CanKeepUndefined);
271
272 static bool classof(const SymbolBody *S) { return S->isUndefined(); }
273
274 bool canKeepUndefined() const { return CanKeepUndefined; }
275};
276
277template <class ELFT> class UndefinedElf : public Undefined {
Rafael Espindola88dddf92015-12-21 21:07:31 +0000278 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000279
Rafael Espindola76e24ea2015-08-11 17:57:05 +0000280public:
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000281 UndefinedElf(StringRef N, const Elf_Sym &Sym);
282 const Elf_Sym &Sym;
Rafael Espindola5f2c46d2015-12-23 01:06:39 +0000283
284 static bool classof(const SymbolBody *S) {
285 return S->kind() == SymbolBody::UndefinedElfKind;
286 }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000287};
288
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000289template <class ELFT> class SharedSymbol : public DefinedElf<ELFT> {
Rafael Espindola88dddf92015-12-21 21:07:31 +0000290 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
George Rimarbc590fe2015-10-28 16:48:58 +0000291 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola18173d42015-09-08 15:50:05 +0000292
293public:
294 static bool classof(const SymbolBody *S) {
Rafael Espindola1e2967e2015-12-21 20:47:33 +0000295 return S->kind() == SymbolBody::SharedKind;
Rafael Espindola18173d42015-09-08 15:50:05 +0000296 }
297
Rui Ueyama35da9b62015-10-11 20:59:12 +0000298 SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym)
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000299 : DefinedElf<ELFT>(SymbolBody::SharedKind, Name, Sym), File(F) {}
Rui Ueyama35da9b62015-10-11 20:59:12 +0000300
301 SharedFile<ELFT> *File;
George Rimarbc590fe2015-10-28 16:48:58 +0000302
Rui Ueyamabb936062015-12-17 01:14:23 +0000303 // True if the linker has to generate a copy relocation for this shared
304 // symbol. OffsetInBSS is significant only when NeedsCopy is true.
305 bool NeedsCopy = false;
306 uintX_t OffsetInBSS = 0;
Rafael Espindola18173d42015-09-08 15:50:05 +0000307};
308
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000309// This class represents a symbol defined in an archive file. It is
310// created from an archive file header, and it knows how to load an
311// object file from an archive to replace itself with a defined
312// symbol. If the resolver finds both Undefined and Lazy for
313// the same name, it will ask the Lazy to load a file.
314class Lazy : public SymbolBody {
315public:
316 Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
Igor Kudrin65bddea2015-10-09 09:58:39 +0000317 : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT, false),
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000318 File(F), Sym(S) {}
319
320 static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
321
322 // Returns an object file for this symbol, or a nullptr if the file
323 // was already returned.
324 std::unique_ptr<InputFile> getMember();
325
Rafael Espindola8614c562015-10-06 14:33:58 +0000326 void setWeak() { IsWeak = true; }
327 void setUsedInRegularObj() { IsUsedInRegularObj = true; }
328
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000329private:
330 ArchiveFile *File;
331 const llvm::object::Archive::Symbol Sym;
332};
333
Michael J. Spencer84487f12015-07-24 21:03:07 +0000334} // namespace elf2
335} // namespace lld
336
337#endif