From 7fafd13e6bb869810d96974ab92a9c815b2bc650 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Fri, 23 Sep 2011 06:06:47 +0300 Subject: [PATCH] fixed parsing of r_info field --- elftools/elf/structs.py | 36 ++++++++++++++++-------------------- scripts/readelf.py | 2 +- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/elftools/elf/structs.py b/elftools/elf/structs.py index a8d3497..d2d6e69 100644 --- a/elftools/elf/structs.py +++ b/elftools/elf/structs.py @@ -136,34 +136,30 @@ class ELFStructs(object): ) def _create_rel(self): - # r_info is hierarchical. To access the type, use - # container['r_info']['type'] + # r_info is also taken apart into r_info_sym and r_info_type. + # This is done in Value to avoid endianity issues while parsing. if self.elfclass == 32: - r_info_struct = BitStruct('r_info', - BitField('sym', 24), - BitField('type', 8)) + r_info_sym = Value('r_info_sym', + lambda ctx: (ctx['r_info'] >> 8) & 0xFFFFFF) + r_info_type = Value('r_info_type', + lambda ctx: ctx['r_info'] & 0xFF) else: # 64 - r_info_struct = BitStruct('r_info', - BitField('sym', 32), - Padding(24), - BitField('type', 8)) - - # Since the raw value of r_info is also needed, it's being re-computed - # in 'r_info_raw' - r_info_raw = Value('r_info_raw', - lambda ctx: - (ctx['r_info']['sym'] << 8 if self.elfclass == 32 else 32) + - ctx['r_info']['type']) + r_info_sym = Value('r_info_sym', + lambda ctx: (ctx['r_info'] >> 32) & 0xFFFFFFFF) + r_info_type = Value('r_info_type', + lambda ctx: ctx['r_info'] & 0xFFFFFFFF) self.Elf_Rel = Struct('Elf_Rel', self.Elf_addr('r_offset'), - r_info_struct, - r_info_raw, + self.Elf_xword('r_info'), + r_info_sym, + r_info_type, ) self.Elf_Rela = Struct('Elf_Rela', self.Elf_addr('r_offset'), - r_info_struct, - r_info_raw, + self.Elf_xword('r_info'), + r_info_sym, + r_info_type, self.Elf_sxword('r_addend'), ) diff --git a/scripts/readelf.py b/scripts/readelf.py index 46caa11..264f25c 100755 --- a/scripts/readelf.py +++ b/scripts/readelf.py @@ -282,7 +282,7 @@ class ReadElf(object): for rel in section.iter_relocations(): self._emitline('%s %s' % ( self._format_hex(rel['r_offset'], fullhex=True, lead0x=False), - self._format_hex(rel['r_info_raw'], fullhex=True, lead0x=False))) + self._format_hex(rel['r_info'], fullhex=True, lead0x=False))) #print rel, rel.entry if not has_relocation_sections: -- 2.30.2