سؤال

I'm just starting to learn ARM assembly (on a RaspberryPi).

I've written the following program, helloworld.s:

.data
HelloWorld:
    .ascii "Hello World\n"

.text
.globl _start
_start:
    mov r7, #4 
    mov r0, #1
    ldr r1,=HelloWorld
    mov r2, #12
    svc #0

    mov r7, #1
    mov r0, #42
    svc #0

I can assemble it (I'm assuming linking is automatic, as with gcc).

chris@raspberrypi ~/elf $ as helloworld.s 
chris@raspberrypi ~/elf $ file a.out
a.out: ELF 32-bit LSB relocatable, ARM, version 1 (SYSV), not stripped

But is doesn't run:

chris@raspberrypi ~/elf $ chmod 755 a.out 
chris@raspberrypi ~/elf $ ./a.out 
-bash: ./a.out: cannot execute binary file

What have I missed?

Is the fact that a.out is not executable (by default) symptomatic of something?

Here is the executable:

chris@raspberrypi ~/elf $ xxd a.out 
0000000: 7f45 4c46 0101 0100 0000 0000 0000 0000  .ELF............
0000010: 0100 2800 0100 0000 0000 0000 0000 0000  ..(.............
0000020: bc00 0000 0000 0005 3400 0000 0000 2800  ........4.....(.
0000030: 0900 0600 0470 a0e3 0100 a0e3 1010 9fe5  .....p..........
0000040: 0c20 a0e3 0000 00ef 0170 a0e3 2a00 a0e3  . .......p..*...
0000050: 0000 00ef 0000 0000 4865 6c6c 6f20 576f  ........Hello Wo
0000060: 726c 640a 4115 0000 0061 6561 6269 0001  rld.A....aeabi..
0000070: 0b00 0000 0601 0801 2c01 002e 7379 6d74  ........,...symt
0000080: 6162 002e 7374 7274 6162 002e 7368 7374  ab..strtab..shst
0000090: 7274 6162 002e 7265 6c2e 7465 7874 002e  rtab..rel.text..
00000a0: 6461 7461 002e 6273 7300 2e41 524d 2e61  data..bss..ARM.a
00000b0: 7474 7269 6275 7465 7300 0000 0000 0000  ttributes.......
00000c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000e0: 0000 0000 1f00 0000 0100 0000 0600 0000  ................
00000f0: 0000 0000 3400 0000 2400 0000 0000 0000  ....4...$.......
0000100: 0000 0000 0400 0000 0000 0000 1b00 0000  ................
0000110: 0900 0000 0000 0000 0000 0000 d002 0000  ................
0000120: 0800 0000 0700 0000 0100 0000 0400 0000  ................
0000130: 0800 0000 2500 0000 0100 0000 0300 0000  ....%...........
0000140: 0000 0000 5800 0000 0c00 0000 0000 0000  ....X...........
0000150: 0000 0000 0100 0000 0000 0000 2b00 0000  ............+...
0000160: 0800 0000 0300 0000 0000 0000 6400 0000  ............d...
0000170: 0000 0000 0000 0000 0000 0000 0100 0000  ................
0000180: 0000 0000 3000 0000 0300 0070 0000 0000  ....0......p....
0000190: 0000 0000 6400 0000 1600 0000 0000 0000  ....d...........
00001a0: 0000 0000 0100 0000 0000 0000 1100 0000  ................
00001b0: 0300 0000 0000 0000 0000 0000 7a00 0000  ............z...
00001c0: 4000 0000 0000 0000 0000 0000 0100 0000  @...............
00001d0: 0000 0000 0100 0000 0200 0000 0000 0000  ................
00001e0: 0000 0000 2402 0000 9000 0000 0800 0000  ....$...........
00001f0: 0800 0000 0400 0000 1000 0000 0900 0000  ................
0000200: 0300 0000 0000 0000 0000 0000 b402 0000  ................
0000210: 1900 0000 0000 0000 0000 0000 0100 0000  ................
0000220: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000230: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000240: 0300 0100 0000 0000 0000 0000 0000 0000  ................
0000250: 0300 0300 0000 0000 0000 0000 0000 0000  ................
0000260: 0300 0400 0100 0000 0000 0000 0000 0000  ................
0000270: 0000 0300 0c00 0000 0000 0000 0000 0000  ................
0000280: 0000 0100 0f00 0000 2000 0000 0000 0000  ........ .......
0000290: 0000 0100 0000 0000 0000 0000 0000 0000  ................
00002a0: 0300 0500 1200 0000 0000 0000 0000 0000  ................
00002b0: 1000 0100 0048 656c 6c6f 576f 726c 6400  .....HelloWorld.
00002c0: 2461 0024 6400 5f73 7461 7274 0000 0000  $a.$d._start....
00002d0: 2000 0000 0202 0000                       .......

           .......
هل كانت مفيدة؟

المحلول

As Paul R noted, gcc -nostartfiles helloworld.s should do the trick.

With just calling as you are building an object file you can use in other programs (like .o files when building C code).

file should tell you that a.out is actually an executable:

a.out: ELF 32-bit LSB executable, ARM, ....

If you want further advice, I wouldn't start with an assembly program from scratch. Instead, write the boilerplate code in C and use GCC's inline assembly features to drop to assembly whenever you wish. That way you can write whole functions in assembly, but still have C to back you up for things like printing results, etc.

نصائح أخرى

You still would need to link it after assembling phase.

$(CROSS_COMPILE)as file.s -o file.o
$(CROSS_COMPILE)ld file.o -o file

I actually have an example at github trying to create smallest elf for arm which might be helpful to you.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top