dwarf2dbg.c revision 1.1.1.3 1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by David Mosberger-Tang <davidm (at) hpl.hp.com>
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 /* Logical line numbers can be controlled by the compiler via the
24 following directives:
25
26 .file FILENO "file.c"
27 .loc FILENO LINENO [COLUMN] [basic_block] [prologue_end] \
28 [epilogue_begin] [is_stmt VALUE] [isa VALUE] \
29 [discriminator VALUE]
30 */
31
32 #include "as.h"
33 #include "safe-ctype.h"
34
35 #ifdef HAVE_LIMITS_H
36 #include <limits.h>
37 #else
38 #ifdef HAVE_SYS_PARAM_H
39 #include <sys/param.h>
40 #endif
41 #ifndef INT_MAX
42 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
43 #endif
44 #endif
45
46 #include "dwarf2dbg.h"
47 #include <filenames.h>
48
49 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
50 /* We need to decide which character to use as a directory separator.
51 Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not
52 necessarily mean that the backslash character is the one to use.
53 Some environments, eg Cygwin, can support both naming conventions.
54 So we use the heuristic that we only need to use the backslash if
55 the path is an absolute path starting with a DOS style drive
56 selector. eg C: or D: */
57 # define INSERT_DIR_SEPARATOR(string, offset) \
58 do \
59 { \
60 if (offset > 1 \
61 && string[0] != 0 \
62 && string[1] == ':') \
63 string [offset] = '\\'; \
64 else \
65 string [offset] = '/'; \
66 } \
67 while (0)
68 #else
69 # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
70 #endif
71
72 #ifndef DWARF2_FORMAT
73 # define DWARF2_FORMAT(SEC) dwarf2_format_32bit
74 #endif
75
76 #ifndef DWARF2_ADDR_SIZE
77 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
78 #endif
79
80 #ifndef DWARF2_FILE_NAME
81 #define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME
82 #endif
83
84 #ifndef DWARF2_FILE_TIME_NAME
85 #define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) 0
86 #endif
87
88 #ifndef DWARF2_FILE_SIZE_NAME
89 #define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) 0
90 #endif
91
92 #ifndef DWARF2_VERSION
93 #define DWARF2_VERSION 2
94 #endif
95
96 /* The .debug_aranges version has been 2 in DWARF version 2, 3 and 4. */
97 #ifndef DWARF2_ARANGES_VERSION
98 #define DWARF2_ARANGES_VERSION 2
99 #endif
100
101 /* This implementation output version 2 .debug_line information. */
102 #ifndef DWARF2_LINE_VERSION
103 #define DWARF2_LINE_VERSION 2
104 #endif
105
106 #include "subsegs.h"
107
108 #include "dwarf2.h"
109
110 /* Since we can't generate the prolog until the body is complete, we
111 use three different subsegments for .debug_line: one holding the
112 prolog, one for the directory and filename info, and one for the
113 body ("statement program"). */
114 #define DL_PROLOG 0
115 #define DL_FILES 1
116 #define DL_BODY 2
117
118 /* If linker relaxation might change offsets in the code, the DWARF special
119 opcodes and variable-length operands cannot be used. If this macro is
120 nonzero, use the DW_LNS_fixed_advance_pc opcode instead. */
121 #ifndef DWARF2_USE_FIXED_ADVANCE_PC
122 # define DWARF2_USE_FIXED_ADVANCE_PC linkrelax
123 #endif
124
125 /* First special line opcde - leave room for the standard opcodes.
126 Note: If you want to change this, you'll have to update the
127 "standard_opcode_lengths" table that is emitted below in
128 out_debug_line(). */
129 #define DWARF2_LINE_OPCODE_BASE 13
130
131 #ifndef DWARF2_LINE_BASE
132 /* Minimum line offset in a special line info. opcode. This value
133 was chosen to give a reasonable range of values. */
134 # define DWARF2_LINE_BASE -5
135 #endif
136
137 /* Range of line offsets in a special line info. opcode. */
138 #ifndef DWARF2_LINE_RANGE
139 # define DWARF2_LINE_RANGE 14
140 #endif
141
142 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
143 /* Define the architecture-dependent minimum instruction length (in
144 bytes). This value should be rather too small than too big. */
145 # define DWARF2_LINE_MIN_INSN_LENGTH 1
146 #endif
147
148 /* Flag that indicates the initial value of the is_stmt_start flag. */
149 #define DWARF2_LINE_DEFAULT_IS_STMT 1
150
151 /* Given a special op, return the line skip amount. */
152 #define SPECIAL_LINE(op) \
153 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
154
155 /* Given a special op, return the address skip amount (in units of
156 DWARF2_LINE_MIN_INSN_LENGTH. */
157 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
158
159 /* The maximum address skip amount that can be encoded with a special op. */
160 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
161
162 struct line_entry {
163 struct line_entry *next;
164 symbolS *label;
165 struct dwarf2_line_info loc;
166 };
167
168 struct line_subseg {
169 struct line_subseg *next;
170 subsegT subseg;
171 struct line_entry *head;
172 struct line_entry **ptail;
173 };
174
175 struct line_seg {
176 struct line_seg *next;
177 segT seg;
178 struct line_subseg *head;
179 symbolS *text_start;
180 symbolS *text_end;
181 };
182
183 /* Collects data for all line table entries during assembly. */
184 static struct line_seg *all_segs;
185 /* Hash used to quickly lookup a segment by name, avoiding the need to search
186 through the all_segs list. */
187 static struct hash_control *all_segs_hash;
188 static struct line_seg **last_seg_ptr;
189
190 struct file_entry {
191 const char *filename;
192 unsigned int dir;
193 };
194
195 /* Table of files used by .debug_line. */
196 static struct file_entry *files;
197 static unsigned int files_in_use;
198 static unsigned int files_allocated;
199
200 /* Table of directories used by .debug_line. */
201 static char **dirs;
202 static unsigned int dirs_in_use;
203 static unsigned int dirs_allocated;
204
205 /* TRUE when we've seen a .loc directive recently. Used to avoid
206 doing work when there's nothing to do. */
207 bfd_boolean dwarf2_loc_directive_seen;
208
209 /* TRUE when we're supposed to set the basic block mark whenever a
210 label is seen. */
211 bfd_boolean dwarf2_loc_mark_labels;
212
213 /* Current location as indicated by the most recent .loc directive. */
214 static struct dwarf2_line_info current = {
215 1, 1, 0, 0,
216 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0,
217 0
218 };
219
220 /* Lines that are at the same location as CURRENT, and which are waiting
221 for a label. */
222 static struct line_entry *pending_lines, **pending_lines_tail = &pending_lines;
223
224 /* The size of an address on the target. */
225 static unsigned int sizeof_address;
226
227 static unsigned int get_filenum (const char *, unsigned int);
229
230 #ifndef TC_DWARF2_EMIT_OFFSET
231 #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
232
233 /* Create an offset to .dwarf2_*. */
234
235 static void
236 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
237 {
238 expressionS exp;
239
240 exp.X_op = O_symbol;
241 exp.X_add_symbol = symbol;
242 exp.X_add_number = 0;
243 emit_expr (&exp, size);
244 }
245 #endif
246
247 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
248
249 static struct line_subseg *
250 get_line_subseg (segT seg, subsegT subseg)
251 {
252 static segT last_seg;
253 static subsegT last_subseg;
254 static struct line_subseg *last_line_subseg;
255
256 struct line_seg *s;
257 struct line_subseg **pss, *lss;
258
259 if (seg == last_seg && subseg == last_subseg)
260 return last_line_subseg;
261
262 s = (struct line_seg *) hash_find (all_segs_hash, seg->name);
263 if (s == NULL)
264 {
265 s = (struct line_seg *) xmalloc (sizeof (*s));
266 s->next = NULL;
267 s->seg = seg;
268 s->head = NULL;
269 *last_seg_ptr = s;
270 last_seg_ptr = &s->next;
271 hash_insert (all_segs_hash, seg->name, s);
272 }
273 gas_assert (seg == s->seg);
274
275 for (pss = &s->head; (lss = *pss) != NULL ; pss = &lss->next)
276 {
277 if (lss->subseg == subseg)
278 goto found_subseg;
279 if (lss->subseg > subseg)
280 break;
281 }
282
283 lss = (struct line_subseg *) xmalloc (sizeof (*lss));
284 lss->next = *pss;
285 lss->subseg = subseg;
286 lss->head = NULL;
287 lss->ptail = &lss->head;
288 *pss = lss;
289
290 found_subseg:
291 last_seg = seg;
292 last_subseg = subseg;
293 last_line_subseg = lss;
294
295 return lss;
296 }
297
298 /* Push LOC onto the pending lines list. */
299
300 static void
301 dwarf2_push_line (struct dwarf2_line_info *loc)
302 {
303 struct line_entry *e;
304
305 e = (struct line_entry *) xmalloc (sizeof (*e));
306 e->next = NULL;
307 e->label = NULL;
308 e->loc = *loc;
309
310 *pending_lines_tail = e;
311 pending_lines_tail = &(*pending_lines_tail)->next;
312 }
313
314 /* Emit all pending line information. LABEL is the label with which the
315 lines should be associated, or null if they should be associated with
316 the current position. */
317
318 static void
319 dwarf2_flush_pending_lines (symbolS *label)
320 {
321 if (pending_lines)
322 {
323 struct line_subseg *lss;
324 struct line_entry *e;
325
326 if (!label)
327 label = symbol_temp_new_now ();
328
329 for (e = pending_lines; e; e = e->next)
330 e->label = label;
331
332 lss = get_line_subseg (now_seg, now_subseg);
333 *lss->ptail = pending_lines;
334 lss->ptail = pending_lines_tail;
335
336 pending_lines = NULL;
337 pending_lines_tail = &pending_lines;
338 }
339 }
340
341 /* Record an entry for LOC occurring at OFS within the current fragment. */
342
343 void
344 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
345 {
346 static unsigned int line = -1;
347 static unsigned int filenum = -1;
348
349 /* Early out for as-yet incomplete location information. */
350 if (loc->filenum == 0 || loc->line == 0)
351 return;
352
353 /* Don't emit sequences of line symbols for the same line when the
354 symbols apply to assembler code. It is necessary to emit
355 duplicate line symbols when a compiler asks for them, because GDB
356 uses them to determine the end of the prologue. */
357 if (debug_type == DEBUG_DWARF2
358 && line == loc->line && filenum == loc->filenum)
359 return;
360
361 line = loc->line;
362 filenum = loc->filenum;
363
364 dwarf2_push_line (loc);
365 if (linkrelax)
366 {
367 char name[120];
368
369 /* Use a non-fake name for the line number location,
370 so that it can be referred to by relocations. */
371 sprintf (name, ".Loc.%u.%u", line, filenum);
372 dwarf2_flush_pending_lines (symbol_new (name, now_seg, ofs, frag_now));
373 }
374 else
375 dwarf2_flush_pending_lines (symbol_temp_new (now_seg, ofs, frag_now));
376 }
377
378 /* Returns the current source information. If .file directives have
379 been encountered, the info for the corresponding source file is
380 returned. Otherwise, the info for the assembly source file is
381 returned. */
382
383 void
384 dwarf2_where (struct dwarf2_line_info *line)
385 {
386 if (debug_type == DEBUG_DWARF2)
387 {
388 char *filename;
389 as_where (&filename, &line->line);
390 line->filenum = get_filenum (filename, 0);
391 line->column = 0;
392 line->flags = DWARF2_FLAG_IS_STMT;
393 line->isa = current.isa;
394 line->discriminator = current.discriminator;
395 }
396 else
397 *line = current;
398 }
399
400 /* A hook to allow the target backend to inform the line number state
401 machine of isa changes when assembler debug info is enabled. */
402
403 void
404 dwarf2_set_isa (unsigned int isa)
405 {
406 current.isa = isa;
407 }
408
409 /* Called for each machine instruction, or relatively atomic group of
410 machine instructions (ie built-in macro). The instruction or group
411 is SIZE bytes in length. If dwarf2 line number generation is called
412 for, emit a line statement appropriately. */
413
414 void
415 dwarf2_emit_insn (int size)
416 {
417 struct dwarf2_line_info loc;
418
419 if (!dwarf2_loc_directive_seen && debug_type != DEBUG_DWARF2)
420 return;
421
422 dwarf2_where (&loc);
423
424 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
425 dwarf2_consume_line_info ();
426 }
427
428 /* Called after the current line information has been either used with
429 dwarf2_gen_line_info or saved with a machine instruction for later use.
430 This resets the state of the line number information to reflect that
431 it has been used. */
432
433 void
434 dwarf2_consume_line_info (void)
435 {
436 /* If the consumer has stashed the current location away for later use,
437 assume that any earlier location information should be associated
438 with ".". */
439 dwarf2_flush_pending_lines (NULL);
440
441 /* Unless we generate DWARF2 debugging information for each
442 assembler line, we only emit one line symbol for one LOC. */
443 dwarf2_loc_directive_seen = FALSE;
444
445 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
446 | DWARF2_FLAG_PROLOGUE_END
447 | DWARF2_FLAG_EPILOGUE_BEGIN);
448 current.discriminator = 0;
449 }
450
451 /* Called for each (preferably code) label. If dwarf2_loc_mark_labels
452 is enabled, emit a basic block marker. */
453
454 void
455 dwarf2_emit_label (symbolS *label)
456 {
457 struct dwarf2_line_info loc;
458
459 if (!dwarf2_loc_mark_labels)
460 return;
461 if (S_GET_SEGMENT (label) != now_seg)
462 return;
463 if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE))
464 return;
465 if (files_in_use == 0 && debug_type != DEBUG_DWARF2)
466 return;
467
468 dwarf2_where (&loc);
469
470 loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
471
472 dwarf2_push_line (&loc);
473 dwarf2_flush_pending_lines (label);
474 dwarf2_consume_line_info ();
475 }
476
477 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
478 allocate it on that file table slot, otherwise return the first
479 empty one. */
480
481 static unsigned int
482 get_filenum (const char *filename, unsigned int num)
483 {
484 static unsigned int last_used, last_used_dir_len;
485 const char *file;
486 size_t dir_len;
487 unsigned int i, dir;
488
489 if (num == 0 && last_used)
490 {
491 if (! files[last_used].dir
492 && filename_cmp (filename, files[last_used].filename) == 0)
493 return last_used;
494 if (files[last_used].dir
495 && filename_ncmp (filename, dirs[files[last_used].dir],
496 last_used_dir_len) == 0
497 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
498 && filename_cmp (filename + last_used_dir_len + 1,
499 files[last_used].filename) == 0)
500 return last_used;
501 }
502
503 file = lbasename (filename);
504 /* Don't make empty string from / or A: from A:/ . */
505 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
506 if (file <= filename + 3)
507 file = filename;
508 #else
509 if (file == filename + 1)
510 file = filename;
511 #endif
512 dir_len = file - filename;
513
514 dir = 0;
515 if (dir_len)
516 {
517 #ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR
518 --dir_len;
519 #endif
520 for (dir = 1; dir < dirs_in_use; ++dir)
521 if (filename_ncmp (filename, dirs[dir], dir_len) == 0
522 && dirs[dir][dir_len] == '\0')
523 break;
524
525 if (dir >= dirs_in_use)
526 {
527 if (dir >= dirs_allocated)
528 {
529 dirs_allocated = dir + 32;
530 dirs = (char **)
531 xrealloc (dirs, (dir + 32) * sizeof (const char *));
532 }
533
534 dirs[dir] = (char *) xmalloc (dir_len + 1);
535 memcpy (dirs[dir], filename, dir_len);
536 dirs[dir][dir_len] = '\0';
537 dirs_in_use = dir + 1;
538 }
539 }
540
541 if (num == 0)
542 {
543 for (i = 1; i < files_in_use; ++i)
544 if (files[i].dir == dir
545 && files[i].filename
546 && filename_cmp (file, files[i].filename) == 0)
547 {
548 last_used = i;
549 last_used_dir_len = dir_len;
550 return i;
551 }
552 }
553 else
554 i = num;
555
556 if (i >= files_allocated)
557 {
558 unsigned int old = files_allocated;
559
560 files_allocated = i + 32;
561 files = (struct file_entry *)
562 xrealloc (files, (i + 32) * sizeof (struct file_entry));
563
564 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
565 }
566
567 files[i].filename = num ? file : xstrdup (file);
568 files[i].dir = dir;
569 if (files_in_use < i + 1)
570 files_in_use = i + 1;
571 last_used = i;
572 last_used_dir_len = dir_len;
573
574 return i;
575 }
576
577 /* Handle two forms of .file directive:
578 - Pass .file "source.c" to s_app_file
579 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
580
581 If an entry is added to the file table, return a pointer to the filename. */
582
583 char *
584 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
585 {
586 offsetT num;
587 char *filename;
588 int filename_len;
589
590 /* Continue to accept a bare string and pass it off. */
591 SKIP_WHITESPACE ();
592 if (*input_line_pointer == '"')
593 {
594 s_app_file (0);
595 return NULL;
596 }
597
598 num = get_absolute_expression ();
599 filename = demand_copy_C_string (&filename_len);
600 if (filename == NULL)
601 return NULL;
602 demand_empty_rest_of_line ();
603
604 if (num < 1)
605 {
606 as_bad (_("file number less than one"));
607 return NULL;
608 }
609
610 /* A .file directive implies compiler generated debug information is
611 being supplied. Turn off gas generated debug info. */
612 debug_type = DEBUG_NONE;
613
614 if (num < (int) files_in_use && files[num].filename != 0)
615 {
616 as_bad (_("file number %ld already allocated"), (long) num);
617 return NULL;
618 }
619
620 get_filenum (filename, num);
621
622 return filename;
623 }
624
625 void
626 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
627 {
628 offsetT filenum, line;
629
630 /* If we see two .loc directives in a row, force the first one to be
631 output now. */
632 if (dwarf2_loc_directive_seen)
633 dwarf2_push_line (¤t);
634
635 filenum = get_absolute_expression ();
636 SKIP_WHITESPACE ();
637 line = get_absolute_expression ();
638
639 if (filenum < 1)
640 {
641 as_bad (_("file number less than one"));
642 return;
643 }
644 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
645 {
646 as_bad (_("unassigned file number %ld"), (long) filenum);
647 return;
648 }
649
650 current.filenum = filenum;
651 current.line = line;
652 current.discriminator = 0;
653
654 #ifndef NO_LISTING
655 if (listing)
656 {
657 if (files[filenum].dir)
658 {
659 size_t dir_len = strlen (dirs[files[filenum].dir]);
660 size_t file_len = strlen (files[filenum].filename);
661 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
662
663 memcpy (cp, dirs[files[filenum].dir], dir_len);
664 INSERT_DIR_SEPARATOR (cp, dir_len);
665 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
666 cp[dir_len + file_len + 1] = '\0';
667 listing_source_file (cp);
668 }
669 else
670 listing_source_file (files[filenum].filename);
671 listing_source_line (line);
672 }
673 #endif
674
675 SKIP_WHITESPACE ();
676 if (ISDIGIT (*input_line_pointer))
677 {
678 current.column = get_absolute_expression ();
679 SKIP_WHITESPACE ();
680 }
681
682 while (ISALPHA (*input_line_pointer))
683 {
684 char *p, c;
685 offsetT value;
686
687 p = input_line_pointer;
688 c = get_symbol_end ();
689
690 if (strcmp (p, "basic_block") == 0)
691 {
692 current.flags |= DWARF2_FLAG_BASIC_BLOCK;
693 *input_line_pointer = c;
694 }
695 else if (strcmp (p, "prologue_end") == 0)
696 {
697 current.flags |= DWARF2_FLAG_PROLOGUE_END;
698 *input_line_pointer = c;
699 }
700 else if (strcmp (p, "epilogue_begin") == 0)
701 {
702 current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
703 *input_line_pointer = c;
704 }
705 else if (strcmp (p, "is_stmt") == 0)
706 {
707 *input_line_pointer = c;
708 value = get_absolute_expression ();
709 if (value == 0)
710 current.flags &= ~DWARF2_FLAG_IS_STMT;
711 else if (value == 1)
712 current.flags |= DWARF2_FLAG_IS_STMT;
713 else
714 {
715 as_bad (_("is_stmt value not 0 or 1"));
716 return;
717 }
718 }
719 else if (strcmp (p, "isa") == 0)
720 {
721 *input_line_pointer = c;
722 value = get_absolute_expression ();
723 if (value >= 0)
724 current.isa = value;
725 else
726 {
727 as_bad (_("isa number less than zero"));
728 return;
729 }
730 }
731 else if (strcmp (p, "discriminator") == 0)
732 {
733 *input_line_pointer = c;
734 value = get_absolute_expression ();
735 if (value >= 0)
736 current.discriminator = value;
737 else
738 {
739 as_bad (_("discriminator less than zero"));
740 return;
741 }
742 }
743 else
744 {
745 as_bad (_("unknown .loc sub-directive `%s'"), p);
746 *input_line_pointer = c;
747 return;
748 }
749
750 SKIP_WHITESPACE ();
751 }
752
753 demand_empty_rest_of_line ();
754 dwarf2_loc_directive_seen = TRUE;
755 debug_type = DEBUG_NONE;
756 }
757
758 void
759 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
760 {
761 offsetT value = get_absolute_expression ();
762
763 if (value != 0 && value != 1)
764 {
765 as_bad (_("expected 0 or 1"));
766 ignore_rest_of_line ();
767 }
768 else
769 {
770 dwarf2_loc_mark_labels = value != 0;
771 demand_empty_rest_of_line ();
772 }
773 }
774
775 static struct frag *
777 first_frag_for_seg (segT seg)
778 {
779 return seg_info (seg)->frchainP->frch_root;
780 }
781
782 static struct frag *
783 last_frag_for_seg (segT seg)
784 {
785 frchainS *f = seg_info (seg)->frchainP;
786
787 while (f->frch_next != NULL)
788 f = f->frch_next;
789
790 return f->frch_last;
791 }
792
793 /* Emit a single byte into the current segment. */
795
796 static inline void
797 out_byte (int byte)
798 {
799 FRAG_APPEND_1_CHAR (byte);
800 }
801
802 /* Emit a statement program opcode into the current segment. */
803
804 static inline void
805 out_opcode (int opc)
806 {
807 out_byte (opc);
808 }
809
810 /* Emit a two-byte word into the current segment. */
811
812 static inline void
813 out_two (int data)
814 {
815 md_number_to_chars (frag_more (2), data, 2);
816 }
817
818 /* Emit a four byte word into the current segment. */
819
820 static inline void
821 out_four (int data)
822 {
823 md_number_to_chars (frag_more (4), data, 4);
824 }
825
826 /* Emit an unsigned "little-endian base 128" number. */
827
828 static void
829 out_uleb128 (addressT value)
830 {
831 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
832 }
833
834 /* Emit a signed "little-endian base 128" number. */
835
836 static void
837 out_leb128 (addressT value)
838 {
839 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
840 }
841
842 /* Emit a tuple for .debug_abbrev. */
843
844 static inline void
845 out_abbrev (int name, int form)
846 {
847 out_uleb128 (name);
848 out_uleb128 (form);
849 }
850
851 /* Get the size of a fragment. */
852
853 static offsetT
854 get_frag_fix (fragS *frag, segT seg)
855 {
856 frchainS *fr;
857
858 if (frag->fr_next)
859 return frag->fr_fix;
860
861 /* If a fragment is the last in the chain, special measures must be
862 taken to find its size before relaxation, since it may be pending
863 on some subsegment chain. */
864 for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
865 if (fr->frch_last == frag)
866 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
867
868 abort ();
869 }
870
871 /* Set an absolute address (may result in a relocation entry). */
872
873 static void
874 out_set_addr (symbolS *sym)
875 {
876 expressionS exp;
877
878 out_opcode (DW_LNS_extended_op);
879 out_uleb128 (sizeof_address + 1);
880
881 out_opcode (DW_LNE_set_address);
882 exp.X_op = O_symbol;
883 exp.X_add_symbol = sym;
884 exp.X_add_number = 0;
885 emit_expr (&exp, sizeof_address);
886 }
887
888 static void scale_addr_delta (addressT *);
889
890 static void
891 scale_addr_delta (addressT *addr_delta)
892 {
893 static int printed_this = 0;
894 if (DWARF2_LINE_MIN_INSN_LENGTH > 1)
895 {
896 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0 && !printed_this)
897 {
898 as_bad("unaligned opcodes detected in executable segment");
899 printed_this = 1;
900 }
901 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
902 }
903 }
904
905 /* Encode a pair of line and address skips as efficiently as possible.
906 Note that the line skip is signed, whereas the address skip is unsigned.
907
908 The following two routines *must* be kept in sync. This is
909 enforced by making emit_inc_line_addr abort if we do not emit
910 exactly the expected number of bytes. */
911
912 static int
913 size_inc_line_addr (int line_delta, addressT addr_delta)
914 {
915 unsigned int tmp, opcode;
916 int len = 0;
917
918 /* Scale the address delta by the minimum instruction length. */
919 scale_addr_delta (&addr_delta);
920
921 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
922 We cannot use special opcodes here, since we want the end_sequence
923 to emit the matrix entry. */
924 if (line_delta == INT_MAX)
925 {
926 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
927 len = 1;
928 else
929 len = 1 + sizeof_leb128 (addr_delta, 0);
930 return len + 3;
931 }
932
933 /* Bias the line delta by the base. */
934 tmp = line_delta - DWARF2_LINE_BASE;
935
936 /* If the line increment is out of range of a special opcode, we
937 must encode it with DW_LNS_advance_line. */
938 if (tmp >= DWARF2_LINE_RANGE)
939 {
940 len = 1 + sizeof_leb128 (line_delta, 1);
941 line_delta = 0;
942 tmp = 0 - DWARF2_LINE_BASE;
943 }
944
945 /* Bias the opcode by the special opcode base. */
946 tmp += DWARF2_LINE_OPCODE_BASE;
947
948 /* Avoid overflow when addr_delta is large. */
949 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
950 {
951 /* Try using a special opcode. */
952 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
953 if (opcode <= 255)
954 return len + 1;
955
956 /* Try using DW_LNS_const_add_pc followed by special op. */
957 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
958 if (opcode <= 255)
959 return len + 2;
960 }
961
962 /* Otherwise use DW_LNS_advance_pc. */
963 len += 1 + sizeof_leb128 (addr_delta, 0);
964
965 /* DW_LNS_copy or special opcode. */
966 len += 1;
967
968 return len;
969 }
970
971 static void
972 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
973 {
974 unsigned int tmp, opcode;
975 int need_copy = 0;
976 char *end = p + len;
977
978 /* Line number sequences cannot go backward in addresses. This means
979 we've incorrectly ordered the statements in the sequence. */
980 gas_assert ((offsetT) addr_delta >= 0);
981
982 /* Scale the address delta by the minimum instruction length. */
983 scale_addr_delta (&addr_delta);
984
985 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
986 We cannot use special opcodes here, since we want the end_sequence
987 to emit the matrix entry. */
988 if (line_delta == INT_MAX)
989 {
990 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
991 *p++ = DW_LNS_const_add_pc;
992 else
993 {
994 *p++ = DW_LNS_advance_pc;
995 p += output_leb128 (p, addr_delta, 0);
996 }
997
998 *p++ = DW_LNS_extended_op;
999 *p++ = 1;
1000 *p++ = DW_LNE_end_sequence;
1001 goto done;
1002 }
1003
1004 /* Bias the line delta by the base. */
1005 tmp = line_delta - DWARF2_LINE_BASE;
1006
1007 /* If the line increment is out of range of a special opcode, we
1008 must encode it with DW_LNS_advance_line. */
1009 if (tmp >= DWARF2_LINE_RANGE)
1010 {
1011 *p++ = DW_LNS_advance_line;
1012 p += output_leb128 (p, line_delta, 1);
1013
1014 line_delta = 0;
1015 tmp = 0 - DWARF2_LINE_BASE;
1016 need_copy = 1;
1017 }
1018
1019 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
1020 special opcode. */
1021 if (line_delta == 0 && addr_delta == 0)
1022 {
1023 *p++ = DW_LNS_copy;
1024 goto done;
1025 }
1026
1027 /* Bias the opcode by the special opcode base. */
1028 tmp += DWARF2_LINE_OPCODE_BASE;
1029
1030 /* Avoid overflow when addr_delta is large. */
1031 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
1032 {
1033 /* Try using a special opcode. */
1034 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
1035 if (opcode <= 255)
1036 {
1037 *p++ = opcode;
1038 goto done;
1039 }
1040
1041 /* Try using DW_LNS_const_add_pc followed by special op. */
1042 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
1043 if (opcode <= 255)
1044 {
1045 *p++ = DW_LNS_const_add_pc;
1046 *p++ = opcode;
1047 goto done;
1048 }
1049 }
1050
1051 /* Otherwise use DW_LNS_advance_pc. */
1052 *p++ = DW_LNS_advance_pc;
1053 p += output_leb128 (p, addr_delta, 0);
1054
1055 if (need_copy)
1056 *p++ = DW_LNS_copy;
1057 else
1058 *p++ = tmp;
1059
1060 done:
1061 gas_assert (p == end);
1062 }
1063
1064 /* Handy routine to combine calls to the above two routines. */
1065
1066 static void
1067 out_inc_line_addr (int line_delta, addressT addr_delta)
1068 {
1069 int len = size_inc_line_addr (line_delta, addr_delta);
1070 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
1071 }
1072
1073 /* Write out an alternative form of line and address skips using
1074 DW_LNS_fixed_advance_pc opcodes. This uses more space than the default
1075 line and address information, but it is required if linker relaxation
1076 could change the code offsets. The following two routines *must* be
1077 kept in sync. */
1078 #define ADDR_DELTA_LIMIT 50000
1079
1080 static int
1081 size_fixed_inc_line_addr (int line_delta, addressT addr_delta)
1082 {
1083 int len = 0;
1084
1085 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */
1086 if (line_delta != INT_MAX)
1087 len = 1 + sizeof_leb128 (line_delta, 1);
1088
1089 if (addr_delta > ADDR_DELTA_LIMIT)
1090 {
1091 /* DW_LNS_extended_op */
1092 len += 1 + sizeof_leb128 (sizeof_address + 1, 0);
1093 /* DW_LNE_set_address */
1094 len += 1 + sizeof_address;
1095 }
1096 else
1097 /* DW_LNS_fixed_advance_pc */
1098 len += 3;
1099
1100 if (line_delta == INT_MAX)
1101 /* DW_LNS_extended_op + DW_LNE_end_sequence */
1102 len += 3;
1103 else
1104 /* DW_LNS_copy */
1105 len += 1;
1106
1107 return len;
1108 }
1109
1110 static void
1111 emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag,
1112 char *p, int len)
1113 {
1114 expressionS *pexp;
1115 segT line_seg;
1116 char *end = p + len;
1117
1118 /* Line number sequences cannot go backward in addresses. This means
1119 we've incorrectly ordered the statements in the sequence. */
1120 gas_assert ((offsetT) addr_delta >= 0);
1121
1122 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */
1123 if (line_delta != INT_MAX)
1124 {
1125 *p++ = DW_LNS_advance_line;
1126 p += output_leb128 (p, line_delta, 1);
1127 }
1128
1129 pexp = symbol_get_value_expression (frag->fr_symbol);
1130 line_seg = subseg_get (".debug_line", 0);
1131
1132 /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can
1133 advance the address by at most 64K. Linker relaxation (without
1134 which this function would not be used) could change the operand by
1135 an unknown amount. If the address increment is getting close to
1136 the limit, just reset the address. */
1137 if (addr_delta > ADDR_DELTA_LIMIT)
1138 {
1139 symbolS *to_sym;
1140 expressionS exp;
1141
1142 gas_assert (pexp->X_op == O_subtract);
1143 to_sym = pexp->X_add_symbol;
1144
1145 *p++ = DW_LNS_extended_op;
1146 p += output_leb128 (p, sizeof_address + 1, 0);
1147 *p++ = DW_LNE_set_address;
1148 exp.X_op = O_symbol;
1149 exp.X_add_symbol = to_sym;
1150 exp.X_add_number = 0;
1151 subseg_change (line_seg, 0);
1152 emit_expr_fix (&exp, sizeof_address, frag, p);
1153 p += sizeof_address;
1154 }
1155 else
1156 {
1157 *p++ = DW_LNS_fixed_advance_pc;
1158 subseg_change (line_seg, 0);
1159 emit_expr_fix (pexp, 2, frag, p);
1160 p += 2;
1161 }
1162
1163 if (line_delta == INT_MAX)
1164 {
1165 *p++ = DW_LNS_extended_op;
1166 *p++ = 1;
1167 *p++ = DW_LNE_end_sequence;
1168 }
1169 else
1170 *p++ = DW_LNS_copy;
1171
1172 gas_assert (p == end);
1173 }
1174
1175 /* Generate a variant frag that we can use to relax address/line
1176 increments between fragments of the target segment. */
1177
1178 static void
1179 relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
1180 {
1181 expressionS exp;
1182 int max_chars;
1183
1184 exp.X_op = O_subtract;
1185 exp.X_add_symbol = to_sym;
1186 exp.X_op_symbol = from_sym;
1187 exp.X_add_number = 0;
1188
1189 /* The maximum size of the frag is the line delta with a maximum
1190 sized address delta. */
1191 if (DWARF2_USE_FIXED_ADVANCE_PC)
1192 max_chars = size_fixed_inc_line_addr (line_delta,
1193 -DWARF2_LINE_MIN_INSN_LENGTH);
1194 else
1195 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
1196
1197 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
1198 make_expr_symbol (&exp), line_delta, NULL);
1199 }
1200
1201 /* The function estimates the size of a rs_dwarf2dbg variant frag
1202 based on the current values of the symbols. It is called before
1203 the relaxation loop. We set fr_subtype to the expected length. */
1204
1205 int
1206 dwarf2dbg_estimate_size_before_relax (fragS *frag)
1207 {
1208 offsetT addr_delta;
1209 int size;
1210
1211 addr_delta = resolve_symbol_value (frag->fr_symbol);
1212 if (DWARF2_USE_FIXED_ADVANCE_PC)
1213 size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta);
1214 else
1215 size = size_inc_line_addr (frag->fr_offset, addr_delta);
1216
1217 frag->fr_subtype = size;
1218
1219 return size;
1220 }
1221
1222 /* This function relaxes a rs_dwarf2dbg variant frag based on the
1223 current values of the symbols. fr_subtype is the current length
1224 of the frag. This returns the change in frag length. */
1225
1226 int
1227 dwarf2dbg_relax_frag (fragS *frag)
1228 {
1229 int old_size, new_size;
1230
1231 old_size = frag->fr_subtype;
1232 new_size = dwarf2dbg_estimate_size_before_relax (frag);
1233
1234 return new_size - old_size;
1235 }
1236
1237 /* This function converts a rs_dwarf2dbg variant frag into a normal
1238 fill frag. This is called after all relaxation has been done.
1239 fr_subtype will be the desired length of the frag. */
1240
1241 void
1242 dwarf2dbg_convert_frag (fragS *frag)
1243 {
1244 offsetT addr_diff;
1245
1246 if (DWARF2_USE_FIXED_ADVANCE_PC)
1247 {
1248 /* If linker relaxation is enabled then the distance bewteen the two
1249 symbols in the frag->fr_symbol expression might change. Hence we
1250 cannot rely upon the value computed by resolve_symbol_value.
1251 Instead we leave the expression unfinalized and allow
1252 emit_fixed_inc_line_addr to create a fixup (which later becomes a
1253 relocation) that will allow the linker to correctly compute the
1254 actual address difference. We have to use a fixed line advance for
1255 this as we cannot (easily) relocate leb128 encoded values. */
1256 int saved_finalize_syms = finalize_syms;
1257
1258 finalize_syms = 0;
1259 addr_diff = resolve_symbol_value (frag->fr_symbol);
1260 finalize_syms = saved_finalize_syms;
1261 }
1262 else
1263 addr_diff = resolve_symbol_value (frag->fr_symbol);
1264
1265 /* fr_var carries the max_chars that we created the fragment with.
1266 fr_subtype carries the current expected length. We must, of
1267 course, have allocated enough memory earlier. */
1268 gas_assert (frag->fr_var >= (int) frag->fr_subtype);
1269
1270 if (DWARF2_USE_FIXED_ADVANCE_PC)
1271 emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag,
1272 frag->fr_literal + frag->fr_fix,
1273 frag->fr_subtype);
1274 else
1275 emit_inc_line_addr (frag->fr_offset, addr_diff,
1276 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
1277
1278 frag->fr_fix += frag->fr_subtype;
1279 frag->fr_type = rs_fill;
1280 frag->fr_var = 0;
1281 frag->fr_offset = 0;
1282 }
1283
1284 /* Generate .debug_line content for the chain of line number entries
1285 beginning at E, for segment SEG. */
1286
1287 static void
1288 process_entries (segT seg, struct line_entry *e)
1289 {
1290 unsigned filenum = 1;
1291 unsigned line = 1;
1292 unsigned column = 0;
1293 unsigned isa = 0;
1294 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1295 fragS *last_frag = NULL, *frag;
1296 addressT last_frag_ofs = 0, frag_ofs;
1297 symbolS *last_lab = NULL, *lab;
1298 struct line_entry *next;
1299
1300 do
1301 {
1302 int line_delta;
1303
1304 if (filenum != e->loc.filenum)
1305 {
1306 filenum = e->loc.filenum;
1307 out_opcode (DW_LNS_set_file);
1308 out_uleb128 (filenum);
1309 }
1310
1311 if (column != e->loc.column)
1312 {
1313 column = e->loc.column;
1314 out_opcode (DW_LNS_set_column);
1315 out_uleb128 (column);
1316 }
1317
1318 if (e->loc.discriminator != 0)
1319 {
1320 out_opcode (DW_LNS_extended_op);
1321 out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0));
1322 out_opcode (DW_LNE_set_discriminator);
1323 out_uleb128 (e->loc.discriminator);
1324 }
1325
1326 if (isa != e->loc.isa)
1327 {
1328 isa = e->loc.isa;
1329 out_opcode (DW_LNS_set_isa);
1330 out_uleb128 (isa);
1331 }
1332
1333 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
1334 {
1335 flags = e->loc.flags;
1336 out_opcode (DW_LNS_negate_stmt);
1337 }
1338
1339 if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
1340 out_opcode (DW_LNS_set_basic_block);
1341
1342 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
1343 out_opcode (DW_LNS_set_prologue_end);
1344
1345 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1346 out_opcode (DW_LNS_set_epilogue_begin);
1347
1348 /* Don't try to optimize away redundant entries; gdb wants two
1349 entries for a function where the code starts on the same line as
1350 the {, and there's no way to identify that case here. Trust gcc
1351 to optimize appropriately. */
1352 line_delta = e->loc.line - line;
1353 lab = e->label;
1354 frag = symbol_get_frag (lab);
1355 frag_ofs = S_GET_VALUE (lab);
1356
1357 if (last_frag == NULL)
1358 {
1359 out_set_addr (lab);
1360 out_inc_line_addr (line_delta, 0);
1361 }
1362 else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1363 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
1364 else
1365 relax_inc_line_addr (line_delta, lab, last_lab);
1366
1367 line = e->loc.line;
1368 last_lab = lab;
1369 last_frag = frag;
1370 last_frag_ofs = frag_ofs;
1371
1372 next = e->next;
1373 free (e);
1374 e = next;
1375 }
1376 while (e);
1377
1378 /* Emit a DW_LNE_end_sequence for the end of the section. */
1379 frag = last_frag_for_seg (seg);
1380 frag_ofs = get_frag_fix (frag, seg);
1381 if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1382 out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
1383 else
1384 {
1385 lab = symbol_temp_new (seg, frag_ofs, frag);
1386 relax_inc_line_addr (INT_MAX, lab, last_lab);
1387 }
1388 }
1389
1390 /* Emit the directory and file tables for .debug_line. */
1391
1392 static void
1393 out_file_list (void)
1394 {
1395 size_t size;
1396 const char *dir;
1397 char *cp;
1398 unsigned int i;
1399
1400 /* Emit directory list. */
1401 for (i = 1; i < dirs_in_use; ++i)
1402 {
1403 dir = remap_debug_filename (dirs[i]);
1404 size = strlen (dir) + 1;
1405 cp = frag_more (size);
1406 memcpy (cp, dir, size);
1407 }
1408 /* Terminate it. */
1409 out_byte ('\0');
1410
1411 for (i = 1; i < files_in_use; ++i)
1412 {
1413 const char *fullfilename;
1414
1415 if (files[i].filename == NULL)
1416 {
1417 as_bad (_("unassigned file number %ld"), (long) i);
1418 /* Prevent a crash later, particularly for file 1. */
1419 files[i].filename = "";
1420 continue;
1421 }
1422
1423 fullfilename = DWARF2_FILE_NAME (files[i].filename,
1424 files[i].dir ? dirs [files [i].dir] : "");
1425 size = strlen (fullfilename) + 1;
1426 cp = frag_more (size);
1427 memcpy (cp, fullfilename, size);
1428
1429 out_uleb128 (files[i].dir); /* directory number */
1430 /* Output the last modification timestamp. */
1431 out_uleb128 (DWARF2_FILE_TIME_NAME (files[i].filename,
1432 files[i].dir ? dirs [files [i].dir] : ""));
1433 /* Output the filesize. */
1434 out_uleb128 (DWARF2_FILE_SIZE_NAME (files[i].filename,
1435 files[i].dir ? dirs [files [i].dir] : ""));
1436 }
1437
1438 /* Terminate filename list. */
1439 out_byte (0);
1440 }
1441
1442 /* Switch to SEC and output a header length field. Return the size of
1443 offsets used in SEC. The caller must set EXPR->X_add_symbol value
1444 to the end of the section. */
1445
1446 static int
1447 out_header (asection *sec, expressionS *exp)
1448 {
1449 symbolS *start_sym;
1450 symbolS *end_sym;
1451
1452 subseg_set (sec, 0);
1453 start_sym = symbol_temp_new_now ();;
1454 end_sym = symbol_temp_make ();
1455
1456 /* Total length of the information. */
1457 exp->X_op = O_subtract;
1458 exp->X_add_symbol = end_sym;
1459 exp->X_op_symbol = start_sym;
1460
1461 switch (DWARF2_FORMAT (sec))
1462 {
1463 case dwarf2_format_32bit:
1464 exp->X_add_number = -4;
1465 emit_expr (exp, 4);
1466 return 4;
1467
1468 case dwarf2_format_64bit:
1469 exp->X_add_number = -12;
1470 out_four (-1);
1471 emit_expr (exp, 8);
1472 return 8;
1473
1474 case dwarf2_format_64bit_irix:
1475 exp->X_add_number = -8;
1476 emit_expr (exp, 8);
1477 return 8;
1478 }
1479
1480 as_fatal (_("internal error: unknown dwarf2 format"));
1481 return 0;
1482 }
1483
1484 /* Emit the collected .debug_line data. */
1485
1486 static void
1487 out_debug_line (segT line_seg)
1488 {
1489 expressionS exp;
1490 symbolS *prologue_end;
1491 symbolS *line_end;
1492 struct line_seg *s;
1493 int sizeof_offset;
1494
1495 sizeof_offset = out_header (line_seg, &exp);
1496 line_end = exp.X_add_symbol;
1497
1498 /* Version. */
1499 out_two (DWARF2_LINE_VERSION);
1500
1501 /* Length of the prologue following this length. */
1502 prologue_end = symbol_temp_make ();
1503 exp.X_add_symbol = prologue_end;
1504 exp.X_add_number = - (4 + 2 + 4);
1505 emit_expr (&exp, sizeof_offset);
1506
1507 /* Parameters of the state machine. */
1508 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1509 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1510 out_byte (DWARF2_LINE_BASE);
1511 out_byte (DWARF2_LINE_RANGE);
1512 out_byte (DWARF2_LINE_OPCODE_BASE);
1513
1514 /* Standard opcode lengths. */
1515 out_byte (0); /* DW_LNS_copy */
1516 out_byte (1); /* DW_LNS_advance_pc */
1517 out_byte (1); /* DW_LNS_advance_line */
1518 out_byte (1); /* DW_LNS_set_file */
1519 out_byte (1); /* DW_LNS_set_column */
1520 out_byte (0); /* DW_LNS_negate_stmt */
1521 out_byte (0); /* DW_LNS_set_basic_block */
1522 out_byte (0); /* DW_LNS_const_add_pc */
1523 out_byte (1); /* DW_LNS_fixed_advance_pc */
1524 out_byte (0); /* DW_LNS_set_prologue_end */
1525 out_byte (0); /* DW_LNS_set_epilogue_begin */
1526 out_byte (1); /* DW_LNS_set_isa */
1527
1528 out_file_list ();
1529
1530 symbol_set_value_now (prologue_end);
1531
1532 /* For each section, emit a statement program. */
1533 for (s = all_segs; s; s = s->next)
1534 if (SEG_NORMAL (s->seg))
1535 process_entries (s->seg, s->head->head);
1536 else
1537 as_warn ("dwarf line number information for %s ignored",
1538 segment_name (s->seg));
1539
1540 symbol_set_value_now (line_end);
1541 }
1542
1543 static void
1544 out_debug_ranges (segT ranges_seg)
1545 {
1546 unsigned int addr_size = sizeof_address;
1547 struct line_seg *s;
1548 expressionS exp;
1549 unsigned int i;
1550
1551 subseg_set (ranges_seg, 0);
1552
1553 /* Base Address Entry. */
1554 for (i = 0; i < addr_size; i++)
1555 out_byte (0xff);
1556 for (i = 0; i < addr_size; i++)
1557 out_byte (0);
1558
1559 /* Range List Entry. */
1560 for (s = all_segs; s; s = s->next)
1561 {
1562 fragS *frag;
1563 symbolS *beg, *end;
1564
1565 frag = first_frag_for_seg (s->seg);
1566 beg = symbol_temp_new (s->seg, 0, frag);
1567 s->text_start = beg;
1568
1569 frag = last_frag_for_seg (s->seg);
1570 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1571 s->text_end = end;
1572
1573 exp.X_op = O_symbol;
1574 exp.X_add_symbol = beg;
1575 exp.X_add_number = 0;
1576 emit_expr (&exp, addr_size);
1577
1578 exp.X_op = O_symbol;
1579 exp.X_add_symbol = end;
1580 exp.X_add_number = 0;
1581 emit_expr (&exp, addr_size);
1582 }
1583
1584 /* End of Range Entry. */
1585 for (i = 0; i < addr_size; i++)
1586 out_byte (0);
1587 for (i = 0; i < addr_size; i++)
1588 out_byte (0);
1589 }
1590
1591 /* Emit data for .debug_aranges. */
1592
1593 static void
1594 out_debug_aranges (segT aranges_seg, segT info_seg)
1595 {
1596 unsigned int addr_size = sizeof_address;
1597 struct line_seg *s;
1598 expressionS exp;
1599 symbolS *aranges_end;
1600 char *p;
1601 int sizeof_offset;
1602
1603 sizeof_offset = out_header (aranges_seg, &exp);
1604 aranges_end = exp.X_add_symbol;
1605
1606 /* Version. */
1607 out_two (DWARF2_ARANGES_VERSION);
1608
1609 /* Offset to .debug_info. */
1610 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset);
1611
1612 /* Size of an address (offset portion). */
1613 out_byte (addr_size);
1614
1615 /* Size of a segment descriptor. */
1616 out_byte (0);
1617
1618 /* Align the header. */
1619 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1620
1621 for (s = all_segs; s; s = s->next)
1622 {
1623 fragS *frag;
1624 symbolS *beg, *end;
1625
1626 frag = first_frag_for_seg (s->seg);
1627 beg = symbol_temp_new (s->seg, 0, frag);
1628 s->text_start = beg;
1629
1630 frag = last_frag_for_seg (s->seg);
1631 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1632 s->text_end = end;
1633
1634 exp.X_op = O_symbol;
1635 exp.X_add_symbol = beg;
1636 exp.X_add_number = 0;
1637 emit_expr (&exp, addr_size);
1638
1639 exp.X_op = O_subtract;
1640 exp.X_add_symbol = end;
1641 exp.X_op_symbol = beg;
1642 exp.X_add_number = 0;
1643 emit_expr (&exp, addr_size);
1644 }
1645
1646 p = frag_more (2 * addr_size);
1647 md_number_to_chars (p, 0, addr_size);
1648 md_number_to_chars (p + addr_size, 0, addr_size);
1649
1650 symbol_set_value_now (aranges_end);
1651 }
1652
1653 /* Emit data for .debug_abbrev. Note that this must be kept in
1654 sync with out_debug_info below. */
1655
1656 static void
1657 out_debug_abbrev (segT abbrev_seg,
1658 segT info_seg ATTRIBUTE_UNUSED,
1659 segT line_seg ATTRIBUTE_UNUSED)
1660 {
1661 subseg_set (abbrev_seg, 0);
1662
1663 out_uleb128 (1);
1664 out_uleb128 (DW_TAG_compile_unit);
1665 out_byte (DW_CHILDREN_no);
1666 if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit)
1667 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1668 else
1669 out_abbrev (DW_AT_stmt_list, DW_FORM_data8);
1670 if (all_segs->next == NULL)
1671 {
1672 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1673 if (DWARF2_VERSION < 4)
1674 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1675 else
1676 out_abbrev (DW_AT_high_pc, (sizeof_address == 4
1677 ? DW_FORM_data4 : DW_FORM_data8));
1678 }
1679 else
1680 {
1681 if (DWARF2_FORMAT (info_seg) == dwarf2_format_32bit)
1682 out_abbrev (DW_AT_ranges, DW_FORM_data4);
1683 else
1684 out_abbrev (DW_AT_ranges, DW_FORM_data8);
1685 }
1686 out_abbrev (DW_AT_name, DW_FORM_string);
1687 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1688 out_abbrev (DW_AT_producer, DW_FORM_string);
1689 out_abbrev (DW_AT_language, DW_FORM_data2);
1690 out_abbrev (0, 0);
1691
1692 /* Terminate the abbreviations for this compilation unit. */
1693 out_byte (0);
1694 }
1695
1696 /* Emit a description of this compilation unit for .debug_info. */
1697
1698 static void
1699 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg)
1700 {
1701 char producer[128];
1702 const char *comp_dir;
1703 const char *dirname;
1704 expressionS exp;
1705 symbolS *info_end;
1706 char *p;
1707 int len;
1708 int sizeof_offset;
1709
1710 sizeof_offset = out_header (info_seg, &exp);
1711 info_end = exp.X_add_symbol;
1712
1713 /* DWARF version. */
1714 out_two (DWARF2_VERSION);
1715
1716 /* .debug_abbrev offset */
1717 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1718
1719 /* Target address size. */
1720 out_byte (sizeof_address);
1721
1722 /* DW_TAG_compile_unit DIE abbrev */
1723 out_uleb128 (1);
1724
1725 /* DW_AT_stmt_list */
1726 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg),
1727 (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit
1728 ? 4 : 8));
1729
1730 /* These two attributes are emitted if all of the code is contiguous. */
1731 if (all_segs->next == NULL)
1732 {
1733 /* DW_AT_low_pc */
1734 exp.X_op = O_symbol;
1735 exp.X_add_symbol = all_segs->text_start;
1736 exp.X_add_number = 0;
1737 emit_expr (&exp, sizeof_address);
1738
1739 /* DW_AT_high_pc */
1740 if (DWARF2_VERSION < 4)
1741 exp.X_op = O_symbol;
1742 else
1743 {
1744 exp.X_op = O_subtract;
1745 exp.X_op_symbol = all_segs->text_start;
1746 }
1747 exp.X_add_symbol = all_segs->text_end;
1748 exp.X_add_number = 0;
1749 emit_expr (&exp, sizeof_address);
1750 }
1751 else
1752 {
1753 /* This attribute is emitted if the code is disjoint. */
1754 /* DW_AT_ranges. */
1755 TC_DWARF2_EMIT_OFFSET (section_symbol (ranges_seg), sizeof_offset);
1756 }
1757
1758 /* DW_AT_name. We don't have the actual file name that was present
1759 on the command line, so assume files[1] is the main input file.
1760 We're not supposed to get called unless at least one line number
1761 entry was emitted, so this should always be defined. */
1762 if (files_in_use == 0)
1763 abort ();
1764 if (files[1].dir)
1765 {
1766 dirname = remap_debug_filename (dirs[files[1].dir]);
1767 len = strlen (dirname);
1768 #ifdef TE_VMS
1769 /* Already has trailing slash. */
1770 p = frag_more (len);
1771 memcpy (p, dirname, len);
1772 #else
1773 p = frag_more (len + 1);
1774 memcpy (p, dirname, len);
1775 INSERT_DIR_SEPARATOR (p, len);
1776 #endif
1777 }
1778 len = strlen (files[1].filename) + 1;
1779 p = frag_more (len);
1780 memcpy (p, files[1].filename, len);
1781
1782 /* DW_AT_comp_dir */
1783 comp_dir = remap_debug_filename (getpwd ());
1784 len = strlen (comp_dir) + 1;
1785 p = frag_more (len);
1786 memcpy (p, comp_dir, len);
1787
1788 /* DW_AT_producer */
1789 sprintf (producer, "GNU AS %s", VERSION);
1790 len = strlen (producer) + 1;
1791 p = frag_more (len);
1792 memcpy (p, producer, len);
1793
1794 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1795 dwarf2 draft has no standard code for assembler. */
1796 out_two (DW_LANG_Mips_Assembler);
1797
1798 symbol_set_value_now (info_end);
1799 }
1800
1801 void
1802 dwarf2_init (void)
1803 {
1804 all_segs_hash = hash_new ();
1805 last_seg_ptr = &all_segs;
1806 }
1807
1808
1809 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1810 were any .file/.loc directives, or --gdwarf2 was given, or if the
1811 file has a non-empty .debug_info section and an empty .debug_line
1812 section. If we emit .debug_line, and the .debug_info section is
1813 empty, we also emit .debug_info, .debug_aranges and .debug_abbrev.
1814 ALL_SEGS will be non-null if there were any .file/.loc directives,
1815 or --gdwarf2 was given and there were any located instructions
1816 emitted. */
1817
1818 void
1819 dwarf2_finish (void)
1820 {
1821 segT line_seg;
1822 struct line_seg *s;
1823 segT info_seg;
1824 int emit_other_sections = 0;
1825 int empty_debug_line = 0;
1826
1827 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
1828 emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
1829
1830 line_seg = bfd_get_section_by_name (stdoutput, ".debug_line");
1831 empty_debug_line = line_seg == NULL || !seg_not_empty_p (line_seg);
1832
1833 /* We can't construct a new debug_line section if we already have one.
1834 Give an error. */
1835 if (all_segs && !empty_debug_line)
1836 as_fatal ("duplicate .debug_line sections");
1837
1838 if ((!all_segs && emit_other_sections)
1839 || (!emit_other_sections && !empty_debug_line))
1840 /* If there is no line information and no non-empty .debug_info
1841 section, or if there is both a non-empty .debug_info and a non-empty
1842 .debug_line, then we do nothing. */
1843 return;
1844
1845 /* Calculate the size of an address for the target machine. */
1846 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1847
1848 /* Create and switch to the line number section. */
1849 line_seg = subseg_new (".debug_line", 0);
1850 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
1851
1852 /* For each subsection, chain the debug entries together. */
1853 for (s = all_segs; s; s = s->next)
1854 {
1855 struct line_subseg *lss = s->head;
1856 struct line_entry **ptail = lss->ptail;
1857
1858 while ((lss = lss->next) != NULL)
1859 {
1860 *ptail = lss->head;
1861 ptail = lss->ptail;
1862 }
1863 }
1864
1865 out_debug_line (line_seg);
1866
1867 /* If this is assembler generated line info, and there is no
1868 debug_info already, we need .debug_info and .debug_abbrev
1869 sections as well. */
1870 if (emit_other_sections)
1871 {
1872 segT abbrev_seg;
1873 segT aranges_seg;
1874 segT ranges_seg;
1875
1876 gas_assert (all_segs);
1877
1878 info_seg = subseg_new (".debug_info", 0);
1879 abbrev_seg = subseg_new (".debug_abbrev", 0);
1880 aranges_seg = subseg_new (".debug_aranges", 0);
1881
1882 bfd_set_section_flags (stdoutput, info_seg,
1883 SEC_READONLY | SEC_DEBUGGING);
1884 bfd_set_section_flags (stdoutput, abbrev_seg,
1885 SEC_READONLY | SEC_DEBUGGING);
1886 bfd_set_section_flags (stdoutput, aranges_seg,
1887 SEC_READONLY | SEC_DEBUGGING);
1888
1889 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1890
1891 if (all_segs->next == NULL)
1892 ranges_seg = NULL;
1893 else
1894 {
1895 ranges_seg = subseg_new (".debug_ranges", 0);
1896 bfd_set_section_flags (stdoutput, ranges_seg,
1897 SEC_READONLY | SEC_DEBUGGING);
1898 record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1);
1899 out_debug_ranges (ranges_seg);
1900 }
1901
1902 out_debug_aranges (aranges_seg, info_seg);
1903 out_debug_abbrev (abbrev_seg, info_seg, line_seg);
1904 out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg);
1905 }
1906 }
1907