gcc_4_7.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include "gcov.h"
  19. #if (__GNUC__ >= 14)
  20. #define GCOV_COUNTERS 9
  21. #elif (__GNUC__ >= 10)
  22. #define GCOV_COUNTERS 8
  23. #elif (__GNUC__ >= 7)
  24. #define GCOV_COUNTERS 9
  25. #elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
  26. #define GCOV_COUNTERS 10
  27. #else
  28. #define GCOV_COUNTERS 9
  29. #endif
  30. #define GCOV_TAG_FUNCTION_LENGTH 3
  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. static struct gcov_info *gcov_info_head;
  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. * struct gcov_fn_info - profiling meta data per function
  52. * @key: comdat key
  53. * @ident: unique ident of function
  54. * @lineno_checksum: function lineo_checksum
  55. * @cfg_checksum: function cfg checksum
  56. * @ctrs: instrumented counters
  57. *
  58. * This data is generated by gcc during compilation and doesn't change
  59. * at run-time.
  60. *
  61. * Information about a single function. This uses the trailing array
  62. * idiom. The number of counters is determined from the merge pointer
  63. * array in gcov_info. The key is used to detect which of a set of
  64. * comdat functions was selected -- it points to the gcov_info object
  65. * of the object file containing the selected comdat function.
  66. */
  67. struct gcov_fn_info {
  68. const struct gcov_info *key;
  69. unsigned int ident;
  70. unsigned int lineno_checksum;
  71. unsigned int cfg_checksum;
  72. struct gcov_ctr_info ctrs[];
  73. };
  74. /**
  75. * struct gcov_info - profiling data per object file
  76. * @version: gcov version magic indicating the gcc version used for compilation
  77. * @next: list head for a singly-linked list
  78. * @stamp: uniquifying time stamp
  79. * @checksum: unique object checksum
  80. * @filename: name of the associated gcov data file
  81. * @merge: merge functions (null for unused counter type)
  82. * @n_functions: number of instrumented functions
  83. * @functions: pointer to pointers to function information
  84. *
  85. * This data is generated by gcc during compilation and doesn't change
  86. * at run-time with the exception of the next pointer.
  87. */
  88. struct gcov_info {
  89. unsigned int version;
  90. struct gcov_info *next;
  91. unsigned int stamp;
  92. /* Since GCC 12.1 a checksum field is added. */
  93. #if (__GNUC__ >= 12)
  94. unsigned int checksum;
  95. #endif
  96. const char *filename;
  97. void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
  98. unsigned int n_functions;
  99. struct gcov_fn_info **functions;
  100. };
  101. /**
  102. * gcov_info_filename - return info filename
  103. * @info: profiling data set
  104. */
  105. const char *gcov_info_filename(struct gcov_info *info)
  106. {
  107. return info->filename;
  108. }
  109. /**
  110. * gcov_info_version - return info version
  111. * @info: profiling data set
  112. */
  113. unsigned int gcov_info_version(struct gcov_info *info)
  114. {
  115. return info->version;
  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. return info->next;
  129. }
  130. /**
  131. * gcov_info_link - link/add profiling data set to the list
  132. * @info: profiling data set
  133. */
  134. void gcov_info_link(struct gcov_info *info)
  135. {
  136. info->next = gcov_info_head;
  137. gcov_info_head = info;
  138. }
  139. /**
  140. * gcov_info_unlink - unlink/remove profiling data set from the list
  141. * @prev: previous profiling data set
  142. * @info: profiling data set
  143. */
  144. void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
  145. {
  146. if (prev)
  147. prev->next = info->next;
  148. else
  149. gcov_info_head = info->next;
  150. }
  151. /**
  152. * gcov_info_within_module - check if a profiling data set belongs to a module
  153. * @info: profiling data set
  154. * @mod: module
  155. *
  156. * Returns true if profiling data belongs module, false otherwise.
  157. */
  158. bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
  159. {
  160. return within_module((unsigned long)info, mod);
  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. * Determine whether a counter is active. Doesn't change at run-time.
  169. */
  170. static int counter_active(struct gcov_info *info, unsigned int type)
  171. {
  172. return info->merge[type] ? 1 : 0;
  173. }
  174. /* Determine number of active counters. Based on gcc magic. */
  175. static unsigned int num_counter_active(struct gcov_info *info)
  176. {
  177. unsigned int i;
  178. unsigned int result = 0;
  179. for (i = 0; i < GCOV_COUNTERS; i++) {
  180. if (counter_active(info, i))
  181. result++;
  182. }
  183. return result;
  184. }
  185. /**
  186. * gcov_info_reset - reset profiling data to zero
  187. * @info: profiling data set
  188. */
  189. void gcov_info_reset(struct gcov_info *info)
  190. {
  191. struct gcov_ctr_info *ci_ptr;
  192. unsigned int fi_idx;
  193. unsigned int ct_idx;
  194. for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
  195. ci_ptr = info->functions[fi_idx]->ctrs;
  196. for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
  197. if (!counter_active(info, ct_idx))
  198. continue;
  199. memset(ci_ptr->values, 0,
  200. sizeof(gcov_type) * ci_ptr->num);
  201. ci_ptr++;
  202. }
  203. }
  204. }
  205. /**
  206. * gcov_info_is_compatible - check if profiling data can be added
  207. * @info1: first profiling data set
  208. * @info2: second profiling data set
  209. *
  210. * Returns non-zero if profiling data can be added, zero otherwise.
  211. */
  212. int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
  213. {
  214. return (info1->stamp == info2->stamp);
  215. }
  216. /**
  217. * gcov_info_add - add up profiling data
  218. * @dst: profiling data set to which data is added
  219. * @src: profiling data set which is added
  220. *
  221. * Adds profiling counts of @src to @dst.
  222. */
  223. void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
  224. {
  225. struct gcov_ctr_info *dci_ptr;
  226. struct gcov_ctr_info *sci_ptr;
  227. unsigned int fi_idx;
  228. unsigned int ct_idx;
  229. unsigned int val_idx;
  230. for (fi_idx = 0; fi_idx < src->n_functions; fi_idx++) {
  231. dci_ptr = dst->functions[fi_idx]->ctrs;
  232. sci_ptr = src->functions[fi_idx]->ctrs;
  233. for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
  234. if (!counter_active(src, ct_idx))
  235. continue;
  236. for (val_idx = 0; val_idx < sci_ptr->num; val_idx++)
  237. dci_ptr->values[val_idx] +=
  238. sci_ptr->values[val_idx];
  239. dci_ptr++;
  240. sci_ptr++;
  241. }
  242. }
  243. }
  244. /**
  245. * gcov_info_dup - duplicate profiling data set
  246. * @info: profiling data set to duplicate
  247. *
  248. * Return newly allocated duplicate on success, %NULL on error.
  249. */
  250. struct gcov_info *gcov_info_dup(struct gcov_info *info)
  251. {
  252. struct gcov_info *dup;
  253. struct gcov_ctr_info *dci_ptr; /* dst counter info */
  254. struct gcov_ctr_info *sci_ptr; /* src counter info */
  255. unsigned int active;
  256. unsigned int fi_idx; /* function info idx */
  257. unsigned int ct_idx; /* counter type idx */
  258. size_t fi_size; /* function info size */
  259. size_t cv_size; /* counter values size */
  260. dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
  261. if (!dup)
  262. return NULL;
  263. dup->next = NULL;
  264. dup->filename = NULL;
  265. dup->functions = NULL;
  266. dup->filename = kstrdup(info->filename, GFP_KERNEL);
  267. if (!dup->filename)
  268. goto err_free;
  269. dup->functions = kcalloc(info->n_functions,
  270. sizeof(struct gcov_fn_info *), GFP_KERNEL);
  271. if (!dup->functions)
  272. goto err_free;
  273. active = num_counter_active(info);
  274. fi_size = sizeof(struct gcov_fn_info);
  275. fi_size += sizeof(struct gcov_ctr_info) * active;
  276. for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
  277. dup->functions[fi_idx] = kzalloc(fi_size, GFP_KERNEL);
  278. if (!dup->functions[fi_idx])
  279. goto err_free;
  280. *(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
  281. sci_ptr = info->functions[fi_idx]->ctrs;
  282. dci_ptr = dup->functions[fi_idx]->ctrs;
  283. for (ct_idx = 0; ct_idx < active; ct_idx++) {
  284. cv_size = sizeof(gcov_type) * sci_ptr->num;
  285. dci_ptr->values = kvmalloc(cv_size, GFP_KERNEL);
  286. if (!dci_ptr->values)
  287. goto err_free;
  288. dci_ptr->num = sci_ptr->num;
  289. memcpy(dci_ptr->values, sci_ptr->values, cv_size);
  290. sci_ptr++;
  291. dci_ptr++;
  292. }
  293. }
  294. return dup;
  295. err_free:
  296. gcov_info_free(dup);
  297. return NULL;
  298. }
  299. /**
  300. * gcov_info_free - release memory for profiling data set duplicate
  301. * @info: profiling data set duplicate to free
  302. */
  303. void gcov_info_free(struct gcov_info *info)
  304. {
  305. unsigned int active;
  306. unsigned int fi_idx;
  307. unsigned int ct_idx;
  308. struct gcov_ctr_info *ci_ptr;
  309. if (!info->functions)
  310. goto free_info;
  311. active = num_counter_active(info);
  312. for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
  313. if (!info->functions[fi_idx])
  314. continue;
  315. ci_ptr = info->functions[fi_idx]->ctrs;
  316. for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++)
  317. kvfree(ci_ptr->values);
  318. kfree(info->functions[fi_idx]);
  319. }
  320. free_info:
  321. kfree(info->functions);
  322. kfree(info->filename);
  323. kfree(info);
  324. }
  325. /**
  326. * convert_to_gcda - convert profiling data set to gcda file format
  327. * @buffer: the buffer to store file data or %NULL if no data should be stored
  328. * @info: profiling data set to be converted
  329. *
  330. * Returns the number of bytes that were/would have been stored into the buffer.
  331. */
  332. size_t convert_to_gcda(char *buffer, struct gcov_info *info)
  333. {
  334. struct gcov_fn_info *fi_ptr;
  335. struct gcov_ctr_info *ci_ptr;
  336. unsigned int fi_idx;
  337. unsigned int ct_idx;
  338. unsigned int cv_idx;
  339. size_t pos = 0;
  340. /* File header. */
  341. pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
  342. pos += store_gcov_u32(buffer, pos, info->version);
  343. pos += store_gcov_u32(buffer, pos, info->stamp);
  344. #if (__GNUC__ >= 12)
  345. /* Use zero as checksum of the compilation unit. */
  346. pos += store_gcov_u32(buffer, pos, 0);
  347. #endif
  348. for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
  349. fi_ptr = info->functions[fi_idx];
  350. /* Function record. */
  351. pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
  352. pos += store_gcov_u32(buffer, pos,
  353. GCOV_TAG_FUNCTION_LENGTH * GCOV_UNIT_SIZE);
  354. pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
  355. pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum);
  356. pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
  357. ci_ptr = fi_ptr->ctrs;
  358. for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
  359. if (!counter_active(info, ct_idx))
  360. continue;
  361. /* Counter record. */
  362. pos += store_gcov_u32(buffer, pos,
  363. GCOV_TAG_FOR_COUNTER(ct_idx));
  364. pos += store_gcov_u32(buffer, pos,
  365. ci_ptr->num * 2 * GCOV_UNIT_SIZE);
  366. for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
  367. pos += store_gcov_u64(buffer, pos,
  368. ci_ptr->values[cv_idx]);
  369. }
  370. ci_ptr++;
  371. }
  372. }
  373. return pos;
  374. }