Search code examples
llvm-clang

error: unexpected token in '.section' directive .section .multiboot


I found a sample of simple kernel loader and try to get how it works. But I can't even build it due to errors:

loader.s:5:20: error: unexpected token in '.section' directive
.section .multiboot
                   ^
loader.s:11:15: error: unexpected token in '.section' directive
.section .text
              ^
loader.s:28:1: error: unknown directive
.sectio .bss 

Here is loader code:

.set MAGIC, 0x1badb002
.set FLAGS, (1<<0 | 1<<1)
.set CHECKSUM, -(MAGIC + FLAGS)

.section .multiboot
    .long MAGIC
    .long FLAGS
    .long CHECKSUM


.section .text
.extern main
.extern callConstructors
.global loader

loader:
    mov $kernel_stack, %esp
    call callConstructors
    push %eax
    push %ebx
    call main

_stop:
    cli
    hlt
    jmp _stop

.sectio .bss 
.space 2*1024*1024;
kernel_stack:

I compile it as as -m32 loader.s.

After search I found that I don't need to use .section with .text and .bss but I don't know what's .multiboot section and how to fix this error. I could not find information about .multiboot in Google.

P.S. If it's important I use MacOS.


Solution

  • P.S. If it's important I use MacOS.

    That's part of it. Presumably your as is actually the default Apple Clang (LLVM) assembler (check as --version) targeting your host machine. This code was written to be built with a GCC cross-compiler. The code is assuming you're running under an i686-elf cross compiler specifically. I think LLVM and GCC assemblers are pretty interchangeable, but the directives depend on your output target. For you, as/clang/whatever on OSX by default assumes you are compiling for your host machine, which I think doesn't use an ELF output target. Passing -target i686-elf should do the trick.

    That said, if you are pushing forward into more meaty OS/kernel dev, there is good reason to build yourself a real cross-compiler. Passing a bunch of flags to your host compiler to force it into cross-compiling is probably going to land you in some pain down the road.

    EDIT: I found myself to your question because I'm trying to set up an OS dev environment on my Macbook. If you're building a multiboot kernel you will eventually need to link the above snippet with the real kernel, probably using a linker script. Unfortunately I just found out that Apple's ld doesn't accept GCC-style linker scripts (real ld from LLVM does, so this is an Apple idiosyncrasy), so I'm giving up and just building GCC on my machine. I would recommend that route!