4
0

reloc.btf.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // clang-format off
  2. #include <linux/bpf.h>
  3. #include <bpf/bpf_helpers.h>
  4. #include <bpf/bpf_core_read.h>
  5. // clang-format on
  6. #include <stdlib.h>
  7. long set_output(__u64 value) { exit((int)value); }
  8. struct relocated_struct_with_scalars {
  9. __u8 b;
  10. __u8 c;
  11. __u8 d;
  12. };
  13. __attribute__((noinline)) int field_global() {
  14. struct relocated_struct_with_scalars s = {1, 2, 3};
  15. return set_output(__builtin_preserve_access_index(s.b));
  16. }
  17. struct relocated_struct_with_pointer {
  18. struct relocated_struct_with_pointer *second;
  19. struct relocated_struct_with_pointer *first;
  20. };
  21. __attribute__((noinline)) int pointer_global() {
  22. struct relocated_struct_with_pointer s = {
  23. (struct relocated_struct_with_pointer *)42,
  24. (struct relocated_struct_with_pointer *)21,
  25. };
  26. return set_output((__u64)__builtin_preserve_access_index(s.first));
  27. }
  28. __attribute__((noinline)) int struct_flavors_global() {
  29. struct relocated_struct_with_scalars s = {1, 2, 3};
  30. if (bpf_core_field_exists(s.b)) {
  31. return set_output(__builtin_preserve_access_index(s.b));
  32. } else {
  33. return set_output(__builtin_preserve_access_index(s.c));
  34. }
  35. }
  36. enum relocated_enum_unsigned_32 { U32 = 0xBBBBBBBB };
  37. __attribute__((noinline)) int enum_unsigned_32_global() {
  38. return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_32, U32));
  39. }
  40. enum relocated_enum_signed_32 { S32 = -0x7BBBBBBB };
  41. __attribute__((noinline)) int enum_signed_32_global() {
  42. return set_output(bpf_core_enum_value(enum relocated_enum_signed_32, S32));
  43. }
  44. enum relocated_enum_unsigned_64 { U64 = 0xCCCCCCCCDDDDDDDD };
  45. __attribute__((noinline)) int enum_unsigned_64_global() {
  46. return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_64, U64));
  47. }
  48. enum relocated_enum_signed_64 { u64 = -0xCCCCCCCDDDDDDDD };
  49. __attribute__((noinline)) int enum_signed_64_global() {
  50. return set_output(bpf_core_enum_value(enum relocated_enum_signed_64, u64));
  51. }
  52. // Avoids dead code elimination by the compiler.
  53. int main() {
  54. field_global();
  55. pointer_global();
  56. struct_flavors_global();
  57. enum_unsigned_32_global();
  58. enum_signed_32_global();
  59. enum_unsigned_64_global();
  60. enum_signed_64_global();
  61. }