Skip to content

Commit

Permalink
Fix for parsing error encountered when pyelftools tries to parse non …
Browse files Browse the repository at this point in the history
…GNU notes that conflicting with known GNU note types. (#584)

Co-authored-by: Tomatillos <>
  • Loading branch information
gettomatillos authored Dec 7, 2024
1 parent d6d7353 commit 2e70218
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
11 changes: 7 additions & 4 deletions elftools/elf/descriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,21 @@ def describe_note(x, machine):
n_desc = x['n_desc']
desc = ''
if x['n_type'] == 'NT_GNU_ABI_TAG':
if x['n_name'] == 'Android':
desc = '\n description data: %s ' % bytes2hex(x['n_descdata'])
else:
if x['n_name'] == 'GNU':
desc = '\n OS: %s, ABI: %d.%d.%d' % (
_DESCR_NOTE_ABI_TAG_OS.get(n_desc['abi_os'], _unknown),
n_desc['abi_major'], n_desc['abi_minor'], n_desc['abi_tiny'])
else:
desc = '\n description data: %s ' % bytes2hex(x['n_descdata'])
elif x['n_type'] == 'NT_GNU_BUILD_ID':
desc = '\n Build ID: %s' % (n_desc)
elif x['n_type'] == 'NT_GNU_GOLD_VERSION':
desc = '\n Version: %s' % (n_desc)
elif x['n_type'] == 'NT_GNU_PROPERTY_TYPE_0':
desc = '\n Properties: ' + describe_note_gnu_properties(x['n_desc'], machine)
if x['n_name'] == 'GNU':
desc = '\n Properties: ' + describe_note_gnu_properties(x['n_desc'], machine)
else:
desc = '\n description data: %s ' % bytes2hex(x['n_descdata'])
else:
desc = '\n description data: {}'.format(bytes2hex(n_desc))

Expand Down
8 changes: 4 additions & 4 deletions elftools/elf/notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def iter_notes(elffile, offset, size):

desc_data = elffile.stream.read(note['n_descsz'])
note['n_descdata'] = desc_data
if note['n_type'] == 'NT_GNU_ABI_TAG':
if note['n_type'] == 'NT_GNU_ABI_TAG' and note['n_name'] == 'GNU':
note['n_desc'] = struct_parse(elffile.structs.Elf_abi,
elffile.stream,
offset)
elif note['n_type'] == 'NT_GNU_BUILD_ID':
elif note['n_type'] == 'NT_GNU_BUILD_ID' and note['n_name'] == 'GNU':
note['n_desc'] = bytes2hex(desc_data)
elif note['n_type'] == 'NT_GNU_GOLD_VERSION':
elif note['n_type'] == 'NT_GNU_GOLD_VERSION' and note['n_name'] == 'GNU':
note['n_desc'] = bytes2str(desc_data)
elif note['n_type'] == 'NT_PRPSINFO':
note['n_desc'] = struct_parse(elffile.structs.Elf_Prpsinfo,
Expand All @@ -49,7 +49,7 @@ def iter_notes(elffile, offset, size):
note['n_desc'] = struct_parse(elffile.structs.Elf_Nt_File,
elffile.stream,
offset)
elif note['n_type'] == 'NT_GNU_PROPERTY_TYPE_0':
elif note['n_type'] == 'NT_GNU_PROPERTY_TYPE_0' and note['n_name'] == 'GNU':
off = offset
props = []
# n_descsz contains the size of the note "descriptor" (the data payload),
Expand Down
9 changes: 9 additions & 0 deletions test/test_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ def test_note_segment_with_8_byte_alignment(self):
notes = list(note_sections[0].iter_notes())
# There's one note in this section:
self.assertEqual(len(notes), 1)

def test_note_tc3xx_blinky(self):
with ELFFile.load_from_path(os.path.join('test', 'testfiles_for_unittests', 'note_tc3xxx_blinky.elf')) as elf:
note_sections = [section for section in elf.iter_sections() if isinstance(section, NoteSection)]
# There's only one note section in this file:
self.assertEqual(len(note_sections), 1)
notes = list(note_sections[0].iter_notes())
# There's one note in this section:
self.assertEqual(len(notes), 522)
Binary file not shown.

0 comments on commit 2e70218

Please sign in to comment.