@@ -499,25 +499,44 @@ static const MCSymbolWasm* ResolveSymbol(const MCSymbolWasm& Symbol) {
499
499
}
500
500
501
501
// Compute a value to write into the code at the location covered
502
- // by RelEntry. This value isn't used by the static linker, since
503
- // we have addends; it just serves to make the code more readable
504
- // and to make standalone wasm modules directly usable .
502
+ // by RelEntry. This value isn't used by the static linker; it just serves
503
+ // to make the object format more readable and more likely to be directly
504
+ // useable .
505
505
uint32_t
506
506
WasmObjectWriter::getProvisionalValue (const WasmRelocationEntry &RelEntry) {
507
- const MCSymbolWasm *Sym = ResolveSymbol (*RelEntry.Symbol );
508
507
509
- // For undefined symbols, use zero
510
- if (!Sym->isDefined ())
511
- return 0 ;
512
-
513
- uint32_t GlobalIndex = SymbolIndices[Sym];
514
- const WasmGlobal& Global = Globals[GlobalIndex - NumGlobalImports];
515
- uint64_t Address = Global.InitialValue + RelEntry.Addend ;
516
-
517
- // Ignore overflow. LLVM allows address arithmetic to silently wrap.
518
- uint32_t Value = Address;
519
-
520
- return Value;
508
+ switch (RelEntry.Type ) {
509
+ case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
510
+ case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
511
+ // Provitional value is the indirect symbol index
512
+ if (!IndirectSymbolIndices.count (RelEntry.Symbol ))
513
+ report_fatal_error (" symbol not found in table index space: " +
514
+ RelEntry.Symbol ->getName ());
515
+ return IndirectSymbolIndices[RelEntry.Symbol ];
516
+ case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
517
+ case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
518
+ case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
519
+ // Provitional value is function/type/global index itself
520
+ return getRelocationIndexValue (RelEntry);
521
+ case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
522
+ case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
523
+ case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
524
+ // Provitional value is address of the global
525
+ const MCSymbolWasm *Sym = ResolveSymbol (*RelEntry.Symbol );
526
+ // For undefined symbols, use zero
527
+ if (!Sym->isDefined ())
528
+ return 0 ;
529
+
530
+ uint32_t GlobalIndex = SymbolIndices[Sym];
531
+ const WasmGlobal& Global = Globals[GlobalIndex - NumGlobalImports];
532
+ uint64_t Address = Global.InitialValue + RelEntry.Addend ;
533
+
534
+ // Ignore overflow. LLVM allows address arithmetic to silently wrap.
535
+ return Address;
536
+ }
537
+ default :
538
+ llvm_unreachable (" invalid relocation type" );
539
+ }
521
540
}
522
541
523
542
static void addData (SmallVectorImpl<char > &DataBytes,
@@ -564,32 +583,19 @@ static void addData(SmallVectorImpl<char> &DataBytes,
564
583
DEBUG (dbgs () << " addData -> " << DataBytes.size () << " \n " );
565
584
}
566
585
567
- uint32_t WasmObjectWriter::getRelocationIndexValue (
568
- const WasmRelocationEntry &RelEntry) {
569
- switch (RelEntry.Type ) {
570
- case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
571
- case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
572
- if (!IndirectSymbolIndices.count (RelEntry.Symbol ))
573
- report_fatal_error (" symbol not found in table index space: " +
574
- RelEntry.Symbol ->getName ());
575
- return IndirectSymbolIndices[RelEntry.Symbol ];
576
- case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
577
- case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
578
- case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
579
- case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
580
- case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
581
- if (!SymbolIndices.count (RelEntry.Symbol ))
582
- report_fatal_error (" symbol not found in function/global index space: " +
583
- RelEntry.Symbol ->getName ());
584
- return SymbolIndices[RelEntry.Symbol ];
585
- case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
586
+ uint32_t
587
+ WasmObjectWriter::getRelocationIndexValue (const WasmRelocationEntry &RelEntry) {
588
+ if (RelEntry.Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB) {
586
589
if (!TypeIndices.count (RelEntry.Symbol ))
587
590
report_fatal_error (" symbol not found in type index space: " +
588
591
RelEntry.Symbol ->getName ());
589
592
return TypeIndices[RelEntry.Symbol ];
590
- default :
591
- llvm_unreachable (" invalid relocation type" );
592
593
}
594
+
595
+ if (!SymbolIndices.count (RelEntry.Symbol ))
596
+ report_fatal_error (" symbol not found in function/global index space: " +
597
+ RelEntry.Symbol ->getName ());
598
+ return SymbolIndices[RelEntry.Symbol ];
593
599
}
594
600
595
601
// Apply the portions of the relocation records that we can handle ourselves
@@ -603,35 +609,23 @@ void WasmObjectWriter::applyRelocations(
603
609
RelEntry.Offset ;
604
610
605
611
DEBUG (dbgs () << " applyRelocation: " << RelEntry << " \n " );
612
+ uint32_t Value = getProvisionalValue (RelEntry);
613
+
606
614
switch (RelEntry.Type ) {
607
- case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
608
615
case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
609
616
case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
610
- case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: {
611
- uint32_t Index = getRelocationIndexValue (RelEntry);
612
- WritePatchableSLEB (Stream, Index, Offset);
613
- break ;
614
- }
615
- case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: {
616
- uint32_t Index = getRelocationIndexValue (RelEntry);
617
- WriteI32 (Stream, Index, Offset);
618
- break ;
619
- }
620
- case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
621
- uint32_t Value = getProvisionalValue (RelEntry);
622
- WritePatchableSLEB (Stream, Value, Offset);
623
- break ;
624
- }
625
- case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: {
626
- uint32_t Value = getProvisionalValue (RelEntry);
617
+ case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
618
+ case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
627
619
WritePatchableLEB (Stream, Value, Offset);
628
620
break ;
629
- }
630
- case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: {
631
- uint32_t Value = getProvisionalValue (RelEntry);
621
+ case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
622
+ case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
632
623
WriteI32 (Stream, Value, Offset);
633
624
break ;
634
- }
625
+ case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
626
+ case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
627
+ WritePatchableSLEB (Stream, Value, Offset);
628
+ break ;
635
629
default :
636
630
llvm_unreachable (" invalid relocation type" );
637
631
}
0 commit comments