blob: c019d89e447ba65dd5fa087c8e8e591437f522a2 [file] [log] [blame]
Sam Cleggb93d75b2017-11-17 18:14:09 +00001//===- OutputSections.cpp -------------------------------------------------===//
2//
Chandler Carruth77351c12019-01-19 08:50:56 +00003// 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
Sam Cleggb93d75b2017-11-17 18:14:09 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "OutputSections.h"
Sam Cleggc8e19172018-01-10 01:13:34 +000010#include "InputChunks.h"
Sam Cleggb93d75b2017-11-17 18:14:09 +000011#include "InputFiles.h"
Sam Cleggb93d75b2017-11-17 18:14:09 +000012#include "OutputSegment.h"
Rui Ueyamadfb6c642018-02-28 00:52:42 +000013#include "WriterUtils.h"
Sam Cleggb93d75b2017-11-17 18:14:09 +000014#include "lld/Common/ErrorHandler.h"
Sam Cleggb93d75b2017-11-17 18:14:09 +000015#include "lld/Common/Threads.h"
16#include "llvm/ADT/Twine.h"
Rui Ueyamaa1cd26b2018-02-16 20:38:00 +000017#include "llvm/Support/LEB128.h"
Sam Cleggb93d75b2017-11-17 18:14:09 +000018
19#define DEBUG_TYPE "lld"
20
21using namespace llvm;
22using namespace llvm::wasm;
23using namespace lld;
24using namespace lld::wasm;
25
Rui Ueyamacc52c362019-07-11 05:40:30 +000026static StringRef sectionTypeToString(uint32_t sectionType) {
27 switch (sectionType) {
Sam Cleggb93d75b2017-11-17 18:14:09 +000028 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";
Heejin Ahn6532a862018-12-08 06:17:43 +000042 case WASM_SEC_EVENT:
43 return "EVENT";
Sam Cleggb93d75b2017-11-17 18:14:09 +000044 case WASM_SEC_EXPORT:
45 return "EXPORT";
46 case WASM_SEC_START:
47 return "START";
48 case WASM_SEC_ELEM:
49 return "ELEM";
50 case WASM_SEC_CODE:
51 return "CODE";
52 case WASM_SEC_DATA:
53 return "DATA";
Thomas Livelyed41ffb2019-04-19 23:40:36 +000054 case WASM_SEC_DATACOUNT:
55 return "DATACOUNT";
Sam Cleggb93d75b2017-11-17 18:14:09 +000056 default:
57 fatal("invalid section type");
58 }
59}
60
Rui Ueyama475951e2018-02-28 17:33:04 +000061// Returns a string, e.g. "FUNCTION(.text)".
Rui Ueyamacc52c362019-07-11 05:40:30 +000062std::string lld::toString(const OutputSection &sec) {
63 if (!sec.name.empty())
64 return (sec.getSectionName() + "(" + sec.name + ")").str();
65 return sec.getSectionName();
Sam Cleggb93d75b2017-11-17 18:14:09 +000066}
67
Rui Ueyama475951e2018-02-28 17:33:04 +000068StringRef OutputSection::getSectionName() const {
Rui Ueyamacc52c362019-07-11 05:40:30 +000069 return sectionTypeToString(type);
Sam Clegg7dc45382017-12-19 17:09:45 +000070}
71
Rui Ueyamacc52c362019-07-11 05:40:30 +000072void OutputSection::createHeader(size_t bodySize) {
73 raw_string_ostream os(header);
74 debugWrite(os.tell(), "section type [" + getSectionName() + "]");
75 encodeULEB128(type, os);
76 writeUleb128(os, bodySize, "section size");
77 os.flush();
78 log("createHeader: " + toString(*this) + " body=" + Twine(bodySize) +
Sam Cleggb93d75b2017-11-17 18:14:09 +000079 " total=" + Twine(getSize()));
80}
81
Sam Cleggaab008a2019-05-16 21:36:06 +000082void CodeSection::finalizeContents() {
Rui Ueyamacc52c362019-07-11 05:40:30 +000083 raw_string_ostream os(codeSectionHeader);
84 writeUleb128(os, functions.size(), "function count");
85 os.flush();
86 bodySize = codeSectionHeader.size();
Sam Cleggb93d75b2017-11-17 18:14:09 +000087
Rui Ueyamacc52c362019-07-11 05:40:30 +000088 for (InputFunction *func : functions) {
89 func->outputOffset = bodySize;
90 func->calculateSize();
91 bodySize += func->getSize();
Sam Cleggb93d75b2017-11-17 18:14:09 +000092 }
93
Rui Ueyamacc52c362019-07-11 05:40:30 +000094 createHeader(bodySize);
Sam Cleggb93d75b2017-11-17 18:14:09 +000095}
96
Rui Ueyamacc52c362019-07-11 05:40:30 +000097void CodeSection::writeTo(uint8_t *buf) {
Sam Clegg266bf352017-12-20 05:14:48 +000098 log("writing " + toString(*this));
Sam Cleggb93d75b2017-11-17 18:14:09 +000099 log(" size=" + Twine(getSize()));
Rui Ueyamacc52c362019-07-11 05:40:30 +0000100 log(" headersize=" + Twine(header.size()));
101 log(" codeheadersize=" + Twine(codeSectionHeader.size()));
102 buf += offset;
Sam Cleggb93d75b2017-11-17 18:14:09 +0000103
104 // Write section header
Rui Ueyamacc52c362019-07-11 05:40:30 +0000105 memcpy(buf, header.data(), header.size());
106 buf += header.size();
Sam Cleggb93d75b2017-11-17 18:14:09 +0000107
Sam Cleggb93d75b2017-11-17 18:14:09 +0000108 // Write code section headers
Rui Ueyamacc52c362019-07-11 05:40:30 +0000109 memcpy(buf, codeSectionHeader.data(), codeSectionHeader.size());
Sam Cleggb93d75b2017-11-17 18:14:09 +0000110
111 // Write code section bodies
Rui Ueyamacc52c362019-07-11 05:40:30 +0000112 for (const InputChunk *chunk : functions)
113 chunk->writeTo(buf);
Sam Cleggb93d75b2017-11-17 18:14:09 +0000114}
115
Rui Ueyama980a5a62019-07-10 09:10:01 +0000116uint32_t CodeSection::getNumRelocations() const {
Rui Ueyamacc52c362019-07-11 05:40:30 +0000117 uint32_t count = 0;
118 for (const InputChunk *func : functions)
119 count += func->getNumRelocations();
120 return count;
Sam Cleggb93d75b2017-11-17 18:14:09 +0000121}
122
Rui Ueyamacc52c362019-07-11 05:40:30 +0000123void CodeSection::writeRelocations(raw_ostream &os) const {
124 for (const InputChunk *c : functions)
125 c->writeRelocations(os);
Sam Cleggb93d75b2017-11-17 18:14:09 +0000126}
127
Sam Cleggaab008a2019-05-16 21:36:06 +0000128void DataSection::finalizeContents() {
Rui Ueyamacc52c362019-07-11 05:40:30 +0000129 raw_string_ostream os(dataSectionHeader);
Sam Cleggb93d75b2017-11-17 18:14:09 +0000130
Rui Ueyamacc52c362019-07-11 05:40:30 +0000131 writeUleb128(os, segments.size(), "data segment count");
132 os.flush();
133 bodySize = dataSectionHeader.size();
Sam Cleggb93d75b2017-11-17 18:14:09 +0000134
Rui Ueyamacc52c362019-07-11 05:40:30 +0000135 assert((!config->isPic || segments.size() <= 1) &&
Bill Wendlingf598b692019-07-08 22:05:02 +0000136 "Currenly only a single data segment is supported in PIC mode");
Thomas Livelye7eb8f02019-07-03 22:04:54 +0000137
Rui Ueyamacc52c362019-07-11 05:40:30 +0000138 for (OutputSegment *segment : segments) {
139 raw_string_ostream os(segment->header);
140 writeUleb128(os, segment->initFlags, "init flags");
141 if (segment->initFlags & WASM_SEGMENT_HAS_MEMINDEX)
142 writeUleb128(os, 0, "memory index");
143 if ((segment->initFlags & WASM_SEGMENT_IS_PASSIVE) == 0) {
144 WasmInitExpr initExpr;
145 if (config->isPic) {
146 initExpr.Opcode = WASM_OPCODE_GLOBAL_GET;
147 initExpr.Value.Global = WasmSym::memoryBase->getGlobalIndex();
Thomas Livelye7eb8f02019-07-03 22:04:54 +0000148 } else {
Rui Ueyamacc52c362019-07-11 05:40:30 +0000149 initExpr.Opcode = WASM_OPCODE_I32_CONST;
150 initExpr.Value.Int32 = segment->startVA;
Thomas Livelye7eb8f02019-07-03 22:04:54 +0000151 }
Rui Ueyamacc52c362019-07-11 05:40:30 +0000152 writeInitExpr(os, initExpr);
Sam Cleggeb90ff62018-11-15 00:37:21 +0000153 }
Rui Ueyamacc52c362019-07-11 05:40:30 +0000154 writeUleb128(os, segment->size, "segment size");
155 os.flush();
Rui Ueyama89322d72018-04-05 19:37:31 +0000156
Rui Ueyamacc52c362019-07-11 05:40:30 +0000157 segment->sectionOffset = bodySize;
158 bodySize += segment->header.size() + segment->size;
159 log("Data segment: size=" + Twine(segment->size) + ", startVA=" +
160 Twine::utohexstr(segment->startVA) + ", name=" + segment->name);
Rui Ueyama89322d72018-04-05 19:37:31 +0000161
Rui Ueyamacc52c362019-07-11 05:40:30 +0000162 for (InputSegment *inputSeg : segment->inputSegments)
163 inputSeg->outputOffset = segment->sectionOffset + segment->header.size() +
164 inputSeg->outputSegmentOffset;
Sam Cleggb93d75b2017-11-17 18:14:09 +0000165 }
166
Rui Ueyamacc52c362019-07-11 05:40:30 +0000167 createHeader(bodySize);
Sam Cleggb93d75b2017-11-17 18:14:09 +0000168}
169
Rui Ueyamacc52c362019-07-11 05:40:30 +0000170void DataSection::writeTo(uint8_t *buf) {
Sam Clegg266bf352017-12-20 05:14:48 +0000171 log("writing " + toString(*this) + " size=" + Twine(getSize()) +
Rui Ueyamacc52c362019-07-11 05:40:30 +0000172 " body=" + Twine(bodySize));
173 buf += offset;
Sam Cleggb93d75b2017-11-17 18:14:09 +0000174
175 // Write section header
Rui Ueyamacc52c362019-07-11 05:40:30 +0000176 memcpy(buf, header.data(), header.size());
177 buf += header.size();
Sam Cleggb93d75b2017-11-17 18:14:09 +0000178
Sam Cleggb93d75b2017-11-17 18:14:09 +0000179 // Write data section headers
Rui Ueyamacc52c362019-07-11 05:40:30 +0000180 memcpy(buf, dataSectionHeader.data(), dataSectionHeader.size());
Sam Cleggb93d75b2017-11-17 18:14:09 +0000181
Rui Ueyamacc52c362019-07-11 05:40:30 +0000182 for (const OutputSegment *segment : segments) {
Sam Cleggb93d75b2017-11-17 18:14:09 +0000183 // Write data segment header
Rui Ueyamacc52c362019-07-11 05:40:30 +0000184 uint8_t *segStart = buf + segment->sectionOffset;
185 memcpy(segStart, segment->header.data(), segment->header.size());
Sam Cleggb93d75b2017-11-17 18:14:09 +0000186
187 // Write segment data payload
Rui Ueyamacc52c362019-07-11 05:40:30 +0000188 for (const InputChunk *chunk : segment->inputSegments)
189 chunk->writeTo(buf);
Rui Ueyamaa0122d82019-04-17 02:12:47 +0000190 }
Sam Cleggd494f502017-12-19 20:45:15 +0000191}
Sam Cleggb93d75b2017-11-17 18:14:09 +0000192
Rui Ueyama980a5a62019-07-10 09:10:01 +0000193uint32_t DataSection::getNumRelocations() const {
Rui Ueyamacc52c362019-07-11 05:40:30 +0000194 uint32_t count = 0;
195 for (const OutputSegment *seg : segments)
196 for (const InputChunk *inputSeg : seg->inputSegments)
197 count += inputSeg->getNumRelocations();
198 return count;
Sam Cleggb93d75b2017-11-17 18:14:09 +0000199}
200
Rui Ueyamacc52c362019-07-11 05:40:30 +0000201void DataSection::writeRelocations(raw_ostream &os) const {
202 for (const OutputSegment *seg : segments)
203 for (const InputChunk *c : seg->inputSegments)
204 c->writeRelocations(os);
Sam Cleggb93d75b2017-11-17 18:14:09 +0000205}
Sam Clegge1964ff2018-04-10 16:12:49 +0000206
Sam Cleggaab008a2019-05-16 21:36:06 +0000207void CustomSection::finalizeContents() {
Rui Ueyamacc52c362019-07-11 05:40:30 +0000208 raw_string_ostream os(nameData);
209 encodeULEB128(name.size(), os);
210 os << name;
211 os.flush();
Sam Clegge1964ff2018-04-10 16:12:49 +0000212
Rui Ueyamacc52c362019-07-11 05:40:30 +0000213 for (InputSection *section : inputSections) {
214 section->outputOffset = payloadSize;
215 section->outputSec = this;
216 payloadSize += section->getSize();
Sam Clegge1964ff2018-04-10 16:12:49 +0000217 }
218
Rui Ueyamacc52c362019-07-11 05:40:30 +0000219 createHeader(payloadSize + nameData.size());
Sam Clegge1964ff2018-04-10 16:12:49 +0000220}
221
Rui Ueyamacc52c362019-07-11 05:40:30 +0000222void CustomSection::writeTo(uint8_t *buf) {
Sam Clegge1964ff2018-04-10 16:12:49 +0000223 log("writing " + toString(*this) + " size=" + Twine(getSize()) +
Rui Ueyamacc52c362019-07-11 05:40:30 +0000224 " chunks=" + Twine(inputSections.size()));
Sam Clegge1964ff2018-04-10 16:12:49 +0000225
Rui Ueyamacc52c362019-07-11 05:40:30 +0000226 assert(offset);
227 buf += offset;
Sam Clegge1964ff2018-04-10 16:12:49 +0000228
229 // Write section header
Rui Ueyamacc52c362019-07-11 05:40:30 +0000230 memcpy(buf, header.data(), header.size());
231 buf += header.size();
232 memcpy(buf, nameData.data(), nameData.size());
233 buf += nameData.size();
Sam Clegge1964ff2018-04-10 16:12:49 +0000234
235 // Write custom sections payload
Rui Ueyamacc52c362019-07-11 05:40:30 +0000236 for (const InputSection *section : inputSections)
237 section->writeTo(buf);
Sam Clegge1964ff2018-04-10 16:12:49 +0000238}
Sam Cleggb5c6a5a2018-05-04 23:14:42 +0000239
Rui Ueyama980a5a62019-07-10 09:10:01 +0000240uint32_t CustomSection::getNumRelocations() const {
Rui Ueyamacc52c362019-07-11 05:40:30 +0000241 uint32_t count = 0;
242 for (const InputSection *inputSect : inputSections)
243 count += inputSect->getNumRelocations();
244 return count;
Sam Cleggb5c6a5a2018-05-04 23:14:42 +0000245}
246
Rui Ueyamacc52c362019-07-11 05:40:30 +0000247void CustomSection::writeRelocations(raw_ostream &os) const {
248 for (const InputSection *s : inputSections)
249 s->writeRelocations(os);
Sam Cleggb5c6a5a2018-05-04 23:14:42 +0000250}