Index: lib/MC/WasmObjectWriter.cpp =================================================================== --- lib/MC/WasmObjectWriter.cpp +++ lib/MC/WasmObjectWriter.cpp @@ -414,12 +414,12 @@ ArrayRef Relocations, raw_pwrite_stream &Stream, DenseMap &SymbolIndices, - uint64_t ContentsOffset) + uint64_t ContentsOffset, uint64_t HeaderSize) { for (const WasmRelocationEntry &RelEntry : Relocations) { uint64_t Offset = ContentsOffset + RelEntry.FixupSection->getSectionOffset() + - RelEntry.Offset; + RelEntry.Offset + HeaderSize; switch (RelEntry.Type) { case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB: { uint32_t Index = SymbolIndices[RelEntry.Symbol]; @@ -471,13 +471,13 @@ static void WriteRelocations( ArrayRef Relocations, raw_pwrite_stream &Stream, - DenseMap &SymbolIndices) + DenseMap &SymbolIndices, uint64_t HeaderSize) { for (const WasmRelocationEntry RelEntry : Relocations) { encodeULEB128(RelEntry.Type, Stream); uint64_t Offset = RelEntry.Offset + - RelEntry.FixupSection->getSectionOffset(); + RelEntry.FixupSection->getSectionOffset() + HeaderSize; uint32_t Index = SymbolIndices[RelEntry.Symbol]; int64_t Addend = RelEntry.Addend; @@ -1044,12 +1044,13 @@ // Apply fixups. ApplyRelocations(CodeRelocations, getStream(), SymbolIndices, - Section.ContentsOffset); + Section.ContentsOffset, 0); endSection(Section); } // === Data Section ========================================================== + uint32_t DataSectionHeaderSize = 0; if (!DataBytes.empty()) { startSection(Section, wasm::WASM_SEC_DATA); @@ -1059,11 +1060,12 @@ encodeSLEB128(0, getStream()); // offset write8(wasm::WASM_OPCODE_END); encodeULEB128(DataBytes.size(), getStream()); // size + DataSectionHeaderSize = getStream().tell() - Section.ContentsOffset; writeBytes(DataBytes); // data // Apply fixups. ApplyRelocations(DataRelocations, getStream(), SymbolIndices, - Section.ContentsOffset); + Section.ContentsOffset, DataSectionHeaderSize); endSection(Section); } @@ -1107,7 +1109,7 @@ encodeULEB128(CodeRelocations.size() + TypeIndexFixups.size(), getStream()); - WriteRelocations(CodeRelocations, getStream(), SymbolIndices); + WriteRelocations(CodeRelocations, getStream(), SymbolIndices, 0); WriteTypeRelocations(TypeIndexFixups, TypeIndexFixupTypes, getStream()); endSection(Section); @@ -1121,7 +1123,7 @@ encodeULEB128(DataRelocations.size(), getStream()); - WriteRelocations(DataRelocations, getStream(), SymbolIndices); + WriteRelocations(DataRelocations, getStream(), SymbolIndices, DataSectionHeaderSize); endSection(Section); } Index: test/MC/WebAssembly/reloc-data.ll =================================================================== --- /dev/null +++ test/MC/WebAssembly/reloc-data.ll @@ -0,0 +1,25 @@ +; RUN: llc -O0 -mtriple wasm32-unknown-unknown-wasm -filetype=obj %s -o - | llvm-readobj -r -expand-relocs | FileCheck %s + +; bar is a pointer initialised to the value of foo +@foo = external global i32, align 4 +@bar = external global i64, align 4 +@a = global i32* @foo, align 8 +@b = global i64* @bar, align 8 + +; CHECK: Format: WASM +; CHECK: Relocations [ +; CHECK: Section (6) DATA { +; CHECK: Relocation { +; CHECK: Type: R_WEBASSEMBLY_GLOBAL_ADDR_I32 (5) +; CHECK: Offset: 0x6 +; CHECK: Index: 0x0 +; CHECK: Addend: 0x0 +; CHECK: } +; CHECK: Relocation { +; CHECK: Type: R_WEBASSEMBLY_GLOBAL_ADDR_I32 (5) +; CHECK: Offset: 0xE +; CHECK: Index: 0x1 +; CHECK: Addend: 0x0 +; CHECK: } +; CHECK: } +; CHECK: ]