btf.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 0x7fffffff
  21. /* Max offset into the string section */
  22. #define BTF_MAX_NAME_OFFSET 0x7fffffff
  23. /* Max # of struct/union/enum members or func args */
  24. #define BTF_MAX_VLEN 0xffff
  25. /* The type id is referring to a parent BTF */
  26. #define BTF_TYPE_PARENT(id) (((id) >> 31) & 0x1)
  27. #define BTF_TYPE_ID(id) ((id) & BTF_MAX_TYPE)
  28. /* String is in the ELF string section */
  29. #define BTF_STR_TBL_ELF_ID(ref) (((ref) >> 31) & 0x1)
  30. #define BTF_STR_OFFSET(ref) ((ref) & BTF_MAX_NAME_OFFSET)
  31. struct btf_type {
  32. __u32 name_off;
  33. /* "info" bits arrangement
  34. * bits 0-15: vlen (e.g. # of struct's members)
  35. * bits 16-23: unused
  36. * bits 24-28: kind (e.g. int, ptr, array...etc)
  37. * bits 29-30: unused
  38. * bits 31: root
  39. */
  40. __u32 info;
  41. /* "size" is used by INT, ENUM, STRUCT and UNION.
  42. * "size" tells the size of the type it is describing.
  43. *
  44. * "type" is used by PTR, TYPEDEF, VOLATILE, CONST and RESTRICT.
  45. * "type" is a type_id referring to another type.
  46. */
  47. union {
  48. __u32 size;
  49. __u32 type;
  50. };
  51. };
  52. #define BTF_INFO_KIND(info) (((info) >> 24) & 0x1f)
  53. #define BTF_INFO_ISROOT(info) (!!(((info) >> 24) & 0x80))
  54. #define BTF_INFO_VLEN(info) ((info) & 0xffff)
  55. #define BTF_KIND_UNKN 0 /* Unknown */
  56. #define BTF_KIND_INT 1 /* Integer */
  57. #define BTF_KIND_PTR 2 /* Pointer */
  58. #define BTF_KIND_ARRAY 3 /* Array */
  59. #define BTF_KIND_STRUCT 4 /* Struct */
  60. #define BTF_KIND_UNION 5 /* Union */
  61. #define BTF_KIND_ENUM 6 /* Enumeration */
  62. #define BTF_KIND_FWD 7 /* Forward */
  63. #define BTF_KIND_TYPEDEF 8 /* Typedef */
  64. #define BTF_KIND_VOLATILE 9 /* Volatile */
  65. #define BTF_KIND_CONST 10 /* Const */
  66. #define BTF_KIND_RESTRICT 11 /* Restrict */
  67. #define BTF_KIND_MAX 11
  68. #define NR_BTF_KINDS 12
  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) & 0xff000000) >> 24)
  76. #define BTF_INT_OFFSET(VAL) (((VAL & 0x00ff0000)) >> 16)
  77. #define BTF_INT_BITS(VAL) ((VAL) & 0x0000ffff)
  78. /* Attributes stored in the BTF_INT_ENCODING */
  79. #define BTF_INT_SIGNED 0x1
  80. #define BTF_INT_CHAR 0x2
  81. #define BTF_INT_BOOL 0x4
  82. #define BTF_INT_VARARGS 0x8
  83. /* BTF_KIND_ENUM is followed by multiple "struct btf_enum".
  84. * The exact number of btf_enum is stored in the vlen (of the
  85. * info in "struct btf_type").
  86. */
  87. struct btf_enum {
  88. __u32 name_off;
  89. __s32 val;
  90. };
  91. /* BTF_KIND_ARRAY is followed by one "struct btf_array" */
  92. struct btf_array {
  93. __u32 type;
  94. __u32 index_type;
  95. __u32 nelems;
  96. };
  97. /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed
  98. * by multiple "struct btf_member". The exact number
  99. * of btf_member is stored in the vlen (of the info in
  100. * "struct btf_type").
  101. */
  102. struct btf_member {
  103. __u32 name_off;
  104. __u32 type;
  105. __u32 offset; /* offset in bits */
  106. };
  107. #endif /* _UAPI__LINUX_BTF_H__ */