Howto create small PE->BIN linked file with gcc+ld
Posted: Tue Oct 18, 2005 1:58 pm
Hello,
For my OS I have a small loader stub which just arranges DLL sections in memory (basically simple memcpys).
The stub has one asm setup file (compiled using nasmw) and the rest is c code (compiled using gcc).
When linking with LD (the format is pe-i386 and later I strip it to binary using objcopy -S -O binary) the smallest resulting BIN file I am getting is around 4k which consists of mostly ZEROs.
This is WAY too much for such small stub, I have no global variables except a stack that sits in the BSS and changing its size does not change the resulting BIN file size.
I have looked into GCC and LD flags but could not find anything that might help, the linker seems to automatically PAD stuff and changing things like section alignment did not help.
I was wondering if anyone can tell me how can I make the resulting BIN smaller, the current flags are:
GCC: -masm=intel -fomit-frame-pointer -fno-inline-functions -nostdinc -fno-builtin
LD script:
OUTPUT_FORMAT("pe-i386")
ENTRY(StubStart)
phys = 0x4000;
SECTIONS
{
.text phys : AT(phys) {
code = .;
*(.text)
. = ALIGN(4);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
*(COMMON)
. = ALIGN(4);
}
end = .;
_stub__end = .;
}
Thanks!
For my OS I have a small loader stub which just arranges DLL sections in memory (basically simple memcpys).
The stub has one asm setup file (compiled using nasmw) and the rest is c code (compiled using gcc).
When linking with LD (the format is pe-i386 and later I strip it to binary using objcopy -S -O binary) the smallest resulting BIN file I am getting is around 4k which consists of mostly ZEROs.
This is WAY too much for such small stub, I have no global variables except a stack that sits in the BSS and changing its size does not change the resulting BIN file size.
I have looked into GCC and LD flags but could not find anything that might help, the linker seems to automatically PAD stuff and changing things like section alignment did not help.
I was wondering if anyone can tell me how can I make the resulting BIN smaller, the current flags are:
GCC: -masm=intel -fomit-frame-pointer -fno-inline-functions -nostdinc -fno-builtin
LD script:
OUTPUT_FORMAT("pe-i386")
ENTRY(StubStart)
phys = 0x4000;
SECTIONS
{
.text phys : AT(phys) {
code = .;
*(.text)
. = ALIGN(4);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
*(COMMON)
. = ALIGN(4);
}
end = .;
_stub__end = .;
}
Thanks!