blob: 346ed7f89f08f3dd87c62c13754e6bf48a1949f8 [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"
11
12#include "Config.h"
13#include "InputFiles.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000014#include "OutputSegment.h"
15#include "SymbolTable.h"
16#include "lld/Common/ErrorHandler.h"
Rui Ueyama2017d522017-11-28 20:39:17 +000017#include "lld/Common/Memory.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000018#include "lld/Common/Threads.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/Support/LEB128.h"
21
22#define DEBUG_TYPE "lld"
23
24using namespace llvm;
25using namespace llvm::wasm;
26using namespace lld;
27using namespace lld::wasm;
28
29enum class RelocEncoding {
30 Uleb128,
31 Sleb128,
32 I32,
33};
34
35static StringRef sectionTypeToString(uint32_t SectionType) {
36 switch (SectionType) {
37 case WASM_SEC_CUSTOM:
38 return "CUSTOM";
39 case WASM_SEC_TYPE:
40 return "TYPE";
41 case WASM_SEC_IMPORT:
42 return "IMPORT";
43 case WASM_SEC_FUNCTION:
44 return "FUNCTION";
45 case WASM_SEC_TABLE:
46 return "TABLE";
47 case WASM_SEC_MEMORY:
48 return "MEMORY";
49 case WASM_SEC_GLOBAL:
50 return "GLOBAL";
51 case WASM_SEC_EXPORT:
52 return "EXPORT";
53 case WASM_SEC_START:
54 return "START";
55 case WASM_SEC_ELEM:
56 return "ELEM";
57 case WASM_SEC_CODE:
58 return "CODE";
59 case WASM_SEC_DATA:
60 return "DATA";
61 default:
62 fatal("invalid section type");
63 }
64}
65
66std::string lld::toString(OutputSection *Section) {
67 std::string rtn = sectionTypeToString(Section->Type);
68 if (!Section->Name.empty())
69 rtn += "(" + Section->Name + ")";
70 return rtn;
71}
72
73static void applyRelocation(uint8_t *Buf, const OutputRelocation &Reloc) {
74 DEBUG(dbgs() << "write reloc: type=" << Reloc.Reloc.Type
Sam Clegg74fe0ba2017-12-07 01:51:24 +000075 << " index=" << Reloc.Reloc.Index << " value=" << Reloc.Value
76 << " offset=" << Reloc.Reloc.Offset << "\n");
Sam Cleggc94d3932017-11-17 18:14:09 +000077 Buf += Reloc.Reloc.Offset;
78 int64_t ExistingValue;
79 switch (Reloc.Reloc.Type) {
80 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
81 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
82 ExistingValue = decodeULEB128(Buf);
83 if (ExistingValue != Reloc.Reloc.Index) {
84 DEBUG(dbgs() << "existing value: " << decodeULEB128(Buf) << "\n");
85 assert(decodeULEB128(Buf) == Reloc.Reloc.Index);
86 }
87 LLVM_FALLTHROUGH;
88 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
89 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
90 encodeULEB128(Reloc.Value, Buf, 5);
91 break;
92 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
93 ExistingValue = decodeSLEB128(Buf);
94 if (ExistingValue != Reloc.Reloc.Index) {
95 DEBUG(dbgs() << "existing value: " << decodeSLEB128(Buf) << "\n");
96 assert(decodeSLEB128(Buf) == Reloc.Reloc.Index);
97 }
98 LLVM_FALLTHROUGH;
99 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
100 encodeSLEB128(static_cast<int32_t>(Reloc.Value), Buf, 5);
101 break;
102 case R_WEBASSEMBLY_TABLE_INDEX_I32:
103 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
104 support::endian::write32<support::little>(Buf, Reloc.Value);
105 break;
106 default:
107 llvm_unreachable("unknown relocation type");
108 }
109}
110
Sam Cleggca16b732017-12-18 20:20:24 +0000111static void applyRelocations(uint8_t *Buf, ArrayRef<OutputRelocation> Relocs) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000112 log("applyRelocations: count=" + Twine(Relocs.size()));
113 for (const OutputRelocation &Reloc : Relocs) {
114 applyRelocation(Buf, Reloc);
115 }
116}
117
118// Relocations contain an index into the function, global or table index
119// space of the input file. This function takes a relocation and returns the
120// relocated index (i.e. translates from the input index space to the output
121// index space).
122static uint32_t calcNewIndex(const ObjFile &File, const WasmRelocation &Reloc) {
123 switch (Reloc.Type) {
124 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
125 return File.relocateTypeIndex(Reloc.Index);
126 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
127 return File.relocateFunctionIndex(Reloc.Index);
128 case R_WEBASSEMBLY_TABLE_INDEX_I32:
129 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
130 return File.relocateTableIndex(Reloc.Index);
131 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
132 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
133 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
134 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
135 return File.relocateGlobalIndex(Reloc.Index);
136 default:
137 llvm_unreachable("unknown relocation type");
138 }
139}
140
141// Take a vector of relocations from an input file and create output
142// relocations based on them. Calculates the updated index and offset for
143// each relocation as well as the value to write out in the final binary.
144static void calcRelocations(const ObjFile &File,
145 ArrayRef<WasmRelocation> Relocs,
146 std::vector<OutputRelocation> &OutputRelocs,
147 int32_t OutputOffset) {
148 log("calcRelocations: " + File.getName() + " offset=" + Twine(OutputOffset));
149 for (const WasmRelocation &Reloc : Relocs) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000150 OutputRelocation NewReloc;
151 NewReloc.Reloc = Reloc;
152 NewReloc.Reloc.Offset += OutputOffset;
Sam Cleggc94d3932017-11-17 18:14:09 +0000153 DEBUG(dbgs() << "reloc: type=" << Reloc.Type << " index=" << Reloc.Index
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000154 << " offset=" << Reloc.Offset
Sam Cleggc94d3932017-11-17 18:14:09 +0000155 << " newOffset=" << NewReloc.Reloc.Offset << "\n");
156
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000157 if (Config->EmitRelocs)
158 NewReloc.NewIndex = calcNewIndex(File, Reloc);
159 else
160 NewReloc.NewIndex = UINT32_MAX;
161
Sam Cleggc94d3932017-11-17 18:14:09 +0000162 switch (Reloc.Type) {
163 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
164 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
165 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
166 NewReloc.Value = File.getRelocatedAddress(Reloc.Index);
167 if (NewReloc.Value != UINT32_MAX)
168 NewReloc.Value += Reloc.Addend;
169 break;
170 default:
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000171 NewReloc.Value = calcNewIndex(File, Reloc);
172 break;
Sam Cleggc94d3932017-11-17 18:14:09 +0000173 }
174
175 OutputRelocs.emplace_back(NewReloc);
176 }
177}
178
Sam Clegg0d0dd392017-12-19 17:09:45 +0000179std::string OutputSection::getSectionName() {
180 return sectionTypeToString(Type);
181}
182
183std::string SubSection::getSectionName() {
184 return std::string("subsection <type=") + std::to_string(Type) + ">";
185}
186
Sam Cleggc94d3932017-11-17 18:14:09 +0000187void OutputSection::createHeader(size_t BodySize) {
188 raw_string_ostream OS(Header);
Sam Clegg0d0dd392017-12-19 17:09:45 +0000189 debugWrite(OS.tell(), "section type [" + Twine(getSectionName()) + "]");
Sam Cleggc94d3932017-11-17 18:14:09 +0000190 writeUleb128(OS, Type, nullptr);
191 writeUleb128(OS, BodySize, "section size");
192 OS.flush();
193 log("createHeader: " + toString(this) + " body=" + Twine(BodySize) +
194 " total=" + Twine(getSize()));
195}
196
Sam Clegg0fb6faa2017-12-08 01:09:21 +0000197CodeSection::CodeSection(uint32_t NumFunctions, ArrayRef<ObjFile *> Objs)
Sam Cleggc94d3932017-11-17 18:14:09 +0000198 : OutputSection(WASM_SEC_CODE), InputObjects(Objs) {
199 raw_string_ostream OS(CodeSectionHeader);
200 writeUleb128(OS, NumFunctions, "function count");
201 OS.flush();
202 BodySize = CodeSectionHeader.size();
203
204 for (ObjFile *File : InputObjects) {
205 if (!File->CodeSection)
206 continue;
207
208 File->CodeOffset = BodySize;
209 ArrayRef<uint8_t> Content = File->CodeSection->Content;
210 unsigned HeaderSize = 0;
211 decodeULEB128(Content.data(), &HeaderSize);
212
213 calcRelocations(*File, File->CodeSection->Relocations,
214 File->CodeRelocations, BodySize - HeaderSize);
215
216 size_t PayloadSize = Content.size() - HeaderSize;
217 BodySize += PayloadSize;
218 }
219
220 createHeader(BodySize);
221}
222
223void CodeSection::writeTo(uint8_t *Buf) {
224 log("writing " + toString(this));
225 log(" size=" + Twine(getSize()));
226 Buf += Offset;
227
228 // Write section header
229 memcpy(Buf, Header.data(), Header.size());
230 Buf += Header.size();
231
232 uint8_t *ContentsStart = Buf;
233
234 // Write code section headers
235 memcpy(Buf, CodeSectionHeader.data(), CodeSectionHeader.size());
236 Buf += CodeSectionHeader.size();
237
238 // Write code section bodies
239 parallelForEach(InputObjects, [ContentsStart](ObjFile *File) {
240 if (!File->CodeSection)
241 return;
242
243 ArrayRef<uint8_t> Content(File->CodeSection->Content);
244
245 // Payload doesn't include the initial header (function count)
246 unsigned HeaderSize = 0;
247 decodeULEB128(Content.data(), &HeaderSize);
248
249 size_t PayloadSize = Content.size() - HeaderSize;
250 memcpy(ContentsStart + File->CodeOffset, Content.data() + HeaderSize,
251 PayloadSize);
252
253 log("applying relocations for: " + File->getName());
254 if (File->CodeRelocations.size())
255 applyRelocations(ContentsStart, File->CodeRelocations);
256 });
257}
258
259uint32_t CodeSection::numRelocations() const {
260 uint32_t Count = 0;
261 for (ObjFile *File : InputObjects)
262 Count += File->CodeRelocations.size();
263 return Count;
264}
265
266void CodeSection::writeRelocations(raw_ostream &OS) const {
267 for (ObjFile *File : InputObjects)
268 for (const OutputRelocation &Reloc : File->CodeRelocations)
269 writeReloc(OS, Reloc);
270}
271
Sam Clegg0fb6faa2017-12-08 01:09:21 +0000272DataSection::DataSection(ArrayRef<OutputSegment *> Segments)
Sam Cleggc94d3932017-11-17 18:14:09 +0000273 : OutputSection(WASM_SEC_DATA), Segments(Segments) {
274 raw_string_ostream OS(DataSectionHeader);
275
276 writeUleb128(OS, Segments.size(), "data segment count");
277 OS.flush();
278 BodySize = DataSectionHeader.size();
279
280 for (OutputSegment *Segment : Segments) {
281 raw_string_ostream OS(Segment->Header);
282 writeUleb128(OS, 0, "memory index");
283 writeUleb128(OS, WASM_OPCODE_I32_CONST, "opcode:i32const");
284 writeSleb128(OS, Segment->StartVA, "memory offset");
285 writeUleb128(OS, WASM_OPCODE_END, "opcode:end");
286 writeUleb128(OS, Segment->Size, "segment size");
287 OS.flush();
288 Segment->setSectionOffset(BodySize);
289 BodySize += Segment->Header.size();
290 log("Data segment: size=" + Twine(Segment->Size));
291 for (const InputSegment *InputSeg : Segment->InputSegments) {
292 uint32_t InputOffset = InputSeg->getInputSectionOffset();
293 uint32_t OutputOffset = Segment->getSectionOffset() +
294 Segment->Header.size() +
295 InputSeg->getOutputSegmentOffset();
296 calcRelocations(*InputSeg->File, InputSeg->Relocations, Relocations,
297 OutputOffset - InputOffset);
298 }
299 BodySize += Segment->Size;
300 }
301
302 createHeader(BodySize);
303}
304
305void DataSection::writeTo(uint8_t *Buf) {
306 log("writing " + toString(this) + " size=" + Twine(getSize()) +
307 " body=" + Twine(BodySize));
308 Buf += Offset;
309
310 // Write section header
311 memcpy(Buf, Header.data(), Header.size());
312 Buf += Header.size();
313
314 uint8_t *ContentsStart = Buf;
315
316 // Write data section headers
317 memcpy(Buf, DataSectionHeader.data(), DataSectionHeader.size());
318
319 parallelForEach(Segments, [ContentsStart](const OutputSegment *Segment) {
320 // Write data segment header
321 uint8_t *SegStart = ContentsStart + Segment->getSectionOffset();
322 memcpy(SegStart, Segment->Header.data(), Segment->Header.size());
323
324 // Write segment data payload
325 for (const InputSegment *Input : Segment->InputSegments) {
326 ArrayRef<uint8_t> Content(Input->Segment->Data.Content);
327 memcpy(SegStart + Segment->Header.size() +
328 Input->getOutputSegmentOffset(),
329 Content.data(), Content.size());
330 }
331 });
332
333 applyRelocations(ContentsStart, Relocations);
334}
335
336void DataSection::writeRelocations(raw_ostream &OS) const {
337 for (const OutputRelocation &Reloc : Relocations)
338 writeReloc(OS, Reloc);
339}