Example #1
0
def c2mpt_objdump(input_file, arguments):
    """

    :param input_file:
    :type input_file:
    :param arguments:
    :type arguments:
    """

    print_info("Compiling program for target")

    cprog = arguments["target_c_compiler"]
    cflags = " -std=c99"
    cflags += " ".join(
        [" -I \"" + elem + "\"" for elem in MICROPROBE_RC['template_paths']])
    cflags += " \"%s\"" % findfiles(MICROPROBE_RC['template_paths'],
                                    "c2mpt.c$")[0]
    cflags += " -Wl,--section-start,microprobe.text="
    cflags += hex(arguments["default_code_address"])
    cflags += " -Wl,--section-start,microprobe.data="
    cflags += hex(arguments["default_data_address"])
    cflags += " " + arguments["target_c_compiler_flags"]

    suffix = ".target.bin"
    if "save_temps" in arguments:
        compile_file = input_file.rstrip(".c") + suffix
    else:
        compile_file = _temp_file(suffix=suffix)
        _RM_FILES.append(compile_file)

    cmd = "\"%s\" %s \"%s\" -o \"%s\"" % (cprog, cflags, input_file,
                                          compile_file)
    print_info("Executing compilation command")
    run_cmd(cmd)

    objdumpprog = arguments["target_objdump"]
    objdumpflags = " -D -z -j microprobe.data -j microprobe.text"

    suffix = ".target.dump"
    if "save_temps" in arguments:
        objdump_file = input_file.rstrip(".c") + suffix
    else:
        objdump_file = _temp_file(suffix=suffix)
        _RM_FILES.append(objdump_file)

    cmd = "\"%s\" %s \"%s\"" % (objdumpprog, objdumpflags, compile_file)
    run_cmd_output_redirect(cmd, objdump_file)
    print_info("Executing objdump command")
    return objdump_file
Example #2
0
def c2mpt_local_run(input_file, arguments):
    """

    :param input_file:
    :type input_file:
    :param arguments:
    :type arguments:
    """

    print_info("Compiling program for host")

    cprog = arguments["host_c_compiler"]
    cflags = " -std=c99"
    cflags += " -DMPT_BASE_ADDRESS=%d" % arguments.get("host_displacement", 0)
    cflags += " ".join(
        [" -I \"" + elem + "\"" for elem in MICROPROBE_RC['template_paths']])
    cflags += " \"%s\"" % findfiles(MICROPROBE_RC['template_paths'],
                                    "c2mpt.c$")[0]
    cflags += " -Wl,--section-start,microprobe.text="
    cflags += hex(arguments["default_code_address"])
    cflags += " -Wl,--section-start,microprobe.data="
    cflags += hex(arguments["default_data_address"])
    cflags += " " + arguments["host_c_compiler_flags"]

    suffix = ".host.bin"
    if "save_temps" in arguments:
        compile_file = input_file.rstrip(".c") + suffix
    else:
        compile_file = _temp_file(suffix=suffix)
        _RM_FILES.append(compile_file)

    cmd = "\"%s\" %s \"%s\" -o \"%s\"" % (cprog, cflags, input_file,
                                          compile_file)

    print_info("Executing compilation command")
    run_cmd(cmd)

    print_info("Executing the compiled program")
    vardefinitions = run_cmd_output("%s" % compile_file)

    return vardefinitions
Example #3
0
def _compile(filename, target, **kwargs):

    if "compiler" not in kwargs:
        print_info("Compiler not provided")
        print_info("To compiler the code, first extract the custom")
        print_info("ld script embedded in the assembly as comments to do")
        print_info("so execute:")
        print_info("grep 'MICROPROBE LD' %s | cut -d '@' -f 2" % filename)
        print_info("then compile using gcc and providing the ld script")
        print_info("using the -T option.")
        return

    print_info("Compiling %s ..." % filename)

    outputname = ".".join(filename.split(".")[:-1]+["elf"])
    ldscriptname = ".".join(filename.split(".")[:-1]+["ldscript"])

    fdout = open(ldscriptname, "w")
    fdin = open(filename, "r")
    for line in fdin.readlines():
        if "MICROPROBE LD" in line:
            line = line.split("@")[1]
            fdout.write(line)
    fdout.close()
    fdin.close()

    try:
        baseldscriptname = findfiles(
            MICROPROBE_RC['template_paths'],
            "%s.ldscript" % target.name
        )[0]
    except IndexError:
        print_error("Unable to find template ld script: %s.ldscript" %
                    target.name)
        exit(-1)

    cprog = kwargs["compiler"]

    cflags = " -o %s" % outputname
    cflags += " -T %s" % ldscriptname
    cflags += " -T %s " % baseldscriptname
    cflags += kwargs["compiler_flags"]
    # We only support BFD linker
    cflags += " -fuse-ld=bfd "

    cmd = "%s %s %s" % (cprog, cflags, filename)

    print_info("Executing compilation command")
    print_info("%s" % cmd)
    try:
        run_cmd(cmd)
        print_info("'%s' generated!" % outputname)
    except MicroprobeRunCmdError:
        cflags += " -static"
        cmd = "%s %s %s" % (cprog, cflags, filename)
        print_info("Executing compilation command (statically)")
        print_info("%s" % cmd)
        try:
            run_cmd(cmd)
            print_info("'%s' generated!" % outputname)
        except MicroprobeRunCmdError:
            print_info("'%s' not generated due compilation"
                       " issues. Try manual compilation." % outputname)