Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1 | //===- Writer.cpp ---------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Writer.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 10 | #include "Config.h" |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 11 | #include "InputChunks.h" |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 12 | #include "InputEvent.h" |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 13 | #include "InputGlobal.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 14 | #include "OutputSections.h" |
| 15 | #include "OutputSegment.h" |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 16 | #include "Relocations.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 17 | #include "SymbolTable.h" |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 18 | #include "SyntheticSections.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 19 | #include "WriterUtils.h" |
| 20 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 2017d52 | 2017-11-28 20:39:17 +0000 | [diff] [blame] | 21 | #include "lld/Common/Memory.h" |
Nicholas Wilson | 8269f37 | 2018-03-07 10:37:50 +0000 | [diff] [blame] | 22 | #include "lld/Common/Strings.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 23 | #include "lld/Common/Threads.h" |
Sam Clegg | 3141ddc | 2018-02-20 21:53:18 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseSet.h" |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallSet.h" |
Thomas Lively | 2a0868f | 2019-01-17 02:29:41 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallVector.h" |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringMap.h" |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 28 | #include "llvm/BinaryFormat/Wasm.h" |
Nicholas Wilson | 3e3f5fb | 2018-03-14 15:58:16 +0000 | [diff] [blame] | 29 | #include "llvm/Object/WasmTraits.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 30 | #include "llvm/Support/FileOutputBuffer.h" |
| 31 | #include "llvm/Support/Format.h" |
| 32 | #include "llvm/Support/FormatVariadic.h" |
| 33 | #include "llvm/Support/LEB128.h" |
| 34 | |
| 35 | #include <cstdarg> |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 36 | #include <map> |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 37 | |
| 38 | #define DEBUG_TYPE "lld" |
| 39 | |
| 40 | using namespace llvm; |
| 41 | using namespace llvm::wasm; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 42 | |
Fangrui Song | 33c59ab | 2019-10-10 05:25:39 +0000 | [diff] [blame] | 43 | namespace lld { |
| 44 | namespace wasm { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 45 | static constexpr int stackAlignment = 16; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 46 | |
| 47 | namespace { |
| 48 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 49 | // The writer writes a SymbolTable result to a file. |
| 50 | class Writer { |
| 51 | public: |
| 52 | void run(); |
| 53 | |
| 54 | private: |
| 55 | void openFile(); |
| 56 | |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 57 | void createInitMemoryFunction(); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 58 | void createApplyRelocationsFunction(); |
| 59 | void createCallCtorsFunction(); |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 60 | void createInitTLSFunction(); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 61 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 62 | void assignIndexes(); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 63 | void populateSymtab(); |
| 64 | void populateProducers(); |
| 65 | void populateTargetFeatures(); |
| 66 | void calculateInitFunctions(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 67 | void calculateImports(); |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 68 | void calculateExports(); |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 69 | void calculateCustomSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 70 | void calculateTypes(); |
| 71 | void createOutputSegments(); |
| 72 | void layoutMemory(); |
| 73 | void createHeader(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 74 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 75 | void addSection(OutputSection *sec); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 76 | |
| 77 | void addSections(); |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 78 | |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 79 | void createCustomSections(); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 80 | void createSyntheticSections(); |
| 81 | void finalizeSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 82 | |
| 83 | // Custom sections |
| 84 | void createRelocSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 85 | |
| 86 | void writeHeader(); |
| 87 | void writeSections(); |
| 88 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 89 | uint64_t fileSize = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 90 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 91 | std::vector<WasmInitEntry> initFunctions; |
| 92 | llvm::StringMap<std::vector<InputSection *>> customSectionMapping; |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 93 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 94 | // Elements that are used to construct the final output |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 95 | std::string header; |
| 96 | std::vector<OutputSection *> outputSections; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 97 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 98 | std::unique_ptr<FileOutputBuffer> buffer; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 99 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 100 | std::vector<OutputSegment *> segments; |
| 101 | llvm::SmallDenseMap<StringRef, OutputSegment *> segmentMap; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | } // anonymous namespace |
| 105 | |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 106 | void Writer::calculateCustomSections() { |
| 107 | log("calculateCustomSections"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 108 | bool stripDebug = config->stripDebug || config->stripAll; |
| 109 | for (ObjFile *file : symtab->objectFiles) { |
| 110 | for (InputSection *section : file->customSections) { |
| 111 | StringRef name = section->getName(); |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 112 | // These custom sections are known the linker and synthesized rather than |
| 113 | // blindly copied |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 114 | if (name == "linking" || name == "name" || name == "producers" || |
| 115 | name == "target_features" || name.startswith("reloc.")) |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 116 | continue; |
| 117 | // .. or it is a debug section |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 118 | if (stripDebug && name.startswith(".debug_")) |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 119 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 120 | customSectionMapping[name].push_back(section); |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 125 | void Writer::createCustomSections() { |
| 126 | log("createCustomSections"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 127 | for (auto &pair : customSectionMapping) { |
| 128 | StringRef name = pair.first(); |
| 129 | LLVM_DEBUG(dbgs() << "createCustomSection: " << name << "\n"); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 130 | |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame^] | 131 | OutputSection *sec = make<CustomSection>(std::string(name), pair.second); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 132 | if (config->relocatable || config->emitRelocs) { |
| 133 | auto *sym = make<OutputSectionSymbol>(sec); |
| 134 | out.linkingSec->addToSymtab(sym); |
| 135 | sec->sectionSym = sym; |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 136 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 137 | addSection(sec); |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Sam Clegg | d451da1 | 2017-12-19 19:56:27 +0000 | [diff] [blame] | 141 | // Create relocations sections in the final output. |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 142 | // These are only created when relocatable output is requested. |
| 143 | void Writer::createRelocSections() { |
| 144 | log("createRelocSections"); |
| 145 | // Don't use iterator here since we are adding to OutputSection |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 146 | size_t origSize = outputSections.size(); |
| 147 | for (size_t i = 0; i < origSize; i++) { |
| 148 | LLVM_DEBUG(dbgs() << "check section " << i << "\n"); |
| 149 | OutputSection *sec = outputSections[i]; |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 150 | |
| 151 | // Count the number of needed sections. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 152 | uint32_t count = sec->getNumRelocations(); |
| 153 | if (!count) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 154 | continue; |
| 155 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 156 | StringRef name; |
| 157 | if (sec->type == WASM_SEC_DATA) |
| 158 | name = "reloc.DATA"; |
| 159 | else if (sec->type == WASM_SEC_CODE) |
| 160 | name = "reloc.CODE"; |
| 161 | else if (sec->type == WASM_SEC_CUSTOM) |
| 162 | name = saver.save("reloc." + sec->name); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 163 | else |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 164 | llvm_unreachable( |
| 165 | "relocations only supported for code, data, or custom sections"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 166 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 167 | addSection(make<RelocSection>(name, sec)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 171 | void Writer::populateProducers() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 172 | for (ObjFile *file : symtab->objectFiles) { |
| 173 | const WasmProducerInfo &info = file->getWasmObj()->getProducerInfo(); |
| 174 | out.producersSec->addInfo(info); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 178 | void Writer::writeHeader() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 179 | memcpy(buffer->getBufferStart(), header.data(), header.size()); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void Writer::writeSections() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 183 | uint8_t *buf = buffer->getBufferStart(); |
| 184 | parallelForEach(outputSections, [buf](OutputSection *s) { |
| 185 | assert(s->isNeeded()); |
| 186 | s->writeTo(buf); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 187 | }); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // Fix the memory layout of the output binary. This assigns memory offsets |
Sam Clegg | 49ed926 | 2017-12-01 00:53:21 +0000 | [diff] [blame] | 191 | // to each of the input data sections as well as the explicit stack region. |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 192 | // The default memory layout is as follows, from low to high. |
| 193 | // |
Fangrui Song | 33fdf82 | 2019-07-16 08:08:17 +0000 | [diff] [blame] | 194 | // - initialized data (starting at Config->globalBase) |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 195 | // - BSS data (not currently implemented in llvm) |
| 196 | // - explicit stack (Config->ZStackSize) |
| 197 | // - heap start / unallocated |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 198 | // |
| 199 | // The --stack-first option means that stack is placed before any static data. |
Heejin Ahn | 4821ebf | 2018-08-29 21:03:16 +0000 | [diff] [blame] | 200 | // This can be useful since it means that stack overflow traps immediately |
| 201 | // rather than overwriting global data, but also increases code size since all |
| 202 | // static data loads and stores requires larger offsets. |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 203 | void Writer::layoutMemory() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 204 | uint32_t memoryPtr = 0; |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 205 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 206 | auto placeStack = [&]() { |
Sam Clegg | fd11ce3 | 2019-07-11 13:13:25 +0000 | [diff] [blame] | 207 | if (config->relocatable || config->isPic) |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 208 | return; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 209 | memoryPtr = alignTo(memoryPtr, stackAlignment); |
| 210 | if (config->zStackSize != alignTo(config->zStackSize, stackAlignment)) |
| 211 | error("stack size must be " + Twine(stackAlignment) + "-byte aligned"); |
| 212 | log("mem: stack size = " + Twine(config->zStackSize)); |
| 213 | log("mem: stack base = " + Twine(memoryPtr)); |
| 214 | memoryPtr += config->zStackSize; |
| 215 | auto *sp = cast<DefinedGlobal>(WasmSym::stackPointer); |
| 216 | sp->global->global.InitExpr.Value.Int32 = memoryPtr; |
| 217 | log("mem: stack top = " + Twine(memoryPtr)); |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 220 | if (config->stackFirst) { |
| 221 | placeStack(); |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 222 | } else { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 223 | memoryPtr = config->globalBase; |
| 224 | log("mem: global base = " + Twine(config->globalBase)); |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 227 | if (WasmSym::globalBase) |
Sam Clegg | 7185a73 | 2019-08-13 17:02:02 +0000 | [diff] [blame] | 228 | WasmSym::globalBase->setVirtualAddress(memoryPtr); |
Guanzhong Chen | e15dc95 | 2019-06-26 20:12:33 +0000 | [diff] [blame] | 229 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 230 | uint32_t dataStart = memoryPtr; |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 231 | |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 232 | // Arbitrarily set __dso_handle handle to point to the start of the data |
| 233 | // segments. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 234 | if (WasmSym::dsoHandle) |
| 235 | WasmSym::dsoHandle->setVirtualAddress(dataStart); |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 236 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 237 | out.dylinkSec->memAlign = 0; |
| 238 | for (OutputSegment *seg : segments) { |
| 239 | out.dylinkSec->memAlign = std::max(out.dylinkSec->memAlign, seg->alignment); |
| 240 | memoryPtr = alignTo(memoryPtr, 1ULL << seg->alignment); |
| 241 | seg->startVA = memoryPtr; |
| 242 | log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", seg->name, |
| 243 | memoryPtr, seg->size, seg->alignment)); |
| 244 | memoryPtr += seg->size; |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 245 | |
| 246 | if (WasmSym::tlsSize && seg->name == ".tdata") { |
| 247 | auto *tlsSize = cast<DefinedGlobal>(WasmSym::tlsSize); |
| 248 | tlsSize->global->global.InitExpr.Value.Int32 = seg->size; |
Guanzhong Chen | 5204f76 | 2019-07-19 23:34:16 +0000 | [diff] [blame] | 249 | |
| 250 | auto *tlsAlign = cast<DefinedGlobal>(WasmSym::tlsAlign); |
| 251 | tlsAlign->global->global.InitExpr.Value.Int32 = 1U << seg->alignment; |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 252 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 255 | // Make space for the memory initialization flag |
| 256 | if (WasmSym::initMemoryFlag) { |
| 257 | memoryPtr = alignTo(memoryPtr, 4); |
| 258 | WasmSym::initMemoryFlag->setVirtualAddress(memoryPtr); |
| 259 | log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", |
| 260 | "__wasm_init_memory_flag", memoryPtr, 4, 4)); |
| 261 | memoryPtr += 4; |
| 262 | } |
| 263 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 264 | if (WasmSym::dataEnd) |
| 265 | WasmSym::dataEnd->setVirtualAddress(memoryPtr); |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 266 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 267 | log("mem: static data = " + Twine(memoryPtr - dataStart)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 268 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 269 | if (config->shared) { |
| 270 | out.dylinkSec->memSize = memoryPtr; |
Sam Clegg | 2dad4e2 | 2018-11-15 18:15:54 +0000 | [diff] [blame] | 271 | return; |
| 272 | } |
| 273 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 274 | if (!config->stackFirst) |
| 275 | placeStack(); |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 276 | |
| 277 | // Set `__heap_base` to directly follow the end of the stack or global data. |
| 278 | // The fact that this comes last means that a malloc/brk implementation |
| 279 | // can grow the heap at runtime. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 280 | log("mem: heap base = " + Twine(memoryPtr)); |
| 281 | if (WasmSym::heapBase) |
| 282 | WasmSym::heapBase->setVirtualAddress(memoryPtr); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 283 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 284 | if (config->initialMemory != 0) { |
| 285 | if (config->initialMemory != alignTo(config->initialMemory, WasmPageSize)) |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 286 | error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 287 | if (memoryPtr > config->initialMemory) |
| 288 | error("initial memory too small, " + Twine(memoryPtr) + " bytes needed"); |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 289 | else |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 290 | memoryPtr = config->initialMemory; |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 291 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 292 | out.dylinkSec->memSize = memoryPtr; |
| 293 | out.memorySec->numMemoryPages = |
| 294 | alignTo(memoryPtr, WasmPageSize) / WasmPageSize; |
| 295 | log("mem: total pages = " + Twine(out.memorySec->numMemoryPages)); |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 296 | |
Thomas Lively | 06391f3 | 2019-03-29 20:43:49 +0000 | [diff] [blame] | 297 | // Check max if explicitly supplied or required by shared memory |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 298 | if (config->maxMemory != 0 || config->sharedMemory) { |
| 299 | if (config->maxMemory != alignTo(config->maxMemory, WasmPageSize)) |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 300 | error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 301 | if (memoryPtr > config->maxMemory) |
| 302 | error("maximum memory too small, " + Twine(memoryPtr) + " bytes needed"); |
| 303 | out.memorySec->maxMemoryPages = config->maxMemory / WasmPageSize; |
| 304 | log("mem: max pages = " + Twine(out.memorySec->maxMemoryPages)); |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 305 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 308 | void Writer::addSection(OutputSection *sec) { |
| 309 | if (!sec->isNeeded()) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 310 | return; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 311 | log("addSection: " + toString(*sec)); |
| 312 | sec->sectionIndex = outputSections.size(); |
| 313 | outputSections.push_back(sec); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 316 | // If a section name is valid as a C identifier (which is rare because of |
| 317 | // the leading '.'), linkers are expected to define __start_<secname> and |
| 318 | // __stop_<secname> symbols. They are at beginning and end of the section, |
| 319 | // respectively. This is not requested by the ELF standard, but GNU ld and |
| 320 | // gold provide the feature, and used by many programs. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 321 | static void addStartStopSymbols(const OutputSegment *seg) { |
| 322 | StringRef name = seg->name; |
| 323 | if (!isValidCIdentifier(name)) |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 324 | return; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 325 | LLVM_DEBUG(dbgs() << "addStartStopSymbols: " << name << "\n"); |
| 326 | uint32_t start = seg->startVA; |
| 327 | uint32_t stop = start + seg->size; |
| 328 | symtab->addOptionalDataSymbol(saver.save("__start_" + name), start); |
| 329 | symtab->addOptionalDataSymbol(saver.save("__stop_" + name), stop); |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 332 | void Writer::addSections() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 333 | addSection(out.dylinkSec); |
| 334 | addSection(out.typeSec); |
| 335 | addSection(out.importSec); |
| 336 | addSection(out.functionSec); |
| 337 | addSection(out.tableSec); |
| 338 | addSection(out.memorySec); |
| 339 | addSection(out.globalSec); |
| 340 | addSection(out.eventSec); |
| 341 | addSection(out.exportSec); |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 342 | addSection(out.startSec); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 343 | addSection(out.elemSec); |
| 344 | addSection(out.dataCountSec); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 345 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 346 | addSection(make<CodeSection>(out.functionSec->inputFunctions)); |
| 347 | addSection(make<DataSection>(segments)); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 348 | |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 349 | createCustomSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 350 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 351 | addSection(out.linkingSec); |
| 352 | if (config->emitRelocs || config->relocatable) { |
Nicholas Wilson | 94d3b16 | 2018-03-05 12:33:58 +0000 | [diff] [blame] | 353 | createRelocSections(); |
Sam Clegg | 99eb42c | 2018-02-27 23:58:03 +0000 | [diff] [blame] | 354 | } |
Thomas Lively | 2a0868f | 2019-01-17 02:29:41 +0000 | [diff] [blame] | 355 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 356 | addSection(out.nameSec); |
| 357 | addSection(out.producersSec); |
| 358 | addSection(out.targetFeaturesSec); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 359 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 360 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 361 | void Writer::finalizeSections() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 362 | for (OutputSection *s : outputSections) { |
| 363 | s->setOffset(fileSize); |
| 364 | s->finalizeContents(); |
| 365 | fileSize += s->getSize(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 369 | void Writer::populateTargetFeatures() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 370 | StringMap<std::string> used; |
| 371 | StringMap<std::string> required; |
| 372 | StringMap<std::string> disallowed; |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 373 | SmallSet<std::string, 8> &allowed = out.targetFeaturesSec->features; |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 374 | bool tlsUsed = false; |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 375 | |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 376 | // Only infer used features if user did not specify features |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 377 | bool inferFeatures = !config->features.hasValue(); |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 378 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 379 | if (!inferFeatures) { |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 380 | auto &explicitFeatures = config->features.getValue(); |
| 381 | allowed.insert(explicitFeatures.begin(), explicitFeatures.end()); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 382 | if (!config->checkFeatures) |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 386 | // Find the sets of used, required, and disallowed features |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 387 | for (ObjFile *file : symtab->objectFiles) { |
| 388 | StringRef fileName(file->getName()); |
| 389 | for (auto &feature : file->getWasmObj()->getTargetFeatures()) { |
| 390 | switch (feature.Prefix) { |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 391 | case WASM_FEATURE_PREFIX_USED: |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame^] | 392 | used.insert({feature.Name, std::string(fileName)}); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 393 | break; |
| 394 | case WASM_FEATURE_PREFIX_REQUIRED: |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame^] | 395 | used.insert({feature.Name, std::string(fileName)}); |
| 396 | required.insert({feature.Name, std::string(fileName)}); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 397 | break; |
| 398 | case WASM_FEATURE_PREFIX_DISALLOWED: |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame^] | 399 | disallowed.insert({feature.Name, std::string(fileName)}); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 400 | break; |
| 401 | default: |
| 402 | error("Unrecognized feature policy prefix " + |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 403 | std::to_string(feature.Prefix)); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 406 | |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 407 | // Find TLS data segments |
| 408 | auto isTLS = [](InputSegment *segment) { |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 409 | StringRef name = segment->getName(); |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 410 | return segment->live && |
| 411 | (name.startswith(".tdata") || name.startswith(".tbss")); |
| 412 | }; |
| 413 | tlsUsed = tlsUsed || |
| 414 | std::any_of(file->segments.begin(), file->segments.end(), isTLS); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 417 | if (inferFeatures) |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame^] | 418 | for (const auto &key : used.keys()) |
| 419 | allowed.insert(std::string(key)); |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 420 | |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 421 | if (allowed.count("atomics") && !config->sharedMemory) { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 422 | if (inferFeatures) |
| 423 | error(Twine("'atomics' feature is used by ") + used["atomics"] + |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame] | 424 | ", so --shared-memory must be used"); |
| 425 | else |
| 426 | error("'atomics' feature is used, so --shared-memory must be used"); |
| 427 | } |
Thomas Lively | 06391f3 | 2019-03-29 20:43:49 +0000 | [diff] [blame] | 428 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 429 | if (!config->checkFeatures) |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 430 | return; |
| 431 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 432 | if (disallowed.count("atomics") && config->sharedMemory) |
| 433 | error("'atomics' feature is disallowed by " + disallowed["atomics"] + |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame] | 434 | ", so --shared-memory must not be used"); |
Thomas Lively | 06391f3 | 2019-03-29 20:43:49 +0000 | [diff] [blame] | 435 | |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 436 | if (!allowed.count("atomics") && config->sharedMemory) |
| 437 | error("'atomics' feature must be used in order to use shared " |
| 438 | "memory"); |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 439 | |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 440 | if (!allowed.count("bulk-memory") && config->sharedMemory) |
| 441 | error("'bulk-memory' feature must be used in order to use shared " |
| 442 | "memory"); |
| 443 | |
| 444 | if (!allowed.count("bulk-memory") && tlsUsed) |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 445 | error("'bulk-memory' feature must be used in order to use thread-local " |
| 446 | "storage"); |
| 447 | |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 448 | // Validate that used features are allowed in output |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 449 | if (!inferFeatures) { |
| 450 | for (auto &feature : used.keys()) { |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame^] | 451 | if (!allowed.count(std::string(feature))) |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 452 | error(Twine("Target feature '") + feature + "' used by " + |
| 453 | used[feature] + " is not allowed."); |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 457 | // Validate the required and disallowed constraints for each file |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 458 | for (ObjFile *file : symtab->objectFiles) { |
| 459 | StringRef fileName(file->getName()); |
| 460 | SmallSet<std::string, 8> objectFeatures; |
| 461 | for (auto &feature : file->getWasmObj()->getTargetFeatures()) { |
| 462 | if (feature.Prefix == WASM_FEATURE_PREFIX_DISALLOWED) |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 463 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 464 | objectFeatures.insert(feature.Name); |
| 465 | if (disallowed.count(feature.Name)) |
| 466 | error(Twine("Target feature '") + feature.Name + "' used in " + |
| 467 | fileName + " is disallowed by " + disallowed[feature.Name] + |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame] | 468 | ". Use --no-check-features to suppress."); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 469 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 470 | for (auto &feature : required.keys()) { |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame^] | 471 | if (!objectFeatures.count(std::string(feature))) |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 472 | error(Twine("Missing target feature '") + feature + "' in " + fileName + |
| 473 | ", required by " + required[feature] + |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame] | 474 | ". Use --no-check-features to suppress."); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 479 | void Writer::calculateImports() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 480 | for (Symbol *sym : symtab->getSymbols()) { |
| 481 | if (!sym->isUndefined()) |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 482 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 483 | if (sym->isWeak() && !config->relocatable) |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 484 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 485 | if (!sym->isLive()) |
Nicholas Wilson | a1e299f | 2018-04-20 17:18:06 +0000 | [diff] [blame] | 486 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 487 | if (!sym->isUsedInRegularObj) |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 488 | continue; |
Sam Clegg | 492f752 | 2019-03-26 19:46:15 +0000 | [diff] [blame] | 489 | // We don't generate imports for data symbols. They however can be imported |
| 490 | // as GOT entries. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 491 | if (isa<DataSymbol>(sym)) |
Sam Clegg | d425d6b | 2019-03-12 21:53:23 +0000 | [diff] [blame] | 492 | continue; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 493 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 494 | LLVM_DEBUG(dbgs() << "import: " << sym->getName() << "\n"); |
| 495 | out.importSec->addImport(sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 499 | void Writer::calculateExports() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 500 | if (config->relocatable) |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 501 | return; |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 502 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 503 | if (!config->relocatable && !config->importMemory) |
| 504 | out.exportSec->exports.push_back( |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 505 | WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0}); |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 506 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 507 | if (!config->relocatable && config->exportTable) |
| 508 | out.exportSec->exports.push_back( |
| 509 | WasmExport{functionTableName, WASM_EXTERNAL_TABLE, 0}); |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 510 | |
Sam Clegg | 937b955 | 2019-09-24 20:52:12 +0000 | [diff] [blame] | 511 | unsigned globalIndex = |
| 512 | out.importSec->getNumImportedGlobals() + out.globalSec->numGlobals(); |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 513 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 514 | for (Symbol *sym : symtab->getSymbols()) { |
| 515 | if (!sym->isExported()) |
Nicholas Wilson | 4cdf5b8 | 2018-03-01 09:38:02 +0000 | [diff] [blame] | 516 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 517 | if (!sym->isLive()) |
Nicholas Wilson | 4cdf5b8 | 2018-03-01 09:38:02 +0000 | [diff] [blame] | 518 | continue; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 519 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 520 | StringRef name = sym->getName(); |
| 521 | WasmExport export_; |
| 522 | if (auto *f = dyn_cast<DefinedFunction>(sym)) { |
Sam Clegg | 881d877 | 2019-11-05 10:15:56 -0800 | [diff] [blame] | 523 | StringRef exportName = f->function->getExportName(); |
| 524 | if (!exportName.empty()) { |
| 525 | name = exportName; |
| 526 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 527 | export_ = {name, WASM_EXTERNAL_FUNCTION, f->getFunctionIndex()}; |
| 528 | } else if (auto *g = dyn_cast<DefinedGlobal>(sym)) { |
Sam Clegg | 177b458 | 2018-06-07 01:27:07 +0000 | [diff] [blame] | 529 | // TODO(sbc): Remove this check once to mutable global proposal is |
| 530 | // implement in all major browsers. |
| 531 | // See: https://github.com/WebAssembly/mutable-global |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 532 | if (g->getGlobalType()->Mutable) { |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 533 | // Only __stack_pointer and __tls_base should ever be create as mutable. |
| 534 | assert(g == WasmSym::stackPointer || g == WasmSym::tlsBase); |
Sam Clegg | 177b458 | 2018-06-07 01:27:07 +0000 | [diff] [blame] | 535 | continue; |
| 536 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 537 | export_ = {name, WASM_EXTERNAL_GLOBAL, g->getGlobalIndex()}; |
| 538 | } else if (auto *e = dyn_cast<DefinedEvent>(sym)) { |
| 539 | export_ = {name, WASM_EXTERNAL_EVENT, e->getEventIndex()}; |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 540 | } else { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 541 | auto *d = cast<DefinedData>(sym); |
Sam Clegg | 937b955 | 2019-09-24 20:52:12 +0000 | [diff] [blame] | 542 | out.globalSec->dataAddressGlobals.push_back(d); |
| 543 | export_ = {name, WASM_EXTERNAL_GLOBAL, globalIndex++}; |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 546 | LLVM_DEBUG(dbgs() << "Export: " << name << "\n"); |
| 547 | out.exportSec->exports.push_back(export_); |
Nicholas Wilson | 4cdf5b8 | 2018-03-01 09:38:02 +0000 | [diff] [blame] | 548 | } |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 551 | void Writer::populateSymtab() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 552 | if (!config->relocatable && !config->emitRelocs) |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 553 | return; |
| 554 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 555 | for (Symbol *sym : symtab->getSymbols()) |
| 556 | if (sym->isUsedInRegularObj && sym->isLive()) |
| 557 | out.linkingSec->addToSymtab(sym); |
Sam Clegg | 89e4dcb | 2019-01-30 18:55:15 +0000 | [diff] [blame] | 558 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 559 | for (ObjFile *file : symtab->objectFiles) { |
| 560 | LLVM_DEBUG(dbgs() << "Local symtab entries: " << file->getName() << "\n"); |
| 561 | for (Symbol *sym : file->getSymbols()) |
| 562 | if (sym->isLocal() && !isa<SectionSymbol>(sym) && sym->isLive()) |
| 563 | out.linkingSec->addToSymtab(sym); |
Sam Clegg | 89e4dcb | 2019-01-30 18:55:15 +0000 | [diff] [blame] | 564 | } |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 567 | void Writer::calculateTypes() { |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 568 | // The output type section is the union of the following sets: |
| 569 | // 1. Any signature used in the TYPE relocation |
| 570 | // 2. The signatures of all imported functions |
| 571 | // 3. The signatures of all defined functions |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 572 | // 4. The signatures of all imported events |
| 573 | // 5. The signatures of all defined events |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 574 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 575 | for (ObjFile *file : symtab->objectFiles) { |
| 576 | ArrayRef<WasmSignature> types = file->getWasmObj()->types(); |
| 577 | for (uint32_t i = 0; i < types.size(); i++) |
| 578 | if (file->typeIsUsed[i]) |
| 579 | file->typeMap[i] = out.typeSec->registerType(types[i]); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 580 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 581 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 582 | for (const Symbol *sym : out.importSec->importedSymbols) { |
| 583 | if (auto *f = dyn_cast<FunctionSymbol>(sym)) |
| 584 | out.typeSec->registerType(*f->signature); |
| 585 | else if (auto *e = dyn_cast<EventSymbol>(sym)) |
| 586 | out.typeSec->registerType(*e->signature); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 587 | } |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 588 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 589 | for (const InputFunction *f : out.functionSec->inputFunctions) |
| 590 | out.typeSec->registerType(f->signature); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 591 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 592 | for (const InputEvent *e : out.eventSec->inputEvents) |
| 593 | out.typeSec->registerType(e->signature); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 596 | static void scanRelocations() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 597 | for (ObjFile *file : symtab->objectFiles) { |
| 598 | LLVM_DEBUG(dbgs() << "scanRelocations: " << file->getName() << "\n"); |
| 599 | for (InputChunk *chunk : file->functions) |
| 600 | scanRelocations(chunk); |
| 601 | for (InputChunk *chunk : file->segments) |
| 602 | scanRelocations(chunk); |
| 603 | for (auto &p : file->customSections) |
| 604 | scanRelocations(p); |
Sam Clegg | 632c2179 | 2019-03-16 01:18:12 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 608 | void Writer::assignIndexes() { |
Sam Clegg | b9889bb | 2019-05-23 09:41:03 +0000 | [diff] [blame] | 609 | // Seal the import section, since other index spaces such as function and |
| 610 | // global are effected by the number of imports. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 611 | out.importSec->seal(); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 612 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 613 | for (InputFunction *func : symtab->syntheticFunctions) |
| 614 | out.functionSec->addFunction(func); |
Nicholas Wilson | 5639da8 | 2018-03-12 15:44:07 +0000 | [diff] [blame] | 615 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 616 | for (ObjFile *file : symtab->objectFiles) { |
| 617 | LLVM_DEBUG(dbgs() << "Functions: " << file->getName() << "\n"); |
| 618 | for (InputFunction *func : file->functions) |
| 619 | out.functionSec->addFunction(func); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 622 | for (InputGlobal *global : symtab->syntheticGlobals) |
| 623 | out.globalSec->addGlobal(global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 624 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 625 | for (ObjFile *file : symtab->objectFiles) { |
| 626 | LLVM_DEBUG(dbgs() << "Globals: " << file->getName() << "\n"); |
| 627 | for (InputGlobal *global : file->globals) |
| 628 | out.globalSec->addGlobal(global); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 629 | } |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 630 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 631 | for (ObjFile *file : symtab->objectFiles) { |
| 632 | LLVM_DEBUG(dbgs() << "Events: " << file->getName() << "\n"); |
| 633 | for (InputEvent *event : file->events) |
| 634 | out.eventSec->addEvent(event); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 635 | } |
Sam Clegg | 7185a73 | 2019-08-13 17:02:02 +0000 | [diff] [blame] | 636 | |
| 637 | out.globalSec->assignIndexes(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 640 | static StringRef getOutputDataSegmentName(StringRef name) { |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 641 | // With PIC code we currently only support a single data segment since |
| 642 | // we only have a single __memory_base to use as our base address. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 643 | if (config->isPic) |
Sam Clegg | 51c2b99 | 2019-07-09 19:47:32 +0000 | [diff] [blame] | 644 | return ".data"; |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 645 | // We only support one thread-local segment, so we must merge the segments |
| 646 | // despite --no-merge-data-segments. |
| 647 | // We also need to merge .tbss into .tdata so they share the same offsets. |
| 648 | if (name.startswith(".tdata") || name.startswith(".tbss")) |
| 649 | return ".tdata"; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 650 | if (!config->mergeDataSegments) |
| 651 | return name; |
| 652 | if (name.startswith(".text.")) |
Rui Ueyama | 4764b57 | 2018-02-28 00:57:28 +0000 | [diff] [blame] | 653 | return ".text"; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 654 | if (name.startswith(".data.")) |
Rui Ueyama | 4764b57 | 2018-02-28 00:57:28 +0000 | [diff] [blame] | 655 | return ".data"; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 656 | if (name.startswith(".bss.")) |
Rui Ueyama | 4764b57 | 2018-02-28 00:57:28 +0000 | [diff] [blame] | 657 | return ".bss"; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 658 | if (name.startswith(".rodata.")) |
Sam Clegg | 57694c5 | 2018-08-08 18:02:55 +0000 | [diff] [blame] | 659 | return ".rodata"; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 660 | return name; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | void Writer::createOutputSegments() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 664 | for (ObjFile *file : symtab->objectFiles) { |
| 665 | for (InputSegment *segment : file->segments) { |
| 666 | if (!segment->live) |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 667 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 668 | StringRef name = getOutputDataSegmentName(segment->getName()); |
| 669 | OutputSegment *&s = segmentMap[name]; |
| 670 | if (s == nullptr) { |
| 671 | LLVM_DEBUG(dbgs() << "new segment: " << name << "\n"); |
Thomas Lively | 21143b9 | 2019-09-19 01:14:59 +0000 | [diff] [blame] | 672 | s = make<OutputSegment>(name); |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 673 | if (config->sharedMemory || name == ".tdata") |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 674 | s->initFlags = WASM_SEGMENT_IS_PASSIVE; |
Thomas Lively | 190dacc | 2019-10-15 19:05:11 +0000 | [diff] [blame] | 675 | // Exported memories are guaranteed to be zero-initialized, so no need |
| 676 | // to emit data segments for bss sections. |
| 677 | // TODO: consider initializing bss sections with memory.fill |
| 678 | // instructions when memory is imported and bulk-memory is available. |
| 679 | if (!config->importMemory && !config->relocatable && |
| 680 | name.startswith(".bss")) |
| 681 | s->isBss = true; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 682 | segments.push_back(s); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 683 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 684 | s->addInputSegment(segment); |
| 685 | LLVM_DEBUG(dbgs() << "added data: " << name << ": " << s->size << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
Thomas Lively | 21143b9 | 2019-09-19 01:14:59 +0000 | [diff] [blame] | 688 | |
| 689 | // Sort segments by type, placing .bss last |
| 690 | std::stable_sort(segments.begin(), segments.end(), |
| 691 | [](const OutputSegment *a, const OutputSegment *b) { |
| 692 | auto order = [](StringRef name) { |
| 693 | return StringSwitch<int>(name) |
| 694 | .StartsWith(".rodata", 0) |
| 695 | .StartsWith(".data", 1) |
| 696 | .StartsWith(".tdata", 2) |
| 697 | .StartsWith(".bss", 4) |
| 698 | .Default(3); |
| 699 | }; |
| 700 | return order(a->name) < order(b->name); |
| 701 | }); |
| 702 | |
Thomas Lively | 0c3d4cf | 2019-09-19 21:51:52 +0000 | [diff] [blame] | 703 | for (size_t i = 0; i < segments.size(); ++i) |
Thomas Lively | 21143b9 | 2019-09-19 01:14:59 +0000 | [diff] [blame] | 704 | segments[i]->index = i; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 707 | static void createFunction(DefinedFunction *func, StringRef bodyContent) { |
| 708 | std::string functionBody; |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 709 | { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 710 | raw_string_ostream os(functionBody); |
| 711 | writeUleb128(os, bodyContent.size(), "function size"); |
| 712 | os << bodyContent; |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 713 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 714 | ArrayRef<uint8_t> body = arrayRefFromStringRef(saver.save(functionBody)); |
| 715 | cast<SyntheticFunction>(func->function)->setBody(body); |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | void Writer::createInitMemoryFunction() { |
| 719 | LLVM_DEBUG(dbgs() << "createInitMemoryFunction\n"); |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 720 | assert(WasmSym::initMemoryFlag); |
| 721 | uint32_t flagAddress = WasmSym::initMemoryFlag->getVirtualAddress(); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 722 | std::string bodyContent; |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 723 | { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 724 | raw_string_ostream os(bodyContent); |
| 725 | writeUleb128(os, 0, "num locals"); |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 726 | |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 727 | if (segments.size()) { |
| 728 | // Initialize memory in a thread-safe manner. The thread that successfully |
| 729 | // increments the flag from 0 to 1 is is responsible for performing the |
| 730 | // memory initialization. Other threads go sleep on the flag until the |
| 731 | // first thread finishing initializing memory, increments the flag to 2, |
| 732 | // and wakes all the other threads. Once the flag has been set to 2, |
| 733 | // subsequently started threads will skip the sleep. All threads |
| 734 | // unconditionally drop their passive data segments once memory has been |
| 735 | // initialized. The generated code is as follows: |
| 736 | // |
| 737 | // (func $__wasm_init_memory |
| 738 | // (if |
| 739 | // (i32.atomic.rmw.cmpxchg align=2 offset=0 |
| 740 | // (i32.const $__init_memory_flag) |
| 741 | // (i32.const 0) |
| 742 | // (i32.const 1) |
| 743 | // ) |
| 744 | // (then |
| 745 | // (drop |
| 746 | // (i32.atomic.wait align=2 offset=0 |
| 747 | // (i32.const $__init_memory_flag) |
| 748 | // (i32.const 1) |
| 749 | // (i32.const -1) |
| 750 | // ) |
| 751 | // ) |
| 752 | // ) |
| 753 | // (else |
| 754 | // ( ... initialize data segments ... ) |
| 755 | // (i32.atomic.store align=2 offset=0 |
| 756 | // (i32.const $__init_memory_flag) |
| 757 | // (i32.const 2) |
| 758 | // ) |
| 759 | // (drop |
| 760 | // (i32.atomic.notify align=2 offset=0 |
| 761 | // (i32.const $__init_memory_flag) |
| 762 | // (i32.const -1u) |
| 763 | // ) |
| 764 | // ) |
| 765 | // ) |
| 766 | // ) |
| 767 | // ( ... drop data segments ... ) |
| 768 | // ) |
| 769 | |
| 770 | // Atomically check whether this is the main thread. |
| 771 | writeI32Const(os, flagAddress, "flag address"); |
| 772 | writeI32Const(os, 0, "expected flag value"); |
| 773 | writeI32Const(os, 1, "flag value"); |
| 774 | writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); |
| 775 | writeUleb128(os, WASM_OPCODE_I32_RMW_CMPXCHG, "i32.atomic.rmw.cmpxchg"); |
| 776 | writeMemArg(os, 2, 0); |
| 777 | writeU8(os, WASM_OPCODE_IF, "IF"); |
| 778 | writeU8(os, WASM_TYPE_NORESULT, "blocktype"); |
| 779 | |
| 780 | // Did not increment 0, so wait for main thread to initialize memory |
| 781 | writeI32Const(os, flagAddress, "flag address"); |
| 782 | writeI32Const(os, 1, "expected flag value"); |
| 783 | writeI64Const(os, -1, "timeout"); |
| 784 | writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); |
| 785 | writeUleb128(os, WASM_OPCODE_I32_ATOMIC_WAIT, "i32.atomic.wait"); |
| 786 | writeMemArg(os, 2, 0); |
| 787 | writeU8(os, WASM_OPCODE_DROP, "drop"); |
| 788 | |
| 789 | writeU8(os, WASM_OPCODE_ELSE, "ELSE"); |
| 790 | |
| 791 | // Did increment 0, so conditionally initialize passive data segments |
| 792 | for (const OutputSegment *s : segments) { |
| 793 | if (s->initFlags & WASM_SEGMENT_IS_PASSIVE && s->name != ".tdata") { |
| 794 | // destination address |
| 795 | writeI32Const(os, s->startVA, "destination address"); |
| 796 | // source segment offset |
| 797 | writeI32Const(os, 0, "segment offset"); |
| 798 | // memory region size |
| 799 | writeI32Const(os, s->size, "memory region size"); |
| 800 | // memory.init instruction |
| 801 | writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix"); |
| 802 | writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "memory.init"); |
| 803 | writeUleb128(os, s->index, "segment index immediate"); |
| 804 | writeU8(os, 0, "memory index immediate"); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | // Set flag to 2 to mark end of initialization |
| 809 | writeI32Const(os, flagAddress, "flag address"); |
| 810 | writeI32Const(os, 2, "flag value"); |
| 811 | writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); |
| 812 | writeUleb128(os, WASM_OPCODE_I32_ATOMIC_STORE, "i32.atomic.store"); |
| 813 | writeMemArg(os, 2, 0); |
| 814 | |
| 815 | // Notify any waiters that memory initialization is complete |
| 816 | writeI32Const(os, flagAddress, "flag address"); |
| 817 | writeI32Const(os, -1, "number of waiters"); |
| 818 | writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); |
| 819 | writeUleb128(os, WASM_OPCODE_ATOMIC_NOTIFY, "atomic.notify"); |
| 820 | writeMemArg(os, 2, 0); |
| 821 | writeU8(os, WASM_OPCODE_DROP, "drop"); |
| 822 | |
| 823 | writeU8(os, WASM_OPCODE_END, "END"); |
| 824 | |
| 825 | // Unconditionally drop passive data segments |
| 826 | for (const OutputSegment *s : segments) { |
| 827 | if (s->initFlags & WASM_SEGMENT_IS_PASSIVE && s->name != ".tdata") { |
| 828 | // data.drop instruction |
| 829 | writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix"); |
| 830 | writeUleb128(os, WASM_OPCODE_DATA_DROP, "data.drop"); |
| 831 | writeUleb128(os, s->index, "segment index immediate"); |
| 832 | } |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 833 | } |
| 834 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 835 | writeU8(os, WASM_OPCODE_END, "END"); |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 838 | createFunction(WasmSym::initMemory, bodyContent); |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 841 | // For -shared (PIC) output, we create create a synthetic function which will |
| 842 | // apply any relocations to the data segments on startup. This function is |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 843 | // called __wasm_apply_relocs and is added at the beginning of __wasm_call_ctors |
| 844 | // before any of the constructors run. |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 845 | void Writer::createApplyRelocationsFunction() { |
| 846 | LLVM_DEBUG(dbgs() << "createApplyRelocationsFunction\n"); |
| 847 | // First write the body's contents to a string. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 848 | std::string bodyContent; |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 849 | { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 850 | raw_string_ostream os(bodyContent); |
| 851 | writeUleb128(os, 0, "num locals"); |
| 852 | for (const OutputSegment *seg : segments) |
| 853 | for (const InputSegment *inSeg : seg->inputSegments) |
| 854 | inSeg->generateRelocationCode(os); |
| 855 | writeU8(os, WASM_OPCODE_END, "END"); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 858 | createFunction(WasmSym::applyRelocs, bodyContent); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 859 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 860 | |
| 861 | // Create synthetic "__wasm_call_ctors" function based on ctor functions |
| 862 | // in input object. |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 863 | void Writer::createCallCtorsFunction() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 864 | if (!WasmSym::callCtors->isLive()) |
Sam Clegg | 0e6b42f | 2019-03-01 22:35:47 +0000 | [diff] [blame] | 865 | return; |
| 866 | |
Nicholas Wilson | f6dbc2e | 2018-03-02 14:48:50 +0000 | [diff] [blame] | 867 | // First write the body's contents to a string. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 868 | std::string bodyContent; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 869 | { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 870 | raw_string_ostream os(bodyContent); |
| 871 | writeUleb128(os, 0, "num locals"); |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 872 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 873 | if (config->isPic) { |
| 874 | writeU8(os, WASM_OPCODE_CALL, "CALL"); |
| 875 | writeUleb128(os, WasmSym::applyRelocs->getFunctionIndex(), |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 876 | "function index"); |
| 877 | } |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 878 | |
| 879 | // Call constructors |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 880 | for (const WasmInitEntry &f : initFunctions) { |
| 881 | writeU8(os, WASM_OPCODE_CALL, "CALL"); |
| 882 | writeUleb128(os, f.sym->getFunctionIndex(), "function index"); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 883 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 884 | writeU8(os, WASM_OPCODE_END, "END"); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 887 | createFunction(WasmSym::callCtors, bodyContent); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 890 | void Writer::createInitTLSFunction() { |
| 891 | if (!WasmSym::initTLS->isLive()) |
| 892 | return; |
| 893 | |
| 894 | std::string bodyContent; |
| 895 | { |
| 896 | raw_string_ostream os(bodyContent); |
| 897 | |
| 898 | OutputSegment *tlsSeg = nullptr; |
| 899 | for (auto *seg : segments) { |
Guanzhong Chen | 21aafc2 | 2019-07-18 21:18:24 +0000 | [diff] [blame] | 900 | if (seg->name == ".tdata") { |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 901 | tlsSeg = seg; |
Guanzhong Chen | 21aafc2 | 2019-07-18 21:18:24 +0000 | [diff] [blame] | 902 | break; |
| 903 | } |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | writeUleb128(os, 0, "num locals"); |
| 907 | if (tlsSeg) { |
| 908 | writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get"); |
| 909 | writeUleb128(os, 0, "local index"); |
| 910 | |
| 911 | writeU8(os, WASM_OPCODE_GLOBAL_SET, "global.set"); |
| 912 | writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "global index"); |
| 913 | |
| 914 | writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get"); |
| 915 | writeUleb128(os, 0, "local index"); |
| 916 | |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 917 | writeI32Const(os, 0, "segment offset"); |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 918 | |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 919 | writeI32Const(os, tlsSeg->size, "memory region size"); |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 920 | |
| 921 | writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix"); |
| 922 | writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "MEMORY.INIT"); |
| 923 | writeUleb128(os, tlsSeg->index, "segment index immediate"); |
| 924 | writeU8(os, 0, "memory index immediate"); |
| 925 | } |
| 926 | writeU8(os, WASM_OPCODE_END, "end function"); |
| 927 | } |
| 928 | |
| 929 | createFunction(WasmSym::initTLS, bodyContent); |
| 930 | } |
| 931 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 932 | // Populate InitFunctions vector with init functions from all input objects. |
| 933 | // This is then used either when creating the output linking section or to |
| 934 | // synthesize the "__wasm_call_ctors" function. |
| 935 | void Writer::calculateInitFunctions() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 936 | if (!config->relocatable && !WasmSym::callCtors->isLive()) |
Sam Clegg | 61f13b3 | 2019-03-02 04:55:02 +0000 | [diff] [blame] | 937 | return; |
| 938 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 939 | for (ObjFile *file : symtab->objectFiles) { |
| 940 | const WasmLinkingData &l = file->getWasmObj()->linkingData(); |
| 941 | for (const WasmInitFunc &f : l.InitFunctions) { |
| 942 | FunctionSymbol *sym = file->getFunctionSymbol(f.Symbol); |
Sam Clegg | fd54fa5 | 2019-06-07 06:00:46 +0000 | [diff] [blame] | 943 | // comdat exclusions can cause init functions be discarded. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 944 | if (sym->isDiscarded()) |
Sam Clegg | fd54fa5 | 2019-06-07 06:00:46 +0000 | [diff] [blame] | 945 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 946 | assert(sym->isLive()); |
| 947 | if (*sym->signature != WasmSignature{{}, {}}) |
| 948 | error("invalid signature for init func: " + toString(*sym)); |
Sam Clegg | accad76 | 2019-07-17 18:43:36 +0000 | [diff] [blame] | 949 | LLVM_DEBUG(dbgs() << "initFunctions: " << toString(*sym) << "\n"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 950 | initFunctions.emplace_back(WasmInitEntry{sym, f.Priority}); |
Nicholas Wilson | cb81a0c | 2018-03-02 14:46:54 +0000 | [diff] [blame] | 951 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 952 | } |
Rui Ueyama | da69b71 | 2018-02-28 00:15:59 +0000 | [diff] [blame] | 953 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 954 | // Sort in order of priority (lowest first) so that they are called |
| 955 | // in the correct order. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 956 | llvm::stable_sort(initFunctions, |
| 957 | [](const WasmInitEntry &l, const WasmInitEntry &r) { |
| 958 | return l.priority < r.priority; |
Fangrui Song | 32c0ebe | 2019-04-23 02:42:06 +0000 | [diff] [blame] | 959 | }); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 962 | void Writer::createSyntheticSections() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 963 | out.dylinkSec = make<DylinkSection>(); |
| 964 | out.typeSec = make<TypeSection>(); |
| 965 | out.importSec = make<ImportSection>(); |
| 966 | out.functionSec = make<FunctionSection>(); |
| 967 | out.tableSec = make<TableSection>(); |
| 968 | out.memorySec = make<MemorySection>(); |
| 969 | out.globalSec = make<GlobalSection>(); |
| 970 | out.eventSec = make<EventSection>(); |
| 971 | out.exportSec = make<ExportSection>(); |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 972 | out.startSec = make<StartSection>(segments.size()); |
Sam Clegg | 1a1df72 | 2019-08-27 04:19:34 +0000 | [diff] [blame] | 973 | out.elemSec = make<ElemSection>(); |
Thomas Lively | 190dacc | 2019-10-15 19:05:11 +0000 | [diff] [blame] | 974 | out.dataCountSec = make<DataCountSection>(segments); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 975 | out.linkingSec = make<LinkingSection>(initFunctions, segments); |
| 976 | out.nameSec = make<NameSection>(); |
| 977 | out.producersSec = make<ProducersSection>(); |
| 978 | out.targetFeaturesSec = make<TargetFeaturesSection>(); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 981 | void Writer::run() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 982 | if (config->relocatable || config->isPic) |
| 983 | config->globalBase = 0; |
Sam Clegg | 99eb42c | 2018-02-27 23:58:03 +0000 | [diff] [blame] | 984 | |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 985 | // For PIC code the table base is assigned dynamically by the loader. |
| 986 | // For non-PIC, we start at 1 so that accessing table index 0 always traps. |
Sam Clegg | 7185a73 | 2019-08-13 17:02:02 +0000 | [diff] [blame] | 987 | if (!config->isPic) { |
Sam Clegg | 1a1df72 | 2019-08-27 04:19:34 +0000 | [diff] [blame] | 988 | config->tableBase = 1; |
Sam Clegg | 7185a73 | 2019-08-13 17:02:02 +0000 | [diff] [blame] | 989 | if (WasmSym::definedTableBase) |
Sam Clegg | 1a1df72 | 2019-08-27 04:19:34 +0000 | [diff] [blame] | 990 | WasmSym::definedTableBase->setVirtualAddress(config->tableBase); |
Sam Clegg | 7185a73 | 2019-08-13 17:02:02 +0000 | [diff] [blame] | 991 | } |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 992 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 993 | log("-- createOutputSegments"); |
| 994 | createOutputSegments(); |
| 995 | log("-- createSyntheticSections"); |
| 996 | createSyntheticSections(); |
| 997 | log("-- populateProducers"); |
| 998 | populateProducers(); |
| 999 | log("-- populateTargetFeatures"); |
| 1000 | populateTargetFeatures(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1001 | log("-- calculateImports"); |
| 1002 | calculateImports(); |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 1003 | log("-- layoutMemory"); |
| 1004 | layoutMemory(); |
| 1005 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1006 | if (!config->relocatable) { |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 1007 | // Create linker synthesized __start_SECNAME/__stop_SECNAME symbols |
| 1008 | // This has to be done after memory layout is performed. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1009 | for (const OutputSegment *seg : segments) |
| 1010 | addStartStopSymbols(seg); |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Sam Clegg | b9889bb | 2019-05-23 09:41:03 +0000 | [diff] [blame] | 1013 | log("-- scanRelocations"); |
| 1014 | scanRelocations(); |
Sam Clegg | 7804dbd | 2019-05-21 10:07:30 +0000 | [diff] [blame] | 1015 | log("-- assignIndexes"); |
| 1016 | assignIndexes(); |
Sam Clegg | 7804dbd | 2019-05-21 10:07:30 +0000 | [diff] [blame] | 1017 | log("-- calculateInitFunctions"); |
| 1018 | calculateInitFunctions(); |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 1019 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1020 | if (!config->relocatable) { |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 1021 | // Create linker synthesized functions |
Thomas Lively | 09768c5 | 2019-09-04 19:50:39 +0000 | [diff] [blame] | 1022 | if (config->sharedMemory) |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 +0000 | [diff] [blame] | 1023 | createInitMemoryFunction(); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1024 | if (config->isPic) |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 1025 | createApplyRelocationsFunction(); |
| 1026 | createCallCtorsFunction(); |
| 1027 | } |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 1028 | |
Guanzhong Chen | 0cb776e | 2019-08-06 20:09:04 +0000 | [diff] [blame] | 1029 | if (!config->relocatable && config->sharedMemory && !config->shared) |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 1030 | createInitTLSFunction(); |
| 1031 | |
| 1032 | if (errorCount()) |
| 1033 | return; |
| 1034 | |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 1035 | log("-- calculateTypes"); |
| 1036 | calculateTypes(); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 1037 | log("-- calculateExports"); |
| 1038 | calculateExports(); |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 1039 | log("-- calculateCustomSections"); |
| 1040 | calculateCustomSections(); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 1041 | log("-- populateSymtab"); |
| 1042 | populateSymtab(); |
| 1043 | log("-- addSections"); |
| 1044 | addSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1045 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1046 | if (errorHandler().verbose) { |
| 1047 | log("Defined Functions: " + Twine(out.functionSec->inputFunctions.size())); |
Sam Clegg | 937b955 | 2019-09-24 20:52:12 +0000 | [diff] [blame] | 1048 | log("Defined Globals : " + Twine(out.globalSec->numGlobals())); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1049 | log("Defined Events : " + Twine(out.eventSec->inputEvents.size())); |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 1050 | log("Function Imports : " + |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1051 | Twine(out.importSec->getNumImportedFunctions())); |
| 1052 | log("Global Imports : " + Twine(out.importSec->getNumImportedGlobals())); |
| 1053 | log("Event Imports : " + Twine(out.importSec->getNumImportedEvents())); |
| 1054 | for (ObjFile *file : symtab->objectFiles) |
| 1055 | file->dumpInfo(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1058 | createHeader(); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 1059 | log("-- finalizeSections"); |
| 1060 | finalizeSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1061 | |
| 1062 | log("-- openFile"); |
| 1063 | openFile(); |
| 1064 | if (errorCount()) |
| 1065 | return; |
| 1066 | |
| 1067 | writeHeader(); |
| 1068 | |
| 1069 | log("-- writeSections"); |
| 1070 | writeSections(); |
| 1071 | if (errorCount()) |
| 1072 | return; |
| 1073 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1074 | if (Error e = buffer->commit()) |
| 1075 | fatal("failed to write the output file: " + toString(std::move(e))); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | // Open a result file. |
| 1079 | void Writer::openFile() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1080 | log("writing: " + config->outputFile); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1081 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1082 | Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr = |
| 1083 | FileOutputBuffer::create(config->outputFile, fileSize, |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1084 | FileOutputBuffer::F_executable); |
| 1085 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1086 | if (!bufferOrErr) |
| 1087 | error("failed to open " + config->outputFile + ": " + |
| 1088 | toString(bufferOrErr.takeError())); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1089 | else |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1090 | buffer = std::move(*bufferOrErr); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | void Writer::createHeader() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1094 | raw_string_ostream os(header); |
| 1095 | writeBytes(os, WasmMagic, sizeof(WasmMagic), "wasm magic"); |
| 1096 | writeU32(os, WasmVersion, "wasm version"); |
| 1097 | os.flush(); |
| 1098 | fileSize += header.size(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Fangrui Song | 33c59ab | 2019-10-10 05:25:39 +0000 | [diff] [blame] | 1101 | void writeResult() { Writer().run(); } |
| 1102 | |
| 1103 | } // namespace wasm |
| 1104 | } // namespace lld |