Undefined reference to `_sbrk' when compiling without semihosting.

Asked by Krzysztof Kawula

Hallo, I’m trying to build pretty basic Cpp project using GCC ARM Embedded 4.7 update 3.
The code is ass follow:

#include <stdio.h>

struct test_class {
  test_class () {
    m = new char[10];
    printf("In ctor\n");
  }
  ~test_class () {
    delete m;
    printf("In dtor\n");
  }
  char * m;
} g;

int main()
{
 printf("In main\n");
 return 0;
}

I’m using(after basic modifications) linker script and start-up code from examples shipped with GCC ARM Embedded.
When I'm compiling with semihosting using linker flags as given in readme.txt
“--specs=nano.specs --specs=rdimon.specs -Wl,--start-group -lgcc -lc -lc -lm -lrdimon -Wl,--end-group”
Compilation finish without any error.

When I try to build without semihosting, once again using flags form readme.txt:
“--specs=nano.specs -Wl,--start-group -lgcc -lc -lc -lm -lnosys -Wl,--end-group”

I gets Errors:
c:/program files (x86)/gnu tools arm embedded/4.7 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-signalr.o): In function `_kill_r':
signalr.c:(.text._kill_r+0xe): undefined reference to `_kill'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-signalr.o): In function `_getpid_r':
signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'
collect2.exe: error: ld returned 1 exit status

In both cases I use compilator flags:
-Os -ffunction-sections -fdata-sections -fno-builtin

What is more when I add -flto to compiler options I get lot of new errors:
`NMI_Handler' referenced in section `.isr_vector' of ./src/startup_ARMCM3.o: defined in discarded section `.text' of ./src/stm32l1xx_it.o (symbol from plugin)
`HardFault_Handler' referenced in section `.isr_vector' of ./src/startup_ARMCM3.o: defined in discarded section `.text' of ./src/stm32l1xx_it.o (symbol from plugin)
`MemManage_Handler' referenced in section `.isr_vector' of ./src/startup_ARMCM3.o: defined in discarded section `.text' of ./src/stm32l1xx_it.o (symbol from plugin)
`BusFault_Handler' referenced in section `.isr_vector' of ./src/startup_ARMCM3.o: defined in discarded section `.text' of ./src/stm32l1xx_it.o (symbol from plugin)
`UsageFault_Handler' referenced in section `.isr_vector' of ./src/startup_ARMCM3.o: defined in discarded section `.text' of ./src/stm32l1xx_it.o (symbol from plugin)
`SVC_Handler' referenced in section `.isr_vector' of ./src/startup_ARMCM3.o: defined in discarded section `.text' of ./src/stm32l1xx_it.o (symbol from plugin)
`DebugMon_Handler' referenced in section `.isr_vector' of ./src/startup_ARMCM3.o: defined in discarded section `.text' of ./src/stm32l1xx_it.o (symbol from plugin)
`PendSV_Handler' referenced in section `.isr_vector' of ./src/startup_ARMCM3.o: defined in discarded section `.text' of ./src/stm32l1xx_it.o (symbol from plugin)
`SysTick_Handler' referenced in section `.isr_vector' of ./src/startup_ARMCM3.o: defined in discarded section `.text' of ./src/stm32l1xx_it.o (symbol from plugin)
collect2.exe: error: ld returned 1 exit status

