public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Figuring out start and end of sections
@ 2010-04-13 17:40 Felipe Balbi
  2010-04-13 17:44 ` Andrew Haley
  2010-04-14  0:24 ` Figuring " Ian Lance Taylor
  0 siblings, 2 replies; 10+ messages in thread
From: Felipe Balbi @ 2010-04-13 17:40 UTC (permalink / raw)
  To: gcc-help

Hi all,

is there any way to figure out where a section starts and ends ?

I added a specific section to my program using
__attribute__((section "<section name>")) and now I want to figure out
where that section starts so I can iterate over it and call the function
pointers I'm adding to it.

Do I need a specific linker script to achieve that or does the default
scripts give me possibility to find that out ?

-- 
balbi

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

* Re: Figuring out start and end of sections
  2010-04-13 17:40 Figuring out start and end of sections Felipe Balbi
@ 2010-04-13 17:44 ` Andrew Haley
  2010-04-14  5:08   ` Figurig " Felipe Balbi
  2010-04-14  0:24 ` Figuring " Ian Lance Taylor
  1 sibling, 1 reply; 10+ messages in thread
From: Andrew Haley @ 2010-04-13 17:44 UTC (permalink / raw)
  To: gcc-help

On 04/13/2010 06:40 PM, Felipe Balbi wrote:

> is there any way to figure out where a section starts and ends ?
> 
> I added a specific section to my program using
> __attribute__((section "<section name>")) and now I want to figure out
> where that section starts so I can iterate over it and call the function
> pointers I'm adding to it.

Terminate the list with a null pointer; AFAIK that's what everyone else
does.  To get the start address, you just need to define a global variable
in that section.  Of course this means you have to link everything in the
correct order.

Andrew.

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

* Re: Figuring out start and end of sections
  2010-04-13 17:40 Figuring out start and end of sections Felipe Balbi
  2010-04-13 17:44 ` Andrew Haley
@ 2010-04-14  0:24 ` Ian Lance Taylor
  2010-04-14  8:49   ` Andrew Haley
  1 sibling, 1 reply; 10+ messages in thread
From: Ian Lance Taylor @ 2010-04-14  0:24 UTC (permalink / raw)
  To: me; +Cc: gcc-help

Felipe Balbi <me@felipebalbi.com> writes:

> is there any way to figure out where a section starts and ends ?
>
> I added a specific section to my program using
> __attribute__((section "<section name>")) and now I want to figure out
> where that section starts so I can iterate over it and call the function
> pointers I'm adding to it.
>
> Do I need a specific linker script to achieve that or does the default
> scripts give me possibility to find that out ?

If you are using the GNU linker, or gold, and you make the section
name a valid C identifier, then the linker will automatically define
symbols __start_SECNAME and __stop_SECNAME which you can use.

Ian

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

* Re: Figurig out start and end of sections
  2010-04-13 17:44 ` Andrew Haley
@ 2010-04-14  5:08   ` Felipe Balbi
  2010-04-14  8:49     ` Andrew Haley
  0 siblings, 1 reply; 10+ messages in thread
From: Felipe Balbi @ 2010-04-14  5:08 UTC (permalink / raw)
  To: Andrew Haley; +Cc: gcc-help

Hi Andrew,

for some reason I didn't receive your reply.

>On 04/13/2010 06:40 PM, Felipe Balbi wrote:
>> is there any way to figure out where a section starts and ends ?
>> 
>> I added a specific section to my program using
>> __attribute__((section "<section name>")) and now I want to figure out
>> where that section starts so I can iterate over it and call the
>> function
>> pointers I'm adding to it.
>
>Terminate the list with a null pointer; AFAIK that's what everyone else
>does.  To get the start address, you just need to define a global
>variable
>in that section.  Of course this means you have to link everything in
>the
>correct order.

I think you're talking about something like:

void (*func_ptr)(void)[] __attribute__((section ".my_section")) = {
	func1,
	func2,
	func3,
	NULL,
};

is that right ? (the code above isn't really correct).

I don't really have an array. I'm trying to mimic what the kernel does
with the modules, but then again, the kernel declares the constants for
the start and end of initcalls in constants within the ld script.

So what I have right now is:

main.c:

[..]

int __attribute__((section(".init"))) register_struct(struct my_struct *ptr)
{
	[..]
}

[..]

static int call_initcalls(void)
{
	initcall_t	*fn;

	for (fn = __start_initcall; fn; fn++)
		fn();

	return 0;
}

then on another source code, I define an initcall_t.

initcall.c:

[..]

static int __attribute__((section(".init"))) init(void)
{
	return register_struct(ptr);
}

static initcall_t initcall_t_init __attribute__((section(".my.init")))
	__attribute__((__used__)) = init;

then several little "plugins" would define that and all those pointers
are going to the right section. Now how to fetch them and call them ??
currently I'm getting segfault. :-p

-- 
balbi

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

* Re: Figurig out start and end of sections
  2010-04-14  5:08   ` Figurig " Felipe Balbi
