How to convert Linux kernel Bin into ELF format

13,530

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
Share:
13,530
Amit Singh Tomar
Author by

Amit Singh Tomar

SOreadytohelp

Updated on June 09, 2022

Comments

  • Amit Singh Tomar
    Amit Singh Tomar about 2 years

    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?

  • Amit Singh Tomar
    Amit Singh Tomar over 11 years
    Thanks Davidg for your response ,We are able to create a elf out of bin file with particular entry address but while compiling Our IFS image our tool "mkxfs" says not able to find linker for this particular elf file .Is there any option we can give in linker script for proper linking??
  • davidg
    davidg over 11 years
    Could you provide the exact error message you are getting? Also, what architecture are you building for? You need to ensure that you use the version of objcopy and the linker that comes with your toolchain.
  • davidg
    davidg over 11 years
    I am not sure, sorry @AmitSinghTomar. Perhaps it is worth creating a new question with the full details of the error so others can try to solve it, or unaccepting this answer so others know that it doesn't fully answer your problem?