Skip to content

Commit 2f0e6a3

Browse files
committed
Auto merge of #48388 - kyrias:relro-level-cg, r=alexcrichton
Add relro-level tests The `relro-level` debugging flag was added in #43170 which was merged in July 2017. This PR moves this flag to be a proper codegen flag.
2 parents 87344aa + 1dbce4b commit 2f0e6a3

File tree

6 files changed

+62
-7
lines changed

6 files changed

+62
-7
lines changed

Diff for: src/librustc_back/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ pub enum RelroLevel {
131131
Full,
132132
Partial,
133133
Off,
134+
None,
134135
}
135136

136137
impl RelroLevel {
@@ -139,6 +140,7 @@ impl RelroLevel {
139140
RelroLevel::Full => "full",
140141
RelroLevel::Partial => "partial",
141142
RelroLevel::Off => "off",
143+
RelroLevel::None => "none",
142144
}
143145
}
144146
}
@@ -151,6 +153,7 @@ impl FromStr for RelroLevel {
151153
"full" => Ok(RelroLevel::Full),
152154
"partial" => Ok(RelroLevel::Partial),
153155
"off" => Ok(RelroLevel::Off),
156+
"none" => Ok(RelroLevel::None),
154157
_ => Err(()),
155158
}
156159
}
@@ -162,6 +165,7 @@ impl ToJson for RelroLevel {
162165
RelroLevel::Full => "full".to_json(),
163166
RelroLevel::Partial => "partial".to_json(),
164167
RelroLevel::Off => "off".to_json(),
168+
RelroLevel::None => "None".to_json(),
165169
}
166170
}
167171
}

Diff for: src/librustc_back/target/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ impl Default for TargetOptions {
514514
has_rpath: false,
515515
no_default_libraries: true,
516516
position_independent_executables: false,
517-
relro_level: RelroLevel::Off,
517+
relro_level: RelroLevel::None,
518518
pre_link_objects_exe: Vec::new(),
519519
pre_link_objects_dll: Vec::new(),
520520
post_link_objects: Vec::new(),

Diff for: src/librustc_trans/back/link.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,11 @@ fn link_args(cmd: &mut Linker,
10141014
RelroLevel::Partial => {
10151015
cmd.partial_relro();
10161016
},
1017-
RelroLevel::Off => {},
1017+
RelroLevel::Off => {
1018+
cmd.no_relro();
1019+
},
1020+
RelroLevel::None => {
1021+
},
10181022
}
10191023

10201024
// Pass optimization flags down to the linker.

Diff for: src/librustc_trans/back/linker.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ pub trait Linker {
113113
fn gc_sections(&mut self, keep_metadata: bool);
114114
fn position_independent_executable(&mut self);
115115
fn no_position_independent_executable(&mut self);
116-
fn partial_relro(&mut self);
117116
fn full_relro(&mut self);
117+
fn partial_relro(&mut self);
118+
fn no_relro(&mut self);
118119
fn optimize(&mut self);
119120
fn debuginfo(&mut self);
120121
fn no_default_libraries(&mut self);
@@ -188,8 +189,9 @@ impl<'a> Linker for GccLinker<'a> {
188189
fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
189190
fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
190191
fn no_position_independent_executable(&mut self) { self.cmd.arg("-no-pie"); }
191-
fn partial_relro(&mut self) { self.linker_arg("-z,relro"); }
192192
fn full_relro(&mut self) { self.linker_arg("-z,relro,-z,now"); }
193+
fn partial_relro(&mut self) { self.linker_arg("-z,relro"); }
194+
fn no_relro(&mut self) { self.linker_arg("-z,norelro"); }
193195
fn build_static_executable(&mut self) { self.cmd.arg("-static"); }
194196
fn args(&mut self, args: &[String]) { self.cmd.args(args); }
195197

@@ -452,11 +454,15 @@ impl<'a> Linker for MsvcLinker<'a> {
452454
// noop
453455
}
454456

457+
fn full_relro(&mut self) {
458+
// noop
459+
}
460+
455461
fn partial_relro(&mut self) {
456462
// noop
457463
}
458464

459-
fn full_relro(&mut self) {
465+
fn no_relro(&mut self) {
460466
// noop
461467
}
462468

@@ -664,11 +670,15 @@ impl<'a> Linker for EmLinker<'a> {
664670
// noop
665671
}
666672

673+
fn full_relro(&mut self) {
674+
// noop
675+
}
676+
667677
fn partial_relro(&mut self) {
668678
// noop
669679
}
670680

671-
fn full_relro(&mut self) {
681+
fn no_relro(&mut self) {
672682
// noop
673683
}
674684

@@ -829,10 +839,13 @@ impl Linker for WasmLd {
829839
fn position_independent_executable(&mut self) {
830840
}
831841

842+
fn full_relro(&mut self) {
843+
}
844+
832845
fn partial_relro(&mut self) {
833846
}
834847

835-
fn full_relro(&mut self) {
848+
fn no_relro(&mut self) {
836849
}
837850

838851
fn build_static_executable(&mut self) {

Diff for: src/test/run-make/relro-levels/Makefile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-include ../tools.mk
2+
3+
# This tests the different -Zrelro-level values, and makes sure that they they work properly.
4+
5+
all:
6+
ifeq ($(UNAME),Linux)
7+
# Ensure that binaries built with the full relro level links them with both
8+
# RELRO and BIND_NOW for doing eager symbol resolving.
9+
$(RUSTC) -Zrelro-level=full hello.rs
10+
readelf -l $(TMPDIR)/hello | grep -q GNU_RELRO
11+
readelf -d $(TMPDIR)/hello | grep -q BIND_NOW
12+
13+
$(RUSTC) -Zrelro-level=partial hello.rs
14+
readelf -l $(TMPDIR)/hello | grep -q GNU_RELRO
15+
16+
# Ensure that we're *not* built with RELRO when setting it to off. We do
17+
# not want to check for BIND_NOW however, as the linker might have that
18+
# enabled by default.
19+
$(RUSTC) -Zrelro-level=off hello.rs
20+
! readelf -l $(TMPDIR)/hello | grep -q GNU_RELRO
21+
endif

Diff for: src/test/run-make/relro-levels/hello.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
println!("Hello, world!");
13+
}

0 commit comments

Comments
 (0)