@ 2010-04-14  8:49     ` Andrew Haley
  0 siblings, 0 replies; 10+ messages in thread
From: Andrew Haley @ 2010-04-14  8:49 UTC (permalink / raw)
  To: me; +Cc: gcc-help

On 04/14/2010 06:08 AM, Felipe Balbi wrote:
> Hi Andrew,
> 
> for some reason I didn't receive your reply.
> 
>> On 04/13/2010 06:40 PM, Felipe Balbi wrote:
>>> is there any way to figure out where a section starts and ends ?
>>>
>>> I added a specific section to my program using
>>> __attribute__((section "<section name>")) and now I want to figure out
>>> where that section starts so I can iterate over it and call the
>>> function
>>> pointers I'm adding to it.
>>
>> Terminate the list with a null pointer; AFAIK that's what everyone else
>> does.  To get the start address, you just need to define a global
>> variable
>> in that section.  Of course this means you have to link everything in
>> the
>> correct order.
> 
> I think you're talking about something like:
> 
> void (*func_ptr)(void)[] __attribute__((section ".my_section")) = {
> 	func1,
> 	func2,
> 	func3,
> 	NULL,
> };

No.

void (*func_ptr)(void) __attribute__((section ".my_section") = foo;

void (*func_ptr)(void) __attribute__((section ".my_section") = bar;

etc, etc.  I'm assuming the entries are in different files.
And in the last file,

void *poo __attribute__((section ".my_section") = NULL;

> is that right ? (the code above isn't really correct).
> 
> I don't really have an array. I'm trying to mimic what the kernel does
> with the modules, but then again, the kernel declares the constants for
> the start and end of initcalls in constants within the ld script.
> 
> So what I have right now is:
> 
> main.c:
> 
> [..]
> 
> int __attribute__((section(".init"))) register_struct(struct my_struct *ptr)
> {
> 	[..]
> }
> 
> [..]
> 
> static int call_initcalls(void)
> {
> 	initcall_t	*fn;
> 
> 	for (fn = __start_initcall; fn; fn++)
> 		fn();

I don't understand this.  How can you increment a function pointer?


> 
> 	return 0;
> }
> 
> then on another source code, I define an initcall_t.
> 
> initcall.c:
> 
> [..]
> 
> static int __attribute__((section(".init"))) init(void)
> {
> 	return register_struct(ptr);
> }
> 
> static initcall_t initcall_t_init __attribute__((section(".my.init")))
> 	__attribute__((__used__)) = init;
> 
> then several little "plugins" would define that and all those pointers
> are going to the right section. Now how to fetch them and call them ??
> currently I'm getting segfault. :-p
> 

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

* Re: Figuring out start and end of sections
  2010-04-14  0:24 ` Figuring " Ian Lance Taylor
