[binutils-gdb] [gdb/contrib] Fix ELFParseError in dwarf-to-dwarf-assembler.py

Tom de Vries via Gdb-cvs <[email protected]> Wed, 10 Jun 2026 07:54:36 +0000 (GMT)
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3feb42142005c63ac424a06d1b4d8a3b667d63e8

commit 3feb42142005c63ac424a06d1b4d8a3b667d63e8
Author: Tom de Vries <[email protected]>
Date:   Wed Jun 10 09:54:22 2026 +0200

    [gdb/contrib] Fix ELFParseError in dwarf-to-dwarf-assembler.py
    
    When running dwarf-to-dwarf-assembler.py on
    outputs/gdb.dwarf2/implptr-64bit/implptr-64bit-d2-o8-a4-r4-t0, I run into:
    ...
    elftools.common.exceptions.ELFParseError: expected 8, found 5
    ...
    for the DWARF:
    ...
     <2><335>: Abbrev Number: 8 (DW_TAG_variable)
        <336>   DW_AT_name        : p
        <338>   DW_AT_location    : 6 byte block: f2 14 3 0 0 0 \
                                    (DW_OP_GNU_implicit_pointer: <0x314> 0)
    ...
    
    Pyelftools expects that DW_OP_GNU_implicit_pointer has an 8-byte reference
    operand (because it's a 64-bit DWARF unit), but instead it has a 4-byte
    reference.
    
    The DWARF is correct, it's a cornercase of using DW_OP_GNU_implicit_pointer in
    DWARF v2, where DW_FORM_ref_addr references have the same size as an address
    on the target system, something that was changed in DWARF v3.
    
    Handle this by catching the error and printing:
    ...
    DW_AT_location "\xf2\x14\x03\x00\x00\x00" DW_FORM_block \
      # Failed to print DWARF expression: expected 8, found 5
    ...
    
    Approved-By: Tom Tromey <[email protected]>

Diff:
---
 gdb/contrib/dwarf-to-dwarf-assembler.py | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/gdb/contrib/dwarf-to-dwarf-assembler.py b/gdb/contrib/dwarf-to-dwarf-assembler.py
index fa32bc4bbab..d47355e0a7c 100755
--- a/gdb/contrib/dwarf-to-dwarf-assembler.py
+++ b/gdb/contrib/dwarf-to-dwarf-assembler.py
@@ -48,6 +48,7 @@ from io import BytesIO, IOBase
 from logging import getLogger
 from typing import Annotated, Optional
 
+from elftools.common.exceptions import ELFParseError
 from elftools.construct.lib.container import ListContainer
 from elftools.dwarf.compileunit import CompileUnit as RawCompileUnit
 from elftools.dwarf.die import DIE as RawDIE
@@ -238,6 +239,9 @@ class DWARFAttribute:
                     postfix = " # Failed to print op as DWARF expression: " + hex(
                         int(str(e))
                     )
+                except ELFParseError as e:
+                    # Fall through to basic printing.
+                    postfix = " # Failed to print DWARF expression: " + str(e)
             # Print the data as a string in "\x01\x02\x03\x04" format.
             result = ""
             for op in self.value: