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
how to make gcc work with segmentation
-
- Member
- Posts: 199
- Joined: Fri Jul 13, 2007 6:37 am
- Location: Stuttgart/Germany
- Contact:
- Combuster
- 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
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.
-
- Member
- Posts: 199
- Joined: Fri Jul 13, 2007 6:37 am
- Location: Stuttgart/Germany
- Contact:
Re: how to make gcc work with segmentation
Well, that's what I first thought. But as it turn s out, it's a bit more difficult: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.
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!)
}
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: how to make gcc work with segmentation
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.
- Combuster
- 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
That's nonsense. lea eax, [5] will set eax to 5 independent of whatever's contained in the hidden part of DS/ES/SS.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
-
- Member
- Posts: 199
- Joined: Fri Jul 13, 2007 6:37 am
- Location: Stuttgart/Germany
- Contact:
Re: how to make gcc work with segmentation
yeah, my problem was something else, sorry..