Very strange GCC/Linker problem!
Posted: Fri Dec 08, 2006 12:14 am
Hi! I got really interested in OS development and wanted to make a simple kernel. I noticed a very strange and annoying case with gcc and ld. Say I create a function called strlen() to calculate the length of a string that is passed to it. If I create the string using a literal string like...
char string[] = "Hello World!";
strlen(string);
and then I pass it to the function, the program goes from normally compiling to about 8kb to about 1.0mb!!! And then when I try to load the kernel with GRUB, it says that it's an invalid kernel. Here is an interesting case however, If I initialize the string like this
char string[16];
string[0] = 'H';
string[1] = 'e';
and so on
and then I append it with
string[x] = 0 (Null terminator)
strlen(string);
And compile, the code compiles back down to 8kb and is successfully loaded by GRUB. The interesting thing is that the program will not increase in size and become corrupt if I merely declare the variable:
char string[16] = "Hello World!";
But it happens when I pass the variable to a function, i.e. strlen (that I have created). I believe it has something to do with the stack but I don't have enough knowledge in this area and I was wondering if someone could give me some insight! I am using a linker script.
http://www.osdever.net/bkerndev/Docs/intro.htm
This is the site that I'm following, so the source code I'm using is basically exactly what he has, except edited a bit because he sucks at proof reading . Thanks for the help!
char string[] = "Hello World!";
strlen(string);
and then I pass it to the function, the program goes from normally compiling to about 8kb to about 1.0mb!!! And then when I try to load the kernel with GRUB, it says that it's an invalid kernel. Here is an interesting case however, If I initialize the string like this
char string[16];
string[0] = 'H';
string[1] = 'e';
and so on
and then I append it with
string[x] = 0 (Null terminator)
strlen(string);
And compile, the code compiles back down to 8kb and is successfully loaded by GRUB. The interesting thing is that the program will not increase in size and become corrupt if I merely declare the variable:
char string[16] = "Hello World!";
But it happens when I pass the variable to a function, i.e. strlen (that I have created). I believe it has something to do with the stack but I don't have enough knowledge in this area and I was wondering if someone could give me some insight! I am using a linker script.
http://www.osdever.net/bkerndev/Docs/intro.htm
This is the site that I'm following, so the source code I'm using is basically exactly what he has, except edited a bit because he sucks at proof reading . Thanks for the help!