[PATCH] libstdc++: Install <bits/indirect.h> for freestanding builds [PR127476]

Jonathan Wakely jwakely.gcc@gmail.com
Tue Oct 20 15:04:24 GMT 2026


On Tue, 20 Oct 2026, 12:56 Bourbon Yang, <bin.macchiato@gmail.com> wrote:

> Thanks! The plan makes total sense. I'll take this on, prepare the patch
> covering indirect and polymorphic, and post it once tested.
>

Great, thanks.



> ________________________________________
> From: Jonathan Wakely <jwakely@redhat.com>
> Sent: Tuesday, October 20, 2026 4:16 AM
> To: Bourbon Yang
> Cc: gcc-patches@gcc.gnu.org; libstdc++@gcc.gnu.org
> Subject: Re: [PATCH] libstdc++: Install <bits/indirect.h> for freestanding
> builds [PR127476]
>
> On Tue, 20 Oct 2026 at 03:51, Bourbon Yang <bin.macchiato@gmail.com>
> wrote:
> >
> > Thanks for the clarification!
> >
> > Just to briefly mention, I'm Bourbon who sent the patch based on the
> earlier suggestion, not Charlie who originally filed the bug report,
>
> Oh sorry, I got mixed up and thought you'd filed the bug report too.
>
> > but I completely understand the rationale — installing a header that
> ends up essentially empty in freestanding isn't the right way to go.
> >
> > Regarding making <bits/indirect.h> actually work in freestanding: if no
> one is currently tackling this, I would be very interested in taking a look
> and working on it.
> >
> > Before diving into the code, are there any known major roadblocks (e.g.
> allocator/exception dependencies or WG21 freestanding wording details) that
> I should pay particular attention to, or would you recommend simply
> starting with stripping out hosted-only headers/features under #if
> _GLIBCXX_HOSTED?
>
> std::allocator is not freestanding (although we could consider
> changing that too... or maybe define std::allocator as an empty class
> for freestanding?)
>
> That means that #include <bits/allocator.h> in bits/indirect.h needs
> to be guarded with _GLIBCXX_HOSTED. Same for #include
> <bits/memory_resource.h>. And the default template argument that uses
> std::allocator needs to be hosted-only, and the deduction guide that
> depends on that default template argument. So we need:
>
>   --- a/libstdc++-v3/include/bits/indirect.h
> +++ b/libstdc++-v3/include/bits/indirect.h
> @@ -37,21 +37,28 @@
> #if __glibcxx_indirect || __glibcxx_polymorphic // >= C++26
> #include <compare>
> #include <initializer_list>
> -#include <bits/allocator.h>
> #include <bits/alloc_traits.h>
> #include <bits/allocated_ptr.h>   // __allocate_guarded
> #include <bits/uses_allocator.h>  // allocator_arg_t
> #include <bits/utility.h>         // __is_in_place_type_v
> #include <bits/functional_hash.h> // hash
> -#include <bits/memory_resource.h> // polymorphic_allocator
> +#if _GLIBCXX_HOSTED
> +# include <bits/allocator.h>
> +# include <bits/memory_resource.h> // polymorphic_allocator
> +#endif
>
> namespace std _GLIBCXX_VISIBILITY(default)
> {
> _GLIBCXX_BEGIN_NAMESPACE_VERSION
>
> #if  __glibcxx_indirect
> +#if _GLIBCXX_HOSTED
>   template<typename _Tp, typename _Alloc = allocator<_Tp>>
>     class indirect;
> +#else
> +  template<typename _Tp, typename _Alloc>
> +    class indirect;
> +#endif
>
>   template<typename _Tp>
>     constexpr bool __is_indirect = false;
> @@ -425,8 +432,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>       pointer _M_objp; // Pointer to the owned object.
>     };
>
> +#if _GLIBCXX_HOSTED
>   template<typename _Value>
>     indirect(_Value) -> indirect<_Value>;
> +#endif
>
>   template<typename _Alloc, typename _Value>
>     indirect(allocator_arg_t, _Alloc, _Value)
>
> That fails for a freestanding build because <bits/allocated_ptr.h>
> isn't installed, so that can be fixed by also adding that to
> bits_freestanding in include/Makefile.am (as you did for
> bits/indirect.h in your first patch).
>
> With those changes (and reverting the change I just pushed to <memory>
> so that bits/indirect.h is included), I can include <memory> in
> freestanding with -D__glibcxx_indirect defined to override the macro
> in <version>. So that seems to work. The next step would be to remove
> the is_hosted=yes line for the 'indirect' macro in
> include/bits/version.def.
>
> And then make similar changes for std::polymorphic.
>
> After all that, I think it should be possible to use indirect and
> polymorphic in freestanding, if you provide a custom allocator. But we
> should consider enabling std::allocator too, because that only depends
> on ::operator new, and we provide that in libsupc++ for freestanding.
> But I didn't look into how hard it would be to install
> <bits/allocator.h> and make that work. Maybe we would want to require
> --enable-libstdcxx-allocator=new when building for freestanding, so
> that we only support bits/new_allocator.h and not the alternative
> allocator base classes (mt_alloc, bitmap_allocator, etc.)
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20261020/7289fd20/attachment.htm>


More information about the Libstdc++ mailing list