Why my code will crash at the end of the start fun

2019-07-15 18:20发布

问题:

Test platform is 32 bit Linux.

Basically, I did a modification to the asm code gcc generated to change the entry point from main to start as below:

asm code:

     .file   "test.c"
    .intel_syntax noprefix
    .section        .rodata
.LC0:
    .string "%d\n"
    .text
    .globl  start
    .type   start, @function
start:
    push    ebp
    mov     ebp, esp
    call    main
    mov     eax, 0
    leave
    ret
    .size   start, .-start

    .globl  main
    .type   main, @function
main:
    push    ebp
    mov     ebp, esp
    and     esp, -16
    sub     esp, 32
    mov     DWORD PTR [esp+28], 1
    mov     eax, OFFSET FLAT:.LC0
    mov     edx, DWORD PTR [esp+28]
    mov     DWORD PTR [esp+4], edx
    mov     DWORD PTR [esp], eax
    call    printf
    mov     eax, 0

Then I use these to compile and link:

 as test.s -g -o test.o
 ld -o test test.o -lc -dynamic-linker /lib/ld-linux.so.2 -e start

When debug using gdb, it can successfully work until the end of start function, then from the debug info it seems that $EIP don't know where to jump next, and segment fault occurs...

Could anyone give me some help on this issue..? Thank you a lot!

回答1:

you should call exit instead of set eax to 0 and return, because you don't use C main function(C-runtime), so nowhere to return.