Search code examples
cgccmakefilelinkerosdev

i386 GNU ld: file not recognized: File format not recognized


I'm working my way through The Little Book About OS Development, specifically the section on the framebuffer (linked). I'm able to successfully assemble, link, turn into an ISO file and boot pure assembly, but as soon as I try to link a compiled object file for my C code (called from my loader, which was written in assembly), the linker complains. Here's the output:

nasm -f elf loader.s -o loader.o
nasm -f elf out.s -o out.o
/usr/local/Cellar/gcc@6/6.4.0/bin/gcc-6 -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c  framebuffer.c -o framebuffer.o
/usr/local/Cellar/gcc@6/6.4.0/bin/gcc-6 -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c  kmain.c -o kmain.o
i386-unknown-linux-gnu-ld -T link.ld -melf_i386 loader.o out.o framebuffer.o kmain.o -o kernel.elf
framebuffer.o: file not recognized: File format not recognized
make: *** [kernel.elf] Error 1

I'm on a Mac, so as you can tell I've compiled a custom version of the GNU linker so that I can use linker scripts, and I've made sure to specify GCC 6 (the system default is 4.0). Anyway, here's my Makefile:

OBJECTS = loader.o out.o framebuffer.o kmain.o
CC = /usr/local/Cellar/gcc@6/6.4.0/bin/gcc-6
CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector \
-nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c
LDFLAGS = -T link.ld -melf_i386
AS = nasm
ASFLAGS = -f elf

all: kernel.elf

kernel.elf: $(OBJECTS)
    i386-unknown-linux-gnu-ld $(LDFLAGS) $(OBJECTS) -o kernel.elf

os.iso: kernel.elf
    cp kernel.elf iso/boot/kernel.elf
    mkisofs -R                          \
        -b boot/grub/stage2_eltorito    \
        -no-emul-boot                   \
        -boot-load-size 4               \
        -A os                           \
        -input-charset utf8             \
        -quiet                          \
        -boot-info-table                \
        -o os.iso                       \
        iso

run: os.iso
    bochs -f bochsrc.txt -q

%.o: %.c
    $(CC) $(CFLAGS)  $< -o $@

%.o: %.s
    $(AS) $(ASFLAGS) $< -o $@

clean:
    rm -rf *.o kernel.elf os.iso

Here's my linker script:

ENTRY(loader)                /* the name of the entry label */

SECTIONS {
. = 0x00100000;          /* the code should be loaded at 1 MB */

.text ALIGN (0x1000) :   /* align at 4 KB */
{
*(.text)             /* all text sections from all files */
}

.rodata ALIGN (0x1000) : /* align at 4 KB */
{
*(.rodata*)          /* all read-only data sections from all files */
}

.data ALIGN (0x1000) :   /* align at 4 KB */
{
*(.data)             /* all data sections from all files */
}

.bss ALIGN (0x1000) :    /* align at 4 KB */
{
*(COMMON)            /* all COMMON sections from all files */
*(.bss)              /* all bss sections from all files */
}
}

Any help would be appreciated.


Solution

  • As it turns out, this was solved by cross-compiling GNU Binutils and GCC together (with the same prefix) on my Mac. I did this according to the instructions here.