reloc.bpf.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/field") int field(void *ctx) {
  27. return field_global();
  28. }
  29. struct relocated_struct_with_pointer {
  30. struct relocated_struct_with_pointer *first;
  31. struct relocated_struct_with_pointer *second;
  32. };
  33. __attribute__((noinline)) int pointer_global() {
  34. struct relocated_struct_with_pointer s = {
  35. (struct relocated_struct_with_pointer *)42,
  36. (struct relocated_struct_with_pointer *)21,
  37. };
  38. return set_output((__u64)__builtin_preserve_access_index(s.first));
  39. }
  40. SEC("uprobe/pointer") int pointer(void *ctx) {
  41. return pointer_global();
  42. }
  43. __attribute__((noinline)) int struct_flavors_global() {
  44. struct relocated_struct_with_scalars s = {1, 2, 3};
  45. if (bpf_core_field_exists(s.a)) {
  46. return set_output(__builtin_preserve_access_index(s.a));
  47. } else {
  48. return set_output(__builtin_preserve_access_index(s.b));
  49. }
  50. }
  51. SEC("uprobe/struct_flavors") int struct_flavors(void *ctx) {
  52. return struct_flavors_global();
  53. }
  54. enum relocated_enum_unsigned_32 { U32 = 0xAAAAAAAA };
  55. __attribute__((noinline)) int enum_unsigned_32_global() {
  56. return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_32, U32));
  57. }
  58. SEC("uprobe/enum_unsigned_32")
  59. int enum_unsigned_32(void *ctx) {
  60. return enum_unsigned_32_global();
  61. }
  62. enum relocated_enum_signed_32 { S32 = -0x7AAAAAAA };
  63. __attribute__((noinline)) int enum_signed_32_global() {
  64. return set_output(bpf_core_enum_value(enum relocated_enum_signed_32, S32));
  65. }
  66. SEC("uprobe/enum_signed_32") int enum_signed_32(void *ctx) {
  67. return enum_signed_32_global();
  68. }
  69. enum relocated_enum_unsigned_64 { U64 = 0xAAAAAAAABBBBBBBB };
  70. __attribute__((noinline)) int enum_unsigned_64_global() {
  71. return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_64, U64));
  72. }
  73. SEC("uprobe/enum_unsigned_64")
  74. int enum_unsigned_64(void *ctx) {
  75. return enum_unsigned_64_global();
  76. }
  77. enum relocated_enum_signed_64 { u64 = -0xAAAAAAABBBBBBBB };
  78. __attribute__((noinline)) int enum_signed_64_global() {
  79. return set_output(bpf_core_enum_value(enum relocated_enum_signed_64, u64));
  80. }
  81. SEC("uprobe/enum_signed_64") int enum_signed_64(void *ctx) {
  82. return enum_signed_64_global();
  83. }