Compiling dynamically linkable executable for my OS

Programming, for all ages and all languages.
Post Reply
Virtlink
Member
Member
Posts: 34
Joined: Thu Jun 05, 2008 3:53 pm
Location: The Netherlands
Contact:

Compiling dynamically linkable executable for my OS

Post 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!
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Compiling dynamically linkable executable for my OS

Post 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! :P
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Compiling dynamically linkable executable for my OS

Post 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
Post Reply