Skip to content

Commit cd5d42a

Browse files
committed
Correctly import foreign statics
Previously foreign statics would actually cause a local static to be defined and exported. This issue was found because std::env::vars() was found to return no env vars despite many being defined. This was caused by libstd importing environ as foreign static. The accidental definition of environ caused libstd to read a null pointer which was interpreted as there being no environment variables at all. Also fix tests. STDOUT is not defined by libc. The correct name is stdout. This previously worked as STDOUT was incorrectly defined as null pointer during codegen.
1 parent e690fb1 commit cd5d42a

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
lines changed

src/consts.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use gccjit::{LValue, RValue, ToRValue, Type};
1+
use gccjit::{GlobalKind, LValue, RValue, ToRValue, Type};
22
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods, DerivedTypeMethods, StaticMethods};
33
use rustc_hir as hir;
44
use rustc_hir::Node;
@@ -218,7 +218,13 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
218218
}
219219

220220
let is_tls = fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
221-
let global = self.declare_global(&sym, llty, is_tls, fn_attrs.link_section);
221+
let global = self.declare_global(
222+
&sym,
223+
llty,
224+
GlobalKind::Exported,
225+
is_tls,
226+
fn_attrs.link_section,
227+
);
222228

223229
if !self.tcx.is_reachable_non_generic(def_id) {
224230
// TODO(antoyo): set visibility.
@@ -389,6 +395,6 @@ fn check_and_apply_linkage<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, attrs: &Codeg
389395
// don't do this then linker errors can be generated where the linker
390396
// complains that one object files has a thread local version of the
391397
// symbol and another one doesn't.
392-
cx.declare_global(&sym, llty, is_tls, attrs.link_section)
398+
cx.declare_global(&sym, llty, GlobalKind::Imported, is_tls, attrs.link_section)
393399
}
394400
}

src/declare.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
2222
global
2323
}
2424
else {
25-
self.declare_global(name, ty, is_tls, link_section)
25+
self.declare_global(name, ty, GlobalKind::Exported, is_tls, link_section)
2626
}
2727
}
2828

@@ -47,8 +47,8 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
4747
unsafe { std::mem::transmute(func) }
4848
}*/
4949

50-
pub fn declare_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool, link_section: Option<Symbol>) -> LValue<'gcc> {
51-
let global = self.context.new_global(None, GlobalKind::Exported, ty, name);
50+
pub fn declare_global(&self, name: &str, ty: Type<'gcc>, global_kind: GlobalKind, is_tls: bool, link_section: Option<Symbol>) -> LValue<'gcc> {
51+
let global = self.context.new_global(None, global_kind, ty, name);
5252
if is_tls {
5353
global.set_tls_model(self.tls_model);
5454
}

tests/run/assign.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ mod libc {
5151
pub fn fflush(stream: *mut i32) -> i32;
5252
pub fn printf(format: *const i8, ...) -> i32;
5353

54-
pub static STDOUT: *mut i32;
54+
pub static stdout: *mut i32;
5555
}
5656
}
5757

@@ -67,7 +67,7 @@ mod intrinsics {
6767
pub fn panic(_msg: &str) -> ! {
6868
unsafe {
6969
libc::puts("Panicking\0" as *const str as *const u8);
70-
libc::fflush(libc::STDOUT);
70+
libc::fflush(libc::stdout);
7171
intrinsics::abort();
7272
}
7373
}

tests/run/int_overflow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mod libc {
4949
pub fn puts(s: *const u8) -> i32;
5050
pub fn fflush(stream: *mut i32) -> i32;
5151

52-
pub static STDOUT: *mut i32;
52+
pub static stdout: *mut i32;
5353
}
5454
}
5555

@@ -65,7 +65,7 @@ mod intrinsics {
6565
pub fn panic(_msg: &str) -> ! {
6666
unsafe {
6767
libc::puts("Panicking\0" as *const str as *const u8);
68-
libc::fflush(libc::STDOUT);
68+
libc::fflush(libc::stdout);
6969
intrinsics::abort();
7070
}
7171
}

tests/run/mut_ref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mod libc {
5353
pub fn fflush(stream: *mut i32) -> i32;
5454
pub fn printf(format: *const i8, ...) -> i32;
5555

56-
pub static STDOUT: *mut i32;
56+
pub static stdout: *mut i32;
5757
}
5858
}
5959

@@ -69,7 +69,7 @@ mod intrinsics {
6969
pub fn panic(_msg: &str) -> ! {
7070
unsafe {
7171
libc::puts("Panicking\0" as *const str as *const u8);
72-
libc::fflush(libc::STDOUT);
72+
libc::fflush(libc::stdout);
7373
intrinsics::abort();
7474
}
7575
}

tests/run/operations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ mod libc {
5959
pub fn puts(s: *const u8) -> i32;
6060
pub fn fflush(stream: *mut i32) -> i32;
6161

62-
pub static STDOUT: *mut i32;
62+
pub static stdout: *mut i32;
6363
}
6464
}
6565

@@ -75,7 +75,7 @@ mod intrinsics {
7575
pub fn panic(_msg: &str) -> ! {
7676
unsafe {
7777
libc::puts("Panicking\0" as *const str as *const u8);
78-
libc::fflush(libc::STDOUT);
78+
libc::fflush(libc::stdout);
7979
intrinsics::abort();
8080
}
8181
}

0 commit comments

Comments
 (0)