gcc_4_7.c 11 KB

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