reloc.bpf.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.b));
  45. }
  46. }
  47. SEC("uprobe") int struct_flavors(void *ctx) { return struct_flavors_global(); }
  48. enum relocated_enum_unsigned_32 { U32 = 0xAAAAAAAA };
  49. __attribute__((noinline)) int enum_unsigned_32_global() {
  50. return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_32, U32));
  51. }
  52. SEC("uprobe") int enum_unsigned_32(void *ctx) {
  53. return enum_unsigned_32_global();
  54. }
  55. enum relocated_enum_signed_32 { S32 = -0x7AAAAAAA };
  56. __attribute__((noinline)) int enum_signed_32_global() {
  57. return set_output(bpf_core_enum_value(enum relocated_enum_signed_32, S32));
  58. }
  59. SEC("uprobe") int enum_signed_32(void *ctx) { return enum_signed_32_global(); }
  60. enum relocated_enum_unsigned_64 { U64 = 0xAAAAAAAABBBBBBBB };
  61. __attribute__((noinline)) int enum_unsigned_64_global() {
  62. return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_64, U64));
  63. }
  64. SEC("uprobe") int enum_unsigned_64(void *ctx) {
  65. return enum_unsigned_64_global();
  66. }
  67. enum relocated_enum_signed_64 { u64 = -0xAAAAAAABBBBBBBB };
  68. __attribute__((noinline)) int enum_signed_64_global() {
  69. return set_output(bpf_core_enum_value(enum relocated_enum_signed_64, u64));
  70. }
  71. SEC("uprobe") int enum_signed_64(void *ctx) { return enum_signed_64_global(); }