tags:

views:

341

answers:

1

I'm writing a small program loader for my language because I gave up on understanding ELF format (and while doing this, I may eventually understand it better). I mmap the files on the memory and tux rejoices whatever..

I don't want to hinder the sharing of the program by doing any changes on it. Therefore I end up doing the same as C and elf does: global offset table.

The problem is: how can I pass the GOT for my program?

First thing that comes to mind is giving it along in a register or stack argument. In a register it'd be great, but x86 is retarded by it's register count. This could mean I will lose ebx or ebp or some such. In a sensible architecture this'd be a fair tradeoff. In x86 it feels a bit of fail.

Disassembly of a shared library shows me that gcc is doing it as an IP-relative addressing. If I'd do this, it'd be:

    call 0
here:
    pop eax
    ; do something with [eax + (got - here) + index*4]

Though, partially this feels complicated. I don't like about doing this.

Any more ideas, anyone?

Edit: When getting to handle this with multiple libraries, I realised this: I will have multiple GOTs per app and the use of certain GOT depends on which chunk of code I am in. Therefore keeping GOT in a separate register is going to require some additional tricks I'm not aware of. I'd like to know how they solve this problem when keeping GOT in registers.

+1  A: 

You can use one of the segment registers (or the base thereof) for the base of your binary image. So you would refer to your global data eg. as FS:xxx.

These registers are remnants of the so called segmented memory model. Basically, segments are "windows" into the linear address space with specified base (and limit), and if you use them for addressing, (eg. if address is 0010:00000001) the resulting address is the (base of segment with selector 0010)+00000001. The base (as well as other parameters) of the segment are stored in the descriptor table (there are more of these) which is a special area in memory. These can be only modified in kernel mode, there are syscalls in linux that do this (modify_ldt, arch_prctl). In 64-bit mode, the situation is a little more complicated.

For a reference, see the AMD64 architecture manual, especially Volume 2: System Programming.

jpalecek
I need to read about these segment registers. This far I've ignored them as a legacy.
Cheery
Actually, you can use GS and FS in 64-bit mode. They are the exceptions to the flat addressing mode.
Nathan Fellman