Skip to content

Commit

Permalink
Add split order attribute for manual reordering
Browse files Browse the repository at this point in the history
Example in splits.txt:
```
file1.cpp: order:0
  ...

file2.cpp: order:1
  ...

file3.cpp: order:2
  ...
```

This ensures that file2.cpp is always
anchored in between 1 and 3 when resolving
the final link order.
  • Loading branch information
encounter committed Aug 12, 2024
1 parent da6a514 commit b6a29fa
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "decomp-toolkit"
description = "Yet another GameCube/Wii decompilation toolkit."
authors = ["Luke Street <[email protected]>"]
license = "MIT OR Apache-2.0"
version = "0.9.3"
version = "0.9.4"
edition = "2021"
publish = false
repository = "https://github.com/encounter/decomp-toolkit"
Expand Down
2 changes: 2 additions & 0 deletions src/obj/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub struct ObjUnit {
pub autogenerated: bool,
/// MW `.comment` section version.
pub comment_version: Option<u8>,
/// Influences the order of this unit relative to other ordered units.
pub order: Option<i32>,
}

#[derive(Debug, Clone)]
Expand Down
21 changes: 19 additions & 2 deletions src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ pub fn parse_u32(s: &str) -> Result<u32, ParseIntError> {
}
}

pub fn parse_i32(s: &str) -> Result<i32, ParseIntError> {
if let Some(s) = s.strip_prefix("-0x").or_else(|| s.strip_prefix("-0X")) {
i32::from_str_radix(s, 16).map(|v| -v)
} else if let Some(s) = s.strip_prefix("0x").or_else(|| s.strip_prefix("0X")) {
i32::from_str_radix(s, 16)
} else {
s.parse::<i32>()
}
}

pub fn apply_symbols_file<P>(path: P, obj: &mut ObjInfo) -> Result<Option<FileReadInfo>>
where P: AsRef<Path> {
Ok(if path.as_ref().is_file() {
Expand Down Expand Up @@ -428,6 +438,9 @@ where W: Write + ?Sized {
if let Some(comment_version) = unit.comment_version {
write!(w, " comment:{}", comment_version)?;
}
if let Some(order) = unit.order {
write!(w, " order:{}", order)?;
}
writeln!(w)?;
let mut split_iter = obj.sections.all_splits().peekable();
while let Some((_section_index, section, addr, split)) = split_iter.next() {
Expand Down Expand Up @@ -475,6 +488,8 @@ struct SplitUnit {
name: String,
/// MW `.comment` section version
comment_version: Option<u8>,
/// Influences the order of this unit relative to other ordered units.
order: Option<i32>,
}

pub struct SectionDef {
Expand Down Expand Up @@ -515,12 +530,13 @@ fn parse_unit_line(captures: Captures) -> Result<SplitLine> {
if name == "Sections" {
return Ok(SplitLine::SectionsStart);
}
let mut unit = SplitUnit { name: name.to_string(), comment_version: None };
let mut unit = SplitUnit { name: name.to_string(), comment_version: None, order: None };

for attr in captures["attrs"].split(' ').filter(|&s| !s.is_empty()) {
if let Some((attr, value)) = attr.split_once(':') {
match attr {
"comment" => unit.comment_version = Some(u8::from_str(value)?),
"order" => unit.order = Some(parse_i32(value)?),
_ => bail!("Unknown unit attribute '{}'", attr),
}
} else {
Expand Down Expand Up @@ -631,12 +647,13 @@ where R: BufRead + ?Sized {
match (&mut state, split_line) {
(
SplitState::None | SplitState::Unit(_) | SplitState::Sections(_),
SplitLine::Unit(SplitUnit { name, comment_version }),
SplitLine::Unit(SplitUnit { name, comment_version, order }),
) => {
obj.link_order.push(ObjUnit {
name: name.clone(),
autogenerated: false,
comment_version,
order,
});
state = SplitState::Unit(name);
}
Expand Down
1 change: 1 addition & 0 deletions src/util/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ where P: AsRef<Path> {
name: file_name.clone(),
autogenerated: false,
comment_version: None,
order: None,
});
}

Expand Down
1 change: 1 addition & 0 deletions src/util/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ pub fn apply_map(result: &MapInfo, obj: &mut ObjInfo) -> Result<()> {
name: unit.clone(),
autogenerated: false,
comment_version: Some(0),
order: None,
});
}

Expand Down
34 changes: 30 additions & 4 deletions src/util/split.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
cmp::{max, min, Ordering},
collections::{BTreeMap, HashMap, HashSet},
collections::{btree_map, BTreeMap, HashMap, HashSet},
};

use anyhow::{anyhow, bail, ensure, Context, Result};
Expand Down Expand Up @@ -816,8 +816,8 @@ fn resolve_link_order(obj: &ObjInfo) -> Result<Vec<ObjUnit>> {
#[allow(dead_code)]
#[derive(Debug, Copy, Clone)]
struct SplitEdge {
from: u32,
to: u32,
from: i64,
to: i64,
}

let mut graph = Graph::<String, SplitEdge>::new();
Expand Down Expand Up @@ -852,11 +852,36 @@ fn resolve_link_order(obj: &ObjInfo) -> Result<Vec<ObjUnit>> {
);
let a_index = *unit_to_index_map.get(&a.unit).unwrap();
let b_index = *unit_to_index_map.get(&b.unit).unwrap();
graph.add_edge(a_index, b_index, SplitEdge { from: a_addr, to: b_addr });
graph.add_edge(a_index, b_index, SplitEdge {
from: a_addr as i64,
to: b_addr as i64,
});
}
}
}

// Apply link order constraints provided by the user
let mut ordered_units = BTreeMap::<i32, String>::new();
for unit in &obj.link_order {
if let Some(order) = unit.order {
match ordered_units.entry(order) {
btree_map::Entry::Vacant(entry) => {
entry.insert(unit.name.clone());
}
btree_map::Entry::Occupied(entry) => {
bail!("Duplicate order {} for units {} and {}", order, entry.get(), unit.name);
}
}
}
}
let mut iter = ordered_units
.into_iter()
.filter_map(|(order, unit)| unit_to_index_map.get(&unit).map(|&index| (order, index)))
.peekable();
while let (Some((a_order, a_index)), Some((b_order, b_index))) = (iter.next(), iter.peek()) {
graph.add_edge(a_index, *b_index, SplitEdge { from: a_order as i64, to: *b_order as i64 });
}

// use petgraph::{
// dot::{Config, Dot},
// graph::EdgeReference,
Expand Down Expand Up @@ -886,6 +911,7 @@ fn resolve_link_order(obj: &ObjInfo) -> Result<Vec<ObjUnit>> {
name: name.clone(),
autogenerated: obj.is_unit_autogenerated(name),
comment_version: None,
order: None,
}
}
})
Expand Down

0 comments on commit b6a29fa

Please sign in to comment.