annotate lld/ELF/Symbols.h @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 1f2b6ac9f198
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- Symbols.h ------------------------------------------------*- C++ -*-===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file defines various types of Symbols.
anatofuz
parents:
diff changeset
10 //
anatofuz
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #ifndef LLD_ELF_SYMBOLS_H
anatofuz
parents:
diff changeset
14 #define LLD_ELF_SYMBOLS_H
anatofuz
parents:
diff changeset
15
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
16 #include "Config.h"
150
anatofuz
parents:
diff changeset
17 #include "lld/Common/LLVM.h"
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
18 #include "lld/Common/Memory.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
19 #include "llvm/ADT/DenseMap.h"
150
anatofuz
parents:
diff changeset
20 #include "llvm/Object/ELF.h"
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
21 #include "llvm/Support/Compiler.h"
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
22 #include <tuple>
150
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 namespace lld {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
25 namespace elf {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
26 class Symbol;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
27 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
28 // Returns a string representation for a symbol for diagnostics.
150
anatofuz
parents:
diff changeset
29 std::string toString(const elf::Symbol &);
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 namespace elf {
anatofuz
parents:
diff changeset
32 class CommonSymbol;
anatofuz
parents:
diff changeset
33 class Defined;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
34 class OutputSection;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
35 class SectionBase;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
36 class InputSectionBase;
150
anatofuz
parents:
diff changeset
37 class SharedSymbol;
anatofuz
parents:
diff changeset
38 class Symbol;
anatofuz
parents:
diff changeset
39 class Undefined;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
40 class LazyObject;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
41 class InputFile;
150
anatofuz
parents:
diff changeset
42
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
43 void printTraceSymbol(const Symbol &sym, StringRef name);
150
anatofuz
parents:
diff changeset
44
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
45 enum {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
46 NEEDS_GOT = 1 << 0,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
47 NEEDS_PLT = 1 << 1,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
48 HAS_DIRECT_RELOC = 1 << 2,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
49 // True if this symbol needs a canonical PLT entry, or (during
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
50 // postScanRelocations) a copy relocation.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
51 NEEDS_COPY = 1 << 3,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
52 NEEDS_TLSDESC = 1 << 4,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
53 NEEDS_TLSGD = 1 << 5,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
54 NEEDS_TLSGD_TO_IE = 1 << 6,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
55 NEEDS_GOT_DTPREL = 1 << 7,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
56 NEEDS_TLSIE = 1 << 8,
150
anatofuz
parents:
diff changeset
57 };
anatofuz
parents:
diff changeset
58
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
59 // Some index properties of a symbol are stored separately in this auxiliary
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
60 // struct to decrease sizeof(SymbolUnion) in the majority of cases.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
61 struct SymbolAux {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
62 uint32_t gotIdx = -1;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
63 uint32_t pltIdx = -1;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
64 uint32_t tlsDescIdx = -1;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
65 uint32_t tlsGdIdx = -1;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
66 };
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
67
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
68 LLVM_LIBRARY_VISIBILITY extern SmallVector<SymbolAux, 0> symAux;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
69
150
anatofuz
parents:
diff changeset
70 // The base class for real symbol classes.
anatofuz
parents:
diff changeset
71 class Symbol {
anatofuz
parents:
diff changeset
72 public:
anatofuz
parents:
diff changeset
73 enum Kind {
anatofuz
parents:
diff changeset
74 PlaceholderKind,
anatofuz
parents:
diff changeset
75 DefinedKind,
anatofuz
parents:
diff changeset
76 CommonKind,
anatofuz
parents:
diff changeset
77 SharedKind,
anatofuz
parents:
diff changeset
78 UndefinedKind,
anatofuz
parents:
diff changeset
79 LazyObjectKind,
anatofuz
parents:
diff changeset
80 };
anatofuz
parents:
diff changeset
81
anatofuz
parents:
diff changeset
82 Kind kind() const { return static_cast<Kind>(symbolKind); }
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 // The file from which this symbol was created.
anatofuz
parents:
diff changeset
85 InputFile *file;
anatofuz
parents:
diff changeset
86
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
87 // The default copy constructor is deleted due to atomic flags. Define one for
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
88 // places where no atomic is needed.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
89 Symbol(const Symbol &o) { memcpy(this, &o, sizeof(o)); }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
90
150
anatofuz
parents:
diff changeset
91 protected:
anatofuz
parents:
diff changeset
92 const char *nameData;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
93 // 32-bit size saves space.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
94 uint32_t nameSize;
150
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 public:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
97 // The next three fields have the same meaning as the ELF symbol attributes.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
98 // type and binding are placed in this order to optimize generating st_info,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
99 // which is defined as (binding << 4) + (type & 0xf), on a little-endian
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
100 // system.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
101 uint8_t type : 4; // symbol type
150
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103 // Symbol binding. This is not overwritten by replace() to track
anatofuz
parents:
diff changeset
104 // changes during resolution. In particular:
anatofuz
parents:
diff changeset
105 // - An undefined weak is still weak when it resolves to a shared library.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
106 // - An undefined weak will not extract archive members, but we have to
150
anatofuz
parents:
diff changeset
107 // remember it is weak.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
108 uint8_t binding : 4;
150
anatofuz
parents:
diff changeset
109
anatofuz
parents:
diff changeset
110 uint8_t stOther; // st_other field value
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 uint8_t symbolKind;
anatofuz
parents:
diff changeset
113
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
114 // The partition whose dynamic symbol table contains this symbol's definition.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
115 uint8_t partition;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
116
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
117 // True if this symbol is preemptible at load time.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
118 uint8_t isPreemptible : 1;
150
anatofuz
parents:
diff changeset
119
anatofuz
parents:
diff changeset
120 // True if the symbol was used for linking and thus need to be added to the
anatofuz
parents:
diff changeset
121 // output file's symbol table. This is true for all symbols except for
anatofuz
parents:
diff changeset
122 // unreferenced DSO symbols, lazy (archive) symbols, and bitcode symbols that
anatofuz
parents:
diff changeset
123 // are unreferenced except by other bitcode objects.
anatofuz
parents:
diff changeset
124 uint8_t isUsedInRegularObj : 1;
anatofuz
parents:
diff changeset
125
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
126 // True if an undefined or shared symbol is used from a live section.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
127 //
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
128 // NOTE: In Writer.cpp the field is used to mark local defined symbols
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
129 // which are referenced by relocations when -r or --emit-relocs is given.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
130 uint8_t used : 1;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
131
150
anatofuz
parents:
diff changeset
132 // Used by a Defined symbol with protected or default visibility, to record
anatofuz
parents:
diff changeset
133 // whether it is required to be exported into .dynsym. This is set when any of
anatofuz
parents:
diff changeset
134 // the following conditions hold:
anatofuz
parents:
diff changeset
135 //
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
136 // - If there is an interposable symbol from a DSO. Note: We also do this for
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
137 // STV_PROTECTED symbols which can't be interposed (to match BFD behavior).
150
anatofuz
parents:
diff changeset
138 // - If -shared or --export-dynamic is specified, any symbol in an object
anatofuz
parents:
diff changeset
139 // file/bitcode sets this property, unless suppressed by LTO
anatofuz
parents:
diff changeset
140 // canBeOmittedFromSymbolTable().
anatofuz
parents:
diff changeset
141 uint8_t exportDynamic : 1;
anatofuz
parents:
diff changeset
142
anatofuz
parents:
diff changeset
143 // True if the symbol is in the --dynamic-list file. A Defined symbol with
anatofuz
parents:
diff changeset
144 // protected or default visibility with this property is required to be
anatofuz
parents:
diff changeset
145 // exported into .dynsym.
anatofuz
parents:
diff changeset
146 uint8_t inDynamicList : 1;
anatofuz
parents:
diff changeset
147
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
148 // Used to track if there has been at least one undefined reference to the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
149 // symbol. For Undefined and SharedSymbol, the binding may change to STB_WEAK
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
150 // if the first undefined reference from a non-shared object is weak.
150
anatofuz
parents:
diff changeset
151 uint8_t referenced : 1;
anatofuz
parents:
diff changeset
152
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
153 // Used to track if this symbol will be referenced after wrapping is performed
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
154 // (i.e. this will be true for foo if __real_foo is referenced, and will be
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
155 // true for __wrap_foo if foo is referenced).
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
156 uint8_t referencedAfterWrap : 1;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
157
150
anatofuz
parents:
diff changeset
158 // True if this symbol is specified by --trace-symbol option.
anatofuz
parents:
diff changeset
159 uint8_t traced : 1;
anatofuz
parents:
diff changeset
160
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
161 // True if the name contains '@'.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
162 uint8_t hasVersionSuffix : 1;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
163
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
164 // Symbol visibility. This is the computed minimum visibility of all
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
165 // observed non-DSO symbols.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
166 uint8_t visibility() const { return stOther & 3; }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
167 void setVisibility(uint8_t visibility) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
168 stOther = (stOther & ~3) | visibility;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
169 }
150
anatofuz
parents:
diff changeset
170
anatofuz
parents:
diff changeset
171 bool includeInDynsym() const;
anatofuz
parents:
diff changeset
172 uint8_t computeBinding() const;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
173 bool isGlobal() const { return binding == llvm::ELF::STB_GLOBAL; }
150
anatofuz
parents:
diff changeset
174 bool isWeak() const { return binding == llvm::ELF::STB_WEAK; }
anatofuz
parents:
diff changeset
175
anatofuz
parents:
diff changeset
176 bool isUndefined() const { return symbolKind == UndefinedKind; }
anatofuz
parents:
diff changeset
177 bool isCommon() const { return symbolKind == CommonKind; }
anatofuz
parents:
diff changeset
178 bool isDefined() const { return symbolKind == DefinedKind; }
anatofuz
parents:
diff changeset
179 bool isShared() const { return symbolKind == SharedKind; }
anatofuz
parents:
diff changeset
180 bool isPlaceholder() const { return symbolKind == PlaceholderKind; }
anatofuz
parents:
diff changeset
181
anatofuz
parents:
diff changeset
182 bool isLocal() const { return binding == llvm::ELF::STB_LOCAL; }
anatofuz
parents:
diff changeset
183
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
184 bool isLazy() const { return symbolKind == LazyObjectKind; }
150
anatofuz
parents:
diff changeset
185
anatofuz
parents:
diff changeset
186 // True if this is an undefined weak symbol. This only works once
anatofuz
parents:
diff changeset
187 // all input files have been added.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
188 bool isUndefWeak() const { return isWeak() && isUndefined(); }
150
anatofuz
parents:
diff changeset
189
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
190 StringRef getName() const { return {nameData, nameSize}; }
150
anatofuz
parents:
diff changeset
191
anatofuz
parents:
diff changeset
192 void setName(StringRef s) {
anatofuz
parents:
diff changeset
193 nameData = s.data();
anatofuz
parents:
diff changeset
194 nameSize = s.size();
anatofuz
parents:
diff changeset
195 }
anatofuz
parents:
diff changeset
196
anatofuz
parents:
diff changeset
197 void parseSymbolVersion();
anatofuz
parents:
diff changeset
198
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
199 // Get the NUL-terminated version suffix ("", "@...", or "@@...").
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
200 //
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
201 // For @@, the name has been truncated by insert(). For @, the name has been
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
202 // truncated by Symbol::parseSymbolVersion().
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
203 const char *getVersionSuffix() const { return nameData + nameSize; }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
204
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
205 uint32_t getGotIdx() const { return symAux[auxIdx].gotIdx; }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
206 uint32_t getPltIdx() const { return symAux[auxIdx].pltIdx; }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
207 uint32_t getTlsDescIdx() const { return symAux[auxIdx].tlsDescIdx; }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
208 uint32_t getTlsGdIdx() const { return symAux[auxIdx].tlsGdIdx; }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
209
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
210 bool isInGot() const { return getGotIdx() != uint32_t(-1); }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
211 bool isInPlt() const { return getPltIdx() != uint32_t(-1); }
150
anatofuz
parents:
diff changeset
212
anatofuz
parents:
diff changeset
213 uint64_t getVA(int64_t addend = 0) const;
anatofuz
parents:
diff changeset
214
anatofuz
parents:
diff changeset
215 uint64_t getGotOffset() const;
anatofuz
parents:
diff changeset
216 uint64_t getGotVA() const;
anatofuz
parents:
diff changeset
217 uint64_t getGotPltOffset() const;
anatofuz
parents:
diff changeset
218 uint64_t getGotPltVA() const;
anatofuz
parents:
diff changeset
219 uint64_t getPltVA() const;
anatofuz
parents:
diff changeset
220 uint64_t getSize() const;
anatofuz
parents:
diff changeset
221 OutputSection *getOutputSection() const;
anatofuz
parents:
diff changeset
222
anatofuz
parents:
diff changeset
223 // The following two functions are used for symbol resolution.
anatofuz
parents:
diff changeset
224 //
anatofuz
parents:
diff changeset
225 // You are expected to call mergeProperties for all symbols in input
anatofuz
parents:
diff changeset
226 // files so that attributes that are attached to names rather than
anatofuz
parents:
diff changeset
227 // indivisual symbol (such as visibility) are merged together.
anatofuz
parents:
diff changeset
228 //
anatofuz
parents:
diff changeset
229 // Every time you read a new symbol from an input, you are supposed
anatofuz
parents:
diff changeset
230 // to call resolve() with the new symbol. That function replaces
anatofuz
parents:
diff changeset
231 // "this" object as a result of name resolution if the new symbol is
anatofuz
parents:
diff changeset
232 // more appropriate to be included in the output.
anatofuz
parents:
diff changeset
233 //
anatofuz
parents:
diff changeset
234 // For example, if "this" is an undefined symbol and a new symbol is
anatofuz
parents:
diff changeset
235 // a defined symbol, "this" is replaced with the new symbol.
anatofuz
parents:
diff changeset
236 void mergeProperties(const Symbol &other);
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
237 void resolve(const Undefined &other);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
238 void resolve(const CommonSymbol &other);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
239 void resolve(const Defined &other);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
240 void resolve(const LazyObject &other);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
241 void resolve(const SharedSymbol &other);
150
anatofuz
parents:
diff changeset
242
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
243 // If this is a lazy symbol, extract an input file and add the symbol
150
anatofuz
parents:
diff changeset
244 // in the file to the symbol table. Calling this function on
anatofuz
parents:
diff changeset
245 // non-lazy object causes a runtime error.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
246 void extract() const;
150
anatofuz
parents:
diff changeset
247
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
248 void checkDuplicate(const Defined &other) const;
150
anatofuz
parents:
diff changeset
249
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
250 private:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
251 bool shouldReplace(const Defined &other) const;
150
anatofuz
parents:
diff changeset
252
anatofuz
parents:
diff changeset
253 protected:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
254 Symbol(Kind k, InputFile *file, StringRef name, uint8_t binding,
150
anatofuz
parents:
diff changeset
255 uint8_t stOther, uint8_t type)
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
256 : file(file), nameData(name.data()), nameSize(name.size()), type(type),
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
257 binding(binding), stOther(stOther), symbolKind(k), exportDynamic(false),
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
258 archSpecificBit(false) {}
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
259
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
260 void overwrite(Symbol &sym, Kind k) const {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
261 if (sym.traced)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
262 printTraceSymbol(*this, sym.getName());
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
263 sym.file = file;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
264 sym.type = type;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
265 sym.binding = binding;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
266 sym.stOther = (stOther & ~3) | sym.visibility();
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
267 sym.symbolKind = k;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
268 }
150
anatofuz
parents:
diff changeset
269
anatofuz
parents:
diff changeset
270 public:
anatofuz
parents:
diff changeset
271 // True if this symbol is in the Iplt sub-section of the Plt and the Igot
anatofuz
parents:
diff changeset
272 // sub-section of the .got.plt or .got.
anatofuz
parents:
diff changeset
273 uint8_t isInIplt : 1;
anatofuz
parents:
diff changeset
274
anatofuz
parents:
diff changeset
275 // True if this symbol needs a GOT entry and its GOT entry is actually in
anatofuz
parents:
diff changeset
276 // Igot. This will be true only for certain non-preemptible ifuncs.
anatofuz
parents:
diff changeset
277 uint8_t gotInIgot : 1;
anatofuz
parents:
diff changeset
278
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
279 // True if defined relative to a section discarded by ICF.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
280 uint8_t folded : 1;
150
anatofuz
parents:
diff changeset
281
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
282 // Allow reuse of a bit between architecture-exclusive symbol flags.
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
283 // - needsTocRestore(): On PPC64, true if a call to this symbol needs to be
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
284 // followed by a restore of the toc pointer.
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
285 // - isTagged(): On AArch64, true if the symbol needs special relocation and
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
286 // metadata semantics because it's tagged, under the AArch64 MemtagABI.
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
287 uint8_t archSpecificBit : 1;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
288 bool needsTocRestore() const { return archSpecificBit; }
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
289 bool isTagged() const { return archSpecificBit; }
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
290 void setNeedsTocRestore(bool v) { archSpecificBit = v; }
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
291 void setIsTagged(bool v) {
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
292 archSpecificBit = v;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
293 }
150
anatofuz
parents:
diff changeset
294
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
295 // True if this symbol is defined by a symbol assignment or wrapped by --wrap.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
296 //
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
297 // LTO shouldn't inline the symbol because it doesn't know the final content
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
298 // of the symbol.
150
anatofuz
parents:
diff changeset
299 uint8_t scriptDefined : 1;
anatofuz
parents:
diff changeset
300
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
301 // True if defined in a DSO as protected visibility.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
302 uint8_t dsoProtected : 1;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
303
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
304 // True if targeted by a range extension thunk.
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
305 uint8_t thunkAccessed : 1;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
306
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
307 // Temporary flags used to communicate which symbol entries need PLT and GOT
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
308 // entries during postScanRelocations();
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
309 std::atomic<uint16_t> flags;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
310
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
311 // A symAux index used to access GOT/PLT entry indexes. This is allocated in
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
312 // postScanRelocations().
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
313 uint32_t auxIdx;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
314 uint32_t dynsymIndex;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
315
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
316 // This field is a index to the symbol's version definition.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
317 uint16_t verdefIndex;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
318
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
319 // Version definition index.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
320 uint16_t versionId;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
321
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
322 void setFlags(uint16_t bits) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
323 flags.fetch_or(bits, std::memory_order_relaxed);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
324 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
325 bool hasFlag(uint16_t bit) const {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
326 assert(bit && (bit & (bit - 1)) == 0 && "bit must be a power of 2");
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
327 return flags.load(std::memory_order_relaxed) & bit;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
328 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
329
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
330 bool needsDynReloc() const {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
331 return flags.load(std::memory_order_relaxed) &
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
332 (NEEDS_COPY | NEEDS_GOT | NEEDS_PLT | NEEDS_TLSDESC | NEEDS_TLSGD |
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
333 NEEDS_TLSGD_TO_IE | NEEDS_GOT_DTPREL | NEEDS_TLSIE);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
334 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
335 void allocateAux() {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
336 assert(auxIdx == 0);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
337 auxIdx = symAux.size();
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
338 symAux.emplace_back();
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
339 }
150
anatofuz
parents:
diff changeset
340
anatofuz
parents:
diff changeset
341 bool isSection() const { return type == llvm::ELF::STT_SECTION; }
anatofuz
parents:
diff changeset
342 bool isTls() const { return type == llvm::ELF::STT_TLS; }
anatofuz
parents:
diff changeset
343 bool isFunc() const { return type == llvm::ELF::STT_FUNC; }
anatofuz
parents:
diff changeset
344 bool isGnuIFunc() const { return type == llvm::ELF::STT_GNU_IFUNC; }
anatofuz
parents:
diff changeset
345 bool isObject() const { return type == llvm::ELF::STT_OBJECT; }
anatofuz
parents:
diff changeset
346 bool isFile() const { return type == llvm::ELF::STT_FILE; }
anatofuz
parents:
diff changeset
347 };
anatofuz
parents:
diff changeset
348
anatofuz
parents:
diff changeset
349 // Represents a symbol that is defined in the current output file.
anatofuz
parents:
diff changeset
350 class Defined : public Symbol {
anatofuz
parents:
diff changeset
351 public:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
352 Defined(InputFile *file, StringRef name, uint8_t binding, uint8_t stOther,
150
anatofuz
parents:
diff changeset
353 uint8_t type, uint64_t value, uint64_t size, SectionBase *section)
anatofuz
parents:
diff changeset
354 : Symbol(DefinedKind, file, name, binding, stOther, type), value(value),
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
355 size(size), section(section) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
356 exportDynamic = config->exportDynamic;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
357 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
358 void overwrite(Symbol &sym) const {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
359 Symbol::overwrite(sym, DefinedKind);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
360 sym.verdefIndex = -1;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
361 auto &s = static_cast<Defined &>(sym);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
362 s.value = value;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
363 s.size = size;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
364 s.section = section;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
365 }
150
anatofuz
parents:
diff changeset
366
anatofuz
parents:
diff changeset
367 static bool classof(const Symbol *s) { return s->isDefined(); }
anatofuz
parents:
diff changeset
368
anatofuz
parents:
diff changeset
369 uint64_t value;
anatofuz
parents:
diff changeset
370 uint64_t size;
anatofuz
parents:
diff changeset
371 SectionBase *section;
anatofuz
parents:
diff changeset
372 };
anatofuz
parents:
diff changeset
373
anatofuz
parents:
diff changeset
374 // Represents a common symbol.
anatofuz
parents:
diff changeset
375 //
anatofuz
parents:
diff changeset
376 // On Unix, it is traditionally allowed to write variable definitions
anatofuz
parents:
diff changeset
377 // without initialization expressions (such as "int foo;") to header
anatofuz
parents:
diff changeset
378 // files. Such definition is called "tentative definition".
anatofuz
parents:
diff changeset
379 //
anatofuz
parents:
diff changeset
380 // Using tentative definition is usually considered a bad practice
anatofuz
parents:
diff changeset
381 // because you should write only declarations (such as "extern int
anatofuz
parents:
diff changeset
382 // foo;") to header files. Nevertheless, the linker and the compiler
anatofuz
parents:
diff changeset
383 // have to do something to support bad code by allowing duplicate
anatofuz
parents:
diff changeset
384 // definitions for this particular case.
anatofuz
parents:
diff changeset
385 //
anatofuz
parents:
diff changeset
386 // Common symbols represent variable definitions without initializations.
anatofuz
parents:
diff changeset
387 // The compiler creates common symbols when it sees variable definitions
anatofuz
parents:
diff changeset
388 // without initialization (you can suppress this behavior and let the
anatofuz
parents:
diff changeset
389 // compiler create a regular defined symbol by -fno-common).
anatofuz
parents:
diff changeset
390 //
anatofuz
parents:
diff changeset
391 // The linker allows common symbols to be replaced by regular defined
anatofuz
parents:
diff changeset
392 // symbols. If there are remaining common symbols after name resolution is
anatofuz
parents:
diff changeset
393 // complete, they are converted to regular defined symbols in a .bss
anatofuz
parents:
diff changeset
394 // section. (Therefore, the later passes don't see any CommonSymbols.)
anatofuz
parents:
diff changeset
395 class CommonSymbol : public Symbol {
anatofuz
parents:
diff changeset
396 public:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
397 CommonSymbol(InputFile *file, StringRef name, uint8_t binding,
150
anatofuz
parents:
diff changeset
398 uint8_t stOther, uint8_t type, uint64_t alignment, uint64_t size)
anatofuz
parents:
diff changeset
399 : Symbol(CommonKind, file, name, binding, stOther, type),
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
400 alignment(alignment), size(size) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
401 exportDynamic = config->exportDynamic;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
402 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
403 void overwrite(Symbol &sym) const {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
404 Symbol::overwrite(sym, CommonKind);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
405 auto &s = static_cast<CommonSymbol &>(sym);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
406 s.alignment = alignment;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
407 s.size = size;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
408 }
150
anatofuz
parents:
diff changeset
409
anatofuz
parents:
diff changeset
410 static bool classof(const Symbol *s) { return s->isCommon(); }
anatofuz
parents:
diff changeset
411
anatofuz
parents:
diff changeset
412 uint32_t alignment;
anatofuz
parents:
diff changeset
413 uint64_t size;
anatofuz
parents:
diff changeset
414 };
anatofuz
parents:
diff changeset
415
anatofuz
parents:
diff changeset
416 class Undefined : public Symbol {
anatofuz
parents:
diff changeset
417 public:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
418 Undefined(InputFile *file, StringRef name, uint8_t binding, uint8_t stOther,
150
anatofuz
parents:
diff changeset
419 uint8_t type, uint32_t discardedSecIdx = 0)
anatofuz
parents:
diff changeset
420 : Symbol(UndefinedKind, file, name, binding, stOther, type),
anatofuz
parents:
diff changeset
421 discardedSecIdx(discardedSecIdx) {}
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
422 void overwrite(Symbol &sym) const {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
423 Symbol::overwrite(sym, UndefinedKind);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
424 auto &s = static_cast<Undefined &>(sym);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
425 s.discardedSecIdx = discardedSecIdx;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
426 s.nonPrevailing = nonPrevailing;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
427 }
150
anatofuz
parents:
diff changeset
428
anatofuz
parents:
diff changeset
429 static bool classof(const Symbol *s) { return s->kind() == UndefinedKind; }
anatofuz
parents:
diff changeset
430
anatofuz
parents:
diff changeset
431 // The section index if in a discarded section, 0 otherwise.
anatofuz
parents:
diff changeset
432 uint32_t discardedSecIdx;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
433 bool nonPrevailing = false;
150
anatofuz
parents:
diff changeset
434 };
anatofuz
parents:
diff changeset
435
anatofuz
parents:
diff changeset
436 class SharedSymbol : public Symbol {
anatofuz
parents:
diff changeset
437 public:
anatofuz
parents:
diff changeset
438 static bool classof(const Symbol *s) { return s->kind() == SharedKind; }
anatofuz
parents:
diff changeset
439
anatofuz
parents:
diff changeset
440 SharedSymbol(InputFile &file, StringRef name, uint8_t binding,
anatofuz
parents:
diff changeset
441 uint8_t stOther, uint8_t type, uint64_t value, uint64_t size,
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
442 uint32_t alignment)
150
anatofuz
parents:
diff changeset
443 : Symbol(SharedKind, &file, name, binding, stOther, type), value(value),
anatofuz
parents:
diff changeset
444 size(size), alignment(alignment) {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
445 exportDynamic = true;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
446 dsoProtected = visibility() == llvm::ELF::STV_PROTECTED;
150
anatofuz
parents:
diff changeset
447 // GNU ifunc is a mechanism to allow user-supplied functions to
anatofuz
parents:
diff changeset
448 // resolve PLT slot values at load-time. This is contrary to the
anatofuz
parents:
diff changeset
449 // regular symbol resolution scheme in which symbols are resolved just
anatofuz
parents:
diff changeset
450 // by name. Using this hook, you can program how symbols are solved
anatofuz
parents:
diff changeset
451 // for you program. For example, you can make "memcpy" to be resolved
anatofuz
parents:
diff changeset
452 // to a SSE-enabled version of memcpy only when a machine running the
anatofuz
parents:
diff changeset
453 // program supports the SSE instruction set.
anatofuz
parents:
diff changeset
454 //
anatofuz
parents:
diff changeset
455 // Naturally, such symbols should always be called through their PLT
anatofuz
parents:
diff changeset
456 // slots. What GNU ifunc symbols point to are resolver functions, and
anatofuz
parents:
diff changeset
457 // calling them directly doesn't make sense (unless you are writing a
anatofuz
parents:
diff changeset
458 // loader).
anatofuz
parents:
diff changeset
459 //
anatofuz
parents:
diff changeset
460 // For DSO symbols, we always call them through PLT slots anyway.
anatofuz
parents:
diff changeset
461 // So there's no difference between GNU ifunc and regular function
anatofuz
parents:
diff changeset
462 // symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC.
anatofuz
parents:
diff changeset
463 if (this->type == llvm::ELF::STT_GNU_IFUNC)
anatofuz
parents:
diff changeset
464 this->type = llvm::ELF::STT_FUNC;
anatofuz
parents:
diff changeset
465 }
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
466 void overwrite(Symbol &sym) const {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
467 Symbol::overwrite(sym, SharedKind);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
468 auto &s = static_cast<SharedSymbol &>(sym);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
469 s.dsoProtected = dsoProtected;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
470 s.value = value;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
471 s.size = size;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
472 s.alignment = alignment;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
473 }
150
anatofuz
parents:
diff changeset
474
anatofuz
parents:
diff changeset
475 uint64_t value; // st_value
anatofuz
parents:
diff changeset
476 uint64_t size; // st_size
anatofuz
parents:
diff changeset
477 uint32_t alignment;
anatofuz
parents:
diff changeset
478 };
anatofuz
parents:
diff changeset
479
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
480 // LazyObject symbols represent symbols in object files between --start-lib and
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
481 // --end-lib options. LLD also handles traditional archives as if all the files
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
482 // in the archive are surrounded by --start-lib and --end-lib.
150
anatofuz
parents:
diff changeset
483 //
anatofuz
parents:
diff changeset
484 // A special complication is the handling of weak undefined symbols. They should
anatofuz
parents:
diff changeset
485 // not load a file, but we have to remember we have seen both the weak undefined
anatofuz
parents:
diff changeset
486 // and the lazy. We represent that with a lazy symbol with a weak binding. This
anatofuz
parents:
diff changeset
487 // means that code looking for undefined symbols normally also has to take lazy
anatofuz
parents:
diff changeset
488 // symbols into consideration.
anatofuz
parents:
diff changeset
489 class LazyObject : public Symbol {
anatofuz
parents:
diff changeset
490 public:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
491 LazyObject(InputFile &file)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
492 : Symbol(LazyObjectKind, &file, {}, llvm::ELF::STB_GLOBAL,
150
anatofuz
parents:
diff changeset
493 llvm::ELF::STV_DEFAULT, llvm::ELF::STT_NOTYPE) {}
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
494 void overwrite(Symbol &sym) const { Symbol::overwrite(sym, LazyObjectKind); }
150
anatofuz
parents:
diff changeset
495
anatofuz
parents:
diff changeset
496 static bool classof(const Symbol *s) { return s->kind() == LazyObjectKind; }
anatofuz
parents:
diff changeset
497 };
anatofuz
parents:
diff changeset
498
anatofuz
parents:
diff changeset
499 // Some linker-generated symbols need to be created as
anatofuz
parents:
diff changeset
500 // Defined symbols.
anatofuz
parents:
diff changeset
501 struct ElfSym {
anatofuz
parents:
diff changeset
502 // __bss_start
anatofuz
parents:
diff changeset
503 static Defined *bss;
anatofuz
parents:
diff changeset
504
anatofuz
parents:
diff changeset
505 // etext and _etext
anatofuz
parents:
diff changeset
506 static Defined *etext1;
anatofuz
parents:
diff changeset
507 static Defined *etext2;
anatofuz
parents:
diff changeset
508
anatofuz
parents:
diff changeset
509 // edata and _edata
anatofuz
parents:
diff changeset
510 static Defined *edata1;
anatofuz
parents:
diff changeset
511 static Defined *edata2;
anatofuz
parents:
diff changeset
512
anatofuz
parents:
diff changeset
513 // end and _end
anatofuz
parents:
diff changeset
514 static Defined *end1;
anatofuz
parents:
diff changeset
515 static Defined *end2;
anatofuz
parents:
diff changeset
516
anatofuz
parents:
diff changeset
517 // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
anatofuz
parents:
diff changeset
518 // be at some offset from the base of the .got section, usually 0 or
anatofuz
parents:
diff changeset
519 // the end of the .got.
anatofuz
parents:
diff changeset
520 static Defined *globalOffsetTable;
anatofuz
parents:
diff changeset
521
anatofuz
parents:
diff changeset
522 // _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
anatofuz
parents:
diff changeset
523 static Defined *mipsGp;
anatofuz
parents:
diff changeset
524 static Defined *mipsGpDisp;
anatofuz
parents:
diff changeset
525 static Defined *mipsLocalGp;
anatofuz
parents:
diff changeset
526
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
527 // __global_pointer$ for RISC-V.
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
528 static Defined *riscvGlobalPointer;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
529
150
anatofuz
parents:
diff changeset
530 // __rel{,a}_iplt_{start,end} symbols.
anatofuz
parents:
diff changeset
531 static Defined *relaIpltStart;
anatofuz
parents:
diff changeset
532 static Defined *relaIpltEnd;
anatofuz
parents:
diff changeset
533
anatofuz
parents:
diff changeset
534 // _TLS_MODULE_BASE_ on targets that support TLSDESC.
anatofuz
parents:
diff changeset
535 static Defined *tlsModuleBase;
anatofuz
parents:
diff changeset
536 };
anatofuz
parents:
diff changeset
537
anatofuz
parents:
diff changeset
538 // A buffer class that is large enough to hold any Symbol-derived
anatofuz
parents:
diff changeset
539 // object. We allocate memory using this class and instantiate a symbol
anatofuz
parents:
diff changeset
540 // using the placement new.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
541
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
542 // It is important to keep the size of SymbolUnion small for performance and
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
543 // memory usage reasons. 64 bytes is a soft limit based on the size of Defined
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
544 // on a 64-bit system. This is enforced by a static_assert in Symbols.cpp.
150
anatofuz
parents:
diff changeset
545 union SymbolUnion {
anatofuz
parents:
diff changeset
546 alignas(Defined) char a[sizeof(Defined)];
anatofuz
parents:
diff changeset
547 alignas(CommonSymbol) char b[sizeof(CommonSymbol)];
anatofuz
parents:
diff changeset
548 alignas(Undefined) char c[sizeof(Undefined)];
anatofuz
parents:
diff changeset
549 alignas(SharedSymbol) char d[sizeof(SharedSymbol)];
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
550 alignas(LazyObject) char e[sizeof(LazyObject)];
150
anatofuz
parents:
diff changeset
551 };
anatofuz
parents:
diff changeset
552
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
553 template <typename... T> Defined *makeDefined(T &&...args) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
554 auto *sym = getSpecificAllocSingleton<SymbolUnion>().Allocate();
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
555 memset(sym, 0, sizeof(Symbol));
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
556 auto &s = *new (reinterpret_cast<Defined *>(sym)) Defined(std::forward<T>(args)...);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
557 return &s;
150
anatofuz
parents:
diff changeset
558 }
anatofuz
parents:
diff changeset
559
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
560 void reportDuplicate(const Symbol &sym, const InputFile *newFile,
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
561 InputSectionBase *errSec, uint64_t errOffset);
150
anatofuz
parents:
diff changeset
562 void maybeWarnUnorderableSymbol(const Symbol *sym);
anatofuz
parents:
diff changeset
563 bool computeIsPreemptible(const Symbol &sym);
anatofuz
parents:
diff changeset
564
anatofuz
parents:
diff changeset
565 } // namespace elf
anatofuz
parents:
diff changeset
566 } // namespace lld
anatofuz
parents:
diff changeset
567
anatofuz
parents:
diff changeset
568 #endif