Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1 | //===- Writer.cpp ---------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "Writer.h" |
| 11 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/DenseSet.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 13 | #include "Config.h" |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 14 | #include "InputChunks.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 15 | #include "OutputSections.h" |
| 16 | #include "OutputSegment.h" |
| 17 | #include "SymbolTable.h" |
| 18 | #include "WriterUtils.h" |
| 19 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 2017d52 | 2017-11-28 20:39:17 +0000 | [diff] [blame] | 20 | #include "lld/Common/Memory.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 21 | #include "lld/Common/Threads.h" |
| 22 | #include "llvm/Support/FileOutputBuffer.h" |
| 23 | #include "llvm/Support/Format.h" |
| 24 | #include "llvm/Support/FormatVariadic.h" |
| 25 | #include "llvm/Support/LEB128.h" |
| 26 | |
| 27 | #include <cstdarg> |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 28 | #include <map> |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 29 | |
| 30 | #define DEBUG_TYPE "lld" |
| 31 | |
| 32 | using namespace llvm; |
| 33 | using namespace llvm::wasm; |
| 34 | using namespace lld; |
| 35 | using namespace lld::wasm; |
| 36 | |
| 37 | static constexpr int kStackAlignment = 16; |
Sam Clegg | 48bbd63 | 2018-01-24 21:37:30 +0000 | [diff] [blame] | 38 | static constexpr int kInitialTableOffset = 1; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 39 | |
| 40 | namespace { |
| 41 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 42 | // Traits for using WasmSignature in a DenseMap. |
| 43 | struct WasmSignatureDenseMapInfo { |
| 44 | static WasmSignature getEmptyKey() { |
| 45 | WasmSignature Sig; |
| 46 | Sig.ReturnType = 1; |
| 47 | return Sig; |
| 48 | } |
| 49 | static WasmSignature getTombstoneKey() { |
| 50 | WasmSignature Sig; |
| 51 | Sig.ReturnType = 2; |
| 52 | return Sig; |
| 53 | } |
| 54 | static unsigned getHashValue(const WasmSignature &Sig) { |
| 55 | uintptr_t Value = 0; |
| 56 | Value += DenseMapInfo<int32_t>::getHashValue(Sig.ReturnType); |
| 57 | for (int32_t Param : Sig.ParamTypes) |
| 58 | Value += DenseMapInfo<int32_t>::getHashValue(Param); |
| 59 | return Value; |
| 60 | } |
| 61 | static bool isEqual(const WasmSignature &LHS, const WasmSignature &RHS) { |
| 62 | return LHS == RHS; |
| 63 | } |
| 64 | }; |
| 65 | |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 66 | // A Wasm export to be written into the export section. |
| 67 | struct WasmExportEntry { |
Sam Clegg | 811236c | 2018-01-19 03:31:07 +0000 | [diff] [blame] | 68 | const Symbol *Sym; |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 69 | StringRef FieldName; // may not match the Symbol name |
| 70 | }; |
| 71 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 72 | // The writer writes a SymbolTable result to a file. |
| 73 | class Writer { |
| 74 | public: |
| 75 | void run(); |
| 76 | |
| 77 | private: |
| 78 | void openFile(); |
| 79 | |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 80 | uint32_t lookupType(const WasmSignature &Sig); |
| 81 | uint32_t registerType(const WasmSignature &Sig); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 82 | void createCtorFunction(); |
| 83 | void calculateInitFunctions(); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 84 | void assignIndexes(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 85 | void calculateImports(); |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 86 | void calculateExports(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 87 | void calculateTypes(); |
| 88 | void createOutputSegments(); |
| 89 | void layoutMemory(); |
| 90 | void createHeader(); |
| 91 | void createSections(); |
| 92 | SyntheticSection *createSyntheticSection(uint32_t Type, |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 93 | StringRef Name = ""); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 94 | |
| 95 | // Builtin sections |
| 96 | void createTypeSection(); |
| 97 | void createFunctionSection(); |
| 98 | void createTableSection(); |
| 99 | void createGlobalSection(); |
| 100 | void createExportSection(); |
| 101 | void createImportSection(); |
| 102 | void createMemorySection(); |
| 103 | void createElemSection(); |
| 104 | void createStartSection(); |
| 105 | void createCodeSection(); |
| 106 | void createDataSection(); |
| 107 | |
| 108 | // Custom sections |
| 109 | void createRelocSections(); |
| 110 | void createLinkingSection(); |
| 111 | void createNameSection(); |
| 112 | |
| 113 | void writeHeader(); |
| 114 | void writeSections(); |
| 115 | |
| 116 | uint64_t FileSize = 0; |
| 117 | uint32_t DataSize = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 118 | uint32_t NumMemoryPages = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 119 | |
| 120 | std::vector<const WasmSignature *> Types; |
| 121 | DenseMap<WasmSignature, int32_t, WasmSignatureDenseMapInfo> TypeIndices; |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 122 | std::vector<const Symbol *> ImportedFunctions; |
| 123 | std::vector<const Symbol *> ImportedGlobals; |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 124 | std::vector<WasmExportEntry> ExportedSymbols; |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 125 | std::vector<const Symbol *> DefinedGlobals; |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 126 | std::vector<InputFunction *> DefinedFunctions; |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 127 | std::vector<const Symbol *> IndirectFunctions; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 128 | std::vector<WasmInitFunc> InitFunctions; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 129 | |
| 130 | // Elements that are used to construct the final output |
| 131 | std::string Header; |
| 132 | std::vector<OutputSection *> OutputSections; |
| 133 | |
| 134 | std::unique_ptr<FileOutputBuffer> Buffer; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 135 | std::unique_ptr<SyntheticFunction> CtorFunction; |
| 136 | std::string CtorFunctionBody; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 137 | |
| 138 | std::vector<OutputSegment *> Segments; |
| 139 | llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap; |
| 140 | }; |
| 141 | |
| 142 | } // anonymous namespace |
| 143 | |
| 144 | static void debugPrint(const char *fmt, ...) { |
| 145 | if (!errorHandler().Verbose) |
| 146 | return; |
| 147 | fprintf(stderr, "lld: "); |
| 148 | va_list ap; |
| 149 | va_start(ap, fmt); |
| 150 | vfprintf(stderr, fmt, ap); |
| 151 | va_end(ap); |
| 152 | } |
| 153 | |
| 154 | void Writer::createImportSection() { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 155 | uint32_t NumImports = ImportedFunctions.size() + ImportedGlobals.size(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 156 | if (Config->ImportMemory) |
| 157 | ++NumImports; |
| 158 | |
| 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 Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 167 | for (const Symbol *Sym : ImportedFunctions) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 168 | WasmImport Import; |
| 169 | Import.Module = "env"; |
| 170 | Import.Field = Sym->getName(); |
| 171 | Import.Kind = WASM_EXTERNAL_FUNCTION; |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 172 | Import.SigIndex = lookupType(Sym->getFunctionType()); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 173 | writeImport(OS, Import); |
| 174 | } |
| 175 | |
| 176 | if (Config->ImportMemory) { |
| 177 | WasmImport Import; |
| 178 | Import.Module = "env"; |
| 179 | Import.Field = "memory"; |
| 180 | Import.Kind = WASM_EXTERNAL_MEMORY; |
| 181 | Import.Memory.Flags = 0; |
| 182 | Import.Memory.Initial = NumMemoryPages; |
| 183 | writeImport(OS, Import); |
| 184 | } |
| 185 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 186 | for (const Symbol *Sym : ImportedGlobals) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 187 | WasmImport Import; |
| 188 | Import.Module = "env"; |
| 189 | Import.Field = Sym->getName(); |
| 190 | Import.Kind = WASM_EXTERNAL_GLOBAL; |
| 191 | Import.Global.Mutable = false; |
Sam Clegg | d451da1 | 2017-12-19 19:56:27 +0000 | [diff] [blame] | 192 | Import.Global.Type = WASM_TYPE_I32; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 193 | writeImport(OS, Import); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void Writer::createTypeSection() { |
| 198 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE); |
| 199 | raw_ostream &OS = Section->getStream(); |
| 200 | writeUleb128(OS, Types.size(), "type count"); |
Sam Clegg | d451da1 | 2017-12-19 19:56:27 +0000 | [diff] [blame] | 201 | for (const WasmSignature *Sig : Types) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 202 | writeSig(OS, *Sig); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void Writer::createFunctionSection() { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 206 | if (DefinedFunctions.empty()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 207 | return; |
| 208 | |
| 209 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION); |
| 210 | raw_ostream &OS = Section->getStream(); |
| 211 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 212 | writeUleb128(OS, DefinedFunctions.size(), "function count"); |
| 213 | for (const InputFunction *Func : DefinedFunctions) |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 214 | writeUleb128(OS, lookupType(Func->Signature), "sig index"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void Writer::createMemorySection() { |
| 218 | if (Config->ImportMemory) |
| 219 | return; |
| 220 | |
| 221 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY); |
| 222 | raw_ostream &OS = Section->getStream(); |
| 223 | |
| 224 | writeUleb128(OS, 1, "memory count"); |
| 225 | writeUleb128(OS, 0, "memory limits flags"); |
| 226 | writeUleb128(OS, NumMemoryPages, "initial pages"); |
| 227 | } |
| 228 | |
| 229 | void Writer::createGlobalSection() { |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 230 | if (DefinedGlobals.empty()) |
| 231 | return; |
| 232 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 233 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL); |
| 234 | raw_ostream &OS = Section->getStream(); |
| 235 | |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 236 | writeUleb128(OS, DefinedGlobals.size(), "global count"); |
| 237 | for (const Symbol *Sym : DefinedGlobals) { |
Sam Clegg | 4eedcfc | 2017-12-05 19:05:45 +0000 | [diff] [blame] | 238 | WasmGlobal Global; |
Sam Clegg | 1a9b7b9 | 2018-01-31 19:54:34 +0000 | [diff] [blame] | 239 | Global.Type.Type = WASM_TYPE_I32; |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 240 | Global.Type.Mutable = Sym == WasmSym::StackPointer; |
Sam Clegg | 4eedcfc | 2017-12-05 19:05:45 +0000 | [diff] [blame] | 241 | Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST; |
| 242 | Global.InitExpr.Value.Int32 = Sym->getVirtualAddress(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 243 | writeGlobal(OS, Global); |
| 244 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void Writer::createTableSection() { |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 248 | // Always output a table section, even if there are no indirect calls. |
| 249 | // There are two reasons for this: |
| 250 | // 1. For executables it is useful to have an empty table slot at 0 |
| 251 | // which can be filled with a null function call handler. |
| 252 | // 2. If we don't do this, any program that contains a call_indirect but |
| 253 | // no address-taken function will fail at validation time since it is |
| 254 | // a validation error to include a call_indirect instruction if there |
| 255 | // is not table. |
Sam Clegg | 48bbd63 | 2018-01-24 21:37:30 +0000 | [diff] [blame] | 256 | uint32_t TableSize = kInitialTableOffset + IndirectFunctions.size(); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 257 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 258 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE); |
| 259 | raw_ostream &OS = Section->getStream(); |
| 260 | |
| 261 | writeUleb128(OS, 1, "table count"); |
| 262 | writeSleb128(OS, WASM_TYPE_ANYFUNC, "table type"); |
| 263 | writeUleb128(OS, WASM_LIMITS_FLAG_HAS_MAX, "table flags"); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 264 | writeUleb128(OS, TableSize, "table initial size"); |
| 265 | writeUleb128(OS, TableSize, "table max size"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void Writer::createExportSection() { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 269 | bool ExportMemory = !Config->Relocatable && !Config->ImportMemory; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 270 | |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 271 | uint32_t NumExports = (ExportMemory ? 1 : 0) + ExportedSymbols.size(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 272 | if (!NumExports) |
| 273 | return; |
| 274 | |
| 275 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT); |
| 276 | raw_ostream &OS = Section->getStream(); |
| 277 | |
| 278 | writeUleb128(OS, NumExports, "export count"); |
| 279 | |
| 280 | if (ExportMemory) { |
| 281 | WasmExport MemoryExport; |
| 282 | MemoryExport.Name = "memory"; |
| 283 | MemoryExport.Kind = WASM_EXTERNAL_MEMORY; |
| 284 | MemoryExport.Index = 0; |
| 285 | writeExport(OS, MemoryExport); |
| 286 | } |
| 287 | |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 288 | for (const WasmExportEntry &E : ExportedSymbols) { |
Sam Clegg | 811236c | 2018-01-19 03:31:07 +0000 | [diff] [blame] | 289 | DEBUG(dbgs() << "Export: " << E.Sym->getName() << "\n"); |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 290 | WasmExport Export; |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 291 | Export.Name = E.FieldName; |
Sam Clegg | 811236c | 2018-01-19 03:31:07 +0000 | [diff] [blame] | 292 | Export.Index = E.Sym->getOutputIndex(); |
| 293 | if (E.Sym->isFunction()) |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 294 | Export.Kind = WASM_EXTERNAL_FUNCTION; |
| 295 | else |
| 296 | Export.Kind = WASM_EXTERNAL_GLOBAL; |
| 297 | writeExport(OS, Export); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | void Writer::createStartSection() {} |
| 302 | |
| 303 | void Writer::createElemSection() { |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 304 | if (IndirectFunctions.empty()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 305 | return; |
| 306 | |
| 307 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM); |
| 308 | raw_ostream &OS = Section->getStream(); |
| 309 | |
| 310 | writeUleb128(OS, 1, "segment count"); |
| 311 | writeUleb128(OS, 0, "table index"); |
| 312 | WasmInitExpr InitExpr; |
| 313 | InitExpr.Opcode = WASM_OPCODE_I32_CONST; |
Sam Clegg | 48bbd63 | 2018-01-24 21:37:30 +0000 | [diff] [blame] | 314 | InitExpr.Value.Int32 = kInitialTableOffset; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 315 | writeInitExpr(OS, InitExpr); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 316 | writeUleb128(OS, IndirectFunctions.size(), "elem count"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 317 | |
Sam Clegg | 48bbd63 | 2018-01-24 21:37:30 +0000 | [diff] [blame] | 318 | uint32_t TableIndex = kInitialTableOffset; |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 319 | for (const Symbol *Sym : IndirectFunctions) { |
| 320 | assert(Sym->getTableIndex() == TableIndex); |
| 321 | writeUleb128(OS, Sym->getOutputIndex(), "function index"); |
| 322 | ++TableIndex; |
| 323 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void Writer::createCodeSection() { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 327 | if (DefinedFunctions.empty()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 328 | return; |
| 329 | |
| 330 | log("createCodeSection"); |
| 331 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 332 | auto Section = make<CodeSection>(DefinedFunctions); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 333 | OutputSections.push_back(Section); |
| 334 | } |
| 335 | |
| 336 | void Writer::createDataSection() { |
| 337 | if (!Segments.size()) |
| 338 | return; |
| 339 | |
| 340 | log("createDataSection"); |
| 341 | auto Section = make<DataSection>(Segments); |
| 342 | OutputSections.push_back(Section); |
| 343 | } |
| 344 | |
Sam Clegg | d451da1 | 2017-12-19 19:56:27 +0000 | [diff] [blame] | 345 | // Create relocations sections in the final output. |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 346 | // These are only created when relocatable output is requested. |
| 347 | void Writer::createRelocSections() { |
| 348 | log("createRelocSections"); |
| 349 | // Don't use iterator here since we are adding to OutputSection |
| 350 | size_t OrigSize = OutputSections.size(); |
| 351 | for (size_t i = 0; i < OrigSize; i++) { |
| 352 | OutputSection *S = OutputSections[i]; |
| 353 | const char *name; |
| 354 | uint32_t Count = S->numRelocations(); |
| 355 | if (!Count) |
| 356 | continue; |
| 357 | |
| 358 | if (S->Type == WASM_SEC_DATA) |
| 359 | name = "reloc.DATA"; |
| 360 | else if (S->Type == WASM_SEC_CODE) |
| 361 | name = "reloc.CODE"; |
| 362 | else |
Sam Clegg | d451da1 | 2017-12-19 19:56:27 +0000 | [diff] [blame] | 363 | llvm_unreachable("relocations only supported for code and data"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 364 | |
| 365 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, name); |
| 366 | raw_ostream &OS = Section->getStream(); |
| 367 | writeUleb128(OS, S->Type, "reloc section"); |
| 368 | writeUleb128(OS, Count, "reloc count"); |
| 369 | S->writeRelocations(OS); |
| 370 | } |
| 371 | } |
| 372 | |
Sam Clegg | 49ed926 | 2017-12-01 00:53:21 +0000 | [diff] [blame] | 373 | // Create the custom "linking" section containing linker metadata. |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 374 | // This is only created when relocatable output is requested. |
| 375 | void Writer::createLinkingSection() { |
| 376 | SyntheticSection *Section = |
| 377 | createSyntheticSection(WASM_SEC_CUSTOM, "linking"); |
| 378 | raw_ostream &OS = Section->getStream(); |
| 379 | |
| 380 | SubSection DataSizeSubSection(WASM_DATA_SIZE); |
| 381 | writeUleb128(DataSizeSubSection.getStream(), DataSize, "data size"); |
| 382 | DataSizeSubSection.finalizeContents(); |
| 383 | DataSizeSubSection.writeToStream(OS); |
| 384 | |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 385 | if (!Config->Relocatable) |
| 386 | return; |
| 387 | |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 388 | std::vector<std::pair<StringRef, uint32_t>> SymbolInfo; |
Sam Clegg | 04b76f4 | 2018-01-19 21:49:41 +0000 | [diff] [blame] | 389 | auto addSymInfo = [&](const Symbol *Sym, StringRef ExternalName) { |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 390 | uint32_t Flags = |
Sam Clegg | 04b76f4 | 2018-01-19 21:49:41 +0000 | [diff] [blame] | 391 | (Sym->isLocal() ? WASM_SYMBOL_BINDING_LOCAL : |
| 392 | Sym->isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0) | |
| 393 | (Sym->isHidden() ? WASM_SYMBOL_VISIBILITY_HIDDEN : 0); |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 394 | if (Flags) |
Sam Clegg | 04b76f4 | 2018-01-19 21:49:41 +0000 | [diff] [blame] | 395 | SymbolInfo.emplace_back(ExternalName, Flags); |
| 396 | }; |
| 397 | // (Imports can't have internal linkage, their names don't need to be budged.) |
| 398 | for (const Symbol *Sym : ImportedFunctions) |
| 399 | addSymInfo(Sym, Sym->getName()); |
| 400 | for (const Symbol *Sym : ImportedGlobals) |
| 401 | addSymInfo(Sym, Sym->getName()); |
| 402 | for (const WasmExportEntry &E : ExportedSymbols) |
| 403 | addSymInfo(E.Sym, E.FieldName); |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 404 | if (!SymbolInfo.empty()) { |
| 405 | SubSection SubSection(WASM_SYMBOL_INFO); |
| 406 | writeUleb128(SubSection.getStream(), SymbolInfo.size(), "num sym info"); |
| 407 | for (auto Pair: SymbolInfo) { |
| 408 | writeStr(SubSection.getStream(), Pair.first, "sym name"); |
| 409 | writeUleb128(SubSection.getStream(), Pair.second, "sym flags"); |
| 410 | } |
| 411 | SubSection.finalizeContents(); |
| 412 | SubSection.writeToStream(OS); |
| 413 | } |
| 414 | |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 415 | if (Segments.size()) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 416 | SubSection SubSection(WASM_SEGMENT_INFO); |
| 417 | writeUleb128(SubSection.getStream(), Segments.size(), "num data segments"); |
| 418 | for (const OutputSegment *S : Segments) { |
| 419 | writeStr(SubSection.getStream(), S->Name, "segment name"); |
| 420 | writeUleb128(SubSection.getStream(), S->Alignment, "alignment"); |
| 421 | writeUleb128(SubSection.getStream(), 0, "flags"); |
| 422 | } |
| 423 | SubSection.finalizeContents(); |
| 424 | SubSection.writeToStream(OS); |
| 425 | } |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 426 | |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 427 | if (!InitFunctions.empty()) { |
| 428 | SubSection SubSection(WASM_INIT_FUNCS); |
| 429 | writeUleb128(SubSection.getStream(), InitFunctions.size(), |
Sam Clegg | 54c3891 | 2018-01-17 18:50:30 +0000 | [diff] [blame] | 430 | "num init functions"); |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 431 | for (const WasmInitFunc &F : InitFunctions) { |
| 432 | writeUleb128(SubSection.getStream(), F.Priority, "priority"); |
| 433 | writeUleb128(SubSection.getStream(), F.FunctionIndex, "function index"); |
| 434 | } |
| 435 | SubSection.finalizeContents(); |
| 436 | SubSection.writeToStream(OS); |
| 437 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 438 | |
| 439 | struct ComdatEntry { unsigned Kind; uint32_t Index; }; |
| 440 | std::map<StringRef,std::vector<ComdatEntry>> Comdats; |
| 441 | |
| 442 | for (const InputFunction *F : DefinedFunctions) { |
| 443 | StringRef Comdat = F->getComdat(); |
| 444 | if (!Comdat.empty()) |
| 445 | Comdats[Comdat].emplace_back( |
| 446 | ComdatEntry{WASM_COMDAT_FUNCTION, F->getOutputIndex()}); |
| 447 | } |
| 448 | for (uint32_t I = 0; I < Segments.size(); ++I) { |
Sam Clegg | f98bccf | 2018-01-13 15:57:48 +0000 | [diff] [blame] | 449 | const auto &InputSegments = Segments[I]->InputSegments; |
| 450 | if (InputSegments.empty()) |
| 451 | continue; |
| 452 | StringRef Comdat = InputSegments[0]->getComdat(); |
Sam Clegg | a697df52 | 2018-01-13 15:59:53 +0000 | [diff] [blame] | 453 | #ifndef NDEBUG |
Sam Clegg | f98bccf | 2018-01-13 15:57:48 +0000 | [diff] [blame] | 454 | for (const InputSegment *IS : InputSegments) |
| 455 | assert(IS->getComdat() == Comdat); |
Sam Clegg | a697df52 | 2018-01-13 15:59:53 +0000 | [diff] [blame] | 456 | #endif |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 457 | if (!Comdat.empty()) |
| 458 | Comdats[Comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, I}); |
| 459 | } |
| 460 | |
| 461 | if (!Comdats.empty()) { |
| 462 | SubSection SubSection(WASM_COMDAT_INFO); |
| 463 | writeUleb128(SubSection.getStream(), Comdats.size(), "num comdats"); |
| 464 | for (const auto &C : Comdats) { |
| 465 | writeStr(SubSection.getStream(), C.first, "comdat name"); |
| 466 | writeUleb128(SubSection.getStream(), 0, "comdat flags"); // flags for future use |
| 467 | writeUleb128(SubSection.getStream(), C.second.size(), "num entries"); |
| 468 | for (const ComdatEntry &Entry : C.second) { |
| 469 | writeUleb128(SubSection.getStream(), Entry.Kind, "entry kind"); |
| 470 | writeUleb128(SubSection.getStream(), Entry.Index, "entry index"); |
| 471 | } |
| 472 | } |
| 473 | SubSection.finalizeContents(); |
| 474 | SubSection.writeToStream(OS); |
| 475 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // Create the custom "name" section containing debug symbol names. |
| 479 | void Writer::createNameSection() { |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 480 | unsigned NumNames = ImportedFunctions.size(); |
| 481 | for (const InputFunction *F : DefinedFunctions) |
| 482 | if (!F->getName().empty()) |
| 483 | ++NumNames; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 484 | |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 485 | if (NumNames == 0) |
| 486 | return; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 487 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 488 | SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name"); |
| 489 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 490 | SubSection FunctionSubsection(WASM_NAMES_FUNCTION); |
| 491 | raw_ostream &OS = FunctionSubsection.getStream(); |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 492 | writeUleb128(OS, NumNames, "name count"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 493 | |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 494 | // Names must appear in function index order. As it happens ImportedFunctions |
| 495 | // and DefinedFunctions are numbers in order with imported functions coming |
| 496 | // first. |
| 497 | for (const Symbol *S : ImportedFunctions) { |
| 498 | writeUleb128(OS, S->getOutputIndex(), "import index"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 499 | writeStr(OS, S->getName(), "symbol name"); |
| 500 | } |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 501 | for (const InputFunction *F : DefinedFunctions) { |
| 502 | if (!F->getName().empty()) { |
| 503 | writeUleb128(OS, F->getOutputIndex(), "func index"); |
| 504 | writeStr(OS, F->getName(), "symbol name"); |
| 505 | } |
| 506 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 507 | |
| 508 | FunctionSubsection.finalizeContents(); |
| 509 | FunctionSubsection.writeToStream(Section->getStream()); |
| 510 | } |
| 511 | |
| 512 | void Writer::writeHeader() { |
| 513 | memcpy(Buffer->getBufferStart(), Header.data(), Header.size()); |
| 514 | } |
| 515 | |
| 516 | void Writer::writeSections() { |
| 517 | uint8_t *Buf = Buffer->getBufferStart(); |
| 518 | parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); }); |
| 519 | } |
| 520 | |
| 521 | // Fix the memory layout of the output binary. This assigns memory offsets |
Sam Clegg | 49ed926 | 2017-12-01 00:53:21 +0000 | [diff] [blame] | 522 | // to each of the input data sections as well as the explicit stack region. |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 523 | // The memory layout is as follows, from low to high. |
| 524 | // - initialized data (starting at Config->GlobalBase) |
| 525 | // - BSS data (not currently implemented in llvm) |
| 526 | // - explicit stack (Config->ZStackSize) |
| 527 | // - heap start / unallocated |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 528 | void Writer::layoutMemory() { |
| 529 | uint32_t MemoryPtr = 0; |
| 530 | if (!Config->Relocatable) { |
| 531 | MemoryPtr = Config->GlobalBase; |
| 532 | debugPrint("mem: global base = %d\n", Config->GlobalBase); |
| 533 | } |
| 534 | |
| 535 | createOutputSegments(); |
| 536 | |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 537 | // Arbitrarily set __dso_handle handle to point to the start of the data |
| 538 | // segments. |
| 539 | if (WasmSym::DsoHandle) |
| 540 | WasmSym::DsoHandle->setVirtualAddress(MemoryPtr); |
| 541 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 542 | for (OutputSegment *Seg : Segments) { |
| 543 | MemoryPtr = alignTo(MemoryPtr, Seg->Alignment); |
| 544 | Seg->StartVA = MemoryPtr; |
Sam Clegg | 7ed293e | 2018-01-12 00:34:04 +0000 | [diff] [blame] | 545 | debugPrint("mem: %-15s offset=%-8d size=%-8d align=%d\n", |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 546 | Seg->Name.str().c_str(), MemoryPtr, Seg->Size, Seg->Alignment); |
| 547 | MemoryPtr += Seg->Size; |
| 548 | } |
| 549 | |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 550 | // TODO: Add .bss space here. |
| 551 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 552 | DataSize = MemoryPtr; |
| 553 | if (!Config->Relocatable) |
| 554 | DataSize -= Config->GlobalBase; |
| 555 | debugPrint("mem: static data = %d\n", DataSize); |
| 556 | |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 557 | // Stack comes after static data and bss |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 558 | if (!Config->Relocatable) { |
| 559 | MemoryPtr = alignTo(MemoryPtr, kStackAlignment); |
| 560 | if (Config->ZStackSize != alignTo(Config->ZStackSize, kStackAlignment)) |
| 561 | error("stack size must be " + Twine(kStackAlignment) + "-byte aligned"); |
| 562 | debugPrint("mem: stack size = %d\n", Config->ZStackSize); |
| 563 | debugPrint("mem: stack base = %d\n", MemoryPtr); |
| 564 | MemoryPtr += Config->ZStackSize; |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 565 | WasmSym::StackPointer->setVirtualAddress(MemoryPtr); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 566 | debugPrint("mem: stack top = %d\n", MemoryPtr); |
Sam Clegg | 51bcdc2 | 2018-01-17 01:34:31 +0000 | [diff] [blame] | 567 | // Set `__heap_base` to directly follow the end of the stack. We don't |
| 568 | // allocate any heap memory up front, but instead really on the malloc/brk |
| 569 | // implementation growing the memory at runtime. |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 570 | WasmSym::HeapBase->setVirtualAddress(MemoryPtr); |
Sam Clegg | 51bcdc2 | 2018-01-17 01:34:31 +0000 | [diff] [blame] | 571 | debugPrint("mem: heap base = %d\n", MemoryPtr); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | uint32_t MemSize = alignTo(MemoryPtr, WasmPageSize); |
| 575 | NumMemoryPages = MemSize / WasmPageSize; |
| 576 | debugPrint("mem: total pages = %d\n", NumMemoryPages); |
| 577 | } |
| 578 | |
| 579 | SyntheticSection *Writer::createSyntheticSection(uint32_t Type, |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 580 | StringRef Name) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 581 | auto Sec = make<SyntheticSection>(Type, Name); |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame] | 582 | log("createSection: " + toString(*Sec)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 583 | OutputSections.push_back(Sec); |
| 584 | return Sec; |
| 585 | } |
| 586 | |
| 587 | void Writer::createSections() { |
| 588 | // Known sections |
| 589 | createTypeSection(); |
| 590 | createImportSection(); |
| 591 | createFunctionSection(); |
| 592 | createTableSection(); |
| 593 | createMemorySection(); |
| 594 | createGlobalSection(); |
| 595 | createExportSection(); |
| 596 | createStartSection(); |
| 597 | createElemSection(); |
| 598 | createCodeSection(); |
| 599 | createDataSection(); |
| 600 | |
| 601 | // Custom sections |
Sam Clegg | ff2b122 | 2018-01-22 21:55:43 +0000 | [diff] [blame] | 602 | if (Config->Relocatable) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 603 | createRelocSections(); |
| 604 | createLinkingSection(); |
| 605 | if (!Config->StripDebug && !Config->StripAll) |
| 606 | createNameSection(); |
| 607 | |
| 608 | for (OutputSection *S : OutputSections) { |
| 609 | S->setOffset(FileSize); |
| 610 | S->finalizeContents(); |
| 611 | FileSize += S->getSize(); |
| 612 | } |
| 613 | } |
| 614 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 615 | void Writer::calculateImports() { |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 616 | for (Symbol *Sym : Symtab->getSymbols()) { |
Sam Clegg | ff2b122 | 2018-01-22 21:55:43 +0000 | [diff] [blame] | 617 | if (!Sym->isUndefined() || (Sym->isWeak() && !Config->Relocatable)) |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 618 | continue; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 619 | |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 620 | if (Sym->isFunction()) { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 621 | Sym->setOutputIndex(ImportedFunctions.size()); |
| 622 | ImportedFunctions.push_back(Sym); |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 623 | } else { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 624 | Sym->setOutputIndex(ImportedGlobals.size()); |
| 625 | ImportedGlobals.push_back(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 630 | void Writer::calculateExports() { |
Sam Clegg | ff2b122 | 2018-01-22 21:55:43 +0000 | [diff] [blame] | 631 | bool ExportHidden = Config->Relocatable; |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 632 | StringSet<> UsedNames; |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 633 | |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 634 | auto BudgeLocalName = [&](const Symbol *Sym) { |
| 635 | StringRef SymName = Sym->getName(); |
| 636 | // We can't budge non-local names. |
| 637 | if (!Sym->isLocal()) |
| 638 | return SymName; |
| 639 | // We must budge local names that have a collision with a symbol that we |
| 640 | // haven't yet processed. |
| 641 | if (!Symtab->find(SymName) && UsedNames.insert(SymName).second) |
| 642 | return SymName; |
| 643 | for (unsigned I = 1; ; ++I) { |
| 644 | std::string NameBuf = (SymName + "." + Twine(I)).str(); |
| 645 | if (!UsedNames.count(NameBuf)) { |
| 646 | StringRef Name = Saver.save(NameBuf); |
| 647 | UsedNames.insert(Name); // Insert must use safe StringRef from save() |
| 648 | return Name; |
| 649 | } |
| 650 | } |
| 651 | }; |
| 652 | |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 653 | if (WasmSym::CallCtors && (!WasmSym::CallCtors->isHidden() || ExportHidden)) |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 654 | ExportedSymbols.emplace_back( |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 655 | WasmExportEntry{WasmSym::CallCtors, WasmSym::CallCtors->getName()}); |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 656 | |
| 657 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 658 | for (Symbol *Sym : File->getSymbols()) { |
| 659 | if (!Sym->isDefined() || File != Sym->getFile()) |
| 660 | continue; |
| 661 | if (Sym->isGlobal()) |
| 662 | continue; |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 663 | if (Sym->getChunk()->Discarded) |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 664 | continue; |
| 665 | |
| 666 | if ((Sym->isHidden() || Sym->isLocal()) && !ExportHidden) |
| 667 | continue; |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 668 | |
| 669 | // We should never be exporting a non-live symbol |
| 670 | assert(Sym->getChunk()->Live); |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 671 | ExportedSymbols.emplace_back(WasmExportEntry{Sym, BudgeLocalName(Sym)}); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | for (const Symbol *Sym : DefinedGlobals) { |
Sam Clegg | 14ae6e7 | 2018-01-18 23:57:55 +0000 | [diff] [blame] | 676 | // Can't export the SP right now because its mutable, and mutuable globals |
Sam Clegg | ff2b122 | 2018-01-22 21:55:43 +0000 | [diff] [blame] | 677 | // are yet supported in the official binary format. |
Sam Clegg | 14ae6e7 | 2018-01-18 23:57:55 +0000 | [diff] [blame] | 678 | // TODO(sbc): Remove this if/when the "mutable global" proposal is accepted. |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 679 | if (Sym == WasmSym::StackPointer) |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 680 | continue; |
| 681 | ExportedSymbols.emplace_back(WasmExportEntry{Sym, BudgeLocalName(Sym)}); |
| 682 | } |
| 683 | } |
| 684 | |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 685 | uint32_t Writer::lookupType(const WasmSignature &Sig) { |
Sam Clegg | 8d027d6 | 2018-01-10 20:12:26 +0000 | [diff] [blame] | 686 | auto It = TypeIndices.find(Sig); |
| 687 | if (It == TypeIndices.end()) { |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 688 | error("type not found: " + toString(Sig)); |
Sam Clegg | 8d027d6 | 2018-01-10 20:12:26 +0000 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | return It->second; |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | uint32_t Writer::registerType(const WasmSignature &Sig) { |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 695 | auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size())); |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 696 | if (Pair.second) { |
| 697 | DEBUG(dbgs() << "type " << toString(Sig) << "\n"); |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 698 | Types.push_back(&Sig); |
Sam Clegg | c375e4e | 2018-01-10 19:18:22 +0000 | [diff] [blame] | 699 | } |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 700 | return Pair.first->second; |
| 701 | } |
| 702 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 703 | void Writer::calculateTypes() { |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 704 | // The output type section is the union of the following sets: |
| 705 | // 1. Any signature used in the TYPE relocation |
| 706 | // 2. The signatures of all imported functions |
| 707 | // 3. The signatures of all defined functions |
| 708 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 709 | for (ObjFile *File : Symtab->ObjectFiles) { |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 710 | ArrayRef<WasmSignature> Types = File->getWasmObj()->types(); |
| 711 | for (uint32_t I = 0; I < Types.size(); I++) |
| 712 | if (File->TypeIsUsed[I]) |
| 713 | File->TypeMap[I] = registerType(Types[I]); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 714 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 715 | |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 716 | for (const Symbol *Sym : ImportedFunctions) |
| 717 | registerType(Sym->getFunctionType()); |
| 718 | |
| 719 | for (const InputFunction *F : DefinedFunctions) |
| 720 | registerType(F->Signature); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 723 | void Writer::assignIndexes() { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 724 | uint32_t GlobalIndex = ImportedGlobals.size() + DefinedGlobals.size(); |
| 725 | uint32_t FunctionIndex = ImportedFunctions.size() + DefinedFunctions.size(); |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 726 | |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 727 | auto AddDefinedGlobal = [&](Symbol* Sym) { |
| 728 | if (Sym) { |
| 729 | DefinedGlobals.emplace_back(Sym); |
| 730 | Sym->setOutputIndex(GlobalIndex++); |
| 731 | } |
| 732 | }; |
| 733 | AddDefinedGlobal(WasmSym::StackPointer); |
| 734 | AddDefinedGlobal(WasmSym::HeapBase); |
Sam Clegg | 51bcdc2 | 2018-01-17 01:34:31 +0000 | [diff] [blame] | 735 | |
Sam Clegg | ff2b122 | 2018-01-22 21:55:43 +0000 | [diff] [blame] | 736 | if (Config->Relocatable) |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 737 | DefinedGlobals.reserve(Symtab->getSymbols().size()); |
| 738 | |
Sam Clegg | 48bbd63 | 2018-01-24 21:37:30 +0000 | [diff] [blame] | 739 | uint32_t TableIndex = kInitialTableOffset; |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 740 | |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 741 | if (Config->Relocatable) { |
| 742 | for (ObjFile *File : Symtab->ObjectFiles) { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 743 | DEBUG(dbgs() << "Globals: " << File->getName() << "\n"); |
| 744 | for (Symbol *Sym : File->getSymbols()) { |
| 745 | // Create wasm globals for data symbols defined in this file |
| 746 | if (!Sym->isDefined() || File != Sym->getFile()) |
| 747 | continue; |
| 748 | if (Sym->isFunction()) |
| 749 | continue; |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 750 | |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 751 | AddDefinedGlobal(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 752 | } |
| 753 | } |
Sam Clegg | 87e6192 | 2018-01-08 23:39:11 +0000 | [diff] [blame] | 754 | } |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 755 | |
Sam Clegg | 87e6192 | 2018-01-08 23:39:11 +0000 | [diff] [blame] | 756 | for (ObjFile *File : Symtab->ObjectFiles) { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 757 | DEBUG(dbgs() << "Functions: " << File->getName() << "\n"); |
| 758 | for (InputFunction *Func : File->Functions) { |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 759 | if (Func->Discarded || !Func->Live) |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 760 | continue; |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 761 | DefinedFunctions.emplace_back(Func); |
| 762 | Func->setOutputIndex(FunctionIndex++); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | for (ObjFile *File : Symtab->ObjectFiles) { |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 767 | DEBUG(dbgs() << "Handle relocs: " << File->getName() << "\n"); |
| 768 | auto HandleRelocs = [&](InputChunk *Chunk) { |
Sam Clegg | ab604a9 | 2018-01-23 01:25:56 +0000 | [diff] [blame] | 769 | if (Chunk->Discarded) |
| 770 | return; |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 771 | ArrayRef<WasmSignature> Types = File->getWasmObj()->types(); |
Sam Clegg | ab604a9 | 2018-01-23 01:25:56 +0000 | [diff] [blame] | 772 | for (const WasmRelocation& Reloc : Chunk->getRelocations()) { |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 773 | if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32 || |
| 774 | Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB) { |
| 775 | Symbol *Sym = File->getFunctionSymbol(Reloc.Index); |
| 776 | if (Sym->hasTableIndex() || !Sym->hasOutputIndex()) |
| 777 | continue; |
| 778 | Sym->setTableIndex(TableIndex++); |
| 779 | IndirectFunctions.emplace_back(Sym); |
| 780 | } else if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB) { |
| 781 | Chunk->File->TypeMap[Reloc.Index] = registerType(Types[Reloc.Index]); |
| 782 | Chunk->File->TypeIsUsed[Reloc.Index] = true; |
| 783 | } |
Sam Clegg | ab604a9 | 2018-01-23 01:25:56 +0000 | [diff] [blame] | 784 | } |
| 785 | }; |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 786 | |
Sam Clegg | ab604a9 | 2018-01-23 01:25:56 +0000 | [diff] [blame] | 787 | for (InputFunction* Function : File->Functions) |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 788 | HandleRelocs(Function); |
Sam Clegg | ab604a9 | 2018-01-23 01:25:56 +0000 | [diff] [blame] | 789 | for (InputSegment* Segment : File->Segments) |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 790 | HandleRelocs(Segment); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
| 794 | static StringRef getOutputDataSegmentName(StringRef Name) { |
| 795 | if (Config->Relocatable) |
| 796 | return Name; |
| 797 | |
| 798 | for (StringRef V : |
| 799 | {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.", |
| 800 | ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.", |
| 801 | ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."}) { |
| 802 | StringRef Prefix = V.drop_back(); |
| 803 | if (Name.startswith(V) || Name == Prefix) |
| 804 | return Prefix; |
| 805 | } |
| 806 | |
| 807 | return Name; |
| 808 | } |
| 809 | |
| 810 | void Writer::createOutputSegments() { |
| 811 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 812 | for (InputSegment *Segment : File->Segments) { |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 813 | if (Segment->Discarded || !Segment->Live) |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 814 | continue; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 815 | StringRef Name = getOutputDataSegmentName(Segment->getName()); |
| 816 | OutputSegment *&S = SegmentMap[Name]; |
| 817 | if (S == nullptr) { |
| 818 | DEBUG(dbgs() << "new segment: " << Name << "\n"); |
| 819 | S = make<OutputSegment>(Name); |
| 820 | Segments.push_back(S); |
| 821 | } |
| 822 | S->addInputSegment(Segment); |
| 823 | DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 828 | static const int OPCODE_CALL = 0x10; |
| 829 | static const int OPCODE_END = 0xb; |
| 830 | |
| 831 | // Create synthetic "__wasm_call_ctors" function based on ctor functions |
| 832 | // in input object. |
| 833 | void Writer::createCtorFunction() { |
| 834 | uint32_t FunctionIndex = ImportedFunctions.size() + DefinedFunctions.size(); |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 835 | WasmSym::CallCtors->setOutputIndex(FunctionIndex); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 836 | |
| 837 | // First write the body bytes to a string. |
| 838 | std::string FunctionBody; |
| 839 | static WasmSignature Signature = {{}, WASM_TYPE_NORESULT}; |
| 840 | { |
| 841 | raw_string_ostream OS(FunctionBody); |
| 842 | writeUleb128(OS, 0, "num locals"); |
| 843 | for (const WasmInitFunc &F : InitFunctions) { |
| 844 | writeU8(OS, OPCODE_CALL, "CALL"); |
| 845 | writeUleb128(OS, F.FunctionIndex, "function index"); |
| 846 | } |
| 847 | writeU8(OS, OPCODE_END, "END"); |
| 848 | } |
| 849 | |
| 850 | // Once we know the size of the body we can create the final function body |
| 851 | raw_string_ostream OS(CtorFunctionBody); |
| 852 | writeUleb128(OS, FunctionBody.size(), "function size"); |
| 853 | OS.flush(); |
| 854 | CtorFunctionBody += FunctionBody; |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 855 | ArrayRef<uint8_t> BodyArray( |
| 856 | reinterpret_cast<const uint8_t *>(CtorFunctionBody.data()), |
| 857 | CtorFunctionBody.size()); |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 858 | CtorFunction = llvm::make_unique<SyntheticFunction>( |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame^] | 859 | Signature, BodyArray, WasmSym::CallCtors->getName()); |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 860 | CtorFunction->setOutputIndex(FunctionIndex); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 861 | DefinedFunctions.emplace_back(CtorFunction.get()); |
| 862 | } |
| 863 | |
| 864 | // Populate InitFunctions vector with init functions from all input objects. |
| 865 | // This is then used either when creating the output linking section or to |
| 866 | // synthesize the "__wasm_call_ctors" function. |
| 867 | void Writer::calculateInitFunctions() { |
| 868 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 869 | const WasmLinkingData &L = File->getWasmObj()->linkingData(); |
| 870 | InitFunctions.reserve(InitFunctions.size() + L.InitFunctions.size()); |
| 871 | for (const WasmInitFunc &F : L.InitFunctions) |
| 872 | InitFunctions.emplace_back(WasmInitFunc{ |
| 873 | F.Priority, File->relocateFunctionIndex(F.FunctionIndex)}); |
| 874 | } |
| 875 | // Sort in order of priority (lowest first) so that they are called |
| 876 | // in the correct order. |
| 877 | std::sort(InitFunctions.begin(), InitFunctions.end(), |
| 878 | [](const WasmInitFunc &L, const WasmInitFunc &R) { |
| 879 | return L.Priority < R.Priority; |
| 880 | }); |
| 881 | } |
| 882 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 883 | void Writer::run() { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 884 | log("-- calculateImports"); |
| 885 | calculateImports(); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 886 | log("-- assignIndexes"); |
| 887 | assignIndexes(); |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 888 | log("-- calculateExports"); |
| 889 | calculateExports(); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 890 | log("-- calculateInitFunctions"); |
| 891 | calculateInitFunctions(); |
| 892 | if (!Config->Relocatable) |
| 893 | createCtorFunction(); |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 894 | log("-- calculateTypes"); |
| 895 | calculateTypes(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 896 | |
| 897 | if (errorHandler().Verbose) { |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 898 | log("Defined Functions: " + Twine(DefinedFunctions.size())); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 899 | log("Defined Globals : " + Twine(DefinedGlobals.size())); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 900 | log("Function Imports : " + Twine(ImportedFunctions.size())); |
| 901 | log("Global Imports : " + Twine(ImportedGlobals.size())); |
Sam Clegg | fc1a912 | 2017-12-11 22:00:56 +0000 | [diff] [blame] | 902 | log("Total Imports : " + |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 903 | Twine(ImportedFunctions.size() + ImportedGlobals.size())); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 904 | for (ObjFile *File : Symtab->ObjectFiles) |
| 905 | File->dumpInfo(); |
| 906 | } |
| 907 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 908 | log("-- layoutMemory"); |
| 909 | layoutMemory(); |
| 910 | |
| 911 | createHeader(); |
| 912 | log("-- createSections"); |
| 913 | createSections(); |
| 914 | |
| 915 | log("-- openFile"); |
| 916 | openFile(); |
| 917 | if (errorCount()) |
| 918 | return; |
| 919 | |
| 920 | writeHeader(); |
| 921 | |
| 922 | log("-- writeSections"); |
| 923 | writeSections(); |
| 924 | if (errorCount()) |
| 925 | return; |
| 926 | |
| 927 | if (Error E = Buffer->commit()) |
| 928 | fatal("failed to write the output file: " + toString(std::move(E))); |
| 929 | } |
| 930 | |
| 931 | // Open a result file. |
| 932 | void Writer::openFile() { |
| 933 | log("writing: " + Config->OutputFile); |
| 934 | ::remove(Config->OutputFile.str().c_str()); |
| 935 | |
| 936 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 937 | FileOutputBuffer::create(Config->OutputFile, FileSize, |
| 938 | FileOutputBuffer::F_executable); |
| 939 | |
| 940 | if (!BufferOrErr) |
| 941 | error("failed to open " + Config->OutputFile + ": " + |
| 942 | toString(BufferOrErr.takeError())); |
| 943 | else |
| 944 | Buffer = std::move(*BufferOrErr); |
| 945 | } |
| 946 | |
| 947 | void Writer::createHeader() { |
| 948 | raw_string_ostream OS(Header); |
| 949 | writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic"); |
| 950 | writeU32(OS, WasmVersion, "wasm version"); |
| 951 | OS.flush(); |
| 952 | FileSize += Header.size(); |
| 953 | } |
| 954 | |
| 955 | void lld::wasm::writeResult() { Writer().run(); } |