Пример #1
0
def load_program( fp, mem, alignment=0 ):

  mem_image  = elf.elf_reader( fp )
  sections   = mem_image.get_sections()
  entrypoint = -1

  for section in sections:
    start_addr = section.addr
    for i, data in enumerate( section.data ):
      mem.write( start_addr+i, 1, ord( data ) )

    # TODO: HACK should really have elf_reader return the entry point
    #       address in the elf header!
    if section.name == '.text':
      entrypoint = section.addr
    if section.name == '.data':
      mem.data_section = section.addr

  assert entrypoint >= 0

  last_sec   = sections[-1]
  breakpoint = last_sec.addr + len( last_sec.data )

  if alignment > 0:
    def round_up( val, alignment ):
      return (val + alignment - 1) & ~(alignment - 1)
    breakpoint = round_up( breakpoint, alignment )

  return entrypoint, breakpoint
Пример #2
0
def load_program( fp, mem, alignment=0, is_64bit=False ):

  mem_image  = elf.elf_reader( fp, is_64bit=is_64bit )
  sections   = mem_image.get_sections()
  entrypoint = -1

  for section in sections:
    start_addr = section.addr
    for i, data in enumerate( section.data ):
      mem.write( start_addr+i, 1, ord( data ) )

    # TODO: HACK should really have elf_reader return the entry point
    #       address in the elf header!
    if section.name == '.text':
      entrypoint = intmask( section.addr )
    if section.name == '.data':
      mem.data_section = section.addr

  assert entrypoint >= 0

  last_sec   = sections[-1]
  breakpoint = last_sec.addr + len( last_sec.data )

  if alignment > 0:
    def round_up( val, alignment ):
      return (val + alignment - 1) & ~(alignment - 1)
    breakpoint = round_up( breakpoint, alignment )

  return entrypoint, breakpoint
Пример #3
0
def load_program(fp):
    mem_image = elf.elf_reader(fp)

    sections = mem_image.get_sections()
    mem = Memory(size=memory_size, byte_storage=False)

    for section in sections:
        start_addr = section.addr
        for i, data in enumerate(section.data):
            mem.write(start_addr + i, 1, ord(data))

    bss = sections[-1]
    breakpoint = bss.addr + len(bss.data)
    return mem, breakpoint
Пример #4
0
def test_basic( tmpdir ):

  # Create a sparse memory image

  mem_image = SparseMemoryImage()

  section_names = [ ".text", ".data" ]

  for i in xrange(4):

    section = SparseMemoryImage.Section()
    section.name = section_names[ random.randint(0,1) ]
    section.addr = i * 0x00001000

    data_ints  = [ random.randint(0,1000) for r in xrange(10) ]
    data_bytes = bytearray()
    for data_int in data_ints:
      data_bytes.extend(struct.pack("<I",data_int))

    section.data = data_bytes

    mem_image.add_section( section )

  # Write the sparse memory image to an ELF file

  with tmpdir.join("elf-test").open('wb') as file_obj:
    elf.elf_writer( mem_image, file_obj )

  # Read the ELF file back into a new sparse memory image

  mem_image_test = None
  with tmpdir.join("elf-test").open('rb') as file_obj:
    mem_image_test = elf.elf_reader( file_obj )

  # Check that the original and new sparse memory images are equal

  assert mem_image == mem_image_test
Пример #5
0
def test_basic(tmpdir):

    # Create a sparse memory image

    mem_image = SparseMemoryImage()

    section_names = [".text", ".data"]

    for i in range(4):

        section = SparseMemoryImage.Section()
        section.name = section_names[random.randint(0, 1)]
        section.addr = i * 0x00000200

        data_ints = [random.randint(0, 1000) for r in range(10)]
        data_bytes = bytearray()
        for data_int in data_ints:
            data_bytes.extend(struct.pack("<I", data_int))

        section.data = data_bytes

        mem_image.add_section(section)

    # Write the sparse memory image to an ELF file

    with tmpdir.join("elf-test").open('wb') as file_obj:
        elf.elf_writer(mem_image, file_obj)

    # Read the ELF file back into a new sparse memory image

    mem_image_test = None
    with tmpdir.join("elf-test").open('rb') as file_obj:
        mem_image_test = elf.elf_reader(file_obj)

    # Check that the original and new sparse memory images are equal

    assert mem_image == mem_image_test