Skip to content
Advertisement

gnu ld treating assembly output as linker script, how to fix? or am i doing something wrong in my compiling?

i’ve been working on my kernel project and to simulate it (that is to run it on QEMU), i need it as a .iso file.

I have an assembly file and to assemble it – as --32 boot.s -o boot.o

and for the main code (which is in c++), to compile it – gcc -S kernel.cpp -lstdc++ -o kernel.o

gives no error. but,

while linking it with this linker script:-

ENTRY(_start)

SECTIONS
{
    /* we need 1MB of space atleast */
    . = 1M;

    /* text section */
    .text BLOCK(4K) : ALIGN(4K)
    {
        *(.multiboot)
        *(.text)
    }

    /* read only data section */
    .rodata BLOCK(4K) : ALIGN(4K)
    {
        *(.rodata)
    }

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

    /* bss section */
    .bss BLOCK(4K) : ALIGN(4K)
    {
        *(COMMON)
        *(.bss)
    }

}

(the linking command i’m using is ld -T linker.ld kernel.o boot.o -o OS.bin)

it says:-

ld:kernel.o: file format not recognized; treating as linker script
ld:kernel.o:1: syntax error

am i doing something wrong with linking or assembling boot.s or compiling kernel.cpp?

Advertisement

Answer

gcc -S produces assembly language, but ld expects an object file. Somewhere in between you have to run the assembler.

There’s no particular need to use the assembler output, so most likely you want to use -c, which does compilation and then assembly to produce an object file, instead of -S:

gcc -c kernel.cpp -o kernel.o

The -lstdc++ is useless because it only applies when linking, and anyway it seems unlikely that you can successfully use the standard C++ library in a kernel.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement