Browse Source

+ wasm internal linker optimization: write only the non-empty data sections to
the resulting .wasm file (the 'fpc.resources' section specifically can be
empty, if the binary doesn't use resources)

Nikolay Nikolov 1 year ago
parent
commit
07da0058e3
1 changed files with 17 additions and 4 deletions
  1. 17 4
      compiler/ogwasm.pas

+ 17 - 4
compiler/ogwasm.pas

@@ -4632,15 +4632,28 @@ implementation
               internalerror(2024010107);
               internalerror(2024010107);
           end;
           end;
 
 
+        const
+          DataSections: array [1..3] of string = (
+            '.rodata',
+            '.data',
+            'fpc.resources');
         var
         var
           DataCount: Integer;
           DataCount: Integer;
+          DataSecName: string;
+          ExeSec: TExeSection;
         begin
         begin
-          DataCount:=3;
+          DataCount:=0;
+          for DataSecName in DataSections do
+            if FindExeSection(DataSecName).Size>0 then
+              Inc(DataCount);
           WriteUleb(FWasmSections[wsiDataCount],DataCount);
           WriteUleb(FWasmSections[wsiDataCount],DataCount);
           WriteUleb(FWasmSections[wsiData],DataCount);
           WriteUleb(FWasmSections[wsiData],DataCount);
-          WriteExeSection(FindExeSection('.rodata'));
-          WriteExeSection(FindExeSection('.data'));
-          WriteExeSection(FindExeSection('fpc.resources'));
+          for DataSecName in DataSections do
+            begin
+              ExeSec:=FindExeSection(DataSecName);
+              if ExeSec.Size>0 then
+                WriteExeSection(ExeSec);
+            end;
         end;
         end;
 
 
       procedure WriteTableAndElemSections;
       procedure WriteTableAndElemSections;