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/STLExtras.h" 12 #include "llvm/ADT/SmallSet.h" 13 #include "llvm/ADT/StringRef.h" 14 #include "llvm/ADT/StringSet.h" 15 #include "llvm/ADT/Triple.h" 16 #include "llvm/BinaryFormat/Wasm.h" 17 #include "llvm/MC/SubtargetFeature.h" 18 #include "llvm/Object/Binary.h" 19 #include "llvm/Object/Error.h" 20 #include "llvm/Object/ObjectFile.h" 21 #include "llvm/Object/SymbolicFile.h" 22 #include "llvm/Object/Wasm.h" 23 #include "llvm/Support/Endian.h" 24 #include "llvm/Support/Error.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/LEB128.h" 27 #include "llvm/Support/ScopedPrinter.h" 28 #include <algorithm> 29 #include <cassert> 30 #include <cstdint> 31 #include <cstring> 32 #include <system_error> 33 34 #define DEBUG_TYPE "wasm-object" 35 36 using namespace llvm; 37 using namespace object; 38 39 void WasmSymbol::print(raw_ostream &Out) const { 40 Out << "Name=" << Info.Name 41 << ", Kind=" << toString(wasm::WasmSymbolType(Info.Kind)) 42 << ", Flags=" << Info.Flags; 43 if (!isTypeData()) { 44 Out << ", ElemIndex=" << Info.ElementIndex; 45 } else if (isDefined()) { 46 Out << ", Segment=" << Info.DataRef.Segment; 47 Out << ", Offset=" << Info.DataRef.Offset; 48 Out << ", Size=" << Info.DataRef.Size; 49 } 50 } 51 52 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 53 LLVM_DUMP_METHOD void WasmSymbol::dump() const { print(dbgs()); } 54 #endif 55 56 Expected<std::unique_ptr<WasmObjectFile>> 57 ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) { 58 Error Err = Error::success(); 59 auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err); 60 if (Err) 61 return std::move(Err); 62 63 return std::move(ObjectFile); 64 } 65 66 #define VARINT7_MAX ((1 << 7) - 1) 67 #define VARINT7_MIN (-(1 << 7)) 68 #define VARUINT7_MAX (1 << 7) 69 #define VARUINT1_MAX (1) 70 71 static uint8_t readUint8(WasmObjectFile::ReadContext &Ctx) { 72 if (Ctx.Ptr == Ctx.End) 73 report_fatal_error("EOF while reading uint8"); 74 return *Ctx.Ptr++; 75 } 76 77 static uint32_t readUint32(WasmObjectFile::ReadContext &Ctx) { 78 if (Ctx.Ptr + 4 > Ctx.End) 79 report_fatal_error("EOF while reading uint32"); 80 uint32_t Result = support::endian::read32le(Ctx.Ptr); 81 Ctx.Ptr += 4; 82 return Result; 83 } 84 85 static int32_t readFloat32(WasmObjectFile::ReadContext &Ctx) { 86 if (Ctx.Ptr + 4 > Ctx.End) 87 report_fatal_error("EOF while reading float64"); 88 int32_t Result = 0; 89 memcpy(&Result, Ctx.Ptr, sizeof(Result)); 90 Ctx.Ptr += sizeof(Result); 91 return Result; 92 } 93 94 static int64_t readFloat64(WasmObjectFile::ReadContext &Ctx) { 95 if (Ctx.Ptr + 8 > Ctx.End) 96 report_fatal_error("EOF while reading float64"); 97 int64_t Result = 0; 98 memcpy(&Result, Ctx.Ptr, sizeof(Result)); 99 Ctx.Ptr += sizeof(Result); 100 return Result; 101 } 102 103 static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx) { 104 unsigned Count; 105 const char *Error = nullptr; 106 uint64_t Result = decodeULEB128(Ctx.Ptr, &Count, Ctx.End, &Error); 107 if (Error) 108 report_fatal_error(Error); 109 Ctx.Ptr += Count; 110 return Result; 111 } 112 113 static StringRef readString(WasmObjectFile::ReadContext &Ctx) { 114 uint32_t StringLen = readULEB128(Ctx); 115 if (Ctx.Ptr + StringLen > Ctx.End) 116 report_fatal_error("EOF while reading string"); 117 StringRef Return = 118 StringRef(reinterpret_cast<const char *>(Ctx.Ptr), StringLen); 119 Ctx.Ptr += StringLen; 120 return Return; 121 } 122 123 static int64_t readLEB128(WasmObjectFile::ReadContext &Ctx) { 124 unsigned Count; 125 const char *Error = nullptr; 126 uint64_t Result = decodeSLEB128(Ctx.Ptr, &Count, Ctx.End, &Error); 127 if (Error) 128 report_fatal_error(Error); 129 Ctx.Ptr += Count; 130 return Result; 131 } 132 133 static uint8_t readVaruint1(WasmObjectFile::ReadContext &Ctx) { 134 int64_t Result = readLEB128(Ctx); 135 if (Result > VARUINT1_MAX || Result < 0) 136 report_fatal_error("LEB is outside Varuint1 range"); 137 return Result; 138 } 139 140 static int32_t readVarint32(WasmObjectFile::ReadContext &Ctx) { 141 int64_t Result = readLEB128(Ctx); 142 if (Result > INT32_MAX || Result < INT32_MIN) 143 report_fatal_error("LEB is outside Varint32 range"); 144 return Result; 145 } 146 147 static uint32_t readVaruint32(WasmObjectFile::ReadContext &Ctx) { 148 uint64_t Result = readULEB128(Ctx); 149 if (Result > UINT32_MAX) 150 report_fatal_error("LEB is outside Varuint32 range"); 151 return Result; 152 } 153 154 static int64_t readVarint64(WasmObjectFile::ReadContext &Ctx) { 155 return readLEB128(Ctx); 156 } 157 158 static uint8_t readOpcode(WasmObjectFile::ReadContext &Ctx) { 159 return readUint8(Ctx); 160 } 161 162 static Error readInitExpr(wasm::WasmInitExpr &Expr, 163 WasmObjectFile::ReadContext &Ctx) { 164 Expr.Opcode = readOpcode(Ctx); 165 166 switch (Expr.Opcode) { 167 case wasm::WASM_OPCODE_I32_CONST: 168 Expr.Value.Int32 = readVarint32(Ctx); 169 break; 170 case wasm::WASM_OPCODE_I64_CONST: 171 Expr.Value.Int64 = readVarint64(Ctx); 172 break; 173 case wasm::WASM_OPCODE_F32_CONST: 174 Expr.Value.Float32 = readFloat32(Ctx); 175 break; 176 case wasm::WASM_OPCODE_F64_CONST: 177 Expr.Value.Float64 = readFloat64(Ctx); 178 break; 179 case wasm::WASM_OPCODE_GLOBAL_GET: 180 Expr.Value.Global = readULEB128(Ctx); 181 break; 182 default: 183 return make_error<GenericBinaryError>("Invalid opcode in init_expr", 184 object_error::parse_failed); 185 } 186 187 uint8_t EndOpcode = readOpcode(Ctx); 188 if (EndOpcode != wasm::WASM_OPCODE_END) { 189 return make_error<GenericBinaryError>("Invalid init_expr", 190 object_error::parse_failed); 191 } 192 return Error::success(); 193 } 194 195 static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) { 196 wasm::WasmLimits Result; 197 Result.Flags = readVaruint32(Ctx); 198 Result.Initial = readVaruint32(Ctx); 199 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) 200 Result.Maximum = readVaruint32(Ctx); 201 return Result; 202 } 203 204 static wasm::WasmTable readTable(WasmObjectFile::ReadContext &Ctx) { 205 wasm::WasmTable Table; 206 Table.ElemType = readUint8(Ctx); 207 Table.Limits = readLimits(Ctx); 208 return Table; 209 } 210 211 static Error readSection(WasmSection &Section, WasmObjectFile::ReadContext &Ctx, 212 WasmSectionOrderChecker &Checker) { 213 Section.Offset = Ctx.Ptr - Ctx.Start; 214 Section.Type = readUint8(Ctx); 215 LLVM_DEBUG(dbgs() << "readSection type=" << Section.Type << "\n"); 216 uint32_t Size = readVaruint32(Ctx); 217 if (Size == 0) 218 return make_error<StringError>("Zero length section", 219 object_error::parse_failed); 220 if (Ctx.Ptr + Size > Ctx.End) 221 return make_error<StringError>("Section too large", 222 object_error::parse_failed); 223 if (Section.Type == wasm::WASM_SEC_CUSTOM) { 224 WasmObjectFile::ReadContext SectionCtx; 225 SectionCtx.Start = Ctx.Ptr; 226 SectionCtx.Ptr = Ctx.Ptr; 227 SectionCtx.End = Ctx.Ptr + Size; 228 229 Section.Name = readString(SectionCtx); 230 231 uint32_t SectionNameSize = SectionCtx.Ptr - SectionCtx.Start; 232 Ctx.Ptr += SectionNameSize; 233 Size -= SectionNameSize; 234 } 235 236 if (!Checker.isValidSectionOrder(Section.Type, Section.Name)) { 237 return make_error<StringError>("Out of order section type: " + 238 llvm::to_string(Section.Type), 239 object_error::parse_failed); 240 } 241 242 Section.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size); 243 Ctx.Ptr += Size; 244 return Error::success(); 245 } 246 247 WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err) 248 : ObjectFile(Binary::ID_Wasm, Buffer) { 249 ErrorAsOutParameter ErrAsOutParam(&Err); 250 Header.Magic = getData().substr(0, 4); 251 if (Header.Magic != StringRef("\0asm", 4)) { 252 Err = 253 make_error<StringError>("Bad magic number", object_error::parse_failed); 254 return; 255 } 256 257 ReadContext Ctx; 258 Ctx.Start = getData().bytes_begin(); 259 Ctx.Ptr = Ctx.Start + 4; 260 Ctx.End = Ctx.Start + getData().size(); 261 262 if (Ctx.Ptr + 4 > Ctx.End) { 263 Err = make_error<StringError>("Missing version number", 264 object_error::parse_failed); 265 return; 266 } 267 268 Header.Version = readUint32(Ctx); 269 if (Header.Version != wasm::WasmVersion) { 270 Err = make_error<StringError>("Bad version number", 271 object_error::parse_failed); 272 return; 273 } 274 275 WasmSection Sec; 276 WasmSectionOrderChecker Checker; 277 while (Ctx.Ptr < Ctx.End) { 278 if ((Err = readSection(Sec, Ctx, Checker))) 279 return; 280 if ((Err = parseSection(Sec))) 281 return; 282 283 Sections.push_back(Sec); 284 } 285 } 286 287 Error WasmObjectFile::parseSection(WasmSection &Sec) { 288 ReadContext Ctx; 289 Ctx.Start = Sec.Content.data(); 290 Ctx.End = Ctx.Start + Sec.Content.size(); 291 Ctx.Ptr = Ctx.Start; 292 switch (Sec.Type) { 293 case wasm::WASM_SEC_CUSTOM: 294 return parseCustomSection(Sec, Ctx); 295 case wasm::WASM_SEC_TYPE: 296 return parseTypeSection(Ctx); 297 case wasm::WASM_SEC_IMPORT: 298 return parseImportSection(Ctx); 299 case wasm::WASM_SEC_FUNCTION: 300 return parseFunctionSection(Ctx); 301 case wasm::WASM_SEC_TABLE: 302 return parseTableSection(Ctx); 303 case wasm::WASM_SEC_MEMORY: 304 return parseMemorySection(Ctx); 305 case wasm::WASM_SEC_GLOBAL: 306 return parseGlobalSection(Ctx); 307 case wasm::WASM_SEC_EVENT: 308 return parseEventSection(Ctx); 309 case wasm::WASM_SEC_EXPORT: 310 return parseExportSection(Ctx); 311 case wasm::WASM_SEC_START: 312 return parseStartSection(Ctx); 313 case wasm::WASM_SEC_ELEM: 314 return parseElemSection(Ctx); 315 case wasm::WASM_SEC_CODE: 316 return parseCodeSection(Ctx); 317 case wasm::WASM_SEC_DATA: 318 return parseDataSection(Ctx); 319 default: 320 return make_error<GenericBinaryError>("Bad section type", 321 object_error::parse_failed); 322 } 323 } 324 325 Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) { 326 // See https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md 327 HasDylinkSection = true; 328 DylinkInfo.MemorySize = readVaruint32(Ctx); 329 DylinkInfo.MemoryAlignment = readVaruint32(Ctx); 330 DylinkInfo.TableSize = readVaruint32(Ctx); 331 DylinkInfo.TableAlignment = readVaruint32(Ctx); 332 uint32_t Count = readVaruint32(Ctx); 333 while (Count--) { 334 DylinkInfo.Needed.push_back(readString(Ctx)); 335 } 336 if (Ctx.Ptr != Ctx.End) 337 return make_error<GenericBinaryError>("dylink section ended prematurely", 338 object_error::parse_failed); 339 return Error::success(); 340 } 341 342 Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { 343 llvm::DenseSet<uint64_t> Seen; 344 if (Functions.size() != FunctionTypes.size()) { 345 return make_error<GenericBinaryError>("Names must come after code section", 346 object_error::parse_failed); 347 } 348 349 while (Ctx.Ptr < Ctx.End) { 350 uint8_t Type = readUint8(Ctx); 351 uint32_t Size = readVaruint32(Ctx); 352 const uint8_t *SubSectionEnd = Ctx.Ptr + Size; 353 switch (Type) { 354 case wasm::WASM_NAMES_FUNCTION: { 355 uint32_t Count = readVaruint32(Ctx); 356 while (Count--) { 357 uint32_t Index = readVaruint32(Ctx); 358 if (!Seen.insert(Index).second) 359 return make_error<GenericBinaryError>("Function named more than once", 360 object_error::parse_failed); 361 StringRef Name = readString(Ctx); 362 if (!isValidFunctionIndex(Index) || Name.empty()) 363 return make_error<GenericBinaryError>("Invalid name entry", 364 object_error::parse_failed); 365 DebugNames.push_back(wasm::WasmFunctionName{Index, Name}); 366 if (isDefinedFunctionIndex(Index)) 367 getDefinedFunction(Index).DebugName = Name; 368 } 369 break; 370 } 371 // Ignore local names for now 372 case wasm::WASM_NAMES_LOCAL: 373 default: 374 Ctx.Ptr += Size; 375 break; 376 } 377 if (Ctx.Ptr != SubSectionEnd) 378 return make_error<GenericBinaryError>( 379 "Name sub-section ended prematurely", object_error::parse_failed); 380 } 381 382 if (Ctx.Ptr != Ctx.End) 383 return make_error<GenericBinaryError>("Name section ended prematurely", 384 object_error::parse_failed); 385 return Error::success(); 386 } 387 388 Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) { 389 HasLinkingSection = true; 390 if (Functions.size() != FunctionTypes.size()) { 391 return make_error<GenericBinaryError>( 392 "Linking data must come after code section", 393 object_error::parse_failed); 394 } 395 396 LinkingData.Version = readVaruint32(Ctx); 397 if (LinkingData.Version != wasm::WasmMetadataVersion) { 398 return make_error<GenericBinaryError>( 399 "Unexpected metadata version: " + Twine(LinkingData.Version) + 400 " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")", 401 object_error::parse_failed); 402 } 403 404 const uint8_t *OrigEnd = Ctx.End; 405 while (Ctx.Ptr < OrigEnd) { 406 Ctx.End = OrigEnd; 407 uint8_t Type = readUint8(Ctx); 408 uint32_t Size = readVaruint32(Ctx); 409 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size 410 << "\n"); 411 Ctx.End = Ctx.Ptr + Size; 412 switch (Type) { 413 case wasm::WASM_SYMBOL_TABLE: 414 if (Error Err = parseLinkingSectionSymtab(Ctx)) 415 return Err; 416 break; 417 case wasm::WASM_SEGMENT_INFO: { 418 uint32_t Count = readVaruint32(Ctx); 419 if (Count > DataSegments.size()) 420 return make_error<GenericBinaryError>("Too many segment names", 421 object_error::parse_failed); 422 for (uint32_t I = 0; I < Count; I++) { 423 DataSegments[I].Data.Name = readString(Ctx); 424 DataSegments[I].Data.Alignment = readVaruint32(Ctx); 425 DataSegments[I].Data.LinkerFlags = readVaruint32(Ctx); 426 } 427 break; 428 } 429 case wasm::WASM_INIT_FUNCS: { 430 uint32_t Count = readVaruint32(Ctx); 431 LinkingData.InitFunctions.reserve(Count); 432 for (uint32_t I = 0; I < Count; I++) { 433 wasm::WasmInitFunc Init; 434 Init.Priority = readVaruint32(Ctx); 435 Init.Symbol = readVaruint32(Ctx); 436 if (!isValidFunctionSymbol(Init.Symbol)) 437 return make_error<GenericBinaryError>("Invalid function symbol: " + 438 Twine(Init.Symbol), 439 object_error::parse_failed); 440 LinkingData.InitFunctions.emplace_back(Init); 441 } 442 break; 443 } 444 case wasm::WASM_COMDAT_INFO: 445 if (Error Err = parseLinkingSectionComdat(Ctx)) 446 return Err; 447 break; 448 default: 449 Ctx.Ptr += Size; 450 break; 451 } 452 if (Ctx.Ptr != Ctx.End) 453 return make_error<GenericBinaryError>( 454 "Linking sub-section ended prematurely", object_error::parse_failed); 455 } 456 if (Ctx.Ptr != OrigEnd) 457 return make_error<GenericBinaryError>("Linking section ended prematurely", 458 object_error::parse_failed); 459 return Error::success(); 460 } 461 462 Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { 463 uint32_t Count = readVaruint32(Ctx); 464 LinkingData.SymbolTable.reserve(Count); 465 Symbols.reserve(Count); 466 StringSet<> SymbolNames; 467 468 std::vector<wasm::WasmImport *> ImportedGlobals; 469 std::vector<wasm::WasmImport *> ImportedFunctions; 470 std::vector<wasm::WasmImport *> ImportedEvents; 471 ImportedGlobals.reserve(Imports.size()); 472 ImportedFunctions.reserve(Imports.size()); 473 ImportedEvents.reserve(Imports.size()); 474 for (auto &I : Imports) { 475 if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION) 476 ImportedFunctions.emplace_back(&I); 477 else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL) 478 ImportedGlobals.emplace_back(&I); 479 else if (I.Kind == wasm::WASM_EXTERNAL_EVENT) 480 ImportedEvents.emplace_back(&I); 481 } 482 483 while (Count--) { 484 wasm::WasmSymbolInfo Info; 485 const wasm::WasmSignature *Signature = nullptr; 486 const wasm::WasmGlobalType *GlobalType = nullptr; 487 const wasm::WasmEventType *EventType = nullptr; 488 489 Info.Kind = readUint8(Ctx); 490 Info.Flags = readVaruint32(Ctx); 491 bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0; 492 493 switch (Info.Kind) { 494 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 495 Info.ElementIndex = readVaruint32(Ctx); 496 if (!isValidFunctionIndex(Info.ElementIndex) || 497 IsDefined != isDefinedFunctionIndex(Info.ElementIndex)) 498 return make_error<GenericBinaryError>("invalid function symbol index", 499 object_error::parse_failed); 500 if (IsDefined) { 501 Info.Name = readString(Ctx); 502 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions; 503 Signature = &Signatures[FunctionTypes[FuncIndex]]; 504 wasm::WasmFunction &Function = Functions[FuncIndex]; 505 if (Function.SymbolName.empty()) 506 Function.SymbolName = Info.Name; 507 } else { 508 wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex]; 509 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) 510 Info.Name = readString(Ctx); 511 else 512 Info.Name = Import.Field; 513 Signature = &Signatures[Import.SigIndex]; 514 Info.ImportName = Import.Field; 515 Info.ImportModule = Import.Module; 516 } 517 break; 518 519 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 520 Info.ElementIndex = readVaruint32(Ctx); 521 if (!isValidGlobalIndex(Info.ElementIndex) || 522 IsDefined != isDefinedGlobalIndex(Info.ElementIndex)) 523 return make_error<GenericBinaryError>("invalid global symbol index", 524 object_error::parse_failed); 525 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == 526 wasm::WASM_SYMBOL_BINDING_WEAK) 527 return make_error<GenericBinaryError>("undefined weak global symbol", 528 object_error::parse_failed); 529 if (IsDefined) { 530 Info.Name = readString(Ctx); 531 unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals; 532 wasm::WasmGlobal &Global = Globals[GlobalIndex]; 533 GlobalType = &Global.Type; 534 if (Global.SymbolName.empty()) 535 Global.SymbolName = Info.Name; 536 } else { 537 wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex]; 538 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) 539 Info.Name = readString(Ctx); 540 else 541 Info.Name = Import.Field; 542 GlobalType = &Import.Global; 543 Info.ImportName = Import.Field; 544 Info.ImportModule = Import.Module; 545 } 546 break; 547 548 case wasm::WASM_SYMBOL_TYPE_DATA: 549 Info.Name = readString(Ctx); 550 if (IsDefined) { 551 uint32_t Index = readVaruint32(Ctx); 552 if (Index >= DataSegments.size()) 553 return make_error<GenericBinaryError>("invalid data symbol index", 554 object_error::parse_failed); 555 uint32_t Offset = readVaruint32(Ctx); 556 uint32_t Size = readVaruint32(Ctx); 557 if (Offset + Size > DataSegments[Index].Data.Content.size()) 558 return make_error<GenericBinaryError>("invalid data symbol offset", 559 object_error::parse_failed); 560 Info.DataRef = wasm::WasmDataReference{Index, Offset, Size}; 561 } 562 break; 563 564 case wasm::WASM_SYMBOL_TYPE_SECTION: { 565 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != 566 wasm::WASM_SYMBOL_BINDING_LOCAL) 567 return make_error<GenericBinaryError>( 568 "Section symbols must have local binding", 569 object_error::parse_failed); 570 Info.ElementIndex = readVaruint32(Ctx); 571 // Use somewhat unique section name as symbol name. 572 StringRef SectionName = Sections[Info.ElementIndex].Name; 573 Info.Name = SectionName; 574 break; 575 } 576 577 case wasm::WASM_SYMBOL_TYPE_EVENT: { 578 Info.ElementIndex = readVaruint32(Ctx); 579 if (!isValidEventIndex(Info.ElementIndex) || 580 IsDefined != isDefinedEventIndex(Info.ElementIndex)) 581 return make_error<GenericBinaryError>("invalid event symbol index", 582 object_error::parse_failed); 583 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == 584 wasm::WASM_SYMBOL_BINDING_WEAK) 585 return make_error<GenericBinaryError>("undefined weak global symbol", 586 object_error::parse_failed); 587 if (IsDefined) { 588 Info.Name = readString(Ctx); 589 unsigned EventIndex = Info.ElementIndex - NumImportedEvents; 590 wasm::WasmEvent &Event = Events[EventIndex]; 591 Signature = &Signatures[Event.Type.SigIndex]; 592 EventType = &Event.Type; 593 if (Event.SymbolName.empty()) 594 Event.SymbolName = Info.Name; 595 596 } else { 597 wasm::WasmImport &Import = *ImportedEvents[Info.ElementIndex]; 598 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) 599 Info.Name = readString(Ctx); 600 else 601 Info.Name = Import.Field; 602 EventType = &Import.Event; 603 Signature = &Signatures[EventType->SigIndex]; 604 Info.ImportName = Import.Field; 605 Info.ImportModule = Import.Module; 606 } 607 break; 608 } 609 610 default: 611 return make_error<GenericBinaryError>("Invalid symbol type", 612 object_error::parse_failed); 613 } 614 615 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != 616 wasm::WASM_SYMBOL_BINDING_LOCAL && 617 !SymbolNames.insert(Info.Name).second) 618 return make_error<GenericBinaryError>("Duplicate symbol name " + 619 Twine(Info.Name), 620 object_error::parse_failed); 621 LinkingData.SymbolTable.emplace_back(Info); 622 Symbols.emplace_back(LinkingData.SymbolTable.back(), GlobalType, EventType, 623 Signature); 624 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n"); 625 } 626 627 return Error::success(); 628 } 629 630 Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) { 631 uint32_t ComdatCount = readVaruint32(Ctx); 632 StringSet<> ComdatSet; 633 for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) { 634 StringRef Name = readString(Ctx); 635 if (Name.empty() || !ComdatSet.insert(Name).second) 636 return make_error<GenericBinaryError>("Bad/duplicate COMDAT name " + 637 Twine(Name), 638 object_error::parse_failed); 639 LinkingData.Comdats.emplace_back(Name); 640 uint32_t Flags = readVaruint32(Ctx); 641 if (Flags != 0) 642 return make_error<GenericBinaryError>("Unsupported COMDAT flags", 643 object_error::parse_failed); 644 645 uint32_t EntryCount = readVaruint32(Ctx); 646 while (EntryCount--) { 647 unsigned Kind = readVaruint32(Ctx); 648 unsigned Index = readVaruint32(Ctx); 649 switch (Kind) { 650 default: 651 return make_error<GenericBinaryError>("Invalid COMDAT entry type", 652 object_error::parse_failed); 653 case wasm::WASM_COMDAT_DATA: 654 if (Index >= DataSegments.size()) 655 return make_error<GenericBinaryError>( 656 "COMDAT data index out of range", object_error::parse_failed); 657 if (DataSegments[Index].Data.Comdat != UINT32_MAX) 658 return make_error<GenericBinaryError>("Data segment in two COMDATs", 659 object_error::parse_failed); 660 DataSegments[Index].Data.Comdat = ComdatIndex; 661 break; 662 case wasm::WASM_COMDAT_FUNCTION: 663 if (!isDefinedFunctionIndex(Index)) 664 return make_error<GenericBinaryError>( 665 "COMDAT function index out of range", object_error::parse_failed); 666 if (getDefinedFunction(Index).Comdat != UINT32_MAX) 667 return make_error<GenericBinaryError>("Function in two COMDATs", 668 object_error::parse_failed); 669 getDefinedFunction(Index).Comdat = ComdatIndex; 670 break; 671 } 672 } 673 } 674 return Error::success(); 675 } 676 677 Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) { 678 llvm::SmallSet<StringRef, 3> FieldsSeen; 679 uint32_t Fields = readVaruint32(Ctx); 680 for (size_t I = 0; I < Fields; ++I) { 681 StringRef FieldName = readString(Ctx); 682 if (!FieldsSeen.insert(FieldName).second) 683 return make_error<GenericBinaryError>( 684 "Producers section does not have unique fields", 685 object_error::parse_failed); 686 std::vector<std::pair<std::string, std::string>> *ProducerVec = nullptr; 687 if (FieldName == "language") { 688 ProducerVec = &ProducerInfo.Languages; 689 } else if (FieldName == "processed-by") { 690 ProducerVec = &ProducerInfo.Tools; 691 } else if (FieldName == "sdk") { 692 ProducerVec = &ProducerInfo.SDKs; 693 } else { 694 return make_error<GenericBinaryError>( 695 "Producers section field is not named one of language, processed-by, " 696 "or sdk", 697 object_error::parse_failed); 698 } 699 uint32_t ValueCount = readVaruint32(Ctx); 700 llvm::SmallSet<StringRef, 8> ProducersSeen; 701 for (size_t J = 0; J < ValueCount; ++J) { 702 StringRef Name = readString(Ctx); 703 StringRef Version = readString(Ctx); 704 if (!ProducersSeen.insert(Name).second) { 705 return make_error<GenericBinaryError>( 706 "Producers section contains repeated producer", 707 object_error::parse_failed); 708 } 709 ProducerVec->emplace_back(Name, Version); 710 } 711 } 712 if (Ctx.Ptr != Ctx.End) 713 return make_error<GenericBinaryError>("Producers section ended prematurely", 714 object_error::parse_failed); 715 return Error::success(); 716 } 717 718 Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) { 719 llvm::SmallSet<std::string, 8> FeaturesSeen; 720 uint32_t FeatureCount = readVaruint32(Ctx); 721 for (size_t I = 0; I < FeatureCount; ++I) { 722 wasm::WasmFeatureEntry Feature; 723 Feature.Prefix = readUint8(Ctx); 724 switch (Feature.Prefix) { 725 case wasm::WASM_FEATURE_PREFIX_USED: 726 case wasm::WASM_FEATURE_PREFIX_REQUIRED: 727 case wasm::WASM_FEATURE_PREFIX_DISALLOWED: 728 break; 729 default: 730 return make_error<GenericBinaryError>("Unknown feature policy prefix", 731 object_error::parse_failed); 732 } 733 Feature.Name = readString(Ctx); 734 if (!FeaturesSeen.insert(Feature.Name).second) 735 return make_error<GenericBinaryError>( 736 "Target features section contains repeated feature \"" + 737 Feature.Name + "\"", 738 object_error::parse_failed); 739 TargetFeatures.push_back(Feature); 740 } 741 if (Ctx.Ptr != Ctx.End) 742 return make_error<GenericBinaryError>( 743 "Target features section ended prematurely", 744 object_error::parse_failed); 745 return Error::success(); 746 } 747 748 Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) { 749 uint32_t SectionIndex = readVaruint32(Ctx); 750 if (SectionIndex >= Sections.size()) 751 return make_error<GenericBinaryError>("Invalid section index", 752 object_error::parse_failed); 753 WasmSection &Section = Sections[SectionIndex]; 754 uint32_t RelocCount = readVaruint32(Ctx); 755 uint32_t EndOffset = Section.Content.size(); 756 uint32_t PreviousOffset = 0; 757 while (RelocCount--) { 758 wasm::WasmRelocation Reloc = {}; 759 Reloc.Type = readVaruint32(Ctx); 760 Reloc.Offset = readVaruint32(Ctx); 761 if (Reloc.Offset < PreviousOffset) 762 return make_error<GenericBinaryError>("Relocations not in offset order", 763 object_error::parse_failed); 764 PreviousOffset = Reloc.Offset; 765 Reloc.Index = readVaruint32(Ctx); 766 switch (Reloc.Type) { 767 case wasm::R_WASM_FUNCTION_INDEX_LEB: 768 case wasm::R_WASM_TABLE_INDEX_SLEB: 769 case wasm::R_WASM_TABLE_INDEX_I32: 770 case wasm::R_WASM_TABLE_INDEX_REL_SLEB: 771 if (!isValidFunctionSymbol(Reloc.Index)) 772 return make_error<GenericBinaryError>("Bad relocation function index", 773 object_error::parse_failed); 774 break; 775 case wasm::R_WASM_TYPE_INDEX_LEB: 776 if (Reloc.Index >= Signatures.size()) 777 return make_error<GenericBinaryError>("Bad relocation type index", 778 object_error::parse_failed); 779 break; 780 case wasm::R_WASM_GLOBAL_INDEX_LEB: 781 // R_WASM_GLOBAL_INDEX_LEB are can be used against function and data 782 // symbols to refer to thier GOT enties. 783 if (!isValidGlobalSymbol(Reloc.Index) && 784 !isValidDataSymbol(Reloc.Index) && 785 !isValidFunctionSymbol(Reloc.Index)) 786 return make_error<GenericBinaryError>("Bad relocation global index", 787 object_error::parse_failed); 788 break; 789 case wasm::R_WASM_EVENT_INDEX_LEB: 790 if (!isValidEventSymbol(Reloc.Index)) 791 return make_error<GenericBinaryError>("Bad relocation event index", 792 object_error::parse_failed); 793 break; 794 case wasm::R_WASM_MEMORY_ADDR_LEB: 795 case wasm::R_WASM_MEMORY_ADDR_SLEB: 796 case wasm::R_WASM_MEMORY_ADDR_I32: 797 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB: 798 if (!isValidDataSymbol(Reloc.Index)) 799 return make_error<GenericBinaryError>("Bad relocation data index", 800 object_error::parse_failed); 801 Reloc.Addend = readVarint32(Ctx); 802 break; 803 case wasm::R_WASM_FUNCTION_OFFSET_I32: 804 if (!isValidFunctionSymbol(Reloc.Index)) 805 return make_error<GenericBinaryError>("Bad relocation function index", 806 object_error::parse_failed); 807 Reloc.Addend = readVarint32(Ctx); 808 break; 809 case wasm::R_WASM_SECTION_OFFSET_I32: 810 if (!isValidSectionSymbol(Reloc.Index)) 811 return make_error<GenericBinaryError>("Bad relocation section index", 812 object_error::parse_failed); 813 Reloc.Addend = readVarint32(Ctx); 814 break; 815 default: 816 return make_error<GenericBinaryError>("Bad relocation type: " + 817 Twine(Reloc.Type), 818 object_error::parse_failed); 819 } 820 821 // Relocations must fit inside the section, and must appear in order. They 822 // also shouldn't overlap a function/element boundary, but we don't bother 823 // to check that. 824 uint64_t Size = 5; 825 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I32 || 826 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I32 || 827 Reloc.Type == wasm::R_WASM_SECTION_OFFSET_I32 || 828 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I32) 829 Size = 4; 830 if (Reloc.Offset + Size > EndOffset) 831 return make_error<GenericBinaryError>("Bad relocation offset", 832 object_error::parse_failed); 833 834 Section.Relocations.push_back(Reloc); 835 } 836 if (Ctx.Ptr != Ctx.End) 837 return make_error<GenericBinaryError>("Reloc section ended prematurely", 838 object_error::parse_failed); 839 return Error::success(); 840 } 841 842 Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) { 843 if (Sec.Name == "dylink") { 844 if (Error Err = parseDylinkSection(Ctx)) 845 return Err; 846 } else if (Sec.Name == "name") { 847 if (Error Err = parseNameSection(Ctx)) 848 return Err; 849 } else if (Sec.Name == "linking") { 850 if (Error Err = parseLinkingSection(Ctx)) 851 return Err; 852 } else if (Sec.Name == "producers") { 853 if (Error Err = parseProducersSection(Ctx)) 854 return Err; 855 } else if (Sec.Name == "target_features") { 856 if (Error Err = parseTargetFeaturesSection(Ctx)) 857 return Err; 858 } else if (Sec.Name.startswith("reloc.")) { 859 if (Error Err = parseRelocSection(Sec.Name, Ctx)) 860 return Err; 861 } 862 return Error::success(); 863 } 864 865 Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) { 866 uint32_t Count = readVaruint32(Ctx); 867 Signatures.reserve(Count); 868 while (Count--) { 869 wasm::WasmSignature Sig; 870 uint8_t Form = readUint8(Ctx); 871 if (Form != wasm::WASM_TYPE_FUNC) { 872 return make_error<GenericBinaryError>("Invalid signature type", 873 object_error::parse_failed); 874 } 875 uint32_t ParamCount = readVaruint32(Ctx); 876 Sig.Params.reserve(ParamCount); 877 while (ParamCount--) { 878 uint32_t ParamType = readUint8(Ctx); 879 Sig.Params.push_back(wasm::ValType(ParamType)); 880 } 881 uint32_t ReturnCount = readVaruint32(Ctx); 882 if (ReturnCount) { 883 if (ReturnCount != 1) { 884 return make_error<GenericBinaryError>( 885 "Multiple return types not supported", object_error::parse_failed); 886 } 887 Sig.Returns.push_back(wasm::ValType(readUint8(Ctx))); 888 } 889 Signatures.push_back(std::move(Sig)); 890 } 891 if (Ctx.Ptr != Ctx.End) 892 return make_error<GenericBinaryError>("Type section ended prematurely", 893 object_error::parse_failed); 894 return Error::success(); 895 } 896 897 Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { 898 uint32_t Count = readVaruint32(Ctx); 899 Imports.reserve(Count); 900 for (uint32_t I = 0; I < Count; I++) { 901 wasm::WasmImport Im; 902 Im.Module = readString(Ctx); 903 Im.Field = readString(Ctx); 904 Im.Kind = readUint8(Ctx); 905 switch (Im.Kind) { 906 case wasm::WASM_EXTERNAL_FUNCTION: 907 NumImportedFunctions++; 908 Im.SigIndex = readVaruint32(Ctx); 909 break; 910 case wasm::WASM_EXTERNAL_GLOBAL: 911 NumImportedGlobals++; 912 Im.Global.Type = readUint8(Ctx); 913 Im.Global.Mutable = readVaruint1(Ctx); 914 break; 915 case wasm::WASM_EXTERNAL_MEMORY: 916 Im.Memory = readLimits(Ctx); 917 break; 918 case wasm::WASM_EXTERNAL_TABLE: 919 Im.Table = readTable(Ctx); 920 if (Im.Table.ElemType != wasm::WASM_TYPE_FUNCREF) 921 return make_error<GenericBinaryError>("Invalid table element type", 922 object_error::parse_failed); 923 break; 924 case wasm::WASM_EXTERNAL_EVENT: 925 NumImportedEvents++; 926 Im.Event.Attribute = readVarint32(Ctx); 927 Im.Event.SigIndex = readVarint32(Ctx); 928 break; 929 default: 930 return make_error<GenericBinaryError>("Unexpected import kind", 931 object_error::parse_failed); 932 } 933 Imports.push_back(Im); 934 } 935 if (Ctx.Ptr != Ctx.End) 936 return make_error<GenericBinaryError>("Import section ended prematurely", 937 object_error::parse_failed); 938 return Error::success(); 939 } 940 941 Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) { 942 uint32_t Count = readVaruint32(Ctx); 943 FunctionTypes.reserve(Count); 944 uint32_t NumTypes = Signatures.size(); 945 while (Count--) { 946 uint32_t Type = readVaruint32(Ctx); 947 if (Type >= NumTypes) 948 return make_error<GenericBinaryError>("Invalid function type", 949 object_error::parse_failed); 950 FunctionTypes.push_back(Type); 951 } 952 if (Ctx.Ptr != Ctx.End) 953 return make_error<GenericBinaryError>("Function section ended prematurely", 954 object_error::parse_failed); 955 return Error::success(); 956 } 957 958 Error WasmObjectFile::parseTableSection(ReadContext &Ctx) { 959 uint32_t Count = readVaruint32(Ctx); 960 Tables.reserve(Count); 961 while (Count--) { 962 Tables.push_back(readTable(Ctx)); 963 if (Tables.back().ElemType != wasm::WASM_TYPE_FUNCREF) { 964 return make_error<GenericBinaryError>("Invalid table element type", 965 object_error::parse_failed); 966 } 967 } 968 if (Ctx.Ptr != Ctx.End) 969 return make_error<GenericBinaryError>("Table section ended prematurely", 970 object_error::parse_failed); 971 return Error::success(); 972 } 973 974 Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) { 975 uint32_t Count = readVaruint32(Ctx); 976 Memories.reserve(Count); 977 while (Count--) { 978 Memories.push_back(readLimits(Ctx)); 979 } 980 if (Ctx.Ptr != Ctx.End) 981 return make_error<GenericBinaryError>("Memory section ended prematurely", 982 object_error::parse_failed); 983 return Error::success(); 984 } 985 986 Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) { 987 GlobalSection = Sections.size(); 988 uint32_t Count = readVaruint32(Ctx); 989 Globals.reserve(Count); 990 while (Count--) { 991 wasm::WasmGlobal Global; 992 Global.Index = NumImportedGlobals + Globals.size(); 993 Global.Type.Type = readUint8(Ctx); 994 Global.Type.Mutable = readVaruint1(Ctx); 995 if (Error Err = readInitExpr(Global.InitExpr, Ctx)) 996 return Err; 997 Globals.push_back(Global); 998 } 999 if (Ctx.Ptr != Ctx.End) 1000 return make_error<GenericBinaryError>("Global section ended prematurely", 1001 object_error::parse_failed); 1002 return Error::success(); 1003 } 1004 1005 Error WasmObjectFile::parseEventSection(ReadContext &Ctx) { 1006 EventSection = Sections.size(); 1007 uint32_t Count = readVarint32(Ctx); 1008 Events.reserve(Count); 1009 while (Count--) { 1010 wasm::WasmEvent Event; 1011 Event.Index = NumImportedEvents + Events.size(); 1012 Event.Type.Attribute = readVaruint32(Ctx); 1013 Event.Type.SigIndex = readVarint32(Ctx); 1014 Events.push_back(Event); 1015 } 1016 1017 if (Ctx.Ptr != Ctx.End) 1018 return make_error<GenericBinaryError>("Event section ended prematurely", 1019 object_error::parse_failed); 1020 return Error::success(); 1021 } 1022 1023 Error WasmObjectFile::parseExportSection(ReadContext &Ctx) { 1024 uint32_t Count = readVaruint32(Ctx); 1025 Exports.reserve(Count); 1026 for (uint32_t I = 0; I < Count; I++) { 1027 wasm::WasmExport Ex; 1028 Ex.Name = readString(Ctx); 1029 Ex.Kind = readUint8(Ctx); 1030 Ex.Index = readVaruint32(Ctx); 1031 switch (Ex.Kind) { 1032 case wasm::WASM_EXTERNAL_FUNCTION: 1033 if (!isValidFunctionIndex(Ex.Index)) 1034 return make_error<GenericBinaryError>("Invalid function export", 1035 object_error::parse_failed); 1036 break; 1037 case wasm::WASM_EXTERNAL_GLOBAL: 1038 if (!isValidGlobalIndex(Ex.Index)) 1039 return make_error<GenericBinaryError>("Invalid global export", 1040 object_error::parse_failed); 1041 break; 1042 case wasm::WASM_EXTERNAL_EVENT: 1043 if (!isValidEventIndex(Ex.Index)) 1044 return make_error<GenericBinaryError>("Invalid event export", 1045 object_error::parse_failed); 1046 break; 1047 case wasm::WASM_EXTERNAL_MEMORY: 1048 case wasm::WASM_EXTERNAL_TABLE: 1049 break; 1050 default: 1051 return make_error<GenericBinaryError>("Unexpected export kind", 1052 object_error::parse_failed); 1053 } 1054 Exports.push_back(Ex); 1055 } 1056 if (Ctx.Ptr != Ctx.End) 1057 return make_error<GenericBinaryError>("Export section ended prematurely", 1058 object_error::parse_failed); 1059 return Error::success(); 1060 } 1061 1062 bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const { 1063 return Index < NumImportedFunctions + FunctionTypes.size(); 1064 } 1065 1066 bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const { 1067 return Index >= NumImportedFunctions && isValidFunctionIndex(Index); 1068 } 1069 1070 bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const { 1071 return Index < NumImportedGlobals + Globals.size(); 1072 } 1073 1074 bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const { 1075 return Index >= NumImportedGlobals && isValidGlobalIndex(Index); 1076 } 1077 1078 bool WasmObjectFile::isValidEventIndex(uint32_t Index) const { 1079 return Index < NumImportedEvents + Events.size(); 1080 } 1081 1082 bool WasmObjectFile::isDefinedEventIndex(uint32_t Index) const { 1083 return Index >= NumImportedEvents && isValidEventIndex(Index); 1084 } 1085 1086 bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const { 1087 return Index < Symbols.size() && Symbols[Index].isTypeFunction(); 1088 } 1089 1090 bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const { 1091 return Index < Symbols.size() && Symbols[Index].isTypeGlobal(); 1092 } 1093 1094 bool WasmObjectFile::isValidEventSymbol(uint32_t Index) const { 1095 return Index < Symbols.size() && Symbols[Index].isTypeEvent(); 1096 } 1097 1098 bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const { 1099 return Index < Symbols.size() && Symbols[Index].isTypeData(); 1100 } 1101 1102 bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const { 1103 return Index < Symbols.size() && Symbols[Index].isTypeSection(); 1104 } 1105 1106 wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) { 1107 assert(isDefinedFunctionIndex(Index)); 1108 return Functions[Index - NumImportedFunctions]; 1109 } 1110 1111 const wasm::WasmFunction & 1112 WasmObjectFile::getDefinedFunction(uint32_t Index) const { 1113 assert(isDefinedFunctionIndex(Index)); 1114 return Functions[Index - NumImportedFunctions]; 1115 } 1116 1117 wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) { 1118 assert(isDefinedGlobalIndex(Index)); 1119 return Globals[Index - NumImportedGlobals]; 1120 } 1121 1122 wasm::WasmEvent &WasmObjectFile::getDefinedEvent(uint32_t Index) { 1123 assert(isDefinedEventIndex(Index)); 1124 return Events[Index - NumImportedEvents]; 1125 } 1126 1127 Error WasmObjectFile::parseStartSection(ReadContext &Ctx) { 1128 StartFunction = readVaruint32(Ctx); 1129 if (!isValidFunctionIndex(StartFunction)) 1130 return make_error<GenericBinaryError>("Invalid start function", 1131 object_error::parse_failed); 1132 return Error::success(); 1133 } 1134 1135 Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) { 1136 CodeSection = Sections.size(); 1137 uint32_t FunctionCount = readVaruint32(Ctx); 1138 if (FunctionCount != FunctionTypes.size()) { 1139 return make_error<GenericBinaryError>("Invalid function count", 1140 object_error::parse_failed); 1141 } 1142 1143 while (FunctionCount--) { 1144 wasm::WasmFunction Function; 1145 const uint8_t *FunctionStart = Ctx.Ptr; 1146 uint32_t Size = readVaruint32(Ctx); 1147 const uint8_t *FunctionEnd = Ctx.Ptr + Size; 1148 1149 Function.CodeOffset = Ctx.Ptr - FunctionStart; 1150 Function.Index = NumImportedFunctions + Functions.size(); 1151 Function.CodeSectionOffset = FunctionStart - Ctx.Start; 1152 Function.Size = FunctionEnd - FunctionStart; 1153 1154 uint32_t NumLocalDecls = readVaruint32(Ctx); 1155 Function.Locals.reserve(NumLocalDecls); 1156 while (NumLocalDecls--) { 1157 wasm::WasmLocalDecl Decl; 1158 Decl.Count = readVaruint32(Ctx); 1159 Decl.Type = readUint8(Ctx); 1160 Function.Locals.push_back(Decl); 1161 } 1162 1163 uint32_t BodySize = FunctionEnd - Ctx.Ptr; 1164 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize); 1165 // This will be set later when reading in the linking metadata section. 1166 Function.Comdat = UINT32_MAX; 1167 Ctx.Ptr += BodySize; 1168 assert(Ctx.Ptr == FunctionEnd); 1169 Functions.push_back(Function); 1170 } 1171 if (Ctx.Ptr != Ctx.End) 1172 return make_error<GenericBinaryError>("Code section ended prematurely", 1173 object_error::parse_failed); 1174 return Error::success(); 1175 } 1176 1177 Error WasmObjectFile::parseElemSection(ReadContext &Ctx) { 1178 uint32_t Count = readVaruint32(Ctx); 1179 ElemSegments.reserve(Count); 1180 while (Count--) { 1181 wasm::WasmElemSegment Segment; 1182 Segment.TableIndex = readVaruint32(Ctx); 1183 if (Segment.TableIndex != 0) { 1184 return make_error<GenericBinaryError>("Invalid TableIndex", 1185 object_error::parse_failed); 1186 } 1187 if (Error Err = readInitExpr(Segment.Offset, Ctx)) 1188 return Err; 1189 uint32_t NumElems = readVaruint32(Ctx); 1190 while (NumElems--) { 1191 Segment.Functions.push_back(readVaruint32(Ctx)); 1192 } 1193 ElemSegments.push_back(Segment); 1194 } 1195 if (Ctx.Ptr != Ctx.End) 1196 return make_error<GenericBinaryError>("Elem section ended prematurely", 1197 object_error::parse_failed); 1198 return Error::success(); 1199 } 1200 1201 Error WasmObjectFile::parseDataSection(ReadContext &Ctx) { 1202 DataSection = Sections.size(); 1203 uint32_t Count = readVaruint32(Ctx); 1204 DataSegments.reserve(Count); 1205 while (Count--) { 1206 WasmSegment Segment; 1207 Segment.Data.InitFlags = readVaruint32(Ctx); 1208 Segment.Data.MemoryIndex = (Segment.Data.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX) 1209 ? readVaruint32(Ctx) : 0; 1210 if ((Segment.Data.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) { 1211 if (Error Err = readInitExpr(Segment.Data.Offset, Ctx)) 1212 return Err; 1213 } else { 1214 Segment.Data.Offset.Opcode = wasm::WASM_OPCODE_I32_CONST; 1215 Segment.Data.Offset.Value.Int32 = 0; 1216 } 1217 uint32_t Size = readVaruint32(Ctx); 1218 if (Size > (size_t)(Ctx.End - Ctx.Ptr)) 1219 return make_error<GenericBinaryError>("Invalid segment size", 1220 object_error::parse_failed); 1221 Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size); 1222 // The rest of these Data fields are set later, when reading in the linking 1223 // metadata section. 1224 Segment.Data.Alignment = 0; 1225 Segment.Data.LinkerFlags = 0; 1226 Segment.Data.Comdat = UINT32_MAX; 1227 Segment.SectionOffset = Ctx.Ptr - Ctx.Start; 1228 Ctx.Ptr += Size; 1229 DataSegments.push_back(Segment); 1230 } 1231 if (Ctx.Ptr != Ctx.End) 1232 return make_error<GenericBinaryError>("Data section ended prematurely", 1233 object_error::parse_failed); 1234 return Error::success(); 1235 } 1236 1237 const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const { 1238 return Header; 1239 } 1240 1241 void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.b++; } 1242 1243 uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const { 1244 uint32_t Result = SymbolRef::SF_None; 1245 const WasmSymbol &Sym = getWasmSymbol(Symb); 1246 1247 LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n"); 1248 if (Sym.isBindingWeak()) 1249 Result |= SymbolRef::SF_Weak; 1250 if (!Sym.isBindingLocal()) 1251 Result |= SymbolRef::SF_Global; 1252 if (Sym.isHidden()) 1253 Result |= SymbolRef::SF_Hidden; 1254 if (!Sym.isDefined()) 1255 Result |= SymbolRef::SF_Undefined; 1256 if (Sym.isTypeFunction()) 1257 Result |= SymbolRef::SF_Executable; 1258 return Result; 1259 } 1260 1261 basic_symbol_iterator WasmObjectFile::symbol_begin() const { 1262 DataRefImpl Ref; 1263 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null 1264 Ref.d.b = 0; // Symbol index 1265 return BasicSymbolRef(Ref, this); 1266 } 1267 1268 basic_symbol_iterator WasmObjectFile::symbol_end() const { 1269 DataRefImpl Ref; 1270 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null 1271 Ref.d.b = Symbols.size(); // Symbol index 1272 return BasicSymbolRef(Ref, this); 1273 } 1274 1275 const WasmSymbol &WasmObjectFile::getWasmSymbol(const DataRefImpl &Symb) const { 1276 return Symbols[Symb.d.b]; 1277 } 1278 1279 const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const { 1280 return getWasmSymbol(Symb.getRawDataRefImpl()); 1281 } 1282 1283 Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const { 1284 return getWasmSymbol(Symb).Info.Name; 1285 } 1286 1287 Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const { 1288 auto &Sym = getWasmSymbol(Symb); 1289 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION && 1290 isDefinedFunctionIndex(Sym.Info.ElementIndex)) 1291 return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset; 1292 else 1293 return getSymbolValue(Symb); 1294 } 1295 1296 uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol &Sym) const { 1297 switch (Sym.Info.Kind) { 1298 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1299 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1300 case wasm::WASM_SYMBOL_TYPE_EVENT: 1301 return Sym.Info.ElementIndex; 1302 case wasm::WASM_SYMBOL_TYPE_DATA: { 1303 // The value of a data symbol is the segment offset, plus the symbol 1304 // offset within the segment. 1305 uint32_t SegmentIndex = Sym.Info.DataRef.Segment; 1306 const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data; 1307 assert(Segment.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST); 1308 return Segment.Offset.Value.Int32 + Sym.Info.DataRef.Offset; 1309 } 1310 case wasm::WASM_SYMBOL_TYPE_SECTION: 1311 return 0; 1312 } 1313 llvm_unreachable("invalid symbol type"); 1314 } 1315 1316 uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const { 1317 return getWasmSymbolValue(getWasmSymbol(Symb)); 1318 } 1319 1320 uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const { 1321 llvm_unreachable("not yet implemented"); 1322 return 0; 1323 } 1324 1325 uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const { 1326 llvm_unreachable("not yet implemented"); 1327 return 0; 1328 } 1329 1330 Expected<SymbolRef::Type> 1331 WasmObjectFile::getSymbolType(DataRefImpl Symb) const { 1332 const WasmSymbol &Sym = getWasmSymbol(Symb); 1333 1334 switch (Sym.Info.Kind) { 1335 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1336 return SymbolRef::ST_Function; 1337 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1338 return SymbolRef::ST_Other; 1339 case wasm::WASM_SYMBOL_TYPE_DATA: 1340 return SymbolRef::ST_Data; 1341 case wasm::WASM_SYMBOL_TYPE_SECTION: 1342 return SymbolRef::ST_Debug; 1343 case wasm::WASM_SYMBOL_TYPE_EVENT: 1344 return SymbolRef::ST_Other; 1345 } 1346 1347 llvm_unreachable("Unknown WasmSymbol::SymbolType"); 1348 return SymbolRef::ST_Other; 1349 } 1350 1351 Expected<section_iterator> 1352 WasmObjectFile::getSymbolSection(DataRefImpl Symb) const { 1353 const WasmSymbol &Sym = getWasmSymbol(Symb); 1354 if (Sym.isUndefined()) 1355 return section_end(); 1356 1357 DataRefImpl Ref; 1358 switch (Sym.Info.Kind) { 1359 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1360 Ref.d.a = CodeSection; 1361 break; 1362 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1363 Ref.d.a = GlobalSection; 1364 break; 1365 case wasm::WASM_SYMBOL_TYPE_DATA: 1366 Ref.d.a = DataSection; 1367 break; 1368 case wasm::WASM_SYMBOL_TYPE_SECTION: 1369 Ref.d.a = Sym.Info.ElementIndex; 1370 break; 1371 case wasm::WASM_SYMBOL_TYPE_EVENT: 1372 Ref.d.a = EventSection; 1373 break; 1374 default: 1375 llvm_unreachable("Unknown WasmSymbol::SymbolType"); 1376 } 1377 return section_iterator(SectionRef(Ref, this)); 1378 } 1379 1380 void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; } 1381 1382 std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec, 1383 StringRef &Res) const { 1384 const WasmSection &S = Sections[Sec.d.a]; 1385 #define ECase(X) \ 1386 case wasm::WASM_SEC_##X: \ 1387 Res = #X; \ 1388 break 1389 switch (S.Type) { 1390 ECase(TYPE); 1391 ECase(IMPORT); 1392 ECase(FUNCTION); 1393 ECase(TABLE); 1394 ECase(MEMORY); 1395 ECase(GLOBAL); 1396 ECase(EVENT); 1397 ECase(EXPORT); 1398 ECase(START); 1399 ECase(ELEM); 1400 ECase(CODE); 1401 ECase(DATA); 1402 case wasm::WASM_SEC_CUSTOM: 1403 Res = S.Name; 1404 break; 1405 default: 1406 return object_error::invalid_section_index; 1407 } 1408 #undef ECase 1409 return std::error_code(); 1410 } 1411 1412 uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; } 1413 1414 uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const { 1415 return Sec.d.a; 1416 } 1417 1418 uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const { 1419 const WasmSection &S = Sections[Sec.d.a]; 1420 return S.Content.size(); 1421 } 1422 1423 std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec, 1424 StringRef &Res) const { 1425 const WasmSection &S = Sections[Sec.d.a]; 1426 // This will never fail since wasm sections can never be empty (user-sections 1427 // must have a name and non-user sections each have a defined structure). 1428 Res = StringRef(reinterpret_cast<const char *>(S.Content.data()), 1429 S.Content.size()); 1430 return std::error_code(); 1431 } 1432 1433 uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const { 1434 return 1; 1435 } 1436 1437 bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const { 1438 return false; 1439 } 1440 1441 bool WasmObjectFile::isSectionText(DataRefImpl Sec) const { 1442 return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE; 1443 } 1444 1445 bool WasmObjectFile::isSectionData(DataRefImpl Sec) const { 1446 return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA; 1447 } 1448 1449 bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; } 1450 1451 bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; } 1452 1453 bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; } 1454 1455 relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const { 1456 DataRefImpl RelocRef; 1457 RelocRef.d.a = Ref.d.a; 1458 RelocRef.d.b = 0; 1459 return relocation_iterator(RelocationRef(RelocRef, this)); 1460 } 1461 1462 relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const { 1463 const WasmSection &Sec = getWasmSection(Ref); 1464 DataRefImpl RelocRef; 1465 RelocRef.d.a = Ref.d.a; 1466 RelocRef.d.b = Sec.Relocations.size(); 1467 return relocation_iterator(RelocationRef(RelocRef, this)); 1468 } 1469 1470 void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const { Rel.d.b++; } 1471 1472 uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const { 1473 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1474 return Rel.Offset; 1475 } 1476 1477 symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Ref) const { 1478 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1479 if (Rel.Type == wasm::R_WASM_TYPE_INDEX_LEB) 1480 return symbol_end(); 1481 DataRefImpl Sym; 1482 Sym.d.a = 1; 1483 Sym.d.b = Rel.Index; 1484 return symbol_iterator(SymbolRef(Sym, this)); 1485 } 1486 1487 uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const { 1488 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1489 return Rel.Type; 1490 } 1491 1492 void WasmObjectFile::getRelocationTypeName( 1493 DataRefImpl Ref, SmallVectorImpl<char> &Result) const { 1494 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1495 StringRef Res = "Unknown"; 1496 1497 #define WASM_RELOC(name, value) \ 1498 case wasm::name: \ 1499 Res = #name; \ 1500 break; 1501 1502 switch (Rel.Type) { 1503 #include "llvm/BinaryFormat/WasmRelocs.def" 1504 } 1505 1506 #undef WASM_RELOC 1507 1508 Result.append(Res.begin(), Res.end()); 1509 } 1510 1511 section_iterator WasmObjectFile::section_begin() const { 1512 DataRefImpl Ref; 1513 Ref.d.a = 0; 1514 return section_iterator(SectionRef(Ref, this)); 1515 } 1516 1517 section_iterator WasmObjectFile::section_end() const { 1518 DataRefImpl Ref; 1519 Ref.d.a = Sections.size(); 1520 return section_iterator(SectionRef(Ref, this)); 1521 } 1522 1523 uint8_t WasmObjectFile::getBytesInAddress() const { return 4; } 1524 1525 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; } 1526 1527 Triple::ArchType WasmObjectFile::getArch() const { return Triple::wasm32; } 1528 1529 SubtargetFeatures WasmObjectFile::getFeatures() const { 1530 return SubtargetFeatures(); 1531 } 1532 1533 bool WasmObjectFile::isRelocatableObject() const { return HasLinkingSection; } 1534 1535 bool WasmObjectFile::isSharedObject() const { return HasDylinkSection; } 1536 1537 const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const { 1538 assert(Ref.d.a < Sections.size()); 1539 return Sections[Ref.d.a]; 1540 } 1541 1542 const WasmSection & 1543 WasmObjectFile::getWasmSection(const SectionRef &Section) const { 1544 return getWasmSection(Section.getRawDataRefImpl()); 1545 } 1546 1547 const wasm::WasmRelocation & 1548 WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const { 1549 return getWasmRelocation(Ref.getRawDataRefImpl()); 1550 } 1551 1552 const wasm::WasmRelocation & 1553 WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const { 1554 assert(Ref.d.a < Sections.size()); 1555 const WasmSection &Sec = Sections[Ref.d.a]; 1556 assert(Ref.d.b < Sec.Relocations.size()); 1557 return Sec.Relocations[Ref.d.b]; 1558 } 1559 1560 int WasmSectionOrderChecker::getSectionOrder(unsigned ID, 1561 StringRef CustomSectionName) { 1562 switch (ID) { 1563 case wasm::WASM_SEC_CUSTOM: 1564 return StringSwitch<unsigned>(CustomSectionName) 1565 .Case("dylink", WASM_SEC_ORDER_DYLINK) 1566 .Case("linking", WASM_SEC_ORDER_LINKING) 1567 .StartsWith("reloc.", WASM_SEC_ORDER_RELOC) 1568 .Case("name", WASM_SEC_ORDER_NAME) 1569 .Case("producers", WASM_SEC_ORDER_PRODUCERS) 1570 .Case("target_features", WASM_SEC_ORDER_TARGET_FEATURES) 1571 .Default(WASM_SEC_ORDER_NONE); 1572 case wasm::WASM_SEC_TYPE: 1573 return WASM_SEC_ORDER_TYPE; 1574 case wasm::WASM_SEC_IMPORT: 1575 return WASM_SEC_ORDER_IMPORT; 1576 case wasm::WASM_SEC_FUNCTION: 1577 return WASM_SEC_ORDER_FUNCTION; 1578 case wasm::WASM_SEC_TABLE: 1579 return WASM_SEC_ORDER_TABLE; 1580 case wasm::WASM_SEC_MEMORY: 1581 return WASM_SEC_ORDER_MEMORY; 1582 case wasm::WASM_SEC_GLOBAL: 1583 return WASM_SEC_ORDER_GLOBAL; 1584 case wasm::WASM_SEC_EXPORT: 1585 return WASM_SEC_ORDER_EXPORT; 1586 case wasm::WASM_SEC_START: 1587 return WASM_SEC_ORDER_START; 1588 case wasm::WASM_SEC_ELEM: 1589 return WASM_SEC_ORDER_ELEM; 1590 case wasm::WASM_SEC_CODE: 1591 return WASM_SEC_ORDER_CODE; 1592 case wasm::WASM_SEC_DATA: 1593 return WASM_SEC_ORDER_DATA; 1594 case wasm::WASM_SEC_DATACOUNT: 1595 return WASM_SEC_ORDER_DATACOUNT; 1596 case wasm::WASM_SEC_EVENT: 1597 return WASM_SEC_ORDER_EVENT; 1598 default: 1599 llvm_unreachable("invalid section"); 1600 } 1601 } 1602 1603 // Represents the edges in a directed graph where any node B reachable from node 1604 // A is not allowed to appear before A in the section ordering, but may appear 1605 // afterward. 1606 int WasmSectionOrderChecker::DisallowedPredecessors[WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = { 1607 {}, // WASM_SEC_ORDER_NONE 1608 {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT}, // WASM_SEC_ORDER_TYPE, 1609 {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION}, // WASM_SEC_ORDER_IMPORT, 1610 {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE}, // WASM_SEC_ORDER_FUNCTION, 1611 {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY}, // WASM_SEC_ORDER_TABLE, 1612 {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_GLOBAL}, // WASM_SEC_ORDER_MEMORY, 1613 {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EVENT}, // WASM_SEC_ORDER_GLOBAL, 1614 {WASM_SEC_ORDER_EVENT, WASM_SEC_ORDER_EXPORT}, // WASM_SEC_ORDER_EVENT, 1615 {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START}, // WASM_SEC_ORDER_EXPORT, 1616 {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM}, // WASM_SEC_ORDER_START, 1617 {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT}, // WASM_SEC_ORDER_ELEM, 1618 {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE}, // WASM_SEC_ORDER_DATACOUNT, 1619 {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA}, // WASM_SEC_ORDER_CODE, 1620 {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING}, // WASM_SEC_ORDER_DATA, 1621 1622 // Custom Sections 1623 {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE}, // WASM_SEC_ORDER_DYLINK, 1624 {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME}, // WASM_SEC_ORDER_LINKING, 1625 {}, // WASM_SEC_ORDER_RELOC (can be repeated), 1626 {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS}, // WASM_SEC_ORDER_NAME, 1627 {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES}, // WASM_SEC_ORDER_PRODUCERS, 1628 {WASM_SEC_ORDER_TARGET_FEATURES} // WASM_SEC_ORDER_TARGET_FEATURES 1629 }; 1630 1631 bool WasmSectionOrderChecker::isValidSectionOrder(unsigned ID, 1632 StringRef CustomSectionName) { 1633 int Order = getSectionOrder(ID, CustomSectionName); 1634 if (Order == WASM_SEC_ORDER_NONE) 1635 return true; 1636 1637 // Disallowed predecessors we need to check for 1638 SmallVector<int, WASM_NUM_SEC_ORDERS> WorkList; 1639 1640 // Keep track of completed checks to avoid repeating work 1641 bool Checked[WASM_NUM_SEC_ORDERS] = {}; 1642 1643 int Curr = Order; 1644 while (true) { 1645 // Add new disallowed predecessors to work list 1646 for (size_t I = 0;; ++I) { 1647 int Next = DisallowedPredecessors[Curr][I]; 1648 if (Next == WASM_SEC_ORDER_NONE) 1649 break; 1650 if (Checked[Next]) 1651 continue; 1652 WorkList.push_back(Next); 1653 Checked[Next] = true; 1654 } 1655 1656 if (WorkList.empty()) 1657 break; 1658 1659 // Consider next disallowed predecessor 1660 Curr = WorkList.pop_back_val(); 1661 if (Seen[Curr]) 1662 return false; 1663 } 1664 1665 // Have not seen any disallowed predecessors 1666 Seen[Order] = true; 1667 return true; 1668 } 1669