Skip to content

Commit 927db7d

Browse files
committed
linker: Factor out linking of pre- and post-link objects
1 parent 032462e commit 927db7d

File tree

1 file changed

+37
-23
lines changed
  • src/librustc_codegen_ssa/back

1 file changed

+37
-23
lines changed

src/librustc_codegen_ssa/back/link.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ fn get_crt_libs_path(sess: &Session) -> Option<PathBuf> {
987987
}
988988
}
989989

990-
pub fn get_file_path(sess: &Session, name: &str) -> PathBuf {
990+
fn get_object_file_path(sess: &Session, name: &str) -> PathBuf {
991991
// prefer system {,dll}crt2.o libs, see get_crt_libs_path comment for more details
992992
if sess.target.target.llvm_target.contains("windows-gnu") {
993993
if let Some(compiler_libs_path) = get_crt_libs_path(sess) {
@@ -1159,6 +1159,36 @@ pub fn exec_linker(
11591159
}
11601160
}
11611161

1162+
/// Add begin object files defined by the target spec.
1163+
fn add_pre_link_objects(cmd: &mut dyn Linker, sess: &'a Session, crate_type: config::CrateType) {
1164+
let pre_link_objects = if crate_type == config::CrateType::Executable {
1165+
&sess.target.target.options.pre_link_objects_exe
1166+
} else {
1167+
&sess.target.target.options.pre_link_objects_dll
1168+
};
1169+
for obj in pre_link_objects {
1170+
cmd.add_object(&get_object_file_path(sess, obj));
1171+
}
1172+
1173+
if crate_type == config::CrateType::Executable && sess.crt_static(Some(crate_type)) {
1174+
for obj in &sess.target.target.options.pre_link_objects_exe_crt {
1175+
cmd.add_object(&get_object_file_path(sess, obj));
1176+
}
1177+
}
1178+
}
1179+
1180+
/// Add end object files defined by the target spec.
1181+
fn add_post_link_objects(cmd: &mut dyn Linker, sess: &'a Session, crate_type: config::CrateType) {
1182+
for obj in &sess.target.target.options.post_link_objects {
1183+
cmd.add_object(&get_object_file_path(sess, obj));
1184+
}
1185+
if sess.crt_static(Some(crate_type)) {
1186+
for obj in &sess.target.target.options.post_link_objects_crt {
1187+
cmd.add_object(&get_object_file_path(sess, obj));
1188+
}
1189+
}
1190+
}
1191+
11621192
fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
11631193
path: &Path,
11641194
flavor: LinkerFlavor,
@@ -1193,20 +1223,8 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
11931223
cmd.arg(format!("--dynamic-linker={}ld.so.1", prefix));
11941224
}
11951225

1196-
let pre_link_objects = if crate_type == config::CrateType::Executable {
1197-
&sess.target.target.options.pre_link_objects_exe
1198-
} else {
1199-
&sess.target.target.options.pre_link_objects_dll
1200-
};
1201-
for obj in pre_link_objects {
1202-
cmd.arg(get_file_path(sess, obj));
1203-
}
1204-
1205-
if crate_type == config::CrateType::Executable && sess.crt_static(Some(crate_type)) {
1206-
for obj in &sess.target.target.options.pre_link_objects_exe_crt {
1207-
cmd.arg(get_file_path(sess, obj));
1208-
}
1209-
}
1226+
// NO-OPT-OUT
1227+
add_pre_link_objects(cmd, sess, crate_type);
12101228

12111229
if sess.target.target.options.is_like_emscripten {
12121230
cmd.arg("-s");
@@ -1436,14 +1454,10 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
14361454
cmd.args(args);
14371455
}
14381456
}
1439-
for obj in &sess.target.target.options.post_link_objects {
1440-
cmd.arg(get_file_path(sess, obj));
1441-
}
1442-
if sess.crt_static(Some(crate_type)) {
1443-
for obj in &sess.target.target.options.post_link_objects_crt {
1444-
cmd.arg(get_file_path(sess, obj));
1445-
}
1446-
}
1457+
1458+
// NO-OPT-OUT
1459+
add_post_link_objects(cmd, sess, crate_type);
1460+
14471461
if let Some(args) = sess.target.target.options.post_link_args.get(&flavor) {
14481462
cmd.args(args);
14491463
}

0 commit comments

Comments
 (0)