Page 1 of 1

wrong address

Posted: Sat Jan 29, 2005 1:01 pm
by Mr. P
Hi!
I've got this code:

Code: Select all

int gtest;

int main() {
   int test;
   printf("%x, %x", &gtest, &test);
      
   return 0;
}
Which displays 442* and FFFFFFFC, the first address is correct but the second isn't, so why do variables in functions get incorrect addresses?

This is my linker script:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(main)
OUTPUT("test")

SECTIONS  {
  .text 0x44200000: {
    *(.text)
  }
  .data  : {
    *(.data)
  }
  .rodata : {
    *(.rodata)
  }
  .bss  :  {                                    
    *(.bss)
  }
}
And this is how I compile:

Code: Select all

CFLAGS = -Wall -nostdlib -nostartfiles -fno-builtin -fwritable-strings -Isrc/include
OBJS = src/main.o

all: $(OBJS)

%.o:%.c
   $(CC) $(CFLAGS) -o $@ -c $<

all:
   $(LD) $(OBJS) -T link.ld
I'm posting in this forum because I'm loading it into my OS as a module.

Re:wrong address

Posted: Sat Jan 29, 2005 1:07 pm
by FlashBurn
Because local variables are on the stack!

Re:wrong address

Posted: Sat Jan 29, 2005 2:19 pm
by Mr. P
well, my esp isn't pointing at 0xFFFFFFFF...

Re:wrong address

Posted: Sat Jan 29, 2005 3:11 pm
by Candy
Mr. P wrote: well, my esp isn't pointing at 0xFFFFFFFF...
What if your ESP was 0 or 4 before getting there? That would cause this...

if you didn't link it through btw, this is also a known offset for jumps in unlinked files.

Re:wrong address

Posted: Sat Jan 29, 2005 3:32 pm
by Mr. P
If the stack would be incorrect, my context switcher would warn me.
I checked the ESP at the position of the variable, and it was 0x44200FFC (correct).

Re:wrong address

Posted: Sat Jan 29, 2005 9:18 pm
by Mr. P
Candy wrote:
Mr. P wrote: well, my esp isn't pointing at 0xFFFFFFFF...
What if your ESP was 0 or 4 before getting there? That would cause this...

if you didn't link it through btw, this is also a known offset for jumps in unlinked files.
Candy, you lead me to the answer. EBP was 0.