Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1 | //===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===// |
| 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 |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements Wasm object file writer information. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/ADT/STLExtras.h" |
| 14 | #include "llvm/ADT/SmallPtrSet.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 15 | #include "llvm/BinaryFormat/Wasm.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 16 | #include "llvm/Config/llvm-config.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCAsmBackend.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAsmLayout.h" |
| 19 | #include "llvm/MC/MCAssembler.h" |
| 20 | #include "llvm/MC/MCContext.h" |
| 21 | #include "llvm/MC/MCExpr.h" |
| 22 | #include "llvm/MC/MCFixupKindInfo.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCObjectWriter.h" |
| 24 | #include "llvm/MC/MCSectionWasm.h" |
| 25 | #include "llvm/MC/MCSymbolWasm.h" |
| 26 | #include "llvm/MC/MCValue.h" |
| 27 | #include "llvm/MC/MCWasmObjectWriter.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Casting.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 31 | #include "llvm/Support/LEB128.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 32 | #include "llvm/Support/StringSaver.h" |
| 33 | #include <vector> |
| 34 | |
| 35 | using namespace llvm; |
| 36 | |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 37 | #define DEBUG_TYPE "mc" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 38 | |
| 39 | namespace { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 40 | |
Sam Clegg | 30e1bbc | 2018-01-19 18:57:01 +0000 | [diff] [blame] | 41 | // Went we ceate the indirect function table we start at 1, so that there is |
| 42 | // and emtpy slot at 0 and therefore calling a null function pointer will trap. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 43 | static const uint32_t InitialTableOffset = 1; |
Sam Clegg | 30e1bbc | 2018-01-19 18:57:01 +0000 | [diff] [blame] | 44 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 45 | // For patching purposes, we need to remember where each section starts, both |
| 46 | // for patching up the section size field, and for patching up references to |
| 47 | // locations within the section. |
| 48 | struct SectionBookkeeping { |
| 49 | // Where the size of the section is written. |
| 50 | uint64_t SizeOffset; |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 51 | // Where the section header ends (without custom section name). |
| 52 | uint64_t PayloadOffset; |
| 53 | // Where the contents of the section starts. |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 54 | uint64_t ContentsOffset; |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 55 | uint32_t Index; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 58 | // The signature of a wasm function or event, in a struct capable of being used |
| 59 | // as a DenseMap key. |
| 60 | // TODO: Consider using wasm::WasmSignature directly instead. |
| 61 | struct WasmSignature { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 62 | // Support empty and tombstone instances, needed by DenseMap. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 63 | enum { Plain, Empty, Tombstone } State = Plain; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 64 | |
| 65 | // The return types of the function. |
| 66 | SmallVector<wasm::ValType, 1> Returns; |
| 67 | |
| 68 | // The parameter types of the function. |
| 69 | SmallVector<wasm::ValType, 4> Params; |
| 70 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 71 | bool operator==(const WasmSignature &Other) const { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 72 | return State == Other.State && Returns == Other.Returns && |
| 73 | Params == Other.Params; |
| 74 | } |
| 75 | }; |
| 76 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 77 | // Traits for using WasmSignature in a DenseMap. |
| 78 | struct WasmSignatureDenseMapInfo { |
| 79 | static WasmSignature getEmptyKey() { |
| 80 | WasmSignature Sig; |
| 81 | Sig.State = WasmSignature::Empty; |
| 82 | return Sig; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 83 | } |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 84 | static WasmSignature getTombstoneKey() { |
| 85 | WasmSignature Sig; |
| 86 | Sig.State = WasmSignature::Tombstone; |
| 87 | return Sig; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 88 | } |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 89 | static unsigned getHashValue(const WasmSignature &Sig) { |
| 90 | uintptr_t Value = Sig.State; |
| 91 | for (wasm::ValType Ret : Sig.Returns) |
Heejin Ahn | 4808914 | 2018-11-02 19:25:09 +0000 | [diff] [blame] | 92 | Value += DenseMapInfo<uint32_t>::getHashValue(uint32_t(Ret)); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 93 | for (wasm::ValType Param : Sig.Params) |
Heejin Ahn | 4808914 | 2018-11-02 19:25:09 +0000 | [diff] [blame] | 94 | Value += DenseMapInfo<uint32_t>::getHashValue(uint32_t(Param)); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 95 | return Value; |
| 96 | } |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 97 | static bool isEqual(const WasmSignature &LHS, const WasmSignature &RHS) { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 98 | return LHS == RHS; |
| 99 | } |
| 100 | }; |
| 101 | |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 102 | // A wasm data segment. A wasm binary contains only a single data section |
| 103 | // but that can contain many segments, each with their own virtual location |
| 104 | // in memory. Each MCSection data created by llvm is modeled as its own |
| 105 | // wasm data segment. |
| 106 | struct WasmDataSegment { |
| 107 | MCSectionWasm *Section; |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 108 | StringRef Name; |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 109 | uint32_t Offset; |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 110 | uint32_t Alignment; |
| 111 | uint32_t Flags; |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 112 | SmallVector<char, 4> Data; |
| 113 | }; |
| 114 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 115 | // A wasm function to be written into the function section. |
| 116 | struct WasmFunction { |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 117 | uint32_t SigIndex; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 118 | const MCSymbolWasm *Sym; |
| 119 | }; |
| 120 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 121 | // A wasm global to be written into the global section. |
| 122 | struct WasmGlobal { |
Sam Clegg | 6e7f182 | 2018-01-31 19:50:14 +0000 | [diff] [blame] | 123 | wasm::WasmGlobalType Type; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 124 | uint64_t InitialValue; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 127 | // Information about a single item which is part of a COMDAT. For each data |
| 128 | // segment or function which is in the COMDAT, there is a corresponding |
| 129 | // WasmComdatEntry. |
| 130 | struct WasmComdatEntry { |
| 131 | unsigned Kind; |
| 132 | uint32_t Index; |
| 133 | }; |
| 134 | |
Sam Clegg | 6dc65e9 | 2017-06-06 16:38:59 +0000 | [diff] [blame] | 135 | // Information about a single relocation. |
| 136 | struct WasmRelocationEntry { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 137 | uint64_t Offset; // Where is the relocation. |
| 138 | const MCSymbolWasm *Symbol; // The symbol to relocate with. |
| 139 | int64_t Addend; // A value to add to the symbol. |
| 140 | unsigned Type; // The type of the relocation. |
| 141 | const MCSectionWasm *FixupSection; // The section the relocation is targeting. |
Sam Clegg | 6dc65e9 | 2017-06-06 16:38:59 +0000 | [diff] [blame] | 142 | |
| 143 | WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol, |
| 144 | int64_t Addend, unsigned Type, |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 145 | const MCSectionWasm *FixupSection) |
Sam Clegg | 6dc65e9 | 2017-06-06 16:38:59 +0000 | [diff] [blame] | 146 | : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type), |
| 147 | FixupSection(FixupSection) {} |
| 148 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 149 | bool hasAddend() const { |
| 150 | switch (Type) { |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 151 | case wasm::R_WASM_MEMORY_ADDR_LEB: |
| 152 | case wasm::R_WASM_MEMORY_ADDR_SLEB: |
| 153 | case wasm::R_WASM_MEMORY_ADDR_I32: |
| 154 | case wasm::R_WASM_FUNCTION_OFFSET_I32: |
| 155 | case wasm::R_WASM_SECTION_OFFSET_I32: |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 156 | return true; |
| 157 | default: |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | |
Sam Clegg | 6dc65e9 | 2017-06-06 16:38:59 +0000 | [diff] [blame] | 162 | void print(raw_ostream &Out) const { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 163 | Out << wasm::relocTypetoString(Type) << " Off=" << Offset |
| 164 | << ", Sym=" << *Symbol << ", Addend=" << Addend |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 165 | << ", FixupSection=" << FixupSection->getSectionName(); |
Sam Clegg | 6dc65e9 | 2017-06-06 16:38:59 +0000 | [diff] [blame] | 166 | } |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 167 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 168 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 169 | LLVM_DUMP_METHOD void dump() const { print(dbgs()); } |
| 170 | #endif |
Sam Clegg | 6dc65e9 | 2017-06-06 16:38:59 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 173 | static const uint32_t InvalidIndex = -1; |
Sam Clegg | 25d8e68 | 2018-05-08 00:08:21 +0000 | [diff] [blame] | 174 | |
Sam Clegg | cfd44a2 | 2018-04-05 17:01:39 +0000 | [diff] [blame] | 175 | struct WasmCustomSection { |
Sam Clegg | cfd44a2 | 2018-04-05 17:01:39 +0000 | [diff] [blame] | 176 | |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 177 | StringRef Name; |
| 178 | MCSectionWasm *Section; |
| 179 | |
| 180 | uint32_t OutputContentsOffset; |
| 181 | uint32_t OutputIndex; |
| 182 | |
| 183 | WasmCustomSection(StringRef Name, MCSectionWasm *Section) |
| 184 | : Name(Name), Section(Section), OutputContentsOffset(0), |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 185 | OutputIndex(InvalidIndex) {} |
Sam Clegg | cfd44a2 | 2018-04-05 17:01:39 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
Sam Clegg | 1fb8daa | 2017-06-20 05:05:10 +0000 | [diff] [blame] | 188 | #if !defined(NDEBUG) |
Sam Clegg | 7f055de | 2017-06-20 04:47:58 +0000 | [diff] [blame] | 189 | raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 190 | Rel.print(OS); |
| 191 | return OS; |
| 192 | } |
Sam Clegg | 1fb8daa | 2017-06-20 05:05:10 +0000 | [diff] [blame] | 193 | #endif |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 194 | |
Sam Clegg | 19e8bef | 2019-01-30 22:47:35 +0000 | [diff] [blame] | 195 | // Write X as an (unsigned) LEB value at offset Offset in Stream, padded |
| 196 | // to allow patching. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 197 | static void writePatchableLEB(raw_pwrite_stream &Stream, uint32_t X, |
Sam Clegg | 19e8bef | 2019-01-30 22:47:35 +0000 | [diff] [blame] | 198 | uint64_t Offset) { |
| 199 | uint8_t Buffer[5]; |
| 200 | unsigned SizeLen = encodeULEB128(X, Buffer, 5); |
| 201 | assert(SizeLen == 5); |
| 202 | Stream.pwrite((char *)Buffer, SizeLen, Offset); |
| 203 | } |
| 204 | |
| 205 | // Write X as an signed LEB value at offset Offset in Stream, padded |
| 206 | // to allow patching. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 207 | static void writePatchableSLEB(raw_pwrite_stream &Stream, int32_t X, |
Sam Clegg | 19e8bef | 2019-01-30 22:47:35 +0000 | [diff] [blame] | 208 | uint64_t Offset) { |
| 209 | uint8_t Buffer[5]; |
| 210 | unsigned SizeLen = encodeSLEB128(X, Buffer, 5); |
| 211 | assert(SizeLen == 5); |
| 212 | Stream.pwrite((char *)Buffer, SizeLen, Offset); |
| 213 | } |
| 214 | |
| 215 | // Write X as a plain integer value at offset Offset in Stream. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 216 | static void writeI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) { |
Sam Clegg | 19e8bef | 2019-01-30 22:47:35 +0000 | [diff] [blame] | 217 | uint8_t Buffer[4]; |
| 218 | support::endian::write32le(Buffer, X); |
| 219 | Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset); |
| 220 | } |
| 221 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 222 | class WasmObjectWriter : public MCObjectWriter { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 223 | support::endian::Writer W; |
| 224 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 225 | /// The target specific Wasm writer instance. |
| 226 | std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter; |
| 227 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 228 | // Relocations for fixing up references in the code section. |
| 229 | std::vector<WasmRelocationEntry> CodeRelocations; |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 230 | uint32_t CodeSectionIndex; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 231 | |
| 232 | // Relocations for fixing up references in the data section. |
| 233 | std::vector<WasmRelocationEntry> DataRelocations; |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 234 | uint32_t DataSectionIndex; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 235 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 236 | // Index values to use for fixing up call_indirect type indices. |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 237 | // Maps function symbols to the index of the type of the function |
| 238 | DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices; |
Sam Clegg | d99f607 | 2017-06-12 23:52:44 +0000 | [diff] [blame] | 239 | // Maps function symbols to the table element index space. Used |
| 240 | // for TABLE_INDEX relocation types (i.e. address taken functions). |
Sam Clegg | f9edbe9 | 2018-01-31 19:28:47 +0000 | [diff] [blame] | 241 | DenseMap<const MCSymbolWasm *, uint32_t> TableIndices; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 242 | // Maps function/global symbols to the function/global/event/section index |
| 243 | // space. |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 244 | DenseMap<const MCSymbolWasm *, uint32_t> WasmIndices; |
| 245 | // Maps data symbols to the Wasm segment and offset/size with the segment. |
| 246 | DenseMap<const MCSymbolWasm *, wasm::WasmDataReference> DataLocations; |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 247 | |
| 248 | // Stores output data (index, relocations, content offset) for custom |
| 249 | // section. |
| 250 | std::vector<WasmCustomSection> CustomSections; |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 251 | std::unique_ptr<WasmCustomSection> ProducersSection; |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 252 | // Relocations for fixing up references in the custom sections. |
| 253 | DenseMap<const MCSectionWasm *, std::vector<WasmRelocationEntry>> |
| 254 | CustomSectionsRelocations; |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 255 | |
Sam Clegg | c0d4119 | 2018-05-17 17:15:15 +0000 | [diff] [blame] | 256 | // Map from section to defining function symbol. |
Sam Clegg | 6ccb59b | 2018-05-16 20:09:05 +0000 | [diff] [blame] | 257 | DenseMap<const MCSection *, const MCSymbol *> SectionFunctions; |
| 258 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 259 | DenseMap<WasmSignature, uint32_t, WasmSignatureDenseMapInfo> SignatureIndices; |
| 260 | SmallVector<WasmSignature, 4> Signatures; |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 261 | SmallVector<WasmGlobal, 4> Globals; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 262 | SmallVector<WasmDataSegment, 4> DataSegments; |
Sam Clegg | 9f3fe42 | 2018-01-17 19:28:43 +0000 | [diff] [blame] | 263 | unsigned NumFunctionImports = 0; |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 264 | unsigned NumGlobalImports = 0; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 265 | unsigned NumEventImports = 0; |
Chandler Carruth | 7e1c334 | 2018-04-24 20:30:56 +0000 | [diff] [blame] | 266 | uint32_t SectionCount = 0; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 267 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 268 | // TargetObjectWriter wrappers. |
| 269 | bool is64Bit() const { return TargetObjectWriter->is64Bit(); } |
Sam Clegg | ae03c1e7 | 2017-06-13 18:51:50 +0000 | [diff] [blame] | 270 | unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup) const { |
| 271 | return TargetObjectWriter->getRelocType(Target, Fixup); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Sam Clegg | 2322a93 | 2018-04-23 19:16:19 +0000 | [diff] [blame] | 274 | void startSection(SectionBookkeeping &Section, unsigned SectionId); |
| 275 | void startCustomSection(SectionBookkeeping &Section, StringRef Name); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 276 | void endSection(SectionBookkeeping &Section); |
| 277 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 278 | public: |
Lang Hames | 1301a87 | 2017-10-10 01:15:10 +0000 | [diff] [blame] | 279 | WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW, |
| 280 | raw_pwrite_stream &OS) |
Peter Collingbourne | 59a6fc4 | 2018-05-21 18:28:57 +0000 | [diff] [blame] | 281 | : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {} |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 282 | |
Dan Gohman | 0917c9e | 2018-01-15 17:06:23 +0000 | [diff] [blame] | 283 | private: |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 284 | void reset() override { |
| 285 | CodeRelocations.clear(); |
| 286 | DataRelocations.clear(); |
| 287 | TypeIndices.clear(); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 288 | WasmIndices.clear(); |
Sam Clegg | f9edbe9 | 2018-01-31 19:28:47 +0000 | [diff] [blame] | 289 | TableIndices.clear(); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 290 | DataLocations.clear(); |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 291 | CustomSections.clear(); |
| 292 | ProducersSection.reset(); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 293 | CustomSectionsRelocations.clear(); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 294 | SignatureIndices.clear(); |
| 295 | Signatures.clear(); |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 296 | Globals.clear(); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 297 | DataSegments.clear(); |
Sam Clegg | 6ccb59b | 2018-05-16 20:09:05 +0000 | [diff] [blame] | 298 | SectionFunctions.clear(); |
Sam Clegg | 9f3fe42 | 2018-01-17 19:28:43 +0000 | [diff] [blame] | 299 | NumFunctionImports = 0; |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 300 | NumGlobalImports = 0; |
Sam Clegg | 105bdc2 | 2018-05-30 02:57:20 +0000 | [diff] [blame] | 301 | MCObjectWriter::reset(); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 304 | void writeHeader(const MCAssembler &Asm); |
| 305 | |
| 306 | void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, |
| 307 | const MCFragment *Fragment, const MCFixup &Fixup, |
Rafael Espindola | ceecfe5b | 2017-07-11 23:56:10 +0000 | [diff] [blame] | 308 | MCValue Target, uint64_t &FixedValue) override; |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 309 | |
| 310 | void executePostLayoutBinding(MCAssembler &Asm, |
| 311 | const MCAsmLayout &Layout) override; |
| 312 | |
Peter Collingbourne | 438390f | 2018-05-21 18:23:50 +0000 | [diff] [blame] | 313 | uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 314 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 315 | void writeString(const StringRef Str) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 316 | encodeULEB128(Str.size(), W.OS); |
| 317 | W.OS << Str; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 320 | void writeValueType(wasm::ValType Ty) { W.OS << static_cast<char>(Ty); } |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 321 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 322 | void writeTypeSection(ArrayRef<WasmSignature> Signatures); |
Sam Clegg | 8defa95 | 2018-02-12 22:41:29 +0000 | [diff] [blame] | 323 | void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint32_t DataSize, |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 324 | uint32_t NumElements); |
Sam Clegg | 457fb0b | 2017-09-15 19:50:44 +0000 | [diff] [blame] | 325 | void writeFunctionSection(ArrayRef<WasmFunction> Functions); |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 326 | void writeGlobalSection(); |
Sam Clegg | 8defa95 | 2018-02-12 22:41:29 +0000 | [diff] [blame] | 327 | void writeExportSection(ArrayRef<wasm::WasmExport> Exports); |
Sam Clegg | 457fb0b | 2017-09-15 19:50:44 +0000 | [diff] [blame] | 328 | void writeElemSection(ArrayRef<uint32_t> TableElems); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 329 | void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout, |
Sam Clegg | 457fb0b | 2017-09-15 19:50:44 +0000 | [diff] [blame] | 330 | ArrayRef<WasmFunction> Functions); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 331 | void writeDataSection(); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 332 | void writeEventSection(ArrayRef<wasm::WasmEventType> Events); |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 333 | void writeRelocSection(uint32_t SectionIndex, StringRef Name, |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 334 | std::vector<WasmRelocationEntry> &Relocations); |
Sam Clegg | 31a2c80 | 2017-09-20 21:17:04 +0000 | [diff] [blame] | 335 | void writeLinkingMetaDataSection( |
Sam Clegg | 86b4a09 | 2018-02-27 23:57:37 +0000 | [diff] [blame] | 336 | ArrayRef<wasm::WasmSymbolInfo> SymbolInfos, |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 337 | ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs, |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 338 | const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats); |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 339 | void writeCustomSection(WasmCustomSection &CustomSection, |
| 340 | const MCAssembler &Asm, const MCAsmLayout &Layout); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 341 | void writeCustomRelocSections(); |
| 342 | void |
| 343 | updateCustomSectionRelocations(const SmallVector<WasmFunction, 4> &Functions, |
| 344 | const MCAsmLayout &Layout); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 345 | |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 346 | uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 347 | void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations, |
| 348 | uint64_t ContentsOffset); |
| 349 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 350 | uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry); |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 351 | uint32_t getFunctionType(const MCSymbolWasm &Symbol); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 352 | uint32_t getEventType(const MCSymbolWasm &Symbol); |
Heejin Ahn | 38b12f5 | 2018-11-20 00:38:10 +0000 | [diff] [blame] | 353 | void registerFunctionType(const MCSymbolWasm &Symbol); |
| 354 | void registerEventType(const MCSymbolWasm &Symbol); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 355 | }; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 356 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 357 | } // end anonymous namespace |
| 358 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 359 | // Write out a section header and a patchable section size field. |
| 360 | void WasmObjectWriter::startSection(SectionBookkeeping &Section, |
Sam Clegg | 2322a93 | 2018-04-23 19:16:19 +0000 | [diff] [blame] | 361 | unsigned SectionId) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 362 | LLVM_DEBUG(dbgs() << "startSection " << SectionId << "\n"); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 363 | W.OS << char(SectionId); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 364 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 365 | Section.SizeOffset = W.OS.tell(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 366 | |
| 367 | // The section size. We don't know the size yet, so reserve enough space |
| 368 | // for any 32-bit value; we'll patch it later. |
Sam Clegg | 19e8bef | 2019-01-30 22:47:35 +0000 | [diff] [blame] | 369 | encodeULEB128(0, W.OS, 5); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 370 | |
| 371 | // The position where the section starts, for measuring its size. |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 372 | Section.ContentsOffset = W.OS.tell(); |
| 373 | Section.PayloadOffset = W.OS.tell(); |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 374 | Section.Index = SectionCount++; |
Sam Clegg | 2322a93 | 2018-04-23 19:16:19 +0000 | [diff] [blame] | 375 | } |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 376 | |
Sam Clegg | 2322a93 | 2018-04-23 19:16:19 +0000 | [diff] [blame] | 377 | void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section, |
| 378 | StringRef Name) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 379 | LLVM_DEBUG(dbgs() << "startCustomSection " << Name << "\n"); |
Sam Clegg | 2322a93 | 2018-04-23 19:16:19 +0000 | [diff] [blame] | 380 | startSection(Section, wasm::WASM_SEC_CUSTOM); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 381 | |
| 382 | // The position where the section header ends, for measuring its size. |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 383 | Section.PayloadOffset = W.OS.tell(); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 384 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 385 | // Custom sections in wasm also have a string identifier. |
Sam Clegg | 2322a93 | 2018-04-23 19:16:19 +0000 | [diff] [blame] | 386 | writeString(Name); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 387 | |
| 388 | // The position where the custom section starts. |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 389 | Section.ContentsOffset = W.OS.tell(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Now that the section is complete and we know how big it is, patch up the |
| 393 | // section size field at the start of the section. |
| 394 | void WasmObjectWriter::endSection(SectionBookkeeping &Section) { |
Sam Clegg | c0affde | 2019-01-31 22:38:22 +0000 | [diff] [blame] | 395 | uint64_t Size = W.OS.tell(); |
| 396 | // /dev/null doesn't support seek/tell and can report offset of 0. |
| 397 | // Simply skip this patching in that case. |
| 398 | if (!Size) |
| 399 | return; |
| 400 | |
| 401 | Size -= Section.PayloadOffset; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 402 | if (uint32_t(Size) != Size) |
| 403 | report_fatal_error("section size does not fit in a uint32_t"); |
| 404 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 405 | LLVM_DEBUG(dbgs() << "endSection size=" << Size << "\n"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 406 | |
| 407 | // Write the final section size to the payload_len field, which follows |
| 408 | // the section id byte. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 409 | writePatchableLEB(static_cast<raw_pwrite_stream &>(W.OS), Size, |
Sam Clegg | 19e8bef | 2019-01-30 22:47:35 +0000 | [diff] [blame] | 410 | Section.SizeOffset); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 413 | // Emit the Wasm header. |
| 414 | void WasmObjectWriter::writeHeader(const MCAssembler &Asm) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 415 | W.OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic)); |
| 416 | W.write<uint32_t>(wasm::WasmVersion); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm, |
| 420 | const MCAsmLayout &Layout) { |
Sam Clegg | 6ccb59b | 2018-05-16 20:09:05 +0000 | [diff] [blame] | 421 | // Build a map of sections to the function that defines them, for use |
| 422 | // in recordRelocation. |
| 423 | for (const MCSymbol &S : Asm.symbols()) { |
| 424 | const auto &WS = static_cast<const MCSymbolWasm &>(S); |
| 425 | if (WS.isDefined() && WS.isFunction() && !WS.isVariable()) { |
| 426 | const auto &Sec = static_cast<const MCSectionWasm &>(S.getSection()); |
| 427 | auto Pair = SectionFunctions.insert(std::make_pair(&Sec, &S)); |
| 428 | if (!Pair.second) |
| 429 | report_fatal_error("section already has a defining function: " + |
| 430 | Sec.getSectionName()); |
| 431 | } |
| 432 | } |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void WasmObjectWriter::recordRelocation(MCAssembler &Asm, |
| 436 | const MCAsmLayout &Layout, |
| 437 | const MCFragment *Fragment, |
| 438 | const MCFixup &Fixup, MCValue Target, |
Rafael Espindola | ceecfe5b | 2017-07-11 23:56:10 +0000 | [diff] [blame] | 439 | uint64_t &FixedValue) { |
| 440 | MCAsmBackend &Backend = Asm.getBackend(); |
| 441 | bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags & |
| 442 | MCFixupKindInfo::FKF_IsPCRel; |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 443 | const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent()); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 444 | uint64_t C = Target.getConstant(); |
| 445 | uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); |
| 446 | MCContext &Ctx = Asm.getContext(); |
| 447 | |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 448 | // The .init_array isn't translated as data, so don't do relocations in it. |
| 449 | if (FixupSection.getSectionName().startswith(".init_array")) |
| 450 | return; |
| 451 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 452 | if (const MCSymbolRefExpr *RefB = Target.getSymB()) { |
| 453 | assert(RefB->getKind() == MCSymbolRefExpr::VK_None && |
| 454 | "Should not have constructed this"); |
| 455 | |
| 456 | // Let A, B and C being the components of Target and R be the location of |
| 457 | // the fixup. If the fixup is not pcrel, we want to compute (A - B + C). |
| 458 | // If it is pcrel, we want to compute (A - B + C - R). |
| 459 | |
| 460 | // In general, Wasm has no relocations for -B. It can only represent (A + C) |
| 461 | // or (A + C - R). If B = R + K and the relocation is not pcrel, we can |
| 462 | // replace B to implement it: (A - R - K + C) |
| 463 | if (IsPCRel) { |
| 464 | Ctx.reportError( |
| 465 | Fixup.getLoc(), |
| 466 | "No relocation available to represent this relative expression"); |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol()); |
| 471 | |
| 472 | if (SymB.isUndefined()) { |
| 473 | Ctx.reportError(Fixup.getLoc(), |
| 474 | Twine("symbol '") + SymB.getName() + |
| 475 | "' can not be undefined in a subtraction expression"); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | assert(!SymB.isAbsolute() && "Should have been folded"); |
| 480 | const MCSection &SecB = SymB.getSection(); |
| 481 | if (&SecB != &FixupSection) { |
| 482 | Ctx.reportError(Fixup.getLoc(), |
| 483 | "Cannot represent a difference across sections"); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | uint64_t SymBOffset = Layout.getSymbolOffset(SymB); |
| 488 | uint64_t K = SymBOffset - FixupOffset; |
| 489 | IsPCRel = true; |
| 490 | C -= K; |
| 491 | } |
| 492 | |
| 493 | // We either rejected the fixup or folded B into C at this point. |
| 494 | const MCSymbolRefExpr *RefA = Target.getSymA(); |
| 495 | const auto *SymA = RefA ? cast<MCSymbolWasm>(&RefA->getSymbol()) : nullptr; |
| 496 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 497 | if (SymA && SymA->isVariable()) { |
| 498 | const MCExpr *Expr = SymA->getVariableValue(); |
Sam Clegg | 6ad8f19 | 2017-07-11 02:21:57 +0000 | [diff] [blame] | 499 | const auto *Inner = cast<MCSymbolRefExpr>(Expr); |
| 500 | if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) |
| 501 | llvm_unreachable("weakref used in reloc not yet implemented"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // Put any constant offset in an addend. Offsets can be negative, and |
| 505 | // LLVM expects wrapping, in contrast to wasm's immediates which can't |
| 506 | // be negative and don't wrap. |
| 507 | FixedValue = 0; |
| 508 | |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 509 | unsigned Type = getRelocType(Target, Fixup); |
Sam Clegg | ae03c1e7 | 2017-06-13 18:51:50 +0000 | [diff] [blame] | 510 | assert(!IsPCRel); |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame] | 511 | assert(SymA); |
| 512 | |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 513 | // Absolute offset within a section or a function. |
| 514 | // Currently only supported for for metadata sections. |
| 515 | // See: test/MC/WebAssembly/blockaddress.ll |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 516 | if (Type == wasm::R_WASM_FUNCTION_OFFSET_I32 || |
| 517 | Type == wasm::R_WASM_SECTION_OFFSET_I32) { |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 518 | if (!FixupSection.getKind().isMetadata()) |
| 519 | report_fatal_error("relocations for function or section offsets are " |
| 520 | "only supported in metadata sections"); |
| 521 | |
| 522 | const MCSymbol *SectionSymbol = nullptr; |
| 523 | const MCSection &SecA = SymA->getSection(); |
| 524 | if (SecA.getKind().isText()) |
Sam Clegg | 6ccb59b | 2018-05-16 20:09:05 +0000 | [diff] [blame] | 525 | SectionSymbol = SectionFunctions.find(&SecA)->second; |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 526 | else |
| 527 | SectionSymbol = SecA.getBeginSymbol(); |
| 528 | if (!SectionSymbol) |
| 529 | report_fatal_error("section symbol is required for relocation"); |
| 530 | |
| 531 | C += Layout.getSymbolOffset(*SymA); |
| 532 | SymA = cast<MCSymbolWasm>(SectionSymbol); |
| 533 | } |
| 534 | |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 535 | // Relocation other than R_WASM_TYPE_INDEX_LEB are required to be |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 536 | // against a named symbol. |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 537 | if (Type != wasm::R_WASM_TYPE_INDEX_LEB) { |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 538 | if (SymA->getName().empty()) |
| 539 | report_fatal_error("relocations against un-named temporaries are not yet " |
| 540 | "supported by wasm"); |
| 541 | |
| 542 | SymA->setUsedInReloc(); |
| 543 | } |
Sam Clegg | ae03c1e7 | 2017-06-13 18:51:50 +0000 | [diff] [blame] | 544 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 545 | WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 546 | LLVM_DEBUG(dbgs() << "WasmReloc: " << Rec << "\n"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 547 | |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 548 | if (FixupSection.isWasmData()) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 549 | DataRelocations.push_back(Rec); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 550 | } else if (FixupSection.getKind().isText()) { |
Sam Clegg | 12fd3da | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 551 | CodeRelocations.push_back(Rec); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 552 | } else if (FixupSection.getKind().isMetadata()) { |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 553 | CustomSectionsRelocations[&FixupSection].push_back(Rec); |
| 554 | } else { |
Sam Clegg | 12fd3da | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 555 | llvm_unreachable("unexpected section type"); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 556 | } |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 559 | static const MCSymbolWasm *resolveSymbol(const MCSymbolWasm &Symbol) { |
Sam Clegg | aff1c4d | 2017-09-15 19:22:01 +0000 | [diff] [blame] | 560 | if (Symbol.isVariable()) { |
| 561 | const MCExpr *Expr = Symbol.getVariableValue(); |
| 562 | auto *Inner = cast<MCSymbolRefExpr>(Expr); |
| 563 | return cast<MCSymbolWasm>(&Inner->getSymbol()); |
| 564 | } |
| 565 | return &Symbol; |
| 566 | } |
| 567 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 568 | // Compute a value to write into the code at the location covered |
Sam Clegg | 60ec303 | 2018-01-23 01:23:17 +0000 | [diff] [blame] | 569 | // by RelEntry. This value isn't used by the static linker; it just serves |
| 570 | // to make the object format more readable and more likely to be directly |
| 571 | // useable. |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 572 | uint32_t |
| 573 | WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) { |
Sam Clegg | 60ec303 | 2018-01-23 01:23:17 +0000 | [diff] [blame] | 574 | switch (RelEntry.Type) { |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 575 | case wasm::R_WASM_TABLE_INDEX_SLEB: |
| 576 | case wasm::R_WASM_TABLE_INDEX_I32: { |
Sam Clegg | f9edbe9 | 2018-01-31 19:28:47 +0000 | [diff] [blame] | 577 | // Provisional value is table address of the resolved symbol itself |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 578 | const MCSymbolWasm *Sym = resolveSymbol(*RelEntry.Symbol); |
Sam Clegg | f9edbe9 | 2018-01-31 19:28:47 +0000 | [diff] [blame] | 579 | assert(Sym->isFunction()); |
| 580 | return TableIndices[Sym]; |
| 581 | } |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 582 | case wasm::R_WASM_TYPE_INDEX_LEB: |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 583 | // Provisional value is same as the index |
Sam Clegg | 60ec303 | 2018-01-23 01:23:17 +0000 | [diff] [blame] | 584 | return getRelocationIndexValue(RelEntry); |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 585 | case wasm::R_WASM_FUNCTION_INDEX_LEB: |
| 586 | case wasm::R_WASM_GLOBAL_INDEX_LEB: |
| 587 | case wasm::R_WASM_EVENT_INDEX_LEB: |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 588 | // Provisional value is function/global/event Wasm index |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 589 | if (!WasmIndices.count(RelEntry.Symbol)) |
| 590 | report_fatal_error("symbol not found in wasm index space: " + |
| 591 | RelEntry.Symbol->getName()); |
| 592 | return WasmIndices[RelEntry.Symbol]; |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 593 | case wasm::R_WASM_FUNCTION_OFFSET_I32: |
| 594 | case wasm::R_WASM_SECTION_OFFSET_I32: { |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 595 | const auto &Section = |
| 596 | static_cast<const MCSectionWasm &>(RelEntry.Symbol->getSection()); |
| 597 | return Section.getSectionOffset() + RelEntry.Addend; |
| 598 | } |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 599 | case wasm::R_WASM_MEMORY_ADDR_LEB: |
| 600 | case wasm::R_WASM_MEMORY_ADDR_I32: |
| 601 | case wasm::R_WASM_MEMORY_ADDR_SLEB: { |
Sam Clegg | f9edbe9 | 2018-01-31 19:28:47 +0000 | [diff] [blame] | 602 | // Provisional value is address of the global |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 603 | const MCSymbolWasm *Sym = resolveSymbol(*RelEntry.Symbol); |
Sam Clegg | 60ec303 | 2018-01-23 01:23:17 +0000 | [diff] [blame] | 604 | // For undefined symbols, use zero |
| 605 | if (!Sym->isDefined()) |
| 606 | return 0; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 607 | const wasm::WasmDataReference &Ref = DataLocations[Sym]; |
| 608 | const WasmDataSegment &Segment = DataSegments[Ref.Segment]; |
Sam Clegg | 60ec303 | 2018-01-23 01:23:17 +0000 | [diff] [blame] | 609 | // Ignore overflow. LLVM allows address arithmetic to silently wrap. |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 610 | return Segment.Offset + Ref.Offset + RelEntry.Addend; |
Sam Clegg | 60ec303 | 2018-01-23 01:23:17 +0000 | [diff] [blame] | 611 | } |
| 612 | default: |
| 613 | llvm_unreachable("invalid relocation type"); |
| 614 | } |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 617 | static void addData(SmallVectorImpl<char> &DataBytes, |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 618 | MCSectionWasm &DataSection) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 619 | LLVM_DEBUG(errs() << "addData: " << DataSection.getSectionName() << "\n"); |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 620 | |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 621 | DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment())); |
| 622 | |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 623 | for (const MCFragment &Frag : DataSection) { |
| 624 | if (Frag.hasInstructions()) |
| 625 | report_fatal_error("only data supported in data sections"); |
| 626 | |
| 627 | if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) { |
| 628 | if (Align->getValueSize() != 1) |
| 629 | report_fatal_error("only byte values supported for alignment"); |
| 630 | // If nops are requested, use zeros, as this is the data section. |
| 631 | uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue(); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 632 | uint64_t Size = |
| 633 | std::min<uint64_t>(alignTo(DataBytes.size(), Align->getAlignment()), |
| 634 | DataBytes.size() + Align->getMaxBytesToEmit()); |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 635 | DataBytes.resize(Size, Value); |
| 636 | } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) { |
Nirav Dave | 588fad4 | 2018-05-18 17:45:48 +0000 | [diff] [blame] | 637 | int64_t NumValues; |
| 638 | if (!Fill->getNumValues().evaluateAsAbsolute(NumValues)) |
Rafael Espindola | d707c37 | 2018-01-09 22:48:37 +0000 | [diff] [blame] | 639 | llvm_unreachable("The fill should be an assembler constant"); |
Nirav Dave | 588fad4 | 2018-05-18 17:45:48 +0000 | [diff] [blame] | 640 | DataBytes.insert(DataBytes.end(), Fill->getValueSize() * NumValues, |
| 641 | Fill->getValue()); |
Heejin Ahn | 24faf85 | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 642 | } else if (auto *LEB = dyn_cast<MCLEBFragment>(&Frag)) { |
| 643 | const SmallVectorImpl<char> &Contents = LEB->getContents(); |
| 644 | DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end()); |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 645 | } else { |
| 646 | const auto &DataFrag = cast<MCDataFragment>(Frag); |
| 647 | const SmallVectorImpl<char> &Contents = DataFrag.getContents(); |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 648 | DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end()); |
| 649 | } |
| 650 | } |
| 651 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 652 | LLVM_DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n"); |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Sam Clegg | 60ec303 | 2018-01-23 01:23:17 +0000 | [diff] [blame] | 655 | uint32_t |
| 656 | WasmObjectWriter::getRelocationIndexValue(const WasmRelocationEntry &RelEntry) { |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 657 | if (RelEntry.Type == wasm::R_WASM_TYPE_INDEX_LEB) { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 658 | if (!TypeIndices.count(RelEntry.Symbol)) |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 659 | report_fatal_error("symbol not found in type index space: " + |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 660 | RelEntry.Symbol->getName()); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 661 | return TypeIndices[RelEntry.Symbol]; |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 662 | } |
Sam Clegg | 60ec303 | 2018-01-23 01:23:17 +0000 | [diff] [blame] | 663 | |
Sam Clegg | 25d8e68 | 2018-05-08 00:08:21 +0000 | [diff] [blame] | 664 | return RelEntry.Symbol->getIndex(); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 667 | // Apply the portions of the relocation records that we can handle ourselves |
| 668 | // directly. |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 669 | void WasmObjectWriter::applyRelocations( |
| 670 | ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 671 | auto &Stream = static_cast<raw_pwrite_stream &>(W.OS); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 672 | for (const WasmRelocationEntry &RelEntry : Relocations) { |
| 673 | uint64_t Offset = ContentsOffset + |
| 674 | RelEntry.FixupSection->getSectionOffset() + |
| 675 | RelEntry.Offset; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 676 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 677 | LLVM_DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n"); |
Sam Clegg | 60ec303 | 2018-01-23 01:23:17 +0000 | [diff] [blame] | 678 | uint32_t Value = getProvisionalValue(RelEntry); |
| 679 | |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 680 | switch (RelEntry.Type) { |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 681 | case wasm::R_WASM_FUNCTION_INDEX_LEB: |
| 682 | case wasm::R_WASM_TYPE_INDEX_LEB: |
| 683 | case wasm::R_WASM_GLOBAL_INDEX_LEB: |
| 684 | case wasm::R_WASM_MEMORY_ADDR_LEB: |
| 685 | case wasm::R_WASM_EVENT_INDEX_LEB: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 686 | writePatchableLEB(Stream, Value, Offset); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 687 | break; |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 688 | case wasm::R_WASM_TABLE_INDEX_I32: |
| 689 | case wasm::R_WASM_MEMORY_ADDR_I32: |
| 690 | case wasm::R_WASM_FUNCTION_OFFSET_I32: |
| 691 | case wasm::R_WASM_SECTION_OFFSET_I32: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 692 | writeI32(Stream, Value, Offset); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 693 | break; |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 694 | case wasm::R_WASM_TABLE_INDEX_SLEB: |
| 695 | case wasm::R_WASM_MEMORY_ADDR_SLEB: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 696 | writePatchableSLEB(Stream, Value, Offset); |
Sam Clegg | 60ec303 | 2018-01-23 01:23:17 +0000 | [diff] [blame] | 697 | break; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 698 | default: |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame] | 699 | llvm_unreachable("invalid relocation type"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 700 | } |
| 701 | } |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 704 | void WasmObjectWriter::writeTypeSection(ArrayRef<WasmSignature> Signatures) { |
| 705 | if (Signatures.empty()) |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 706 | return; |
| 707 | |
| 708 | SectionBookkeeping Section; |
| 709 | startSection(Section, wasm::WASM_SEC_TYPE); |
| 710 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 711 | encodeULEB128(Signatures.size(), W.OS); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 712 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 713 | for (const WasmSignature &Sig : Signatures) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 714 | W.OS << char(wasm::WASM_TYPE_FUNC); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 715 | encodeULEB128(Sig.Params.size(), W.OS); |
| 716 | for (wasm::ValType Ty : Sig.Params) |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 717 | writeValueType(Ty); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 718 | encodeULEB128(Sig.Returns.size(), W.OS); |
| 719 | for (wasm::ValType Ty : Sig.Returns) |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 720 | writeValueType(Ty); |
| 721 | } |
| 722 | |
| 723 | endSection(Section); |
| 724 | } |
| 725 | |
Sam Clegg | 8defa95 | 2018-02-12 22:41:29 +0000 | [diff] [blame] | 726 | void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports, |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 727 | uint32_t DataSize, |
| 728 | uint32_t NumElements) { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 729 | if (Imports.empty()) |
| 730 | return; |
| 731 | |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 732 | uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize; |
| 733 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 734 | SectionBookkeeping Section; |
| 735 | startSection(Section, wasm::WASM_SEC_IMPORT); |
| 736 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 737 | encodeULEB128(Imports.size(), W.OS); |
Sam Clegg | 8defa95 | 2018-02-12 22:41:29 +0000 | [diff] [blame] | 738 | for (const wasm::WasmImport &Import : Imports) { |
| 739 | writeString(Import.Module); |
| 740 | writeString(Import.Field); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 741 | W.OS << char(Import.Kind); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 742 | |
| 743 | switch (Import.Kind) { |
| 744 | case wasm::WASM_EXTERNAL_FUNCTION: |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 745 | encodeULEB128(Import.SigIndex, W.OS); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 746 | break; |
| 747 | case wasm::WASM_EXTERNAL_GLOBAL: |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 748 | W.OS << char(Import.Global.Type); |
| 749 | W.OS << char(Import.Global.Mutable ? 1 : 0); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 750 | break; |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 751 | case wasm::WASM_EXTERNAL_MEMORY: |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 752 | encodeULEB128(0, W.OS); // flags |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 753 | encodeULEB128(NumPages, W.OS); // initial |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 754 | break; |
| 755 | case wasm::WASM_EXTERNAL_TABLE: |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 756 | W.OS << char(Import.Table.ElemType); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 757 | encodeULEB128(0, W.OS); // flags |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 758 | encodeULEB128(NumElements, W.OS); // initial |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 759 | break; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 760 | case wasm::WASM_EXTERNAL_EVENT: |
| 761 | encodeULEB128(Import.Event.Attribute, W.OS); |
| 762 | encodeULEB128(Import.Event.SigIndex, W.OS); |
| 763 | break; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 764 | default: |
| 765 | llvm_unreachable("unsupported import kind"); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | endSection(Section); |
| 770 | } |
| 771 | |
Sam Clegg | 457fb0b | 2017-09-15 19:50:44 +0000 | [diff] [blame] | 772 | void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 773 | if (Functions.empty()) |
| 774 | return; |
| 775 | |
| 776 | SectionBookkeeping Section; |
| 777 | startSection(Section, wasm::WASM_SEC_FUNCTION); |
| 778 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 779 | encodeULEB128(Functions.size(), W.OS); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 780 | for (const WasmFunction &Func : Functions) |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 781 | encodeULEB128(Func.SigIndex, W.OS); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 782 | |
| 783 | endSection(Section); |
| 784 | } |
| 785 | |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 786 | void WasmObjectWriter::writeGlobalSection() { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 787 | if (Globals.empty()) |
| 788 | return; |
| 789 | |
| 790 | SectionBookkeeping Section; |
| 791 | startSection(Section, wasm::WASM_SEC_GLOBAL); |
| 792 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 793 | encodeULEB128(Globals.size(), W.OS); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 794 | for (const WasmGlobal &Global : Globals) { |
Sam Clegg | 6e7f182 | 2018-01-31 19:50:14 +0000 | [diff] [blame] | 795 | writeValueType(static_cast<wasm::ValType>(Global.Type.Type)); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 796 | W.OS << char(Global.Type.Mutable); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 797 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 798 | W.OS << char(wasm::WASM_OPCODE_I32_CONST); |
| 799 | encodeSLEB128(Global.InitialValue, W.OS); |
| 800 | W.OS << char(wasm::WASM_OPCODE_END); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | endSection(Section); |
| 804 | } |
| 805 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 806 | void WasmObjectWriter::writeEventSection(ArrayRef<wasm::WasmEventType> Events) { |
| 807 | if (Events.empty()) |
| 808 | return; |
| 809 | |
| 810 | SectionBookkeeping Section; |
| 811 | startSection(Section, wasm::WASM_SEC_EVENT); |
| 812 | |
| 813 | encodeULEB128(Events.size(), W.OS); |
| 814 | for (const wasm::WasmEventType &Event : Events) { |
| 815 | encodeULEB128(Event.Attribute, W.OS); |
| 816 | encodeULEB128(Event.SigIndex, W.OS); |
| 817 | } |
| 818 | |
| 819 | endSection(Section); |
| 820 | } |
| 821 | |
Sam Clegg | 8defa95 | 2018-02-12 22:41:29 +0000 | [diff] [blame] | 822 | void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 823 | if (Exports.empty()) |
| 824 | return; |
| 825 | |
| 826 | SectionBookkeeping Section; |
| 827 | startSection(Section, wasm::WASM_SEC_EXPORT); |
| 828 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 829 | encodeULEB128(Exports.size(), W.OS); |
Sam Clegg | 8defa95 | 2018-02-12 22:41:29 +0000 | [diff] [blame] | 830 | for (const wasm::WasmExport &Export : Exports) { |
| 831 | writeString(Export.Name); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 832 | W.OS << char(Export.Kind); |
| 833 | encodeULEB128(Export.Index, W.OS); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | endSection(Section); |
| 837 | } |
| 838 | |
Sam Clegg | 457fb0b | 2017-09-15 19:50:44 +0000 | [diff] [blame] | 839 | void WasmObjectWriter::writeElemSection(ArrayRef<uint32_t> TableElems) { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 840 | if (TableElems.empty()) |
| 841 | return; |
| 842 | |
| 843 | SectionBookkeeping Section; |
| 844 | startSection(Section, wasm::WASM_SEC_ELEM); |
| 845 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 846 | encodeULEB128(1, W.OS); // number of "segments" |
| 847 | encodeULEB128(0, W.OS); // the table index |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 848 | |
| 849 | // init expr for starting offset |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 850 | W.OS << char(wasm::WASM_OPCODE_I32_CONST); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 851 | encodeSLEB128(InitialTableOffset, W.OS); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 852 | W.OS << char(wasm::WASM_OPCODE_END); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 853 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 854 | encodeULEB128(TableElems.size(), W.OS); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 855 | for (uint32_t Elem : TableElems) |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 856 | encodeULEB128(Elem, W.OS); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 857 | |
| 858 | endSection(Section); |
| 859 | } |
| 860 | |
Sam Clegg | 457fb0b | 2017-09-15 19:50:44 +0000 | [diff] [blame] | 861 | void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm, |
| 862 | const MCAsmLayout &Layout, |
| 863 | ArrayRef<WasmFunction> Functions) { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 864 | if (Functions.empty()) |
| 865 | return; |
| 866 | |
| 867 | SectionBookkeeping Section; |
| 868 | startSection(Section, wasm::WASM_SEC_CODE); |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 869 | CodeSectionIndex = Section.Index; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 870 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 871 | encodeULEB128(Functions.size(), W.OS); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 872 | |
| 873 | for (const WasmFunction &Func : Functions) { |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 874 | auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection()); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 875 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 876 | int64_t Size = 0; |
| 877 | if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout)) |
| 878 | report_fatal_error(".size expression must be evaluatable"); |
| 879 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 880 | encodeULEB128(Size, W.OS); |
| 881 | FuncSection.setSectionOffset(W.OS.tell() - Section.ContentsOffset); |
| 882 | Asm.writeSectionData(W.OS, &FuncSection, Layout); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 885 | // Apply fixups. |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 886 | applyRelocations(CodeRelocations, Section.ContentsOffset); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 887 | |
| 888 | endSection(Section); |
| 889 | } |
| 890 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 891 | void WasmObjectWriter::writeDataSection() { |
| 892 | if (DataSegments.empty()) |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 893 | return; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 894 | |
| 895 | SectionBookkeeping Section; |
| 896 | startSection(Section, wasm::WASM_SEC_DATA); |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 897 | DataSectionIndex = Section.Index; |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 898 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 899 | encodeULEB128(DataSegments.size(), W.OS); // count |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 900 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 901 | for (const WasmDataSegment &Segment : DataSegments) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 902 | encodeULEB128(0, W.OS); // memory index |
| 903 | W.OS << char(wasm::WASM_OPCODE_I32_CONST); |
| 904 | encodeSLEB128(Segment.Offset, W.OS); // offset |
| 905 | W.OS << char(wasm::WASM_OPCODE_END); |
| 906 | encodeULEB128(Segment.Data.size(), W.OS); // size |
| 907 | Segment.Section->setSectionOffset(W.OS.tell() - Section.ContentsOffset); |
| 908 | W.OS << Segment.Data; // data |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 909 | } |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 910 | |
| 911 | // Apply fixups. |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 912 | applyRelocations(DataRelocations, Section.ContentsOffset); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 913 | |
| 914 | endSection(Section); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 917 | void WasmObjectWriter::writeRelocSection( |
| 918 | uint32_t SectionIndex, StringRef Name, |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 919 | std::vector<WasmRelocationEntry> &Relocs) { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 920 | // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md |
| 921 | // for descriptions of the reloc sections. |
| 922 | |
Sam Clegg | f77dc2a | 2018-08-22 17:27:31 +0000 | [diff] [blame] | 923 | if (Relocs.empty()) |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 924 | return; |
| 925 | |
Sam Clegg | f77dc2a | 2018-08-22 17:27:31 +0000 | [diff] [blame] | 926 | // First, ensure the relocations are sorted in offset order. In general they |
| 927 | // should already be sorted since `recordRelocation` is called in offset |
| 928 | // order, but for the code section we combine many MC sections into single |
| 929 | // wasm section, and this order is determined by the order of Asm.Symbols() |
| 930 | // not the sections order. |
| 931 | std::stable_sort( |
| 932 | Relocs.begin(), Relocs.end(), |
| 933 | [](const WasmRelocationEntry &A, const WasmRelocationEntry &B) { |
| 934 | return (A.Offset + A.FixupSection->getSectionOffset()) < |
| 935 | (B.Offset + B.FixupSection->getSectionOffset()); |
| 936 | }); |
| 937 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 938 | SectionBookkeeping Section; |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 939 | startCustomSection(Section, std::string("reloc.") + Name.str()); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 940 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 941 | encodeULEB128(SectionIndex, W.OS); |
Sam Clegg | f77dc2a | 2018-08-22 17:27:31 +0000 | [diff] [blame] | 942 | encodeULEB128(Relocs.size(), W.OS); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 943 | for (const WasmRelocationEntry &RelEntry : Relocs) { |
| 944 | uint64_t Offset = |
| 945 | RelEntry.Offset + RelEntry.FixupSection->getSectionOffset(); |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 946 | uint32_t Index = getRelocationIndexValue(RelEntry); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 947 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 948 | W.OS << char(RelEntry.Type); |
| 949 | encodeULEB128(Offset, W.OS); |
| 950 | encodeULEB128(Index, W.OS); |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 951 | if (RelEntry.hasAddend()) |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 952 | encodeSLEB128(RelEntry.Addend, W.OS); |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 953 | } |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 954 | |
| 955 | endSection(Section); |
| 956 | } |
| 957 | |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 958 | void WasmObjectWriter::writeCustomRelocSections() { |
| 959 | for (const auto &Sec : CustomSections) { |
| 960 | auto &Relocations = CustomSectionsRelocations[Sec.Section]; |
| 961 | writeRelocSection(Sec.OutputIndex, Sec.Name, Relocations); |
| 962 | } |
| 963 | } |
| 964 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 965 | void WasmObjectWriter::writeLinkingMetaDataSection( |
Sam Clegg | 86b4a09 | 2018-02-27 23:57:37 +0000 | [diff] [blame] | 966 | ArrayRef<wasm::WasmSymbolInfo> SymbolInfos, |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 967 | ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs, |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 968 | const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) { |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 969 | SectionBookkeeping Section; |
Sam Clegg | 2322a93 | 2018-04-23 19:16:19 +0000 | [diff] [blame] | 970 | startCustomSection(Section, "linking"); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 971 | encodeULEB128(wasm::WasmMetadataVersion, W.OS); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 972 | |
Sam Clegg | 6bb5a41 | 2018-04-26 18:15:32 +0000 | [diff] [blame] | 973 | SectionBookkeeping SubSection; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 974 | if (SymbolInfos.size() != 0) { |
| 975 | startSection(SubSection, wasm::WASM_SYMBOL_TABLE); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 976 | encodeULEB128(SymbolInfos.size(), W.OS); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 977 | for (const wasm::WasmSymbolInfo &Sym : SymbolInfos) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 978 | encodeULEB128(Sym.Kind, W.OS); |
| 979 | encodeULEB128(Sym.Flags, W.OS); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 980 | switch (Sym.Kind) { |
| 981 | case wasm::WASM_SYMBOL_TYPE_FUNCTION: |
| 982 | case wasm::WASM_SYMBOL_TYPE_GLOBAL: |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 983 | case wasm::WASM_SYMBOL_TYPE_EVENT: |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 984 | encodeULEB128(Sym.ElementIndex, W.OS); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 985 | if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) |
| 986 | writeString(Sym.Name); |
| 987 | break; |
| 988 | case wasm::WASM_SYMBOL_TYPE_DATA: |
| 989 | writeString(Sym.Name); |
| 990 | if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 991 | encodeULEB128(Sym.DataRef.Segment, W.OS); |
| 992 | encodeULEB128(Sym.DataRef.Offset, W.OS); |
| 993 | encodeULEB128(Sym.DataRef.Size, W.OS); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 994 | } |
| 995 | break; |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 996 | case wasm::WASM_SYMBOL_TYPE_SECTION: { |
| 997 | const uint32_t SectionIndex = |
| 998 | CustomSections[Sym.ElementIndex].OutputIndex; |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 999 | encodeULEB128(SectionIndex, W.OS); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 1000 | break; |
| 1001 | } |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1002 | default: |
| 1003 | llvm_unreachable("unexpected kind"); |
| 1004 | } |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1005 | } |
| 1006 | endSection(SubSection); |
| 1007 | } |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 1008 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1009 | if (DataSegments.size()) { |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 1010 | startSection(SubSection, wasm::WASM_SEGMENT_INFO); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1011 | encodeULEB128(DataSegments.size(), W.OS); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1012 | for (const WasmDataSegment &Segment : DataSegments) { |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 1013 | writeString(Segment.Name); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1014 | encodeULEB128(Segment.Alignment, W.OS); |
| 1015 | encodeULEB128(Segment.Flags, W.OS); |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 1016 | } |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 1017 | endSection(SubSection); |
| 1018 | } |
| 1019 | |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1020 | if (!InitFuncs.empty()) { |
| 1021 | startSection(SubSection, wasm::WASM_INIT_FUNCS); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1022 | encodeULEB128(InitFuncs.size(), W.OS); |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1023 | for (auto &StartFunc : InitFuncs) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 1024 | encodeULEB128(StartFunc.first, W.OS); // priority |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1025 | encodeULEB128(StartFunc.second, W.OS); // function index |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1026 | } |
| 1027 | endSection(SubSection); |
| 1028 | } |
| 1029 | |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1030 | if (Comdats.size()) { |
| 1031 | startSection(SubSection, wasm::WASM_COMDAT_INFO); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1032 | encodeULEB128(Comdats.size(), W.OS); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1033 | for (const auto &C : Comdats) { |
| 1034 | writeString(C.first); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1035 | encodeULEB128(0, W.OS); // flags for future use |
| 1036 | encodeULEB128(C.second.size(), W.OS); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1037 | for (const WasmComdatEntry &Entry : C.second) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1038 | encodeULEB128(Entry.Kind, W.OS); |
| 1039 | encodeULEB128(Entry.Index, W.OS); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | endSection(SubSection); |
| 1043 | } |
| 1044 | |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 1045 | endSection(Section); |
| 1046 | } |
| 1047 | |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 1048 | void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection, |
| 1049 | const MCAssembler &Asm, |
| 1050 | const MCAsmLayout &Layout) { |
| 1051 | SectionBookkeeping Section; |
| 1052 | auto *Sec = CustomSection.Section; |
| 1053 | startCustomSection(Section, CustomSection.Name); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 1054 | |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 1055 | Sec->setSectionOffset(W.OS.tell() - Section.ContentsOffset); |
| 1056 | Asm.writeSectionData(W.OS, Sec, Layout); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 1057 | |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 1058 | CustomSection.OutputContentsOffset = Section.ContentsOffset; |
| 1059 | CustomSection.OutputIndex = Section.Index; |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 1060 | |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 1061 | endSection(Section); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 1062 | |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 1063 | // Apply fixups. |
| 1064 | auto &Relocations = CustomSectionsRelocations[CustomSection.Section]; |
| 1065 | applyRelocations(Relocations, CustomSection.OutputContentsOffset); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 1068 | uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm &Symbol) { |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1069 | assert(Symbol.isFunction()); |
| 1070 | assert(TypeIndices.count(&Symbol)); |
| 1071 | return TypeIndices[&Symbol]; |
| 1072 | } |
| 1073 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1074 | uint32_t WasmObjectWriter::getEventType(const MCSymbolWasm &Symbol) { |
| 1075 | assert(Symbol.isEvent()); |
| 1076 | assert(TypeIndices.count(&Symbol)); |
| 1077 | return TypeIndices[&Symbol]; |
| 1078 | } |
| 1079 | |
Heejin Ahn | 38b12f5 | 2018-11-20 00:38:10 +0000 | [diff] [blame] | 1080 | void WasmObjectWriter::registerFunctionType(const MCSymbolWasm &Symbol) { |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1081 | assert(Symbol.isFunction()); |
| 1082 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1083 | WasmSignature S; |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 1084 | const MCSymbolWasm *ResolvedSym = resolveSymbol(Symbol); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 1085 | if (auto *Sig = ResolvedSym->getSignature()) { |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1086 | S.Returns = Sig->Returns; |
| 1087 | S.Params = Sig->Params; |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 1088 | } |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1089 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1090 | auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size())); |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1091 | if (Pair.second) |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1092 | Signatures.push_back(S); |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1093 | TypeIndices[&Symbol] = Pair.first->second; |
| 1094 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1095 | LLVM_DEBUG(dbgs() << "registerFunctionType: " << Symbol |
| 1096 | << " new:" << Pair.second << "\n"); |
| 1097 | LLVM_DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n"); |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Heejin Ahn | 38b12f5 | 2018-11-20 00:38:10 +0000 | [diff] [blame] | 1100 | void WasmObjectWriter::registerEventType(const MCSymbolWasm &Symbol) { |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1101 | assert(Symbol.isEvent()); |
| 1102 | |
| 1103 | // TODO Currently we don't generate imported exceptions, but if we do, we |
| 1104 | // should have a way of infering types of imported exceptions. |
| 1105 | WasmSignature S; |
| 1106 | if (auto *Sig = Symbol.getSignature()) { |
| 1107 | S.Returns = Sig->Returns; |
| 1108 | S.Params = Sig->Params; |
| 1109 | } |
| 1110 | |
| 1111 | auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size())); |
| 1112 | if (Pair.second) |
| 1113 | Signatures.push_back(S); |
| 1114 | TypeIndices[&Symbol] = Pair.first->second; |
| 1115 | |
| 1116 | LLVM_DEBUG(dbgs() << "registerEventType: " << Symbol << " new:" << Pair.second |
| 1117 | << "\n"); |
| 1118 | LLVM_DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n"); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1121 | static bool isInSymtab(const MCSymbolWasm &Sym) { |
| 1122 | if (Sym.isUsedInReloc()) |
| 1123 | return true; |
| 1124 | |
| 1125 | if (Sym.isComdat() && !Sym.isDefined()) |
| 1126 | return false; |
| 1127 | |
| 1128 | if (Sym.isTemporary() && Sym.getName().empty()) |
| 1129 | return false; |
| 1130 | |
| 1131 | if (Sym.isTemporary() && Sym.isData() && !Sym.getSize()) |
| 1132 | return false; |
| 1133 | |
| 1134 | if (Sym.isSection()) |
| 1135 | return false; |
| 1136 | |
| 1137 | return true; |
| 1138 | } |
| 1139 | |
Peter Collingbourne | 438390f | 2018-05-21 18:23:50 +0000 | [diff] [blame] | 1140 | uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm, |
| 1141 | const MCAsmLayout &Layout) { |
| 1142 | uint64_t StartOffset = W.OS.tell(); |
| 1143 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1144 | LLVM_DEBUG(dbgs() << "WasmObjectWriter::writeObject\n"); |
Dan Gohman | 82607f5 | 2017-02-24 23:46:05 +0000 | [diff] [blame] | 1145 | MCContext &Ctx = Asm.getContext(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1146 | |
| 1147 | // Collect information from the available symbols. |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1148 | SmallVector<WasmFunction, 4> Functions; |
| 1149 | SmallVector<uint32_t, 4> TableElems; |
Sam Clegg | 8defa95 | 2018-02-12 22:41:29 +0000 | [diff] [blame] | 1150 | SmallVector<wasm::WasmImport, 4> Imports; |
| 1151 | SmallVector<wasm::WasmExport, 4> Exports; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1152 | SmallVector<wasm::WasmEventType, 1> Events; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1153 | SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos; |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1154 | SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs; |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1155 | std::map<StringRef, std::vector<WasmComdatEntry>> Comdats; |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 1156 | uint32_t DataSize = 0; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1157 | |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 1158 | // For now, always emit the memory import, since loads and stores are not |
| 1159 | // valid without it. In the future, we could perhaps be more clever and omit |
| 1160 | // it if there are no loads or stores. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 1161 | auto *MemorySym = |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 1162 | cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__linear_memory")); |
Sam Clegg | 8defa95 | 2018-02-12 22:41:29 +0000 | [diff] [blame] | 1163 | wasm::WasmImport MemImport; |
Dan Gohman | f726e44 | 2019-02-01 22:27:34 +0000 | [diff] [blame] | 1164 | MemImport.Module = MemorySym->getImportModule(); |
| 1165 | MemImport.Field = MemorySym->getImportName(); |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 1166 | MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY; |
| 1167 | Imports.push_back(MemImport); |
| 1168 | |
| 1169 | // For now, always emit the table section, since indirect calls are not |
| 1170 | // valid without it. In the future, we could perhaps be more clever and omit |
| 1171 | // it if there are no indirect calls. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 1172 | auto *TableSym = |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 1173 | cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__indirect_function_table")); |
Sam Clegg | 8defa95 | 2018-02-12 22:41:29 +0000 | [diff] [blame] | 1174 | wasm::WasmImport TableImport; |
Dan Gohman | f726e44 | 2019-02-01 22:27:34 +0000 | [diff] [blame] | 1175 | TableImport.Module = TableSym->getImportModule(); |
| 1176 | TableImport.Field = TableSym->getImportName(); |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 1177 | TableImport.Kind = wasm::WASM_EXTERNAL_TABLE; |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 1178 | TableImport.Table.ElemType = wasm::WASM_TYPE_FUNCREF; |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 1179 | Imports.push_back(TableImport); |
| 1180 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1181 | // Populate SignatureIndices, and Imports and WasmIndices for undefined |
Nicholas Wilson | 586320c | 2018-02-28 17:19:48 +0000 | [diff] [blame] | 1182 | // symbols. This must be done before populating WasmIndices for defined |
| 1183 | // symbols. |
Dan Gohman | 32ce5ca | 2017-12-05 18:29:48 +0000 | [diff] [blame] | 1184 | for (const MCSymbol &S : Asm.symbols()) { |
| 1185 | const auto &WS = static_cast<const MCSymbolWasm &>(S); |
| 1186 | |
| 1187 | // Register types for all functions, including those with private linkage |
Sam Clegg | 9f3fe42 | 2018-01-17 19:28:43 +0000 | [diff] [blame] | 1188 | // (because wasm always needs a type signature). |
Dan Gohman | 32ce5ca | 2017-12-05 18:29:48 +0000 | [diff] [blame] | 1189 | if (WS.isFunction()) |
| 1190 | registerFunctionType(WS); |
| 1191 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1192 | if (WS.isEvent()) |
| 1193 | registerEventType(WS); |
| 1194 | |
Dan Gohman | 32ce5ca | 2017-12-05 18:29:48 +0000 | [diff] [blame] | 1195 | if (WS.isTemporary()) |
| 1196 | continue; |
| 1197 | |
| 1198 | // If the symbol is not defined in this translation unit, import it. |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1199 | if (!WS.isDefined() && !WS.isComdat()) { |
Dan Gohman | 32ce5ca | 2017-12-05 18:29:48 +0000 | [diff] [blame] | 1200 | if (WS.isFunction()) { |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1201 | wasm::WasmImport Import; |
Dan Gohman | f726e44 | 2019-02-01 22:27:34 +0000 | [diff] [blame] | 1202 | Import.Module = WS.getImportModule(); |
| 1203 | Import.Field = WS.getImportName(); |
Dan Gohman | 32ce5ca | 2017-12-05 18:29:48 +0000 | [diff] [blame] | 1204 | Import.Kind = wasm::WASM_EXTERNAL_FUNCTION; |
Sam Clegg | 8defa95 | 2018-02-12 22:41:29 +0000 | [diff] [blame] | 1205 | Import.SigIndex = getFunctionType(WS); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1206 | Imports.push_back(Import); |
| 1207 | WasmIndices[&WS] = NumFunctionImports++; |
| 1208 | } else if (WS.isGlobal()) { |
Nicholas Wilson | 15f349f | 2018-03-09 16:30:44 +0000 | [diff] [blame] | 1209 | if (WS.isWeak()) |
| 1210 | report_fatal_error("undefined global symbol cannot be weak"); |
| 1211 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1212 | wasm::WasmImport Import; |
Dan Gohman | f726e44 | 2019-02-01 22:27:34 +0000 | [diff] [blame] | 1213 | Import.Module = WS.getImportModule(); |
| 1214 | Import.Field = WS.getImportName(); |
Dan Gohman | 32ce5ca | 2017-12-05 18:29:48 +0000 | [diff] [blame] | 1215 | Import.Kind = wasm::WASM_EXTERNAL_GLOBAL; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1216 | Import.Global = WS.getGlobalType(); |
| 1217 | Imports.push_back(Import); |
| 1218 | WasmIndices[&WS] = NumGlobalImports++; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1219 | } else if (WS.isEvent()) { |
| 1220 | if (WS.isWeak()) |
| 1221 | report_fatal_error("undefined event symbol cannot be weak"); |
| 1222 | |
| 1223 | wasm::WasmImport Import; |
Dan Gohman | f726e44 | 2019-02-01 22:27:34 +0000 | [diff] [blame] | 1224 | Import.Module = WS.getImportModule(); |
| 1225 | Import.Field = WS.getImportName(); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1226 | Import.Kind = wasm::WASM_EXTERNAL_EVENT; |
| 1227 | Import.Event.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION; |
| 1228 | Import.Event.SigIndex = getEventType(WS); |
| 1229 | Imports.push_back(Import); |
| 1230 | WasmIndices[&WS] = NumEventImports++; |
Dan Gohman | 32ce5ca | 2017-12-05 18:29:48 +0000 | [diff] [blame] | 1231 | } |
Dan Gohman | 32ce5ca | 2017-12-05 18:29:48 +0000 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1235 | // Populate DataSegments and CustomSections, which must be done before |
| 1236 | // populating DataLocations. |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 1237 | for (MCSection &Sec : Asm) { |
| 1238 | auto &Section = static_cast<MCSectionWasm &>(Sec); |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1239 | StringRef SectionName = Section.getSectionName(); |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 1240 | |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1241 | // .init_array sections are handled specially elsewhere. |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1242 | if (SectionName.startswith(".init_array")) |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1243 | continue; |
| 1244 | |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1245 | // Code is handled separately |
| 1246 | if (Section.getKind().isText()) |
| 1247 | continue; |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1248 | |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1249 | if (Section.isWasmData()) { |
| 1250 | uint32_t SegmentIndex = DataSegments.size(); |
| 1251 | DataSize = alignTo(DataSize, Section.getAlignment()); |
| 1252 | DataSegments.emplace_back(); |
| 1253 | WasmDataSegment &Segment = DataSegments.back(); |
| 1254 | Segment.Name = SectionName; |
| 1255 | Segment.Offset = DataSize; |
| 1256 | Segment.Section = &Section; |
| 1257 | addData(Segment.Data, Section); |
Sam Clegg | 56c587a | 2019-01-16 01:34:48 +0000 | [diff] [blame] | 1258 | Segment.Alignment = Log2_32(Section.getAlignment()); |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1259 | Segment.Flags = 0; |
| 1260 | DataSize += Segment.Data.size(); |
| 1261 | Section.setSegmentIndex(SegmentIndex); |
| 1262 | |
| 1263 | if (const MCSymbolWasm *C = Section.getGroup()) { |
| 1264 | Comdats[C->getName()].emplace_back( |
| 1265 | WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex}); |
| 1266 | } |
| 1267 | } else { |
| 1268 | // Create custom sections |
| 1269 | assert(Sec.getKind().isMetadata()); |
| 1270 | |
| 1271 | StringRef Name = SectionName; |
| 1272 | |
| 1273 | // For user-defined custom sections, strip the prefix |
| 1274 | if (Name.startswith(".custom_section.")) |
| 1275 | Name = Name.substr(strlen(".custom_section.")); |
| 1276 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 1277 | MCSymbol *Begin = Sec.getBeginSymbol(); |
Sam Clegg | fb807d4 | 2018-05-07 19:40:50 +0000 | [diff] [blame] | 1278 | if (Begin) { |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1279 | WasmIndices[cast<MCSymbolWasm>(Begin)] = CustomSections.size(); |
Sam Clegg | b210c64 | 2018-05-10 17:38:35 +0000 | [diff] [blame] | 1280 | if (SectionName != Begin->getName()) |
Sam Clegg | fb807d4 | 2018-05-07 19:40:50 +0000 | [diff] [blame] | 1281 | report_fatal_error("section name and begin symbol should match: " + |
Sam Clegg | b210c64 | 2018-05-10 17:38:35 +0000 | [diff] [blame] | 1282 | Twine(SectionName)); |
Sam Clegg | fb807d4 | 2018-05-07 19:40:50 +0000 | [diff] [blame] | 1283 | } |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 1284 | |
| 1285 | // Separate out the producers section |
| 1286 | if (Name == "producers") { |
| 1287 | ProducersSection = llvm::make_unique<WasmCustomSection>(Name, &Section); |
| 1288 | continue; |
| 1289 | } |
| 1290 | |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1291 | CustomSections.emplace_back(Name, &Section); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1292 | } |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Nicholas Wilson | 586320c | 2018-02-28 17:19:48 +0000 | [diff] [blame] | 1295 | // Populate WasmIndices and DataLocations for defined symbols. |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1296 | for (const MCSymbol &S : Asm.symbols()) { |
| 1297 | // Ignore unnamed temporary symbols, which aren't ever exported, imported, |
| 1298 | // or used in relocations. |
| 1299 | if (S.isTemporary() && S.getName().empty()) |
| 1300 | continue; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1301 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1302 | const auto &WS = static_cast<const MCSymbolWasm &>(S); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1303 | LLVM_DEBUG( |
| 1304 | dbgs() << "MCSymbol: " << toString(WS.getType()) << " '" << S << "'" |
| 1305 | << " isDefined=" << S.isDefined() << " isExternal=" |
| 1306 | << S.isExternal() << " isTemporary=" << S.isTemporary() |
| 1307 | << " isWeak=" << WS.isWeak() << " isHidden=" << WS.isHidden() |
| 1308 | << " isVariable=" << WS.isVariable() << "\n"); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1309 | |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1310 | if (WS.isVariable()) |
| 1311 | continue; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1312 | if (WS.isComdat() && !WS.isDefined()) |
| 1313 | continue; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1314 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1315 | if (WS.isFunction()) { |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1316 | unsigned Index; |
Sam Clegg | cd65f69 | 2018-01-11 23:59:16 +0000 | [diff] [blame] | 1317 | if (WS.isDefined()) { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1318 | if (WS.getOffset() != 0) |
| 1319 | report_fatal_error( |
| 1320 | "function sections must contain one function each"); |
| 1321 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 1322 | if (WS.getSize() == nullptr) |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1323 | report_fatal_error( |
| 1324 | "function symbols must have a size set with .size"); |
| 1325 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1326 | // A definition. Write out the function body. |
Sam Clegg | 9f3fe42 | 2018-01-17 19:28:43 +0000 | [diff] [blame] | 1327 | Index = NumFunctionImports + Functions.size(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1328 | WasmFunction Func; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1329 | Func.SigIndex = getFunctionType(WS); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1330 | Func.Sym = &WS; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1331 | WasmIndices[&WS] = Index; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1332 | Functions.push_back(Func); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1333 | |
| 1334 | auto &Section = static_cast<MCSectionWasm &>(WS.getSection()); |
| 1335 | if (const MCSymbolWasm *C = Section.getGroup()) { |
| 1336 | Comdats[C->getName()].emplace_back( |
| 1337 | WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index}); |
| 1338 | } |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1339 | } else { |
| 1340 | // An import; the index was assigned above. |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1341 | Index = WasmIndices.find(&WS)->second; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1344 | LLVM_DEBUG(dbgs() << " -> function index: " << Index << "\n"); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1345 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1346 | } else if (WS.isData()) { |
Sam Clegg | c38e947 | 2017-06-02 01:05:24 +0000 | [diff] [blame] | 1347 | if (WS.isTemporary() && !WS.getSize()) |
| 1348 | continue; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1349 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1350 | if (!WS.isDefined()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1351 | LLVM_DEBUG(dbgs() << " -> segment index: -1" |
| 1352 | << "\n"); |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1353 | continue; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1354 | } |
Sam Clegg | c38e947 | 2017-06-02 01:05:24 +0000 | [diff] [blame] | 1355 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1356 | if (!WS.getSize()) |
| 1357 | report_fatal_error("data symbols must have a size set with .size: " + |
| 1358 | WS.getName()); |
Sam Clegg | c38e947 | 2017-06-02 01:05:24 +0000 | [diff] [blame] | 1359 | |
Sam Clegg | fe6414b | 2017-06-21 23:46:41 +0000 | [diff] [blame] | 1360 | int64_t Size = 0; |
| 1361 | if (!WS.getSize()->evaluateAsAbsolute(Size, Layout)) |
| 1362 | report_fatal_error(".size expression must be evaluatable"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1363 | |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 1364 | auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection()); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1365 | assert(DataSection.isWasmData()); |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 1366 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1367 | // For each data symbol, export it in the symtab as a reference to the |
| 1368 | // corresponding Wasm data segment. |
| 1369 | wasm::WasmDataReference Ref = wasm::WasmDataReference{ |
| 1370 | DataSection.getSegmentIndex(), |
| 1371 | static_cast<uint32_t>(Layout.getSymbolOffset(WS)), |
| 1372 | static_cast<uint32_t>(Size)}; |
| 1373 | DataLocations[&WS] = Ref; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1374 | LLVM_DEBUG(dbgs() << " -> segment index: " << Ref.Segment << "\n"); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1375 | |
Sam Clegg | a165f2d | 2018-04-30 19:40:57 +0000 | [diff] [blame] | 1376 | } else if (WS.isGlobal()) { |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1377 | // A "true" Wasm global (currently just __stack_pointer) |
Eric Christopher | 545932b | 2018-02-23 21:14:47 +0000 | [diff] [blame] | 1378 | if (WS.isDefined()) |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1379 | report_fatal_error("don't yet support defined globals"); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1380 | |
Eric Christopher | 545932b | 2018-02-23 21:14:47 +0000 | [diff] [blame] | 1381 | // An import; the index was assigned above |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1382 | LLVM_DEBUG(dbgs() << " -> global index: " |
| 1383 | << WasmIndices.find(&WS)->second << "\n"); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1384 | |
| 1385 | } else if (WS.isEvent()) { |
| 1386 | // C++ exception symbol (__cpp_exception) |
| 1387 | unsigned Index; |
| 1388 | if (WS.isDefined()) { |
| 1389 | Index = NumEventImports + Events.size(); |
| 1390 | wasm::WasmEventType Event; |
| 1391 | Event.SigIndex = getEventType(WS); |
| 1392 | Event.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION; |
| 1393 | WasmIndices[&WS] = Index; |
| 1394 | Events.push_back(Event); |
| 1395 | } else { |
| 1396 | // An import; the index was assigned above. |
| 1397 | Index = WasmIndices.find(&WS)->second; |
| 1398 | } |
| 1399 | LLVM_DEBUG(dbgs() << " -> event index: " << WasmIndices.find(&WS)->second |
| 1400 | << "\n"); |
| 1401 | |
Sam Clegg | a165f2d | 2018-04-30 19:40:57 +0000 | [diff] [blame] | 1402 | } else { |
| 1403 | assert(WS.isSection()); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1404 | } |
| 1405 | } |
| 1406 | |
Nicholas Wilson | 586320c | 2018-02-28 17:19:48 +0000 | [diff] [blame] | 1407 | // Populate WasmIndices and DataLocations for aliased symbols. We need to |
| 1408 | // process these in a separate pass because we need to have processed the |
| 1409 | // target of the alias before the alias itself and the symbols are not |
| 1410 | // necessarily ordered in this way. |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1411 | for (const MCSymbol &S : Asm.symbols()) { |
| 1412 | if (!S.isVariable()) |
| 1413 | continue; |
Sam Clegg | 31a2c80 | 2017-09-20 21:17:04 +0000 | [diff] [blame] | 1414 | |
Sam Clegg | cd65f69 | 2018-01-11 23:59:16 +0000 | [diff] [blame] | 1415 | assert(S.isDefined()); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1416 | |
Sam Clegg | 5e3d33a | 2017-07-07 02:01:29 +0000 | [diff] [blame] | 1417 | // Find the target symbol of this weak alias and export that index |
Sam Clegg | aff1c4d | 2017-09-15 19:22:01 +0000 | [diff] [blame] | 1418 | const auto &WS = static_cast<const MCSymbolWasm &>(S); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 1419 | const MCSymbolWasm *ResolvedSym = resolveSymbol(WS); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1420 | LLVM_DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym |
| 1421 | << "'\n"); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1422 | |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1423 | if (WS.isFunction()) { |
| 1424 | assert(WasmIndices.count(ResolvedSym) > 0); |
| 1425 | uint32_t WasmIndex = WasmIndices.find(ResolvedSym)->second; |
| 1426 | WasmIndices[&WS] = WasmIndex; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1427 | LLVM_DEBUG(dbgs() << " -> index:" << WasmIndex << "\n"); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1428 | } else if (WS.isData()) { |
| 1429 | assert(DataLocations.count(ResolvedSym) > 0); |
| 1430 | const wasm::WasmDataReference &Ref = |
| 1431 | DataLocations.find(ResolvedSym)->second; |
| 1432 | DataLocations[&WS] = Ref; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1433 | LLVM_DEBUG(dbgs() << " -> index:" << Ref.Segment << "\n"); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1434 | } else { |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1435 | report_fatal_error("don't yet support global/event aliases"); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1436 | } |
Nicholas Wilson | 586320c | 2018-02-28 17:19:48 +0000 | [diff] [blame] | 1437 | } |
Sam Clegg | 31a2c80 | 2017-09-20 21:17:04 +0000 | [diff] [blame] | 1438 | |
Nicholas Wilson | 586320c | 2018-02-28 17:19:48 +0000 | [diff] [blame] | 1439 | // Finally, populate the symbol table itself, in its "natural" order. |
| 1440 | for (const MCSymbol &S : Asm.symbols()) { |
| 1441 | const auto &WS = static_cast<const MCSymbolWasm &>(S); |
Sam Clegg | 25d8e68 | 2018-05-08 00:08:21 +0000 | [diff] [blame] | 1442 | if (!isInSymtab(WS)) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 1443 | WS.setIndex(InvalidIndex); |
Nicholas Wilson | 586320c | 2018-02-28 17:19:48 +0000 | [diff] [blame] | 1444 | continue; |
Sam Clegg | 25d8e68 | 2018-05-08 00:08:21 +0000 | [diff] [blame] | 1445 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1446 | LLVM_DEBUG(dbgs() << "adding to symtab: " << WS << "\n"); |
Nicholas Wilson | 586320c | 2018-02-28 17:19:48 +0000 | [diff] [blame] | 1447 | |
| 1448 | uint32_t Flags = 0; |
| 1449 | if (WS.isWeak()) |
| 1450 | Flags |= wasm::WASM_SYMBOL_BINDING_WEAK; |
| 1451 | if (WS.isHidden()) |
| 1452 | Flags |= wasm::WASM_SYMBOL_VISIBILITY_HIDDEN; |
| 1453 | if (!WS.isExternal() && WS.isDefined()) |
| 1454 | Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL; |
| 1455 | if (WS.isUndefined()) |
| 1456 | Flags |= wasm::WASM_SYMBOL_UNDEFINED; |
Sam Clegg | d6ef8da | 2019-02-07 01:24:44 +0000 | [diff] [blame] | 1457 | if (WS.isExported()) |
| 1458 | Flags |= wasm::WASM_SYMBOL_EXPORTED; |
Nicholas Wilson | 586320c | 2018-02-28 17:19:48 +0000 | [diff] [blame] | 1459 | |
| 1460 | wasm::WasmSymbolInfo Info; |
| 1461 | Info.Name = WS.getName(); |
| 1462 | Info.Kind = WS.getType(); |
| 1463 | Info.Flags = Flags; |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1464 | if (!WS.isData()) { |
| 1465 | assert(WasmIndices.count(&WS) > 0); |
Nicholas Wilson | 586320c | 2018-02-28 17:19:48 +0000 | [diff] [blame] | 1466 | Info.ElementIndex = WasmIndices.find(&WS)->second; |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1467 | } else if (WS.isDefined()) { |
| 1468 | assert(DataLocations.count(&WS) > 0); |
Nicholas Wilson | 586320c | 2018-02-28 17:19:48 +0000 | [diff] [blame] | 1469 | Info.DataRef = DataLocations.find(&WS)->second; |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1470 | } |
Sam Clegg | 25d8e68 | 2018-05-08 00:08:21 +0000 | [diff] [blame] | 1471 | WS.setIndex(SymbolInfos.size()); |
Nicholas Wilson | 586320c | 2018-02-28 17:19:48 +0000 | [diff] [blame] | 1472 | SymbolInfos.emplace_back(Info); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Sam Clegg | 6006e09 | 2017-12-22 20:31:39 +0000 | [diff] [blame] | 1475 | { |
| 1476 | auto HandleReloc = [&](const WasmRelocationEntry &Rel) { |
Sam Clegg | f9edbe9 | 2018-01-31 19:28:47 +0000 | [diff] [blame] | 1477 | // Functions referenced by a relocation need to put in the table. This is |
| 1478 | // purely to make the object file's provisional values readable, and is |
| 1479 | // ignored by the linker, which re-calculates the relocations itself. |
Sam Clegg | d1152a2 | 2019-02-04 17:28:46 +0000 | [diff] [blame] | 1480 | if (Rel.Type != wasm::R_WASM_TABLE_INDEX_I32 && |
| 1481 | Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB) |
Sam Clegg | f9edbe9 | 2018-01-31 19:28:47 +0000 | [diff] [blame] | 1482 | return; |
| 1483 | assert(Rel.Symbol->isFunction()); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 1484 | const MCSymbolWasm &WS = *resolveSymbol(*Rel.Symbol); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1485 | uint32_t FunctionIndex = WasmIndices.find(&WS)->second; |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 1486 | uint32_t TableIndex = TableElems.size() + InitialTableOffset; |
Sam Clegg | f9edbe9 | 2018-01-31 19:28:47 +0000 | [diff] [blame] | 1487 | if (TableIndices.try_emplace(&WS, TableIndex).second) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1488 | LLVM_DEBUG(dbgs() << " -> adding " << WS.getName() |
| 1489 | << " to table: " << TableIndex << "\n"); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1490 | TableElems.push_back(FunctionIndex); |
Sam Clegg | f9edbe9 | 2018-01-31 19:28:47 +0000 | [diff] [blame] | 1491 | registerFunctionType(WS); |
Sam Clegg | 6006e09 | 2017-12-22 20:31:39 +0000 | [diff] [blame] | 1492 | } |
| 1493 | }; |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 1494 | |
Sam Clegg | 6006e09 | 2017-12-22 20:31:39 +0000 | [diff] [blame] | 1495 | for (const WasmRelocationEntry &RelEntry : CodeRelocations) |
| 1496 | HandleReloc(RelEntry); |
| 1497 | for (const WasmRelocationEntry &RelEntry : DataRelocations) |
| 1498 | HandleReloc(RelEntry); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1501 | // Translate .init_array section contents into start functions. |
| 1502 | for (const MCSection &S : Asm) { |
| 1503 | const auto &WS = static_cast<const MCSectionWasm &>(S); |
| 1504 | if (WS.getSectionName().startswith(".fini_array")) |
| 1505 | report_fatal_error(".fini_array sections are unsupported"); |
| 1506 | if (!WS.getSectionName().startswith(".init_array")) |
| 1507 | continue; |
| 1508 | if (WS.getFragmentList().empty()) |
| 1509 | continue; |
Sam Clegg | b210c64 | 2018-05-10 17:38:35 +0000 | [diff] [blame] | 1510 | |
| 1511 | // init_array is expected to contain a single non-empty data fragment |
| 1512 | if (WS.getFragmentList().size() != 3) |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1513 | report_fatal_error("only one .init_array section fragment supported"); |
Sam Clegg | b210c64 | 2018-05-10 17:38:35 +0000 | [diff] [blame] | 1514 | |
| 1515 | auto IT = WS.begin(); |
| 1516 | const MCFragment &EmptyFrag = *IT; |
| 1517 | if (EmptyFrag.getKind() != MCFragment::FT_Data) |
| 1518 | report_fatal_error(".init_array section should be aligned"); |
| 1519 | |
| 1520 | IT = std::next(IT); |
| 1521 | const MCFragment &AlignFrag = *IT; |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1522 | if (AlignFrag.getKind() != MCFragment::FT_Align) |
| 1523 | report_fatal_error(".init_array section should be aligned"); |
| 1524 | if (cast<MCAlignFragment>(AlignFrag).getAlignment() != (is64Bit() ? 8 : 4)) |
| 1525 | report_fatal_error(".init_array section should be aligned for pointers"); |
Sam Clegg | b210c64 | 2018-05-10 17:38:35 +0000 | [diff] [blame] | 1526 | |
| 1527 | const MCFragment &Frag = *std::next(IT); |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1528 | if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data) |
| 1529 | report_fatal_error("only data supported in .init_array section"); |
Sam Clegg | b210c64 | 2018-05-10 17:38:35 +0000 | [diff] [blame] | 1530 | |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1531 | uint16_t Priority = UINT16_MAX; |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1532 | unsigned PrefixLength = strlen(".init_array"); |
| 1533 | if (WS.getSectionName().size() > PrefixLength) { |
| 1534 | if (WS.getSectionName()[PrefixLength] != '.') |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 1535 | report_fatal_error( |
| 1536 | ".init_array section priority should start with '.'"); |
Sam Clegg | 4d57fbd | 2018-05-02 23:11:38 +0000 | [diff] [blame] | 1537 | if (WS.getSectionName() |
| 1538 | .substr(PrefixLength + 1) |
| 1539 | .getAsInteger(10, Priority)) |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1540 | report_fatal_error("invalid .init_array section priority"); |
| 1541 | } |
| 1542 | const auto &DataFrag = cast<MCDataFragment>(Frag); |
| 1543 | const SmallVectorImpl<char> &Contents = DataFrag.getContents(); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 1544 | for (const uint8_t * |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 1545 | P = (const uint8_t *)Contents.data(), |
| 1546 | *End = (const uint8_t *)Contents.data() + Contents.size(); |
| 1547 | P != End; ++P) { |
| 1548 | if (*P != 0) |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1549 | report_fatal_error("non-symbolic data in .init_array section"); |
| 1550 | } |
| 1551 | for (const MCFixup &Fixup : DataFrag.getFixups()) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 1552 | assert(Fixup.getKind() == |
| 1553 | MCFixup::getKindForSize(is64Bit() ? 8 : 4, false)); |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1554 | const MCExpr *Expr = Fixup.getValue(); |
| 1555 | auto *Sym = dyn_cast<MCSymbolRefExpr>(Expr); |
| 1556 | if (!Sym) |
| 1557 | report_fatal_error("fixups in .init_array should be symbol references"); |
| 1558 | if (Sym->getKind() != MCSymbolRefExpr::VK_WebAssembly_FUNCTION) |
| 1559 | report_fatal_error("symbols in .init_array should be for functions"); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 1560 | if (Sym->getSymbol().getIndex() == InvalidIndex) |
Sam Clegg | 25d8e68 | 2018-05-08 00:08:21 +0000 | [diff] [blame] | 1561 | report_fatal_error("symbols in .init_array should exist in symbtab"); |
| 1562 | InitFuncs.push_back( |
| 1563 | std::make_pair(Priority, Sym->getSymbol().getIndex())); |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1564 | } |
| 1565 | } |
| 1566 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1567 | // Write out the Wasm header. |
| 1568 | writeHeader(Asm); |
| 1569 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1570 | writeTypeSection(Signatures); |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 1571 | writeImportSection(Imports, DataSize, TableElems.size()); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 1572 | writeFunctionSection(Functions); |
Sam Clegg | f950b24 | 2017-12-11 23:03:38 +0000 | [diff] [blame] | 1573 | // Skip the "table" section; we import the table instead. |
| 1574 | // Skip the "memory" section; we import the memory instead. |
Sam Clegg | 7c39594 | 2017-09-14 23:07:53 +0000 | [diff] [blame] | 1575 | writeGlobalSection(); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1576 | writeEventSection(Events); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 1577 | writeExportSection(Exports); |
Sam Clegg | 9e15f35 | 2017-06-03 02:01:24 +0000 | [diff] [blame] | 1578 | writeElemSection(TableElems); |
Sam Clegg | acd7d2b | 2017-06-06 19:15:05 +0000 | [diff] [blame] | 1579 | writeCodeSection(Asm, Layout, Functions); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 1580 | writeDataSection(); |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 1581 | for (auto &CustomSection : CustomSections) |
| 1582 | writeCustomSection(CustomSection, Asm, Layout); |
Nicholas Wilson | c22bfb6 | 2018-03-05 12:59:03 +0000 | [diff] [blame] | 1583 | writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats); |
Sam Clegg | 6f08c84 | 2018-04-24 18:11:36 +0000 | [diff] [blame] | 1584 | writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations); |
| 1585 | writeRelocSection(DataSectionIndex, "DATA", DataRelocations); |
Sam Clegg | 6a31a0d | 2018-04-26 19:27:28 +0000 | [diff] [blame] | 1586 | writeCustomRelocSections(); |
Thomas Lively | cbda16e | 2019-01-17 02:29:55 +0000 | [diff] [blame] | 1587 | if (ProducersSection) |
| 1588 | writeCustomSection(*ProducersSection, Asm, Layout); |
Dan Gohman | 970d02c | 2017-03-30 23:58:19 +0000 | [diff] [blame] | 1589 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1590 | // TODO: Translate the .comment section to the output. |
Peter Collingbourne | 438390f | 2018-05-21 18:23:50 +0000 | [diff] [blame] | 1591 | return W.OS.tell() - StartOffset; |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Lang Hames | 60fbc7c | 2017-10-10 16:28:07 +0000 | [diff] [blame] | 1594 | std::unique_ptr<MCObjectWriter> |
Lang Hames | 1301a87 | 2017-10-10 01:15:10 +0000 | [diff] [blame] | 1595 | llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW, |
| 1596 | raw_pwrite_stream &OS) { |
Dan Gohman | 0917c9e | 2018-01-15 17:06:23 +0000 | [diff] [blame] | 1597 | return llvm::make_unique<WasmObjectWriter>(std::move(MOTW), OS); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1598 | } |