orc_gen.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2017 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. #include <stdlib.h>
  18. #include <string.h>
  19. #include "orc.h"
  20. #include "check.h"
  21. #include "warn.h"
  22. int create_orc(struct objtool_file *file)
  23. {
  24. struct instruction *insn;
  25. for_each_insn(file, insn) {
  26. struct orc_entry *orc = &insn->orc;
  27. struct cfi_reg *cfa = &insn->state.cfa;
  28. struct cfi_reg *bp = &insn->state.regs[CFI_BP];
  29. orc->end = insn->state.end;
  30. if (cfa->base == CFI_UNDEFINED) {
  31. orc->sp_reg = ORC_REG_UNDEFINED;
  32. continue;
  33. }
  34. switch (cfa->base) {
  35. case CFI_SP:
  36. orc->sp_reg = ORC_REG_SP;
  37. break;
  38. case CFI_SP_INDIRECT:
  39. orc->sp_reg = ORC_REG_SP_INDIRECT;
  40. break;
  41. case CFI_BP:
  42. orc->sp_reg = ORC_REG_BP;
  43. break;
  44. case CFI_BP_INDIRECT:
  45. orc->sp_reg = ORC_REG_BP_INDIRECT;
  46. break;
  47. case CFI_R10:
  48. orc->sp_reg = ORC_REG_R10;
  49. break;
  50. case CFI_R13:
  51. orc->sp_reg = ORC_REG_R13;
  52. break;
  53. case CFI_DI:
  54. orc->sp_reg = ORC_REG_DI;
  55. break;
  56. case CFI_DX:
  57. orc->sp_reg = ORC_REG_DX;
  58. break;
  59. default:
  60. WARN_FUNC("unknown CFA base reg %d",
  61. insn->sec, insn->offset, cfa->base);
  62. return -1;
  63. }
  64. switch(bp->base) {
  65. case CFI_UNDEFINED:
  66. orc->bp_reg = ORC_REG_UNDEFINED;
  67. break;
  68. case CFI_CFA:
  69. orc->bp_reg = ORC_REG_PREV_SP;
  70. break;
  71. case CFI_BP:
  72. orc->bp_reg = ORC_REG_BP;
  73. break;
  74. default:
  75. WARN_FUNC("unknown BP base reg %d",
  76. insn->sec, insn->offset, bp->base);
  77. return -1;
  78. }
  79. orc->sp_offset = cfa->offset;
  80. orc->bp_offset = bp->offset;
  81. orc->type = insn->state.type;
  82. }
  83. return 0;
  84. }
  85. static int create_orc_entry(struct section *u_sec, struct section *ip_relasec,
  86. unsigned int idx, struct section *insn_sec,
  87. unsigned long insn_off, struct orc_entry *o)
  88. {
  89. struct orc_entry *orc;
  90. struct rela *rela;
  91. if (!insn_sec->sym) {
  92. WARN("missing symbol for section %s", insn_sec->name);
  93. return -1;
  94. }
  95. /* populate ORC data */
  96. orc = (struct orc_entry *)u_sec->data->d_buf + idx;
  97. memcpy(orc, o, sizeof(*orc));
  98. /* populate rela for ip */
  99. rela = malloc(sizeof(*rela));
  100. if (!rela) {
  101. perror("malloc");
  102. return -1;
  103. }
  104. memset(rela, 0, sizeof(*rela));
  105. rela->sym = insn_sec->sym;
  106. rela->addend = insn_off;
  107. rela->type = R_X86_64_PC32;
  108. rela->offset = idx * sizeof(int);
  109. list_add_tail(&rela->list, &ip_relasec->rela_list);
  110. hash_add(ip_relasec->rela_hash, &rela->hash, rela->offset);
  111. return 0;
  112. }
  113. int create_orc_sections(struct objtool_file *file)
  114. {
  115. struct instruction *insn, *prev_insn;
  116. struct section *sec, *u_sec, *ip_relasec;
  117. unsigned int idx;
  118. struct orc_entry empty = {
  119. .sp_reg = ORC_REG_UNDEFINED,
  120. .bp_reg = ORC_REG_UNDEFINED,
  121. .type = ORC_TYPE_CALL,
  122. };
  123. sec = find_section_by_name(file->elf, ".orc_unwind");
  124. if (sec) {
  125. WARN("file already has .orc_unwind section, skipping");
  126. return -1;
  127. }
  128. /* count the number of needed orcs */
  129. idx = 0;
  130. for_each_sec(file, sec) {
  131. if (!sec->text)
  132. continue;
  133. prev_insn = NULL;
  134. sec_for_each_insn(file, sec, insn) {
  135. if (!prev_insn ||
  136. memcmp(&insn->orc, &prev_insn->orc,
  137. sizeof(struct orc_entry))) {
  138. idx++;
  139. }
  140. prev_insn = insn;
  141. }
  142. /* section terminator */
  143. if (prev_insn)
  144. idx++;
  145. }
  146. if (!idx)
  147. return -1;
  148. /* create .orc_unwind_ip and .rela.orc_unwind_ip sections */
  149. sec = elf_create_section(file->elf, ".orc_unwind_ip", sizeof(int), idx);
  150. if (!sec)
  151. return -1;
  152. ip_relasec = elf_create_rela_section(file->elf, sec);
  153. if (!ip_relasec)
  154. return -1;
  155. /* create .orc_unwind section */
  156. u_sec = elf_create_section(file->elf, ".orc_unwind",
  157. sizeof(struct orc_entry), idx);
  158. /* populate sections */
  159. idx = 0;
  160. for_each_sec(file, sec) {
  161. if (!sec->text)
  162. continue;
  163. prev_insn = NULL;
  164. sec_for_each_insn(file, sec, insn) {
  165. if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
  166. sizeof(struct orc_entry))) {
  167. if (create_orc_entry(u_sec, ip_relasec, idx,
  168. insn->sec, insn->offset,
  169. &insn->orc))
  170. return -1;
  171. idx++;
  172. }
  173. prev_insn = insn;
  174. }
  175. /* section terminator */
  176. if (prev_insn) {
  177. if (create_orc_entry(u_sec, ip_relasec, idx,
  178. prev_insn->sec,
  179. prev_insn->offset + prev_insn->len,
  180. &empty))
  181. return -1;
  182. idx++;
  183. }
  184. }
  185. if (elf_rebuild_rela_section(ip_relasec))
  186. return -1;
  187. return 0;
  188. }