btf.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* Declarations and definitions relating to the BPF Type Format (BTF).
  2. Copyright (C) 2021-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This file is derived from the BTF specification described in the
  16. Linux kernel source tree (linux/Documentation/bpf/btf.rst). */
  17. #ifndef _BTF_H_
  18. #define _BTF_H_
  19. #include <stdint.h>
  20. #ifdef __cplusplus
  21. extern "C"
  22. {
  23. #endif
  24. /* BTF magic number to identify header, endianness. */
  25. #define BTF_MAGIC 0xeb9f
  26. /* Data format version number. */
  27. #define BTF_VERSION 1
  28. struct btf_header
  29. {
  30. uint16_t magic; /* Magic number (BTF_MAGIC). */
  31. uint8_t version; /* Data format version (BTF_VERSION). */
  32. uint8_t flags; /* Flags. Currently unused. */
  33. uint32_t hdr_len; /* Length of this header (sizeof (struct btf_header)). */
  34. /* Following offsets are relative to the end of this header. */
  35. uint32_t type_off; /* Offset of type section, in bytes. */
  36. uint32_t type_len; /* Length of type section, in bytes. */
  37. uint32_t str_off; /* Offset of string section, in bytes. */
  38. uint32_t str_len; /* Length of string section, in bytes. */
  39. };
  40. /* Maximum type identifier. */
  41. #define BTF_MAX_TYPE 0x000fffff
  42. /* Maximum offset into the string section. */
  43. #define BTF_MAX_NAME_OFFSET 0x00ffffff
  44. /* Maximum number of struct, union, enum members or func args. */
  45. #define BTF_MAX_VLEN 0xffff
  46. struct btf_type
  47. {
  48. uint32_t name_off; /* Offset in string section of type name. */
  49. uint32_t info; /* Encoded kind, variant length, kind flag:
  50. - bits 0-15: vlen
  51. - bits 16-23: unused
  52. - bits 24-28: kind
  53. - bits 29-30: unused
  54. - bit 31: kind_flag
  55. See accessor macros below. */
  56. /* SIZE is used by INT, ENUM, STRUCT, UNION, DATASEC kinds.
  57. TYPE is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, FUNC,
  58. FUNC_PROTO and VAR kinds. */
  59. union
  60. {
  61. uint32_t size; /* Size of the entire type, in bytes. */
  62. uint32_t type; /* A type_id referring to another type. */
  63. };
  64. };
  65. /* The folloing macros access the information encoded in btf_type.info. */
  66. /* Type kind. See below. */
  67. #define BTF_INFO_KIND(info) (((info) >> 24) & 0x1f)
  68. /* Number of entries of variable length data following certain type kinds.
  69. For example, number of structure members, number of function parameters. */
  70. #define BTF_INFO_VLEN(info) ((info) & 0xffff)
  71. /* For BTF_KIND_FWD, 1 if forward to union, 0 if forward to struct.
  72. For BTF_KIND_STRUCT and BTF_KIND_UNION, 1 if the struct/union contains
  73. a bitfield. */
  74. #define BTF_INFO_KFLAG(info) ((info) >> 31)
  75. /* Encoding for struct btf_type.info. */
  76. #define BTF_TYPE_INFO(kind, kflag, vlen) \
  77. ((((kflag) ? 1 : 0 ) << 31) | ((kind) << 24) | ((vlen) & 0xffff))
  78. #define BTF_KIND_UNKN 0 /* Unknown or invalid. */
  79. #define BTF_KIND_INT 1 /* Integer. */
  80. #define BTF_KIND_PTR 2 /* Pointer. */
  81. #define BTF_KIND_ARRAY 3 /* Array. */
  82. #define BTF_KIND_STRUCT 4 /* Struct. */
  83. #define BTF_KIND_UNION 5 /* Union. */
  84. #define BTF_KIND_ENUM 6 /* Enumeration. */
  85. #define BTF_KIND_FWD 7 /* Forward. */
  86. #define BTF_KIND_TYPEDEF 8 /* Typedef. */
  87. #define BTF_KIND_VOLATILE 9 /* Referenced type is volatile. */
  88. #define BTF_KIND_CONST 10 /* Referenced type is const. */
  89. #define BTF_KIND_RESTRICT 11 /* Restrict. */
  90. #define BTF_KIND_FUNC 12 /* Subprogram. */
  91. #define BTF_KIND_FUNC_PROTO 13 /* Function Prototype. */
  92. #define BTF_KIND_VAR 14 /* Variable. */
  93. #define BTF_KIND_DATASEC 15 /* Section such as .bss or .data. */
  94. #define BTF_KIND_FLOAT 16 /* Floating point. */
  95. #define BTF_KIND_MAX BTF_KIND_FLOAT
  96. #define NR_BTF_KINDS (BTF_KIND_MAX + 1)
  97. /* For some BTF_KINDs, struct btf_type is immediately followed by
  98. additional data describing the type. */
  99. /* BTF_KIND_INT is followed by a 32-bit word, with the following
  100. bit arrangement. */
  101. #define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24)
  102. #define BTF_INT_OFFSET(VAL) (((VAL) & 0x00ff0000) >> 16)
  103. #define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff)
  104. #define BTF_INT_DATA(encoding, offset, bits) \
  105. ((((encoding) & 0x0f) << 24) | (((offset) & 0xff) << 16) | ((bits) & 0xff))
  106. /* BTF_INT_ENCODING holds the following attribute flags. */
  107. #define BTF_INT_SIGNED (1 << 0)
  108. #define BTF_INT_CHAR (1 << 1)
  109. #define BTF_INT_BOOL (1 << 2)
  110. /* BTF_KIND_ENUM is followed by VLEN struct btf_enum entries,
  111. which describe the enumerators. Note that BTF currently only
  112. supports signed 32-bit enumerator values. */
  113. struct btf_enum
  114. {
  115. uint32_t name_off; /* Offset in string section of enumerator name. */
  116. int32_t val; /* Enumerator value. */
  117. };
  118. /* BTF_KIND_ARRAY is followed by a single struct btf_array. */
  119. struct btf_array
  120. {
  121. uint32_t type; /* Type of array elements. */
  122. uint32_t index_type; /* Type of array index. */
  123. uint32_t nelems; /* Number of elements. 0 for unsized/variable length. */
  124. };
  125. /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed by VLEN
  126. struct btf_member. */
  127. struct btf_member
  128. {
  129. uint32_t name_off; /* Offset in string section of member name. */
  130. uint32_t type; /* Type of member. */
  131. uint32_t offset; /* If the type info kind_flag is set, this contains
  132. both the member bitfield size and bit offset,
  133. according to the macros below. If kind_flag is not
  134. set, offset contains only the bit offset (from the
  135. beginning of the struct). */
  136. };
  137. /* If struct or union type info kind_flag is set, used to access member
  138. bitfield size from btf_member.offset. */
  139. #define BTF_MEMBER_BITFIELD_SIZE (val) ((val) >> 24)
  140. /* If struct or union type info kind_flag is set, used to access member
  141. bit offset from btf_member.offset. */
  142. #define BTF_MEMBER_BIT_OFFSET (val) ((val) & 0x00ffffff)
  143. /* BTF_KIND_FUNC_PROTO is followed by VLEN struct btf_param entries, which
  144. describe the types of the function parameters. */
  145. struct btf_param
  146. {
  147. uint32_t name_off; /* Offset in string section of parameter name. */
  148. uint32_t type; /* Type of parameter. */
  149. };
  150. /* BTF_KIND_VAR is followed by a single struct btf_var, which describes
  151. information about the variable. */
  152. struct btf_var
  153. {
  154. uint32_t linkage; /* Currently only 0=static or 1=global. */
  155. };
  156. /* BTF_KIND_DATASEC is followed by VLEN struct btf_var_secinfo entries,
  157. which describe all BTF_KIND_VAR types contained in the section. */
  158. struct btf_var_secinfo
  159. {
  160. uint32_t type; /* Type of variable. */
  161. uint32_t offset; /* In-section offset of variable (in bytes). */
  162. uint32_t size; /* Size (in bytes) of variable. */
  163. };
  164. #ifdef __cplusplus
  165. }
  166. #endif
  167. #endif /* _BTF_H_ */