Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use String in MergeError::ExternalToolError stdout/stderr, instead of Vec<u8> #5

Merged
merged 4 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
499 changes: 309 additions & 190 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "armerge"
version = "1.5.1"
version = "2.0.0"
authors = ["tux3 <[email protected]>"]
edition = "2021"
rust-version = "1.56"
Expand Down
4 changes: 2 additions & 2 deletions src/arbuilder/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl MacArBuilder {
reason: "Failed to merge object files with `libtool`".to_string(),
tool: "libtool".to_string(),
args,
stdout: output.stdout,
stderr: output.stderr,
stdout: String::from_utf8_lossy(&output.stdout).to_string(),
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/archives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ pub fn create_index(archive_path: &std::path::Path) -> Result<(), MergeError> {
reason: "Failed to create archive index with `ranlib`".to_string(),
tool: "ranlib".to_string(),
args: archive_path.iter().map(|p| p.to_owned()).collect(),
stdout: output.stdout,
stderr: output.stderr,
stdout: String::from_utf8_lossy(&output.stdout).to_string(),
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
})
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/merge_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ use thiserror::Error;
#[derive(Debug, Error)]
pub enum MergeError {
#[error(
"{reason}: {tool:?} {args:?})\nstdout: {}\nstderr: {}",
String::from_utf8_lossy(stdout),
String::from_utf8_lossy(stderr)
"{reason}: {tool:?} {args:?})\nstdout: {stdout}\nstderr: {stderr}"
)]
ExternalToolError {
reason: String,
tool: String,
args: Vec<OsString>,
stdout: Vec<u8>,
stderr: Vec<u8>,
stdout: String,
stderr: String,
},
#[error("failed to launch external tool `{tool}`: {inner})")]
ExternalToolLaunchError { tool: String, inner: io::Error },
Expand Down
8 changes: 7 additions & 1 deletion src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ pub fn merge_required_objects(
#[allow(clippy::if_same_then_else)] // Clippy can't see both [cfg] at once
if contents_type == ArchiveContents::Elf {
#[cfg(feature = "objpoke_symbols")]
builtin_filter::merge_required_objects(obj_dir, merged_path, objs, keep_or_remove, keeps)?;
builtin_filter::merge_required_objects(
obj_dir,
merged_path,
objs,
keep_or_remove,
regexes,
)?;
#[cfg(not(feature = "objpoke_symbols"))]
system_filter::merge_required_objects(obj_dir, merged_path, objs, keep_or_remove, regexes)?;
} else if contents_type == ArchiveContents::MachO {
Expand Down
13 changes: 9 additions & 4 deletions src/objects/builtin_filter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::MergeError;
use crate::{ArmergeKeepOrRemove, MergeError};
use regex::Regex;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
Expand All @@ -10,20 +10,25 @@ pub fn merge_required_objects(
_obj_dir: &Path,
merged_path: &Path,
objects: &HashMap<PathBuf, ObjectSyms>,
keep_regexes: &[Regex],
keep_or_remove: ArmergeKeepOrRemove,
regexes: &[Regex],
) -> Result<(), MergeError> {
if keep_or_remove == ArmergeKeepOrRemove::RemoveSymbols {
unimplemented!("--remove-symbols not yet supported with builtin filter")
}

// The merging part is still not builtin, it has to be done by a real linker
merge::create_merged_object(merged_path, &[], objects.keys(), false)?;

// Filtering the symbols is faster in pure Rust, compared to calling the system's objcopy
let merged_elf = std::fs::read(merged_path)?;
let filtered_elf = objpoke::elf::localize_elf_symbols(merged_elf, keep_regexes)
let filtered_elf = objpoke::elf::localize_elf_symbols(merged_elf, regexes)
.map_err(|e| MergeError::InternalError(e.into()))?;

// If a symbol we localize is in a COMDAT section group, we also want to turn it into a regular
// section group. Otherwise the local symbol is not really local, because the containing section
// could later get COMDAT-folded with other (potentially incompatible) object files.
let filtered_elf = objpoke::elf::demote_comdat_groups(filtered_elf, keep_regexes)
let filtered_elf = objpoke::elf::demote_comdat_groups(filtered_elf, regexes)
.map_err(|e| MergeError::InternalError(e.into()))?;

std::fs::write(merged_path, filtered_elf)?;
Expand Down
1 change: 0 additions & 1 deletion src/objects/filter_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::collections::{HashMap, HashSet};
use std::path::PathBuf;

use crate::{ArmergeKeepOrRemove, MergeError};
use rayon::iter::IntoParallelIterator;
use rayon::prelude::*;
use regex::Regex;
use tracing::{event_enabled, info, Level};
Expand Down
4 changes: 2 additions & 2 deletions src/objects/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ pub fn create_merged_object(
reason: "Failed to merged object files".to_string(),
tool: ld_path.to_string_lossy().to_string(),
args,
stdout: output.stdout,
stderr: output.stderr,
stdout: String::from_utf8_lossy(&output.stdout).to_string(),
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
})
}
}
6 changes: 3 additions & 3 deletions src/objects/system_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn create_filtered_merged_macho_object(
let extra_args = &["-unexported_symbols_list".as_ref(), filter_list.as_os_str()];
let merged_firstpass_path = merged_path.parent().unwrap().join("merged_firstpass.o");
create_merged_object(&merged_firstpass_path, extra_args, objects, false)?;
create_merged_object(merged_path, &[], &[&merged_firstpass_path], true)?;
create_merged_object(merged_path, &[], [&merged_firstpass_path], true)?;

Ok(())
}
Expand Down Expand Up @@ -135,8 +135,8 @@ fn filter_symbols(object_path: &Path, filter_list_path: &Path) -> Result<(), Mer
reason: "Failed to filter symbols".to_string(),
tool: objcopy_path.to_string_lossy().to_string(),
args,
stdout: output.stdout,
stderr: output.stderr,
stdout: String::from_utf8_lossy(&output.stdout).to_string(),
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
})
}
}
Expand Down
Loading