Skip to content

Commit

Permalink
Implement sections_allowlist_extra
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Feb 27, 2024
1 parent 6aec53d commit 364663f
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 4 deletions.
34 changes: 33 additions & 1 deletion docs/file_format/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ A positive integer or `null`.

A list of sections to that should be preserved during linking.

Usually used for keeping debugging sections.
Usually used to avoid discarding debugging sections.

### Example

Expand All @@ -121,6 +121,38 @@ List of strings.

`[]`

## `sections_allowlist_extra`

A list of sections to that should be preserved during linking.

This setting works the same as same as
[`sections_allowlist`](#sections_allowlist). This option exists as a way to have
a default list of sections to be preserved that won't be overrided if the user
wants to specify their own allow list by setting `sections_allowlist`.

These defaults exists because some linkers (like clang's `lld`) complain if the
`.shstrtab` is not listed explicitly if a wildcard was used on the `/DISCARD/`
section (see [`discard_wildcard_section`](#discard_wildcard_section)), so to
avoid issues when wanting to use other linkers we emit the section by default.

### Example

```yaml
settings:
sections_allowlist_extra:
- .shstrtab
- .strtab
- .symtab
```

### Valid values

List of strings.

### Default value

`[.shstrtab]`

## `sections_denylist`

List of sections to be discarded.
Expand Down
19 changes: 19 additions & 0 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ impl<'a> LinkerWriter<'a> {
need_ln = true;
}

if !self.settings.sections_allowlist_extra.is_empty() {
if need_ln {
self.writeln("");
}

let address = " 0";

for sect in &self.settings.sections_allowlist_extra {
self.writeln(&format!("{}{} :", sect, address));
self.begin_block();

self.writeln(&format!("*({});", sect));

self.end_block();
}

need_ln = true;
}

if self.settings.discard_wildcard_section || !self.settings.sections_denylist.is_empty() {
if need_ln {
self.writeln("");
Expand Down
19 changes: 16 additions & 3 deletions slinky/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Settings {
pub hardcoded_gp_value: Option<u32>,

pub sections_allowlist: Vec<String>,
pub sections_allowlist_extra: Vec<String>,
pub sections_denylist: Vec<String>,
pub discard_wildcard_section: bool,

Expand Down Expand Up @@ -48,7 +49,11 @@ fn settings_default_sections_allowlist() -> Vec<String> {
vec![]
}

fn settings_default_section_denylist() -> Vec<String> {
fn settings_default_sections_allowlist_extra() -> Vec<String> {
vec![".shstrtab".into()]
}

fn settings_default_sections_denylist() -> Vec<String> {
vec![
".reginfo".into(),
".MIPS.abiflags".into(),
Expand Down Expand Up @@ -102,7 +107,8 @@ impl Default for Settings {
hardcoded_gp_value: settings_default_hardcoded_gp_value(),

sections_allowlist: settings_default_sections_allowlist(),
sections_denylist: settings_default_section_denylist(),
sections_allowlist_extra: settings_default_sections_allowlist_extra(),
sections_denylist: settings_default_sections_denylist(),
discard_wildcard_section: settings_default_discard_wildcard_section(),

alloc_sections: settings_default_alloc_sections(),
Expand Down Expand Up @@ -131,6 +137,8 @@ pub(crate) struct SettingsSerial {
#[serde(default)]
pub sections_allowlist: AbsentNullable<Vec<String>>,
#[serde(default)]
pub sections_allowlist_extra: AbsentNullable<Vec<String>>,
#[serde(default)]
pub sections_denylist: AbsentNullable<Vec<String>>,
#[serde(default)]
pub discard_wildcard_section: AbsentNullable<bool>,
Expand Down Expand Up @@ -168,9 +176,13 @@ impl SettingsSerial {
let sections_allowlist = self
.sections_allowlist
.get_non_null("sections_allowlist", settings_default_sections_allowlist)?;
let sections_allowlist_extra = self.sections_allowlist_extra.get_non_null(
"sections_allowlist_extra",
settings_default_sections_allowlist_extra,
)?;
let sections_denylist = self
.sections_denylist
.get_non_null("sections_denylist", settings_default_section_denylist)?;
.get_non_null("sections_denylist", settings_default_sections_denylist)?;
let discard_wildcard_section = self.discard_wildcard_section.get_non_null(
"discard_wildcard_section",
settings_default_discard_wildcard_section,
Expand Down Expand Up @@ -200,6 +212,7 @@ impl SettingsSerial {
linker_symbols_style,
hardcoded_gp_value,
sections_allowlist,
sections_allowlist_extra,
sections_denylist,
discard_wildcard_section,
alloc_sections,
Expand Down
13 changes: 13 additions & 0 deletions tests/linker_scripts/basic_example.ld
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,19 @@ SECTIONS
boot_ROM_END = __romPos;
boot_ROM_SIZE = ABSOLUTE(boot_ROM_END - boot_ROM_START);

.strtab 0 :
{
*(.strtab);
}
.symtab 0 :
{
*(.symtab);
}
.shstrtab 0 :
{
*(.shstrtab);
}

/DISCARD/ :
{
*(.reginfo);
Expand Down
13 changes: 13 additions & 0 deletions tests/linker_scripts/drmario64.ld
Original file line number Diff line number Diff line change
Expand Up @@ -7624,6 +7624,19 @@ SECTIONS
*(.comment);
}

.strtab 0 :
{
*(.strtab);
}
.symtab 0 :
{
*(.symtab);
}
.shstrtab 0 :
{
*(.shstrtab);
}

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

.strtab 0 :
{
*(.strtab);
}
.symtab 0 :
{
*(.symtab);
}
.shstrtab 0 :
{
*(.shstrtab);
}

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

.strtab 0 :
{
*(.strtab);
}
.symtab 0 :
{
*(.symtab);
}
.shstrtab 0 :
{
*(.shstrtab);
}

/DISCARD/ :
{
*(.reginfo);
Expand Down

0 comments on commit 364663f

Please sign in to comment.