error: invalid arch-dependent ELF magic when using grub

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
anthony
Posts: 9
Joined: Sat Jun 18, 2022 10:42 am

error: invalid arch-dependent ELF magic when using grub

Post by anthony »

i was trying to follow the bare bones tutorial with c++
i think the problem might be with my makefile so im gonna put it here

Code: Select all

C_SOURCES = $(wildcard kernel/*.cpp libk/*.cpp libc/*.cpp)
HEADERS = $(wildcard kernel/*.hpp libk/*.hpp libc/*.hpp)
OBJ = ${C_SOURCES:.cpp=.o}

CC = i686-elf-g++
GDB = i686-elf-gdb
CFLAGS = -g -m32 -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs \
		 -Wall -Wextra -fcommon

run: iota.iso
	qemu-system-i386 -cdrom iota.iso
	
iota.iso: kernel.bin
	cp kernel.bin isodir/boot/kernel.bin
	grub-mkrescue -o iota.iso isodir

kernel.bin: boot.o ${OBJ}
	i686-elf-ld -o $@ -T linker.ld $^ --oformat binary

boot.o: boot.s
	i686-elf-as boot.s -o boot.o

%.o: %.cpp ${HEADERS}
	${CC} ${CFLAGS} -ffreestanding -c $< -o $@

clean:
	rm ${OBJ} iota.iso kernel.bin
i tried copying the boot.s code from the wiki page but it still keeps giving me the error
it might also be the c++ code so im gonna put that there too

Code: Select all

//
//  kernel.cpp
//  iota
//
//  Created by Anthony Valdes on 2022-06-17.
//

#include "kernel.hpp"

extern "C" void kmain(void) {
    
}
the kernel.hpp file is empty apart from an include guard and the comment at the start
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: error: invalid arch-dependent ELF magic when using grub

Post by klange »

Code: Select all

kernel.bin: boot.o ${OBJ}
   i686-elf-ld -o $@ -T linker.ld $^ --oformat binary
If you're using the boot.s from the Bare Bones template on the wiki, it does not set up the Multiboot header with necessary information for making a flat binary readable by Grub - its Makefile builds an ELF binary, but your use of --oformat binary produces a raw binary. If this is what you want to do, you'll need to adjust the header you build to set the appropriate flag to tell Grub the address section of the Multiboot header is valid, and to set the appropriate values in it. You can read more about those entries in the header here: https://www.gnu.org/software/grub/manua ... ess-fields

Additionally, you should not set CC to a C++ compiler in a Makefile, and not all C sources will compile as C++, so be careful with that.
anthony
Posts: 9
Joined: Sat Jun 18, 2022 10:42 am

Re: error: invalid arch-dependent ELF magic when using grub

Post by anthony »

thanks i noticed that mistake before so i removed it and changed my makefile to use c instead of c++
Post Reply