Issue
I am using Mingw gcc and ld to generate an executable for windows. I would like to access the start address of a section in c code, but I have tried a lot of methods and no one works.
My linker script file has this:
.data BLOCK(__section_alignment__) :
{
__data_start__ = . ;
*(.data)
*(.data2)
*(SORT(.data$*))
KEEP(*(.jcr))
__data_end__ = . ;
*(.data_cygwin_nocopy)
}
In C code I do this:
extern char __data_start__;
uint32_t test = &__data_start__;
And get this error:
undefined reference to __data_start__
Could anyone help me with this? Thanks
Solution
mingw32 is using leading underscores, i.e. the compiler will add a _
(underscore) to each symbol. The part of the linker description file you are showing is part of the default linker script (at least in my installation), and for your code I am getting the mentioned error for
int main()
{
extern char __data_start__;
return (int) &__data_start__;
}
with -save-temps
, the assembly code has
movl $___data_start__, %eax
which has 3 leading _
's not just 2.
Thus, there are 2 solutions: You can advise the compiler to emit __data_start__
as assembly name, which is a GCC extension:
int main()
{
extern char some_identifier __asm("__data_start__");
return (int) &some_identifier;
}
Or you can use _data_start
:
int main()
{
extern char _data_start__;
return (int) &_data_start__;
}
Both use __data_start__
in assembly / object file:
movl $__data_start__, %eax
i686-w64-mingw32-nm module.o
U __data_start__
Answered By - emacs drives me nuts