bin2c: Honor symbol alignment

This commit is contained in:
Luke Street 2023-11-26 01:12:34 -05:00
parent dd60128ba0
commit 5128ff67b2
3 changed files with 17 additions and 3 deletions

2
Cargo.lock generated
View File

@ -295,7 +295,7 @@ dependencies = [
[[package]]
name = "decomp-toolkit"
version = "0.6.2"
version = "0.6.3"
dependencies = [
"anyhow",
"ar",

View File

@ -3,7 +3,7 @@ name = "decomp-toolkit"
description = "Yet another GameCube/Wii decompilation toolkit."
authors = ["Luke Street <luke@street.dev>"]
license = "MIT OR Apache-2.0"
version = "0.6.2"
version = "0.6.3"
edition = "2021"
publish = false
repository = "https://github.com/encounter/decomp-toolkit"

View File

@ -1,8 +1,22 @@
use crate::obj::{ObjSection, ObjSectionKind, ObjSymbol};
const PROLOGUE: &str = r#"
#ifndef ATTRIBUTE_ALIGN
#if defined(__MWERKS__) || defined(__GNUC__)
#define ATTRIBUTE_ALIGN(num) __attribute__((aligned(num)))
#elif defined(_MSC_VER) || defined(__INTELLISENSE__)
#define ATTRIBUTE_ALIGN(num)
#else
#error unknown compiler
#endif
#endif
"#;
/// Converts a binary blob into a C array.
pub fn bin2c(symbol: &ObjSymbol, section: &ObjSection, data: &[u8]) -> String {
let mut output = String::new();
output.push_str(PROLOGUE);
output.push_str(&format!(
"// {} (size: {:#X}, address: {:#X}, section: {})\n",
symbol.name, symbol.size, symbol.address, section.name
@ -15,7 +29,7 @@ pub fn bin2c(symbol: &ObjSymbol, section: &ObjSection, data: &[u8]) -> String {
}
output.push_str("unsigned char ");
output.push_str(symbol.demangled_name.as_deref().unwrap_or(symbol.name.as_str()));
output.push_str("[] = {");
output.push_str(&format!("[] ATTRIBUTE_ALIGN({}) = {{", symbol.align.unwrap_or(4)));
for (i, byte) in data.iter().enumerate() {
if i % 16 == 0 {
output.push_str("\n ");