reloc.bpf.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // clang-format off
  2. #include <vmlinux.h>
  3. #include <bpf/bpf_helpers.h>
  4. #include <bpf/bpf_core_read.h>
  5. // clang-format on
  6. char _license[] SEC("license") = "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. #ifndef TARGET
  19. __u8 a;
  20. #endif
  21. __u8 b;
  22. __u8 c;
  23. #ifdef TARGET
  24. __u8 d;
  25. #endif
  26. };
  27. __noinline int field_global() {
  28. struct relocated_struct_with_scalars s = {1, 2, 3};
  29. return set_output(__builtin_preserve_access_index(s.b));
  30. }
  31. SEC("uprobe") int field(void *ctx) { return field_global(); }
  32. struct relocated_struct_with_pointer {
  33. #ifndef TARGET
  34. struct relocated_struct_with_pointer *first;
  35. #endif
  36. struct relocated_struct_with_pointer *second;
  37. #ifdef TARGET
  38. struct relocated_struct_with_pointer *first;
  39. #endif
  40. };
  41. __noinline int pointer_global() {
  42. struct relocated_struct_with_pointer s = {
  43. (struct relocated_struct_with_pointer *)42,
  44. (struct relocated_struct_with_pointer *)21,
  45. };
  46. return set_output((__u64)__builtin_preserve_access_index(s.first));
  47. }
  48. SEC("uprobe") int pointer(void *ctx) { return pointer_global(); }
  49. __noinline int struct_flavors_global() {
  50. struct relocated_struct_with_scalars s = {1, 2, 3};
  51. #ifndef TARGET
  52. if (bpf_core_field_exists(s.a)) {
  53. return set_output(__builtin_preserve_access_index(s.a));
  54. #else
  55. if (bpf_core_field_exists(s.d)) {
  56. return set_output(__builtin_preserve_access_index(s.d));
  57. #endif
  58. } else {
  59. return set_output(__builtin_preserve_access_index(s.c));
  60. }
  61. }
  62. SEC("uprobe") int struct_flavors(void *ctx) { return struct_flavors_global(); }
  63. enum relocated_enum_unsigned_32 {
  64. U32_VAL =
  65. #ifndef TARGET
  66. 0xAAAAAAAA
  67. #else
  68. 0xBBBBBBBB
  69. #endif
  70. };
  71. __noinline int enum_unsigned_32_global() {
  72. return set_output(
  73. bpf_core_enum_value(enum relocated_enum_unsigned_32, U32_VAL));
  74. }
  75. SEC("uprobe") int enum_unsigned_32(void *ctx) {
  76. return enum_unsigned_32_global();
  77. }
  78. enum relocated_enum_signed_32 {
  79. S32_VAL =
  80. #ifndef TARGET
  81. -0x7AAAAAAA
  82. #else
  83. -0x7BBBBBBB
  84. #endif
  85. };
  86. __noinline int enum_signed_32_global() {
  87. return set_output(
  88. bpf_core_enum_value(enum relocated_enum_signed_32, S32_VAL));
  89. }
  90. SEC("uprobe") int enum_signed_32(void *ctx) { return enum_signed_32_global(); }
  91. enum relocated_enum_unsigned_64 {
  92. U64_VAL =
  93. #ifndef TARGET
  94. 0xAAAAAAAABBBBBBBB
  95. #else
  96. 0xCCCCCCCCDDDDDDDD
  97. #endif
  98. };
  99. __noinline int enum_unsigned_64_global() {
  100. return set_output(
  101. bpf_core_enum_value(enum relocated_enum_unsigned_64, U64_VAL));
  102. }
  103. SEC("uprobe") int enum_unsigned_64(void *ctx) {
  104. return enum_unsigned_64_global();
  105. }
  106. enum relocated_enum_signed_64 {
  107. S64_VAL =
  108. #ifndef TARGET
  109. -0xAAAAAAABBBBBBBB
  110. #else
  111. -0xCCCCCCCDDDDDDDD
  112. #endif
  113. };
  114. __noinline int enum_signed_64_global() {
  115. return set_output(
  116. bpf_core_enum_value(enum relocated_enum_signed_64, S64_VAL));
  117. }
  118. SEC("uprobe") int enum_signed_64(void *ctx) { return enum_signed_64_global(); }