Page 1 of 1

extern char[] vs extern void*: using linker script values

Posted: Sun Dec 14, 2014 9:36 am
by Espanish
This [Wiki article] explains why we should use extern char symbolname[]; to access linker scripts labels.

My question is: is there anything wrong with using extern void *symbolname; instead?

Re: extern char[] vs extern void*: using linker script value

Posted: Sun Dec 14, 2014 10:01 am
by max
Espanish wrote:This [Wiki article] explains why we should use extern char symbolname[]; to access linker scripts labels.

My question is: is there anything wrong with using extern void *symbolname; instead?
I don't really get what the wiki article is trying to tell, but I have a "typedef void* linkersymbol" that I use for all linker symbols and that works fine. The only thing to note is that this symbol actually *is* the symbol that you wrote in the linker script. Like when you do "mysymbol = .;" in the linker script right before the text secrion, then using "&mysymbol" will give you the address where the text section starts.

Re: extern char[] vs extern void*: using linker script value

Posted: Sun Dec 14, 2014 10:18 am
by jnc100
extern void *symbolname informs the compiler that there is somewhere a pointer (i.e. a 4 or 8 byte memory address, depending on platform), which is stored at the address symbolname. The actual value of the pointer is whatever is at the address symbolname. Therefore to get the address of the label symbolname as an integer, you'd have to do something like (uintptr_t)&symbolname. extern char[] symbolname informs the compiler that there is somewhere an array of characters stored at address symbolname. In C, referencing the name of array is the same as the address of its first value, so to get the address of the label you'd do (uintptr_t)&symbolname[0], or more simply (uintptr_t)symbolname;

In essence it doesn't matter how you inform your compiler about these variables, as long as you know how to determine their addresses. The array method helps in that you don't have to worry about taking the address of the variable, rather just its name (thus potentially reducing bugs in your code).

Regards,
John.

Re: extern char[] vs extern void*: using linker script value

Posted: Mon Dec 15, 2014 9:05 am
by Kevin
I still prefer this one:

Code: Select all

extern const void symbolname;
It is an incomplete type, so you can only have pointers to it and not accidentally dereference it. And logically it's the most accurrate one, too: There simply is no value of interest at the given address.

Re: extern char[] vs extern void*: using linker script value

Posted: Mon Dec 22, 2014 1:34 pm
by xenos
Kevin wrote:

Code: Select all

extern const void symbolname;
I somehow like this approach, but unfortunately it seems to be specific to C and not allowed in C++...

Re: extern char[] vs extern void*: using linker script value

Posted: Tue Dec 23, 2014 4:01 am
by Combuster
Getting your incomplete type isn't that hard in C++ :wink:

Code: Select all

class Void;
extern Void symbolname;

Re: extern char[] vs extern void*: using linker script value

Posted: Tue Dec 23, 2014 6:26 am
by xenos
Makes sense ;) Well, actually it's about getting an incomplete object type such as the Void in your example - void in an incomplete type, but not an object type. I guess I'll go with your solution.

Re: extern char[] vs extern void*: using linker script value

Posted: Tue Dec 23, 2014 3:12 pm
by Kevin
XenOS wrote:I somehow like this approach, but unfortunately it seems to be specific to C and not allowed in C++...
I need to remember this one the next time a C vs. C++ discussion comes up. ;)