Page 1 of 1
objcopy -O binary
Posted: Sat May 08, 2021 12:02 am
by 432
Hello,
I'm trying to convert ELF file to binary file. How do I do so if I want some symbol to be at the beginning of the file? I want entry to be at offset 0x0 of the file and then load the file at 0x80000.
Re: objcopy -O binary
Posted: Sun May 09, 2021 9:42 pm
by alexfru
For 32-bit ELF object files you may try to use Smaller C's linker, smlrl.
Code: Select all
int add(int a, int b) { return a + b; }
int one = 1;
int two = 2;
void _start(void) { add(one, two); }
Compiling:
Code: Select all
$ smlrcc -c -o bin.o bin.c
$ smlrl -flat32 -origin 0x80000 -o bin.bin bin.o
Disassembly:
Code: Select all
$ ndisasm -b32 -o0x80000 bin.bin
00080000 E916000000 jmp dword 0x8001b
00080005 CC int3
00080006 CC int3
00080007 CC int3
00080008 CC int3
00080009 CC int3
0008000A CC int3
0008000B CC int3
0008000C CC int3
0008000D CC int3
0008000E CC int3
0008000F CC int3
00080010 55 push ebp
00080011 89E5 mov ebp,esp
00080013 8B4508 mov eax,[ebp+0x8]
00080016 03450C add eax,[ebp+0xc]
00080019 C9 leave
0008001A C3 ret
0008001B 55 push ebp
0008001C 89E5 mov ebp,esp
0008001E FF3538000800 push dword [dword 0x80038]
00080024 FF3534000800 push dword [dword 0x80034]
0008002A E8E1FFFFFF call dword 0x80010
0008002F 83ECF8 sub esp,byte -0x8
00080032 C9 leave
00080033 C3 ret
00080034 0100 add [eax],eax
00080036 0000 add [eax],al
00080038 0200 add al,[eax]
0008003A 0000 add [eax],al
You don't have to use the entire compiler for this, just the linker. However, note that ELF object files produced by other compilers may not be fully supported.
Re: objcopy -O binary
Posted: Sun May 09, 2021 10:12 pm
by nullplan
My tip would be to put the entry symbol into a special section, and use a linker script to link that section to address 0x80000 and all other sections after it.
Re: objcopy -O binary
Posted: Mon May 10, 2021 6:26 am
by bzt
432 wrote:Hello,
I'm trying to convert ELF file to binary file. How do I do so if I want some symbol to be at the beginning of the file? I want entry to be at offset 0x0 of the file and then load the file at 0x80000.
See a working example
here
Code: Select all
objcopy -O binary kernel8.elf kernel8.img
You use a linker script to place the text segment and the entry point at the beginning of the raw image:
Code: Select all
. = 0x80000;
.text : { *(.text.boot) }
and in start.S:
(If you don't define otherwise in the linker script, the standard entry point is at label _start).
Cheers,
bzt
Re: objcopy -O binary
Posted: Mon May 10, 2021 12:38 pm
by 432
It looks like I have nothing to do then. I already do it this way. Thank you.