Page 1 of 1

Ada and name mangling

Posted: Mon Jan 31, 2011 9:37 am
by kriscrump
Hello OSDev.org Community!

This is my first post on the forums, but I have been reading these threads for about two years now, give or take a bit. I find this place to be one of my most valuable resources to developing my own OS.
I've been programming for about ten years now, give or take a few again(I know, so vague!). I've experimented with a fairly large number of programming languages, and in the recent years have started researching OS development, and started keeping lots of notes.

I've followed all sorts of tutorials and even just played around with things myself, applying what I know and finding out the results. Learn by doing! That being said, I usually never bother asking anyone else for help, since usually the answer can be found in the error messages, the internet, books, or various other resources.

For the little OS fun I am working on right now, I am using GRUB-legacy and GRUB2 as a bootloader, a few lines of assembly code to get me into my kernel entry function/method/procedure/routine. I use this as a base/template to test out writing kernel code in different languages.

Sorry for all of the background info, here is the real reason I am posting:

I am trying to load an extremely short kernel written in Ada. It is basically an empty for loop.
I used:

Code: Select all

gcc -c main.adb
to only compile the file.
Since, I like to also snoop around, I automatically checked the resulting object file with:

Code: Select all

objdump -d main.o
to view the assembly code, and:

Code: Select all

objdump -t main.o
to view the symbols.

I noticed immediately that the symbol that would indicate my kernels start was prefixed with '_ada_'. Which now finally brings me to my question. Is there an option in gcc or gnat to specifiy not using the prefix or keep it from name mangling?

I've read the various documentations for gcc / gnat / gnatmake / gnatbind and such, and I saw no such thing. Also, if there is no way of changing how the compiler outputs the symbol, would it be plausible to write a script to patch the symbol?

Thanks for any ideas or thoughts you might have on the matter!

-Kris

p.s. I've tried my hardest to find info on this and have had no luck. I've also never had to deal much with making sure things like this 'add up'.

Re: Ada and name mangling

Posted: Mon Jan 31, 2011 10:21 am
by Solar
I know next to nothing about ADA or GNAT, but I found pragma Export_Function and pragma Export_Procedure. It looks as if they allow exporting an ADA function / procedure in a way that would allow C code to call them. (Implying no name mangling.)

Re: Ada and name mangling

Posted: Mon Jan 31, 2011 10:37 am
by kriscrump
Thanks a bunch! I was looking at those myself, but wasn't sure if those are the proper use. I'll edit my source code and post back on the findings!

Thanks again,
~Kris

edit:
@Solar: Thank you! It turns out using those is the proper convention of exporting the proper symbols to keep ada procedure names from mangling. There is a downside to this though. I looked at all of the documentations of Export, as well as the pragma Linker things, and in order to export my main symbol correctly it has to be done in a very round-about way using a 'header file' to predefine the function, and then follow it with an export.

@everyone: I'm unsure if this interests anyone, but I will see about implementing this properly and will compare/post the print-outs of:

Code: Select all

objdump -d
from a C, as well as Ada version of the code. I'm sure the results will be interesting, considering as the two kernels are now, implementing the same things.

The C version:

Code: Select all

objdump -d main.o

Code: Select all

Disassembly of section .text:

00000000 <main>:
   0:	55                   	push   %ebp
   1:	89 e5                	mov    %esp,%ebp
   3:	eb fe                	jmp    3 <main+0x3>
The Ada version:

Code: Select all

objdump -d main.o

Code: Select all

Disassembly of section .text:

00000000 <_ada_main>:
   0:	55                   	push   %ebp
   1:	89 e5                	mov    %esp,%ebp
   3:	83 ec 08             	sub    $0x8,%esp
   6:	eb fe                	jmp    6 <_ada_main+0x6>
Just curious but why is this in the code?

Code: Select all

sub    $0x8,%esp
<_< Sorry I write walls of text.
~Kris