orc_gen.c 5.3 KB

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