blob: e5fce719567375ccd1e005aed31d731d0d98bf2a [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;
Sam Clegg492f7522019-03-26 19:46:15 +0000121 std::vector<const Symbol *> GOTSymbols;
Sam Clegg93102972018-02-23 05:08:53 +0000122 unsigned NumImportedFunctions = 0;
123 unsigned NumImportedGlobals = 0;
Heejin Ahne915a712018-12-08 06:17:43 +0000124 unsigned NumImportedEvents = 0;
Sam Cleggd6beb322018-05-10 18:10:34 +0000125 std::vector<WasmExport> Exports;
Sam Clegg93102972018-02-23 05:08:53 +0000126 std::vector<const DefinedData *> DefinedFakeGlobals;
127 std::vector<InputGlobal *> InputGlobals;
Sam Clegg9f934222018-02-21 18:29:23 +0000128 std::vector<InputFunction *> InputFunctions;
Heejin Ahne915a712018-12-08 06:17:43 +0000129 std::vector<InputEvent *> InputEvents;
Sam Cleggdfb0b2c2018-02-14 18:27:59 +0000130 std::vector<const FunctionSymbol *> IndirectFunctions;
Sam Clegg93102972018-02-23 05:08:53 +0000131 std::vector<const Symbol *> SymtabEntries;
132 std::vector<WasmInitEntry> InitFunctions;
Sam Cleggc94d3932017-11-17 18:14:09 +0000133
Sam Clegg80ba4382018-04-10 16:12:49 +0000134 llvm::StringMap<std::vector<InputSection *>> CustomSectionMapping;
Sam Cleggd177ab22018-05-04 23:14:42 +0000135 llvm::StringMap<SectionSymbol *> CustomSectionSymbols;
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000136 llvm::SmallSet<std::string, 8> TargetFeatures;
Sam Clegg80ba4382018-04-10 16:12:49 +0000137
Sam Cleggc94d3932017-11-17 18:14:09 +0000138 // Elements that are used to construct the final output
139 std::string Header;
140 std::vector<OutputSection *> OutputSections;
141
142 std::unique_ptr<FileOutputBuffer> Buffer;
143
144 std::vector<OutputSegment *> Segments;
145 llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap;
146};
147
148} // anonymous namespace
149
Sam Cleggc94d3932017-11-17 18:14:09 +0000150void Writer::createImportSection() {
Sam Clegg492f7522019-03-26 19:46:15 +0000151 uint32_t NumImports = ImportedSymbols.size() + GOTSymbols.size();
Sam Cleggc94d3932017-11-17 18:14:09 +0000152 if (Config->ImportMemory)
153 ++NumImports;
Nicholas Wilsonfc90b302018-03-28 12:53:29 +0000154 if (Config->ImportTable)
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000155 ++NumImports;
Sam Cleggc94d3932017-11-17 18:14:09 +0000156
157 if (NumImports == 0)
158 return;
159
160 SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT);
161 raw_ostream &OS = Section->getStream();
162
163 writeUleb128(OS, NumImports, "import count");
164
Sam Cleggc94d3932017-11-17 18:14:09 +0000165 if (Config->ImportMemory) {
166 WasmImport Import;
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000167 Import.Module = DefaultModule;
Sam Cleggc94d3932017-11-17 18:14:09 +0000168 Import.Field = "memory";
169 Import.Kind = WASM_EXTERNAL_MEMORY;
170 Import.Memory.Flags = 0;
171 Import.Memory.Initial = NumMemoryPages;
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000172 if (MaxMemoryPages != 0) {
173 Import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX;
174 Import.Memory.Maximum = MaxMemoryPages;
175 }
Derek Schuff786760a2018-11-06 18:02:39 +0000176 if (Config->SharedMemory)
Derek Schuff3bea8bc2018-11-06 17:59:32 +0000177 Import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED;
Sam Cleggc94d3932017-11-17 18:14:09 +0000178 writeImport(OS, Import);
179 }
180
Nicholas Wilsonfc90b302018-03-28 12:53:29 +0000181 if (Config->ImportTable) {
Sam Cleggbfb75342018-11-15 00:37:21 +0000182 uint32_t TableSize = TableBase + IndirectFunctions.size();
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000183 WasmImport Import;
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000184 Import.Module = DefaultModule;
185 Import.Field = FunctionTableName;
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000186 Import.Kind = WASM_EXTERNAL_TABLE;
Thomas Lively25ff8932019-01-08 06:25:55 +0000187 Import.Table.ElemType = WASM_TYPE_FUNCREF;
Sam Clegg748f59cae2018-12-03 22:37:55 +0000188 Import.Table.Limits = {0, TableSize, 0};
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000189 writeImport(OS, Import);
190 }
191
Sam Clegg93102972018-02-23 05:08:53 +0000192 for (const Symbol *Sym : ImportedSymbols) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000193 WasmImport Import;
Dan Gohman9b84eea2019-02-07 22:00:48 +0000194 if (auto *F = dyn_cast<UndefinedFunction>(Sym)) {
195 Import.Field = F->ImportName;
196 Import.Module = F->ImportModule;
197 } else if (auto *G = dyn_cast<UndefinedGlobal>(Sym)) {
198 Import.Field = G->ImportName;
199 Import.Module = G->ImportModule;
200 } else {
201 Import.Field = Sym->getName();
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000202 Import.Module = DefaultModule;
Dan Gohman9b84eea2019-02-07 22:00:48 +0000203 }
Sam Clegg7cc07532019-02-01 02:29:57 +0000204
Sam Clegg93102972018-02-23 05:08:53 +0000205 if (auto *FunctionSym = dyn_cast<FunctionSymbol>(Sym)) {
206 Import.Kind = WASM_EXTERNAL_FUNCTION;
Heejin Ahne915a712018-12-08 06:17:43 +0000207 Import.SigIndex = lookupType(*FunctionSym->Signature);
208 } else if (auto *GlobalSym = dyn_cast<GlobalSymbol>(Sym)) {
Sam Clegg93102972018-02-23 05:08:53 +0000209 Import.Kind = WASM_EXTERNAL_GLOBAL;
210 Import.Global = *GlobalSym->getGlobalType();
Heejin Ahne915a712018-12-08 06:17:43 +0000211 } else {
212 auto *EventSym = cast<EventSymbol>(Sym);
213 Import.Kind = WASM_EXTERNAL_EVENT;
214 Import.Event.Attribute = EventSym->getEventType()->Attribute;
215 Import.Event.SigIndex = lookupType(*EventSym->Signature);
Sam Clegg93102972018-02-23 05:08:53 +0000216 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000217 writeImport(OS, Import);
218 }
Sam Clegg492f7522019-03-26 19:46:15 +0000219
220 for (const Symbol *Sym : GOTSymbols) {
221 WasmImport Import;
222 Import.Kind = WASM_EXTERNAL_GLOBAL;
223 Import.Global = {WASM_TYPE_I32, true};
224 if (isa<DataSymbol>(Sym))
225 Import.Module = "GOT.mem";
226 else
227 Import.Module = "GOT.func";
228 Import.Field = Sym->getName();
229 writeImport(OS, Import);
230 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000231}
232
233void Writer::createTypeSection() {
234 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE);
235 raw_ostream &OS = Section->getStream();
236 writeUleb128(OS, Types.size(), "type count");
Sam Cleggd451da12017-12-19 19:56:27 +0000237 for (const WasmSignature *Sig : Types)
Sam Cleggc94d3932017-11-17 18:14:09 +0000238 writeSig(OS, *Sig);
Sam Cleggc94d3932017-11-17 18:14:09 +0000239}
240
241void Writer::createFunctionSection() {
Sam Clegg9f934222018-02-21 18:29:23 +0000242 if (InputFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000243 return;
244
245 SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION);
246 raw_ostream &OS = Section->getStream();
247
Sam Clegg9f934222018-02-21 18:29:23 +0000248 writeUleb128(OS, InputFunctions.size(), "function count");
249 for (const InputFunction *Func : InputFunctions)
Sam Cleggc375e4e2018-01-10 19:18:22 +0000250 writeUleb128(OS, lookupType(Func->Signature), "sig index");
Sam Cleggc94d3932017-11-17 18:14:09 +0000251}
252
253void Writer::createMemorySection() {
254 if (Config->ImportMemory)
255 return;
256
257 SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY);
258 raw_ostream &OS = Section->getStream();
259
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000260 bool HasMax = MaxMemoryPages != 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000261 writeUleb128(OS, 1, "memory count");
Derek Schuff786760a2018-11-06 18:02:39 +0000262 unsigned Flags = 0;
263 if (HasMax)
264 Flags |= WASM_LIMITS_FLAG_HAS_MAX;
Derek Schuff3bea8bc2018-11-06 17:59:32 +0000265 if (Config->SharedMemory)
266 Flags |= WASM_LIMITS_FLAG_IS_SHARED;
267 writeUleb128(OS, Flags, "memory limits flags");
Sam Cleggc94d3932017-11-17 18:14:09 +0000268 writeUleb128(OS, NumMemoryPages, "initial pages");
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000269 if (HasMax)
270 writeUleb128(OS, MaxMemoryPages, "max pages");
Sam Cleggc94d3932017-11-17 18:14:09 +0000271}
272
273void Writer::createGlobalSection() {
Sam Clegg93102972018-02-23 05:08:53 +0000274 unsigned NumGlobals = InputGlobals.size() + DefinedFakeGlobals.size();
275 if (NumGlobals == 0)
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000276 return;
277
Sam Cleggc94d3932017-11-17 18:14:09 +0000278 SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL);
279 raw_ostream &OS = Section->getStream();
280
Sam Clegg93102972018-02-23 05:08:53 +0000281 writeUleb128(OS, NumGlobals, "global count");
282 for (const InputGlobal *G : InputGlobals)
283 writeGlobal(OS, G->Global);
284 for (const DefinedData *Sym : DefinedFakeGlobals) {
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000285 WasmGlobal Global;
Sam Clegg93102972018-02-23 05:08:53 +0000286 Global.Type = {WASM_TYPE_I32, false};
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000287 Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
288 Global.InitExpr.Value.Int32 = Sym->getVirtualAddress();
Sam Cleggc94d3932017-11-17 18:14:09 +0000289 writeGlobal(OS, Global);
290 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000291}
292
Heejin Ahne915a712018-12-08 06:17:43 +0000293// The event section contains a list of declared wasm events associated with the
294// module. Currently the only supported event kind is exceptions. A single event
295// entry represents a single event with an event tag. All C++ exceptions are
296// represented by a single event. An event entry in this section contains
297// information on what kind of event it is (e.g. exception) and the type of
298// values contained in a single event object. (In wasm, an event can contain
299// multiple values of primitive types. But for C++ exceptions, we just throw a
300// pointer which is an i32 value (for wasm32 architecture), so the signature of
301// C++ exception is (i32)->(void), because all event types are assumed to have
302// void return type to share WasmSignature with functions.)
303void Writer::createEventSection() {
304 unsigned NumEvents = InputEvents.size();
305 if (NumEvents == 0)
306 return;
307
308 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EVENT);
309 raw_ostream &OS = Section->getStream();
310
311 writeUleb128(OS, NumEvents, "event count");
312 for (InputEvent *E : InputEvents) {
313 E->Event.Type.SigIndex = lookupType(E->Signature);
314 writeEvent(OS, E->Event);
315 }
316}
317
Sam Cleggc94d3932017-11-17 18:14:09 +0000318void Writer::createTableSection() {
Nicholas Wilsonfc90b302018-03-28 12:53:29 +0000319 if (Config->ImportTable)
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000320 return;
321
322 // Always output a table section (or table import), even if there are no
323 // indirect calls. There are two reasons for this:
Sam Cleggfc1a9122017-12-11 22:00:56 +0000324 // 1. For executables it is useful to have an empty table slot at 0
325 // which can be filled with a null function call handler.
326 // 2. If we don't do this, any program that contains a call_indirect but
327 // no address-taken function will fail at validation time since it is
328 // a validation error to include a call_indirect instruction if there
329 // is not table.
Sam Cleggbfb75342018-11-15 00:37:21 +0000330 uint32_t TableSize = TableBase + IndirectFunctions.size();
Sam Cleggfc1a9122017-12-11 22:00:56 +0000331
Sam Cleggc94d3932017-11-17 18:14:09 +0000332 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE);
333 raw_ostream &OS = Section->getStream();
334
335 writeUleb128(OS, 1, "table count");
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000336 WasmLimits Limits = {WASM_LIMITS_FLAG_HAS_MAX, TableSize, TableSize};
Thomas Lively25ff8932019-01-08 06:25:55 +0000337 writeTableType(OS, WasmTable{WASM_TYPE_FUNCREF, Limits});
Sam Cleggc94d3932017-11-17 18:14:09 +0000338}
339
340void Writer::createExportSection() {
Sam Cleggd6beb322018-05-10 18:10:34 +0000341 if (!Exports.size())
Sam Cleggc94d3932017-11-17 18:14:09 +0000342 return;
343
344 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT);
345 raw_ostream &OS = Section->getStream();
346
Sam Cleggd6beb322018-05-10 18:10:34 +0000347 writeUleb128(OS, Exports.size(), "export count");
348 for (const WasmExport &Export : Exports)
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000349 writeExport(OS, Export);
Sam Cleggc94d3932017-11-17 18:14:09 +0000350}
351
Sam Cleggd177ab22018-05-04 23:14:42 +0000352void Writer::calculateCustomSections() {
353 log("calculateCustomSections");
354 bool StripDebug = Config->StripDebug || Config->StripAll;
355 for (ObjFile *File : Symtab->ObjectFiles) {
356 for (InputSection *Section : File->CustomSections) {
357 StringRef Name = Section->getName();
358 // These custom sections are known the linker and synthesized rather than
359 // blindly copied
Thomas Lively2a0868f2019-01-17 02:29:41 +0000360 if (Name == "linking" || Name == "name" || Name == "producers" ||
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000361 Name == "target_features" || Name.startswith("reloc."))
Sam Cleggd177ab22018-05-04 23:14:42 +0000362 continue;
363 // .. or it is a debug section
364 if (StripDebug && Name.startswith(".debug_"))
365 continue;
366 CustomSectionMapping[Name].push_back(Section);
367 }
368 }
369}
370
Sam Clegg80ba4382018-04-10 16:12:49 +0000371void Writer::createCustomSections() {
372 log("createCustomSections");
Sam Clegg80ba4382018-04-10 16:12:49 +0000373 for (auto &Pair : CustomSectionMapping) {
374 StringRef Name = Pair.first();
Sam Cleggd177ab22018-05-04 23:14:42 +0000375
376 auto P = CustomSectionSymbols.find(Name);
377 if (P != CustomSectionSymbols.end()) {
378 uint32_t SectionIndex = OutputSections.size();
379 P->second->setOutputSectionIndex(SectionIndex);
380 }
381
Nicola Zaghene7245b42018-05-15 13:36:20 +0000382 LLVM_DEBUG(dbgs() << "createCustomSection: " << Name << "\n");
Sam Clegg80ba4382018-04-10 16:12:49 +0000383 OutputSections.push_back(make<CustomSection>(Name, Pair.second));
384 }
385}
386
Sam Cleggc94d3932017-11-17 18:14:09 +0000387void Writer::createElemSection() {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000388 if (IndirectFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000389 return;
390
391 SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM);
392 raw_ostream &OS = Section->getStream();
393
394 writeUleb128(OS, 1, "segment count");
395 writeUleb128(OS, 0, "table index");
396 WasmInitExpr InitExpr;
Sam Cleggbfb75342018-11-15 00:37:21 +0000397 if (Config->Pic) {
Thomas Lively25ff8932019-01-08 06:25:55 +0000398 InitExpr.Opcode = WASM_OPCODE_GLOBAL_GET;
Sam Clegg2dad4e22018-11-15 18:15:54 +0000399 InitExpr.Value.Global = WasmSym::TableBase->getGlobalIndex();
Sam Cleggbfb75342018-11-15 00:37:21 +0000400 } else {
401 InitExpr.Opcode = WASM_OPCODE_I32_CONST;
402 InitExpr.Value.Int32 = TableBase;
403 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000404 writeInitExpr(OS, InitExpr);
Sam Cleggfc1a9122017-12-11 22:00:56 +0000405 writeUleb128(OS, IndirectFunctions.size(), "elem count");
Sam Cleggc94d3932017-11-17 18:14:09 +0000406
Sam Cleggbfb75342018-11-15 00:37:21 +0000407 uint32_t TableIndex = TableBase;
Sam Cleggdfb0b2c2018-02-14 18:27:59 +0000408 for (const FunctionSymbol *Sym : IndirectFunctions) {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000409 assert(Sym->getTableIndex() == TableIndex);
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000410 writeUleb128(OS, Sym->getFunctionIndex(), "function index");
Sam Cleggfc1a9122017-12-11 22:00:56 +0000411 ++TableIndex;
412 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000413}
414
415void Writer::createCodeSection() {
Sam Clegg9f934222018-02-21 18:29:23 +0000416 if (InputFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000417 return;
418
419 log("createCodeSection");
420
Sam Clegg9f934222018-02-21 18:29:23 +0000421 auto Section = make<CodeSection>(InputFunctions);
Sam Cleggc94d3932017-11-17 18:14:09 +0000422 OutputSections.push_back(Section);
423}
424
425void Writer::createDataSection() {
426 if (!Segments.size())
427 return;
428
429 log("createDataSection");
430 auto Section = make<DataSection>(Segments);
431 OutputSections.push_back(Section);
432}
433
Sam Cleggd451da12017-12-19 19:56:27 +0000434// Create relocations sections in the final output.
Sam Cleggc94d3932017-11-17 18:14:09 +0000435// These are only created when relocatable output is requested.
436void Writer::createRelocSections() {
437 log("createRelocSections");
438 // Don't use iterator here since we are adding to OutputSection
439 size_t OrigSize = OutputSections.size();
Rui Ueyamaffa650a2018-04-24 23:09:57 +0000440 for (size_t I = 0; I < OrigSize; I++) {
441 OutputSection *OSec = OutputSections[I];
Rui Ueyama37254062018-02-28 00:01:31 +0000442 uint32_t Count = OSec->numRelocations();
Sam Cleggc94d3932017-11-17 18:14:09 +0000443 if (!Count)
444 continue;
445
Rui Ueyama37254062018-02-28 00:01:31 +0000446 StringRef Name;
447 if (OSec->Type == WASM_SEC_DATA)
448 Name = "reloc.DATA";
449 else if (OSec->Type == WASM_SEC_CODE)
450 Name = "reloc.CODE";
Sam Cleggd177ab22018-05-04 23:14:42 +0000451 else if (OSec->Type == WASM_SEC_CUSTOM)
452 Name = Saver.save("reloc." + OSec->Name);
Sam Cleggc94d3932017-11-17 18:14:09 +0000453 else
Sam Cleggd177ab22018-05-04 23:14:42 +0000454 llvm_unreachable(
455 "relocations only supported for code, data, or custom sections");
Sam Cleggc94d3932017-11-17 18:14:09 +0000456
Rui Ueyama37254062018-02-28 00:01:31 +0000457 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, Name);
Sam Cleggc94d3932017-11-17 18:14:09 +0000458 raw_ostream &OS = Section->getStream();
Rui Ueyamaffa650a2018-04-24 23:09:57 +0000459 writeUleb128(OS, I, "reloc section");
Sam Cleggc94d3932017-11-17 18:14:09 +0000460 writeUleb128(OS, Count, "reloc count");
Rui Ueyama37254062018-02-28 00:01:31 +0000461 OSec->writeRelocations(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000462 }
463}
464
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000465static uint32_t getWasmFlags(const Symbol *Sym) {
466 uint32_t Flags = 0;
467 if (Sym->isLocal())
468 Flags |= WASM_SYMBOL_BINDING_LOCAL;
469 if (Sym->isWeak())
470 Flags |= WASM_SYMBOL_BINDING_WEAK;
471 if (Sym->isHidden())
472 Flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
473 if (Sym->isUndefined())
474 Flags |= WASM_SYMBOL_UNDEFINED;
Dan Gohman9b84eea2019-02-07 22:00:48 +0000475 if (auto *F = dyn_cast<UndefinedFunction>(Sym)) {
476 if (F->getName() != F->ImportName)
477 Flags |= WASM_SYMBOL_EXPLICIT_NAME;
478 } else if (auto *G = dyn_cast<UndefinedGlobal>(Sym)) {
479 if (G->getName() != G->ImportName)
480 Flags |= WASM_SYMBOL_EXPLICIT_NAME;
481 }
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000482 return Flags;
483}
484
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000485// Some synthetic sections (e.g. "name" and "linking") have subsections.
486// Just like the synthetic sections themselves these need to be created before
487// they can be written out (since they are preceded by their length). This
488// class is used to create subsections and then write them into the stream
489// of the parent section.
490class SubSection {
491public:
492 explicit SubSection(uint32_t Type) : Type(Type) {}
493
494 void writeTo(raw_ostream &To) {
495 OS.flush();
Rui Ueyama67769102018-02-28 03:38:14 +0000496 writeUleb128(To, Type, "subsection type");
497 writeUleb128(To, Body.size(), "subsection size");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000498 To.write(Body.data(), Body.size());
499 }
500
501private:
502 uint32_t Type;
503 std::string Body;
504
505public:
506 raw_string_ostream OS{Body};
507};
508
Sam Cleggbfb75342018-11-15 00:37:21 +0000509// Create the custom "dylink" section containing information for the dynamic
510// linker.
511// See
512// https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md
513void Writer::createDylinkSection() {
514 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "dylink");
515 raw_ostream &OS = Section->getStream();
516
517 writeUleb128(OS, MemSize, "MemSize");
Sam Clegg6320efb2019-01-16 01:43:21 +0000518 writeUleb128(OS, MemAlign, "MemAlign");
Sam Cleggbfb75342018-11-15 00:37:21 +0000519 writeUleb128(OS, IndirectFunctions.size(), "TableSize");
520 writeUleb128(OS, 0, "TableAlign");
Sam Clegga688a422019-03-13 21:29:20 +0000521 writeUleb128(OS, Symtab->SharedFiles.size(), "Needed");
522 for (auto *SO : Symtab->SharedFiles)
523 writeStr(OS, llvm::sys::path::filename(SO->getName()), "so name");
Sam Cleggbfb75342018-11-15 00:37:21 +0000524}
525
Sam Clegg49ed9262017-12-01 00:53:21 +0000526// Create the custom "linking" section containing linker metadata.
Sam Cleggc94d3932017-11-17 18:14:09 +0000527// This is only created when relocatable output is requested.
528void Writer::createLinkingSection() {
529 SyntheticSection *Section =
530 createSyntheticSection(WASM_SEC_CUSTOM, "linking");
531 raw_ostream &OS = Section->getStream();
532
Sam Clegg2b8b1792018-04-26 18:17:21 +0000533 writeUleb128(OS, WasmMetadataVersion, "Version");
Sam Clegg0d0dd392017-12-19 17:09:45 +0000534
Sam Clegg93102972018-02-23 05:08:53 +0000535 if (!SymtabEntries.empty()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000536 SubSection Sub(WASM_SYMBOL_TABLE);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000537 writeUleb128(Sub.OS, SymtabEntries.size(), "num symbols");
538
Sam Clegg93102972018-02-23 05:08:53 +0000539 for (const Symbol *Sym : SymtabEntries) {
540 assert(Sym->isDefined() || Sym->isUndefined());
541 WasmSymbolType Kind = Sym->getWasmType();
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000542 uint32_t Flags = getWasmFlags(Sym);
543
Sam Clegg8518e7d2018-03-01 18:06:39 +0000544 writeU8(Sub.OS, Kind, "sym kind");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000545 writeUleb128(Sub.OS, Flags, "sym flags");
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000546
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000547 if (auto *F = dyn_cast<FunctionSymbol>(Sym)) {
548 writeUleb128(Sub.OS, F->getFunctionIndex(), "index");
Dan Gohman9b84eea2019-02-07 22:00:48 +0000549 if (Sym->isDefined() ||
550 (Flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000551 writeStr(Sub.OS, Sym->getName(), "sym name");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000552 } else if (auto *G = dyn_cast<GlobalSymbol>(Sym)) {
553 writeUleb128(Sub.OS, G->getGlobalIndex(), "index");
Dan Gohman9b84eea2019-02-07 22:00:48 +0000554 if (Sym->isDefined() ||
555 (Flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000556 writeStr(Sub.OS, Sym->getName(), "sym name");
Heejin Ahne915a712018-12-08 06:17:43 +0000557 } else if (auto *E = dyn_cast<EventSymbol>(Sym)) {
558 writeUleb128(Sub.OS, E->getEventIndex(), "index");
Dan Gohman9b84eea2019-02-07 22:00:48 +0000559 if (Sym->isDefined() ||
560 (Flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
Heejin Ahne915a712018-12-08 06:17:43 +0000561 writeStr(Sub.OS, Sym->getName(), "sym name");
Andrea Di Biagio25c1a2f2018-05-05 10:53:31 +0000562 } else if (isa<DataSymbol>(Sym)) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000563 writeStr(Sub.OS, Sym->getName(), "sym name");
Sam Clegg93102972018-02-23 05:08:53 +0000564 if (auto *DataSym = dyn_cast<DefinedData>(Sym)) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000565 writeUleb128(Sub.OS, DataSym->getOutputSegmentIndex(), "index");
566 writeUleb128(Sub.OS, DataSym->getOutputSegmentOffset(),
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000567 "data offset");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000568 writeUleb128(Sub.OS, DataSym->getSize(), "data size");
Sam Clegg93102972018-02-23 05:08:53 +0000569 }
Sam Cleggd177ab22018-05-04 23:14:42 +0000570 } else {
571 auto *S = cast<SectionSymbol>(Sym);
572 writeUleb128(Sub.OS, S->getOutputSectionIndex(), "sym section index");
Sam Clegg93102972018-02-23 05:08:53 +0000573 }
Sam Cleggd3052d52018-01-18 23:40:49 +0000574 }
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000575
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000576 Sub.writeTo(OS);
Sam Cleggd3052d52018-01-18 23:40:49 +0000577 }
578
Sam Clegg0d0dd392017-12-19 17:09:45 +0000579 if (Segments.size()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000580 SubSection Sub(WASM_SEGMENT_INFO);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000581 writeUleb128(Sub.OS, Segments.size(), "num data segments");
Sam Cleggc94d3932017-11-17 18:14:09 +0000582 for (const OutputSegment *S : Segments) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000583 writeStr(Sub.OS, S->Name, "segment name");
584 writeUleb128(Sub.OS, S->Alignment, "alignment");
585 writeUleb128(Sub.OS, 0, "flags");
Sam Cleggc94d3932017-11-17 18:14:09 +0000586 }
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000587 Sub.writeTo(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000588 }
Sam Clegg0d0dd392017-12-19 17:09:45 +0000589
Sam Clegg0d0dd392017-12-19 17:09:45 +0000590 if (!InitFunctions.empty()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000591 SubSection Sub(WASM_INIT_FUNCS);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000592 writeUleb128(Sub.OS, InitFunctions.size(), "num init functions");
Sam Clegg93102972018-02-23 05:08:53 +0000593 for (const WasmInitEntry &F : InitFunctions) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000594 writeUleb128(Sub.OS, F.Priority, "priority");
595 writeUleb128(Sub.OS, F.Sym->getOutputSymbolIndex(), "function index");
Sam Clegg0d0dd392017-12-19 17:09:45 +0000596 }
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000597 Sub.writeTo(OS);
Sam Clegg0d0dd392017-12-19 17:09:45 +0000598 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000599
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000600 struct ComdatEntry {
601 unsigned Kind;
602 uint32_t Index;
603 };
604 std::map<StringRef, std::vector<ComdatEntry>> Comdats;
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000605
Sam Clegg9f934222018-02-21 18:29:23 +0000606 for (const InputFunction *F : InputFunctions) {
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000607 StringRef Comdat = F->getComdatName();
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000608 if (!Comdat.empty())
609 Comdats[Comdat].emplace_back(
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000610 ComdatEntry{WASM_COMDAT_FUNCTION, F->getFunctionIndex()});
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000611 }
612 for (uint32_t I = 0; I < Segments.size(); ++I) {
Sam Cleggf98bccf2018-01-13 15:57:48 +0000613 const auto &InputSegments = Segments[I]->InputSegments;
614 if (InputSegments.empty())
615 continue;
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000616 StringRef Comdat = InputSegments[0]->getComdatName();
Sam Clegga697df522018-01-13 15:59:53 +0000617#ifndef NDEBUG
Sam Cleggf98bccf2018-01-13 15:57:48 +0000618 for (const InputSegment *IS : InputSegments)
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000619 assert(IS->getComdatName() == Comdat);
Sam Clegga697df522018-01-13 15:59:53 +0000620#endif
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000621 if (!Comdat.empty())
622 Comdats[Comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, I});
623 }
624
625 if (!Comdats.empty()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000626 SubSection Sub(WASM_COMDAT_INFO);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000627 writeUleb128(Sub.OS, Comdats.size(), "num comdats");
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000628 for (const auto &C : Comdats) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000629 writeStr(Sub.OS, C.first, "comdat name");
630 writeUleb128(Sub.OS, 0, "comdat flags"); // flags for future use
631 writeUleb128(Sub.OS, C.second.size(), "num entries");
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000632 for (const ComdatEntry &Entry : C.second) {
Sam Clegg8518e7d2018-03-01 18:06:39 +0000633 writeU8(Sub.OS, Entry.Kind, "entry kind");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000634 writeUleb128(Sub.OS, Entry.Index, "entry index");
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000635 }
636 }
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000637 Sub.writeTo(OS);
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000638 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000639}
640
641// Create the custom "name" section containing debug symbol names.
642void Writer::createNameSection() {
Sam Clegg93102972018-02-23 05:08:53 +0000643 unsigned NumNames = NumImportedFunctions;
Sam Clegg9f934222018-02-21 18:29:23 +0000644 for (const InputFunction *F : InputFunctions)
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000645 if (!F->getName().empty() || !F->getDebugName().empty())
Sam Clegg1963d712018-01-17 20:19:04 +0000646 ++NumNames;
Sam Cleggc94d3932017-11-17 18:14:09 +0000647
Sam Clegg1963d712018-01-17 20:19:04 +0000648 if (NumNames == 0)
649 return;
Sam Clegg50686852018-01-12 18:35:13 +0000650
Sam Cleggc94d3932017-11-17 18:14:09 +0000651 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name");
652
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000653 SubSection Sub(WASM_NAMES_FUNCTION);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000654 writeUleb128(Sub.OS, NumNames, "name count");
Sam Cleggc94d3932017-11-17 18:14:09 +0000655
Sam Clegg93102972018-02-23 05:08:53 +0000656 // Names must appear in function index order. As it happens ImportedSymbols
657 // and InputFunctions are numbered in order with imported functions coming
Sam Clegg1963d712018-01-17 20:19:04 +0000658 // first.
Sam Clegg93102972018-02-23 05:08:53 +0000659 for (const Symbol *S : ImportedSymbols) {
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000660 if (auto *F = dyn_cast<FunctionSymbol>(S)) {
661 writeUleb128(Sub.OS, F->getFunctionIndex(), "func index");
Sam Clegg37125f02018-11-09 16:57:41 +0000662 writeStr(Sub.OS, toString(*S), "symbol name");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000663 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000664 }
Sam Clegg9f934222018-02-21 18:29:23 +0000665 for (const InputFunction *F : InputFunctions) {
Sam Clegg1963d712018-01-17 20:19:04 +0000666 if (!F->getName().empty()) {
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000667 writeUleb128(Sub.OS, F->getFunctionIndex(), "func index");
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000668 if (!F->getDebugName().empty()) {
669 writeStr(Sub.OS, F->getDebugName(), "symbol name");
670 } else {
Sam Clegg37125f02018-11-09 16:57:41 +0000671 writeStr(Sub.OS, maybeDemangleSymbol(F->getName()), "symbol name");
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000672 }
Sam Clegg1963d712018-01-17 20:19:04 +0000673 }
674 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000675
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000676 Sub.writeTo(Section->getStream());
Sam Cleggc94d3932017-11-17 18:14:09 +0000677}
678
Thomas Lively2a0868f2019-01-17 02:29:41 +0000679void Writer::createProducersSection() {
680 SmallVector<std::pair<std::string, std::string>, 8> Languages;
681 SmallVector<std::pair<std::string, std::string>, 8> Tools;
682 SmallVector<std::pair<std::string, std::string>, 8> SDKs;
683 for (ObjFile *File : Symtab->ObjectFiles) {
684 const WasmProducerInfo &Info = File->getWasmObj()->getProducerInfo();
685 for (auto &Producers : {std::make_pair(&Info.Languages, &Languages),
686 std::make_pair(&Info.Tools, &Tools),
687 std::make_pair(&Info.SDKs, &SDKs)})
688 for (auto &Producer : *Producers.first)
689 if (Producers.second->end() ==
690 std::find_if(Producers.second->begin(), Producers.second->end(),
691 [&](std::pair<std::string, std::string> Seen) {
692 return Seen.first == Producer.first;
693 }))
694 Producers.second->push_back(Producer);
695 }
696 int FieldCount =
697 int(!Languages.empty()) + int(!Tools.empty()) + int(!SDKs.empty());
698 if (FieldCount == 0)
699 return;
700 SyntheticSection *Section =
701 createSyntheticSection(WASM_SEC_CUSTOM, "producers");
702 auto &OS = Section->getStream();
703 writeUleb128(OS, FieldCount, "field count");
704 for (auto &Field :
705 {std::make_pair("language", Languages),
706 std::make_pair("processed-by", Tools), std::make_pair("sdk", SDKs)}) {
707 if (Field.second.empty())
708 continue;
709 writeStr(OS, Field.first, "field name");
710 writeUleb128(OS, Field.second.size(), "number of entries");
711 for (auto &Entry : Field.second) {
712 writeStr(OS, Entry.first, "producer name");
713 writeStr(OS, Entry.second, "producer version");
714 }
715 }
716}
717
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000718void Writer::createTargetFeaturesSection() {
719 if (TargetFeatures.size() == 0)
720 return;
721
722 SmallVector<std::string, 8> Emitted(TargetFeatures.begin(),
723 TargetFeatures.end());
724 std::sort(Emitted.begin(), Emitted.end());
725 SyntheticSection *Section =
726 createSyntheticSection(WASM_SEC_CUSTOM, "target_features");
727 auto &OS = Section->getStream();
728 writeUleb128(OS, Emitted.size(), "feature count");
729 for (auto &Feature : Emitted) {
730 writeU8(OS, WASM_FEATURE_PREFIX_USED, "feature used prefix");
731 writeStr(OS, Feature, "feature name");
732 }
733}
734
Sam Cleggc94d3932017-11-17 18:14:09 +0000735void Writer::writeHeader() {
736 memcpy(Buffer->getBufferStart(), Header.data(), Header.size());
737}
738
739void Writer::writeSections() {
740 uint8_t *Buf = Buffer->getBufferStart();
741 parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); });
742}
743
744// Fix the memory layout of the output binary. This assigns memory offsets
Sam Clegg49ed9262017-12-01 00:53:21 +0000745// to each of the input data sections as well as the explicit stack region.
Sam Clegga0f095e2018-05-03 17:21:53 +0000746// The default memory layout is as follows, from low to high.
747//
Sam Cleggf0d433d2018-02-02 22:59:56 +0000748// - initialized data (starting at Config->GlobalBase)
749// - BSS data (not currently implemented in llvm)
750// - explicit stack (Config->ZStackSize)
751// - heap start / unallocated
Sam Clegga0f095e2018-05-03 17:21:53 +0000752//
753// The --stack-first option means that stack is placed before any static data.
Heejin Ahn4821ebf2018-08-29 21:03:16 +0000754// This can be useful since it means that stack overflow traps immediately
755// rather than overwriting global data, but also increases code size since all
756// static data loads and stores requires larger offsets.
Sam Cleggc94d3932017-11-17 18:14:09 +0000757void Writer::layoutMemory() {
Sam Cleggc94d3932017-11-17 18:14:09 +0000758 createOutputSegments();
759
Sam Clegga0f095e2018-05-03 17:21:53 +0000760 uint32_t MemoryPtr = 0;
761
762 auto PlaceStack = [&]() {
Sam Cleggbfb75342018-11-15 00:37:21 +0000763 if (Config->Relocatable || Config->Shared)
Sam Clegga0f095e2018-05-03 17:21:53 +0000764 return;
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000765 MemoryPtr = alignTo(MemoryPtr, StackAlignment);
766 if (Config->ZStackSize != alignTo(Config->ZStackSize, StackAlignment))
767 error("stack size must be " + Twine(StackAlignment) + "-byte aligned");
Sam Clegga0f095e2018-05-03 17:21:53 +0000768 log("mem: stack size = " + Twine(Config->ZStackSize));
769 log("mem: stack base = " + Twine(MemoryPtr));
770 MemoryPtr += Config->ZStackSize;
Sam Clegg2dad4e22018-11-15 18:15:54 +0000771 auto *SP = cast<DefinedGlobal>(WasmSym::StackPointer);
772 SP->Global->Global.InitExpr.Value.Int32 = MemoryPtr;
Sam Clegga0f095e2018-05-03 17:21:53 +0000773 log("mem: stack top = " + Twine(MemoryPtr));
774 };
775
776 if (Config->StackFirst) {
777 PlaceStack();
778 } else {
779 MemoryPtr = Config->GlobalBase;
780 log("mem: global base = " + Twine(Config->GlobalBase));
781 }
782
783 uint32_t DataStart = MemoryPtr;
784
Sam Cleggf0d433d2018-02-02 22:59:56 +0000785 // Arbitrarily set __dso_handle handle to point to the start of the data
786 // segments.
787 if (WasmSym::DsoHandle)
Sam Clegga0f095e2018-05-03 17:21:53 +0000788 WasmSym::DsoHandle->setVirtualAddress(DataStart);
Sam Cleggf0d433d2018-02-02 22:59:56 +0000789
Sam Cleggbfb75342018-11-15 00:37:21 +0000790 MemAlign = 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000791 for (OutputSegment *Seg : Segments) {
Sam Cleggbfb75342018-11-15 00:37:21 +0000792 MemAlign = std::max(MemAlign, Seg->Alignment);
Sam Clegg622ad042019-01-17 22:09:09 +0000793 MemoryPtr = alignTo(MemoryPtr, 1ULL << Seg->Alignment);
Sam Cleggc94d3932017-11-17 18:14:09 +0000794 Seg->StartVA = MemoryPtr;
Nicholas Wilsona06a3552018-03-14 13:50:20 +0000795 log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", Seg->Name,
796 MemoryPtr, Seg->Size, Seg->Alignment));
Sam Cleggc94d3932017-11-17 18:14:09 +0000797 MemoryPtr += Seg->Size;
798 }
799
Sam Cleggf0d433d2018-02-02 22:59:56 +0000800 // TODO: Add .bss space here.
Sam Clegg37a4a8a2018-02-07 03:04:53 +0000801 if (WasmSym::DataEnd)
802 WasmSym::DataEnd->setVirtualAddress(MemoryPtr);
Sam Cleggf0d433d2018-02-02 22:59:56 +0000803
Sam Clegga0f095e2018-05-03 17:21:53 +0000804 log("mem: static data = " + Twine(MemoryPtr - DataStart));
Sam Cleggc94d3932017-11-17 18:14:09 +0000805
Sam Clegg2dad4e22018-11-15 18:15:54 +0000806 if (Config->Shared) {
807 MemSize = MemoryPtr;
808 return;
809 }
810
Sam Clegga0f095e2018-05-03 17:21:53 +0000811 if (!Config->StackFirst)
812 PlaceStack();
813
814 // Set `__heap_base` to directly follow the end of the stack or global data.
815 // The fact that this comes last means that a malloc/brk implementation
816 // can grow the heap at runtime.
Sam Cleggc94d3932017-11-17 18:14:09 +0000817 if (!Config->Relocatable) {
Sam Cleggf0d433d2018-02-02 22:59:56 +0000818 WasmSym::HeapBase->setVirtualAddress(MemoryPtr);
Nicholas Wilsona06a3552018-03-14 13:50:20 +0000819 log("mem: heap base = " + Twine(MemoryPtr));
Sam Cleggc94d3932017-11-17 18:14:09 +0000820 }
821
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000822 if (Config->InitialMemory != 0) {
823 if (Config->InitialMemory != alignTo(Config->InitialMemory, WasmPageSize))
824 error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned");
825 if (MemoryPtr > Config->InitialMemory)
826 error("initial memory too small, " + Twine(MemoryPtr) + " bytes needed");
827 else
828 MemoryPtr = Config->InitialMemory;
829 }
Sam Cleggbfb75342018-11-15 00:37:21 +0000830 MemSize = MemoryPtr;
831 NumMemoryPages = alignTo(MemoryPtr, WasmPageSize) / WasmPageSize;
Nicholas Wilsona06a3552018-03-14 13:50:20 +0000832 log("mem: total pages = " + Twine(NumMemoryPages));
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000833
834 if (Config->MaxMemory != 0) {
835 if (Config->MaxMemory != alignTo(Config->MaxMemory, WasmPageSize))
836 error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned");
837 if (MemoryPtr > Config->MaxMemory)
838 error("maximum memory too small, " + Twine(MemoryPtr) + " bytes needed");
839 MaxMemoryPages = Config->MaxMemory / WasmPageSize;
840 log("mem: max pages = " + Twine(MaxMemoryPages));
841 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000842}
843
844SyntheticSection *Writer::createSyntheticSection(uint32_t Type,
Sam Cleggc375e4e2018-01-10 19:18:22 +0000845 StringRef Name) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000846 auto Sec = make<SyntheticSection>(Type, Name);
Sam Cleggab2ac292017-12-20 05:14:48 +0000847 log("createSection: " + toString(*Sec));
Sam Cleggc94d3932017-11-17 18:14:09 +0000848 OutputSections.push_back(Sec);
849 return Sec;
850}
851
852void Writer::createSections() {
853 // Known sections
Sam Cleggbfb75342018-11-15 00:37:21 +0000854 if (Config->Pic)
855 createDylinkSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000856 createTypeSection();
857 createImportSection();
858 createFunctionSection();
859 createTableSection();
860 createMemorySection();
861 createGlobalSection();
Heejin Ahne915a712018-12-08 06:17:43 +0000862 createEventSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000863 createExportSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000864 createElemSection();
865 createCodeSection();
866 createDataSection();
Sam Clegg80ba4382018-04-10 16:12:49 +0000867 createCustomSections();
Sam Cleggc94d3932017-11-17 18:14:09 +0000868
869 // Custom sections
Sam Clegg99eb42c2018-02-27 23:58:03 +0000870 if (Config->Relocatable) {
Sam Clegg99eb42c2018-02-27 23:58:03 +0000871 createLinkingSection();
Nicholas Wilson94d3b162018-03-05 12:33:58 +0000872 createRelocSections();
Sam Clegg99eb42c2018-02-27 23:58:03 +0000873 }
Thomas Lively2a0868f2019-01-17 02:29:41 +0000874
Sam Cleggc94d3932017-11-17 18:14:09 +0000875 if (!Config->StripDebug && !Config->StripAll)
876 createNameSection();
877
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000878 if (!Config->StripAll) {
Thomas Lively2a0868f2019-01-17 02:29:41 +0000879 createProducersSection();
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000880 createTargetFeaturesSection();
881 }
Thomas Lively2a0868f2019-01-17 02:29:41 +0000882
Sam Cleggc94d3932017-11-17 18:14:09 +0000883 for (OutputSection *S : OutputSections) {
884 S->setOffset(FileSize);
885 S->finalizeContents();
886 FileSize += S->getSize();
887 }
888}
889
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000890void Writer::calculateTargetFeatures() {
Thomas Lively82de51a2019-03-26 04:11:05 +0000891 SmallSet<std::string, 8> Used;
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000892 SmallSet<std::string, 8> Required;
893 SmallSet<std::string, 8> Disallowed;
894
Thomas Lively82de51a2019-03-26 04:11:05 +0000895 // Only infer used features if user did not specify features
896 bool InferFeatures = !Config->Features.hasValue();
897
898 if (!InferFeatures) {
899 for (auto &Feature : Config->Features.getValue())
900 TargetFeatures.insert(Feature);
901 // No need to read or check features
902 if (!Config->CheckFeatures)
903 return;
904 }
905
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000906 // Find the sets of used, required, and disallowed features
907 for (ObjFile *File : Symtab->ObjectFiles) {
908 for (auto &Feature : File->getWasmObj()->getTargetFeatures()) {
909 switch (Feature.Prefix) {
910 case WASM_FEATURE_PREFIX_USED:
Thomas Lively82de51a2019-03-26 04:11:05 +0000911 Used.insert(Feature.Name);
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000912 break;
913 case WASM_FEATURE_PREFIX_REQUIRED:
Thomas Lively82de51a2019-03-26 04:11:05 +0000914 Used.insert(Feature.Name);
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000915 Required.insert(Feature.Name);
916 break;
917 case WASM_FEATURE_PREFIX_DISALLOWED:
918 Disallowed.insert(Feature.Name);
919 break;
920 default:
921 error("Unrecognized feature policy prefix " +
922 std::to_string(Feature.Prefix));
923 }
924 }
925 }
926
Thomas Lively82de51a2019-03-26 04:11:05 +0000927 if (InferFeatures)
928 TargetFeatures.insert(Used.begin(), Used.end());
929
930 if (!Config->CheckFeatures)
931 return;
932
933 // Validate that used features are allowed in output
934 if (!InferFeatures) {
935 for (auto &Feature : Used) {
936 if (!TargetFeatures.count(Feature))
937 error(Twine("Target feature '") + Feature + "' is not allowed.");
938 }
939 }
940
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000941 // Validate the required and disallowed constraints for each file
942 for (ObjFile *File : Symtab->ObjectFiles) {
943 SmallSet<std::string, 8> ObjectFeatures;
944 for (auto &Feature : File->getWasmObj()->getTargetFeatures()) {
945 if (Feature.Prefix == WASM_FEATURE_PREFIX_DISALLOWED)
946 continue;
947 ObjectFeatures.insert(Feature.Name);
948 if (Disallowed.count(Feature.Name))
Thomas Lively82de51a2019-03-26 04:11:05 +0000949 error(Twine("Target feature '") + Feature.Name +
950 "' is disallowed. Use --no-check-features to suppress.");
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000951 }
952 for (auto &Feature : Required) {
953 if (!ObjectFeatures.count(Feature))
Thomas Lively82de51a2019-03-26 04:11:05 +0000954 error(Twine("Missing required target feature '") + Feature +
955 "'. Use --no-check-features to suppress.");
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000956 }
957 }
958}
959
Sam Cleggc94d3932017-11-17 18:14:09 +0000960void Writer::calculateImports() {
Sam Clegg574d7ce2017-12-15 19:23:49 +0000961 for (Symbol *Sym : Symtab->getSymbols()) {
Sam Clegg93102972018-02-23 05:08:53 +0000962 if (!Sym->isUndefined())
963 continue;
Sam Clegg93102972018-02-23 05:08:53 +0000964 if (Sym->isWeak() && !Config->Relocatable)
Sam Clegg574d7ce2017-12-15 19:23:49 +0000965 continue;
Nicholas Wilsona1e299f2018-04-20 17:18:06 +0000966 if (!Sym->isLive())
967 continue;
Sam Cleggc729c1b2018-05-30 18:07:52 +0000968 if (!Sym->IsUsedInRegularObj)
969 continue;
Sam Clegg492f7522019-03-26 19:46:15 +0000970 // We don't generate imports for data symbols. They however can be imported
971 // as GOT entries.
972 if (isa<DataSymbol>(Sym))
Sam Cleggd425d6b2019-03-12 21:53:23 +0000973 continue;
Sam Cleggc94d3932017-11-17 18:14:09 +0000974
Nicola Zaghene7245b42018-05-15 13:36:20 +0000975 LLVM_DEBUG(dbgs() << "import: " << Sym->getName() << "\n");
Sam Clegg93102972018-02-23 05:08:53 +0000976 ImportedSymbols.emplace_back(Sym);
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000977 if (auto *F = dyn_cast<FunctionSymbol>(Sym))
978 F->setFunctionIndex(NumImportedFunctions++);
Heejin Ahne915a712018-12-08 06:17:43 +0000979 else if (auto *G = dyn_cast<GlobalSymbol>(Sym))
980 G->setGlobalIndex(NumImportedGlobals++);
Sam Clegg93102972018-02-23 05:08:53 +0000981 else
Heejin Ahne915a712018-12-08 06:17:43 +0000982 cast<EventSymbol>(Sym)->setEventIndex(NumImportedEvents++);
Sam Cleggc94d3932017-11-17 18:14:09 +0000983 }
984}
985
Sam Cleggd3052d52018-01-18 23:40:49 +0000986void Writer::calculateExports() {
Sam Clegg93102972018-02-23 05:08:53 +0000987 if (Config->Relocatable)
988 return;
Sam Cleggf0d433d2018-02-02 22:59:56 +0000989
Sam Cleggd6beb322018-05-10 18:10:34 +0000990 if (!Config->Relocatable && !Config->ImportMemory)
991 Exports.push_back(WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0});
992
993 if (!Config->Relocatable && Config->ExportTable)
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000994 Exports.push_back(WasmExport{FunctionTableName, WASM_EXTERNAL_TABLE, 0});
Sam Cleggd6beb322018-05-10 18:10:34 +0000995
996 unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size();
997
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +0000998 for (Symbol *Sym : Symtab->getSymbols()) {
Sam Cleggce004bf2018-06-28 17:04:58 +0000999 if (!Sym->isExported())
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +00001000 continue;
Sam Clegg93102972018-02-23 05:08:53 +00001001 if (!Sym->isLive())
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +00001002 continue;
Sam Clegg93102972018-02-23 05:08:53 +00001003
Sam Cleggd6beb322018-05-10 18:10:34 +00001004 StringRef Name = Sym->getName();
1005 WasmExport Export;
1006 if (auto *F = dyn_cast<DefinedFunction>(Sym)) {
1007 Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()};
1008 } else if (auto *G = dyn_cast<DefinedGlobal>(Sym)) {
Sam Clegg177b4582018-06-07 01:27:07 +00001009 // TODO(sbc): Remove this check once to mutable global proposal is
1010 // implement in all major browsers.
1011 // See: https://github.com/WebAssembly/mutable-global
1012 if (G->getGlobalType()->Mutable) {
1013 // Only the __stack_pointer should ever be create as mutable.
1014 assert(G == WasmSym::StackPointer);
1015 continue;
1016 }
Sam Cleggd6beb322018-05-10 18:10:34 +00001017 Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()};
Heejin Ahne915a712018-12-08 06:17:43 +00001018 } else if (auto *E = dyn_cast<DefinedEvent>(Sym)) {
1019 Export = {Name, WASM_EXTERNAL_EVENT, E->getEventIndex()};
Sam Cleggd6beb322018-05-10 18:10:34 +00001020 } else {
1021 auto *D = cast<DefinedData>(Sym);
Sam Clegg93102972018-02-23 05:08:53 +00001022 DefinedFakeGlobals.emplace_back(D);
Sam Cleggd6beb322018-05-10 18:10:34 +00001023 Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++};
1024 }
1025
Nicola Zaghene7245b42018-05-15 13:36:20 +00001026 LLVM_DEBUG(dbgs() << "Export: " << Name << "\n");
Sam Cleggd6beb322018-05-10 18:10:34 +00001027 Exports.push_back(Export);
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +00001028 }
Sam Clegg93102972018-02-23 05:08:53 +00001029}
1030
1031void Writer::assignSymtab() {
1032 if (!Config->Relocatable)
1033 return;
1034
Sam Cleggd177ab22018-05-04 23:14:42 +00001035 StringMap<uint32_t> SectionSymbolIndices;
1036
Sam Clegg93102972018-02-23 05:08:53 +00001037 unsigned SymbolIndex = SymtabEntries.size();
Sam Cleggd177ab22018-05-04 23:14:42 +00001038
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001039 auto AddSymbol = [&](Symbol *Sym) {
1040 if (auto *S = dyn_cast<SectionSymbol>(Sym)) {
1041 StringRef Name = S->getName();
1042 if (CustomSectionMapping.count(Name) == 0)
1043 return;
Sam Cleggd177ab22018-05-04 23:14:42 +00001044
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001045 auto SSI = SectionSymbolIndices.find(Name);
1046 if (SSI != SectionSymbolIndices.end()) {
1047 Sym->setOutputSymbolIndex(SSI->second);
1048 return;
Sam Cleggd177ab22018-05-04 23:14:42 +00001049 }
1050
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001051 SectionSymbolIndices[Name] = SymbolIndex;
1052 CustomSectionSymbols[Name] = cast<SectionSymbol>(Sym);
Sam Cleggd3052d52018-01-18 23:40:49 +00001053
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001054 Sym->markLive();
1055 }
1056
1057 // (Since this is relocatable output, GC is not performed so symbols must
1058 // be live.)
1059 assert(Sym->isLive());
1060 Sym->setOutputSymbolIndex(SymbolIndex++);
1061 SymtabEntries.emplace_back(Sym);
1062 };
1063
1064 for (Symbol *Sym : Symtab->getSymbols())
Sam Cleggd15a41542019-03-08 21:10:48 +00001065 if (Sym->IsUsedInRegularObj)
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001066 AddSymbol(Sym);
1067
1068 for (ObjFile *File : Symtab->ObjectFiles) {
1069 LLVM_DEBUG(dbgs() << "Local symtab entries: " << File->getName() << "\n");
1070 for (Symbol *Sym : File->getSymbols())
1071 if (Sym->isLocal())
1072 AddSymbol(Sym);
1073 }
Sam Cleggd3052d52018-01-18 23:40:49 +00001074}
1075
Sam Cleggc375e4e2018-01-10 19:18:22 +00001076uint32_t Writer::lookupType(const WasmSignature &Sig) {
Sam Clegg8d027d62018-01-10 20:12:26 +00001077 auto It = TypeIndices.find(Sig);
1078 if (It == TypeIndices.end()) {
Sam Cleggc375e4e2018-01-10 19:18:22 +00001079 error("type not found: " + toString(Sig));
Sam Clegg8d027d62018-01-10 20:12:26 +00001080 return 0;
1081 }
1082 return It->second;
Sam Cleggc375e4e2018-01-10 19:18:22 +00001083}
1084
1085uint32_t Writer::registerType(const WasmSignature &Sig) {
Sam Cleggb8621592017-11-30 01:40:08 +00001086 auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size()));
Sam Cleggc375e4e2018-01-10 19:18:22 +00001087 if (Pair.second) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001088 LLVM_DEBUG(dbgs() << "type " << toString(Sig) << "\n");
Sam Cleggb8621592017-11-30 01:40:08 +00001089 Types.push_back(&Sig);
Sam Cleggc375e4e2018-01-10 19:18:22 +00001090 }
Sam Cleggb8621592017-11-30 01:40:08 +00001091 return Pair.first->second;
1092}
1093
Sam Cleggc94d3932017-11-17 18:14:09 +00001094void Writer::calculateTypes() {
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001095 // The output type section is the union of the following sets:
1096 // 1. Any signature used in the TYPE relocation
1097 // 2. The signatures of all imported functions
1098 // 3. The signatures of all defined functions
Heejin Ahne915a712018-12-08 06:17:43 +00001099 // 4. The signatures of all imported events
1100 // 5. The signatures of all defined events
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001101
Sam Cleggc94d3932017-11-17 18:14:09 +00001102 for (ObjFile *File : Symtab->ObjectFiles) {
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001103 ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
1104 for (uint32_t I = 0; I < Types.size(); I++)
1105 if (File->TypeIsUsed[I])
1106 File->TypeMap[I] = registerType(Types[I]);
Sam Cleggc94d3932017-11-17 18:14:09 +00001107 }
Sam Clegg50686852018-01-12 18:35:13 +00001108
Heejin Ahne915a712018-12-08 06:17:43 +00001109 for (const Symbol *Sym : ImportedSymbols) {
Sam Clegg93102972018-02-23 05:08:53 +00001110 if (auto *F = dyn_cast<FunctionSymbol>(Sym))
Heejin Ahne915a712018-12-08 06:17:43 +00001111 registerType(*F->Signature);
1112 else if (auto *E = dyn_cast<EventSymbol>(Sym))
1113 registerType(*E->Signature);
1114 }
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001115
Sam Clegg9f934222018-02-21 18:29:23 +00001116 for (const InputFunction *F : InputFunctions)
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001117 registerType(F->Signature);
Heejin Ahne915a712018-12-08 06:17:43 +00001118
1119 for (const InputEvent *E : InputEvents)
1120 registerType(E->Signature);
Sam Cleggc94d3932017-11-17 18:14:09 +00001121}
1122
Sam Clegg632c21792019-03-16 01:18:12 +00001123void Writer::processRelocations(InputChunk *Chunk) {
1124 if (!Chunk->Live)
1125 return;
1126 ObjFile *File = Chunk->File;
1127 ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
1128 for (const WasmRelocation &Reloc : Chunk->getRelocations()) {
1129 switch (Reloc.Type) {
1130 case R_WASM_TABLE_INDEX_I32:
1131 case R_WASM_TABLE_INDEX_SLEB: {
1132 FunctionSymbol *Sym = File->getFunctionSymbol(Reloc.Index);
1133 if (Sym->hasTableIndex() || !Sym->hasFunctionIndex())
1134 continue;
1135 Sym->setTableIndex(TableBase + IndirectFunctions.size());
1136 IndirectFunctions.emplace_back(Sym);
1137 break;
1138 }
1139 case R_WASM_TYPE_INDEX_LEB:
1140 // Mark target type as live
1141 File->TypeMap[Reloc.Index] = registerType(Types[Reloc.Index]);
1142 File->TypeIsUsed[Reloc.Index] = true;
1143 break;
Sam Clegg492f7522019-03-26 19:46:15 +00001144 case R_WASM_GLOBAL_INDEX_LEB: {
1145 auto* Sym = File->getSymbols()[Reloc.Index];
1146 if (!isa<GlobalSymbol>(Sym) && !Sym->isInGOT()) {
1147 Sym->setGOTIndex(NumImportedGlobals++);
1148 GOTSymbols.push_back(Sym);
1149 }
1150 }
Sam Clegg632c21792019-03-16 01:18:12 +00001151 }
Sam Clegg0805ec52019-03-28 02:02:07 +00001152
1153 if (!Config->Relocatable) {
1154 switch (Reloc.Type) {
1155 // These relocations types appear the code section.
1156 // They should never appear in code compiled with -fPIC.
1157 case R_WASM_TABLE_INDEX_SLEB:
1158 case R_WASM_MEMORY_ADDR_I32:
1159 case R_WASM_MEMORY_ADDR_LEB: {
1160 auto *Sym = File->getSymbols()[Reloc.Index];
1161 if (Sym->isUndefined() && !Sym->isWeak())
1162 error(File->getName() + ": relocation " +
Sam Clegga9958fc2019-03-28 02:04:31 +00001163 relocTypeToString(Reloc.Type) +
Sam Clegg0805ec52019-03-28 02:02:07 +00001164 " cannot be used againt symbol " + Sym->getName() +
1165 "; recompile with -fPIC");
1166 }
1167 }
1168 }
Sam Clegg632c21792019-03-16 01:18:12 +00001169 }
1170}
1171
Sam Clegg8d146bb2018-01-09 23:56:44 +00001172void Writer::assignIndexes() {
Heejin Ahnaeaab992018-11-19 23:21:25 +00001173 assert(InputFunctions.empty());
1174 uint32_t FunctionIndex = NumImportedFunctions;
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001175 auto AddDefinedFunction = [&](InputFunction *Func) {
1176 if (!Func->Live)
1177 return;
1178 InputFunctions.emplace_back(Func);
Sam Clegge3f3ccf2018-03-12 19:56:23 +00001179 Func->setFunctionIndex(FunctionIndex++);
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001180 };
1181
Nicholas Wilson5639da82018-03-12 15:44:07 +00001182 for (InputFunction *Func : Symtab->SyntheticFunctions)
1183 AddDefinedFunction(Func);
1184
Sam Clegg87e61922018-01-08 23:39:11 +00001185 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001186 LLVM_DEBUG(dbgs() << "Functions: " << File->getName() << "\n");
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001187 for (InputFunction *Func : File->Functions)
1188 AddDefinedFunction(Func);
Sam Clegg8d146bb2018-01-09 23:56:44 +00001189 }
1190
1191 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001192 LLVM_DEBUG(dbgs() << "Handle relocs: " << File->getName() << "\n");
Sam Clegg93102972018-02-23 05:08:53 +00001193 for (InputChunk *Chunk : File->Functions)
Sam Clegg632c21792019-03-16 01:18:12 +00001194 processRelocations(Chunk);
Sam Clegg93102972018-02-23 05:08:53 +00001195 for (InputChunk *Chunk : File->Segments)
Sam Clegg632c21792019-03-16 01:18:12 +00001196 processRelocations(Chunk);
Sam Cleggd177ab22018-05-04 23:14:42 +00001197 for (auto &P : File->CustomSections)
Sam Clegg632c21792019-03-16 01:18:12 +00001198 processRelocations(P);
Sam Clegg93102972018-02-23 05:08:53 +00001199 }
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001200
Heejin Ahnaeaab992018-11-19 23:21:25 +00001201 assert(InputGlobals.empty());
1202 uint32_t GlobalIndex = NumImportedGlobals;
Sam Clegg93102972018-02-23 05:08:53 +00001203 auto AddDefinedGlobal = [&](InputGlobal *Global) {
1204 if (Global->Live) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001205 LLVM_DEBUG(dbgs() << "AddDefinedGlobal: " << GlobalIndex << "\n");
Sam Clegge3f3ccf2018-03-12 19:56:23 +00001206 Global->setGlobalIndex(GlobalIndex++);
Sam Clegg93102972018-02-23 05:08:53 +00001207 InputGlobals.push_back(Global);
1208 }
1209 };
1210
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001211 for (InputGlobal *Global : Symtab->SyntheticGlobals)
1212 AddDefinedGlobal(Global);
Sam Clegg93102972018-02-23 05:08:53 +00001213
1214 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001215 LLVM_DEBUG(dbgs() << "Globals: " << File->getName() << "\n");
Sam Clegg93102972018-02-23 05:08:53 +00001216 for (InputGlobal *Global : File->Globals)
1217 AddDefinedGlobal(Global);
Sam Cleggc94d3932017-11-17 18:14:09 +00001218 }
Heejin Ahne915a712018-12-08 06:17:43 +00001219
1220 assert(InputEvents.empty());
1221 uint32_t EventIndex = NumImportedEvents;
1222 auto AddDefinedEvent = [&](InputEvent *Event) {
1223 if (Event->Live) {
1224 LLVM_DEBUG(dbgs() << "AddDefinedEvent: " << EventIndex << "\n");
1225 Event->setEventIndex(EventIndex++);
1226 InputEvents.push_back(Event);
1227 }
1228 };
1229
1230 for (ObjFile *File : Symtab->ObjectFiles) {
1231 LLVM_DEBUG(dbgs() << "Events: " << File->getName() << "\n");
1232 for (InputEvent *Event : File->Events)
1233 AddDefinedEvent(Event);
1234 }
Sam Cleggc94d3932017-11-17 18:14:09 +00001235}
1236
1237static StringRef getOutputDataSegmentName(StringRef Name) {
Sam Cleggbfb75342018-11-15 00:37:21 +00001238 // With PIC code we currently only support a single data segment since
1239 // we only have a single __memory_base to use as our base address.
1240 if (Config->Pic)
1241 return "data";
Sam Clegg66844762018-05-10 18:23:51 +00001242 if (!Config->MergeDataSegments)
Sam Cleggc94d3932017-11-17 18:14:09 +00001243 return Name;
Rui Ueyama4764b572018-02-28 00:57:28 +00001244 if (Name.startswith(".text."))
1245 return ".text";
1246 if (Name.startswith(".data."))
1247 return ".data";
1248 if (Name.startswith(".bss."))
1249 return ".bss";
Sam Clegg57694c52018-08-08 18:02:55 +00001250 if (Name.startswith(".rodata."))
1251 return ".rodata";
Sam Cleggc94d3932017-11-17 18:14:09 +00001252 return Name;
1253}
1254
1255void Writer::createOutputSegments() {
1256 for (ObjFile *File : Symtab->ObjectFiles) {
1257 for (InputSegment *Segment : File->Segments) {
Sam Clegg447ae402018-02-13 20:29:38 +00001258 if (!Segment->Live)
Sam Clegge0f6fcd2018-01-12 22:25:17 +00001259 continue;
Sam Cleggc94d3932017-11-17 18:14:09 +00001260 StringRef Name = getOutputDataSegmentName(Segment->getName());
1261 OutputSegment *&S = SegmentMap[Name];
1262 if (S == nullptr) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001263 LLVM_DEBUG(dbgs() << "new segment: " << Name << "\n");
Sam Clegg93102972018-02-23 05:08:53 +00001264 S = make<OutputSegment>(Name, Segments.size());
Sam Cleggc94d3932017-11-17 18:14:09 +00001265 Segments.push_back(S);
1266 }
1267 S->addInputSegment(Segment);
Nicola Zaghene7245b42018-05-15 13:36:20 +00001268 LLVM_DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n");
Sam Cleggc94d3932017-11-17 18:14:09 +00001269 }
1270 }
1271}
1272
Sam Clegg50686852018-01-12 18:35:13 +00001273static const int OPCODE_CALL = 0x10;
1274static const int OPCODE_END = 0xb;
1275
1276// Create synthetic "__wasm_call_ctors" function based on ctor functions
1277// in input object.
1278void Writer::createCtorFunction() {
Sam Clegg0e6b42f2019-03-01 22:35:47 +00001279 if (!WasmSym::CallCtors->isLive())
1280 return;
1281
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +00001282 // First write the body's contents to a string.
1283 std::string BodyContent;
Sam Clegg50686852018-01-12 18:35:13 +00001284 {
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +00001285 raw_string_ostream OS(BodyContent);
Sam Clegg50686852018-01-12 18:35:13 +00001286 writeUleb128(OS, 0, "num locals");
Sam Clegg93102972018-02-23 05:08:53 +00001287 for (const WasmInitEntry &F : InitFunctions) {
Sam Clegg50686852018-01-12 18:35:13 +00001288 writeU8(OS, OPCODE_CALL, "CALL");
Sam Clegge3f3ccf2018-03-12 19:56:23 +00001289 writeUleb128(OS, F.Sym->getFunctionIndex(), "function index");
Sam Clegg50686852018-01-12 18:35:13 +00001290 }
1291 writeU8(OS, OPCODE_END, "END");
1292 }
1293
1294 // Once we know the size of the body we can create the final function body
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +00001295 std::string FunctionBody;
1296 {
1297 raw_string_ostream OS(FunctionBody);
1298 writeUleb128(OS, BodyContent.size(), "function size");
1299 OS << BodyContent;
1300 }
Rui Ueyama29abfe42018-02-28 17:43:15 +00001301
Sam Cleggea656472018-10-22 08:35:39 +00001302 ArrayRef<uint8_t> Body = arrayRefFromStringRef(Saver.save(FunctionBody));
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001303 cast<SyntheticFunction>(WasmSym::CallCtors->Function)->setBody(Body);
Sam Clegg50686852018-01-12 18:35:13 +00001304}
1305
1306// Populate InitFunctions vector with init functions from all input objects.
1307// This is then used either when creating the output linking section or to
1308// synthesize the "__wasm_call_ctors" function.
1309void Writer::calculateInitFunctions() {
Sam Clegg61f13b32019-03-02 04:55:02 +00001310 if (!Config->Relocatable && !WasmSym::CallCtors->isLive())
1311 return;
1312
Sam Clegg50686852018-01-12 18:35:13 +00001313 for (ObjFile *File : Symtab->ObjectFiles) {
1314 const WasmLinkingData &L = File->getWasmObj()->linkingData();
Nicholas Wilsoncb81a0c2018-03-02 14:46:54 +00001315 for (const WasmInitFunc &F : L.InitFunctions) {
1316 FunctionSymbol *Sym = File->getFunctionSymbol(F.Symbol);
Sam Clegg0e6b42f2019-03-01 22:35:47 +00001317 assert(Sym->isLive());
Heejin Ahne915a712018-12-08 06:17:43 +00001318 if (*Sym->Signature != WasmSignature{{}, {}})
Nicholas Wilsoncb81a0c2018-03-02 14:46:54 +00001319 error("invalid signature for init func: " + toString(*Sym));
1320 InitFunctions.emplace_back(WasmInitEntry{Sym, F.Priority});
1321 }
Sam Clegg50686852018-01-12 18:35:13 +00001322 }
Rui Ueyamada69b712018-02-28 00:15:59 +00001323
Sam Clegg50686852018-01-12 18:35:13 +00001324 // Sort in order of priority (lowest first) so that they are called
1325 // in the correct order.
Sam Clegg29b8feb2018-02-21 00:34:34 +00001326 std::stable_sort(InitFunctions.begin(), InitFunctions.end(),
Sam Clegg93102972018-02-23 05:08:53 +00001327 [](const WasmInitEntry &L, const WasmInitEntry &R) {
Sam Clegg29b8feb2018-02-21 00:34:34 +00001328 return L.Priority < R.Priority;
1329 });
Sam Clegg50686852018-01-12 18:35:13 +00001330}
1331
Sam Cleggc94d3932017-11-17 18:14:09 +00001332void Writer::run() {
Sam Cleggbfb75342018-11-15 00:37:21 +00001333 if (Config->Relocatable || Config->Pic)
Sam Clegg99eb42c2018-02-27 23:58:03 +00001334 Config->GlobalBase = 0;
1335
Sam Cleggbfb75342018-11-15 00:37:21 +00001336 // For PIC code the table base is assigned dynamically by the loader.
1337 // For non-PIC, we start at 1 so that accessing table index 0 always traps.
1338 if (!Config->Pic)
1339 TableBase = 1;
1340
Thomas Livelyf6f4f842019-03-20 20:26:45 +00001341 log("-- calculateTargetFeatures");
1342 calculateTargetFeatures();
Sam Cleggc94d3932017-11-17 18:14:09 +00001343 log("-- calculateImports");
1344 calculateImports();
Sam Clegg8d146bb2018-01-09 23:56:44 +00001345 log("-- assignIndexes");
1346 assignIndexes();
Sam Clegg50686852018-01-12 18:35:13 +00001347 log("-- calculateInitFunctions");
1348 calculateInitFunctions();
1349 if (!Config->Relocatable)
1350 createCtorFunction();
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001351 log("-- calculateTypes");
1352 calculateTypes();
Sam Clegg93102972018-02-23 05:08:53 +00001353 log("-- layoutMemory");
1354 layoutMemory();
1355 log("-- calculateExports");
1356 calculateExports();
Sam Cleggd177ab22018-05-04 23:14:42 +00001357 log("-- calculateCustomSections");
1358 calculateCustomSections();
Sam Clegg93102972018-02-23 05:08:53 +00001359 log("-- assignSymtab");
1360 assignSymtab();
Sam Cleggc94d3932017-11-17 18:14:09 +00001361
1362 if (errorHandler().Verbose) {
Sam Clegg9f934222018-02-21 18:29:23 +00001363 log("Defined Functions: " + Twine(InputFunctions.size()));
Sam Clegg93102972018-02-23 05:08:53 +00001364 log("Defined Globals : " + Twine(InputGlobals.size()));
Heejin Ahne915a712018-12-08 06:17:43 +00001365 log("Defined Events : " + Twine(InputEvents.size()));
Sam Clegg93102972018-02-23 05:08:53 +00001366 log("Function Imports : " + Twine(NumImportedFunctions));
1367 log("Global Imports : " + Twine(NumImportedGlobals));
Heejin Ahne915a712018-12-08 06:17:43 +00001368 log("Event Imports : " + Twine(NumImportedEvents));
Sam Cleggc94d3932017-11-17 18:14:09 +00001369 for (ObjFile *File : Symtab->ObjectFiles)
1370 File->dumpInfo();
1371 }
1372
Sam Cleggc94d3932017-11-17 18:14:09 +00001373 createHeader();
1374 log("-- createSections");
1375 createSections();
1376
1377 log("-- openFile");
1378 openFile();
1379 if (errorCount())
1380 return;
1381
1382 writeHeader();
1383
1384 log("-- writeSections");
1385 writeSections();
1386 if (errorCount())
1387 return;
1388
1389 if (Error E = Buffer->commit())
1390 fatal("failed to write the output file: " + toString(std::move(E)));
1391}
1392
1393// Open a result file.
1394void Writer::openFile() {
1395 log("writing: " + Config->OutputFile);
Sam Cleggc94d3932017-11-17 18:14:09 +00001396
1397 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1398 FileOutputBuffer::create(Config->OutputFile, FileSize,
1399 FileOutputBuffer::F_executable);
1400
1401 if (!BufferOrErr)
1402 error("failed to open " + Config->OutputFile + ": " +
1403 toString(BufferOrErr.takeError()));
1404 else
1405 Buffer = std::move(*BufferOrErr);
1406}
1407
1408void Writer::createHeader() {
1409 raw_string_ostream OS(Header);
1410 writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic");
1411 writeU32(OS, WasmVersion, "wasm version");
1412 OS.flush();
1413 FileSize += Header.size();
1414}
1415
1416void lld::wasm::writeResult() { Writer().run(); }