orc_gen.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. 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. /* populate ORC data */
  91. orc = (struct orc_entry *)u_sec->data->d_buf + idx;
  92. memcpy(orc, o, sizeof(*orc));
  93. /* populate rela for ip */
  94. rela = malloc(sizeof(*rela));
  95. if (!rela) {
  96. perror("malloc");
  97. return -1;
  98. }
  99. memset(rela, 0, sizeof(*rela));
  100. if (insn_sec->sym) {
  101. rela->sym = insn_sec->sym;
  102. rela->addend = insn_off;
  103. } else {
  104. /*
  105. * The Clang assembler doesn't produce section symbols, so we
  106. * have to reference the function symbol instead:
  107. */
  108. rela->sym = find_symbol_containing(insn_sec, insn_off);
  109. if (!rela->sym) {
  110. /*
  111. * Hack alert. This happens when we need to reference
  112. * the NOP pad insn immediately after the function.
  113. */
  114. rela->sym = find_symbol_containing(insn_sec,
  115. insn_off - 1);
  116. }
  117. if (!rela->sym) {
  118. WARN("missing symbol for insn at offset 0x%lx\n",
  119. insn_off);
  120. return -1;
  121. }
  122. rela->addend = insn_off - rela->sym->offset;
  123. }
  124. rela->type = R_X86_64_PC32;
  125. rela->offset = idx * sizeof(int);
  126. list_add_tail(&rela->list, &ip_relasec->rela_list);
  127. hash_add(ip_relasec->rela_hash, &rela->hash, rela->offset);
  128. return 0;
  129. }
  130. int create_orc_sections(struct objtool_file *file)
  131. {
  132. struct instruction *insn, *prev_insn;
  133. struct section *sec, *u_sec, *ip_relasec;
  134. unsigned int idx;
  135. struct orc_entry empty = {
  136. .sp_reg = ORC_REG_UNDEFINED,
  137. .bp_reg = ORC_REG_UNDEFINED,
  138. .type = ORC_TYPE_CALL,
  139. };
  140. sec = find_section_by_name(file->elf, ".orc_unwind");
  141. if (sec) {
  142. WARN("file already has .orc_unwind section, skipping");
  143. return -1;
  144. }
  145. /* count the number of needed orcs */
  146. idx = 0;
  147. for_each_sec(file, sec) {
  148. if (!sec->text)
  149. continue;
  150. prev_insn = NULL;
  151. sec_for_each_insn(file, sec, insn) {
  152. if (!prev_insn ||
  153. memcmp(&insn->orc, &prev_insn->orc,
  154. sizeof(struct orc_entry))) {
  155. idx++;
  156. }
  157. prev_insn = insn;
  158. }
  159. /* section terminator */
  160. if (prev_insn)
  161. idx++;
  162. }
  163. if (!idx)
  164. return -1;
  165. /* create .orc_unwind_ip and .rela.orc_unwind_ip sections */
  166. sec = elf_create_section(file->elf, ".orc_unwind_ip", sizeof(int), idx);
  167. if (!sec)
  168. return -1;
  169. ip_relasec = elf_create_rela_section(file->elf, sec);
  170. if (!ip_relasec)
  171. return -1;
  172. /* create .orc_unwind section */
  173. u_sec = elf_create_section(file->elf, ".orc_unwind",
  174. sizeof(struct orc_entry), idx);
  175. /* populate sections */
  176. idx = 0;
  177. for_each_sec(file, sec) {
  178. if (!sec->text)
  179. continue;
  180. prev_insn = NULL;
  181. sec_for_each_insn(file, sec, insn) {
  182. if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
  183. sizeof(struct orc_entry))) {
  184. if (create_orc_entry(u_sec, ip_relasec, idx,
  185. insn->sec, insn->offset,
  186. &insn->orc))
  187. return -1;
  188. idx++;
  189. }
  190. prev_insn = insn;
  191. }
  192. /* section terminator */
  193. if (prev_insn) {
  194. if (create_orc_entry(u_sec, ip_relasec, idx,
  195. prev_insn->sec,
  196. prev_insn->offset + prev_insn->len,
  197. &empty))
  198. return -1;
  199. idx++;
  200. }
  201. }
  202. if (elf_rebuild_rela_section(ip_relasec))
  203. return -1;
  204. return 0;
  205. }