Skip to content

Commit 7f3b3df

Browse files
committed
Auto merge of #71579 - Dylan-DPC:rollup-h9om2g3, r=Dylan-DPC
Rollup of 5 pull requests Successful merges: - #71490 (Cleanup and document `-C relocation-model`) - #71562 (fix more clippy warnings) - #71571 (Fix since attribute for nonzero_bitor impl's) - #71574 (proc_macro: Fix since attributes for new Span methods) - #71575 (Fix stable(since) attribute for BTreeMap::remove_entry) Failed merges: r? @ghost
2 parents ec1f28f + aa9dc69 commit 7f3b3df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+247
-189
lines changed

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

+39-4
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,46 @@ to a valid `.profdata` file. See the chapter on
319319

320320
## relocation-model
321321

322-
This option lets you choose which
323-
[relocation](https://en.wikipedia.org/wiki/Relocation_\(computing\)) model to
324-
use.
322+
This option controls generation of
323+
[position-independent code (PIC)](https://en.wikipedia.org/wiki/Position-independent_code).
325324

326-
To find the valid options for this flag, run `rustc --print relocation-models`.
325+
Supported values for this option are:
326+
327+
#### Primary relocation models
328+
329+
- `static` - non-relocatable code, machine instructions may use absolute addressing modes.
330+
331+
- `pic` - fully relocatable position independent code,
332+
machine instructions need to use relative addressing modes.
333+
Equivalent to the "uppercase" `-fPIC` or `-fPIE` options in other compilers,
334+
depending on the produced crate types.
335+
This is the default model for majority of supported targets.
336+
337+
#### Special relocation models
338+
339+
- `dynamic-no-pic` - relocatable external references, non-relocatable code.
340+
Only makes sense on Darwin and is rarely used.
341+
If StackOverflow tells you to use this as an opt-out of PIC or PIE, don't believe it,
342+
use `-C relocation-model=static` instead.
343+
- `ropi`, `rwpi` and `ropi-rwpi` - relocatable code and read-only data, relocatable read-write data,
344+
and combination of both, respectively.
345+
Only makes sense for certain embedded ARM targets.
346+
- `default` - relocation model default to the current target.
347+
Only makes sense as an override for some other explicitly specified relocation model
348+
previously set on the command line.
349+
350+
Supported values can also be discovered by running `rustc --print relocation-models`.
351+
352+
#### Linking effects
353+
354+
In addition to codegen effects, `relocation-model` has effects during linking.
355+
356+
If the relocation model is `pic` and the current target supports position-independent executables
357+
(PIE), the linker will be instructed (`-pie`) to produce one.
358+
If the target doesn't support both position-independent and statically linked executables,
359+
then `-C target-feature=+crt-static` "wins" over `-C relocation-model=pic`,
360+
and the linker is instructed (`-static`) to produce a statically linked
361+
but not position-independent executable.
327362

328363
## remark
329364

src/liballoc/collections/binary_heap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ impl<'a, T: Ord> Drop for DrainSorted<'a, T> {
12691269

12701270
impl<'r, 'a, T: Ord> Drop for DropGuard<'r, 'a, T> {
12711271
fn drop(&mut self) {
1272-
while let Some(_) = self.0.inner.pop() {}
1272+
while self.0.inner.pop().is_some() {}
12731273
}
12741274
}
12751275

src/liballoc/collections/btree/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
930930
/// assert_eq!(map.remove_entry(&1), Some((1, "a")));
931931
/// assert_eq!(map.remove_entry(&1), None);
932932
/// ```
933-
#[stable(feature = "btreemap_remove_entry", since = "1.44.0")]
933+
#[stable(feature = "btreemap_remove_entry", since = "1.45.0")]
934934
pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
935935
where
936936
K: Borrow<Q>,

src/liballoc/collections/linked_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ unsafe impl<#[may_dangle] T> Drop for LinkedList<T> {
972972
fn drop(&mut self) {
973973
// Continue the same loop we do below. This only runs when a destructor has
974974
// panicked. If another one panics this will abort.
975-
while let Some(_) = self.0.pop_front_node() {}
975+
while self.0.pop_front_node().is_some() {}
976976
}
977977
}
978978

src/libcore/num/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", s
111111
}
112112
}
113113

114-
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
114+
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
115115
impl BitOr for $Ty {
116116
type Output = Self;
117117
#[inline]
@@ -122,7 +122,7 @@ assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", s
122122
}
123123
}
124124

125-
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
125+
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
126126
impl BitOr<$Int> for $Ty {
127127
type Output = Self;
128128
#[inline]
@@ -134,7 +134,7 @@ assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", s
134134
}
135135
}
136136

