Skip to content

Commit

Permalink
Remove ld_section_labels (#362)
Browse files Browse the repository at this point in the history
* Yeet ld_section_labels

* Give a proper error message for missing sections on `section_order` during linker script generation

* black

* a
  • Loading branch information
AngheloAlf authored Mar 19, 2024
1 parent f628112 commit 8425949
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* `ld_bss_is_noload` is now `False` by default for `psx` projects.
* Allow to properly override `get_linker_section` and `get_section_flags` in `asm` and `hasm` files.
* Fix disassembling segments that only have bss.
* `ld_section_labels` was removed since it was a redundant list to `section_order`.
* Give a proper error message for missing sections on `section_order` during linker script generation.

### 0.22.3

Expand Down
4 changes: 0 additions & 4 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,6 @@ ld_symbol_header_path: path/to/linker_symbol_header

Determines whether to add a discard section to the linker script

### ld_section_labels

Determines the list of section labels that are to be added to the linker script

### ld_wildcard_sections

Determines whether to add wildcards for section linking in the linker script (.rodata* for example)
Expand Down
34 changes: 25 additions & 9 deletions src/splat/segtypes/linker_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path
from typing import Dict, List, OrderedDict, Set, Tuple, Union, Optional

from ..util import options
from ..util import options, log

from .segment import Segment
from ..util.symbols import to_cname
Expand Down Expand Up @@ -236,25 +236,33 @@ def add(self, segment: Segment, max_vram_syms: List[Tuple[str, List[Segment]]]):

section_entries: OrderedDict[str, List[LinkerEntry]] = OrderedDict()
for l in segment.section_order:
if l in options.opts.ld_section_labels:
if l in options.opts.section_order:
section_entries[l] = []

# Add all entries to section_entries
prev_entry = None
for entry in entries:
if entry.section_order_type in section_entries:
# Search for the very first section type
# This is required in case the very first entry is a type that's not listed on ld_section_labels (like linker_offset) because it would be dropped
# This is required in case the very first entry is a type that's not listed on section_order (like linker_offset) because it would be dropped
prev_entry = entry.section_order_type
break

any_load = False
any_noload = False
for entry in entries:
if entry.section_order_type in section_entries:
if entry.section_order_type not in section_entries:
log.error(
f"\nError on linker script generation: section '{entry.section_order_type}' not found.\n Make sure the section '{entry.section_order_type}' is listed on 'section_order' of the yaml options."
)
section_entries[entry.section_order_type].append(entry)
elif prev_entry is not None:
# If this section is not present in section_order or ld_section_labels then pretend it is part of the last seen section, mainly for handling linker_offset
# If this section is not present in section_order or section_order then pretend it is part of the last seen section, mainly for handling linker_offset
if prev_entry not in section_entries:
log.error(
f"\nError on linker script generation: section '{prev_entry}' not found.\n Make sure the section '{prev_entry}' is listed on 'section_order' of the yaml options."
)
section_entries[prev_entry].append(entry)
any_load = any_load or not entry.noload
any_noload = any_noload or entry.noload
Expand Down Expand Up @@ -285,14 +293,14 @@ def add_legacy(self, segment: Segment, entries: List[LinkerEntry]):

# To keep track which sections has been started
started_sections: Dict[str, bool] = {
l: False for l in options.opts.ld_section_labels
l: False for l in options.opts.section_order
}

# Find where sections are last seen
last_seen_sections: Dict[LinkerEntry, str] = {}
for entry in reversed(entries):
if (
entry.section_order_type in options.opts.ld_section_labels
entry.section_order_type in options.opts.section_order
and entry.section_order_type not in last_seen_sections.values()
):
last_seen_sections[entry] = entry.section_order_type
Expand Down Expand Up @@ -362,7 +370,7 @@ def add_referenced_partial_segment(
self._begin_segment(segment, seg_name, noload=False, is_first=is_first)

for l in segment.section_order:
if l not in options.opts.ld_section_labels:
if l not in options.opts.section_order:
continue
if l == ".bss":
continue
Expand Down Expand Up @@ -412,16 +420,24 @@ def add_partial_segment(self, segment: Segment):

section_entries: OrderedDict[str, List[LinkerEntry]] = OrderedDict()
for l in segment.section_order:
if l in options.opts.ld_section_labels:
if l in options.opts.section_order:
section_entries[l] = []

# Add all entries to section_entries
prev_entry = None
for entry in entries:
if entry.section_order_type in section_entries:
if entry.section_order_type not in section_entries:
log.error(
f"\nError on linker script generation: section '{entry.section_order_type}' not found.\n Make sure the section '{entry.section_order_type}' is listed on 'section_order' of the yaml options."
)
section_entries[entry.section_order_type].append(entry)
elif prev_entry is not None:
# If this section is not present in section_order or ld_section_labels then pretend it is part of the last seen section, mainly for handling linker_offset
# If this section is not present in section_order or section_order then pretend it is part of the last seen section, mainly for handling linker_offset
if prev_entry not in section_entries:
log.error(
f"\nError on linker script generation: section '{prev_entry}' not found.\n Make sure the section '{prev_entry}' is listed on 'section_order' of the yaml options."
)
section_entries[prev_entry].append(entry)
prev_entry = entry.section_order_type

Expand Down
7 changes: 0 additions & 7 deletions src/splat/util/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ class SplatOpts:
ld_sections_allowlist: List[str]
# A list of sections to discard during link time. It can be useful to avoid using the wildcard discard. Note that this option does not turn off `ld_discard_section`
ld_sections_denylist: List[str]
# Determines the list of section labels that are to be added to the linker script
ld_section_labels: List[str]
# Determines whether to add wildcards for section linking in the linker script (.rodata* for example)
ld_wildcard_sections: bool
# Determines whether to use `follows_vram` (segment option) and
Expand Down Expand Up @@ -428,11 +426,6 @@ def parse_endianness() -> Literal["big", "little"]:
ld_discard_section=p.parse_opt("ld_discard_section", bool, True),
ld_sections_allowlist=p.parse_opt("ld_sections_allowlist", list, []),
ld_sections_denylist=p.parse_opt("ld_sections_denylist", list, []),
ld_section_labels=p.parse_opt(
"ld_section_labels",
list,
[".text", ".data", ".rodata", ".bss"],
),
ld_wildcard_sections=p.parse_opt("ld_wildcard_sections", bool, False),
ld_use_symbolic_vram_addresses=p.parse_opt(
"ld_use_symbolic_vram_addresses", bool, True
Expand Down

0 comments on commit 8425949

Please sign in to comment.