orc_gen.c 3.4 KB

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