ld linker makes huge files

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
blastrock
Posts: 2
Joined: Thu Feb 21, 2013 5:55 am

ld linker makes huge files

Post by blastrock »

Hi!

I'm am trying to link an ELF64 kernel but I have some trouble understanding how ld works.

I have a file named a.asm:

Code: Select all

dd 0x12345678
which I assemble with:

Code: Select all

nasm a.asm -o a.o -felf64
This gives me a file a.o of 544 bytes. Now i try to link it into an executable. I create an empty file l.ld and run:

Code: Select all

ld -Tl.ld a.o -o o
This gives me an ELF64 file, but the size is more than 2MB! I can see that my a.o file was mapped at 0x0200000 in the file and everything before that (except the ELF header) is full of 0. Why is ld doing that?!

Also, if I compile the asm file with -felf32 and link with -melf_i386, I get a 4.3KB file (still with a lot of 0 in it, but better than 2MB).

And how this is related to my problem: I need to put a multiboot header in the first 32KB of my file (as the spec says) and ld keeps putting it at 1MB or 2MB whatever I tell it :(

Any help is appreciated!
blastrock
Posts: 2
Joined: Thu Feb 21, 2013 5:55 am

Re: ld linker makes huge files

Post by blastrock »

Ok, I'm answering to my own question here: I needed -z max-page-size=4096 when linking. For some reason ld keeps the first page empty and by default with ELF64 max-page-size seems to be 2MB.

I was wondering, would it hurt to link a kernel with something like 128 for max-page-size and specifying ALIGN()s in the linker script? This would make that 0 padding at the beginning of the file smaller.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: ld linker makes huge files

Post by Combuster »

2MB is meant to make memory mapped files map directly to large pages, same with 4096 and default pages. Other values typically mean you'll have little chance to save yourself copies to align the file to the paging structure.

Then again, you might not need to care at first.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply