tools/android/elf_compression/compress_section.py
def _SplitLoadSegmentAndNullifyRange(data, l, r):
  """Find LOAD segment covering [l, r) and splits it into three segments.

  Split is done so one of the LOAD segments contains only [l, r) and nothing
  else. If the range is located at the start or at the end of the segment less
  than three segments may be created.

  The resulting LOAD segment containing [l, r) is edited so it sets the
  corresponding virtual address range to zeroes, ignoring file content.

  Returns virtual address range corresponding to [l, r).
  """
  elf_hdr = elf_headers.ElfHeader(data)

  range_phdr = None
  for phdr in elf_hdr.GetProgramHeadersByType(
      elf_headers.ProgramHeader.Type.PT_LOAD):
    if SegmentContains(phdr.p_offset, phdr.FilePositionEnd(), l, r):
      range_phdr = phdr
      break
  if range_phdr is None:
    raise RuntimeError('No LOAD segment covering the range found')

  # The range_phdr will become the LOAD segment containing the [l, r) range
  # but we need to create the additional two segments.
  left_segment_size = l - range_phdr.p_offset
  if left_segment_size > 0:
    # Creating LOAD segment containing the [phdr.p_offset, l) part.
    elf_hdr.AddProgramHeader(
        elf_headers.ProgramHeader.Create(
            elf_hdr.byte_order,
            p_type=range_phdr.p_type,
            p_flags=range_phdr.p_flags,
            p_offset=range_phdr.p_offset,
            p_vaddr=range_phdr.p_vaddr,
            p_paddr=range_phdr.p_paddr,
            p_filesz=left_segment_size,
            p_memsz=left_segment_size,
            p_align=range_phdr.p_align,
        ))
  if range_phdr.p_offset + range_phdr.p_memsz > r:
    # Creating LOAD segment containing the [r, phdr.p_offset + phdr.p_memsz).
    right_segment_delta = r - range_phdr.p_offset
    right_segment_address = range_phdr.p_vaddr + right_segment_delta
    right_segment_filesize = max(range_phdr.p_filesz - right_segment_delta, 0)
    right_segment_memsize = range_phdr.p_memsz - right_segment_delta
    elf_hdr.AddProgramHeader(
        elf_headers.ProgramHeader.Create(
            elf_hdr.byte_order,
            p_type=range_phdr.p_type,
            p_flags=range_phdr.p_flags,
            p_offset=r,
            p_vaddr=right_segment_address,
            p_paddr=right_segment_address,
            p_filesz=right_segment_filesize,
            p_memsz=right_segment_memsize,
            p_align=range_phdr.p_align,
        ))
  # Modifying the range_phdr
  central_segment_address = range_phdr.p_vaddr + left_segment_size
  range_phdr.p_offset = l
  range_phdr.p_vaddr = central_segment_address
  range_phdr.p_paddr = central_segment_address
  range_phdr.p_filesz = 0
  range_phdr.p_memsz = r - l

  elf_hdr.PatchData(data)
  return central_segment_address, central_segment_address + (r - l)
Find LOAD segment covering [l, r) and splits it into three segments.

  Split is done so one of the LOAD segments contains only [l, r) and nothing
  else. If the range is located at the start or at the end of the segment less
  than three segments may be created.

  The resulting LOAD segment containing [l, r) is edited so it sets the
  corresponding virtual address range to zeroes, ignoring file content.

  Returns virtual address range corresponding to [l, r).
  
Find LOAD segment covering [l, r) and splits it into three segments.
Split is done so one of the LOAD segments contains only [l, r) and nothing
else. If the range is located at the start or at the end of the segment less
than three segments may be created.

The resulting LOAD segment containing [l, r) is edited so it sets the
corresponding virtual address range to zeroes, ignoring file content.

