Skip to content

Commit 02c81fe

Browse files
committed
auto merge of #13340 : FlaPer87/rust/code-model, r=cmr
Rust currently defaults to `RelocPIC` regardless. This patch adds a new codegen option that allows choosing different relocation-model. The available models are: - default (Use the target-specific default model) - static - pic - no-pic For a more detailed information use `llc --help`
2 parents 4af69f2 + b78ac5b commit 02c81fe

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

Diff for: src/etc/zsh/_rust

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ _rustc_opts_switches=(
3636
--target'[Target triple cpu-manufacturer-kernel\[-os\] to compile]'
3737
--target-cpu'[Select target processor (llc -mcpu=help for details)]'
3838
--target-feature'[Target specific attributes (llc -mattr=help for details)]'
39+
--relocation-model'[Relocation model (llc --help for details)]'
3940
{-v,--version}'[Print version info and exit]'
4041
)
4142
_rustc_opts_lint=(

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,26 @@ pub mod write {
152152
(sess.targ_cfg.os == abi::OsMacos &&
153153
sess.targ_cfg.arch == abi::X86_64);
154154

155+
let reloc_model = match sess.opts.cg.relocation_model.as_slice() {
156+
"pic" => lib::llvm::RelocPIC,
157+
"static" => lib::llvm::RelocStatic,
158+
"default" => lib::llvm::RelocDefault,
159+
"dynamic-no-pic" => lib::llvm::RelocDynamicNoPic,
160+
_ => {
161+
sess.err(format!("{} is not a valid relocation mode",
162+
sess.opts.cg.relocation_model));
163+
sess.abort_if_errors();
164+
return;
165+
}
166+
};
167+
155168
let tm = sess.targ_cfg.target_strs.target_triple.with_c_str(|t| {
156169
sess.opts.cg.target_cpu.with_c_str(|cpu| {
157170
target_feature(sess).with_c_str(|features| {
158171
llvm::LLVMRustCreateTargetMachine(
159172
t, cpu, features,
160173
lib::llvm::CodeModelDefault,
161-
lib::llvm::RelocPIC,
174+
reloc_model,
162175
opt_level,
163176
true,
164177
use_softfp,

Diff for: src/librustc/driver/session.rs

+2
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ cgoptions!(
458458
"prefer dynamic linking to static linking"),
459459
no_integrated_as: bool = (false, parse_bool,
460460
"use an external assembler rather than LLVM's integrated one"),
461+
relocation_model: ~str = (~"pic", parse_string,
462+
"choose the relocation model to use (llc -relocation-model for details)"),
461463
)
462464
463465
// Seems out of place, but it uses session, so I'm putting it here

Diff for: src/test/run-make/relocation-model/Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) -C relocation-model=dynamic-no-pic foo.rs
5+
$(call RUN,foo)
6+
7+
$(RUSTC) -C relocation-model=default foo.rs
8+
$(call RUN,foo)
9+
10+
$(RUSTC) -C relocation-model=static foo.rs
11+
$(call RUN,foo)
12+
13+
$(RUSTC) -C relocation-model=default --crate-type=dylib foo.rs
14+
$(RUSTC) -C relocation-model=static --crate-type=dylib foo.rs
15+
$(RUSTC) -C relocation-model=dynamic-no-pic --crate-type=dylib foo.rs

Diff for: src/test/run-make/relocation-model/foo.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2014 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+
pub fn main() {}

0 commit comments

Comments
 (0)