3

Currently, I am learning to do ARM bare metal development. The development board that I am using is SAMA5D3x-EK.

I am trying to build the sample code (Atmel/sama5d3x-ek/examples/getting-started) given by Atmel.

IDE

eclipse with GNU ARM plugin

linker script

Atmel/sama5d3x-ek/libraries/libboard_sama5d3x-ek/resources/gcc/sama5d3x/sram.ld

startup code

Atmel/sama5d3x-ek/libraries/libboard_sama5d3x-ek/source/board_cstartup_gnu.S

c runtime library stubs

Atmel/sama5d3x-ek/libraries/libboard_sama5d3x-ek/source/syscalls.c

I have built the libboard_sama5d3x_ek_gcc_dbg.a. And I am very sure that all the stub functions have been packed into the .a static library.

    $ arm-none-eabi-nm libboard_sama5d3x_ek_gcc_dbg.a  
    ...
    syscalls.o:
        00000000 T _close
                 U DBGU_PutChar
        00000000 T _exit
        00000000 T _fstat
        00000000 T _getpid
                 U _heap
        00000000 b heap.9035
                 U iprintf
        00000000 T _isatty
        00000000 T _kill
        00000000 r .LC0
        00000000 T link
        00000000 T _lseek
        00000000 T _read
        00000000 T _sbrk
        00000000 T _write
    ...

However when I try to build the project, I get the following errors for linker section:

