Skip to content

Commit 5d0fd3a

Browse files
strip_test: build executables with debug symbols (#90)
On Darwin, it seems that the plugin's default compiler and linker flags generate executables that are already pretty stripped, to the extent that `-Wl,-S -Wl,-x` doesn't do anything meaningful to all of them (e.g. the stripped and unstripped shared objects have identical file sizes and contain the same number of symbols). Guarantee that the linker has symbols to strip by emitting debug symbols; that way, the tests can ensure that the unstripped executables contain them, and the stripped ones don't. This also removes the run-time dependency on `file(1)`, which might not be present (e.g. on Alpine); `nm(1)` is part of both GNU binutils and LLVM, and is therefore much more likely to be installed if GCC or Clang are installed.
1 parent 282dd5d commit 5d0fd3a

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

test/strip/BUILD

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ subinclude(
33
"//build_defs:cc",
44
)
55

6+
package(
7+
cc = {
8+
# This is slightly contrived, but Mach-O executables always contain at least one symbol, even when stripped.
9+
# Emitting debug symbols ensures that Mach-O executables contain superfluous symbols that can later be stripped,
10+
# thus providing a clear distinction between stripped and unstripped executables for our tests.
11+
"default_opt_cflags": CONFIG.CC.DEFAULT_OPT_CFLAGS + ["-g"],
12+
"default_opt_cppflags": CONFIG.CC.DEFAULT_OPT_CPPFLAGS + ["-g"],
13+
},
14+
)
15+
616
binary_linker_flags = [
717
"-L" + package_name(),
818
"-rpath '%s'" % ("@executable_path" if CONFIG.OS == "darwin" else "$ORIGIN"),
@@ -21,7 +31,7 @@ for strip in [True, False]:
2131
linker_flags = [f"-install_name @rpath/libc_so_{stripped}.so"] if CONFIG.OS == "darwin" else [],
2232
strip = strip,
2333
)
24-
data[f"c_binary_{stripped}"] = c_binary(
34+
data[f"c_{stripped}"] = c_binary(
2535
name = f"c_binary_{stripped}",
2636
srcs = ["binary.c"],
2737
hdrs = ["so.h"],
@@ -37,21 +47,35 @@ for strip in [True, False]:
3747
linker_flags = [f"-install_name @rpath/libcc_so_{stripped}.so"] if CONFIG.OS == "darwin" else [],
3848
strip = strip,
3949
)
40-
data[f"cc_binary_{stripped}"] = cc_binary(
50+
data[f"cc_{stripped}"] = cc_binary(
4151
name = f"cc_binary_{stripped}",
4252
srcs = ["binary.cpp"],
4353
linker_flags = binary_linker_flags + [f"-lcc_so_{stripped}"],
4454
strip = strip,
4555
deps = [f":cc_so_{stripped}"],
4656
)
4757

58+
# Ensure the executables still function as expected after being stripped.
59+
test_cmd = ["for i in $DATA_C_STRIPPED $DATA_CC_STRIPPED $DATA_C_UNSTRIPPED $DATA_CC_UNSTRIPPED; do test $($i) = 42; done"]
60+
61+
if CONFIG.TARGET_OS == "darwin":
62+
test_cmd += [
63+
# `nm -a` should show that the unstripped executables contain debug symbols ("-")...
64+
"for i in $DATA_C_SO_UNSTRIPPED $DATA_C_UNSTRIPPED $DATA_CC_UNSTRIPPED; do nm -a $i | grep -qF ' - '; done",
65+
# ...but that the stripped executables don't.
66+
"for i in $DATA_C_SO_STRIPPED $DATA_C_STRIPPED $DATA_CC_STRIPPED; do ! nm -a $i | grep -qF ' - '; done",
67+
]
68+
else:
69+
test_cmd += [
70+
# `nm -a` should show that the unstripped executables contain at least one symbol...
71+
"for i in $DATA_C_SO_UNSTRIPPED $DATA_C_UNSTRIPPED $DATA_CC_UNSTRIPPED; do ! nm -a $i 2>&1 | grep -qF 'no symbols'; done",
72+
# ...but that the stripped executables contain none.
73+
"for i in $DATA_C_SO_STRIPPED $DATA_C_STRIPPED $DATA_CC_STRIPPED; do nm -a $i 2>&1 | grep -qF 'no symbols'; done",
74+
]
75+
4876
gentest(
4977
name = "strip_test",
50-
test_cmd = [
51-
"for i in $DATA_C_BINARY_STRIPPED $DATA_CC_BINARY_STRIPPED $DATA_C_BINARY_UNSTRIPPED $DATA_CC_BINARY_UNSTRIPPED; do test $($i) = 42; done",
52-
"for i in $DATA_C_SO_UNSTRIPPED $DATA_C_BINARY_UNSTRIPPED $DATA_CC_BINARY_UNSTRIPPED; do file $i | grep -q 'not stripped'; done",
53-
"for i in $DATA_C_SO_STRIPPED $DATA_C_BINARY_STRIPPED $DATA_CC_BINARY_STRIPPED; do file $i | grep -q ', stripped'; done",
54-
],
78+
test_cmd = test_cmd,
5579
data = data,
5680
exit_on_error = True,
5781
no_test_output = True,

0 commit comments

Comments
 (0)