Bug Summary

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