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 = getPtr(0); 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 DylinkInfo.MemorySize = readVaruint32(Ctx); 328 DylinkInfo.MemoryAlignment = readVaruint32(Ctx); 329 DylinkInfo.TableSize = readVaruint32(Ctx); 330 DylinkInfo.TableAlignment = readVaruint32(Ctx); 331 uint32_t Count = readVaruint32(Ctx); 332 while (Count--) { 333 DylinkInfo.Needed.push_back(readString(Ctx)); 334 } 335 if (Ctx.Ptr != Ctx.End) 336 return make_error<GenericBinaryError>("dylink section ended prematurely", 337 object_error::parse_failed); 338 return Error::success(); 339 } 340 341 Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { 342 llvm::DenseSet<uint64_t> Seen; 343 if (Functions.size() != FunctionTypes.size()) { 344 return make_error<GenericBinaryError>("Names must come after code section", 345 object_error::parse_failed); 346 } 347 348 while (Ctx.Ptr < Ctx.End) { 349 uint8_t Type = readUint8(Ctx); 350 uint32_t Size = readVaruint32(Ctx); 351 const uint8_t *SubSectionEnd = Ctx.Ptr + Size; 352 switch (Type) { 353 case wasm::WASM_NAMES_FUNCTION: { 354 uint32_t Count = readVaruint32(Ctx); 355 while (Count--) { 356 uint32_t Index = readVaruint32(Ctx); 357 if (!Seen.insert(Index).second) 358 return make_error<GenericBinaryError>("Function named more than once", 359 object_error::parse_failed); 360 StringRef Name = readString(Ctx); 361 if (!isValidFunctionIndex(Index) || Name.empty()) 362 return make_error<GenericBinaryError>("Invalid name entry", 363 object_error::parse_failed); 364 DebugNames.push_back(wasm::WasmFunctionName{Index, Name}); 365 if (isDefinedFunctionIndex(Index)) 366 getDefinedFunction(Index).DebugName = Name; 367 } 368 break; 369 } 370 // Ignore local names for now 371 case wasm::WASM_NAMES_LOCAL: 372 default: 373 Ctx.Ptr += Size; 374 break; 375 } 376 if (Ctx.Ptr != SubSectionEnd) 377 return make_error<GenericBinaryError>( 378 "Name sub-section ended prematurely", object_error::parse_failed); 379 } 380 381 if (Ctx.Ptr != Ctx.End) 382 return make_error<GenericBinaryError>("Name section ended prematurely", 383 object_error::parse_failed); 384 return Error::success(); 385 } 386 387 Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) { 388 HasLinkingSection = true; 389 if (Functions.size() != FunctionTypes.size()) { 390 return make_error<GenericBinaryError>( 391 "Linking data must come after code section", 392 object_error::parse_failed); 393 } 394 395 LinkingData.Version = readVaruint32(Ctx); 396 if (LinkingData.Version != wasm::WasmMetadataVersion) { 397 return make_error<GenericBinaryError>( 398 "Unexpected metadata version: " + Twine(LinkingData.Version) + 399 " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")", 400 object_error::parse_failed); 401 } 402 403 const uint8_t *OrigEnd = Ctx.End; 404 while (Ctx.Ptr < OrigEnd) { 405 Ctx.End = OrigEnd; 406 uint8_t Type = readUint8(Ctx); 407 uint32_t Size = readVaruint32(Ctx); 408 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size 409 << "\n"); 410 Ctx.End = Ctx.Ptr + Size; 411 switch (Type) { 412 case wasm::WASM_SYMBOL_TABLE: 413 if (Error Err = parseLinkingSectionSymtab(Ctx)) 414 return Err; 415 break; 416 case wasm::WASM_SEGMENT_INFO: { 417 uint32_t Count = readVaruint32(Ctx); 418 if (Count > DataSegments.size()) 419 return make_error<GenericBinaryError>("Too many segment names", 420 object_error::parse_failed); 421 for (uint32_t i = 0; i < Count; i++) { 422 DataSegments[i].Data.Name = readString(Ctx); 423 DataSegments[i].Data.Alignment = readVaruint32(Ctx); 424 DataSegments[i].Data.Flags = readVaruint32(Ctx); 425 } 426 break; 427 } 428 case wasm::WASM_INIT_FUNCS: { 429 uint32_t Count = readVaruint32(Ctx); 430 LinkingData.InitFunctions.reserve(Count); 431 for (uint32_t i = 0; i < Count; i++) { 432 wasm::WasmInitFunc Init; 433 Init.Priority = readVaruint32(Ctx); 434 Init.Symbol = readVaruint32(Ctx); 435 if (!isValidFunctionSymbol(Init.Symbol)) 436 return make_error<GenericBinaryError>("Invalid function symbol: " + 437 Twine(Init.Symbol), 438 object_error::parse_failed); 439 LinkingData.InitFunctions.emplace_back(Init); 440 } 441 break; 442 } 443 case wasm::WASM_COMDAT_INFO: 444 if (Error Err = parseLinkingSectionComdat(Ctx)) 445 return Err; 446 break; 447 default: 448 Ctx.Ptr += Size; 449 break; 450 } 451 if (Ctx.Ptr != Ctx.End) 452 return make_error<GenericBinaryError>( 453 "Linking sub-section ended prematurely", object_error::parse_failed); 454 } 455 if (Ctx.Ptr != OrigEnd) 456 return make_error<GenericBinaryError>("Linking section ended prematurely", 457 object_error::parse_failed); 458 return Error::success(); 459 } 460 461 Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { 462 uint32_t Count = readVaruint32(Ctx); 463 LinkingData.SymbolTable.reserve(Count); 464 Symbols.reserve(Count); 465 StringSet<> SymbolNames; 466 467 std::vector<wasm::WasmImport *> ImportedGlobals; 468 std::vector<wasm::WasmImport *> ImportedFunctions; 469 std::vector<wasm::WasmImport *> ImportedEvents; 470 ImportedGlobals.reserve(Imports.size()); 471 ImportedFunctions.reserve(Imports.size()); 472 ImportedEvents.reserve(Imports.size()); 473 for (auto &I : Imports) { 474 if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION) 475 ImportedFunctions.emplace_back(&I); 476 else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL) 477 ImportedGlobals.emplace_back(&I); 478 else if (I.Kind == wasm::WASM_EXTERNAL_EVENT) 479 ImportedEvents.emplace_back(&I); 480 } 481 482 while (Count--) { 483 wasm::WasmSymbolInfo Info; 484 const wasm::WasmSignature *Signature = nullptr; 485 const wasm::WasmGlobalType *GlobalType = nullptr; 486 const wasm::WasmEventType *EventType = nullptr; 487 488 Info.Kind = readUint8(Ctx); 489 Info.Flags = readVaruint32(Ctx); 490 bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0; 491 492 switch (Info.Kind) { 493 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 494 Info.ElementIndex = readVaruint32(Ctx); 495 if (!isValidFunctionIndex(Info.ElementIndex) || 496 IsDefined != isDefinedFunctionIndex(Info.ElementIndex)) 497 return make_error<GenericBinaryError>("invalid function symbol index", 498 object_error::parse_failed); 499 if (IsDefined) { 500 Info.Name = readString(Ctx); 501 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions; 502 Signature = &Signatures[FunctionTypes[FuncIndex]]; 503 wasm::WasmFunction &Function = Functions[FuncIndex]; 504 if (Function.SymbolName.empty()) 505 Function.SymbolName = Info.Name; 506 } else { 507 wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex]; 508 Signature = &Signatures[Import.SigIndex]; 509 Info.Name = Import.Field; 510 Info.Module = Import.Module; 511 } 512 break; 513 514 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 515 Info.ElementIndex = readVaruint32(Ctx); 516 if (!isValidGlobalIndex(Info.ElementIndex) || 517 IsDefined != isDefinedGlobalIndex(Info.ElementIndex)) 518 return make_error<GenericBinaryError>("invalid global symbol index", 519 object_error::parse_failed); 520 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == 521 wasm::WASM_SYMBOL_BINDING_WEAK) 522 return make_error<GenericBinaryError>("undefined weak global symbol", 523 object_error::parse_failed); 524 if (IsDefined) { 525 Info.Name = readString(Ctx); 526 unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals; 527 wasm::WasmGlobal &Global = Globals[GlobalIndex]; 528 GlobalType = &Global.Type; 529 if (Global.SymbolName.empty()) 530 Global.SymbolName = Info.Name; 531 } else { 532 wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex]; 533 Info.Name = Import.Field; 534 GlobalType = &Import.Global; 535 } 536 break; 537 538 case wasm::WASM_SYMBOL_TYPE_DATA: 539 Info.Name = readString(Ctx); 540 if (IsDefined) { 541 uint32_t Index = readVaruint32(Ctx); 542 if (Index >= DataSegments.size()) 543 return make_error<GenericBinaryError>("invalid data symbol index", 544 object_error::parse_failed); 545 uint32_t Offset = readVaruint32(Ctx); 546 uint32_t Size = readVaruint32(Ctx); 547 if (Offset + Size > DataSegments[Index].Data.Content.size()) 548 return make_error<GenericBinaryError>("invalid data symbol offset", 549 object_error::parse_failed); 550 Info.DataRef = wasm::WasmDataReference{Index, Offset, Size}; 551 } 552 break; 553 554 case wasm::WASM_SYMBOL_TYPE_SECTION: { 555 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != 556 wasm::WASM_SYMBOL_BINDING_LOCAL) 557 return make_error<GenericBinaryError>( 558 "Section symbols must have local binding", 559 object_error::parse_failed); 560 Info.ElementIndex = readVaruint32(Ctx); 561 // Use somewhat unique section name as symbol name. 562 StringRef SectionName = Sections[Info.ElementIndex].Name; 563 Info.Name = SectionName; 564 break; 565 } 566 567 case wasm::WASM_SYMBOL_TYPE_EVENT: { 568 Info.ElementIndex = readVaruint32(Ctx); 569 if (!isValidEventIndex(Info.ElementIndex) || 570 IsDefined != isDefinedEventIndex(Info.ElementIndex)) 571 return make_error<GenericBinaryError>("invalid event symbol index", 572 object_error::parse_failed); 573 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == 574 wasm::WASM_SYMBOL_BINDING_WEAK) 575 return make_error<GenericBinaryError>("undefined weak global symbol", 576 object_error::parse_failed); 577 if (IsDefined) { 578 Info.Name = readString(Ctx); 579 unsigned EventIndex = Info.ElementIndex - NumImportedEvents; 580 wasm::WasmEvent &Event = Events[EventIndex]; 581 Signature = &Signatures[Event.Type.SigIndex]; 582 EventType = &Event.Type; 583 if (Event.SymbolName.empty()) 584 Event.SymbolName = Info.Name; 585 586 } else { 587 wasm::WasmImport &Import = *ImportedEvents[Info.ElementIndex]; 588 EventType = &Import.Event; 589 Signature = &Signatures[EventType->SigIndex]; 590 Info.Name = Import.Field; 591 } 592 break; 593 } 594 595 default: 596 return make_error<GenericBinaryError>("Invalid symbol type", 597 object_error::parse_failed); 598 } 599 600 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != 601 wasm::WASM_SYMBOL_BINDING_LOCAL && 602 !SymbolNames.insert(Info.Name).second) 603 return make_error<GenericBinaryError>("Duplicate symbol name " + 604 Twine(Info.Name), 605 object_error::parse_failed); 606 LinkingData.SymbolTable.emplace_back(Info); 607 Symbols.emplace_back(LinkingData.SymbolTable.back(), GlobalType, EventType, 608 Signature); 609 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n"); 610 } 611 612 return Error::success(); 613 } 614 615 Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) { 616 uint32_t ComdatCount = readVaruint32(Ctx); 617 StringSet<> ComdatSet; 618 for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) { 619 StringRef Name = readString(Ctx); 620 if (Name.empty() || !ComdatSet.insert(Name).second) 621 return make_error<GenericBinaryError>("Bad/duplicate COMDAT name " + 622 Twine(Name), 623 object_error::parse_failed); 624 LinkingData.Comdats.emplace_back(Name); 625 uint32_t Flags = readVaruint32(Ctx); 626 if (Flags != 0) 627 return make_error<GenericBinaryError>("Unsupported COMDAT flags", 628 object_error::parse_failed); 629 630 uint32_t EntryCount = readVaruint32(Ctx); 631 while (EntryCount--) { 632 unsigned Kind = readVaruint32(Ctx); 633 unsigned Index = readVaruint32(Ctx); 634 switch (Kind) { 635 default: 636 return make_error<GenericBinaryError>("Invalid COMDAT entry type", 637 object_error::parse_failed); 638 case wasm::WASM_COMDAT_DATA: 639 if (Index >= DataSegments.size()) 640 return make_error<GenericBinaryError>( 641 "COMDAT data index out of range", object_error::parse_failed); 642 if (DataSegments[Index].Data.Comdat != UINT32_MAX) 643 return make_error<GenericBinaryError>("Data segment in two COMDATs", 644 object_error::parse_failed); 645 DataSegments[Index].Data.Comdat = ComdatIndex; 646 break; 647 case wasm::WASM_COMDAT_FUNCTION: 648 if (!isDefinedFunctionIndex(Index)) 649 return make_error<GenericBinaryError>( 650 "COMDAT function index out of range", object_error::parse_failed); 651 if (getDefinedFunction(Index).Comdat != UINT32_MAX) 652 return make_error<GenericBinaryError>("Function in two COMDATs", 653 object_error::parse_failed); 654 getDefinedFunction(Index).Comdat = ComdatIndex; 655 break; 656 } 657 } 658 } 659 return Error::success(); 660 } 661 662 Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) { 663 llvm::SmallSet<StringRef, 3> FieldsSeen; 664 uint32_t Fields = readVaruint32(Ctx); 665 for (size_t i = 0; i < Fields; ++i) { 666 StringRef FieldName = readString(Ctx); 667 if (!FieldsSeen.insert(FieldName).second) 668 return make_error<GenericBinaryError>( 669 "Producers section does not have unique fields", 670 object_error::parse_failed); 671 std::vector<std::pair<std::string, std::string>> *ProducerVec = nullptr; 672 if (FieldName == "language") { 673 ProducerVec = &ProducerInfo.Languages; 674 } else if (FieldName == "processed-by") { 675 ProducerVec = &ProducerInfo.Tools; 676 } else if (FieldName == "sdk") { 677 ProducerVec = &ProducerInfo.SDKs; 678 } else { 679 return make_error<GenericBinaryError>( 680 "Producers section field is not named one of language, processed-by, " 681 "or sdk", 682 object_error::parse_failed); 683 } 684 uint32_t ValueCount = readVaruint32(Ctx); 685 llvm::SmallSet<StringRef, 8> ProducersSeen; 686 for (size_t j = 0; j < ValueCount; ++j) { 687 StringRef Name = readString(Ctx); 688 StringRef Version = readString(Ctx); 689 if (!ProducersSeen.insert(Name).second) { 690 return make_error<GenericBinaryError>( 691 "Producers section contains repeated producer", 692 object_error::parse_failed); 693 } 694 ProducerVec->emplace_back(Name, Version); 695 } 696 } 697 if (Ctx.Ptr != Ctx.End) 698 return make_error<GenericBinaryError>("Producers section ended prematurely", 699 object_error::parse_failed); 700 return Error::success(); 701 } 702 703 Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) { 704 uint32_t SectionIndex = readVaruint32(Ctx); 705 if (SectionIndex >= Sections.size()) 706 return make_error<GenericBinaryError>("Invalid section index", 707 object_error::parse_failed); 708 WasmSection &Section = Sections[SectionIndex]; 709 uint32_t RelocCount = readVaruint32(Ctx); 710 uint32_t EndOffset = Section.Content.size(); 711 uint32_t PreviousOffset = 0; 712 while (RelocCount--) { 713 wasm::WasmRelocation Reloc = {}; 714 Reloc.Type = readVaruint32(Ctx); 715 Reloc.Offset = readVaruint32(Ctx); 716 if (Reloc.Offset < PreviousOffset) 717 return make_error<GenericBinaryError>("Relocations not in offset order", 718 object_error::parse_failed); 719 PreviousOffset = Reloc.Offset; 720 Reloc.Index = readVaruint32(Ctx); 721 switch (Reloc.Type) { 722 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB: 723 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB: 724 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: 725 if (!isValidFunctionSymbol(Reloc.Index)) 726 return make_error<GenericBinaryError>("Bad relocation function index", 727 object_error::parse_failed); 728 break; 729 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB: 730 if (Reloc.Index >= Signatures.size()) 731 return make_error<GenericBinaryError>("Bad relocation type index", 732 object_error::parse_failed); 733 break; 734 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: 735 if (!isValidGlobalSymbol(Reloc.Index)) 736 return make_error<GenericBinaryError>("Bad relocation global index", 737 object_error::parse_failed); 738 break; 739 case wasm::R_WEBASSEMBLY_EVENT_INDEX_LEB: 740 if (!isValidEventSymbol(Reloc.Index)) 741 return make_error<GenericBinaryError>("Bad relocation event index", 742 object_error::parse_failed); 743 break; 744 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: 745 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: 746 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: 747 if (!isValidDataSymbol(Reloc.Index)) 748 return make_error<GenericBinaryError>("Bad relocation data index", 749 object_error::parse_failed); 750 Reloc.Addend = readVarint32(Ctx); 751 break; 752 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: 753 if (!isValidFunctionSymbol(Reloc.Index)) 754 return make_error<GenericBinaryError>("Bad relocation function index", 755 object_error::parse_failed); 756 Reloc.Addend = readVarint32(Ctx); 757 break; 758 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: 759 if (!isValidSectionSymbol(Reloc.Index)) 760 return make_error<GenericBinaryError>("Bad relocation section index", 761 object_error::parse_failed); 762 Reloc.Addend = readVarint32(Ctx); 763 break; 764 default: 765 return make_error<GenericBinaryError>("Bad relocation type: " + 766 Twine(Reloc.Type), 767 object_error::parse_failed); 768 } 769 770 // Relocations must fit inside the section, and must appear in order. They 771 // also shouldn't overlap a function/element boundary, but we don't bother 772 // to check that. 773 uint64_t Size = 5; 774 if (Reloc.Type == wasm::R_WEBASSEMBLY_TABLE_INDEX_I32 || 775 Reloc.Type == wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32 || 776 Reloc.Type == wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32 || 777 Reloc.Type == wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32) 778 Size = 4; 779 if (Reloc.Offset + Size > EndOffset) 780 return make_error<GenericBinaryError>("Bad relocation offset", 781 object_error::parse_failed); 782 783 Section.Relocations.push_back(Reloc); 784 } 785 if (Ctx.Ptr != Ctx.End) 786 return make_error<GenericBinaryError>("Reloc section ended prematurely", 787 object_error::parse_failed); 788 return Error::success(); 789 } 790 791 Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) { 792 if (Sec.Name == "dylink") { 793 if (Error Err = parseDylinkSection(Ctx)) 794 return Err; 795 } else if (Sec.Name == "name") { 796 if (Error Err = parseNameSection(Ctx)) 797 return Err; 798 } else if (Sec.Name == "linking") { 799 if (Error Err = parseLinkingSection(Ctx)) 800 return Err; 801 } else if (Sec.Name == "producers") { 802 if (Error Err = parseProducersSection(Ctx)) 803 return Err; 804 } else if (Sec.Name.startswith("reloc.")) { 805 if (Error Err = parseRelocSection(Sec.Name, Ctx)) 806 return Err; 807 } 808 return Error::success(); 809 } 810 811 Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) { 812 uint32_t Count = readVaruint32(Ctx); 813 Signatures.reserve(Count); 814 while (Count--) { 815 wasm::WasmSignature Sig; 816 uint8_t Form = readUint8(Ctx); 817 if (Form != wasm::WASM_TYPE_FUNC) { 818 return make_error<GenericBinaryError>("Invalid signature type", 819 object_error::parse_failed); 820 } 821 uint32_t ParamCount = readVaruint32(Ctx); 822 Sig.Params.reserve(ParamCount); 823 while (ParamCount--) { 824 uint32_t ParamType = readUint8(Ctx); 825 Sig.Params.push_back(wasm::ValType(ParamType)); 826 } 827 uint32_t ReturnCount = readVaruint32(Ctx); 828 if (ReturnCount) { 829 if (ReturnCount != 1) { 830 return make_error<GenericBinaryError>( 831 "Multiple return types not supported", object_error::parse_failed); 832 } 833 Sig.Returns.push_back(wasm::ValType(readUint8(Ctx))); 834 } 835 Signatures.push_back(std::move(Sig)); 836 } 837 if (Ctx.Ptr != Ctx.End) 838 return make_error<GenericBinaryError>("Type section ended prematurely", 839 object_error::parse_failed); 840 return Error::success(); 841 } 842 843 Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { 844 uint32_t Count = readVaruint32(Ctx); 845 Imports.reserve(Count); 846 for (uint32_t i = 0; i < Count; i++) { 847 wasm::WasmImport Im; 848 Im.Module = readString(Ctx); 849 Im.Field = readString(Ctx); 850 Im.Kind = readUint8(Ctx); 851 switch (Im.Kind) { 852 case wasm::WASM_EXTERNAL_FUNCTION: 853 NumImportedFunctions++; 854 Im.SigIndex = readVaruint32(Ctx); 855 break; 856 case wasm::WASM_EXTERNAL_GLOBAL: 857 NumImportedGlobals++; 858 Im.Global.Type = readUint8(Ctx); 859 Im.Global.Mutable = readVaruint1(Ctx); 860 break; 861 case wasm::WASM_EXTERNAL_MEMORY: 862 Im.Memory = readLimits(Ctx); 863 break; 864 case wasm::WASM_EXTERNAL_TABLE: 865 Im.Table = readTable(Ctx); 866 if (Im.Table.ElemType != wasm::WASM_TYPE_FUNCREF) 867 return make_error<GenericBinaryError>("Invalid table element type", 868 object_error::parse_failed); 869 break; 870 case wasm::WASM_EXTERNAL_EVENT: 871 NumImportedEvents++; 872 Im.Event.Attribute = readVarint32(Ctx); 873 Im.Event.SigIndex = readVarint32(Ctx); 874 break; 875 default: 876 return make_error<GenericBinaryError>("Unexpected import kind", 877 object_error::parse_failed); 878 } 879 Imports.push_back(Im); 880 } 881 if (Ctx.Ptr != Ctx.End) 882 return make_error<GenericBinaryError>("Import section ended prematurely", 883 object_error::parse_failed); 884 return Error::success(); 885 } 886 887 Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) { 888 uint32_t Count = readVaruint32(Ctx); 889 FunctionTypes.reserve(Count); 890 uint32_t NumTypes = Signatures.size(); 891 while (Count--) { 892 uint32_t Type = readVaruint32(Ctx); 893 if (Type >= NumTypes) 894 return make_error<GenericBinaryError>("Invalid function type", 895 object_error::parse_failed); 896 FunctionTypes.push_back(Type); 897 } 898 if (Ctx.Ptr != Ctx.End) 899 return make_error<GenericBinaryError>("Function section ended prematurely", 900 object_error::parse_failed); 901 return Error::success(); 902 } 903 904 Error WasmObjectFile::parseTableSection(ReadContext &Ctx) { 905 uint32_t Count = readVaruint32(Ctx); 906 Tables.reserve(Count); 907 while (Count--) { 908 Tables.push_back(readTable(Ctx)); 909 if (Tables.back().ElemType != wasm::WASM_TYPE_FUNCREF) { 910 return make_error<GenericBinaryError>("Invalid table element type", 911 object_error::parse_failed); 912 } 913 } 914 if (Ctx.Ptr != Ctx.End) 915 return make_error<GenericBinaryError>("Table section ended prematurely", 916 object_error::parse_failed); 917 return Error::success(); 918 } 919 920 Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) { 921 uint32_t Count = readVaruint32(Ctx); 922 Memories.reserve(Count); 923 while (Count--) { 924 Memories.push_back(readLimits(Ctx)); 925 } 926 if (Ctx.Ptr != Ctx.End) 927 return make_error<GenericBinaryError>("Memory section ended prematurely", 928 object_error::parse_failed); 929 return Error::success(); 930 } 931 932 Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) { 933 GlobalSection = Sections.size(); 934 uint32_t Count = readVaruint32(Ctx); 935 Globals.reserve(Count); 936 while (Count--) { 937 wasm::WasmGlobal Global; 938 Global.Index = NumImportedGlobals + Globals.size(); 939 Global.Type.Type = readUint8(Ctx); 940 Global.Type.Mutable = readVaruint1(Ctx); 941 if (Error Err = readInitExpr(Global.InitExpr, Ctx)) 942 return Err; 943 Globals.push_back(Global); 944 } 945 if (Ctx.Ptr != Ctx.End) 946 return make_error<GenericBinaryError>("Global section ended prematurely", 947 object_error::parse_failed); 948 return Error::success(); 949 } 950 951 Error WasmObjectFile::parseEventSection(ReadContext &Ctx) { 952 EventSection = Sections.size(); 953 uint32_t Count = readVarint32(Ctx); 954 Events.reserve(Count); 955 while (Count--) { 956 wasm::WasmEvent Event; 957 Event.Index = NumImportedEvents + Events.size(); 958 Event.Type.Attribute = readVaruint32(Ctx); 959 Event.Type.SigIndex = readVarint32(Ctx); 960 Events.push_back(Event); 961 } 962 963 if (Ctx.Ptr != Ctx.End) 964 return make_error<GenericBinaryError>("Event section ended prematurely", 965 object_error::parse_failed); 966 return Error::success(); 967 } 968 969 Error WasmObjectFile::parseExportSection(ReadContext &Ctx) { 970 uint32_t Count = readVaruint32(Ctx); 971 Exports.reserve(Count); 972 for (uint32_t i = 0; i < Count; i++) { 973 wasm::WasmExport Ex; 974 Ex.Name = readString(Ctx); 975 Ex.Kind = readUint8(Ctx); 976 Ex.Index = readVaruint32(Ctx); 977 switch (Ex.Kind) { 978 case wasm::WASM_EXTERNAL_FUNCTION: 979 if (!isValidFunctionIndex(Ex.Index)) 980 return make_error<GenericBinaryError>("Invalid function export", 981 object_error::parse_failed); 982 break; 983 case wasm::WASM_EXTERNAL_GLOBAL: 984 if (!isValidGlobalIndex(Ex.Index)) 985 return make_error<GenericBinaryError>("Invalid global export", 986 object_error::parse_failed); 987 break; 988 case wasm::WASM_EXTERNAL_EVENT: 989 if (!isValidEventIndex(Ex.Index)) 990 return make_error<GenericBinaryError>("Invalid event export", 991 object_error::parse_failed); 992 break; 993 case wasm::WASM_EXTERNAL_MEMORY: 994 case wasm::WASM_EXTERNAL_TABLE: 995 break; 996 default: 997 return make_error<GenericBinaryError>("Unexpected export kind", 998 object_error::parse_failed); 999 } 1000 Exports.push_back(Ex); 1001 } 1002 if (Ctx.Ptr != Ctx.End) 1003 return make_error<GenericBinaryError>("Export section ended prematurely", 1004 object_error::parse_failed); 1005 return Error::success(); 1006 } 1007 1008 bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const { 1009 return Index < NumImportedFunctions + FunctionTypes.size(); 1010 } 1011 1012 bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const { 1013 return Index >= NumImportedFunctions && isValidFunctionIndex(Index); 1014 } 1015 1016 bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const { 1017 return Index < NumImportedGlobals + Globals.size(); 1018 } 1019 1020 bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const { 1021 return Index >= NumImportedGlobals && isValidGlobalIndex(Index); 1022 } 1023 1024 bool WasmObjectFile::isValidEventIndex(uint32_t Index) const { 1025 return Index < NumImportedEvents + Events.size(); 1026 } 1027 1028 bool WasmObjectFile::isDefinedEventIndex(uint32_t Index) const { 1029 return Index >= NumImportedEvents && isValidEventIndex(Index); 1030 } 1031 1032 bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const { 1033 return Index < Symbols.size() && Symbols[Index].isTypeFunction(); 1034 } 1035 1036 bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const { 1037 return Index < Symbols.size() && Symbols[Index].isTypeGlobal(); 1038 } 1039 1040 bool WasmObjectFile::isValidEventSymbol(uint32_t Index) const { 1041 return Index < Symbols.size() && Symbols[Index].isTypeEvent(); 1042 } 1043 1044 bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const { 1045 return Index < Symbols.size() && Symbols[Index].isTypeData(); 1046 } 1047 1048 bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const { 1049 return Index < Symbols.size() && Symbols[Index].isTypeSection(); 1050 } 1051 1052 wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) { 1053 assert(isDefinedFunctionIndex(Index)); 1054 return Functions[Index - NumImportedFunctions]; 1055 } 1056 1057 const wasm::WasmFunction & 1058 WasmObjectFile::getDefinedFunction(uint32_t Index) const { 1059 assert(isDefinedFunctionIndex(Index)); 1060 return Functions[Index - NumImportedFunctions]; 1061 } 1062 1063 wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) { 1064 assert(isDefinedGlobalIndex(Index)); 1065 return Globals[Index - NumImportedGlobals]; 1066 } 1067 1068 wasm::WasmEvent &WasmObjectFile::getDefinedEvent(uint32_t Index) { 1069 assert(isDefinedEventIndex(Index)); 1070 return Events[Index - NumImportedEvents]; 1071 } 1072 1073 Error WasmObjectFile::parseStartSection(ReadContext &Ctx) { 1074 StartFunction = readVaruint32(Ctx); 1075 if (!isValidFunctionIndex(StartFunction)) 1076 return make_error<GenericBinaryError>("Invalid start function", 1077 object_error::parse_failed); 1078 return Error::success(); 1079 } 1080 1081 Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) { 1082 CodeSection = Sections.size(); 1083 uint32_t FunctionCount = readVaruint32(Ctx); 1084 if (FunctionCount != FunctionTypes.size()) { 1085 return make_error<GenericBinaryError>("Invalid function count", 1086 object_error::parse_failed); 1087 } 1088 1089 while (FunctionCount--) { 1090 wasm::WasmFunction Function; 1091 const uint8_t *FunctionStart = Ctx.Ptr; 1092 uint32_t Size = readVaruint32(Ctx); 1093 const uint8_t *FunctionEnd = Ctx.Ptr + Size; 1094 1095 Function.CodeOffset = Ctx.Ptr - FunctionStart; 1096 Function.Index = NumImportedFunctions + Functions.size(); 1097 Function.CodeSectionOffset = FunctionStart - Ctx.Start; 1098 Function.Size = FunctionEnd - FunctionStart; 1099 1100 uint32_t NumLocalDecls = readVaruint32(Ctx); 1101 Function.Locals.reserve(NumLocalDecls); 1102 while (NumLocalDecls--) { 1103 wasm::WasmLocalDecl Decl; 1104 Decl.Count = readVaruint32(Ctx); 1105 Decl.Type = readUint8(Ctx); 1106 Function.Locals.push_back(Decl); 1107 } 1108 1109 uint32_t BodySize = FunctionEnd - Ctx.Ptr; 1110 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize); 1111 // This will be set later when reading in the linking metadata section. 1112 Function.Comdat = UINT32_MAX; 1113 Ctx.Ptr += BodySize; 1114 assert(Ctx.Ptr == FunctionEnd); 1115 Functions.push_back(Function); 1116 } 1117 if (Ctx.Ptr != Ctx.End) 1118 return make_error<GenericBinaryError>("Code section ended prematurely", 1119 object_error::parse_failed); 1120 return Error::success(); 1121 } 1122 1123 Error WasmObjectFile::parseElemSection(ReadContext &Ctx) { 1124 uint32_t Count = readVaruint32(Ctx); 1125 ElemSegments.reserve(Count); 1126 while (Count--) { 1127 wasm::WasmElemSegment Segment; 1128 Segment.TableIndex = readVaruint32(Ctx); 1129 if (Segment.TableIndex != 0) { 1130 return make_error<GenericBinaryError>("Invalid TableIndex", 1131 object_error::parse_failed); 1132 } 1133 if (Error Err = readInitExpr(Segment.Offset, Ctx)) 1134 return Err; 1135 uint32_t NumElems = readVaruint32(Ctx); 1136 while (NumElems--) { 1137 Segment.Functions.push_back(readVaruint32(Ctx)); 1138 } 1139 ElemSegments.push_back(Segment); 1140 } 1141 if (Ctx.Ptr != Ctx.End) 1142 return make_error<GenericBinaryError>("Elem section ended prematurely", 1143 object_error::parse_failed); 1144 return Error::success(); 1145 } 1146 1147 Error WasmObjectFile::parseDataSection(ReadContext &Ctx) { 1148 DataSection = Sections.size(); 1149 uint32_t Count = readVaruint32(Ctx); 1150 DataSegments.reserve(Count); 1151 while (Count--) { 1152 WasmSegment Segment; 1153 Segment.Data.MemoryIndex = readVaruint32(Ctx); 1154 if (Error Err = readInitExpr(Segment.Data.Offset, Ctx)) 1155 return Err; 1156 uint32_t Size = readVaruint32(Ctx); 1157 if (Size > (size_t)(Ctx.End - Ctx.Ptr)) 1158 return make_error<GenericBinaryError>("Invalid segment size", 1159 object_error::parse_failed); 1160 Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size); 1161 // The rest of these Data fields are set later, when reading in the linking 1162 // metadata section. 1163 Segment.Data.Alignment = 0; 1164 Segment.Data.Flags = 0; 1165 Segment.Data.Comdat = UINT32_MAX; 1166 Segment.SectionOffset = Ctx.Ptr - Ctx.Start; 1167 Ctx.Ptr += Size; 1168 DataSegments.push_back(Segment); 1169 } 1170 if (Ctx.Ptr != Ctx.End) 1171 return make_error<GenericBinaryError>("Data section ended prematurely", 1172 object_error::parse_failed); 1173 return Error::success(); 1174 } 1175 1176 const uint8_t *WasmObjectFile::getPtr(size_t Offset) const { 1177 return reinterpret_cast<const uint8_t *>(getData().data() + Offset); 1178 } 1179 1180 const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const { 1181 return Header; 1182 } 1183 1184 void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.a++; } 1185 1186 uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const { 1187 uint32_t Result = SymbolRef::SF_None; 1188 const WasmSymbol &Sym = getWasmSymbol(Symb); 1189 1190 LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n"); 1191 if (Sym.isBindingWeak()) 1192 Result |= SymbolRef::SF_Weak; 1193 if (!Sym.isBindingLocal()) 1194 Result |= SymbolRef::SF_Global; 1195 if (Sym.isHidden()) 1196 Result |= SymbolRef::SF_Hidden; 1197 if (!Sym.isDefined()) 1198 Result |= SymbolRef::SF_Undefined; 1199 if (Sym.isTypeFunction()) 1200 Result |= SymbolRef::SF_Executable; 1201 return Result; 1202 } 1203 1204 basic_symbol_iterator WasmObjectFile::symbol_begin() const { 1205 DataRefImpl Ref; 1206 Ref.d.a = 0; 1207 return BasicSymbolRef(Ref, this); 1208 } 1209 1210 basic_symbol_iterator WasmObjectFile::symbol_end() const { 1211 DataRefImpl Ref; 1212 Ref.d.a = Symbols.size(); 1213 return BasicSymbolRef(Ref, this); 1214 } 1215 1216 const WasmSymbol &WasmObjectFile::getWasmSymbol(const DataRefImpl &Symb) const { 1217 return Symbols[Symb.d.a]; 1218 } 1219 1220 const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const { 1221 return getWasmSymbol(Symb.getRawDataRefImpl()); 1222 } 1223 1224 Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const { 1225 return getWasmSymbol(Symb).Info.Name; 1226 } 1227 1228 Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const { 1229 auto &Sym = getWasmSymbol(Symb); 1230 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION && 1231 isDefinedFunctionIndex(Sym.Info.ElementIndex)) 1232 return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset; 1233 else 1234 return getSymbolValue(Symb); 1235 } 1236 1237 uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol &Sym) const { 1238 switch (Sym.Info.Kind) { 1239 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1240 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1241 case wasm::WASM_SYMBOL_TYPE_EVENT: 1242 return Sym.Info.ElementIndex; 1243 case wasm::WASM_SYMBOL_TYPE_DATA: { 1244 // The value of a data symbol is the segment offset, plus the symbol 1245 // offset within the segment. 1246 uint32_t SegmentIndex = Sym.Info.DataRef.Segment; 1247 const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data; 1248 assert(Segment.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST); 1249 return Segment.Offset.Value.Int32 + Sym.Info.DataRef.Offset; 1250 } 1251 case wasm::WASM_SYMBOL_TYPE_SECTION: 1252 return 0; 1253 } 1254 llvm_unreachable("invalid symbol type"); 1255 } 1256 1257 uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const { 1258 return getWasmSymbolValue(getWasmSymbol(Symb)); 1259 } 1260 1261 uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const { 1262 llvm_unreachable("not yet implemented"); 1263 return 0; 1264 } 1265 1266 uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const { 1267 llvm_unreachable("not yet implemented"); 1268 return 0; 1269 } 1270 1271 Expected<SymbolRef::Type> 1272 WasmObjectFile::getSymbolType(DataRefImpl Symb) const { 1273 const WasmSymbol &Sym = getWasmSymbol(Symb); 1274 1275 switch (Sym.Info.Kind) { 1276 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1277 return SymbolRef::ST_Function; 1278 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1279 return SymbolRef::ST_Other; 1280 case wasm::WASM_SYMBOL_TYPE_DATA: 1281 return SymbolRef::ST_Data; 1282 case wasm::WASM_SYMBOL_TYPE_SECTION: 1283 return SymbolRef::ST_Debug; 1284 case wasm::WASM_SYMBOL_TYPE_EVENT: 1285 return SymbolRef::ST_Other; 1286 } 1287 1288 llvm_unreachable("Unknown WasmSymbol::SymbolType"); 1289 return SymbolRef::ST_Other; 1290 } 1291 1292 Expected<section_iterator> 1293 WasmObjectFile::getSymbolSection(DataRefImpl Symb) const { 1294 const WasmSymbol &Sym = getWasmSymbol(Symb); 1295 if (Sym.isUndefined()) 1296 return section_end(); 1297 1298 DataRefImpl Ref; 1299 switch (Sym.Info.Kind) { 1300 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1301 Ref.d.a = CodeSection; 1302 break; 1303 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1304 Ref.d.a = GlobalSection; 1305 break; 1306 case wasm::WASM_SYMBOL_TYPE_DATA: 1307 Ref.d.a = DataSection; 1308 break; 1309 case wasm::WASM_SYMBOL_TYPE_SECTION: 1310 Ref.d.a = Sym.Info.ElementIndex; 1311 break; 1312 case wasm::WASM_SYMBOL_TYPE_EVENT: 1313 Ref.d.a = EventSection; 1314 break; 1315 default: 1316 llvm_unreachable("Unknown WasmSymbol::SymbolType"); 1317 } 1318 return section_iterator(SectionRef(Ref, this)); 1319 } 1320 1321 void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; } 1322 1323 std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec, 1324 StringRef &Res) const { 1325 const WasmSection &S = Sections[Sec.d.a]; 1326 #define ECase(X) \ 1327 case wasm::WASM_SEC_##X: \ 1328 Res = #X; \ 1329 break 1330 switch (S.Type) { 1331 ECase(TYPE); 1332 ECase(IMPORT); 1333 ECase(FUNCTION); 1334 ECase(TABLE); 1335 ECase(MEMORY); 1336 ECase(GLOBAL); 1337 ECase(EVENT); 1338 ECase(EXPORT); 1339 ECase(START); 1340 ECase(ELEM); 1341 ECase(CODE); 1342 ECase(DATA); 1343 case wasm::WASM_SEC_CUSTOM: 1344 Res = S.Name; 1345 break; 1346 default: 1347 return object_error::invalid_section_index; 1348 } 1349 #undef ECase 1350 return std::error_code(); 1351 } 1352 1353 uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; } 1354 1355 uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const { 1356 return Sec.d.a; 1357 } 1358 1359 uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const { 1360 const WasmSection &S = Sections[Sec.d.a]; 1361 return S.Content.size(); 1362 } 1363 1364 std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec, 1365 StringRef &Res) const { 1366 const WasmSection &S = Sections[Sec.d.a]; 1367 // This will never fail since wasm sections can never be empty (user-sections 1368 // must have a name and non-user sections each have a defined structure). 1369 Res = StringRef(reinterpret_cast<const char *>(S.Content.data()), 1370 S.Content.size()); 1371 return std::error_code(); 1372 } 1373 1374 uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const { 1375 return 1; 1376 } 1377 1378 bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const { 1379 return false; 1380 } 1381 1382 bool WasmObjectFile::isSectionText(DataRefImpl Sec) const { 1383 return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE; 1384 } 1385 1386 bool WasmObjectFile::isSectionData(DataRefImpl Sec) const { 1387 return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA; 1388 } 1389 1390 bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; } 1391 1392 bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; } 1393 1394 bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; } 1395 1396 relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const { 1397 DataRefImpl RelocRef; 1398 RelocRef.d.a = Ref.d.a; 1399 RelocRef.d.b = 0; 1400 return relocation_iterator(RelocationRef(RelocRef, this)); 1401 } 1402 1403 relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const { 1404 const WasmSection &Sec = getWasmSection(Ref); 1405 DataRefImpl RelocRef; 1406 RelocRef.d.a = Ref.d.a; 1407 RelocRef.d.b = Sec.Relocations.size(); 1408 return relocation_iterator(RelocationRef(RelocRef, this)); 1409 } 1410 1411 void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const { Rel.d.b++; } 1412 1413 uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const { 1414 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1415 return Rel.Offset; 1416 } 1417 1418 symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Ref) const { 1419 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1420 if (Rel.Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB) 1421 return symbol_end(); 1422 DataRefImpl Sym; 1423 Sym.d.a = Rel.Index; 1424 Sym.d.b = 0; 1425 return symbol_iterator(SymbolRef(Sym, this)); 1426 } 1427 1428 uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const { 1429 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1430 return Rel.Type; 1431 } 1432 1433 void WasmObjectFile::getRelocationTypeName( 1434 DataRefImpl Ref, SmallVectorImpl<char> &Result) const { 1435 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1436 StringRef Res = "Unknown"; 1437 1438 #define WASM_RELOC(name, value) \ 1439 case wasm::name: \ 1440 Res = #name; \ 1441 break; 1442 1443 switch (Rel.Type) { 1444 #include "llvm/BinaryFormat/WasmRelocs.def" 1445 } 1446 1447 #undef WASM_RELOC 1448 1449 Result.append(Res.begin(), Res.end()); 1450 } 1451 1452 section_iterator WasmObjectFile::section_begin() const { 1453 DataRefImpl Ref; 1454 Ref.d.a = 0; 1455 return section_iterator(SectionRef(Ref, this)); 1456 } 1457 1458 section_iterator WasmObjectFile::section_end() const { 1459 DataRefImpl Ref; 1460 Ref.d.a = Sections.size(); 1461 return section_iterator(SectionRef(Ref, this)); 1462 } 1463 1464 uint8_t WasmObjectFile::getBytesInAddress() const { return 4; } 1465 1466 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; } 1467 1468 Triple::ArchType WasmObjectFile::getArch() const { return Triple::wasm32; } 1469 1470 SubtargetFeatures WasmObjectFile::getFeatures() const { 1471 return SubtargetFeatures(); 1472 } 1473 1474 bool WasmObjectFile::isRelocatableObject() const { return HasLinkingSection; } 1475 1476 bool WasmObjectFile::isSharedObject() const { return HasDylinkSection; } 1477 1478 const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const { 1479 assert(Ref.d.a < Sections.size()); 1480 return Sections[Ref.d.a]; 1481 } 1482 1483 const WasmSection & 1484 WasmObjectFile::getWasmSection(const SectionRef &Section) const { 1485 return getWasmSection(Section.getRawDataRefImpl()); 1486 } 1487 1488 const wasm::WasmRelocation & 1489 WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const { 1490 return getWasmRelocation(Ref.getRawDataRefImpl()); 1491 } 1492 1493 const wasm::WasmRelocation & 1494 WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const { 1495 assert(Ref.d.a < Sections.size()); 1496 const WasmSection &Sec = Sections[Ref.d.a]; 1497 assert(Ref.d.b < Sec.Relocations.size()); 1498 return Sec.Relocations[Ref.d.b]; 1499 } 1500 1501 int WasmSectionOrderChecker::getSectionOrder(unsigned ID, 1502 StringRef CustomSectionName) { 1503 switch (ID) { 1504 case wasm::WASM_SEC_CUSTOM: 1505 return StringSwitch<unsigned>(CustomSectionName) 1506 .Case("dylink", WASM_SEC_ORDER_DYLINK) 1507 .Case("linking", WASM_SEC_ORDER_LINKING) 1508 .StartsWith("reloc.", WASM_SEC_ORDER_RELOC) 1509 .Case("name", WASM_SEC_ORDER_NAME) 1510 .Case("producers", WASM_SEC_ORDER_PRODUCERS) 1511 .Default(-1); 1512 case wasm::WASM_SEC_TYPE: 1513 return WASM_SEC_ORDER_TYPE; 1514 case wasm::WASM_SEC_IMPORT: 1515 return WASM_SEC_ORDER_IMPORT; 1516 case wasm::WASM_SEC_FUNCTION: 1517 return WASM_SEC_ORDER_FUNCTION; 1518 case wasm::WASM_SEC_TABLE: 1519 return WASM_SEC_ORDER_TABLE; 1520 case wasm::WASM_SEC_MEMORY: 1521 return WASM_SEC_ORDER_MEMORY; 1522 case wasm::WASM_SEC_GLOBAL: 1523 return WASM_SEC_ORDER_GLOBAL; 1524 case wasm::WASM_SEC_EXPORT: 1525 return WASM_SEC_ORDER_EXPORT; 1526 case wasm::WASM_SEC_START: 1527 return WASM_SEC_ORDER_START; 1528 case wasm::WASM_SEC_ELEM: 1529 return WASM_SEC_ORDER_ELEM; 1530 case wasm::WASM_SEC_CODE: 1531 return WASM_SEC_ORDER_CODE; 1532 case wasm::WASM_SEC_DATA: 1533 return WASM_SEC_ORDER_DATA; 1534 case wasm::WASM_SEC_DATACOUNT: 1535 return WASM_SEC_ORDER_DATACOUNT; 1536 case wasm::WASM_SEC_EVENT: 1537 return WASM_SEC_ORDER_EVENT; 1538 default: 1539 llvm_unreachable("invalid section"); 1540 } 1541 } 1542 1543 bool WasmSectionOrderChecker::isValidSectionOrder(unsigned ID, 1544 StringRef CustomSectionName) { 1545 int Order = getSectionOrder(ID, CustomSectionName); 1546 if (Order == -1) // Skip unknown sections 1547 return true; 1548 // There can be multiple "reloc." sections. Otherwise there shouldn't be any 1549 // duplicate section orders. 1550 bool IsValid = (LastOrder == Order && Order == WASM_SEC_ORDER_RELOC) || 1551 LastOrder < Order; 1552 LastOrder = Order; 1553 return IsValid; 1554 } 1555