Ejemplo n.º 1
0
    def collect_needed(self, sofile):
        try:
            with open(sofile, 'rb') as f:
                try:
                    elffile = ELFFile(f)

                    # we try to avoid superfluous work by not calling
                    # elffile.itersections() directly
                    # Instead we use the lower level API and continue
                    # if the section type is not SHT_DYNAMIC
                    # We can thus avoid to construct Section objects
                    for i in range(elffile.num_sections()):
                        section_header = elffile._get_section_header(i)
                        sectype = section_header['sh_type']
                        if sectype != 'SHT_DYNAMIC':
                            continue
                        name = elffile._get_section_name(section_header)
                        section = DynamicSection(section_header, name,
                                                 elffile.stream,
                                                 elffile)

                        for tag in section.iter_tags('DT_NEEDED'):
                            self.lib2required_by[tag.needed].append(sofile)
                        break # there should only be one dyanmic section

                except ELFError:
                    pass  # not an ELF file
        except PermissionError:
            warn("Could not open {}; please check permissions".format(sofile))
Ejemplo n.º 2
0
 def init_mem(self):
     print("init_mem")
     sw_image = cocotb.plusargs["SW_IMAGE"]
     with open(sw_image, "rb") as f:
         elffile = ELFFile(f)
         symtab = elffile.get_section_by_name('.symtab')
         
         begin_signature = symtab.get_symbol_by_name("begin_signature")[0]["st_value"]
         end_signature = symtab.get_symbol_by_name("end_signature")[0]["st_value"]
         
         addr = begin_signature
         
         # Find the section that contains the data we need
         section = None
         for i in range(elffile.num_sections()):
             shdr = elffile._get_section_header(i)
             if begin_signature >= shdr['sh_addr'] and begin_signature <= (shdr['sh_addr'] + shdr['sh_size']):
                 section = elffile.get_section(i)
                 begin_signature_offset = begin_signature - shdr['sh_addr']
                 break
             
         data = section.data()
         for addr in range(begin_signature, end_signature, 4):
             word = (
                 (data[begin_signature_offset+0] << (8*0))
                 | (data[begin_signature_offset+1] << (8*1))
                 | (data[begin_signature_offset+2] << (8*2))
                 | (data[begin_signature_offset+3] << (8*3))
                 );
             self.mem[(addr & 0xFFFF) >> 2] = word
             
             begin_signature_offset += 4
Ejemplo n.º 3
0
async def test(top):
    await pybfms.init()

    u_bram = pybfms.find_bfm(".*u_bram")
    u_dbg_bfm: RiscvDebugBfm = pybfms.find_bfm(".*u_dbg_bfm")
    u_uart_bfm: UartBfm = pybfms.find_bfm(".*u_uart_bfm")

    uart_bfm_sw: UartBfmSwAPI = UartBfmSwAPI([u_uart_bfm])

    sw_image = cocotb.plusargs["sw.image"]
    u_dbg_bfm.load_elf(sw_image)

    u_dbg_bfm.register_export_api(UartBfmSwAPI)
    u_dbg_bfm.set_export_impl(UartBfmSwAPI, uart_bfm_sw)

    print("Note: loading image " + sw_image)
    with open(sw_image, "rb") as f:
        elffile = ELFFile(f)

        symtab = elffile.get_section_by_name('.symtab')

        # Find the section that contains the data we need
        section = None
        for i in range(elffile.num_sections()):
            shdr = elffile._get_section_header(i)
            #            print("sh_addr=" + hex(shdr['sh_addr']) + " sh_size=" + hex(shdr['sh_size']) + " flags=" + hex(shdr['sh_flags']))
            #            print("  keys=" + str(shdr.keys()))
            if shdr['sh_size'] != 0 and (shdr['sh_flags'] & 0x2) == 0x2:
                section = elffile.get_section(i)
                data = section.data()
                addr = shdr['sh_addr']
                j = 0
                while j < len(data):
                    word = (data[j + 0] << (8 * 0))
                    word |= (data[j + 1] <<
                             (8 * 1)) if j + 1 < len(data) else 0
                    word |= (data[j + 2] <<
                             (8 * 2)) if j + 2 < len(data) else 0
                    word |= (data[j + 3] <<
                             (8 * 3)) if j + 3 < len(data) else 0
                    #                    print("Write: " + hex(addr) + "(" + hex(int((addr & 0xFFFFF)/4)) + ") " + hex(word))
                    u_bram.write_nb(int((addr & 0xFFFFF) / 4), word, 0xF)
                    addr += 4
                    j += 4

    # Wait for the main function to exit
    print("--> wait main")
    await u_dbg_bfm.on_exit("main")
    print("<-- wait main")

    # Wait for all objections to be dropped
    await pybfms.objection.inst().wait()
