blob: 5f12ce73922229f83dc2c08d8d1fbd9ad90ae1e0 [file] [log] [blame]
Sam Cleggc94d3932017-11-17 18:14:09 +00001//===- Writer.cpp ---------------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sam Cleggc94d3932017-11-17 18:14:09 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "Writer.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000010#include "Config.h"
Sam Clegg5fa274b2018-01-10 01:13:34 +000011#include "InputChunks.h"
Heejin Ahne915a712018-12-08 06:17:43 +000012#include "InputEvent.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"
Thomas Livelyf6f4f842019-03-20 20:26:45 +000023#include "llvm/ADT/SmallSet.h"
Thomas Lively2a0868f2019-01-17 02:29:41 +000024#include "llvm/ADT/SmallVector.h"
Sam Clegg80ba4382018-04-10 16:12:49 +000025#include "llvm/ADT/StringMap.h"
Sam Clegg93102972018-02-23 05:08:53 +000026#include "llvm/BinaryFormat/Wasm.h"
Nicholas Wilson3e3f5fb2018-03-14 15:58:16 +000027#include "llvm/Object/WasmTraits.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000028#include "llvm/Support/FileOutputBuffer.h"
29#include "llvm/Support/Format.h"
30#include "llvm/Support/FormatVariadic.h"
31#include "llvm/Support/LEB128.h"
Sam Clegga688a422019-03-13 21:29:20 +000032#include "llvm/Support/Path.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000033
34#include <cstdarg>
Sam Clegge0f6fcd2018-01-12 22:25:17 +000035#include <map>
Sam Cleggc94d3932017-11-17 18:14:09 +000036
37#define DEBUG_TYPE "lld"
38
39using namespace llvm;
40using namespace llvm::wasm;
41using namespace lld;
42using namespace lld::wasm;
43
Heejin Ahna1cc4ea2019-02-04 19:13:46 +000044static constexpr int StackAlignment = 16;
45static constexpr const char *FunctionTableName = "__indirect_function_table";
46const char *lld::wasm::DefaultModule = "env";
Sam Cleggc94d3932017-11-17 18:14:09 +000047
48namespace {
49
Sam Clegg93102972018-02-23 05:08:53 +000050// An init entry to be written to either the synthetic init func or the
51// linking metadata.
52struct WasmInitEntry {
Sam Clegge3f3ccf2018-03-12 19:56:23 +000053 const FunctionSymbol *Sym;
Sam Clegg93102972018-02-23 05:08:53 +000054 uint32_t Priority;
Sam Cleggd3052d52018-01-18 23:40:49 +000055};
56
Sam Cleggc94d3932017-11-17 18:14:09 +000057// The writer writes a SymbolTable result to a file.
58class Writer {
59public:
60 void run();
61
62private:
63 void openFile();
64
Sam Cleggc375e4e2018-01-10 19:18:22 +000065 uint32_t lookupType(const WasmSignature &Sig);
66 uint32_t registerType(const WasmSignature &Sig);
Sam Clegg93102972018-02-23 05:08:53 +000067
Sam Clegg50686852018-01-12 18:35:13 +000068 void createCtorFunction();
69 void calculateInitFunctions();
Sam Clegg632c21792019-03-16 01:18:12 +000070 void processRelocations(InputChunk *Chunk);
Sam Clegg8d146bb2018-01-09 23:56:44 +000071 void assignIndexes();
Thomas Livelyf6f4f842019-03-20 20:26:45 +000072 void calculateTargetFeatures();
Sam Cleggc94d3932017-11-17 18:14:09 +000073 void calculateImports();
Sam Cleggd3052d52018-01-18 23:40:49 +000074 void calculateExports();
Sam Cleggd177ab22018-05-04 23:14:42 +000075 void calculateCustomSections();
Sam Clegg93102972018-02-23 05:08:53 +000076 void assignSymtab();
Sam Cleggc94d3932017-11-17 18:14:09 +000077 void calculateTypes();
78 void createOutputSegments();
79 void layoutMemory();
80 void createHeader();
81 void createSections();
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +000082 SyntheticSection *createSyntheticSection(uint32_t Type, StringRef Name = "");
Sam Cleggc94d3932017-11-17 18:14:09 +000083
84 // Builtin sections
85 void createTypeSection();
86 void createFunctionSection();
87 void createTableSection();
88 void createGlobalSection();
Heejin Ahne915a712018-12-08 06:17:43 +000089 void createEventSection();
Sam Cleggc94d3932017-11-17 18:14:09 +000090 void createExportSection();
91 void createImportSection();
92 void createMemorySection();
93 void createElemSection();
Sam Cleggc94d3932017-11-17 18:14:09 +000094 void createCodeSection();
95 void createDataSection();
Sam Clegg80ba4382018-04-10 16:12:49 +000096 void createCustomSections();
Sam Cleggc94d3932017-11-17 18:14:09 +000097
98 // Custom sections
Sam Cleggbfb75342018-11-15 00:37:21 +000099 void createDylinkSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000100 void createRelocSections();
101 void createLinkingSection();
102 void createNameSection();
Thomas Lively2a0868f2019-01-17 02:29:41 +0000103 void createProducersSection();
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000104 void createTargetFeaturesSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000105
106 void writeHeader();
107 void writeSections();
108
109 uint64_t FileSize = 0;
Sam Cleggbfb75342018-11-15 00:37:21 +0000110 uint32_t TableBase = 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000111 uint32_t NumMemoryPages = 0;
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000112 uint32_t MaxMemoryPages = 0;
Sam Cleggbfb75342018-11-15 00:37:21 +0000113 // Memory size and aligment. Written to the "dylink" section
114 // when build with -shared or -pie.
115 uint32_t MemAlign = 0;
116 uint32_t MemSize = 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000117
118 std::vector<const WasmSignature *> Types;
Nicholas Wilson3e3f5fb2018-03-14 15:58:16 +0000119 DenseMap<WasmSignature, int32_t> TypeIndices;
Sam Clegg93102972018-02-23 05:08:53 +0000120 std::vector<const Symbol *> ImportedSymbols;
121 unsigned NumImportedFunctions = 0;
122 unsigned NumImportedGlobals = 0;
Heejin Ahne915a712018-12-08 06:17:43 +0000123 unsigned NumImportedEvents = 0;
Sam Cleggd6beb322018-05-10 18:10:34 +0000124 std::vector<WasmExport> Exports;
Sam Clegg93102972018-02-23 05:08:53 +0000125 std::vector<const DefinedData *> DefinedFakeGlobals;
126 std::vector<InputGlobal *> InputGlobals;
Sam Clegg9f934222018-02-21 18:29:23 +0000127 std::vector<InputFunction *> InputFunctions;
Heejin Ahne915a712018-12-08 06:17:43 +0000128 std::vector<InputEvent *> InputEvents;
Sam Cleggdfb0b2c2018-02-14 18:27:59 +0000129 std::vector<const FunctionSymbol *> IndirectFunctions;
Sam Clegg93102972018-02-23 05:08:53 +0000130 std::vector<const Symbol *> SymtabEntries;
131 std::vector<WasmInitEntry> InitFunctions;
Sam Cleggc94d3932017-11-17 18:14:09 +0000132
Sam Clegg80ba4382018-04-10 16:12:49 +0000133 llvm::StringMap<std::vector<InputSection *>> CustomSectionMapping;
Sam Cleggd177ab22018-05-04 23:14:42 +0000134 llvm::StringMap<SectionSymbol *> CustomSectionSymbols;
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000135 llvm::SmallSet<std::string, 8> TargetFeatures;
Sam Clegg80ba4382018-04-10 16:12:49 +0000136
Sam Cleggc94d3932017-11-17 18:14:09 +0000137 // Elements that are used to construct the final output
138 std::string Header;
139 std::vector<OutputSection *> OutputSections;
140
141 std::unique_ptr<FileOutputBuffer> Buffer;
142
143 std::vector<OutputSegment *> Segments;
144 llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap;
145};
146
147} // anonymous namespace
148
Sam Cleggc94d3932017-11-17 18:14:09 +0000149void Writer::createImportSection() {
Sam Clegg93102972018-02-23 05:08:53 +0000150 uint32_t NumImports = ImportedSymbols.size();
Sam Cleggc94d3932017-11-17 18:14:09 +0000151 if (Config->ImportMemory)
152 ++NumImports;
Nicholas Wilsonfc90b302018-03-28 12:53:29 +0000153 if (Config->ImportTable)
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000154 ++NumImports;
Sam Cleggc94d3932017-11-17 18:14:09 +0000155
156 if (NumImports == 0)
157 return;
158
159 SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT);
160 raw_ostream &OS = Section->getStream();
161
162 writeUleb128(OS, NumImports, "import count");
163
Sam Cleggc94d3932017-11-17 18:14:09 +0000164 if (Config->ImportMemory) {
165 WasmImport Import;
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000166 Import.Module = DefaultModule;
Sam Cleggc94d3932017-11-17 18:14:09 +0000167 Import.Field = "memory";
168 Import.Kind = WASM_EXTERNAL_MEMORY;
169 Import.Memory.Flags = 0;
170 Import.Memory.Initial = NumMemoryPages;
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000171 if (MaxMemoryPages != 0) {
172 Import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX;
173 Import.Memory.Maximum = MaxMemoryPages;
174 }
Derek Schuff786760a2018-11-06 18:02:39 +0000175 if (Config->SharedMemory)
Derek Schuff3bea8bc2018-11-06 17:59:32 +0000176 Import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED;
Sam Cleggc94d3932017-11-17 18:14:09 +0000177 writeImport(OS, Import);
178 }
179
Nicholas Wilsonfc90b302018-03-28 12:53:29 +0000180 if (Config->ImportTable) {
Sam Cleggbfb75342018-11-15 00:37:21 +0000181 uint32_t TableSize = TableBase + IndirectFunctions.size();
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000182 WasmImport Import;
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000183 Import.Module = DefaultModule;
184 Import.Field = FunctionTableName;
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000185 Import.Kind = WASM_EXTERNAL_TABLE;
Thomas Lively25ff8932019-01-08 06:25:55 +0000186 Import.Table.ElemType = WASM_TYPE_FUNCREF;
Sam Clegg748f59cae2018-12-03 22:37:55 +0000187 Import.Table.Limits = {0, TableSize, 0};
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000188 writeImport(OS, Import);
189 }
190
Sam Clegg93102972018-02-23 05:08:53 +0000191 for (const Symbol *Sym : ImportedSymbols) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000192 WasmImport Import;
Dan Gohman9b84eea2019-02-07 22:00:48 +0000193 if (auto *F = dyn_cast<UndefinedFunction>(Sym)) {
194 Import.Field = F->ImportName;
195 Import.Module = F->ImportModule;
196 } else if (auto *G = dyn_cast<UndefinedGlobal>(Sym)) {
197 Import.Field = G->ImportName;
198 Import.Module = G->ImportModule;
199 } else {
200 Import.Field = Sym->getName();
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000201 Import.Module = DefaultModule;
Dan Gohman9b84eea2019-02-07 22:00:48 +0000202 }
Sam Clegg7cc07532019-02-01 02:29:57 +0000203
Sam Clegg93102972018-02-23 05:08:53 +0000204 if (auto *FunctionSym = dyn_cast<FunctionSymbol>(Sym)) {
205 Import.Kind = WASM_EXTERNAL_FUNCTION;
Heejin Ahne915a712018-12-08 06:17:43 +0000206 Import.SigIndex = lookupType(*FunctionSym->Signature);
Sam Cleggd425d6b2019-03-12 21:53:23 +0000207 } else if (auto *DataSym = dyn_cast<UndefinedData>(Sym)) {
208 Import.Kind = WASM_EXTERNAL_GLOBAL;
209 Import.Global = {WASM_TYPE_I32, true};
Heejin Ahne915a712018-12-08 06:17:43 +0000210 } else if (auto *GlobalSym = dyn_cast<GlobalSymbol>(Sym)) {
Sam Clegg93102972018-02-23 05:08:53 +0000211 Import.Kind = WASM_EXTERNAL_GLOBAL;
212 Import.Global = *GlobalSym->getGlobalType();
Heejin Ahne915a712018-12-08 06:17:43 +0000213 } else {
214 auto *EventSym = cast<EventSymbol>(Sym);
215 Import.Kind = WASM_EXTERNAL_EVENT;
216 Import.Event.Attribute = EventSym->getEventType()->Attribute;
217 Import.Event.SigIndex = lookupType(*EventSym->Signature);
Sam Clegg93102972018-02-23 05:08:53 +0000218 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000219 writeImport(OS, Import);
220 }
221}
222
223void Writer::createTypeSection() {
224 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE);
225 raw_ostream &OS = Section->getStream();
226 writeUleb128(OS, Types.size(), "type count");
Sam Cleggd451da12017-12-19 19:56:27 +0000227 for (const WasmSignature *Sig : Types)
Sam Cleggc94d3932017-11-17 18:14:09 +0000228 writeSig(OS, *Sig);
Sam Cleggc94d3932017-11-17 18:14:09 +0000229}
230
231void Writer::createFunctionSection() {
Sam Clegg9f934222018-02-21 18:29:23 +0000232 if (InputFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000233 return;
234
235 SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION);
236 raw_ostream &OS = Section->getStream();
237
Sam Clegg9f934222018-02-21 18:29:23 +0000238 writeUleb128(OS, InputFunctions.size(), "function count");
239 for (const InputFunction *Func : InputFunctions)
Sam Cleggc375e4e2018-01-10 19:18:22 +0000240 writeUleb128(OS, lookupType(Func->Signature), "sig index");
Sam Cleggc94d3932017-11-17 18:14:09 +0000241}
242
243void Writer::createMemorySection() {
244 if (Config->ImportMemory)
245 return;
246
247 SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY);
248 raw_ostream &OS = Section->getStream();
249
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000250 bool HasMax = MaxMemoryPages != 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000251 writeUleb128(OS, 1, "memory count");
Derek Schuff786760a2018-11-06 18:02:39 +0000252 unsigned Flags = 0;
253 if (HasMax)
254 Flags |= WASM_LIMITS_FLAG_HAS_MAX;
Derek Schuff3bea8bc2018-11-06 17:59:32 +0000255 if (Config->SharedMemory)
256 Flags |= WASM_LIMITS_FLAG_IS_SHARED;
257 writeUleb128(OS, Flags, "memory limits flags");
Sam Cleggc94d3932017-11-17 18:14:09 +0000258 writeUleb128(OS, NumMemoryPages, "initial pages");
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000259 if (HasMax)
260 writeUleb128(OS, MaxMemoryPages, "max pages");
Sam Cleggc94d3932017-11-17 18:14:09 +0000261}
262
263void Writer::createGlobalSection() {
Sam Clegg93102972018-02-23 05:08:53 +0000264 unsigned NumGlobals = InputGlobals.size() + DefinedFakeGlobals.size();
265 if (NumGlobals == 0)
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000266 return;
267
Sam Cleggc94d3932017-11-17 18:14:09 +0000268 SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL);
269 raw_ostream &OS = Section->getStream();
270
Sam Clegg93102972018-02-23 05:08:53 +0000271 writeUleb128(OS, NumGlobals, "global count");
272 for (const InputGlobal *G : InputGlobals)
273 writeGlobal(OS, G->Global);
274 for (const DefinedData *Sym : DefinedFakeGlobals) {
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000275 WasmGlobal Global;
Sam Clegg93102972018-02-23 05:08:53 +0000276 Global.Type = {WASM_TYPE_I32, false};
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000277 Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
278 Global.InitExpr.Value.Int32 = Sym->getVirtualAddress();
Sam Cleggc94d3932017-11-17 18:14:09 +0000279 writeGlobal(OS, Global);
280 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000281}
282
Heejin Ahne915a712018-12-08 06:17:43 +0000283// The event section contains a list of declared wasm events associated with the
284// module. Currently the only supported event kind is exceptions. A single event
285// entry represents a single event with an event tag. All C++ exceptions are
286// represented by a single event. An event entry in this section contains
287// information on what kind of event it is (e.g. exception) and the type of
288// values contained in a single event object. (In wasm, an event can contain
289// multiple values of primitive types. But for C++ exceptions, we just throw a
290// pointer which is an i32 value (for wasm32 architecture), so the signature of
291// C++ exception is (i32)->(void), because all event types are assumed to have
292// void return type to share WasmSignature with functions.)
293void Writer::createEventSection() {
294 unsigned NumEvents = InputEvents.size();
295 if (NumEvents == 0)
296 return;
297
298 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EVENT);
299 raw_ostream &OS = Section->getStream();
300
301 writeUleb128(OS, NumEvents, "event count");
302 for (InputEvent *E : InputEvents) {
303 E->Event.Type.SigIndex = lookupType(E->Signature);
304 writeEvent(OS, E->Event);
305 }
306}
307
Sam Cleggc94d3932017-11-17 18:14:09 +0000308void Writer::createTableSection() {
Nicholas Wilsonfc90b302018-03-28 12:53:29 +0000309 if (Config->ImportTable)
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000310 return;
311
312 // Always output a table section (or table import), even if there are no
313 // indirect calls. There are two reasons for this:
Sam Cleggfc1a9122017-12-11 22:00:56 +0000314 // 1. For executables it is useful to have an empty table slot at 0
315 // which can be filled with a null function call handler.
316 // 2. If we don't do this, any program that contains a call_indirect but
317 // no address-taken function will fail at validation time since it is
318 // a validation error to include a call_indirect instruction if there
319 // is not table.
Sam Cleggbfb75342018-11-15 00:37:21 +0000320 uint32_t TableSize = TableBase + IndirectFunctions.size();
Sam Cleggfc1a9122017-12-11 22:00:56 +0000321
Sam Cleggc94d3932017-11-17 18:14:09 +0000322 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE);
323 raw_ostream &OS = Section->getStream();
324
325 writeUleb128(OS, 1, "table count");
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000326 WasmLimits Limits = {WASM_LIMITS_FLAG_HAS_MAX, TableSize, TableSize};
Thomas Lively25ff8932019-01-08 06:25:55 +0000327 writeTableType(OS, WasmTable{WASM_TYPE_FUNCREF, Limits});
Sam Cleggc94d3932017-11-17 18:14:09 +0000328}
329
330void Writer::createExportSection() {
Sam Cleggd6beb322018-05-10 18:10:34 +0000331 if (!Exports.size())
Sam Cleggc94d3932017-11-17 18:14:09 +0000332 return;
333
334 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT);
335 raw_ostream &OS = Section->getStream();
336
Sam Cleggd6beb322018-05-10 18:10:34 +0000337 writeUleb128(OS, Exports.size(), "export count");
338 for (const WasmExport &Export : Exports)
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000339 writeExport(OS, Export);
Sam Cleggc94d3932017-11-17 18:14:09 +0000340}
341
Sam Cleggd177ab22018-05-04 23:14:42 +0000342void Writer::calculateCustomSections() {
343 log("calculateCustomSections");
344 bool StripDebug = Config->StripDebug || Config->StripAll;
345 for (ObjFile *File : Symtab->ObjectFiles) {
346 for (InputSection *Section : File->CustomSections) {
347 StringRef Name = Section->getName();
348 // These custom sections are known the linker and synthesized rather than
349 // blindly copied
Thomas Lively2a0868f2019-01-17 02:29:41 +0000350 if (Name == "linking" || Name == "name" || Name == "producers" ||
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000351 Name == "target_features" || Name.startswith("reloc."))
Sam Cleggd177ab22018-05-04 23:14:42 +0000352 continue;
353 // .. or it is a debug section
354 if (StripDebug && Name.startswith(".debug_"))
355 continue;
356 CustomSectionMapping[Name].push_back(Section);
357 }
358 }
359}
360
Sam Clegg80ba4382018-04-10 16:12:49 +0000361void Writer::createCustomSections() {
362 log("createCustomSections");
Sam Clegg80ba4382018-04-10 16:12:49 +0000363 for (auto &Pair : CustomSectionMapping) {
364 StringRef Name = Pair.first();
Sam Cleggd177ab22018-05-04 23:14:42 +0000365
366 auto P = CustomSectionSymbols.find(Name);
367 if (P != CustomSectionSymbols.end()) {
368 uint32_t SectionIndex = OutputSections.size();
369 P->second->setOutputSectionIndex(SectionIndex);
370 }
371
Nicola Zaghene7245b42018-05-15 13:36:20 +0000372 LLVM_DEBUG(dbgs() << "createCustomSection: " << Name << "\n");
Sam Clegg80ba4382018-04-10 16:12:49 +0000373 OutputSections.push_back(make<CustomSection>(Name, Pair.second));
374 }
375}
376
Sam Cleggc94d3932017-11-17 18:14:09 +0000377void Writer::createElemSection() {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000378 if (IndirectFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000379 return;
380
381 SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM);
382 raw_ostream &OS = Section->getStream();
383
384 writeUleb128(OS, 1, "segment count");
385 writeUleb128(OS, 0, "table index");
386 WasmInitExpr InitExpr;
Sam Cleggbfb75342018-11-15 00:37:21 +0000387 if (Config->Pic) {
Thomas Lively25ff8932019-01-08 06:25:55 +0000388 InitExpr.Opcode = WASM_OPCODE_GLOBAL_GET;
Sam Clegg2dad4e22018-11-15 18:15:54 +0000389 InitExpr.Value.Global = WasmSym::TableBase->getGlobalIndex();
Sam Cleggbfb75342018-11-15 00:37:21 +0000390 } else {
391 InitExpr.Opcode = WASM_OPCODE_I32_CONST;
392 InitExpr.Value.Int32 = TableBase;
393 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000394 writeInitExpr(OS, InitExpr);
Sam Cleggfc1a9122017-12-11 22:00:56 +0000395 writeUleb128(OS, IndirectFunctions.size(), "elem count");
Sam Cleggc94d3932017-11-17 18:14:09 +0000396
Sam Cleggbfb75342018-11-15 00:37:21 +0000397 uint32_t TableIndex = TableBase;
Sam Cleggdfb0b2c2018-02-14 18:27:59 +0000398 for (const FunctionSymbol *Sym : IndirectFunctions) {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000399 assert(Sym->getTableIndex() == TableIndex);
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000400 writeUleb128(OS, Sym->getFunctionIndex(), "function index");
Sam Cleggfc1a9122017-12-11 22:00:56 +0000401 ++TableIndex;
402 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000403}
404
405void Writer::createCodeSection() {
Sam Clegg9f934222018-02-21 18:29:23 +0000406 if (InputFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000407 return;
408
409 log("createCodeSection");
410
Sam Clegg9f934222018-02-21 18:29:23 +0000411 auto Section = make<CodeSection>(InputFunctions);
Sam Cleggc94d3932017-11-17 18:14:09 +0000412 OutputSections.push_back(Section);
413}
414
415void Writer::createDataSection() {
416 if (!Segments.size())
417 return;
418
419 log("createDataSection");
420 auto Section = make<DataSection>(Segments);
421 OutputSections.push_back(Section);
422}
423
Sam Cleggd451da12017-12-19 19:56:27 +0000424// Create relocations sections in the final output.
Sam Cleggc94d3932017-11-17 18:14:09 +0000425// These are only created when relocatable output is requested.
426void Writer::createRelocSections() {
427 log("createRelocSections");
428 // Don't use iterator here since we are adding to OutputSection
429 size_t OrigSize = OutputSections.size();
Rui Ueyamaffa650a2018-04-24 23:09:57 +0000430 for (size_t I = 0; I < OrigSize; I++) {
431 OutputSection *OSec = OutputSections[I];
Rui Ueyama37254062018-02-28 00:01:31 +0000432 uint32_t Count = OSec->numRelocations();
Sam Cleggc94d3932017-11-17 18:14:09 +0000433 if (!Count)
434 continue;
435
Rui Ueyama37254062018-02-28 00:01:31 +0000436 StringRef Name;
437 if (OSec->Type == WASM_SEC_DATA)
438 Name = "reloc.DATA";
439 else if (OSec->Type == WASM_SEC_CODE)
440 Name = "reloc.CODE";
Sam Cleggd177ab22018-05-04 23:14:42 +0000441 else if (OSec->Type == WASM_SEC_CUSTOM)
442 Name = Saver.save("reloc." + OSec->Name);
Sam Cleggc94d3932017-11-17 18:14:09 +0000443 else
Sam Cleggd177ab22018-05-04 23:14:42 +0000444 llvm_unreachable(
445 "relocations only supported for code, data, or custom sections");
Sam Cleggc94d3932017-11-17 18:14:09 +0000446
Rui Ueyama37254062018-02-28 00:01:31 +0000447 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, Name);
Sam Cleggc94d3932017-11-17 18:14:09 +0000448 raw_ostream &OS = Section->getStream();
Rui Ueyamaffa650a2018-04-24 23:09:57 +0000449 writeUleb128(OS, I, "reloc section");
Sam Cleggc94d3932017-11-17 18:14:09 +0000450 writeUleb128(OS, Count, "reloc count");
Rui Ueyama37254062018-02-28 00:01:31 +0000451 OSec->writeRelocations(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000452 }
453}
454
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000455static uint32_t getWasmFlags(const Symbol *Sym) {
456 uint32_t Flags = 0;
457 if (Sym->isLocal())
458 Flags |= WASM_SYMBOL_BINDING_LOCAL;
459 if (Sym->isWeak())
460 Flags |= WASM_SYMBOL_BINDING_WEAK;
461 if (Sym->isHidden())
462 Flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
463 if (Sym->isUndefined())
464 Flags |= WASM_SYMBOL_UNDEFINED;
Dan Gohman9b84eea2019-02-07 22:00:48 +0000465 if (auto *F = dyn_cast<UndefinedFunction>(Sym)) {
466 if (F->getName() != F->ImportName)
467 Flags |= WASM_SYMBOL_EXPLICIT_NAME;
468 } else if (auto *G = dyn_cast<UndefinedGlobal>(Sym)) {
469 if (G->getName() != G->ImportName)
470 Flags |= WASM_SYMBOL_EXPLICIT_NAME;
471 }
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000472 return Flags;
473}
474
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000475// Some synthetic sections (e.g. "name" and "linking") have subsections.
476// Just like the synthetic sections themselves these need to be created before
477// they can be written out (since they are preceded by their length). This
478// class is used to create subsections and then write them into the stream
479// of the parent section.
480class SubSection {
481public:
482 explicit SubSection(uint32_t Type) : Type(Type) {}
483
484 void writeTo(raw_ostream &To) {
485 OS.flush();
Rui Ueyama67769102018-02-28 03:38:14 +0000486 writeUleb128(To, Type, "subsection type");
487 writeUleb128(To, Body.size(), "subsection size");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000488 To.write(Body.data(), Body.size());
489 }
490
491private:
492 uint32_t Type;
493 std::string Body;
494
495public:
496 raw_string_ostream OS{Body};
497};
498
Sam Cleggbfb75342018-11-15 00:37:21 +0000499// Create the custom "dylink" section containing information for the dynamic
500// linker.
501// See
502// https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md
503void Writer::createDylinkSection() {
504 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "dylink");
505 raw_ostream &OS = Section->getStream();
506
507 writeUleb128(OS, MemSize, "MemSize");
Sam Clegg6320efb2019-01-16 01:43:21 +0000508 writeUleb128(OS, MemAlign, "MemAlign");
Sam Cleggbfb75342018-11-15 00:37:21 +0000509 writeUleb128(OS, IndirectFunctions.size(), "TableSize");
510 writeUleb128(OS, 0, "TableAlign");
Sam Clegga688a422019-03-13 21:29:20 +0000511 writeUleb128(OS, Symtab->SharedFiles.size(), "Needed");
512 for (auto *SO : Symtab->SharedFiles)
513 writeStr(OS, llvm::sys::path::filename(SO->getName()), "so name");
Sam Cleggbfb75342018-11-15 00:37:21 +0000514}
515
Sam Clegg49ed9262017-12-01 00:53:21 +0000516// Create the custom "linking" section containing linker metadata.
Sam Cleggc94d3932017-11-17 18:14:09 +0000517// This is only created when relocatable output is requested.
518void Writer::createLinkingSection() {
519 SyntheticSection *Section =
520 createSyntheticSection(WASM_SEC_CUSTOM, "linking");
521 raw_ostream &OS = Section->getStream();
522
Sam Clegg2b8b1792018-04-26 18:17:21 +0000523 writeUleb128(OS, WasmMetadataVersion, "Version");
Sam Clegg0d0dd392017-12-19 17:09:45 +0000524
Sam Clegg93102972018-02-23 05:08:53 +0000525 if (!SymtabEntries.empty()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000526 SubSection Sub(WASM_SYMBOL_TABLE);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000527 writeUleb128(Sub.OS, SymtabEntries.size(), "num symbols");
528
Sam Clegg93102972018-02-23 05:08:53 +0000529 for (const Symbol *Sym : SymtabEntries) {
530 assert(Sym->isDefined() || Sym->isUndefined());
531 WasmSymbolType Kind = Sym->getWasmType();
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000532 uint32_t Flags = getWasmFlags(Sym);
533
Sam Clegg8518e7d2018-03-01 18:06:39 +0000534 writeU8(Sub.OS, Kind, "sym kind");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000535 writeUleb128(Sub.OS, Flags, "sym flags");
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000536
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000537 if (auto *F = dyn_cast<FunctionSymbol>(Sym)) {
538 writeUleb128(Sub.OS, F->getFunctionIndex(), "index");
Dan Gohman9b84eea2019-02-07 22:00:48 +0000539 if (Sym->isDefined() ||
540 (Flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000541 writeStr(Sub.OS, Sym->getName(), "sym name");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000542 } else if (auto *G = dyn_cast<GlobalSymbol>(Sym)) {
543 writeUleb128(Sub.OS, G->getGlobalIndex(), "index");
Dan Gohman9b84eea2019-02-07 22:00:48 +0000544 if (Sym->isDefined() ||
545 (Flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000546 writeStr(Sub.OS, Sym->getName(), "sym name");
Heejin Ahne915a712018-12-08 06:17:43 +0000547 } else if (auto *E = dyn_cast<EventSymbol>(Sym)) {
548 writeUleb128(Sub.OS, E->getEventIndex(), "index");
Dan Gohman9b84eea2019-02-07 22:00:48 +0000549 if (Sym->isDefined() ||
550 (Flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
Heejin Ahne915a712018-12-08 06:17:43 +0000551 writeStr(Sub.OS, Sym->getName(), "sym name");
Andrea Di Biagio25c1a2f2018-05-05 10:53:31 +0000552 } else if (isa<DataSymbol>(Sym)) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000553 writeStr(Sub.OS, Sym->getName(), "sym name");
Sam Clegg93102972018-02-23 05:08:53 +0000554 if (auto *DataSym = dyn_cast<DefinedData>(Sym)) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000555 writeUleb128(Sub.OS, DataSym->getOutputSegmentIndex(), "index");
556 writeUleb128(Sub.OS, DataSym->getOutputSegmentOffset(),
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000557 "data offset");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000558 writeUleb128(Sub.OS, DataSym->getSize(), "data size");
Sam Clegg93102972018-02-23 05:08:53 +0000559 }
Sam Cleggd177ab22018-05-04 23:14:42 +0000560 } else {
561 auto *S = cast<SectionSymbol>(Sym);
562 writeUleb128(Sub.OS, S->getOutputSectionIndex(), "sym section index");
Sam Clegg93102972018-02-23 05:08:53 +0000563 }
Sam Cleggd3052d52018-01-18 23:40:49 +0000564 }
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000565
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000566 Sub.writeTo(OS);
Sam Cleggd3052d52018-01-18 23:40:49 +0000567 }
568
Sam Clegg0d0dd392017-12-19 17:09:45 +0000569 if (Segments.size()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000570 SubSection Sub(WASM_SEGMENT_INFO);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000571 writeUleb128(Sub.OS, Segments.size(), "num data segments");
Sam Cleggc94d3932017-11-17 18:14:09 +0000572 for (const OutputSegment *S : Segments) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000573 writeStr(Sub.OS, S->Name, "segment name");
574 writeUleb128(Sub.OS, S->Alignment, "alignment");
575 writeUleb128(Sub.OS, 0, "flags");
Sam Cleggc94d3932017-11-17 18:14:09 +0000576 }
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000577 Sub.writeTo(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000578 }
Sam Clegg0d0dd392017-12-19 17:09:45 +0000579
Sam Clegg0d0dd392017-12-19 17:09:45 +0000580 if (!InitFunctions.empty()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000581 SubSection Sub(WASM_INIT_FUNCS);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000582 writeUleb128(Sub.OS, InitFunctions.size(), "num init functions");
Sam Clegg93102972018-02-23 05:08:53 +0000583 for (const WasmInitEntry &F : InitFunctions) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000584 writeUleb128(Sub.OS, F.Priority, "priority");
585 writeUleb128(Sub.OS, F.Sym->getOutputSymbolIndex(), "function index");
Sam Clegg0d0dd392017-12-19 17:09:45 +0000586 }
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000587 Sub.writeTo(OS);
Sam Clegg0d0dd392017-12-19 17:09:45 +0000588 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000589
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000590 struct ComdatEntry {
591 unsigned Kind;
592 uint32_t Index;
593 };
594 std::map<StringRef, std::vector<ComdatEntry>> Comdats;
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000595
Sam Clegg9f934222018-02-21 18:29:23 +0000596 for (const InputFunction *F : InputFunctions) {
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000597 StringRef Comdat = F->getComdatName();
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000598 if (!Comdat.empty())
599 Comdats[Comdat].emplace_back(
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000600 ComdatEntry{WASM_COMDAT_FUNCTION, F->getFunctionIndex()});
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000601 }
602 for (uint32_t I = 0; I < Segments.size(); ++I) {
Sam Cleggf98bccf2018-01-13 15:57:48 +0000603 const auto &InputSegments = Segments[I]->InputSegments;
604 if (InputSegments.empty())
605 continue;
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000606 StringRef Comdat = InputSegments[0]->getComdatName();
Sam Clegga697df522018-01-13 15:59:53 +0000607#ifndef NDEBUG
Sam Cleggf98bccf2018-01-13 15:57:48 +0000608 for (const InputSegment *IS : InputSegments)
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000609 assert(IS->getComdatName() == Comdat);
Sam Clegga697df522018-01-13 15:59:53 +0000610#endif
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000611 if (!Comdat.empty())
612 Comdats[Comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, I});
613 }
614
615 if (!Comdats.empty()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000616 SubSection Sub(WASM_COMDAT_INFO);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000617 writeUleb128(Sub.OS, Comdats.size(), "num comdats");
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000618 for (const auto &C : Comdats) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000619 writeStr(Sub.OS, C.first, "comdat name");
620 writeUleb128(Sub.OS, 0, "comdat flags"); // flags for future use
621 writeUleb128(Sub.OS, C.second.size(), "num entries");
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000622 for (const ComdatEntry &Entry : C.second) {
Sam Clegg8518e7d2018-03-01 18:06:39 +0000623 writeU8(Sub.OS, Entry.Kind, "entry kind");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000624 writeUleb128(Sub.OS, Entry.Index, "entry index");
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000625 }
626 }
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000627 Sub.writeTo(OS);
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000628 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000629}
630
631// Create the custom "name" section containing debug symbol names.
632void Writer::createNameSection() {
Sam Clegg93102972018-02-23 05:08:53 +0000633 unsigned NumNames = NumImportedFunctions;
Sam Clegg9f934222018-02-21 18:29:23 +0000634 for (const InputFunction *F : InputFunctions)
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000635 if (!F->getName().empty() || !F->getDebugName().empty())
Sam Clegg1963d712018-01-17 20:19:04 +0000636 ++NumNames;
Sam Cleggc94d3932017-11-17 18:14:09 +0000637
Sam Clegg1963d712018-01-17 20:19:04 +0000638 if (NumNames == 0)
639 return;
Sam Clegg50686852018-01-12 18:35:13 +0000640
Sam Cleggc94d3932017-11-17 18:14:09 +0000641 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name");
642
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000643 SubSection Sub(WASM_NAMES_FUNCTION);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000644 writeUleb128(Sub.OS, NumNames, "name count");
Sam Cleggc94d3932017-11-17 18:14:09 +0000645
Sam Clegg93102972018-02-23 05:08:53 +0000646 // Names must appear in function index order. As it happens ImportedSymbols
647 // and InputFunctions are numbered in order with imported functions coming
Sam Clegg1963d712018-01-17 20:19:04 +0000648 // first.
Sam Clegg93102972018-02-23 05:08:53 +0000649 for (const Symbol *S : ImportedSymbols) {
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000650 if (auto *F = dyn_cast<FunctionSymbol>(S)) {
651 writeUleb128(Sub.OS, F->getFunctionIndex(), "func index");
Sam Clegg37125f02018-11-09 16:57:41 +0000652 writeStr(Sub.OS, toString(*S), "symbol name");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000653 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000654 }
Sam Clegg9f934222018-02-21 18:29:23 +0000655 for (const InputFunction *F : InputFunctions) {
Sam Clegg1963d712018-01-17 20:19:04 +0000656 if (!F->getName().empty()) {
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000657 writeUleb128(Sub.OS, F->getFunctionIndex(), "func index");
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000658 if (!F->getDebugName().empty()) {
659 writeStr(Sub.OS, F->getDebugName(), "symbol name");
660 } else {
Sam Clegg37125f02018-11-09 16:57:41 +0000661 writeStr(Sub.OS, maybeDemangleSymbol(F->getName()), "symbol name");
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000662 }
Sam Clegg1963d712018-01-17 20:19:04 +0000663 }
664 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000665
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000666 Sub.writeTo(Section->getStream());
Sam Cleggc94d3932017-11-17 18:14:09 +0000667}
668
Thomas Lively2a0868f2019-01-17 02:29:41 +0000669void Writer::createProducersSection() {
670 SmallVector<std::pair<std::string, std::string>, 8> Languages;
671 SmallVector<std::pair<std::string, std::string>, 8> Tools;
672 SmallVector<std::pair<std::string, std::string>, 8> SDKs;
673 for (ObjFile *File : Symtab->ObjectFiles) {
674 const WasmProducerInfo &Info = File->getWasmObj()->getProducerInfo();
675 for (auto &Producers : {std::make_pair(&Info.Languages, &Languages),
676 std::make_pair(&Info.Tools, &Tools),
677 std::make_pair(&Info.SDKs, &SDKs)})
678 for (auto &Producer : *Producers.first)
679 if (Producers.second->end() ==
680 std::find_if(Producers.second->begin(), Producers.second->end(),
681 [&](std::pair<std::string, std::string> Seen) {
682 return Seen.first == Producer.first;
683 }))
684 Producers.second->push_back(Producer);
685 }
686 int FieldCount =
687 int(!Languages.empty()) + int(!Tools.empty()) + int(!SDKs.empty());
688 if (FieldCount == 0)
689 return;
690 SyntheticSection *Section =
691 createSyntheticSection(WASM_SEC_CUSTOM, "producers");
692 auto &OS = Section->getStream();
693 writeUleb128(OS, FieldCount, "field count");
694 for (auto &Field :
695 {std::make_pair("language", Languages),
696 std::make_pair("processed-by", Tools), std::make_pair("sdk", SDKs)}) {
697 if (Field.second.empty())
698 continue;
699 writeStr(OS, Field.first, "field name");
700 writeUleb128(OS, Field.second.size(), "number of entries");
701 for (auto &Entry : Field.second) {
702 writeStr(OS, Entry.first, "producer name");
703 writeStr(OS, Entry.second, "producer version");
704 }
705 }
706}
707
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000708void Writer::createTargetFeaturesSection() {
709 if (TargetFeatures.size() == 0)
710 return;
711
712 SmallVector<std::string, 8> Emitted(TargetFeatures.begin(),
713 TargetFeatures.end());
714 std::sort(Emitted.begin(), Emitted.end());
715 SyntheticSection *Section =
716 createSyntheticSection(WASM_SEC_CUSTOM, "target_features");
717 auto &OS = Section->getStream();
718 writeUleb128(OS, Emitted.size(), "feature count");
719 for (auto &Feature : Emitted) {
720 writeU8(OS, WASM_FEATURE_PREFIX_USED, "feature used prefix");
721 writeStr(OS, Feature, "feature name");
722 }
723}
724
Sam Cleggc94d3932017-11-17 18:14:09 +0000725void Writer::writeHeader() {
726 memcpy(Buffer->getBufferStart(), Header.data(), Header.size());
727}
728
729void Writer::writeSections() {
730 uint8_t *Buf = Buffer->getBufferStart();
731 parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); });
732}
733
734// Fix the memory layout of the output binary. This assigns memory offsets
Sam Clegg49ed9262017-12-01 00:53:21 +0000735// to each of the input data sections as well as the explicit stack region.
Sam Clegga0f095e2018-05-03 17:21:53 +0000736// The default memory layout is as follows, from low to high.
737//
Sam Cleggf0d433d2018-02-02 22:59:56 +0000738// - initialized data (starting at Config->GlobalBase)
739// - BSS data (not currently implemented in llvm)
740// - explicit stack (Config->ZStackSize)
741// - heap start / unallocated
Sam Clegga0f095e2018-05-03 17:21:53 +0000742//
743// The --stack-first option means that stack is placed before any static data.
Heejin Ahn4821ebf2018-08-29 21:03:16 +0000744// This can be useful since it means that stack overflow traps immediately
745// rather than overwriting global data, but also increases code size since all
746// static data loads and stores requires larger offsets.
Sam Cleggc94d3932017-11-17 18:14:09 +0000747void Writer::layoutMemory() {
Sam Cleggc94d3932017-11-17 18:14:09 +0000748 createOutputSegments();
749
Sam Clegga0f095e2018-05-03 17:21:53 +0000750 uint32_t MemoryPtr = 0;
751
752 auto PlaceStack = [&]() {
Sam Cleggbfb75342018-11-15 00:37:21 +0000753 if (Config->Relocatable || Config->Shared)
Sam Clegga0f095e2018-05-03 17:21:53 +0000754 return;
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000755 MemoryPtr = alignTo(MemoryPtr, StackAlignment);
756 if (Config->ZStackSize != alignTo(Config->ZStackSize, StackAlignment))
757 error("stack size must be " + Twine(StackAlignment) + "-byte aligned");
Sam Clegga0f095e2018-05-03 17:21:53 +0000758 log("mem: stack size = " + Twine(Config->ZStackSize));
759 log("mem: stack base = " + Twine(MemoryPtr));
760 MemoryPtr += Config->ZStackSize;
Sam Clegg2dad4e22018-11-15 18:15:54 +0000761 auto *SP = cast<DefinedGlobal>(WasmSym::StackPointer);
762 SP->Global->Global.InitExpr.Value.Int32 = MemoryPtr;
Sam Clegga0f095e2018-05-03 17:21:53 +0000763 log("mem: stack top = " + Twine(MemoryPtr));
764 };
765
766 if (Config->StackFirst) {
767 PlaceStack();
768 } else {
769 MemoryPtr = Config->GlobalBase;
770 log("mem: global base = " + Twine(Config->GlobalBase));
771 }
772
773 uint32_t DataStart = MemoryPtr;
774
Sam Cleggf0d433d2018-02-02 22:59:56 +0000775 // Arbitrarily set __dso_handle handle to point to the start of the data
776 // segments.
777 if (WasmSym::DsoHandle)
Sam Clegga0f095e2018-05-03 17:21:53 +0000778 WasmSym::DsoHandle->setVirtualAddress(DataStart);
Sam Cleggf0d433d2018-02-02 22:59:56 +0000779
Sam Cleggbfb75342018-11-15 00:37:21 +0000780 MemAlign = 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000781 for (OutputSegment *Seg : Segments) {
Sam Cleggbfb75342018-11-15 00:37:21 +0000782 MemAlign = std::max(MemAlign, Seg->Alignment);
Sam Clegg622ad042019-01-17 22:09:09 +0000783 MemoryPtr = alignTo(MemoryPtr, 1ULL << Seg->Alignment);
Sam Cleggc94d3932017-11-17 18:14:09 +0000784 Seg->StartVA = MemoryPtr;
Nicholas Wilsona06a3552018-03-14 13:50:20 +0000785 log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", Seg->Name,
786 MemoryPtr, Seg->Size, Seg->Alignment));
Sam Cleggc94d3932017-11-17 18:14:09 +0000787 MemoryPtr += Seg->Size;
788 }
789
Sam Cleggf0d433d2018-02-02 22:59:56 +0000790 // TODO: Add .bss space here.
Sam Clegg37a4a8a2018-02-07 03:04:53 +0000791 if (WasmSym::DataEnd)
792 WasmSym::DataEnd->setVirtualAddress(MemoryPtr);
Sam Cleggf0d433d2018-02-02 22:59:56 +0000793
Sam Clegga0f095e2018-05-03 17:21:53 +0000794 log("mem: static data = " + Twine(MemoryPtr - DataStart));
Sam Cleggc94d3932017-11-17 18:14:09 +0000795
Sam Clegg2dad4e22018-11-15 18:15:54 +0000796 if (Config->Shared) {
797 MemSize = MemoryPtr;
798 return;
799 }
800
Sam Clegga0f095e2018-05-03 17:21:53 +0000801 if (!Config->StackFirst)
802 PlaceStack();
803
804 // Set `__heap_base` to directly follow the end of the stack or global data.
805 // The fact that this comes last means that a malloc/brk implementation
806 // can grow the heap at runtime.
Sam Cleggc94d3932017-11-17 18:14:09 +0000807 if (!Config->Relocatable) {
Sam Cleggf0d433d2018-02-02 22:59:56 +0000808 WasmSym::HeapBase->setVirtualAddress(MemoryPtr);
Nicholas Wilsona06a3552018-03-14 13:50:20 +0000809 log("mem: heap base = " + Twine(MemoryPtr));
Sam Cleggc94d3932017-11-17 18:14:09 +0000810 }
811
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000812 if (Config->InitialMemory != 0) {
813 if (Config->InitialMemory != alignTo(Config->InitialMemory, WasmPageSize))
814 error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned");
815 if (MemoryPtr > Config->InitialMemory)
816 error("initial memory too small, " + Twine(MemoryPtr) + " bytes needed");
817 else
818 MemoryPtr = Config->InitialMemory;
819 }
Sam Cleggbfb75342018-11-15 00:37:21 +0000820 MemSize = MemoryPtr;
821 NumMemoryPages = alignTo(MemoryPtr, WasmPageSize) / WasmPageSize;
Nicholas Wilsona06a3552018-03-14 13:50:20 +0000822 log("mem: total pages = " + Twine(NumMemoryPages));
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000823
824 if (Config->MaxMemory != 0) {
825 if (Config->MaxMemory != alignTo(Config->MaxMemory, WasmPageSize))
826 error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned");
827 if (MemoryPtr > Config->MaxMemory)
828 error("maximum memory too small, " + Twine(MemoryPtr) + " bytes needed");
829 MaxMemoryPages = Config->MaxMemory / WasmPageSize;
830 log("mem: max pages = " + Twine(MaxMemoryPages));
831 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000832}
833
834SyntheticSection *Writer::createSyntheticSection(uint32_t Type,
Sam Cleggc375e4e2018-01-10 19:18:22 +0000835 StringRef Name) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000836 auto Sec = make<SyntheticSection>(Type, Name);
Sam Cleggab2ac292017-12-20 05:14:48 +0000837 log("createSection: " + toString(*Sec));
Sam Cleggc94d3932017-11-17 18:14:09 +0000838 OutputSections.push_back(Sec);
839 return Sec;
840}
841
842void Writer::createSections() {
843 // Known sections
Sam Cleggbfb75342018-11-15 00:37:21 +0000844 if (Config->Pic)
845 createDylinkSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000846 createTypeSection();
847 createImportSection();
848 createFunctionSection();
849 createTableSection();
850 createMemorySection();
851 createGlobalSection();
Heejin Ahne915a712018-12-08 06:17:43 +0000852 createEventSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000853 createExportSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000854 createElemSection();
855 createCodeSection();
856 createDataSection();
Sam Clegg80ba4382018-04-10 16:12:49 +0000857 createCustomSections();
Sam Cleggc94d3932017-11-17 18:14:09 +0000858
859 // Custom sections
Sam Clegg99eb42c2018-02-27 23:58:03 +0000860 if (Config->Relocatable) {
Sam Clegg99eb42c2018-02-27 23:58:03 +0000861 createLinkingSection();
Nicholas Wilson94d3b162018-03-05 12:33:58 +0000862 createRelocSections();
Sam Clegg99eb42c2018-02-27 23:58:03 +0000863 }
Thomas Lively2a0868f2019-01-17 02:29:41 +0000864
Sam Cleggc94d3932017-11-17 18:14:09 +0000865 if (!Config->StripDebug && !Config->StripAll)
866 createNameSection();
867
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000868 if (!Config->StripAll) {
Thomas Lively2a0868f2019-01-17 02:29:41 +0000869 createProducersSection();
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000870 createTargetFeaturesSection();
871 }
Thomas Lively2a0868f2019-01-17 02:29:41 +0000872
Sam Cleggc94d3932017-11-17 18:14:09 +0000873 for (OutputSection *S : OutputSections) {
874 S->setOffset(FileSize);
875 S->finalizeContents();
876 FileSize += S->getSize();
877 }
878}
879
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000880void Writer::calculateTargetFeatures() {
Thomas Lively82de51a2019-03-26 04:11:05 +0000881 SmallSet<std::string, 8> Used;
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000882 SmallSet<std::string, 8> Required;
883 SmallSet<std::string, 8> Disallowed;
884
Thomas Lively82de51a2019-03-26 04:11:05 +0000885 // Only infer used features if user did not specify features
886 bool InferFeatures = !Config->Features.hasValue();
887
888 if (!InferFeatures) {
889 for (auto &Feature : Config->Features.getValue())
890 TargetFeatures.insert(Feature);
891 // No need to read or check features
892 if (!Config->CheckFeatures)
893 return;
894 }
895
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000896 // Find the sets of used, required, and disallowed features
897 for (ObjFile *File : Symtab->ObjectFiles) {
898 for (auto &Feature : File->getWasmObj()->getTargetFeatures()) {
899 switch (Feature.Prefix) {
900 case WASM_FEATURE_PREFIX_USED:
Thomas Lively82de51a2019-03-26 04:11:05 +0000901 Used.insert(Feature.Name);
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000902 break;
903 case WASM_FEATURE_PREFIX_REQUIRED:
Thomas Lively82de51a2019-03-26 04:11:05 +0000904 Used.insert(Feature.Name);
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000905 Required.insert(Feature.Name);
906 break;
907 case WASM_FEATURE_PREFIX_DISALLOWED:
908 Disallowed.insert(Feature.Name);
909 break;
910 default:
911 error("Unrecognized feature policy prefix " +
912 std::to_string(Feature.Prefix));
913 }
914 }
915 }
916
Thomas Lively82de51a2019-03-26 04:11:05 +0000917 if (InferFeatures)
918 TargetFeatures.insert(Used.begin(), Used.end());
919
920 if (!Config->CheckFeatures)
921 return;
922
923 // Validate that used features are allowed in output
924 if (!InferFeatures) {
925 for (auto &Feature : Used) {
926 if (!TargetFeatures.count(Feature))
927 error(Twine("Target feature '") + Feature + "' is not allowed.");
928 }
929 }
930
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000931 // Validate the required and disallowed constraints for each file
932 for (ObjFile *File : Symtab->ObjectFiles) {
933 SmallSet<std::string, 8> ObjectFeatures;
934 for (auto &Feature : File->getWasmObj()->getTargetFeatures()) {
935 if (Feature.Prefix == WASM_FEATURE_PREFIX_DISALLOWED)
936 continue;
937 ObjectFeatures.insert(Feature.Name);
938 if (Disallowed.count(Feature.Name))
Thomas Lively82de51a2019-03-26 04:11:05 +0000939 error(Twine("Target feature '") + Feature.Name +
940 "' is disallowed. Use --no-check-features to suppress.");
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000941 }
942 for (auto &Feature : Required) {
943 if (!ObjectFeatures.count(Feature))
Thomas Lively82de51a2019-03-26 04:11:05 +0000944 error(Twine("Missing required target feature '") + Feature +
945 "'. Use --no-check-features to suppress.");
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000946 }
947 }
948}
949
Sam Cleggc94d3932017-11-17 18:14:09 +0000950void Writer::calculateImports() {
Sam Clegg574d7ce2017-12-15 19:23:49 +0000951 for (Symbol *Sym : Symtab->getSymbols()) {
Sam Clegg93102972018-02-23 05:08:53 +0000952 if (!Sym->isUndefined())
953 continue;
Sam Clegg93102972018-02-23 05:08:53 +0000954 if (Sym->isWeak() && !Config->Relocatable)
Sam Clegg574d7ce2017-12-15 19:23:49 +0000955 continue;
Nicholas Wilsona1e299f2018-04-20 17:18:06 +0000956 if (!Sym->isLive())
957 continue;
Sam Cleggc729c1b2018-05-30 18:07:52 +0000958 if (!Sym->IsUsedInRegularObj)
959 continue;
Sam Cleggd425d6b2019-03-12 21:53:23 +0000960 // In relocatable output we don't generate imports for data symbols.
961 // These live only in the symbol table.
962 if (Config->Relocatable && isa<DataSymbol>(Sym))
963 continue;
Sam Cleggc94d3932017-11-17 18:14:09 +0000964
Nicola Zaghene7245b42018-05-15 13:36:20 +0000965 LLVM_DEBUG(dbgs() << "import: " << Sym->getName() << "\n");
Sam Clegg93102972018-02-23 05:08:53 +0000966 ImportedSymbols.emplace_back(Sym);
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000967 if (auto *F = dyn_cast<FunctionSymbol>(Sym))
968 F->setFunctionIndex(NumImportedFunctions++);
Heejin Ahne915a712018-12-08 06:17:43 +0000969 else if (auto *G = dyn_cast<GlobalSymbol>(Sym))
970 G->setGlobalIndex(NumImportedGlobals++);
Sam Cleggd425d6b2019-03-12 21:53:23 +0000971 else if (auto *D = dyn_cast<UndefinedData>(Sym))
972 D->setGlobalIndex(NumImportedGlobals++);
Sam Clegg93102972018-02-23 05:08:53 +0000973 else
Heejin Ahne915a712018-12-08 06:17:43 +0000974 cast<EventSymbol>(Sym)->setEventIndex(NumImportedEvents++);
Sam Cleggc94d3932017-11-17 18:14:09 +0000975 }
976}
977
Sam Cleggd3052d52018-01-18 23:40:49 +0000978void Writer::calculateExports() {
Sam Clegg93102972018-02-23 05:08:53 +0000979 if (Config->Relocatable)
980 return;
Sam Cleggf0d433d2018-02-02 22:59:56 +0000981
Sam Cleggd6beb322018-05-10 18:10:34 +0000982 if (!Config->Relocatable && !Config->ImportMemory)
983 Exports.push_back(WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0});
984
985 if (!Config->Relocatable && Config->ExportTable)
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000986 Exports.push_back(WasmExport{FunctionTableName, WASM_EXTERNAL_TABLE, 0});
Sam Cleggd6beb322018-05-10 18:10:34 +0000987
988 unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size();
989
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +0000990 for (Symbol *Sym : Symtab->getSymbols()) {
Sam Cleggce004bf2018-06-28 17:04:58 +0000991 if (!Sym->isExported())
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +0000992 continue;
Sam Clegg93102972018-02-23 05:08:53 +0000993 if (!Sym->isLive())
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +0000994 continue;
Sam Clegg93102972018-02-23 05:08:53 +0000995
Sam Cleggd6beb322018-05-10 18:10:34 +0000996 StringRef Name = Sym->getName();
997 WasmExport Export;
998 if (auto *F = dyn_cast<DefinedFunction>(Sym)) {
999 Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()};
1000 } else if (auto *G = dyn_cast<DefinedGlobal>(Sym)) {
Sam Clegg177b4582018-06-07 01:27:07 +00001001 // TODO(sbc): Remove this check once to mutable global proposal is
1002 // implement in all major browsers.
1003 // See: https://github.com/WebAssembly/mutable-global
1004 if (G->getGlobalType()->Mutable) {
1005 // Only the __stack_pointer should ever be create as mutable.
1006 assert(G == WasmSym::StackPointer);
1007 continue;
1008 }
Sam Cleggd6beb322018-05-10 18:10:34 +00001009 Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()};
Heejin Ahne915a712018-12-08 06:17:43 +00001010 } else if (auto *E = dyn_cast<DefinedEvent>(Sym)) {
1011 Export = {Name, WASM_EXTERNAL_EVENT, E->getEventIndex()};
Sam Cleggd6beb322018-05-10 18:10:34 +00001012 } else {
1013 auto *D = cast<DefinedData>(Sym);
Sam Clegg93102972018-02-23 05:08:53 +00001014 DefinedFakeGlobals.emplace_back(D);
Sam Cleggd6beb322018-05-10 18:10:34 +00001015 Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++};
1016 }
1017
Nicola Zaghene7245b42018-05-15 13:36:20 +00001018 LLVM_DEBUG(dbgs() << "Export: " << Name << "\n");
Sam Cleggd6beb322018-05-10 18:10:34 +00001019 Exports.push_back(Export);
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +00001020 }
Sam Clegg93102972018-02-23 05:08:53 +00001021}
1022
1023void Writer::assignSymtab() {
1024 if (!Config->Relocatable)
1025 return;
1026
Sam Cleggd177ab22018-05-04 23:14:42 +00001027 StringMap<uint32_t> SectionSymbolIndices;
1028
Sam Clegg93102972018-02-23 05:08:53 +00001029 unsigned SymbolIndex = SymtabEntries.size();
Sam Cleggd177ab22018-05-04 23:14:42 +00001030
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001031 auto AddSymbol = [&](Symbol *Sym) {
1032 if (auto *S = dyn_cast<SectionSymbol>(Sym)) {
1033 StringRef Name = S->getName();
1034 if (CustomSectionMapping.count(Name) == 0)
1035 return;
Sam Cleggd177ab22018-05-04 23:14:42 +00001036
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001037 auto SSI = SectionSymbolIndices.find(Name);
1038 if (SSI != SectionSymbolIndices.end()) {
1039 Sym->setOutputSymbolIndex(SSI->second);
1040 return;
Sam Cleggd177ab22018-05-04 23:14:42 +00001041 }
1042
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001043 SectionSymbolIndices[Name] = SymbolIndex;
1044 CustomSectionSymbols[Name] = cast<SectionSymbol>(Sym);
Sam Cleggd3052d52018-01-18 23:40:49 +00001045
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001046 Sym->markLive();
1047 }
1048
1049 // (Since this is relocatable output, GC is not performed so symbols must
1050 // be live.)
1051 assert(Sym->isLive());
1052 Sym->setOutputSymbolIndex(SymbolIndex++);
1053 SymtabEntries.emplace_back(Sym);
1054 };
1055
1056 for (Symbol *Sym : Symtab->getSymbols())
Sam Cleggd15a41542019-03-08 21:10:48 +00001057 if (Sym->IsUsedInRegularObj)
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001058 AddSymbol(Sym);
1059
1060 for (ObjFile *File : Symtab->ObjectFiles) {
1061 LLVM_DEBUG(dbgs() << "Local symtab entries: " << File->getName() << "\n");
1062 for (Symbol *Sym : File->getSymbols())
1063 if (Sym->isLocal())
1064 AddSymbol(Sym);
1065 }
Sam Cleggd3052d52018-01-18 23:40:49 +00001066}
1067
Sam Cleggc375e4e2018-01-10 19:18:22 +00001068uint32_t Writer::lookupType(const WasmSignature &Sig) {
Sam Clegg8d027d62018-01-10 20:12:26 +00001069 auto It = TypeIndices.find(Sig);
1070 if (It == TypeIndices.end()) {
Sam Cleggc375e4e2018-01-10 19:18:22 +00001071 error("type not found: " + toString(Sig));
Sam Clegg8d027d62018-01-10 20:12:26 +00001072 return 0;
1073 }
1074 return It->second;
Sam Cleggc375e4e2018-01-10 19:18:22 +00001075}
1076
1077uint32_t Writer::registerType(const WasmSignature &Sig) {
Sam Cleggb8621592017-11-30 01:40:08 +00001078 auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size()));
Sam Cleggc375e4e2018-01-10 19:18:22 +00001079 if (Pair.second) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001080 LLVM_DEBUG(dbgs() << "type " << toString(Sig) << "\n");
Sam Cleggb8621592017-11-30 01:40:08 +00001081 Types.push_back(&Sig);
Sam Cleggc375e4e2018-01-10 19:18:22 +00001082 }
Sam Cleggb8621592017-11-30 01:40:08 +00001083 return Pair.first->second;
1084}
1085
Sam Cleggc94d3932017-11-17 18:14:09 +00001086void Writer::calculateTypes() {
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001087 // The output type section is the union of the following sets:
1088 // 1. Any signature used in the TYPE relocation
1089 // 2. The signatures of all imported functions
1090 // 3. The signatures of all defined functions
Heejin Ahne915a712018-12-08 06:17:43 +00001091 // 4. The signatures of all imported events
1092 // 5. The signatures of all defined events
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001093
Sam Cleggc94d3932017-11-17 18:14:09 +00001094 for (ObjFile *File : Symtab->ObjectFiles) {
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001095 ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
1096 for (uint32_t I = 0; I < Types.size(); I++)
1097 if (File->TypeIsUsed[I])
1098 File->TypeMap[I] = registerType(Types[I]);
Sam Cleggc94d3932017-11-17 18:14:09 +00001099 }
Sam Clegg50686852018-01-12 18:35:13 +00001100
Heejin Ahne915a712018-12-08 06:17:43 +00001101 for (const Symbol *Sym : ImportedSymbols) {
Sam Clegg93102972018-02-23 05:08:53 +00001102 if (auto *F = dyn_cast<FunctionSymbol>(Sym))
Heejin Ahne915a712018-12-08 06:17:43 +00001103 registerType(*F->Signature);
1104 else if (auto *E = dyn_cast<EventSymbol>(Sym))
1105 registerType(*E->Signature);
1106 }
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001107
Sam Clegg9f934222018-02-21 18:29:23 +00001108 for (const InputFunction *F : InputFunctions)
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001109 registerType(F->Signature);
Heejin Ahne915a712018-12-08 06:17:43 +00001110
1111 for (const InputEvent *E : InputEvents)
1112 registerType(E->Signature);
Sam Cleggc94d3932017-11-17 18:14:09 +00001113}
1114
Sam Clegg632c21792019-03-16 01:18:12 +00001115void Writer::processRelocations(InputChunk *Chunk) {
1116 if (!Chunk->Live)
1117 return;
1118 ObjFile *File = Chunk->File;
1119 ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
1120 for (const WasmRelocation &Reloc : Chunk->getRelocations()) {
1121 switch (Reloc.Type) {
1122 case R_WASM_TABLE_INDEX_I32:
1123 case R_WASM_TABLE_INDEX_SLEB: {
1124 FunctionSymbol *Sym = File->getFunctionSymbol(Reloc.Index);
1125 if (Sym->hasTableIndex() || !Sym->hasFunctionIndex())
1126 continue;
1127 Sym->setTableIndex(TableBase + IndirectFunctions.size());
1128 IndirectFunctions.emplace_back(Sym);
1129 break;
1130 }
1131 case R_WASM_TYPE_INDEX_LEB:
1132 // Mark target type as live
1133 File->TypeMap[Reloc.Index] = registerType(Types[Reloc.Index]);
1134 File->TypeIsUsed[Reloc.Index] = true;
1135 break;
1136 case R_WASM_MEMORY_ADDR_SLEB:
1137 case R_WASM_MEMORY_ADDR_I32:
1138 case R_WASM_MEMORY_ADDR_LEB: {
1139 DataSymbol *DataSym = File->getDataSymbol(Reloc.Index);
1140 if (!Config->Relocatable && !isa<DefinedData>(DataSym) &&
1141 !DataSym->isWeak())
1142 error(File->getName() +
1143 ": relocation of type R_WASM_MEMORY_ADDR_* "
1144 "against undefined data symbol: " +
1145 DataSym->getName());
1146 break;
1147 }
1148 }
1149 }
1150}
1151
Sam Clegg8d146bb2018-01-09 23:56:44 +00001152void Writer::assignIndexes() {
Heejin Ahnaeaab992018-11-19 23:21:25 +00001153 assert(InputFunctions.empty());
1154 uint32_t FunctionIndex = NumImportedFunctions;
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001155 auto AddDefinedFunction = [&](InputFunction *Func) {
1156 if (!Func->Live)
1157 return;
1158 InputFunctions.emplace_back(Func);
Sam Clegge3f3ccf2018-03-12 19:56:23 +00001159 Func->setFunctionIndex(FunctionIndex++);
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001160 };
1161
Nicholas Wilson5639da82018-03-12 15:44:07 +00001162 for (InputFunction *Func : Symtab->SyntheticFunctions)
1163 AddDefinedFunction(Func);
1164
Sam Clegg87e61922018-01-08 23:39:11 +00001165 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001166 LLVM_DEBUG(dbgs() << "Functions: " << File->getName() << "\n");
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001167 for (InputFunction *Func : File->Functions)
1168 AddDefinedFunction(Func);
Sam Clegg8d146bb2018-01-09 23:56:44 +00001169 }
1170
1171 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001172 LLVM_DEBUG(dbgs() << "Handle relocs: " << File->getName() << "\n");
Sam Clegg93102972018-02-23 05:08:53 +00001173 for (InputChunk *Chunk : File->Functions)
Sam Clegg632c21792019-03-16 01:18:12 +00001174 processRelocations(Chunk);
Sam Clegg93102972018-02-23 05:08:53 +00001175 for (InputChunk *Chunk : File->Segments)
Sam Clegg632c21792019-03-16 01:18:12 +00001176 processRelocations(Chunk);
Sam Cleggd177ab22018-05-04 23:14:42 +00001177 for (auto &P : File->CustomSections)
Sam Clegg632c21792019-03-16 01:18:12 +00001178 processRelocations(P);
Sam Clegg93102972018-02-23 05:08:53 +00001179 }
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001180
Heejin Ahnaeaab992018-11-19 23:21:25 +00001181 assert(InputGlobals.empty());
1182 uint32_t GlobalIndex = NumImportedGlobals;
Sam Clegg93102972018-02-23 05:08:53 +00001183 auto AddDefinedGlobal = [&](InputGlobal *Global) {
1184 if (Global->Live) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001185 LLVM_DEBUG(dbgs() << "AddDefinedGlobal: " << GlobalIndex << "\n");
Sam Clegge3f3ccf2018-03-12 19:56:23 +00001186 Global->setGlobalIndex(GlobalIndex++);
Sam Clegg93102972018-02-23 05:08:53 +00001187 InputGlobals.push_back(Global);
1188 }
1189 };
1190
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001191 for (InputGlobal *Global : Symtab->SyntheticGlobals)
1192 AddDefinedGlobal(Global);
Sam Clegg93102972018-02-23 05:08:53 +00001193
1194 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001195 LLVM_DEBUG(dbgs() << "Globals: " << File->getName() << "\n");
Sam Clegg93102972018-02-23 05:08:53 +00001196 for (InputGlobal *Global : File->Globals)
1197 AddDefinedGlobal(Global);
Sam Cleggc94d3932017-11-17 18:14:09 +00001198 }
Heejin Ahne915a712018-12-08 06:17:43 +00001199
1200 assert(InputEvents.empty());
1201 uint32_t EventIndex = NumImportedEvents;
1202 auto AddDefinedEvent = [&](InputEvent *Event) {
1203 if (Event->Live) {
1204 LLVM_DEBUG(dbgs() << "AddDefinedEvent: " << EventIndex << "\n");
1205 Event->setEventIndex(EventIndex++);
1206 InputEvents.push_back(Event);
1207 }
1208 };
1209
1210 for (ObjFile *File : Symtab->ObjectFiles) {
1211 LLVM_DEBUG(dbgs() << "Events: " << File->getName() << "\n");
1212 for (InputEvent *Event : File->Events)
1213 AddDefinedEvent(Event);
1214 }
Sam Cleggc94d3932017-11-17 18:14:09 +00001215}
1216
1217static StringRef getOutputDataSegmentName(StringRef Name) {
Sam Cleggbfb75342018-11-15 00:37:21 +00001218 // With PIC code we currently only support a single data segment since
1219 // we only have a single __memory_base to use as our base address.
1220 if (Config->Pic)
1221 return "data";
Sam Clegg66844762018-05-10 18:23:51 +00001222 if (!Config->MergeDataSegments)
Sam Cleggc94d3932017-11-17 18:14:09 +00001223 return Name;
Rui Ueyama4764b572018-02-28 00:57:28 +00001224 if (Name.startswith(".text."))
1225 return ".text";
1226 if (Name.startswith(".data."))
1227 return ".data";
1228 if (Name.startswith(".bss."))
1229 return ".bss";
Sam Clegg57694c52018-08-08 18:02:55 +00001230 if (Name.startswith(".rodata."))
1231 return ".rodata";
Sam Cleggc94d3932017-11-17 18:14:09 +00001232 return Name;
1233}
1234
1235void Writer::createOutputSegments() {
1236 for (ObjFile *File : Symtab->ObjectFiles) {
1237 for (InputSegment *Segment : File->Segments) {
Sam Clegg447ae402018-02-13 20:29:38 +00001238 if (!Segment->Live)
Sam Clegge0f6fcd2018-01-12 22:25:17 +00001239 continue;
Sam Cleggc94d3932017-11-17 18:14:09 +00001240 StringRef Name = getOutputDataSegmentName(Segment->getName());
1241 OutputSegment *&S = SegmentMap[Name];
1242 if (S == nullptr) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001243 LLVM_DEBUG(dbgs() << "new segment: " << Name << "\n");
Sam Clegg93102972018-02-23 05:08:53 +00001244 S = make<OutputSegment>(Name, Segments.size());
Sam Cleggc94d3932017-11-17 18:14:09 +00001245 Segments.push_back(S);
1246 }
1247 S->addInputSegment(Segment);
Nicola Zaghene7245b42018-05-15 13:36:20 +00001248 LLVM_DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n");
Sam Cleggc94d3932017-11-17 18:14:09 +00001249 }
1250 }
1251}
1252
Sam Clegg50686852018-01-12 18:35:13 +00001253static const int OPCODE_CALL = 0x10;
1254static const int OPCODE_END = 0xb;
1255
1256// Create synthetic "__wasm_call_ctors" function based on ctor functions
1257// in input object.
1258void Writer::createCtorFunction() {
Sam Clegg0e6b42f2019-03-01 22:35:47 +00001259 if (!WasmSym::CallCtors->isLive())
1260 return;
1261
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +00001262 // First write the body's contents to a string.
1263 std::string BodyContent;
Sam Clegg50686852018-01-12 18:35:13 +00001264 {
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +00001265 raw_string_ostream OS(BodyContent);
Sam Clegg50686852018-01-12 18:35:13 +00001266 writeUleb128(OS, 0, "num locals");
Sam Clegg93102972018-02-23 05:08:53 +00001267 for (const WasmInitEntry &F : InitFunctions) {
Sam Clegg50686852018-01-12 18:35:13 +00001268 writeU8(OS, OPCODE_CALL, "CALL");
Sam Clegge3f3ccf2018-03-12 19:56:23 +00001269 writeUleb128(OS, F.Sym->getFunctionIndex(), "function index");
Sam Clegg50686852018-01-12 18:35:13 +00001270 }
1271 writeU8(OS, OPCODE_END, "END");
1272 }
1273
1274 // Once we know the size of the body we can create the final function body
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +00001275 std::string FunctionBody;
1276 {
1277 raw_string_ostream OS(FunctionBody);
1278 writeUleb128(OS, BodyContent.size(), "function size");
1279 OS << BodyContent;
1280 }
Rui Ueyama29abfe42018-02-28 17:43:15 +00001281
Sam Cleggea656472018-10-22 08:35:39 +00001282 ArrayRef<uint8_t> Body = arrayRefFromStringRef(Saver.save(FunctionBody));
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001283 cast<SyntheticFunction>(WasmSym::CallCtors->Function)->setBody(Body);
Sam Clegg50686852018-01-12 18:35:13 +00001284}
1285
1286// Populate InitFunctions vector with init functions from all input objects.
1287// This is then used either when creating the output linking section or to
1288// synthesize the "__wasm_call_ctors" function.
1289void Writer::calculateInitFunctions() {
Sam Clegg61f13b32019-03-02 04:55:02 +00001290 if (!Config->Relocatable && !WasmSym::CallCtors->isLive())
1291 return;
1292
Sam Clegg50686852018-01-12 18:35:13 +00001293 for (ObjFile *File : Symtab->ObjectFiles) {
1294 const WasmLinkingData &L = File->getWasmObj()->linkingData();
Nicholas Wilsoncb81a0c2018-03-02 14:46:54 +00001295 for (const WasmInitFunc &F : L.InitFunctions) {
1296 FunctionSymbol *Sym = File->getFunctionSymbol(F.Symbol);
Sam Clegg0e6b42f2019-03-01 22:35:47 +00001297 assert(Sym->isLive());
Heejin Ahne915a712018-12-08 06:17:43 +00001298 if (*Sym->Signature != WasmSignature{{}, {}})
Nicholas Wilsoncb81a0c2018-03-02 14:46:54 +00001299 error("invalid signature for init func: " + toString(*Sym));
1300 InitFunctions.emplace_back(WasmInitEntry{Sym, F.Priority});
1301 }
Sam Clegg50686852018-01-12 18:35:13 +00001302 }
Rui Ueyamada69b712018-02-28 00:15:59 +00001303
Sam Clegg50686852018-01-12 18:35:13 +00001304 // Sort in order of priority (lowest first) so that they are called
1305 // in the correct order.
Sam Clegg29b8feb2018-02-21 00:34:34 +00001306 std::stable_sort(InitFunctions.begin(), InitFunctions.end(),
Sam Clegg93102972018-02-23 05:08:53 +00001307 [](const WasmInitEntry &L, const WasmInitEntry &R) {
Sam Clegg29b8feb2018-02-21 00:34:34 +00001308 return L.Priority < R.Priority;
1309 });
Sam Clegg50686852018-01-12 18:35:13 +00001310}
1311
Sam Cleggc94d3932017-11-17 18:14:09 +00001312void Writer::run() {
Sam Cleggbfb75342018-11-15 00:37:21 +00001313 if (Config->Relocatable || Config->Pic)
Sam Clegg99eb42c2018-02-27 23:58:03 +00001314 Config->GlobalBase = 0;
1315
Sam Cleggbfb75342018-11-15 00:37:21 +00001316 // For PIC code the table base is assigned dynamically by the loader.
1317 // For non-PIC, we start at 1 so that accessing table index 0 always traps.
1318 if (!Config->Pic)
1319 TableBase = 1;
1320
Thomas Livelyf6f4f842019-03-20 20:26:45 +00001321 log("-- calculateTargetFeatures");
1322 calculateTargetFeatures();
Sam Cleggc94d3932017-11-17 18:14:09 +00001323 log("-- calculateImports");
1324 calculateImports();
Sam Clegg8d146bb2018-01-09 23:56:44 +00001325 log("-- assignIndexes");
1326 assignIndexes();
Sam Clegg50686852018-01-12 18:35:13 +00001327 log("-- calculateInitFunctions");
1328 calculateInitFunctions();
1329 if (!Config->Relocatable)
1330 createCtorFunction();
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001331 log("-- calculateTypes");
1332 calculateTypes();
Sam Clegg93102972018-02-23 05:08:53 +00001333 log("-- layoutMemory");
1334 layoutMemory();
1335 log("-- calculateExports");
1336 calculateExports();
Sam Cleggd177ab22018-05-04 23:14:42 +00001337 log("-- calculateCustomSections");
1338 calculateCustomSections();
Sam Clegg93102972018-02-23 05:08:53 +00001339 log("-- assignSymtab");
1340 assignSymtab();
Sam Cleggc94d3932017-11-17 18:14:09 +00001341
1342 if (errorHandler().Verbose) {
Sam Clegg9f934222018-02-21 18:29:23 +00001343 log("Defined Functions: " + Twine(InputFunctions.size()));
Sam Clegg93102972018-02-23 05:08:53 +00001344 log("Defined Globals : " + Twine(InputGlobals.size()));
Heejin Ahne915a712018-12-08 06:17:43 +00001345 log("Defined Events : " + Twine(InputEvents.size()));
Sam Clegg93102972018-02-23 05:08:53 +00001346 log("Function Imports : " + Twine(NumImportedFunctions));
1347 log("Global Imports : " + Twine(NumImportedGlobals));
Heejin Ahne915a712018-12-08 06:17:43 +00001348 log("Event Imports : " + Twine(NumImportedEvents));
Sam Cleggc94d3932017-11-17 18:14:09 +00001349 for (ObjFile *File : Symtab->ObjectFiles)
1350 File->dumpInfo();
1351 }
1352
Sam Cleggc94d3932017-11-17 18:14:09 +00001353 createHeader();
1354 log("-- createSections");
1355 createSections();
1356
1357 log("-- openFile");
1358 openFile();
1359 if (errorCount())
1360 return;
1361
1362 writeHeader();
1363
1364 log("-- writeSections");
1365 writeSections();
1366 if (errorCount())
1367 return;
1368
1369 if (Error E = Buffer->commit())
1370 fatal("failed to write the output file: " + toString(std::move(E)));
1371}
1372
1373// Open a result file.
1374void Writer::openFile() {
1375 log("writing: " + Config->OutputFile);
Sam Cleggc94d3932017-11-17 18:14:09 +00001376
1377 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1378 FileOutputBuffer::create(Config->OutputFile, FileSize,
1379 FileOutputBuffer::F_executable);
1380
1381 if (!BufferOrErr)
1382 error("failed to open " + Config->OutputFile + ": " +
1383 toString(BufferOrErr.takeError()));
1384 else
1385 Buffer = std::move(*BufferOrErr);
1386}
1387
1388void Writer::createHeader() {
1389 raw_string_ostream OS(Header);
1390 writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic");
1391 writeU32(OS, WasmVersion, "wasm version");
1392 OS.flush();
1393 FileSize += Header.size();
1394}
1395
1396void lld::wasm::writeResult() { Writer().run(); }