merge from gcc
[binutils.git] / gas / dwarf2dbg.c
blob2e00b9857eda6fd3ee523aade24534b1cd591476
1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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 "as.h"
32 #include "safe-ctype.h"
34 #ifdef HAVE_LIMITS_H
35 #include <limits.h>
36 #else
37 #ifdef HAVE_SYS_PARAM_H
38 #include <sys/param.h>
39 #endif
40 #ifndef INT_MAX
41 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
42 #endif
43 #endif
45 #include "dwarf2dbg.h"
46 #include <filenames.h>
48 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
49 /* We need to decide which character to use as a directory separator.
50 Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not
51 necessarily mean that the backslash character is the one to use.
52 Some environments, eg Cygwin, can support both naming conventions.
53 So we use the heuristic that we only need to use the backslash if
54 the path is an absolute path starting with a DOS style drive
55 selector. eg C: or D: */
56 # define INSERT_DIR_SEPARATOR(string, offset) \
57 do \
58 { \
59 if (offset > 1 \
60 && string[0] != 0 \
61 && string[1] == ':') \
62 string [offset] = '\\'; \
63 else \
64 string [offset] = '/'; \
65 } \
66 while (0)
67 #else
68 # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
69 #endif
71 #ifndef DWARF2_FORMAT
72 # define DWARF2_FORMAT() dwarf2_format_32bit
73 #endif
75 #ifndef DWARF2_ADDR_SIZE
76 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
77 #endif
79 #include "subsegs.h"
81 #include "elf/dwarf2.h"
83 /* Since we can't generate the prolog until the body is complete, we
84 use three different subsegments for .debug_line: one holding the
85 prolog, one for the directory and filename info, and one for the
86 body ("statement program"). */
87 #define DL_PROLOG 0
88 #define DL_FILES 1
89 #define DL_BODY 2
91 /* First special line opcde - leave room for the standard opcodes.
92 Note: If you want to change this, you'll have to update the
93 "standard_opcode_lengths" table that is emitted below in
94 out_debug_line(). */
95 #define DWARF2_LINE_OPCODE_BASE 13
97 #ifndef DWARF2_LINE_BASE
98 /* Minimum line offset in a special line info. opcode. This value
99 was chosen to give a reasonable range of values. */
100 # define DWARF2_LINE_BASE -5
101 #endif
103 /* Range of line offsets in a special line info. opcode. */
104 #ifndef DWARF2_LINE_RANGE
105 # define DWARF2_LINE_RANGE 14
106 #endif
108 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
109 /* Define the architecture-dependent minimum instruction length (in
110 bytes). This value should be rather too small than too big. */
111 # define DWARF2_LINE_MIN_INSN_LENGTH 1
112 #endif
114 /* Flag that indicates the initial value of the is_stmt_start flag. */
115 #define DWARF2_LINE_DEFAULT_IS_STMT 1
117 /* Given a special op, return the line skip amount. */
118 #define SPECIAL_LINE(op) \
119 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
121 /* Given a special op, return the address skip amount (in units of
122 DWARF2_LINE_MIN_INSN_LENGTH. */
123 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
125 /* The maximum address skip amount that can be encoded with a special op. */
126 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
128 struct line_entry {
129 struct line_entry *next;
130 symbolS *label;
131 struct dwarf2_line_info loc;
134 struct line_subseg {
135 struct line_subseg *next;
136 subsegT subseg;
137 struct line_entry *head;
138 struct line_entry **ptail;
141 struct line_seg {
142 struct line_seg *next;
143 segT seg;
144 struct line_subseg *head;
145 symbolS *text_start;
146 symbolS *text_end;
149 /* Collects data for all line table entries during assembly. */
150 static struct line_seg *all_segs;
152 struct file_entry {
153 const char *filename;
154 unsigned int dir;
157 /* Table of files used by .debug_line. */
158 static struct file_entry *files;
159 static unsigned int files_in_use;
160 static unsigned int files_allocated;
162 /* Table of directories used by .debug_line. */
163 static char **dirs;
164 static unsigned int dirs_in_use;
165 static unsigned int dirs_allocated;
167 /* TRUE when we've seen a .loc directive recently. Used to avoid
168 doing work when there's nothing to do. */
169 static bfd_boolean loc_directive_seen;
171 /* TRUE when we're supposed to set the basic block mark whenever a
172 label is seen. */
173 bfd_boolean dwarf2_loc_mark_labels;
175 /* Current location as indicated by the most recent .loc directive. */
176 static struct dwarf2_line_info current = {
177 1, 1, 0, 0,
178 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0
181 /* The size of an address on the target. */
182 static unsigned int sizeof_address;
184 static struct line_subseg *get_line_subseg (segT, subsegT);
185 static unsigned int get_filenum (const char *, unsigned int);
186 static struct frag *first_frag_for_seg (segT);
187 static struct frag *last_frag_for_seg (segT);
188 static void out_byte (int);
189 static void out_opcode (int);
190 static void out_two (int);
191 static void out_four (int);
192 static void out_abbrev (int, int);
193 static void out_uleb128 (addressT);
194 static offsetT get_frag_fix (fragS *, segT);
195 static void out_set_addr (symbolS *);
196 static int size_inc_line_addr (int, addressT);
197 static void emit_inc_line_addr (int, addressT, char *, int);
198 static void out_inc_line_addr (int, addressT);
199 static void relax_inc_line_addr (int, symbolS *, symbolS *);
200 static void process_entries (segT, struct line_entry *);
201 static void out_file_list (void);
202 static void out_debug_line (segT);
203 static void out_debug_aranges (segT, segT);
204 static void out_debug_abbrev (segT);
205 static void out_debug_info (segT, segT, segT);
207 #ifndef TC_DWARF2_EMIT_OFFSET
208 # define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
209 static void generic_dwarf2_emit_offset (symbolS *, unsigned int);
211 /* Create an offset to .dwarf2_*. */
213 static void
214 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
216 expressionS expr;
218 expr.X_op = O_symbol;
219 expr.X_add_symbol = symbol;
220 expr.X_add_number = 0;
221 emit_expr (&expr, size);
223 #endif
225 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
227 static struct line_subseg *
228 get_line_subseg (segT seg, subsegT subseg)
230 static segT last_seg;
231 static subsegT last_subseg;
232 static struct line_subseg *last_line_subseg;
234 struct line_seg **ps, *s;
235 struct line_subseg **pss, *ss;
237 if (seg == last_seg && subseg == last_subseg)
238 return last_line_subseg;
240 for (ps = &all_segs; (s = *ps) != NULL; ps = &s->next)
241 if (s->seg == seg)
242 goto found_seg;
244 s = (struct line_seg *) xmalloc (sizeof (*s));
245 s->next = NULL;
246 s->seg = seg;
247 s->head = NULL;
248 *ps = s;
250 found_seg:
251 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
253 if (ss->subseg == subseg)
254 goto found_subseg;
255 if (ss->subseg > subseg)
256 break;
259 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
260 ss->next = *pss;
261 ss->subseg = subseg;
262 ss->head = NULL;
263 ss->ptail = &ss->head;
264 *pss = ss;
266 found_subseg:
267 last_seg = seg;
268 last_subseg = subseg;
269 last_line_subseg = ss;
271 return ss;
274 /* Record an entry for LOC occurring at LABEL. */
276 static void
277 dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
279 struct line_subseg *ss;
280 struct line_entry *e;
282 e = (struct line_entry *) xmalloc (sizeof (*e));
283 e->next = NULL;
284 e->label = label;
285 e->loc = *loc;
287 ss = get_line_subseg (now_seg, now_subseg);
288 *ss->ptail = e;
289 ss->ptail = &e->next;
292 /* Record an entry for LOC occurring at OFS within the current fragment. */
294 void
295 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
297 static unsigned int line = -1;
298 static unsigned int filenum = -1;
300 symbolS *sym;
302 /* Early out for as-yet incomplete location information. */
303 if (loc->filenum == 0 || loc->line == 0)
304 return;
306 /* Don't emit sequences of line symbols for the same line when the
307 symbols apply to assembler code. It is necessary to emit
308 duplicate line symbols when a compiler asks for them, because GDB
309 uses them to determine the end of the prologue. */
310 if (debug_type == DEBUG_DWARF2
311 && line == loc->line && filenum == loc->filenum)
312 return;
314 line = loc->line;
315 filenum = loc->filenum;
317 sym = symbol_temp_new (now_seg, ofs, frag_now);
318 dwarf2_gen_line_info_1 (sym, loc);
321 /* Returns the current source information. If .file directives have
322 been encountered, the info for the corresponding source file is
323 returned. Otherwise, the info for the assembly source file is
324 returned. */
326 void
327 dwarf2_where (struct dwarf2_line_info *line)
329 if (debug_type == DEBUG_DWARF2)
331 char *filename;
332 as_where (&filename, &line->line);
333 line->filenum = get_filenum (filename, 0);
334 line->column = 0;
335 line->flags = DWARF2_FLAG_IS_STMT;
336 line->isa = current.isa;
338 else
339 *line = current;
342 /* A hook to allow the target backend to inform the line number state
343 machine of isa changes when assembler debug info is enabled. */
345 void
346 dwarf2_set_isa (unsigned int isa)
348 current.isa = isa;
351 /* Called for each machine instruction, or relatively atomic group of
352 machine instructions (ie built-in macro). The instruction or group
353 is SIZE bytes in length. If dwarf2 line number generation is called
354 for, emit a line statement appropriately. */
356 void
357 dwarf2_emit_insn (int size)
359 struct dwarf2_line_info loc;
361 if (loc_directive_seen)
363 /* Use the last location established by a .loc directive, not
364 the value returned by dwarf2_where(). That calls as_where()
365 which will return either the logical input file name (foo.c)
366 or the physical input file name (foo.s) and not the file name
367 specified in the most recent .loc directive (eg foo.h). */
368 loc = current;
370 /* Unless we generate DWARF2 debugging information for each
371 assembler line, we only emit one line symbol for one LOC. */
372 if (debug_type != DEBUG_DWARF2)
373 loc_directive_seen = FALSE;
375 else if (debug_type != DEBUG_DWARF2)
376 return;
377 else
378 dwarf2_where (&loc);
380 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
382 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
383 | DWARF2_FLAG_PROLOGUE_END
384 | DWARF2_FLAG_EPILOGUE_BEGIN);
387 /* Called for each (preferably code) label. If dwarf2_loc_mark_labels
388 is enabled, emit a basic block marker. */
390 void
391 dwarf2_emit_label (symbolS *label)
393 struct dwarf2_line_info loc;
395 if (!dwarf2_loc_mark_labels)
396 return;
397 if (S_GET_SEGMENT (label) != now_seg)
398 return;
399 if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE))
400 return;
402 if (debug_type == DEBUG_DWARF2)
403 dwarf2_where (&loc);
404 else
406 loc = current;
407 loc_directive_seen = FALSE;
410 loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
412 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
413 | DWARF2_FLAG_PROLOGUE_END
414 | DWARF2_FLAG_EPILOGUE_BEGIN);
416 dwarf2_gen_line_info_1 (label, &loc);
419 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
420 allocate it on that file table slot, otherwise return the first
421 empty one. */
423 static unsigned int
424 get_filenum (const char *filename, unsigned int num)
426 static unsigned int last_used, last_used_dir_len;
427 const char *file;
428 size_t dir_len;
429 unsigned int i, dir;
431 if (num == 0 && last_used)
433 if (! files[last_used].dir
434 && strcmp (filename, files[last_used].filename) == 0)
435 return last_used;
436 if (files[last_used].dir
437 && strncmp (filename, dirs[files[last_used].dir],
438 last_used_dir_len) == 0
439 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
440 && strcmp (filename + last_used_dir_len + 1,
441 files[last_used].filename) == 0)
442 return last_used;
445 file = lbasename (filename);
446 /* Don't make empty string from / or A: from A:/ . */
447 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
448 if (file <= filename + 3)
449 file = filename;
450 #else
451 if (file == filename + 1)
452 file = filename;
453 #endif
454 dir_len = file - filename;
456 dir = 0;
457 if (dir_len)
459 --dir_len;
460 for (dir = 1; dir < dirs_in_use; ++dir)
461 if (strncmp (filename, dirs[dir], dir_len) == 0
462 && dirs[dir][dir_len] == '\0')
463 break;
465 if (dir >= dirs_in_use)
467 if (dir >= dirs_allocated)
469 dirs_allocated = dir + 32;
470 dirs = (char **)
471 xrealloc (dirs, (dir + 32) * sizeof (const char *));
474 dirs[dir] = xmalloc (dir_len + 1);
475 memcpy (dirs[dir], filename, dir_len);
476 dirs[dir][dir_len] = '\0';
477 dirs_in_use = dir + 1;
481 if (num == 0)
483 for (i = 1; i < files_in_use; ++i)
484 if (files[i].dir == dir
485 && files[i].filename
486 && strcmp (file, files[i].filename) == 0)
488 last_used = i;
489 last_used_dir_len = dir_len;
490 return i;
493 else
494 i = num;
496 if (i >= files_allocated)
498 unsigned int old = files_allocated;
500 files_allocated = i + 32;
501 files = (struct file_entry *)
502 xrealloc (files, (i + 32) * sizeof (struct file_entry));
504 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
507 files[i].filename = num ? file : xstrdup (file);
508 files[i].dir = dir;
509 if (files_in_use < i + 1)
510 files_in_use = i + 1;
511 last_used = i;
512 last_used_dir_len = dir_len;
514 return i;
517 /* Handle two forms of .file directive:
518 - Pass .file "source.c" to s_app_file
519 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
521 If an entry is added to the file table, return a pointer to the filename. */
523 char *
524 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
526 offsetT num;
527 char *filename;
528 int filename_len;
530 /* Continue to accept a bare string and pass it off. */
531 SKIP_WHITESPACE ();
532 if (*input_line_pointer == '"')
534 s_app_file (0);
535 return NULL;
538 num = get_absolute_expression ();
539 filename = demand_copy_C_string (&filename_len);
540 if (filename == NULL)
541 return NULL;
542 demand_empty_rest_of_line ();
544 if (num < 1)
546 as_bad (_("file number less than one"));
547 return NULL;
550 if (num < (int) files_in_use && files[num].filename != 0)
552 as_bad (_("file number %ld already allocated"), (long) num);
553 return NULL;
556 get_filenum (filename, num);
558 return filename;
561 void
562 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
564 offsetT filenum, line;
566 filenum = get_absolute_expression ();
567 SKIP_WHITESPACE ();
568 line = get_absolute_expression ();
570 if (filenum < 1)
572 as_bad (_("file number less than one"));
573 return;
575 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
577 as_bad (_("unassigned file number %ld"), (long) filenum);
578 return;
581 current.filenum = filenum;
582 current.line = line;
584 #ifndef NO_LISTING
585 if (listing)
587 if (files[filenum].dir)
589 size_t dir_len = strlen (dirs[files[filenum].dir]);
590 size_t file_len = strlen (files[filenum].filename);
591 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
593 memcpy (cp, dirs[files[filenum].dir], dir_len);
594 INSERT_DIR_SEPARATOR (cp, dir_len);
595 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
596 cp[dir_len + file_len + 1] = '\0';
597 listing_source_file (cp);
599 else
600 listing_source_file (files[filenum].filename);
601 listing_source_line (line);
603 #endif
605 SKIP_WHITESPACE ();
606 if (ISDIGIT (*input_line_pointer))
608 current.column = get_absolute_expression ();
609 SKIP_WHITESPACE ();
612 while (ISALPHA (*input_line_pointer))
614 char *p, c;
615 offsetT value;
617 p = input_line_pointer;
618 c = get_symbol_end ();
620 if (strcmp (p, "basic_block") == 0)
622 current.flags |= DWARF2_FLAG_BASIC_BLOCK;
623 *input_line_pointer = c;
625 else if (strcmp (p, "prologue_end") == 0)
627 current.flags |= DWARF2_FLAG_PROLOGUE_END;
628 *input_line_pointer = c;
630 else if (strcmp (p, "epilogue_begin") == 0)
632 current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
633 *input_line_pointer = c;
635 else if (strcmp (p, "is_stmt") == 0)
637 *input_line_pointer = c;
638 value = get_absolute_expression ();
639 if (value == 0)
640 current.flags &= ~DWARF2_FLAG_IS_STMT;
641 else if (value == 1)
642 current.flags |= DWARF2_FLAG_IS_STMT;
643 else
645 as_bad (_("is_stmt value not 0 or 1"));
646 return;
649 else if (strcmp (p, "isa") == 0)
651 *input_line_pointer = c;
652 value = get_absolute_expression ();
653 if (value >= 0)
654 current.isa = value;
655 else
657 as_bad (_("isa number less than zero"));
658 return;
661 else
663 as_bad (_("unknown .loc sub-directive `%s'"), p);
664 *input_line_pointer = c;
665 return;
668 SKIP_WHITESPACE ();
671 demand_empty_rest_of_line ();
672 loc_directive_seen = TRUE;
675 void
676 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
678 offsetT value = get_absolute_expression ();
680 if (value != 0 && value != 1)
682 as_bad (_("expected 0 or 1"));
683 ignore_rest_of_line ();
685 else
687 dwarf2_loc_mark_labels = value != 0;
688 demand_empty_rest_of_line ();
692 static struct frag *
693 first_frag_for_seg (segT seg)
695 return seg_info (seg)->frchainP->frch_root;
698 static struct frag *
699 last_frag_for_seg (segT seg)
701 frchainS *f = seg_info (seg)->frchainP;
703 while (f->frch_next != NULL)
704 f = f->frch_next;
706 return f->frch_last;
709 /* Emit a single byte into the current segment. */
711 static inline void
712 out_byte (int byte)
714 FRAG_APPEND_1_CHAR (byte);
717 /* Emit a statement program opcode into the current segment. */
719 static inline void
720 out_opcode (int opc)
722 out_byte (opc);
725 /* Emit a two-byte word into the current segment. */
727 static inline void
728 out_two (int data)
730 md_number_to_chars (frag_more (2), data, 2);
733 /* Emit a four byte word into the current segment. */
735 static inline void
736 out_four (int data)
738 md_number_to_chars (frag_more (4), data, 4);
741 /* Emit an unsigned "little-endian base 128" number. */
743 static void
744 out_uleb128 (addressT value)
746 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
749 /* Emit a tuple for .debug_abbrev. */
751 static inline void
752 out_abbrev (int name, int form)
754 out_uleb128 (name);
755 out_uleb128 (form);
758 /* Get the size of a fragment. */
760 static offsetT
761 get_frag_fix (fragS *frag, segT seg)
763 frchainS *fr;
765 if (frag->fr_next)
766 return frag->fr_fix;
768 /* If a fragment is the last in the chain, special measures must be
769 taken to find its size before relaxation, since it may be pending
770 on some subsegment chain. */
771 for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
772 if (fr->frch_last == frag)
773 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
775 abort ();
778 /* Set an absolute address (may result in a relocation entry). */
780 static void
781 out_set_addr (symbolS *sym)
783 expressionS expr;
785 out_opcode (DW_LNS_extended_op);
786 out_uleb128 (sizeof_address + 1);
788 out_opcode (DW_LNE_set_address);
789 expr.X_op = O_symbol;
790 expr.X_add_symbol = sym;
791 expr.X_add_number = 0;
792 emit_expr (&expr, sizeof_address);
795 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
796 static void scale_addr_delta (addressT *);
798 static void
799 scale_addr_delta (addressT *addr_delta)
801 static int printed_this = 0;
802 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
804 if (!printed_this)
805 as_bad("unaligned opcodes detected in executable segment");
806 printed_this = 1;
808 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
810 #else
811 #define scale_addr_delta(A)
812 #endif
814 /* Encode a pair of line and address skips as efficiently as possible.
815 Note that the line skip is signed, whereas the address skip is unsigned.
817 The following two routines *must* be kept in sync. This is
818 enforced by making emit_inc_line_addr abort if we do not emit
819 exactly the expected number of bytes. */
821 static int
822 size_inc_line_addr (int line_delta, addressT addr_delta)
824 unsigned int tmp, opcode;
825 int len = 0;
827 /* Scale the address delta by the minimum instruction length. */
828 scale_addr_delta (&addr_delta);
830 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
831 We cannot use special opcodes here, since we want the end_sequence
832 to emit the matrix entry. */
833 if (line_delta == INT_MAX)
835 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
836 len = 1;
837 else
838 len = 1 + sizeof_leb128 (addr_delta, 0);
839 return len + 3;
842 /* Bias the line delta by the base. */
843 tmp = line_delta - DWARF2_LINE_BASE;
845 /* If the line increment is out of range of a special opcode, we
846 must encode it with DW_LNS_advance_line. */
847 if (tmp >= DWARF2_LINE_RANGE)
849 len = 1 + sizeof_leb128 (line_delta, 1);
850 line_delta = 0;
851 tmp = 0 - DWARF2_LINE_BASE;
854 /* Bias the opcode by the special opcode base. */
855 tmp += DWARF2_LINE_OPCODE_BASE;
857 /* Avoid overflow when addr_delta is large. */
858 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
860 /* Try using a special opcode. */
861 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
862 if (opcode <= 255)
863 return len + 1;
865 /* Try using DW_LNS_const_add_pc followed by special op. */
866 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
867 if (opcode <= 255)
868 return len + 2;
871 /* Otherwise use DW_LNS_advance_pc. */
872 len += 1 + sizeof_leb128 (addr_delta, 0);
874 /* DW_LNS_copy or special opcode. */
875 len += 1;
877 return len;
880 static void
881 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
883 unsigned int tmp, opcode;
884 int need_copy = 0;
885 char *end = p + len;
887 /* Line number sequences cannot go backward in addresses. This means
888 we've incorrectly ordered the statements in the sequence. */
889 assert ((offsetT) addr_delta >= 0);
891 /* Scale the address delta by the minimum instruction length. */
892 scale_addr_delta (&addr_delta);
894 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
895 We cannot use special opcodes here, since we want the end_sequence
896 to emit the matrix entry. */
897 if (line_delta == INT_MAX)
899 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
900 *p++ = DW_LNS_const_add_pc;
901 else
903 *p++ = DW_LNS_advance_pc;
904 p += output_leb128 (p, addr_delta, 0);
907 *p++ = DW_LNS_extended_op;
908 *p++ = 1;
909 *p++ = DW_LNE_end_sequence;
910 goto done;
913 /* Bias the line delta by the base. */
914 tmp = line_delta - DWARF2_LINE_BASE;
916 /* If the line increment is out of range of a special opcode, we
917 must encode it with DW_LNS_advance_line. */
918 if (tmp >= DWARF2_LINE_RANGE)
920 *p++ = DW_LNS_advance_line;
921 p += output_leb128 (p, line_delta, 1);
923 line_delta = 0;
924 tmp = 0 - DWARF2_LINE_BASE;
925 need_copy = 1;
928 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
929 special opcode. */
930 if (line_delta == 0 && addr_delta == 0)
932 *p++ = DW_LNS_copy;
933 goto done;
936 /* Bias the opcode by the special opcode base. */
937 tmp += DWARF2_LINE_OPCODE_BASE;
939 /* Avoid overflow when addr_delta is large. */
940 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
942 /* Try using a special opcode. */
943 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
944 if (opcode <= 255)
946 *p++ = opcode;
947 goto done;
950 /* Try using DW_LNS_const_add_pc followed by special op. */
951 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
952 if (opcode <= 255)
954 *p++ = DW_LNS_const_add_pc;
955 *p++ = opcode;
956 goto done;
960 /* Otherwise use DW_LNS_advance_pc. */
961 *p++ = DW_LNS_advance_pc;
962 p += output_leb128 (p, addr_delta, 0);
964 if (need_copy)
965 *p++ = DW_LNS_copy;
966 else
967 *p++ = tmp;
969 done:
970 assert (p == end);
973 /* Handy routine to combine calls to the above two routines. */
975 static void
976 out_inc_line_addr (int line_delta, addressT addr_delta)
978 int len = size_inc_line_addr (line_delta, addr_delta);
979 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
982 /* Generate a variant frag that we can use to relax address/line
983 increments between fragments of the target segment. */
985 static void
986 relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
988 expressionS expr;
989 int max_chars;
991 expr.X_op = O_subtract;
992 expr.X_add_symbol = to_sym;
993 expr.X_op_symbol = from_sym;
994 expr.X_add_number = 0;
996 /* The maximum size of the frag is the line delta with a maximum
997 sized address delta. */
998 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
1000 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
1001 make_expr_symbol (&expr), line_delta, NULL);
1004 /* The function estimates the size of a rs_dwarf2dbg variant frag
1005 based on the current values of the symbols. It is called before
1006 the relaxation loop. We set fr_subtype to the expected length. */
1009 dwarf2dbg_estimate_size_before_relax (fragS *frag)
1011 offsetT addr_delta;
1012 int size;
1014 addr_delta = resolve_symbol_value (frag->fr_symbol);
1015 size = size_inc_line_addr (frag->fr_offset, addr_delta);
1017 frag->fr_subtype = size;
1019 return size;
1022 /* This function relaxes a rs_dwarf2dbg variant frag based on the
1023 current values of the symbols. fr_subtype is the current length
1024 of the frag. This returns the change in frag length. */
1027 dwarf2dbg_relax_frag (fragS *frag)
1029 int old_size, new_size;
1031 old_size = frag->fr_subtype;
1032 new_size = dwarf2dbg_estimate_size_before_relax (frag);
1034 return new_size - old_size;
1037 /* This function converts a rs_dwarf2dbg variant frag into a normal
1038 fill frag. This is called after all relaxation has been done.
1039 fr_subtype will be the desired length of the frag. */
1041 void
1042 dwarf2dbg_convert_frag (fragS *frag)
1044 offsetT addr_diff;
1046 addr_diff = resolve_symbol_value (frag->fr_symbol);
1048 /* fr_var carries the max_chars that we created the fragment with.
1049 fr_subtype carries the current expected length. We must, of
1050 course, have allocated enough memory earlier. */
1051 assert (frag->fr_var >= (int) frag->fr_subtype);
1053 emit_inc_line_addr (frag->fr_offset, addr_diff,
1054 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
1056 frag->fr_fix += frag->fr_subtype;
1057 frag->fr_type = rs_fill;
1058 frag->fr_var = 0;
1059 frag->fr_offset = 0;
1062 /* Generate .debug_line content for the chain of line number entries
1063 beginning at E, for segment SEG. */
1065 static void
1066 process_entries (segT seg, struct line_entry *e)
1068 unsigned filenum = 1;
1069 unsigned line = 1;
1070 unsigned column = 0;
1071 unsigned isa = 0;
1072 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1073 fragS *last_frag = NULL, *frag;
1074 addressT last_frag_ofs = 0, frag_ofs;
1075 symbolS *last_lab = NULL, *lab;
1076 struct line_entry *next;
1080 int line_delta;
1082 if (filenum != e->loc.filenum)
1084 filenum = e->loc.filenum;
1085 out_opcode (DW_LNS_set_file);
1086 out_uleb128 (filenum);
1089 if (column != e->loc.column)
1091 column = e->loc.column;
1092 out_opcode (DW_LNS_set_column);
1093 out_uleb128 (column);
1096 if (isa != e->loc.isa)
1098 isa = e->loc.isa;
1099 out_opcode (DW_LNS_set_isa);
1100 out_uleb128 (isa);
1103 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
1105 flags = e->loc.flags;
1106 out_opcode (DW_LNS_negate_stmt);
1109 if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
1110 out_opcode (DW_LNS_set_basic_block);
1112 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
1113 out_opcode (DW_LNS_set_prologue_end);
1115 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1116 out_opcode (DW_LNS_set_epilogue_begin);
1118 /* Don't try to optimize away redundant entries; gdb wants two
1119 entries for a function where the code starts on the same line as
1120 the {, and there's no way to identify that case here. Trust gcc
1121 to optimize appropriately. */
1122 line_delta = e->loc.line - line;
1123 lab = e->label;
1124 frag = symbol_get_frag (lab);
1125 frag_ofs = S_GET_VALUE (lab);
1127 if (last_frag == NULL)
1129 out_set_addr (lab);
1130 out_inc_line_addr (line_delta, 0);
1132 else if (frag == last_frag)
1133 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
1134 else
1135 relax_inc_line_addr (line_delta, lab, last_lab);
1137 line = e->loc.line;
1138 last_lab = lab;
1139 last_frag = frag;
1140 last_frag_ofs = frag_ofs;
1142 next = e->next;
1143 free (e);
1144 e = next;
1146 while (e);
1148 /* Emit a DW_LNE_end_sequence for the end of the section. */
1149 frag = last_frag_for_seg (seg);
1150 frag_ofs = get_frag_fix (frag, seg);
1151 if (frag == last_frag)
1152 out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
1153 else
1155 lab = symbol_temp_new (seg, frag_ofs, frag);
1156 relax_inc_line_addr (INT_MAX, lab, last_lab);
1160 /* Emit the directory and file tables for .debug_line. */
1162 static void
1163 out_file_list (void)
1165 size_t size;
1166 char *cp;
1167 unsigned int i;
1169 /* Emit directory list. */
1170 for (i = 1; i < dirs_in_use; ++i)
1172 size = strlen (dirs[i]) + 1;
1173 cp = frag_more (size);
1174 memcpy (cp, dirs[i], size);
1176 /* Terminate it. */
1177 out_byte ('\0');
1179 for (i = 1; i < files_in_use; ++i)
1181 if (files[i].filename == NULL)
1183 as_bad (_("unassigned file number %ld"), (long) i);
1184 /* Prevent a crash later, particularly for file 1. */
1185 files[i].filename = "";
1186 continue;
1189 size = strlen (files[i].filename) + 1;
1190 cp = frag_more (size);
1191 memcpy (cp, files[i].filename, size);
1193 out_uleb128 (files[i].dir); /* directory number */
1194 out_uleb128 (0); /* last modification timestamp */
1195 out_uleb128 (0); /* filesize */
1198 /* Terminate filename list. */
1199 out_byte (0);
1202 /* Emit the collected .debug_line data. */
1204 static void
1205 out_debug_line (segT line_seg)
1207 expressionS expr;
1208 symbolS *line_start;
1209 symbolS *prologue_end;
1210 symbolS *line_end;
1211 struct line_seg *s;
1212 enum dwarf2_format d2f;
1213 int sizeof_offset;
1215 subseg_set (line_seg, 0);
1217 line_start = symbol_temp_new_now ();
1218 prologue_end = symbol_temp_make ();
1219 line_end = symbol_temp_make ();
1221 /* Total length of the information for this compilation unit. */
1222 expr.X_op = O_subtract;
1223 expr.X_add_symbol = line_end;
1224 expr.X_op_symbol = line_start;
1226 d2f = DWARF2_FORMAT ();
1227 if (d2f == dwarf2_format_32bit)
1229 expr.X_add_number = -4;
1230 emit_expr (&expr, 4);
1231 sizeof_offset = 4;
1233 else if (d2f == dwarf2_format_64bit)
1235 expr.X_add_number = -12;
1236 out_four (-1);
1237 emit_expr (&expr, 8);
1238 sizeof_offset = 8;
1240 else if (d2f == dwarf2_format_64bit_irix)
1242 expr.X_add_number = -8;
1243 emit_expr (&expr, 8);
1244 sizeof_offset = 8;
1246 else
1248 as_fatal (_("internal error: unknown dwarf2 format"));
1251 /* Version. */
1252 out_two (2);
1254 /* Length of the prologue following this length. */
1255 expr.X_op = O_subtract;
1256 expr.X_add_symbol = prologue_end;
1257 expr.X_op_symbol = line_start;
1258 expr.X_add_number = - (4 + 2 + 4);
1259 emit_expr (&expr, sizeof_offset);
1261 /* Parameters of the state machine. */
1262 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1263 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1264 out_byte (DWARF2_LINE_BASE);
1265 out_byte (DWARF2_LINE_RANGE);
1266 out_byte (DWARF2_LINE_OPCODE_BASE);
1268 /* Standard opcode lengths. */
1269 out_byte (0); /* DW_LNS_copy */
1270 out_byte (1); /* DW_LNS_advance_pc */
1271 out_byte (1); /* DW_LNS_advance_line */
1272 out_byte (1); /* DW_LNS_set_file */
1273 out_byte (1); /* DW_LNS_set_column */
1274 out_byte (0); /* DW_LNS_negate_stmt */
1275 out_byte (0); /* DW_LNS_set_basic_block */
1276 out_byte (0); /* DW_LNS_const_add_pc */
1277 out_byte (1); /* DW_LNS_fixed_advance_pc */
1278 out_byte (0); /* DW_LNS_set_prologue_end */
1279 out_byte (0); /* DW_LNS_set_epilogue_begin */
1280 out_byte (1); /* DW_LNS_set_isa */
1282 out_file_list ();
1284 symbol_set_value_now (prologue_end);
1286 /* For each section, emit a statement program. */
1287 for (s = all_segs; s; s = s->next)
1288 process_entries (s->seg, s->head->head);
1290 symbol_set_value_now (line_end);
1293 /* Emit data for .debug_aranges. */
1295 static void
1296 out_debug_aranges (segT aranges_seg, segT info_seg)
1298 unsigned int addr_size = sizeof_address;
1299 addressT size, skip;
1300 struct line_seg *s;
1301 expressionS expr;
1302 char *p;
1304 size = 4 + 2 + 4 + 1 + 1;
1306 skip = 2 * addr_size - (size & (2 * addr_size - 1));
1307 if (skip == 2 * addr_size)
1308 skip = 0;
1309 size += skip;
1311 for (s = all_segs; s; s = s->next)
1312 size += 2 * addr_size;
1314 size += 2 * addr_size;
1316 subseg_set (aranges_seg, 0);
1318 /* Length of the compilation unit. */
1319 out_four (size - 4);
1321 /* Version. */
1322 out_two (2);
1324 /* Offset to .debug_info. */
1325 /* ??? sizeof_offset */
1326 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), 4);
1328 /* Size of an address (offset portion). */
1329 out_byte (addr_size);
1331 /* Size of a segment descriptor. */
1332 out_byte (0);
1334 /* Align the header. */
1335 if (skip)
1336 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1338 for (s = all_segs; s; s = s->next)
1340 fragS *frag;
1341 symbolS *beg, *end;
1343 frag = first_frag_for_seg (s->seg);
1344 beg = symbol_temp_new (s->seg, 0, frag);
1345 s->text_start = beg;
1347 frag = last_frag_for_seg (s->seg);
1348 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1349 s->text_end = end;
1351 expr.X_op = O_symbol;
1352 expr.X_add_symbol = beg;
1353 expr.X_add_number = 0;
1354 emit_expr (&expr, addr_size);
1356 expr.X_op = O_subtract;
1357 expr.X_add_symbol = end;
1358 expr.X_op_symbol = beg;
1359 expr.X_add_number = 0;
1360 emit_expr (&expr, addr_size);
1363 p = frag_more (2 * addr_size);
1364 md_number_to_chars (p, 0, addr_size);
1365 md_number_to_chars (p + addr_size, 0, addr_size);
1368 /* Emit data for .debug_abbrev. Note that this must be kept in
1369 sync with out_debug_info below. */
1371 static void
1372 out_debug_abbrev (segT abbrev_seg)
1374 subseg_set (abbrev_seg, 0);
1376 out_uleb128 (1);
1377 out_uleb128 (DW_TAG_compile_unit);
1378 out_byte (DW_CHILDREN_no);
1379 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1380 if (all_segs->next == NULL)
1382 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1383 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1385 out_abbrev (DW_AT_name, DW_FORM_string);
1386 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1387 out_abbrev (DW_AT_producer, DW_FORM_string);
1388 out_abbrev (DW_AT_language, DW_FORM_data2);
1389 out_abbrev (0, 0);
1391 /* Terminate the abbreviations for this compilation unit. */
1392 out_byte (0);
1395 /* Emit a description of this compilation unit for .debug_info. */
1397 static void
1398 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg)
1400 char producer[128];
1401 char *comp_dir;
1402 expressionS expr;
1403 symbolS *info_start;
1404 symbolS *info_end;
1405 char *p;
1406 int len;
1407 enum dwarf2_format d2f;
1408 int sizeof_offset;
1410 subseg_set (info_seg, 0);
1412 info_start = symbol_temp_new_now ();
1413 info_end = symbol_temp_make ();
1415 /* Compilation Unit length. */
1416 expr.X_op = O_subtract;
1417 expr.X_add_symbol = info_end;
1418 expr.X_op_symbol = info_start;
1420 d2f = DWARF2_FORMAT ();
1421 if (d2f == dwarf2_format_32bit)
1423 expr.X_add_number = -4;
1424 emit_expr (&expr, 4);
1425 sizeof_offset = 4;
1427 else if (d2f == dwarf2_format_64bit)
1429 expr.X_add_number = -12;
1430 out_four (-1);
1431 emit_expr (&expr, 8);
1432 sizeof_offset = 8;
1434 else if (d2f == dwarf2_format_64bit_irix)
1436 expr.X_add_number = -8;
1437 emit_expr (&expr, 8);
1438 sizeof_offset = 8;
1440 else
1442 as_fatal (_("internal error: unknown dwarf2 format"));
1445 /* DWARF version. */
1446 out_two (2);
1448 /* .debug_abbrev offset */
1449 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1451 /* Target address size. */
1452 out_byte (sizeof_address);
1454 /* DW_TAG_compile_unit DIE abbrev */
1455 out_uleb128 (1);
1457 /* DW_AT_stmt_list */
1458 /* ??? sizeof_offset */
1459 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 4);
1461 /* These two attributes may only be emitted if all of the code is
1462 contiguous. Multiple sections are not that. */
1463 if (all_segs->next == NULL)
1465 /* DW_AT_low_pc */
1466 expr.X_op = O_symbol;
1467 expr.X_add_symbol = all_segs->text_start;
1468 expr.X_add_number = 0;
1469 emit_expr (&expr, sizeof_address);
1471 /* DW_AT_high_pc */
1472 expr.X_op = O_symbol;
1473 expr.X_add_symbol = all_segs->text_end;
1474 expr.X_add_number = 0;
1475 emit_expr (&expr, sizeof_address);
1478 /* DW_AT_name. We don't have the actual file name that was present
1479 on the command line, so assume files[1] is the main input file.
1480 We're not supposed to get called unless at least one line number
1481 entry was emitted, so this should always be defined. */
1482 if (!files || files_in_use < 1)
1483 abort ();
1484 if (files[1].dir)
1486 len = strlen (dirs[files[1].dir]);
1487 p = frag_more (len + 1);
1488 memcpy (p, dirs[files[1].dir], len);
1489 INSERT_DIR_SEPARATOR (p, len);
1491 len = strlen (files[1].filename) + 1;
1492 p = frag_more (len);
1493 memcpy (p, files[1].filename, len);
1495 /* DW_AT_comp_dir */
1496 comp_dir = getpwd ();
1497 len = strlen (comp_dir) + 1;
1498 p = frag_more (len);
1499 memcpy (p, comp_dir, len);
1501 /* DW_AT_producer */
1502 sprintf (producer, "GNU AS %s", VERSION);
1503 len = strlen (producer) + 1;
1504 p = frag_more (len);
1505 memcpy (p, producer, len);
1507 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1508 dwarf2 draft has no standard code for assembler. */
1509 out_two (DW_LANG_Mips_Assembler);
1511 symbol_set_value_now (info_end);
1514 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1515 were any .file/.loc directives, or --gdwarf2 was given, or if the
1516 file has a non-empty .debug_info section. If we emit .debug_line,
1517 and the .debug_info section is empty, we also emit .debug_info,
1518 .debug_aranges and .debug_abbrev. ALL_SEGS will be non-null if
1519 there were any .file/.loc directives, or --gdwarf2 was given and
1520 there were any located instructions emitted. */
1522 void
1523 dwarf2_finish (void)
1525 segT line_seg;
1526 struct line_seg *s;
1527 segT info_seg;
1528 int emit_other_sections = 0;
1530 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
1531 emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
1533 if (!all_segs && emit_other_sections)
1534 /* There is no line information and no non-empty .debug_info
1535 section. */
1536 return;
1538 /* Calculate the size of an address for the target machine. */
1539 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1541 /* Create and switch to the line number section. */
1542 line_seg = subseg_new (".debug_line", 0);
1543 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
1545 /* For each subsection, chain the debug entries together. */
1546 for (s = all_segs; s; s = s->next)
1548 struct line_subseg *ss = s->head;
1549 struct line_entry **ptail = ss->ptail;
1551 while ((ss = ss->next) != NULL)
1553 *ptail = ss->head;
1554 ptail = ss->ptail;
1558 out_debug_line (line_seg);
1560 /* If this is assembler generated line info, and there is no
1561 debug_info already, we need .debug_info and .debug_abbrev
1562 sections as well. */
1563 if (emit_other_sections)
1565 segT abbrev_seg;
1566 segT aranges_seg;
1568 assert (all_segs);
1570 info_seg = subseg_new (".debug_info", 0);
1571 abbrev_seg = subseg_new (".debug_abbrev", 0);
1572 aranges_seg = subseg_new (".debug_aranges", 0);
1574 bfd_set_section_flags (stdoutput, info_seg,
1575 SEC_READONLY | SEC_DEBUGGING);
1576 bfd_set_section_flags (stdoutput, abbrev_seg,
1577 SEC_READONLY | SEC_DEBUGGING);
1578 bfd_set_section_flags (stdoutput, aranges_seg,
1579 SEC_READONLY | SEC_DEBUGGING);
1581 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1583 out_debug_aranges (aranges_seg, info_seg);
1584 out_debug_abbrev (abbrev_seg);
1585 out_debug_info (info_seg, abbrev_seg, line_seg);