Strange behaviour when cross-compiling w/clang and gcc
Posted: Thu Sep 29, 2022 1:23 pm
Hello everyone!
I'm currently writing an MBR bootloader for x86; as a learning exercise.
It uses FAT16 as of right now and has the usual stuff: BPB, root directory parsing and file loading to memory (The second stage of the bootloader) etc...
I've recently upgraded the host binutils to (I think) 2.39 and all of a sudden a bunch of new warnings have appeared.
One of them was 'missing .note.GNU-stack section implies executable stack'
Which I easily solved by supplying the linker a simple no-exec-stack flag.
The other 2 ones, are kinda giving me a bad time.
I'm talking about: 'warning: <file> has a LOAD segment with RWX permissions'
and: warning: relocation in read-only section `.text' /usr/bin/ld: warning: creating DT_TEXTREL in a PIE
For the first one, (Based on https://www.redhat.com/zh/blog/linkers- ... d-segments) I specified a 4K ALIGN param between each section of the linker script; that looks like it fixed it.
And for the second one... Well, I've added the regular no-pie and no-pic flags (CFLAGS-> -fno-pic -fno-pie ; LDFLAGS -> -no-pie -nostdlib -static) and for some reason, under gcc, the warning doesn't appear anymore and the code works and boots correctly; but under clang, it's a completely different story.
The code straight up doesn't work.
Under a debugger I can see this:
Which is strange because code seems to get compiled perfectly fine.
I'll leave the gcc debugger output for reference:
After further inspection, I found two things.
If I remove both -no-pie and -static from the link flags, it works under clang.
If I only remove -static, I get the 'Cannot find bounds of current function' in gdb again.
Or, if I remove -no-pie, the emulator (qemu in this case) constantly reboots (It's triple faulting).
I'm not really sure why this is happening and I'd love some guidance if possible, I've tried a bunch of things I've thought but it doesn't look like it fixes it.
I could go gcc-only but I think I'd be missing some stuff I really appreciate from clang (And I also like having code that has 0 to no warnings)
Repository:
https://github.com/cakehonolulu/atom
Thanks for reading!
I'm currently writing an MBR bootloader for x86; as a learning exercise.
It uses FAT16 as of right now and has the usual stuff: BPB, root directory parsing and file loading to memory (The second stage of the bootloader) etc...
I've recently upgraded the host binutils to (I think) 2.39 and all of a sudden a bunch of new warnings have appeared.
One of them was 'missing .note.GNU-stack section implies executable stack'
Which I easily solved by supplying the linker a simple no-exec-stack flag.
The other 2 ones, are kinda giving me a bad time.
I'm talking about: 'warning: <file> has a LOAD segment with RWX permissions'
and: warning: relocation in read-only section `.text' /usr/bin/ld: warning: creating DT_TEXTREL in a PIE
For the first one, (Based on https://www.redhat.com/zh/blog/linkers- ... d-segments) I specified a 4K ALIGN param between each section of the linker script; that looks like it fixed it.
And for the second one... Well, I've added the regular no-pie and no-pic flags (CFLAGS-> -fno-pic -fno-pie ; LDFLAGS -> -no-pie -nostdlib -static) and for some reason, under gcc, the warning doesn't appear anymore and the code works and boots correctly; but under clang, it's a completely different story.
The code straight up doesn't work.
Under a debugger I can see this:
Code: Select all
Breakpoint 1, 0x00007c00 in ?? ()
(gdb) s
Cannot find bounds of current function
(gdb)
Cannot find bounds of current function
I'll leave the gcc debugger output for reference:
Code: Select all
Breakpoint 1, 0x00007c00 in init0_fat16 ()
(gdb) s
Single stepping until exit from function init0_fat16,
which has no line number information.
53 xor %ax, %ax # Xor'ing ax to ax, results in a 0, as xor'ing two registers with
(gdb)
55 mov %ax, %ds # Move 0x0 to the data segment register.
(gdb)
If I remove both -no-pie and -static from the link flags, it works under clang.
If I only remove -static, I get the 'Cannot find bounds of current function' in gdb again.
Or, if I remove -no-pie, the emulator (qemu in this case) constantly reboots (It's triple faulting).
I'm not really sure why this is happening and I'd love some guidance if possible, I've tried a bunch of things I've thought but it doesn't look like it fixes it.
I could go gcc-only but I think I'd be missing some stuff I really appreciate from clang (And I also like having code that has 0 to no warnings)
Repository:
https://github.com/cakehonolulu/atom
Thanks for reading!