Page 1 of 1

ld linker makes huge files

Posted: Thu Feb 21, 2013 6:02 am
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!

Re: ld linker makes huge files

Posted: Thu Feb 21, 2013 6:26 am
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.

Re: ld linker makes huge files

Posted: Thu Feb 21, 2013 6:58 am
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.