I know this is a rather difficult question to answer, mainly because there are so many things that can be wrong, and it's hard to catch. But I will give as much information as possible; hope this helps.
I started writing my own kernel using the D language and the Digital Mars D compiler, and after many problems figuring out how to create flat binary files that can be moved, I finally came up with the idea of creating a regular PE file for address 0xC0000000 and replacing all its headers byte 0x90 (operation code NOP). This worked perfectly, and I could write things on the screen, set up paging, enter protected mode, etc. Great, of course, with the help of the help of a 16-bit build-based assembly loader.
Everything was fine, that is, I decided to port the D runtime library for use in my kernel. I managed to extract a subset of the library and modify it so that it was compiled into my application. Then I launched my program. (Note: I didn’t use the whole library, my code was the first code executed after loading - the first thing that happened was printing "Kernel" on the screen and the lack of runtime caused the code before.)
The AD array (and therefore the string, since the string is just char[] ), is nothing more than a structure with a pointer and a size member, so on a 32-bit system it will be 8 bytes. The funny thing is that when I ran my program, members of the structure showed that they are equal to zero, that is, both pointers and size are equal to zero. (I checked this by typing the value of the pointer on the screen, as well as the length member - both were zero.) As soon as I deleted the source code for the runtime (which was never executed), they worked fine.
I narrowed this down to two possibilities:
The stack somehow wasn’t configured correctly: I solved it because everything worked perfectly without a runtime library, and I confirmed that another code was not executed before my code by parsing the file.
Something is funny with sections of the PE file: I checked and found out that there are two TLS (thread-local) variables in the version in the version. Of course, when I made them shared (not streamed), my code worked! However, my code still showed the same problem when I called the code that I wrote in another file - only kernel.d , which is the launch file, correctly lines up; in other files, the arrays were zero again.
Now, can anyone guess why this could happen?
If you need more information, I will be happy to publish it.
Thanks!
Mehrdad
source share