objtool.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
  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 "builtin.h"
  33. #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
  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 [OPTIONS] COMMAND [ARGS]";
  41. static struct cmd_struct objtool_cmds[] = {
  42. {"check", cmd_check, "Perform stack metadata validation on an object file" },
  43. };
  44. bool help;
  45. static void cmd_usage(void)
  46. {
  47. unsigned int i, longest = 0;
  48. printf("\n usage: %s\n\n", objtool_usage_string);
  49. for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
  50. if (longest < strlen(objtool_cmds[i].name))
  51. longest = strlen(objtool_cmds[i].name);
  52. }
  53. puts(" Commands:");
  54. for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
  55. printf(" %-*s ", longest, objtool_cmds[i].name);
  56. puts(objtool_cmds[i].help);
  57. }
  58. printf("\n");
  59. exit(1);
  60. }
  61. static void handle_options(int *argc, const char ***argv)
  62. {
  63. while (*argc > 0) {
  64. const char *cmd = (*argv)[0];
  65. if (cmd[0] != '-')
  66. break;
  67. if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h")) {
  68. help = true;
  69. break;
  70. } else {
  71. fprintf(stderr, "Unknown option: %s\n", cmd);
  72. fprintf(stderr, "\n Usage: %s\n",
  73. objtool_usage_string);
  74. exit(1);
  75. }
  76. (*argv)++;
  77. (*argc)--;
  78. }
  79. }
  80. static void handle_internal_command(int argc, const char **argv)
  81. {
  82. const char *cmd = argv[0];
  83. unsigned int i, ret;
  84. for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
  85. struct cmd_struct *p = objtool_cmds+i;
  86. if (strcmp(p->name, cmd))
  87. continue;
  88. ret = p->fn(argc, argv);
  89. exit(ret);
  90. }
  91. cmd_usage();
  92. }
  93. int main(int argc, const char **argv)
  94. {
  95. static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
  96. /* libsubcmd init */
  97. exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
  98. pager_init(UNUSED);
  99. argv++;
  100. argc--;
  101. handle_options(&argc, &argv);
  102. if (!argc || help)
  103. cmd_usage();
  104. handle_internal_command(argc, argv);
  105. return 0;
  106. }