blob: ee6056ab76dd252359f1a764977c76a622602202 [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"
Sam Cleggc94d3932017-11-17 18:14:09 +000011#include "Config.h"
Sam Clegg5fa274b2018-01-10 01:13:34 +000012#include "InputChunks.h"
Sam Clegg93102972018-02-23 05:08:53 +000013#include "InputGlobal.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"
Nicholas Wilson8269f372018-03-07 10:37:50 +000020#include "lld/Common/Strings.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000021#include "lld/Common/Threads.h"
Sam Clegg3141ddc2018-02-20 21:53:18 +000022#include "llvm/ADT/DenseSet.h"
Sam Clegg80ba4382018-04-10 16:12:49 +000023#include "llvm/ADT/StringMap.h"
Sam Clegg93102972018-02-23 05:08:53 +000024#include "llvm/BinaryFormat/Wasm.h"
Nicholas Wilson3e3f5fb2018-03-14 15:58:16 +000025#include "llvm/Object/WasmTraits.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000026#include "llvm/Support/FileOutputBuffer.h"
27#include "llvm/Support/Format.h"
28#include "llvm/Support/FormatVariadic.h"
29#include "llvm/Support/LEB128.h"
30
31#include <cstdarg>
Sam Clegge0f6fcd2018-01-12 22:25:17 +000032#include <map>
Sam Cleggc94d3932017-11-17 18:14:09 +000033
34#define DEBUG_TYPE "lld"
35
36using namespace llvm;
37using namespace llvm::wasm;
38using namespace lld;
39using namespace lld::wasm;
40
41static constexpr int kStackAlignment = 16;
Sam Clegg48bbd632018-01-24 21:37:30 +000042static constexpr int kInitialTableOffset = 1;
Nicholas Wilson874eedd2018-03-27 17:38:51 +000043static constexpr const char *kFunctionTableName = "__indirect_function_table";
Sam Cleggc94d3932017-11-17 18:14:09 +000044
45namespace {
46
Sam Clegg93102972018-02-23 05:08:53 +000047// An init entry to be written to either the synthetic init func or the
48// linking metadata.
49struct WasmInitEntry {
Sam Clegge3f3ccf2018-03-12 19:56:23 +000050 const FunctionSymbol *Sym;
Sam Clegg93102972018-02-23 05:08:53 +000051 uint32_t Priority;
Sam Cleggd3052d52018-01-18 23:40:49 +000052};
53
Sam Cleggc94d3932017-11-17 18:14:09 +000054// The writer writes a SymbolTable result to a file.
55class Writer {
56public:
57 void run();
58
59private:
60 void openFile();
61
Sam Cleggc375e4e2018-01-10 19:18:22 +000062 uint32_t lookupType(const WasmSignature &Sig);
63 uint32_t registerType(const WasmSignature &Sig);
Sam Clegg93102972018-02-23 05:08:53 +000064
Sam Clegg50686852018-01-12 18:35:13 +000065 void createCtorFunction();
66 void calculateInitFunctions();
Sam Clegg8d146bb2018-01-09 23:56:44 +000067 void assignIndexes();
Sam Cleggc94d3932017-11-17 18:14:09 +000068 void calculateImports();
Sam Cleggd3052d52018-01-18 23:40:49 +000069 void calculateExports();
Sam Cleggd177ab22018-05-04 23:14:42 +000070 void calculateCustomSections();
Sam Clegg93102972018-02-23 05:08:53 +000071 void assignSymtab();
Sam Cleggc94d3932017-11-17 18:14:09 +000072 void calculateTypes();
73 void createOutputSegments();
74 void layoutMemory();
75 void createHeader();
76 void createSections();
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +000077 SyntheticSection *createSyntheticSection(uint32_t Type, StringRef Name = "");
Sam Cleggc94d3932017-11-17 18:14:09 +000078
79 // Builtin sections
80 void createTypeSection();
81 void createFunctionSection();
82 void createTableSection();
83 void createGlobalSection();
84 void createExportSection();
85 void createImportSection();
86 void createMemorySection();
87 void createElemSection();
Sam Cleggc94d3932017-11-17 18:14:09 +000088 void createCodeSection();
89 void createDataSection();
Sam Clegg80ba4382018-04-10 16:12:49 +000090 void createCustomSections();
Sam Cleggc94d3932017-11-17 18:14:09 +000091
92 // Custom sections
93 void createRelocSections();
94 void createLinkingSection();
95 void createNameSection();
96
97 void writeHeader();
98 void writeSections();
99
100 uint64_t FileSize = 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000101 uint32_t NumMemoryPages = 0;
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000102 uint32_t MaxMemoryPages = 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000103
104 std::vector<const WasmSignature *> Types;
Nicholas Wilson3e3f5fb2018-03-14 15:58:16 +0000105 DenseMap<WasmSignature, int32_t> TypeIndices;
Sam Clegg93102972018-02-23 05:08:53 +0000106 std::vector<const Symbol *> ImportedSymbols;
107 unsigned NumImportedFunctions = 0;
108 unsigned NumImportedGlobals = 0;
Sam Cleggd6beb322018-05-10 18:10:34 +0000109 std::vector<WasmExport> Exports;
Sam Clegg93102972018-02-23 05:08:53 +0000110 std::vector<const DefinedData *> DefinedFakeGlobals;
111 std::vector<InputGlobal *> InputGlobals;
Sam Clegg9f934222018-02-21 18:29:23 +0000112 std::vector<InputFunction *> InputFunctions;
Sam Cleggdfb0b2c2018-02-14 18:27:59 +0000113 std::vector<const FunctionSymbol *> IndirectFunctions;
Sam Clegg93102972018-02-23 05:08:53 +0000114 std::vector<const Symbol *> SymtabEntries;
115 std::vector<WasmInitEntry> InitFunctions;
Sam Cleggc94d3932017-11-17 18:14:09 +0000116
Sam Clegg80ba4382018-04-10 16:12:49 +0000117 llvm::StringMap<std::vector<InputSection *>> CustomSectionMapping;
Sam Cleggd177ab22018-05-04 23:14:42 +0000118 llvm::StringMap<SectionSymbol *> CustomSectionSymbols;
Sam Clegg80ba4382018-04-10 16:12:49 +0000119
Sam Cleggc94d3932017-11-17 18:14:09 +0000120 // Elements that are used to construct the final output
121 std::string Header;
122 std::vector<OutputSection *> OutputSections;
123
124 std::unique_ptr<FileOutputBuffer> Buffer;
125
126 std::vector<OutputSegment *> Segments;
127 llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap;
128};
129
130} // anonymous namespace
131
Sam Cleggc94d3932017-11-17 18:14:09 +0000132void Writer::createImportSection() {
Sam Clegg93102972018-02-23 05:08:53 +0000133 uint32_t NumImports = ImportedSymbols.size();
Sam Cleggc94d3932017-11-17 18:14:09 +0000134 if (Config->ImportMemory)
135 ++NumImports;
Nicholas Wilsonfc90b302018-03-28 12:53:29 +0000136 if (Config->ImportTable)
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000137 ++NumImports;
Sam Cleggc94d3932017-11-17 18:14:09 +0000138
139 if (NumImports == 0)
140 return;
141
142 SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT);
143 raw_ostream &OS = Section->getStream();
144
145 writeUleb128(OS, NumImports, "import count");
146
Sam Cleggc94d3932017-11-17 18:14:09 +0000147 if (Config->ImportMemory) {
148 WasmImport Import;
149 Import.Module = "env";
150 Import.Field = "memory";
151 Import.Kind = WASM_EXTERNAL_MEMORY;
152 Import.Memory.Flags = 0;
153 Import.Memory.Initial = NumMemoryPages;
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000154 if (MaxMemoryPages != 0) {
155 Import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX;
156 Import.Memory.Maximum = MaxMemoryPages;
157 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000158 writeImport(OS, Import);
159 }
160
Nicholas Wilsonfc90b302018-03-28 12:53:29 +0000161 if (Config->ImportTable) {
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000162 uint32_t TableSize = kInitialTableOffset + IndirectFunctions.size();
163 WasmImport Import;
164 Import.Module = "env";
165 Import.Field = kFunctionTableName;
166 Import.Kind = WASM_EXTERNAL_TABLE;
167 Import.Table.ElemType = WASM_TYPE_ANYFUNC;
168 Import.Table.Limits = {WASM_LIMITS_FLAG_HAS_MAX, TableSize, TableSize};
169 writeImport(OS, Import);
170 }
171
Sam Clegg93102972018-02-23 05:08:53 +0000172 for (const Symbol *Sym : ImportedSymbols) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000173 WasmImport Import;
174 Import.Module = "env";
175 Import.Field = Sym->getName();
Sam Clegg93102972018-02-23 05:08:53 +0000176 if (auto *FunctionSym = dyn_cast<FunctionSymbol>(Sym)) {
177 Import.Kind = WASM_EXTERNAL_FUNCTION;
178 Import.SigIndex = lookupType(*FunctionSym->getFunctionType());
179 } else {
180 auto *GlobalSym = cast<GlobalSymbol>(Sym);
181 Import.Kind = WASM_EXTERNAL_GLOBAL;
182 Import.Global = *GlobalSym->getGlobalType();
183 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000184 writeImport(OS, Import);
185 }
186}
187
188void Writer::createTypeSection() {
189 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE);
190 raw_ostream &OS = Section->getStream();
191 writeUleb128(OS, Types.size(), "type count");
Sam Cleggd451da12017-12-19 19:56:27 +0000192 for (const WasmSignature *Sig : Types)
Sam Cleggc94d3932017-11-17 18:14:09 +0000193 writeSig(OS, *Sig);
Sam Cleggc94d3932017-11-17 18:14:09 +0000194}
195
196void Writer::createFunctionSection() {
Sam Clegg9f934222018-02-21 18:29:23 +0000197 if (InputFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000198 return;
199
200 SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION);
201 raw_ostream &OS = Section->getStream();
202
Sam Clegg9f934222018-02-21 18:29:23 +0000203 writeUleb128(OS, InputFunctions.size(), "function count");
204 for (const InputFunction *Func : InputFunctions)
Sam Cleggc375e4e2018-01-10 19:18:22 +0000205 writeUleb128(OS, lookupType(Func->Signature), "sig index");
Sam Cleggc94d3932017-11-17 18:14:09 +0000206}
207
208void Writer::createMemorySection() {
209 if (Config->ImportMemory)
210 return;
211
212 SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY);
213 raw_ostream &OS = Section->getStream();
214
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000215 bool HasMax = MaxMemoryPages != 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000216 writeUleb128(OS, 1, "memory count");
Nicholas Wilsonca5cc202018-03-14 21:43:04 +0000217 writeUleb128(OS, HasMax ? static_cast<unsigned>(WASM_LIMITS_FLAG_HAS_MAX) : 0,
218 "memory limits flags");
Sam Cleggc94d3932017-11-17 18:14:09 +0000219 writeUleb128(OS, NumMemoryPages, "initial pages");
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000220 if (HasMax)
221 writeUleb128(OS, MaxMemoryPages, "max pages");
Sam Cleggc94d3932017-11-17 18:14:09 +0000222}
223
224void Writer::createGlobalSection() {
Sam Clegg93102972018-02-23 05:08:53 +0000225 unsigned NumGlobals = InputGlobals.size() + DefinedFakeGlobals.size();
226 if (NumGlobals == 0)
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000227 return;
228
Sam Cleggc94d3932017-11-17 18:14:09 +0000229 SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL);
230 raw_ostream &OS = Section->getStream();
231
Sam Clegg93102972018-02-23 05:08:53 +0000232 writeUleb128(OS, NumGlobals, "global count");
233 for (const InputGlobal *G : InputGlobals)
234 writeGlobal(OS, G->Global);
235 for (const DefinedData *Sym : DefinedFakeGlobals) {
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000236 WasmGlobal Global;
Sam Clegg93102972018-02-23 05:08:53 +0000237 Global.Type = {WASM_TYPE_I32, false};
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000238 Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
239 Global.InitExpr.Value.Int32 = Sym->getVirtualAddress();
Sam Cleggc94d3932017-11-17 18:14:09 +0000240 writeGlobal(OS, Global);
241 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000242}
243
244void Writer::createTableSection() {
Nicholas Wilsonfc90b302018-03-28 12:53:29 +0000245 if (Config->ImportTable)
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000246 return;
247
248 // Always output a table section (or table import), even if there are no
249 // indirect calls. There are two reasons for this:
Sam Cleggfc1a9122017-12-11 22:00:56 +0000250 // 1. For executables it is useful to have an empty table slot at 0
251 // which can be filled with a null function call handler.
252 // 2. If we don't do this, any program that contains a call_indirect but
253 // no address-taken function will fail at validation time since it is
254 // a validation error to include a call_indirect instruction if there
255 // is not table.
Sam Clegg48bbd632018-01-24 21:37:30 +0000256 uint32_t TableSize = kInitialTableOffset + IndirectFunctions.size();
Sam Cleggfc1a9122017-12-11 22:00:56 +0000257
Sam Cleggc94d3932017-11-17 18:14:09 +0000258 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE);
259 raw_ostream &OS = Section->getStream();
260
261 writeUleb128(OS, 1, "table count");
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000262 WasmLimits Limits = {WASM_LIMITS_FLAG_HAS_MAX, TableSize, TableSize};
263 writeTableType(OS, WasmTable{WASM_TYPE_ANYFUNC, Limits});
Sam Cleggc94d3932017-11-17 18:14:09 +0000264}
265
266void Writer::createExportSection() {
Sam Cleggd6beb322018-05-10 18:10:34 +0000267 if (!Exports.size())
Sam Cleggc94d3932017-11-17 18:14:09 +0000268 return;
269
270 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT);
271 raw_ostream &OS = Section->getStream();
272
Sam Cleggd6beb322018-05-10 18:10:34 +0000273 writeUleb128(OS, Exports.size(), "export count");
274 for (const WasmExport &Export : Exports)
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000275 writeExport(OS, Export);
Sam Cleggc94d3932017-11-17 18:14:09 +0000276}
277
Sam Cleggd177ab22018-05-04 23:14:42 +0000278void Writer::calculateCustomSections() {
279 log("calculateCustomSections");
280 bool StripDebug = Config->StripDebug || Config->StripAll;
281 for (ObjFile *File : Symtab->ObjectFiles) {
282 for (InputSection *Section : File->CustomSections) {
283 StringRef Name = Section->getName();
284 // These custom sections are known the linker and synthesized rather than
285 // blindly copied
286 if (Name == "linking" || Name == "name" || Name.startswith("reloc."))
287 continue;
288 // .. or it is a debug section
289 if (StripDebug && Name.startswith(".debug_"))
290 continue;
291 CustomSectionMapping[Name].push_back(Section);
292 }
293 }
294}
295
Sam Clegg80ba4382018-04-10 16:12:49 +0000296void Writer::createCustomSections() {
297 log("createCustomSections");
Sam Clegg80ba4382018-04-10 16:12:49 +0000298 for (auto &Pair : CustomSectionMapping) {
299 StringRef Name = Pair.first();
Sam Cleggd177ab22018-05-04 23:14:42 +0000300
301 auto P = CustomSectionSymbols.find(Name);
302 if (P != CustomSectionSymbols.end()) {
303 uint32_t SectionIndex = OutputSections.size();
304 P->second->setOutputSectionIndex(SectionIndex);
305 }
306
Nicola Zaghene7245b42018-05-15 13:36:20 +0000307 LLVM_DEBUG(dbgs() << "createCustomSection: " << Name << "\n");
Sam Clegg80ba4382018-04-10 16:12:49 +0000308 OutputSections.push_back(make<CustomSection>(Name, Pair.second));
309 }
310}
311
Sam Cleggc94d3932017-11-17 18:14:09 +0000312void Writer::createElemSection() {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000313 if (IndirectFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000314 return;
315
316 SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM);
317 raw_ostream &OS = Section->getStream();
318
319 writeUleb128(OS, 1, "segment count");
320 writeUleb128(OS, 0, "table index");
321 WasmInitExpr InitExpr;
322 InitExpr.Opcode = WASM_OPCODE_I32_CONST;
Sam Clegg48bbd632018-01-24 21:37:30 +0000323 InitExpr.Value.Int32 = kInitialTableOffset;
Sam Cleggc94d3932017-11-17 18:14:09 +0000324 writeInitExpr(OS, InitExpr);
Sam Cleggfc1a9122017-12-11 22:00:56 +0000325 writeUleb128(OS, IndirectFunctions.size(), "elem count");
Sam Cleggc94d3932017-11-17 18:14:09 +0000326
Sam Clegg48bbd632018-01-24 21:37:30 +0000327 uint32_t TableIndex = kInitialTableOffset;
Sam Cleggdfb0b2c2018-02-14 18:27:59 +0000328 for (const FunctionSymbol *Sym : IndirectFunctions) {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000329 assert(Sym->getTableIndex() == TableIndex);
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000330 writeUleb128(OS, Sym->getFunctionIndex(), "function index");
Sam Cleggfc1a9122017-12-11 22:00:56 +0000331 ++TableIndex;
332 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000333}
334
335void Writer::createCodeSection() {
Sam Clegg9f934222018-02-21 18:29:23 +0000336 if (InputFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000337 return;
338
339 log("createCodeSection");
340
Sam Clegg9f934222018-02-21 18:29:23 +0000341 auto Section = make<CodeSection>(InputFunctions);
Sam Cleggc94d3932017-11-17 18:14:09 +0000342 OutputSections.push_back(Section);
343}
344
345void Writer::createDataSection() {
346 if (!Segments.size())
347 return;
348
349 log("createDataSection");
350 auto Section = make<DataSection>(Segments);
351 OutputSections.push_back(Section);
352}
353
Sam Cleggd451da12017-12-19 19:56:27 +0000354// Create relocations sections in the final output.
Sam Cleggc94d3932017-11-17 18:14:09 +0000355// These are only created when relocatable output is requested.
356void Writer::createRelocSections() {
357 log("createRelocSections");
358 // Don't use iterator here since we are adding to OutputSection
359 size_t OrigSize = OutputSections.size();
Rui Ueyamaffa650a2018-04-24 23:09:57 +0000360 for (size_t I = 0; I < OrigSize; I++) {
361 OutputSection *OSec = OutputSections[I];
Rui Ueyama37254062018-02-28 00:01:31 +0000362 uint32_t Count = OSec->numRelocations();
Sam Cleggc94d3932017-11-17 18:14:09 +0000363 if (!Count)
364 continue;
365
Rui Ueyama37254062018-02-28 00:01:31 +0000366 StringRef Name;
367 if (OSec->Type == WASM_SEC_DATA)
368 Name = "reloc.DATA";
369 else if (OSec->Type == WASM_SEC_CODE)
370 Name = "reloc.CODE";
Sam Cleggd177ab22018-05-04 23:14:42 +0000371 else if (OSec->Type == WASM_SEC_CUSTOM)
372 Name = Saver.save("reloc." + OSec->Name);
Sam Cleggc94d3932017-11-17 18:14:09 +0000373 else
Sam Cleggd177ab22018-05-04 23:14:42 +0000374 llvm_unreachable(
375 "relocations only supported for code, data, or custom sections");
Sam Cleggc94d3932017-11-17 18:14:09 +0000376
Rui Ueyama37254062018-02-28 00:01:31 +0000377 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, Name);
Sam Cleggc94d3932017-11-17 18:14:09 +0000378 raw_ostream &OS = Section->getStream();
Rui Ueyamaffa650a2018-04-24 23:09:57 +0000379 writeUleb128(OS, I, "reloc section");
Sam Cleggc94d3932017-11-17 18:14:09 +0000380 writeUleb128(OS, Count, "reloc count");
Rui Ueyama37254062018-02-28 00:01:31 +0000381 OSec->writeRelocations(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000382 }
383}
384
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000385static uint32_t getWasmFlags(const Symbol *Sym) {
386 uint32_t Flags = 0;
387 if (Sym->isLocal())
388 Flags |= WASM_SYMBOL_BINDING_LOCAL;
389 if (Sym->isWeak())
390 Flags |= WASM_SYMBOL_BINDING_WEAK;
391 if (Sym->isHidden())
392 Flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
393 if (Sym->isUndefined())
394 Flags |= WASM_SYMBOL_UNDEFINED;
395 return Flags;
396}
397
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000398// Some synthetic sections (e.g. "name" and "linking") have subsections.
399// Just like the synthetic sections themselves these need to be created before
400// they can be written out (since they are preceded by their length). This
401// class is used to create subsections and then write them into the stream
402// of the parent section.
403class SubSection {
404public:
405 explicit SubSection(uint32_t Type) : Type(Type) {}
406
407 void writeTo(raw_ostream &To) {
408 OS.flush();
Rui Ueyama67769102018-02-28 03:38:14 +0000409 writeUleb128(To, Type, "subsection type");
410 writeUleb128(To, Body.size(), "subsection size");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000411 To.write(Body.data(), Body.size());
412 }
413
414private:
415 uint32_t Type;
416 std::string Body;
417
418public:
419 raw_string_ostream OS{Body};
420};
421
Sam Clegg49ed9262017-12-01 00:53:21 +0000422// Create the custom "linking" section containing linker metadata.
Sam Cleggc94d3932017-11-17 18:14:09 +0000423// This is only created when relocatable output is requested.
424void Writer::createLinkingSection() {
425 SyntheticSection *Section =
426 createSyntheticSection(WASM_SEC_CUSTOM, "linking");
427 raw_ostream &OS = Section->getStream();
428
Sam Clegg2b8b1792018-04-26 18:17:21 +0000429 writeUleb128(OS, WasmMetadataVersion, "Version");
Sam Clegg0d0dd392017-12-19 17:09:45 +0000430
Sam Clegg93102972018-02-23 05:08:53 +0000431 if (!SymtabEntries.empty()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000432 SubSection Sub(WASM_SYMBOL_TABLE);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000433 writeUleb128(Sub.OS, SymtabEntries.size(), "num symbols");
434
Sam Clegg93102972018-02-23 05:08:53 +0000435 for (const Symbol *Sym : SymtabEntries) {
436 assert(Sym->isDefined() || Sym->isUndefined());
437 WasmSymbolType Kind = Sym->getWasmType();
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000438 uint32_t Flags = getWasmFlags(Sym);
439
Sam Clegg8518e7d2018-03-01 18:06:39 +0000440 writeU8(Sub.OS, Kind, "sym kind");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000441 writeUleb128(Sub.OS, Flags, "sym flags");
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000442
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000443 if (auto *F = dyn_cast<FunctionSymbol>(Sym)) {
444 writeUleb128(Sub.OS, F->getFunctionIndex(), "index");
Sam Clegg93102972018-02-23 05:08:53 +0000445 if (Sym->isDefined())
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000446 writeStr(Sub.OS, Sym->getName(), "sym name");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000447 } else if (auto *G = dyn_cast<GlobalSymbol>(Sym)) {
448 writeUleb128(Sub.OS, G->getGlobalIndex(), "index");
449 if (Sym->isDefined())
450 writeStr(Sub.OS, Sym->getName(), "sym name");
Andrea Di Biagio25c1a2f2018-05-05 10:53:31 +0000451 } else if (isa<DataSymbol>(Sym)) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000452 writeStr(Sub.OS, Sym->getName(), "sym name");
Sam Clegg93102972018-02-23 05:08:53 +0000453 if (auto *DataSym = dyn_cast<DefinedData>(Sym)) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000454 writeUleb128(Sub.OS, DataSym->getOutputSegmentIndex(), "index");
455 writeUleb128(Sub.OS, DataSym->getOutputSegmentOffset(),
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000456 "data offset");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000457 writeUleb128(Sub.OS, DataSym->getSize(), "data size");
Sam Clegg93102972018-02-23 05:08:53 +0000458 }
Sam Cleggd177ab22018-05-04 23:14:42 +0000459 } else {
460 auto *S = cast<SectionSymbol>(Sym);
461 writeUleb128(Sub.OS, S->getOutputSectionIndex(), "sym section index");
Sam Clegg93102972018-02-23 05:08:53 +0000462 }
Sam Cleggd3052d52018-01-18 23:40:49 +0000463 }
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000464
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000465 Sub.writeTo(OS);
Sam Cleggd3052d52018-01-18 23:40:49 +0000466 }
467
Sam Clegg0d0dd392017-12-19 17:09:45 +0000468 if (Segments.size()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000469 SubSection Sub(WASM_SEGMENT_INFO);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000470 writeUleb128(Sub.OS, Segments.size(), "num data segments");
Sam Cleggc94d3932017-11-17 18:14:09 +0000471 for (const OutputSegment *S : Segments) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000472 writeStr(Sub.OS, S->Name, "segment name");
473 writeUleb128(Sub.OS, S->Alignment, "alignment");
474 writeUleb128(Sub.OS, 0, "flags");
Sam Cleggc94d3932017-11-17 18:14:09 +0000475 }
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000476 Sub.writeTo(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000477 }
Sam Clegg0d0dd392017-12-19 17:09:45 +0000478
Sam Clegg0d0dd392017-12-19 17:09:45 +0000479 if (!InitFunctions.empty()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000480 SubSection Sub(WASM_INIT_FUNCS);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000481 writeUleb128(Sub.OS, InitFunctions.size(), "num init functions");
Sam Clegg93102972018-02-23 05:08:53 +0000482 for (const WasmInitEntry &F : InitFunctions) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000483 writeUleb128(Sub.OS, F.Priority, "priority");
484 writeUleb128(Sub.OS, F.Sym->getOutputSymbolIndex(), "function index");
Sam Clegg0d0dd392017-12-19 17:09:45 +0000485 }
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000486 Sub.writeTo(OS);
Sam Clegg0d0dd392017-12-19 17:09:45 +0000487 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000488
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000489 struct ComdatEntry {
490 unsigned Kind;
491 uint32_t Index;
492 };
493 std::map<StringRef, std::vector<ComdatEntry>> Comdats;
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000494
Sam Clegg9f934222018-02-21 18:29:23 +0000495 for (const InputFunction *F : InputFunctions) {
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000496 StringRef Comdat = F->getComdatName();
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000497 if (!Comdat.empty())
498 Comdats[Comdat].emplace_back(
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000499 ComdatEntry{WASM_COMDAT_FUNCTION, F->getFunctionIndex()});
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000500 }
501 for (uint32_t I = 0; I < Segments.size(); ++I) {
Sam Cleggf98bccf2018-01-13 15:57:48 +0000502 const auto &InputSegments = Segments[I]->InputSegments;
503 if (InputSegments.empty())
504 continue;
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000505 StringRef Comdat = InputSegments[0]->getComdatName();
Sam Clegga697df522018-01-13 15:59:53 +0000506#ifndef NDEBUG
Sam Cleggf98bccf2018-01-13 15:57:48 +0000507 for (const InputSegment *IS : InputSegments)
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000508 assert(IS->getComdatName() == Comdat);
Sam Clegga697df522018-01-13 15:59:53 +0000509#endif
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000510 if (!Comdat.empty())
511 Comdats[Comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, I});
512 }
513
514 if (!Comdats.empty()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000515 SubSection Sub(WASM_COMDAT_INFO);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000516 writeUleb128(Sub.OS, Comdats.size(), "num comdats");
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000517 for (const auto &C : Comdats) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000518 writeStr(Sub.OS, C.first, "comdat name");
519 writeUleb128(Sub.OS, 0, "comdat flags"); // flags for future use
520 writeUleb128(Sub.OS, C.second.size(), "num entries");
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000521 for (const ComdatEntry &Entry : C.second) {
Sam Clegg8518e7d2018-03-01 18:06:39 +0000522 writeU8(Sub.OS, Entry.Kind, "entry kind");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000523 writeUleb128(Sub.OS, Entry.Index, "entry index");
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000524 }
525 }
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000526 Sub.writeTo(OS);
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000527 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000528}
529
530// Create the custom "name" section containing debug symbol names.
531void Writer::createNameSection() {
Sam Clegg93102972018-02-23 05:08:53 +0000532 unsigned NumNames = NumImportedFunctions;
Sam Clegg9f934222018-02-21 18:29:23 +0000533 for (const InputFunction *F : InputFunctions)
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000534 if (!F->getName().empty() || !F->getDebugName().empty())
Sam Clegg1963d712018-01-17 20:19:04 +0000535 ++NumNames;
Sam Cleggc94d3932017-11-17 18:14:09 +0000536
Sam Clegg1963d712018-01-17 20:19:04 +0000537 if (NumNames == 0)
538 return;
Sam Clegg50686852018-01-12 18:35:13 +0000539
Sam Cleggc94d3932017-11-17 18:14:09 +0000540 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name");
541
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000542 SubSection Sub(WASM_NAMES_FUNCTION);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000543 writeUleb128(Sub.OS, NumNames, "name count");
Sam Cleggc94d3932017-11-17 18:14:09 +0000544
Sam Clegg93102972018-02-23 05:08:53 +0000545 // Names must appear in function index order. As it happens ImportedSymbols
546 // and InputFunctions are numbered in order with imported functions coming
Sam Clegg1963d712018-01-17 20:19:04 +0000547 // first.
Sam Clegg93102972018-02-23 05:08:53 +0000548 for (const Symbol *S : ImportedSymbols) {
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000549 if (auto *F = dyn_cast<FunctionSymbol>(S)) {
550 writeUleb128(Sub.OS, F->getFunctionIndex(), "func index");
Nicholas Wilson531769b2018-03-13 13:30:04 +0000551 Optional<std::string> Name = demangleItanium(F->getName());
552 writeStr(Sub.OS, Name ? StringRef(*Name) : F->getName(), "symbol name");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000553 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000554 }
Sam Clegg9f934222018-02-21 18:29:23 +0000555 for (const InputFunction *F : InputFunctions) {
Sam Clegg1963d712018-01-17 20:19:04 +0000556 if (!F->getName().empty()) {
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000557 writeUleb128(Sub.OS, F->getFunctionIndex(), "func index");
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000558 if (!F->getDebugName().empty()) {
559 writeStr(Sub.OS, F->getDebugName(), "symbol name");
560 } else {
561 Optional<std::string> Name = demangleItanium(F->getName());
562 writeStr(Sub.OS, Name ? StringRef(*Name) : F->getName(), "symbol name");
563 }
Sam Clegg1963d712018-01-17 20:19:04 +0000564 }
565 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000566
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000567 Sub.writeTo(Section->getStream());
Sam Cleggc94d3932017-11-17 18:14:09 +0000568}
569
570void Writer::writeHeader() {
571 memcpy(Buffer->getBufferStart(), Header.data(), Header.size());
572}
573
574void Writer::writeSections() {
575 uint8_t *Buf = Buffer->getBufferStart();
576 parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); });
577}
578
579// Fix the memory layout of the output binary. This assigns memory offsets
Sam Clegg49ed9262017-12-01 00:53:21 +0000580// to each of the input data sections as well as the explicit stack region.
Sam Clegga0f095e2018-05-03 17:21:53 +0000581// The default memory layout is as follows, from low to high.
582//
Sam Cleggf0d433d2018-02-02 22:59:56 +0000583// - initialized data (starting at Config->GlobalBase)
584// - BSS data (not currently implemented in llvm)
585// - explicit stack (Config->ZStackSize)
586// - heap start / unallocated
Sam Clegga0f095e2018-05-03 17:21:53 +0000587//
588// The --stack-first option means that stack is placed before any static data.
589// This can be useful since it means that stack overflow traps immediately rather
590// than overwriting global data, but also increases code size since all static
591// data loads and stores requires larger offsets.
Sam Cleggc94d3932017-11-17 18:14:09 +0000592void Writer::layoutMemory() {
Sam Cleggc94d3932017-11-17 18:14:09 +0000593 createOutputSegments();
594
Sam Clegga0f095e2018-05-03 17:21:53 +0000595 uint32_t MemoryPtr = 0;
596
597 auto PlaceStack = [&]() {
598 if (Config->Relocatable)
599 return;
600 MemoryPtr = alignTo(MemoryPtr, kStackAlignment);
601 if (Config->ZStackSize != alignTo(Config->ZStackSize, kStackAlignment))
602 error("stack size must be " + Twine(kStackAlignment) + "-byte aligned");
603 log("mem: stack size = " + Twine(Config->ZStackSize));
604 log("mem: stack base = " + Twine(MemoryPtr));
605 MemoryPtr += Config->ZStackSize;
606 WasmSym::StackPointer->Global->Global.InitExpr.Value.Int32 = MemoryPtr;
607 log("mem: stack top = " + Twine(MemoryPtr));
608 };
609
610 if (Config->StackFirst) {
611 PlaceStack();
612 } else {
613 MemoryPtr = Config->GlobalBase;
614 log("mem: global base = " + Twine(Config->GlobalBase));
615 }
616
617 uint32_t DataStart = MemoryPtr;
618
Sam Cleggf0d433d2018-02-02 22:59:56 +0000619 // Arbitrarily set __dso_handle handle to point to the start of the data
620 // segments.
621 if (WasmSym::DsoHandle)
Sam Clegga0f095e2018-05-03 17:21:53 +0000622 WasmSym::DsoHandle->setVirtualAddress(DataStart);
Sam Cleggf0d433d2018-02-02 22:59:56 +0000623
Sam Cleggc94d3932017-11-17 18:14:09 +0000624 for (OutputSegment *Seg : Segments) {
625 MemoryPtr = alignTo(MemoryPtr, Seg->Alignment);
626 Seg->StartVA = MemoryPtr;
Nicholas Wilsona06a3552018-03-14 13:50:20 +0000627 log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", Seg->Name,
628 MemoryPtr, Seg->Size, Seg->Alignment));
Sam Cleggc94d3932017-11-17 18:14:09 +0000629 MemoryPtr += Seg->Size;
630 }
631
Sam Cleggf0d433d2018-02-02 22:59:56 +0000632 // TODO: Add .bss space here.
Sam Clegg37a4a8a2018-02-07 03:04:53 +0000633 if (WasmSym::DataEnd)
634 WasmSym::DataEnd->setVirtualAddress(MemoryPtr);
Sam Cleggf0d433d2018-02-02 22:59:56 +0000635
Sam Clegga0f095e2018-05-03 17:21:53 +0000636 log("mem: static data = " + Twine(MemoryPtr - DataStart));
Sam Cleggc94d3932017-11-17 18:14:09 +0000637
Sam Clegga0f095e2018-05-03 17:21:53 +0000638 if (!Config->StackFirst)
639 PlaceStack();
640
641 // Set `__heap_base` to directly follow the end of the stack or global data.
642 // The fact that this comes last means that a malloc/brk implementation
643 // can grow the heap at runtime.
Sam Cleggc94d3932017-11-17 18:14:09 +0000644 if (!Config->Relocatable) {
Sam Cleggf0d433d2018-02-02 22:59:56 +0000645 WasmSym::HeapBase->setVirtualAddress(MemoryPtr);
Nicholas Wilsona06a3552018-03-14 13:50:20 +0000646 log("mem: heap base = " + Twine(MemoryPtr));
Sam Cleggc94d3932017-11-17 18:14:09 +0000647 }
648
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000649 if (Config->InitialMemory != 0) {
650 if (Config->InitialMemory != alignTo(Config->InitialMemory, WasmPageSize))
651 error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned");
652 if (MemoryPtr > Config->InitialMemory)
653 error("initial memory too small, " + Twine(MemoryPtr) + " bytes needed");
654 else
655 MemoryPtr = Config->InitialMemory;
656 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000657 uint32_t MemSize = alignTo(MemoryPtr, WasmPageSize);
658 NumMemoryPages = MemSize / WasmPageSize;
Nicholas Wilsona06a3552018-03-14 13:50:20 +0000659 log("mem: total pages = " + Twine(NumMemoryPages));
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000660
661 if (Config->MaxMemory != 0) {
662 if (Config->MaxMemory != alignTo(Config->MaxMemory, WasmPageSize))
663 error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned");
664 if (MemoryPtr > Config->MaxMemory)
665 error("maximum memory too small, " + Twine(MemoryPtr) + " bytes needed");
666 MaxMemoryPages = Config->MaxMemory / WasmPageSize;
667 log("mem: max pages = " + Twine(MaxMemoryPages));
668 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000669}
670
671SyntheticSection *Writer::createSyntheticSection(uint32_t Type,
Sam Cleggc375e4e2018-01-10 19:18:22 +0000672 StringRef Name) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000673 auto Sec = make<SyntheticSection>(Type, Name);
Sam Cleggab2ac292017-12-20 05:14:48 +0000674 log("createSection: " + toString(*Sec));
Sam Cleggc94d3932017-11-17 18:14:09 +0000675 OutputSections.push_back(Sec);
676 return Sec;
677}
678
679void Writer::createSections() {
680 // Known sections
681 createTypeSection();
682 createImportSection();
683 createFunctionSection();
684 createTableSection();
685 createMemorySection();
686 createGlobalSection();
687 createExportSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000688 createElemSection();
689 createCodeSection();
690 createDataSection();
Sam Clegg80ba4382018-04-10 16:12:49 +0000691 createCustomSections();
Sam Cleggc94d3932017-11-17 18:14:09 +0000692
693 // Custom sections
Sam Clegg99eb42c2018-02-27 23:58:03 +0000694 if (Config->Relocatable) {
Sam Clegg99eb42c2018-02-27 23:58:03 +0000695 createLinkingSection();
Nicholas Wilson94d3b162018-03-05 12:33:58 +0000696 createRelocSections();
Sam Clegg99eb42c2018-02-27 23:58:03 +0000697 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000698 if (!Config->StripDebug && !Config->StripAll)
699 createNameSection();
700
701 for (OutputSection *S : OutputSections) {
702 S->setOffset(FileSize);
703 S->finalizeContents();
704 FileSize += S->getSize();
705 }
706}
707
Sam Cleggc94d3932017-11-17 18:14:09 +0000708void Writer::calculateImports() {
Sam Clegg574d7ce2017-12-15 19:23:49 +0000709 for (Symbol *Sym : Symtab->getSymbols()) {
Sam Clegg93102972018-02-23 05:08:53 +0000710 if (!Sym->isUndefined())
711 continue;
712 if (isa<DataSymbol>(Sym))
713 continue;
714 if (Sym->isWeak() && !Config->Relocatable)
Sam Clegg574d7ce2017-12-15 19:23:49 +0000715 continue;
Nicholas Wilsona1e299f2018-04-20 17:18:06 +0000716 if (!Sym->isLive())
717 continue;
Sam Cleggc729c1b2018-05-30 18:07:52 +0000718 if (!Sym->IsUsedInRegularObj)
719 continue;
Sam Cleggc94d3932017-11-17 18:14:09 +0000720
Nicola Zaghene7245b42018-05-15 13:36:20 +0000721 LLVM_DEBUG(dbgs() << "import: " << Sym->getName() << "\n");
Sam Clegg93102972018-02-23 05:08:53 +0000722 ImportedSymbols.emplace_back(Sym);
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000723 if (auto *F = dyn_cast<FunctionSymbol>(Sym))
724 F->setFunctionIndex(NumImportedFunctions++);
Sam Clegg93102972018-02-23 05:08:53 +0000725 else
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000726 cast<GlobalSymbol>(Sym)->setGlobalIndex(NumImportedGlobals++);
Sam Cleggc94d3932017-11-17 18:14:09 +0000727 }
728}
729
Sam Cleggd3052d52018-01-18 23:40:49 +0000730void Writer::calculateExports() {
Sam Clegg93102972018-02-23 05:08:53 +0000731 if (Config->Relocatable)
732 return;
Sam Cleggf0d433d2018-02-02 22:59:56 +0000733
Sam Cleggd6beb322018-05-10 18:10:34 +0000734 if (!Config->Relocatable && !Config->ImportMemory)
735 Exports.push_back(WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0});
736
737 if (!Config->Relocatable && Config->ExportTable)
738 Exports.push_back(WasmExport{kFunctionTableName, WASM_EXTERNAL_TABLE, 0});
739
740 unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size();
741
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +0000742 for (Symbol *Sym : Symtab->getSymbols()) {
Sam Clegg93102972018-02-23 05:08:53 +0000743 if (!Sym->isDefined())
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +0000744 continue;
Sam Clegg93102972018-02-23 05:08:53 +0000745 if (Sym->isHidden() || Sym->isLocal())
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +0000746 continue;
Sam Clegg93102972018-02-23 05:08:53 +0000747 if (!Sym->isLive())
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +0000748 continue;
Sam Clegg93102972018-02-23 05:08:53 +0000749
Sam Cleggd6beb322018-05-10 18:10:34 +0000750 StringRef Name = Sym->getName();
751 WasmExport Export;
752 if (auto *F = dyn_cast<DefinedFunction>(Sym)) {
753 Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()};
754 } else if (auto *G = dyn_cast<DefinedGlobal>(Sym)) {
755 Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()};
756 } else {
757 auto *D = cast<DefinedData>(Sym);
Sam Clegg93102972018-02-23 05:08:53 +0000758 DefinedFakeGlobals.emplace_back(D);
Sam Cleggd6beb322018-05-10 18:10:34 +0000759 Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++};
760 }
761
Nicola Zaghene7245b42018-05-15 13:36:20 +0000762 LLVM_DEBUG(dbgs() << "Export: " << Name << "\n");
Sam Cleggd6beb322018-05-10 18:10:34 +0000763 Exports.push_back(Export);
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +0000764 }
Sam Clegg93102972018-02-23 05:08:53 +0000765}
766
767void Writer::assignSymtab() {
768 if (!Config->Relocatable)
769 return;
770
Sam Cleggd177ab22018-05-04 23:14:42 +0000771 StringMap<uint32_t> SectionSymbolIndices;
772
Sam Clegg93102972018-02-23 05:08:53 +0000773 unsigned SymbolIndex = SymtabEntries.size();
Sam Cleggd3052d52018-01-18 23:40:49 +0000774 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000775 LLVM_DEBUG(dbgs() << "Symtab entries: " << File->getName() << "\n");
Sam Cleggd3052d52018-01-18 23:40:49 +0000776 for (Symbol *Sym : File->getSymbols()) {
Sam Clegg93102972018-02-23 05:08:53 +0000777 if (Sym->getFile() != File)
Sam Cleggd3052d52018-01-18 23:40:49 +0000778 continue;
Sam Cleggd177ab22018-05-04 23:14:42 +0000779
780 if (auto *S = dyn_cast<SectionSymbol>(Sym)) {
781 StringRef Name = S->getName();
782 if (CustomSectionMapping.count(Name) == 0)
783 continue;
784
785 auto SSI = SectionSymbolIndices.find(Name);
786 if (SSI != SectionSymbolIndices.end()) {
787 Sym->setOutputSymbolIndex(SSI->second);
788 continue;
789 }
790
791 SectionSymbolIndices[Name] = SymbolIndex;
792 CustomSectionSymbols[Name] = cast<SectionSymbol>(Sym);
793
794 Sym->markLive();
795 }
796
Nicholas Wilson06e0d172018-03-07 11:15:47 +0000797 // (Since this is relocatable output, GC is not performed so symbols must
798 // be live.)
799 assert(Sym->isLive());
Sam Clegg93102972018-02-23 05:08:53 +0000800 Sym->setOutputSymbolIndex(SymbolIndex++);
801 SymtabEntries.emplace_back(Sym);
Sam Cleggd3052d52018-01-18 23:40:49 +0000802 }
803 }
804
Sam Clegg93102972018-02-23 05:08:53 +0000805 // For the moment, relocatable output doesn't contain any synthetic functions,
806 // so no need to look through the Symtab for symbols not referenced by
807 // Symtab->ObjectFiles.
Sam Cleggd3052d52018-01-18 23:40:49 +0000808}
809
Sam Cleggc375e4e2018-01-10 19:18:22 +0000810uint32_t Writer::lookupType(const WasmSignature &Sig) {
Sam Clegg8d027d62018-01-10 20:12:26 +0000811 auto It = TypeIndices.find(Sig);
812 if (It == TypeIndices.end()) {
Sam Cleggc375e4e2018-01-10 19:18:22 +0000813 error("type not found: " + toString(Sig));
Sam Clegg8d027d62018-01-10 20:12:26 +0000814 return 0;
815 }
816 return It->second;
Sam Cleggc375e4e2018-01-10 19:18:22 +0000817}
818
819uint32_t Writer::registerType(const WasmSignature &Sig) {
Sam Cleggb8621592017-11-30 01:40:08 +0000820 auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size()));
Sam Cleggc375e4e2018-01-10 19:18:22 +0000821 if (Pair.second) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000822 LLVM_DEBUG(dbgs() << "type " << toString(Sig) << "\n");
Sam Cleggb8621592017-11-30 01:40:08 +0000823 Types.push_back(&Sig);
Sam Cleggc375e4e2018-01-10 19:18:22 +0000824 }
Sam Cleggb8621592017-11-30 01:40:08 +0000825 return Pair.first->second;
826}
827
Sam Cleggc94d3932017-11-17 18:14:09 +0000828void Writer::calculateTypes() {
Sam Clegg8f6d2de2018-01-31 23:48:14 +0000829 // The output type section is the union of the following sets:
830 // 1. Any signature used in the TYPE relocation
831 // 2. The signatures of all imported functions
832 // 3. The signatures of all defined functions
833
Sam Cleggc94d3932017-11-17 18:14:09 +0000834 for (ObjFile *File : Symtab->ObjectFiles) {
Sam Clegg8f6d2de2018-01-31 23:48:14 +0000835 ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
836 for (uint32_t I = 0; I < Types.size(); I++)
837 if (File->TypeIsUsed[I])
838 File->TypeMap[I] = registerType(Types[I]);
Sam Cleggc94d3932017-11-17 18:14:09 +0000839 }
Sam Clegg50686852018-01-12 18:35:13 +0000840
Sam Clegg93102972018-02-23 05:08:53 +0000841 for (const Symbol *Sym : ImportedSymbols)
842 if (auto *F = dyn_cast<FunctionSymbol>(Sym))
843 registerType(*F->getFunctionType());
Sam Clegg8f6d2de2018-01-31 23:48:14 +0000844
Sam Clegg9f934222018-02-21 18:29:23 +0000845 for (const InputFunction *F : InputFunctions)
Sam Clegg8f6d2de2018-01-31 23:48:14 +0000846 registerType(F->Signature);
Sam Cleggc94d3932017-11-17 18:14:09 +0000847}
848
Sam Clegg8d146bb2018-01-09 23:56:44 +0000849void Writer::assignIndexes() {
Sam Clegg93102972018-02-23 05:08:53 +0000850 uint32_t FunctionIndex = NumImportedFunctions + InputFunctions.size();
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000851 auto AddDefinedFunction = [&](InputFunction *Func) {
852 if (!Func->Live)
853 return;
854 InputFunctions.emplace_back(Func);
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000855 Func->setFunctionIndex(FunctionIndex++);
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000856 };
857
Nicholas Wilson5639da82018-03-12 15:44:07 +0000858 for (InputFunction *Func : Symtab->SyntheticFunctions)
859 AddDefinedFunction(Func);
860
Sam Clegg87e61922018-01-08 23:39:11 +0000861 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000862 LLVM_DEBUG(dbgs() << "Functions: " << File->getName() << "\n");
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000863 for (InputFunction *Func : File->Functions)
864 AddDefinedFunction(Func);
Sam Clegg8d146bb2018-01-09 23:56:44 +0000865 }
866
Sam Clegg93102972018-02-23 05:08:53 +0000867 uint32_t TableIndex = kInitialTableOffset;
Sam Clegg6c4dbfee2018-02-23 04:59:57 +0000868 auto HandleRelocs = [&](InputChunk *Chunk) {
869 if (!Chunk->Live)
870 return;
871 ObjFile *File = Chunk->File;
872 ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
Sam Clegg93102972018-02-23 05:08:53 +0000873 for (const WasmRelocation &Reloc : Chunk->getRelocations()) {
Sam Clegg6c4dbfee2018-02-23 04:59:57 +0000874 if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32 ||
875 Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB) {
876 FunctionSymbol *Sym = File->getFunctionSymbol(Reloc.Index);
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000877 if (Sym->hasTableIndex() || !Sym->hasFunctionIndex())
Sam Clegg6c4dbfee2018-02-23 04:59:57 +0000878 continue;
879 Sym->setTableIndex(TableIndex++);
880 IndirectFunctions.emplace_back(Sym);
881 } else if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB) {
Sam Clegg93102972018-02-23 05:08:53 +0000882 // Mark target type as live
Sam Clegg6c4dbfee2018-02-23 04:59:57 +0000883 File->TypeMap[Reloc.Index] = registerType(Types[Reloc.Index]);
884 File->TypeIsUsed[Reloc.Index] = true;
885 }
886 }
887 };
888
Sam Clegg8d146bb2018-01-09 23:56:44 +0000889 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000890 LLVM_DEBUG(dbgs() << "Handle relocs: " << File->getName() << "\n");
Sam Clegg93102972018-02-23 05:08:53 +0000891 for (InputChunk *Chunk : File->Functions)
892 HandleRelocs(Chunk);
893 for (InputChunk *Chunk : File->Segments)
894 HandleRelocs(Chunk);
Sam Cleggd177ab22018-05-04 23:14:42 +0000895 for (auto &P : File->CustomSections)
896 HandleRelocs(P);
Sam Clegg93102972018-02-23 05:08:53 +0000897 }
Sam Clegg8f6d2de2018-01-31 23:48:14 +0000898
Sam Clegg93102972018-02-23 05:08:53 +0000899 uint32_t GlobalIndex = NumImportedGlobals + InputGlobals.size();
900 auto AddDefinedGlobal = [&](InputGlobal *Global) {
901 if (Global->Live) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000902 LLVM_DEBUG(dbgs() << "AddDefinedGlobal: " << GlobalIndex << "\n");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000903 Global->setGlobalIndex(GlobalIndex++);
Sam Clegg93102972018-02-23 05:08:53 +0000904 InputGlobals.push_back(Global);
905 }
906 };
907
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000908 for (InputGlobal *Global : Symtab->SyntheticGlobals)
909 AddDefinedGlobal(Global);
Sam Clegg93102972018-02-23 05:08:53 +0000910
911 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000912 LLVM_DEBUG(dbgs() << "Globals: " << File->getName() << "\n");
Sam Clegg93102972018-02-23 05:08:53 +0000913 for (InputGlobal *Global : File->Globals)
914 AddDefinedGlobal(Global);
Sam Cleggc94d3932017-11-17 18:14:09 +0000915 }
916}
917
918static StringRef getOutputDataSegmentName(StringRef Name) {
Sam Clegg66844762018-05-10 18:23:51 +0000919 if (!Config->MergeDataSegments)
Sam Cleggc94d3932017-11-17 18:14:09 +0000920 return Name;
Rui Ueyama4764b572018-02-28 00:57:28 +0000921 if (Name.startswith(".text."))
922 return ".text";
923 if (Name.startswith(".data."))
924 return ".data";
925 if (Name.startswith(".bss."))
926 return ".bss";
Sam Cleggc94d3932017-11-17 18:14:09 +0000927 return Name;
928}
929
930void Writer::createOutputSegments() {
931 for (ObjFile *File : Symtab->ObjectFiles) {
932 for (InputSegment *Segment : File->Segments) {
Sam Clegg447ae402018-02-13 20:29:38 +0000933 if (!Segment->Live)
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000934 continue;
Sam Cleggc94d3932017-11-17 18:14:09 +0000935 StringRef Name = getOutputDataSegmentName(Segment->getName());
936 OutputSegment *&S = SegmentMap[Name];
937 if (S == nullptr) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000938 LLVM_DEBUG(dbgs() << "new segment: " << Name << "\n");
Sam Clegg93102972018-02-23 05:08:53 +0000939 S = make<OutputSegment>(Name, Segments.size());
Sam Cleggc94d3932017-11-17 18:14:09 +0000940 Segments.push_back(S);
941 }
942 S->addInputSegment(Segment);
Nicola Zaghene7245b42018-05-15 13:36:20 +0000943 LLVM_DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n");
Sam Cleggc94d3932017-11-17 18:14:09 +0000944 }
945 }
946}
947
Sam Clegg50686852018-01-12 18:35:13 +0000948static const int OPCODE_CALL = 0x10;
949static const int OPCODE_END = 0xb;
950
951// Create synthetic "__wasm_call_ctors" function based on ctor functions
952// in input object.
953void Writer::createCtorFunction() {
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +0000954 // First write the body's contents to a string.
955 std::string BodyContent;
Sam Clegg50686852018-01-12 18:35:13 +0000956 {
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +0000957 raw_string_ostream OS(BodyContent);
Sam Clegg50686852018-01-12 18:35:13 +0000958 writeUleb128(OS, 0, "num locals");
Sam Clegg93102972018-02-23 05:08:53 +0000959 for (const WasmInitEntry &F : InitFunctions) {
Sam Clegg50686852018-01-12 18:35:13 +0000960 writeU8(OS, OPCODE_CALL, "CALL");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000961 writeUleb128(OS, F.Sym->getFunctionIndex(), "function index");
Sam Clegg50686852018-01-12 18:35:13 +0000962 }
963 writeU8(OS, OPCODE_END, "END");
964 }
965
966 // Once we know the size of the body we can create the final function body
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +0000967 std::string FunctionBody;
968 {
969 raw_string_ostream OS(FunctionBody);
970 writeUleb128(OS, BodyContent.size(), "function size");
971 OS << BodyContent;
972 }
Rui Ueyama29abfe42018-02-28 17:43:15 +0000973
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000974 ArrayRef<uint8_t> Body = toArrayRef(Saver.save(FunctionBody));
975 cast<SyntheticFunction>(WasmSym::CallCtors->Function)->setBody(Body);
Sam Clegg50686852018-01-12 18:35:13 +0000976}
977
978// Populate InitFunctions vector with init functions from all input objects.
979// This is then used either when creating the output linking section or to
980// synthesize the "__wasm_call_ctors" function.
981void Writer::calculateInitFunctions() {
982 for (ObjFile *File : Symtab->ObjectFiles) {
983 const WasmLinkingData &L = File->getWasmObj()->linkingData();
Nicholas Wilsoncb81a0c2018-03-02 14:46:54 +0000984 for (const WasmInitFunc &F : L.InitFunctions) {
985 FunctionSymbol *Sym = File->getFunctionSymbol(F.Symbol);
986 if (*Sym->getFunctionType() != WasmSignature{{}, WASM_TYPE_NORESULT})
987 error("invalid signature for init func: " + toString(*Sym));
988 InitFunctions.emplace_back(WasmInitEntry{Sym, F.Priority});
989 }
Sam Clegg50686852018-01-12 18:35:13 +0000990 }
Rui Ueyamada69b712018-02-28 00:15:59 +0000991
Sam Clegg50686852018-01-12 18:35:13 +0000992 // Sort in order of priority (lowest first) so that they are called
993 // in the correct order.
Sam Clegg29b8feb2018-02-21 00:34:34 +0000994 std::stable_sort(InitFunctions.begin(), InitFunctions.end(),
Sam Clegg93102972018-02-23 05:08:53 +0000995 [](const WasmInitEntry &L, const WasmInitEntry &R) {
Sam Clegg29b8feb2018-02-21 00:34:34 +0000996 return L.Priority < R.Priority;
997 });
Sam Clegg50686852018-01-12 18:35:13 +0000998}
999
Sam Cleggc94d3932017-11-17 18:14:09 +00001000void Writer::run() {
Sam Clegg99eb42c2018-02-27 23:58:03 +00001001 if (Config->Relocatable)
1002 Config->GlobalBase = 0;
1003
Sam Cleggc94d3932017-11-17 18:14:09 +00001004 log("-- calculateImports");
1005 calculateImports();
Sam Clegg8d146bb2018-01-09 23:56:44 +00001006 log("-- assignIndexes");
1007 assignIndexes();
Sam Clegg50686852018-01-12 18:35:13 +00001008 log("-- calculateInitFunctions");
1009 calculateInitFunctions();
1010 if (!Config->Relocatable)
1011 createCtorFunction();
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001012 log("-- calculateTypes");
1013 calculateTypes();
Sam Clegg93102972018-02-23 05:08:53 +00001014 log("-- layoutMemory");
1015 layoutMemory();
1016 log("-- calculateExports");
1017 calculateExports();
Sam Cleggd177ab22018-05-04 23:14:42 +00001018 log("-- calculateCustomSections");
1019 calculateCustomSections();
Sam Clegg93102972018-02-23 05:08:53 +00001020 log("-- assignSymtab");
1021 assignSymtab();
Sam Cleggc94d3932017-11-17 18:14:09 +00001022
1023 if (errorHandler().Verbose) {
Sam Clegg9f934222018-02-21 18:29:23 +00001024 log("Defined Functions: " + Twine(InputFunctions.size()));
Sam Clegg93102972018-02-23 05:08:53 +00001025 log("Defined Globals : " + Twine(InputGlobals.size()));
1026 log("Function Imports : " + Twine(NumImportedFunctions));
1027 log("Global Imports : " + Twine(NumImportedGlobals));
Sam Cleggc94d3932017-11-17 18:14:09 +00001028 for (ObjFile *File : Symtab->ObjectFiles)
1029 File->dumpInfo();
1030 }
1031
Sam Cleggc94d3932017-11-17 18:14:09 +00001032 createHeader();
1033 log("-- createSections");
1034 createSections();
1035
1036 log("-- openFile");
1037 openFile();
1038 if (errorCount())
1039 return;
1040
1041 writeHeader();
1042
1043 log("-- writeSections");
1044 writeSections();
1045 if (errorCount())
1046 return;
1047
1048 if (Error E = Buffer->commit())
1049 fatal("failed to write the output file: " + toString(std::move(E)));
1050}
1051
1052// Open a result file.
1053void Writer::openFile() {
1054 log("writing: " + Config->OutputFile);
Sam Cleggc94d3932017-11-17 18:14:09 +00001055
1056 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1057 FileOutputBuffer::create(Config->OutputFile, FileSize,
1058 FileOutputBuffer::F_executable);
1059
1060 if (!BufferOrErr)
1061 error("failed to open " + Config->OutputFile + ": " +
1062 toString(BufferOrErr.takeError()));
1063 else
1064 Buffer = std::move(*BufferOrErr);
1065}
1066
1067void Writer::createHeader() {
1068 raw_string_ostream OS(Header);
1069 writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic");
1070 writeU32(OS, WasmVersion, "wasm version");
1071 OS.flush();
1072 FileSize += Header.size();
1073}
1074
1075void lld::wasm::writeResult() { Writer().run(); }