-->
Page 1 of 3

Errors when linking against libstdc++

PostPosted: Sat Jan 30, 2016 10:30 pm
by bouvrais
Hi,

I received a couple of ESP-01 boards last week and started playing around with them. I installed the esp-open-sdk and compiled the toolchain. I have since compiled a few pure C examples and wrote a few on my own as well with success.
Lately I decided to up my game and try to get something up and running based on C++. The example I am trying to compile right now uses std::string and std::function. Whenever I try to add libstdc++ as an additional library, the linker complains that section `.text' will not fit in region `iram1_0_seg'.
Looking at the map file, all libstdc++ object do indeed seem to end up in the iram1_0 segment. I am not completely familiar with the memory space layout of the ESP8266, but from what I get from reading this forum, one can choose to place its code directly in iram1_0_seg or in irom0_0_seg where it will be loaded and cached in ram on demand. Is there a way to have the libstdc++ objects place in irom0_0_seg?
Is there a working example somewhere of a project leveraging the c++ standard library that I could start from?

I am attaching my Makefile for completeness.

Thanks in advance.

Re: Errors when linking against libstdc++

PostPosted: Tue Feb 02, 2016 11:27 am
by dkinzer
bouvrais wrote:Is there a way to have the libstdc++ objects place in irom0_0_seg?
You can use a custom linker script to put certain pieces of code in the irom0_0_seg. The linker scripts that the Arduino platform uses employ this technique. Alternately, you can rename all of the text segments in the stdc++ library to irom0_0_seg and then use the standard linker scripts.

By the way, this issue isn't specific to C++ or the stdc++ libraries. Any source code or library written by another party that isn't specifically for the ESP8266 will have the same issue.

Re: Errors when linking against libstdc++

PostPosted: Wed Feb 03, 2016 9:27 am
by bouvrais
Thanks dkinzer.
The Arduino ESP8266 GitHub was indeed a good source for inspiration. Creating a custom linker script and moving libstdc++. (as well as libgcc.a) to the irom0_0_seg did indeed hush most of the linker errors. For the remaining ones, I had to stub some libc functions on which libstdc++ rely and are not provided with the toolchain.
So now my app does compile and link correctly. And of course it crashes on load with a cycling Fatal Exception over and over again :) :

Code: Select allFatal exception (0):
epc1=0x4024129c, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000
Fatal exception (0):
epc1=0x4024129c, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000
Fatal exception (0):
epc1=0x4024129c, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000
Fatal exception (0):
epc1=0x4024129c, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000


According to my map file, 0x4024129c is the address of the __udivsi3 libgcc function.
This happens with an empty user_init() function and as far as I know static constructors are not called by default (one needs to call them explicitely) so I would like to rule that out as a possible cause. That leaves me with very few ideas other than the bootloader doesn't like the code I am providing.

Any suggestion on how I could investigate this crash further?

Re: Errors when linking against libstdc++

PostPosted: Wed Feb 03, 2016 8:19 pm
by bouvrais
So! After some trial and error I found that if I remove libgcc.a from irom and let it back into iram, my empty user_init() behaves correctly (no crash). So I am pretty much back to square one as doing this not only fulls the iram (which becomes too small) but now code in the text section of libstdc++ complains that calls made to symbols in libgcc are out of range:

