Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Linux kernel Bin into ELF format

We have a Linux kernel binary which is without an ELF header, and our bootloader will be loading the kernel image (earlier QNX kernel image has the ELF header) based on a calculation coming from the ELF header, but since our Linux kernel image is without an ELF header, our bootloader is denying loading of this kernel image to memory.

For some reasons we don't have an option to alter our bootloader code, so the only option we have is to insert an ELF header into the Linux BIN file with a particular entry point.

What is the way to achieve it?

like image 690
Amit Singh Tomar Avatar asked Mar 08 '13 09:03

Amit Singh Tomar


1 Answers

The objcopy command is able to wrap a binary file with an appropriate ELF header. For example, the following command will convert the binary file input.in into an i386 object file output.o:

objcopy -I binary -O elf32-i386 --binary-architecture i386 input.bin output.o

Three symbols will be defined in output.o: _binary_input_bin_start, _binary_input_bin_end and _binary_input_bin_size. Additionally, that data of your input file will be in a .data section.

You will then need to use ld with a linker script to set the appropriate load/virtual/physical addresses and entry points. The following is a minimal script:

ENTRY(_start);

SECTIONS
{
    _start = 0x12000;
    . = 0x10000;
    .data : {
        *(.data)
    }
}

but will likely need to be heavily modified depending on precisely how your bootloader works, your physical memory layout, where you want to kernel located, your architecture, etc. Once tweaked, it can then be used to generate your final ELF file as follows:

ld -m elf_i386 output.o -T binary.ld -o output.elf
like image 108
davidg Avatar answered Dec 28 '22 11:12

davidg