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