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