3

I am trying to build ELF-file using LLVM (from Homebrew) but I can't get how to link it.

My files:
multiboot2.h:

struct multiboot2_header_t {
// Stub
} multiboot2_header __attribute__((section(".multiboot")));

kernel.c:

#include "multiboot2.h"

void _start() {
// Stub
}

linker.ld:

ENTRY(_start)

SECTIONS
{
    .text: {
    /* link the multiboot struct here */
    . = ALIGN(8);
    KEEP(*(.multiboot))
    /* place all of your code afterwards */
    *(.text)
    }
}

I can compile it to object file kernel.o by command clang -c -o kernel.o kernel.c --target x86_64-none-gnu but I can't get how to link this object file using my linker script.

P.S. Before I never worked with LLVM and linker directly, only GNU GCC building simple Linux apps.

Olsonist
  • 2,051
  • 1
  • 20
  • 35
Denis Sologub
  • 7,277
  • 11
  • 56
  • 123
  • 1
    When you say "build ELF-file" on macOS, what are you really trying to do? Macs don't use ELF — they use their own object file format, and ELF-related tools are not much use, therefore. You won't be able to run an ELF-file on macOS (unless you revise the kernel process management code extensively) and you won't have any ELF support libraries (system library, etc — let alone `libelf.dylib`). – Jonathan Leffler Feb 23 '20 at 14:54
  • @JonathanLeffler I know I can launch it on mac, I wanna run it on qemu via grub loader – Denis Sologub Feb 23 '20 at 14:57
  • 1
    OK — well, good luck. I'm not convinced, but you know and I don't so I bow to your greater knowledge. – Jonathan Leffler Feb 23 '20 at 14:58
  • 1
    You obviously need a linker that can produce an ELF file. Are you using such linker? Which linker is that, and what is the actual command line you used? – Employed Russian Feb 23 '20 at 15:01
  • @JonathanLeffler I built ELF already (but object file only) and found how to pass linker script to clang but now it fails with my linker script. I will put more detail in my question when I will have access to my macbook – Denis Sologub Feb 23 '20 at 15:01
  • @EmployedRussian sorry, I will post a bit later, I am away form macbook now, unfortunately – Denis Sologub Feb 23 '20 at 15:03
  • @EmployedRussian but you are right the problem is with llvm-ld linker, it can't understand my linker.ld script (fails at `.text: {`) – Denis Sologub Feb 23 '20 at 15:05
  • 1
    https://stackoverflow.com/q/60245812/816536 and https://stackoverflow.com/q/59811705/816536 both seem related -- and do not yet have an answer either. The docs suggest linker script support is not yet complete in LLD. Perhaps you can post your linker command line too? – Greg A. Woods Feb 25 '20 at 00:26
  • Just so everybody knows, LLVM is a cross compiler in and of itself. What OP is asking is doable on any platform, and is a primary use case of clang. The answer below is correct if memory serves. – Qix - MONICA WAS MISTREATED Dec 26 '20 at 03:08

1 Answers1

6
clang --target=aarch64-unknown-linux-gnu -c file.c

Clang will see that you are targeting Linux and emit an ELF file.o

% file file.o
file.o: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), not stripped

You'll then want to use ld.lld which you can get from Homebrew. BTW, linker script support for ELF ld.lld is quite good. However, linker script support for Mach-O ld64.lld is non-existent.

Olsonist
  • 2,051
  • 1
  • 20
  • 35