tools/android/elf_compression/compress_section.py
def _MovePhdrToTheEnd(data):
  """Moves Phdrs to the end of the file and adjusts all references to it."""
  elf_hdr = elf_headers.ElfHeader(data)

  # If program headers are already in the end of the file, nothing to do.
  if elf_hdr.e_phoff + elf_hdr.e_phnum * elf_hdr.e_phentsize == len(data):
    return

  old_phoff = elf_hdr.e_phoff
  new_phoff = elf_hdr.e_phoff = len(data)

  unaligned_new_vaddr = _FindNewVaddr(elf_hdr.GetProgramHeaders())
  new_vaddr = MatchVaddrAlignment(unaligned_new_vaddr, new_phoff)
  # Since we moved the PHDR section to the end of the file, we need to create a
  # new LOAD segment to load it in.
  current_filesize = elf_hdr.e_phnum * elf_hdr.e_phentsize
  # We are using current_filesize while adding new program header due to
  # AddProgramHeader handling the increase of size due to addition of new
  # header.
  elf_hdr.AddProgramHeader(
      elf_headers.ProgramHeader.Create(
          elf_hdr.byte_order,
          p_type=elf_headers.ProgramHeader.Type.PT_LOAD,
          p_flags=elf_headers.ProgramHeader.Flags.PF_R,
          p_offset=new_phoff,
          p_vaddr=new_vaddr,
          p_paddr=new_vaddr,
          p_filesz=current_filesize,
          p_memsz=current_filesize,
          p_align=ADDRESS_ALIGN,
      ))

  # PHDR segment if it exists should point to the new location.
  for phdr in elf_hdr.GetProgramHeadersByType(
      elf_headers.ProgramHeader.Type.PT_PHDR):
    phdr.p_offset = new_phoff
    phdr.p_vaddr = new_vaddr
    phdr.p_paddr = new_vaddr
    phdr.p_align = ADDRESS_ALIGN

  # We need to replace the previous phdr placement with zero bytes to fail
  # fast if dynamic linker doesn't like the new program header.
  previous_phdr_size = (elf_hdr.e_phnum - 1) * elf_hdr.e_phentsize
  data[old_phoff:old_phoff + previous_phdr_size] = [0] * previous_phdr_size

  # Updating ELF header to point to the new location.
  elf_hdr.PatchData(data)
Moves Phdrs to the end of the file and adjusts all references to it.
Moves Phdrs to the end of the file and adjusts all references to it.
Moves Phdrs to the end of the file and adjusts all references to it .
def _MovePhdrToTheEnd(data):
  elf_hdr = elf_headers.ElfHeader(data)
  if elf_hdr.e_phoff + elf_hdr.e_phnum * elf_hdr.e_phentsize == len(data):
    return
  old_phoff = elf_hdr.e_phoff
  new_phoff = elf_hdr.e_phoff = len(data)
  unaligned_new_vaddr = _FindNewVaddr(elf_hdr.GetProgramHeaders())
  new_vaddr = MatchVaddrAlignment(unaligned_new_vaddr, new_phoff)
  current_filesize = elf_hdr.e_phnum * elf_hdr.e_phentsize
  elf_hdr.AddProgramHeader(
      elf_headers.ProgramHeader.Create(
          elf_hdr.byte_order,
          p_type=elf_headers.ProgramHeader.Type.PT_LOAD,
          p_flags=elf_headers.ProgramHeader.Flags.PF_R,
          p_offset=new_phoff,
          p_vaddr=new_vaddr,
          p_paddr=new_vaddr,
          p_filesz=current_filesize,
          p_memsz=current_filesize,
          p_align=ADDRESS_ALIGN,
      ))
  for phdr in elf_hdr.GetProgramHeadersByType(
      elf_headers.ProgramHeader.Type.PT_PHDR):
    phdr.p_offset = new_phoff
    phdr.p_vaddr = new_vaddr
    phdr.p_paddr = new_vaddr
    phdr.p_align = ADDRESS_ALIGN
  previous_phdr_size = (elf_hdr.e_phnum - 1) * elf_hdr.e_phentsize
  data[old_phoff:old_phoff + previous_phdr_size] = [0] * previous_phdr_size
  elf_hdr.PatchData(data)
def _MovePhdrToTheEnd ( data ) : elf_hdr = elf_headers . ElfHeader ( data ) if elf_hdr . e_phoff + elf_hdr . e_phnum * elf_hdr . e_phentsize == len ( data ) : return old_phoff = elf_hdr . e_phoff new_phoff = elf_hdr . e_phoff = len ( data ) unaligned_new_vaddr = _FindNewVaddr ( elf_hdr . GetProgramHeaders ( ) ) new_vaddr = MatchVaddrAlignment ( unaligned_new_vaddr , new_phoff ) current_filesize = elf_hdr . e_phnum * elf_hdr . e_phentsize elf_hdr . AddProgramHeader ( elf_headers . ProgramHeader . Create ( elf_hdr . byte_order , p_type = elf_headers . ProgramHeader . Type . PT_LOAD , p_flags = elf_headers . ProgramHeader . Flags . PF_R , p_offset = new_phoff , p_vaddr = new_vaddr , p_paddr = new_vaddr , p_filesz = current_filesize , p_memsz = current_filesize , p_align = ADDRESS_ALIGN , ) ) for phdr in elf_hdr . GetProgramHeadersByType ( elf_headers . ProgramHeader . Type . PT_PHDR ) : phdr . p_offset = new_phoff phdr . p_vaddr = new_vaddr phdr . p_paddr = new_vaddr phdr . p_align = ADDRESS_ALIGN previous_phdr_size = ( elf_hdr . e_phnum - 1 ) * elf_hdr . e_phentsize data [ old_phoff : old_phoff + previous_phdr_size ] = [ 0 ] * previous_phdr_size elf_hdr . PatchData ( data )
Moves Phdrs to the end of the file and adjusts all references to it.
Moves Phdrs to the end of the file and adjusts all references to it .
"""Moves Phdrs to the end of the file and adjusts all references to it.""" # If program headers are already in the end of the file, nothing to do. # Since we moved the PHDR section to the end of the file, we need to create a # new LOAD segment to load it in. # We are using current_filesize while adding new program header due to # AddProgramHeader handling the increase of size due to addition of new # header. # PHDR segment if it exists should point to the new location. # We need to replace the previous phdr placement with zero bytes to fail # fast if dynamic linker doesn't like the new program header. # Updating ELF header to point to the new location.
data