Skip to content

Commit

Permalink
Implement section_end_align
Browse files Browse the repository at this point in the history
Fixes #25
  • Loading branch information
AngheloAlf committed Feb 28, 2024
1 parent cda0176 commit de04bde
Show file tree
Hide file tree
Showing 14 changed files with 904 additions and 5 deletions.
26 changes: 26 additions & 0 deletions docs/file_format/segments.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,32 @@ Positive integers or `null`.

The value specified for [settings.md#subalign](settings.md#subalign)

## `section_end_align`

Force aligning the end of each section for this segment to the specified value.

If the value is `null` then no alignment will be forced on the end of the
sections of this segment.

This option overrides the global setting, see
[settings.md#section_end_align](settings.md#section_end_align) for more info.

### Example

```yaml
segments:
- name: main
section_end_align: null
```

### Valid values

Positive integers or `null`.

### Default value

The value specified for [settings.md#section_end_align](settings.md#section_end_align)

## `wildcard_sections`

Toggles using wildcards (`*`) as suffix in the emitted sections for this
Expand Down
27 changes: 26 additions & 1 deletion docs/file_format/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,32 @@ Positive integers or `null`.

### Default value

`16`
`0x10`

## `section_end_align`

Force aligning the end of each section to the specified value.

If the value is `null` then no alignment will be forced on the end of the
sections.

This option can be overriden per segment, see
[segments.md#section_end_align](segments.md#section_end_align) for more info.

### Example

```yaml
settings:
section_end_align: 0x10
```

### Valid values

Positive integers or `null`.

### Default value

`0x10`

## `wildcard_sections`

Expand Down
7 changes: 5 additions & 2 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl LinkerWriter<'_> {
self.write_sym_end_size(seg_sym_start, seg_sym_end, seg_sym_size, ".");
}

fn write_files_for_section(&mut self, segment: &Segment, section: &str) {
fn emit_section(&mut self, segment: &Segment, section: &str) {
let style = &self.settings.linker_symbols_style;

for file in &segment.files {
Expand Down Expand Up @@ -352,8 +352,11 @@ impl LinkerWriter<'_> {

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

self.write_files_for_section(segment, section);
self.emit_section(segment, section);

if let Some(section_end_align) = segment.section_end_align {
self.writeln(&format!(". = ALIGN(0x{:X})", section_end_align));
}
self.write_sym_end_size(&section_start_sym, &section_end_sym, &section_size_sym, ".");

if i + 1 < sections.len() {
Expand Down
8 changes: 8 additions & 0 deletions slinky/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Segment {
pub noload_sections: Vec<String>,

pub subalign: Option<u32>,
pub section_end_align: Option<u32>,

pub wildcard_sections: bool,

Expand All @@ -54,6 +55,8 @@ pub(crate) struct SegmentSerial {

#[serde(default)]
pub subalign: AbsentNullable<u32>,
#[serde(default)]
pub section_end_align: AbsentNullable<u32>,

#[serde(default)]
pub wildcard_sections: AbsentNullable<bool>,
Expand Down Expand Up @@ -108,6 +111,10 @@ impl SegmentSerial {
.subalign
.get_optional_nullable("subalign", || settings.subalign)?;

let section_end_align = self
.section_end_align
.get_optional_nullable("section_end_align", || settings.section_end_align)?;

let wildcard_sections = self
.wildcard_sections
.get_non_null("wildcard_sections", || settings.wildcard_sections)?;
Expand All @@ -124,6 +131,7 @@ impl SegmentSerial {
alloc_sections,
noload_sections,
subalign,
section_end_align,
wildcard_sections,
fill_value,
})
Expand Down
17 changes: 15 additions & 2 deletions slinky/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Settings {
pub noload_sections: Vec<String>,

pub subalign: Option<u32>,
pub section_end_align: Option<u32>,

pub wildcard_sections: bool,

Expand Down Expand Up @@ -87,7 +88,11 @@ fn settings_default_noload_sections() -> Vec<String> {
}

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

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

fn settings_default_wildcard_sections() -> bool {
Expand Down Expand Up @@ -115,6 +120,7 @@ impl Default for Settings {
noload_sections: settings_default_noload_sections(),

subalign: settings_default_subalign(),
section_end_align: settings_default_section_end_align(),

wildcard_sections: settings_default_wildcard_sections(),

Expand Down Expand Up @@ -143,14 +149,16 @@ pub(crate) struct SettingsSerial {
#[serde(default)]
pub discard_wildcard_section: AbsentNullable<bool>,

// Options passed down to each Segment
#[serde(default)]
pub alloc_sections: AbsentNullable<Vec<String>>,
#[serde(default)]
pub noload_sections: AbsentNullable<Vec<String>>,

// Options passed down to each Segment
#[serde(default)]
pub subalign: AbsentNullable<u32>,
#[serde(default)]
pub section_end_align: AbsentNullable<u32>,

#[serde(default)]
pub wildcard_sections: AbsentNullable<bool>,
Expand Down Expand Up @@ -199,6 +207,10 @@ impl SettingsSerial {
.subalign
.get_optional_nullable("subalign", settings_default_subalign)?;

let section_end_align = self
.section_end_align
.get_optional_nullable("section_end_align", settings_default_section_end_align)?;

let wildcard_sections = self
.wildcard_sections
.get_non_null("wildcard_sections", settings_default_wildcard_sections)?;
Expand All @@ -218,6 +230,7 @@ impl SettingsSerial {
alloc_sections,
noload_sections,
subalign,
section_end_align,
wildcard_sections,
fill_value,
})
Expand Down
2 changes: 2 additions & 0 deletions tests/input_files/follow_segment.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
settings:
base_path: build
section_end_align: null

segments:
- name: boot
Expand All @@ -13,6 +14,7 @@ segments:

- name: main
follows_segment: boot
section_end_align: 0x20
files:
- { path: src/main/main.o }
- { path: src/main/dmadata.o }
Expand Down
1 change: 1 addition & 0 deletions tests/input_files/pad_test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
settings:
base_path: build
section_end_align: null

segments:
- name: boot
Expand Down
8 changes: 8 additions & 0 deletions tests/linker_scripts/archives.ld
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SECTIONS
build/lib/libmus.a:aud_thread.o(.text*);
build/lib/libmus.a:lib_memory.o(.text*);
build/lib/libmus.a:aud_samples.o(.text*);
. = ALIGN(0x10)
boot_TEXT_END = .;
boot_TEXT_SIZE = ABSOLUTE(boot_TEXT_END - boot_TEXT_START);

Expand All @@ -35,6 +36,7 @@ SECTIONS
build/lib/libmus.a:aud_thread.o(.data*);
build/lib/libmus.a:lib_memory.o(.data*);
build/lib/libmus.a:aud_samples.o(.data*);
. = ALIGN(0x10)
boot_DATA_END = .;
boot_DATA_SIZE = ABSOLUTE(boot_DATA_END - boot_DATA_START);

Expand All @@ -50,6 +52,7 @@ SECTIONS
build/lib/libmus.a:aud_thread.o(.rodata*);
build/lib/libmus.a:lib_memory.o(.rodata*);
build/lib/libmus.a:aud_samples.o(.rodata*);
. = ALIGN(0x10)
boot_RODATA_END = .;
boot_RODATA_SIZE = ABSOLUTE(boot_RODATA_END - boot_RODATA_START);

Expand All @@ -65,6 +68,7 @@ SECTIONS
build/lib/libmus.a:aud_thread.o(.sdata*);
build/lib/libmus.a:lib_memory.o(.sdata*);
build/lib/libmus.a:aud_samples.o(.sdata*);
. = ALIGN(0x10)
boot_SDATA_END = .;
boot_SDATA_SIZE = ABSOLUTE(boot_SDATA_END - boot_SDATA_START);
}
Expand All @@ -87,6 +91,7 @@ SECTIONS
build/lib/libmus.a:aud_thread.o(.sbss*);
build/lib/libmus.a:lib_memory.o(.sbss*);
build/lib/libmus.a:aud_samples.o(.sbss*);
. = ALIGN(0x10)
boot_SBSS_END = .;
boot_SBSS_SIZE = ABSOLUTE(boot_SBSS_END - boot_SBSS_START);

Expand All @@ -102,6 +107,7 @@ SECTIONS
build/lib/libmus.a:aud_thread.o(.scommon*);
build/lib/libmus.a:lib_memory.o(.scommon*);
build/lib/libmus.a:aud_samples.o(.scommon*);
. = ALIGN(0x10)
boot_SCOMMON_END = .;
boot_SCOMMON_SIZE = ABSOLUTE(boot_SCOMMON_END - boot_SCOMMON_START);

Expand All @@ -117,6 +123,7 @@ SECTIONS
build/lib/libmus.a:aud_thread.o(.bss*);
build/lib/libmus.a:lib_memory.o(.bss*);
build/lib/libmus.a:aud_samples.o(.bss*);
. = ALIGN(0x10)
boot_BSS_END = .;
boot_BSS_SIZE = ABSOLUTE(boot_BSS_END - boot_BSS_START);

Expand All @@ -132,6 +139,7 @@ SECTIONS
build/lib/libmus.a:aud_thread.o(COMMON*);
build/lib/libmus.a:lib_memory.o(COMMON*);
build/lib/libmus.a:aud_samples.o(COMMON*);
. = ALIGN(0x10)
bootCOMMON_END = .;
bootCOMMON_SIZE = ABSOLUTE(bootCOMMON_END - bootCOMMON_START);
}
Expand Down
Loading

0 comments on commit de04bde

Please sign in to comment.