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/mm.h> 19 #include "gcov.h" 20 21 #if (__GNUC__ >= 14) 22 #define GCOV_COUNTERS 9 23 #elif (__GNUC__ >= 10) 24 #define GCOV_COUNTERS 8 25 #else 26 #define GCOV_COUNTERS 9 27 #endif 28 29 #define GCOV_TAG_FUNCTION_LENGTH 3 30 31 /* Since GCC 12.1 sizes are in BYTES and not in WORDS (4B). */ 32 #if (__GNUC__ >= 12) 33 #define GCOV_UNIT_SIZE 4 34 #else 35 #define GCOV_UNIT_SIZE 1 36 #endif 37 38 static struct gcov_info *gcov_info_head; 39 40 /** 41 * struct gcov_ctr_info - information about counters for a single function 42 * @num: number of counter values for this type 43 * @values: array of counter values for this type 44 * 45 * This data is generated by gcc during compilation and doesn't change 46 * at run-time with the exception of the values array. 47 */ 48 struct gcov_ctr_info { 49 unsigned int num; 50 gcov_type *values; 51 }; 52 53 /** 54 * struct gcov_fn_info - profiling meta data per function 55 * @key: comdat key 56 * @ident: unique ident of function 57 * @lineno_checksum: function lineo_checksum 58 * @cfg_checksum: function cfg checksum 59 * @ctrs: instrumented counters 60 * 61 * This data is generated by gcc during compilation and doesn't change 62 * at run-time. 63 * 64 * Information about a single function. This uses the trailing array 65 * idiom. The number of counters is determined from the merge pointer 66 * array in gcov_info. The key is used to detect which of a set of 67 * comdat functions was selected -- it points to the gcov_info object 68 * of the object file containing the selected comdat function. 69 */ 70 struct gcov_fn_info { 71 const struct gcov_info *key; 72 unsigned int ident; 73 unsigned int lineno_checksum; 74 unsigned int cfg_checksum; 75 struct gcov_ctr_info ctrs[]; 76 }; 77 78 /** 79 * struct gcov_info - profiling data per object file 80 * @version: gcov version magic indicating the gcc version used for compilation 81 * @next: list head for a singly-linked list 82 * @stamp: uniquifying time stamp 83 * @checksum: unique object checksum 84 * @filename: name of the associated gcov data file 85 * @merge: merge functions (null for unused counter type) 86 * @n_functions: number of instrumented functions 87 * @functions: pointer to pointers to function information 88 * 89 * This data is generated by gcc during compilation and doesn't change 90 * at run-time with the exception of the next pointer. 91 */ 92 struct gcov_info { 93 unsigned int version; 94 struct gcov_info *next; 95 unsigned int stamp; 96 /* Since GCC 12.1 a checksum field is added. */ 97 #if (__GNUC__ >= 12) 98 unsigned int checksum; 99 #endif 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 /** 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 */ 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 */ 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. */ 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 */ 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 */ 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 * @dst: profiling data set to which data is added 240 * @src: profiling data set which is added 241 * 242 * Adds profiling counts of @src to @dst. 243 */ 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 */ 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 = kvmalloc(cv_size, GFP_KERNEL); 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 */ 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 kvfree(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 /** 376 * convert_to_gcda - convert profiling data set to gcda file format 377 * @buffer: the buffer to store file data or %NULL if no data should be stored 378 * @info: profiling data set to be converted 379 * 380 * Returns the number of bytes that were/would have been stored into the buffer. 381 */ 382 size_t convert_to_gcda(char *buffer, struct gcov_info *info) 383 { 384 struct gcov_fn_info *fi_ptr; 385 struct gcov_ctr_info *ci_ptr; 386 unsigned int fi_idx; 387 unsigned int ct_idx; 388 unsigned int cv_idx; 389 size_t pos = 0; 390 391 /* File header. */ 392 pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC); 393 pos += store_gcov_u32(buffer, pos, info->version); 394 pos += store_gcov_u32(buffer, pos, info->stamp); 395 396 #if (__GNUC__ >= 12) 397 /* Use zero as checksum of the compilation unit. */ 398 pos += store_gcov_u32(buffer, pos, 0); 399 #endif 400 401 for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) { 402 fi_ptr = info->functions[fi_idx]; 403 404 /* Function record. */ 405 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION); 406 pos += store_gcov_u32(buffer, pos, 407 GCOV_TAG_FUNCTION_LENGTH * GCOV_UNIT_SIZE); 408 pos += store_gcov_u32(buffer, pos, fi_ptr->ident); 409 pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum); 410 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum); 411 412 ci_ptr = fi_ptr->ctrs; 413 414 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) { 415 if (!counter_active(info, ct_idx)) 416 continue; 417 418 /* Counter record. */ 419 pos += store_gcov_u32(buffer, pos, 420 GCOV_TAG_FOR_COUNTER(ct_idx)); 421 pos += store_gcov_u32(buffer, pos, 422 ci_ptr->num * 2 * GCOV_UNIT_SIZE); 423 424 for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) { 425 pos += store_gcov_u64(buffer, pos, 426 ci_ptr->values[cv_idx]); 427 } 428 429 ci_ptr++; 430 } 431 } 432 433 return pos; 434 } 435