Code: Select all/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld: build/app.out section `.text' will not fit in region `iram1_0_seg'
/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libstdc++.a(eh_personality.o): In function `base_of_encoded_value':
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/../libgcc/unwind-pe.h:114:(.text._ZL21base_of_encoded_valuehP15_Unwind_Context+0x33): dangerous relocation: call0: call target out of range: _Unwind_GetTextRelBase
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/../libgcc/unwind-pe.h:116:(.text._ZL21base_of_encoded_valuehP15_Unwind_Context+0x3b): dangerous relocation: call0: call target out of range: _Unwind_GetDataRelBase
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/../libgcc/unwind-pe.h:118:(.text._ZL21base_of_encoded_valuehP15_Unwind_Context+0x43): dangerous relocation: call0: call target out of range: _Unwind_GetRegionStart
/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libstdc++.a(eh_personality.o): In function `parse_lsda_header':
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/libsupc++/eh_personality.cc:55:(.text._ZL17parse_lsda_headerP15_Unwind_ContextPKhP16lsda_header_info+0x19): dangerous relocation: call0: call target out of range: _Unwind_GetRegionStart
/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libstdc++.a(eh_personality.o): In function `__gxx_personality_v0':
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/libsupc++/eh_personality.cc:432:(.text.__gxx_personality_v0+0x6e): dangerous relocation: call0: call target out of range: _Unwind_GetLanguageSpecificData
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/libsupc++/eh_personality.cc:442:(.text.__gxx_personality_v0+0x95): dangerous relocation: call0: call target out of range: _Unwind_GetIPInfo
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/libsupc++/eh_personality.cc:707:(.text.__gxx_personality_v0+0x256): dangerous relocation: call0: call target out of range: _Unwind_SetGR
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/libsupc++/eh_personality.cc:709:(.text.__gxx_personality_v0+0x262): dangerous relocation: call0: call target out of range: _Unwind_SetGR
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/libsupc++/eh_personality.cc:710:(.text.__gxx_personality_v0+0x269): dangerous relocation: call0: call target out of range: _Unwind_SetIP
/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libstdc++.a(eh_personality.o): In function `~end_catch_protect':
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/libsupc++/eh_personality.cc:736:(.text.__cxa_call_unexpected+0x36): dangerous relocation: call0: call target out of range: _Unwind_Resume
/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libstdc++.a(stdexcept.o): In function `std::logic_error::logic_error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/src/c++98/stdexcept.cc:53:(.text._ZNSt11logic_errorC2ERKSs+0x22): dangerous relocation: call0: call target out of range: _Unwind_Resume
/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libstdc++.a(vterminate.o): In function `__gnu_cxx::__verbose_terminate_handler()':
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/libsupc++/vterminate.cc:82:(.text._ZN9__gnu_cxx27__verbose_terminate_handlerEv+0x96): dangerous relocation: call0: call target out of range: _Unwind_Resume
/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libstdc++.a(functexcept.o): In function `std::__throw_logic_error(char const*)':
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/src/c++11/functexcept.cc:109:(.text._ZSt19__throw_logic_errorPKc+0x4a): dangerous relocation: call0: call target out of range: _Unwind_Resume
/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libstdc++.a(functexcept.o): In function `std::__throw_length_error(char const*)':
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/src/c++11/functexcept.cc:109:(.text._ZSt20__throw_length_errorPKc+0x4a): dangerous relocation: call0: call target out of range: _Unwind_Resume
/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libstdc++.a(eh_terminate.o): In function `__cxxabiv1::__terminate(void (*)())':
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/libsupc++/eh_terminate.cc:42:(.text._ZN10__cxxabiv111__terminateEPFvvE+0x24): dangerous relocation: call0: call target out of range: _Unwind_Resume
/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libstdc++.a(eh_catch.o): In function `__cxa_end_catch':
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/libsupc++/eh_catch.cc:125:(.text.__cxa_end_catch+0x49): dangerous relocation: call0: call target out of range: _Unwind_DeleteException
/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libstdc++.a(eh_throw.o): In function `__cxa_throw':
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/libsupc++/eh_throw.cc:79:(.text.__cxa_throw+0x39): dangerous relocation: call0: call target out of range: _Unwind_RaiseException
/Volumes/esp-open-sdk/esp-open-sdk/xtensa-lx106-elf/lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libstdc++.a(eh_throw.o): In function `__cxa_rethrow':
/Volumes/esp-open-sdk/esp-open-sdk/crosstool-NG/.build/src/gcc-4.8.2/libstdc++-v3/libsupc++/eh_throw.cc:115:(.text.__cxa_rethrow+0x45): dangerous relocation: call0: call target out of range: _Unwind_Resume_or_Rethrow
collect2: error: ld returned 1 exit status
make: *** [build/app.out] Error 1


So that leaves me with two questions:
  • Why does the app crash when libgcc is in irom? I guess understanding this would be easier with information on the bootloader.
  • Would libstdc++ need to be recompiled with -mlongcalls? Why is that not the default (or is it and something else explains this)?