GNU Linux-libre 4.19.264-gnu1
[releases.git] / kernel / gcov / gcc_4_7.c
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[0];
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  */
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  */
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  */
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  */
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  */
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 /* Symbolic links to be created for each profiling data file. */
163 const struct gcov_link gcov_link[] = {
164         { OBJ_TREE, "gcno" },   /* Link to .gcno file in $(objtree). */
165         { 0, NULL},
166 };
167
168 /*
169  * Determine whether a counter is active. Doesn't change at run-time.
170  */
171 static int counter_active(struct gcov_info *info, unsigned int type)
172 {
173         return info->merge[type] ? 1 : 0;
174 }
175
176 /* Determine number of active counters. Based on gcc magic. */
177 static unsigned int num_counter_active(struct gcov_info *info)
178 {
179         unsigned int i;
180         unsigned int result = 0;
181
182         for (i = 0; i < GCOV_COUNTERS; i++) {
183                 if (counter_active(info, i))
184                         result++;
185         }
186         return result;
187 }
188
189 /**
190  * gcov_info_reset - reset profiling data to zero
191  * @info: profiling data set
192  */
193 void gcov_info_reset(struct gcov_info *info)
194 {
195         struct gcov_ctr_info *ci_ptr;
196         unsigned int fi_idx;
197         unsigned int ct_idx;
198
199         for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
200                 ci_ptr = info->functions[fi_idx]->ctrs;
201
202                 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
203                         if (!counter_active(info, ct_idx))
204                                 continue;
205
206                         memset(ci_ptr->values, 0,
207                                         sizeof(gcov_type) * ci_ptr->num);
208                         ci_ptr++;
209                 }
210         }
211 }
212
213 /**
214  * gcov_info_is_compatible - check if profiling data can be added
215  * @info1: first profiling data set
216  * @info2: second profiling data set
217  *
218  * Returns non-zero if profiling data can be added, zero otherwise.
219  */
220 int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
221 {
222         return (info1->stamp == info2->stamp);
223 }
224
225 /**
226  * gcov_info_add - add up profiling data
227  * @dest: profiling data set to which data is added
228  * @source: profiling data set which is added
229  *
230  * Adds profiling counts of @source to @dest.
231  */
232 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
233 {
234         struct gcov_ctr_info *dci_ptr;
235         struct gcov_ctr_info *sci_ptr;
236         unsigned int fi_idx;
237         unsigned int ct_idx;
238         unsigned int val_idx;
239
240         for (fi_idx = 0; fi_idx < src->n_functions; fi_idx++) {
241                 dci_ptr = dst->functions[fi_idx]->ctrs;
242                 sci_ptr = src->functions[fi_idx]->ctrs;
243
244                 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
245                         if (!counter_active(src, ct_idx))
246                                 continue;
247
248                         for (val_idx = 0; val_idx < sci_ptr->num; val_idx++)
249                                 dci_ptr->values[val_idx] +=
250                                         sci_ptr->values[val_idx];
251
252                         dci_ptr++;
253                         sci_ptr++;
254                 }
255         }
256 }
257
258 /**
259  * gcov_info_dup - duplicate profiling data set
260  * @info: profiling data set to duplicate
261  *
262  * Return newly allocated duplicate on success, %NULL on error.
263  */
264 struct gcov_info *gcov_info_dup(struct gcov_info *info)
265 {
266         struct gcov_info *dup;
267         struct gcov_ctr_info *dci_ptr; /* dst counter info */
268         struct gcov_ctr_info *sci_ptr; /* src counter info */
269         unsigned int active;
270         unsigned int fi_idx; /* function info idx */
271         unsigned int ct_idx; /* counter type idx */
272         size_t fi_size; /* function info size */
273         size_t cv_size; /* counter values size */
274
275         dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
276         if (!dup)
277                 return NULL;
278
279         dup->next = NULL;
280         dup->filename = NULL;
281         dup->functions = NULL;
282
283         dup->filename = kstrdup(info->filename, GFP_KERNEL);
284         if (!dup->filename)
285                 goto err_free;
286
287         dup->functions = kcalloc(info->n_functions,
288                                  sizeof(struct gcov_fn_info *), GFP_KERNEL);
289         if (!dup->functions)
290                 goto err_free;
291
292         active = num_counter_active(info);
293         fi_size = sizeof(struct gcov_fn_info);
294         fi_size += sizeof(struct gcov_ctr_info) * active;
295
296         for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
297                 dup->functions[fi_idx] = kzalloc(fi_size, GFP_KERNEL);
298                 if (!dup->functions[fi_idx])
299                         goto err_free;
300
301                 *(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
302
303                 sci_ptr = info->functions[fi_idx]->ctrs;
304                 dci_ptr = dup->functions[fi_idx]->ctrs;
305
306                 for (ct_idx = 0; ct_idx < active; ct_idx++) {
307
308                         cv_size = sizeof(gcov_type) * sci_ptr->num;
309
310                         dci_ptr->values = vmalloc(cv_size);
311
312                         if (!dci_ptr->values)
313                                 goto err_free;
314
315                         dci_ptr->num = sci_ptr->num;
316                         memcpy(dci_ptr->values, sci_ptr->values, cv_size);
317
318                         sci_ptr++;
319                         dci_ptr++;
320                 }
321         }
322
323         return dup;
324 err_free:
325         gcov_info_free(dup);
326         return NULL;
327 }
328
329 /**
330  * gcov_info_free - release memory for profiling data set duplicate
331  * @info: profiling data set duplicate to free
332  */
333 void gcov_info_free(struct gcov_info *info)
334 {
335         unsigned int active;
336         unsigned int fi_idx;
337         unsigned int ct_idx;
338         struct gcov_ctr_info *ci_ptr;
339
340         if (!info->functions)
341                 goto free_info;
342
343         active = num_counter_active(info);
344
345         for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
346                 if (!info->functions[fi_idx])
347                         continue;
348
349                 ci_ptr = info->functions[fi_idx]->ctrs;
350
351                 for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++)
352                         vfree(ci_ptr->values);
353
354                 kfree(info->functions[fi_idx]);
355         }
356
357 free_info:
358         kfree(info->functions);
359         kfree(info->filename);
360         kfree(info);
361 }
362
363 #define ITER_STRIDE     PAGE_SIZE
364
365 /**
366  * struct gcov_iterator - specifies current file position in logical records
367  * @info: associated profiling data
368  * @buffer: buffer containing file data
369  * @size: size of buffer
370  * @pos: current position in file
371  */
372 struct gcov_iterator {
373         struct gcov_info *info;
374         void *buffer;
375         size_t size;
376         loff_t pos;
377 };
378
379 /**
380  * store_gcov_u32 - store 32 bit number in gcov format to buffer
381  * @buffer: target buffer or NULL
382  * @off: offset into the buffer
383  * @v: value to be stored
384  *
385  * Number format defined by gcc: numbers are recorded in the 32 bit
386  * unsigned binary form of the endianness of the machine generating the
387  * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
388  * store anything.
389  */
390 static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
391 {
392         u32 *data;
393
394         if (buffer) {
395                 data = buffer + off;
396                 *data = v;
397         }
398
399         return sizeof(*data);
400 }
401
402 /**
403  * store_gcov_u64 - store 64 bit number in gcov format to buffer
404  * @buffer: target buffer or NULL
405  * @off: offset into the buffer
406  * @v: value to be stored
407  *
408  * Number format defined by gcc: numbers are recorded in the 32 bit
409  * unsigned binary form of the endianness of the machine generating the
410  * file. 64 bit numbers are stored as two 32 bit numbers, the low part
411  * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
412  * anything.
413  */
414 static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
415 {
416         u32 *data;
417
418         if (buffer) {
419                 data = buffer + off;
420
421                 data[0] = (v & 0xffffffffUL);
422                 data[1] = (v >> 32);
423         }
424
425         return sizeof(*data) * 2;
426 }
427
428 /**
429  * convert_to_gcda - convert profiling data set to gcda file format
430  * @buffer: the buffer to store file data or %NULL if no data should be stored
431  * @info: profiling data set to be converted
432  *
433  * Returns the number of bytes that were/would have been stored into the buffer.
434  */
435 static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
436 {
437         struct gcov_fn_info *fi_ptr;
438         struct gcov_ctr_info *ci_ptr;
439         unsigned int fi_idx;
440         unsigned int ct_idx;
441         unsigned int cv_idx;
442         size_t pos = 0;
443
444         /* File header. */
445         pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
446         pos += store_gcov_u32(buffer, pos, info->version);
447         pos += store_gcov_u32(buffer, pos, info->stamp);
448
449 #if (__GNUC__ >= 12)
450         /* Use zero as checksum of the compilation unit. */
451         pos += store_gcov_u32(buffer, pos, 0);
452 #endif
453
454         for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
455                 fi_ptr = info->functions[fi_idx];
456
457                 /* Function record. */
458                 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
459                 pos += store_gcov_u32(buffer, pos,
460                         GCOV_TAG_FUNCTION_LENGTH * GCOV_UNIT_SIZE);
461                 pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
462                 pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum);
463                 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
464
465                 ci_ptr = fi_ptr->ctrs;
466
467                 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
468                         if (!counter_active(info, ct_idx))
469                                 continue;
470
471                         /* Counter record. */
472                         pos += store_gcov_u32(buffer, pos,
473                                               GCOV_TAG_FOR_COUNTER(ct_idx));
474                         pos += store_gcov_u32(buffer, pos,
475                                 ci_ptr->num * 2 * GCOV_UNIT_SIZE);
476
477                         for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
478                                 pos += store_gcov_u64(buffer, pos,
479                                                       ci_ptr->values[cv_idx]);
480                         }
481
482                         ci_ptr++;
483                 }
484         }
485
486         return pos;
487 }
488
489 /**
490  * gcov_iter_new - allocate and initialize profiling data iterator
491  * @info: profiling data set to be iterated
492  *
493  * Return file iterator on success, %NULL otherwise.
494  */
495 struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
496 {
497         struct gcov_iterator *iter;
498
499         iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
500         if (!iter)
501                 goto err_free;
502
503         iter->info = info;
504         /* Dry-run to get the actual buffer size. */
505         iter->size = convert_to_gcda(NULL, info);
506         iter->buffer = vmalloc(iter->size);
507         if (!iter->buffer)
508                 goto err_free;
509
510         convert_to_gcda(iter->buffer, info);
511
512         return iter;
513
514 err_free:
515         kfree(iter);
516         return NULL;
517 }
518
519
520 /**
521  * gcov_iter_get_info - return profiling data set for given file iterator
522  * @iter: file iterator
523  */
524 void gcov_iter_free(struct gcov_iterator *iter)
525 {
526         vfree(iter->buffer);
527         kfree(iter);
528 }
529
530 /**
531  * gcov_iter_get_info - return profiling data set for given file iterator
532  * @iter: file iterator
533  */
534 struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
535 {
536         return iter->info;
537 }
538
539 /**
540  * gcov_iter_start - reset file iterator to starting position
541  * @iter: file iterator
542  */
543 void gcov_iter_start(struct gcov_iterator *iter)
544 {
545         iter->pos = 0;
546 }
547
548 /**
549  * gcov_iter_next - advance file iterator to next logical record
550  * @iter: file iterator
551  *
552  * Return zero if new position is valid, non-zero if iterator has reached end.
553  */
554 int gcov_iter_next(struct gcov_iterator *iter)
555 {
556         if (iter->pos < iter->size)
557                 iter->pos += ITER_STRIDE;
558
559         if (iter->pos >= iter->size)
560                 return -EINVAL;
561
562         return 0;
563 }
564
565 /**
566  * gcov_iter_write - write data for current pos to seq_file
567  * @iter: file iterator
568  * @seq: seq_file handle
569  *
570  * Return zero on success, non-zero otherwise.
571  */
572 int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
573 {
574         size_t len;
575
576         if (iter->pos >= iter->size)
577                 return -EINVAL;
578
579         len = ITER_STRIDE;
580         if (iter->pos + len > iter->size)
581                 len = iter->size - iter->pos;
582
583         seq_write(seq, iter->buffer + iter->pos, len);
584
585         return 0;
586 }