OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Nov 28, 2021 5:06 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: objcopy -O binary
PostPosted: Sat May 08, 2021 12:02 am 
Offline

Joined: Fri May 07, 2021 11:48 pm
Posts: 5
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.


Top
 Profile  
 
 Post subject: Re: objcopy -O binary
PostPosted: Sun May 09, 2021 9:42 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1090
For 32-bit ELF object files you may try to use Smaller C's linker, smlrl.

Code:
int add(int a, int b) { return a + b; }

int one = 1;
int two = 2;

void _start(void) { add(one, two); }


Compiling:
Code:
$ smlrcc -c -o bin.o bin.c
$ smlrl -flat32 -origin 0x80000 -o bin.bin bin.o


Disassembly:
Code:
$ 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.


Top
 Profile  
 
 Post subject: Re: objcopy -O binary
PostPosted: Sun May 09, 2021 10:12 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1132
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.

_________________
Thou hast outraged, not insulted me, sir; but for that I ask thee not to beware of Starbuck; thou wouldst but laugh; but let Ahab beware of Ahab; beware of thyself, old man.


Top
 Profile  
 
 Post subject: Re: objcopy -O binary
PostPosted: Mon May 10, 2021 6:26 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
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:
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:
    . = 0x80000;
    .text : { *(.text.boot) }
and in start.S:
Code:
.section ".text.boot"
_start:
(If you don't define otherwise in the linker script, the standard entry point is at label _start).

Cheers,
bzt


Top
 Profile  
 
 Post subject: Re: objcopy -O binary
PostPosted: Mon May 10, 2021 12:38 pm 
Offline

Joined: Fri May 07, 2021 11:48 pm
Posts: 5
It looks like I have nothing to do then. I already do it this way. Thank you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group