Bug Summary

File:tools/lld/ELF/Symbols.cpp
Warning:line 299, column 35
The result of the '<<' expression is undefined

Annotated Source Code

1//===- Symbols.cpp --------------------------------------------------------===//
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#include "Symbols.h"
11#include "Error.h"
12#include "InputFiles.h"
13#include "InputSection.h"
14#include "OutputSections.h"
15#include "Strings.h"
16#include "SyntheticSections.h"
17#include "Target.h"
18#include "Writer.h"
19
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/Support/Path.h"
22#include <cstring>
23
24using namespace llvm;
25using namespace llvm::object;
26using namespace llvm::ELF;
27
28using namespace lld;
29using namespace lld::elf;
30
31DefinedRegular *ElfSym::Bss;
32DefinedRegular *ElfSym::Etext1;
33DefinedRegular *ElfSym::Etext2;
34DefinedRegular *ElfSym::Edata1;
35DefinedRegular *ElfSym::Edata2;
36DefinedRegular *ElfSym::End1;
37DefinedRegular *ElfSym::End2;
38DefinedRegular *ElfSym::MipsGp;
39DefinedRegular *ElfSym::MipsGpDisp;
40DefinedRegular *ElfSym::MipsLocalGp;
41
42static uint64_t getSymVA(const SymbolBody &Body, int64_t &Addend) {
43 switch (Body.kind()) {
44 case SymbolBody::DefinedRegularKind: {
45 auto &D = cast<DefinedRegular>(Body);
46 SectionBase *IS = D.Section;
47 if (auto *ISB = dyn_cast_or_null<InputSectionBase>(IS))
48 IS = ISB->Repl;
49
50 // According to the ELF spec reference to a local symbol from outside
51 // the group are not allowed. Unfortunately .eh_frame breaks that rule
52 // and must be treated specially. For now we just replace the symbol with
53 // 0.
54 if (IS == &InputSection::Discarded)
55 return 0;
56
57 // This is an absolute symbol.
58 if (!IS)
59 return D.Value;
60
61 uint64_t Offset = D.Value;
62
63 // An object in an SHF_MERGE section might be referenced via a
64 // section symbol (as a hack for reducing the number of local
65 // symbols).
66 // Depending on the addend, the reference via a section symbol
67 // refers to a different object in the merge section.
68 // Since the objects in the merge section are not necessarily
69 // contiguous in the output, the addend can thus affect the final
70 // VA in a non-linear way.
71 // To make this work, we incorporate the addend into the section
72 // offset (and zero out the addend for later processing) so that
73 // we find the right object in the section.
74 if (D.isSection()) {
75 Offset += Addend;
76 Addend = 0;
77 }
78
79 const OutputSection *OutSec = IS->getOutputSection();
80
81 // In the typical case, this is actually very simple and boils
82 // down to adding together 3 numbers:
83 // 1. The address of the output section.
84 // 2. The offset of the input section within the output section.
85 // 3. The offset within the input section (this addition happens
86 // inside InputSection::getOffset).
87 //
88 // If you understand the data structures involved with this next
89 // line (and how they get built), then you have a pretty good
90 // understanding of the linker.
91 uint64_t VA = (OutSec ? OutSec->Addr : 0) + IS->getOffset(Offset);
92
93 if (D.isTls() && !Config->Relocatable) {
94 if (!Out::TlsPhdr)
95 fatal(toString(D.File) +
96 " has a STT_TLS symbol but doesn't have a PT_TLS section");
97 return VA - Out::TlsPhdr->p_vaddr;
98 }
99 return VA;
100 }
101 case SymbolBody::DefinedCommonKind:
102 if (!Config->DefineCommon)
103 return 0;
104 return InX::Common->OutSec->Addr + InX::Common->OutSecOff +
105 cast<DefinedCommon>(Body).Offset;
106 case SymbolBody::SharedKind: {
107 auto &SS = cast<SharedSymbol>(Body);
108 if (SS.NeedsCopy)
109 return SS.CopyRelSec->OutSec->Addr + SS.CopyRelSec->OutSecOff +
110 SS.CopyRelSecOff;
111 if (SS.NeedsPltAddr)
112 return Body.getPltVA();
113 return 0;
114 }
115 case SymbolBody::UndefinedKind:
116 return 0;
117 case SymbolBody::LazyArchiveKind:
118 case SymbolBody::LazyObjectKind:
119 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer")((Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer"
) ? static_cast<void> (0) : __assert_fail ("Body.symbol()->IsUsedInRegularObj && \"lazy symbol reached writer\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/tools/lld/ELF/Symbols.cpp"
, 119, __PRETTY_FUNCTION__))
;
120 return 0;
121 }
122 llvm_unreachable("invalid symbol kind")::llvm::llvm_unreachable_internal("invalid symbol kind", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/tools/lld/ELF/Symbols.cpp"
, 122)
;
123}
124
125SymbolBody::SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
126 uint8_t Type)
127 : SymbolKind(K), NeedsCopy(false), NeedsPltAddr(false), IsLocal(IsLocal),
128 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
129 IsInIgot(false), Type(Type), StOther(StOther), Name(Name) {}
130
131// Returns true if a symbol can be replaced at load-time by a symbol
132// with the same name defined in other ELF executable or DSO.
133bool SymbolBody::isPreemptible() const {
134 if (isLocal())
135 return false;
136
137 // Shared symbols resolve to the definition in the DSO. The exceptions are
138 // symbols with copy relocations (which resolve to .bss) or preempt plt
139 // entries (which resolve to that plt entry).
140 if (isShared())
141 return !NeedsCopy && !NeedsPltAddr;
142
143 // That's all that can be preempted in a non-DSO.
144 if (!Config->Shared)
145 return false;
146
147 // Only symbols that appear in dynsym can be preempted.
148 if (!symbol()->includeInDynsym())
149 return false;
150
151 // Only default visibility symbols can be preempted.
152 if (symbol()->Visibility != STV_DEFAULT)
153 return false;
154
155 // -Bsymbolic means that definitions are not preempted.
156 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
157 return !isDefined();
158 return true;
159}
160
161uint64_t SymbolBody::getVA(int64_t Addend) const {
162 uint64_t OutVA = getSymVA(*this, Addend);
163 return OutVA + Addend;
164}
165
166uint64_t SymbolBody::getGotVA() const {
167 return InX::Got->getVA() + getGotOffset();
168}
169
170uint64_t SymbolBody::getGotOffset() const {
171 return GotIndex * Target->GotEntrySize;
172}
173
174uint64_t SymbolBody::getGotPltVA() const {
175 if (this->IsInIgot)
176 return InX::IgotPlt->getVA() + getGotPltOffset();
177 return InX::GotPlt->getVA() + getGotPltOffset();
178}
179
180uint64_t SymbolBody::getGotPltOffset() const {
181 return GotPltIndex * Target->GotPltEntrySize;
182}
183
184uint64_t SymbolBody::getPltVA() const {
185 if (this->IsInIplt)
186 return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
187 return InX::Plt->getVA() + Target->PltHeaderSize +
188 PltIndex * Target->PltEntrySize;
189}
190
191template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
192 if (const auto *C = dyn_cast<DefinedCommon>(this))
193 return C->Size;
194 if (const auto *DR = dyn_cast<DefinedRegular>(this))
195 return DR->Size;
196 if (const auto *S = dyn_cast<SharedSymbol>(this))
197 return S->getSize<ELFT>();
198 return 0;
199}
200
201OutputSection *SymbolBody::getOutputSection() const {
202 if (auto *S = dyn_cast<DefinedRegular>(this)) {
203 if (S->Section)
204 return S->Section->getOutputSection();
205 return nullptr;
206 }
207
208 if (auto *S = dyn_cast<SharedSymbol>(this)) {
209 if (S->NeedsCopy)
210 return S->CopyRelSec->OutSec;
211 return nullptr;
212 }
213
214 if (isa<DefinedCommon>(this)) {
215 if (Config->DefineCommon)
216 return InX::Common->OutSec;
217 return nullptr;
218 }
219
220 return nullptr;
221}
222
223// If a symbol name contains '@', the characters after that is
224// a symbol version name. This function parses that.
225void SymbolBody::parseSymbolVersion() {
226 StringRef S = getName();
227 size_t Pos = S.find('@');
228 if (Pos == 0 || Pos == StringRef::npos)
229 return;
230 StringRef Verstr = S.substr(Pos + 1);
231 if (Verstr.empty())
232 return;
233
234 // Truncate the symbol name so that it doesn't include the version string.
235 Name = {S.data(), Pos};
236
237 // If this is not in this DSO, it is not a definition.
238 if (!isInCurrentDSO())
239 return;
240
241 // '@@' in a symbol name means the default version.
242 // It is usually the most recent one.
243 bool IsDefault = (Verstr[0] == '@');
244 if (IsDefault)
245 Verstr = Verstr.substr(1);
246
247 for (VersionDefinition &Ver : Config->VersionDefinitions) {
248 if (Ver.Name != Verstr)
249 continue;
250
251 if (IsDefault)
252 symbol()->VersionId = Ver.Id;
253 else
254 symbol()->VersionId = Ver.Id | VERSYM_HIDDEN;
255 return;
256 }
257
258 // It is an error if the specified version is not defined.
259 error(toString(File) + ": symbol " + S + " has undefined version " + Verstr);
260}
261
262Defined::Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
263 uint8_t Type)
264 : SymbolBody(K, Name, IsLocal, StOther, Type) {}
265
266template <class ELFT> bool DefinedRegular::isMipsPIC() const {
267 if (!Section || !isFunc())
268 return false;
269 return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
270 (cast<InputSectionBase>(Section)
271 ->template getFile<ELFT>()
272 ->getObj()
273 .getHeader()
274 ->e_flags &
275 EF_MIPS_PIC);
276}
277
278Undefined::Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther,
279 uint8_t Type, InputFile *File)
280 : SymbolBody(SymbolBody::UndefinedKind, Name, IsLocal, StOther, Type) {
281 this->File = File;
282}
283
284DefinedCommon::DefinedCommon(StringRef Name, uint64_t Size, uint32_t Alignment,
285 uint8_t StOther, uint8_t Type, InputFile *File)
286 : Defined(SymbolBody::DefinedCommonKind, Name, /*IsLocal=*/false, StOther,
287 Type),
288 Alignment(Alignment), Size(Size) {
289 this->File = File;
290}
291
292// If a shared symbol is referred via a copy relocation, its alignment
293// becomes part of the ABI. This function returns a symbol alignment.
294// Because symbols don't have alignment attributes, we need to infer that.
295template <class ELFT> uint32_t SharedSymbol::getAlignment() const {
296 auto *File = cast<SharedFile<ELFT>>(this->File);
297 uint32_t SecAlign = File->getSection(getSym<ELFT>())->sh_addralign;
298 uint64_t SymValue = getSym<ELFT>().st_value;
299 uint32_t SymAlign = uint32_t(1) << countTrailingZeros(SymValue);
The result of the '<<' expression is undefined
300 return std::min(SecAlign, SymAlign);
301}
302
303InputFile *Lazy::fetch() {
304 if (auto *S = dyn_cast<LazyArchive>(this))
305 return S->fetch();
306 return cast<LazyObject>(this)->fetch();
307}
308
309LazyArchive::LazyArchive(ArchiveFile &File,
310 const llvm::object::Archive::Symbol S, uint8_t Type)
311 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
312 this->File = &File;
313}
314
315LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
316 : Lazy(LazyObjectKind, Name, Type) {
317 this->File = &File;
318}
319
320InputFile *LazyArchive::fetch() {
321 std::pair<MemoryBufferRef, uint64_t> MBInfo = file()->getMember(&Sym);
322
323 // getMember returns an empty buffer if the member was already
324 // read from the library.
325 if (MBInfo.first.getBuffer().empty())
326 return nullptr;
327 return createObjectFile(MBInfo.first, file()->getName(), MBInfo.second);
328}
329
330InputFile *LazyObject::fetch() { return file()->fetch(); }
331
332uint8_t Symbol::computeBinding() const {
333 if (Config->Relocatable)
334 return Binding;
335 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
336 return STB_LOCAL;
337 if (VersionId == VER_NDX_LOCAL && body()->isInCurrentDSO())
338 return STB_LOCAL;
339 if (Config->NoGnuUnique && Binding == STB_GNU_UNIQUE)
340 return STB_GLOBAL;
341 return Binding;
342}
343
344bool Symbol::includeInDynsym() const {
345 if (computeBinding() == STB_LOCAL)
346 return false;
347 return ExportDynamic || body()->isShared() ||
348 (body()->isUndefined() && Config->Shared);
349}
350
351// Print out a log message for --trace-symbol.
352void elf::printTraceSymbol(Symbol *Sym) {
353 SymbolBody *B = Sym->body();
354 std::string S;
355 if (B->isUndefined())
356 S = ": reference to ";
357 else if (B->isCommon())
358 S = ": common definition of ";
359 else
360 S = ": definition of ";
361
362 message(toString(B->File) + S + B->getName());
363}
364
365// Returns a symbol for an error message.
366std::string lld::toString(const SymbolBody &B) {
367 if (Config->Demangle)
368 if (Optional<std::string> S = demangle(B.getName()))
369 return *S;
370 return B.getName();
371}
372
373template uint32_t SymbolBody::template getSize<ELF32LE>() const;
374template uint32_t SymbolBody::template getSize<ELF32BE>() const;
375template uint64_t SymbolBody::template getSize<ELF64LE>() const;
376template uint64_t SymbolBody::template getSize<ELF64BE>() const;
377
378template bool DefinedRegular::template isMipsPIC<ELF32LE>() const;
379template bool DefinedRegular::template isMipsPIC<ELF32BE>() const;
380template bool DefinedRegular::template isMipsPIC<ELF64LE>() const;
381template bool DefinedRegular::template isMipsPIC<ELF64BE>() const;
382
383template uint32_t SharedSymbol::template getAlignment<ELF32LE>() const;
384template uint32_t SharedSymbol::template getAlignment<ELF32BE>() const;
385template uint32_t SharedSymbol::template getAlignment<ELF64LE>() const;
386template uint32_t SharedSymbol::template getAlignment<ELF64BE>() const;