Browse Source

[PATCH 188/188] fix offset reference within the code section, when
writing relocation info

From ad32136edcdc2c4df42f5a305358aa01b90755b9 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Mon, 13 Apr 2020 10:13:06 -0400

git-svn-id: branches/wasm@46184 -

nickysn 5 years ago
parent
commit
34460a9fae
2 changed files with 7 additions and 15 deletions
  1. 4 15
      utils/wasmbin/wasmbinwriter.pas
  2. 3 0
      utils/wasmbin/wasmnormalize.pas

+ 4 - 15
utils/wasmbin/wasmbinwriter.pas

@@ -245,17 +245,6 @@ begin
   reloc[i].offset:=secOfs;
   reloc[i].offset:=secOfs;
   reloc[i].index:=index;
   reloc[i].index:=index;
   inc(relocCount);
   inc(relocCount);
-
-  case relocType of
-    R_WASM_FUNCTION_INDEX_LEB:
-    begin
-      // ok, so this is function.
-      // the function MUST HAVE a symbol information available
-      // if it doesn't have a symbol then "wasm-objdump" would fail to read it
-      f:=module.GetFunc(index);
-      inc(f.codeRefCount);
-    end;
-  end;
 end;
 end;
 
 
 procedure TBinWriter.AddRelocToObj(relocType: byte; secOfs: int64; wasmObj: TObject);
 procedure TBinWriter.AddRelocToObj(relocType: byte; secOfs: int64; wasmObj: TObject);
@@ -512,6 +501,7 @@ var
   la    : TLocalInfoArray;
   la    : TLocalInfoArray;
   f     : TWasmFunc;
   f     : TWasmFunc;
   dofs  : Int64;
   dofs  : Int64;
+  fnofs : Int64; // function offset in the data of "code section"
   main  : TMemoryStream;
   main  : TMemoryStream;
 begin
 begin
   //  for the use of leb128, the header can be written ahead of the body
   //  for the use of leb128, the header can be written ahead of the body
@@ -535,7 +525,7 @@ begin
       GetLocalInfo(f, la);
       GetLocalInfo(f, la);
 
 
       mem.Position:=0;
       mem.Position:=0;
-      dofs := dofs + main.Position + 5; // "la" will be written after, 5 is for the writeSize. +5 is for WriteRelocU32(sz)
+      fnofs := dofs + main.Position + 5; // "la" will be written after, 5 is for the writeSize. +5 is for WriteRelocU32(sz)
       pushStream(mem);
       pushStream(mem);
 
 
       WriteU32(dst, length(la));
       WriteU32(dst, length(la));
@@ -543,7 +533,7 @@ begin
         WriteU32(dst, la[j].count);
         WriteU32(dst, la[j].count);
         dst.WriteByte(la[j].tp);
         dst.WriteByte(la[j].tp);
       end;
       end;
-      WriteInstList(f.instr, LongWord(dofs-sc.datapos));
+      WriteInstList(f.instr, LongWord(fnofs-sc.datapos));
       popStream;
       popStream;
 
 
       sz:=mem.Position;
       sz:=mem.Position;
@@ -679,7 +669,6 @@ var
   i   : integer;
   i   : integer;
   j   : integer;
   j   : integer;
   ci  : TWasmInstr;
   ci  : TWasmInstr;
-  idx : integer;
   rt  : Byte;
   rt  : Byte;
 begin
 begin
   for i:=0 to list.Count-1 do begin
   for i:=0 to list.Count-1 do begin
@@ -858,7 +847,7 @@ var
 begin
 begin
   for i:=0 to m.FuncCount-1 do begin
   for i:=0 to m.FuncCount-1 do begin
     f := m.GetFunc(i);
     f := m.GetFunc(i);
-    if isFuncLinkSym(f.LinkInfo) or (f.codeRefCount>0) then begin
+    if isFuncLinkSym(f.LinkInfo) then begin
       if f.LinkInfo.Name ='' then f.LinkInfo.Name := f.id;
       if f.LinkInfo.Name ='' then f.LinkInfo.Name := f.id;
       so:=AddSymbolObject(f);
       so:=AddSymbolObject(f);
       LinkInfoToBin(f.linkInfo, so.syminfo, SYMTAB_FUNCTION, f.idNum);
       LinkInfoToBin(f.linkInfo, so.syminfo, SYMTAB_FUNCTION, f.idNum);

+ 3 - 0
utils/wasmbin/wasmnormalize.pas

@@ -29,6 +29,9 @@ begin
     ipLeb:
     ipLeb:
        if (INST_RELOC_FLAGS[ci.code].doReloc) then begin
        if (INST_RELOC_FLAGS[ci.code].doReloc) then begin
          case INST_RELOC_FLAGS[ci.code].relocType of
          case INST_RELOC_FLAGS[ci.code].relocType of
+           R_WASM_FUNCTION_INDEX_LEB:
+             ci.SetReloc(INST_RELOC_FLAGS[ci.code].relocType, GetFuncByNum(module, ci.operandNum));
+
            R_WASM_GLOBAL_INDEX_LEB:
            R_WASM_GLOBAL_INDEX_LEB:
              ci.SetReloc(INST_RELOC_FLAGS[ci.code].relocType, GetGlobalByNum(module, ci.operandNum));
              ci.SetReloc(INST_RELOC_FLAGS[ci.code].relocType, GetGlobalByNum(module, ci.operandNum));