* dwarf2dbg.c: Remove superfluous forward function declarations.
[platform/upstream/binutils.git] / gas / dwarf2dbg.c
1 /* dwarf2dbg.c - DWARF2 debug support
2    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3    Free Software Foundation, Inc.
4    Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5
6    This file is part of GAS, the GNU Assembler.
7
8    GAS is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GAS is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GAS; see the file COPYING.  If not, write to the Free
20    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21    02110-1301, USA.  */
22
23 /* Logical line numbers can be controlled by the compiler via the
24    following directives:
25
26         .file FILENO "file.c"
27         .loc  FILENO LINENO [COLUMN] [basic_block] [prologue_end] \
28               [epilogue_begin] [is_stmt VALUE] [isa VALUE]
29 */
30
31 #include "as.h"
32 #include "safe-ctype.h"
33
34 #ifdef HAVE_LIMITS_H
35 #include <limits.h>
36 #else
37 #ifdef HAVE_SYS_PARAM_H
38 #include <sys/param.h>
39 #endif
40 #ifndef INT_MAX
41 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
42 #endif
43 #endif
44
45 #include "dwarf2dbg.h"
46 #include <filenames.h>
47
48 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
49 /* We need to decide which character to use as a directory separator.
50    Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not
51    necessarily mean that the backslash character is the one to use.
52    Some environments, eg Cygwin, can support both naming conventions.
53    So we use the heuristic that we only need to use the backslash if
54    the path is an absolute path starting with a DOS style drive
55    selector.  eg C: or D:  */
56 # define INSERT_DIR_SEPARATOR(string, offset) \
57   do \
58     { \
59       if (offset > 1 \
60           && string[0] != 0 \
61           && string[1] == ':') \
62        string [offset] = '\\'; \
63       else \
64        string [offset] = '/'; \
65     } \
66   while (0)
67 #else
68 # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
69 #endif
70
71 #ifndef DWARF2_FORMAT
72 # define DWARF2_FORMAT(SEC) dwarf2_format_32bit
73 #endif
74
75 #ifndef DWARF2_ADDR_SIZE
76 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
77 #endif
78
79 #include "subsegs.h"
80
81 #include "elf/dwarf2.h"
82
83 /* Since we can't generate the prolog until the body is complete, we
84    use three different subsegments for .debug_line: one holding the
85    prolog, one for the directory and filename info, and one for the
86    body ("statement program").  */
87 #define DL_PROLOG       0
88 #define DL_FILES        1
89 #define DL_BODY         2
90
91 /* If linker relaxation might change offsets in the code, the DWARF special
92    opcodes and variable-length operands cannot be used.  If this macro is
93    nonzero, use the DW_LNS_fixed_advance_pc opcode instead.  */
94 #ifndef DWARF2_USE_FIXED_ADVANCE_PC
95 # define DWARF2_USE_FIXED_ADVANCE_PC    0
96 #endif
97
98 /* First special line opcde - leave room for the standard opcodes.
99    Note: If you want to change this, you'll have to update the
100    "standard_opcode_lengths" table that is emitted below in
101    out_debug_line().  */
102 #define DWARF2_LINE_OPCODE_BASE         13
103
104 #ifndef DWARF2_LINE_BASE
105   /* Minimum line offset in a special line info. opcode.  This value
106      was chosen to give a reasonable range of values.  */
107 # define DWARF2_LINE_BASE               -5
108 #endif
109
110 /* Range of line offsets in a special line info. opcode.  */
111 #ifndef DWARF2_LINE_RANGE
112 # define DWARF2_LINE_RANGE              14
113 #endif
114
115 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
116   /* Define the architecture-dependent minimum instruction length (in
117      bytes).  This value should be rather too small than too big.  */
118 # define DWARF2_LINE_MIN_INSN_LENGTH    1
119 #endif
120
121 /* Flag that indicates the initial value of the is_stmt_start flag.  */
122 #define DWARF2_LINE_DEFAULT_IS_STMT     1
123
124 /* Given a special op, return the line skip amount.  */
125 #define SPECIAL_LINE(op) \
126         (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
127
128 /* Given a special op, return the address skip amount (in units of
129    DWARF2_LINE_MIN_INSN_LENGTH.  */
130 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
131
132 /* The maximum address skip amount that can be encoded with a special op.  */
133 #define MAX_SPECIAL_ADDR_DELTA          SPECIAL_ADDR(255)
134
135 struct line_entry {
136   struct line_entry *next;
137   symbolS *label;
138   struct dwarf2_line_info loc;
139 };
140
141 struct line_subseg {
142   struct line_subseg *next;
143   subsegT subseg;
144   struct line_entry *head;
145   struct line_entry **ptail;
146 };
147
148 struct line_seg {
149   struct line_seg *next;
150   segT seg;
151   struct line_subseg *head;
152   symbolS *text_start;
153   symbolS *text_end;
154 };
155
156 /* Collects data for all line table entries during assembly.  */
157 static struct line_seg *all_segs;
158
159 struct file_entry {
160   const char *filename;
161   unsigned int dir;
162 };
163
164 /* Table of files used by .debug_line.  */
165 static struct file_entry *files;
166 static unsigned int files_in_use;
167 static unsigned int files_allocated;
168
169 /* Table of directories used by .debug_line.  */
170 static char **dirs;
171 static unsigned int dirs_in_use;
172 static unsigned int dirs_allocated;
173
174 /* TRUE when we've seen a .loc directive recently.  Used to avoid
175    doing work when there's nothing to do.  */
176 bfd_boolean dwarf2_loc_directive_seen;
177
178 /* TRUE when we're supposed to set the basic block mark whenever a
179    label is seen.  */
180 bfd_boolean dwarf2_loc_mark_labels;
181
182 /* Current location as indicated by the most recent .loc directive.  */
183 static struct dwarf2_line_info current = {
184   1, 1, 0, 0,
185   DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0
186 };
187
188 /* The size of an address on the target.  */
189 static unsigned int sizeof_address;
190 \f
191 static unsigned int get_filenum (const char *, unsigned int);
192
193 #ifndef TC_DWARF2_EMIT_OFFSET
194 #define TC_DWARF2_EMIT_OFFSET  generic_dwarf2_emit_offset
195
196 /* Create an offset to .dwarf2_*.  */
197
198 static void
199 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
200 {
201   expressionS expr;
202
203   expr.X_op = O_symbol;
204   expr.X_add_symbol = symbol;
205   expr.X_add_number = 0;
206   emit_expr (&expr, size);
207 }
208 #endif
209
210 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS.  */
211
212 static struct line_subseg *
213 get_line_subseg (segT seg, subsegT subseg)
214 {
215   static segT last_seg;
216   static subsegT last_subseg;
217   static struct line_subseg *last_line_subseg;
218
219   struct line_seg **ps, *s;
220   struct line_subseg **pss, *ss;
221
222   if (seg == last_seg && subseg == last_subseg)
223     return last_line_subseg;
224
225   for (ps = &all_segs; (s = *ps) != NULL; ps = &s->next)
226     if (s->seg == seg)
227       goto found_seg;
228
229   s = (struct line_seg *) xmalloc (sizeof (*s));
230   s->next = NULL;
231   s->seg = seg;
232   s->head = NULL;
233   *ps = s;
234
235  found_seg:
236   for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
237     {
238       if (ss->subseg == subseg)
239         goto found_subseg;
240       if (ss->subseg > subseg)
241         break;
242     }
243
244   ss = (struct line_subseg *) xmalloc (sizeof (*ss));
245   ss->next = *pss;
246   ss->subseg = subseg;
247   ss->head = NULL;
248   ss->ptail = &ss->head;
249   *pss = ss;
250
251  found_subseg:
252   last_seg = seg;
253   last_subseg = subseg;
254   last_line_subseg = ss;
255
256   return ss;
257 }
258
259 /* Record an entry for LOC occurring at LABEL.  */
260
261 static void
262 dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
263 {
264   struct line_subseg *ss;
265   struct line_entry *e;
266
267   e = (struct line_entry *) xmalloc (sizeof (*e));
268   e->next = NULL;
269   e->label = label;
270   e->loc = *loc;
271
272   ss = get_line_subseg (now_seg, now_subseg);
273   *ss->ptail = e;
274   ss->ptail = &e->next;
275 }
276
277 /* Record an entry for LOC occurring at OFS within the current fragment.  */
278
279 void
280 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
281 {
282   static unsigned int line = -1;
283   static unsigned int filenum = -1;
284
285   symbolS *sym;
286
287   /* Early out for as-yet incomplete location information.  */
288   if (loc->filenum == 0 || loc->line == 0)
289     return;
290
291   /* Don't emit sequences of line symbols for the same line when the
292      symbols apply to assembler code.  It is necessary to emit
293      duplicate line symbols when a compiler asks for them, because GDB
294      uses them to determine the end of the prologue.  */
295   if (debug_type == DEBUG_DWARF2
296       && line == loc->line && filenum == loc->filenum)
297     return;
298
299   line = loc->line;
300   filenum = loc->filenum;
301
302   sym = symbol_temp_new (now_seg, ofs, frag_now);
303   dwarf2_gen_line_info_1 (sym, loc);
304 }
305
306 /* Returns the current source information.  If .file directives have
307    been encountered, the info for the corresponding source file is
308    returned.  Otherwise, the info for the assembly source file is
309    returned.  */
310
311 void
312 dwarf2_where (struct dwarf2_line_info *line)
313 {
314   if (debug_type == DEBUG_DWARF2)
315     {
316       char *filename;
317       as_where (&filename, &line->line);
318       line->filenum = get_filenum (filename, 0);
319       line->column = 0;
320       line->flags = DWARF2_FLAG_IS_STMT;
321       line->isa = current.isa;
322     }
323   else
324     *line = current;
325 }
326
327 /* A hook to allow the target backend to inform the line number state
328    machine of isa changes when assembler debug info is enabled.  */
329
330 void
331 dwarf2_set_isa (unsigned int isa)
332 {
333   current.isa = isa;
334 }
335
336 /* Called for each machine instruction, or relatively atomic group of
337    machine instructions (ie built-in macro).  The instruction or group
338    is SIZE bytes in length.  If dwarf2 line number generation is called
339    for, emit a line statement appropriately.  */
340
341 void
342 dwarf2_emit_insn (int size)
343 {
344   struct dwarf2_line_info loc;
345
346   if (!dwarf2_loc_directive_seen && debug_type != DEBUG_DWARF2)
347     return;
348
349   dwarf2_where (&loc);
350
351   dwarf2_gen_line_info (frag_now_fix () - size, &loc);
352   dwarf2_consume_line_info ();
353 }
354
355 /* Called after the current line information has been either used with
356    dwarf2_gen_line_info or saved with a machine instruction for later use.
357    This resets the state of the line number information to reflect that
358    it has been used.  */
359
360 void
361 dwarf2_consume_line_info (void)
362 {
363   /* Unless we generate DWARF2 debugging information for each
364      assembler line, we only emit one line symbol for one LOC.  */
365   dwarf2_loc_directive_seen = FALSE;
366
367   current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
368                      | DWARF2_FLAG_PROLOGUE_END
369                      | DWARF2_FLAG_EPILOGUE_BEGIN);
370 }
371
372 /* Called for each (preferably code) label.  If dwarf2_loc_mark_labels
373    is enabled, emit a basic block marker.  */
374
375 void
376 dwarf2_emit_label (symbolS *label)
377 {
378   struct dwarf2_line_info loc;
379
380   if (!dwarf2_loc_mark_labels)
381     return;
382   if (S_GET_SEGMENT (label) != now_seg)
383     return;
384   if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE))
385     return;
386   if (files_in_use == 0 && debug_type != DEBUG_DWARF2)
387     return;
388
389   dwarf2_where (&loc);
390
391   loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
392
393   dwarf2_gen_line_info_1 (label, &loc);
394   dwarf2_consume_line_info ();
395 }
396
397 /* Get a .debug_line file number for FILENAME.  If NUM is nonzero,
398    allocate it on that file table slot, otherwise return the first
399    empty one.  */
400
401 static unsigned int
402 get_filenum (const char *filename, unsigned int num)
403 {
404   static unsigned int last_used, last_used_dir_len;
405   const char *file;
406   size_t dir_len;
407   unsigned int i, dir;
408
409   if (num == 0 && last_used)
410     {
411       if (! files[last_used].dir
412           && strcmp (filename, files[last_used].filename) == 0)
413         return last_used;
414       if (files[last_used].dir
415           && strncmp (filename, dirs[files[last_used].dir],
416                       last_used_dir_len) == 0
417           && IS_DIR_SEPARATOR (filename [last_used_dir_len])
418           && strcmp (filename + last_used_dir_len + 1,
419                      files[last_used].filename) == 0)
420         return last_used;
421     }
422
423   file = lbasename (filename);
424   /* Don't make empty string from / or A: from A:/ .  */
425 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
426   if (file <= filename + 3)
427     file = filename;
428 #else
429   if (file == filename + 1)
430     file = filename;
431 #endif
432   dir_len = file - filename;
433
434   dir = 0;
435   if (dir_len)
436     {
437       --dir_len;
438       for (dir = 1; dir < dirs_in_use; ++dir)
439         if (strncmp (filename, dirs[dir], dir_len) == 0
440             && dirs[dir][dir_len] == '\0')
441           break;
442
443       if (dir >= dirs_in_use)
444         {
445           if (dir >= dirs_allocated)
446             {
447               dirs_allocated = dir + 32;
448               dirs = (char **)
449                      xrealloc (dirs, (dir + 32) * sizeof (const char *));
450             }
451
452           dirs[dir] = xmalloc (dir_len + 1);
453           memcpy (dirs[dir], filename, dir_len);
454           dirs[dir][dir_len] = '\0';
455           dirs_in_use = dir + 1;
456         }
457     }
458
459   if (num == 0)
460     {
461       for (i = 1; i < files_in_use; ++i)
462         if (files[i].dir == dir
463             && files[i].filename
464             && strcmp (file, files[i].filename) == 0)
465           {
466             last_used = i;
467             last_used_dir_len = dir_len;
468             return i;
469           }
470     }
471   else
472     i = num;
473
474   if (i >= files_allocated)
475     {
476       unsigned int old = files_allocated;
477
478       files_allocated = i + 32;
479       files = (struct file_entry *)
480         xrealloc (files, (i + 32) * sizeof (struct file_entry));
481
482       memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
483     }
484
485   files[i].filename = num ? file : xstrdup (file);
486   files[i].dir = dir;
487   if (files_in_use < i + 1)
488     files_in_use = i + 1;
489   last_used = i;
490   last_used_dir_len = dir_len;
491
492   return i;
493 }
494
495 /* Handle two forms of .file directive:
496    - Pass .file "source.c" to s_app_file
497    - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
498
499    If an entry is added to the file table, return a pointer to the filename. */
500
501 char *
502 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
503 {
504   offsetT num;
505   char *filename;
506   int filename_len;
507
508   /* Continue to accept a bare string and pass it off.  */
509   SKIP_WHITESPACE ();
510   if (*input_line_pointer == '"')
511     {
512       s_app_file (0);
513       return NULL;
514     }
515
516   num = get_absolute_expression ();
517   filename = demand_copy_C_string (&filename_len);
518   if (filename == NULL)
519     return NULL;
520   demand_empty_rest_of_line ();
521
522   if (num < 1)
523     {
524       as_bad (_("file number less than one"));
525       return NULL;
526     }
527
528   if (num < (int) files_in_use && files[num].filename != 0)
529     {
530       as_bad (_("file number %ld already allocated"), (long) num);
531       return NULL;
532     }
533
534   get_filenum (filename, num);
535
536   return filename;
537 }
538
539 void
540 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
541 {
542   offsetT filenum, line;
543
544   /* If we see two .loc directives in a row, force the first one to be
545      output now.  */
546   if (dwarf2_loc_directive_seen && debug_type != DEBUG_DWARF2)
547     dwarf2_emit_insn (0);
548
549   filenum = get_absolute_expression ();
550   SKIP_WHITESPACE ();
551   line = get_absolute_expression ();
552
553   if (filenum < 1)
554     {
555       as_bad (_("file number less than one"));
556       return;
557     }
558   if (filenum >= (int) files_in_use || files[filenum].filename == 0)
559     {
560       as_bad (_("unassigned file number %ld"), (long) filenum);
561       return;
562     }
563
564   current.filenum = filenum;
565   current.line = line;
566
567 #ifndef NO_LISTING
568   if (listing)
569     {
570       if (files[filenum].dir)
571         {
572           size_t dir_len = strlen (dirs[files[filenum].dir]);
573           size_t file_len = strlen (files[filenum].filename);
574           char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
575
576           memcpy (cp, dirs[files[filenum].dir], dir_len);
577           INSERT_DIR_SEPARATOR (cp, dir_len);
578           memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
579           cp[dir_len + file_len + 1] = '\0';
580           listing_source_file (cp);
581         }
582       else
583         listing_source_file (files[filenum].filename);
584       listing_source_line (line);
585     }
586 #endif
587
588   SKIP_WHITESPACE ();
589   if (ISDIGIT (*input_line_pointer))
590     {
591       current.column = get_absolute_expression ();
592       SKIP_WHITESPACE ();
593     }
594
595   while (ISALPHA (*input_line_pointer))
596     {
597       char *p, c;
598       offsetT value;
599
600       p = input_line_pointer;
601       c = get_symbol_end ();
602
603       if (strcmp (p, "basic_block") == 0)
604         {
605           current.flags |= DWARF2_FLAG_BASIC_BLOCK;
606           *input_line_pointer = c;
607         }
608       else if (strcmp (p, "prologue_end") == 0)
609         {
610           current.flags |= DWARF2_FLAG_PROLOGUE_END;
611           *input_line_pointer = c;
612         }
613       else if (strcmp (p, "epilogue_begin") == 0)
614         {
615           current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
616           *input_line_pointer = c;
617         }
618       else if (strcmp (p, "is_stmt") == 0)
619         {
620           *input_line_pointer = c;
621           value = get_absolute_expression ();
622           if (value == 0)
623             current.flags &= ~DWARF2_FLAG_IS_STMT;
624           else if (value == 1)
625             current.flags |= DWARF2_FLAG_IS_STMT;
626           else
627             {
628               as_bad (_("is_stmt value not 0 or 1"));
629               return;
630             }
631         }
632       else if (strcmp (p, "isa") == 0)
633         {
634           *input_line_pointer = c;
635           value = get_absolute_expression ();
636           if (value >= 0)
637             current.isa = value;
638           else
639             {
640               as_bad (_("isa number less than zero"));
641               return;
642             }
643         }
644       else
645         {
646           as_bad (_("unknown .loc sub-directive `%s'"), p);
647           *input_line_pointer = c;
648           return;
649         }
650
651       SKIP_WHITESPACE ();
652     }
653
654   demand_empty_rest_of_line ();
655   dwarf2_loc_directive_seen = TRUE;
656 }
657
658 void
659 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
660 {
661   offsetT value = get_absolute_expression ();
662
663   if (value != 0 && value != 1)
664     {
665       as_bad (_("expected 0 or 1"));
666       ignore_rest_of_line ();
667     }
668   else
669     {
670       dwarf2_loc_mark_labels = value != 0;
671       demand_empty_rest_of_line ();
672     }
673 }
674 \f
675 static struct frag *
676 first_frag_for_seg (segT seg)
677 {
678   return seg_info (seg)->frchainP->frch_root;
679 }
680
681 static struct frag *
682 last_frag_for_seg (segT seg)
683 {
684   frchainS *f = seg_info (seg)->frchainP;
685
686   while (f->frch_next != NULL)
687     f = f->frch_next;
688
689   return f->frch_last;
690 }
691 \f
692 /* Emit a single byte into the current segment.  */
693
694 static inline void
695 out_byte (int byte)
696 {
697   FRAG_APPEND_1_CHAR (byte);
698 }
699
700 /* Emit a statement program opcode into the current segment.  */
701
702 static inline void
703 out_opcode (int opc)
704 {
705   out_byte (opc);
706 }
707
708 /* Emit a two-byte word into the current segment.  */
709
710 static inline void
711 out_two (int data)
712 {
713   md_number_to_chars (frag_more (2), data, 2);
714 }
715
716 /* Emit a four byte word into the current segment.  */
717
718 static inline void
719 out_four (int data)
720 {
721   md_number_to_chars (frag_more (4), data, 4);
722 }
723
724 /* Emit an unsigned "little-endian base 128" number.  */
725
726 static void
727 out_uleb128 (addressT value)
728 {
729   output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
730 }
731
732 /* Emit a tuple for .debug_abbrev.  */
733
734 static inline void
735 out_abbrev (int name, int form)
736 {
737   out_uleb128 (name);
738   out_uleb128 (form);
739 }
740
741 /* Get the size of a fragment.  */
742
743 static offsetT
744 get_frag_fix (fragS *frag, segT seg)
745 {
746   frchainS *fr;
747
748   if (frag->fr_next)
749     return frag->fr_fix;
750
751   /* If a fragment is the last in the chain, special measures must be
752      taken to find its size before relaxation, since it may be pending
753      on some subsegment chain.  */
754   for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
755     if (fr->frch_last == frag)
756       return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
757
758   abort ();
759 }
760
761 /* Set an absolute address (may result in a relocation entry).  */
762
763 static void
764 out_set_addr (symbolS *sym)
765 {
766   expressionS expr;
767
768   out_opcode (DW_LNS_extended_op);
769   out_uleb128 (sizeof_address + 1);
770
771   out_opcode (DW_LNE_set_address);
772   expr.X_op = O_symbol;
773   expr.X_add_symbol = sym;
774   expr.X_add_number = 0;
775   emit_expr (&expr, sizeof_address);
776 }
777
778 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
779 static void scale_addr_delta (addressT *);
780
781 static void
782 scale_addr_delta (addressT *addr_delta)
783 {
784   static int printed_this = 0;
785   if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
786     {
787       if (!printed_this)
788         as_bad("unaligned opcodes detected in executable segment");
789       printed_this = 1;
790     }
791   *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
792 }
793 #else
794 #define scale_addr_delta(A)
795 #endif
796
797 /* Encode a pair of line and address skips as efficiently as possible.
798    Note that the line skip is signed, whereas the address skip is unsigned.
799
800    The following two routines *must* be kept in sync.  This is
801    enforced by making emit_inc_line_addr abort if we do not emit
802    exactly the expected number of bytes.  */
803
804 static int
805 size_inc_line_addr (int line_delta, addressT addr_delta)
806 {
807   unsigned int tmp, opcode;
808   int len = 0;
809
810   /* Scale the address delta by the minimum instruction length.  */
811   scale_addr_delta (&addr_delta);
812
813   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
814      We cannot use special opcodes here, since we want the end_sequence
815      to emit the matrix entry.  */
816   if (line_delta == INT_MAX)
817     {
818       if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
819         len = 1;
820       else
821         len = 1 + sizeof_leb128 (addr_delta, 0);
822       return len + 3;
823     }
824
825   /* Bias the line delta by the base.  */
826   tmp = line_delta - DWARF2_LINE_BASE;
827
828   /* If the line increment is out of range of a special opcode, we
829      must encode it with DW_LNS_advance_line.  */
830   if (tmp >= DWARF2_LINE_RANGE)
831     {
832       len = 1 + sizeof_leb128 (line_delta, 1);
833       line_delta = 0;
834       tmp = 0 - DWARF2_LINE_BASE;
835     }
836
837   /* Bias the opcode by the special opcode base.  */
838   tmp += DWARF2_LINE_OPCODE_BASE;
839
840   /* Avoid overflow when addr_delta is large.  */
841   if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
842     {
843       /* Try using a special opcode.  */
844       opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
845       if (opcode <= 255)
846         return len + 1;
847
848       /* Try using DW_LNS_const_add_pc followed by special op.  */
849       opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
850       if (opcode <= 255)
851         return len + 2;
852     }
853
854   /* Otherwise use DW_LNS_advance_pc.  */
855   len += 1 + sizeof_leb128 (addr_delta, 0);
856
857   /* DW_LNS_copy or special opcode.  */
858   len += 1;
859
860   return len;
861 }
862
863 static void
864 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
865 {
866   unsigned int tmp, opcode;
867   int need_copy = 0;
868   char *end = p + len;
869
870   /* Line number sequences cannot go backward in addresses.  This means
871      we've incorrectly ordered the statements in the sequence.  */
872   assert ((offsetT) addr_delta >= 0);
873
874   /* Scale the address delta by the minimum instruction length.  */
875   scale_addr_delta (&addr_delta);
876
877   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
878      We cannot use special opcodes here, since we want the end_sequence
879      to emit the matrix entry.  */
880   if (line_delta == INT_MAX)
881     {
882       if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
883         *p++ = DW_LNS_const_add_pc;
884       else
885         {
886           *p++ = DW_LNS_advance_pc;
887           p += output_leb128 (p, addr_delta, 0);
888         }
889
890       *p++ = DW_LNS_extended_op;
891       *p++ = 1;
892       *p++ = DW_LNE_end_sequence;
893       goto done;
894     }
895
896   /* Bias the line delta by the base.  */
897   tmp = line_delta - DWARF2_LINE_BASE;
898
899   /* If the line increment is out of range of a special opcode, we
900      must encode it with DW_LNS_advance_line.  */
901   if (tmp >= DWARF2_LINE_RANGE)
902     {
903       *p++ = DW_LNS_advance_line;
904       p += output_leb128 (p, line_delta, 1);
905
906       line_delta = 0;
907       tmp = 0 - DWARF2_LINE_BASE;
908       need_copy = 1;
909     }
910
911   /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
912      special opcode.  */
913   if (line_delta == 0 && addr_delta == 0)
914     {
915       *p++ = DW_LNS_copy;
916       goto done;
917     }
918
919   /* Bias the opcode by the special opcode base.  */
920   tmp += DWARF2_LINE_OPCODE_BASE;
921
922   /* Avoid overflow when addr_delta is large.  */
923   if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
924     {
925       /* Try using a special opcode.  */
926       opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
927       if (opcode <= 255)
928         {
929           *p++ = opcode;
930           goto done;
931         }
932
933       /* Try using DW_LNS_const_add_pc followed by special op.  */
934       opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
935       if (opcode <= 255)
936         {
937           *p++ = DW_LNS_const_add_pc;
938           *p++ = opcode;
939           goto done;
940         }
941     }
942
943   /* Otherwise use DW_LNS_advance_pc.  */
944   *p++ = DW_LNS_advance_pc;
945   p += output_leb128 (p, addr_delta, 0);
946
947   if (need_copy)
948     *p++ = DW_LNS_copy;
949   else
950     *p++ = tmp;
951
952  done:
953   assert (p == end);
954 }
955
956 /* Handy routine to combine calls to the above two routines.  */
957
958 static void
959 out_inc_line_addr (int line_delta, addressT addr_delta)
960 {
961   int len = size_inc_line_addr (line_delta, addr_delta);
962   emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
963 }
964
965 /* Write out an alternative form of line and address skips using
966    DW_LNS_fixed_advance_pc opcodes.  This uses more space than the default
967    line and address information, but it is required if linker relaxation
968    could change the code offsets.  The following two routines *must* be
969    kept in sync.  */
970
971 static int
972 size_fixed_inc_line_addr (int line_delta, addressT addr_delta)
973 {
974   int len = 0;
975
976   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.  */
977   if (line_delta != INT_MAX)
978     len = 1 + sizeof_leb128 (line_delta, 1);
979
980   if (addr_delta > 50000)
981     {
982       /* DW_LNS_extended_op */
983       len += 1 + sizeof_leb128 (sizeof_address + 1, 0);
984       /* DW_LNE_set_address */
985       len += 1 + sizeof_address;
986     }
987   else
988     /* DW_LNS_fixed_advance_pc */
989     len += 3;
990
991   if (line_delta == INT_MAX)
992     /* DW_LNS_extended_op + DW_LNE_end_sequence */
993     len += 3;
994   else
995     /* DW_LNS_copy */
996     len += 1;
997
998   return len;
999 }
1000
1001 static void
1002 emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag,
1003                           char *p, int len)
1004 {
1005   expressionS *exp;
1006   segT line_seg;
1007   char *end = p + len;
1008
1009   /* Line number sequences cannot go backward in addresses.  This means
1010      we've incorrectly ordered the statements in the sequence.  */
1011   assert ((offsetT) addr_delta >= 0);
1012
1013   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.  */
1014   if (line_delta != INT_MAX)
1015     {
1016       *p++ = DW_LNS_advance_line;
1017       p += output_leb128 (p, line_delta, 1);
1018     }
1019
1020   exp = symbol_get_value_expression (frag->fr_symbol);
1021   line_seg = subseg_get (".debug_line", 0);
1022
1023   /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can
1024      advance the address by at most 64K.  Linker relaxation (without
1025      which this function would not be used) could change the operand by
1026      an unknown amount.  If the address increment is getting close to
1027      the limit, just reset the address.  */
1028   if (addr_delta > 50000)
1029     {
1030       symbolS *to_sym;
1031       expressionS expr;
1032
1033       assert (exp->X_op = O_subtract);
1034       to_sym = exp->X_add_symbol;
1035
1036       *p++ = DW_LNS_extended_op;
1037       p += output_leb128 (p, sizeof_address + 1, 0);
1038       *p++ = DW_LNE_set_address;
1039       expr.X_op = O_symbol;
1040       expr.X_add_symbol = to_sym;
1041       expr.X_add_number = 0;
1042       subseg_change (line_seg, 0);
1043       emit_expr_fix (&expr, sizeof_address, frag, p);
1044       p += sizeof_address;
1045     }
1046   else
1047     {
1048       *p++ = DW_LNS_fixed_advance_pc;
1049       subseg_change (line_seg, 0);
1050       emit_expr_fix (exp, 2, frag, p);
1051       p += 2;
1052     }
1053
1054   if (line_delta == INT_MAX)
1055     {
1056       *p++ = DW_LNS_extended_op;
1057       *p++ = 1;
1058       *p++ = DW_LNE_end_sequence;
1059     }
1060   else
1061     *p++ = DW_LNS_copy;
1062
1063   assert (p == end);
1064 }
1065
1066 /* Generate a variant frag that we can use to relax address/line
1067    increments between fragments of the target segment.  */
1068
1069 static void
1070 relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
1071 {
1072   expressionS expr;
1073   int max_chars;
1074
1075   expr.X_op = O_subtract;
1076   expr.X_add_symbol = to_sym;
1077   expr.X_op_symbol = from_sym;
1078   expr.X_add_number = 0;
1079
1080   /* The maximum size of the frag is the line delta with a maximum
1081      sized address delta.  */
1082   if (DWARF2_USE_FIXED_ADVANCE_PC)
1083     max_chars = size_fixed_inc_line_addr (line_delta,
1084                                           -DWARF2_LINE_MIN_INSN_LENGTH);
1085   else
1086     max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
1087
1088   frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
1089             make_expr_symbol (&expr), line_delta, NULL);
1090 }
1091
1092 /* The function estimates the size of a rs_dwarf2dbg variant frag
1093    based on the current values of the symbols.  It is called before
1094    the relaxation loop.  We set fr_subtype to the expected length.  */
1095
1096 int
1097 dwarf2dbg_estimate_size_before_relax (fragS *frag)
1098 {
1099   offsetT addr_delta;
1100   int size;
1101
1102   addr_delta = resolve_symbol_value (frag->fr_symbol);
1103   if (DWARF2_USE_FIXED_ADVANCE_PC)
1104     size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta);
1105   else
1106     size = size_inc_line_addr (frag->fr_offset, addr_delta);
1107
1108   frag->fr_subtype = size;
1109
1110   return size;
1111 }
1112
1113 /* This function relaxes a rs_dwarf2dbg variant frag based on the
1114    current values of the symbols.  fr_subtype is the current length
1115    of the frag.  This returns the change in frag length.  */
1116
1117 int
1118 dwarf2dbg_relax_frag (fragS *frag)
1119 {
1120   int old_size, new_size;
1121
1122   old_size = frag->fr_subtype;
1123   new_size = dwarf2dbg_estimate_size_before_relax (frag);
1124
1125   return new_size - old_size;
1126 }
1127
1128 /* This function converts a rs_dwarf2dbg variant frag into a normal
1129    fill frag.  This is called after all relaxation has been done.
1130    fr_subtype will be the desired length of the frag.  */
1131
1132 void
1133 dwarf2dbg_convert_frag (fragS *frag)
1134 {
1135   offsetT addr_diff;
1136
1137   addr_diff = resolve_symbol_value (frag->fr_symbol);
1138
1139   /* fr_var carries the max_chars that we created the fragment with.
1140      fr_subtype carries the current expected length.  We must, of
1141      course, have allocated enough memory earlier.  */
1142   assert (frag->fr_var >= (int) frag->fr_subtype);
1143
1144   if (DWARF2_USE_FIXED_ADVANCE_PC)
1145     emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag,
1146                               frag->fr_literal + frag->fr_fix,
1147                               frag->fr_subtype);
1148   else
1149     emit_inc_line_addr (frag->fr_offset, addr_diff,
1150                         frag->fr_literal + frag->fr_fix, frag->fr_subtype);
1151
1152   frag->fr_fix += frag->fr_subtype;
1153   frag->fr_type = rs_fill;
1154   frag->fr_var = 0;
1155   frag->fr_offset = 0;
1156 }
1157
1158 /* Generate .debug_line content for the chain of line number entries
1159    beginning at E, for segment SEG.  */
1160
1161 static void
1162 process_entries (segT seg, struct line_entry *e)
1163 {
1164   unsigned filenum = 1;
1165   unsigned line = 1;
1166   unsigned column = 0;
1167   unsigned isa = 0;
1168   unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1169   fragS *last_frag = NULL, *frag;
1170   addressT last_frag_ofs = 0, frag_ofs;
1171   symbolS *last_lab = NULL, *lab;
1172   struct line_entry *next;
1173
1174   do
1175     {
1176       int line_delta;
1177
1178       if (filenum != e->loc.filenum)
1179         {
1180           filenum = e->loc.filenum;
1181           out_opcode (DW_LNS_set_file);
1182           out_uleb128 (filenum);
1183         }
1184
1185       if (column != e->loc.column)
1186         {
1187           column = e->loc.column;
1188           out_opcode (DW_LNS_set_column);
1189           out_uleb128 (column);
1190         }
1191
1192       if (isa != e->loc.isa)
1193         {
1194           isa = e->loc.isa;
1195           out_opcode (DW_LNS_set_isa);
1196           out_uleb128 (isa);
1197         }
1198
1199       if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
1200         {
1201           flags = e->loc.flags;
1202           out_opcode (DW_LNS_negate_stmt);
1203         }
1204
1205       if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
1206         out_opcode (DW_LNS_set_basic_block);
1207
1208       if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
1209         out_opcode (DW_LNS_set_prologue_end);
1210
1211       if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1212         out_opcode (DW_LNS_set_epilogue_begin);
1213
1214       /* Don't try to optimize away redundant entries; gdb wants two
1215          entries for a function where the code starts on the same line as
1216          the {, and there's no way to identify that case here.  Trust gcc
1217          to optimize appropriately.  */
1218       line_delta = e->loc.line - line;
1219       lab = e->label;
1220       frag = symbol_get_frag (lab);
1221       frag_ofs = S_GET_VALUE (lab);
1222
1223       if (last_frag == NULL)
1224         {
1225           out_set_addr (lab);
1226           out_inc_line_addr (line_delta, 0);
1227         }
1228       else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1229         out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
1230       else
1231         relax_inc_line_addr (line_delta, lab, last_lab);
1232
1233       line = e->loc.line;
1234       last_lab = lab;
1235       last_frag = frag;
1236       last_frag_ofs = frag_ofs;
1237
1238       next = e->next;
1239       free (e);
1240       e = next;
1241     }
1242   while (e);
1243
1244   /* Emit a DW_LNE_end_sequence for the end of the section.  */
1245   frag = last_frag_for_seg (seg);
1246   frag_ofs = get_frag_fix (frag, seg);
1247   if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1248     out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
1249   else
1250     {
1251       lab = symbol_temp_new (seg, frag_ofs, frag);
1252       relax_inc_line_addr (INT_MAX, lab, last_lab);
1253     }
1254 }
1255
1256 /* Emit the directory and file tables for .debug_line.  */
1257
1258 static void
1259 out_file_list (void)
1260 {
1261   size_t size;
1262   const char *dir;
1263   char *cp;
1264   unsigned int i;
1265
1266   /* Emit directory list.  */
1267   for (i = 1; i < dirs_in_use; ++i)
1268     {
1269       dir = remap_debug_filename (dirs[i]);
1270       size = strlen (dir) + 1;
1271       cp = frag_more (size);
1272       memcpy (cp, dir, size);
1273     }
1274   /* Terminate it.  */
1275   out_byte ('\0');
1276
1277   for (i = 1; i < files_in_use; ++i)
1278     {
1279       if (files[i].filename == NULL)
1280         {
1281           as_bad (_("unassigned file number %ld"), (long) i);
1282           /* Prevent a crash later, particularly for file 1.  */
1283           files[i].filename = "";
1284           continue;
1285         }
1286
1287       size = strlen (files[i].filename) + 1;
1288       cp = frag_more (size);
1289       memcpy (cp, files[i].filename, size);
1290
1291       out_uleb128 (files[i].dir);       /* directory number */
1292       out_uleb128 (0);                  /* last modification timestamp */
1293       out_uleb128 (0);                  /* filesize */
1294     }
1295
1296   /* Terminate filename list.  */
1297   out_byte (0);
1298 }
1299
1300 /* Switch to SEC and output a header length field.  Return the size of
1301    offsets used in SEC.  The caller must set EXPR->X_add_symbol value
1302    to the end of the section.  */
1303
1304 static int
1305 out_header (asection *sec, expressionS *expr)
1306 {
1307   symbolS *start_sym;
1308   symbolS *end_sym;
1309
1310   subseg_set (sec, 0);
1311   start_sym = symbol_temp_new_now ();;
1312   end_sym = symbol_temp_make ();
1313
1314   /* Total length of the information.  */
1315   expr->X_op = O_subtract;
1316   expr->X_add_symbol = end_sym;
1317   expr->X_op_symbol = start_sym;
1318
1319   switch (DWARF2_FORMAT (sec))
1320     {
1321     case dwarf2_format_32bit:
1322       expr->X_add_number = -4;
1323       emit_expr (expr, 4);
1324       return 4;
1325
1326     case dwarf2_format_64bit:
1327       expr->X_add_number = -12;
1328       out_four (-1);
1329       emit_expr (expr, 8);
1330       return 8;
1331
1332     case dwarf2_format_64bit_irix:
1333       expr->X_add_number = -8;
1334       emit_expr (expr, 8);
1335       return 8;
1336     }
1337
1338   as_fatal (_("internal error: unknown dwarf2 format"));
1339   return 0;
1340 }
1341
1342 /* Emit the collected .debug_line data.  */
1343
1344 static void
1345 out_debug_line (segT line_seg)
1346 {
1347   expressionS expr;
1348   symbolS *prologue_end;
1349   symbolS *line_end;
1350   struct line_seg *s;
1351   int sizeof_offset;
1352
1353   sizeof_offset = out_header (line_seg, &expr);
1354   line_end = expr.X_add_symbol;
1355
1356   /* Version.  */
1357   out_two (2);
1358
1359   /* Length of the prologue following this length.  */
1360   prologue_end = symbol_temp_make ();
1361   expr.X_add_symbol = prologue_end;
1362   expr.X_add_number = - (4 + 2 + 4);
1363   emit_expr (&expr, sizeof_offset);
1364
1365   /* Parameters of the state machine.  */
1366   out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1367   out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1368   out_byte (DWARF2_LINE_BASE);
1369   out_byte (DWARF2_LINE_RANGE);
1370   out_byte (DWARF2_LINE_OPCODE_BASE);
1371
1372   /* Standard opcode lengths.  */
1373   out_byte (0);                 /* DW_LNS_copy */
1374   out_byte (1);                 /* DW_LNS_advance_pc */
1375   out_byte (1);                 /* DW_LNS_advance_line */
1376   out_byte (1);                 /* DW_LNS_set_file */
1377   out_byte (1);                 /* DW_LNS_set_column */
1378   out_byte (0);                 /* DW_LNS_negate_stmt */
1379   out_byte (0);                 /* DW_LNS_set_basic_block */
1380   out_byte (0);                 /* DW_LNS_const_add_pc */
1381   out_byte (1);                 /* DW_LNS_fixed_advance_pc */
1382   out_byte (0);                 /* DW_LNS_set_prologue_end */
1383   out_byte (0);                 /* DW_LNS_set_epilogue_begin */
1384   out_byte (1);                 /* DW_LNS_set_isa */
1385
1386   out_file_list ();
1387
1388   symbol_set_value_now (prologue_end);
1389
1390   /* For each section, emit a statement program.  */
1391   for (s = all_segs; s; s = s->next)
1392     process_entries (s->seg, s->head->head);
1393
1394   symbol_set_value_now (line_end);
1395 }
1396
1397 static void
1398 out_debug_ranges (segT ranges_seg)
1399 {
1400   unsigned int addr_size = sizeof_address;
1401   struct line_seg *s;
1402   expressionS expr;
1403   unsigned int i;
1404
1405   subseg_set (ranges_seg, 0);
1406
1407   /* Base Address Entry.  */
1408   for (i = 0; i < addr_size; i++)
1409     out_byte (0xff);
1410   for (i = 0; i < addr_size; i++)
1411     out_byte (0);
1412
1413   /* Range List Entry.  */
1414   for (s = all_segs; s; s = s->next)
1415     {
1416       fragS *frag;
1417       symbolS *beg, *end;
1418
1419       frag = first_frag_for_seg (s->seg);
1420       beg = symbol_temp_new (s->seg, 0, frag);
1421       s->text_start = beg;
1422
1423       frag = last_frag_for_seg (s->seg);
1424       end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1425       s->text_end = end;
1426
1427       expr.X_op = O_symbol;
1428       expr.X_add_symbol = beg;
1429       expr.X_add_number = 0;
1430       emit_expr (&expr, addr_size);
1431
1432       expr.X_op = O_symbol;
1433       expr.X_add_symbol = end;
1434       expr.X_add_number = 0;
1435       emit_expr (&expr, addr_size);
1436     }
1437
1438   /* End of Range Entry.   */
1439   for (i = 0; i < addr_size; i++)
1440     out_byte (0);
1441   for (i = 0; i < addr_size; i++)
1442     out_byte (0);
1443 }
1444
1445 /* Emit data for .debug_aranges.  */
1446
1447 static void
1448 out_debug_aranges (segT aranges_seg, segT info_seg)
1449 {
1450   unsigned int addr_size = sizeof_address;
1451   struct line_seg *s;
1452   expressionS expr;
1453   symbolS *aranges_end;
1454   char *p;
1455   int sizeof_offset;
1456
1457   sizeof_offset = out_header (aranges_seg, &expr);
1458   aranges_end = expr.X_add_symbol;
1459
1460   /* Version.  */
1461   out_two (2);
1462
1463   /* Offset to .debug_info.  */
1464   TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset);
1465
1466   /* Size of an address (offset portion).  */
1467   out_byte (addr_size);
1468
1469   /* Size of a segment descriptor.  */
1470   out_byte (0);
1471
1472   /* Align the header.  */
1473   frag_align (ffs (2 * addr_size) - 1, 0, 0);
1474
1475   for (s = all_segs; s; s = s->next)
1476     {
1477       fragS *frag;
1478       symbolS *beg, *end;
1479
1480       frag = first_frag_for_seg (s->seg);
1481       beg = symbol_temp_new (s->seg, 0, frag);
1482       s->text_start = beg;
1483
1484       frag = last_frag_for_seg (s->seg);
1485       end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1486       s->text_end = end;
1487
1488       expr.X_op = O_symbol;
1489       expr.X_add_symbol = beg;
1490       expr.X_add_number = 0;
1491       emit_expr (&expr, addr_size);
1492
1493       expr.X_op = O_subtract;
1494       expr.X_add_symbol = end;
1495       expr.X_op_symbol = beg;
1496       expr.X_add_number = 0;
1497       emit_expr (&expr, addr_size);
1498     }
1499
1500   p = frag_more (2 * addr_size);
1501   md_number_to_chars (p, 0, addr_size);
1502   md_number_to_chars (p + addr_size, 0, addr_size);
1503
1504   symbol_set_value_now (aranges_end);
1505 }
1506
1507 /* Emit data for .debug_abbrev.  Note that this must be kept in
1508    sync with out_debug_info below.  */
1509
1510 static void
1511 out_debug_abbrev (segT abbrev_seg,
1512                   segT info_seg ATTRIBUTE_UNUSED,
1513                   segT line_seg ATTRIBUTE_UNUSED)
1514 {
1515   subseg_set (abbrev_seg, 0);
1516
1517   out_uleb128 (1);
1518   out_uleb128 (DW_TAG_compile_unit);
1519   out_byte (DW_CHILDREN_no);
1520   if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit)
1521     out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1522   else
1523     out_abbrev (DW_AT_stmt_list, DW_FORM_data8);
1524   if (all_segs->next == NULL)
1525     {
1526       out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1527       out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1528     }
1529   else
1530     {
1531       if (DWARF2_FORMAT (info_seg) == dwarf2_format_32bit)
1532         out_abbrev (DW_AT_ranges, DW_FORM_data4);
1533       else
1534         out_abbrev (DW_AT_ranges, DW_FORM_data8);
1535     }
1536   out_abbrev (DW_AT_name, DW_FORM_string);
1537   out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1538   out_abbrev (DW_AT_producer, DW_FORM_string);
1539   out_abbrev (DW_AT_language, DW_FORM_data2);
1540   out_abbrev (0, 0);
1541
1542   /* Terminate the abbreviations for this compilation unit.  */
1543   out_byte (0);
1544 }
1545
1546 /* Emit a description of this compilation unit for .debug_info.  */
1547
1548 static void
1549 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg)
1550 {
1551   char producer[128];
1552   const char *comp_dir;
1553   const char *dirname;
1554   expressionS expr;
1555   symbolS *info_end;
1556   char *p;
1557   int len;
1558   int sizeof_offset;
1559
1560   sizeof_offset = out_header (info_seg, &expr);
1561   info_end = expr.X_add_symbol;
1562
1563   /* DWARF version.  */
1564   out_two (2);
1565
1566   /* .debug_abbrev offset */
1567   TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1568
1569   /* Target address size.  */
1570   out_byte (sizeof_address);
1571
1572   /* DW_TAG_compile_unit DIE abbrev */
1573   out_uleb128 (1);
1574
1575   /* DW_AT_stmt_list */
1576   TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg),
1577                          (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit
1578                           ? 4 : 8));
1579
1580   /* These two attributes are emitted if all of the code is contiguous.  */
1581   if (all_segs->next == NULL)
1582     {
1583       /* DW_AT_low_pc */
1584       expr.X_op = O_symbol;
1585       expr.X_add_symbol = all_segs->text_start;
1586       expr.X_add_number = 0;
1587       emit_expr (&expr, sizeof_address);
1588
1589       /* DW_AT_high_pc */
1590       expr.X_op = O_symbol;
1591       expr.X_add_symbol = all_segs->text_end;
1592       expr.X_add_number = 0;
1593       emit_expr (&expr, sizeof_address);
1594     }
1595   else
1596     {
1597       /* This attribute is emitted if the code is disjoint.  */
1598       /* DW_AT_ranges.  */
1599       TC_DWARF2_EMIT_OFFSET (section_symbol (ranges_seg), sizeof_offset);
1600     }
1601
1602   /* DW_AT_name.  We don't have the actual file name that was present
1603      on the command line, so assume files[1] is the main input file.
1604      We're not supposed to get called unless at least one line number
1605      entry was emitted, so this should always be defined.  */
1606   if (files_in_use == 0)
1607     abort ();
1608   if (files[1].dir)
1609     {
1610       dirname = remap_debug_filename (dirs[files[1].dir]);
1611       len = strlen (dirname);
1612       p = frag_more (len + 1);
1613       memcpy (p, dirname, len);
1614       INSERT_DIR_SEPARATOR (p, len);
1615     }
1616   len = strlen (files[1].filename) + 1;
1617   p = frag_more (len);
1618   memcpy (p, files[1].filename, len);
1619
1620   /* DW_AT_comp_dir */
1621   comp_dir = remap_debug_filename (getpwd ());
1622   len = strlen (comp_dir) + 1;
1623   p = frag_more (len);
1624   memcpy (p, comp_dir, len);
1625
1626   /* DW_AT_producer */
1627   sprintf (producer, "GNU AS %s", VERSION);
1628   len = strlen (producer) + 1;
1629   p = frag_more (len);
1630   memcpy (p, producer, len);
1631
1632   /* DW_AT_language.  Yes, this is probably not really MIPS, but the
1633      dwarf2 draft has no standard code for assembler.  */
1634   out_two (DW_LANG_Mips_Assembler);
1635
1636   symbol_set_value_now (info_end);
1637 }
1638
1639 /* Finish the dwarf2 debug sections.  We emit .debug.line if there
1640    were any .file/.loc directives, or --gdwarf2 was given, or if the
1641    file has a non-empty .debug_info section.  If we emit .debug_line,
1642    and the .debug_info section is empty, we also emit .debug_info,
1643    .debug_aranges and .debug_abbrev.  ALL_SEGS will be non-null if
1644    there were any .file/.loc directives, or --gdwarf2 was given and
1645    there were any located instructions emitted.  */
1646
1647 void
1648 dwarf2_finish (void)
1649 {
1650   segT line_seg;
1651   struct line_seg *s;
1652   segT info_seg;
1653   int emit_other_sections = 0;
1654
1655   info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
1656   emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
1657
1658   if (!all_segs && emit_other_sections)
1659     /* There is no line information and no non-empty .debug_info
1660        section.  */
1661     return;
1662
1663   /* Calculate the size of an address for the target machine.  */
1664   sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1665
1666   /* Create and switch to the line number section.  */
1667   line_seg = subseg_new (".debug_line", 0);
1668   bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
1669
1670   /* For each subsection, chain the debug entries together.  */
1671   for (s = all_segs; s; s = s->next)
1672     {
1673       struct line_subseg *ss = s->head;
1674       struct line_entry **ptail = ss->ptail;
1675
1676       while ((ss = ss->next) != NULL)
1677         {
1678           *ptail = ss->head;
1679           ptail = ss->ptail;
1680         }
1681     }
1682
1683   out_debug_line (line_seg);
1684
1685   /* If this is assembler generated line info, and there is no
1686      debug_info already, we need .debug_info and .debug_abbrev
1687      sections as well.  */
1688   if (emit_other_sections)
1689     {
1690       segT abbrev_seg;
1691       segT aranges_seg;
1692       segT ranges_seg;
1693
1694       assert (all_segs);
1695
1696       info_seg = subseg_new (".debug_info", 0);
1697       abbrev_seg = subseg_new (".debug_abbrev", 0);
1698       aranges_seg = subseg_new (".debug_aranges", 0);
1699
1700       bfd_set_section_flags (stdoutput, info_seg,
1701                              SEC_READONLY | SEC_DEBUGGING);
1702       bfd_set_section_flags (stdoutput, abbrev_seg,
1703                              SEC_READONLY | SEC_DEBUGGING);
1704       bfd_set_section_flags (stdoutput, aranges_seg,
1705                              SEC_READONLY | SEC_DEBUGGING);
1706
1707       record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1708
1709       if (all_segs->next == NULL)
1710         ranges_seg = NULL;
1711       else
1712         {
1713           ranges_seg = subseg_new (".debug_ranges", 0);
1714           bfd_set_section_flags (stdoutput, ranges_seg,
1715                                  SEC_READONLY | SEC_DEBUGGING);
1716           record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1);
1717           out_debug_ranges (ranges_seg);
1718         }
1719
1720       out_debug_aranges (aranges_seg, info_seg);
1721       out_debug_abbrev (abbrev_seg, info_seg, line_seg);
1722       out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg);
1723     }
1724 }