GCC optimization causing an invalid opcode
Posted: Mon Jan 09, 2012 5:21 pm
I've been a long time stalker of the forums but I've met a stumper that I cannot pass. Five hours of battle with this error and it is still winning.
After several false-starts (albeit it valuable learning experiences) I've decided to officially start my project.
GCC optimizations are causing the Invalid Opcode exceptions though whenever I call my second or third functions. I have my init() function (the entry point from the bootloader), print, printHex, and clearscreen. But calling either printHex or clearscreen causes the invalid opcode exception. Both functions call the print function; printHex simply creates a string of characters that is the ascii representation of the number to be printed and calls print with that string while clearscreen calls print 25 times with a string of 80 <space key> characters (Hackish method yes, but as these functions are only until I have a graphics driver I just need them to work until then).
Any level of optimizations causes this so I do know that it is something activated with -O1 but I'm not sure what specifically. I thought it was -fauto-inc-dec but when I turned off optimizations and turned only it on there was no error. I decided to go through the entire list of -O1 optimizations and enable them all one at a time, getting no invalid opcode errors. The only error I did get was a no compile-time error when I enabled -fdelayed-branch, with GCC claiming that the target (x86_64-elf with -m64 and -mtune=corei7) does not support delayed branches. Could it be that when optimization is enabled it doesn't check for target support of each individual optimization? And if so does this mean I must compile with this particular optimization turned off?
Or is this the mark of a source code error that only optimization brings out? Unfortunately despite having all warnings turned on and have all of them treated as an error the compiler builds the kernel without any sort of error or warning and bochs simply fails whenever a call is made to the clearscreen or printHex functions. I can call print as much as I want though.
Sorry if this is fairly obvious.
EDIT: Some non-readable characters appear in the first two positions on the screen, which happened before for me when I had an include file that contained code before the entry point as it started executing random code that corresponded to functions and their data that were misaligned but now I'm using a linker script with my init() function set as the entry point. As the script tells it to output a flat binary and that the init() is the entry point shouldn't it just create a jump/call to init() at the very beginning? Another reason why I doubt it is this is because the two included files only contain typedefs, defines, and macros. Nothing that ends up being actually compiled into code. The only other thing before the init() function in the actually source file is function declarations which also don't get compiled into actual code as the source from them are after the init() function so it doesn't seem like it can be a case of improper entry but it sure is looking like one.
After several false-starts (albeit it valuable learning experiences) I've decided to officially start my project.
GCC optimizations are causing the Invalid Opcode exceptions though whenever I call my second or third functions. I have my init() function (the entry point from the bootloader), print, printHex, and clearscreen. But calling either printHex or clearscreen causes the invalid opcode exception. Both functions call the print function; printHex simply creates a string of characters that is the ascii representation of the number to be printed and calls print with that string while clearscreen calls print 25 times with a string of 80 <space key> characters (Hackish method yes, but as these functions are only until I have a graphics driver I just need them to work until then).
Any level of optimizations causes this so I do know that it is something activated with -O1 but I'm not sure what specifically. I thought it was -fauto-inc-dec but when I turned off optimizations and turned only it on there was no error. I decided to go through the entire list of -O1 optimizations and enable them all one at a time, getting no invalid opcode errors. The only error I did get was a no compile-time error when I enabled -fdelayed-branch, with GCC claiming that the target (x86_64-elf with -m64 and -mtune=corei7) does not support delayed branches. Could it be that when optimization is enabled it doesn't check for target support of each individual optimization? And if so does this mean I must compile with this particular optimization turned off?
Or is this the mark of a source code error that only optimization brings out? Unfortunately despite having all warnings turned on and have all of them treated as an error the compiler builds the kernel without any sort of error or warning and bochs simply fails whenever a call is made to the clearscreen or printHex functions. I can call print as much as I want though.
Sorry if this is fairly obvious.
EDIT: Some non-readable characters appear in the first two positions on the screen, which happened before for me when I had an include file that contained code before the entry point as it started executing random code that corresponded to functions and their data that were misaligned but now I'm using a linker script with my init() function set as the entry point. As the script tells it to output a flat binary and that the init() is the entry point shouldn't it just create a jump/call to init() at the very beginning? Another reason why I doubt it is this is because the two included files only contain typedefs, defines, and macros. Nothing that ends up being actually compiled into code. The only other thing before the init() function in the actually source file is function declarations which also don't get compiled into actual code as the source from them are after the init() function so it doesn't seem like it can be a case of improper entry but it sure is looking like one.