Skip to content

Commit

Permalink
Progress for partial linker stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Apr 25, 2024
1 parent 2be8382 commit bcd4a3b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 15 deletions.
9 changes: 9 additions & 0 deletions slinky-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion slinky/src/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
2 changes: 1 addition & 1 deletion slinky/src/file_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
45 changes: 41 additions & 4 deletions slinky/src/partial_linker_writer.rs
Original file line number Diff line number Diff line change
@@ -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>,
Expand All @@ -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(())
}
}

Expand Down
2 changes: 1 addition & 1 deletion slinky/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions slinky/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub struct Settings {

pub single_segment_mode: bool,

pub partial_scripts_path: Option<PathBuf>,
pub partial_build_segments_path: Option<PathBuf>,
pub partial_scripts_path: PathBuf,
pub partial_build_segments_path: PathBuf,

// Options passed down to each segment
pub alloc_sections: Vec<String>,
Expand Down Expand Up @@ -106,12 +106,12 @@ const fn settings_default_single_segment_mode() -> bool {
false
}

const fn settings_default_partial_scripts_path() -> Option<PathBuf> {
None
fn settings_default_partial_scripts_path() -> PathBuf {
PathBuf::new()
}

const fn settings_default_partial_build_segments_path() -> Option<PathBuf> {
None
fn settings_default_partial_build_segments_path() -> PathBuf {
PathBuf::new()
}

fn settings_default_alloc_sections() -> Vec<String> {
Expand Down Expand Up @@ -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,
)?;
Expand Down

0 comments on commit bcd4a3b

Please sign in to comment.