@@ -5,7 +5,8 @@ use samply_symbols::{
5
5
FileAndPathHelperError , LibraryInfo , SymbolManager ,
6
6
} ;
7
7
use serde_json:: json;
8
- use yaxpeax_arch:: { Arch , DecodeError , Reader , U8Reader } ;
8
+ use yaxpeax_arch:: { Arch , DecodeError , LengthedInstruction , Reader , U8Reader } ;
9
+ use yaxpeax_x86:: amd64:: { Opcode , Operand } ;
9
10
10
11
use crate :: asm:: response_json:: DecodedInstruction ;
11
12
@@ -192,7 +193,8 @@ trait InstructionDecoding: Arch {
192
193
const SYNTAX : & ' static [ & ' static str ] ;
193
194
const ADJUST_BY_AFTER_ERROR : usize ;
194
195
fn make_decoder ( ) -> Self :: Decoder ;
195
- fn stringify_inst ( offset : u32 , inst : Self :: Instruction ) -> DecodedInstruction ;
196
+ fn stringify_inst ( rel_address : u32 , offset : u32 , inst : Self :: Instruction )
197
+ -> DecodedInstruction ;
196
198
}
197
199
198
200
impl InstructionDecoding for yaxpeax_x86:: amd64:: Arch {
@@ -204,15 +206,70 @@ impl InstructionDecoding for yaxpeax_x86::amd64::Arch {
204
206
yaxpeax_x86:: amd64:: InstDecoder :: default ( )
205
207
}
206
208
207
- fn stringify_inst ( offset : u32 , inst : Self :: Instruction ) -> DecodedInstruction {
209
+ fn stringify_inst (
210
+ rel_address : u32 ,
211
+ offset : u32 ,
212
+ inst : Self :: Instruction ,
213
+ ) -> DecodedInstruction {
214
+ let ( mut intel_insn, mut c_insn) = (
215
+ inst. display_with ( yaxpeax_x86:: amd64:: DisplayStyle :: Intel )
216
+ . to_string ( ) ,
217
+ inst. display_with ( yaxpeax_x86:: amd64:: DisplayStyle :: C )
218
+ . to_string ( ) ,
219
+ ) ;
220
+
221
+ fn is_relative_branch ( opcode : Opcode ) -> bool {
222
+ matches ! (
223
+ opcode,
224
+ Opcode :: JMP
225
+ | Opcode :: JRCXZ
226
+ | Opcode :: LOOP
227
+ | Opcode :: LOOPZ
228
+ | Opcode :: LOOPNZ
229
+ | Opcode :: JO
230
+ | Opcode :: JNO
231
+ | Opcode :: JB
232
+ | Opcode :: JNB
233
+ | Opcode :: JZ
234
+ | Opcode :: JNZ
235
+ | Opcode :: JNA
236
+ | Opcode :: JA
237
+ | Opcode :: JS
238
+ | Opcode :: JNS
239
+ | Opcode :: JP
240
+ | Opcode :: JNP
241
+ | Opcode :: JL
242
+ | Opcode :: JGE
243
+ | Opcode :: JLE
244
+ | Opcode :: JG
245
+ )
246
+ }
247
+
248
+ if is_relative_branch ( inst. opcode ( ) ) {
249
+ match inst. operand ( 0 ) {
250
+ Operand :: ImmediateI8 ( rel) => {
251
+ let dest = rel_address as i64
252
+ + offset as i64
253
+ + inst. len ( ) . to_const ( ) as i64
254
+ + rel as i64 ;
255
+ intel_insn = format ! ( "{} 0x{:x}" , inst. opcode( ) , dest) ;
256
+ c_insn = intel_insn. clone ( ) ;
257
+ }
258
+ Operand :: ImmediateI32 ( rel) => {
259
+ let dest = rel_address as i64
260
+ + offset as i64
261
+ + inst. len ( ) . to_const ( ) as i64
262
+ + rel as i64 ;
263
+ intel_insn = format ! ( "{} 0x{:x}" , inst. opcode( ) , dest) ;
264
+ c_insn = intel_insn. clone ( ) ;
265
+ }
266
+ _ => { }
267
+ } ;
268
+ }
269
+
208
270
DecodedInstruction {
209
271
offset,
210
- decoded_string_per_syntax : vec ! [
211
- inst. display_with( yaxpeax_x86:: amd64:: DisplayStyle :: Intel )
212
- . to_string( ) ,
213
- inst. display_with( yaxpeax_x86:: amd64:: DisplayStyle :: C )
214
- . to_string( ) ,
215
- ] ,
272
+ decoded_string_per_syntax : vec ! [ intel_insn, c_insn] ,
216
273
}
217
274
}
218
275
}
@@ -226,7 +283,11 @@ impl InstructionDecoding for yaxpeax_x86::protected_mode::Arch {
226
283
yaxpeax_x86:: protected_mode:: InstDecoder :: default ( )
227
284
}
228
285
229
- fn stringify_inst ( offset : u32 , inst : Self :: Instruction ) -> DecodedInstruction {
286
+ fn stringify_inst (
287
+ _rel_address : u32 ,
288
+ offset : u32 ,
289
+ inst : Self :: Instruction ,
290
+ ) -> DecodedInstruction {
230
291
DecodedInstruction {
231
292
offset,
232
293
decoded_string_per_syntax : vec ! [ inst. to_string( ) ] ,
@@ -243,7 +304,11 @@ impl InstructionDecoding for yaxpeax_arm::armv8::a64::ARMv8 {
243
304
yaxpeax_arm:: armv8:: a64:: InstDecoder :: default ( )
244
305
}
245
306
246
- fn stringify_inst ( offset : u32 , inst : Self :: Instruction ) -> DecodedInstruction {
307
+ fn stringify_inst (
308
+ _rel_address : u32 ,
309
+ offset : u32 ,
310
+ inst : Self :: Instruction ,
311
+ ) -> DecodedInstruction {
247
312
DecodedInstruction {
248
313
offset,
249
314
decoded_string_per_syntax : vec ! [ inst. to_string( ) ] ,
@@ -271,7 +336,11 @@ impl InstructionDecoding for yaxpeax_arm::armv7::ARMv7 {
271
336
yaxpeax_arm:: armv7:: InstDecoder :: default_thumb ( )
272
337
}
273
338
274
- fn stringify_inst ( offset : u32 , inst : Self :: Instruction ) -> DecodedInstruction {
339
+ fn stringify_inst (
340
+ _rel_address : u32 ,
341
+ offset : u32 ,
342
+ inst : Self :: Instruction ,
343
+ ) -> DecodedInstruction {
275
344
DecodedInstruction {
276
345
offset,
277
346
decoded_string_per_syntax : vec ! [ inst. to_string( ) ] ,
@@ -300,7 +369,7 @@ where
300
369
let before = u64:: from ( reader. total_offset ( ) ) as u32 ;
301
370
match decoder. decode ( & mut reader) {
302
371
Ok ( inst) => {
303
- instructions. push ( A :: stringify_inst ( offset, inst) ) ;
372
+ instructions. push ( A :: stringify_inst ( rel_address , offset, inst) ) ;
304
373
let after = u64:: from ( reader. total_offset ( ) ) as u32 ;
305
374
offset += after - before;
306
375
}
0 commit comments