Skip to content

Commit

Permalink
Implement discard_wildcard_section
Browse files Browse the repository at this point in the history
Fixes #19
  • Loading branch information
AngheloAlf committed Feb 26, 2024
1 parent 20bb2ed commit 6209860
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/file_format/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ A positive integer or `null`.

`null`

## `discard_wildcard_section`

Toggles emitting a discard section with a wildcard (`*`) entry, essentially
discarding every section and file that is not explicitly listed on the linker
script.

GNU LD docs for `/DISCARD/`: <https://sourceware.org/binutils/docs/ld/Output-Section-Discarding.html#index-_002fDISCARD_002f>

### Example

```yaml
settings:
discard_wildcard_section: False
```

### Valid values

Boolean.

### Default value

`True`

## `alloc_sections`

List of allocatable sections (the ones that take ROM space).
Expand Down
11 changes: 11 additions & 0 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ impl<'a> LinkerWriter<'a> {
}

pub fn end_sections(&mut self) {
if self.settings.discard_wildcard_section {
self.writeln("/DISCARD/ :");
self.begin_block();

if self.settings.discard_wildcard_section {
self.writeln("*(*);")
}

self.end_block();
}

self.end_block();
assert!(self.indent_level == 0);
}
Expand Down
17 changes: 17 additions & 0 deletions slinky/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub struct Settings {

pub hardcoded_gp_value: Option<u32>,

pub discard_wildcard_section: bool,

// Options passed down to each segment
pub alloc_sections: Vec<String>,
pub noload_sections: Vec<String>,
Expand All @@ -40,6 +42,10 @@ fn settings_hardcoded_gp_value_default() -> Option<u32> {
None
}

fn settings_discard_wildcard_section_default() -> bool {
true
}

fn settings_alloc_sections_default() -> Vec<String> {
vec![
".text".into(),
Expand Down Expand Up @@ -78,6 +84,8 @@ impl Default for Settings {

hardcoded_gp_value: settings_hardcoded_gp_value_default(),

discard_wildcard_section: settings_discard_wildcard_section_default(),

alloc_sections: settings_alloc_sections_default(),
noload_sections: settings_noload_sections_default(),

Expand All @@ -101,6 +109,9 @@ pub(crate) struct SettingsSerial {
#[serde(default)]
pub hardcoded_gp_value: AbsentNullable<u32>,

#[serde(default)]
pub discard_wildcard_section: AbsentNullable<bool>,

#[serde(default)]
pub alloc_sections: AbsentNullable<Vec<String>>,
#[serde(default)]
Expand Down Expand Up @@ -131,6 +142,11 @@ impl SettingsSerial {
.hardcoded_gp_value
.get_optional_nullable("hardcoded_gp_value", settings_hardcoded_gp_value_default)?;

let discard_wildcard_section = self.discard_wildcard_section.get_non_null(
"discard_wildcard_section",
settings_discard_wildcard_section_default,
)?;

let alloc_sections = self
.alloc_sections
.get_non_null("alloc_sections", settings_alloc_sections_default)?;
Expand All @@ -154,6 +170,7 @@ impl SettingsSerial {
base_path,
linker_symbols_style,
hardcoded_gp_value,
discard_wildcard_section,
alloc_sections,
noload_sections,
subalign,
Expand Down
4 changes: 4 additions & 0 deletions tests/linker_scripts/basic_example.ld
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,8 @@ SECTIONS
boot_ROM_END = __romPos;
boot_ROM_SIZE = ABSOLUTE(boot_ROM_END - boot_ROM_START);

/DISCARD/ :
{
*(*);
}
}
4 changes: 4 additions & 0 deletions tests/linker_scripts/drmario64.ld
Original file line number Diff line number Diff line change
Expand Up @@ -7475,4 +7475,8 @@ SECTIONS
segment_tutorial_data_ROM_END = __romPos;
segment_tutorial_data_ROM_SIZE = ABSOLUTE(segment_tutorial_data_ROM_END - segment_tutorial_data_ROM_START);

/DISCARD/ :
{
*(*);
}
}
4 changes: 4 additions & 0 deletions tests/linker_scripts/hardcoded_gp.ld
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ SECTIONS
main_ROM_END = __romPos;
main_ROM_SIZE = ABSOLUTE(main_ROM_END - main_ROM_START);

/DISCARD/ :
{
*(*);
}
}
4 changes: 4 additions & 0 deletions tests/linker_scripts/makerom_zelda_like.ld
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,8 @@ SECTIONS
_bootSegmentRomEnd = __romPos;
_bootSegmentRomSize = ABSOLUTE(_bootSegmentRomEnd - _bootSegmentRomStart);

/DISCARD/ :
{
*(*);
}
}

0 comments on commit 6209860

Please sign in to comment.