Victor Campos 119aecb955
[DebugInfo] Emit negative DW_AT_bit_offset in explicit signed form (#87994)
Before this patch, the value of DW_AT_bit_offset, used for bitfields
before DWARF version 4, was always emitted as an unsigned integer using
the form DW_FORM_data<n>. If the value was originally a signed integer,
for instance in the case of negative offsets, it was up to debug
information consumers to re-cast it to a signed integer.

This is problematic since the burden of deciding if the value should be
read as signed or unsigned was put onto the debug info consumers: the
DWARF specification doesn't define DW_AT_bit_offset's underlying type.
If a debugger decided to interpret this attribute in the form data<n> as
unsigned, then negative offsets would be completely broken.

The DWARF specification version 3 mentions in the Data Representation
section, page 127:

> If one of the DW_FORM_data<n> forms is used to represent a signed or
unsigned integer, it can be hard for a consumer to discover the context
necessary to determine which interpretation is intended. Producers are
therefore strongly encouraged to use DW_FORM_sdata or DW_FORM_udata for
signed and unsigned integers respectively, rather than DW_FORM_data<n>.

Therefore, the proposal is to use DW_FORM_sdata, which is explicitly
signed. This is an indication to consumers that the offset must be
parsed unambiguously as a signed integer.

Finally, gcc already uses DW_FORM_sdata for negative offsets, fixing the
potential ambiguity altogether.

This patch mimics gcc's behaviour by emitting negative values of
DW_AT_bit_offset using the DW_FORM_sdata form. This eliminates any
potential misinterpretation.

One could argue that all values should use DW_FORM_sdata, but for the
sake of parity with gcc, it is safe to restrict the change to negative
values.
2024-05-13 11:14:35 +01:00

45 lines
2.0 KiB
LLVM

; RUN: llc -mtriple=thumbv7-apple-ios -O0 -filetype=obj -o %t.o %s
; RUN: llvm-dwarfdump -v -debug-info %t.o | FileCheck %s
;
; Generated from:
; struct {
; char c;
; int : 4;
; int reserved : 28;
; } a;
;
; CHECK: DW_TAG_member
; CHECK: DW_AT_name {{.*}} "reserved"
; CHECK: DW_AT_byte_size {{.*}} (0x04)
; CHECK: DW_AT_bit_size {{.*}} (0x1c)
; CHECK: DW_AT_bit_offset {{.*}} (-8)
; CHECK: DW_AT_data_member_location {{.*}} (DW_OP_plus_uconst 0x0)
%struct.anon = type { i8, [5 x i8] }
@a = common global %struct.anon zeroinitializer, align 1, !dbg !0
!llvm.dbg.cu = !{!2}
!llvm.module.flags = !{!12, !13, !14, !15, !16}
!llvm.ident = !{!17}
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 5, type: !6, isLocal: false, isDefinition: true)
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 3.7.0 (trunk 240548) (llvm/trunk 240554)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, retainedTypes: !4, globals: !5, imports: !4)
!3 = !DIFile(filename: "test.i", directory: "/")
!4 = !{}
!5 = !{!0}
!6 = !DICompositeType(tag: DW_TAG_structure_type, file: !3, line: 1, size: 48, align: 8, elements: !7)
!7 = !{!8, !10}
!8 = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: !6, file: !3, line: 2, baseType: !9, size: 8, align: 8)
!9 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char)
!10 = !DIDerivedType(tag: DW_TAG_member, name: "reserved", scope: !6, file: !3, line: 4, baseType: !11, size: 28, align: 32, offset: 12, flags: DIFlagBitField)
!11 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
!12 = !{i32 2, !"Dwarf Version", i32 2}
!13 = !{i32 2, !"Debug Info Version", i32 3}
!14 = !{i32 1, !"wchar_size", i32 4}
!15 = !{i32 1, !"min_enum_size", i32 4}
!16 = !{i32 1, !"PIC Level", i32 2}
!17 = !{!"clang version 3.7.0 (trunk 240548) (llvm/trunk 240554)"}