Skip to content

Commit 2477a5f

Browse files
committed
add safe compilation options
add two options when building rustc : strip and stack protector if set `strip = true`, `rustc` will be stripped of symbols using `-Cstrip=symbols` also can set `stack-protector` and `rustc` will be compiled with stack protectors.
1 parent 9144d51 commit 2477a5f

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Diff for: config.example.toml

+7
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,13 @@ change-id = 116881
603603
# desired in distributions, for example.
604604
#rpath = true
605605

606+
# Indicates whether `rustc` should be stripped of symbols using `-Cstrip=symbols`.
607+
#strip = false
608+
609+
# Indicates whether `rustc` should be compiled with stack protectors.
610+
# Valid options are : `none`(default),`basic`,`strong`, or `all`.
611+
#stack-protector = "none"
612+
606613
# Prints each test name as it is executed, to help debug issues in the test harness itself.
607614
#verbose-tests = false
608615

Diff for: src/bootstrap/src/core/builder.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::core::build_steps::llvm;
1616
use crate::core::build_steps::tool::{self, SourceType};
1717
use crate::core::build_steps::{check, clean, compile, dist, doc, install, run, setup, test};
1818
use crate::core::config::flags::{Color, Subcommand};
19-
use crate::core::config::{DryRun, SplitDebuginfo, TargetSelection};
19+
use crate::core::config::{DryRun, SplitDebuginfo, TargetSelection, StackProtector};
2020
use crate::utils::cache::{Cache, Interned, INTERNER};
2121
use crate::utils::helpers::{self, add_dylib_path, add_link_lib_path, exe, libdir, output, t};
2222
use crate::Crate;
@@ -1686,6 +1686,16 @@ impl<'a> Builder<'a> {
16861686
rustflags.arg(&format!("-Clink-args={rpath}"));
16871687
}
16881688
}
1689+
1690+
if self.config.rust_strip {
1691+
rustflags.arg("-Cstrip=symbols");
1692+
}
1693+
match self.config.rust_stack_protector {
1694+
StackProtector::All => rustflags.arg("-Zstack-protector=all"),
1695+
StackProtector::Strong => rustflags.arg("-Zstack-protector=strong"),
1696+
StackProtector::Basic => rustflags.arg("-Zstack-protector=basic"),
1697+
StackProtector::None => rustflags.arg("-Zstack-protector=none"),
1698+
};
16891699

16901700
if let Some(host_linker) = self.linker(compiler.host) {
16911701
hostflags.arg(format!("-Clinker={}", host_linker.display()));

Diff for: src/bootstrap/src/core/config/config.rs

+34
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ pub struct Config {
222222
pub rust_debuginfo_level_tests: DebuginfoLevel,
223223
pub rust_split_debuginfo: SplitDebuginfo,
224224
pub rust_rpath: bool,
225+
pub rust_strip: bool,
226+
pub rust_stack_protector: StackProtector,
225227
pub rustc_parallel: bool,
226228
pub rustc_default_linker: Option<String>,
227229
pub rust_optimize_tests: bool,
@@ -394,6 +396,29 @@ impl SplitDebuginfo {
394396
}
395397
}
396398

399+
/// Stack protector mode for compiling rustc itself
400+
#[derive(Default, Clone, PartialEq, Debug)]
401+
pub enum StackProtector {
402+
#[default]
403+
None,
404+
Basic,
405+
Strong,
406+
All,
407+
}
408+
409+
impl std::str::FromStr for StackProtector {
410+
type Err = String;
411+
fn from_str(s: &str) -> Result<Self, Self::Err> {
412+
match s {
413+
"none" => Ok(StackProtector::None),
414+
"basic" => Ok(StackProtector::Basic),
415+
"strong" => Ok(StackProtector::Strong),
416+
"all" => Ok(StackProtector::All),
417+
_ => Err(format!("Invalid value for stack protector: {s}")),
418+
}
419+
}
420+
}
421+
397422
/// LTO mode used for compiling rustc itself.
398423
#[derive(Default, Clone, PartialEq, Debug)]
399424
pub enum RustcLto {
@@ -1000,6 +1025,8 @@ define_config! {
10001025
description: Option<String> = "description",
10011026
musl_root: Option<String> = "musl-root",
10021027
rpath: Option<bool> = "rpath",
1028+
strip: Option<bool> = "strip",
1029+
stack_protector: Option<String> = "stack-protector",
10031030
verbose_tests: Option<bool> = "verbose-tests",
10041031
optimize_tests: Option<bool> = "optimize-tests",
10051032
codegen_tests: Option<bool> = "codegen-tests",
@@ -1067,6 +1094,7 @@ impl Config {
10671094
config.docs = true;
10681095
config.docs_minification = true;
10691096
config.rust_rpath = true;
1097+
config.rust_strip = false;
10701098
config.channel = "dev".to_string();
10711099
config.codegen_tests = true;
10721100
config.rust_dist_src = true;
@@ -1420,6 +1448,12 @@ impl Config {
14201448
set(&mut config.rust_optimize_tests, rust.optimize_tests);
14211449
set(&mut config.codegen_tests, rust.codegen_tests);
14221450
set(&mut config.rust_rpath, rust.rpath);
1451+
set(&mut config.rust_strip, rust.strip);
1452+
config.rust_stack_protector = rust
1453+
.stack_protector
1454+
.as_deref()
1455+
.map(|value| StackProtector::from_str(value).unwrap())
1456+
.unwrap_or_default();
14231457
set(&mut config.jemalloc, rust.jemalloc);
14241458
set(&mut config.test_compare_mode, rust.test_compare_mode);
14251459
set(&mut config.backtrace, rust.backtrace);

0 commit comments

Comments
 (0)