【问题标题】:returning value from main() without glibc从没有 glibc 的 main() 返回值
【发布时间】:2013-06-01 18:39:38
【问题描述】:

在没有 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)

【问题讨论】:

    标签: c assembly


    【解决方案1】:

    因为你没有将main()的返回值提供给_exit()

    如果你这样做:

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

    您将返回值从eax 保存到ebx,它应该是退出代码。

    【讨论】:

      猜你喜欢
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      • 2014-08-06
      相关资源
      最近更新 更多