0

Can yasm, ld, gcc exclude unnecessary parts? I expect gcc exclude it like in C/C++.

;testSize1.asm
    segment .text
    global  main
    extern  printf,scanf ;<== difference part
main:
    push    rbp
    mov     rbp,rsp
    sub     rsp,16

    mov     rax,2

    leave
    ret

;testSize2.asm
    segment .text
    global  main
    ;extern printf,scanf ;<== difference part
main:
    push    rbp
    mov     rbp,rsp
    sub     rsp,16

    mov     rax,2

    leave
    ret

I compile and link with these commands.

yasm -f elf64 -l testSize1.lst testSize1.asm    
gcc -o testSize2 testSize2.o

-rwxrwxr-x 1 joe joe 8562 Jun 26 15:20 testSize1
-rw-rw-r-- 1 joe joe  119 Jun 26 15:16 testSize1.asm
-rw-rw-r-- 1 joe joe  822 Jun 26 15:20 testSize1.lst
-rw-rw-r-- 1 joe joe  624 Jun 26 15:20 testSize1.o
-rwxrwxr-x 1 joe joe 8475 Jun 26 15:21 testSize2
-rw-rw-r-- 1 joe joe  120 Jun 26 15:20 testSize2.asm
-rw-rw-r-- 1 joe joe  627 Jun 26 15:20 testSize2.lst
-rw-rw-r-- 1 joe joe  560 Jun 26 15:20 testSize2.o

Size of testSize1.o is greater than testSize2.o .

SuperJOE
  • 73
  • 1
  • 8
  • 1
    The presence of the `extern` directive should make a difference: it creates an entry in the symbol table which is otherwise not present. – fuz Jun 26 '17 at 09:43
  • 1
    *"exclude unnecessary parts?"* How is the assembler to know that the part is "unnecessary"? You might want to link that object file into a binary that *does* use these parts. – Cody Gray - on strike Jun 26 '17 at 10:42
  • 1
    Don't expect assembly to work as C++ in the first place. Usually if you have to use assembly, you want full control over everything. If you want to exclude something, then exclude it, you don't want some "smart" tool interfering with your assembly, that would annoy the hell out of me. Smart tools are there for all the common cases, when you don't need assembly at all, enjoy them there. Besides that, with big effort it's possible to use that extern `printf` symbol without directly using it in the source, it would be task of "Halting problem" (NP complete) to decide if it can be removed safely. – Ped7g Jun 26 '17 at 12:28

0 Answers0