GNU ld 消息:“添加符号时出错:汇编语言格式错误的文件”

Pra*_*wat -1 ubuntu assembly android nasm termux

我正在尝试运行一个简单的汇编代码来打印 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'
Run Code Online (Sandbox Code Playgroud)

我用nasm组装的

root@localhost:~# nasm -f elf64 do.asm -o do.o
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

有什么方法可以解决这个问题我正在 Ubuntu-in-termux 中运行它

我的系统信息: 这是我的系统信息

提前致谢

请解决

Mar*_*nau 5

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

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

您的汇编代码适用于 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)
Run Code Online (Sandbox Code Playgroud)

请参阅什么是 ARM 系统调用的接口以及它在 Linux 内核中的何处定义?

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