diff --git a/rust/lanzatool/src/pe.rs b/rust/lanzatool/src/pe.rs index a91851c..0111585 100644 --- a/rust/lanzatool/src/pe.rs +++ b/rust/lanzatool/src/pe.rs @@ -1,3 +1,4 @@ +use std::ffi::OsString; use std::fs; use std::io::Write; use std::os::unix::fs::MetadataExt; @@ -8,8 +9,6 @@ use std::process::Command; use anyhow::{Context, Result}; use goblin::pe::PE; -use crate::utils; - use tempfile::TempDir; /// Attach all information that lanzaboote needs into the PE binary. @@ -94,12 +93,11 @@ fn wrap_in_pe( .open(&image_path) .context("Failed to generate named temp file")?; - let mut args: Vec = sections.iter().flat_map(Section::to_objcopy).collect(); - let extra_args = vec![ - utils::path_to_string(stub), - utils::path_to_string(&image_path), - ]; - args.extend(extra_args); + let mut args: Vec = sections.iter().flat_map(Section::to_objcopy).collect(); + + [stub.as_os_str(), image_path.as_os_str()] + .iter() + .for_each(|a| args.push(a.into())); let status = Command::new("objcopy") .args(&args) @@ -122,12 +120,19 @@ struct Section { } impl Section { - fn to_objcopy(&self) -> Vec { + /// Create objcopy `-add-section` command line parameters that + /// attach the section to a PE file. + fn to_objcopy(&self) -> Vec { + // There is unfortunately no format! for OsString, so we cannot + // just format a path. + let mut map_str: OsString = format!("{}=", self.name).into(); + map_str.push(&self.file_path); + vec![ - String::from("--add-section"), - format!("{}={}", self.name, utils::path_to_string(&self.file_path)), - String::from("--change-section-vma"), - format!("{}={:#x}", self.name, self.offset), + OsString::from("--add-section"), + map_str, + OsString::from("--change-section-vma"), + format!("{}={:#x}", self.name, self.offset).into(), ] } }