Skip to content

Commit d01e7d1

Browse files
committed
WIP: Fix undefined symbol errors
1 parent e85346e commit d01e7d1

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/back/lto.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::fs::{self, File};
2121
use std::path::{Path, PathBuf};
2222
use std::sync::Arc;
2323

24-
use gccjit::{Context, OutputKind};
24+
use gccjit::{Context, FnAttribute, FunctionType, GlobalKind, OutputKind};
2525
use object::read::archive::ArchiveFile;
2626
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared};
2727
use rustc_codegen_ssa::back::symbol_export;
@@ -178,8 +178,6 @@ pub(crate) fn run_fat(
178178
let dcx = cgcx.create_dcx();
179179
let dcx = dcx.handle();
180180
let lto_data = prepare_lto(cgcx, dcx)?;
181-
let symbols_below_threshold =
182-
lto_data.symbols_below_threshold.iter().map(|c| c.as_ptr()).collect::<Vec<_>>();
183181
fat_lto(
184182
cgcx,
185183
dcx,
@@ -315,11 +313,28 @@ fn fat_lto(
315313
}
316314
save_temp_bitcode(cgcx, &module, "lto.input");
317315

316+
let int_type = module.module_llvm.context.new_type::<i32>();
318317
for symbol in symbols_below_threshold {
319-
println!("Internalize {}", symbol);
318+
println!("*** Keeping symbol: {}", symbol);
319+
module.module_llvm.context.new_global(None, GlobalKind::Imported, int_type, symbol);
320320
// TODO: Create a function that is always_inline and that calls the symbol here (e.g.
321321
// main)?
322322
}
323+
let void_type = module.module_llvm.context.new_type::<()>();
324+
let main_func = module.module_llvm.context.new_function(None, FunctionType::Extern, void_type, &[], "main", false);
325+
main_func.add_attribute(FnAttribute::Used);
326+
327+
// NOTE: look at the code from 64b30d344ce54f8ee496cb3590b4c7cf3cb30447 to see previous
328+
// attemps.
329+
let func = module.module_llvm.context.new_function(None, FunctionType::Exported, void_type, &[], "__my_call_main", false);
330+
func.add_attribute(FnAttribute::AlwaysInline);
331+
func.add_attribute(FnAttribute::Inline);
332+
func.add_attribute(FnAttribute::Used);
333+
let block = func.new_block("start");
334+
let call = module.module_llvm.context.new_call(None, main_func, &[]);
335+
block.add_eval(None, call);
336+
block.end_with_void_return(None);
337+
323338
// Internalize everything below threshold to help strip out more modules and such.
324339
/*unsafe {
325340
let ptr = symbols_below_threshold.as_ptr();

src/back/write.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ pub(crate) unsafe fn codegen(
169169
if fat_lto {
170170
context.add_command_line_option("-flto=auto");
171171
context.add_command_line_option("-flto-partition=one");
172+
173+
// FIXME: the problem is probably that the code is only in GIMPLE IR while
174+
// we would want to get the optimized asm done from LTO.
175+
// ===> But we call "gcc -x lto" later, so that should be asm and not
176+
// GIMPLE IR.
172177
}
173178

174179
context.add_driver_option("-Wl,-r");
@@ -206,6 +211,8 @@ pub(crate) unsafe fn codegen(
206211
context.add_driver_option("lto");
207212
add_pic_option(&context, module.module_llvm.relocation_model);
208213
context.add_driver_option(lto_path);
214+
// TODO TODO: inspect the resulting file to see if it contains the GIMPLE IR or
215+
// the asm.
209216

210217
context.compile_to_file(OutputKind::ObjectFile, path);
211218
} else {

0 commit comments

Comments
 (0)