Skip to content

Commit

Permalink
Fix partial linking related paths not being properly escaped
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Jul 16, 2024
1 parent 600d412 commit c20ea9c
Show file tree
Hide file tree
Showing 18 changed files with 207 additions and 167 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix partial linking related paths not being properly escaped.
- Fix `partial_build_segments_folder` not being properly prefixed on some places.

## [0.2.4] - 2024-07-15

### Fixed
Expand Down
25 changes: 21 additions & 4 deletions slinky/src/partial_linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ impl<'a> PartialLinkerWriter<'a> {

let mut p = PathBuf::new();

p.push(&self.d.settings.partial_build_segments_folder);
p.push(
&self
.rs
.escape_path(&self.d.settings.partial_build_segments_folder)?,
);
p.push(&format!("{}.o", segment.name));

let mut reference_segment = segment.clone();
Expand All @@ -62,7 +66,11 @@ impl<'a> PartialLinkerWriter<'a> {
for (partial, name) in &self.partial_writers {
let mut p = PathBuf::new();

p.push(&self.d.settings.partial_scripts_folder);
p.push(
&self
.rs
.escape_path(&self.d.settings.partial_scripts_folder)?,
);
p.push(&format!("{}.ld", name));

partial.export_linker_script_to_file(&p)?;
Expand All @@ -78,12 +86,21 @@ impl<'a> PartialLinkerWriter<'a> {
for (partial, name) in &self.partial_writers {
let mut target_path = PathBuf::new();

target_path.push(&self.d.settings.partial_build_segments_folder);
target_path.push(&self.rs.escape_path(&self.d.settings.base_path)?);
target_path.push(
&self
.rs
.escape_path(&self.d.settings.partial_build_segments_folder)?,
);
target_path.push(&format!("{}.o", name));

let mut d_path = PathBuf::new();

d_path.push(&self.d.settings.partial_scripts_folder);
d_path.push(
&self
.rs
.escape_path(&self.d.settings.partial_scripts_folder)?,
);
d_path.push(&format!("{}.d", name));

partial.export_dependencies_file_to_file(&d_path, &target_path)?;
Expand Down
4 changes: 2 additions & 2 deletions slinky/src/runtime_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl RuntimeSettings {
/// Replace all the `{key}` instances on the `path` argument with the corresponding value specified on the global `custom_options`.
///
/// If the `key` is not present on the custom options then it returns an error.
pub(crate) fn escape_path(&self, path: &Path) -> Result<PathBuf, SlinkyError> {
pub fn escape_path(&self, path: &Path) -> Result<PathBuf, SlinkyError> {
let mut new_path = PathBuf::new();

for component in path.iter() {
Expand Down Expand Up @@ -121,7 +121,7 @@ impl RuntimeSettings {
Ok(new_path)
}

pub(crate) fn should_emit_entry(
pub fn should_emit_entry(
&self,
exclude_if_any: &[(String, String)],
exclude_if_all: &[(String, String)],
Expand Down
19 changes: 16 additions & 3 deletions slinky/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ fn test_partial_linking_script_generation(
let mut p = PathBuf::new();

p.push("..");
p.push(&document.settings.partial_scripts_folder);
p.push(
&rs.escape_path(&document.settings.partial_scripts_folder)
.expect("Not able to escape path"),
);
p.push(&format!("{}.ld", name));

let expected_partial_ld_contents =
Expand Down Expand Up @@ -168,14 +171,24 @@ fn test_partial_linking_d_generation(#[files("../tests/partial_linking/*.d")] d_
let mut p = PathBuf::new();

p.push("..");
p.push(&document.settings.partial_scripts_folder);
p.push(
&rs.escape_path(&document.settings.partial_scripts_folder)
.expect("Unable to escape path"),
);
p.push(&format!("{}.d", name));

let expected_partial_ld_contents =
fs::read_to_string(p).expect("unable to read expected d file");

let mut partial_target = PathBuf::new();
partial_target.push(&document.settings.partial_build_segments_folder);
partial_target.push(
&rs.escape_path(&document.settings.base_path)
.expect("Failed to escape path"),
);
partial_target.push(
&rs.escape_path(&document.settings.partial_build_segments_folder)
.expect("Failed to escape path"),
);
partial_target.push(&format!("{}.o", name));
assert_eq!(
expected_partial_ld_contents,
Expand Down
14 changes: 7 additions & 7 deletions tests/partial_linking/follow_segment.d
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
build/rom.elf: \
build/segments/boot.o \
build/segments/kanji.o \
build/segments/main.o
build/us/rom.elf: \
build/us/segments/boot.o \
build/us/segments/kanji.o \
build/us/segments/main.o

build/segments/boot.o:
build/segments/kanji.o:
build/segments/main.o:
build/us/segments/boot.o:
build/us/segments/kanji.o:
build/us/segments/main.o:
48 changes: 24 additions & 24 deletions tests/partial_linking/follow_segment.ld
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ SECTIONS
{
FILL(0x00000000);
boot_TEXT_START = .;
build/segments/boot.o(.text*);
build/us/segments/boot.o(.text*);
. = ALIGN(., 0x8);
boot_TEXT_END = .;
boot_TEXT_SIZE = ABSOLUTE(boot_TEXT_END - boot_TEXT_START);

boot_DATA_START = .;
build/segments/boot.o(.data*);
build/us/segments/boot.o(.data*);
. = ALIGN(., 0x8);
boot_DATA_END = .;
boot_DATA_SIZE = ABSOLUTE(boot_DATA_END - boot_DATA_START);

boot_RODATA_START = .;
build/segments/boot.o(.rodata*);
build/us/segments/boot.o(.rodata*);
. = ALIGN(., 0x8);
. = ALIGN(., 0x80);
boot_RODATA_END = .;
boot_RODATA_SIZE = ABSOLUTE(boot_RODATA_END - boot_RODATA_START);

boot_SDATA_START = .;
build/segments/boot.o(.sdata*);
build/us/segments/boot.o(.sdata*);
. = ALIGN(., 0x8);
boot_SDATA_END = .;
boot_SDATA_SIZE = ABSOLUTE(boot_SDATA_END - boot_SDATA_START);
Expand All @@ -46,26 +46,26 @@ SECTIONS
{
FILL(0x00000000);
boot_SBSS_START = .;
build/segments/boot.o(.sbss*);
build/us/segments/boot.o(.sbss*);
. = ALIGN(., 0x8);
. = ALIGN(., 0x20);
boot_SBSS_END = .;
boot_SBSS_SIZE = ABSOLUTE(boot_SBSS_END - boot_SBSS_START);

boot_SCOMMON_START = .;
build/segments/boot.o(.scommon*);
build/us/segments/boot.o(.scommon*);
. = ALIGN(., 0x8);
boot_SCOMMON_END = .;
boot_SCOMMON_SIZE = ABSOLUTE(boot_SCOMMON_END - boot_SCOMMON_START);

boot_BSS_START = .;
build/segments/boot.o(.bss*);
build/us/segments/boot.o(.bss*);
. = ALIGN(., 0x8);
boot_BSS_END = .;
boot_BSS_SIZE = ABSOLUTE(boot_BSS_END - boot_BSS_START);

bootCOMMON_START = .;
build/segments/boot.o(COMMON*);
build/us/segments/boot.o(COMMON*);
. = ALIGN(., 0x8);
bootCOMMON_END = .;
bootCOMMON_SIZE = ABSOLUTE(bootCOMMON_END - bootCOMMON_START);
Expand All @@ -92,26 +92,26 @@ SECTIONS
{
FILL(0x00000000);
kanji_TEXT_START = .;
build/segments/kanji.o(.text*);
build/us/segments/kanji.o(.text*);
. = ALIGN(., 0x8);
kanji_TEXT_END = .;
kanji_TEXT_SIZE = ABSOLUTE(kanji_TEXT_END - kanji_TEXT_START);

kanji_DATA_START = .;
build/segments/kanji.o(.data*);
build/us/segments/kanji.o(.data*);
. = ALIGN(., 0x8);
kanji_DATA_END = .;
kanji_DATA_SIZE = ABSOLUTE(kanji_DATA_END - kanji_DATA_START);

kanji_RODATA_START = .;
build/segments/kanji.o(.rodata*);
build/us/segments/kanji.o(.rodata*);
. = ALIGN(., 0x8);
. = ALIGN(., 0x80);
kanji_RODATA_END = .;
kanji_RODATA_SIZE = ABSOLUTE(kanji_RODATA_END - kanji_RODATA_START);

kanji_SDATA_START = .;
build/segments/kanji.o(.sdata*);
build/us/segments/kanji.o(.sdata*);
. = ALIGN(., 0x8);
kanji_SDATA_END = .;
kanji_SDATA_SIZE = ABSOLUTE(kanji_SDATA_END - kanji_SDATA_START);
Expand All @@ -126,26 +126,26 @@ SECTIONS
{
FILL(0x00000000);
kanji_SBSS_START = .;
build/segments/kanji.o(.sbss*);
build/us/segments/kanji.o(.sbss*);
. = ALIGN(., 0x8);
. = ALIGN(., 0x20);
kanji_SBSS_END = .;
kanji_SBSS_SIZE = ABSOLUTE(kanji_SBSS_END - kanji_SBSS_START);

kanji_SCOMMON_START = .;
build/segments/kanji.o(.scommon*);
build/us/segments/kanji.o(.scommon*);
. = ALIGN(., 0x8);
kanji_SCOMMON_END = .;
kanji_SCOMMON_SIZE = ABSOLUTE(kanji_SCOMMON_END - kanji_SCOMMON_START);

kanji_BSS_START = .;
build/segments/kanji.o(.bss*);
build/us/segments/kanji.o(.bss*);
. = ALIGN(., 0x8);
kanji_BSS_END = .;
kanji_BSS_SIZE = ABSOLUTE(kanji_BSS_END - kanji_BSS_START);

kanjiCOMMON_START = .;
build/segments/kanji.o(COMMON*);
build/us/segments/kanji.o(COMMON*);
. = ALIGN(., 0x8);
kanjiCOMMON_END = .;
kanjiCOMMON_SIZE = ABSOLUTE(kanjiCOMMON_END - kanjiCOMMON_START);
Expand Down Expand Up @@ -173,29 +173,29 @@ SECTIONS
FILL(0x00000000);
. = ALIGN(., 0x40);
main_TEXT_START = .;
build/segments/main.o(.text*);
build/us/segments/main.o(.text*);
. = ALIGN(., 0x20);
main_TEXT_END = .;
main_TEXT_SIZE = ABSOLUTE(main_TEXT_END - main_TEXT_START);

. = ALIGN(., 0x40);
main_DATA_START = .;
build/segments/main.o(.data*);
build/us/segments/main.o(.data*);
. = ALIGN(., 0x20);
main_DATA_END = .;
main_DATA_SIZE = ABSOLUTE(main_DATA_END - main_DATA_START);

. = ALIGN(., 0x40);
main_RODATA_START = .;
build/segments/main.o(.rodata*);
build/us/segments/main.o(.rodata*);
. = ALIGN(., 0x20);
. = ALIGN(., 0x80);
main_RODATA_END = .;
main_RODATA_SIZE = ABSOLUTE(main_RODATA_END - main_RODATA_START);

. = ALIGN(., 0x40);
main_SDATA_START = .;
build/segments/main.o(.sdata*);
build/us/segments/main.o(.sdata*);
. = ALIGN(., 0x20);
main_SDATA_END = .;
main_SDATA_SIZE = ABSOLUTE(main_SDATA_END - main_SDATA_START);
Expand All @@ -211,29 +211,29 @@ SECTIONS
FILL(0x00000000);
. = ALIGN(., 0x40);
main_SBSS_START = .;
build/segments/main.o(.sbss*);
build/us/segments/main.o(.sbss*);
. = ALIGN(., 0x20);
. = ALIGN(., 0x20);
main_SBSS_END = .;
main_SBSS_SIZE = ABSOLUTE(main_SBSS_END - main_SBSS_START);

. = ALIGN(., 0x40);
main_SCOMMON_START = .;
build/segments/main.o(.scommon*);
build/us/segments/main.o(.scommon*);
. = ALIGN(., 0x20);
main_SCOMMON_END = .;
main_SCOMMON_SIZE = ABSOLUTE(main_SCOMMON_END - main_SCOMMON_START);

. = ALIGN(., 0x40);
main_BSS_START = .;
build/segments/main.o(.bss*);
build/us/segments/main.o(.bss*);
. = ALIGN(., 0x20);
main_BSS_END = .;
main_BSS_SIZE = ABSOLUTE(main_BSS_END - main_BSS_START);

. = ALIGN(., 0x40);
mainCOMMON_START = .;
build/segments/main.o(COMMON*);
build/us/segments/main.o(COMMON*);
. = ALIGN(., 0x20);
mainCOMMON_END = .;
mainCOMMON_SIZE = ABSOLUTE(mainCOMMON_END - mainCOMMON_START);
Expand Down
6 changes: 3 additions & 3 deletions tests/partial_linking/follow_segment.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
settings:
base_path: build
base_path: build/{version}
segment_start_align: 0x8
segment_end_align: 0x40
section_end_align: 0x8
sections_end_alignment: { .rodata: 0x80, .sbss: 0x20 }

target_path: build/rom.elf
target_path: build/{version}/rom.elf
d_path: tests/partial_linking/follow_segment.d

symbols_header_path: tests/partial_linking/follow_segment.h

partial_scripts_folder: tests/partial_linking/follow_segment
partial_scripts_folder: tests/partial_linking/follow_segment/{version}
partial_build_segments_folder: segments

segments:
Expand Down
6 changes: 0 additions & 6 deletions tests/partial_linking/follow_segment/boot.d

This file was deleted.

4 changes: 0 additions & 4 deletions tests/partial_linking/follow_segment/kanji.d

This file was deleted.

8 changes: 0 additions & 8 deletions tests/partial_linking/follow_segment/main.d

This file was deleted.

Loading

0 comments on commit c20ea9c

Please sign in to comment.