Skip to main content

objdiff_core/obj/
comment.rs

1use anyhow::{Result, anyhow};
2
3use crate::util::{read_u8, read_u16, read_u32};
4
5pub const COMMENT_SECTION: &str = ".comment";
6
7const MAGIC: &[u8] = "CodeWarrior".as_bytes();
8const HEADER_SIZE: u8 = 0x2C;
9
10// Partial implementation, does not include all fields
11pub struct MWComment {
12    pub version: u8,
13}
14
15impl MWComment {
16    pub fn from_reader(_obj_file: &object::File, reader: &mut &[u8]) -> Result<Self> {
17        let mut out = MWComment { version: 0 };
18        let magic: [u8; MAGIC.len()] = reader[..MAGIC.len()].try_into()?;
19        *reader = &reader[MAGIC.len()..];
20        if magic != MAGIC {
21            return Err(anyhow!("Invalid .comment section magic: {magic:?}"));
22        }
23        out.version = read_u8(reader)?;
24        if !matches!(out.version, 8 | 10 | 11 | 13 | 14 | 15) {
25            return Err(anyhow!("Unknown .comment section version: {}", out.version));
26        }
27        *reader = &reader[8..];
28        let header_size = read_u8(reader)?;
29        if header_size != HEADER_SIZE {
30            return Err(anyhow!("Expected header size {HEADER_SIZE:#X}, got {header_size:#X}"));
31        }
32        *reader = &reader[0x17..];
33        Ok(out)
34    }
35}
36
37#[derive(Debug, Copy, Clone)]
38pub struct CommentSym {
39    pub align: u32,
40    pub vis_flags: u8,
41    pub active_flags: u8,
42}
43
44impl CommentSym {
45    pub fn from_reader(obj_file: &object::File, reader: &mut &[u8]) -> Result<Self> {
46        let mut out = CommentSym { align: 0, vis_flags: 0, active_flags: 0 };
47        out.align = read_u32(obj_file, reader)?;
48        out.vis_flags = read_u8(reader)?;
49        if !matches!(out.vis_flags, 0 | 0xD | 0xE) {
50            log::warn!("Unknown vis_flags: {:#X}", out.vis_flags);
51        }
52        out.active_flags = read_u8(reader)?;
53        if !matches!(out.active_flags, 0 | 0x8 | 0x10 | 0x20) {
54            log::warn!("Unknown active_flags: {:#X}", out.active_flags);
55        }
56        let padding = read_u16(obj_file, reader)?;
57        if padding != 0 {
58            return Err(anyhow!("Unexpected value after active_flags: {padding:#X}"));
59        }
60        Ok(out)
61    }
62}