Skip to content

Commit 5a4fa45

Browse files
committed
linker: Some minor code cleanup
1 parent 379c255 commit 5a4fa45

File tree

1 file changed

+38
-58
lines changed
  • src/librustc_codegen_ssa/back

1 file changed

+38
-58
lines changed

src/librustc_codegen_ssa/back/link.rs

+38-58
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
154154
// The third parameter is for env vars, used on windows to set up the
155155
// path for MSVC to find its DLLs, and gcc to find its bundled
156156
// toolchain
157-
pub fn get_linker(sess: &Session, linker: &Path, flavor: LinkerFlavor) -> Command {
157+
fn get_linker(sess: &Session, linker: &Path, flavor: LinkerFlavor) -> Command {
158158
let msvc_tool = windows_registry::find_tool(&sess.opts.target_triple.triple(), "link.exe");
159159

160160
// If our linker looks like a batch script on Windows then to execute this
@@ -285,11 +285,7 @@ pub fn each_linked_rlib(
285285
/// building an `.rlib` (stomping over one another), or writing an `.rmeta` into a
286286
/// directory being searched for `extern crate` (observing an incomplete file).
287287
/// The returned path is the temporary file containing the complete metadata.
288-
pub fn emit_metadata<'a>(
289-
sess: &'a Session,
290-
metadata: &EncodedMetadata,
291-
tmpdir: &TempDir,
292-
) -> PathBuf {
288+
pub fn emit_metadata(sess: &Session, metadata: &EncodedMetadata, tmpdir: &TempDir) -> PathBuf {
293289
let out_filename = tmpdir.path().join(METADATA_FILENAME);
294290
let result = fs::write(&out_filename, &metadata.raw_data);
295291

@@ -744,7 +740,7 @@ pub fn ignored_for_lto(sess: &Session, info: &CrateInfo, cnum: CrateNum) -> bool
744740
&& (info.compiler_builtins == Some(cnum) || info.is_no_builtins.contains(&cnum))
745741
}
746742

747-
pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
743+
fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
748744
fn infer_from(
749745
sess: &Session,
750746
linker: Option<PathBuf>,
@@ -832,7 +828,7 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
832828
/// Returns a boolean indicating whether we should preserve the object files on
833829
/// the filesystem for their debug information. This is often useful with
834830
/// split-dwarf like schemes.
835-
pub fn preserve_objects_for_their_debuginfo(sess: &Session) -> bool {
831+
fn preserve_objects_for_their_debuginfo(sess: &Session) -> bool {
836832
// If the objects don't have debuginfo there's nothing to preserve.
837833
if sess.opts.debuginfo == config::DebugInfo::None {
838834
return false;
@@ -886,7 +882,7 @@ enum RlibFlavor {
886882
StaticlibBase,
887883
}
888884

889-
pub fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLibrary]) {
885+
fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLibrary]) {
890886
let lib_args: Vec<_> = all_native_libs
891887
.iter()
892888
.filter(|l| relevant_lib(sess, l))
@@ -1012,7 +1008,7 @@ fn get_object_file_path(sess: &Session, name: &str) -> PathBuf {
10121008
PathBuf::from(name)
10131009
}
10141010

1015-
pub fn exec_linker(
1011+
fn exec_linker(
10161012
sess: &Session,
10171013
cmd: &Command,
10181014
out_filename: &Path,
@@ -1161,7 +1157,7 @@ pub fn exec_linker(
11611157
}
11621158

11631159
/// Add begin object files defined by the target spec.
1164-
fn add_pre_link_objects(cmd: &mut dyn Linker, sess: &'a Session, crate_type: config::CrateType) {
1160+
fn add_pre_link_objects(cmd: &mut dyn Linker, sess: &Session, crate_type: config::CrateType) {
11651161
let pre_link_objects = if crate_type == config::CrateType::Executable {
11661162
&sess.target.target.options.pre_link_objects_exe
11671163
} else {
@@ -1179,7 +1175,7 @@ fn add_pre_link_objects(cmd: &mut dyn Linker, sess: &'a Session, crate_type: con
11791175
}
11801176

11811177
/// Add end object files defined by the target spec.
1182-
fn add_post_link_objects(cmd: &mut dyn Linker, sess: &'a Session, crate_type: config::CrateType) {
1178+
fn add_post_link_objects(cmd: &mut dyn Linker, sess: &Session, crate_type: config::CrateType) {
11831179
for obj in &sess.target.target.options.post_link_objects {
11841180
cmd.add_object(&get_object_file_path(sess, obj));
11851181
}
@@ -1194,7 +1190,7 @@ fn add_post_link_objects(cmd: &mut dyn Linker, sess: &'a Session, crate_type: co
11941190
/// FIXME: Determine where exactly these args need to be inserted.
11951191
fn add_pre_link_args(
11961192
cmd: &mut dyn Linker,
1197-
sess: &'a Session,
1193+
sess: &Session,
11981194
flavor: LinkerFlavor,
11991195
crate_type: config::CrateType,
12001196
) {
@@ -1213,7 +1209,7 @@ fn add_pre_link_args(
12131209
/// FIXME: Determine where exactly these args need to be inserted.
12141210
fn add_user_defined_link_args(
12151211
cmd: &mut dyn Linker,
1216-
sess: &'a Session,
1212+
sess: &Session,
12171213
codegen_results: &CodegenResults,
12181214
) {
12191215
cmd.args(&sess.opts.cg.link_args);
@@ -1224,7 +1220,7 @@ fn add_user_defined_link_args(
12241220
/// FIXME: Determine where exactly these args need to be inserted.
12251221
fn add_late_link_args(
12261222
cmd: &mut dyn Linker,
1227-
sess: &'a Session,
1223+
sess: &Session,
12281224
flavor: LinkerFlavor,
12291225
crate_type: config::CrateType,
12301226
codegen_results: &CodegenResults,
@@ -1249,7 +1245,7 @@ fn add_late_link_args(
12491245

12501246
/// Add arbitrary "post-link" args defined by the target spec.
12511247
/// FIXME: Determine where exactly these args need to be inserted.
1252-
fn add_post_link_args(cmd: &mut dyn Linker, sess: &'a Session, flavor: LinkerFlavor) {
1248+
fn add_post_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
12531249
if let Some(args) = sess.target.target.options.post_link_args.get(&flavor) {
12541250
cmd.args(args);
12551251
}
@@ -1264,8 +1260,7 @@ fn add_local_crate_regular_objects(cmd: &mut dyn Linker, codegen_results: &Codeg
12641260

12651261
/// Add object files for allocator code linked once for the whole crate tree.
12661262
fn add_local_crate_allocator_objects(cmd: &mut dyn Linker, codegen_results: &CodegenResults) {
1267-
let obj = codegen_results.allocator_module.as_ref().and_then(|m| m.object.as_ref());
1268-
if let Some(obj) = obj {
1263+
if let Some(obj) = codegen_results.allocator_module.as_ref().and_then(|m| m.object.as_ref()) {
12691264
cmd.add_object(obj);
12701265
}
12711266
}
@@ -1280,8 +1275,8 @@ fn add_local_crate_metadata_objects(
12801275
// executable. This metadata is in a separate object file from the main
12811276
// object file, so we link that in here.
12821277
if crate_type == config::CrateType::Dylib || crate_type == config::CrateType::ProcMacro {
1283-
let obj = codegen_results.metadata_module.as_ref().and_then(|m| m.object.as_ref());
1284-
if let Some(obj) = obj {
1278+
if let Some(obj) = codegen_results.metadata_module.as_ref().and_then(|m| m.object.as_ref())
1279+
{
12851280
cmd.add_object(obj);
12861281
}
12871282
}
@@ -1337,25 +1332,24 @@ fn link_local_crate_native_libs_and_dependent_crate_libs<'a, B: ArchiveBuilder<'
13371332
}
13381333

13391334
/// Add sysroot and other globally set directories to the directory search list.
1340-
fn add_library_search_dirs(cmd: &mut dyn Linker, sess: &'a Session) {
1341-
// The default library location, we need this to find the runtime.
1342-
// The location of crates will be determined as needed.
1343-
let lib_path = sess.target_filesearch(PathKind::All).get_lib_path();
1344-
1345-
// prefer system mingw-w64 libs, see get_crt_libs_path comment for more details
1335+
fn add_library_search_dirs(cmd: &mut dyn Linker, sess: &Session) {
1336+
// Prefer system mingw-w64 libs, see get_crt_libs_path comment for more details.
13461337
if cfg!(windows) && sess.target.target.llvm_target.contains("windows-gnu") {
13471338
if let Some(compiler_libs_path) = get_crt_libs_path(sess) {
13481339
cmd.include_path(&compiler_libs_path);
13491340
}
13501341
}
13511342

1343+
// The default library location, we need this to find the runtime.
1344+
// The location of crates will be determined as needed.
1345+
let lib_path = sess.target_filesearch(PathKind::All).get_lib_path();
13521346
cmd.include_path(&fix_windows_verbatim_for_gcc(&lib_path));
13531347
}
13541348

13551349
/// Add options requesting executables to be position-independent or not position-independent.
13561350
fn add_position_independent_executable_args(
13571351
cmd: &mut dyn Linker,
1358-
sess: &'a Session,
1352+
sess: &Session,
13591353
flavor: LinkerFlavor,
13601354
crate_type: config::CrateType,
13611355
codegen_results: &CodegenResults,
@@ -1364,55 +1358,41 @@ fn add_position_independent_executable_args(
13641358
return;
13651359
}
13661360

1367-
let mut position_independent_executable = false;
13681361
if sess.target.target.options.position_independent_executables {
13691362
let attr_link_args = &*codegen_results.crate_info.link_args;
13701363
let mut user_defined_link_args = sess.opts.cg.link_args.iter().chain(attr_link_args);
13711364
if is_pic(sess)
13721365
&& !sess.crt_static(Some(crate_type))
13731366
&& !user_defined_link_args.any(|x| x == "-static")
13741367
{
1375-
position_independent_executable = true;
1368+
cmd.position_independent_executable();
1369+
return;
13761370
}
13771371
}
13781372

1379-
if position_independent_executable {
1380-
cmd.position_independent_executable();
1381-
} else {
1382-
// recent versions of gcc can be configured to generate position
1383-
// independent executables by default. We have to pass -no-pie to
1384-
// explicitly turn that off. Not applicable to ld.
1385-
if sess.target.target.options.linker_is_gnu && flavor != LinkerFlavor::Ld {
1386-
cmd.no_position_independent_executable();
1387-
}
1373+
// Recent versions of gcc can be configured to generate position
1374+
// independent executables by default. We have to pass -no-pie to
1375+
// explicitly turn that off. Not applicable to ld.
1376+
if sess.target.target.options.linker_is_gnu && flavor != LinkerFlavor::Ld {
1377+
cmd.no_position_independent_executable();
13881378
}
13891379
}
13901380

13911381
/// Add options making relocation sections in the produced ELF files read-only
13921382
/// and suppressing lazy binding.
1393-
fn add_relro_args(cmd: &mut dyn Linker, sess: &'a Session) {
1394-
let relro_level = match sess.opts.debugging_opts.relro_level {
1395-
Some(level) => level,
1396-
None => sess.target.target.options.relro_level,
1397-
};
1398-
match relro_level {
1399-
RelroLevel::Full => {
1400-
cmd.full_relro();
1401-
}
1402-
RelroLevel::Partial => {
1403-
cmd.partial_relro();
1404-
}
1405-
RelroLevel::Off => {
1406-
cmd.no_relro();
1407-
}
1383+
fn add_relro_args(cmd: &mut dyn Linker, sess: &Session) {
1384+
match sess.opts.debugging_opts.relro_level.unwrap_or(sess.target.target.options.relro_level) {
1385+
RelroLevel::Full => cmd.full_relro(),
1386+
RelroLevel::Partial => cmd.partial_relro(),
1387+
RelroLevel::Off => cmd.no_relro(),
14081388
RelroLevel::None => {}
14091389
}
14101390
}
14111391

14121392
/// Add library search paths used at runtime by dynamic linkers.
14131393
fn add_rpath_args(
14141394
cmd: &mut dyn Linker,
1415-
sess: &'a Session,
1395+
sess: &Session,
14161396
codegen_results: &CodegenResults,
14171397
out_filename: &Path,
14181398
) {
@@ -1623,7 +1603,7 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
16231603
// Also note that the native libraries linked here are only the ones located
16241604
// in the current crate. Upstream crates with native library dependencies
16251605
// may have their native library pulled in above.
1626-
pub fn add_local_native_libraries(
1606+
fn add_local_native_libraries(
16271607
cmd: &mut dyn Linker,
16281608
sess: &Session,
16291609
codegen_results: &CodegenResults,
@@ -1953,7 +1933,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
19531933
// generic function calls a native function, then the generic function must
19541934
// be instantiated in the target crate, meaning that the native symbol must
19551935
// also be resolved in the target crate.
1956-
pub fn add_upstream_native_libraries(
1936+
fn add_upstream_native_libraries(
19571937
cmd: &mut dyn Linker,
19581938
sess: &Session,
19591939
codegen_results: &CodegenResults,
@@ -2010,14 +1990,14 @@ pub fn add_upstream_native_libraries(
20101990
}
20111991
}
20121992

2013-
pub fn relevant_lib(sess: &Session, lib: &NativeLibrary) -> bool {
1993+
fn relevant_lib(sess: &Session, lib: &NativeLibrary) -> bool {
20141994
match lib.cfg {
20151995
Some(ref cfg) => rustc_attr::cfg_matches(cfg, &sess.parse_sess, None),
20161996
None => true,
20171997
}
20181998
}
20191999

2020-
pub fn are_upstream_rust_objects_already_included(sess: &Session) -> bool {
2000+
fn are_upstream_rust_objects_already_included(sess: &Session) -> bool {
20212001
match sess.lto() {
20222002
config::Lto::Fat => true,
20232003
config::Lto::Thin => {

0 commit comments

Comments
 (0)