Page 1 of 1
Compiling dynamically linkable executable for my OS
Posted: Mon Oct 20, 2008 6:11 pm
by Virtlink
It's kind of a noob question, but what arguments do I give to gcc and ld to get an executable with a symbol table for dynamic linking? Of course, the executable must be able to use the functions I have defined in my OS and no libc. Do I need to create a .so file from my kernel, somehow, or is having the header files enough?
The reason is that I want to see how I can load and link the executable at run-time. I don't really care what executable format it will be, I just want to see how it looks. But I seem unable to get a simple "Hello world", which uses one of my OS functions, to compile (undefined reference). This is probably because I am not a native C or C++ programmer, having only ever dealed with the functions that I defined myself for my OS, in my own header files.
Any help or pointers in the right direction are greatly appreciated!
Re: Compiling dynamically linkable executable for my OS
Posted: Mon Oct 20, 2008 6:31 pm
by Troy Martin
Here's how you do something like it in NASM:
Well, in a real mode OS that puts everything in a single segment, you would set up vectors at the beginning at, say 0x1000, each one jumping to its respective system call. They would be jmp short instructions, each three bytes long (the jmp is one, and the address is two).
Then, your programs would include an include file that does something like this:
Code: Select all
call_printf equ 0x1003
call_getch equ 0x1006
call_gets equ 0x1009
etc, etc...
Then again, that only works in real mode in a single segment. It's extremely simple, though!
EDIT: The first equ being 0x1003 is because the first three bytes are a jmp short os_main (or whatever the label after the equ's are called), taking up three bytes!
Re: Compiling dynamically linkable executable for my OS
Posted: Tue Oct 21, 2008 1:32 am
by JamesM
Virtlink wrote:It's kind of a noob question, but what arguments do I give to gcc and ld to get an executable with a symbol table for dynamic linking? Of course, the executable must be able to use the functions I have defined in my OS and no libc. Do I need to create a .so file from my kernel, somehow, or is having the header files enough?
The reason is that I want to see how I can load and link the executable at run-time. I don't really care what executable format it will be, I just want to see how it looks. But I seem unable to get a simple "Hello world", which uses one of my OS functions, to compile (undefined reference). This is probably because I am not a native C or C++ programmer, having only ever dealed with the functions that I defined myself for my OS, in my own header files.
Any help or pointers in the right direction are greatly appreciated!
Hi,
The headers are enough. Compile your program with "-Wl,-shared -Bsymbolic", and you should get a dynamically linkable shared object out.
EDIT: For clarification, those are link time flags, assuming you're linking by calling "gcc". If you're linking by calling "ld" directly, remove the -Wl, bit - "-shared -Bsymbolic".
Cheers,
James