将Linux x86-64程序集hello world程序与ld链接失败 [英] Linking Linux x86-64 assembly hello world program with ld fails

查看:49
本文介绍了将Linux x86-64程序集hello world程序与ld链接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在玩弄Linux上的x86 64位汇编,并且在编译了一个看似简单的程序之后,我挠头了:P

I've been toying around with x86 64 bit assembly on linux recently and after compiling a seemingly simple program I am left scratching my head :P

尽管我编译并链接它不会引发任何错误并生成linux ELF当我尝试运行它时,我得到:

Although I compile and link it throws no errors and produces a linux ELF When i try to run it I get:

.:[ h4unt3r@sp3ctr4l-h0st asm ]:.
#(0)> ./hello 
bash: ./hello: No such file or directory

我认为它会生成一个无效的ELF文件,这就是为什么即使存在它也会报告hello的原因.不知道为什么-我可能会继续研究它,只是好奇是否可以通过简单的方法解决^ _ ^

I assume its producing an invalid ELF file which is why it reports hello is not there even though it IS. Not sure why-- I'll probably keep playing around with it, just curious if this can be solved the easy way ^_^

这是我的编译/链接命令行:

Here is my compile / link command line:

nasm -f elf64 hello.s -g
ld -o hello hello.o -lc

代码如下:

section .data
    msg: db "Hello, world!",0xa,0

section .text
    extern printf
    global main

main:
    push rbp
    mov rbp, rsp

    mov rdi, msg
    xor rax, rax
    call printf
    xor rax, rax

    pop rbp
    ret

编辑-我不想使用gcc:)

Edit -- I do not want to use gcc :)

推荐答案

我正在运行32位硬件,无法测试64位硬件.我已经在32位代码中看到此无此类文件"错误.ld默认情况下使用"/lib/ld-linux.so.1"-您可以在可执行文件中以纯文本形式看到此字符串.这是不存在的文件(显然"hello"就在这里!).解决方案是告诉ld -I/lib/ld-linux.so.2 .我怀疑类似的解决方案适用于64位,但是我不知道您需要什么解释器"或动态链接器".尝试在可执行文件中查找相似的字符串,然后在您的库中查找相似的.so.您不应该需要"使用gcc ...但是gcc知道在哪里可以找到这些东西!可能更容易使用.令人困惑的错误,不是吗?

I am running 32-bit hardware, and can't test 64-bit stuff. I have seen this "no such file" error in 32-bit code. ld is, by default, using "/lib/ld-linux.so.1" - you can see this string in plain text in the executable. This is the file that doesn't exist (obviously "hello" is right there!). The solution is to tell ld -I/lib/ld-linux.so.2. I suspect that a similar solution would work for 64-bit, but I don't know what "interpreter" or "dynamic linker" you need. Try lookng in the executable for a similar string, and looking in your libs for a similar .so. You shouldn't "need" to use gcc... but gcc knows where to find this stuff! May be easier to use it. Heck of a confusing error, isn't it?

(如果您要这样做,我希望您的入口点是 _start ,而不是 main .您将无法进行退出-使用sys_exit或exit().)

(I would expect your entrypoint to be _start, not main if you're going to do it this way. You won't be able to ret from this - use sys_exit or exit().)

我不熟悉nrz提到的有关符号表0"的错误.当然不是故意更改Nasm!Nasm开发人员可以在 http://www.nasm.us 上或周围闲逛,很高兴听到反馈并错误报告.(好吧,也许对错误报告不是高兴".)我将看看是否能找到任何东西……

I'm not familiar with the error nrz mentions about "symbol table 0". Surely not a deliberate change to Nasm! The Nasm developers hang out on or around http://www.nasm.us and would be delighted to hear feedback and bug reports there. (Okay, not "delighted" about bug reports, perhaps.) I'll see if I can find out anything...

FWIW,Nasm仅使用 -g 开关默认为刺破"调试信息.要启用矮人"调试信息,请 -F矮人 ...应该可以更好地工作...

FWIW, Nasm defaults to "stabs" debugging info with just the -g switch. To enable "dwarf" debugging info, -F dwarf... Supposed to work better...

这篇关于将Linux x86-64程序集hello world程序与ld链接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