1 //===- OutputSections.cpp -------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "OutputSections.h"
10 #include "InputChunks.h"
11 #include "InputFiles.h"
12 #include "OutputSegment.h"
13 #include "WriterUtils.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"
19 #define DEBUG_TYPE "lld"
22 using namespace llvm::wasm
;
26 // Returns a string, e.g. "FUNCTION(.text)".
27 std::string
toString(const wasm::OutputSection
&sec
) {
28 if (!sec
.name
.empty())
29 return (sec
.getSectionName() + "(" + sec
.name
+ ")").str();
30 return std::string(sec
.getSectionName());
34 static StringRef
sectionTypeToString(uint32_t sectionType
) {
35 switch (sectionType
) {
42 case WASM_SEC_FUNCTION
:
62 case WASM_SEC_DATACOUNT
:
65 fatal("invalid section type");
69 StringRef
OutputSection::getSectionName() const {
70 return sectionTypeToString(type
);
73 void OutputSection::createHeader(size_t bodySize
) {
74 raw_string_ostream
os(header
);
75 debugWrite(os
.tell(), "section type [" + getSectionName() + "]");
76 encodeULEB128(type
, os
);
77 writeUleb128(os
, bodySize
, "section size");
79 log("createHeader: " + toString(*this) + " body=" + Twine(bodySize
) +
80 " total=" + Twine(getSize()));
83 void CodeSection::finalizeContents() {
84 raw_string_ostream
os(codeSectionHeader
);
85 writeUleb128(os
, functions
.size(), "function count");
87 bodySize
= codeSectionHeader
.size();
89 for (InputFunction
*func
: functions
) {
90 func
->outputOffset
= bodySize
;
91 func
->calculateSize();
92 bodySize
+= func
->getSize();
95 createHeader(bodySize
);
98 void CodeSection::writeTo(uint8_t *buf
) {
99 log("writing " + toString(*this));
100 log(" size=" + Twine(getSize()));
101 log(" headersize=" + Twine(header
.size()));
102 log(" codeheadersize=" + Twine(codeSectionHeader
.size()));
105 // Write section header
106 memcpy(buf
, header
.data(), header
.size());
107 buf
+= header
.size();
109 // Write code section headers
110 memcpy(buf
, codeSectionHeader
.data(), codeSectionHeader
.size());
112 // Write code section bodies
113 for (const InputChunk
*chunk
: functions
)
117 uint32_t CodeSection::getNumRelocations() const {
119 for (const InputChunk
*func
: functions
)
120 count
+= func
->getNumRelocations();
124 void CodeSection::writeRelocations(raw_ostream
&os
) const {
125 for (const InputChunk
*c
: functions
)
126 c
->writeRelocations(os
);
129 void DataSection::finalizeContents() {
130 raw_string_ostream
os(dataSectionHeader
);
131 unsigned segmentCount
=
132 std::count_if(segments
.begin(), segments
.end(),
133 [](OutputSegment
*segment
) { return !segment
->isBss
; });
135 writeUleb128(os
, segmentCount
, "data segment count");
137 bodySize
= dataSectionHeader
.size();
139 assert((!config
->isPic
|| segments
.size() <= 1) &&
140 "Currenly only a single data segment is supported in PIC mode");
142 for (OutputSegment
*segment
: segments
) {
145 raw_string_ostream
os(segment
->header
);
146 writeUleb128(os
, segment
->initFlags
, "init flags");
147 if (segment
->initFlags
& WASM_SEGMENT_HAS_MEMINDEX
)
148 writeUleb128(os
, 0, "memory index");
149 if ((segment
->initFlags
& WASM_SEGMENT_IS_PASSIVE
) == 0) {
150 WasmInitExpr initExpr
;
152 initExpr
.Opcode
= WASM_OPCODE_GLOBAL_GET
;
153 initExpr
.Value
.Global
= WasmSym::memoryBase
->getGlobalIndex();
155 initExpr
.Opcode
= WASM_OPCODE_I32_CONST
;
156 initExpr
.Value
.Int32
= segment
->startVA
;
158 writeInitExpr(os
, initExpr
);
160 writeUleb128(os
, segment
->size
, "segment size");
163 segment
->sectionOffset
= bodySize
;
164 bodySize
+= segment
->header
.size() + segment
->size
;
165 log("Data segment: size=" + Twine(segment
->size
) + ", startVA=" +
166 Twine::utohexstr(segment
->startVA
) + ", name=" + segment
->name
);
168 for (InputSegment
*inputSeg
: segment
->inputSegments
)
169 inputSeg
->outputOffset
= segment
->sectionOffset
+ segment
->header
.size() +
170 inputSeg
->outputSegmentOffset
;
173 createHeader(bodySize
);
176 void DataSection::writeTo(uint8_t *buf
) {
177 log("writing " + toString(*this) + " size=" + Twine(getSize()) +
178 " body=" + Twine(bodySize
));
181 // Write section header
182 memcpy(buf
, header
.data(), header
.size());
183 buf
+= header
.size();
185 // Write data section headers
186 memcpy(buf
, dataSectionHeader
.data(), dataSectionHeader
.size());
188 for (const OutputSegment
*segment
: segments
) {
191 // Write data segment header
192 uint8_t *segStart
= buf
+ segment
->sectionOffset
;
193 memcpy(segStart
, segment
->header
.data(), segment
->header
.size());
195 // Write segment data payload
196 for (const InputChunk
*chunk
: segment
->inputSegments
)
201 uint32_t DataSection::getNumRelocations() const {
203 for (const OutputSegment
*seg
: segments
)
204 for (const InputChunk
*inputSeg
: seg
->inputSegments
)
205 count
+= inputSeg
->getNumRelocations();
209 void DataSection::writeRelocations(raw_ostream
&os
) const {
210 for (const OutputSegment
*seg
: segments
)
211 for (const InputChunk
*c
: seg
->inputSegments
)
212 c
->writeRelocations(os
);
215 bool DataSection::isNeeded() const {
216 for (const OutputSegment
*seg
: segments
)
222 void CustomSection::finalizeContents() {
223 raw_string_ostream
os(nameData
);
224 encodeULEB128(name
.size(), os
);
228 for (InputSection
*section
: inputSections
) {
229 section
->outputOffset
= payloadSize
;
230 section
->outputSec
= this;
231 payloadSize
+= section
->getSize();
234 createHeader(payloadSize
+ nameData
.size());
237 void CustomSection::writeTo(uint8_t *buf
) {
238 log("writing " + toString(*this) + " size=" + Twine(getSize()) +
239 " chunks=" + Twine(inputSections
.size()));
244 // Write section header
245 memcpy(buf
, header
.data(), header
.size());
246 buf
+= header
.size();
247 memcpy(buf
, nameData
.data(), nameData
.size());
248 buf
+= nameData
.size();
250 // Write custom sections payload
251 for (const InputSection
*section
: inputSections
)
252 section
->writeTo(buf
);
255 uint32_t CustomSection::getNumRelocations() const {
257 for (const InputSection
*inputSect
: inputSections
)
258 count
+= inputSect
->getNumRelocations();
262 void CustomSection::writeRelocations(raw_ostream
&os
) const {
263 for (const InputSection
*s
: inputSections
)
264 s
->writeRelocations(os
);