def PatchUpPyinstaller(self):
    """PyInstaller binaries need to be repaired.

    PyInstaller just dumps the packed payload at the end of the ELF binary
    without adjusting the ELF sections. This means that any manipulation of the
    ELF file (e.g. prelinking the file as is commonly done on RedHat machines)
    will strip the payload from the file.

    We fix this by extending the last section to the end of the file so the
    payload is included.
    """
    root = config_lib.CONFIG.Get("PyInstaller.dpkg_root", context=self.context)
    with open(os.path.join(root, "debian/grr-client/grr-client"), "r+b") as fd:
      # Find the size of the file.
      fd.seek(0, 2)
      size = fd.tell()
      fd.seek(0, 0)

      # The headers should fall within the first part.
      data = fd.read(1000000)

      # Support 64 bit ELF files.
      if data[:5] == "\x7fELF\x02":

        # Ref: http://en.wikipedia.org/wiki/Executable_and_Linkable_Format
        shr_offset = struct.unpack("<Q", data[0x28:0x28 + 8])[0]
        number_of_sections = struct.unpack("<H", data[0x3c:0x3c + 2])[0]
        size_of_section = struct.unpack("<H", data[0x3a:0x3a + 2])[0]

        # We extend the last section right up to the end of the file.
        last_section_offset = (shr_offset +
                               (number_of_sections - 1) * size_of_section)

        # The file offset where the section starts.
        start_of_section = struct.unpack("<Q", data[
            last_section_offset + 0x18:last_section_offset + 0x18 + 8])[0]

        # Overwrite the size of the section.
        fd.seek(last_section_offset + 0x20)
        fd.write(struct.pack("<Q", size - start_of_section))

      # Also support 32 bit ELF.
      elif data[:5] == "\x7fELF\x01":
        # Ref: http://en.wikipedia.org/wiki/Executable_and_Linkable_Format
        shr_offset = struct.unpack("<I", data[0x20:0x20 + 4])[0]
        number_of_sections = struct.unpack("<H", data[0x30:0x30 + 2])[0]
        size_of_section = struct.unpack("<H", data[0x2e:0x2e + 2])[0]

        # We extend the last section right up to the end of the file.
        last_section_offset = (shr_offset +
                               (number_of_sections - 1) * size_of_section)

        # The file offset where the section starts (Elf32_Shdr.sh_offset).
        start_of_section = struct.unpack("<I", data[
            last_section_offset + 0x10:last_section_offset + 0x10 + 4])[0]

        # Overwrite the size of the section (Elf32_Shdr.sh_size).
        fd.seek(last_section_offset + 0x14)
        fd.write(struct.pack("<I", size - start_of_section))
dataset/ETHPy150Open google/grr/grr/lib/builders/linux.py/LinuxClientBuilder.PatchUpPyinstaller/original