forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MCP rust-lang#705: Provide the option `-Zsymbol-mangling-version=hash…
…ed` to shorten symbol names by replacing them with a digest. Enrich test cases
- Loading branch information
1 parent
714b29a
commit 0633073
Showing
14 changed files
with
153 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher}; | ||
use rustc_hir::def_id::CrateNum; | ||
use rustc_middle::ty::{Instance, TyCtxt}; | ||
|
||
pub(super) fn mangle<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
instance: Instance<'tcx>, | ||
instantiating_crate: Option<CrateNum>, | ||
full_mangling_name: impl FnOnce() -> String, | ||
) -> String { | ||
// A generic function of an upstream library can be included in a dylib that depends on it. | ||
// In this case, `instantiating-crate name` is used to replace `crate name`. | ||
// If hash conflicats with other APIs in dylib, the method can be detected immediately. | ||
// Based on `instantiating-crate name`, symbol conflicts with other dylibs are avoided. | ||
let crate_num = | ||
if let Some(krate) = instantiating_crate { krate } else { instance.def_id().krate }; | ||
let crate_name = tcx.crate_name(crate_num); | ||
let crate_name = crate_name.as_str(); | ||
|
||
let hash = tcx.with_stable_hashing_context(|mut hcx| { | ||
let mut hasher = StableHasher::new(); | ||
full_mangling_name().hash_stable(&mut hcx, &mut hasher); | ||
hasher.finish::<Hash64>().as_u64() | ||
}); | ||
|
||
format!("_RNxC{}{crate_name}17H{hash:016x}", crate_name.len()) | ||
} |
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,47 @@ | ||
# ignore-cross-compile | ||
include ../tools.mk | ||
|
||
# ignore-windows-msvc | ||
|
||
NM=nm -D | ||
RLIB_NAME=liba_rlib.rlib | ||
DYLIB_NAME=liba_dylib.so | ||
SO_NAME=libb_dylib.so | ||
BIN_NAME=b_bin | ||
|
||
ifeq ($(UNAME),Darwin) | ||
NM=nm -gU | ||
RLIB_NAME=liba_rlib.rlib | ||
DYLIB_NAME=liba_dylib.dylib | ||
SO_NAME=libb_dylib.dylib | ||
BIN_NAME=b_bin | ||
endif | ||
|
||
ifdef IS_WINDOWS | ||
NM=nm -g | ||
RLIB_NAME=liba_rlib.dll.a | ||
DYLIB_NAME=liba_dylib.dll | ||
SO_NAME=libb_dylib.dll | ||
BIN_NAME=b_bin.exe | ||
endif | ||
|
||
all: | ||
$(RUSTC) -C prefer-dynamic -Z symbol-mangling-version=hashed -C metadata=foo a_dylib.rs | ||
$(RUSTC) -C prefer-dynamic -Z symbol-mangling-version=hashed -C metadata=bar a_rlib.rs | ||
$(RUSTC) -C prefer-dynamic -L $(TMPDIR) b_dylib.rs | ||
$(RUSTC) -C prefer-dynamic -L $(TMPDIR) b_bin.rs | ||
|
||
# Check hashed symbol name | ||
|
||
[ "$$($(NM) $(TMPDIR)/$(DYLIB_NAME) | grep -c hello)" -eq "0" ] | ||
[ "$$($(NM) $(TMPDIR)/$(DYLIB_NAME) | grep a_dylib17H | grep -c ' T ')" -eq "1" ] | ||
|
||
[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep b_dylib | grep -c hello)" -eq "1" ] | ||
[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep a_rlib17H | grep -c ' T ')" -eq "1" ] | ||
[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep a_dylib17H | grep -c ' U ')" -eq "1" ] | ||
|
||
[ "$$($(NM) $(TMPDIR)/$(BIN_NAME) | grep a_rlib17H | grep -c ' U ')" -eq "1" ] | ||
[ "$$($(NM) $(TMPDIR)/$(BIN_NAME) | grep a_dylib17H | grep -c ' U ')" -eq "1" ] | ||
[ "$$($(NM) $(TMPDIR)/$(BIN_NAME) | grep b_dylib | grep hello | grep -c ' U ')" -eq "1" ] | ||
|
||
$(call RUN,$(BIN_NAME)) |
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 hello() { | ||
println!("hello dylib"); | ||
} |
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 @@ | ||
#![crate_type="rlib"] | ||
|
||
pub fn hello() { | ||
println!("hello rlib"); | ||
} |
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,9 @@ | ||
extern crate a_rlib; | ||
extern crate a_dylib; | ||
extern crate b_dylib; | ||
|
||
fn main() { | ||
a_rlib::hello(); | ||
a_dylib::hello(); | ||
b_dylib::hello(); | ||
} |
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,9 @@ | ||
#![crate_type="dylib"] | ||
|
||
extern crate a_rlib; | ||
extern crate a_dylib; | ||
|
||
pub fn hello() { | ||
a_rlib::hello(); | ||
a_dylib::hello(); | ||
} |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
error: incorrect value `bad-value` for codegen option `symbol-mangling-version` - either `legacy` or `v0` (RFC 2603) was expected | ||
error: incorrect value `bad-value` for codegen option `symbol-mangling-version` - one of: `legacy`, `v0` (RFC 2603), or `hashed` was expected | ||
|
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
error: incorrect value `` for codegen option `symbol-mangling-version` - either `legacy` or `v0` (RFC 2603) was expected | ||
error: incorrect value `` for codegen option `symbol-mangling-version` - one of: `legacy`, `v0` (RFC 2603), or `hashed` was expected | ||
|
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
error: codegen option `symbol-mangling-version` requires either `legacy` or `v0` (RFC 2603) (C symbol-mangling-version=<value>) | ||
error: codegen option `symbol-mangling-version` requires one of: `legacy`, `v0` (RFC 2603), or `hashed` (C symbol-mangling-version=<value>) | ||
|
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,2 @@ | ||
error: `-C symbol-mangling-version=hashed` requires `-Z unstable-options` | ||
|
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
// revisions: legacy legacy-ok | ||
// revisions: legacy legacy-ok hashed hashed-ok | ||
// [legacy] compile-flags: -Csymbol-mangling-version=legacy | ||
// [legacy-ok] check-pass | ||
// [legacy-ok] compile-flags: -Zunstable-options -Csymbol-mangling-version=legacy | ||
// [hashed] compile-flags: -Csymbol-mangling-version=hashed | ||
// [hashed-ok] check-pass | ||
// [hashed-ok] compile-flags: -Zunstable-options -Csymbol-mangling-version=hashed | ||
|
||
fn main() {} |