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