1 //===- WasmObjectFile.cpp - Wasm object file implementation ---------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/ArrayRef.h" 10 #include "llvm/ADT/DenseSet.h" 11 #include "llvm/ADT/SmallSet.h" 12 #include "llvm/ADT/StringRef.h" 13 #include "llvm/ADT/StringSet.h" 14 #include "llvm/ADT/StringSwitch.h" 15 #include "llvm/BinaryFormat/Wasm.h" 16 #include "llvm/Object/Binary.h" 17 #include "llvm/Object/Error.h" 18 #include "llvm/Object/ObjectFile.h" 19 #include "llvm/Object/SymbolicFile.h" 20 #include "llvm/Object/Wasm.h" 21 #include "llvm/Support/Endian.h" 22 #include "llvm/Support/Error.h" 23 #include "llvm/Support/ErrorHandling.h" 24 #include "llvm/Support/LEB128.h" 25 #include "llvm/Support/ScopedPrinter.h" 26 #include "llvm/TargetParser/SubtargetFeature.h" 27 #include "llvm/TargetParser/Triple.h" 28 #include <cassert> 29 #include <cstdint> 30 #include <cstring> 31 32 #define DEBUG_TYPE "wasm-object" 33 34 using namespace llvm; 35 using namespace object; 36 37 void WasmSymbol::print(raw_ostream &Out) const { 38 Out << "Name=" << Info.Name 39 << ", Kind=" << toString(wasm::WasmSymbolType(Info.Kind)) << ", Flags=0x" 40 << Twine::utohexstr(Info.Flags) << " ["; 41 switch (getBinding()) { 42 case wasm::WASM_SYMBOL_BINDING_GLOBAL: Out << "global"; break; 43 case wasm::WASM_SYMBOL_BINDING_LOCAL: Out << "local"; break; 44 case wasm::WASM_SYMBOL_BINDING_WEAK: Out << "weak"; break; 45 } 46 if (isHidden()) { 47 Out << ", hidden"; 48 } else { 49 Out << ", default"; 50 } 51 Out << "]"; 52 if (!isTypeData()) { 53 Out << ", ElemIndex=" << Info.ElementIndex; 54 } else if (isDefined()) { 55 Out << ", Segment=" << Info.DataRef.Segment; 56 Out << ", Offset=" << Info.DataRef.Offset; 57 Out << ", Size=" << Info.DataRef.Size; 58 } 59 } 60 61 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 62 LLVM_DUMP_METHOD void WasmSymbol::dump() const { print(dbgs()); } 63 #endif 64 65 Expected<std::unique_ptr<WasmObjectFile>> 66 ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) { 67 Error Err = Error::success(); 68 auto ObjectFile = std::make_unique<WasmObjectFile>(Buffer, Err); 69 if (Err) 70 return std::move(Err); 71 72 return std::move(ObjectFile); 73 } 74 75 #define VARINT7_MAX ((1 << 7) - 1) 76 #define VARINT7_MIN (-(1 << 7)) 77 #define VARUINT7_MAX (1 << 7) 78 #define VARUINT1_MAX (1) 79 80 static uint8_t readUint8(WasmObjectFile::ReadContext &Ctx) { 81 if (Ctx.Ptr == Ctx.End) 82 report_fatal_error("EOF while reading uint8"); 83 return *Ctx.Ptr++; 84 } 85 86 static uint32_t readUint32(WasmObjectFile::ReadContext &Ctx) { 87 if (Ctx.Ptr + 4 > Ctx.End) 88 report_fatal_error("EOF while reading uint32"); 89 uint32_t Result = support::endian::read32le(Ctx.Ptr); 90 Ctx.Ptr += 4; 91 return Result; 92 } 93 94 static int32_t readFloat32(WasmObjectFile::ReadContext &Ctx) { 95 if (Ctx.Ptr + 4 > Ctx.End) 96 report_fatal_error("EOF while reading float64"); 97 int32_t Result = 0; 98 memcpy(&Result, Ctx.Ptr, sizeof(Result)); 99 Ctx.Ptr += sizeof(Result); 100 return Result; 101 } 102 103 static int64_t readFloat64(WasmObjectFile::ReadContext &Ctx) { 104 if (Ctx.Ptr + 8 > Ctx.End) 105 report_fatal_error("EOF while reading float64"); 106 int64_t Result = 0; 107 memcpy(&Result, Ctx.Ptr, sizeof(Result)); 108 Ctx.Ptr += sizeof(Result); 109 return Result; 110 } 111 112 static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx) { 113 unsigned Count; 114 const char *Error = nullptr; 115 uint64_t Result = decodeULEB128(Ctx.Ptr, &Count, Ctx.End, &Error); 116 if (Error) 117 report_fatal_error(Error); 118 Ctx.Ptr += Count; 119 return Result; 120 } 121 122 static StringRef readString(WasmObjectFile::ReadContext &Ctx) { 123 uint32_t StringLen = readULEB128(Ctx); 124 if (Ctx.Ptr + StringLen > Ctx.End) 125 report_fatal_error("EOF while reading string"); 126 StringRef Return = 127 StringRef(reinterpret_cast<const char *>(Ctx.Ptr), StringLen); 128 Ctx.Ptr += StringLen; 129 return Return; 130 } 131 132 static int64_t readLEB128(WasmObjectFile::ReadContext &Ctx) { 133 unsigned Count; 134 const char *Error = nullptr; 135 uint64_t Result = decodeSLEB128(Ctx.Ptr, &Count, Ctx.End, &Error); 136 if (Error) 137 report_fatal_error(Error); 138 Ctx.Ptr += Count; 139 return Result; 140 } 141 142 static uint8_t readVaruint1(WasmObjectFile::ReadContext &Ctx) { 143 int64_t Result = readLEB128(Ctx); 144 if (Result > VARUINT1_MAX || Result < 0) 145 report_fatal_error("LEB is outside Varuint1 range"); 146 return Result; 147 } 148 149 static int32_t readVarint32(WasmObjectFile::ReadContext &Ctx) { 150 int64_t Result = readLEB128(Ctx); 151 if (Result > INT32_MAX || Result < INT32_MIN) 152 report_fatal_error("LEB is outside Varint32 range"); 153 return Result; 154 } 155 156 static uint32_t readVaruint32(WasmObjectFile::ReadContext &Ctx) { 157 uint64_t Result = readULEB128(Ctx); 158 if (Result > UINT32_MAX) 159 report_fatal_error("LEB is outside Varuint32 range"); 160 return Result; 161 } 162 163 static int64_t readVarint64(WasmObjectFile::ReadContext &Ctx) { 164 return readLEB128(Ctx); 165 } 166 167 static uint64_t readVaruint64(WasmObjectFile::ReadContext &Ctx) { 168 return readULEB128(Ctx); 169 } 170 171 static uint8_t readOpcode(WasmObjectFile::ReadContext &Ctx) { 172 return readUint8(Ctx); 173 } 174 175 static wasm::ValType parseValType(WasmObjectFile::ReadContext &Ctx, 176 uint32_t Code) { 177 // only directly encoded FUNCREF/EXTERNREF/EXNREF are supported 178 // (not ref null func, ref null extern, or ref null exn) 179 switch (Code) { 180 case wasm::WASM_TYPE_I32: 181 case wasm::WASM_TYPE_I64: 182 case wasm::WASM_TYPE_F32: 183 case wasm::WASM_TYPE_F64: 184 case wasm::WASM_TYPE_V128: 185 case wasm::WASM_TYPE_FUNCREF: 186 case wasm::WASM_TYPE_EXTERNREF: 187 case wasm::WASM_TYPE_EXNREF: 188 return wasm::ValType(Code); 189 } 190 if (Code == wasm::WASM_TYPE_NULLABLE || Code == wasm::WASM_TYPE_NONNULLABLE) { 191 /* Discard HeapType */ readVarint64(Ctx); 192 } 193 return wasm::ValType(wasm::ValType::OTHERREF); 194 } 195 196 static Error readInitExpr(wasm::WasmInitExpr &Expr, 197 WasmObjectFile::ReadContext &Ctx) { 198 auto Start = Ctx.Ptr; 199 200 Expr.Extended = false; 201 Expr.Inst.Opcode = readOpcode(Ctx); 202 switch (Expr.Inst.Opcode) { 203 case wasm::WASM_OPCODE_I32_CONST: 204 Expr.Inst.Value.Int32 = readVarint32(Ctx); 205 break; 206 case wasm::WASM_OPCODE_I64_CONST: 207 Expr.Inst.Value.Int64 = readVarint64(Ctx); 208 break; 209 case wasm::WASM_OPCODE_F32_CONST: 210 Expr.Inst.Value.Float32 = readFloat32(Ctx); 211 break; 212 case wasm::WASM_OPCODE_F64_CONST: 213 Expr.Inst.Value.Float64 = readFloat64(Ctx); 214 break; 215 case wasm::WASM_OPCODE_GLOBAL_GET: 216 Expr.Inst.Value.Global = readULEB128(Ctx); 217 break; 218 case wasm::WASM_OPCODE_REF_NULL: { 219 /* Discard type */ parseValType(Ctx, readVaruint32(Ctx)); 220 break; 221 } 222 default: 223 Expr.Extended = true; 224 } 225 226 if (!Expr.Extended) { 227 uint8_t EndOpcode = readOpcode(Ctx); 228 if (EndOpcode != wasm::WASM_OPCODE_END) 229 Expr.Extended = true; 230 } 231 232 if (Expr.Extended) { 233 Ctx.Ptr = Start; 234 while (true) { 235 uint8_t Opcode = readOpcode(Ctx); 236 switch (Opcode) { 237 case wasm::WASM_OPCODE_I32_CONST: 238 case wasm::WASM_OPCODE_GLOBAL_GET: 239 case wasm::WASM_OPCODE_REF_NULL: 240 case wasm::WASM_OPCODE_REF_FUNC: 241 case wasm::WASM_OPCODE_I64_CONST: 242 readULEB128(Ctx); 243 break; 244 case wasm::WASM_OPCODE_F32_CONST: 245 readFloat32(Ctx); 246 break; 247 case wasm::WASM_OPCODE_F64_CONST: 248 readFloat64(Ctx); 249 break; 250 case wasm::WASM_OPCODE_I32_ADD: 251 case wasm::WASM_OPCODE_I32_SUB: 252 case wasm::WASM_OPCODE_I32_MUL: 253 case wasm::WASM_OPCODE_I64_ADD: 254 case wasm::WASM_OPCODE_I64_SUB: 255 case wasm::WASM_OPCODE_I64_MUL: 256 break; 257 case wasm::WASM_OPCODE_GC_PREFIX: 258 break; 259 // The GC opcodes are in a separate (prefixed space). This flat switch 260 // structure works as long as there is no overlap between the GC and 261 // general opcodes used in init exprs. 262 case wasm::WASM_OPCODE_STRUCT_NEW: 263 case wasm::WASM_OPCODE_STRUCT_NEW_DEFAULT: 264 case wasm::WASM_OPCODE_ARRAY_NEW: 265 case wasm::WASM_OPCODE_ARRAY_NEW_DEFAULT: 266 readULEB128(Ctx); // heap type index 267 break; 268 case wasm::WASM_OPCODE_ARRAY_NEW_FIXED: 269 readULEB128(Ctx); // heap type index 270 readULEB128(Ctx); // array size 271 break; 272 case wasm::WASM_OPCODE_REF_I31: 273 break; 274 case wasm::WASM_OPCODE_END: 275 Expr.Body = ArrayRef<uint8_t>(Start, Ctx.Ptr - Start); 276 return Error::success(); 277 default: 278 return make_error<GenericBinaryError>( 279 Twine("invalid opcode in init_expr: ") + Twine(unsigned(Opcode)), 280 object_error::parse_failed); 281 } 282 } 283 } 284 285 return Error::success(); 286 } 287 288 static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) { 289 wasm::WasmLimits Result; 290 Result.Flags = readVaruint32(Ctx); 291 Result.Minimum = readVaruint64(Ctx); 292 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) 293 Result.Maximum = readVaruint64(Ctx); 294 return Result; 295 } 296 297 static wasm::WasmTableType readTableType(WasmObjectFile::ReadContext &Ctx) { 298 wasm::WasmTableType TableType; 299 auto ElemType = parseValType(Ctx, readVaruint32(Ctx)); 300 TableType.ElemType = ElemType; 301 TableType.Limits = readLimits(Ctx); 302 return TableType; 303 } 304 305 static Error readSection(WasmSection &Section, WasmObjectFile::ReadContext &Ctx, 306 WasmSectionOrderChecker &Checker) { 307 Section.Type = readUint8(Ctx); 308 LLVM_DEBUG(dbgs() << "readSection type=" << Section.Type << "\n"); 309 // When reading the section's size, store the size of the LEB used to encode 310 // it. This allows objcopy/strip to reproduce the binary identically. 311 const uint8_t *PreSizePtr = Ctx.Ptr; 312 uint32_t Size = readVaruint32(Ctx); 313 Section.HeaderSecSizeEncodingLen = Ctx.Ptr - PreSizePtr; 314 Section.Offset = Ctx.Ptr - Ctx.Start; 315 if (Size == 0) 316 return make_error<StringError>("zero length section", 317 object_error::parse_failed); 318 if (Ctx.Ptr + Size > Ctx.End) 319 return make_error<StringError>("section too large", 320 object_error::parse_failed); 321 if (Section.Type == wasm::WASM_SEC_CUSTOM) { 322 WasmObjectFile::ReadContext SectionCtx; 323 SectionCtx.Start = Ctx.Ptr; 324 SectionCtx.Ptr = Ctx.Ptr; 325 SectionCtx.End = Ctx.Ptr + Size; 326 327 Section.Name = readString(SectionCtx); 328 329 uint32_t SectionNameSize = SectionCtx.Ptr - SectionCtx.Start; 330 Ctx.Ptr += SectionNameSize; 331 Size -= SectionNameSize; 332 } 333 334 if (!Checker.isValidSectionOrder(Section.Type, Section.Name)) { 335 return make_error<StringError>("out of order section type: " + 336 llvm::to_string(Section.Type), 337 object_error::parse_failed); 338 } 339 340 Section.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size); 341 Ctx.Ptr += Size; 342 return Error::success(); 343 } 344 345 WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err) 346 : ObjectFile(Binary::ID_Wasm, Buffer) { 347 ErrorAsOutParameter ErrAsOutParam(Err); 348 Header.Magic = getData().substr(0, 4); 349 if (Header.Magic != StringRef("\0asm", 4)) { 350 Err = make_error<StringError>("invalid magic number", 351 object_error::parse_failed); 352 return; 353 } 354 355 ReadContext Ctx; 356 Ctx.Start = getData().bytes_begin(); 357 Ctx.Ptr = Ctx.Start + 4; 358 Ctx.End = Ctx.Start + getData().size(); 359 360 if (Ctx.Ptr + 4 > Ctx.End) { 361 Err = make_error<StringError>("missing version number", 362 object_error::parse_failed); 363 return; 364 } 365 366 Header.Version = readUint32(Ctx); 367 if (Header.Version != wasm::WasmVersion) { 368 Err = make_error<StringError>("invalid version number: " + 369 Twine(Header.Version), 370 object_error::parse_failed); 371 return; 372 } 373 374 WasmSectionOrderChecker Checker; 375 while (Ctx.Ptr < Ctx.End) { 376 WasmSection Sec; 377 if ((Err = readSection(Sec, Ctx, Checker))) 378 return; 379 if ((Err = parseSection(Sec))) 380 return; 381 382 Sections.push_back(Sec); 383 } 384 } 385 386 Error WasmObjectFile::parseSection(WasmSection &Sec) { 387 ReadContext Ctx; 388 Ctx.Start = Sec.Content.data(); 389 Ctx.End = Ctx.Start + Sec.Content.size(); 390 Ctx.Ptr = Ctx.Start; 391 switch (Sec.Type) { 392 case wasm::WASM_SEC_CUSTOM: 393 return parseCustomSection(Sec, Ctx); 394 case wasm::WASM_SEC_TYPE: 395 return parseTypeSection(Ctx); 396 case wasm::WASM_SEC_IMPORT: 397 return parseImportSection(Ctx); 398 case wasm::WASM_SEC_FUNCTION: 399 return parseFunctionSection(Ctx); 400 case wasm::WASM_SEC_TABLE: 401 return parseTableSection(Ctx); 402 case wasm::WASM_SEC_MEMORY: 403 return parseMemorySection(Ctx); 404 case wasm::WASM_SEC_TAG: 405 return parseTagSection(Ctx); 406 case wasm::WASM_SEC_GLOBAL: 407 return parseGlobalSection(Ctx); 408 case wasm::WASM_SEC_EXPORT: 409 return parseExportSection(Ctx); 410 case wasm::WASM_SEC_START: 411 return parseStartSection(Ctx); 412 case wasm::WASM_SEC_ELEM: 413 return parseElemSection(Ctx); 414 case wasm::WASM_SEC_CODE: 415 return parseCodeSection(Ctx); 416 case wasm::WASM_SEC_DATA: 417 return parseDataSection(Ctx); 418 case wasm::WASM_SEC_DATACOUNT: 419 return parseDataCountSection(Ctx); 420 default: 421 return make_error<GenericBinaryError>( 422 "invalid section type: " + Twine(Sec.Type), object_error::parse_failed); 423 } 424 } 425 426 Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) { 427 // Legacy "dylink" section support. 428 // See parseDylink0Section for the current "dylink.0" section parsing. 429 HasDylinkSection = true; 430 DylinkInfo.MemorySize = readVaruint32(Ctx); 431 DylinkInfo.MemoryAlignment = readVaruint32(Ctx); 432 DylinkInfo.TableSize = readVaruint32(Ctx); 433 DylinkInfo.TableAlignment = readVaruint32(Ctx); 434 uint32_t Count = readVaruint32(Ctx); 435 while (Count--) { 436 DylinkInfo.Needed.push_back(readString(Ctx)); 437 } 438 439 if (Ctx.Ptr != Ctx.End) 440 return make_error<GenericBinaryError>("dylink section ended prematurely", 441 object_error::parse_failed); 442 return Error::success(); 443 } 444 445 Error WasmObjectFile::parseDylink0Section(ReadContext &Ctx) { 446 // See 447 // https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md 448 HasDylinkSection = true; 449 450 const uint8_t *OrigEnd = Ctx.End; 451 while (Ctx.Ptr < OrigEnd) { 452 Ctx.End = OrigEnd; 453 uint8_t Type = readUint8(Ctx); 454 uint32_t Size = readVaruint32(Ctx); 455 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size 456 << "\n"); 457 Ctx.End = Ctx.Ptr + Size; 458 uint32_t Count; 459 switch (Type) { 460 case wasm::WASM_DYLINK_MEM_INFO: 461 DylinkInfo.MemorySize = readVaruint32(Ctx); 462 DylinkInfo.MemoryAlignment = readVaruint32(Ctx); 463 DylinkInfo.TableSize = readVaruint32(Ctx); 464 DylinkInfo.TableAlignment = readVaruint32(Ctx); 465 break; 466 case wasm::WASM_DYLINK_NEEDED: 467 Count = readVaruint32(Ctx); 468 while (Count--) { 469 DylinkInfo.Needed.push_back(readString(Ctx)); 470 } 471 break; 472 case wasm::WASM_DYLINK_EXPORT_INFO: { 473 uint32_t Count = readVaruint32(Ctx); 474 while (Count--) { 475 DylinkInfo.ExportInfo.push_back({readString(Ctx), readVaruint32(Ctx)}); 476 } 477 break; 478 } 479 case wasm::WASM_DYLINK_IMPORT_INFO: { 480 uint32_t Count = readVaruint32(Ctx); 481 while (Count--) { 482 DylinkInfo.ImportInfo.push_back( 483 {readString(Ctx), readString(Ctx), readVaruint32(Ctx)}); 484 } 485 break; 486 } 487 default: 488 LLVM_DEBUG(dbgs() << "unknown dylink.0 sub-section: " << Type << "\n"); 489 Ctx.Ptr += Size; 490 break; 491 } 492 if (Ctx.Ptr != Ctx.End) { 493 return make_error<GenericBinaryError>( 494 "dylink.0 sub-section ended prematurely", object_error::parse_failed); 495 } 496 } 497 498 if (Ctx.Ptr != Ctx.End) 499 return make_error<GenericBinaryError>("dylink.0 section ended prematurely", 500 object_error::parse_failed); 501 return Error::success(); 502 } 503 504 Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { 505 llvm::DenseSet<uint64_t> SeenFunctions; 506 llvm::DenseSet<uint64_t> SeenGlobals; 507 llvm::DenseSet<uint64_t> SeenSegments; 508 509 // If we have linking section (symbol table) or if we are parsing a DSO 510 // then we don't use the name section for symbol information. 511 bool PopulateSymbolTable = !HasLinkingSection && !HasDylinkSection; 512 513 // If we are using the name section for symbol information then it will 514 // supersede any symbols created by the export section. 515 if (PopulateSymbolTable) 516 Symbols.clear(); 517 518 while (Ctx.Ptr < Ctx.End) { 519 uint8_t Type = readUint8(Ctx); 520 uint32_t Size = readVaruint32(Ctx); 521 const uint8_t *SubSectionEnd = Ctx.Ptr + Size; 522 523 switch (Type) { 524 case wasm::WASM_NAMES_FUNCTION: 525 case wasm::WASM_NAMES_GLOBAL: 526 case wasm::WASM_NAMES_DATA_SEGMENT: { 527 uint32_t Count = readVaruint32(Ctx); 528 while (Count--) { 529 uint32_t Index = readVaruint32(Ctx); 530 StringRef Name = readString(Ctx); 531 wasm::NameType nameType = wasm::NameType::FUNCTION; 532 wasm::WasmSymbolInfo Info{Name, 533 /*Kind */ wasm::WASM_SYMBOL_TYPE_FUNCTION, 534 /* Flags */ 0, 535 /* ImportModule */ std::nullopt, 536 /* ImportName */ std::nullopt, 537 /* ExportName */ std::nullopt, 538 {/* ElementIndex */ Index}}; 539 const wasm::WasmSignature *Signature = nullptr; 540 const wasm::WasmGlobalType *GlobalType = nullptr; 541 const wasm::WasmTableType *TableType = nullptr; 542 if (Type == wasm::WASM_NAMES_FUNCTION) { 543 if (!SeenFunctions.insert(Index).second) 544 return make_error<GenericBinaryError>( 545 "function named more than once", object_error::parse_failed); 546 if (!isValidFunctionIndex(Index) || Name.empty()) 547 return make_error<GenericBinaryError>("invalid function name entry", 548 object_error::parse_failed); 549 550 if (isDefinedFunctionIndex(Index)) { 551 wasm::WasmFunction &F = getDefinedFunction(Index); 552 F.DebugName = Name; 553 Signature = &Signatures[F.SigIndex]; 554 if (F.ExportName) { 555 Info.ExportName = F.ExportName; 556 Info.Flags |= wasm::WASM_SYMBOL_BINDING_GLOBAL; 557 } else { 558 Info.Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL; 559 } 560 } else { 561 Info.Flags |= wasm::WASM_SYMBOL_UNDEFINED; 562 } 563 } else if (Type == wasm::WASM_NAMES_GLOBAL) { 564 if (!SeenGlobals.insert(Index).second) 565 return make_error<GenericBinaryError>("global named more than once", 566 object_error::parse_failed); 567 if (!isValidGlobalIndex(Index) || Name.empty()) 568 return make_error<GenericBinaryError>("invalid global name entry", 569 object_error::parse_failed); 570 nameType = wasm::NameType::GLOBAL; 571 Info.Kind = wasm::WASM_SYMBOL_TYPE_GLOBAL; 572 if (isDefinedGlobalIndex(Index)) { 573 GlobalType = &getDefinedGlobal(Index).Type; 574 } else { 575 Info.Flags |= wasm::WASM_SYMBOL_UNDEFINED; 576 } 577 } else { 578 if (!SeenSegments.insert(Index).second) 579 return make_error<GenericBinaryError>( 580 "segment named more than once", object_error::parse_failed); 581 if (Index > DataSegments.size()) 582 return make_error<GenericBinaryError>("invalid data segment name entry", 583 object_error::parse_failed); 584 nameType = wasm::NameType::DATA_SEGMENT; 585 Info.Kind = wasm::WASM_SYMBOL_TYPE_DATA; 586 Info.Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL; 587 assert(Index < DataSegments.size()); 588 Info.DataRef = wasm::WasmDataReference{ 589 Index, 0, DataSegments[Index].Data.Content.size()}; 590 } 591 DebugNames.push_back(wasm::WasmDebugName{nameType, Index, Name}); 592 if (PopulateSymbolTable) 593 Symbols.emplace_back(Info, GlobalType, TableType, Signature); 594 } 595 break; 596 } 597 // Ignore local names for now 598 case wasm::WASM_NAMES_LOCAL: 599 default: 600 Ctx.Ptr += Size; 601 break; 602 } 603 if (Ctx.Ptr != SubSectionEnd) 604 return make_error<GenericBinaryError>( 605 "name sub-section ended prematurely", object_error::parse_failed); 606 } 607 608 if (Ctx.Ptr != Ctx.End) 609 return make_error<GenericBinaryError>("name section ended prematurely", 610 object_error::parse_failed); 611 return Error::success(); 612 } 613 614 Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) { 615 HasLinkingSection = true; 616 617 LinkingData.Version = readVaruint32(Ctx); 618 if (LinkingData.Version != wasm::WasmMetadataVersion) { 619 return make_error<GenericBinaryError>( 620 "unexpected metadata version: " + Twine(LinkingData.Version) + 621 " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")", 622 object_error::parse_failed); 623 } 624 625 const uint8_t *OrigEnd = Ctx.End; 626 while (Ctx.Ptr < OrigEnd) { 627 Ctx.End = OrigEnd; 628 uint8_t Type = readUint8(Ctx); 629 uint32_t Size = readVaruint32(Ctx); 630 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size 631 << "\n"); 632 Ctx.End = Ctx.Ptr + Size; 633 switch (Type) { 634 case wasm::WASM_SYMBOL_TABLE: 635 if (Error Err = parseLinkingSectionSymtab(Ctx)) 636 return Err; 637 break; 638 case wasm::WASM_SEGMENT_INFO: { 639 uint32_t Count = readVaruint32(Ctx); 640 if (Count > DataSegments.size()) 641 return make_error<GenericBinaryError>("too many segment names", 642 object_error::parse_failed); 643 for (uint32_t I = 0; I < Count; I++) { 644 DataSegments[I].Data.Name = readString(Ctx); 645 DataSegments[I].Data.Alignment = readVaruint32(Ctx); 646 DataSegments[I].Data.LinkingFlags = readVaruint32(Ctx); 647 } 648 break; 649 } 650 case wasm::WASM_INIT_FUNCS: { 651 uint32_t Count = readVaruint32(Ctx); 652 LinkingData.InitFunctions.reserve(Count); 653 for (uint32_t I = 0; I < Count; I++) { 654 wasm::WasmInitFunc Init; 655 Init.Priority = readVaruint32(Ctx); 656 Init.Symbol = readVaruint32(Ctx); 657 if (!isValidFunctionSymbol(Init.Symbol)) 658 return make_error<GenericBinaryError>("invalid function symbol: " + 659 Twine(Init.Symbol), 660 object_error::parse_failed); 661 LinkingData.InitFunctions.emplace_back(Init); 662 } 663 break; 664 } 665 case wasm::WASM_COMDAT_INFO: 666 if (Error Err = parseLinkingSectionComdat(Ctx)) 667 return Err; 668 break; 669 default: 670 Ctx.Ptr += Size; 671 break; 672 } 673 if (Ctx.Ptr != Ctx.End) 674 return make_error<GenericBinaryError>( 675 "linking sub-section ended prematurely", object_error::parse_failed); 676 } 677 if (Ctx.Ptr != OrigEnd) 678 return make_error<GenericBinaryError>("linking section ended prematurely", 679 object_error::parse_failed); 680 return Error::success(); 681 } 682 683 Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { 684 uint32_t Count = readVaruint32(Ctx); 685 // Clear out any symbol information that was derived from the exports 686 // section. 687 Symbols.clear(); 688 Symbols.reserve(Count); 689 StringSet<> SymbolNames; 690 691 std::vector<wasm::WasmImport *> ImportedGlobals; 692 std::vector<wasm::WasmImport *> ImportedFunctions; 693 std::vector<wasm::WasmImport *> ImportedTags; 694 std::vector<wasm::WasmImport *> ImportedTables; 695 ImportedGlobals.reserve(Imports.size()); 696 ImportedFunctions.reserve(Imports.size()); 697 ImportedTags.reserve(Imports.size()); 698 ImportedTables.reserve(Imports.size()); 699 for (auto &I : Imports) { 700 if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION) 701 ImportedFunctions.emplace_back(&I); 702 else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL) 703 ImportedGlobals.emplace_back(&I); 704 else if (I.Kind == wasm::WASM_EXTERNAL_TAG) 705 ImportedTags.emplace_back(&I); 706 else if (I.Kind == wasm::WASM_EXTERNAL_TABLE) 707 ImportedTables.emplace_back(&I); 708 } 709 710 while (Count--) { 711 wasm::WasmSymbolInfo Info; 712 const wasm::WasmSignature *Signature = nullptr; 713 const wasm::WasmGlobalType *GlobalType = nullptr; 714 const wasm::WasmTableType *TableType = nullptr; 715 716 Info.Kind = readUint8(Ctx); 717 Info.Flags = readVaruint32(Ctx); 718 bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0; 719 720 switch (Info.Kind) { 721 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 722 Info.ElementIndex = readVaruint32(Ctx); 723 if (!isValidFunctionIndex(Info.ElementIndex) || 724 IsDefined != isDefinedFunctionIndex(Info.ElementIndex)) 725 return make_error<GenericBinaryError>("invalid function symbol index", 726 object_error::parse_failed); 727 if (IsDefined) { 728 Info.Name = readString(Ctx); 729 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions; 730 wasm::WasmFunction &Function = Functions[FuncIndex]; 731 Signature = &Signatures[Function.SigIndex]; 732 if (Function.SymbolName.empty()) 733 Function.SymbolName = Info.Name; 734 } else { 735 wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex]; 736 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) { 737 Info.Name = readString(Ctx); 738 Info.ImportName = Import.Field; 739 } else { 740 Info.Name = Import.Field; 741 } 742 Signature = &Signatures[Import.SigIndex]; 743 Info.ImportModule = Import.Module; 744 } 745 break; 746 747 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 748 Info.ElementIndex = readVaruint32(Ctx); 749 if (!isValidGlobalIndex(Info.ElementIndex) || 750 IsDefined != isDefinedGlobalIndex(Info.ElementIndex)) 751 return make_error<GenericBinaryError>("invalid global symbol index", 752 object_error::parse_failed); 753 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == 754 wasm::WASM_SYMBOL_BINDING_WEAK) 755 return make_error<GenericBinaryError>("undefined weak global symbol", 756 object_error::parse_failed); 757 if (IsDefined) { 758 Info.Name = readString(Ctx); 759 unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals; 760 wasm::WasmGlobal &Global = Globals[GlobalIndex]; 761 GlobalType = &Global.Type; 762 if (Global.SymbolName.empty()) 763 Global.SymbolName = Info.Name; 764 } else { 765 wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex]; 766 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) { 767 Info.Name = readString(Ctx); 768 Info.ImportName = Import.Field; 769 } else { 770 Info.Name = Import.Field; 771 } 772 GlobalType = &Import.Global; 773 Info.ImportModule = Import.Module; 774 } 775 break; 776 777 case wasm::WASM_SYMBOL_TYPE_TABLE: 778 Info.ElementIndex = readVaruint32(Ctx); 779 if (!isValidTableNumber(Info.ElementIndex) || 780 IsDefined != isDefinedTableNumber(Info.ElementIndex)) 781 return make_error<GenericBinaryError>("invalid table symbol index", 782 object_error::parse_failed); 783 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == 784 wasm::WASM_SYMBOL_BINDING_WEAK) 785 return make_error<GenericBinaryError>("undefined weak table symbol", 786 object_error::parse_failed); 787 if (IsDefined) { 788 Info.Name = readString(Ctx); 789 unsigned TableNumber = Info.ElementIndex - NumImportedTables; 790 wasm::WasmTable &Table = Tables[TableNumber]; 791 TableType = &Table.Type; 792 if (Table.SymbolName.empty()) 793 Table.SymbolName = Info.Name; 794 } else { 795 wasm::WasmImport &Import = *ImportedTables[Info.ElementIndex]; 796 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) { 797 Info.Name = readString(Ctx); 798 Info.ImportName = Import.Field; 799 } else { 800 Info.Name = Import.Field; 801 } 802 TableType = &Import.Table; 803 Info.ImportModule = Import.Module; 804 } 805 break; 806 807 case wasm::WASM_SYMBOL_TYPE_DATA: 808 Info.Name = readString(Ctx); 809 if (IsDefined) { 810 auto Index = readVaruint32(Ctx); 811 auto Offset = readVaruint64(Ctx); 812 auto Size = readVaruint64(Ctx); 813 if (!(Info.Flags & wasm::WASM_SYMBOL_ABSOLUTE)) { 814 if (static_cast<size_t>(Index) >= DataSegments.size()) 815 return make_error<GenericBinaryError>( 816 "invalid data segment index: " + Twine(Index), 817 object_error::parse_failed); 818 size_t SegmentSize = DataSegments[Index].Data.Content.size(); 819 if (Offset > SegmentSize) 820 return make_error<GenericBinaryError>( 821 "invalid data symbol offset: `" + Info.Name + 822 "` (offset: " + Twine(Offset) + 823 " segment size: " + Twine(SegmentSize) + ")", 824 object_error::parse_failed); 825 } 826 Info.DataRef = wasm::WasmDataReference{Index, Offset, Size}; 827 } 828 break; 829 830 case wasm::WASM_SYMBOL_TYPE_SECTION: { 831 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != 832 wasm::WASM_SYMBOL_BINDING_LOCAL) 833 return make_error<GenericBinaryError>( 834 "section symbols must have local binding", 835 object_error::parse_failed); 836 Info.ElementIndex = readVaruint32(Ctx); 837 // Use somewhat unique section name as symbol name. 838 StringRef SectionName = Sections[Info.ElementIndex].Name; 839 Info.Name = SectionName; 840 break; 841 } 842 843 case wasm::WASM_SYMBOL_TYPE_TAG: { 844 Info.ElementIndex = readVaruint32(Ctx); 845 if (!isValidTagIndex(Info.ElementIndex) || 846 IsDefined != isDefinedTagIndex(Info.ElementIndex)) 847 return make_error<GenericBinaryError>("invalid tag symbol index", 848 object_error::parse_failed); 849 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == 850 wasm::WASM_SYMBOL_BINDING_WEAK) 851 return make_error<GenericBinaryError>("undefined weak global symbol", 852 object_error::parse_failed); 853 if (IsDefined) { 854 Info.Name = readString(Ctx); 855 unsigned TagIndex = Info.ElementIndex - NumImportedTags; 856 wasm::WasmTag &Tag = Tags[TagIndex]; 857 Signature = &Signatures[Tag.SigIndex]; 858 if (Tag.SymbolName.empty()) 859 Tag.SymbolName = Info.Name; 860 861 } else { 862 wasm::WasmImport &Import = *ImportedTags[Info.ElementIndex]; 863 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) { 864 Info.Name = readString(Ctx); 865 Info.ImportName = Import.Field; 866 } else { 867 Info.Name = Import.Field; 868 } 869 Signature = &Signatures[Import.SigIndex]; 870 Info.ImportModule = Import.Module; 871 } 872 break; 873 } 874 875 default: 876 return make_error<GenericBinaryError>("invalid symbol type: " + 877 Twine(unsigned(Info.Kind)), 878 object_error::parse_failed); 879 } 880 881 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != 882 wasm::WASM_SYMBOL_BINDING_LOCAL && 883 !SymbolNames.insert(Info.Name).second) 884 return make_error<GenericBinaryError>("duplicate symbol name " + 885 Twine(Info.Name), 886 object_error::parse_failed); 887 Symbols.emplace_back(Info, GlobalType, TableType, Signature); 888 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n"); 889 } 890 891 return Error::success(); 892 } 893 894 Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) { 895 uint32_t ComdatCount = readVaruint32(Ctx); 896 StringSet<> ComdatSet; 897 for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) { 898 StringRef Name = readString(Ctx); 899 if (Name.empty() || !ComdatSet.insert(Name).second) 900 return make_error<GenericBinaryError>("bad/duplicate COMDAT name " + 901 Twine(Name), 902 object_error::parse_failed); 903 LinkingData.Comdats.emplace_back(Name); 904 uint32_t Flags = readVaruint32(Ctx); 905 if (Flags != 0) 906 return make_error<GenericBinaryError>("unsupported COMDAT flags", 907 object_error::parse_failed); 908 909 uint32_t EntryCount = readVaruint32(Ctx); 910 while (EntryCount--) { 911 unsigned Kind = readVaruint32(Ctx); 912 unsigned Index = readVaruint32(Ctx); 913 switch (Kind) { 914 default: 915 return make_error<GenericBinaryError>("invalid COMDAT entry type", 916 object_error::parse_failed); 917 case wasm::WASM_COMDAT_DATA: 918 if (Index >= DataSegments.size()) 919 return make_error<GenericBinaryError>( 920 "COMDAT data index out of range", object_error::parse_failed); 921 if (DataSegments[Index].Data.Comdat != UINT32_MAX) 922 return make_error<GenericBinaryError>("data segment in two COMDATs", 923 object_error::parse_failed); 924 DataSegments[Index].Data.Comdat = ComdatIndex; 925 break; 926 case wasm::WASM_COMDAT_FUNCTION: 927 if (!isDefinedFunctionIndex(Index)) 928 return make_error<GenericBinaryError>( 929 "COMDAT function index out of range", object_error::parse_failed); 930 if (getDefinedFunction(Index).Comdat != UINT32_MAX) 931 return make_error<GenericBinaryError>("function in two COMDATs", 932 object_error::parse_failed); 933 getDefinedFunction(Index).Comdat = ComdatIndex; 934 break; 935 case wasm::WASM_COMDAT_SECTION: 936 if (Index >= Sections.size()) 937 return make_error<GenericBinaryError>( 938 "COMDAT section index out of range", object_error::parse_failed); 939 if (Sections[Index].Type != wasm::WASM_SEC_CUSTOM) 940 return make_error<GenericBinaryError>( 941 "non-custom section in a COMDAT", object_error::parse_failed); 942 Sections[Index].Comdat = ComdatIndex; 943 break; 944 } 945 } 946 } 947 return Error::success(); 948 } 949 950 Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) { 951 llvm::SmallSet<StringRef, 3> FieldsSeen; 952 uint32_t Fields = readVaruint32(Ctx); 953 for (size_t I = 0; I < Fields; ++I) { 954 StringRef FieldName = readString(Ctx); 955 if (!FieldsSeen.insert(FieldName).second) 956 return make_error<GenericBinaryError>( 957 "producers section does not have unique fields", 958 object_error::parse_failed); 959 std::vector<std::pair<std::string, std::string>> *ProducerVec = nullptr; 960 if (FieldName == "language") { 961 ProducerVec = &ProducerInfo.Languages; 962 } else if (FieldName == "processed-by") { 963 ProducerVec = &ProducerInfo.Tools; 964 } else if (FieldName == "sdk") { 965 ProducerVec = &ProducerInfo.SDKs; 966 } else { 967 return make_error<GenericBinaryError>( 968 "producers section field is not named one of language, processed-by, " 969 "or sdk", 970 object_error::parse_failed); 971 } 972 uint32_t ValueCount = readVaruint32(Ctx); 973 llvm::SmallSet<StringRef, 8> ProducersSeen; 974 for (size_t J = 0; J < ValueCount; ++J) { 975 StringRef Name = readString(Ctx); 976 StringRef Version = readString(Ctx); 977 if (!ProducersSeen.insert(Name).second) { 978 return make_error<GenericBinaryError>( 979 "producers section contains repeated producer", 980 object_error::parse_failed); 981 } 982 ProducerVec->emplace_back(std::string(Name), std::string(Version)); 983 } 984 } 985 if (Ctx.Ptr != Ctx.End) 986 return make_error<GenericBinaryError>("producers section ended prematurely", 987 object_error::parse_failed); 988 return Error::success(); 989 } 990 991 Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) { 992 llvm::SmallSet<std::string, 8> FeaturesSeen; 993 uint32_t FeatureCount = readVaruint32(Ctx); 994 for (size_t I = 0; I < FeatureCount; ++I) { 995 wasm::WasmFeatureEntry Feature; 996 Feature.Prefix = readUint8(Ctx); 997 switch (Feature.Prefix) { 998 case wasm::WASM_FEATURE_PREFIX_USED: 999 case wasm::WASM_FEATURE_PREFIX_DISALLOWED: 1000 break; 1001 default: 1002 return make_error<GenericBinaryError>("unknown feature policy prefix", 1003 object_error::parse_failed); 1004 } 1005 Feature.Name = std::string(readString(Ctx)); 1006 if (!FeaturesSeen.insert(Feature.Name).second) 1007 return make_error<GenericBinaryError>( 1008 "target features section contains repeated feature \"" + 1009 Feature.Name + "\"", 1010 object_error::parse_failed); 1011 TargetFeatures.push_back(Feature); 1012 } 1013 if (Ctx.Ptr != Ctx.End) 1014 return make_error<GenericBinaryError>( 1015 "target features section ended prematurely", 1016 object_error::parse_failed); 1017 return Error::success(); 1018 } 1019 1020 Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) { 1021 uint32_t SectionIndex = readVaruint32(Ctx); 1022 if (SectionIndex >= Sections.size()) 1023 return make_error<GenericBinaryError>("invalid section index", 1024 object_error::parse_failed); 1025 WasmSection &Section = Sections[SectionIndex]; 1026 uint32_t RelocCount = readVaruint32(Ctx); 1027 uint32_t EndOffset = Section.Content.size(); 1028 uint32_t PreviousOffset = 0; 1029 while (RelocCount--) { 1030 wasm::WasmRelocation Reloc = {}; 1031 uint32_t type = readVaruint32(Ctx); 1032 Reloc.Type = type; 1033 Reloc.Offset = readVaruint32(Ctx); 1034 if (Reloc.Offset < PreviousOffset) 1035 return make_error<GenericBinaryError>("relocations not in offset order", 1036 object_error::parse_failed); 1037 1038 auto badReloc = [&](StringRef msg) { 1039 return make_error<GenericBinaryError>( 1040 msg + ": " + Twine(Symbols[Reloc.Index].Info.Name), 1041 object_error::parse_failed); 1042 }; 1043 1044 PreviousOffset = Reloc.Offset; 1045 Reloc.Index = readVaruint32(Ctx); 1046 switch (type) { 1047 case wasm::R_WASM_FUNCTION_INDEX_LEB: 1048 case wasm::R_WASM_FUNCTION_INDEX_I32: 1049 case wasm::R_WASM_TABLE_INDEX_SLEB: 1050 case wasm::R_WASM_TABLE_INDEX_SLEB64: 1051 case wasm::R_WASM_TABLE_INDEX_I32: 1052 case wasm::R_WASM_TABLE_INDEX_I64: 1053 case wasm::R_WASM_TABLE_INDEX_REL_SLEB: 1054 case wasm::R_WASM_TABLE_INDEX_REL_SLEB64: 1055 if (!isValidFunctionSymbol(Reloc.Index)) 1056 return badReloc("invalid function relocation"); 1057 break; 1058 case wasm::R_WASM_TABLE_NUMBER_LEB: 1059 if (!isValidTableSymbol(Reloc.Index)) 1060 return badReloc("invalid table relocation"); 1061 break; 1062 case wasm::R_WASM_TYPE_INDEX_LEB: 1063 if (Reloc.Index >= Signatures.size()) 1064 return badReloc("invalid relocation type index"); 1065 break; 1066 case wasm::R_WASM_GLOBAL_INDEX_LEB: 1067 // R_WASM_GLOBAL_INDEX_LEB are can be used against function and data 1068 // symbols to refer to their GOT entries. 1069 if (!isValidGlobalSymbol(Reloc.Index) && 1070 !isValidDataSymbol(Reloc.Index) && 1071 !isValidFunctionSymbol(Reloc.Index)) 1072 return badReloc("invalid global relocation"); 1073 break; 1074 case wasm::R_WASM_GLOBAL_INDEX_I32: 1075 if (!isValidGlobalSymbol(Reloc.Index)) 1076 return badReloc("invalid global relocation"); 1077 break; 1078 case wasm::R_WASM_TAG_INDEX_LEB: 1079 if (!isValidTagSymbol(Reloc.Index)) 1080 return badReloc("invalid tag relocation"); 1081 break; 1082 case wasm::R_WASM_MEMORY_ADDR_LEB: 1083 case wasm::R_WASM_MEMORY_ADDR_SLEB: 1084 case wasm::R_WASM_MEMORY_ADDR_I32: 1085 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB: 1086 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB: 1087 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32: 1088 if (!isValidDataSymbol(Reloc.Index)) 1089 return badReloc("invalid data relocation"); 1090 Reloc.Addend = readVarint32(Ctx); 1091 break; 1092 case wasm::R_WASM_MEMORY_ADDR_LEB64: 1093 case wasm::R_WASM_MEMORY_ADDR_SLEB64: 1094 case wasm::R_WASM_MEMORY_ADDR_I64: 1095 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64: 1096 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64: 1097 if (!isValidDataSymbol(Reloc.Index)) 1098 return badReloc("invalid data relocation"); 1099 Reloc.Addend = readVarint64(Ctx); 1100 break; 1101 case wasm::R_WASM_FUNCTION_OFFSET_I32: 1102 if (!isValidFunctionSymbol(Reloc.Index)) 1103 return badReloc("invalid function relocation"); 1104 Reloc.Addend = readVarint32(Ctx); 1105 break; 1106 case wasm::R_WASM_FUNCTION_OFFSET_I64: 1107 if (!isValidFunctionSymbol(Reloc.Index)) 1108 return badReloc("invalid function relocation"); 1109 Reloc.Addend = readVarint64(Ctx); 1110 break; 1111 case wasm::R_WASM_SECTION_OFFSET_I32: 1112 if (!isValidSectionSymbol(Reloc.Index)) 1113 return badReloc("invalid section relocation"); 1114 Reloc.Addend = readVarint32(Ctx); 1115 break; 1116 default: 1117 return make_error<GenericBinaryError>("invalid relocation type: " + 1118 Twine(type), 1119 object_error::parse_failed); 1120 } 1121 1122 // Relocations must fit inside the section, and must appear in order. They 1123 // also shouldn't overlap a function/element boundary, but we don't bother 1124 // to check that. 1125 uint64_t Size = 5; 1126 if (Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LEB64 || 1127 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_SLEB64 || 1128 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_REL_SLEB64) 1129 Size = 10; 1130 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I32 || 1131 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I32 || 1132 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_LOCREL_I32 || 1133 Reloc.Type == wasm::R_WASM_SECTION_OFFSET_I32 || 1134 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I32 || 1135 Reloc.Type == wasm::R_WASM_FUNCTION_INDEX_I32 || 1136 Reloc.Type == wasm::R_WASM_GLOBAL_INDEX_I32) 1137 Size = 4; 1138 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I64 || 1139 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I64 || 1140 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I64) 1141 Size = 8; 1142 if (Reloc.Offset + Size > EndOffset) 1143 return make_error<GenericBinaryError>("invalid relocation offset", 1144 object_error::parse_failed); 1145 1146 Section.Relocations.push_back(Reloc); 1147 } 1148 if (Ctx.Ptr != Ctx.End) 1149 return make_error<GenericBinaryError>("reloc section ended prematurely", 1150 object_error::parse_failed); 1151 return Error::success(); 1152 } 1153 1154 Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) { 1155 if (Sec.Name == "dylink") { 1156 if (Error Err = parseDylinkSection(Ctx)) 1157 return Err; 1158 } else if (Sec.Name == "dylink.0") { 1159 if (Error Err = parseDylink0Section(Ctx)) 1160 return Err; 1161 } else if (Sec.Name == "name") { 1162 if (Error Err = parseNameSection(Ctx)) 1163 return Err; 1164 } else if (Sec.Name == "linking") { 1165 if (Error Err = parseLinkingSection(Ctx)) 1166 return Err; 1167 } else if (Sec.Name == "producers") { 1168 if (Error Err = parseProducersSection(Ctx)) 1169 return Err; 1170 } else if (Sec.Name == "target_features") { 1171 if (Error Err = parseTargetFeaturesSection(Ctx)) 1172 return Err; 1173 } else if (Sec.Name.starts_with("reloc.")) { 1174 if (Error Err = parseRelocSection(Sec.Name, Ctx)) 1175 return Err; 1176 } 1177 return Error::success(); 1178 } 1179 1180 Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) { 1181 auto parseFieldDef = [&]() { 1182 uint32_t TypeCode = readVaruint32((Ctx)); 1183 /* Discard StorageType */ parseValType(Ctx, TypeCode); 1184 /* Discard Mutability */ readVaruint32(Ctx); 1185 }; 1186 1187 uint32_t Count = readVaruint32(Ctx); 1188 Signatures.reserve(Count); 1189 while (Count--) { 1190 wasm::WasmSignature Sig; 1191 uint8_t Form = readUint8(Ctx); 1192 if (Form == wasm::WASM_TYPE_REC) { 1193 // Rec groups expand the type index space (beyond what was declared at 1194 // the top of the section, and also consume one element in that space. 1195 uint32_t RecSize = readVaruint32(Ctx); 1196 if (RecSize == 0) 1197 return make_error<GenericBinaryError>("Rec group size cannot be 0", 1198 object_error::parse_failed); 1199 Signatures.reserve(Signatures.size() + RecSize); 1200 Count += RecSize; 1201 Sig.Kind = wasm::WasmSignature::Placeholder; 1202 Signatures.push_back(std::move(Sig)); 1203 HasUnmodeledTypes = true; 1204 continue; 1205 } 1206 if (Form != wasm::WASM_TYPE_FUNC) { 1207 // Currently LLVM only models function types, and not other composite 1208 // types. Here we parse the type declarations just enough to skip past 1209 // them in the binary. 1210 if (Form == wasm::WASM_TYPE_SUB || Form == wasm::WASM_TYPE_SUB_FINAL) { 1211 uint32_t Supers = readVaruint32(Ctx); 1212 if (Supers > 0) { 1213 if (Supers != 1) 1214 return make_error<GenericBinaryError>( 1215 "Invalid number of supertypes", object_error::parse_failed); 1216 /* Discard SuperIndex */ readVaruint32(Ctx); 1217 } 1218 Form = readVaruint32(Ctx); 1219 } 1220 if (Form == wasm::WASM_TYPE_STRUCT) { 1221 uint32_t FieldCount = readVaruint32(Ctx); 1222 while (FieldCount--) { 1223 parseFieldDef(); 1224 } 1225 } else if (Form == wasm::WASM_TYPE_ARRAY) { 1226 parseFieldDef(); 1227 } else { 1228 return make_error<GenericBinaryError>("bad form", 1229 object_error::parse_failed); 1230 } 1231 Sig.Kind = wasm::WasmSignature::Placeholder; 1232 Signatures.push_back(std::move(Sig)); 1233 HasUnmodeledTypes = true; 1234 continue; 1235 } 1236 1237 uint32_t ParamCount = readVaruint32(Ctx); 1238 Sig.Params.reserve(ParamCount); 1239 while (ParamCount--) { 1240 uint32_t ParamType = readUint8(Ctx); 1241 Sig.Params.push_back(parseValType(Ctx, ParamType)); 1242 } 1243 uint32_t ReturnCount = readVaruint32(Ctx); 1244 while (ReturnCount--) { 1245 uint32_t ReturnType = readUint8(Ctx); 1246 Sig.Returns.push_back(parseValType(Ctx, ReturnType)); 1247 } 1248 1249 Signatures.push_back(std::move(Sig)); 1250 } 1251 if (Ctx.Ptr != Ctx.End) 1252 return make_error<GenericBinaryError>("type section ended prematurely", 1253 object_error::parse_failed); 1254 return Error::success(); 1255 } 1256 1257 Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { 1258 uint32_t Count = readVaruint32(Ctx); 1259 uint32_t NumTypes = Signatures.size(); 1260 Imports.reserve(Count); 1261 for (uint32_t I = 0; I < Count; I++) { 1262 wasm::WasmImport Im; 1263 Im.Module = readString(Ctx); 1264 Im.Field = readString(Ctx); 1265 Im.Kind = readUint8(Ctx); 1266 switch (Im.Kind) { 1267 case wasm::WASM_EXTERNAL_FUNCTION: 1268 NumImportedFunctions++; 1269 Im.SigIndex = readVaruint32(Ctx); 1270 if (Im.SigIndex >= NumTypes) 1271 return make_error<GenericBinaryError>("invalid function type", 1272 object_error::parse_failed); 1273 break; 1274 case wasm::WASM_EXTERNAL_GLOBAL: 1275 NumImportedGlobals++; 1276 Im.Global.Type = readUint8(Ctx); 1277 Im.Global.Mutable = readVaruint1(Ctx); 1278 break; 1279 case wasm::WASM_EXTERNAL_MEMORY: 1280 Im.Memory = readLimits(Ctx); 1281 if (Im.Memory.Flags & wasm::WASM_LIMITS_FLAG_IS_64) 1282 HasMemory64 = true; 1283 break; 1284 case wasm::WASM_EXTERNAL_TABLE: { 1285 Im.Table = readTableType(Ctx); 1286 NumImportedTables++; 1287 auto ElemType = Im.Table.ElemType; 1288 if (ElemType != wasm::ValType::FUNCREF && 1289 ElemType != wasm::ValType::EXTERNREF && 1290 ElemType != wasm::ValType::EXNREF && 1291 ElemType != wasm::ValType::OTHERREF) 1292 return make_error<GenericBinaryError>("invalid table element type", 1293 object_error::parse_failed); 1294 break; 1295 } 1296 case wasm::WASM_EXTERNAL_TAG: 1297 NumImportedTags++; 1298 if (readUint8(Ctx) != 0) // Reserved 'attribute' field 1299 return make_error<GenericBinaryError>("invalid attribute", 1300 object_error::parse_failed); 1301 Im.SigIndex = readVaruint32(Ctx); 1302 if (Im.SigIndex >= NumTypes) 1303 return make_error<GenericBinaryError>("invalid tag type", 1304 object_error::parse_failed); 1305 break; 1306 default: 1307 return make_error<GenericBinaryError>("unexpected import kind", 1308 object_error::parse_failed); 1309 } 1310 Imports.push_back(Im); 1311 } 1312 if (Ctx.Ptr != Ctx.End) 1313 return make_error<GenericBinaryError>("import section ended prematurely", 1314 object_error::parse_failed); 1315 return Error::success(); 1316 } 1317 1318 Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) { 1319 uint32_t Count = readVaruint32(Ctx); 1320 Functions.reserve(Count); 1321 uint32_t NumTypes = Signatures.size(); 1322 while (Count--) { 1323 uint32_t Type = readVaruint32(Ctx); 1324 if (Type >= NumTypes) 1325 return make_error<GenericBinaryError>("invalid function type", 1326 object_error::parse_failed); 1327 wasm::WasmFunction F; 1328 F.SigIndex = Type; 1329 Functions.push_back(F); 1330 } 1331 if (Ctx.Ptr != Ctx.End) 1332 return make_error<GenericBinaryError>("function section ended prematurely", 1333 object_error::parse_failed); 1334 return Error::success(); 1335 } 1336 1337 Error WasmObjectFile::parseTableSection(ReadContext &Ctx) { 1338 TableSection = Sections.size(); 1339 uint32_t Count = readVaruint32(Ctx); 1340 Tables.reserve(Count); 1341 while (Count--) { 1342 wasm::WasmTable T; 1343 T.Type = readTableType(Ctx); 1344 T.Index = NumImportedTables + Tables.size(); 1345 Tables.push_back(T); 1346 auto ElemType = Tables.back().Type.ElemType; 1347 if (ElemType != wasm::ValType::FUNCREF && 1348 ElemType != wasm::ValType::EXTERNREF && 1349 ElemType != wasm::ValType::EXNREF && 1350 ElemType != wasm::ValType::OTHERREF) { 1351 return make_error<GenericBinaryError>("invalid table element type", 1352 object_error::parse_failed); 1353 } 1354 } 1355 if (Ctx.Ptr != Ctx.End) 1356 return make_error<GenericBinaryError>("table section ended prematurely", 1357 object_error::parse_failed); 1358 return Error::success(); 1359 } 1360 1361 Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) { 1362 uint32_t Count = readVaruint32(Ctx); 1363 Memories.reserve(Count); 1364 while (Count--) { 1365 auto Limits = readLimits(Ctx); 1366 if (Limits.Flags & wasm::WASM_LIMITS_FLAG_IS_64) 1367 HasMemory64 = true; 1368 Memories.push_back(Limits); 1369 } 1370 if (Ctx.Ptr != Ctx.End) 1371 return make_error<GenericBinaryError>("memory section ended prematurely", 1372 object_error::parse_failed); 1373 return Error::success(); 1374 } 1375 1376 Error WasmObjectFile::parseTagSection(ReadContext &Ctx) { 1377 TagSection = Sections.size(); 1378 uint32_t Count = readVaruint32(Ctx); 1379 Tags.reserve(Count); 1380 uint32_t NumTypes = Signatures.size(); 1381 while (Count--) { 1382 if (readUint8(Ctx) != 0) // Reserved 'attribute' field 1383 return make_error<GenericBinaryError>("invalid attribute", 1384 object_error::parse_failed); 1385 uint32_t Type = readVaruint32(Ctx); 1386 if (Type >= NumTypes) 1387 return make_error<GenericBinaryError>("invalid tag type", 1388 object_error::parse_failed); 1389 wasm::WasmTag Tag; 1390 Tag.Index = NumImportedTags + Tags.size(); 1391 Tag.SigIndex = Type; 1392 Signatures[Type].Kind = wasm::WasmSignature::Tag; 1393 Tags.push_back(Tag); 1394 } 1395 1396 if (Ctx.Ptr != Ctx.End) 1397 return make_error<GenericBinaryError>("tag section ended prematurely", 1398 object_error::parse_failed); 1399 return Error::success(); 1400 } 1401 1402 Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) { 1403 GlobalSection = Sections.size(); 1404 const uint8_t *SectionStart = Ctx.Ptr; 1405 uint32_t Count = readVaruint32(Ctx); 1406 Globals.reserve(Count); 1407 while (Count--) { 1408 wasm::WasmGlobal Global; 1409 Global.Index = NumImportedGlobals + Globals.size(); 1410 const uint8_t *GlobalStart = Ctx.Ptr; 1411 Global.Offset = static_cast<uint32_t>(GlobalStart - SectionStart); 1412 auto GlobalOpcode = readVaruint32(Ctx); 1413 Global.Type.Type = (uint8_t)parseValType(Ctx, GlobalOpcode); 1414 Global.Type.Mutable = readVaruint1(Ctx); 1415 if (Error Err = readInitExpr(Global.InitExpr, Ctx)) 1416 return Err; 1417 Global.Size = static_cast<uint32_t>(Ctx.Ptr - GlobalStart); 1418 Globals.push_back(Global); 1419 } 1420 if (Ctx.Ptr != Ctx.End) 1421 return make_error<GenericBinaryError>("global section ended prematurely", 1422 object_error::parse_failed); 1423 return Error::success(); 1424 } 1425 1426 Error WasmObjectFile::parseExportSection(ReadContext &Ctx) { 1427 uint32_t Count = readVaruint32(Ctx); 1428 Exports.reserve(Count); 1429 Symbols.reserve(Count); 1430 for (uint32_t I = 0; I < Count; I++) { 1431 wasm::WasmExport Ex; 1432 Ex.Name = readString(Ctx); 1433 Ex.Kind = readUint8(Ctx); 1434 Ex.Index = readVaruint32(Ctx); 1435 const wasm::WasmSignature *Signature = nullptr; 1436 const wasm::WasmGlobalType *GlobalType = nullptr; 1437 const wasm::WasmTableType *TableType = nullptr; 1438 wasm::WasmSymbolInfo Info; 1439 Info.Name = Ex.Name; 1440 Info.Flags = 0; 1441 switch (Ex.Kind) { 1442 case wasm::WASM_EXTERNAL_FUNCTION: { 1443 if (!isValidFunctionIndex(Ex.Index)) 1444 return make_error<GenericBinaryError>("invalid function export", 1445 object_error::parse_failed); 1446 Info.Kind = wasm::WASM_SYMBOL_TYPE_FUNCTION; 1447 Info.ElementIndex = Ex.Index; 1448 if (isDefinedFunctionIndex(Ex.Index)) { 1449 getDefinedFunction(Ex.Index).ExportName = Ex.Name; 1450 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions; 1451 wasm::WasmFunction &Function = Functions[FuncIndex]; 1452 Signature = &Signatures[Function.SigIndex]; 1453 } 1454 // Else the function is imported. LLVM object files don't use this 1455 // pattern and we still treat this as an undefined symbol, but we want to 1456 // parse it without crashing. 1457 break; 1458 } 1459 case wasm::WASM_EXTERNAL_GLOBAL: { 1460 if (!isValidGlobalIndex(Ex.Index)) 1461 return make_error<GenericBinaryError>("invalid global export", 1462 object_error::parse_failed); 1463 Info.Kind = wasm::WASM_SYMBOL_TYPE_DATA; 1464 uint64_t Offset = 0; 1465 if (isDefinedGlobalIndex(Ex.Index)) { 1466 auto Global = getDefinedGlobal(Ex.Index); 1467 if (!Global.InitExpr.Extended) { 1468 auto Inst = Global.InitExpr.Inst; 1469 if (Inst.Opcode == wasm::WASM_OPCODE_I32_CONST) { 1470 Offset = Inst.Value.Int32; 1471 } else if (Inst.Opcode == wasm::WASM_OPCODE_I64_CONST) { 1472 Offset = Inst.Value.Int64; 1473 } 1474 } 1475 } 1476 Info.DataRef = wasm::WasmDataReference{0, Offset, 0}; 1477 break; 1478 } 1479 case wasm::WASM_EXTERNAL_TAG: 1480 if (!isValidTagIndex(Ex.Index)) 1481 return make_error<GenericBinaryError>("invalid tag export", 1482 object_error::parse_failed); 1483 Info.Kind = wasm::WASM_SYMBOL_TYPE_TAG; 1484 Info.ElementIndex = Ex.Index; 1485 break; 1486 case wasm::WASM_EXTERNAL_MEMORY: 1487 break; 1488 case wasm::WASM_EXTERNAL_TABLE: 1489 Info.Kind = wasm::WASM_SYMBOL_TYPE_TABLE; 1490 Info.ElementIndex = Ex.Index; 1491 break; 1492 default: 1493 return make_error<GenericBinaryError>("unexpected export kind", 1494 object_error::parse_failed); 1495 } 1496 Exports.push_back(Ex); 1497 if (Ex.Kind != wasm::WASM_EXTERNAL_MEMORY) { 1498 Symbols.emplace_back(Info, GlobalType, TableType, Signature); 1499 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n"); 1500 } 1501 } 1502 if (Ctx.Ptr != Ctx.End) 1503 return make_error<GenericBinaryError>("export section ended prematurely", 1504 object_error::parse_failed); 1505 return Error::success(); 1506 } 1507 1508 bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const { 1509 return Index < NumImportedFunctions + Functions.size(); 1510 } 1511 1512 bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const { 1513 return Index >= NumImportedFunctions && isValidFunctionIndex(Index); 1514 } 1515 1516 bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const { 1517 return Index < NumImportedGlobals + Globals.size(); 1518 } 1519 1520 bool WasmObjectFile::isValidTableNumber(uint32_t Index) const { 1521 return Index < NumImportedTables + Tables.size(); 1522 } 1523 1524 bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const { 1525 return Index >= NumImportedGlobals && isValidGlobalIndex(Index); 1526 } 1527 1528 bool WasmObjectFile::isDefinedTableNumber(uint32_t Index) const { 1529 return Index >= NumImportedTables && isValidTableNumber(Index); 1530 } 1531 1532 bool WasmObjectFile::isValidTagIndex(uint32_t Index) const { 1533 return Index < NumImportedTags + Tags.size(); 1534 } 1535 1536 bool WasmObjectFile::isDefinedTagIndex(uint32_t Index) const { 1537 return Index >= NumImportedTags && isValidTagIndex(Index); 1538 } 1539 1540 bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const { 1541 return Index < Symbols.size() && Symbols[Index].isTypeFunction(); 1542 } 1543 1544 bool WasmObjectFile::isValidTableSymbol(uint32_t Index) const { 1545 return Index < Symbols.size() && Symbols[Index].isTypeTable(); 1546 } 1547 1548 bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const { 1549 return Index < Symbols.size() && Symbols[Index].isTypeGlobal(); 1550 } 1551 1552 bool WasmObjectFile::isValidTagSymbol(uint32_t Index) const { 1553 return Index < Symbols.size() && Symbols[Index].isTypeTag(); 1554 } 1555 1556 bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const { 1557 return Index < Symbols.size() && Symbols[Index].isTypeData(); 1558 } 1559 1560 bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const { 1561 return Index < Symbols.size() && Symbols[Index].isTypeSection(); 1562 } 1563 1564 wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) { 1565 assert(isDefinedFunctionIndex(Index)); 1566 return Functions[Index - NumImportedFunctions]; 1567 } 1568 1569 const wasm::WasmFunction & 1570 WasmObjectFile::getDefinedFunction(uint32_t Index) const { 1571 assert(isDefinedFunctionIndex(Index)); 1572 return Functions[Index - NumImportedFunctions]; 1573 } 1574 1575 const wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) const { 1576 assert(isDefinedGlobalIndex(Index)); 1577 return Globals[Index - NumImportedGlobals]; 1578 } 1579 1580 wasm::WasmTag &WasmObjectFile::getDefinedTag(uint32_t Index) { 1581 assert(isDefinedTagIndex(Index)); 1582 return Tags[Index - NumImportedTags]; 1583 } 1584 1585 Error WasmObjectFile::parseStartSection(ReadContext &Ctx) { 1586 StartFunction = readVaruint32(Ctx); 1587 if (!isValidFunctionIndex(StartFunction)) 1588 return make_error<GenericBinaryError>("invalid start function", 1589 object_error::parse_failed); 1590 return Error::success(); 1591 } 1592 1593 Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) { 1594 CodeSection = Sections.size(); 1595 uint32_t FunctionCount = readVaruint32(Ctx); 1596 if (FunctionCount != Functions.size()) { 1597 return make_error<GenericBinaryError>("invalid function count", 1598 object_error::parse_failed); 1599 } 1600 1601 for (uint32_t i = 0; i < FunctionCount; i++) { 1602 wasm::WasmFunction& Function = Functions[i]; 1603 const uint8_t *FunctionStart = Ctx.Ptr; 1604 uint32_t Size = readVaruint32(Ctx); 1605 const uint8_t *FunctionEnd = Ctx.Ptr + Size; 1606 1607 Function.CodeOffset = Ctx.Ptr - FunctionStart; 1608 Function.Index = NumImportedFunctions + i; 1609 Function.CodeSectionOffset = FunctionStart - Ctx.Start; 1610 Function.Size = FunctionEnd - FunctionStart; 1611 1612 uint32_t NumLocalDecls = readVaruint32(Ctx); 1613 Function.Locals.reserve(NumLocalDecls); 1614 while (NumLocalDecls--) { 1615 wasm::WasmLocalDecl Decl; 1616 Decl.Count = readVaruint32(Ctx); 1617 Decl.Type = readUint8(Ctx); 1618 Function.Locals.push_back(Decl); 1619 } 1620 1621 uint32_t BodySize = FunctionEnd - Ctx.Ptr; 1622 // Ensure that Function is within Ctx's buffer. 1623 if (Ctx.Ptr + BodySize > Ctx.End) { 1624 return make_error<GenericBinaryError>("Function extends beyond buffer", 1625 object_error::parse_failed); 1626 } 1627 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize); 1628 // This will be set later when reading in the linking metadata section. 1629 Function.Comdat = UINT32_MAX; 1630 Ctx.Ptr += BodySize; 1631 assert(Ctx.Ptr == FunctionEnd); 1632 } 1633 if (Ctx.Ptr != Ctx.End) 1634 return make_error<GenericBinaryError>("code section ended prematurely", 1635 object_error::parse_failed); 1636 return Error::success(); 1637 } 1638 1639 Error WasmObjectFile::parseElemSection(ReadContext &Ctx) { 1640 uint32_t Count = readVaruint32(Ctx); 1641 ElemSegments.reserve(Count); 1642 while (Count--) { 1643 wasm::WasmElemSegment Segment; 1644 Segment.Flags = readVaruint32(Ctx); 1645 1646 uint32_t SupportedFlags = wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER | 1647 wasm::WASM_ELEM_SEGMENT_IS_PASSIVE | 1648 wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS; 1649 if (Segment.Flags & ~SupportedFlags) 1650 return make_error<GenericBinaryError>( 1651 "Unsupported flags for element segment", object_error::parse_failed); 1652 1653 wasm::ElemSegmentMode Mode; 1654 if ((Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_PASSIVE) == 0) { 1655 Mode = wasm::ElemSegmentMode::Active; 1656 } else if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_DECLARATIVE) { 1657 Mode = wasm::ElemSegmentMode::Declarative; 1658 } else { 1659 Mode = wasm::ElemSegmentMode::Passive; 1660 } 1661 bool HasTableNumber = 1662 Mode == wasm::ElemSegmentMode::Active && 1663 (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER); 1664 bool HasElemKind = 1665 (Segment.Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_DESC) && 1666 !(Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS); 1667 bool HasElemType = 1668 (Segment.Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_DESC) && 1669 (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS); 1670 bool HasInitExprs = 1671 (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS); 1672 1673 if (HasTableNumber) 1674 Segment.TableNumber = readVaruint32(Ctx); 1675 else 1676 Segment.TableNumber = 0; 1677 1678 if (!isValidTableNumber(Segment.TableNumber)) 1679 return make_error<GenericBinaryError>("invalid TableNumber", 1680 object_error::parse_failed); 1681 1682 if (Mode != wasm::ElemSegmentMode::Active) { 1683 Segment.Offset.Extended = false; 1684 Segment.Offset.Inst.Opcode = wasm::WASM_OPCODE_I32_CONST; 1685 Segment.Offset.Inst.Value.Int32 = 0; 1686 } else { 1687 if (Error Err = readInitExpr(Segment.Offset, Ctx)) 1688 return Err; 1689 } 1690 1691 if (HasElemKind) { 1692 auto ElemKind = readVaruint32(Ctx); 1693 if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS) { 1694 Segment.ElemKind = parseValType(Ctx, ElemKind); 1695 if (Segment.ElemKind != wasm::ValType::FUNCREF && 1696 Segment.ElemKind != wasm::ValType::EXTERNREF && 1697 Segment.ElemKind != wasm::ValType::EXNREF && 1698 Segment.ElemKind != wasm::ValType::OTHERREF) { 1699 return make_error<GenericBinaryError>("invalid elem type", 1700 object_error::parse_failed); 1701 } 1702 } else { 1703 if (ElemKind != 0) 1704 return make_error<GenericBinaryError>("invalid elem type", 1705 object_error::parse_failed); 1706 Segment.ElemKind = wasm::ValType::FUNCREF; 1707 } 1708 } else if (HasElemType) { 1709 auto ElemType = parseValType(Ctx, readVaruint32(Ctx)); 1710 Segment.ElemKind = ElemType; 1711 } else { 1712 Segment.ElemKind = wasm::ValType::FUNCREF; 1713 } 1714 1715 uint32_t NumElems = readVaruint32(Ctx); 1716 1717 if (HasInitExprs) { 1718 while (NumElems--) { 1719 wasm::WasmInitExpr Expr; 1720 if (Error Err = readInitExpr(Expr, Ctx)) 1721 return Err; 1722 } 1723 } else { 1724 while (NumElems--) { 1725 Segment.Functions.push_back(readVaruint32(Ctx)); 1726 } 1727 } 1728 ElemSegments.push_back(Segment); 1729 } 1730 if (Ctx.Ptr != Ctx.End) 1731 return make_error<GenericBinaryError>("elem section ended prematurely", 1732 object_error::parse_failed); 1733 return Error::success(); 1734 } 1735 1736 Error WasmObjectFile::parseDataSection(ReadContext &Ctx) { 1737 DataSection = Sections.size(); 1738 uint32_t Count = readVaruint32(Ctx); 1739 if (DataCount && Count != *DataCount) 1740 return make_error<GenericBinaryError>( 1741 "number of data segments does not match DataCount section"); 1742 DataSegments.reserve(Count); 1743 while (Count--) { 1744 WasmSegment Segment; 1745 Segment.Data.InitFlags = readVaruint32(Ctx); 1746 Segment.Data.MemoryIndex = 1747 (Segment.Data.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX) 1748 ? readVaruint32(Ctx) 1749 : 0; 1750 if ((Segment.Data.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) { 1751 if (Error Err = readInitExpr(Segment.Data.Offset, Ctx)) 1752 return Err; 1753 } else { 1754 Segment.Data.Offset.Extended = false; 1755 Segment.Data.Offset.Inst.Opcode = wasm::WASM_OPCODE_I32_CONST; 1756 Segment.Data.Offset.Inst.Value.Int32 = 0; 1757 } 1758 uint32_t Size = readVaruint32(Ctx); 1759 if (Size > (size_t)(Ctx.End - Ctx.Ptr)) 1760 return make_error<GenericBinaryError>("invalid segment size", 1761 object_error::parse_failed); 1762 Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size); 1763 // The rest of these Data fields are set later, when reading in the linking 1764 // metadata section. 1765 Segment.Data.Alignment = 0; 1766 Segment.Data.LinkingFlags = 0; 1767 Segment.Data.Comdat = UINT32_MAX; 1768 Segment.SectionOffset = Ctx.Ptr - Ctx.Start; 1769 Ctx.Ptr += Size; 1770 DataSegments.push_back(Segment); 1771 } 1772 if (Ctx.Ptr != Ctx.End) 1773 return make_error<GenericBinaryError>("data section ended prematurely", 1774 object_error::parse_failed); 1775 return Error::success(); 1776 } 1777 1778 Error WasmObjectFile::parseDataCountSection(ReadContext &Ctx) { 1779 DataCount = readVaruint32(Ctx); 1780 return Error::success(); 1781 } 1782 1783 const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const { 1784 return Header; 1785 } 1786 1787 void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.b++; } 1788 1789 Expected<uint32_t> WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const { 1790 uint32_t Result = SymbolRef::SF_None; 1791 const WasmSymbol &Sym = getWasmSymbol(Symb); 1792 1793 LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n"); 1794 if (Sym.isBindingWeak()) 1795 Result |= SymbolRef::SF_Weak; 1796 if (!Sym.isBindingLocal()) 1797 Result |= SymbolRef::SF_Global; 1798 if (Sym.isHidden()) 1799 Result |= SymbolRef::SF_Hidden; 1800 if (!Sym.isDefined()) 1801 Result |= SymbolRef::SF_Undefined; 1802 if (Sym.isTypeFunction()) 1803 Result |= SymbolRef::SF_Executable; 1804 return Result; 1805 } 1806 1807 basic_symbol_iterator WasmObjectFile::symbol_begin() const { 1808 DataRefImpl Ref; 1809 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null 1810 Ref.d.b = 0; // Symbol index 1811 return BasicSymbolRef(Ref, this); 1812 } 1813 1814 basic_symbol_iterator WasmObjectFile::symbol_end() const { 1815 DataRefImpl Ref; 1816 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null 1817 Ref.d.b = Symbols.size(); // Symbol index 1818 return BasicSymbolRef(Ref, this); 1819 } 1820 1821 const WasmSymbol &WasmObjectFile::getWasmSymbol(const DataRefImpl &Symb) const { 1822 return Symbols[Symb.d.b]; 1823 } 1824 1825 const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const { 1826 return getWasmSymbol(Symb.getRawDataRefImpl()); 1827 } 1828 1829 Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const { 1830 return getWasmSymbol(Symb).Info.Name; 1831 } 1832 1833 Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const { 1834 auto &Sym = getWasmSymbol(Symb); 1835 if (!Sym.isDefined()) 1836 return 0; 1837 Expected<section_iterator> Sec = getSymbolSection(Symb); 1838 if (!Sec) 1839 return Sec.takeError(); 1840 uint32_t SectionAddress = getSectionAddress(Sec.get()->getRawDataRefImpl()); 1841 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION && 1842 isDefinedFunctionIndex(Sym.Info.ElementIndex)) { 1843 return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset + 1844 SectionAddress; 1845 } 1846 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL && 1847 isDefinedGlobalIndex(Sym.Info.ElementIndex)) { 1848 return getDefinedGlobal(Sym.Info.ElementIndex).Offset + SectionAddress; 1849 } 1850 1851 return getSymbolValue(Symb); 1852 } 1853 1854 uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol &Sym) const { 1855 switch (Sym.Info.Kind) { 1856 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1857 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1858 case wasm::WASM_SYMBOL_TYPE_TAG: 1859 case wasm::WASM_SYMBOL_TYPE_TABLE: 1860 return Sym.Info.ElementIndex; 1861 case wasm::WASM_SYMBOL_TYPE_DATA: { 1862 // The value of a data symbol is the segment offset, plus the symbol 1863 // offset within the segment. 1864 uint32_t SegmentIndex = Sym.Info.DataRef.Segment; 1865 const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data; 1866 if (Segment.Offset.Extended) { 1867 llvm_unreachable("extended init exprs not supported"); 1868 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_I32_CONST) { 1869 return Segment.Offset.Inst.Value.Int32 + Sym.Info.DataRef.Offset; 1870 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_I64_CONST) { 1871 return Segment.Offset.Inst.Value.Int64 + Sym.Info.DataRef.Offset; 1872 } else if (Segment.Offset.Inst.Opcode == wasm::WASM_OPCODE_GLOBAL_GET) { 1873 return Sym.Info.DataRef.Offset; 1874 } else { 1875 llvm_unreachable("unknown init expr opcode"); 1876 } 1877 } 1878 case wasm::WASM_SYMBOL_TYPE_SECTION: 1879 return 0; 1880 } 1881 llvm_unreachable("invalid symbol type"); 1882 } 1883 1884 uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const { 1885 return getWasmSymbolValue(getWasmSymbol(Symb)); 1886 } 1887 1888 uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const { 1889 llvm_unreachable("not yet implemented"); 1890 return 0; 1891 } 1892 1893 uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const { 1894 llvm_unreachable("not yet implemented"); 1895 return 0; 1896 } 1897 1898 Expected<SymbolRef::Type> 1899 WasmObjectFile::getSymbolType(DataRefImpl Symb) const { 1900 const WasmSymbol &Sym = getWasmSymbol(Symb); 1901 1902 switch (Sym.Info.Kind) { 1903 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1904 return SymbolRef::ST_Function; 1905 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1906 return SymbolRef::ST_Other; 1907 case wasm::WASM_SYMBOL_TYPE_DATA: 1908 return SymbolRef::ST_Data; 1909 case wasm::WASM_SYMBOL_TYPE_SECTION: 1910 return SymbolRef::ST_Debug; 1911 case wasm::WASM_SYMBOL_TYPE_TAG: 1912 return SymbolRef::ST_Other; 1913 case wasm::WASM_SYMBOL_TYPE_TABLE: 1914 return SymbolRef::ST_Other; 1915 } 1916 1917 llvm_unreachable("unknown WasmSymbol::SymbolType"); 1918 return SymbolRef::ST_Other; 1919 } 1920 1921 Expected<section_iterator> 1922 WasmObjectFile::getSymbolSection(DataRefImpl Symb) const { 1923 const WasmSymbol &Sym = getWasmSymbol(Symb); 1924 if (Sym.isUndefined()) 1925 return section_end(); 1926 1927 DataRefImpl Ref; 1928 Ref.d.a = getSymbolSectionIdImpl(Sym); 1929 return section_iterator(SectionRef(Ref, this)); 1930 } 1931 1932 uint32_t WasmObjectFile::getSymbolSectionId(SymbolRef Symb) const { 1933 const WasmSymbol &Sym = getWasmSymbol(Symb); 1934 return getSymbolSectionIdImpl(Sym); 1935 } 1936 1937 uint32_t WasmObjectFile::getSymbolSectionIdImpl(const WasmSymbol &Sym) const { 1938 switch (Sym.Info.Kind) { 1939 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1940 return CodeSection; 1941 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1942 return GlobalSection; 1943 case wasm::WASM_SYMBOL_TYPE_DATA: 1944 return DataSection; 1945 case wasm::WASM_SYMBOL_TYPE_SECTION: 1946 return Sym.Info.ElementIndex; 1947 case wasm::WASM_SYMBOL_TYPE_TAG: 1948 return TagSection; 1949 case wasm::WASM_SYMBOL_TYPE_TABLE: 1950 return TableSection; 1951 default: 1952 llvm_unreachable("unknown WasmSymbol::SymbolType"); 1953 } 1954 } 1955 1956 uint32_t WasmObjectFile::getSymbolSize(SymbolRef Symb) const { 1957 const WasmSymbol &Sym = getWasmSymbol(Symb); 1958 if (!Sym.isDefined()) 1959 return 0; 1960 if (Sym.isTypeGlobal()) 1961 return getDefinedGlobal(Sym.Info.ElementIndex).Size; 1962 if (Sym.isTypeData()) 1963 return Sym.Info.DataRef.Size; 1964 if (Sym.isTypeFunction()) 1965 return functions()[Sym.Info.ElementIndex - getNumImportedFunctions()].Size; 1966 // Currently symbol size is only tracked for data segments and functions. In 1967 // principle we could also track size (e.g. binary size) for tables, globals 1968 // and element segments etc too. 1969 return 0; 1970 } 1971 1972 void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; } 1973 1974 Expected<StringRef> WasmObjectFile::getSectionName(DataRefImpl Sec) const { 1975 const WasmSection &S = Sections[Sec.d.a]; 1976 if (S.Type == wasm::WASM_SEC_CUSTOM) 1977 return S.Name; 1978 if (S.Type > wasm::WASM_SEC_LAST_KNOWN) 1979 return createStringError(object_error::invalid_section_index, ""); 1980 return wasm::sectionTypeToString(S.Type); 1981 } 1982 1983 uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { 1984 // For object files, use 0 for section addresses, and section offsets for 1985 // symbol addresses. For linked files, use file offsets. 1986 // See also getSymbolAddress. 1987 return isRelocatableObject() || isSharedObject() ? 0 1988 : Sections[Sec.d.a].Offset; 1989 } 1990 1991 uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const { 1992 return Sec.d.a; 1993 } 1994 1995 uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const { 1996 const WasmSection &S = Sections[Sec.d.a]; 1997 return S.Content.size(); 1998 } 1999 2000 Expected<ArrayRef<uint8_t>> 2001 WasmObjectFile::getSectionContents(DataRefImpl Sec) const { 2002 const WasmSection &S = Sections[Sec.d.a]; 2003 // This will never fail since wasm sections can never be empty (user-sections 2004 // must have a name and non-user sections each have a defined structure). 2005 return S.Content; 2006 } 2007 2008 uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const { 2009 return 1; 2010 } 2011 2012 bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const { 2013 return false; 2014 } 2015 2016 bool WasmObjectFile::isSectionText(DataRefImpl Sec) const { 2017 return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE; 2018 } 2019 2020 bool WasmObjectFile::isSectionData(DataRefImpl Sec) const { 2021 return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA; 2022 } 2023 2024 bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; } 2025 2026 bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; } 2027 2028 relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const { 2029 DataRefImpl RelocRef; 2030 RelocRef.d.a = Ref.d.a; 2031 RelocRef.d.b = 0; 2032 return relocation_iterator(RelocationRef(RelocRef, this)); 2033 } 2034 2035 relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const { 2036 const WasmSection &Sec = getWasmSection(Ref); 2037 DataRefImpl RelocRef; 2038 RelocRef.d.a = Ref.d.a; 2039 RelocRef.d.b = Sec.Relocations.size(); 2040 return relocation_iterator(RelocationRef(RelocRef, this)); 2041 } 2042 2043 void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const { Rel.d.b++; } 2044 2045 uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const { 2046 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 2047 return Rel.Offset; 2048 } 2049 2050 symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Ref) const { 2051 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 2052 if (Rel.Type == wasm::R_WASM_TYPE_INDEX_LEB) 2053 return symbol_end(); 2054 DataRefImpl Sym; 2055 Sym.d.a = 1; 2056 Sym.d.b = Rel.Index; 2057 return symbol_iterator(SymbolRef(Sym, this)); 2058 } 2059 2060 uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const { 2061 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 2062 return Rel.Type; 2063 } 2064 2065 void WasmObjectFile::getRelocationTypeName( 2066 DataRefImpl Ref, SmallVectorImpl<char> &Result) const { 2067 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 2068 StringRef Res = "Unknown"; 2069 2070 #define WASM_RELOC(name, value) \ 2071 case wasm::name: \ 2072 Res = #name; \ 2073 break; 2074 2075 switch (Rel.Type) { 2076 #include "llvm/BinaryFormat/WasmRelocs.def" 2077 } 2078 2079 #undef WASM_RELOC 2080 2081 Result.append(Res.begin(), Res.end()); 2082 } 2083 2084 section_iterator WasmObjectFile::section_begin() const { 2085 DataRefImpl Ref; 2086 Ref.d.a = 0; 2087 return section_iterator(SectionRef(Ref, this)); 2088 } 2089 2090 section_iterator WasmObjectFile::section_end() const { 2091 DataRefImpl Ref; 2092 Ref.d.a = Sections.size(); 2093 return section_iterator(SectionRef(Ref, this)); 2094 } 2095 2096 uint8_t WasmObjectFile::getBytesInAddress() const { 2097 return HasMemory64 ? 8 : 4; 2098 } 2099 2100 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; } 2101 2102 Triple::ArchType WasmObjectFile::getArch() const { 2103 return HasMemory64 ? Triple::wasm64 : Triple::wasm32; 2104 } 2105 2106 Expected<SubtargetFeatures> WasmObjectFile::getFeatures() const { 2107 return SubtargetFeatures(); 2108 } 2109 2110 bool WasmObjectFile::isRelocatableObject() const { return HasLinkingSection; } 2111 2112 bool WasmObjectFile::isSharedObject() const { return HasDylinkSection; } 2113 2114 const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const { 2115 assert(Ref.d.a < Sections.size()); 2116 return Sections[Ref.d.a]; 2117 } 2118 2119 const WasmSection & 2120 WasmObjectFile::getWasmSection(const SectionRef &Section) const { 2121 return getWasmSection(Section.getRawDataRefImpl()); 2122 } 2123 2124 const wasm::WasmRelocation & 2125 WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const { 2126 return getWasmRelocation(Ref.getRawDataRefImpl()); 2127 } 2128 2129 const wasm::WasmRelocation & 2130 WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const { 2131 assert(Ref.d.a < Sections.size()); 2132 const WasmSection &Sec = Sections[Ref.d.a]; 2133 assert(Ref.d.b < Sec.Relocations.size()); 2134 return Sec.Relocations[Ref.d.b]; 2135 } 2136 2137 int WasmSectionOrderChecker::getSectionOrder(unsigned ID, 2138 StringRef CustomSectionName) { 2139 switch (ID) { 2140 case wasm::WASM_SEC_CUSTOM: 2141 return StringSwitch<unsigned>(CustomSectionName) 2142 .Case("dylink", WASM_SEC_ORDER_DYLINK) 2143 .Case("dylink.0", WASM_SEC_ORDER_DYLINK) 2144 .Case("linking", WASM_SEC_ORDER_LINKING) 2145 .StartsWith("reloc.", WASM_SEC_ORDER_RELOC) 2146 .Case("name", WASM_SEC_ORDER_NAME) 2147 .Case("producers", WASM_SEC_ORDER_PRODUCERS) 2148 .Case("target_features", WASM_SEC_ORDER_TARGET_FEATURES) 2149 .Default(WASM_SEC_ORDER_NONE); 2150 case wasm::WASM_SEC_TYPE: 2151 return WASM_SEC_ORDER_TYPE; 2152 case wasm::WASM_SEC_IMPORT: 2153 return WASM_SEC_ORDER_IMPORT; 2154 case wasm::WASM_SEC_FUNCTION: 2155 return WASM_SEC_ORDER_FUNCTION; 2156 case wasm::WASM_SEC_TABLE: 2157 return WASM_SEC_ORDER_TABLE; 2158 case wasm::WASM_SEC_MEMORY: 2159 return WASM_SEC_ORDER_MEMORY; 2160 case wasm::WASM_SEC_GLOBAL: 2161 return WASM_SEC_ORDER_GLOBAL; 2162 case wasm::WASM_SEC_EXPORT: 2163 return WASM_SEC_ORDER_EXPORT; 2164 case wasm::WASM_SEC_START: 2165 return WASM_SEC_ORDER_START; 2166 case wasm::WASM_SEC_ELEM: 2167 return WASM_SEC_ORDER_ELEM; 2168 case wasm::WASM_SEC_CODE: 2169 return WASM_SEC_ORDER_CODE; 2170 case wasm::WASM_SEC_DATA: 2171 return WASM_SEC_ORDER_DATA; 2172 case wasm::WASM_SEC_DATACOUNT: 2173 return WASM_SEC_ORDER_DATACOUNT; 2174 case wasm::WASM_SEC_TAG: 2175 return WASM_SEC_ORDER_TAG; 2176 default: 2177 return WASM_SEC_ORDER_NONE; 2178 } 2179 } 2180 2181 // Represents the edges in a directed graph where any node B reachable from node 2182 // A is not allowed to appear before A in the section ordering, but may appear 2183 // afterward. 2184 int WasmSectionOrderChecker::DisallowedPredecessors 2185 [WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = { 2186 // WASM_SEC_ORDER_NONE 2187 {}, 2188 // WASM_SEC_ORDER_TYPE 2189 {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT}, 2190 // WASM_SEC_ORDER_IMPORT 2191 {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION}, 2192 // WASM_SEC_ORDER_FUNCTION 2193 {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE}, 2194 // WASM_SEC_ORDER_TABLE 2195 {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY}, 2196 // WASM_SEC_ORDER_MEMORY 2197 {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_TAG}, 2198 // WASM_SEC_ORDER_TAG 2199 {WASM_SEC_ORDER_TAG, WASM_SEC_ORDER_GLOBAL}, 2200 // WASM_SEC_ORDER_GLOBAL 2201 {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EXPORT}, 2202 // WASM_SEC_ORDER_EXPORT 2203 {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START}, 2204 // WASM_SEC_ORDER_START 2205 {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM}, 2206 // WASM_SEC_ORDER_ELEM 2207 {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT}, 2208 // WASM_SEC_ORDER_DATACOUNT 2209 {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE}, 2210 // WASM_SEC_ORDER_CODE 2211 {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA}, 2212 // WASM_SEC_ORDER_DATA 2213 {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING}, 2214 2215 // Custom Sections 2216 // WASM_SEC_ORDER_DYLINK 2217 {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE}, 2218 // WASM_SEC_ORDER_LINKING 2219 {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME}, 2220 // WASM_SEC_ORDER_RELOC (can be repeated) 2221 {}, 2222 // WASM_SEC_ORDER_NAME 2223 {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS}, 2224 // WASM_SEC_ORDER_PRODUCERS 2225 {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES}, 2226 // WASM_SEC_ORDER_TARGET_FEATURES 2227 {WASM_SEC_ORDER_TARGET_FEATURES}}; 2228 2229 bool WasmSectionOrderChecker::isValidSectionOrder(unsigned ID, 2230 StringRef CustomSectionName) { 2231 int Order = getSectionOrder(ID, CustomSectionName); 2232 if (Order == WASM_SEC_ORDER_NONE) 2233 return true; 2234 2235 // Disallowed predecessors we need to check for 2236 SmallVector<int, WASM_NUM_SEC_ORDERS> WorkList; 2237 2238 // Keep track of completed checks to avoid repeating work 2239 bool Checked[WASM_NUM_SEC_ORDERS] = {}; 2240 2241 int Curr = Order; 2242 while (true) { 2243 // Add new disallowed predecessors to work list 2244 for (size_t I = 0;; ++I) { 2245 int Next = DisallowedPredecessors[Curr][I]; 2246 if (Next == WASM_SEC_ORDER_NONE) 2247 break; 2248 if (Checked[Next]) 2249 continue; 2250 WorkList.push_back(Next); 2251 Checked[Next] = true; 2252 } 2253 2254 if (WorkList.empty()) 2255 break; 2256 2257 // Consider next disallowed predecessor 2258 Curr = WorkList.pop_back_val(); 2259 if (Seen[Curr]) 2260 return false; 2261 } 2262 2263 // Have not seen any disallowed predecessors 2264 Seen[Order] = true; 2265 return true; 2266 } 2267