OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 23, 2024 8:19 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Linking Question
PostPosted: Sat Jun 26, 2004 7:53 am 
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


Top
  
 
 Post subject: Re:Linking Question
PostPosted: Sat Jun 26, 2004 1:26 pm 
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


Top
  
 
 Post subject: Re:Linking Question
PostPosted: Sat Jun 26, 2004 6:13 pm 
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 )"

Quote:
in GCC
------
void draw ( void ) asm ( "draw" );

in NASM
--------
draw:
...
ret


the second way is: extern "C" a function

Quote:
in GCC
------
extern "C" void draw ( void );

in NASM
--------
_draw: ; you must put an "underscore"
...
ret


any C ( extern "C" ) function will be put to asm
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.

Quote:
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



Top
  
 
 Post subject: Re:Linking Question
PostPosted: Sat Jun 26, 2004 6:54 pm 
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


Top
  
 
 Post subject: Re:Linking Question
PostPosted: Sun Jun 27, 2004 9:15 am 
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.


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], SemrushBot [Bot] and 104 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group