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
use std::io::{Read, Write};
use {Error, Header, SectionContent};
use types;
use num_traits::{FromPrimitive, ToPrimitive};

#[derive(Debug, Clone)]
pub enum DynamicContent {
    None,
    String((Vec<u8>,Option<u64>)),
    Address(u64),
    Flags1(types::DynamicFlags1),
}

impl Default for DynamicContent{
    fn default() -> Self {
        DynamicContent::None
    }
}

#[derive(Debug, Clone, Default)]
pub struct Dynamic {
    pub dhtype: types::DynamicType,
    pub content: DynamicContent,
}

impl Dynamic {
    pub fn entsize(eh: &Header) -> usize {
        match eh.ident_class {
            types::Class::Class64 => 16,
            types::Class::Class32 => 8,
        }
    }

    pub fn from_reader<R>(
        mut io: R,
        linked: Option<&SectionContent>,
        eh: &Header,
    ) -> Result<SectionContent, Error>
    where
        R: Read,
    {
        let strtab = match linked {
            None => None,
            Some(&SectionContent::Strtab(ref s)) => Some(s),
            any => return Err(Error::LinkedSectionIsNotStrtab{
                during: "reading dynamic",
                link: any.map(|v|v.clone()),
            }),
        };

        let mut r = Vec::new();

        while let Ok(tag) = elf_read_uclass!(eh, io) {
            let val = elf_read_uclass!(eh, io)?;

            match types::DynamicType::from_u64(tag) {
                None => return Err(Error::InvalidDynamicType(tag)),
                Some(types::DynamicType::NULL) => {
                    r.push(Dynamic {
                        dhtype: types::DynamicType::NULL,
                        content: DynamicContent::None,
                    });
                    break;
                },
                Some(types::DynamicType::RPATH) => {
                    r.push(Dynamic {
                        dhtype: types::DynamicType::RPATH,
                        content: DynamicContent::String(match strtab {
                            None => (Vec::default(),None),
                            Some(s) => (s.get(val as usize), Some(val)),
                        }),
                    });
                },
                Some(types::DynamicType::NEEDED) => {
                    r.push(Dynamic {
                        dhtype: types::DynamicType::NEEDED,
                        content: DynamicContent::String(match strtab {
                            None => (Vec::default(),None),
                            Some(s) => (s.get(val as usize), Some(val)),
                        }),
                    });
                },
                Some(types::DynamicType::FLAGS_1) => {
                    r.push(Dynamic {
                        dhtype: types::DynamicType::FLAGS_1,
                        content: DynamicContent::Flags1(
                            match types::DynamicFlags1::from_bits(val) {
                                Some(v) => v,
                                None => return Err(Error::InvalidDynamicFlags1(val)),
                            },
                        ),
                    });
                },
                Some(x) => {
                    r.push(Dynamic {
                        dhtype: x,
                        content: DynamicContent::Address(val),
                    });
                }
            };
        }

        Ok(SectionContent::Dynamic(r))
    }
    pub fn to_writer<W>(
        &self,
        mut io: W,
        eh: &Header,
    ) -> Result<(usize), Error>
    where
        W: Write,
    {
        elf_write_uclass!(eh, io, self.dhtype.to_u64().unwrap())?;

        match self.content {
            DynamicContent::None => {
                elf_write_uclass!(eh, io, 0)?;
            }
            DynamicContent::String(ref s) => match s.1 {
                Some(val) => elf_write_uclass!(eh, io, val)?,
                None      => return Err(Error::WritingNotSynced),
            },
            DynamicContent::Address(ref v) => {
                elf_write_uclass!(eh, io, *v)?;
            }
            DynamicContent::Flags1(ref v) => {
                elf_write_uclass!(eh, io, v.bits())?;
            }
        }
        Ok(Dynamic::entsize(eh))
    }

    pub fn sync(&mut self, linked: Option<&mut SectionContent>, _: &Header) -> Result<(), Error> {
        match self.content {
            DynamicContent::String(ref mut s) => match linked {
                Some(&mut SectionContent::Strtab(ref mut strtab)) => {
                    s.1 = Some(strtab.insert(&s.0) as u64);
                }
                any => return Err(Error::LinkedSectionIsNotStrtab{
                    during: "syncing dynamic",
                    link: any.map(|v|v.clone()),
                }),
            },
            DynamicContent::None => {}
            DynamicContent::Address(_) => {}
            DynamicContent::Flags1(_) => {}
        }
        Ok(())
    }
}