* config/tc-xtensa.c (xg_add_opcode_fix): Number operands starting
[binutils.git] / gas / dwarf2dbg.c
blobc7d99c5997bdea090b4f761a23837b0d45c15a20
1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
6 This file is part of GAS, the GNU Assembler.
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 2, or (at your option)
11 any later version.
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.
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. */
23 /* Logical line numbers can be controlled by the compiler via the
24 following directives:
26 .file FILENO "file.c"
27 .loc FILENO LINENO [COLUMN] [basic_block] [prologue_end] \
28 [epilogue_begin] [is_stmt VALUE] [isa VALUE]
31 #include "ansidecl.h"
32 #include "as.h"
33 #include "safe-ctype.h"
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
46 #include "dwarf2dbg.h"
47 #include <filenames.h>
49 #ifndef DWARF2_FORMAT
50 # define DWARF2_FORMAT() dwarf2_format_32bit
51 #endif
53 #ifndef DWARF2_ADDR_SIZE
54 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
55 #endif
57 #include "subsegs.h"
59 #include "elf/dwarf2.h"
61 /* Since we can't generate the prolog until the body is complete, we
62 use three different subsegments for .debug_line: one holding the
63 prolog, one for the directory and filename info, and one for the
64 body ("statement program"). */
65 #define DL_PROLOG 0
66 #define DL_FILES 1
67 #define DL_BODY 2
69 /* First special line opcde - leave room for the standard opcodes.
70 Note: If you want to change this, you'll have to update the
71 "standard_opcode_lengths" table that is emitted below in
72 out_debug_line(). */
73 #define DWARF2_LINE_OPCODE_BASE 13
75 #ifndef DWARF2_LINE_BASE
76 /* Minimum line offset in a special line info. opcode. This value
77 was chosen to give a reasonable range of values. */
78 # define DWARF2_LINE_BASE -5
79 #endif
81 /* Range of line offsets in a special line info. opcode. */
82 #ifndef DWARF2_LINE_RANGE
83 # define DWARF2_LINE_RANGE 14
84 #endif
86 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
87 /* Define the architecture-dependent minimum instruction length (in
88 bytes). This value should be rather too small than too big. */
89 # define DWARF2_LINE_MIN_INSN_LENGTH 1
90 #endif
92 /* Flag that indicates the initial value of the is_stmt_start flag. */
93 #define DWARF2_LINE_DEFAULT_IS_STMT 1
95 /* Given a special op, return the line skip amount. */
96 #define SPECIAL_LINE(op) \
97 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
99 /* Given a special op, return the address skip amount (in units of
100 DWARF2_LINE_MIN_INSN_LENGTH. */
101 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
103 /* The maximum address skip amount that can be encoded with a special op. */
104 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
106 struct line_entry {
107 struct line_entry *next;
108 symbolS *label;
109 struct dwarf2_line_info loc;
112 struct line_subseg {
113 struct line_subseg *next;
114 subsegT subseg;
115 struct line_entry *head;
116 struct line_entry **ptail;
119 struct line_seg {
120 struct line_seg *next;
121 segT seg;
122 struct line_subseg *head;
123 symbolS *text_start;
124 symbolS *text_end;
127 /* Collects data for all line table entries during assembly. */
128 static struct line_seg *all_segs;
130 struct file_entry {
131 const char *filename;
132 unsigned int dir;
135 /* Table of files used by .debug_line. */
136 static struct file_entry *files;
137 static unsigned int files_in_use;
138 static unsigned int files_allocated;
140 /* Table of directories used by .debug_line. */
141 static char **dirs;
142 static unsigned int dirs_in_use;
143 static unsigned int dirs_allocated;
145 /* TRUE when we've seen a .loc directive recently. Used to avoid
146 doing work when there's nothing to do. */
147 static bfd_boolean loc_directive_seen;
149 /* TRUE when we're supposed to set the basic block mark whenever a
150 label is seen. */
151 bfd_boolean dwarf2_loc_mark_labels;
153 /* Current location as indicated by the most recent .loc directive. */
154 static struct dwarf2_line_info current = {
155 1, 1, 0, 0,
156 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0
159 /* The size of an address on the target. */
160 static unsigned int sizeof_address;
162 static struct line_subseg *get_line_subseg (segT, subsegT);
163 static unsigned int get_filenum (const char *, unsigned int);
164 static struct frag *first_frag_for_seg (segT);
165 static struct frag *last_frag_for_seg (segT);
166 static void out_byte (int);
167 static void out_opcode (int);
168 static void out_two (int);
169 static void out_four (int);
170 static void out_abbrev (int, int);
171 static void out_uleb128 (addressT);
172 static offsetT get_frag_fix (fragS *);
173 static void out_set_addr (symbolS *);
174 static int size_inc_line_addr (int, addressT);
175 static void emit_inc_line_addr (int, addressT, char *, int);
176 static void out_inc_line_addr (int, addressT);
177 static void relax_inc_line_addr (int, symbolS *, symbolS *);
178 static void process_entries (segT, struct line_entry *);
179 static void out_file_list (void);
180 static void out_debug_line (segT);
181 static void out_debug_aranges (segT, segT);
182 static void out_debug_abbrev (segT);
183 static void out_debug_info (segT, segT, segT);
185 #ifndef TC_DWARF2_EMIT_OFFSET
186 # define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
187 static void generic_dwarf2_emit_offset (symbolS *, unsigned int);
189 /* Create an offset to .dwarf2_*. */
191 static void
192 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
194 expressionS expr;
196 expr.X_op = O_symbol;
197 expr.X_add_symbol = symbol;
198 expr.X_add_number = 0;
199 emit_expr (&expr, size);
201 #endif
203 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
205 static struct line_subseg *
206 get_line_subseg (segT seg, subsegT subseg)
208 static segT last_seg;
209 static subsegT last_subseg;
210 static struct line_subseg *last_line_subseg;
212 struct line_seg *s;
213 struct line_subseg **pss, *ss;
215 if (seg == last_seg && subseg == last_subseg)
216 return last_line_subseg;
218 for (s = all_segs; s; s = s->next)
219 if (s->seg == seg)
220 goto found_seg;
222 s = (struct line_seg *) xmalloc (sizeof (*s));
223 s->next = all_segs;
224 s->seg = seg;
225 s->head = NULL;
226 all_segs = s;
228 found_seg:
229 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
231 if (ss->subseg == subseg)
232 goto found_subseg;
233 if (ss->subseg > subseg)
234 break;
237 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
238 ss->next = *pss;
239 ss->subseg = subseg;
240 ss->head = NULL;
241 ss->ptail = &ss->head;
242 *pss = ss;
244 found_subseg:
245 last_seg = seg;
246 last_subseg = subseg;
247 last_line_subseg = ss;
249 return ss;
252 /* Record an entry for LOC occurring at LABEL. */
254 static void
255 dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
257 struct line_subseg *ss;
258 struct line_entry *e;
260 e = (struct line_entry *) xmalloc (sizeof (*e));
261 e->next = NULL;
262 e->label = label;
263 e->loc = *loc;
265 ss = get_line_subseg (now_seg, now_subseg);
266 *ss->ptail = e;
267 ss->ptail = &e->next;
270 /* Record an entry for LOC occurring at OFS within the current fragment. */
272 void
273 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
275 static unsigned int line = -1;
276 static unsigned int filenum = -1;
278 symbolS *sym;
280 /* Early out for as-yet incomplete location information. */
281 if (loc->filenum == 0 || loc->line == 0)
282 return;
284 /* Don't emit sequences of line symbols for the same line when the
285 symbols apply to assembler code. It is necessary to emit
286 duplicate line symbols when a compiler asks for them, because GDB
287 uses them to determine the end of the prologue. */
288 if (debug_type == DEBUG_DWARF2
289 && line == loc->line && filenum == loc->filenum)
290 return;
292 line = loc->line;
293 filenum = loc->filenum;
295 sym = symbol_temp_new (now_seg, ofs, frag_now);
296 dwarf2_gen_line_info_1 (sym, loc);
299 /* Returns the current source information. If .file directives have
300 been encountered, the info for the corresponding source file is
301 returned. Otherwise, the info for the assembly source file is
302 returned. */
304 void
305 dwarf2_where (struct dwarf2_line_info *line)
307 if (debug_type == DEBUG_DWARF2)
309 char *filename;
310 as_where (&filename, &line->line);
311 line->filenum = get_filenum (filename, 0);
312 line->column = 0;
313 line->flags = DWARF2_FLAG_IS_STMT;
314 line->isa = current.isa;
316 else
317 *line = current;
320 /* A hook to allow the target backend to inform the line number state
321 machine of isa changes when assembler debug info is enabled. */
323 void
324 dwarf2_set_isa (unsigned int isa)
326 current.isa = isa;
329 /* Called for each machine instruction, or relatively atomic group of
330 machine instructions (ie built-in macro). The instruction or group
331 is SIZE bytes in length. If dwarf2 line number generation is called
332 for, emit a line statement appropriately. */
334 void
335 dwarf2_emit_insn (int size)
337 struct dwarf2_line_info loc;
339 if (loc_directive_seen)
341 /* Use the last location established by a .loc directive, not
342 the value returned by dwarf2_where(). That calls as_where()
343 which will return either the logical input file name (foo.c)
344 or the physical input file name (foo.s) and not the file name
345 specified in the most recent .loc directive (eg foo.h). */
346 loc = current;
348 /* Unless we generate DWARF2 debugging information for each
349 assembler line, we only emit one line symbol for one LOC. */
350 if (debug_type != DEBUG_DWARF2)
351 loc_directive_seen = FALSE;
353 else if (debug_type != DEBUG_DWARF2)
354 return;
355 else
356 dwarf2_where (&loc);
358 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
360 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
361 | DWARF2_FLAG_PROLOGUE_END
362 | DWARF2_FLAG_EPILOGUE_BEGIN);
365 /* Called for each (preferably code) label. If dwarf2_loc_mark_labels
366 is enabled, emit a basic block marker. */
368 void
369 dwarf2_emit_label (symbolS *label)
371 struct dwarf2_line_info loc;
373 if (!dwarf2_loc_mark_labels)
374 return;
375 if (S_GET_SEGMENT (label) != now_seg)
376 return;
377 if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE))
378 return;
380 if (debug_type == DEBUG_DWARF2)
381 dwarf2_where (&loc);
382 else
384 loc = current;
385 loc_directive_seen = FALSE;
388 loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
390 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
391 | DWARF2_FLAG_PROLOGUE_END
392 | DWARF2_FLAG_EPILOGUE_BEGIN);
394 dwarf2_gen_line_info_1 (label, &loc);
397 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
398 allocate it on that file table slot, otherwise return the first
399 empty one. */
401 static unsigned int
402 get_filenum (const char *filename, unsigned int num)
404 static unsigned int last_used, last_used_dir_len;
405 const char *file;
406 size_t dir_len;
407 unsigned int i, dir;
409 if (num == 0 && last_used)
411 if (! files[last_used].dir
412 && strcmp (filename, files[last_used].filename) == 0)
413 return last_used;
414 if (files[last_used].dir
415 && strncmp (filename, dirs[files[last_used].dir],
416 last_used_dir_len) == 0
417 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
418 && strcmp (filename + last_used_dir_len + 1,
419 files[last_used].filename) == 0)
420 return last_used;
423 file = lbasename (filename);
424 /* Don't make empty string from / or A: from A:/ . */
425 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
426 if (file <= filename + 3)
427 file = filename;
428 #else
429 if (file == filename + 1)
430 file = filename;
431 #endif
432 dir_len = file - filename;
434 dir = 0;
435 if (dir_len)
437 --dir_len;
438 for (dir = 1; dir < dirs_in_use; ++dir)
439 if (strncmp (filename, dirs[dir], dir_len) == 0
440 && dirs[dir][dir_len] == '\0')
441 break;
443 if (dir >= dirs_in_use)
445 if (dir >= dirs_allocated)
447 dirs_allocated = dir + 32;
448 dirs = (char **)
449 xrealloc (dirs, (dir + 32) * sizeof (const char *));
452 dirs[dir] = xmalloc (dir_len + 1);
453 memcpy (dirs[dir], filename, dir_len);
454 dirs[dir][dir_len] = '\0';
455 dirs_in_use = dir + 1;
459 if (num == 0)
461 for (i = 1; i < files_in_use; ++i)
462 if (files[i].dir == dir
463 && files[i].filename
464 && strcmp (file, files[i].filename) == 0)
466 last_used = i;
467 last_used_dir_len = dir_len;
468 return i;
471 else
472 i = num;
474 if (i >= files_allocated)
476 unsigned int old = files_allocated;
478 files_allocated = i + 32;
479 files = (struct file_entry *)
480 xrealloc (files, (i + 32) * sizeof (struct file_entry));
482 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
485 files[i].filename = num ? file : xstrdup (file);
486 files[i].dir = dir;
487 files_in_use = i + 1;
488 last_used = i;
489 last_used_dir_len = dir_len;
491 return i;
494 /* Handle two forms of .file directive:
495 - Pass .file "source.c" to s_app_file
496 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
498 If an entry is added to the file table, return a pointer to the filename. */
500 char *
501 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
503 offsetT num;
504 char *filename;
505 int filename_len;
507 /* Continue to accept a bare string and pass it off. */
508 SKIP_WHITESPACE ();
509 if (*input_line_pointer == '"')
511 s_app_file (0);
512 return NULL;
515 num = get_absolute_expression ();
516 filename = demand_copy_C_string (&filename_len);
517 if (filename == NULL)
518 return NULL;
519 demand_empty_rest_of_line ();
521 if (num < 1)
523 as_bad (_("file number less than one"));
524 return NULL;
527 if (num < (int) files_in_use && files[num].filename != 0)
529 as_bad (_("file number %ld already allocated"), (long) num);
530 return NULL;
533 get_filenum (filename, num);
535 return filename;
538 void
539 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
541 offsetT filenum, line;
543 filenum = get_absolute_expression ();
544 SKIP_WHITESPACE ();
545 line = get_absolute_expression ();
547 if (filenum < 1)
549 as_bad (_("file number less than one"));
550 return;
552 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
554 as_bad (_("unassigned file number %ld"), (long) filenum);
555 return;
558 current.filenum = filenum;
559 current.line = line;
561 #ifndef NO_LISTING
562 if (listing)
564 if (files[filenum].dir)
566 size_t dir_len = strlen (dirs[files[filenum].dir]);
567 size_t file_len = strlen (files[filenum].filename);
568 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
570 memcpy (cp, dirs[files[filenum].dir], dir_len);
571 cp[dir_len] = '/';
572 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
573 cp[dir_len + file_len + 1] = '\0';
574 listing_source_file (cp);
576 else
577 listing_source_file (files[filenum].filename);
578 listing_source_line (line);
580 #endif
582 SKIP_WHITESPACE ();
583 if (ISDIGIT (*input_line_pointer))
585 current.column = get_absolute_expression ();
586 SKIP_WHITESPACE ();
589 while (ISALPHA (*input_line_pointer))
591 char *p, c;
592 offsetT value;
594 p = input_line_pointer;
595 c = get_symbol_end ();
597 if (strcmp (p, "basic_block") == 0)
599 current.flags |= DWARF2_FLAG_BASIC_BLOCK;
600 *input_line_pointer = c;
602 else if (strcmp (p, "prologue_end") == 0)
604 current.flags |= DWARF2_FLAG_PROLOGUE_END;
605 *input_line_pointer = c;
607 else if (strcmp (p, "epilogue_begin") == 0)
609 current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
610 *input_line_pointer = c;
612 else if (strcmp (p, "is_stmt") == 0)
614 *input_line_pointer = c;
615 value = get_absolute_expression ();
616 if (value == 0)
617 current.flags &= ~DWARF2_FLAG_IS_STMT;
618 else if (value == 1)
619 current.flags |= DWARF2_FLAG_IS_STMT;
620 else
622 as_bad (_("is_stmt value not 0 or 1"));
623 return;
626 else if (strcmp (p, "isa") == 0)
628 *input_line_pointer = c;
629 value = get_absolute_expression ();
630 if (value >= 0)
631 current.isa = value;
632 else
634 as_bad (_("isa number less than zero"));
635 return;
638 else
640 as_bad (_("unknown .loc sub-directive `%s'"), p);
641 *input_line_pointer = c;
642 return;
645 SKIP_WHITESPACE ();
648 demand_empty_rest_of_line ();
649 loc_directive_seen = TRUE;
652 void
653 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
655 offsetT value = get_absolute_expression ();
657 if (value != 0 && value != 1)
659 as_bad (_("expected 0 or 1"));
660 ignore_rest_of_line ();
662 else
664 dwarf2_loc_mark_labels = value != 0;
665 demand_empty_rest_of_line ();
669 static struct frag *
670 first_frag_for_seg (segT seg)
672 frchainS *f, *first = NULL;
674 for (f = frchain_root; f; f = f->frch_next)
675 if (f->frch_seg == seg
676 && (! first || first->frch_subseg > f->frch_subseg))
677 first = f;
679 return first ? first->frch_root : NULL;
682 static struct frag *
683 last_frag_for_seg (segT seg)
685 frchainS *f, *last = NULL;
687 for (f = frchain_root; f; f = f->frch_next)
688 if (f->frch_seg == seg
689 && (! last || last->frch_subseg < f->frch_subseg))
690 last= f;
692 return last ? last->frch_last : NULL;
695 /* Emit a single byte into the current segment. */
697 static inline void
698 out_byte (int byte)
700 FRAG_APPEND_1_CHAR (byte);
703 /* Emit a statement program opcode into the current segment. */
705 static inline void
706 out_opcode (int opc)
708 out_byte (opc);
711 /* Emit a two-byte word into the current segment. */
713 static inline void
714 out_two (int data)
716 md_number_to_chars (frag_more (2), data, 2);
719 /* Emit a four byte word into the current segment. */
721 static inline void
722 out_four (int data)
724 md_number_to_chars (frag_more (4), data, 4);
727 /* Emit an unsigned "little-endian base 128" number. */
729 static void
730 out_uleb128 (addressT value)
732 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
735 /* Emit a tuple for .debug_abbrev. */
737 static inline void
738 out_abbrev (int name, int form)
740 out_uleb128 (name);
741 out_uleb128 (form);
744 /* Get the size of a fragment. */
746 static offsetT
747 get_frag_fix (fragS *frag)
749 frchainS *fr;
751 if (frag->fr_next)
752 return frag->fr_fix;
754 /* If a fragment is the last in the chain, special measures must be
755 taken to find its size before relaxation, since it may be pending
756 on some subsegment chain. */
757 for (fr = frchain_root; fr; fr = fr->frch_next)
758 if (fr->frch_last == frag)
759 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
761 abort ();
764 /* Set an absolute address (may result in a relocation entry). */
766 static void
767 out_set_addr (symbolS *sym)
769 expressionS expr;
771 out_opcode (DW_LNS_extended_op);
772 out_uleb128 (sizeof_address + 1);
774 out_opcode (DW_LNE_set_address);
775 expr.X_op = O_symbol;
776 expr.X_add_symbol = sym;
777 expr.X_add_number = 0;
778 emit_expr (&expr, sizeof_address);
781 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
782 static void scale_addr_delta (addressT *);
784 static void
785 scale_addr_delta (addressT *addr_delta)
787 static int printed_this = 0;
788 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
790 if (!printed_this)
791 as_bad("unaligned opcodes detected in executable segment");
792 printed_this = 1;
794 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
796 #else
797 #define scale_addr_delta(A)
798 #endif
800 /* Encode a pair of line and address skips as efficiently as possible.
801 Note that the line skip is signed, whereas the address skip is unsigned.
803 The following two routines *must* be kept in sync. This is
804 enforced by making emit_inc_line_addr abort if we do not emit
805 exactly the expected number of bytes. */
807 static int
808 size_inc_line_addr (int line_delta, addressT addr_delta)
810 unsigned int tmp, opcode;
811 int len = 0;
813 /* Scale the address delta by the minimum instruction length. */
814 scale_addr_delta (&addr_delta);
816 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
817 We cannot use special opcodes here, since we want the end_sequence
818 to emit the matrix entry. */
819 if (line_delta == INT_MAX)
821 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
822 len = 1;
823 else
824 len = 1 + sizeof_leb128 (addr_delta, 0);
825 return len + 3;
828 /* Bias the line delta by the base. */
829 tmp = line_delta - DWARF2_LINE_BASE;
831 /* If the line increment is out of range of a special opcode, we
832 must encode it with DW_LNS_advance_line. */
833 if (tmp >= DWARF2_LINE_RANGE)
835 len = 1 + sizeof_leb128 (line_delta, 1);
836 line_delta = 0;
837 tmp = 0 - DWARF2_LINE_BASE;
840 /* Bias the opcode by the special opcode base. */
841 tmp += DWARF2_LINE_OPCODE_BASE;
843 /* Avoid overflow when addr_delta is large. */
844 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
846 /* Try using a special opcode. */
847 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
848 if (opcode <= 255)
849 return len + 1;
851 /* Try using DW_LNS_const_add_pc followed by special op. */
852 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
853 if (opcode <= 255)
854 return len + 2;
857 /* Otherwise use DW_LNS_advance_pc. */
858 len += 1 + sizeof_leb128 (addr_delta, 0);
860 /* DW_LNS_copy or special opcode. */
861 len += 1;
863 return len;
866 static void
867 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
869 unsigned int tmp, opcode;
870 int need_copy = 0;
871 char *end = p + len;
873 /* Line number sequences cannot go backward in addresses. This means
874 we've incorrectly ordered the statements in the sequence. */
875 assert ((offsetT) addr_delta >= 0);
877 /* Scale the address delta by the minimum instruction length. */
878 scale_addr_delta (&addr_delta);
880 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
881 We cannot use special opcodes here, since we want the end_sequence
882 to emit the matrix entry. */
883 if (line_delta == INT_MAX)
885 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
886 *p++ = DW_LNS_const_add_pc;
887 else
889 *p++ = DW_LNS_advance_pc;
890 p += output_leb128 (p, addr_delta, 0);
893 *p++ = DW_LNS_extended_op;
894 *p++ = 1;
895 *p++ = DW_LNE_end_sequence;
896 goto done;
899 /* Bias the line delta by the base. */
900 tmp = line_delta - DWARF2_LINE_BASE;
902 /* If the line increment is out of range of a special opcode, we
903 must encode it with DW_LNS_advance_line. */
904 if (tmp >= DWARF2_LINE_RANGE)
906 *p++ = DW_LNS_advance_line;
907 p += output_leb128 (p, line_delta, 1);
909 line_delta = 0;
910 tmp = 0 - DWARF2_LINE_BASE;
911 need_copy = 1;
914 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
915 special opcode. */
916 if (line_delta == 0 && addr_delta == 0)
918 *p++ = DW_LNS_copy;
919 goto done;
922 /* Bias the opcode by the special opcode base. */
923 tmp += DWARF2_LINE_OPCODE_BASE;
925 /* Avoid overflow when addr_delta is large. */
926 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
928 /* Try using a special opcode. */
929 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
930 if (opcode <= 255)
932 *p++ = opcode;
933 goto done;
936 /* Try using DW_LNS_const_add_pc followed by special op. */
937 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
938 if (opcode <= 255)
940 *p++ = DW_LNS_const_add_pc;
941 *p++ = opcode;
942 goto done;
946 /* Otherwise use DW_LNS_advance_pc. */
947 *p++ = DW_LNS_advance_pc;
948 p += output_leb128 (p, addr_delta, 0);
950 if (need_copy)
951 *p++ = DW_LNS_copy;
952 else
953 *p++ = tmp;
955 done:
956 assert (p == end);
959 /* Handy routine to combine calls to the above two routines. */
961 static void
962 out_inc_line_addr (int line_delta, addressT addr_delta)
964 int len = size_inc_line_addr (line_delta, addr_delta);
965 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
968 /* Generate a variant frag that we can use to relax address/line
969 increments between fragments of the target segment. */
971 static void
972 relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
974 expressionS expr;
975 int max_chars;
977 expr.X_op = O_subtract;
978 expr.X_add_symbol = to_sym;
979 expr.X_op_symbol = from_sym;
980 expr.X_add_number = 0;
982 /* The maximum size of the frag is the line delta with a maximum
983 sized address delta. */
984 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
986 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
987 make_expr_symbol (&expr), line_delta, NULL);
990 /* The function estimates the size of a rs_dwarf2dbg variant frag
991 based on the current values of the symbols. It is called before
992 the relaxation loop. We set fr_subtype to the expected length. */
995 dwarf2dbg_estimate_size_before_relax (fragS *frag)
997 offsetT addr_delta;
998 int size;
1000 addr_delta = resolve_symbol_value (frag->fr_symbol);
1001 size = size_inc_line_addr (frag->fr_offset, addr_delta);
1003 frag->fr_subtype = size;
1005 return size;
1008 /* This function relaxes a rs_dwarf2dbg variant frag based on the
1009 current values of the symbols. fr_subtype is the current length
1010 of the frag. This returns the change in frag length. */
1013 dwarf2dbg_relax_frag (fragS *frag)
1015 int old_size, new_size;
1017 old_size = frag->fr_subtype;
1018 new_size = dwarf2dbg_estimate_size_before_relax (frag);
1020 return new_size - old_size;
1023 /* This function converts a rs_dwarf2dbg variant frag into a normal
1024 fill frag. This is called after all relaxation has been done.
1025 fr_subtype will be the desired length of the frag. */
1027 void
1028 dwarf2dbg_convert_frag (fragS *frag)
1030 offsetT addr_diff;
1032 addr_diff = resolve_symbol_value (frag->fr_symbol);
1034 /* fr_var carries the max_chars that we created the fragment with.
1035 fr_subtype carries the current expected length. We must, of
1036 course, have allocated enough memory earlier. */
1037 assert (frag->fr_var >= (int) frag->fr_subtype);
1039 emit_inc_line_addr (frag->fr_offset, addr_diff,
1040 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
1042 frag->fr_fix += frag->fr_subtype;
1043 frag->fr_type = rs_fill;
1044 frag->fr_var = 0;
1045 frag->fr_offset = 0;
1048 /* Generate .debug_line content for the chain of line number entries
1049 beginning at E, for segment SEG. */
1051 static void
1052 process_entries (segT seg, struct line_entry *e)
1054 unsigned filenum = 1;
1055 unsigned line = 1;
1056 unsigned column = 0;
1057 unsigned isa = 0;
1058 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1059 fragS *last_frag = NULL, *frag;
1060 addressT last_frag_ofs = 0, frag_ofs;
1061 symbolS *last_lab = NULL, *lab;
1062 struct line_entry *next;
1066 int line_delta;
1068 if (filenum != e->loc.filenum)
1070 filenum = e->loc.filenum;
1071 out_opcode (DW_LNS_set_file);
1072 out_uleb128 (filenum);
1075 if (column != e->loc.column)
1077 column = e->loc.column;
1078 out_opcode (DW_LNS_set_column);
1079 out_uleb128 (column);
1082 if (isa != e->loc.isa)
1084 isa = e->loc.isa;
1085 out_opcode (DW_LNS_set_isa);
1086 out_uleb128 (isa);
1089 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
1091 flags = e->loc.flags;
1092 out_opcode (DW_LNS_negate_stmt);
1095 if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
1096 out_opcode (DW_LNS_set_basic_block);
1098 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
1099 out_opcode (DW_LNS_set_prologue_end);
1101 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1102 out_opcode (DW_LNS_set_epilogue_begin);
1104 /* Don't try to optimize away redundant entries; gdb wants two
1105 entries for a function where the code starts on the same line as
1106 the {, and there's no way to identify that case here. Trust gcc
1107 to optimize appropriately. */
1108 line_delta = e->loc.line - line;
1109 lab = e->label;
1110 frag = symbol_get_frag (lab);
1111 frag_ofs = S_GET_VALUE (lab);
1113 if (last_frag == NULL)
1115 out_set_addr (lab);
1116 out_inc_line_addr (line_delta, 0);
1118 else if (frag == last_frag)
1119 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
1120 else
1121 relax_inc_line_addr (line_delta, lab, last_lab);
1123 line = e->loc.line;
1124 last_lab = lab;
1125 last_frag = frag;
1126 last_frag_ofs = frag_ofs;
1128 next = e->next;
1129 free (e);
1130 e = next;
1132 while (e);
1134 /* Emit a DW_LNE_end_sequence for the end of the section. */
1135 frag = last_frag_for_seg (seg);
1136 frag_ofs = get_frag_fix (frag);
1137 if (frag == last_frag)
1138 out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
1139 else
1141 lab = symbol_temp_new (seg, frag_ofs, frag);
1142 relax_inc_line_addr (INT_MAX, lab, last_lab);
1146 /* Emit the directory and file tables for .debug_line. */
1148 static void
1149 out_file_list (void)
1151 size_t size;
1152 char *cp;
1153 unsigned int i;
1155 /* Emit directory list. */
1156 for (i = 1; i < dirs_in_use; ++i)
1158 size = strlen (dirs[i]) + 1;
1159 cp = frag_more (size);
1160 memcpy (cp, dirs[i], size);
1162 /* Terminate it. */
1163 out_byte ('\0');
1165 for (i = 1; i < files_in_use; ++i)
1167 if (files[i].filename == NULL)
1169 as_bad (_("unassigned file number %ld"), (long) i);
1170 /* Prevent a crash later, particularly for file 1. */
1171 files[i].filename = "";
1172 continue;
1175 size = strlen (files[i].filename) + 1;
1176 cp = frag_more (size);
1177 memcpy (cp, files[i].filename, size);
1179 out_uleb128 (files[i].dir); /* directory number */
1180 out_uleb128 (0); /* last modification timestamp */
1181 out_uleb128 (0); /* filesize */
1184 /* Terminate filename list. */
1185 out_byte (0);
1188 /* Emit the collected .debug_line data. */
1190 static void
1191 out_debug_line (segT line_seg)
1193 expressionS expr;
1194 symbolS *line_start;
1195 symbolS *prologue_end;
1196 symbolS *line_end;
1197 struct line_seg *s;
1198 enum dwarf2_format d2f;
1199 int sizeof_offset;
1201 subseg_set (line_seg, 0);
1203 line_start = symbol_temp_new_now ();
1204 prologue_end = symbol_temp_make ();
1205 line_end = symbol_temp_make ();
1207 /* Total length of the information for this compilation unit. */
1208 expr.X_op = O_subtract;
1209 expr.X_add_symbol = line_end;
1210 expr.X_op_symbol = line_start;
1212 d2f = DWARF2_FORMAT ();
1213 if (d2f == dwarf2_format_32bit)
1215 expr.X_add_number = -4;
1216 emit_expr (&expr, 4);
1217 sizeof_offset = 4;
1219 else if (d2f == dwarf2_format_64bit)
1221 expr.X_add_number = -12;
1222 out_four (-1);
1223 emit_expr (&expr, 8);
1224 sizeof_offset = 8;
1226 else if (d2f == dwarf2_format_64bit_irix)
1228 expr.X_add_number = -8;
1229 emit_expr (&expr, 8);
1230 sizeof_offset = 8;
1232 else
1234 as_fatal (_("internal error: unknown dwarf2 format"));
1237 /* Version. */
1238 out_two (2);
1240 /* Length of the prologue following this length. */
1241 expr.X_op = O_subtract;
1242 expr.X_add_symbol = prologue_end;
1243 expr.X_op_symbol = line_start;
1244 expr.X_add_number = - (4 + 2 + 4);
1245 emit_expr (&expr, sizeof_offset);
1247 /* Parameters of the state machine. */
1248 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1249 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1250 out_byte (DWARF2_LINE_BASE);
1251 out_byte (DWARF2_LINE_RANGE);
1252 out_byte (DWARF2_LINE_OPCODE_BASE);
1254 /* Standard opcode lengths. */
1255 out_byte (0); /* DW_LNS_copy */
1256 out_byte (1); /* DW_LNS_advance_pc */
1257 out_byte (1); /* DW_LNS_advance_line */
1258 out_byte (1); /* DW_LNS_set_file */
1259 out_byte (1); /* DW_LNS_set_column */
1260 out_byte (0); /* DW_LNS_negate_stmt */
1261 out_byte (0); /* DW_LNS_set_basic_block */
1262 out_byte (0); /* DW_LNS_const_add_pc */
1263 out_byte (1); /* DW_LNS_fixed_advance_pc */
1264 out_byte (0); /* DW_LNS_set_prologue_end */
1265 out_byte (0); /* DW_LNS_set_epilogue_begin */
1266 out_byte (1); /* DW_LNS_set_isa */
1268 out_file_list ();
1270 symbol_set_value_now (prologue_end);
1272 /* For each section, emit a statement program. */
1273 for (s = all_segs; s; s = s->next)
1274 process_entries (s->seg, s->head->head);
1276 symbol_set_value_now (line_end);
1279 /* Emit data for .debug_aranges. */
1281 static void
1282 out_debug_aranges (segT aranges_seg, segT info_seg)
1284 unsigned int addr_size = sizeof_address;
1285 addressT size, skip;
1286 struct line_seg *s;
1287 expressionS expr;
1288 char *p;
1290 size = 4 + 2 + 4 + 1 + 1;
1292 skip = 2 * addr_size - (size & (2 * addr_size - 1));
1293 if (skip == 2 * addr_size)
1294 skip = 0;
1295 size += skip;
1297 for (s = all_segs; s; s = s->next)
1298 size += 2 * addr_size;
1300 size += 2 * addr_size;
1302 subseg_set (aranges_seg, 0);
1304 /* Length of the compilation unit. */
1305 out_four (size - 4);
1307 /* Version. */
1308 out_two (2);
1310 /* Offset to .debug_info. */
1311 /* ??? sizeof_offset */
1312 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), 4);
1314 /* Size of an address (offset portion). */
1315 out_byte (addr_size);
1317 /* Size of a segment descriptor. */
1318 out_byte (0);
1320 /* Align the header. */
1321 if (skip)
1322 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1324 for (s = all_segs; s; s = s->next)
1326 fragS *frag;
1327 symbolS *beg, *end;
1329 frag = first_frag_for_seg (s->seg);
1330 beg = symbol_temp_new (s->seg, 0, frag);
1331 s->text_start = beg;
1333 frag = last_frag_for_seg (s->seg);
1334 end = symbol_temp_new (s->seg, get_frag_fix (frag), frag);
1335 s->text_end = end;
1337 expr.X_op = O_symbol;
1338 expr.X_add_symbol = beg;
1339 expr.X_add_number = 0;
1340 emit_expr (&expr, addr_size);
1342 expr.X_op = O_subtract;
1343 expr.X_add_symbol = end;
1344 expr.X_op_symbol = beg;
1345 expr.X_add_number = 0;
1346 emit_expr (&expr, addr_size);
1349 p = frag_more (2 * addr_size);
1350 md_number_to_chars (p, 0, addr_size);
1351 md_number_to_chars (p + addr_size, 0, addr_size);
1354 /* Emit data for .debug_abbrev. Note that this must be kept in
1355 sync with out_debug_info below. */
1357 static void
1358 out_debug_abbrev (segT abbrev_seg)
1360 subseg_set (abbrev_seg, 0);
1362 out_uleb128 (1);
1363 out_uleb128 (DW_TAG_compile_unit);
1364 out_byte (DW_CHILDREN_no);
1365 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1366 if (all_segs->next == NULL)
1368 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1369 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1371 out_abbrev (DW_AT_name, DW_FORM_string);
1372 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1373 out_abbrev (DW_AT_producer, DW_FORM_string);
1374 out_abbrev (DW_AT_language, DW_FORM_data2);
1375 out_abbrev (0, 0);
1377 /* Terminate the abbreviations for this compilation unit. */
1378 out_byte (0);
1381 /* Emit a description of this compilation unit for .debug_info. */
1383 static void
1384 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg)
1386 char producer[128];
1387 char *comp_dir;
1388 expressionS expr;
1389 symbolS *info_start;
1390 symbolS *info_end;
1391 char *p;
1392 int len;
1393 enum dwarf2_format d2f;
1394 int sizeof_offset;
1396 subseg_set (info_seg, 0);
1398 info_start = symbol_temp_new_now ();
1399 info_end = symbol_temp_make ();
1401 /* Compilation Unit length. */
1402 expr.X_op = O_subtract;
1403 expr.X_add_symbol = info_end;
1404 expr.X_op_symbol = info_start;
1406 d2f = DWARF2_FORMAT ();
1407 if (d2f == dwarf2_format_32bit)
1409 expr.X_add_number = -4;
1410 emit_expr (&expr, 4);
1411 sizeof_offset = 4;
1413 else if (d2f == dwarf2_format_64bit)
1415 expr.X_add_number = -12;
1416 out_four (-1);
1417 emit_expr (&expr, 8);
1418 sizeof_offset = 8;
1420 else if (d2f == dwarf2_format_64bit_irix)
1422 expr.X_add_number = -8;
1423 emit_expr (&expr, 8);
1424 sizeof_offset = 8;
1426 else
1428 as_fatal (_("internal error: unknown dwarf2 format"));
1431 /* DWARF version. */
1432 out_two (2);
1434 /* .debug_abbrev offset */
1435 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1437 /* Target address size. */
1438 out_byte (sizeof_address);
1440 /* DW_TAG_compile_unit DIE abbrev */
1441 out_uleb128 (1);
1443 /* DW_AT_stmt_list */
1444 /* ??? sizeof_offset */
1445 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 4);
1447 /* These two attributes may only be emitted if all of the code is
1448 contiguous. Multiple sections are not that. */
1449 if (all_segs->next == NULL)
1451 /* DW_AT_low_pc */
1452 expr.X_op = O_symbol;
1453 expr.X_add_symbol = all_segs->text_start;
1454 expr.X_add_number = 0;
1455 emit_expr (&expr, sizeof_address);
1457 /* DW_AT_high_pc */
1458 expr.X_op = O_symbol;
1459 expr.X_add_symbol = all_segs->text_end;
1460 expr.X_add_number = 0;
1461 emit_expr (&expr, sizeof_address);
1464 /* DW_AT_name. We don't have the actual file name that was present
1465 on the command line, so assume files[1] is the main input file.
1466 We're not supposed to get called unless at least one line number
1467 entry was emitted, so this should always be defined. */
1468 if (!files || files_in_use < 1)
1469 abort ();
1470 if (files[1].dir)
1472 len = strlen (dirs[files[1].dir]);
1473 p = frag_more (len + 1);
1474 memcpy (p, dirs[files[1].dir], len);
1475 p[len] = '/';
1477 len = strlen (files[1].filename) + 1;
1478 p = frag_more (len);
1479 memcpy (p, files[1].filename, len);
1481 /* DW_AT_comp_dir */
1482 comp_dir = getpwd ();
1483 len = strlen (comp_dir) + 1;
1484 p = frag_more (len);
1485 memcpy (p, comp_dir, len);
1487 /* DW_AT_producer */
1488 sprintf (producer, "GNU AS %s", VERSION);
1489 len = strlen (producer) + 1;
1490 p = frag_more (len);
1491 memcpy (p, producer, len);
1493 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1494 dwarf2 draft has no standard code for assembler. */
1495 out_two (DW_LANG_Mips_Assembler);
1497 symbol_set_value_now (info_end);
1500 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1501 were any .file/.loc directives, or --gdwarf2 was given, or if the
1502 file has a non-empty .debug_info section. If we emit .debug_line,
1503 and the .debug_info section is empty, we also emit .debug_info,
1504 .debug_aranges and .debug_abbrev. ALL_SEGS will be non-null if
1505 there were any .file/.loc directives, or --gdwarf2 was given and
1506 there were any located instructions emitted. */
1508 void
1509 dwarf2_finish (void)
1511 segT line_seg;
1512 struct line_seg *s;
1513 segT info_seg;
1514 int emit_other_sections = 0;
1516 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
1517 emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
1519 if (!all_segs && emit_other_sections)
1520 /* There is no line information and no non-empty .debug_info
1521 section. */
1522 return;
1524 /* Calculate the size of an address for the target machine. */
1525 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1527 /* Create and switch to the line number section. */
1528 line_seg = subseg_new (".debug_line", 0);
1529 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
1531 /* For each subsection, chain the debug entries together. */
1532 for (s = all_segs; s; s = s->next)
1534 struct line_subseg *ss = s->head;
1535 struct line_entry **ptail = ss->ptail;
1537 while ((ss = ss->next) != NULL)
1539 *ptail = ss->head;
1540 ptail = ss->ptail;
1544 out_debug_line (line_seg);
1546 /* If this is assembler generated line info, and there is no
1547 debug_info already, we need .debug_info and .debug_abbrev
1548 sections as well. */
1549 if (emit_other_sections)
1551 segT abbrev_seg;
1552 segT aranges_seg;
1554 assert (all_segs);
1556 info_seg = subseg_new (".debug_info", 0);
1557 abbrev_seg = subseg_new (".debug_abbrev", 0);
1558 aranges_seg = subseg_new (".debug_aranges", 0);
1560 bfd_set_section_flags (stdoutput, info_seg,
1561 SEC_READONLY | SEC_DEBUGGING);
1562 bfd_set_section_flags (stdoutput, abbrev_seg,
1563 SEC_READONLY | SEC_DEBUGGING);
1564 bfd_set_section_flags (stdoutput, aranges_seg,
1565 SEC_READONLY | SEC_DEBUGGING);
1567 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1569 out_debug_aranges (aranges_seg, info_seg);
1570 out_debug_abbrev (abbrev_seg);
1571 out_debug_info (info_seg, abbrev_seg, line_seg);