Skip to content

Elf

Elf

Bases: object

Minimal wrapper around pyelftools to easily inspect ELF files.

Source code in util/sim/Elf.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Elf(object):
    """Minimal wrapper around `pyelftools` to easily inspect ELF files."""

    def __init__(self, elf_path):
        """Default constructor.

        Arguments:
            elf_path: Path to an ELF binary.
        """
        self.elf_path = elf_path
        self.stream = open(self.elf_path, 'rb')
        self.elf = ELFFile(self.stream)
        # Get symbol table
        section = self.elf.get_section_by_name('.symtab')
        if not section or not isinstance(section, SymbolTableSection):
            print('No symbol table found. Perhaps this ELF has been stripped?')
            self.symtab = None
        else:
            self.symtab = section

    def get_symbol_address(self, uid):
        """Returns the address of a global symbol.

        Arguments:
            uid: A global symbol.
        """
        symbol = self.symtab.get_symbol_by_name(uid)[0]
        return symbol.entry["st_value"]

    def get_symbol_size(self, uid):
        """Returns the size of a global symbol.

        Arguments:
            uid: A global symbol.
        """
        symbol = self.symtab.get_symbol_by_name(uid)[0]
        return symbol.entry["st_size"]

    def get_raw_symbol_contents(self, uid):
        """Returns a bytearray with the contents of a global symbol.

        Arguments:
            uid: A global symbol.
        """
        addr = self.get_symbol_address(uid)
        size = self.get_symbol_size(uid)
        try:
            fpos = list(self.elf.address_offsets(addr, size))[0]
            self.elf.stream.seek(fpos)
            contents = self.elf.stream.read(size)
        except IndexError:
            # We assume all segments in our ELF are of type PT_LOAD and
            # that the only section whose contents are not stored in
            # the ELF file is the .bss section. Therefore, whenever
            # `address_offsets()` fails to return a valid offset into the
            # file we assume that the address falls in the .bss section.
            contents = bytearray([0] * size)
        return contents

    def from_symbol(self, uid, ctype):
        """Returns an array with the contents of a global symbol.

        The array is formatted from the raw byte contents returned by
        [`get_raw_symbol_contents()`][Elf.Elf.get_raw_symbol_contents]
        using the [`from_buffer()`][data_utils.from_buffer] function.

        Arguments:
            uid: A global symbol.
            ctype: C type identifier passed on to the
                [`from_buffer()`][data_utils.from_buffer] function.
        """
        return from_buffer(self.get_raw_symbol_contents(uid), ctype)

__init__(elf_path)

Default constructor.

Parameters:

Name Type Description Default
elf_path

Path to an ELF binary.

required
Source code in util/sim/Elf.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def __init__(self, elf_path):
    """Default constructor.

    Arguments:
        elf_path: Path to an ELF binary.
    """
    self.elf_path = elf_path
    self.stream = open(self.elf_path, 'rb')
    self.elf = ELFFile(self.stream)
    # Get symbol table
    section = self.elf.get_section_by_name('.symtab')
    if not section or not isinstance(section, SymbolTableSection):
        print('No symbol table found. Perhaps this ELF has been stripped?')
        self.symtab = None
    else:
        self.symtab = section

from_symbol(uid, ctype)

Returns an array with the contents of a global symbol.

The array is formatted from the raw byte contents returned by get_raw_symbol_contents() using the from_buffer() function.

Parameters:

Name Type Description Default
uid

A global symbol.

required
ctype

C type identifier passed on to the from_buffer() function.

required
Source code in util/sim/Elf.py
71
72
73
74
75
76
77
78
79
80
81
82
83
def from_symbol(self, uid, ctype):
    """Returns an array with the contents of a global symbol.

    The array is formatted from the raw byte contents returned by
    [`get_raw_symbol_contents()`][Elf.Elf.get_raw_symbol_contents]
    using the [`from_buffer()`][data_utils.from_buffer] function.

    Arguments:
        uid: A global symbol.
        ctype: C type identifier passed on to the
            [`from_buffer()`][data_utils.from_buffer] function.
    """
    return from_buffer(self.get_raw_symbol_contents(uid), ctype)

get_raw_symbol_contents(uid)

Returns a bytearray with the contents of a global symbol.

Parameters:

Name Type Description Default
uid

A global symbol.

required
Source code in util/sim/Elf.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def get_raw_symbol_contents(self, uid):
    """Returns a bytearray with the contents of a global symbol.

    Arguments:
        uid: A global symbol.
    """
    addr = self.get_symbol_address(uid)
    size = self.get_symbol_size(uid)
    try:
        fpos = list(self.elf.address_offsets(addr, size))[0]
        self.elf.stream.seek(fpos)
        contents = self.elf.stream.read(size)
    except IndexError:
        # We assume all segments in our ELF are of type PT_LOAD and
        # that the only section whose contents are not stored in
        # the ELF file is the .bss section. Therefore, whenever
        # `address_offsets()` fails to return a valid offset into the
        # file we assume that the address falls in the .bss section.
        contents = bytearray([0] * size)
    return contents

get_symbol_address(uid)

Returns the address of a global symbol.

Parameters:

Name Type Description Default
uid

A global symbol.

required
Source code in util/sim/Elf.py
32
33
34
35
36
37
38
39
def get_symbol_address(self, uid):
    """Returns the address of a global symbol.

    Arguments:
        uid: A global symbol.
    """
    symbol = self.symtab.get_symbol_by_name(uid)[0]
    return symbol.entry["st_value"]

get_symbol_size(uid)

Returns the size of a global symbol.

Parameters:

Name Type Description Default
uid

A global symbol.

required
Source code in util/sim/Elf.py
41
42
43
44
45
46
47
48
def get_symbol_size(self, uid):
    """Returns the size of a global symbol.

    Arguments:
        uid: A global symbol.
    """
    symbol = self.symtab.get_symbol_by_name(uid)[0]
    return symbol.entry["st_size"]