how to make gcc work with segmentation

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
sancho1980
Member
Member
Posts: 199
Joined: Fri Jul 13, 2007 6:37 am
Location: Stuttgart/Germany
Contact:

how to make gcc work with segmentation

Post 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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: how to make gcc work with segmentation

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
sancho1980
Member
Member
Posts: 199
Joined: Fri Jul 13, 2007 6:37 am
Location: Stuttgart/Germany
Contact:

Re: how to make gcc work with segmentation

Post 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??
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: how to make gcc work with segmentation

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: how to make gcc work with segmentation

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
sancho1980
Member
Member
Posts: 199
Joined: Fri Jul 13, 2007 6:37 am
Location: Stuttgart/Germany
Contact:

Re: how to make gcc work with segmentation

Post by sancho1980 »

yeah, my problem was something else, sorry..
Post Reply