Bug Summary

File:tools/lld/ELF/OutputSections.cpp
Warning:line 301, column 19
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name OutputSections.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/tools/lld/ELF -I /build/llvm-toolchain-snapshot-7~svn325118/tools/lld/ELF -I /build/llvm-toolchain-snapshot-7~svn325118/tools/lld/include -I /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/tools/lld/include -I /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn325118/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/tools/lld/ELF -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-02-14-150435-17243-1 -x c++ /build/llvm-toolchain-snapshot-7~svn325118/tools/lld/ELF/OutputSections.cpp
1//===- OutputSections.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 "OutputSections.h"
11#include "Config.h"
12#include "LinkerScript.h"
13#include "Strings.h"
14#include "SymbolTable.h"
15#include "SyntheticSections.h"
16#include "Target.h"
17#include "lld/Common/Memory.h"
18#include "lld/Common/Threads.h"
19#include "llvm/BinaryFormat/Dwarf.h"
20#include "llvm/Support/Compression.h"
21#include "llvm/Support/MD5.h"
22#include "llvm/Support/MathExtras.h"
23#include "llvm/Support/SHA1.h"
24
25using namespace llvm;
26using namespace llvm::dwarf;
27using namespace llvm::object;
28using namespace llvm::support::endian;
29using namespace llvm::ELF;
30
31using namespace lld;
32using namespace lld::elf;
33
34uint8_t Out::First;
35OutputSection *Out::Opd;
36uint8_t *Out::OpdBuf;
37PhdrEntry *Out::TlsPhdr;
38OutputSection *Out::DebugInfo;
39OutputSection *Out::ElfHeader;
40OutputSection *Out::ProgramHeaders;
41OutputSection *Out::PreinitArray;
42OutputSection *Out::InitArray;
43OutputSection *Out::FiniArray;
44
45std::vector<OutputSection *> elf::OutputSections;
46
47uint32_t OutputSection::getPhdrFlags() const {
48 uint32_t Ret = PF_R;
49 if (Flags & SHF_WRITE)
50 Ret |= PF_W;
51 if (Flags & SHF_EXECINSTR)
52 Ret |= PF_X;
53 return Ret;
54}
55
56template <class ELFT>
57void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
58 Shdr->sh_entsize = Entsize;
59 Shdr->sh_addralign = Alignment;
60 Shdr->sh_type = Type;
61 Shdr->sh_offset = Offset;
62 Shdr->sh_flags = Flags;
63 Shdr->sh_info = Info;
64 Shdr->sh_link = Link;
65 Shdr->sh_addr = Addr;
66 Shdr->sh_size = Size;
67 Shdr->sh_name = ShName;
68}
69
70OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags)
71 : BaseCommand(OutputSectionKind),
72 SectionBase(Output, Name, Flags, /*Entsize*/ 0, /*Alignment*/ 1, Type,
73 /*Info*/ 0,
74 /*Link*/ 0),
75 SectionIndex(INT_MAX2147483647) {
76 Live = false;
77}
78
79// We allow sections of types listed below to merged into a
80// single progbits section. This is typically done by linker
81// scripts. Merging nobits and progbits will force disk space
82// to be allocated for nobits sections. Other ones don't require
83// any special treatment on top of progbits, so there doesn't
84// seem to be a harm in merging them.
85static bool canMergeToProgbits(unsigned Type) {
86 return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY ||
87 Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY ||
88 Type == SHT_NOTE;
89}
90
91void OutputSection::addSection(InputSection *IS) {
92 if (!Live) {
93 // If IS is the first section to be added to this section,
94 // initialize Type and Entsize from IS.
95 Live = true;
96 Type = IS->Type;
97 Entsize = IS->Entsize;
98 } else {
99 // Otherwise, check if new type or flags are compatible with existing ones.
100 if ((Flags & (SHF_ALLOC | SHF_TLS)) != (IS->Flags & (SHF_ALLOC | SHF_TLS)))
101 error("incompatible section flags for " + Name + "\n>>> " + toString(IS) +
102 ": 0x" + utohexstr(IS->Flags) + "\n>>> output section " + Name +
103 ": 0x" + utohexstr(Flags));
104
105 if (Type != IS->Type) {
106 if (!canMergeToProgbits(Type) || !canMergeToProgbits(IS->Type))
107 error("section type mismatch for " + IS->Name + "\n>>> " +
108 toString(IS) + ": " +
109 getELFSectionTypeName(Config->EMachine, IS->Type) +
110 "\n>>> output section " + Name + ": " +
111 getELFSectionTypeName(Config->EMachine, Type));
112 Type = SHT_PROGBITS;
113 }
114 }
115
116 IS->Parent = this;
117 Flags |= IS->Flags;
118 Alignment = std::max(Alignment, IS->Alignment);
119 IS->OutSecOff = Size++;
120
121 // If this section contains a table of fixed-size entries, sh_entsize
122 // holds the element size. If it contains elements of different size we
123 // set sh_entsize to 0.
124 if (Entsize != IS->Entsize)
125 Entsize = 0;
126
127 if (!IS->Assigned) {
128 IS->Assigned = true;
129 if (SectionCommands.empty() ||
130 !isa<InputSectionDescription>(SectionCommands.back()))
131 SectionCommands.push_back(make<InputSectionDescription>(""));
132 auto *ISD = cast<InputSectionDescription>(SectionCommands.back());
133 ISD->Sections.push_back(IS);
134 }
135}
136
137static void sortByOrder(MutableArrayRef<InputSection *> In,
138 std::function<int(InputSectionBase *S)> Order) {
139 typedef std::pair<int, InputSection *> Pair;
140 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
141
142 std::vector<Pair> V;
143 for (InputSection *S : In)
144 V.push_back({Order(S), S});
145 std::stable_sort(V.begin(), V.end(), Comp);
146
147 for (size_t I = 0; I < V.size(); ++I)
148 In[I] = V[I].second;
149}
150
151uint64_t elf::getHeaderSize() {
152 if (Config->OFormatBinary)
153 return 0;
154 return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
155}
156
157bool OutputSection::classof(const BaseCommand *C) {
158 return C->Kind == OutputSectionKind;
159}
160
161void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) {
162 assert(Live)(static_cast <bool> (Live) ? void (0) : __assert_fail (
"Live", "/build/llvm-toolchain-snapshot-7~svn325118/tools/lld/ELF/OutputSections.cpp"
, 162, __extension__ __PRETTY_FUNCTION__))
;
163 for (BaseCommand *B : SectionCommands)
164 if (auto *ISD = dyn_cast<InputSectionDescription>(B))
165 sortByOrder(ISD->Sections, Order);
166}
167
168// Fill [Buf, Buf + Size) with Filler.
169// This is used for linker script "=fillexp" command.
170static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
171 size_t I = 0;
172 for (; I + 4 < Size; I += 4)
173 memcpy(Buf + I, &Filler, 4);
174 memcpy(Buf + I, &Filler, Size - I);
175}
176
177// Compress section contents if this section contains debug info.
178template <class ELFT> void OutputSection::maybeCompress() {
179 typedef typename ELFT::Chdr Elf_Chdr;
180
181 // Compress only DWARF debug sections.
182 if (!Config->CompressDebugSections || (Flags & SHF_ALLOC) ||
183 !Name.startswith(".debug_"))
184 return;
185
186 // Create a section header.
187 ZDebugHeader.resize(sizeof(Elf_Chdr));
188 auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
189 Hdr->ch_type = ELFCOMPRESS_ZLIB;
190 Hdr->ch_size = Size;
191 Hdr->ch_addralign = Alignment;
192
193 // Write section contents to a temporary buffer and compress it.
194 std::vector<uint8_t> Buf(Size);
195 writeTo<ELFT>(Buf.data());
196 if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
197 fatal("compress failed: " + llvm::toString(std::move(E)));
198
199 // Update section headers.
200 Size = sizeof(Elf_Chdr) + CompressedData.size();
201 Flags |= SHF_COMPRESSED;
202}
203
204static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
205 if (Size == 1)
206 *Buf = Data;
207 else if (Size == 2)
208 write16(Buf, Data, Config->Endianness);
209 else if (Size == 4)
210 write32(Buf, Data, Config->Endianness);
211 else if (Size == 8)
212 write64(Buf, Data, Config->Endianness);
213 else
214 llvm_unreachable("unsupported Size argument")::llvm::llvm_unreachable_internal("unsupported Size argument"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/lld/ELF/OutputSections.cpp"
, 214)
;
215}
216
217template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
218 if (Type == SHT_NOBITS)
219 return;
220
221 Loc = Buf;
222
223 // If -compress-debug-section is specified and if this is a debug seciton,
224 // we've already compressed section contents. If that's the case,
225 // just write it down.
226 if (!CompressedData.empty()) {
227 memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
228 memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
229 CompressedData.size());
230 return;
231 }
232
233 // Write leading padding.
234 std::vector<InputSection *> Sections;
235 for (BaseCommand *Cmd : SectionCommands)
236 if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd))
237 for (InputSection *IS : ISD->Sections)
238 if (IS->Live)
239 Sections.push_back(IS);
240 uint32_t Filler = getFiller();
241 if (Filler)
242 fill(Buf, Sections.empty() ? Size : Sections[0]->OutSecOff, Filler);
243
244 parallelForEachN(0, Sections.size(), [&](size_t I) {
245 InputSection *IS = Sections[I];
246 IS->writeTo<ELFT>(Buf);
247
248 // Fill gaps between sections.
249 if (Filler) {
250 uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
251 uint8_t *End;
252 if (I + 1 == Sections.size())
253 End = Buf + Size;
254 else
255 End = Buf + Sections[I + 1]->OutSecOff;
256 fill(Start, End - Start, Filler);
257 }
258 });
259
260 // Linker scripts may have BYTE()-family commands with which you
261 // can write arbitrary bytes to the output. Process them if any.
262 for (BaseCommand *Base : SectionCommands)
263 if (auto *Data = dyn_cast<ByteCommand>(Base))
264 writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
265}
266
267template <class ELFT>
268static void finalizeShtGroup(OutputSection *OS,
269 InputSection *Section) {
270 assert(Config->Relocatable)(static_cast <bool> (Config->Relocatable) ? void (0)
: __assert_fail ("Config->Relocatable", "/build/llvm-toolchain-snapshot-7~svn325118/tools/lld/ELF/OutputSections.cpp"
, 270, __extension__ __PRETTY_FUNCTION__))
;
271
272 // sh_link field for SHT_GROUP sections should contain the section index of
273 // the symbol table.
274 OS->Link = InX::SymTab->getParent()->SectionIndex;
275
276 // sh_info then contain index of an entry in symbol table section which
277 // provides signature of the section group.
278 ObjFile<ELFT> *Obj = Section->getFile<ELFT>();
279 ArrayRef<Symbol *> Symbols = Obj->getSymbols();
280 OS->Info = InX::SymTab->getSymbolIndex(Symbols[Section->Info]);
281}
282
283template <class ELFT> void OutputSection::finalize() {
284 InputSection *First = nullptr;
1
'First' initialized to a null pointer value
285 for (BaseCommand *Base : SectionCommands) {
286 if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) {
287 if (ISD->Sections.empty())
288 continue;
289 if (First == nullptr)
290 First = ISD->Sections.front();
291 }
292 if (isa<ByteCommand>(Base) && Type == SHT_NOBITS)
293 Type = SHT_PROGBITS;
294 }
295
296 if (Flags & SHF_LINK_ORDER) {
2
Assuming the condition is true
3
Taking true branch
297 // We must preserve the link order dependency of sections with the
298 // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
299 // need to translate the InputSection sh_link to the OutputSection sh_link,
300 // all InputSections in the OutputSection have the same dependency.
301 if (auto *D = First->getLinkOrderDep())
4
Called C++ object pointer is null
302 Link = D->getParent()->SectionIndex;
303 }
304
305 if (Type == SHT_GROUP) {
306 finalizeShtGroup<ELFT>(this, First);
307 return;
308 }
309
310 if (!Config->CopyRelocs || (Type != SHT_RELA && Type != SHT_REL))
311 return;
312
313 if (isa<SyntheticSection>(First))
314 return;
315
316 Link = InX::SymTab->getParent()->SectionIndex;
317 // sh_info for SHT_REL[A] sections should contain the section header index of
318 // the section to which the relocation applies.
319 InputSectionBase *S = First->getRelocatedSection();
320 Info = S->getOutputSection()->SectionIndex;
321 Flags |= SHF_INFO_LINK;
322}
323
324// Returns true if S matches /Filename.?\.o$/.
325static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
326 if (!S.endswith(".o"))
327 return false;
328 S = S.drop_back(2);
329 if (S.endswith(Filename))
330 return true;
331 return !S.empty() && S.drop_back().endswith(Filename);
332}
333
334static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
335static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
336
337// .ctors and .dtors are sorted by this priority from highest to lowest.
338//
339// 1. The section was contained in crtbegin (crtbegin contains
340// some sentinel value in its .ctors and .dtors so that the runtime
341// can find the beginning of the sections.)
342//
343// 2. The section has an optional priority value in the form of ".ctors.N"
344// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
345// they are compared as string rather than number.
346//
347// 3. The section is just ".ctors" or ".dtors".
348//
349// 4. The section was contained in crtend, which contains an end marker.
350//
351// In an ideal world, we don't need this function because .init_array and
352// .ctors are duplicate features (and .init_array is newer.) However, there
353// are too many real-world use cases of .ctors, so we had no choice to
354// support that with this rather ad-hoc semantics.
355static bool compCtors(const InputSection *A, const InputSection *B) {
356 bool BeginA = isCrtbegin(A->File->getName());
357 bool BeginB = isCrtbegin(B->File->getName());
358 if (BeginA != BeginB)
359 return BeginA;
360 bool EndA = isCrtend(A->File->getName());
361 bool EndB = isCrtend(B->File->getName());
362 if (EndA != EndB)
363 return EndB;
364 StringRef X = A->Name;
365 StringRef Y = B->Name;
366 assert(X.startswith(".ctors") || X.startswith(".dtors"))(static_cast <bool> (X.startswith(".ctors") || X.startswith
(".dtors")) ? void (0) : __assert_fail ("X.startswith(\".ctors\") || X.startswith(\".dtors\")"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/lld/ELF/OutputSections.cpp"
, 366, __extension__ __PRETTY_FUNCTION__))
;
367 assert(Y.startswith(".ctors") || Y.startswith(".dtors"))(static_cast <bool> (Y.startswith(".ctors") || Y.startswith
(".dtors")) ? void (0) : __assert_fail ("Y.startswith(\".ctors\") || Y.startswith(\".dtors\")"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/lld/ELF/OutputSections.cpp"
, 367, __extension__ __PRETTY_FUNCTION__))
;
368 X = X.substr(6);
369 Y = Y.substr(6);
370 if (X.empty() && Y.empty())
371 return false;
372 return X < Y;
373}
374
375// Sorts input sections by the special rules for .ctors and .dtors.
376// Unfortunately, the rules are different from the one for .{init,fini}_array.
377// Read the comment above.
378void OutputSection::sortCtorsDtors() {
379 assert(SectionCommands.size() == 1)(static_cast <bool> (SectionCommands.size() == 1) ? void
(0) : __assert_fail ("SectionCommands.size() == 1", "/build/llvm-toolchain-snapshot-7~svn325118/tools/lld/ELF/OutputSections.cpp"
, 379, __extension__ __PRETTY_FUNCTION__))
;
380 auto *ISD = cast<InputSectionDescription>(SectionCommands[0]);
381 std::stable_sort(ISD->Sections.begin(), ISD->Sections.end(), compCtors);
382}
383
384// If an input string is in the form of "foo.N" where N is a number,
385// return N. Otherwise, returns 65536, which is one greater than the
386// lowest priority.
387int elf::getPriority(StringRef S) {
388 size_t Pos = S.rfind('.');
389 if (Pos == StringRef::npos)
390 return 65536;
391 int V;
392 if (!to_integer(S.substr(Pos + 1), V, 10))
393 return 65536;
394 return V;
395}
396
397// Sorts input sections by section name suffixes, so that .foo.N comes
398// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
399// We want to keep the original order if the priorities are the same
400// because the compiler keeps the original initialization order in a
401// translation unit and we need to respect that.
402// For more detail, read the section of the GCC's manual about init_priority.
403void OutputSection::sortInitFini() {
404 // Sort sections by priority.
405 sort([](InputSectionBase *S) { return getPriority(S->Name); });
406}
407
408uint32_t OutputSection::getFiller() {
409 if (Filler)
410 return *Filler;
411 if (Flags & SHF_EXECINSTR)
412 return Target->TrapInstr;
413 return 0;
414}
415
416template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
417template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
418template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
419template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
420
421template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf);
422template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf);
423template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf);
424template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf);
425
426template void OutputSection::maybeCompress<ELF32LE>();
427template void OutputSection::maybeCompress<ELF32BE>();
428template void OutputSection::maybeCompress<ELF64LE>();
429template void OutputSection::maybeCompress<ELF64BE>();
430
431template void OutputSection::finalize<ELF32LE>();
432template void OutputSection::finalize<ELF32BE>();
433template void OutputSection::finalize<ELF64LE>();
434template void OutputSection::finalize<ELF64BE>();