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