btf.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. /* Copyright (c) 2018 Facebook */
  3. #ifndef _UAPI__LINUX_BTF_H__
  4. #define _UAPI__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-28: kind (e.g. int, ptr, array...etc)
  31. * bits 29-30: unused
  32. * bit 31: kind_flag, currently used by
  33. * struct, union, enum, fwd, enum64,
  34. * decl_tag and type_tag
  35. */
  36. __u32 info;
  37. /* "size" is used by INT, ENUM, STRUCT, UNION, DATASEC and ENUM64.
  38. * "size" tells the size of the type it is describing.
  39. *
  40. * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
  41. * FUNC, FUNC_PROTO, VAR, DECL_TAG and TYPE_TAG.
  42. * "type" is a type_id referring to another type.
  43. */
  44. union {
  45. __u32 size;
  46. __u32 type;
  47. };
  48. };
  49. #define BTF_INFO_KIND(info) (((info) >> 24) & 0x1f)
  50. #define BTF_INFO_VLEN(info) ((info) & 0xffff)
  51. #define BTF_INFO_KFLAG(info) ((info) >> 31)
  52. enum {
  53. BTF_KIND_UNKN = 0, /* Unknown */
  54. BTF_KIND_INT = 1, /* Integer */
  55. BTF_KIND_PTR = 2, /* Pointer */
  56. BTF_KIND_ARRAY = 3, /* Array */
  57. BTF_KIND_STRUCT = 4, /* Struct */
  58. BTF_KIND_UNION = 5, /* Union */
  59. BTF_KIND_ENUM = 6, /* Enumeration up to 32-bit values */
  60. BTF_KIND_FWD = 7, /* Forward */
  61. BTF_KIND_TYPEDEF = 8, /* Typedef */
  62. BTF_KIND_VOLATILE = 9, /* Volatile */
  63. BTF_KIND_CONST = 10, /* Const */
  64. BTF_KIND_RESTRICT = 11, /* Restrict */
  65. BTF_KIND_FUNC = 12, /* Function */
  66. BTF_KIND_FUNC_PROTO = 13, /* Function Proto */
  67. BTF_KIND_VAR = 14, /* Variable */
  68. BTF_KIND_DATASEC = 15, /* Section */
  69. BTF_KIND_FLOAT = 16, /* Floating point */
  70. BTF_KIND_DECL_TAG = 17, /* Decl Tag */
  71. BTF_KIND_TYPE_TAG = 18, /* Type Tag */
  72. BTF_KIND_ENUM64 = 19, /* Enumeration up to 64-bit values */
  73. NR_BTF_KINDS,
  74. BTF_KIND_MAX = NR_BTF_KINDS - 1,
  75. };
  76. /* For some specific BTF_KIND, "struct btf_type" is immediately
  77. * followed by extra data.
  78. */
  79. /* BTF_KIND_INT is followed by a u32 and the following
  80. * is the 32 bits arrangement:
  81. */
  82. #define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24)
  83. #define BTF_INT_OFFSET(VAL) (((VAL) & 0x00ff0000) >> 16)
  84. #define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff)
  85. /* Attributes stored in the BTF_INT_ENCODING */
  86. #define BTF_INT_SIGNED (1 << 0)
  87. #define BTF_INT_CHAR (1 << 1)
  88. #define BTF_INT_BOOL (1 << 2)
  89. /* BTF_KIND_ENUM is followed by multiple "struct btf_enum".
  90. * The exact number of btf_enum is stored in the vlen (of the
  91. * info in "struct btf_type").
  92. */
  93. struct btf_enum {
  94. __u32 name_off;
  95. __s32 val;
  96. };
  97. /* BTF_KIND_ARRAY is followed by one "struct btf_array" */
  98. struct btf_array {
  99. __u32 type;
  100. __u32 index_type;
  101. __u32 nelems;
  102. };
  103. /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed
  104. * by multiple "struct btf_member". The exact number
  105. * of btf_member is stored in the vlen (of the info in
  106. * "struct btf_type").
  107. */
  108. struct btf_member {
  109. __u32 name_off;
  110. __u32 type;
  111. /* If the type info kind_flag is set, the btf_member offset
  112. * contains both member bitfield size and bit offset. The
  113. * bitfield size is set for bitfield members. If the type
  114. * info kind_flag is not set, the offset contains only bit
  115. * offset.
  116. */
  117. __u32 offset;
  118. };
  119. /* If the struct/union type info kind_flag is set, the
  120. * following two macros are used to access bitfield_size
  121. * and bit_offset from btf_member.offset.
  122. */
  123. #define BTF_MEMBER_BITFIELD_SIZE(val) ((val) >> 24)
  124. #define BTF_MEMBER_BIT_OFFSET(val) ((val) & 0xffffff)
  125. /* BTF_KIND_FUNC_PROTO is followed by multiple "struct btf_param".
  126. * The exact number of btf_param is stored in the vlen (of the
  127. * info in "struct btf_type").
  128. */
  129. struct btf_param {
  130. __u32 name_off;
  131. __u32 type;
  132. };
  133. enum {
  134. BTF_VAR_STATIC = 0,
  135. BTF_VAR_GLOBAL_ALLOCATED = 1,
  136. BTF_VAR_GLOBAL_EXTERN = 2,
  137. };
  138. enum btf_func_linkage {
  139. BTF_FUNC_STATIC = 0,
  140. BTF_FUNC_GLOBAL = 1,
  141. BTF_FUNC_EXTERN = 2,
  142. };
  143. /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe
  144. * additional information related to the variable such as its linkage.
  145. */
  146. struct btf_var {
  147. __u32 linkage;
  148. };
  149. /* BTF_KIND_DATASEC is followed by multiple "struct btf_var_secinfo"
  150. * to describe all BTF_KIND_VAR types it contains along with it's
  151. * in-section offset as well as size.
  152. */
  153. struct btf_var_secinfo {
  154. __u32 type;
  155. __u32 offset;
  156. __u32 size;
  157. };
  158. /* BTF_KIND_DECL_TAG is followed by a single "struct btf_decl_tag" to describe
  159. * additional information related to the tag applied location.
  160. * If component_idx == -1, the tag is applied to a struct, union,
  161. * variable or function. Otherwise, it is applied to a struct/union
  162. * member or a func argument, and component_idx indicates which member
  163. * or argument (0 ... vlen-1).
  164. */
  165. struct btf_decl_tag {
  166. __s32 component_idx;
  167. };
  168. /* BTF_KIND_ENUM64 is followed by multiple "struct btf_enum64".
  169. * The exact number of btf_enum64 is stored in the vlen (of the
  170. * info in "struct btf_type").
  171. */
  172. struct btf_enum64 {
  173. __u32 name_off;
  174. __u32 val_lo32;
  175. __u32 val_hi32;
  176. };
  177. #endif /* _UAPI__LINUX_BTF_H__ */