Index: lld/trunk/test/wasm/data-layout.ll =================================================================== --- lld/trunk/test/wasm/data-layout.ll +++ lld/trunk/test/wasm/data-layout.ll @@ -9,6 +9,10 @@ @hello_str = external global i8* @external_ref = global i8** @hello_str, align 8 +%struct.s = type { i32, i32 } +@local_struct = hidden global %struct.s zeroinitializer, align 4 +@local_struct_internal_ptr = hidden local_unnamed_addr global i32* getelementptr inbounds (%struct.s, %struct.s* @local_struct, i32 0, i32 1), align 4 + ; RUN: wasm-ld -no-gc-sections --check-signatures --allow-undefined -o %t.wasm %t.o %t.hello.o ; RUN: obj2yaml %t.wasm | FileCheck %s @@ -34,13 +38,20 @@ ; CHECK-NEXT: Offset: ; CHECK-NEXT: Opcode: I32_CONST ; CHECK-NEXT: Value: 1024 -; CHECK-NEXT: Content: 0100000000000000000000000000000003000000000000001C040000 -; CHECK-NEXT: - SectionOffset: 41 +; CHECK-NEXT: Content: '0100000000000000000000000000000003000000000000002804000024040000' +; CHECK-NEXT: - SectionOffset: 45 ; CHECK-NEXT: MemoryIndex: 0 ; CHECK-NEXT: Offset: ; CHECK-NEXT: Opcode: I32_CONST -; CHECK-NEXT: Value: 1052 +; CHECK-NEXT: Value: 1056 +; CHECK-NEXT: Content: '0000000000000000' +; CHECK-NEXT: - SectionOffset: 59 +; CHECK-NEXT: MemoryIndex: 0 +; CHECK-NEXT: Offset: +; CHECK-NEXT: Opcode: I32_CONST +; CHECK-NEXT: Value: 1064 ; CHECK-NEXT: Content: 68656C6C6F0A00 +; CHECK-NEXT: - Type: CUSTOM ; RUN: wasm-ld --check-signatures --relocatable -o %t_reloc.wasm %t.o %t.hello.o @@ -49,8 +60,12 @@ ; RELOC: - Type: DATA ; RELOC-NEXT: Relocations: ; RELOC-NEXT: - Type: R_WEBASSEMBLY_MEMORY_ADDR_I32 -; RELOC-NEXT: Index: 4 +; RELOC-NEXT: Index: 6 ; RELOC-NEXT: Offset: 0x00000018 +; RELOC-NEXT: - Type: R_WEBASSEMBLY_MEMORY_ADDR_I32 +; RELOC-NEXT: Index: 3 +; RELOC-NEXT: Offset: 0x0000002E +; RELOC-NEXT: Addend: 4 ; RELOC-NEXT: Segments: ; RELOC-NEXT: - SectionOffset: 6 ; RELOC-NEXT: MemoryIndex: 0 @@ -69,12 +84,24 @@ ; RELOC-NEXT: Offset: ; RELOC-NEXT: Opcode: I32_CONST ; RELOC-NEXT: Value: 24 -; RELOC-NEXT: Content: 1C000000 +; RELOC-NEXT: Content: '28000000' ; RELOC-NEXT: - SectionOffset: 33 ; RELOC-NEXT: MemoryIndex: 0 -; RELOC-NEXT: Offset: +; RELOC-NEXT: Offset: ; RELOC-NEXT: Opcode: I32_CONST ; RELOC-NEXT: Value: 28 +; RELOC-NEXT: Content: '0000000000000000' +; RELOC-NEXT: - SectionOffset: 46 +; RELOC-NEXT: MemoryIndex: 0 +; RELOC-NEXT: Offset: +; RELOC-NEXT: Opcode: I32_CONST +; RELOC-NEXT: Value: 36 +; RELOC-NEXT: Content: '20000000' +; RELOC-NEXT: - SectionOffset: 55 +; RELOC-NEXT: MemoryIndex: 0 +; RELOC-NEXT: Offset: +; RELOC-NEXT: Opcode: I32_CONST +; RELOC-NEXT: Value: 40 ; RELOC-NEXT: Content: 68656C6C6F0A00 ; RELOC: - Type: CUSTOM @@ -98,9 +125,9 @@ ; RELOC-NEXT: Flags: [ ] ; RELOC-NEXT: Segment: 2 ; RELOC-NEXT: Size: 4 -; RELOC: - Index: 4 +; RELOC: - Index: 6 ; RELOC-NEXT: Kind: DATA ; RELOC-NEXT: Name: hello_str ; RELOC-NEXT: Flags: [ ] -; RELOC-NEXT: Segment: 3 +; RELOC-NEXT: Segment: 5 ; RELOC-NEXT: Size: 7 Index: lld/trunk/wasm/InputChunks.cpp =================================================================== --- lld/trunk/wasm/InputChunks.cpp +++ lld/trunk/wasm/InputChunks.cpp @@ -55,7 +55,8 @@ if (Relocations.empty()) return; - DEBUG(dbgs() << "applying relocations: count=" << Relocations.size() << "\n"); + DEBUG(dbgs() << "applying relocations: " << getName() + << " count=" << Relocations.size() << "\n"); int32_t Off = OutputOffset - getInputSectionOffset(); for (const WasmRelocation &Rel : Relocations) { Index: lld/trunk/wasm/InputFiles.cpp =================================================================== --- lld/trunk/wasm/InputFiles.cpp +++ lld/trunk/wasm/InputFiles.cpp @@ -77,7 +77,8 @@ if (Sym.isUndefined()) return 0; const WasmSegment& Segment = WasmObj->dataSegments()[Sym.Info.DataRef.Segment]; - return Segment.Data.Offset.Value.Int32 + Sym.Info.DataRef.Offset; + return Segment.Data.Offset.Value.Int32 + Sym.Info.DataRef.Offset + + Reloc.Addend; } case R_WEBASSEMBLY_TYPE_INDEX_LEB: return Reloc.Index; @@ -102,7 +103,7 @@ case R_WEBASSEMBLY_MEMORY_ADDR_LEB: if (auto *Sym = dyn_cast(getDataSymbol(Reloc.Index))) return Sym->getVirtualAddress() + Reloc.Addend; - return Reloc.Addend; + return 0; case R_WEBASSEMBLY_TYPE_INDEX_LEB: return TypeMap[Reloc.Index]; case R_WEBASSEMBLY_FUNCTION_INDEX_LEB: