Skip to content

Commit

Permalink
Remove elf split command
Browse files Browse the repository at this point in the history
This command was an early experiment and only worked
correctly on one game. The project system has long
superseded it. ELFs are better supported by
generating a project config via `elf config` and
using the DOL as the object target.

Closes #1
Closes #4
Closes #21
  • Loading branch information
encounter committed Jan 28, 2025
1 parent f270e9c commit a6c7001
Showing 1 changed file with 4 additions and 50 deletions.
54 changes: 4 additions & 50 deletions src/cmd/elf.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{
collections::{btree_map, hash_map, BTreeMap, HashMap},
collections::{btree_map, BTreeMap, HashMap},
fs,
fs::DirBuilder,
io::{Cursor, Write},
};

use anyhow::{anyhow, bail, ensure, Context, Result};
use anyhow::{anyhow, ensure, Context, Result};
use argp::FromArgs;
use objdiff_core::obj::split_meta::{SplitMeta, SPLITMETA_SECTION};
use object::{
Expand All @@ -14,15 +14,15 @@ use object::{
FileFlags, Object, ObjectSection, ObjectSymbol, RelocationTarget, SectionFlags, SectionIndex,
SectionKind, SymbolFlags, SymbolIndex, SymbolKind, SymbolScope, SymbolSection,
};
use typed_path::{Utf8NativePath, Utf8NativePathBuf};
use typed_path::Utf8NativePathBuf;

use crate::{
obj::ObjKind,
util::{
asm::write_asm,
comment::{CommentSym, MWComment},
config::{write_splits_file, write_symbols_file},
elf::{process_elf, write_elf},
elf::process_elf,
file::{buf_writer, process_rsp},
path::native_path,
reader::{Endian, FromReader},
Expand All @@ -47,7 +47,6 @@ enum SubCommand {
Disasm(DisasmArgs),
Fixup(FixupArgs),
Signatures(SignaturesArgs),
Split(SplitArgs),
Info(InfoArgs),
}

Expand Down Expand Up @@ -75,18 +74,6 @@ pub struct FixupArgs {
out_file: Utf8NativePathBuf,
}

#[derive(FromArgs, PartialEq, Eq, Debug)]
/// Splits an executable ELF into relocatable objects.
#[argp(subcommand, name = "split")]
pub struct SplitArgs {
#[argp(positional, from_str_fn(native_path))]
/// input file
in_file: Utf8NativePathBuf,
#[argp(positional, from_str_fn(native_path))]
/// output directory
out_dir: Utf8NativePathBuf,
}

#[derive(FromArgs, PartialEq, Eq, Debug)]
/// Generates configuration files from an executable ELF.
#[argp(subcommand, name = "config")]
Expand Down Expand Up @@ -128,7 +115,6 @@ pub fn run(args: Args) -> Result<()> {
SubCommand::Config(c_args) => config(c_args),
SubCommand::Disasm(c_args) => disasm(c_args),
SubCommand::Fixup(c_args) => fixup(c_args),
SubCommand::Split(c_args) => split(c_args),
SubCommand::Signatures(c_args) => signatures(c_args),
SubCommand::Info(c_args) => info(c_args),
}
Expand Down Expand Up @@ -180,38 +166,6 @@ fn disasm(args: DisasmArgs) -> Result<()> {
Ok(())
}

fn split(args: SplitArgs) -> Result<()> {
let obj = process_elf(&args.in_file)?;
ensure!(obj.kind == ObjKind::Executable, "Can only split executable objects");

let mut file_map = HashMap::<String, Vec<u8>>::new();

let split_objs = split_obj(&obj, None)?;
for (unit, split_obj) in obj.link_order.iter().zip(&split_objs) {
let out_obj = write_elf(split_obj, false)?;
match file_map.entry(unit.name.clone()) {
hash_map::Entry::Vacant(e) => e.insert(out_obj),
hash_map::Entry::Occupied(_) => bail!("Duplicate file {}", unit.name),
};
}

let mut rsp_file = buf_writer(Utf8NativePath::new("rsp"))?;
for unit in &obj.link_order {
let object = file_map
.get(&unit.name)
.ok_or_else(|| anyhow!("Failed to find object file for unit '{}'", unit.name))?;
let out_name = file_stem_from_unit(&unit.name);
let out_path = args.out_dir.join(format!("{}.o", out_name));
writeln!(rsp_file, "{}", out_path)?;
if let Some(parent) = out_path.parent() {
DirBuilder::new().recursive(true).create(parent)?;
}
fs::write(&out_path, object).with_context(|| format!("Failed to write '{}'", out_path))?;
}
rsp_file.flush()?;
Ok(())
}

fn file_stem_from_unit(str: &str) -> String {
let str = str.strip_suffix(ASM_SUFFIX).unwrap_or(str);
let str = str.strip_prefix("C:").unwrap_or(str);
Expand Down

0 comments on commit a6c7001

Please sign in to comment.