Page 1 of 1

Position Independant Functions

Posted: Thu Aug 19, 2004 7:03 am
by XStream
Hey Everyone,

Just wondering if someone can help me out with how to write position independant functions, I want to write my os system calls so that they can be relocated.

I want to write them in assembler and using NASM as well.

Cheers.

Re:Position Independant Functions

Posted: Thu Aug 19, 2004 7:15 am
by Candy
use a random register (usually ebx) as base

Side question, why in gods name do you want your OS calls to be relocatable?

Re:Position Independant Functions

Posted: Thu Aug 19, 2004 7:17 am
by Solar
Don't use state, that's the most important. Do not keep any references or values between calls. As you'll be using assembler, don't make absolute references, jumps, or branches.

Then, make sure your object format supports relocatable code...

Re:Position Independant Functions

Posted: Thu Aug 19, 2004 7:23 am
by XStream
@Candy: I am just toying with a few ideas, not really anything solid yet, I will share if it turns out ok. Can you give a quick example of using ebx in this way, does this mean that you must set ebx before calling the function?

@Solar: It seems pretty useless to not be able to jump or branch anywhere, surely there must be some way to do it.

Cheers.

Re:Position Independant Functions

Posted: Thu Aug 19, 2004 7:38 am
by Pype.Clicker
note that solar told you about not doing *absolute* jumps. There are *relative* jumps (most of them but far jumps, actually), and same for calls.

As for the memory reference, you can always have register-based references like [ebp+8] for getting arguments, but [myVar] will probably not be position-independent.

Now you should know that *position independent* code and *relocatable* code are certainly not the same beast. A relocatable code *do* have absolute references, but they're listed so that the loader can fix the target address ...

Now why do you want your kernel code to be position-independent is an interresting question. You *know* at which address the code will be loaded ... And if you don't (for instance because you want to boot and return seamlessly to DOS), you can use the segmentation unit to force an unknown physical location to appear as a well-known logical location...

Re:Position Independant Functions

Posted: Thu Aug 19, 2004 7:39 am
by Solar
XStream wrote:
Can you give a quick example of using ebx in this way, does this mean that you must set ebx before calling the function?
The problem is in the i386 instruction set, which does not support PIC-relative addressing, but only register-relative... which means you have to load a register with a base address, and use offsets from that register.
It seems pretty useless to not be able to jump or branch anywhere, surely there must be some way to do it.
Sure you can jump and branch, but only relative, not absolute. You can't jump to address XYZ (since that would obviously position-dependent), you can only jump to [register + offset].

Re:Position Independant Functions

Posted: Thu Aug 19, 2004 8:16 am
by fraserjgordon
If you want to see how PIC can cause a headache, take a look here: http://www.mega-tokyo.com/forum/index.p ... eadid=6566

It was a problem that I had when trying to use it - take my advice and don't touch it with a 50ft pole.

Re:Position Independant Functions

Posted: Thu Aug 19, 2004 1:48 pm
by bkilgore
Fraser Gordon wrote:take my advice and don't touch it with a 50ft pole.
It depends on why you're using it. When my kernel gets loaded by grubs, it gets dumped into my c kernel but without paging enabled yet, and without virt==phys. So until I enable paging (which I of course do almost right away) I have some position independent code that loads some variables with values that I want to access before enabling paging, such as the physical address of the code, etc. So i do a minimal amount of PIC before enabling paging and then back to normal. It's not a big deal to use, as long as you have a reason to use it, learn to use it, and remember that you're using it.