Page 1 of 1

"end" symbol from my linker script is zero?

Posted: Thu Jun 16, 2011 3:24 pm
by Caleb1994
Hey, I was having an odd error when trying to clone a page directory. It was saying that my frame allocator was returning zero. This made no sense, because I explicitly tell the allocator to reserve the first frame as it holds important system stuff. So, I opened up GDB, connected, and began examining variables. I noticed that the pointer to my frame bitmap was NULL. As I stepped through the program (from the beginning) I realized it had always been NULL from the beginning, but for some reason it hadn't caused errors (it should have though... that is the IDT...). I set this pointer to the value of "end" which is exported from my linker script, and should point directly after my kernel, and upon examining "end" in gdb, I realized end was null also! Can you guys think of any reason that the linker would report "end" to be zero? Here is my linker script:

Code: Select all

OUTPUT_FORMAT("elf32-i386")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
Thanks, Caleb.

Re: "end" symbol from my linker script is zero?

Posted: Thu Jun 16, 2011 3:30 pm
by gerryg400
Remember that 'end' is not a variable. It does not have a value. You must take its address with &end to get the pointer you are looking for.

[EDIT:] Berkus is fast.

Re: "end" symbol from my linker script is zero?

Posted: Thu Jun 16, 2011 3:37 pm
by Caleb1994
Wow... You guys are fast... You nailed it... I had read online that the symbols in a linker script were accessable in C, but not that an address had to be referenced like that! I thought it was a little weird that when I ran:

gdb>whatis end

out of curiosity, I got "int" which, if it was an address, should have been an "unsigned int". Thanks!

Re: "end" symbol from my linker script is zero?

Posted: Thu Jun 16, 2011 3:51 pm
by gerryg400
For portability between 32 and 64 bit address spaces, it's better to be

Code: Select all

extern const void end;
That makes &end a void *.

Re: "end" symbol from my linker script is zero?

Posted: Thu Jun 16, 2011 4:25 pm
by Caleb1994
Well I was just doing this:

extern end;

because I wasn't sure what type that the linker gave it.

Although, it seems I can't rely on this value now... sigh... Apperantly grub places important things like boot modules right after the kernel so... I have to account for that lol

EDIT:


Oh... wow... I wasn't thinking. In C, when there is no type, "int" is assumed. Bahahahaha I forgot. xD

Re: "end" symbol from my linker script is zero?

Posted: Fri Jun 17, 2011 12:30 pm
by Caleb1994
Yeah, it isn't a problem. It's just that now that I figured out what was wrong with "end", I realized I didn't need it anymore xP