Search code examples
cassemblygnu-assembler

How to print out symbols assigned via the .size directive in GNU Assembler


I have the following assembly code

.LC0:
    .string "%d\n"
    .text
    .globl  main
    .type   main, @function
main:
    leaq    .LC0(%rip), %rdi
    movl    $5, %esi
    movl    $0, %eax
    call    printf@PLT

Rather than print out 5, I want to print out the space between my current location and my first function (named func), I've been trying the following:

var1:
    .long .-func
.LC0:
    .string "%d\n"
    .text
    .globl  main
    .type   main, @function
main:
    leaq    .LC0(%rip), %rdi
    movl    var1, %esi  
    movl    $0, %eax
    call    printf@PLT

And I've tried

.size var1, .-func
.LC0:
    .string "%d\n"
    .text
    .globl  main
    .type   main, @function
main:
    leaq    .LC0(%rip), %rdi
    movl    var1, %esi  
    movl    $0, %eax
    call    printf@PLT

Nothing I've tried has worked so far, any help would be much appreciated.


Solution

  • Your attempt:

    var1:
        .long .-func
    

    will make an object whose value is the distance between it and func. As I understand your question, you want the distance between the point of the call in main and func. So something like:

    leaq    .LC0(%rip), %rdi
    movl    $.-func, %esi  
    movl    $0, %eax
    call    printf@PLT
    

    Instead of ., you could use a label for a particular instruction in main, if needed.

    The .size directive has nothing to do with what you want, although it's a common place you may have seen the .-symbol idiom. Its only purposes are debugging/disassembly assistance, making dladdr identify symbols that addresses belong to correctly, and (for data) making copy relocations work.