blob: 309b10cabc465ca88f3538f7fa515c73c8b23e27 [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;
106 uint32_t NumGlobals = 0;
107 uint32_t NumMemoryPages = 0;
108 uint32_t NumTableElems = 0;
109 uint32_t NumElements = 0;
110 uint32_t InitialTableOffset = 0;
111
112 std::vector<const WasmSignature *> Types;
113 DenseMap<WasmSignature, int32_t, WasmSignatureDenseMapInfo> TypeIndices;
114 std::vector<Symbol *> FunctionImports;
115 std::vector<Symbol *> GlobalImports;
116
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() {
140 uint32_t NumImports = FunctionImports.size() + GlobalImports.size();
141 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
152 for (Symbol *Sym : FunctionImports) {
153 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
172 for (Symbol *Sym : GlobalImports) {
173 WasmImport Import;
174 Import.Module = "env";
175 Import.Field = Sym->getName();
176 Import.Kind = WASM_EXTERNAL_GLOBAL;
177 Import.Global.Mutable = false;
Sam Cleggc94d3932017-11-17 18:14:09 +0000178 Import.Global.Type = WASM_TYPE_I32; // Sym->getGlobalType();
179 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");
187 for (const WasmSignature *Sig : Types) {
188 writeSig(OS, *Sig);
189 }
190}
191
192void Writer::createFunctionSection() {
193 if (!NumFunctions)
194 return;
195
196 SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION);
197 raw_ostream &OS = Section->getStream();
198
199 writeUleb128(OS, NumFunctions, "function count");
200 for (ObjFile *File : Symtab->ObjectFiles) {
201 for (uint32_t Sig : File->getWasmObj()->functionTypes()) {
202 writeUleb128(OS, File->relocateTypeIndex(Sig), "sig index");
203 }
204 }
205}
206
207void Writer::createMemorySection() {
208 if (Config->ImportMemory)
209 return;
210
211 SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY);
212 raw_ostream &OS = Section->getStream();
213
214 writeUleb128(OS, 1, "memory count");
215 writeUleb128(OS, 0, "memory limits flags");
216 writeUleb128(OS, NumMemoryPages, "initial pages");
217}
218
219void Writer::createGlobalSection() {
220 SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL);
221 raw_ostream &OS = Section->getStream();
222
223 writeUleb128(OS, NumGlobals, "global count");
224 for (auto &Pair : Config->SyntheticGlobals) {
225 WasmGlobal &Global = Pair.second;
226 writeGlobal(OS, Global);
227 }
228
229 if (Config->Relocatable || Config->EmitRelocs) {
230 for (ObjFile *File : Symtab->ObjectFiles) {
231 uint32_t GlobalIndex = File->NumGlobalImports();
232 for (const WasmGlobal &Global : File->getWasmObj()->globals()) {
233 WasmGlobal RelocatedGlobal(Global);
234 if (Global.Type != WASM_TYPE_I32)
235 fatal("unsupported global type: " + Twine(Global.Type));
236 if (Global.InitExpr.Opcode != WASM_OPCODE_I32_CONST)
237 fatal("unsupported global init opcode: " +
238 Twine(Global.InitExpr.Opcode));
239 RelocatedGlobal.InitExpr.Value.Int32 =
240 File->getRelocatedAddress(GlobalIndex);
241 writeGlobal(OS, RelocatedGlobal);
242 ++GlobalIndex;
243 }
244 }
245 }
246}
247
248void Writer::createTableSection() {
249 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE);
250 raw_ostream &OS = Section->getStream();
251
252 writeUleb128(OS, 1, "table count");
253 writeSleb128(OS, WASM_TYPE_ANYFUNC, "table type");
254 writeUleb128(OS, WASM_LIMITS_FLAG_HAS_MAX, "table flags");
255 writeUleb128(OS, NumTableElems, "table initial size");
256 writeUleb128(OS, NumTableElems, "table max size");
257}
258
259void Writer::createExportSection() {
260 // Memory is and main function are exported for executables.
261 bool ExportMemory = !Config->Relocatable && !Config->ImportMemory;
Sam Clegg4b27c052017-12-03 02:38:04 +0000262 bool ExportOther = true; // ??? TODO Config->Relocatable;
263 bool ExportHidden = Config->Relocatable;
264 Symbol *EntrySym = Symtab->find(Config->Entry);
265 bool ExportEntry = !Config->Relocatable && EntrySym && EntrySym->isDefined();
Sam Cleggc94d3932017-11-17 18:14:09 +0000266
267 uint32_t NumExports = 0;
268
269 if (ExportMemory)
270 ++NumExports;
271
Sam Clegg4b27c052017-12-03 02:38:04 +0000272 if (ExportEntry)
Sam Cleggc94d3932017-11-17 18:14:09 +0000273 ++NumExports;
274
275 if (ExportOther) {
276 for (ObjFile *File : Symtab->ObjectFiles) {
277 for (Symbol *Sym : File->getSymbols()) {
278 if (!Sym->isFunction() || Sym->isLocal() || Sym->isUndefined() ||
Sam Clegg4b27c052017-12-03 02:38:04 +0000279 (Sym->isHidden() && !ExportHidden) || Sym->WrittenToSymtab)
280 continue;
281 if (Sym == EntrySym)
Sam Cleggc94d3932017-11-17 18:14:09 +0000282 continue;
283 Sym->WrittenToSymtab = true;
284 ++NumExports;
285 }
286 }
287 }
288
289 if (!NumExports)
290 return;
291
292 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT);
293 raw_ostream &OS = Section->getStream();
294
295 writeUleb128(OS, NumExports, "export count");
296
297 if (ExportMemory) {
298 WasmExport MemoryExport;
299 MemoryExport.Name = "memory";
300 MemoryExport.Kind = WASM_EXTERNAL_MEMORY;
301 MemoryExport.Index = 0;
302 writeExport(OS, MemoryExport);
303 }
304
Sam Clegg4b27c052017-12-03 02:38:04 +0000305 if (ExportEntry) {
306 WasmExport EntryExport;
307 EntryExport.Name = Config->Entry;
308 EntryExport.Kind = WASM_EXTERNAL_FUNCTION;
309 EntryExport.Index = EntrySym->getOutputIndex();
310 writeExport(OS, EntryExport);
Sam Cleggc94d3932017-11-17 18:14:09 +0000311 }
312
313 if (ExportOther) {
314 for (ObjFile *File : Symtab->ObjectFiles) {
315 for (Symbol *Sym : File->getSymbols()) {
Sam Clegg4b27c052017-12-03 02:38:04 +0000316 if (!Sym->isFunction() || Sym->isLocal() || Sym->isUndefined() ||
317 (Sym->isHidden() && !ExportHidden) || !Sym->WrittenToSymtab)
318 continue;
319 if (Sym == EntrySym)
Sam Cleggc94d3932017-11-17 18:14:09 +0000320 continue;
321 Sym->WrittenToSymtab = false;
322 log("Export: " + Sym->getName());
323 WasmExport Export;
324 Export.Name = Sym->getName();
325 Export.Index = Sym->getOutputIndex();
326 if (Sym->isFunction())
327 Export.Kind = WASM_EXTERNAL_FUNCTION;
328 else
329 Export.Kind = WASM_EXTERNAL_GLOBAL;
330 writeExport(OS, Export);
331 }
332 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000333 }
334}
335
336void Writer::createStartSection() {}
337
338void Writer::createElemSection() {
339 if (!NumElements)
340 return;
341
342 SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM);
343 raw_ostream &OS = Section->getStream();
344
345 writeUleb128(OS, 1, "segment count");
346 writeUleb128(OS, 0, "table index");
347 WasmInitExpr InitExpr;
348 InitExpr.Opcode = WASM_OPCODE_I32_CONST;
349 InitExpr.Value.Int32 = InitialTableOffset;
350 writeInitExpr(OS, InitExpr);
351 writeUleb128(OS, NumElements, "elem count");
352
353 for (ObjFile *File : Symtab->ObjectFiles)
354 for (const WasmElemSegment &Segment : File->getWasmObj()->elements())
355 for (uint64_t FunctionIndex : Segment.Functions)
356 writeUleb128(OS, File->relocateFunctionIndex(FunctionIndex),
357 "function index");
358}
359
360void Writer::createCodeSection() {
361 if (!NumFunctions)
362 return;
363
364 log("createCodeSection");
365
366 auto Section = make<CodeSection>(NumFunctions, Symtab->ObjectFiles);
367 OutputSections.push_back(Section);
368}
369
370void Writer::createDataSection() {
371 if (!Segments.size())
372 return;
373
374 log("createDataSection");
375 auto Section = make<DataSection>(Segments);
376 OutputSections.push_back(Section);
377}
378
379// Create reloctions sections in the final output.
380// These are only created when relocatable output is requested.
381void Writer::createRelocSections() {
382 log("createRelocSections");
383 // Don't use iterator here since we are adding to OutputSection
384 size_t OrigSize = OutputSections.size();
385 for (size_t i = 0; i < OrigSize; i++) {
386 OutputSection *S = OutputSections[i];
387 const char *name;
388 uint32_t Count = S->numRelocations();
389 if (!Count)
390 continue;
391
392 if (S->Type == WASM_SEC_DATA)
393 name = "reloc.DATA";
394 else if (S->Type == WASM_SEC_CODE)
395 name = "reloc.CODE";
396 else
397 llvm_unreachable("relocations only support for code and data");
398
399 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, name);
400 raw_ostream &OS = Section->getStream();
401 writeUleb128(OS, S->Type, "reloc section");
402 writeUleb128(OS, Count, "reloc count");
403 S->writeRelocations(OS);
404 }
405}
406
Sam Clegg49ed9262017-12-01 00:53:21 +0000407// Create the custom "linking" section containing linker metadata.
Sam Cleggc94d3932017-11-17 18:14:09 +0000408// This is only created when relocatable output is requested.
409void Writer::createLinkingSection() {
410 SyntheticSection *Section =
411 createSyntheticSection(WASM_SEC_CUSTOM, "linking");
412 raw_ostream &OS = Section->getStream();
413
414 SubSection DataSizeSubSection(WASM_DATA_SIZE);
415 writeUleb128(DataSizeSubSection.getStream(), DataSize, "data size");
416 DataSizeSubSection.finalizeContents();
417 DataSizeSubSection.writeToStream(OS);
418
419 if (Segments.size() && Config->Relocatable) {
420 SubSection SubSection(WASM_SEGMENT_INFO);
421 writeUleb128(SubSection.getStream(), Segments.size(), "num data segments");
422 for (const OutputSegment *S : Segments) {
423 writeStr(SubSection.getStream(), S->Name, "segment name");
424 writeUleb128(SubSection.getStream(), S->Alignment, "alignment");
425 writeUleb128(SubSection.getStream(), 0, "flags");
426 }
427 SubSection.finalizeContents();
428 SubSection.writeToStream(OS);
429 }
430}
431
432// Create the custom "name" section containing debug symbol names.
433void Writer::createNameSection() {
434 // Create an array of all function sorted by function index space
435 std::vector<const Symbol *> Names;
436
437 for (ObjFile *File : Symtab->ObjectFiles) {
438 Names.reserve(Names.size() + File->getSymbols().size());
439 for (Symbol *S : File->getSymbols()) {
440 if (!S->isFunction() || S->isWeak() || S->WrittenToNameSec)
441 continue;
442 S->WrittenToNameSec = true;
443 Names.emplace_back(S);
444 }
445 }
446
447 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name");
448
449 std::sort(Names.begin(), Names.end(), [](const Symbol *A, const Symbol *B) {
450 return A->getOutputIndex() < B->getOutputIndex();
451 });
452
453 SubSection FunctionSubsection(WASM_NAMES_FUNCTION);
454 raw_ostream &OS = FunctionSubsection.getStream();
455 writeUleb128(OS, Names.size(), "name count");
456
457 // We have to iterate through the inputs twice so that all the imports
458 // appear first before any of the local function names.
459 for (const Symbol *S : Names) {
460 writeUleb128(OS, S->getOutputIndex(), "func index");
461 writeStr(OS, S->getName(), "symbol name");
462 }
463
464 FunctionSubsection.finalizeContents();
465 FunctionSubsection.writeToStream(Section->getStream());
466}
467
468void Writer::writeHeader() {
469 memcpy(Buffer->getBufferStart(), Header.data(), Header.size());
470}
471
472void Writer::writeSections() {
473 uint8_t *Buf = Buffer->getBufferStart();
474 parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); });
475}
476
477// Fix the memory layout of the output binary. This assigns memory offsets
Sam Clegg49ed9262017-12-01 00:53:21 +0000478// to each of the input data sections as well as the explicit stack region.
Sam Cleggc94d3932017-11-17 18:14:09 +0000479void Writer::layoutMemory() {
480 uint32_t MemoryPtr = 0;
481 if (!Config->Relocatable) {
482 MemoryPtr = Config->GlobalBase;
483 debugPrint("mem: global base = %d\n", Config->GlobalBase);
484 }
485
486 createOutputSegments();
487
488 // Static data comes first
489 for (OutputSegment *Seg : Segments) {
490 MemoryPtr = alignTo(MemoryPtr, Seg->Alignment);
491 Seg->StartVA = MemoryPtr;
492 debugPrint("mem: %-10s offset=%-8d size=%-4d align=%d\n",
493 Seg->Name.str().c_str(), MemoryPtr, Seg->Size, Seg->Alignment);
494 MemoryPtr += Seg->Size;
495 }
496
497 DataSize = MemoryPtr;
498 if (!Config->Relocatable)
499 DataSize -= Config->GlobalBase;
500 debugPrint("mem: static data = %d\n", DataSize);
501
502 // Stack comes after static data
503 if (!Config->Relocatable) {
504 MemoryPtr = alignTo(MemoryPtr, kStackAlignment);
505 if (Config->ZStackSize != alignTo(Config->ZStackSize, kStackAlignment))
506 error("stack size must be " + Twine(kStackAlignment) + "-byte aligned");
507 debugPrint("mem: stack size = %d\n", Config->ZStackSize);
508 debugPrint("mem: stack base = %d\n", MemoryPtr);
509 MemoryPtr += Config->ZStackSize;
510 Config->SyntheticGlobals[0].second.InitExpr.Value.Int32 = MemoryPtr;
511 debugPrint("mem: stack top = %d\n", MemoryPtr);
512 }
513
514 uint32_t MemSize = alignTo(MemoryPtr, WasmPageSize);
515 NumMemoryPages = MemSize / WasmPageSize;
516 debugPrint("mem: total pages = %d\n", NumMemoryPages);
517}
518
519SyntheticSection *Writer::createSyntheticSection(uint32_t Type,
520 std::string Name) {
521 auto Sec = make<SyntheticSection>(Type, Name);
522 log("createSection: " + toString(Sec));
523 OutputSections.push_back(Sec);
524 return Sec;
525}
526
527void Writer::createSections() {
528 // Known sections
529 createTypeSection();
530 createImportSection();
531 createFunctionSection();
532 createTableSection();
533 createMemorySection();
534 createGlobalSection();
535 createExportSection();
536 createStartSection();
537 createElemSection();
538 createCodeSection();
539 createDataSection();
540
541 // Custom sections
542 if (Config->EmitRelocs || Config->Relocatable)
543 createRelocSections();
544 createLinkingSection();
545 if (!Config->StripDebug && !Config->StripAll)
546 createNameSection();
547
548 for (OutputSection *S : OutputSections) {
549 S->setOffset(FileSize);
550 S->finalizeContents();
551 FileSize += S->getSize();
552 }
553}
554
555void Writer::calculateOffsets() {
556 NumGlobals = Config->SyntheticGlobals.size();
557 NumTableElems = InitialTableOffset;
558
559 for (ObjFile *File : Symtab->ObjectFiles) {
560 const WasmObjectFile *WasmFile = File->getWasmObj();
561
562 // Function Index
563 File->FunctionIndexOffset =
564 FunctionImports.size() - File->NumFunctionImports() + NumFunctions;
565 NumFunctions += WasmFile->functions().size();
566
567 // Global Index
568 if (Config->Relocatable || Config->EmitRelocs) {
569 File->GlobalIndexOffset =
570 GlobalImports.size() - File->NumGlobalImports() + NumGlobals;
571 NumGlobals += WasmFile->globals().size();
572 }
573
574 // Memory
575 if (WasmFile->memories().size()) {
576 if (WasmFile->memories().size() > 1) {
577 fatal(File->getName() + ": contains more than one memory");
578 }
579 }
580
581 // Table
582 uint32_t TableCount = WasmFile->tables().size();
583 if (TableCount) {
584 if (TableCount > 1)
585 fatal(File->getName() + ": contains more than one table");
586 File->TableIndexOffset = NumTableElems;
587 NumTableElems += WasmFile->tables()[0].Limits.Initial;
588 }
589
590 // Elem
591 uint32_t SegmentCount = WasmFile->elements().size();
592 if (SegmentCount) {
Rui Ueyamae48f2232017-11-29 20:45:58 +0000593 if (SegmentCount > 1)
Sam Cleggc94d3932017-11-17 18:14:09 +0000594 fatal(File->getName() + ": contains more than element segment");
Rui Ueyamae48f2232017-11-29 20:45:58 +0000595
596 const WasmElemSegment &Segment = WasmFile->elements()[0];
597 if (Segment.TableIndex != 0)
598 fatal(File->getName() + ": unsupported table index");
599 if (Segment.Offset.Value.Int32 != 0)
600 fatal(File->getName() + ": unsupported segment offset");
601 NumElements += Segment.Functions.size();
Sam Cleggc94d3932017-11-17 18:14:09 +0000602 }
603 }
604}
605
606void Writer::calculateImports() {
607 for (ObjFile *File : Symtab->ObjectFiles) {
608 for (Symbol *Sym : File->getSymbols()) {
609 if (Sym->hasOutputIndex() || Sym->isDefined() || Sym->isWeak())
610 continue;
611
612 if (Sym->isFunction()) {
613 Sym->setOutputIndex(FunctionImports.size());
614 FunctionImports.push_back(Sym);
615 } else {
616 Sym->setOutputIndex(GlobalImports.size());
617 GlobalImports.push_back(Sym);
618 }
619 }
620 }
621}
622
Sam Cleggb8621592017-11-30 01:40:08 +0000623uint32_t Writer::getTypeIndex(const WasmSignature &Sig) {
624 auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size()));
625 if (Pair.second)
626 Types.push_back(&Sig);
627 return Pair.first->second;
628}
629
Sam Cleggc94d3932017-11-17 18:14:09 +0000630void Writer::calculateTypes() {
631 for (ObjFile *File : Symtab->ObjectFiles) {
632 File->TypeMap.reserve(File->getWasmObj()->types().size());
Sam Cleggb8621592017-11-30 01:40:08 +0000633 for (const WasmSignature &Sig : File->getWasmObj()->types())
634 File->TypeMap.push_back(getTypeIndex(Sig));
Sam Cleggc94d3932017-11-17 18:14:09 +0000635 }
636}
637
638void Writer::assignSymbolIndexes() {
639 for (ObjFile *File : Symtab->ObjectFiles) {
640 DEBUG(dbgs() << "assignSymbolIndexes: " << File->getName() << "\n");
641 for (Symbol *Sym : File->getSymbols()) {
642 if (Sym->hasOutputIndex() || !Sym->isDefined())
643 continue;
644
645 if (Sym->getFile() && isa<ObjFile>(Sym->getFile())) {
646 auto *Obj = cast<ObjFile>(Sym->getFile());
647 if (Sym->isFunction())
648 Sym->setOutputIndex(Obj->FunctionIndexOffset +
649 Sym->getFunctionIndex());
650 else
651 Sym->setOutputIndex(Obj->GlobalIndexOffset + Sym->getGlobalIndex());
652 }
653 }
654 }
655}
656
657static StringRef getOutputDataSegmentName(StringRef Name) {
658 if (Config->Relocatable)
659 return Name;
660
661 for (StringRef V :
662 {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
663 ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
664 ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."}) {
665 StringRef Prefix = V.drop_back();
666 if (Name.startswith(V) || Name == Prefix)
667 return Prefix;
668 }
669
670 return Name;
671}
672
673void Writer::createOutputSegments() {
674 for (ObjFile *File : Symtab->ObjectFiles) {
675 for (InputSegment *Segment : File->Segments) {
676 StringRef Name = getOutputDataSegmentName(Segment->getName());
677 OutputSegment *&S = SegmentMap[Name];
678 if (S == nullptr) {
679 DEBUG(dbgs() << "new segment: " << Name << "\n");
680 S = make<OutputSegment>(Name);
681 Segments.push_back(S);
682 }
683 S->addInputSegment(Segment);
684 DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n");
685 for (const WasmRelocation &R : File->DataSection->Relocations) {
686 if (R.Offset >= Segment->getInputSectionOffset() &&
687 R.Offset < Segment->getInputSectionOffset() + Segment->getSize()) {
688 Segment->Relocations.push_back(R);
689 }
690 }
691 }
692 }
693}
694
695void Writer::run() {
696 if (!Config->Relocatable)
697 InitialTableOffset = 1;
698
699 log("-- calculateTypes");
700 calculateTypes();
701 log("-- calculateImports");
702 calculateImports();
703 log("-- calculateOffsets");
704 calculateOffsets();
705
706 if (errorHandler().Verbose) {
707 log("NumFunctions : " + Twine(NumFunctions));
708 log("NumGlobals : " + Twine(NumGlobals));
709 log("NumImports : " +
710 Twine(FunctionImports.size() + GlobalImports.size()));
711 log("FunctionImports : " + Twine(FunctionImports.size()));
712 log("GlobalImports : " + Twine(GlobalImports.size()));
713 for (ObjFile *File : Symtab->ObjectFiles)
714 File->dumpInfo();
715 }
716
717 log("-- assignSymbolIndexes");
718 assignSymbolIndexes();
719 log("-- layoutMemory");
720 layoutMemory();
721
722 createHeader();
723 log("-- createSections");
724 createSections();
725
726 log("-- openFile");
727 openFile();
728 if (errorCount())
729 return;
730
731 writeHeader();
732
733 log("-- writeSections");
734 writeSections();
735 if (errorCount())
736 return;
737
738 if (Error E = Buffer->commit())
739 fatal("failed to write the output file: " + toString(std::move(E)));
740}
741
742// Open a result file.
743void Writer::openFile() {
744 log("writing: " + Config->OutputFile);
745 ::remove(Config->OutputFile.str().c_str());
746
747 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
748 FileOutputBuffer::create(Config->OutputFile, FileSize,
749 FileOutputBuffer::F_executable);
750
751 if (!BufferOrErr)
752 error("failed to open " + Config->OutputFile + ": " +
753 toString(BufferOrErr.takeError()));
754 else
755 Buffer = std::move(*BufferOrErr);
756}
757
758void Writer::createHeader() {
759 raw_string_ostream OS(Header);
760 writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic");
761 writeU32(OS, WasmVersion, "wasm version");
762 OS.flush();
763 FileSize += Header.size();
764}
765
766void lld::wasm::writeResult() { Writer().run(); }