Skip to content

Commit 2e70218

Browse files
Fix for parsing error encountered when pyelftools tries to parse non GNU notes that conflicting with known GNU note types. (#584)
Co-authored-by: Tomatillos <>
1 parent d6d7353 commit 2e70218

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

elftools/elf/descriptions.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,21 @@ def describe_note(x, machine):
206206
n_desc = x['n_desc']
207207
desc = ''
208208
if x['n_type'] == 'NT_GNU_ABI_TAG':
209-
if x['n_name'] == 'Android':
210-
desc = '\n description data: %s ' % bytes2hex(x['n_descdata'])
211-
else:
209+
if x['n_name'] == 'GNU':
212210
desc = '\n OS: %s, ABI: %d.%d.%d' % (
213211
_DESCR_NOTE_ABI_TAG_OS.get(n_desc['abi_os'], _unknown),
214212
n_desc['abi_major'], n_desc['abi_minor'], n_desc['abi_tiny'])
213+
else:
214+
desc = '\n description data: %s ' % bytes2hex(x['n_descdata'])
215215
elif x['n_type'] == 'NT_GNU_BUILD_ID':
216216
desc = '\n Build ID: %s' % (n_desc)
217217
elif x['n_type'] == 'NT_GNU_GOLD_VERSION':
218218
desc = '\n Version: %s' % (n_desc)
219219
elif x['n_type'] == 'NT_GNU_PROPERTY_TYPE_0':
220-
desc = '\n Properties: ' + describe_note_gnu_properties(x['n_desc'], machine)
220+
if x['n_name'] == 'GNU':
221+
desc = '\n Properties: ' + describe_note_gnu_properties(x['n_desc'], machine)
222+
else:
223+
desc = '\n description data: %s ' % bytes2hex(x['n_descdata'])
221224
else:
222225
desc = '\n description data: {}'.format(bytes2hex(n_desc))
223226

elftools/elf/notes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ def iter_notes(elffile, offset, size):
3333

3434
desc_data = elffile.stream.read(note['n_descsz'])
3535
note['n_descdata'] = desc_data
36-
if note['n_type'] == 'NT_GNU_ABI_TAG':
36+
if note['n_type'] == 'NT_GNU_ABI_TAG' and note['n_name'] == 'GNU':
3737
note['n_desc'] = struct_parse(elffile.structs.Elf_abi,
3838
elffile.stream,
3939
offset)
40-
elif note['n_type'] == 'NT_GNU_BUILD_ID':
40+
elif note['n_type'] == 'NT_GNU_BUILD_ID' and note['n_name'] == 'GNU':
4141
note['n_desc'] = bytes2hex(desc_data)
42-
elif note['n_type'] == 'NT_GNU_GOLD_VERSION':
42+
elif note['n_type'] == 'NT_GNU_GOLD_VERSION' and note['n_name'] == 'GNU':
4343
note['n_desc'] = bytes2str(desc_data)
4444
elif note['n_type'] == 'NT_PRPSINFO':
4545
note['n_desc'] = struct_parse(elffile.structs.Elf_Prpsinfo,
@@ -49,7 +49,7 @@ def iter_notes(elffile, offset, size):
4949
note['n_desc'] = struct_parse(elffile.structs.Elf_Nt_File,
5050
elffile.stream,
5151
offset)
52-
elif note['n_type'] == 'NT_GNU_PROPERTY_TYPE_0':
52+
elif note['n_type'] == 'NT_GNU_PROPERTY_TYPE_0' and note['n_name'] == 'GNU':
5353
off = offset
5454
props = []
5555
# n_descsz contains the size of the note "descriptor" (the data payload),

test/test_notes.py

+9
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@ def test_note_segment_with_8_byte_alignment(self):
2727
notes = list(note_sections[0].iter_notes())
2828
# There's one note in this section:
2929
self.assertEqual(len(notes), 1)
30+
31+
def test_note_tc3xx_blinky(self):
32+
with ELFFile.load_from_path(os.path.join('test', 'testfiles_for_unittests', 'note_tc3xxx_blinky.elf')) as elf:
33+
note_sections = [section for section in elf.iter_sections() if isinstance(section, NoteSection)]
34+
# There's only one note section in this file:
35+
self.assertEqual(len(note_sections), 1)
36+
notes = list(note_sections[0].iter_notes())
37+
# There's one note in this section:
38+
self.assertEqual(len(notes), 522)
Binary file not shown.

0 commit comments

Comments
 (0)