Building target: arm_sama5d3x_baremetal_template.elf
Invoking: Cross ARM C Linker
arm-none-eabi-gcc -mcpu=cortex-a5 -march=armv7-a -marm -mthumb-interwork -mfloat-abi=hard -mfpu=vfpv4-d16 -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections  -g3 -T "/home/yusheng/workspace/arm_sama5d3x_baremetal_template/sram.ld" -nostartfiles -Xlinker --gc-sections -L/usr/local/Atmel/sama5d3x-ek/libraries/libchip_sama5d3x/lib -L/usr/local/Atmel/sama5d3x-ek/libraries/libboard_sama5d3x-ek/lib -Wl,-Map,"arm_sama5d3x_baremetal_template.map" -v -static -o "arm_sama5d3x_baremetal_template.elf"  ./board_cstartup_gnu.o ./main.o  /usr/local/Atmel/sama5d3x-ek/libraries/libboard_sama5d3x-ek/lib/libboard_sama5d3x_ek_gcc_dbg.a /usr/local/Atmel/sama5d3x-ek/libraries/libchip_sama5d3x/lib/libchip_sama5d3x_gcc_dbg.a -lboard_sama5d3x_ek_gcc_dbg -lchip_sama5d3x_gcc_dbg
Using built-in specs.
COLLECT_GCC=arm-none-eabi-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-none-eabi/4.8.2/lto-wrapper
Target: arm-none-eabi
Configured with: ../gcc-4.8.2/configure --build=i686-linux-gnu --prefix=/usr --includedir='/usr/lib/include' --mandir='/usr/lib/share/man' --infodir='/usr/lib/share/info' --sysconfdir=/etc --localstatedir=/var --libexecdir='/usr/lib/lib/gcc-arm-none-eabi' --disable-maintainer-mode --disable-dependency-tracking --enable-languages=c,c++ --prefix=/usr/lib --infodir=/usr/share/doc/gcc-arm-none-eabi/info --mandir=/usr/share/man --htmldir=/usr/share/doc/gcc-arm-none-eabi/html --pdfdir=/usr/share/doc/gcc-arm-none-eabi/pdf --bindir=/usr/bin --libexecdir=/usr/lib --libdir=/usr/lib --with-system-zlib --enable-multilib --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-libstdc++-v3 --disable-nls --disable-shared --disable-threads --disable-tls --disable-plugin --build=i686-linux-gnu --host=i686-linux-gnu --target=arm-none-eabi --with-gnu-as --with-gnu-ld --with-headers=no --without-newlib --with-pkgversion=4.8.2-14ubuntu1+6 --without-included-gettext --with-multilib-list=armv6-m,armv7-m,armv7e-m,armv7-r CFLAGS='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat' CPPFLAGS=-D_FORTIFY_SOURCE=2 CXXFLAGS='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat' FFLAGS='-g -O2' LDFLAGS=-Wl,-z,relro AR_FOR_TARGET=arm-none-eabi-ar AS_FOR_TARGET=arm-none-eabi-as LD_FOR_TARGET=arm-none-eabi-ld NM_FOR_TARGET=arm-none-eabi-nm OBJDUMP_FOR_TARGET=arm-none-eabi-objdump RANLIB_FOR_TARGET=arm-none-eabi-ranlib READELF_FOR_TARGET=arm-none-eabi-readelf STRIP_FOR_TARGET=arm-none-eabi-strip
Thread model: single
gcc version 4.8.2 (4.8.2-14ubuntu1+6) 
COMPILER_PATH=/usr/lib/gcc/arm-none-eabi/4.8.2/:/usr/lib/gcc/arm-none-eabi/4.8.2/:/usr/lib/gcc/arm-none-eabi/:/usr/lib/gcc/arm-none-eabi/4.8.2/:/usr/lib/gcc/arm-none-eabi/:/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/bin/
LIBRARY_PATH=/usr/lib/gcc/arm-none-eabi/4.8.2/armv7-ar/thumb/fpu/:/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/:/usr/lib/gcc/arm-none-eabi/4.8.2/:/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/
COLLECT_GCC_OPTIONS='-mcpu=cortex-a5' '-march=armv7-a' '-marm' '-mthumb-interwork' '-mfloat-abi=hard' '-mfpu=vfpv4-d16' '-O0' '-fmessage-length=0' '-fsigned-char' '-ffunction-sections' '-fdata-sections' '-g3' '-T' '/home/yusheng/workspace/arm_sama5d3x_baremetal_template/sram.ld' '-nostartfiles' '-L/usr/local/Atmel/sama5d3x-ek/libraries/libchip_sama5d3x/lib' '-L/usr/local/Atmel/sama5d3x-ek/libraries/libboard_sama5d3x-ek/lib' '-v' '-static' '-o' 'arm_sama5d3x_baremetal_template.elf'
 /usr/lib/gcc/arm-none-eabi/4.8.2/collect2 -Bstatic -X -o arm_sama5d3x_baremetal_template.elf -L/usr/local/Atmel/sama5d3x-ek/libraries/libchip_sama5d3x/lib -L/usr/local/Atmel/sama5d3x-ek/libraries/libboard_sama5d3x-ek/lib -L/usr/lib/gcc/arm-none-eabi/4.8.2/armv7-ar/thumb/fpu -L/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu -L/usr/lib/gcc/arm-none-eabi/4.8.2 -L/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib --gc-sections -Map arm_sama5d3x_baremetal_template.map ./board_cstartup_gnu.o ./main.o /usr/local/Atmel/sama5d3x-ek/libraries/libboard_sama5d3x-ek/lib/libboard_sama5d3x_ek_gcc_dbg.a /usr/local/Atmel/sama5d3x-ek/libraries/libchip_sama5d3x/lib/libchip_sama5d3x_gcc_dbg.a -lboard_sama5d3x_ek_gcc_dbg -lchip_sama5d3x_gcc_dbg --start-group -lgcc -lg -lc --end-group -T /home/yusheng/workspace/arm_sama5d3x_baremetal_template/sram.ld
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/libg.a(lib_a-init.o): In function `__libc_init_array':
/build/buildd/newlib-2.1.0/build/arm-none-eabi/armv7-ar/thumb/fpu/newlib/libc/misc/../../../../../../../../newlib/libc/misc/init.c:37: undefined reference to `_init'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/libg.a(lib_a-abort.o): In function `abort':
/build/buildd/newlib-2.1.0/build/arm-none-eabi/armv7-ar/thumb/fpu/newlib/libc/stdlib/../../../../../../../../newlib/libc/stdlib/abort.c:63: undefined reference to `_exit'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/libg.a(lib_a-sbrkr.o): In function `_sbrk_r':
/build/buildd/newlib-2.1.0/build/arm-none-eabi/armv7-ar/thumb/fpu/newlib/libc/reent/../../../../../../../../newlib/libc/reent/sbrkr.c:58: undefined reference to `_sbrk'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/libg.a(lib_a-signalr.o): In function `_kill_r':
/build/buildd/newlib-2.1.0/build/arm-none-eabi/armv7-ar/thumb/fpu/newlib/libc/reent/../../../../../../../../newlib/libc/reent/signalr.c:61: undefined reference to `_kill'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/libg.a(lib_a-signalr.o): In function `_getpid_r':
/build/buildd/newlib-2.1.0/build/arm-none-eabi/armv7-ar/thumb/fpu/newlib/libc/reent/../../../../../../../../newlib/libc/reent/signalr.c:96: undefined reference to `_getpid'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/libg.a(lib_a-writer.o): In function `_write_r':
/build/buildd/newlib-2.1.0/build/arm-none-eabi/armv7-ar/thumb/fpu/newlib/libc/reent/../../../../../../../../newlib/libc/reent/writer.c:58: undefined reference to `_write'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/libg.a(lib_a-closer.o): In function `_close_r':
/build/buildd/newlib-2.1.0/build/arm-none-eabi/armv7-ar/thumb/fpu/newlib/libc/reent/../../../../../../../../newlib/libc/reent/closer.c:53: undefined reference to `_close'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/libg.a(lib_a-fstatr.o): In function `_fstat_r':
/build/buildd/newlib-2.1.0/build/arm-none-eabi/armv7-ar/thumb/fpu/newlib/libc/reent/../../../../../../../../newlib/libc/reent/fstatr.c:62: undefined reference to `_fstat'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/libg.a(lib_a-isattyr.o): In function `_isatty_r':
/build/buildd/newlib-2.1.0/build/arm-none-eabi/armv7-ar/thumb/fpu/newlib/libc/reent/../../../../../../../../newlib/libc/reent/isattyr.c:58: undefined reference to `_isatty'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/libg.a(lib_a-lseekr.o): In function `_lseek_r':
/build/buildd/newlib-2.1.0/build/arm-none-eabi/armv7-ar/thumb/fpu/newlib/libc/reent/../../../../../../../../newlib/libc/reent/lseekr.c:58: undefined reference to `_lseek'
/usr/lib/gcc/arm-none-eabi/4.8.2/../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/libg.a(lib_a-readr.o): In function `_read_r':
/build/buildd/newlib-2.1.0/build/arm-none-eabi/armv7-ar/thumb/fpu/newlib/libc/reent/../../../../../../../../newlib/libc/reent/readr.c:58: undefined reference to `_read'
collect2: error: ld returned 1 exit status
make: *** [arm_sama5d3x_baremetal_template.elf] Error 1

The error claims that the stub functions cannot be found, and yet the functions are inside the static library, which I have already added to the project.

I would be grateful if someone can give me some advices. Thanks.

Edit 1

@Valeri Atamaniouk The options for /usr/lib/gcc/arm-none-eabi/4.8.2/collect2 command is generated by eclipse. I have no idea on how to change the static linking order for this.

Also as given in the option list, the linking order is as below:

./board_cstartup_gnu.o ./main.o /usr/local/Atmel/sama5d3x-ek/libraries/libboard_sama5d3x-ek/lib/libboard_sama5d3x_ek_gcc_dbg.a /usr/local/Atmel/sama5d3x-ek/libraries/libchip_sama5d3x/lib/libchip_sama5d3x_gcc_dbg.a -lboard_sama5d3x_ek_gcc_dbg -lchip_sama5d3x_gcc_dbg --start-group -lgcc -lg -lc --end-group

Here I dont know where the newlib library comes in. Assuming the newlib library is referred to "-lgcc -lg -lc", we should see as above that the libboard_sama5d3x_ek_gcc_dbg.a come first before them.

I have a workaround. Rebuild the static library excluding the syscalls.c and board_cstartup_gnu.S. Then copy and include the source files into the project. Although the workaround works, I want to understand where the problem comes from, and what the proper way is to fix this.

Community
  • 1
  • 1
  • possible duplicate of [Linker order - GCC](http://stackoverflow.com/questions/45135/linker-order-gcc) - specifically, all the bits about static library ordering. – Notlikethat Feb 14 '15 at 00:17

2 Answers2

0

Put your library somewhere between --start-group and --end-group tags. Most likely you have a case, when different libraries reference each other, and library group is how it shall be resolved.

Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18
0

can you try the following two flags in your command line (one at a time) this will resolve the unresolved init issue.

-Xlinker --specs=nosys.specs
-Xlinker --specs=rdimon.specs

the rest of the issues are related with -Xlinker --start-group and -Xlinker --end-group before and after libraries plus object files (as already mentioned)

PS: I hope this will resolve your issue

theadnangondal
  • 1,546
  • 3
  • 14
  • 28
  • 1
    Thanks theadnangondal. For the init issue, as we are doing c programming, we can define an empty `void _init(void)` function or comment out the call of `__libc_init_array` in the `XX_cstartup.s` to solve it, because based on "Building bare-metal ARM with GNU.pdf": "(22) The library function `__libc_init_array` invokes all C++ static constructors (see also the linker script). This function is invoked with the `BX` instruction, which allows state change to `THUMB`. This function is harmless in C." – user3792371 Feb 19 '15 at 16:35