modula-2: Fix registration of modules via constructors [PR108183].

Message ID 20230102121913.62841-1-iain@sandoe.co.uk
State Committed
Commit ec6d1fc09faea4a6be4df74babb73dca6b50ff0c
Headers
Series modula-2: Fix registration of modules via constructors [PR108183]. |

Commit Message

Iain Sandoe Jan. 2, 2023, 12:19 p.m. UTC
  When I first made this patch I had a question as to what should be
 done for registration CTORs generated by the compiler for .mod files.
 I've now answered that question (the code that makes the GCC decl
 has also be updated in a separately posted patch).
 
tested on x86_64-linux-gnu, x86_64, aarch64-darwin21,
OK for master?
Thanks,
Iain
 
 --- 8< ---

This reworks the mechanism used for module registration to use init-
time constructors.  The order of registration is not important, the
actual initialization dependency tree will be computed early in the
execution (all that matters is that we have registered before that).

This fixes a potential issue in which the external name known to the
m2 system is of the form _M2_XXXXXX_ctor() but the C++ code was
producing a static variable instance with the same name.

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>

	PR modula2/108183

gcc/m2/ChangeLog:

	* gm2-libs-ch/UnixArgs.cc (_M2_UnixArgs_ctor): Rework to use
	an extern "C" function with 'constructor' attribute.
	* gm2-libs-ch/dtoa.cc (_M2_dtoa_ctor): Likewise.
	* gm2-libs-ch/ldtoa.cc (_M2_ldtoa_ctor): Likewise.

libgm2/ChangeLog:

	* libm2cor/KeyBoardLEDs.cc (_M2_KeyBoardLEDs_ctor): Rework to use
	an extern "C" function with 'constructor' attribute.
	* libm2iso/ErrnoCategory.cc (_M2_ErrnoCategory_ctor): Likewise.
	* libm2iso/RTco.cc (_M2_RTco_ctor): Likewise.
	* libm2pim/Selective.cc (_M2_Selective_ctor): Likewise.
	* libm2pim/SysExceptions.cc (_M2_SysExceptions_ctor): Likewise.
	* libm2pim/UnixArgs.cc (_M2_UnixArgs_ctor): Likewise.
	* libm2pim/cgetopt.cc (_M2_cgetopt_ctor): Likewise.
	* libm2pim/dtoa.cc (_M2_dtoa_ctor): Likewise.
	* libm2pim/errno.cc (_M2_errno_ctor): Likewise.
	* libm2pim/ldtoa.cc (_M2_ldtoa_ctor): Likewise.
	* libm2pim/sckt.cc (_M2_sckt_ctor): Likewise.
	* libm2pim/termios.cc (_M2_termios_ctor): Likewise.
	* libm2pim/wrapc.c: Add a new line to the file end.
---
 gcc/m2/gm2-libs-ch/UnixArgs.cc   | 5 ++---
 gcc/m2/gm2-libs-ch/dtoa.cc       | 5 ++---
 gcc/m2/gm2-libs-ch/ldtoa.cc      | 5 ++---
 libgm2/libm2cor/KeyBoardLEDs.cc  | 5 ++---
 libgm2/libm2iso/ErrnoCategory.cc | 5 ++---
 libgm2/libm2iso/RTco.cc          | 5 ++---
 libgm2/libm2pim/Selective.cc     | 5 ++---
 libgm2/libm2pim/SysExceptions.cc | 5 ++---
 libgm2/libm2pim/UnixArgs.cc      | 5 ++---
 libgm2/libm2pim/cgetopt.cc       | 5 ++---
 libgm2/libm2pim/dtoa.cc          | 5 ++---
 libgm2/libm2pim/errno.cc         | 5 ++---
 libgm2/libm2pim/ldtoa.cc         | 5 ++---
 libgm2/libm2pim/sckt.cc          | 5 ++---
 libgm2/libm2pim/termios.cc       | 5 ++---
 libgm2/libm2pim/wrapc.c          | 1 +
 16 files changed, 31 insertions(+), 45 deletions(-)
  

Comments

Gaius Mulley Jan. 4, 2023, 12:05 p.m. UTC | #1
Iain Sandoe <iains.gcc@gmail.com> writes:

