objtool.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
  4. */
  5. #include <stdio.h>
  6. #include <stdbool.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <subcmd/exec-cmd.h>
  11. #include <subcmd/pager.h>
  12. #include <linux/kernel.h>
  13. #include <objtool/builtin.h>
  14. #include <objtool/objtool.h>
  15. #include <objtool/warn.h>
  16. bool debug;
  17. int indent;
  18. static struct objtool_file file;
  19. struct objtool_file *objtool_open_read(const char *filename)
  20. {
  21. if (file.elf) {
  22. ERROR("won't handle more than one file at a time");
  23. return NULL;
  24. }
  25. file.elf = elf_open_read(filename, O_RDWR);
  26. if (!file.elf)
  27. return NULL;
  28. hash_init(file.insn_hash);
  29. INIT_LIST_HEAD(&file.retpoline_call_list);
  30. INIT_LIST_HEAD(&file.return_thunk_list);
  31. INIT_LIST_HEAD(&file.static_call_list);
  32. INIT_LIST_HEAD(&file.mcount_loc_list);
  33. INIT_LIST_HEAD(&file.endbr_list);
  34. INIT_LIST_HEAD(&file.call_list);
  35. file.ignore_unreachables = opts.no_unreachable;
  36. file.hints = false;
  37. return &file;
  38. }
  39. int objtool_pv_add(struct objtool_file *f, int idx, struct symbol *func)
  40. {
  41. if (!opts.noinstr)
  42. return 0;
  43. if (!f->pv_ops) {
  44. ERROR("paravirt confusion");
  45. return -1;
  46. }
  47. /*
  48. * These functions will be patched into native code,
  49. * see paravirt_patch().
  50. */
  51. if (!strcmp(func->name, "_paravirt_nop") ||
  52. !strcmp(func->name, "_paravirt_ident_64"))
  53. return 0;
  54. /* already added this function */
  55. if (!list_empty(&func->pv_target))
  56. return 0;
  57. list_add(&func->pv_target, &f->pv_ops[idx].targets);
  58. f->pv_ops[idx].clean = false;
  59. return 0;
  60. }
  61. char *top_level_dir(const char *file)
  62. {
  63. ssize_t len, self_len, file_len;
  64. char self[PATH_MAX], *str;
  65. int i;
  66. len = readlink("/proc/self/exe", self, sizeof(self) - 1);
  67. if (len <= 0)
  68. return NULL;
  69. self[len] = '\0';
  70. for (i = 0; i < 3; i++) {
  71. char *s = strrchr(self, '/');
  72. if (!s)
  73. return NULL;
  74. *s = '\0';
  75. }
  76. self_len = strlen(self);
  77. file_len = strlen(file);
  78. str = malloc(self_len + file_len + 2);
  79. if (!str)
  80. return NULL;
  81. memcpy(str, self, self_len);
  82. str[self_len] = '/';
  83. strcpy(str + self_len + 1, file);
  84. return str;
  85. }
  86. int main(int argc, const char **argv)
  87. {
  88. static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
  89. if (init_signal_handler())
  90. return -1;
  91. /* libsubcmd init */
  92. exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
  93. pager_init(UNUSED);
  94. if (argc > 1 && !strcmp(argv[1], "klp")) {
  95. argc--;
  96. argv++;
  97. return cmd_klp(argc, argv);
  98. }
  99. return objtool_run(argc, argv);
  100. }