Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libstdc++ with RTTI (i.e. without -fno-rtti) (IDFGH-751) #1684

Closed
jovermann opened this issue Mar 6, 2018 · 14 comments
Closed

libstdc++ with RTTI (i.e. without -fno-rtti) (IDFGH-751) #1684

jovermann opened this issue Mar 6, 2018 · 14 comments
Labels
Type: Feature Request Feature request for IDF

Comments

@jovermann
Copy link

jovermann commented Mar 6, 2018

Apparently the libstdc++ in the current toolchain (xtensa-esp32-elf-linux64-1.22.0-75-gbaf03c2-5.2.0.tar.gz) is compiled with -fno-rtti.

I have a project which relies on RTTI and I modified project.mk to have -frtti instead of -fno-rtti. This compiled fine, but when linking I get lots of errors like:

undefined reference to `typeinfo for std::basic_istream<char, std::char_traits >'

for various types of course. I think this is because libstdc++ is compiled with -fno-rtti.

Request: Please compile libstdc++ in the toolchain with -frtti (or with no rtti flag at all because RTTI is enabled by default). Please change the default flags for CXX to -frtti (or to no rtti flag at all because RTTI is enabled by defaul).

The linker will ignore the additional data when it is not needed, so I do not see a drawback.

I currently see non-deterministic crashes when doing dynamic_cast on user defined types which are derived from std:: types (like locale etc). This is perfectly explainable since the RTTI settings between my code and libstdc++ are inconsistent. But as an esp-idf user I have no way to fix libstdc++.

@FayeY FayeY changed the title libstdc++ with RTTI (i.e. without -fno-rtti) [TW#19011] libstdc++ with RTTI (i.e. without -fno-rtti) Mar 9, 2018
@igrr igrr added the Type: Feature Request Feature request for IDF label Mar 15, 2019
@github-actions github-actions bot changed the title [TW#19011] libstdc++ with RTTI (i.e. without -fno-rtti) [TW#19011] libstdc++ with RTTI (i.e. without -fno-rtti) (IDFGH-751) Mar 15, 2019
@projectgus projectgus changed the title [TW#19011] libstdc++ with RTTI (i.e. without -fno-rtti) (IDFGH-751) libstdc++ with RTTI (i.e. without -fno-rtti) (IDFGH-751) Apr 29, 2019
@chengyuhui
Copy link

Any update on this?

Recently I have been trying to get https://github.com/ThePhD/sol2 running on EPS32, which depends on rtti to compile successfully.

@igrr
Copy link
Member

igrr commented Jun 5, 2019

The linker will ignore the additional data when it is not needed, so I do not see a drawback.

Unfortunately, this is not entirely the case. If RTTI is enabled when compiling libstdc++.a, then vtables of classes compiled into libstdc++.a will contain pointers to the typeinfo structures. When such classes are used in the application, typeinfo structures will also be linked, because they are referenced from vtables. This will be the case even if the application does not enable RTTI. In some tests we have seen increase of application binary size by 10kB or more.

We are looking into approaches to reduce this penalty for users who do not need to enable RTTI.

@v1993
Copy link
Contributor

v1993 commented Jun 5, 2019

@igrr Can't you build libstdc++ together with project and not provide pre-compiled library? That would introduce some time penalty for complete rebuilds, but they aren't required pretty often I think.

@bpietsch
Copy link

bpietsch commented Jun 5, 2019

Or provide two versions of the library and choose which to link to based on whether rtti was used in the project via the makefile?

@v1993
Copy link
Contributor

v1993 commented Jun 5, 2019

@bpietsch Yep, that would work as well I guess. Options from "menuconfig" can be read from CMake/Makefiles.

@igrr
Copy link
Member

igrr commented Jun 5, 2019

Can't you build libstdc++ together with project and not provide pre-compiled library?

It's not exactly trivial for a couple of reasons. First, it is hard to separate libstdc++v3 from the rest of the GCC repository, and still have the correct ./configure behavior. Second, libstdc++v3 has a dependency on autoconf and make. The former was never a requirement for ESP-IDF, the latter is not a requirement for the users of CMake build system.

Or provide two versions of the library and choose which to link to based on whether rtti was used in the project via the makefile?

This is possible, but will need some tweaks to the toolchain build process. Currently we use crosstool-NG to build the toolchain, and building multiple copies of libstdc++.a is not supported out of the box there.

GCC's multilib support is another option, we already use it to produce a separate set of libraries for the PSRAM case. However that would result in bundling of extra copies of libc.a, libm.a, libgcc.a, which do not actually depend on RTTI.

Yet another option is to keep RTTI enabled in libstdc++ build, but modify the linker script to place typeinfo structures into a non-loadable section, so that they would not contribute to the binary size, while technically still be linked into the application.

We will be exploring these options and this issue will be updated when there is some progress.

@bpietsch
Copy link

bpietsch commented Jun 5, 2019

Thank you Ivan, appreciate your efforts to come to a resolution on this!

@davidthings
Copy link

davidthings commented Aug 7, 2019

Hi! My development just came to a halt with this problem too. We have a lot of C++ code that does rely on RTTI, so RTTI must be on. Sadly this causes an "undefined reference to typeinfo for std::thread::_State" error when linking. It looks like this problem is limited to a couple of calls into the standard thread library - creating threads.

It's hard for me to see a work around here. And I for one agree with the poster at the top who says that leaving RTTI default on (RTTI users' code works, non RTTI users' code is a little bigger) might be a gentler compromise than default off (RTTI users' code doesn't work, non RTTI users' code is a little smaller)

Help! What is the latest for a resolution for this issue?

@mirronelli
Copy link

Is there at least some kind of a workaround for this? My projects are halted too because of this. I have to use older esp_idf commits to build those projects.
Ivan Grokhotkov you have mentioned in a linked post that some form of dependency could be added to libstc++.a so that the references are resolved, but there was no followup on that, how to do so. Is that still a possibility?
If not how can I force my project to be built with no RTTI support so that it builds. Anyway are there any news about how you plan to solve it?
Thanks.

igrr added a commit that referenced this issue Oct 25, 2019
Ref. #1684

This change allows RTTI to be enabled in menuconfig. For full RTTI
support, libstdc++.a in the toolchain should be built without
-fno-rtti, as it is done now.

Generally if libstdc++.a is built with RTTI, applications which do not
use RTTI (and build with -fno-rtti) could still include typeinfo
structures referenced from STL classes’ vtables. This change works
around this, by moving all typeinfo structures from libstdc++.a into
a non-loadable section, placed into a non-existent memory region
starting at address 0. This can be done because when the application
is compiled with -fno-rtti, typeinfo structures are not used at run
time. This way, typeinfo structures do not contribute to the
application binary size.

If the application is build with RTTI support, typeinfo structures are
linked into the application .rodata section as usual.

Note that this commit does not actually enable RTTI support.
The respective Kconfig option is hidden, and will be made visible when
the toolchain is updated.
igrr added a commit that referenced this issue Oct 27, 2019
Ref. #1684

This change allows RTTI to be enabled in menuconfig. For full RTTI
support, libstdc++.a in the toolchain should be built without
-fno-rtti, as it is done now.

Generally if libstdc++.a is built with RTTI, applications which do not
use RTTI (and build with -fno-rtti) could still include typeinfo
structures referenced from STL classes’ vtables. This change works
around this, by moving all typeinfo structures from libstdc++.a into
a non-loadable section, placed into a non-existent memory region
starting at address 0. This can be done because when the application
is compiled with -fno-rtti, typeinfo structures are not used at run
time. This way, typeinfo structures do not contribute to the
application binary size.

If the application is build with RTTI support, typeinfo structures are
linked into the application .rodata section as usual.

Note that this commit does not actually enable RTTI support.
The respective Kconfig option is hidden, and will be made visible when
the toolchain is updated.
igrr pushed a commit to igrr/esp-idf that referenced this issue Nov 4, 2019
Ref. espressif#1684

Also, for full RTTI support, libstdc++.a in the toolchain should be built
in both with RTTI and w/o RTTI options. Multilib with -fno-rtti
flag is used for that.

Note that this commit does not actually enable RTTI support.
The respective Kconfig option is hidden, and will be made visible when
the toolchain is updated.
espressif-bot pushed a commit that referenced this issue Nov 19, 2019
Ref. #1684

Also, for full RTTI support, libstdc++.a in the toolchain should be built
in both with RTTI and w/o RTTI options. Multilib with -fno-rtti
flag is used for that.

Note that this commit does not actually enable RTTI support.
The respective Kconfig option is hidden, and will be made visible when
the toolchain is updated.
@drony
Copy link

drony commented Dec 3, 2019

thank you. which version toolchain works with this update. Toolchain 2.1 version seems doesn't work

@igrr
Copy link
Member

igrr commented Dec 3, 2019

@drony what do you see when you run xtensa-esp32-elf-gcc --version? It should be xtensa-esp32-elf-gcc (crosstool-NG esp-2019r2) 8.2.0. If it isn't, please follow the docs for installing the tools: https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html#step-3-set-up-the-tools

@drony
Copy link

drony commented Dec 3, 2019

yes it's same version
xtensa-esp32-elf-gcc (crosstool-NG esp-2019r2) 8.2.0
also i'm using idf 4.0beta2
but idf.py menuconfig doesn't show setttings under compiler menu

@igrr
Copy link
Member

igrr commented Dec 3, 2019

Ah, that's right. RTTI support is not part of IDF release/v4.0 branch. It has been added in master in commit 959ca74, and will be part of the next release (IDF 4.1).

@drony
Copy link

drony commented Dec 3, 2019

:( when will this version be released? beta or preview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature Request Feature request for IDF
Projects
None yet
Development

No branches or pull requests

8 participants