Home Ask Login Register

Developers Planet

Your answer is one click away!

ErwinM February 2016

How can i include a separate binary into an ELF executable

I am writing a hobby OS. I would like to include a small asm program into my main kernel elf that can serve as the first process to load.

I am having trouble getting this to work.

The program is initcode.s. I using the following Makefile (which I modified from the xv6 os source) for this task:

    $(AS) $(ASFLAGS) initcode.s -o initcode.o
    ld $(LDFLAGS) -N -e start -Ttext 0 -o initcode initcode.o
    objcopy --input binary --output elf32-i386 --binary-architecture i386 initcode.out initcode

kernel.elf: $(OBJECTS) initcode
    ld -T link.ld -melf_i386 $(OBJECTS) -o kernel.elf initcode

The main kernel compiles and links fine. objcopy also creates markers which enable me to find the binary from within the kernel code. However the contents of initcode is trashed, the contents does not resemble what the assembler step produced (initcode.out).

How can I achieve my goal: include initcode.s as a separate binary somewhere in my main kernel.elf with some markers generated so I can find it from within my kernel.


gerryg February 2016

I can think of 2 simple methods. There are certainly more complex methods if you prefer.

  1. Write a small utility to convert the initcode binary to an asm file (or even C file) containing a GAS data section. Then assemble that file and link it with your kernel. Your initcode will than appear as a variable in your kernel.


  1. Simply 'cat' the initcode binary to the end of your kernel elf after the link step. This method will depend on whether your loader is happy to support this.

Post Status

Asked in February 2016
Viewed 1,000 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life