blob: ea284777b9b9f6a19c9f2761ada3b76ba6e3a8b8 [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 {
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +000041 DefinedKind,
Rui Ueyama32665f72017-11-06 04:13:24 +000042 SharedKind,
Peter Collingbourne60976ed2016-04-27 00:05:06 +000043 UndefinedKind,
Rui Ueyamaf8baa662016-04-07 19:24:51 +000044 LazyArchiveKind,
45 LazyObjectKind,
Michael J. Spencer84487f12015-07-24 21:03:07 +000046 };
47
Michael J. Spencercdae0a42015-07-28 22:58:25 +000048 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000049
Rui Ueyamaf1f00842017-10-31 16:07:41 +000050 // Symbol binding. This is not overwritten by replaceSymbol to track
51 // changes during resolution. In particular:
52 // - An undefined weak is still weak when it resolves to a shared library.
53 // - An undefined weak will not fetch archive members, but we have to
54 // remember it is weak.
55 uint8_t Binding;
56
57 // Version definition index.
58 uint16_t VersionId;
59
60 // Symbol visibility. This is the computed minimum visibility of all
61 // observed non-DSO symbols.
62 unsigned Visibility : 2;
63
64 // True if the symbol was used for linking and thus need to be added to the
65 // output file's symbol table. This is true for all symbols except for
66 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
67 // by other bitcode objects.
68 unsigned IsUsedInRegularObj : 1;
69
70 // If this flag is true and the symbol has protected or default visibility, it
71 // will appear in .dynsym. This flag is set by interposable DSO symbols in
72 // executables, by most symbols in DSOs and executables built with
73 // --export-dynamic, and by dynamic lists.
74 unsigned ExportDynamic : 1;
75
76 // False if LTO shouldn't inline whatever this symbol points to. If a symbol
77 // is overwritten after LTO, LTO shouldn't inline the symbol because it
78 // doesn't know the final contents of the symbol.
79 unsigned CanInline : 1;
80
81 // True if this symbol is specified by --trace-symbol option.
82 unsigned Traced : 1;
83
84 // This symbol version was found in a version script.
85 unsigned InVersionScript : 1;
86
87 // The file from which this symbol was created.
Rafael Espindoladfebd362017-11-29 22:47:35 +000088 InputFile *File;
Rui Ueyamaf1f00842017-10-31 16:07:41 +000089
90 bool includeInDynsym() const;
91 uint8_t computeBinding() const;
92 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
93
Peter Collingbourne60976ed2016-04-27 00:05:06 +000094 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +000095 bool isDefined() const { return SymbolKind == DefinedKind; }
Rui Ueyamac0f56482017-10-13 03:37:11 +000096 bool isShared() const { return SymbolKind == SharedKind; }
Rafael Espindolabec37652017-11-17 01:37:50 +000097 bool isLocal() const { return Binding == llvm::ELF::STB_LOCAL; }
Rui Ueyamac0f56482017-10-13 03:37:11 +000098
Rui Ueyamaf8baa662016-04-07 19:24:51 +000099 bool isLazy() const {
100 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
101 }
Rui Ueyamac0f56482017-10-13 03:37:11 +0000102
Rafael Espindola3d9f1c02017-09-13 20:43:04 +0000103 // True is this is an undefined weak symbol. This only works once
104 // all input files have been added.
Rafael Espindolab9a18fd2017-12-21 22:26:44 +0000105 bool isUndefWeak() const {
106 // See comment on Lazy the details.
107 return isWeak() && (isUndefined() || isLazy());
108 }
Rafael Espindola3d9f1c02017-09-13 20:43:04 +0000109
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000110 StringRef getName() const { return Name; }
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000111 void parseSymbolVersion();
Rafael Espindola78471f02015-09-01 23:12:52 +0000112
Rafael Espindola5c2310c2015-09-18 14:40:19 +0000113 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000114 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000115
George Rimarf64618a2017-03-17 11:56:54 +0000116 uint64_t getVA(int64_t Addend = 0) const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000117
George Rimar4afe42e2017-03-17 14:12:51 +0000118 uint64_t getGotOffset() const;
Rafael Espindolaf9e3c9c2017-05-11 23:28:49 +0000119 uint64_t getGotVA() const;
George Rimar4670bb02017-03-16 12:58:11 +0000120 uint64_t getGotPltOffset() const;
121 uint64_t getGotPltVA() const;
George Rimarf64618a2017-03-17 11:56:54 +0000122 uint64_t getPltVA() const;
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000123 uint64_t getSize() const;
George Rimar69268a82017-03-16 11:06:13 +0000124 OutputSection *getOutputSection() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000125
Rafael Espindola7ae21772016-10-26 00:58:23 +0000126 uint32_t DynsymIndex = 0;
Rafael Espindolaca656232016-10-21 20:32:41 +0000127 uint32_t GotIndex = -1;
128 uint32_t GotPltIndex = -1;
129 uint32_t PltIndex = -1;
130 uint32_t GlobalDynIndex = -1;
131
Michael J. Spencer84487f12015-07-24 21:03:07 +0000132protected:
Rafael Espindoladfebd362017-11-29 22:47:35 +0000133 Symbol(Kind K, InputFile *File, StringRefZ Name, uint8_t Binding,
134 uint8_t StOther, uint8_t Type)
135 : Binding(Binding), File(File), SymbolKind(K), NeedsPltAddr(false),
Rui Ueyama7833afd2017-10-27 23:26:46 +0000136 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
Rafael Espindola1d4b3022017-11-28 20:17:58 +0000137 IsInIgot(false), IsPreemptible(false), Used(!Config->GcSections),
138 Type(Type), StOther(StOther), Name(Name) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000139
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000140 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000141
Rafael Espindolaabebed92016-02-05 15:27:15 +0000142public:
Rui Ueyama924b3612017-02-16 06:12:22 +0000143 // True the symbol should point to its PLT entry.
144 // For SharedSymbol only.
145 unsigned NeedsPltAddr : 1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000146 // True if this symbol has an entry in the global part of MIPS GOT.
147 unsigned IsInGlobalMipsGot : 1;
148
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000149 // True if this symbol is referenced by 32-bit GOT relocations.
150 unsigned Is32BitMipsGot : 1;
151
Peter Smithbaffdb82016-12-08 12:58:55 +0000152 // True if this symbol is in the Iplt sub-section of the Plt.
153 unsigned IsInIplt : 1;
154
155 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
156 unsigned IsInIgot : 1;
157
Rafael Espindola35c908f2017-08-10 15:05:37 +0000158 unsigned IsPreemptible : 1;
159
Rafael Espindola1d4b3022017-11-28 20:17:58 +0000160 // True if an undefined or shared symbol is used from a live section.
161 unsigned Used : 1;
162
Rui Ueyamadd418072016-04-04 18:15:38 +0000163 // The following fields have the same meaning as the ELF symbol attributes.
164 uint8_t Type; // symbol type
Rui Ueyamab5792b22016-04-04 19:09:08 +0000165 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000166
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000167 // The Type field may also have this value. It means that we have not yet seen
168 // a non-Lazy symbol with this name, so we don't know what its type is. The
169 // Type field is normally set to this value for Lazy symbols unless we saw a
170 // weak undefined symbol first, in which case we need to remember the original
171 // symbol's type in order to check for TLS mismatches.
172 enum { UnknownType = 255 };
173
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000174 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
175 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
176 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
177 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
178 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
179 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000180
George Rimar2f0fab52016-03-06 06:26:18 +0000181protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000182 StringRefZ Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000183};
184
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000185// Represents a symbol that is defined in the current output file.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000186class Defined : public Symbol {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000187public:
Rafael Espindoladfebd362017-11-29 22:47:35 +0000188 Defined(InputFile *File, StringRefZ Name, uint8_t Binding, uint8_t StOther,
189 uint8_t Type, uint64_t Value, uint64_t Size, SectionBase *Section)
190 : Symbol(DefinedKind, File, Name, Binding, StOther, Type), Value(Value),
Rafael Espindolaf8e405d2017-11-24 19:06:14 +0000191 Size(Size), Section(Section) {}
Rafael Espindolaa8bf71c2016-10-26 20:34:59 +0000192
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000193 static bool classof(const Symbol *S) { return S->isDefined(); }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000194
Rui Ueyama80474a22017-02-28 19:29:55 +0000195 uint64_t Value;
196 uint64_t Size;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000197 SectionBase *Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000198};
199
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000200class Undefined : public Symbol {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000201public:
Rafael Espindoladfebd362017-11-29 22:47:35 +0000202 Undefined(InputFile *File, StringRefZ Name, uint8_t Binding, uint8_t StOther,
203 uint8_t Type)
204 : Symbol(UndefinedKind, File, Name, Binding, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000205
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000206 static bool classof(const Symbol *S) { return S->kind() == UndefinedKind; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000207};
208
Rui Ueyama32665f72017-11-06 04:13:24 +0000209class SharedSymbol : public Symbol {
Rafael Espindola18173d42015-09-08 15:50:05 +0000210public:
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000211 static bool classof(const Symbol *S) { return S->kind() == SharedKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +0000212
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000213 SharedSymbol(InputFile &File, StringRef Name, uint8_t Binding,
Rafael Espindoladfebd362017-11-29 22:47:35 +0000214 uint8_t StOther, uint8_t Type, uint64_t Value, uint64_t Size,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000215 uint32_t Alignment, uint32_t VerdefIndex)
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000216 : Symbol(SharedKind, &File, Name, Binding, StOther, Type), Value(Value),
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000217 Size(Size), VerdefIndex(VerdefIndex), Alignment(Alignment) {
Rui Ueyamad32f06c2017-10-12 21:45:53 +0000218 // GNU ifunc is a mechanism to allow user-supplied functions to
219 // resolve PLT slot values at load-time. This is contrary to the
George Rimard70da0e2017-12-23 16:34:58 +0000220 // regular symbol resolution scheme in which symbols are resolved just
Rui Ueyamad32f06c2017-10-12 21:45:53 +0000221 // by name. Using this hook, you can program how symbols are solved
222 // for you program. For example, you can make "memcpy" to be resolved
223 // to a SSE-enabled version of memcpy only when a machine running the
224 // program supports the SSE instruction set.
225 //
226 // Naturally, such symbols should always be called through their PLT
227 // slots. What GNU ifunc symbols point to are resolver functions, and
228 // calling them directly doesn't make sense (unless you are writing a
229 // loader).
230 //
231 // For DSO symbols, we always call them through PLT slots anyway.
232 // So there's no difference between GNU ifunc and regular function
Shoaib Meenai14cf5142017-10-12 21:52:14 +0000233 // symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC.
Rui Ueyamad32f06c2017-10-12 21:45:53 +0000234 if (this->Type == llvm::ELF::STT_GNU_IFUNC)
George Rimar4a9cfc82017-07-11 11:40:59 +0000235 this->Type = llvm::ELF::STT_FUNC;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000236 }
237
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000238 template <class ELFT> SharedFile<ELFT> &getFile() const {
239 return *cast<SharedFile<ELFT>>(File);
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000240 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000241
Rafael Espindolacb562312017-10-13 21:44:10 +0000242 // If not null, there is a copy relocation to this section.
243 InputSection *CopyRelSec = nullptr;
Rui Ueyama007c0022017-03-08 17:24:24 +0000244
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000245 uint64_t Value; // st_value
Rafael Espindola458173e2017-10-30 17:43:16 +0000246 uint64_t Size; // st_size
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000247
248 // This field is a index to the symbol's version definition.
249 uint32_t VerdefIndex;
250
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000251 uint32_t Alignment;
Rafael Espindola18173d42015-09-08 15:50:05 +0000252};
253
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000254// This represents a symbol that is not yet in the link, but we know where to
255// find it if needed. If the resolver finds both Undefined and Lazy for the same
256// name, it will ask the Lazy to load a file.
257//
258// A special complication is the handling of weak undefined symbols. They should
259// not load a file, but we have to remember we have seen both the weak undefined
260// and the lazy. We represent that with a lazy symbol with a weak binding. This
261// means that code looking for undefined symbols normally also has to take lazy
262// symbols into consideration.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000263class Lazy : public Symbol {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000264public:
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000265 static bool classof(const Symbol *S) { return S->isLazy(); }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000266
267 // Returns an object file for this symbol, or a nullptr if the file
268 // was already returned.
Rui Ueyama55518e72016-10-28 20:57:25 +0000269 InputFile *fetch();
Rui Ueyamab06700f2016-07-17 17:44:41 +0000270
271protected:
Rafael Espindolaefb483f2017-12-20 18:01:32 +0000272 Lazy(Kind K, InputFile &File, StringRef Name, uint8_t Type)
273 : Symbol(K, &File, Name, llvm::ELF::STB_GLOBAL, llvm::ELF::STV_DEFAULT,
Rafael Espindoladfebd362017-11-29 22:47:35 +0000274 Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000275};
276
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000277// This class represents a symbol defined in an archive file. It is
278// created from an archive file header, and it knows how to load an
279// object file from an archive to replace itself with a defined
280// symbol.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000281class LazyArchive : public Lazy {
282public:
Rafael Espindola8276f1b2017-12-20 17:59:43 +0000283 LazyArchive(InputFile &File, const llvm::object::Archive::Symbol S,
Rafael Espindoladfebd362017-11-29 22:47:35 +0000284 uint8_t Type)
Rafael Espindolaefb483f2017-12-20 18:01:32 +0000285 : Lazy(LazyArchiveKind, File, S.getName(), Type), Sym(S) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000286
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000287 static bool classof(const Symbol *S) { return S->kind() == LazyArchiveKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000288
Rafael Espindola8276f1b2017-12-20 17:59:43 +0000289 ArchiveFile &getFile();
Rui Ueyama55518e72016-10-28 20:57:25 +0000290 InputFile *fetch();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000291
292private:
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000293 const llvm::object::Archive::Symbol Sym;
294};
295
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000296// LazyObject symbols represents symbols in object files between
297// --start-lib and --end-lib options.
298class LazyObject : public Lazy {
299public:
Rafael Espindola2e5c71e2017-12-20 17:52:36 +0000300 LazyObject(InputFile &File, StringRef Name, uint8_t Type)
Rafael Espindolaefb483f2017-12-20 18:01:32 +0000301 : Lazy(LazyObjectKind, File, Name, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000302
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000303 static bool classof(const Symbol *S) { return S->kind() == LazyObjectKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000304
Rafael Espindola2e5c71e2017-12-20 17:52:36 +0000305 LazyObjFile &getFile();
Rui Ueyama55518e72016-10-28 20:57:25 +0000306 InputFile *fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000307};
308
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000309// Some linker-generated symbols need to be created as
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000310// Defined symbols.
Rui Ueyama80474a22017-02-28 19:29:55 +0000311struct ElfSym {
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000312 // __bss_start
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000313 static Defined *Bss;
George Rimare6c5d382017-04-05 10:03:25 +0000314
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000315 // etext and _etext
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000316 static Defined *Etext1;
317 static Defined *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000318
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000319 // edata and _edata
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000320 static Defined *Edata1;
321 static Defined *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000322
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000323 // end and _end
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000324 static Defined *End1;
325 static Defined *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000326
Rui Ueyama92c37812017-06-26 15:11:24 +0000327 // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
328 // be at some offset from the base of the .got section, usually 0 or
329 // the end of the .got.
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000330 static Defined *GlobalOffsetTable;
Rui Ueyama92c37812017-06-26 15:11:24 +0000331
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000332 // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000333 static Defined *MipsGp;
334 static Defined *MipsGpDisp;
335 static Defined *MipsLocalGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000336};
337
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000338// A buffer class that is large enough to hold any Symbol-derived
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000339// object. We allocate memory using this class and instantiate a symbol
340// using the placement new.
341union SymbolUnion {
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000342 alignas(Defined) char A[sizeof(Defined)];
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000343 alignas(Undefined) char C[sizeof(Undefined)];
344 alignas(SharedSymbol) char D[sizeof(SharedSymbol)];
345 alignas(LazyArchive) char E[sizeof(LazyArchive)];
346 alignas(LazyObject) char F[sizeof(LazyObject)];
Peter Collingbourne4f952702016-05-01 04:55:03 +0000347};
348
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000349void printTraceSymbol(Symbol *Sym);
Rui Ueyama69c778c2016-07-17 17:50:09 +0000350
Peter Collingbourne4f952702016-05-01 04:55:03 +0000351template <typename T, typename... ArgT>
Rafael Espindoladfebd362017-11-29 22:47:35 +0000352void replaceSymbol(Symbol *S, ArgT &&... Arg) {
Sam Clegg38f52b22018-02-13 17:32:31 +0000353 static_assert(std::is_trivially_destructible<T>(),
354 "Symbol types must be trivially destructible");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000355 static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
356 static_assert(alignof(T) <= alignof(SymbolUnion),
357 "SymbolUnion not aligned enough");
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000358 assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
359 "Not a Symbol");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000360
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000361 Symbol Sym = *S;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000362
363 new (S) T(std::forward<ArgT>(Arg)...);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000364
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000365 S->VersionId = Sym.VersionId;
366 S->Visibility = Sym.Visibility;
367 S->IsUsedInRegularObj = Sym.IsUsedInRegularObj;
368 S->ExportDynamic = Sym.ExportDynamic;
369 S->CanInline = Sym.CanInline;
370 S->Traced = Sym.Traced;
371 S->InVersionScript = Sym.InVersionScript;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000372
373 // Print out a log message if --trace-symbol was specified.
374 // This is for debugging.
375 if (S->Traced)
376 printTraceSymbol(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000377}
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000378} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000379
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000380std::string toString(const elf::Symbol &B);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000381} // namespace lld
382
383#endif