【问题标题】:Segmentaion Fault Assembly NASM [duplicate]分段故障组装 NASM [重复]
【发布时间】:2016-07-08 15:13:40
【问题描述】:

我有一个简单的 hello world 程序的分段错误。我目前正在运行 64 位 Ubuntu x86_64 arch.. uname-a:

Linux ubuntu 4.4.0-28-generic #47-Ubuntu SMP Fri Jun 24 10:09:13 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

这是我的代码:

section.data ;Constant
            msg:    db "Hello World!"
            msg_L:   equ $-msg  ; Current - msg1

section.bss ;Varialble

section.text ; Code
        global _start:

_start:
        mov eax,4
        mov ebx,1; Where to wrte it out. Terminal
        mov ecx, msg
        mov edx, msg_L
        int 80h

        mov eax, 1 ; EXIT COMMAND
        mov ebx,0 ; No Eror
        int 80h

我使用以下命令运行它: nasm -f elf64 first.asm ld -elf_x86_64 -o first first.o

结果我得到了常见的错误分段错误。这有什么问题吗?帮助将不胜感激!

编辑

试过了:

sudo apt-get install libc6-dev-i386
nasm -f elf32 first.asm
gcc -m32 first.o -o first

当我执行 gcc 时,它给了我:

first.o: In function `section.bss':
first.asm:(.text+0xc): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib32/crt1.o:(.text+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib32/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status

【问题讨论】:

标签: assembly segmentation-fault nasm


【解决方案1】:

您的 asm 提供的是 _start 符号,而不是 main

gcc 试图将其链接为 C 程序,因此它提供了自己的设置代码(带有自己的 _start 标签),并寻找 main 来启动您的代码。

要么将您的标签更改为 main 并使用类似 C 的完整初始化,要么使用 gcc -m32 first.o -o first -nostdlib 在链接期间省略 stdlib 启动代码。

【讨论】:

  • 你的效果太棒了!我要使用 -nostdlib
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
相关资源
最近更新 更多