Page 1 of 1

ctor and dtor sections missing

Posted: Sat Jan 30, 2016 11:44 am
by Karlosoft
Hi everyone, I'm trying to add the support for static objects construction/destruction to C++. I'm compiling with GCC 4.9. It seems that .ctor and .dtor are no longer populated by the compiler. How to walk around this problem? I was used to keep some symbols at the beginning and at the end of these sections. Than I executed the functions before and after calling main from a different entry point. May I still do something like this?

Re: ctor and dtor sections missing

Posted: Sat Jan 30, 2016 4:29 pm
by FallenAvatar
Are you using a Linker Script? What does it contain? Where are you telling the linker to put the .ctor and .dtor sections? Are you sure that is the only name they can be? (I honestly, don't know the answer to that last one, make sure to double check with GCC's docs.)

- Monk

Re: ctor and dtor sections missing

Posted: Sat Jan 30, 2016 5:38 pm
by shmx

Code: Select all

typedef void (*func_ptr) (void);
extern func_ptr __CTOR_LIST__[];

static void __do_global_ctors(void)
{
	unsigned long nptrs = (unsigned long) __CTOR_LIST__[0];
	unsigned i;
	/*
	* If the first entry in the constructor list is -1 then the list
	* is terminated with a null entry. Otherwise the first entry was
	* the number of pointers in the list.
	*/
	if (nptrs == (unsigned)-1)
	{
		for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++)
		;
	}
	/*
	* Go through the list backwards calling constructors.
	*/
	for (i = nptrs; i >= 1; i--)
	{
		__CTOR_LIST__[i] ();
	}
}

extern int KernelMain(void *);

int entry(void *KernelParams)
{
	__do_global_ctors();
	KernelMain(KernelParams);
	return 0;
}

Re: ctor and dtor sections missing

Posted: Sat Jan 30, 2016 5:47 pm
by shmx
Similarly, for destructors (Google to the rescue).

Re: ctor and dtor sections missing

Posted: Sat Jan 30, 2016 6:26 pm
by FallenAvatar
shmx wrote:...
Not sure what either of your posts have to do with the OPs questions...

- Monk

Re: ctor and dtor sections missing

Posted: Sat Jan 30, 2016 8:31 pm
by Nutterts
Karlosoft wrote:I'm compiling with GCC 4.9. It seems that .ctor and .dtor are no longer populated by the compiler.
Like Monk already said the obvious reason might be your linker script. Afaik those sections are called .ctors & dtors so I hope it's just a simple typo.

C++ isn't my language of choice so I don't stay up to date but I remember reading something about recent versions of GCC using .init_array/fini_array instead. I might be wrong but if it isn't your linker script then that might be something to look into.

Re: ctor and dtor sections missing

Posted: Sun Jan 31, 2016 8:16 am
by Karlosoft
C++ isn't my language of choice so I don't stay up to date but I remember reading something about recent versions of GCC using .init_array/fini_array instead. I might be wrong but if it isn't your linker script then that might be something to look into.
They were .ctors and .dtors.
Actually your hint was good! There are still some problems but the job is almost done! I guess that the format of these newer structures is different. Thank you very much :).