Skip to content

Commit

Permalink
Implement sections_allowlist
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
AngheloAlf committed Feb 27, 2024
1 parent f749bcc commit 6aec53d
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/file_format/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ A positive integer or `null`.

`null`

## `sections_allowlist`

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

Usually used for keeping debugging sections.

### Example

```yaml
settings:
sections_allowlist: [.mdebug, .note, .comment]
```

### Valid values

List of strings.

### Default value

`[]`

## `sections_denylist`

List of sections to be discarded.
Expand Down
21 changes: 21 additions & 0 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,28 @@ impl<'a> LinkerWriter<'a> {
}

pub fn end_sections(&mut self) {
let mut need_ln = false;

if !self.settings.sections_allowlist.is_empty() {
let address = " 0";

for sect in &self.settings.sections_allowlist {
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("");
}

self.writeln("/DISCARD/ :");
self.begin_block();

Expand Down
12 changes: 12 additions & 0 deletions slinky/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Settings {

pub hardcoded_gp_value: Option<u32>,

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

Expand Down Expand Up @@ -43,6 +44,10 @@ fn settings_default_hardcoded_gp_value() -> Option<u32> {
None
}

fn settings_default_sections_allowlist() -> Vec<String> {
vec![]
}

fn settings_default_section_denylist() -> Vec<String> {
vec![
".reginfo".into(),
Expand Down Expand Up @@ -96,6 +101,7 @@ impl Default for Settings {

hardcoded_gp_value: settings_default_hardcoded_gp_value(),

sections_allowlist: settings_default_sections_allowlist(),
sections_denylist: settings_default_section_denylist(),
discard_wildcard_section: settings_default_discard_wildcard_section(),

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

#[serde(default)]
pub sections_allowlist: AbsentNullable<Vec<String>>,
#[serde(default)]
pub sections_denylist: AbsentNullable<Vec<String>>,
#[serde(default)]
Expand Down Expand Up @@ -157,6 +165,9 @@ impl SettingsSerial {
.hardcoded_gp_value
.get_optional_nullable("hardcoded_gp_value", settings_default_hardcoded_gp_value)?;

let sections_allowlist = self
.sections_allowlist
.get_non_null("sections_allowlist", settings_default_sections_allowlist)?;
let sections_denylist = self
.sections_denylist
.get_non_null("sections_denylist", settings_default_section_denylist)?;
Expand Down Expand Up @@ -188,6 +199,7 @@ impl SettingsSerial {
base_path,
linker_symbols_style,
hardcoded_gp_value,
sections_allowlist,
sections_denylist,
discard_wildcard_section,
alloc_sections,
Expand Down
62 changes: 62 additions & 0 deletions tests/input_files/drmario64.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@
settings:
base_path: build/us

sections_allowlist:
# Basic sections.
# lld complains if .shstrtab is not explictly mentioned on the linker script when using a wildcard discard, so here we are
- .strtab
- .symtab
- .shstrtab
# mips debug sections
- .pdr
- .mdebug
- .mdebug.abi32
# DWARF debug sections
# Symbols in the DWARF debugging sections are relative to the beginning of the section so we begin them at 0.
# DWARF 1
- .debug
- .line
# GNU DWARF 1 extensions
- .debug_srcinfo
- .debug_sfnames
# DWARF 1.1 and DWARF 2
- .debug_aranges
- .debug_pubnames
# DWARF 2
- .debug_info
# - .gnu.linkonce.wi.*
- .debug_abbrev
- .debug_line
# - .debug_line.*
- .debug_line_end
- .debug_frame
- .debug_str
- .debug_loc
- .debug_macinfo
# SGI/MIPS DWARF 2 extensions
- .debug_weaknames
- .debug_funcnames
- .debug_typenames
- .debug_varnames
# DWARF 3
- .debug_pubtypes
- .debug_ranges
# DWARF 5
- .debug_addr
- .debug_line_str
- .debug_loclists
- .debug_macro
- .debug_names
- .debug_rnglists
- .debug_str_offsets
- .debug_sup
# gnu attributes
- .gnu.attributes
# misc
- .note
- .comment
sections_denylist:
- .reginfo
- .MIPS.abiflags
- .MIPS.options
- .note.gnu.build-id
- .interp
- .eh_frame

segments:
- name: header
fixed_vram: 0x8F000000
Expand Down
149 changes: 149 additions & 0 deletions tests/linker_scripts/drmario64.ld
Original file line number Diff line number Diff line change
Expand Up @@ -7475,6 +7475,155 @@ SECTIONS
segment_tutorial_data_ROM_END = __romPos;
segment_tutorial_data_ROM_SIZE = ABSOLUTE(segment_tutorial_data_ROM_END - segment_tutorial_data_ROM_START);

.strtab 0 :
{
*(.strtab);
}
.symtab 0 :
{
*(.symtab);
}
.shstrtab 0 :
{
*(.shstrtab);
}
.pdr 0 :
{
*(.pdr);
}
.mdebug 0 :
{
*(.mdebug);
}
.mdebug.abi32 0 :
{
*(.mdebug.abi32);
}
.debug 0 :
{
*(.debug);
}
.line 0 :
{
*(.line);
}
.debug_srcinfo 0 :
{
*(.debug_srcinfo);
}
.debug_sfnames 0 :
{
*(.debug_sfnames);
}
.debug_aranges 0 :
{
*(.debug_aranges);
}
.debug_pubnames 0 :
{
*(.debug_pubnames);
}
.debug_info 0 :
{
*(.debug_info);
}
.debug_abbrev 0 :
{
*(.debug_abbrev);
}
.debug_line 0 :
{
*(.debug_line);
}
.debug_line_end 0 :
{
*(.debug_line_end);
}
.debug_frame 0 :
{
*(.debug_frame);
}
.debug_str 0 :
{
*(.debug_str);
}
.debug_loc 0 :
{
*(.debug_loc);
}
.debug_macinfo 0 :
{
*(.debug_macinfo);
}
.debug_weaknames 0 :
{
*(.debug_weaknames);
}
.debug_funcnames 0 :
{
*(.debug_funcnames);
}
.debug_typenames 0 :
{
*(.debug_typenames);
}
.debug_varnames 0 :
{
*(.debug_varnames);
}
.debug_pubtypes 0 :
{
*(.debug_pubtypes);
}
.debug_ranges 0 :
{
*(.debug_ranges);
}
.debug_addr 0 :
{
*(.debug_addr);
}
.debug_line_str 0 :
{
*(.debug_line_str);
}
.debug_loclists 0 :
{
*(.debug_loclists);
}
.debug_macro 0 :
{
*(.debug_macro);
}
.debug_names 0 :
{
*(.debug_names);
}
.debug_rnglists 0 :
{
*(.debug_rnglists);
}
.debug_str_offsets 0 :
{
*(.debug_str_offsets);
}
.debug_sup 0 :
{
*(.debug_sup);
}
.gnu.attributes 0 :
{
*(.gnu.attributes);
}
.note 0 :
{
*(.note);
}
.comment 0 :
{
*(.comment);
}

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

0 comments on commit 6aec53d

Please sign in to comment.