Skip to content

Commit 59afbe0

Browse files
committed
Fix for the relocation bug
1 parent 8bc4863 commit 59afbe0

File tree

7 files changed

+65
-17
lines changed

7 files changed

+65
-17
lines changed

build_system/src/build.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,28 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
158158

159159
let mut env = env.clone();
160160
/*rustflags.push_str(" -C link-arg=-Wl,--verbose");
161-
rustflags.push_str(" -C link-arg=-Wl,--warn-ifunc-textrel");
161+
rustflags.push_str(" -C link-arg=-Wl,--warn-shared-textrel"); // Mold and Gold.
162+
//rustflags.push_str(" -C link-arg=-Wl,--warn-textrel"); // Mold
162163
rustflags.push_str(" -C link-arg=-Wl,--warn-unresolved-symbols");
163164
rustflags.push_str(" -C link-arg=-Wl,--fatal-warnings");
165+
rustflags.push_str(" -C link-arg=-Wl,--print-gc-sections");*/
166+
//rustflags.push_str(" -C link-arg=-Wl,--no-apply-dynamic-relocs"); // Mold
167+
/*rustflags.push_str(" -C link-arg=-Wl,--warn-ifunc-textrel");
164168
rustflags.push_str(" -C link-arg=-Wl,--warn-backrefs");
165169
rustflags.push_str(" -C link-arg=-Wl,-znotext");
166170
//rustflags.push_str(" -C link-arg=-Wl,--emit-relocs");
167171
//rustflags.push_str(" -C link-arg=-Wl,--trace-symbol=memchr::arch::x86_64::memchr::memchr_raw::FN");
168172
//rustflags.push_str(" -C link-arg=-Wl,--trace-symbol=_ZN6memchr4arch6x86_646memchr10memchr_raw2FN17haaf621f7b8ca567eE");
169173
//rustflags.push_str(" -C link-arg=-Wl,--print-map");
170-
//rustflags.push_str(" -C link-arg=-Wl,");
174+
//rustflags.push_str(" -C link-arg=-Wl,");*/
175+
//rustflags.push_str(" -Clinker=/usr/bin/ld.gold");
176+
// TODO: try with verbose, warnings and logs.
177+
//rustflags.push_str(" -Clinker=/usr/bin/clang");
178+
//rustflags.push_str(" -Clink-arg=--ld-path=/usr/bin/mold");
179+
//rustflags.push_str(" -Clink-arg=--ld-path=/usr/bin/ld.bfd");
180+
//rustflags.push_str(" -Clink-arg=--ld-path=/usr/bin/ld.gold");
181+
env.insert("RUSTC_LOG".to_string(), "rustc_codegen_ssa::back::link=info".to_string());
171182
env.insert("RUSTFLAGS".to_string(), rustflags);
172-
env.insert("RUSTC_LOG".to_string(), "rustc_codegen_ssa::back::link=info".to_string());*/
173183
run_command_with_output_and_env(&args, Some(&start_dir), Some(&env))?;
174184

175185
// Copy files to sysroot

