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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use crate::io::{Endian, ReadExt as _, WriteExt as _};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use std::io::{Read, Write};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NList64 {
pub n_strx: u32,
pub n_type: NTypeField,
pub n_sect: u8,
pub n_desc: u16,
pub n_value: u64,
}
impl NList64 {
pub const SIZE: u32 = 0x10;
pub const NO_SECT: u8 = 0;
pub const MAX_SECT: u8 = 255;
pub fn read_from_in<R: Read>(read: &mut R, endian: Endian) -> Self {
let n_strx = read.read_u32_in(endian);
let n_type_n = read.read_u8();
let n_type = NTypeField::from_u8(n_type_n);
let n_sect = read.read_u8();
let n_desc = read.read_u16_in(endian);
let n_value = read.read_u64_in(endian);
NList64 {
n_strx,
n_type,
n_sect,
n_desc,
n_value,
}
}
pub fn write_into<W: Write>(&self, write: &mut W) {
write.write_u32_native(self.n_strx);
write.write_u8(self.n_type.to_u8());
write.write_u8(self.n_sect);
write.write_u16_native(self.n_desc);
write.write_u64_native(self.n_value);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NTypeField {
Norm {
n_pext: bool,
n_type: NType,
n_ext: bool,
},
Stab(DebugSymbol),
}
impl NTypeField {
pub const N_STAB_MASK: u8 = 0xe0;
pub const N_PEXT_MASK: u8 = 0x10;
pub const N_TYPE_MASK: u8 = 0x0e;
pub const N_EXT_MASK: u8 = 0x01;
pub fn from_u8(n: u8) -> Self {
let n_stab = n & Self::N_STAB_MASK;
if n_stab == 0 {
let n_pext = n & Self::N_PEXT_MASK == Self::N_PEXT_MASK;
let n_type = NType::from_u8(n & Self::N_TYPE_MASK);
let n_ext = n & Self::N_EXT_MASK == Self::N_EXT_MASK;
NTypeField::Norm {
n_pext,
n_type,
n_ext,
}
} else {
NTypeField::Stab(DebugSymbol::from_u8(n_stab))
}
}
pub fn to_u8(self) -> u8 {
match self {
NTypeField::Norm {
n_pext,
n_type,
n_ext,
} => n_pext as u8 * Self::N_PEXT_MASK | n_type.to_u8() | n_ext as u8 * Self::N_EXT_MASK,
NTypeField::Stab(stab) => stab.to_u8(),
}
}
}
#[derive(FromPrimitive, Debug, Clone, Copy, PartialEq, Eq)]
pub enum NType {
Undf = 0x0,
Abs = 0x2,
Sect = 0xe,
Pbud = 0xc,
Indr = 0xa,
}
impl NType {
pub fn from_u8(n: u8) -> Self {
FromPrimitive::from_u8(n).unwrap_or_else(|| panic!("Invalid n_type number {}", n))
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
#[derive(FromPrimitive, Debug, Clone, Copy, PartialEq, Eq)]
pub enum DebugSymbol {
Gsym = 0x20,
}
impl DebugSymbol {
pub fn from_u8(n: u8) -> Self {
FromPrimitive::from_u8(n).unwrap_or_else(|| panic!("Unsupported debug symbol {}", n))
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn write_and_read_nlist() {
let nlist = NList64 {
n_strx: 42,
n_type: NTypeField::Norm {
n_pext: false,
n_type: NType::Sect,
n_ext: true,
},
n_sect: 2,
n_desc: 0,
n_value: 42,
};
let mut buf = Vec::new();
nlist.write_into(&mut buf);
assert_eq!(buf.len(), NList64::SIZE as usize);
let read = NList64::read_from_in(&mut buf.as_slice(), Endian::NATIVE);
assert_eq!(read, nlist);
}
}