Page 1 of 1

Constant Strings in C giving problems

Posted: Tue Mar 31, 2009 2:03 pm
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... :)

Re: Constant Strings in C giving problems

Posted: Tue Mar 31, 2009 3:21 pm
by tarrox
Maybe your Linker-Skript is wrong, and writes wrong addresses.

Re: Constant Strings in C giving problems

Posted: Tue Mar 31, 2009 3:35 pm
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.

Re: Constant Strings in C giving problems

Posted: Tue Mar 31, 2009 3:58 pm
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?

Re: Constant Strings in C giving problems

Posted: Tue Mar 31, 2009 5:07 pm
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

Re: Constant Strings in C giving problems

Posted: Wed Apr 01, 2009 2:31 am
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.