Skip to content

Commit e9762e2

Browse files
authored
Add support for x86 ELF object files (#213)
1 parent dab79d9 commit e9762e2

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

objdiff-core/src/arch/x86.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use iced_x86::{
66
Decoder, DecoderOptions, DecoratorKind, FormatterOutput, FormatterTextKind, GasFormatter,
77
Instruction, IntelFormatter, MasmFormatter, NasmFormatter, NumberKind, OpKind, Register,
88
};
9-
use object::{Endian as _, Object as _, ObjectSection as _, pe};
9+
use object::{Endian as _, Object as _, ObjectSection as _, elf, pe};
1010

1111
use crate::{
1212
arch::Arch,
@@ -67,15 +67,23 @@ impl ArchX86 {
6767
pe::IMAGE_REL_I386_DIR32 | pe::IMAGE_REL_I386_REL32 => Some(4),
6868
_ => None,
6969
},
70-
_ => None,
70+
RelocationFlags::Elf(typ) => match typ {
71+
elf::R_386_32 | elf::R_386_PC32 => Some(4),
72+
elf::R_386_16 => Some(2),
73+
_ => None,
74+
},
7175
},
7276
Architecture::X86_64 => match flags {
7377
RelocationFlags::Coff(typ) => match typ {
7478
pe::IMAGE_REL_AMD64_ADDR32NB | pe::IMAGE_REL_AMD64_REL32 => Some(4),
7579
pe::IMAGE_REL_AMD64_ADDR64 => Some(8),
7680
_ => None,
7781
},
78-
_ => None,
82+
RelocationFlags::Elf(typ) => match typ {
83+
elf::R_X86_64_PC32 => Some(4),
84+
elf::R_X86_64_64 => Some(8),
85+
_ => None,
86+
},
7987
},
8088
}
8189
}
@@ -227,20 +235,23 @@ impl Arch for ArchX86 {
227235
) -> Result<i64> {
228236
match self.arch {
229237
Architecture::X86 => match flags {
230-
RelocationFlags::Coff(pe::IMAGE_REL_I386_DIR32 | pe::IMAGE_REL_I386_REL32) => {
238+
RelocationFlags::Coff(pe::IMAGE_REL_I386_DIR32 | pe::IMAGE_REL_I386_REL32)
239+
| RelocationFlags::Elf(elf::R_386_32 | elf::R_386_PC32) => {
231240
let data =
232241
section.data()?[address as usize..address as usize + 4].try_into()?;
233242
Ok(self.endianness.read_i32_bytes(data) as i64)
234243
}
235244
flags => bail!("Unsupported x86 implicit relocation {flags:?}"),
236245
},
237246
Architecture::X86_64 => match flags {
238-
RelocationFlags::Coff(pe::IMAGE_REL_AMD64_ADDR32NB | pe::IMAGE_REL_AMD64_REL32) => {
247+
RelocationFlags::Coff(pe::IMAGE_REL_AMD64_ADDR32NB | pe::IMAGE_REL_AMD64_REL32)
248+
| RelocationFlags::Elf(elf::R_X86_64_32 | elf::R_X86_64_PC32) => {
239249
let data =
240250
section.data()?[address as usize..address as usize + 4].try_into()?;
241251
Ok(self.endianness.read_i32_bytes(data) as i64)
242252
}
243-
RelocationFlags::Coff(pe::IMAGE_REL_AMD64_ADDR64) => {
253+
RelocationFlags::Coff(pe::IMAGE_REL_AMD64_ADDR64)
254+
| RelocationFlags::Elf(elf::R_X86_64_64) => {
244255
let data =
245256
section.data()?[address as usize..address as usize + 8].try_into()?;
246257
Ok(self.endianness.read_i64_bytes(data))

0 commit comments

Comments
 (0)