137-
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
137+
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
138138
impl BitOr<$Ty> for $Int {
139139
type Output = $Ty;
140140
#[inline]
@@ -146,15 +146,15 @@ assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", s
146146
}
147147
}
148148

149-
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
149+
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
150150
impl BitOrAssign for $Ty {
151151
#[inline]
152152
fn bitor_assign(&mut self, rhs: Self) {
153153
*self = *self | rhs;
154154
}
155155
}
156156

157-
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
157+
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
158158
impl BitOrAssign<$Int> for $Ty {
159159
#[inline]
160160
fn bitor_assign(&mut self, rhs: $Int) {

src/libproc_macro/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,14 @@ impl Span {
351351

352352
/// Creates a new span with the same line/column information as `self` but
353353
/// that resolves symbols as though it were at `other`.
354-
#[stable(feature = "proc_macro_span_resolved_at", since = "1.43.0")]
354+
#[stable(feature = "proc_macro_span_resolved_at", since = "1.45.0")]
355355
pub fn resolved_at(&self, other: Span) -> Span {
356356
Span(self.0.resolved_at(other.0))
357357
}
358358

359359
/// Creates a new span with the same name resolution behavior as `self` but
360360
/// with the line/column information of `other`.
361-
#[stable(feature = "proc_macro_span_located_at", since = "1.43.0")]
361+
#[stable(feature = "proc_macro_span_located_at", since = "1.45.0")]
362362
pub fn located_at(&self, other: Span) -> Span {
363363
other.resolved_at(*self)
364364
}

src/librustc_codegen_llvm/back/write.rs

+20-28
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::back::profiling::{
77
use crate::base;
88
use crate::common;
99
use crate::consts;
10-
use crate::context::{get_reloc_model, is_pie_binary};
10+
use crate::context::all_outputs_are_pic_executables;
1111
use crate::llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic};
1212
use crate::llvm_util;
1313
use crate::type_::Type;
@@ -25,6 +25,7 @@ use rustc_middle::bug;
2525
use rustc_middle::ty::TyCtxt;
2626
use rustc_session::config::{self, Lto, OutputType, Passes, Sanitizer, SwitchWithOptPath};
2727
use rustc_session::Session;
28+
use rustc_target::spec::RelocModel;
2829

2930
use libc::{c_char, c_int, c_uint, c_void, size_t};
3031
use std::ffi::CString;
@@ -35,16 +36,6 @@ use std::slice;
3536
use std::str;
3637
use std::sync::Arc;
3738

38-
pub const RELOC_MODEL_ARGS: [(&str, llvm::RelocMode); 7] = [
39-
("pic", llvm::RelocMode::PIC),
40-
("static", llvm::RelocMode::Static),
41-
("default", llvm::RelocMode::Default),
42-
("dynamic-no-pic", llvm::RelocMode::DynamicNoPic),
43-
("ropi", llvm::RelocMode::ROPI),
44-
("rwpi", llvm::RelocMode::RWPI),
45-
("ropi-rwpi", llvm::RelocMode::ROPI_RWPI),
46-
];
47-
4839
pub const CODE_GEN_MODEL_ARGS: &[(&str, llvm::CodeModel)] = &[
4940
("small", llvm::CodeModel::Small),
5041
("kernel", llvm::CodeModel::Kernel),
@@ -84,19 +75,13 @@ pub fn write_output_file(
8475
}
8576
}
8677

87-
pub fn create_informational_target_machine(
88-
sess: &Session,
89-
find_features: bool,
90-
) -> &'static mut llvm::TargetMachine {
91-
target_machine_factory(sess, config::OptLevel::No, find_features)()
78+
pub fn create_informational_target_machine(sess: &Session) -> &'static mut llvm::TargetMachine {
79+
target_machine_factory(sess, config::OptLevel::No)()
9280
.unwrap_or_else(|err| llvm_err(sess.diagnostic(), &err).raise())
9381
}
9482

95-
pub fn create_target_machine(
96-
tcx: TyCtxt<'_>,
97-
find_features: bool,
98-
) -> &'static mut llvm::TargetMachine {
99-
target_machine_factory(&tcx.sess, tcx.backend_optimization_level(LOCAL_CRATE), find_features)()
83+
pub fn create_target_machine(tcx: TyCtxt<'_>) -> &'static mut llvm::TargetMachine {
84+
target_machine_factory(&tcx.sess, tcx.backend_optimization_level(LOCAL_CRATE))()
10085
.unwrap_or_else(|err| llvm_err(tcx.sess.diagnostic(), &err).raise())
10186
}
10287

@@ -126,15 +111,22 @@ fn to_pass_builder_opt_level(cfg: config::OptLevel) -> llvm::PassBuilderOptLevel
126111
}
127112
}
128113

129-
// If find_features is true this won't access `sess.crate_types` by assuming
130-
// that `is_pie_binary` is false. When we discover LLVM target features
131-
// `sess.crate_types` is uninitialized so we cannot access it.
114+
fn to_llvm_relocation_model(relocation_model: RelocModel) -> llvm::RelocModel {
115+
match relocation_model {
116+
RelocModel::Static => llvm::RelocModel::Static,
117+
RelocModel::Pic => llvm::RelocModel::PIC,
118+
RelocModel::DynamicNoPic => llvm::RelocModel::DynamicNoPic,
119+
RelocModel::Ropi => llvm::RelocModel::ROPI,
120+
RelocModel::Rwpi => llvm::RelocModel::RWPI,
121+
RelocModel::RopiRwpi => llvm::RelocModel::ROPI_RWPI,
122+
}
123+
}
124+
132125
pub fn target_machine_factory(
133126
sess: &Session,
134127
optlvl: config::OptLevel,
135-
find_features: bool,
136128
) -> Arc<dyn Fn() -> Result<&'static mut llvm::TargetMachine, String> + Send + Sync> {
137-
let reloc_model = get_reloc_model(sess);
129+
let reloc_model = to_llvm_relocation_model(sess.relocation_model());
138130

139131
let (opt_level, _) = to_llvm_opt_settings(optlvl);
140132
let use_softfp = sess.opts.cg.soft_float;
@@ -175,7 +167,7 @@ pub fn target_machine_factory(
175167
let features = features.join(",");
176168
let features = CString::new(features).unwrap();
177169
let abi = SmallCStr::new(&sess.target.target.options.llvm_abiname);
178-
let is_pie_binary = !find_features && is_pie_binary(sess);
170+
let pic_is_pie = all_outputs_are_pic_executables(sess);
179171
let trap_unreachable = sess.target.target.options.trap_unreachable;
180172
let emit_stack_size_section = sess.opts.debugging_opts.emit_stack_sizes;
181173

@@ -192,7 +184,7 @@ pub fn target_machine_factory(
192184
reloc_model,
193185
opt_level,
194186
use_softfp,
195-
is_pie_binary,
187+
pic_is_pie,
196188
ffunction_sections,
197189
fdata_sections,
198190
trap_unreachable,

src/librustc_codegen_llvm/context.rs

+12-26
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_session::Session;
2121
use rustc_span::source_map::{Span, DUMMY_SP};
2222
use rustc_span::symbol::Symbol;
2323
use rustc_target::abi::{HasDataLayout, LayoutOf, PointeeInfo, Size, TargetDataLayout, VariantIdx};
24-
use rustc_target::spec::{HasTargetSpec, Target};
24+
use rustc_target::spec::{HasTargetSpec, RelocModel, Target};
2525

2626
use std::cell::{Cell, RefCell};
2727
use std::ffi::CStr;
@@ -87,22 +87,6 @@ pub struct CodegenCx<'ll, 'tcx> {
8787
local_gen_sym_counter: Cell<usize>,
8888
}
8989

90-
pub fn get_reloc_model(sess: &Session) -> llvm::RelocMode {
91-
let reloc_model_arg = match sess.opts.cg.relocation_model {
92-
Some(ref s) => &s[..],
93-
None => &sess.target.target.options.relocation_model[..],
94-
};
95-
96-
match crate::back::write::RELOC_MODEL_ARGS.iter().find(|&&arg| arg.0 == reloc_model_arg) {
97-
Some(x) => x.1,
98-
_ => {
99-
sess.err(&format!("{:?} is not a valid relocation mode", reloc_model_arg));
100-
sess.abort_if_errors();
101-
bug!();
102-
}
103-
}
104-
}
105-
10690
fn get_tls_model(sess: &Session) -> llvm::ThreadLocalMode {
10791
let tls_model_arg = match sess.opts.debugging_opts.tls_model {
10892
Some(ref s) => &s[..],
@@ -119,12 +103,14 @@ fn get_tls_model(sess: &Session) -> llvm::ThreadLocalMode {
119103
}
120104
}
121105

122-
fn is_any_library(sess: &Session) -> bool {
123-
sess.crate_types.borrow().iter().any(|ty| *ty != config::CrateType::Executable)
124-
}
125-
126-
pub fn is_pie_binary(sess: &Session) -> bool {
127-
!is_any_library(sess) && get_reloc_model(sess) == llvm::RelocMode::PIC
106+
/// PIE is potentially more effective than PIC, but can only be used in executables.
107+
/// If all our outputs are executables, then we can relax PIC to PIE when producing object code.
108+
/// If the list of crate types is not yet known we conservatively return `false`.
109+
pub fn all_outputs_are_pic_executables(sess: &Session) -> bool {
110+
sess.relocation_model() == RelocModel::Pic
111+
&& sess.crate_types.try_get().map_or(false, |crate_types| {
112+
crate_types.iter().all(|ty| *ty == config::CrateType::Executable)
113+
})
128114
}
129115

130116
fn strip_function_ptr_alignment(data_layout: String) -> String {
@@ -157,7 +143,7 @@ pub unsafe fn create_module(
157143

158144
// Ensure the data-layout values hardcoded remain the defaults.
159145
if sess.target.target.options.is_builtin {
160-
let tm = crate::back::write::create_informational_target_machine(&tcx.sess, false);
146+
let tm = crate::back::write::create_informational_target_machine(tcx.sess);
161147
llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm);
162148
llvm::LLVMRustDisposeTargetMachine(tm);
163149

@@ -200,11 +186,11 @@ pub unsafe fn create_module(
200186
let llvm_target = SmallCStr::new(&sess.target.target.llvm_target);
201187
llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
202188

203-
if get_reloc_model(sess) == llvm::RelocMode::PIC {
189+
if sess.relocation_model() == RelocModel::Pic {
204190
llvm::LLVMRustSetModulePICLevel(llmod);
205191
}
206192

207-
if is_pie_binary(sess) {
193+
if all_outputs_are_pic_executables(sess) {
208194
llvm::LLVMRustSetModulePIELevel(llmod);
209195
}
210196

src/librustc_codegen_llvm/lib.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
110110
&self,
111111
sess: &Session,
112112
optlvl: OptLevel,
113-
find_features: bool,
114113
) -> Arc<dyn Fn() -> Result<&'static mut llvm::TargetMachine, String> + Send + Sync> {
115-
back::write::target_machine_factory(sess, optlvl, find_features)
114+
back::write::target_machine_factory(sess, optlvl)
116115
}
117116
fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str {
118117
llvm_util::target_cpu(sess)
@@ -201,7 +200,9 @@ impl CodegenBackend for LlvmCodegenBackend {
201200
match req {
202201
PrintRequest::RelocationModels => {
203202
println!("Available relocation models:");
204-
for &(name, _) in back::write::RELOC_MODEL_ARGS.iter() {
203+
for name in
204+
&["static", "pic", "dynamic-no-pic", "ropi", "rwpi", "ropi-rwpi", "default"]
205+
{
205206
println!(" {}", name);
206207
}
207208
println!();
@@ -351,19 +352,15 @@ impl ModuleLlvm {
351352
unsafe {
352353
let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names());
353354
let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _;
354-
ModuleLlvm { llmod_raw, llcx, tm: create_target_machine(tcx, false) }
355+
ModuleLlvm { llmod_raw, llcx, tm: create_target_machine(tcx) }
355356
}
356357
}
357358

358359
fn new_metadata(tcx: TyCtxt<'_>, mod_name: &str) -> Self {
359360
unsafe {
360361
let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names());
361362
let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _;
362-
ModuleLlvm {
363-
llmod_raw,
364-
llcx,
365-
tm: create_informational_target_machine(&tcx.sess, false),
366-
}
363+
ModuleLlvm { llmod_raw, llcx, tm: create_informational_target_machine(tcx.sess) }
367364
}
368365
}
369366

src/librustc_codegen_llvm/llvm/ffi.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,7 @@ pub struct SanitizerOptions {
445445
/// LLVMRelocMode
446446
#[derive(Copy, Clone, PartialEq)]
447447
#[repr(C)]
448-
pub enum RelocMode {
449-
Default,
448+
pub enum RelocModel {
450449
Static,
451450
PIC,
452451
DynamicNoPic,
@@ -1946,7 +1945,7 @@ extern "C" {
19461945
Features: *const c_char,
19471946
Abi: *const c_char,
19481947
Model: CodeModel,
1949-
Reloc: RelocMode,
1948+
Reloc: RelocModel,
19501949
Level: CodeGenOptLevel,
19511950
UseSoftFP: bool,
19521951
PositionIndependentExecutable: bool,

0 commit comments

Comments
 (0)