Commit | Line | Data |
---|---|---|
54cfded0 AM |
1 | /* dw2gencfi.c - Support for generating Dwarf2 CFI information. |
2 | Copyright 2003 Free Software Foundation, Inc. | |
3 | Contributed by Michal Ludvig <mludvig@suse.cz> | |
4 | ||
5 | This file is part of GAS, the GNU Assembler. | |
6 | ||
7 | GAS is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GAS is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GAS; see the file COPYING. If not, write to the Free | |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
21 | ||
54cfded0 AM |
22 | #include "as.h" |
23 | #include "dw2gencfi.h" | |
24 | ||
a4447b93 RH |
25 | |
26 | /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field | |
27 | of the CIE. Default to 1 if not otherwise specified. */ | |
28 | #ifndef DWARF2_LINE_MIN_INSN_LENGTH | |
29 | # define DWARF2_LINE_MIN_INSN_LENGTH 1 | |
30 | #endif | |
31 | ||
32 | /* If TARGET_USE_CFIPOP is defined, it is required that the target | |
33 | provide the following definitions. Otherwise provide them to | |
34 | allow compilation to continue. */ | |
35 | #ifndef TARGET_USE_CFIPOP | |
36 | # ifndef DWARF2_DEFAULT_RETURN_COLUMN | |
37 | # define DWARF2_DEFAULT_RETURN_COLUMN 0 | |
38 | # endif | |
39 | # ifndef DWARF2_CIE_DATA_ALIGNMENT | |
40 | # define DWARF2_CIE_DATA_ALIGNMENT 1 | |
41 | # endif | |
42 | #endif | |
43 | ||
9393cb0d JJ |
44 | #ifndef EH_FRAME_ALIGNMENT |
45 | # ifdef BFD_ASSEMBLER | |
46 | # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2) | |
47 | # else | |
48 | # define EH_FRAME_ALIGNMENT 2 | |
49 | # endif | |
50 | #endif | |
51 | ||
a4447b93 RH |
52 | #ifndef tc_cfi_frame_initial_instructions |
53 | # define tc_cfi_frame_initial_instructions() ((void)0) | |
54 | #endif | |
55 | ||
56 | ||
57 | struct cfi_insn_data | |
39b82151 | 58 | { |
a4447b93 RH |
59 | struct cfi_insn_data *next; |
60 | int insn; | |
61 | union { | |
62 | struct { | |
63 | unsigned reg; | |
64 | offsetT offset; | |
65 | } ri; | |
66 | ||
67 | struct { | |
68 | unsigned reg1; | |
69 | unsigned reg2; | |
70 | } rr; | |
71 | ||
72 | unsigned r; | |
73 | offsetT i; | |
74 | ||
75 | struct { | |
76 | symbolS *lab1; | |
77 | symbolS *lab2; | |
78 | } ll; | |
cdfbf930 RH |
79 | |
80 | struct cfi_escape_data { | |
81 | struct cfi_escape_data *next; | |
82 | expressionS exp; | |
83 | } *esc; | |
a4447b93 | 84 | } u; |
39b82151 ML |
85 | }; |
86 | ||
a4447b93 | 87 | struct fde_entry |
39b82151 | 88 | { |
a4447b93 RH |
89 | struct fde_entry *next; |
90 | symbolS *start_address; | |
91 | symbolS *end_address; | |
92 | struct cfi_insn_data *data; | |
93 | struct cfi_insn_data **last; | |
94 | unsigned int return_column; | |
39b82151 ML |
95 | }; |
96 | ||
a4447b93 | 97 | struct cie_entry |
39b82151 | 98 | { |
a4447b93 RH |
99 | struct cie_entry *next; |
100 | symbolS *start_address; | |
101 | unsigned int return_column; | |
102 | struct cfi_insn_data *first, *last; | |
39b82151 ML |
103 | }; |
104 | ||
a4447b93 RH |
105 | |
106 | /* Current open FDE entry. */ | |
107 | static struct fde_entry *cur_fde_data; | |
108 | static symbolS *last_address; | |
109 | static offsetT cur_cfa_offset; | |
110 | ||
111 | /* List of FDE entries. */ | |
112 | static struct fde_entry *all_fde_data; | |
113 | static struct fde_entry **last_fde_data = &all_fde_data; | |
39b82151 ML |
114 | |
115 | /* List of CIEs so that they could be reused. */ | |
116 | static struct cie_entry *cie_root; | |
117 | ||
fa87b337 RH |
118 | /* Stack of old CFI data, for save/restore. */ |
119 | struct cfa_save_data | |
120 | { | |
121 | struct cfa_save_data *next; | |
122 | offsetT cfa_offset; | |
123 | }; | |
124 | ||
125 | static struct cfa_save_data *cfa_save_stack; | |
a4447b93 RH |
126 | \f |
127 | /* Construct a new FDE structure and add it to the end of the fde list. */ | |
54cfded0 | 128 | |
a4447b93 RH |
129 | static struct fde_entry * |
130 | alloc_fde_entry (void) | |
131 | { | |
132 | struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry)); | |
54cfded0 | 133 | |
a4447b93 RH |
134 | cur_fde_data = fde; |
135 | *last_fde_data = fde; | |
136 | last_fde_data = &fde->next; | |
54cfded0 | 137 | |
a4447b93 RH |
138 | fde->last = &fde->data; |
139 | fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN; | |
140 | ||
141 | return fde; | |
142 | } | |
143 | ||
144 | /* The following functions are available for a backend to construct its | |
145 | own unwind information, usually from legacy unwind directives. */ | |
146 | ||
147 | /* Construct a new INSN structure and add it to the end of the insn list | |
148 | for the currently active FDE. */ | |
149 | ||
150 | static struct cfi_insn_data * | |
151 | alloc_cfi_insn_data (void) | |
54cfded0 | 152 | { |
a4447b93 RH |
153 | struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data)); |
154 | ||
155 | *cur_fde_data->last = insn; | |
156 | cur_fde_data->last = &insn->next; | |
54cfded0 | 157 | |
a4447b93 | 158 | return insn; |
54cfded0 AM |
159 | } |
160 | ||
a4447b93 RH |
161 | /* Construct a new FDE structure that begins at LABEL. */ |
162 | ||
163 | void | |
164 | cfi_new_fde (symbolS *label) | |
54cfded0 | 165 | { |
a4447b93 RH |
166 | struct fde_entry *fde = alloc_fde_entry (); |
167 | fde->start_address = label; | |
168 | last_address = label; | |
54cfded0 AM |
169 | } |
170 | ||
a4447b93 RH |
171 | /* End the currently open FDE. */ |
172 | ||
173 | void | |
174 | cfi_end_fde (symbolS *label) | |
54cfded0 | 175 | { |
a4447b93 RH |
176 | cur_fde_data->end_address = label; |
177 | cur_fde_data = NULL; | |
54cfded0 AM |
178 | } |
179 | ||
a4447b93 RH |
180 | /* Set the return column for the current FDE. */ |
181 | ||
182 | void | |
183 | cfi_set_return_column (unsigned regno) | |
54cfded0 | 184 | { |
a4447b93 RH |
185 | cur_fde_data->return_column = regno; |
186 | } | |
54cfded0 | 187 | |
2be24b54 ML |
188 | /* Universal functions to store new instructions. */ |
189 | ||
190 | static void | |
191 | cfi_add_CFA_insn(int insn) | |
192 | { | |
193 | struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data (); | |
194 | ||
195 | insn_ptr->insn = insn; | |
196 | } | |
197 | ||
198 | static void | |
199 | cfi_add_CFA_insn_reg (int insn, unsigned regno) | |
200 | { | |
201 | struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data (); | |
202 | ||
203 | insn_ptr->insn = insn; | |
204 | insn_ptr->u.r = regno; | |
205 | } | |
206 | ||
207 | static void | |
208 | cfi_add_CFA_insn_offset (int insn, offsetT offset) | |
209 | { | |
210 | struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data (); | |
211 | ||
212 | insn_ptr->insn = insn; | |
213 | insn_ptr->u.i = offset; | |
214 | } | |
215 | ||
216 | static void | |
217 | cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2) | |
218 | { | |
219 | struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data (); | |
220 | ||
221 | insn_ptr->insn = insn; | |
222 | insn_ptr->u.rr.reg1 = reg1; | |
223 | insn_ptr->u.rr.reg2 = reg2; | |
224 | } | |
225 | ||
226 | static void | |
227 | cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset) | |
228 | { | |
229 | struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data (); | |
230 | ||
231 | insn_ptr->insn = insn; | |
232 | insn_ptr->u.ri.reg = regno; | |
233 | insn_ptr->u.ri.offset = offset; | |
234 | } | |
235 | ||
a4447b93 | 236 | /* Add a CFI insn to advance the PC from the last address to LABEL. */ |
54cfded0 | 237 | |
a4447b93 RH |
238 | void |
239 | cfi_add_advance_loc (symbolS *label) | |
240 | { | |
241 | struct cfi_insn_data *insn = alloc_cfi_insn_data (); | |
7c0295b1 | 242 | |
a4447b93 RH |
243 | insn->insn = DW_CFA_advance_loc; |
244 | insn->u.ll.lab1 = last_address; | |
245 | insn->u.ll.lab2 = label; | |
54cfded0 | 246 | |
a4447b93 RH |
247 | last_address = label; |
248 | } | |
54cfded0 | 249 | |
a4447b93 RH |
250 | /* Add a DW_CFA_offset record to the CFI data. */ |
251 | ||
252 | void | |
253 | cfi_add_CFA_offset (unsigned regno, offsetT offset) | |
254 | { | |
fa87b337 RH |
255 | unsigned int abs_data_align; |
256 | ||
2be24b54 | 257 | cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset); |
fa87b337 RH |
258 | |
259 | abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0 | |
260 | ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT); | |
261 | if (offset % abs_data_align) | |
262 | as_bad (_("register save offset not a multiple of %u"), abs_data_align); | |
a4447b93 | 263 | } |
54cfded0 | 264 | |
a4447b93 | 265 | /* Add a DW_CFA_def_cfa record to the CFI data. */ |
54cfded0 | 266 | |
a4447b93 RH |
267 | void |
268 | cfi_add_CFA_def_cfa (unsigned regno, offsetT offset) | |
269 | { | |
2be24b54 | 270 | cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset); |
a4447b93 | 271 | cur_cfa_offset = offset; |
54cfded0 AM |
272 | } |
273 | ||
a4447b93 RH |
274 | /* Add a DW_CFA_register record to the CFI data. */ |
275 | ||
276 | void | |
277 | cfi_add_CFA_register (unsigned reg1, unsigned reg2) | |
54cfded0 | 278 | { |
2be24b54 | 279 | cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2); |
54cfded0 AM |
280 | } |
281 | ||
a4447b93 RH |
282 | /* Add a DW_CFA_def_cfa_register record to the CFI data. */ |
283 | ||
284 | void | |
285 | cfi_add_CFA_def_cfa_register (unsigned regno) | |
54cfded0 | 286 | { |
2be24b54 | 287 | cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno); |
54cfded0 AM |
288 | } |
289 | ||
a4447b93 RH |
290 | /* Add a DW_CFA_def_cfa_offset record to the CFI data. */ |
291 | ||
54cfded0 | 292 | void |
a4447b93 | 293 | cfi_add_CFA_def_cfa_offset (offsetT offset) |
54cfded0 | 294 | { |
2be24b54 | 295 | cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset); |
a4447b93 RH |
296 | cur_cfa_offset = offset; |
297 | } | |
54cfded0 | 298 | |
2be24b54 ML |
299 | void |
300 | cfi_add_CFA_restore (unsigned regno) | |
301 | { | |
302 | cfi_add_CFA_insn_reg (DW_CFA_restore, regno); | |
303 | } | |
304 | ||
305 | void | |
306 | cfi_add_CFA_undefined (unsigned regno) | |
307 | { | |
308 | cfi_add_CFA_insn_reg (DW_CFA_undefined, regno); | |
309 | } | |
310 | ||
311 | void | |
312 | cfi_add_CFA_same_value (unsigned regno) | |
313 | { | |
314 | cfi_add_CFA_insn_reg (DW_CFA_same_value, regno); | |
315 | } | |
316 | ||
317 | void | |
318 | cfi_add_CFA_remember_state (void) | |
319 | { | |
fa87b337 RH |
320 | struct cfa_save_data *p; |
321 | ||
2be24b54 | 322 | cfi_add_CFA_insn (DW_CFA_remember_state); |
fa87b337 RH |
323 | |
324 | p = xmalloc (sizeof (*p)); | |
325 | p->cfa_offset = cur_cfa_offset; | |
326 | p->next = cfa_save_stack; | |
327 | cfa_save_stack = p; | |
2be24b54 ML |
328 | } |
329 | ||
330 | void | |
331 | cfi_add_CFA_restore_state (void) | |
332 | { | |
fa87b337 RH |
333 | struct cfa_save_data *p; |
334 | ||
2be24b54 | 335 | cfi_add_CFA_insn (DW_CFA_restore_state); |
fa87b337 RH |
336 | |
337 | p = cfa_save_stack; | |
338 | if (p) | |
339 | { | |
340 | cur_cfa_offset = p->cfa_offset; | |
341 | cfa_save_stack = p->next; | |
342 | free (p); | |
343 | } | |
2be24b54 ML |
344 | } |
345 | ||
a4447b93 RH |
346 | \f |
347 | /* Parse CFI assembler directives. */ | |
54cfded0 | 348 | |
a4447b93 | 349 | static void dot_cfi (int); |
cdfbf930 | 350 | static void dot_cfi_escape (int); |
a4447b93 RH |
351 | static void dot_cfi_startproc (int); |
352 | static void dot_cfi_endproc (int); | |
54cfded0 | 353 | |
a4447b93 | 354 | /* Fake CFI type; outside the byte range of any real CFI insn. */ |
2be24b54 ML |
355 | #define CFI_adjust_cfa_offset 0x100 |
356 | #define CFI_return_column 0x101 | |
fa87b337 | 357 | #define CFI_rel_offset 0x102 |
cdfbf930 | 358 | #define CFI_escape 0x103 |
54cfded0 | 359 | |
a4447b93 RH |
360 | const pseudo_typeS cfi_pseudo_table[] = |
361 | { | |
362 | { "cfi_startproc", dot_cfi_startproc, 0 }, | |
363 | { "cfi_endproc", dot_cfi_endproc, 0 }, | |
364 | { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa }, | |
365 | { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register }, | |
366 | { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset }, | |
367 | { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset }, | |
368 | { "cfi_offset", dot_cfi, DW_CFA_offset }, | |
fa87b337 | 369 | { "cfi_rel_offset", dot_cfi, CFI_rel_offset }, |
a4447b93 | 370 | { "cfi_register", dot_cfi, DW_CFA_register }, |
2be24b54 ML |
371 | { "cfi_return_column", dot_cfi, CFI_return_column }, |
372 | { "cfi_restore", dot_cfi, DW_CFA_restore }, | |
373 | { "cfi_undefined", dot_cfi, DW_CFA_undefined }, | |
374 | { "cfi_same_value", dot_cfi, DW_CFA_same_value }, | |
375 | { "cfi_remember_state", dot_cfi, DW_CFA_remember_state }, | |
376 | { "cfi_restore_state", dot_cfi, DW_CFA_restore_state }, | |
6749011b | 377 | { "cfi_window_save", dot_cfi, DW_CFA_GNU_window_save }, |
cdfbf930 | 378 | { "cfi_escape", dot_cfi_escape, 0 }, |
a4447b93 RH |
379 | { NULL, NULL, 0 } |
380 | }; | |
54cfded0 AM |
381 | |
382 | static void | |
a4447b93 | 383 | cfi_parse_separator (void) |
54cfded0 | 384 | { |
a4447b93 RH |
385 | SKIP_WHITESPACE (); |
386 | if (*input_line_pointer == ',') | |
387 | input_line_pointer++; | |
388 | else | |
389 | as_bad (_("missing separator")); | |
54cfded0 AM |
390 | } |
391 | ||
a4447b93 RH |
392 | static unsigned |
393 | cfi_parse_reg (void) | |
54cfded0 | 394 | { |
a4447b93 RH |
395 | int regno; |
396 | expressionS exp; | |
397 | ||
398 | #ifdef tc_regname_to_dw2regnum | |
399 | SKIP_WHITESPACE (); | |
400 | if (is_name_beginner (*input_line_pointer) | |
401 | || (*input_line_pointer == '%' | |
402 | && is_name_beginner (*++input_line_pointer))) | |
403 | { | |
404 | char *name, c; | |
405 | ||
406 | name = input_line_pointer; | |
407 | c = get_symbol_end (); | |
408 | ||
409 | if ((regno = tc_regname_to_dw2regnum (name)) < 0) | |
410 | { | |
411 | as_bad (_("bad register expression")); | |
412 | regno = 0; | |
413 | } | |
54cfded0 | 414 | |
a4447b93 RH |
415 | *input_line_pointer = c; |
416 | return regno; | |
417 | } | |
418 | #endif | |
419 | ||
420 | expression (&exp); | |
421 | switch (exp.X_op) | |
54cfded0 | 422 | { |
a4447b93 RH |
423 | case O_register: |
424 | case O_constant: | |
425 | regno = exp.X_add_number; | |
426 | break; | |
427 | ||
428 | default: | |
429 | as_bad (_("bad register expression")); | |
430 | regno = 0; | |
431 | break; | |
54cfded0 AM |
432 | } |
433 | ||
a4447b93 RH |
434 | return regno; |
435 | } | |
436 | ||
437 | static offsetT | |
438 | cfi_parse_const (void) | |
439 | { | |
440 | return get_absolute_expression (); | |
54cfded0 AM |
441 | } |
442 | ||
443 | static void | |
a4447b93 | 444 | dot_cfi (int arg) |
54cfded0 | 445 | { |
a4447b93 RH |
446 | offsetT offset; |
447 | unsigned reg1, reg2; | |
54cfded0 | 448 | |
a4447b93 | 449 | if (!cur_fde_data) |
54cfded0 AM |
450 | { |
451 | as_bad (_("CFI instruction used without previous .cfi_startproc")); | |
452 | return; | |
453 | } | |
454 | ||
a4447b93 RH |
455 | /* If the last address was not at the current PC, advance to current. */ |
456 | if (symbol_get_frag (last_address) != frag_now | |
457 | || S_GET_VALUE (last_address) != frag_now_fix ()) | |
458 | cfi_add_advance_loc (symbol_temp_new_now ()); | |
54cfded0 AM |
459 | |
460 | switch (arg) | |
461 | { | |
a4447b93 | 462 | case DW_CFA_offset: |
a4447b93 RH |
463 | reg1 = cfi_parse_reg (); |
464 | cfi_parse_separator (); | |
465 | offset = cfi_parse_const (); | |
2be24b54 ML |
466 | cfi_add_CFA_offset (reg1, offset); |
467 | break; | |
a4447b93 | 468 | |
fa87b337 RH |
469 | case CFI_rel_offset: |
470 | reg1 = cfi_parse_reg (); | |
471 | cfi_parse_separator (); | |
472 | offset = cfi_parse_const (); | |
473 | cfi_add_CFA_offset (reg1, offset - cur_cfa_offset); | |
474 | break; | |
475 | ||
2be24b54 ML |
476 | case DW_CFA_def_cfa: |
477 | reg1 = cfi_parse_reg (); | |
478 | cfi_parse_separator (); | |
479 | offset = cfi_parse_const (); | |
480 | cfi_add_CFA_def_cfa (reg1, offset); | |
54cfded0 AM |
481 | break; |
482 | ||
a4447b93 RH |
483 | case DW_CFA_register: |
484 | reg1 = cfi_parse_reg (); | |
485 | cfi_parse_separator (); | |
486 | reg2 = cfi_parse_reg (); | |
a4447b93 | 487 | cfi_add_CFA_register (reg1, reg2); |
39b82151 ML |
488 | break; |
489 | ||
a4447b93 RH |
490 | case DW_CFA_def_cfa_register: |
491 | reg1 = cfi_parse_reg (); | |
492 | cfi_add_CFA_def_cfa_register (reg1); | |
54cfded0 AM |
493 | break; |
494 | ||
a4447b93 RH |
495 | case DW_CFA_def_cfa_offset: |
496 | offset = cfi_parse_const (); | |
497 | cfi_add_CFA_def_cfa_offset (offset); | |
54cfded0 AM |
498 | break; |
499 | ||
54cfded0 | 500 | case CFI_adjust_cfa_offset: |
a4447b93 RH |
501 | offset = cfi_parse_const (); |
502 | cfi_add_CFA_def_cfa_offset (cur_cfa_offset + offset); | |
54cfded0 AM |
503 | break; |
504 | ||
2be24b54 ML |
505 | case DW_CFA_restore: |
506 | reg1 = cfi_parse_reg (); | |
507 | cfi_add_CFA_restore (reg1); | |
508 | break; | |
509 | ||
510 | case DW_CFA_undefined: | |
511 | reg1 = cfi_parse_reg (); | |
512 | cfi_add_CFA_undefined (reg1); | |
513 | break; | |
514 | ||
515 | case DW_CFA_same_value: | |
516 | reg1 = cfi_parse_reg (); | |
517 | cfi_add_CFA_same_value (reg1); | |
518 | break; | |
519 | ||
520 | case CFI_return_column: | |
521 | reg1 = cfi_parse_reg (); | |
522 | cfi_set_return_column (reg1); | |
523 | break; | |
524 | ||
525 | case DW_CFA_remember_state: | |
526 | cfi_add_CFA_remember_state (); | |
527 | break; | |
528 | ||
529 | case DW_CFA_restore_state: | |
530 | cfi_add_CFA_restore_state (); | |
531 | break; | |
532 | ||
364b6d8b JJ |
533 | case DW_CFA_GNU_window_save: |
534 | cfi_add_CFA_insn (DW_CFA_GNU_window_save); | |
535 | break; | |
536 | ||
54cfded0 | 537 | default: |
a4447b93 | 538 | abort (); |
54cfded0 | 539 | } |
54cfded0 | 540 | |
a4447b93 | 541 | demand_empty_rest_of_line (); |
54cfded0 AM |
542 | } |
543 | ||
cdfbf930 RH |
544 | static void |
545 | dot_cfi_escape (int ignored ATTRIBUTE_UNUSED) | |
546 | { | |
547 | struct cfi_escape_data *head, **tail, *e; | |
548 | struct cfi_insn_data *insn; | |
549 | ||
550 | if (!cur_fde_data) | |
551 | { | |
552 | as_bad (_("CFI instruction used without previous .cfi_startproc")); | |
553 | return; | |
554 | } | |
555 | ||
556 | /* If the last address was not at the current PC, advance to current. */ | |
557 | if (symbol_get_frag (last_address) != frag_now | |
558 | || S_GET_VALUE (last_address) != frag_now_fix ()) | |
559 | cfi_add_advance_loc (symbol_temp_new_now ()); | |
560 | ||
561 | tail = &head; | |
562 | do | |
563 | { | |
564 | e = xmalloc (sizeof (*e)); | |
565 | do_parse_cons_expression (&e->exp, 1); | |
566 | *tail = e; | |
567 | tail = &e->next; | |
568 | } | |
569 | while (*input_line_pointer++ == ','); | |
570 | *tail = NULL; | |
571 | ||
572 | insn = alloc_cfi_insn_data (); | |
573 | insn->insn = CFI_escape; | |
574 | insn->u.esc = head; | |
575 | } | |
576 | ||
54cfded0 | 577 | static void |
a4447b93 | 578 | dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED) |
54cfded0 | 579 | { |
a4447b93 | 580 | int simple = 0; |
39b82151 | 581 | |
a4447b93 | 582 | if (cur_fde_data) |
54cfded0 AM |
583 | { |
584 | as_bad (_("previous CFI entry not closed (missing .cfi_endproc)")); | |
585 | return; | |
586 | } | |
587 | ||
a4447b93 | 588 | cfi_new_fde (symbol_temp_new_now ()); |
39b82151 | 589 | |
a4447b93 RH |
590 | SKIP_WHITESPACE (); |
591 | if (is_name_beginner (*input_line_pointer)) | |
592 | { | |
593 | char *name, c; | |
54cfded0 | 594 | |
a4447b93 RH |
595 | name = input_line_pointer; |
596 | c = get_symbol_end (); | |
54cfded0 | 597 | |
a4447b93 RH |
598 | if (strcmp (name, "simple") == 0) |
599 | { | |
600 | simple = 1; | |
601 | *input_line_pointer = c; | |
602 | } | |
603 | else | |
604 | input_line_pointer = name; | |
605 | } | |
606 | demand_empty_rest_of_line (); | |
607 | ||
608 | if (!simple) | |
39b82151 | 609 | tc_cfi_frame_initial_instructions (); |
54cfded0 AM |
610 | } |
611 | ||
a4447b93 RH |
612 | static void |
613 | dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED) | |
614 | { | |
615 | if (! cur_fde_data) | |
616 | { | |
617 | as_bad (_(".cfi_endproc without corresponding .cfi_startproc")); | |
618 | return; | |
619 | } | |
54cfded0 | 620 | |
a4447b93 RH |
621 | cfi_end_fde (symbol_temp_new_now ()); |
622 | } | |
39b82151 | 623 | |
a4447b93 RH |
624 | \f |
625 | /* Emit a single byte into the current segment. */ | |
54cfded0 | 626 | |
a4447b93 RH |
627 | static inline void |
628 | out_one (int byte) | |
54cfded0 | 629 | { |
a4447b93 RH |
630 | FRAG_APPEND_1_CHAR (byte); |
631 | } | |
54cfded0 | 632 | |
a4447b93 | 633 | /* Emit a two-byte word into the current segment. */ |
54cfded0 | 634 | |
a4447b93 RH |
635 | static inline void |
636 | out_two (int data) | |
637 | { | |
638 | md_number_to_chars (frag_more (2), data, 2); | |
639 | } | |
54cfded0 | 640 | |
a4447b93 | 641 | /* Emit a four byte word into the current segment. */ |
54cfded0 | 642 | |
a4447b93 RH |
643 | static inline void |
644 | out_four (int data) | |
645 | { | |
646 | md_number_to_chars (frag_more (4), data, 4); | |
647 | } | |
648 | ||
649 | /* Emit an unsigned "little-endian base 128" number. */ | |
54cfded0 | 650 | |
a4447b93 RH |
651 | static void |
652 | out_uleb128 (addressT value) | |
653 | { | |
654 | output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0); | |
54cfded0 AM |
655 | } |
656 | ||
a4447b93 RH |
657 | /* Emit an unsigned "little-endian base 128" number. */ |
658 | ||
659 | static void | |
660 | out_sleb128 (offsetT value) | |
54cfded0 | 661 | { |
a4447b93 RH |
662 | output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1); |
663 | } | |
54cfded0 | 664 | |
a4447b93 RH |
665 | static void |
666 | output_cfi_insn (struct cfi_insn_data *insn) | |
667 | { | |
668 | offsetT offset; | |
669 | unsigned int regno; | |
54cfded0 | 670 | |
a4447b93 | 671 | switch (insn->insn) |
54cfded0 | 672 | { |
a4447b93 RH |
673 | case DW_CFA_advance_loc: |
674 | { | |
675 | symbolS *from = insn->u.ll.lab1; | |
676 | symbolS *to = insn->u.ll.lab2; | |
677 | ||
678 | if (symbol_get_frag (to) == symbol_get_frag (from)) | |
679 | { | |
680 | addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from); | |
681 | addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH; | |
682 | ||
683 | if (scaled <= 0x3F) | |
684 | out_one (DW_CFA_advance_loc + scaled); | |
685 | else if (delta <= 0xFF) | |
686 | { | |
687 | out_one (DW_CFA_advance_loc1); | |
688 | out_one (delta); | |
689 | } | |
690 | else if (delta <= 0xFFFF) | |
691 | { | |
692 | out_one (DW_CFA_advance_loc2); | |
693 | out_two (delta); | |
694 | } | |
695 | else | |
696 | { | |
697 | out_one (DW_CFA_advance_loc4); | |
698 | out_four (delta); | |
699 | } | |
700 | } | |
701 | else | |
702 | { | |
703 | expressionS exp; | |
704 | ||
705 | exp.X_op = O_subtract; | |
706 | exp.X_add_symbol = to; | |
707 | exp.X_op_symbol = from; | |
708 | exp.X_add_number = 0; | |
709 | ||
710 | /* The code in ehopt.c expects that one byte of the encoding | |
711 | is already allocated to the frag. This comes from the way | |
712 | that it scans the .eh_frame section looking first for the | |
713 | .byte DW_CFA_advance_loc4. */ | |
714 | frag_more (1); | |
715 | ||
716 | frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3, | |
717 | make_expr_symbol (&exp), frag_now_fix () - 1, | |
718 | (char *) frag_now); | |
719 | } | |
720 | } | |
721 | break; | |
722 | ||
723 | case DW_CFA_def_cfa: | |
724 | offset = insn->u.ri.offset; | |
725 | if (offset < 0) | |
54cfded0 | 726 | { |
a4447b93 RH |
727 | out_one (DW_CFA_def_cfa_sf); |
728 | out_uleb128 (insn->u.ri.reg); | |
729 | out_uleb128 (offset); | |
54cfded0 AM |
730 | } |
731 | else | |
732 | { | |
a4447b93 RH |
733 | out_one (DW_CFA_def_cfa); |
734 | out_uleb128 (insn->u.ri.reg); | |
735 | out_uleb128 (offset); | |
54cfded0 AM |
736 | } |
737 | break; | |
738 | ||
a4447b93 | 739 | case DW_CFA_def_cfa_register: |
2be24b54 ML |
740 | case DW_CFA_undefined: |
741 | case DW_CFA_same_value: | |
742 | out_one (insn->insn); | |
743 | out_uleb128 (insn->u.r); | |
54cfded0 AM |
744 | break; |
745 | ||
a4447b93 RH |
746 | case DW_CFA_def_cfa_offset: |
747 | offset = insn->u.i; | |
748 | if (offset < 0) | |
749 | { | |
750 | out_one (DW_CFA_def_cfa_offset_sf); | |
751 | out_sleb128 (offset); | |
752 | } | |
753 | else | |
754 | { | |
755 | out_one (DW_CFA_def_cfa_offset); | |
756 | out_uleb128 (offset); | |
757 | } | |
54cfded0 AM |
758 | break; |
759 | ||
2be24b54 ML |
760 | case DW_CFA_restore: |
761 | regno = insn->u.r; | |
762 | if (regno <= 0x3F) | |
763 | { | |
764 | out_one (DW_CFA_restore + regno); | |
765 | } | |
766 | else | |
767 | { | |
768 | out_one (DW_CFA_restore_extended); | |
769 | out_uleb128 (regno); | |
770 | } | |
771 | break; | |
772 | ||
a4447b93 RH |
773 | case DW_CFA_offset: |
774 | regno = insn->u.ri.reg; | |
775 | offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT; | |
776 | if (offset < 0) | |
777 | { | |
1233ae62 | 778 | out_one (DW_CFA_offset_extended_sf); |
a4447b93 RH |
779 | out_uleb128 (regno); |
780 | out_sleb128 (offset); | |
781 | } | |
782 | else if (regno <= 0x3F) | |
783 | { | |
784 | out_one (DW_CFA_offset + regno); | |
785 | out_uleb128 (offset); | |
786 | } | |
54cfded0 AM |
787 | else |
788 | { | |
a4447b93 RH |
789 | out_one (DW_CFA_offset_extended); |
790 | out_uleb128 (regno); | |
791 | out_uleb128 (offset); | |
54cfded0 | 792 | } |
54cfded0 AM |
793 | break; |
794 | ||
a4447b93 RH |
795 | case DW_CFA_register: |
796 | out_one (DW_CFA_register); | |
797 | out_uleb128 (insn->u.rr.reg1); | |
798 | out_uleb128 (insn->u.rr.reg2); | |
39b82151 ML |
799 | break; |
800 | ||
2be24b54 ML |
801 | case DW_CFA_remember_state: |
802 | case DW_CFA_restore_state: | |
2be24b54 | 803 | out_one (insn->insn); |
54cfded0 AM |
804 | break; |
805 | ||
364b6d8b JJ |
806 | case DW_CFA_GNU_window_save: |
807 | out_one (DW_CFA_GNU_window_save); | |
808 | break; | |
809 | ||
cdfbf930 RH |
810 | case CFI_escape: |
811 | { | |
812 | struct cfi_escape_data *e; | |
813 | for (e = insn->u.esc; e ; e = e->next) | |
814 | emit_expr (&e->exp, 1); | |
815 | break; | |
816 | } | |
817 | ||
54cfded0 | 818 | default: |
a4447b93 | 819 | abort (); |
54cfded0 | 820 | } |
54cfded0 AM |
821 | } |
822 | ||
823 | static void | |
a4447b93 | 824 | output_cie (struct cie_entry *cie) |
54cfded0 | 825 | { |
a4447b93 | 826 | symbolS *after_size_address, *end_address; |
7c0295b1 | 827 | expressionS exp; |
a4447b93 RH |
828 | struct cfi_insn_data *i; |
829 | ||
830 | cie->start_address = symbol_temp_new_now (); | |
831 | after_size_address = symbol_temp_make (); | |
832 | end_address = symbol_temp_make (); | |
833 | ||
834 | exp.X_op = O_subtract; | |
835 | exp.X_add_symbol = end_address; | |
836 | exp.X_op_symbol = after_size_address; | |
837 | exp.X_add_number = 0; | |
838 | ||
839 | emit_expr (&exp, 4); /* Length */ | |
840 | symbol_set_value_now (after_size_address); | |
841 | out_four (0); /* CIE id */ | |
842 | out_one (DW_CIE_VERSION); /* Version */ | |
843 | out_one ('z'); /* Augmentation */ | |
844 | out_one ('R'); | |
845 | out_one (0); | |
846 | out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment */ | |
847 | out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment */ | |
0da76f83 NC |
848 | if (DW_CIE_VERSION == 1) /* Return column. */ |
849 | out_one (cie->return_column); | |
850 | else | |
851 | out_uleb128 (cie->return_column); | |
a4447b93 | 852 | out_uleb128 (1); /* Augmentation size */ |
364b6d8b | 853 | #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr |
a4447b93 | 854 | out_one (DW_EH_PE_pcrel | DW_EH_PE_sdata4); |
364b6d8b JJ |
855 | #else |
856 | out_one (DW_EH_PE_sdata4); | |
857 | #endif | |
a4447b93 RH |
858 | |
859 | if (cie->first) | |
860 | for (i = cie->first; i != cie->last; i = i->next) | |
861 | output_cfi_insn (i); | |
862 | ||
6ec51dba | 863 | frag_align (2, 0, 0); |
a4447b93 RH |
864 | symbol_set_value_now (end_address); |
865 | } | |
54cfded0 | 866 | |
a4447b93 RH |
867 | static void |
868 | output_fde (struct fde_entry *fde, struct cie_entry *cie, | |
9393cb0d | 869 | struct cfi_insn_data *first, int align) |
a4447b93 RH |
870 | { |
871 | symbolS *after_size_address, *end_address; | |
872 | expressionS exp; | |
54cfded0 | 873 | |
a4447b93 RH |
874 | after_size_address = symbol_temp_make (); |
875 | end_address = symbol_temp_make (); | |
54cfded0 | 876 | |
a4447b93 RH |
877 | exp.X_op = O_subtract; |
878 | exp.X_add_symbol = end_address; | |
879 | exp.X_op_symbol = after_size_address; | |
880 | exp.X_add_number = 0; | |
881 | emit_expr (&exp, 4); /* Length */ | |
882 | symbol_set_value_now (after_size_address); | |
54cfded0 | 883 | |
a4447b93 RH |
884 | exp.X_add_symbol = after_size_address; |
885 | exp.X_op_symbol = cie->start_address; | |
886 | emit_expr (&exp, 4); /* CIE offset */ | |
364b6d8b JJ |
887 | |
888 | #ifdef DIFF_EXPR_OK | |
a4447b93 RH |
889 | exp.X_add_symbol = fde->start_address; |
890 | exp.X_op_symbol = symbol_temp_new_now (); | |
ed7d5d1a | 891 | emit_expr (&exp, 4); /* Code offset */ |
364b6d8b JJ |
892 | #else |
893 | exp.X_op = O_symbol; | |
894 | exp.X_add_symbol = fde->start_address; | |
895 | exp.X_op_symbol = NULL; | |
896 | #ifdef tc_cfi_emit_pcrel_expr | |
897 | tc_cfi_emit_pcrel_expr (&exp, 4); /* Code offset */ | |
898 | #else | |
899 | emit_expr (&exp, 4); /* Code offset */ | |
900 | #endif | |
901 | exp.X_op = O_subtract; | |
902 | #endif | |
54cfded0 | 903 | |
a4447b93 RH |
904 | exp.X_add_symbol = fde->end_address; |
905 | exp.X_op_symbol = fde->start_address; /* Code length */ | |
906 | emit_expr (&exp, 4); | |
54cfded0 | 907 | |
a4447b93 | 908 | out_uleb128 (0); /* Augmentation size */ |
39b82151 | 909 | |
a4447b93 RH |
910 | for (; first; first = first->next) |
911 | output_cfi_insn (first); | |
39b82151 | 912 | |
6ec51dba | 913 | frag_align (align, 0, 0); |
a4447b93 RH |
914 | symbol_set_value_now (end_address); |
915 | } | |
916 | ||
917 | static struct cie_entry * | |
918 | select_cie_for_fde (struct fde_entry *fde, struct cfi_insn_data **pfirst) | |
919 | { | |
920 | struct cfi_insn_data *i, *j; | |
921 | struct cie_entry *cie; | |
922 | ||
923 | for (cie = cie_root; cie; cie = cie->next) | |
39b82151 | 924 | { |
a4447b93 RH |
925 | if (cie->return_column != fde->return_column) |
926 | continue; | |
927 | for (i = cie->first, j = fde->data; | |
928 | i != cie->last && j != NULL; | |
929 | i = i->next, j = j->next) | |
39b82151 | 930 | { |
a4447b93 RH |
931 | if (i->insn != j->insn) |
932 | goto fail; | |
933 | switch (i->insn) | |
934 | { | |
935 | case DW_CFA_advance_loc: | |
936 | /* We reached the first advance in the FDE, but did not | |
937 | reach the end of the CIE list. */ | |
938 | goto fail; | |
939 | ||
940 | case DW_CFA_offset: | |
941 | case DW_CFA_def_cfa: | |
942 | if (i->u.ri.reg != j->u.ri.reg) | |
943 | goto fail; | |
944 | if (i->u.ri.offset != j->u.ri.offset) | |
945 | goto fail; | |
946 | break; | |
947 | ||
948 | case DW_CFA_register: | |
949 | if (i->u.rr.reg1 != j->u.rr.reg1) | |
950 | goto fail; | |
951 | if (i->u.rr.reg2 != j->u.rr.reg2) | |
952 | goto fail; | |
953 | break; | |
954 | ||
955 | case DW_CFA_def_cfa_register: | |
2be24b54 ML |
956 | case DW_CFA_restore: |
957 | case DW_CFA_undefined: | |
958 | case DW_CFA_same_value: | |
a4447b93 RH |
959 | if (i->u.r != j->u.r) |
960 | goto fail; | |
961 | break; | |
962 | ||
963 | case DW_CFA_def_cfa_offset: | |
964 | if (i->u.i != j->u.i) | |
965 | goto fail; | |
966 | break; | |
967 | ||
cdfbf930 RH |
968 | case CFI_escape: |
969 | /* Don't bother matching these for now. */ | |
970 | goto fail; | |
971 | ||
a4447b93 RH |
972 | default: |
973 | abort (); | |
974 | } | |
39b82151 | 975 | } |
a4447b93 RH |
976 | |
977 | /* Success if we reached the end of the CIE list, and we've either | |
978 | run out of FDE entries or we've encountered an advance. */ | |
979 | if (i == cie->last && (!j || j->insn == DW_CFA_advance_loc)) | |
39b82151 | 980 | { |
a4447b93 RH |
981 | *pfirst = j; |
982 | return cie; | |
39b82151 ML |
983 | } |
984 | ||
a4447b93 | 985 | fail:; |
54cfded0 AM |
986 | } |
987 | ||
a4447b93 RH |
988 | cie = xmalloc (sizeof (struct cie_entry)); |
989 | cie->next = cie_root; | |
990 | cie_root = cie; | |
991 | cie->return_column = fde->return_column; | |
992 | cie->first = fde->data; | |
54cfded0 | 993 | |
a4447b93 RH |
994 | for (i = cie->first; i ; i = i->next) |
995 | if (i->insn == DW_CFA_advance_loc) | |
996 | break; | |
54cfded0 | 997 | |
a4447b93 RH |
998 | cie->last = i; |
999 | *pfirst = i; | |
1000 | ||
1001 | output_cie (cie); | |
54cfded0 | 1002 | |
a4447b93 | 1003 | return cie; |
54cfded0 AM |
1004 | } |
1005 | ||
1006 | void | |
a4447b93 | 1007 | cfi_finish (void) |
54cfded0 | 1008 | { |
a4447b93 RH |
1009 | segT cfi_seg; |
1010 | struct fde_entry *fde; | |
eafbc43f | 1011 | int save_flag_traditional_format; |
54cfded0 | 1012 | |
a4447b93 | 1013 | if (cur_fde_data) |
54cfded0 | 1014 | { |
a4447b93 RH |
1015 | as_bad (_("open CFI at the end of file; missing .cfi_endproc directive")); |
1016 | cur_fde_data->end_address = cur_fde_data->start_address; | |
54cfded0 | 1017 | } |
54cfded0 | 1018 | |
a4447b93 RH |
1019 | if (all_fde_data == 0) |
1020 | return; | |
54cfded0 | 1021 | |
a4447b93 RH |
1022 | /* Open .eh_frame section. */ |
1023 | cfi_seg = subseg_new (".eh_frame", 0); | |
1024 | #ifdef BFD_ASSEMBLER | |
1025 | bfd_set_section_flags (stdoutput, cfi_seg, | |
757bc393 | 1026 | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_READONLY); |
a4447b93 RH |
1027 | #endif |
1028 | subseg_set (cfi_seg, 0); | |
9393cb0d | 1029 | record_alignment (cfi_seg, EH_FRAME_ALIGNMENT); |
54cfded0 | 1030 | |
eafbc43f RH |
1031 | /* Make sure check_eh_frame doesn't do anything with our output. */ |
1032 | save_flag_traditional_format = flag_traditional_format; | |
1033 | flag_traditional_format = 1; | |
1034 | ||
a4447b93 RH |
1035 | for (fde = all_fde_data; fde ; fde = fde->next) |
1036 | { | |
1037 | struct cfi_insn_data *first; | |
1038 | struct cie_entry *cie; | |
1039 | ||
1040 | cie = select_cie_for_fde (fde, &first); | |
6ec51dba | 1041 | output_fde (fde, cie, first, fde->next == NULL ? EH_FRAME_ALIGNMENT : 2); |
a4447b93 | 1042 | } |
eafbc43f RH |
1043 | |
1044 | flag_traditional_format = save_flag_traditional_format; | |
54cfded0 | 1045 | } |