Strange string problem

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
hunter
Posts: 12
Joined: Fri Jan 05, 2007 9:47 am

Strange string problem

Post 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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Sounds like a missing .rodata in your linker script... have you checked that?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
hunter
Posts: 12
Joined: Fri Jan 05, 2007 9:47 am

Post 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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post 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)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
hunter
Posts: 12
Joined: Fri Jan 05, 2007 9:47 am

Post 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
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Post 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)
  }
}
User avatar
muisei
Member
Member
Posts: 79
Joined: Sat Sep 23, 2006 2:10 pm
Location: Bulgaria
Contact:

Post 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"
Otter
Member
Member
Posts: 75
Joined: Sun Dec 31, 2006 11:56 am
Location: Germany

Post 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.
Post Reply