symbol wrapping for libcobalt
Since its very inception, the POSIX API in xenomai3 enables automatic wrapping of POSIX calls to their libcobalt counterpart using a linker trick (-Wl,--wrap), so that applications can use the pristine POSIX routine names to invoke Xenomai services. For instance, the following snippet:
ret = pthread_create(&tid, &attr, foo_routine, NULL);
would translate internally to:
ret = __wrap_pthread_create(&tid, &attr, foo_routine, NULL);
which is a weak symbol alias to:
ret = __cobalt_pthread_create(&tid, &attr, foo_routine, NULL);
The significant part of this wrapping is that we go from symbol_x in the source code to __cobalt_symbol_x at link time, with libcobalt implementing the latter.
Over the years, we noticed several issues with this scheme:
- because turning on wrapping only happens at link time, there is no indication whatsoever that some object file does expect wrapping to be applied by the linker when producing the executable. For instance, compiling such object file with build flags obtained from xeno-config --cobalt --cflags then linking with xeno-config --posix --ldflags would successfully create an executable, which would send all POSIX calls to the underlying *libc, which may or may not be the intent, depending on which xeno-config call was wrong. When building an application from multiple parts which are not necessarily maintained and built in the same team/organization which uses them (e.g. 3rd party libs), this issue is a source of headaches. At the very least, we need some guardrails to prevent this from going unnoticed.
- symbols in static archives are always wrapped although we may not want this. Likewise, symbols in shared libs are not wrapped although we may want the opposite. We have to resort to wrap-link.sh to accommodate complex library dependencies, which makes the process quite brittle since correctness here depends on the order of appearance of each object and dependency in the link args. A badly ordered link sequence successfully produces an executable, but the latter behaves incorrectly.
For instance, say we have lib3dparty.a which is a mere in-band library that does not know about Xenomai, but is used by a Xenomai application for non-rt business by threads it starts using some mundane library service (e.g. libstdc++, via std::thread). Some foo() service lib3dparty.a provides serializes callers using a pthread_mutex_t lock.
We then link our rt app using wrap-link.sh, with lib3dparty.a appearing before libcobalt because this seems to be the most natural order to most users at first glance. Which would give this under the hood:
g++ -static -pthread -o app main.o -Wl,@cobalt.wrappers -lthirdparty -lcobalt -lpthread # stage 1: g++ -o app.tmp -r -nostdlib -static -pthread main.o -Wl,@cobalt.wrappers -lthirdparty # stage 2: g++ -o app app.tmp -static -pthread -lcobalt -lpthread
Because lib3dparty.a comes before -lcobalt, the library goes into the --wrap stage, and its lock is set up as a Cobalt mutex by some init call in that library. Statically linking against libstdc++.a does not help, because this dependency is injected on stage 2 when wrapping is no more enabled in the link args. Mere dynamic linking against libstdc++.so would not help either, since symbols defined in a DSO escape wrapping entirely.
And now the fireworks are set off: when the std::thread workers attempt to lock the mutex using the plain glibc service, they fail. At best by crashing, at worse with EPERM. Not everyone checks pthread_mutex_lock() return codes, right? So now we have multiple workers stepping on each others toes when accessing a shared resource. Zero indication of this happening except maybe weird application output or crash. Perfect day.
We could use C11 thrd_create() the same way to create workers and get into the exact same pitfalls without involving libstdc++ at all.
- a POSIX object such as a mutex, condvar or semaphore can be set up on one side and used on another, and we have no way to detect it; we usually receive obscure errors at runtime, in the best case the application crashes. Things get uglier with C++ when inline header code gets wrapped and out-of-line parts are not.
- the y2038 issue makes things worse: v3 needs to keep yet another hand-written list of wrappers for this,
i.e. cobalt-glibc-time64.wrappers so that time64 calls in 32bit builds end up at the right place. The day the underlying *libc decides to redirect other symbols to whatever build-specific counterpart (not exclusively time64), we can only notice this after a runtime breakage.
Also, since the glibc headers know nothing about the __real_* symbols, they won't redirect them to the proper 64bit implementation but keep branching to their legacy 32bit variant, unless they are declared with COBALT_DECL_TIME64. It looks like v3 may have at least fcntl, and all the socket *msg calls escaping redirection right now. There is another example, which is harmless this time fortunately, with wrapping pthread_yield(). A recent glibc installs a redirection for this routine:
extern int __REDIRECT_NTH (pthread_yield, (void), sched_yield)
This means that __cobalt_pthread_yield() is not called when such redirect is in place. The only reason that keeps it working is that pthread_yield() is redirected to sched_yield(), and we do intercept sched_yield() anyway.
- when link-time optimization is involved, any references the intermediate representation may add escapes wrapping. Once again, this is 100% opaque to us, we have no idea what happens under the hood.
- there is a cost to implicit wrapping for I/O calls such as open(), read(), write(), ioctl() and friends: we have to try the Xenomai side first, then fallback to the regular *libc side if that fails. Granted, this is only a problem for in-band code, but still: although SCHED_WEAK threads are running in-band by convention and oob by exception, they should not have to pay twice the price for each wrapped I/O system call.
- on a more general note, we times and often recommend that a clear real-time vs non real-time split be enforced in any application which runs in a dual kernel environment (I'd argue that this should be a rule of thumb for all rt designs, even those served by native preemption as well). Doing so is basic sanity, so that responsibilities and constraints are obvious from the get go when reviewing the implementation, and we don't create situations in which rt-unsafe code is called from a time-critical path too easily. In that sense, symbol wrapping blurs the boundaries between rt and non-rt, which contradicts this recommendation.
I believe that the last point carries a lot of weight in favor of explicit annotation of libcobalt calls in the application code (i.e. __COBALT(), __RT(), __EVL()). However, I'm not recommending that link-wrapping is dropped from v4, especially since we need an easy migration path from v3, but at the very least v4 should prevent misuses, help in figuring out where problems lie with symbol wrapping. What I'd like to see in v4:
- a way to somehow mark the object files that did include the libcobalt headers, so that linking them without -Wl,--wrap fails.
- conversely, a way to mark the object files that did _not_ include the libcobalt headers but the regular glibc ones, so that linking them _with_ -Wl,--wrap fails when the wrap list contains inconsistently used symbols.
This metadata could be used by a checker tool that would answer the following question: "where the heck each call to libcobalt services actually goes for this executable"? It could also be used to generate the wrap list on the fly for symbols which carry the 'wrapped' marker by inspecting the object list.
Icing on the cake, it should even be possible to use this information to implement auto-annotation of source code by reading a wrapped binary, i.e. patching in the __COBALT() annotations mechanically, for those who want an easy way to depart from symbol wrapping for an existing application.
Thoughts welcome.
Signature · 1 line
Philippe.
Le lundi 23 novembre 2026 à 21:33 +0100, Philippe Gerum a écrit :
Quoted 17 lines
Since its very inception, the POSIX API in xenomai3 enables automatic wrapping of POSIX calls to their libcobalt counterpart using a linker trick (-Wl,--wrap), so that applications can use the pristine POSIX routine names to invoke Xenomai services. For instance, the following snippet:
ret =
pthread_create(&tid, &attr, foo_routine, NULL);would translate internally to:
ret =
__wrap_pthread_create(&tid, &attr, foo_routine, NULL);which is a weak symbol alias to:
ret =
__cobalt_pthread_create(&tid, &attr, foo_routine, NULL);
Quoted 7 lines
The significant part of this wrapping is that we go from
symbol_xin the source code to__cobalt_symbol_xat link time, with libcobalt implementing the latter.Over the years, we noticed several issues with this scheme:
[Snip]
Just saying that as a xenomai user, we share all of these concerns. Another issue is interop with eg rust code : i have no idea what happens when a rust static lib is linked onto the executable regarding the calls it may do to the pthread api (this one is actually a big concern for us for rust code integration into the rt domain, and even the non-rt domain as it's unsure how it will behave).
We've gradually migrated to explicit calls everywhere, with the goal to get rid of wrapping entirely. It looks to me that the wrapping process is too fragile indeed. For us completely dropping the wrapping would be fine, as it's on our roadmap anyway.
Actually, there's one feature that we rely upon : being able to compile for both an RT core and a standard kernel. But that is quite easy to do by just redifining the __RT and __STD macros, wrapping is not really relevant here.
Regards,
Julien
Quoted 24 lines · Julien Blanc
Le lundi 23 novembre 2026 à 21:33 +0100, Philippe Gerum a écrit :
Quoted 22 lines
Since its very inception, the POSIX API in xenomai3 enables automatic wrapping of POSIX calls to their libcobalt counterpart using a linker trick (-Wl,--wrap), so that applications can use the pristine POSIX routine names to invoke Xenomai services. For instance, the following snippet:
ret =
pthread_create(&tid, &attr, foo_routine, NULL);would translate internally to:
ret =
__wrap_pthread_create(&tid, &attr, foo_routine, NULL);which is a weak symbol alias to:
ret =
__cobalt_pthread_create(&tid, &attr, foo_routine, NULL);The significant part of this wrapping is that we go from
symbol_xin the source code to__cobalt_symbol_xat link time, with libcobalt implementing the latter.Over the years, we noticed several issues with this scheme:
Quoted 9 lines
Quoted 1 line
[Snip]
Just saying that as a xenomai user, we share all of these concerns. Another issue is interop with eg rust code : i have no idea what happens when a rust static lib is linked onto the executable regarding the calls it may do to the pthread api (this one is actually a big concern for us for rust code integration into the rt domain, and even the non-rt domain as it's unsure how it will behave).
Rust's rlib format is basically a static ar(chive) in disguise, storing one object file which contains some build metadata, and any number of so-called "Rust codegen units", optionally with additional files generated from other languages (C/C++), so we may assume that wrapping is applicable. This said, you are likely going through other crates which call the *libc eventually (e.g. std, libc, nix, rustix et al), your own crates are unlikely to emit references to these symbols directly, but to mangled Rust names implementing some FFI. For some of the reasons explained in my RFC about link stage issues, I would not expect wrapping to work out of the box in this case, especially with the cargo toolchain in the loop, at least not in any intuitive and POLA fashion.
This is why v4 comes with the 'revl' crate, which provides the bindings required to leverage the libevl (original) API calls in Rust abstractions. Extending revl to introduce abstractions covering the portions of the POSIX API which were made oob-aware like syslog support is a non-issue: these portions are based on the libevl intrinsics, which are plain custom C functions, we'd need no wrapping there.
Quoted 8 lines
We've gradually migrated to explicit calls everywhere, with the goal to get rid of wrapping entirely. It looks to me that the wrapping process is too fragile indeed. For us completely dropping the wrapping would be fine, as it's on our roadmap anyway.
Actually, there's one feature that we rely upon : being able to compile for both an RT core and a standard kernel. But that is quite easy to do by just redifining the
__RTand__STDmacros, wrapping is not really relevant here.
We are on the same page. Wrapping may have been a good idea on paper; v4 should provide complete support for it and even beyond what v3 is capable of today (i.e. with guardrails and checkers), but only as an opt-in feature I believe, because this is too much of a footgun.
Signature · 1 line
Philippe.
Quoted 76 lines · Philippe Gerum
Since its very inception, the POSIX API in xenomai3 enables automatic wrapping of POSIX calls to their libcobalt counterpart using a linker trick (-Wl,--wrap), so that applications can use the pristine POSIX routine names to invoke Xenomai services. For instance, the following snippet:
ret = pthread_create(&tid, &attr, foo_routine, NULL);would translate internally to:
ret = __wrap_pthread_create(&tid, &attr, foo_routine, NULL);which is a weak symbol alias to:
ret = __cobalt_pthread_create(&tid, &attr, foo_routine, NULL);The significant part of this wrapping is that we go from
symbol_xin the source code to__cobalt_symbol_xat link time, with libcobalt implementing the latter.Over the years, we noticed several issues with this scheme:
- because turning on wrapping only happens at link time, there is no indication whatsoever that some object file does expect wrapping to be applied by the linker when producing the executable. For instance, compiling such object file with build flags obtained from
xeno-config --cobalt --cflagsthen linking withxeno-config --posix --ldflagswould successfully create an executable, which would send all POSIX calls to the underlying *libc, which may or may not be the intent, depending on which xeno-config call was wrong. When building an application from multiple parts which are not necessarily maintained and built in the same team/organization which uses them (e.g. 3rd party libs), this issue is a source of headaches. At the very least, we need some guardrails to prevent this from going unnoticed.- symbols in static archives are always wrapped although we may not want this. Likewise, symbols in shared libs are not wrapped although we may want the opposite. We have to resort to
wrap-link.shto accommodate complex library dependencies, which makes the process quite brittle since correctness here depends on the order of appearance of each object and dependency in the link args. A badly ordered link sequence successfully produces an executable, but the latter behaves incorrectly.For instance, say we have lib3dparty.a which is a mere in-band library that does not know about Xenomai, but is used by a Xenomai application for non-rt business by threads it starts using some mundane library service (e.g. libstdc++, via
std::thread). Somefoo()service lib3dparty.a provides serializes callers using apthread_mutex_tlock.We then link our rt app using
wrap-link.sh, with lib3dparty.a appearing before libcobalt because this seems to be the most natural order to most users at first glance. Which would give this under the hood:g++ -static -pthread -o app main.o -Wl,@cobalt.wrappers -lthirdparty -lcobalt -lpthread # stage 1: g++ -o app.tmp -r -nostdlib -static -pthread main.o -Wl,@cobalt.wrappers -lthirdparty # stage 2: g++ -o app app.tmp -static -pthread -lcobalt -lpthread
Because lib3dparty.a comes before -lcobalt, the library goes into the --wrap stage, and its lock is set up as a Cobalt mutex by some init call in that library. Statically linking against libstdc++.a does not help, because this dependency is injected on stage 2 when wrapping is no more enabled in the link args. Mere dynamic linking against libstdc++.so would not help either, since symbols defined in a DSO escape wrapping entirely.
And now the fireworks are set off: when the
std::threadworkers attempt to lock the mutex using the plain glibc service, they fail. At best by crashing, at worse with EPERM. Not everyone checkspthread_mutex_lock()return codes, right? So now we have multiple workers stepping on each others toes when accessing a shared resource. Zero indication of this happening except maybe weird application output or crash. Perfect day.
Quoted 3 lines
We could use C11
thrd_create()the same way to create workers and get into the exact same pitfalls without involving libstdc++ at all.
To be fair, using language provided threading and locking is generally are recipe for desaster on PREEMPT-RT as well because most are non-RT only.
Quoted 61 lines
- a POSIX object such as a mutex, condvar or semaphore can be set up on one side and used on another, and we have no way to detect it; we usually receive obscure errors at runtime, in the best case the application crashes. Things get uglier with C++ when inline header code gets wrapped and out-of-line parts are not.
- the y2038 issue makes things worse: v3 needs to keep yet another hand-written list of wrappers for this,
i.e. cobalt-glibc-time64.wrappers so that time64 calls in 32bit builds end up at the right place. The day the underlying *libc decides to redirect other symbols to whatever build-specific counterpart (not exclusively time64), we can only notice this after a runtime breakage.Also, since the glibc headers know nothing about the __real_* symbols, they won't redirect them to the proper 64bit implementation but keep branching to their legacy 32bit variant, unless they are declared with
COBALT_DECL_TIME64. It looks like v3 may have at least fcntl, and all the socket *msg calls escaping redirection right now. There is another example, which is harmless this time fortunately, with wrappingpthread_yield(). A recent glibc installs a redirection for this routine:extern int
__REDIRECT_NTH(pthread_yield, (void),sched_yield)This means that
__cobalt_pthread_yield()is not called when such redirect is in place. The only reason that keeps it working is thatpthread_yield()is redirected tosched_yield(), and we do interceptsched_yield()anyway.- when link-time optimization is involved, any references the intermediate representation may add escapes wrapping. Once again, this is 100% opaque to us, we have no idea what happens under the hood.
- there is a cost to implicit wrapping for I/O calls such as
open(),read(),write(),ioctl()and friends: we have to try the Xenomai side first, then fallback to the regular *libc side if that fails. Granted, this is only a problem for in-band code, but still: althoughSCHED_WEAKthreads are running in-band by convention and oob by exception, they should not have to pay twice the price for each wrapped I/O system call.- on a more general note, we times and often recommend that a clear real-time vs non real-time split be enforced in any application which runs in a dual kernel environment (I'd argue that this should be a rule of thumb for all rt designs, even those served by native preemption as well). Doing so is basic sanity, so that responsibilities and constraints are obvious from the get go when reviewing the implementation, and we don't create situations in which rt-unsafe code is called from a time-critical path too easily. In that sense, symbol wrapping blurs the boundaries between rt and non-rt, which contradicts this recommendation.
I believe that the last point carries a lot of weight in favor of explicit annotation of libcobalt calls in the application code (i.e.
__COBALT(),__RT(),__EVL()). However, I'm not recommending that link-wrapping is dropped from v4, especially since we need an easy migration path from v3, but at the very least v4 should prevent misuses, help in figuring out where problems lie with symbol wrapping. What I'd like to see in v4:
Quoted 3 lines
- a way to somehow mark the object files that did include the libcobalt headers, so that linking them without -Wl,--wrap fails.
Already some idea for how this could be implemented?
Quoted 4 lines
- conversely, a way to mark the object files that did _not_ include the libcobalt headers but the regular glibc ones, so that linking them _with_ -Wl,--wrap fails when the wrap list contains inconsistently used symbols.
Well, the absence of the marker above must be enough in order to detect pre-built standard libs.
Quoted 7 lines
This metadata could be used by a checker tool that would answer the following question: "where the heck each call to libcobalt services actually goes for this executable"? It could also be used to generate the wrap list on the fly for symbols which carry the 'wrapped' marker by inspecting the object list.
Quoted 8 lines
Icing on the cake, it should even be possible to use this information to implement auto-annotation of source code by reading a wrapped binary, i.e. patching in the
__COBALT()annotations mechanically, for those who want an easy way to depart from symbol wrapping for an existing application.Thoughts welcome.
Having wrapping as an opt-in layer on top of an explicit POSIX-compatible API sounds fair to me. Then only users who need the wrapping for a good reason can select that separately. Technically, users could already do that with Xenomai 3 today, but it is not presented that way, thus likely only few will do it.
Jan
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Quoted 8 lines · Jan Kiszka
Quoted 3 lines · Philippe Gerum
We could use C11
thrd_create()the same way to create workers and get into the exact same pitfalls without involving libstdc++ at all.To be fair, using language provided threading and locking is generally are recipe for desaster on PREEMPT-RT as well because most are non-RT only.
Agreed. Also known as "The Fine Print" of the "Real-time Everywhere, Go Wild, We Care!" promise.
Quoted 6 lines
Quoted 3 lines
- a way to somehow mark the object files that did include the libcobalt headers, so that linking them without -Wl,--wrap fails.
Already some idea for how this could be implemented?
Some, still at the early stage though. I've been experimenting with them lately. Not far away from a solution, but there is still some work ahead to keep it simple. I'll follow-up on this.
Quoted 8 lines
Quoted 4 lines
- conversely, a way to mark the object files that did _not_ include the libcobalt headers but the regular glibc ones, so that linking them _with_ -Wl,--wrap fails when the wrap list contains inconsistently used symbols.
Well, the absence of the marker above must be enough in order to detect pre-built standard libs.
I don't think so. We need a way to actually claim that we don't want the wrapping step to happen on a set of objects we manage, while excluding libraries which plays no part in the Xenomai system from the check. Otherwise, we would reject libgcc.a, crt.o, plus any unrelated 3rd-party library on the ground that they don't claim anything. IOW, having no marker does not mean that we claim that we should not have one, it just means that we don't care (in most cases because we don't even know about Xenomai in the first place). So these are two separate issues. Tricky.
Quoted 13 lines
Quoted 13 lines
This metadata could be used by a checker tool that would answer the following question: "where the heck each call to libcobalt services actually goes for this executable"? It could also be used to generate the wrap list on the fly for symbols which carry the 'wrapped' marker by inspecting the object list.
Icing on the cake, it should even be possible to use this information to implement auto-annotation of source code by reading a wrapped binary, i.e. patching in the
__COBALT()annotations mechanically, for those who want an easy way to depart from symbol wrapping for an existing application.
Quoted 9 lines
Quoted 2 lines
Thoughts welcome.
Having wrapping as an opt-in layer on top of an explicit POSIX-compatible API sounds fair to me. Then only users who need the wrapping for a good reason can select that separately. Technically, users could already do that with Xenomai 3 today, but it is not presented that way, thus likely only few will do it.
True, as wrapping even appears to be recommended since it is used throughout the examples, tests and so on in our code base. Opt-in is clearly the proper way.
Signature · 1 line
Philippe.