blob: 77f727902837963d7a7aa5258ec2c55f00072d78 [file] [log] [blame]
Sam Cleggc94d3932017-11-17 18:14:09 +00001//===- OutputSections.cpp -------------------------------------------------===//
2//
Chandler Carruth2946cd72019-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 Cleggc94d3932017-11-17 18:14:09 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "OutputSections.h"
Sam Clegg5fa274b2018-01-10 01:13:34 +000010#include "InputChunks.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000011#include "InputFiles.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000012#include "OutputSegment.h"
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +000013#include "WriterUtils.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000014#include "lld/Common/ErrorHandler.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000015#include "lld/Common/Threads.h"
16#include "llvm/ADT/Twine.h"
Rui Ueyama11842532018-02-16 20:38:00 +000017#include "llvm/Support/LEB128.h"
Sam Cleggc94d3932017-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
Sam Cleggc94d3932017-11-17 18:14:09 +000026static StringRef sectionTypeToString(uint32_t SectionType) {
27 switch (SectionType) {
28 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 Ahne915a712018-12-08 06:17:43 +000042 case WASM_SEC_EVENT:
43 return "EVENT";
Sam Cleggc94d3932017-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 Lively84771e22019-04-19 23:40:36 +000054 case WASM_SEC_DATACOUNT:
55 return "DATACOUNT";
Sam Cleggc94d3932017-11-17 18:14:09 +000056 default:
57 fatal("invalid section type");
58 }
59}
60
Rui Ueyama22c8f332018-02-28 17:33:04 +000061// Returns a string, e.g. "FUNCTION(.text)".
62std::string lld::toString(const OutputSection &Sec) {
63 if (!Sec.Name.empty())
64 return (Sec.getSectionName() + "(" + Sec.Name + ")").str();
65 return Sec.getSectionName();
Sam Cleggc94d3932017-11-17 18:14:09 +000066}
67
Rui Ueyama22c8f332018-02-28 17:33:04 +000068StringRef OutputSection::getSectionName() const {
Sam Clegg0d0dd392017-12-19 17:09:45 +000069 return sectionTypeToString(Type);
70}
71
Sam Cleggc94d3932017-11-17 18:14:09 +000072void OutputSection::createHeader(size_t BodySize) {
73 raw_string_ostream OS(Header);
Rui Ueyama22c8f332018-02-28 17:33:04 +000074 debugWrite(OS.tell(), "section type [" + getSectionName() + "]");
Rui Ueyama11842532018-02-16 20:38:00 +000075 encodeULEB128(Type, OS);
Sam Cleggc94d3932017-11-17 18:14:09 +000076 writeUleb128(OS, BodySize, "section size");
77 OS.flush();
Sam Cleggab2ac292017-12-20 05:14:48 +000078 log("createHeader: " + toString(*this) + " body=" + Twine(BodySize) +
Sam Cleggc94d3932017-11-17 18:14:09 +000079 " total=" + Twine(getSize()));
80}
81
Sam Cleggd029bf02019-05-16 21:36:06 +000082void CodeSection::finalizeContents() {
Sam Cleggc94d3932017-11-17 18:14:09 +000083 raw_string_ostream OS(CodeSectionHeader);
Sam Clegg8d146bb2018-01-09 23:56:44 +000084 writeUleb128(OS, Functions.size(), "function count");
Sam Cleggc94d3932017-11-17 18:14:09 +000085 OS.flush();
86 BodySize = CodeSectionHeader.size();
87
Sam Cleggfb983cd2018-05-18 23:28:05 +000088 for (InputFunction *Func : Functions) {
Rui Ueyamabf450d92018-02-20 04:26:26 +000089 Func->OutputOffset = BodySize;
Sam Cleggfb983cd2018-05-18 23:28:05 +000090 Func->calculateSize();
Sam Clegg5fa274b2018-01-10 01:13:34 +000091 BodySize += Func->getSize();
Sam Cleggc94d3932017-11-17 18:14:09 +000092 }
93
94 createHeader(BodySize);
95}
96
97void CodeSection::writeTo(uint8_t *Buf) {
Sam Cleggab2ac292017-12-20 05:14:48 +000098 log("writing " + toString(*this));
Sam Cleggc94d3932017-11-17 18:14:09 +000099 log(" size=" + Twine(getSize()));
Sam Clegg8d146bb2018-01-09 23:56:44 +0000100 log(" headersize=" + Twine(Header.size()));
101 log(" codeheadersize=" + Twine(CodeSectionHeader.size()));
Sam Cleggc94d3932017-11-17 18:14:09 +0000102 Buf += Offset;
103
104 // Write section header
105 memcpy(Buf, Header.data(), Header.size());
106 Buf += Header.size();
107
Sam Cleggc94d3932017-11-17 18:14:09 +0000108 // Write code section headers
109 memcpy(Buf, CodeSectionHeader.data(), CodeSectionHeader.size());
Sam Cleggc94d3932017-11-17 18:14:09 +0000110
111 // Write code section bodies
Rui Ueyama5081e412019-04-17 02:12:47 +0000112 for (const InputChunk *Chunk : Functions)
113 Chunk->writeTo(Buf);
Sam Cleggc94d3932017-11-17 18:14:09 +0000114}
115
116uint32_t CodeSection::numRelocations() const {
117 uint32_t Count = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000118 for (const InputChunk *Func : Functions)
Rui Ueyamabf450d92018-02-20 04:26:26 +0000119 Count += Func->NumRelocations();
Sam Cleggc94d3932017-11-17 18:14:09 +0000120 return Count;
121}
122
123void CodeSection::writeRelocations(raw_ostream &OS) const {
Rui Ueyamabf450d92018-02-20 04:26:26 +0000124 for (const InputChunk *C : Functions)
125 C->writeRelocations(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000126}
127
Sam Cleggd029bf02019-05-16 21:36:06 +0000128void DataSection::finalizeContents() {
Sam Cleggc94d3932017-11-17 18:14:09 +0000129 raw_string_ostream OS(DataSectionHeader);
130
131 writeUleb128(OS, Segments.size(), "data segment count");
132 OS.flush();
133 BodySize = DataSectionHeader.size();
134
Bill Wendling858e3512019-07-08 22:05:02 +0000135 assert((!Config->Pic || Segments.size() <= 1) &&
136 "Currenly only a single data segment is supported in PIC mode");
Thomas Lively6004d9a2019-07-03 22:04:54 +0000137
Sam Cleggc94d3932017-11-17 18:14:09 +0000138 for (OutputSegment *Segment : Segments) {
139 raw_string_ostream OS(Segment->Header);
Thomas Lively6004d9a2019-07-03 22:04:54 +0000140 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->Pic) {
146 InitExpr.Opcode = WASM_OPCODE_GLOBAL_GET;
147 InitExpr.Value.Global = WasmSym::MemoryBase->getGlobalIndex();
148 } else {
149 InitExpr.Opcode = WASM_OPCODE_I32_CONST;
150 InitExpr.Value.Int32 = Segment->StartVA;
151 }
152 writeInitExpr(OS, InitExpr);
Sam Cleggbfb75342018-11-15 00:37:21 +0000153 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000154 writeUleb128(OS, Segment->Size, "segment size");
155 OS.flush();
Rui Ueyamaac95bb12018-04-05 19:37:31 +0000156
157 Segment->SectionOffset = BodySize;
Sam Cleggd96d9352018-01-10 19:22:42 +0000158 BodySize += Segment->Header.size() + Segment->Size;
Thomas Livelyb98025a2019-06-04 21:13:41 +0000159 log("Data segment: size=" + Twine(Segment->Size) + ", startVA=" +
160 Twine::utohexstr(Segment->StartVA) + ", name=" + Segment->Name);
Rui Ueyamaac95bb12018-04-05 19:37:31 +0000161
Sam Cleggd96d9352018-01-10 19:22:42 +0000162 for (InputSegment *InputSeg : Segment->InputSegments)
Rui Ueyamaac95bb12018-04-05 19:37:31 +0000163 InputSeg->OutputOffset = Segment->SectionOffset + Segment->Header.size() +
Rui Ueyamabf450d92018-02-20 04:26:26 +0000164 InputSeg->OutputSegmentOffset;
Sam Cleggc94d3932017-11-17 18:14:09 +0000165 }
166
167 createHeader(BodySize);
168}
169
170void DataSection::writeTo(uint8_t *Buf) {
Sam Cleggab2ac292017-12-20 05:14:48 +0000171 log("writing " + toString(*this) + " size=" + Twine(getSize()) +
Sam Cleggc94d3932017-11-17 18:14:09 +0000172 " body=" + Twine(BodySize));
173 Buf += Offset;
174
175 // Write section header
176 memcpy(Buf, Header.data(), Header.size());
177 Buf += Header.size();
178
Sam Cleggc94d3932017-11-17 18:14:09 +0000179 // Write data section headers
180 memcpy(Buf, DataSectionHeader.data(), DataSectionHeader.size());
181
Rui Ueyama5081e412019-04-17 02:12:47 +0000182 for (const OutputSegment *Segment : Segments) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000183 // Write data segment header
Rui Ueyamaac95bb12018-04-05 19:37:31 +0000184 uint8_t *SegStart = Buf + Segment->SectionOffset;
Sam Cleggc94d3932017-11-17 18:14:09 +0000185 memcpy(SegStart, Segment->Header.data(), Segment->Header.size());
186
187 // Write segment data payload
Sam Cleggd96d9352018-01-10 19:22:42 +0000188 for (const InputChunk *Chunk : Segment->InputSegments)
Rui Ueyama319eb8b2018-02-28 00:31:16 +0000189 Chunk->writeTo(Buf);
Rui Ueyama5081e412019-04-17 02:12:47 +0000190 }
Sam Clegg5e8cba92017-12-19 20:45:15 +0000191}
Sam Cleggc94d3932017-11-17 18:14:09 +0000192
Sam Clegg5e8cba92017-12-19 20:45:15 +0000193uint32_t DataSection::numRelocations() const {
194 uint32_t Count = 0;
195 for (const OutputSegment *Seg : Segments)
Sam Clegg5fa274b2018-01-10 01:13:34 +0000196 for (const InputChunk *InputSeg : Seg->InputSegments)
Rui Ueyamabf450d92018-02-20 04:26:26 +0000197 Count += InputSeg->NumRelocations();
Sam Clegg5e8cba92017-12-19 20:45:15 +0000198 return Count;
Sam Cleggc94d3932017-11-17 18:14:09 +0000199}
200
201void DataSection::writeRelocations(raw_ostream &OS) const {
Sam Clegg5e8cba92017-12-19 20:45:15 +0000202 for (const OutputSegment *Seg : Segments)
Rui Ueyamabf450d92018-02-20 04:26:26 +0000203 for (const InputChunk *C : Seg->InputSegments)
204 C->writeRelocations(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000205}
Sam Clegg80ba4382018-04-10 16:12:49 +0000206
Sam Cleggd029bf02019-05-16 21:36:06 +0000207void CustomSection::finalizeContents() {
Sam Clegg80ba4382018-04-10 16:12:49 +0000208 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;
Sam Clegg8fcf0122019-05-21 09:13:09 +0000215 Section->OutputSec = this;
Sam Clegg80ba4382018-04-10 16:12:49 +0000216 PayloadSize += Section->getSize();
217 }
218
219 createHeader(PayloadSize + NameData.size());
220}
221
222void CustomSection::writeTo(uint8_t *Buf) {
223 log("writing " + toString(*this) + " size=" + Twine(getSize()) +
224 " chunks=" + Twine(InputSections.size()));
225
226 assert(Offset);
227 Buf += Offset;
228
229 // Write section header
230 memcpy(Buf, Header.data(), Header.size());
231 Buf += Header.size();
232 memcpy(Buf, NameData.data(), NameData.size());
233 Buf += NameData.size();
234
235 // Write custom sections payload
Rui Ueyama5081e412019-04-17 02:12:47 +0000236 for (const InputSection *Section : InputSections)
237 Section->writeTo(Buf);
Sam Clegg80ba4382018-04-10 16:12:49 +0000238}
Sam Cleggd177ab22018-05-04 23:14:42 +0000239
240uint32_t CustomSection::numRelocations() const {
241 uint32_t Count = 0;
242 for (const InputSection *InputSect : InputSections)
243 Count += InputSect->NumRelocations();
244 return Count;
245}
246
247void CustomSection::writeRelocations(raw_ostream &OS) const {
248 for (const InputSection *S : InputSections)
249 S->writeRelocations(OS);
250}