Add outputting_stabs_line_debug varaible and D10v code to use it
[external/binutils.git] / gas / dwarf2dbg.c
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>
4
5    This file is part of GAS, the GNU Assembler.
6
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.
11
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.
16
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.  */
21
22 /* Logical line numbers can be controlled by the compiler via the
23    following two directives:
24
25         .file FILENO "file.c"
26         .loc  FILENO LINENO [COLUMN]
27
28    FILENO is the filenumber.  */
29
30 #include "ansidecl.h"
31 #include <limits.h>
32
33 #include "as.h"
34 #include "dwarf2dbg.h"
35 #include "subsegs.h"
36
37 #include "elf/dwarf2.h"
38
39 /* Since we can't generate the prolog until the body is complete, we
40    use three different subsegments for .debug_line: one holding the
41    prolog, one for the directory and filename info, and one for the
42    body ("statement program").  */
43 #define DL_PROLOG       0
44 #define DL_FILES        1
45 #define DL_BODY         2
46
47 /* First special line opcde - leave room for the standard opcodes.
48    Note: If you want to change this, you'll have to update the
49    "standard_opcode_lengths" table that is emitted below in
50    dwarf2_finish().  */
51 #define DWARF2_LINE_OPCODE_BASE         10
52
53 #ifndef DWARF2_LINE_BASE
54   /* Minimum line offset in a special line info. opcode.  This value
55      was chosen to give a reasonable range of values.  */
56 # define DWARF2_LINE_BASE               -5
57 #endif
58
59 /* Range of line offsets in a special line info. opcode.  */
60 #ifndef DWARF2_LINE_RANGE
61 # define DWARF2_LINE_RANGE              14
62 #endif
63
64 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
65   /* Define the architecture-dependent minimum instruction length (in
66      bytes).  This value should be rather too small than too big.  */
67 # define DWARF2_LINE_MIN_INSN_LENGTH    1
68 #endif
69
70 /* Flag that indicates the initial value of the is_stmt_start flag.
71    In the present implementation, we do not mark any lines as
72    the beginning of a source statement, because that information
73    is not made available by the GCC front-end.  */
74 #define DWARF2_LINE_DEFAULT_IS_STMT     1
75
76 /* Given a special op, return the line skip amount.  */
77 #define SPECIAL_LINE(op) \
78         (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
79
80 /* Given a special op, return the address skip amount (in units of
81    DWARF2_LINE_MIN_INSN_LENGTH.  */
82 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
83
84 /* The maximum address skip amount that can be encoded with a special op.  */
85 #define MAX_SPECIAL_ADDR_DELTA          SPECIAL_ADDR(255)
86
87
88 struct line_entry
89 {
90   struct line_entry *next;
91   fragS *frag;
92   addressT frag_ofs;
93   struct dwarf2_line_info loc;
94 };
95
96 struct line_subseg
97 {
98   struct line_subseg *next;
99   subsegT subseg;
100   struct line_entry *head;
101   struct line_entry **ptail;
102 };
103
104 struct line_seg
105 {
106   struct line_seg *next;
107   segT seg;
108   struct line_subseg *head;
109   symbolS *text_start;
110   symbolS *text_end;
111 };
112
113 /* Collects data for all line table entries during assembly.  */
114 static struct line_seg *all_segs;
115
116 struct file_entry
117 {
118   char *filename;
119   unsigned int dir;
120 };
121
122 /* Table of files used by .debug_line.  */
123 static struct file_entry *files;
124 static unsigned int files_in_use;
125 static unsigned int files_allocated;
126
127 /* Correlate file numbers as given by the user in .file/.loc directives
128    with the file numbers used in the output debug info.  */
129 static unsigned int *user_filenum;
130 static unsigned int user_filenum_allocated;
131
132 /* True when we've seen a .loc directive recently.  Used to avoid
133    doing work when there's nothing to do.  */
134 static boolean loc_directive_seen;
135
136 /* Current location as indicated by the most recent .loc directive.  */
137 static struct dwarf2_line_info current;
138
139 /* Fake label name.  */
140 static char const fake_label_name[] = ".L0\001";
141
142 /* The size of an address on the target.  */
143 static unsigned int sizeof_address;
144 \f
145 static struct line_subseg *get_line_subseg PARAMS ((segT, subsegT));
146 static unsigned int get_filenum PARAMS ((const char *));
147 static struct frag *first_frag_for_seg PARAMS ((segT));
148 static struct frag *last_frag_for_seg PARAMS ((segT));
149 static void out_byte PARAMS ((int));
150 static void out_opcode PARAMS ((int));
151 static void out_two PARAMS ((int));
152 static void out_four PARAMS ((int));
153 static void out_abbrev PARAMS ((int, int));
154 static void out_uleb128 PARAMS ((addressT));
155 static symbolS *symbol_new_now PARAMS ((void));
156 static void set_symbol_value_now PARAMS ((symbolS *));
157 static offsetT get_frag_fix PARAMS ((fragS *));
158 static void out_set_addr PARAMS ((segT, fragS *, addressT));
159 static int size_inc_line_addr PARAMS ((int, addressT));
160 static void emit_inc_line_addr PARAMS ((int, addressT, char *, int));
161 static void out_inc_line_addr PARAMS ((int, addressT));
162 static void relax_inc_line_addr PARAMS ((int, segT, fragS *, addressT,
163                                          fragS *, addressT));
164 static void process_entries PARAMS ((segT, struct line_entry *));
165 static void out_file_list PARAMS ((void));
166 static void out_debug_line PARAMS ((segT));
167 static void out_debug_aranges PARAMS ((segT, segT));
168 static void out_debug_abbrev PARAMS ((segT));
169 static void out_debug_info PARAMS ((segT, segT, segT));
170 \f
171 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS.  */
172
173 static struct line_subseg *
174 get_line_subseg (seg, subseg)
175      segT seg;
176      subsegT subseg;
177 {
178   static segT last_seg;
179   static subsegT last_subseg;
180   static struct line_subseg *last_line_subseg;
181
182   struct line_seg *s;
183   struct line_subseg **pss, *ss;
184
185   if (seg == last_seg && subseg == last_subseg)
186     return last_line_subseg;
187
188   for (s = all_segs; s ; s = s->next)  
189     if (s->seg == seg)
190       goto found_seg;
191
192   s = (struct line_seg *) xmalloc (sizeof (*s));
193   s->next = all_segs;
194   s->seg = seg;
195   s->head = NULL;
196   all_segs = s;
197
198  found_seg:
199   for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
200     {
201       if (ss->subseg == subseg)
202         goto found_subseg;
203       if (ss->subseg > subseg)
204         break;
205     }
206
207   ss = (struct line_subseg *) xmalloc (sizeof (*ss));
208   ss->next = *pss;
209   ss->subseg = subseg;
210   ss->head = NULL;
211   ss->ptail = &ss->head;
212   *pss = ss;
213
214  found_subseg:
215   last_seg = seg;
216   last_subseg = subseg;
217   last_line_subseg = ss;
218
219   return ss;
220 }
221
222 /* Record an entry for LOC ocurring at OFS within the current fragment.  */
223
224 void
225 dwarf2_gen_line_info (ofs, loc)
226      addressT ofs;
227      struct dwarf2_line_info *loc;
228 {
229   struct line_subseg *ss;
230   struct line_entry *e;
231
232   /* Early out for as-yet incomplete location information.  */
233   if (loc->filenum == 0 || loc->line == 0)
234     return;
235
236   e = (struct line_entry *) xmalloc (sizeof (*e));
237   e->next = NULL;
238   e->frag = frag_now;
239   e->frag_ofs = ofs;
240   e->loc = *loc;
241
242   ss = get_line_subseg (now_seg, now_subseg);
243   *ss->ptail = e;
244   ss->ptail = &e->next;
245 }
246
247 void
248 dwarf2_where (line)
249      struct dwarf2_line_info *line;
250 {
251   if (debug_type == DEBUG_DWARF2)
252     {
253       char *filename;
254       as_where (&filename, &line->line);
255       line->filenum = get_filenum (filename);
256       line->column = 0;
257       line->flags = DWARF2_FLAG_BEGIN_STMT;
258     }
259   else
260     *line = current;
261 }
262
263 /* Called for each machine instruction, or relatively atomic group of
264    machine instructions (ie built-in macro).  The instruction or group
265    is SIZE bytes in length.  If dwarf2 line number generation is called
266    for, emit a line statement appropriately.  */
267
268 void
269 dwarf2_emit_insn (size)
270      int size;
271 {
272   struct dwarf2_line_info loc;
273
274   if (debug_type != DEBUG_DWARF2 && ! loc_directive_seen)
275     return;
276   loc_directive_seen = false;
277      
278   dwarf2_where (&loc);
279   dwarf2_gen_line_info (frag_now_fix () - size, &loc);
280 }
281
282 /* Get a .debug_line file number for FILENAME.  */
283
284 static unsigned int
285 get_filenum (filename)
286      const char *filename;
287 {
288   static unsigned int last_used;
289   unsigned int i;
290
291   if (last_used)
292     if (strcmp (filename, files[last_used].filename) == 0)
293       return last_used;
294
295   for (i = 1; i < files_in_use; ++i)
296     if (strcmp (filename, files[i].filename) == 0)
297       return i;
298
299   if (i >= files_allocated)
300     {
301       files_allocated = i + 32;
302       files = (struct file_entry *)
303         xrealloc (files, (i + 32) * sizeof(struct file_entry));
304     }
305
306   files[i].filename = xstrdup(filename);
307   files[i].dir = 0;
308   files_in_use = i + 1;
309   last_used = i;
310
311   return i;
312 }
313
314 /* Handle the .file directive.  */
315
316 void
317 dwarf2_directive_file (dummy)
318      int dummy ATTRIBUTE_UNUSED;
319 {
320   offsetT num;
321   const char *filename;
322   int filename_len;
323
324   /* Continue to accept a bare string and pass it off.  */
325   SKIP_WHITESPACE ();
326   if (*input_line_pointer == '"')
327     {
328       s_app_file (0);
329       return;
330     }
331
332   num = get_absolute_expression ();
333   filename = demand_copy_C_string (&filename_len);
334   demand_empty_rest_of_line ();
335
336   if (num < 0)
337     {
338       as_bad (_("File number less than zero"));
339       return;
340     }
341
342   if (num >= (int) user_filenum_allocated)
343     {
344       unsigned int old = user_filenum_allocated;
345
346       user_filenum_allocated = num + 16;
347       user_filenum = (unsigned int *)
348         xrealloc (user_filenum, (num + 16) * sizeof (unsigned int));
349
350       /* Zero the new memory.  */
351       memset (user_filenum + old, 0, (num + 16 - old) * sizeof(unsigned int));
352     }
353
354   user_filenum[num] = get_filenum (filename);
355 }
356
357 void
358 dwarf2_directive_loc (dummy)
359      int dummy ATTRIBUTE_UNUSED;
360 {
361   offsetT filenum, line, column;
362
363   filenum = get_absolute_expression ();
364   SKIP_WHITESPACE ();
365   line = get_absolute_expression ();
366   SKIP_WHITESPACE ();
367   column = get_absolute_expression ();
368   demand_empty_rest_of_line ();
369
370   if (filenum < 0)
371     {
372       as_bad (_("File number less than zero"));
373       return;
374     }
375   if (filenum >= (int) user_filenum_allocated
376       || user_filenum[filenum] == 0)
377     {
378       as_bad (_("Unassigned file number %ld"), (long) filenum);
379       return;
380     }
381
382   current.filenum = user_filenum[filenum];
383   current.line = line;
384   current.column = column;
385   current.flags = DWARF2_FLAG_BEGIN_STMT;
386
387   loc_directive_seen = true;
388
389 #ifndef NO_LISTING
390   if (listing)
391     listing_source_line (line);
392 #endif
393 }
394
395 \f
396 static struct frag *
397 first_frag_for_seg (seg)
398      segT seg;
399 {
400   frchainS *f, *first = NULL;
401
402   for (f = frchain_root; f ; f = f->frch_next)
403     if (f->frch_seg == seg
404         && (! first || first->frch_subseg > f->frch_subseg))
405       first = f;
406
407   return first ? first->frch_root : NULL;
408 }
409
410 static struct frag *
411 last_frag_for_seg (seg)
412      segT seg;
413 {
414   frchainS *f, *last = NULL;
415
416   for (f = frchain_root; f ; f = f->frch_next)
417     if (f->frch_seg == seg
418         && (! last || last->frch_subseg < f->frch_subseg))
419       last= f;
420
421   return last ? last->frch_last : NULL;
422 }
423 \f
424 /* Emit a single byte into the current segment.  */
425
426 static inline void
427 out_byte (byte)
428      int byte;
429 {
430   FRAG_APPEND_1_CHAR (byte);
431 }
432
433 /* Emit a statement program opcode into the current segment.  */
434
435 static inline void
436 out_opcode (opc)
437      int opc;
438 {
439   out_byte (opc);
440 }
441
442 /* Emit a two-byte word into the current segment.  */
443
444 static inline void
445 out_two (data)
446      int data;
447 {
448   md_number_to_chars (frag_more (2), data, 2);
449 }
450
451 /* Emit a four byte word into the current segment.  */
452
453 static inline void
454 out_four (data)
455      int data;
456 {
457   md_number_to_chars (frag_more (4), data, 4);
458 }
459
460 /* Emit an unsigned "little-endian base 128" number.  */
461
462 static void
463 out_uleb128 (value)
464      addressT value;
465 {
466   output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
467 }
468
469 /* Emit a tuple for .debug_abbrev.  */
470
471 static inline void
472 out_abbrev (name, form)
473      int name, form;
474 {
475   out_uleb128 (name);
476   out_uleb128 (form);
477 }
478
479 /* Create a new fake symbol whose value is the current position.  */
480
481 static symbolS *
482 symbol_new_now ()
483 {
484   return symbol_new (fake_label_name, now_seg, frag_now_fix (), frag_now);
485 }
486
487 /* Set the value of SYM to the current position in the current segment.  */
488
489 static void
490 set_symbol_value_now (sym)
491      symbolS *sym;
492 {
493   S_SET_SEGMENT (sym, now_seg);
494   S_SET_VALUE (sym, frag_now_fix ());
495   symbol_set_frag (sym, frag_now);
496 }
497
498 /* Get the size of a fragment.  */
499
500 static offsetT
501 get_frag_fix (frag)
502      fragS *frag;
503 {
504   frchainS *fr;
505
506   if (frag->fr_next)
507     return frag->fr_fix;
508
509   /* If a fragment is the last in the chain, special measures must be
510      taken to find its size before relaxation, since it may be pending
511      on some subsegment chain.  */
512   for (fr = frchain_root; fr ; fr = fr->frch_next)
513     if (fr->frch_last == frag)
514       {
515         return ((char *) obstack_next_free (&fr->frch_obstack)
516                 - frag->fr_literal);
517       }
518
519   abort ();
520 }
521
522 /* Set an absolute address (may result in a relocation entry).  */
523
524 static void
525 out_set_addr (seg, frag, ofs)
526      segT seg;
527      fragS *frag;
528      addressT ofs;
529 {
530   expressionS expr;
531   symbolS *sym;
532
533   sym = symbol_new (fake_label_name, seg, ofs, frag);
534
535   out_opcode (DW_LNS_extended_op);
536   out_uleb128 (sizeof_address + 1);
537
538   out_opcode (DW_LNE_set_address);
539   expr.X_op = O_symbol;
540   expr.X_add_symbol = sym;
541   expr.X_add_number = 0;
542   emit_expr (&expr, sizeof_address);
543 }
544
545 /* Encode a pair of line and address skips as efficiently as possible.
546    Note that the line skip is signed, whereas the address skip is unsigned.
547
548    The following two routines *must* be kept in sync.  This is
549    enforced by making emit_inc_line_addr abort if we do not emit
550    exactly the expected number of bytes.  */
551
552 static int
553 size_inc_line_addr (line_delta, addr_delta)
554      int line_delta;
555      addressT addr_delta;
556 {
557   unsigned int tmp, opcode;
558   int len = 0;
559
560   /* Scale the address delta by the minimum instruction length.  */
561 #if DWARF2_LINE_MIN_INSN_LENGTH > 1  
562   assert (addr_delta % DWARF2_LINE_MIN_INSN_LENGTH == 0);
563   addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
564 #endif
565
566   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
567      We cannot use special opcodes here, since we want the end_sequence
568      to emit the matrix entry.  */
569   if (line_delta == INT_MAX)
570     {
571       if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
572         len = 1;
573       else
574         len = 1 + sizeof_leb128 (addr_delta, 0);
575       return len + 3;
576     }
577
578   /* Bias the line delta by the base.  */
579   tmp = line_delta - DWARF2_LINE_BASE;
580
581   /* If the line increment is out of range of a special opcode, we
582      must encode it with DW_LNS_advance_line.  */
583   if (tmp >= DWARF2_LINE_RANGE)
584     {
585       len = 1 + sizeof_leb128 (line_delta, 1);
586       line_delta = 0;
587       tmp = 0 - DWARF2_LINE_BASE;
588     }
589
590   /* Bias the opcode by the special opcode base.  */
591   tmp += DWARF2_LINE_OPCODE_BASE;
592
593   /* Avoid overflow when addr_delta is large.  */
594   if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
595     {
596       /* Try using a special opcode.  */
597       opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
598       if (opcode <= 255)
599         return len + 1;
600
601       /* Try using DW_LNS_const_add_pc followed by special op.  */
602       opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
603       if (opcode <= 255)
604         return len + 2;
605     }
606
607   /* Otherwise use DW_LNS_advance_pc.  */
608   len += 1 + sizeof_leb128 (addr_delta, 0);
609
610   /* DW_LNS_copy or special opcode.  */
611   len += 1;
612
613   return len;
614 }
615
616 static void
617 emit_inc_line_addr (line_delta, addr_delta, p, len)
618      int line_delta;
619      addressT addr_delta;
620      char *p;
621      int len;
622 {
623   unsigned int tmp, opcode;
624   int need_copy = 0;
625   char *end = p + len;
626
627 #if DWARF2_LINE_MIN_INSN_LENGTH > 1  
628   /* Scale the address delta by the minimum instruction length.  */
629   assert (addr_delta % DWARF2_LINE_MIN_INSN_LENGTH == 0);
630   addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
631 #endif
632   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
633      We cannot use special opcodes here, since we want the end_sequence
634      to emit the matrix entry.  */
635   if (line_delta == INT_MAX)
636     {
637       if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
638         *p++ = DW_LNS_const_add_pc;
639       else
640         {
641           *p++ = DW_LNS_advance_pc;
642           p += output_leb128 (p, addr_delta, 0);
643         }
644
645       *p++ = DW_LNS_extended_op;
646       *p++ = 1;
647       *p++ = DW_LNE_end_sequence;
648       goto done;
649     }
650
651   /* Bias the line delta by the base.  */
652   tmp = line_delta - DWARF2_LINE_BASE;
653
654   /* If the line increment is out of range of a special opcode, we
655      must encode it with DW_LNS_advance_line.  */
656   if (tmp >= DWARF2_LINE_RANGE)
657     {
658       *p++ = DW_LNS_advance_line;
659       p += output_leb128 (p, line_delta, 1);
660
661       /* Prettier, I think, to use DW_LNS_copy instead of a
662          "line +0, addr +0" special opcode.  */
663       if (addr_delta == 0)
664         {
665           *p++ = DW_LNS_copy;
666           goto done;
667         }
668
669       line_delta = 0;
670       tmp = 0 - DWARF2_LINE_BASE;
671       need_copy = 1;
672     }
673
674   /* Bias the opcode by the special opcode base.  */
675   tmp += DWARF2_LINE_OPCODE_BASE;
676
677   /* Avoid overflow when addr_delta is large.  */
678   if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
679     {
680       /* Try using a special opcode.  */
681       opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
682       if (opcode <= 255)
683         {
684           *p++ = opcode;
685           goto done;
686         }
687
688       /* Try using DW_LNS_const_add_pc followed by special op.  */
689       opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
690       if (opcode <= 255)
691         {
692           *p++ = DW_LNS_const_add_pc;
693           *p++ = opcode;
694           goto done;
695         }
696     }
697
698   /* Otherwise use DW_LNS_advance_pc.  */
699   *p++ = DW_LNS_advance_pc;
700   p += output_leb128 (p, addr_delta, 0);
701
702   if (need_copy)
703     *p++ = DW_LNS_copy;
704   else
705     *p++ = tmp;
706
707  done:
708   assert (p == end);
709 }
710
711 /* Handy routine to combine calls to the above two routines.  */
712
713 static void
714 out_inc_line_addr (line_delta, addr_delta)
715      int line_delta;
716      addressT addr_delta;
717 {
718   int len = size_inc_line_addr (line_delta, addr_delta);
719   emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
720 }
721
722 /* Generate a variant frag that we can use to relax address/line
723    increments between fragments of the target segment.  */
724
725 static void
726 relax_inc_line_addr (line_delta, seg, to_frag, to_ofs, from_frag, from_ofs)
727      int line_delta;
728      segT seg;
729      fragS *to_frag, *from_frag;
730      addressT to_ofs, from_ofs;
731 {
732   symbolS *to_sym, *from_sym;
733   expressionS expr;
734   int max_chars;
735
736   to_sym = symbol_new (fake_label_name, seg, to_ofs, to_frag);
737   from_sym = symbol_new (fake_label_name, seg, from_ofs, from_frag);
738
739   expr.X_op = O_subtract;
740   expr.X_add_symbol = to_sym;
741   expr.X_op_symbol = from_sym;
742   expr.X_add_number = 0;
743
744   /* The maximum size of the frag is the line delta with a maximum
745      sized address delta.  */
746   max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
747
748   frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
749             make_expr_symbol (&expr), line_delta, NULL);
750 }
751
752 /* The function estimates the size of a rs_dwarf2dbg variant frag
753    based on the current values of the symbols.  It is called before
754    the relaxation loop.  We set fr_subtype to the expected length.  */
755
756 int
757 dwarf2dbg_estimate_size_before_relax (frag)
758      fragS *frag;
759 {
760   offsetT addr_delta;
761   int size;
762
763   addr_delta = resolve_symbol_value (frag->fr_symbol, 0);
764   size = size_inc_line_addr (frag->fr_offset, addr_delta);
765
766   frag->fr_subtype = size;
767
768   return size;
769 }
770
771 /* This function relaxes a rs_dwarf2dbg variant frag based on the
772    current values of the symbols.  fr_subtype is the current length
773    of the frag.  This returns the change in frag length.  */
774
775 int
776 dwarf2dbg_relax_frag (frag)
777      fragS *frag;
778 {
779   int old_size, new_size;
780
781   old_size = frag->fr_subtype;
782   new_size = dwarf2dbg_estimate_size_before_relax (frag);
783   
784   return new_size - old_size;
785 }
786
787 /* This function converts a rs_dwarf2dbg variant frag into a normal
788    fill frag.  This is called after all relaxation has been done.
789    fr_subtype will be the desired length of the frag.  */
790
791 void
792 dwarf2dbg_convert_frag (frag)
793      fragS *frag;
794 {
795   offsetT addr_diff;
796
797   addr_diff = resolve_symbol_value (frag->fr_symbol, 1);
798
799   /* fr_var carries the max_chars that we created the fragment with.
800      fr_subtype carries the current expected length.  We must, of
801      course, have allocated enough memory earlier.  */
802   assert (frag->fr_var >= (int) frag->fr_subtype);
803
804   emit_inc_line_addr (frag->fr_offset, addr_diff, 
805                       frag->fr_literal + frag->fr_fix, frag->fr_subtype);
806
807   frag->fr_fix += frag->fr_subtype;
808   frag->fr_type = rs_fill;
809   frag->fr_var = 0;
810   frag->fr_offset = 0;
811 }
812
813 /* Generate .debug_line content for the chain of line number entries
814    beginning at E, for segment SEG.  */
815
816 static void
817 process_entries (seg, e)
818      segT seg;
819      struct line_entry *e;
820 {
821   unsigned filenum = 1;
822   unsigned line = 1;
823   unsigned column = 0;
824   unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_BEGIN_STMT : 0;
825   fragS *frag = NULL;
826   fragS *last_frag;
827   addressT frag_ofs = 0;
828   addressT last_frag_ofs;
829   struct line_entry *next;
830
831   while (e)
832     {
833       int changed = 0;
834
835       if (filenum != e->loc.filenum)
836         {
837           filenum = e->loc.filenum;
838           out_opcode (DW_LNS_set_file);
839           out_uleb128 (filenum);
840           changed = 1;
841         }
842
843       if (column != e->loc.column)
844         {
845           column = e->loc.column;
846           out_opcode (DW_LNS_set_column);
847           out_uleb128 (column);
848           changed = 1;
849         }
850
851       if ((e->loc.flags ^ flags) & DWARF2_FLAG_BEGIN_STMT)
852         {
853           flags = e->loc.flags;
854           out_opcode (DW_LNS_negate_stmt);
855           changed = 1;
856         }
857
858       if (e->loc.flags & DWARF2_FLAG_BEGIN_BLOCK)
859         {
860           out_opcode (DW_LNS_set_basic_block);
861           changed = 1;
862         }
863
864       if (line != e->loc.line || changed)
865         {
866           int line_delta = e->loc.line - line;
867           if (frag == NULL)
868             {
869               out_set_addr (seg, e->frag, e->frag_ofs);
870               out_inc_line_addr (line_delta, 0);
871             }
872           else if (frag == e->frag)
873             out_inc_line_addr (line_delta, e->frag_ofs - frag_ofs);
874           else
875             relax_inc_line_addr (line_delta, seg, e->frag, e->frag_ofs,
876                                  frag, frag_ofs);
877
878           frag = e->frag;
879           frag_ofs = e->frag_ofs;
880           line = e->loc.line;
881         }
882       else if (frag == NULL)
883         {
884           out_set_addr (seg, e->frag, e->frag_ofs);
885           frag = e->frag;
886           frag_ofs = e->frag_ofs;
887         }
888
889       next = e->next;
890       free (e);
891       e = next;
892     }
893
894   /* Emit a DW_LNE_end_sequence for the end of the section.  */
895   last_frag = last_frag_for_seg (seg);
896   last_frag_ofs = get_frag_fix (last_frag);
897   if (frag == last_frag)
898     out_inc_line_addr (INT_MAX, last_frag_ofs - frag_ofs);
899   else
900     relax_inc_line_addr (INT_MAX, seg, last_frag, last_frag_ofs, 
901                          frag, frag_ofs);
902 }
903
904 /* Emit the directory and file tables for .debug_line.  */
905
906 static void
907 out_file_list ()
908 {
909   size_t size;
910   char *cp;
911   unsigned int i;
912
913   /* Terminate directory list.  */
914   out_byte ('\0');
915
916   for (i = 1; i < files_in_use; ++i)
917     {
918       size = strlen (files[i].filename) + 1;
919       cp = frag_more (size);
920       memcpy (cp, files[i].filename, size);
921
922       out_uleb128 (files[i].dir);       /* directory number */
923       out_uleb128 (0);                  /* last modification timestamp */
924       out_uleb128 (0);                  /* filesize */
925     }
926
927   /* Terminate filename list.  */
928   out_byte (0);
929 }
930
931 /* Emit the collected .debug_line data.  */
932
933 static void
934 out_debug_line (line_seg)
935      segT line_seg;
936 {
937   expressionS expr;
938   symbolS *line_start;
939   symbolS *prologue_end;
940   symbolS *line_end;
941   struct line_seg *s;
942
943   subseg_set (line_seg, 0);
944
945   line_start = symbol_new_now ();
946   prologue_end = symbol_make (fake_label_name);
947   line_end = symbol_make (fake_label_name);
948
949   /* Total length of the information for this compilation unit.  */
950   expr.X_op = O_subtract;
951   expr.X_add_symbol = line_end;
952   expr.X_op_symbol = line_start;
953   expr.X_add_number = -4;
954   emit_expr (&expr, 4);
955
956   /* Version.  */
957   out_two (2);
958
959   /* Length of the prologue following this length.  */
960   expr.X_op = O_subtract;
961   expr.X_add_symbol = prologue_end;
962   expr.X_op_symbol = line_start;
963   expr.X_add_number = - (4 + 2 + 4);
964   emit_expr (&expr, 4);
965
966   /* Parameters of the state machine.  */
967   out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
968   out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
969   out_byte (DWARF2_LINE_BASE);
970   out_byte (DWARF2_LINE_RANGE);
971   out_byte (DWARF2_LINE_OPCODE_BASE);
972
973   /* Standard opcode lengths.  */
974   out_byte (0);                 /* DW_LNS_copy */
975   out_byte (1);                 /* DW_LNS_advance_pc */
976   out_byte (1);                 /* DW_LNS_advance_line */
977   out_byte (1);                 /* DW_LNS_set_file */
978   out_byte (1);                 /* DW_LNS_set_column */
979   out_byte (0);                 /* DW_LNS_negate_stmt */
980   out_byte (0);                 /* DW_LNS_set_basic_block */
981   out_byte (0);                 /* DW_LNS_const_add_pc */
982   out_byte (1);                 /* DW_LNS_fixed_advance_pc */
983
984   out_file_list ();
985
986   set_symbol_value_now (prologue_end);
987
988   /* For each section, emit a statement program.  */
989   for (s = all_segs; s ; s = s->next)
990     process_entries (s->seg, s->head->head);
991
992   set_symbol_value_now (line_end);
993 }
994
995 /* Emit data for .debug_aranges.  */
996
997 static void
998 out_debug_aranges (aranges_seg, info_seg)
999      segT aranges_seg;
1000      segT info_seg;
1001 {
1002   unsigned int addr_size = sizeof_address;
1003   addressT size, skip;
1004   struct line_seg *s;
1005   expressionS expr;
1006   char *p;
1007
1008   size = 4 + 2 + 4 + 1 + 1;
1009
1010   skip = 2*addr_size - (size & (2*addr_size - 1));
1011   if (skip == 2*addr_size)
1012     skip = 0;
1013   size += skip;
1014
1015   for (s = all_segs; s ; s = s->next)
1016     size += 2*addr_size;
1017
1018   size += 2*addr_size;
1019
1020   subseg_set (aranges_seg, 0);
1021
1022   /* Length of the compilation unit.  */
1023   out_four (size - 4);
1024
1025   /* Version.  */
1026   out_two (2);
1027
1028   /* Offset to .debug_info.  */
1029   expr.X_op = O_symbol;
1030   expr.X_add_symbol = section_symbol (info_seg);
1031   expr.X_add_number = 0;
1032   emit_expr (&expr, 4);
1033
1034   /* Size of an address (offset portion).  */
1035   out_byte (addr_size);
1036
1037   /* Size of a segment descriptor.  */
1038   out_byte (0);
1039
1040   /* Align the header.  */
1041   if (skip)
1042     frag_align (ffs (2*addr_size) - 1, 0, 0);
1043
1044   for (s = all_segs; s ; s = s->next)
1045     {
1046       fragS *frag;
1047       symbolS *beg, *end;
1048
1049       frag = first_frag_for_seg (s->seg);
1050       beg = symbol_new (fake_label_name, s->seg, 0, frag);
1051       s->text_start = beg;
1052
1053       frag = last_frag_for_seg (s->seg);
1054       end = symbol_new (fake_label_name, s->seg, get_frag_fix (frag), frag);
1055       s->text_end = end;
1056
1057       expr.X_op = O_symbol;
1058       expr.X_add_symbol = beg;
1059       expr.X_add_number = 0;
1060       emit_expr (&expr, addr_size);
1061
1062       expr.X_op = O_subtract;
1063       expr.X_add_symbol = end;
1064       expr.X_op_symbol = beg;
1065       expr.X_add_number = 0;
1066       emit_expr (&expr, addr_size);
1067     }
1068
1069   p = frag_more (2 * addr_size);
1070   md_number_to_chars (p, 0, addr_size);
1071   md_number_to_chars (p + addr_size, 0, addr_size);
1072 }
1073
1074 /* Emit data for .debug_abbrev.  Note that this must be kept in
1075    sync with out_debug_info below.  */
1076
1077 static void
1078 out_debug_abbrev (abbrev_seg)
1079      segT abbrev_seg;
1080 {
1081   subseg_set (abbrev_seg, 0);
1082
1083   out_uleb128 (1);
1084   out_uleb128 (DW_TAG_compile_unit);
1085   out_byte (DW_CHILDREN_no);
1086   out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1087   if (all_segs->next == NULL)
1088     {
1089       out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1090       out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1091     }
1092   out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1093   out_abbrev (DW_AT_producer, DW_FORM_string);
1094   out_abbrev (DW_AT_language, DW_FORM_data2);
1095   out_abbrev (0, 0);
1096 }
1097
1098 /* Emit a description of this compilation unit for .debug_info.  */
1099
1100 static void
1101 out_debug_info (info_seg, abbrev_seg, line_seg)
1102      segT info_seg;
1103      segT abbrev_seg;
1104      segT line_seg;
1105 {
1106   char producer[128];
1107   char *comp_dir;
1108   expressionS expr;
1109   symbolS *info_start;
1110   symbolS *info_end;
1111   char *p;
1112   int len;
1113
1114   subseg_set (info_seg, 0);
1115
1116   info_start = symbol_new_now();
1117   info_end = symbol_make (fake_label_name);
1118
1119   /* Compilation Unit length.  */
1120   expr.X_op = O_subtract;
1121   expr.X_add_symbol = info_end;
1122   expr.X_op_symbol = info_start;
1123   expr.X_add_number = -4;
1124   emit_expr (&expr, 4);
1125
1126   /* DWARF version.  */
1127   out_two (2);
1128
1129   /* .debug_abbrev offset */
1130   expr.X_op = O_symbol;
1131   expr.X_add_symbol = section_symbol (abbrev_seg);
1132   expr.X_add_number = 0;
1133   emit_expr (&expr, 4);
1134
1135   /* Target address size.  */
1136   out_byte (sizeof_address);
1137
1138   /* DW_TAG_compile_unit DIE abbrev */
1139   out_uleb128 (1);
1140
1141   /* DW_AT_stmt_list */
1142   expr.X_op = O_symbol;
1143   expr.X_add_symbol = section_symbol (line_seg);
1144   expr.X_add_number = 0;
1145   emit_expr (&expr, 4);
1146
1147   /* These two attributes may only be emitted if all of the code is
1148      contiguous.  Multiple sections are not that.  */
1149   if (all_segs->next == NULL)
1150     {
1151       /* DW_AT_low_pc */
1152       expr.X_op = O_symbol;
1153       expr.X_add_symbol = all_segs->text_start;
1154       expr.X_add_number = 0;
1155       emit_expr (&expr, sizeof_address);
1156
1157       /* DW_AT_high_pc */
1158       expr.X_op = O_symbol;
1159       expr.X_add_symbol = all_segs->text_end;
1160       expr.X_add_number = 0;
1161       emit_expr (&expr, sizeof_address);
1162     }
1163
1164   /* DW_AT_comp_dir */
1165   comp_dir = getpwd ();
1166   len = strlen (comp_dir) + 1;
1167   p = frag_more (len);
1168   memcpy (p, comp_dir, len);
1169
1170   /* DW_AT_producer */
1171   sprintf (producer, "GNU AS %s", VERSION);
1172   len = strlen (producer) + 1;
1173   p = frag_more (len);
1174   memcpy (p, producer, len);
1175
1176   /* DW_AT_language.  Yes, this is probably not really MIPS, but the
1177      dwarf2 draft has no standard code for assembler.  */
1178   out_two (DW_LANG_Mips_Assembler);
1179
1180   set_symbol_value_now (info_end);
1181 }
1182
1183 void
1184 dwarf2_finish ()
1185 {
1186   segT line_seg;
1187   struct line_seg *s;
1188
1189   /* If no debug information was recorded, nothing to do.  */
1190   if (all_segs == NULL)
1191     return;
1192
1193   /* Calculate the size of an address for the target machine.  */
1194 #ifdef BFD_ASSEMBLER
1195   sizeof_address = bfd_arch_bits_per_address (stdoutput) / 8;
1196 #else
1197   /* FIXME.  */
1198   sizeof_address = 4;
1199 #endif
1200
1201   /* Create and switch to the line number section.  */
1202   line_seg = subseg_new (".debug_line", 0);
1203 #ifdef BFD_ASSEMBLER
1204   bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY);
1205 #endif
1206
1207   /* For each subsection, chain the debug entries together.  */
1208   for (s = all_segs; s ; s = s->next)
1209     {
1210       struct line_subseg *ss = s->head;
1211       struct line_entry **ptail = ss->ptail;
1212
1213       while ((ss = ss->next) != NULL)
1214         {
1215           *ptail = ss->head;
1216           ptail = ss->ptail;
1217         }
1218     }
1219
1220   out_debug_line (line_seg);
1221
1222   /* If this is assembler generated line info, we need .debug_info
1223      and .debug_abbrev sections as well.  */
1224   if (debug_type == DEBUG_DWARF2)
1225     {
1226       segT abbrev_seg;
1227       segT info_seg;
1228       segT aranges_seg;
1229
1230       info_seg = subseg_new (".debug_info", 0);
1231       abbrev_seg = subseg_new (".debug_abbrev", 0);
1232       aranges_seg = subseg_new (".debug_aranges", 0);
1233
1234 #ifdef BFD_ASSEMBLER
1235       bfd_set_section_flags (stdoutput, info_seg, SEC_READONLY);
1236       bfd_set_section_flags (stdoutput, abbrev_seg, SEC_READONLY);
1237       bfd_set_section_flags (stdoutput, aranges_seg, SEC_READONLY);
1238 #endif
1239
1240       record_alignment (aranges_seg, ffs (2*sizeof_address) - 1);
1241
1242       out_debug_aranges (aranges_seg, info_seg);
1243       out_debug_abbrev (abbrev_seg);
1244       out_debug_info (info_seg, abbrev_seg, line_seg);
1245     }
1246 }