Skip to content

Commit 0e038bf

Browse files
committed
Fix to avoid having to update the hash in variable names hack manually
1 parent 9cbbd6f commit 0e038bf

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (Modul
9292
block.end_with_void_return(None);
9393
});
9494

95-
println!("module_codegen: {:?} {:?}", cgu_name, &cx.context as *const _);
95+
//println!("module_codegen: {:?} {:?}", cgu_name, &cx.context as *const _);
9696
let mono_items = cgu.items_in_deterministic_order(tcx);
9797
for &(mono_item, (linkage, visibility)) in &mono_items {
9898
mono_item.predefine::<Builder<'_, '_, '_>>(&cx, linkage, visibility);

src/consts.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ impl<'gcc, 'tcx> StaticMethods for CodegenCx<'gcc, 'tcx> {
106106
else {
107107
// If we created the global with the wrong type,
108108
// correct the type.
109-
let name = self.get_global_name(global).expect("global name");
110109
/*let name = llvm::get_value_name(global).to_vec();
111110
llvm::set_value_name(global, b"");
112111
@@ -141,7 +140,7 @@ impl<'gcc, 'tcx> StaticMethods for CodegenCx<'gcc, 'tcx> {
141140
ARGC,
142141
ARGV,
143142
];
144-
if !skip_init.contains(&name) {
143+
if !skip_init.iter().any(|symbol_name| name.starts_with(symbol_name)) {
145144
// TODO: switch to set_initializer when libgccjit supports that.
146145
let memcpy = self.context.get_builtin_function("memcpy");
147146
let dst = self.context.new_cast(None, global, self.type_i8p());

src/context.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ pub struct CodegenCx<'gcc, 'tcx> {
9999
/// Cache of emitted const globals (value -> global)
100100
pub const_globals: RefCell<FxHashMap<RValue<'gcc>, RValue<'gcc>>>,
101101

102+
pub init_argv_var: RefCell<String>,
103+
pub argv_initialized: Cell<bool>,
104+
102105
/// Cache of constant strings,
103106
pub const_cstr_cache: RefCell<FxHashMap<Symbol, LValue<'gcc>>>,
104107

@@ -227,6 +230,8 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
227230
instances: Default::default(),
228231
vtables: Default::default(),
229232
const_globals: Default::default(),
233+
init_argv_var: RefCell::new(String::new()),
234+
argv_initialized: Cell::new(false),
230235
const_cstr_cache: Default::default(),
231236
global_names: Default::default(),
232237
globals: Default::default(),
@@ -254,6 +259,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
254259
}
255260
}
256261

262+
// TODO: remove this method and global_names.
257263
pub fn get_global_name(&self, value: RValue<'gcc>) -> Option<String> {
258264
self.global_names.borrow().get(&value).cloned()
259265
}

src/declare.rs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_codegen_ssa::traits::BaseTypeMethods;
33
use rustc_middle::ty::Ty;
44
use rustc_span::Symbol;
55
use rustc_target::abi::call::FnAbi;
6+
use rustc_symbol_mangling::symbol_name_for_instance_in_crate;
67

78
use crate::abi::FnAbiGccExt;
89
use crate::context::{CodegenCx, unit_name};
@@ -53,19 +54,17 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
5354

5455
pub fn declare_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool, link_section: Option<Symbol>) -> RValue<'gcc> {
5556
//debug!("declare_global(name={:?})", name);
56-
let mut initializer = None;
57-
// NOTE: hack to initialize ARGC_INIT_ARRAY before libc tries to call the function it
58-
// points to.
5957
// FIXME: correctly support global variable initialization.
60-
if name == ARGV_INIT_ARRAY {
61-
let return_type = self.type_void();
62-
let params = [
63-
self.context.new_parameter(None, self.int_type, "argc"),
64-
self.context.new_parameter(None, self.u8_type.make_pointer().make_pointer(), "argv"),
65-
self.context.new_parameter(None, self.u8_type.make_pointer().make_pointer(), "envp"),
66-
];
67-
let function = self.context.new_function(None, FunctionType::Extern, return_type, &params, ARGV_INIT_WRAPPER, false);
68-
initializer = Some(function.get_address(None));
58+
if name.starts_with(ARGV_INIT_ARRAY) {
59+
// NOTE: hack to avoid having to update the names in mangled_std_symbols: we save the
60+
// name of the variable now to actually declare it later.
61+
*self.init_argv_var.borrow_mut() = name.to_string();
62+
63+
let global = self.context.new_global(None, GlobalKind::Imported, ty, name);
64+
if let Some(link_section) = link_section {
65+
global.set_link_section(&link_section.as_str());
66+
}
67+
return global.get_address(None);
6968
}
7069
let global = self.context.new_global(None, GlobalKind::Exported, ty, name);
7170
if is_tls {
@@ -74,9 +73,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
7473
if let Some(link_section) = link_section {
7574
global.set_link_section(&link_section.as_str());
7675
}
77-
if let Some(initializer) = initializer {
78-
global.global_set_initializer_value(initializer);
79-
}
8076
let global = global.get_address(None);
8177
self.globals.borrow_mut().insert(name.to_string(), global);
8278
// NOTE: global seems to only be global in a module. So save the name instead of the value
@@ -100,6 +96,38 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
10096
}
10197

10298
pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> RValue<'gcc> {
99+
// NOTE: hack to avoid having to update the names in mangled_std_symbols: we found the name
100+
// of the variable earlier, so we declare it now.
101+
// Since we don't correctly support initializers yet, we initialize this variable manually
102+
// for now.
103+
if name.starts_with(ARGV_INIT_WRAPPER) && !self.argv_initialized.get() {
104+
let global_name = &*self.init_argv_var.borrow();
105+
let return_type = self.type_void();
106+
let params = [
107+
self.context.new_parameter(None, self.int_type, "argc"),
108+
self.context.new_parameter(None, self.u8_type.make_pointer().make_pointer(), "argv"),
109+
self.context.new_parameter(None, self.u8_type.make_pointer().make_pointer(), "envp"),
110+
];
111+
let function = self.context.new_function(None, FunctionType::Extern, return_type, &params, name, false);
112+
let initializer = function.get_address(None);
113+
114+
let param_types = [
115+
self.int_type,
116+
self.u8_type.make_pointer().make_pointer(),
117+
self.u8_type.make_pointer().make_pointer(),
118+
];
119+
let ty = self.context.new_function_pointer_type(None, return_type, &param_types, false);
120+
121+
let global = self.context.new_global(None, GlobalKind::Exported, ty, global_name);
122+
global.set_link_section(".init_array.00099");
123+
global.global_set_initializer_value(initializer);
124+
let global = global.get_address(None);
125+
self.globals.borrow_mut().insert(global_name.to_string(), global);
126+
// NOTE: global seems to only be global in a module. So save the name instead of the value
127+
// to import it later.
128+
self.global_names.borrow_mut().insert(global, global_name.to_string());
129+
self.argv_initialized.set(true);
130+
}
103131
//debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
104132
let (return_type, params, variadic) = fn_abi.gcc_type(self);
105133
let func = declare_raw_fn(self, name, () /*fn_abi.llvm_cconv()*/, return_type, &params, variadic);

src/mangled_std_symbols.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub const ARGV_INIT_ARRAY: &str = "_ZN3std3sys4unix4args3imp15ARGV_INIT_ARRAY17h60e5c750c1dd807cE";
2-
pub const ARGV_INIT_WRAPPER: &str = "_ZN3std3sys4unix4args3imp15ARGV_INIT_ARRAY12init_wrapper17h3768020202ef2bb7E";
3-
pub const ARGC: &str = "_ZN3std3sys4unix4args3imp4ARGC17h5ff1230607524ec2E";
4-
pub const ARGV: &str = "_ZN3std3sys4unix4args3imp4ARGV17hf68828ed70415113E";
1+
pub const ARGV_INIT_ARRAY: &str = "_ZN3std3sys4unix4args3imp15ARGV_INIT_ARRAY";
2+
pub const ARGV_INIT_WRAPPER: &str = "_ZN3std3sys4unix4args3imp15ARGV_INIT_ARRAY12init_wrapper";
3+
pub const ARGC: &str = "_ZN3std3sys4unix4args3imp4ARGC";
4+
pub const ARGV: &str = "_ZN3std3sys4unix4args3imp4ARGV";

0 commit comments

Comments
 (0)