Skip to content

Commit 8082034

Browse files
danakjcopybara-github
authored andcommitted
Explicitly include compiler-rt builtins library in linking
This makes the intrinsic symbols contained within considered as strong symbols, so that when the Rust stdlib intrinsics are marked as weak, the C++ ones will take precedence. Otherwise, even if they are marked weak, the Rust stdlib intrinsics are selected by the linker. It may be the Rust intrinsics are preferable, but we want to stay on the C++ intrinsics (for C++ and Rust code in mixed binaries) unless we can demonstrate a good reason to switch. Upstream bug for marking Rust intrinsic symbols weak: rust-lang/compiler-builtins#525 To test and verify this works: 1. Revert the changes to shift.rs in rust-lang/compiler-builtins@1634193 to third_party/rust-toolchain/lib/rustlib/src/rust/vendor/compiler_builtins-*/src/int/shift.rs 2. Apply https://chromium-review.googlesource.com/c/chromium/src/+/45461823 3. Follow the instructions in its description to build and run bad_intrinsics 4. The bad_intrinsics binary panics without this CL, but not with this CL, meaning the C++ intrinsics are used. Bug: 1445978 Change-Id: Ib251660346d03902f531285999aeeabc28882049 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4546164 Commit-Queue: Arthur Eubanks <[email protected]> Reviewed-by: Arthur Eubanks <[email protected]> Auto-Submit: danakj <[email protected]> Cr-Commit-Position: refs/heads/main@{#1150647} NOKEYCHECK=True GitOrigin-RevId: 31b9cb886d359780569bc5e1281bc7515d16e9bb
1 parent f3ccd4b commit 8082034

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

config/compiler/BUILD.gn

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,96 @@ config("compiler_arm") {
15541554
}
15551555
}
15561556

1557+
# Rust and C++ both provide intrinsics for LLVM to call for math operations. We
1558+
# want to use the C++ intrinsics, not the ones in the Rust compiler_builtins
1559+
# library. The Rust symbols are marked as weak, so that they can be replaced by
1560+
# the C++ symbols. This config ensures the C++ symbols exist and are strong in
1561+
# order to cause that replacement to occur by explicitly linking in clang's
1562+
# compiler-rt library.
1563+
config("compiler_builtins") {
1564+
visibility = [ ":runtime_library" ]
1565+
1566+
_supported = true
1567+
_dir = ""
1568+
_libname = "builtins"
1569+
_suffix = ""
1570+
_ext = "a"
1571+
if (!toolchain_has_rust) {
1572+
# Since there's no Rust in the toolchain, there's no concern that we'll use
1573+
# the Rust stdlib's intrinsics here.
1574+
_supported = false
1575+
} else if (is_win) {
1576+
_dir = "windows"
1577+
_ext = "lib"
1578+
if (current_cpu == "x64") {
1579+
_suffix = "-x86_64"
1580+
} else if (current_cpu == "x86") {
1581+
_suffix = "-i386"
1582+
} else {
1583+
assert(false) # Unhandled cpu type
1584+
}
1585+
} else if (is_mac) {
1586+
_dir = "darwin"
1587+
_libname = "osx"
1588+
} else if (is_ios) {
1589+
import("//build/config/ios/config.gni") # For `target_environment`
1590+
1591+
_dir = "darwin"
1592+
if (target_environment == "simulator") {
1593+
_libname = "iossim"
1594+
} else {
1595+
_libname = "ios"
1596+
}
1597+
} else if (is_linux || is_chromeos) {
1598+
if (current_cpu == "x64") {
1599+
_dir = "x86_64-unknown-linux-gnu"
1600+
} else if (current_cpu == "x86") {
1601+
_dir = "i386-unknown-linux-gnu"
1602+
} else if (current_cpu == "arm") {
1603+
_dir = "armv7-unknown-linux-gnueabihf"
1604+
} else if (current_cpu == "arm64") {
1605+
_dir = "aarch64-unknown-linux-gnu"
1606+
} else {
1607+
assert(false) # Unhandled cpu type
1608+
}
1609+
} else if (is_fuchsia) {
1610+
if (current_cpu == "x64") {
1611+
_dir = "x86_64-unknown-fuchsia"
1612+
} else if (current_cpu == "arm64") {
1613+
_dir = "aarch64-unknown-fuchsia"
1614+
} else {
1615+
assert(false) # Unhandled cpu type
1616+
}
1617+
} else if (is_android) {
1618+
_dir = "linux"
1619+
if (current_cpu == "x64") {
1620+
_suffix = "-x86_64-android"
1621+
} else if (current_cpu == "x86") {
1622+
_suffix = "-i686-android"
1623+
} else if (current_cpu == "arm") {
1624+
_suffix = "-arm-android"
1625+
} else if (current_cpu == "arm64") {
1626+
_suffix = "-aarch64-android"
1627+
} else {
1628+
assert(false) # Unhandled cpu type
1629+
}
1630+
} else {
1631+
assert(false) # Unhandled target platform
1632+
}
1633+
1634+
if (_supported) {
1635+
_clang_lib_dir = "$clang_base_path/lib/clang/$clang_version/lib"
1636+
libs = [ "$_clang_lib_dir/$_dir/libclang_rt.$_libname$_suffix.$_ext" ]
1637+
} else {
1638+
not_needed([
1639+
"_dir",
1640+
"_libname",
1641+
"_suffix",
1642+
"_ext",
1643+
])
1644+
}
1645+
}
1646+
15571647
# runtime_library -------------------------------------------------------------
15581648
#
15591649
# Sets the runtime library and associated options.
@@ -1575,6 +1665,10 @@ config("runtime_library") {
15751665
configs += [ "//build/config/c++:runtime_library" ]
15761666
}
15771667

1668+
if (toolchain_has_rust) {
1669+
configs += [ ":compiler_builtins" ]
1670+
}
1671+
15781672
# TODO(crbug.com/830987): Come up with a better name for is POSIX + Fuchsia
15791673
# configuration.
15801674
if (is_posix || is_fuchsia) {

0 commit comments

Comments
 (0)