Skip to content

Commit

Permalink
Start working on Partial linking support
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Apr 24, 2024
1 parent d195b11 commit 6aca2b2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 19 deletions.
8 changes: 7 additions & 1 deletion slinky-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ struct Cli {
/// Output file. Print to stdout if missing
#[arg(short, long)]
output: Option<PathBuf>,

/// Generate linker script for partial linking multiple segments.
/// Requires both `partial_scripts_path` and `partial_build_segments_path` YAML settings to be set.
#[arg(short, long, default_value_t = false)]
partial_linking: bool,
}

fn main() {
Expand All @@ -33,7 +38,8 @@ fn main() {
}
writer.end_sections();

if let Some(output_path) = cli.output {
if cli.partial_linking {
} else if let Some(output_path) = cli.output {
writer
.save_linker_script(&output_path)
.expect("Error writing the linker script");
Expand Down
2 changes: 2 additions & 0 deletions slinky/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod segment;
mod document;

mod linker_writer;
mod partial_linker_writer;

pub use error::SlinkyError;

Expand All @@ -28,3 +29,4 @@ pub use segment::Segment;
pub use document::Document;

pub use linker_writer::LinkerWriter;
pub use partial_linker_writer::PartialLinkerWriter;
13 changes: 8 additions & 5 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,6 @@ impl<'a> LinkerWriter<'a> {

ret
}

#[must_use]
pub fn get(&self) -> &HashSet<String> {
&self.linker_symbols
}
}

impl LinkerWriter<'_> {
Expand All @@ -440,6 +435,14 @@ impl LinkerWriter<'_> {
}
}

// Getters / Setters
impl LinkerWriter<'_> {
#[must_use]
pub fn get_linker_symbols(&self) -> &HashSet<String> {
&self.linker_symbols
}
}

// internal functions
impl LinkerWriter<'_> {
fn writeln(&mut self, line: &str) {
Expand Down
41 changes: 41 additions & 0 deletions slinky/src/partial_linker_writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

use crate::{LinkerWriter, Segment, Settings};

pub struct PartialLinkerWriter<'a> {
main_writer: LinkerWriter<'a>,
partial_writers: Vec<LinkerWriter<'a>>,

settings: &'a Settings,
}

impl<'a> PartialLinkerWriter<'a> {
pub fn new(settings: &'a Settings) -> Self {
Self {
main_writer: LinkerWriter::new(settings),
partial_writers: Vec::new(),

settings,
}
}

pub fn add_segment(&mut self, _segment: &Segment) {
let partial_writer = LinkerWriter::new(self.settings);

self.partial_writers.push(partial_writer);
}
}

// Getters / Setters
impl PartialLinkerWriter<'_> {
#[must_use]
pub fn get_main_writer(&self) -> &LinkerWriter {
&self.main_writer
}

#[must_use]
pub fn get_partial_writers(&self) -> &Vec<LinkerWriter> {
&self.partial_writers
}
}
56 changes: 43 additions & 13 deletions slinky/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct Settings {
pub sections_denylist: Vec<String>,
pub discard_wildcard_section: bool,

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

// Options passed down to each segment
pub alloc_sections: Vec<String>,
pub noload_sections: Vec<String>,
Expand All @@ -46,35 +49,35 @@ fn settings_default_base_path() -> PathBuf {
PathBuf::new()
}

fn settings_default_linker_symbols_style() -> LinkerSymbolsStyle {
const fn settings_default_linker_symbols_style() -> LinkerSymbolsStyle {
LinkerSymbolsStyle::Splat
}

fn settings_default_d_path() -> Option<PathBuf> {
const fn settings_default_d_path() -> Option<PathBuf> {
None
}

fn settings_default_target_path() -> Option<PathBuf> {
const fn settings_default_target_path() -> Option<PathBuf> {
None
}

fn settings_default_symbols_header_path() -> Option<PathBuf> {
const fn settings_default_symbols_header_path() -> Option<PathBuf> {
None
}

fn settings_default_symbols_header_type() -> String {
"char".to_string()
}

fn settings_default_symbols_header_as_array() -> bool {
const fn settings_default_symbols_header_as_array() -> bool {
true
}

fn settings_default_hardcoded_gp_value() -> Option<u32> {
const fn settings_default_hardcoded_gp_value() -> Option<u32> {
None
}

fn settings_default_sections_allowlist() -> Vec<String> {
const fn settings_default_sections_allowlist() -> Vec<String> {
vec![]
}

Expand All @@ -93,10 +96,18 @@ fn settings_default_sections_denylist() -> Vec<String> {
]
}

fn settings_default_discard_wildcard_section() -> bool {
const fn settings_default_discard_wildcard_section() -> bool {
true
}

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

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

fn settings_default_alloc_sections() -> Vec<String> {
vec![
".text".into(),
Expand All @@ -115,23 +126,23 @@ fn settings_default_noload_sections() -> Vec<String> {
]
}

fn settings_default_subalign() -> Option<u32> {
const fn settings_default_subalign() -> Option<u32> {
Some(0x10)
}

fn settings_default_segment_start_align() -> Option<u32> {
const fn settings_default_segment_start_align() -> Option<u32> {
None
}

fn settings_default_section_end_align() -> Option<u32> {
const fn settings_default_section_end_align() -> Option<u32> {
Some(0x10)
}

fn settings_default_wildcard_sections() -> bool {
const fn settings_default_wildcard_sections() -> bool {
true
}

fn settings_default_fill_value() -> Option<u32> {
const fn settings_default_fill_value() -> Option<u32> {
Some(0)
}

Expand All @@ -155,6 +166,9 @@ impl Default for Settings {
sections_denylist: settings_default_sections_denylist(),
discard_wildcard_section: settings_default_discard_wildcard_section(),

partial_scripts_path: settings_default_partial_scripts_path(),
partial_build_segments_path: settings_default_partial_build_segments_path(),

alloc_sections: settings_default_alloc_sections(),
noload_sections: settings_default_noload_sections(),

Expand Down Expand Up @@ -201,6 +215,9 @@ pub(crate) struct SettingsSerial {
#[serde(default)]
pub discard_wildcard_section: AbsentNullable<bool>,

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

// Options passed down to each Segment
#[serde(default)]
pub alloc_sections: AbsentNullable<Vec<String>>,
Expand Down Expand Up @@ -268,6 +285,15 @@ impl SettingsSerial {
settings_default_discard_wildcard_section,
)?;

let partial_scripts_path = self.partial_scripts_path.get_optional_nullable(
"partial_scripts_path",
settings_default_partial_scripts_path,
)?;
let partial_build_segments_path = self.partial_build_segments_path.get_optional_nullable(
"partial_build_segments_path",
settings_default_partial_build_segments_path,
)?;

if d_path.is_some() && target_path.is_none() {
return Err(SlinkyError::MissingRequiredFieldCombo {
required: "target_path".to_string(),
Expand Down Expand Up @@ -318,6 +344,10 @@ impl SettingsSerial {
sections_allowlist_extra,
sections_denylist,
discard_wildcard_section,

partial_scripts_path,
partial_build_segments_path,

alloc_sections,
noload_sections,
subalign,
Expand Down

0 comments on commit 6aca2b2

Please sign in to comment.