Ejemplo n.º 1
0
def compile_dwarf(dies, little_endian=True, bits=64):
    if isinstance(dies, DwarfDie):
        dies = (dies, )
    assert all(isinstance(die, DwarfDie) for die in dies)
    cu_die = DwarfDie(DW_TAG.compile_unit, [
        DwarfAttrib(DW_AT.comp_dir, DW_FORM.string, '/usr/src'),
        DwarfAttrib(DW_AT.stmt_list, DW_FORM.sec_offset, 0),
    ], dies)

    return create_elf_file(ET.EXEC, [
        ElfSection(
            name='.debug_abbrev',
            sh_type=SHT.PROGBITS,
            data=_compile_debug_abbrev(cu_die),
        ),
        ElfSection(
            name='.debug_info',
            sh_type=SHT.PROGBITS,
            data=_compile_debug_info(cu_die, little_endian, bits),
        ),
        ElfSection(
            name='.debug_line',
            sh_type=SHT.PROGBITS,
            data=_compile_debug_line(cu_die, little_endian),
        ),
        ElfSection(
            name='.debug_str',
            sh_type=SHT.PROGBITS,
            data=b'\0',
        ),
    ],
                           little_endian=little_endian,
                           bits=bits)
Ejemplo n.º 2
0
 def test_simple(self):
     data = b"hello, world"
     prog = Program()
     with tempfile.NamedTemporaryFile() as f:
         f.write(
             create_elf_file(
                 ET.CORE,
                 [ElfSection(p_type=PT.LOAD, vaddr=0xFFFF0000, data=data)]))
         f.flush()
         prog.set_core_dump(f.name)
     self.assertEqual(prog.read(0xFFFF0000, len(data)), data)
     self.assertRaises(FaultError, prog.read, 0x0, len(data), physical=True)
Ejemplo n.º 3
0
def compile_dwarf(
    dies, little_endian=True, bits=64, *, lang=None, use_dw_form_indirect=False
):
    return create_elf_file(
        ET.EXEC,
        dwarf_sections(
            dies,
            little_endian=little_endian,
            bits=bits,
            lang=lang,
            use_dw_form_indirect=use_dw_form_indirect,
        ),
        little_endian=little_endian,
        bits=bits,
    )
Ejemplo n.º 4
0
def compile_dwarf(dies, little_endian=True, bits=64, *, lang=None):
    if isinstance(dies, DwarfDie):
        dies = (dies, )
    assert all(isinstance(die, DwarfDie) for die in dies)
    cu_attribs = [
        DwarfAttrib(DW_AT.comp_dir, DW_FORM.string, "/usr/src"),
        DwarfAttrib(DW_AT.stmt_list, DW_FORM.sec_offset, 0),
    ]
    if lang is not None:
        cu_attribs.append(DwarfAttrib(DW_AT.language, DW_FORM.data1, lang))
    cu_die = DwarfDie(DW_TAG.compile_unit, cu_attribs, dies)

    return create_elf_file(
        ET.EXEC,
        [
            ElfSection(
                p_type=PT.LOAD,
                vaddr=0xFFFF0000,
                data=b"",
            ),
            ElfSection(
                name=".debug_abbrev",
                sh_type=SHT.PROGBITS,
                data=_compile_debug_abbrev(cu_die),
            ),
            ElfSection(
                name=".debug_info",
                sh_type=SHT.PROGBITS,
                data=_compile_debug_info(cu_die, little_endian, bits),
            ),
            ElfSection(
                name=".debug_line",
                sh_type=SHT.PROGBITS,
                data=_compile_debug_line(cu_die, little_endian),
            ),
            ElfSection(
                name=".debug_str",
                sh_type=SHT.PROGBITS,
                data=b"\0",
            ),
        ],
        little_endian=little_endian,
        bits=bits,
    )
Ejemplo n.º 5
0
def create_elf_symbol_file(symbols):
    # We need some DWARF data so that libdwfl will load the file.
    sections = dwarf_sections(())
    # Create a section for the symbols to reference and the corresponding
    # segment for address lookups.
    min_address = min(symbol.value for symbol in symbols)
    max_address = max(symbol.value + symbol.size for symbol in symbols)
    sections.append(
        ElfSection(
            name=".foo",
            sh_type=SHT.NOBITS,
            p_type=PT.LOAD,
            vaddr=min_address,
            memsz=max_address - min_address,
        ))
    symbols = [
        symbol._replace(shindex=len(sections)
                        if symbol.shindex is None else symbol.shindex)
        for symbol in symbols
    ]
    return create_elf_file(ET.EXEC, sections, symbols)