Debug why python.obj doesn't get linked correctly

This commit is contained in:
Felix Schütt
2022-11-15 23:44:15 +01:00
parent 2c604e88f9
commit e3b317ab35
4 changed files with 53 additions and 28 deletions

View File

@@ -226,9 +226,6 @@ impl CreateExe {
println!("Target: {}", target.triple()); println!("Target: {}", target.triple());
println!("Format: {:?}", object_format); println!("Format: {:?}", object_format);
#[cfg(not(windows))]
let wasm_object_path = PathBuf::from("wasm.o");
#[cfg(windows)]
let wasm_object_path = PathBuf::from("wasm.obj"); let wasm_object_path = PathBuf::from("wasm.obj");
if let Some(header_path) = self.header.as_ref() { if let Some(header_path) = self.header.as_ref() {
@@ -238,6 +235,7 @@ impl CreateExe {
std::fs::copy(&header_path, Path::new("static_defs.h")) std::fs::copy(&header_path, Path::new("static_defs.h"))
.context("Could not access given header file")?; .context("Could not access given header file")?;
if let Some(setup) = cross_compilation.as_ref() { if let Some(setup) = cross_compilation.as_ref() {
println!("compile zig static_defs.h");
self.compile_zig( self.compile_zig(
output_path, output_path,
wasm_module_path, wasm_module_path,
@@ -310,6 +308,7 @@ impl CreateExe {
writer.write_all(header_file_src.as_bytes())?; writer.write_all(header_file_src.as_bytes())?;
writer.flush()?; writer.flush()?;
if let Some(setup) = cross_compilation.as_ref() { if let Some(setup) = cross_compilation.as_ref() {
println!("compile zig static_defs.h 2");
self.compile_zig( self.compile_zig(
output_path, output_path,
object_file_path, object_file_path,
@@ -481,9 +480,6 @@ impl CreateExe {
// write C src to disk // write C src to disk
let c_src_path = tempdir_path.join("wasmer_main.c"); let c_src_path = tempdir_path.join("wasmer_main.c");
#[cfg(not(windows))]
let c_src_obj = tempdir_path.join("wasmer_main.o");
#[cfg(windows)]
let c_src_obj = tempdir_path.join("wasmer_main.obj"); let c_src_obj = tempdir_path.join("wasmer_main.obj");
std::fs::write( std::fs::write(
@@ -493,8 +489,13 @@ impl CreateExe {
.as_bytes(), .as_bytes(),
)?; )?;
println!("compile_c: output {c_src_path:?} to {c_src_obj:?}");
run_c_compile(&c_src_path, &c_src_obj, target_triple.clone()) run_c_compile(&c_src_path, &c_src_obj, target_triple.clone())
.context("Failed to compile C source code")?; .context("Failed to compile C source code")?;
println!("linking code...");
LinkCode { LinkCode {
object_paths: vec![c_src_obj, wasm_object_path], object_paths: vec![c_src_obj, wasm_object_path],
output_path, output_path,
@@ -505,6 +506,8 @@ impl CreateExe {
.run() .run()
.context("Failed to link objects together")?; .context("Failed to link objects together")?;
println!("linking code done");
Ok(()) Ok(())
} }
@@ -543,13 +546,18 @@ impl CreateExe {
libwasmer_path.pop(); libwasmer_path.pop();
if let Some(entrypoint) = pirita_main_atom.as_ref() { if let Some(entrypoint) = pirita_main_atom.as_ref() {
println!("entrypint - static wasmer_main.c source:");
let c_code = Self::generate_pirita_wasmer_main_c_static(pirita_atoms, entrypoint); let c_code = Self::generate_pirita_wasmer_main_c_static(pirita_atoms, entrypoint);
println!("{c_code}");
std::fs::write(&c_src_path, c_code)?; std::fs::write(&c_src_path, c_code)?;
} else { } else {
println!("no entrypint - default wasmer_main.c source");
std::fs::write(&c_src_path, WASMER_STATIC_MAIN_C_SOURCE)?; std::fs::write(&c_src_path, WASMER_STATIC_MAIN_C_SOURCE)?;
} }
println!("header code path code:");
if !header_code_path.is_dir() { if !header_code_path.is_dir() {
println!("{}", std::fs::read_to_string(&header_code_path).unwrap());
header_code_path.pop(); header_code_path.pop();
} }
@@ -565,6 +573,7 @@ impl CreateExe {
let mut cmd = Command::new(zig_binary_path); let mut cmd = Command::new(zig_binary_path);
cmd.arg("cc"); cmd.arg("cc");
cmd.arg("--verbose");
cmd.arg("-target"); cmd.arg("-target");
cmd.arg(&zig_triple); cmd.arg(&zig_triple);
cmd.arg(&format!("-L{}", libwasmer_path.display())); cmd.arg(&format!("-L{}", libwasmer_path.display()));
@@ -577,6 +586,8 @@ impl CreateExe {
cmd.arg("-lunwind"); cmd.arg("-lunwind");
} }
println!("pirita object path (should contain all functions): {}", object_path.display());
cmd.arg(&object_path); cmd.arg(&object_path);
cmd.arg(&c_src_path); cmd.arg(&c_src_path);
@@ -584,12 +595,15 @@ impl CreateExe {
cmd.arg(volume_obj.clone()); cmd.arg(volume_obj.clone());
} }
println!("zig cc command: {cmd:#?} -> {output_path:?}");
cmd.arg("-o") cmd.arg("-o")
.arg(&output_path) .arg(&output_path)
.output() .output()
.context("Could not execute `zig`")? .context("Could not execute `zig`")?
}; };
if !compilation.status.success() { if !compilation.status.success() {
println!("compilation error");
return Err(anyhow::anyhow!(String::from_utf8_lossy( return Err(anyhow::anyhow!(String::from_utf8_lossy(
&compilation.stderr &compilation.stderr
) )
@@ -605,9 +619,7 @@ impl CreateExe {
target: &Target, target: &Target,
output_path: &Path, output_path: &Path,
) -> anyhow::Result<PathBuf> { ) -> anyhow::Result<PathBuf> {
#[cfg(not(windows))]
let volume_object_path = output_path.join("volumes.o");
#[cfg(windows)]
let volume_object_path = output_path.join("volumes.obj"); let volume_object_path = output_path.join("volumes.obj");
let mut volumes_object = get_object_for_target(target.triple())?; let mut volumes_object = get_object_for_target(target.triple())?;
@@ -658,11 +670,8 @@ impl CreateExe {
for (atom_name, atom_bytes) in file.get_all_atoms() { for (atom_name, atom_bytes) in file.get_all_atoms() {
std::fs::create_dir_all(output_path.join("atoms"))?; std::fs::create_dir_all(output_path.join("atoms"))?;
#[cfg(not(windows))]
let object_path = output_path.join("atoms").join(&format!("{atom_name}.o"));
#[cfg(windows)]
let object_path = output_path.join("atoms").join(&format!("{atom_name}.obj")); let object_path = output_path.join("atoms").join(&format!("{atom_name}.obj"));
std::fs::create_dir_all(output_path.join("atoms").join(&atom_name))?; std::fs::create_dir_all(output_path.join("atoms").join(&atom_name))?;
let header_path = output_path let header_path = output_path
@@ -704,14 +713,23 @@ impl CreateExe {
metadata_length, metadata_length,
); );
println!("symbols: pirita header file:");
println!("{header_file_src}");
println!("------");
let mut writer = BufWriter::new(File::create(&object_path)?); let mut writer = BufWriter::new(File::create(&object_path)?);
obj.write_stream(&mut writer) obj.write_stream(&mut writer)
.map_err(|err| anyhow::anyhow!(err.to_string()))?; .map_err(|err| anyhow::anyhow!(err.to_string()))?;
writer.flush()?; writer.flush()?;
std::fs::copy(&object_path, format!("{atom_name}.obj"));
let mut writer = BufWriter::new(File::create(&header_path)?); let mut writer = BufWriter::new(File::create(&header_path)?);
writer.write_all(header_file_src.as_bytes())?; writer.write_all(header_file_src.as_bytes())?;
writer.flush()?; writer.flush()?;
println!("pirita obj created at {}", object_path.display());
println!("pirita static_defs.h created at {}", header_path.display());
} }
#[cfg(not(feature = "static-artifact-create"))] #[cfg(not(feature = "static-artifact-create"))]
ObjectFormat::Symbols => { ObjectFormat::Symbols => {
@@ -768,9 +786,6 @@ impl CreateExe {
link_objects.push(volume_object_path); link_objects.push(volume_object_path);
#[cfg(not(windows))]
let c_src_obj = working_dir.join("wasmer_main.o");
#[cfg(windows)]
let c_src_obj = working_dir.join("wasmer_main.obj"); let c_src_obj = working_dir.join("wasmer_main.obj");
for obj in std::fs::read_dir(working_dir.join("atoms"))? { for obj in std::fs::read_dir(working_dir.join("atoms"))? {
@@ -815,7 +830,6 @@ impl CreateExe {
let mut include_dir = libwasmer_path.clone(); let mut include_dir = libwasmer_path.clone();
include_dir.pop(); include_dir.pop();
include_dir.push("include"); include_dir.push("include");
println!("include dir: {}", include_dir.display());
let mut cmd = Command::new(zig_binary_path); let mut cmd = Command::new(zig_binary_path);
let mut cmd_mut: &mut Command = cmd let mut cmd_mut: &mut Command = cmd
.arg("cc") .arg("cc")
@@ -824,10 +838,14 @@ impl CreateExe {
.arg(&zig_triple) .arg(&zig_triple)
.arg(&format!("-L{}", libwasmer_path.display())) .arg(&format!("-L{}", libwasmer_path.display()))
.arg(&format!("-l:{}", lib_filename)) .arg(&format!("-l:{}", lib_filename))
.arg("-isystem")
.arg(&format!("-I{}", include_dir.display())); .arg(&format!("-I{}", include_dir.display()));
if !zig_triple.contains("windows") { if !zig_triple.contains("windows") {
cmd_mut = cmd_mut.arg("-lunwind"); cmd_mut = cmd_mut.arg("-lunwind");
} }
println!("pirita: output {cmd_mut:?} to {output_path:?}");
cmd_mut cmd_mut
.args(link_objects.into_iter()) .args(link_objects.into_iter())
.arg(&c_src_path) .arg(&c_src_path)
@@ -859,7 +877,7 @@ impl CreateExe {
} }
} }
ObjectFormat::Symbols => { ObjectFormat::Symbols => {
let object_file_path = working_dir.join("atoms").join(&format!("{entrypoint}.o")); let object_file_path = working_dir.join("atoms").join(&format!("{entrypoint}.obj"));
let static_defs_file_path = working_dir let static_defs_file_path = working_dir
.join("atoms") .join("atoms")
.join(&entrypoint) .join(&entrypoint)
@@ -867,6 +885,7 @@ impl CreateExe {
let volumes_obj_path = Self::write_volume_obj(volume_bytes, target, tempdir_path)?; let volumes_obj_path = Self::write_volume_obj(volume_bytes, target, tempdir_path)?;
if let Some(setup) = cross_compilation.as_ref() { if let Some(setup) = cross_compilation.as_ref() {
println!("compile zig static_defs.h pirita");
self.compile_zig( self.compile_zig(
output_path, output_path,
object_file_path, object_file_path,
@@ -995,7 +1014,9 @@ impl CreateExe {
let _ = std::fs::create_dir_all(&working_dir); let _ = std::fs::create_dir_all(&working_dir);
let (store, _) = self.compiler.get_store_for_target(target.clone())?; let (store, _) = self.compiler.get_store_for_target(target.clone())?;
println!("create objs pirita...");
Self::create_objs_pirita(&store, file, &target, working_dir, object_format)?; Self::create_objs_pirita(&store, file, &target, working_dir, object_format)?;
println!("pirita objs created!");
let volumes_obj = file.get_volumes_as_fileblock(); let volumes_obj = file.get_volumes_as_fileblock();
self.link_exe_from_dir( self.link_exe_from_dir(

View File

@@ -539,8 +539,10 @@ impl Artifact {
metadata_binary.extend(MetadataHeader::new(serialized_data.len()).into_bytes()); metadata_binary.extend(MetadataHeader::new(serialized_data.len()).into_bytes());
metadata_binary.extend(serialized_data); metadata_binary.extend(serialized_data);
let (_compile_info, symbol_registry) = metadata.split(); let (_compile_info, mut symbol_registry) = metadata.split();
symbol_registry.set_windows_underscore(true);
let compilation: wasmer_types::compilation::function::Compilation = compiler let compilation: wasmer_types::compilation::function::Compilation = compiler
.compile_module( .compile_module(
target, target,

View File

@@ -71,15 +71,25 @@ pub struct ModuleMetadata {
/// A simple metadata registry /// A simple metadata registry
pub struct ModuleMetadataSymbolRegistry { pub struct ModuleMetadataSymbolRegistry {
/// Whether to prefix function symbols with a "_" when compiling the object
pub windows_underscore: bool,
/// Symbol prefix stirng /// Symbol prefix stirng
pub prefix: String, pub prefix: String,
} }
impl ModuleMetadataSymbolRegistry {
/// Sets the self.windows_underscore field
pub fn set_windows_underscore(&mut self, b: bool) {
self.windows_underscore = b;
}
}
impl ModuleMetadata { impl ModuleMetadata {
/// Get mutable ref to compile info and a copy of the registry /// Get mutable ref to compile info and a copy of the registry
pub fn split(&mut self) -> (&mut CompileModuleInfo, ModuleMetadataSymbolRegistry) { pub fn split(&mut self) -> (&mut CompileModuleInfo, ModuleMetadataSymbolRegistry) {
let compile_info = &mut self.compile_info; let compile_info = &mut self.compile_info;
let symbol_registry = ModuleMetadataSymbolRegistry { let symbol_registry = ModuleMetadataSymbolRegistry {
windows_underscore: false,
prefix: self.prefix.clone(), prefix: self.prefix.clone(),
}; };
(compile_info, symbol_registry) (compile_info, symbol_registry)
@@ -88,6 +98,7 @@ impl ModuleMetadata {
/// Returns symbol registry. /// Returns symbol registry.
pub fn get_symbol_registry(&self) -> ModuleMetadataSymbolRegistry { pub fn get_symbol_registry(&self) -> ModuleMetadataSymbolRegistry {
ModuleMetadataSymbolRegistry { ModuleMetadataSymbolRegistry {
windows_underscore: false,
prefix: self.prefix.clone(), prefix: self.prefix.clone(),
} }
} }

View File

@@ -91,9 +91,6 @@ struct WasmerCreateObj {
impl Default for WasmerCreateObj { impl Default for WasmerCreateObj {
fn default() -> Self { fn default() -> Self {
#[cfg(not(windows))]
let output_object_path = PathBuf::from("wasm.o");
#[cfg(windows)]
let output_object_path = PathBuf::from("wasm.obj"); let output_object_path = PathBuf::from("wasm.obj");
Self { Self {
current_dir: std::env::current_dir().unwrap(), current_dir: std::env::current_dir().unwrap(),
@@ -271,9 +268,6 @@ fn create_obj(args: Vec<&'static str>, keyword_needle: &str, keyword: &str) -> a
let wasm_path = operating_dir.join(create_exe_test_wasm_path()); let wasm_path = operating_dir.join(create_exe_test_wasm_path());
#[cfg(not(windows))]
let object_path = operating_dir.join("wasm.o");
#[cfg(windows)]
let object_path = operating_dir.join("wasm.obj"); let object_path = operating_dir.join("wasm.obj");
let output: Vec<u8> = WasmerCreateObj { let output: Vec<u8> = WasmerCreateObj {
@@ -336,9 +330,6 @@ fn create_exe_with_object_input(args: Vec<&'static str>) -> anyhow::Result<()> {
let wasm_path = operating_dir.join(create_exe_test_wasm_path()); let wasm_path = operating_dir.join(create_exe_test_wasm_path());
#[cfg(not(windows))]
let object_path = operating_dir.join("wasm.o");
#[cfg(windows)]
let object_path = operating_dir.join("wasm.obj"); let object_path = operating_dir.join("wasm.obj");
WasmerCreateObj { WasmerCreateObj {