The OS does not link correctly on x86_64
Posted: Wed Feb 24, 2021 11:57 am
Recently I switched to running my OS in long mode on x86_64, but problems occured nearly immediately after the switch:
The main problem I have in the switch to x86_64 is linkage "errors" - by "errors" I mean not the ones linker gives and refuses to link because of them, but the ones that occur because of wrong configuration and can only be seen when your OS works incorrectly:
1. I use clang to compile C code and to link it with assembly output(which is assembled by nasm). The problem is that my code is linked correctly in some variations of set target, but not in others(I haven't included ones where the output binary isn't produced):
- Works - By works I mean that the OS boots and GRUB manages to find multiboot2 header
* Compiler Target(--target x86-64-elf), Linker Target(--target x86-64-elf)
* Compiler Target(-arch x86_64-elf), Linker Target(-arch x86_64-elf)
* But here the linker warns me that clang-11: warning: argument unused during compilation: '-arch x86_64-elf'
- Does not work - by "does not work I mean that the GRUB fails to find the multiboot2 header - which means that OS is incorrectly linked
* Compiler Target(--target x86_64-elf), Linker Target(--target x86_64-elf),
2. The second problem with linkage is that the variables that I made during protected mode in assembly in the .bss section and filled them with values at runtime(not at write time because i know .bss is cleared), but then when i gte to C code and get them through extern, they hold no or garbage value which is probably another linkage problem
Also I have had a few other questions about long mode:
3. Should a new stack be made after switch to long mode if one was made in the protected mode?
4. Can I mark the pages kernel is located on as read only(in page flags) so the kernel can't be accidentaly overwritten by data(which in my case is under the kernel)?
For compilation(if somebody want's to look at the makfile) I just write make(so the first make rule is used and i make the x86_64_clang OS)
Thanks to everybody in advance, this linkage problem followed me even in the 32 bit mode
The main problem I have in the switch to x86_64 is linkage "errors" - by "errors" I mean not the ones linker gives and refuses to link because of them, but the ones that occur because of wrong configuration and can only be seen when your OS works incorrectly:
1. I use clang to compile C code and to link it with assembly output(which is assembled by nasm). The problem is that my code is linked correctly in some variations of set target, but not in others(I haven't included ones where the output binary isn't produced):
- Works - By works I mean that the OS boots and GRUB manages to find multiboot2 header
* Compiler Target(--target x86-64-elf), Linker Target(--target x86-64-elf)
* Compiler Target(-arch x86_64-elf), Linker Target(-arch x86_64-elf)
* But here the linker warns me that clang-11: warning: argument unused during compilation: '-arch x86_64-elf'
- Does not work - by "does not work I mean that the GRUB fails to find the multiboot2 header - which means that OS is incorrectly linked
* Compiler Target(--target x86_64-elf), Linker Target(--target x86_64-elf),
2. The second problem with linkage is that the variables that I made during protected mode in assembly in the .bss section and filled them with values at runtime(not at write time because i know .bss is cleared), but then when i gte to C code and get them through extern, they hold no or garbage value which is probably another linkage problem
Also I have had a few other questions about long mode:
3. Should a new stack be made after switch to long mode if one was made in the protected mode?
4. Can I mark the pages kernel is located on as read only(in page flags) so the kernel can't be accidentaly overwritten by data(which in my case is under the kernel)?
For compilation(if somebody want's to look at the makfile) I just write make(so the first make rule is used and i make the x86_64_clang OS)
Thanks to everybody in advance, this linkage problem followed me even in the 32 bit mode