Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,20 @@ fn link_args(cmd: &mut Command,
cmd.arg("-Wl,--gc-sections");
}

let used_link_args = sess.cstore.get_used_link_args().borrow();

// Dynamically linked executables can be compiled as position independent if the default
// relocation model of position independent code is not changed. This is a requirement to take
// advantage of ASLR, as otherwise the functions in the executable are not randomized and can
// be used during an exploit of a vulnerability in any code.
if sess.targ_cfg.os == abi::OsLinux {
let mut args = sess.opts.cg.link_args.iter().chain(used_link_args.iter());
if !dylib && sess.opts.cg.relocation_model.as_slice() == "pic" &&
!args.any(|x| x.as_slice() == "-static") {
cmd.arg("-pie");
}
}

if sess.targ_cfg.os == abi::OsLinux || sess.targ_cfg.os == abi::OsDragonfly {
// GNU-style linkers will use this to omit linking to libraries which
// don't actually fulfill any relocations, but only for libraries which
Expand Down Expand Up @@ -1568,9 +1582,7 @@ fn link_args(cmd: &mut Command,
// Finally add all the linker arguments provided on the command line along
// with any #[link_args] attributes found inside the crate
cmd.args(sess.opts.cg.link_args.as_slice());
for arg in sess.cstore.get_used_link_args().borrow().iter() {
cmd.arg(arg.as_slice());
}
cmd.args(used_link_args.as_slice());
}

// # Native library linking
Expand Down