Skip to content

Commit bbda6f9

Browse files
committed
Auto merge of #27304 - alexcrichton:revert-picky-dllimport, r=brson
This reverts commit a0efd3a. This commit caused a lot of unintended breakage for many Cargo builds. The problem is that Cargo compiles build scripts with `-C prefer-dynamic`, so the standard library is always dynamically linked and hence any imports need to be marked with `dllimport`. Dependencies of build scripts, however, were compiled as rlibs and did not have their imports tagged with `dllimport`, so build scripts would fail to link. While known that this situation would break, it was unknown that it was a common scenario in the wild. As a result I'm just reverting these heuristics for now.
2 parents 8988043 + 316e1b0 commit bbda6f9

File tree

4 files changed

+1
-93
lines changed

4 files changed

+1
-93
lines changed

src/librustc_trans/trans/base.rs

+1-37
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ pub fn get_extern_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, did: ast::DefId,
227227
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
228228
// FIXME(nagisa): investigate whether it can be changed into define_global
229229
let c = declare::declare_global(ccx, &name[..], ty);
230-
231230
// Thread-local statics in some other crate need to *always* be linked
232231
// against in a thread-local fashion, so we need to be sure to apply the
233232
// thread-local attribute locally if it was present remotely. If we
@@ -239,42 +238,7 @@ pub fn get_extern_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, did: ast::DefId,
239238
llvm::set_thread_local(c, true);
240239
}
241240
}
242-
243-
// MSVC is a little ornery about how items are imported across dlls, and for
244-
// lots more info on dllimport/dllexport see the large comment in
245-
// SharedCrateContext::new. Unfortunately, unlike functions, statics
246-
// imported from dlls *must* be tagged with dllimport (if you forget
247-
// dllimport on a function then the linker fixes it up with an injected
248-
// shim). This means that to link correctly to an upstream Rust dynamic
249-
// library we need to make sure its statics are tagged with dllimport.
250-
//
251-
// Hence, if this translation is using dll storage attributes and the crate
252-
// that this const originated from is being imported as a dylib at some
253-
// point we tag this with dllimport.
254-
//
255-
// Note that this is not 100% correct for a variety of reasons:
256-
//
257-
// 1. If we are producing an rlib and linking to an upstream rlib, we'll
258-
// omit the dllimport. It's a possibility, though, that some later
259-
// downstream compilation will link the same upstream dependency as a
260-
// dylib and use our rlib, causing linker errors because we didn't use
261-
// dllimport.
262-
// 2. We may have multiple crate output types. For example if we are
263-
// emitting a statically linked binary as well as a dynamic library we'll
264-
// want to omit dllimport for the binary but we need to have it for the
265-
// dylib.
266-
//
267-
// For most every day uses, however, this should suffice. During the
268-
// bootstrap we're almost always linking upstream to a dylib for some crate
269-
// type output, so most imports will be tagged with dllimport (somewhat
270-
// appropriately). Otherwise rust dylibs linking against rust dylibs is
271-
// pretty rare in Rust so this will likely always be `false` and we'll never
272-
// tag with dllimport.
273-
//
274-
// Note that we can't just blindly tag all constants with dllimport as can
275-
// cause linkage errors when we're not actually linking against a dll. For
276-
// more info on this see rust-lang/rust#26591.
277-
if ccx.use_dll_storage_attrs() && ccx.upstream_dylib_used(did.krate) {
241+
if ccx.use_dll_storage_attrs() {
278242
llvm::SetDLLStorageClass(c, llvm::DLLImportStorageClass);
279243
}
280244
ccx.externs().borrow_mut().insert(name.to_string(), c);

src/librustc_trans/trans/context.rs

-24
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use llvm;
1212
use llvm::{ContextRef, ModuleRef, ValueRef, BuilderRef};
1313
use metadata::common::LinkMeta;
14-
use metadata::cstore;
1514
use middle::def::ExportMap;
1615
use middle::traits;
1716
use trans::adt;
@@ -788,29 +787,6 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
788787
pub fn use_dll_storage_attrs(&self) -> bool {
789788
self.shared.use_dll_storage_attrs()
790789
}
791-
792-
/// Tests whether the given `krate` (an upstream crate) is ever used as a
793-
/// dynamic library for the final linkage of this crate.
794-
pub fn upstream_dylib_used(&self, krate: ast::CrateNum) -> bool {
795-
let tcx = self.tcx();
796-
let formats = tcx.dependency_formats.borrow();
797-
tcx.sess.crate_types.borrow().iter().any(|ct| {
798-
match formats[ct].get(krate as usize - 1) {
799-
// If a crate is explicitly linked dynamically then we're
800-
// definitely using it dynamically. If it's not being linked
801-
// then currently that means it's being included through another
802-
// dynamic library, so we're including it dynamically.
803-
Some(&Some(cstore::RequireDynamic)) |
804-
Some(&None) => true,
805-
806-
// Static linkage isn't included dynamically and if there's not
807-
// an entry in the array then this crate type isn't actually
808-
// doing much linkage so there's nothing dynamic going on.
809-
Some(&Some(cstore::RequireStatic)) |
810-
None => false,
811-
}
812-
})
813-
}
814790
}
815791

816792
/// Declare any llvm intrinsics that you might need

src/test/auxiliary/xcrate-static.rs

-15
This file was deleted.

src/test/run-pass/xcrate-static.rs

-17
This file was deleted.

0 commit comments

Comments
 (0)