[libcxx-dev] [llvm-dev] Shipping custom libc++ on MacOS

Louis Dionne via libcxx-dev libcxx-dev at lists.llvm.org
Tue Feb 2 12:45:42 PST 2021



> On Feb 1, 2021, at 16:03, Ken Cunningham <ken.cunningham.webuse at gmail.com> wrote:
> 
> On MacPorts we are just getting into this issue, in our case trying to build a new libc++.dylib that will be accepted by the existing system frameworks on 10.7 to 10.13 to support c++17 software on older systems without resorting to libstdc++ from gcc.
> 
> Is this macro of value in helping avoid ODR violations?  I’m just investigating that now:
> 
>  _LIBCPP_HIDE_FROM_ABI_PER_TU
> 

Pasting from the documentation:

**_LIBCPP_HIDE_FROM_ABI_PER_TU**
  This macro controls whether symbols hidden from the ABI with `_LIBCPP_HIDE_FROM_ABI`
  are local to each translation unit in addition to being local to each final
  linked image. This macro is defined to either 0 or 1. When it is defined to
  1, translation units compiled with different versions of libc++ can be linked
  together, since all non ABI-facing functions are local to each translation unit.
  This allows static archives built with different versions of libc++ to be linked
  together. This also means that functions marked with `_LIBCPP_HIDE_FROM_ABI`
  are not guaranteed to have the same address across translation unit boundaries.

  When the macro is defined to 0, there is no guarantee that translation units
  compiled with different versions of libc++ can interoperate. However, this
  leads to code size improvements, since non ABI-facing functions can be
  deduplicated across translation unit boundaries.

  This macro can be defined by users to control the behavior they want from
  libc++. The default value of this macro (0 or 1) is controlled by whether
  `_LIBCPP_HIDE_FROM_ABI_PER_TU_BY_DEFAULT` is defined, which is intended to
  be used by vendors only (see below).

Basically, this macro allows mixing different TUs or static archives built with different versions of libc++. That is relevant if you have multiple versions of libc++ within the same executable, which doesn't appear to be your case here.

Louis


> Ken
> 
> 
> 
>> On Feb 1, 2021, at 12:19 PM, Louis Dionne via libcxx-dev <libcxx-dev at lists.llvm.org <mailto:libcxx-dev at lists.llvm.org>> wrote:
>> 
>> 
>> 
>>> On Feb 1, 2021, at 15:16, Louis Dionne via llvm-dev <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote:
>>> 
>>> 
>>> 
>>>> On Feb 1, 2021, at 02:37, Tobias Hieta via libcxx-dev <libcxx-dev at lists.llvm.org <mailto:libcxx-dev at lists.llvm.org>> wrote:
>>>> 
>>>> Hello,
>>>> 
>>>> We are working on an application where we want to have full support
>>>> for C++17 but still ship on older versions of macOS (10.9/10.10 in
>>>> this case). Our plan was to build our own libc++ from llvm and ship it
>>>> with the application and then pass "-D_LIBCPP_DISABLE_AVAILABILITY"
>>>> 
>>>> This seemed to work fine in internal testing - but then we ran into a
>>>> similar issue as the one described here:
>>>> https://reviews.llvm.org/D74489#2497044 <https://reviews.llvm.org/D74489#2497044>
>>>> 
>>>> Now I can patch my copy of libc++ with the upstream fix for this - but
>>>> the comment below made me think otherwise:
>>>> https://reviews.llvm.org/D74489#2505156 <https://reviews.llvm.org/D74489#2505156>
>>>> 
>>>> We have run into problem where if you ship your own libc++ the system
>>>> frameworks loads the system libc++ and some strange errors happen - we
>>>> thought we could work around these issues - but maybe this is just us
>>>> being naive.
>>>> 
>>>> So I guess my question is - is it possible to ship a custom libc++ in
>>>> a .app archive for older versions of macOS in order to use C++17? Or
>>>> is my only hope to raise the minimum version of our app and always
>>>> link to the system libc++?
>>> 
>>> Generally, I would say this is a tricky thing to do. As you mention, the problem is that if you link against any other library that does link against the system-provided libc++.dylib, you'll have ODR violations because some symbols will be found in both libraries. As a result, I believe you could also end up in a situation where you end up using globals from one library when the globals from the other library has been initialized. So basically, nothing good happens.
>>> 
>>> If you can ensure that your application doesn't pull in the system libc++.dylib (even transitively), then this would in theory be safe to do. Due to the brittleness of the solution, my immediate reaction would be to not recommend that. The reason why some parts of C++17 aren't available on older platforms is that we put the vtables required for exception classes in the dylib. That is a code size optimization that we think is worth the trouble. You can generally use the parts of C++17 APIs that can't throw exceptions even on older platforms because they don't require those vtable symbols - perhaps that is something you can consider?
>> 
>> Also, Chrome is known to do something similar to what you're inquiring about. They build libc++ statically with hidden visibility, and then they link it into their application. I assume they also make sure to not export any symbol from libc++ that would be created by using the libc++ headers (e.g. by instantiating some function template). I don't know their setup very well, but I assume they make sure to not link against any other system library that does require libc++.dylib from the system, or else things get trickier as I explained above. But as long as you satisfy that requirements, I believe things have worked fairly well for them so far. You just have to be able and willing to walk that fine line.
>> 
>> Louis
>> 
>> _______________________________________________
>> libcxx-dev mailing list
>> libcxx-dev at lists.llvm.org <mailto:libcxx-dev at lists.llvm.org>
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-dev <https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-dev>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/libcxx-dev/attachments/20210202/150d9498/attachment.html>


More information about the libcxx-dev mailing list