@ 2010-04-14  8:49   ` Andrew Haley
  0 siblings, 0 replies; 10+ messages in thread
From: Andrew Haley @ 2010-04-14  8:49 UTC (permalink / raw)
  To: gcc-help; +Cc: me

On 04/14/2010 01:24 AM, Ian Lance Taylor wrote:
> Felipe Balbi <me@felipebalbi.com> writes:
> 
>> is there any way to figure out where a section starts and ends ?
>>
>> I added a specific section to my program using
>> __attribute__((section "<section name>")) and now I want to figure out
>> where that section starts so I can iterate over it and call the function
>> pointers I'm adding to it.
>>
>> Do I need a specific linker script to achieve that or does the default
>> scripts give me possibility to find that out ?
> 
> If you are using the GNU linker, or gold, and you make the section
> name a valid C identifier, then the linker will automatically define
> symbols __start_SECNAME and __stop_SECNAME which you can use.

Ah, that's a much better idea.  Forget my suggestion.

Andrew.

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

* Re: Figuring out start and end of sections
  2010-04-14  6:30 ` Felipe Balbi
  2010-04-14  6:47   ` Felipe Balbi
@ 2010-04-14  6:50   ` Fabian Cenedese
  1 sibling, 0 replies; 10+ messages in thread
From: Fabian Cenedese @ 2010-04-14  6:50 UTC (permalink / raw)
  To: gcc-help


>I commented the for loop just to get the thing compiling and nm doesn't
>show any __start_ symbols:
>
>$ nm tst | grep init
>080494cc d __init_array_end
>080494cc d __init_array_start
>08048400 T __libc_csu_init
>080495d4 d __my_initcall_my_init
>08048298 T _init
>080483c4 t my_init

Here's what we do for structures:

In the linker script:
        PROVIDE (__HWDEVICE_DESC_BEGIN = .);
        .struct_hw_dev_desc : {*(.struct_hw_dev_desc)} > ram
        PROVIDE (__HWDEVICE_DESC_END = .);

In the code:
#define STRUCT_HW_DEV_DESC __attribute__ ((section (".struct_hw_dev_desc")))

STRUCT_HW_DEV_DESC SINOSHwDeviceDesc { definition }

extern char* __HWDEVICE_DESC_BEGIN[];
extern char* __HWDEVICE_DESC_END[];

        SINOSHwDeviceDesc* desc = (SINOSHwDeviceDesc*) __HWDEVICE_DESC_BEGIN;
        desc++;

And then you can use these values for whatever you want
until you reach __HWDEVICE_DESC_END.

bye  Fabi

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

* Re: Figuring out start and end of sections
  2010-04-14  6:30 ` Felipe Balbi
@ 2010-04-14  6:47   ` Felipe Balbi
  2010-04-14  6:50   ` Fabian Cenedese
  1 sibling, 0 replies; 10+ messages in thread
From: Felipe Balbi @ 2010-04-14  6:47 UTC (permalink / raw)
  To: Felipe Balbi; +Cc: gcc-help, iant

Hi,

On Wed, Apr 14, 2010 at 09:30:43AM +0300, Felipe Balbi wrote:
> I commented the for loop just to get the thing compiling and nm doesn't
> show any __start_ symbols:

now I got it working:

#include <stdio.h>

typedef int (*initcall_t)(void);

extern initcall_t __start__initcall[];
extern initcall_t __stop__initcall[];

static int my_init(void)
{
	printf("hello world from _init\n");
}
static initcall_t __my_initcall_my_init
	__attribute__((__used__))
	__attribute__((section("_initcall"))) = my_init;

static void call_initcall(initcall_t fn)
{
	fn();
}

int main(int argc, char *argv[])
{
	initcall_t	*fn;

	for (fn = __start__initcall; fn
			< __stop__initcall; fn++)
		call_initcall(*fn);

	return
		0;
}

thanks a lot Ian and Andrew.

-- 
balbi

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

* Re: Figuring out start and end of sections
  2010-04-14  6:21 Felipe Balbi
@ 2010-04-14  6:30 ` Felipe Balbi
  2010-04-14  6:47   ` Felipe Balbi
  2010-04-14  6:50   ` Fabian Cenedese
  0 siblings, 2 replies; 10+ messages in thread
