diff --git a/compiler/ogbase.pas b/compiler/ogbase.pas index ab8bd56e07..f7c5e666af 100644 --- a/compiler/ogbase.pas +++ b/compiler/ogbase.pas @@ -115,7 +115,7 @@ interface {$ifdef WASM32} RELOC_FUNCTION_INDEX_LEB, RELOC_MEMORY_ADDR_LEB, - RELOC_MEMORY_ADDR_SLEB, + RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB, RELOC_TYPE_INDEX_LEB, {$endif WASM32} { Relative relocation } diff --git a/compiler/ogwasm.pas b/compiler/ogwasm.pas index 77e6414ab7..4289867d6f 100644 --- a/compiler/ogwasm.pas +++ b/compiler/ogwasm.pas @@ -550,7 +550,7 @@ implementation writebytes(leb_zero,5); end; RELOC_MEMORY_ADDR_LEB, - RELOC_MEMORY_ADDR_SLEB: + RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB: begin if (Reloctype=RELOC_MEMORY_ADDR_LEB) and (Data<0) then internalerror(2021092602); @@ -909,7 +909,7 @@ implementation objsec.Data.seek(objrel.DataOffset); WriteUleb5(objsec.Data,TWasmObjSymbol(objrel.symbol).ImportOrFuncIndex); end; - RELOC_MEMORY_ADDR_SLEB: + RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB: begin if not assigned(objrel.symbol) then internalerror(2021092605); @@ -984,15 +984,24 @@ implementation WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex); WriteUleb(relout,0); { addend to add to the address } end; - RELOC_MEMORY_ADDR_SLEB: + RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB: begin if not assigned(objrel.symbol) then internalerror(2021092604); Inc(relcount^); - WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_SLEB)); - WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs); - WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex); - WriteUleb(relout,0); { addend to add to the address } + if IsExternalFunction(objrel.symbol) or (objrel.symbol.typ=AT_FUNCTION) then + begin + WriteByte(relout,Ord(R_WASM_TABLE_INDEX_SLEB)); + WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs); + WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex); + end + else + begin + WriteByte(relout,Ord(R_WASM_MEMORY_ADDR_SLEB)); + WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs); + WriteUleb(relout,TWasmObjSymbol(objrel.symbol).SymbolIndex); + WriteUleb(relout,0); { addend to add to the address } + end; end; RELOC_ABSOLUTE: begin diff --git a/compiler/wasm32/aasmcpu.pas b/compiler/wasm32/aasmcpu.pas index e41daf55a2..35cb846d2e 100644 --- a/compiler/wasm32/aasmcpu.pas +++ b/compiler/wasm32/aasmcpu.pas @@ -1014,7 +1014,7 @@ uses top_ref: begin if assigned(ref^.symbol) then - objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_SLEB) + objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB) else begin if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then