-
Describe the bugHopefully I've not missed the obvious here ... to the best of my knowledge there isn't currently "an official" way to keep constant/ A potential For instance, in the eBPF C code: #define MAX_FOOBAR 42
const volatile u64 max_foobar = MAX_FOOBAR;
SEC("very_raw_tracepoint/ohlala")
int BPF_PROG(ohlala) {
/* ensure .rodata volatile constants get loaded */ __u32 _ = max_foobar;
} Nota bene: I found out the hard way that How to reproducen/a Version informationgithub.com/cilium/ebpf v0.16.0 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Reading constants is now possible with #1564 and will ship in 0.17. Manual is here: https://ebpf-go.dev/concepts/global-variables. However, this strikes me as rather roundabout, esp. since we cull unreferenced symbols as you've mentioned. This cannot be worked around (aside from creating a reference..) and is still the case with the new CollectionSpec.Variables implementation. For carrying e.g. some version info or other build-time info in an ELF, consider putting them in enums. Your
Try this in C: enum metadata { bar = 1 };
// __hidden to omit from CollectionSpec.Variables. You could make this static,
// but then you'll need to mark it with __attribute__((used)), pick your poison..
// In the ELF, these would look exactly the same.
__hidden volatile enum metadata __meta; bpftool:
In Go: var consts *btf.Enum
if err := spec.Types.TypeByName("metadata", &consts); err != nil {
panic(err)
}
for _, v := range consts.Values {
if v.Name == "non_exist" {
return v.Value
}
} Hope this helps. |
Beta Was this translation helpful? Give feedback.
Reading constants is now possible with #1564 and will ship in 0.17. Manual is here: https://ebpf-go.dev/concepts/global-variables.
However, this strikes me as rather roundabout, esp. since we cull unreferenced symbols as you've mentioned. This cannot be worked around (aside from creating a reference..) and is still the case with the new CollectionSpec.Variables implementation.
For carrying e.g. some version info or other build-time info in an ELF, consider putting them in enums. Your
MAX_
symbol in particular looks like it could be an enum in your actual code base. An ELF's enums are preserved in BTF, quick example in the repo's testdata: