summaryrefslogtreecommitdiff
path: root/cli/build.rs
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-10-06 08:50:00 -0700
committerGitHub <noreply@github.com>2022-10-06 21:20:00 +0530
commitcd1c63ad71965df6ee95d156fd17271177bd50c8 (patch)
tree724d0f8ecd30edb42e99d3ece730d8cea1f6efab /cli/build.rs
parent9102ba9b0f50ba1fe4f13111ec66ac4e09ba2db4 (diff)
fix(build): don't export all symbols to dynamic symbol table (#16171)
Currently, we use `-rdynamic` for exporting Node API symbols to the symbol table. `-rdynamic` will export *all* symbols, that means previously unused functions will not be optimized away introducing a lot of binary bloat. This patch uses `-exported_symbol` and `--export-dynamic-symbol` link flags (not as universal as `-rdynamic`) to only mark Node API symbols to be put in the dynamic symbol table.
Diffstat (limited to 'cli/build.rs')
-rw-r--r--cli/build.rs31
1 files changed, 30 insertions, 1 deletions
diff --git a/cli/build.rs b/cli/build.rs
index aaa29a6d0..c1bc3305e 100644
--- a/cli/build.rs
+++ b/cli/build.rs
@@ -344,7 +344,36 @@ fn main() {
);
#[cfg(not(target_os = "windows"))]
- println!("cargo:rustc-link-arg-bin=deno=-rdynamic");
+ {
+ // Load the symbols file generated by the `napi_sym` macro.
+ #[derive(serde::Deserialize)]
+ struct Symbols {
+ symbols: Vec<String>,
+ }
+ let symbols_json =
+ std::fs::read_to_string("../tools/napi/symbol_exports.json").expect(
+ "Missing tools/napi/symbol_exports.json! This is a bug in napi_sym",
+ );
+ let symbols: Symbols = serde_json::from_str(&symbols_json)
+ .expect("tools/napi/symbol_exports.json is not valid JSON");
+
+ // Don't export all symbols into the dynamic symbol table. -rdynamic exports *all* symbols introducing binary bloat.
+ // We only need to export Node API symbols.
+ for symbol in symbols.symbols {
+ // TODO(@littledivy): We _might_ hit an argument size limit?
+ // Maybe use `--export-dynamic-symbol-list` / `--exported_symbols_list` instead? https://reviews.llvm.org/D107317
+ #[cfg(target_os = "macos")]
+ println!(
+ "cargo:rustc-link-arg-bin=deno=-Wl,-exported_symbol,_{}",
+ symbol
+ );
+ #[cfg(target_os = "linux")]
+ println!(
+ "cargo:rustc-link-arg-bin=deno=-Wl,--export-dynamic-symbol={}",
+ symbol
+ );
+ }
+ }
// To debug snapshot issues uncomment:
// op_fetch_asset::trace_serializer();