Index: mutek/include/mutek/ctors.h
===================================================================
--- mutek/include/mutek/ctors.h	(révision 0)
+++ mutek/include/mutek/ctors.h	(révision 0)
@@ -0,0 +1,46 @@
+/*
+    This file is part of MutekH.
+
+    MutekH is free software; you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    MutekH 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.  See the GNU
+    General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with MutekH; if not, write to the Free Software Foundation,
+    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+    Copyright Alexandre Becoulet <alexandre.becoulet@lip6.fr> (c) 2010
+*/
+
+#ifndef _MUTEK_CTORS_H_
+#define _MUTEK_CTORS_H_
+
+#include <hexo/types.h>
+#include <hexo/error.h>
+
+#define __MUTEK_AT_START(n) error_t (n)(uint_fast8_t level)
+typedef __MUTEK_AT_START(mutek_at_start_t);
+
+#define __MUTEK_AT_END(n) void (n)()
+typedef __MUTEK_AT_END(mutek_at_end_t);
+
+#define MUTEK_AT_START(n) __attribute__((constructor,cold)) __MUTEK_AT_START(n)
+#define MUTEK_AT_END(n) __attribute__((destructor,cold)) __MUTEK_AT_END(n)
+
+enum mutek_start_level_e
+  {
+    MUTEK_AT_DRVINIT_LEVEL,
+    MUTEK_AT_MUTEK_START_LEVEL,
+  };
+
+void execute_at_start(uint_fast8_t level);
+void execute_at_end();
+
+#endif
+
Index: mutek/ctors.c
===================================================================
--- mutek/ctors.c	(révision 0)
+++ mutek/ctors.c	(révision 0)
@@ -0,0 +1,51 @@
+/*
+    This file is part of MutekH.
+
+    MutekH is free software; you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    MutekH 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.  See the GNU
+    General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with MutekH; if not, write to the Free Software Foundation,
+    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+    Copyright Alexandre Becoulet <alexandre.becoulet@lip6.fr> (c) 2010
+*/
+
+#include <mutek/ctors.h>
+
+extern __ldscript_symbol_t ctors_begin, ctors_end;
+extern __ldscript_symbol_t dtors_begin, dtors_end;
+
+void execute_at_start(uint_fast8_t level)
+{
+  mutek_at_start_t **ctors = (mutek_at_start_t**)&ctors_begin;
+  size_t i, count = (mutek_at_start_t**)&ctors_end - ctors;
+
+  for (i = 0; i < count; i++)
+    {
+      if (ctors[i] == NULL)
+	continue;
+
+      if (ctors[i](level))
+	continue;
+
+      ctors[i] = NULL;
+    }
+}
+
+void execute_at_end()
+{
+  mutek_at_end_t **dtors = (mutek_at_end_t**)&dtors_begin;
+  size_t i, count = (mutek_at_end_t**)&dtors_end - dtors;
+
+  for (i = 0; i < count; i++)
+    dtors[i]();
+}
+
Index: mutek/Makefile
===================================================================
--- mutek/Makefile	(révision 1463)
+++ mutek/Makefile	(copie de travail)
@@ -3,7 +3,7 @@
 	mutek/printk.h mutek/rwlock.h mutek/scheduler.h mutek/timer.h	\
 	mutek/vmem_kalloc.h mutek/semaphore.h mutek/fdt.h mutek/console.h
 
-objs = timer.o printf_arg.o console.o
+objs = timer.o printf_arg.o console.o ctors.o
 
 ifeq ($(CONFIG_MUTEK_MEM_REGION), defined)
 objs += mem_region.o					
Index: arch/emu/arch_init.c
===================================================================
--- arch/emu/arch_init.c	(révision 1463)
+++ arch/emu/arch_init.c	(copie de travail)
@@ -116,6 +116,8 @@
     sched_cpu_init();
 #endif
 
+    execute_at_start(MUTEK_AT_DRVINIT_LEVEL);
+
     arch_hw_init();
     mem_region_init();
 
@@ -123,6 +125,8 @@
     cpu_init_flag = 1;
 #endif
 
+    execute_at_start(MUTEK_AT_MUTEK_START_LEVEL);
+
     /* run mutek_start() */
     mutek_start(0, 0);
 
Index: arch/emu/ldscript.cpp
===================================================================
--- arch/emu/ldscript.cpp	(révision 1463)
+++ arch/emu/ldscript.cpp	(copie de travail)
@@ -17,8 +17,14 @@
 			*(.drivers)
 			global_driver_registry_end = .;
 
-			/* data depending on cpu architecture (fonction pointer variables, ...) */
-			*(.cpuarchdata*)
+			// constructors and destructors
+			. = ALIGN(4);
+			ctors_begin = .;
+			KEEP(*(.ctors))
+			  ctors_end = .;
+			dtors_begin = .;
+			KEEP(*(.dtors))
+			  dtors_end = .;
 		}
         __data_start = LOADADDR(.data);
     __data_end = LOADADDR(.data) + SIZEOF(.data);
