1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use crate::util::{opt_u32, read_str, read_u32le, read_u8};
use std::fmt::{self, Display, Formatter};
use std::io::{self, Read};

/// A symbol declaration, which may include a definition.
#[derive(Debug)]
pub struct Symbol {
    name: Vec<u8>,
    visibility: SymbolVisibility,
}
impl Symbol {
    pub(crate) fn read_from(mut input: impl Read) -> Result<Self, io::Error> {
        let name = read_str(&mut input)?;
        let visibility = SymbolVisibility::read_from(input)?;
        Ok(Self { name, visibility })
    }

    /// The symbol's name, as stored raw in the object file.
    /// Like all names pulled from object files, this is not guaranteed to be valid UTF-8.
    pub fn name(&self) -> &[u8] {
        &self.name
    }

    /// The symbol's visibility, including its definition data (if any).
    pub fn visibility(&self) -> &SymbolVisibility {
        &self.visibility
    }
}

/// A symbol's visibility, and accompanying data if applicable.
#[derive(Debug)]
pub enum SymbolVisibility {
    /// The symbol is defined in this object file, but is not visible outside of it.
    Local(SymbolDef),
    /// The symbol is not defined in this object file.
    Imported,
    /// The symbol is defined in this object file, and is visible in others as well.
    Exported(SymbolDef),
}
impl SymbolVisibility {
    fn read_from(mut input: impl Read) -> Result<Self, io::Error> {
        use SymbolVisibility::*;

        match read_u8(&mut input)? {
            0 => Ok(Local(SymbolDef::read_from(input)?)),
            1 => Ok(Imported),
            2 => Ok(Exported(SymbolDef::read_from(input)?)),
            _ => Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "Invalid symbol visibility type",
            )),
        }
    }

    /// The symbol visibility's name.
    pub fn name(&self) -> &'static str {
        use SymbolVisibility::*;

        match self {
            Local(..) => "local",
            Imported => "import",
            Exported(..) => "export",
        }
    }

    /// Returns the symbol's definition data, if any.
    pub fn data(&self) -> Option<&SymbolDef> {
        use SymbolVisibility::*;

        match self {
            Local(data) | Exported(data) => Some(data),
            Imported => None,
        }
    }
}
impl Display for SymbolVisibility {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> {
        write!(fmt, "{}", self.name())
    }
}

/// A symbol definition's data.
#[derive(Debug)]
pub struct SymbolDef {
    source_file_id: u32,
    line_no: u32,
    section_id: Option<u32>,
    value: u32,
}
impl SymbolDef {
    fn read_from(mut input: impl Read) -> Result<Self, io::Error> {
        let source_file_id = read_u32le(&mut input)?;
        let line_no = read_u32le(&mut input)?;
        let section_id = opt_u32(read_u32le(&mut input)?);
        let value = read_u32le(input)?;

        Ok(Self {
            source_file_id,
            line_no,
            section_id,
            value,
        })
    }

    /// Where the file has been defined.
    /// That is, the [file stack node][crate::Node] ID, and the line number.
    pub fn source(&self) -> (u32, u32) {
        (self.source_file_id, self.line_no)
    }

    /// The ID of the [`Section`][crate::Section] the symbol was defined in.
    /// This is `None` for constants.
    pub fn section(&self) -> Option<u32> {
        self.section_id
    }

    /// The symbol's offset within its [`Section`][crate::Section], or its value if not attached to one.
    pub fn value(&self) -> u32 {
        self.value
    }
}