GNU Linux-libre 4.14.265-gnu1
[releases.git] / tools / objtool / orc_gen.c
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
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "orc.h"
22 #include "check.h"
23 #include "warn.h"
24
25 int create_orc(struct objtool_file *file)
26 {
27         struct instruction *insn;
28
29         for_each_insn(file, insn) {
30                 struct orc_entry *orc = &insn->orc;
31                 struct cfi_reg *cfa = &insn->state.cfa;
32                 struct cfi_reg *bp = &insn->state.regs[CFI_BP];
33
34                 if (cfa->base == CFI_UNDEFINED) {
35                         orc->sp_reg = ORC_REG_UNDEFINED;
36                         continue;
37                 }
38
39                 switch (cfa->base) {
40                 case CFI_SP:
41                         orc->sp_reg = ORC_REG_SP;
42                         break;
43                 case CFI_SP_INDIRECT:
44                         orc->sp_reg = ORC_REG_SP_INDIRECT;
45                         break;
46                 case CFI_BP:
47                         orc->sp_reg = ORC_REG_BP;
48                         break;
49                 case CFI_BP_INDIRECT:
50                         orc->sp_reg = ORC_REG_BP_INDIRECT;
51                         break;
52                 case CFI_R10:
53                         orc->sp_reg = ORC_REG_R10;
54                         break;
55                 case CFI_R13:
56                         orc->sp_reg = ORC_REG_R13;
57                         break;
58                 case CFI_DI:
59                         orc->sp_reg = ORC_REG_DI;
60                         break;
61                 case CFI_DX:
62                         orc->sp_reg = ORC_REG_DX;
63                         break;
64                 default:
65                         WARN_FUNC("unknown CFA base reg %d",
66                                   insn->sec, insn->offset, cfa->base);
67                         return -1;
68                 }
69
70                 switch(bp->base) {
71                 case CFI_UNDEFINED:
72                         orc->bp_reg = ORC_REG_UNDEFINED;
73                         break;
74                 case CFI_CFA:
75                         orc->bp_reg = ORC_REG_PREV_SP;
76                         break;
77                 case CFI_BP:
78                         orc->bp_reg = ORC_REG_BP;
79                         break;
80                 default:
81                         WARN_FUNC("unknown BP base reg %d",
82                                   insn->sec, insn->offset, bp->base);
83                         return -1;
84                 }
85
86                 orc->sp_offset = cfa->offset;
87                 orc->bp_offset = bp->offset;
88                 orc->type = insn->state.type;
89         }
90
91         return 0;
92 }
93
94 static int create_orc_entry(struct section *u_sec, struct section *ip_relasec,
95                                 unsigned int idx, struct section *insn_sec,
96                                 unsigned long insn_off, struct orc_entry *o)
97 {
98         struct orc_entry *orc;
99         struct rela *rela;
100
101         /* populate ORC data */
102         orc = (struct orc_entry *)u_sec->data->d_buf + idx;
103         memcpy(orc, o, sizeof(*orc));
104
105         /* populate rela for ip */
106         rela = malloc(sizeof(*rela));
107         if (!rela) {
108                 perror("malloc");
109                 return -1;
110         }
111         memset(rela, 0, sizeof(*rela));
112
113         if (insn_sec->sym) {
114                 rela->sym = insn_sec->sym;
115                 rela->addend = insn_off;
116         } else {
117                 /*
118                  * The Clang assembler doesn't produce section symbols, so we
119                  * have to reference the function symbol instead:
120                  */
121                 rela->sym = find_symbol_containing(insn_sec, insn_off);
122                 if (!rela->sym) {
123                         /*
124                          * Hack alert.  This happens when we need to reference
125                          * the NOP pad insn immediately after the function.
126                          */
127                         rela->sym = find_symbol_containing(insn_sec,
128                                                            insn_off - 1);
129                 }
130                 if (!rela->sym) {
131                         WARN("missing symbol for insn at offset 0x%lx\n",
132                              insn_off);
133                         return -1;
134                 }
135
136                 rela->addend = insn_off - rela->sym->offset;
137         }
138
139         rela->type = R_X86_64_PC32;
140         rela->offset = idx * sizeof(int);
141
142         list_add_tail(&rela->list, &ip_relasec->rela_list);
143         hash_add(ip_relasec->rela_hash, &rela->hash, rela->offset);
144
145         return 0;
146 }
147
148 int create_orc_sections(struct objtool_file *file)
149 {
150         struct instruction *insn, *prev_insn;
151         struct section *sec, *u_sec, *ip_relasec;
152         unsigned int idx;
153
154         struct orc_entry empty = {
155                 .sp_reg = ORC_REG_UNDEFINED,
156                 .bp_reg  = ORC_REG_UNDEFINED,
157                 .type    = ORC_TYPE_CALL,
158         };
159
160         sec = find_section_by_name(file->elf, ".orc_unwind");
161         if (sec) {
162                 WARN("file already has .orc_unwind section, skipping");
163                 return -1;
164         }
165
166         /* count the number of needed orcs */
167         idx = 0;
168         for_each_sec(file, sec) {
169                 if (!sec->text)
170                         continue;
171
172                 prev_insn = NULL;
173                 sec_for_each_insn(file, sec, insn) {
174                         if (!prev_insn ||
175                             memcmp(&insn->orc, &prev_insn->orc,
176                                    sizeof(struct orc_entry))) {
177                                 idx++;
178                         }
179                         prev_insn = insn;
180                 }
181
182                 /* section terminator */
183                 if (prev_insn)
184                         idx++;
185         }
186         if (!idx)
187                 return -1;
188
189
190         /* create .orc_unwind_ip and .rela.orc_unwind_ip sections */
191         sec = elf_create_section(file->elf, ".orc_unwind_ip", sizeof(int), idx);
192         if (!sec)
193                 return -1;
194
195         ip_relasec = elf_create_rela_section(file->elf, sec);
196         if (!ip_relasec)
197                 return -1;
198
199         /* create .orc_unwind section */
200         u_sec = elf_create_section(file->elf, ".orc_unwind",
201                                    sizeof(struct orc_entry), idx);
202
203         /* populate sections */
204         idx = 0;
205         for_each_sec(file, sec) {
206                 if (!sec->text)
207                         continue;
208
209                 prev_insn = NULL;
210                 sec_for_each_insn(file, sec, insn) {
211                         if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
212                                                  sizeof(struct orc_entry))) {
213
214                                 if (create_orc_entry(u_sec, ip_relasec, idx,
215                                                      insn->sec, insn->offset,
216                                                      &insn->orc))
217                                         return -1;
218
219                                 idx++;
220                         }
221                         prev_insn = insn;
222                 }
223
224                 /* section terminator */
225                 if (prev_insn) {
226                         if (create_orc_entry(u_sec, ip_relasec, idx,
227                                              prev_insn->sec,
228                                              prev_insn->offset + prev_insn->len,
229                                              &empty))
230                                 return -1;
231
232                         idx++;
233                 }
234         }
235
236         if (elf_rebuild_rela_section(ip_relasec))
237                 return -1;
238
239         return 0;
240 }