blob: 4123d63b74662b567d25fc3f51aca7b1b447b940 [file] [log] [blame]
Sam Cleggc94d3932017-11-17 18:14:09 +00001//===- OutputSections.cpp -------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "OutputSections.h"
Sam Clegg5fa274b2018-01-10 01:13:34 +000011#include "InputChunks.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000012#include "InputFiles.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000013#include "OutputSegment.h"
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +000014#include "WriterUtils.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000015#include "lld/Common/ErrorHandler.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000016#include "lld/Common/Threads.h"
17#include "llvm/ADT/Twine.h"
Rui Ueyama11842532018-02-16 20:38:00 +000018#include "llvm/Support/LEB128.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000019
20#define DEBUG_TYPE "lld"
21
22using namespace llvm;
23using namespace llvm::wasm;
24using namespace lld;
25using namespace lld::wasm;
26
Sam Cleggc94d3932017-11-17 18:14:09 +000027static StringRef sectionTypeToString(uint32_t SectionType) {
28 switch (SectionType) {
29 case WASM_SEC_CUSTOM:
30 return "CUSTOM";
31 case WASM_SEC_TYPE:
32 return "TYPE";
33 case WASM_SEC_IMPORT:
34 return "IMPORT";
35 case WASM_SEC_FUNCTION:
36 return "FUNCTION";
37 case WASM_SEC_TABLE:
38 return "TABLE";
39 case WASM_SEC_MEMORY:
40 return "MEMORY";
41 case WASM_SEC_GLOBAL:
42 return "GLOBAL";
Heejin Ahne915a712018-12-08 06:17:43 +000043 case WASM_SEC_EVENT:
44 return "EVENT";
Sam Cleggc94d3932017-11-17 18:14:09 +000045 case WASM_SEC_EXPORT:
46 return "EXPORT";
47 case WASM_SEC_START:
48 return "START";
49 case WASM_SEC_ELEM:
50 return "ELEM";
51 case WASM_SEC_CODE:
52 return "CODE";
53 case WASM_SEC_DATA:
54 return "DATA";
55 default:
56 fatal("invalid section type");
57 }
58}
59
Rui Ueyama22c8f332018-02-28 17:33:04 +000060// Returns a string, e.g. "FUNCTION(.text)".
61std::string lld::toString(const OutputSection &Sec) {
62 if (!Sec.Name.empty())
63 return (Sec.getSectionName() + "(" + Sec.Name + ")").str();
64 return Sec.getSectionName();
Sam Cleggc94d3932017-11-17 18:14:09 +000065}
66
Rui Ueyama22c8f332018-02-28 17:33:04 +000067StringRef OutputSection::getSectionName() const {
Sam Clegg0d0dd392017-12-19 17:09:45 +000068 return sectionTypeToString(Type);
69}
70
Sam Cleggc94d3932017-11-17 18:14:09 +000071void OutputSection::createHeader(size_t BodySize) {
72 raw_string_ostream OS(Header);
Rui Ueyama22c8f332018-02-28 17:33:04 +000073 debugWrite(OS.tell(), "section type [" + getSectionName() + "]");
Rui Ueyama11842532018-02-16 20:38:00 +000074 encodeULEB128(Type, OS);
Sam Cleggc94d3932017-11-17 18:14:09 +000075 writeUleb128(OS, BodySize, "section size");
76 OS.flush();
Sam Cleggab2ac292017-12-20 05:14:48 +000077 log("createHeader: " + toString(*this) + " body=" + Twine(BodySize) +
Sam Cleggc94d3932017-11-17 18:14:09 +000078 " total=" + Twine(getSize()));
79}
80
Sam Clegg8d146bb2018-01-09 23:56:44 +000081CodeSection::CodeSection(ArrayRef<InputFunction *> Functions)
82 : OutputSection(WASM_SEC_CODE), Functions(Functions) {
83 assert(Functions.size() > 0);
84
Sam Cleggc94d3932017-11-17 18:14:09 +000085 raw_string_ostream OS(CodeSectionHeader);
Sam Clegg8d146bb2018-01-09 23:56:44 +000086 writeUleb128(OS, Functions.size(), "function count");
Sam Cleggc94d3932017-11-17 18:14:09 +000087 OS.flush();
88 BodySize = CodeSectionHeader.size();
89
Sam Cleggfb983cd2018-05-18 23:28:05 +000090 for (InputFunction *Func : Functions) {
Rui Ueyamabf450d92018-02-20 04:26:26 +000091 Func->OutputOffset = BodySize;
Sam Cleggfb983cd2018-05-18 23:28:05 +000092 Func->calculateSize();
Sam Clegg5fa274b2018-01-10 01:13:34 +000093 BodySize += Func->getSize();
Sam Cleggc94d3932017-11-17 18:14:09 +000094 }
95
96 createHeader(BodySize);
97}
98
99void CodeSection::writeTo(uint8_t *Buf) {
Sam Cleggab2ac292017-12-20 05:14:48 +0000100 log("writing " + toString(*this));
Sam Cleggc94d3932017-11-17 18:14:09 +0000101 log(" size=" + Twine(getSize()));
Sam Clegg8d146bb2018-01-09 23:56:44 +0000102 log(" headersize=" + Twine(Header.size()));
103 log(" codeheadersize=" + Twine(CodeSectionHeader.size()));
Sam Cleggc94d3932017-11-17 18:14:09 +0000104 Buf += Offset;
105
106 // Write section header
107 memcpy(Buf, Header.data(), Header.size());
108 Buf += Header.size();
109
Sam Cleggc94d3932017-11-17 18:14:09 +0000110 // Write code section headers
111 memcpy(Buf, CodeSectionHeader.data(), CodeSectionHeader.size());
Sam Cleggc94d3932017-11-17 18:14:09 +0000112
113 // Write code section bodies
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000114 parallelForEach(Functions,
115 [&](const InputChunk *Chunk) { Chunk->writeTo(Buf); });
Sam Cleggc94d3932017-11-17 18:14:09 +0000116}
117
118uint32_t CodeSection::numRelocations() const {
119 uint32_t Count = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000120 for (const InputChunk *Func : Functions)
Rui Ueyamabf450d92018-02-20 04:26:26 +0000121 Count += Func->NumRelocations();
Sam Cleggc94d3932017-11-17 18:14:09 +0000122 return Count;
123}
124
125void CodeSection::writeRelocations(raw_ostream &OS) const {
Rui Ueyamabf450d92018-02-20 04:26:26 +0000126 for (const InputChunk *C : Functions)
127 C->writeRelocations(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000128}
129
Sam Clegg0fb6faa2017-12-08 01:09:21 +0000130DataSection::DataSection(ArrayRef<OutputSegment *> Segments)
Sam Cleggc94d3932017-11-17 18:14:09 +0000131 : OutputSection(WASM_SEC_DATA), Segments(Segments) {
132 raw_string_ostream OS(DataSectionHeader);
133
134 writeUleb128(OS, Segments.size(), "data segment count");
135 OS.flush();
136 BodySize = DataSectionHeader.size();
137
138 for (OutputSegment *Segment : Segments) {
139 raw_string_ostream OS(Segment->Header);
140 writeUleb128(OS, 0, "memory index");
Sam Cleggbfb75342018-11-15 00:37:21 +0000141 WasmInitExpr InitExpr;
142 if (Config->Pic) {
143 assert(Segments.size() <= 1 &&
144 "Currenly only a single data segment is supported in PIC mode");
Thomas Lively25ff8932019-01-08 06:25:55 +0000145 InitExpr.Opcode = WASM_OPCODE_GLOBAL_GET;
Sam Clegg2dad4e22018-11-15 18:15:54 +0000146 InitExpr.Value.Global = WasmSym::MemoryBase->getGlobalIndex();
Sam Cleggbfb75342018-11-15 00:37:21 +0000147 } else {
148 InitExpr.Opcode = WASM_OPCODE_I32_CONST;
149 InitExpr.Value.Int32 = Segment->StartVA;
150 }
151 writeInitExpr(OS, InitExpr);
Sam Cleggc94d3932017-11-17 18:14:09 +0000152 writeUleb128(OS, Segment->Size, "segment size");
153 OS.flush();
Rui Ueyamaac95bb12018-04-05 19:37:31 +0000154
155 Segment->SectionOffset = BodySize;
Sam Cleggd96d9352018-01-10 19:22:42 +0000156 BodySize += Segment->Header.size() + Segment->Size;
Sam Cleggc94d3932017-11-17 18:14:09 +0000157 log("Data segment: size=" + Twine(Segment->Size));
Rui Ueyamaac95bb12018-04-05 19:37:31 +0000158
Sam Cleggd96d9352018-01-10 19:22:42 +0000159 for (InputSegment *InputSeg : Segment->InputSegments)
Rui Ueyamaac95bb12018-04-05 19:37:31 +0000160 InputSeg->OutputOffset = Segment->SectionOffset + Segment->Header.size() +
Rui Ueyamabf450d92018-02-20 04:26:26 +0000161 InputSeg->OutputSegmentOffset;
Sam Cleggc94d3932017-11-17 18:14:09 +0000162 }
163
164 createHeader(BodySize);
165}
166
167void DataSection::writeTo(uint8_t *Buf) {
Sam Cleggab2ac292017-12-20 05:14:48 +0000168 log("writing " + toString(*this) + " size=" + Twine(getSize()) +
Sam Cleggc94d3932017-11-17 18:14:09 +0000169 " body=" + Twine(BodySize));
170 Buf += Offset;
171
172 // Write section header
173 memcpy(Buf, Header.data(), Header.size());
174 Buf += Header.size();
175
Sam Cleggc94d3932017-11-17 18:14:09 +0000176 // Write data section headers
177 memcpy(Buf, DataSectionHeader.data(), DataSectionHeader.size());
178
Rui Ueyama319eb8b2018-02-28 00:31:16 +0000179 parallelForEach(Segments, [&](const OutputSegment *Segment) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000180 // Write data segment header
Rui Ueyamaac95bb12018-04-05 19:37:31 +0000181 uint8_t *SegStart = Buf + Segment->SectionOffset;
Sam Cleggc94d3932017-11-17 18:14:09 +0000182 memcpy(SegStart, Segment->Header.data(), Segment->Header.size());
183
184 // Write segment data payload
Sam Cleggd96d9352018-01-10 19:22:42 +0000185 for (const InputChunk *Chunk : Segment->InputSegments)
Rui Ueyama319eb8b2018-02-28 00:31:16 +0000186 Chunk->writeTo(Buf);
Sam Cleggc94d3932017-11-17 18:14:09 +0000187 });
Sam Clegg5e8cba92017-12-19 20:45:15 +0000188}
Sam Cleggc94d3932017-11-17 18:14:09 +0000189
Sam Clegg5e8cba92017-12-19 20:45:15 +0000190uint32_t DataSection::numRelocations() const {
191 uint32_t Count = 0;
192 for (const OutputSegment *Seg : Segments)
Sam Clegg5fa274b2018-01-10 01:13:34 +0000193 for (const InputChunk *InputSeg : Seg->InputSegments)
Rui Ueyamabf450d92018-02-20 04:26:26 +0000194 Count += InputSeg->NumRelocations();
Sam Clegg5e8cba92017-12-19 20:45:15 +0000195 return Count;
Sam Cleggc94d3932017-11-17 18:14:09 +0000196}
197
198void DataSection::writeRelocations(raw_ostream &OS) const {
Sam Clegg5e8cba92017-12-19 20:45:15 +0000199 for (const OutputSegment *Seg : Segments)
Rui Ueyamabf450d92018-02-20 04:26:26 +0000200 for (const InputChunk *C : Seg->InputSegments)
201 C->writeRelocations(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000202}
Sam Clegg80ba4382018-04-10 16:12:49 +0000203
204CustomSection::CustomSection(std::string Name,
205 ArrayRef<InputSection *> InputSections)
206 : OutputSection(WASM_SEC_CUSTOM, Name), PayloadSize(0),
207 InputSections(InputSections) {
208 raw_string_ostream OS(NameData);
209 encodeULEB128(Name.size(), OS);
210 OS << Name;
211 OS.flush();
212
213 for (InputSection *Section : InputSections) {
214 Section->OutputOffset = PayloadSize;
215 PayloadSize += Section->getSize();
216 }
217
218 createHeader(PayloadSize + NameData.size());
219}
220
221void CustomSection::writeTo(uint8_t *Buf) {
222 log("writing " + toString(*this) + " size=" + Twine(getSize()) +
223 " chunks=" + Twine(InputSections.size()));
224
225 assert(Offset);
226 Buf += Offset;
227
228 // Write section header
229 memcpy(Buf, Header.data(), Header.size());
230 Buf += Header.size();
231 memcpy(Buf, NameData.data(), NameData.size());
232 Buf += NameData.size();
233
234 // Write custom sections payload
235 parallelForEach(InputSections,
236 [&](const InputSection *Section) { Section->writeTo(Buf); });
237}
Sam Cleggd177ab22018-05-04 23:14:42 +0000238
239uint32_t CustomSection::numRelocations() const {
240 uint32_t Count = 0;
241 for (const InputSection *InputSect : InputSections)
242 Count += InputSect->NumRelocations();
243 return Count;
244}
245
246void CustomSection::writeRelocations(raw_ostream &OS) const {
247 for (const InputSection *S : InputSections)
248 S->writeRelocations(OS);
249}