Ejemplo n.º 4
0
    def check(self):
        status = True
        sw_image = hpi.get_plusarg("SW_IMAGE")
        print("SW_IMAGE=" + sw_image)

        with open(sw_image, "rb") as f:
            elffile = ELFFile(f)

            symtab = elffile.get_section_by_name('.symtab')
            start_expected = symtab.get_symbol_by_name(
                "start_expected")[0]["st_value"]
            end_expected = symtab.get_symbol_by_name(
                "end_expected")[0]["st_value"]

            section = None
            for i in range(elffile.num_sections()):
                shdr = elffile._get_section_header(i)
                if (start_expected >= shdr['sh_addr']) and (
                        end_expected <= (shdr['sh_addr'] + shdr['sh_size'])):
                    start_expected -= shdr['sh_addr']
                    end_expected -= shdr['sh_addr']
                    section = elffile.get_section(i)
                    break

            data = section.data()

            exp_l = []

            for i in range(start_expected, end_expected, 8):
                reg = data[i + 0] | (data[i + 1] << 8) | (
                    data[i + 2] << 16) | (data[i + 3] << 24)
                exp = data[i + 4] | (data[i + 5] << 8) | (
                    data[i + 6] << 16) | (data[i + 7] << 24)

                exp_l.append([reg, exp])

            # Now, check results
            for exp in exp_l:
                print("Expect: R[" + str(exp[0]) + "] = " + str(exp[1]))

            for exp in exp_l:
                if not self.regs[exp[0]][1]:
                    print("Error: R[" + str(exp[0]) + "] not written")
                    status = False

                if self.regs[exp[0]][0] != exp[1]:
                    print("Error: R[" + str(exp[0]) + "] has unexpected value")

        return status
Ejemplo n.º 5
0
    def check(self):
        reg_data = []

        sw_image = cocotb.plusargs["SW_IMAGE"]
        testname = cocotb.plusargs["TESTNAME"]
        print("SW_IMAGE=" + sw_image)

        with open(sw_image, "rb") as f:
            elffile = ELFFile(f)

            symtab = elffile.get_section_by_name('.symtab')
            start_expected = symtab.get_symbol_by_name(
                "start_expected")[0]["st_value"]
            end_expected = symtab.get_symbol_by_name(
                "end_expected")[0]["st_value"]

            section = None
            for i in range(elffile.num_sections()):
                shdr = elffile._get_section_header(i)
                if (start_expected >= shdr['sh_addr']) and (
                        end_expected <= (shdr['sh_addr'] + shdr['sh_size'])):
                    start_expected -= shdr['sh_addr']
                    end_expected -= shdr['sh_addr']
                    section = elffile.get_section(i)
                    break

            data = section.data()

            exp_l = []

            for i in range(start_expected, end_expected, 8):
                reg = data[i + 0] | (data[i + 1] << 8) | (
                    data[i + 2] << 16) | (data[i + 3] << 24)
                exp = data[i + 4] | (data[i + 5] << 8) | (
                    data[i + 6] << 16) | (data[i + 7] << 24)

                exp_l.append([reg, exp])

        for i in range(64):
            info = yield self.tracer_bfm.get_reg_info(i)
            reg_data.append(info)

        if not self.complete:
            print("FAIL: " + testname)
        else:
            print("PASS: " + testname)
Ejemplo n.º 6
0
async def test(top):
    await pybfms.init()

    u_bram = pybfms.find_bfm(".*u_bram")
    u_dbg_bfm: RiscvDebugBfm = pybfms.find_bfm(".*u_dbg_bfm")

    sw_image = cocotb.plusargs["sw.image"]
    u_dbg_bfm.load_elf(sw_image)

    print("Note: loading image " + sw_image)
    with open(sw_image, "rb") as f:
        elffile = ELFFile(f)

        # Find the section that contains the data we need
        section = None
        for i in range(elffile.num_sections()):
            shdr = elffile._get_section_header(i)
            #            print("sh_addr=" + hex(shdr['sh_addr']) + " sh_size=" + hex(shdr['sh_size']) + " flags=" + hex(shdr['sh_flags']))
            #            print("  keys=" + str(shdr.keys()))
            print("sh_size=" + hex(shdr['sh_size']) + " sh_flags=" +
                  hex(shdr['sh_flags']))
            if shdr['sh_size'] != 0 and (shdr['sh_flags'] & 0x2) == 0x2:
                section = elffile.get_section(i)
                data = section.data()
                addr = shdr['sh_addr']
                j = 0
                while j < len(data):
                    word = (data[j + 0] << (8 * 0))
                    word |= (data[j + 1] <<
                             (8 * 1)) if j + 1 < len(data) else 0
                    word |= (data[j + 2] <<
                             (8 * 2)) if j + 2 < len(data) else 0
                    word |= (data[j + 3] <<
                             (8 * 3)) if j + 3 < len(data) else 0
                    print("Write: " + hex(addr) + "(" +
                          hex(int((addr & 0xFFFFF) / 4)) + ") " + hex(word))
                    u_bram.write_nb(int((addr & 0xFFFFF) / 4), word, 0xF)
                    addr += 4
                    j += 4

    print("Hello")
    print("--> wait main")
    await u_dbg_bfm.on_exit("done")
    print("<-- wait main")
