Skip to content

Commit

Permalink
Add support to specify the entrypoint
Browse files Browse the repository at this point in the history
Closes #54
  • Loading branch information
AngheloAlf committed Aug 14, 2024
1 parent 865e786 commit c83af88
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add way to define a non hardcoded `_gp` symbol for a given segment.
- Used by defining the `gp_info` field on a segment.
- Can't be combined with the global `hardcoded_gp_value`.
- Add new top-level attribute for the file format: `entry`.
- Specifies the entrypoint for the build.

### Changed

Expand Down
13 changes: 13 additions & 0 deletions docs/file_format/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ attribute. Other optional top-level attributes may be specified, like
[vram_classes](vram_classes.md) or [symbol_assignments](symbol_assignments.md).
Check their specific documents for in-deep explanations.

## Format itself

The document is composed by the following top-level attributes:

- [`settings`](settings.md).
- A list of [`vram_classes`](vram_classes.md).
- A list of [`segments`](segments.md).
- This list is required.
- `entry`
- A single optional string that specifies the entrypoint of the final build.
- A list of [`symbol_assignments`](symbol_assignments.md).
- A list of [`required_symbols`](required_symbols.md).

## Example

The following example corresponds to the
Expand Down
6 changes: 6 additions & 0 deletions slinky/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Document {

pub segments: Vec<Segment>,

pub entry: Option<String>,
pub symbol_assignments: Vec<SymbolAssignment>,
pub required_symbols: Vec<RequiredSymbol>,
}
Expand Down Expand Up @@ -59,6 +60,8 @@ pub(crate) struct DocumentSerial {

pub segments: Vec<SegmentSerial>,

#[serde(default)]
pub entry: AbsentNullable<String>,
#[serde(default)]
pub symbol_assignments: AbsentNullable<Vec<SymbolAssignmentSerial>>,
#[serde(default)]
Expand All @@ -85,6 +88,8 @@ impl DocumentSerial {

let segments = self.segments.unserialize(&settings)?;

let entry = self.entry.get_non_null_no_default("entry")?;

let symbol_assignments = self
.symbol_assignments
.get_non_null("symbol_assignments", Vec::new)?
Expand All @@ -99,6 +104,7 @@ impl DocumentSerial {
settings,
vram_classes,
segments,
entry,
symbol_assignments,
required_symbols,
})
Expand Down
10 changes: 10 additions & 0 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ impl ScriptImporter for LinkerWriter<'_> {
Ok(())
}

fn add_entry(&mut self, entry: &str) -> Result<(), SlinkyError> {
if !self.buffer.is_empty() {
self.buffer.write_empty_line();
}

self.buffer.writeln(&format!("ENTRY({});", entry));

Ok(())
}

fn add_all_symbol_assignments(
&mut self,
symbol_assignments: &[SymbolAssignment],
Expand Down
4 changes: 4 additions & 0 deletions slinky/src/partial_linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ impl ScriptImporter for PartialLinkerWriter<'_> {
Ok(())
}

fn add_entry(&mut self, entry: &str) -> Result<(), SlinkyError> {
self.main_writer.add_entry(entry)
}

fn add_all_symbol_assignments(
&mut self,
symbol_assignments: &[SymbolAssignment],
Expand Down
4 changes: 4 additions & 0 deletions slinky/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod private {

pub trait ScriptImporter: private::Sealed {
fn add_all_segments(&mut self, segments: &[Segment]) -> Result<(), SlinkyError>;
fn add_entry(&mut self, entry: &str) -> Result<(), SlinkyError>;
fn add_all_symbol_assignments(
&mut self,
symbol_assignments: &[SymbolAssignment],
Expand All @@ -41,6 +42,9 @@ pub trait ScriptImporter: private::Sealed {

fn add_whole_document(&mut self, document: &Document) -> Result<(), SlinkyError> {
self.add_all_segments(&document.segments)?;
if let Some(entry) = &document.entry {
self.add_entry(entry)?;
}
self.add_all_symbol_assignments(&document.symbol_assignments)?;
self.add_all_required_symbols(&document.required_symbols)?;

Expand Down
2 changes: 2 additions & 0 deletions tests/partial_linking/follow_segment.ld
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,5 @@ SECTIONS
*(*);
}
}

ENTRY(ENTRYPOINT);
2 changes: 2 additions & 0 deletions tests/partial_linking/follow_segment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ segments:
- { path: src/main/main.o }
- { path: src/main/dmadata.o }
- { path: asm/main/util.o }

entry: ENTRYPOINT
2 changes: 2 additions & 0 deletions tests/test_cases/drmario64.ld
Original file line number Diff line number Diff line change
Expand Up @@ -8093,3 +8093,5 @@ SECTIONS
*(*);
}
}

ENTRY(entrypoint);
2 changes: 2 additions & 0 deletions tests/test_cases/drmario64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -781,3 +781,5 @@ segments:
fixed_vram: 0x0
files:
- { path: src/assets/tutorial/tutorial_kasa.o }

entry: entrypoint

0 comments on commit c83af88

Please sign in to comment.