2013-06-01 47 views
1

我想在沒有GLIBC的情況下編譯時從main()返回一個值,但它不起作用。讓我們這個例子中,我的互聯網上找到:從main()返回沒有glibc的值

[[email protected] tests]$ cat stubstart.S 
.globl _start 

_start: 
    call main 
    movl $1, %eax 
    xorl %ebx, %ebx 
    int $0x80 
[[email protected] tests]$ cat m.c 
int main(int argc,char **argv) { 

    return(90); 

} 
[[email protected] tests]$ gcc -nostdlib stubstart.S -o m m.c 
[[email protected] tests]$ ./m 
[[email protected] tests]$ echo $? 
0 
[[email protected] tests]$ 

現在,如果我用glibc編譯我得到的返回值就好:

[[email protected] tests]$ gcc -o mglibc m.c 
[[email protected] tests]$ ./mglibc 
[[email protected] tests]$ echo $? 
90 
[[email protected] tests]$ 

所以,aparently返回不正確stubstart完成。 S,我該怎麼做纔對? (僅適用於Linux)

回答

4

因爲您不提供main()的返回值爲_exit()

如果你這樣做的:

.globl _start 

_start: 
    call main 
    movl %eax, %ebx 
    movl $1, %eax 
    int $0x80 

你的返回值保存從eaxebx,在退出代碼它預計。