>  When I first made this patch I had a question as to what should be
>  done for registration CTORs generated by the compiler for .mod files.
>  I've now answered that question (the code that makes the GCC decl
>  has also be updated in a separately posted patch).
>  
> tested on x86_64-linux-gnu, x86_64, aarch64-darwin21,
> OK for master?
> Thanks,
> Iain
>  
>  --- 8< ---
>
> This reworks the mechanism used for module registration to use init-
> time constructors.  The order of registration is not important, the
> actual initialization dependency tree will be computed early in the
> execution (all that matters is that we have registered before that).
>
> This fixes a potential issue in which the external name known to the
> m2 system is of the form _M2_XXXXXX_ctor() but the C++ code was
> producing a static variable instance with the same name.
>
> Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
>
> 	PR modula2/108183
>
> gcc/m2/ChangeLog:
>
> 	* gm2-libs-ch/UnixArgs.cc (_M2_UnixArgs_ctor): Rework to use
> 	an extern "C" function with 'constructor' attribute.
> 	* gm2-libs-ch/dtoa.cc (_M2_dtoa_ctor): Likewise.
> 	* gm2-libs-ch/ldtoa.cc (_M2_ldtoa_ctor): Likewise.
>
> libgm2/ChangeLog:
>
> 	* libm2cor/KeyBoardLEDs.cc (_M2_KeyBoardLEDs_ctor): Rework to use
> 	an extern "C" function with 'constructor' attribute.
> 	* libm2iso/ErrnoCategory.cc (_M2_ErrnoCategory_ctor): Likewise.
> 	* libm2iso/RTco.cc (_M2_RTco_ctor): Likewise.
> 	* libm2pim/Selective.cc (_M2_Selective_ctor): Likewise.
> 	* libm2pim/SysExceptions.cc (_M2_SysExceptions_ctor): Likewise.
> 	* libm2pim/UnixArgs.cc (_M2_UnixArgs_ctor): Likewise.
> 	* libm2pim/cgetopt.cc (_M2_cgetopt_ctor): Likewise.
> 	* libm2pim/dtoa.cc (_M2_dtoa_ctor): Likewise.
> 	* libm2pim/errno.cc (_M2_errno_ctor): Likewise.
> 	* libm2pim/ldtoa.cc (_M2_ldtoa_ctor): Likewise.
> 	* libm2pim/sckt.cc (_M2_sckt_ctor): Likewise.
> 	* libm2pim/termios.cc (_M2_termios_ctor): Likewise.
> 	* libm2pim/wrapc.c: Add a new line to the file end.

all LGTM  (together with the other GCC decl patch),

thanks,
Gaius
  

Patch

diff --git a/gcc/m2/gm2-libs-ch/UnixArgs.cc b/gcc/m2/gm2-libs-ch/UnixArgs.cc
index 1180f351b24..4bb3769644c 100644
--- a/gcc/m2/gm2-libs-ch/UnixArgs.cc
+++ b/gcc/m2/gm2-libs-ch/UnixArgs.cc
@@ -82,9 +82,8 @@  _M2_UnixArgs_dep (void)
 {
 }
 
-struct _M2_UnixArgs_ctor { _M2_UnixArgs_ctor (); } _M2_UnixArgs_ctor;
-
-_M2_UnixArgs_ctor::_M2_UnixArgs_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_UnixArgs_ctor (void)
 {
   M2RTS_RegisterModule ("UnixArgs", _M2_UnixArgs_init, _M2_UnixArgs_finish,
 			_M2_UnixArgs_dep);
diff --git a/gcc/m2/gm2-libs-ch/dtoa.cc b/gcc/m2/gm2-libs-ch/dtoa.cc
index 57317588ba1..111fc4b67dc 100644
--- a/gcc/m2/gm2-libs-ch/dtoa.cc
+++ b/gcc/m2/gm2-libs-ch/dtoa.cc
@@ -189,9 +189,8 @@  _M2_dtoa_dep (void)
 #ifdef __cplusplus
 }
 
-struct _M2_dtoa_ctor { _M2_dtoa_ctor (); } _M2_dtoa_ctor;
-
-_M2_dtoa_ctor::_M2_dtoa_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_dtoa_ctor (void)
 {
   M2RTS_RegisterModule ("dtoa", _M2_dtoa_init, _M2_dtoa_finish,
 			_M2_dtoa_dep);
diff --git a/gcc/m2/gm2-libs-ch/ldtoa.cc b/gcc/m2/gm2-libs-ch/ldtoa.cc
index ac14297ec24..8d9e648fddf 100644
--- a/gcc/m2/gm2-libs-ch/ldtoa.cc
+++ b/gcc/m2/gm2-libs-ch/ldtoa.cc
@@ -118,9 +118,8 @@  _M2_ldtoa_dep (void)
 #   ifdef __cplusplus
 }
 
