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};
#[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 })
}
pub fn name(&self) -> &[u8] {
&self.name
}
pub fn visibility(&self) -> &SymbolVisibility {
&self.visibility
}
}
#[derive(Debug)]
pub enum SymbolVisibility {
Local(SymbolDef),
Imported,
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",
)),
}
}
pub fn name(&self) -> &'static str {
use SymbolVisibility::*;
match self {
Local(..) => "local",
Imported => "import",
Exported(..) => "export",
}
}
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())
}
}
#[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,
})
}
pub fn source(&self) -> (u32, u32) {
(self.source_file_id, self.line_no)
}
pub fn section(&self) -> Option<u32> {
self.section_id
}
pub fn value(&self) -> u32 {
self.value
}
}