blob: 8219754752368f90811d69aa244f8d5353e14da7 [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 Cleggc94d3932017-11-17 18:14:09 +000013#include "OutputSections.h"
14#include "OutputSegment.h"
15#include "SymbolTable.h"
16#include "WriterUtils.h"
17#include "lld/Common/ErrorHandler.h"
Rui Ueyama2017d522017-11-28 20:39:17 +000018#include "lld/Common/Memory.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000019#include "lld/Common/Threads.h"
20#include "llvm/Support/FileOutputBuffer.h"
21#include "llvm/Support/Format.h"
22#include "llvm/Support/FormatVariadic.h"
23#include "llvm/Support/LEB128.h"
24
25#include <cstdarg>
26
27#define DEBUG_TYPE "lld"
28
29using namespace llvm;
30using namespace llvm::wasm;
31using namespace lld;
32using namespace lld::wasm;
33
34static constexpr int kStackAlignment = 16;
35
36namespace {
37
Sam Cleggc94d3932017-11-17 18:14:09 +000038// Traits for using WasmSignature in a DenseMap.
39struct WasmSignatureDenseMapInfo {
40 static WasmSignature getEmptyKey() {
41 WasmSignature Sig;
42 Sig.ReturnType = 1;
43 return Sig;
44 }
45 static WasmSignature getTombstoneKey() {
46 WasmSignature Sig;
47 Sig.ReturnType = 2;
48 return Sig;
49 }
50 static unsigned getHashValue(const WasmSignature &Sig) {
51 uintptr_t Value = 0;
52 Value += DenseMapInfo<int32_t>::getHashValue(Sig.ReturnType);
53 for (int32_t Param : Sig.ParamTypes)
54 Value += DenseMapInfo<int32_t>::getHashValue(Param);
55 return Value;
56 }
57 static bool isEqual(const WasmSignature &LHS, const WasmSignature &RHS) {
58 return LHS == RHS;
59 }
60};
61
62// The writer writes a SymbolTable result to a file.
63class Writer {
64public:
65 void run();
66
67private:
68 void openFile();
69
Sam Cleggb8621592017-11-30 01:40:08 +000070 uint32_t getTypeIndex(const WasmSignature &Sig);
Sam Cleggc94d3932017-11-17 18:14:09 +000071 void assignSymbolIndexes();
72 void calculateImports();
73 void calculateOffsets();
74 void calculateTypes();
75 void createOutputSegments();
76 void layoutMemory();
77 void createHeader();
78 void createSections();
79 SyntheticSection *createSyntheticSection(uint32_t Type,
80 std::string Name = "");
81
82 // Builtin sections
83 void createTypeSection();
84 void createFunctionSection();
85 void createTableSection();
86 void createGlobalSection();
87 void createExportSection();
88 void createImportSection();
89 void createMemorySection();
90 void createElemSection();
91 void createStartSection();
92 void createCodeSection();
93 void createDataSection();
94
95 // Custom sections
96 void createRelocSections();
97 void createLinkingSection();
98 void createNameSection();
99
100 void writeHeader();
101 void writeSections();
102
103 uint64_t FileSize = 0;
104 uint32_t DataSize = 0;
105 uint32_t NumFunctions = 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 Clegg74fe0ba2017-12-07 01:51:24 +0000111 std::vector<const Symbol *> FunctionImports;
112 std::vector<const Symbol *> GlobalImports;
113 std::vector<const Symbol *> DefinedGlobals;
Sam Cleggfc1a9122017-12-11 22:00:56 +0000114 std::vector<const Symbol *> IndirectFunctions;
Sam Cleggc94d3932017-11-17 18:14:09 +0000115
116 // Elements that are used to construct the final output
117 std::string Header;
118 std::vector<OutputSection *> OutputSections;
119
120 std::unique_ptr<FileOutputBuffer> Buffer;
121
122 std::vector<OutputSegment *> Segments;
123 llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap;
124};
125
126} // anonymous namespace
127
128static void debugPrint(const char *fmt, ...) {
129 if (!errorHandler().Verbose)
130 return;
131 fprintf(stderr, "lld: ");
132 va_list ap;
133 va_start(ap, fmt);
134 vfprintf(stderr, fmt, ap);
135 va_end(ap);
136}
137
138void Writer::createImportSection() {
139 uint32_t NumImports = FunctionImports.size() + GlobalImports.size();
140 if (Config->ImportMemory)
141 ++NumImports;
142
143 if (NumImports == 0)
144 return;
145
146 SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT);
147 raw_ostream &OS = Section->getStream();
148
149 writeUleb128(OS, NumImports, "import count");
150
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000151 for (const Symbol *Sym : FunctionImports) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000152 WasmImport Import;
153 Import.Module = "env";
154 Import.Field = Sym->getName();
155 Import.Kind = WASM_EXTERNAL_FUNCTION;
Sam Cleggb8621592017-11-30 01:40:08 +0000156 assert(TypeIndices.count(Sym->getFunctionType()) > 0);
157 Import.SigIndex = TypeIndices.lookup(Sym->getFunctionType());
Sam Cleggc94d3932017-11-17 18:14:09 +0000158 writeImport(OS, Import);
159 }
160
161 if (Config->ImportMemory) {
162 WasmImport Import;
163 Import.Module = "env";
164 Import.Field = "memory";
165 Import.Kind = WASM_EXTERNAL_MEMORY;
166 Import.Memory.Flags = 0;
167 Import.Memory.Initial = NumMemoryPages;
168 writeImport(OS, Import);
169 }
170
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000171 for (const Symbol *Sym : GlobalImports) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000172 WasmImport Import;
173 Import.Module = "env";
174 Import.Field = Sym->getName();
175 Import.Kind = WASM_EXTERNAL_GLOBAL;
176 Import.Global.Mutable = false;
Sam Cleggc94d3932017-11-17 18:14:09 +0000177 Import.Global.Type = WASM_TYPE_I32; // Sym->getGlobalType();
178 writeImport(OS, Import);
179 }
180}
181
182void Writer::createTypeSection() {
183 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE);
184 raw_ostream &OS = Section->getStream();
185 writeUleb128(OS, Types.size(), "type count");
186 for (const WasmSignature *Sig : Types) {
187 writeSig(OS, *Sig);
188 }
189}
190
191void Writer::createFunctionSection() {
192 if (!NumFunctions)
193 return;
194
195 SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION);
196 raw_ostream &OS = Section->getStream();
197
198 writeUleb128(OS, NumFunctions, "function count");
199 for (ObjFile *File : Symtab->ObjectFiles) {
200 for (uint32_t Sig : File->getWasmObj()->functionTypes()) {
201 writeUleb128(OS, File->relocateTypeIndex(Sig), "sig index");
202 }
203 }
204}
205
206void Writer::createMemorySection() {
207 if (Config->ImportMemory)
208 return;
209
210 SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY);
211 raw_ostream &OS = Section->getStream();
212
213 writeUleb128(OS, 1, "memory count");
214 writeUleb128(OS, 0, "memory limits flags");
215 writeUleb128(OS, NumMemoryPages, "initial pages");
216}
217
218void Writer::createGlobalSection() {
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000219 if (DefinedGlobals.empty())
220 return;
221
Sam Cleggc94d3932017-11-17 18:14:09 +0000222 SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL);
223 raw_ostream &OS = Section->getStream();
224
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000225 writeUleb128(OS, DefinedGlobals.size(), "global count");
226 for (const Symbol *Sym : DefinedGlobals) {
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000227 WasmGlobal Global;
228 Global.Type = WASM_TYPE_I32;
229 Global.Mutable = Sym == Config->StackPointerSymbol;
230 Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
231 Global.InitExpr.Value.Int32 = Sym->getVirtualAddress();
Sam Cleggc94d3932017-11-17 18:14:09 +0000232 writeGlobal(OS, Global);
233 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000234}
235
236void Writer::createTableSection() {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000237 // Always output a table section, even if there are no indirect calls.
238 // There are two reasons for this:
239 // 1. For executables it is useful to have an empty table slot at 0
240 // which can be filled with a null function call handler.
241 // 2. If we don't do this, any program that contains a call_indirect but
242 // no address-taken function will fail at validation time since it is
243 // a validation error to include a call_indirect instruction if there
244 // is not table.
245 uint32_t TableSize = InitialTableOffset + IndirectFunctions.size();
246
Sam Cleggc94d3932017-11-17 18:14:09 +0000247 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE);
248 raw_ostream &OS = Section->getStream();
249
250 writeUleb128(OS, 1, "table count");
251 writeSleb128(OS, WASM_TYPE_ANYFUNC, "table type");
252 writeUleb128(OS, WASM_LIMITS_FLAG_HAS_MAX, "table flags");
Sam Cleggfc1a9122017-12-11 22:00:56 +0000253 writeUleb128(OS, TableSize, "table initial size");
254 writeUleb128(OS, TableSize, "table max size");
Sam Cleggc94d3932017-11-17 18:14:09 +0000255}
256
257void Writer::createExportSection() {
Sam Cleggc94d3932017-11-17 18:14:09 +0000258 bool ExportMemory = !Config->Relocatable && !Config->ImportMemory;
Sam Clegg4b27c052017-12-03 02:38:04 +0000259 Symbol *EntrySym = Symtab->find(Config->Entry);
260 bool ExportEntry = !Config->Relocatable && EntrySym && EntrySym->isDefined();
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000261 bool ExportHidden = Config->EmitRelocs;
Sam Cleggc94d3932017-11-17 18:14:09 +0000262
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000263 uint32_t NumExports = ExportMemory ? 1 : 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000264
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000265 std::vector<const Symbol *> SymbolExports;
Sam Clegg4b27c052017-12-03 02:38:04 +0000266 if (ExportEntry)
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000267 SymbolExports.emplace_back(EntrySym);
Sam Cleggc94d3932017-11-17 18:14:09 +0000268
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000269 for (const Symbol *Sym : Symtab->getSymbols()) {
270 if (Sym->isUndefined() || Sym->isGlobal())
271 continue;
272 if (Sym->isHidden() && !ExportHidden)
273 continue;
274 if (ExportEntry && Sym == EntrySym)
275 continue;
276 SymbolExports.emplace_back(Sym);
Sam Cleggc94d3932017-11-17 18:14:09 +0000277 }
278
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000279 for (const Symbol *Sym : DefinedGlobals) {
280 // Can't export the SP right now because it mutable and mutable globals
281 // connot be exported.
282 if (Sym == Config->StackPointerSymbol)
283 continue;
284 SymbolExports.emplace_back(Sym);
285 }
286
287 NumExports += SymbolExports.size();
Sam Cleggc94d3932017-11-17 18:14:09 +0000288 if (!NumExports)
289 return;
290
291 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT);
292 raw_ostream &OS = Section->getStream();
293
294 writeUleb128(OS, NumExports, "export count");
295
296 if (ExportMemory) {
297 WasmExport MemoryExport;
298 MemoryExport.Name = "memory";
299 MemoryExport.Kind = WASM_EXTERNAL_MEMORY;
300 MemoryExport.Index = 0;
301 writeExport(OS, MemoryExport);
302 }
303
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000304 for (const Symbol *Sym : SymbolExports) {
305 log("Export: " + Sym->getName());
306 WasmExport Export;
307 Export.Name = Sym->getName();
308 Export.Index = Sym->getOutputIndex();
309 if (Sym->isFunction())
310 Export.Kind = WASM_EXTERNAL_FUNCTION;
311 else
312 Export.Kind = WASM_EXTERNAL_GLOBAL;
313 writeExport(OS, Export);
Sam Cleggc94d3932017-11-17 18:14:09 +0000314 }
315}
316
317void Writer::createStartSection() {}
318
319void Writer::createElemSection() {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000320 if (IndirectFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000321 return;
322
323 SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM);
324 raw_ostream &OS = Section->getStream();
325
326 writeUleb128(OS, 1, "segment count");
327 writeUleb128(OS, 0, "table index");
328 WasmInitExpr InitExpr;
329 InitExpr.Opcode = WASM_OPCODE_I32_CONST;
330 InitExpr.Value.Int32 = InitialTableOffset;
331 writeInitExpr(OS, InitExpr);
Sam Cleggfc1a9122017-12-11 22:00:56 +0000332 writeUleb128(OS, IndirectFunctions.size(), "elem count");
Sam Cleggc94d3932017-11-17 18:14:09 +0000333
Sam Cleggfc1a9122017-12-11 22:00:56 +0000334 uint32_t TableIndex = InitialTableOffset;
335 for (const Symbol *Sym : IndirectFunctions) {
336 assert(Sym->getTableIndex() == TableIndex);
337 writeUleb128(OS, Sym->getOutputIndex(), "function index");
338 ++TableIndex;
339 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000340}
341
342void Writer::createCodeSection() {
343 if (!NumFunctions)
344 return;
345
346 log("createCodeSection");
347
348 auto Section = make<CodeSection>(NumFunctions, Symtab->ObjectFiles);
349 OutputSections.push_back(Section);
350}
351
352void Writer::createDataSection() {
353 if (!Segments.size())
354 return;
355
356 log("createDataSection");
357 auto Section = make<DataSection>(Segments);
358 OutputSections.push_back(Section);
359}
360
361// Create reloctions sections in the final output.
362// These are only created when relocatable output is requested.
363void Writer::createRelocSections() {
364 log("createRelocSections");
365 // Don't use iterator here since we are adding to OutputSection
366 size_t OrigSize = OutputSections.size();
367 for (size_t i = 0; i < OrigSize; i++) {
368 OutputSection *S = OutputSections[i];
369 const char *name;
370 uint32_t Count = S->numRelocations();
371 if (!Count)
372 continue;
373
374 if (S->Type == WASM_SEC_DATA)
375 name = "reloc.DATA";
376 else if (S->Type == WASM_SEC_CODE)
377 name = "reloc.CODE";
378 else
379 llvm_unreachable("relocations only support for code and data");
380
381 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, name);
382 raw_ostream &OS = Section->getStream();
383 writeUleb128(OS, S->Type, "reloc section");
384 writeUleb128(OS, Count, "reloc count");
385 S->writeRelocations(OS);
386 }
387}
388
Sam Clegg49ed9262017-12-01 00:53:21 +0000389// Create the custom "linking" section containing linker metadata.
Sam Cleggc94d3932017-11-17 18:14:09 +0000390// This is only created when relocatable output is requested.
391void Writer::createLinkingSection() {
392 SyntheticSection *Section =
393 createSyntheticSection(WASM_SEC_CUSTOM, "linking");
394 raw_ostream &OS = Section->getStream();
395
396 SubSection DataSizeSubSection(WASM_DATA_SIZE);
397 writeUleb128(DataSizeSubSection.getStream(), DataSize, "data size");
398 DataSizeSubSection.finalizeContents();
399 DataSizeSubSection.writeToStream(OS);
400
401 if (Segments.size() && Config->Relocatable) {
402 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 }
412}
413
414// Create the custom "name" section containing debug symbol names.
415void Writer::createNameSection() {
416 // Create an array of all function sorted by function index space
417 std::vector<const Symbol *> Names;
418
419 for (ObjFile *File : Symtab->ObjectFiles) {
420 Names.reserve(Names.size() + File->getSymbols().size());
421 for (Symbol *S : File->getSymbols()) {
422 if (!S->isFunction() || S->isWeak() || S->WrittenToNameSec)
423 continue;
424 S->WrittenToNameSec = true;
425 Names.emplace_back(S);
426 }
427 }
428
429 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name");
430
431 std::sort(Names.begin(), Names.end(), [](const Symbol *A, const Symbol *B) {
432 return A->getOutputIndex() < B->getOutputIndex();
433 });
434
435 SubSection FunctionSubsection(WASM_NAMES_FUNCTION);
436 raw_ostream &OS = FunctionSubsection.getStream();
437 writeUleb128(OS, Names.size(), "name count");
438
439 // We have to iterate through the inputs twice so that all the imports
440 // appear first before any of the local function names.
441 for (const Symbol *S : Names) {
442 writeUleb128(OS, S->getOutputIndex(), "func index");
443 writeStr(OS, S->getName(), "symbol name");
444 }
445
446 FunctionSubsection.finalizeContents();
447 FunctionSubsection.writeToStream(Section->getStream());
448}
449
450void Writer::writeHeader() {
451 memcpy(Buffer->getBufferStart(), Header.data(), Header.size());
452}
453
454void Writer::writeSections() {
455 uint8_t *Buf = Buffer->getBufferStart();
456 parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); });
457}
458
459// Fix the memory layout of the output binary. This assigns memory offsets
Sam Clegg49ed9262017-12-01 00:53:21 +0000460// to each of the input data sections as well as the explicit stack region.
Sam Cleggc94d3932017-11-17 18:14:09 +0000461void Writer::layoutMemory() {
462 uint32_t MemoryPtr = 0;
463 if (!Config->Relocatable) {
464 MemoryPtr = Config->GlobalBase;
465 debugPrint("mem: global base = %d\n", Config->GlobalBase);
466 }
467
468 createOutputSegments();
469
470 // Static data comes first
471 for (OutputSegment *Seg : Segments) {
472 MemoryPtr = alignTo(MemoryPtr, Seg->Alignment);
473 Seg->StartVA = MemoryPtr;
474 debugPrint("mem: %-10s offset=%-8d size=%-4d align=%d\n",
475 Seg->Name.str().c_str(), MemoryPtr, Seg->Size, Seg->Alignment);
476 MemoryPtr += Seg->Size;
477 }
478
479 DataSize = MemoryPtr;
480 if (!Config->Relocatable)
481 DataSize -= Config->GlobalBase;
482 debugPrint("mem: static data = %d\n", DataSize);
483
484 // Stack comes after static data
485 if (!Config->Relocatable) {
486 MemoryPtr = alignTo(MemoryPtr, kStackAlignment);
487 if (Config->ZStackSize != alignTo(Config->ZStackSize, kStackAlignment))
488 error("stack size must be " + Twine(kStackAlignment) + "-byte aligned");
489 debugPrint("mem: stack size = %d\n", Config->ZStackSize);
490 debugPrint("mem: stack base = %d\n", MemoryPtr);
491 MemoryPtr += Config->ZStackSize;
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000492 Config->StackPointerSymbol->setVirtualAddress(MemoryPtr);
Sam Cleggc94d3932017-11-17 18:14:09 +0000493 debugPrint("mem: stack top = %d\n", MemoryPtr);
494 }
495
496 uint32_t MemSize = alignTo(MemoryPtr, WasmPageSize);
497 NumMemoryPages = MemSize / WasmPageSize;
498 debugPrint("mem: total pages = %d\n", NumMemoryPages);
499}
500
501SyntheticSection *Writer::createSyntheticSection(uint32_t Type,
502 std::string Name) {
503 auto Sec = make<SyntheticSection>(Type, Name);
504 log("createSection: " + toString(Sec));
505 OutputSections.push_back(Sec);
506 return Sec;
507}
508
509void Writer::createSections() {
510 // Known sections
511 createTypeSection();
512 createImportSection();
513 createFunctionSection();
514 createTableSection();
515 createMemorySection();
516 createGlobalSection();
517 createExportSection();
518 createStartSection();
519 createElemSection();
520 createCodeSection();
521 createDataSection();
522
523 // Custom sections
Sam Clegg22cfe522017-12-05 16:53:25 +0000524 if (Config->EmitRelocs)
Sam Cleggc94d3932017-11-17 18:14:09 +0000525 createRelocSections();
526 createLinkingSection();
527 if (!Config->StripDebug && !Config->StripAll)
528 createNameSection();
529
530 for (OutputSection *S : OutputSections) {
531 S->setOffset(FileSize);
532 S->finalizeContents();
533 FileSize += S->getSize();
534 }
535}
536
537void Writer::calculateOffsets() {
Sam Cleggc94d3932017-11-17 18:14:09 +0000538 for (ObjFile *File : Symtab->ObjectFiles) {
539 const WasmObjectFile *WasmFile = File->getWasmObj();
540
541 // Function Index
542 File->FunctionIndexOffset =
543 FunctionImports.size() - File->NumFunctionImports() + NumFunctions;
544 NumFunctions += WasmFile->functions().size();
545
Sam Cleggc94d3932017-11-17 18:14:09 +0000546 // Memory
Sam Cleggfc1a9122017-12-11 22:00:56 +0000547 if (WasmFile->memories().size() > 1)
548 fatal(File->getName() + ": contains more than one memory");
Sam Cleggc94d3932017-11-17 18:14:09 +0000549 }
550}
551
552void Writer::calculateImports() {
Sam Clegg574d7ce2017-12-15 19:23:49 +0000553 for (Symbol *Sym : Symtab->getSymbols()) {
554 if (Sym->isDefined() || Sym->isWeak())
555 continue;
Sam Cleggc94d3932017-11-17 18:14:09 +0000556
Sam Clegg574d7ce2017-12-15 19:23:49 +0000557 if (Sym->isFunction()) {
558 Sym->setOutputIndex(FunctionImports.size());
559 FunctionImports.push_back(Sym);
560 } else {
561 Sym->setOutputIndex(GlobalImports.size());
562 GlobalImports.push_back(Sym);
Sam Cleggc94d3932017-11-17 18:14:09 +0000563 }
564 }
565}
566
Sam Cleggb8621592017-11-30 01:40:08 +0000567uint32_t Writer::getTypeIndex(const WasmSignature &Sig) {
568 auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size()));
569 if (Pair.second)
570 Types.push_back(&Sig);
571 return Pair.first->second;
572}
573
Sam Cleggc94d3932017-11-17 18:14:09 +0000574void Writer::calculateTypes() {
575 for (ObjFile *File : Symtab->ObjectFiles) {
576 File->TypeMap.reserve(File->getWasmObj()->types().size());
Sam Cleggb8621592017-11-30 01:40:08 +0000577 for (const WasmSignature &Sig : File->getWasmObj()->types())
578 File->TypeMap.push_back(getTypeIndex(Sig));
Sam Cleggc94d3932017-11-17 18:14:09 +0000579 }
580}
581
582void Writer::assignSymbolIndexes() {
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000583 uint32_t GlobalIndex = GlobalImports.size();
584
585 if (Config->StackPointerSymbol) {
586 DefinedGlobals.emplace_back(Config->StackPointerSymbol);
587 Config->StackPointerSymbol->setOutputIndex(GlobalIndex++);
588 }
589
590 if (Config->EmitRelocs)
591 DefinedGlobals.reserve(Symtab->getSymbols().size());
592
Sam Cleggfc1a9122017-12-11 22:00:56 +0000593 uint32_t TableIndex = InitialTableOffset;
594
Sam Cleggc94d3932017-11-17 18:14:09 +0000595 for (ObjFile *File : Symtab->ObjectFiles) {
596 DEBUG(dbgs() << "assignSymbolIndexes: " << File->getName() << "\n");
Sam Cleggfc1a9122017-12-11 22:00:56 +0000597
Sam Cleggc94d3932017-11-17 18:14:09 +0000598 for (Symbol *Sym : File->getSymbols()) {
Sam Clegg3f1fb882017-12-11 21:52:21 +0000599 // Assign indexes for symbols defined with this file.
600 if (!Sym->isDefined() || File != Sym->getFile())
Sam Cleggc94d3932017-11-17 18:14:09 +0000601 continue;
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000602 if (Sym->isFunction()) {
Sam Clegg3f1fb882017-12-11 21:52:21 +0000603 auto *Obj = cast<ObjFile>(Sym->getFile());
604 Sym->setOutputIndex(Obj->FunctionIndexOffset +
605 Sym->getFunctionIndex());
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000606 } else if (Config->EmitRelocs) {
607 DefinedGlobals.emplace_back(Sym);
608 Sym->setOutputIndex(GlobalIndex++);
Sam Cleggc94d3932017-11-17 18:14:09 +0000609 }
610 }
Sam Cleggfc1a9122017-12-11 22:00:56 +0000611
612 for (Symbol *Sym : File->getTableSymbols()) {
613 if (!Sym->hasTableIndex()) {
614 Sym->setTableIndex(TableIndex++);
615 IndirectFunctions.emplace_back(Sym);
616 }
617 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000618 }
619}
620
621static StringRef getOutputDataSegmentName(StringRef Name) {
622 if (Config->Relocatable)
623 return Name;
624
625 for (StringRef V :
626 {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
627 ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
628 ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."}) {
629 StringRef Prefix = V.drop_back();
630 if (Name.startswith(V) || Name == Prefix)
631 return Prefix;
632 }
633
634 return Name;
635}
636
637void Writer::createOutputSegments() {
638 for (ObjFile *File : Symtab->ObjectFiles) {
639 for (InputSegment *Segment : File->Segments) {
640 StringRef Name = getOutputDataSegmentName(Segment->getName());
641 OutputSegment *&S = SegmentMap[Name];
642 if (S == nullptr) {
643 DEBUG(dbgs() << "new segment: " << Name << "\n");
644 S = make<OutputSegment>(Name);
645 Segments.push_back(S);
646 }
647 S->addInputSegment(Segment);
648 DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n");
649 for (const WasmRelocation &R : File->DataSection->Relocations) {
650 if (R.Offset >= Segment->getInputSectionOffset() &&
651 R.Offset < Segment->getInputSectionOffset() + Segment->getSize()) {
652 Segment->Relocations.push_back(R);
653 }
654 }
655 }
656 }
657}
658
659void Writer::run() {
660 if (!Config->Relocatable)
661 InitialTableOffset = 1;
662
663 log("-- calculateTypes");
664 calculateTypes();
665 log("-- calculateImports");
666 calculateImports();
667 log("-- calculateOffsets");
668 calculateOffsets();
669
670 if (errorHandler().Verbose) {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000671 log("Defined Functions: " + Twine(NumFunctions));
672 log("Defined Globals : " + Twine(DefinedGlobals.size()));
673 log("Function Imports : " + Twine(FunctionImports.size()));
674 log("Global Imports : " + Twine(GlobalImports.size()));
675 log("Total Imports : " +
Sam Cleggc94d3932017-11-17 18:14:09 +0000676 Twine(FunctionImports.size() + GlobalImports.size()));
Sam Cleggc94d3932017-11-17 18:14:09 +0000677 for (ObjFile *File : Symtab->ObjectFiles)
678 File->dumpInfo();
679 }
680
681 log("-- assignSymbolIndexes");
682 assignSymbolIndexes();
683 log("-- layoutMemory");
684 layoutMemory();
685
686 createHeader();
687 log("-- createSections");
688 createSections();
689
690 log("-- openFile");
691 openFile();
692 if (errorCount())
693 return;
694
695 writeHeader();
696
697 log("-- writeSections");
698 writeSections();
699 if (errorCount())
700 return;
701
702 if (Error E = Buffer->commit())
703 fatal("failed to write the output file: " + toString(std::move(E)));
704}
705
706// Open a result file.
707void Writer::openFile() {
708 log("writing: " + Config->OutputFile);
709 ::remove(Config->OutputFile.str().c_str());
710
711 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
712 FileOutputBuffer::create(Config->OutputFile, FileSize,
713 FileOutputBuffer::F_executable);
714
715 if (!BufferOrErr)
716 error("failed to open " + Config->OutputFile + ": " +
717 toString(BufferOrErr.takeError()));
718 else
719 Buffer = std::move(*BufferOrErr);
720}
721
722void Writer::createHeader() {
723 raw_string_ostream OS(Header);
724 writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic");
725 writeU32(OS, WasmVersion, "wasm version");
726 OS.flush();
727 FileSize += Header.size();
728}
729
730void lld::wasm::writeResult() { Writer().run(); }