fixed parsing of r_info field
authorEli Bendersky <eliben@gmail.com>
Fri, 23 Sep 2011 03:06:47 +0000 (06:06 +0300)
committerEli Bendersky <eliben@gmail.com>
Fri, 23 Sep 2011 03:06:47 +0000 (06:06 +0300)
elftools/elf/structs.py
scripts/readelf.py

index a8d34976d02d0dd629865b0663354c492b57cbb6..d2d6e691d924602e77f40090156901a461c0fa67 100644 (file)
@@ -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'),
         )
 
index 46caa1173b2916a5fd42d1c413a66a623fffefe5..264f25c131b7acede429ca8c894f0446246b26ed 100755 (executable)
@@ -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: