-lgcc seemingly ignored when linking with -nostdlib

Asked by John on 2018-09-13

I tried to link using a freestanding environment by passing -ffreestanding, -nostdlib and -lgcc, but the -lgcc option seems to be ignored by the linker because a symbol in libgcc.a is not linked in. The following error message is emitted.

./main.c.o: In function `main':
main.c:21: undefined reference to `__aeabi_fmul'
collect2: error: ld returned 1 exit status

The linker command line is shown below.

arm-none-eabi-gcc -mcpu=cortex-m0plus -mthumb -ffreestanding -nostdlib -lgcc -Tlinker/ATSAMD21J18A.ld -Tlinker/sections.ld -o exec.elf [object files]

The program being compiled is

int main( void )
{
 volatile float a = 1.10, b=2.2;

 a *= b;

 return 0;
}

The __aeabi_fmul function is located in libgcc.a. When linking without the -nostdlib option the map file generated by the linker has the following lines indicating that __aeabi_fmul is indeed being linked from libgcc.

Archive member included to satisfy reference by file (symbol)
/usr/bin/../lib/gcc/arm-none-eabi/7.3.1/libgcc.a(_arm_muldivsf3.o)
                              ./main.c.o (__aeabi_fmul)

 .text 0x0000000000000024 0x2f8 /usr/bin/../lib/gcc/arm-none-eabi/7.3.1/libgcc.a(_arm_muldivsf3.o)
                0x0000000000000024 __mulsf3
                0x0000000000000024 __aeabi_fmul
                0x00000000000001bc __divsf3
                0x00000000000001bc __aeabi_fdiv

I suspect that the -lgcc option when passed along with -nostdlib is being ignored by the linker. The compiler version is

arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2018-q3-update) 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907]
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
Thomas Preud'homme
Solved:
2018-09-18
Last query:
2018-09-18
Last reply:
2018-09-18

Hi John,

Have you tried putting -lgcc at the end? Linking work by having definition after uses, so the -lgcc should be after the object files on the command-line. When not using -nostdlib GCC would add it at the end which would explains why it works in that case.

Best regards.

John (jdoe95) said : #2

Thomas,

Thank you very much for providing an answer. Your suggestion solved the problem. The working linker command line is

arm-none-eabi-gcc -mcpu=cortex-m0plus -mthumb -ffreestanding -nostdlib -Tlinker/ATSAMD21J18A.ld -Tlinker/sections.ld -o exec.elf [object files] -lgcc

Thank you.
Regards,
John

John (jdoe95) said : #3

Thanks Thomas Preud'homme, that solved my question.