GCC/LD padding binary with 0's?

Programming, for all ages and all languages.
Post Reply
User avatar
furryfreak
Member
Member
Posts: 28
Joined: Mon Nov 03, 2008 12:45 pm
Location: SW England

GCC/LD padding binary with 0's?

Post 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?
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: GCC/LD padding binary with 0's?

Post 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?
"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 ]
User avatar
furryfreak
Member
Member
Posts: 28
Joined: Mon Nov 03, 2008 12:45 pm
Location: SW England

Re: GCC/LD padding binary with 0's?

Post 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.
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: GCC/LD padding binary with 0's?

Post 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
"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 ]
User avatar
furryfreak
Member
Member
Posts: 28
Joined: Mon Nov 03, 2008 12:45 pm
Location: SW England

Re: GCC/LD padding binary with 0's?

Post 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?
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: GCC/LD padding binary with 0's?

Post by Combuster »

It allows you to later apply no-execute and read-only protection on the kernel
"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 ]
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: GCC/LD padding binary with 0's?

Post by Love4Boobies »

4096 is 4 KiB - the page granularity you're likely to use. Such options are set on a per-page basis.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply