Linux bin to elf

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?

1 Answer 1

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:

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 

Источник

unixforum.org

Нужно сделать полноценный ELF файл с нужными секциями для отладчика/дизасемблера. Не знаю как наверно через ассемблерный код с описанием секций но как вставить бинарный код вместо асм кода и где как указывать адрес загрузки адреса функций. Описание формата ELF смотрел, не помогло.

Читайте также:  Браузер chromium linux mint

Re: [решено] Как собрать ELF из бинарного кода?

Есть скомпилированный бинарный код
Есть адрес в памяти куда он грузится и откуда стартует
скажем .init : 0xc0008000 (кернел код)
Есть адреса и названия функций в этом коде

Нужно сделать полноценный ELF файл с нужными секциями для отладчика/дизасемблера. Не знаю как наверно через ассемблерный код с описанием секций но как вставить бинарный код вместо асм кода и где как указывать адрес загрузки адреса функций. Описание формата ELF смотрел, не помогло.

Re: [решено] Как собрать ELF из бинарного кода?

Сообщение max100 » 05.08.2013 17:36

а. это не правильно
б. это не возможно

правильное решение явно есть

serzh-z Бывший модератор Сообщения: 8256 Статус: Маньяк ОС: Arch, Fedora, Ubuntu Контактная информация:

Re: [решено] Как собрать ELF из бинарного кода?

Сообщение serzh-z » 05.08.2013 19:25

Задача звучит как «Есть варённое мясо, как сделать из него борщ?». Скомпилированный во что? Если это машинный код, есть информация о выравнивании, наборе используемых инструкций и архитектуре, базовом адресе, относительно которого делаются переходы и нет ссылок на внешние символы, то можно разобраться со структурой раздела ELF, прикрутить к этому коду заголовки, а потом слинковать его с загрузчиком типа ld-linux.so, натравив на него ld.

Можно, в конце концов, сделать приложение, которое будет запускаться, загружать этот код в память по адресу 0xc0008000 и просто передать управление на начало этого кода. Что-то типа:

int f; void *a; f = open("binary", O_RDONLY); a = mmap(0xc0008000, len, PROT_READ | PROT_EXEC, MAP_PRIVATE, f, 0); __asm__ ("jmp " . )

Если же этот код ссылается на какие-то внешние функции, то нужно, после загрузки, настроить таблицу переходов, если она есть. Опять же, нужно знать во что этот код скомпилирован.

Источник

MCU on Eclipse

Everything on Eclipse, Microcontrollers and Software

MCU on Eclipse

Converting a Raw Binary File into an ELF/Dwarf File for Loading and Debugging

Binary files are just a binary blob without debug information. Most debug tools and flashers are able to deal (raw) binary (see “S-Record, Intel Hex and Binary Files“). But GDB or the P&E GDB server really needs a ELF/Dwarf file which usually has all the debug information in it. This is a problem if all what I have is a binary file.

Читайте также:  Linux on sd card speed

This post is about transforming a raw binary (.bin) file into an ELF/Dwarf file with adding a header to it:

Added Elf Header to Raw Binary File

Added Elf Header to Raw Binary File

I can make a elf file out of a bin (raw binary) file with the GNU ‘objcopy‘ command like this:

arm-none-eabi-objcopy.exe --input-target=binary --output-target=elf32-little myApp.bin myApp.bin.elf

‘myApp.bin’ is the application binary file and it adds an ELF/Dwarf header file to the output file myApp.bin.elf. I can inspect/verify with the ‘readelf’ GNU program:

arm-none-eabi-readelf.exe -a myApp.bin.elf
ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: REL (Relocatable file) Machine: None Version: 0x1 Entry point address: 0x0 Start of program headers: 0 (bytes into file) Start of section headers: 26956 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 0 (bytes) Number of program headers: 0 Size of section headers: 40 (bytes) Number of section headers: 5 Section header string table index: 2 Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .data PROGBITS 00000000 000034 0068f4 00 WA 0 0 1 [ 2] .shstrtab STRTAB 00000000 006928 000021 00 0 0 1 [ 3] .symtab SYMTAB 00000000 006a14 000050 10 4 2 4 [ 4] .strtab STRTAB 00000000 006a64 000058 00 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific)

Now I can use that file like any normal ELF/Dwarf file (of course it does not have debug information in it), e.g. see Using Eclipse to Program Binary Files to an Embedded Target.

Источник

Converting a Raw Binary File Into an ELF/Dwarf File

Join the DZone community and get the full member experience.

Binary files are just a binary blob without debug information. Most debug tools and flashers are able to deal (raw) binary (see S-Record, Intel Hex, and Binary Files). But GDB and/or the P&E GDB server really needs an ELF/Dwarf file, which usually has all the debug information in it. This is a problem if all that I have is a binary file.

Читайте также:  Linux find running time

This post is about transforming a raw binary (.bin) file into an ELF/Dwarf file and adding a header to it:

Added Elf Header to Raw Binary File

Added ELF header to the raw binary file

I can make an ELF file out of a bin (raw binary) file with the GNU ‘objcopy‘ command like this:

arm-none-eabi-objcopy.exe --input-target=binary --output-target=elf32-little myApp.bin myApp.bin.elf

‘ myApp.bin ’ is the application binary file, and it adds an ELF/Dwarf header file to the output file myApp.bin.elf . I can inspect/verify that with the ‘readelf’ GNU program:

arm-none-eabi-readelf.exe -a myApp.bin.elf
ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: REL (Relocatable file) Machine: None Version: 0x1 Entry point address: 0x0 Start of program headers: 0 (bytes into file) Start of section headers: 26956 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 0 (bytes) Number of program headers: 0 Size of section headers: 40 (bytes) Number of section headers: 5 Section header string table index: 2 Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .data PROGBITS 00000000 000034 0068f4 00 WA 0 0 1 [ 2] .shstrtab STRTAB 00000000 006928 000021 00 0 0 1 [ 3] .symtab SYMTAB 00000000 006a14 000050 10 4 2 4 [ 4] .strtab STRTAB 00000000 006a64 000058 00 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific)

Now I can use that file like any normal ELF/Dwarf file (of course, it does not have debug information in it), e.g. see Using Eclipse to Program Binary Files to an Embedded Target.

Published at DZone with permission of Erich Styger , DZone MVB . See the original article here.

Opinions expressed by DZone contributors are their own.

Источник

Оцените статью
Adblock
detector