@@ -34,11 +34,36 @@ pub enum DataType {
34
34
String ,
35
35
}
36
36
37
+ impl std:: fmt:: Display for DataType {
38
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
39
+ match self {
40
+ DataType :: Int8 => write ! ( f, "Int8" ) ,
41
+ DataType :: Int16 => write ! ( f, "Int16" ) ,
42
+ DataType :: Int32 => write ! ( f, "Int32" ) ,
43
+ DataType :: Int64 => write ! ( f, "Int64" ) ,
44
+ DataType :: Int128 => write ! ( f, "Int128" ) ,
45
+ DataType :: Float => write ! ( f, "Float" ) ,
46
+ DataType :: Double => write ! ( f, "Double" ) ,
47
+ DataType :: Bytes => write ! ( f, "Bytes" ) ,
48
+ DataType :: String => write ! ( f, "String" ) ,
49
+ }
50
+ }
51
+ }
52
+
37
53
impl DataType {
38
- pub fn display_bytes < Endian : ByteOrder > ( & self , bytes : & [ u8 ] ) -> Option < String > {
54
+ pub fn display_labels < Endian : ByteOrder > ( & self , bytes : & [ u8 ] ) -> Vec < String > {
55
+ let mut strs = Vec :: new ( ) ;
56
+ for literal in self . display_literals :: < Endian > ( bytes) {
57
+ strs. push ( format ! ( "{}: {}" , self , literal) )
58
+ }
59
+ strs
60
+ }
61
+
62
+ pub fn display_literals < Endian : ByteOrder > ( & self , bytes : & [ u8 ] ) -> Vec < String > {
63
+ let mut strs = Vec :: new ( ) ;
39
64
if self . required_len ( ) . is_some_and ( |l| bytes. len ( ) < l) {
40
65
log:: warn!( "Failed to display a symbol value for a symbol whose size is too small for instruction referencing it." ) ;
41
- return None ;
66
+ return strs ;
42
67
}
43
68
let mut bytes = bytes;
44
69
if self . required_len ( ) . is_some_and ( |l| bytes. len ( ) > l) {
@@ -56,58 +81,61 @@ impl DataType {
56
81
match self {
57
82
DataType :: Int8 => {
58
83
let i = i8:: from_ne_bytes ( bytes. try_into ( ) . unwrap ( ) ) ;
84
+ strs. push ( format ! ( "{:#x}" , i) ) ;
85
+
59
86
if i < 0 {
60
- format ! ( "Int8: {:#x} ({:#x})" , i, ReallySigned ( i) )
61
- } else {
62
- format ! ( "Int8: {:#x}" , i)
87
+ strs. push ( format ! ( "{:#x}" , ReallySigned ( i) ) ) ;
63
88
}
64
89
}
65
90
DataType :: Int16 => {
66
91
let i = Endian :: read_i16 ( bytes) ;
92
+ strs. push ( format ! ( "{:#x}" , i) ) ;
93
+
67
94
if i < 0 {
68
- format ! ( "Int16: {:#x} ({:#x})" , i, ReallySigned ( i) )
69
- } else {
70
- format ! ( "Int16: {:#x}" , i)
95
+ strs. push ( format ! ( "{:#x}" , ReallySigned ( i) ) ) ;
71
96
}
72
97
}
73
98
DataType :: Int32 => {
74
99
let i = Endian :: read_i32 ( bytes) ;
100
+ strs. push ( format ! ( "{:#x}" , i) ) ;
101
+
75
102
if i < 0 {
76
- format ! ( "Int32: {:#x} ({:#x})" , i, ReallySigned ( i) )
77
- } else {
78
- format ! ( "Int32: {:#x}" , i)
103
+ strs. push ( format ! ( "{:#x}" , ReallySigned ( i) ) ) ;
79
104
}
80
105
}
81
106
DataType :: Int64 => {
82
107
let i = Endian :: read_i64 ( bytes) ;
108
+ strs. push ( format ! ( "{:#x}" , i) ) ;
109
+
83
110
if i < 0 {
84
- format ! ( "Int64: {:#x} ({:#x})" , i, ReallySigned ( i) )
85
- } else {
86
- format ! ( "Int64: {:#x}" , i)
111
+ strs. push ( format ! ( "{:#x}" , ReallySigned ( i) ) ) ;
87
112
}
88
113
}
89
114
DataType :: Int128 => {
90
115
let i = Endian :: read_i128 ( bytes) ;
116
+ strs. push ( format ! ( "{:#x}" , i) ) ;
117
+
91
118
if i < 0 {
92
- format ! ( "Int128: {:#x} ({:#x})" , i, ReallySigned ( i) )
93
- } else {
94
- format ! ( "Int128: {:#x}" , i)
119
+ strs. push ( format ! ( "{:#x}" , ReallySigned ( i) ) ) ;
95
120
}
96
121
}
97
122
DataType :: Float => {
98
- format ! ( "Float: {:?}f" , Endian :: read_f32( bytes) )
123
+ strs . push ( format ! ( "{:?}f" , Endian :: read_f32( bytes) ) ) ;
99
124
}
100
125
DataType :: Double => {
101
- format ! ( "Double: {:?}" , Endian :: read_f64( bytes) )
126
+ strs . push ( format ! ( "{:?}" , Endian :: read_f64( bytes) ) ) ;
102
127
}
103
128
DataType :: Bytes => {
104
- format ! ( "Bytes: {:#?}" , bytes)
129
+ strs . push ( format ! ( "{:#?}" , bytes) ) ;
105
130
}
106
131
DataType :: String => {
107
- format ! ( "String: {:?}" , CStr :: from_bytes_until_nul( bytes) . ok( ) ?)
132
+ if let Ok ( cstr) = CStr :: from_bytes_until_nul ( bytes) {
133
+ strs. push ( format ! ( "{:?}" , cstr) ) ;
134
+ }
108
135
}
109
136
}
110
- . into ( )
137
+
138
+ strs
111
139
}
112
140
113
141
fn required_len ( & self ) -> Option < usize > {
@@ -154,19 +182,42 @@ pub trait ObjArch: Send + Sync {
154
182
155
183
fn guess_data_type ( & self , _instruction : & ObjIns ) -> Option < DataType > { None }
156
184
157
- fn display_data_type ( & self , _ty : DataType , bytes : & [ u8 ] ) -> Option < String > {
158
- Some ( format ! ( "Bytes: {:#x?}" , bytes) )
185
+ fn display_data_labels ( & self , _ty : DataType , bytes : & [ u8 ] ) -> Vec < String > {
186
+ vec ! [ format!( "Bytes: {:#x?}" , bytes) ]
187
+ }
188
+
189
+ fn display_data_literals ( & self , _ty : DataType , bytes : & [ u8 ] ) -> Vec < String > {
190
+ vec ! [ format!( "{:#?}" , bytes) ]
191
+ }
192
+
193
+ fn display_ins_data_labels ( & self , ins : & ObjIns ) -> Vec < String > {
194
+ let Some ( reloc) = ins. reloc . as_ref ( ) else {
195
+ return Vec :: new ( ) ;
196
+ } ;
197
+ if reloc. addend >= 0 && reloc. target . bytes . len ( ) > reloc. addend as usize {
198
+ return self
199
+ . guess_data_type ( ins)
200
+ . map ( |ty| {
201
+ self . display_data_labels ( ty, & reloc. target . bytes [ reloc. addend as usize ..] )
202
+ } )
203
+ . unwrap_or_default ( ) ;
204
+ }
205
+ Vec :: new ( )
159
206
}
160
207
161
- fn display_ins_data ( & self , ins : & ObjIns ) -> Option < String > {
162
- let reloc = ins. reloc . as_ref ( ) ?;
208
+ fn display_ins_data_literals ( & self , ins : & ObjIns ) -> Vec < String > {
209
+ let Some ( reloc) = ins. reloc . as_ref ( ) else {
210
+ return Vec :: new ( ) ;
211
+ } ;
163
212
if reloc. addend >= 0 && reloc. target . bytes . len ( ) > reloc. addend as usize {
164
- self . guess_data_type ( ins) . and_then ( |ty| {
165
- self . display_data_type ( ty, & reloc. target . bytes [ reloc. addend as usize ..] )
166
- } )
167
- } else {
168
- None
213
+ return self
214
+ . guess_data_type ( ins)
215
+ . map ( |ty| {
216
+ self . display_data_literals ( ty, & reloc. target . bytes [ reloc. addend as usize ..] )
217
+ } )
218
+ . unwrap_or_default ( ) ;
169
219
}
220
+ Vec :: new ( )
170
221
}
171
222
172
223
// Downcast methods
0 commit comments