bin2c: Honor symbol alignment

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

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 ");