From 590c8786902fb712aa84e1e87351f0cf484380a5 Mon Sep 17 00:00:00 2001 From: Nikolay Nikolov Date: Wed, 16 Feb 2022 05:24:41 +0200 Subject: [PATCH] + added separate startup code for libraries, that declares _initialize, instead of _start --- compiler/systems/t_wasi.pas | 5 +++- rtl/wasi/Makefile | 4 ++- rtl/wasi/Makefile.fpc | 5 +++- rtl/wasi/si_dll.pp | 53 +++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 rtl/wasi/si_dll.pp diff --git a/compiler/systems/t_wasi.pas b/compiler/systems/t_wasi.pas index d9ca02e689..f8c4b57085 100644 --- a/compiler/systems/t_wasi.pas +++ b/compiler/systems/t_wasi.pas @@ -103,7 +103,10 @@ end; procedure tlinkerwasi.InitSysInitUnitName; begin - sysinitunit:='si_prc'; + if current_module.islibrary then + sysinitunit:='si_dll' + else + sysinitunit:='si_prc'; end; function tlinkerwasi.MakeExecutable:boolean; diff --git a/rtl/wasi/Makefile b/rtl/wasi/Makefile index f23540f7c8..2d64f2fc05 100644 --- a/rtl/wasi/Makefile +++ b/rtl/wasi/Makefile @@ -359,7 +359,7 @@ COMMON=$(RTL)/common PROCINC=../$(CPU_TARGET) UNITPREFIX=rtl SYSTEMUNIT=system -SYSINIT_UNITS=si_prc +SYSINIT_UNITS=si_prc si_dll OBJPASDIR=$(RTL)/objpas ifdef EXCEPTIONS_IN_SYSTEM override FPCOPT+=-dEXCEPTIONS_IN_SYSTEM @@ -2699,6 +2699,8 @@ extpas$(PPUEXT) : $(INC)/extpas.pp dos$(PPUEXT) $(SYSTEMUNIT)$(PPUEXT) $(COMPILER) $< si_prc$(PPUEXT) : si_prc.pp $(SYSTEMUNIT)$(PPUEXT) $(COMPILER) $< +si_dll$(PPUEXT) : si_dll.pp $(SYSTEMUNIT)$(PPUEXT) + $(COMPILER) $< wasiapi$(PPUEXT) : wasiapi.pp wasiinc/wasitypes.inc wasiinc/wasiprocs.inc $(SYSTEMUNIT)$(PPUEXT) $(COMPILER) $< -Fiwasiinc wasiutil$(PPUEXT) : wasiutil.pp wasiapi$(PPUEXT) objpas$(PPUEXT) $(SYSTEMUNIT)$(PPUEXT) diff --git a/rtl/wasi/Makefile.fpc b/rtl/wasi/Makefile.fpc index 90c0921b37..3a09f58d3b 100644 --- a/rtl/wasi/Makefile.fpc +++ b/rtl/wasi/Makefile.fpc @@ -28,7 +28,7 @@ COMMON=$(RTL)/common PROCINC=../$(CPU_TARGET) UNITPREFIX=rtl SYSTEMUNIT=system -SYSINIT_UNITS=si_prc +SYSINIT_UNITS=si_prc si_dll # Paths OBJPASDIR=$(RTL)/objpas @@ -83,6 +83,9 @@ extpas$(PPUEXT) : $(INC)/extpas.pp dos$(PPUEXT) $(SYSTEMUNIT)$(PPUEXT) si_prc$(PPUEXT) : si_prc.pp $(SYSTEMUNIT)$(PPUEXT) $(COMPILER) $< +si_dll$(PPUEXT) : si_dll.pp $(SYSTEMUNIT)$(PPUEXT) + $(COMPILER) $< + # # Other $(SYSTEMUNIT)-dependent RTL Units # diff --git a/rtl/wasi/si_dll.pp b/rtl/wasi/si_dll.pp new file mode 100644 index 0000000000..0f4654d8c4 --- /dev/null +++ b/rtl/wasi/si_dll.pp @@ -0,0 +1,53 @@ +{ + This file is part of the Free Pascal run time library. + Copyright (c) 2021, 2022 by Free Pascal development team + + This file implements the startup code for WebAssembly programs that + don't link to the C library. + + See the file COPYING.FPC, included in this distribution, + for details about the copyright. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + **********************************************************************} + + +unit si_dll; + +{$if defined(FPC_WASM_BRANCHFUL_EXCEPTIONS) or defined(FPC_WASM_NATIVE_EXCEPTIONS)} + {$MODESWITCH EXCEPTIONS} +{$endif} + +interface + +procedure _initialize; + +implementation + +procedure PASCALMAIN; external 'PASCALMAIN'; + +{$if defined(FPC_WASM_BRANCHFUL_EXCEPTIONS) or defined(FPC_WASM_NATIVE_EXCEPTIONS)} +Procedure DoUnHandledException; external name 'FPC_DOUNHANDLEDEXCEPTION'; + +procedure _initialize; +begin + try + PASCALMAIN; + except + DoUnhandledException; + end; +end; +{$else} +procedure _initialize; +begin + PASCALMAIN; +end; +{$endif} + +exports + _initialize; + +end.