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