MCPcopy Create free account
hub / github.com/qilingframework/qiling / get_elfdata_mapping

Method get_elfdata_mapping

qiling/loader/elf.py:657–703  ·  view source on GitHub ↗
(self, elffile: ELFFile)

Source from the content-addressed store, hash-verified

655 self.import_symbols[self.ql.os.hook_addr + 2 * self.ql.arch.pointersize] = hook_sys_open
656
657 def get_elfdata_mapping(self, elffile: ELFFile) -> bytes:
658 # from io import BytesIO
659 #
660 # rh = RelocationHandler(elffile)
661 #
662 # for sec in elffile.iter_sections():
663 # rs = rh.find_relocations_for_section(sec)
664 #
665 # if rs is not None:
666 # ss = BytesIO(sec.data())
667 # rh.apply_section_relocations(ss, rs)
668 #
669 # # apply changes to stream
670 # elffile.stream.seek(sec['sh_offset'])
671 # elffile.stream.write(ss.getbuffer())
672 #
673 # TODO: need to patch hooked symbols with their hook targets
674 # (e.g. replace calls to 'printk' with the hooked address that
675 # was allocate for it)
676
677 elfdata_mapping = bytearray()
678
679 # pick up elf header
680 with preserve_stream_pos(elffile.stream):
681 elffile.stream.seek(0)
682 elf_header = elffile.stream.read(elffile['e_ehsize'])
683
684 elfdata_mapping.extend(elf_header)
685
686 # FIXME: normally the address of a section would be determined by its 'sh_addr' value.
687 # in case of a relocatable object all its sections' sh_addr will be set to zero, so
688 # the value in 'sh_offset' should be used to determine the final address.
689 # see: https://refspecs.linuxbase.org/elf/gabi4+/ch4.sheader.html
690 #
691 # here we presume this a relocatable object and don't do any relocation (that is, it
692 # is relocated to 0)
693
694 # pick up loadable sections
695 for sec in elffile.iter_sections():
696 if sec['sh_flags'] & SH_FLAGS.SHF_ALLOC:
697 # pad aggregated elf data to the offset of the current section
698 elfdata_mapping.extend(b'\x00' * (sec['sh_offset'] - len(elfdata_mapping)))
699
700 # aggregate section data
701 elfdata_mapping.extend(sec.data())
702
703 return bytes(elfdata_mapping)
704
705 def save(self) -> Mapping[str, Any]:
706 saved = super().save()

Callers 1

load_driverMethod · 0.95

Calls 2

seekMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected