llvm_reloc.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. .. SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  2. ====================
  3. BPF LLVM Relocations
  4. ====================
  5. This document describes LLVM BPF backend relocation types.
  6. Relocation Record
  7. =================
  8. LLVM BPF backend records each relocation with the following 16-byte
  9. ELF structure::
  10. typedef struct
  11. {
  12. Elf64_Addr r_offset; // Offset from the beginning of section.
  13. Elf64_Xword r_info; // Relocation type and symbol index.
  14. } Elf64_Rel;
  15. For example, for the following code::
  16. int g1 __attribute__((section("sec")));
  17. int g2 __attribute__((section("sec")));
  18. static volatile int l1 __attribute__((section("sec")));
  19. static volatile int l2 __attribute__((section("sec")));
  20. int test() {
  21. return g1 + g2 + l1 + l2;
  22. }
  23. Compiled with ``clang --target=bpf -O2 -c test.c``, the following is
  24. the code with ``llvm-objdump -dr test.o``::
  25. 0: 18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 = 0 ll
  26. 0000000000000000: R_BPF_64_64 g1
  27. 2: 61 11 00 00 00 00 00 00 r1 = *(u32 *)(r1 + 0)
  28. 3: 18 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r2 = 0 ll
  29. 0000000000000018: R_BPF_64_64 g2
  30. 5: 61 20 00 00 00 00 00 00 r0 = *(u32 *)(r2 + 0)
  31. 6: 0f 10 00 00 00 00 00 00 r0 += r1
  32. 7: 18 01 00 00 08 00 00 00 00 00 00 00 00 00 00 00 r1 = 8 ll
  33. 0000000000000038: R_BPF_64_64 sec
  34. 9: 61 11 00 00 00 00 00 00 r1 = *(u32 *)(r1 + 0)
  35. 10: 0f 10 00 00 00 00 00 00 r0 += r1
  36. 11: 18 01 00 00 0c 00 00 00 00 00 00 00 00 00 00 00 r1 = 12 ll
  37. 0000000000000058: R_BPF_64_64 sec
  38. 13: 61 11 00 00 00 00 00 00 r1 = *(u32 *)(r1 + 0)
  39. 14: 0f 10 00 00 00 00 00 00 r0 += r1
  40. 15: 95 00 00 00 00 00 00 00 exit
  41. There are four relocations in the above for four ``LD_imm64`` instructions.
  42. The following ``llvm-readelf -r test.o`` shows the binary values of the four
  43. relocations::
  44. Relocation section '.rel.text' at offset 0x190 contains 4 entries:
  45. Offset Info Type Symbol's Value Symbol's Name
  46. 0000000000000000 0000000600000001 R_BPF_64_64 0000000000000000 g1
  47. 0000000000000018 0000000700000001 R_BPF_64_64 0000000000000004 g2
  48. 0000000000000038 0000000400000001 R_BPF_64_64 0000000000000000 sec
  49. 0000000000000058 0000000400000001 R_BPF_64_64 0000000000000000 sec
  50. Each relocation is represented by ``Offset`` (8 bytes) and ``Info`` (8 bytes).
  51. For example, the first relocation corresponds to the first instruction
  52. (Offset 0x0) and the corresponding ``Info`` indicates the relocation type
  53. of ``R_BPF_64_64`` (type 1) and the entry in the symbol table (entry 6).
  54. The following is the symbol table with ``llvm-readelf -s test.o``::
  55. Symbol table '.symtab' contains 8 entries:
  56. Num: Value Size Type Bind Vis Ndx Name
  57. 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
  58. 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS test.c
  59. 2: 0000000000000008 4 OBJECT LOCAL DEFAULT 4 l1
  60. 3: 000000000000000c 4 OBJECT LOCAL DEFAULT 4 l2
  61. 4: 0000000000000000 0 SECTION LOCAL DEFAULT 4 sec
  62. 5: 0000000000000000 128 FUNC GLOBAL DEFAULT 2 test
  63. 6: 0000000000000000 4 OBJECT GLOBAL DEFAULT 4 g1
  64. 7: 0000000000000004 4 OBJECT GLOBAL DEFAULT 4 g2
  65. The 6th entry is global variable ``g1`` with value 0.
  66. Similarly, the second relocation is at ``.text`` offset ``0x18``, instruction 3,
  67. has a type of ``R_BPF_64_64`` and refers to entry 7 in the symbol table.
  68. The second relocation resolves to global variable ``g2`` which has a symbol
  69. value 4. The symbol value represents the offset from the start of ``.data``
  70. section where the initial value of the global variable ``g2`` is stored.
  71. The third and fourth relocations refer to static variables ``l1``
  72. and ``l2``. From the ``.rel.text`` section above, it is not clear
  73. to which symbols they really refer as they both refer to
  74. symbol table entry 4, symbol ``sec``, which has ``STT_SECTION`` type
  75. and represents a section. So for a static variable or function,
  76. the section offset is written to the original insn
  77. buffer, which is called ``A`` (addend). Looking at
  78. above insn ``7`` and ``11``, they have section offset ``8`` and ``12``.
  79. From symbol table, we can find that they correspond to entries ``2``
  80. and ``3`` for ``l1`` and ``l2``.
  81. In general, the ``A`` is 0 for global variables and functions,
  82. and is the section offset or some computation result based on
  83. section offset for static variables/functions. The non-section-offset
  84. case refers to function calls. See below for more details.
  85. Different Relocation Types
  86. ==========================
  87. Six relocation types are supported. The following is an overview and
  88. ``S`` represents the value of the symbol in the symbol table::
  89. Enum ELF Reloc Type Description BitSize Offset Calculation
  90. 0 R_BPF_NONE None
  91. 1 R_BPF_64_64 ld_imm64 insn 32 r_offset + 4 S + A
  92. 2 R_BPF_64_ABS64 normal data 64 r_offset S + A
  93. 3 R_BPF_64_ABS32 normal data 32 r_offset S + A
  94. 4 R_BPF_64_NODYLD32 .BTF[.ext] data 32 r_offset S + A
  95. 10 R_BPF_64_32 call insn 32 r_offset + 4 (S + A) / 8 - 1
  96. For example, ``R_BPF_64_64`` relocation type is used for ``ld_imm64`` instruction.
  97. The actual to-be-relocated data (0 or section offset)
  98. is stored at ``r_offset + 4`` and the read/write
  99. data bitsize is 32 (4 bytes). The relocation can be resolved with
  100. the symbol value plus implicit addend. Note that the ``BitSize`` is 32 which
  101. means the section offset must be less than or equal to ``UINT32_MAX`` and this
  102. is enforced by LLVM BPF backend.
  103. In another case, ``R_BPF_64_ABS64`` relocation type is used for normal 64-bit data.
  104. The actual to-be-relocated data is stored at ``r_offset`` and the read/write data
  105. bitsize is 64 (8 bytes). The relocation can be resolved with
  106. the symbol value plus implicit addend.
  107. Both ``R_BPF_64_ABS32`` and ``R_BPF_64_NODYLD32`` types are for 32-bit data.
  108. But ``R_BPF_64_NODYLD32`` specifically refers to relocations in ``.BTF`` and
  109. ``.BTF.ext`` sections. For cases like bcc where llvm ``ExecutionEngine RuntimeDyld``
  110. is involved, ``R_BPF_64_NODYLD32`` types of relocations should not be resolved
  111. to actual function/variable address. Otherwise, ``.BTF`` and ``.BTF.ext``
  112. become unusable by bcc and kernel.
  113. Type ``R_BPF_64_32`` is used for call instruction. The call target section
  114. offset is stored at ``r_offset + 4`` (32bit) and calculated as
  115. ``(S + A) / 8 - 1``.
  116. Examples
  117. ========
  118. Types ``R_BPF_64_64`` and ``R_BPF_64_32`` are used to resolve ``ld_imm64``
  119. and ``call`` instructions. For example::
  120. __attribute__((noinline)) __attribute__((section("sec1")))
  121. int gfunc(int a, int b) {
  122. return a * b;
  123. }
  124. static __attribute__((noinline)) __attribute__((section("sec1")))
  125. int lfunc(int a, int b) {
  126. return a + b;
  127. }
  128. int global __attribute__((section("sec2")));
  129. int test(int a, int b) {
  130. return gfunc(a, b) + lfunc(a, b) + global;
  131. }
  132. Compiled with ``clang --target=bpf -O2 -c test.c``, we will have
  133. following code with `llvm-objdump -dr test.o``::
  134. Disassembly of section .text:
  135. 0000000000000000 <test>:
  136. 0: bf 26 00 00 00 00 00 00 r6 = r2
  137. 1: bf 17 00 00 00 00 00 00 r7 = r1
  138. 2: 85 10 00 00 ff ff ff ff call -1
  139. 0000000000000010: R_BPF_64_32 gfunc
  140. 3: bf 08 00 00 00 00 00 00 r8 = r0
  141. 4: bf 71 00 00 00 00 00 00 r1 = r7
  142. 5: bf 62 00 00 00 00 00 00 r2 = r6
  143. 6: 85 10 00 00 02 00 00 00 call 2
  144. 0000000000000030: R_BPF_64_32 sec1
  145. 7: 0f 80 00 00 00 00 00 00 r0 += r8
  146. 8: 18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 = 0 ll
  147. 0000000000000040: R_BPF_64_64 global
  148. 10: 61 11 00 00 00 00 00 00 r1 = *(u32 *)(r1 + 0)
  149. 11: 0f 10 00 00 00 00 00 00 r0 += r1
  150. 12: 95 00 00 00 00 00 00 00 exit
  151. Disassembly of section sec1:
  152. 0000000000000000 <gfunc>:
  153. 0: bf 20 00 00 00 00 00 00 r0 = r2
  154. 1: 2f 10 00 00 00 00 00 00 r0 *= r1
  155. 2: 95 00 00 00 00 00 00 00 exit
  156. 0000000000000018 <lfunc>:
  157. 3: bf 20 00 00 00 00 00 00 r0 = r2
  158. 4: 0f 10 00 00 00 00 00 00 r0 += r1
  159. 5: 95 00 00 00 00 00 00 00 exit
  160. The first relocation corresponds to ``gfunc(a, b)`` where ``gfunc`` has a value of 0,
  161. so the ``call`` instruction offset is ``(0 + 0)/8 - 1 = -1``.
  162. The second relocation corresponds to ``lfunc(a, b)`` where ``lfunc`` has a section
  163. offset ``0x18``, so the ``call`` instruction offset is ``(0 + 0x18)/8 - 1 = 2``.
  164. The third relocation corresponds to ld_imm64 of ``global``, which has a section
  165. offset ``0``.
  166. The following is an example to show how R_BPF_64_ABS64 could be generated::
  167. int global() { return 0; }
  168. struct t { void *g; } gbl = { global };
  169. Compiled with ``clang --target=bpf -O2 -g -c test.c``, we will see a
  170. relocation below in ``.data`` section with command
  171. ``llvm-readelf -r test.o``::
  172. Relocation section '.rel.data' at offset 0x458 contains 1 entries:
  173. Offset Info Type Symbol's Value Symbol's Name
  174. 0000000000000000 0000000700000002 R_BPF_64_ABS64 0000000000000000 global
  175. The relocation says the first 8-byte of ``.data`` section should be
  176. filled with address of ``global`` variable.
  177. With ``llvm-readelf`` output, we can see that dwarf sections have a bunch of
  178. ``R_BPF_64_ABS32`` and ``R_BPF_64_ABS64`` relocations::
  179. Relocation section '.rel.debug_info' at offset 0x468 contains 13 entries:
  180. Offset Info Type Symbol's Value Symbol's Name
  181. 0000000000000006 0000000300000003 R_BPF_64_ABS32 0000000000000000 .debug_abbrev
  182. 000000000000000c 0000000400000003 R_BPF_64_ABS32 0000000000000000 .debug_str
  183. 0000000000000012 0000000400000003 R_BPF_64_ABS32 0000000000000000 .debug_str
  184. 0000000000000016 0000000600000003 R_BPF_64_ABS32 0000000000000000 .debug_line
  185. 000000000000001a 0000000400000003 R_BPF_64_ABS32 0000000000000000 .debug_str
  186. 000000000000001e 0000000200000002 R_BPF_64_ABS64 0000000000000000 .text
  187. 000000000000002b 0000000400000003 R_BPF_64_ABS32 0000000000000000 .debug_str
  188. 0000000000000037 0000000800000002 R_BPF_64_ABS64 0000000000000000 gbl
  189. 0000000000000040 0000000400000003 R_BPF_64_ABS32 0000000000000000 .debug_str
  190. ......
  191. The .BTF/.BTF.ext sections has R_BPF_64_NODYLD32 relocations::
  192. Relocation section '.rel.BTF' at offset 0x538 contains 1 entries:
  193. Offset Info Type Symbol's Value Symbol's Name
  194. 0000000000000084 0000000800000004 R_BPF_64_NODYLD32 0000000000000000 gbl
  195. Relocation section '.rel.BTF.ext' at offset 0x548 contains 2 entries:
  196. Offset Info Type Symbol's Value Symbol's Name
  197. 000000000000002c 0000000200000004 R_BPF_64_NODYLD32 0000000000000000 .text
  198. 0000000000000040 0000000200000004 R_BPF_64_NODYLD32 0000000000000000 .text
  199. .. _btf-co-re-relocations:
  200. =================
  201. CO-RE Relocations
  202. =================
  203. From object file point of view CO-RE mechanism is implemented as a set
  204. of CO-RE specific relocation records. These relocation records are not
  205. related to ELF relocations and are encoded in .BTF.ext section.
  206. See :ref:`Documentation/bpf/btf.rst <BTF_Ext_Section>` for more
  207. information on .BTF.ext structure.
  208. CO-RE relocations are applied to BPF instructions to update immediate
  209. or offset fields of the instruction at load time with information
  210. relevant for target kernel.
  211. Field to patch is selected basing on the instruction class:
  212. * For BPF_ALU, BPF_ALU64, BPF_LD `immediate` field is patched;
  213. * For BPF_LDX, BPF_STX, BPF_ST `offset` field is patched;
  214. * BPF_JMP, BPF_JMP32 instructions **should not** be patched.
  215. Relocation kinds
  216. ================
  217. There are several kinds of CO-RE relocations that could be split in
  218. three groups:
  219. * Field-based - patch instruction with field related information, e.g.
  220. change offset field of the BPF_LDX instruction to reflect offset
  221. of a specific structure field in the target kernel.
  222. * Type-based - patch instruction with type related information, e.g.
  223. change immediate field of the BPF_ALU move instruction to 0 or 1 to
  224. reflect if specific type is present in the target kernel.
  225. * Enum-based - patch instruction with enum related information, e.g.
  226. change immediate field of the BPF_LD_IMM64 instruction to reflect
  227. value of a specific enum literal in the target kernel.
  228. The complete list of relocation kinds is represented by the following enum:
  229. .. code-block:: c
  230. enum bpf_core_relo_kind {
  231. BPF_CORE_FIELD_BYTE_OFFSET = 0, /* field byte offset */
  232. BPF_CORE_FIELD_BYTE_SIZE = 1, /* field size in bytes */
  233. BPF_CORE_FIELD_EXISTS = 2, /* field existence in target kernel */
  234. BPF_CORE_FIELD_SIGNED = 3, /* field signedness (0 - unsigned, 1 - signed) */
  235. BPF_CORE_FIELD_LSHIFT_U64 = 4, /* bitfield-specific left bitshift */
  236. BPF_CORE_FIELD_RSHIFT_U64 = 5, /* bitfield-specific right bitshift */
  237. BPF_CORE_TYPE_ID_LOCAL = 6, /* type ID in local BPF object */
  238. BPF_CORE_TYPE_ID_TARGET = 7, /* type ID in target kernel */
  239. BPF_CORE_TYPE_EXISTS = 8, /* type existence in target kernel */
  240. BPF_CORE_TYPE_SIZE = 9, /* type size in bytes */
  241. BPF_CORE_ENUMVAL_EXISTS = 10, /* enum value existence in target kernel */
  242. BPF_CORE_ENUMVAL_VALUE = 11, /* enum value integer value */
  243. BPF_CORE_TYPE_MATCHES = 12, /* type match in target kernel */
  244. };
  245. Notes:
  246. * ``BPF_CORE_FIELD_LSHIFT_U64`` and ``BPF_CORE_FIELD_RSHIFT_U64`` are
  247. supposed to be used to read bitfield values using the following
  248. algorithm:
  249. .. code-block:: c
  250. // To read bitfield ``f`` from ``struct s``
  251. is_signed = relo(s->f, BPF_CORE_FIELD_SIGNED)
  252. off = relo(s->f, BPF_CORE_FIELD_BYTE_OFFSET)
  253. sz = relo(s->f, BPF_CORE_FIELD_BYTE_SIZE)
  254. l = relo(s->f, BPF_CORE_FIELD_LSHIFT_U64)
  255. r = relo(s->f, BPF_CORE_FIELD_RSHIFT_U64)
  256. // define ``v`` as signed or unsigned integer of size ``sz``
  257. v = *({s|u}<sz> *)((void *)s + off)
  258. v <<= l
  259. v >>= r
  260. * The ``BPF_CORE_TYPE_MATCHES`` queries matching relation, defined as
  261. follows:
  262. * for integers: types match if size and signedness match;
  263. * for arrays & pointers: target types are recursively matched;
  264. * for structs & unions:
  265. * local members need to exist in target with the same name;
  266. * for each member we recursively check match unless it is already behind a
  267. pointer, in which case we only check matching names and compatible kind;
  268. * for enums:
  269. * local variants have to have a match in target by symbolic name (but not
  270. numeric value);
  271. * size has to match (but enum may match enum64 and vice versa);
  272. * for function pointers:
  273. * number and position of arguments in local type has to match target;
  274. * for each argument and the return value we recursively check match.
  275. CO-RE Relocation Record
  276. =======================
  277. Relocation record is encoded as the following structure:
  278. .. code-block:: c
  279. struct bpf_core_relo {
  280. __u32 insn_off;
  281. __u32 type_id;
  282. __u32 access_str_off;
  283. enum bpf_core_relo_kind kind;
  284. };
  285. * ``insn_off`` - instruction offset (in bytes) within a code section
  286. associated with this relocation;
  287. * ``type_id`` - BTF type ID of the "root" (containing) entity of a
  288. relocatable type or field;
  289. * ``access_str_off`` - offset into corresponding .BTF string section.
  290. String interpretation depends on specific relocation kind:
  291. * for field-based relocations, string encodes an accessed field using
  292. a sequence of field and array indices, separated by colon (:). It's
  293. conceptually very close to LLVM's `getelementptr <GEP_>`_ instruction's
  294. arguments for identifying offset to a field. For example, consider the
  295. following C code:
  296. .. code-block:: c
  297. struct sample {
  298. int a;
  299. int b;
  300. struct { int c[10]; };
  301. } __attribute__((preserve_access_index));
  302. struct sample *s;
  303. * Access to ``s[0].a`` would be encoded as ``0:0``:
  304. * ``0``: first element of ``s`` (as if ``s`` is an array);
  305. * ``0``: index of field ``a`` in ``struct sample``.
  306. * Access to ``s->a`` would be encoded as ``0:0`` as well.
  307. * Access to ``s->b`` would be encoded as ``0:1``:
  308. * ``0``: first element of ``s``;
  309. * ``1``: index of field ``b`` in ``struct sample``.
  310. * Access to ``s[1].c[5]`` would be encoded as ``1:2:0:5``:
  311. * ``1``: second element of ``s``;
  312. * ``2``: index of anonymous structure field in ``struct sample``;
  313. * ``0``: index of field ``c`` in anonymous structure;
  314. * ``5``: access to array element #5.
  315. * for type-based relocations, string is expected to be just "0";
  316. * for enum value-based relocations, string contains an index of enum
  317. value within its enum type;
  318. * ``kind`` - one of ``enum bpf_core_relo_kind``.
  319. .. _GEP: https://llvm.org/docs/LangRef.html#getelementptr-instruction
  320. .. _btf_co_re_relocation_examples:
  321. CO-RE Relocation Examples
  322. =========================
  323. For the following C code:
  324. .. code-block:: c
  325. struct foo {
  326. int a;
  327. int b;
  328. unsigned c:15;
  329. } __attribute__((preserve_access_index));
  330. enum bar { U, V };
  331. With the following BTF definitions:
  332. .. code-block::
  333. ...
  334. [2] STRUCT 'foo' size=8 vlen=2
  335. 'a' type_id=3 bits_offset=0
  336. 'b' type_id=3 bits_offset=32
  337. 'c' type_id=4 bits_offset=64 bitfield_size=15
  338. [3] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
  339. [4] INT 'unsigned int' size=4 bits_offset=0 nr_bits=32 encoding=(none)
  340. ...
  341. [16] ENUM 'bar' encoding=UNSIGNED size=4 vlen=2
  342. 'U' val=0
  343. 'V' val=1
  344. Field offset relocations are generated automatically when
  345. ``__attribute__((preserve_access_index))`` is used, for example:
  346. .. code-block:: c
  347. void alpha(struct foo *s, volatile unsigned long *g) {
  348. *g = s->a;
  349. s->a = 1;
  350. }
  351. 00 <alpha>:
  352. 0: r3 = *(s32 *)(r1 + 0x0)
  353. 00: CO-RE <byte_off> [2] struct foo::a (0:0)
  354. 1: *(u64 *)(r2 + 0x0) = r3
  355. 2: *(u32 *)(r1 + 0x0) = 0x1
  356. 10: CO-RE <byte_off> [2] struct foo::a (0:0)
  357. 3: exit
  358. All relocation kinds could be requested via built-in functions.
  359. E.g. field-based relocations:
  360. .. code-block:: c
  361. void bravo(struct foo *s, volatile unsigned long *g) {
  362. *g = __builtin_preserve_field_info(s->b, 0 /* field byte offset */);
  363. *g = __builtin_preserve_field_info(s->b, 1 /* field byte size */);
  364. *g = __builtin_preserve_field_info(s->b, 2 /* field existence */);
  365. *g = __builtin_preserve_field_info(s->b, 3 /* field signedness */);
  366. *g = __builtin_preserve_field_info(s->c, 4 /* bitfield left shift */);
  367. *g = __builtin_preserve_field_info(s->c, 5 /* bitfield right shift */);
  368. }
  369. 20 <bravo>:
  370. 4: r1 = 0x4
  371. 20: CO-RE <byte_off> [2] struct foo::b (0:1)
  372. 5: *(u64 *)(r2 + 0x0) = r1
  373. 6: r1 = 0x4
  374. 30: CO-RE <byte_sz> [2] struct foo::b (0:1)
  375. 7: *(u64 *)(r2 + 0x0) = r1
  376. 8: r1 = 0x1
  377. 40: CO-RE <field_exists> [2] struct foo::b (0:1)
  378. 9: *(u64 *)(r2 + 0x0) = r1
  379. 10: r1 = 0x1
  380. 50: CO-RE <signed> [2] struct foo::b (0:1)
  381. 11: *(u64 *)(r2 + 0x0) = r1
  382. 12: r1 = 0x31
  383. 60: CO-RE <lshift_u64> [2] struct foo::c (0:2)
  384. 13: *(u64 *)(r2 + 0x0) = r1
  385. 14: r1 = 0x31
  386. 70: CO-RE <rshift_u64> [2] struct foo::c (0:2)
  387. 15: *(u64 *)(r2 + 0x0) = r1
  388. 16: exit
  389. Type-based relocations:
  390. .. code-block:: c
  391. void charlie(struct foo *s, volatile unsigned long *g) {
  392. *g = __builtin_preserve_type_info(*s, 0 /* type existence */);
  393. *g = __builtin_preserve_type_info(*s, 1 /* type size */);
  394. *g = __builtin_preserve_type_info(*s, 2 /* type matches */);
  395. *g = __builtin_btf_type_id(*s, 0 /* type id in this object file */);
  396. *g = __builtin_btf_type_id(*s, 1 /* type id in target kernel */);
  397. }
  398. 88 <charlie>:
  399. 17: r1 = 0x1
  400. 88: CO-RE <type_exists> [2] struct foo
  401. 18: *(u64 *)(r2 + 0x0) = r1
  402. 19: r1 = 0xc
  403. 98: CO-RE <type_size> [2] struct foo
  404. 20: *(u64 *)(r2 + 0x0) = r1
  405. 21: r1 = 0x1
  406. a8: CO-RE <type_matches> [2] struct foo
  407. 22: *(u64 *)(r2 + 0x0) = r1
  408. 23: r1 = 0x2 ll
  409. b8: CO-RE <local_type_id> [2] struct foo
  410. 25: *(u64 *)(r2 + 0x0) = r1
  411. 26: r1 = 0x2 ll
  412. d0: CO-RE <target_type_id> [2] struct foo
  413. 28: *(u64 *)(r2 + 0x0) = r1
  414. 29: exit
  415. Enum-based relocations:
  416. .. code-block:: c
  417. void delta(struct foo *s, volatile unsigned long *g) {
  418. *g = __builtin_preserve_enum_value(*(enum bar *)U, 0 /* enum literal existence */);
  419. *g = __builtin_preserve_enum_value(*(enum bar *)V, 1 /* enum literal value */);
  420. }
  421. f0 <delta>:
  422. 30: r1 = 0x1 ll
  423. f0: CO-RE <enumval_exists> [16] enum bar::U = 0
  424. 32: *(u64 *)(r2 + 0x0) = r1
  425. 33: r1 = 0x1 ll
  426. 108: CO-RE <enumval_value> [16] enum bar::V = 1
  427. 35: *(u64 *)(r2 + 0x0) = r1
  428. 36: exit