Page 1 of 2
Datatype of C++ string literal
Posted: Fri Dec 14, 2007 7:03 pm
by ubergeek
So if I enter text in quotes, like so:
What is the datatype of "Test"? Is it a char*, a const char*, an unsigned char*, or what? I'm having a hard time implementing puts(). (I've got putch... so far all my OS does is print "x" on the screen. Not much, but...
Posted: Fri Dec 14, 2007 7:40 pm
by Pyrofan1
pretty sure it's a const char pointer as the text is stored in the readonly section of the executable.
Posted: Sat Dec 15, 2007 12:41 am
by B.E
Just do some test, i.e write a function that excepts a char * and see if you can call it, if it works, than it's a char *, if not try const char *. etc
Posted: Sat Dec 15, 2007 4:33 am
by JamesM
It is a "const char*". Try putting a string literal into a function that accepts a char* parameter and you'll get a warning on G++ something like "Deprecated implicit conversion from const char* to char*".
Posted: Sat Dec 15, 2007 4:39 am
by B.E
I knew that, I wanted him to find out for him self.
Posted: Sat Dec 15, 2007 7:14 am
by ubergeek
Well then something's wrong. I have
and when I try to do
it compiles successfully, but GRUB tells me something about the executable format not being supported. This leads me to think it didn't really compile successfully, it just thought it did. What's wrong?
Posted: Sat Dec 15, 2007 7:26 am
by JamesM
ubergeek wrote:Well then something's wrong. I have
and when I try to do
it compiles successfully, but GRUB tells me something about the executable format not being supported. This leads me to think it didn't really compile successfully, it just thought it did. What's wrong?
That has nothing to do with the executable format. Does GRUB work without that compiled in? Have you tried removing all code from that function until something breaks?
Posted: Sat Dec 15, 2007 7:47 am
by mystran
Sounds like a problem with linker script.
Do you have .rodata somewhere?
Posted: Sat Dec 15, 2007 8:04 am
by ubergeek
No, I do not have any rodata section in my linker script. Before someone tells me, let me guess (correct me if I'm wrong) - is rodata "read only data?" Because that would explain why initializing a const char* with " " would not work. (consts are read-only.)
So how do I add rodata to my linker script?
Posted: Sat Dec 15, 2007 8:06 am
by ubergeek
JamesM wrote:
That has nothing to do with the executable format. Does GRUB work without that compiled in? Have you tried removing all code from that function until something breaks?
I tested this, what doesn't work is the actual call to puts(). (See below for insight)
Posted: Sat Dec 15, 2007 8:14 am
by mystran
ubergeek wrote:No, I do not have any rodata section in my linker script. Before someone tells me, let me guess (correct me if I'm wrong) - is rodata "read only data?" Because that would explain why initializing a const char* with " " would not work. (consts are read-only.)
So how do I add rodata to my linker script?
Well yes, string go to .rodata by default (there's -fwritable-strings in gcc for forcing them into .data but that's not necessarily a good idea).
You can add .rodata into your script just like any other section. I forexample just use:
You can make it a separate section in the resulting binary just as well, if you feel like it. I couldn't be bothered when I wrote my script.
Posted: Sat Dec 15, 2007 8:33 am
by ubergeek
mystran wrote:I forexample just use:
Is that your entire linker script? I took this one now, from Bran's Kernel Development Tutorial, and it still gave me the exact same error.
Code: Select all
OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
.text phys : AT(phys) {
code = .;
*(.text)
*(.rodata)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .;
}
Posted: Sat Dec 15, 2007 8:45 am
by ubergeek
Wait wait wait! My mistake, I actually was linking with an older linker script, with the new one (with the rodata section) it works great! Now my kernel prints "Hello World!" instead of 'x'.
Next step: Printing "Victory!!!"
Thanks all for your help on this one.
Posted: Sat Dec 15, 2007 10:42 am
by JamesM
np. As a completely shameless plug, if you'd have copied the linker script from
my tutorials you wouldn't have had that problem (it has .rodata in already)!
Posted: Sat Dec 15, 2007 11:27 am
by ubergeek
Thanks, but actually, I was using a linker script from Bran's Kernel Development Tutorial (see the article in the wiki on its errors which, if I had seen first, would have made this post redundant). But your printing to the screen function worked great for me.