Core: Emit nested CO-RE access strings for anonymous members

A field lifted out of an anonymous member needs an access string that walks
into that member, so `struct pt_regs.cs` is `llvm.pt_regs:0:136$0:17:0`
(member 0 of anonymous member 17) rather than the flat `$0:<index>` form.
This is byte-for-byte what clang emits for the equivalent C, verified against
`__builtin_preserve_access_index(ctx->cs)` on a struct with the same shape,
and the indices and offsets agree with `bpftool btf dump file
/sys/kernel/btf/vmlinux`.

Only fields carrying an access path take the new path; every other field
keeps the existing flat and indexed/array forms unchanged. The only .ll diff
across all vmlinux/xdp test programs is four brand new globals for cs, csx,
ss and ssx. No pre-existing relocation string changes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Pragyansh Chaturvedi
2026-08-07 04:59:08 +05:30
parent f2ec70759e
commit 4ff3ce26c6

View File

@ -85,10 +85,6 @@ 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
@ -137,8 +133,18 @@ class IRGenerator:
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.
# Member lifted out of an anonymous member. Its access string
# comes from the recorded path, and it must not consume a
# top-level field index.
field_co_re_name, returned = self._struct_name_generator(
struct, field, field.access_path[0]
)
globvar = ir.GlobalVariable(
self.llvm_module, ir.IntType(64), name=field_co_re_name
)
globvar.linkage = "external"
globvar.set_metadata("llvm.preserve.access.index", debug_info)
self.generated_field_names[struct.name][field_name] = globvar
continue
# does not take arrays and similar types into consideration yet.
if callable(field.ctype_complex_type):
@ -271,12 +277,19 @@ class IRGenerator:
)
return name, True
elif struct.name.startswith("struct_"):
if field.access_path is not None:
# Field lifted out of an anonymous member: the access string has
# to walk into the anonymous member, e.g. `0:17:0` for
# `struct pt_regs.cs`, which is member 0 of anonymous member 17.
access_string = ":".join(str(index) for index in field.access_path)
else:
access_string = str(field_index)
name = (
"llvm."
+ struct.name.removeprefix("struct_")
+ f":0:{field.offset}"
+ "$"
+ f"0:{field_index}"
+ f"0:{access_string}"
)
return name, True
else: