1 #-------------------------------------------------------------------------------
2 # elftools: elf/notes.py
6 # Eli Bendersky (eliben@gmail.com)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
9 from ..common
.utils
import struct_parse
, bytes2hex
, roundup
, bytes2str
10 from ..construct
import CString
13 def iter_notes(elffile
, offset
, size
):
14 """ Yield all the notes in a section or segment.
19 elffile
.structs
.Elf_Nhdr
,
22 note
['n_offset'] = offset
23 offset
+= elffile
.structs
.Elf_Nhdr
.sizeof()
24 elffile
.stream
.seek(offset
)
25 # n_namesz is 4-byte aligned.
26 disk_namesz
= roundup(note
['n_namesz'], 2)
27 note
['n_name'] = bytes2str(
28 CString('').parse(elffile
.stream
.read(disk_namesz
)))
31 desc_data
= elffile
.stream
.read(note
['n_descsz'])
32 note
['n_descdata'] = desc_data
33 if note
['n_type'] == 'NT_GNU_ABI_TAG':
34 note
['n_desc'] = struct_parse(elffile
.structs
.Elf_abi
,
37 elif note
['n_type'] == 'NT_GNU_BUILD_ID':
38 note
['n_desc'] = bytes2hex(desc_data
)
39 elif note
['n_type'] == 'NT_GNU_GOLD_VERSION':
40 note
['n_desc'] = bytes2str(desc_data
)
41 elif note
['n_type'] == 'NT_PRPSINFO':
42 note
['n_desc'] = struct_parse(elffile
.structs
.Elf_Prpsinfo
,
45 elif note
['n_type'] == 'NT_FILE':
46 note
['n_desc'] = struct_parse(elffile
.structs
.Elf_Nt_File
,
49 elif note
['n_type'] == 'NT_GNU_PROPERTY_TYPE_0':
53 p
= struct_parse(elffile
.structs
.Elf_Prop
, elffile
.stream
, off
)
54 off
+= roundup(p
.pr_datasz
+ 8, 2 if elffile
.elfclass
== 32 else 3)
56 note
['n_desc'] = props
58 note
['n_desc'] = desc_data
59 offset
+= roundup(note
['n_descsz'], 2)
60 note
['n_size'] = offset
- note
['n_offset']