OpenCores
no use no use 1/1 no use no use
why or32-elf-gcc adds a _ in var names
by ramkumarj2000 on Mar 3, 2010
ramkumarj2000
Posts: 18
Joined: Aug 11, 2008
Last seen: Jan 27, 2012
Hi All,

Lets say I have 2 trivial programs as below,

[ramkumarj@localhost comps]$ cat dummy.c

int main()
{
process("Hello World");
return 0;
}

[ramkumarj@localhost comps]$ cat process.c

extern unsigned char *_bss_start;

void process(char *x)
{
unsigned char *tmp = _bss_start;

*tmp=*x;

}

I compiled using powerpc cross compiler as well as or32-elf- compilers and dumped the symbol tables. I find that a _ is appended in front of all variables in or32-elf but for powerpc the same variable name is used as symbol name.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PowerPC Compiler
---------------------

[ramkumarj@localhost comps]$
[ramkumarj@localhost comps]$ powerpc-405-linux-gnu-gcc -c dummy.c -o dummy.o
[ramkumarj@localhost comps]$ powerpc-405-linux-gnu-gcc -c process.c -o process.o
[ramkumarj@localhost comps]$
[ramkumarj@localhost comps]$ powerpc-405-linux-gnu-nm dummy.o
00000000 T main
U process
[ramkumarj@localhost comps]$ powerpc-405-linux-gnu-nm process.o
U _bss_start
00000000 T process

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
OpenRISC Compiler
---------------------

[ramkumarj@localhost comps]$ or32-elf-gcc -c dummy.c -o dummy.o
[ramkumarj@localhost comps]$ or32-elf-gcc -c process.c -o process.o
[ramkumarj@localhost comps]$ or32-elf-nm dummy.o
00000000 T _main
U _process
[ramkumarj@localhost comps]$ or32-elf-nm process.o
U __bss_start
00000000 T _process


1. What is the reason for appending the _ symbol to the start of each variable name ?
2. Is there anyway to turn this behavior off ?
3. Sometimes I feel this appending of _ to all variable name introduces a need to define symbols in the linker script in a way different as other architectures. For Eg, _bss_start becomes __bss_start. Is this fine. Doesnt that affect the code portability. Just wondering...

Thanks and Regards,
Ram
RE: why or32-elf-gcc adds a _ in var names
by jeremybennett on Mar 3, 2010
jeremybennett
Posts: 689
Joined: May 29, 2008
Last seen: Feb 9, 2012

Hi Ram,

It's a historical legacy. Early systems restricted the number of characters for symbols in link tables, leading to many clashes. Prepending '_' became a convention to indicate symbols with external linkage.

In time that became part of the standards for C and C++, so that leading underscore is now reserved for the implementation. By generating symbols with a leading '_' you can guarantee the symbol will not appear in any user program to be linked against.

However with modern linkage conventions, there is no restriction on the number of characters and the historical requirement is no longer valid. Most C/C++ compilers still use the convention, but some (such as PowerPC) have dropped it.

You can try using the -fleading-underscore and -fno-leading-underscore flags to control this in compiled code. I'm not sure to what extend they are implemented for the OpenRISC GCC. Note that if you do this, you will have code that will not link with standard compiled code.

In practice it is not a problem, since tools, such as GDB, that work with compiled C code understand the convention and know how to convert between names.

HTH

Jeremy

--
Tel: +44 (1590) 610184
Cell: +44 (7970) 676050
SkypeID: jeremybennett
Email: jeremy.bennett@embecosm.com
Web: www.embecosm.com

RE: why or32-elf-gcc adds a _ in var names
by ramkumarj2000 on Mar 4, 2010
ramkumarj2000
Posts: 18
Joined: Aug 11, 2008
Last seen: Jan 27, 2012
Hi Jeremy,

That was one good explanation, exactly what I needed. I am curious of one thing. I was of the assumption that the gcc compilers even acros the different architectures has most of their behavior same except the generation of the specific assembly code for that architecture. Am I right to say this.

Thanks and Regards,
Ram
no use no use 1/1 no use no use
© copyright 1999-2012 OpenCores.org, equivalent to ORSoC AB, all rights reserved. OpenCores®, registered trademark.