Page 1 of 1
GCC/LD padding binary with 0's?
Posted: Wed Dec 31, 2008 6:51 am
by furryfreak
For some reason a binary compiled and linked with gcc and ld is being padded with zero's, making a file that should only be about 2kb into a 2mb file.
This is is a problem I've had before, but last time I managed to fix it by making all global variables initialized (e.g. "long x = 0;" instead of "long x;"), unfortunately, that doesn't seem to have worked this time.
any one know why this might be happening?
Re: GCC/LD padding binary with 0's?
Posted: Wed Dec 31, 2008 8:19 am
by Combuster
Because you are probably telling the linker to have the code start at 1M, and you don't tell it that the file starts at 1M as well.
Your linker script?
Re: GCC/LD padding binary with 0's?
Posted: Wed Dec 31, 2008 8:35 am
by furryfreak
Your linker script?
I've never bothered with linker scripts, I just use a shell script which is as follows:
Code: Select all
#!/bin/sh
echo "./make: compiling temporary objects..."
mkdir tmp
gcc -I include -c -o tmp/boot.out boot/boot.S
gcc -I include -c -o tmp/init.out kernel/init.c
gcc -I include -c -o tmp/main.out kernel/main.c
echo "\n./make: linking objects..."
ld -nostdlib --oformat binary -o bin/boot.bin tmp/boot.out tmp/init.out tmp/main.out
echo "\n./make: cleaning up tempory files..."
rm tmp/*.*
rmdir tmp
I don't
think the problem has anything to do with the command-line switches, though there might be a switch that'd sort out the problem.
The thing is, this problem only occurred after rewriting most of my memory management code by replacing a bitmap with a table.
Re: GCC/LD padding binary with 0's?
Posted: Wed Dec 31, 2008 8:43 am
by Combuster
then you should start with writing a linker script. Now ld is free to put everything wherever it wants to (which is not what you want - and apparently it has some ideas of its own about the .bss section)
See
Bare bones for ideas
Re: GCC/LD padding binary with 0's?
Posted: Wed Dec 31, 2008 9:06 am
by furryfreak
Nice one, that seems to have done the trick.
One question though, are the "ALIGN(4096)"s really necessary? I've tried the script without them and the binary works fine and is almost half the size, Is there any reason that most example linker scripts do this?
Re: GCC/LD padding binary with 0's?
Posted: Wed Dec 31, 2008 9:08 am
by Combuster
It allows you to later apply no-execute and read-only protection on the kernel
Re: GCC/LD padding binary with 0's?
Posted: Thu Jan 01, 2009 4:16 am
by Love4Boobies
4096 is 4 KiB - the page granularity you're likely to use. Such options are set on a per-page basis.