Page 1 of 1

Howto create small PE->BIN linked file with gcc+ld

Posted: Tue Oct 18, 2005 1:58 pm
by Trashey
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!

Re:Howto create small PE->BIN linked file with gcc+ld

Posted: Tue Oct 18, 2005 4:34 pm
by Kemp
phys = 0x4000
The entire thing is set to start at the 16kb mark is it not?

Re:Howto create small PE->BIN linked file with gcc+ld

Posted: Tue Oct 18, 2005 5:34 pm
by AR
Try creating a linkmap, also try adding the .rodata section to the .text section of the linkscript. ("*(.rodata*)")

Re:Howto create small PE->BIN linked file with gcc+ld

Posted: Tue Oct 18, 2005 5:34 pm
by Trashey
Kemp, the stub is linked to the virtual address of 0x4000, this has nothing to do with the size of the executable.
Phys is a linker script "variable" helper used in the followed calculation to tell the linker where to find the sections.

If you knew all that and I've misinterpreted your question then I'm sorry, and yes, the stub will be loaded in 0x4000.
(Still has nothing to do with the file size, which is 4188 bytes).

Re:Howto create small PE->BIN linked file with gcc+ld

Posted: Tue Oct 18, 2005 5:39 pm
by Trashey
Adding the section did not change the file size.

Isn't there any way to prevent such alignment?
The resulting binary has around 4k of zeros between the code and the data sections and nothing that I do eliminates it.

I can see the binary code, then 4k of zeros and then my strings. (data)

Thanks.

Re:Howto create small PE->BIN linked file with gcc+ld

Posted: Wed Oct 19, 2005 1:05 am
by Candy
Trashey wrote: Adding the section did not change the file size.

Isn't there any way to prevent such alignment?
The resulting binary has around 4k of zeros between the code and the data sections and nothing that I do eliminates it.

I can see the binary code, then 4k of zeros and then my strings. (data)

Thanks.
For ELF etc. you could use some form of omagic, qmagic or such, one of them packs the sections together.

You could also realise that it might be a significant amount now, where it'll stay below 4k all the time. You can however map it in directly with proper permissions since it's all page-sized. In the long run, you'll stop caring about that small wastage.

Or, try to make a compressor for it :)...

Re:Howto create small PE->BIN linked file with gcc+ld

Posted: Wed Oct 19, 2005 3:51 am
by Kemp
Eeek, sorry about my previous post. A couple of days with no sleep doesn't do much for your concentration, I kinda managed to post while still thinking about it, hence the completely random question.

My real question was meant to be, could it be the fact that you haven't included a rodata/rdata/etc section in there?