【问题标题】:Assembly/Linking problem with nasm and ldnasm 和 ld 的装配/链接问题
【发布时间】:2011-01-15 20:20:23
【问题描述】:

我有一个用 nasm 编译的示例程序集文件:

nasm -f elf syscall.asm 

这会生成一个 syscall.o 文件。我尝试将它与 ld 链接:

ld -o syscall syscall.o

ld 命令失败并出现以下错误:

ld: i386 architecture of input file `syscall.o' is incompatible with i386:x86-64 output

但是,如果我这样做了

ld -o syscall syscall.o -melf_i386

命令成功,我得到一个 syscall 可执行文件。

发现 nasm 没有生成 x86-64 格式的目标代码,我在 syscall.asm 文件的开头添加了“BITS 64”指令。

然后尝试用 nasm 组装 syscall.asm 出现以下错误:

error: elf output format does not support 64-bit code

这似乎很奇怪,因为在我的终端上执行“file /usr/bin/nasm”会给出:

/usr/bin/nasm: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

我的 64 位 Fedora Core 11 安装了最新版本的 nasm,我的 CPU 是 Intel Core 2 Duo E7200。

[编辑]

我的问题是如何让 nasm 发出与 i386:x86-64 兼容的目标文件。

【问题讨论】:

  • 那么问题是什么?

标签: linux assembly linker x86-64 nasm


【解决方案1】:

尝试使用elf64 作为输出格式。

示例运行:

$ cat elf64.asm
section .text
        jmp [rax]
$ nasm -f elf64 elf64.asm
$ objdump -Sr elf64.o

elf64.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <.text>:
   0:   ff 20                   jmpq   *(%rax)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-20
    • 2011-07-09
    • 2011-01-18
    • 2021-12-04
    • 2010-10-22
    • 2016-05-18
    • 1970-01-01
    • 2010-12-18
    相关资源
    最近更新 更多