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

Bourbon Yang bin.macchiato@gmail.com
Wed Nov 4 12:56:01 GMT 2026


Hi Jonathan,

Gentle ping on the patch I sent on Oct 21, following your guidance from Oct 20:

https://gcc.gnu.org/pipermail/gcc-patches/2026-October/735548.html

I understand the simpler fix has already been committed and the bug is marked fixed. This patch mainly adjusts the freestanding build so that std::indirect and std::polymorphic can be used with a custom allocator.

Could you let me know if there are any issues with this version, or if it is still of interest?

If the approach looks good, I would also be happy to continue with the next step you mentioned (making std::allocator available in freestanding). Please let me know if that is something you would like pursued now, or if it should wait.

Thanks again for the earlier guidance.


________________________________________
From: Bourbon Yang <bin.macchiato@gmail.com>
Sent: Wednesday, October 21, 2026 2:24 AM
To: Jonathan Wakely
Cc: Jonathan Wakely; gcc-patches; libstdc++
Subject: Re: [PATCH] libstdc++: Install <bits/indirect.h> for freestanding builds [PR127476]

Thanks for the detailed guidance! I've familiarized myself with the
build system and implemented the changes you suggested.

This patch makes <bits/indirect.h> available for freestanding by:
- Guarding <bits/allocator.h> and <bits/memory_resource.h> with _GLIBCXX_HOSTED.
- Providing non-default template declarations for indirect and polymorphic
  in freestanding mode, and guarding the single-argument deduction guide.
- Adding <bits/allocated_ptr.h> and <bits/indirect.h> to bits_freestanding.
- Removing hosted = yes for indirect and polymorphic in version.def.
- Reverting the temporary hosted guard in <memory>.
- Adding a regression testcase in testsuite/std/memory/indirect/127476.cc.

Note: Since I don't have a freestanding cross-compiler environment
configured locally, I haven't run the DejaGnu testsuite against a
freestanding target myself.

Please let me know if any adjustment is needed. Thanks again for your patience and help!



________________________________________
From: Jonathan Wakely <jwakely.gcc@gmail.com>
Sent: Tuesday, October 20, 2026 8:04 AM
To: Bourbon Yang
Cc: Jonathan Wakely; gcc-patches; libstdc++
Subject: Re: [PATCH] libstdc++: Install <bits/indirect.h> for freestanding builds [PR127476]



On Tue, 20 Oct 2026, 12:56 Bourbon Yang, <bin.macchiato@gmail.com<mailto: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<mailto:jwakely@redhat.com>>
Sent: Tuesday, October 20, 2026 4:16 AM
To: Bourbon Yang
Cc: gcc-patches@gcc.gnu.org<mailto:gcc-patches@gcc.gnu.org>; libstdc++@gcc.gnu.org<mailto:libstdc%2B%2B@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<mailto: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.)



More information about the Libstdc++ mailing list