Page 1 of 1

Over sized kernel.bin after linking with -Ttext 0x1000

Posted: Sun Apr 26, 2020 3:40 am
by alexey
I got an over sized binary file (around 129M) after linking with parameter -Ttext0x1000
ld -melf_i386 -o kernel.bin -Ttext 0x1000 kernel.o --oformat binary
(The kernel.o is only 2K)

But after removing the -Ttext0x1000, by just using:
ld -melf_i386 -o kernel.bin kernel.o --oformat binary
The size of kernel.bin will be only around 8K

Any idea about that? need some configs for linking? thanks a lot.

Environment:
WSL2 / Ubuntu
ld version: GNU ld (GNU Binutils for Ubuntu) 2.33

Re: Over sized kernel.bin after linking with -Ttext 0x1000

Posted: Tue Apr 28, 2020 7:33 am
by Octocontrabass
You're only telling the linker where to put the text section. Your binary has other sections, and you need to tell the linker where all of them should go. I suggest writing a complete linker script instead of passing individual command line parameters. The linker script on this page should give you some ideas, although it's meant for a format like ELF instead of a flat binary.

Re: Over sized kernel.bin after linking with -Ttext 0x1000

Posted: Tue Apr 28, 2020 11:08 am
by gungomanj
Could you provide more info on the compilation steps? my first guesses are that you did not add -nostdlib.

Re: Over sized kernel.bin after linking with -Ttext 0x1000

Posted: Tue Apr 28, 2020 11:29 am
by zaval
i write from the inconvenient tablet, can't check, there is an option something like nmagic - if you put yiur sections well aligned, elf gets bloated, since it doesn't have file alignment page alignment notion. setting that nmagic thing makes fatty elf skinnier, search it in the gcc help.