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