public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/7] kernel: call constructors
@ 2008-05-19  8:43 Peter Oberparleiter
  2008-05-19 22:06 ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Oberparleiter @ 2008-05-19  8:43 UTC (permalink / raw)
  To: linux-kernel
  Cc: ltp-coverage, Andrew Morton, Sam Ravnborg, Rusty Russell,
	Peter Oberparleiter

From: Peter Oberparleiter <peter.oberparleiter@de.ibm.com>

Call constructors during kernel init and module load. Required by the
gcov profiling infrastructure: gcc's profiling code uses constructors
to register profiling data structures.

Signed-off-by: Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
---
 arch/powerpc/kernel/vmlinux.lds.S |    3 +++
 include/asm-generic/sections.h    |    1 +
 include/asm-generic/vmlinux.lds.h |    5 +++++
 include/linux/init.h              |    2 ++
 include/linux/module.h            |    3 +++
 init/main.c                       |   10 ++++++++++
 kernel/module.c                   |   13 +++++++++++++
 7 files changed, 37 insertions(+)

Index: linux-2.6.26-rc3/include/asm-generic/vmlinux.lds.h
===================================================================
--- linux-2.6.26-rc3.orig/include/asm-generic/vmlinux.lds.h
+++ linux-2.6.26-rc3/include/asm-generic/vmlinux.lds.h
@@ -352,3 +352,8 @@
 		*(.data.percpu.shared_aligned)				\
 	}								\
 	__per_cpu_end = .;
+
+#define CONSTRUCTORS							\
+		VMLINUX_SYMBOL(__ctor_start) = .;			\
+		*(.ctors)						\
+		VMLINUX_SYMBOL(__ctor_end) = .;
Index: linux-2.6.26-rc3/arch/powerpc/kernel/vmlinux.lds.S
===================================================================
--- linux-2.6.26-rc3.orig/arch/powerpc/kernel/vmlinux.lds.S
+++ linux-2.6.26-rc3/arch/powerpc/kernel/vmlinux.lds.S
@@ -193,6 +193,9 @@ SECTIONS
 		*(.toc)
 	}
 #endif
+	.data.gcov : {
+		CONSTRUCTORS
+	}
 
 	. = ALIGN(PAGE_SIZE);
 	_edata  =  .;
Index: linux-2.6.26-rc3/include/asm-generic/sections.h
===================================================================
--- linux-2.6.26-rc3.orig/include/asm-generic/sections.h
+++ linux-2.6.26-rc3/include/asm-generic/sections.h
@@ -13,5 +13,6 @@ extern char __per_cpu_start[], __per_cpu
 extern char __kprobes_text_start[], __kprobes_text_end[];
 extern char __initdata_begin[], __initdata_end[];
 extern char __start_rodata[], __end_rodata[];
+extern char __ctor_start[], __ctor_end[];
 
 #endif /* _ASM_GENERIC_SECTIONS_H_ */
Index: linux-2.6.26-rc3/include/linux/module.h
===================================================================
--- linux-2.6.26-rc3.orig/include/linux/module.h
+++ linux-2.6.26-rc3/include/linux/module.h
@@ -342,6 +342,9 @@ struct module
 	struct marker *markers;
 	unsigned int num_markers;
 #endif
+	/* Constructor calls. */
+	ctorcall_t *ctors;
+	unsigned long num_ctors;
 };
 #ifndef MODULE_ARCH_INIT
 #define MODULE_ARCH_INIT {}
