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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
use std::iter::Peekable; use std::slice::Iter; use crate::{FromBytes, ToBytes}; use crate::errors::{ReadError, ReadResult}; #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum SymBind { Local, Global, Extern, Unknown, } impl From<u8> for SymBind { fn from(byte: u8) -> SymBind { match byte { 0 => Self::Local, 1 => Self::Global, 2 => Self::Extern, _ => Self::Unknown, } } } impl From<SymBind> for u8 { fn from(bind: SymBind) -> u8 { match bind { SymBind::Local => 0, SymBind::Global => 1, SymBind::Extern => 2, SymBind::Unknown => 3, } } } impl ToBytes for SymBind { fn to_bytes(&self, buf: &mut Vec<u8>) { buf.push((*self).into()); } } impl FromBytes for SymBind { fn from_bytes(source: &mut Peekable<Iter<u8>>, _debug: bool) -> ReadResult<Self> where Self: Sized, { let value = *source.next().ok_or(ReadError::SymBindReadError)?; let sym_bind = SymBind::from(value); match sym_bind { SymBind::Unknown => Err(ReadError::UnknownSimBindReadError(value)), _ => Ok(sym_bind), } } } #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum SymType { NoType, Object, Func, Section, File, Unknown, } impl From<u8> for SymType { fn from(byte: u8) -> SymType { match byte { 0 => Self::NoType, 1 => Self::Object, 2 => Self::Func, 3 => Self::Section, 4 => Self::File, _ => Self::Unknown, } } } impl From<SymType> for u8 { fn from(sym_type: SymType) -> u8 { match sym_type { SymType::NoType => 0, SymType::Object => 1, SymType::Func => 2, SymType::Section => 3, SymType::File => 4, SymType::Unknown => 5, } } } impl ToBytes for SymType { fn to_bytes(&self, buf: &mut Vec<u8>) { buf.push((*self).into()); } } impl FromBytes for SymType { fn from_bytes(source: &mut Peekable<Iter<u8>>, _debug: bool) -> ReadResult<Self> where Self: Sized, { let value = *source.next().ok_or(ReadError::SymTypeReadError)?; let sym_type = SymType::from(value); match sym_type { SymType::Unknown => Err(ReadError::UnknownSimTypeReadError(value)), _ => Ok(sym_type), } } } #[derive(Debug, Clone, Copy, PartialEq)] pub struct KOSymbol { name_idx: usize, value_idx: usize, size: u16, sym_bind: SymBind, sym_type: SymType, sh_idx: u16, } impl KOSymbol { pub fn new( value_idx: usize, size: u16, sym_bind: SymBind, sym_type: SymType, sh_idx: u16, ) -> Self { KOSymbol { name_idx: 0, value_idx, size, sym_bind, sym_type, sh_idx, } } pub fn set_name_idx(&mut self, name_idx: usize) { self.name_idx = name_idx; } pub fn name_idx(&self) -> usize { self.name_idx } pub fn value_idx(&self) -> usize { self.value_idx } pub fn size(&self) -> u16 { self.size } pub fn sym_bind(&self) -> SymBind { self.sym_bind } pub fn sym_type(&self) -> SymType { self.sym_type } pub fn sh_idx(&self) -> u16 { self.sh_idx } pub fn size_bytes() -> u32 { 14 } } impl ToBytes for KOSymbol { fn to_bytes(&self, buf: &mut Vec<u8>) { (self.name_idx as u32).to_bytes(buf); (self.value_idx as u32).to_bytes(buf); self.size.to_bytes(buf); self.sym_bind.to_bytes(buf); self.sym_type.to_bytes(buf); self.sh_idx.to_bytes(buf); } } impl FromBytes for KOSymbol { fn from_bytes(source: &mut Peekable<Iter<u8>>, debug: bool) -> ReadResult<Self> where Self: Sized, { let name_idx = u32::from_bytes(source, debug) .map_err(|_| ReadError::KOSymbolConstantReadError("name index"))? as usize; let value_idx = u32::from_bytes(source, debug) .map_err(|_| ReadError::KOSymbolConstantReadError("value index"))? as usize; let size = u16::from_bytes(source, debug) .map_err(|_| ReadError::KOSymbolConstantReadError("size"))?; let sym_bind = SymBind::from_bytes(source, debug)?; let sym_type = SymType::from_bytes(source, debug)?; let sh_idx = u16::from_bytes(source, debug) .map_err(|_| ReadError::KOSymbolConstantReadError("section index"))?; Ok(KOSymbol { name_idx, value_idx, size, sym_bind, sym_type, sh_idx, }) } }