Index: arch/ibmpc/arch_init.c
===================================================================
--- arch/ibmpc/arch_init.c	(révision 1463)
+++ arch/ibmpc/arch_init.c	(copie de travail)
@@ -35,6 +35,7 @@
 #include <mutek/mem_alloc.h>
 #include <mutek/scheduler.h>
 #include <mutek/printk.h>
+#include <mutek/ctors.h>
 
 #ifdef CONFIG_DRIVER_ICU_APIC
 # include <drivers/icu/apic/icu-apic.h>
@@ -159,6 +160,8 @@
       sched_cpu_init();
 #endif
 
+      execute_at_start(MUTEK_AT_DRVINIT_LEVEL);
+
 #if defined(CONFIG_ARCH_HW_INIT_USER)
 	  user_hw_init();
 #elif defined(CONFIG_ARCH_HW_INIT)
@@ -167,6 +170,7 @@
 # error No supported hardware initialization
 #endif
 
+	  execute_at_start(MUTEK_AT_MUTEK_START_LEVEL);
 
       /* run mutek_start() */
       mutek_start(0, 0);
Index: arch/ibmpc/ldscript.cpp
===================================================================
--- arch/ibmpc/ldscript.cpp	(révision 1463)
+++ arch/ibmpc/ldscript.cpp	(copie de travail)
@@ -25,7 +25,16 @@
 	.sdata : { *(.sdata*) } > mem_ram
 	.sbss :	 { *(.sbss*) } > mem_ram
 	.bss :	 { *(.bss*) } > mem_ram
-	.data :	 { *(.data*) } > mem_ram
+	.data :	 { *(.data*) 
+		   // constructors and destructors
+		   . = ALIGN(4);
+                   ctors_begin = .;
+                   KEEP(*(.ctors))
+                   ctors_end = .;
+                   dtors_begin = .;
+                   KEEP(*(.dtors))
+                   dtors_end = .;
+                  } > mem_ram
 	.cpuarchdata : { *(.cpuarchdata*) } > mem_ram
 
 	__system_heap_start = ADDR(.cpuarchdata) + SIZEOF(.cpuarchdata);
Index: arch/soclib/arch_init.c
===================================================================
--- arch/soclib/arch_init.c	(révision 1463)
+++ arch/soclib/arch_init.c	(copie de travail)
@@ -43,6 +43,7 @@
 #include <hexo/cpu.h>
 #include <mutek/printk.h>
 #include <mutek/scheduler.h>
+#include <mutek/ctors.h>
 
 #include <string.h>
 
@@ -141,6 +142,8 @@
         mmu_cpu_init();
 #endif
 
+        execute_at_start(MUTEK_AT_DRVINIT_LEVEL);
+
 #if defined(CONFIG_ARCH_DEVICE_TREE)
         device_init(&fdt_enum_dev);
         enum_fdt_init(&fdt_enum_dev, device_tree);
@@ -174,6 +177,8 @@
         sched_cpu_init();
 #endif
 
+        execute_at_start(MUTEK_AT_MUTEK_START_LEVEL);
+
         /* run mutek_start() */
         mutek_start(0, 0);
 #ifdef CONFIG_ARCH_SMP
Index: arch/soclib/ldscript.cpp
===================================================================
--- arch/soclib/ldscript.cpp	(révision 1463)
+++ arch/soclib/ldscript.cpp	(copie de travail)
@@ -120,7 +120,15 @@
 		__data_start = ABSOLUTE(.);
 		*(.sdata*)
 		*(.data*)
-		*(.cpuarchdata*)
+
+		// constructors and destructors
+		. = ALIGN(4);
+		ctors_begin = .;
+		KEEP(*(.ctors))
+		ctors_end = .;
+		dtors_begin = .;
+		KEEP(*(.dtors))
+		dtors_end = .;
 	} > mem_ram __AT_MEM_ROM
 
 	__data_load_start = LOADADDR(.data);
Index: examples/hello/hello.c
===================================================================
--- examples/hello/hello.c	(révision 1463)
+++ examples/hello/hello.c	(copie de travail)
@@ -1,6 +1,7 @@
 
 #include <pthread.h>
 #include <mutek/printk.h>
+#include <mutek/ctors.h>
 
 pthread_mutex_t m;
 pthread_t a, b;
@@ -18,8 +19,15 @@
 
 void app_start()
 {
+  return;
   pthread_mutex_init(&m, NULL);
   pthread_create(&a, NULL, f, "Hello world\n");
   pthread_create(&b, NULL, f, "Hello world\n");
 }
 
+static MUTEK_AT_START(myinit)
+{
+  printk("init level %i\n", level);
+  return -EAGAIN;
+}
+