Index: linux-2.6.26-rc3/kernel/module.c
===================================================================
--- linux-2.6.26-rc3.orig/kernel/module.c
+++ linux-2.6.26-rc3/kernel/module.c
@@ -1758,6 +1758,7 @@ static struct module *load_module(void _
 	unsigned int unusedgplcrcindex;
 	unsigned int markersindex;
 	unsigned int markersstringsindex;
+	unsigned int ctorsindex;
 	struct module *mod;
 	long err = 0;
 	void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
@@ -1854,6 +1855,7 @@ static struct module *load_module(void _
 #ifdef ARCH_UNWIND_SECTION_NAME
 	unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
 #endif
+	ctorsindex = find_sec(hdr, sechdrs, secstrings, ".ctors");
 
 	/* Don't keep modinfo and version sections. */
 	sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
@@ -2064,6 +2066,8 @@ static struct module *load_module(void _
 	mod->num_markers =
 		sechdrs[markersindex].sh_size / sizeof(*mod->markers);
 #endif
+	mod->ctors = (void *) sechdrs[ctorsindex].sh_addr;
+	mod->num_ctors = sechdrs[ctorsindex].sh_size / sizeof(*mod->ctors);
 
         /* Find duplicate symbols */
 	err = verify_export_symbols(mod);
@@ -2176,6 +2180,14 @@ static struct module *load_module(void _
 	goto free_hdr;
 }
 
+static void do_mod_ctors(struct module *mod)
+{
+	unsigned long i;
+
+	for (i = 0; i < mod->num_ctors; i++)
+		mod->ctors[i]();
+}
+
 /* This is where the real work happens */
 asmlinkage long
 sys_init_module(void __user *umod,
@@ -2206,6 +2218,7 @@ sys_init_module(void __user *umod,
 	blocking_notifier_call_chain(&module_notify_list,
 			MODULE_STATE_COMING, mod);
 
+	do_mod_ctors(mod);
 	/* Start the module */
 	if (mod->init != NULL)
 		ret = mod->init();
Index: linux-2.6.26-rc3/init/main.c
===================================================================
--- linux-2.6.26-rc3.orig/init/main.c
+++ linux-2.6.26-rc3/init/main.c
@@ -684,6 +684,15 @@ asmlinkage void __init start_kernel(void
 	rest_init();
 }
 
+static void __init do_ctors(void)
+{
+	ctorcall_t *call;
+
+	for (call = (ctorcall_t *) __ctor_start;
+	     call < (ctorcall_t *) __ctor_end; call++)
+		(*call)();
+}
+
 static int __initdata initcall_debug;
 
 static int __init initcall_debug_setup(char *str)
@@ -763,6 +772,7 @@ static void __init do_basic_setup(void)
 	usermodehelper_init();
 	driver_init();
 	init_irq_proc();
+	do_ctors();
 	do_initcalls();
 }
 
Index: linux-2.6.26-rc3/include/linux/init.h
===================================================================
--- linux-2.6.26-rc3.orig/include/linux/init.h
+++ linux-2.6.26-rc3/include/linux/init.h
@@ -138,6 +138,8 @@ typedef void (*exitcall_t)(void);
 extern initcall_t __con_initcall_start[], __con_initcall_end[];
 extern initcall_t __security_initcall_start[], __security_initcall_end[];
 
+typedef void (*ctorcall_t)(void);
+
 /* Defined in init/main.c */
 extern char __initdata boot_command_line[];
 extern char *saved_command_line;







^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/7] kernel: call constructors
  2008-05-19  8:43 [PATCH 1/7] kernel: call constructors Peter Oberparleiter
@ 2008-05-19 22:06 ` Andrew Morton
  2008-05-20  6:45   ` Peter Oberparleiter
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2008-05-19 22:06 UTC (permalink / raw)
  To: Peter Oberparleiter; +Cc: linux-kernel, ltp-coverage, sam, rusty, oberparleiter

On Mon, 19 May 2008 10:43:14 +0200
Peter Oberparleiter <peter.oberparleiter@de.ibm.com> wrote:

> From: Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
> 
> Call constructors during kernel init and module load. Required by the
> gcov profiling infrastructure: gcc's profiling code uses constructors
> to register profiling data structures.
> 
> Signed-off-by: Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
> ---
>  arch/powerpc/kernel/vmlinux.lds.S |    3 +++
>  include/asm-generic/sections.h    |    1 +
>  include/asm-generic/vmlinux.lds.h |    5 +++++
>  include/linux/init.h              |    2 ++
>  include/linux/module.h            |    3 +++
>  init/main.c                       |   10 ++++++++++
>  kernel/module.c                   |   13 +++++++++++++

What happens with architectures which do not use
include/asm-generic/vmlinux.lds.h?  A quicky grep points at

arch/m68k/kernel/vmlinux.lds.S
arch/sh/kernel/vmlinux.lds.S
arch/um/kernel/vmlinux.lds.S

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/7] kernel: call constructors
  2008-05-19 22:06 ` Andrew Morton
@ 2008-05-20  6:45   ` Peter Oberparleiter
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Oberparleiter @ 2008-05-20  6:45 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Peter Oberparleiter, linux-kernel, ltp-coverage, sam, rusty

Andrew Morton wrote:
> On Mon, 19 May 2008 10:43:14 +0200
> Peter Oberparleiter <peter.oberparleiter@de.ibm.com> wrote:
> 
>> From: Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
>> 
>> Call constructors during kernel init and module load. Required by the
>> gcov profiling infrastructure: gcc's profiling code uses constructors
>> to register profiling data structures.
>> 
> 
> What happens with architectures which do not use
> include/asm-generic/vmlinux.lds.h?  A quicky grep points at

In that case, no constructors would be called and as a result the gcov
debugfs directory would be empty. This should be easily fixable as the
case arises though. I've re-checked arch/*/vmlinux* and all of those
linker scripts seem to include asm-generic/vmlinux.lds.h at most via
one indirection.

> arch/m68k/kernel/vmlinux.lds.S

Includes asm-generic/vmlinux.lds.h either via vmlinux-sun3.lds or
vmlinux-std.lds.

> arch/sh/kernel/vmlinux.lds.S

Includes asm-generic/vmlinux.lds.h either via vmlinux_32.lds.S or
vmlinux_64.lds.S.

> arch/um/kernel/vmlinux.lds.S

Includes asm-generic/vmlinux.lds.h either via uml.lds.S or dyn.lds.S.


Regards,
  Peter

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-05-20  6:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-05-19  8:43 [PATCH 1/7] kernel: call constructors Peter Oberparleiter
2008-05-19 22:06 ` Andrew Morton
2008-05-20  6:45   ` Peter Oberparleiter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox