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)
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
23 /* Logical line numbers can be controlled by the compiler via the
27 .loc FILENO LINENO [COLUMN]
37 #include "safe-ctype.h"
42 #ifdef HAVE_SYS_PARAM_H
43 #include <sys/param.h>
46 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
50 #include "dwarf2dbg.h"
51 #include <filenames.h>
54 # define DWARF2_FORMAT() dwarf2_format_32bit
57 #ifndef DWARF2_ADDR_SIZE
58 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
63 #include "elf/dwarf2.h"
65 /* Since we can't generate the prolog until the body is complete, we
66 use three different subsegments for .debug_line: one holding the
67 prolog, one for the directory and filename info, and one for the
68 body ("statement program"). */
73 /* First special line opcde - leave room for the standard opcodes.
74 Note: If you want to change this, you'll have to update the
75 "standard_opcode_lengths" table that is emitted below in
77 #define DWARF2_LINE_OPCODE_BASE 13
79 #ifndef DWARF2_LINE_BASE
80 /* Minimum line offset in a special line info. opcode. This value
81 was chosen to give a reasonable range of values. */
82 # define DWARF2_LINE_BASE -5
85 /* Range of line offsets in a special line info. opcode. */
86 #ifndef DWARF2_LINE_RANGE
87 # define DWARF2_LINE_RANGE 14
90 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
91 /* Define the architecture-dependent minimum instruction length (in
92 bytes). This value should be rather too small than too big. */
93 # define DWARF2_LINE_MIN_INSN_LENGTH 1
96 /* Flag that indicates the initial value of the is_stmt_start flag. */
97 #define DWARF2_LINE_DEFAULT_IS_STMT 1
99 /* Given a special op, return the line skip amount. */
100 #define SPECIAL_LINE(op) \
101 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
103 /* Given a special op, return the address skip amount (in units of
104 DWARF2_LINE_MIN_INSN_LENGTH. */
105 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
107 /* The maximum address skip amount that can be encoded with a special op. */
108 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
111 struct line_entry *next;
114 struct dwarf2_line_info loc;
118 struct line_subseg *next;
120 struct line_entry *head;
121 struct line_entry **ptail;
125 struct line_seg *next;
127 struct line_subseg *head;
132 /* Collects data for all line table entries during assembly. */
133 static struct line_seg *all_segs;
136 const char *filename;
140 /* Table of files used by .debug_line. */
141 static struct file_entry *files;
142 static unsigned int files_in_use;
143 static unsigned int files_allocated;
145 /* Table of directories used by .debug_line. */
147 static unsigned int dirs_in_use;
148 static unsigned int dirs_allocated;
150 /* TRUE when we've seen a .loc directive recently. Used to avoid
151 doing work when there's nothing to do. */
152 static bfd_boolean loc_directive_seen;
154 /* Current location as indicated by the most recent .loc directive. */
155 static struct dwarf2_line_info current = {
157 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0
160 /* The size of an address on the target. */
161 static unsigned int sizeof_address;
163 static struct line_subseg *get_line_subseg (segT, subsegT);
164 static unsigned int get_filenum (const char *, unsigned int);
165 static struct frag *first_frag_for_seg (segT);
166 static struct frag *last_frag_for_seg (segT);
167 static void out_byte (int);
168 static void out_opcode (int);
169 static void out_two (int);
170 static void out_four (int);
171 static void out_abbrev (int, int);
172 static void out_uleb128 (addressT);
173 static offsetT get_frag_fix (fragS *);
174 static void out_set_addr (segT, fragS *, addressT);
175 static int size_inc_line_addr (int, addressT);
176 static void emit_inc_line_addr (int, addressT, char *, int);
177 static void out_inc_line_addr (int, addressT);
178 static void relax_inc_line_addr (int, segT, fragS *, addressT,
180 static void process_entries (segT, struct line_entry *);
181 static void out_file_list (void);
182 static void out_debug_line (segT);
183 static void out_debug_aranges (segT, segT);
184 static void out_debug_abbrev (segT);
185 static void out_debug_info (segT, segT, segT);
187 #ifndef TC_DWARF2_EMIT_OFFSET
188 # define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
189 static void generic_dwarf2_emit_offset (symbolS *, unsigned int);
191 /* Create an offset to .dwarf2_*. */
194 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
198 expr.X_op = O_symbol;
199 expr.X_add_symbol = symbol;
200 expr.X_add_number = 0;
201 emit_expr (&expr, size);
205 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
207 static struct line_subseg *
208 get_line_subseg (segT seg, subsegT subseg)
210 static segT last_seg;
211 static subsegT last_subseg;
212 static struct line_subseg *last_line_subseg;
215 struct line_subseg **pss, *ss;
217 if (seg == last_seg && subseg == last_subseg)
218 return last_line_subseg;
220 for (s = all_segs; s; s = s->next)
224 s = (struct line_seg *) xmalloc (sizeof (*s));
231 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
233 if (ss->subseg == subseg)
235 if (ss->subseg > subseg)
239 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
243 ss->ptail = &ss->head;
248 last_subseg = subseg;
249 last_line_subseg = ss;
254 /* Record an entry for LOC occurring at OFS within the current fragment. */
257 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
259 struct line_subseg *ss;
260 struct line_entry *e;
261 static unsigned int line = -1;
262 static unsigned int filenum = -1;
264 /* Early out for as-yet incomplete location information. */
265 if (loc->filenum == 0 || loc->line == 0)
268 /* Don't emit sequences of line symbols for the same line when the
269 symbols apply to assembler code. It is necessary to emit
270 duplicate line symbols when a compiler asks for them, because GDB
271 uses them to determine the end of the prologue. */
272 if (debug_type == DEBUG_DWARF2
273 && line == loc->line && filenum == loc->filenum)
277 filenum = loc->filenum;
279 e = (struct line_entry *) xmalloc (sizeof (*e));
285 ss = get_line_subseg (now_seg, now_subseg);
287 ss->ptail = &e->next;
291 dwarf2_where (struct dwarf2_line_info *line)
293 if (debug_type == DEBUG_DWARF2)
296 as_where (&filename, &line->line);
297 line->filenum = get_filenum (filename, 0);
299 line->flags = DWARF2_FLAG_IS_STMT;
305 /* Called for each machine instruction, or relatively atomic group of
306 machine instructions (ie built-in macro). The instruction or group
307 is SIZE bytes in length. If dwarf2 line number generation is called
308 for, emit a line statement appropriately. */
311 dwarf2_emit_insn (int size)
313 struct dwarf2_line_info loc;
315 if (loc_directive_seen)
317 /* Use the last location established by a .loc directive, not
318 the value returned by dwarf2_where(). That calls as_where()
319 which will return either the logical input file name (foo.c)
320 or the physical input file name (foo.s) and not the file name
321 specified in the most recent .loc directive (eg foo.h). */
324 /* Unless we generate DWARF2 debugging information for each
325 assembler line, we only emit one line symbol for one LOC. */
326 if (debug_type != DEBUG_DWARF2)
327 loc_directive_seen = FALSE;
329 else if (debug_type != DEBUG_DWARF2)
334 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
336 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
337 | DWARF2_FLAG_PROLOGUE_END
338 | DWARF2_FLAG_EPILOGUE_BEGIN);
341 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
342 allocate it on that file table slot, otherwise return the first
346 get_filenum (const char *filename, unsigned int num)
348 static unsigned int last_used, last_used_dir_len;
353 if (num == 0 && last_used)
355 if (! files[last_used].dir
356 && strcmp (filename, files[last_used].filename) == 0)
358 if (files[last_used].dir
359 && strncmp (filename, dirs[files[last_used].dir],
360 last_used_dir_len) == 0
361 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
362 && strcmp (filename + last_used_dir_len + 1,
363 files[last_used].filename) == 0)
367 file = lbasename (filename);
368 /* Don't make empty string from / or A: from A:/ . */
369 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
370 if (file <= filename + 3)
373 if (file == filename + 1)
376 dir_len = file - filename;
382 for (dir = 1; dir < dirs_in_use; ++dir)
383 if (strncmp (filename, dirs[dir], dir_len) == 0
384 && dirs[dir][dir_len] == '\0')
387 if (dir >= dirs_in_use)
389 if (dir >= dirs_allocated)
391 dirs_allocated = dir + 32;
393 xrealloc (dirs, (dir + 32) * sizeof (const char *));
396 dirs[dir] = xmalloc (dir_len + 1);
397 memcpy (dirs[dir], filename, dir_len);
398 dirs[dir][dir_len] = '\0';
399 dirs_in_use = dir + 1;
405 for (i = 1; i < files_in_use; ++i)
406 if (files[i].dir == dir
408 && strcmp (file, files[i].filename) == 0)
411 last_used_dir_len = dir_len;
418 if (i >= files_allocated)
420 unsigned int old = files_allocated;
422 files_allocated = i + 32;
423 files = (struct file_entry *)
424 xrealloc (files, (i + 32) * sizeof (struct file_entry));
426 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
429 files[i].filename = num ? file : xstrdup (file);
431 files_in_use = i + 1;
433 last_used_dir_len = dir_len;
438 /* Handle two forms of .file directive:
439 - Pass .file "source.c" to s_app_file
440 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
442 If an entry is added to the file table, return a pointer to the filename. */
445 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
451 /* Continue to accept a bare string and pass it off. */
453 if (*input_line_pointer == '"')
459 num = get_absolute_expression ();
460 filename = demand_copy_C_string (&filename_len);
461 if (filename == NULL)
463 demand_empty_rest_of_line ();
467 as_bad (_("file number less than one"));
471 if (num < (int) files_in_use && files[num].filename != 0)
473 as_bad (_("file number %ld already allocated"), (long) num);
477 get_filenum (filename, num);
483 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
486 if (ISALPHA (*input_line_pointer))
491 p = input_line_pointer;
492 c = get_symbol_end ();
494 if (strcmp (p, "basic_block") == 0)
496 current.flags |= DWARF2_FLAG_BASIC_BLOCK;
497 *input_line_pointer = c;
499 else if (strcmp (p, "prologue_end") == 0)
501 current.flags |= DWARF2_FLAG_PROLOGUE_END;
502 *input_line_pointer = c;
504 else if (strcmp (p, "epilogue_begin") == 0)
506 current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
507 *input_line_pointer = c;
509 else if (strcmp (p, "is_stmt") == 0)
511 *input_line_pointer = c;
512 value = get_absolute_expression ();
514 current.flags &= ~DWARF2_FLAG_IS_STMT;
516 current.flags |= DWARF2_FLAG_IS_STMT;
518 as_bad (_("is_stmt value not 0 or 1"));
520 else if (strcmp (p, "isa") == 0)
522 *input_line_pointer = c;
523 value = get_absolute_expression ();
525 as_bad (_("isa number less than zero"));
531 as_bad (_("unknown .loc sub-directive %s"), p);
532 *input_line_pointer = c;
537 offsetT filenum, line, column;
539 filenum = get_absolute_expression ();
541 line = get_absolute_expression ();
543 column = get_absolute_expression ();
547 as_bad (_("file number less than one"));
550 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
552 as_bad (_("unassigned file number %ld"), (long) filenum);
556 current.filenum = filenum;
558 current.column = column;
563 if (files[filenum].dir)
565 size_t dir_len = strlen (dirs[files[filenum].dir]);
566 size_t file_len = strlen (files[filenum].filename);
567 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
569 memcpy (cp, dirs[files[filenum].dir], dir_len);
571 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
572 cp[dir_len + file_len + 1] = '\0';
573 listing_source_file (cp);
576 listing_source_file (files[filenum].filename);
577 listing_source_line (line);
582 demand_empty_rest_of_line ();
583 loc_directive_seen = TRUE;
587 first_frag_for_seg (segT seg)
589 frchainS *f, *first = NULL;
591 for (f = frchain_root; f; f = f->frch_next)
592 if (f->frch_seg == seg
593 && (! first || first->frch_subseg > f->frch_subseg))
596 return first ? first->frch_root : NULL;
600 last_frag_for_seg (segT seg)
602 frchainS *f, *last = NULL;
604 for (f = frchain_root; f; f = f->frch_next)
605 if (f->frch_seg == seg
606 && (! last || last->frch_subseg < f->frch_subseg))
609 return last ? last->frch_last : NULL;
612 /* Emit a single byte into the current segment. */
617 FRAG_APPEND_1_CHAR (byte);
620 /* Emit a statement program opcode into the current segment. */
628 /* Emit a two-byte word into the current segment. */
633 md_number_to_chars (frag_more (2), data, 2);
636 /* Emit a four byte word into the current segment. */
641 md_number_to_chars (frag_more (4), data, 4);
644 /* Emit an unsigned "little-endian base 128" number. */
647 out_uleb128 (addressT value)
649 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
652 /* Emit a tuple for .debug_abbrev. */
655 out_abbrev (int name, int form)
661 /* Get the size of a fragment. */
664 get_frag_fix (fragS *frag)
671 /* If a fragment is the last in the chain, special measures must be
672 taken to find its size before relaxation, since it may be pending
673 on some subsegment chain. */
674 for (fr = frchain_root; fr; fr = fr->frch_next)
675 if (fr->frch_last == frag)
676 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
681 /* Set an absolute address (may result in a relocation entry). */
684 out_set_addr (segT seg, fragS *frag, addressT ofs)
689 sym = symbol_temp_new (seg, ofs, frag);
691 out_opcode (DW_LNS_extended_op);
692 out_uleb128 (sizeof_address + 1);
694 out_opcode (DW_LNE_set_address);
695 expr.X_op = O_symbol;
696 expr.X_add_symbol = sym;
697 expr.X_add_number = 0;
698 emit_expr (&expr, sizeof_address);
701 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
702 static void scale_addr_delta (addressT *);
705 scale_addr_delta (addressT *addr_delta)
707 static int printed_this = 0;
708 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
711 as_bad("unaligned opcodes detected in executable segment");
714 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
717 #define scale_addr_delta(A)
720 /* Encode a pair of line and address skips as efficiently as possible.
721 Note that the line skip is signed, whereas the address skip is unsigned.
723 The following two routines *must* be kept in sync. This is
724 enforced by making emit_inc_line_addr abort if we do not emit
725 exactly the expected number of bytes. */
728 size_inc_line_addr (int line_delta, addressT addr_delta)
730 unsigned int tmp, opcode;
733 /* Scale the address delta by the minimum instruction length. */
734 scale_addr_delta (&addr_delta);
736 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
737 We cannot use special opcodes here, since we want the end_sequence
738 to emit the matrix entry. */
739 if (line_delta == INT_MAX)
741 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
744 len = 1 + sizeof_leb128 (addr_delta, 0);
748 /* Bias the line delta by the base. */
749 tmp = line_delta - DWARF2_LINE_BASE;
751 /* If the line increment is out of range of a special opcode, we
752 must encode it with DW_LNS_advance_line. */
753 if (tmp >= DWARF2_LINE_RANGE)
755 len = 1 + sizeof_leb128 (line_delta, 1);
757 tmp = 0 - DWARF2_LINE_BASE;
760 /* Bias the opcode by the special opcode base. */
761 tmp += DWARF2_LINE_OPCODE_BASE;
763 /* Avoid overflow when addr_delta is large. */
764 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
766 /* Try using a special opcode. */
767 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
771 /* Try using DW_LNS_const_add_pc followed by special op. */
772 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
777 /* Otherwise use DW_LNS_advance_pc. */
778 len += 1 + sizeof_leb128 (addr_delta, 0);
780 /* DW_LNS_copy or special opcode. */
787 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
789 unsigned int tmp, opcode;
793 /* Scale the address delta by the minimum instruction length. */
794 scale_addr_delta (&addr_delta);
796 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
797 We cannot use special opcodes here, since we want the end_sequence
798 to emit the matrix entry. */
799 if (line_delta == INT_MAX)
801 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
802 *p++ = DW_LNS_const_add_pc;
805 *p++ = DW_LNS_advance_pc;
806 p += output_leb128 (p, addr_delta, 0);
809 *p++ = DW_LNS_extended_op;
811 *p++ = DW_LNE_end_sequence;
815 /* Bias the line delta by the base. */
816 tmp = line_delta - DWARF2_LINE_BASE;
818 /* If the line increment is out of range of a special opcode, we
819 must encode it with DW_LNS_advance_line. */
820 if (tmp >= DWARF2_LINE_RANGE)
822 *p++ = DW_LNS_advance_line;
823 p += output_leb128 (p, line_delta, 1);
826 tmp = 0 - DWARF2_LINE_BASE;
830 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
832 if (line_delta == 0 && addr_delta == 0)
838 /* Bias the opcode by the special opcode base. */
839 tmp += DWARF2_LINE_OPCODE_BASE;
841 /* Avoid overflow when addr_delta is large. */
842 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
844 /* Try using a special opcode. */
845 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
852 /* Try using DW_LNS_const_add_pc followed by special op. */
853 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
856 *p++ = DW_LNS_const_add_pc;
862 /* Otherwise use DW_LNS_advance_pc. */
863 *p++ = DW_LNS_advance_pc;
864 p += output_leb128 (p, addr_delta, 0);
875 /* Handy routine to combine calls to the above two routines. */
878 out_inc_line_addr (int line_delta, addressT addr_delta)
880 int len = size_inc_line_addr (line_delta, addr_delta);
881 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
884 /* Generate a variant frag that we can use to relax address/line
885 increments between fragments of the target segment. */
888 relax_inc_line_addr (int line_delta, segT seg,
889 fragS *to_frag, addressT to_ofs,
890 fragS *from_frag, addressT from_ofs)
892 symbolS *to_sym, *from_sym;
896 to_sym = symbol_temp_new (seg, to_ofs, to_frag);
897 from_sym = symbol_temp_new (seg, from_ofs, from_frag);
899 expr.X_op = O_subtract;
900 expr.X_add_symbol = to_sym;
901 expr.X_op_symbol = from_sym;
902 expr.X_add_number = 0;
904 /* The maximum size of the frag is the line delta with a maximum
905 sized address delta. */
906 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
908 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
909 make_expr_symbol (&expr), line_delta, NULL);
912 /* The function estimates the size of a rs_dwarf2dbg variant frag
913 based on the current values of the symbols. It is called before
914 the relaxation loop. We set fr_subtype to the expected length. */
917 dwarf2dbg_estimate_size_before_relax (fragS *frag)
922 addr_delta = resolve_symbol_value (frag->fr_symbol);
923 size = size_inc_line_addr (frag->fr_offset, addr_delta);
925 frag->fr_subtype = size;
930 /* This function relaxes a rs_dwarf2dbg variant frag based on the
931 current values of the symbols. fr_subtype is the current length
932 of the frag. This returns the change in frag length. */
935 dwarf2dbg_relax_frag (fragS *frag)
937 int old_size, new_size;
939 old_size = frag->fr_subtype;
940 new_size = dwarf2dbg_estimate_size_before_relax (frag);
942 return new_size - old_size;
945 /* This function converts a rs_dwarf2dbg variant frag into a normal
946 fill frag. This is called after all relaxation has been done.
947 fr_subtype will be the desired length of the frag. */
950 dwarf2dbg_convert_frag (fragS *frag)
954 addr_diff = resolve_symbol_value (frag->fr_symbol);
956 /* fr_var carries the max_chars that we created the fragment with.
957 fr_subtype carries the current expected length. We must, of
958 course, have allocated enough memory earlier. */
959 assert (frag->fr_var >= (int) frag->fr_subtype);
961 emit_inc_line_addr (frag->fr_offset, addr_diff,
962 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
964 frag->fr_fix += frag->fr_subtype;
965 frag->fr_type = rs_fill;
970 /* Generate .debug_line content for the chain of line number entries
971 beginning at E, for segment SEG. */
974 process_entries (segT seg, struct line_entry *e)
976 unsigned filenum = 1;
980 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
983 addressT frag_ofs = 0;
984 addressT last_frag_ofs;
985 struct line_entry *next;
991 if (filenum != e->loc.filenum)
993 filenum = e->loc.filenum;
994 out_opcode (DW_LNS_set_file);
995 out_uleb128 (filenum);
999 if (column != e->loc.column)
1001 column = e->loc.column;
1002 out_opcode (DW_LNS_set_column);
1003 out_uleb128 (column);
1007 if (isa != e->loc.isa)
1010 out_opcode (DW_LNS_set_isa);
1015 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
1017 flags = e->loc.flags;
1018 out_opcode (DW_LNS_negate_stmt);
1022 if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
1024 out_opcode (DW_LNS_set_basic_block);
1028 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
1030 out_opcode (DW_LNS_set_prologue_end);
1034 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1036 out_opcode (DW_LNS_set_epilogue_begin);
1040 /* Don't try to optimize away redundant entries; gdb wants two
1041 entries for a function where the code starts on the same line as
1042 the {, and there's no way to identify that case here. Trust gcc
1043 to optimize appropriately. */
1044 if (1 /* line != e->loc.line || changed */)
1046 int line_delta = e->loc.line - line;
1049 out_set_addr (seg, e->frag, e->frag_ofs);
1050 out_inc_line_addr (line_delta, 0);
1052 else if (frag == e->frag)
1053 out_inc_line_addr (line_delta, e->frag_ofs - frag_ofs);
1055 relax_inc_line_addr (line_delta, seg, e->frag, e->frag_ofs,
1059 frag_ofs = e->frag_ofs;
1062 else if (frag == NULL)
1064 out_set_addr (seg, e->frag, e->frag_ofs);
1066 frag_ofs = e->frag_ofs;
1074 /* Emit a DW_LNE_end_sequence for the end of the section. */
1075 last_frag = last_frag_for_seg (seg);
1076 last_frag_ofs = get_frag_fix (last_frag);
1077 if (frag == last_frag)
1078 out_inc_line_addr (INT_MAX, last_frag_ofs - frag_ofs);
1080 relax_inc_line_addr (INT_MAX, seg, last_frag, last_frag_ofs,
1084 /* Emit the directory and file tables for .debug_line. */
1087 out_file_list (void)
1093 /* Emit directory list. */
1094 for (i = 1; i < dirs_in_use; ++i)
1096 size = strlen (dirs[i]) + 1;
1097 cp = frag_more (size);
1098 memcpy (cp, dirs[i], size);
1103 for (i = 1; i < files_in_use; ++i)
1105 if (files[i].filename == NULL)
1107 as_bad (_("unassigned file number %ld"), (long) i);
1108 /* Prevent a crash later, particularly for file 1. */
1109 files[i].filename = "";
1113 size = strlen (files[i].filename) + 1;
1114 cp = frag_more (size);
1115 memcpy (cp, files[i].filename, size);
1117 out_uleb128 (files[i].dir); /* directory number */
1118 out_uleb128 (0); /* last modification timestamp */
1119 out_uleb128 (0); /* filesize */
1122 /* Terminate filename list. */
1126 /* Emit the collected .debug_line data. */
1129 out_debug_line (segT line_seg)
1132 symbolS *line_start;
1133 symbolS *prologue_end;
1136 enum dwarf2_format d2f;
1139 subseg_set (line_seg, 0);
1141 line_start = symbol_temp_new_now ();
1142 prologue_end = symbol_temp_make ();
1143 line_end = symbol_temp_make ();
1145 /* Total length of the information for this compilation unit. */
1146 expr.X_op = O_subtract;
1147 expr.X_add_symbol = line_end;
1148 expr.X_op_symbol = line_start;
1150 d2f = DWARF2_FORMAT ();
1151 if (d2f == dwarf2_format_32bit)
1153 expr.X_add_number = -4;
1154 emit_expr (&expr, 4);
1157 else if (d2f == dwarf2_format_64bit)
1159 expr.X_add_number = -12;
1161 emit_expr (&expr, 8);
1164 else if (d2f == dwarf2_format_64bit_irix)
1166 expr.X_add_number = -8;
1167 emit_expr (&expr, 8);
1172 as_fatal (_("internal error: unknown dwarf2 format"));
1178 /* Length of the prologue following this length. */
1179 expr.X_op = O_subtract;
1180 expr.X_add_symbol = prologue_end;
1181 expr.X_op_symbol = line_start;
1182 expr.X_add_number = - (4 + 2 + 4);
1183 emit_expr (&expr, sizeof_offset);
1185 /* Parameters of the state machine. */
1186 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1187 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1188 out_byte (DWARF2_LINE_BASE);
1189 out_byte (DWARF2_LINE_RANGE);
1190 out_byte (DWARF2_LINE_OPCODE_BASE);
1192 /* Standard opcode lengths. */
1193 out_byte (0); /* DW_LNS_copy */
1194 out_byte (1); /* DW_LNS_advance_pc */
1195 out_byte (1); /* DW_LNS_advance_line */
1196 out_byte (1); /* DW_LNS_set_file */
1197 out_byte (1); /* DW_LNS_set_column */
1198 out_byte (0); /* DW_LNS_negate_stmt */
1199 out_byte (0); /* DW_LNS_set_basic_block */
1200 out_byte (0); /* DW_LNS_const_add_pc */
1201 out_byte (1); /* DW_LNS_fixed_advance_pc */
1202 out_byte (0); /* DW_LNS_set_prologue_end */
1203 out_byte (0); /* DW_LNS_set_epilogue_begin */
1204 out_byte (1); /* DW_LNS_set_isa */
1208 symbol_set_value_now (prologue_end);
1210 /* For each section, emit a statement program. */
1211 for (s = all_segs; s; s = s->next)
1212 process_entries (s->seg, s->head->head);
1214 symbol_set_value_now (line_end);
1217 /* Emit data for .debug_aranges. */
1220 out_debug_aranges (segT aranges_seg, segT info_seg)
1222 unsigned int addr_size = sizeof_address;
1223 addressT size, skip;
1228 size = 4 + 2 + 4 + 1 + 1;
1230 skip = 2 * addr_size - (size & (2 * addr_size - 1));
1231 if (skip == 2 * addr_size)
1235 for (s = all_segs; s; s = s->next)
1236 size += 2 * addr_size;
1238 size += 2 * addr_size;
1240 subseg_set (aranges_seg, 0);
1242 /* Length of the compilation unit. */
1243 out_four (size - 4);
1248 /* Offset to .debug_info. */
1249 /* ??? sizeof_offset */
1250 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), 4);
1252 /* Size of an address (offset portion). */
1253 out_byte (addr_size);
1255 /* Size of a segment descriptor. */
1258 /* Align the header. */
1260 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1262 for (s = all_segs; s; s = s->next)
1267 frag = first_frag_for_seg (s->seg);
1268 beg = symbol_temp_new (s->seg, 0, frag);
1269 s->text_start = beg;
1271 frag = last_frag_for_seg (s->seg);
1272 end = symbol_temp_new (s->seg, get_frag_fix (frag), frag);
1275 expr.X_op = O_symbol;
1276 expr.X_add_symbol = beg;
1277 expr.X_add_number = 0;
1278 emit_expr (&expr, addr_size);
1280 expr.X_op = O_subtract;
1281 expr.X_add_symbol = end;
1282 expr.X_op_symbol = beg;
1283 expr.X_add_number = 0;
1284 emit_expr (&expr, addr_size);
1287 p = frag_more (2 * addr_size);
1288 md_number_to_chars (p, 0, addr_size);
1289 md_number_to_chars (p + addr_size, 0, addr_size);
1292 /* Emit data for .debug_abbrev. Note that this must be kept in
1293 sync with out_debug_info below. */
1296 out_debug_abbrev (segT abbrev_seg)
1298 subseg_set (abbrev_seg, 0);
1301 out_uleb128 (DW_TAG_compile_unit);
1302 out_byte (DW_CHILDREN_no);
1303 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1304 if (all_segs->next == NULL)
1306 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1307 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1309 out_abbrev (DW_AT_name, DW_FORM_string);
1310 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1311 out_abbrev (DW_AT_producer, DW_FORM_string);
1312 out_abbrev (DW_AT_language, DW_FORM_data2);
1315 /* Terminate the abbreviations for this compilation unit. */
1319 /* Emit a description of this compilation unit for .debug_info. */
1322 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg)
1327 symbolS *info_start;
1331 enum dwarf2_format d2f;
1334 subseg_set (info_seg, 0);
1336 info_start = symbol_temp_new_now ();
1337 info_end = symbol_temp_make ();
1339 /* Compilation Unit length. */
1340 expr.X_op = O_subtract;
1341 expr.X_add_symbol = info_end;
1342 expr.X_op_symbol = info_start;
1344 d2f = DWARF2_FORMAT ();
1345 if (d2f == dwarf2_format_32bit)
1347 expr.X_add_number = -4;
1348 emit_expr (&expr, 4);
1351 else if (d2f == dwarf2_format_64bit)
1353 expr.X_add_number = -12;
1355 emit_expr (&expr, 8);
1358 else if (d2f == dwarf2_format_64bit_irix)
1360 expr.X_add_number = -8;
1361 emit_expr (&expr, 8);
1366 as_fatal (_("internal error: unknown dwarf2 format"));
1369 /* DWARF version. */
1372 /* .debug_abbrev offset */
1373 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1375 /* Target address size. */
1376 out_byte (sizeof_address);
1378 /* DW_TAG_compile_unit DIE abbrev */
1381 /* DW_AT_stmt_list */
1382 /* ??? sizeof_offset */
1383 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 4);
1385 /* These two attributes may only be emitted if all of the code is
1386 contiguous. Multiple sections are not that. */
1387 if (all_segs->next == NULL)
1390 expr.X_op = O_symbol;
1391 expr.X_add_symbol = all_segs->text_start;
1392 expr.X_add_number = 0;
1393 emit_expr (&expr, sizeof_address);
1396 expr.X_op = O_symbol;
1397 expr.X_add_symbol = all_segs->text_end;
1398 expr.X_add_number = 0;
1399 emit_expr (&expr, sizeof_address);
1402 /* DW_AT_name. We don't have the actual file name that was present
1403 on the command line, so assume files[1] is the main input file.
1404 We're not supposed to get called unless at least one line number
1405 entry was emitted, so this should always be defined. */
1406 if (!files || files_in_use < 1)
1410 len = strlen (dirs[files[1].dir]);
1411 p = frag_more (len + 1);
1412 memcpy (p, dirs[files[1].dir], len);
1415 len = strlen (files[1].filename) + 1;
1416 p = frag_more (len);
1417 memcpy (p, files[1].filename, len);
1419 /* DW_AT_comp_dir */
1420 comp_dir = getpwd ();
1421 len = strlen (comp_dir) + 1;
1422 p = frag_more (len);
1423 memcpy (p, comp_dir, len);
1425 /* DW_AT_producer */
1426 sprintf (producer, "GNU AS %s", VERSION);
1427 len = strlen (producer) + 1;
1428 p = frag_more (len);
1429 memcpy (p, producer, len);
1431 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1432 dwarf2 draft has no standard code for assembler. */
1433 out_two (DW_LANG_Mips_Assembler);
1435 symbol_set_value_now (info_end);
1438 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1439 were any .file/.loc directives, or --gdwarf2 was given, or if the
1440 file has a non-empty .debug_info section. If we emit .debug_line,
1441 and the .debug_info section is empty, we also emit .debug_info,
1442 .debug_aranges and .debug_abbrev. ALL_SEGS will be non-null if
1443 there were any .file/.loc directives, or --gdwarf2 was given and
1444 there were any located instructions emitted. */
1447 dwarf2_finish (void)
1452 int emit_other_sections = 0;
1454 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
1455 emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
1457 if (!all_segs && emit_other_sections)
1458 /* There is no line information and no non-empty .debug_info
1462 /* Calculate the size of an address for the target machine. */
1463 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1465 /* Create and switch to the line number section. */
1466 line_seg = subseg_new (".debug_line", 0);
1467 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
1469 /* For each subsection, chain the debug entries together. */
1470 for (s = all_segs; s; s = s->next)
1472 struct line_subseg *ss = s->head;
1473 struct line_entry **ptail = ss->ptail;
1475 while ((ss = ss->next) != NULL)
1482 out_debug_line (line_seg);
1484 /* If this is assembler generated line info, and there is no
1485 debug_info already, we need .debug_info and .debug_abbrev
1486 sections as well. */
1487 if (emit_other_sections)
1494 info_seg = subseg_new (".debug_info", 0);
1495 abbrev_seg = subseg_new (".debug_abbrev", 0);
1496 aranges_seg = subseg_new (".debug_aranges", 0);
1498 bfd_set_section_flags (stdoutput, info_seg,
1499 SEC_READONLY | SEC_DEBUGGING);
1500 bfd_set_section_flags (stdoutput, abbrev_seg,
1501 SEC_READONLY | SEC_DEBUGGING);
1502 bfd_set_section_flags (stdoutput, aranges_seg,
1503 SEC_READONLY | SEC_DEBUGGING);
1505 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1507 out_debug_aranges (aranges_seg, info_seg);
1508 out_debug_abbrev (abbrev_seg);
1509 out_debug_info (info_seg, abbrev_seg, line_seg);