Skip to content

c++: Define some support functions required by GCC#3107

Merged
jnohlgard merged 7 commits intoRIOT-OS:masterfrom
jnohlgard:pr/cppsupport
Aug 19, 2015
Merged

c++: Define some support functions required by GCC#3107
jnohlgard merged 7 commits intoRIOT-OS:masterfrom
jnohlgard:pr/cppsupport

Conversation

@jnohlgard
Copy link
Copy Markdown
Member

GCC needs a definition of void *__dso_handle = NULL; to handle global constructors. In order to use virtual functions there has to exist a definition of a fallback function when calling pure virtual functions to signal the error. This PR adds a new file cppsupport.cpp inside sys/cpp11-compat for providing these symbols.

TODO: also provide an implementation of new and delete for dynamic creation of objects.

@jnohlgard jnohlgard added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Area: C++ Area: C++ wrapper labels May 29, 2015
@jnohlgard jnohlgard added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels May 29, 2015
@jnohlgard
Copy link
Copy Markdown
Member Author

The cpp11_thread test fails on my machine for all Cortex-M0 platforms because of missing __atomic_fetch_sub_4. It may be caused by the way the cortex-m0 toolchain is built or the linking order of the libraries, I don't know how to fix it however.

@josephnoir
Copy link
Copy Markdown
Contributor

Sounds like I good idea. Moving the __dso_handle to a single location is much cleaner. I am not entirely sure why we need to implement newand delete, can you elaborate that?

Regarding the Cortex-M0, I have only tested the code on the stm32f4discovery boards, hence the whitelist. I will see if I can get my hands on a Cortex-M0 take a look at the error.

@jnohlgard
Copy link
Copy Markdown
Member Author

@josephnoir
you can try it with make BOARD=yunjia-nrf51822 or make BOARD=stm32f0discovery for example.

@jnohlgard
Copy link
Copy Markdown
Member Author

I did not actually test new and delete, I just assumed that they would not work properly without a custom implementation. Does GCC/newlib provide any fallbacks for those operators?

@josephnoir
Copy link
Copy Markdown
Contributor

If I recall correctly, the __dso_handle is not required on native. (And, I run into: multiple definition of '__dso_handle'.)

@josephnoir
Copy link
Copy Markdown
Contributor

Regarding the problem on the Cortex-M0 I found this two threads on launchpad: one and two. It seems we need to implement an alternative atomic header if we want them on these boards.

Or, maybe, they will offer support in the future ...

(An atomic is only used at one point, for the reference count of the thread class. Alternatively we could use a mutex to guard the reference count and remove the dependency ...)

@jnohlgard
Copy link
Copy Markdown
Member Author

@josephnoir If you can live with using C for the atomics we could change the reference count into an atomic_int_t and use atomic_inc and atomic_dec to modify it.

@jnohlgard
Copy link
Copy Markdown
Member Author

It does however not solve the problem if any users try to use C++ atomics

@jnohlgard
Copy link
Copy Markdown
Member Author

@josephnoir Thank you for your Launchpad research!

After reading through the threads I think it might be possible to add implementations of __atomic_xxxxx in case the libc does not provide them.

One question though: Should I define the __atomic_ functions as weak symbols or should they be protected by an #ifdef CORTEXM0?

From the thread on LP it seems like the CM0 newlib or similar will provide these in the future, but currently doesn't.

I will update this PR to provide simple implementations of the GCC builtin atomics.

@jnohlgard jnohlgard added the State: waiting for other PR State: The PR requires another PR to be merged first label May 30, 2015
@jnohlgard
Copy link
Copy Markdown
Member Author

Opened #3115 for Cortex M0 support.

@jnohlgard
Copy link
Copy Markdown
Member Author

Updated with __dso_handle defined weak. Fixes linker errors on native

@jnohlgard jnohlgard added this to the Release 2015.06 milestone May 31, 2015
@josephnoir
Copy link
Copy Markdown
Contributor

I can live with using an atomic implementation that works for most (or all) devices. If that is using C for atomics, so be it.

However, did I understand your later comments correctly, i.e., #3115 can offer the missing atomic calls to allow the usage of the C++ atomic header (which would be much nicer fix and lead to better C++ support)?

@jnohlgard
Copy link
Copy Markdown
Member Author

@josephnoir yes, #3115 introduces the helper functions that are necessary to support at least parts of #include <stdatomic.h> on GCC (which is also used by C++11 #include <atomic>)
We can expand on it later if we notice that we need more helpers.

