white-list .rcgu.o files in the linking phase, otherwise dump them into the normal linking pile (#4246)

* white-list .rcgu.o files in the linking phase, otherwise dump them into the normal linking pile

* simplify a bit

* remove noisy trace

* remove wrong comment
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2025-06-03 16:40:28 +02:00
1 parent ae64d9534c
commit 2ee54e507b
1 file changed
+18 -12
+18 -12
View File
@@ -1693,6 +1693,7 @@ impl BuildRequest {
let rlib_contents = std::fs::read(rlib)?; let rlib_contents = std::fs::read(rlib)?;
let mut reader = ar::Archive::new(std::io::Cursor::new(rlib_contents)); let mut reader = ar::Archive::new(std::io::Cursor::new(rlib_contents));
let mut keep_linker_rlib = false;
while let Some(Ok(object_file)) = reader.next_entry() { while let Some(Ok(object_file)) = reader.next_entry() {
let name = std::str::from_utf8(object_file.header().identifier()).unwrap(); let name = std::str::from_utf8(object_file.header().identifier()).unwrap();
if name.ends_with(".rmeta") { if name.ends_with(".rmeta") {
@@ -1704,24 +1705,30 @@ impl BuildRequest {
} }
// rlibs might contain dlls/sos/lib files which we don't want to include // rlibs might contain dlls/sos/lib files which we don't want to include
if name.ends_with(".dll") //
|| name.ends_with(".so") // This catches .dylib, .so, .dll, .lib, .o, etc files that are not compatible with
|| name.ends_with(".lib") // our "fat archive" linking process.
|| name.ends_with(".dylib") //
{ // We only trust `.rcgu.o` files to make it into the --all_load archive.
compiler_rlibs.push(rlib.to_owned()); // This is a temporary stopgap to prevent issues with libraries that generate
// object files that are not compatible with --all_load.
// see https://github.com/DioxusLabs/dioxus/issues/4237
if !(name.ends_with(".rcgu.o") || name.ends_with(".obj")) {
keep_linker_rlib = true;
continue; continue;
} }
if !(name.ends_with(".o") || name.ends_with(".obj")) {
tracing::debug!("Unknown object file in rlib: {:?}", name);
}
archive_has_contents = true; archive_has_contents = true;
out_ar out_ar
.append(&object_file.header().clone(), object_file) .append(&object_file.header().clone(), object_file)
.context("Failed to add object file to archive")?; .context("Failed to add object file to archive")?;
} }
// Some rlibs contain weird artifacts that we don't want to include in the fat archive.
// However, we still want them around in the linker in case the regular linker can handle them.
if keep_linker_rlib {
compiler_rlibs.push(rlib.clone());
}
} }
let bytes = out_ar.into_inner().context("Failed to finalize archive")?; let bytes = out_ar.into_inner().context("Failed to finalize archive")?;
@@ -1798,8 +1805,7 @@ impl BuildRequest {
args.push("-Wl,--export-dynamic-symbol,main".to_string()); args.push("-Wl,--export-dynamic-symbol,main".to_string());
} }
LinkerFlavor::Darwin => { LinkerFlavor::Darwin => {
// `-all_load` is an extra step to ensure that all symbols are loaded (different than force_load) args.push("-Wl,-exported_symbol,_main".to_string());
args.push("-Wl,-all_load".to_string());
} }
LinkerFlavor::Msvc => { LinkerFlavor::Msvc => {
// Prevent alsr from overflowing 32 bits // Prevent alsr from overflowing 32 bits