objtool.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015 Josh Poimboeuf <[email protected]>
  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 help;
  17. const char *objname;
  18. static struct objtool_file file;
  19. static bool objtool_create_backup(const char *_objname)
  20. {
  21. int len = strlen(_objname);
  22. char *buf, *base, *name = malloc(len+6);
  23. int s, d, l, t;
  24. if (!name) {
  25. perror("failed backup name malloc");
  26. return false;
  27. }
  28. strcpy(name, _objname);
  29. strcpy(name + len, ".orig");
  30. d = open(name, O_CREAT|O_WRONLY|O_TRUNC, 0644);
  31. if (d < 0) {
  32. perror("failed to create backup file");
  33. return false;
  34. }
  35. s = open(_objname, O_RDONLY);
  36. if (s < 0) {
  37. perror("failed to open orig file");
  38. return false;
  39. }
  40. buf = malloc(4096);
  41. if (!buf) {
  42. perror("failed backup data malloc");
  43. return false;
  44. }
  45. while ((l = read(s, buf, 4096)) > 0) {
  46. base = buf;
  47. do {
  48. t = write(d, base, l);
  49. if (t < 0) {
  50. perror("failed backup write");
  51. return false;
  52. }
  53. base += t;
  54. l -= t;
  55. } while (l);
  56. }
  57. if (l < 0) {
  58. perror("failed backup read");
  59. return false;
  60. }
  61. free(name);
  62. free(buf);
  63. close(d);
  64. close(s);
  65. return true;
  66. }
  67. struct objtool_file *objtool_open_read(const char *_objname)
  68. {
  69. if (objname) {
  70. if (strcmp(objname, _objname)) {
  71. WARN("won't handle more than one file at a time");
  72. return NULL;
  73. }
  74. return &file;
  75. }
  76. objname = _objname;
  77. file.elf = elf_open_read(objname, O_RDWR);
  78. if (!file.elf)
  79. return NULL;
  80. if (opts.backup && !objtool_create_backup(objname)) {
  81. WARN("can't create backup file");
  82. return NULL;
  83. }
  84. INIT_LIST_HEAD(&file.insn_list);
  85. hash_init(file.insn_hash);
  86. INIT_LIST_HEAD(&file.retpoline_call_list);
  87. INIT_LIST_HEAD(&file.return_thunk_list);
  88. INIT_LIST_HEAD(&file.static_call_list);
  89. INIT_LIST_HEAD(&file.mcount_loc_list);
  90. INIT_LIST_HEAD(&file.endbr_list);
  91. file.ignore_unreachables = opts.no_unreachable;
  92. file.hints = false;
  93. return &file;
  94. }
  95. void objtool_pv_add(struct objtool_file *f, int idx, struct symbol *func)
  96. {
  97. if (!opts.noinstr)
  98. return;
  99. if (!f->pv_ops) {
  100. WARN("paravirt confusion");
  101. return;
  102. }
  103. /*
  104. * These functions will be patched into native code,
  105. * see paravirt_patch().
  106. */
  107. if (!strcmp(func->name, "_paravirt_nop") ||
  108. !strcmp(func->name, "_paravirt_ident_64"))
  109. return;
  110. /* already added this function */
  111. if (!list_empty(&func->pv_target))
  112. return;
  113. list_add(&func->pv_target, &f->pv_ops[idx].targets);
  114. f->pv_ops[idx].clean = false;
  115. }
  116. int main(int argc, const char **argv)
  117. {
  118. static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
  119. /* libsubcmd init */
  120. exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
  121. pager_init(UNUSED);
  122. return objtool_run(argc, argv);
  123. }