Page 1 of 1

Data segment at the beginning of the binary

Posted: Sun Oct 01, 2006 10:07 am
by delroth
Hello !

I have now another problem : when i use strings in my program (like "Hello world !"), the compiler makes me an 1M binary file, and place the strings at the beginning of the program, before the AOUT kludge needed by GRUB, so I can't use my kernel.

My main code is :

Code: Select all

#include <VgaText.h> //My VGA Text driver

extern "C" void kernel_main()
{
	VGA::Text::Init();
	VGA::Text::PutString("Hello, World !");
	while(true);
}
And my link.ld file is :

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
But when i use character constants (like 'A'), my kernel boots without any problem.
Can someone say me where is the problem ?

Posted: Mon Oct 02, 2006 8:30 am
by gaf
Probably just another rodata related problem:

Code: Select all

OUTPUT_FORMAT("binary") 
ENTRY(start) 
phys = 0x00100000; 
SECTIONS 
{ 
  .text phys :
  { 
    code = .; 
    *(.text) 
    *(.rodata*)  <- Include the string data
  }
 
  .data : 
  { 
    data = .; 
    *(.data) 
  } 

  .bss : 
  { 
    bss = .; 
    *(.bss) 
  } 
  end = .; 
}
I wrote:The rodata section (read-only data) includes all constant global data that the executable uses. As string literals are constant by definition, they also get put into this section. If your linker-script doesn't include the rodata section, the linker can't add the strings to the binary and all strings pointers are invalid.
regards,
gaf

Posted: Mon Oct 02, 2006 9:17 am
by delroth
Thank you, it works :)