-1

I am trying to run a simple assembly code who print 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'

I assembled it by nasm

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

But when I try to compile/run it , it show error

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

Any way to solve it I am running it in Ubuntu-in-termux

My system info : this is my system info

Thanks in advance

Please solve it

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
  • 1
    AArch64 Android `ld` can't link an x86-64 object file. You also can't run x86-64 machine code binaries natively on an AArch64 kernel, only via qemu or some other emulation. Either write in AArch64 assembly language (and assemble with `as` instead of NASM), or use an x86-64 cross toolchain and emulator. – Peter Cordes Jul 24 '21 at 05:29

1 Answers1

4

But when I try to compile/run it , it show error

When I understand you correctly, the screen shot shows the target device (the device you generate code for).

Your assembly code is for 64-bit x86 CPUs, but your Android device uses an ARM CPU.

You cannot run assembly code for an x86 CPU on an ARM device.

You have to use an assembler for ARM and write ARM assembly code - maybe like this, in 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)

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

GAS (the GNU assembler) for ARM uses @ as the comment character, like in ARM manuals.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
  • 1
    Does modern Linux even support putting the call number as the `svc` immediate? I thought you had to use `r7`. Also, that's 32-bit ARM code, so probably not what `as` and `ld` would default to on an AArch64 system. – Peter Cordes Jul 24 '21 at 06:36
  • @PeterCordes The `svc #1234` is just an example for an instruction. I never wrote assembly code for Linux running on ARM, so I don't know about the correct calling convention. – Martin Rosenau Jul 25 '21 at 19:49
  • I have a couple times, to run in qemu for a code-golf answer. I edited your answer so it might actually work. It assembles locally for me with `arm-none-eabi-gcc -c` if I substitute `__NR_write` and `__NR_exit` with numeric literals (since my arm-none GCC doesn't have Linux headers). – Peter Cordes Jul 25 '21 at 20:09