blob: 0158f4f60fa1efdf1e7ac00694fa3ad62edb7e6b [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.
105 bool isUndefWeak() const;
106
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000107 StringRef getName() const { return Name; }
Rui Ueyamab5792b22016-04-04 19:09:08 +0000108 uint8_t getVisibility() const { return StOther & 0x3; }
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000109 void parseSymbolVersion();
Rafael Espindola78471f02015-09-01 23:12:52 +0000110
Rafael Espindola5c2310c2015-09-18 14:40:19 +0000111 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000112 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000113
George Rimarf64618a2017-03-17 11:56:54 +0000114 uint64_t getVA(int64_t Addend = 0) const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000115
George Rimar4afe42e2017-03-17 14:12:51 +0000116 uint64_t getGotOffset() const;
Rafael Espindolaf9e3c9c2017-05-11 23:28:49 +0000117 uint64_t getGotVA() const;
George Rimar4670bb02017-03-16 12:58:11 +0000118 uint64_t getGotPltOffset() const;
119 uint64_t getGotPltVA() const;
George Rimarf64618a2017-03-17 11:56:54 +0000120 uint64_t getPltVA() const;
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000121 uint64_t getSize() const;
George Rimar69268a82017-03-16 11:06:13 +0000122 OutputSection *getOutputSection() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000123
Rafael Espindola7ae21772016-10-26 00:58:23 +0000124 uint32_t DynsymIndex = 0;
Rafael Espindolaca656232016-10-21 20:32:41 +0000125 uint32_t GotIndex = -1;
126 uint32_t GotPltIndex = -1;
127 uint32_t PltIndex = -1;
128 uint32_t GlobalDynIndex = -1;
129
Michael J. Spencer84487f12015-07-24 21:03:07 +0000130protected:
Rafael Espindoladfebd362017-11-29 22:47:35 +0000131 Symbol(Kind K, InputFile *File, StringRefZ Name, uint8_t Binding,
132 uint8_t StOther, uint8_t Type)
133 : Binding(Binding), File(File), SymbolKind(K), NeedsPltAddr(false),
Rui Ueyama7833afd2017-10-27 23:26:46 +0000134 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
Rafael Espindola1d4b3022017-11-28 20:17:58 +0000135 IsInIgot(false), IsPreemptible(false), Used(!Config->GcSections),
136 Type(Type), StOther(StOther), Name(Name) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000137
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000138 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000139
Rafael Espindolaabebed92016-02-05 15:27:15 +0000140public:
Rui Ueyama924b3612017-02-16 06:12:22 +0000141 // True the symbol should point to its PLT entry.
142 // For SharedSymbol only.
143 unsigned NeedsPltAddr : 1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000144 // True if this symbol has an entry in the global part of MIPS GOT.
145 unsigned IsInGlobalMipsGot : 1;
146
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000147 // True if this symbol is referenced by 32-bit GOT relocations.
148 unsigned Is32BitMipsGot : 1;
149
Peter Smithbaffdb82016-12-08 12:58:55 +0000150 // True if this symbol is in the Iplt sub-section of the Plt.
151 unsigned IsInIplt : 1;
152
153 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
154 unsigned IsInIgot : 1;
155
Rafael Espindola35c908f2017-08-10 15:05:37 +0000156 unsigned IsPreemptible : 1;
157
Rafael Espindola1d4b3022017-11-28 20:17:58 +0000158 // True if an undefined or shared symbol is used from a live section.
159 unsigned Used : 1;
160
Rui Ueyamadd418072016-04-04 18:15:38 +0000161 // The following fields have the same meaning as the ELF symbol attributes.
162 uint8_t Type; // symbol type
Rui Ueyamab5792b22016-04-04 19:09:08 +0000163 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000164
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000165 // The Type field may also have this value. It means that we have not yet seen
166 // a non-Lazy symbol with this name, so we don't know what its type is. The
167 // Type field is normally set to this value for Lazy symbols unless we saw a
168 // weak undefined symbol first, in which case we need to remember the original
169 // symbol's type in order to check for TLS mismatches.
170 enum { UnknownType = 255 };
171
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000172 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
173 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
174 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
175 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
176 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
177 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000178
George Rimar2f0fab52016-03-06 06:26:18 +0000179protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000180 StringRefZ Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000181};
182
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000183// Represents a symbol that is defined in the current output file.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000184class Defined : public Symbol {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000185public:
Rafael Espindoladfebd362017-11-29 22:47:35 +0000186 Defined(InputFile *File, StringRefZ Name, uint8_t Binding, uint8_t StOther,
187 uint8_t Type, uint64_t Value, uint64_t Size, SectionBase *Section)
188 : Symbol(DefinedKind, File, Name, Binding, StOther, Type), Value(Value),
Rafael Espindolaf8e405d2017-11-24 19:06:14 +0000189 Size(Size), Section(Section) {}
Rafael Espindolaa8bf71c2016-10-26 20:34:59 +0000190
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000191 static bool classof(const Symbol *S) { return S->isDefined(); }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000192
Rui Ueyama80474a22017-02-28 19:29:55 +0000193 uint64_t Value;
194 uint64_t Size;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000195 SectionBase *Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000196};
197
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000198class Undefined : public Symbol {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000199public:
Rafael Espindoladfebd362017-11-29 22:47:35 +0000200 Undefined(InputFile *File, StringRefZ Name, uint8_t Binding, uint8_t StOther,
201 uint8_t Type)
202 : Symbol(UndefinedKind, File, 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 Espindoladfebd362017-11-29 22:47:35 +0000211 SharedSymbol(InputFile *File, StringRef Name, uint8_t Binding,
212 uint8_t StOther, uint8_t Type, uint64_t Value, uint64_t Size,
213 uint32_t Alignment, const void *Verdef)
214 : Symbol(SharedKind, File, Name, Binding, StOther, Type), Verdef(Verdef),
Rafael Espindola9e3381e2017-11-28 01:04:51 +0000215 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 {
Rafael Espindoladfebd362017-11-29 22:47:35 +0000237 return cast<SharedFile<ELFT>>(File);
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:
Rafael Espindoladfebd362017-11-29 22:47:35 +0000269 Lazy(Kind K, InputFile *File, StringRef Name, uint8_t Type)
270 : Symbol(K, File, Name, llvm::ELF::STB_GLOBAL, llvm::ELF::STV_DEFAULT,
271 Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000272};
273
Rafael Espindola9c8f8532017-10-24 16:27:31 +0000274// This class represents a symbol defined in an archive file. It is
275// created from an archive file header, and it knows how to load an
276// object file from an archive to replace itself with a defined
277// symbol.
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000278class LazyArchive : public Lazy {
279public:
Rafael Espindoladfebd362017-11-29 22:47:35 +0000280 LazyArchive(InputFile *File, const llvm::object::Archive::Symbol S,
281 uint8_t Type)
282 : Lazy(LazyArchiveKind, File, S.getName(), Type), Sym(S) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000283
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000284 static bool classof(const Symbol *S) { return S->kind() == LazyArchiveKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000285
Rafael Espindola6e93d052017-08-04 22:31:42 +0000286 ArchiveFile *getFile();
Rui Ueyama55518e72016-10-28 20:57:25 +0000287 InputFile *fetch();
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000288
289private:
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000290 const llvm::object::Archive::Symbol Sym;
291};
292
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000293// LazyObject symbols represents symbols in object files between
294// --start-lib and --end-lib options.
295class LazyObject : public Lazy {
296public:
Rafael Espindoladfebd362017-11-29 22:47:35 +0000297 LazyObject(InputFile *File, StringRef Name, uint8_t Type)
298 : Lazy(LazyObjectKind, File, Name, Type) {}
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000299
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000300 static bool classof(const Symbol *S) { return S->kind() == LazyObjectKind; }
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000301
Rafael Espindola6e93d052017-08-04 22:31:42 +0000302 LazyObjFile *getFile();
Rui Ueyama55518e72016-10-28 20:57:25 +0000303 InputFile *fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000304};
305
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000306// Some linker-generated symbols need to be created as
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000307// Defined symbols.
Rui Ueyama80474a22017-02-28 19:29:55 +0000308struct ElfSym {
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000309 // __bss_start
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000310 static Defined *Bss;
George Rimare6c5d382017-04-05 10:03:25 +0000311
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000312 // etext and _etext
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000313 static Defined *Etext1;
314 static Defined *Etext2;
George Rimar9e859392016-02-26 14:36:36 +0000315
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000316 // edata and _edata
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000317 static Defined *Edata1;
318 static Defined *Edata2;
George Rimar9e859392016-02-26 14:36:36 +0000319
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000320 // end and _end
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000321 static Defined *End1;
322 static Defined *End2;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000323
Rui Ueyama92c37812017-06-26 15:11:24 +0000324 // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
325 // be at some offset from the base of the .got section, usually 0 or
326 // the end of the .got.
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000327 static Defined *GlobalOffsetTable;
Rui Ueyama92c37812017-06-26 15:11:24 +0000328
Rui Ueyama3e1fc3f2017-04-13 21:37:56 +0000329 // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000330 static Defined *MipsGp;
331 static Defined *MipsGpDisp;
332 static Defined *MipsLocalGp;
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000333};
334
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000335// A buffer class that is large enough to hold any Symbol-derived
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000336// object. We allocate memory using this class and instantiate a symbol
337// using the placement new.
338union SymbolUnion {
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000339 alignas(Defined) char A[sizeof(Defined)];
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000340 alignas(Undefined) char C[sizeof(Undefined)];
341 alignas(SharedSymbol) char D[sizeof(SharedSymbol)];
342 alignas(LazyArchive) char E[sizeof(LazyArchive)];
343 alignas(LazyObject) char F[sizeof(LazyObject)];
Peter Collingbourne4f952702016-05-01 04:55:03 +0000344};
345
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000346void printTraceSymbol(Symbol *Sym);
Rui Ueyama69c778c2016-07-17 17:50:09 +0000347
Peter Collingbourne4f952702016-05-01 04:55:03 +0000348template <typename T, typename... ArgT>
Rafael Espindoladfebd362017-11-29 22:47:35 +0000349void replaceSymbol(Symbol *S, ArgT &&... Arg) {
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000350 static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
351 static_assert(alignof(T) <= alignof(SymbolUnion),
352 "SymbolUnion not aligned enough");
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000353 assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
354 "Not a Symbol");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000355
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000356 Symbol Sym = *S;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000357
358 new (S) T(std::forward<ArgT>(Arg)...);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000359
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000360 S->VersionId = Sym.VersionId;
361 S->Visibility = Sym.Visibility;
362 S->IsUsedInRegularObj = Sym.IsUsedInRegularObj;
363 S->ExportDynamic = Sym.ExportDynamic;
364 S->CanInline = Sym.CanInline;
365 S->Traced = Sym.Traced;
366 S->InVersionScript = Sym.InVersionScript;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000367
368 // Print out a log message if --trace-symbol was specified.
369 // This is for debugging.
370 if (S->Traced)
371 printTraceSymbol(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000372}
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000373} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000374
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000375std::string toString(const elf::Symbol &B);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000376} // namespace lld
377
378#endif