blob: 828bd53bf5a00f5fac431565772d2fe8cac0f7e5 [file] [log] [blame]
Sam Cleggc94d3932017-11-17 18:14:09 +00001//===- Writer.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 "Writer.h"
11
12#include "Config.h"
Sam Clegg8d146bb2018-01-09 23:56:44 +000013#include "InputFunction.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000014#include "OutputSections.h"
15#include "OutputSegment.h"
16#include "SymbolTable.h"
17#include "WriterUtils.h"
18#include "lld/Common/ErrorHandler.h"
Rui Ueyama2017d522017-11-28 20:39:17 +000019#include "lld/Common/Memory.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000020#include "lld/Common/Threads.h"
21#include "llvm/Support/FileOutputBuffer.h"
22#include "llvm/Support/Format.h"
23#include "llvm/Support/FormatVariadic.h"
24#include "llvm/Support/LEB128.h"
25
26#include <cstdarg>
27
28#define DEBUG_TYPE "lld"
29
30using namespace llvm;
31using namespace llvm::wasm;
32using namespace lld;
33using namespace lld::wasm;
34
35static constexpr int kStackAlignment = 16;
36
37namespace {
38
Sam Cleggc94d3932017-11-17 18:14:09 +000039// Traits for using WasmSignature in a DenseMap.
40struct WasmSignatureDenseMapInfo {
41 static WasmSignature getEmptyKey() {
42 WasmSignature Sig;
43 Sig.ReturnType = 1;
44 return Sig;
45 }
46 static WasmSignature getTombstoneKey() {
47 WasmSignature Sig;
48 Sig.ReturnType = 2;
49 return Sig;
50 }
51 static unsigned getHashValue(const WasmSignature &Sig) {
52 uintptr_t Value = 0;
53 Value += DenseMapInfo<int32_t>::getHashValue(Sig.ReturnType);
54 for (int32_t Param : Sig.ParamTypes)
55 Value += DenseMapInfo<int32_t>::getHashValue(Param);
56 return Value;
57 }
58 static bool isEqual(const WasmSignature &LHS, const WasmSignature &RHS) {
59 return LHS == RHS;
60 }
61};
62
63// The writer writes a SymbolTable result to a file.
64class Writer {
65public:
66 void run();
67
68private:
69 void openFile();
70
Sam Cleggb8621592017-11-30 01:40:08 +000071 uint32_t getTypeIndex(const WasmSignature &Sig);
Sam Clegg8d146bb2018-01-09 23:56:44 +000072 void assignIndexes();
Sam Cleggc94d3932017-11-17 18:14:09 +000073 void calculateImports();
74 void calculateOffsets();
75 void calculateTypes();
76 void createOutputSegments();
77 void layoutMemory();
78 void createHeader();
79 void createSections();
80 SyntheticSection *createSyntheticSection(uint32_t Type,
81 std::string Name = "");
82
83 // Builtin sections
84 void createTypeSection();
85 void createFunctionSection();
86 void createTableSection();
87 void createGlobalSection();
88 void createExportSection();
89 void createImportSection();
90 void createMemorySection();
91 void createElemSection();
92 void createStartSection();
93 void createCodeSection();
94 void createDataSection();
95
96 // Custom sections
97 void createRelocSections();
98 void createLinkingSection();
99 void createNameSection();
100
101 void writeHeader();
102 void writeSections();
103
104 uint64_t FileSize = 0;
105 uint32_t DataSize = 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000106 uint32_t NumMemoryPages = 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000107 uint32_t InitialTableOffset = 0;
108
109 std::vector<const WasmSignature *> Types;
110 DenseMap<WasmSignature, int32_t, WasmSignatureDenseMapInfo> TypeIndices;
Sam Clegg8d146bb2018-01-09 23:56:44 +0000111 std::vector<const Symbol *> ImportedFunctions;
112 std::vector<const Symbol *> ImportedGlobals;
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000113 std::vector<const Symbol *> DefinedGlobals;
Sam Clegg8d146bb2018-01-09 23:56:44 +0000114 std::vector<InputFunction *> DefinedFunctions;
Sam Cleggfc1a9122017-12-11 22:00:56 +0000115 std::vector<const Symbol *> IndirectFunctions;
Sam Cleggc94d3932017-11-17 18:14:09 +0000116
117 // Elements that are used to construct the final output
118 std::string Header;
119 std::vector<OutputSection *> OutputSections;
120
121 std::unique_ptr<FileOutputBuffer> Buffer;
122
123 std::vector<OutputSegment *> Segments;
124 llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap;
125};
126
127} // anonymous namespace
128
129static void debugPrint(const char *fmt, ...) {
130 if (!errorHandler().Verbose)
131 return;
132 fprintf(stderr, "lld: ");
133 va_list ap;
134 va_start(ap, fmt);
135 vfprintf(stderr, fmt, ap);
136 va_end(ap);
137}
138
139void Writer::createImportSection() {
Sam Clegg8d146bb2018-01-09 23:56:44 +0000140 uint32_t NumImports = ImportedFunctions.size() + ImportedGlobals.size();
Sam Cleggc94d3932017-11-17 18:14:09 +0000141 if (Config->ImportMemory)
142 ++NumImports;
143
144 if (NumImports == 0)
145 return;
146
147 SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT);
148 raw_ostream &OS = Section->getStream();
149
150 writeUleb128(OS, NumImports, "import count");
151
Sam Clegg8d146bb2018-01-09 23:56:44 +0000152 for (const Symbol *Sym : ImportedFunctions) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000153 WasmImport Import;
154 Import.Module = "env";
155 Import.Field = Sym->getName();
156 Import.Kind = WASM_EXTERNAL_FUNCTION;
Sam Cleggb8621592017-11-30 01:40:08 +0000157 assert(TypeIndices.count(Sym->getFunctionType()) > 0);
158 Import.SigIndex = TypeIndices.lookup(Sym->getFunctionType());
Sam Cleggc94d3932017-11-17 18:14:09 +0000159 writeImport(OS, Import);
160 }
161
162 if (Config->ImportMemory) {
163 WasmImport Import;
164 Import.Module = "env";
165 Import.Field = "memory";
166 Import.Kind = WASM_EXTERNAL_MEMORY;
167 Import.Memory.Flags = 0;
168 Import.Memory.Initial = NumMemoryPages;
169 writeImport(OS, Import);
170 }
171
Sam Clegg8d146bb2018-01-09 23:56:44 +0000172 for (const Symbol *Sym : ImportedGlobals) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000173 WasmImport Import;
174 Import.Module = "env";
175 Import.Field = Sym->getName();
176 Import.Kind = WASM_EXTERNAL_GLOBAL;
177 Import.Global.Mutable = false;
Sam Cleggd451da12017-12-19 19:56:27 +0000178 Import.Global.Type = WASM_TYPE_I32;
Sam Cleggc94d3932017-11-17 18:14:09 +0000179 writeImport(OS, Import);
180 }
181}
182
183void Writer::createTypeSection() {
184 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE);
185 raw_ostream &OS = Section->getStream();
186 writeUleb128(OS, Types.size(), "type count");
Sam Cleggd451da12017-12-19 19:56:27 +0000187 for (const WasmSignature *Sig : Types)
Sam Cleggc94d3932017-11-17 18:14:09 +0000188 writeSig(OS, *Sig);
Sam Cleggc94d3932017-11-17 18:14:09 +0000189}
190
191void Writer::createFunctionSection() {
Sam Clegg8d146bb2018-01-09 23:56:44 +0000192 if (DefinedFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000193 return;
194
195 SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION);
196 raw_ostream &OS = Section->getStream();
197
Sam Clegg8d146bb2018-01-09 23:56:44 +0000198 writeUleb128(OS, DefinedFunctions.size(), "function count");
199 for (const InputFunction *Func : DefinedFunctions)
200 writeUleb128(OS, TypeIndices.lookup(Func->Signature), "sig index");
Sam Cleggc94d3932017-11-17 18:14:09 +0000201}
202
203void Writer::createMemorySection() {
204 if (Config->ImportMemory)
205 return;
206
207 SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY);
208 raw_ostream &OS = Section->getStream();
209
210 writeUleb128(OS, 1, "memory count");
211 writeUleb128(OS, 0, "memory limits flags");
212 writeUleb128(OS, NumMemoryPages, "initial pages");
213}
214
215void Writer::createGlobalSection() {
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000216 if (DefinedGlobals.empty())
217 return;
218
Sam Cleggc94d3932017-11-17 18:14:09 +0000219 SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL);
220 raw_ostream &OS = Section->getStream();
221
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000222 writeUleb128(OS, DefinedGlobals.size(), "global count");
223 for (const Symbol *Sym : DefinedGlobals) {
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000224 WasmGlobal Global;
225 Global.Type = WASM_TYPE_I32;
226 Global.Mutable = Sym == Config->StackPointerSymbol;
227 Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
228 Global.InitExpr.Value.Int32 = Sym->getVirtualAddress();
Sam Cleggc94d3932017-11-17 18:14:09 +0000229 writeGlobal(OS, Global);
230 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000231}
232
233void Writer::createTableSection() {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000234 // Always output a table section, even if there are no indirect calls.
235 // There are two reasons for this:
236 // 1. For executables it is useful to have an empty table slot at 0
237 // which can be filled with a null function call handler.
238 // 2. If we don't do this, any program that contains a call_indirect but
239 // no address-taken function will fail at validation time since it is
240 // a validation error to include a call_indirect instruction if there
241 // is not table.
242 uint32_t TableSize = InitialTableOffset + IndirectFunctions.size();
243
Sam Cleggc94d3932017-11-17 18:14:09 +0000244 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE);
245 raw_ostream &OS = Section->getStream();
246
247 writeUleb128(OS, 1, "table count");
248 writeSleb128(OS, WASM_TYPE_ANYFUNC, "table type");
249 writeUleb128(OS, WASM_LIMITS_FLAG_HAS_MAX, "table flags");
Sam Cleggfc1a9122017-12-11 22:00:56 +0000250 writeUleb128(OS, TableSize, "table initial size");
251 writeUleb128(OS, TableSize, "table max size");
Sam Cleggc94d3932017-11-17 18:14:09 +0000252}
253
254void Writer::createExportSection() {
Sam Cleggc94d3932017-11-17 18:14:09 +0000255 bool ExportMemory = !Config->Relocatable && !Config->ImportMemory;
Sam Clegg4b27c052017-12-03 02:38:04 +0000256 Symbol *EntrySym = Symtab->find(Config->Entry);
257 bool ExportEntry = !Config->Relocatable && EntrySym && EntrySym->isDefined();
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000258 bool ExportHidden = Config->EmitRelocs;
Sam Cleggc94d3932017-11-17 18:14:09 +0000259
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000260 uint32_t NumExports = ExportMemory ? 1 : 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000261
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000262 std::vector<const Symbol *> SymbolExports;
Sam Clegg4b27c052017-12-03 02:38:04 +0000263 if (ExportEntry)
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000264 SymbolExports.emplace_back(EntrySym);
Sam Cleggc94d3932017-11-17 18:14:09 +0000265
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000266 for (const Symbol *Sym : Symtab->getSymbols()) {
267 if (Sym->isUndefined() || Sym->isGlobal())
268 continue;
269 if (Sym->isHidden() && !ExportHidden)
270 continue;
271 if (ExportEntry && Sym == EntrySym)
272 continue;
273 SymbolExports.emplace_back(Sym);
Sam Cleggc94d3932017-11-17 18:14:09 +0000274 }
275
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000276 for (const Symbol *Sym : DefinedGlobals) {
277 // Can't export the SP right now because it mutable and mutable globals
278 // connot be exported.
279 if (Sym == Config->StackPointerSymbol)
280 continue;
281 SymbolExports.emplace_back(Sym);
282 }
283
284 NumExports += SymbolExports.size();
Sam Cleggc94d3932017-11-17 18:14:09 +0000285 if (!NumExports)
286 return;
287
288 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT);
289 raw_ostream &OS = Section->getStream();
290
291 writeUleb128(OS, NumExports, "export count");
292
293 if (ExportMemory) {
294 WasmExport MemoryExport;
295 MemoryExport.Name = "memory";
296 MemoryExport.Kind = WASM_EXTERNAL_MEMORY;
297 MemoryExport.Index = 0;
298 writeExport(OS, MemoryExport);
299 }
300
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000301 for (const Symbol *Sym : SymbolExports) {
302 log("Export: " + Sym->getName());
303 WasmExport Export;
304 Export.Name = Sym->getName();
305 Export.Index = Sym->getOutputIndex();
306 if (Sym->isFunction())
307 Export.Kind = WASM_EXTERNAL_FUNCTION;
308 else
309 Export.Kind = WASM_EXTERNAL_GLOBAL;
310 writeExport(OS, Export);
Sam Cleggc94d3932017-11-17 18:14:09 +0000311 }
312}
313
314void Writer::createStartSection() {}
315
316void Writer::createElemSection() {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000317 if (IndirectFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000318 return;
319
320 SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM);
321 raw_ostream &OS = Section->getStream();
322
323 writeUleb128(OS, 1, "segment count");
324 writeUleb128(OS, 0, "table index");
325 WasmInitExpr InitExpr;
326 InitExpr.Opcode = WASM_OPCODE_I32_CONST;
327 InitExpr.Value.Int32 = InitialTableOffset;
328 writeInitExpr(OS, InitExpr);
Sam Cleggfc1a9122017-12-11 22:00:56 +0000329 writeUleb128(OS, IndirectFunctions.size(), "elem count");
Sam Cleggc94d3932017-11-17 18:14:09 +0000330
Sam Cleggfc1a9122017-12-11 22:00:56 +0000331 uint32_t TableIndex = InitialTableOffset;
332 for (const Symbol *Sym : IndirectFunctions) {
333 assert(Sym->getTableIndex() == TableIndex);
334 writeUleb128(OS, Sym->getOutputIndex(), "function index");
335 ++TableIndex;
336 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000337}
338
339void Writer::createCodeSection() {
Sam Clegg8d146bb2018-01-09 23:56:44 +0000340 if (DefinedFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000341 return;
342
343 log("createCodeSection");
344
Sam Clegg8d146bb2018-01-09 23:56:44 +0000345 auto Section = make<CodeSection>(DefinedFunctions);
Sam Cleggc94d3932017-11-17 18:14:09 +0000346 OutputSections.push_back(Section);
347}
348
349void Writer::createDataSection() {
350 if (!Segments.size())
351 return;
352
353 log("createDataSection");
354 auto Section = make<DataSection>(Segments);
355 OutputSections.push_back(Section);
356}
357
Sam Cleggd451da12017-12-19 19:56:27 +0000358// Create relocations sections in the final output.
Sam Cleggc94d3932017-11-17 18:14:09 +0000359// These are only created when relocatable output is requested.
360void Writer::createRelocSections() {
361 log("createRelocSections");
362 // Don't use iterator here since we are adding to OutputSection
363 size_t OrigSize = OutputSections.size();
364 for (size_t i = 0; i < OrigSize; i++) {
365 OutputSection *S = OutputSections[i];
366 const char *name;
367 uint32_t Count = S->numRelocations();
368 if (!Count)
369 continue;
370
371 if (S->Type == WASM_SEC_DATA)
372 name = "reloc.DATA";
373 else if (S->Type == WASM_SEC_CODE)
374 name = "reloc.CODE";
375 else
Sam Cleggd451da12017-12-19 19:56:27 +0000376 llvm_unreachable("relocations only supported for code and data");
Sam Cleggc94d3932017-11-17 18:14:09 +0000377
378 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, name);
379 raw_ostream &OS = Section->getStream();
380 writeUleb128(OS, S->Type, "reloc section");
381 writeUleb128(OS, Count, "reloc count");
382 S->writeRelocations(OS);
383 }
384}
385
Sam Clegg49ed9262017-12-01 00:53:21 +0000386// Create the custom "linking" section containing linker metadata.
Sam Cleggc94d3932017-11-17 18:14:09 +0000387// This is only created when relocatable output is requested.
388void Writer::createLinkingSection() {
389 SyntheticSection *Section =
390 createSyntheticSection(WASM_SEC_CUSTOM, "linking");
391 raw_ostream &OS = Section->getStream();
392
393 SubSection DataSizeSubSection(WASM_DATA_SIZE);
394 writeUleb128(DataSizeSubSection.getStream(), DataSize, "data size");
395 DataSizeSubSection.finalizeContents();
396 DataSizeSubSection.writeToStream(OS);
397
Sam Clegg0d0dd392017-12-19 17:09:45 +0000398 if (!Config->Relocatable)
399 return;
400
401 if (Segments.size()) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000402 SubSection SubSection(WASM_SEGMENT_INFO);
403 writeUleb128(SubSection.getStream(), Segments.size(), "num data segments");
404 for (const OutputSegment *S : Segments) {
405 writeStr(SubSection.getStream(), S->Name, "segment name");
406 writeUleb128(SubSection.getStream(), S->Alignment, "alignment");
407 writeUleb128(SubSection.getStream(), 0, "flags");
408 }
409 SubSection.finalizeContents();
410 SubSection.writeToStream(OS);
411 }
Sam Clegg0d0dd392017-12-19 17:09:45 +0000412
413 std::vector<WasmInitFunc> InitFunctions;
414 for (ObjFile *File : Symtab->ObjectFiles) {
415 const WasmLinkingData &L = File->getWasmObj()->linkingData();
416 InitFunctions.reserve(InitFunctions.size() + L.InitFunctions.size());
Sam Cleggd451da12017-12-19 19:56:27 +0000417 for (const WasmInitFunc &F : L.InitFunctions)
Sam Clegg0d0dd392017-12-19 17:09:45 +0000418 InitFunctions.emplace_back(WasmInitFunc{
419 F.Priority, File->relocateFunctionIndex(F.FunctionIndex)});
Sam Clegg0d0dd392017-12-19 17:09:45 +0000420 }
421
422 if (!InitFunctions.empty()) {
423 SubSection SubSection(WASM_INIT_FUNCS);
424 writeUleb128(SubSection.getStream(), InitFunctions.size(),
425 "num init functionsw");
426 for (const WasmInitFunc &F : InitFunctions) {
427 writeUleb128(SubSection.getStream(), F.Priority, "priority");
428 writeUleb128(SubSection.getStream(), F.FunctionIndex, "function index");
429 }
430 SubSection.finalizeContents();
431 SubSection.writeToStream(OS);
432 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000433}
434
435// Create the custom "name" section containing debug symbol names.
436void Writer::createNameSection() {
437 // Create an array of all function sorted by function index space
438 std::vector<const Symbol *> Names;
439
440 for (ObjFile *File : Symtab->ObjectFiles) {
441 Names.reserve(Names.size() + File->getSymbols().size());
442 for (Symbol *S : File->getSymbols()) {
443 if (!S->isFunction() || S->isWeak() || S->WrittenToNameSec)
444 continue;
445 S->WrittenToNameSec = true;
446 Names.emplace_back(S);
447 }
448 }
449
450 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name");
451
452 std::sort(Names.begin(), Names.end(), [](const Symbol *A, const Symbol *B) {
453 return A->getOutputIndex() < B->getOutputIndex();
454 });
455
456 SubSection FunctionSubsection(WASM_NAMES_FUNCTION);
457 raw_ostream &OS = FunctionSubsection.getStream();
458 writeUleb128(OS, Names.size(), "name count");
459
460 // We have to iterate through the inputs twice so that all the imports
461 // appear first before any of the local function names.
462 for (const Symbol *S : Names) {
463 writeUleb128(OS, S->getOutputIndex(), "func index");
464 writeStr(OS, S->getName(), "symbol name");
465 }
466
467 FunctionSubsection.finalizeContents();
468 FunctionSubsection.writeToStream(Section->getStream());
469}
470
471void Writer::writeHeader() {
472 memcpy(Buffer->getBufferStart(), Header.data(), Header.size());
473}
474
475void Writer::writeSections() {
476 uint8_t *Buf = Buffer->getBufferStart();
477 parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); });
478}
479
480// Fix the memory layout of the output binary. This assigns memory offsets
Sam Clegg49ed9262017-12-01 00:53:21 +0000481// to each of the input data sections as well as the explicit stack region.
Sam Cleggc94d3932017-11-17 18:14:09 +0000482void Writer::layoutMemory() {
483 uint32_t MemoryPtr = 0;
484 if (!Config->Relocatable) {
485 MemoryPtr = Config->GlobalBase;
486 debugPrint("mem: global base = %d\n", Config->GlobalBase);
487 }
488
489 createOutputSegments();
490
491 // Static data comes first
492 for (OutputSegment *Seg : Segments) {
493 MemoryPtr = alignTo(MemoryPtr, Seg->Alignment);
494 Seg->StartVA = MemoryPtr;
495 debugPrint("mem: %-10s offset=%-8d size=%-4d align=%d\n",
496 Seg->Name.str().c_str(), MemoryPtr, Seg->Size, Seg->Alignment);
497 MemoryPtr += Seg->Size;
498 }
499
500 DataSize = MemoryPtr;
501 if (!Config->Relocatable)
502 DataSize -= Config->GlobalBase;
503 debugPrint("mem: static data = %d\n", DataSize);
504
505 // Stack comes after static data
506 if (!Config->Relocatable) {
507 MemoryPtr = alignTo(MemoryPtr, kStackAlignment);
508 if (Config->ZStackSize != alignTo(Config->ZStackSize, kStackAlignment))
509 error("stack size must be " + Twine(kStackAlignment) + "-byte aligned");
510 debugPrint("mem: stack size = %d\n", Config->ZStackSize);
511 debugPrint("mem: stack base = %d\n", MemoryPtr);
512 MemoryPtr += Config->ZStackSize;
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000513 Config->StackPointerSymbol->setVirtualAddress(MemoryPtr);
Sam Cleggc94d3932017-11-17 18:14:09 +0000514 debugPrint("mem: stack top = %d\n", MemoryPtr);
515 }
516
517 uint32_t MemSize = alignTo(MemoryPtr, WasmPageSize);
518 NumMemoryPages = MemSize / WasmPageSize;
519 debugPrint("mem: total pages = %d\n", NumMemoryPages);
520}
521
522SyntheticSection *Writer::createSyntheticSection(uint32_t Type,
523 std::string Name) {
524 auto Sec = make<SyntheticSection>(Type, Name);
Sam Cleggab2ac292017-12-20 05:14:48 +0000525 log("createSection: " + toString(*Sec));
Sam Cleggc94d3932017-11-17 18:14:09 +0000526 OutputSections.push_back(Sec);
527 return Sec;
528}
529
530void Writer::createSections() {
531 // Known sections
532 createTypeSection();
533 createImportSection();
534 createFunctionSection();
535 createTableSection();
536 createMemorySection();
537 createGlobalSection();
538 createExportSection();
539 createStartSection();
540 createElemSection();
541 createCodeSection();
542 createDataSection();
543
544 // Custom sections
Sam Clegg22cfe522017-12-05 16:53:25 +0000545 if (Config->EmitRelocs)
Sam Cleggc94d3932017-11-17 18:14:09 +0000546 createRelocSections();
547 createLinkingSection();
548 if (!Config->StripDebug && !Config->StripAll)
549 createNameSection();
550
551 for (OutputSection *S : OutputSections) {
552 S->setOffset(FileSize);
553 S->finalizeContents();
554 FileSize += S->getSize();
555 }
556}
557
Sam Cleggc94d3932017-11-17 18:14:09 +0000558void Writer::calculateImports() {
Sam Clegg574d7ce2017-12-15 19:23:49 +0000559 for (Symbol *Sym : Symtab->getSymbols()) {
Sam Cleggc0181152017-12-15 22:17:15 +0000560 if (!Sym->isUndefined() || Sym->isWeak())
Sam Clegg574d7ce2017-12-15 19:23:49 +0000561 continue;
Sam Cleggc94d3932017-11-17 18:14:09 +0000562
Sam Clegg574d7ce2017-12-15 19:23:49 +0000563 if (Sym->isFunction()) {
Sam Clegg8d146bb2018-01-09 23:56:44 +0000564 Sym->setOutputIndex(ImportedFunctions.size());
565 ImportedFunctions.push_back(Sym);
Sam Clegg574d7ce2017-12-15 19:23:49 +0000566 } else {
Sam Clegg8d146bb2018-01-09 23:56:44 +0000567 Sym->setOutputIndex(ImportedGlobals.size());
568 ImportedGlobals.push_back(Sym);
Sam Cleggc94d3932017-11-17 18:14:09 +0000569 }
570 }
571}
572
Sam Cleggb8621592017-11-30 01:40:08 +0000573uint32_t Writer::getTypeIndex(const WasmSignature &Sig) {
574 auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size()));
575 if (Pair.second)
576 Types.push_back(&Sig);
577 return Pair.first->second;
578}
579
Sam Cleggc94d3932017-11-17 18:14:09 +0000580void Writer::calculateTypes() {
581 for (ObjFile *File : Symtab->ObjectFiles) {
582 File->TypeMap.reserve(File->getWasmObj()->types().size());
Sam Cleggb8621592017-11-30 01:40:08 +0000583 for (const WasmSignature &Sig : File->getWasmObj()->types())
584 File->TypeMap.push_back(getTypeIndex(Sig));
Sam Cleggc94d3932017-11-17 18:14:09 +0000585 }
586}
587
Sam Clegg8d146bb2018-01-09 23:56:44 +0000588void Writer::assignIndexes() {
589 uint32_t GlobalIndex = ImportedGlobals.size();
590 uint32_t FunctionIndex = ImportedFunctions.size();
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000591
592 if (Config->StackPointerSymbol) {
593 DefinedGlobals.emplace_back(Config->StackPointerSymbol);
594 Config->StackPointerSymbol->setOutputIndex(GlobalIndex++);
595 }
596
597 if (Config->EmitRelocs)
598 DefinedGlobals.reserve(Symtab->getSymbols().size());
599
Sam Cleggfc1a9122017-12-11 22:00:56 +0000600 uint32_t TableIndex = InitialTableOffset;
601
Sam Cleggc94d3932017-11-17 18:14:09 +0000602 for (ObjFile *File : Symtab->ObjectFiles) {
Sam Clegg8d146bb2018-01-09 23:56:44 +0000603 if (Config->EmitRelocs) {
604 DEBUG(dbgs() << "Globals: " << File->getName() << "\n");
605 for (Symbol *Sym : File->getSymbols()) {
606 // Create wasm globals for data symbols defined in this file
607 if (!Sym->isDefined() || File != Sym->getFile())
608 continue;
609 if (Sym->isFunction())
610 continue;
Sam Cleggfc1a9122017-12-11 22:00:56 +0000611
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000612 DefinedGlobals.emplace_back(Sym);
613 Sym->setOutputIndex(GlobalIndex++);
Sam Cleggc94d3932017-11-17 18:14:09 +0000614 }
615 }
Sam Clegg87e61922018-01-08 23:39:11 +0000616 }
Sam Cleggfc1a9122017-12-11 22:00:56 +0000617
Sam Clegg87e61922018-01-08 23:39:11 +0000618 for (ObjFile *File : Symtab->ObjectFiles) {
Sam Clegg8d146bb2018-01-09 23:56:44 +0000619 DEBUG(dbgs() << "Functions: " << File->getName() << "\n");
620 for (InputFunction *Func : File->Functions) {
621 DefinedFunctions.emplace_back(Func);
622 Func->setOutputIndex(FunctionIndex++);
623 }
624 }
625
626 for (ObjFile *File : Symtab->ObjectFiles) {
627 DEBUG(dbgs() << "Table Indexes: " << File->getName() << "\n");
Sam Cleggfc1a9122017-12-11 22:00:56 +0000628 for (Symbol *Sym : File->getTableSymbols()) {
Sam Clegg87e61922018-01-08 23:39:11 +0000629 if (Sym->hasTableIndex() || !Sym->hasOutputIndex())
630 continue;
631 Sym->setTableIndex(TableIndex++);
632 IndirectFunctions.emplace_back(Sym);
Sam Cleggfc1a9122017-12-11 22:00:56 +0000633 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000634 }
635}
636
637static StringRef getOutputDataSegmentName(StringRef Name) {
638 if (Config->Relocatable)
639 return Name;
640
641 for (StringRef V :
642 {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
643 ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
644 ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."}) {
645 StringRef Prefix = V.drop_back();
646 if (Name.startswith(V) || Name == Prefix)
647 return Prefix;
648 }
649
650 return Name;
651}
652
653void Writer::createOutputSegments() {
654 for (ObjFile *File : Symtab->ObjectFiles) {
655 for (InputSegment *Segment : File->Segments) {
656 StringRef Name = getOutputDataSegmentName(Segment->getName());
657 OutputSegment *&S = SegmentMap[Name];
658 if (S == nullptr) {
659 DEBUG(dbgs() << "new segment: " << Name << "\n");
660 S = make<OutputSegment>(Name);
661 Segments.push_back(S);
662 }
663 S->addInputSegment(Segment);
664 DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n");
Sam Cleggc94d3932017-11-17 18:14:09 +0000665 }
666 }
667}
668
669void Writer::run() {
670 if (!Config->Relocatable)
671 InitialTableOffset = 1;
672
673 log("-- calculateTypes");
674 calculateTypes();
675 log("-- calculateImports");
676 calculateImports();
Sam Clegg8d146bb2018-01-09 23:56:44 +0000677 log("-- assignIndexes");
678 assignIndexes();
Sam Cleggc94d3932017-11-17 18:14:09 +0000679
680 if (errorHandler().Verbose) {
Sam Clegg8d146bb2018-01-09 23:56:44 +0000681 log("Defined Functions: " + Twine(DefinedFunctions.size()));
Sam Cleggfc1a9122017-12-11 22:00:56 +0000682 log("Defined Globals : " + Twine(DefinedGlobals.size()));
Sam Clegg8d146bb2018-01-09 23:56:44 +0000683 log("Function Imports : " + Twine(ImportedFunctions.size()));
684 log("Global Imports : " + Twine(ImportedGlobals.size()));
Sam Cleggfc1a9122017-12-11 22:00:56 +0000685 log("Total Imports : " +
Sam Clegg8d146bb2018-01-09 23:56:44 +0000686 Twine(ImportedFunctions.size() + ImportedGlobals.size()));
Sam Cleggc94d3932017-11-17 18:14:09 +0000687 for (ObjFile *File : Symtab->ObjectFiles)
688 File->dumpInfo();
689 }
690
Sam Cleggc94d3932017-11-17 18:14:09 +0000691 log("-- layoutMemory");
692 layoutMemory();
693
694 createHeader();
695 log("-- createSections");
696 createSections();
697
698 log("-- openFile");
699 openFile();
700 if (errorCount())
701 return;
702
703 writeHeader();
704
705 log("-- writeSections");
706 writeSections();
707 if (errorCount())
708 return;
709
710 if (Error E = Buffer->commit())
711 fatal("failed to write the output file: " + toString(std::move(E)));
712}
713
714// Open a result file.
715void Writer::openFile() {
716 log("writing: " + Config->OutputFile);
717 ::remove(Config->OutputFile.str().c_str());
718
719 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
720 FileOutputBuffer::create(Config->OutputFile, FileSize,
721 FileOutputBuffer::F_executable);
722
723 if (!BufferOrErr)
724 error("failed to open " + Config->OutputFile + ": " +
725 toString(BufferOrErr.takeError()));
726 else
727 Buffer = std::move(*BufferOrErr);
728}
729
730void Writer::createHeader() {
731 raw_string_ostream OS(Header);
732 writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic");
733 writeU32(OS, WasmVersion, "wasm version");
734 OS.flush();
735 FileSize += Header.size();
736}
737
738void lld::wasm::writeResult() { Writer().run(); }