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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use crate::common::{BaseAddresses, Encoding, Format, padding, sign_extend, size_mask};
use crate::constants::*;
use crate::endianity::Endianity;
use crate::leb128;
use crate::write::{Address, Error, Result};
/// A trait for writing the data to a section that holds LSDAs.
///
/// All write operations append to the section unless otherwise specified.
#[allow(clippy::len_without_is_empty)]
pub trait Writer {
/// The endianity of bytes that are written.
type Endian: Endianity;
/// Return the endianity of bytes that are written.
fn endian(&self) -> Self::Endian;
/// Return the current section length.
///
/// This is the section offset of the next byte written. It is used for
/// alignment and to compute `DW_EH_PE_pcrel` pointers.
fn len(&self) -> usize;
/// Write a slice.
///
/// # Errors
///
/// Returns an error if the writer cannot store the bytes.
fn write(&mut self, bytes: &[u8]) -> Result<()>;
/// Write an encoded pointer to `address`.
///
/// A constant address is converted to the encoding's application by
/// subtracting the base address from `bases` (for `DW_EH_PE_pcrel`, the
/// address of the field itself, from `bases.section` and
/// [`len`](Self::len)). Address 0 is written as 0 whatever the
/// application, since that is how a null pointer is read. The indirect
/// flag does not change what is written: `address` is then the address of
/// the pointer to the real value. A `DW_EH_PE_aligned` pointer is
/// preceded by zero padding up to the address size.
///
/// If the writer supports relocations, then it must provide its own
/// implementation of this method.
///
/// # Errors
///
/// Returns [`Error::InvalidEncoding`] for an undefined or omitted
/// encoding, [`Error::MissingBaseAddress`] if the base for the
/// application is not set, [`Error::PointerOutOfRange`] if the value does
/// not fit in the format, and [`Error::InvalidAddress`] for a symbol if the
/// writer does not support relocations.
fn write_pointer(
&mut self,
encoding: Encoding,
address: Address,
format: Format,
bases: &BaseAddresses,
) -> Result<()> {
match address {
Address::Constant(value) => {
write_constant_pointer(self, encoding, value, format, bases)
}
Address::Symbol { .. } => Err(Error::InvalidAddress),
}
}
/// Write a reference to a `type_info` object: a type table entry or an
/// Arm EHABI exception specification entry.
///
/// The default implementation calls
/// [`write_pointer`](Self::write_pointer). A writer that records
/// relocations uses this to emit `R_ARM_TARGET2` for the Arm EHABI.
///
/// # Errors
///
/// Returns the errors of [`write_pointer`](Self::write_pointer).
fn write_type_info(
&mut self,
encoding: Encoding,
address: Address,
format: Format,
bases: &BaseAddresses,
) -> Result<()> {
self.write_pointer(encoding, address, format, bases)
}
/// Write a u8.
///
/// # Errors
///
/// Returns the errors of [`write`](Self::write).
fn write_u8(&mut self, val: u8) -> Result<()> {
self.write(&[val])
}
/// Write a u16.
///
/// # Errors
///
/// Returns the errors of [`write`](Self::write).
fn write_u16(&mut self, val: u16) -> Result<()> {
let mut bytes = [0; 2];
self.endian().write_u16(&mut bytes, val);
self.write(&bytes)
}
/// Write a u32.
///
/// # Errors
///
/// Returns the errors of [`write`](Self::write).
fn write_u32(&mut self, val: u32) -> Result<()> {
let mut bytes = [0; 4];
self.endian().write_u32(&mut bytes, val);
self.write(&bytes)
}
/// Write a u64.
///
/// # Errors
///
/// Returns the errors of [`write`](Self::write).
fn write_u64(&mut self, val: u64) -> Result<()> {
let mut bytes = [0; 8];
self.endian().write_u64(&mut bytes, val);
self.write(&bytes)
}
/// Write the low `size` bytes (1, 2, 4 or 8) of `val`.
///
/// # Errors
///
/// Returns [`Error::UnsupportedAddressSize`] for any other size, and the
/// errors of [`write`](Self::write).
fn write_sized(&mut self, val: u64, size: u8) -> Result<()> {
match size {
1 => self.write_u8(val as u8),
2 => self.write_u16(val as u16),
4 => self.write_u32(val as u32),
8 => self.write_u64(val),
_ => Err(Error::UnsupportedAddressSize(size)),
}
}
/// Write an unsigned LEB128 value in at least `len` bytes; 0 writes the
/// minimal encoding.
///
/// # Errors
///
/// Returns the errors of [`write`](Self::write).
fn write_uleb128(&mut self, val: u64, len: usize) -> Result<()> {
write_bytes(self, |out| leb128::write_unsigned_padded(val, len, out))
}
/// Write a signed LEB128 value in at least `len` bytes; 0 writes the
/// minimal encoding.
///
/// # Errors
///
/// Returns the errors of [`write`](Self::write).
fn write_sleb128(&mut self, val: i64, len: usize) -> Result<()> {
write_bytes(self, |out| leb128::write_signed_padded(val, len, out))
}
/// Write zero bytes until the length is a multiple of `alignment`.
///
/// # Errors
///
/// Returns [`Error::InvalidAlignment`] if `alignment` is not a power of
/// two, and the errors of [`write`](Self::write).
fn align(&mut self, alignment: u32) -> Result<()> {
let padding = padding(self.len(), check_alignment(alignment)?);
for _ in 0..padding {
self.write_u8(0)?;
}
Ok(())
}
}
/// Write the bytes produced by `encode`, stopping at the first error.
fn write_bytes<W: Writer + ?Sized>(
w: &mut W,
encode: impl FnOnce(&mut dyn FnMut(u8)),
) -> Result<()> {
let mut result = Ok(());
encode(&mut |byte| {
if result.is_ok() {
result = w.write_u8(byte);
}
});
result
}
/// Return `alignment` as a `usize`, or an error if it is not a power of two.
pub(crate) fn check_alignment(alignment: u32) -> Result<usize> {
if alignment.is_power_of_two() {
Ok(alignment as usize)
} else {
Err(Error::InvalidAlignment(alignment))
}
}
/// Return the field size of a fixed-size encoding, or `None` for LEB128.
pub(crate) fn field_size(encoding: Encoding, format: Format) -> Result<Option<u8>> {
if !encoding.is_valid() {
return Err(Error::InvalidEncoding(encoding));
}
if !format.has_valid_address_size() {
return Err(Error::UnsupportedAddressSize(format.address_size));
}
Ok(encoding.size(format.address_size))
}
/// Return the value that reading the field of `raw` written in `encoding`
/// (of `size` bytes, or LEB128) gives back.
pub(crate) fn read_back(encoding: Encoding, raw: u64, size: Option<u8>) -> u64 {
match size {
Some(size) if encoding.is_signed() => sign_extend(raw & size_mask(size), size),
Some(size) => raw & size_mask(size),
None => raw,
}
}
/// Write a pointer to a constant address.
pub(crate) fn write_constant_pointer<W: Writer + ?Sized>(
w: &mut W,
encoding: Encoding,
value: u64,
format: Format,
bases: &BaseAddresses,
) -> Result<()> {
let size = field_size(encoding, format)?;
if encoding.application() == DW_EH_PE_ALIGNED {
w.align(u32::from(format.address_size))?;
}
let mask = format.address_mask();
if value & !mask != 0 {
return Err(Error::PointerOutOfRange(value));
}
if value == 0 {
return write_raw(w, encoding, 0, size, 0);
}
let base = bases
.relative_base(encoding, w.len())
.map_err(Error::MissingBaseAddress)?;
let mut raw = value.wrapping_sub(base) & mask;
if encoding.format() == DW_EH_PE_SLEB128 {
// Keep negative offsets short.
raw = sign_extend(raw, format.address_size);
}
// Check that reading the field back gives the same address, and is not
// mistaken for a null pointer.
let read_back = read_back(encoding, raw, size);
if read_back & mask == 0 || read_back.wrapping_add(base) & mask != value {
return Err(Error::PointerOutOfRange(value));
}
write_raw(w, encoding, raw, size, 0)
}
/// Write the raw field value of a pointer, in at least `leb128_len` bytes if
/// it is LEB128-encoded.
pub(crate) fn write_raw<W: Writer + ?Sized>(
w: &mut W,
encoding: Encoding,
raw: u64,
size: Option<u8>,
leb128_len: usize,
) -> Result<()> {
match size {
Some(size) => w.write_sized(raw, size),
None if encoding.format() == DW_EH_PE_SLEB128 => {
w.write_sleb128(raw.cast_signed(), leb128_len)
}
None => w.write_uleb128(raw, leb128_len),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Base;
use crate::endianity::LittleEndian;
use crate::write::EndianVec;
fn write(
encoding: u8,
address: u64,
prefix: usize,
format: Format,
) -> Result<alloc::vec::Vec<u8>> {
let mut w = EndianVec::new(LittleEndian);
w.write(&alloc::vec![0xaa; prefix])?;
let bases = BaseAddresses::default().set_section(0x1000).set_text(0x400);
w.write_pointer(
Encoding(encoding),
Address::Constant(address),
format,
&bases,
)?;
Ok(w.into_vec().split_off(prefix))
}
#[test]
fn test_write_pointer() {
let f8 = Format::new(8);
let f4 = Format::new(4);
assert_eq!(
write(0x00, 0x1234, 0, f4),
Ok(alloc::vec![0x34, 0x12, 0, 0])
);
assert_eq!(write(0x00, 0x1234, 0, f8).unwrap().len(), 8);
// pcrel from 0x1004 to 0x1000.
assert_eq!(
write(0x1b, 0x1000, 4, f8),
Ok(alloc::vec![0xfc, 0xff, 0xff, 0xff])
);
assert_eq!(
write(0x9b, 0x2000, 0, f8),
Ok(alloc::vec![0x00, 0x10, 0, 0])
);
// udata4 pcrel backwards only works with 4-byte addresses.
assert_eq!(
write(0x13, 0x1000, 4, f4),
Ok(alloc::vec![0xfc, 0xff, 0xff, 0xff])
);
assert_eq!(
write(0x13, 0x1000, 4, f8),
Err(Error::PointerOutOfRange(0x1000))
);
assert_eq!(
write(0x02, 0x1_0000, 0, f8),
Err(Error::PointerOutOfRange(0x1_0000))
);
// Addresses wider than the address size are not truncated.
assert_eq!(
write(0x00, 0x1_0000_0000, 0, f4),
Err(Error::PointerOutOfRange(0x1_0000_0000))
);
assert_eq!(
write(0x00, 0x1_0000_1000, 0, f4),
Err(Error::PointerOutOfRange(0x1_0000_1000))
);
assert_eq!(write(0x21, 0x480, 0, f8), Ok(alloc::vec![0x80, 0x01]));
assert_eq!(write(0x19, 0xff0, 0, f8), Ok(alloc::vec![0x70]));
assert_eq!(write(0x19, 0xff0, 0, f4), Ok(alloc::vec![0x70]));
// Null stays null.
assert_eq!(write(0x1b, 0, 4, f8), Ok(alloc::vec![0, 0, 0, 0]));
// A non-null pointer that would read back as null.
assert_eq!(
write(0x1b, 0x1004, 4, f8),
Err(Error::PointerOutOfRange(0x1004))
);
// Aligned pointers are padded first.
assert_eq!(
write(0x50, 0x10, 1, f4),
Ok(alloc::vec![0, 0, 0, 0x10, 0, 0, 0])
);
assert_eq!(
write(0x33, 0x10, 0, f8),
Err(Error::MissingBaseAddress(Base::Data))
);
assert_eq!(
write(0xff, 0x10, 0, f8),
Err(Error::InvalidEncoding(Encoding::OMIT))
);
assert_eq!(
write(0x00, 0x10, 0, Format::new(3)),
Err(Error::UnsupportedAddressSize(3))
);
let mut w = EndianVec::new(LittleEndian);
assert_eq!(
w.write_pointer(
Encoding::ABSPTR,
Address::Symbol {
symbol: 0,
addend: 0
},
f8,
&BaseAddresses::default()
),
Err(Error::InvalidAddress)
);
}
#[test]
fn test_leb128_and_align() {
let mut w = EndianVec::new(LittleEndian);
w.write_uleb128(2, 3).unwrap();
w.write_sleb128(-2, 0).unwrap();
w.align(4).unwrap();
w.align(1).unwrap();
assert_eq!(w.slice(), [0x82, 0x80, 0x00, 0x7e]);
w.write_u8(1).unwrap();
w.align(8).unwrap();
assert_eq!(w.len(), 8);
assert_eq!(w.align(3), Err(Error::InvalidAlignment(3)));
w.write_uleb128(1, 20).unwrap();
assert_eq!(w.len(), 28);
}
}