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?
GCC/LD padding binary with 0's?
- furryfreak
- Member
- Posts: 28
- Joined: Mon Nov 03, 2008 12:45 pm
- Location: SW England
- Combuster
- 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?
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?
Your linker script?
- furryfreak
- Member
- Posts: 28
- Joined: Mon Nov 03, 2008 12:45 pm
- Location: SW England
Re: GCC/LD padding binary with 0's?
I've never bothered with linker scripts, I just use a shell script which is as follows:Your linker script?
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
The thing is, this problem only occurred after rewriting most of my memory management code by replacing a bitmap with a table.
- Combuster
- 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?
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
See Bare bones for ideas
- furryfreak
- Member
- Posts: 28
- Joined: Mon Nov 03, 2008 12:45 pm
- Location: SW England
Re: GCC/LD padding binary with 0's?
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?
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?
- Combuster
- 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?
It allows you to later apply no-execute and read-only protection on the kernel
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: GCC/LD padding binary with 0's?
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 ]
[ Project UDI ]