GCC question

Franck Jullien franck.jullien at gmail.com
Wed Jan 9 04:02:32 EST 2013


I have a question not directly related to Barebox but I think I can
find some answer here: )

I would like to use initcalls in a Linux user's land program on a x86 target.

I'm doing something like this:

#ifndef _INIT_H
#define _INIT_H

typedef int (*initcall_t)(void);

extern initcall_t __start_target, __stop_target;

#define target_initcall(fn)	static initcall_t _##fn \
				__attribute__((used)) \
				__attribute__ ((section("target"))) = fn



	initcall_t *initcall;

	for (initcall = &__start_target;
			initcall < &__stop_target; initcall++) {
		printf("initcall-> %p\n", *initcall);
		ret = (*initcall)();
		if (ret)
			printf("initcall %p failed: %d\n", *initcall, ret);

Everything looks fine except the linker removes the function
"initcalled" because it is not
referenced anywhere and this is normal.

I have not modified the linker script (I'm using the default one). I'm
using auto generated
__start_target and __stop_target symbols generated by the linker.

My question is: why does it work in barebox ? For example, in
nios2/generic.c we have only
static function and initcalls. So why the linker does optimize out
those functions ? Is it
because we have initcall corresponding sections in the linker script ?

Thank you,


More information about the barebox mailing list