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
use std::io::{Read, Write}; use {Error, Header, types, SectionContent}; use num_traits::{FromPrimitive,ToPrimitive}; use utils::find_or_add_to_strtab; #[derive(Debug, Default, Clone)] pub struct Symbol { pub shndx: u16, pub value: u64, pub size: u64, pub name: String, pub stype: types::SymbolType, pub bind: types::SymbolBind, pub vis: types::SymbolVis, } impl Symbol { fn from_val( tab: Option<&Vec<u8>>, _name: u32, info: u8, other: u8, shndx: u16, value: u64, size: u64, ) -> Result<Symbol, Error> { let name = match tab { Some(s) => String::from_utf8_lossy( s[_name as usize ..].split(|c|*c==0).next().unwrap_or(&[0;0])).into_owned(), None => String::default(), }; let reb = info & 0xf; let stype = match types::SymbolType::from_u8(reb) { Some(v) => v, None => return Err(Error::InvalidSymbolType(reb)), }; let reb = info >> 4; let bind = match types::SymbolBind::from_u8(reb) { Some(v) => v, None => return Err(Error::InvalidSymbolBind(reb)), }; let reb = other & 0x3; let vis = match types::SymbolVis::from_u8(reb) { Some(v) => v, None => return Err(Error::InvalidSymbolVis(reb)), }; Ok(Symbol { shndx: shndx, value: value, size: size, name: name, stype: stype, bind: bind, vis: vis, }) } pub fn entsize(eh: &Header) -> usize { match eh.ident_class { types::Class::Class64 => 24, types::Class::Class32 => 16, } } pub fn from_reader<R>(mut io: R, linked: Option<&SectionContent>, eh: &Header) -> Result<SectionContent, Error> where R: Read{ let tab = match linked { None => None, Some(&SectionContent::Raw(ref s)) => Some(s), _ => return Err(Error::LinkedSectionIsNotStrings), }; let mut r = Vec::new(); let mut b = vec![0; Self::entsize(eh)]; while io.read(&mut b)? > 0 { let mut br = &b[..]; let _name = elf_read_u32!(eh, br)?; r.push(match eh.ident_class { types::Class::Class64 => { let info = b[4]; let other = b[5]; br = &b[6..]; let shndx = elf_read_u16!(eh, br)?; let value = elf_read_u64!(eh, br)?; let size = elf_read_u64!(eh, br)?; Symbol::from_val(tab, _name, info, other, shndx, value, size)? }, types::Class::Class32 => { let value = elf_read_u32!(eh, br)?; let size = elf_read_u32!(eh, br)?; let info = b[12]; let other = b[13]; br = &b[14..]; let shndx = elf_read_u16!(eh, br)?; Symbol::from_val(tab, _name, info, other, shndx, value as u64, size as u64)? }, }) } Ok(SectionContent::Symbols(r)) } pub fn to_writer<W>(&self, mut io: W, linked: Option<&mut SectionContent>, eh: &Header) -> Result<(), Error> where W: Write { match linked { Some(&mut SectionContent::Raw(ref mut strtab)) => { let off = find_or_add_to_strtab(strtab, self.name.bytes().collect()) as u32; elf_write_u32!(eh, io, off)?; }, _ => elf_write_u32!(eh, io, 0)?, } let info = (self.bind.to_u8().unwrap() << 4) + (self.stype.to_u8().unwrap() & 0xf); let other = self.vis.to_u8().unwrap(); match eh.ident_class { types::Class::Class64 => { io.write(&[info, other])?; elf_write_u16!(eh, io, self.shndx)?; elf_write_u64!(eh, io, self.value)?; elf_write_u64!(eh, io, self.size)?; }, types::Class::Class32 => { elf_write_u32!(eh, io, self.value as u32)?; elf_write_u32!(eh, io, self.size as u32)?; io.write(&[info, other])?; elf_write_u16!(eh, io, self.shndx)?; }, }; Ok(()) } }