pyelftools icon indicating copy to clipboard operation
pyelftools copied to clipboard

Unsupported relocation type

Open tyb0807 opened this issue 8 years ago • 2 comments

Hi all,

I'm trying to understand how relocation works in ELF file. When I test the elf.RelocationHandler.apply_section_relocations for section .rela.dyn of sample_exe64.elf program (in examples folder), it gave me elftools.common.exceptions.ELFRelocationError: Unsupported relocation type: 6.

Tracing down the bug, I've found that the recipe for that .rela.dyn was R_X86_64_GLOB_DAT. Is it intentional that this recipe, as well as many other ones, are not supported? What can be the reason for it?

tyb0807 avatar May 10 '17 15:05 tyb0807

Probably not intentional. Feel free to send a PR to fix this

eliben avatar May 12 '17 12:05 eliben

Oh, and another point. Here's the code of apply_section_relocations:

for reloc in reloc_section.iter_relocations():
            self._do_apply_relocation(stream, reloc, symtab)

Then in _do_apply_relocation:

 relocated_value = recipe.calc_func(
            value=original_value,
            sym_value=sym_value,
            offset=reloc['r_offset'],
            addend=reloc['r_addend'] if recipe.has_addend else 0)

We see that offset is directly the value from r_offset member of the relocation entry. However, from the ELF format doc: This member gives the location at which to apply the relocation action. For a relocatable file, the value is the byte offset from the beginning of the section to the storage unit affected by the relocation.

I mean, offset should be either:

  • directly reloc['r_offset'] (for executable and shared object files), or
  • reloc['r_offset'] + reloc_section.elffile.get_section(reloc_section['sh_info']), since, according to the doc, sh_info field of sections of type SHT_REL(A) type holds the section header index of the section to which the relocation apply (for relocatable files).

Am I missing something here?

tyb0807 avatar May 12 '17 12:05 tyb0807