reloc.bpf.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. char _license[] __attribute__((section("license"), used)) = "GPL";
  7. struct {
  8. __uint(type, BPF_MAP_TYPE_ARRAY);
  9. __type(key, __u32);
  10. __type(value, __u64);
  11. __uint(max_entries, 1);
  12. } output_map SEC(".maps");
  13. long set_output(__u64 value) {
  14. __u32 key = 0;
  15. return bpf_map_update_elem(&output_map, &key, &value, BPF_ANY);
  16. }
  17. struct relocated_struct_with_scalars {
  18. __u8 a;
  19. __u8 b;
  20. __u8 c;
  21. };
  22. __attribute__((noinline)) int field_global() {
  23. struct relocated_struct_with_scalars s = {1, 2, 3};
  24. return set_output(__builtin_preserve_access_index(s.b));
  25. }
  26. SEC("uprobe") int field(void *ctx) { return field_global(); }
  27. struct relocated_struct_with_pointer {
  28. struct relocated_struct_with_pointer *first;
  29. struct relocated_struct_with_pointer *second;
  30. };
  31. __attribute__((noinline)) int pointer_global() {
  32. struct relocated_struct_with_pointer s = {
  33. (struct relocated_struct_with_pointer *)42,
  34. (struct relocated_struct_with_pointer *)21,
  35. };
  36. return set_output((__u64)__builtin_preserve_access_index(s.first));
  37. }
  38. SEC("uprobe") int pointer(void *ctx) { return pointer_global(); }
  39. __attribute__((noinline)) int struct_flavors_global() {
  40. struct relocated_struct_with_scalars s = {1, 2, 3};
  41. if (bpf_core_field_exists(s.a)) {
  42. return set_output(__builtin_preserve_access_index(s.a));
  43. } else {
  44. return set_output(__builtin_preserve_access_index(s.c));
  45. }
  46. }
  47. SEC("uprobe") int struct_flavors(void *ctx) { return struct_flavors_global(); }
  48. enum relocated_enum_unsigned_32 { U32_VAL = 0xAAAAAAAA };
  49. __attribute__((noinline)) int enum_unsigned_32_global() {
  50. return set_output(
  51. bpf_core_enum_value(enum relocated_enum_unsigned_32, U32_VAL));
  52. }
  53. SEC("uprobe") int enum_unsigned_32(void *ctx) {
  54. return enum_unsigned_32_global();
  55. }
  56. enum relocated_enum_signed_32 { S32_VAL = -0x7AAAAAAA };
  57. __attribute__((noinline)) int enum_signed_32_global() {
  58. return set_output(
  59. bpf_core_enum_value(enum relocated_enum_signed_32, S32_VAL));
  60. }
  61. SEC("uprobe") int enum_signed_32(void *ctx) { return enum_signed_32_global(); }
  62. enum relocated_enum_unsigned_64 { U64_VAL = 0xAAAAAAAABBBBBBBB };
  63. __attribute__((noinline)) int enum_unsigned_64_global() {
  64. return set_output(
  65. bpf_core_enum_value(enum relocated_enum_unsigned_64, U64_VAL));
  66. }
  67. SEC("uprobe") int enum_unsigned_64(void *ctx) {
  68. return enum_unsigned_64_global();
  69. }
  70. enum relocated_enum_signed_64 { S64_VAL = -0xAAAAAAABBBBBBBB };
  71. __attribute__((noinline)) int enum_signed_64_global() {
  72. return set_output(
  73. bpf_core_enum_value(enum relocated_enum_signed_64, S64_VAL));
  74. }
  75. SEC("uprobe") int enum_signed_64(void *ctx) { return enum_signed_64_global(); }