File: notes.py

package info (click to toggle)
python-pyelftools 0.24-3~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 4,448 kB
  • sloc: python: 8,404; ansic: 142; makefile: 22
file content (43 lines) | stat: -rw-r--r-- 1,604 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
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
#-------------------------------------------------------------------------------
# elftools: elf/notes.py
#
# ELF notes
#
# Eli Bendersky (eliben@gmail.com)
# This code is in the public domain
#-------------------------------------------------------------------------------
from ..common.py3compat import bytes2str
from ..common.utils import struct_parse, roundup
from ..construct import CString


def iter_notes(elffile, offset, size):
    """ Yield all the notes in a section or segment.
    """
    end = offset + size
    while offset < end:
        note = struct_parse(
            elffile.structs.Elf_Nhdr,
            elffile.stream,
            stream_pos=offset)
        note['n_offset'] = offset
        offset += elffile.structs.Elf_Nhdr.sizeof()
        elffile.stream.seek(offset)
        # n_namesz is 4-byte aligned.
        disk_namesz = roundup(note['n_namesz'], 2)
        note['n_name'] = bytes2str(
            CString('').parse(elffile.stream.read(disk_namesz)))
        offset += disk_namesz

        desc_data = bytes2str(elffile.stream.read(note['n_descsz']))
        if note['n_type'] == 'NT_GNU_ABI_TAG':
            note['n_desc'] = struct_parse(elffile.structs.Elf_Nhdr_abi,
                                          elffile.stream,
                                          offset)
        elif note['n_type'] == 'NT_GNU_BUILD_ID':
            note['n_desc'] = ''.join('%.2x' % ord(b) for b in desc_data)
        else:
            note['n_desc'] = desc_data
        offset += roundup(note['n_descsz'], 2)
        note['n_size'] = offset - note['n_offset']
        yield note