1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright (C) 1999-2014 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]
29 */
30
31 #include "as.h"
32 #include "safe-ctype.h"
33
34 #ifdef HAVE_LIMITS_H
35 #include <limits.h>
36 #else
37 #ifdef HAVE_SYS_PARAM_H
38 #include <sys/param.h>
39 #endif
40 #ifndef INT_MAX
41 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
42 #endif
43 #endif
44
45 #include "dwarf2dbg.h"
46 #include <filenames.h>
47 #include <stdint.h>
48
49 #include "hash.h"
50
51 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
52 /* We need to decide which character to use as a directory separator.
53 Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not
54 necessarily mean that the backslash character is the one to use.
55 Some environments, eg Cygwin, can support both naming conventions.
56 So we use the heuristic that we only need to use the backslash if
57 the path is an absolute path starting with a DOS style drive
58 selector. eg C: or D: */
59 # define INSERT_DIR_SEPARATOR(string, offset) \
60 do \
61 { \
62 if (offset > 1 \
63 && string[0] != 0 \
64 && string[1] == ':') \
65 string [offset] = '\\'; \
66 else \
67 string [offset] = '/'; \
68 } \
69 while (0)
70 #else
71 # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
72 #endif
73
74 #ifndef DWARF2_FORMAT
75 # define DWARF2_FORMAT(SEC) dwarf2_format_32bit
76 #endif
77
78 #ifndef DWARF2_ADDR_SIZE
79 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
80 #endif
81
82 #ifndef DWARF2_FILE_NAME
83 #define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME
84 #endif
85
86 #ifndef DWARF2_FILE_TIME_NAME
87 #define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) 0
88 #endif
89
90 #ifndef DWARF2_FILE_SIZE_NAME
91 #define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) 0
92 #endif
93
94 #ifndef DWARF2_VERSION
95 #define DWARF2_VERSION 2
96 #endif
97
98 /* The .debug_aranges version has been 2 in DWARF version 2, 3 and 4. */
99 #ifndef DWARF2_ARANGES_VERSION
100 #define DWARF2_ARANGES_VERSION 2
101 #endif
102
103 /* This implementation output version 2 .debug_line information. */
104 #ifndef DWARF2_LINE_VERSION
105 #define DWARF2_LINE_VERSION 2
106 #endif
107 /* If we see .lloc directives, generate an experimental version 6. */
108 #ifndef DWARF2_LINE_EXPERIMENTAL_VERSION
109 #define DWARF2_LINE_EXPERIMENTAL_VERSION 0xf006
110 #endif
111
112 #include "subsegs.h"
113
114 #include "dwarf2.h"
115
116 /* Since we can't generate the prolog until the body is complete, we
117 use three different subsegments for .debug_line: one holding the
118 prolog, one for the directory and filename info, and one for the
119 body ("statement program"). */
120 #define DL_PROLOG 0
121 #define DL_FILES 1
122 #define DL_BODY 2
123
124 /* If linker relaxation might change offsets in the code, the DWARF special
125 opcodes and variable-length operands cannot be used. If this macro is
126 nonzero, use the DW_LNS_fixed_advance_pc opcode instead. */
127 #ifndef DWARF2_USE_FIXED_ADVANCE_PC
128 # define DWARF2_USE_FIXED_ADVANCE_PC linkrelax
129 #endif
130
131 /* First special line opcde - leave room for the standard opcodes.
132 Note: If you want to change this, you'll have to update the
133 "standard_opcode_lengths" table that is emitted below in
134 out_debug_line(). */
135 #define DWARF2_LINE_OPCODE_BASE 13
136 #define DWARF5_EXPERIMENTAL_LINE_OPCODE_BASE 16
137
138 static int opcode_base;
139 static int line_base;
140 static unsigned int line_range;
141
142 #ifndef DWARF2_LINE_BASE
143 /* Minimum line offset in a special line info. opcode. This value
144 was chosen to give a reasonable range of values. */
145 # define DWARF2_LINE_BASE -5
146 #endif
147
148 /* Range of line offsets in a special line info. opcode. */
149 #ifndef DWARF2_LINE_RANGE
150 # define DWARF2_LINE_RANGE 14
151 #endif
152
153 /* For two-level line tables, these values work a bit better. */
154 #define DWARF5_EXPERIMENTAL_LINE_BASE -3
155 #define DWARF5_EXPERIMENTAL_LINE_RANGE 10
156
157 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
158 /* Define the architecture-dependent minimum instruction length (in
159 bytes). This value should be rather too small than too big. */
160 # define DWARF2_LINE_MIN_INSN_LENGTH 1
161 #endif
162
163 #ifndef DWARF2_LINE_MAX_OPS_PER_INSN
164 # define DWARF2_LINE_MAX_OPS_PER_INSN 1
165 #endif
166
167 /* Flag that indicates the initial value of the is_stmt_start flag. */
168 #define DWARF2_LINE_DEFAULT_IS_STMT 1
169
170 /* Given a special op, return the line skip amount. */
171 #define SPECIAL_LINE(op) \
172 (((op) - opcode_base) % line_range + line_base)
173
174 /* Given a special op, return the address skip amount (in units of
175 DWARF2_LINE_MIN_INSN_LENGTH. */
176 #define SPECIAL_ADDR(op) (((op) - opcode_base) / line_range)
177
178 /* The maximum address skip amount that can be encoded with a special op. */
179 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
180
181 #ifndef TC_PARSE_CONS_RETURN_NONE
182 #define TC_PARSE_CONS_RETURN_NONE BFD_RELOC_NONE
183 #endif
184
185 struct line_entry {
186 struct line_entry *next;
187 symbolS *label;
188 struct dwarf2_line_info loc;
189 };
190
191 struct line_subseg {
192 struct line_subseg *next;
193 subsegT subseg;
194 struct line_entry *head;
195 struct line_entry **ptail;
196 struct line_entry **pmove_tail;
197 };
198
199 struct line_seg {
200 struct line_seg *next;
201 segT seg;
202 struct line_subseg *head;
203 symbolS *text_start;
204 symbolS *text_end;
205 };
206
207 /* Collects data for all line table entries during assembly. */
208 static struct line_seg *all_segs;
209 static struct line_seg **last_seg_ptr;
210
211 struct file_entry {
212 const char *filename;
213 unsigned int dir;
214 };
215
216 /* Table of files used by .debug_line. */
217 static struct file_entry *files;
218 static unsigned int files_in_use;
219 static unsigned int files_allocated;
220
221 /* Table of directories used by .debug_line. */
222 static char **dirs;
223 static unsigned int dirs_in_use;
224 static unsigned int dirs_allocated;
225
226 /* Experimental DWARF-5 Extension: Table of subprograms. */
227 struct subprog_entry {
228 const char *subpname;
229 unsigned int filenum;
230 unsigned int line;
231 };
232
233 static struct subprog_entry *subprogs;
234 static unsigned int subprogs_in_use;
235 static unsigned int subprogs_allocated;
236
237 /* Experimental DWARF-5 Extension: Logicals table. */
238 struct logicals_entry {
239 segT seg;
240 symbolS *label;
241 /* A logical row doesn't use every field in this struct, but using it
242 here makes the code for writing the line number program simpler. */
243 struct dwarf2_line_info loc;
244 unsigned int context;
245 unsigned int subprog;
246 };
247
248 static struct logicals_entry *logicals;
249 static unsigned int logicals_in_use;
250 static unsigned int logicals_allocated = 0;
251 static unsigned int logicals_with_labels = 0;
252
253 /* DWARF-5: .debug_line_str string table. */
254 struct string_table {
255 struct hash_control *hashtab;
256 const char **strings;
257 unsigned int strings_in_use;
258 unsigned int strings_allocated;
259 offsetT next_offset;
260 };
261
262 static struct string_table debug_line_str_table;
263
264 /* TRUE when we've seen a .loc directive recently. Used to avoid
265 doing work when there's nothing to do. */
266 bfd_boolean dwarf2_loc_directive_seen;
267
268 /* TRUE when we're supposed to set the basic block mark whenever a
269 label is seen. */
270 bfd_boolean dwarf2_loc_mark_labels;
271
272 /* Current location as indicated by the most recent .loc directive. */
273 static struct dwarf2_line_info current = {
274 1, 1, 0, 0, /* filenum, line, column, isa */
275 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, /* flags */
276 0, 0 /* discriminator, logical */
277 };
278
279 /* The size of an address on the target. */
280 static unsigned int sizeof_address;
281
282 static unsigned int get_filenum (const char *, unsigned int);
283
284 #ifndef TC_DWARF2_EMIT_OFFSET
285 #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
286
287 /* Create an offset to .dwarf2_*. */
288
289 static void
generic_dwarf2_emit_offset(symbolS * symbol,unsigned int size)290 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
291 {
292 expressionS exp;
293
294 exp.X_op = O_symbol;
295 exp.X_add_symbol = symbol;
296 exp.X_add_number = 0;
297 emit_expr (&exp, size);
298 }
299 #endif
300
301 /* Find or create (if CREATE_P) an entry for SEG+SUBSEG in ALL_SEGS. */
302
303 static struct line_subseg *
get_line_subseg(segT seg,subsegT subseg,bfd_boolean create_p)304 get_line_subseg (segT seg, subsegT subseg, bfd_boolean create_p)
305 {
306 struct line_seg *s = seg_info (seg)->dwarf2_line_seg;
307 struct line_subseg **pss, *lss;
308
309 if (s == NULL)
310 {
311 if (!create_p)
312 return NULL;
313
314 s = (struct line_seg *) xmalloc (sizeof (*s));
315 s->next = NULL;
316 s->seg = seg;
317 s->head = NULL;
318 *last_seg_ptr = s;
319 last_seg_ptr = &s->next;
320 seg_info (seg)->dwarf2_line_seg = s;
321 }
322 gas_assert (seg == s->seg);
323
324 for (pss = &s->head; (lss = *pss) != NULL ; pss = &lss->next)
325 {
326 if (lss->subseg == subseg)
327 goto found_subseg;
328 if (lss->subseg > subseg)
329 break;
330 }
331
332 lss = (struct line_subseg *) xmalloc (sizeof (*lss));
333 lss->next = *pss;
334 lss->subseg = subseg;
335 lss->head = NULL;
336 lss->ptail = &lss->head;
337 lss->pmove_tail = &lss->head;
338 *pss = lss;
339
340 found_subseg:
341 return lss;
342 }
343
344 /* Record an entry for LOC occurring at LABEL. */
345
346 static void
dwarf2_gen_line_info_1(symbolS * label,struct dwarf2_line_info * loc)347 dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
348 {
349 struct line_subseg *lss;
350 struct line_entry *e;
351
352 e = (struct line_entry *) xmalloc (sizeof (*e));
353 e->next = NULL;
354 e->label = label;
355 e->loc = *loc;
356
357 lss = get_line_subseg (now_seg, now_subseg, TRUE);
358 *lss->ptail = e;
359 lss->ptail = &e->next;
360 }
361
362 /* Record an entry for LOC occurring at OFS within the current fragment. */
363
364 void
dwarf2_gen_line_info(addressT ofs,struct dwarf2_line_info * loc)365 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
366 {
367 static unsigned int line = -1;
368 static unsigned int filenum = -1;
369
370 symbolS *sym;
371
372 /* Early out for as-yet incomplete location information. */
373 if (loc->filenum == 0 || loc->line == 0)
374 return;
375
376 /* Don't emit sequences of line symbols for the same line when the
377 symbols apply to assembler code. It is necessary to emit
378 duplicate line symbols when a compiler asks for them, because GDB
379 uses them to determine the end of the prologue. */
380 if (debug_type == DEBUG_DWARF2
381 && line == loc->line && filenum == loc->filenum)
382 return;
383
384 line = loc->line;
385 filenum = loc->filenum;
386
387 if (linkrelax)
388 {
389 char name[120];
390
391 /* Use a non-fake name for the line number location,
392 so that it can be referred to by relocations. */
393 sprintf (name, ".Loc.%u.%u", line, filenum);
394 sym = symbol_new (name, now_seg, ofs, frag_now);
395 }
396 else
397 sym = symbol_temp_new (now_seg, ofs, frag_now);
398 dwarf2_gen_line_info_1 (sym, loc);
399
400 /* Record the current symbol with all logical rows created since
401 the last emitted instruction. */
402 while (logicals_with_labels < logicals_in_use)
403 {
404 logicals[logicals_with_labels].label = sym;
405 logicals[logicals_with_labels].seg = now_seg;
406 logicals_with_labels++;
407 }
408 }
409
410 /* Returns the current source information. If .file directives have
411 been encountered, the info for the corresponding source file is
412 returned. Otherwise, the info for the assembly source file is
413 returned. */
414
415 void
dwarf2_where(struct dwarf2_line_info * line)416 dwarf2_where (struct dwarf2_line_info *line)
417 {
418 if (debug_type == DEBUG_DWARF2)
419 {
420 char *filename;
421 as_where (&filename, &line->line);
422 line->filenum = get_filenum (filename, 0);
423 line->column = 0;
424 line->flags = DWARF2_FLAG_IS_STMT;
425 line->isa = current.isa;
426 line->discriminator = current.discriminator;
427 line->logical = 0;
428 }
429 else
430 *line = current;
431 }
432
433 /* A hook to allow the target backend to inform the line number state
434 machine of isa changes when assembler debug info is enabled. */
435
436 void
dwarf2_set_isa(unsigned int isa)437 dwarf2_set_isa (unsigned int isa)
438 {
439 current.isa = isa;
440 }
441
442 /* Called for each machine instruction, or relatively atomic group of
443 machine instructions (ie built-in macro). The instruction or group
444 is SIZE bytes in length. If dwarf2 line number generation is called
445 for, emit a line statement appropriately. */
446
447 void
dwarf2_emit_insn(int size)448 dwarf2_emit_insn (int size)
449 {
450 struct dwarf2_line_info loc;
451
452 if (!dwarf2_loc_directive_seen && debug_type != DEBUG_DWARF2)
453 return;
454
455 dwarf2_where (&loc);
456
457 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
458 dwarf2_consume_line_info ();
459 }
460
461 /* Move all previously-emitted line entries for the current position by
462 DELTA bytes. This function cannot be used to move the same entries
463 twice. */
464
465 void
dwarf2_move_insn(int delta)466 dwarf2_move_insn (int delta)
467 {
468 struct line_subseg *lss;
469 struct line_entry *e;
470 valueT now;
471
472 if (delta == 0)
473 return;
474
475 lss = get_line_subseg (now_seg, now_subseg, FALSE);
476 if (!lss)
477 return;
478
479 now = frag_now_fix ();
480 while ((e = *lss->pmove_tail))
481 {
482 if (S_GET_VALUE (e->label) == now)
483 S_SET_VALUE (e->label, now + delta);
484 lss->pmove_tail = &e->next;
485 }
486 }
487
488 /* Called after the current line information has been either used with
489 dwarf2_gen_line_info or saved with a machine instruction for later use.
490 This resets the state of the line number information to reflect that
491 it has been used. */
492
493 void
dwarf2_consume_line_info(void)494 dwarf2_consume_line_info (void)
495 {
496 /* Unless we generate DWARF2 debugging information for each
497 assembler line, we only emit one line symbol for one LOC. */
498 dwarf2_loc_directive_seen = FALSE;
499
500 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
501 | DWARF2_FLAG_PROLOGUE_END
502 | DWARF2_FLAG_EPILOGUE_BEGIN);
503 current.discriminator = 0;
504 }
505
506 /* Called for each (preferably code) label. If dwarf2_loc_mark_labels
507 is enabled, emit a basic block marker. */
508
509 void
dwarf2_emit_label(symbolS * label)510 dwarf2_emit_label (symbolS *label)
511 {
512 struct dwarf2_line_info loc;
513
514 if (!dwarf2_loc_mark_labels)
515 return;
516 if (S_GET_SEGMENT (label) != now_seg)
517 return;
518 if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE))
519 return;
520 if (files_in_use == 0 && debug_type != DEBUG_DWARF2)
521 return;
522
523 dwarf2_where (&loc);
524
525 loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
526
527 dwarf2_gen_line_info_1 (label, &loc);
528 dwarf2_consume_line_info ();
529 }
530
531 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
532 allocate it on that file table slot, otherwise return the first
533 empty one. */
534
535 static unsigned int
get_filenum(const char * filename,unsigned int num)536 get_filenum (const char *filename, unsigned int num)
537 {
538 static unsigned int last_used, last_used_dir_len;
539 const char *file;
540 size_t dir_len;
541 unsigned int i, dir;
542
543 if (num == 0 && last_used)
544 {
545 if (! files[last_used].dir
546 && filename_cmp (filename, files[last_used].filename) == 0)
547 return last_used;
548 if (files[last_used].dir
549 && filename_ncmp (filename, dirs[files[last_used].dir],
550 last_used_dir_len) == 0
551 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
552 && filename_cmp (filename + last_used_dir_len + 1,
553 files[last_used].filename) == 0)
554 return last_used;
555 }
556
557 file = lbasename (filename);
558 /* Don't make empty string from / or A: from A:/ . */
559 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
560 if (file <= filename + 3)
561 file = filename;
562 #else
563 if (file == filename + 1)
564 file = filename;
565 #endif
566 dir_len = file - filename;
567
568 dir = 0;
569 if (dir_len)
570 {
571 #ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR
572 --dir_len;
573 #endif
574 for (dir = 1; dir < dirs_in_use; ++dir)
575 if (filename_ncmp (filename, dirs[dir], dir_len) == 0
576 && dirs[dir][dir_len] == '\0')
577 break;
578
579 if (dir >= dirs_in_use)
580 {
581 if (dir >= dirs_allocated)
582 {
583 dirs_allocated = dir + 32;
584 dirs = (char **)
585 xrealloc (dirs, (dir + 32) * sizeof (const char *));
586 }
587
588 dirs[dir] = (char *) xmalloc (dir_len + 1);
589 memcpy (dirs[dir], filename, dir_len);
590 dirs[dir][dir_len] = '\0';
591 dirs_in_use = dir + 1;
592 }
593 }
594
595 if (num == 0)
596 {
597 for (i = 1; i < files_in_use; ++i)
598 if (files[i].dir == dir
599 && files[i].filename
600 && filename_cmp (file, files[i].filename) == 0)
601 {
602 last_used = i;
603 last_used_dir_len = dir_len;
604 return i;
605 }
606 }
607 else
608 i = num;
609
610 if (i >= files_allocated)
611 {
612 unsigned int old = files_allocated;
613
614 files_allocated = i + 32;
615 files = (struct file_entry *)
616 xrealloc (files, (i + 32) * sizeof (struct file_entry));
617
618 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
619 }
620
621 files[i].filename = num ? file : xstrdup (file);
622 files[i].dir = dir;
623 if (files_in_use < i + 1)
624 files_in_use = i + 1;
625 last_used = i;
626 last_used_dir_len = dir_len;
627
628 return i;
629 }
630
631 /* Make a new entry in the subprograms table. */
632
633 static void
make_subprog_entry(unsigned int num,char * subpname,int filenum,int line)634 make_subprog_entry (unsigned int num, char *subpname, int filenum, int line)
635 {
636 if (subprogs_allocated == 0)
637 {
638 subprogs_allocated = 4;
639 subprogs = (struct subprog_entry *)
640 xcalloc (subprogs_allocated, sizeof (struct subprog_entry));
641 }
642 if (num > subprogs_allocated)
643 {
644 unsigned int old = subprogs_allocated;
645
646 subprogs_allocated *= 2;
647 if (num > subprogs_allocated)
648 subprogs_allocated = num;
649 subprogs = (struct subprog_entry *)
650 xrealloc (subprogs,
651 subprogs_allocated * sizeof (struct subprog_entry));
652 memset (subprogs + old, 0,
653 (subprogs_allocated - old) * sizeof (struct subprog_entry));
654 }
655 if (subprogs_in_use < num)
656 subprogs_in_use = num;
657 subprogs[num - 1].subpname = xstrdup (subpname);
658 subprogs[num - 1].filenum = filenum;
659 subprogs[num - 1].line = line;
660 }
661
662 /* Make a new entry in the logicals table. */
663
664 static void
make_logical(unsigned int logical,int context,int subprog)665 make_logical (unsigned int logical, int context, int subprog)
666 {
667 if (logicals_allocated == 0)
668 {
669 logicals_allocated = 4;
670 logicals = (struct logicals_entry *)
671 xcalloc (logicals_allocated, sizeof (struct logicals_entry));
672 }
673 if (logical > logicals_allocated)
674 {
675 unsigned int old = logicals_allocated;
676
677 logicals_allocated *= 2;
678 if (logical > logicals_allocated)
679 logicals_allocated = logical;
680 logicals = (struct logicals_entry *)
681 xrealloc (logicals,
682 logicals_allocated * sizeof (struct logicals_entry));
683 memset (logicals + old, 0,
684 (logicals_allocated - old) * sizeof (struct logicals_entry));
685 }
686 logicals[logical - 1].loc = current;
687 logicals[logical - 1].context = context;
688 logicals[logical - 1].subprog = subprog;
689 if (logical > logicals_in_use)
690 logicals_in_use = logical;
691 }
692
693 /* Handle two forms of .file directive:
694 - Pass .file "source.c" to s_app_file
695 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
696
697 If an entry is added to the file table, return a pointer to the filename. */
698
699 char *
dwarf2_directive_file(int dummy ATTRIBUTE_UNUSED)700 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
701 {
702 offsetT num;
703 char *filename;
704 int filename_len;
705
706 /* Continue to accept a bare string and pass it off. */
707 SKIP_WHITESPACE ();
708 if (*input_line_pointer == '"')
709 {
710 s_app_file (0);
711 return NULL;
712 }
713
714 num = get_absolute_expression ();
715 filename = demand_copy_C_string (&filename_len);
716 if (filename == NULL)
717 return NULL;
718 demand_empty_rest_of_line ();
719
720 if (num < 1)
721 {
722 as_bad (_("file number less than one"));
723 return NULL;
724 }
725
726 /* A .file directive implies compiler generated debug information is
727 being supplied. Turn off gas generated debug info. */
728 debug_type = DEBUG_NONE;
729
730 if (num < (int) files_in_use && files[num].filename != 0)
731 {
732 as_bad (_("file number %ld already allocated"), (long) num);
733 return NULL;
734 }
735
736 get_filenum (filename, num);
737
738 return filename;
739 }
740
741 /* Experimental DWARF-5 extension:
742 Implements the .subprog SUBPNO ["SUBPROG" [FILENO LINENO]] directive.
743 FILENO is the file number, LINENO the line number and the
744 (optional) COLUMN the column of the source code that the following
745 instruction corresponds to. FILENO can be 0 to indicate that the
746 filename specified by the textually most recent .file directive
747 should be used. */
748 void
dwarf2_directive_subprog(int dummy ATTRIBUTE_UNUSED)749 dwarf2_directive_subprog (int dummy ATTRIBUTE_UNUSED)
750 {
751 offsetT num, filenum, line;
752 char *subpname;
753 int subpname_len;
754
755 num = get_absolute_expression ();
756 subpname = demand_copy_C_string (&subpname_len);
757 if (subpname == NULL)
758 return;
759
760 SKIP_WHITESPACE ();
761 filenum = get_absolute_expression ();
762 SKIP_WHITESPACE ();
763 line = get_absolute_expression ();
764 demand_empty_rest_of_line ();
765
766 if (num < 1)
767 {
768 as_bad (_("subprogram number less than one"));
769 return;
770 }
771
772 /* A .subprog directive implies compiler generated debug information is
773 being supplied. Turn off gas generated debug info. */
774 debug_type = DEBUG_NONE;
775
776 if (num < (int) subprogs_in_use && subprogs[num].subpname != NULL)
777 {
778 as_bad (_("subprogram number %ld already allocated"), (long) num);
779 return;
780 }
781
782 make_subprog_entry (num, subpname, filenum, line);
783 }
784
785 void
dwarf2_directive_loc(int is_lloc)786 dwarf2_directive_loc (int is_lloc)
787 {
788 offsetT filenum, line;
789 offsetT logical = 0;
790 offsetT context = 0;
791 offsetT subprog = 0;
792 bfd_boolean is_new_logical = FALSE;
793 bfd_boolean is_actual = FALSE;
794 static bfd_boolean saw_loc = FALSE;
795 static bfd_boolean saw_lloc = FALSE;
796 static bfd_boolean saw_both = FALSE;
797
798 if ((is_lloc && saw_loc) || (!is_lloc && saw_lloc))
799 {
800 if (!saw_both)
801 as_bad (_(".loc and .lloc cannot both be used"));
802 saw_both = TRUE;
803 return;
804 }
805
806 if (is_lloc)
807 {
808 saw_lloc = TRUE;
809 logical = get_absolute_expression ();
810 SKIP_WHITESPACE ();
811
812 if (ISDIGIT (*input_line_pointer))
813 is_new_logical = TRUE;
814 else
815 is_actual = TRUE;
816
817 if (logical < 1)
818 {
819 as_bad (_("logical row less than one"));
820 return;
821 }
822 if (is_actual &&
823 ((unsigned int) logical > logicals_in_use
824 || logicals[logical - 1].loc.line == 0))
825 {
826 as_bad (_("unassigned logical row %ld"), (long) logical);
827 return;
828 }
829 }
830 else
831 saw_loc = TRUE;
832
833 /* If we see two .loc directives in a row, force the first one to be
834 output now. */
835 if (dwarf2_loc_directive_seen)
836 dwarf2_emit_insn (0);
837
838 if (is_lloc && !is_new_logical)
839 {
840 filenum = logicals[logical - 1].loc.filenum;
841 line = logicals[logical - 1].loc.line;
842 }
843 else
844 {
845 filenum = get_absolute_expression ();
846 SKIP_WHITESPACE ();
847 line = get_absolute_expression ();
848
849 if (filenum < 1)
850 {
851 as_bad (_("file number less than one"));
852 return;
853 }
854 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
855 {
856 as_bad (_("unassigned file number %ld"), (long) filenum);
857 return;
858 }
859 }
860
861 current.filenum = filenum;
862 current.line = line;
863 current.discriminator = 0;
864 current.logical = logical;
865
866 #ifndef NO_LISTING
867 if (listing)
868 {
869 if (files[filenum].dir)
870 {
871 size_t dir_len = strlen (dirs[files[filenum].dir]);
872 size_t file_len = strlen (files[filenum].filename);
873 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
874
875 memcpy (cp, dirs[files[filenum].dir], dir_len);
876 INSERT_DIR_SEPARATOR (cp, dir_len);
877 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
878 cp[dir_len + file_len + 1] = '\0';
879 listing_source_file (cp);
880 }
881 else
882 listing_source_file (files[filenum].filename);
883 listing_source_line (line);
884 }
885 #endif
886
887 SKIP_WHITESPACE ();
888 if (ISDIGIT (*input_line_pointer))
889 {
890 current.column = get_absolute_expression ();
891 SKIP_WHITESPACE ();
892 }
893
894 while (ISALPHA (*input_line_pointer))
895 {
896 char *p, c;
897 offsetT value;
898
899 p = input_line_pointer;
900 c = get_symbol_end ();
901
902 if (strcmp (p, "basic_block") == 0)
903 {
904 current.flags |= DWARF2_FLAG_BASIC_BLOCK;
905 *input_line_pointer = c;
906 }
907 else if (!is_actual && strcmp (p, "prologue_end") == 0)
908 {
909 current.flags |= DWARF2_FLAG_PROLOGUE_END;
910 *input_line_pointer = c;
911 }
912 else if (!is_actual && strcmp (p, "epilogue_begin") == 0)
913 {
914 current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
915 *input_line_pointer = c;
916 }
917 else if (!is_actual && strcmp (p, "is_stmt") == 0)
918 {
919 *input_line_pointer = c;
920 value = get_absolute_expression ();
921 if (value == 0)
922 current.flags &= ~DWARF2_FLAG_IS_STMT;
923 else if (value == 1)
924 current.flags |= DWARF2_FLAG_IS_STMT;
925 else
926 {
927 as_bad (_("is_stmt value not 0 or 1"));
928 return;
929 }
930 }
931 else if (strcmp (p, "isa") == 0)
932 {
933 *input_line_pointer = c;
934 value = get_absolute_expression ();
935 if (value >= 0)
936 current.isa = value;
937 else
938 {
939 as_bad (_("isa number less than zero"));
940 return;
941 }
942 }
943 else if (!is_actual && strcmp (p, "discriminator") == 0)
944 {
945 *input_line_pointer = c;
946 value = get_absolute_expression ();
947 if (value >= 0)
948 current.discriminator = value;
949 else
950 {
951 as_bad (_("discriminator less than zero"));
952 return;
953 }
954 }
955 else if (!is_actual && strcmp (p, "context") == 0)
956 {
957 *input_line_pointer = c;
958 value = get_absolute_expression ();
959 if (value >= 0)
960 context = value;
961 else
962 {
963 as_bad (_("context less than zero"));
964 return;
965 }
966 }
967 else if (!is_actual && strcmp (p, "subprog") == 0)
968 {
969 *input_line_pointer = c;
970 value = get_absolute_expression ();
971 if (value >= 0)
972 subprog = value;
973 else
974 {
975 as_bad (_("subprog number less than zero"));
976 return;
977 }
978 }
979 else
980 {
981 as_bad (_("unknown .loc sub-directive `%s'"), p);
982 *input_line_pointer = c;
983 return;
984 }
985
986 SKIP_WHITESPACE ();
987 }
988
989 demand_empty_rest_of_line ();
990 dwarf2_loc_directive_seen = TRUE;
991 debug_type = DEBUG_NONE;
992
993 if (is_new_logical)
994 make_logical (logical, context, subprog);
995 }
996
997 void
dwarf2_directive_loc_mark_labels(int dummy ATTRIBUTE_UNUSED)998 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
999 {
1000 offsetT value = get_absolute_expression ();
1001
1002 if (value != 0 && value != 1)
1003 {
1004 as_bad (_("expected 0 or 1"));
1005 ignore_rest_of_line ();
1006 }
1007 else
1008 {
1009 dwarf2_loc_mark_labels = value != 0;
1010 demand_empty_rest_of_line ();
1011 }
1012 }
1013
1014 static struct frag *
first_frag_for_seg(segT seg)1015 first_frag_for_seg (segT seg)
1016 {
1017 return seg_info (seg)->frchainP->frch_root;
1018 }
1019
1020 static struct frag *
last_frag_for_seg(segT seg)1021 last_frag_for_seg (segT seg)
1022 {
1023 frchainS *f = seg_info (seg)->frchainP;
1024
1025 while (f->frch_next != NULL)
1026 f = f->frch_next;
1027
1028 return f->frch_last;
1029 }
1030
1031 /* Emit a single byte into the current segment. */
1032
1033 static inline void
out_byte(int byte)1034 out_byte (int byte)
1035 {
1036 FRAG_APPEND_1_CHAR (byte);
1037 }
1038
1039 /* Emit a statement program opcode into the current segment. */
1040
1041 static inline void
out_opcode(int opc)1042 out_opcode (int opc)
1043 {
1044 out_byte (opc);
1045 }
1046
1047 /* Emit a two-byte word into the current segment. */
1048
1049 static inline void
out_two(int data)1050 out_two (int data)
1051 {
1052 md_number_to_chars (frag_more (2), data, 2);
1053 }
1054
1055 /* Emit a four byte word into the current segment. */
1056
1057 static inline void
out_four(int data)1058 out_four (int data)
1059 {
1060 md_number_to_chars (frag_more (4), data, 4);
1061 }
1062
1063 /* Emit an unsigned "little-endian base 128" number. */
1064
1065 static void
out_uleb128(addressT value)1066 out_uleb128 (addressT value)
1067 {
1068 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
1069 }
1070
1071 /* Emit a signed "little-endian base 128" number. */
1072
1073 static void
out_leb128(addressT value)1074 out_leb128 (addressT value)
1075 {
1076 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
1077 }
1078
1079 /* Emit a tuple for .debug_abbrev. */
1080
1081 static inline void
out_abbrev(int name,int form)1082 out_abbrev (int name, int form)
1083 {
1084 out_uleb128 (name);
1085 out_uleb128 (form);
1086 }
1087
1088 /* Get the size of a fragment. */
1089
1090 static offsetT
get_frag_fix(fragS * frag,segT seg)1091 get_frag_fix (fragS *frag, segT seg)
1092 {
1093 frchainS *fr;
1094
1095 if (frag->fr_next)
1096 return frag->fr_fix;
1097
1098 /* If a fragment is the last in the chain, special measures must be
1099 taken to find its size before relaxation, since it may be pending
1100 on some subsegment chain. */
1101 for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
1102 if (fr->frch_last == frag)
1103 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
1104
1105 abort ();
1106 }
1107
1108 /* Set an absolute address (may result in a relocation entry). */
1109
1110 static void
out_set_addr(symbolS * sym)1111 out_set_addr (symbolS *sym)
1112 {
1113 expressionS exp;
1114
1115 out_opcode (DW_LNS_extended_op);
1116 out_uleb128 (sizeof_address + 1);
1117
1118 out_opcode (DW_LNE_set_address);
1119 exp.X_op = O_symbol;
1120 exp.X_add_symbol = sym;
1121 exp.X_add_number = 0;
1122 emit_expr (&exp, sizeof_address);
1123 }
1124
1125 /* Set the address from a logicals table entry. */
1126
1127 static void
out_set_addr_from_logical(int logical_delta)1128 out_set_addr_from_logical (int logical_delta)
1129 {
1130 out_opcode (DW_LNS_set_address_from_logical);
1131 out_leb128 (logical_delta);
1132 }
1133
1134 static void scale_addr_delta (addressT *);
1135
1136 static void
scale_addr_delta(addressT * addr_delta)1137 scale_addr_delta (addressT *addr_delta)
1138 {
1139 static int printed_this = 0;
1140 if (DWARF2_LINE_MIN_INSN_LENGTH > 1)
1141 {
1142 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0 && !printed_this)
1143 {
1144 as_bad("unaligned opcodes detected in executable segment");
1145 printed_this = 1;
1146 }
1147 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
1148 }
1149 }
1150
1151 /* Encode a pair of line and address skips as efficiently as possible.
1152 Note that the line skip is signed, whereas the address skip is unsigned.
1153
1154 The following two routines *must* be kept in sync. This is
1155 enforced by making emit_inc_line_addr abort if we do not emit
1156 exactly the expected number of bytes. */
1157
1158 static int
size_inc_line_addr(int line_delta,addressT addr_delta)1159 size_inc_line_addr (int line_delta, addressT addr_delta)
1160 {
1161 unsigned int tmp, opcode;
1162 int len = 0;
1163
1164 /* Scale the address delta by the minimum instruction length. */
1165 scale_addr_delta (&addr_delta);
1166
1167 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
1168 We cannot use special opcodes here, since we want the end_sequence
1169 to emit the matrix entry. */
1170 if (line_delta == INT_MAX)
1171 {
1172 if (addr_delta == (unsigned int) MAX_SPECIAL_ADDR_DELTA)
1173 len = 1;
1174 else
1175 len = 1 + sizeof_leb128 (addr_delta, 0);
1176 return len + 3;
1177 }
1178
1179 /* Bias the line delta by the base. */
1180 tmp = line_delta - line_base;
1181
1182 /* If the line increment is out of range of a special opcode, we
1183 must encode it with DW_LNS_advance_line. */
1184 if (tmp >= line_range)
1185 {
1186 len = 1 + sizeof_leb128 (line_delta, 1);
1187 line_delta = 0;
1188 tmp = 0 - line_base;
1189 }
1190
1191 /* Bias the opcode by the special opcode base. */
1192 tmp += opcode_base;
1193
1194 /* Avoid overflow when addr_delta is large. */
1195 if (addr_delta < (unsigned int) (256 + MAX_SPECIAL_ADDR_DELTA))
1196 {
1197 /* Try using a special opcode. */
1198 opcode = tmp + addr_delta * line_range;
1199 if (opcode <= 255)
1200 return len + 1;
1201
1202 /* Try using DW_LNS_const_add_pc followed by special op. */
1203 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * line_range;
1204 if (opcode <= 255)
1205 return len + 2;
1206 }
1207
1208 /* Otherwise use DW_LNS_advance_pc. */
1209 len += 1 + sizeof_leb128 (addr_delta, 0);
1210
1211 /* DW_LNS_copy or special opcode. */
1212 len += 1;
1213
1214 return len;
1215 }
1216
1217 static void
emit_inc_line_addr(int line_delta,addressT addr_delta,char * p,int len)1218 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
1219 {
1220 unsigned int tmp, opcode;
1221 int need_copy = 0;
1222 char *end = p + len;
1223
1224 /* Line number sequences cannot go backward in addresses. This means
1225 we've incorrectly ordered the statements in the sequence. */
1226 gas_assert ((offsetT) addr_delta >= 0);
1227
1228 /* Scale the address delta by the minimum instruction length. */
1229 scale_addr_delta (&addr_delta);
1230
1231 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
1232 We cannot use special opcodes here, since we want the end_sequence
1233 to emit the matrix entry. */
1234 if (line_delta == INT_MAX)
1235 {
1236 if (addr_delta == (unsigned int) MAX_SPECIAL_ADDR_DELTA)
1237 *p++ = DW_LNS_const_add_pc;
1238 else
1239 {
1240 *p++ = DW_LNS_advance_pc;
1241 p += output_leb128 (p, addr_delta, 0);
1242 }
1243
1244 *p++ = DW_LNS_extended_op;
1245 *p++ = 1;
1246 *p++ = DW_LNE_end_sequence;
1247 goto done;
1248 }
1249
1250 /* Bias the line delta by the base. */
1251 tmp = line_delta - line_base;
1252
1253 /* If the line increment is out of range of a special opcode, we
1254 must encode it with DW_LNS_advance_line. */
1255 if (tmp >= line_range)
1256 {
1257 *p++ = DW_LNS_advance_line;
1258 p += output_leb128 (p, line_delta, 1);
1259
1260 line_delta = 0;
1261 tmp = 0 - line_base;
1262 need_copy = 1;
1263 }
1264
1265 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
1266 special opcode. */
1267 if (line_delta == 0 && addr_delta == 0)
1268 {
1269 *p++ = DW_LNS_copy;
1270 goto done;
1271 }
1272
1273 /* Bias the opcode by the special opcode base. */
1274 tmp += opcode_base;
1275
1276 /* Avoid overflow when addr_delta is large. */
1277 if (addr_delta < (unsigned int) (256 + MAX_SPECIAL_ADDR_DELTA))
1278 {
1279 /* Try using a special opcode. */
1280 opcode = tmp + addr_delta * line_range;
1281 if (opcode <= 255)
1282 {
1283 *p++ = opcode;
1284 goto done;
1285 }
1286
1287 /* Try using DW_LNS_const_add_pc followed by special op. */
1288 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * line_range;
1289 if (opcode <= 255)
1290 {
1291 *p++ = DW_LNS_const_add_pc;
1292 *p++ = opcode;
1293 goto done;
1294 }
1295 }
1296
1297 /* Otherwise use DW_LNS_advance_pc. */
1298 *p++ = DW_LNS_advance_pc;
1299 p += output_leb128 (p, addr_delta, 0);
1300
1301 if (need_copy)
1302 *p++ = DW_LNS_copy;
1303 else
1304 *p++ = tmp;
1305
1306 done:
1307 gas_assert (p == end);
1308 }
1309
1310 /* Handy routine to combine calls to the above two routines. */
1311
1312 static void
out_inc_line_addr(int line_delta,addressT addr_delta)1313 out_inc_line_addr (int line_delta, addressT addr_delta)
1314 {
1315 int len = size_inc_line_addr (line_delta, addr_delta);
1316 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
1317 }
1318
1319 /* Write out an alternative form of line and address skips using
1320 DW_LNS_fixed_advance_pc opcodes. This uses more space than the default
1321 line and address information, but it is required if linker relaxation
1322 could change the code offsets. The following two routines *must* be
1323 kept in sync. */
1324 #define ADDR_DELTA_LIMIT 50000
1325
1326 static int
size_fixed_inc_line_addr(int line_delta,addressT addr_delta)1327 size_fixed_inc_line_addr (int line_delta, addressT addr_delta)
1328 {
1329 int len = 0;
1330
1331 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */
1332 if (line_delta != INT_MAX)
1333 len = 1 + sizeof_leb128 (line_delta, 1);
1334
1335 if (addr_delta > ADDR_DELTA_LIMIT)
1336 {
1337 /* DW_LNS_extended_op */
1338 len += 1 + sizeof_leb128 (sizeof_address + 1, 0);
1339 /* DW_LNE_set_address */
1340 len += 1 + sizeof_address;
1341 }
1342 else
1343 /* DW_LNS_fixed_advance_pc */
1344 len += 3;
1345
1346 if (line_delta == INT_MAX)
1347 /* DW_LNS_extended_op + DW_LNE_end_sequence */
1348 len += 3;
1349 else
1350 /* DW_LNS_copy */
1351 len += 1;
1352
1353 return len;
1354 }
1355
1356 static void
emit_fixed_inc_line_addr(int line_delta,addressT addr_delta,fragS * frag,char * p,int len)1357 emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag,
1358 char *p, int len)
1359 {
1360 expressionS *pexp;
1361 char *end = p + len;
1362
1363 /* Line number sequences cannot go backward in addresses. This means
1364 we've incorrectly ordered the statements in the sequence. */
1365 gas_assert ((offsetT) addr_delta >= 0);
1366
1367 /* Verify that we have kept in sync with size_fixed_inc_line_addr. */
1368 gas_assert (len == size_fixed_inc_line_addr (line_delta, addr_delta));
1369
1370 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */
1371 if (line_delta != INT_MAX)
1372 {
1373 *p++ = DW_LNS_advance_line;
1374 p += output_leb128 (p, line_delta, 1);
1375 }
1376
1377 pexp = symbol_get_value_expression (frag->fr_symbol);
1378
1379 /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can
1380 advance the address by at most 64K. Linker relaxation (without
1381 which this function would not be used) could change the operand by
1382 an unknown amount. If the address increment is getting close to
1383 the limit, just reset the address. */
1384 if (addr_delta > ADDR_DELTA_LIMIT)
1385 {
1386 symbolS *to_sym;
1387 expressionS exp;
1388
1389 gas_assert (pexp->X_op == O_subtract);
1390 to_sym = pexp->X_add_symbol;
1391
1392 *p++ = DW_LNS_extended_op;
1393 p += output_leb128 (p, sizeof_address + 1, 0);
1394 *p++ = DW_LNE_set_address;
1395 exp.X_op = O_symbol;
1396 exp.X_add_symbol = to_sym;
1397 exp.X_add_number = 0;
1398 emit_expr_fix (&exp, sizeof_address, frag, p, TC_PARSE_CONS_RETURN_NONE);
1399 p += sizeof_address;
1400 }
1401 else
1402 {
1403 *p++ = DW_LNS_fixed_advance_pc;
1404 emit_expr_fix (pexp, 2, frag, p, TC_PARSE_CONS_RETURN_NONE);
1405 p += 2;
1406 }
1407
1408 if (line_delta == INT_MAX)
1409 {
1410 *p++ = DW_LNS_extended_op;
1411 *p++ = 1;
1412 *p++ = DW_LNE_end_sequence;
1413 }
1414 else
1415 *p++ = DW_LNS_copy;
1416
1417 gas_assert (p == end);
1418 }
1419
1420 /* Generate a variant frag that we can use to relax address/line
1421 increments between fragments of the target segment. */
1422
1423 static void
relax_inc_line_addr(int line_delta,symbolS * to_sym,symbolS * from_sym)1424 relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
1425 {
1426 expressionS exp;
1427 int max_chars;
1428
1429 exp.X_op = O_subtract;
1430 exp.X_add_symbol = to_sym;
1431 exp.X_op_symbol = from_sym;
1432 exp.X_add_number = 0;
1433
1434 /* The maximum size of the frag is the line delta with a maximum
1435 sized address delta. */
1436 if (DWARF2_USE_FIXED_ADVANCE_PC)
1437 max_chars = size_fixed_inc_line_addr (line_delta,
1438 -DWARF2_LINE_MIN_INSN_LENGTH);
1439 else
1440 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
1441
1442 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
1443 make_expr_symbol (&exp), line_delta, NULL);
1444 }
1445
1446 /* The function estimates the size of a rs_dwarf2dbg variant frag
1447 based on the current values of the symbols. It is called before
1448 the relaxation loop. We set fr_subtype to the expected length. */
1449
1450 int
dwarf2dbg_estimate_size_before_relax(fragS * frag)1451 dwarf2dbg_estimate_size_before_relax (fragS *frag)
1452 {
1453 offsetT addr_delta;
1454 int size;
1455
1456 addr_delta = resolve_symbol_value (frag->fr_symbol);
1457 if (DWARF2_USE_FIXED_ADVANCE_PC)
1458 size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta);
1459 else
1460 size = size_inc_line_addr (frag->fr_offset, addr_delta);
1461
1462 frag->fr_subtype = size;
1463
1464 return size;
1465 }
1466
1467 /* This function relaxes a rs_dwarf2dbg variant frag based on the
1468 current values of the symbols. fr_subtype is the current length
1469 of the frag. This returns the change in frag length. */
1470
1471 int
dwarf2dbg_relax_frag(fragS * frag)1472 dwarf2dbg_relax_frag (fragS *frag)
1473 {
1474 int old_size, new_size;
1475
1476 old_size = frag->fr_subtype;
1477 new_size = dwarf2dbg_estimate_size_before_relax (frag);
1478
1479 return new_size - old_size;
1480 }
1481
1482 /* This function converts a rs_dwarf2dbg variant frag into a normal
1483 fill frag. This is called after all relaxation has been done.
1484 fr_subtype will be the desired length of the frag. */
1485
1486 void
dwarf2dbg_convert_frag(fragS * frag)1487 dwarf2dbg_convert_frag (fragS *frag)
1488 {
1489 offsetT addr_diff;
1490
1491 if (DWARF2_USE_FIXED_ADVANCE_PC)
1492 {
1493 /* If linker relaxation is enabled then the distance bewteen the two
1494 symbols in the frag->fr_symbol expression might change. Hence we
1495 cannot rely upon the value computed by resolve_symbol_value.
1496 Instead we leave the expression unfinalized and allow
1497 emit_fixed_inc_line_addr to create a fixup (which later becomes a
1498 relocation) that will allow the linker to correctly compute the
1499 actual address difference. We have to use a fixed line advance for
1500 this as we cannot (easily) relocate leb128 encoded values. */
1501 int saved_finalize_syms = finalize_syms;
1502
1503 finalize_syms = 0;
1504 addr_diff = resolve_symbol_value (frag->fr_symbol);
1505 finalize_syms = saved_finalize_syms;
1506 }
1507 else
1508 addr_diff = resolve_symbol_value (frag->fr_symbol);
1509
1510 /* fr_var carries the max_chars that we created the fragment with.
1511 fr_subtype carries the current expected length. We must, of
1512 course, have allocated enough memory earlier. */
1513 gas_assert (frag->fr_var >= (int) frag->fr_subtype);
1514
1515 if (DWARF2_USE_FIXED_ADVANCE_PC)
1516 emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag,
1517 frag->fr_literal + frag->fr_fix,
1518 frag->fr_subtype);
1519 else
1520 emit_inc_line_addr (frag->fr_offset, addr_diff,
1521 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
1522
1523 frag->fr_fix += frag->fr_subtype;
1524 frag->fr_type = rs_fill;
1525 frag->fr_var = 0;
1526 frag->fr_offset = 0;
1527 }
1528
1529 /* Generate .debug_line content for the logicals table rows. */
1530
1531 static void
emit_logicals(void)1532 emit_logicals (void)
1533 {
1534 unsigned logical;
1535 unsigned filenum = 1;
1536 unsigned line = 1;
1537 unsigned column = 0;
1538 unsigned discriminator;
1539 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1540 unsigned context = 0;
1541 unsigned subprog = 0;
1542 segT last_seg = NULL;
1543 fragS *last_frag = NULL, *frag;
1544 addressT last_frag_ofs = 0, frag_ofs;
1545 symbolS *last_lab = NULL, *lab;
1546
1547 for (logical = 1; logical <= logicals_in_use; ++logical)
1548 {
1549 int line_delta;
1550 int context_delta;
1551 struct logicals_entry *e = &logicals[logical - 1];
1552
1553 discriminator = 0;
1554
1555 if (context != e->context || subprog != e->subprog)
1556 {
1557 unsigned int caller = context;
1558 unsigned int npop = 0;
1559
1560 // See if a sequence of DW_LNS_pop_context ops will get
1561 // to the state we want.
1562 while (caller > 0 && caller <= logicals_in_use)
1563 {
1564 ++npop;
1565 if (logicals[caller - 1].subprog == e->subprog)
1566 break;
1567 caller = logicals[caller - 1].context;
1568 }
1569 if (caller > 0 && caller <= logicals_in_use && npop < 10)
1570 {
1571 while (npop-- > 0)
1572 out_opcode (DW_LNS_pop_context);
1573 filenum = logicals[caller - 1].loc.filenum;
1574 line = logicals[caller - 1].loc.line;
1575 column = logicals[caller - 1].loc.column;
1576 discriminator = logicals[caller - 1].loc.discriminator;
1577 flags = logicals[caller - 1].loc.flags;
1578 context = logicals[caller - 1].context;
1579 subprog = logicals[caller - 1].subprog;
1580 }
1581 if (context != e->context && e->context == 0)
1582 {
1583 context = 0;
1584 subprog = e->subprog;
1585 out_opcode (DW_LNS_set_subprogram);
1586 out_uleb128 (subprog);
1587 }
1588 else if (context != e->context || subprog != e->subprog)
1589 {
1590 context_delta = e->context - (logical - 1);
1591 context = e->context;
1592 subprog = e->subprog;
1593 out_opcode (DW_LNS_inlined_call);
1594 out_leb128 (context_delta);
1595 out_uleb128 (subprog);
1596 }
1597 }
1598
1599 if (filenum != e->loc.filenum)
1600 {
1601 filenum = e->loc.filenum;
1602 out_opcode (DW_LNS_set_file);
1603 out_uleb128 (filenum);
1604 }
1605
1606 if (column != e->loc.column)
1607 {
1608 column = e->loc.column;
1609 out_opcode (DW_LNS_set_column);
1610 out_uleb128 (column);
1611 }
1612
1613 if (e->loc.discriminator != discriminator)
1614 {
1615 out_opcode (DW_LNS_extended_op);
1616 out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0));
1617 out_opcode (DW_LNE_set_discriminator);
1618 out_uleb128 (e->loc.discriminator);
1619 }
1620
1621 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
1622 {
1623 flags = e->loc.flags;
1624 out_opcode (DW_LNS_negate_stmt);
1625 }
1626
1627 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
1628 out_opcode (DW_LNS_set_prologue_end);
1629
1630 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1631 out_opcode (DW_LNS_set_epilogue_begin);
1632
1633 line_delta = e->loc.line - line;
1634 if (e->label == NULL)
1635 {
1636 out_inc_line_addr (line_delta, 0);
1637 }
1638 else
1639 {
1640 lab = e->label;
1641 frag = symbol_get_frag (lab);
1642 frag_ofs = S_GET_VALUE (lab);
1643
1644 if (last_frag == NULL || e->seg != last_seg)
1645 {
1646 out_set_addr (lab);
1647 out_inc_line_addr (line_delta, 0);
1648 }
1649 else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1650 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
1651 else
1652 relax_inc_line_addr (line_delta, lab, last_lab);
1653
1654 line = e->loc.line;
1655 last_seg = e->seg;
1656 last_lab = lab;
1657 last_frag = frag;
1658 last_frag_ofs = frag_ofs;
1659 }
1660 }
1661 }
1662
1663 /* Generate .debug_line content for the chain of line number entries
1664 beginning at E, for segment SEG. */
1665
1666 static void
process_entries(segT seg,struct line_entry * e)1667 process_entries (segT seg, struct line_entry *e)
1668 {
1669 unsigned filenum = 1;
1670 unsigned line = 1;
1671 unsigned column = 0;
1672 unsigned isa = 0;
1673 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1674 fragS *last_frag = NULL, *frag;
1675 addressT last_frag_ofs = 0, frag_ofs;
1676 symbolS *last_lab = NULL, *lab;
1677 struct line_entry *next;
1678
1679 if (flag_dwarf_sections)
1680 {
1681 char * name;
1682 const char * sec_name;
1683
1684 /* Switch to the relevent sub-section before we start to emit
1685 the line number table.
1686
1687 FIXME: These sub-sections do not have a normal Line Number
1688 Program Header, thus strictly speaking they are not valid
1689 DWARF sections. Unfortunately the DWARF standard assumes
1690 a one-to-one relationship between compilation units and
1691 line number tables. Thus we have to have a .debug_line
1692 section, as well as our sub-sections, and we have to ensure
1693 that all of the sub-sections are merged into a proper
1694 .debug_line section before a debugger sees them. */
1695
1696 sec_name = bfd_get_section_name (stdoutput, seg);
1697 if (strcmp (sec_name, ".text") != 0)
1698 {
1699 unsigned int len;
1700
1701 len = strlen (sec_name);
1702 name = xmalloc (len + 11 + 2);
1703 sprintf (name, ".debug_line%s", sec_name);
1704 subseg_set (subseg_get (name, FALSE), 0);
1705 }
1706 else
1707 /* Don't create a .debug_line.text section -
1708 that is redundant. Instead just switch back to the
1709 normal .debug_line section. */
1710 subseg_set (subseg_get (".debug_line", FALSE), 0);
1711 }
1712
1713 do
1714 {
1715 int line_delta;
1716
1717 if (logicals_in_use == 0)
1718 {
1719 if (filenum != e->loc.filenum)
1720 {
1721 filenum = e->loc.filenum;
1722 out_opcode (DW_LNS_set_file);
1723 out_uleb128 (filenum);
1724 }
1725
1726 if (column != e->loc.column)
1727 {
1728 column = e->loc.column;
1729 out_opcode (DW_LNS_set_column);
1730 out_uleb128 (column);
1731 }
1732
1733 if (e->loc.discriminator != 0)
1734 {
1735 out_opcode (DW_LNS_extended_op);
1736 out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0));
1737 out_opcode (DW_LNE_set_discriminator);
1738 out_uleb128 (e->loc.discriminator);
1739 }
1740 }
1741
1742 if (isa != e->loc.isa)
1743 {
1744 isa = e->loc.isa;
1745 out_opcode (DW_LNS_set_isa);
1746 out_uleb128 (isa);
1747 }
1748
1749 if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
1750 out_opcode (DW_LNS_set_basic_block);
1751
1752 if (logicals_in_use == 0)
1753 {
1754 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
1755 {
1756 flags = e->loc.flags;
1757 out_opcode (DW_LNS_negate_stmt);
1758 }
1759
1760 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
1761 out_opcode (DW_LNS_set_prologue_end);
1762
1763 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1764 out_opcode (DW_LNS_set_epilogue_begin);
1765 }
1766
1767 /* Don't try to optimize away redundant entries; gdb wants two
1768 entries for a function where the code starts on the same line as
1769 the {, and there's no way to identify that case here. Trust gcc
1770 to optimize appropriately. */
1771 if (logicals_in_use == 0)
1772 line_delta = e->loc.line - line;
1773 else
1774 line_delta = e->loc.logical - line;
1775 lab = e->label;
1776 frag = symbol_get_frag (lab);
1777 frag_ofs = S_GET_VALUE (lab);
1778
1779 if (last_frag == NULL)
1780 {
1781 if (logicals_in_use > 0 && logicals[e->loc.logical - 1].label == lab)
1782 {
1783 out_set_addr_from_logical (line_delta);
1784 out_opcode (DW_LNS_copy);
1785 }
1786 else
1787 {
1788 out_set_addr (lab);
1789 out_inc_line_addr (line_delta, 0);
1790 }
1791 }
1792 else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1793 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
1794 else
1795 relax_inc_line_addr (line_delta, lab, last_lab);
1796
1797 if (logicals_in_use == 0)
1798 line = e->loc.line;
1799 else
1800 line = e->loc.logical;
1801 last_lab = lab;
1802 last_frag = frag;
1803 last_frag_ofs = frag_ofs;
1804
1805 next = e->next;
1806 free (e);
1807 e = next;
1808 }
1809 while (e);
1810
1811 /* Emit a DW_LNE_end_sequence for the end of the section. */
1812 frag = last_frag_for_seg (seg);
1813 frag_ofs = get_frag_fix (frag, seg);
1814 if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1815 out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
1816 else
1817 {
1818 lab = symbol_temp_new (seg, frag_ofs, frag);
1819 relax_inc_line_addr (INT_MAX, lab, last_lab);
1820 }
1821 }
1822
1823 /* Emit the directory and file tables for .debug_line. */
1824
1825 static void
out_file_list(void)1826 out_file_list (void)
1827 {
1828 size_t size;
1829 const char *dir;
1830 char *cp;
1831 unsigned int i;
1832
1833 /* Emit directory list. */
1834 for (i = 1; i < dirs_in_use; ++i)
1835 {
1836 dir = remap_debug_filename (dirs[i]);
1837 size = strlen (dir) + 1;
1838 cp = frag_more (size);
1839 memcpy (cp, dir, size);
1840 }
1841 /* Terminate it. */
1842 out_byte ('\0');
1843
1844 for (i = 1; i < files_in_use; ++i)
1845 {
1846 const char *fullfilename;
1847
1848 if (files[i].filename == NULL)
1849 {
1850 as_bad (_("unassigned file number %ld"), (long) i);
1851 /* Prevent a crash later, particularly for file 1. */
1852 files[i].filename = "";
1853 continue;
1854 }
1855
1856 fullfilename = DWARF2_FILE_NAME (files[i].filename,
1857 files[i].dir ? dirs [files [i].dir] : "");
1858 size = strlen (fullfilename) + 1;
1859 cp = frag_more (size);
1860 memcpy (cp, fullfilename, size);
1861
1862 out_uleb128 (files[i].dir); /* directory number */
1863 /* Output the last modification timestamp. */
1864 out_uleb128 (DWARF2_FILE_TIME_NAME (files[i].filename,
1865 files[i].dir ? dirs [files [i].dir] : ""));
1866 /* Output the filesize. */
1867 out_uleb128 (DWARF2_FILE_SIZE_NAME (files[i].filename,
1868 files[i].dir ? dirs [files [i].dir] : ""));
1869 }
1870
1871 /* Terminate filename list. */
1872 out_byte (0);
1873 }
1874
1875 /* Add a string to the string table. */
1876
1877 static offsetT
add_to_string_table(struct string_table * strtab,const char * str)1878 add_to_string_table (struct string_table *strtab, const char *str)
1879 {
1880 const char *key;
1881 offsetT val;
1882
1883 if (strtab->strings_allocated == 0)
1884 {
1885 strtab->strings_allocated = 4;
1886 strtab->strings = (const char **)
1887 xcalloc (strtab->strings_allocated, sizeof(char *));
1888 strtab->hashtab = hash_new ();
1889 }
1890
1891 val = (offsetT) (uintptr_t)hash_find (strtab->hashtab, str);
1892 if (val != 0)
1893 return val;
1894
1895 if (strtab->strings_in_use >= strtab->strings_allocated)
1896 {
1897 unsigned int old = strtab->strings_allocated;
1898
1899 strtab->strings_allocated *= 2;
1900 strtab->strings = (const char **)
1901 xrealloc (strtab->strings,
1902 strtab->strings_allocated * sizeof (char *));
1903 memset (strtab->strings + old, 0,
1904 (strtab->strings_allocated - old) * sizeof (char *));
1905 }
1906
1907 key = xstrdup (str);
1908 val = strtab->next_offset;
1909 hash_insert (strtab->hashtab, key, (void *) (uintptr_t)val);
1910 strtab->strings[strtab->strings_in_use++] = key;
1911 strtab->next_offset += strlen(key) + 1;
1912 return val;
1913 }
1914
1915 /* Output the string table STRTAB to the section STR_SEG.
1916 In a debug string table, the first byte is always '\0',
1917 and valid indexes begin at 1. */
1918
1919 static void
out_string_table(segT str_seg,struct string_table * strtab)1920 out_string_table (segT str_seg, struct string_table *strtab)
1921 {
1922 unsigned int i;
1923 size_t size;
1924 char *cp;
1925
1926 subseg_set (str_seg, 0);
1927 out_byte (0);
1928 for (i = 0; i < strtab->strings_in_use; i++)
1929 {
1930 size = strlen (strtab->strings[i]) + 1;
1931 cp = frag_more (size);
1932 memcpy (cp, strtab->strings[i], size);
1933 }
1934 }
1935
1936 static void
out_dwarf5_file_list(segT str_seg,int sizeof_offset)1937 out_dwarf5_file_list (segT str_seg, int sizeof_offset)
1938 {
1939 const char *dir;
1940 offsetT strp;
1941 unsigned int i;
1942 expressionS exp;
1943 unsigned int dir_count = dirs_in_use > 0 ? dirs_in_use - 1 : 0;
1944 unsigned int file_count = files_in_use > 0 ? files_in_use - 1 : 0;
1945
1946 exp.X_op = O_symbol;
1947 exp.X_add_symbol = section_symbol (str_seg);
1948
1949 out_byte (1); /* directory_entry_format_count */
1950 out_uleb128 (DW_LNCT_path); /* directory_entry_format[0].content_type */
1951 out_uleb128 (DW_FORM_line_strp); /* directory_entry_format[0].form */
1952 out_uleb128 (dir_count); /* directories_count */
1953
1954 /* Emit directories list. */
1955 for (i = 1; i < dirs_in_use; ++i)
1956 {
1957 dir = remap_debug_filename (dirs[i]);
1958 strp = add_to_string_table (&debug_line_str_table, dir);
1959 exp.X_add_number = strp;
1960 emit_expr (&exp, sizeof_offset);
1961 }
1962
1963 out_byte (2); /* file_name_entry_format_count */
1964 out_uleb128 (DW_LNCT_path); /* file_name_entry_format[0].type */
1965 out_uleb128 (DW_FORM_line_strp); /* file_name_entry_format[0].form */
1966 out_uleb128 (DW_LNCT_directory_index); /* file_name_entry_format[0].type */
1967 out_uleb128 (DW_FORM_udata); /* file_name_entry_format[0].form */
1968 out_uleb128 (file_count); /* file_names_count */
1969
1970 /* Emit file_names list. */
1971 for (i = 1; i < files_in_use; ++i)
1972 {
1973 const char *fullfilename;
1974
1975 if (files[i].filename == NULL)
1976 {
1977 as_bad (_("unassigned file number %ld"), (long) i);
1978 /* Prevent a crash later, particularly for file 1. */
1979 files[i].filename = "";
1980 }
1981
1982 fullfilename = DWARF2_FILE_NAME (files[i].filename,
1983 files[i].dir ? dirs [files [i].dir] : "");
1984 strp = add_to_string_table (&debug_line_str_table, fullfilename);
1985 exp.X_add_number = strp;
1986 emit_expr (&exp, sizeof_offset);
1987 out_uleb128 (files[i].dir); /* directory number */
1988 }
1989 }
1990
1991 static void
out_subprog_list(segT str_seg,int sizeof_offset)1992 out_subprog_list (segT str_seg, int sizeof_offset)
1993 {
1994 const char *name;
1995 offsetT strp;
1996 unsigned int i;
1997 expressionS exp;
1998
1999 exp.X_op = O_symbol;
2000 exp.X_add_symbol = section_symbol (str_seg);
2001
2002 out_byte (3); /* subprogram_entry_format_count */
2003 out_uleb128 (DW_LNCT_subprogram_name); /* subprogram_entry_format[0].type */
2004 out_uleb128 (DW_FORM_line_strp); /* subprogram_entry_format[0].form */
2005 out_uleb128 (DW_LNCT_decl_file); /* subprogram_entry_format[1].type */
2006 out_uleb128 (DW_FORM_udata); /* subprogram_entry_format[1].form */
2007 out_uleb128 (DW_LNCT_decl_line); /* subprogram_entry_format[2].type */
2008 out_uleb128 (DW_FORM_udata); /* subprogram_entry_format[2].form */
2009 out_uleb128 (subprogs_in_use); /* subprograms_count */
2010
2011 /* Emit subprograms list. */
2012 for (i = 0; i < subprogs_in_use; ++i)
2013 {
2014 name = subprogs[i].subpname;
2015 if (name == NULL)
2016 {
2017 as_bad (_("unassigned subprogram number %ld"), (long) i);
2018 strp = 0;
2019 }
2020 else
2021 strp = add_to_string_table (&debug_line_str_table, name);
2022 exp.X_add_number = strp;
2023 emit_expr (&exp, sizeof_offset);
2024 out_uleb128 (subprogs[i].filenum);
2025 out_uleb128 (subprogs[i].line);
2026 }
2027 }
2028
2029 /* Switch to SEC and output a header length field. Return the size of
2030 offsets used in SEC. The caller must set EXPR->X_add_symbol value
2031 to the end of the section. */
2032
2033 static int
out_header(asection * sec,expressionS * exp)2034 out_header (asection *sec, expressionS *exp)
2035 {
2036 symbolS *start_sym;
2037 symbolS *end_sym;
2038
2039 subseg_set (sec, 0);
2040 start_sym = symbol_temp_new_now ();
2041 end_sym = symbol_temp_make ();
2042
2043 /* Total length of the information. */
2044 exp->X_op = O_subtract;
2045 exp->X_add_symbol = end_sym;
2046 exp->X_op_symbol = start_sym;
2047
2048 switch (DWARF2_FORMAT (sec))
2049 {
2050 case dwarf2_format_32bit:
2051 exp->X_add_number = -4;
2052 emit_expr (exp, 4);
2053 return 4;
2054
2055 case dwarf2_format_64bit:
2056 exp->X_add_number = -12;
2057 out_four (-1);
2058 emit_expr (exp, 8);
2059 return 8;
2060
2061 case dwarf2_format_64bit_irix:
2062 exp->X_add_number = -8;
2063 emit_expr (exp, 8);
2064 return 8;
2065 }
2066
2067 as_fatal (_("internal error: unknown dwarf2 format"));
2068 return 0;
2069 }
2070
2071 /* Emit the collected .debug_line data. */
2072
2073 static void
out_debug_line(segT line_seg,segT str_seg)2074 out_debug_line (segT line_seg, segT str_seg)
2075 {
2076 expressionS exp;
2077 symbolS *prologue_start, *prologue_end, *logicals_start, *actuals_start;
2078 symbolS *line_end;
2079 struct line_seg *s;
2080 int sizeof_offset;
2081 unsigned int version;
2082
2083 if (logicals_in_use == 0)
2084 {
2085 version = DWARF2_LINE_VERSION;
2086 opcode_base = DWARF2_LINE_OPCODE_BASE;
2087 line_base = DWARF2_LINE_BASE;
2088 line_range = DWARF2_LINE_RANGE;
2089 }
2090 else
2091 {
2092 version = DWARF2_LINE_EXPERIMENTAL_VERSION;
2093 opcode_base = DWARF5_EXPERIMENTAL_LINE_OPCODE_BASE;
2094 line_base = DWARF5_EXPERIMENTAL_LINE_BASE;
2095 line_range = DWARF5_EXPERIMENTAL_LINE_RANGE;
2096 }
2097
2098 sizeof_offset = out_header (line_seg, &exp);
2099 line_end = exp.X_add_symbol;
2100
2101 /* Version. */
2102 out_two (version);
2103
2104 /* Length of the prologue following this length. */
2105 prologue_start = symbol_temp_make ();
2106 prologue_end = symbol_temp_make ();
2107 exp.X_op = O_subtract;
2108 exp.X_add_symbol = prologue_end;
2109 exp.X_op_symbol = prologue_start;
2110 exp.X_add_number = 0;
2111 emit_expr (&exp, sizeof_offset);
2112 symbol_set_value_now (prologue_start);
2113
2114 /* Parameters of the state machine. */
2115 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
2116 if (version >= 4)
2117 out_byte (DWARF2_LINE_MAX_OPS_PER_INSN);
2118 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
2119 out_byte (line_base);
2120 out_byte (line_range);
2121 out_byte (opcode_base);
2122
2123 /* Standard opcode lengths. */
2124 out_byte (0); /* DW_LNS_copy */
2125 out_byte (1); /* DW_LNS_advance_pc */
2126 out_byte (1); /* DW_LNS_advance_line */
2127 out_byte (1); /* DW_LNS_set_file */
2128 out_byte (1); /* DW_LNS_set_column */
2129 out_byte (0); /* DW_LNS_negate_stmt */
2130 out_byte (0); /* DW_LNS_set_basic_block */
2131 out_byte (0); /* DW_LNS_const_add_pc */
2132 out_byte (1); /* DW_LNS_fixed_advance_pc */
2133 out_byte (0); /* DW_LNS_set_prologue_end */
2134 out_byte (0); /* DW_LNS_set_epilogue_begin */
2135 out_byte (1); /* DW_LNS_set_isa */
2136 if (opcode_base == DWARF5_EXPERIMENTAL_LINE_OPCODE_BASE)
2137 {
2138 out_byte (1); /* DW_LNS_set_subprogram/DW_LNS_set_address_from_logical */
2139 out_byte (2); /* DW_LNS_inlined_call */
2140 out_byte (0); /* DW_LNS_pop_context */
2141 }
2142
2143 if (version == DWARF2_LINE_EXPERIMENTAL_VERSION)
2144 {
2145 /* Fake empty version 4 directory and filename lists, to fool
2146 old consumers who don't check the version number. */
2147 out_byte (0);
2148 out_byte (0);
2149
2150 symbol_set_value_now (prologue_end);
2151
2152 /* Now wrap the remainder of the section inside a fake
2153 extended opcode, so old consumers will see just the single
2154 extended opcode, and will not try to read anything else.
2155 For simplicity, we simply output a very large number for
2156 the size of the extended op. */
2157 out_opcode (DW_LNS_extended_op);
2158 out_byte (255); /* 3-byte LEB128 for 0x1fffff. */
2159 out_byte (255);
2160 out_byte (127);
2161 out_byte (127); /* Fake extended opcode. */
2162
2163 /* Logicals table offset. */
2164 logicals_start = symbol_temp_make ();
2165 exp.X_add_symbol = logicals_start;
2166 emit_expr (&exp, sizeof_offset);
2167
2168 /* Actuals table offset. */
2169 actuals_start = symbol_temp_make ();
2170 exp.X_add_symbol = actuals_start;
2171 emit_expr (&exp, sizeof_offset);
2172
2173 /* Directory and filename lists. */
2174 out_dwarf5_file_list (str_seg, sizeof_offset);
2175
2176 /* Subprogram list. */
2177 out_subprog_list (str_seg, sizeof_offset);
2178
2179 symbol_set_value_now (logicals_start);
2180 emit_logicals ();
2181 symbol_set_value_now (actuals_start);
2182 }
2183 else if (version >= 5)
2184 {
2185 out_dwarf5_file_list (str_seg, sizeof_offset);
2186 symbol_set_value_now (prologue_end);
2187 }
2188 else
2189 {
2190 out_file_list ();
2191 symbol_set_value_now (prologue_end);
2192 }
2193
2194 /* For each section, emit a statement program. */
2195 for (s = all_segs; s; s = s->next)
2196 if (SEG_NORMAL (s->seg))
2197 process_entries (s->seg, s->head->head);
2198 else
2199 as_warn ("dwarf line number information for %s ignored",
2200 segment_name (s->seg));
2201
2202 if (flag_dwarf_sections)
2203 /* We have to switch to the special .debug_line_end section
2204 before emitting the end-of-debug_line symbol. The linker
2205 script arranges for this section to be placed after all the
2206 (potentially garbage collected) .debug_line.<foo> sections.
2207 This section contains the line_end symbol which is used to
2208 compute the size of the linked .debug_line section, as seen
2209 in the DWARF Line Number header. */
2210 subseg_set (subseg_get (".debug_line_end", FALSE), 0);
2211
2212 symbol_set_value_now (line_end);
2213 }
2214
2215 static void
out_debug_ranges(segT ranges_seg)2216 out_debug_ranges (segT ranges_seg)
2217 {
2218 unsigned int addr_size = sizeof_address;
2219 struct line_seg *s;
2220 expressionS exp;
2221 unsigned int i;
2222
2223 subseg_set (ranges_seg, 0);
2224
2225 /* Base Address Entry. */
2226 for (i = 0; i < addr_size; i++)
2227 out_byte (0xff);
2228 for (i = 0; i < addr_size; i++)
2229 out_byte (0);
2230
2231 /* Range List Entry. */
2232 for (s = all_segs; s; s = s->next)
2233 {
2234 fragS *frag;
2235 symbolS *beg, *end;
2236
2237 frag = first_frag_for_seg (s->seg);
2238 beg = symbol_temp_new (s->seg, 0, frag);
2239 s->text_start = beg;
2240
2241 frag = last_frag_for_seg (s->seg);
2242 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
2243 s->text_end = end;
2244
2245 exp.X_op = O_symbol;
2246 exp.X_add_symbol = beg;
2247 exp.X_add_number = 0;
2248 emit_expr (&exp, addr_size);
2249
2250 exp.X_op = O_symbol;
2251 exp.X_add_symbol = end;
2252 exp.X_add_number = 0;
2253 emit_expr (&exp, addr_size);
2254 }
2255
2256 /* End of Range Entry. */
2257 for (i = 0; i < addr_size; i++)
2258 out_byte (0);
2259 for (i = 0; i < addr_size; i++)
2260 out_byte (0);
2261 }
2262
2263 /* Emit data for .debug_aranges. */
2264
2265 static void
out_debug_aranges(segT aranges_seg,segT info_seg)2266 out_debug_aranges (segT aranges_seg, segT info_seg)
2267 {
2268 unsigned int addr_size = sizeof_address;
2269 struct line_seg *s;
2270 expressionS exp;
2271 symbolS *aranges_end;
2272 char *p;
2273 int sizeof_offset;
2274
2275 sizeof_offset = out_header (aranges_seg, &exp);
2276 aranges_end = exp.X_add_symbol;
2277
2278 /* Version. */
2279 out_two (DWARF2_ARANGES_VERSION);
2280
2281 /* Offset to .debug_info. */
2282 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset);
2283
2284 /* Size of an address (offset portion). */
2285 out_byte (addr_size);
2286
2287 /* Size of a segment descriptor. */
2288 out_byte (0);
2289
2290 /* Align the header. */
2291 frag_align (ffs (2 * addr_size) - 1, 0, 0);
2292
2293 for (s = all_segs; s; s = s->next)
2294 {
2295 fragS *frag;
2296 symbolS *beg, *end;
2297
2298 frag = first_frag_for_seg (s->seg);
2299 beg = symbol_temp_new (s->seg, 0, frag);
2300 s->text_start = beg;
2301
2302 frag = last_frag_for_seg (s->seg);
2303 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
2304 s->text_end = end;
2305
2306 exp.X_op = O_symbol;
2307 exp.X_add_symbol = beg;
2308 exp.X_add_number = 0;
2309 emit_expr (&exp, addr_size);
2310
2311 exp.X_op = O_subtract;
2312 exp.X_add_symbol = end;
2313 exp.X_op_symbol = beg;
2314 exp.X_add_number = 0;
2315 emit_expr (&exp, addr_size);
2316 }
2317
2318 p = frag_more (2 * addr_size);
2319 md_number_to_chars (p, 0, addr_size);
2320 md_number_to_chars (p + addr_size, 0, addr_size);
2321
2322 symbol_set_value_now (aranges_end);
2323 }
2324
2325 /* Emit data for .debug_abbrev. Note that this must be kept in
2326 sync with out_debug_info below. */
2327
2328 static void
out_debug_abbrev(segT abbrev_seg,segT info_seg ATTRIBUTE_UNUSED,segT line_seg ATTRIBUTE_UNUSED)2329 out_debug_abbrev (segT abbrev_seg,
2330 segT info_seg ATTRIBUTE_UNUSED,
2331 segT line_seg ATTRIBUTE_UNUSED)
2332 {
2333 subseg_set (abbrev_seg, 0);
2334
2335 out_uleb128 (1);
2336 out_uleb128 (DW_TAG_compile_unit);
2337 out_byte (DW_CHILDREN_no);
2338 if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit)
2339 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
2340 else
2341 out_abbrev (DW_AT_stmt_list, DW_FORM_data8);
2342 if (all_segs->next == NULL)
2343 {
2344 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
2345 if (DWARF2_VERSION < 4)
2346 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
2347 else
2348 out_abbrev (DW_AT_high_pc, (sizeof_address == 4
2349 ? DW_FORM_data4 : DW_FORM_data8));
2350 }
2351 else
2352 {
2353 if (DWARF2_FORMAT (info_seg) == dwarf2_format_32bit)
2354 out_abbrev (DW_AT_ranges, DW_FORM_data4);
2355 else
2356 out_abbrev (DW_AT_ranges, DW_FORM_data8);
2357 }
2358 out_abbrev (DW_AT_name, DW_FORM_string);
2359 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
2360 out_abbrev (DW_AT_producer, DW_FORM_string);
2361 out_abbrev (DW_AT_language, DW_FORM_data2);
2362 out_abbrev (0, 0);
2363
2364 /* Terminate the abbreviations for this compilation unit. */
2365 out_byte (0);
2366 }
2367
2368 /* Emit a description of this compilation unit for .debug_info. */
2369
2370 static void
out_debug_info(segT info_seg,segT abbrev_seg,segT line_seg,segT ranges_seg)2371 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg)
2372 {
2373 char producer[128];
2374 const char *comp_dir;
2375 const char *dirname;
2376 expressionS exp;
2377 symbolS *info_end;
2378 char *p;
2379 int len;
2380 int sizeof_offset;
2381
2382 sizeof_offset = out_header (info_seg, &exp);
2383 info_end = exp.X_add_symbol;
2384
2385 /* DWARF version. */
2386 out_two (DWARF2_VERSION);
2387
2388 /* .debug_abbrev offset */
2389 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
2390
2391 /* Target address size. */
2392 out_byte (sizeof_address);
2393
2394 /* DW_TAG_compile_unit DIE abbrev */
2395 out_uleb128 (1);
2396
2397 /* DW_AT_stmt_list */
2398 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg),
2399 (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit
2400 ? 4 : 8));
2401
2402 /* These two attributes are emitted if all of the code is contiguous. */
2403 if (all_segs->next == NULL)
2404 {
2405 /* DW_AT_low_pc */
2406 exp.X_op = O_symbol;
2407 exp.X_add_symbol = all_segs->text_start;
2408 exp.X_add_number = 0;
2409 emit_expr (&exp, sizeof_address);
2410
2411 /* DW_AT_high_pc */
2412 if (DWARF2_VERSION < 4)
2413 exp.X_op = O_symbol;
2414 else
2415 {
2416 exp.X_op = O_subtract;
2417 exp.X_op_symbol = all_segs->text_start;
2418 }
2419 exp.X_add_symbol = all_segs->text_end;
2420 exp.X_add_number = 0;
2421 emit_expr (&exp, sizeof_address);
2422 }
2423 else
2424 {
2425 /* This attribute is emitted if the code is disjoint. */
2426 /* DW_AT_ranges. */
2427 TC_DWARF2_EMIT_OFFSET (section_symbol (ranges_seg), sizeof_offset);
2428 }
2429
2430 /* DW_AT_name. We don't have the actual file name that was present
2431 on the command line, so assume files[1] is the main input file.
2432 We're not supposed to get called unless at least one line number
2433 entry was emitted, so this should always be defined. */
2434 if (files_in_use == 0)
2435 abort ();
2436 if (files[1].dir)
2437 {
2438 dirname = remap_debug_filename (dirs[files[1].dir]);
2439 len = strlen (dirname);
2440 #ifdef TE_VMS
2441 /* Already has trailing slash. */
2442 p = frag_more (len);
2443 memcpy (p, dirname, len);
2444 #else
2445 p = frag_more (len + 1);
2446 memcpy (p, dirname, len);
2447 INSERT_DIR_SEPARATOR (p, len);
2448 #endif
2449 }
2450 len = strlen (files[1].filename) + 1;
2451 p = frag_more (len);
2452 memcpy (p, files[1].filename, len);
2453
2454 /* DW_AT_comp_dir */
2455 comp_dir = remap_debug_filename (getpwd ());
2456 len = strlen (comp_dir) + 1;
2457 p = frag_more (len);
2458 memcpy (p, comp_dir, len);
2459
2460 /* DW_AT_producer */
2461 sprintf (producer, "GNU AS %s", VERSION);
2462 len = strlen (producer) + 1;
2463 p = frag_more (len);
2464 memcpy (p, producer, len);
2465
2466 /* DW_AT_language. Yes, this is probably not really MIPS, but the
2467 dwarf2 draft has no standard code for assembler. */
2468 out_two (DW_LANG_Mips_Assembler);
2469
2470 symbol_set_value_now (info_end);
2471 }
2472
2473 void
dwarf2_init(void)2474 dwarf2_init (void)
2475 {
2476 last_seg_ptr = &all_segs;
2477 }
2478
2479
2480 /* Finish the dwarf2 debug sections. We emit .debug.line if there
2481 were any .file/.loc directives, or --gdwarf2 was given, or if the
2482 file has a non-empty .debug_info section and an empty .debug_line
2483 section. If we emit .debug_line, and the .debug_info section is
2484 empty, we also emit .debug_info, .debug_aranges and .debug_abbrev.
2485 ALL_SEGS will be non-null if there were any .file/.loc directives,
2486 or --gdwarf2 was given and there were any located instructions
2487 emitted. */
2488
2489 void
dwarf2_finish(void)2490 dwarf2_finish (void)
2491 {
2492 segT line_seg;
2493 struct line_seg *s;
2494 segT info_seg;
2495 segT str_seg = NULL;
2496 int emit_other_sections = 0;
2497 int empty_debug_line = 0;
2498
2499 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
2500 emit_other_sections = ((info_seg == NULL || !seg_not_empty_p (info_seg))
2501 && logicals_in_use == 0);
2502
2503 line_seg = bfd_get_section_by_name (stdoutput, ".debug_line");
2504 empty_debug_line = line_seg == NULL || !seg_not_empty_p (line_seg);
2505
2506 /* We can't construct a new debug_line section if we already have one.
2507 Give an error. */
2508 if (all_segs && !empty_debug_line)
2509 as_fatal ("duplicate .debug_line sections");
2510
2511 if ((!all_segs && emit_other_sections)
2512 || (!emit_other_sections && !empty_debug_line))
2513 /* If there is no line information and no non-empty .debug_info
2514 section, or if there is both a non-empty .debug_info and a non-empty
2515 .debug_line, then we do nothing. */
2516 return;
2517
2518 /* Calculate the size of an address for the target machine. */
2519 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
2520
2521 /* Create and switch to the line number section. */
2522 line_seg = subseg_new (".debug_line", 0);
2523 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
2524
2525 /* For each subsection, chain the debug entries together. */
2526 for (s = all_segs; s; s = s->next)
2527 {
2528 struct line_subseg *lss = s->head;
2529 struct line_entry **ptail = lss->ptail;
2530
2531 while ((lss = lss->next) != NULL)
2532 {
2533 *ptail = lss->head;
2534 ptail = lss->ptail;
2535 }
2536 }
2537
2538 if (logicals_in_use > 0)
2539 {
2540 str_seg = subseg_new (".debug_line_str", 0);
2541 bfd_set_section_flags (stdoutput, str_seg,
2542 (SEC_READONLY | SEC_DEBUGGING
2543 | SEC_MERGE | SEC_STRINGS));
2544 str_seg->entsize = 1;
2545 debug_line_str_table.strings = NULL;
2546 debug_line_str_table.strings_in_use = 0;
2547 debug_line_str_table.strings_allocated = 0;
2548 debug_line_str_table.next_offset = 1;
2549 }
2550
2551 out_debug_line (line_seg, str_seg);
2552
2553 if (str_seg != NULL)
2554 out_string_table (str_seg, &debug_line_str_table);
2555
2556 /* If this is assembler generated line info, and there is no
2557 debug_info already, we need .debug_info and .debug_abbrev
2558 sections as well. */
2559 if (emit_other_sections)
2560 {
2561 segT abbrev_seg;
2562 segT aranges_seg;
2563 segT ranges_seg;
2564
2565 gas_assert (all_segs);
2566
2567 info_seg = subseg_new (".debug_info", 0);
2568 abbrev_seg = subseg_new (".debug_abbrev", 0);
2569 aranges_seg = subseg_new (".debug_aranges", 0);
2570
2571 bfd_set_section_flags (stdoutput, info_seg,
2572 SEC_READONLY | SEC_DEBUGGING);
2573 bfd_set_section_flags (stdoutput, abbrev_seg,
2574 SEC_READONLY | SEC_DEBUGGING);
2575 bfd_set_section_flags (stdoutput, aranges_seg,
2576 SEC_READONLY | SEC_DEBUGGING);
2577
2578 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
2579
2580 if (all_segs->next == NULL)
2581 ranges_seg = NULL;
2582 else
2583 {
2584 ranges_seg = subseg_new (".debug_ranges", 0);
2585 bfd_set_section_flags (stdoutput, ranges_seg,
2586 SEC_READONLY | SEC_DEBUGGING);
2587 record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1);
2588 out_debug_ranges (ranges_seg);
2589 }
2590
2591 out_debug_aranges (aranges_seg, info_seg);
2592 out_debug_abbrev (abbrev_seg, info_seg, line_seg);
2593 out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg);
2594 }
2595 }
2596