From bcd4a3bcd3f7a7068b2de9dbd61335e5216d3dc3 Mon Sep 17 00:00:00 2001 From: Angie Date: Thu, 25 Apr 2024 01:18:33 -0400 Subject: [PATCH] Progress for partial linker stuff #20 --- slinky-cli/src/main.rs | 9 ++++++ slinky/src/file_info.rs | 2 +- slinky/src/file_kind.rs | 2 +- slinky/src/partial_linker_writer.rs | 45 ++++++++++++++++++++++++++--- slinky/src/segment.rs | 2 +- slinky/src/settings.rs | 16 +++++----- 6 files changed, 61 insertions(+), 15 deletions(-) diff --git a/slinky-cli/src/main.rs b/slinky-cli/src/main.rs index f44c9d9..65d3e52 100644 --- a/slinky-cli/src/main.rs +++ b/slinky-cli/src/main.rs @@ -32,6 +32,15 @@ fn main() { // println!("settings {:#?}", document.settings); if cli.partial_linking { + let mut writer = slinky::PartialLinkerWriter::new(&document.settings); + + writer.add_all_segment(&document.segments); + + let output_path = cli.output.expect("output path is required for partial linking"); + writer.save_linker_scripts(&output_path).expect("Error writing the linker scripts"); + writer + .write_other_files() + .expect("Error writing other files listed on the document"); } else { let mut writer = slinky::LinkerWriter::new(&document.settings); diff --git a/slinky/src/file_info.rs b/slinky/src/file_info.rs index f1fcd17..1456491 100644 --- a/slinky/src/file_info.rs +++ b/slinky/src/file_info.rs @@ -9,7 +9,7 @@ use std::{ use crate::{absent_nullable::AbsentNullable, file_kind::FileKind, Settings, SlinkyError}; -#[derive(PartialEq, Debug)] +#[derive(PartialEq, Debug, Clone)] pub struct FileInfo { pub path: PathBuf, diff --git a/slinky/src/file_kind.rs b/slinky/src/file_kind.rs index 91004e6..b648308 100644 --- a/slinky/src/file_kind.rs +++ b/slinky/src/file_kind.rs @@ -5,7 +5,7 @@ use std::path::Path; use serde::Deserialize; -#[derive(Deserialize, PartialEq, Debug)] +#[derive(Deserialize, PartialEq, Debug, Clone)] #[serde(rename_all = "snake_case")] pub enum FileKind { Object, diff --git a/slinky/src/partial_linker_writer.rs b/slinky/src/partial_linker_writer.rs index 907e497..aa5f288 100644 --- a/slinky/src/partial_linker_writer.rs +++ b/slinky/src/partial_linker_writer.rs @@ -1,7 +1,9 @@ /* SPDX-FileCopyrightText: © 2024 decompals */ /* SPDX-License-Identifier: MIT */ -use crate::{LinkerWriter, Segment, Settings}; +use std::{collections::HashMap, path::{Path, PathBuf}}; + +use crate::{FileInfo, FileKind, LinkerWriter, Segment, Settings, SlinkyError}; pub struct PartialLinkerWriter<'a> { main_writer: LinkerWriter<'a>, @@ -20,10 +22,45 @@ impl<'a> PartialLinkerWriter<'a> { } } - pub fn add_segment(&mut self, _segment: &Segment) { - let partial_writer = LinkerWriter::new(self.settings); + pub fn add_all_segment(&mut self, segments: &[Segment]) { + self.main_writer.begin_sections(); + + for segment in segments { + let mut partial_writer = LinkerWriter::new(self.settings); + + partial_writer.add_single_segment(segment); + + self.partial_writers.push(partial_writer); + + let mut p = PathBuf::new(); + + p.push(&self.settings.partial_build_segments_path); + p.push(&format!("{}.o", segment.name)); + + let mut reference_segment = segment.clone(); + reference_segment.files = vec![FileInfo { + path: p, + kind: FileKind::Object, + subfile: "".into(), + pad_amount: 0, + section: "".into(), + linker_offset_name: "".into(), + section_order: HashMap::new(), + files: Vec::new(), + dir: PathBuf::new(), + }]; + self.main_writer.add_segment(&reference_segment); + } + + self.main_writer.end_sections(); + } + + pub fn save_linker_scripts(&self, _path: &Path) -> Result<(), SlinkyError> { + Ok(()) + } - self.partial_writers.push(partial_writer); + pub fn write_other_files(&self) -> Result<(), SlinkyError> { + Ok(()) } } diff --git a/slinky/src/segment.rs b/slinky/src/segment.rs index 75ee714..db76769 100644 --- a/slinky/src/segment.rs +++ b/slinky/src/segment.rs @@ -9,7 +9,7 @@ use crate::{ Settings, SlinkyError, }; -#[derive(PartialEq, Debug)] +#[derive(PartialEq, Debug, Clone)] pub struct Segment { /// Name of the segment pub name: String, diff --git a/slinky/src/settings.rs b/slinky/src/settings.rs index 90c81b6..f7d5eec 100644 --- a/slinky/src/settings.rs +++ b/slinky/src/settings.rs @@ -29,8 +29,8 @@ pub struct Settings { pub single_segment_mode: bool, - pub partial_scripts_path: Option, - pub partial_build_segments_path: Option, + pub partial_scripts_path: PathBuf, + pub partial_build_segments_path: PathBuf, // Options passed down to each segment pub alloc_sections: Vec, @@ -106,12 +106,12 @@ const fn settings_default_single_segment_mode() -> bool { false } -const fn settings_default_partial_scripts_path() -> Option { - None +fn settings_default_partial_scripts_path() -> PathBuf { + PathBuf::new() } -const fn settings_default_partial_build_segments_path() -> Option { - None +fn settings_default_partial_build_segments_path() -> PathBuf { + PathBuf::new() } fn settings_default_alloc_sections() -> Vec { @@ -302,11 +302,11 @@ impl SettingsSerial { .single_segment_mode .get_non_null("single_segment_mode", settings_default_single_segment_mode)?; - let partial_scripts_path = self.partial_scripts_path.get_optional_nullable( + let partial_scripts_path = self.partial_scripts_path.get_non_null( "partial_scripts_path", settings_default_partial_scripts_path, )?; - let partial_build_segments_path = self.partial_build_segments_path.get_optional_nullable( + let partial_build_segments_path = self.partial_build_segments_path.get_non_null( "partial_build_segments_path", settings_default_partial_build_segments_path, )?;