@@ -137,7 +137,7 @@ impl Arch for ArchX86 {
137
137
{
138
138
instruction. set_memory_displacement64 ( PLACEHOLDER ) ;
139
139
// Formatter always writes the displacement as Int32
140
- reloc_replace = Some ( ( OpKind :: Memory , NumberKind :: Int32 , PLACEHOLDER ) ) ;
140
+ reloc_replace = Some ( ( OpKind :: Memory , 4 , PLACEHOLDER ) ) ;
141
141
} else if reloc_offset == offsets. immediate_offset ( ) as u64
142
142
&& reloc_size == offsets. immediate_size ( )
143
143
{
@@ -155,18 +155,12 @@ impl Arch for ArchX86 {
155
155
_ => OpKind :: default ( ) ,
156
156
}
157
157
} ;
158
- let number_kind = match reloc_size {
159
- 2 => NumberKind :: UInt16 ,
160
- 4 => NumberKind :: UInt32 ,
161
- 8 => NumberKind :: UInt64 ,
162
- _ => NumberKind :: default ( ) ,
163
- } ;
164
158
if is_branch {
165
159
instruction. set_near_branch64 ( PLACEHOLDER ) ;
166
160
} else {
167
161
instruction. set_immediate32 ( PLACEHOLDER as u32 ) ;
168
162
}
169
- reloc_replace = Some ( ( op_kind, number_kind , PLACEHOLDER ) ) ;
163
+ reloc_replace = Some ( ( op_kind, reloc_size , PLACEHOLDER ) ) ;
170
164
}
171
165
}
172
166
@@ -251,7 +245,7 @@ impl Arch for ArchX86 {
251
245
252
246
struct InstructionFormatterOutput < ' a > {
253
247
cb : & ' a mut dyn FnMut ( InstructionPart < ' _ > ) -> Result < ( ) > ,
254
- reloc_replace : Option < ( OpKind , NumberKind , u64 ) > ,
248
+ reloc_replace : Option < ( OpKind , usize , u64 ) > ,
255
249
error : Option < anyhow:: Error > ,
256
250
skip_next : bool ,
257
251
}
@@ -326,11 +320,17 @@ impl FormatterOutput for InstructionFormatterOutput<'_> {
326
320
return ;
327
321
}
328
322
329
- if let ( Some ( operand) , Some ( ( target_op_kind, target_number_kind , target_value) ) ) =
323
+ if let ( Some ( operand) , Some ( ( target_op_kind, reloc_size , target_value) ) ) =
330
324
( instruction_operand, self . reloc_replace )
331
325
{
332
326
if instruction. op_kind ( operand) == target_op_kind
333
- && number_kind == target_number_kind
327
+ && match ( number_kind, reloc_size) {
328
+ ( NumberKind :: Int8 | NumberKind :: UInt8 , 1 )
329
+ | ( NumberKind :: Int16 | NumberKind :: UInt16 , 2 )
330
+ | ( NumberKind :: Int32 | NumberKind :: UInt32 , 4 )
331
+ | ( NumberKind :: Int64 | NumberKind :: UInt64 , 8 ) => true ,
332
+ _ => false ,
333
+ }
334
334
&& value == target_value
335
335
{
336
336
if let Err ( e) = ( self . cb ) ( InstructionPart :: reloc ( ) ) {
@@ -571,4 +571,43 @@ mod test {
571
571
. unwrap ( ) ;
572
572
assert_eq ! ( parts, & [ InstructionPart :: opcode( "call" , opcode) , InstructionPart :: reloc( ) ] ) ;
573
573
}
574
+
575
+ #[ test]
576
+ fn test_process_instruction_with_reloc_4 ( ) {
577
+ let arch = ArchX86 { arch : Architecture :: X86 , endianness : object:: Endianness :: Little } ;
578
+ let code = [ 0x8b , 0x15 , 0xa4 , 0x21 , 0x7e , 0x00 ] ;
579
+ let opcode = iced_x86:: Mnemonic :: Mov as u16 ;
580
+ let mut parts = Vec :: new ( ) ;
581
+ arch. display_instruction (
582
+ ResolvedInstructionRef {
583
+ ins_ref : InstructionRef { address : 0x1234 , size : 6 , opcode } ,
584
+ code : & code,
585
+ relocation : Some ( ResolvedRelocation {
586
+ relocation : & Relocation {
587
+ flags : RelocationFlags :: Coff ( pe:: IMAGE_REL_I386_DIR32 ) ,
588
+ address : 0x1234 + 2 ,
589
+ target_symbol : 0 ,
590
+ addend : 0 ,
591
+ } ,
592
+ symbol : & Default :: default ( ) ,
593
+ } ) ,
594
+ ..Default :: default ( )
595
+ } ,
596
+ & DiffObjConfig :: default ( ) ,
597
+ & mut |part| {
598
+ parts. push ( part. into_static ( ) ) ;
599
+ Ok ( ( ) )
600
+ } ,
601
+ )
602
+ . unwrap ( ) ;
603
+ assert_eq ! ( parts, & [
604
+ InstructionPart :: opcode( "mov" , opcode) ,
605
+ InstructionPart :: opaque( "edx" ) ,
606
+ InstructionPart :: basic( "," ) ,
607
+ InstructionPart :: basic( " " ) ,
608
+ InstructionPart :: basic( "[" ) ,
609
+ InstructionPart :: reloc( ) ,
610
+ InstructionPart :: basic( "]" ) ,
611
+ ] ) ;
612
+ }
574
613
}
0 commit comments