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