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?
extern char[] vs extern void*: using linker script values
- max
- Member
- Posts: 616
- Joined: Mon Mar 05, 2012 11:23 am
- Libera.chat IRC: maxdev
- Location: Germany
- Contact:
Re: extern char[] vs extern void*: using linker script value
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.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?
Re: extern char[] vs extern void*: using linker script value
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.
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
I still prefer this one:
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.
Code: Select all
extern const void symbolname;
- xenos
- Member
- Posts: 1121
- Joined: Thu Aug 11, 2005 11:00 pm
- Libera.chat IRC: xenos1984
- Location: Tartu, Estonia
- Contact:
Re: extern char[] vs extern void*: using linker script value
I somehow like this approach, but unfortunately it seems to be specific to C and not allowed in C++...Kevin wrote:Code: Select all
extern const void symbolname;
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: extern char[] vs extern void*: using linker script value
Getting your incomplete type isn't that hard in C++
Code: Select all
class Void;
extern Void symbolname;
- xenos
- Member
- Posts: 1121
- Joined: Thu Aug 11, 2005 11:00 pm
- Libera.chat IRC: xenos1984
- Location: Tartu, Estonia
- Contact:
Re: extern char[] vs extern void*: using linker script value
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
I need to remember this one the next time a C vs. C++ discussion comes up.XenOS wrote:I somehow like this approach, but unfortunately it seems to be specific to C and not allowed in C++...