daily update
[binutils.git] / gas / dwarf2dbg.c
blob0d5c072682c08c07dc3b142d12addfde5c075a15
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)
10 any later version.
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
20 02111-1307, USA. */
22 /* Logical line numbers can be controlled by the compiler via the
23 following two directives:
25 .file FILENO "file.c"
26 .loc FILENO LINENO [COLUMN]
28 FILENO is the filenumber. */
30 #include "ansidecl.h"
31 #include "as.h"
33 #ifdef HAVE_LIMITS_H
34 #include <limits.h>
35 #else
36 #ifdef HAVE_SYS_PARAM_H
37 #include <sys/param.h>
38 #endif
39 #ifndef INT_MAX
40 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
41 #endif
42 #endif
44 #include "dwarf2dbg.h"
46 #ifndef DWARF2_FORMAT
47 #define DWARF2_FORMAT() dwarf2_format_32bit
48 #endif
50 #ifdef BFD_ASSEMBLER
52 #include "subsegs.h"
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"). */
60 #define DL_PROLOG 0
61 #define DL_FILES 1
62 #define DL_BODY 2
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
67 dwarf2_finish(). */
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
74 #endif
76 /* Range of line offsets in a special line info. opcode. */
77 #ifndef DWARF2_LINE_RANGE
78 # define DWARF2_LINE_RANGE 14
79 #endif
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
85 #endif
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)
104 struct line_entry {
105 struct line_entry *next;
106 fragS *frag;
107 addressT frag_ofs;
108 struct dwarf2_line_info loc;
111 struct line_subseg {
112 struct line_subseg *next;
113 subsegT subseg;
114 struct line_entry *head;
115 struct line_entry **ptail;
118 struct line_seg {
119 struct line_seg *next;
120 segT seg;
121 struct line_subseg *head;
122 symbolS *text_start;
123 symbolS *text_end;
126 /* Collects data for all line table entries during assembly. */
127 static struct line_seg *all_segs;
129 struct file_entry {
130 char *filename;
131 unsigned int dir;
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,
170 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));
178 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
180 static struct line_subseg *
181 get_line_subseg (seg, subseg)
182 segT seg;
183 subsegT subseg;
185 static segT last_seg;
186 static subsegT last_subseg;
187 static struct line_subseg *last_line_subseg;
189 struct line_seg *s;
190 struct line_subseg **pss, *ss;
192 if (seg == last_seg && subseg == last_subseg)
193 return last_line_subseg;
195 for (s = all_segs; s; s = s->next)
196 if (s->seg == seg)
197 goto found_seg;
199 s = (struct line_seg *) xmalloc (sizeof (*s));
200 s->next = all_segs;
201 s->seg = seg;
202 s->head = NULL;
203 all_segs = s;
205 found_seg:
206 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
208 if (ss->subseg == subseg)
209 goto found_subseg;
210 if (ss->subseg > subseg)
211 break;
214 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
215 ss->next = *pss;
216 ss->subseg = subseg;
217 ss->head = NULL;
218 ss->ptail = &ss->head;
219 *pss = ss;
221 found_subseg:
222 last_seg = seg;
223 last_subseg = subseg;
224 last_line_subseg = ss;
226 return ss;
229 /* Record an entry for LOC ocurring at OFS within the current fragment. */
231 void
232 dwarf2_gen_line_info (ofs, loc)
233 addressT ofs;
234 struct dwarf2_line_info *loc;
236 struct line_subseg *ss;
237 struct line_entry *e;
238 static unsigned int line = -1;
239 static unsigned int filenum = -1;
241 /* Early out for as-yet incomplete location information. */
242 if (loc->filenum == 0 || loc->line == 0)
243 return;
245 /* Don't emit sequences of line symbols for the same line when the
246 symbols apply to assembler code. It is necessary to emit
247 duplicate line symbols when a compiler asks for them, because GDB
248 uses them to determine the end of the prologue. */
249 if (debug_type == DEBUG_DWARF2
250 && line == loc->line && filenum == loc->filenum)
251 return;
253 line = loc->line;
254 filenum = loc->filenum;
256 e = (struct line_entry *) xmalloc (sizeof (*e));
257 e->next = NULL;
258 e->frag = frag_now;
259 e->frag_ofs = ofs;
260 e->loc = *loc;
262 ss = get_line_subseg (now_seg, now_subseg);
263 *ss->ptail = e;
264 ss->ptail = &e->next;
267 void
268 dwarf2_where (line)
269 struct dwarf2_line_info *line;
271 if (debug_type == DEBUG_DWARF2)
273 char *filename;
274 as_where (&filename, &line->line);
275 line->filenum = get_filenum (filename);
276 line->column = 0;
277 line->flags = DWARF2_FLAG_BEGIN_STMT;
279 else
280 *line = current;
283 /* Called for each machine instruction, or relatively atomic group of
284 machine instructions (ie built-in macro). The instruction or group
285 is SIZE bytes in length. If dwarf2 line number generation is called
286 for, emit a line statement appropriately. */
288 void
289 dwarf2_emit_insn (size)
290 int size;
292 struct dwarf2_line_info loc;
294 if (loc_directive_seen)
296 /* Use the last location established by a .loc directive, not
297 the value returned by dwarf2_where(). That calls as_where()
298 which will return either the logical input file name (foo.c)
299 or the physical input file name (foo.s) and not the file name
300 specified in the most recent .loc directive (eg foo.h). */
301 loc = current;
303 /* Unless we generate DWARF2 debugging information for each
304 assembler line, we only emit one line symbol for one LOC. */
305 if (debug_type != DEBUG_DWARF2)
306 loc_directive_seen = FALSE;
308 else if (debug_type != DEBUG_DWARF2)
309 return;
310 else
311 dwarf2_where (& loc);
313 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
316 /* Get a .debug_line file number for FILENAME. */
318 static unsigned int
319 get_filenum (filename)
320 const char *filename;
322 static unsigned int last_used;
323 unsigned int i;
325 if (last_used)
326 if (strcmp (filename, files[last_used].filename) == 0)
327 return last_used;
329 for (i = 1; i < files_in_use; ++i)
330 if (strcmp (filename, files[i].filename) == 0)
331 return i;
333 if (i >= files_allocated)
335 unsigned int old = files_allocated;
337 files_allocated = i + 32;
338 files = (struct file_entry *)
339 xrealloc (files, (i + 32) * sizeof (struct file_entry));
341 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
344 files[i].filename = xstrdup (filename);
345 files[i].dir = 0;
346 files_in_use = i + 1;
347 last_used = i;
349 return i;
352 /* Handle two forms of .file directive:
353 - Pass .file "source.c" to s_app_file
354 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
356 If an entry is added to the file table, return a pointer to the filename. */
358 char *
359 dwarf2_directive_file (dummy)
360 int dummy ATTRIBUTE_UNUSED;
362 offsetT num;
363 char *filename;
364 int filename_len;
366 /* Continue to accept a bare string and pass it off. */
367 SKIP_WHITESPACE ();
368 if (*input_line_pointer == '"')
370 s_app_file (0);
371 return NULL;
374 num = get_absolute_expression ();
375 filename = demand_copy_C_string (&filename_len);
376 demand_empty_rest_of_line ();
378 if (num < 1)
380 as_bad (_("file number less than one"));
381 return NULL;
384 if (num < (int) files_in_use && files[num].filename != 0)
386 as_bad (_("file number %ld already allocated"), (long) num);
387 return NULL;
390 if (num >= (int) files_allocated)
392 unsigned int old = files_allocated;
394 files_allocated = num + 16;
395 files = (struct file_entry *)
396 xrealloc (files, (num + 16) * sizeof (struct file_entry));
398 /* Zero the new memory. */
399 memset (files + old, 0, (num + 16 - old) * sizeof (struct file_entry));
402 files[num].filename = filename;
403 files[num].dir = 0;
404 files_in_use = num + 1;
406 return filename;
409 void
410 dwarf2_directive_loc (dummy)
411 int dummy ATTRIBUTE_UNUSED;
413 offsetT filenum, line, column;
415 filenum = get_absolute_expression ();
416 SKIP_WHITESPACE ();
417 line = get_absolute_expression ();
418 SKIP_WHITESPACE ();
419 column = get_absolute_expression ();
420 demand_empty_rest_of_line ();
422 if (filenum < 1)
424 as_bad (_("file number less than one"));
425 return;
427 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
429 as_bad (_("unassigned file number %ld"), (long) filenum);
430 return;
433 current.filenum = filenum;
434 current.line = line;
435 current.column = column;
436 current.flags = DWARF2_FLAG_BEGIN_STMT;
438 loc_directive_seen = TRUE;
440 #ifndef NO_LISTING
441 if (listing)
443 listing_source_file (files[filenum].filename);
444 listing_source_line (line);
446 #endif
449 static struct frag *
450 first_frag_for_seg (seg)
451 segT seg;
453 frchainS *f, *first = NULL;
455 for (f = frchain_root; f; f = f->frch_next)
456 if (f->frch_seg == seg
457 && (! first || first->frch_subseg > f->frch_subseg))
458 first = f;
460 return first ? first->frch_root : NULL;
463 static struct frag *
464 last_frag_for_seg (seg)
465 segT seg;
467 frchainS *f, *last = NULL;
469 for (f = frchain_root; f; f = f->frch_next)
470 if (f->frch_seg == seg
471 && (! last || last->frch_subseg < f->frch_subseg))
472 last= f;
474 return last ? last->frch_last : NULL;
477 /* Emit a single byte into the current segment. */
479 static inline void
480 out_byte (byte)
481 int byte;
483 FRAG_APPEND_1_CHAR (byte);
486 /* Emit a statement program opcode into the current segment. */
488 static inline void
489 out_opcode (opc)
490 int opc;
492 out_byte (opc);
495 /* Emit a two-byte word into the current segment. */
497 static inline void
498 out_two (data)
499 int data;
501 md_number_to_chars (frag_more (2), data, 2);
504 /* Emit a four byte word into the current segment. */
506 static inline void
507 out_four (data)
508 int data;
510 md_number_to_chars (frag_more (4), data, 4);
513 /* Emit an unsigned "little-endian base 128" number. */
515 static void
516 out_uleb128 (value)
517 addressT value;
519 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
522 /* Emit a tuple for .debug_abbrev. */
524 static inline void
525 out_abbrev (name, form)
526 int name, form;
528 out_uleb128 (name);
529 out_uleb128 (form);
532 /* Create a new fake symbol whose value is the current position. */
534 static symbolS *
535 symbol_new_now ()
537 return symbol_new (fake_label_name, now_seg, frag_now_fix (), frag_now);
540 /* Set the value of SYM to the current position in the current segment. */
542 static void
543 set_symbol_value_now (sym)
544 symbolS *sym;
546 S_SET_SEGMENT (sym, now_seg);
547 S_SET_VALUE (sym, frag_now_fix ());
548 symbol_set_frag (sym, frag_now);
551 /* Get the size of a fragment. */
553 static offsetT
554 get_frag_fix (frag)
555 fragS *frag;
557 frchainS *fr;
559 if (frag->fr_next)
560 return frag->fr_fix;
562 /* If a fragment is the last in the chain, special measures must be
563 taken to find its size before relaxation, since it may be pending
564 on some subsegment chain. */
565 for (fr = frchain_root; fr; fr = fr->frch_next)
566 if (fr->frch_last == frag)
568 long align_mask = -1 << get_recorded_alignment (fr->frch_seg);
569 return (((char *) obstack_next_free (&fr->frch_obstack)
570 - frag->fr_literal) + ~align_mask) & align_mask;
573 abort ();
576 /* Set an absolute address (may result in a relocation entry). */
578 static void
579 out_set_addr (seg, frag, ofs)
580 segT seg;
581 fragS *frag;
582 addressT ofs;
584 expressionS expr;
585 symbolS *sym;
587 sym = symbol_new (fake_label_name, seg, ofs, frag);
589 out_opcode (DW_LNS_extended_op);
590 out_uleb128 (sizeof_address + 1);
592 out_opcode (DW_LNE_set_address);
593 expr.X_op = O_symbol;
594 expr.X_add_symbol = sym;
595 expr.X_add_number = 0;
596 emit_expr (&expr, sizeof_address);
599 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
600 static void scale_addr_delta PARAMS ((addressT *));
602 static void
603 scale_addr_delta (addr_delta)
604 addressT *addr_delta;
606 static int printed_this = 0;
607 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
609 if (!printed_this)
610 as_bad("unaligned opcodes detected in executable segment");
611 printed_this = 1;
613 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
615 #else
616 #define scale_addr_delta(A)
617 #endif
619 /* Encode a pair of line and address skips as efficiently as possible.
620 Note that the line skip is signed, whereas the address skip is unsigned.
622 The following two routines *must* be kept in sync. This is
623 enforced by making emit_inc_line_addr abort if we do not emit
624 exactly the expected number of bytes. */
626 static int
627 size_inc_line_addr (line_delta, addr_delta)
628 int line_delta;
629 addressT addr_delta;
631 unsigned int tmp, opcode;
632 int len = 0;
634 /* Scale the address delta by the minimum instruction length. */
635 scale_addr_delta (&addr_delta);
637 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
638 We cannot use special opcodes here, since we want the end_sequence
639 to emit the matrix entry. */
640 if (line_delta == INT_MAX)
642 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
643 len = 1;
644 else
645 len = 1 + sizeof_leb128 (addr_delta, 0);
646 return len + 3;
649 /* Bias the line delta by the base. */
650 tmp = line_delta - DWARF2_LINE_BASE;
652 /* If the line increment is out of range of a special opcode, we
653 must encode it with DW_LNS_advance_line. */
654 if (tmp >= DWARF2_LINE_RANGE)
656 len = 1 + sizeof_leb128 (line_delta, 1);
657 line_delta = 0;
658 tmp = 0 - DWARF2_LINE_BASE;
661 /* Bias the opcode by the special opcode base. */
662 tmp += DWARF2_LINE_OPCODE_BASE;
664 /* Avoid overflow when addr_delta is large. */
665 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
667 /* Try using a special opcode. */
668 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
669 if (opcode <= 255)
670 return len + 1;
672 /* Try using DW_LNS_const_add_pc followed by special op. */
673 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
674 if (opcode <= 255)
675 return len + 2;
678 /* Otherwise use DW_LNS_advance_pc. */
679 len += 1 + sizeof_leb128 (addr_delta, 0);
681 /* DW_LNS_copy or special opcode. */
682 len += 1;
684 return len;
687 static void
688 emit_inc_line_addr (line_delta, addr_delta, p, len)
689 int line_delta;
690 addressT addr_delta;
691 char *p;
692 int len;
694 unsigned int tmp, opcode;
695 int need_copy = 0;
696 char *end = p + len;
698 /* Scale the address delta by the minimum instruction length. */
699 scale_addr_delta (&addr_delta);
701 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
702 We cannot use special opcodes here, since we want the end_sequence
703 to emit the matrix entry. */
704 if (line_delta == INT_MAX)
706 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
707 *p++ = DW_LNS_const_add_pc;
708 else
710 *p++ = DW_LNS_advance_pc;
711 p += output_leb128 (p, addr_delta, 0);
714 *p++ = DW_LNS_extended_op;
715 *p++ = 1;
716 *p++ = DW_LNE_end_sequence;
717 goto done;
720 /* Bias the line delta by the base. */
721 tmp = line_delta - DWARF2_LINE_BASE;
723 /* If the line increment is out of range of a special opcode, we
724 must encode it with DW_LNS_advance_line. */
725 if (tmp >= DWARF2_LINE_RANGE)
727 *p++ = DW_LNS_advance_line;
728 p += output_leb128 (p, line_delta, 1);
730 /* Prettier, I think, to use DW_LNS_copy instead of a
731 "line +0, addr +0" special opcode. */
732 if (addr_delta == 0)
734 *p++ = DW_LNS_copy;
735 goto done;
738 line_delta = 0;
739 tmp = 0 - DWARF2_LINE_BASE;
740 need_copy = 1;
743 /* Bias the opcode by the special opcode base. */
744 tmp += DWARF2_LINE_OPCODE_BASE;
746 /* Avoid overflow when addr_delta is large. */
747 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
749 /* Try using a special opcode. */
750 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
751 if (opcode <= 255)
753 *p++ = opcode;
754 goto done;
757 /* Try using DW_LNS_const_add_pc followed by special op. */
758 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
759 if (opcode <= 255)
761 *p++ = DW_LNS_const_add_pc;
762 *p++ = opcode;
763 goto done;
767 /* Otherwise use DW_LNS_advance_pc. */
768 *p++ = DW_LNS_advance_pc;
769 p += output_leb128 (p, addr_delta, 0);
771 if (need_copy)
772 *p++ = DW_LNS_copy;
773 else
774 *p++ = tmp;
776 done:
777 assert (p == end);
780 /* Handy routine to combine calls to the above two routines. */
782 static void
783 out_inc_line_addr (line_delta, addr_delta)
784 int line_delta;
785 addressT addr_delta;
787 int len = size_inc_line_addr (line_delta, addr_delta);
788 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
791 /* Generate a variant frag that we can use to relax address/line
792 increments between fragments of the target segment. */
794 static void
795 relax_inc_line_addr (line_delta, seg, to_frag, to_ofs, from_frag, from_ofs)
796 int line_delta;
797 segT seg;
798 fragS *to_frag, *from_frag;
799 addressT to_ofs, from_ofs;
801 symbolS *to_sym, *from_sym;
802 expressionS expr;
803 int max_chars;
805 to_sym = symbol_new (fake_label_name, seg, to_ofs, to_frag);
806 from_sym = symbol_new (fake_label_name, seg, from_ofs, from_frag);
808 expr.X_op = O_subtract;
809 expr.X_add_symbol = to_sym;
810 expr.X_op_symbol = from_sym;
811 expr.X_add_number = 0;
813 /* The maximum size of the frag is the line delta with a maximum
814 sized address delta. */
815 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
817 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
818 make_expr_symbol (&expr), line_delta, NULL);
821 /* The function estimates the size of a rs_dwarf2dbg variant frag
822 based on the current values of the symbols. It is called before
823 the relaxation loop. We set fr_subtype to the expected length. */
826 dwarf2dbg_estimate_size_before_relax (frag)
827 fragS *frag;
829 offsetT addr_delta;
830 int size;
832 addr_delta = resolve_symbol_value (frag->fr_symbol);
833 size = size_inc_line_addr (frag->fr_offset, addr_delta);
835 frag->fr_subtype = size;
837 return size;
840 /* This function relaxes a rs_dwarf2dbg variant frag based on the
841 current values of the symbols. fr_subtype is the current length
842 of the frag. This returns the change in frag length. */
845 dwarf2dbg_relax_frag (frag)
846 fragS *frag;
848 int old_size, new_size;
850 old_size = frag->fr_subtype;
851 new_size = dwarf2dbg_estimate_size_before_relax (frag);
853 return new_size - old_size;
856 /* This function converts a rs_dwarf2dbg variant frag into a normal
857 fill frag. This is called after all relaxation has been done.
858 fr_subtype will be the desired length of the frag. */
860 void
861 dwarf2dbg_convert_frag (frag)
862 fragS *frag;
864 offsetT addr_diff;
866 addr_diff = resolve_symbol_value (frag->fr_symbol);
868 /* fr_var carries the max_chars that we created the fragment with.
869 fr_subtype carries the current expected length. We must, of
870 course, have allocated enough memory earlier. */
871 assert (frag->fr_var >= (int) frag->fr_subtype);
873 emit_inc_line_addr (frag->fr_offset, addr_diff,
874 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
876 frag->fr_fix += frag->fr_subtype;
877 frag->fr_type = rs_fill;
878 frag->fr_var = 0;
879 frag->fr_offset = 0;
882 /* Generate .debug_line content for the chain of line number entries
883 beginning at E, for segment SEG. */
885 static void
886 process_entries (seg, e)
887 segT seg;
888 struct line_entry *e;
890 unsigned filenum = 1;
891 unsigned line = 1;
892 unsigned column = 0;
893 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_BEGIN_STMT : 0;
894 fragS *frag = NULL;
895 fragS *last_frag;
896 addressT frag_ofs = 0;
897 addressT last_frag_ofs;
898 struct line_entry *next;
900 while (e)
902 int changed = 0;
904 if (filenum != e->loc.filenum)
906 filenum = e->loc.filenum;
907 out_opcode (DW_LNS_set_file);
908 out_uleb128 (filenum);
909 changed = 1;
912 if (column != e->loc.column)
914 column = e->loc.column;
915 out_opcode (DW_LNS_set_column);
916 out_uleb128 (column);
917 changed = 1;
920 if ((e->loc.flags ^ flags) & DWARF2_FLAG_BEGIN_STMT)
922 flags = e->loc.flags;
923 out_opcode (DW_LNS_negate_stmt);
924 changed = 1;
927 if (e->loc.flags & DWARF2_FLAG_BEGIN_BLOCK)
929 out_opcode (DW_LNS_set_basic_block);
930 changed = 1;
933 /* Don't try to optimize away redundant entries; gdb wants two
934 entries for a function where the code starts on the same line as
935 the {, and there's no way to identify that case here. Trust gcc
936 to optimize appropriately. */
937 if (1 /* line != e->loc.line || changed */)
939 int line_delta = e->loc.line - line;
940 if (frag == NULL)
942 out_set_addr (seg, e->frag, e->frag_ofs);
943 out_inc_line_addr (line_delta, 0);
945 else if (frag == e->frag)
946 out_inc_line_addr (line_delta, e->frag_ofs - frag_ofs);
947 else
948 relax_inc_line_addr (line_delta, seg, e->frag, e->frag_ofs,
949 frag, frag_ofs);
951 frag = e->frag;
952 frag_ofs = e->frag_ofs;
953 line = e->loc.line;
955 else if (frag == NULL)
957 out_set_addr (seg, e->frag, e->frag_ofs);
958 frag = e->frag;
959 frag_ofs = e->frag_ofs;
962 next = e->next;
963 free (e);
964 e = next;
967 /* Emit a DW_LNE_end_sequence for the end of the section. */
968 last_frag = last_frag_for_seg (seg);
969 last_frag_ofs = get_frag_fix (last_frag);
970 if (frag == last_frag)
971 out_inc_line_addr (INT_MAX, last_frag_ofs - frag_ofs);
972 else
973 relax_inc_line_addr (INT_MAX, seg, last_frag, last_frag_ofs,
974 frag, frag_ofs);
977 /* Emit the directory and file tables for .debug_line. */
979 static void
980 out_file_list ()
982 size_t size;
983 char *cp;
984 unsigned int i;
986 /* Terminate directory list. */
987 out_byte ('\0');
989 for (i = 1; i < files_in_use; ++i)
991 if (files[i].filename == NULL)
993 as_bad (_("unassigned file number %ld"), (long) i);
994 continue;
997 size = strlen (files[i].filename) + 1;
998 cp = frag_more (size);
999 memcpy (cp, files[i].filename, size);
1001 out_uleb128 (files[i].dir); /* directory number */
1002 out_uleb128 (0); /* last modification timestamp */
1003 out_uleb128 (0); /* filesize */
1006 /* Terminate filename list. */
1007 out_byte (0);
1010 /* Emit the collected .debug_line data. */
1012 static void
1013 out_debug_line (line_seg)
1014 segT line_seg;
1016 expressionS expr;
1017 symbolS *line_start;
1018 symbolS *prologue_end;
1019 symbolS *line_end;
1020 struct line_seg *s;
1021 enum dwarf2_format d2f;
1022 int sizeof_offset;
1024 subseg_set (line_seg, 0);
1026 line_start = symbol_new_now ();
1027 prologue_end = symbol_make (fake_label_name);
1028 line_end = symbol_make (fake_label_name);
1030 /* Total length of the information for this compilation unit. */
1031 expr.X_op = O_subtract;
1032 expr.X_add_symbol = line_end;
1033 expr.X_op_symbol = line_start;
1035 d2f = DWARF2_FORMAT ();
1036 if (d2f == dwarf2_format_32bit)
1038 expr.X_add_number = -4;
1039 emit_expr (&expr, 4);
1040 sizeof_offset = 4;
1042 else if (d2f == dwarf2_format_64bit)
1044 expr.X_add_number = -12;
1045 out_four (-1);
1046 emit_expr (&expr, 8);
1047 sizeof_offset = 8;
1049 else if (d2f == dwarf2_format_64bit_irix)
1051 expr.X_add_number = -8;
1052 emit_expr (&expr, 8);
1053 sizeof_offset = 8;
1055 else
1057 as_fatal (_("internal error: unknown dwarf2 format"));
1060 /* Version. */
1061 out_two (2);
1063 /* Length of the prologue following this length. */
1064 expr.X_op = O_subtract;
1065 expr.X_add_symbol = prologue_end;
1066 expr.X_op_symbol = line_start;
1067 expr.X_add_number = - (4 + 2 + 4);
1068 emit_expr (&expr, sizeof_offset);
1070 /* Parameters of the state machine. */
1071 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1072 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1073 out_byte (DWARF2_LINE_BASE);
1074 out_byte (DWARF2_LINE_RANGE);
1075 out_byte (DWARF2_LINE_OPCODE_BASE);
1077 /* Standard opcode lengths. */
1078 out_byte (0); /* DW_LNS_copy */
1079 out_byte (1); /* DW_LNS_advance_pc */
1080 out_byte (1); /* DW_LNS_advance_line */
1081 out_byte (1); /* DW_LNS_set_file */
1082 out_byte (1); /* DW_LNS_set_column */
1083 out_byte (0); /* DW_LNS_negate_stmt */
1084 out_byte (0); /* DW_LNS_set_basic_block */
1085 out_byte (0); /* DW_LNS_const_add_pc */
1086 out_byte (1); /* DW_LNS_fixed_advance_pc */
1088 out_file_list ();
1090 set_symbol_value_now (prologue_end);
1092 /* For each section, emit a statement program. */
1093 for (s = all_segs; s; s = s->next)
1094 process_entries (s->seg, s->head->head);
1096 set_symbol_value_now (line_end);
1099 /* Emit data for .debug_aranges. */
1101 static void
1102 out_debug_aranges (aranges_seg, info_seg)
1103 segT aranges_seg;
1104 segT info_seg;
1106 unsigned int addr_size = sizeof_address;
1107 addressT size, skip;
1108 struct line_seg *s;
1109 expressionS expr;
1110 char *p;
1112 size = 4 + 2 + 4 + 1 + 1;
1114 skip = 2 * addr_size - (size & (2 * addr_size - 1));
1115 if (skip == 2 * addr_size)
1116 skip = 0;
1117 size += skip;
1119 for (s = all_segs; s; s = s->next)
1120 size += 2 * addr_size;
1122 size += 2 * addr_size;
1124 subseg_set (aranges_seg, 0);
1126 /* Length of the compilation unit. */
1127 out_four (size - 4);
1129 /* Version. */
1130 out_two (2);
1132 /* Offset to .debug_info. */
1133 expr.X_op = O_symbol;
1134 expr.X_add_symbol = section_symbol (info_seg);
1135 expr.X_add_number = 0;
1136 emit_expr (&expr, 4);
1138 /* Size of an address (offset portion). */
1139 out_byte (addr_size);
1141 /* Size of a segment descriptor. */
1142 out_byte (0);
1144 /* Align the header. */
1145 if (skip)
1146 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1148 for (s = all_segs; s; s = s->next)
1150 fragS *frag;
1151 symbolS *beg, *end;
1153 frag = first_frag_for_seg (s->seg);
1154 beg = symbol_new (fake_label_name, s->seg, 0, frag);
1155 s->text_start = beg;
1157 frag = last_frag_for_seg (s->seg);
1158 end = symbol_new (fake_label_name, s->seg, get_frag_fix (frag), frag);
1159 s->text_end = end;
1161 expr.X_op = O_symbol;
1162 expr.X_add_symbol = beg;
1163 expr.X_add_number = 0;
1164 emit_expr (&expr, addr_size);
1166 expr.X_op = O_subtract;
1167 expr.X_add_symbol = end;
1168 expr.X_op_symbol = beg;
1169 expr.X_add_number = 0;
1170 emit_expr (&expr, addr_size);
1173 p = frag_more (2 * addr_size);
1174 md_number_to_chars (p, 0, addr_size);
1175 md_number_to_chars (p + addr_size, 0, addr_size);
1178 /* Emit data for .debug_abbrev. Note that this must be kept in
1179 sync with out_debug_info below. */
1181 static void
1182 out_debug_abbrev (abbrev_seg)
1183 segT abbrev_seg;
1185 subseg_set (abbrev_seg, 0);
1187 out_uleb128 (1);
1188 out_uleb128 (DW_TAG_compile_unit);
1189 out_byte (DW_CHILDREN_no);
1190 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1191 if (all_segs->next == NULL)
1193 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1194 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1196 out_abbrev (DW_AT_name, DW_FORM_string);
1197 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1198 out_abbrev (DW_AT_producer, DW_FORM_string);
1199 out_abbrev (DW_AT_language, DW_FORM_data2);
1200 out_abbrev (0, 0);
1202 /* Terminate the abbreviations for this compilation unit. */
1203 out_byte (0);
1206 /* Emit a description of this compilation unit for .debug_info. */
1208 static void
1209 out_debug_info (info_seg, abbrev_seg, line_seg)
1210 segT info_seg;
1211 segT abbrev_seg;
1212 segT line_seg;
1214 char producer[128];
1215 char *comp_dir;
1216 expressionS expr;
1217 symbolS *info_start;
1218 symbolS *info_end;
1219 char *p;
1220 int len;
1221 enum dwarf2_format d2f;
1222 int sizeof_offset;
1224 subseg_set (info_seg, 0);
1226 info_start = symbol_new_now ();
1227 info_end = symbol_make (fake_label_name);
1229 /* Compilation Unit length. */
1230 expr.X_op = O_subtract;
1231 expr.X_add_symbol = info_end;
1232 expr.X_op_symbol = info_start;
1234 d2f = DWARF2_FORMAT ();
1235 if (d2f == dwarf2_format_32bit)
1237 expr.X_add_number = -4;
1238 emit_expr (&expr, 4);
1239 sizeof_offset = 4;
1241 else if (d2f == dwarf2_format_64bit)
1243 expr.X_add_number = -12;
1244 out_four (-1);
1245 emit_expr (&expr, 8);
1246 sizeof_offset = 8;
1248 else if (d2f == dwarf2_format_64bit_irix)
1250 expr.X_add_number = -8;
1251 emit_expr (&expr, 8);
1252 sizeof_offset = 8;
1254 else
1256 as_fatal (_("internal error: unknown dwarf2 format"));
1259 /* DWARF version. */
1260 out_two (2);
1262 /* .debug_abbrev offset */
1263 expr.X_op = O_symbol;
1264 expr.X_add_symbol = section_symbol (abbrev_seg);
1265 expr.X_add_number = 0;
1266 emit_expr (&expr, sizeof_offset);
1268 /* Target address size. */
1269 out_byte (sizeof_address);
1271 /* DW_TAG_compile_unit DIE abbrev */
1272 out_uleb128 (1);
1274 /* DW_AT_stmt_list */
1275 expr.X_op = O_symbol;
1276 expr.X_add_symbol = section_symbol (line_seg);
1277 expr.X_add_number = 0;
1278 emit_expr (&expr, 4);
1280 /* These two attributes may only be emitted if all of the code is
1281 contiguous. Multiple sections are not that. */
1282 if (all_segs->next == NULL)
1284 /* DW_AT_low_pc */
1285 expr.X_op = O_symbol;
1286 expr.X_add_symbol = all_segs->text_start;
1287 expr.X_add_number = 0;
1288 emit_expr (&expr, sizeof_address);
1290 /* DW_AT_high_pc */
1291 expr.X_op = O_symbol;
1292 expr.X_add_symbol = all_segs->text_end;
1293 expr.X_add_number = 0;
1294 emit_expr (&expr, sizeof_address);
1297 /* DW_AT_name. We don't have the actual file name that was present
1298 on the command line, so assume files[1] is the main input file.
1299 We're not supposed to get called unless at least one line number
1300 entry was emitted, so this should always be defined. */
1301 if (!files || files_in_use < 1)
1302 abort ();
1303 len = strlen (files[1].filename) + 1;
1304 p = frag_more (len);
1305 memcpy (p, files[1].filename, len);
1307 /* DW_AT_comp_dir */
1308 comp_dir = getpwd ();
1309 len = strlen (comp_dir) + 1;
1310 p = frag_more (len);
1311 memcpy (p, comp_dir, len);
1313 /* DW_AT_producer */
1314 sprintf (producer, "GNU AS %s", VERSION);
1315 len = strlen (producer) + 1;
1316 p = frag_more (len);
1317 memcpy (p, producer, len);
1319 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1320 dwarf2 draft has no standard code for assembler. */
1321 out_two (DW_LANG_Mips_Assembler);
1323 set_symbol_value_now (info_end);
1326 void
1327 dwarf2_finish ()
1329 segT line_seg;
1330 struct line_seg *s;
1332 /* We don't need to do anything unless:
1333 - Some debug information was recorded via .file/.loc
1334 - or, we are generating DWARF2 information ourself (--gdwarf2)
1335 - or, there is a user-provided .debug_info section which could
1336 reference the file table in the .debug_line section we generate
1337 below. */
1338 if (all_segs == NULL
1339 && debug_type != DEBUG_DWARF2
1340 && bfd_get_section_by_name (stdoutput, ".debug_info") == NULL)
1341 return;
1343 /* Calculate the size of an address for the target machine. */
1344 sizeof_address = bfd_arch_bits_per_address (stdoutput) / 8;
1346 /* Create and switch to the line number section. */
1347 line_seg = subseg_new (".debug_line", 0);
1348 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY);
1350 /* For each subsection, chain the debug entries together. */
1351 for (s = all_segs; s; s = s->next)
1353 struct line_subseg *ss = s->head;
1354 struct line_entry **ptail = ss->ptail;
1356 while ((ss = ss->next) != NULL)
1358 *ptail = ss->head;
1359 ptail = ss->ptail;
1363 out_debug_line (line_seg);
1365 /* If this is assembler generated line info, we need .debug_info
1366 and .debug_abbrev sections as well. */
1367 if (all_segs != NULL && debug_type == DEBUG_DWARF2)
1369 segT abbrev_seg;
1370 segT info_seg;
1371 segT aranges_seg;
1373 info_seg = subseg_new (".debug_info", 0);
1374 abbrev_seg = subseg_new (".debug_abbrev", 0);
1375 aranges_seg = subseg_new (".debug_aranges", 0);
1377 bfd_set_section_flags (stdoutput, info_seg, SEC_READONLY);
1378 bfd_set_section_flags (stdoutput, abbrev_seg, SEC_READONLY);
1379 bfd_set_section_flags (stdoutput, aranges_seg, SEC_READONLY);
1381 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1383 out_debug_aranges (aranges_seg, info_seg);
1384 out_debug_abbrev (abbrev_seg);
1385 out_debug_info (info_seg, abbrev_seg, line_seg);
1389 #else
1390 void
1391 dwarf2_finish ()
1396 dwarf2dbg_estimate_size_before_relax (frag)
1397 fragS *frag ATTRIBUTE_UNUSED;
1399 as_fatal (_("dwarf2 is not supported for this object file format"));
1400 return 0;
1404 dwarf2dbg_relax_frag (frag)
1405 fragS *frag ATTRIBUTE_UNUSED;
1407 as_fatal (_("dwarf2 is not supported for this object file format"));
1408 return 0;
1411 void
1412 dwarf2dbg_convert_frag (frag)
1413 fragS *frag ATTRIBUTE_UNUSED;
1415 as_fatal (_("dwarf2 is not supported for this object file format"));
1418 void
1419 dwarf2_emit_insn (size)
1420 int size ATTRIBUTE_UNUSED;
1424 char *
1425 dwarf2_directive_file (dummy)
1426 int dummy ATTRIBUTE_UNUSED;
1428 s_app_file (0);
1429 return NULL;
1432 void
1433 dwarf2_directive_loc (dummy)
1434 int dummy ATTRIBUTE_UNUSED;
1436 as_fatal (_("dwarf2 is not supported for this object file format"));
1438 #endif /* BFD_ASSEMBLER */