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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use std::prelude::v1::*;
use std::{cmp, fmt, iter, mem, slice};
use crate::image::{IMAGE_BASE_RELOCATION, IMAGE_REL_BASED_ABSOLUTE};
use crate::util::{extend_in_place, AlignTo};
use crate::{Error, Result};
#[derive(Copy, Clone)]
pub struct BaseRelocs<'a> {
relocs: &'a [u8],
}
impl<'a> BaseRelocs<'a> {
pub(crate) unsafe fn new(relocs: &'a [u8]) -> BaseRelocs<'a> {
debug_assert!(relocs.as_ptr().aligned_to(4)); BaseRelocs { relocs }
}
pub fn parse(relocs: &'a [u8]) -> Result<BaseRelocs<'a>> {
if !(cfg!(feature = "unsafe_alignment") || relocs.as_ptr().aligned_to(4)) { return Err(Error::Misaligned);
}
Ok(BaseRelocs { relocs })
}
pub fn image(&self) -> &'a [u8] {
self.relocs
}
pub fn iter_blocks(&self) -> IterBlocks<'a> {
IterBlocks { data: self.relocs }
}
pub fn for_each<F: FnMut(u32, u8)>(&self, mut f: F) {
self.fold((), |(), rva, ty| f(rva, ty))
}
pub fn fold<T, F>(&self, init: T, mut f: F) -> T where F: FnMut(T, u32, u8) -> T {
let mut accum = init;
for block in self.iter_blocks() {
for word in block.words() {
let ty = block.type_of(word);
if ty != IMAGE_REL_BASED_ABSOLUTE {
let rva = block.rva_of(word);
accum = f(accum, rva, ty);
}
}
}
accum
}
}
impl<'a> fmt::Debug for BaseRelocs<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("BaseRelocs").finish()
}
}
#[derive(Clone)]
pub struct IterBlocks<'a> {
data: &'a [u8],
}
impl<'a> IterBlocks<'a> {
fn peek(&self) -> Option<Block<'a>> {
if mem::size_of_val(self.data) >= mem::size_of::<IMAGE_BASE_RELOCATION>() { Some(unsafe {
let image_p = self.data.as_ptr() as *const IMAGE_BASE_RELOCATION;
let image = &*image_p;
let len = cmp::min(image.SizeOfBlock as usize, self.data.len()).saturating_sub(mem::size_of::<IMAGE_BASE_RELOCATION>()) / 2;
let words = slice::from_raw_parts(image_p.offset(1) as *const u16, len);
Block { image, words }
})
}
else {
None
}
}
}
impl<'a> Iterator for IterBlocks<'a> {
type Item = Block<'a>;
fn next(&mut self) -> Option<Block<'a>> {
if let Some(block) = self.peek() {
let block_size = block.image.SizeOfBlock;
let block_size = cmp::max(block_size, mem::size_of::<IMAGE_BASE_RELOCATION>() as u32);
let block_size = block_size.align_to(4); let block_size = cmp::min(block_size as usize, self.data.len());
self.data = &self.data[block_size..];
Some(block)
}
else {
None
}
}
}
impl<'a> iter::FusedIterator for IterBlocks<'a> {}
#[derive(Copy, Clone)]
pub struct Block<'a> {
image: &'a IMAGE_BASE_RELOCATION,
words: &'a [u16],
}
impl<'a> Block<'a> {
pub fn image(&self) -> &'a IMAGE_BASE_RELOCATION {
self.image
}
pub fn words(&self) -> &'a [u16] {
self.words
}
pub fn rva_of(&self, word: &u16) -> u32 {
let offset = (word & 0x0fff) as u32;
self.image.VirtualAddress.wrapping_add(offset)
}
pub fn type_of(&self, word: &u16) -> u8 {
(word >> 12) as u8
}
}
impl<'a> fmt::Debug for Block<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Block")
.field("virtual_address", &format_args!("{:#x?}", self.image.VirtualAddress))
.field("words.len", &self.words().len())
.finish()
}
}
#[cfg(feature = "serde")]
mod serde {
use crate::util::serde_helper::*;
use super::BaseRelocs;
impl<'a> Serialize for BaseRelocs<'a> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut state = serializer.serialize_struct("BaseRelocs", 2)?;
let mut rvas = Vec::new();
let mut types = Vec::new();
self.for_each(|rva, ty| {
rvas.push(rva);
types.push(ty);
});
state.serialize_field("rvas", &*rvas)?;
state.serialize_field("types", &*types)?;
state.end()
}
}
}
fn encode_type_offset(base: u32, rva: u32, ty: u8) -> u16 {
(((rva - base) | (ty as u32) << 12) & 0xffff) as u16
}
pub fn build(mut rvas: &[u32], mut types: &[u8]) -> Vec<u8> {
assert_eq!(rvas.len(), types.len());
let mut result = Vec::<u8>::new();
while rvas.len() > 0 {
let start = rvas[0] & !0x0fff;
let end = start + 0x0fff;
let mut n = 0;
while n < rvas.len() && rvas[n] >= start && rvas[n] < end {
n += 1;
}
let size = (8 + 2 * n).align_to(4);
unsafe {
extend_in_place(&mut result, size, |bytes| {
let block_ptr = bytes.as_mut_ptr() as *mut IMAGE_BASE_RELOCATION;
(*block_ptr).VirtualAddress = start;
(*block_ptr).SizeOfBlock = size as u32;
let words = slice::from_raw_parts_mut(block_ptr.offset(1) as *mut u16, n.align_to(2));
for i in 0..n {
let rva = *rvas.get_unchecked(i);
let ty = *types.get_unchecked(i);
words[i] = encode_type_offset(start, rva, ty);
}
if n < words.len() {
words[n] = 0;
}
});
}
rvas = &rvas[n..];
types = &types[n..];
}
result
}
#[cfg(windows)]
#[test]
fn test_build_self() {
if crate::image::IMAGE_BASE_PANICS {
return;
}
use crate::pe::*;
let view = unsafe { PeView::new() };
if let Ok(base_relocs) = view.base_relocs() {
let mut rvas = Vec::new();
let mut types = Vec::new();
base_relocs.for_each(|rva, ty| {
rvas.push(rva);
types.push(ty);
});
let rebuild = build(&rvas, &types);
assert_eq!(rebuild, base_relocs.image());
}
}