1 #include <stdint.h>
2 #include <stdbool.h>
3 
4 /*
5  * FLAVORS
6  */
7 struct core_reloc_flavors {
8 	int a;
9 	int b;
10 	int c;
11 };
12 
13 /* this is not a flavor, as it doesn't have triple underscore */
14 struct core_reloc_flavors__err_wrong_name {
15 	int a;
16 	int b;
17 	int c;
18 };
19 
20 /*
21  * NESTING
22  */
23 /* original set up, used to record relocations in BPF program */
24 struct core_reloc_nesting_substruct {
25 	int a;
26 };
27 
28 union core_reloc_nesting_subunion {
29 	int b;
30 };
31 
32 struct core_reloc_nesting {
33 	union {
34 		struct core_reloc_nesting_substruct a;
35 	} a;
36 	struct {
37 		union core_reloc_nesting_subunion b;
38 	} b;
39 };
40 
41 /* inlined anonymous struct/union instead of named structs in original */
42 struct core_reloc_nesting___anon_embed {
43 	int __just_for_padding;
44 	union {
45 		struct {
46 			int a;
47 		} a;
48 	} a;
49 	struct {
50 		union {
51 			int b;
52 		} b;
53 	} b;
54 };
55 
56 /* different mix of nested structs/unions than in original */
57 struct core_reloc_nesting___struct_union_mixup {
58 	int __a;
59 	struct {
60 		int __a;
61 		union {
62 			char __a;
63 			int a;
64 		} a;
65 	} a;
66 	int __b;
67 	union {
68 		int __b;
69 		union {
70 			char __b;
71 			int b;
72 		} b;
73 	} b;
74 };
75 
76 /* extra anon structs/unions, but still valid a.a.a and b.b.b accessors */
77 struct core_reloc_nesting___extra_nesting {
78 	int __padding;
79 	struct {
80 		struct {
81 			struct {
82 				struct {
83 					union {
84 						int a;
85 					} a;
86 				};
87 			};
88 		} a;
89 		int __some_more;
90 		struct {
91 			union {
92 				union {
93 					union {
94 						struct {
95 							int b;
96 						};
97 					} b;
98 				};
99 			} b;
100 		};
101 	};
102 };
103 
104 /* three flavors of same struct with different structure but same layout for
105  * a.a.a and b.b.b, thus successfully resolved and relocatable */
106 struct core_reloc_nesting___dup_compat_types {
107 	char __just_for_padding;
108 	/* 3 more bytes of padding */
109 	struct {
110 		struct {
111 			int a; /* offset 4 */
112 		} a;
113 	} a;
114 	long long __more_padding;
115 	struct {
116 		struct {
117 			int b; /* offset 16 */
118 		} b;
119 	} b;
120 };
121 
122 struct core_reloc_nesting___dup_compat_types__2 {
123 	int __aligned_padding;
124 	struct {
125 		int __trickier_noop[0];
126 		struct {
127 			char __some_more_noops[0];
128 			int a; /* offset 4 */
129 		} a;
130 	} a;
131 	int __more_padding;
132 	struct {
133 		struct {
134 			struct {
135 				int __critical_padding;
136 				int b; /* offset 16 */
137 			} b;
138 			int __does_not_matter;
139 		};
140 	} b;
141 	int __more_irrelevant_stuff;
142 };
143 
144 struct core_reloc_nesting___dup_compat_types__3 {
145 	char __correct_padding[4];
146 	struct {
147 		struct {
148 			int a; /* offset 4 */
149 		} a;
150 	} a;
151 	/* 8 byte padding due to next struct's alignment */
152 	struct {
153 		struct {
154 			int b;
155 		} b;
156 	} b __attribute__((aligned(16)));
157 };
158 
159 /* b.b.b field is missing */
160 struct core_reloc_nesting___err_missing_field {
161 	struct {
162 		struct {
163 			int a;
164 		} a;
165 	} a;
166 	struct {
167 		struct {
168 			int x;
169 		} b;
170 	} b;
171 };
172 
173 /* b.b.b field is an array of integers instead of plain int */
174 struct core_reloc_nesting___err_array_field {
175 	struct {
176 		struct {
177 			int a;
178 		} a;
179 	} a;
180 	struct {
181 		struct {
182 			int b[1];
183 		} b;
184 	} b;
185 };
186 
187 /* middle b container is missing */
188 struct core_reloc_nesting___err_missing_container {
189 	struct {
190 		struct {
191 			int a;
192 		} a;
193 	} a;
194 	struct {
195 		int x;
196 	} b;
197 };
198 
199 /* middle b container is referenced through pointer instead of being embedded */
200 struct core_reloc_nesting___err_nonstruct_container {
201 	struct {
202 		struct {
203 			int a;
204 		} a;
205 	} a;
206 	struct {
207 		struct {
208 			int b;
209 		} *b;
210 	} b;
211 };
212 
213 /* middle b container is an array of structs instead of plain struct */
214 struct core_reloc_nesting___err_array_container {
215 	struct {
216 		struct {
217 			int a;
218 		} a;
219 	} a;
220 	struct {
221 		struct {
222 			int b;
223 		} b[1];
224 	} b;
225 };
226 
227 /* two flavors of same struct with incompatible layout for b.b.b */
228 struct core_reloc_nesting___err_dup_incompat_types__1 {
229 	struct {
230 		struct {
231 			int a; /* offset 0 */
232 		} a;
233 	} a;
234 	struct {
235 		struct {
236 			int b; /* offset 4 */
237 		} b;
238 	} b;
239 };
240 
241 struct core_reloc_nesting___err_dup_incompat_types__2 {
242 	struct {
243 		struct {
244 			int a; /* offset 0 */
245 		} a;
246 	} a;
247 	int __extra_padding;
248 	struct {
249 		struct {
250 			int b; /* offset 8 (!) */
251 		} b;
252 	} b;
253 };
254 
255 /* two flavors of same struct having one of a.a.a and b.b.b, but not both */
256 struct core_reloc_nesting___err_partial_match_dups__a {
257 	struct {
258 		struct {
259 			int a;
260 		} a;
261 	} a;
262 };
263 
264 struct core_reloc_nesting___err_partial_match_dups__b {
265 	struct {
266 		struct {
267 			int b;
268 		} b;
269 	} b;
270 };
271 
272 struct core_reloc_nesting___err_too_deep {
273 	struct {
274 		struct {
275 			int a;
276 		} a;
277 	} a;
278 	/* 65 levels of nestedness for b.b.b */
279 	struct {
280 		struct {
281 			struct { struct { struct { struct { struct {
282 			struct { struct { struct { struct { struct {
283 			struct { struct { struct { struct { struct {
284 			struct { struct { struct { struct { struct {
285 			struct { struct { struct { struct { struct {
286 			struct { struct { struct { struct { struct {
287 			struct { struct { struct { struct { struct {
288 			struct { struct { struct { struct { struct {
289 			struct { struct { struct { struct { struct {
290 			struct { struct { struct { struct { struct {
291 			struct { struct { struct { struct { struct {
292 			struct { struct { struct { struct { struct {
293 				/* this one is one too much */
294 				struct {
295 					int b;
296 				};
297 			}; }; }; }; };
298 			}; }; }; }; };
299 			}; }; }; }; };
300 			}; }; }; }; };
301 			}; }; }; }; };
302 			}; }; }; }; };
303 			}; }; }; }; };
304 			}; }; }; }; };
305 			}; }; }; }; };
306 			}; }; }; }; };
307 			}; }; }; }; };
308 			}; }; }; }; };
309 		} b;
310 	} b;
311 };
312 
313 /*
314  * ARRAYS
315  */
316 struct core_reloc_arrays_output {
317 	int a2;
318 	char b123;
319 	int c1c;
320 	int d00d;
321 };
322 
323 struct core_reloc_arrays_substruct {
324 	int c;
325 	int d;
326 };
327 
328 struct core_reloc_arrays {
329 	int a[5];
330 	char b[2][3][4];
331 	struct core_reloc_arrays_substruct c[3];
332 	struct core_reloc_arrays_substruct d[1][2];
333 };
334 
335 /* bigger array dimensions */
336 struct core_reloc_arrays___diff_arr_dim {
337 	int a[7];
338 	char b[3][4][5];
339 	struct core_reloc_arrays_substruct c[4];
340 	struct core_reloc_arrays_substruct d[2][3];
341 };
342 
343 /* different size of array's value (struct) */
344 struct core_reloc_arrays___diff_arr_val_sz {
345 	int a[5];
346 	char b[2][3][4];
347 	struct {
348 		int __padding1;
349 		int c;
350 		int __padding2;
351 	} c[3];
352 	struct {
353 		int __padding1;
354 		int d;
355 		int __padding2;
356 	} d[1][2];
357 };
358 
359 struct core_reloc_arrays___err_too_small {
360 	int a[2]; /* this one is too small */
361 	char b[2][3][4];
362 	struct core_reloc_arrays_substruct c[3];
363 	struct core_reloc_arrays_substruct d[1][2];
364 };
365 
366 struct core_reloc_arrays___err_too_shallow {
367 	int a[5];
368 	char b[2][3]; /* this one lacks one dimension */
369 	struct core_reloc_arrays_substruct c[3];
370 	struct core_reloc_arrays_substruct d[1][2];
371 };
372 
373 struct core_reloc_arrays___err_non_array {
374 	int a; /* not an array */
375 	char b[2][3][4];
376 	struct core_reloc_arrays_substruct c[3];
377 	struct core_reloc_arrays_substruct d[1][2];
378 };
379 
380 struct core_reloc_arrays___err_wrong_val_type1 {
381 	char a[5]; /* char instead of int */
382 	char b[2][3][4];
383 	struct core_reloc_arrays_substruct c[3];
384 	struct core_reloc_arrays_substruct d[1][2];
385 };
386 
387 struct core_reloc_arrays___err_wrong_val_type2 {
388 	int a[5];
389 	char b[2][3][4];
390 	int c[3]; /* value is not a struct */
391 	struct core_reloc_arrays_substruct d[1][2];
392 };
393 
394 /*
395  * PRIMITIVES
396  */
397 enum core_reloc_primitives_enum {
398 	A = 0,
399 	B = 1,
400 };
401 
402 struct core_reloc_primitives {
403 	char a;
404 	int b;
405 	enum core_reloc_primitives_enum c;
406 	void *d;
407 	int (*f)(const char *);
408 };
409 
410 struct core_reloc_primitives___diff_enum_def {
411 	char a;
412 	int b;
413 	void *d;
414 	int (*f)(const char *);
415 	enum {
416 		X = 100,
417 		Y = 200,
418 	} c; /* inline enum def with differing set of values */
419 };
420 
421 struct core_reloc_primitives___diff_func_proto {
422 	void (*f)(int); /* incompatible function prototype */
423 	void *d;
424 	enum core_reloc_primitives_enum c;
425 	int b;
426 	char a;
427 };
428 
429 struct core_reloc_primitives___diff_ptr_type {
430 	const char * const d; /* different pointee type + modifiers */
431 	char a;
432 	int b;
433 	enum core_reloc_primitives_enum c;
434 	int (*f)(const char *);
435 };
436 
437 struct core_reloc_primitives___err_non_enum {
438 	char a[1];
439 	int b;
440 	int c; /* int instead of enum */
441 	void *d;
442 	int (*f)(const char *);
443 };
444 
445 struct core_reloc_primitives___err_non_int {
446 	char a[1];
447 	int *b; /* ptr instead of int */
448 	enum core_reloc_primitives_enum c;
449 	void *d;
450 	int (*f)(const char *);
451 };
452 
453 struct core_reloc_primitives___err_non_ptr {
454 	char a[1];
455 	int b;
456 	enum core_reloc_primitives_enum c;
457 	int d; /* int instead of ptr */
458 	int (*f)(const char *);
459 };
460 
461 /*
462  * MODS
463  */
464 struct core_reloc_mods_output {
465 	int a, b, c, d, e, f, g, h;
466 };
467 
468 typedef const int int_t;
469 typedef const char *char_ptr_t;
470 typedef const int arr_t[7];
471 
472 struct core_reloc_mods_substruct {
473 	int x;
474 	int y;
475 };
476 
477 typedef struct {
478 	int x;
479 	int y;
480 } core_reloc_mods_substruct_t;
481 
482 struct core_reloc_mods {
483 	int a;
484 	int_t b;
485 	char *c;
486 	char_ptr_t d;
487 	int e[3];
488 	arr_t f;
489 	struct core_reloc_mods_substruct g;
490 	core_reloc_mods_substruct_t h;
491 };
492 
493 /* a/b, c/d, e/f, and g/h pairs are swapped */
494 struct core_reloc_mods___mod_swap {
495 	int b;
496 	int_t a;
497 	char *d;
498 	char_ptr_t c;
499 	int f[3];
500 	arr_t e;
501 	struct {
502 		int y;
503 		int x;
504 	} h;
505 	core_reloc_mods_substruct_t g;
506 };
507 
508 typedef int int1_t;
509 typedef int1_t int2_t;
510 typedef int2_t int3_t;
511 
512 typedef int arr1_t[5];
513 typedef arr1_t arr2_t;
514 typedef arr2_t arr3_t;
515 typedef arr3_t arr4_t;
516 
517 typedef const char * const volatile fancy_char_ptr_t;
518 
519 typedef core_reloc_mods_substruct_t core_reloc_mods_substruct_tt;
520 
521 /* we need more typedefs */
522 struct core_reloc_mods___typedefs {
523 	core_reloc_mods_substruct_tt g;
524 	core_reloc_mods_substruct_tt h;
525 	arr4_t f;
526 	arr4_t e;
527 	fancy_char_ptr_t d;
528 	fancy_char_ptr_t c;
529 	int3_t b;
530 	int3_t a;
531 };
532 
533 /*
534  * PTR_AS_ARR
535  */
536 struct core_reloc_ptr_as_arr {
537 	int a;
538 };
539 
540 struct core_reloc_ptr_as_arr___diff_sz {
541 	int :32; /* padding */
542 	char __some_more_padding;
543 	int a;
544 };
545 
546 /*
547  * INTS
548  */
549 struct core_reloc_ints {
550 	uint8_t		u8_field;
551 	int8_t		s8_field;
552 	uint16_t	u16_field;
553 	int16_t		s16_field;
554 	uint32_t	u32_field;
555 	int32_t		s32_field;
556 	uint64_t	u64_field;
557 	int64_t		s64_field;
558 };
559 
560 /* signed/unsigned types swap */
561 struct core_reloc_ints___reverse_sign {
562 	int8_t		u8_field;
563 	uint8_t		s8_field;
564 	int16_t		u16_field;
565 	uint16_t	s16_field;
566 	int32_t		u32_field;
567 	uint32_t	s32_field;
568 	int64_t		u64_field;
569 	uint64_t	s64_field;
570 };
571 
572 struct core_reloc_ints___bool {
573 	bool		u8_field; /* bool instead of uint8 */
574 	int8_t		s8_field;
575 	uint16_t	u16_field;
576 	int16_t		s16_field;
577 	uint32_t	u32_field;
578 	int32_t		s32_field;
579 	uint64_t	u64_field;
580 	int64_t		s64_field;
581 };
582 
583 struct core_reloc_ints___err_bitfield {
584 	uint8_t		u8_field;
585 	int8_t		s8_field;
586 	uint16_t	u16_field;
587 	int16_t		s16_field;
588 	uint32_t	u32_field: 32; /* bitfields are not supported */
589 	int32_t		s32_field;
590 	uint64_t	u64_field;
591 	int64_t		s64_field;
592 };
593 
594 struct core_reloc_ints___err_wrong_sz_8 {
595 	uint16_t	u8_field; /* not 8-bit anymore */
596 	int16_t		s8_field; /* not 8-bit anymore */
597 
598 	uint16_t	u16_field;
599 	int16_t		s16_field;
600 	uint32_t	u32_field;
601 	int32_t		s32_field;
602 	uint64_t	u64_field;
603 	int64_t		s64_field;
604 };
605 
606 struct core_reloc_ints___err_wrong_sz_16 {
607 	uint8_t		u8_field;
608 	int8_t		s8_field;
609 
610 	uint32_t	u16_field; /* not 16-bit anymore */
611 	int32_t		s16_field; /* not 16-bit anymore */
612 
613 	uint32_t	u32_field;
614 	int32_t		s32_field;
615 	uint64_t	u64_field;
616 	int64_t		s64_field;
617 };
618 
619 struct core_reloc_ints___err_wrong_sz_32 {
620 	uint8_t		u8_field;
621 	int8_t		s8_field;
622 	uint16_t	u16_field;
623 	int16_t		s16_field;
624 
625 	uint64_t	u32_field; /* not 32-bit anymore */
626 	int64_t		s32_field; /* not 32-bit anymore */
627 
628 	uint64_t	u64_field;
629 	int64_t		s64_field;
630 };
631 
632 struct core_reloc_ints___err_wrong_sz_64 {
633 	uint8_t		u8_field;
634 	int8_t		s8_field;
635 	uint16_t	u16_field;
636 	int16_t		s16_field;
637 	uint32_t	u32_field;
638 	int32_t		s32_field;
639 
640 	uint32_t	u64_field; /* not 64-bit anymore */
641 	int32_t		s64_field; /* not 64-bit anymore */
642 };
643 
644 /*
645  * MISC
646  */
647 struct core_reloc_misc_output {
648 	int a, b, c;
649 };
650 
651 struct core_reloc_misc___a {
652 	int a1;
653 	int a2;
654 };
655 
656 struct core_reloc_misc___b {
657 	int b1;
658 	int b2;
659 };
660 
661 /* this one extends core_reloc_misc_extensible struct from BPF prog */
662 struct core_reloc_misc_extensible {
663 	int a;
664 	int b;
665 	int c;
666 	int d;
667 };
668