【问题标题】:GNU ld message: "error adding symbols: file in wrong format assembly language"GNU ld 消息:“错误添加符号:文件格式错误的汇编语言”
【发布时间】:2021-07-24 03:10:41
【问题描述】:

我正在尝试运行一个打印 hello world 的简单汇编代码

global _start

section .text
_start:
    ;printing hello world
    mov rax,1
    mov rdi,1
    mov rsi,texta
    mov rdx,11
    syscall
    ;exiting
    mov rax,60
    mov rdi,1
    syscall

section .data

    texta: db 'Hello world'

我是用 nasm 组装的

root@localhost:~# nasm -f elf64 do.asm -o do.o

但是当我尝试编译/运行它时,它会显示错误

root@localhost:~# ld do.o -o do
ld: do.o: Relocations in 
generic ELF (EM: 62)
ld: do.o: error adding 
symbols: file in wrong format

任何解决方法 我在 Ubuntu-in-termux 中运行它

我的系统信息:

提前致谢

请解决

【问题讨论】:

  • AArch64 Android ld 无法链接 x86-64 对象文件。您也不能在 AArch64 内核上本地运行 x86-64 机器代码二进制文件,只能通过 qemu 或其他一些仿真。要么使用 AArch64 汇编语言编写(并使用 as 而不是 NASM 进行汇编),要么使用 x86-64 跨工具链和模拟器。

标签: android ubuntu assembly nasm termux


【解决方案1】:

但是当我尝试编译/运行它时,它会显示错误

如果我理解正确,屏幕截图会显示目标设备(您为其生成代码的设备)。

您的汇编代码适用于 64 位 x86 CPU,但您的 Android 设备使用的是 ARM CPU。

您不能在 ARM 设备上运行 x86 CPU 的汇编代码。

你必须使用 ARM 的汇编器并编写 ARM 汇编代码 - 可能像这样,在 hello.S 中

.section .rodata
    texta: .ascii "Hello world"
    texta_len = . - texta         @ define assemble-time constant length

#include <asm/unistd.h>          @ only includes #define so can be included in a .S
.text
.global _start
_start:
  @ Printing hello world
    ldr r0, =1
    ldr r1, =texta            @ symbol address
    ldr r2, =texta_len        @ value of the assemble-time constant
    ldr r7, =__NR_write       @ call number from <asm/unistd.h>
    svc #0                    @ write(1, texta, len)

  @@ And exiting
    mov  r0, #0
    ldr  r7, =__NR_exit
    svc  #0                   @ exit(0)

What is the interface for ARM system calls and where is it defined in the Linux kernel?

用于 ARM 的 GAS(GNU 汇编器)使用 @ 作为注释字符,就像在 ARM 手册中一样。

【讨论】:

  • 现代 Linux 是否甚至支持将电话号码作为 svc 立即数?我以为你必须使用r7。此外,这是 32 位 ARM 代码,所以可能不是 asld 在 AArch64 系统上默认使用的代码。
  • @PeterCordes svc #1234 只是一个指令示例。我从来没有为在 ARM 上运行的 Linux 编写过汇编代码,所以我不知道正确的调用约定。
  • 我有几次在 qemu 中运行以获得代码高尔夫的答案。我编辑了你的答案,所以它可能真的有效。如果我将 __NR_write__NR_exit 替换为数字文字(因为我的 arm-none GCC 没有 Linux 头文件),它会在本地为我组装 arm-none-eabi-gcc -c
猜你喜欢
  • 2017-03-19
  • 2020-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多