Page 1 of 1

GCC adding "junk" to end of binary

Posted: Fri Aug 27, 2010 11:52 am
by IanSeyler
Hello everyone,

I have been compiling some C programs for my OS and noticed GCC adding a bunch of 'junk' to the end of the executable files.

Code: Select all

int main()
{
        unsigned int i=1;
        i = i - 1;
        return i;
}
I compile it as so: gcc -c -m64 -nostdlib -nostartfiles -nodefaultlibs -O2 -o test.o test.c
and then link as: ld -T app.ld -o test.app test.o

The output is a flat binary but much larger than necessary.

Code: Select all

[root@baremetal-dev prime]# ndisasm -b 64 test.app 
00000000  31C0              xor eax,eax
00000002  C3                ret
00000003  0000              add [rax],al
00000005  0000              add [rax],al
00000007  001400            add [rax+rax],dl
0000000A  0000              add [rax],al
0000000C  0000              add [rax],al
0000000E  0000              add [rax],al
00000010  017A52            add [rdx+0x52],edi
00000013  0001              add [rcx],al
00000015  7810              js 0x27
00000017  011B              add [rbx],ebx
00000019  0C07              or al,0x7
0000001B  089001000014      or [rax+0x14000001],dl
00000021  0000              add [rax],al
00000023  001C00            add [rax+rax],bl
00000026  0000              add [rax],al
00000028  D8FF              fdivr st7
0000002A  FF                db 0xff
0000002B  FF03              inc dword [rbx]
0000002D  0000              add [rax],al
0000002F  0000              add [rax],al
00000031  0000              add [rax],al
00000033  0000              add [rax],al
00000035  0000              add [rax],al
00000037  00                db 0x00
[root@baremetal-dev prime]#
56 bytes when it only really needed the first 3. What is this extra code/data for?

Thanks,
-Ian

Re: GCC adding "junk" to end of binary

Posted: Fri Aug 27, 2010 12:37 pm
by skyking
It seems to be the .eh_frame section...

Re: GCC adding "junk" to end of binary

Posted: Fri Aug 27, 2010 1:52 pm
by IanSeyler
app.ld:

Code: Select all

OUTPUT_FORMAT("binary")
OUTPUT_ARCH("i386:x86-64")
ENTRY(main)
SECTIONS
{
    .text 0x0000000000200000 :
    {
        *(.text)
    }
    .data :
    {
        *(.data)
    }
    .bss :
    {
        *(.bss)
    }
}

Re: GCC adding "junk" to end of binary

Posted: Tue Aug 31, 2010 7:50 am
by IanSeyler
Yes, it appears to be the ".eh_frame" section. I couldn't find any good docs describing the DISCARD option in a linker script but objcopy seems to do the trick.

Code: Select all

gcc -c -m64 -nostdlib -nostartfiles -nodefaultlibs -O2 -o test.o test.c
objcopy --remove-section .eh_frame --remove-section .rel.eh_frame --remove-section .rela.eh_frame test.o test.o
ld -T app.ld -o test.app test.o
test.app compiles to a proper 3 byte binary now instead of 56 bytes. My "Hello World" app compiles to 556 bytes vs 1256 bytes as well.

Thanks,
-Ian

Re: GCC adding "junk" to end of binary

Posted: Tue Aug 31, 2010 8:55 am
by qw
LD Manual wrote:The special output section name `/DISCARD/' may be used to discard input sections. Any input sections which are assigned to an output section named `/DISCARD/' are not included in the output file.

Re: GCC adding "junk" to end of binary

Posted: Tue Aug 31, 2010 12:35 pm
by Jezze
Doing objdump -s on my kernel and programs didn't show that section. Is this x86-64 specific?

EDIT: I noticed OUTPUT_FORMAT("binary"). Thats probably why.

Re: GCC adding "junk" to end of binary

Posted: Tue Aug 31, 2010 12:46 pm
by fronty
IIRC .eh_frame contains information used when unwounding on exception. If you're not using C++ or tell the compiler to avoid exceptions like pest that could explain your lack of it.