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