builtin-check.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
  4. */
  5. #include <subcmd/parse-options.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <objtool/builtin.h>
  9. #include <objtool/objtool.h>
  10. #define ERROR(format, ...) \
  11. fprintf(stderr, \
  12. "error: objtool: " format "\n", \
  13. ##__VA_ARGS__)
  14. struct opts opts;
  15. static const char * const check_usage[] = {
  16. "objtool <actions> [<options>] file.o",
  17. NULL,
  18. };
  19. static const char * const env_usage[] = {
  20. "OBJTOOL_ARGS=\"<options>\"",
  21. NULL,
  22. };
  23. static int parse_dump(const struct option *opt, const char *str, int unset)
  24. {
  25. if (!str || !strcmp(str, "orc")) {
  26. opts.dump_orc = true;
  27. return 0;
  28. }
  29. return -1;
  30. }
  31. static int parse_hacks(const struct option *opt, const char *str, int unset)
  32. {
  33. bool found = false;
  34. /*
  35. * Use strstr() as a lazy method of checking for comma-separated
  36. * options.
  37. *
  38. * No string provided == enable all options.
  39. */
  40. if (!str || strstr(str, "jump_label")) {
  41. opts.hack_jump_label = true;
  42. found = true;
  43. }
  44. if (!str || strstr(str, "noinstr")) {
  45. opts.hack_noinstr = true;
  46. found = true;
  47. }
  48. if (!str || strstr(str, "skylake")) {
  49. opts.hack_skylake = true;
  50. found = true;
  51. }
  52. return found ? 0 : -1;
  53. }
  54. static const struct option check_options[] = {
  55. OPT_GROUP("Actions:"),
  56. OPT_CALLBACK_OPTARG('h', "hacks", NULL, NULL, "jump_label,noinstr,skylake", "patch toolchain bugs/limitations", parse_hacks),
  57. OPT_BOOLEAN('i', "ibt", &opts.ibt, "validate and annotate IBT"),
  58. OPT_BOOLEAN('m', "mcount", &opts.mcount, "annotate mcount/fentry calls for ftrace"),
  59. OPT_BOOLEAN('n', "noinstr", &opts.noinstr, "validate noinstr rules"),
  60. OPT_BOOLEAN('o', "orc", &opts.orc, "generate ORC metadata"),
  61. OPT_BOOLEAN('r', "retpoline", &opts.retpoline, "validate and annotate retpoline usage"),
  62. OPT_BOOLEAN(0, "rethunk", &opts.rethunk, "validate and annotate rethunk usage"),
  63. OPT_BOOLEAN(0, "unret", &opts.unret, "validate entry unret placement"),
  64. OPT_INTEGER(0, "prefix", &opts.prefix, "generate prefix symbols"),
  65. OPT_BOOLEAN('l', "sls", &opts.sls, "validate straight-line-speculation mitigations"),
  66. OPT_BOOLEAN('s', "stackval", &opts.stackval, "validate frame pointer rules"),
  67. OPT_BOOLEAN('t', "static-call", &opts.static_call, "annotate static calls"),
  68. OPT_BOOLEAN('u', "uaccess", &opts.uaccess, "validate uaccess rules for SMAP"),
  69. OPT_BOOLEAN(0 , "cfi", &opts.cfi, "annotate kernel control flow integrity (kCFI) function preambles"),
  70. OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump),
  71. OPT_GROUP("Options:"),
  72. OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
  73. OPT_BOOLEAN(0, "backup", &opts.backup, "create .orig files before modification"),
  74. OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
  75. OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
  76. OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
  77. OPT_BOOLEAN(0, "mnop", &opts.mnop, "nop out mcount call sites"),
  78. OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
  79. OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
  80. OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
  81. OPT_BOOLEAN('v', "verbose", &opts.verbose, "verbose warnings"),
  82. OPT_END(),
  83. };
  84. int cmd_parse_options(int argc, const char **argv, const char * const usage[])
  85. {
  86. const char *envv[16] = { };
  87. char *env;
  88. int envc;
  89. env = getenv("OBJTOOL_ARGS");
  90. if (env) {
  91. envv[0] = "OBJTOOL_ARGS";
  92. for (envc = 1; envc < ARRAY_SIZE(envv); ) {
  93. envv[envc++] = env;
  94. env = strchr(env, ' ');
  95. if (!env)
  96. break;
  97. *env = '\0';
  98. env++;
  99. }
  100. parse_options(envc, envv, check_options, env_usage, 0);
  101. }
  102. env = getenv("OBJTOOL_VERBOSE");
  103. if (env && !strcmp(env, "1"))
  104. opts.verbose = true;
  105. argc = parse_options(argc, argv, check_options, usage, 0);
  106. if (argc != 1)
  107. usage_with_options(usage, check_options);
  108. return argc;
  109. }
  110. static bool opts_valid(void)
  111. {
  112. if (opts.hack_jump_label ||
  113. opts.hack_noinstr ||
  114. opts.ibt ||
  115. opts.mcount ||
  116. opts.noinstr ||
  117. opts.orc ||
  118. opts.retpoline ||
  119. opts.rethunk ||
  120. opts.sls ||
  121. opts.stackval ||
  122. opts.static_call ||
  123. opts.uaccess) {
  124. if (opts.dump_orc) {
  125. ERROR("--dump can't be combined with other actions");
  126. return false;
  127. }
  128. return true;
  129. }
  130. if (opts.unret && !opts.rethunk) {
  131. ERROR("--unret requires --rethunk");
  132. return false;
  133. }
  134. if (opts.dump_orc)
  135. return true;
  136. ERROR("At least one action required");
  137. return false;
  138. }
  139. static bool mnop_opts_valid(void)
  140. {
  141. if (opts.mnop && !opts.mcount) {
  142. ERROR("--mnop requires --mcount");
  143. return false;
  144. }
  145. return true;
  146. }
  147. static bool link_opts_valid(struct objtool_file *file)
  148. {
  149. if (opts.link)
  150. return true;
  151. if (has_multiple_files(file->elf)) {
  152. ERROR("Linked object detected, forcing --link");
  153. opts.link = true;
  154. return true;
  155. }
  156. if (opts.noinstr) {
  157. ERROR("--noinstr requires --link");
  158. return false;
  159. }
  160. if (opts.ibt) {
  161. ERROR("--ibt requires --link");
  162. return false;
  163. }
  164. if (opts.unret) {
  165. ERROR("--unret requires --link");
  166. return false;
  167. }
  168. return true;
  169. }
  170. int objtool_run(int argc, const char **argv)
  171. {
  172. const char *objname;
  173. struct objtool_file *file;
  174. int ret;
  175. argc = cmd_parse_options(argc, argv, check_usage);
  176. objname = argv[0];
  177. if (!opts_valid())
  178. return 1;
  179. if (opts.dump_orc)
  180. return orc_dump(objname);
  181. file = objtool_open_read(objname);
  182. if (!file)
  183. return 1;
  184. if (!mnop_opts_valid())
  185. return 1;
  186. if (!link_opts_valid(file))
  187. return 1;
  188. ret = check(file);
  189. if (ret)
  190. return ret;
  191. if (file->elf->changed)
  192. return elf_write(file->elf);
  193. return 0;
  194. }