Returns virtual address range corresponding to [l, r).
Find LOAD segment covering [ l r ) and splits it into three segments . Split is done so one of the LOAD segments contains only [ l r ) and nothing else . If the range is located at the start or at the end of the segment less than three segments may be created . The resulting LOAD segment containing [ l r ) is edited so it sets the corresponding virtual address range to zeroes ignoring file content . Returns virtual address range corresponding to [ l r ) .
def _SplitLoadSegmentAndNullifyRange(data, l, r):
  elf_hdr = elf_headers.ElfHeader(data)
  range_phdr = None
  for phdr in elf_hdr.GetProgramHeadersByType(
      elf_headers.ProgramHeader.Type.PT_LOAD):
    if SegmentContains(phdr.p_offset, phdr.FilePositionEnd(), l, r):
      range_phdr = phdr
      break
  if range_phdr is None:
    raise RuntimeError('No LOAD segment covering the range found')
  left_segment_size = l - range_phdr.p_offset
  if left_segment_size > 0:
    elf_hdr.AddProgramHeader(
        elf_headers.ProgramHeader.Create(
            elf_hdr.byte_order,
            p_type=range_phdr.p_type,
            p_flags=range_phdr.p_flags,
            p_offset=range_phdr.p_offset,
            p_vaddr=range_phdr.p_vaddr,
            p_paddr=range_phdr.p_paddr,
            p_filesz=left_segment_size,
            p_memsz=left_segment_size,
            p_align=range_phdr.p_align,
        ))
  if range_phdr.p_offset + range_phdr.p_memsz > r:
    right_segment_delta = r - range_phdr.p_offset
    right_segment_address = range_phdr.p_vaddr + right_segment_delta
    right_segment_filesize = max(range_phdr.p_filesz - right_segment_delta, 0)
    right_segment_memsize = range_phdr.p_memsz - right_segment_delta
    elf_hdr.AddProgramHeader(
        elf_headers.ProgramHeader.Create(
            elf_hdr.byte_order,
            p_type=range_phdr.p_type,
            p_flags=range_phdr.p_flags,
            p_offset=r,
            p_vaddr=right_segment_address,
            p_paddr=right_segment_address,
            p_filesz=right_segment_filesize,
            p_memsz=right_segment_memsize,
            p_align=range_phdr.p_align,
        ))
  central_segment_address = range_phdr.p_vaddr + left_segment_size
  range_phdr.p_offset = l
  range_phdr.p_vaddr = central_segment_address
  range_phdr.p_paddr = central_segment_address
  range_phdr.p_filesz = 0
  range_phdr.p_memsz = r - l
  elf_hdr.PatchData(data)
  return central_segment_address, central_segment_address + (r - l)
def _SplitLoadSegmentAndNullifyRange ( data , l , r ) : elf_hdr = elf_headers . ElfHeader ( data ) range_phdr = None for phdr in elf_hdr . GetProgramHeadersByType ( elf_headers . ProgramHeader . Type . PT_LOAD ) : if SegmentContains ( phdr . p_offset , phdr . FilePositionEnd ( ) , l , r ) : range_phdr = phdr break if range_phdr is None : raise RuntimeError ( 'No LOAD segment covering the range found' ) left_segment_size = l - range_phdr . p_offset if left_segment_size > 0 : elf_hdr . AddProgramHeader ( elf_headers . ProgramHeader . Create ( elf_hdr . byte_order , p_type = range_phdr . p_type , p_flags = range_phdr . p_flags , p_offset = range_phdr . p_offset , p_vaddr = range_phdr . p_vaddr , p_paddr = range_phdr . p_paddr , p_filesz = left_segment_size , p_memsz = left_segment_size , p_align = range_phdr . p_align , ) ) if range_phdr . p_offset + range_phdr . p_memsz > r : right_segment_delta = r - range_phdr . p_offset right_segment_address = range_phdr . p_vaddr + right_segment_delta right_segment_filesize = max ( range_phdr . p_filesz - right_segment_delta , 0 ) right_segment_memsize = range_phdr . p_memsz - right_segment_delta elf_hdr . AddProgramHeader ( elf_headers . ProgramHeader . Create ( elf_hdr . byte_order , p_type = range_phdr . p_type , p_flags = range_phdr . p_flags , p_offset = r , p_vaddr = right_segment_address , p_paddr = right_segment_address , p_filesz = right_segment_filesize , p_memsz = right_segment_memsize , p_align = range_phdr . p_align , ) ) central_segment_address = range_phdr . p_vaddr + left_segment_size range_phdr . p_offset = l range_phdr . p_vaddr = central_segment_address range_phdr . p_paddr = central_segment_address range_phdr . p_filesz = 0 range_phdr . p_memsz = r - l elf_hdr . PatchData ( data ) return central_segment_address , central_segment_address + ( r - l )
Find LOAD segment covering [l, r) and splits it into three segments.
Find LOAD segment covering [ l r ) and splits it into three segments .
"""Find LOAD segment covering [l, r) and splits it into three segments.

  Split is done so one of the LOAD segments contains only [l, r) and nothing
  else. If the range is located at the start or at the end of the segment less
  than three segments may be created.

  The resulting LOAD segment containing [l, r) is edited so it sets the
  corresponding virtual address range to zeroes, ignoring file content.

  Returns virtual address range corresponding to [l, r).
  """ # The range_phdr will become the LOAD segment containing the [l, r) range # but we need to create the additional two segments. # Creating LOAD segment containing the [phdr.p_offset, l) part. # Creating LOAD segment containing the [r, phdr.p_offset + phdr.p_memsz). # Modifying the range_phdr
data
l
r