Can I execute the `ret` command from code in _start on MacOS? Linux?

I am wondering if it is possible to return retfrom the entry point to the program.

NASM example:

section .text
global _start
_start:
ret

; Linux: nasm -f elf64 foo.asm -o foo.o && ld foo.o
; OS X:  nasm -f macho64 foo.asm -o foo.o && ld foo.o -lc -macosx_version_min 10.12.0 -e _start -o foo

ret prints the return address from the stack and goes to it.

But are the top stack bytes a valid return address at the program entry point, or do I need to call exit?

In addition, the above program does not perform the functions of OS X. Where does it return?

+4
source share
2 answers

MacOS Dynamic Executables

When you use MacOS and refer to:

ld foo.o -lc -macosx_version_min 10.12.0 -e _start -o foo

. _start , . C/++/ Objective-C, , -e. Apple Forking and Executing the Process :

Mach-O , . , , , . Xcode, /usr/lib/dyld, OS X.

execve, ​​ mach_header . , Mach-O , . ​​ , , .

, , ( ) . . /usr/lib/crt1.o. ++, Objective-C ,

_start. , , ret , _start, . . gobjdump -Dx foo, :

start address 0x0000000000000000

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000001  0000000000001fff  0000000000001fff  00000fff  2**0
                  CONTENTS, ALLOC, LOAD, CODE
SYMBOL TABLE:
0000000000001000 g       03 ABS    01 0010 __mh_execute_header
0000000000001fff g       0f SECT   01 0000 [.text] _start
0000000000000000 g       01 UND    00 0100 dyld_stub_binder

Disassembly of section .text:

0000000000001fff <_start>:
    1fff:       c3                      retq

, start address 0. 0 dyld_stub_binder. , C, _start. , main.


MacOS

static, , ret , . :

, , , .

dyld crt1.o. CRT= C , ++/ Objective-C, MacOS. , C/++/Objective-C , .

, -lc ( -lSystem) -static:

ld foo.o -macosx_version_min 10.12.0 -e _start -o foo -static

, . gobjdump -Dx foo

start address 0x0000000000001fff

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000001  0000000000001fff  0000000000001fff  00000fff  2**0
                  CONTENTS, ALLOC, LOAD, CODE
  1 LC_THREAD.x86_THREAD_STATE64.0 000000a8  0000000000000000  0000000000000000  00000198  2**0
                  CONTENTS
SYMBOL TABLE:
0000000000001000 g       03 ABS    01 0010 __mh_execute_header
0000000000001fff g       0f SECT   01 0000 [.text] _start

Disassembly of section .text:

0000000000001fff <_start>:
    1fff:       c3                      retq

, start_address 0x1fff. 0x1fff - (_start). .


Linux

Linux, , , . , ELF Linux article . , , , Linux C/++/ Objective-C MacOS.

Linux (ld.so) MacOS one (dynld) , MacOS C/++/ Objective-C crt1.o. crt1.o , -e ( - main). Linux , . .


FreeBSD ( MacOS), Linux - . 64- . 32- , 4 , 8.

enter image description here

, , , , . , main C/++. C, C main (argc, argv, envp).

fooobar.com/questions/1690738/..., , MacOS .

+10

, : Man-o - load LC_MAIN ( 10.7), DYLD LC_UNIXTHREAD. , ret , DYLD __mh_execute_header. . ret syscall API (64 , int 0x80 32 ), DLLD- C lib, (). LC_MAIN, LC_UNIXTHREAD, , ret segmentation fault.

+2

Source: https://habr.com/ru/post/1690736/


All Articles