The debug-info metadata can provide that kind of information, although maybe not as directly as you would prefer.
@a = global %struct.A zeroinitializer, align 4, !dbg !0
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 6, type: !5, isLocal: false, isDefinition: true)
!2 = distinct !DICompileUnit(language: DW_LANG_C11, file: !3, producer: "clang version 17.0.6 (PS5 clang version 9.00.0.501 cdbd5f6a)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, debugInfoForProfiling: true, nameTableKind: None)
!3 = !DIFile(filename: "padding.c", directory: "D:\\Dev\\ours\\scratch", checksumkind: CSK_MD5, checksum: "766809f898304fd25444f40ce70e17fa")
!4 = !{!0}
!5 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "A", file: !3, line: 2, size: 128, align: 32, elements: !6)
!6 = !{!7, !9, !11}
!7 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !5, file: !3, line: 3, baseType: !8, size: 32)
!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!9 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !5, file: !3, line: 4, baseType: !10, size: 8, offset: 32)
!10 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
!11 = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: !5, file: !3, line: 5, baseType: !12, size: 64, offset: 40)
!12 = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed)
Variable @a
points to the debug-info description at !0
. This points to the variable at !1
, which has its type at !5
, which points to the list of members, and so on.
Sizes and offsets of members are in bits. There are no member descriptions for padding, so you can derive the size and location of padding bits by what parts of the struct are not covered by members.
I am not deeply familiar with the APIs for navigating the debug info. If I were working on a project like this, I’d probably look first at the IR verifier to see how it walks the tree of debug-info metadata.