Skip to content

Commit 38f7a1b

Browse files
committed
rustc: Pass --enable-long-section-names to gcc
This was quite a curious bug on windows, and the details can be found in the comment I added to src/librustc/back/link.rs
1 parent 0459ee7 commit 38f7a1b

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

src/liblibc/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
#![allow(missing_doc)]
7676
#![allow(uppercase_variables)]
7777

78+
#![feature(link_args)] // NOTE: remove after stage0
79+
7880
#[cfg(test)] extern crate std;
7981
#[cfg(test)] extern crate test;
8082
#[cfg(test)] extern crate native;
@@ -197,6 +199,11 @@ pub use funcs::posix88::unistd::{rmdir, unlink, write};
197199
#[link(name = "m")]
198200
extern {}
199201

202+
// NOTE: remove this after a stage0 snap
203+
#[cfg(stage0, windows)]
204+
#[link_args = "-Wl,--enable-long-section-names"]
205+
extern {}
206+
200207
/// A wrapper for a nullable pointer. Don't use this except for interacting
201208
/// with libc. Basically Option, but without the dependance on libstd.
202209
// If/when libprim happens, this can be removed in favor of that

src/librustc/back/link.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,33 @@ fn link_args(sess: &Session,
11271127
// DWARF stack unwinding will not work.
11281128
// This behavior may be overridden by --link-args "-static-libgcc"
11291129
args.push(~"-shared-libgcc");
1130+
1131+
// And here, we see obscure linker flags #45. On windows, it has been
1132+
// found to be necessary to have this flag to compile liblibc.
1133+
//
1134+
// First a bit of background. On Windows, the file format is not ELF,
1135+
// but COFF (at least according to LLVM). COFF doesn't officially allow
1136+
// for section names over 8 characters, apparently. Our metadata
1137+
// section, ".note.rustc", you'll note is over 8 characters.
1138+
//
1139+
// On more recent versions of gcc on mingw, apparently the section name
1140+
// is *not* truncated, but rather stored elsewhere in a separate lookup
1141+
// table. On older versions of gcc, they apparently always truncated the
1142+
// section names (at least in some cases). Truncating the section name
1143+
// actually creates "invalid" objects [1] [2], but only for some
1144+
// introspection tools, not in terms of whether it can be loaded.
1145+
//
1146+
// Long story shory, passing this flag forces the linker to *not*
1147+
// truncate section names (so we can find the metadata section after
1148+
// it's compiled). The real kicker is that rust compiled just fine on
1149+
// windows for quite a long time *without* this flag, so I have no idea
1150+
// why it suddenly started failing for liblibc. Regardless, we
1151+
// definitely don't want section name truncation, so we're keeping this
1152+
// flag for windows.
1153+
//
1154+
// [1] - https://sourceware.org/bugzilla/show_bug.cgi?id=13130
1155+
// [2] - https://code.google.com/p/go/issues/detail?id=2139
1156+
args.push(~"-Wl,--enable-long-section-names");
11301157
}
11311158

11321159
if sess.targ_cfg.os == abi::OsAndroid {

src/test/run-make/lto-smoke-c/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ ifneq ($(shell uname),Darwin)
44
EXTRAFLAGS := -lm -lrt -ldl -lpthread
55
endif
66

7+
# Apparently older versions of GCC segfault if -g is passed...
8+
CC := $(CC:-g=)
9+
710
all:
811
$(RUSTC) foo.rs -Z lto
912
ln -s $(call STATICLIB,foo-*) $(call STATICLIB,foo)

src/test/run-make/lto-smoke-c/foo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#[crate_type = "staticlib"];
11+
#![crate_type = "staticlib"]
1212

1313
#[no_mangle]
1414
pub extern "C" fn foo() {}

0 commit comments

Comments
 (0)