Skip to content

Commit 420c58f

Browse files
committed
sess: stabilize relro-level
Signed-off-by: David Wood <david@davidtw.co>
1 parent 9afdb8d commit 420c58f

File tree

6 files changed

+29
-9
lines changed

6 files changed

+29
-9
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2013,7 +2013,7 @@ fn add_library_search_dirs(cmd: &mut dyn Linker, sess: &Session, self_contained:
20132013
/// Add options making relocation sections in the produced ELF files read-only
20142014
/// and suppressing lazy binding.
20152015
fn add_relro_args(cmd: &mut dyn Linker, sess: &Session) {
2016-
match sess.opts.unstable_opts.relro_level.unwrap_or(sess.target.relro_level) {
2016+
match sess.opts.cg.relro_level.unwrap_or(sess.target.relro_level) {
20172017
RelroLevel::Full => cmd.full_relro(),
20182018
RelroLevel::Partial => cmd.partial_relro(),
20192019
RelroLevel::Off => cmd.no_relro(),

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ fn test_codegen_options_tracking_hash() {
608608
tracked!(profile_generate, SwitchWithOptPath::Enabled(None));
609609
tracked!(profile_use, Some(PathBuf::from("abc")));
610610
tracked!(relocation_model, Some(RelocModel::Pic));
611+
tracked!(relro_level, Some(RelroLevel::Full));
611612
tracked!(soft_float, true);
612613
tracked!(split_debuginfo, Some(SplitDebuginfo::Packed));
613614
tracked!(symbol_mangling_version, Some(SymbolManglingVersion::V0));
@@ -805,7 +806,6 @@ fn test_unstable_options_tracking_hash() {
805806
tracked!(profile_sample_use, Some(PathBuf::from("abc")));
806807
tracked!(profiler_runtime, "abc".to_string());
807808
tracked!(relax_elf_relocations, Some(true));
808-
tracked!(relro_level, Some(RelroLevel::Full));
809809
tracked!(remap_cwd_prefix, Some(PathBuf::from("abc")));
810810
tracked!(sanitizer, SanitizerSet::ADDRESS);
811811
tracked!(sanitizer_cfi_canonical_jump_tables, None);

compiler/rustc_session/src/options.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,8 @@ options! {
14941494
relocation_model: Option<RelocModel> = (None, parse_relocation_model, [TRACKED],
14951495
"control generation of position-independent code (PIC) \
14961496
(`rustc --print relocation-models` for details)"),
1497+
relro_level: Option<RelroLevel> = (None, parse_relro_level, [TRACKED],
1498+
"choose which RELRO level to use"),
14971499
remark: Passes = (Passes::Some(Vec::new()), parse_passes, [UNTRACKED],
14981500
"output remarks for these optimization passes (space separated, or \"all\")"),
14991501
rpath: bool = (false, parse_bool, [UNTRACKED],
@@ -1829,8 +1831,6 @@ options! {
18291831
"randomize the layout of types (default: no)"),
18301832
relax_elf_relocations: Option<bool> = (None, parse_opt_bool, [TRACKED],
18311833
"whether ELF relocations can be relaxed"),
1832-
relro_level: Option<RelroLevel> = (None, parse_relro_level, [TRACKED],
1833-
"choose which RELRO level to use"),
18341834
remap_cwd_prefix: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
18351835
"remap paths under the current working directory to this path prefix"),
18361836
remap_path_scope: RemapPathScopeComponents = (RemapPathScopeComponents::all(), parse_remap_path_scope, [TRACKED],

compiler/rustc_session/src/session.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ impl Session {
587587

588588
let dbg_opts = &self.opts.unstable_opts;
589589

590-
let relro_level = dbg_opts.relro_level.unwrap_or(self.target.relro_level);
590+
let relro_level = self.opts.cg.relro_level.unwrap_or(self.target.relro_level);
591591

592592
// Only enable this optimization by default if full relro is also enabled.
593593
// In this case, lazy binding was already unavailable, so nothing is lost.

src/doc/rustc/src/codegen-options/index.md

+20
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,26 @@ then `-C target-feature=+crt-static` "wins" over `-C relocation-model=pic`,
479479
and the linker is instructed (`-static`) to produce a statically linked
480480
but not position-independent executable.
481481

482+
## relro-level
483+
484+
This flag controls what level of RELRO (Relocation Read-Only) is enabled. RELRO is an exploit
485+
mitigation which makes the Global Offset Table (GOT) read-only.
486+
487+
Supported values for this option are:
488+
489+
- `off`: Dynamically linked functions are resolved lazily and the GOT is writable.
490+
- `partial`: Dynamically linked functions are resolved lazily and written into the Procedure
491+
Linking Table (PLT) part of the GOT (`.got.plt`). The non-PLT part of the GOT (`.got`) is made
492+
read-only and both are moved to prevent writing from buffer overflows.
493+
- `full`: Dynamically linked functions are resolved at the start of program execution and the
494+
Global Offset Table (`.got`/`.got.plt`) is populated eagerly and then made read-only. The GOT is
495+
also moved to prevent writing from buffer overflows. Full RELRO uses more memory and increases
496+
process startup time.
497+
498+
This flag is ignored on platforms where RELRO is not supported (targets which do not use the ELF
499+
binary format), such as Windows or macOS. Each rustc target has its own default for RELRO. rustc
500+
enables Full RELRO by default on platforms where it is supported.
501+
482502
## remark
483503

484504
This flag lets you print remarks for optimization passes.

tests/run-make/relro-levels/Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ include ../tools.mk
33

44
# only-linux
55
#
6-
# This tests the different -Zrelro-level values, and makes sure that they work properly.
6+
# This tests the different -Crelro-level values, and makes sure that they work properly.
77

88
all:
99
# Ensure that binaries built with the full relro level links them with both
1010
# RELRO and BIND_NOW for doing eager symbol resolving.
11-
$(RUSTC) -Zrelro-level=full hello.rs
11+
$(RUSTC) -Crelro-level=full hello.rs
1212
readelf -l $(TMPDIR)/hello | grep -q GNU_RELRO
1313
readelf -d $(TMPDIR)/hello | grep -q BIND_NOW
1414

15-
$(RUSTC) -Zrelro-level=partial hello.rs
15+
$(RUSTC) -Crelro-level=partial hello.rs
1616
readelf -l $(TMPDIR)/hello | grep -q GNU_RELRO
1717

1818
# Ensure that we're *not* built with RELRO when setting it to off. We do
1919
# not want to check for BIND_NOW however, as the linker might have that
2020
# enabled by default.
21-
$(RUSTC) -Zrelro-level=off hello.rs
21+
$(RUSTC) -Crelro-level=off hello.rs
2222
! readelf -l $(TMPDIR)/hello | grep -q GNU_RELRO

0 commit comments

Comments
 (0)