forked from cloudflare/boring
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add optional Unix-only BoringSSL symbol prefixing #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,98 @@ | ||
| use crate::{config::Config, run_command}; | ||
| use std::{fs, io::Write, path::PathBuf, process::Command}; | ||
|
|
||
| /// Prefix applied to all BoringSSL symbols so they don't collide with OpenSSL. | ||
| /// Uses the crate version to generate a dynamic prefix. | ||
| const SYMBOL_PREFIX: &str = concat!( | ||
| "BSSL_", | ||
| env!("CARGO_PKG_VERSION_MAJOR"), | ||
| "_", | ||
| env!("CARGO_PKG_VERSION_MINOR"), | ||
| "_", | ||
| env!("CARGO_PKG_VERSION_PATCH"), | ||
| ); | ||
|
|
||
| /// Bindgen callback that rewrites link names to use the prefixed symbol. | ||
| /// | ||
| /// C symbol `SSL_new` → Rust binding `#[link_name = "BSSL_SSL_new"]`. | ||
| #[derive(Debug)] | ||
| pub struct SymbolPrefixCallbacks; | ||
|
|
||
| impl bindgen::callbacks::ParseCallbacks for SymbolPrefixCallbacks { | ||
| fn generated_link_name_override( | ||
| &self, | ||
| item_info: bindgen::callbacks::ItemInfo<'_>, | ||
| ) -> Option<String> { | ||
| Some(format!("{SYMBOL_PREFIX}_{}", item_info.name)) | ||
| } | ||
| } | ||
|
|
||
| /// Rewrite all global symbols in libssl.a/libcrypto.a to use the S2N_BSSL_ prefix. | ||
| /// | ||
| /// This runs `nm` to list symbols and `objcopy --redefine-syms` to edit the archives | ||
| /// in-place, so they can safely coexist with other {libssl,libcrypto} in the process. | ||
| pub fn apply_symbol_prefixes(config: &Config) { | ||
| // CMake output directories where libssl.a/libcrypto.a are expected. | ||
| let static_lib_dirs = [ | ||
| config.out_dir.join("build"), | ||
| config.out_dir.join("build").join("ssl"), | ||
| config.out_dir.join("build").join("crypto"), | ||
| ]; | ||
|
|
||
| let static_libs: Vec<PathBuf> = static_lib_dirs | ||
| .iter() | ||
| .flat_map(|dir| { | ||
| ["libssl.a", "libcrypto.a"] | ||
| .into_iter() | ||
| .map(move |file| dir.join(file)) | ||
| }) | ||
| .filter(|path| path.exists()) | ||
| .collect(); | ||
|
|
||
| if static_libs.is_empty() { | ||
| eprintln!("warning: no libssl.a/libcrypto.a archives found to prefix"); | ||
| return; | ||
| } | ||
|
|
||
| // 1. Use `nm` to list global symbols in the archives. | ||
| let nm_output = run_command(Command::new("nm").args(&static_libs)) | ||
| .expect("failed to run `nm` on BoringSSL archives"); | ||
|
|
||
| let mut mappings: Vec<String> = String::from_utf8_lossy(&nm_output.stdout) | ||
| .lines() | ||
| // Keep only global symbol types we care about. | ||
| .filter(|line| { | ||
| [" T ", " D ", " B ", " C ", " R ", " W "] | ||
| .iter() | ||
| .any(|marker| line.contains(marker)) | ||
| }) | ||
| // Symbol name is usually the 3rd column. | ||
| .filter_map(|line| line.split_whitespace().nth(2).map(str::to_owned)) | ||
| // Skip leading-underscore internals. | ||
| .filter(|sym| !sym.starts_with('_')) | ||
| // Compose `old new` mapping line: `sym S2N_BSSL_sym`. | ||
| .map(|sym| format!("{sym} {SYMBOL_PREFIX}_{sym}")) | ||
| .collect(); | ||
|
|
||
| mappings.sort(); | ||
| mappings.dedup(); | ||
|
|
||
| let mapping_file = config.out_dir.join("redefine_syms.txt"); | ||
| let mut f = fs::File::create(&mapping_file) | ||
| .expect("failed to create redefine_syms.txt for symbol prefixing"); | ||
|
|
||
| for mapping in &mappings { | ||
| writeln!(f, "{mapping}").expect("failed to write symbol mapping"); | ||
| } | ||
| f.flush().expect("failed to flush symbol mapping file"); | ||
|
|
||
| // 2. Use `objcopy` to apply the mapping to each archive in-place. | ||
| for static_lib in &static_libs { | ||
| run_command( | ||
| Command::new("objcopy") | ||
| .arg(format!("--redefine-syms={}", mapping_file.display())) | ||
| .arg(static_lib), | ||
| ) | ||
| .expect("failed to run `objcopy` to redefine symbols"); | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.