build_system/src/test.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -697,14 +697,23 @@ fn test_libcore(env: &Env, args: &TestArg) -> Result<(), String> {
697697
let rustflags =
698698
env.entry("RUSTFLAGS".to_string())
699699
.or_default();
700-
rustflags.push_str(" -C link-arg=-Wl,--verbose");
701-
rustflags.push_str(" -C link-arg=-Wl,--warn-ifunc-textrel");
702-
rustflags.push_str(" -C link-arg=-Wl,--warn-unresolved-symbols");
703-
rustflags.push_str(" -C link-arg=-Wl,--fatal-warnings");
700+
//rustflags.push_str(" -C link-arg=-Wl,--verbose");
701+
//rustflags.push_str(" -C link-arg=-Wl,--fatal-warnings");
702+
//rustflags.push_str(" -C link-arg=-Wl,--warn-unresolved-symbols");
703+
//rustflags.push_str(" -C link-arg=-Wl,--warn-shared-textrel"); // Mold and Gold.
704+
//rustflags.push_str(" -C link-arg=-Wl,--warn-textrel"); // Mold
705+
//rustflags.push_str(" -C link-arg=-Wl,--print-gc-sections");
706+
//rustflags.push_str(" -C link-arg=-Wl,--no-apply-dynamic-relocs"); // Mold
707+
/*rustflags.push_str(" -C link-arg=-Wl,--warn-ifunc-textrel");
704708
rustflags.push_str(" -C link-arg=-Wl,--warn-backrefs");
705709
rustflags.push_str(" -C link-arg=-Wl,-znotext");
706710
rustflags.push_str(" -C link-arg=-Wl,--emit-relocs");
707-
rustflags.push_str(" -C link-arg=-Wl,--trace-symbol=memchr::arch::x86_64::memchr::memchr_raw::FN");
711+
rustflags.push_str(" -C link-arg=-Wl,--trace-symbol=memchr::arch::x86_64::memchr::memchr_raw::FN");*/
712+
//rustflags.push_str(" -Clinker=/usr/bin/ld.gol");
713+
//rustflags.push_str(" -Clinker=/usr/bin/clang");
714+
//rustflags.push_str(" -Clink-arg=--ld-path=/usr/bin/mold");
715+
//rustflags.push_str(" -Clink-arg=--ld-path=/usr/bin/ld.bfd");
716+
//rustflags.push_str(" -Clink-arg=--ld-path=/usr/bin/ld.gold");
708717
// FIXME FIXME: seems like RUSTFLAGS is not set here.
709718
//rustflags.push_str(" -C link-arg=-Wl,--trace-symbol=_ZN6memchr4arch6x86_646memchr10memchr_raw2FN17haaf621f7b8ca567eE");
710719
//rustflags.push_str(" -C link-arg=-Wl,--print-map");

src/base.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ pub fn symbol_visibility_to_gcc(visibility: SymbolVisibility) -> gccjit::Visibil
4343

4444
pub fn global_linkage_to_gcc(linkage: Linkage) -> GlobalKind {
4545
match linkage {
46-
Linkage::External => GlobalKind::Imported,
47-
Linkage::AvailableExternally => GlobalKind::Imported,
46+
Linkage::External => GlobalKind::Exported,
47+
Linkage::AvailableExternally => GlobalKind::Exported,
4848
Linkage::LinkOnceAny => unimplemented!(),
4949
Linkage::LinkOnceODR => unimplemented!(),
5050
Linkage::WeakAny => unimplemented!(),
@@ -151,8 +151,23 @@ pub fn compile_codegen_unit(
151151
});
152152
}
153153

154-
if tcx.sess.relocation_model() == rustc_target::spec::RelocModel::Static {
155-
context.add_command_line_option("-fno-pie");
154+
match tcx.sess.relocation_model() {
155+
rustc_target::spec::RelocModel::Static => {
156+
//println!("*** Static");
157+
context.add_command_line_option("-fno-pie");
158+
context.add_driver_option("-fno-pie");
159+
},
160+
rustc_target::spec::RelocModel::Pic => {
161+
//println!("*** Pic");
162+
context.add_command_line_option("-fPIC");
163+
context.add_driver_option("-fPIC");
164+
},
165+
rustc_target::spec::RelocModel::Pie => {
166+
//println!("*** Pie");
167+
context.add_command_line_option("-fPIE");
168+
context.add_driver_option("-fPIE");
169+
},
170+
model => eprintln!("Unsupported relocation model: {:?}", model),
156171
}
157172

158173
let target_cpu = gcc_util::target_cpu(tcx.sess);

src/consts.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ impl<'gcc, 'tcx> StaticCodegenMethods for CodegenCx<'gcc, 'tcx> {
9797
// def_id).
9898
let var_name = format!("{:?}", global);
9999
if var_name.contains("FN") && var_name.contains("memchr") {
100-
println!("Var name: {:?}", var_name);
100+
//println!("Var name: {:?}", var_name);
101+
println!("INITIALIZE: {:?} = {:?}", var_name, value);
101102

103+
/*
102104
let ptr_type = value.get_type().make_pointer();
103105
104106
// TODO: remove \x01
@@ -108,8 +110,9 @@ impl<'gcc, 'tcx> StaticCodegenMethods for CodegenCx<'gcc, 'tcx> {
108110
imp_name.push_str(&var_name);
109111
110112
// FIXME: if I understand correctly the code in cg_llvm, the kind should be Imported.
111-
/*let imp_global = self.context.new_global(None, GlobalKind::Exported, ptr_type, &imp_name);
112-
imp_global.global_set_initializer_rvalue(global.get_address(None));*/
113+
let imp_global = self.context.new_global(None, GlobalKind::Exported, ptr_type, &imp_name);
114+
imp_global.global_set_initializer_rvalue(global.get_address(None));
115+
*/
113116

114117
/*
115118
/*let context = gccjit::Context::default();
@@ -122,7 +125,6 @@ impl<'gcc, 'tcx> StaticCodegenMethods for CodegenCx<'gcc, 'tcx> {
122125
let my_name = format!("MY_NAME${}", var_name);
123126
//let global = self.context.new_global(None, GlobalKind::Exported, fn_ptr_type, my_name);
124127
//global.add_attribute(VarAttribute::Used);
125-
println!("{:?} = {:?}", var_name, value);
126128
127129
let my_func_name = format!("MY_FUNC${}", var_name);
128130
let func = self.context.new_function(None, FunctionType::Exported, void_type, &[], &my_func_name, false);
@@ -166,6 +168,7 @@ impl<'gcc, 'tcx> StaticCodegenMethods for CodegenCx<'gcc, 'tcx> {
166168
//let value = self.context.new_bitcast(None, value, fn_ptr_type); // Also WORKS
167169
let value = self.context.new_bitcast(None, value, val_llty);*/
168170
global.global_set_initializer_rvalue(value);
171+
println!("=== AFTER INITIALIZE");
169172
}
170173
else {
171174
global.global_set_initializer_rvalue(value);
@@ -326,10 +329,13 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
326329
}
327330

328331
let is_tls = fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
332+
if sym.contains("memchr") && sym.contains("FN") {
333+
println!("** DECLARE");
334+
}
329335
let global = self.declare_global(
330336
sym,
331337
gcc_type,
332-
GlobalKind::Exported,
338+
GlobalKind::Imported,
333339
is_tls,
334340
fn_attrs.link_section,
335341
);

src/declare.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
2929
}
3030
global
3131
} else {
32+
// HERE: ExternalLinkage.
3233
self.declare_global(name, ty, GlobalKind::Exported, is_tls, link_section)
3334
}
3435
}
@@ -69,6 +70,9 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
6970
is_tls: bool,
7071
link_section: Option<Symbol>,
7172
) -> LValue<'gcc> {
73+
if name.contains("memchr") && name.contains("FN") {
74+
println!("{}: {:?}: {:?}", self.codegen_unit.name(), name, global_kind);
75+
}
7276
let global = self.context.new_global(None, global_kind, ty, name);
7377
if is_tls {
7478
global.set_tls_model(self.tls_model);

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ impl CodegenBackend for GccCodegenBackend {
269269

270270
fn new_context<'gcc, 'tcx>(tcx: TyCtxt<'tcx>) -> Context<'gcc> {
271271
let context = Context::default();
272+
//context.add_driver_option("-pie");
272273
if tcx.sess.target.arch == "x86" || tcx.sess.target.arch == "x86_64" {
273274
context.add_command_line_option("-masm=intel");
274275
}

src/mono_item.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ impl<'gcc, 'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
3232
let gcc_type = self.layout_of(ty).gcc_type(self);
3333

3434
let is_tls = attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
35+
if symbol_name.contains("memchr") && symbol_name.contains("FN") {
36+
println!("** DECLARE static");
37+
}
3538
let global = self.define_global(symbol_name, gcc_type, is_tls, attrs.link_section);
3639
#[cfg(feature = "master")]
3740
global.add_attribute(VarAttribute::Visibility(base::visibility_to_gcc(visibility)));

0 commit comments

Comments
 (0)