objtool.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2015 Josh Poimboeuf <[email protected]>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * objtool:
  19. *
  20. * The 'check' subcmd analyzes every .o file and ensures the validity of its
  21. * stack trace metadata. It enforces a set of rules on asm code and C inline
  22. * assembly code so that stack traces can be reliable.
  23. *
  24. * For more information, see tools/objtool/Documentation/stack-validation.txt.
  25. */
  26. #include <stdio.h>
  27. #include <stdbool.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <subcmd/exec-cmd.h>
  31. #include <subcmd/pager.h>
  32. #include <linux/kernel.h>
  33. #include "builtin.h"
  34. struct cmd_struct {
  35. const char *name;
  36. int (*fn)(int, const char **);
  37. const char *help;
  38. };
  39. static const char objtool_usage_string[] =
  40. "objtool COMMAND [ARGS]";
  41. static struct cmd_struct objtool_cmds[] = {
  42. {"check", cmd_check, "Perform stack metadata validation on an object file" },
  43. {"orc", cmd_orc, "Generate in-place ORC unwind tables for an object file" },
  44. };
  45. bool help;
  46. static void cmd_usage(void)
  47. {
  48. unsigned int i, longest = 0;
  49. printf("\n usage: %s\n\n", objtool_usage_string);
  50. for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
  51. if (longest < strlen(objtool_cmds[i].name))
  52. longest = strlen(objtool_cmds[i].name);
  53. }
  54. puts(" Commands:");
  55. for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
  56. printf(" %-*s ", longest, objtool_cmds[i].name);
  57. puts(objtool_cmds[i].help);
  58. }
  59. printf("\n");
  60. exit(129);
  61. }
  62. static void handle_options(int *argc, const char ***argv)
  63. {
  64. while (*argc > 0) {
  65. const char *cmd = (*argv)[0];
  66. if (cmd[0] != '-')
  67. break;
  68. if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h")) {
  69. help = true;
  70. break;
  71. } else {
  72. fprintf(stderr, "Unknown option: %s\n", cmd);
  73. cmd_usage();
  74. }
  75. (*argv)++;
  76. (*argc)--;
  77. }
  78. }
  79. static void handle_internal_command(int argc, const char **argv)
  80. {
  81. const char *cmd = argv[0];
  82. unsigned int i, ret;
  83. for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
  84. struct cmd_struct *p = objtool_cmds+i;
  85. if (strcmp(p->name, cmd))
  86. continue;
  87. ret = p->fn(argc, argv);
  88. exit(ret);
  89. }
  90. cmd_usage();
  91. }
  92. int main(int argc, const char **argv)
  93. {
  94. static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
  95. /* libsubcmd init */
  96. exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
  97. pager_init(UNUSED);
  98. argv++;
  99. argc--;
  100. handle_options(&argc, &argv);
  101. if (!argc || help)
  102. cmd_usage();
  103. handle_internal_command(argc, argv);
  104. return 0;
  105. }