Position Independant Functions

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
XStream

Position Independant Functions

Post 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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Position Independant Functions

Post 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?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Position Independant Functions

Post 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...
Every good solution is obvious once you've found it.
XStream

Re:Position Independant Functions

Post 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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Position Independant Functions

Post 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...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Position Independant Functions

Post 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].
Every good solution is obvious once you've found it.
fraserjgordon

Re:Position Independant Functions

Post 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.
bkilgore

Re:Position Independant Functions

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