blob: bef5897b131f0c91f3532e318f46b8e63b0e019b [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 Ueyama3f851702017-10-02 21:00:41 +000019#include "lld/Common/LLVM.h"
Rui Ueyamaee173712018-02-28 17:38:19 +000020#include "lld/Common/Strings.h"
Michael J. Spencer1b348a62015-09-04 22:28:10 +000021#include "llvm/Object/Archive.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000022#include "llvm/Object/ELF.h"
23
24namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000025namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000026
Michael J. Spencer1b348a62015-09-04 22:28:10 +000027class ArchiveFile;
Peter Collingbourne4f952702016-05-01 04:55:03 +000028class BitcodeFile;
Dmitry Mikulin1e30f072017-09-08 16:22:43 +000029class BssSection;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030class InputFile;
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000031class LazyObjFile;
32template <class ELFT> class ObjFile;
Rafael Espindola24e6f362017-02-24 15:07:30 +000033class OutputSection;
Rui Ueyama35da9b62015-10-11 20:59:12 +000034template <class ELFT> class SharedFile;
Michael J. Spencer84487f12015-07-24 21:03:07 +000035
Michael J. Spencer84487f12015-07-24 21:03:07 +000036// The base class for real symbol classes.
Rui Ueyamaf52496e2017-11-03 21:21:47 +000037class Symbol {
Michael J. Spencer84487f12015-07-24 21:03:07 +000038public:
39 enum Kind {
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +000040 DefinedKind,
Rui Ueyama32665f72017-11-06 04:13:24 +000041 SharedKind,
Peter Collingbourne60976ed2016-04-27 00:05:06 +000042 UndefinedKind,
Rui Ueyamaf8baa662016-04-07 19:24:51 +000043 LazyArchiveKind,
44 LazyObjectKind,
Michael J. Spencer84487f12015-07-24 21:03:07 +000045 };
46
Michael J. Spencercdae0a42015-07-28 22:58:25 +000047 Kind kind() const { return static_cast<Kind>(SymbolKind); }
Michael J. Spencer84487f12015-07-24 21:03:07 +000048
Rui Ueyamaf1f00842017-10-31 16:07:41 +000049 // Symbol binding. This is not overwritten by replaceSymbol to track
50 // changes during resolution. In particular:
51 // - An undefined weak is still weak when it resolves to a shared library.
52 // - An undefined weak will not fetch archive members, but we have to
53 // remember it is weak.
54 uint8_t Binding;
55
56 // Version definition index.
57 uint16_t VersionId;
58
59 // Symbol visibility. This is the computed minimum visibility of all
60 // observed non-DSO symbols.
61 unsigned Visibility : 2;
62
63 // True if the symbol was used for linking and thus need to be added to the
64 // output file's symbol table. This is true for all symbols except for
65 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
66 // by other bitcode objects.
67 unsigned IsUsedInRegularObj : 1;
68
69 // If this flag is true and the symbol has protected or default visibility, it
70 // will appear in .dynsym. This flag is set by interposable DSO symbols in
71 // executables, by most symbols in DSOs and executables built with
72 // --export-dynamic, and by dynamic lists.
73 unsigned ExportDynamic : 1;
74
75 // False if LTO shouldn't inline whatever this symbol points to. If a symbol
76 // is overwritten after LTO, LTO shouldn't inline the symbol because it
77 // doesn't know the final contents of the symbol.
78 unsigned CanInline : 1;
79
80 // True if this symbol is specified by --trace-symbol option.
81 unsigned Traced : 1;
82
Rui Ueyamaf1f00842017-10-31 16:07:41 +000083 // The file from which this symbol was created.
Rafael Espindoladfebd362017-11-29 22:47:35 +000084 InputFile *File;
Rui Ueyamaf1f00842017-10-31 16:07:41 +000085
86 bool includeInDynsym() const;
87 uint8_t computeBinding() const;
88 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
89
Peter Collingbourne60976ed2016-04-27 00:05:06 +000090 bool isUndefined() const { return SymbolKind == UndefinedKind; }
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +000091 bool isDefined() const { return SymbolKind == DefinedKind; }
Rui Ueyamac0f56482017-10-13 03:37:11 +000092 bool isShared() const { return SymbolKind == SharedKind; }
Rafael Espindolabec37652017-11-17 01:37:50 +000093 bool isLocal() const { return Binding == llvm::ELF::STB_LOCAL; }
Rui Ueyamac0f56482017-10-13 03:37:11 +000094
Rui Ueyamaf8baa662016-04-07 19:24:51 +000095 bool isLazy() const {
96 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
97 }
Rui Ueyamac0f56482017-10-13 03:37:11 +000098
Fangrui Song9cd5df02018-02-23 02:05:48 +000099 // True if this is an undefined weak symbol. This only works once
Rafael Espindola3d9f1c02017-09-13 20:43:04 +0000100 // all input files have been added.
Rafael Espindolab9a18fd2017-12-21 22:26:44 +0000101 bool isUndefWeak() const {
Fangrui Song9cd5df02018-02-23 02:05:48 +0000102 // See comment on Lazy for details.
Rafael Espindolab9a18fd2017-12-21 22:26:44 +0000103 return isWeak() && (isUndefined() || isLazy());
104 }
Rafael Espindola3d9f1c02017-09-13 20:43:04 +0000105
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000106 StringRef getName() const { return Name; }
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000107 void parseSymbolVersion();
Rafael Espindola78471f02015-09-01 23:12:52 +0000108
Rafael Espindola5c2310c2015-09-18 14:40:19 +0000109 bool isInGot() const { return GotIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000110 bool isInPlt() const { return PltIndex != -1U; }
Rafael Espindolaeb792732015-09-21 15:11:29 +0000111
George Rimarf64618a2017-03-17 11:56:54 +0000112 uint64_t getVA(int64_t Addend = 0) const;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000113
George Rimar4afe42e2017-03-17 14:12:51 +0000114 uint64_t getGotOffset() const;
Rafael Espindolaf9e3c9c2017-05-11 23:28:49 +0000115 uint64_t getGotVA() const;
George Rimar4670bb02017-03-16 12:58:11 +0000116 uint64_t getGotPltOffset() const;
117 uint64_t getGotPltVA() const;
George Rimarf64618a2017-03-17 11:56:54 +0000118 uint64_t getPltVA() const;
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000119 uint64_t getSize() const;
George Rimar69268a82017-03-16 11:06:13 +0000120 OutputSection *getOutputSection() const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000121
Rafael Espindola7ae21772016-10-26 00:58:23 +0000122 uint32_t DynsymIndex = 0;
Rafael Espindolaca656232016-10-21 20:32:41 +0000123 uint32_t GotIndex = -1;
124 uint32_t GotPltIndex = -1;
125 uint32_t PltIndex = -1;
126 uint32_t GlobalDynIndex = -1;
127
Michael J. Spencer84487f12015-07-24 21:03:07 +0000128protected:
Rafael Espindoladfebd362017-11-29 22:47:35 +0000129 Symbol(Kind K, InputFile *File, StringRefZ Name, uint8_t Binding,
130 uint8_t StOther, uint8_t Type)
131 : Binding(Binding), File(File), SymbolKind(K), NeedsPltAddr(false),
Rui Ueyama7833afd2017-10-27 23:26:46 +0000132 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
Rafael Espindola1d4b3022017-11-28 20:17:58 +0000133 IsInIgot(false), IsPreemptible(false), Used(!Config->GcSections),
134 Type(Type), StOther(StOther), Name(Name) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000135
Michael J. Spencercdae0a42015-07-28 22:58:25 +0000136 const unsigned SymbolKind : 8;
Rui Ueyama7d332f52015-12-25 06:55:39 +0000137
Rafael Espindolaabebed92016-02-05 15:27:15 +0000138public:
Rui Ueyama924b3612017-02-16 06:12:22 +0000139 // True the symbol should point to its PLT entry.
140 // For SharedSymbol only.
141 unsigned NeedsPltAddr : 1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000142 // True if this symbol has an entry in the global part of MIPS GOT.
143 unsigned IsInGlobalMipsGot : 1;
144
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000145 // True if this symbol is referenced by 32-bit GOT relocations.
146 unsigned Is32BitMipsGot : 1;
147
Peter Smithbaffdb82016-12-08 12:58:55 +0000148 // True if this symbol is in the Iplt sub-section of the Plt.
149 unsigned IsInIplt : 1;
150
151 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
152 unsigned IsInIgot : 1;
153
Fangrui Songffac3ed2018-02-23 21:57:49 +0000154 // True if this symbol is preemptible at load time.
Rafael Espindola35c908f2017-08-10 15:05:37 +0000155 unsigned IsPreemptible : 1;
156
Rafael Espindola1d4b3022017-11-28 20:17:58 +0000157 // True if an undefined or shared symbol is used from a live section.
158 unsigned Used : 1;
159
Rui Ueyamadd418072016-04-04 18:15:38 +0000160 // The following fields have the same meaning as the ELF symbol attributes.
161 uint8_t Type; // symbol type
Rui Ueyamab5792b22016-04-04 19:09:08 +0000162 uint8_t StOther; // st_other field value
Rui Ueyamadd418072016-04-04 18:15:38 +0000163
Peter Collingbournef3a2b0e2016-05-03 18:03:47 +0000164 // The Type field may also have this value. It means that we have not yet seen
165 // a non-Lazy symbol with this name, so we don't know what its type is. The
166 // Type field is normally set to this value for Lazy symbols unless we saw a
167 // weak undefined symbol first, in which case we need to remember the original
168 // symbol's type in order to check for TLS mismatches.
169 enum { UnknownType = 255 };
170
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000171 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
172 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
173 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
174 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
175 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
176 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
Peter Collingbournedadcc172016-04-22 18:42:48 +0000177
George Rimar2f0fab52016-03-06 06:26:18 +0000178protected:
Rui Ueyamaa13efc22016-11-29 18:05:04 +0000179 StringRefZ Name;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000180};
181
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000182// Represents a symbol that is defined in the current output file.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000183class Defined : public Symbol {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000184public:
Rafael Espindoladfebd362017-11-29 22:47:35 +0000185 Defined(InputFile *File, StringRefZ Name, uint8_t Binding, uint8_t StOther,
186 uint8_t Type, uint64_t Value, uint64_t Size, SectionBase *Section)
187 : Symbol(DefinedKind, File, Name, Binding, StOther, Type), Value(Value),
Rafael Espindolaf8e405d2017-11-24 19:06:14 +0000188 Size(Size), Section(Section) {}
Rafael Espindolaa8bf71c2016-10-26 20:34:59 +0000189
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000190 static bool classof(const Symbol *S) { return S->isDefined(); }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000191
Rui Ueyama80474a22017-02-28 19:29:55 +0000192 uint64_t Value;
193 uint64_t Size;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000194 SectionBase *Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000195};
196
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000197class Undefined : public Symbol {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000198public:
Rafael Espindoladfebd362017-11-29 22:47:35 +0000199 Undefined(InputFile *File, StringRefZ Name, uint8_t Binding, uint8_t StOther,
200 uint8_t Type)
201 : Symbol(UndefinedKind, File, Name, Binding, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000202
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000203 static bool classof(const Symbol *S) { return S->kind() == UndefinedKind; }
Rafael Espindola1bd885a2015-08-14 16:46:28 +0000204};
205
Rui Ueyama32665f72017-11-06 04:13:24 +0000206class SharedSymbol : public Symbol {
Rafael Espindola18173d42015-09-08 15:50:05 +0000207public:
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000208 static bool classof(const Symbol *S) { return S->kind() == SharedKind; }
Rafael Espindola18173d42015-09-08 15:50:05 +0000209
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000210 SharedSymbol(InputFile &File, StringRef Name, uint8_t Binding,
Rafael Espindoladfebd362017-11-29 22:47:35 +0000211 uint8_t StOther, uint8_t Type, uint64_t Value, uint64_t Size,
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000212 uint32_t Alignment, uint32_t VerdefIndex)
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000213 : Symbol(SharedKind, &File, Name, Binding, StOther, Type), Value(Value),
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000214 Size(Size), VerdefIndex(VerdefIndex), Alignment(Alignment) {
Rui Ueyamad32f06c2017-10-12 21:45:53 +0000215 // GNU ifunc is a mechanism to allow user-supplied functions to
216 // resolve PLT slot values at load-time. This is contrary to the
George Rimard70da0e2017-12-23 16:34:58 +0000217 // regular symbol resolution scheme in which symbols are resolved just
Rui Ueyamad32f06c2017-10-12 21:45:53 +0000218 // by name. Using this hook, you can program how symbols are solved
219 // for you program. For example, you can make "memcpy" to be resolved
220 // to a SSE-enabled version of memcpy only when a machine running the
221 // program supports the SSE instruction set.
222 //
223 // Naturally, such symbols should always be called through their PLT
224 // slots. What GNU ifunc symbols point to are resolver functions, and
225 // calling them directly doesn't make sense (unless you are writing a
226 // loader).
227 //
228 // For DSO symbols, we always call them through PLT slots anyway.
229 // So there's no difference between GNU ifunc and regular function
Shoaib Meenai14cf5142017-10-12 21:52:14 +0000230 // symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC.
Rui Ueyamad32f06c2017-10-12 21:45:53 +0000231 if (this->Type == llvm::ELF::STT_GNU_IFUNC)
George Rimar4a9cfc82017-07-11 11:40:59 +0000232 this->Type = llvm::ELF::STT_FUNC;
Rafael Espindola6e93d052017-08-04 22:31:42 +0000233 }
234
Rafael Espindolaa32ddc42017-12-20 16:28:19 +0000235 template <class ELFT> SharedFile<ELFT> &getFile() const {
236 return *cast<SharedFile<ELFT>>(File);
Peter Collingbourne5dd550a2016-04-26 16:22:51 +0000237 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000238
Rafael Espindolacb562312017-10-13 21:44:10 +0000239 // If not null, there is a copy relocation to this section.
240 InputSection *CopyRelSec = nullptr;
Rui Ueyama007c0022017-03-08 17:24:24 +0000241
Rui Ueyama7f9694a2017-10-28 20:15:56 +0000242 uint64_t Value; // st_value
Rafael Espindola458173e2017-10-30 17:43:16 +0000243 uint64_t Size; // st_size
Rafael Espindola8f619ab2017-12-12 01:45:49 +0000244
245 // This field is a index to the symbol's version definition.
246 uint32_t VerdefIndex;
247
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 Espindolaefb483f2017-12-20 18:01:32 +0000269 Lazy(Kind K, InputFile &File, StringRef Name, uint8_t Type)
270 : Symbol(K, &File, Name, llvm::ELF::STB_GLOBAL, llvm::ELF::STV_DEFAULT,
Rafael Espindoladfebd362017-11-29 22:47:35 +0000271 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 Espindola8276f1b2017-12-20 17:59:43 +0000280 LazyArchive(InputFile &File, const llvm::object::Archive::Symbol S,
Rafael Espindoladfebd362017-11-29 22:47:35 +0000281 uint8_t Type)
Rafael Espindolaefb483f2017-12-20 18:01:32 +0000282 : 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 Espindola8276f1b2017-12-20 17:59:43 +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 Espindola2e5c71e2017-12-20 17:52:36 +0000297 LazyObject(InputFile &File, StringRef Name, uint8_t Type)
Rafael Espindolaefb483f2017-12-20 18:01:32 +0000298 : 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 Espindola2e5c71e2017-12-20 17:52:36 +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) {
Sam Clegg38f52b22018-02-13 17:32:31 +0000350 static_assert(std::is_trivially_destructible<T>(),
351 "Symbol types must be trivially destructible");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000352 static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
353 static_assert(alignof(T) <= alignof(SymbolUnion),
354 "SymbolUnion not aligned enough");
Rui Ueyama38781a52018-02-14 22:43:43 +0000355 assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
356 "Not a Symbol");
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000357
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000358 Symbol Sym = *S;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000359
360 new (S) T(std::forward<ArgT>(Arg)...);
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000361
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000362 S->VersionId = Sym.VersionId;
363 S->Visibility = Sym.Visibility;
364 S->IsUsedInRegularObj = Sym.IsUsedInRegularObj;
365 S->ExportDynamic = Sym.ExportDynamic;
366 S->CanInline = Sym.CanInline;
367 S->Traced = Sym.Traced;
Rui Ueyama69c778c2016-07-17 17:50:09 +0000368
369 // Print out a log message if --trace-symbol was specified.
370 // This is for debugging.
371 if (S->Traced)
372 printTraceSymbol(S);
Peter Collingbourne4f952702016-05-01 04:55:03 +0000373}
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000374} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000375
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000376std::string toString(const elf::Symbol &B);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000377} // namespace lld
378
379#endif