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