From 5128ff67b2adc7938ec00e931ba366bdb38d363b Mon Sep 17 00:00:00 2001 From: Luke Street Date: Sun, 26 Nov 2023 01:12:34 -0500 Subject: [PATCH] bin2c: Honor symbol alignment --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/util/bin2c.rs | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd5041f..243dcce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -295,7 +295,7 @@ dependencies = [ [[package]] name = "decomp-toolkit" -version = "0.6.2" +version = "0.6.3" dependencies = [ "anyhow", "ar", diff --git a/Cargo.toml b/Cargo.toml index 0e9ac07..7c69e64 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "decomp-toolkit" description = "Yet another GameCube/Wii decompilation toolkit." authors = ["Luke Street "] 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" diff --git a/src/util/bin2c.rs b/src/util/bin2c.rs index 5818cd5..64f9030 100644 --- a/src/util/bin2c.rs +++ b/src/util/bin2c.rs @@ -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 ");