btf.h 5.5 KB

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