예제 #1
0
def patch_elf():
    dprint("Loading \"%s\"..." % args.source)

    with open(args.source, "rb") as file:
        elf_data = bytearray(file.read())

    elf = ELFObject()
    elf.fromFile(StringIO(elf_data))

    for s in elf.getSections():
        dprint("  %-16s: 0x%08x -> 0x%08x %8d" %
            (s.name, s.lma, s.sh_addr, s.sh_size)
        )

    ##########

    dprint("\nPatching section \"%s\"..." % args.section)

    section = elf.getSection(args.section)
    if section == None:
        raise Exception("Section not found")
    
    info = VersionInfo(elf)

    patch_section(elf, elf_data, section, info.pack())

    info.image_crc = CRC32().forge(
        0x00000000, elf_to_bin(elf, elf_data), 
        section.lma - elf.getSections()[0].lma
    )
    
    patch_section(elf, elf_data, section, info.pack())
    
    dprint("  image_crc  = 0x%08x" % info.image_crc)
    dprint("  image_size = %d"     % info.image_size)

    ##########

    dprint("\nSaving \"%s\"..." % args.target)

    with open(args.target, "wb") as file:
        file.write(elf_data)
예제 #2
0
def patch_elf():
    dprint("Loading \"%s\"..." % args.source)

    with open(args.source, "rb") as file:
        elf_data = bytearray(file.read())

    elf = ELFObject()
    elf.fromFile(StringIO(elf_data))

    for s in elf.getSections():
        dprint("  %-16s: 0x%08x -> 0x%08x %8d" %
            (s.name, s.lma, s.sh_addr, s.sh_size)
        )

    ##########

    dprint("\nPatching section \"%s\"..." % args.section)

    section = elf.getSection(args.section)
    if section == None:
        raise Exception("Section not found")

    info = VersionInfo(elf)

    patch_section(elf, elf_data, section, info.pack())

    info.image_crc = CRC32().forge(
        0x00000000, elf_to_bin(elf, elf_data),
        section.lma - elf.getSections()[0].lma
    )

    patch_section(elf, elf_data, section, info.pack())

    dprint("  image_crc  = 0x%08x" % info.image_crc)
    dprint("  image_size = %d"     % info.image_size)

    ##########

    dprint("\nSaving \"%s\"..." % args.target)

    with open(args.target, "wb") as file:
        file.write(elf_data)