1 //===- WasmObjectFile.cpp - Wasm object file implementation ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/ADT/ArrayRef.h" 11 #include "llvm/ADT/DenseSet.h" 12 #include "llvm/ADT/STLExtras.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 <algorithm> 28 #include <cassert> 29 #include <cstdint> 30 #include <cstring> 31 #include <system_error> 32 33 #define DEBUG_TYPE "wasm-object" 34 35 using namespace llvm; 36 using namespace object; 37 38 void WasmSymbol::print(raw_ostream &Out) const { 39 Out << "Name=" << Info.Name 40 << ", Kind=" << toString(wasm::WasmSymbolType(Info.Kind)) 41 << ", Flags=" << Info.Flags; 42 if (!isTypeData()) { 43 Out << ", ElemIndex=" << Info.ElementIndex; 44 } else if (isDefined()) { 45 Out << ", Segment=" << Info.DataRef.Segment; 46 Out << ", Offset=" << Info.DataRef.Offset; 47 Out << ", Size=" << Info.DataRef.Size; 48 } 49 } 50 51 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 52 LLVM_DUMP_METHOD void WasmSymbol::dump() const { print(dbgs()); } 53 #endif 54 55 Expected<std::unique_ptr<WasmObjectFile>> 56 ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) { 57 Error Err = Error::success(); 58 auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err); 59 if (Err) 60 return std::move(Err); 61 62 return std::move(ObjectFile); 63 } 64 65 #define VARINT7_MAX ((1<<7)-1) 66 #define VARINT7_MIN (-(1<<7)) 67 #define VARUINT7_MAX (1<<7) 68 #define VARUINT1_MAX (1) 69 70 static uint8_t readUint8(WasmObjectFile::ReadContext &Ctx) { 71 if (Ctx.Ptr == Ctx.End) 72 report_fatal_error("EOF while reading uint8"); 73 return *Ctx.Ptr++; 74 } 75 76 static uint32_t readUint32(WasmObjectFile::ReadContext &Ctx) { 77 if (Ctx.Ptr + 4 > Ctx.End) 78 report_fatal_error("EOF while reading uint32"); 79 uint32_t Result = support::endian::read32le(Ctx.Ptr); 80 Ctx.Ptr += 4; 81 return Result; 82 } 83 84 static int32_t readFloat32(WasmObjectFile::ReadContext &Ctx) { 85 int32_t Result = 0; 86 memcpy(&Result, Ctx.Ptr, sizeof(Result)); 87 Ctx.Ptr += sizeof(Result); 88 return Result; 89 } 90 91 static int64_t readFloat64(WasmObjectFile::ReadContext &Ctx) { 92 int64_t Result = 0; 93 memcpy(&Result, Ctx.Ptr, sizeof(Result)); 94 Ctx.Ptr += sizeof(Result); 95 return Result; 96 } 97 98 static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx) { 99 unsigned Count; 100 const char* Error = nullptr; 101 uint64_t Result = decodeULEB128(Ctx.Ptr, &Count, Ctx.End, &Error); 102 if (Error) 103 report_fatal_error(Error); 104 Ctx.Ptr += Count; 105 return Result; 106 } 107 108 static StringRef readString(WasmObjectFile::ReadContext &Ctx) { 109 uint32_t StringLen = readULEB128(Ctx); 110 if (Ctx.Ptr + StringLen > Ctx.End) 111 report_fatal_error("EOF while reading string"); 112 StringRef Return = 113 StringRef(reinterpret_cast<const char *>(Ctx.Ptr), StringLen); 114 Ctx.Ptr += StringLen; 115 return Return; 116 } 117 118 static int64_t readLEB128(WasmObjectFile::ReadContext &Ctx) { 119 unsigned Count; 120 const char* Error = nullptr; 121 uint64_t Result = decodeSLEB128(Ctx.Ptr, &Count, Ctx.End, &Error); 122 if (Error) 123 report_fatal_error(Error); 124 Ctx.Ptr += Count; 125 return Result; 126 } 127 128 static uint8_t readVaruint1(WasmObjectFile::ReadContext &Ctx) { 129 int64_t result = readLEB128(Ctx); 130 if (result > VARUINT1_MAX || result < 0) 131 report_fatal_error("LEB is outside Varuint1 range"); 132 return result; 133 } 134 135 static int32_t readVarint32(WasmObjectFile::ReadContext &Ctx) { 136 int64_t result = readLEB128(Ctx); 137 if (result > INT32_MAX || result < INT32_MIN) 138 report_fatal_error("LEB is outside Varint32 range"); 139 return result; 140 } 141 142 static uint32_t readVaruint32(WasmObjectFile::ReadContext &Ctx) { 143 uint64_t result = readULEB128(Ctx); 144 if (result > UINT32_MAX) 145 report_fatal_error("LEB is outside Varuint32 range"); 146 return result; 147 } 148 149 static int64_t readVarint64(WasmObjectFile::ReadContext &Ctx) { 150 return readLEB128(Ctx); 151 } 152 153 static uint8_t readOpcode(WasmObjectFile::ReadContext &Ctx) { 154 return readUint8(Ctx); 155 } 156 157 static Error readInitExpr(wasm::WasmInitExpr &Expr, 158 WasmObjectFile::ReadContext &Ctx) { 159 Expr.Opcode = readOpcode(Ctx); 160 161 switch (Expr.Opcode) { 162 case wasm::WASM_OPCODE_I32_CONST: 163 Expr.Value.Int32 = readVarint32(Ctx); 164 break; 165 case wasm::WASM_OPCODE_I64_CONST: 166 Expr.Value.Int64 = readVarint64(Ctx); 167 break; 168 case wasm::WASM_OPCODE_F32_CONST: 169 Expr.Value.Float32 = readFloat32(Ctx); 170 break; 171 case wasm::WASM_OPCODE_F64_CONST: 172 Expr.Value.Float64 = readFloat64(Ctx); 173 break; 174 case wasm::WASM_OPCODE_GET_GLOBAL: 175 Expr.Value.Global = readULEB128(Ctx); 176 break; 177 default: 178 return make_error<GenericBinaryError>("Invalid opcode in init_expr", 179 object_error::parse_failed); 180 } 181 182 uint8_t EndOpcode = readOpcode(Ctx); 183 if (EndOpcode != wasm::WASM_OPCODE_END) { 184 return make_error<GenericBinaryError>("Invalid init_expr", 185 object_error::parse_failed); 186 } 187 return Error::success(); 188 } 189 190 static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) { 191 wasm::WasmLimits Result; 192 Result.Flags = readVaruint1(Ctx); 193 Result.Initial = readVaruint32(Ctx); 194 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) 195 Result.Maximum = readVaruint32(Ctx); 196 return Result; 197 } 198 199 static wasm::WasmTable readTable(WasmObjectFile::ReadContext &Ctx) { 200 wasm::WasmTable Table; 201 Table.ElemType = readUint8(Ctx); 202 Table.Limits = readLimits(Ctx); 203 return Table; 204 } 205 206 static Error readSection(WasmSection &Section, 207 WasmObjectFile::ReadContext &Ctx) { 208 Section.Offset = Ctx.Ptr - Ctx.Start; 209 Section.Type = readUint8(Ctx); 210 LLVM_DEBUG(dbgs() << "readSection type=" << Section.Type << "\n"); 211 uint32_t Size = readVaruint32(Ctx); 212 if (Size == 0) 213 return make_error<StringError>("Zero length section", 214 object_error::parse_failed); 215 if (Ctx.Ptr + Size > Ctx.End) 216 return make_error<StringError>("Section too large", 217 object_error::parse_failed); 218 if (Section.Type == wasm::WASM_SEC_CUSTOM) { 219 WasmObjectFile::ReadContext SectionCtx; 220 SectionCtx.Start = Ctx.Ptr; 221 SectionCtx.Ptr = Ctx.Ptr; 222 SectionCtx.End = Ctx.Ptr + Size; 223 224 Section.Name = readString(SectionCtx); 225 226 uint32_t SectionNameSize = SectionCtx.Ptr - SectionCtx.Start; 227 Ctx.Ptr += SectionNameSize; 228 Size -= SectionNameSize; 229 } 230 Section.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size); 231 Ctx.Ptr += Size; 232 return Error::success(); 233 } 234 235 WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err) 236 : ObjectFile(Binary::ID_Wasm, Buffer) { 237 ErrorAsOutParameter ErrAsOutParam(&Err); 238 Header.Magic = getData().substr(0, 4); 239 if (Header.Magic != StringRef("\0asm", 4)) { 240 Err = make_error<StringError>("Bad magic number", 241 object_error::parse_failed); 242 return; 243 } 244 245 ReadContext Ctx; 246 Ctx.Start = getPtr(0); 247 Ctx.Ptr = Ctx.Start + 4; 248 Ctx.End = Ctx.Start + getData().size(); 249 250 if (Ctx.Ptr + 4 > Ctx.End) { 251 Err = make_error<StringError>("Missing version number", 252 object_error::parse_failed); 253 return; 254 } 255 256 Header.Version = readUint32(Ctx); 257 if (Header.Version != wasm::WasmVersion) { 258 Err = make_error<StringError>("Bad version number", 259 object_error::parse_failed); 260 return; 261 } 262 263 WasmSection Sec; 264 while (Ctx.Ptr < Ctx.End) { 265 if ((Err = readSection(Sec, Ctx))) 266 return; 267 if ((Err = parseSection(Sec))) 268 return; 269 270 Sections.push_back(Sec); 271 } 272 } 273 274 Error WasmObjectFile::parseSection(WasmSection &Sec) { 275 ReadContext Ctx; 276 Ctx.Start = Sec.Content.data(); 277 Ctx.End = Ctx.Start + Sec.Content.size(); 278 Ctx.Ptr = Ctx.Start; 279 switch (Sec.Type) { 280 case wasm::WASM_SEC_CUSTOM: 281 return parseCustomSection(Sec, Ctx); 282 case wasm::WASM_SEC_TYPE: 283 return parseTypeSection(Ctx); 284 case wasm::WASM_SEC_IMPORT: 285 return parseImportSection(Ctx); 286 case wasm::WASM_SEC_FUNCTION: 287 return parseFunctionSection(Ctx); 288 case wasm::WASM_SEC_TABLE: 289 return parseTableSection(Ctx); 290 case wasm::WASM_SEC_MEMORY: 291 return parseMemorySection(Ctx); 292 case wasm::WASM_SEC_GLOBAL: 293 return parseGlobalSection(Ctx); 294 case wasm::WASM_SEC_EXPORT: 295 return parseExportSection(Ctx); 296 case wasm::WASM_SEC_START: 297 return parseStartSection(Ctx); 298 case wasm::WASM_SEC_ELEM: 299 return parseElemSection(Ctx); 300 case wasm::WASM_SEC_CODE: 301 return parseCodeSection(Ctx); 302 case wasm::WASM_SEC_DATA: 303 return parseDataSection(Ctx); 304 default: 305 return make_error<GenericBinaryError>("Bad section type", 306 object_error::parse_failed); 307 } 308 } 309 310 Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { 311 llvm::DenseSet<uint64_t> Seen; 312 if (Functions.size() != FunctionTypes.size()) { 313 return make_error<GenericBinaryError>("Names must come after code section", 314 object_error::parse_failed); 315 } 316 317 while (Ctx.Ptr < Ctx.End) { 318 uint8_t Type = readUint8(Ctx); 319 uint32_t Size = readVaruint32(Ctx); 320 const uint8_t *SubSectionEnd = Ctx.Ptr + Size; 321 switch (Type) { 322 case wasm::WASM_NAMES_FUNCTION: { 323 uint32_t Count = readVaruint32(Ctx); 324 while (Count--) { 325 uint32_t Index = readVaruint32(Ctx); 326 if (!Seen.insert(Index).second) 327 return make_error<GenericBinaryError>("Function named more than once", 328 object_error::parse_failed); 329 StringRef Name = readString(Ctx); 330 if (!isValidFunctionIndex(Index) || Name.empty()) 331 return make_error<GenericBinaryError>("Invalid name entry", 332 object_error::parse_failed); 333 DebugNames.push_back(wasm::WasmFunctionName{Index, Name}); 334 if (isDefinedFunctionIndex(Index)) 335 getDefinedFunction(Index).DebugName = Name; 336 } 337 break; 338 } 339 // Ignore local names for now 340 case wasm::WASM_NAMES_LOCAL: 341 default: 342 Ctx.Ptr += Size; 343 break; 344 } 345 if (Ctx.Ptr != SubSectionEnd) 346 return make_error<GenericBinaryError>("Name sub-section ended prematurely", 347 object_error::parse_failed); 348 } 349 350 if (Ctx.Ptr != Ctx.End) 351 return make_error<GenericBinaryError>("Name section ended prematurely", 352 object_error::parse_failed); 353 return Error::success(); 354 } 355 356 Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) { 357 HasLinkingSection = true; 358 if (Functions.size() != FunctionTypes.size()) { 359 return make_error<GenericBinaryError>( 360 "Linking data must come after code section", object_error::parse_failed); 361 } 362 363 LinkingData.Version = readVaruint32(Ctx); 364 if (LinkingData.Version != wasm::WasmMetadataVersion) { 365 return make_error<GenericBinaryError>( 366 "Unexpected metadata version: " + Twine(LinkingData.Version) + 367 " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")", 368 object_error::parse_failed); 369 } 370 371 const uint8_t *OrigEnd = Ctx.End; 372 while (Ctx.Ptr < OrigEnd) { 373 Ctx.End = OrigEnd; 374 uint8_t Type = readUint8(Ctx); 375 uint32_t Size = readVaruint32(Ctx); 376 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size 377 << "\n"); 378 Ctx.End = Ctx.Ptr + Size; 379 switch (Type) { 380 case wasm::WASM_SYMBOL_TABLE: 381 if (Error Err = parseLinkingSectionSymtab(Ctx)) 382 return Err; 383 break; 384 case wasm::WASM_SEGMENT_INFO: { 385 uint32_t Count = readVaruint32(Ctx); 386 if (Count > DataSegments.size()) 387 return make_error<GenericBinaryError>("Too many segment names", 388 object_error::parse_failed); 389 for (uint32_t i = 0; i < Count; i++) { 390 DataSegments[i].Data.Name = readString(Ctx); 391 DataSegments[i].Data.Alignment = readVaruint32(Ctx); 392 DataSegments[i].Data.Flags = readVaruint32(Ctx); 393 } 394 break; 395 } 396 case wasm::WASM_INIT_FUNCS: { 397 uint32_t Count = readVaruint32(Ctx); 398 LinkingData.InitFunctions.reserve(Count); 399 for (uint32_t i = 0; i < Count; i++) { 400 wasm::WasmInitFunc Init; 401 Init.Priority = readVaruint32(Ctx); 402 Init.Symbol = readVaruint32(Ctx); 403 if (!isValidFunctionSymbol(Init.Symbol)) 404 return make_error<GenericBinaryError>("Invalid function symbol: " + 405 Twine(Init.Symbol), 406 object_error::parse_failed); 407 LinkingData.InitFunctions.emplace_back(Init); 408 } 409 break; 410 } 411 case wasm::WASM_COMDAT_INFO: 412 if (Error Err = parseLinkingSectionComdat(Ctx)) 413 return Err; 414 break; 415 default: 416 Ctx.Ptr += Size; 417 break; 418 } 419 if (Ctx.Ptr != Ctx.End) 420 return make_error<GenericBinaryError>( 421 "Linking sub-section ended prematurely", object_error::parse_failed); 422 } 423 if (Ctx.Ptr != OrigEnd) 424 return make_error<GenericBinaryError>("Linking section ended prematurely", 425 object_error::parse_failed); 426 return Error::success(); 427 } 428 429 Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { 430 uint32_t Count = readVaruint32(Ctx); 431 LinkingData.SymbolTable.reserve(Count); 432 Symbols.reserve(Count); 433 StringSet<> SymbolNames; 434 435 std::vector<wasm::WasmImport *> ImportedGlobals; 436 std::vector<wasm::WasmImport *> ImportedFunctions; 437 ImportedGlobals.reserve(Imports.size()); 438 ImportedFunctions.reserve(Imports.size()); 439 for (auto &I : Imports) { 440 if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION) 441 ImportedFunctions.emplace_back(&I); 442 else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL) 443 ImportedGlobals.emplace_back(&I); 444 } 445 446 while (Count--) { 447 wasm::WasmSymbolInfo Info; 448 const wasm::WasmSignature *FunctionType = nullptr; 449 const wasm::WasmGlobalType *GlobalType = nullptr; 450 451 Info.Kind = readUint8(Ctx); 452 Info.Flags = readVaruint32(Ctx); 453 bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0; 454 455 switch (Info.Kind) { 456 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 457 Info.ElementIndex = readVaruint32(Ctx); 458 if (!isValidFunctionIndex(Info.ElementIndex) || 459 IsDefined != isDefinedFunctionIndex(Info.ElementIndex)) 460 return make_error<GenericBinaryError>("invalid function symbol index", 461 object_error::parse_failed); 462 if (IsDefined) { 463 Info.Name = readString(Ctx); 464 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions; 465 FunctionType = &Signatures[FunctionTypes[FuncIndex]]; 466 wasm::WasmFunction &Function = Functions[FuncIndex]; 467 if (Function.SymbolName.empty()) 468 Function.SymbolName = Info.Name; 469 } else { 470 wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex]; 471 FunctionType = &Signatures[Import.SigIndex]; 472 Info.Name = Import.Field; 473 Info.Module = Import.Module; 474 } 475 break; 476 477 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 478 Info.ElementIndex = readVaruint32(Ctx); 479 if (!isValidGlobalIndex(Info.ElementIndex) || 480 IsDefined != isDefinedGlobalIndex(Info.ElementIndex)) 481 return make_error<GenericBinaryError>("invalid global symbol index", 482 object_error::parse_failed); 483 if (!IsDefined && 484 (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == 485 wasm::WASM_SYMBOL_BINDING_WEAK) 486 return make_error<GenericBinaryError>("undefined weak global symbol", 487 object_error::parse_failed); 488 if (IsDefined) { 489 Info.Name = readString(Ctx); 490 unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals; 491 wasm::WasmGlobal &Global = Globals[GlobalIndex]; 492 GlobalType = &Global.Type; 493 if (Global.SymbolName.empty()) 494 Global.SymbolName = Info.Name; 495 } else { 496 wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex]; 497 Info.Name = Import.Field; 498 GlobalType = &Import.Global; 499 } 500 break; 501 502 case wasm::WASM_SYMBOL_TYPE_DATA: 503 Info.Name = readString(Ctx); 504 if (IsDefined) { 505 uint32_t Index = readVaruint32(Ctx); 506 if (Index >= DataSegments.size()) 507 return make_error<GenericBinaryError>("invalid data symbol index", 508 object_error::parse_failed); 509 uint32_t Offset = readVaruint32(Ctx); 510 uint32_t Size = readVaruint32(Ctx); 511 if (Offset + Size > DataSegments[Index].Data.Content.size()) 512 return make_error<GenericBinaryError>("invalid data symbol offset", 513 object_error::parse_failed); 514 Info.DataRef = wasm::WasmDataReference{Index, Offset, Size}; 515 } 516 break; 517 518 case wasm::WASM_SYMBOL_TYPE_SECTION: { 519 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != 520 wasm::WASM_SYMBOL_BINDING_LOCAL) 521 return make_error<GenericBinaryError>( 522 "Section symbols must have local binding", 523 object_error::parse_failed); 524 Info.ElementIndex = readVaruint32(Ctx); 525 // Use somewhat unique section name as symbol name. 526 StringRef SectionName = Sections[Info.ElementIndex].Name; 527 Info.Name = SectionName; 528 break; 529 } 530 531 default: 532 return make_error<GenericBinaryError>("Invalid symbol type", 533 object_error::parse_failed); 534 } 535 536 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != 537 wasm::WASM_SYMBOL_BINDING_LOCAL && 538 !SymbolNames.insert(Info.Name).second) 539 return make_error<GenericBinaryError>("Duplicate symbol name " + 540 Twine(Info.Name), 541 object_error::parse_failed); 542 LinkingData.SymbolTable.emplace_back(Info); 543 Symbols.emplace_back(LinkingData.SymbolTable.back(), FunctionType, 544 GlobalType); 545 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n"); 546 } 547 548 return Error::success(); 549 } 550 551 Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) { 552 uint32_t ComdatCount = readVaruint32(Ctx); 553 StringSet<> ComdatSet; 554 for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) { 555 StringRef Name = readString(Ctx); 556 if (Name.empty() || !ComdatSet.insert(Name).second) 557 return make_error<GenericBinaryError>("Bad/duplicate COMDAT name " + Twine(Name), 558 object_error::parse_failed); 559 LinkingData.Comdats.emplace_back(Name); 560 uint32_t Flags = readVaruint32(Ctx); 561 if (Flags != 0) 562 return make_error<GenericBinaryError>("Unsupported COMDAT flags", 563 object_error::parse_failed); 564 565 uint32_t EntryCount = readVaruint32(Ctx); 566 while (EntryCount--) { 567 unsigned Kind = readVaruint32(Ctx); 568 unsigned Index = readVaruint32(Ctx); 569 switch (Kind) { 570 default: 571 return make_error<GenericBinaryError>("Invalid COMDAT entry type", 572 object_error::parse_failed); 573 case wasm::WASM_COMDAT_DATA: 574 if (Index >= DataSegments.size()) 575 return make_error<GenericBinaryError>("COMDAT data index out of range", 576 object_error::parse_failed); 577 if (DataSegments[Index].Data.Comdat != UINT32_MAX) 578 return make_error<GenericBinaryError>("Data segment in two COMDATs", 579 object_error::parse_failed); 580 DataSegments[Index].Data.Comdat = ComdatIndex; 581 break; 582 case wasm::WASM_COMDAT_FUNCTION: 583 if (!isDefinedFunctionIndex(Index)) 584 return make_error<GenericBinaryError>("COMDAT function index out of range", 585 object_error::parse_failed); 586 if (getDefinedFunction(Index).Comdat != UINT32_MAX) 587 return make_error<GenericBinaryError>("Function in two COMDATs", 588 object_error::parse_failed); 589 getDefinedFunction(Index).Comdat = ComdatIndex; 590 break; 591 } 592 } 593 } 594 return Error::success(); 595 } 596 597 Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) { 598 uint32_t SectionIndex = readVaruint32(Ctx); 599 if (SectionIndex >= Sections.size()) 600 return make_error<GenericBinaryError>("Invalid section index", 601 object_error::parse_failed); 602 WasmSection& Section = Sections[SectionIndex]; 603 uint32_t RelocCount = readVaruint32(Ctx); 604 uint32_t EndOffset = Section.Content.size(); 605 while (RelocCount--) { 606 wasm::WasmRelocation Reloc = {}; 607 Reloc.Type = readVaruint32(Ctx); 608 Reloc.Offset = readVaruint32(Ctx); 609 Reloc.Index = readVaruint32(Ctx); 610 switch (Reloc.Type) { 611 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB: 612 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB: 613 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: 614 if (!isValidFunctionSymbol(Reloc.Index)) 615 return make_error<GenericBinaryError>("Bad relocation function index", 616 object_error::parse_failed); 617 break; 618 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB: 619 if (Reloc.Index >= Signatures.size()) 620 return make_error<GenericBinaryError>("Bad relocation type index", 621 object_error::parse_failed); 622 break; 623 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: 624 if (!isValidGlobalSymbol(Reloc.Index)) 625 return make_error<GenericBinaryError>("Bad relocation global index", 626 object_error::parse_failed); 627 break; 628 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: 629 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: 630 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: 631 if (!isValidDataSymbol(Reloc.Index)) 632 return make_error<GenericBinaryError>("Bad relocation data index", 633 object_error::parse_failed); 634 Reloc.Addend = readVarint32(Ctx); 635 break; 636 case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: 637 if (!isValidFunctionSymbol(Reloc.Index)) 638 return make_error<GenericBinaryError>("Bad relocation function index", 639 object_error::parse_failed); 640 Reloc.Addend = readVarint32(Ctx); 641 break; 642 case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: 643 if (!isValidSectionSymbol(Reloc.Index)) 644 return make_error<GenericBinaryError>("Bad relocation section index", 645 object_error::parse_failed); 646 Reloc.Addend = readVarint32(Ctx); 647 break; 648 default: 649 return make_error<GenericBinaryError>("Bad relocation type: " + 650 Twine(Reloc.Type), 651 object_error::parse_failed); 652 } 653 654 // Relocations must fit inside the section, and must appear in order. They 655 // also shouldn't overlap a function/element boundary, but we don't bother 656 // to check that. 657 uint64_t Size = 5; 658 if (Reloc.Type == wasm::R_WEBASSEMBLY_TABLE_INDEX_I32 || 659 Reloc.Type == wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32 || 660 Reloc.Type == wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32 || 661 Reloc.Type == wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32) 662 Size = 4; 663 if (Reloc.Offset + Size > EndOffset) 664 return make_error<GenericBinaryError>("Bad relocation offset", 665 object_error::parse_failed); 666 667 Section.Relocations.push_back(Reloc); 668 } 669 if (Ctx.Ptr != Ctx.End) 670 return make_error<GenericBinaryError>("Reloc section ended prematurely", 671 object_error::parse_failed); 672 return Error::success(); 673 } 674 675 Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) { 676 if (Sec.Name == "name") { 677 if (Error Err = parseNameSection(Ctx)) 678 return Err; 679 } else if (Sec.Name == "linking") { 680 if (Error Err = parseLinkingSection(Ctx)) 681 return Err; 682 } else if (Sec.Name.startswith("reloc.")) { 683 if (Error Err = parseRelocSection(Sec.Name, Ctx)) 684 return Err; 685 } 686 return Error::success(); 687 } 688 689 Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) { 690 uint32_t Count = readVaruint32(Ctx); 691 Signatures.reserve(Count); 692 while (Count--) { 693 wasm::WasmSignature Sig; 694 Sig.ReturnType = wasm::WASM_TYPE_NORESULT; 695 uint8_t Form = readUint8(Ctx); 696 if (Form != wasm::WASM_TYPE_FUNC) { 697 return make_error<GenericBinaryError>("Invalid signature type", 698 object_error::parse_failed); 699 } 700 uint32_t ParamCount = readVaruint32(Ctx); 701 Sig.ParamTypes.reserve(ParamCount); 702 while (ParamCount--) { 703 uint32_t ParamType = readUint8(Ctx); 704 Sig.ParamTypes.push_back(ParamType); 705 } 706 uint32_t ReturnCount = readVaruint32(Ctx); 707 if (ReturnCount) { 708 if (ReturnCount != 1) { 709 return make_error<GenericBinaryError>( 710 "Multiple return types not supported", object_error::parse_failed); 711 } 712 Sig.ReturnType = readUint8(Ctx); 713 } 714 Signatures.push_back(Sig); 715 } 716 if (Ctx.Ptr != Ctx.End) 717 return make_error<GenericBinaryError>("Type section ended prematurely", 718 object_error::parse_failed); 719 return Error::success(); 720 } 721 722 Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { 723 uint32_t Count = readVaruint32(Ctx); 724 Imports.reserve(Count); 725 for (uint32_t i = 0; i < Count; i++) { 726 wasm::WasmImport Im; 727 Im.Module = readString(Ctx); 728 Im.Field = readString(Ctx); 729 Im.Kind = readUint8(Ctx); 730 switch (Im.Kind) { 731 case wasm::WASM_EXTERNAL_FUNCTION: 732 NumImportedFunctions++; 733 Im.SigIndex = readVaruint32(Ctx); 734 break; 735 case wasm::WASM_EXTERNAL_GLOBAL: 736 NumImportedGlobals++; 737 Im.Global.Type = readUint8(Ctx); 738 Im.Global.Mutable = readVaruint1(Ctx); 739 break; 740 case wasm::WASM_EXTERNAL_MEMORY: 741 Im.Memory = readLimits(Ctx); 742 break; 743 case wasm::WASM_EXTERNAL_TABLE: 744 Im.Table = readTable(Ctx); 745 if (Im.Table.ElemType != wasm::WASM_TYPE_ANYFUNC) 746 return make_error<GenericBinaryError>("Invalid table element type", 747 object_error::parse_failed); 748 break; 749 default: 750 return make_error<GenericBinaryError>( 751 "Unexpected import kind", object_error::parse_failed); 752 } 753 Imports.push_back(Im); 754 } 755 if (Ctx.Ptr != Ctx.End) 756 return make_error<GenericBinaryError>("Import section ended prematurely", 757 object_error::parse_failed); 758 return Error::success(); 759 } 760 761 Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) { 762 uint32_t Count = readVaruint32(Ctx); 763 FunctionTypes.reserve(Count); 764 uint32_t NumTypes = Signatures.size(); 765 while (Count--) { 766 uint32_t Type = readVaruint32(Ctx); 767 if (Type >= NumTypes) 768 return make_error<GenericBinaryError>("Invalid function type", 769 object_error::parse_failed); 770 FunctionTypes.push_back(Type); 771 } 772 if (Ctx.Ptr != Ctx.End) 773 return make_error<GenericBinaryError>("Function section ended prematurely", 774 object_error::parse_failed); 775 return Error::success(); 776 } 777 778 Error WasmObjectFile::parseTableSection(ReadContext &Ctx) { 779 uint32_t Count = readVaruint32(Ctx); 780 Tables.reserve(Count); 781 while (Count--) { 782 Tables.push_back(readTable(Ctx)); 783 if (Tables.back().ElemType != wasm::WASM_TYPE_ANYFUNC) { 784 return make_error<GenericBinaryError>("Invalid table element type", 785 object_error::parse_failed); 786 } 787 } 788 if (Ctx.Ptr != Ctx.End) 789 return make_error<GenericBinaryError>("Table section ended prematurely", 790 object_error::parse_failed); 791 return Error::success(); 792 } 793 794 Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) { 795 uint32_t Count = readVaruint32(Ctx); 796 Memories.reserve(Count); 797 while (Count--) { 798 Memories.push_back(readLimits(Ctx)); 799 } 800 if (Ctx.Ptr != Ctx.End) 801 return make_error<GenericBinaryError>("Memory section ended prematurely", 802 object_error::parse_failed); 803 return Error::success(); 804 } 805 806 Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) { 807 GlobalSection = Sections.size(); 808 uint32_t Count = readVaruint32(Ctx); 809 Globals.reserve(Count); 810 while (Count--) { 811 wasm::WasmGlobal Global; 812 Global.Index = NumImportedGlobals + Globals.size(); 813 Global.Type.Type = readUint8(Ctx); 814 Global.Type.Mutable = readVaruint1(Ctx); 815 if (Error Err = readInitExpr(Global.InitExpr, Ctx)) 816 return Err; 817 Globals.push_back(Global); 818 } 819 if (Ctx.Ptr != Ctx.End) 820 return make_error<GenericBinaryError>("Global section ended prematurely", 821 object_error::parse_failed); 822 return Error::success(); 823 } 824 825 Error WasmObjectFile::parseExportSection(ReadContext &Ctx) { 826 uint32_t Count = readVaruint32(Ctx); 827 Exports.reserve(Count); 828 for (uint32_t i = 0; i < Count; i++) { 829 wasm::WasmExport Ex; 830 Ex.Name = readString(Ctx); 831 Ex.Kind = readUint8(Ctx); 832 Ex.Index = readVaruint32(Ctx); 833 switch (Ex.Kind) { 834 case wasm::WASM_EXTERNAL_FUNCTION: 835 if (!isValidFunctionIndex(Ex.Index)) 836 return make_error<GenericBinaryError>("Invalid function export", 837 object_error::parse_failed); 838 break; 839 case wasm::WASM_EXTERNAL_GLOBAL: 840 if (!isValidGlobalIndex(Ex.Index)) 841 return make_error<GenericBinaryError>("Invalid global export", 842 object_error::parse_failed); 843 break; 844 case wasm::WASM_EXTERNAL_MEMORY: 845 case wasm::WASM_EXTERNAL_TABLE: 846 break; 847 default: 848 return make_error<GenericBinaryError>( 849 "Unexpected export kind", object_error::parse_failed); 850 } 851 Exports.push_back(Ex); 852 } 853 if (Ctx.Ptr != Ctx.End) 854 return make_error<GenericBinaryError>("Export section ended prematurely", 855 object_error::parse_failed); 856 return Error::success(); 857 } 858 859 bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const { 860 return Index < NumImportedFunctions + FunctionTypes.size(); 861 } 862 863 bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const { 864 return Index >= NumImportedFunctions && isValidFunctionIndex(Index); 865 } 866 867 bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const { 868 return Index < NumImportedGlobals + Globals.size(); 869 } 870 871 bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const { 872 return Index >= NumImportedGlobals && isValidGlobalIndex(Index); 873 } 874 875 bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const { 876 return Index < Symbols.size() && Symbols[Index].isTypeFunction(); 877 } 878 879 bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const { 880 return Index < Symbols.size() && Symbols[Index].isTypeGlobal(); 881 } 882 883 bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const { 884 return Index < Symbols.size() && Symbols[Index].isTypeData(); 885 } 886 887 bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const { 888 return Index < Symbols.size() && Symbols[Index].isTypeSection(); 889 } 890 891 wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) { 892 assert(isDefinedFunctionIndex(Index)); 893 return Functions[Index - NumImportedFunctions]; 894 } 895 896 wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) { 897 assert(isDefinedGlobalIndex(Index)); 898 return Globals[Index - NumImportedGlobals]; 899 } 900 901 Error WasmObjectFile::parseStartSection(ReadContext &Ctx) { 902 StartFunction = readVaruint32(Ctx); 903 if (!isValidFunctionIndex(StartFunction)) 904 return make_error<GenericBinaryError>("Invalid start function", 905 object_error::parse_failed); 906 return Error::success(); 907 } 908 909 Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) { 910 CodeSection = Sections.size(); 911 uint32_t FunctionCount = readVaruint32(Ctx); 912 if (FunctionCount != FunctionTypes.size()) { 913 return make_error<GenericBinaryError>("Invalid function count", 914 object_error::parse_failed); 915 } 916 917 while (FunctionCount--) { 918 wasm::WasmFunction Function; 919 const uint8_t *FunctionStart = Ctx.Ptr; 920 uint32_t Size = readVaruint32(Ctx); 921 const uint8_t *FunctionEnd = Ctx.Ptr + Size; 922 923 Function.CodeOffset = Ctx.Ptr - FunctionStart; 924 Function.Index = NumImportedFunctions + Functions.size(); 925 Function.CodeSectionOffset = FunctionStart - Ctx.Start; 926 Function.Size = FunctionEnd - FunctionStart; 927 928 uint32_t NumLocalDecls = readVaruint32(Ctx); 929 Function.Locals.reserve(NumLocalDecls); 930 while (NumLocalDecls--) { 931 wasm::WasmLocalDecl Decl; 932 Decl.Count = readVaruint32(Ctx); 933 Decl.Type = readUint8(Ctx); 934 Function.Locals.push_back(Decl); 935 } 936 937 uint32_t BodySize = FunctionEnd - Ctx.Ptr; 938 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize); 939 // This will be set later when reading in the linking metadata section. 940 Function.Comdat = UINT32_MAX; 941 Ctx.Ptr += BodySize; 942 assert(Ctx.Ptr == FunctionEnd); 943 Functions.push_back(Function); 944 } 945 if (Ctx.Ptr != Ctx.End) 946 return make_error<GenericBinaryError>("Code section ended prematurely", 947 object_error::parse_failed); 948 return Error::success(); 949 } 950 951 Error WasmObjectFile::parseElemSection(ReadContext &Ctx) { 952 uint32_t Count = readVaruint32(Ctx); 953 ElemSegments.reserve(Count); 954 while (Count--) { 955 wasm::WasmElemSegment Segment; 956 Segment.TableIndex = readVaruint32(Ctx); 957 if (Segment.TableIndex != 0) { 958 return make_error<GenericBinaryError>("Invalid TableIndex", 959 object_error::parse_failed); 960 } 961 if (Error Err = readInitExpr(Segment.Offset, Ctx)) 962 return Err; 963 uint32_t NumElems = readVaruint32(Ctx); 964 while (NumElems--) { 965 Segment.Functions.push_back(readVaruint32(Ctx)); 966 } 967 ElemSegments.push_back(Segment); 968 } 969 if (Ctx.Ptr != Ctx.End) 970 return make_error<GenericBinaryError>("Elem section ended prematurely", 971 object_error::parse_failed); 972 return Error::success(); 973 } 974 975 Error WasmObjectFile::parseDataSection(ReadContext &Ctx) { 976 DataSection = Sections.size(); 977 uint32_t Count = readVaruint32(Ctx); 978 DataSegments.reserve(Count); 979 while (Count--) { 980 WasmSegment Segment; 981 Segment.Data.MemoryIndex = readVaruint32(Ctx); 982 if (Error Err = readInitExpr(Segment.Data.Offset, Ctx)) 983 return Err; 984 uint32_t Size = readVaruint32(Ctx); 985 if (Size > (size_t)(Ctx.End - Ctx.Ptr)) 986 return make_error<GenericBinaryError>("Invalid segment size", 987 object_error::parse_failed); 988 Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size); 989 // The rest of these Data fields are set later, when reading in the linking 990 // metadata section. 991 Segment.Data.Alignment = 0; 992 Segment.Data.Flags = 0; 993 Segment.Data.Comdat = UINT32_MAX; 994 Segment.SectionOffset = Ctx.Ptr - Ctx.Start; 995 Ctx.Ptr += Size; 996 DataSegments.push_back(Segment); 997 } 998 if (Ctx.Ptr != Ctx.End) 999 return make_error<GenericBinaryError>("Data section ended prematurely", 1000 object_error::parse_failed); 1001 return Error::success(); 1002 } 1003 1004 const uint8_t *WasmObjectFile::getPtr(size_t Offset) const { 1005 return reinterpret_cast<const uint8_t *>(getData().data() + Offset); 1006 } 1007 1008 const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const { 1009 return Header; 1010 } 1011 1012 void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.a++; } 1013 1014 uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const { 1015 uint32_t Result = SymbolRef::SF_None; 1016 const WasmSymbol &Sym = getWasmSymbol(Symb); 1017 1018 LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n"); 1019 if (Sym.isBindingWeak()) 1020 Result |= SymbolRef::SF_Weak; 1021 if (!Sym.isBindingLocal()) 1022 Result |= SymbolRef::SF_Global; 1023 if (Sym.isHidden()) 1024 Result |= SymbolRef::SF_Hidden; 1025 if (!Sym.isDefined()) 1026 Result |= SymbolRef::SF_Undefined; 1027 if (Sym.isTypeFunction()) 1028 Result |= SymbolRef::SF_Executable; 1029 return Result; 1030 } 1031 1032 basic_symbol_iterator WasmObjectFile::symbol_begin() const { 1033 DataRefImpl Ref; 1034 Ref.d.a = 0; 1035 return BasicSymbolRef(Ref, this); 1036 } 1037 1038 basic_symbol_iterator WasmObjectFile::symbol_end() const { 1039 DataRefImpl Ref; 1040 Ref.d.a = Symbols.size(); 1041 return BasicSymbolRef(Ref, this); 1042 } 1043 1044 const WasmSymbol &WasmObjectFile::getWasmSymbol(const DataRefImpl &Symb) const { 1045 return Symbols[Symb.d.a]; 1046 } 1047 1048 const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const { 1049 return getWasmSymbol(Symb.getRawDataRefImpl()); 1050 } 1051 1052 Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const { 1053 return getWasmSymbol(Symb).Info.Name; 1054 } 1055 1056 Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const { 1057 return getSymbolValue(Symb); 1058 } 1059 1060 uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol& Sym) const { 1061 switch (Sym.Info.Kind) { 1062 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1063 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1064 return Sym.Info.ElementIndex; 1065 case wasm::WASM_SYMBOL_TYPE_DATA: { 1066 // The value of a data symbol is the segment offset, plus the symbol 1067 // offset within the segment. 1068 uint32_t SegmentIndex = Sym.Info.DataRef.Segment; 1069 const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data; 1070 assert(Segment.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST); 1071 return Segment.Offset.Value.Int32 + Sym.Info.DataRef.Offset; 1072 } 1073 case wasm::WASM_SYMBOL_TYPE_SECTION: 1074 return 0; 1075 } 1076 llvm_unreachable("invalid symbol type"); 1077 } 1078 1079 uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const { 1080 return getWasmSymbolValue(getWasmSymbol(Symb)); 1081 } 1082 1083 uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const { 1084 llvm_unreachable("not yet implemented"); 1085 return 0; 1086 } 1087 1088 uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const { 1089 llvm_unreachable("not yet implemented"); 1090 return 0; 1091 } 1092 1093 Expected<SymbolRef::Type> 1094 WasmObjectFile::getSymbolType(DataRefImpl Symb) const { 1095 const WasmSymbol &Sym = getWasmSymbol(Symb); 1096 1097 switch (Sym.Info.Kind) { 1098 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1099 return SymbolRef::ST_Function; 1100 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1101 return SymbolRef::ST_Other; 1102 case wasm::WASM_SYMBOL_TYPE_DATA: 1103 return SymbolRef::ST_Data; 1104 case wasm::WASM_SYMBOL_TYPE_SECTION: 1105 return SymbolRef::ST_Debug; 1106 } 1107 1108 llvm_unreachable("Unknown WasmSymbol::SymbolType"); 1109 return SymbolRef::ST_Other; 1110 } 1111 1112 Expected<section_iterator> 1113 WasmObjectFile::getSymbolSection(DataRefImpl Symb) const { 1114 const WasmSymbol& Sym = getWasmSymbol(Symb); 1115 if (Sym.isUndefined()) 1116 return section_end(); 1117 1118 DataRefImpl Ref; 1119 switch (Sym.Info.Kind) { 1120 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1121 Ref.d.a = CodeSection; 1122 break; 1123 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1124 Ref.d.a = GlobalSection; 1125 break; 1126 case wasm::WASM_SYMBOL_TYPE_DATA: 1127 Ref.d.a = DataSection; 1128 break; 1129 case wasm::WASM_SYMBOL_TYPE_SECTION: { 1130 Ref.d.a = Sym.Info.ElementIndex; 1131 break; 1132 } 1133 default: 1134 llvm_unreachable("Unknown WasmSymbol::SymbolType"); 1135 } 1136 return section_iterator(SectionRef(Ref, this)); 1137 } 1138 1139 void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; } 1140 1141 std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec, 1142 StringRef &Res) const { 1143 const WasmSection &S = Sections[Sec.d.a]; 1144 #define ECase(X) \ 1145 case wasm::WASM_SEC_##X: \ 1146 Res = #X; \ 1147 break 1148 switch (S.Type) { 1149 ECase(TYPE); 1150 ECase(IMPORT); 1151 ECase(FUNCTION); 1152 ECase(TABLE); 1153 ECase(MEMORY); 1154 ECase(GLOBAL); 1155 ECase(EXPORT); 1156 ECase(START); 1157 ECase(ELEM); 1158 ECase(CODE); 1159 ECase(DATA); 1160 case wasm::WASM_SEC_CUSTOM: 1161 Res = S.Name; 1162 break; 1163 default: 1164 return object_error::invalid_section_index; 1165 } 1166 #undef ECase 1167 return std::error_code(); 1168 } 1169 1170 uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; } 1171 1172 uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const { 1173 return Sec.d.a; 1174 } 1175 1176 uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const { 1177 const WasmSection &S = Sections[Sec.d.a]; 1178 return S.Content.size(); 1179 } 1180 1181 std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec, 1182 StringRef &Res) const { 1183 const WasmSection &S = Sections[Sec.d.a]; 1184 // This will never fail since wasm sections can never be empty (user-sections 1185 // must have a name and non-user sections each have a defined structure). 1186 Res = StringRef(reinterpret_cast<const char *>(S.Content.data()), 1187 S.Content.size()); 1188 return std::error_code(); 1189 } 1190 1191 uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const { 1192 return 1; 1193 } 1194 1195 bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const { 1196 return false; 1197 } 1198 1199 bool WasmObjectFile::isSectionText(DataRefImpl Sec) const { 1200 return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE; 1201 } 1202 1203 bool WasmObjectFile::isSectionData(DataRefImpl Sec) const { 1204 return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA; 1205 } 1206 1207 bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; } 1208 1209 bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; } 1210 1211 bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; } 1212 1213 relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const { 1214 DataRefImpl RelocRef; 1215 RelocRef.d.a = Ref.d.a; 1216 RelocRef.d.b = 0; 1217 return relocation_iterator(RelocationRef(RelocRef, this)); 1218 } 1219 1220 relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const { 1221 const WasmSection &Sec = getWasmSection(Ref); 1222 DataRefImpl RelocRef; 1223 RelocRef.d.a = Ref.d.a; 1224 RelocRef.d.b = Sec.Relocations.size(); 1225 return relocation_iterator(RelocationRef(RelocRef, this)); 1226 } 1227 1228 void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const { 1229 Rel.d.b++; 1230 } 1231 1232 uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const { 1233 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1234 return Rel.Offset; 1235 } 1236 1237 symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Ref) const { 1238 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1239 if (Rel.Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB) 1240 return symbol_end(); 1241 DataRefImpl Sym; 1242 Sym.d.a = Rel.Index; 1243 Sym.d.b = 0; 1244 return symbol_iterator(SymbolRef(Sym, this)); 1245 } 1246 1247 uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const { 1248 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1249 return Rel.Type; 1250 } 1251 1252 void WasmObjectFile::getRelocationTypeName( 1253 DataRefImpl Ref, SmallVectorImpl<char> &Result) const { 1254 const wasm::WasmRelocation& Rel = getWasmRelocation(Ref); 1255 StringRef Res = "Unknown"; 1256 1257 #define WASM_RELOC(name, value) \ 1258 case wasm::name: \ 1259 Res = #name; \ 1260 break; 1261 1262 switch (Rel.Type) { 1263 #include "llvm/BinaryFormat/WasmRelocs.def" 1264 } 1265 1266 #undef WASM_RELOC 1267 1268 Result.append(Res.begin(), Res.end()); 1269 } 1270 1271 section_iterator WasmObjectFile::section_begin() const { 1272 DataRefImpl Ref; 1273 Ref.d.a = 0; 1274 return section_iterator(SectionRef(Ref, this)); 1275 } 1276 1277 section_iterator WasmObjectFile::section_end() const { 1278 DataRefImpl Ref; 1279 Ref.d.a = Sections.size(); 1280 return section_iterator(SectionRef(Ref, this)); 1281 } 1282 1283 uint8_t WasmObjectFile::getBytesInAddress() const { return 4; } 1284 1285 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; } 1286 1287 Triple::ArchType WasmObjectFile::getArch() const { return Triple::wasm32; } 1288 1289 SubtargetFeatures WasmObjectFile::getFeatures() const { 1290 return SubtargetFeatures(); 1291 } 1292 1293 bool WasmObjectFile::isRelocatableObject() const { 1294 return HasLinkingSection; 1295 } 1296 1297 const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const { 1298 assert(Ref.d.a < Sections.size()); 1299 return Sections[Ref.d.a]; 1300 } 1301 1302 const WasmSection & 1303 WasmObjectFile::getWasmSection(const SectionRef &Section) const { 1304 return getWasmSection(Section.getRawDataRefImpl()); 1305 } 1306 1307 const wasm::WasmRelocation & 1308 WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const { 1309 return getWasmRelocation(Ref.getRawDataRefImpl()); 1310 } 1311 1312 const wasm::WasmRelocation & 1313 WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const { 1314 assert(Ref.d.a < Sections.size()); 1315 const WasmSection& Sec = Sections[Ref.d.a]; 1316 assert(Ref.d.b < Sec.Relocations.size()); 1317 return Sec.Relocations[Ref.d.b]; 1318 } 1319