1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* Logical line numbers can be controlled by the compiler via the
23 following two directives:
26 .loc FILENO LINENO [COLUMN]
28 FILENO is the filenumber. */
36 #ifdef HAVE_SYS_PARAM_H
37 #include <sys/param.h>
40 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
44 #include "dwarf2dbg.h"
47 #define DWARF2_FORMAT() dwarf2_format_32bit
54 #include "elf/dwarf2.h"
56 /* Since we can't generate the prolog until the body is complete, we
57 use three different subsegments for .debug_line: one holding the
58 prolog, one for the directory and filename info, and one for the
59 body ("statement program"). */
64 /* First special line opcde - leave room for the standard opcodes.
65 Note: If you want to change this, you'll have to update the
66 "standard_opcode_lengths" table that is emitted below in
68 #define DWARF2_LINE_OPCODE_BASE 10
70 #ifndef DWARF2_LINE_BASE
71 /* Minimum line offset in a special line info. opcode. This value
72 was chosen to give a reasonable range of values. */
73 # define DWARF2_LINE_BASE -5
76 /* Range of line offsets in a special line info. opcode. */
77 #ifndef DWARF2_LINE_RANGE
78 # define DWARF2_LINE_RANGE 14
81 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
82 /* Define the architecture-dependent minimum instruction length (in
83 bytes). This value should be rather too small than too big. */
84 # define DWARF2_LINE_MIN_INSN_LENGTH 1
87 /* Flag that indicates the initial value of the is_stmt_start flag.
88 In the present implementation, we do not mark any lines as
89 the beginning of a source statement, because that information
90 is not made available by the GCC front-end. */
91 #define DWARF2_LINE_DEFAULT_IS_STMT 1
93 /* Given a special op, return the line skip amount. */
94 #define SPECIAL_LINE(op) \
95 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
97 /* Given a special op, return the address skip amount (in units of
98 DWARF2_LINE_MIN_INSN_LENGTH. */
99 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
101 /* The maximum address skip amount that can be encoded with a special op. */
102 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
105 struct line_entry *next;
108 struct dwarf2_line_info loc;
112 struct line_subseg *next;
114 struct line_entry *head;
115 struct line_entry **ptail;
119 struct line_seg *next;
121 struct line_subseg *head;
126 /* Collects data for all line table entries during assembly. */
127 static struct line_seg *all_segs;
134 /* Table of files used by .debug_line. */
135 static struct file_entry *files;
136 static unsigned int files_in_use;
137 static unsigned int files_allocated;
139 /* TRUE when we've seen a .loc directive recently. Used to avoid
140 doing work when there's nothing to do. */
141 static bfd_boolean loc_directive_seen;
143 /* Current location as indicated by the most recent .loc directive. */
144 static struct dwarf2_line_info current;
146 /* Fake label name. */
147 static char const fake_label_name[] = ".L0\001";
149 /* The size of an address on the target. */
150 static unsigned int sizeof_address;
152 static struct line_subseg *get_line_subseg PARAMS ((segT, subsegT));
153 static unsigned int get_filenum PARAMS ((const char *));
154 static struct frag *first_frag_for_seg PARAMS ((segT));
155 static struct frag *last_frag_for_seg PARAMS ((segT));
156 static void out_byte PARAMS ((int));
157 static void out_opcode PARAMS ((int));
158 static void out_two PARAMS ((int));
159 static void out_four PARAMS ((int));
160 static void out_abbrev PARAMS ((int, int));
161 static void out_uleb128 PARAMS ((addressT));
162 static symbolS *symbol_new_now PARAMS ((void));
163 static void set_symbol_value_now PARAMS ((symbolS *));
164 static offsetT get_frag_fix PARAMS ((fragS *));
165 static void out_set_addr PARAMS ((segT, fragS *, addressT));
166 static int size_inc_line_addr PARAMS ((int, addressT));
167 static void emit_inc_line_addr PARAMS ((int, addressT, char *, int));
168 static void out_inc_line_addr PARAMS ((int, addressT));
169 static void relax_inc_line_addr PARAMS ((int, segT, fragS *, addressT,
171 static void process_entries PARAMS ((segT, struct line_entry *));
172 static void out_file_list PARAMS ((void));
173 static void out_debug_line PARAMS ((segT));
174 static void out_debug_aranges PARAMS ((segT, segT));
175 static void out_debug_abbrev PARAMS ((segT));
176 static void out_debug_info PARAMS ((segT, segT, segT));
177 static void scale_addr_delta PARAMS ((int *));
179 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
181 static struct line_subseg *
182 get_line_subseg (seg, subseg)
186 static segT last_seg;
187 static subsegT last_subseg;
188 static struct line_subseg *last_line_subseg;
191 struct line_subseg **pss, *ss;
193 if (seg == last_seg && subseg == last_subseg)
194 return last_line_subseg;
196 for (s = all_segs; s; s = s->next)
200 s = (struct line_seg *) xmalloc (sizeof (*s));
207 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
209 if (ss->subseg == subseg)
211 if (ss->subseg > subseg)
215 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
219 ss->ptail = &ss->head;
224 last_subseg = subseg;
225 last_line_subseg = ss;
230 /* Record an entry for LOC ocurring at OFS within the current fragment. */
233 dwarf2_gen_line_info (ofs, loc)
235 struct dwarf2_line_info *loc;
237 struct line_subseg *ss;
238 struct line_entry *e;
239 static unsigned int line = -1;
240 static unsigned int filenum = -1;
242 /* Early out for as-yet incomplete location information. */
243 if (loc->filenum == 0 || loc->line == 0)
246 /* Don't emit sequences of line symbols for the same line when the
247 symbols apply to assembler code. It is necessary to emit
248 duplicate line symbols when a compiler asks for them, because GDB
249 uses them to determine the end of the prologue. */
250 if (debug_type == DEBUG_DWARF2
251 && line == loc->line && filenum == loc->filenum)
255 filenum = loc->filenum;
257 e = (struct line_entry *) xmalloc (sizeof (*e));
263 ss = get_line_subseg (now_seg, now_subseg);
265 ss->ptail = &e->next;
270 struct dwarf2_line_info *line;
272 if (debug_type == DEBUG_DWARF2)
275 as_where (&filename, &line->line);
276 line->filenum = get_filenum (filename);
278 line->flags = DWARF2_FLAG_BEGIN_STMT;
284 /* Called for each machine instruction, or relatively atomic group of
285 machine instructions (ie built-in macro). The instruction or group
286 is SIZE bytes in length. If dwarf2 line number generation is called
287 for, emit a line statement appropriately. */
290 dwarf2_emit_insn (size)
293 struct dwarf2_line_info loc;
295 if (loc_directive_seen)
297 /* Use the last location established by a .loc directive, not
298 the value returned by dwarf2_where(). That calls as_where()
299 which will return either the logical input file name (foo.c)
300 or the physical input file name (foo.s) and not the file name
301 specified in the most recent .loc directive (eg foo.h). */
304 /* Unless we generate DWARF2 debugging information for each
305 assembler line, we only emit one line symbol for one LOC. */
306 if (debug_type != DEBUG_DWARF2)
307 loc_directive_seen = FALSE;
309 else if (debug_type != DEBUG_DWARF2)
312 dwarf2_where (& loc);
314 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
317 /* Get a .debug_line file number for FILENAME. */
320 get_filenum (filename)
321 const char *filename;
323 static unsigned int last_used;
327 if (strcmp (filename, files[last_used].filename) == 0)
330 for (i = 1; i < files_in_use; ++i)
331 if (strcmp (filename, files[i].filename) == 0)
334 if (i >= files_allocated)
336 unsigned int old = files_allocated;
338 files_allocated = i + 32;
339 files = (struct file_entry *)
340 xrealloc (files, (i + 32) * sizeof (struct file_entry));
342 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
345 files[i].filename = xstrdup (filename);
347 files_in_use = i + 1;
353 /* Handle two forms of .file directive:
354 - Pass .file "source.c" to s_app_file
355 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
357 If an entry is added to the file table, return a pointer to the filename. */
360 dwarf2_directive_file (dummy)
361 int dummy ATTRIBUTE_UNUSED;
367 /* Continue to accept a bare string and pass it off. */
369 if (*input_line_pointer == '"')
375 num = get_absolute_expression ();
376 filename = demand_copy_C_string (&filename_len);
377 demand_empty_rest_of_line ();
381 as_bad (_("file number less than one"));
385 if (num < (int) files_in_use && files[num].filename != 0)
387 as_bad (_("file number %ld already allocated"), (long) num);
391 if (num >= (int) files_allocated)
393 unsigned int old = files_allocated;
395 files_allocated = num + 16;
396 files = (struct file_entry *)
397 xrealloc (files, (num + 16) * sizeof (struct file_entry));
399 /* Zero the new memory. */
400 memset (files + old, 0, (num + 16 - old) * sizeof (struct file_entry));
403 files[num].filename = filename;
405 files_in_use = num + 1;
411 dwarf2_directive_loc (dummy)
412 int dummy ATTRIBUTE_UNUSED;
414 offsetT filenum, line, column;
416 filenum = get_absolute_expression ();
418 line = get_absolute_expression ();
420 column = get_absolute_expression ();
421 demand_empty_rest_of_line ();
425 as_bad (_("file number less than one"));
428 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
430 as_bad (_("unassigned file number %ld"), (long) filenum);
434 current.filenum = filenum;
436 current.column = column;
437 current.flags = DWARF2_FLAG_BEGIN_STMT;
439 loc_directive_seen = TRUE;
444 listing_source_file (files[filenum].filename);
445 listing_source_line (line);
451 first_frag_for_seg (seg)
454 frchainS *f, *first = NULL;
456 for (f = frchain_root; f; f = f->frch_next)
457 if (f->frch_seg == seg
458 && (! first || first->frch_subseg > f->frch_subseg))
461 return first ? first->frch_root : NULL;
465 last_frag_for_seg (seg)
468 frchainS *f, *last = NULL;
470 for (f = frchain_root; f; f = f->frch_next)
471 if (f->frch_seg == seg
472 && (! last || last->frch_subseg < f->frch_subseg))
475 return last ? last->frch_last : NULL;
478 /* Emit a single byte into the current segment. */
484 FRAG_APPEND_1_CHAR (byte);
487 /* Emit a statement program opcode into the current segment. */
496 /* Emit a two-byte word into the current segment. */
502 md_number_to_chars (frag_more (2), data, 2);
505 /* Emit a four byte word into the current segment. */
511 md_number_to_chars (frag_more (4), data, 4);
514 /* Emit an unsigned "little-endian base 128" number. */
520 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
523 /* Emit a tuple for .debug_abbrev. */
526 out_abbrev (name, form)
533 /* Create a new fake symbol whose value is the current position. */
538 return symbol_new (fake_label_name, now_seg, frag_now_fix (), frag_now);
541 /* Set the value of SYM to the current position in the current segment. */
544 set_symbol_value_now (sym)
547 S_SET_SEGMENT (sym, now_seg);
548 S_SET_VALUE (sym, frag_now_fix ());
549 symbol_set_frag (sym, frag_now);
552 /* Get the size of a fragment. */
563 /* If a fragment is the last in the chain, special measures must be
564 taken to find its size before relaxation, since it may be pending
565 on some subsegment chain. */
566 for (fr = frchain_root; fr; fr = fr->frch_next)
567 if (fr->frch_last == frag)
569 long align_mask = -1 << get_recorded_alignment (fr->frch_seg);
570 return (((char *) obstack_next_free (&fr->frch_obstack)
571 - frag->fr_literal) + ~align_mask) & align_mask;
577 /* Set an absolute address (may result in a relocation entry). */
580 out_set_addr (seg, frag, ofs)
588 sym = symbol_new (fake_label_name, seg, ofs, frag);
590 out_opcode (DW_LNS_extended_op);
591 out_uleb128 (sizeof_address + 1);
593 out_opcode (DW_LNE_set_address);
594 expr.X_op = O_symbol;
595 expr.X_add_symbol = sym;
596 expr.X_add_number = 0;
597 emit_expr (&expr, sizeof_address);
600 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
602 scale_addr_delta (addr_delta)
605 static int printed_this = 0;
606 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
609 as_bad("unaligned opcodes detected in executable segment");
612 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
615 #define scale_addr_delta(A)
618 /* Encode a pair of line and address skips as efficiently as possible.
619 Note that the line skip is signed, whereas the address skip is unsigned.
621 The following two routines *must* be kept in sync. This is
622 enforced by making emit_inc_line_addr abort if we do not emit
623 exactly the expected number of bytes. */
626 size_inc_line_addr (line_delta, addr_delta)
630 unsigned int tmp, opcode;
633 /* Scale the address delta by the minimum instruction length. */
634 scale_addr_delta (&addr_delta);
636 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
637 We cannot use special opcodes here, since we want the end_sequence
638 to emit the matrix entry. */
639 if (line_delta == INT_MAX)
641 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
644 len = 1 + sizeof_leb128 (addr_delta, 0);
648 /* Bias the line delta by the base. */
649 tmp = line_delta - DWARF2_LINE_BASE;
651 /* If the line increment is out of range of a special opcode, we
652 must encode it with DW_LNS_advance_line. */
653 if (tmp >= DWARF2_LINE_RANGE)
655 len = 1 + sizeof_leb128 (line_delta, 1);
657 tmp = 0 - DWARF2_LINE_BASE;
660 /* Bias the opcode by the special opcode base. */
661 tmp += DWARF2_LINE_OPCODE_BASE;
663 /* Avoid overflow when addr_delta is large. */
664 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
666 /* Try using a special opcode. */
667 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
671 /* Try using DW_LNS_const_add_pc followed by special op. */
672 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
677 /* Otherwise use DW_LNS_advance_pc. */
678 len += 1 + sizeof_leb128 (addr_delta, 0);
680 /* DW_LNS_copy or special opcode. */
687 emit_inc_line_addr (line_delta, addr_delta, p, len)
693 unsigned int tmp, opcode;
697 /* Scale the address delta by the minimum instruction length. */
698 scale_addr_delta (&addr_delta);
700 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
701 We cannot use special opcodes here, since we want the end_sequence
702 to emit the matrix entry. */
703 if (line_delta == INT_MAX)
705 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
706 *p++ = DW_LNS_const_add_pc;
709 *p++ = DW_LNS_advance_pc;
710 p += output_leb128 (p, addr_delta, 0);
713 *p++ = DW_LNS_extended_op;
715 *p++ = DW_LNE_end_sequence;
719 /* Bias the line delta by the base. */
720 tmp = line_delta - DWARF2_LINE_BASE;
722 /* If the line increment is out of range of a special opcode, we
723 must encode it with DW_LNS_advance_line. */
724 if (tmp >= DWARF2_LINE_RANGE)
726 *p++ = DW_LNS_advance_line;
727 p += output_leb128 (p, line_delta, 1);
729 /* Prettier, I think, to use DW_LNS_copy instead of a
730 "line +0, addr +0" special opcode. */
738 tmp = 0 - DWARF2_LINE_BASE;
742 /* Bias the opcode by the special opcode base. */
743 tmp += DWARF2_LINE_OPCODE_BASE;
745 /* Avoid overflow when addr_delta is large. */
746 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
748 /* Try using a special opcode. */
749 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
756 /* Try using DW_LNS_const_add_pc followed by special op. */
757 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
760 *p++ = DW_LNS_const_add_pc;
766 /* Otherwise use DW_LNS_advance_pc. */
767 *p++ = DW_LNS_advance_pc;
768 p += output_leb128 (p, addr_delta, 0);
779 /* Handy routine to combine calls to the above two routines. */
782 out_inc_line_addr (line_delta, addr_delta)
786 int len = size_inc_line_addr (line_delta, addr_delta);
787 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
790 /* Generate a variant frag that we can use to relax address/line
791 increments between fragments of the target segment. */
794 relax_inc_line_addr (line_delta, seg, to_frag, to_ofs, from_frag, from_ofs)
797 fragS *to_frag, *from_frag;
798 addressT to_ofs, from_ofs;
800 symbolS *to_sym, *from_sym;
804 to_sym = symbol_new (fake_label_name, seg, to_ofs, to_frag);
805 from_sym = symbol_new (fake_label_name, seg, from_ofs, from_frag);
807 expr.X_op = O_subtract;
808 expr.X_add_symbol = to_sym;
809 expr.X_op_symbol = from_sym;
810 expr.X_add_number = 0;
812 /* The maximum size of the frag is the line delta with a maximum
813 sized address delta. */
814 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
816 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
817 make_expr_symbol (&expr), line_delta, NULL);
820 /* The function estimates the size of a rs_dwarf2dbg variant frag
821 based on the current values of the symbols. It is called before
822 the relaxation loop. We set fr_subtype to the expected length. */
825 dwarf2dbg_estimate_size_before_relax (frag)
831 addr_delta = resolve_symbol_value (frag->fr_symbol);
832 size = size_inc_line_addr (frag->fr_offset, addr_delta);
834 frag->fr_subtype = size;
839 /* This function relaxes a rs_dwarf2dbg variant frag based on the
840 current values of the symbols. fr_subtype is the current length
841 of the frag. This returns the change in frag length. */
844 dwarf2dbg_relax_frag (frag)
847 int old_size, new_size;
849 old_size = frag->fr_subtype;
850 new_size = dwarf2dbg_estimate_size_before_relax (frag);
852 return new_size - old_size;
855 /* This function converts a rs_dwarf2dbg variant frag into a normal
856 fill frag. This is called after all relaxation has been done.
857 fr_subtype will be the desired length of the frag. */
860 dwarf2dbg_convert_frag (frag)
865 addr_diff = resolve_symbol_value (frag->fr_symbol);
867 /* fr_var carries the max_chars that we created the fragment with.
868 fr_subtype carries the current expected length. We must, of
869 course, have allocated enough memory earlier. */
870 assert (frag->fr_var >= (int) frag->fr_subtype);
872 emit_inc_line_addr (frag->fr_offset, addr_diff,
873 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
875 frag->fr_fix += frag->fr_subtype;
876 frag->fr_type = rs_fill;
881 /* Generate .debug_line content for the chain of line number entries
882 beginning at E, for segment SEG. */
885 process_entries (seg, e)
887 struct line_entry *e;
889 unsigned filenum = 1;
892 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_BEGIN_STMT : 0;
895 addressT frag_ofs = 0;
896 addressT last_frag_ofs;
897 struct line_entry *next;
903 if (filenum != e->loc.filenum)
905 filenum = e->loc.filenum;
906 out_opcode (DW_LNS_set_file);
907 out_uleb128 (filenum);
911 if (column != e->loc.column)
913 column = e->loc.column;
914 out_opcode (DW_LNS_set_column);
915 out_uleb128 (column);
919 if ((e->loc.flags ^ flags) & DWARF2_FLAG_BEGIN_STMT)
921 flags = e->loc.flags;
922 out_opcode (DW_LNS_negate_stmt);
926 if (e->loc.flags & DWARF2_FLAG_BEGIN_BLOCK)
928 out_opcode (DW_LNS_set_basic_block);
932 /* Don't try to optimize away redundant entries; gdb wants two
933 entries for a function where the code starts on the same line as
934 the {, and there's no way to identify that case here. Trust gcc
935 to optimize appropriately. */
936 if (1 /* line != e->loc.line || changed */)
938 int line_delta = e->loc.line - line;
941 out_set_addr (seg, e->frag, e->frag_ofs);
942 out_inc_line_addr (line_delta, 0);
944 else if (frag == e->frag)
945 out_inc_line_addr (line_delta, e->frag_ofs - frag_ofs);
947 relax_inc_line_addr (line_delta, seg, e->frag, e->frag_ofs,
951 frag_ofs = e->frag_ofs;
954 else if (frag == NULL)
956 out_set_addr (seg, e->frag, e->frag_ofs);
958 frag_ofs = e->frag_ofs;
966 /* Emit a DW_LNE_end_sequence for the end of the section. */
967 last_frag = last_frag_for_seg (seg);
968 last_frag_ofs = get_frag_fix (last_frag);
969 if (frag == last_frag)
970 out_inc_line_addr (INT_MAX, last_frag_ofs - frag_ofs);
972 relax_inc_line_addr (INT_MAX, seg, last_frag, last_frag_ofs,
976 /* Emit the directory and file tables for .debug_line. */
985 /* Terminate directory list. */
988 for (i = 1; i < files_in_use; ++i)
990 if (files[i].filename == NULL)
992 as_bad (_("unassigned file number %ld"), (long) i);
996 size = strlen (files[i].filename) + 1;
997 cp = frag_more (size);
998 memcpy (cp, files[i].filename, size);
1000 out_uleb128 (files[i].dir); /* directory number */
1001 out_uleb128 (0); /* last modification timestamp */
1002 out_uleb128 (0); /* filesize */
1005 /* Terminate filename list. */
1009 /* Emit the collected .debug_line data. */
1012 out_debug_line (line_seg)
1016 symbolS *line_start;
1017 symbolS *prologue_end;
1020 enum dwarf2_format d2f;
1023 subseg_set (line_seg, 0);
1025 line_start = symbol_new_now ();
1026 prologue_end = symbol_make (fake_label_name);
1027 line_end = symbol_make (fake_label_name);
1029 /* Total length of the information for this compilation unit. */
1030 expr.X_op = O_subtract;
1031 expr.X_add_symbol = line_end;
1032 expr.X_op_symbol = line_start;
1034 d2f = DWARF2_FORMAT ();
1035 if (d2f == dwarf2_format_32bit)
1037 expr.X_add_number = -4;
1038 emit_expr (&expr, 4);
1041 else if (d2f == dwarf2_format_64bit)
1043 expr.X_add_number = -12;
1045 emit_expr (&expr, 8);
1048 else if (d2f == dwarf2_format_64bit_irix)
1050 expr.X_add_number = -8;
1051 emit_expr (&expr, 8);
1056 as_fatal (_("internal error: unknown dwarf2 format"));
1062 /* Length of the prologue following this length. */
1063 expr.X_op = O_subtract;
1064 expr.X_add_symbol = prologue_end;
1065 expr.X_op_symbol = line_start;
1066 expr.X_add_number = - (4 + 2 + 4);
1067 emit_expr (&expr, sizeof_offset);
1069 /* Parameters of the state machine. */
1070 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1071 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1072 out_byte (DWARF2_LINE_BASE);
1073 out_byte (DWARF2_LINE_RANGE);
1074 out_byte (DWARF2_LINE_OPCODE_BASE);
1076 /* Standard opcode lengths. */
1077 out_byte (0); /* DW_LNS_copy */
1078 out_byte (1); /* DW_LNS_advance_pc */
1079 out_byte (1); /* DW_LNS_advance_line */
1080 out_byte (1); /* DW_LNS_set_file */
1081 out_byte (1); /* DW_LNS_set_column */
1082 out_byte (0); /* DW_LNS_negate_stmt */
1083 out_byte (0); /* DW_LNS_set_basic_block */
1084 out_byte (0); /* DW_LNS_const_add_pc */
1085 out_byte (1); /* DW_LNS_fixed_advance_pc */
1089 set_symbol_value_now (prologue_end);
1091 /* For each section, emit a statement program. */
1092 for (s = all_segs; s; s = s->next)
1093 process_entries (s->seg, s->head->head);
1095 set_symbol_value_now (line_end);
1098 /* Emit data for .debug_aranges. */
1101 out_debug_aranges (aranges_seg, info_seg)
1105 unsigned int addr_size = sizeof_address;
1106 addressT size, skip;
1111 size = 4 + 2 + 4 + 1 + 1;
1113 skip = 2 * addr_size - (size & (2 * addr_size - 1));
1114 if (skip == 2 * addr_size)
1118 for (s = all_segs; s; s = s->next)
1119 size += 2 * addr_size;
1121 size += 2 * addr_size;
1123 subseg_set (aranges_seg, 0);
1125 /* Length of the compilation unit. */
1126 out_four (size - 4);
1131 /* Offset to .debug_info. */
1132 expr.X_op = O_symbol;
1133 expr.X_add_symbol = section_symbol (info_seg);
1134 expr.X_add_number = 0;
1135 emit_expr (&expr, 4);
1137 /* Size of an address (offset portion). */
1138 out_byte (addr_size);
1140 /* Size of a segment descriptor. */
1143 /* Align the header. */
1145 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1147 for (s = all_segs; s; s = s->next)
1152 frag = first_frag_for_seg (s->seg);
1153 beg = symbol_new (fake_label_name, s->seg, 0, frag);
1154 s->text_start = beg;
1156 frag = last_frag_for_seg (s->seg);
1157 end = symbol_new (fake_label_name, s->seg, get_frag_fix (frag), frag);
1160 expr.X_op = O_symbol;
1161 expr.X_add_symbol = beg;
1162 expr.X_add_number = 0;
1163 emit_expr (&expr, addr_size);
1165 expr.X_op = O_subtract;
1166 expr.X_add_symbol = end;
1167 expr.X_op_symbol = beg;
1168 expr.X_add_number = 0;
1169 emit_expr (&expr, addr_size);
1172 p = frag_more (2 * addr_size);
1173 md_number_to_chars (p, 0, addr_size);
1174 md_number_to_chars (p + addr_size, 0, addr_size);
1177 /* Emit data for .debug_abbrev. Note that this must be kept in
1178 sync with out_debug_info below. */
1181 out_debug_abbrev (abbrev_seg)
1184 subseg_set (abbrev_seg, 0);
1187 out_uleb128 (DW_TAG_compile_unit);
1188 out_byte (DW_CHILDREN_no);
1189 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1190 if (all_segs->next == NULL)
1192 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1193 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1195 out_abbrev (DW_AT_name, DW_FORM_string);
1196 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1197 out_abbrev (DW_AT_producer, DW_FORM_string);
1198 out_abbrev (DW_AT_language, DW_FORM_data2);
1201 /* Terminate the abbreviations for this compilation unit. */
1205 /* Emit a description of this compilation unit for .debug_info. */
1208 out_debug_info (info_seg, abbrev_seg, line_seg)
1216 symbolS *info_start;
1220 enum dwarf2_format d2f;
1223 subseg_set (info_seg, 0);
1225 info_start = symbol_new_now ();
1226 info_end = symbol_make (fake_label_name);
1228 /* Compilation Unit length. */
1229 expr.X_op = O_subtract;
1230 expr.X_add_symbol = info_end;
1231 expr.X_op_symbol = info_start;
1233 d2f = DWARF2_FORMAT ();
1234 if (d2f == dwarf2_format_32bit)
1236 expr.X_add_number = -4;
1237 emit_expr (&expr, 4);
1240 else if (d2f == dwarf2_format_64bit)
1242 expr.X_add_number = -12;
1244 emit_expr (&expr, 8);
1247 else if (d2f == dwarf2_format_64bit_irix)
1249 expr.X_add_number = -8;
1250 emit_expr (&expr, 8);
1255 as_fatal (_("internal error: unknown dwarf2 format"));
1258 /* DWARF version. */
1261 /* .debug_abbrev offset */
1262 expr.X_op = O_symbol;
1263 expr.X_add_symbol = section_symbol (abbrev_seg);
1264 expr.X_add_number = 0;
1265 emit_expr (&expr, sizeof_offset);
1267 /* Target address size. */
1268 out_byte (sizeof_address);
1270 /* DW_TAG_compile_unit DIE abbrev */
1273 /* DW_AT_stmt_list */
1274 expr.X_op = O_symbol;
1275 expr.X_add_symbol = section_symbol (line_seg);
1276 expr.X_add_number = 0;
1277 emit_expr (&expr, 4);
1279 /* These two attributes may only be emitted if all of the code is
1280 contiguous. Multiple sections are not that. */
1281 if (all_segs->next == NULL)
1284 expr.X_op = O_symbol;
1285 expr.X_add_symbol = all_segs->text_start;
1286 expr.X_add_number = 0;
1287 emit_expr (&expr, sizeof_address);
1290 expr.X_op = O_symbol;
1291 expr.X_add_symbol = all_segs->text_end;
1292 expr.X_add_number = 0;
1293 emit_expr (&expr, sizeof_address);
1296 /* DW_AT_name. We don't have the actual file name that was present
1297 on the command line, so assume files[1] is the main input file.
1298 We're not supposed to get called unless at least one line number
1299 entry was emitted, so this should always be defined. */
1300 if (!files || files_in_use < 1)
1302 len = strlen (files[1].filename) + 1;
1303 p = frag_more (len);
1304 memcpy (p, files[1].filename, len);
1306 /* DW_AT_comp_dir */
1307 comp_dir = getpwd ();
1308 len = strlen (comp_dir) + 1;
1309 p = frag_more (len);
1310 memcpy (p, comp_dir, len);
1312 /* DW_AT_producer */
1313 sprintf (producer, "GNU AS %s", VERSION);
1314 len = strlen (producer) + 1;
1315 p = frag_more (len);
1316 memcpy (p, producer, len);
1318 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1319 dwarf2 draft has no standard code for assembler. */
1320 out_two (DW_LANG_Mips_Assembler);
1322 set_symbol_value_now (info_end);
1331 /* We don't need to do anything unless:
1332 - Some debug information was recorded via .file/.loc
1333 - or, we are generating DWARF2 information ourself (--gdwarf2)
1334 - or, there is a user-provided .debug_info section which could
1335 reference the file table in the .debug_line section we generate
1337 if (all_segs == NULL
1338 && debug_type != DEBUG_DWARF2
1339 && bfd_get_section_by_name (stdoutput, ".debug_info") == NULL)
1342 /* Calculate the size of an address for the target machine. */
1343 sizeof_address = bfd_arch_bits_per_address (stdoutput) / 8;
1345 /* Create and switch to the line number section. */
1346 line_seg = subseg_new (".debug_line", 0);
1347 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY);
1349 /* For each subsection, chain the debug entries together. */
1350 for (s = all_segs; s; s = s->next)
1352 struct line_subseg *ss = s->head;
1353 struct line_entry **ptail = ss->ptail;
1355 while ((ss = ss->next) != NULL)
1362 out_debug_line (line_seg);
1364 /* If this is assembler generated line info, we need .debug_info
1365 and .debug_abbrev sections as well. */
1366 if (all_segs != NULL && debug_type == DEBUG_DWARF2)
1372 info_seg = subseg_new (".debug_info", 0);
1373 abbrev_seg = subseg_new (".debug_abbrev", 0);
1374 aranges_seg = subseg_new (".debug_aranges", 0);
1376 bfd_set_section_flags (stdoutput, info_seg, SEC_READONLY);
1377 bfd_set_section_flags (stdoutput, abbrev_seg, SEC_READONLY);
1378 bfd_set_section_flags (stdoutput, aranges_seg, SEC_READONLY);
1380 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1382 out_debug_aranges (aranges_seg, info_seg);
1383 out_debug_abbrev (abbrev_seg);
1384 out_debug_info (info_seg, abbrev_seg, line_seg);
1395 dwarf2dbg_estimate_size_before_relax (frag)
1396 fragS *frag ATTRIBUTE_UNUSED;
1398 as_fatal (_("dwarf2 is not supported for this object file format"));
1403 dwarf2dbg_relax_frag (frag)
1404 fragS *frag ATTRIBUTE_UNUSED;
1406 as_fatal (_("dwarf2 is not supported for this object file format"));
1411 dwarf2dbg_convert_frag (frag)
1412 fragS *frag ATTRIBUTE_UNUSED;
1414 as_fatal (_("dwarf2 is not supported for this object file format"));
1418 dwarf2_emit_insn (size)
1419 int size ATTRIBUTE_UNUSED;
1424 dwarf2_directive_file (dummy)
1425 int dummy ATTRIBUTE_UNUSED;
1432 dwarf2_directive_loc (dummy)
1433 int dummy ATTRIBUTE_UNUSED;
1435 as_fatal (_("dwarf2 is not supported for this object file format"));
1437 #endif /* BFD_ASSEMBLER */