orc_gen.c 4.8 KB

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