Skip to content

Commit

Permalink
[libcxx] Don't use -fvisibility-global-new-delete-hidden when not def…
Browse files Browse the repository at this point in the history
…ining them

When builing the hermetic static library, the compiler switch
-fvisibility-global-new-delete-hidden is necessary to get the new and
delete operator definitions made correctly. However, when those
definitions are not included in the library, then this switch does harm.
With lld (though not all linkers) setting STV_HIDDEN on SHN_UNDEF
symbols makes it an error to leave them undefined or defined via dynamic
linking that should generate PLTs for -shared linking (lld makes this a
hard error even without -z defs). Though leaving the symbols undefined
would usually work in practice if the linker were to allow it (and the
user didn't pass -z defs), this actually indicates a real problem that
could bite some target configurations more subtly at runtime. For
example, x86-32 ELF -fpic code generation uses hidden visibility on
declarations in the caller's scope as a signal that the call will never
be resolved to a PLT entry and so doesn't have to meet the special ABI
requirements for PLT calls (setting %ebx). Since these functions might
actually be resolved to PLT entries at link time (we don't know what the
user is linking in when the hermetic library doesn't provide all the
symbols itself), it's not safe for the compiler to treat their
declarations at call sites as having hidden visibility.

Differential Revision: https://reviews.llvm.org/D61571

llvm-svn: 360003
  • Loading branch information
petrhosek committed May 6, 2019
1 parent ee14310 commit 741f52c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 6 additions & 1 deletion libcxx/src/CMakeLists.txt
Expand Up @@ -349,7 +349,12 @@ if (LIBCXX_ENABLE_STATIC)

if (LIBCXX_HERMETIC_STATIC_LIBRARY)
append_flags_if_supported(CXX_STATIC_LIBRARY_FLAGS -fvisibility=hidden)
append_flags_if_supported(CXX_STATIC_LIBRARY_FLAGS -fvisibility-global-new-delete-hidden)
# If the hermetic library doesn't define the operator new/delete functions
# then its code shouldn't declare them with hidden visibility. They might
# actually be provided by a shared library at link time.
if (LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
append_flags_if_supported(CXX_STATIC_LIBRARY_FLAGS -fvisibility-global-new-delete-hidden)
endif()
target_compile_options(cxx_static PRIVATE ${CXX_STATIC_LIBRARY_FLAGS})
target_compile_definitions(cxx_static PRIVATE _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
endif()
Expand Down
11 changes: 8 additions & 3 deletions llvm/utils/gn/secondary/libcxx/src/BUILD.gn
Expand Up @@ -84,6 +84,9 @@ config("cxx_config") {
"_CRT_STDIO_ISO_WIDE_SPECIFIERS",
]
}
if (!libcxx_enable_new_delete_definitions) {
defines += [ "_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS" ]
}
if (libcxx_enable_exceptions) {
if (current_os == "win") {
cflags_cc += [ "/EHsc" ]
Expand All @@ -97,15 +100,15 @@ config("cxx_config") {
} else {
cflags_cc += [ "-fno-exceptions" ]
}
defines += [ "-D_LIBCPP_NO_EXCEPTIONS" ]
defines += [ "_LIBCPP_NO_EXCEPTIONS" ]
}
if (!libcxx_enable_rtti) {
if (current_os == "win") {
cflags_cc += [ "/GR-" ]
} else {
cflags_cc += [ "-fno-rtti" ]
}
defines += [ "-D_LIBCPP_NO_RTTI" ]
defines += [ "_LIBCPP_NO_RTTI" ]
}
}

Expand Down Expand Up @@ -251,7 +254,9 @@ if (libcxx_enable_static) {
sources = cxx_sources
if (libcxx_hermetic_static_library) {
cflags = [ "-fvisibility=hidden" ]
cflags_cc = [ "-fvisibility-global-new-delete-hidden" ]
if (libcxx_enable_new_delete_definitions) {
cflags_cc = [ "-fvisibility-global-new-delete-hidden" ]
}
defines = [ "_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS" ]
}
deps = [
Expand Down

0 comments on commit 741f52c

Please sign in to comment.