Skip to content

Commit 8250d26

Browse files
committed
Support R_MIPS_LITERAL, R_MIPS15_S3 relocations
Resolves #92 Resolves #95
1 parent fd555a6 commit 8250d26

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

objdiff-core/src/arch/mips.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const EF_MIPS_MACH: u32 = 0x00FF0000;
3434
const EF_MIPS_MACH_ALLEGREX: u32 = 0x00840000;
3535
const EF_MIPS_MACH_5900: u32 = 0x00920000;
3636

37+
const R_MIPS15_S3: u32 = 119;
38+
3739
impl ObjArchMips {
3840
pub fn new(object: &File) -> Result<Self> {
3941
let mut abi = Abi::NUMERIC;
@@ -169,6 +171,18 @@ impl ObjArch for ObjArchMips {
169171
)));
170172
args.push(ObjInsArg::PlainText(")".into()));
171173
}
174+
// OperandType::r5900_immediate15 => match reloc {
175+
// Some(reloc)
176+
// if reloc.flags == RelocationFlags::Elf { r_type: R_MIPS15_S3 } =>
177+
// {
178+
// push_reloc(&mut args, reloc)?;
179+
// }
180+
// _ => {
181+
// args.push(ObjInsArg::Arg(ObjInsArgValue::Opaque(
182+
// op.disassemble(&instruction, None).into(),
183+
// )));
184+
// }
185+
// },
172186
_ => {
173187
args.push(ObjInsArg::Arg(ObjInsArgValue::Opaque(
174188
op.disassemble(&instruction, None).into(),
@@ -205,13 +219,14 @@ impl ObjArch for ObjArchMips {
205219
let addend = self.endianness.read_u32_bytes(data);
206220
Ok(match reloc.flags() {
207221
RelocationFlags::Elf { r_type: elf::R_MIPS_32 } => addend as i64,
222+
RelocationFlags::Elf { r_type: elf::R_MIPS_26 } => ((addend & 0x03FFFFFF) << 2) as i64,
208223
RelocationFlags::Elf { r_type: elf::R_MIPS_HI16 } => {
209224
((addend & 0x0000FFFF) << 16) as i32 as i64
210225
}
211226
RelocationFlags::Elf {
212227
r_type: elf::R_MIPS_LO16 | elf::R_MIPS_GOT16 | elf::R_MIPS_CALL16,
213228
} => (addend & 0x0000FFFF) as i16 as i64,
214-
RelocationFlags::Elf { r_type: elf::R_MIPS_GPREL16 } => {
229+
RelocationFlags::Elf { r_type: elf::R_MIPS_GPREL16 | elf::R_MIPS_LITERAL } => {
215230
let RelocationTarget::Symbol(idx) = reloc.target() else {
216231
bail!("Unsupported R_MIPS_GPREL16 relocation against a non-symbol");
217232
};
@@ -225,23 +240,25 @@ impl ObjArch for ObjArchMips {
225240
(addend & 0x0000FFFF) as i16 as i64
226241
}
227242
}
228-
RelocationFlags::Elf { r_type: elf::R_MIPS_26 } => ((addend & 0x03FFFFFF) << 2) as i64,
229243
RelocationFlags::Elf { r_type: elf::R_MIPS_PC16 } => 0, // PC-relative relocation
244+
RelocationFlags::Elf { r_type: R_MIPS15_S3 } => ((addend & 0x001FFFC0) >> 3) as i64,
230245
flags => bail!("Unsupported MIPS implicit relocation {flags:?}"),
231246
})
232247
}
233248

234249
fn display_reloc(&self, flags: RelocationFlags) -> Cow<'static, str> {
235250
match flags {
236251
RelocationFlags::Elf { r_type } => match r_type {
252+
elf::R_MIPS_32 => Cow::Borrowed("R_MIPS_32"),
253+
elf::R_MIPS_26 => Cow::Borrowed("R_MIPS_26"),
237254
elf::R_MIPS_HI16 => Cow::Borrowed("R_MIPS_HI16"),
238255
elf::R_MIPS_LO16 => Cow::Borrowed("R_MIPS_LO16"),
256+
elf::R_MIPS_GPREL16 => Cow::Borrowed("R_MIPS_GPREL16"),
257+
elf::R_MIPS_LITERAL => Cow::Borrowed("R_MIPS_LITERAL"),
239258
elf::R_MIPS_GOT16 => Cow::Borrowed("R_MIPS_GOT16"),
240259
elf::R_MIPS_PC16 => Cow::Borrowed("R_MIPS_PC16"),
241260
elf::R_MIPS_CALL16 => Cow::Borrowed("R_MIPS_CALL16"),
242-
elf::R_MIPS_GPREL16 => Cow::Borrowed("R_MIPS_GPREL16"),
243-
elf::R_MIPS_32 => Cow::Borrowed("R_MIPS_32"),
244-
elf::R_MIPS_26 => Cow::Borrowed("R_MIPS_26"),
261+
R_MIPS15_S3 => Cow::Borrowed("R_MIPS15_S3"),
245262
_ => Cow::Owned(format!("<{flags:?}>")),
246263
},
247264
_ => Cow::Owned(format!("<{flags:?}>")),
@@ -277,7 +294,11 @@ fn push_reloc(args: &mut Vec<ObjInsArg>, reloc: &ObjReloc) -> Result<()> {
277294
args.push(ObjInsArg::Reloc);
278295
args.push(ObjInsArg::PlainText(")".into()));
279296
}
280-
elf::R_MIPS_32 | elf::R_MIPS_26 | elf::R_MIPS_PC16 => {
297+
elf::R_MIPS_32
298+
| elf::R_MIPS_26
299+
| elf::R_MIPS_LITERAL
300+
| elf::R_MIPS_PC16
301+
| R_MIPS15_S3 => {
281302
args.push(ObjInsArg::Reloc);
282303
}
283304
_ => bail!("Unsupported ELF MIPS relocation type {r_type}"),

0 commit comments

Comments
 (0)