Baremetal riscv gcc compiled code and linked. I can load my program at any address. firmware calls my program using pointer to my function. I am not able to print the global variables address, its printing offsets instead of absolute address. what to do? running on qemu. If I am giving fixed address to ram in memory map. I am getting correct address but without giving memory map I am getting offset as its actual absolute address. But thats not I wanted.
this is the below code
void fun() {
__attribute__((used)) static const int lsc = 0x55; //.const
printf("%d %x %x\n",lsc,lsc,&lsc);
}
when printing the address of local_static_const, I am getting some offset 0xd0, but not absolute address.
I randomly found out that -Wl,-mno-relax giving me correct output but if I am removing then again its giving me wrong output .i.e giving offset address. I am not able to understand why.
Note: %p is not the issue, I have changed it to %x, its working in the same way listed above.
It seems the problem is that you are using an incorrect conversion specifier to output a pointer. The conversion specification
%xis designed to output objects of the typeunsigned intthat usually occupy4bytes while pointers in 64-bit systems occupy8bytes. To convert a pointer to an unsigned integer type you need to use type specificationuintptr_tdefined in header<stdint.h>. As a result the functionprintfis just even unable to access all8bytes using the convefrsion specification%x.Instead try the following using conversion specifier
pspecially designed to output pointersAn alternative approach is to include header
<inttypes.h>and write