Bug Summary

File:tools/lld/wasm/OutputSections.cpp
Warning:line 114, column 3
Value stored to 'Buf' is never read

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~svn326246/build-llvm/tools/lld/wasm -I /build/llvm-toolchain-snapshot-7~svn326246/tools/lld/wasm -I /build/llvm-toolchain-snapshot-7~svn326246/tools/lld/include -I /build/llvm-toolchain-snapshot-7~svn326246/build-llvm/tools/lld/include -I /build/llvm-toolchain-snapshot-7~svn326246/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn326246/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~svn326246/build-llvm/tools/lld/wasm -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-28-041547-14988-1 -x c++ /build/llvm-toolchain-snapshot-7~svn326246/tools/lld/wasm/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 "InputChunks.h"
12#include "InputFiles.h"
13#include "OutputSegment.h"
14#include "lld/Common/ErrorHandler.h"
15#include "lld/Common/Threads.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/Support/LEB128.h"
18
19#define DEBUG_TYPE"lld" "lld"
20
21using namespace llvm;
22using namespace llvm::wasm;
23using namespace lld;
24using namespace lld::wasm;
25
26static StringRef sectionTypeToString(uint32_t SectionType) {
27 switch (SectionType) {
28 case WASM_SEC_CUSTOM:
29 return "CUSTOM";
30 case WASM_SEC_TYPE:
31 return "TYPE";
32 case WASM_SEC_IMPORT:
33 return "IMPORT";
34 case WASM_SEC_FUNCTION:
35 return "FUNCTION";
36 case WASM_SEC_TABLE:
37 return "TABLE";
38 case WASM_SEC_MEMORY:
39 return "MEMORY";
40 case WASM_SEC_GLOBAL:
41 return "GLOBAL";
42 case WASM_SEC_EXPORT:
43 return "EXPORT";
44 case WASM_SEC_START:
45 return "START";
46 case WASM_SEC_ELEM:
47 return "ELEM";
48 case WASM_SEC_CODE:
49 return "CODE";
50 case WASM_SEC_DATA:
51 return "DATA";
52 default:
53 fatal("invalid section type");
54 }
55}
56
57std::string lld::toString(const OutputSection &Section) {
58 std::string rtn = Section.getSectionName();
59 if (!Section.Name.empty())
60 rtn += "(" + Section.Name + ")";
61 return rtn;
62}
63
64std::string OutputSection::getSectionName() const {
65 return sectionTypeToString(Type);
66}
67
68std::string SubSection::getSectionName() const {
69 return std::string("subsection <type=") + std::to_string(Type) + ">";
70}
71
72void OutputSection::createHeader(size_t BodySize) {
73 raw_string_ostream OS(Header);
74 debugWrite(OS.tell(), "section type [" + Twine(getSectionName()) + "]");
75 encodeULEB128(Type, OS);
76 writeUleb128(OS, BodySize, "section size");
77 OS.flush();
78 log("createHeader: " + toString(*this) + " body=" + Twine(BodySize) +
79 " total=" + Twine(getSize()));
80}
81
82CodeSection::CodeSection(ArrayRef<InputFunction *> Functions)
83 : OutputSection(WASM_SEC_CODE), Functions(Functions) {
84 assert(Functions.size() > 0)(static_cast <bool> (Functions.size() > 0) ? void (0
) : __assert_fail ("Functions.size() > 0", "/build/llvm-toolchain-snapshot-7~svn326246/tools/lld/wasm/OutputSections.cpp"
, 84, __extension__ __PRETTY_FUNCTION__))
;
85
86 raw_string_ostream OS(CodeSectionHeader);
87 writeUleb128(OS, Functions.size(), "function count");
88 OS.flush();
89 BodySize = CodeSectionHeader.size();
90
91 for (InputChunk *Func : Functions) {
92 Func->OutputOffset = BodySize;
93 BodySize += Func->getSize();
94 }
95
96 createHeader(BodySize);
97}
98
99void CodeSection::writeTo(uint8_t *Buf) {
100 log("writing " + toString(*this));
101 log(" size=" + Twine(getSize()));
102 log(" headersize=" + Twine(Header.size()));
103 log(" codeheadersize=" + Twine(CodeSectionHeader.size()));
104 Buf += Offset;
105
106 // Write section header
107 memcpy(Buf, Header.data(), Header.size());
108 Buf += Header.size();
109
110 uint8_t *ContentsStart = Buf;
111
112 // Write code section headers
113 memcpy(Buf, CodeSectionHeader.data(), CodeSectionHeader.size());
114 Buf += CodeSectionHeader.size();
Value stored to 'Buf' is never read
115
116 // Write code section bodies
117 parallelForEach(Functions, [ContentsStart](const InputChunk *Chunk) {
118 Chunk->writeTo(ContentsStart);
119 });
120}
121
122uint32_t CodeSection::numRelocations() const {
123 uint32_t Count = 0;
124 for (const InputChunk *Func : Functions)
125 Count += Func->NumRelocations();
126 return Count;
127}
128
129void CodeSection::writeRelocations(raw_ostream &OS) const {
130 for (const InputChunk *C : Functions)
131 C->writeRelocations(OS);
132}
133
134DataSection::DataSection(ArrayRef<OutputSegment *> Segments)
135 : OutputSection(WASM_SEC_DATA), Segments(Segments) {
136 raw_string_ostream OS(DataSectionHeader);
137
138 writeUleb128(OS, Segments.size(), "data segment count");
139 OS.flush();
140 BodySize = DataSectionHeader.size();
141
142 for (OutputSegment *Segment : Segments) {
143 raw_string_ostream OS(Segment->Header);
144 writeUleb128(OS, 0, "memory index");
145 writeUleb128(OS, WASM_OPCODE_I32_CONST, "opcode:i32const");
146 writeSleb128(OS, Segment->StartVA, "memory offset");
147 writeUleb128(OS, WASM_OPCODE_END, "opcode:end");
148 writeUleb128(OS, Segment->Size, "segment size");
149 OS.flush();
150 Segment->setSectionOffset(BodySize);
151 BodySize += Segment->Header.size() + Segment->Size;
152 log("Data segment: size=" + Twine(Segment->Size));
153 for (InputSegment *InputSeg : Segment->InputSegments)
154 InputSeg->OutputOffset = Segment->getSectionOffset() +
155 Segment->Header.size() +
156 InputSeg->OutputSegmentOffset;
157 }
158
159 createHeader(BodySize);
160}
161
162void DataSection::writeTo(uint8_t *Buf) {
163 log("writing " + toString(*this) + " size=" + Twine(getSize()) +
164 " body=" + Twine(BodySize));
165 Buf += Offset;
166
167 // Write section header
168 memcpy(Buf, Header.data(), Header.size());
169 Buf += Header.size();
170
171 uint8_t *ContentsStart = Buf;
172
173 // Write data section headers
174 memcpy(Buf, DataSectionHeader.data(), DataSectionHeader.size());
175
176 parallelForEach(Segments, [ContentsStart](const OutputSegment *Segment) {
177 // Write data segment header
178 uint8_t *SegStart = ContentsStart + Segment->getSectionOffset();
179 memcpy(SegStart, Segment->Header.data(), Segment->Header.size());
180
181 // Write segment data payload
182 for (const InputChunk *Chunk : Segment->InputSegments)
183 Chunk->writeTo(ContentsStart);
184 });
185}
186
187uint32_t DataSection::numRelocations() const {
188 uint32_t Count = 0;
189 for (const OutputSegment *Seg : Segments)
190 for (const InputChunk *InputSeg : Seg->InputSegments)
191 Count += InputSeg->NumRelocations();
192 return Count;
193}
194
195void DataSection::writeRelocations(raw_ostream &OS) const {
196 for (const OutputSegment *Seg : Segments)
197 for (const InputChunk *C : Seg->InputSegments)
198 C->writeRelocations(OS);
199}