From e6ca0848df365e43b19ecff2cca63e2ac4aebc76 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Wed, 12 Apr 2017 03:03:35 +0200 Subject: testing/rust: fix multiple issues with static/dynamic linking --- testing/rust/static-pie.patch | 102 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 4 deletions(-) (limited to 'testing/rust/static-pie.patch') diff --git a/testing/rust/static-pie.patch b/testing/rust/static-pie.patch index adc615f95f..5ae091f691 100644 --- a/testing/rust/static-pie.patch +++ b/testing/rust/static-pie.patch @@ -1,16 +1,16 @@ From: Shiz -Date: Sat, 8 Apr 2017 05:38:00 +0200 +Date: Tue, 11 Apr 2017 04:37:00 +0200 Subject: [PATCH] Add support for static PIE executables -Note that static PIE binaries are reported as dynamically linked by -the "file" utility: +Note that static PIE binaries are reported as dynamically linked by file(1): $ rustc -C target-feature=+crt-static hello_world.rb $ file hello_world ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically \ linked, not stripped, with debug_info -Also ldd reports that it's linked with ldd: +In addition, ldd(1) reports that it's linked with ldd, as it can't find any +dependencies except itself loaded into the process: $ ldd hello_world ldd (0x237fcc81000) @@ -121,3 +121,97 @@ library, just like static binaries, but more secure. } fn no_default_libraries(&mut self) { +--- a/src/librustc_trans/back/link.rs ++++ b/src/librustc_trans/back/link.rs +@@ -987,11 +987,14 @@ + cmd.link_whole_staticlib(&l.name.as_str(), &search_path); + } + +- cmd.hint_dynamic(); ++ let crt_static = sess.crt_static(); ++ if !crt_static { ++ cmd.hint_dynamic(); ++ } + + for lib in others { + match lib.kind { +- NativeLibraryKind::NativeUnknown => cmd.link_dylib(&lib.name.as_str()), ++ NativeLibraryKind::NativeUnknown => if crt_static { cmd.link_staticlib(&lib.name.as_str()) } else { cmd.link_dylib(&lib.name.as_str()) }, + NativeLibraryKind::NativeFramework => cmd.link_framework(&lib.name.as_str()), + NativeLibraryKind::NativeStatic => bug!(), + } +--- a/src/librustc_trans/back/link.rs ++++ b/src/librustc_trans/back/link.rs +@@ -874,15 +874,8 @@ + cmd.no_default_libraries(); + } + +- // Take careful note of the ordering of the arguments we pass to the linker +- // here. Linkers will assume that things on the left depend on things to the +- // right. Things on the right cannot depend on things on the left. This is +- // all formally implemented in terms of resolving symbols (libs on the right +- // resolve unknown symbols of libs on the left, but not vice versa). ++ // We have organized the arguments we pass to the linker as such: + // +- // For this reason, we have organized the arguments we pass to the linker as +- // such: +- // + // 1. The local object that LLVM just generated + // 2. Local native libraries + // 3. Upstream rust libraries +@@ -892,17 +885,12 @@ + // list can't depend on items higher up in the list. For example nothing can + // depend on what we just generated (e.g. that'd be a circular dependency). + // Upstream rust libraries are not allowed to depend on our local native +- // libraries as that would violate the structure of the DAG, in that +- // scenario they are required to link to them as well in a shared fashion. +- // +- // Note that upstream rust libraries may contain native dependencies as +- // well, but they also can't depend on what we just started to add to the +- // link line. And finally upstream native libraries can't depend on anything +- // in this DAG so far because they're only dylibs and dylibs can only depend +- // on other dylibs (e.g. other native deps). ++ // libraries as that would violate the structure of the DAG. ++ cmd.start_group(); + add_local_native_libraries(cmd, sess); + add_upstream_rust_crates(cmd, sess, crate_type, tmpdir); + add_upstream_native_libraries(cmd, sess); ++ cmd.end_group(); + + // # Telling the linker what we're doing + +--- a/src/librustc_trans/back/linker.rs ++++ b/src/librustc_trans/back/linker.rs +@@ -94,6 +94,8 @@ + fn no_whole_archives(&mut self); + fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType); + fn subsystem(&mut self, subsystem: &str); ++ fn start_group(&mut self); ++ fn end_group(&mut self); + } + + pub struct GnuLinker<'a> { +@@ -118,6 +120,8 @@ + fn add_object(&mut self, path: &Path) { self.cmd.arg(path); } + fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); } + fn static_executable(&mut self) { self.cmd.arg("-static"); } ++ fn start_group(&mut self) { self.cmd.arg("-Wl,-("); } ++ fn end_group(&mut self) { self.cmd.arg("-Wl,-)"); } + fn args(&mut self, args: &[String]) { self.cmd.args(args); } + + fn link_rust_dylib(&mut self, lib: &str, _path: &Path) { +@@ -490,6 +494,14 @@ + if subsystem == "windows" { + self.cmd.arg("/ENTRY:mainCRTStartup"); + } ++ } ++ ++ fn start_group(&mut self) { ++ // Not needed ++ } ++ ++ fn end_group(&mut self) { ++ // Not needed + } + } + -- cgit v1.2.3