Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 1 | //===- WasmObjectFile.cpp - Wasm object file implementation ---------------===// |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 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 | |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/ArrayRef.h" |
| 11 | #include "llvm/ADT/STLExtras.h" |
| 12 | #include "llvm/ADT/StringRef.h" |
| 13 | #include "llvm/ADT/Triple.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 14 | #include "llvm/BinaryFormat/Wasm.h" |
Eugene Zelenko | 9f5094d | 2017-04-21 22:03:05 +0000 | [diff] [blame] | 15 | #include "llvm/MC/SubtargetFeature.h" |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 16 | #include "llvm/Object/Binary.h" |
| 17 | #include "llvm/Object/Error.h" |
| 18 | #include "llvm/Object/ObjectFile.h" |
| 19 | #include "llvm/Object/SymbolicFile.h" |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 20 | #include "llvm/Object/Wasm.h" |
| 21 | #include "llvm/Support/Endian.h" |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Error.h" |
| 23 | #include "llvm/Support/ErrorHandling.h" |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 24 | #include "llvm/Support/LEB128.h" |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Eugene Zelenko | 9f5094d | 2017-04-21 22:03:05 +0000 | [diff] [blame] | 26 | #include <cassert> |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 27 | #include <cstdint> |
Eugene Zelenko | 9f5094d | 2017-04-21 22:03:05 +0000 | [diff] [blame] | 28 | #include <cstring> |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 29 | #include <system_error> |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 30 | |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | using namespace object; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 33 | |
| 34 | Expected<std::unique_ptr<WasmObjectFile>> |
| 35 | ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) { |
| 36 | Error Err = Error::success(); |
| 37 | auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err); |
| 38 | if (Err) |
| 39 | return std::move(Err); |
| 40 | |
| 41 | return std::move(ObjectFile); |
| 42 | } |
| 43 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 44 | #define VARINT7_MAX ((1<<7)-1) |
| 45 | #define VARINT7_MIN (-(1<<7)) |
| 46 | #define VARUINT7_MAX (1<<7) |
| 47 | #define VARUINT1_MAX (1) |
| 48 | |
| 49 | static uint8_t readUint8(const uint8_t *&Ptr) { return *Ptr++; } |
| 50 | |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 51 | static uint32_t readUint32(const uint8_t *&Ptr) { |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 52 | uint32_t Result = support::endian::read32le(Ptr); |
| 53 | Ptr += sizeof(Result); |
| 54 | return Result; |
| 55 | } |
| 56 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 57 | static int32_t readFloat32(const uint8_t *&Ptr) { |
| 58 | int32_t Result = 0; |
| 59 | memcpy(&Result, Ptr, sizeof(Result)); |
| 60 | Ptr += sizeof(Result); |
| 61 | return Result; |
| 62 | } |
| 63 | |
| 64 | static int64_t readFloat64(const uint8_t *&Ptr) { |
| 65 | int64_t Result = 0; |
| 66 | memcpy(&Result, Ptr, sizeof(Result)); |
| 67 | Ptr += sizeof(Result); |
| 68 | return Result; |
| 69 | } |
| 70 | |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 71 | static uint64_t readULEB128(const uint8_t *&Ptr) { |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 72 | unsigned Count; |
| 73 | uint64_t Result = decodeULEB128(Ptr, &Count); |
| 74 | Ptr += Count; |
| 75 | return Result; |
| 76 | } |
| 77 | |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 78 | static StringRef readString(const uint8_t *&Ptr) { |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 79 | uint32_t StringLen = readULEB128(Ptr); |
| 80 | StringRef Return = StringRef(reinterpret_cast<const char *>(Ptr), StringLen); |
| 81 | Ptr += StringLen; |
| 82 | return Return; |
| 83 | } |
| 84 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 85 | static int64_t readLEB128(const uint8_t *&Ptr) { |
| 86 | unsigned Count; |
| 87 | uint64_t Result = decodeSLEB128(Ptr, &Count); |
| 88 | Ptr += Count; |
| 89 | return Result; |
| 90 | } |
| 91 | |
| 92 | static uint8_t readVaruint1(const uint8_t *&Ptr) { |
| 93 | int64_t result = readLEB128(Ptr); |
| 94 | assert(result <= VARUINT1_MAX && result >= 0); |
| 95 | return result; |
| 96 | } |
| 97 | |
| 98 | static int8_t readVarint7(const uint8_t *&Ptr) { |
| 99 | int64_t result = readLEB128(Ptr); |
| 100 | assert(result <= VARINT7_MAX && result >= VARINT7_MIN); |
| 101 | return result; |
| 102 | } |
| 103 | |
| 104 | static uint8_t readVaruint7(const uint8_t *&Ptr) { |
| 105 | uint64_t result = readULEB128(Ptr); |
Davide Italiano | 5437602 | 2017-04-01 19:37:15 +0000 | [diff] [blame] | 106 | assert(result <= VARUINT7_MAX); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 107 | return result; |
| 108 | } |
| 109 | |
| 110 | static int32_t readVarint32(const uint8_t *&Ptr) { |
| 111 | int64_t result = readLEB128(Ptr); |
| 112 | assert(result <= INT32_MAX && result >= INT32_MIN); |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | static uint32_t readVaruint32(const uint8_t *&Ptr) { |
| 117 | uint64_t result = readULEB128(Ptr); |
Davide Italiano | 5437602 | 2017-04-01 19:37:15 +0000 | [diff] [blame] | 118 | assert(result <= UINT32_MAX); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 119 | return result; |
| 120 | } |
| 121 | |
| 122 | static int64_t readVarint64(const uint8_t *&Ptr) { |
| 123 | return readLEB128(Ptr); |
| 124 | } |
| 125 | |
| 126 | static uint8_t readOpcode(const uint8_t *&Ptr) { |
| 127 | return readUint8(Ptr); |
| 128 | } |
| 129 | |
| 130 | static Error readInitExpr(wasm::WasmInitExpr &Expr, const uint8_t *&Ptr) { |
| 131 | Expr.Opcode = readOpcode(Ptr); |
| 132 | |
| 133 | switch (Expr.Opcode) { |
| 134 | case wasm::WASM_OPCODE_I32_CONST: |
| 135 | Expr.Value.Int32 = readVarint32(Ptr); |
| 136 | break; |
| 137 | case wasm::WASM_OPCODE_I64_CONST: |
| 138 | Expr.Value.Int64 = readVarint64(Ptr); |
| 139 | break; |
| 140 | case wasm::WASM_OPCODE_F32_CONST: |
| 141 | Expr.Value.Float32 = readFloat32(Ptr); |
| 142 | break; |
| 143 | case wasm::WASM_OPCODE_F64_CONST: |
| 144 | Expr.Value.Float64 = readFloat64(Ptr); |
| 145 | break; |
| 146 | case wasm::WASM_OPCODE_GET_GLOBAL: |
Sam Clegg | 7fb391f | 2017-04-25 17:11:56 +0000 | [diff] [blame] | 147 | Expr.Value.Global = readULEB128(Ptr); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 148 | break; |
| 149 | default: |
| 150 | return make_error<GenericBinaryError>("Invalid opcode in init_expr", |
| 151 | object_error::parse_failed); |
| 152 | } |
| 153 | |
| 154 | uint8_t EndOpcode = readOpcode(Ptr); |
| 155 | if (EndOpcode != wasm::WASM_OPCODE_END) { |
| 156 | return make_error<GenericBinaryError>("Invalid init_expr", |
| 157 | object_error::parse_failed); |
| 158 | } |
| 159 | return Error::success(); |
| 160 | } |
| 161 | |
| 162 | static wasm::WasmLimits readLimits(const uint8_t *&Ptr) { |
| 163 | wasm::WasmLimits Result; |
| 164 | Result.Flags = readVaruint1(Ptr); |
| 165 | Result.Initial = readVaruint32(Ptr); |
| 166 | if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) |
| 167 | Result.Maximum = readVaruint32(Ptr); |
| 168 | return Result; |
| 169 | } |
| 170 | |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 171 | static wasm::WasmTable readTable(const uint8_t *&Ptr) { |
| 172 | wasm::WasmTable Table; |
| 173 | Table.ElemType = readVarint7(Ptr); |
| 174 | Table.Limits = readLimits(Ptr); |
| 175 | return Table; |
| 176 | } |
| 177 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 178 | static Error readSection(WasmSection &Section, const uint8_t *&Ptr, |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 179 | const uint8_t *Start) { |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 180 | // TODO(sbc): Avoid reading past EOF in the case of malformed files. |
| 181 | Section.Offset = Ptr - Start; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 182 | Section.Type = readVaruint7(Ptr); |
| 183 | uint32_t Size = readVaruint32(Ptr); |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 184 | if (Size == 0) |
| 185 | return make_error<StringError>("Zero length section", |
| 186 | object_error::parse_failed); |
| 187 | Section.Content = ArrayRef<uint8_t>(Ptr, Size); |
| 188 | Ptr += Size; |
| 189 | return Error::success(); |
| 190 | } |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 191 | |
| 192 | WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err) |
Eugene Zelenko | 9f5094d | 2017-04-21 22:03:05 +0000 | [diff] [blame] | 193 | : ObjectFile(Binary::ID_Wasm, Buffer) { |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 194 | ErrorAsOutParameter ErrAsOutParam(&Err); |
| 195 | Header.Magic = getData().substr(0, 4); |
| 196 | if (Header.Magic != StringRef("\0asm", 4)) { |
| 197 | Err = make_error<StringError>("Bad magic number", |
| 198 | object_error::parse_failed); |
| 199 | return; |
| 200 | } |
| 201 | const uint8_t *Ptr = getPtr(4); |
| 202 | Header.Version = readUint32(Ptr); |
| 203 | if (Header.Version != wasm::WasmVersion) { |
| 204 | Err = make_error<StringError>("Bad version number", |
| 205 | object_error::parse_failed); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | const uint8_t *Eof = getPtr(getData().size()); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 210 | WasmSection Sec; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 211 | while (Ptr < Eof) { |
| 212 | if ((Err = readSection(Sec, Ptr, getPtr(0)))) |
| 213 | return; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 214 | if ((Err = parseSection(Sec))) |
| 215 | return; |
| 216 | |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 217 | Sections.push_back(Sec); |
| 218 | } |
| 219 | } |
| 220 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 221 | Error WasmObjectFile::parseSection(WasmSection &Sec) { |
| 222 | const uint8_t* Start = Sec.Content.data(); |
| 223 | const uint8_t* End = Start + Sec.Content.size(); |
| 224 | switch (Sec.Type) { |
| 225 | case wasm::WASM_SEC_CUSTOM: |
| 226 | return parseCustomSection(Sec, Start, End); |
| 227 | case wasm::WASM_SEC_TYPE: |
| 228 | return parseTypeSection(Start, End); |
| 229 | case wasm::WASM_SEC_IMPORT: |
| 230 | return parseImportSection(Start, End); |
| 231 | case wasm::WASM_SEC_FUNCTION: |
| 232 | return parseFunctionSection(Start, End); |
| 233 | case wasm::WASM_SEC_TABLE: |
| 234 | return parseTableSection(Start, End); |
| 235 | case wasm::WASM_SEC_MEMORY: |
| 236 | return parseMemorySection(Start, End); |
| 237 | case wasm::WASM_SEC_GLOBAL: |
| 238 | return parseGlobalSection(Start, End); |
| 239 | case wasm::WASM_SEC_EXPORT: |
| 240 | return parseExportSection(Start, End); |
| 241 | case wasm::WASM_SEC_START: |
| 242 | return parseStartSection(Start, End); |
| 243 | case wasm::WASM_SEC_ELEM: |
| 244 | return parseElemSection(Start, End); |
| 245 | case wasm::WASM_SEC_CODE: |
| 246 | return parseCodeSection(Start, End); |
| 247 | case wasm::WASM_SEC_DATA: |
| 248 | return parseDataSection(Start, End); |
| 249 | default: |
| 250 | return make_error<GenericBinaryError>("Bad section type", |
| 251 | object_error::parse_failed); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | Error WasmObjectFile::parseNameSection(const uint8_t *Ptr, const uint8_t *End) { |
| 256 | while (Ptr < End) { |
| 257 | uint8_t Type = readVarint7(Ptr); |
| 258 | uint32_t Size = readVaruint32(Ptr); |
| 259 | switch (Type) { |
| 260 | case wasm::WASM_NAMES_FUNCTION: { |
| 261 | uint32_t Count = readVaruint32(Ptr); |
| 262 | while (Count--) { |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 263 | uint32_t Index = readVaruint32(Ptr); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 264 | StringRef Name = readString(Ptr); |
Eugene Zelenko | 9f5094d | 2017-04-21 22:03:05 +0000 | [diff] [blame] | 265 | if (!Name.empty()) |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 266 | Symbols.emplace_back(Name, |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 267 | WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME, |
| 268 | Sections.size(), Index); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 269 | } |
| 270 | break; |
| 271 | } |
| 272 | // Ignore local names for now |
| 273 | case wasm::WASM_NAMES_LOCAL: |
| 274 | default: |
| 275 | Ptr += Size; |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | if (Ptr != End) |
| 281 | return make_error<GenericBinaryError>("Name section ended prematurely", |
| 282 | object_error::parse_failed); |
| 283 | return Error::success(); |
| 284 | } |
| 285 | |
| 286 | WasmSection* WasmObjectFile::findCustomSectionByName(StringRef Name) { |
| 287 | for (WasmSection& Section : Sections) { |
| 288 | if (Section.Type == wasm::WASM_SEC_CUSTOM && Section.Name == Name) |
| 289 | return &Section; |
| 290 | } |
| 291 | return nullptr; |
| 292 | } |
| 293 | |
| 294 | WasmSection* WasmObjectFile::findSectionByType(uint32_t Type) { |
| 295 | assert(Type != wasm::WASM_SEC_CUSTOM); |
| 296 | for (WasmSection& Section : Sections) { |
| 297 | if (Section.Type == Type) |
| 298 | return &Section; |
| 299 | } |
| 300 | return nullptr; |
| 301 | } |
| 302 | |
| 303 | Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr, |
| 304 | const uint8_t *End) { |
| 305 | uint8_t SectionCode = readVarint7(Ptr); |
| 306 | WasmSection* Section = nullptr; |
| 307 | if (SectionCode == wasm::WASM_SEC_CUSTOM) { |
| 308 | StringRef Name = readString(Ptr); |
| 309 | Section = findCustomSectionByName(Name); |
| 310 | } else { |
| 311 | Section = findSectionByType(SectionCode); |
| 312 | } |
| 313 | if (!Section) |
| 314 | return make_error<GenericBinaryError>("Invalid section code", |
| 315 | object_error::parse_failed); |
| 316 | uint32_t RelocCount = readVaruint32(Ptr); |
| 317 | while (RelocCount--) { |
| 318 | wasm::WasmRelocation Reloc; |
| 319 | memset(&Reloc, 0, sizeof(Reloc)); |
| 320 | Reloc.Type = readVaruint32(Ptr); |
| 321 | Reloc.Offset = readVaruint32(Ptr); |
| 322 | Reloc.Index = readVaruint32(Ptr); |
| 323 | switch (Reloc.Type) { |
| 324 | case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB: |
| 325 | case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB: |
| 326 | case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: |
Sam Clegg | cc182aa | 2017-04-26 00:02:31 +0000 | [diff] [blame] | 327 | case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB: |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame^] | 328 | case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 329 | break; |
| 330 | case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB: |
| 331 | case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB: |
| 332 | case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32: |
Sam Clegg | cc182aa | 2017-04-26 00:02:31 +0000 | [diff] [blame] | 333 | Reloc.Addend = readVarint32(Ptr); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 334 | break; |
| 335 | default: |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame^] | 336 | return make_error<GenericBinaryError>("Bad relocation type: " + |
| 337 | Twine(Reloc.Type), |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 338 | object_error::parse_failed); |
| 339 | } |
| 340 | Section->Relocations.push_back(Reloc); |
| 341 | } |
| 342 | if (Ptr != End) |
| 343 | return make_error<GenericBinaryError>("Reloc section ended prematurely", |
| 344 | object_error::parse_failed); |
| 345 | return Error::success(); |
| 346 | } |
| 347 | |
| 348 | Error WasmObjectFile::parseCustomSection(WasmSection &Sec, |
| 349 | const uint8_t *Ptr, const uint8_t *End) { |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 350 | Sec.Name = readString(Ptr); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 351 | if (Sec.Name == "name") { |
| 352 | if (Error Err = parseNameSection(Ptr, End)) |
| 353 | return Err; |
| 354 | } else if (Sec.Name.startswith("reloc.")) { |
| 355 | if (Error Err = parseRelocSection(Sec.Name, Ptr, End)) |
| 356 | return Err; |
| 357 | } |
| 358 | return Error::success(); |
| 359 | } |
| 360 | |
| 361 | Error WasmObjectFile::parseTypeSection(const uint8_t *Ptr, const uint8_t *End) { |
| 362 | uint32_t Count = readVaruint32(Ptr); |
| 363 | Signatures.reserve(Count); |
| 364 | while (Count--) { |
| 365 | wasm::WasmSignature Sig; |
| 366 | Sig.ReturnType = wasm::WASM_TYPE_NORESULT; |
| 367 | int8_t Form = readVarint7(Ptr); |
| 368 | if (Form != wasm::WASM_TYPE_FUNC) { |
| 369 | return make_error<GenericBinaryError>("Invalid signature type", |
| 370 | object_error::parse_failed); |
| 371 | } |
| 372 | uint32_t ParamCount = readVaruint32(Ptr); |
| 373 | Sig.ParamTypes.reserve(ParamCount); |
| 374 | while (ParamCount--) { |
| 375 | uint32_t ParamType = readVarint7(Ptr); |
| 376 | Sig.ParamTypes.push_back(ParamType); |
| 377 | } |
| 378 | uint32_t ReturnCount = readVaruint32(Ptr); |
| 379 | if (ReturnCount) { |
| 380 | if (ReturnCount != 1) { |
| 381 | return make_error<GenericBinaryError>( |
| 382 | "Multiple return types not supported", object_error::parse_failed); |
| 383 | } |
| 384 | Sig.ReturnType = readVarint7(Ptr); |
| 385 | } |
| 386 | Signatures.push_back(Sig); |
| 387 | } |
| 388 | if (Ptr != End) |
| 389 | return make_error<GenericBinaryError>("Type section ended prematurely", |
| 390 | object_error::parse_failed); |
| 391 | return Error::success(); |
| 392 | } |
| 393 | |
| 394 | Error WasmObjectFile::parseImportSection(const uint8_t *Ptr, const uint8_t *End) { |
| 395 | uint32_t Count = readVaruint32(Ptr); |
| 396 | Imports.reserve(Count); |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 397 | for (uint32_t i = 0; i < Count; i++) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 398 | wasm::WasmImport Im; |
| 399 | Im.Module = readString(Ptr); |
| 400 | Im.Field = readString(Ptr); |
| 401 | Im.Kind = readUint8(Ptr); |
| 402 | switch (Im.Kind) { |
| 403 | case wasm::WASM_EXTERNAL_FUNCTION: |
| 404 | Im.SigIndex = readVaruint32(Ptr); |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 405 | Symbols.emplace_back(Im.Field, WasmSymbol::SymbolType::FUNCTION_IMPORT, |
| 406 | Sections.size(), i); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 407 | break; |
| 408 | case wasm::WASM_EXTERNAL_GLOBAL: |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 409 | Im.Global.Type = readVarint7(Ptr); |
| 410 | Im.Global.Mutable = readVaruint1(Ptr); |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 411 | Symbols.emplace_back(Im.Field, WasmSymbol::SymbolType::GLOBAL_IMPORT, |
| 412 | Sections.size(), i); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 413 | break; |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 414 | case wasm::WASM_EXTERNAL_MEMORY: |
| 415 | Im.Memory = readLimits(Ptr); |
| 416 | break; |
| 417 | case wasm::WASM_EXTERNAL_TABLE: |
| 418 | Im.Table = readTable(Ptr); |
| 419 | if (Im.Table.ElemType != wasm::WASM_TYPE_ANYFUNC) { |
| 420 | return make_error<GenericBinaryError>("Invalid table element type", |
| 421 | object_error::parse_failed); |
| 422 | } |
| 423 | break; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 424 | default: |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 425 | return make_error<GenericBinaryError>( |
| 426 | "Unexpected import kind", object_error::parse_failed); |
| 427 | } |
| 428 | Imports.push_back(Im); |
| 429 | } |
| 430 | if (Ptr != End) |
| 431 | return make_error<GenericBinaryError>("Import section ended prematurely", |
| 432 | object_error::parse_failed); |
| 433 | return Error::success(); |
| 434 | } |
| 435 | |
| 436 | Error WasmObjectFile::parseFunctionSection(const uint8_t *Ptr, const uint8_t *End) { |
| 437 | uint32_t Count = readVaruint32(Ptr); |
| 438 | FunctionTypes.reserve(Count); |
| 439 | while (Count--) { |
| 440 | FunctionTypes.push_back(readVaruint32(Ptr)); |
| 441 | } |
| 442 | if (Ptr != End) |
| 443 | return make_error<GenericBinaryError>("Function section ended prematurely", |
| 444 | object_error::parse_failed); |
| 445 | return Error::success(); |
| 446 | } |
| 447 | |
| 448 | Error WasmObjectFile::parseTableSection(const uint8_t *Ptr, const uint8_t *End) { |
| 449 | uint32_t Count = readVaruint32(Ptr); |
| 450 | Tables.reserve(Count); |
| 451 | while (Count--) { |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 452 | Tables.push_back(readTable(Ptr)); |
| 453 | if (Tables.back().ElemType != wasm::WASM_TYPE_ANYFUNC) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 454 | return make_error<GenericBinaryError>("Invalid table element type", |
| 455 | object_error::parse_failed); |
| 456 | } |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 457 | } |
| 458 | if (Ptr != End) |
| 459 | return make_error<GenericBinaryError>("Table section ended prematurely", |
| 460 | object_error::parse_failed); |
| 461 | return Error::success(); |
| 462 | } |
| 463 | |
| 464 | Error WasmObjectFile::parseMemorySection(const uint8_t *Ptr, const uint8_t *End) { |
| 465 | uint32_t Count = readVaruint32(Ptr); |
| 466 | Memories.reserve(Count); |
| 467 | while (Count--) { |
| 468 | Memories.push_back(readLimits(Ptr)); |
| 469 | } |
| 470 | if (Ptr != End) |
| 471 | return make_error<GenericBinaryError>("Memory section ended prematurely", |
| 472 | object_error::parse_failed); |
| 473 | return Error::success(); |
| 474 | } |
| 475 | |
| 476 | Error WasmObjectFile::parseGlobalSection(const uint8_t *Ptr, const uint8_t *End) { |
| 477 | uint32_t Count = readVaruint32(Ptr); |
| 478 | Globals.reserve(Count); |
| 479 | while (Count--) { |
| 480 | wasm::WasmGlobal Global; |
| 481 | Global.Type = readVarint7(Ptr); |
| 482 | Global.Mutable = readVaruint1(Ptr); |
Davide Italiano | deede83 | 2017-04-01 19:40:51 +0000 | [diff] [blame] | 483 | if (Error Err = readInitExpr(Global.InitExpr, Ptr)) |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 484 | return Err; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 485 | Globals.push_back(Global); |
| 486 | } |
| 487 | if (Ptr != End) |
| 488 | return make_error<GenericBinaryError>("Global section ended prematurely", |
| 489 | object_error::parse_failed); |
| 490 | return Error::success(); |
| 491 | } |
| 492 | |
| 493 | Error WasmObjectFile::parseExportSection(const uint8_t *Ptr, const uint8_t *End) { |
| 494 | uint32_t Count = readVaruint32(Ptr); |
| 495 | Exports.reserve(Count); |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 496 | for (uint32_t i = 0; i < Count; i++) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 497 | wasm::WasmExport Ex; |
| 498 | Ex.Name = readString(Ptr); |
| 499 | Ex.Kind = readUint8(Ptr); |
| 500 | Ex.Index = readVaruint32(Ptr); |
| 501 | Exports.push_back(Ex); |
| 502 | switch (Ex.Kind) { |
| 503 | case wasm::WASM_EXTERNAL_FUNCTION: |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 504 | Symbols.emplace_back(Ex.Name, WasmSymbol::SymbolType::FUNCTION_EXPORT, |
| 505 | Sections.size(), i); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 506 | break; |
| 507 | case wasm::WASM_EXTERNAL_GLOBAL: |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 508 | Symbols.emplace_back(Ex.Name, WasmSymbol::SymbolType::GLOBAL_EXPORT, |
| 509 | Sections.size(), i); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 510 | break; |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 511 | case wasm::WASM_EXTERNAL_MEMORY: |
| 512 | case wasm::WASM_EXTERNAL_TABLE: |
| 513 | break; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 514 | default: |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 515 | return make_error<GenericBinaryError>( |
| 516 | "Unexpected export kind", object_error::parse_failed); |
| 517 | } |
| 518 | } |
| 519 | if (Ptr != End) |
| 520 | return make_error<GenericBinaryError>("Export section ended prematurely", |
| 521 | object_error::parse_failed); |
| 522 | return Error::success(); |
| 523 | } |
| 524 | |
| 525 | Error WasmObjectFile::parseStartSection(const uint8_t *Ptr, const uint8_t *End) { |
| 526 | StartFunction = readVaruint32(Ptr); |
Sam Clegg | a0efcfe | 2017-05-09 17:51:38 +0000 | [diff] [blame] | 527 | if (StartFunction >= FunctionTypes.size()) |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 528 | return make_error<GenericBinaryError>("Invalid start function", |
| 529 | object_error::parse_failed); |
| 530 | return Error::success(); |
| 531 | } |
| 532 | |
| 533 | Error WasmObjectFile::parseCodeSection(const uint8_t *Ptr, const uint8_t *End) { |
| 534 | uint32_t FunctionCount = readVaruint32(Ptr); |
| 535 | if (FunctionCount != FunctionTypes.size()) { |
| 536 | return make_error<GenericBinaryError>("Invalid function count", |
| 537 | object_error::parse_failed); |
| 538 | } |
| 539 | |
| 540 | CodeSection = ArrayRef<uint8_t>(Ptr, End - Ptr); |
| 541 | |
| 542 | while (FunctionCount--) { |
| 543 | wasm::WasmFunction Function; |
| 544 | uint32_t FunctionSize = readVaruint32(Ptr); |
| 545 | const uint8_t *FunctionEnd = Ptr + FunctionSize; |
| 546 | |
| 547 | uint32_t NumLocalDecls = readVaruint32(Ptr); |
| 548 | Function.Locals.reserve(NumLocalDecls); |
| 549 | while (NumLocalDecls--) { |
| 550 | wasm::WasmLocalDecl Decl; |
| 551 | Decl.Count = readVaruint32(Ptr); |
| 552 | Decl.Type = readVarint7(Ptr); |
| 553 | Function.Locals.push_back(Decl); |
| 554 | } |
| 555 | |
| 556 | uint32_t BodySize = FunctionEnd - Ptr; |
| 557 | Function.Body = ArrayRef<uint8_t>(Ptr, BodySize); |
| 558 | Ptr += BodySize; |
| 559 | assert(Ptr == FunctionEnd); |
| 560 | Functions.push_back(Function); |
| 561 | } |
| 562 | if (Ptr != End) |
| 563 | return make_error<GenericBinaryError>("Code section ended prematurely", |
| 564 | object_error::parse_failed); |
| 565 | return Error::success(); |
| 566 | } |
| 567 | |
| 568 | Error WasmObjectFile::parseElemSection(const uint8_t *Ptr, const uint8_t *End) { |
| 569 | uint32_t Count = readVaruint32(Ptr); |
| 570 | ElemSegments.reserve(Count); |
| 571 | while (Count--) { |
| 572 | wasm::WasmElemSegment Segment; |
| 573 | Segment.TableIndex = readVaruint32(Ptr); |
| 574 | if (Segment.TableIndex != 0) { |
| 575 | return make_error<GenericBinaryError>("Invalid TableIndex", |
| 576 | object_error::parse_failed); |
| 577 | } |
| 578 | if (Error Err = readInitExpr(Segment.Offset, Ptr)) |
| 579 | return Err; |
| 580 | uint32_t NumElems = readVaruint32(Ptr); |
| 581 | while (NumElems--) { |
| 582 | Segment.Functions.push_back(readVaruint32(Ptr)); |
| 583 | } |
| 584 | ElemSegments.push_back(Segment); |
| 585 | } |
| 586 | if (Ptr != End) |
| 587 | return make_error<GenericBinaryError>("Elem section ended prematurely", |
| 588 | object_error::parse_failed); |
| 589 | return Error::success(); |
| 590 | } |
| 591 | |
| 592 | Error WasmObjectFile::parseDataSection(const uint8_t *Ptr, const uint8_t *End) { |
| 593 | uint32_t Count = readVaruint32(Ptr); |
| 594 | DataSegments.reserve(Count); |
| 595 | while (Count--) { |
| 596 | wasm::WasmDataSegment Segment; |
| 597 | Segment.Index = readVaruint32(Ptr); |
| 598 | if (Error Err = readInitExpr(Segment.Offset, Ptr)) |
| 599 | return Err; |
| 600 | uint32_t Size = readVaruint32(Ptr); |
| 601 | Segment.Content = ArrayRef<uint8_t>(Ptr, Size); |
| 602 | Ptr += Size; |
| 603 | DataSegments.push_back(Segment); |
| 604 | } |
| 605 | if (Ptr != End) |
| 606 | return make_error<GenericBinaryError>("Data section ended prematurely", |
| 607 | object_error::parse_failed); |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 608 | return Error::success(); |
| 609 | } |
| 610 | |
| 611 | const uint8_t *WasmObjectFile::getPtr(size_t Offset) const { |
| 612 | return reinterpret_cast<const uint8_t *>(getData().substr(Offset, 1).data()); |
| 613 | } |
| 614 | |
| 615 | const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const { |
| 616 | return Header; |
| 617 | } |
| 618 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 619 | void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.a++; } |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 620 | |
| 621 | uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const { |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 622 | uint32_t Result = SymbolRef::SF_None; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 623 | const WasmSymbol &Sym = getWasmSymbol(Symb); |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 624 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 625 | switch (Sym.Type) { |
| 626 | case WasmSymbol::SymbolType::FUNCTION_IMPORT: |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 627 | Result |= SymbolRef::SF_Undefined | SymbolRef::SF_Executable; |
| 628 | break; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 629 | case WasmSymbol::SymbolType::FUNCTION_EXPORT: |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 630 | Result |= SymbolRef::SF_Global | SymbolRef::SF_Executable; |
| 631 | break; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 632 | case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME: |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 633 | Result |= SymbolRef::SF_Executable; |
| 634 | break; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 635 | case WasmSymbol::SymbolType::GLOBAL_IMPORT: |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 636 | Result |= SymbolRef::SF_Undefined; |
| 637 | break; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 638 | case WasmSymbol::SymbolType::GLOBAL_EXPORT: |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 639 | Result |= SymbolRef::SF_Global; |
| 640 | break; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 641 | } |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 642 | |
| 643 | return Result; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | basic_symbol_iterator WasmObjectFile::symbol_begin() const { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 647 | DataRefImpl Ref; |
| 648 | Ref.d.a = 0; |
| 649 | return BasicSymbolRef(Ref, this); |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | basic_symbol_iterator WasmObjectFile::symbol_end() const { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 653 | DataRefImpl Ref; |
| 654 | Ref.d.a = Symbols.size(); |
| 655 | return BasicSymbolRef(Ref, this); |
| 656 | } |
| 657 | |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 658 | const WasmSymbol &WasmObjectFile::getWasmSymbol(const DataRefImpl &Symb) const { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 659 | return Symbols[Symb.d.a]; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 662 | const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const { |
| 663 | return getWasmSymbol(Symb.getRawDataRefImpl()); |
| 664 | } |
| 665 | |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 666 | Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 667 | const WasmSymbol &Sym = getWasmSymbol(Symb); |
| 668 | return Sym.Name; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const { |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 672 | return getSymbolValue(Symb); |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const { |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 676 | const WasmSymbol &Sym = getWasmSymbol(Symb); |
| 677 | return Sym.ElementIndex; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const { |
| 681 | llvm_unreachable("not yet implemented"); |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const { |
| 686 | llvm_unreachable("not yet implemented"); |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | Expected<SymbolRef::Type> |
| 691 | WasmObjectFile::getSymbolType(DataRefImpl Symb) const { |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 692 | const WasmSymbol &Sym = getWasmSymbol(Symb); |
| 693 | |
| 694 | switch (Sym.Type) { |
| 695 | case WasmSymbol::SymbolType::FUNCTION_IMPORT: |
| 696 | case WasmSymbol::SymbolType::FUNCTION_EXPORT: |
| 697 | case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME: |
| 698 | return SymbolRef::ST_Function; |
| 699 | case WasmSymbol::SymbolType::GLOBAL_IMPORT: |
| 700 | case WasmSymbol::SymbolType::GLOBAL_EXPORT: |
| 701 | return SymbolRef::ST_Data; |
| 702 | } |
| 703 | |
| 704 | llvm_unreachable("Unknown WasmSymbol::SymbolType"); |
| 705 | return SymbolRef::ST_Other; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | Expected<section_iterator> |
| 709 | WasmObjectFile::getSymbolSection(DataRefImpl Symb) const { |
Sam Clegg | fc5b5cd | 2017-05-04 19:32:43 +0000 | [diff] [blame] | 710 | DataRefImpl Ref; |
| 711 | Ref.d.a = getWasmSymbol(Symb).Section; |
| 712 | return section_iterator(SectionRef(Ref, this)); |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; } |
| 716 | |
| 717 | std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec, |
| 718 | StringRef &Res) const { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 719 | const WasmSection &S = Sections[Sec.d.a]; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 720 | #define ECase(X) \ |
| 721 | case wasm::WASM_SEC_##X: \ |
| 722 | Res = #X; \ |
| 723 | break |
| 724 | switch (S.Type) { |
| 725 | ECase(TYPE); |
| 726 | ECase(IMPORT); |
| 727 | ECase(FUNCTION); |
| 728 | ECase(TABLE); |
| 729 | ECase(MEMORY); |
| 730 | ECase(GLOBAL); |
| 731 | ECase(EXPORT); |
| 732 | ECase(START); |
| 733 | ECase(ELEM); |
| 734 | ECase(CODE); |
| 735 | ECase(DATA); |
Derek Schuff | 6d76b7b | 2017-01-30 23:30:52 +0000 | [diff] [blame] | 736 | case wasm::WASM_SEC_CUSTOM: |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 737 | Res = S.Name; |
| 738 | break; |
| 739 | default: |
| 740 | return object_error::invalid_section_index; |
| 741 | } |
| 742 | #undef ECase |
| 743 | return std::error_code(); |
| 744 | } |
| 745 | |
| 746 | uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; } |
| 747 | |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 748 | uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const { |
| 749 | return Sec.d.a; |
| 750 | } |
| 751 | |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 752 | uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 753 | const WasmSection &S = Sections[Sec.d.a]; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 754 | return S.Content.size(); |
| 755 | } |
| 756 | |
| 757 | std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec, |
| 758 | StringRef &Res) const { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 759 | const WasmSection &S = Sections[Sec.d.a]; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 760 | // This will never fail since wasm sections can never be empty (user-sections |
| 761 | // must have a name and non-user sections each have a defined structure). |
| 762 | Res = StringRef(reinterpret_cast<const char *>(S.Content.data()), |
| 763 | S.Content.size()); |
| 764 | return std::error_code(); |
| 765 | } |
| 766 | |
| 767 | uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const { |
| 768 | return 1; |
| 769 | } |
| 770 | |
| 771 | bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const { |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | bool WasmObjectFile::isSectionText(DataRefImpl Sec) const { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 776 | return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | bool WasmObjectFile::isSectionData(DataRefImpl Sec) const { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 780 | return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; } |
| 784 | |
| 785 | bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; } |
| 786 | |
| 787 | bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; } |
| 788 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 789 | relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const { |
| 790 | DataRefImpl RelocRef; |
| 791 | RelocRef.d.a = Ref.d.a; |
| 792 | RelocRef.d.b = 0; |
| 793 | return relocation_iterator(RelocationRef(RelocRef, this)); |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 796 | relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const { |
| 797 | const WasmSection &Sec = getWasmSection(Ref); |
| 798 | DataRefImpl RelocRef; |
| 799 | RelocRef.d.a = Ref.d.a; |
| 800 | RelocRef.d.b = Sec.Relocations.size(); |
| 801 | return relocation_iterator(RelocationRef(RelocRef, this)); |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 805 | Rel.d.b++; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 808 | uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const { |
| 809 | const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); |
| 810 | return Rel.Offset; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Rel) const { |
| 814 | llvm_unreachable("not yet implemented"); |
| 815 | SymbolRef Ref; |
| 816 | return symbol_iterator(Ref); |
| 817 | } |
| 818 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 819 | uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const { |
| 820 | const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); |
| 821 | return Rel.Type; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | void WasmObjectFile::getRelocationTypeName( |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 825 | DataRefImpl Ref, SmallVectorImpl<char> &Result) const { |
| 826 | const wasm::WasmRelocation& Rel = getWasmRelocation(Ref); |
| 827 | StringRef Res = "Unknown"; |
| 828 | |
| 829 | #define WASM_RELOC(name, value) \ |
| 830 | case wasm::name: \ |
| 831 | Res = #name; \ |
| 832 | break; |
| 833 | |
| 834 | switch (Rel.Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 835 | #include "llvm/BinaryFormat/WasmRelocs/WebAssembly.def" |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | #undef WASM_RELOC |
| 839 | |
| 840 | Result.append(Res.begin(), Res.end()); |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | section_iterator WasmObjectFile::section_begin() const { |
| 844 | DataRefImpl Ref; |
| 845 | Ref.d.a = 0; |
| 846 | return section_iterator(SectionRef(Ref, this)); |
| 847 | } |
| 848 | |
| 849 | section_iterator WasmObjectFile::section_end() const { |
| 850 | DataRefImpl Ref; |
| 851 | Ref.d.a = Sections.size(); |
| 852 | return section_iterator(SectionRef(Ref, this)); |
| 853 | } |
| 854 | |
| 855 | uint8_t WasmObjectFile::getBytesInAddress() const { return 4; } |
| 856 | |
| 857 | StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; } |
| 858 | |
| 859 | unsigned WasmObjectFile::getArch() const { return Triple::wasm32; } |
| 860 | |
| 861 | SubtargetFeatures WasmObjectFile::getFeatures() const { |
| 862 | return SubtargetFeatures(); |
| 863 | } |
| 864 | |
| 865 | bool WasmObjectFile::isRelocatableObject() const { return false; } |
| 866 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 867 | const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const { |
Davide Italiano | 16fe582 | 2017-04-01 19:47:52 +0000 | [diff] [blame] | 868 | assert(Ref.d.a < Sections.size()); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 869 | return Sections[Ref.d.a]; |
| 870 | } |
| 871 | |
| 872 | const WasmSection & |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 873 | WasmObjectFile::getWasmSection(const SectionRef &Section) const { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 874 | return getWasmSection(Section.getRawDataRefImpl()); |
| 875 | } |
| 876 | |
| 877 | const wasm::WasmRelocation & |
| 878 | WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const { |
| 879 | return getWasmRelocation(Ref.getRawDataRefImpl()); |
| 880 | } |
| 881 | |
| 882 | const wasm::WasmRelocation & |
| 883 | WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const { |
Davide Italiano | 16fe582 | 2017-04-01 19:47:52 +0000 | [diff] [blame] | 884 | assert(Ref.d.a < Sections.size()); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 885 | const WasmSection& Sec = Sections[Ref.d.a]; |
Davide Italiano | 16fe582 | 2017-04-01 19:47:52 +0000 | [diff] [blame] | 886 | assert(Ref.d.b < Sec.Relocations.size()); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 887 | return Sec.Relocations[Ref.d.b]; |
Derek Schuff | 2c6f75d | 2016-11-30 16:49:11 +0000 | [diff] [blame] | 888 | } |