Hi All,
I am currently working on loading and linking library functions into my OS.
My question is to do with nasm elf output, is there anyway to prototype a function so that the symbol name will look like
ie _Z4Drawii (which is the output from gcc)
instead of just
Draw (Nasm Output)
or do i just have to name the functions that way?
pkd
Linking Question
Re:Linking Question
looks like you compiled with a c++ compiler which does name mangling (is your file having a .cpp or .C extention?) if you want to use plain C use .c.
also if you want to use C++ but have C style symbols use:
extern "C"
proxy
also if you want to use C++ but have C style symbols use:
extern "C"
proxy
Re:Linking Question
first thing first: there is NO standart for naming
functions in CPP ( GCC uses its own standart )
"_Z4Drawii" means a string with length 4 "_Z4"
"Draw" and that this function takes two integer "i"
parameters.
double DoSth ( int , char , unsigned int );
would be named something like "_dZ4DoSthicu" in GCC
you can decode it by yourself, now lets return to
our main subject
-----------------------------------------------------
there are two ways to get rid of CPP naming problem.
first is ( only in GCC ), prototype the function like the
following. The string in asm () will be the name of the
function in the assembly output of the compiler.
so if you write your draw function in NASM
the linker ld will link this to "void draw ( void )"
by putting an underscore at the beginning of the
method name.
REMEMBER:
you CANNOT use ANY CPP functionality if you
extern "C" a function. overloading, using in a class
as a member function is impossible.
but if you use asm() you can put it anywhere.
but remember also, class member functions take
a pointer to "this" as its hidden first parameter.
functions in CPP ( GCC uses its own standart )
"_Z4Drawii" means a string with length 4 "_Z4"
"Draw" and that this function takes two integer "i"
parameters.
double DoSth ( int , char , unsigned int );
would be named something like "_dZ4DoSthicu" in GCC
you can decode it by yourself, now lets return to
our main subject
-----------------------------------------------------
there are two ways to get rid of CPP naming problem.
first is ( only in GCC ), prototype the function like the
following. The string in asm () will be the name of the
function in the assembly output of the compiler.
so if you write your draw function in NASM
the linker ld will link this to "void draw ( void )"
the second way is: extern "C" a functionin GCC
------
void draw ( void ) asm ( "draw" );
in NASM
--------
draw:
...
ret
any C ( extern "C" ) function will be put to asmin GCC
------
extern "C" void draw ( void );
in NASM
--------
_draw: ; you must put an "underscore"
...
ret
by putting an underscore at the beginning of the
method name.
REMEMBER:
you CANNOT use ANY CPP functionality if you
extern "C" a function. overloading, using in a class
as a member function is impossible.
but if you use asm() you can put it anywhere.
but remember also, class member functions take
a pointer to "this" as its hidden first parameter.
in GCC
------
class SomeClass {
int ss1;
int ss2;
...
void doSth ( int par1 , int par2 ) asm ( "doSth" );
...
};
in NASM
--------
; doSth ( SomeClass* this , int par1 , int par2 )
doSth:
push ebp
mov ebp , esp
; [ ebp ] = pushed ebp
; [ ebp + 4 ] = return ip
; i hope i dont need to explain why they are
; multiple of 4... because sizeof(int) = 4
mov edi , [ ebp + 8 ] ; edi = this;
mov ecx , [ ebp + 12 ] ; ecx = par1
mov edx , [ ebp + 16 ] ; edx = par2
mov eax , [ edi ] ; get first class member
; property "ss1" to eax
mov ebx , [ edi + 4 ] ; get second class member
; property "ss2" to ebx
...
pop ebp
ret
Re:Linking Question
just to correct the previous post, there IS a standard for name mangling among other things known as an ABI (application binary interface) both intel, gcc 3.x, and some others agree in the catagory (though the early 3.x compilers were close but not 100% compliant).
So while there is no global standard yet, there will be soon enough as it is already in the works.
also please note that if you use the asm("name") trick with gcc, that it is up to the coder to ensure no overlap with existing names...if possible use the extern "C" method to make things accessible within C/ASM as it is part of the C++ standard and is thus handled much more cleanly.
proxy
So while there is no global standard yet, there will be soon enough as it is already in the works.
also please note that if you use the asm("name") trick with gcc, that it is up to the coder to ensure no overlap with existing names...if possible use the extern "C" method to make things accessible within C/ASM as it is part of the C++ standard and is thus handled much more cleanly.
proxy
Re:Linking Question
Hi,
Ive read your answers and some of the documentation for ABI,
and have decided to stick with the naming convention,
It makes sense to store parameter info in the function name while in asmcode, and has made linking much easier,
I just run my standard linking code on the library, and it does not matter if the source is c, c++, asm (and possibly other languages)
as long as it is an elf object (linking at runtime)
Thanks for all your help
pkd.
Ive read your answers and some of the documentation for ABI,
and have decided to stick with the naming convention,
It makes sense to store parameter info in the function name while in asmcode, and has made linking much easier,
I just run my standard linking code on the library, and it does not matter if the source is c, c++, asm (and possibly other languages)
as long as it is an elf object (linking at runtime)
Thanks for all your help
pkd.