예제 #1
0
def perform_test(filename):
    """ Try to compile the given snippet. """
    logger.info("Step 1: Compile %s!", filename)
    march = "x86_64"

    html_report = os.path.splitext(filename)[0] + "_report.html"

    coptions = COptions()
    root_folder = os.path.join(this_dir, "..", "..", "..")
    libc_folder = os.path.join(root_folder, "librt", "libc")
    libc_include = os.path.join(libc_folder, "include")
    coptions.add_include_path(libc_include)

    # TODO: this should be injected elsewhere?
    coptions.add_define('__LP64__', '1')
    # coptions.enable('freestanding')

    with html_reporter(html_report) as reporter:
        with open(filename, "r") as f:
            try:
                obj1 = api.cc(f, march, coptions=coptions, reporter=reporter)
            except CompilerError as ex:
                ex.print()
                raise
    logger.info("Compilation complete, %s", obj1)

    obj0 = api.asm(io.StringIO(STARTERCODE), march)
    obj2 = api.c3c([io.StringIO(BSP_C3_SRC)], [], march)
    with open(os.path.join(libc_include, "lib.c"), "r") as f:
        obj3 = api.cc(f, march, coptions=coptions)

    obj = api.link([obj0, obj1, obj2, obj3], layout=io.StringIO(ARCH_MMAP))

    logger.info("Step 2: Run it!")

    exe_filename = os.path.splitext(filename)[0] + "_executable.elf"
    with open(exe_filename, "wb") as f:
        write_elf(obj, f, type="executable")
    api.chmod_x(exe_filename)

    logger.info("Running %s", exe_filename)
    test_prog = subprocess.Popen(exe_filename, stdout=subprocess.PIPE)
    exit_code = test_prog.wait()
    assert exit_code == 0
    captured_stdout = test_prog.stdout.read().decode("ascii")

    with open(filename + ".expected", "r") as f:
        expected_stdout = f.read()

    # Compare stdout:
    assert captured_stdout == expected_stdout
예제 #2
0
파일: compile_8cc.py 프로젝트: ttwj/ppci
def main():
    t1 = time.time()
    failed = 0
    passed = 0
    sources = [
        'cpp.c',
        'debug.c',
        'dict.c',
        'gen.c',
        'lex.c',
        'vector.c',
        'parse.c',
        'buffer.c',
        'map.c',
        'error.c',
        'path.c',
        'file.c',
        'set.c',
        'encoding.c',
    ]
    objs = []
    with html_reporter(report_filename) as reporter:
        for filename in sources:
            filename = os.path.join(_8cc_folder, filename)
            print('==> Compiling', filename)
            try:
                obj = do_compile(filename, reporter)
            except CompilerError as ex:
                print('Error:', ex.msg, ex.loc)
                ex.print()
                print_exc()
                failed += 1
            except Exception as ex:
                print('General exception:', ex)
                print_exc()
                failed += 1
            else:
                objs.append(obj)
                print('Great success!')
                passed += 1

    t2 = time.time()
    elapsed = t2 - t1
    print(passed, 'passed,', failed, 'failed in', elapsed, 'seconds')

    obj = api.link(objs)
    with open('8cc.exe', 'wb') as f:
        write_elf(obj, f, type='executable')
예제 #3
0
linker_script = os.path.join(linux64_folder, 'linux64.mmap')
objs.append(api.asm(crt0_asm, march))
objs.append(api.c3c([crt0_c3], [], march))
objs.append(api.cc(io.StringIO(hacked_libc_extras), march, coptions=coptions))

sources = list(glob.glob(os.path.join(core_mark_folder, '*.c')))
sources.extend(glob.glob(os.path.join(port_folder, '*.c')))
sources.extend(glob.glob(os.path.join(libc_folder, '*.c')))

for source_file in sources:
    print(source_file)
    try:
        with open(source_file, 'r') as f:
            obj = api.cc(f, march, coptions=coptions, opt_level=opt_level)
    except CompilerError as ex:
        print('ERROR!')
        print(ex)
        ex.print()
    else:
        objs.append(obj)

print(objs)

full_obj = api.link(objs, layout=linker_script)

exe_filename = 'coremark.elf'
with open(exe_filename, 'wb') as f:
    write_elf(full_obj, f)

api.chmod_x(exe_filename)
예제 #4
0
// This will trigger relocations on the data section:
double a;
double *pa = &a;

// idea: link with libc for putchar!
int putchar(int);

int magic_helper(int x)
{
    putchar(65); // 'A'
    return x + 1 + *pa;
}

int barf(int x, double y)
{
    putchar(66); // 'B'
    return magic_helper(x) - y;
}

"""



obj = api.cc(io.StringIO(c_src), 'x86_64')

print(obj)

with open('barf.o', 'wb') as f:
    write_elf(obj, f, type='relocatable')

예제 #5
0
파일: test_elf.py 프로젝트: ttwj/ppci
 def test_save_load(self):
     arch = get_arch('arm')
     f = io.BytesIO()
     write_elf(ObjectFile(arch), f)
     f2 = io.BytesIO(f.getvalue())
     ElfFile.load(f2)