[MC][WebAssembly] Fix provisional values for data alias relocations

When calculating the symbol offsets to write as provisitonal values
in object files we are only interested in the offset of the symbol
itself.  For aliases this offset already includes the offset of the
base symbol.

The testin question was added back in https://reviews.llvm.org/D87407
but I believe the expectations here were incorrect.   sym_a lives
at offset 4 and sym_b lives 4 bytes into that (should be 8).

The addresses of the 3 symbosl in this object file are:

foo  : 0
sym_a: 4
sym_b: 8

Differential Revision: https://reviews.llvm.org/D96234
This commit is contained in:
Sam Clegg 2021-02-07 18:51:42 -08:00
parent 622611f7e5
commit 01a48535c3
2 changed files with 6 additions and 9 deletions

View File

@ -610,16 +610,13 @@ WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry,
case wasm::R_WASM_MEMORY_ADDR_I64:
case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB: {
// Provisional value is address of the global plus the offset
const MCSymbolWasm *Base =
cast<MCSymbolWasm>(Layout.getBaseSymbol(*RelEntry.Symbol));
// For undefined symbols, use zero
if (!Base->isDefined())
if (!RelEntry.Symbol->isDefined())
return 0;
const wasm::WasmDataReference &BaseRef = DataLocations[Base],
&SymRef = DataLocations[RelEntry.Symbol];
const WasmDataSegment &Segment = DataSegments[BaseRef.Segment];
const wasm::WasmDataReference &SymRef = DataLocations[RelEntry.Symbol];
const WasmDataSegment &Segment = DataSegments[SymRef.Segment];
// Ignore overflow. LLVM allows address arithmetic to silently wrap.
return Segment.Offset + BaseRef.Offset + SymRef.Offset + RelEntry.Addend;
return Segment.Offset + SymRef.Offset + RelEntry.Addend;
}
default:
llvm_unreachable("invalid relocation type");

View File

@ -27,8 +27,8 @@ main:
# CHECK-LABEL: <main>:
# CHECK-EMPTY:
# CHECK-NEXT: 3: 41 88 80 80 80 00 i32.const 8
# CHECK-NEXT: 3: 41 84 80 80 80 00 i32.const 4
# CHECK-NEXT: 00000004: R_WASM_MEMORY_ADDR_SLEB sym_a+0
# CHECK-NEXT: 9: 36 02 8c 80 80 80 00 i32.store 12
# CHECK-NEXT: 9: 36 02 88 80 80 80 00 i32.store 8
# CHECK-NEXT: 0000000b: R_WASM_MEMORY_ADDR_LEB sym_b+0
# CHECK-NEXT: 10: 0b end