Mr_M_from_G
Mr_M_from_G

Reputation: 49

GCC produces unaligned function address on Cortex M3

I just placed a function to a specific address using a section and then I output the address of that function and the result is the chosen section address + 1. This is what I did:

void __attribute__((section (".my_fct_address"))) Fct_Ptr_Test (void)
{
...
}

and

void (*fct_ptr) (void);
fct_ptr = Fct_Ptr_Test;
printf ("0X%X\r\n", (uint32_t)(fct_ptr));
fct_ptr ();

in ld-file:

.my_fct_address 0x800F000 :
 {
   KEEP(*(.my_fct_address)) /* keep my variable even if not referenced */
 } > FLASH

The above printf statement outputs 0x800F001 and Fct_Ptr_Test is called properly

If I set

fct_ptr = 0x800F000;

the system crashes. If I set

fct_ptr = 0x800F001;

everything is fine again. If I don't place Fct_Ptr_Test in its own section , ie let the linker place it anywhere I also get an odd address. Now I wonder how 0x800F001 can be a proper address on a 32 bit controller (ARM cortex M3) and what is stored in 0x800F000. Even more strange: map-file always shows the even addresses Can anybody help?

Thanks

Martin

Upvotes: 0

Views: 231

Answers (1)

yugr
yugr

Reputation: 21878

Linker sets the least-significant bit of Thumb functions to 1 to facilitate interworking (see docs). Perhaps that's your case?

Upvotes: 3

Related Questions