Calling a static member from GAS assembly

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
Yoshi
Posts: 5
Joined: Tue Nov 30, 2004 12:00 am
Location: Philadelphia

Calling a static member from GAS assembly

Post by Yoshi »

Hi,
I have been writing a small OS using C++. I was wondering if there is any way to call a static member function of a class from within a GAS assembly code linked together with the C++ codes.
I know that I can envoke a normal function outside of a class using extern "c", but I am not sure how one can go about doing that in this case...
Could someone give me a pointer?

Thanks,

-Yoshi
Last edited by Yoshi on Tue Nov 30, 2004 12:00 am, edited 1 time in total.
bregma
Member
Member
Posts: 25
Joined: Tue Oct 26, 2004 11:00 pm
Location: the back woods
Contact:

Re: Calling a static member from GAS assembly

Post by bregma »

Yoshi wrote:I have been writing a small OS using C++. I was wondering if there is any way to call a static member function of a class from within a GAS assembly code linked together with the C++ codes.
I know that I can envoke a normal function outside of a class using extern "c", but I am not sure how one can go about doing that in this case...
Static member functions in C++ are regular functions, just like namespace-level functions. The trick with calling any function with C++ linkage is that the name of the function will have been mangled, which means the name of the function was modified by the compiler to include information on class membership and parameter types, among other information. The mechanism and rules the compiler follows are implementation-dependent, and varies not only from vendor to vendor but from compiler release to compiler release.

If you can gurantee you'll only event be compiling you code with the same version of the same complier from the same vendor, all you need to do is find out the mangled name of your static member function. You can generally do this using the tools that come with your compiler. In the case of the GCC suite, the binutils tool nm will list all the symbols produced by your compiler, and the tool c++filt will convert a mangled name into a human-readable name for cross-checking that you've got the right one.

Once you have that mangled name, you can just call it as you would a regular C function.
Yoshi
Posts: 5
Joined: Tue Nov 30, 2004 12:00 am
Location: Philadelphia

Re: Calling a static member from GAS assembly

Post by Yoshi »

Hi, thanks for your reply.
I was actually thinking of doing that, but I was a bit afraid of breaking the portability of the code. Since this is an OS kernel, it may be ok to restrict the choice of the compiler to GCC. But, I was wondering that the mangling convention of GCC may change in the future releases, and in that case my kernel won't compile correctly.
Well...but, I'm guessing that this is pretty much the only way to call a static member. So, I think I will do this way or implement differently.

Hm...it would have been cool if there's a macro or something in GAS to acheive this thing.

- Yoshi
bregma
Member
Member
Posts: 25
Joined: Tue Oct 26, 2004 11:00 pm
Location: the back woods
Contact:

Re: Calling a static member from GAS assembly

Post by bregma »

Yoshi wrote: I was actually thinking of doing that, but I was a bit afraid of breaking the portability of the code. Since this is an OS kernel, it may be ok to restrict the choice of the compiler to GCC. But, I was wondering that the mangling convention of GCC may change in the future releases, and in that case my kernel won't compile correctly.
Yes, and I would not recommend calling a C++-linkage function directly.

Rather than making your C++ function a static member function, you should make it a namespace-level function with C linkage. A static member function is not object-dependent anyways, so this will solve your portability problems.

If you're making your function a static member function to provide access to private internals of your class, you can just make it a friend of your class. On the other hand, static member functions accessing object members might just indicate the wrong class design to start with. Either way, you may find moving the function out of the class the best all-around solution.
mikeleany
Posts: 3
Joined: Fri Nov 19, 2004 12:00 am
Contact:

Re: Calling a static member from GAS assembly

Post by mikeleany »

Yoshi wrote:Hi, thanks for your reply.
I was actually thinking of doing that, but I was a bit afraid of breaking the portability of the code. Since this is an OS kernel, it may be ok to restrict the choice of the compiler to GCC. But, I was wondering that the mangling convention of GCC may change in the future releases, and in that case my kernel won't compile correctly.
Well...but, I'm guessing that this is pretty much the only way to call a static member. So, I think I will do this way or implement differently.

Hm...it would have been cool if there's a macro or something in GAS to acheive this thing.

- Yoshi
There are actually many other ways to do this. As bregma suggested, you could possibly move the function outside the class, as a friend or otherwise. Another alternative would be to create a function outside the class which would then call your static function. Either of these options would give you maximum portability.

If you don't care if your code compiles on non-gcc compilers (which I assume you don't since you're using GAS) you can use gcc's Asm Labels.
You could declare your static function as follows:

Code: Select all

class myclass {
public:
  static func() asm ("func");

  . . .
}
(The keyword "asm" could be replaced with "__asm__" if you want)
You could then call your function by the name "func" in your assembly code. While there's no gaurantee that Asm Labels won't change between versions of gcc, it's probably better than relying on the mangling method to stay the same.

You can find more about Asm Labels and other of gcc's C and C++ extensions in gcc's info pages under "C extensions" (this is where you'll find Asm Labels) and "C++ extensions".
Yoshi
Posts: 5
Joined: Tue Nov 30, 2004 12:00 am
Location: Philadelphia

Re: Calling a static member from GAS assembly

Post by Yoshi »

xSadar wrote: If you don't care if your code compiles on non-gcc compilers (which I assume you don't since you're using GAS) you can use gcc's Asm Labels.
You could declare your static function as follows:
Oh, I didn't realize that there's such an extention.
I was once defining a global function pointer variable, assigning a pointer to a static member function to it, and then referring to it in a GAS assembly code...
Well....as bregma points out, it might be a bit questionable whether it is really necessary to try calling static members, but it's always good to have options....(maybe)

Thanks=)
Last edited by Yoshi on Thu Dec 09, 2004 12:00 am, edited 1 time in total.
tommi G
Posts: 3
Joined: Tue Dec 07, 2004 12:00 am

Re: Calling a static member from GAS assembly

Post by tommi G »

right you enter a code of im a faggit then it should work as simple as dat ma bredda BrRrRaAaAp BrRrRaaAaAp
tommi G
Posts: 3
Joined: Tue Dec 07, 2004 12:00 am

Re: Calling a static member from GAS assembly

Post by tommi G »

right you enter a code of im a faggit then it should work as simple as dat ma bredda BrRrRaAaAp BrRrRaaAaAp
tommi G
Posts: 3
Joined: Tue Dec 07, 2004 12:00 am

Re: Calling a static member from GAS assembly

Post by tommi G »

right you enter a code of im a faggit then it should work as simple as dat ma bredda BrRrRaAaAp BrRrRaaAaAp
dixonboy
Posts: 1
Joined: Fri Dec 10, 2004 12:00 am

Re: Calling a static member from GAS assembly

Post by dixonboy »

wogwong mi friends
Post Reply