Bug Summary

File:tools/lld/ELF/Symbols.h
Warning:line 220, column 7
Value stored to 'Type' is never read

Annotated Source Code

1//===- Symbols.h ------------------------------------------------*- C++ -*-===//
2//
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//===----------------------------------------------------------------------===//
9//
10// All symbols are handled as SymbolBodies regardless of their types.
11// This file defines various types of SymbolBodies.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLD_ELF_SYMBOLS_H
16#define LLD_ELF_SYMBOLS_H
17
18#include "InputSection.h"
19#include "Strings.h"
20
21#include "lld/Core/LLVM.h"
22#include "llvm/Object/Archive.h"
23#include "llvm/Object/ELF.h"
24
25namespace lld {
26namespace elf {
27
28class ArchiveFile;
29class BitcodeFile;
30class InputFile;
31class LazyObjectFile;
32template <class ELFT> class ObjectFile;
33class OutputSection;
34template <class ELFT> class SharedFile;
35
36struct Symbol;
37
38// The base class for real symbol classes.
39class SymbolBody {
40public:
41 enum Kind {
42 DefinedFirst,
43 DefinedRegularKind = DefinedFirst,
44 SharedKind,
45 DefinedCommonKind,
46 DefinedLast = DefinedCommonKind,
47 UndefinedKind,
48 LazyArchiveKind,
49 LazyObjectKind,
50 };
51
52 SymbolBody(Kind K) : SymbolKind(K) {}
53
54 Symbol *symbol();
55 const Symbol *symbol() const {
56 return const_cast<SymbolBody *>(this)->symbol();
57 }
58
59 Kind kind() const { return static_cast<Kind>(SymbolKind); }
60
61 bool isUndefined() const { return SymbolKind == UndefinedKind; }
62 bool isDefined() const { return SymbolKind <= DefinedLast; }
63 bool isCommon() const { return SymbolKind == DefinedCommonKind; }
64 bool isLazy() const {
65 return SymbolKind == LazyArchiveKind || SymbolKind == LazyObjectKind;
66 }
67 bool isShared() const { return SymbolKind == SharedKind; }
68 bool isInCurrentDSO() const { return !isUndefined() && !isShared(); }
69 bool isLocal() const { return IsLocal; }
70 bool isPreemptible() const;
71 StringRef getName() const { return Name; }
72 uint8_t getVisibility() const { return StOther & 0x3; }
73 void parseSymbolVersion();
74
75 bool isInGot() const { return GotIndex != -1U; }
76 bool isInPlt() const { return PltIndex != -1U; }
77
78 uint64_t getVA(int64_t Addend = 0) const;
79
80 uint64_t getGotOffset() const;
81 template <class ELFT> typename ELFT::uint getGotVA() const;
82 uint64_t getGotPltOffset() const;
83 uint64_t getGotPltVA() const;
84 uint64_t getPltVA() const;
85 template <class ELFT> typename ELFT::uint getSize() const;
86 OutputSection *getOutputSection() const;
87
88 // The file from which this symbol was created.
89 InputFile *File = nullptr;
90
91 uint32_t DynsymIndex = 0;
92 uint32_t GotIndex = -1;
93 uint32_t GotPltIndex = -1;
94 uint32_t PltIndex = -1;
95 uint32_t GlobalDynIndex = -1;
96
97protected:
98 SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
99 uint8_t Type);
100
101 const unsigned SymbolKind : 8;
102
103public:
104 // True if the linker has to generate a copy relocation.
105 // For SharedSymbol only.
106 unsigned NeedsCopy : 1;
107
108 // True the symbol should point to its PLT entry.
109 // For SharedSymbol only.
110 unsigned NeedsPltAddr : 1;
111
112 // True if this is a local symbol.
113 unsigned IsLocal : 1;
114
115 // True if this symbol has an entry in the global part of MIPS GOT.
116 unsigned IsInGlobalMipsGot : 1;
117
118 // True if this symbol is referenced by 32-bit GOT relocations.
119 unsigned Is32BitMipsGot : 1;
120
121 // True if this symbol is in the Iplt sub-section of the Plt.
122 unsigned IsInIplt : 1;
123
124 // True if this symbol is in the Igot sub-section of the .got.plt or .got.
125 unsigned IsInIgot : 1;
126
127 // The following fields have the same meaning as the ELF symbol attributes.
128 uint8_t Type; // symbol type
129 uint8_t StOther; // st_other field value
130
131 // The Type field may also have this value. It means that we have not yet seen
132 // a non-Lazy symbol with this name, so we don't know what its type is. The
133 // Type field is normally set to this value for Lazy symbols unless we saw a
134 // weak undefined symbol first, in which case we need to remember the original
135 // symbol's type in order to check for TLS mismatches.
136 enum { UnknownType = 255 };
137
138 bool isSection() const { return Type == llvm::ELF::STT_SECTION; }
139 bool isTls() const { return Type == llvm::ELF::STT_TLS; }
140 bool isFunc() const { return Type == llvm::ELF::STT_FUNC; }
141 bool isGnuIFunc() const { return Type == llvm::ELF::STT_GNU_IFUNC; }
142 bool isObject() const { return Type == llvm::ELF::STT_OBJECT; }
143 bool isFile() const { return Type == llvm::ELF::STT_FILE; }
144
145protected:
146 StringRefZ Name;
147};
148
149// The base class for any defined symbols.
150class Defined : public SymbolBody {
151public:
152 Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type);
153 static bool classof(const SymbolBody *S) { return S->isDefined(); }
154};
155
156class DefinedCommon : public Defined {
157public:
158 DefinedCommon(StringRef N, uint64_t Size, uint32_t Alignment, uint8_t StOther,
159 uint8_t Type, InputFile *File);
160
161 static bool classof(const SymbolBody *S) {
162 return S->kind() == SymbolBody::DefinedCommonKind;
163 }
164
165 // The output offset of this common symbol in the output bss. Computed by the
166 // writer.
167 uint64_t Offset;
168
169 // The maximum alignment we have seen for this symbol.
170 uint32_t Alignment;
171
172 uint64_t Size;
173};
174
175// Regular defined symbols read from object file symbol tables.
176class DefinedRegular : public Defined {
177public:
178 DefinedRegular(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
179 uint64_t Value, uint64_t Size, SectionBase *Section,
180 InputFile *File)
181 : Defined(SymbolBody::DefinedRegularKind, Name, IsLocal, StOther, Type),
182 Value(Value), Size(Size), Section(Section) {
183 this->File = File;
184 }
185
186 // Return true if the symbol is a PIC function.
187 template <class ELFT> bool isMipsPIC() const;
188
189 static bool classof(const SymbolBody *S) {
190 return S->kind() == SymbolBody::DefinedRegularKind;
191 }
192
193 uint64_t Value;
194 uint64_t Size;
195 SectionBase *Section;
196};
197
198class Undefined : public SymbolBody {
199public:
200 Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, uint8_t Type,
201 InputFile *F);
202
203 static bool classof(const SymbolBody *S) {
204 return S->kind() == UndefinedKind;
205 }
206};
207
208class SharedSymbol : public Defined {
209public:
210 static bool classof(const SymbolBody *S) {
211 return S->kind() == SymbolBody::SharedKind;
212 }
213
214 SharedSymbol(InputFile *File, StringRef Name, uint8_t StOther, uint8_t Type,
215 const void *ElfSym, const void *Verdef)
216 : Defined(SymbolBody::SharedKind, Name, /*IsLocal=*/false, StOther, Type),
217 Verdef(Verdef), ElfSym(ElfSym) {
218 // IFuncs defined in DSOs are treated as functions by the static linker.
219 if (isGnuIFunc())
220 Type = llvm::ELF::STT_FUNC;
Value stored to 'Type' is never read
221 this->File = File;
222 }
223
224 template <class ELFT> uint64_t getShndx() const {
225 return getSym<ELFT>().st_shndx;
226 }
227
228 template <class ELFT> uint64_t getValue() const {
229 return getSym<ELFT>().st_value;
230 }
231
232 template <class ELFT> uint64_t getSize() const {
233 return getSym<ELFT>().st_size;
234 }
235
236 template <class ELFT> uint32_t getAlignment() const;
237
238 // This field is a pointer to the symbol's version definition.
239 const void *Verdef;
240
241 // CopyRelSec and CopyRelSecOff are significant only when NeedsCopy is true.
242 InputSection *CopyRelSec;
243 size_t CopyRelSecOff;
244
245private:
246 template <class ELFT> const typename ELFT::Sym &getSym() const {
247 return *(const typename ELFT::Sym *)ElfSym;
248 }
249
250 const void *ElfSym;
251};
252
253// This class represents a symbol defined in an archive file. It is
254// created from an archive file header, and it knows how to load an
255// object file from an archive to replace itself with a defined
256// symbol. If the resolver finds both Undefined and Lazy for
257// the same name, it will ask the Lazy to load a file.
258class Lazy : public SymbolBody {
259public:
260 static bool classof(const SymbolBody *S) { return S->isLazy(); }
261
262 // Returns an object file for this symbol, or a nullptr if the file
263 // was already returned.
264 InputFile *fetch();
265
266protected:
267 Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
268 : SymbolBody(K, Name, /*IsLocal=*/false, llvm::ELF::STV_DEFAULT, Type) {}
269};
270
271// LazyArchive symbols represents symbols in archive files.
272class LazyArchive : public Lazy {
273public:
274 LazyArchive(ArchiveFile &File, const llvm::object::Archive::Symbol S,
275 uint8_t Type);
276
277 static bool classof(const SymbolBody *S) {
278 return S->kind() == LazyArchiveKind;
279 }
280
281 ArchiveFile *file() { return (ArchiveFile *)this->File; }
282 InputFile *fetch();
283
284private:
285 const llvm::object::Archive::Symbol Sym;
286};
287
288// LazyObject symbols represents symbols in object files between
289// --start-lib and --end-lib options.
290class LazyObject : public Lazy {
291public:
292 LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type);
293
294 static bool classof(const SymbolBody *S) {
295 return S->kind() == LazyObjectKind;
296 }
297
298 LazyObjectFile *file() { return (LazyObjectFile *)this->File; }
299 InputFile *fetch();
300};
301
302// Some linker-generated symbols need to be created as
303// DefinedRegular symbols.
304struct ElfSym {
305 // The content for _etext and etext symbols.
306 static DefinedRegular *Etext;
307 static DefinedRegular *Etext2;
308
309 // The content for _edata and edata symbols.
310 static DefinedRegular *Edata;
311 static DefinedRegular *Edata2;
312
313 // The content for _end and end symbols.
314 static DefinedRegular *End;
315 static DefinedRegular *End2;
316
317 // The content for _gp_disp/__gnu_local_gp symbols for MIPS target.
318 static DefinedRegular *MipsGpDisp;
319 static DefinedRegular *MipsLocalGp;
320 static DefinedRegular *MipsGp;
321};
322
323// A real symbol object, SymbolBody, is usually stored within a Symbol. There's
324// always one Symbol for each symbol name. The resolver updates the SymbolBody
325// stored in the Body field of this object as it resolves symbols. Symbol also
326// holds computed properties of symbol names.
327struct Symbol {
328 // Symbol binding. This is on the Symbol to track changes during resolution.
329 // In particular:
330 // An undefined weak is still weak when it resolves to a shared library.
331 // An undefined weak will not fetch archive members, but we have to remember
332 // it is weak.
333 uint8_t Binding;
334
335 // Version definition index.
336 uint16_t VersionId;
337
338 // Symbol visibility. This is the computed minimum visibility of all
339 // observed non-DSO symbols.
340 unsigned Visibility : 2;
341
342 // True if the symbol was used for linking and thus need to be added to the
343 // output file's symbol table. This is true for all symbols except for
344 // unreferenced DSO symbols and bitcode symbols that are unreferenced except
345 // by other bitcode objects.
346 unsigned IsUsedInRegularObj : 1;
347
348 // If this flag is true and the symbol has protected or default visibility, it
349 // will appear in .dynsym. This flag is set by interposable DSO symbols in
350 // executables, by most symbols in DSOs and executables built with
351 // --export-dynamic, and by dynamic lists.
352 unsigned ExportDynamic : 1;
353
354 // True if this symbol is specified by --trace-symbol option.
355 unsigned Traced : 1;
356
357 // This symbol version was found in a version script.
358 unsigned InVersionScript : 1;
359
360 bool includeInDynsym() const;
361 uint8_t computeBinding() const;
362 bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
363
364 // This field is used to store the Symbol's SymbolBody. This instantiation of
365 // AlignedCharArrayUnion gives us a struct with a char array field that is
366 // large and aligned enough to store any derived class of SymbolBody.
367 llvm::AlignedCharArrayUnion<DefinedCommon, DefinedRegular, Undefined,
368 SharedSymbol, LazyArchive, LazyObject>
369 Body;
370
371 SymbolBody *body() { return reinterpret_cast<SymbolBody *>(Body.buffer); }
372 const SymbolBody *body() const { return const_cast<Symbol *>(this)->body(); }
373};
374
375void printTraceSymbol(Symbol *Sym);
376
377template <typename T, typename... ArgT>
378void replaceBody(Symbol *S, ArgT &&... Arg) {
379 static_assert(sizeof(T) <= sizeof(S->Body), "Body too small");
380 static_assert(alignof(T) <= alignof(decltype(S->Body)),
381 "Body not aligned enough");
382 assert(static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr &&((static_cast<SymbolBody *>(static_cast<T *>(nullptr
)) == nullptr && "Not a SymbolBody") ? static_cast<
void> (0) : __assert_fail ("static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr && \"Not a SymbolBody\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/lld/ELF/Symbols.h"
, 383, __PRETTY_FUNCTION__))
383 "Not a SymbolBody")((static_cast<SymbolBody *>(static_cast<T *>(nullptr
)) == nullptr && "Not a SymbolBody") ? static_cast<
void> (0) : __assert_fail ("static_cast<SymbolBody *>(static_cast<T *>(nullptr)) == nullptr && \"Not a SymbolBody\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/lld/ELF/Symbols.h"
, 383, __PRETTY_FUNCTION__))
;
384
385 new (S->Body.buffer) T(std::forward<ArgT>(Arg)...);
386
387 // Print out a log message if --trace-symbol was specified.
388 // This is for debugging.
389 if (S->Traced)
390 printTraceSymbol(S);
391}
392
393inline Symbol *SymbolBody::symbol() {
394 assert(!isLocal())((!isLocal()) ? static_cast<void> (0) : __assert_fail (
"!isLocal()", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/lld/ELF/Symbols.h"
, 394, __PRETTY_FUNCTION__))
;
395 return reinterpret_cast<Symbol *>(reinterpret_cast<char *>(this) -
396 offsetof(Symbol, Body)__builtin_offsetof(Symbol, Body));
397}
398} // namespace elf
399
400std::string toString(const elf::SymbolBody &B);
401} // namespace lld
402
403#endif