1

GLIBC なしでコンパイルするときに main() から値を返そうとしていますが、機能しません。インターネットで見つけたこの例を見てみましょう:

[niko@localhost tests]$ cat stubstart.S 
.globl _start

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

    return(90);

}
[niko@localhost tests]$ gcc -nostdlib stubstart.S -o m m.c 
[niko@localhost tests]$ ./m
[niko@localhost tests]$ echo $?
0
[niko@localhost tests]$ 

ここで、GLIBC でコンパイルすると、戻り値が正常に取得されます。

[niko@localhost tests]$ gcc -o mglibc m.c
[niko@localhost tests]$ ./mglibc 
[niko@localhost tests]$ echo $?
90
[niko@localhost tests]$ 

どうやら stubstart.S で正しく返されていません。どうすれば正しくなりますか? (Linux のみ)

4

1 に答える 1

4

main()の戻り値を に提供していないためです_exit()

そのようにすると:

.globl _start

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

eaxからの戻り値をebx、終了コードが期待される場所に保存します。

于 2013-06-01T18:49:52.997 に答える