-struct _M2_ldtoa_ctor { _M2_ldtoa_ctor (); } _M2_ldtoa_ctor;
-
-_M2_ldtoa_ctor::_M2_ldtoa_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_ldtoa_ctor (void)
 {
   M2RTS_RegisterModule ("ldtoa", _M2_ldtoa_init, _M2_ldtoa_finish,
 			_M2_ldtoa_dep);
diff --git a/libgm2/libm2cor/KeyBoardLEDs.cc b/libgm2/libm2cor/KeyBoardLEDs.cc
index b640df67adc..bd0a4506a93 100644
--- a/libgm2/libm2cor/KeyBoardLEDs.cc
+++ b/libgm2/libm2cor/KeyBoardLEDs.cc
@@ -148,9 +148,8 @@  _M2_KeyBoardLEDs_dep (void)
 {
 }
 
-struct _M2_KeyBoardLEDs_ctor { _M2_KeyBoardLEDs_ctor (); } _M2_KeyBoardLEDs_ctor;
-
-_M2_KeyBoardLEDs_ctor::_M2_KeyBoardLEDs_ctor (void)
+extern "C" void __attribute__((__constructor__))
+ _M2_KeyBoardLEDs_ctor (void)
 {
   M2RTS_RegisterModule ("KeyBoardLEDs", _M2_KeyBoardLEDs_init, _M2_KeyBoardLEDs_finish,
 			_M2_KeyBoardLEDs_dep);
diff --git a/libgm2/libm2iso/ErrnoCategory.cc b/libgm2/libm2iso/ErrnoCategory.cc
index 70e840d352b..2db26393f4a 100644
--- a/libgm2/libm2iso/ErrnoCategory.cc
+++ b/libgm2/libm2iso/ErrnoCategory.cc
@@ -171,9 +171,8 @@  _M2_ErrnoCategory_dep (void)
 {
 }
 
-struct _M2_ErrnoCategory_ctor { _M2_ErrnoCategory_ctor (); } _M2_ErrnoCategory_ctor;
-
-_M2_ErrnoCategory_ctor::_M2_ErrnoCategory_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_ErrnoCategory_ctor (void)
 {
   M2RTS_RegisterModule ("ErrnoCategory", _M2_ErrnoCategory_init, _M2_ErrnoCategory_fini,
 			_M2_ErrnoCategory_dep);
diff --git a/libgm2/libm2iso/RTco.cc b/libgm2/libm2iso/RTco.cc
index 54c5078f03f..b6e46653530 100644
--- a/libgm2/libm2iso/RTco.cc
+++ b/libgm2/libm2iso/RTco.cc
@@ -459,9 +459,8 @@  RTco_init (void)
   return 0;
 }
 
-struct _M2_RTco_ctor { _M2_RTco_ctor (); } _M2_RTco_ctor;
-
-_M2_RTco_ctor::_M2_RTco_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_RTco_ctor (void)
 {
   M2RTS_RegisterModule ("RTco", _M2_RTco_init, _M2_RTco_fini,
 			_M2_RTco_dep);
diff --git a/libgm2/libm2pim/Selective.cc b/libgm2/libm2pim/Selective.cc
index a71c6577946..3c6b8553362 100644
--- a/libgm2/libm2pim/Selective.cc
+++ b/libgm2/libm2pim/Selective.cc
@@ -310,9 +310,8 @@  _M2_Selective_dep (void)
 {
 }
 
-struct _M2_Selective_ctor { _M2_Selective_ctor (); } _M2_Selective_ctor;
-
-_M2_Selective_ctor::_M2_Selective_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_Selective_ctor (void)
 {
   M2RTS_RegisterModule ("Selective", _M2_Selective_init, _M2_Selective_fini,
 			_M2_Selective_dep);
diff --git a/libgm2/libm2pim/SysExceptions.cc b/libgm2/libm2pim/SysExceptions.cc
index 780b097aaa6..2c37c7af87e 100644
--- a/libgm2/libm2pim/SysExceptions.cc
+++ b/libgm2/libm2pim/SysExceptions.cc
@@ -250,9 +250,8 @@  _M2_SysExceptions_dep (void)
 {
 }
 
-struct _M2_SysExceptions_ctor { _M2_SysExceptions_ctor (); } _M2_SysExceptions_ctor;
-
-_M2_SysExceptions_ctor::_M2_SysExceptions_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_SysExceptions_ctor (void)
 {
   M2RTS_RegisterModule ("SysExceptions", _M2_SysExceptions_init, _M2_SysExceptions_fini,
 			_M2_SysExceptions_dep);
diff --git a/libgm2/libm2pim/UnixArgs.cc b/libgm2/libm2pim/UnixArgs.cc
index 419ad8388e7..944a3cde72f 100644
--- a/libgm2/libm2pim/UnixArgs.cc
+++ b/libgm2/libm2pim/UnixArgs.cc
@@ -82,9 +82,8 @@  _M2_UnixArgs_dep (void)
 {
 }
 
-struct _M2_UnixArgs_ctor { _M2_UnixArgs_ctor (); } _M2_UnixArgs_ctor;
-
-_M2_UnixArgs_ctor::_M2_UnixArgs_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_UnixArgs_ctor (void)
 {
   M2RTS_RegisterModule ("UnixArgs", _M2_UnixArgs_init, _M2_UnixArgs_fini,
 			_M2_UnixArgs_dep);
diff --git a/libgm2/libm2pim/cgetopt.cc b/libgm2/libm2pim/cgetopt.cc
index fba20fccd22..4e8ac9e1660 100644
--- a/libgm2/libm2pim/cgetopt.cc
+++ b/libgm2/libm2pim/cgetopt.cc
@@ -149,9 +149,8 @@  _M2_cgetopt_dep (void)
 {
 }
 
-struct _M2_cgetopt_ctor { _M2_cgetopt_ctor (); } _M2_cgetopt_ctor;
-
-_M2_cgetopt_ctor::_M2_cgetopt_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_cgetopt_ctor (void)
 {
   M2RTS_RegisterModule ("cgetopt", _M2_cgetopt_init, _M2_cgetopt_fini,
 			_M2_cgetopt_dep);
diff --git a/libgm2/libm2pim/dtoa.cc b/libgm2/libm2pim/dtoa.cc
index 7b8dff97813..95d24d456f9 100644
--- a/libgm2/libm2pim/dtoa.cc
+++ b/libgm2/libm2pim/dtoa.cc
@@ -255,9 +255,8 @@  _M2_dtoa_dep (void)
 {
 }
 
-struct _M2_dtoa_ctor { _M2_dtoa_ctor (); } _M2_dtoa_ctor;
-
-_M2_dtoa_ctor::_M2_dtoa_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_dtoa_ctor (void)
 {
   M2RTS_RegisterModule ("dtoa", _M2_dtoa_init, _M2_dtoa_fini,
 			_M2_dtoa_dep);
diff --git a/libgm2/libm2pim/errno.cc b/libgm2/libm2pim/errno.cc
index d47b7b75253..296ab0b4c11 100644
--- a/libgm2/libm2pim/errno.cc
+++ b/libgm2/libm2pim/errno.cc
@@ -61,9 +61,8 @@  _M2_errno_dep (void)
 {
 }
 
-struct _M2_errno_ctor { _M2_errno_ctor (); } _M2_errno_ctor;
-
-_M2_errno_ctor::_M2_errno_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_errno_ctor (void)
 {
   M2RTS_RegisterModule ("errno", _M2_errno_init, _M2_errno_fini,
 			_M2_errno_dep);
diff --git a/libgm2/libm2pim/ldtoa.cc b/libgm2/libm2pim/ldtoa.cc
index 8f0ae68c8d0..8b449830d70 100644
--- a/libgm2/libm2pim/ldtoa.cc
+++ b/libgm2/libm2pim/ldtoa.cc
@@ -180,9 +180,8 @@  _M2_ldtoa_dep (void)
 {
 }
 
-struct _M2_ldtoa_ctor { _M2_ldtoa_ctor (); } _M2_ldtoa_ctor;
-
-_M2_ldtoa_ctor::_M2_ldtoa_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_ldtoa_ctor (void)
 {
   M2RTS_RegisterModule ("ldtoa", _M2_ldtoa_init, _M2_ldtoa_fini,
 			_M2_ldtoa_dep);
diff --git a/libgm2/libm2pim/sckt.cc b/libgm2/libm2pim/sckt.cc
index 6c68525acb0..585ed14aff0 100644
--- a/libgm2/libm2pim/sckt.cc
+++ b/libgm2/libm2pim/sckt.cc
@@ -421,9 +421,8 @@  _M2_sckt_dep (void)
 {
 }
 
-struct _M2_sckt_ctor { _M2_sckt_ctor (); } _M2_sckt_ctor;
-
-_M2_sckt_ctor::_M2_sckt_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_sckt_ctor (void)
 {
   M2RTS_RegisterModule ("sckt", _M2_sckt_init, _M2_sckt_finish,
 			_M2_sckt_dep);
diff --git a/libgm2/libm2pim/termios.cc b/libgm2/libm2pim/termios.cc
index b446bb80cf3..55818d2963e 100644
--- a/libgm2/libm2pim/termios.cc
+++ b/libgm2/libm2pim/termios.cc
@@ -1978,9 +1978,8 @@  _M2_termios_dep (void)
 {
 }
 
-struct _M2_termios_ctor { _M2_termios_ctor (); } _M2_termios_ctor;
-
-_M2_termios_ctor::_M2_termios_ctor (void)
+extern "C" void __attribute__((__constructor__))
+_M2_termios_ctor (void)
 {
   M2RTS_RegisterModule ("termios", _M2_termios_init, _M2_termios_fini,
 			_M2_termios_dep);
diff --git a/libgm2/libm2pim/wrapc.c b/libgm2/libm2pim/wrapc.c
index 5aa491219c4..8e35949a87f 100644
--- a/libgm2/libm2pim/wrapc.c
+++ b/libgm2/libm2pim/wrapc.c
@@ -294,3 +294,4 @@  void
 _M2_wrapc_ctor ()
 {
 }
+