Data segment at the beginning of the binary

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
delroth
Posts: 8
Joined: Sun Oct 01, 2006 3:29 am
Location: At my computer
Contact:

Data segment at the beginning of the binary

Post 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 ?
User avatar
gaf
Member
Member
Posts: 349
Joined: Thu Oct 21, 2004 11:00 pm
Location: Munich, Germany

Post 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
delroth
Posts: 8
Joined: Sun Oct 01, 2006 3:29 am
Location: At my computer
Contact:

Post by delroth »

Thank you, it works :)
Post Reply