Skip to content

Commit 0b3df19

Browse files
committed
rustc: Tweak where -lmorestack is on link commands
In removing many fields from the crate map, executables no longer always have an explicit dependency on all upstream libraries. This means that the linker is no longer picking them up as it used to. To the best of my knowledge, the current situation is happening: * On linux, we're passing the --as-needed flag to the linker, meaning that libraries are stripped out if there are no references to symbols in them. * Executables may not reference libstd at all, such as "fn main() {}" * When linking, the linker will discard libstd because there are no references to symbols in it. I presume that this means that all previous libs have had all their symbols resolved, so none of the libs are pulling in libstd as a dependency. * The only real dependence on libstd comes from the rust_stack_exhausted symbol (which comes from libmorestack), but -lmorestack is at the end so by the time this comes up libstd is completely gone, leading to undefined references to rust_stack_exhausted I'm not entirely convinced that this is what's happening, but it appears to be along these lines. The one thing that I'm sure of is that removing the crate map (and hence implicit dependency on all upstream libraries) has changed how objects depend on upstream libraries.
1 parent a921dc4 commit 0b3df19

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/librustc/back/link.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,23 @@ fn link_args(sess: Session,
10851085
~"-o", out_filename.as_str().unwrap().to_owned(),
10861086
obj_filename.as_str().unwrap().to_owned()]);
10871087

1088+
// Stack growth requires statically linking a __morestack function. Note
1089+
// that this is listed *before* all other libraries, even though it may be
1090+
// used to resolve symbols in other libraries. The only case that this
1091+
// wouldn't be pulled in by the object file is if the object file had no
1092+
// functions.
1093+
//
1094+
// If we're building an executable, there must be at least one function (the
1095+
// main function), and if we're building a dylib then we don't need it for
1096+
// later libraries because they're all dylibs (not rlibs).
1097+
//
1098+
// I'm honestly not entirely sure why this needs to come first. Apparently
1099+
// the --as-needed flag above sometimes strips out libstd from the command
1100+
// line, but inserting this farther to the left makes the
1101+
// "rust_stack_exhausted" symbol an outstanding undefined symbol, which
1102+
// flags libstd as a required library (or whatever provides the symbol).
1103+
args.push(~"-lmorestack");
1104+
10881105
// When linking a dynamic library, we put the metadata into a section of the
10891106
// executable. This metadata is in a separate object file from the main
10901107
// object file, so we link that in here.
@@ -1200,11 +1217,13 @@ fn link_args(sess: Session,
12001217
args.push_all(rpath::get_rpath_flags(sess, out_filename).as_slice());
12011218
}
12021219

1203-
// Stack growth requires statically linking a __morestack function
1204-
args.push(~"-lmorestack");
1205-
// compiler-rt contains implementations of low-level LLVM helpers
1206-
// It should go before platform and user libraries, so it has first dibs
1207-
// at resolving symbols that also appear in libgcc.
1220+
// compiler-rt contains implementations of low-level LLVM helpers. This is
1221+
// used to resolve symbols from the object file we just created, as well as
1222+
// any system static libraries that may be expecting gcc instead. Most
1223+
// symbols in libgcc also appear in compiler-rt.
1224+
//
1225+
// This is the end of the command line, so this library is used to resolve
1226+
// *all* undefined symbols in all other libraries, and this is intentional.
12081227
args.push(~"-lcompiler-rt");
12091228

12101229
// Finally add all the linker arguments provided on the command line along

0 commit comments

Comments
 (0)