string literals break kernel

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
Sages

string literals break kernel

Post by Sages »

Hi everyone, I apologize for the total newb question I'm about to ask but for someone reason my kernel doesn't print text correctly when I use a string literal. Here is a sample.

Code: Select all

void KernelEntry()
{
    ...
    // This version works fine
    char strMe[3] = { 'M', 'e', 0 };

    // but if I use this instead...
    char strMe[] = "Me";    // this causes nothing to print    
    PrintString(strMe);
    ...
}
I'm new to all of this and would like to thank you all in advance for any help you may be able to give me. Oh, and I am using a linker script which places my text segment above .data and .bss. my tools are gcc, nasm and ld.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:string literals break kernel

Post by Candy »

default guess: the string is recognised as read-only and thus put with the code.

Solutions: configure the rodata to end up properly, fix your pointers, use -fwritable-strings or put .rodata in .data in your linker script.
Sages

Re:string literals break kernel

Post by Sages »

I am using the -fwritable-strings option but I'm not too sure what that .rodata stuff is about. could you please expand on that? This is my current linker script.

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(_start)
SECTIONS
{
  .text 0x10000 : {
    *(.text)
  }
  .data : {
    *(.data)
  }
  .bss : {
    *(.bss)
  }
}
Sages

Re:string literals break kernel

Post by Sages »

well, the problem was... I'm retarded. Unfortunately that's do to a genetic defect that I can't change. However, I did manage to get the code to work.

kernel.lnk...

Code: Select all

...
  .text 0x10000    #  << I'm only off by a power of 2
  .text 0x100000   # << amazing what a missing zero can do
...
but if anyone could please explain what that .rodata segment was I'd appreciate it. Or I suppose I could just google for it. Well thanks for you help.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:string literals break kernel

Post by Candy »

.rodata == read only data

what your strings are put in if you don't put them in .data (with -fwritable-strings). That part is data but can be put in the .text section to save overhead with program texts being multiply mapped.
Dragon88

Re:string literals break kernel

Post by Dragon88 »

I have this problem too. I'm not using a linker script though, what can I do? If I use -fwritable-strings it puts the strings directly in front of the code and it doesn't work right.
Tim

Re:string literals break kernel

Post by Tim »

Use a linker script and put the .rodata section in with the .text section. Then you don't have to use -fwriteable-strings.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:string literals break kernel

Post by bubach »

char strMe[] = "Me"; // this causes nothing to print
where is the zero in that? u need to have a zero in the end just like u had in :
char strMe[3] = { 'M', 'e', 0 };
/ Christoffer
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
Tim

Re:string literals break kernel

Post by Tim »

The zero is added automatically by the compiler. All "quoted strings" are nul-terminated; otherwise no quoted strings would work.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:string literals break kernel

Post by bubach »

Ok, stupid me.. ;)
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
Post Reply