1ece8a530Spatrick //===- OutputSections.cpp -------------------------------------------------===//
2ece8a530Spatrick //
3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information.
5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ece8a530Spatrick //
7ece8a530Spatrick //===----------------------------------------------------------------------===//
8ece8a530Spatrick
9ece8a530Spatrick #include "OutputSections.h"
10ece8a530Spatrick #include "InputChunks.h"
111cf9926bSpatrick #include "InputElement.h"
12ece8a530Spatrick #include "InputFiles.h"
13ece8a530Spatrick #include "OutputSegment.h"
14ece8a530Spatrick #include "WriterUtils.h"
15ece8a530Spatrick #include "lld/Common/ErrorHandler.h"
161cf9926bSpatrick #include "lld/Common/Memory.h"
17ece8a530Spatrick #include "llvm/ADT/Twine.h"
18ece8a530Spatrick #include "llvm/Support/LEB128.h"
19bb684c34Spatrick #include "llvm/Support/Parallel.h"
20ece8a530Spatrick
21ece8a530Spatrick #define DEBUG_TYPE "lld"
22ece8a530Spatrick
23ece8a530Spatrick using namespace llvm;
24ece8a530Spatrick using namespace llvm::wasm;
25ece8a530Spatrick
26ece8a530Spatrick namespace lld {
27ece8a530Spatrick
28ece8a530Spatrick // Returns a string, e.g. "FUNCTION(.text)".
toString(const wasm::OutputSection & sec)29ece8a530Spatrick std::string toString(const wasm::OutputSection &sec) {
30ece8a530Spatrick if (!sec.name.empty())
31ece8a530Spatrick return (sec.getSectionName() + "(" + sec.name + ")").str();
32bb684c34Spatrick return std::string(sec.getSectionName());
33ece8a530Spatrick }
34ece8a530Spatrick
35ece8a530Spatrick namespace wasm {
getSectionName() const36ece8a530Spatrick StringRef OutputSection::getSectionName() const {
37ece8a530Spatrick return sectionTypeToString(type);
38ece8a530Spatrick }
39ece8a530Spatrick
createHeader(size_t bodySize)40ece8a530Spatrick void OutputSection::createHeader(size_t bodySize) {
41ece8a530Spatrick raw_string_ostream os(header);
42ece8a530Spatrick debugWrite(os.tell(), "section type [" + getSectionName() + "]");
43ece8a530Spatrick encodeULEB128(type, os);
44ece8a530Spatrick writeUleb128(os, bodySize, "section size");
45ece8a530Spatrick os.flush();
46ece8a530Spatrick log("createHeader: " + toString(*this) + " body=" + Twine(bodySize) +
47ece8a530Spatrick " total=" + Twine(getSize()));
48ece8a530Spatrick }
49ece8a530Spatrick
finalizeContents()50ece8a530Spatrick void CodeSection::finalizeContents() {
51ece8a530Spatrick raw_string_ostream os(codeSectionHeader);
52ece8a530Spatrick writeUleb128(os, functions.size(), "function count");
53ece8a530Spatrick os.flush();
54ece8a530Spatrick bodySize = codeSectionHeader.size();
55ece8a530Spatrick
56ece8a530Spatrick for (InputFunction *func : functions) {
571cf9926bSpatrick func->outputSec = this;
581cf9926bSpatrick func->outSecOff = bodySize;
59ece8a530Spatrick func->calculateSize();
601cf9926bSpatrick // All functions should have a non-empty body at this point
611cf9926bSpatrick assert(func->getSize());
62ece8a530Spatrick bodySize += func->getSize();
63ece8a530Spatrick }
64ece8a530Spatrick
65ece8a530Spatrick createHeader(bodySize);
66ece8a530Spatrick }
67ece8a530Spatrick
writeTo(uint8_t * buf)68ece8a530Spatrick void CodeSection::writeTo(uint8_t *buf) {
69*dfe94b16Srobert log("writing " + toString(*this) + " offset=" + Twine(offset) +
70*dfe94b16Srobert " size=" + Twine(getSize()));
71ece8a530Spatrick log(" headersize=" + Twine(header.size()));
72ece8a530Spatrick log(" codeheadersize=" + Twine(codeSectionHeader.size()));
73ece8a530Spatrick buf += offset;
74ece8a530Spatrick
75ece8a530Spatrick // Write section header
76ece8a530Spatrick memcpy(buf, header.data(), header.size());
77ece8a530Spatrick buf += header.size();
78ece8a530Spatrick
79ece8a530Spatrick // Write code section headers
80ece8a530Spatrick memcpy(buf, codeSectionHeader.data(), codeSectionHeader.size());
81ece8a530Spatrick
82ece8a530Spatrick // Write code section bodies
83ece8a530Spatrick for (const InputChunk *chunk : functions)
84ece8a530Spatrick chunk->writeTo(buf);
85ece8a530Spatrick }
86ece8a530Spatrick
getNumRelocations() const87ece8a530Spatrick uint32_t CodeSection::getNumRelocations() const {
88ece8a530Spatrick uint32_t count = 0;
89ece8a530Spatrick for (const InputChunk *func : functions)
90ece8a530Spatrick count += func->getNumRelocations();
91ece8a530Spatrick return count;
92ece8a530Spatrick }
93ece8a530Spatrick
writeRelocations(raw_ostream & os) const94ece8a530Spatrick void CodeSection::writeRelocations(raw_ostream &os) const {
95ece8a530Spatrick for (const InputChunk *c : functions)
96ece8a530Spatrick c->writeRelocations(os);
97ece8a530Spatrick }
98ece8a530Spatrick
finalizeContents()99ece8a530Spatrick void DataSection::finalizeContents() {
100ece8a530Spatrick raw_string_ostream os(dataSectionHeader);
101*dfe94b16Srobert unsigned segmentCount = llvm::count_if(segments, [](OutputSegment *segment) {
102*dfe94b16Srobert return segment->requiredInBinary();
103*dfe94b16Srobert });
1041cf9926bSpatrick #ifndef NDEBUG
105*dfe94b16Srobert unsigned activeCount = llvm::count_if(segments, [](OutputSegment *segment) {
1061cf9926bSpatrick return (segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0;
1071cf9926bSpatrick });
1081cf9926bSpatrick #endif
1091cf9926bSpatrick
110*dfe94b16Srobert assert((config->sharedMemory || !config->isPic || config->extendedConst ||
111*dfe94b16Srobert activeCount <= 1) &&
1121cf9926bSpatrick "output segments should have been combined by now");
1131cf9926bSpatrick
114ece8a530Spatrick writeUleb128(os, segmentCount, "data segment count");
115ece8a530Spatrick os.flush();
116ece8a530Spatrick bodySize = dataSectionHeader.size();
117*dfe94b16Srobert bool is64 = config->is64.value_or(false);
118ece8a530Spatrick
119ece8a530Spatrick for (OutputSegment *segment : segments) {
120*dfe94b16Srobert if (!segment->requiredInBinary())
121ece8a530Spatrick continue;
122ece8a530Spatrick raw_string_ostream os(segment->header);
123ece8a530Spatrick writeUleb128(os, segment->initFlags, "init flags");
1241cf9926bSpatrick if (segment->initFlags & WASM_DATA_SEGMENT_HAS_MEMINDEX)
125ece8a530Spatrick writeUleb128(os, 0, "memory index");
1261cf9926bSpatrick if ((segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
127*dfe94b16Srobert if (config->isPic && config->extendedConst) {
128*dfe94b16Srobert writeU8(os, WASM_OPCODE_GLOBAL_GET, "global get");
129*dfe94b16Srobert writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(),
130*dfe94b16Srobert "literal (global index)");
131*dfe94b16Srobert if (segment->startVA) {
132*dfe94b16Srobert writePtrConst(os, segment->startVA, is64, "offset");
133*dfe94b16Srobert writeU8(os, is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD, "add");
134*dfe94b16Srobert }
135*dfe94b16Srobert writeU8(os, WASM_OPCODE_END, "opcode:end");
136ece8a530Spatrick } else {
137*dfe94b16Srobert WasmInitExpr initExpr;
138*dfe94b16Srobert initExpr.Extended = false;
139*dfe94b16Srobert if (config->isPic) {
140*dfe94b16Srobert assert(segment->startVA == 0);
141*dfe94b16Srobert initExpr.Inst.Opcode = WASM_OPCODE_GLOBAL_GET;
142*dfe94b16Srobert initExpr.Inst.Value.Global = WasmSym::memoryBase->getGlobalIndex();
143*dfe94b16Srobert } else {
144*dfe94b16Srobert initExpr = intConst(segment->startVA, is64);
145ece8a530Spatrick }
146ece8a530Spatrick writeInitExpr(os, initExpr);
147ece8a530Spatrick }
148*dfe94b16Srobert }
149ece8a530Spatrick writeUleb128(os, segment->size, "segment size");
150ece8a530Spatrick os.flush();
151ece8a530Spatrick
152ece8a530Spatrick segment->sectionOffset = bodySize;
153ece8a530Spatrick bodySize += segment->header.size() + segment->size;
154ece8a530Spatrick log("Data segment: size=" + Twine(segment->size) + ", startVA=" +
155ece8a530Spatrick Twine::utohexstr(segment->startVA) + ", name=" + segment->name);
156ece8a530Spatrick
1571cf9926bSpatrick for (InputChunk *inputSeg : segment->inputSegments) {
1581cf9926bSpatrick inputSeg->outputSec = this;
1591cf9926bSpatrick inputSeg->outSecOff = segment->sectionOffset + segment->header.size() +
160ece8a530Spatrick inputSeg->outputSegmentOffset;
161ece8a530Spatrick }
1621cf9926bSpatrick }
163ece8a530Spatrick
164ece8a530Spatrick createHeader(bodySize);
165ece8a530Spatrick }
166ece8a530Spatrick
writeTo(uint8_t * buf)167ece8a530Spatrick void DataSection::writeTo(uint8_t *buf) {
168*dfe94b16Srobert log("writing " + toString(*this) + " offset=" + Twine(offset) +
169*dfe94b16Srobert " size=" + Twine(getSize()) + " body=" + Twine(bodySize));
170ece8a530Spatrick buf += offset;
171ece8a530Spatrick
172ece8a530Spatrick // Write section header
173ece8a530Spatrick memcpy(buf, header.data(), header.size());
174ece8a530Spatrick buf += header.size();
175ece8a530Spatrick
176ece8a530Spatrick // Write data section headers
177ece8a530Spatrick memcpy(buf, dataSectionHeader.data(), dataSectionHeader.size());
178ece8a530Spatrick
179ece8a530Spatrick for (const OutputSegment *segment : segments) {
180*dfe94b16Srobert if (!segment->requiredInBinary())
181ece8a530Spatrick continue;
182ece8a530Spatrick // Write data segment header
183ece8a530Spatrick uint8_t *segStart = buf + segment->sectionOffset;
184ece8a530Spatrick memcpy(segStart, segment->header.data(), segment->header.size());
185ece8a530Spatrick
186ece8a530Spatrick // Write segment data payload
187ece8a530Spatrick for (const InputChunk *chunk : segment->inputSegments)
188ece8a530Spatrick chunk->writeTo(buf);
189ece8a530Spatrick }
190ece8a530Spatrick }
191ece8a530Spatrick
getNumRelocations() const192ece8a530Spatrick uint32_t DataSection::getNumRelocations() const {
193ece8a530Spatrick uint32_t count = 0;
194ece8a530Spatrick for (const OutputSegment *seg : segments)
195ece8a530Spatrick for (const InputChunk *inputSeg : seg->inputSegments)
196ece8a530Spatrick count += inputSeg->getNumRelocations();
197ece8a530Spatrick return count;
198ece8a530Spatrick }
199ece8a530Spatrick
writeRelocations(raw_ostream & os) const200ece8a530Spatrick void DataSection::writeRelocations(raw_ostream &os) const {
201ece8a530Spatrick for (const OutputSegment *seg : segments)
202ece8a530Spatrick for (const InputChunk *c : seg->inputSegments)
203ece8a530Spatrick c->writeRelocations(os);
204ece8a530Spatrick }
205ece8a530Spatrick
isNeeded() const206ece8a530Spatrick bool DataSection::isNeeded() const {
207ece8a530Spatrick for (const OutputSegment *seg : segments)
208*dfe94b16Srobert if (seg->requiredInBinary())
209ece8a530Spatrick return true;
210ece8a530Spatrick return false;
211ece8a530Spatrick }
212ece8a530Spatrick
2131cf9926bSpatrick // Lots of duplication here with OutputSegment::finalizeInputSegments
finalizeInputSections()2141cf9926bSpatrick void CustomSection::finalizeInputSections() {
2151cf9926bSpatrick SyntheticMergedChunk *mergedSection = nullptr;
2161cf9926bSpatrick std::vector<InputChunk *> newSections;
2171cf9926bSpatrick
2181cf9926bSpatrick for (InputChunk *s : inputSections) {
2191cf9926bSpatrick s->outputSec = this;
2201cf9926bSpatrick MergeInputChunk *ms = dyn_cast<MergeInputChunk>(s);
2211cf9926bSpatrick if (!ms) {
2221cf9926bSpatrick newSections.push_back(s);
2231cf9926bSpatrick continue;
2241cf9926bSpatrick }
2251cf9926bSpatrick
2261cf9926bSpatrick if (!mergedSection) {
2271cf9926bSpatrick mergedSection =
2281cf9926bSpatrick make<SyntheticMergedChunk>(name, 0, WASM_SEG_FLAG_STRINGS);
2291cf9926bSpatrick newSections.push_back(mergedSection);
2301cf9926bSpatrick mergedSection->outputSec = this;
2311cf9926bSpatrick }
2321cf9926bSpatrick mergedSection->addMergeChunk(ms);
2331cf9926bSpatrick }
2341cf9926bSpatrick
2351cf9926bSpatrick if (!mergedSection)
2361cf9926bSpatrick return;
2371cf9926bSpatrick
2381cf9926bSpatrick mergedSection->finalizeContents();
2391cf9926bSpatrick inputSections = newSections;
2401cf9926bSpatrick }
2411cf9926bSpatrick
finalizeContents()242ece8a530Spatrick void CustomSection::finalizeContents() {
2431cf9926bSpatrick finalizeInputSections();
2441cf9926bSpatrick
245ece8a530Spatrick raw_string_ostream os(nameData);
246ece8a530Spatrick encodeULEB128(name.size(), os);
247ece8a530Spatrick os << name;
248ece8a530Spatrick os.flush();
249ece8a530Spatrick
2501cf9926bSpatrick for (InputChunk *section : inputSections) {
2511cf9926bSpatrick assert(!section->discarded);
2521cf9926bSpatrick section->outSecOff = payloadSize;
253ece8a530Spatrick payloadSize += section->getSize();
254ece8a530Spatrick }
255ece8a530Spatrick
256ece8a530Spatrick createHeader(payloadSize + nameData.size());
257ece8a530Spatrick }
258ece8a530Spatrick
writeTo(uint8_t * buf)259ece8a530Spatrick void CustomSection::writeTo(uint8_t *buf) {
260*dfe94b16Srobert log("writing " + toString(*this) + " offset=" + Twine(offset) +
261*dfe94b16Srobert " size=" + Twine(getSize()) + " chunks=" + Twine(inputSections.size()));
262ece8a530Spatrick
263ece8a530Spatrick assert(offset);
264ece8a530Spatrick buf += offset;
265ece8a530Spatrick
266ece8a530Spatrick // Write section header
267ece8a530Spatrick memcpy(buf, header.data(), header.size());
268ece8a530Spatrick buf += header.size();
269ece8a530Spatrick memcpy(buf, nameData.data(), nameData.size());
270ece8a530Spatrick buf += nameData.size();
271ece8a530Spatrick
272ece8a530Spatrick // Write custom sections payload
2731cf9926bSpatrick for (const InputChunk *section : inputSections)
274ece8a530Spatrick section->writeTo(buf);
275ece8a530Spatrick }
276ece8a530Spatrick
getNumRelocations() const277ece8a530Spatrick uint32_t CustomSection::getNumRelocations() const {
278ece8a530Spatrick uint32_t count = 0;
2791cf9926bSpatrick for (const InputChunk *inputSect : inputSections)
280ece8a530Spatrick count += inputSect->getNumRelocations();
281ece8a530Spatrick return count;
282ece8a530Spatrick }
283ece8a530Spatrick
writeRelocations(raw_ostream & os) const284ece8a530Spatrick void CustomSection::writeRelocations(raw_ostream &os) const {
2851cf9926bSpatrick for (const InputChunk *s : inputSections)
286ece8a530Spatrick s->writeRelocations(os);
287ece8a530Spatrick }
288ece8a530Spatrick
289ece8a530Spatrick } // namespace wasm
290ece8a530Spatrick } // namespace lld
291