1 //===- OutputSections.cpp -------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "OutputSections.h"
11 #include "InputChunks.h"
12 #include "InputFiles.h"
13 #include "OutputSegment.h"
14 #include "WriterUtils.h"
15 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/Threads.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/Support/LEB128.h"
20 #define DEBUG_TYPE "lld"
23 using namespace llvm::wasm;
25 using namespace lld::wasm;
27 static StringRef sectionTypeToString(uint32_t SectionType) {
28 switch (SectionType) {
35 case WASM_SEC_FUNCTION:
54 fatal("invalid section type");
58 // Returns a string, e.g. "FUNCTION(.text)".
59 std::string lld::toString(const OutputSection &Sec) {
60 if (!Sec.Name.empty())
61 return (Sec.getSectionName() + "(" + Sec.Name + ")").str();
62 return Sec.getSectionName();
65 StringRef OutputSection::getSectionName() const {
66 return sectionTypeToString(Type);
69 void OutputSection::createHeader(size_t BodySize) {
70 raw_string_ostream OS(Header);
71 debugWrite(OS.tell(), "section type [" + getSectionName() + "]");
72 encodeULEB128(Type, OS);
73 writeUleb128(OS, BodySize, "section size");
75 log("createHeader: " + toString(*this) + " body=" + Twine(BodySize) +
76 " total=" + Twine(getSize()));
79 CodeSection::CodeSection(ArrayRef<InputFunction *> Functions)
80 : OutputSection(WASM_SEC_CODE), Functions(Functions) {
81 assert(Functions.size() > 0);
83 raw_string_ostream OS(CodeSectionHeader);
84 writeUleb128(OS, Functions.size(), "function count");
86 BodySize = CodeSectionHeader.size();
88 for (InputFunction *Func : Functions) {
89 Func->OutputOffset = BodySize;
90 Func->calculateSize();
91 BodySize += Func->getSize();
94 createHeader(BodySize);
97 void CodeSection::writeTo(uint8_t *Buf) {
98 log("writing " + toString(*this));
99 log(" size=" + Twine(getSize()));
100 log(" headersize=" + Twine(Header.size()));
101 log(" codeheadersize=" + Twine(CodeSectionHeader.size()));
104 // Write section header
105 memcpy(Buf, Header.data(), Header.size());
106 Buf += Header.size();
108 // Write code section headers
109 memcpy(Buf, CodeSectionHeader.data(), CodeSectionHeader.size());
111 // Write code section bodies
112 parallelForEach(Functions,
113 [&](const InputChunk *Chunk) { Chunk->writeTo(Buf); });
116 uint32_t CodeSection::numRelocations() const {
118 for (const InputChunk *Func : Functions)
119 Count += Func->NumRelocations();
123 void CodeSection::writeRelocations(raw_ostream &OS) const {
124 for (const InputChunk *C : Functions)
125 C->writeRelocations(OS);
128 DataSection::DataSection(ArrayRef<OutputSegment *> Segments)
129 : OutputSection(WASM_SEC_DATA), Segments(Segments) {
130 raw_string_ostream OS(DataSectionHeader);
132 writeUleb128(OS, Segments.size(), "data segment count");
134 BodySize = DataSectionHeader.size();
136 for (OutputSegment *Segment : Segments) {
137 raw_string_ostream OS(Segment->Header);
138 writeUleb128(OS, 0, "memory index");
139 writeUleb128(OS, WASM_OPCODE_I32_CONST, "opcode:i32const");
140 writeSleb128(OS, Segment->StartVA, "memory offset");
141 writeUleb128(OS, WASM_OPCODE_END, "opcode:end");
142 writeUleb128(OS, Segment->Size, "segment size");
145 Segment->SectionOffset = BodySize;
146 BodySize += Segment->Header.size() + Segment->Size;
147 log("Data segment: size=" + Twine(Segment->Size));
149 for (InputSegment *InputSeg : Segment->InputSegments)
150 InputSeg->OutputOffset = Segment->SectionOffset + Segment->Header.size() +
151 InputSeg->OutputSegmentOffset;
154 createHeader(BodySize);
157 void DataSection::writeTo(uint8_t *Buf) {
158 log("writing " + toString(*this) + " size=" + Twine(getSize()) +
159 " body=" + Twine(BodySize));
162 // Write section header
163 memcpy(Buf, Header.data(), Header.size());
164 Buf += Header.size();
166 // Write data section headers
167 memcpy(Buf, DataSectionHeader.data(), DataSectionHeader.size());
169 parallelForEach(Segments, [&](const OutputSegment *Segment) {
170 // Write data segment header
171 uint8_t *SegStart = Buf + Segment->SectionOffset;
172 memcpy(SegStart, Segment->Header.data(), Segment->Header.size());
174 // Write segment data payload
175 for (const InputChunk *Chunk : Segment->InputSegments)
180 uint32_t DataSection::numRelocations() const {
182 for (const OutputSegment *Seg : Segments)
183 for (const InputChunk *InputSeg : Seg->InputSegments)
184 Count += InputSeg->NumRelocations();
188 void DataSection::writeRelocations(raw_ostream &OS) const {
189 for (const OutputSegment *Seg : Segments)
190 for (const InputChunk *C : Seg->InputSegments)
191 C->writeRelocations(OS);
194 CustomSection::CustomSection(std::string Name,
195 ArrayRef<InputSection *> InputSections)
196 : OutputSection(WASM_SEC_CUSTOM, Name), PayloadSize(0),
197 InputSections(InputSections) {
198 raw_string_ostream OS(NameData);
199 encodeULEB128(Name.size(), OS);
203 for (InputSection *Section : InputSections) {
204 Section->OutputOffset = PayloadSize;
205 PayloadSize += Section->getSize();
208 createHeader(PayloadSize + NameData.size());
211 void CustomSection::writeTo(uint8_t *Buf) {
212 log("writing " + toString(*this) + " size=" + Twine(getSize()) +
213 " chunks=" + Twine(InputSections.size()));
218 // Write section header
219 memcpy(Buf, Header.data(), Header.size());
220 Buf += Header.size();
221 memcpy(Buf, NameData.data(), NameData.size());
222 Buf += NameData.size();
224 // Write custom sections payload
225 parallelForEach(InputSections,
226 [&](const InputSection *Section) { Section->writeTo(Buf); });
229 uint32_t CustomSection::numRelocations() const {
231 for (const InputSection *InputSect : InputSections)
232 Count += InputSect->NumRelocations();
236 void CustomSection::writeRelocations(raw_ostream &OS) const {
237 for (const InputSection *S : InputSections)
238 S->writeRelocations(OS);