Page 1 of 1

how to make gcc work with segmentation

Posted: Sun May 16, 2010 9:31 am
by sancho1980
hi

i have the following problem
my toy os uses segmentation in the sense that the base address of a segment not necessarily starts at 0
rather, the executable code of each process starts at offset 0 within the bounds of the code segment of that process, get my point?
so, i have done quite a bit in assembly so far, but im tired of it because its just to error prone and slow, so i want to switch over to C
and now im having a big big problem:

whenever my c code calls something like this:

someFunction(&someVariable); //some variable being a local variable

then the compiler does something like this:

lea someVariable, eax
push eax
call someFunction

the problem with this is that someFunction gets as a parameter the linear address of someVariable, whereas what it needs to make sense of the address is the offset relative to its own data segment...

if someFunction is written in assembly then i can of course subtract the base address of the data segment from the address provided and then use it as an offset but what if someFunction is written in C?

do i have a chance at all here??
thanks

martin

Re: how to make gcc work with segmentation

Posted: Sun May 16, 2010 9:48 am
by Combuster
As you figured out, gcc assumes that DS=ES=SS. If you can fit that in your design (i.e. no separate stack and data segments) then there is no problem with wrong addresses.

Re: how to make gcc work with segmentation

Posted: Sun May 16, 2010 10:05 am
by sancho1980
Combuster wrote:If you can fit that in your design (i.e. no separate stack and data segments) then there is no problem with wrong addresses.
Well, that's what I first thought. But as it turn s out, it's a bit more difficult:

lets assume the base addresses of cs, ds, es, fs, gs, and ss are all the same, but NOT 0, (let's assume base address of 10):

Code: Select all


void func1()
{
  int x; //let's assume 'x' to be at offset 5 from stack segment base
  func2(&x); //because gcc emits an 'lea' instruction to figure the address of 'x', func2 will be passed a value of 15 (effectively, the linear address of x)
}

void func2(int *x)
{
  *x = 42; //will be dereferenced by using x's value as an offset from data segment base address (i.e. this will try to store 42 at physical address 25!)
}

see the problem??

Re: how to make gcc work with segmentation

Posted: Sun May 16, 2010 10:24 am
by Owen
Address 0 in your segment must correspond to address 0 as far as GCC and the linker are concerned. Other than that, as long as all segments point at the same memory, GCC shouldn't notice.

Re: how to make gcc work with segmentation

Posted: Sun May 16, 2010 11:34 am
by Combuster
sancho1980 wrote: int x; //let's assume 'x' to be at offset 5 from stack segment base
func2(&x); //because gcc emits an 'lea' instruction to figure the address of 'x', func2 will be passed a value of 15
That's nonsense. lea eax, [5] will set eax to 5 independent of whatever's contained in the hidden part of DS/ES/SS.

Re: how to make gcc work with segmentation

Posted: Sun May 16, 2010 11:45 am
by sancho1980
yeah, my problem was something else, sorry..