blob: 4f497af263cdef10894a88e02eaecf89406a910c [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//
Rui Ueyama34f29242015-10-13 19:51:57 +000013//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000014
15#ifndef LLD_ELF_SYMBOLS_H
16#define LLD_ELF_SYMBOLS_H
17
Rafael Espindola9d06ab62015-09-22 00:01:39 +000018#include "InputSection.h"
Rui Ueyamaa13efc22016-11-29 18:05:04 +000019#include "Strings.h"
Rafael Espindola832b93f2015-08-24 20:06:32 +000020
Rui Ueyama3f851702017-10-02 21:00:41 +000021#include "lld/Common/LLVM.h"
Michael J. Spencer1b348a62015-09-04 22:28:10 +000022#include "llvm/Object/Archive.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000023#include "llvm/Object/ELF.h"
24
25namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000026namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000027
Michael J. Spencer1b348a62015-09-04 22:28:10 +000028class ArchiveFile;
Peter Collingbourne4f952702016-05-01 04:55:03 +000029class BitcodeFile;
Dmitry Mikulin1e30f072017-09-08 16:22:43 +000030class BssSection;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031class InputFile;
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000032class LazyObjFile;
33template <class ELFT> class ObjFile;
Rafael Espindola24e6f362017-02-24 15:07:30 +000034class OutputSection;
Rui Ueyama35da9b62015-10-11 20:59:12 +000035template <class ELFT> class SharedFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000036
Michael J. Spencer84487f12015-07-24 21:03:07 +000037// The base class for real symbol classes.
Rui Ueyamaf52496e2017-11-03 21:21:47 +000038class Symbol {
Michael J. Spencer84487f12015-07-24 21:03:07 +000039public:
40 enum Kind {
Rafael Espindola84aff152015-09-25 21:20:23 +000041 DefinedFirst,
42 DefinedRegularKind = DefinedFirst,
Rafael Espindola11191912015-12-24 16:23:37 +000043 DefinedCommonKind,
Rafael Espindola5616adf2017-03-08 22:36:28 +000044 DefinedLast = DefinedCommonKind,
Rui Ueyama32665f72017-11-06 04:13:24 +000045 SharedKind,
Peter Collingbourne60976ed2016-04-27 00:05:06 +000046 UndefinedKind,
Rui Ueyamaf8baa662016-04-07 19:24:51 +000047 LazyArchiveKind,
48 LazyObjectKind,
Michael J. Spencer84487f12015-07-24 21:03:07 +000049 };
50
Rui Ueyamaf52496e2017-11-03 21:21:47 +000051 Symbol(Kind K) : SymbolKind(K) {}
Michael J. Spencercdae0a42015-07-28 22:58:25 +000052 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000053
Rui Ueyamaf1f00842017-10-31 16:07:41 +000054 // Symbol binding. This is not overwritten by replaceSymbol to track
55 // changes during resolution. In particular:
56 // - An undefined weak is still weak when it resolves to a shared library.
57 // - An undefined weak will not fetch archive members, but we have to
58 // remember it is weak.
59 uint8_t Binding;
60
61 // Version definition index.
62 uint16_t VersionId;
63
64 // Symbol visibility. This is the computed minimum visibility of all
65 // observed non-DSO symbols.
66 unsigned Visibility : 2;
67
68 // True if the symbol was used for linking and thus need to be added to the
69 // output file's symbol table. This is true for all symbols except for
70 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
71 // by other bitcode objects.
72 unsigned IsUsedInRegularObj : 1;
73
74 // If this flag is true and the symbol has protected or default visibility, it
75 // will appear in .dynsym. This flag is set by interposable DSO symbols in
76 // executables, by most symbols in DSOs and executables built with
77 // --export-dynamic, and by dynamic lists.
78 unsigned ExportDynamic : 1;
79
80 // False if LTO shouldn't inline whatever this symbol points to. If a symbol
81 // is overwritten after LTO, LTO shouldn't inline the symbol because it
82 // doesn't know the final contents of the symbol.
83 unsigned CanInline : 1;
84
85 // True if this symbol is specified by --trace-symbol option.
86 unsigned Traced : 1;
87
88 // This symbol version was found in a version script.
89 unsigned InVersionScript : 1;
90
91 // The file from which this symbol was created.
92 InputFile *File = nullptr;
93
94 bool includeInDynsym() const;
95 uint8_t computeBinding() const;
96 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
97
Peter Collingbourne60976ed2016-04-27 00:05:06 +000098 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Michael J. Spencer1b348a62015-09-04 22:28:10 +000099 bool isDefined() const { return SymbolKind <= DefinedLast; }
Rafael Espindola30e17972015-08-30 23:17:30 +0000100 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
Rui Ueyamac0f56482017-10-13 03:37:11 +0000101 bool isShared() const { return SymbolKind == SharedKind; }
102 bool isLocal() const { return IsLocal; }
103
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000104 bool isLazy() const {
105 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
106 }
Rui Ueyamac0f56482017-10-13 03:37:11 +0000107
Rui Ueyamabda337a2017-10-27 22:54:16 +0000108 bool isInCurrentOutput() const {
Rui Ueyamac0f56482017-10-13 03:37:11 +0000109 return SymbolKind == DefinedRegularKind || SymbolKind == DefinedCommonKind;
George Rimard92e1282017-07-12 11:09:46 +0000110 }
Rafael Espindola3d9f1c02017-09-13 20:43:04 +0000111
112 // True is this is an undefined weak symbol. This only works once
113 // all input files have been added.
114 bool isUndefWeak() const;
115
Rafael Espindola6e93d052017-08-04 22:31:42 +0000116 InputFile *getFile() const;
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000117 StringRef getName() const { return Name; }
Rui Ueyamab5792b22016-04-04 19:09:08 +0000118 uint8_t getVisibility() const { return StOther & 0x3; }
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000119 void parseSymbolVersion();
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000120 void copyFrom(Symbol *Other);
Rafael Espindola78471f02015-09-01 23:12:52 +0000121
Rafael Espindola5c2310c2015-09-18 14:40:19 +0000122 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000123 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000124
George Rimarf64618a2017-03-17 11:56:54 +0000125 uint64_t getVA(int64_t Addend = 0) const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000126
George Rimar4afe42e2017-03-17 14:12:51 +0000127 uint64_t getGotOffset() const;
Rafael Espindolaf9e3c9c2017-05-11 23:28:49 +0000128 uint64_t getGotVA() const;
George Rimar4670bb02017-03-16 12:58:11 +0000129 uint64_t getGotPltOffset() const;
130 uint64_t getGotPltVA() const;
George Rimarf64618a2017-03-17 11:56:54 +0000131 uint64_t getPltVA() const;
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000132 uint64_t getSize() const;
George Rimar69268a82017-03-16 11:06:13 +0000133 OutputSection *getOutputSection() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000134
Rafael Espindola7ae21772016-10-26 00:58:23 +0000135 uint32_t DynsymIndex = 0;
Rafael Espindolaca656232016-10-21 20:32:41 +0000136 uint32_t GotIndex = -1;
137 uint32_t GotPltIndex = -1;
138 uint32_t PltIndex = -1;
139 uint32_t GlobalDynIndex = -1;
140
Michael J. Spencer84487f12015-07-24 21:03:07 +0000141protected:
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000142 Symbol(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type)
Rui Ueyama7833afd2017-10-27 23:26:46 +0000143 : SymbolKind(K), IsLocal(IsLocal), NeedsPltAddr(false),
144 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
145 IsInIgot(false), IsPreemptible(false), Type(Type), StOther(StOther),
146 Name(Name) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000147
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000148 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000149
Rui Ueyama662bb002017-10-13 03:37:26 +0000150 // True if this is a local symbol.
151 unsigned IsLocal : 1;
152
Rafael Espindolaabebed92016-02-05 15:27:15 +0000153public:
Rui Ueyama924b3612017-02-16 06:12:22 +0000154 // True the symbol should point to its PLT entry.
155 // For SharedSymbol only.
156 unsigned NeedsPltAddr : 1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000157 // True if this symbol has an entry in the global part of MIPS GOT.
158 unsigned IsInGlobalMipsGot : 1;
159
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000160 // True if this symbol is referenced by 32-bit GOT relocations.
161 unsigned Is32BitMipsGot : 1;
162
Peter Smithbaffdb82016-12-08 12:58:55 +0000163 // True if this symbol is in the Iplt sub-section of the Plt.
164 unsigned IsInIplt : 1;
165
166 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
167 unsigned IsInIgot : 1;
168
Rafael Espindola35c908f2017-08-10 15:05:37 +0000169 unsigned IsPreemptible : 1;
170
Rui Ueyamadd418072016-04-04 18:15:38 +0000171 // The following fields have the same meaning as the ELF symbol attributes.
172 uint8_t Type; // symbol type
Rui Ueyamab5792b22016-04-04 19:09:08 +0000173 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000174
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000175 // The Type field may also have this value. It means that we have not yet seen
176 // a non-Lazy symbol with this name, so we don't know what its type is. The
177 // Type field is normally set to this value for Lazy symbols unless we saw a
178 // weak undefined symbol first, in which case we need to remember the original
179 // symbol's type in order to check for TLS mismatches.
180 enum { UnknownType = 255 };
181
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000182 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
183 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
184 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
185 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
186 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
187 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000188
George Rimar2f0fab52016-03-06 06:26:18 +0000189protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000190 StringRefZ Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000191};
192
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000193// The base class for any defined symbols.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000194class Defined : public Symbol {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000195public:
Rui Ueyama7833afd2017-10-27 23:26:46 +0000196 Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type)
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000197 : Symbol(K, Name, IsLocal, StOther, Type) {}
Rui Ueyama7833afd2017-10-27 23:26:46 +0000198
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000199 static bool classof(const Symbol *S) { return S->isDefined(); }
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000200};
201
Rafael Espindolae7553e42016-08-31 13:28:33 +0000202class DefinedCommon : public Defined {
Rafael Espindola51d46902015-08-28 21:26:51 +0000203public:
Rui Ueyama7833afd2017-10-27 23:26:46 +0000204 DefinedCommon(StringRef Name, uint64_t Size, uint32_t Alignment,
205 uint8_t StOther, uint8_t Type)
Rui Ueyamada524d02017-10-27 23:29:58 +0000206 : Defined(DefinedCommonKind, Name, /*IsLocal=*/false, StOther, Type),
Rui Ueyama7833afd2017-10-27 23:26:46 +0000207 Alignment(Alignment), Size(Size) {}
Rafael Espindola51d46902015-08-28 21:26:51 +0000208
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000209 static bool classof(const Symbol *S) {
Rui Ueyamada524d02017-10-27 23:29:58 +0000210 return S->kind() == DefinedCommonKind;
Rafael Espindola51d46902015-08-28 21:26:51 +0000211 }
Rafael Espindolace8c9c02015-08-31 22:55:21 +0000212
Rafael Espindolaf31f9612015-09-01 01:19:12 +0000213 // The maximum alignment we have seen for this symbol.
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000214 uint32_t Alignment;
Rafael Espindola11191912015-12-24 16:23:37 +0000215
Rui Ueyama9c77d272017-08-10 15:54:27 +0000216 // The output offset of this common symbol in the output bss.
217 // Computed by the writer.
Rafael Espindola11191912015-12-24 16:23:37 +0000218 uint64_t Size;
Dmitry Mikulin1e30f072017-09-08 16:22:43 +0000219 BssSection *Section = nullptr;
Rafael Espindola51d46902015-08-28 21:26:51 +0000220};
221
Michael J. Spencer84487f12015-07-24 21:03:07 +0000222// Regular defined symbols read from object file symbol tables.
Rui Ueyama80474a22017-02-28 19:29:55 +0000223class DefinedRegular : public Defined {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000224public:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000225 DefinedRegular(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
Rafael Espindola6e93d052017-08-04 22:31:42 +0000226 uint64_t Value, uint64_t Size, SectionBase *Section)
Rui Ueyamada524d02017-10-27 23:29:58 +0000227 : Defined(DefinedRegularKind, Name, IsLocal, StOther, Type), Value(Value),
228 Size(Size), Section(Section) {}
Rafael Espindolaa8bf71c2016-10-26 20:34:59 +0000229
Simon Atanasyanf967f092016-09-29 12:58:36 +0000230 // Return true if the symbol is a PIC function.
Rui Ueyama80474a22017-02-28 19:29:55 +0000231 template <class ELFT> bool isMipsPIC() const;
Simon Atanasyanf967f092016-09-29 12:58:36 +0000232
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000233 static bool classof(const Symbol *S) {
Rui Ueyamada524d02017-10-27 23:29:58 +0000234 return S->kind() == DefinedRegularKind;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000235 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000236
Rui Ueyama80474a22017-02-28 19:29:55 +0000237 uint64_t Value;
238 uint64_t Size;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000239 SectionBase *Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000240};
241
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000242class Undefined : public Symbol {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000243public:
Rui Ueyama7833afd2017-10-27 23:26:46 +0000244 Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type)
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000245 : Symbol(UndefinedKind, Name, IsLocal, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000246
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000247 static bool classof(const Symbol *S) { return S->kind() == UndefinedKind; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000248};
249
Rui Ueyama32665f72017-11-06 04:13:24 +0000250class SharedSymbol : public Symbol {
Rafael Espindola18173d42015-09-08 15:50:05 +0000251public:
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000252 static bool classof(const Symbol *S) { return S->kind() == SharedKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +0000253
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000254 SharedSymbol(StringRef Name, uint8_t StOther, uint8_t Type, uint64_t Value,
Rui Ueyamabd730e32017-10-28 22:18:17 +0000255 uint64_t Size, uint32_t Alignment, const void *Verdef)
Rui Ueyama32665f72017-11-06 04:13:24 +0000256 : Symbol(SharedKind, Name, /*IsLocal=*/false, StOther, Type),
Rui Ueyamabd730e32017-10-28 22:18:17 +0000257 Verdef(Verdef), Value(Value), Size(Size), Alignment(Alignment) {
Rui Ueyamad32f06c2017-10-12 21:45:53 +0000258 // GNU ifunc is a mechanism to allow user-supplied functions to
259 // resolve PLT slot values at load-time. This is contrary to the
260 // regualr symbol resolution scheme in which symbols are resolved just
261 // by name. Using this hook, you can program how symbols are solved
262 // for you program. For example, you can make "memcpy" to be resolved
263 // to a SSE-enabled version of memcpy only when a machine running the
264 // program supports the SSE instruction set.
265 //
266 // Naturally, such symbols should always be called through their PLT
267 // slots. What GNU ifunc symbols point to are resolver functions, and
268 // calling them directly doesn't make sense (unless you are writing a
269 // loader).
270 //
271 // For DSO symbols, we always call them through PLT slots anyway.
272 // So there's no difference between GNU ifunc and regular function
Shoaib Meenai14cf5142017-10-12 21:52:14 +0000273 // symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC.
Rui Ueyamad32f06c2017-10-12 21:45:53 +0000274 if (this->Type == llvm::ELF::STT_GNU_IFUNC)
George Rimar4a9cfc82017-07-11 11:40:59 +0000275 this->Type = llvm::ELF::STT_FUNC;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000276 }
277
278 template <class ELFT> SharedFile<ELFT> *getFile() const {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000279 return cast<SharedFile<ELFT>>(Symbol::getFile());
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000280 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000281
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000282 template <class ELFT> uint32_t getAlignment() const;
George Rimarbc590fe2015-10-28 16:48:58 +0000283
George Rimard3566302016-06-20 11:55:12 +0000284 // This field is a pointer to the symbol's version definition.
Rui Ueyama4076fa12017-02-26 23:35:34 +0000285 const void *Verdef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000286
Rafael Espindolacb562312017-10-13 21:44:10 +0000287 // If not null, there is a copy relocation to this section.
288 InputSection *CopyRelSec = nullptr;
Rui Ueyama007c0022017-03-08 17:24:24 +0000289
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000290 uint64_t Value; // st_value
Rafael Espindola458173e2017-10-30 17:43:16 +0000291 uint64_t Size; // st_size
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000292 uint32_t Alignment;
Rafael Espindola18173d42015-09-08 15:50:05 +0000293};
294
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000295// This represents a symbol that is not yet in the link, but we know where to
296// find it if needed. If the resolver finds both Undefined and Lazy for the same
297// name, it will ask the Lazy to load a file.
298//
299// A special complication is the handling of weak undefined symbols. They should
300// not load a file, but we have to remember we have seen both the weak undefined
301// and the lazy. We represent that with a lazy symbol with a weak binding. This
302// means that code looking for undefined symbols normally also has to take lazy
303// symbols into consideration.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000304class Lazy : public Symbol {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000305public:
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000306 static bool classof(const Symbol *S) { return S->isLazy(); }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000307
308 // Returns an object file for this symbol, or a nullptr if the file
309 // was already returned.
Rui Ueyama55518e72016-10-28 20:57:25 +0000310 InputFile *fetch();
Rui Ueyamab06700f2016-07-17 17:44:41 +0000311
312protected:
Rui Ueyamada524d02017-10-27 23:29:58 +0000313 Lazy(Kind K, StringRef Name, uint8_t Type)
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000314 : Symbol(K, Name, /*IsLocal=*/false, llvm::ELF::STV_DEFAULT, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000315};
316
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000317// This class represents a symbol defined in an archive file. It is
318// created from an archive file header, and it knows how to load an
319// object file from an archive to replace itself with a defined
320// symbol.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000321class LazyArchive : public Lazy {
322public:
Rui Ueyama7833afd2017-10-27 23:26:46 +0000323 LazyArchive(const llvm::object::Archive::Symbol S, uint8_t Type)
324 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000325
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000326 static bool classof(const Symbol *S) { return S->kind() == LazyArchiveKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000327
Rafael Espindola6e93d052017-08-04 22:31:42 +0000328 ArchiveFile *getFile();
Rui Ueyama55518e72016-10-28 20:57:25 +0000329 InputFile *fetch();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000330
331private:
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000332 const llvm::object::Archive::Symbol Sym;
333};
334
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000335// LazyObject symbols represents symbols in object files between
336// --start-lib and --end-lib options.
337class LazyObject : public Lazy {
338public:
Rui Ueyama7833afd2017-10-27 23:26:46 +0000339 LazyObject(StringRef Name, uint8_t Type) : Lazy(LazyObjectKind, Name, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000340
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000341 static bool classof(const Symbol *S) { return S->kind() == LazyObjectKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000342
Rafael Espindola6e93d052017-08-04 22:31:42 +0000343 LazyObjFile *getFile();
Rui Ueyama55518e72016-10-28 20:57:25 +0000344 InputFile *fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000345};
346
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000347// Some linker-generated symbols need to be created as
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000348// DefinedRegular symbols.
Rui Ueyama80474a22017-02-28 19:29:55 +0000349struct ElfSym {
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000350 // __bss_start
George Rimare6c5d382017-04-05 10:03:25 +0000351 static DefinedRegular *Bss;
352
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000353 // etext and _etext
354 static DefinedRegular *Etext1;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000355 static DefinedRegular *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000356
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000357 // edata and _edata
358 static DefinedRegular *Edata1;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000359 static DefinedRegular *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000360
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000361 // end and _end
362 static DefinedRegular *End1;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000363 static DefinedRegular *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000364
Rui Ueyama92c37812017-06-26 15:11:24 +0000365 // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
366 // be at some offset from the base of the .got section, usually 0 or
367 // the end of the .got.
368 static DefinedRegular *GlobalOffsetTable;
369
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000370 // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
371 static DefinedRegular *MipsGp;
Rui Ueyama80474a22017-02-28 19:29:55 +0000372 static DefinedRegular *MipsGpDisp;
373 static DefinedRegular *MipsLocalGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000374};
375
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000376// A buffer class that is large enough to hold any Symbol-derived
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000377// object. We allocate memory using this class and instantiate a symbol
378// using the placement new.
379union SymbolUnion {
380 alignas(DefinedRegular) char A[sizeof(DefinedRegular)];
381 alignas(DefinedCommon) char B[sizeof(DefinedCommon)];
382 alignas(Undefined) char C[sizeof(Undefined)];
383 alignas(SharedSymbol) char D[sizeof(SharedSymbol)];
384 alignas(LazyArchive) char E[sizeof(LazyArchive)];
385 alignas(LazyObject) char F[sizeof(LazyObject)];
Peter Collingbourne4f952702016-05-01 04:55:03 +0000386};
387
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000388void printTraceSymbol(Symbol *Sym);
Rui Ueyama69c778c2016-07-17 17:50:09 +0000389
Peter Collingbourne4f952702016-05-01 04:55:03 +0000390template <typename T, typename... ArgT>
Rui Ueyamaf483da02017-11-03 22:48:47 +0000391void replaceSymbol(Symbol *S, InputFile *File, ArgT &&... Arg) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000392 static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
393 static_assert(alignof(T) <= alignof(SymbolUnion),
394 "SymbolUnion not aligned enough");
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000395 assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
396 "Not a Symbol");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000397
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000398 Symbol Sym = *S;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000399
400 new (S) T(std::forward<ArgT>(Arg)...);
Rafael Espindola6e93d052017-08-04 22:31:42 +0000401 S->File = File;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000402
403 S->Binding = Sym.Binding;
404 S->VersionId = Sym.VersionId;
405 S->Visibility = Sym.Visibility;
406 S->IsUsedInRegularObj = Sym.IsUsedInRegularObj;
407 S->ExportDynamic = Sym.ExportDynamic;
408 S->CanInline = Sym.CanInline;
409 S->Traced = Sym.Traced;
410 S->InVersionScript = Sym.InVersionScript;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000411
412 // Print out a log message if --trace-symbol was specified.
413 // This is for debugging.
414 if (S->Traced)
415 printTraceSymbol(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000416}
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000417} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000418
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000419std::string toString(const elf::Symbol &B);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000420} // namespace lld
421
422#endif