forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathOutputSections.cpp
287 lines (240 loc) · 8.56 KB
/
OutputSections.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//===- OutputSections.cpp -------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "OutputSections.h"
#include "InputChunks.h"
#include "InputElement.h"
#include "InputFiles.h"
#include "OutputSegment.h"
#include "WriterUtils.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Memory.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/Parallel.h"
#define DEBUG_TYPE "lld"
using namespace llvm;
using namespace llvm::wasm;
namespace lld {
// Returns a string, e.g. "FUNCTION(.text)".
std::string toString(const wasm::OutputSection &sec) {
if (!sec.name.empty())
return (sec.getSectionName() + "(" + sec.name + ")").str();
return std::string(sec.getSectionName());
}
namespace wasm {
StringRef OutputSection::getSectionName() const {
return sectionTypeToString(type);
}
void OutputSection::createHeader(size_t bodySize) {
raw_string_ostream os(header);
debugWrite(os.tell(), "section type [" + getSectionName() + "]");
encodeULEB128(type, os);
writeUleb128(os, bodySize, "section size");
log("createHeader: " + toString(*this) + " body=" + Twine(bodySize) +
" total=" + Twine(getSize()));
}
void CodeSection::finalizeContents() {
raw_string_ostream os(codeSectionHeader);
writeUleb128(os, functions.size(), "function count");
bodySize = codeSectionHeader.size();
for (InputFunction *func : functions) {
func->outputSec = this;
func->outSecOff = bodySize;
func->calculateSize();
// All functions should have a non-empty body at this point
assert(func->getSize());
bodySize += func->getSize();
}
createHeader(bodySize);
}
void CodeSection::writeTo(uint8_t *buf) {
log("writing " + toString(*this) + " offset=" + Twine(offset) +
" size=" + Twine(getSize()));
log(" headersize=" + Twine(header.size()));
log(" codeheadersize=" + Twine(codeSectionHeader.size()));
buf += offset;
// Write section header
memcpy(buf, header.data(), header.size());
buf += header.size();
// Write code section headers
memcpy(buf, codeSectionHeader.data(), codeSectionHeader.size());
// Write code section bodies
for (const InputChunk *chunk : functions)
chunk->writeTo(buf);
}
uint32_t CodeSection::getNumRelocations() const {
uint32_t count = 0;
for (const InputChunk *func : functions)
count += func->getNumRelocations();
return count;
}
void CodeSection::writeRelocations(raw_ostream &os) const {
for (const InputChunk *c : functions)
c->writeRelocations(os);
}
void DataSection::finalizeContents() {
raw_string_ostream os(dataSectionHeader);
unsigned segmentCount = llvm::count_if(segments, [](OutputSegment *segment) {
return segment->requiredInBinary();
});
#ifndef NDEBUG
unsigned activeCount = llvm::count_if(segments, [](OutputSegment *segment) {
return segment->requiredInBinary() &&
(segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0;
});
#endif
assert((ctx.arg.sharedMemory || !ctx.isPic || ctx.arg.extendedConst ||
activeCount <= 1) &&
"output segments should have been combined by now");
writeUleb128(os, segmentCount, "data segment count");
bodySize = dataSectionHeader.size();
bool is64 = ctx.arg.is64.value_or(false);
for (OutputSegment *segment : segments) {
if (!segment->requiredInBinary())
continue;
raw_string_ostream os(segment->header);
writeUleb128(os, segment->initFlags, "init flags");
if (segment->initFlags & WASM_DATA_SEGMENT_HAS_MEMINDEX)
writeUleb128(os, 0, "memory index");
if ((segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
if (ctx.isPic && ctx.arg.extendedConst) {
writeU8(os, WASM_OPCODE_GLOBAL_GET, "global get");
writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(),
"literal (global index)");
if (segment->startVA) {
writePtrConst(os, segment->startVA, is64, "offset");
writeU8(os, is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD, "add");
}
writeU8(os, WASM_OPCODE_END, "opcode:end");
} else {
WasmInitExpr initExpr;
initExpr.Extended = false;
if (ctx.isPic) {
assert(segment->startVA == 0);
initExpr.Inst.Opcode = WASM_OPCODE_GLOBAL_GET;
initExpr.Inst.Value.Global = WasmSym::memoryBase->getGlobalIndex();
} else {
initExpr = intConst(segment->startVA, is64);
}
writeInitExpr(os, initExpr);
}
}
writeUleb128(os, segment->size, "segment size");
segment->sectionOffset = bodySize;
bodySize += segment->header.size() + segment->size;
log("Data segment: size=" + Twine(segment->size) + ", startVA=" +
Twine::utohexstr(segment->startVA) + ", name=" + segment->name);
for (InputChunk *inputSeg : segment->inputSegments) {
inputSeg->outputSec = this;
inputSeg->outSecOff = segment->sectionOffset + segment->header.size() +
inputSeg->outputSegmentOffset;
}
}
createHeader(bodySize);
}
void DataSection::writeTo(uint8_t *buf) {
log("writing " + toString(*this) + " offset=" + Twine(offset) +
" size=" + Twine(getSize()) + " body=" + Twine(bodySize));
buf += offset;
// Write section header
memcpy(buf, header.data(), header.size());
buf += header.size();
// Write data section headers
memcpy(buf, dataSectionHeader.data(), dataSectionHeader.size());
for (const OutputSegment *segment : segments) {
if (!segment->requiredInBinary())
continue;
// Write data segment header
uint8_t *segStart = buf + segment->sectionOffset;
memcpy(segStart, segment->header.data(), segment->header.size());
// Write segment data payload
for (const InputChunk *chunk : segment->inputSegments)
chunk->writeTo(buf);
}
}
uint32_t DataSection::getNumRelocations() const {
uint32_t count = 0;
for (const OutputSegment *seg : segments)
for (const InputChunk *inputSeg : seg->inputSegments)
count += inputSeg->getNumRelocations();
return count;
}
void DataSection::writeRelocations(raw_ostream &os) const {
for (const OutputSegment *seg : segments)
for (const InputChunk *c : seg->inputSegments)
c->writeRelocations(os);
}
bool DataSection::isNeeded() const {
for (const OutputSegment *seg : segments)
if (seg->requiredInBinary())
return true;
return false;
}
// Lots of duplication here with OutputSegment::finalizeInputSegments
void CustomSection::finalizeInputSections() {
SyntheticMergedChunk *mergedSection = nullptr;
std::vector<InputChunk *> newSections;
for (InputChunk *s : inputSections) {
s->outputSec = this;
MergeInputChunk *ms = dyn_cast<MergeInputChunk>(s);
if (!ms) {
newSections.push_back(s);
continue;
}
if (!mergedSection) {
mergedSection =
make<SyntheticMergedChunk>(name, 0, WASM_SEG_FLAG_STRINGS);
newSections.push_back(mergedSection);
mergedSection->outputSec = this;
}
mergedSection->addMergeChunk(ms);
}
if (!mergedSection)
return;
mergedSection->finalizeContents();
inputSections = newSections;
}
void CustomSection::finalizeContents() {
finalizeInputSections();
raw_string_ostream os(nameData);
encodeULEB128(name.size(), os);
os << name;
for (InputChunk *section : inputSections) {
assert(!section->discarded);
payloadSize = alignTo(payloadSize, section->alignment);
section->outSecOff = payloadSize;
payloadSize += section->getSize();
}
createHeader(payloadSize + nameData.size());
}
void CustomSection::writeTo(uint8_t *buf) {
log("writing " + toString(*this) + " offset=" + Twine(offset) +
" size=" + Twine(getSize()) + " chunks=" + Twine(inputSections.size()));
assert(offset);
buf += offset;
// Write section header
memcpy(buf, header.data(), header.size());
buf += header.size();
memcpy(buf, nameData.data(), nameData.size());
buf += nameData.size();
// Write custom sections payload
for (const InputChunk *section : inputSections)
section->writeTo(buf);
}
uint32_t CustomSection::getNumRelocations() const {
uint32_t count = 0;
for (const InputChunk *inputSect : inputSections)
count += inputSect->getNumLiveRelocations();
return count;
}
void CustomSection::writeRelocations(raw_ostream &os) const {
for (const InputChunk *s : inputSections)
s->writeRelocations(os);
}
} // namespace wasm
} // namespace lld