-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the unstable option symbol_mangling_digest to reduce the binary s…
…ize of dynamic library based on service requirements
- Loading branch information
1 parent
cda4736
commit 30c6868
Showing
9 changed files
with
224 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher}; | ||
use rustc_hir::def_id::DefId; | ||
use rustc_middle::ty::TyCtxt; | ||
|
||
pub(super) fn generate<'tcx>(tcx: TyCtxt<'tcx>, symbol: String, def_id: DefId) -> String { | ||
let crate_name = tcx.crate_name(def_id.krate); | ||
let crate_name = crate_name.as_str(); | ||
let symbol_mangling_digest = &tcx.sess.opts.unstable_opts.symbol_mangling_digest; | ||
if !symbol_mangling_digest.hasher_contains(crate_name) { | ||
return symbol; | ||
} | ||
|
||
let (salt, level) = symbol_mangling_digest.hasher_args(); | ||
|
||
let hash = tcx.with_stable_hashing_context(|mut hcx| { | ||
let mut hasher = StableHasher::new(); | ||
symbol.hash_stable(&mut hcx, &mut hasher); | ||
salt.hash_stable(&mut hcx, &mut hasher); | ||
hasher.finish::<Hash64>().as_u64() | ||
}); | ||
|
||
match level { | ||
1 => encode_1(tcx, crate_name, hash, def_id), | ||
_ => encode_2(tcx, crate_name, hash, def_id), | ||
} | ||
} | ||
|
||
fn encode_1<'tcx>(tcx: TyCtxt<'tcx>, crate_name: &str, hash: u64, def_id: DefId) -> String { | ||
if let Some(item_name) = tcx.opt_item_name(def_id) { | ||
let item_name = item_name.as_str(); | ||
format!( | ||
"_ZN{}{crate_name}.{item_name}.{:08x}E", | ||
crate_name.len() + item_name.len() + 11, | ||
hash >> 8 | ||
) | ||
} else { | ||
encode_2(tcx, crate_name, hash, def_id) | ||
} | ||
} | ||
|
||
fn encode_2<'tcx>(_tcx: TyCtxt<'tcx>, crate_name: &str, hash: u64, _def_id: DefId) -> String { | ||
format!("_ZN{}{crate_name}.{hash:016x}E", crate_name.len() + 18) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/doc/unstable-book/src/compiler-flags/symbol_mangling_digest.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# `symbol_mangling_digest` | ||
|
||
An optimization option is added to allow users to generate shorter symbol names for dylib. At the expense of commissioning capabilities such as readability of symbol names, this option eliminates the space bottlenech encountered by using Rust to replace existing C/C++ functional modules in resource-constrained scenarios. | ||
|
||
The new option are defined as follows: `-Z symbol_mangling_digest=<crate_name>[*],...[,excluded=<true|false>][,salt=<value>][,level=<1|2>]`. | ||
|
||
- `crate_name[*],...`: Name of a crate. Multiple crate names are allowd. If the suffix `*` is carried, it is the prefix of the crate name. It and `excluded` togeter determine the range of symbols to be optimized. User must be very clear about the optimization range. If the crate supports regular expression maching, the optimization range is difficult to determine. May cause confusion. Defaults to null. | ||
- `excluded=<true|false>`: If the value is `false`, only the names of symbols whose crate names are successfully matched are optimized. If the value is `true`, it indicates that the name of the symbol that fails to be matched is optimized. the default value is `false`. | ||
- `salt=<value>`: User-specified salt value used in hash calculation. The default value is null. | ||
- `level=<1|2>`: Specifies the combination policy of the final symbol name. If the value is `1`, the final combination format is `{crate}.{item}.{hash32}`. If the value is `2`, the final combination format is `{crate}.{hash64}`. The default value is `2`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
include ../tools.mk | ||
|
||
all: | ||
$(RUSTC) -C prefer-dynamic -Z symbol_mangling_digest=foo foo.rs | ||
$(RUSTC) -C prefer-dynamic -Z symbol_mangling_digest=foo --extern foo=$(TMPDIR)/libfoo.so bar.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![crate_type = "bin"] | ||
|
||
extern crate foo; | ||
|
||
fn main() { | ||
foo::foo(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#![crate_type = "dylib"] | ||
pub fn foo() { | ||
println!("hello foo"); | ||
} |