The OS does not link correctly on x86_64

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
rpio
Member
Member
Posts: 92
Joined: Sat Feb 20, 2021 3:11 pm

The OS does not link correctly on x86_64

Post by rpio »

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
Last edited by rpio on Sun Jan 23, 2022 4:42 am, edited 1 time in total.
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: The OS does not link correctly on x86_64

Post by austanss »

try switching --target x86_64-elf and --arch x86_64-elf to --target=x86_64-elf and -march=x86_64
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
rpio
Member
Member
Posts: 92
Joined: Sat Feb 20, 2021 3:11 pm

Re: The OS does not link correctly on x86_64

Post by rpio »

rizxt wrote:try switching --target x86_64-elf and --arch x86_64-elf to --target=x86_64-elf and -march=x86_64
Yes thanks, that also compiles well, but what exactly is the difference between -march and -arch for the linker - because linker said it would ignore -arch, but does not say same thing when I use -march?

But I probably figured out the problem with the address printing, I checked if the address is correct and not garbage not by looking at printf output, but instead by using the if comparison and it showed that the variable stores the right address. The problem is then with my printf implementation, I would be very grateful if you helped me to figure out the problem with printf(I think it is somehow related to 64-Bit mode switch because it worked in 32-Bit mode)
Last edited by rpio on Sun Jan 23, 2022 4:44 am, edited 1 time in total.
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: The OS does not link correctly on x86_64

Post by austanss »

ngx wrote:
rizxt wrote:try switching --target x86_64-elf and --arch x86_64-elf to --target=x86_64-elf and -march=x86_64
Yes thanks, that also compiles well, but what exactly is the difference between -march and -arch for the linker - because linker said it would ignore -arch, but does not say same thing when I use -march?

But I probably figured out the problem with the address printing, I checked if the address is correct and not garbage not by looking at printf output, but instead by using the if comparison and it showed that the variable stores the right address. The problem is then with my printf implementation, I would be very grateful if you helped me to figure out the problem with printf(I think it is somehow related to 64-Bit mode switch because it worked in 32-Bit mode)

My printf implementation is starting on line 149 in https://pastebin.com/5LhRHSUA
I don't believe the -arch switch exists. I believe you were attempting to use -march.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: The OS does not link correctly on x86_64

Post by austanss »

ngx wrote:
rizxt wrote:try switching --target x86_64-elf and --arch x86_64-elf to --target=x86_64-elf and -march=x86_64
Yes thanks, that also compiles well, but what exactly is the difference between -march and -arch for the linker - because linker said it would ignore -arch, but does not say same thing when I use -march?

But I probably figured out the problem with the address printing, I checked if the address is correct and not garbage not by looking at printf output, but instead by using the if comparison and it showed that the variable stores the right address. The problem is then with my printf implementation, I would be very grateful if you helped me to figure out the problem with printf(I think it is somehow related to 64-Bit mode switch because it worked in 32-Bit mode)

My printf implementation is starting on line 149 in https://pastebin.com/5LhRHSUA
Your printf function probably only supports 64-bit integers.

I have printf errors too! :lol:
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
rpio
Member
Member
Posts: 92
Joined: Sat Feb 20, 2021 3:11 pm

Re: The OS does not link correctly on x86_64

Post by rpio »

rizxt wrote:
ngx wrote:
rizxt wrote:try switching --target x86_64-elf and --arch x86_64-elf to --target=x86_64-elf and -march=x86_64
Yes thanks, that also compiles well, but what exactly is the difference between -march and -arch for the linker - because linker said it would ignore -arch, but does not say same thing when I use -march?

But I probably figured out the problem with the address printing, I checked if the address is correct and not garbage not by looking at printf output, but instead by using the if comparison and it showed that the variable stores the right address. The problem is then with my printf implementation, I would be very grateful if you helped me to figure out the problem with printf(I think it is somehow related to 64-Bit mode switch because it worked in 32-Bit mode)
I don't believe the -arch switch exists. I believe you were attempting to use -march.
No, arch exists because I saw it in the clang manual, but I can't understand why the linker says it will ignore it?
Last edited by rpio on Sun Jan 23, 2022 4:44 am, edited 1 time in total.
rpio
Member
Member
Posts: 92
Joined: Sat Feb 20, 2021 3:11 pm

Re: The OS does not link correctly on x86_64

Post by rpio »

rizxt wrote:
ngx wrote:
rizxt wrote:try switching --target x86_64-elf and --arch x86_64-elf to --target=x86_64-elf and -march=x86_64
Yes thanks, that also compiles well, but what exactly is the difference between -march and -arch for the linker - because linker said it would ignore -arch, but does not say same thing when I use -march?

But I probably figured out the problem with the address printing, I checked if the address is correct and not garbage not by looking at printf output, but instead by using the if comparison and it showed that the variable stores the right address. The problem is then with my printf implementation, I would be very grateful if you helped me to figure out the problem with printf(I think it is somehow related to 64-Bit mode switch because it worked in 32-Bit mode)
Your printf function probably only supports 64-bit integers.

I have printf errors too! :lol:
What is the problem if the printf function supports only 64-Bit integers, why would it print zeroes instead of the real addresses?
Maybe it is related to stack, because it was setup in protected mode - should I make a new one in long mode ?
Last edited by rpio on Sun Jan 23, 2022 4:44 am, edited 1 time in total.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: The OS does not link correctly on x86_64

Post by Octocontrabass »

You need to use the stdarg.h macros to read the arguments to your printf function.
Post Reply