From: Felipe Balbi @ 2010-04-14  6:30 UTC (permalink / raw)
  To: Felipe Balbi; +Cc: gcc-help, iant

Hi,

On Wed, Apr 14, 2010 at 09:22:35AM +0300, Felipe Balbi wrote:
> #include <stdio.h>
> 
> typedef int (*initcall_t)(void);
> 
> static int my_init(void)
> {
> 	printf("hello world from _init\n");
> }
> static initcall_t __my_initcall_my_init
> 	__attribute__((__used__))
> 	__attribute__((section("_initcall"))) = my_init;
> 
> int main(int argc, char *argv[])
> {
> 	initcall_t	*fn;
> 
> 	for (fn = __start__initcall; fn <
> 			__stop__initcall; fn++)
> 		(void) fn();
> 
> 	return 0;
> }

I commented the for loop just to get the thing compiling and nm doesn't
show any __start_ symbols:

$ nm tst | grep init
080494cc d __init_array_end
080494cc d __init_array_start
08048400 T __libc_csu_init
080495d4 d __my_initcall_my_init
08048298 T _init
080483c4 t my_init

-- 
balbi

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

* Re: Figuring out start and end of sections
@ 2010-04-14  6:21 Felipe Balbi
  2010-04-14  6:30 ` Felipe Balbi
  0 siblings, 1 reply; 10+ messages in thread
From: Felipe Balbi @ 2010-04-14  6:21 UTC (permalink / raw)
  To: gcc-help; +Cc: iant

[-- Attachment #1: Type: text/plain, Size: 65 bytes --]

Added back the list. Sorry for that, I had forgotten.

-- 
balbi

[-- Attachment #2: Type: message/rfc822, Size: 1825 bytes --]

From: Felipe Balbi <me@felipebalbi.com>
To: Ian Lance Taylor <iant@google.com>
Subject: Re: Figuring out start and end of sections
Date: Wed, 14 Apr 2010 09:17:33 +0300
Message-ID: <20100414061733.GA4285@gandalf>

Hi Ian,

I guess my mailer is fishy, didn't get your mail either

Ian Taylor <iant@google.com> writes:
>> is there any way to figure out where a section starts and ends ?
>>
>> I added a specific section to my program using
>> __attribute__((section "<section name>")) and now I want to figure out
>> where that section starts so I can iterate over it and call the
>> function
>> pointers I'm adding to it.
>>
>> Do I need a specific linker script to achieve that or does the default
>> scripts give me possibility to find that out ?
> 
> If you are using the GNU linker, or gold, and you make the section
> name a valid C identifier, then the linker will automatically define
> symbols __start_SECNAME and __stop_SECNAME which you can use.

so you're saying that something if I name my section _init then I should
have __start__init and __stop__init identifiers ??

The following isn't compiling:

#include <stdio.h>

typedef int (*initcall_t)(void);

static int my_init(void)
{
	printf("hello world from _init\n");
}
static initcall_t __my_initcall_my_init
	__attribute__((__used__))
	__attribute__((section("_initcall"))) = my_init;

int main(int argc, char *argv[])
{
	initcall_t	*fn;

	for (fn = __start__initcall; fn <
			__stop__initcall; fn++)
		(void) fn();

	return 0;
}

do I need to pass any particular option to gcc/ld to get that working ??

-- 
balbi

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

end of thread, other threads:[~2010-04-14  8:49 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-13 17:40 Figuring out start and end of sections Felipe Balbi
2010-04-13 17:44 ` Andrew Haley
2010-04-14  5:08   ` Figurig " Felipe Balbi
2010-04-14  8:49     ` Andrew Haley
2010-04-14  0:24 ` Figuring " Ian Lance Taylor
2010-04-14  8:49   ` Andrew Haley
2010-04-14  6:21 Felipe Balbi
2010-04-14  6:30 ` Felipe Balbi
2010-04-14  6:47   ` Felipe Balbi
2010-04-14  6:50   ` Fabian Cenedese

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).