Simple asm program with gcc, ld error

Asked by EI24

Hi, i'm trying to assemble a simple program to check if i've gotten it right. The program(test.s) simply contains, MOVS r0, #0x5.
In terminal i first tried:
$arm-none-eabi-gcc -x assembler-with-cpp -Wa,-mcpu=cortex-m0 -o test.bin test2.s

However got error from the linker:
/opt/gcc-arm-none-eabi-4_9-2015q1/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/libc.a(lib_a-exit.o): In function `exit':
exit.c:(.text.exit+0x2c): undefined reference to `_exit'
/opt/gcc-arm-none-eabi-4_9-2015q1/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/crt0.o: In function `_start':
(.text+0xec): undefined reference to `main'
collect2: error: ld returned 1 exit status

Searching the net, a recommendation was to use, --specs=rdimon.specs . However when i try this one error remains:
$ arm-none-eabi-gcc -x assembler-with-cpp --specs=nosys.specs --specs=rdimon.specs -Wa,-mcpu=cortex-m0 -o test.bin test2.s
/opt/gcc-arm-none-eabi-4_9-2015q1/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/rdimon-crt0.o: In function `_start':
(.text+0x120): undefined reference to `main'
collect2: error: ld returned 1 exit status

Any idea how to solve this? Im simply trying to assemble arm code to a cortex-m0 based MCU(which im going to download to the MCU). If there is a better way to do this i greatly appreciate any suggestions!

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
Andre Vieira
Solved:
Last query:
Last reply:
Revision history for this message
Tony Liu (mrtoniliu) said :
#1

Hi,

This is because you don't define a main function as entry point which is required for linker. For this simple case, you can create a test.c file contains:

void main(){
    asm
    (
        "MOVS r0, #0x5"
    );
}

BR,
Tony

Revision history for this message
Tony Liu (mrtoniliu) said :
#2

Then just use

arm-none-eabi-gcc -mcpu=cortex-m0 -mthumb -specs=rdimon.specs -o test.bin test.c

it works for me.

Revision history for this message
EI24 (josefcool-95) said :
#3

Thx for the answer! Ok, but there is no way to assemble without using inline assembly?(assuming your suggestion is inline)?

Revision history for this message
Best Andre Vieira (andre-simoesdiasvieira) said :
#4

Hi,

You can write the following assembly:
.global main
main:
  MOVS r0, #0x5

That should work too.

Revision history for this message
EI24 (josefcool-95) said :
#5

Thanks Andre Vieira, that solved my question.