2

I am freshman, I want to learn about ARM assembly language and using gnu toolchain so I decided to start with small project that mixes arm-assembly file and C file by gnu toolchain. My idea is calling a function that is defined in assembly file.

multi.S:

   .globl multi
multi:
    str     fp,[sp,#-4]!
    add     fp,sp,#0
    sub     sp,sp,#12
    str     r0,[fp,#-8]
    str     r1,[fp,#-12]
    ldr     r3,[fp,#-8]
    ldr     r2,[fp,#-12]
    mul     r1,r2,r3
    mov     r3,r1
    mov     r0,r3
    add     sp,fp,#0
    ldr     fp,[sp],#4
    bx      lr

multi.c:

#include <stdio.h>
unsigned int multi(unsigned int a, unsigned int b);

int main(int argc, char *argv[]){
    unsigned int x = multi(3,4);
    printf("%u\n",x);
    return 0;
}

Then I tried to link them together by using command as follows:

arm-none-eabi-gcc -g -c -o multi-arm.o multi.S
arm-none-eabi-gcc -g -c -o multi.o multi.c
arm-none-eabi-ld multi.o multi-arm.o -o multi.elf

But there's some errors occurred:

  1. warning: can not find entry symbol _start; defaulting to 0000000082000000 => I solved this problem by adding -lc --entry main and the warning message is gone.
  2. multi.c:6: undefined reference to 'printf'. I am stuck with this error and it took me 2 hours searching for sulution but I still can't not fix it.

Above is my question. Thank you all for reading.

shawnadl
  • 21
  • 3
  • 5
    There is no `printf` in your code? Is the code you are running is different from this one? – svtag Apr 09 '19 at 09:28
  • 2
    I don't see you linking in a library or object file that provides a `printf` symbol, so it's clear why the link editor is unable to find it. – fuz Apr 09 '19 at 09:29
  • Sorry. I tried to remove the printf to see whether the code would run and forgot to write it again – shawnadl Apr 09 '19 at 14:43
  • I was able to build it by substituting `ld` with `gcc` and adding `--specs=nosys.specs`. See https://stackoverflow.com/questions/17633115/arm-sourcery-toolchain-linking-error and https://stackoverflow.com/questions/19419782/exit-c-text0x18-undefined-reference-to-exit-when-using-arm-none-eabi-gcc – Jeff Apr 09 '19 at 16:40
  • I appreciate you for helping me, I did it by following Jeff's guide:arm-none-eabi-gcc --specs=nosys.specs -o multi.elf multi.o multi-arm.o – shawnadl Apr 10 '19 at 03:18

1 Answers1

0

Try this

arm-none-eabi-as -g -o multi-arm.o multi.S
arm-none-eabi-gcc -g -o multi.elf multi.c multi-arm.o

If you want to use the linker directly then you have to provide the path to the C library on the command line. Gcc knows where the library is (relative to where it executes from and was compiled for) when it calls the linker, but for some strange reason ld does not.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • Thank you, I tried your guide and it worked well with arm-eabi toolchain but with arm-none-eabi tool chain, there were some errors and i fixed it by adding: --spec-nosys.specs – shawnadl Apr 10 '19 at 03:24
  • most C library calls require a system, printf is by a long shot one of the worst ones to use. So if you are not cross compiling for something running on an operating system, say a raspberry pi running linux, then arm-none-eabi makes sense but printf or other C library calls do not. Id learn without those calls first then figure out how to get newlib or other in there later. – old_timer Apr 10 '19 at 14:00
  • the problem here had nothing to do with that just elementary tool usage, you want to call an external function you need to link it in. – old_timer Apr 10 '19 at 14:01