tools/android/elf_compression/compress_section.py
def _CutRangeAndCorrectFile(data, l, r):
  """Removes [l, r) from the data and fixes offsets to stabilize the ELF."""
  elf = elf_headers.ElfHeader(data)
  # Removing the range from the file:
  del data[l:r]

  range_length = r - l
  for phdr in elf.GetProgramHeaders():
    # Any other program header intersecting the [l, r) range poses serious
    # problem as this header needs to be split if possible. However since we are
    # compressing part of program's code this is highly unlikely, albeit
    # possible in worst case scenario.
    # With that in mind we assert that such thing doesn't happen in our case.
    if SegmentsIntersect(phdr.p_offset, phdr.FilePositionEnd(), l, r):
      raise RuntimeError('Segment intersects with provided range')
    if phdr.p_offset >= r:
      phdr.p_offset -= range_length
    # Per ELF standard: p_offset % p_align == p_vaddr % p_align.
    # Since we moved the p_offset we could have broken this rule.
    # To mitigate this issue we notice the following two facts:
    # 1) range_length % ADDRESS_ALIGN == 0.
    # 2) We can reduce the p_align without breaking the alignment.
    # We reduce all p_align to be less or equal than ADDRESS_ALIGN and now
    # range_length % p_align == 0, so the alignment remains valid.
    # Our changes of p_align are perfectly legal per standard
    # as long as p_align % PAGE_SIZE == 0.
    if phdr.p_align > ADDRESS_ALIGN:
      phdr.p_align = ADDRESS_ALIGN

  for shdr in elf.GetSectionHeaders():
    # Note that if the section overlaps with the cut range we are unable
    # to adjust its size to match both file and virtual size of it. In such
    # case we treat sh_size as memory size and don't adjust it, which may
    # cause some tools treating sh_size as file size to stop working.
    if shdr.sh_offset >= l:
      if shdr.sh_offset < r:
        raise RuntimeError('Section starts within the provided range')
      else:
        shdr.sh_offset -= range_length
  if elf.e_phoff > l:
    elf.e_phoff -= range_length
  if elf.e_shoff > l:
    elf.e_shoff -= range_length
  elf.PatchData(data)
Removes [l, r) from the data and fixes offsets to stabilize the ELF.
Removes [l, r) from the data and fixes offsets to stabilize the ELF.
Removes [ l r ) from the data and fixes offsets to stabilize the ELF .
def _CutRangeAndCorrectFile(data, l, r):
  elf = elf_headers.ElfHeader(data)
  del data[l:r]
  range_length = r - l
  for phdr in elf.GetProgramHeaders():
    if SegmentsIntersect(phdr.p_offset, phdr.FilePositionEnd(), l, r):
      raise RuntimeError('Segment intersects with provided range')
    if phdr.p_offset >= r:
      phdr.p_offset -= range_length
    if phdr.p_align > ADDRESS_ALIGN:
      phdr.p_align = ADDRESS_ALIGN
  for shdr in elf.GetSectionHeaders():
    if shdr.sh_offset >= l:
      if shdr.sh_offset < r:
        raise RuntimeError('Section starts within the provided range')
      else:
        shdr.sh_offset -= range_length
  if elf.e_phoff > l:
    elf.e_phoff -= range_length
  if elf.e_shoff > l:
    elf.e_shoff -= range_length
  elf.PatchData(data)
def _CutRangeAndCorrectFile ( data , l , r ) : elf = elf_headers . ElfHeader ( data ) del data [ l : r ] range_length = r - l for phdr in elf . GetProgramHeaders ( ) : if SegmentsIntersect ( phdr . p_offset , phdr . FilePositionEnd ( ) , l , r ) : raise RuntimeError ( 'Segment intersects with provided range' ) if phdr . p_offset >= r : phdr . p_offset -= range_length if phdr . p_align > ADDRESS_ALIGN : phdr . p_align = ADDRESS_ALIGN for shdr in elf . GetSectionHeaders ( ) : if shdr . sh_offset >= l : if shdr . sh_offset < r : raise RuntimeError ( 'Section starts within the provided range' ) else : shdr . sh_offset -= range_length if elf . e_phoff > l : elf . e_phoff -= range_length if elf . e_shoff > l : elf . e_shoff -= range_length elf . PatchData ( data )
Removes [l, r) from the data and fixes offsets to stabilize the ELF.
Removes [ l r ) from the data and fixes offsets to stabilize the ELF .
"""Removes [l, r) from the data and fixes offsets to stabilize the ELF.""" # Removing the range from the file: # Any other program header intersecting the [l, r) range poses serious # problem as this header needs to be split if possible. However since we are # compressing part of program's code this is highly unlikely, albeit # possible in worst case scenario. # With that in mind we assert that such thing doesn't happen in our case. # Per ELF standard: p_offset % p_align == p_vaddr % p_align. # Since we moved the p_offset we could have broken this rule. # To mitigate this issue we notice the following two facts: # 1) range_length % ADDRESS_ALIGN == 0. # 2) We can reduce the p_align without breaking the alignment. # We reduce all p_align to be less or equal than ADDRESS_ALIGN and now # range_length % p_align == 0, so the alignment remains valid. # Our changes of p_align are perfectly legal per standard # as long as p_align % PAGE_SIZE == 0. # Note that if the section overlaps with the cut range we are unable # to adjust its size to match both file and virtual size of it. In such # case we treat sh_size as memory size and don't adjust it, which may # cause some tools treating sh_size as file size to stop working.
data
l
r