Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

get start and end of .text from C

I am trying to get the start and end of the .text section in C code. I have currently tried:

extern int  stext;
extern int  etext;
struct ta 
{
  void * start;
  void * end;
};
struct ta text_area;
char image_auth_sig;
void image_global_vars_init()
{
  text_area.start = &stext;
  text_area.end = &etext;
}

But, I get the following:

obj/common/image_global_vars.o(.text+0xc): In function `image_global_vars_init':
../common/image_global_vars.c:17: Unable to reach stext (at 0x00200150) from the global pointer (at 0x0025d900) because the offset (-382896) is out of the allowed range, -32678 to 32767.

Can anyone suggest how this might be done?

Thanks,

Cliff

1 Reply

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi Cliff,

    By default, the compiler wants to access data less than 8 bytes with the global

    pointer. An int is less than 8 bytes, so the global pointer is used to calculate the

    address. In your case, .text section can't be reached with the gp.

    The -G0 option can be used to turn off addressing via the gp, but it's not very

    portable. So far, I've found it best to use a void data type so you don't have to

    worry about the gp at all:

    <div class='quotetop'>QUOTE </div>

    --- Quote Start ---

    - extern int stext;

    + extern void stext;

    - extern int etext;

    + extern void etext;[/b]

    --- Quote End ---

    Basically, void doesn&#39;t have a size, so the compiler _should_ never use the gp.

    You can also define the symbols as functions & cast on assignment, but void

    is probably more readible ;-)

    Regards,

    --Scott