Ejemplo n.º 7
0
#!/usr/bin/env python3

import sys
import struct
import zlib
from elftools.elf.elffile import ELFFile

with open(sys.argv[1], "r+b") as f:
    elf = ELFFile(f)
    elf.get_section_by_name("")
    sec_index = elf._section_name_map["compressed"]
    section = elf._get_section_header(sec_index)

    section_offset = section["sh_offset"]
    section_size = section["sh_size"]

    sec_size_index = elf._section_name_map["compressed_size"]
    size_section = elf._get_section_header(sec_size_index)
    size_section_offset = size_section["sh_offset"]

    f.seek(section_offset)
    section_data = f.read(section_size)

    compressed_data = zlib.compress(section_data, level=9)
    if len(compressed_data) > section_size:
        print(len(compressed_data), len(section_data))
        raise RuntimeError("Smeh")
    f.seek(section_offset)
    f.write(compressed_data)
    f.write(b"\0" * (section_size - len(compressed_data)))
Ejemplo n.º 8
0
async def test(top):
    await pybfms.init()

    u_bram = pybfms.find_bfm(".*u_bram")
    u_dbg_bfm: RiscvDebugBfm = pybfms.find_bfm(".*u_dbg_bfm")

    sw_image = cocotb.plusargs["sw.image"]
    u_dbg_bfm.load_elf(sw_image)
    u_dbg_bfm.set_trace_level(RiscvDebugTraceLevel.All)

    ram_console = 0
    ram_console_sz = 0

    print("Note: loading image " + sw_image)
    with open(sw_image, "rb") as f:
        elffile = ELFFile(f)

        symtab = elffile.get_section_by_name('.symtab')

        ram_console = 0
        ram_console_sz = 0
        if symtab.get_symbol_by_name("ram_console") is not None:
            ram_console = symtab.get_symbol_by_name(
                "ram_console")[0]["st_value"]
            ram_console_sz = symtab.get_symbol_by_name(
                "CONFIG_RAM_CONSOLE_BUFFER_SIZE")[0]["st_value"]

        # Find the section that contains the data we need
        section = None
        for i in range(elffile.num_sections()):
            shdr = elffile._get_section_header(i)
            #            print("sh_addr=" + hex(shdr['sh_addr']) + " sh_size=" + hex(shdr['sh_size']) + " flags=" + hex(shdr['sh_flags']))
            #            print("  keys=" + str(shdr.keys()))
            if shdr['sh_size'] != 0 and (shdr['sh_flags'] & 0x2) == 0x2:
                section = elffile.get_section(i)
                data = section.data()
                addr = shdr['sh_addr']
                j = 0
                while j < len(data):
                    word = (data[j + 0] << (8 * 0))
                    word |= (data[j + 1] <<
                             (8 * 1)) if j + 1 < len(data) else 0
                    word |= (data[j + 2] <<
                             (8 * 2)) if j + 2 < len(data) else 0
                    word |= (data[j + 3] <<
                             (8 * 3)) if j + 3 < len(data) else 0
                    #                    print("Write: " + hex(addr) + "(" + hex(int((addr & 0xFFFFF)/4)) + ") " + hex(word))
                    u_bram.write_nb(int((addr & 0xFFFFF) / 4), word, 0xF)
                    addr += 4
                    j += 4

    if ram_console != 0:
        console = RamConsole(ram_console, ram_console_sz)
        u_dbg_bfm.add_memwrite_cb(console.memwrite)

    # Wait for the main function to exit
    print("--> wait main")
    await u_dbg_bfm.on_exit("main")
    print("<-- wait main")

    # Wait for the OS to go idle
    print("--> wait idle")
    await u_dbg_bfm.on_entry("idle")
    print("<-- wait idle")

    # Wait for all objections to be dropped
    await pybfms.objection.inst().wait()