Core: Flatten anonymous struct/union members during vmlinux parsing

On this kernel `struct pt_regs` wraps `cs` and `ss` in anonymous unions.
ctypes lifts those names onto the parent class, exactly as C does, but the
parser only walked the top-level `_fields_`, so `cs` and `ss` were unknown to
the compiler and `ctx.cs` blew up.

Register the members of an anonymous member as fields of the parent, one
level deep, taking the ABSOLUTE offset ctypes reports on the parent (no
arithmetic) and recording the access path (anonymous member index, member
index). The anonymous member itself stays registered as before.

Deliberately conservative: only scalar ctypes members are flattened. Nested
vmlinux structs (`fred_cs`, `fred_ss`) and bitfields are skipped with a
warning rather than half-registered, and a flattened member never overwrites
an existing top-level field of the same name. Overlapping members are kept as
distinct fields, since `cs` and `csx` both legitimately live at offset 136.

Flattened fields are not yet emitted: IR generation and DWARF generation skip
them, and critically they do not consume a top-level field index, so existing
CO-RE names are untouched. Every vmlinux/xdp test program still compiles to a
byte-identical .ll. Entirely inert for any struct without `_anonymous_`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Pragyansh Chaturvedi
2026-08-07 04:54:17 +05:30
parent aa51d123de
commit f2ec70759e
3 changed files with 133 additions and 1 deletions

View File

@ -43,7 +43,13 @@ def debug_info_generation(
# Process all fields and create members for the struct
members = []
sorted_fields = sorted(struct.fields.items(), key=lambda item: item[1].offset)
# Members lifted out of an anonymous member are not members of this struct in
# DWARF terms; they belong to the anonymous member's own type. Emitting them
# here would both duplicate offsets and shift every later member's index.
sorted_fields = sorted(
(item for item in struct.fields.items() if item[1].access_path is None),
key=lambda item: item[1].offset,
)
for field_name, field in sorted_fields:
try:

View File

@ -85,6 +85,10 @@ class IRGenerator:
# Create a members dictionary for AssignmentInfo
members_dict = {}
for field_name, field in struct.fields.items():
if field.access_path is not None:
# Member lifted out of an anonymous member. gen_ir does
# not emit a global for it yet, so it stays unexported.
continue
# Get the generated field name from our dictionary, or use field_name if not found
if (
struct.name in self.generated_field_names
@ -132,6 +136,10 @@ class IRGenerator:
self.generated_field_names[struct.name] = {}
for field_name, field in struct.fields.items():
if field.access_path is not None:
# Member lifted out of an anonymous member. It is not a
# top-level field, so it must not consume a field index.
continue
# does not take arrays and similar types into consideration yet.
if callable(field.ctype_complex_type):
# Function pointer case - generate a simple field accessor