Added changes to support C++ apps
authorAndrew Waterman <waterman@r53.millennium.berkeley.edu>
Wed, 4 Nov 2009 06:26:15 +0000 (22:26 -0800)
committerAndrew Waterman <waterman@r53.millennium.berkeley.edu>
Wed, 4 Nov 2009 06:26:15 +0000 (22:26 -0800)
Specifically, static initialization and destruction.  Need to
provide to g++ a __CTOR_LIST__ and __DTOR_LIST__ section in
the binary.  Also need to call all the CTORs before entering
main(), and need to use atexit() to call all the DTORs on exit.

user/parlib/src/i386/Makefrag
user/parlib/src/i386/crtbegin.S [new file with mode: 0644]
user/parlib/src/i386/crtend.S [new file with mode: 0644]
user/parlib/src/parlibmain.c
user/parlib/src/sparc/Makefrag
user/parlib/src/sparc/crtbegin.S [new file with mode: 0644]
user/parlib/src/sparc/crtend.S [new file with mode: 0644]

index 93f663a..4b9998e 100644 (file)
@@ -11,5 +11,7 @@ OBJDIRS += $(USER_PARLIB_ARCH_SRC_DIR)
 # from the lib directory, to avoid gratuitous code duplication.
 USER_PARLIB_ARCH_SRCFILES := $(USER_PARLIB_ARCH_SRC_DIR)/syscall.c \
                              $(USER_PARLIB_ARCH_SRC_DIR)/entry.S \
+                             $(USER_PARLIB_ARCH_SRC_DIR)/crtbegin.S \
+                             $(USER_PARLIB_ARCH_SRC_DIR)/crtend.S \
                              $(USER_PARLIB_ARCH_SRC_DIR)/newlib_backend.c
 
diff --git a/user/parlib/src/i386/crtbegin.S b/user/parlib/src/i386/crtbegin.S
new file mode 100644 (file)
index 0000000..176fedb
--- /dev/null
@@ -0,0 +1,9 @@
+.section ".ctors"
+.align 8
+.global __CTOR_LIST__
+__CTOR_LIST__:
+
+.section ".dtors"
+.align 8
+.global __DTOR_LIST__
+__DTOR_LIST__:
diff --git a/user/parlib/src/i386/crtend.S b/user/parlib/src/i386/crtend.S
new file mode 100644 (file)
index 0000000..5b8e9f9
--- /dev/null
@@ -0,0 +1,7 @@
+.section ".ctors"
+.global __CTOR_END__
+__CTOR_END__:
+
+.section ".dtors"
+.global __DTOR_END__
+__DTOR_END__:
index f2d1efb..b1a2f03 100644 (file)
@@ -5,8 +5,34 @@
 #include <unistd.h>
 #include <debug.h>
 
+// call static destructors.
+void parlib_dtors()
+{
+       typedef void (*dtor)(void);
+       extern dtor __DTOR_LIST__[], __DTOR_END__[];
+
+       for(int i = 0; i < __DTOR_END__ - __DTOR_LIST__; i++)
+               __DTOR_LIST__[i]();
+}
+
+// call static constructors.
+void parlib_ctors()
+{
+       typedef void (*ctor)(void);
+       extern ctor __CTOR_LIST__[],__CTOR_END__[];
+
+       for(int i = -1; i >= __CTOR_LIST__-__CTOR_END__; i--)
+               __CTOR_END__[i]();
+}
+
 void parlibmain()
 {
+       // call static destructors
+       parlib_ctors();
+
+       // call static destructors on exit
+       atexit(&parlib_dtors);
+
        // call user main routine
        extern int main(int argc, char * NTS * COUNT(argc) NT argv);
        int r = main(procinfo.argc, procinfo.argv);
index cad10d6..0ad7a11 100644 (file)
@@ -11,4 +11,6 @@ OBJDIRS += $(USER_PARLIB_ARCH_SRC_DIR)
 # from the lib directory, to avoid gratuitous code duplication.
 USER_PARLIB_ARCH_SRCFILES := $(USER_PARLIB_ARCH_SRC_DIR)/syscall.c \
                              $(USER_PARLIB_ARCH_SRC_DIR)/entry.S \
+                             $(USER_PARLIB_ARCH_SRC_DIR)/crtbegin.S \
+                             $(USER_PARLIB_ARCH_SRC_DIR)/crtend.S \
                              $(USER_PARLIB_ARCH_SRC_DIR)/newlib_backend.c
diff --git a/user/parlib/src/sparc/crtbegin.S b/user/parlib/src/sparc/crtbegin.S
new file mode 100644 (file)
index 0000000..176fedb
--- /dev/null
@@ -0,0 +1,9 @@
+.section ".ctors"
+.align 8
+.global __CTOR_LIST__
+__CTOR_LIST__:
+
+.section ".dtors"
+.align 8
+.global __DTOR_LIST__
+__DTOR_LIST__:
diff --git a/user/parlib/src/sparc/crtend.S b/user/parlib/src/sparc/crtend.S
new file mode 100644 (file)
index 0000000..5b8e9f9
--- /dev/null
@@ -0,0 +1,7 @@
+.section ".ctors"
+.global __CTOR_END__
+__CTOR_END__:
+
+.section ".dtors"
+.global __DTOR_END__
+__DTOR_END__: