Beispiel #1
0
    def __init__(self,
                 typ,
                 off=0,
                 backend=None,
                 parent=None,
                 fieldname='',
                 default_backend=True,
                 force_array_size=None,
                 child_backend=None,
                 byteoff=None):
        super().__init__(handler=self.item_handler,
                         repr_blacklist_keys=['parent'])
        self._typ = typ
        self._base_typ = typ.get_base_typ()
        self._children = OrderedDict()
        if byteoff is not None:
            off = byteoff * 8
        self._off = off  # in bits
        self.parent = parent
        self._fieldname = fieldname
        self._bytesize = None
        self._fields = OrderedDict()
        self._force_array_size = force_array_size
        if backend is None and default_backend:
            backend = StructBackend(BufAccessor(self.byte_size))
        self._backend = backend

        if child_backend is None: child_backend = backend
        self._child_backend = child_backend

        if self._base_typ != self._typ:
            self._base_size = self._base_typ.size
            self._base_align = self._base_typ.align

            if self.is_array:
                pos = 0
                self.stride = BitOps.align(self._base_size,
                                           self._base_align * 8)
                assert self.stride != 0

                #for i in range(typ.array_size):
                #  self.add_child(self._base_typ, pos)
                #  pos += self._base_size
                #  pos = BitOps.align(pos, self._base_align)
            else:
                self._pointee = None
        else:
            if typ.is_primitive():
                pass
            elif typ.is_union:
                self._selected = 0
                for field in typ.ordered_fields:
                    self.add_field(field)
            else:
                pos = 0
                for field in typ.ordered_fields:
                    self.add_field(field)

        #self._mem = Memory(reader=self.get_buf, writer=self.set_buf, minv=0, maxv=self.bytesize)
        self._init_accessors()
Beispiel #2
0
  def build(self):
    for i, section in enumerate(self.sections):
      section.new_id = i
    for i, prog in enumerate(self.progs):
      prog.new_id = i

    res = io.BytesIO(bytearray())
    self.elf.header.e_shnum = len(self.sections)
    align_v = 1
    ehdr = self.elf.header
    ehdr.e_shnum = len(self.sections)
    ehdr.e_phnum = len(self.progs)
    ehdr.e_shstrndx = self.shstrtab_section.new_id
    ehdr_size = self.elf.structs.Elf_Ehdr.sizeof()
    shdr_size = ehdr.e_shentsize * ehdr.e_shnum
    phdr_size = ehdr.e_phentsize * ehdr.e_phnum

    self.write_phdr(ehdr, res)
    pos = BitOps.align(ehdr.e_phoff + phdr_size, align_v)


    self.prepare_sections(pos)

    pos = BitOps.align(pos, align_v)
    pos = self.write_sections_content(res)

    pos = BitOps.align(pos, align_v)
    ehdr.e_shoff  = pos

    #for prog in self.progs:
    #  if prog.raw.header.p_type != 'PT_PHDR': continue
    #  prog.raw.header.p_offset = ehdr.e_phoff
    #  prog.raw.header.p_filesz = phdr_size
    #  prog.raw.header.p_memsz = phdr_size


    self.write_sections(ehdr, res)

    res.seek(0)
    res.write(self.elf.structs.Elf_Ehdr.build(ehdr))
    print(ehdr_size, shdr_size, phdr_size, ehdr.e_shentsize)


    return res.getvalue()
Beispiel #3
0
  def set_section_at(self, section, pos):
    if section.done: return pos
    section.done = True
    shdr = section.raw.header
    pos = BitOps.align(pos, shdr.sh_addralign)

    shdr.sh_size = len(section.data)
    shdr.sh_offset = pos

    section.new_offset = pos
    pos += len(section.data)
    return pos