2000-12-12 H.J. Lu <hjl@gnu.org>
[binutils.git] / gas / dwarf2dbg.c
blob5866cb4ae6bbc20cfaf62c1d4c2bc321e23be89c
1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright (C) 1999, 2000 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 <limits.h>
33 #include "as.h"
35 #ifdef BFD_ASSEMBLER
37 #include "dwarf2dbg.h"
38 #include "subsegs.h"
40 #include "elf/dwarf2.h"
42 /* Since we can't generate the prolog until the body is complete, we
43 use three different subsegments for .debug_line: one holding the
44 prolog, one for the directory and filename info, and one for the
45 body ("statement program"). */
46 #define DL_PROLOG 0
47 #define DL_FILES 1
48 #define DL_BODY 2
50 /* First special line opcde - leave room for the standard opcodes.
51 Note: If you want to change this, you'll have to update the
52 "standard_opcode_lengths" table that is emitted below in
53 dwarf2_finish(). */
54 #define DWARF2_LINE_OPCODE_BASE 10
56 #ifndef DWARF2_LINE_BASE
57 /* Minimum line offset in a special line info. opcode. This value
58 was chosen to give a reasonable range of values. */
59 # define DWARF2_LINE_BASE -5
60 #endif
62 /* Range of line offsets in a special line info. opcode. */
63 #ifndef DWARF2_LINE_RANGE
64 # define DWARF2_LINE_RANGE 14
65 #endif
67 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
68 /* Define the architecture-dependent minimum instruction length (in
69 bytes). This value should be rather too small than too big. */
70 # define DWARF2_LINE_MIN_INSN_LENGTH 1
71 #endif
73 /* Flag that indicates the initial value of the is_stmt_start flag.
74 In the present implementation, we do not mark any lines as
75 the beginning of a source statement, because that information
76 is not made available by the GCC front-end. */
77 #define DWARF2_LINE_DEFAULT_IS_STMT 1
79 /* Given a special op, return the line skip amount. */
80 #define SPECIAL_LINE(op) \
81 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
83 /* Given a special op, return the address skip amount (in units of
84 DWARF2_LINE_MIN_INSN_LENGTH. */
85 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
87 /* The maximum address skip amount that can be encoded with a special op. */
88 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
91 struct line_entry
93 struct line_entry *next;
94 fragS *frag;
95 addressT frag_ofs;
96 struct dwarf2_line_info loc;
99 struct line_subseg
101 struct line_subseg *next;
102 subsegT subseg;
103 struct line_entry *head;
104 struct line_entry **ptail;
107 struct line_seg
109 struct line_seg *next;
110 segT seg;
111 struct line_subseg *head;
112 symbolS *text_start;
113 symbolS *text_end;
116 /* Collects data for all line table entries during assembly. */
117 static struct line_seg *all_segs;
119 struct file_entry
121 char *filename;
122 unsigned int dir;
125 /* Table of files used by .debug_line. */
126 static struct file_entry *files;
127 static unsigned int files_in_use;
128 static unsigned int files_allocated;
130 /* Correlate file numbers as given by the user in .file/.loc directives
131 with the file numbers used in the output debug info. */
132 static unsigned int *user_filenum;
133 static unsigned int user_filenum_allocated;
135 /* True when we've seen a .loc directive recently. Used to avoid
136 doing work when there's nothing to do. */
137 static boolean loc_directive_seen;
139 /* Current location as indicated by the most recent .loc directive. */
140 static struct dwarf2_line_info current;
142 /* Fake label name. */
143 static char const fake_label_name[] = ".L0\001";
145 /* The size of an address on the target. */
146 static unsigned int sizeof_address;
148 static struct line_subseg *get_line_subseg PARAMS ((segT, subsegT));
149 static unsigned int get_filenum PARAMS ((const char *));
150 static struct frag *first_frag_for_seg PARAMS ((segT));
151 static struct frag *last_frag_for_seg PARAMS ((segT));
152 static void out_byte PARAMS ((int));
153 static void out_opcode PARAMS ((int));
154 static void out_two PARAMS ((int));
155 static void out_four PARAMS ((int));
156 static void out_abbrev PARAMS ((int, int));
157 static void out_uleb128 PARAMS ((addressT));
158 static symbolS *symbol_new_now PARAMS ((void));
159 static void set_symbol_value_now PARAMS ((symbolS *));
160 static offsetT get_frag_fix PARAMS ((fragS *));
161 static void out_set_addr PARAMS ((segT, fragS *, addressT));
162 static int size_inc_line_addr PARAMS ((int, addressT));
163 static void emit_inc_line_addr PARAMS ((int, addressT, char *, int));
164 static void out_inc_line_addr PARAMS ((int, addressT));
165 static void relax_inc_line_addr PARAMS ((int, segT, fragS *, addressT,
166 fragS *, addressT));
167 static void process_entries PARAMS ((segT, struct line_entry *));
168 static void out_file_list PARAMS ((void));
169 static void out_debug_line PARAMS ((segT));
170 static void out_debug_aranges PARAMS ((segT, segT));
171 static void out_debug_abbrev PARAMS ((segT));
172 static void out_debug_info PARAMS ((segT, segT, segT));
174 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
176 static struct line_subseg *
177 get_line_subseg (seg, subseg)
178 segT seg;
179 subsegT subseg;
181 static segT last_seg;
182 static subsegT last_subseg;
183 static struct line_subseg *last_line_subseg;
185 struct line_seg *s;
186 struct line_subseg **pss, *ss;
188 if (seg == last_seg && subseg == last_subseg)
189 return last_line_subseg;
191 for (s = all_segs; s ; s = s->next)
192 if (s->seg == seg)
193 goto found_seg;
195 s = (struct line_seg *) xmalloc (sizeof (*s));
196 s->next = all_segs;
197 s->seg = seg;
198 s->head = NULL;
199 all_segs = s;
201 found_seg:
202 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
204 if (ss->subseg == subseg)
205 goto found_subseg;
206 if (ss->subseg > subseg)
207 break;
210 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
211 ss->next = *pss;
212 ss->subseg = subseg;
213 ss->head = NULL;
214 ss->ptail = &ss->head;
215 *pss = ss;
217 found_subseg:
218 last_seg = seg;
219 last_subseg = subseg;
220 last_line_subseg = ss;
222 return ss;
225 /* Record an entry for LOC ocurring at OFS within the current fragment. */
227 void
228 dwarf2_gen_line_info (ofs, loc)
229 addressT ofs;
230 struct dwarf2_line_info *loc;
232 struct line_subseg *ss;
233 struct line_entry *e;
235 /* Early out for as-yet incomplete location information. */
236 if (loc->filenum == 0 || loc->line == 0)
237 return;
239 e = (struct line_entry *) xmalloc (sizeof (*e));
240 e->next = NULL;
241 e->frag = frag_now;
242 e->frag_ofs = ofs;
243 e->loc = *loc;
245 ss = get_line_subseg (now_seg, now_subseg);
246 *ss->ptail = e;
247 ss->ptail = &e->next;
250 void
251 dwarf2_where (line)
252 struct dwarf2_line_info *line;
254 if (debug_type == DEBUG_DWARF2)
256 char *filename;
257 as_where (&filename, &line->line);
258 line->filenum = get_filenum (filename);
259 line->column = 0;
260 line->flags = DWARF2_FLAG_BEGIN_STMT;
262 else
263 *line = current;
266 /* Called for each machine instruction, or relatively atomic group of
267 machine instructions (ie built-in macro). The instruction or group
268 is SIZE bytes in length. If dwarf2 line number generation is called
269 for, emit a line statement appropriately. */
271 void
272 dwarf2_emit_insn (size)
273 int size;
275 struct dwarf2_line_info loc;
277 if (debug_type != DEBUG_DWARF2 && ! loc_directive_seen)
278 return;
279 loc_directive_seen = false;
281 dwarf2_where (&loc);
282 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
285 /* Get a .debug_line file number for FILENAME. */
287 static unsigned int
288 get_filenum (filename)
289 const char *filename;
291 static unsigned int last_used;
292 unsigned int i;
294 if (last_used)
295 if (strcmp (filename, files[last_used].filename) == 0)
296 return last_used;
298 for (i = 1; i < files_in_use; ++i)
299 if (strcmp (filename, files[i].filename) == 0)
300 return i;
302 if (i >= files_allocated)
304 files_allocated = i + 32;
305 files = (struct file_entry *)
306 xrealloc (files, (i + 32) * sizeof(struct file_entry));
309 files[i].filename = xstrdup(filename);
310 files[i].dir = 0;
311 files_in_use = i + 1;
312 last_used = i;
314 return i;
317 /* Handle the .file directive. */
319 void
320 dwarf2_directive_file (dummy)
321 int dummy ATTRIBUTE_UNUSED;
323 offsetT num;
324 const char *filename;
325 int filename_len;
327 /* Continue to accept a bare string and pass it off. */
328 SKIP_WHITESPACE ();
329 if (*input_line_pointer == '"')
331 s_app_file (0);
332 return;
335 num = get_absolute_expression ();
336 filename = demand_copy_C_string (&filename_len);
337 demand_empty_rest_of_line ();
339 if (num < 0)
341 as_bad (_("File number less than zero"));
342 return;
345 if (num >= (int) user_filenum_allocated)
347 unsigned int old = user_filenum_allocated;
349 user_filenum_allocated = num + 16;
350 user_filenum = (unsigned int *)
351 xrealloc (user_filenum, (num + 16) * sizeof (unsigned int));
353 /* Zero the new memory. */
354 memset (user_filenum + old, 0, (num + 16 - old) * sizeof(unsigned int));
357 user_filenum[num] = get_filenum (filename);
360 void
361 dwarf2_directive_loc (dummy)
362 int dummy ATTRIBUTE_UNUSED;
364 offsetT filenum, line, column;
366 filenum = get_absolute_expression ();
367 SKIP_WHITESPACE ();
368 line = get_absolute_expression ();
369 SKIP_WHITESPACE ();
370 column = get_absolute_expression ();
371 demand_empty_rest_of_line ();
373 if (filenum < 0)
375 as_bad (_("File number less than zero"));
376 return;
378 if (filenum >= (int) user_filenum_allocated
379 || user_filenum[filenum] == 0)
381 as_bad (_("Unassigned file number %ld"), (long) filenum);
382 return;
385 current.filenum = user_filenum[filenum];
386 current.line = line;
387 current.column = column;
388 current.flags = DWARF2_FLAG_BEGIN_STMT;
390 loc_directive_seen = true;
392 #ifndef NO_LISTING
393 if (listing)
394 listing_source_line (line);
395 #endif
399 static struct frag *
400 first_frag_for_seg (seg)
401 segT seg;
403 frchainS *f, *first = NULL;
405 for (f = frchain_root; f ; f = f->frch_next)
406 if (f->frch_seg == seg
407 && (! first || first->frch_subseg > f->frch_subseg))
408 first = f;
410 return first ? first->frch_root : NULL;
413 static struct frag *
414 last_frag_for_seg (seg)
415 segT seg;
417 frchainS *f, *last = NULL;
419 for (f = frchain_root; f ; f = f->frch_next)
420 if (f->frch_seg == seg
421 && (! last || last->frch_subseg < f->frch_subseg))
422 last= f;
424 return last ? last->frch_last : NULL;
427 /* Emit a single byte into the current segment. */
429 static inline void
430 out_byte (byte)
431 int byte;
433 FRAG_APPEND_1_CHAR (byte);
436 /* Emit a statement program opcode into the current segment. */
438 static inline void
439 out_opcode (opc)
440 int opc;
442 out_byte (opc);
445 /* Emit a two-byte word into the current segment. */
447 static inline void
448 out_two (data)
449 int data;
451 md_number_to_chars (frag_more (2), data, 2);
454 /* Emit a four byte word into the current segment. */
456 static inline void
457 out_four (data)
458 int data;
460 md_number_to_chars (frag_more (4), data, 4);
463 /* Emit an unsigned "little-endian base 128" number. */
465 static void
466 out_uleb128 (value)
467 addressT value;
469 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
472 /* Emit a tuple for .debug_abbrev. */
474 static inline void
475 out_abbrev (name, form)
476 int name, form;
478 out_uleb128 (name);
479 out_uleb128 (form);
482 /* Create a new fake symbol whose value is the current position. */
484 static symbolS *
485 symbol_new_now ()
487 return symbol_new (fake_label_name, now_seg, frag_now_fix (), frag_now);
490 /* Set the value of SYM to the current position in the current segment. */
492 static void
493 set_symbol_value_now (sym)
494 symbolS *sym;
496 S_SET_SEGMENT (sym, now_seg);
497 S_SET_VALUE (sym, frag_now_fix ());
498 symbol_set_frag (sym, frag_now);
501 /* Get the size of a fragment. */
503 static offsetT
504 get_frag_fix (frag)
505 fragS *frag;
507 frchainS *fr;
509 if (frag->fr_next)
510 return frag->fr_fix;
512 /* If a fragment is the last in the chain, special measures must be
513 taken to find its size before relaxation, since it may be pending
514 on some subsegment chain. */
515 for (fr = frchain_root; fr ; fr = fr->frch_next)
516 if (fr->frch_last == frag)
518 return ((char *) obstack_next_free (&fr->frch_obstack)
519 - frag->fr_literal);
522 abort ();
525 /* Set an absolute address (may result in a relocation entry). */
527 static void
528 out_set_addr (seg, frag, ofs)
529 segT seg;
530 fragS *frag;
531 addressT ofs;
533 expressionS expr;
534 symbolS *sym;
536 sym = symbol_new (fake_label_name, seg, ofs, frag);
538 out_opcode (DW_LNS_extended_op);
539 out_uleb128 (sizeof_address + 1);
541 out_opcode (DW_LNE_set_address);
542 expr.X_op = O_symbol;
543 expr.X_add_symbol = sym;
544 expr.X_add_number = 0;
545 emit_expr (&expr, sizeof_address);
548 /* Encode a pair of line and address skips as efficiently as possible.
549 Note that the line skip is signed, whereas the address skip is unsigned.
551 The following two routines *must* be kept in sync. This is
552 enforced by making emit_inc_line_addr abort if we do not emit
553 exactly the expected number of bytes. */
555 static int
556 size_inc_line_addr (line_delta, addr_delta)
557 int line_delta;
558 addressT addr_delta;
560 unsigned int tmp, opcode;
561 int len = 0;
563 /* Scale the address delta by the minimum instruction length. */
564 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
565 assert (addr_delta % DWARF2_LINE_MIN_INSN_LENGTH == 0);
566 addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
567 #endif
569 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
570 We cannot use special opcodes here, since we want the end_sequence
571 to emit the matrix entry. */
572 if (line_delta == INT_MAX)
574 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
575 len = 1;
576 else
577 len = 1 + sizeof_leb128 (addr_delta, 0);
578 return len + 3;
581 /* Bias the line delta by the base. */
582 tmp = line_delta - DWARF2_LINE_BASE;
584 /* If the line increment is out of range of a special opcode, we
585 must encode it with DW_LNS_advance_line. */
586 if (tmp >= DWARF2_LINE_RANGE)
588 len = 1 + sizeof_leb128 (line_delta, 1);
589 line_delta = 0;
590 tmp = 0 - DWARF2_LINE_BASE;
593 /* Bias the opcode by the special opcode base. */
594 tmp += DWARF2_LINE_OPCODE_BASE;
596 /* Avoid overflow when addr_delta is large. */
597 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
599 /* Try using a special opcode. */
600 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
601 if (opcode <= 255)
602 return len + 1;
604 /* Try using DW_LNS_const_add_pc followed by special op. */
605 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
606 if (opcode <= 255)
607 return len + 2;
610 /* Otherwise use DW_LNS_advance_pc. */
611 len += 1 + sizeof_leb128 (addr_delta, 0);
613 /* DW_LNS_copy or special opcode. */
614 len += 1;
616 return len;
619 static void
620 emit_inc_line_addr (line_delta, addr_delta, p, len)
621 int line_delta;
622 addressT addr_delta;
623 char *p;
624 int len;
626 unsigned int tmp, opcode;
627 int need_copy = 0;
628 char *end = p + len;
630 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
631 /* Scale the address delta by the minimum instruction length. */
632 assert (addr_delta % DWARF2_LINE_MIN_INSN_LENGTH == 0);
633 addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
634 #endif
635 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
636 We cannot use special opcodes here, since we want the end_sequence
637 to emit the matrix entry. */
638 if (line_delta == INT_MAX)
640 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
641 *p++ = DW_LNS_const_add_pc;
642 else
644 *p++ = DW_LNS_advance_pc;
645 p += output_leb128 (p, addr_delta, 0);
648 *p++ = DW_LNS_extended_op;
649 *p++ = 1;
650 *p++ = DW_LNE_end_sequence;
651 goto done;
654 /* Bias the line delta by the base. */
655 tmp = line_delta - DWARF2_LINE_BASE;
657 /* If the line increment is out of range of a special opcode, we
658 must encode it with DW_LNS_advance_line. */
659 if (tmp >= DWARF2_LINE_RANGE)
661 *p++ = DW_LNS_advance_line;
662 p += output_leb128 (p, line_delta, 1);
664 /* Prettier, I think, to use DW_LNS_copy instead of a
665 "line +0, addr +0" special opcode. */
666 if (addr_delta == 0)
668 *p++ = DW_LNS_copy;
669 goto done;
672 line_delta = 0;
673 tmp = 0 - DWARF2_LINE_BASE;
674 need_copy = 1;
677 /* Bias the opcode by the special opcode base. */
678 tmp += DWARF2_LINE_OPCODE_BASE;
680 /* Avoid overflow when addr_delta is large. */
681 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
683 /* Try using a special opcode. */
684 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
685 if (opcode <= 255)
687 *p++ = opcode;
688 goto done;
691 /* Try using DW_LNS_const_add_pc followed by special op. */
692 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
693 if (opcode <= 255)
695 *p++ = DW_LNS_const_add_pc;
696 *p++ = opcode;
697 goto done;
701 /* Otherwise use DW_LNS_advance_pc. */
702 *p++ = DW_LNS_advance_pc;
703 p += output_leb128 (p, addr_delta, 0);
705 if (need_copy)
706 *p++ = DW_LNS_copy;
707 else
708 *p++ = tmp;
710 done:
711 assert (p == end);
714 /* Handy routine to combine calls to the above two routines. */
716 static void
717 out_inc_line_addr (line_delta, addr_delta)
718 int line_delta;
719 addressT addr_delta;
721 int len = size_inc_line_addr (line_delta, addr_delta);
722 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
725 /* Generate a variant frag that we can use to relax address/line
726 increments between fragments of the target segment. */
728 static void
729 relax_inc_line_addr (line_delta, seg, to_frag, to_ofs, from_frag, from_ofs)
730 int line_delta;
731 segT seg;
732 fragS *to_frag, *from_frag;
733 addressT to_ofs, from_ofs;
735 symbolS *to_sym, *from_sym;
736 expressionS expr;
737 int max_chars;
739 to_sym = symbol_new (fake_label_name, seg, to_ofs, to_frag);
740 from_sym = symbol_new (fake_label_name, seg, from_ofs, from_frag);
742 expr.X_op = O_subtract;
743 expr.X_add_symbol = to_sym;
744 expr.X_op_symbol = from_sym;
745 expr.X_add_number = 0;
747 /* The maximum size of the frag is the line delta with a maximum
748 sized address delta. */
749 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
751 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
752 make_expr_symbol (&expr), line_delta, NULL);
755 /* The function estimates the size of a rs_dwarf2dbg variant frag
756 based on the current values of the symbols. It is called before
757 the relaxation loop. We set fr_subtype to the expected length. */
760 dwarf2dbg_estimate_size_before_relax (frag)
761 fragS *frag;
763 offsetT addr_delta;
764 int size;
766 addr_delta = resolve_symbol_value (frag->fr_symbol, 0);
767 size = size_inc_line_addr (frag->fr_offset, addr_delta);
769 frag->fr_subtype = size;
771 return size;
774 /* This function relaxes a rs_dwarf2dbg variant frag based on the
775 current values of the symbols. fr_subtype is the current length
776 of the frag. This returns the change in frag length. */
779 dwarf2dbg_relax_frag (frag)
780 fragS *frag;
782 int old_size, new_size;
784 old_size = frag->fr_subtype;
785 new_size = dwarf2dbg_estimate_size_before_relax (frag);
787 return new_size - old_size;
790 /* This function converts a rs_dwarf2dbg variant frag into a normal
791 fill frag. This is called after all relaxation has been done.
792 fr_subtype will be the desired length of the frag. */
794 void
795 dwarf2dbg_convert_frag (frag)
796 fragS *frag;
798 offsetT addr_diff;
800 addr_diff = resolve_symbol_value (frag->fr_symbol, 1);
802 /* fr_var carries the max_chars that we created the fragment with.
803 fr_subtype carries the current expected length. We must, of
804 course, have allocated enough memory earlier. */
805 assert (frag->fr_var >= (int) frag->fr_subtype);
807 emit_inc_line_addr (frag->fr_offset, addr_diff,
808 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
810 frag->fr_fix += frag->fr_subtype;
811 frag->fr_type = rs_fill;
812 frag->fr_var = 0;
813 frag->fr_offset = 0;
816 /* Generate .debug_line content for the chain of line number entries
817 beginning at E, for segment SEG. */
819 static void
820 process_entries (seg, e)
821 segT seg;
822 struct line_entry *e;
824 unsigned filenum = 1;
825 unsigned line = 1;
826 unsigned column = 0;
827 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_BEGIN_STMT : 0;
828 fragS *frag = NULL;
829 fragS *last_frag;
830 addressT frag_ofs = 0;
831 addressT last_frag_ofs;
832 struct line_entry *next;
834 while (e)
836 int changed = 0;
838 if (filenum != e->loc.filenum)
840 filenum = e->loc.filenum;
841 out_opcode (DW_LNS_set_file);
842 out_uleb128 (filenum);
843 changed = 1;
846 if (column != e->loc.column)
848 column = e->loc.column;
849 out_opcode (DW_LNS_set_column);
850 out_uleb128 (column);
851 changed = 1;
854 if ((e->loc.flags ^ flags) & DWARF2_FLAG_BEGIN_STMT)
856 flags = e->loc.flags;
857 out_opcode (DW_LNS_negate_stmt);
858 changed = 1;
861 if (e->loc.flags & DWARF2_FLAG_BEGIN_BLOCK)
863 out_opcode (DW_LNS_set_basic_block);
864 changed = 1;
867 if (line != e->loc.line || changed)
869 int line_delta = e->loc.line - line;
870 if (frag == NULL)
872 out_set_addr (seg, e->frag, e->frag_ofs);
873 out_inc_line_addr (line_delta, 0);
875 else if (frag == e->frag)
876 out_inc_line_addr (line_delta, e->frag_ofs - frag_ofs);
877 else
878 relax_inc_line_addr (line_delta, seg, e->frag, e->frag_ofs,
879 frag, frag_ofs);
881 frag = e->frag;
882 frag_ofs = e->frag_ofs;
883 line = e->loc.line;
885 else if (frag == NULL)
887 out_set_addr (seg, e->frag, e->frag_ofs);
888 frag = e->frag;
889 frag_ofs = e->frag_ofs;
892 next = e->next;
893 free (e);
894 e = next;
897 /* Emit a DW_LNE_end_sequence for the end of the section. */
898 last_frag = last_frag_for_seg (seg);
899 last_frag_ofs = get_frag_fix (last_frag);
900 if (frag == last_frag)
901 out_inc_line_addr (INT_MAX, last_frag_ofs - frag_ofs);
902 else
903 relax_inc_line_addr (INT_MAX, seg, last_frag, last_frag_ofs,
904 frag, frag_ofs);
907 /* Emit the directory and file tables for .debug_line. */
909 static void
910 out_file_list ()
912 size_t size;
913 char *cp;
914 unsigned int i;
916 /* Terminate directory list. */
917 out_byte ('\0');
919 for (i = 1; i < files_in_use; ++i)
921 size = strlen (files[i].filename) + 1;
922 cp = frag_more (size);
923 memcpy (cp, files[i].filename, size);
925 out_uleb128 (files[i].dir); /* directory number */
926 out_uleb128 (0); /* last modification timestamp */
927 out_uleb128 (0); /* filesize */
930 /* Terminate filename list. */
931 out_byte (0);
934 /* Emit the collected .debug_line data. */
936 static void
937 out_debug_line (line_seg)
938 segT line_seg;
940 expressionS expr;
941 symbolS *line_start;
942 symbolS *prologue_end;
943 symbolS *line_end;
944 struct line_seg *s;
946 subseg_set (line_seg, 0);
948 line_start = symbol_new_now ();
949 prologue_end = symbol_make (fake_label_name);
950 line_end = symbol_make (fake_label_name);
952 /* Total length of the information for this compilation unit. */
953 expr.X_op = O_subtract;
954 expr.X_add_symbol = line_end;
955 expr.X_op_symbol = line_start;
956 expr.X_add_number = -4;
957 emit_expr (&expr, 4);
959 /* Version. */
960 out_two (2);
962 /* Length of the prologue following this length. */
963 expr.X_op = O_subtract;
964 expr.X_add_symbol = prologue_end;
965 expr.X_op_symbol = line_start;
966 expr.X_add_number = - (4 + 2 + 4);
967 emit_expr (&expr, 4);
969 /* Parameters of the state machine. */
970 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
971 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
972 out_byte (DWARF2_LINE_BASE);
973 out_byte (DWARF2_LINE_RANGE);
974 out_byte (DWARF2_LINE_OPCODE_BASE);
976 /* Standard opcode lengths. */
977 out_byte (0); /* DW_LNS_copy */
978 out_byte (1); /* DW_LNS_advance_pc */
979 out_byte (1); /* DW_LNS_advance_line */
980 out_byte (1); /* DW_LNS_set_file */
981 out_byte (1); /* DW_LNS_set_column */
982 out_byte (0); /* DW_LNS_negate_stmt */
983 out_byte (0); /* DW_LNS_set_basic_block */
984 out_byte (0); /* DW_LNS_const_add_pc */
985 out_byte (1); /* DW_LNS_fixed_advance_pc */
987 out_file_list ();
989 set_symbol_value_now (prologue_end);
991 /* For each section, emit a statement program. */
992 for (s = all_segs; s ; s = s->next)
993 process_entries (s->seg, s->head->head);
995 set_symbol_value_now (line_end);
998 /* Emit data for .debug_aranges. */
1000 static void
1001 out_debug_aranges (aranges_seg, info_seg)
1002 segT aranges_seg;
1003 segT info_seg;
1005 unsigned int addr_size = sizeof_address;
1006 addressT size, skip;
1007 struct line_seg *s;
1008 expressionS expr;
1009 char *p;
1011 size = 4 + 2 + 4 + 1 + 1;
1013 skip = 2*addr_size - (size & (2*addr_size - 1));
1014 if (skip == 2*addr_size)
1015 skip = 0;
1016 size += skip;
1018 for (s = all_segs; s ; s = s->next)
1019 size += 2*addr_size;
1021 size += 2*addr_size;
1023 subseg_set (aranges_seg, 0);
1025 /* Length of the compilation unit. */
1026 out_four (size - 4);
1028 /* Version. */
1029 out_two (2);
1031 /* Offset to .debug_info. */
1032 expr.X_op = O_symbol;
1033 expr.X_add_symbol = section_symbol (info_seg);
1034 expr.X_add_number = 0;
1035 emit_expr (&expr, 4);
1037 /* Size of an address (offset portion). */
1038 out_byte (addr_size);
1040 /* Size of a segment descriptor. */
1041 out_byte (0);
1043 /* Align the header. */
1044 if (skip)
1045 frag_align (ffs (2*addr_size) - 1, 0, 0);
1047 for (s = all_segs; s ; s = s->next)
1049 fragS *frag;
1050 symbolS *beg, *end;
1052 frag = first_frag_for_seg (s->seg);
1053 beg = symbol_new (fake_label_name, s->seg, 0, frag);
1054 s->text_start = beg;
1056 frag = last_frag_for_seg (s->seg);
1057 end = symbol_new (fake_label_name, s->seg, get_frag_fix (frag), frag);
1058 s->text_end = end;
1060 expr.X_op = O_symbol;
1061 expr.X_add_symbol = beg;
1062 expr.X_add_number = 0;
1063 emit_expr (&expr, addr_size);
1065 expr.X_op = O_subtract;
1066 expr.X_add_symbol = end;
1067 expr.X_op_symbol = beg;
1068 expr.X_add_number = 0;
1069 emit_expr (&expr, addr_size);
1072 p = frag_more (2 * addr_size);
1073 md_number_to_chars (p, 0, addr_size);
1074 md_number_to_chars (p + addr_size, 0, addr_size);
1077 /* Emit data for .debug_abbrev. Note that this must be kept in
1078 sync with out_debug_info below. */
1080 static void
1081 out_debug_abbrev (abbrev_seg)
1082 segT abbrev_seg;
1084 subseg_set (abbrev_seg, 0);
1086 out_uleb128 (1);
1087 out_uleb128 (DW_TAG_compile_unit);
1088 out_byte (DW_CHILDREN_no);
1089 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1090 if (all_segs->next == NULL)
1092 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1093 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1095 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1096 out_abbrev (DW_AT_producer, DW_FORM_string);
1097 out_abbrev (DW_AT_language, DW_FORM_data2);
1098 out_abbrev (0, 0);
1101 /* Emit a description of this compilation unit for .debug_info. */
1103 static void
1104 out_debug_info (info_seg, abbrev_seg, line_seg)
1105 segT info_seg;
1106 segT abbrev_seg;
1107 segT line_seg;
1109 char producer[128];
1110 char *comp_dir;
1111 expressionS expr;
1112 symbolS *info_start;
1113 symbolS *info_end;
1114 char *p;
1115 int len;
1117 subseg_set (info_seg, 0);
1119 info_start = symbol_new_now();
1120 info_end = symbol_make (fake_label_name);
1122 /* Compilation Unit length. */
1123 expr.X_op = O_subtract;
1124 expr.X_add_symbol = info_end;
1125 expr.X_op_symbol = info_start;
1126 expr.X_add_number = -4;
1127 emit_expr (&expr, 4);
1129 /* DWARF version. */
1130 out_two (2);
1132 /* .debug_abbrev offset */
1133 expr.X_op = O_symbol;
1134 expr.X_add_symbol = section_symbol (abbrev_seg);
1135 expr.X_add_number = 0;
1136 emit_expr (&expr, 4);
1138 /* Target address size. */
1139 out_byte (sizeof_address);
1141 /* DW_TAG_compile_unit DIE abbrev */
1142 out_uleb128 (1);
1144 /* DW_AT_stmt_list */
1145 expr.X_op = O_symbol;
1146 expr.X_add_symbol = section_symbol (line_seg);
1147 expr.X_add_number = 0;
1148 emit_expr (&expr, 4);
1150 /* These two attributes may only be emitted if all of the code is
1151 contiguous. Multiple sections are not that. */
1152 if (all_segs->next == NULL)
1154 /* DW_AT_low_pc */
1155 expr.X_op = O_symbol;
1156 expr.X_add_symbol = all_segs->text_start;
1157 expr.X_add_number = 0;
1158 emit_expr (&expr, sizeof_address);
1160 /* DW_AT_high_pc */
1161 expr.X_op = O_symbol;
1162 expr.X_add_symbol = all_segs->text_end;
1163 expr.X_add_number = 0;
1164 emit_expr (&expr, sizeof_address);
1167 /* DW_AT_comp_dir */
1168 comp_dir = getpwd ();
1169 len = strlen (comp_dir) + 1;
1170 p = frag_more (len);
1171 memcpy (p, comp_dir, len);
1173 /* DW_AT_producer */
1174 sprintf (producer, "GNU AS %s", VERSION);
1175 len = strlen (producer) + 1;
1176 p = frag_more (len);
1177 memcpy (p, producer, len);
1179 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1180 dwarf2 draft has no standard code for assembler. */
1181 out_two (DW_LANG_Mips_Assembler);
1183 set_symbol_value_now (info_end);
1186 void
1187 dwarf2_finish ()
1189 segT line_seg;
1190 struct line_seg *s;
1192 /* If no debug information was recorded, nothing to do. */
1193 if (all_segs == NULL)
1194 return;
1196 /* Calculate the size of an address for the target machine. */
1197 #ifdef BFD_ASSEMBLER
1198 sizeof_address = bfd_arch_bits_per_address (stdoutput) / 8;
1199 #else
1200 /* FIXME. */
1201 sizeof_address = 4;
1202 #endif
1204 /* Create and switch to the line number section. */
1205 line_seg = subseg_new (".debug_line", 0);
1206 #ifdef BFD_ASSEMBLER
1207 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY);
1208 #endif
1210 /* For each subsection, chain the debug entries together. */
1211 for (s = all_segs; s ; s = s->next)
1213 struct line_subseg *ss = s->head;
1214 struct line_entry **ptail = ss->ptail;
1216 while ((ss = ss->next) != NULL)
1218 *ptail = ss->head;
1219 ptail = ss->ptail;
1223 out_debug_line (line_seg);
1225 /* If this is assembler generated line info, we need .debug_info
1226 and .debug_abbrev sections as well. */
1227 if (debug_type == DEBUG_DWARF2)
1229 segT abbrev_seg;
1230 segT info_seg;
1231 segT aranges_seg;
1233 info_seg = subseg_new (".debug_info", 0);
1234 abbrev_seg = subseg_new (".debug_abbrev", 0);
1235 aranges_seg = subseg_new (".debug_aranges", 0);
1237 #ifdef BFD_ASSEMBLER
1238 bfd_set_section_flags (stdoutput, info_seg, SEC_READONLY);
1239 bfd_set_section_flags (stdoutput, abbrev_seg, SEC_READONLY);
1240 bfd_set_section_flags (stdoutput, aranges_seg, SEC_READONLY);
1241 #endif
1243 record_alignment (aranges_seg, ffs (2*sizeof_address) - 1);
1245 out_debug_aranges (aranges_seg, info_seg);
1246 out_debug_abbrev (abbrev_seg);
1247 out_debug_info (info_seg, abbrev_seg, line_seg);
1251 #else
1252 void
1253 dwarf2_finish ()
1258 dwarf2dbg_estimate_size_before_relax (frag)
1259 fragS *frag ATTRIBUTE_UNUSED;
1261 as_fatal (_("dwarf2 is not supported for this object file format"));
1262 return 0;
1266 dwarf2dbg_relax_frag (frag)
1267 fragS *frag ATTRIBUTE_UNUSED;
1269 as_fatal (_("dwarf2 is not supported for this object file format"));
1270 return 0;
1273 void
1274 dwarf2dbg_convert_frag (frag)
1275 fragS *frag ATTRIBUTE_UNUSED;
1277 as_fatal (_("dwarf2 is not supported for this object file format"));
1280 void
1281 dwarf2_emit_insn (size)
1282 int size ATTRIBUTE_UNUSED;
1286 void
1287 dwarf2_directive_file (dummy)
1288 int dummy ATTRIBUTE_UNUSED;
1290 as_fatal (_("dwarf2 is not supported for this object file format"));
1293 void
1294 dwarf2_directive_loc (dummy)
1295 int dummy ATTRIBUTE_UNUSED;
1297 as_fatal (_("dwarf2 is not supported for this object file format"));
1299 #endif /* BFD_ASSEMBLER */