Page 1 of 1

LLVM/Clang Linker Scripts - Bare Bones Tutorial

Posted: Sat Dec 07, 2019 4:17 am
by elgun
Hi. It's my third time in kernel development. Previously I developed toy kernels with C (GCC) and Rust.
This time I want to give a try to LLVM/Clang, as it is much easier to cross-compile.
I followed Bare Bones tutorial. For cross-compiling Assembly and C source files I added

Code: Select all

-target i686-none-elf
flags to clang. It compiled successfully.

But I stuck at linking step. First I run this command:

Code: Select all

clang -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib boot.o kernel.o -lgcc
And It gave me this error

Code: Select all

clang: warning: argument unused during compilation: '-T linker.ld' [-Wunused-command-line-argument]
boot.o : fatal error LNK1107: invalid or corrupt file: cannot read at 0x2A0
After I tried -target i686-none-elf and -fuse-ld=lld-link flags. But it doesn't work.
Any idea about how can we link final kernel binary?

Re: LLVM/Clang Linker Scripts - Bare Bones Tutorial

Posted: Sat Dec 07, 2019 12:09 pm
by beyondsociety
With clang, you will need to add

Code: Select all

-Wl
to the linker options to fix that error. So for example,

Code: Select all

-Wl,-T,linker.ld
Hope this helps.