【问题标题】:Why does stat system call return 0 for st_size in NASM为什么 stat 系统调用在 NASM 中为 st_size 返回 0
【发布时间】:2020-05-09 09:29:45
【问题描述】:

我一直在尝试使用 NASM 中的 stat 来获取文件大小。但是,st_size 返回 0。谁能解释为什么会发生这种情况?

这是我的代码:

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

这不是Get file size with stat syscall的重复项

【问题讨论】:

    标签: macos system-calls stat


    【解决方案1】:

    您使用了错误的系统调用。这是为了向后兼容 32 位大小的结构。当然,这意味着st_size 字段不在您的代码所期望的偏移量处。

    stat() 函数的符号名称不是 _stat,默认情况下,自 10.6 起。相反,它是_stat$INODE64。如果您查看/usr/lib/system/libsystem_kernel.dylib 中的程序集,您会发现它使用系统调用值0x2000152

    【讨论】:

    • 这行得通,但是我如何查看 dylib 的程序集? Objdump -d 不起作用。
    • 应该可以使用otool -tV。另外,尝试使用objdump-macho 选项来告诉它文件格式(即objdump -macho -d)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2012-08-29
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2010-11-06
    • 2014-01-23
    相关资源
    最近更新 更多