orc_gen.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
  4. */
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <linux/objtool_types.h>
  8. #include <asm/orc_types.h>
  9. #include <objtool/check.h>
  10. #include <objtool/orc.h>
  11. #include <objtool/warn.h>
  12. #include <objtool/endianness.h>
  13. struct orc_list_entry {
  14. struct list_head list;
  15. struct orc_entry orc;
  16. struct section *insn_sec;
  17. unsigned long insn_off;
  18. };
  19. static int orc_list_add(struct list_head *orc_list, struct orc_entry *orc,
  20. struct section *sec, unsigned long offset)
  21. {
  22. struct orc_list_entry *entry = malloc(sizeof(*entry));
  23. if (!entry) {
  24. WARN("malloc failed");
  25. return -1;
  26. }
  27. entry->orc = *orc;
  28. entry->insn_sec = sec;
  29. entry->insn_off = offset;
  30. list_add_tail(&entry->list, orc_list);
  31. return 0;
  32. }
  33. static unsigned long alt_group_len(struct alt_group *alt_group)
  34. {
  35. return alt_group->last_insn->offset +
  36. alt_group->last_insn->len -
  37. alt_group->first_insn->offset;
  38. }
  39. int orc_create(struct objtool_file *file)
  40. {
  41. struct section *sec, *orc_sec;
  42. unsigned int nr = 0, idx = 0;
  43. struct orc_list_entry *entry;
  44. struct list_head orc_list;
  45. struct orc_entry null = { .type = ORC_TYPE_UNDEFINED };
  46. /* Build a deduplicated list of ORC entries: */
  47. INIT_LIST_HEAD(&orc_list);
  48. for_each_sec(file, sec) {
  49. struct orc_entry orc, prev_orc = {0};
  50. struct instruction *insn;
  51. bool empty = true;
  52. if (!sec->text)
  53. continue;
  54. sec_for_each_insn(file, sec, insn) {
  55. struct alt_group *alt_group = insn->alt_group;
  56. int i;
  57. if (!alt_group) {
  58. if (init_orc_entry(&orc, insn->cfi, insn))
  59. return -1;
  60. if (!memcmp(&prev_orc, &orc, sizeof(orc)))
  61. continue;
  62. if (orc_list_add(&orc_list, &orc, sec,
  63. insn->offset))
  64. return -1;
  65. nr++;
  66. prev_orc = orc;
  67. empty = false;
  68. continue;
  69. }
  70. /*
  71. * Alternatives can have different stack layout
  72. * possibilities (but they shouldn't conflict).
  73. * Instead of traversing the instructions, use the
  74. * alt_group's flattened byte-offset-addressed CFI
  75. * array.
  76. */
  77. for (i = 0; i < alt_group_len(alt_group); i++) {
  78. struct cfi_state *cfi = alt_group->cfi[i];
  79. if (!cfi)
  80. continue;
  81. /* errors are reported on the original insn */
  82. if (init_orc_entry(&orc, cfi, insn))
  83. return -1;
  84. if (!memcmp(&prev_orc, &orc, sizeof(orc)))
  85. continue;
  86. if (orc_list_add(&orc_list, &orc, insn->sec,
  87. insn->offset + i))
  88. return -1;
  89. nr++;
  90. prev_orc = orc;
  91. empty = false;
  92. }
  93. /* Skip to the end of the alt_group */
  94. insn = alt_group->last_insn;
  95. }
  96. /* Add a section terminator */
  97. if (!empty) {
  98. orc_list_add(&orc_list, &null, sec, sec->sh.sh_size);
  99. nr++;
  100. }
  101. }
  102. if (!nr)
  103. return 0;
  104. /* Create .orc_unwind, .orc_unwind_ip and .rela.orc_unwind_ip sections: */
  105. sec = find_section_by_name(file->elf, ".orc_unwind");
  106. if (sec) {
  107. WARN("file already has .orc_unwind section, skipping");
  108. return -1;
  109. }
  110. orc_sec = elf_create_section(file->elf, ".orc_unwind",
  111. sizeof(struct orc_entry), nr);
  112. if (!orc_sec)
  113. return -1;
  114. sec = elf_create_section_pair(file->elf, ".orc_unwind_ip", sizeof(int), nr, nr);
  115. if (!sec)
  116. return -1;
  117. /* Write ORC entries to sections: */
  118. list_for_each_entry(entry, &orc_list, list) {
  119. if (write_orc_entry(file->elf, orc_sec, sec, idx++,
  120. entry->insn_sec, entry->insn_off,
  121. &entry->orc))
  122. return -1;
  123. }
  124. return 0;
  125. }