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