Browse Source

[PATCH 054/188] writing relocation information

From f0b77566cbe8bb85d0a6c480e48b673ce5141367 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Sat, 23 Nov 2019 19:56:14 -0500

git-svn-id: branches/wasm@46050 -
nickysn 5 years ago
parent
commit
cbf2541394
2 changed files with 51 additions and 7 deletions
  1. 50 7
      utils/wasmbin/wasmbinwriter.pas
  2. 1 0
      utils/wasmbin/wasmlink.pas

+ 50 - 7
utils/wasmbin/wasmbinwriter.pas

@@ -25,9 +25,9 @@ type
     strm : TList;
     strm : TList;
 
 
     // the list of relocations per module
     // the list of relocations per module
+    writeSec   : Byte;
     reloc      : array of TRelocationEntry;
     reloc      : array of TRelocationEntry;
     relocCount : integer;
     relocCount : integer;
-
     procedure AddReloc(relocType: byte; ofs: int64; index: UInt32);
     procedure AddReloc(relocType: byte; ofs: int64; index: UInt32);
 
 
     procedure WriteRelocU32(u: longword);
     procedure WriteRelocU32(u: longword);
@@ -130,10 +130,11 @@ begin
     if relocCount=0 then SetLength(reloc, 16)
     if relocCount=0 then SetLength(reloc, 16)
     else SetLength(reloc, relocCount*2);
     else SetLength(reloc, relocCount*2);
   end;
   end;
+  reloc[relocCount].sec:=writeSec;
   reloc[relocCount].reltype:=relocType;
   reloc[relocCount].reltype:=relocType;
   reloc[relocCount].offset:=ofs;
   reloc[relocCount].offset:=ofs;
   reloc[relocCount].index:=index;
   reloc[relocCount].index:=index;
-  inc(relocType);
+  inc(relocCount);
 end;
 end;
 
 
 procedure TBinWriter.WriteRelocU32(u: longword);
 procedure TBinWriter.WriteRelocU32(u: longword);
@@ -165,17 +166,30 @@ begin
   l:=NtoLE(Wasm_Version1);
   l:=NtoLE(Wasm_Version1);
   dst.Write(l, sizeof(l));
   dst.Write(l, sizeof(l));
 
 
+  writeSec:=0;
   // 01 function type section
   // 01 function type section
-  WriteFuncTypeSect(m);
+  if m.TypesCount>0 then begin
+    WriteFuncTypeSect(m);
+    inc(writeSec);
+  end;
 
 
   // 03 function section
   // 03 function section
-  WriteFuncSect(m);
+  if m.FuncCount>0 then begin
+    WriteFuncSect(m);
+    inc(writeSec);
+  end;
 
 
   // 07 export section
   // 07 export section
-  WriteExportSect(m);
+  if m.ExportCount>0 then begin
+    WriteExportSect(m);
+    inc(writeSec);
+  end;
 
 
   // 10 code section
   // 10 code section
-  WriteCodeSect(m);
+  if m.FuncCount>0 then begin
+    WriteCodeSect(m);
+    inc(writeSec);
+  end;
 
 
   if writeReloc then begin
   if writeReloc then begin
     WriteLinkingSect(m);
     WriteLinkingSect(m);
@@ -331,8 +345,37 @@ begin
 end;
 end;
 
 
 procedure TBinWriter.WriteRelocSect(m: TWasmModule);
 procedure TBinWriter.WriteRelocSect(m: TWasmModule);
+var
+  i  : integer;
+  j  : integer;
+  si : Byte;
+  cnt: integer;
+  sc : TSectionRec;
 begin
 begin
-
+  si:=0;
+  i:=0;
+
+  while (si<writeSec) and (i<relocCount) do begin
+    if reloc[i].sec=si then begin
+      SectionBegin(SECT_CUSTOM, sc);
+      WriteString(SectionNamePfx_Reloc+'Code');
+      j:=i;
+      cnt:=0;
+      while (i<relocCount) and (reloc[i].sec=si) do begin
+        inc(cnt);
+        inc(i);
+      end;
+      WriteU32(dst, reloc[j].sec);
+      WriteU32(dst, cnt);
+      for j:=j to i-1 do begin
+        dst.WriteByte(reloc[j].reltype);
+        WriteU32(dst, reloc[j].offset);
+        WriteU32(dst, reloc[j].index);
+      end;
+      SectionEnd(sc);
+    end;
+    inc(si);
+  end;
 end;
 end;
 
 
 procedure TBinWriter.WriteInstList(list: TWasmInstrList; ofsAddition: LongWord);
 procedure TBinWriter.WriteInstList(list: TWasmInstrList; ofsAddition: LongWord);

+ 1 - 0
utils/wasmbin/wasmlink.pas

@@ -18,6 +18,7 @@ type
   end;
   end;
 
 
   TRelocationEntry = record
   TRelocationEntry = record
+    sec     : Uint8;  // section to be relocated at
     reltype : UInt8;  // the relocation type (see R_WASM constants)
     reltype : UInt8;  // the relocation type (see R_WASM constants)
     offset  : UInt32; // offset of the value to rewrite
     offset  : UInt32; // offset of the value to rewrite
     index   : Uint32; // the index of the symbol used (or, for R_WASM_TYPE_INDEX_LEB relocations, the index of the type)
     index   : Uint32; // the index of the symbol used (or, for R_WASM_TYPE_INDEX_LEB relocations, the index of the type)