https://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary is a not quite up to date page regarding the GCC 4.7 atomic work, it was the best I could find for describing the helper functions (see "list of library routines"). It mentions a couple of other functions in addition to the ones I implemented, but I got your example to compile using only the functions in #3115 . I did not try running it though, I don't have any board with an M0.

@josephnoir
Copy link
Copy Markdown
Contributor

Awesome! I can confirm that #3115 allows compiling the cpp11 tests for the samr21-xpro and the example I tested (cpp11_thread) works on the board.

@OlegHahm
Copy link
Copy Markdown
Member

OlegHahm commented Jun 1, 2015

One question though: Should I define the _atomic functions as weak symbols or should they be protected by an #ifdef CORTEXM0?

I would prefer the ifdef solution (even if it is definitely uglier), but the weak symbol is GCC dependent.

@jnohlgard
Copy link
Copy Markdown
Member Author

I don't have any desire to use dynamic memory in any application right now and I don't have time to test it.

In its current state, this PR adds the rudimentary support necessary for static constructors and virtual functions.

@josephnoir OK to postpone any new and delete tests (and any necessary function implementations) until later and merge this (after the dependencies has been merged) as a step towards proper C++ support?

@josephnoir
Copy link
Copy Markdown
Contributor

@gebart Yes, sounds good.

(The thread implementation does use a new at the moment and works on the boards I tested ...)

@jnohlgard jnohlgard added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed State: waiting for other PR State: The PR requires another PR to be merged first labels Jul 6, 2015
@jnohlgard jnohlgard added the CI: needs squashing Commits in this PR need to be squashed; If set, CI systems will mark this PR as unmergable label Jul 6, 2015
@jnohlgard
Copy link
Copy Markdown
Member Author

Rebased after #3115 was merged.

@jnohlgard
Copy link
Copy Markdown
Member Author

@josephnoir OK to squash?

@josephnoir
Copy link
Copy Markdown
Contributor

Sorry, I am traveling a lot at the moment. What does the __gnu_cxx stuff do when using clang as a compiler? Otherwise, go ahead.

@jnohlgard
Copy link
Copy Markdown
Member Author

@josephnoir the __gnu_cxx stuff will be just another namespace and it will even be GC:ed when linking as usual if it is not referenced, so it should not affect compilers/C++ libraries which does not need it. It is only used in certain GCC configurations, but it is such a tiny function that we're just wasting time if we try to exclude it with #ifdef depending on configuration.

@jnohlgard jnohlgard removed the CI: needs squashing Commits in this PR need to be squashed; If set, CI systems will mark this PR as unmergable label Jul 20, 2015
@jnohlgard jnohlgard force-pushed the pr/cppsupport branch 3 times, most recently from 2600287 to c2de3c5 Compare July 20, 2015 17:00
Joakim Gebart added 7 commits August 8, 2015 14:44
cppsupport.cpp contains functions which are necessary in order to use
some features of the C++ language, e.g. calling static constructors or
virtual functions.

TODO/Not implemented yet:
 - Test virtual functions
 - Handle exceptions (stack unwinding)
 - Run time type identification (RTTI)
 - Remove BOARD_WHITELIST
 - Clean up CXXFLAGS
 - Clean up CFLAGS
 - Blacklist boards where the C++ library is too large on Travis
 - Remove BOARD_WHITELIST
 - Clean up CXXFLAGS
 - Clean up CFLAGS
 - Blacklist boards where the C++ library is too large on Travis
 - Remove BOARD_WHITELIST
 - Clean up CXXFLAGS
 - Clean up CFLAGS
 - Blacklist boards where the C++ library is too large on Travis
@jnohlgard
Copy link
Copy Markdown
Member Author

rebased after the libfixmath_unittests build problems were fixed in #3470. Waiting for Travis.

@jnohlgard
Copy link
Copy Markdown
Member Author

Travis is fine. Anyone care to ACK this?

@PeterKietzmann
Copy link
Copy Markdown
Member

@josephnoir, @BytesGalore ping

@josephnoir
Copy link
Copy Markdown
Contributor

ACK

@OlegHahm
Copy link
Copy Markdown
Member

I see an ACK and Travis being green. @gebart, if you wanna squash, go ahead, otherwise just hit the button!

jnohlgard pushed a commit that referenced this pull request Aug 19, 2015
c++: Define some support functions required by GCC
@jnohlgard jnohlgard merged commit 56b0bee into RIOT-OS:master Aug 19, 2015
@jnohlgard jnohlgard deleted the pr/cppsupport branch August 19, 2015 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: C++ Area: C++ wrapper CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants