GCC adding "junk" to end of binary

Programming, for all ages and all languages.
Post Reply
User avatar
IanSeyler
Member
Member
Posts: 326
Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:

GCC adding "junk" to end of binary

Post 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
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: GCC adding "junk" to end of binary

Post by skyking »

It seems to be the .eh_frame section...
User avatar
IanSeyler
Member
Member
Posts: 326
Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:

Re: GCC adding "junk" to end of binary

Post 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)
    }
}
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
User avatar
IanSeyler
Member
Member
Posts: 326
Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:

Re: GCC adding "junk" to end of binary

Post 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
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: GCC adding "junk" to end of binary

Post 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.
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: GCC adding "junk" to end of binary

Post 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.
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
fronty
Member
Member
Posts: 188
Joined: Mon Jan 14, 2008 5:53 am
Location: Helsinki

Re: GCC adding "junk" to end of binary

Post 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.
Post Reply