/src/binutils-gdb/gas/dwarf2dbg.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* dwarf2dbg.c - DWARF2 debug support |
2 | | Copyright (C) 1999-2025 Free Software Foundation, Inc. |
3 | | Contributed by David Mosberger-Tang <davidm@hpl.hp.com> |
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 3, 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, 51 Franklin Street - Fifth Floor, Boston, MA |
20 | | 02110-1301, USA. */ |
21 | | |
22 | | /* Logical line numbers can be controlled by the compiler via the |
23 | | following directives: |
24 | | |
25 | | .file FILENO "file.c" |
26 | | .loc FILENO LINENO [COLUMN] [basic_block] [prologue_end] \ |
27 | | [epilogue_begin] [is_stmt VALUE] [isa VALUE] \ |
28 | | [discriminator VALUE] [view VALUE] |
29 | | */ |
30 | | |
31 | | #include "as.h" |
32 | | #include "safe-ctype.h" |
33 | | #include <limits.h> |
34 | | #include "dwarf2dbg.h" |
35 | | #include <filenames.h> |
36 | | |
37 | | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
38 | | /* We need to decide which character to use as a directory separator. |
39 | | Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not |
40 | | necessarily mean that the backslash character is the one to use. |
41 | | Some environments, eg Cygwin, can support both naming conventions. |
42 | | So we use the heuristic that we only need to use the backslash if |
43 | | the path is an absolute path starting with a DOS style drive |
44 | | selector. eg C: or D: */ |
45 | | # define INSERT_DIR_SEPARATOR(string, offset) \ |
46 | | do \ |
47 | | { \ |
48 | | if (offset > 1 \ |
49 | | && string[0] != 0 \ |
50 | | && string[1] == ':') \ |
51 | | string [offset] = '\\'; \ |
52 | | else \ |
53 | | string [offset] = '/'; \ |
54 | | } \ |
55 | | while (0) |
56 | | #else |
57 | 1 | # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/' |
58 | | #endif |
59 | | |
60 | | #ifndef DWARF2_FORMAT |
61 | 45 | # define DWARF2_FORMAT(SEC) dwarf2_format_32bit |
62 | | #endif |
63 | | |
64 | | #ifndef DWARF2_ADDR_SIZE |
65 | | # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8) |
66 | | #endif |
67 | | |
68 | | #ifndef DWARF2_FILE_NAME |
69 | 35 | #define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME |
70 | | #endif |
71 | | |
72 | | #ifndef DWARF2_FILE_TIME_NAME |
73 | 12 | #define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) -1 |
74 | | #endif |
75 | | |
76 | | #ifndef DWARF2_FILE_SIZE_NAME |
77 | 12 | #define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) -1 |
78 | | #endif |
79 | | |
80 | | #ifndef DWARF2_VERSION |
81 | 22.1k | #define DWARF2_VERSION dwarf_level |
82 | | #endif |
83 | | |
84 | | /* The .debug_aranges version has been 2 in DWARF version 2, 3 and 4. */ |
85 | | #ifndef DWARF2_ARANGES_VERSION |
86 | 11 | #define DWARF2_ARANGES_VERSION 2 |
87 | | #endif |
88 | | |
89 | | /* The .debug_line version is the same as the .debug_info version. */ |
90 | | #ifndef DWARF2_LINE_VERSION |
91 | 22.1k | #define DWARF2_LINE_VERSION DWARF2_VERSION |
92 | | #endif |
93 | | |
94 | | /* The .debug_rnglists has only been in DWARF version 5. */ |
95 | | #ifndef DWARF2_RNGLISTS_VERSION |
96 | 0 | #define DWARF2_RNGLISTS_VERSION 5 |
97 | | #endif |
98 | | |
99 | | #include "subsegs.h" |
100 | | |
101 | | #include "dwarf2.h" |
102 | | |
103 | | /* Since we can't generate the prolog until the body is complete, we |
104 | | use three different subsegments for .debug_line: one holding the |
105 | | prolog, one for the directory and filename info, and one for the |
106 | | body ("statement program"). */ |
107 | | #define DL_PROLOG 0 |
108 | | #define DL_FILES 1 |
109 | | #define DL_BODY 2 |
110 | | |
111 | | /* If linker relaxation might change offsets in the code, the DWARF special |
112 | | opcodes and variable-length operands cannot be used. If this macro is |
113 | | nonzero, use the DW_LNS_fixed_advance_pc opcode instead. */ |
114 | | #ifndef DWARF2_USE_FIXED_ADVANCE_PC |
115 | 682 | # define DWARF2_USE_FIXED_ADVANCE_PC linkrelax |
116 | | #endif |
117 | | |
118 | | /* First special line opcode - leave room for the standard opcodes. |
119 | | Note: If you want to change this, you'll have to update the |
120 | | "standard_opcode_lengths" table that is emitted below in |
121 | | out_debug_line(). */ |
122 | 1.62k | #define DWARF2_LINE_OPCODE_BASE (DWARF2_LINE_VERSION == 2 ? 10 : 13) |
123 | | |
124 | | #ifndef DWARF2_LINE_BASE |
125 | | /* Minimum line offset in a special line info. opcode. This value |
126 | | was chosen to give a reasonable range of values. */ |
127 | 1.93k | # define DWARF2_LINE_BASE -5 |
128 | | #endif |
129 | | |
130 | | /* Range of line offsets in a special line info. opcode. */ |
131 | | #ifndef DWARF2_LINE_RANGE |
132 | 2.36k | # define DWARF2_LINE_RANGE 14 |
133 | | #endif |
134 | | |
135 | | #ifndef DWARF2_LINE_MIN_INSN_LENGTH |
136 | | /* Define the architecture-dependent minimum instruction length (in |
137 | | bytes). This value should be rather too small than too big. */ |
138 | 1.40k | # define DWARF2_LINE_MIN_INSN_LENGTH 1 |
139 | | #endif |
140 | | |
141 | | /* Flag that indicates the initial value of the is_stmt_start flag. */ |
142 | 52 | #define DWARF2_LINE_DEFAULT_IS_STMT 1 |
143 | | |
144 | | #ifndef DWARF2_LINE_MAX_OPS_PER_INSN |
145 | 12 | #define DWARF2_LINE_MAX_OPS_PER_INSN 1 |
146 | | #endif |
147 | | |
148 | | /* Given a special op, return the line skip amount. */ |
149 | | #define SPECIAL_LINE(op) \ |
150 | | (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE) |
151 | | |
152 | | /* Given a special op, return the address skip amount (in units of |
153 | | DWARF2_LINE_MIN_INSN_LENGTH. */ |
154 | 825 | #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) |
155 | | |
156 | | /* The maximum address skip amount that can be encoded with a special op. */ |
157 | 825 | #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) |
158 | | |
159 | | #ifndef TC_PARSE_CONS_RETURN_NONE |
160 | | #define TC_PARSE_CONS_RETURN_NONE BFD_RELOC_NONE |
161 | | #endif |
162 | | |
163 | 22 | #define GAS_ABBREV_COMP_UNIT 1 |
164 | 0 | #define GAS_ABBREV_SUBPROG 2 |
165 | 0 | #define GAS_ABBREV_NO_TYPE 3 |
166 | | |
167 | | struct line_entry |
168 | | { |
169 | | struct line_entry *next; |
170 | | symbolS *label; |
171 | | struct dwarf2_line_info loc; |
172 | | }; |
173 | | |
174 | | /* Given line_entry list HEAD and PTAIL pointers, return a pointer to |
175 | | the last line_entry on the list. */ |
176 | | static inline struct line_entry * |
177 | | line_entry_at_tail (void *head, struct line_entry **ptail) |
178 | 0 | { |
179 | | /* If the list is empty ptail points at head. */ |
180 | 0 | if (head == NULL) |
181 | 0 | return NULL; |
182 | | /* Otherwise ptail points to line_entry.next of the last entry. */ |
183 | 0 | void *p = (char *) ptail - offsetof (struct line_entry, next); |
184 | 0 | return p; |
185 | 0 | } |
186 | | |
187 | | struct line_subseg |
188 | | { |
189 | | struct line_subseg *next; |
190 | | subsegT subseg; |
191 | | struct line_entry *head; |
192 | | struct line_entry **ptail; |
193 | | struct line_entry **pmove_tail; |
194 | | }; |
195 | | |
196 | | struct line_seg |
197 | | { |
198 | | struct line_seg *next; |
199 | | segT seg; |
200 | | struct line_subseg *head; |
201 | | symbolS *text_start; |
202 | | symbolS *text_end; |
203 | | }; |
204 | | |
205 | | /* Collects data for all line table entries during assembly. */ |
206 | | static struct line_seg *all_segs; |
207 | | static struct line_seg **last_seg_ptr; |
208 | | |
209 | 70 | #define NUM_MD5_BYTES 16 |
210 | | |
211 | | struct file_entry |
212 | | { |
213 | | const char * filename; |
214 | | unsigned int dir; |
215 | | unsigned char md5[NUM_MD5_BYTES]; |
216 | | }; |
217 | | |
218 | | /* Table of files used by .debug_line. */ |
219 | | static struct file_entry *files; |
220 | | static unsigned int files_in_use; |
221 | | static unsigned int files_allocated; |
222 | | |
223 | | /* Table of directories used by .debug_line. */ |
224 | | static char ** dirs; |
225 | | static unsigned int dirs_in_use; |
226 | | static unsigned int dirs_allocated; |
227 | | |
228 | | /* TRUE when we've seen a .loc directive recently. Used to avoid |
229 | | doing work when there's nothing to do. Will be reset by |
230 | | dwarf2_consume_line_info. */ |
231 | | bool dwarf2_loc_directive_seen; |
232 | | |
233 | | /* TRUE when we've seen any .loc directive at any time during parsing. |
234 | | Indicates the user wants us to generate a .debug_line section. |
235 | | Used in dwarf2_finish as sanity check. */ |
236 | | static bool dwarf2_any_loc_directive_seen; |
237 | | |
238 | | /* TRUE when we're supposed to set the basic block mark whenever a |
239 | | label is seen. */ |
240 | | bool dwarf2_loc_mark_labels; |
241 | | |
242 | | /* Current location as indicated by the most recent .loc directive. */ |
243 | | static struct dwarf2_line_info current; |
244 | | |
245 | | /* This symbol is used to recognize view number forced resets in loc |
246 | | lists. */ |
247 | | static symbolS *force_reset_view; |
248 | | |
249 | | /* This symbol evaluates to an expression that, if nonzero, indicates |
250 | | some view assert check failed. */ |
251 | | static symbolS *view_assert_failed; |
252 | | |
253 | | /* The size of an address on the target. */ |
254 | | static unsigned int sizeof_address; |
255 | | |
256 | | #ifndef TC_DWARF2_EMIT_OFFSET |
257 | 143 | #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset |
258 | | |
259 | | /* Create an offset to .dwarf2_*. */ |
260 | | |
261 | | static void |
262 | | generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size) |
263 | 143 | { |
264 | 143 | expressionS exp; |
265 | | |
266 | 143 | memset (&exp, 0, sizeof exp); |
267 | 143 | exp.X_op = O_symbol; |
268 | 143 | exp.X_add_symbol = symbol; |
269 | 143 | exp.X_add_number = 0; |
270 | 143 | emit_expr (&exp, size); |
271 | 143 | } |
272 | | #endif |
273 | | |
274 | | /* Find or create (if CREATE_P) an entry for SEG+SUBSEG in ALL_SEGS. */ |
275 | | |
276 | | static struct line_subseg * |
277 | | get_line_subseg (segT seg, subsegT subseg, bool create_p) |
278 | 682 | { |
279 | 682 | struct line_seg *s = seg_info (seg)->dwarf2_line_seg; |
280 | 682 | struct line_subseg **pss, *lss; |
281 | | |
282 | 682 | if (s == NULL) |
283 | 12 | { |
284 | 12 | if (!create_p) |
285 | 0 | return NULL; |
286 | | |
287 | 12 | s = XNEW (struct line_seg); |
288 | 12 | s->next = NULL; |
289 | 12 | s->seg = seg; |
290 | 12 | s->head = NULL; |
291 | 12 | *last_seg_ptr = s; |
292 | 12 | last_seg_ptr = &s->next; |
293 | 12 | seg_info (seg)->dwarf2_line_seg = s; |
294 | 12 | } |
295 | | |
296 | 682 | gas_assert (seg == s->seg); |
297 | | |
298 | 714 | for (pss = &s->head; (lss = *pss) != NULL ; pss = &lss->next) |
299 | 697 | { |
300 | 697 | if (lss->subseg == subseg) |
301 | 662 | goto found_subseg; |
302 | 35 | if (lss->subseg > subseg) |
303 | 3 | break; |
304 | 35 | } |
305 | | |
306 | 20 | lss = XNEW (struct line_subseg); |
307 | 20 | lss->next = *pss; |
308 | 20 | lss->subseg = subseg; |
309 | 20 | lss->head = NULL; |
310 | 20 | lss->ptail = &lss->head; |
311 | 20 | lss->pmove_tail = &lss->head; |
312 | 20 | *pss = lss; |
313 | | |
314 | 682 | found_subseg: |
315 | 682 | return lss; |
316 | 20 | } |
317 | | |
318 | | /* (Un)reverse the line_entry list starting from H. */ |
319 | | |
320 | | static struct line_entry * |
321 | | reverse_line_entry_list (struct line_entry *h) |
322 | 0 | { |
323 | 0 | struct line_entry *p = NULL, *e, *n; |
324 | |
|
325 | 0 | for (e = h; e; e = n) |
326 | 0 | { |
327 | 0 | n = e->next; |
328 | 0 | e->next = p; |
329 | 0 | p = e; |
330 | 0 | } |
331 | 0 | return p; |
332 | 0 | } |
333 | | |
334 | | /* Compute the view for E based on the previous entry P. If we |
335 | | introduce an (undefined) view symbol for P, and H is given (P must |
336 | | be the tail in this case), introduce view symbols for earlier list |
337 | | entries as well, until one of them is constant. */ |
338 | | |
339 | | static void |
340 | | set_or_check_view (struct line_entry *e, struct line_entry *p, |
341 | | struct line_entry *h) |
342 | 0 | { |
343 | 0 | expressionS viewx; |
344 | |
|
345 | 0 | memset (&viewx, 0, sizeof (viewx)); |
346 | 0 | viewx.X_unsigned = 1; |
347 | | |
348 | | /* First, compute !(E->label > P->label), to tell whether or not |
349 | | we're to reset the view number. If we can't resolve it to a |
350 | | constant, keep it symbolic. */ |
351 | 0 | if (!p || (e->loc.u.view == force_reset_view && force_reset_view)) |
352 | 0 | { |
353 | 0 | viewx.X_op = O_constant; |
354 | 0 | viewx.X_add_number = 0; |
355 | 0 | viewx.X_add_symbol = NULL; |
356 | 0 | viewx.X_op_symbol = NULL; |
357 | 0 | } |
358 | 0 | else |
359 | 0 | { |
360 | 0 | viewx.X_op = O_gt; |
361 | 0 | viewx.X_add_number = 0; |
362 | 0 | viewx.X_add_symbol = e->label; |
363 | 0 | viewx.X_op_symbol = p->label; |
364 | 0 | resolve_expression (&viewx); |
365 | 0 | if (viewx.X_op == O_constant) |
366 | 0 | viewx.X_add_number = !viewx.X_add_number; |
367 | 0 | else |
368 | 0 | { |
369 | 0 | viewx.X_add_symbol = make_expr_symbol (&viewx); |
370 | 0 | viewx.X_add_number = 0; |
371 | 0 | viewx.X_op_symbol = NULL; |
372 | 0 | viewx.X_op = O_logical_not; |
373 | 0 | } |
374 | 0 | } |
375 | |
|
376 | 0 | if (S_IS_DEFINED (e->loc.u.view) && symbol_constant_p (e->loc.u.view)) |
377 | 0 | { |
378 | 0 | expressionS *value = symbol_get_value_expression (e->loc.u.view); |
379 | | /* We can't compare the view numbers at this point, because in |
380 | | VIEWX we've only determined whether we're to reset it so |
381 | | far. */ |
382 | 0 | if (viewx.X_op == O_constant) |
383 | 0 | { |
384 | 0 | if (!value->X_add_number != !viewx.X_add_number) |
385 | 0 | as_bad (_("view number mismatch")); |
386 | 0 | } |
387 | | /* Record the expression to check it later. It is the result of |
388 | | a logical not, thus 0 or 1. We just add up all such deferred |
389 | | expressions, and resolve it at the end. */ |
390 | 0 | else if (!value->X_add_number) |
391 | 0 | { |
392 | 0 | symbolS *deferred = make_expr_symbol (&viewx); |
393 | 0 | if (view_assert_failed) |
394 | 0 | { |
395 | 0 | expressionS chk; |
396 | |
|
397 | 0 | memset (&chk, 0, sizeof (chk)); |
398 | 0 | chk.X_unsigned = 1; |
399 | 0 | chk.X_op = O_add; |
400 | 0 | chk.X_add_number = 0; |
401 | 0 | chk.X_add_symbol = view_assert_failed; |
402 | 0 | chk.X_op_symbol = deferred; |
403 | 0 | deferred = make_expr_symbol (&chk); |
404 | 0 | } |
405 | 0 | view_assert_failed = deferred; |
406 | 0 | } |
407 | 0 | } |
408 | |
|
409 | 0 | if (viewx.X_op != O_constant || viewx.X_add_number) |
410 | 0 | { |
411 | 0 | expressionS incv; |
412 | 0 | expressionS *p_view; |
413 | |
|
414 | 0 | if (!p->loc.u.view) |
415 | 0 | p->loc.u.view = symbol_temp_make (); |
416 | |
|
417 | 0 | memset (&incv, 0, sizeof (incv)); |
418 | 0 | incv.X_unsigned = 1; |
419 | 0 | incv.X_op = O_symbol; |
420 | 0 | incv.X_add_symbol = p->loc.u.view; |
421 | 0 | incv.X_add_number = 1; |
422 | 0 | p_view = symbol_get_value_expression (p->loc.u.view); |
423 | 0 | if (p_view->X_op == O_constant || p_view->X_op == O_symbol) |
424 | 0 | { |
425 | | /* If we can, constant fold increments so that a chain of |
426 | | expressions v + 1 + 1 ... + 1 is not created. |
427 | | resolve_expression isn't ideal for this purpose. The |
428 | | base v might not be resolvable until later. */ |
429 | 0 | incv.X_op = p_view->X_op; |
430 | 0 | incv.X_add_symbol = p_view->X_add_symbol; |
431 | 0 | incv.X_add_number = p_view->X_add_number + 1; |
432 | 0 | } |
433 | |
|
434 | 0 | if (viewx.X_op == O_constant) |
435 | 0 | { |
436 | 0 | gas_assert (viewx.X_add_number == 1); |
437 | 0 | viewx = incv; |
438 | 0 | } |
439 | 0 | else |
440 | 0 | { |
441 | 0 | viewx.X_add_symbol = make_expr_symbol (&viewx); |
442 | 0 | viewx.X_add_number = 0; |
443 | 0 | viewx.X_op_symbol = make_expr_symbol (&incv); |
444 | 0 | viewx.X_op = O_multiply; |
445 | 0 | } |
446 | 0 | } |
447 | | |
448 | 0 | if (!S_IS_DEFINED (e->loc.u.view)) |
449 | 0 | { |
450 | 0 | symbol_set_value_expression (e->loc.u.view, &viewx); |
451 | 0 | S_SET_SEGMENT (e->loc.u.view, expr_section); |
452 | 0 | symbol_set_frag (e->loc.u.view, &zero_address_frag); |
453 | 0 | } |
454 | | |
455 | | /* Define and attempt to simplify any earlier views needed to |
456 | | compute E's. */ |
457 | 0 | if (h && p && p->loc.u.view && !S_IS_DEFINED (p->loc.u.view)) |
458 | 0 | { |
459 | 0 | struct line_entry *h2; |
460 | | /* Reverse the list to avoid quadratic behavior going backwards |
461 | | in a single-linked list. */ |
462 | 0 | struct line_entry *r = reverse_line_entry_list (h); |
463 | |
|
464 | 0 | gas_assert (r == p); |
465 | | /* Set or check views until we find a defined or absent view. */ |
466 | 0 | do |
467 | 0 | { |
468 | | /* Do not define the head of a (sub?)segment view while |
469 | | handling others. It would be defined too early, without |
470 | | regard to the last view of other subsegments. |
471 | | set_or_check_view will be called for every head segment |
472 | | that needs it. */ |
473 | 0 | if (r == h) |
474 | 0 | break; |
475 | 0 | set_or_check_view (r, r->next, NULL); |
476 | 0 | } |
477 | 0 | while (r->next |
478 | 0 | && r->next->loc.u.view |
479 | 0 | && !S_IS_DEFINED (r->next->loc.u.view) |
480 | 0 | && (r = r->next)); |
481 | | |
482 | | /* Unreverse the list, so that we can go forward again. */ |
483 | 0 | h2 = reverse_line_entry_list (p); |
484 | 0 | gas_assert (h2 == h); |
485 | | |
486 | | /* Starting from the last view we just defined, attempt to |
487 | | simplify the view expressions, until we do so to P. */ |
488 | 0 | do |
489 | 0 | { |
490 | | /* The head view of a subsegment may remain undefined while |
491 | | handling other elements, before it is linked to the last |
492 | | view of the previous subsegment. */ |
493 | 0 | if (r == h) |
494 | 0 | continue; |
495 | 0 | gas_assert (S_IS_DEFINED (r->loc.u.view)); |
496 | 0 | resolve_expression (symbol_get_value_expression (r->loc.u.view)); |
497 | 0 | } |
498 | 0 | while (r != p && (r = r->next)); |
499 | | |
500 | | /* Now that we've defined and computed all earlier views that might |
501 | | be needed to compute E's, attempt to simplify it. */ |
502 | 0 | resolve_expression (symbol_get_value_expression (e->loc.u.view)); |
503 | 0 | } |
504 | 0 | } |
505 | | |
506 | | /* Record an entry for LOC occurring at LABEL. */ |
507 | | |
508 | | static void |
509 | | dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc) |
510 | 1.66k | { |
511 | 1.66k | struct line_subseg *lss; |
512 | 1.66k | struct line_entry *e; |
513 | 1.66k | flagword need_flags = SEC_LOAD | SEC_CODE; |
514 | | |
515 | | /* PR 26850: Do not record LOCs in non-executable or non-loaded |
516 | | sections. SEC_ALLOC isn't tested for non-ELF because obj-coff.c |
517 | | obj_coff_section is careless in setting SEC_ALLOC. */ |
518 | 1.66k | if (IS_ELF) |
519 | 1.66k | need_flags |= SEC_ALLOC; |
520 | 1.66k | if ((now_seg->flags & need_flags) != need_flags) |
521 | 986 | { |
522 | | /* FIXME: Add code to suppress multiple warnings ? */ |
523 | 986 | if (debug_type != DEBUG_DWARF2) |
524 | 986 | as_warn ("dwarf line number information for %s ignored", |
525 | 986 | segment_name (now_seg)); |
526 | 986 | return; |
527 | 986 | } |
528 | | |
529 | 682 | e = XNEW (struct line_entry); |
530 | 682 | e->next = NULL; |
531 | 682 | e->label = label; |
532 | 682 | e->loc = *loc; |
533 | | |
534 | 682 | lss = get_line_subseg (now_seg, now_subseg, true); |
535 | | |
536 | | /* Subseg heads are chained to previous subsegs in |
537 | | dwarf2_finish. */ |
538 | 682 | if (loc->filenum != -1u && loc->u.view && lss->head) |
539 | 0 | set_or_check_view (e, line_entry_at_tail (lss->head, lss->ptail), |
540 | 0 | lss->head); |
541 | | |
542 | 682 | *lss->ptail = e; |
543 | 682 | lss->ptail = &e->next; |
544 | 682 | } |
545 | | |
546 | | /* Record an entry for LOC occurring at OFS within the current fragment. */ |
547 | | |
548 | | static unsigned int dw2_line; |
549 | | static const char *dw2_filename; |
550 | | static int label_num; |
551 | | |
552 | | void |
553 | | dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc) |
554 | 8.13k | { |
555 | 8.13k | symbolS *sym; |
556 | | |
557 | | /* Early out for as-yet incomplete location information. */ |
558 | 8.13k | if (loc->line == 0) |
559 | 6.46k | return; |
560 | 1.66k | if (loc->filenum == 0) |
561 | 1.66k | { |
562 | 1.66k | if (dwarf_level < 5) |
563 | 0 | dwarf_level = 5; |
564 | 1.66k | if (DWARF2_LINE_VERSION < 5) |
565 | 0 | return; |
566 | 1.66k | } |
567 | | |
568 | | /* Don't emit sequences of line symbols for the same line when the |
569 | | symbols apply to assembler code. It is necessary to emit |
570 | | duplicate line symbols when a compiler asks for them, because GDB |
571 | | uses them to determine the end of the prologue. */ |
572 | 1.66k | if (debug_type == DEBUG_DWARF2) |
573 | 0 | { |
574 | 0 | if (dw2_line == loc->line) |
575 | 0 | { |
576 | 0 | if (dw2_filename == loc->u.filename) |
577 | 0 | return; |
578 | 0 | if (filename_cmp (dw2_filename, loc->u.filename) == 0) |
579 | 0 | { |
580 | 0 | dw2_filename = loc->u.filename; |
581 | 0 | return; |
582 | 0 | } |
583 | 0 | } |
584 | | |
585 | 0 | dw2_line = loc->line; |
586 | 0 | dw2_filename = loc->u.filename; |
587 | 0 | } |
588 | | |
589 | 1.66k | if (linkrelax) |
590 | 0 | { |
591 | 0 | char name[32]; |
592 | | |
593 | | /* Use a non-fake name for the line number location, |
594 | | so that it can be referred to by relocations. */ |
595 | 0 | sprintf (name, ".Loc.%u", label_num); |
596 | 0 | label_num++; |
597 | 0 | sym = symbol_new (name, now_seg, frag_now, ofs); |
598 | 0 | } |
599 | 1.66k | else |
600 | 1.66k | sym = symbol_temp_new (now_seg, frag_now, ofs); |
601 | 1.66k | dwarf2_gen_line_info_1 (sym, loc); |
602 | 1.66k | } |
603 | | |
604 | | static const char * |
605 | | get_basename (const char * pathname) |
606 | 562 | { |
607 | 562 | const char * file; |
608 | | |
609 | 562 | file = lbasename (pathname); |
610 | | /* Don't make empty string from / or A: from A:/ . */ |
611 | | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
612 | | if (file <= pathname + 3) |
613 | | file = pathname; |
614 | | #else |
615 | 562 | if (file == pathname + 1) |
616 | 0 | file = pathname; |
617 | 562 | #endif |
618 | 562 | return file; |
619 | 562 | } |
620 | | |
621 | | static unsigned int |
622 | | get_directory_table_entry (const char *dirname, |
623 | | const char *file0_dirname, |
624 | | size_t dirlen, |
625 | | bool can_use_zero) |
626 | 209 | { |
627 | 209 | unsigned int d; |
628 | | |
629 | 209 | if (dirlen == 0) |
630 | 112 | return 0; |
631 | | |
632 | 97 | #ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR |
633 | 97 | if (IS_DIR_SEPARATOR (dirname[dirlen - 1])) |
634 | 92 | { |
635 | 92 | -- dirlen; |
636 | 92 | if (dirlen == 0) |
637 | 0 | return 0; |
638 | 92 | } |
639 | 97 | #endif |
640 | | |
641 | 377 | for (d = 0; d < dirs_in_use; ++d) |
642 | 342 | { |
643 | 342 | if (dirs[d] != NULL |
644 | 342 | && filename_ncmp (dirname, dirs[d], dirlen) == 0 |
645 | 342 | && dirs[d][dirlen] == '\0') |
646 | 62 | return d; |
647 | 342 | } |
648 | | |
649 | 35 | if (can_use_zero) |
650 | 6 | { |
651 | 6 | if (dirs == NULL || dirs[0] == NULL) |
652 | 6 | { |
653 | 6 | const char * pwd = file0_dirname ? file0_dirname : getpwd (); |
654 | | |
655 | 6 | if (dwarf_level >= 5 && filename_cmp (dirname, pwd) != 0) |
656 | 1 | { |
657 | | /* In DWARF-5 the 0 entry in the directory table is |
658 | | expected to be the same as the DW_AT_comp_dir (which |
659 | | is set to the current build directory). Since we are |
660 | | about to create a directory entry that is not the |
661 | | same, allocate the current directory first. */ |
662 | 1 | (void) get_directory_table_entry (pwd, pwd, strlen (pwd), true); |
663 | 1 | d = dirs_in_use; |
664 | 1 | } |
665 | 5 | else |
666 | 5 | d = 0; |
667 | 6 | } |
668 | 6 | } |
669 | 29 | else if (d == 0) |
670 | 6 | d = 1; |
671 | | |
672 | 35 | if (d >= dirs_allocated) |
673 | 11 | { |
674 | 11 | unsigned int old = dirs_allocated; |
675 | 11 | #define DIR_TABLE_INCREMENT 32 |
676 | 11 | dirs_allocated = d + DIR_TABLE_INCREMENT; |
677 | 11 | dirs = XRESIZEVEC (char *, dirs, dirs_allocated); |
678 | 11 | memset (dirs + old, 0, (dirs_allocated - old) * sizeof (char *)); |
679 | 11 | } |
680 | | |
681 | 35 | dirs[d] = xmemdup0 (dirname, dirlen); |
682 | 35 | if (dirs_in_use <= d) |
683 | 35 | dirs_in_use = d + 1; |
684 | | |
685 | 35 | return d; |
686 | 97 | } |
687 | | |
688 | | static bool |
689 | | assign_file_to_slot (valueT i, const char *file, unsigned int dir) |
690 | 208 | { |
691 | 208 | if (i >= files_allocated) |
692 | 199 | { |
693 | 199 | unsigned int want = i + 32; |
694 | | |
695 | | /* If this array is taking 1G or more, someone is using silly |
696 | | file numbers. */ |
697 | 199 | if (want < i || want > UINT_MAX / 4 / sizeof (struct file_entry)) |
698 | 173 | { |
699 | 173 | as_bad (_("file number %" PRIu64 " is too big"), (uint64_t) i); |
700 | 173 | return false; |
701 | 173 | } |
702 | | |
703 | 26 | files = XRESIZEVEC (struct file_entry, files, want); |
704 | 26 | memset (files + files_allocated, 0, |
705 | 26 | (want - files_allocated) * sizeof (struct file_entry)); |
706 | 26 | files_allocated = want; |
707 | 26 | } |
708 | | |
709 | 35 | files[i].filename = file; |
710 | 35 | files[i].dir = dir; |
711 | 35 | memset (files[i].md5, 0, NUM_MD5_BYTES); |
712 | | |
713 | 35 | if (files_in_use < i + 1) |
714 | 26 | files_in_use = i + 1; |
715 | | |
716 | 35 | return true; |
717 | 208 | } |
718 | | |
719 | | /* Get a .debug_line file number for PATHNAME. If there is a |
720 | | directory component to PATHNAME, then this will be stored |
721 | | in the directory table, if it is not already present. |
722 | | Returns the slot number allocated to that filename or -1 |
723 | | if there was a problem. */ |
724 | | |
725 | | static int last_used; |
726 | | static int last_used_dir_len; |
727 | | |
728 | | static signed int |
729 | | allocate_filenum (const char * pathname) |
730 | 0 | { |
731 | 0 | const char *file; |
732 | 0 | size_t dir_len; |
733 | 0 | unsigned int i, dir; |
734 | | |
735 | | /* Short circuit the common case of adding the same pathname |
736 | | as last time. */ |
737 | 0 | if (last_used != -1) |
738 | 0 | { |
739 | 0 | const char * dirname = NULL; |
740 | |
|
741 | 0 | if (dirs != NULL) |
742 | 0 | dirname = dirs[files[last_used].dir]; |
743 | |
|
744 | 0 | if (dirname == NULL) |
745 | 0 | { |
746 | 0 | if (filename_cmp (pathname, files[last_used].filename) == 0) |
747 | 0 | return last_used; |
748 | 0 | } |
749 | 0 | else |
750 | 0 | { |
751 | 0 | if (filename_ncmp (pathname, dirname, last_used_dir_len - 1) == 0 |
752 | 0 | && IS_DIR_SEPARATOR (pathname [last_used_dir_len - 1]) |
753 | 0 | && filename_cmp (pathname + last_used_dir_len, |
754 | 0 | files[last_used].filename) == 0) |
755 | 0 | return last_used; |
756 | 0 | } |
757 | 0 | } |
758 | | |
759 | 0 | file = get_basename (pathname); |
760 | 0 | dir_len = file - pathname; |
761 | |
|
762 | 0 | dir = get_directory_table_entry (pathname, NULL, dir_len, false); |
763 | | |
764 | | /* Do not use slot-0. That is specifically reserved for use by |
765 | | the '.file 0 "name"' directive. */ |
766 | 0 | for (i = 1; i < files_in_use; ++i) |
767 | 0 | if (files[i].dir == dir |
768 | 0 | && files[i].filename |
769 | 0 | && filename_cmp (file, files[i].filename) == 0) |
770 | 0 | { |
771 | 0 | last_used = i; |
772 | 0 | last_used_dir_len = dir_len; |
773 | 0 | return i; |
774 | 0 | } |
775 | | |
776 | 0 | if (!assign_file_to_slot (i, file, dir)) |
777 | 0 | return -1; |
778 | | |
779 | 0 | last_used = i; |
780 | 0 | last_used_dir_len = dir_len; |
781 | |
|
782 | 0 | return i; |
783 | 0 | } |
784 | | |
785 | | /* Run through the list of line entries starting at E, allocating |
786 | | file entries for gas generated debug. */ |
787 | | |
788 | | static void |
789 | | do_allocate_filenum (struct line_entry *e) |
790 | 20 | { |
791 | 20 | do |
792 | 682 | { |
793 | 682 | if (e->loc.filenum == -1u) |
794 | 0 | { |
795 | 0 | e->loc.filenum = allocate_filenum (e->loc.u.filename); |
796 | 0 | e->loc.u.view = NULL; |
797 | 0 | } |
798 | 682 | e = e->next; |
799 | 682 | } |
800 | 682 | while (e); |
801 | 20 | } |
802 | | |
803 | | /* Remove any generated line entries. These don't live comfortably |
804 | | with compiler generated line info. If THELOT then remove |
805 | | everything, freeing all list entries we have created. */ |
806 | | |
807 | | static void |
808 | | purge_generated_debug (bool thelot) |
809 | 28 | { |
810 | 28 | struct line_seg *s, *nexts; |
811 | | |
812 | 40 | for (s = all_segs; s; s = nexts) |
813 | 12 | { |
814 | 12 | struct line_subseg *lss, *nextlss; |
815 | | |
816 | 32 | for (lss = s->head; lss; lss = nextlss) |
817 | 20 | { |
818 | 20 | struct line_entry *e, *next; |
819 | | |
820 | 702 | for (e = lss->head; e; e = next) |
821 | 682 | { |
822 | 682 | if (!thelot) |
823 | 682 | know (e->loc.filenum == -1u); |
824 | 682 | next = e->next; |
825 | 682 | free (e); |
826 | 682 | } |
827 | | |
828 | 20 | lss->head = NULL; |
829 | 20 | lss->ptail = &lss->head; |
830 | 20 | lss->pmove_tail = &lss->head; |
831 | 20 | nextlss = lss->next; |
832 | 20 | if (thelot) |
833 | 20 | free (lss); |
834 | 20 | } |
835 | 12 | nexts = s->next; |
836 | 12 | if (thelot) |
837 | 12 | { |
838 | 12 | seg_info (s->seg)->dwarf2_line_seg = NULL; |
839 | 12 | free (s); |
840 | 12 | } |
841 | 12 | } |
842 | 28 | } |
843 | | |
844 | | /* Allocate slot NUM in the .debug_line file table to FILENAME. |
845 | | If DIRNAME is not NULL or there is a directory component to FILENAME |
846 | | then this will be stored in the directory table, if not already present. |
847 | | if WITH_MD5 is TRUE then there is a md5 value in generic_bignum. |
848 | | Returns TRUE if allocation succeeded, FALSE otherwise. */ |
849 | | |
850 | | static bool |
851 | | allocate_filename_to_slot (const char *dirname, |
852 | | const char *filename, |
853 | | valueT num, |
854 | | bool with_md5) |
855 | 1.59k | { |
856 | 1.59k | const char *file; |
857 | 1.59k | size_t dirlen; |
858 | 1.59k | unsigned int i, d; |
859 | 1.59k | const char *file0_dirname; |
860 | | |
861 | | /* Short circuit the common case of adding the same pathname |
862 | | as last time. */ |
863 | 1.59k | if (num < files_allocated && files[num].filename != NULL) |
864 | 1.38k | { |
865 | 1.38k | const char * dir = NULL; |
866 | | |
867 | 1.38k | if (dirs != NULL) |
868 | 1.27k | dir = dirs[files[num].dir]; |
869 | | |
870 | 1.38k | if (with_md5 |
871 | 1.38k | && memcmp (generic_bignum, files[num].md5, NUM_MD5_BYTES) != 0) |
872 | 0 | goto fail; |
873 | | |
874 | 1.38k | if (dirname != NULL) |
875 | 315 | { |
876 | 315 | if (dir != NULL && filename_cmp (dir, dirname) != 0) |
877 | 100 | goto fail; |
878 | | |
879 | 215 | if (filename_cmp (filename, files[num].filename) != 0) |
880 | 205 | goto fail; |
881 | | |
882 | | /* If the filenames match, but the directory table entry was |
883 | | empty, then fill it with the provided directory name. */ |
884 | 10 | if (dir == NULL) |
885 | 0 | { |
886 | 0 | if (dirs == NULL) |
887 | 0 | { |
888 | 0 | dirs_allocated = files[num].dir + DIR_TABLE_INCREMENT; |
889 | 0 | dirs = XCNEWVEC (char *, dirs_allocated); |
890 | 0 | } |
891 | | |
892 | 0 | dirs[files[num].dir] = xmemdup0 (dirname, strlen (dirname)); |
893 | 0 | if (dirs_in_use <= files[num].dir) |
894 | 0 | dirs_in_use = files[num].dir + 1; |
895 | 0 | } |
896 | | |
897 | 10 | return true; |
898 | 215 | } |
899 | 1.07k | else if (dir != NULL) |
900 | 716 | { |
901 | 716 | dirlen = strlen (dir); |
902 | 716 | if (filename_ncmp (filename, dir, dirlen) == 0 |
903 | 716 | && IS_DIR_SEPARATOR (filename [dirlen]) |
904 | 716 | && filename_cmp (filename + dirlen + 1, files[num].filename) == 0) |
905 | 4 | return true; |
906 | 716 | } |
907 | 354 | else /* dir == NULL */ |
908 | 354 | { |
909 | 354 | file = get_basename (filename); |
910 | 354 | if (filename_cmp (file, files[num].filename) == 0) |
911 | 53 | { |
912 | | /* The filenames match, but the directory table entry is empty. |
913 | | Fill it with the provided directory name. */ |
914 | 53 | if (file > filename) |
915 | 0 | { |
916 | 0 | if (dirs == NULL) |
917 | 0 | { |
918 | 0 | dirs_allocated = files[num].dir + DIR_TABLE_INCREMENT; |
919 | 0 | dirs = XCNEWVEC (char *, dirs_allocated); |
920 | 0 | } |
921 | |
|
922 | 0 | dirs[files[num].dir] = xmemdup0 (filename, file - filename); |
923 | 0 | if (dirs_in_use <= files[num].dir) |
924 | 0 | dirs_in_use = files[num].dir + 1; |
925 | 0 | } |
926 | 53 | return true; |
927 | 53 | } |
928 | 354 | } |
929 | | |
930 | 1.31k | fail: |
931 | 1.31k | as_bad (_("file table slot %u is already occupied by a different file" |
932 | 1.31k | " (%s%s%s vs %s%s%s)"), |
933 | 1.31k | (unsigned int) num, |
934 | 1.31k | dir == NULL ? "" : dir, |
935 | 1.31k | dir == NULL ? "" : "/", |
936 | 1.31k | files[num].filename, |
937 | 1.31k | dirname == NULL ? "" : dirname, |
938 | 1.31k | dirname == NULL ? "" : "/", |
939 | 1.31k | filename); |
940 | 1.31k | return false; |
941 | 1.38k | } |
942 | | |
943 | | /* For file .0, the directory name is the current directory and the file |
944 | | may be in another directory contained in the file name. */ |
945 | 208 | if (num == 0) |
946 | 12 | { |
947 | 12 | file0_dirname = dirname; |
948 | | |
949 | 12 | file = get_basename (filename); |
950 | | |
951 | 12 | if (dirname && file == filename) |
952 | 4 | dirlen = strlen (dirname); |
953 | 8 | else |
954 | 8 | { |
955 | 8 | dirname = filename; |
956 | 8 | dirlen = file - filename; |
957 | 8 | } |
958 | 12 | } |
959 | 196 | else |
960 | 196 | { |
961 | 196 | file0_dirname = NULL; |
962 | | |
963 | 196 | if (dirname == NULL) |
964 | 196 | { |
965 | 196 | dirname = filename; |
966 | 196 | file = get_basename (filename); |
967 | 196 | dirlen = file - filename; |
968 | 196 | } |
969 | 0 | else |
970 | 0 | { |
971 | 0 | dirlen = strlen (dirname); |
972 | 0 | file = filename; |
973 | 0 | } |
974 | 196 | } |
975 | | |
976 | 208 | d = get_directory_table_entry (dirname, file0_dirname, dirlen, num == 0); |
977 | 208 | i = num; |
978 | | |
979 | 208 | if (!assign_file_to_slot (num, file, d)) |
980 | 173 | return false; |
981 | | |
982 | 35 | if (with_md5) |
983 | 0 | { |
984 | 0 | if (target_big_endian) |
985 | 0 | { |
986 | | /* md5's are stored in litte endian format. */ |
987 | 0 | unsigned int bits_remaining = NUM_MD5_BYTES * BITS_PER_CHAR; |
988 | 0 | unsigned int byte = NUM_MD5_BYTES; |
989 | 0 | unsigned int bignum_index = 0; |
990 | |
|
991 | 0 | while (bits_remaining) |
992 | 0 | { |
993 | 0 | unsigned int bignum_bits_remaining = LITTLENUM_NUMBER_OF_BITS; |
994 | 0 | valueT bignum_value = generic_bignum [bignum_index]; |
995 | 0 | bignum_index ++; |
996 | |
|
997 | 0 | while (bignum_bits_remaining) |
998 | 0 | { |
999 | 0 | files[i].md5[--byte] = bignum_value & 0xff; |
1000 | 0 | bignum_value >>= 8; |
1001 | 0 | bignum_bits_remaining -= 8; |
1002 | 0 | bits_remaining -= 8; |
1003 | 0 | } |
1004 | 0 | } |
1005 | 0 | } |
1006 | 0 | else |
1007 | 0 | { |
1008 | 0 | unsigned int bits_remaining = NUM_MD5_BYTES * BITS_PER_CHAR; |
1009 | 0 | unsigned int byte = 0; |
1010 | 0 | unsigned int bignum_index = 0; |
1011 | |
|
1012 | 0 | while (bits_remaining) |
1013 | 0 | { |
1014 | 0 | unsigned int bignum_bits_remaining = LITTLENUM_NUMBER_OF_BITS; |
1015 | 0 | valueT bignum_value = generic_bignum [bignum_index]; |
1016 | |
|
1017 | 0 | bignum_index ++; |
1018 | |
|
1019 | 0 | while (bignum_bits_remaining) |
1020 | 0 | { |
1021 | 0 | files[i].md5[byte++] = bignum_value & 0xff; |
1022 | 0 | bignum_value >>= 8; |
1023 | 0 | bignum_bits_remaining -= 8; |
1024 | 0 | bits_remaining -= 8; |
1025 | 0 | } |
1026 | 0 | } |
1027 | 0 | } |
1028 | 0 | } |
1029 | 35 | else |
1030 | 35 | memset (files[i].md5, 0, NUM_MD5_BYTES); |
1031 | | |
1032 | 35 | return true; |
1033 | 208 | } |
1034 | | |
1035 | | /* Returns the current source information. If .file directives have |
1036 | | been encountered, the info for the corresponding source file is |
1037 | | returned. Otherwise, the info for the assembly source file is |
1038 | | returned. */ |
1039 | | |
1040 | | void |
1041 | | dwarf2_where (struct dwarf2_line_info *line) |
1042 | 8.13k | { |
1043 | 8.13k | if (debug_type == DEBUG_DWARF2) |
1044 | 0 | { |
1045 | 0 | line->u.filename = as_where (&line->line); |
1046 | 0 | line->filenum = -1u; |
1047 | 0 | line->column = 0; |
1048 | 0 | line->flags = DWARF2_FLAG_IS_STMT; |
1049 | 0 | line->isa = current.isa; |
1050 | 0 | line->discriminator = current.discriminator; |
1051 | 0 | } |
1052 | 8.13k | else |
1053 | 8.13k | *line = current; |
1054 | 8.13k | } |
1055 | | |
1056 | | /* A hook to allow the target backend to inform the line number state |
1057 | | machine of isa changes when assembler debug info is enabled. */ |
1058 | | |
1059 | | void |
1060 | | dwarf2_set_isa (unsigned int isa) |
1061 | 0 | { |
1062 | 0 | current.isa = isa; |
1063 | 0 | } |
1064 | | |
1065 | | /* Called for each machine instruction, or relatively atomic group of |
1066 | | machine instructions (ie built-in macro). The instruction or group |
1067 | | is SIZE bytes in length. If dwarf2 line number generation is called |
1068 | | for, emit a line statement appropriately. */ |
1069 | | |
1070 | | void |
1071 | | dwarf2_emit_insn (int size) |
1072 | 11.2k | { |
1073 | 11.2k | struct dwarf2_line_info loc; |
1074 | | |
1075 | 11.2k | if (debug_type != DEBUG_DWARF2 |
1076 | 11.2k | ? !dwarf2_loc_directive_seen |
1077 | 11.2k | : !seen_at_least_1_file ()) |
1078 | 3.09k | return; |
1079 | | |
1080 | 8.13k | dwarf2_where (&loc); |
1081 | | |
1082 | 8.13k | dwarf2_gen_line_info ((frag_now_fix_octets () - size) / OCTETS_PER_BYTE, &loc); |
1083 | 8.13k | dwarf2_consume_line_info (); |
1084 | 8.13k | } |
1085 | | |
1086 | | /* Move all previously-emitted line entries for the current position by |
1087 | | DELTA bytes. This function cannot be used to move the same entries |
1088 | | twice. */ |
1089 | | |
1090 | | void |
1091 | | dwarf2_move_insn (int delta) |
1092 | 0 | { |
1093 | 0 | struct line_subseg *lss; |
1094 | 0 | struct line_entry *e; |
1095 | 0 | valueT now; |
1096 | |
|
1097 | 0 | if (delta == 0) |
1098 | 0 | return; |
1099 | | |
1100 | 0 | lss = get_line_subseg (now_seg, now_subseg, false); |
1101 | 0 | if (!lss) |
1102 | 0 | return; |
1103 | | |
1104 | 0 | now = frag_now_fix (); |
1105 | 0 | while ((e = *lss->pmove_tail)) |
1106 | 0 | { |
1107 | 0 | if (S_GET_VALUE (e->label) == now) |
1108 | 0 | S_SET_VALUE (e->label, now + delta); |
1109 | 0 | lss->pmove_tail = &e->next; |
1110 | 0 | } |
1111 | 0 | } |
1112 | | |
1113 | | /* Called after the current line information has been either used with |
1114 | | dwarf2_gen_line_info or saved with a machine instruction for later use. |
1115 | | This resets the state of the line number information to reflect that |
1116 | | it has been used. */ |
1117 | | |
1118 | | void |
1119 | | dwarf2_consume_line_info (void) |
1120 | 8.13k | { |
1121 | | /* Unless we generate DWARF2 debugging information for each |
1122 | | assembler line, we only emit one line symbol for one LOC. */ |
1123 | 8.13k | dwarf2_loc_directive_seen = false; |
1124 | | |
1125 | 8.13k | current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK |
1126 | 8.13k | | DWARF2_FLAG_PROLOGUE_END |
1127 | 8.13k | | DWARF2_FLAG_EPILOGUE_BEGIN); |
1128 | 8.13k | current.discriminator = 0; |
1129 | 8.13k | current.u.view = NULL; |
1130 | 8.13k | } |
1131 | | |
1132 | | /* Called for each (preferably code) label. If dwarf2_loc_mark_labels |
1133 | | is enabled, emit a basic block marker. */ |
1134 | | |
1135 | | void |
1136 | | dwarf2_emit_label (symbolS *label) |
1137 | 20.4k | { |
1138 | 20.4k | struct dwarf2_line_info loc; |
1139 | | |
1140 | 20.4k | if (!dwarf2_loc_mark_labels) |
1141 | 20.4k | return; |
1142 | 0 | if (S_GET_SEGMENT (label) != now_seg) |
1143 | 0 | return; |
1144 | 0 | if (!(bfd_section_flags (now_seg) & SEC_CODE)) |
1145 | 0 | return; |
1146 | 0 | if (files_in_use == 0 && debug_type != DEBUG_DWARF2) |
1147 | 0 | return; |
1148 | | |
1149 | 0 | dwarf2_where (&loc); |
1150 | |
|
1151 | 0 | loc.flags |= DWARF2_FLAG_BASIC_BLOCK; |
1152 | |
|
1153 | 0 | dwarf2_gen_line_info_1 (label, &loc); |
1154 | 0 | dwarf2_consume_line_info (); |
1155 | 0 | } |
1156 | | |
1157 | | /* Handle two forms of .file directive: |
1158 | | - Pass .file "source.c" to s_file |
1159 | | - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table |
1160 | | |
1161 | | If an entry is added to the file table, return a pointer to the filename. */ |
1162 | | |
1163 | | char * |
1164 | | dwarf2_directive_filename (void) |
1165 | 2.27k | { |
1166 | 2.27k | bool with_md5 = false; |
1167 | 2.27k | valueT num; |
1168 | 2.27k | char *filename; |
1169 | 2.27k | const char * dirname = NULL; |
1170 | 2.27k | int filename_len; |
1171 | | |
1172 | | /* Continue to accept a bare string and pass it off. */ |
1173 | 2.27k | SKIP_WHITESPACE (); |
1174 | 2.27k | if (*input_line_pointer == '"') |
1175 | 432 | { |
1176 | 432 | s_file (0); |
1177 | 432 | return NULL; |
1178 | 432 | } |
1179 | | |
1180 | 1.84k | num = get_absolute_expression (); |
1181 | | |
1182 | 1.84k | if ((offsetT) num < 1) |
1183 | 1.28k | { |
1184 | 1.28k | if (num == 0 && dwarf_level < 5) |
1185 | 1 | dwarf_level = 5; |
1186 | 1.28k | if ((offsetT) num < 0 || DWARF2_LINE_VERSION < 5) |
1187 | 13 | { |
1188 | 13 | as_bad (_("file number less than one")); |
1189 | 13 | ignore_rest_of_line (); |
1190 | 13 | return NULL; |
1191 | 13 | } |
1192 | 1.28k | } |
1193 | | |
1194 | | /* FIXME: Should we allow ".file <N>\n" as an expression meaning |
1195 | | "switch back to the already allocated file <N> as the current |
1196 | | file" ? */ |
1197 | | |
1198 | 1.83k | filename = demand_copy_C_string (&filename_len); |
1199 | 1.83k | if (filename == NULL) |
1200 | | /* demand_copy_C_string will have already generated an error message. */ |
1201 | 231 | return NULL; |
1202 | | |
1203 | | /* For DWARF-5 support we also accept: |
1204 | | .file <NUM> ["<dir>"] "<file>" [md5 <NUM>] */ |
1205 | 1.59k | if (DWARF2_LINE_VERSION > 4) |
1206 | 1.59k | { |
1207 | 1.59k | SKIP_WHITESPACE (); |
1208 | 1.59k | if (*input_line_pointer == '"') |
1209 | 325 | { |
1210 | 325 | dirname = filename; |
1211 | 325 | filename = demand_copy_C_string (&filename_len); |
1212 | 325 | if (filename == NULL) |
1213 | 6 | return NULL; |
1214 | 319 | SKIP_WHITESPACE (); |
1215 | 319 | } |
1216 | | |
1217 | 1.59k | if (startswith (input_line_pointer, "md5")) |
1218 | 0 | { |
1219 | 0 | input_line_pointer += 3; |
1220 | 0 | SKIP_WHITESPACE (); |
1221 | |
|
1222 | 0 | expressionS exp; |
1223 | 0 | expression_and_evaluate (& exp); |
1224 | 0 | if (exp.X_op != O_big) |
1225 | 0 | as_bad (_("md5 value too small or not a constant")); |
1226 | 0 | else |
1227 | 0 | with_md5 = true; |
1228 | 0 | } |
1229 | 1.59k | } |
1230 | | |
1231 | 1.59k | demand_empty_rest_of_line (); |
1232 | | |
1233 | | /* A .file directive implies compiler generated debug information is |
1234 | | being supplied. Turn off gas generated debug info. */ |
1235 | 1.59k | if (debug_type == DEBUG_DWARF2) |
1236 | 0 | purge_generated_debug (false); |
1237 | 1.59k | debug_type = DEBUG_NONE; |
1238 | | |
1239 | 1.59k | if (!allocate_filename_to_slot (dirname, filename, num, with_md5)) |
1240 | 1.49k | return NULL; |
1241 | | |
1242 | 102 | return filename; |
1243 | 1.59k | } |
1244 | | |
1245 | | /* Calls dwarf2_directive_filename, but discards its result. |
1246 | | Used in pseudo-op tables where the function result is ignored. */ |
1247 | | |
1248 | | void |
1249 | | dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED) |
1250 | 2.27k | { |
1251 | 2.27k | (void) dwarf2_directive_filename (); |
1252 | 2.27k | } |
1253 | | |
1254 | | void |
1255 | | dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED) |
1256 | 17.1k | { |
1257 | 17.1k | offsetT filenum, line; |
1258 | | |
1259 | | /* If we see two .loc directives in a row, force the first one to be |
1260 | | output now. */ |
1261 | 17.1k | if (dwarf2_loc_directive_seen) |
1262 | 7.81k | dwarf2_emit_insn (0); |
1263 | | |
1264 | 17.1k | filenum = get_absolute_expression (); |
1265 | 17.1k | SKIP_WHITESPACE (); |
1266 | 17.1k | line = get_absolute_expression (); |
1267 | | |
1268 | 17.1k | if (filenum < 1) |
1269 | 13.9k | { |
1270 | 13.9k | if (filenum == 0 && dwarf_level < 5) |
1271 | 0 | dwarf_level = 5; |
1272 | 13.9k | if (filenum < 0 || DWARF2_LINE_VERSION < 5) |
1273 | 77 | { |
1274 | 77 | as_bad (_("file number less than one")); |
1275 | 77 | return; |
1276 | 77 | } |
1277 | 13.9k | } |
1278 | | |
1279 | 17.1k | if ((valueT) filenum >= files_in_use || files[filenum].filename == NULL) |
1280 | 3.55k | { |
1281 | 3.55k | as_bad (_("unassigned file number %ld"), (long) filenum); |
1282 | 3.55k | return; |
1283 | 3.55k | } |
1284 | | |
1285 | | /* debug_type will be turned off by dwarf2_directive_filename, and |
1286 | | if we don't have a dwarf style .file then files_in_use will be |
1287 | | zero and the above error will trigger. */ |
1288 | 13.5k | gas_assert (debug_type == DEBUG_NONE); |
1289 | | |
1290 | 13.5k | current.filenum = filenum; |
1291 | 13.5k | current.line = line; |
1292 | 13.5k | current.discriminator = 0; |
1293 | | |
1294 | 13.5k | #ifndef NO_LISTING |
1295 | 13.5k | if (listing) |
1296 | 0 | { |
1297 | 0 | if (files[filenum].dir) |
1298 | 0 | { |
1299 | 0 | size_t dir_len = strlen (dirs[files[filenum].dir]); |
1300 | 0 | size_t file_len = strlen (files[filenum].filename); |
1301 | 0 | char *cp = XNEWVEC (char, dir_len + 1 + file_len + 1); |
1302 | |
|
1303 | 0 | memcpy (cp, dirs[files[filenum].dir], dir_len); |
1304 | 0 | INSERT_DIR_SEPARATOR (cp, dir_len); |
1305 | 0 | memcpy (cp + dir_len + 1, files[filenum].filename, file_len); |
1306 | 0 | cp[dir_len + file_len + 1] = '\0'; |
1307 | 0 | listing_source_file (cp); |
1308 | 0 | free (cp); |
1309 | 0 | } |
1310 | 0 | else |
1311 | 0 | listing_source_file (files[filenum].filename); |
1312 | 0 | listing_source_line (line); |
1313 | 0 | } |
1314 | 13.5k | #endif |
1315 | | |
1316 | 13.5k | SKIP_WHITESPACE (); |
1317 | 13.5k | if (ISDIGIT (*input_line_pointer)) |
1318 | 2.76k | { |
1319 | 2.76k | current.column = get_absolute_expression (); |
1320 | 2.76k | SKIP_WHITESPACE (); |
1321 | 2.76k | } |
1322 | | |
1323 | 13.5k | while (ISALPHA (*input_line_pointer)) |
1324 | 5.41k | { |
1325 | 5.41k | char *p, c; |
1326 | 5.41k | offsetT value; |
1327 | | |
1328 | 5.41k | c = get_symbol_name (& p); |
1329 | | |
1330 | 5.41k | if (strcmp (p, "basic_block") == 0) |
1331 | 0 | { |
1332 | 0 | current.flags |= DWARF2_FLAG_BASIC_BLOCK; |
1333 | 0 | restore_line_pointer (c); |
1334 | 0 | } |
1335 | 5.41k | else if (strcmp (p, "prologue_end") == 0) |
1336 | 0 | { |
1337 | 0 | if (dwarf_level < 3) |
1338 | 0 | dwarf_level = 3; |
1339 | 0 | current.flags |= DWARF2_FLAG_PROLOGUE_END; |
1340 | 0 | restore_line_pointer (c); |
1341 | 0 | } |
1342 | 5.41k | else if (strcmp (p, "epilogue_begin") == 0) |
1343 | 0 | { |
1344 | 0 | if (dwarf_level < 3) |
1345 | 0 | dwarf_level = 3; |
1346 | 0 | current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
1347 | 0 | restore_line_pointer (c); |
1348 | 0 | } |
1349 | 5.41k | else if (strcmp (p, "is_stmt") == 0) |
1350 | 0 | { |
1351 | 0 | (void) restore_line_pointer (c); |
1352 | 0 | value = get_absolute_expression (); |
1353 | 0 | if (value == 0) |
1354 | 0 | current.flags &= ~DWARF2_FLAG_IS_STMT; |
1355 | 0 | else if (value == 1) |
1356 | 0 | current.flags |= DWARF2_FLAG_IS_STMT; |
1357 | 0 | else |
1358 | 0 | { |
1359 | 0 | as_bad (_("is_stmt value not 0 or 1")); |
1360 | 0 | return; |
1361 | 0 | } |
1362 | 0 | } |
1363 | 5.41k | else if (strcmp (p, "isa") == 0) |
1364 | 0 | { |
1365 | 0 | if (dwarf_level < 3) |
1366 | 0 | dwarf_level = 3; |
1367 | 0 | (void) restore_line_pointer (c); |
1368 | 0 | value = get_absolute_expression (); |
1369 | 0 | if (value >= 0) |
1370 | 0 | current.isa = value; |
1371 | 0 | else |
1372 | 0 | { |
1373 | 0 | as_bad (_("isa number less than zero")); |
1374 | 0 | return; |
1375 | 0 | } |
1376 | 0 | } |
1377 | 5.41k | else if (strcmp (p, "discriminator") == 0) |
1378 | 0 | { |
1379 | 0 | (void) restore_line_pointer (c); |
1380 | 0 | value = get_absolute_expression (); |
1381 | 0 | if (value >= 0) |
1382 | 0 | current.discriminator = value; |
1383 | 0 | else |
1384 | 0 | { |
1385 | 0 | as_bad (_("discriminator less than zero")); |
1386 | 0 | return; |
1387 | 0 | } |
1388 | 0 | } |
1389 | 5.41k | else if (strcmp (p, "view") == 0) |
1390 | 0 | { |
1391 | 0 | symbolS *sym; |
1392 | |
|
1393 | 0 | (void) restore_line_pointer (c); |
1394 | 0 | SKIP_WHITESPACE (); |
1395 | |
|
1396 | 0 | if (ISDIGIT (*input_line_pointer) |
1397 | 0 | || *input_line_pointer == '-') |
1398 | 0 | { |
1399 | 0 | bool force_reset = *input_line_pointer == '-'; |
1400 | |
|
1401 | 0 | value = get_absolute_expression (); |
1402 | 0 | if (value != 0) |
1403 | 0 | { |
1404 | 0 | as_bad (_("numeric view can only be asserted to zero")); |
1405 | 0 | return; |
1406 | 0 | } |
1407 | 0 | if (force_reset && force_reset_view) |
1408 | 0 | sym = force_reset_view; |
1409 | 0 | else |
1410 | 0 | { |
1411 | 0 | sym = symbol_temp_new (absolute_section, &zero_address_frag, |
1412 | 0 | value); |
1413 | 0 | if (force_reset) |
1414 | 0 | force_reset_view = sym; |
1415 | 0 | } |
1416 | 0 | } |
1417 | 0 | else |
1418 | 0 | { |
1419 | 0 | char *name = read_symbol_name (); |
1420 | |
|
1421 | 0 | if (!name) |
1422 | 0 | return; |
1423 | 0 | sym = symbol_find_or_make (name); |
1424 | 0 | free (name); |
1425 | 0 | if (S_IS_DEFINED (sym) || symbol_equated_p (sym)) |
1426 | 0 | { |
1427 | 0 | if (S_IS_VOLATILE (sym)) |
1428 | 0 | sym = symbol_clone (sym, 1); |
1429 | 0 | else if (!S_CAN_BE_REDEFINED (sym)) |
1430 | 0 | { |
1431 | 0 | as_bad (_("symbol `%s' is already defined"), |
1432 | 0 | S_GET_NAME (sym)); |
1433 | 0 | return; |
1434 | 0 | } |
1435 | 0 | } |
1436 | 0 | S_SET_SEGMENT (sym, undefined_section); |
1437 | 0 | S_SET_VALUE (sym, 0); |
1438 | 0 | symbol_set_frag (sym, &zero_address_frag); |
1439 | 0 | } |
1440 | 0 | current.u.view = sym; |
1441 | 0 | } |
1442 | 5.41k | else |
1443 | 5.41k | { |
1444 | 5.41k | as_bad (_("unknown .loc sub-directive `%s'"), p); |
1445 | 5.41k | (void) restore_line_pointer (c); |
1446 | 5.41k | return; |
1447 | 5.41k | } |
1448 | | |
1449 | 0 | SKIP_WHITESPACE (); |
1450 | 0 | } |
1451 | | |
1452 | 8.13k | demand_empty_rest_of_line (); |
1453 | 8.13k | dwarf2_any_loc_directive_seen = dwarf2_loc_directive_seen = true; |
1454 | | |
1455 | | /* If we were given a view id, emit the row right away. */ |
1456 | 8.13k | if (current.u.view) |
1457 | 0 | dwarf2_emit_insn (0); |
1458 | 8.13k | } |
1459 | | |
1460 | | void |
1461 | | dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED) |
1462 | 0 | { |
1463 | 0 | offsetT value = get_absolute_expression (); |
1464 | |
|
1465 | 0 | if (value != 0 && value != 1) |
1466 | 0 | { |
1467 | 0 | as_bad (_("expected 0 or 1")); |
1468 | 0 | ignore_rest_of_line (); |
1469 | 0 | } |
1470 | 0 | else |
1471 | 0 | { |
1472 | 0 | dwarf2_loc_mark_labels = value != 0; |
1473 | 0 | demand_empty_rest_of_line (); |
1474 | 0 | } |
1475 | 0 | } |
1476 | | |
1477 | | static struct frag * |
1478 | | first_frag_for_seg (segT seg) |
1479 | 11 | { |
1480 | 11 | return seg_info (seg)->frchainP->frch_root; |
1481 | 11 | } |
1482 | | |
1483 | | static struct frag * |
1484 | | last_frag_for_seg (segT seg) |
1485 | 23 | { |
1486 | 23 | frchainS *f = seg_info (seg)->frchainP; |
1487 | | |
1488 | 98 | while (f->frch_next != NULL) |
1489 | 75 | f = f->frch_next; |
1490 | | |
1491 | 23 | return f->frch_last; |
1492 | 23 | } |
1493 | | |
1494 | | /* Emit a single byte into the current segment. */ |
1495 | | |
1496 | | static inline void |
1497 | | out_byte (int byte) |
1498 | 463 | { |
1499 | 463 | FRAG_APPEND_1_CHAR (byte); |
1500 | 463 | } |
1501 | | |
1502 | | /* Emit a statement program opcode into the current segment. */ |
1503 | | |
1504 | | static inline void |
1505 | | out_opcode (int opc) |
1506 | 89 | { |
1507 | 89 | out_byte (opc); |
1508 | 89 | } |
1509 | | |
1510 | | /* Emit a two-byte word into the current segment. */ |
1511 | | |
1512 | | static inline void |
1513 | | out_two (int data) |
1514 | 45 | { |
1515 | 45 | md_number_to_chars (frag_more (2), data, 2); |
1516 | 45 | } |
1517 | | |
1518 | | /* Emit a four byte word into the current segment. */ |
1519 | | |
1520 | | static inline void |
1521 | | out_four (int data) |
1522 | 0 | { |
1523 | 0 | md_number_to_chars (frag_more (4), data, 4); |
1524 | 0 | } |
1525 | | |
1526 | | /* Emit an unsigned "little-endian base 128" number. */ |
1527 | | |
1528 | | static void |
1529 | | out_uleb128 (addressT value) |
1530 | 417 | { |
1531 | 417 | output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0); |
1532 | 417 | } |
1533 | | |
1534 | | /* Emit a signed "little-endian base 128" number. */ |
1535 | | |
1536 | | static void |
1537 | | out_leb128 (addressT value) |
1538 | 0 | { |
1539 | 0 | output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1); |
1540 | 0 | } |
1541 | | |
1542 | | /* Emit a tuple for .debug_abbrev. */ |
1543 | | |
1544 | | static inline void |
1545 | | out_abbrev (int name, int form) |
1546 | 88 | { |
1547 | 88 | out_uleb128 (name); |
1548 | 88 | out_uleb128 (form); |
1549 | 88 | } |
1550 | | |
1551 | | /* Get the size of a fragment. */ |
1552 | | |
1553 | | static offsetT |
1554 | | get_frag_fix (fragS *frag, segT seg) |
1555 | 23 | { |
1556 | 23 | frchainS *fr; |
1557 | | |
1558 | 23 | if (frag->fr_next) |
1559 | 0 | return frag->fr_fix; |
1560 | | |
1561 | | /* If a fragment is the last in the chain, special measures must be |
1562 | | taken to find its size before relaxation, since it may be pending |
1563 | | on some subsegment chain. */ |
1564 | 98 | for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next) |
1565 | 98 | if (fr->frch_last == frag) |
1566 | 23 | return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal; |
1567 | | |
1568 | 23 | abort (); |
1569 | 23 | } |
1570 | | |
1571 | | /* Set an absolute address (may result in a relocation entry). */ |
1572 | | |
1573 | | static void |
1574 | | out_set_addr (symbolS *sym) |
1575 | 12 | { |
1576 | 12 | expressionS exp; |
1577 | | |
1578 | 12 | memset (&exp, 0, sizeof exp); |
1579 | 12 | out_opcode (DW_LNS_extended_op); |
1580 | 12 | out_uleb128 (sizeof_address + 1); |
1581 | | |
1582 | 12 | out_opcode (DW_LNE_set_address); |
1583 | 12 | exp.X_op = O_symbol; |
1584 | 12 | exp.X_add_symbol = sym; |
1585 | 12 | exp.X_add_number = 0; |
1586 | 12 | emit_expr (&exp, sizeof_address); |
1587 | 12 | } |
1588 | | |
1589 | | static void |
1590 | | scale_addr_delta (int line_delta, addressT *addr_delta) |
1591 | 1.05k | { |
1592 | 1.05k | static int printed_this = 0; |
1593 | 1.05k | if (DWARF2_LINE_MIN_INSN_LENGTH > 1) |
1594 | 0 | { |
1595 | | /* Don't error on non-instruction bytes at end of section. */ |
1596 | 0 | if (line_delta != INT_MAX |
1597 | 0 | && *addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0 && !printed_this) |
1598 | 0 | { |
1599 | 0 | as_bad("unaligned opcodes detected in executable segment"); |
1600 | 0 | printed_this = 1; |
1601 | 0 | } |
1602 | 0 | *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH; |
1603 | 0 | } |
1604 | 1.05k | } |
1605 | | |
1606 | | /* Encode a pair of line and address skips as efficiently as possible. |
1607 | | Note that the line skip is signed, whereas the address skip is unsigned. |
1608 | | |
1609 | | The following two routines *must* be kept in sync. This is |
1610 | | enforced by making emit_inc_line_addr abort if we do not emit |
1611 | | exactly the expected number of bytes. */ |
1612 | | |
1613 | | static int |
1614 | | size_inc_line_addr (int line_delta, addressT addr_delta) |
1615 | 694 | { |
1616 | 694 | unsigned int tmp, opcode; |
1617 | 694 | int len = 0; |
1618 | | |
1619 | | /* Scale the address delta by the minimum instruction length. */ |
1620 | 694 | scale_addr_delta (line_delta, &addr_delta); |
1621 | | |
1622 | | /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. |
1623 | | We cannot use special opcodes here, since we want the end_sequence |
1624 | | to emit the matrix entry. */ |
1625 | 694 | if (line_delta == INT_MAX) |
1626 | 12 | { |
1627 | 12 | if (addr_delta == MAX_SPECIAL_ADDR_DELTA) |
1628 | 0 | len = 1; |
1629 | 12 | else if (addr_delta) |
1630 | 12 | len = 1 + sizeof_leb128 (addr_delta, 0); |
1631 | 12 | return len + 3; |
1632 | 12 | } |
1633 | | |
1634 | | /* Bias the line delta by the base. */ |
1635 | 682 | tmp = (unsigned) line_delta - DWARF2_LINE_BASE; |
1636 | | |
1637 | | /* If the line increment is out of range of a special opcode, we |
1638 | | must encode it with DW_LNS_advance_line. */ |
1639 | 682 | if (tmp >= DWARF2_LINE_RANGE) |
1640 | 598 | { |
1641 | 598 | len = 1 + sizeof_leb128 (line_delta, 1); |
1642 | 598 | line_delta = 0; |
1643 | 598 | tmp = 0 - DWARF2_LINE_BASE; |
1644 | 598 | } |
1645 | | |
1646 | | /* Bias the opcode by the special opcode base. */ |
1647 | 682 | tmp += DWARF2_LINE_OPCODE_BASE; |
1648 | | |
1649 | | /* Avoid overflow when addr_delta is large. */ |
1650 | 682 | if (addr_delta < 256U + MAX_SPECIAL_ADDR_DELTA) |
1651 | 356 | { |
1652 | | /* Try using a special opcode. */ |
1653 | 356 | opcode = tmp + addr_delta * DWARF2_LINE_RANGE; |
1654 | 356 | if (opcode <= 255) |
1655 | 346 | return len + 1; |
1656 | | |
1657 | | /* Try using DW_LNS_const_add_pc followed by special op. */ |
1658 | 10 | opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; |
1659 | 10 | if (opcode <= 255) |
1660 | 5 | return len + 2; |
1661 | 10 | } |
1662 | | |
1663 | | /* Otherwise use DW_LNS_advance_pc. */ |
1664 | 331 | len += 1 + sizeof_leb128 (addr_delta, 0); |
1665 | | |
1666 | | /* DW_LNS_copy or special opcode. */ |
1667 | 331 | len += 1; |
1668 | | |
1669 | 331 | return len; |
1670 | 682 | } |
1671 | | |
1672 | | static void |
1673 | | emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len) |
1674 | 358 | { |
1675 | 358 | unsigned int tmp, opcode; |
1676 | 358 | int need_copy = 0; |
1677 | 358 | char *end = p + len; |
1678 | | |
1679 | | /* Line number sequences cannot go backward in addresses. This means |
1680 | | we've incorrectly ordered the statements in the sequence. */ |
1681 | 358 | gas_assert ((offsetT) addr_delta >= 0); |
1682 | | |
1683 | | /* Scale the address delta by the minimum instruction length. */ |
1684 | 358 | scale_addr_delta (line_delta, &addr_delta); |
1685 | | |
1686 | | /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. |
1687 | | We cannot use special opcodes here, since we want the end_sequence |
1688 | | to emit the matrix entry. */ |
1689 | 358 | if (line_delta == INT_MAX) |
1690 | 2 | { |
1691 | 2 | if (addr_delta == MAX_SPECIAL_ADDR_DELTA) |
1692 | 0 | *p++ = DW_LNS_const_add_pc; |
1693 | 2 | else if (addr_delta) |
1694 | 2 | { |
1695 | 2 | *p++ = DW_LNS_advance_pc; |
1696 | 2 | p += output_leb128 (p, addr_delta, 0); |
1697 | 2 | } |
1698 | | |
1699 | 2 | *p++ = DW_LNS_extended_op; |
1700 | 2 | *p++ = 1; |
1701 | 2 | *p++ = DW_LNE_end_sequence; |
1702 | 2 | goto done; |
1703 | 2 | } |
1704 | | |
1705 | | /* Bias the line delta by the base. */ |
1706 | 356 | tmp = (unsigned) line_delta - DWARF2_LINE_BASE; |
1707 | | |
1708 | | /* If the line increment is out of range of a special opcode, we |
1709 | | must encode it with DW_LNS_advance_line. */ |
1710 | 356 | if (tmp >= DWARF2_LINE_RANGE) |
1711 | 291 | { |
1712 | 291 | *p++ = DW_LNS_advance_line; |
1713 | 291 | p += output_leb128 (p, line_delta, 1); |
1714 | | |
1715 | 291 | line_delta = 0; |
1716 | 291 | tmp = 0 - DWARF2_LINE_BASE; |
1717 | 291 | need_copy = 1; |
1718 | 291 | } |
1719 | | |
1720 | | /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0" |
1721 | | special opcode. */ |
1722 | 356 | if (line_delta == 0 && addr_delta == 0) |
1723 | 247 | { |
1724 | 247 | *p++ = DW_LNS_copy; |
1725 | 247 | goto done; |
1726 | 247 | } |
1727 | | |
1728 | | /* Bias the opcode by the special opcode base. */ |
1729 | 109 | tmp += DWARF2_LINE_OPCODE_BASE; |
1730 | | |
1731 | | /* Avoid overflow when addr_delta is large. */ |
1732 | 109 | if (addr_delta < 256U + MAX_SPECIAL_ADDR_DELTA) |
1733 | 109 | { |
1734 | | /* Try using a special opcode. */ |
1735 | 109 | opcode = tmp + addr_delta * DWARF2_LINE_RANGE; |
1736 | 109 | if (opcode <= 255) |
1737 | 99 | { |
1738 | 99 | *p++ = opcode; |
1739 | 99 | goto done; |
1740 | 99 | } |
1741 | | |
1742 | | /* Try using DW_LNS_const_add_pc followed by special op. */ |
1743 | 10 | opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; |
1744 | 10 | if (opcode <= 255) |
1745 | 5 | { |
1746 | 5 | *p++ = DW_LNS_const_add_pc; |
1747 | 5 | *p++ = opcode; |
1748 | 5 | goto done; |
1749 | 5 | } |
1750 | 10 | } |
1751 | | |
1752 | | /* Otherwise use DW_LNS_advance_pc. */ |
1753 | 5 | *p++ = DW_LNS_advance_pc; |
1754 | 5 | p += output_leb128 (p, addr_delta, 0); |
1755 | | |
1756 | 5 | if (need_copy) |
1757 | 3 | *p++ = DW_LNS_copy; |
1758 | 2 | else |
1759 | 2 | *p++ = tmp; |
1760 | | |
1761 | 358 | done: |
1762 | 358 | gas_assert (p == end); |
1763 | 358 | } |
1764 | | |
1765 | | /* Handy routine to combine calls to the above two routines. */ |
1766 | | |
1767 | | static void |
1768 | | out_inc_line_addr (int line_delta, addressT addr_delta) |
1769 | 358 | { |
1770 | 358 | int len = size_inc_line_addr (line_delta, addr_delta); |
1771 | 358 | emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len); |
1772 | 358 | } |
1773 | | |
1774 | | /* Write out an alternative form of line and address skips using |
1775 | | DW_LNS_fixed_advance_pc opcodes. This uses more space than the default |
1776 | | line and address information, but it is required if linker relaxation |
1777 | | could change the code offsets. The following two routines *must* be |
1778 | | kept in sync. */ |
1779 | 0 | #define ADDR_DELTA_LIMIT 50000 |
1780 | | |
1781 | | static int |
1782 | | size_fixed_inc_line_addr (int line_delta, addressT addr_delta) |
1783 | 0 | { |
1784 | 0 | int len = 0; |
1785 | | |
1786 | | /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */ |
1787 | 0 | if (line_delta != INT_MAX) |
1788 | 0 | len = 1 + sizeof_leb128 (line_delta, 1); |
1789 | |
|
1790 | 0 | if (addr_delta > ADDR_DELTA_LIMIT) |
1791 | 0 | { |
1792 | | /* DW_LNS_extended_op */ |
1793 | 0 | len += 1 + sizeof_leb128 (sizeof_address + 1, 0); |
1794 | | /* DW_LNE_set_address */ |
1795 | 0 | len += 1 + sizeof_address; |
1796 | 0 | } |
1797 | 0 | else |
1798 | | /* DW_LNS_fixed_advance_pc */ |
1799 | 0 | len += 3; |
1800 | |
|
1801 | 0 | if (line_delta == INT_MAX) |
1802 | | /* DW_LNS_extended_op + DW_LNE_end_sequence */ |
1803 | 0 | len += 3; |
1804 | 0 | else |
1805 | | /* DW_LNS_copy */ |
1806 | 0 | len += 1; |
1807 | |
|
1808 | 0 | return len; |
1809 | 0 | } |
1810 | | |
1811 | | static void |
1812 | | emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag, |
1813 | | char *p, int len) |
1814 | 0 | { |
1815 | 0 | expressionS *pexp; |
1816 | 0 | char *end = p + len; |
1817 | | |
1818 | | /* Line number sequences cannot go backward in addresses. This means |
1819 | | we've incorrectly ordered the statements in the sequence. */ |
1820 | 0 | gas_assert ((offsetT) addr_delta >= 0); |
1821 | | |
1822 | | /* Verify that we have kept in sync with size_fixed_inc_line_addr. */ |
1823 | 0 | gas_assert (len == size_fixed_inc_line_addr (line_delta, addr_delta)); |
1824 | | |
1825 | | /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */ |
1826 | 0 | if (line_delta != INT_MAX) |
1827 | 0 | { |
1828 | 0 | *p++ = DW_LNS_advance_line; |
1829 | 0 | p += output_leb128 (p, line_delta, 1); |
1830 | 0 | } |
1831 | |
|
1832 | 0 | pexp = symbol_get_value_expression (frag->fr_symbol); |
1833 | | |
1834 | | /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can |
1835 | | advance the address by at most 64K. Linker relaxation (without |
1836 | | which this function would not be used) could change the operand by |
1837 | | an unknown amount. If the address increment is getting close to |
1838 | | the limit, just reset the address. */ |
1839 | 0 | if (addr_delta > ADDR_DELTA_LIMIT) |
1840 | 0 | { |
1841 | 0 | symbolS *to_sym; |
1842 | 0 | expressionS exp; |
1843 | |
|
1844 | 0 | memset (&exp, 0, sizeof exp); |
1845 | 0 | gas_assert (pexp->X_op == O_subtract); |
1846 | 0 | to_sym = pexp->X_add_symbol; |
1847 | |
|
1848 | 0 | *p++ = DW_LNS_extended_op; |
1849 | 0 | p += output_leb128 (p, sizeof_address + 1, 0); |
1850 | 0 | *p++ = DW_LNE_set_address; |
1851 | 0 | exp.X_op = O_symbol; |
1852 | 0 | exp.X_add_symbol = to_sym; |
1853 | 0 | exp.X_add_number = 0; |
1854 | 0 | emit_expr_fix (&exp, sizeof_address, frag, p, TC_PARSE_CONS_RETURN_NONE); |
1855 | 0 | p += sizeof_address; |
1856 | 0 | } |
1857 | 0 | else |
1858 | 0 | { |
1859 | 0 | *p++ = DW_LNS_fixed_advance_pc; |
1860 | 0 | emit_expr_fix (pexp, 2, frag, p, TC_PARSE_CONS_RETURN_NONE); |
1861 | 0 | p += 2; |
1862 | 0 | } |
1863 | | |
1864 | 0 | if (line_delta == INT_MAX) |
1865 | 0 | { |
1866 | 0 | *p++ = DW_LNS_extended_op; |
1867 | 0 | *p++ = 1; |
1868 | 0 | *p++ = DW_LNE_end_sequence; |
1869 | 0 | } |
1870 | 0 | else |
1871 | 0 | *p++ = DW_LNS_copy; |
1872 | |
|
1873 | 0 | gas_assert (p == end); |
1874 | 0 | } |
1875 | | |
1876 | | /* Generate a variant frag that we can use to relax address/line |
1877 | | increments between fragments of the target segment. */ |
1878 | | |
1879 | | static void |
1880 | | relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym) |
1881 | 336 | { |
1882 | 336 | expressionS exp; |
1883 | 336 | int max_chars; |
1884 | | |
1885 | 336 | memset (&exp, 0, sizeof exp); |
1886 | 336 | exp.X_op = O_subtract; |
1887 | 336 | exp.X_add_symbol = to_sym; |
1888 | 336 | exp.X_op_symbol = from_sym; |
1889 | 336 | exp.X_add_number = 0; |
1890 | | |
1891 | | /* The maximum size of the frag is the line delta with a maximum |
1892 | | sized address delta. */ |
1893 | 336 | if (DWARF2_USE_FIXED_ADVANCE_PC) |
1894 | 0 | max_chars = size_fixed_inc_line_addr (line_delta, |
1895 | 0 | -DWARF2_LINE_MIN_INSN_LENGTH); |
1896 | 336 | else |
1897 | 336 | max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH); |
1898 | | |
1899 | 336 | frag_var (rs_dwarf2dbg, max_chars, max_chars, 1, |
1900 | 336 | make_expr_symbol (&exp), line_delta, NULL); |
1901 | 336 | } |
1902 | | |
1903 | | /* The function estimates the size of a rs_dwarf2dbg variant frag |
1904 | | based on the current values of the symbols. It is called before |
1905 | | the relaxation loop. We set fr_subtype to the expected length. */ |
1906 | | |
1907 | | int |
1908 | | dwarf2dbg_estimate_size_before_relax (fragS *frag) |
1909 | 0 | { |
1910 | 0 | offsetT addr_delta; |
1911 | 0 | int size; |
1912 | |
|
1913 | 0 | addr_delta = resolve_symbol_value (frag->fr_symbol); |
1914 | 0 | if (DWARF2_USE_FIXED_ADVANCE_PC) |
1915 | 0 | size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta); |
1916 | 0 | else |
1917 | 0 | size = size_inc_line_addr (frag->fr_offset, addr_delta); |
1918 | |
|
1919 | 0 | frag->fr_subtype = size; |
1920 | |
|
1921 | 0 | return size; |
1922 | 0 | } |
1923 | | |
1924 | | /* This function relaxes a rs_dwarf2dbg variant frag based on the |
1925 | | current values of the symbols. fr_subtype is the current length |
1926 | | of the frag. This returns the change in frag length. */ |
1927 | | |
1928 | | int |
1929 | | dwarf2dbg_relax_frag (fragS *frag) |
1930 | 0 | { |
1931 | 0 | int old_size, new_size; |
1932 | |
|
1933 | 0 | old_size = frag->fr_subtype; |
1934 | 0 | new_size = dwarf2dbg_estimate_size_before_relax (frag); |
1935 | |
|
1936 | 0 | return new_size - old_size; |
1937 | 0 | } |
1938 | | |
1939 | | /* This function converts a rs_dwarf2dbg variant frag into a normal |
1940 | | fill frag. This is called after all relaxation has been done. |
1941 | | fr_subtype will be the desired length of the frag. */ |
1942 | | |
1943 | | void |
1944 | | dwarf2dbg_convert_frag (fragS *frag) |
1945 | 0 | { |
1946 | 0 | offsetT addr_diff; |
1947 | |
|
1948 | 0 | if (DWARF2_USE_FIXED_ADVANCE_PC) |
1949 | 0 | { |
1950 | | /* If linker relaxation is enabled then the distance between the two |
1951 | | symbols in the frag->fr_symbol expression might change. Hence we |
1952 | | cannot rely upon the value computed by resolve_symbol_value. |
1953 | | Instead we leave the expression unfinalized and allow |
1954 | | emit_fixed_inc_line_addr to create a fixup (which later becomes a |
1955 | | relocation) that will allow the linker to correctly compute the |
1956 | | actual address difference. We have to use a fixed line advance for |
1957 | | this as we cannot (easily) relocate leb128 encoded values. */ |
1958 | 0 | int saved_finalize_syms = finalize_syms; |
1959 | |
|
1960 | 0 | finalize_syms = 0; |
1961 | 0 | addr_diff = resolve_symbol_value (frag->fr_symbol); |
1962 | 0 | finalize_syms = saved_finalize_syms; |
1963 | 0 | } |
1964 | 0 | else |
1965 | 0 | addr_diff = resolve_symbol_value (frag->fr_symbol); |
1966 | | |
1967 | | /* fr_var carries the max_chars that we created the fragment with. |
1968 | | fr_subtype carries the current expected length. We must, of |
1969 | | course, have allocated enough memory earlier. */ |
1970 | 0 | gas_assert (frag->fr_var >= (int) frag->fr_subtype); |
1971 | | |
1972 | 0 | if (DWARF2_USE_FIXED_ADVANCE_PC) |
1973 | 0 | emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag, |
1974 | 0 | frag->fr_literal + frag->fr_fix, |
1975 | 0 | frag->fr_subtype); |
1976 | 0 | else |
1977 | 0 | emit_inc_line_addr (frag->fr_offset, addr_diff, |
1978 | 0 | frag->fr_literal + frag->fr_fix, frag->fr_subtype); |
1979 | |
|
1980 | 0 | frag->fr_fix += frag->fr_subtype; |
1981 | 0 | frag->fr_type = rs_fill; |
1982 | 0 | frag->fr_var = 0; |
1983 | 0 | frag->fr_offset = 0; |
1984 | 0 | } |
1985 | | |
1986 | | /* Generate .debug_line content for the chain of line number entries |
1987 | | beginning at E, for segment SEG. */ |
1988 | | |
1989 | | static void |
1990 | | process_entries (segT seg, struct line_entry *e) |
1991 | 12 | { |
1992 | 12 | unsigned filenum = 1; |
1993 | 12 | unsigned line = 1; |
1994 | 12 | unsigned column = 0; |
1995 | 12 | unsigned isa = 0; |
1996 | 12 | unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
1997 | 12 | fragS *last_frag = NULL, *frag; |
1998 | 12 | addressT last_frag_ofs = 0, frag_ofs; |
1999 | 12 | symbolS *last_lab = NULL, *lab; |
2000 | | |
2001 | 12 | if (flag_dwarf_sections) |
2002 | 0 | { |
2003 | 0 | char * name; |
2004 | 0 | const char * sec_name; |
2005 | | |
2006 | | /* Switch to the relevant sub-section before we start to emit |
2007 | | the line number table. |
2008 | | |
2009 | | FIXME: These sub-sections do not have a normal Line Number |
2010 | | Program Header, thus strictly speaking they are not valid |
2011 | | DWARF sections. Unfortunately the DWARF standard assumes |
2012 | | a one-to-one relationship between compilation units and |
2013 | | line number tables. Thus we have to have a .debug_line |
2014 | | section, as well as our sub-sections, and we have to ensure |
2015 | | that all of the sub-sections are merged into a proper |
2016 | | .debug_line section before a debugger sees them. */ |
2017 | |
|
2018 | 0 | sec_name = bfd_section_name (seg); |
2019 | 0 | if (strcmp (sec_name, ".text") != 0) |
2020 | 0 | { |
2021 | 0 | name = concat (".debug_line", sec_name, (char *) NULL); |
2022 | 0 | subseg_set (subseg_get (name, false), 0); |
2023 | 0 | } |
2024 | 0 | else |
2025 | | /* Don't create a .debug_line.text section - |
2026 | | that is redundant. Instead just switch back to the |
2027 | | normal .debug_line section. */ |
2028 | 0 | subseg_set (subseg_get (".debug_line", false), 0); |
2029 | 0 | } |
2030 | | |
2031 | 12 | do |
2032 | 682 | { |
2033 | 682 | int line_delta; |
2034 | | |
2035 | 682 | if (filenum != e->loc.filenum) |
2036 | 12 | { |
2037 | 12 | filenum = e->loc.filenum; |
2038 | 12 | out_opcode (DW_LNS_set_file); |
2039 | 12 | out_uleb128 (filenum); |
2040 | 12 | } |
2041 | | |
2042 | 682 | if (column != e->loc.column) |
2043 | 53 | { |
2044 | 53 | column = e->loc.column; |
2045 | 53 | out_opcode (DW_LNS_set_column); |
2046 | 53 | out_uleb128 (column); |
2047 | 53 | } |
2048 | | |
2049 | 682 | if (e->loc.discriminator != 0) |
2050 | 0 | { |
2051 | 0 | out_opcode (DW_LNS_extended_op); |
2052 | 0 | out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0)); |
2053 | 0 | out_opcode (DW_LNE_set_discriminator); |
2054 | 0 | out_uleb128 (e->loc.discriminator); |
2055 | 0 | } |
2056 | | |
2057 | 682 | if (isa != e->loc.isa) |
2058 | 0 | { |
2059 | 0 | isa = e->loc.isa; |
2060 | 0 | out_opcode (DW_LNS_set_isa); |
2061 | 0 | out_uleb128 (isa); |
2062 | 0 | } |
2063 | | |
2064 | 682 | if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT) |
2065 | 0 | { |
2066 | 0 | flags = e->loc.flags; |
2067 | 0 | out_opcode (DW_LNS_negate_stmt); |
2068 | 0 | } |
2069 | | |
2070 | 682 | if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK) |
2071 | 0 | out_opcode (DW_LNS_set_basic_block); |
2072 | | |
2073 | 682 | if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END) |
2074 | 0 | out_opcode (DW_LNS_set_prologue_end); |
2075 | | |
2076 | 682 | if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN) |
2077 | 0 | out_opcode (DW_LNS_set_epilogue_begin); |
2078 | | |
2079 | | /* Don't try to optimize away redundant entries; gdb wants two |
2080 | | entries for a function where the code starts on the same line as |
2081 | | the {, and there's no way to identify that case here. Trust gcc |
2082 | | to optimize appropriately. */ |
2083 | 682 | line_delta = e->loc.line - line; |
2084 | 682 | lab = e->label; |
2085 | 682 | frag = symbol_get_frag (lab); |
2086 | 682 | frag_ofs = S_GET_VALUE (lab); |
2087 | | |
2088 | 682 | if (last_frag == NULL |
2089 | 682 | || (e->loc.u.view == force_reset_view && force_reset_view |
2090 | | /* If we're going to reset the view, but we know we're |
2091 | | advancing the PC, we don't have to force with |
2092 | | set_address. We know we do when we're at the same |
2093 | | address of the same frag, and we know we might when |
2094 | | we're in the beginning of a frag, and we were at the |
2095 | | end of the previous frag. */ |
2096 | 670 | && (frag == last_frag |
2097 | 0 | ? (last_frag_ofs == frag_ofs) |
2098 | 0 | : (frag_ofs == 0 |
2099 | 0 | && ((offsetT)last_frag_ofs |
2100 | 0 | >= get_frag_fix (last_frag, seg)))))) |
2101 | 12 | { |
2102 | 12 | out_set_addr (lab); |
2103 | 12 | out_inc_line_addr (line_delta, 0); |
2104 | 12 | } |
2105 | 670 | else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC) |
2106 | 344 | out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs); |
2107 | 326 | else |
2108 | 326 | relax_inc_line_addr (line_delta, lab, last_lab); |
2109 | | |
2110 | 682 | line = e->loc.line; |
2111 | 682 | last_lab = lab; |
2112 | 682 | last_frag = frag; |
2113 | 682 | last_frag_ofs = frag_ofs; |
2114 | | |
2115 | 682 | e = e->next; |
2116 | 682 | } |
2117 | 682 | while (e); |
2118 | | |
2119 | | /* Emit a DW_LNE_end_sequence for the end of the section. */ |
2120 | 12 | frag = last_frag_for_seg (seg); |
2121 | 12 | frag_ofs = get_frag_fix (frag, seg); |
2122 | 12 | if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC) |
2123 | 2 | out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs); |
2124 | 10 | else |
2125 | 10 | { |
2126 | 10 | lab = symbol_temp_new (seg, frag, frag_ofs); |
2127 | 10 | relax_inc_line_addr (INT_MAX, lab, last_lab); |
2128 | 10 | } |
2129 | 12 | } |
2130 | | |
2131 | | /* Switch to LINE_STR_SEG and output the given STR. Return the |
2132 | | symbol pointing to the new string in the section. */ |
2133 | | |
2134 | | static symbolS * |
2135 | | add_line_strp (segT line_str_seg, const char *str) |
2136 | 77 | { |
2137 | 77 | char *cp; |
2138 | 77 | size_t size; |
2139 | 77 | symbolS *sym; |
2140 | | |
2141 | 77 | subseg_set (line_str_seg, 0); |
2142 | | |
2143 | 77 | sym = symbol_temp_new_now_octets (); |
2144 | | |
2145 | 77 | size = strlen (str) + 1; |
2146 | 77 | cp = frag_more (size); |
2147 | 77 | memcpy (cp, str, size); |
2148 | | |
2149 | 77 | return sym; |
2150 | 77 | } |
2151 | | |
2152 | | |
2153 | | /* Emit the directory and file tables for .debug_line. */ |
2154 | | |
2155 | | static void |
2156 | | out_dir_and_file_list (segT line_seg, int sizeof_offset) |
2157 | 12 | { |
2158 | 12 | size_t size; |
2159 | 12 | char *dir; |
2160 | 12 | char *cp; |
2161 | 12 | unsigned int i, j; |
2162 | 12 | bool emit_md5 = false; |
2163 | 12 | bool emit_timestamps = true; |
2164 | 12 | bool emit_filesize = true; |
2165 | 12 | segT line_str_seg = NULL; |
2166 | 12 | symbolS *line_strp, *file0_strp = NULL; |
2167 | | |
2168 | | /* Output the Directory Table. */ |
2169 | 12 | if (DWARF2_LINE_VERSION >= 5) |
2170 | 12 | { |
2171 | | /* We only have one column in the directory table. */ |
2172 | 12 | out_byte (1); |
2173 | | |
2174 | | /* Describe the purpose and format of the column. */ |
2175 | 12 | out_uleb128 (DW_LNCT_path); |
2176 | | /* Store these strings in the .debug_line_str section so they |
2177 | | can be shared. */ |
2178 | 12 | out_uleb128 (DW_FORM_line_strp); |
2179 | | |
2180 | | /* Now state how many rows there are in the table. We need at |
2181 | | least 1 if there is one or more file names to store the |
2182 | | "working directory". */ |
2183 | 12 | if (dirs_in_use == 0 && files_in_use > 0) |
2184 | 1 | out_uleb128 (1); |
2185 | 11 | else |
2186 | 11 | out_uleb128 (dirs_in_use); |
2187 | 12 | } |
2188 | | |
2189 | | /* Emit directory list. */ |
2190 | 12 | if (DWARF2_LINE_VERSION >= 5 && (dirs_in_use > 0 || files_in_use > 0)) |
2191 | 12 | { |
2192 | 12 | line_str_seg = subseg_new (".debug_line_str", 0); |
2193 | 12 | bfd_set_section_flags (line_str_seg, |
2194 | 12 | SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS |
2195 | 12 | | SEC_MERGE | SEC_STRINGS); |
2196 | 12 | line_str_seg->entsize = 1; |
2197 | | |
2198 | | /* DWARF5 uses slot zero, but that is only set explicitly |
2199 | | using a .file 0 directive. Otherwise use pwd as main file |
2200 | | directory. */ |
2201 | 12 | if (dirs_in_use > 0 && dirs[0] != NULL) |
2202 | 5 | dir = remap_debug_filename (dirs[0]); |
2203 | 7 | else |
2204 | 7 | dir = remap_debug_filename (getpwd ()); |
2205 | | |
2206 | 12 | line_strp = add_line_strp (line_str_seg, dir); |
2207 | 12 | free (dir); |
2208 | 12 | subseg_set (line_seg, 0); |
2209 | 12 | TC_DWARF2_EMIT_OFFSET (line_strp, sizeof_offset); |
2210 | 12 | } |
2211 | 42 | for (i = 1; i < dirs_in_use; ++i) |
2212 | 30 | { |
2213 | 30 | dir = remap_debug_filename (dirs[i]); |
2214 | 30 | if (DWARF2_LINE_VERSION < 5) |
2215 | 0 | { |
2216 | 0 | size = strlen (dir) + 1; |
2217 | 0 | cp = frag_more (size); |
2218 | 0 | memcpy (cp, dir, size); |
2219 | 0 | } |
2220 | 30 | else |
2221 | 30 | { |
2222 | 30 | line_strp = add_line_strp (line_str_seg, dir); |
2223 | 30 | subseg_set (line_seg, 0); |
2224 | 30 | TC_DWARF2_EMIT_OFFSET (line_strp, sizeof_offset); |
2225 | 30 | } |
2226 | 30 | free (dir); |
2227 | 30 | } |
2228 | | |
2229 | 12 | if (DWARF2_LINE_VERSION < 5) |
2230 | | /* Terminate it. */ |
2231 | 0 | out_byte ('\0'); |
2232 | | |
2233 | | /* Output the File Name Table. */ |
2234 | 12 | if (DWARF2_LINE_VERSION >= 5) |
2235 | 12 | { |
2236 | 12 | unsigned int columns = 4; |
2237 | | |
2238 | 12 | if (((unsigned long) DWARF2_FILE_TIME_NAME ("", "")) == -1UL) |
2239 | 12 | { |
2240 | 12 | emit_timestamps = false; |
2241 | 12 | -- columns; |
2242 | 12 | } |
2243 | | |
2244 | 12 | if (DWARF2_FILE_SIZE_NAME ("", "") == -1) |
2245 | 12 | { |
2246 | 12 | emit_filesize = false; |
2247 | 12 | -- columns; |
2248 | 12 | } |
2249 | | |
2250 | 1.97k | for (i = 0; i < files_in_use; ++i) |
2251 | 1.95k | if (files[i].md5[0] != 0) |
2252 | 0 | break; |
2253 | 12 | if (i < files_in_use) |
2254 | 0 | { |
2255 | 0 | emit_md5 = true; |
2256 | 0 | ++ columns; |
2257 | 0 | } |
2258 | | |
2259 | | /* The number of format entries to follow. */ |
2260 | 12 | out_byte (columns); |
2261 | | /* The format of the file name. */ |
2262 | 12 | out_uleb128 (DW_LNCT_path); |
2263 | | /* Store these strings in the .debug_line_str section so they |
2264 | | can be shared. */ |
2265 | 12 | out_uleb128 (DW_FORM_line_strp); |
2266 | | |
2267 | | /* The format of the directory index. */ |
2268 | 12 | out_uleb128 (DW_LNCT_directory_index); |
2269 | 12 | out_uleb128 (DW_FORM_udata); |
2270 | | |
2271 | 12 | if (emit_timestamps) |
2272 | 0 | { |
2273 | | /* The format of the timestamp. */ |
2274 | 0 | out_uleb128 (DW_LNCT_timestamp); |
2275 | 0 | out_uleb128 (DW_FORM_udata); |
2276 | 0 | } |
2277 | | |
2278 | 12 | if (emit_filesize) |
2279 | 0 | { |
2280 | | /* The format of the file size. */ |
2281 | 0 | out_uleb128 (DW_LNCT_size); |
2282 | 0 | out_uleb128 (DW_FORM_udata); |
2283 | 0 | } |
2284 | | |
2285 | 12 | if (emit_md5) |
2286 | 0 | { |
2287 | | /* The format of the MD5 sum. */ |
2288 | 0 | out_uleb128 (DW_LNCT_MD5); |
2289 | 0 | out_uleb128 (DW_FORM_data16); |
2290 | 0 | } |
2291 | | |
2292 | | /* The number of entries in the table. */ |
2293 | 12 | out_uleb128 (files_in_use); |
2294 | 12 | } |
2295 | | |
2296 | 1.97k | for (i = DWARF2_LINE_VERSION > 4 ? 0 : 1; i < files_in_use; ++i) |
2297 | 1.95k | { |
2298 | 1.95k | const char *fullfilename; |
2299 | | |
2300 | 1.95k | if (files[i].filename == NULL) |
2301 | 1.92k | { |
2302 | 1.92k | if (DWARF2_LINE_VERSION < 5 || i != 0) |
2303 | 1.92k | { |
2304 | 1.92k | as_bad (_("unassigned file number %ld"), (long) i); |
2305 | 1.92k | continue; |
2306 | 1.92k | } |
2307 | | /* DWARF5 uses slot zero, but that is only set explicitly using |
2308 | | a .file 0 directive. If that isn't used, but file 1 is, then |
2309 | | use that as main file name. */ |
2310 | 0 | if (files_in_use > 1 && files[1].filename != NULL) |
2311 | 0 | { |
2312 | 0 | files[0].filename = files[1].filename; |
2313 | 0 | files[0].dir = files[1].dir; |
2314 | 0 | if (emit_md5) |
2315 | 0 | for (j = 0; j < NUM_MD5_BYTES; ++j) |
2316 | 0 | files[0].md5[j] = files[1].md5[j]; |
2317 | 0 | } |
2318 | 0 | else |
2319 | 0 | files[0].filename = ""; |
2320 | 0 | } |
2321 | | |
2322 | 35 | fullfilename = DWARF2_FILE_NAME (files[i].filename, |
2323 | 35 | files[i].dir ? dirs [files [i].dir] : ""); |
2324 | 35 | if (DWARF2_LINE_VERSION < 5) |
2325 | 0 | { |
2326 | 0 | size = strlen (fullfilename) + 1; |
2327 | 0 | cp = frag_more (size); |
2328 | 0 | memcpy (cp, fullfilename, size); |
2329 | 0 | } |
2330 | 35 | else |
2331 | 35 | { |
2332 | 35 | if (!file0_strp) |
2333 | 35 | line_strp = add_line_strp (line_str_seg, fullfilename); |
2334 | 0 | else |
2335 | 0 | line_strp = file0_strp; |
2336 | 35 | subseg_set (line_seg, 0); |
2337 | 35 | TC_DWARF2_EMIT_OFFSET (line_strp, sizeof_offset); |
2338 | 35 | if (i == 0 && files_in_use > 1 |
2339 | 35 | && files[0].filename == files[1].filename) |
2340 | 0 | file0_strp = line_strp; |
2341 | 35 | else |
2342 | 35 | file0_strp = NULL; |
2343 | 35 | } |
2344 | | |
2345 | | /* Directory number. */ |
2346 | 35 | out_uleb128 (files[i].dir); |
2347 | | |
2348 | | /* Output the last modification timestamp. */ |
2349 | 35 | if (emit_timestamps) |
2350 | 0 | { |
2351 | 0 | offsetT timestamp; |
2352 | |
|
2353 | 0 | timestamp = DWARF2_FILE_TIME_NAME (files[i].filename, |
2354 | 0 | files[i].dir ? dirs [files [i].dir] : ""); |
2355 | 0 | if (timestamp == -1) |
2356 | 0 | timestamp = 0; |
2357 | 0 | out_uleb128 (timestamp); |
2358 | 0 | } |
2359 | | |
2360 | | /* Output the filesize. */ |
2361 | 35 | if (emit_filesize) |
2362 | 0 | { |
2363 | 0 | offsetT filesize; |
2364 | 0 | filesize = DWARF2_FILE_SIZE_NAME (files[i].filename, |
2365 | 0 | files[i].dir ? dirs [files [i].dir] : ""); |
2366 | 0 | if (filesize == -1) |
2367 | 0 | filesize = 0; |
2368 | 0 | out_uleb128 (filesize); |
2369 | 0 | } |
2370 | | |
2371 | | /* Output the md5 sum. */ |
2372 | 35 | if (emit_md5) |
2373 | 0 | { |
2374 | 0 | int b; |
2375 | |
|
2376 | 0 | for (b = 0; b < NUM_MD5_BYTES; b++) |
2377 | 0 | out_byte (files[i].md5[b]); |
2378 | 0 | } |
2379 | 35 | } |
2380 | | |
2381 | 12 | if (DWARF2_LINE_VERSION < 5) |
2382 | | /* Terminate filename list. */ |
2383 | 0 | out_byte (0); |
2384 | 12 | } |
2385 | | |
2386 | | /* Switch to SEC and output a header length field. Return the size of |
2387 | | offsets used in SEC. The caller must set EXPR->X_add_symbol value |
2388 | | to the end of the section. EXPR->X_add_number will be set to the |
2389 | | negative size of the header. */ |
2390 | | |
2391 | | static int |
2392 | | out_header (asection *sec, expressionS *exp) |
2393 | 34 | { |
2394 | 34 | symbolS *start_sym; |
2395 | 34 | symbolS *end_sym; |
2396 | | |
2397 | 34 | subseg_set (sec, 0); |
2398 | | |
2399 | 34 | if (flag_dwarf_sections) |
2400 | 0 | { |
2401 | | /* If we are going to put the start and end symbols in different |
2402 | | sections, then we need real symbols, not just fake, local ones. */ |
2403 | 0 | frag_now_fix (); |
2404 | 0 | start_sym = symbol_make (".Ldebug_line_start"); |
2405 | 0 | end_sym = symbol_make (".Ldebug_line_end"); |
2406 | 0 | symbol_set_value_now (start_sym); |
2407 | 0 | } |
2408 | 34 | else |
2409 | 34 | { |
2410 | 34 | start_sym = symbol_temp_new_now_octets (); |
2411 | 34 | end_sym = symbol_temp_make (); |
2412 | 34 | } |
2413 | | |
2414 | | /* Total length of the information. */ |
2415 | 34 | exp->X_op = O_subtract; |
2416 | 34 | exp->X_add_symbol = end_sym; |
2417 | 34 | exp->X_op_symbol = start_sym; |
2418 | | |
2419 | 34 | switch (DWARF2_FORMAT (sec)) |
2420 | 34 | { |
2421 | 34 | case dwarf2_format_32bit: |
2422 | 34 | exp->X_add_number = -4; |
2423 | 34 | emit_expr (exp, 4); |
2424 | 34 | return 4; |
2425 | | |
2426 | 0 | case dwarf2_format_64bit: |
2427 | 0 | exp->X_add_number = -12; |
2428 | 0 | out_four (-1); |
2429 | 0 | emit_expr (exp, 8); |
2430 | 0 | return 8; |
2431 | | |
2432 | 0 | case dwarf2_format_64bit_irix: |
2433 | 0 | exp->X_add_number = -8; |
2434 | 0 | emit_expr (exp, 8); |
2435 | 0 | return 8; |
2436 | 34 | } |
2437 | | |
2438 | 0 | as_fatal (_("internal error: unknown dwarf2 format")); |
2439 | 0 | return 0; |
2440 | 34 | } |
2441 | | |
2442 | | /* Emit the collected .debug_line data. */ |
2443 | | |
2444 | | static void |
2445 | | out_debug_line (segT line_seg) |
2446 | 12 | { |
2447 | 12 | expressionS exp; |
2448 | 12 | symbolS *prologue_start, *prologue_end; |
2449 | 12 | symbolS *line_end; |
2450 | 12 | struct line_seg *s; |
2451 | 12 | int sizeof_offset; |
2452 | | |
2453 | 12 | memset (&exp, 0, sizeof exp); |
2454 | 12 | sizeof_offset = out_header (line_seg, &exp); |
2455 | 12 | line_end = exp.X_add_symbol; |
2456 | | |
2457 | | /* Version. */ |
2458 | 12 | out_two (DWARF2_LINE_VERSION); |
2459 | | |
2460 | 12 | if (DWARF2_LINE_VERSION >= 5) |
2461 | 12 | { |
2462 | 12 | out_byte (sizeof_address); |
2463 | 12 | out_byte (0); /* Segment Selector size. */ |
2464 | 12 | } |
2465 | | /* Length of the prologue following this length. */ |
2466 | 12 | prologue_start = symbol_temp_make (); |
2467 | 12 | prologue_end = symbol_temp_make (); |
2468 | 12 | exp.X_op = O_subtract; |
2469 | 12 | exp.X_add_symbol = prologue_end; |
2470 | 12 | exp.X_op_symbol = prologue_start; |
2471 | 12 | exp.X_add_number = 0; |
2472 | 12 | emit_expr (&exp, sizeof_offset); |
2473 | 12 | symbol_set_value_now (prologue_start); |
2474 | | |
2475 | | /* Parameters of the state machine. */ |
2476 | 12 | out_byte (DWARF2_LINE_MIN_INSN_LENGTH); |
2477 | 12 | if (DWARF2_LINE_VERSION >= 4) |
2478 | 12 | out_byte (DWARF2_LINE_MAX_OPS_PER_INSN); |
2479 | 12 | out_byte (DWARF2_LINE_DEFAULT_IS_STMT); |
2480 | 12 | out_byte (DWARF2_LINE_BASE); |
2481 | 12 | out_byte (DWARF2_LINE_RANGE); |
2482 | 12 | out_byte (DWARF2_LINE_OPCODE_BASE); |
2483 | | |
2484 | | /* Standard opcode lengths. */ |
2485 | 12 | out_byte (0); /* DW_LNS_copy */ |
2486 | 12 | out_byte (1); /* DW_LNS_advance_pc */ |
2487 | 12 | out_byte (1); /* DW_LNS_advance_line */ |
2488 | 12 | out_byte (1); /* DW_LNS_set_file */ |
2489 | 12 | out_byte (1); /* DW_LNS_set_column */ |
2490 | 12 | out_byte (0); /* DW_LNS_negate_stmt */ |
2491 | 12 | out_byte (0); /* DW_LNS_set_basic_block */ |
2492 | 12 | out_byte (0); /* DW_LNS_const_add_pc */ |
2493 | 12 | out_byte (1); /* DW_LNS_fixed_advance_pc */ |
2494 | 12 | if (DWARF2_LINE_VERSION >= 3) |
2495 | 12 | { |
2496 | 12 | out_byte (0); /* DW_LNS_set_prologue_end */ |
2497 | 12 | out_byte (0); /* DW_LNS_set_epilogue_begin */ |
2498 | 12 | out_byte (1); /* DW_LNS_set_isa */ |
2499 | | /* We have emitted 12 opcode lengths, so make that this |
2500 | | matches up to the opcode base value we have been using. */ |
2501 | 12 | gas_assert (DWARF2_LINE_OPCODE_BASE == 13); |
2502 | 12 | } |
2503 | 0 | else |
2504 | 0 | gas_assert (DWARF2_LINE_OPCODE_BASE == 10); |
2505 | | |
2506 | 12 | out_dir_and_file_list (line_seg, sizeof_offset); |
2507 | | |
2508 | 12 | symbol_set_value_now (prologue_end); |
2509 | | |
2510 | | /* For each section, emit a statement program. */ |
2511 | 24 | for (s = all_segs; s; s = s->next) |
2512 | | /* Paranoia - this check should have already have |
2513 | | been handled in dwarf2_gen_line_info_1(). */ |
2514 | 12 | if (s->head->head && SEG_NORMAL (s->seg)) |
2515 | 12 | process_entries (s->seg, s->head->head); |
2516 | | |
2517 | 12 | if (flag_dwarf_sections) |
2518 | | /* We have to switch to the special .debug_line_end section |
2519 | | before emitting the end-of-debug_line symbol. The linker |
2520 | | script arranges for this section to be placed after all the |
2521 | | (potentially garbage collected) .debug_line.<foo> sections. |
2522 | | This section contains the line_end symbol which is used to |
2523 | | compute the size of the linked .debug_line section, as seen |
2524 | | in the DWARF Line Number header. */ |
2525 | 0 | subseg_set (subseg_get (".debug_line_end", false), 0); |
2526 | | |
2527 | 12 | symbol_set_value_now (line_end); |
2528 | 12 | } |
2529 | | |
2530 | | static void |
2531 | | out_debug_ranges (segT ranges_seg, symbolS **ranges_sym) |
2532 | 0 | { |
2533 | 0 | unsigned int addr_size = sizeof_address; |
2534 | 0 | struct line_seg *s; |
2535 | 0 | expressionS exp; |
2536 | 0 | unsigned int i; |
2537 | |
|
2538 | 0 | memset (&exp, 0, sizeof exp); |
2539 | 0 | subseg_set (ranges_seg, 0); |
2540 | | |
2541 | | /* For DW_AT_ranges to point at (there is no header, so really start |
2542 | | of section, but see out_debug_rnglists). */ |
2543 | 0 | *ranges_sym = symbol_temp_new_now_octets (); |
2544 | | |
2545 | | /* Base Address Entry. */ |
2546 | 0 | for (i = 0; i < addr_size; i++) |
2547 | 0 | out_byte (0xff); |
2548 | 0 | for (i = 0; i < addr_size; i++) |
2549 | 0 | out_byte (0); |
2550 | | |
2551 | | /* Range List Entry. */ |
2552 | 0 | for (s = all_segs; s; s = s->next) |
2553 | 0 | { |
2554 | 0 | fragS *frag; |
2555 | 0 | symbolS *beg, *end; |
2556 | |
|
2557 | 0 | frag = first_frag_for_seg (s->seg); |
2558 | 0 | beg = symbol_temp_new (s->seg, frag, 0); |
2559 | 0 | s->text_start = beg; |
2560 | |
|
2561 | 0 | frag = last_frag_for_seg (s->seg); |
2562 | 0 | end = symbol_temp_new (s->seg, frag, get_frag_fix (frag, s->seg)); |
2563 | 0 | s->text_end = end; |
2564 | |
|
2565 | 0 | exp.X_op = O_symbol; |
2566 | 0 | exp.X_add_symbol = beg; |
2567 | 0 | exp.X_add_number = 0; |
2568 | 0 | emit_expr (&exp, addr_size); |
2569 | |
|
2570 | 0 | exp.X_op = O_symbol; |
2571 | 0 | exp.X_add_symbol = end; |
2572 | 0 | exp.X_add_number = 0; |
2573 | 0 | emit_expr (&exp, addr_size); |
2574 | 0 | } |
2575 | | |
2576 | | /* End of Range Entry. */ |
2577 | 0 | for (i = 0; i < addr_size; i++) |
2578 | 0 | out_byte (0); |
2579 | 0 | for (i = 0; i < addr_size; i++) |
2580 | 0 | out_byte (0); |
2581 | 0 | } |
2582 | | |
2583 | | static void |
2584 | | out_debug_rnglists (segT ranges_seg, symbolS **ranges_sym) |
2585 | 0 | { |
2586 | 0 | expressionS exp; |
2587 | 0 | symbolS *ranges_end; |
2588 | 0 | struct line_seg *s; |
2589 | | |
2590 | | /* Unit length. */ |
2591 | 0 | memset (&exp, 0, sizeof exp); |
2592 | 0 | out_header (ranges_seg, &exp); |
2593 | 0 | ranges_end = exp.X_add_symbol; |
2594 | |
|
2595 | 0 | out_two (DWARF2_RNGLISTS_VERSION); |
2596 | 0 | out_byte (sizeof_address); |
2597 | 0 | out_byte (0); /* Segment Selector size. */ |
2598 | 0 | out_four (0); /* Offset entry count. */ |
2599 | | |
2600 | | /* For DW_AT_ranges to point at (must be after the header). */ |
2601 | 0 | *ranges_sym = symbol_temp_new_now_octets (); |
2602 | |
|
2603 | 0 | for (s = all_segs; s; s = s->next) |
2604 | 0 | { |
2605 | 0 | fragS *frag; |
2606 | 0 | symbolS *beg, *end; |
2607 | |
|
2608 | 0 | out_byte (DW_RLE_start_length); |
2609 | |
|
2610 | 0 | frag = first_frag_for_seg (s->seg); |
2611 | 0 | beg = symbol_temp_new (s->seg, frag, 0); |
2612 | 0 | s->text_start = beg; |
2613 | |
|
2614 | 0 | frag = last_frag_for_seg (s->seg); |
2615 | 0 | end = symbol_temp_new (s->seg, frag, get_frag_fix (frag, s->seg)); |
2616 | 0 | s->text_end = end; |
2617 | |
|
2618 | 0 | exp.X_op = O_symbol; |
2619 | 0 | exp.X_add_symbol = beg; |
2620 | 0 | exp.X_add_number = 0; |
2621 | 0 | emit_expr (&exp, sizeof_address); |
2622 | |
|
2623 | 0 | exp.X_op = O_symbol; |
2624 | 0 | exp.X_add_symbol = end; |
2625 | 0 | exp.X_add_number = 0; |
2626 | 0 | emit_leb128_expr (&exp, 0); |
2627 | 0 | } |
2628 | |
|
2629 | 0 | out_byte (DW_RLE_end_of_list); |
2630 | |
|
2631 | 0 | symbol_set_value_now (ranges_end); |
2632 | 0 | } |
2633 | | |
2634 | | /* Emit data for .debug_aranges. */ |
2635 | | |
2636 | | static void |
2637 | | out_debug_aranges (segT aranges_seg, segT info_seg) |
2638 | 11 | { |
2639 | 11 | unsigned int addr_size = sizeof_address; |
2640 | 11 | offsetT size; |
2641 | 11 | struct line_seg *s; |
2642 | 11 | expressionS exp; |
2643 | 11 | symbolS *aranges_end; |
2644 | 11 | char *p; |
2645 | 11 | int sizeof_offset; |
2646 | | |
2647 | 11 | memset (&exp, 0, sizeof exp); |
2648 | 11 | sizeof_offset = out_header (aranges_seg, &exp); |
2649 | 11 | aranges_end = exp.X_add_symbol; |
2650 | 11 | size = -exp.X_add_number; |
2651 | | |
2652 | | /* Version. */ |
2653 | 11 | out_two (DWARF2_ARANGES_VERSION); |
2654 | 11 | size += 2; |
2655 | | |
2656 | | /* Offset to .debug_info. */ |
2657 | 11 | TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset); |
2658 | 11 | size += sizeof_offset; |
2659 | | |
2660 | | /* Size of an address (offset portion). */ |
2661 | 11 | out_byte (addr_size); |
2662 | 11 | size++; |
2663 | | |
2664 | | /* Size of a segment descriptor. */ |
2665 | 11 | out_byte (0); |
2666 | 11 | size++; |
2667 | | |
2668 | | /* Align the header. */ |
2669 | 55 | while ((size++ % (2 * addr_size)) > 0) |
2670 | 44 | out_byte (0); |
2671 | | |
2672 | 22 | for (s = all_segs; s; s = s->next) |
2673 | 11 | { |
2674 | 11 | fragS *frag; |
2675 | 11 | symbolS *beg, *end; |
2676 | | |
2677 | 11 | frag = first_frag_for_seg (s->seg); |
2678 | 11 | beg = symbol_temp_new (s->seg, frag, 0); |
2679 | 11 | s->text_start = beg; |
2680 | | |
2681 | 11 | frag = last_frag_for_seg (s->seg); |
2682 | 11 | end = symbol_temp_new (s->seg, frag, get_frag_fix (frag, s->seg)); |
2683 | 11 | s->text_end = end; |
2684 | | |
2685 | 11 | exp.X_op = O_symbol; |
2686 | 11 | exp.X_add_symbol = beg; |
2687 | 11 | exp.X_add_number = 0; |
2688 | 11 | emit_expr (&exp, addr_size); |
2689 | | |
2690 | 11 | exp.X_op = O_subtract; |
2691 | 11 | exp.X_add_symbol = end; |
2692 | 11 | exp.X_op_symbol = beg; |
2693 | 11 | exp.X_add_number = 0; |
2694 | 11 | emit_expr (&exp, addr_size); |
2695 | 11 | } |
2696 | | |
2697 | 11 | p = frag_more (2 * addr_size); |
2698 | 11 | md_number_to_chars (p, 0, addr_size); |
2699 | 11 | md_number_to_chars (p + addr_size, 0, addr_size); |
2700 | | |
2701 | 11 | symbol_set_value_now (aranges_end); |
2702 | 11 | } |
2703 | | |
2704 | | /* Emit data for .debug_abbrev. Note that this must be kept in |
2705 | | sync with out_debug_info below. */ |
2706 | | |
2707 | | static void |
2708 | | out_debug_abbrev (segT abbrev_seg, |
2709 | | segT info_seg ATTRIBUTE_UNUSED, |
2710 | | segT line_seg ATTRIBUTE_UNUSED, |
2711 | | unsigned char *func_formP) |
2712 | 11 | { |
2713 | 11 | int secoff_form; |
2714 | 11 | bool have_efunc = false, have_lfunc = false; |
2715 | | |
2716 | | /* Check the symbol table for function symbols which also have their size |
2717 | | specified. */ |
2718 | 11 | if (symbol_rootP) |
2719 | 11 | { |
2720 | 11 | symbolS *symp; |
2721 | | |
2722 | 7.39k | for (symp = symbol_rootP; symp; symp = symbol_next (symp)) |
2723 | 7.37k | { |
2724 | | /* A warning construct is a warning symbol followed by the |
2725 | | symbol warned about. Skip this and the following symbol. */ |
2726 | 7.37k | if (symbol_get_bfdsym (symp)->flags & BSF_WARNING) |
2727 | 0 | { |
2728 | 0 | symp = symbol_next (symp); |
2729 | 0 | if (!symp) |
2730 | 0 | break; |
2731 | 0 | continue; |
2732 | 0 | } |
2733 | | |
2734 | 7.37k | if (!S_IS_DEFINED (symp) || !S_IS_FUNCTION (symp)) |
2735 | 7.37k | continue; |
2736 | | |
2737 | 0 | #if defined (OBJ_ELF) /* || defined (OBJ_MAYBE_ELF) */ |
2738 | 0 | if (S_GET_SIZE (symp) == 0) |
2739 | 0 | { |
2740 | 0 | if (!IS_ELF || symbol_get_obj (symp)->size == NULL) |
2741 | 0 | continue; |
2742 | 0 | } |
2743 | | #else |
2744 | | continue; |
2745 | | #endif |
2746 | | |
2747 | 0 | if (S_IS_EXTERNAL (symp)) |
2748 | 0 | have_efunc = true; |
2749 | 0 | else |
2750 | 0 | have_lfunc = true; |
2751 | 0 | } |
2752 | 11 | } |
2753 | | |
2754 | 11 | subseg_set (abbrev_seg, 0); |
2755 | | |
2756 | 11 | out_uleb128 (GAS_ABBREV_COMP_UNIT); |
2757 | 11 | out_uleb128 (DW_TAG_compile_unit); |
2758 | 11 | out_byte (have_efunc || have_lfunc ? DW_CHILDREN_yes : DW_CHILDREN_no); |
2759 | 11 | if (DWARF2_VERSION < 4) |
2760 | 0 | { |
2761 | 0 | if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit) |
2762 | 0 | secoff_form = DW_FORM_data4; |
2763 | 0 | else |
2764 | 0 | secoff_form = DW_FORM_data8; |
2765 | 0 | } |
2766 | 11 | else |
2767 | 11 | secoff_form = DW_FORM_sec_offset; |
2768 | 11 | out_abbrev (DW_AT_stmt_list, secoff_form); |
2769 | 11 | if (all_segs->next == NULL) |
2770 | 11 | { |
2771 | 11 | out_abbrev (DW_AT_low_pc, DW_FORM_addr); |
2772 | 11 | if (DWARF2_VERSION < 4) |
2773 | 0 | out_abbrev (DW_AT_high_pc, DW_FORM_addr); |
2774 | 11 | else |
2775 | 11 | out_abbrev (DW_AT_high_pc, DW_FORM_udata); |
2776 | 11 | } |
2777 | 0 | else |
2778 | 0 | out_abbrev (DW_AT_ranges, secoff_form); |
2779 | 11 | out_abbrev (DW_AT_name, DW_FORM_strp); |
2780 | 11 | out_abbrev (DW_AT_comp_dir, DW_FORM_strp); |
2781 | 11 | out_abbrev (DW_AT_producer, DW_FORM_strp); |
2782 | 11 | out_abbrev (DW_AT_language, DW_FORM_data2); |
2783 | 11 | out_abbrev (0, 0); |
2784 | | |
2785 | 11 | if (have_efunc || have_lfunc) |
2786 | 0 | { |
2787 | 0 | out_uleb128 (GAS_ABBREV_SUBPROG); |
2788 | 0 | out_uleb128 (DW_TAG_subprogram); |
2789 | 0 | out_byte (DW_CHILDREN_no); |
2790 | 0 | out_abbrev (DW_AT_name, DW_FORM_strp); |
2791 | 0 | if (have_efunc) |
2792 | 0 | { |
2793 | 0 | if (have_lfunc || DWARF2_VERSION < 4) |
2794 | 0 | *func_formP = DW_FORM_flag; |
2795 | 0 | else |
2796 | 0 | *func_formP = DW_FORM_flag_present; |
2797 | 0 | out_abbrev (DW_AT_external, *func_formP); |
2798 | 0 | } |
2799 | 0 | else |
2800 | | /* Any non-zero value other than DW_FORM_flag will do. */ |
2801 | 0 | *func_formP = DW_FORM_block; |
2802 | | |
2803 | | /* PR 29517: Provide a return type for the function. */ |
2804 | 0 | if (DWARF2_VERSION > 2) |
2805 | 0 | out_abbrev (DW_AT_type, DW_FORM_ref_udata); |
2806 | |
|
2807 | 0 | out_abbrev (DW_AT_low_pc, DW_FORM_addr); |
2808 | 0 | out_abbrev (DW_AT_high_pc, |
2809 | 0 | DWARF2_VERSION < 4 ? DW_FORM_addr : DW_FORM_udata); |
2810 | 0 | out_abbrev (0, 0); |
2811 | |
|
2812 | 0 | if (DWARF2_VERSION > 2) |
2813 | 0 | { |
2814 | | /* PR 29517: We do not actually know the return type of these |
2815 | | functions, so provide an abbrev that uses DWARF's unspecified |
2816 | | type. */ |
2817 | 0 | out_uleb128 (GAS_ABBREV_NO_TYPE); |
2818 | 0 | out_uleb128 (DW_TAG_unspecified_type); |
2819 | 0 | out_byte (DW_CHILDREN_no); |
2820 | 0 | out_abbrev (0, 0); |
2821 | 0 | } |
2822 | 0 | } |
2823 | | |
2824 | | /* Terminate the abbreviations for this compilation unit. */ |
2825 | 11 | out_byte (0); |
2826 | 11 | } |
2827 | | |
2828 | | /* Emit a description of this compilation unit for .debug_info. */ |
2829 | | |
2830 | | static void |
2831 | | out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT str_seg, |
2832 | | symbolS *ranges_sym, symbolS *name_sym, |
2833 | | symbolS *comp_dir_sym, symbolS *producer_sym, |
2834 | | unsigned char func_form) |
2835 | 11 | { |
2836 | 11 | expressionS exp; |
2837 | 11 | symbolS *info_end; |
2838 | 11 | int sizeof_offset; |
2839 | | |
2840 | 11 | memset (&exp, 0, sizeof exp); |
2841 | 11 | sizeof_offset = out_header (info_seg, &exp); |
2842 | 11 | info_end = exp.X_add_symbol; |
2843 | | |
2844 | | /* DWARF version. */ |
2845 | 11 | out_two (DWARF2_VERSION); |
2846 | | |
2847 | 11 | if (DWARF2_VERSION < 5) |
2848 | 0 | { |
2849 | | /* .debug_abbrev offset */ |
2850 | 0 | TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset); |
2851 | 0 | } |
2852 | 11 | else |
2853 | 11 | { |
2854 | | /* unit (header) type */ |
2855 | 11 | out_byte (DW_UT_compile); |
2856 | 11 | } |
2857 | | |
2858 | | /* Target address size. */ |
2859 | 11 | out_byte (sizeof_address); |
2860 | | |
2861 | 11 | if (DWARF2_VERSION >= 5) |
2862 | 11 | { |
2863 | | /* .debug_abbrev offset */ |
2864 | 11 | TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset); |
2865 | 11 | } |
2866 | | |
2867 | | /* DW_TAG_compile_unit DIE abbrev */ |
2868 | 11 | out_uleb128 (GAS_ABBREV_COMP_UNIT); |
2869 | | |
2870 | | /* DW_AT_stmt_list */ |
2871 | 11 | TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), |
2872 | 11 | (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit |
2873 | 11 | ? 4 : 8)); |
2874 | | |
2875 | | /* These two attributes are emitted if all of the code is contiguous. */ |
2876 | 11 | if (all_segs->next == NULL) |
2877 | 11 | { |
2878 | | /* DW_AT_low_pc */ |
2879 | 11 | exp.X_op = O_symbol; |
2880 | 11 | exp.X_add_symbol = all_segs->text_start; |
2881 | 11 | exp.X_add_number = 0; |
2882 | 11 | emit_expr (&exp, sizeof_address); |
2883 | | |
2884 | | /* DW_AT_high_pc */ |
2885 | 11 | if (DWARF2_VERSION < 4) |
2886 | 0 | exp.X_op = O_symbol; |
2887 | 11 | else |
2888 | 11 | { |
2889 | 11 | exp.X_op = O_subtract; |
2890 | 11 | exp.X_op_symbol = all_segs->text_start; |
2891 | 11 | } |
2892 | 11 | exp.X_add_symbol = all_segs->text_end; |
2893 | 11 | exp.X_add_number = 0; |
2894 | 11 | if (DWARF2_VERSION < 4) |
2895 | 0 | emit_expr (&exp, sizeof_address); |
2896 | 11 | else |
2897 | 11 | emit_leb128_expr (&exp, 0); |
2898 | 11 | } |
2899 | 0 | else |
2900 | 0 | { |
2901 | | /* This attribute is emitted if the code is disjoint. */ |
2902 | | /* DW_AT_ranges. */ |
2903 | 0 | TC_DWARF2_EMIT_OFFSET (ranges_sym, sizeof_offset); |
2904 | 0 | } |
2905 | | |
2906 | | /* DW_AT_name, DW_AT_comp_dir and DW_AT_producer. Symbols in .debug_str |
2907 | | setup in out_debug_str below. */ |
2908 | 11 | TC_DWARF2_EMIT_OFFSET (name_sym, sizeof_offset); |
2909 | 11 | TC_DWARF2_EMIT_OFFSET (comp_dir_sym, sizeof_offset); |
2910 | 11 | TC_DWARF2_EMIT_OFFSET (producer_sym, sizeof_offset); |
2911 | | |
2912 | | /* DW_AT_language. Yes, this is probably not really MIPS, but the |
2913 | | dwarf2 draft has no standard code for assembler. */ |
2914 | 11 | out_two (DW_LANG_Mips_Assembler); |
2915 | | |
2916 | 11 | if (func_form) |
2917 | 0 | { |
2918 | 0 | symbolS *symp; |
2919 | 0 | symbolS *no_type_tag; |
2920 | |
|
2921 | 0 | if (DWARF2_VERSION > 2) |
2922 | 0 | no_type_tag = symbol_make (".Ldebug_no_type_tag"); |
2923 | 0 | else |
2924 | 0 | no_type_tag = NULL; |
2925 | |
|
2926 | 0 | for (symp = symbol_rootP; symp; symp = symbol_next (symp)) |
2927 | 0 | { |
2928 | 0 | const char *name; |
2929 | 0 | size_t len; |
2930 | 0 | expressionS size = { .X_op = O_constant }; |
2931 | | |
2932 | | /* Skip warning constructs (see above). */ |
2933 | 0 | if (symbol_get_bfdsym (symp)->flags & BSF_WARNING) |
2934 | 0 | { |
2935 | 0 | symp = symbol_next (symp); |
2936 | 0 | if (!symp) |
2937 | 0 | break; |
2938 | 0 | continue; |
2939 | 0 | } |
2940 | | |
2941 | 0 | if (!S_IS_DEFINED (symp) || !S_IS_FUNCTION (symp)) |
2942 | 0 | continue; |
2943 | | |
2944 | 0 | #if defined (OBJ_ELF) /* || defined (OBJ_MAYBE_ELF) */ |
2945 | 0 | size.X_add_number = S_GET_SIZE (symp); |
2946 | 0 | if (size.X_add_number == 0 && IS_ELF |
2947 | 0 | && symbol_get_obj (symp)->size != NULL) |
2948 | 0 | { |
2949 | 0 | size.X_op = O_add; |
2950 | 0 | size.X_op_symbol = make_expr_symbol (symbol_get_obj (symp)->size); |
2951 | 0 | } |
2952 | 0 | #endif |
2953 | 0 | if (size.X_op == O_constant && size.X_add_number == 0) |
2954 | 0 | continue; |
2955 | | |
2956 | 0 | subseg_set (str_seg, 0); |
2957 | 0 | name_sym = symbol_temp_new_now_octets (); |
2958 | 0 | name = S_GET_NAME (symp); |
2959 | 0 | len = strlen (name) + 1; |
2960 | 0 | memcpy (frag_more (len), name, len); |
2961 | |
|
2962 | 0 | subseg_set (info_seg, 0); |
2963 | | |
2964 | | /* DW_TAG_subprogram DIE abbrev */ |
2965 | 0 | out_uleb128 (GAS_ABBREV_SUBPROG); |
2966 | | |
2967 | | /* DW_AT_name */ |
2968 | 0 | TC_DWARF2_EMIT_OFFSET (name_sym, sizeof_offset); |
2969 | | |
2970 | | /* DW_AT_external. */ |
2971 | 0 | if (func_form == DW_FORM_flag) |
2972 | 0 | out_byte (S_IS_EXTERNAL (symp)); |
2973 | | |
2974 | | /* PR 29517: Let consumers know that we do not have |
2975 | | return type information for this function. */ |
2976 | 0 | if (DWARF2_VERSION > 2) |
2977 | 0 | { |
2978 | 0 | exp.X_op = O_symbol; |
2979 | 0 | exp.X_add_symbol = no_type_tag; |
2980 | 0 | exp.X_add_number = 0; |
2981 | 0 | emit_leb128_expr (&exp, 0); |
2982 | 0 | } |
2983 | | |
2984 | | /* DW_AT_low_pc */ |
2985 | 0 | exp.X_op = O_symbol; |
2986 | 0 | exp.X_add_symbol = symp; |
2987 | 0 | exp.X_add_number = 0; |
2988 | 0 | emit_expr (&exp, sizeof_address); |
2989 | | |
2990 | | /* DW_AT_high_pc */ |
2991 | 0 | if (DWARF2_VERSION < 4) |
2992 | 0 | { |
2993 | 0 | if (size.X_op == O_constant) |
2994 | 0 | size.X_op = O_symbol; |
2995 | 0 | size.X_add_symbol = symp; |
2996 | 0 | emit_expr (&size, sizeof_address); |
2997 | 0 | } |
2998 | 0 | else if (size.X_op == O_constant) |
2999 | 0 | out_uleb128 (size.X_add_number); |
3000 | 0 | else |
3001 | 0 | emit_leb128_expr (symbol_get_value_expression (size.X_op_symbol), 0); |
3002 | 0 | } |
3003 | |
|
3004 | 0 | if (DWARF2_VERSION > 2) |
3005 | 0 | { |
3006 | | /* PR 29517: Generate a DIE for the unspecified type abbrev. |
3007 | | We do it here because it cannot be part of the top level DIE. */ |
3008 | 0 | subseg_set (info_seg, 0); |
3009 | 0 | symbol_set_value_now (no_type_tag); |
3010 | 0 | out_uleb128 (GAS_ABBREV_NO_TYPE); |
3011 | 0 | } |
3012 | | |
3013 | | /* End of children. */ |
3014 | 0 | out_leb128 (0); |
3015 | 0 | } |
3016 | | |
3017 | 11 | symbol_set_value_now (info_end); |
3018 | 11 | } |
3019 | | |
3020 | | /* Emit the three debug strings needed in .debug_str and setup symbols |
3021 | | to them for use in out_debug_info. */ |
3022 | | static void |
3023 | | out_debug_str (segT str_seg, symbolS **name_sym, symbolS **comp_dir_sym, |
3024 | | symbolS **producer_sym) |
3025 | 11 | { |
3026 | 11 | char producer[128]; |
3027 | 11 | char *p; |
3028 | 11 | int len; |
3029 | 11 | int first_file = DWARF2_LINE_VERSION > 4 ? 0 : 1; |
3030 | | |
3031 | 11 | if (files_in_use == 0) |
3032 | 0 | abort (); |
3033 | 11 | if (first_file == 0 && files[first_file].filename == NULL) |
3034 | 0 | first_file = 1; |
3035 | | |
3036 | 11 | subseg_set (str_seg, 0); |
3037 | | |
3038 | | /* DW_AT_name. We don't have the actual file name that was present |
3039 | | on the command line, so assume files[first_file] is the main input file. |
3040 | | We're not supposed to get called unless at least one line number |
3041 | | entry was emitted, so this should always be defined. */ |
3042 | 11 | *name_sym = symbol_temp_new_now_octets (); |
3043 | | |
3044 | 11 | if (files[first_file].dir) |
3045 | 1 | { |
3046 | 1 | char *dirname = remap_debug_filename (dirs[files[first_file].dir]); |
3047 | 1 | len = strlen (dirname); |
3048 | | #ifdef TE_VMS |
3049 | | /* Already has trailing slash. */ |
3050 | | p = frag_more (len); |
3051 | | memcpy (p, dirname, len); |
3052 | | #else |
3053 | 1 | p = frag_more (len + 1); |
3054 | 1 | memcpy (p, dirname, len); |
3055 | 1 | INSERT_DIR_SEPARATOR (p, len); |
3056 | 1 | #endif |
3057 | 1 | free (dirname); |
3058 | 1 | } |
3059 | 11 | len = strlen (files[first_file].filename) + 1; |
3060 | 11 | p = frag_more (len); |
3061 | 11 | memcpy (p, files[first_file].filename, len); |
3062 | | |
3063 | | /* DW_AT_comp_dir */ |
3064 | 11 | *comp_dir_sym = symbol_temp_new_now_octets (); |
3065 | 11 | char *comp_dir = remap_debug_filename (getpwd ()); |
3066 | 11 | len = strlen (comp_dir) + 1; |
3067 | 11 | p = frag_more (len); |
3068 | 11 | memcpy (p, comp_dir, len); |
3069 | 11 | free (comp_dir); |
3070 | | |
3071 | | /* DW_AT_producer */ |
3072 | 11 | *producer_sym = symbol_temp_new_now_octets (); |
3073 | 11 | sprintf (producer, "GNU AS %s", VERSION); |
3074 | 11 | len = strlen (producer) + 1; |
3075 | 11 | p = frag_more (len); |
3076 | 11 | memcpy (p, producer, len); |
3077 | 11 | } |
3078 | | |
3079 | | void |
3080 | | dwarf2_init (void) |
3081 | 28 | { |
3082 | 28 | all_segs = NULL; |
3083 | 28 | last_seg_ptr = &all_segs; |
3084 | 28 | files = NULL; |
3085 | 28 | files_in_use = 0; |
3086 | 28 | files_allocated = 0; |
3087 | 28 | dirs = NULL; |
3088 | 28 | dirs_in_use = 0; |
3089 | 28 | dirs_allocated = 0; |
3090 | 28 | dwarf2_loc_directive_seen = false; |
3091 | 28 | dwarf2_any_loc_directive_seen = false; |
3092 | 28 | dwarf2_loc_mark_labels = false; |
3093 | 28 | current.filenum = 1; |
3094 | 28 | current.line = 1; |
3095 | 28 | current.column = 0; |
3096 | 28 | current.isa = 0; |
3097 | 28 | current.flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
3098 | 28 | current.discriminator = 0; |
3099 | 28 | current.u.view = NULL; |
3100 | 28 | force_reset_view = NULL; |
3101 | 28 | view_assert_failed = NULL; |
3102 | 28 | dw2_line = -1; |
3103 | 28 | dw2_filename = NULL; |
3104 | 28 | label_num = 0; |
3105 | 28 | last_used = -1; |
3106 | | |
3107 | | /* Select the default CIE version to produce here. The global |
3108 | | starts with a value of -1 and will be modified to a valid value |
3109 | | either by the user providing a command line option, or some |
3110 | | targets will select their own default in md_after_parse_args. If |
3111 | | we get here and the global still contains -1 then it is up to us |
3112 | | to pick a sane default. The default we choose is 1, this is the |
3113 | | CIE version gas has produced for a long time, and there seems no |
3114 | | reason to change it yet. */ |
3115 | 28 | if (flag_dwarf_cie_version == -1) |
3116 | 1 | flag_dwarf_cie_version = 1; |
3117 | 28 | } |
3118 | | |
3119 | | static void |
3120 | | dwarf2_cleanup (void) |
3121 | 28 | { |
3122 | 28 | purge_generated_debug (true); |
3123 | 28 | free (files); |
3124 | 69 | for (unsigned int i = 0; i < dirs_in_use; i++) |
3125 | 41 | free (dirs[i]); |
3126 | 28 | free (dirs); |
3127 | 28 | } |
3128 | | |
3129 | | /* Finish the dwarf2 debug sections. We emit .debug.line if there |
3130 | | were any .file/.loc directives, or --gdwarf2 was given, and if the |
3131 | | file has a non-empty .debug_info section and an empty .debug_line |
3132 | | section. If we emit .debug_line, and the .debug_info section is |
3133 | | empty, we also emit .debug_info, .debug_aranges and .debug_abbrev. |
3134 | | ALL_SEGS will be non-null if there were any .file/.loc directives, |
3135 | | or --gdwarf2 was given and there were any located instructions |
3136 | | emitted. */ |
3137 | | |
3138 | | void |
3139 | | dwarf2_finish (void) |
3140 | 28 | { |
3141 | 28 | segT line_seg; |
3142 | 28 | struct line_seg *s; |
3143 | 28 | segT info_seg; |
3144 | 28 | int emit_other_sections = 0; |
3145 | 28 | int empty_debug_line = 0; |
3146 | | |
3147 | 28 | info_seg = bfd_get_section_by_name (stdoutput, ".debug_info"); |
3148 | 28 | emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg); |
3149 | | |
3150 | 28 | line_seg = bfd_get_section_by_name (stdoutput, ".debug_line"); |
3151 | 28 | empty_debug_line = line_seg == NULL || !seg_not_empty_p (line_seg); |
3152 | | |
3153 | | /* We can't construct a new debug_line section if we already have one. |
3154 | | Give an error if we have seen any .loc, otherwise trust the user |
3155 | | knows what they are doing and want to generate the .debug_line |
3156 | | (and all other debug sections) themselves. */ |
3157 | 28 | if (all_segs && !empty_debug_line && dwarf2_any_loc_directive_seen) |
3158 | 0 | as_fatal ("duplicate .debug_line sections"); |
3159 | | |
3160 | 28 | if ((!all_segs && emit_other_sections) |
3161 | 28 | || (!emit_other_sections && !empty_debug_line)) |
3162 | | /* If there is no line information and no non-empty .debug_info |
3163 | | section, or if there is both a non-empty .debug_info and a non-empty |
3164 | | .debug_line, then we do nothing. */ |
3165 | 16 | { |
3166 | 16 | dwarf2_cleanup (); |
3167 | 16 | return; |
3168 | 16 | } |
3169 | | |
3170 | | /* Calculate the size of an address for the target machine. */ |
3171 | 12 | sizeof_address = DWARF2_ADDR_SIZE (stdoutput); |
3172 | | |
3173 | | /* Create and switch to the line number section. */ |
3174 | 12 | if (empty_debug_line) |
3175 | 12 | { |
3176 | 12 | line_seg = subseg_new (".debug_line", 0); |
3177 | 12 | bfd_set_section_flags (line_seg, |
3178 | 12 | SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS); |
3179 | 12 | } |
3180 | | |
3181 | 24 | for (s = all_segs; s; s = s->next) |
3182 | 12 | { |
3183 | 12 | struct line_subseg *lss; |
3184 | | |
3185 | 32 | for (lss = s->head; lss; lss = lss->next) |
3186 | 20 | if (lss->head) |
3187 | 20 | do_allocate_filenum (lss->head); |
3188 | 12 | } |
3189 | | |
3190 | | /* For each subsection, chain the debug entries together. */ |
3191 | 24 | for (s = all_segs; s; s = s->next) |
3192 | 12 | { |
3193 | 12 | struct line_subseg *lss = s->head; |
3194 | 12 | struct line_entry **ptail = lss->ptail; |
3195 | | |
3196 | | /* Reset the initial view of the first subsection of the |
3197 | | section. */ |
3198 | 12 | if (lss->head && lss->head->loc.u.view) |
3199 | 0 | set_or_check_view (lss->head, NULL, NULL); |
3200 | | |
3201 | 20 | while ((lss = lss->next) != NULL) |
3202 | 8 | { |
3203 | | /* Link the first view of subsequent subsections to the |
3204 | | previous view. */ |
3205 | 8 | if (lss->head && lss->head->loc.u.view) |
3206 | 0 | set_or_check_view (lss->head, line_entry_at_tail (s->head, ptail), |
3207 | 0 | s->head ? s->head->head : NULL); |
3208 | 8 | *ptail = lss->head; |
3209 | 8 | lss->head = NULL; |
3210 | 8 | ptail = lss->ptail; |
3211 | 8 | } |
3212 | 12 | } |
3213 | | |
3214 | 12 | if (empty_debug_line) |
3215 | 12 | out_debug_line (line_seg); |
3216 | | |
3217 | | /* If this is assembler generated line info, and there is no |
3218 | | debug_info already, we need .debug_info, .debug_abbrev and |
3219 | | .debug_str sections as well. */ |
3220 | 12 | if (emit_other_sections) |
3221 | 11 | { |
3222 | 11 | segT abbrev_seg; |
3223 | 11 | segT aranges_seg; |
3224 | 11 | segT str_seg; |
3225 | 11 | symbolS *name_sym, *comp_dir_sym, *producer_sym, *ranges_sym; |
3226 | 11 | unsigned char func_form = 0; |
3227 | | |
3228 | 11 | gas_assert (all_segs); |
3229 | | |
3230 | 11 | info_seg = subseg_new (".debug_info", 0); |
3231 | 11 | abbrev_seg = subseg_new (".debug_abbrev", 0); |
3232 | 11 | aranges_seg = subseg_new (".debug_aranges", 0); |
3233 | 11 | str_seg = subseg_new (".debug_str", 0); |
3234 | | |
3235 | 11 | bfd_set_section_flags (info_seg, |
3236 | 11 | SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS); |
3237 | 11 | bfd_set_section_flags (abbrev_seg, |
3238 | 11 | SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS); |
3239 | 11 | bfd_set_section_flags (aranges_seg, |
3240 | 11 | SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS); |
3241 | 11 | bfd_set_section_flags (str_seg, |
3242 | 11 | SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS |
3243 | 11 | | SEC_MERGE | SEC_STRINGS); |
3244 | 11 | str_seg->entsize = 1; |
3245 | | |
3246 | 11 | record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1); |
3247 | | |
3248 | 11 | if (all_segs->next == NULL) |
3249 | 11 | ranges_sym = NULL; |
3250 | 0 | else |
3251 | 0 | { |
3252 | 0 | if (DWARF2_VERSION < 5) |
3253 | 0 | { |
3254 | 0 | segT ranges_seg = subseg_new (".debug_ranges", 0); |
3255 | 0 | bfd_set_section_flags (ranges_seg, (SEC_READONLY |
3256 | 0 | | SEC_DEBUGGING |
3257 | 0 | | SEC_OCTETS)); |
3258 | 0 | record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1); |
3259 | 0 | out_debug_ranges (ranges_seg, &ranges_sym); |
3260 | 0 | } |
3261 | 0 | else |
3262 | 0 | { |
3263 | 0 | segT rnglists_seg = subseg_new (".debug_rnglists", 0); |
3264 | 0 | bfd_set_section_flags (rnglists_seg, (SEC_READONLY |
3265 | 0 | | SEC_DEBUGGING |
3266 | 0 | | SEC_OCTETS)); |
3267 | 0 | out_debug_rnglists (rnglists_seg, &ranges_sym); |
3268 | 0 | } |
3269 | 0 | } |
3270 | | |
3271 | 11 | out_debug_aranges (aranges_seg, info_seg); |
3272 | 11 | out_debug_abbrev (abbrev_seg, info_seg, line_seg, &func_form); |
3273 | 11 | out_debug_str (str_seg, &name_sym, &comp_dir_sym, &producer_sym); |
3274 | 11 | out_debug_info (info_seg, abbrev_seg, line_seg, str_seg, |
3275 | 11 | ranges_sym, name_sym, comp_dir_sym, producer_sym, |
3276 | 11 | func_form); |
3277 | 11 | } |
3278 | 12 | dwarf2_cleanup (); |
3279 | 12 | } |
3280 | | |
3281 | | /* Perform any deferred checks pertaining to debug information. */ |
3282 | | |
3283 | | void |
3284 | | dwarf2dbg_final_check (void) |
3285 | 0 | { |
3286 | | /* Perform reset-view checks. Don't evaluate view_assert_failed |
3287 | | recursively: it could be very deep. It's a chain of adds, with |
3288 | | each chain element pointing to the next in X_add_symbol, and |
3289 | | holding the check value in X_op_symbol. */ |
3290 | 0 | while (view_assert_failed) |
3291 | 0 | { |
3292 | 0 | expressionS *exp; |
3293 | 0 | symbolS *sym; |
3294 | 0 | offsetT failed; |
3295 | |
|
3296 | 0 | gas_assert (!symbol_resolved_p (view_assert_failed)); |
3297 | | |
3298 | 0 | exp = symbol_get_value_expression (view_assert_failed); |
3299 | 0 | sym = view_assert_failed; |
3300 | | |
3301 | | /* If view_assert_failed looks like a compound check in the |
3302 | | chain, break it up. */ |
3303 | 0 | if (exp->X_op == O_add && exp->X_add_number == 0 && exp->X_unsigned) |
3304 | 0 | { |
3305 | 0 | view_assert_failed = exp->X_add_symbol; |
3306 | 0 | sym = exp->X_op_symbol; |
3307 | 0 | } |
3308 | 0 | else |
3309 | 0 | view_assert_failed = NULL; |
3310 | |
|
3311 | 0 | failed = resolve_symbol_value (sym); |
3312 | 0 | if (!symbol_resolved_p (sym) || failed) |
3313 | 0 | { |
3314 | 0 | as_bad (_("view number mismatch")); |
3315 | 0 | break; |
3316 | 0 | } |
3317 | 0 | } |
3318 | 0 | } |