File: | tools/lld/wasm/OutputSections.cpp |
Warning: | line 114, column 3 Value stored to 'Buf' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
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 | |
21 | using namespace llvm; |
22 | using namespace llvm::wasm; |
23 | using namespace lld; |
24 | using namespace lld::wasm; |
25 | |
26 | static 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 | |
57 | std::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 | |
64 | std::string OutputSection::getSectionName() const { |
65 | return sectionTypeToString(Type); |
66 | } |
67 | |
68 | std::string SubSection::getSectionName() const { |
69 | return std::string("subsection <type=") + std::to_string(Type) + ">"; |
70 | } |
71 | |
72 | void 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 | |
82 | CodeSection::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 | |
99 | void 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 | |
122 | uint32_t CodeSection::numRelocations() const { |
123 | uint32_t Count = 0; |
124 | for (const InputChunk *Func : Functions) |
125 | Count += Func->NumRelocations(); |
126 | return Count; |
127 | } |
128 | |
129 | void CodeSection::writeRelocations(raw_ostream &OS) const { |
130 | for (const InputChunk *C : Functions) |
131 | C->writeRelocations(OS); |
132 | } |
133 | |
134 | DataSection::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 | |
162 | void 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 | |
187 | uint32_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 | |
195 | void DataSection::writeRelocations(raw_ostream &OS) const { |
196 | for (const OutputSegment *Seg : Segments) |
197 | for (const InputChunk *C : Seg->InputSegments) |
198 | C->writeRelocations(OS); |
199 | } |