wrong address

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
Mr. P

wrong address

Post 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.
FlashBurn

Re:wrong address

Post by FlashBurn »

Because local variables are on the stack!
Mr. P

Re:wrong address

Post by Mr. P »

well, my esp isn't pointing at 0xFFFFFFFF...
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:wrong address

Post 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.
Mr. P

Re:wrong address

Post 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).
Mr. P

Re:wrong address

Post 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.
Post Reply