Skip to content

Commit

Permalink
Move symbol assignment outside of section on single_segment_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Apr 25, 2024
1 parent ce5cfa8 commit c82d097
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 73 deletions.
12 changes: 1 addition & 11 deletions slinky-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,7 @@ fn main() {
} else {
let mut writer = slinky::LinkerWriter::new(&document.settings);

if document.settings.single_segment_mode {
assert!(document.segments.len() == 1);

writer.add_single_segment(&document.segments[0]);
} else {
writer.begin_sections();
for segment in &document.segments {
writer.add_segment(segment);
}
writer.end_sections();
}
writer.add_all_segments(&document.segments);

if let Some(output_path) = cli.output {
writer
Expand Down
43 changes: 29 additions & 14 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ impl<'a> LinkerWriter<'a> {
}
}

pub fn add_all_segments(&mut self, segments: &[Segment]) {
if self.settings.single_segment_mode {
assert!(segments.len() == 1);

self.add_single_segment(&segments[0]);
} else {
self.begin_sections();
for segment in segments {
self.add_segment(segment);
}
self.end_sections();
}
}

pub fn begin_sections(&mut self) {
self.writeln("SECTIONS");
self.begin_block();
Expand Down Expand Up @@ -173,6 +187,7 @@ impl<'a> LinkerWriter<'a> {

if let Some(fixed_vram) = segment.fixed_vram {
self.writeln(&format!(". = 0x{:08X};", fixed_vram));
self.writeln("");
}

//let dotted_seg_name = format!(".{}", segment.name);
Expand All @@ -188,6 +203,12 @@ impl<'a> LinkerWriter<'a> {
for section in &self.settings.alloc_sections {
let mut line = String::new();

let section_start_sym = style.segment_section_start(&segment.name, section);
let section_end_sym = style.segment_section_end(&segment.name, section);
let section_size_sym = style.segment_section_size(&segment.name, section);

self.write_symbol(&section_start_sym, ".");

line += &format!("{} :", section);

if let Some(subalign) = segment.subalign {
Expand All @@ -201,27 +222,27 @@ impl<'a> LinkerWriter<'a> {
self.writeln(&format!("FILL(0x{:08X});", fill_value));
}

let section_start_sym = style.segment_section_start(&segment.name, section);
let section_end_sym = style.segment_section_end(&segment.name, section);
let section_size_sym = style.segment_section_size(&segment.name, section);

self.write_symbol(&section_start_sym, ".");

self.emit_section(segment, section);

if let Some(section_end_align) = segment.section_end_align {
self.align_symbol(".", section_end_align);
}
self.write_sym_end_size(&section_start_sym, &section_end_sym, &section_size_sym, ".");

self.end_block();
self.write_sym_end_size(&section_start_sym, &section_end_sym, &section_size_sym, ".");

self.writeln("");
}

for section in &self.settings.noload_sections {
let mut line = String::new();

let section_start_sym = style.segment_section_start(&segment.name, section);
let section_end_sym = style.segment_section_end(&segment.name, section);
let section_size_sym = style.segment_section_size(&segment.name, section);

self.write_symbol(&section_start_sym, ".");

line += &format!("{} (NOLOAD) :", section);

if let Some(subalign) = segment.subalign {
Expand All @@ -235,20 +256,14 @@ impl<'a> LinkerWriter<'a> {
self.writeln(&format!("FILL(0x{:08X});", fill_value));
}

let section_start_sym = style.segment_section_start(&segment.name, section);
let section_end_sym = style.segment_section_end(&segment.name, section);
let section_size_sym = style.segment_section_size(&segment.name, section);

self.write_symbol(&section_start_sym, ".");

self.emit_section(segment, section);

if let Some(section_end_align) = segment.section_end_align {
self.align_symbol(".", section_end_align);
}
self.write_sym_end_size(&section_start_sym, &section_end_sym, &section_size_sym, ".");

self.end_block();
self.write_sym_end_size(&section_start_sym, &section_end_sym, &section_size_sym, ".");

self.writeln("");
}
Expand Down
18 changes: 3 additions & 15 deletions slinky/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ fn check_ld_generation(yaml_path: &Path, ld_path: &Path) {
let document = slinky::Document::read_file(yaml_path).expect("unable to read original file");

let mut writer = slinky::LinkerWriter::new(&document.settings);
writer.begin_sections();
for segment in &document.segments {
writer.add_segment(segment);
}
writer.end_sections();
writer.add_all_segments(&document.segments);

let expected_ld_contents =
fs::read_to_string(ld_path).expect("unable to read expected ld file");
Expand All @@ -26,11 +22,7 @@ fn check_d_generation(yaml_path: &Path, ld_path: &Path) {
let document = slinky::Document::read_file(yaml_path).expect("unable to read original file");

let mut writer = slinky::LinkerWriter::new(&document.settings);
writer.begin_sections();
for segment in &document.segments {
writer.add_segment(segment);
}
writer.end_sections();
writer.add_all_segments(&document.segments);

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

Expand All @@ -45,11 +37,7 @@ fn check_symbols_header_generation(yaml_path: &Path, ld_path: &Path) {
let document = slinky::Document::read_file(yaml_path).expect("unable to read original file");

let mut writer = slinky::LinkerWriter::new(&document.settings);
writer.begin_sections();
for segment in &document.segments {
writer.add_segment(segment);
}
writer.end_sections();
writer.add_all_segments(&document.segments);

let expected_h_contents = fs::read_to_string(ld_path).expect("unable to read expected h file");

Expand Down
67 changes: 34 additions & 33 deletions tests/test_cases/hit_and_run.ld
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
SECTIONS
{
. = 0x00100000;

cod_TEXT_START = .;
.text :
{
FILL(0x00000000);
cod_TEXT_START = .;
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0880.scommon.o(.text*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0888.scommon.o(.text*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0890.scommon.o(.text*);
Expand Down Expand Up @@ -3043,14 +3044,14 @@ SECTIONS
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198E0.bss.o(.text*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198F0.bss.o(.text*);
. = ALIGN(., 0x4);
cod_TEXT_END = .;
cod_TEXT_SIZE = ABSOLUTE(cod_TEXT_END - cod_TEXT_START);
}
cod_TEXT_END = .;
cod_TEXT_SIZE = ABSOLUTE(cod_TEXT_END - cod_TEXT_START);

cod_VUTEXT_START = .;
.vutext :
{
FILL(0x00000000);
cod_VUTEXT_START = .;
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0880.scommon.o(.vutext*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0888.scommon.o(.vutext*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0890.scommon.o(.vutext*);
Expand Down Expand Up @@ -6089,14 +6090,14 @@ SECTIONS
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198E0.bss.o(.vutext*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198F0.bss.o(.vutext*);
. = ALIGN(., 0x4);
cod_VUTEXT_END = .;
cod_VUTEXT_SIZE = ABSOLUTE(cod_VUTEXT_END - cod_VUTEXT_START);
}
cod_VUTEXT_END = .;
cod_VUTEXT_SIZE = ABSOLUTE(cod_VUTEXT_END - cod_VUTEXT_START);

cod_DATA_START = .;
.data :
{
FILL(0x00000000);
cod_DATA_START = .;
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0880.scommon.o(.data*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0888.scommon.o(.data*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0890.scommon.o(.data*);
Expand Down Expand Up @@ -9135,14 +9136,14 @@ SECTIONS
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198E0.bss.o(.data*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198F0.bss.o(.data*);
. = ALIGN(., 0x4);
cod_DATA_END = .;
cod_DATA_SIZE = ABSOLUTE(cod_DATA_END - cod_DATA_START);
}
cod_DATA_END = .;
cod_DATA_SIZE = ABSOLUTE(cod_DATA_END - cod_DATA_START);

cod_VUDATA_START = .;
.vudata :
{
FILL(0x00000000);
cod_VUDATA_START = .;
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0880.scommon.o(.vudata*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0888.scommon.o(.vudata*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0890.scommon.o(.vudata*);
Expand Down Expand Up @@ -12181,14 +12182,14 @@ SECTIONS
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198E0.bss.o(.vudata*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198F0.bss.o(.vudata*);
. = ALIGN(., 0x4);
cod_VUDATA_END = .;
cod_VUDATA_SIZE = ABSOLUTE(cod_VUDATA_END - cod_VUDATA_START);
}
cod_VUDATA_END = .;
cod_VUDATA_SIZE = ABSOLUTE(cod_VUDATA_END - cod_VUDATA_START);

cod_RODATA_START = .;
.rodata :
{
FILL(0x00000000);
cod_RODATA_START = .;
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0880.scommon.o(.rodata*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0888.scommon.o(.rodata*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0890.scommon.o(.rodata*);
Expand Down Expand Up @@ -15227,14 +15228,14 @@ SECTIONS
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198E0.bss.o(.rodata*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198F0.bss.o(.rodata*);
. = ALIGN(., 0x4);
cod_RODATA_END = .;
cod_RODATA_SIZE = ABSOLUTE(cod_RODATA_END - cod_RODATA_START);
}
cod_RODATA_END = .;
cod_RODATA_SIZE = ABSOLUTE(cod_RODATA_END - cod_RODATA_START);

cod_GCC_EXCEPT_TABLE_START = .;
.gcc_except_table :
{
FILL(0x00000000);
cod_GCC_EXCEPT_TABLE_START = .;
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0880.scommon.o(.gcc_except_table*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0888.scommon.o(.gcc_except_table*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0890.scommon.o(.gcc_except_table*);
Expand Down Expand Up @@ -18273,14 +18274,14 @@ SECTIONS
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198E0.bss.o(.gcc_except_table*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198F0.bss.o(.gcc_except_table*);
. = ALIGN(., 0x4);
cod_GCC_EXCEPT_TABLE_END = .;
cod_GCC_EXCEPT_TABLE_SIZE = ABSOLUTE(cod_GCC_EXCEPT_TABLE_END - cod_GCC_EXCEPT_TABLE_START);
}
cod_GCC_EXCEPT_TABLE_END = .;
cod_GCC_EXCEPT_TABLE_SIZE = ABSOLUTE(cod_GCC_EXCEPT_TABLE_END - cod_GCC_EXCEPT_TABLE_START);

cod_SDATA_START = .;
.sdata :
{
FILL(0x00000000);
cod_SDATA_START = .;
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0880.scommon.o(.sdata*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0888.scommon.o(.sdata*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0890.scommon.o(.sdata*);
Expand Down Expand Up @@ -21319,14 +21320,14 @@ SECTIONS
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198E0.bss.o(.sdata*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198F0.bss.o(.sdata*);
. = ALIGN(., 0x4);
cod_SDATA_END = .;
cod_SDATA_SIZE = ABSOLUTE(cod_SDATA_END - cod_SDATA_START);
}
cod_SDATA_END = .;
cod_SDATA_SIZE = ABSOLUTE(cod_SDATA_END - cod_SDATA_START);

cod_SCOMMON_START = .;
.scommon (NOLOAD) :
{
FILL(0x00000000);
cod_SCOMMON_START = .;
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0880.scommon.o(.scommon*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0888.scommon.o(.scommon*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0890.scommon.o(.scommon*);
Expand Down Expand Up @@ -24365,14 +24366,14 @@ SECTIONS
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198E0.bss.o(.scommon*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198F0.bss.o(.scommon*);
. = ALIGN(., 0x4);
cod_SCOMMON_END = .;
cod_SCOMMON_SIZE = ABSOLUTE(cod_SCOMMON_END - cod_SCOMMON_START);
}
cod_SCOMMON_END = .;
cod_SCOMMON_SIZE = ABSOLUTE(cod_SCOMMON_END - cod_SCOMMON_START);

cod_SBSS_START = .;
.sbss (NOLOAD) :
{
FILL(0x00000000);
cod_SBSS_START = .;
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0880.scommon.o(.sbss*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0888.scommon.o(.sbss*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0890.scommon.o(.sbss*);
Expand Down Expand Up @@ -27411,14 +27412,14 @@ SECTIONS
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198E0.bss.o(.sbss*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198F0.bss.o(.sbss*);
. = ALIGN(., 0x4);
cod_SBSS_END = .;
cod_SBSS_SIZE = ABSOLUTE(cod_SBSS_END - cod_SBSS_START);
}
cod_SBSS_END = .;
cod_SBSS_SIZE = ABSOLUTE(cod_SBSS_END - cod_SBSS_START);

cod_BSS_START = .;
.bss (NOLOAD) :
{
FILL(0x00000000);
cod_BSS_START = .;
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0880.scommon.o(.bss*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0888.scommon.o(.bss*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0890.scommon.o(.bss*);
Expand Down Expand Up @@ -30457,14 +30458,14 @@ SECTIONS
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198E0.bss.o(.bss*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198F0.bss.o(.bss*);
. = ALIGN(., 0x4);
cod_BSS_END = .;
cod_BSS_SIZE = ABSOLUTE(cod_BSS_END - cod_BSS_START);
}
cod_BSS_END = .;
cod_BSS_SIZE = ABSOLUTE(cod_BSS_END - cod_BSS_START);

cod_VUBSS_START = .;
.vubss (NOLOAD) :
{
FILL(0x00000000);
cod_VUBSS_START = .;
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0880.scommon.o(.vubss*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0888.scommon.o(.vubss*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/004B0890.scommon.o(.vubss*);
Expand Down Expand Up @@ -33503,9 +33504,9 @@ SECTIONS
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198E0.bss.o(.vubss*);
build/us_2003_07_10/asm/us_2003_07_10/data/unk/005198F0.bss.o(.vubss*);
. = ALIGN(., 0x4);
cod_VUBSS_END = .;
cod_VUBSS_SIZE = ABSOLUTE(cod_VUBSS_END - cod_VUBSS_START);
}
cod_VUBSS_END = .;
cod_VUBSS_SIZE = ABSOLUTE(cod_VUBSS_END - cod_VUBSS_START);

.pdr 0 :
{
Expand Down

0 comments on commit c82d097

Please sign in to comment.