Successive sys_write syscalls not working as expected, NASM bug on OS X?

后端 未结 1 1974
难免孤独
难免孤独 2020-11-27 08:21

I\'m trying to learn MacOS assembly using NASM and I can\'t get a trivial program to work. I\'m trying a variation of the \"Hello, World\" where the two words are independen

相关标签:
1条回答
  • 2020-11-27 09:01

    NASM 2.11.08 and 2.13.02+ have bugs with macho64 output. What you are observing seems to be something I saw specifically with 2.13.02+ recently when using absolute references. The final linked program has incorrect fixups applied so the reference to str2 is incorrect. The incorrect fixup causes us to print out memory that isn't str2.

    NASM has a bug report about this issue in their system. I have added a specific example of this failure based on the code in the question. Hopefully the NASM developers will be able to reproduce the failure and create a fix.

    Update: As of June 2018 my view is that there are enough recurring bugs and regressions in NASM that I do not recommend NASM at this point in time for Macho-64 development.


    Another recommendation I have for Macho-64 development is to use RIP relative addressing rather than absolute. RIP relative addressing is the default for 64-bit programs on later versions of MacOS.

    In NASM you can use the default rel directive in your file to change the default from absolute to RIP relative addresses. For this to work you will have to change from using mov register, variable to lea register, [variable] when trying to move the address of a variable to a register. Your revised code could look like:

    default rel
    
    %macro printString 2
        mov     rax, 0x2000004 ; write
        mov     rdi, 1 ; stdout
        lea     rsi, [%1]
        mov     rdx, %2
        syscall
    %endmacro
    
    global start
    
    section .text
    
    start:
        printString str1,str1.len
    
        printString str2,str2.len
    
        mov     rax, 0x2000001 ; exit
        mov     rdi, 0
        syscall
    
    
    section .data
    
    str1:   db      "Hello,",10
    .len:  equ       $ - str1
    
    str2:   db      "world",10
    .len:  equ       $ - str2
    
    0 讨论(0)
提交回复
热议问题