Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

A compiler option, a hidden visibility, and a weak symbol walk into a bar

October 27, 2021
Serge Guelton
Related topics:
CompilersC, C#, C++Linux
Related products:
Red Hat Enterprise Linux

    The LLVM packaging team recently ran into a compiler problem. A build of the LLVM package with Clang, with link-time optimization activated, failed validation. This article steps through how we explored, identified, and ultimately fixed the problem.

    The LLVM package build

    The build on the Fedora Rawhide version should be an easy task at the packaging level:

    diff --git a/llvm.spec b/llvm.spec
    (...)
    @@ -1,3 +1,5 @@
    +%global toolchain clang
    (...)
    +BuildRequires: clang
    

    Now, let's introduce the characters in the mystery to follow.

    Disabling runtime type information

    By default, LLVM compiles with the -fno-rtti option, which disables runtime type information. According to the LLVM coding standard, the compiler disables the information to reduce code and executable size.

    Yet, sometimes, a type must be associated with a unique identifier. One example involves llvm::Any, the LLVM version of std::any. A typical implementation of std::any involves typeid, as showcased by the libcxx version.

    The typeid operator cannot be used with the -fno-rtti option, so we had to find an alternative. The current implementation of llvm::Any and std::any can be mocked by the following snippet, mapping &TypeId<MyType>::Id to a unique identifier:

    template <typename T> struct TypeId { static const char Id; };
    

    Hidden visibility

    Parts of LLVM are compiled with -fvisibility=hidden. This option forces the default visibility of all symbols to be hidden, which prevents them from being visible across library boundaries. Hiding symbols offers better control over exported symbols in a shared library.

    What happens when the TypeId construct from the previous section is combined with hidden visibility? Let's compile two shared libraries out of the same code:

    template <typename T> struct TypeId { static const char Id; };
    template <typename T> const char TypeId<T>::Id = 0;
    #ifdef FOO
        const char* foo() { return &TypeId<int>::Id; }
    #else
        const char* bar() { return &TypeId<int>::Id; }
    #endif
    

    We compile two binaries, one with FOO defined and one without, to carry out the different #ifdef paths:

    > clang++ -DFOO foo.cpp -shared -o libfoo.so
    > clang++       foo.cpp -shared -o libbar.so
    

    Without hidden visibility, both libraries place our Id at the same address:

    > llvm-nm -C libfoo.so
    (...)
    0000000000000679 V TypeId<int>::Id
    > llvm-nm -C libbar.so
    (...)
    0000000000000679 V TypeId<int>::Id
    

    The V in the output indicates that the symbol is a weak object, which means that only one of the items will be chosen by the linker. Therefore, we keep unicity of the symbol and its address across compilation units.

    But when compiled with -fvisibility-hidden, the symbols no longer are weak:

    > clang++ -fvisibility=hidden foo.cpp -shared -o libbar.so
    > llvm-nm -C libfoo.so
    (...)
    0000000000000629 r TypeId<int>::Id
    

    The r next to the address means that the symbol is in a read-only data section. The symbol is not dynamically linked (as we can confirm from the output of llvm-nm -D), so it gets different addresses in libfoo and libbar. In short, unicity is not preserved.

    Fine-grained control over symbol visibility

    A straightforward fix for the incompatibility we've uncovered is to explicitly state that TypeId::Id must always have the default visibility. We can make this change as follows:

    template <typename T> struct __attribute__((visibility("default"))) TypeId { static const char Id; };
    

    Let's check that the fix works:

    > clang++ -fvisibility=hidden foo.cpp -shared -o libbar.so
    > llvm-nm -C libfoo.so
    (...)
    0000000000000659 V TypeId<int>::Id
    

    The V for a weak symbol has returned, but that's not the end of the story.

    Instead of parameterizing TypeId by int, let's parameterize it by a HiddenType class declared with hidden visibility:

    struct HiddenType {};
    template <typename T> struct __attribute__((visibility("default"))) TypeId { static const char Id; };
    template <typename T> const char TypeId<T>::Id = 0;
    const char* foo() { return &TypeId<HiddenType>::Id; }
    

    When compiling this code with -fvisibility-hidden, where does TypeId<HiddenType>::Id end up?

    > clang++ -fvisibility=hidden foo.cpp -shared -o libbar.so
    > llvm-nm -CD libbar.so | grep -c TypeId<HiddenType>::Id
    0
    

    Fascinating! This exercise shows that a template function with default visibility, instantiated with a type of hidden visibility, ends up with hidden visibility. Indeed, flagging HiddenType with __attribute__((visibility("default"))) restores the expected behavior.

    Where theory meets LLVM

    Once we isolated the behavior described in the preceding section, we could easily provide the relevant patches in LLVM:

    • Force visibility of llvm::Any to external
    • Fine grain control over some symbol visibility
    • Add extra check for llvm::Any::TypeId visibility

    These patches fix the build issue mentioned at the beginning of the article and ensure that it won't reproduce, which is the kind of outcome programmers always look for.

    Acknowledgments

    The author would like to thank Béatrice Creusillet, Adrien Guinet, and the editorial team for their help on this article.

    Last updated: August 14, 2023

    Related Posts

    • Optimizing the Clang compiler’s line-to-offset mapping

    • Profile-guided optimization in Clang: Dealing with modified sources

    • Customize the compilation process with Clang: Making compromises

    • A look at LLVM Advanced Data Types and trivially copyable types

    Recent Posts

    • Preventing GPU waste: A guide to JIT checkpointing with Kubeflow Trainer on OpenShift AI

    • How to manage TLS certificates used by OpenShift GitOps operator

    • Configure a split disk on OpenShift Container Platform

    • Red Hat Enterprise Linux 10.2 and 9.8: Top features for developers

    • What GPU kernels mean for your distributed inference

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.