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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Espanish
Posts: 24
Joined: Fri Nov 21, 2014 6:30 am

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

Post 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?
User avatar
max
Member
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

Post 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.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

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

Post 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.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

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

Post 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.
Developer of tyndur - community OS of Lowlevel (German)
User avatar
xenos
Member
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

Post 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++...
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
Combuster
Member
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

Post by Combuster »

Getting your incomplete type isn't that hard in C++ :wink:

Code: Select all

class Void;
extern Void symbolname;
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
xenos
Member
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

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

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

Post 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. ;)
Developer of tyndur - community OS of Lowlevel (German)
Post Reply