atom_macho/load_command/
dysymtab.rs1use crate::io::{Endian, ReadExt as _, WriteExt as _};
2use std::io::{Read, Write};
3
4#[derive(Debug, Clone, PartialEq, Eq)]
43pub struct DysymtabCommand {
44 pub cmd: u32,
45 pub cmdsize: u32,
46 pub ilocalsym: u32,
48 pub nlocalsym: u32,
50 pub iextdefsym: u32,
52 pub nextdefsym: u32,
54 pub iundefsym: u32,
56 pub nundefsym: u32,
58 pub tocoff: u32,
60 pub ntoc: u32,
62 pub modtaboff: u32,
64 pub nmodtab: u32,
66 pub extrefsymoff: u32,
68 pub nextrefsyms: u32,
70 pub indirectsymoff: u32,
72 pub nindirectsyms: u32,
74 pub extreloff: u32,
76 pub nextrel: u32,
78 pub locreloff: u32,
80 pub nlocrel: u32,
82}
83
84impl DysymtabCommand {
85 pub const TYPE: u32 = 0xB;
86
87 pub const SIZE: u32 = 0x50;
88
89 pub fn read_from_in<R: Read>(read: &mut R, endian: Endian) -> Self {
90 let cmd = read.read_u32_in(endian);
91 assert_eq!(cmd, Self::TYPE);
92
93 let cmdsize = read.read_u32_in(endian);
94 assert_eq!(cmdsize, Self::SIZE);
95
96 let ilocalsym = read.read_u32_in(endian);
97 let nlocalsym = read.read_u32_in(endian);
98 let iextdefsym = read.read_u32_in(endian);
99 let nextdefsym = read.read_u32_in(endian);
100 let iundefsym = read.read_u32_in(endian);
101 let nundefsym = read.read_u32_in(endian);
102 let tocoff = read.read_u32_in(endian);
103 let ntoc = read.read_u32_in(endian);
104 let modtaboff = read.read_u32_in(endian);
105 let nmodtab = read.read_u32_in(endian);
106 let extrefsymoff = read.read_u32_in(endian);
107 let nextrefsyms = read.read_u32_in(endian);
108 let indirectsymoff = read.read_u32_in(endian);
109 let nindirectsyms = read.read_u32_in(endian);
110 let extreloff = read.read_u32_in(endian);
111 let nextrel = read.read_u32_in(endian);
112 let locreloff = read.read_u32_in(endian);
113 let nlocrel = read.read_u32_in(endian);
114
115 DysymtabCommand {
116 cmd,
117 cmdsize,
118 ilocalsym,
119 nlocalsym,
120 iextdefsym,
121 nextdefsym,
122 iundefsym,
123 nundefsym,
124 tocoff,
125 ntoc,
126 modtaboff,
127 nmodtab,
128 extrefsymoff,
129 nextrefsyms,
130 indirectsymoff,
131 nindirectsyms,
132 extreloff,
133 nextrel,
134 locreloff,
135 nlocrel,
136 }
137 }
138
139 pub fn write_into<W: Write>(&self, write: &mut W) {
140 write.write_u32_native(self.cmd);
141 write.write_u32_native(self.cmdsize);
142 write.write_u32_native(self.ilocalsym);
143 write.write_u32_native(self.nlocalsym);
144 write.write_u32_native(self.iextdefsym);
145 write.write_u32_native(self.nextdefsym);
146 write.write_u32_native(self.iundefsym);
147 write.write_u32_native(self.nundefsym);
148 write.write_u32_native(self.tocoff);
149 write.write_u32_native(self.ntoc);
150 write.write_u32_native(self.modtaboff);
151 write.write_u32_native(self.nmodtab);
152 write.write_u32_native(self.extrefsymoff);
153 write.write_u32_native(self.nextrefsyms);
154 write.write_u32_native(self.indirectsymoff);
155 write.write_u32_native(self.nindirectsyms);
156 write.write_u32_native(self.extreloff);
157 write.write_u32_native(self.nextrel);
158 write.write_u32_native(self.locreloff);
159 write.write_u32_native(self.nlocrel);
160 }
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166
167 #[test]
168 fn write_and_read_dysymtab_command() {
169 let cmd = DysymtabCommand {
170 cmd: DysymtabCommand::TYPE,
171 cmdsize: DysymtabCommand::SIZE,
172 ilocalsym: 0,
173 nlocalsym: 2,
174 iextdefsym: 3,
175 nextdefsym: 4,
176 iundefsym: 5,
177 nundefsym: 9,
178 tocoff: 8,
179 ntoc: 5,
180 modtaboff: 8,
181 nmodtab: 5,
182 extrefsymoff: 0,
183 nextrefsyms: 2,
184 indirectsymoff: 3,
185 nindirectsyms: 6,
186 extreloff: 8,
187 nextrel: 9,
188 locreloff: 0,
189 nlocrel: 2,
190 };
191
192 let mut buf = Vec::new();
193
194 cmd.write_into(&mut buf);
195
196 assert_eq!(buf.len(), DysymtabCommand::SIZE as usize);
197
198 let read_cmd = DysymtabCommand::read_from_in(&mut buf.as_slice(), Endian::NATIVE);
199
200 assert_eq!(read_cmd, cmd);
201 }
202}