Page 1 of 1

Strange string problem

Posted: Fri Feb 23, 2007 3:02 pm
by hunter
Hello all,

i have big problems with strings in my user applications ... if i want to send the kernel strings from a user application there are strange problems ..

For example: If i use this way to write a string it works ...

Code: Select all

	string[0] = 'A';
	string[1] = 'B';
	string[2] = 'C';
	string[3] = '\0';
But if i use this way ... it doesn't work ...

Code: Select all

char test[4] = "ABC\0";
	
	for( i=0;i<4;i++ )
		string[i] = test[i];
Do the codes the same one ?? Or is it possible the second code doesn't do the same as the first one ??

... sorry for my bad english ...

Hunter

Posted: Fri Feb 23, 2007 3:04 pm
by Combuster
Sounds like a missing .rodata in your linker script... have you checked that?

Posted: Fri Feb 23, 2007 3:08 pm
by hunter
Combuster wrote:Sounds like a missing .rodata in your linker script... have you checked that?
What do mean with .rodata .. could you give me an example please ...

Hunter

Posted: Fri Feb 23, 2007 3:21 pm
by Combuster
gcc normally puts strings in a special section in your executable: .rodata

If you use a linker script, you should check that it copies .rodata to the output. (common knowledge. see one of Solar's post for an example)

Posted: Sat Feb 24, 2007 6:32 am
by hunter
Hello,

I have add the .rodata part in my linker file ... but it doesn't work ...
I have two linker files one for the kernel and an other for the user programs ..

Here the Linkerfile for the Kernel:

Code: Select all

OUTPUT_FORMAT( "binary" )
INPUT( ....................... )
ENTRY(start)
SECTIONS
{
  .text 0x100000 : {
    *(.text)
    *(.rodata*)
    . = ALIGN(4096);
  }
  .data : {
    *(.data)
  }
  .bss :  {					
    *(.bss)
  }
}
And her is the linkerfile for the Userprograms:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(_main)
Which linkerfile i have to edit that the kernel can see the strings from the userprograms ?

I hope somebody could help me ...

Hunter

Posted: Sat Feb 24, 2007 9:17 am
by iammisc
You need to use the same style of linker script for userspace apps too.

try this

Code: Select all

OUTPUT_FORMAT( "binary" )
INPUT( ....................... )
ENTRY(start)
SECTIONS
{
  . = wherever user space things are loaded
  .text : {
    *(.text)
    *(.rodata*)
    . = ALIGN(4096);
  }
  .data : {
    *(.data)
  }
  .bss :  {               
    *(.bss)
  }
}

Posted: Sat Feb 24, 2007 11:56 am
by muisei
Why do you add the terminating '0' to the string?The compiler should add it itself.

char test[4]='A,'B','C','\0' is equal to char test[4]="ABC"

Posted: Sat Feb 24, 2007 12:03 pm
by Otter
You can also use a simple stdlib you link in before the rest of your kernel/program:

Code: Select all

$ ld stdlib.o kernel.o -o kernel
This stdlib could call the main function of the kernel.