Clang/LLD Dysfunctional with Bare Bones Linker Script

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
Cdev
Posts: 8
Joined: Wed Mar 10, 2021 5:03 am

Clang/LLD Dysfunctional with Bare Bones Linker Script

Post by Cdev »

I am using the exact same code from the Barebones tutorial for my linker script. It works very well with GNU LD. However, there are multiple problems with LLVM's LLD when linking. In both cases, the object files from both GNU GCC and LLVM Clang were tested in combination; these issues are linker-only.

The script is :

Code: Select all

ENTRY(_start)

SECTIONS
{
	. = 1M;
 
	.text BLOCK(4K) : ALIGN(4K)
	{
		*(.multiboot)
		*(.text)
	}
 
	.rodata BLOCK(4K) : ALIGN(4K)
	{
		*(.rodata)
	}

	.data BLOCK(4K) : ALIGN(4K)
	{
		*(.data)
	}

	.bss BLOCK(4K) : ALIGN(4K)
	{
		*(COMMON)
		*(.bss)
	}
        end = .;
}
My only addition is the little "end =.;" at the end.

Now :
1. Linking like this :

Code: Select all

clang --target=i686-pc-none-elf -march=i686 -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib *.o -lgcc
Returns :

Code: Select all

/usr/bin/ld: boot.o: warning: relocation in read-only section `.text'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
(No such issues on i686-elf-gcc)
However, the hello world boots as it should, and I don't know what these warnings are so ...?

2. Linking like above (or even like below) but with the -static option gives 0 warnings ; but grub tells me "multiboot header not found".

3. Linking like first but with an added

Code: Select all

-fuse-ld=lld
it then proceeds to dumbfound me with a whacky :

Code: Select all

ld.lld: error: linker.ld:7: unknown section directive: 4K
Perplexing, much.

All above being done on Arch Linux , Linux lts 5.10.21-1 , Clang 11.1.0 (from the Arch repo) , i686-elf-gcc 10.2.0 from the GNU mirror, Binutils 2.36.1 with pretty much a hello world kernel.

As would be obvious, I am quite new here, though by no means ignorant. Thanks a lot for help in advance !

P.S : The assembly "boot.s" that I am using (same as the Barebones one) :

Code: Select all

.set ALIGN,    1<<0  
.set MEMINFO,  1<<1 
.set FLAGS,    ALIGN | MEMINFO
.set MAGIC,    0x1BADB002
.set CHECKSUM, -(MAGIC + FLAGS) 

.section .multiboot
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM

.section .bss
.align 16
stack_bottom:
.skip 16384 # 16 KiB
stack_top:

.section .text
.global _start
.type _start, @function
_start:
	mov $stack_top, %esp

	call kernel_main

	cli
1:	hlt
	jmp 1b
.size _start, . - _start
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Clang/LLD Dysfunctional with Bare Bones Linker Script

Post by Octocontrabass »

Cdev wrote:

Code: Select all

/usr/bin/ld
Clang is using the host LD instead of the target LD. I'm not especially familiar with this combination (why wouldn't you want to use LLD with Clang?) but if you really want to use LD you'll have to tell Clang how to find the correct one. This guide might help.
Cdev wrote:3. Linking like first but with an added

Code: Select all

-fuse-ld=lld
it then proceeds to dumbfound me with a whacky :
LD supports a lot of directives for backwards compatibility. LLD does not. BLOCK() is one of the directives LD supports but LLD does not. As far as I know it's an alias for ALIGN() so it should be fine to just delete BLOCK().
Post Reply