1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This code provides functions to handle gcc's profiling data format
4 * introduced with gcc 4.7.
5 *
6 * This file is based heavily on gcc_3_4.c file.
7 *
8 * For a better understanding, refer to gcc source:
9 * gcc/gcov-io.h
10 * libgcc/libgcov.c
11 *
12 * Uses gcc-internal data definitions.
13 */
14
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/seq_file.h>
19 #include <linux/vmalloc.h>
20 #include "gcov.h"
21
22 #if (__GNUC__ >= 10)
23 #define GCOV_COUNTERS 8
24 #elif (__GNUC__ >= 7)
25 #define GCOV_COUNTERS 9
26 #elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
27 #define GCOV_COUNTERS 10
28 #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
29 #define GCOV_COUNTERS 9
30 #else
31 #define GCOV_COUNTERS 8
32 #endif
33
34 #define GCOV_TAG_FUNCTION_LENGTH 3
35
36 /* Since GCC 12.1 sizes are in BYTES and not in WORDS (4B). */
37 #if (__GNUC__ >= 12)
38 #define GCOV_UNIT_SIZE 4
39 #else
40 #define GCOV_UNIT_SIZE 1
41 #endif
42
43 static struct gcov_info *gcov_info_head;
44
45 /**
46 * struct gcov_ctr_info - information about counters for a single function
47 * @num: number of counter values for this type
48 * @values: array of counter values for this type
49 *
50 * This data is generated by gcc during compilation and doesn't change
51 * at run-time with the exception of the values array.
52 */
53 struct gcov_ctr_info {
54 unsigned int num;
55 gcov_type *values;
56 };
57
58 /**
59 * struct gcov_fn_info - profiling meta data per function
60 * @key: comdat key
61 * @ident: unique ident of function
62 * @lineno_checksum: function lineo_checksum
63 * @cfg_checksum: function cfg checksum
64 * @ctrs: instrumented counters
65 *
66 * This data is generated by gcc during compilation and doesn't change
67 * at run-time.
68 *
69 * Information about a single function. This uses the trailing array
70 * idiom. The number of counters is determined from the merge pointer
71 * array in gcov_info. The key is used to detect which of a set of
72 * comdat functions was selected -- it points to the gcov_info object
73 * of the object file containing the selected comdat function.
74 */
75 struct gcov_fn_info {
76 const struct gcov_info *key;
77 unsigned int ident;
78 unsigned int lineno_checksum;
79 unsigned int cfg_checksum;
80 struct gcov_ctr_info ctrs[];
81 };
82
83 /**
84 * struct gcov_info - profiling data per object file
85 * @version: gcov version magic indicating the gcc version used for compilation
86 * @next: list head for a singly-linked list
87 * @stamp: uniquifying time stamp
88 * @filename: name of the associated gcov data file
89 * @merge: merge functions (null for unused counter type)
90 * @n_functions: number of instrumented functions
91 * @functions: pointer to pointers to function information
92 *
93 * This data is generated by gcc during compilation and doesn't change
94 * at run-time with the exception of the next pointer.
95 */
96 struct gcov_info {
97 unsigned int version;
98 struct gcov_info *next;
99 unsigned int stamp;
100 const char *filename;
101 void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
102 unsigned int n_functions;
103 struct gcov_fn_info **functions;
104 };
105
106 /**
107 * gcov_info_filename - return info filename
108 * @info: profiling data set
109 */
gcov_info_filename(struct gcov_info * info)110 const char *gcov_info_filename(struct gcov_info *info)
111 {
112 return info->filename;
113 }
114
115 /**
116 * gcov_info_version - return info version
117 * @info: profiling data set
118 */
gcov_info_version(struct gcov_info * info)119 unsigned int gcov_info_version(struct gcov_info *info)
120 {
121 return info->version;
122 }
123
124 /**
125 * gcov_info_next - return next profiling data set
126 * @info: profiling data set
127 *
128 * Returns next gcov_info following @info or first gcov_info in the chain if
129 * @info is %NULL.
130 */
gcov_info_next(struct gcov_info * info)131 struct gcov_info *gcov_info_next(struct gcov_info *info)
132 {
133 if (!info)
134 return gcov_info_head;
135
136 return info->next;
137 }
138
139 /**
140 * gcov_info_link - link/add profiling data set to the list
141 * @info: profiling data set
142 */
gcov_info_link(struct gcov_info * info)143 void gcov_info_link(struct gcov_info *info)
144 {
145 info->next = gcov_info_head;
146 gcov_info_head = info;
147 }
148
149 /**
150 * gcov_info_unlink - unlink/remove profiling data set from the list
151 * @prev: previous profiling data set
152 * @info: profiling data set
153 */
gcov_info_unlink(struct gcov_info * prev,struct gcov_info * info)154 void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
155 {
156 if (prev)
157 prev->next = info->next;
158 else
159 gcov_info_head = info->next;
160 }
161
162 /**
163 * gcov_info_within_module - check if a profiling data set belongs to a module
164 * @info: profiling data set
165 * @mod: module
166 *
167 * Returns true if profiling data belongs module, false otherwise.
168 */
gcov_info_within_module(struct gcov_info * info,struct module * mod)169 bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
170 {
171 return within_module((unsigned long)info, mod);
172 }
173
174 /* Symbolic links to be created for each profiling data file. */
175 const struct gcov_link gcov_link[] = {
176 { OBJ_TREE, "gcno" }, /* Link to .gcno file in $(objtree). */
177 { 0, NULL},
178 };
179
180 /*
181 * Determine whether a counter is active. Doesn't change at run-time.
182 */
counter_active(struct gcov_info * info,unsigned int type)183 static int counter_active(struct gcov_info *info, unsigned int type)
184 {
185 return info->merge[type] ? 1 : 0;
186 }
187
188 /* Determine number of active counters. Based on gcc magic. */
num_counter_active(struct gcov_info * info)189 static unsigned int num_counter_active(struct gcov_info *info)
190 {
191 unsigned int i;
192 unsigned int result = 0;
193
194 for (i = 0; i < GCOV_COUNTERS; i++) {
195 if (counter_active(info, i))
196 result++;
197 }
198 return result;
199 }
200
201 /**
202 * gcov_info_reset - reset profiling data to zero
203 * @info: profiling data set
204 */
gcov_info_reset(struct gcov_info * info)205 void gcov_info_reset(struct gcov_info *info)
206 {
207 struct gcov_ctr_info *ci_ptr;
208 unsigned int fi_idx;
209 unsigned int ct_idx;
210
211 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
212 ci_ptr = info->functions[fi_idx]->ctrs;
213
214 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
215 if (!counter_active(info, ct_idx))
216 continue;
217
218 memset(ci_ptr->values, 0,
219 sizeof(gcov_type) * ci_ptr->num);
220 ci_ptr++;
221 }
222 }
223 }
224
225 /**
226 * gcov_info_is_compatible - check if profiling data can be added
227 * @info1: first profiling data set
228 * @info2: second profiling data set
229 *
230 * Returns non-zero if profiling data can be added, zero otherwise.
231 */
gcov_info_is_compatible(struct gcov_info * info1,struct gcov_info * info2)232 int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
233 {
234 return (info1->stamp == info2->stamp);
235 }
236
237 /**
238 * gcov_info_add - add up profiling data
239 * @dest: profiling data set to which data is added
240 * @source: profiling data set which is added
241 *
242 * Adds profiling counts of @source to @dest.
243 */
gcov_info_add(struct gcov_info * dst,struct gcov_info * src)244 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
245 {
246 struct gcov_ctr_info *dci_ptr;
247 struct gcov_ctr_info *sci_ptr;
248 unsigned int fi_idx;
249 unsigned int ct_idx;
250 unsigned int val_idx;
251
252 for (fi_idx = 0; fi_idx < src->n_functions; fi_idx++) {
253 dci_ptr = dst->functions[fi_idx]->ctrs;
254 sci_ptr = src->functions[fi_idx]->ctrs;
255
256 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
257 if (!counter_active(src, ct_idx))
258 continue;
259
260 for (val_idx = 0; val_idx < sci_ptr->num; val_idx++)
261 dci_ptr->values[val_idx] +=
262 sci_ptr->values[val_idx];
263
264 dci_ptr++;
265 sci_ptr++;
266 }
267 }
268 }
269
270 /**
271 * gcov_info_dup - duplicate profiling data set
272 * @info: profiling data set to duplicate
273 *
274 * Return newly allocated duplicate on success, %NULL on error.
275 */
gcov_info_dup(struct gcov_info * info)276 struct gcov_info *gcov_info_dup(struct gcov_info *info)
277 {
278 struct gcov_info *dup;
279 struct gcov_ctr_info *dci_ptr; /* dst counter info */
280 struct gcov_ctr_info *sci_ptr; /* src counter info */
281 unsigned int active;
282 unsigned int fi_idx; /* function info idx */
283 unsigned int ct_idx; /* counter type idx */
284 size_t fi_size; /* function info size */
285 size_t cv_size; /* counter values size */
286
287 dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
288 if (!dup)
289 return NULL;
290
291 dup->next = NULL;
292 dup->filename = NULL;
293 dup->functions = NULL;
294
295 dup->filename = kstrdup(info->filename, GFP_KERNEL);
296 if (!dup->filename)
297 goto err_free;
298
299 dup->functions = kcalloc(info->n_functions,
300 sizeof(struct gcov_fn_info *), GFP_KERNEL);
301 if (!dup->functions)
302 goto err_free;
303
304 active = num_counter_active(info);
305 fi_size = sizeof(struct gcov_fn_info);
306 fi_size += sizeof(struct gcov_ctr_info) * active;
307
308 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
309 dup->functions[fi_idx] = kzalloc(fi_size, GFP_KERNEL);
310 if (!dup->functions[fi_idx])
311 goto err_free;
312
313 *(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
314
315 sci_ptr = info->functions[fi_idx]->ctrs;
316 dci_ptr = dup->functions[fi_idx]->ctrs;
317
318 for (ct_idx = 0; ct_idx < active; ct_idx++) {
319
320 cv_size = sizeof(gcov_type) * sci_ptr->num;
321
322 dci_ptr->values = vmalloc(cv_size);
323
324 if (!dci_ptr->values)
325 goto err_free;
326
327 dci_ptr->num = sci_ptr->num;
328 memcpy(dci_ptr->values, sci_ptr->values, cv_size);
329
330 sci_ptr++;
331 dci_ptr++;
332 }
333 }
334
335 return dup;
336 err_free:
337 gcov_info_free(dup);
338 return NULL;
339 }
340
341 /**
342 * gcov_info_free - release memory for profiling data set duplicate
343 * @info: profiling data set duplicate to free
344 */
gcov_info_free(struct gcov_info * info)345 void gcov_info_free(struct gcov_info *info)
346 {
347 unsigned int active;
348 unsigned int fi_idx;
349 unsigned int ct_idx;
350 struct gcov_ctr_info *ci_ptr;
351
352 if (!info->functions)
353 goto free_info;
354
355 active = num_counter_active(info);
356
357 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
358 if (!info->functions[fi_idx])
359 continue;
360
361 ci_ptr = info->functions[fi_idx]->ctrs;
362
363 for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++)
364 vfree(ci_ptr->values);
365
366 kfree(info->functions[fi_idx]);
367 }
368
369 free_info:
370 kfree(info->functions);
371 kfree(info->filename);
372 kfree(info);
373 }
374
375 #define ITER_STRIDE PAGE_SIZE
376
377 /**
378 * struct gcov_iterator - specifies current file position in logical records
379 * @info: associated profiling data
380 * @buffer: buffer containing file data
381 * @size: size of buffer
382 * @pos: current position in file
383 */
384 struct gcov_iterator {
385 struct gcov_info *info;
386 void *buffer;
387 size_t size;
388 loff_t pos;
389 };
390
391 /**
392 * store_gcov_u32 - store 32 bit number in gcov format to buffer
393 * @buffer: target buffer or NULL
394 * @off: offset into the buffer
395 * @v: value to be stored
396 *
397 * Number format defined by gcc: numbers are recorded in the 32 bit
398 * unsigned binary form of the endianness of the machine generating the
399 * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
400 * store anything.
401 */
store_gcov_u32(void * buffer,size_t off,u32 v)402 static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
403 {
404 u32 *data;
405
406 if (buffer) {
407 data = buffer + off;
408 *data = v;
409 }
410
411 return sizeof(*data);
412 }
413
414 /**
415 * store_gcov_u64 - store 64 bit number in gcov format to buffer
416 * @buffer: target buffer or NULL
417 * @off: offset into the buffer
418 * @v: value to be stored
419 *
420 * Number format defined by gcc: numbers are recorded in the 32 bit
421 * unsigned binary form of the endianness of the machine generating the
422 * file. 64 bit numbers are stored as two 32 bit numbers, the low part
423 * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
424 * anything.
425 */
store_gcov_u64(void * buffer,size_t off,u64 v)426 static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
427 {
428 u32 *data;
429
430 if (buffer) {
431 data = buffer + off;
432
433 data[0] = (v & 0xffffffffUL);
434 data[1] = (v >> 32);
435 }
436
437 return sizeof(*data) * 2;
438 }
439
440 /**
441 * convert_to_gcda - convert profiling data set to gcda file format
442 * @buffer: the buffer to store file data or %NULL if no data should be stored
443 * @info: profiling data set to be converted
444 *
445 * Returns the number of bytes that were/would have been stored into the buffer.
446 */
convert_to_gcda(char * buffer,struct gcov_info * info)447 static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
448 {
449 struct gcov_fn_info *fi_ptr;
450 struct gcov_ctr_info *ci_ptr;
451 unsigned int fi_idx;
452 unsigned int ct_idx;
453 unsigned int cv_idx;
454 size_t pos = 0;
455
456 /* File header. */
457 pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
458 pos += store_gcov_u32(buffer, pos, info->version);
459 pos += store_gcov_u32(buffer, pos, info->stamp);
460
461 #if (__GNUC__ >= 12)
462 /* Use zero as checksum of the compilation unit. */
463 pos += store_gcov_u32(buffer, pos, 0);
464 #endif
465
466 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
467 fi_ptr = info->functions[fi_idx];
468
469 /* Function record. */
470 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
471 pos += store_gcov_u32(buffer, pos,
472 GCOV_TAG_FUNCTION_LENGTH * GCOV_UNIT_SIZE);
473 pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
474 pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum);
475 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
476
477 ci_ptr = fi_ptr->ctrs;
478
479 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
480 if (!counter_active(info, ct_idx))
481 continue;
482
483 /* Counter record. */
484 pos += store_gcov_u32(buffer, pos,
485 GCOV_TAG_FOR_COUNTER(ct_idx));
486 pos += store_gcov_u32(buffer, pos,
487 ci_ptr->num * 2 * GCOV_UNIT_SIZE);
488
489 for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
490 pos += store_gcov_u64(buffer, pos,
491 ci_ptr->values[cv_idx]);
492 }
493
494 ci_ptr++;
495 }
496 }
497
498 return pos;
499 }
500
501 /**
502 * gcov_iter_new - allocate and initialize profiling data iterator
503 * @info: profiling data set to be iterated
504 *
505 * Return file iterator on success, %NULL otherwise.
506 */
gcov_iter_new(struct gcov_info * info)507 struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
508 {
509 struct gcov_iterator *iter;
510
511 iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
512 if (!iter)
513 goto err_free;
514
515 iter->info = info;
516 /* Dry-run to get the actual buffer size. */
517 iter->size = convert_to_gcda(NULL, info);
518 iter->buffer = vmalloc(iter->size);
519 if (!iter->buffer)
520 goto err_free;
521
522 convert_to_gcda(iter->buffer, info);
523
524 return iter;
525
526 err_free:
527 kfree(iter);
528 return NULL;
529 }
530
531
532 /**
533 * gcov_iter_get_info - return profiling data set for given file iterator
534 * @iter: file iterator
535 */
gcov_iter_free(struct gcov_iterator * iter)536 void gcov_iter_free(struct gcov_iterator *iter)
537 {
538 vfree(iter->buffer);
539 kfree(iter);
540 }
541
542 /**
543 * gcov_iter_get_info - return profiling data set for given file iterator
544 * @iter: file iterator
545 */
gcov_iter_get_info(struct gcov_iterator * iter)546 struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
547 {
548 return iter->info;
549 }
550
551 /**
552 * gcov_iter_start - reset file iterator to starting position
553 * @iter: file iterator
554 */
gcov_iter_start(struct gcov_iterator * iter)555 void gcov_iter_start(struct gcov_iterator *iter)
556 {
557 iter->pos = 0;
558 }
559
560 /**
561 * gcov_iter_next - advance file iterator to next logical record
562 * @iter: file iterator
563 *
564 * Return zero if new position is valid, non-zero if iterator has reached end.
565 */
gcov_iter_next(struct gcov_iterator * iter)566 int gcov_iter_next(struct gcov_iterator *iter)
567 {
568 if (iter->pos < iter->size)
569 iter->pos += ITER_STRIDE;
570
571 if (iter->pos >= iter->size)
572 return -EINVAL;
573
574 return 0;
575 }
576
577 /**
578 * gcov_iter_write - write data for current pos to seq_file
579 * @iter: file iterator
580 * @seq: seq_file handle
581 *
582 * Return zero on success, non-zero otherwise.
583 */
gcov_iter_write(struct gcov_iterator * iter,struct seq_file * seq)584 int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
585 {
586 size_t len;
587
588 if (iter->pos >= iter->size)
589 return -EINVAL;
590
591 len = ITER_STRIDE;
592 if (iter->pos + len > iter->size)
593 len = iter->size - iter->pos;
594
595 seq_write(seq, iter->buffer + iter->pos, len);
596
597 return 0;
598 }
599