blob: 7309bab04148a8b1876a878c9280786806b9985f [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 Clegg09137be2019-04-04 18:40:51 +000068 void createApplyRelocationsFunction();
69 void createCallCtorsFunction();
70
Sam Clegg50686852018-01-12 18:35:13 +000071 void calculateInitFunctions();
Sam Clegg632c21792019-03-16 01:18:12 +000072 void processRelocations(InputChunk *Chunk);
Sam Clegg8d146bb2018-01-09 23:56:44 +000073 void assignIndexes();
Thomas Livelyf6f4f842019-03-20 20:26:45 +000074 void calculateTargetFeatures();
Sam Cleggc94d3932017-11-17 18:14:09 +000075 void calculateImports();
Sam Cleggd3052d52018-01-18 23:40:49 +000076 void calculateExports();
Sam Cleggd177ab22018-05-04 23:14:42 +000077 void calculateCustomSections();
Sam Clegg93102972018-02-23 05:08:53 +000078 void assignSymtab();
Sam Cleggc94d3932017-11-17 18:14:09 +000079 void calculateTypes();
80 void createOutputSegments();
81 void layoutMemory();
82 void createHeader();
83 void createSections();
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +000084 SyntheticSection *createSyntheticSection(uint32_t Type, StringRef Name = "");
Sam Cleggc94d3932017-11-17 18:14:09 +000085
86 // Builtin sections
87 void createTypeSection();
88 void createFunctionSection();
89 void createTableSection();
90 void createGlobalSection();
Heejin Ahne915a712018-12-08 06:17:43 +000091 void createEventSection();
Sam Cleggc94d3932017-11-17 18:14:09 +000092 void createExportSection();
93 void createImportSection();
94 void createMemorySection();
95 void createElemSection();
Sam Cleggc94d3932017-11-17 18:14:09 +000096 void createCodeSection();
97 void createDataSection();
Sam Clegg80ba4382018-04-10 16:12:49 +000098 void createCustomSections();
Sam Cleggc94d3932017-11-17 18:14:09 +000099
100 // Custom sections
Sam Cleggbfb75342018-11-15 00:37:21 +0000101 void createDylinkSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000102 void createRelocSections();
103 void createLinkingSection();
104 void createNameSection();
Thomas Lively2a0868f2019-01-17 02:29:41 +0000105 void createProducersSection();
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000106 void createTargetFeaturesSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000107
108 void writeHeader();
109 void writeSections();
110
111 uint64_t FileSize = 0;
Sam Cleggbfb75342018-11-15 00:37:21 +0000112 uint32_t TableBase = 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000113 uint32_t NumMemoryPages = 0;
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000114 uint32_t MaxMemoryPages = 0;
Sam Cleggbfb75342018-11-15 00:37:21 +0000115 // Memory size and aligment. Written to the "dylink" section
116 // when build with -shared or -pie.
117 uint32_t MemAlign = 0;
118 uint32_t MemSize = 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000119
120 std::vector<const WasmSignature *> Types;
Nicholas Wilson3e3f5fb2018-03-14 15:58:16 +0000121 DenseMap<WasmSignature, int32_t> TypeIndices;
Sam Clegg93102972018-02-23 05:08:53 +0000122 std::vector<const Symbol *> ImportedSymbols;
Sam Clegg492f7522019-03-26 19:46:15 +0000123 std::vector<const Symbol *> GOTSymbols;
Sam Clegg93102972018-02-23 05:08:53 +0000124 unsigned NumImportedFunctions = 0;
125 unsigned NumImportedGlobals = 0;
Heejin Ahne915a712018-12-08 06:17:43 +0000126 unsigned NumImportedEvents = 0;
Sam Cleggd6beb322018-05-10 18:10:34 +0000127 std::vector<WasmExport> Exports;
Sam Clegg93102972018-02-23 05:08:53 +0000128 std::vector<const DefinedData *> DefinedFakeGlobals;
129 std::vector<InputGlobal *> InputGlobals;
Sam Clegg9f934222018-02-21 18:29:23 +0000130 std::vector<InputFunction *> InputFunctions;
Heejin Ahne915a712018-12-08 06:17:43 +0000131 std::vector<InputEvent *> InputEvents;
Sam Cleggdfb0b2c2018-02-14 18:27:59 +0000132 std::vector<const FunctionSymbol *> IndirectFunctions;
Sam Clegg93102972018-02-23 05:08:53 +0000133 std::vector<const Symbol *> SymtabEntries;
134 std::vector<WasmInitEntry> InitFunctions;
Sam Cleggc94d3932017-11-17 18:14:09 +0000135
Sam Clegg80ba4382018-04-10 16:12:49 +0000136 llvm::StringMap<std::vector<InputSection *>> CustomSectionMapping;
Sam Cleggd177ab22018-05-04 23:14:42 +0000137 llvm::StringMap<SectionSymbol *> CustomSectionSymbols;
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000138 llvm::SmallSet<std::string, 8> TargetFeatures;
Sam Clegg80ba4382018-04-10 16:12:49 +0000139
Sam Cleggc94d3932017-11-17 18:14:09 +0000140 // Elements that are used to construct the final output
141 std::string Header;
142 std::vector<OutputSection *> OutputSections;
143
144 std::unique_ptr<FileOutputBuffer> Buffer;
145
146 std::vector<OutputSegment *> Segments;
147 llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap;
148};
149
150} // anonymous namespace
151
Sam Cleggc94d3932017-11-17 18:14:09 +0000152void Writer::createImportSection() {
Sam Clegg492f7522019-03-26 19:46:15 +0000153 uint32_t NumImports = ImportedSymbols.size() + GOTSymbols.size();
Sam Cleggc94d3932017-11-17 18:14:09 +0000154 if (Config->ImportMemory)
155 ++NumImports;
Nicholas Wilsonfc90b302018-03-28 12:53:29 +0000156 if (Config->ImportTable)
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000157 ++NumImports;
Sam Cleggc94d3932017-11-17 18:14:09 +0000158
159 if (NumImports == 0)
160 return;
161
162 SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT);
163 raw_ostream &OS = Section->getStream();
164
165 writeUleb128(OS, NumImports, "import count");
166
Sam Cleggc94d3932017-11-17 18:14:09 +0000167 if (Config->ImportMemory) {
168 WasmImport Import;
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000169 Import.Module = DefaultModule;
Sam Cleggc94d3932017-11-17 18:14:09 +0000170 Import.Field = "memory";
171 Import.Kind = WASM_EXTERNAL_MEMORY;
172 Import.Memory.Flags = 0;
173 Import.Memory.Initial = NumMemoryPages;
Thomas Lively06391f32019-03-29 20:43:49 +0000174 if (MaxMemoryPages != 0 || Config->SharedMemory) {
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000175 Import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX;
176 Import.Memory.Maximum = MaxMemoryPages;
177 }
Derek Schuff786760a2018-11-06 18:02:39 +0000178 if (Config->SharedMemory)
Derek Schuff3bea8bc2018-11-06 17:59:32 +0000179 Import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED;
Sam Cleggc94d3932017-11-17 18:14:09 +0000180 writeImport(OS, Import);
181 }
182
Nicholas Wilsonfc90b302018-03-28 12:53:29 +0000183 if (Config->ImportTable) {
Sam Cleggbfb75342018-11-15 00:37:21 +0000184 uint32_t TableSize = TableBase + IndirectFunctions.size();
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000185 WasmImport Import;
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000186 Import.Module = DefaultModule;
187 Import.Field = FunctionTableName;
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000188 Import.Kind = WASM_EXTERNAL_TABLE;
Thomas Lively25ff8932019-01-08 06:25:55 +0000189 Import.Table.ElemType = WASM_TYPE_FUNCREF;
Sam Clegg748f59cae2018-12-03 22:37:55 +0000190 Import.Table.Limits = {0, TableSize, 0};
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000191 writeImport(OS, Import);
192 }
193
Sam Clegg93102972018-02-23 05:08:53 +0000194 for (const Symbol *Sym : ImportedSymbols) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000195 WasmImport Import;
Dan Gohman9b84eea2019-02-07 22:00:48 +0000196 if (auto *F = dyn_cast<UndefinedFunction>(Sym)) {
197 Import.Field = F->ImportName;
198 Import.Module = F->ImportModule;
199 } else if (auto *G = dyn_cast<UndefinedGlobal>(Sym)) {
200 Import.Field = G->ImportName;
201 Import.Module = G->ImportModule;
202 } else {
203 Import.Field = Sym->getName();
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000204 Import.Module = DefaultModule;
Dan Gohman9b84eea2019-02-07 22:00:48 +0000205 }
Sam Clegg7cc07532019-02-01 02:29:57 +0000206
Sam Clegg93102972018-02-23 05:08:53 +0000207 if (auto *FunctionSym = dyn_cast<FunctionSymbol>(Sym)) {
208 Import.Kind = WASM_EXTERNAL_FUNCTION;
Heejin Ahne915a712018-12-08 06:17:43 +0000209 Import.SigIndex = lookupType(*FunctionSym->Signature);
210 } 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 }
Sam Clegg492f7522019-03-26 19:46:15 +0000221
222 for (const Symbol *Sym : GOTSymbols) {
223 WasmImport Import;
224 Import.Kind = WASM_EXTERNAL_GLOBAL;
225 Import.Global = {WASM_TYPE_I32, true};
226 if (isa<DataSymbol>(Sym))
227 Import.Module = "GOT.mem";
228 else
229 Import.Module = "GOT.func";
230 Import.Field = Sym->getName();
231 writeImport(OS, Import);
232 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000233}
234
235void Writer::createTypeSection() {
236 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE);
237 raw_ostream &OS = Section->getStream();
238 writeUleb128(OS, Types.size(), "type count");
Sam Cleggd451da12017-12-19 19:56:27 +0000239 for (const WasmSignature *Sig : Types)
Sam Cleggc94d3932017-11-17 18:14:09 +0000240 writeSig(OS, *Sig);
Sam Cleggc94d3932017-11-17 18:14:09 +0000241}
242
243void Writer::createFunctionSection() {
Sam Clegg9f934222018-02-21 18:29:23 +0000244 if (InputFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000245 return;
246
247 SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION);
248 raw_ostream &OS = Section->getStream();
249
Sam Clegg9f934222018-02-21 18:29:23 +0000250 writeUleb128(OS, InputFunctions.size(), "function count");
251 for (const InputFunction *Func : InputFunctions)
Sam Cleggc375e4e2018-01-10 19:18:22 +0000252 writeUleb128(OS, lookupType(Func->Signature), "sig index");
Sam Cleggc94d3932017-11-17 18:14:09 +0000253}
254
255void Writer::createMemorySection() {
256 if (Config->ImportMemory)
257 return;
258
259 SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY);
260 raw_ostream &OS = Section->getStream();
261
Thomas Lively06391f32019-03-29 20:43:49 +0000262 bool HasMax = MaxMemoryPages != 0 || Config->SharedMemory;
Sam Cleggc94d3932017-11-17 18:14:09 +0000263 writeUleb128(OS, 1, "memory count");
Derek Schuff786760a2018-11-06 18:02:39 +0000264 unsigned Flags = 0;
265 if (HasMax)
266 Flags |= WASM_LIMITS_FLAG_HAS_MAX;
Derek Schuff3bea8bc2018-11-06 17:59:32 +0000267 if (Config->SharedMemory)
268 Flags |= WASM_LIMITS_FLAG_IS_SHARED;
269 writeUleb128(OS, Flags, "memory limits flags");
Sam Cleggc94d3932017-11-17 18:14:09 +0000270 writeUleb128(OS, NumMemoryPages, "initial pages");
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000271 if (HasMax)
272 writeUleb128(OS, MaxMemoryPages, "max pages");
Sam Cleggc94d3932017-11-17 18:14:09 +0000273}
274
275void Writer::createGlobalSection() {
Sam Clegg93102972018-02-23 05:08:53 +0000276 unsigned NumGlobals = InputGlobals.size() + DefinedFakeGlobals.size();
277 if (NumGlobals == 0)
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000278 return;
279
Sam Cleggc94d3932017-11-17 18:14:09 +0000280 SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL);
281 raw_ostream &OS = Section->getStream();
282
Sam Clegg93102972018-02-23 05:08:53 +0000283 writeUleb128(OS, NumGlobals, "global count");
284 for (const InputGlobal *G : InputGlobals)
285 writeGlobal(OS, G->Global);
286 for (const DefinedData *Sym : DefinedFakeGlobals) {
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000287 WasmGlobal Global;
Sam Clegg93102972018-02-23 05:08:53 +0000288 Global.Type = {WASM_TYPE_I32, false};
Sam Clegg4eedcfc2017-12-05 19:05:45 +0000289 Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
290 Global.InitExpr.Value.Int32 = Sym->getVirtualAddress();
Sam Cleggc94d3932017-11-17 18:14:09 +0000291 writeGlobal(OS, Global);
292 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000293}
294
Heejin Ahne915a712018-12-08 06:17:43 +0000295// The event section contains a list of declared wasm events associated with the
296// module. Currently the only supported event kind is exceptions. A single event
297// entry represents a single event with an event tag. All C++ exceptions are
298// represented by a single event. An event entry in this section contains
299// information on what kind of event it is (e.g. exception) and the type of
300// values contained in a single event object. (In wasm, an event can contain
301// multiple values of primitive types. But for C++ exceptions, we just throw a
302// pointer which is an i32 value (for wasm32 architecture), so the signature of
303// C++ exception is (i32)->(void), because all event types are assumed to have
304// void return type to share WasmSignature with functions.)
305void Writer::createEventSection() {
306 unsigned NumEvents = InputEvents.size();
307 if (NumEvents == 0)
308 return;
309
310 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EVENT);
311 raw_ostream &OS = Section->getStream();
312
313 writeUleb128(OS, NumEvents, "event count");
314 for (InputEvent *E : InputEvents) {
315 E->Event.Type.SigIndex = lookupType(E->Signature);
316 writeEvent(OS, E->Event);
317 }
318}
319
Sam Cleggc94d3932017-11-17 18:14:09 +0000320void Writer::createTableSection() {
Nicholas Wilsonfc90b302018-03-28 12:53:29 +0000321 if (Config->ImportTable)
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000322 return;
323
324 // Always output a table section (or table import), even if there are no
325 // indirect calls. There are two reasons for this:
Sam Cleggfc1a9122017-12-11 22:00:56 +0000326 // 1. For executables it is useful to have an empty table slot at 0
327 // which can be filled with a null function call handler.
328 // 2. If we don't do this, any program that contains a call_indirect but
329 // no address-taken function will fail at validation time since it is
330 // a validation error to include a call_indirect instruction if there
331 // is not table.
Sam Cleggbfb75342018-11-15 00:37:21 +0000332 uint32_t TableSize = TableBase + IndirectFunctions.size();
Sam Cleggfc1a9122017-12-11 22:00:56 +0000333
Sam Cleggc94d3932017-11-17 18:14:09 +0000334 SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE);
335 raw_ostream &OS = Section->getStream();
336
337 writeUleb128(OS, 1, "table count");
Nicholas Wilson874eedd2018-03-27 17:38:51 +0000338 WasmLimits Limits = {WASM_LIMITS_FLAG_HAS_MAX, TableSize, TableSize};
Thomas Lively25ff8932019-01-08 06:25:55 +0000339 writeTableType(OS, WasmTable{WASM_TYPE_FUNCREF, Limits});
Sam Cleggc94d3932017-11-17 18:14:09 +0000340}
341
342void Writer::createExportSection() {
Sam Cleggd6beb322018-05-10 18:10:34 +0000343 if (!Exports.size())
Sam Cleggc94d3932017-11-17 18:14:09 +0000344 return;
345
346 SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT);
347 raw_ostream &OS = Section->getStream();
348
Sam Cleggd6beb322018-05-10 18:10:34 +0000349 writeUleb128(OS, Exports.size(), "export count");
350 for (const WasmExport &Export : Exports)
Sam Clegg74fe0ba2017-12-07 01:51:24 +0000351 writeExport(OS, Export);
Sam Cleggc94d3932017-11-17 18:14:09 +0000352}
353
Sam Cleggd177ab22018-05-04 23:14:42 +0000354void Writer::calculateCustomSections() {
355 log("calculateCustomSections");
356 bool StripDebug = Config->StripDebug || Config->StripAll;
357 for (ObjFile *File : Symtab->ObjectFiles) {
358 for (InputSection *Section : File->CustomSections) {
359 StringRef Name = Section->getName();
360 // These custom sections are known the linker and synthesized rather than
361 // blindly copied
Thomas Lively2a0868f2019-01-17 02:29:41 +0000362 if (Name == "linking" || Name == "name" || Name == "producers" ||
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000363 Name == "target_features" || Name.startswith("reloc."))
Sam Cleggd177ab22018-05-04 23:14:42 +0000364 continue;
365 // .. or it is a debug section
366 if (StripDebug && Name.startswith(".debug_"))
367 continue;
368 CustomSectionMapping[Name].push_back(Section);
369 }
370 }
371}
372
Sam Clegg80ba4382018-04-10 16:12:49 +0000373void Writer::createCustomSections() {
374 log("createCustomSections");
Sam Clegg80ba4382018-04-10 16:12:49 +0000375 for (auto &Pair : CustomSectionMapping) {
376 StringRef Name = Pair.first();
Sam Cleggd177ab22018-05-04 23:14:42 +0000377
378 auto P = CustomSectionSymbols.find(Name);
379 if (P != CustomSectionSymbols.end()) {
380 uint32_t SectionIndex = OutputSections.size();
381 P->second->setOutputSectionIndex(SectionIndex);
382 }
383
Nicola Zaghene7245b42018-05-15 13:36:20 +0000384 LLVM_DEBUG(dbgs() << "createCustomSection: " << Name << "\n");
Sam Clegg80ba4382018-04-10 16:12:49 +0000385 OutputSections.push_back(make<CustomSection>(Name, Pair.second));
386 }
387}
388
Sam Cleggc94d3932017-11-17 18:14:09 +0000389void Writer::createElemSection() {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000390 if (IndirectFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000391 return;
392
393 SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM);
394 raw_ostream &OS = Section->getStream();
395
396 writeUleb128(OS, 1, "segment count");
397 writeUleb128(OS, 0, "table index");
398 WasmInitExpr InitExpr;
Sam Cleggbfb75342018-11-15 00:37:21 +0000399 if (Config->Pic) {
Thomas Lively25ff8932019-01-08 06:25:55 +0000400 InitExpr.Opcode = WASM_OPCODE_GLOBAL_GET;
Sam Clegg2dad4e22018-11-15 18:15:54 +0000401 InitExpr.Value.Global = WasmSym::TableBase->getGlobalIndex();
Sam Cleggbfb75342018-11-15 00:37:21 +0000402 } else {
403 InitExpr.Opcode = WASM_OPCODE_I32_CONST;
404 InitExpr.Value.Int32 = TableBase;
405 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000406 writeInitExpr(OS, InitExpr);
Sam Cleggfc1a9122017-12-11 22:00:56 +0000407 writeUleb128(OS, IndirectFunctions.size(), "elem count");
Sam Cleggc94d3932017-11-17 18:14:09 +0000408
Sam Cleggbfb75342018-11-15 00:37:21 +0000409 uint32_t TableIndex = TableBase;
Sam Cleggdfb0b2c2018-02-14 18:27:59 +0000410 for (const FunctionSymbol *Sym : IndirectFunctions) {
Sam Cleggfc1a9122017-12-11 22:00:56 +0000411 assert(Sym->getTableIndex() == TableIndex);
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000412 writeUleb128(OS, Sym->getFunctionIndex(), "function index");
Sam Cleggfc1a9122017-12-11 22:00:56 +0000413 ++TableIndex;
414 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000415}
416
417void Writer::createCodeSection() {
Sam Clegg9f934222018-02-21 18:29:23 +0000418 if (InputFunctions.empty())
Sam Cleggc94d3932017-11-17 18:14:09 +0000419 return;
420
421 log("createCodeSection");
422
Sam Clegg9f934222018-02-21 18:29:23 +0000423 auto Section = make<CodeSection>(InputFunctions);
Sam Cleggc94d3932017-11-17 18:14:09 +0000424 OutputSections.push_back(Section);
425}
426
427void Writer::createDataSection() {
428 if (!Segments.size())
429 return;
430
431 log("createDataSection");
432 auto Section = make<DataSection>(Segments);
433 OutputSections.push_back(Section);
434}
435
Sam Cleggd451da12017-12-19 19:56:27 +0000436// Create relocations sections in the final output.
Sam Cleggc94d3932017-11-17 18:14:09 +0000437// These are only created when relocatable output is requested.
438void Writer::createRelocSections() {
439 log("createRelocSections");
440 // Don't use iterator here since we are adding to OutputSection
441 size_t OrigSize = OutputSections.size();
Rui Ueyamaffa650a2018-04-24 23:09:57 +0000442 for (size_t I = 0; I < OrigSize; I++) {
443 OutputSection *OSec = OutputSections[I];
Rui Ueyama37254062018-02-28 00:01:31 +0000444 uint32_t Count = OSec->numRelocations();
Sam Cleggc94d3932017-11-17 18:14:09 +0000445 if (!Count)
446 continue;
447
Rui Ueyama37254062018-02-28 00:01:31 +0000448 StringRef Name;
449 if (OSec->Type == WASM_SEC_DATA)
450 Name = "reloc.DATA";
451 else if (OSec->Type == WASM_SEC_CODE)
452 Name = "reloc.CODE";
Sam Cleggd177ab22018-05-04 23:14:42 +0000453 else if (OSec->Type == WASM_SEC_CUSTOM)
454 Name = Saver.save("reloc." + OSec->Name);
Sam Cleggc94d3932017-11-17 18:14:09 +0000455 else
Sam Cleggd177ab22018-05-04 23:14:42 +0000456 llvm_unreachable(
457 "relocations only supported for code, data, or custom sections");
Sam Cleggc94d3932017-11-17 18:14:09 +0000458
Rui Ueyama37254062018-02-28 00:01:31 +0000459 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, Name);
Sam Cleggc94d3932017-11-17 18:14:09 +0000460 raw_ostream &OS = Section->getStream();
Rui Ueyamaffa650a2018-04-24 23:09:57 +0000461 writeUleb128(OS, I, "reloc section");
Sam Cleggc94d3932017-11-17 18:14:09 +0000462 writeUleb128(OS, Count, "reloc count");
Rui Ueyama37254062018-02-28 00:01:31 +0000463 OSec->writeRelocations(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000464 }
465}
466
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000467static uint32_t getWasmFlags(const Symbol *Sym) {
468 uint32_t Flags = 0;
469 if (Sym->isLocal())
470 Flags |= WASM_SYMBOL_BINDING_LOCAL;
471 if (Sym->isWeak())
472 Flags |= WASM_SYMBOL_BINDING_WEAK;
473 if (Sym->isHidden())
474 Flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
475 if (Sym->isUndefined())
476 Flags |= WASM_SYMBOL_UNDEFINED;
Dan Gohman9b84eea2019-02-07 22:00:48 +0000477 if (auto *F = dyn_cast<UndefinedFunction>(Sym)) {
478 if (F->getName() != F->ImportName)
479 Flags |= WASM_SYMBOL_EXPLICIT_NAME;
480 } else if (auto *G = dyn_cast<UndefinedGlobal>(Sym)) {
481 if (G->getName() != G->ImportName)
482 Flags |= WASM_SYMBOL_EXPLICIT_NAME;
483 }
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000484 return Flags;
485}
486
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000487// Some synthetic sections (e.g. "name" and "linking") have subsections.
488// Just like the synthetic sections themselves these need to be created before
489// they can be written out (since they are preceded by their length). This
490// class is used to create subsections and then write them into the stream
491// of the parent section.
492class SubSection {
493public:
494 explicit SubSection(uint32_t Type) : Type(Type) {}
495
496 void writeTo(raw_ostream &To) {
497 OS.flush();
Rui Ueyama67769102018-02-28 03:38:14 +0000498 writeUleb128(To, Type, "subsection type");
499 writeUleb128(To, Body.size(), "subsection size");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000500 To.write(Body.data(), Body.size());
501 }
502
503private:
504 uint32_t Type;
505 std::string Body;
506
507public:
508 raw_string_ostream OS{Body};
509};
510
Sam Cleggbfb75342018-11-15 00:37:21 +0000511// Create the custom "dylink" section containing information for the dynamic
512// linker.
513// See
514// https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md
515void Writer::createDylinkSection() {
516 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "dylink");
517 raw_ostream &OS = Section->getStream();
518
519 writeUleb128(OS, MemSize, "MemSize");
Sam Clegg6320efb2019-01-16 01:43:21 +0000520 writeUleb128(OS, MemAlign, "MemAlign");
Sam Cleggbfb75342018-11-15 00:37:21 +0000521 writeUleb128(OS, IndirectFunctions.size(), "TableSize");
522 writeUleb128(OS, 0, "TableAlign");
Sam Clegga688a422019-03-13 21:29:20 +0000523 writeUleb128(OS, Symtab->SharedFiles.size(), "Needed");
524 for (auto *SO : Symtab->SharedFiles)
525 writeStr(OS, llvm::sys::path::filename(SO->getName()), "so name");
Sam Cleggbfb75342018-11-15 00:37:21 +0000526}
527
Sam Clegg49ed9262017-12-01 00:53:21 +0000528// Create the custom "linking" section containing linker metadata.
Sam Cleggc94d3932017-11-17 18:14:09 +0000529// This is only created when relocatable output is requested.
530void Writer::createLinkingSection() {
531 SyntheticSection *Section =
532 createSyntheticSection(WASM_SEC_CUSTOM, "linking");
533 raw_ostream &OS = Section->getStream();
534
Sam Clegg2b8b1792018-04-26 18:17:21 +0000535 writeUleb128(OS, WasmMetadataVersion, "Version");
Sam Clegg0d0dd392017-12-19 17:09:45 +0000536
Sam Clegg93102972018-02-23 05:08:53 +0000537 if (!SymtabEntries.empty()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000538 SubSection Sub(WASM_SYMBOL_TABLE);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000539 writeUleb128(Sub.OS, SymtabEntries.size(), "num symbols");
540
Sam Clegg93102972018-02-23 05:08:53 +0000541 for (const Symbol *Sym : SymtabEntries) {
542 assert(Sym->isDefined() || Sym->isUndefined());
543 WasmSymbolType Kind = Sym->getWasmType();
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000544 uint32_t Flags = getWasmFlags(Sym);
545
Sam Clegg8518e7d2018-03-01 18:06:39 +0000546 writeU8(Sub.OS, Kind, "sym kind");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000547 writeUleb128(Sub.OS, Flags, "sym flags");
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000548
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000549 if (auto *F = dyn_cast<FunctionSymbol>(Sym)) {
550 writeUleb128(Sub.OS, F->getFunctionIndex(), "index");
Dan Gohman9b84eea2019-02-07 22:00:48 +0000551 if (Sym->isDefined() ||
552 (Flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000553 writeStr(Sub.OS, Sym->getName(), "sym name");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000554 } else if (auto *G = dyn_cast<GlobalSymbol>(Sym)) {
555 writeUleb128(Sub.OS, G->getGlobalIndex(), "index");
Dan Gohman9b84eea2019-02-07 22:00:48 +0000556 if (Sym->isDefined() ||
557 (Flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000558 writeStr(Sub.OS, Sym->getName(), "sym name");
Heejin Ahne915a712018-12-08 06:17:43 +0000559 } else if (auto *E = dyn_cast<EventSymbol>(Sym)) {
560 writeUleb128(Sub.OS, E->getEventIndex(), "index");
Dan Gohman9b84eea2019-02-07 22:00:48 +0000561 if (Sym->isDefined() ||
562 (Flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
Heejin Ahne915a712018-12-08 06:17:43 +0000563 writeStr(Sub.OS, Sym->getName(), "sym name");
Andrea Di Biagio25c1a2f2018-05-05 10:53:31 +0000564 } else if (isa<DataSymbol>(Sym)) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000565 writeStr(Sub.OS, Sym->getName(), "sym name");
Sam Clegg93102972018-02-23 05:08:53 +0000566 if (auto *DataSym = dyn_cast<DefinedData>(Sym)) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000567 writeUleb128(Sub.OS, DataSym->getOutputSegmentIndex(), "index");
568 writeUleb128(Sub.OS, DataSym->getOutputSegmentOffset(),
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000569 "data offset");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000570 writeUleb128(Sub.OS, DataSym->getSize(), "data size");
Sam Clegg93102972018-02-23 05:08:53 +0000571 }
Sam Cleggd177ab22018-05-04 23:14:42 +0000572 } else {
573 auto *S = cast<SectionSymbol>(Sym);
574 writeUleb128(Sub.OS, S->getOutputSectionIndex(), "sym section index");
Sam Clegg93102972018-02-23 05:08:53 +0000575 }
Sam Cleggd3052d52018-01-18 23:40:49 +0000576 }
Rui Ueyama8bfa2a62018-02-28 00:28:07 +0000577
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000578 Sub.writeTo(OS);
Sam Cleggd3052d52018-01-18 23:40:49 +0000579 }
580
Sam Clegg0d0dd392017-12-19 17:09:45 +0000581 if (Segments.size()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000582 SubSection Sub(WASM_SEGMENT_INFO);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000583 writeUleb128(Sub.OS, Segments.size(), "num data segments");
Sam Cleggc94d3932017-11-17 18:14:09 +0000584 for (const OutputSegment *S : Segments) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000585 writeStr(Sub.OS, S->Name, "segment name");
586 writeUleb128(Sub.OS, S->Alignment, "alignment");
587 writeUleb128(Sub.OS, 0, "flags");
Sam Cleggc94d3932017-11-17 18:14:09 +0000588 }
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000589 Sub.writeTo(OS);
Sam Cleggc94d3932017-11-17 18:14:09 +0000590 }
Sam Clegg0d0dd392017-12-19 17:09:45 +0000591
Sam Clegg0d0dd392017-12-19 17:09:45 +0000592 if (!InitFunctions.empty()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000593 SubSection Sub(WASM_INIT_FUNCS);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000594 writeUleb128(Sub.OS, InitFunctions.size(), "num init functions");
Sam Clegg93102972018-02-23 05:08:53 +0000595 for (const WasmInitEntry &F : InitFunctions) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000596 writeUleb128(Sub.OS, F.Priority, "priority");
597 writeUleb128(Sub.OS, F.Sym->getOutputSymbolIndex(), "function index");
Sam Clegg0d0dd392017-12-19 17:09:45 +0000598 }
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000599 Sub.writeTo(OS);
Sam Clegg0d0dd392017-12-19 17:09:45 +0000600 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000601
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000602 struct ComdatEntry {
603 unsigned Kind;
604 uint32_t Index;
605 };
606 std::map<StringRef, std::vector<ComdatEntry>> Comdats;
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000607
Sam Clegg9f934222018-02-21 18:29:23 +0000608 for (const InputFunction *F : InputFunctions) {
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000609 StringRef Comdat = F->getComdatName();
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000610 if (!Comdat.empty())
611 Comdats[Comdat].emplace_back(
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000612 ComdatEntry{WASM_COMDAT_FUNCTION, F->getFunctionIndex()});
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000613 }
614 for (uint32_t I = 0; I < Segments.size(); ++I) {
Sam Cleggf98bccf2018-01-13 15:57:48 +0000615 const auto &InputSegments = Segments[I]->InputSegments;
616 if (InputSegments.empty())
617 continue;
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000618 StringRef Comdat = InputSegments[0]->getComdatName();
Sam Clegga697df522018-01-13 15:59:53 +0000619#ifndef NDEBUG
Sam Cleggf98bccf2018-01-13 15:57:48 +0000620 for (const InputSegment *IS : InputSegments)
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000621 assert(IS->getComdatName() == Comdat);
Sam Clegga697df522018-01-13 15:59:53 +0000622#endif
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000623 if (!Comdat.empty())
624 Comdats[Comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, I});
625 }
626
627 if (!Comdats.empty()) {
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000628 SubSection Sub(WASM_COMDAT_INFO);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000629 writeUleb128(Sub.OS, Comdats.size(), "num comdats");
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000630 for (const auto &C : Comdats) {
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000631 writeStr(Sub.OS, C.first, "comdat name");
632 writeUleb128(Sub.OS, 0, "comdat flags"); // flags for future use
633 writeUleb128(Sub.OS, C.second.size(), "num entries");
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000634 for (const ComdatEntry &Entry : C.second) {
Sam Clegg8518e7d2018-03-01 18:06:39 +0000635 writeU8(Sub.OS, Entry.Kind, "entry kind");
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000636 writeUleb128(Sub.OS, Entry.Index, "entry index");
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000637 }
638 }
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000639 Sub.writeTo(OS);
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000640 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000641}
642
643// Create the custom "name" section containing debug symbol names.
644void Writer::createNameSection() {
Sam Clegg93102972018-02-23 05:08:53 +0000645 unsigned NumNames = NumImportedFunctions;
Sam Clegg9f934222018-02-21 18:29:23 +0000646 for (const InputFunction *F : InputFunctions)
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000647 if (!F->getName().empty() || !F->getDebugName().empty())
Sam Clegg1963d712018-01-17 20:19:04 +0000648 ++NumNames;
Sam Cleggc94d3932017-11-17 18:14:09 +0000649
Sam Clegg1963d712018-01-17 20:19:04 +0000650 if (NumNames == 0)
651 return;
Sam Clegg50686852018-01-12 18:35:13 +0000652
Sam Cleggc94d3932017-11-17 18:14:09 +0000653 SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name");
654
Rui Ueyama19eedbf2018-02-28 00:39:30 +0000655 SubSection Sub(WASM_NAMES_FUNCTION);
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000656 writeUleb128(Sub.OS, NumNames, "name count");
Sam Cleggc94d3932017-11-17 18:14:09 +0000657
Sam Clegg93102972018-02-23 05:08:53 +0000658 // Names must appear in function index order. As it happens ImportedSymbols
659 // and InputFunctions are numbered in order with imported functions coming
Sam Clegg1963d712018-01-17 20:19:04 +0000660 // first.
Sam Clegg93102972018-02-23 05:08:53 +0000661 for (const Symbol *S : ImportedSymbols) {
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000662 if (auto *F = dyn_cast<FunctionSymbol>(S)) {
663 writeUleb128(Sub.OS, F->getFunctionIndex(), "func index");
Sam Clegg37125f02018-11-09 16:57:41 +0000664 writeStr(Sub.OS, toString(*S), "symbol name");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000665 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000666 }
Sam Clegg9f934222018-02-21 18:29:23 +0000667 for (const InputFunction *F : InputFunctions) {
Sam Clegg1963d712018-01-17 20:19:04 +0000668 if (!F->getName().empty()) {
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000669 writeUleb128(Sub.OS, F->getFunctionIndex(), "func index");
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000670 if (!F->getDebugName().empty()) {
671 writeStr(Sub.OS, F->getDebugName(), "symbol name");
672 } else {
Sam Clegg37125f02018-11-09 16:57:41 +0000673 writeStr(Sub.OS, maybeDemangleSymbol(F->getName()), "symbol name");
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000674 }
Sam Clegg1963d712018-01-17 20:19:04 +0000675 }
676 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000677
Rui Ueyama4a1b2bb2018-02-28 00:52:42 +0000678 Sub.writeTo(Section->getStream());
Sam Cleggc94d3932017-11-17 18:14:09 +0000679}
680
Thomas Lively2a0868f2019-01-17 02:29:41 +0000681void Writer::createProducersSection() {
682 SmallVector<std::pair<std::string, std::string>, 8> Languages;
683 SmallVector<std::pair<std::string, std::string>, 8> Tools;
684 SmallVector<std::pair<std::string, std::string>, 8> SDKs;
685 for (ObjFile *File : Symtab->ObjectFiles) {
686 const WasmProducerInfo &Info = File->getWasmObj()->getProducerInfo();
687 for (auto &Producers : {std::make_pair(&Info.Languages, &Languages),
688 std::make_pair(&Info.Tools, &Tools),
689 std::make_pair(&Info.SDKs, &SDKs)})
690 for (auto &Producer : *Producers.first)
691 if (Producers.second->end() ==
Fangrui Song8048fe22019-03-29 16:21:16 +0000692 llvm::find_if(*Producers.second,
693 [&](std::pair<std::string, std::string> Seen) {
694 return Seen.first == Producer.first;
695 }))
Thomas Lively2a0868f2019-01-17 02:29:41 +0000696 Producers.second->push_back(Producer);
697 }
698 int FieldCount =
699 int(!Languages.empty()) + int(!Tools.empty()) + int(!SDKs.empty());
700 if (FieldCount == 0)
701 return;
702 SyntheticSection *Section =
703 createSyntheticSection(WASM_SEC_CUSTOM, "producers");
704 auto &OS = Section->getStream();
705 writeUleb128(OS, FieldCount, "field count");
706 for (auto &Field :
707 {std::make_pair("language", Languages),
708 std::make_pair("processed-by", Tools), std::make_pair("sdk", SDKs)}) {
709 if (Field.second.empty())
710 continue;
711 writeStr(OS, Field.first, "field name");
712 writeUleb128(OS, Field.second.size(), "number of entries");
713 for (auto &Entry : Field.second) {
714 writeStr(OS, Entry.first, "producer name");
715 writeStr(OS, Entry.second, "producer version");
716 }
717 }
718}
719
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000720void Writer::createTargetFeaturesSection() {
Fangrui Song196a4402019-04-18 13:33:29 +0000721 if (TargetFeatures.empty())
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000722 return;
723
724 SmallVector<std::string, 8> Emitted(TargetFeatures.begin(),
725 TargetFeatures.end());
Fangrui Song196a4402019-04-18 13:33:29 +0000726 llvm::sort(Emitted);
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000727 SyntheticSection *Section =
728 createSyntheticSection(WASM_SEC_CUSTOM, "target_features");
729 auto &OS = Section->getStream();
730 writeUleb128(OS, Emitted.size(), "feature count");
731 for (auto &Feature : Emitted) {
732 writeU8(OS, WASM_FEATURE_PREFIX_USED, "feature used prefix");
733 writeStr(OS, Feature, "feature name");
734 }
735}
736
Sam Cleggc94d3932017-11-17 18:14:09 +0000737void Writer::writeHeader() {
738 memcpy(Buffer->getBufferStart(), Header.data(), Header.size());
739}
740
741void Writer::writeSections() {
742 uint8_t *Buf = Buffer->getBufferStart();
743 parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); });
744}
745
746// Fix the memory layout of the output binary. This assigns memory offsets
Sam Clegg49ed9262017-12-01 00:53:21 +0000747// to each of the input data sections as well as the explicit stack region.
Sam Clegga0f095e2018-05-03 17:21:53 +0000748// The default memory layout is as follows, from low to high.
749//
Sam Cleggf0d433d2018-02-02 22:59:56 +0000750// - initialized data (starting at Config->GlobalBase)
751// - BSS data (not currently implemented in llvm)
752// - explicit stack (Config->ZStackSize)
753// - heap start / unallocated
Sam Clegga0f095e2018-05-03 17:21:53 +0000754//
755// The --stack-first option means that stack is placed before any static data.
Heejin Ahn4821ebf2018-08-29 21:03:16 +0000756// This can be useful since it means that stack overflow traps immediately
757// rather than overwriting global data, but also increases code size since all
758// static data loads and stores requires larger offsets.
Sam Cleggc94d3932017-11-17 18:14:09 +0000759void Writer::layoutMemory() {
Sam Cleggc94d3932017-11-17 18:14:09 +0000760 createOutputSegments();
761
Sam Clegga0f095e2018-05-03 17:21:53 +0000762 uint32_t MemoryPtr = 0;
763
764 auto PlaceStack = [&]() {
Sam Cleggbfb75342018-11-15 00:37:21 +0000765 if (Config->Relocatable || Config->Shared)
Sam Clegga0f095e2018-05-03 17:21:53 +0000766 return;
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000767 MemoryPtr = alignTo(MemoryPtr, StackAlignment);
768 if (Config->ZStackSize != alignTo(Config->ZStackSize, StackAlignment))
769 error("stack size must be " + Twine(StackAlignment) + "-byte aligned");
Sam Clegga0f095e2018-05-03 17:21:53 +0000770 log("mem: stack size = " + Twine(Config->ZStackSize));
771 log("mem: stack base = " + Twine(MemoryPtr));
772 MemoryPtr += Config->ZStackSize;
Sam Clegg2dad4e22018-11-15 18:15:54 +0000773 auto *SP = cast<DefinedGlobal>(WasmSym::StackPointer);
774 SP->Global->Global.InitExpr.Value.Int32 = MemoryPtr;
Sam Clegga0f095e2018-05-03 17:21:53 +0000775 log("mem: stack top = " + Twine(MemoryPtr));
776 };
777
778 if (Config->StackFirst) {
779 PlaceStack();
780 } else {
781 MemoryPtr = Config->GlobalBase;
782 log("mem: global base = " + Twine(Config->GlobalBase));
783 }
784
785 uint32_t DataStart = MemoryPtr;
786
Sam Cleggf0d433d2018-02-02 22:59:56 +0000787 // Arbitrarily set __dso_handle handle to point to the start of the data
788 // segments.
789 if (WasmSym::DsoHandle)
Sam Clegga0f095e2018-05-03 17:21:53 +0000790 WasmSym::DsoHandle->setVirtualAddress(DataStart);
Sam Cleggf0d433d2018-02-02 22:59:56 +0000791
Sam Cleggbfb75342018-11-15 00:37:21 +0000792 MemAlign = 0;
Sam Cleggc94d3932017-11-17 18:14:09 +0000793 for (OutputSegment *Seg : Segments) {
Sam Cleggbfb75342018-11-15 00:37:21 +0000794 MemAlign = std::max(MemAlign, Seg->Alignment);
Sam Clegg622ad042019-01-17 22:09:09 +0000795 MemoryPtr = alignTo(MemoryPtr, 1ULL << Seg->Alignment);
Sam Cleggc94d3932017-11-17 18:14:09 +0000796 Seg->StartVA = MemoryPtr;
Nicholas Wilsona06a3552018-03-14 13:50:20 +0000797 log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", Seg->Name,
798 MemoryPtr, Seg->Size, Seg->Alignment));
Sam Cleggc94d3932017-11-17 18:14:09 +0000799 MemoryPtr += Seg->Size;
800 }
801
Sam Cleggf0d433d2018-02-02 22:59:56 +0000802 // TODO: Add .bss space here.
Sam Clegg37a4a8a2018-02-07 03:04:53 +0000803 if (WasmSym::DataEnd)
804 WasmSym::DataEnd->setVirtualAddress(MemoryPtr);
Sam Cleggf0d433d2018-02-02 22:59:56 +0000805
Sam Clegga0f095e2018-05-03 17:21:53 +0000806 log("mem: static data = " + Twine(MemoryPtr - DataStart));
Sam Cleggc94d3932017-11-17 18:14:09 +0000807
Sam Clegg2dad4e22018-11-15 18:15:54 +0000808 if (Config->Shared) {
809 MemSize = MemoryPtr;
810 return;
811 }
812
Sam Clegga0f095e2018-05-03 17:21:53 +0000813 if (!Config->StackFirst)
814 PlaceStack();
815
816 // Set `__heap_base` to directly follow the end of the stack or global data.
817 // The fact that this comes last means that a malloc/brk implementation
818 // can grow the heap at runtime.
Sam Cleggc94d3932017-11-17 18:14:09 +0000819 if (!Config->Relocatable) {
Sam Cleggf0d433d2018-02-02 22:59:56 +0000820 WasmSym::HeapBase->setVirtualAddress(MemoryPtr);
Nicholas Wilsona06a3552018-03-14 13:50:20 +0000821 log("mem: heap base = " + Twine(MemoryPtr));
Sam Cleggc94d3932017-11-17 18:14:09 +0000822 }
823
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000824 if (Config->InitialMemory != 0) {
825 if (Config->InitialMemory != alignTo(Config->InitialMemory, WasmPageSize))
826 error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned");
827 if (MemoryPtr > Config->InitialMemory)
828 error("initial memory too small, " + Twine(MemoryPtr) + " bytes needed");
829 else
830 MemoryPtr = Config->InitialMemory;
831 }
Sam Cleggbfb75342018-11-15 00:37:21 +0000832 MemSize = MemoryPtr;
833 NumMemoryPages = alignTo(MemoryPtr, WasmPageSize) / WasmPageSize;
Nicholas Wilsona06a3552018-03-14 13:50:20 +0000834 log("mem: total pages = " + Twine(NumMemoryPages));
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000835
Thomas Lively06391f32019-03-29 20:43:49 +0000836 // Check max if explicitly supplied or required by shared memory
837 if (Config->MaxMemory != 0 || Config->SharedMemory) {
Nicholas Wilson2eb39c12018-03-14 13:53:58 +0000838 if (Config->MaxMemory != alignTo(Config->MaxMemory, WasmPageSize))
839 error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned");
840 if (MemoryPtr > Config->MaxMemory)
841 error("maximum memory too small, " + Twine(MemoryPtr) + " bytes needed");
842 MaxMemoryPages = Config->MaxMemory / WasmPageSize;
843 log("mem: max pages = " + Twine(MaxMemoryPages));
844 }
Sam Cleggc94d3932017-11-17 18:14:09 +0000845}
846
847SyntheticSection *Writer::createSyntheticSection(uint32_t Type,
Sam Cleggc375e4e2018-01-10 19:18:22 +0000848 StringRef Name) {
Sam Cleggc94d3932017-11-17 18:14:09 +0000849 auto Sec = make<SyntheticSection>(Type, Name);
Sam Cleggab2ac292017-12-20 05:14:48 +0000850 log("createSection: " + toString(*Sec));
Sam Cleggc94d3932017-11-17 18:14:09 +0000851 OutputSections.push_back(Sec);
852 return Sec;
853}
854
855void Writer::createSections() {
856 // Known sections
Sam Cleggbfb75342018-11-15 00:37:21 +0000857 if (Config->Pic)
858 createDylinkSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000859 createTypeSection();
860 createImportSection();
861 createFunctionSection();
862 createTableSection();
863 createMemorySection();
864 createGlobalSection();
Heejin Ahne915a712018-12-08 06:17:43 +0000865 createEventSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000866 createExportSection();
Sam Cleggc94d3932017-11-17 18:14:09 +0000867 createElemSection();
868 createCodeSection();
869 createDataSection();
Sam Clegg80ba4382018-04-10 16:12:49 +0000870 createCustomSections();
Sam Cleggc94d3932017-11-17 18:14:09 +0000871
872 // Custom sections
Sam Clegg99eb42c2018-02-27 23:58:03 +0000873 if (Config->Relocatable) {
Sam Clegg99eb42c2018-02-27 23:58:03 +0000874 createLinkingSection();
Nicholas Wilson94d3b162018-03-05 12:33:58 +0000875 createRelocSections();
Sam Clegg99eb42c2018-02-27 23:58:03 +0000876 }
Thomas Lively2a0868f2019-01-17 02:29:41 +0000877
Sam Cleggc94d3932017-11-17 18:14:09 +0000878 if (!Config->StripDebug && !Config->StripAll)
879 createNameSection();
880
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000881 if (!Config->StripAll) {
Thomas Lively2a0868f2019-01-17 02:29:41 +0000882 createProducersSection();
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000883 createTargetFeaturesSection();
884 }
Thomas Lively2a0868f2019-01-17 02:29:41 +0000885
Sam Cleggc94d3932017-11-17 18:14:09 +0000886 for (OutputSection *S : OutputSections) {
887 S->setOffset(FileSize);
888 S->finalizeContents();
889 FileSize += S->getSize();
890 }
891}
892
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000893void Writer::calculateTargetFeatures() {
Thomas Lively82de51a2019-03-26 04:11:05 +0000894 SmallSet<std::string, 8> Used;
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000895 SmallSet<std::string, 8> Required;
896 SmallSet<std::string, 8> Disallowed;
897
Thomas Lively82de51a2019-03-26 04:11:05 +0000898 // Only infer used features if user did not specify features
899 bool InferFeatures = !Config->Features.hasValue();
900
901 if (!InferFeatures) {
902 for (auto &Feature : Config->Features.getValue())
903 TargetFeatures.insert(Feature);
904 // No need to read or check features
905 if (!Config->CheckFeatures)
906 return;
907 }
908
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000909 // Find the sets of used, required, and disallowed features
910 for (ObjFile *File : Symtab->ObjectFiles) {
911 for (auto &Feature : File->getWasmObj()->getTargetFeatures()) {
912 switch (Feature.Prefix) {
913 case WASM_FEATURE_PREFIX_USED:
Thomas Lively82de51a2019-03-26 04:11:05 +0000914 Used.insert(Feature.Name);
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000915 break;
916 case WASM_FEATURE_PREFIX_REQUIRED:
Thomas Lively82de51a2019-03-26 04:11:05 +0000917 Used.insert(Feature.Name);
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000918 Required.insert(Feature.Name);
919 break;
920 case WASM_FEATURE_PREFIX_DISALLOWED:
921 Disallowed.insert(Feature.Name);
922 break;
923 default:
924 error("Unrecognized feature policy prefix " +
925 std::to_string(Feature.Prefix));
926 }
927 }
928 }
929
Thomas Lively82de51a2019-03-26 04:11:05 +0000930 if (InferFeatures)
931 TargetFeatures.insert(Used.begin(), Used.end());
932
Thomas Lively06391f32019-03-29 20:43:49 +0000933 if (TargetFeatures.count("atomics") && !Config->SharedMemory)
934 error("'atomics' feature is used, so --shared-memory must be used");
935
Thomas Lively82de51a2019-03-26 04:11:05 +0000936 if (!Config->CheckFeatures)
937 return;
938
Thomas Lively06391f32019-03-29 20:43:49 +0000939 if (Disallowed.count("atomics") && Config->SharedMemory)
940 error(
941 "'atomics' feature is disallowed, so --shared-memory must not be used");
942
Thomas Lively82de51a2019-03-26 04:11:05 +0000943 // Validate that used features are allowed in output
944 if (!InferFeatures) {
945 for (auto &Feature : Used) {
946 if (!TargetFeatures.count(Feature))
947 error(Twine("Target feature '") + Feature + "' is not allowed.");
948 }
949 }
950
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000951 // Validate the required and disallowed constraints for each file
952 for (ObjFile *File : Symtab->ObjectFiles) {
953 SmallSet<std::string, 8> ObjectFeatures;
954 for (auto &Feature : File->getWasmObj()->getTargetFeatures()) {
955 if (Feature.Prefix == WASM_FEATURE_PREFIX_DISALLOWED)
956 continue;
957 ObjectFeatures.insert(Feature.Name);
958 if (Disallowed.count(Feature.Name))
Thomas Lively82de51a2019-03-26 04:11:05 +0000959 error(Twine("Target feature '") + Feature.Name +
960 "' is disallowed. Use --no-check-features to suppress.");
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000961 }
962 for (auto &Feature : Required) {
963 if (!ObjectFeatures.count(Feature))
Thomas Lively82de51a2019-03-26 04:11:05 +0000964 error(Twine("Missing required target feature '") + Feature +
965 "'. Use --no-check-features to suppress.");
Thomas Livelyf6f4f842019-03-20 20:26:45 +0000966 }
967 }
968}
969
Sam Cleggc94d3932017-11-17 18:14:09 +0000970void Writer::calculateImports() {
Sam Clegg574d7ce2017-12-15 19:23:49 +0000971 for (Symbol *Sym : Symtab->getSymbols()) {
Sam Clegg93102972018-02-23 05:08:53 +0000972 if (!Sym->isUndefined())
973 continue;
Sam Clegg93102972018-02-23 05:08:53 +0000974 if (Sym->isWeak() && !Config->Relocatable)
Sam Clegg574d7ce2017-12-15 19:23:49 +0000975 continue;
Nicholas Wilsona1e299f2018-04-20 17:18:06 +0000976 if (!Sym->isLive())
977 continue;
Sam Cleggc729c1b2018-05-30 18:07:52 +0000978 if (!Sym->IsUsedInRegularObj)
979 continue;
Sam Clegg492f7522019-03-26 19:46:15 +0000980 // We don't generate imports for data symbols. They however can be imported
981 // as GOT entries.
982 if (isa<DataSymbol>(Sym))
Sam Cleggd425d6b2019-03-12 21:53:23 +0000983 continue;
Sam Cleggc94d3932017-11-17 18:14:09 +0000984
Nicola Zaghene7245b42018-05-15 13:36:20 +0000985 LLVM_DEBUG(dbgs() << "import: " << Sym->getName() << "\n");
Sam Clegg93102972018-02-23 05:08:53 +0000986 ImportedSymbols.emplace_back(Sym);
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000987 if (auto *F = dyn_cast<FunctionSymbol>(Sym))
988 F->setFunctionIndex(NumImportedFunctions++);
Heejin Ahne915a712018-12-08 06:17:43 +0000989 else if (auto *G = dyn_cast<GlobalSymbol>(Sym))
990 G->setGlobalIndex(NumImportedGlobals++);
Sam Clegg93102972018-02-23 05:08:53 +0000991 else
Heejin Ahne915a712018-12-08 06:17:43 +0000992 cast<EventSymbol>(Sym)->setEventIndex(NumImportedEvents++);
Sam Cleggc94d3932017-11-17 18:14:09 +0000993 }
994}
995
Sam Cleggd3052d52018-01-18 23:40:49 +0000996void Writer::calculateExports() {
Sam Clegg93102972018-02-23 05:08:53 +0000997 if (Config->Relocatable)
998 return;
Sam Cleggf0d433d2018-02-02 22:59:56 +0000999
Sam Cleggd6beb322018-05-10 18:10:34 +00001000 if (!Config->Relocatable && !Config->ImportMemory)
1001 Exports.push_back(WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0});
1002
1003 if (!Config->Relocatable && Config->ExportTable)
Heejin Ahna1cc4ea2019-02-04 19:13:46 +00001004 Exports.push_back(WasmExport{FunctionTableName, WASM_EXTERNAL_TABLE, 0});
Sam Cleggd6beb322018-05-10 18:10:34 +00001005
1006 unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size();
1007
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +00001008 for (Symbol *Sym : Symtab->getSymbols()) {
Sam Cleggce004bf2018-06-28 17:04:58 +00001009 if (!Sym->isExported())
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +00001010 continue;
Sam Clegg93102972018-02-23 05:08:53 +00001011 if (!Sym->isLive())
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +00001012 continue;
Sam Clegg93102972018-02-23 05:08:53 +00001013
Sam Cleggd6beb322018-05-10 18:10:34 +00001014 StringRef Name = Sym->getName();
1015 WasmExport Export;
1016 if (auto *F = dyn_cast<DefinedFunction>(Sym)) {
1017 Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()};
1018 } else if (auto *G = dyn_cast<DefinedGlobal>(Sym)) {
Sam Clegg177b4582018-06-07 01:27:07 +00001019 // TODO(sbc): Remove this check once to mutable global proposal is
1020 // implement in all major browsers.
1021 // See: https://github.com/WebAssembly/mutable-global
1022 if (G->getGlobalType()->Mutable) {
1023 // Only the __stack_pointer should ever be create as mutable.
1024 assert(G == WasmSym::StackPointer);
1025 continue;
1026 }
Sam Cleggd6beb322018-05-10 18:10:34 +00001027 Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()};
Heejin Ahne915a712018-12-08 06:17:43 +00001028 } else if (auto *E = dyn_cast<DefinedEvent>(Sym)) {
1029 Export = {Name, WASM_EXTERNAL_EVENT, E->getEventIndex()};
Sam Cleggd6beb322018-05-10 18:10:34 +00001030 } else {
1031 auto *D = cast<DefinedData>(Sym);
Sam Clegg93102972018-02-23 05:08:53 +00001032 DefinedFakeGlobals.emplace_back(D);
Sam Cleggd6beb322018-05-10 18:10:34 +00001033 Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++};
1034 }
1035
Nicola Zaghene7245b42018-05-15 13:36:20 +00001036 LLVM_DEBUG(dbgs() << "Export: " << Name << "\n");
Sam Cleggd6beb322018-05-10 18:10:34 +00001037 Exports.push_back(Export);
Nicholas Wilson4cdf5b82018-03-01 09:38:02 +00001038 }
Sam Clegg93102972018-02-23 05:08:53 +00001039}
1040
1041void Writer::assignSymtab() {
1042 if (!Config->Relocatable)
1043 return;
1044
Sam Cleggd177ab22018-05-04 23:14:42 +00001045 StringMap<uint32_t> SectionSymbolIndices;
1046
Sam Clegg93102972018-02-23 05:08:53 +00001047 unsigned SymbolIndex = SymtabEntries.size();
Sam Cleggd177ab22018-05-04 23:14:42 +00001048
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001049 auto AddSymbol = [&](Symbol *Sym) {
1050 if (auto *S = dyn_cast<SectionSymbol>(Sym)) {
1051 StringRef Name = S->getName();
1052 if (CustomSectionMapping.count(Name) == 0)
1053 return;
Sam Cleggd177ab22018-05-04 23:14:42 +00001054
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001055 auto SSI = SectionSymbolIndices.find(Name);
1056 if (SSI != SectionSymbolIndices.end()) {
1057 Sym->setOutputSymbolIndex(SSI->second);
1058 return;
Sam Cleggd177ab22018-05-04 23:14:42 +00001059 }
1060
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001061 SectionSymbolIndices[Name] = SymbolIndex;
1062 CustomSectionSymbols[Name] = cast<SectionSymbol>(Sym);
Sam Cleggd3052d52018-01-18 23:40:49 +00001063
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001064 Sym->markLive();
1065 }
1066
1067 // (Since this is relocatable output, GC is not performed so symbols must
1068 // be live.)
1069 assert(Sym->isLive());
1070 Sym->setOutputSymbolIndex(SymbolIndex++);
1071 SymtabEntries.emplace_back(Sym);
1072 };
1073
1074 for (Symbol *Sym : Symtab->getSymbols())
Sam Cleggd15a41542019-03-08 21:10:48 +00001075 if (Sym->IsUsedInRegularObj)
Sam Clegg89e4dcb2019-01-30 18:55:15 +00001076 AddSymbol(Sym);
1077
1078 for (ObjFile *File : Symtab->ObjectFiles) {
1079 LLVM_DEBUG(dbgs() << "Local symtab entries: " << File->getName() << "\n");
1080 for (Symbol *Sym : File->getSymbols())
1081 if (Sym->isLocal())
1082 AddSymbol(Sym);
1083 }
Sam Cleggd3052d52018-01-18 23:40:49 +00001084}
1085
Sam Cleggc375e4e2018-01-10 19:18:22 +00001086uint32_t Writer::lookupType(const WasmSignature &Sig) {
Sam Clegg8d027d62018-01-10 20:12:26 +00001087 auto It = TypeIndices.find(Sig);
1088 if (It == TypeIndices.end()) {
Sam Cleggc375e4e2018-01-10 19:18:22 +00001089 error("type not found: " + toString(Sig));
Sam Clegg8d027d62018-01-10 20:12:26 +00001090 return 0;
1091 }
1092 return It->second;
Sam Cleggc375e4e2018-01-10 19:18:22 +00001093}
1094
1095uint32_t Writer::registerType(const WasmSignature &Sig) {
Sam Cleggb8621592017-11-30 01:40:08 +00001096 auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size()));
Sam Cleggc375e4e2018-01-10 19:18:22 +00001097 if (Pair.second) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001098 LLVM_DEBUG(dbgs() << "type " << toString(Sig) << "\n");
Sam Cleggb8621592017-11-30 01:40:08 +00001099 Types.push_back(&Sig);
Sam Cleggc375e4e2018-01-10 19:18:22 +00001100 }
Sam Cleggb8621592017-11-30 01:40:08 +00001101 return Pair.first->second;
1102}
1103
Sam Cleggc94d3932017-11-17 18:14:09 +00001104void Writer::calculateTypes() {
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001105 // The output type section is the union of the following sets:
1106 // 1. Any signature used in the TYPE relocation
1107 // 2. The signatures of all imported functions
1108 // 3. The signatures of all defined functions
Heejin Ahne915a712018-12-08 06:17:43 +00001109 // 4. The signatures of all imported events
1110 // 5. The signatures of all defined events
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001111
Sam Cleggc94d3932017-11-17 18:14:09 +00001112 for (ObjFile *File : Symtab->ObjectFiles) {
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001113 ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
1114 for (uint32_t I = 0; I < Types.size(); I++)
1115 if (File->TypeIsUsed[I])
1116 File->TypeMap[I] = registerType(Types[I]);
Sam Cleggc94d3932017-11-17 18:14:09 +00001117 }
Sam Clegg50686852018-01-12 18:35:13 +00001118
Heejin Ahne915a712018-12-08 06:17:43 +00001119 for (const Symbol *Sym : ImportedSymbols) {
Sam Clegg93102972018-02-23 05:08:53 +00001120 if (auto *F = dyn_cast<FunctionSymbol>(Sym))
Heejin Ahne915a712018-12-08 06:17:43 +00001121 registerType(*F->Signature);
1122 else if (auto *E = dyn_cast<EventSymbol>(Sym))
1123 registerType(*E->Signature);
1124 }
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001125
Sam Clegg9f934222018-02-21 18:29:23 +00001126 for (const InputFunction *F : InputFunctions)
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001127 registerType(F->Signature);
Heejin Ahne915a712018-12-08 06:17:43 +00001128
1129 for (const InputEvent *E : InputEvents)
1130 registerType(E->Signature);
Sam Cleggc94d3932017-11-17 18:14:09 +00001131}
1132
Sam Clegg632c21792019-03-16 01:18:12 +00001133void Writer::processRelocations(InputChunk *Chunk) {
1134 if (!Chunk->Live)
1135 return;
1136 ObjFile *File = Chunk->File;
1137 ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
1138 for (const WasmRelocation &Reloc : Chunk->getRelocations()) {
1139 switch (Reloc.Type) {
1140 case R_WASM_TABLE_INDEX_I32:
Sam Clegga116d912019-04-05 00:35:12 +00001141 case R_WASM_TABLE_INDEX_SLEB:
1142 case R_WASM_TABLE_INDEX_REL_SLEB: {
Sam Clegg632c21792019-03-16 01:18:12 +00001143 FunctionSymbol *Sym = File->getFunctionSymbol(Reloc.Index);
1144 if (Sym->hasTableIndex() || !Sym->hasFunctionIndex())
1145 continue;
1146 Sym->setTableIndex(TableBase + IndirectFunctions.size());
1147 IndirectFunctions.emplace_back(Sym);
1148 break;
1149 }
1150 case R_WASM_TYPE_INDEX_LEB:
1151 // Mark target type as live
1152 File->TypeMap[Reloc.Index] = registerType(Types[Reloc.Index]);
1153 File->TypeIsUsed[Reloc.Index] = true;
1154 break;
Sam Clegg492f7522019-03-26 19:46:15 +00001155 case R_WASM_GLOBAL_INDEX_LEB: {
1156 auto* Sym = File->getSymbols()[Reloc.Index];
1157 if (!isa<GlobalSymbol>(Sym) && !Sym->isInGOT()) {
1158 Sym->setGOTIndex(NumImportedGlobals++);
1159 GOTSymbols.push_back(Sym);
1160 }
1161 }
Sam Clegg632c21792019-03-16 01:18:12 +00001162 }
Sam Clegg09137be2019-04-04 18:40:51 +00001163
1164 if (Config->Pic) {
Sam Clegg09137be2019-04-04 18:40:51 +00001165 switch (Reloc.Type) {
1166 case R_WASM_TABLE_INDEX_SLEB:
1167 case R_WASM_MEMORY_ADDR_SLEB:
1168 case R_WASM_MEMORY_ADDR_LEB: {
Sam Clegg0d9f6092019-04-10 15:06:17 +00001169 // Certain relocation types can't be used when building PIC output, since
1170 // they would require absolute symbol addresses at link time.
Sam Clegg09137be2019-04-04 18:40:51 +00001171 Symbol *Sym = File->getSymbols()[Reloc.Index];
1172 error(toString(File) + ": relocation " +
1173 relocTypeToString(Reloc.Type) + " cannot be used againt symbol " +
1174 toString(*Sym) + "; recompile with -fPIC");
1175 break;
1176 }
Sam Clegg0d9f6092019-04-10 15:06:17 +00001177 case R_WASM_TABLE_INDEX_I32:
1178 case R_WASM_MEMORY_ADDR_I32: {
1179 // These relocation types are only present in the data section and
1180 // will be converted into code by `generateRelocationCode`. This code
1181 // requires the symbols to have GOT entires.
1182 auto* Sym = File->getSymbols()[Reloc.Index];
1183 if (!Sym->isHidden() && !Sym->isLocal() && !Sym->isInGOT()) {
1184 Sym->setGOTIndex(NumImportedGlobals++);
1185 GOTSymbols.push_back(Sym);
1186 }
1187 break;
1188 }
Sam Clegg09137be2019-04-04 18:40:51 +00001189 }
1190 }
Sam Clegg632c21792019-03-16 01:18:12 +00001191 }
1192}
1193
Sam Clegg8d146bb2018-01-09 23:56:44 +00001194void Writer::assignIndexes() {
Heejin Ahnaeaab992018-11-19 23:21:25 +00001195 assert(InputFunctions.empty());
1196 uint32_t FunctionIndex = NumImportedFunctions;
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001197 auto AddDefinedFunction = [&](InputFunction *Func) {
1198 if (!Func->Live)
1199 return;
1200 InputFunctions.emplace_back(Func);
Sam Clegge3f3ccf2018-03-12 19:56:23 +00001201 Func->setFunctionIndex(FunctionIndex++);
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001202 };
1203
Nicholas Wilson5639da82018-03-12 15:44:07 +00001204 for (InputFunction *Func : Symtab->SyntheticFunctions)
1205 AddDefinedFunction(Func);
1206
Sam Clegg87e61922018-01-08 23:39:11 +00001207 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001208 LLVM_DEBUG(dbgs() << "Functions: " << File->getName() << "\n");
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001209 for (InputFunction *Func : File->Functions)
1210 AddDefinedFunction(Func);
Sam Clegg8d146bb2018-01-09 23:56:44 +00001211 }
1212
1213 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001214 LLVM_DEBUG(dbgs() << "Handle relocs: " << File->getName() << "\n");
Sam Clegg93102972018-02-23 05:08:53 +00001215 for (InputChunk *Chunk : File->Functions)
Sam Clegg632c21792019-03-16 01:18:12 +00001216 processRelocations(Chunk);
Sam Clegg93102972018-02-23 05:08:53 +00001217 for (InputChunk *Chunk : File->Segments)
Sam Clegg632c21792019-03-16 01:18:12 +00001218 processRelocations(Chunk);
Sam Cleggd177ab22018-05-04 23:14:42 +00001219 for (auto &P : File->CustomSections)
Sam Clegg632c21792019-03-16 01:18:12 +00001220 processRelocations(P);
Sam Clegg93102972018-02-23 05:08:53 +00001221 }
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001222
Heejin Ahnaeaab992018-11-19 23:21:25 +00001223 assert(InputGlobals.empty());
1224 uint32_t GlobalIndex = NumImportedGlobals;
Sam Clegg93102972018-02-23 05:08:53 +00001225 auto AddDefinedGlobal = [&](InputGlobal *Global) {
1226 if (Global->Live) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001227 LLVM_DEBUG(dbgs() << "AddDefinedGlobal: " << GlobalIndex << "\n");
Sam Clegge3f3ccf2018-03-12 19:56:23 +00001228 Global->setGlobalIndex(GlobalIndex++);
Sam Clegg93102972018-02-23 05:08:53 +00001229 InputGlobals.push_back(Global);
1230 }
1231 };
1232
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001233 for (InputGlobal *Global : Symtab->SyntheticGlobals)
1234 AddDefinedGlobal(Global);
Sam Clegg93102972018-02-23 05:08:53 +00001235
1236 for (ObjFile *File : Symtab->ObjectFiles) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001237 LLVM_DEBUG(dbgs() << "Globals: " << File->getName() << "\n");
Sam Clegg93102972018-02-23 05:08:53 +00001238 for (InputGlobal *Global : File->Globals)
1239 AddDefinedGlobal(Global);
Sam Cleggc94d3932017-11-17 18:14:09 +00001240 }
Heejin Ahne915a712018-12-08 06:17:43 +00001241
1242 assert(InputEvents.empty());
1243 uint32_t EventIndex = NumImportedEvents;
1244 auto AddDefinedEvent = [&](InputEvent *Event) {
1245 if (Event->Live) {
1246 LLVM_DEBUG(dbgs() << "AddDefinedEvent: " << EventIndex << "\n");
1247 Event->setEventIndex(EventIndex++);
1248 InputEvents.push_back(Event);
1249 }
1250 };
1251
1252 for (ObjFile *File : Symtab->ObjectFiles) {
1253 LLVM_DEBUG(dbgs() << "Events: " << File->getName() << "\n");
1254 for (InputEvent *Event : File->Events)
1255 AddDefinedEvent(Event);
1256 }
Sam Cleggc94d3932017-11-17 18:14:09 +00001257}
1258
1259static StringRef getOutputDataSegmentName(StringRef Name) {
Sam Cleggbfb75342018-11-15 00:37:21 +00001260 // With PIC code we currently only support a single data segment since
1261 // we only have a single __memory_base to use as our base address.
1262 if (Config->Pic)
1263 return "data";
Sam Clegg66844762018-05-10 18:23:51 +00001264 if (!Config->MergeDataSegments)
Sam Cleggc94d3932017-11-17 18:14:09 +00001265 return Name;
Rui Ueyama4764b572018-02-28 00:57:28 +00001266 if (Name.startswith(".text."))
1267 return ".text";
1268 if (Name.startswith(".data."))
1269 return ".data";
1270 if (Name.startswith(".bss."))
1271 return ".bss";
Sam Clegg57694c52018-08-08 18:02:55 +00001272 if (Name.startswith(".rodata."))
1273 return ".rodata";
Sam Cleggc94d3932017-11-17 18:14:09 +00001274 return Name;
1275}
1276
1277void Writer::createOutputSegments() {
1278 for (ObjFile *File : Symtab->ObjectFiles) {
1279 for (InputSegment *Segment : File->Segments) {
Sam Clegg447ae402018-02-13 20:29:38 +00001280 if (!Segment->Live)
Sam Clegge0f6fcd2018-01-12 22:25:17 +00001281 continue;
Sam Cleggc94d3932017-11-17 18:14:09 +00001282 StringRef Name = getOutputDataSegmentName(Segment->getName());
1283 OutputSegment *&S = SegmentMap[Name];
1284 if (S == nullptr) {
Nicola Zaghene7245b42018-05-15 13:36:20 +00001285 LLVM_DEBUG(dbgs() << "new segment: " << Name << "\n");
Sam Clegg93102972018-02-23 05:08:53 +00001286 S = make<OutputSegment>(Name, Segments.size());
Sam Cleggc94d3932017-11-17 18:14:09 +00001287 Segments.push_back(S);
1288 }
1289 S->addInputSegment(Segment);
Nicola Zaghene7245b42018-05-15 13:36:20 +00001290 LLVM_DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n");
Sam Cleggc94d3932017-11-17 18:14:09 +00001291 }
1292 }
1293}
1294
Sam Clegg09137be2019-04-04 18:40:51 +00001295// For -shared (PIC) output, we create create a synthetic function which will
1296// apply any relocations to the data segments on startup. This function is
1297// called __wasm_apply_relocs and is added at the very beginning of
1298// __wasm_call_ctors before any of the constructors run.
1299void Writer::createApplyRelocationsFunction() {
1300 LLVM_DEBUG(dbgs() << "createApplyRelocationsFunction\n");
1301 // First write the body's contents to a string.
1302 std::string BodyContent;
1303 {
1304 raw_string_ostream OS(BodyContent);
1305 writeUleb128(OS, 0, "num locals");
1306 for (const OutputSegment *Seg : Segments)
1307 for (const InputSegment *InSeg : Seg->InputSegments)
1308 InSeg->generateRelocationCode(OS);
1309 writeU8(OS, WASM_OPCODE_END, "END");
1310 }
1311
1312 // Once we know the size of the body we can create the final function body
1313 std::string FunctionBody;
1314 {
1315 raw_string_ostream OS(FunctionBody);
1316 writeUleb128(OS, BodyContent.size(), "function size");
1317 OS << BodyContent;
1318 }
1319
1320 ArrayRef<uint8_t> Body = arrayRefFromStringRef(Saver.save(FunctionBody));
1321 cast<SyntheticFunction>(WasmSym::ApplyRelocs->Function)->setBody(Body);
1322}
Sam Clegg50686852018-01-12 18:35:13 +00001323
1324// Create synthetic "__wasm_call_ctors" function based on ctor functions
1325// in input object.
Sam Clegg09137be2019-04-04 18:40:51 +00001326void Writer::createCallCtorsFunction() {
Sam Clegg0e6b42f2019-03-01 22:35:47 +00001327 if (!WasmSym::CallCtors->isLive())
1328 return;
1329
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +00001330 // First write the body's contents to a string.
1331 std::string BodyContent;
Sam Clegg50686852018-01-12 18:35:13 +00001332 {
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +00001333 raw_string_ostream OS(BodyContent);
Sam Clegg50686852018-01-12 18:35:13 +00001334 writeUleb128(OS, 0, "num locals");
Sam Clegg09137be2019-04-04 18:40:51 +00001335 if (Config->Pic) {
1336 writeU8(OS, WASM_OPCODE_CALL, "CALL");
1337 writeUleb128(OS, WasmSym::ApplyRelocs->getFunctionIndex(),
1338 "function index");
1339 }
Sam Clegg93102972018-02-23 05:08:53 +00001340 for (const WasmInitEntry &F : InitFunctions) {
Sam Clegg09137be2019-04-04 18:40:51 +00001341 writeU8(OS, WASM_OPCODE_CALL, "CALL");
Sam Clegge3f3ccf2018-03-12 19:56:23 +00001342 writeUleb128(OS, F.Sym->getFunctionIndex(), "function index");
Sam Clegg50686852018-01-12 18:35:13 +00001343 }
Sam Clegg09137be2019-04-04 18:40:51 +00001344 writeU8(OS, WASM_OPCODE_END, "END");
Sam Clegg50686852018-01-12 18:35:13 +00001345 }
1346
1347 // Once we know the size of the body we can create the final function body
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +00001348 std::string FunctionBody;
1349 {
1350 raw_string_ostream OS(FunctionBody);
1351 writeUleb128(OS, BodyContent.size(), "function size");
1352 OS << BodyContent;
1353 }
Rui Ueyama29abfe42018-02-28 17:43:15 +00001354
Sam Cleggea656472018-10-22 08:35:39 +00001355 ArrayRef<uint8_t> Body = arrayRefFromStringRef(Saver.save(FunctionBody));
Nicholas Wilsonebda41f2018-03-09 16:43:05 +00001356 cast<SyntheticFunction>(WasmSym::CallCtors->Function)->setBody(Body);
Sam Clegg50686852018-01-12 18:35:13 +00001357}
1358
1359// Populate InitFunctions vector with init functions from all input objects.
1360// This is then used either when creating the output linking section or to
1361// synthesize the "__wasm_call_ctors" function.
1362void Writer::calculateInitFunctions() {
Sam Clegg61f13b32019-03-02 04:55:02 +00001363 if (!Config->Relocatable && !WasmSym::CallCtors->isLive())
1364 return;
1365
Sam Clegg50686852018-01-12 18:35:13 +00001366 for (ObjFile *File : Symtab->ObjectFiles) {
1367 const WasmLinkingData &L = File->getWasmObj()->linkingData();
Nicholas Wilsoncb81a0c2018-03-02 14:46:54 +00001368 for (const WasmInitFunc &F : L.InitFunctions) {
1369 FunctionSymbol *Sym = File->getFunctionSymbol(F.Symbol);
Sam Clegg0e6b42f2019-03-01 22:35:47 +00001370 assert(Sym->isLive());
Heejin Ahne915a712018-12-08 06:17:43 +00001371 if (*Sym->Signature != WasmSignature{{}, {}})
Nicholas Wilsoncb81a0c2018-03-02 14:46:54 +00001372 error("invalid signature for init func: " + toString(*Sym));
1373 InitFunctions.emplace_back(WasmInitEntry{Sym, F.Priority});
1374 }
Sam Clegg50686852018-01-12 18:35:13 +00001375 }
Rui Ueyamada69b712018-02-28 00:15:59 +00001376
Sam Clegg50686852018-01-12 18:35:13 +00001377 // Sort in order of priority (lowest first) so that they are called
1378 // in the correct order.
Sam Clegg29b8feb2018-02-21 00:34:34 +00001379 std::stable_sort(InitFunctions.begin(), InitFunctions.end(),
Sam Clegg93102972018-02-23 05:08:53 +00001380 [](const WasmInitEntry &L, const WasmInitEntry &R) {
Sam Clegg29b8feb2018-02-21 00:34:34 +00001381 return L.Priority < R.Priority;
1382 });
Sam Clegg50686852018-01-12 18:35:13 +00001383}
1384
Sam Cleggc94d3932017-11-17 18:14:09 +00001385void Writer::run() {
Sam Cleggbfb75342018-11-15 00:37:21 +00001386 if (Config->Relocatable || Config->Pic)
Sam Clegg99eb42c2018-02-27 23:58:03 +00001387 Config->GlobalBase = 0;
1388
Sam Cleggbfb75342018-11-15 00:37:21 +00001389 // For PIC code the table base is assigned dynamically by the loader.
1390 // For non-PIC, we start at 1 so that accessing table index 0 always traps.
1391 if (!Config->Pic)
1392 TableBase = 1;
1393
Thomas Livelyf6f4f842019-03-20 20:26:45 +00001394 log("-- calculateTargetFeatures");
1395 calculateTargetFeatures();
Sam Cleggc94d3932017-11-17 18:14:09 +00001396 log("-- calculateImports");
1397 calculateImports();
Sam Clegg8d146bb2018-01-09 23:56:44 +00001398 log("-- assignIndexes");
1399 assignIndexes();
Sam Clegg50686852018-01-12 18:35:13 +00001400 log("-- calculateInitFunctions");
1401 calculateInitFunctions();
Sam Clegg8f6d2de2018-01-31 23:48:14 +00001402 log("-- calculateTypes");
1403 calculateTypes();
Sam Clegg93102972018-02-23 05:08:53 +00001404 log("-- layoutMemory");
1405 layoutMemory();
Sam Clegg09137be2019-04-04 18:40:51 +00001406 if (!Config->Relocatable) {
1407 if (Config->Pic)
1408 createApplyRelocationsFunction();
1409 createCallCtorsFunction();
1410 }
Sam Clegg93102972018-02-23 05:08:53 +00001411 log("-- calculateExports");
1412 calculateExports();
Sam Cleggd177ab22018-05-04 23:14:42 +00001413 log("-- calculateCustomSections");
1414 calculateCustomSections();
Sam Clegg93102972018-02-23 05:08:53 +00001415 log("-- assignSymtab");
1416 assignSymtab();
Sam Cleggc94d3932017-11-17 18:14:09 +00001417
1418 if (errorHandler().Verbose) {
Sam Clegg9f934222018-02-21 18:29:23 +00001419 log("Defined Functions: " + Twine(InputFunctions.size()));
Sam Clegg93102972018-02-23 05:08:53 +00001420 log("Defined Globals : " + Twine(InputGlobals.size()));
Heejin Ahne915a712018-12-08 06:17:43 +00001421 log("Defined Events : " + Twine(InputEvents.size()));
Sam Clegg93102972018-02-23 05:08:53 +00001422 log("Function Imports : " + Twine(NumImportedFunctions));
1423 log("Global Imports : " + Twine(NumImportedGlobals));
Heejin Ahne915a712018-12-08 06:17:43 +00001424 log("Event Imports : " + Twine(NumImportedEvents));
Sam Cleggc94d3932017-11-17 18:14:09 +00001425 for (ObjFile *File : Symtab->ObjectFiles)
1426 File->dumpInfo();
1427 }
1428
Sam Cleggc94d3932017-11-17 18:14:09 +00001429 createHeader();
1430 log("-- createSections");
1431 createSections();
1432
1433 log("-- openFile");
1434 openFile();
1435 if (errorCount())
1436 return;
1437
1438 writeHeader();
1439
1440 log("-- writeSections");
1441 writeSections();
1442 if (errorCount())
1443 return;
1444
1445 if (Error E = Buffer->commit())
1446 fatal("failed to write the output file: " + toString(std::move(E)));
1447}
1448
1449// Open a result file.
1450void Writer::openFile() {
1451 log("writing: " + Config->OutputFile);
Sam Cleggc94d3932017-11-17 18:14:09 +00001452
1453 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1454 FileOutputBuffer::create(Config->OutputFile, FileSize,
1455 FileOutputBuffer::F_executable);
1456
1457 if (!BufferOrErr)
1458 error("failed to open " + Config->OutputFile + ": " +
1459 toString(BufferOrErr.takeError()));
1460 else
1461 Buffer = std::move(*BufferOrErr);
1462}
1463
1464void Writer::createHeader() {
1465 raw_string_ostream OS(Header);
1466 writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic");
1467 writeU32(OS, WasmVersion, "wasm version");
1468 OS.flush();
1469 FileSize += Header.size();
1470}
1471
1472void lld::wasm::writeResult() { Writer().run(); }