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