I'm using NASM version 2.14.02 and GNU ld 2.34 to compile an assembly file (for example, hello world) on a 64-bit Linux.
I would like (just for fun basically) to produce an executable file of the smallest possible size. However, in the executable file produced by the utilities, there are some strings that are definitely meaningless for the executable (like the name of the source file, section names, and some others). How do I get rid of them?
Here is what I do:
$ cat hello_world_32.s
SECTION .rodata
msg: db 'Hello world!',0xA
msg_len: equ $-msg
SECTION .text
global _start
_start:
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, msg_len
int 0x80
mov eax, 1
xor ebx, ebx
int 0x80
$ nasm -f elf32 -o hello_world.o hello_world_32.s
$ ld --nmagic -m elf_i386 -o hello_world hello_world.o
$ ./hello_world
Hello world!
$ grep hello_world_32.s hello_world
Binary file hello_world matches
$ grep .text hello_world
Binary file hello_world matches
$ grep .rodata hello_world
Binary file hello_world matches
$
Here is the output of xxd hello_world
:
00000000: 7f45 4c46 0101 0100 0000 0000 0000 0000 .ELF............
00000010: 0200 0300 0100 0000 6080 0408 3400 0000 ........`...4...
00000020: 9001 0000 0000 0000 3400 2000 0100 2800 ........4. ...(.
00000030: 0600 0500 0100 0000 6000 0000 6080 0408 ........`...`...
00000040: 6080 0408 2d00 0000 2d00 0000 0500 0000 `...-...-.......
00000050: 1000 0000 0000 0000 0000 0000 0000 0000 ................
00000060: b804 0000 00bb 0200 0000 b980 8004 08ba ................
00000070: 0d00 0000 cd80 b801 0000 0031 dbcd 8000 ...........1....
00000080: 4865 6c6c 6f20 776f 726c 6421 0a00 0000 Hello world!....
00000090: 0000 0000 0000 0000 0000 0000 0000 0000 ................
000000a0: 0000 0000 6080 0408 0000 0000 0300 0100 ....`...........
000000b0: 0000 0000 8080 0408 0000 0000 0300 0200 ................
000000c0: 0100 0000 0000 0000 0000 0000 0400 f1ff ................
000000d0: 1200 0000 8080 0408 0000 0000 0000 0200 ................
000000e0: 1600 0000 0d00 0000 0000 0000 0000 f1ff ................
000000f0: 2300 0000 6080 0408 0000 0000 1000 0100 #...`...........
00000100: 1e00 0000 8d90 0408 0000 0000 1000 0200 ................
00000110: 2a00 0000 8d90 0408 0000 0000 1000 0200 *...............
00000120: 3100 0000 9090 0408 0000 0000 1000 0200 1...............
00000130: 0068 656c 6c6f 5f77 6f72 6c64 5f33 322e .hello_world_32.
00000140: 7300 6d73 6700 6d73 675f 6c65 6e00 5f5f s.msg.msg_len.__
00000150: 6273 735f 7374 6172 7400 5f65 6461 7461 bss_start._edata
00000160: 005f 656e 6400 002e 7379 6d74 6162 002e ._end...symtab..
00000170: 7374 7274 6162 002e 7368 7374 7274 6162 strtab..shstrtab
00000180: 002e 7465 7874 002e 726f 6461 7461 0000 ..text..rodata..
00000190: 0000 0000 0000 0000 0000 0000 0000 0000 ................
000001a0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
000001b0: 0000 0000 0000 0000 1b00 0000 0100 0000 ................
000001c0: 0600 0000 6080 0408 6000 0000 1f00 0000 ....`...`.......
000001d0: 0000 0000 0000 0000 1000 0000 0000 0000 ................
000001e0: 2100 0000 0100 0000 0200 0000 8080 0408 !...............
000001f0: 8000 0000 0d00 0000 0000 0000 0000 0000 ................
00000200: 0400 0000 0000 0000 0100 0000 0200 0000 ................
00000210: 0000 0000 0000 0000 9000 0000 a000 0000 ................
00000220: 0400 0000 0600 0000 0400 0000 1000 0000 ................
00000230: 0900 0000 0300 0000 0000 0000 0000 0000 ................
00000240: 3001 0000 3600 0000 0000 0000 0000 0000 0...6...........
00000250: 0100 0000 0000 0000 1100 0000 0300 0000 ................
00000260: 0000 0000 0000 0000 6601 0000 2900 0000 ........f...)...
00000270: 0000 0000 0000 0000 0100 0000 0000 0000 ................
How do I get rid of the unneeded strings in the executable? Is there, probably, some way to only compile instructions, ignoring all the rest?