blob: 7a3ed4ffcaab4f3fffcffe31e9920c3c3d968ade [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.
88 InputFile *File = nullptr;
89
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.
105 bool isUndefWeak() const;
106
Rafael Espindola6e93d052017-08-04 22:31:42 +0000107 InputFile *getFile() const;
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000108 StringRef getName() const { return Name; }
Rui Ueyamab5792b22016-04-04 19:09:08 +0000109 uint8_t getVisibility() const { return StOther & 0x3; }
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000110 void parseSymbolVersion();
Rafael Espindola78471f02015-09-01 23:12:52 +0000111
Rafael Espindola5c2310c2015-09-18 14:40:19 +0000112 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000113 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000114
George Rimarf64618a2017-03-17 11:56:54 +0000115 uint64_t getVA(int64_t Addend = 0) const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000116
George Rimar4afe42e2017-03-17 14:12:51 +0000117 uint64_t getGotOffset() const;
Rafael Espindolaf9e3c9c2017-05-11 23:28:49 +0000118 uint64_t getGotVA() const;
George Rimar4670bb02017-03-16 12:58:11 +0000119 uint64_t getGotPltOffset() const;
120 uint64_t getGotPltVA() const;
George Rimarf64618a2017-03-17 11:56:54 +0000121 uint64_t getPltVA() const;
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000122 uint64_t getSize() const;
George Rimar69268a82017-03-16 11:06:13 +0000123 OutputSection *getOutputSection() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000124
Rafael Espindola7ae21772016-10-26 00:58:23 +0000125 uint32_t DynsymIndex = 0;
Rafael Espindolaca656232016-10-21 20:32:41 +0000126 uint32_t GotIndex = -1;
127 uint32_t GotPltIndex = -1;
128 uint32_t PltIndex = -1;
129 uint32_t GlobalDynIndex = -1;
130
Michael J. Spencer84487f12015-07-24 21:03:07 +0000131protected:
Rafael Espindolabec37652017-11-17 01:37:50 +0000132 Symbol(Kind K, StringRefZ Name, uint8_t Binding, uint8_t StOther,
133 uint8_t Type)
134 : Binding(Binding), SymbolKind(K), NeedsPltAddr(false),
Rui Ueyama7833afd2017-10-27 23:26:46 +0000135 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
Rafael Espindola1d4b3022017-11-28 20:17:58 +0000136 IsInIgot(false), IsPreemptible(false), Used(!Config->GcSections),
137 Type(Type), StOther(StOther), Name(Name) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000138
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000139 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000140
Rafael Espindolaabebed92016-02-05 15:27:15 +0000141public:
Rui Ueyama924b3612017-02-16 06:12:22 +0000142 // True the symbol should point to its PLT entry.
143 // For SharedSymbol only.
144 unsigned NeedsPltAddr : 1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000145 // True if this symbol has an entry in the global part of MIPS GOT.
146 unsigned IsInGlobalMipsGot : 1;
147
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000148 // True if this symbol is referenced by 32-bit GOT relocations.
149 unsigned Is32BitMipsGot : 1;
150
Peter Smithbaffdb82016-12-08 12:58:55 +0000151 // True if this symbol is in the Iplt sub-section of the Plt.
152 unsigned IsInIplt : 1;
153
154 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
155 unsigned IsInIgot : 1;
156
Rafael Espindola35c908f2017-08-10 15:05:37 +0000157 unsigned IsPreemptible : 1;
158
Rafael Espindola1d4b3022017-11-28 20:17:58 +0000159 // True if an undefined or shared symbol is used from a live section.
160 unsigned Used : 1;
161
Rui Ueyamadd418072016-04-04 18:15:38 +0000162 // The following fields have the same meaning as the ELF symbol attributes.
163 uint8_t Type; // symbol type
Rui Ueyamab5792b22016-04-04 19:09:08 +0000164 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000165
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000166 // The Type field may also have this value. It means that we have not yet seen
167 // a non-Lazy symbol with this name, so we don't know what its type is. The
168 // Type field is normally set to this value for Lazy symbols unless we saw a
169 // weak undefined symbol first, in which case we need to remember the original
170 // symbol's type in order to check for TLS mismatches.
171 enum { UnknownType = 255 };
172
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000173 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
174 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
175 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
176 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
177 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
178 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000179
George Rimar2f0fab52016-03-06 06:26:18 +0000180protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000181 StringRefZ Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000182};
183
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000184// Represents a symbol that is defined in the current output file.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000185class Defined : public Symbol {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000186public:
Rafael Espindolabec37652017-11-17 01:37:50 +0000187 Defined(StringRefZ Name, uint8_t Binding, uint8_t StOther, uint8_t Type,
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000188 uint64_t Value, uint64_t Size, SectionBase *Section)
Rafael Espindolabec37652017-11-17 01:37:50 +0000189 : Symbol(DefinedKind, Name, Binding, StOther, Type), Value(Value),
Rafael Espindolaf8e405d2017-11-24 19:06:14 +0000190 Size(Size), Section(Section) {}
Rafael Espindolaa8bf71c2016-10-26 20:34:59 +0000191
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000192 static bool classof(const Symbol *S) { return S->isDefined(); }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000193
Rui Ueyama80474a22017-02-28 19:29:55 +0000194 uint64_t Value;
195 uint64_t Size;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000196 SectionBase *Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000197};
198
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000199class Undefined : public Symbol {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000200public:
Rafael Espindolabec37652017-11-17 01:37:50 +0000201 Undefined(StringRefZ Name, uint8_t Binding, uint8_t StOther, uint8_t Type)
Rafael Espindolaf8e405d2017-11-24 19:06:14 +0000202 : Symbol(UndefinedKind, Name, Binding, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000203
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000204 static bool classof(const Symbol *S) { return S->kind() == UndefinedKind; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000205};
206
Rui Ueyama32665f72017-11-06 04:13:24 +0000207class SharedSymbol : public Symbol {
Rafael Espindola18173d42015-09-08 15:50:05 +0000208public:
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000209 static bool classof(const Symbol *S) { return S->kind() == SharedKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +0000210
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000211 SharedSymbol(StringRef Name, uint8_t Binding, uint8_t StOther, uint8_t Type,
212 uint64_t Value, uint64_t Size, uint32_t Alignment,
213 const void *Verdef)
214 : Symbol(SharedKind, Name, Binding, StOther, Type), Verdef(Verdef),
215 Value(Value), Size(Size), Alignment(Alignment) {
Rui Ueyamad32f06c2017-10-12 21:45:53 +0000216 // GNU ifunc is a mechanism to allow user-supplied functions to
217 // resolve PLT slot values at load-time. This is contrary to the
218 // regualr symbol resolution scheme in which symbols are resolved just
219 // by name. Using this hook, you can program how symbols are solved
220 // for you program. For example, you can make "memcpy" to be resolved
221 // to a SSE-enabled version of memcpy only when a machine running the
222 // program supports the SSE instruction set.
223 //
224 // Naturally, such symbols should always be called through their PLT
225 // slots. What GNU ifunc symbols point to are resolver functions, and
226 // calling them directly doesn't make sense (unless you are writing a
227 // loader).
228 //
229 // For DSO symbols, we always call them through PLT slots anyway.
230 // So there's no difference between GNU ifunc and regular function
Shoaib Meenai14cf5142017-10-12 21:52:14 +0000231 // symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC.
Rui Ueyamad32f06c2017-10-12 21:45:53 +0000232 if (this->Type == llvm::ELF::STT_GNU_IFUNC)
George Rimar4a9cfc82017-07-11 11:40:59 +0000233 this->Type = llvm::ELF::STT_FUNC;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000234 }
235
236 template <class ELFT> SharedFile<ELFT> *getFile() const {
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000237 return cast<SharedFile<ELFT>>(Symbol::getFile());
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000238 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000239
George Rimard3566302016-06-20 11:55:12 +0000240 // This field is a pointer to the symbol's version definition.
Rui Ueyama4076fa12017-02-26 23:35:34 +0000241 const void *Verdef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000242
Rafael Espindolacb562312017-10-13 21:44:10 +0000243 // If not null, there is a copy relocation to this section.
244 InputSection *CopyRelSec = nullptr;
Rui Ueyama007c0022017-03-08 17:24:24 +0000245
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000246 uint64_t Value; // st_value
Rafael Espindola458173e2017-10-30 17:43:16 +0000247 uint64_t Size; // st_size
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000248 uint32_t Alignment;
Rafael Espindola18173d42015-09-08 15:50:05 +0000249};
250
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000251// This represents a symbol that is not yet in the link, but we know where to
252// find it if needed. If the resolver finds both Undefined and Lazy for the same
253// name, it will ask the Lazy to load a file.
254//
255// A special complication is the handling of weak undefined symbols. They should
256// not load a file, but we have to remember we have seen both the weak undefined
257// and the lazy. We represent that with a lazy symbol with a weak binding. This
258// means that code looking for undefined symbols normally also has to take lazy
259// symbols into consideration.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000260class Lazy : public Symbol {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000261public:
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000262 static bool classof(const Symbol *S) { return S->isLazy(); }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000263
264 // Returns an object file for this symbol, or a nullptr if the file
265 // was already returned.
Rui Ueyama55518e72016-10-28 20:57:25 +0000266 InputFile *fetch();
Rui Ueyamab06700f2016-07-17 17:44:41 +0000267
268protected:
Rui Ueyamada524d02017-10-27 23:29:58 +0000269 Lazy(Kind K, StringRef Name, uint8_t Type)
Rafael Espindolabec37652017-11-17 01:37:50 +0000270 : Symbol(K, Name, llvm::ELF::STB_GLOBAL, llvm::ELF::STV_DEFAULT, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000271};
272
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000273// This class represents a symbol defined in an archive file. It is
274// created from an archive file header, and it knows how to load an
275// object file from an archive to replace itself with a defined
276// symbol.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000277class LazyArchive : public Lazy {
278public:
Rui Ueyama7833afd2017-10-27 23:26:46 +0000279 LazyArchive(const llvm::object::Archive::Symbol S, uint8_t Type)
280 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000281
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000282 static bool classof(const Symbol *S) { return S->kind() == LazyArchiveKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000283
Rafael Espindola6e93d052017-08-04 22:31:42 +0000284 ArchiveFile *getFile();
Rui Ueyama55518e72016-10-28 20:57:25 +0000285 InputFile *fetch();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000286
287private:
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000288 const llvm::object::Archive::Symbol Sym;
289};
290
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000291// LazyObject symbols represents symbols in object files between
292// --start-lib and --end-lib options.
293class LazyObject : public Lazy {
294public:
Rui Ueyama7833afd2017-10-27 23:26:46 +0000295 LazyObject(StringRef Name, uint8_t Type) : Lazy(LazyObjectKind, Name, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000296
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000297 static bool classof(const Symbol *S) { return S->kind() == LazyObjectKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000298
Rafael Espindola6e93d052017-08-04 22:31:42 +0000299 LazyObjFile *getFile();
Rui Ueyama55518e72016-10-28 20:57:25 +0000300 InputFile *fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000301};
302
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000303// Some linker-generated symbols need to be created as
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000304// Defined symbols.
Rui Ueyama80474a22017-02-28 19:29:55 +0000305struct ElfSym {
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000306 // __bss_start
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000307 static Defined *Bss;
George Rimare6c5d382017-04-05 10:03:25 +0000308
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000309 // etext and _etext
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000310 static Defined *Etext1;
311 static Defined *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000312
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000313 // edata and _edata
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000314 static Defined *Edata1;
315 static Defined *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000316
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000317 // end and _end
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000318 static Defined *End1;
319 static Defined *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000320
Rui Ueyama92c37812017-06-26 15:11:24 +0000321 // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
322 // be at some offset from the base of the .got section, usually 0 or
323 // the end of the .got.
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000324 static Defined *GlobalOffsetTable;
Rui Ueyama92c37812017-06-26 15:11:24 +0000325
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000326 // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000327 static Defined *MipsGp;
328 static Defined *MipsGpDisp;
329 static Defined *MipsLocalGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000330};
331
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000332// A buffer class that is large enough to hold any Symbol-derived
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000333// object. We allocate memory using this class and instantiate a symbol
334// using the placement new.
335union SymbolUnion {
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000336 alignas(Defined) char A[sizeof(Defined)];
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000337 alignas(Undefined) char C[sizeof(Undefined)];
338 alignas(SharedSymbol) char D[sizeof(SharedSymbol)];
339 alignas(LazyArchive) char E[sizeof(LazyArchive)];
340 alignas(LazyObject) char F[sizeof(LazyObject)];
Peter Collingbourne4f952702016-05-01 04:55:03 +0000341};
342
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000343void printTraceSymbol(Symbol *Sym);
Rui Ueyama69c778c2016-07-17 17:50:09 +0000344
Peter Collingbourne4f952702016-05-01 04:55:03 +0000345template <typename T, typename... ArgT>
Rui Ueyamaf483da02017-11-03 22:48:47 +0000346void replaceSymbol(Symbol *S, InputFile *File, ArgT &&... Arg) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000347 static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
348 static_assert(alignof(T) <= alignof(SymbolUnion),
349 "SymbolUnion not aligned enough");
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000350 assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
351 "Not a Symbol");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000352
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000353 Symbol Sym = *S;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000354
355 new (S) T(std::forward<ArgT>(Arg)...);
Rafael Espindola6e93d052017-08-04 22:31:42 +0000356 S->File = File;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000357
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000358 S->VersionId = Sym.VersionId;
359 S->Visibility = Sym.Visibility;
360 S->IsUsedInRegularObj = Sym.IsUsedInRegularObj;
361 S->ExportDynamic = Sym.ExportDynamic;
362 S->CanInline = Sym.CanInline;
363 S->Traced = Sym.Traced;
364 S->InVersionScript = Sym.InVersionScript;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000365
366 // Print out a log message if --trace-symbol was specified.
367 // This is for debugging.
368 if (S->Traced)
369 printTraceSymbol(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000370}
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000371} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000372
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000373std::string toString(const elf::Symbol &B);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000374} // namespace lld
375
376#endif