I have found how to solved it on Cooox support forum (http://www.coocox.org/forum/topic.php?id=3002), but they use C start-up code and I have no idea what is assembler equivalent of __attribute__ ((used, section(".isr_vector"))) and _attribute__((used)) .

Thanks for any help.

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
Joey Ye
Solved:
Last query:
Last reply:
Revision history for this message
Krzysztof Kawula (kkawula) said :
#1

Ok, I have partly solved problem. instead of adding parameter __attribute__ ((used, section(".isr_vector"))) in start up code I have added it in the application sources (for example: __attribute__ ((used, section(".isr_vector"))) void HardFault_Handler(void) ), additionally I have added compilation flag "-fno-builtin" and now I'm able to compile with "-flto" using semihosting and without semihosting.

But still I can't compile without "-flto" parameter and without semihosting because of the errors listed bellow:
c:/program files (x86)/gnu tools arm embedded/4.7 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-signalr.o): In function `_kill_r':
signalr.c:(.text._kill_r+0xe): undefined reference to `_kill'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-signalr.o): In function `_getpid_r':
signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-writer.o): In function `_write_r':
writer.c:(.text._write_r+0x10): undefined reference to `_write'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-closer.o): In function `_close_r':
closer.c:(.text._close_r+0xc): undefined reference to `_close'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-fstatr.o): In function `_fstat_r':
fstatr.c:(.text._fstat_r+0xe): undefined reference to `_fstat'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-isattyr.o): In function `_isatty_r':
isattyr.c:(.text._isatty_r+0xc): undefined reference to `_isatty'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-lseekr.o): In function `_lseek_r':
lseekr.c:(.text._lseek_r+0x10): undefined reference to `_lseek'
c:/program files (x86)/gnu tools arm embedded/4.7 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-readr.o): In function `_read_r':
readr.c:(.text._read_r+0x10): undefined reference to `_read'
collect2.exe: error: ld returned 1 exit status
make: *** [test_Cpp_proj.elf] Error 1

Without "-flto" and without semihosting project is building correctly.
Can somebody explain me what "-fno-builtin" flag does? As far as I understand gcc manual correctly the "-fno-builtin" discard default gcc functions in favour of libc functions?

Revision history for this message
Best Joey Ye (jinyun-ye) said :
#2

Recommend to follow examples under the samples directory. In your case
please put -l* after object files.
On Oct 29, 2013 5:41 PM, "Krzysztof Kawula" <
<email address hidden>> wrote:

> New question #238326 on GCC ARM Embedded:
> https://answers.launchpad.net/gcc-arm-embedded/+question/238326
>
> Hallo, I’m trying to build pretty basic Cpp project using GCC ARM Embedded
> 4.7 update 3.
> The code is ass follow:
>
> #include <stdio.h>
>
> struct test_class {
> test_class () {
> m = new char[10];
> printf("In ctor\n");
> }
> ~test_class () {
> delete m;
> printf("In dtor\n");
> }
> char * m;
> } g;
>
> int main()
> {
> printf("In main\n");
> return 0;
> }
>
> I’m using(after basic modifications) linker script and start-up code from
> examples shipped with GCC ARM Embedded.
> When I'm compiling with semihosting using linker flags as given in
> readme.txt
> “--specs=nano.specs --specs=rdimon.specs -Wl,--start-group -lgcc -lc -lc
> -lm -lrdimon -Wl,--end-group”
> Compilation finish without any error.
>
> When I try to build without semihosting, once again using flags form
> readme.txt:
> “--specs=nano.specs -Wl,--start-group -lgcc -lc -lc -lm -lnosys
> -Wl,--end-group”
>
> I gets Errors:
> c:/program files (x86)/gnu tools arm embedded/4.7
> 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-sbrkr.o):
> In function `_sbrk_r':
> sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
> c:/program files (x86)/gnu tools arm embedded/4.7
> 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-signalr.o):
> In function `_kill_r':
> signalr.c:(.text._kill_r+0xe): undefined reference to `_kill'
> c:/program files (x86)/gnu tools arm embedded/4.7
> 2013q3/bin/../lib/gcc/arm-none-eabi/4.7.4/../../../../arm-none-eabi/lib/armv7-m\libc_s.a(lib_a-signalr.o):
> In function `_getpid_r':
> signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'
> collect2.exe: error: ld returned 1 exit status
>
> In both cases I use compilator flags:
> -Os -ffunction-sections -fdata-sections -fno-builtin
>
> What is more when I add -flto to compiler options I get lot of new errors:
> `NMI_Handler' referenced in section `.isr_vector' of
> ./src/startup_ARMCM3.o: defined in discarded section `.text' of
> ./src/stm32l1xx_it.o (symbol from plugin)
> `HardFault_Handler' referenced in section `.isr_vector' of
> ./src/startup_ARMCM3.o: defined in discarded section `.text' of
> ./src/stm32l1xx_it.o (symbol from plugin)
> `MemManage_Handler' referenced in section `.isr_vector' of
> ./src/startup_ARMCM3.o: defined in discarded section `.text' of
> ./src/stm32l1xx_it.o (symbol from plugin)
> `BusFault_Handler' referenced in section `.isr_vector' of
> ./src/startup_ARMCM3.o: defined in discarded section `.text' of
> ./src/stm32l1xx_it.o (symbol from plugin)
> `UsageFault_Handler' referenced in section `.isr_vector' of
> ./src/startup_ARMCM3.o: defined in discarded section `.text' of
> ./src/stm32l1xx_it.o (symbol from plugin)
> `SVC_Handler' referenced in section `.isr_vector' of
> ./src/startup_ARMCM3.o: defined in discarded section `.text' of
> ./src/stm32l1xx_it.o (symbol from plugin)
> `DebugMon_Handler' referenced in section `.isr_vector' of
> ./src/startup_ARMCM3.o: defined in discarded section `.text' of
> ./src/stm32l1xx_it.o (symbol from plugin)
> `PendSV_Handler' referenced in section `.isr_vector' of
> ./src/startup_ARMCM3.o: defined in discarded section `.text' of
> ./src/stm32l1xx_it.o (symbol from plugin)
> `SysTick_Handler' referenced in section `.isr_vector' of
> ./src/startup_ARMCM3.o: defined in discarded section `.text' of
> ./src/stm32l1xx_it.o (symbol from plugin)
> collect2.exe: error: ld returned 1 exit status
>
> I have found how to solved it on Cooox support forum (
> http://www.coocox.org/forum/topic.php?id=3002), but they use C start-up
> code and I have no idea what is assembler equivalent of __attribute__
> ((used, section(".isr_vector"))) and _attribute__((used)) .
>
> Thanks for any help.
>
>
> --
> You received this question notification because you are an answer
> contact for GCC ARM Embedded.
>

Revision history for this message
Krzysztof Kawula (kkawula) said :
#3

Right linking order of library definitely solve my problem. Now I’m able to compile my project with all flags like in examples. Eclipse plugin make me silly joke.
Thanks a lot for help.

Revision history for this message
Krzysztof Kawula (kkawula) said :
#4

Thanks Joey Ye, that solved my question.