a_random_programmer
a_random_programmer

Reputation: 148

Why does stat system call return 0 for st_size in NASM

I have been trying to use stat in NASM to get file sizes. However, st_size returns 0. Can anyone explain why this happens?

Here is my code:

global _main
extern _printf

section .bss
    stat resb 144

section .text
    filename:
        db "test.asm", 0   ; The name of this NASM file

    format:
        db "%lld", 10, 0

    _main:
        mov rax, 0x20000bc   ; system call for stat
        mov rdi, filename
        mov rsi, stat
        syscall   ; returns 0

        push rax
        mov rdi, format
        mov rsi, stat
        mov rsi, [rsi + 96]   ; the offset of st_size in __DARWIN_STRUCT_STAT64 as defined in <sys/stat.h> is 96
        call _printf
        pop rax

        ret

This is not a duplicate of Get file size with stat syscall

Upvotes: 0

Views: 204

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90521

You're using the wrong syscall. That's the one for backward compatibility with the 32-bit-sized structure. Of course, that means that the st_size field is not at the offset your code is expecting.

The stat() function's symbol name is not _stat, by default, since 10.6. Rather, it's _stat$INODE64. If you look at the assembly for that in /usr/lib/system/libsystem_kernel.dylib, you'll find that it uses the syscall value 0x2000152.

Upvotes: 1

Related Questions