Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing linker defined symbol in C
    primarykey
    data
    text
    <p>I have a linker symbol (let's call it __BASE_ADDR), defined in a linker command script, which contains the address of the longword <em>after</em> a longword that I need to use (e.g. I need to check longword at address 0x0000000C and __BASE_ADDR is equal to 0x00000010). So in the code I try to use __BASE_ADDR and subtract 4 from it to access 0x0000000C.</p> <p>My linker map output correctly shows the value of __BASE_ADDR to be 0x00000010. However when I try to access the location using the following C code it doesn't work:</p> <pre><code>extern unsigned char *__BASE_ADDR; void someFunc( void ) { unsigned long Val = *((unsigned long *) (__BASE_ADDR - 4)); /* Use Val for some comparison...*/ /* ... */ } </code></pre> <p>Val gets set to the wrong value. What appears to be happening is that whatever longword is at __BASE_ADDR is taken as an address, 4 is subtracted, and the contents of the resulting address is put into Val.</p> <p>But when I do the following, it does work fine and Val is set correctly to whatever resides at address 0x0000000C:</p> <pre><code>extern unsigned char __BASE_ADDR[]; void someFunc( void ) { unsigned long Val = *((unsigned long *) (__BASE_ADDR - 4)); /* Use Val for some comparison...*/ /* ... */ } </code></pre> <p>Can anyone shed light on this? I understand why you can't access a linker symbol's contents (it technically has no contents), but why would there be a difference between accessing the linker symbol as a pointer and accessing it as an array?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload