[Dwarf-discuss] Location expressions for partially-optimized-out structs

Kyle Huey khuey@pernos.co
Wed Feb 28 05:10:49 GMT 2024


This originates from https://github.com/rust-lang/rust/issues/46698

This Rust program has a function that takes a struct type as an
argument. The compiler is clever enough to rewrite the function so
that a subset of the struct's fields are passed as arguments at the
machine code level (in this case, the struct  consists of two 8 byte
fields and only the second is needed). LLVM emits the following
DW_AT_location expression for the argument:

DW_OP_piece 0x8, DW_OP_reg5 RDI, DW_OP_piece 0x8

The DWARF spec does not explicitly contemplate this sequence, merely saying:

The DW_OP_piece operation takes a single operand, which is an unsigned
LEB128 number. The number describes the size in bytes of the piece of the
object referenced by the preceding simple location description. If the piece is
located in a register, but does not occupy the entire register, the placement of
the piece within that register is defined by the ABI.

The text for DW_OP_bit_piece, on the other hand, does explicitly
contemplate this, containing the language:

If the location description is empty, the offset doesn’t matter and
the DW_OP_bit_piece operation describes a piece consisting of the
given number of bits whose values are undefined.

I propose adding similar language to the description of DW_OP_piece. e.g.

The DW_OP_piece operation takes a single operand, which is an unsigned
LEB128 number. The number describes the size in bytes of the piece of the
object referenced by the preceding simple location description. If the
location description is empty the DW_OP_piece operation describes a
piece consisting of the given number of bytes whose values are
undefined. If the piece is
located in a register, but does not occupy the entire register, the placement of
the piece within that register is defined by the ABI.

(In practical use, gdb simply errors out on that location expression,
while lldb substitutes the undefined bytes with zeroes, neither of
which are correct IMO.)

- Kyle


More information about the Dwarf-discuss mailing list