OSDN Git Service

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