ctor and dtor sections missing

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

ctor and dtor sections missing

Post 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?
FallenAvatar
Member
Member
Posts: 283
Joined: Mon Jan 03, 2011 6:58 pm

Re: ctor and dtor sections missing

Post 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
shmx
Member
Member
Posts: 68
Joined: Sat Jan 16, 2016 10:43 am

Re: ctor and dtor sections missing

Post 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;
}
shmx
Member
Member
Posts: 68
Joined: Sat Jan 16, 2016 10:43 am

Re: ctor and dtor sections missing

Post by shmx »

Similarly, for destructors (Google to the rescue).
FallenAvatar
Member
Member
Posts: 283
Joined: Mon Jan 03, 2011 6:58 pm

Re: ctor and dtor sections missing

Post by FallenAvatar »

shmx wrote:...
Not sure what either of your posts have to do with the OPs questions...

- Monk
User avatar
Nutterts
Member
Member
Posts: 159
Joined: Wed Aug 05, 2015 5:33 pm
Libera.chat IRC: Nutterts
Location: Drenthe, Netherlands

Re: ctor and dtor sections missing

Post 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.
"Always code as if the guy who ends up maintaining it will be a violent psychopath who knows where you live." - John F. Woods

Failed project: GoOS - https://github.com/nutterts/GoOS
User avatar
Karlosoft
Member
Member
Posts: 277
Joined: Thu Feb 14, 2008 10:46 am
Location: Italy
Contact:

Re: ctor and dtor sections missing

Post 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 :).
Post Reply