Forum Discussion

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

Access linker symbol from .C file

I'm trying to access a linker symbol from my C file. The idea is to find out where the end of my code and R/O memory is in flash so I can use the remainder for storage. I have the following in my .c file:

.C file:

extern int __ram_rodata_end;

void *p = &__ram_rodata_end;

The .map file shows the symbol __ram_rodata_end like this:

0x01014f20 PROVIDE (__ram_rodata_end, <code 336> (.))

But the linker gives me the error below. If I use a symbol that&#39;s in reach (e.g. __sbss_end) everything works and p points to the end of data. The format in the map file is exactly the same so I know that&#39;s not the problem.

Linker error:

../test.c:33: Unable to reach __ram_rodata_end (at 0x01014f20) from the global pointer (at 0x0101ed34) because the offset (-40468) is out of the allowed range, -32678 to 32767.

Here&#39;s the assembly code causing the problem:

145 0100 040080D0 addi r2,gp,%gprel(__ram_rodata_end)

146 0104 150680E0 stw r2,24(fp)

Is there an easy way of getting the value of a linker symbol into a C variable? I&#39;m not above using inline assembly with a global or static variable to get the value.

Thanks,

Andrew

2 Replies

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

    Ok, I&#39;ll answer my own question. To access a linker symbol that&#39;s out of reach use the following code:

    extern int __ram_rodata_end[];

    printf("%x\n", __ram_rodata_end);

    That&#39;ll print out the same value you&#39;ll see in the .map file. The trick is to use an array declaration which forces GCC to use full-range addressing.

    Thanks to David Brooks on comp.arch.embedded for the answer.

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

    The problem you had is related to the fact that you declared the extern variable as "int", that means the compiler will generate GP-relative addressing, that is not the right one in your case.

    Take a look at the post "Disabling GP-addressing in generated source code" on this forum...

    bye

    PJ