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