Can I do `ret` instruction from code at _start in MacOS? Linux?

后端 未结 2 415
孤城傲影
孤城傲影 2020-12-20 21:36

I am wondering if it is legal to return with ret from a program\'s entry point.

Example with NASM:

section .text
global _start
_start:
r         


        
2条回答
  •  一向
    一向 (楼主)
    2020-12-20 22:17

    Suplementing what Michael Petch already answered: from runnable Mach-o executable perspective program launch happens either due to load command LC_MAIN (most modern executables since 10.7) which utilises DYLD in the process or backward compatible load command LC_UNIXTHREAD. The former is the variant where your ret is allowed and in fact preferable because you return control to DYLD __mh_execute_header. This will be followed by a buffer flush. Alternatively to ret you can use system exit call either through undocumented syscall kernel API (64bit, int 0x80 for 32bit) or DYLD wrapper C lib doing it(documented). If your executable is not utilising LC_MAIN you're left with legacy LC_UNIXTHREAD where you have no alternative to system exit call , ret will cause a segmentation fault.

提交回复
热议问题