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
// Copyright (C) 2025 FZI Forschungszentrum Informatik
// SPDX-License-Identifier: Apache-2.0
//! Binaries containing [`Instruction`]s
//!
//! Tracing requires knowledge about the program being traced. This module
//! defines the [`Binary`] trait used by the [`Tracer`][super::tracer::Tracer]
//! for retrieving [`Instruction`]s as well as a number of types implementing
//! the [`Binary`] trait. These include:
//!
//! * some [basic] [`Binary`]s such as adapters that may be created through
//! free fns such as [`from_fn`] and [`from_segment`] and allow defining
//! [`Binary`]s from a wide range of types supplying data,
//! * [combinators] that allow tracing multiple programs or program parts such
//! as a firmware and an appliction,
//! * modifiers such as [`Offset`] that are usually created through provided fns
//! of the [`Binary`] trait and
//! * feature-dependent [`Binary`]s, e.g. for using [ELF][elf] files as
//! [`Binary`]s.
//!
//! # Combining [`Binary`]s
//!
//! Usually, [`Binary`]s used in [combinators] all need to agree on the
//! [`Binary::Error`] type. Combinators such as [`Multi`] in particular also
//! requires the [`Binary`]s themselves to be of the same type. If the `alloc`
//! feature is enabled, the error type may be erased through the provided method
//! [`Binary::boxed`]. The lifetime of the original [`Binary`] is preserved in
//! the resulting [`BoxedBinary`]. This is relevant when using an [`elf::Elf`]
//! or when...
//!
//! # Sharing [`Binary`]s between [`Tracer`] instances
//!
//! [`Binary`]s are intended for use by a single [`Tracer`] and can not be
//! easily shared between instances. They may be mutated when fetching an
//! [`Instruction`], e.g. for caching purposes. For example, a [`Multi`] will
//! remember the [`Binary`] it chooses and pick that particular one first the
//! next time.
//!
//! Sharing a [`Binary`] between [`Tracer`]s by placing them behind a mutex of
//! some kind defeates the caching, incurs considerable overhead and is highly
//! discouraged. Instead, users should consider sharing the data backing the
//! [`Binary`]s. For example, a [`basic::Segment`] may be created from a shared
//! buffer or [`Arc`][alloc::sync::Arc] and then cloned.
//!
//! # Example
//!
//! The following constructs a [`Binary`] from a firmware image and a bootrom
//! and clones it for use by a second [`Tracer`] instance.
//!
//! ```
//! use riscv_etrace::binary::{self, Binary, Multi};
//! use riscv_etrace::instruction::base;
//!
//! # let bootrom = b"\x97\x02\x00\x00\x93\x85\x02\x02\x73\x25\x40\xf1\x83\xb2\x82\x01\x67\x80\x02\x00";
//! # let firmware = b"\x97\x02\x00\x00\x93\x82\x02\x00\x73\xa0\x52\x30\x73\x00\x50\x10\x6f\xf0\xdf\xff";
//! let binary1 = Multi::new([
//! binary::from_segment(bootrom, base::Set::Rv32I).with_offset(0x1000),
//! binary::from_segment(firmware, base::Set::Rv32I).with_offset(0x80000000),
//! ]);
//! let binary2 = binary1.clone();
//! ```
//!
//! [`Tracer`]: [super::tracer::Tracer]
use Box;
pub use ;
pub use Multi;
use crate;
use Miss;
use Info;
/// A binary of some sort that contains [`Instruction`]s
///
/// See the [module level][self] documentation for more details.
/// [`Binary`] implementation for a tuple of two binaries
///
/// This impl allows combining [`Binary`]s as long as they agree on their error
/// type. If the first [`Binary`] returns a "miss", the second one is consulted.
/// [`Binary`] moved by a fixed offset
///
/// Accesses will be mapped by subtracting the fixed offset from the address.
/// Accesses to addresses lower than the offset will result in a [miss][Miss].
/// a [`Binary`] boxed for dynamic dispatch
pub type BoxedBinary<'a, I> = ;