Constant Strings in C giving problems

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
instance
Posts: 16
Joined: Tue Mar 03, 2009 3:40 am

Constant Strings in C giving problems

Post by instance »

Hey,
I'm trying to make some screen functions. One such function is puts, but I realized some pretty anomalous behavior.

..... The following code works-

Code: Select all

int tempVideoMain(){ //Starting point of exec
char str[20]="Hello world";
puts(str);
}
void puts(char str[]){
for(int i=0;str[i]!=0;i++) putch(str[i]); 
}
(note: standard functions of puts and etc are not linked together with this code, this is for the os)

On the other hand, I have no clue why this does not work:

Code: Select all

int tempVideoMain(){
puts("Hello World");       // The only thing that has viably changed
}
void puts(char* str){
for(int i=0;str[i]!=0;i++) putch(str[i]); 
}
When I decompiled the latter code, I see that the argument passed is the memory location 0x160D, which is pretty much nonsense (its actually part of the GDT, which is mostly nulled out).


using djgpp compiler and the putch (put character) function definately works, btw.
Thnx

PS: If this is more related to general programming, then could some1 move this thread there... :)
tarrox
Posts: 19
Joined: Wed Dec 31, 2008 8:40 am

Re: Constant Strings in C giving problems

Post by tarrox »

Maybe your Linker-Skript is wrong, and writes wrong addresses.
instance
Posts: 16
Joined: Tue Mar 03, 2009 3:40 am

Re: Constant Strings in C giving problems

Post by instance »

well, the funny part is I am not really using a linker script :) . I mean, I did ok without it right now... The global data I can access properly, and the function calls also work without a problem.

Anyways, I will try it with a linker script. But shouldn't this data be somehow in the same data section as the global data? If not, could you please tell me where this data is stored?


EDIT:
I just tried out with a linker script, and it did please me. Atleast I got "some" data on the screen getting printed out, but it was some garbage. My linker scripts are-
linkobj.ld

Code: Select all

OUTPUT_FORMAT("coff-go32")
ENTRY(start)
SECTIONS
{
  .text : {
    *(.text)
  }
  .data  : {
    *(.data)
  }
  .bss  :
  { 					
    *(.bss)
  }
}
and link.ld

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS
{
  .text : {
    *(.text)
  }
  .data  : {
    *(.data)
  }
  .bss  :
  { 					
    *(.bss)
  }
}

I have made a few other programs which add null bytes at the end to make the final image.
I need 2 scripts, cause ld.exe of DJGPP seems to have a limit on the no. of arguments it takes in command line. So I wrote a batch file which will incrementally build the kernel into coff object codes, and finally at the end make a binary.


When I ran the ld commands, btw, I got a few warnings.
tarrox
Posts: 19
Joined: Wed Dec 31, 2008 8:40 am

Re: Constant Strings in C giving problems

Post by tarrox »

Try this one, it is not the best but it should work:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x0100000;
SECTIONS
{
	.text phys : AT(phys)
	{
		code = .;
		*(.text)
		. = ALIGN(4096);
	}
	
	.data : AT(phys + (data - code))
	{
		data = .;
		*(.data)
		*(.rodata*)
		. = ALIGN(4096);
	}
	
	.bss : AT(phys + (bss - code))
	{
		bss = .;
		*(.bss)
		. = ALIGN(4096);
	}
	end = .;
}
instance wrote: I have made a few other programs which add null bytes at the end to make the final image.
I need 2 scripts, cause ld.exe of DJGPP seems to have a limit on the no. of arguments it takes in command line. So I wrote a batch file which will incrementally build the kernel into coff object codes, and finally at the end make a binary.
Use a cross-compiler, it's the better use and solves a lot of problems. With the wiki it is no problem to make one.

PS.: I am passing 34 arguments without problems^^. And it is still Rising.
instance wrote: When I ran the ld commands, btw, I got a few warnings.
What warnings?
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:

Re: Constant Strings in C giving problems

Post by Combuster »

Just adding .rodata* or .rdata* to the linker scripts should fix your problem (And with that, you just asked a Frequently Asked Question [-X )

As for the other problems, we need to see error messages
"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 ]
instance
Posts: 16
Joined: Tue Mar 03, 2009 3:40 am

Re: Constant Strings in C giving problems

Post by instance »

Thank you very much Combuster, and very sorry for asking it on the forums...

As for the warnings, they all revolved around .data section is empty, etc.
Post Reply