Thursday, 20 June 2013

The size of an executable

The following 3 programs show the size allocated for uninitialised data.

# sizetest1.s – A sample program to view the executable size
.section .text
.globl _start
_start:
 mov     r0, #0                 @ set exit code to 0
 mov     r7, #1                 @ set r7 to 1 - the syscall for exit
 swi     0                      @ then invoke the syscall from linux

This first program sets the exit code and exits. It is about as simple a program as possible.

# sizetest2.s - A sample program to view the executable size using .lcomm
.section .bss
   .lcomm buffer, 10000
.section .text
.globl _start
_start:
        mov     r0, #0          @ set exit code to 0
        mov     r7, #1          @ set r7 to 1 - the syscall for exit
        swi     0               @ then invoke the syscall from linux

We change the program by allocating 10000 bytes in buffer. This data buffer is allocated in the .bss section (.bss is the name of the uninitialised data section - for historical reasons the acronym bss comes from "block started by symbol").

# sizetest3.s - A sample program to view the executable size using .fill
.section .data
buffer:
   .fill 10000
.section .text
.globl _start
_start:
        mov     r0, #0          @ set exit code to 0
        mov     r7, #1          @ set r7 to 1 - the syscall for exit
        swi     0               @ then invoke the syscall from linux

We now use the .fill directive rather than the .lcomm directive - the 10000 bytes in buffer are now allocated in the .data section.

bob@poland:~/www/examples$make sizetest1 sizetest2 sizetest3
/usr/bin/as -gstabs -o sizetest1.o sizetest1.s
/usr/bin/ld -o sizetest1 sizetest1.o
/usr/bin/as -gstabs -o sizetest2.o sizetest2.s
/usr/bin/ld -o sizetest2 sizetest2.o
/usr/bin/as -gstabs -o sizetest3.o sizetest3.s
/usr/bin/ld -o sizetest3 sizetest3.o
bob@poland:~/www/examples$ls -l sizetest?
-rwxr-xr-x 1 bob bob   938 Jul  3 16:17 sizetest1
-rwxr-xr-x 1 bob bob  1072 Jul  3 16:17 sizetest2
-rwxr-xr-x 1 bob bob 11072 Jul  3 16:17 sizetest3
bob@poland:~/www/examples$

This shows clear the effect of the .lcomm and .fill as directives. .lcomm reserves space in the .bss (uninitialised data) section, but clearly does not initialise it at compile time, as .fill does, but does so at runtime.

The advantage of this is that the code size is much smaller.

No comments:

Post a Comment