OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 1:07 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Binary linked from object files explodes in size
PostPosted: Tue Aug 17, 2021 6:27 am 
Offline

Joined: Mon Aug 16, 2021 3:58 am
Posts: 3
Hey guys, i am writing a small scale OS from scratch, somewhat inspired from Here

I have the following c code and nasm code
Code:
void main()
{
char* VID = (char*)0xb8000;
*VID = 'X';
}

and
Code:
[bits 32]
[extern main]
    call main
    jmp $

I compile these with the following commands
Code:
nasm -f elf32 -o kernel_entry.o kernel_entry.asm
gcc -ffreestanding -fno-pie -m32 -c kernel.c -o kernel.o

and i link these together with the following command
Code:
ld kernel_entry.o kernel.o -o kernel.bin -m elf_i386 -Ttext 0x1000 --oformat binary


which i later cat with my bootsector binary to obtain a qemu operatable binary
The sizes of the individual object files is very less < 1 kb
But after linking, the binaries explode to 135+MBs
The huge binary works, but it still feels weird

I did an ndisasm of the kernel.bin file and i found a lot of space filled with
Code:
0000009E  6A0C              push byte +0xc
000000A0  FF75FC            push dword [ebp-0x4]
000000A3  FF75F4            push dword [ebp-0xc]
000000A6  E894FFFFFF        call 0x3f
000000AB  83C40C            add esp,byte +0xc
000000AE  90                nop
000000AF  C9                leave
000000B0  C3                ret
000000B1  0000              add [eax],al
000000B3  0000              add [eax],al
000000B5  0000              add [eax],al
000000B7  0000              add [eax],al
000000B9  0000              add [eax],al
000000BB  0000              add [eax],al
000000BD  0000              add [eax],al
000000BF  0000              add [eax],al
000000C1  0000              add [eax],al
000000C3  0000              add [eax],al
000000C5  0000              add [eax],al

and such padding type instructions filled the whole way.
I would like to know whats going on here and any ways to fix it.
Found a similar problem on stack overflow and heres the link here


Top
 Profile  
 
 Post subject: Re: Binary linked from object files explodes in size
PostPosted: Thu Aug 19, 2021 6:16 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
One of the sections in your binary has a very high address. When you tell the linker to turn it into a flat binary, the linker has to insert padding so it ends up at the expected address. You can use objdump or readelf to see exactly what it is.

You should be using a cross-compiler.


Top
 Profile  
 
 Post subject: Re: Binary linked from object files explodes in size
PostPosted: Thu Aug 19, 2021 10:13 pm 
Offline

Joined: Mon Aug 16, 2021 3:58 am
Posts: 3
I fixed the exploding binary issue, kinda by using a cross compiler and linking with that, its a 32bit cross compiler and it does its job well of linking and the obtained binary is also of reasonable size.

Another issue which has come up is the qemu os image works only in fda mode and not in hda mode, which the earlier huge binary file could work in.
The value left in the ah register after error in disk read is printed onto the screen

Attachment:
Screenshot from 2021-08-20 09-42-12.png
Screenshot from 2021-08-20 09-42-12.png [ 7.41 KiB | Viewed 3071 times ]


Top
 Profile  
 
 Post subject: Re: Binary linked from object files explodes in size
PostPosted: Fri Aug 20, 2021 6:14 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
Was DL clobbered before reading the sector? Hard disk 0 typically has a drive number of 80h. Also, I would recommend using LBA BIOS functions, as they are a lot easier to work with in the long run then those CHS functions.

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: Binary linked from object files explodes in size
PostPosted: Fri Aug 20, 2021 8:49 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
Are you sure "0x7<<:" is the right error code?


Top
 Profile  
 
 Post subject: Re: Binary linked from object files explodes in size
PostPosted: Fri Aug 20, 2021 6:14 pm 
Offline

Joined: Mon Aug 16, 2021 3:58 am
Posts: 3
I managed to fix the issue of it running only in fda mode. The issue was that the os binary generated was too small for the sectors to read apparently, resizing the os image to a minimum of 20k fixed it, heres the solution i found on stack overflow HERE


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], FrankRay78, Google [Bot] and 51 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