btf.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. /* Copyright (c) 2018 Facebook */
  3. #ifndef __LINUX_BTF_H__
  4. #define __LINUX_BTF_H__
  5. #include <linux/types.h>
  6. #define BTF_MAGIC 0xeB9F
  7. #define BTF_VERSION 1
  8. struct btf_header {
  9. __u16 magic;
  10. __u8 version;
  11. __u8 flags;
  12. __u32 hdr_len;
  13. /* All offsets are in bytes relative to the end of this header */
  14. __u32 type_off; /* offset of type section */
  15. __u32 type_len; /* length of type section */
  16. __u32 str_off; /* offset of string section */
  17. __u32 str_len; /* length of string section */
  18. };
  19. /* Max # of type identifier */
  20. #define BTF_MAX_TYPE 0x000fffff
  21. /* Max offset into the string section */
  22. #define BTF_MAX_NAME_OFFSET 0x00ffffff
  23. /* Max # of struct/union/enum members or func args */
  24. #define BTF_MAX_VLEN 0xffff
  25. struct btf_type {
  26. __u32 name_off;
  27. /* "info" bits arrangement
  28. * bits 0-15: vlen (e.g. # of struct's members)
  29. * bits 16-23: unused
  30. * bits 24-27: kind (e.g. int, ptr, array...etc)
  31. * bits 28-30: unused
  32. * bit 31: kind_flag, currently used by
  33. * struct, union and fwd
  34. */
  35. __u32 info;
  36. /* "size" is used by INT, ENUM, STRUCT, UNION and DATASEC.
  37. * "size" tells the size of the type it is describing.
  38. *
  39. * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
  40. * FUNC, FUNC_PROTO and VAR.
  41. * "type" is a type_id referring to another type.
  42. */
  43. union {
  44. __u32 size;
  45. __u32 type;
  46. };
  47. };
  48. #define BTF_INFO_KIND(info) (((info) >> 24) & 0x0f)
  49. #define BTF_INFO_VLEN(info) ((info) & 0xffff)
  50. #define BTF_INFO_KFLAG(info) ((info) >> 31)
  51. #define BTF_KIND_UNKN 0 /* Unknown */
  52. #define BTF_KIND_INT 1 /* Integer */
  53. #define BTF_KIND_PTR 2 /* Pointer */
  54. #define BTF_KIND_ARRAY 3 /* Array */
  55. #define BTF_KIND_STRUCT 4 /* Struct */
  56. #define BTF_KIND_UNION 5 /* Union */
  57. #define BTF_KIND_ENUM 6 /* Enumeration */
  58. #define BTF_KIND_FWD 7 /* Forward */
  59. #define BTF_KIND_TYPEDEF 8 /* Typedef */
  60. #define BTF_KIND_VOLATILE 9 /* Volatile */
  61. #define BTF_KIND_CONST 10 /* Const */
  62. #define BTF_KIND_RESTRICT 11 /* Restrict */
  63. #define BTF_KIND_FUNC 12 /* Function */
  64. #define BTF_KIND_FUNC_PROTO 13 /* Function Proto */
  65. #define BTF_KIND_VAR 14 /* Variable */
  66. #define BTF_KIND_DATASEC 15 /* Section */
  67. #define BTF_KIND_MAX BTF_KIND_DATASEC
  68. #define NR_BTF_KINDS (BTF_KIND_MAX + 1)
  69. /* For some specific BTF_KIND, "struct btf_type" is immediately
  70. * followed by extra data.
  71. */
  72. /* BTF_KIND_INT is followed by a u32 and the following
  73. * is the 32 bits arrangement:
  74. */
  75. #define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24)
  76. #define BTF_INT_OFFSET(VAL) (((VAL) & 0x00ff0000) >> 16)
  77. #define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff)
  78. /* Attributes stored in the BTF_INT_ENCODING */
  79. #define BTF_INT_SIGNED (1 << 0)
  80. #define BTF_INT_CHAR (1 << 1)
  81. #define BTF_INT_BOOL (1 << 2)
  82. /* BTF_KIND_ENUM is followed by multiple "struct btf_enum".
  83. * The exact number of btf_enum is stored in the vlen (of the
  84. * info in "struct btf_type").
  85. */
  86. struct btf_enum {
  87. __u32 name_off;
  88. __s32 val;
  89. };
  90. /* BTF_KIND_ARRAY is followed by one "struct btf_array" */
  91. struct btf_array {
  92. __u32 type;
  93. __u32 index_type;
  94. __u32 nelems;
  95. };
  96. /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed
  97. * by multiple "struct btf_member". The exact number
  98. * of btf_member is stored in the vlen (of the info in
  99. * "struct btf_type").
  100. */
  101. struct btf_member {
  102. __u32 name_off;
  103. __u32 type;
  104. /* If the type info kind_flag is set, the btf_member offset
  105. * contains both member bitfield size and bit offset. The
  106. * bitfield size is set for bitfield members. If the type
  107. * info kind_flag is not set, the offset contains only bit
  108. * offset.
  109. */
  110. __u32 offset;
  111. };
  112. /* If the struct/union type info kind_flag is set, the
  113. * following two macros are used to access bitfield_size
  114. * and bit_offset from btf_member.offset.
  115. */
  116. #define BTF_MEMBER_BITFIELD_SIZE(val) ((val) >> 24)
  117. #define BTF_MEMBER_BIT_OFFSET(val) ((val) & 0xffffff)
  118. /* BTF_KIND_FUNC_PROTO is followed by multiple "struct btf_param".
  119. * The exact number of btf_param is stored in the vlen (of the
  120. * info in "struct btf_type").
  121. */
  122. struct btf_param {
  123. __u32 name_off;
  124. __u32 type;
  125. };
  126. enum {
  127. BTF_VAR_STATIC = 0,
  128. BTF_VAR_GLOBAL_ALLOCATED = 1,
  129. BTF_VAR_GLOBAL_EXTERN = 2,
  130. };
  131. enum btf_func_linkage {
  132. BTF_FUNC_STATIC = 0,
  133. BTF_FUNC_GLOBAL = 1,
  134. BTF_FUNC_EXTERN = 2,
  135. };
  136. /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe
  137. * additional information related to the variable such as its linkage.
  138. */
  139. struct btf_var {
  140. __u32 linkage;
  141. };
  142. /* BTF_KIND_DATASEC is followed by multiple "struct btf_var_secinfo"
  143. * to describe all BTF_KIND_VAR types it contains along with it's
  144. * in-section offset as well as size.
  145. */
  146. struct btf_var_secinfo {
  147. __u32 type;
  148. __u32 offset;
  149. __u32 size;
  150. };
  151. #endif /* __LINUX_BTF_H__ */