From 5272f90991daa0e8cff532125ddc2ca5e04df6c9 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Thu, 10 Aug 2023 00:03:44 -0400 Subject: [PATCH] Read symbol alignment from .comment --- src/util/elf.rs | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/util/elf.rs b/src/util/elf.rs index 5ff5cc5..44902ef 100644 --- a/src/util/elf.rs +++ b/src/util/elf.rs @@ -99,6 +99,27 @@ pub fn process_elf>(path: P) -> Result { }); } + let mw_comment = if let Some(comment_section) = obj_file.section_by_name(".comment") { + let data = comment_section.uncompressed_data()?; + let mut reader = Cursor::new(&*data); + let header = + MWComment::parse_header(&mut reader).context("While reading .comment section")?; + log::debug!("Loaded .comment section header {:?}", header); + let mut comment_syms = Vec::with_capacity(obj_file.symbols().count()); + for symbol in obj_file.symbols() { + let comment_sym = read_comment_sym(&mut reader)?; + log::debug!("Symbol {:?} -> Comment {:?}", symbol, comment_sym); + comment_syms.push(comment_sym); + } + ensure!( + data.len() - reader.position() as usize == 0, + ".comment section data not fully read" + ); + Some((header, comment_syms)) + } else { + None + }; + let mut symbols: Vec = vec![]; let mut symbol_indexes: Vec> = vec![]; let mut section_starts = IndexMap::>::new(); @@ -247,7 +268,8 @@ pub fn process_elf>(path: P) -> Result { continue; } symbol_indexes.push(Some(symbols.len())); - symbols.push(to_obj_symbol(&obj_file, &symbol, §ion_indexes)?); + let align = mw_comment.as_ref().map(|(_, vec)| vec[symbol.index().0].align); + symbols.push(to_obj_symbol(&obj_file, &symbol, §ion_indexes, align)?); } let mut link_order = Vec::::new(); @@ -294,28 +316,9 @@ pub fn process_elf>(path: P) -> Result { } } - let mw_comment = if let Some(comment_section) = obj_file.section_by_name(".comment") { - let data = comment_section.uncompressed_data()?; - let mut reader = Cursor::new(&*data); - let header = - MWComment::parse_header(&mut reader).context("While reading .comment section")?; - log::debug!("Loaded .comment section header {:?}", header); - for symbol in obj_file.symbols() { - let comment_sym = read_comment_sym(&mut reader)?; - log::debug!("Symbol {:?} -> Comment {:?}", symbol, comment_sym); - } - ensure!( - data.len() - reader.position() as usize == 0, - ".comment section data not fully read" - ); - Some(header) - } else { - None - }; - let mut obj = ObjInfo::new(kind, architecture, obj_name, symbols, sections); obj.entry = obj_file.entry(); - obj.mw_comment = mw_comment; + obj.mw_comment = mw_comment.map(|(header, _)| header); obj.sda2_base = sda2_base; obj.sda_base = sda_base; obj.stack_address = stack_address; @@ -743,6 +746,7 @@ fn to_obj_symbol( obj_file: &object::File<'_>, symbol: &Symbol<'_, '_>, section_indexes: &[Option], + align: Option, ) -> Result { let section = match symbol.section_index() { Some(idx) => Some(obj_file.section_by_index(idx)?), @@ -788,8 +792,7 @@ fn to_obj_symbol( SymbolKind::Section => ObjSymbolKind::Section, _ => bail!("Unsupported symbol kind: {:?}", symbol), }, - // TODO common symbol value? - align: None, + align, data_kind: Default::default(), }) }