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 | |
Heejin Ahn | a1cc4ea | 2019-02-04 19:13:46 +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 | |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 57 | void createApplyRelocationsFunction(); |
| 58 | void createCallCtorsFunction(); |
| 59 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 60 | void assignIndexes(); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 61 | void populateSymtab(); |
| 62 | void populateProducers(); |
| 63 | void populateTargetFeatures(); |
| 64 | void calculateInitFunctions(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 65 | void calculateImports(); |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 66 | void calculateExports(); |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 67 | void calculateCustomSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 68 | void calculateTypes(); |
| 69 | void createOutputSegments(); |
| 70 | void layoutMemory(); |
| 71 | void createHeader(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 72 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 73 | void addSection(OutputSection *Sec); |
| 74 | |
| 75 | void addSections(); |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 76 | void addStartStopSymbols(const InputSegment *Seg); |
| 77 | |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 78 | void createCustomSections(); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 79 | void createSyntheticSections(); |
| 80 | void finalizeSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 81 | |
| 82 | // Custom sections |
| 83 | void createRelocSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 84 | |
| 85 | void writeHeader(); |
| 86 | void writeSections(); |
| 87 | |
| 88 | uint64_t FileSize = 0; |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 89 | uint32_t TableBase = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 90 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 91 | std::vector<WasmInitEntry> InitFunctions; |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 92 | llvm::StringMap<std::vector<InputSection *>> CustomSectionMapping; |
| 93 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 94 | // Elements that are used to construct the final output |
| 95 | std::string Header; |
| 96 | std::vector<OutputSection *> OutputSections; |
| 97 | |
| 98 | std::unique_ptr<FileOutputBuffer> Buffer; |
| 99 | |
| 100 | std::vector<OutputSegment *> Segments; |
| 101 | llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap; |
| 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"); |
| 108 | bool StripDebug = Config->StripDebug || Config->StripAll; |
| 109 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 110 | for (InputSection *Section : File->CustomSections) { |
| 111 | StringRef Name = Section->getName(); |
| 112 | // These custom sections are known the linker and synthesized rather than |
| 113 | // blindly copied |
Thomas Lively | 2a0868f | 2019-01-17 02:29:41 +0000 | [diff] [blame] | 114 | if (Name == "linking" || Name == "name" || Name == "producers" || |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 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 |
| 118 | if (StripDebug && Name.startswith(".debug_")) |
| 119 | continue; |
| 120 | CustomSectionMapping[Name].push_back(Section); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 125 | void Writer::createCustomSections() { |
| 126 | log("createCustomSections"); |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 127 | for (auto &Pair : CustomSectionMapping) { |
| 128 | StringRef Name = Pair.first(); |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 129 | LLVM_DEBUG(dbgs() << "createCustomSection: " << Name << "\n"); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 130 | |
| 131 | OutputSection *Sec = make<CustomSection>(Name, Pair.second); |
Sam Clegg | 35be7ff | 2019-05-24 13:28:27 +0000 | [diff] [blame] | 132 | if (Config->Relocatable || Config->EmitRelocs) { |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 133 | auto *Sym = make<OutputSectionSymbol>(Sec); |
| 134 | Out.LinkingSec->addToSymtab(Sym); |
| 135 | Sec->SectionSym = Sym; |
| 136 | } |
| 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 |
| 146 | size_t OrigSize = OutputSections.size(); |
Rui Ueyama | ffa650a | 2018-04-24 23:09:57 +0000 | [diff] [blame] | 147 | for (size_t I = 0; I < OrigSize; I++) { |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 148 | LLVM_DEBUG(dbgs() << "check section " << I << "\n"); |
| 149 | OutputSection *Sec = OutputSections[I]; |
| 150 | |
| 151 | // Count the number of needed sections. |
| 152 | uint32_t Count = Sec->numRelocations(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 153 | if (!Count) |
| 154 | continue; |
| 155 | |
Rui Ueyama | 3725406 | 2018-02-28 00:01:31 +0000 | [diff] [blame] | 156 | StringRef Name; |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 157 | if (Sec->Type == WASM_SEC_DATA) |
Rui Ueyama | 3725406 | 2018-02-28 00:01:31 +0000 | [diff] [blame] | 158 | Name = "reloc.DATA"; |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 159 | else if (Sec->Type == WASM_SEC_CODE) |
Rui Ueyama | 3725406 | 2018-02-28 00:01:31 +0000 | [diff] [blame] | 160 | Name = "reloc.CODE"; |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 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 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +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() { |
Thomas Lively | 2a0868f | 2019-01-17 02:29:41 +0000 | [diff] [blame] | 172 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 173 | const WasmProducerInfo &Info = File->getWasmObj()->getProducerInfo(); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 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() { |
| 179 | memcpy(Buffer->getBufferStart(), Header.data(), Header.size()); |
| 180 | } |
| 181 | |
| 182 | void Writer::writeSections() { |
| 183 | uint8_t *Buf = Buffer->getBufferStart(); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 184 | parallelForEach(OutputSections, [Buf](OutputSection *S) { |
| 185 | assert(S->isNeeded()); |
| 186 | S->writeTo(Buf); |
| 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 | // |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 194 | // - initialized data (starting at Config->GlobalBase) |
| 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() { |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 204 | uint32_t MemoryPtr = 0; |
| 205 | |
| 206 | auto PlaceStack = [&]() { |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 207 | if (Config->Relocatable || Config->Shared) |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 208 | return; |
Heejin Ahn | a1cc4ea | 2019-02-04 19:13:46 +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"); |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 212 | log("mem: stack size = " + Twine(Config->ZStackSize)); |
| 213 | log("mem: stack base = " + Twine(MemoryPtr)); |
| 214 | MemoryPtr += Config->ZStackSize; |
Sam Clegg | 2dad4e2 | 2018-11-15 18:15:54 +0000 | [diff] [blame] | 215 | auto *SP = cast<DefinedGlobal>(WasmSym::StackPointer); |
| 216 | SP->Global->Global.InitExpr.Value.Int32 = MemoryPtr; |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 217 | log("mem: stack top = " + Twine(MemoryPtr)); |
| 218 | }; |
| 219 | |
| 220 | if (Config->StackFirst) { |
| 221 | PlaceStack(); |
| 222 | } else { |
| 223 | MemoryPtr = Config->GlobalBase; |
| 224 | log("mem: global base = " + Twine(Config->GlobalBase)); |
| 225 | } |
| 226 | |
| 227 | uint32_t DataStart = MemoryPtr; |
| 228 | |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 229 | // Arbitrarily set __dso_handle handle to point to the start of the data |
| 230 | // segments. |
| 231 | if (WasmSym::DsoHandle) |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 232 | WasmSym::DsoHandle->setVirtualAddress(DataStart); |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 233 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 234 | Out.DylinkSec->MemAlign = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 235 | for (OutputSegment *Seg : Segments) { |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 236 | Out.DylinkSec->MemAlign = std::max(Out.DylinkSec->MemAlign, Seg->Alignment); |
Sam Clegg | 622ad04 | 2019-01-17 22:09:09 +0000 | [diff] [blame] | 237 | MemoryPtr = alignTo(MemoryPtr, 1ULL << Seg->Alignment); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 238 | Seg->StartVA = MemoryPtr; |
Nicholas Wilson | a06a355 | 2018-03-14 13:50:20 +0000 | [diff] [blame] | 239 | log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", Seg->Name, |
| 240 | MemoryPtr, Seg->Size, Seg->Alignment)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 241 | MemoryPtr += Seg->Size; |
| 242 | } |
| 243 | |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 244 | // TODO: Add .bss space here. |
Sam Clegg | 37a4a8a | 2018-02-07 03:04:53 +0000 | [diff] [blame] | 245 | if (WasmSym::DataEnd) |
| 246 | WasmSym::DataEnd->setVirtualAddress(MemoryPtr); |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 247 | |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 248 | log("mem: static data = " + Twine(MemoryPtr - DataStart)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 249 | |
Sam Clegg | 2dad4e2 | 2018-11-15 18:15:54 +0000 | [diff] [blame] | 250 | if (Config->Shared) { |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 251 | Out.DylinkSec->MemSize = MemoryPtr; |
Sam Clegg | 2dad4e2 | 2018-11-15 18:15:54 +0000 | [diff] [blame] | 252 | return; |
| 253 | } |
| 254 | |
Sam Clegg | a0f095e | 2018-05-03 17:21:53 +0000 | [diff] [blame] | 255 | if (!Config->StackFirst) |
| 256 | PlaceStack(); |
| 257 | |
| 258 | // Set `__heap_base` to directly follow the end of the stack or global data. |
| 259 | // The fact that this comes last means that a malloc/brk implementation |
| 260 | // can grow the heap at runtime. |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 261 | if (!Config->Relocatable) { |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 262 | WasmSym::HeapBase->setVirtualAddress(MemoryPtr); |
Nicholas Wilson | a06a355 | 2018-03-14 13:50:20 +0000 | [diff] [blame] | 263 | log("mem: heap base = " + Twine(MemoryPtr)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 266 | if (Config->InitialMemory != 0) { |
| 267 | if (Config->InitialMemory != alignTo(Config->InitialMemory, WasmPageSize)) |
| 268 | error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned"); |
| 269 | if (MemoryPtr > Config->InitialMemory) |
| 270 | error("initial memory too small, " + Twine(MemoryPtr) + " bytes needed"); |
| 271 | else |
| 272 | MemoryPtr = Config->InitialMemory; |
| 273 | } |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 274 | Out.DylinkSec->MemSize = MemoryPtr; |
| 275 | Out.MemorySec->NumMemoryPages = |
| 276 | alignTo(MemoryPtr, WasmPageSize) / WasmPageSize; |
| 277 | log("mem: total pages = " + Twine(Out.MemorySec->NumMemoryPages)); |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 278 | |
Thomas Lively | 06391f3 | 2019-03-29 20:43:49 +0000 | [diff] [blame] | 279 | // Check max if explicitly supplied or required by shared memory |
| 280 | if (Config->MaxMemory != 0 || Config->SharedMemory) { |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 281 | if (Config->MaxMemory != alignTo(Config->MaxMemory, WasmPageSize)) |
| 282 | error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned"); |
| 283 | if (MemoryPtr > Config->MaxMemory) |
| 284 | error("maximum memory too small, " + Twine(MemoryPtr) + " bytes needed"); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 285 | Out.MemorySec->MaxMemoryPages = Config->MaxMemory / WasmPageSize; |
| 286 | log("mem: max pages = " + Twine(Out.MemorySec->MaxMemoryPages)); |
Nicholas Wilson | 2eb39c1 | 2018-03-14 13:53:58 +0000 | [diff] [blame] | 287 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 290 | void Writer::addSection(OutputSection *Sec) { |
| 291 | if (!Sec->isNeeded()) |
| 292 | return; |
| 293 | log("addSection: " + toString(*Sec)); |
| 294 | Sec->SectionIndex = OutputSections.size(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 295 | OutputSections.push_back(Sec); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 298 | // If a section name is valid as a C identifier (which is rare because of |
| 299 | // the leading '.'), linkers are expected to define __start_<secname> and |
| 300 | // __stop_<secname> symbols. They are at beginning and end of the section, |
| 301 | // respectively. This is not requested by the ELF standard, but GNU ld and |
| 302 | // gold provide the feature, and used by many programs. |
| 303 | void Writer::addStartStopSymbols(const InputSegment *Seg) { |
| 304 | StringRef S = Seg->getName(); |
| 305 | LLVM_DEBUG(dbgs() << "addStartStopSymbols: " << S << "\n"); |
| 306 | if (!isValidCIdentifier(S)) |
| 307 | return; |
| 308 | uint32_t Start = Seg->OutputSeg->StartVA + Seg->OutputSegmentOffset; |
| 309 | uint32_t Stop = Start + Seg->getSize(); |
| 310 | Symtab->addOptionalDataSymbol(Saver.save("__start_" + S), Start, 0); |
| 311 | Symtab->addOptionalDataSymbol(Saver.save("__stop_" + S), Stop, 0); |
| 312 | } |
| 313 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 314 | void Writer::addSections() { |
| 315 | addSection(Out.DylinkSec); |
| 316 | addSection(Out.TypeSec); |
| 317 | addSection(Out.ImportSec); |
| 318 | addSection(Out.FunctionSec); |
| 319 | addSection(Out.TableSec); |
| 320 | addSection(Out.MemorySec); |
| 321 | addSection(Out.GlobalSec); |
| 322 | addSection(Out.EventSec); |
| 323 | addSection(Out.ExportSec); |
| 324 | addSection(Out.ElemSec); |
| 325 | addSection(Out.DataCountSec); |
| 326 | |
| 327 | addSection(make<CodeSection>(Out.FunctionSec->InputFunctions)); |
| 328 | addSection(make<DataSection>(Segments)); |
| 329 | |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 330 | createCustomSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 331 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 332 | addSection(Out.LinkingSec); |
Sam Clegg | 35be7ff | 2019-05-24 13:28:27 +0000 | [diff] [blame] | 333 | if (Config->EmitRelocs || Config->Relocatable) { |
Nicholas Wilson | 94d3b16 | 2018-03-05 12:33:58 +0000 | [diff] [blame] | 334 | createRelocSections(); |
Sam Clegg | 99eb42c | 2018-02-27 23:58:03 +0000 | [diff] [blame] | 335 | } |
Thomas Lively | 2a0868f | 2019-01-17 02:29:41 +0000 | [diff] [blame] | 336 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 337 | addSection(Out.NameSec); |
| 338 | addSection(Out.ProducersSec); |
| 339 | addSection(Out.TargetFeaturesSec); |
| 340 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 341 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 342 | void Writer::finalizeSections() { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 343 | for (OutputSection *S : OutputSections) { |
| 344 | S->setOffset(FileSize); |
| 345 | S->finalizeContents(); |
| 346 | FileSize += S->getSize(); |
| 347 | } |
| 348 | } |
| 349 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 350 | void Writer::populateTargetFeatures() { |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 351 | StringMap<std::string> Used; |
| 352 | StringMap<std::string> Required; |
| 353 | StringMap<std::string> Disallowed; |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 354 | |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 355 | // Only infer used features if user did not specify features |
| 356 | bool InferFeatures = !Config->Features.hasValue(); |
| 357 | |
| 358 | if (!InferFeatures) { |
| 359 | for (auto &Feature : Config->Features.getValue()) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 360 | Out.TargetFeaturesSec->Features.insert(Feature); |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 361 | // No need to read or check features |
| 362 | if (!Config->CheckFeatures) |
| 363 | return; |
| 364 | } |
| 365 | |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 366 | // Find the sets of used, required, and disallowed features |
| 367 | for (ObjFile *File : Symtab->ObjectFiles) { |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 368 | StringRef FileName(File->getName()); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 369 | for (auto &Feature : File->getWasmObj()->getTargetFeatures()) { |
| 370 | switch (Feature.Prefix) { |
| 371 | case WASM_FEATURE_PREFIX_USED: |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 372 | Used.insert({Feature.Name, FileName}); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 373 | break; |
| 374 | case WASM_FEATURE_PREFIX_REQUIRED: |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 375 | Used.insert({Feature.Name, FileName}); |
| 376 | Required.insert({Feature.Name, FileName}); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 377 | break; |
| 378 | case WASM_FEATURE_PREFIX_DISALLOWED: |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 379 | Disallowed.insert({Feature.Name, FileName}); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 380 | break; |
| 381 | default: |
| 382 | error("Unrecognized feature policy prefix " + |
| 383 | std::to_string(Feature.Prefix)); |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 388 | if (InferFeatures) |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 389 | Out.TargetFeaturesSec->Features.insert(Used.keys().begin(), |
| 390 | Used.keys().end()); |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 391 | |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 392 | if (Out.TargetFeaturesSec->Features.count("atomics") && |
| 393 | !Config->SharedMemory) { |
| 394 | if (InferFeatures) |
| 395 | error(Twine("'atomics' feature is used by ") + Used["atomics"] + |
| 396 | ", so --shared-memory must be used"); |
| 397 | else |
| 398 | error("'atomics' feature is used, so --shared-memory must be used"); |
| 399 | } |
Thomas Lively | 06391f3 | 2019-03-29 20:43:49 +0000 | [diff] [blame] | 400 | |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 401 | if (!Config->CheckFeatures) |
| 402 | return; |
| 403 | |
Thomas Lively | 06391f3 | 2019-03-29 20:43:49 +0000 | [diff] [blame] | 404 | if (Disallowed.count("atomics") && Config->SharedMemory) |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 405 | error("'atomics' feature is disallowed by " + Disallowed["atomics"] + |
| 406 | ", so --shared-memory must not be used"); |
Thomas Lively | 06391f3 | 2019-03-29 20:43:49 +0000 | [diff] [blame] | 407 | |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 408 | // Validate that used features are allowed in output |
| 409 | if (!InferFeatures) { |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 410 | for (auto &Feature : Used.keys()) { |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 411 | if (!Out.TargetFeaturesSec->Features.count(Feature)) |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 412 | error(Twine("Target feature '") + Feature + "' used by " + |
| 413 | Used[Feature] + " is not allowed."); |
Thomas Lively | 82de51a | 2019-03-26 04:11:05 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 417 | // Validate the required and disallowed constraints for each file |
| 418 | for (ObjFile *File : Symtab->ObjectFiles) { |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 419 | StringRef FileName(File->getName()); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 420 | SmallSet<std::string, 8> ObjectFeatures; |
| 421 | for (auto &Feature : File->getWasmObj()->getTargetFeatures()) { |
| 422 | if (Feature.Prefix == WASM_FEATURE_PREFIX_DISALLOWED) |
| 423 | continue; |
| 424 | ObjectFeatures.insert(Feature.Name); |
| 425 | if (Disallowed.count(Feature.Name)) |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 426 | error(Twine("Target feature '") + Feature.Name + "' used in " + |
| 427 | FileName + " is disallowed by " + Disallowed[Feature.Name] + |
| 428 | ". Use --no-check-features to suppress."); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 429 | } |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 430 | for (auto &Feature : Required.keys()) { |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 431 | if (!ObjectFeatures.count(Feature)) |
Thomas Lively | 86e73f5 | 2019-05-30 21:57:23 +0000 | [diff] [blame^] | 432 | error(Twine("Missing target feature '") + Feature + "' in " + FileName + |
| 433 | ", required by " + Required[Feature] + |
| 434 | ". Use --no-check-features to suppress."); |
Thomas Lively | f6f4f84 | 2019-03-20 20:26:45 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 439 | void Writer::calculateImports() { |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 440 | for (Symbol *Sym : Symtab->getSymbols()) { |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 441 | if (!Sym->isUndefined()) |
| 442 | continue; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 443 | if (Sym->isWeak() && !Config->Relocatable) |
Sam Clegg | 574d7ce | 2017-12-15 19:23:49 +0000 | [diff] [blame] | 444 | continue; |
Nicholas Wilson | a1e299f | 2018-04-20 17:18:06 +0000 | [diff] [blame] | 445 | if (!Sym->isLive()) |
| 446 | continue; |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 447 | if (!Sym->IsUsedInRegularObj) |
| 448 | continue; |
Sam Clegg | 492f752 | 2019-03-26 19:46:15 +0000 | [diff] [blame] | 449 | // We don't generate imports for data symbols. They however can be imported |
| 450 | // as GOT entries. |
| 451 | if (isa<DataSymbol>(Sym)) |
Sam Clegg | d425d6b | 2019-03-12 21:53:23 +0000 | [diff] [blame] | 452 | continue; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 453 | |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 454 | LLVM_DEBUG(dbgs() << "import: " << Sym->getName() << "\n"); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 455 | Out.ImportSec->addImport(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 459 | void Writer::calculateExports() { |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 460 | if (Config->Relocatable) |
| 461 | return; |
Sam Clegg | f0d433d | 2018-02-02 22:59:56 +0000 | [diff] [blame] | 462 | |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 463 | if (!Config->Relocatable && !Config->ImportMemory) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 464 | Out.ExportSec->Exports.push_back( |
| 465 | WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0}); |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 466 | |
| 467 | if (!Config->Relocatable && Config->ExportTable) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 468 | Out.ExportSec->Exports.push_back( |
| 469 | WasmExport{FunctionTableName, WASM_EXTERNAL_TABLE, 0}); |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 470 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 471 | unsigned FakeGlobalIndex = |
Sam Clegg | b9889bb | 2019-05-23 09:41:03 +0000 | [diff] [blame] | 472 | Out.ImportSec->numImportedGlobals() + Out.GlobalSec->InputGlobals.size(); |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 473 | |
Nicholas Wilson | 4cdf5b8 | 2018-03-01 09:38:02 +0000 | [diff] [blame] | 474 | for (Symbol *Sym : Symtab->getSymbols()) { |
Sam Clegg | ce004bf | 2018-06-28 17:04:58 +0000 | [diff] [blame] | 475 | if (!Sym->isExported()) |
Nicholas Wilson | 4cdf5b8 | 2018-03-01 09:38:02 +0000 | [diff] [blame] | 476 | continue; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 477 | if (!Sym->isLive()) |
Nicholas Wilson | 4cdf5b8 | 2018-03-01 09:38:02 +0000 | [diff] [blame] | 478 | continue; |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 479 | |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 480 | StringRef Name = Sym->getName(); |
| 481 | WasmExport Export; |
| 482 | if (auto *F = dyn_cast<DefinedFunction>(Sym)) { |
| 483 | Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()}; |
| 484 | } else if (auto *G = dyn_cast<DefinedGlobal>(Sym)) { |
Sam Clegg | 177b458 | 2018-06-07 01:27:07 +0000 | [diff] [blame] | 485 | // TODO(sbc): Remove this check once to mutable global proposal is |
| 486 | // implement in all major browsers. |
| 487 | // See: https://github.com/WebAssembly/mutable-global |
| 488 | if (G->getGlobalType()->Mutable) { |
| 489 | // Only the __stack_pointer should ever be create as mutable. |
| 490 | assert(G == WasmSym::StackPointer); |
| 491 | continue; |
| 492 | } |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 493 | Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()}; |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 494 | } else if (auto *E = dyn_cast<DefinedEvent>(Sym)) { |
| 495 | Export = {Name, WASM_EXTERNAL_EVENT, E->getEventIndex()}; |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 496 | } else { |
| 497 | auto *D = cast<DefinedData>(Sym); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 498 | Out.GlobalSec->DefinedFakeGlobals.emplace_back(D); |
Sam Clegg | d6beb32 | 2018-05-10 18:10:34 +0000 | [diff] [blame] | 499 | Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++}; |
| 500 | } |
| 501 | |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 502 | LLVM_DEBUG(dbgs() << "Export: " << Name << "\n"); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 503 | Out.ExportSec->Exports.push_back(Export); |
Nicholas Wilson | 4cdf5b8 | 2018-03-01 09:38:02 +0000 | [diff] [blame] | 504 | } |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 507 | void Writer::populateSymtab() { |
Sam Clegg | 35be7ff | 2019-05-24 13:28:27 +0000 | [diff] [blame] | 508 | if (!Config->Relocatable && !Config->EmitRelocs) |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 509 | return; |
| 510 | |
Sam Clegg | 89e4dcb | 2019-01-30 18:55:15 +0000 | [diff] [blame] | 511 | for (Symbol *Sym : Symtab->getSymbols()) |
Sam Clegg | 35be7ff | 2019-05-24 13:28:27 +0000 | [diff] [blame] | 512 | if (Sym->IsUsedInRegularObj && Sym->isLive()) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 513 | Out.LinkingSec->addToSymtab(Sym); |
Sam Clegg | 89e4dcb | 2019-01-30 18:55:15 +0000 | [diff] [blame] | 514 | |
| 515 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 516 | LLVM_DEBUG(dbgs() << "Local symtab entries: " << File->getName() << "\n"); |
| 517 | for (Symbol *Sym : File->getSymbols()) |
Sam Clegg | 35be7ff | 2019-05-24 13:28:27 +0000 | [diff] [blame] | 518 | if (Sym->isLocal() && !isa<SectionSymbol>(Sym) && Sym->isLive()) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 519 | Out.LinkingSec->addToSymtab(Sym); |
Sam Clegg | 89e4dcb | 2019-01-30 18:55:15 +0000 | [diff] [blame] | 520 | } |
Sam Clegg | d3052d5 | 2018-01-18 23:40:49 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 523 | void Writer::calculateTypes() { |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 524 | // The output type section is the union of the following sets: |
| 525 | // 1. Any signature used in the TYPE relocation |
| 526 | // 2. The signatures of all imported functions |
| 527 | // 3. The signatures of all defined functions |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 528 | // 4. The signatures of all imported events |
| 529 | // 5. The signatures of all defined events |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 530 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 531 | for (ObjFile *File : Symtab->ObjectFiles) { |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 532 | ArrayRef<WasmSignature> Types = File->getWasmObj()->types(); |
| 533 | for (uint32_t I = 0; I < Types.size(); I++) |
| 534 | if (File->TypeIsUsed[I]) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 535 | File->TypeMap[I] = Out.TypeSec->registerType(Types[I]); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 536 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 537 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 538 | for (const Symbol *Sym : Out.ImportSec->ImportedSymbols) { |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 539 | if (auto *F = dyn_cast<FunctionSymbol>(Sym)) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 540 | Out.TypeSec->registerType(*F->Signature); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 541 | else if (auto *E = dyn_cast<EventSymbol>(Sym)) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 542 | Out.TypeSec->registerType(*E->Signature); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 543 | } |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 544 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 545 | for (const InputFunction *F : Out.FunctionSec->InputFunctions) |
| 546 | Out.TypeSec->registerType(F->Signature); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 547 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 548 | for (const InputEvent *E : Out.EventSec->InputEvents) |
| 549 | Out.TypeSec->registerType(E->Signature); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 552 | static void scanRelocations() { |
| 553 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 554 | LLVM_DEBUG(dbgs() << "scanRelocations: " << File->getName() << "\n"); |
| 555 | for (InputChunk *Chunk : File->Functions) |
| 556 | scanRelocations(Chunk); |
| 557 | for (InputChunk *Chunk : File->Segments) |
| 558 | scanRelocations(Chunk); |
| 559 | for (auto &P : File->CustomSections) |
| 560 | scanRelocations(P); |
Sam Clegg | 632c2179 | 2019-03-16 01:18:12 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 564 | void Writer::assignIndexes() { |
Sam Clegg | b9889bb | 2019-05-23 09:41:03 +0000 | [diff] [blame] | 565 | // Seal the import section, since other index spaces such as function and |
| 566 | // global are effected by the number of imports. |
| 567 | Out.ImportSec->seal(); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 568 | |
Nicholas Wilson | 5639da8 | 2018-03-12 15:44:07 +0000 | [diff] [blame] | 569 | for (InputFunction *Func : Symtab->SyntheticFunctions) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 570 | Out.FunctionSec->addFunction(Func); |
Nicholas Wilson | 5639da8 | 2018-03-12 15:44:07 +0000 | [diff] [blame] | 571 | |
Sam Clegg | 87e6192 | 2018-01-08 23:39:11 +0000 | [diff] [blame] | 572 | for (ObjFile *File : Symtab->ObjectFiles) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 573 | LLVM_DEBUG(dbgs() << "Functions: " << File->getName() << "\n"); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 574 | for (InputFunction *Func : File->Functions) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 575 | Out.FunctionSec->addFunction(Func); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 578 | for (InputGlobal *Global : Symtab->SyntheticGlobals) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 579 | Out.GlobalSec->addGlobal(Global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 580 | |
| 581 | for (ObjFile *File : Symtab->ObjectFiles) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 582 | LLVM_DEBUG(dbgs() << "Globals: " << File->getName() << "\n"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 583 | for (InputGlobal *Global : File->Globals) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 584 | Out.GlobalSec->addGlobal(Global); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 585 | } |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 586 | |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 587 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 588 | LLVM_DEBUG(dbgs() << "Events: " << File->getName() << "\n"); |
| 589 | for (InputEvent *Event : File->Events) |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 590 | Out.EventSec->addEvent(Event); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 591 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | static StringRef getOutputDataSegmentName(StringRef Name) { |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 595 | // With PIC code we currently only support a single data segment since |
| 596 | // we only have a single __memory_base to use as our base address. |
| 597 | if (Config->Pic) |
| 598 | return "data"; |
Sam Clegg | 6684476 | 2018-05-10 18:23:51 +0000 | [diff] [blame] | 599 | if (!Config->MergeDataSegments) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 600 | return Name; |
Rui Ueyama | 4764b57 | 2018-02-28 00:57:28 +0000 | [diff] [blame] | 601 | if (Name.startswith(".text.")) |
| 602 | return ".text"; |
| 603 | if (Name.startswith(".data.")) |
| 604 | return ".data"; |
| 605 | if (Name.startswith(".bss.")) |
| 606 | return ".bss"; |
Sam Clegg | 57694c5 | 2018-08-08 18:02:55 +0000 | [diff] [blame] | 607 | if (Name.startswith(".rodata.")) |
| 608 | return ".rodata"; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 609 | return Name; |
| 610 | } |
| 611 | |
| 612 | void Writer::createOutputSegments() { |
| 613 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 614 | for (InputSegment *Segment : File->Segments) { |
Sam Clegg | 447ae40 | 2018-02-13 20:29:38 +0000 | [diff] [blame] | 615 | if (!Segment->Live) |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 616 | continue; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 617 | StringRef Name = getOutputDataSegmentName(Segment->getName()); |
| 618 | OutputSegment *&S = SegmentMap[Name]; |
| 619 | if (S == nullptr) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 620 | LLVM_DEBUG(dbgs() << "new segment: " << Name << "\n"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 621 | S = make<OutputSegment>(Name, Segments.size()); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 622 | Segments.push_back(S); |
| 623 | } |
| 624 | S->addInputSegment(Segment); |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 625 | LLVM_DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 630 | // For -shared (PIC) output, we create create a synthetic function which will |
| 631 | // apply any relocations to the data segments on startup. This function is |
| 632 | // called __wasm_apply_relocs and is added at the very beginning of |
| 633 | // __wasm_call_ctors before any of the constructors run. |
| 634 | void Writer::createApplyRelocationsFunction() { |
| 635 | LLVM_DEBUG(dbgs() << "createApplyRelocationsFunction\n"); |
| 636 | // First write the body's contents to a string. |
| 637 | std::string BodyContent; |
| 638 | { |
| 639 | raw_string_ostream OS(BodyContent); |
| 640 | writeUleb128(OS, 0, "num locals"); |
| 641 | for (const OutputSegment *Seg : Segments) |
| 642 | for (const InputSegment *InSeg : Seg->InputSegments) |
| 643 | InSeg->generateRelocationCode(OS); |
| 644 | writeU8(OS, WASM_OPCODE_END, "END"); |
| 645 | } |
| 646 | |
| 647 | // Once we know the size of the body we can create the final function body |
| 648 | std::string FunctionBody; |
| 649 | { |
| 650 | raw_string_ostream OS(FunctionBody); |
| 651 | writeUleb128(OS, BodyContent.size(), "function size"); |
| 652 | OS << BodyContent; |
| 653 | } |
| 654 | |
| 655 | ArrayRef<uint8_t> Body = arrayRefFromStringRef(Saver.save(FunctionBody)); |
| 656 | cast<SyntheticFunction>(WasmSym::ApplyRelocs->Function)->setBody(Body); |
| 657 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 658 | |
| 659 | // Create synthetic "__wasm_call_ctors" function based on ctor functions |
| 660 | // in input object. |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 661 | void Writer::createCallCtorsFunction() { |
Sam Clegg | 0e6b42f | 2019-03-01 22:35:47 +0000 | [diff] [blame] | 662 | if (!WasmSym::CallCtors->isLive()) |
| 663 | return; |
| 664 | |
Nicholas Wilson | f6dbc2e | 2018-03-02 14:48:50 +0000 | [diff] [blame] | 665 | // First write the body's contents to a string. |
| 666 | std::string BodyContent; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 667 | { |
Nicholas Wilson | f6dbc2e | 2018-03-02 14:48:50 +0000 | [diff] [blame] | 668 | raw_string_ostream OS(BodyContent); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 669 | writeUleb128(OS, 0, "num locals"); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 670 | if (Config->Pic) { |
| 671 | writeU8(OS, WASM_OPCODE_CALL, "CALL"); |
| 672 | writeUleb128(OS, WasmSym::ApplyRelocs->getFunctionIndex(), |
| 673 | "function index"); |
| 674 | } |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 675 | for (const WasmInitEntry &F : InitFunctions) { |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 676 | writeU8(OS, WASM_OPCODE_CALL, "CALL"); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 677 | writeUleb128(OS, F.Sym->getFunctionIndex(), "function index"); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 678 | } |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 679 | writeU8(OS, WASM_OPCODE_END, "END"); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | // Once we know the size of the body we can create the final function body |
Nicholas Wilson | f6dbc2e | 2018-03-02 14:48:50 +0000 | [diff] [blame] | 683 | std::string FunctionBody; |
| 684 | { |
| 685 | raw_string_ostream OS(FunctionBody); |
| 686 | writeUleb128(OS, BodyContent.size(), "function size"); |
| 687 | OS << BodyContent; |
| 688 | } |
Rui Ueyama | 29abfe4 | 2018-02-28 17:43:15 +0000 | [diff] [blame] | 689 | |
Sam Clegg | ea65647 | 2018-10-22 08:35:39 +0000 | [diff] [blame] | 690 | ArrayRef<uint8_t> Body = arrayRefFromStringRef(Saver.save(FunctionBody)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 691 | cast<SyntheticFunction>(WasmSym::CallCtors->Function)->setBody(Body); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | // Populate InitFunctions vector with init functions from all input objects. |
| 695 | // This is then used either when creating the output linking section or to |
| 696 | // synthesize the "__wasm_call_ctors" function. |
| 697 | void Writer::calculateInitFunctions() { |
Sam Clegg | 61f13b3 | 2019-03-02 04:55:02 +0000 | [diff] [blame] | 698 | if (!Config->Relocatable && !WasmSym::CallCtors->isLive()) |
| 699 | return; |
| 700 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 701 | for (ObjFile *File : Symtab->ObjectFiles) { |
| 702 | const WasmLinkingData &L = File->getWasmObj()->linkingData(); |
Nicholas Wilson | cb81a0c | 2018-03-02 14:46:54 +0000 | [diff] [blame] | 703 | for (const WasmInitFunc &F : L.InitFunctions) { |
| 704 | FunctionSymbol *Sym = File->getFunctionSymbol(F.Symbol); |
Sam Clegg | 0e6b42f | 2019-03-01 22:35:47 +0000 | [diff] [blame] | 705 | assert(Sym->isLive()); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 706 | if (*Sym->Signature != WasmSignature{{}, {}}) |
Nicholas Wilson | cb81a0c | 2018-03-02 14:46:54 +0000 | [diff] [blame] | 707 | error("invalid signature for init func: " + toString(*Sym)); |
| 708 | InitFunctions.emplace_back(WasmInitEntry{Sym, F.Priority}); |
| 709 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 710 | } |
Rui Ueyama | da69b71 | 2018-02-28 00:15:59 +0000 | [diff] [blame] | 711 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 712 | // Sort in order of priority (lowest first) so that they are called |
| 713 | // in the correct order. |
Fangrui Song | 32c0ebe | 2019-04-23 02:42:06 +0000 | [diff] [blame] | 714 | llvm::stable_sort(InitFunctions, |
| 715 | [](const WasmInitEntry &L, const WasmInitEntry &R) { |
| 716 | return L.Priority < R.Priority; |
| 717 | }); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 720 | void Writer::createSyntheticSections() { |
| 721 | Out.DylinkSec = make<DylinkSection>(); |
| 722 | Out.TypeSec = make<TypeSection>(); |
| 723 | Out.ImportSec = make<ImportSection>(); |
| 724 | Out.FunctionSec = make<FunctionSection>(); |
| 725 | Out.TableSec = make<TableSection>(); |
| 726 | Out.MemorySec = make<MemorySection>(); |
| 727 | Out.GlobalSec = make<GlobalSection>(); |
| 728 | Out.EventSec = make<EventSection>(); |
| 729 | Out.ExportSec = make<ExportSection>(); |
| 730 | Out.ElemSec = make<ElemSection>(TableBase); |
| 731 | Out.DataCountSec = make<DataCountSection>(Segments.size()); |
| 732 | Out.LinkingSec = make<LinkingSection>(InitFunctions, Segments); |
| 733 | Out.NameSec = make<NameSection>(); |
| 734 | Out.ProducersSec = make<ProducersSection>(); |
| 735 | Out.TargetFeaturesSec = make<TargetFeaturesSection>(); |
| 736 | } |
| 737 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 738 | void Writer::run() { |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 739 | if (Config->Relocatable || Config->Pic) |
Sam Clegg | 99eb42c | 2018-02-27 23:58:03 +0000 | [diff] [blame] | 740 | Config->GlobalBase = 0; |
| 741 | |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 +0000 | [diff] [blame] | 742 | // For PIC code the table base is assigned dynamically by the loader. |
| 743 | // For non-PIC, we start at 1 so that accessing table index 0 always traps. |
| 744 | if (!Config->Pic) |
| 745 | TableBase = 1; |
| 746 | |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 747 | log("-- createOutputSegments"); |
| 748 | createOutputSegments(); |
| 749 | log("-- createSyntheticSections"); |
| 750 | createSyntheticSections(); |
| 751 | log("-- populateProducers"); |
| 752 | populateProducers(); |
| 753 | log("-- populateTargetFeatures"); |
| 754 | populateTargetFeatures(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 755 | log("-- calculateImports"); |
| 756 | calculateImports(); |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 757 | log("-- layoutMemory"); |
| 758 | layoutMemory(); |
| 759 | |
| 760 | if (!Config->Relocatable) { |
| 761 | // Create linker synthesized __start_SECNAME/__stop_SECNAME symbols |
| 762 | // This has to be done after memory layout is performed. |
| 763 | for (const OutputSegment *Seg : Segments) |
| 764 | for (const InputSegment *S : Seg->InputSegments) |
| 765 | addStartStopSymbols(S); |
| 766 | } |
| 767 | |
Sam Clegg | b9889bb | 2019-05-23 09:41:03 +0000 | [diff] [blame] | 768 | log("-- scanRelocations"); |
| 769 | scanRelocations(); |
Sam Clegg | 7804dbd | 2019-05-21 10:07:30 +0000 | [diff] [blame] | 770 | log("-- assignIndexes"); |
| 771 | assignIndexes(); |
Sam Clegg | 7804dbd | 2019-05-21 10:07:30 +0000 | [diff] [blame] | 772 | log("-- calculateInitFunctions"); |
| 773 | calculateInitFunctions(); |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 774 | |
Sam Clegg | 7804dbd | 2019-05-21 10:07:30 +0000 | [diff] [blame] | 775 | if (!Config->Relocatable) { |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 776 | // Create linker synthesized functions |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 777 | if (Config->Pic) |
| 778 | createApplyRelocationsFunction(); |
| 779 | createCallCtorsFunction(); |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 780 | |
| 781 | // Make sure we have resolved all symbols. |
| 782 | if (!Config->AllowUndefined) |
| 783 | Symtab->reportRemainingUndefines(); |
| 784 | |
| 785 | if (errorCount()) |
| 786 | return; |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 787 | } |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 788 | |
| 789 | log("-- calculateTypes"); |
| 790 | calculateTypes(); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 791 | log("-- calculateExports"); |
| 792 | calculateExports(); |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 793 | log("-- calculateCustomSections"); |
| 794 | calculateCustomSections(); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 795 | log("-- populateSymtab"); |
| 796 | populateSymtab(); |
| 797 | log("-- addSections"); |
| 798 | addSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 799 | |
| 800 | if (errorHandler().Verbose) { |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 801 | log("Defined Functions: " + Twine(Out.FunctionSec->InputFunctions.size())); |
| 802 | log("Defined Globals : " + Twine(Out.GlobalSec->InputGlobals.size())); |
| 803 | log("Defined Events : " + Twine(Out.EventSec->InputEvents.size())); |
Sam Clegg | b9889bb | 2019-05-23 09:41:03 +0000 | [diff] [blame] | 804 | log("Function Imports : " + Twine(Out.ImportSec->numImportedFunctions())); |
| 805 | log("Global Imports : " + Twine(Out.ImportSec->numImportedGlobals())); |
| 806 | log("Event Imports : " + Twine(Out.ImportSec->numImportedEvents())); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 807 | for (ObjFile *File : Symtab->ObjectFiles) |
| 808 | File->dumpInfo(); |
| 809 | } |
| 810 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 811 | createHeader(); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 812 | log("-- finalizeSections"); |
| 813 | finalizeSections(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 814 | |
| 815 | log("-- openFile"); |
| 816 | openFile(); |
| 817 | if (errorCount()) |
| 818 | return; |
| 819 | |
| 820 | writeHeader(); |
| 821 | |
| 822 | log("-- writeSections"); |
| 823 | writeSections(); |
| 824 | if (errorCount()) |
| 825 | return; |
| 826 | |
| 827 | if (Error E = Buffer->commit()) |
| 828 | fatal("failed to write the output file: " + toString(std::move(E))); |
| 829 | } |
| 830 | |
| 831 | // Open a result file. |
| 832 | void Writer::openFile() { |
| 833 | log("writing: " + Config->OutputFile); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 834 | |
| 835 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 836 | FileOutputBuffer::create(Config->OutputFile, FileSize, |
| 837 | FileOutputBuffer::F_executable); |
| 838 | |
| 839 | if (!BufferOrErr) |
| 840 | error("failed to open " + Config->OutputFile + ": " + |
| 841 | toString(BufferOrErr.takeError())); |
| 842 | else |
| 843 | Buffer = std::move(*BufferOrErr); |
| 844 | } |
| 845 | |
| 846 | void Writer::createHeader() { |
| 847 | raw_string_ostream OS(Header); |
| 848 | writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic"); |
| 849 | writeU32(OS, WasmVersion, "wasm version"); |
| 850 | OS.flush(); |
| 851 | FileSize += Header.size(); |
| 852 | } |
| 853 | |
| 854 | void lld::wasm::writeResult() { Writer().run(); } |