Skip to content

Commit

Permalink
fix: cannot lgcc on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Chronostasys committed Aug 29, 2024
1 parent 8c2588f commit f1e97be
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
32 changes: 30 additions & 2 deletions pl_linker/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ fn get_linux_lib_paths() -> Vec<String> {
paths
}

// fn get_libgcc_path() -> Result<String, String> {
// let base_path = "/usr/lib/gcc/x86_64-linux-gnu";
// let entries = std::fs::read_dir(base_path).map_err(|e| format!("Failed to read directory {}: {}", base_path, e))?;

// for entry in entries {
// let entry = entry.map_err(|e| format!("Failed to read directory entry: {}", e))?;
// if entry.path().is_dir() {
// let version = entry.file_name().into_string().map_err(|e| format!("Failed to convert OsString to String: {:?}", e))?;
// return Ok(format!("{}/{}", base_path, version));
// }
// }

// Err("No valid gcc version directory found".to_string())
// }


impl Linker for LdLinker {
fn add_object(&mut self, path: &Path) -> Result<(), LinkerError> {
let path_str = path
Expand All @@ -115,9 +131,21 @@ impl Linker for LdLinker {
paths.iter().for_each(|lib| {
self.push_args(&format!("-L{}", lib.as_str()));
});
// // Add the path to the libgcc library
// match get_libgcc_path() {
// Ok(libgcc_dir) => {
// self.push_args(&format!("-L{}", libgcc_dir));
// }
// Err(e) => {
// return Err(LinkerError::LinkError(format!(
// "Failed to get libgcc path: {}",
// e
// )));
// }
// }
// libs and link args
[
"-pie",
"-no-pie",
"-zrelro",
"--hash-style=gnu",
"--build-id",
Expand All @@ -133,7 +161,7 @@ impl Linker for LdLinker {
"-lunwind",
"--no-as-needed",
"-ldl",
"-lgcc",
// "-lgcc",
// "/usr/lib/gcc/x86_64-linux-gnu/<version>/crtendS.o",
"/lib/x86_64-linux-gnu/crtn.o",
]
Expand Down
25 changes: 25 additions & 0 deletions src/ast/builder/llvmbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,31 @@ impl<'a, 'ctx> IRBuilder<'a, 'ctx> for LLVMBuilder<'a, 'ctx> {
self.builder.position_at(v.get_parent().unwrap(), &v);
}
fn finalize_debug(&self) {
#[cfg(target_os = "linux")]
{
// add __dso_handle defination
let dso = self.module.add_global(
self.context.i8_type().array_type(0),
Some(AddressSpace::from(0)),
"__dso_handle",
);
dso.set_initializer(&self.context.i8_type().array_type(0).const_zero());
dso.set_linkage(Linkage::WeakAny);
let dso = self.module.add_global(
self.context.i8_type().array_type(0),
Some(AddressSpace::from(0)),
"_dso_handle",
);
dso.set_initializer(&self.context.i8_type().array_type(0).const_zero());
dso.set_linkage(Linkage::WeakAny);
let dso = self.module.add_global(
self.context.i8_type().array_type(0),
Some(AddressSpace::from(0)),
"dso_handle",
);
dso.set_initializer(&self.context.i8_type().array_type(0).const_zero());
dso.set_linkage(Linkage::WeakAny);
}
self.dibuilder.finalize();
}
fn print_to_file(&self, file: &Path) -> Result<(), String> {
Expand Down
9 changes: 3 additions & 6 deletions vm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ use std::process::Command;
// format!("{}.{}.0", &LIBUV_VERSION[..dotidx], next_minor_version)
// }



fn build<P: AsRef<Path>>(source_path: &P) {
let src_path = source_path.as_ref().join("src");
let unix_path = src_path.join("unix");
Expand Down Expand Up @@ -298,9 +296,7 @@ fn main() {
let mut uv_src = PathBuf::from(out_dir);
uv_src.push(LIBUV_DIR);
if !uv_src.exists() {
run("git", |cmd| {
cmd.arg("clone").arg(LIBUV_REPO).arg(&uv_src)
});
run("git", |cmd| cmd.arg("clone").arg(LIBUV_REPO).arg(&uv_src));

// run("git", |cmd| {
// cmd.arg("clone")
Expand All @@ -316,7 +312,8 @@ fn main() {
}

build(&uv_src);

#[cfg(target_os = "linux")]
cc::Build::new().file("dso_handle.c").compile("dso_handle");

// println!("cargo:rustc-link-lib=static:+whole-archive=uv");
}
6 changes: 6 additions & 0 deletions vm/dso_handle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// dso_handle.c
#include <stddef.h>

const void *const __dso_handle __attribute__((weak)) = &__dso_handle;
const void *const _dso_handle __attribute__((weak)) = &__dso_handle;
const void *const dso_handle __attribute__((weak)) = &__dso_handle;
7 changes: 7 additions & 0 deletions vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,10 @@ fn millitime() -> i64 {

#[cfg(all(windows, feature = "jitdylib"))]
mod compiler_rt;

// (.data._rust_extern_with_linkage___dso_handle+0x0): undefined reference to `__dso_handle'
// I want to define it manually
#[cfg(target_os = "linux")]
extern "C" {
pub static __dso_handle: *const u8;
}

0 comments on commit f1e97be

Please sign in to comment.