* mh-dgux: Delete.
[binutils.git] / gas / dwarf2dbg.c
blob58ac0f5785204dfbcc35a2af3ee6a97cb91739d5
1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
22 /* Logical line numbers can be controlled by the compiler via the
23 following two directives:
25 .file FILENO "file.c"
26 .loc FILENO LINENO [COLUMN]
28 FILENO is the filenumber. */
30 #include "ansidecl.h"
31 #include "as.h"
33 #ifdef HAVE_LIMITS_H
34 #include <limits.h>
35 #else
36 #ifdef HAVE_SYS_PARAM_H
37 #include <sys/param.h>
38 #endif
39 #ifndef INT_MAX
40 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
41 #endif
42 #endif
44 #include "dwarf2dbg.h"
45 #include <filenames.h>
47 #ifndef DWARF2_FORMAT
48 # define DWARF2_FORMAT() dwarf2_format_32bit
49 #endif
51 #ifndef DWARF2_ADDR_SIZE
52 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8);
53 #endif
56 #ifdef BFD_ASSEMBLER
58 #include "subsegs.h"
60 #include "elf/dwarf2.h"
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
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 dwarf2_finish(). */
74 #define DWARF2_LINE_OPCODE_BASE 10
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
82 /* Range of line offsets in a special line info. opcode. */
83 #ifndef DWARF2_LINE_RANGE
84 # define DWARF2_LINE_RANGE 14
85 #endif
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
93 /* Flag that indicates the initial value of the is_stmt_start flag.
94 In the present implementation, we do not mark any lines as
95 the beginning of a source statement, because that information
96 is not made available by the GCC front-end. */
97 #define DWARF2_LINE_DEFAULT_IS_STMT 1
99 /* Given a special op, return the line skip amount. */
100 #define SPECIAL_LINE(op) \
101 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
103 /* Given a special op, return the address skip amount (in units of
104 DWARF2_LINE_MIN_INSN_LENGTH. */
105 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
107 /* The maximum address skip amount that can be encoded with a special op. */
108 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
110 struct line_entry {
111 struct line_entry *next;
112 fragS *frag;
113 addressT frag_ofs;
114 struct dwarf2_line_info loc;
117 struct line_subseg {
118 struct line_subseg *next;
119 subsegT subseg;
120 struct line_entry *head;
121 struct line_entry **ptail;
124 struct line_seg {
125 struct line_seg *next;
126 segT seg;
127 struct line_subseg *head;
128 symbolS *text_start;
129 symbolS *text_end;
132 /* Collects data for all line table entries during assembly. */
133 static struct line_seg *all_segs;
135 struct file_entry {
136 const char *filename;
137 unsigned int dir;
140 /* Table of files used by .debug_line. */
141 static struct file_entry *files;
142 static unsigned int files_in_use;
143 static unsigned int files_allocated;
145 /* Table of directories used by .debug_line. */
146 static char **dirs;
147 static unsigned int dirs_in_use;
148 static unsigned int dirs_allocated;
150 /* TRUE when we've seen a .loc directive recently. Used to avoid
151 doing work when there's nothing to do. */
152 static bfd_boolean loc_directive_seen;
154 /* Current location as indicated by the most recent .loc directive. */
155 static struct dwarf2_line_info current;
157 /* Fake label name. */
158 static char const fake_label_name[] = ".L0\001";
160 /* The size of an address on the target. */
161 static unsigned int sizeof_address;
163 static struct line_subseg *get_line_subseg PARAMS ((segT, subsegT));
164 static unsigned int get_filenum PARAMS ((const char *, unsigned int));
165 static struct frag *first_frag_for_seg PARAMS ((segT));
166 static struct frag *last_frag_for_seg PARAMS ((segT));
167 static void out_byte PARAMS ((int));
168 static void out_opcode PARAMS ((int));
169 static void out_two PARAMS ((int));
170 static void out_four PARAMS ((int));
171 static void out_abbrev PARAMS ((int, int));
172 static void out_uleb128 PARAMS ((addressT));
173 static symbolS *symbol_new_now PARAMS ((void));
174 static void set_symbol_value_now PARAMS ((symbolS *));
175 static offsetT get_frag_fix PARAMS ((fragS *));
176 static void out_set_addr PARAMS ((segT, fragS *, addressT));
177 static int size_inc_line_addr PARAMS ((int, addressT));
178 static void emit_inc_line_addr PARAMS ((int, addressT, char *, int));
179 static void out_inc_line_addr PARAMS ((int, addressT));
180 static void relax_inc_line_addr PARAMS ((int, segT, fragS *, addressT,
181 fragS *, addressT));
182 static void process_entries PARAMS ((segT, struct line_entry *));
183 static void out_file_list PARAMS ((void));
184 static void out_debug_line PARAMS ((segT));
185 static void out_debug_aranges PARAMS ((segT, segT));
186 static void out_debug_abbrev PARAMS ((segT));
187 static void out_debug_info PARAMS ((segT, segT, segT));
189 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
191 static struct line_subseg *
192 get_line_subseg (seg, subseg)
193 segT seg;
194 subsegT subseg;
196 static segT last_seg;
197 static subsegT last_subseg;
198 static struct line_subseg *last_line_subseg;
200 struct line_seg *s;
201 struct line_subseg **pss, *ss;
203 if (seg == last_seg && subseg == last_subseg)
204 return last_line_subseg;
206 for (s = all_segs; s; s = s->next)
207 if (s->seg == seg)
208 goto found_seg;
210 s = (struct line_seg *) xmalloc (sizeof (*s));
211 s->next = all_segs;
212 s->seg = seg;
213 s->head = NULL;
214 all_segs = s;
216 found_seg:
217 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
219 if (ss->subseg == subseg)
220 goto found_subseg;
221 if (ss->subseg > subseg)
222 break;
225 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
226 ss->next = *pss;
227 ss->subseg = subseg;
228 ss->head = NULL;
229 ss->ptail = &ss->head;
230 *pss = ss;
232 found_subseg:
233 last_seg = seg;
234 last_subseg = subseg;
235 last_line_subseg = ss;
237 return ss;
240 /* Record an entry for LOC ocurring at OFS within the current fragment. */
242 void
243 dwarf2_gen_line_info (ofs, loc)
244 addressT ofs;
245 struct dwarf2_line_info *loc;
247 struct line_subseg *ss;
248 struct line_entry *e;
249 static unsigned int line = -1;
250 static unsigned int filenum = -1;
252 /* Early out for as-yet incomplete location information. */
253 if (loc->filenum == 0 || loc->line == 0)
254 return;
256 /* Don't emit sequences of line symbols for the same line when the
257 symbols apply to assembler code. It is necessary to emit
258 duplicate line symbols when a compiler asks for them, because GDB
259 uses them to determine the end of the prologue. */
260 if (debug_type == DEBUG_DWARF2
261 && line == loc->line && filenum == loc->filenum)
262 return;
264 line = loc->line;
265 filenum = loc->filenum;
267 e = (struct line_entry *) xmalloc (sizeof (*e));
268 e->next = NULL;
269 e->frag = frag_now;
270 e->frag_ofs = ofs;
271 e->loc = *loc;
273 ss = get_line_subseg (now_seg, now_subseg);
274 *ss->ptail = e;
275 ss->ptail = &e->next;
278 void
279 dwarf2_where (line)
280 struct dwarf2_line_info *line;
282 if (debug_type == DEBUG_DWARF2)
284 char *filename;
285 as_where (&filename, &line->line);
286 line->filenum = get_filenum (filename, 0);
287 line->column = 0;
288 line->flags = DWARF2_FLAG_BEGIN_STMT;
290 else
291 *line = current;
294 /* Called for each machine instruction, or relatively atomic group of
295 machine instructions (ie built-in macro). The instruction or group
296 is SIZE bytes in length. If dwarf2 line number generation is called
297 for, emit a line statement appropriately. */
299 void
300 dwarf2_emit_insn (size)
301 int size;
303 struct dwarf2_line_info loc;
305 if (loc_directive_seen)
307 /* Use the last location established by a .loc directive, not
308 the value returned by dwarf2_where(). That calls as_where()
309 which will return either the logical input file name (foo.c)
310 or the physical input file name (foo.s) and not the file name
311 specified in the most recent .loc directive (eg foo.h). */
312 loc = current;
314 /* Unless we generate DWARF2 debugging information for each
315 assembler line, we only emit one line symbol for one LOC. */
316 if (debug_type != DEBUG_DWARF2)
317 loc_directive_seen = FALSE;
319 else if (debug_type != DEBUG_DWARF2)
320 return;
321 else
322 dwarf2_where (& loc);
324 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
327 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
328 allocate it on that file table slot, otherwise return the first
329 empty one. */
331 static unsigned int
332 get_filenum (filename, num)
333 const char *filename;
334 unsigned int num;
336 static unsigned int last_used, last_used_dir_len;
337 const char *file;
338 size_t dir_len;
339 unsigned int i, dir;
341 if (num == 0 && last_used)
343 if (! files[last_used].dir
344 && strcmp (filename, files[last_used].filename) == 0)
345 return last_used;
346 if (files[last_used].dir
347 && strncmp (filename, dirs[files[last_used].dir],
348 last_used_dir_len) == 0
349 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
350 && strcmp (filename + last_used_dir_len + 1,
351 files[last_used].filename) == 0)
352 return last_used;
355 file = lbasename (filename);
356 /* Don't make empty string from / or A: from A:/ . */
357 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
358 if (file <= filename + 3)
359 file = filename;
360 #else
361 if (file == filename + 1)
362 file = filename;
363 #endif
364 dir_len = file - filename;
366 dir = 0;
367 if (dir_len)
369 --dir_len;
370 for (dir = 1; dir < dirs_in_use; ++dir)
371 if (memcmp (filename, dirs[dir], dir_len) == 0
372 && dirs[dir][dir_len] == '\0')
373 break;
375 if (dir >= dirs_in_use)
377 if (dir >= dirs_allocated)
379 dirs_allocated = dir + 32;
380 dirs = (char **)
381 xrealloc (dirs, (dir + 32) * sizeof (const char *));
384 dirs[dir] = xmalloc (dir_len + 1);
385 memcpy (dirs[dir], filename, dir_len);
386 dirs[dir][dir_len] = '\0';
387 dirs_in_use = dir + 1;
391 if (num == 0)
393 for (i = 1; i < files_in_use; ++i)
394 if (files[i].dir == dir
395 && strcmp (file, files[i].filename) == 0)
397 last_used = i;
398 last_used_dir_len = dir_len;
399 return i;
402 else
403 i = num;
405 if (i >= files_allocated)
407 unsigned int old = files_allocated;
409 files_allocated = i + 32;
410 files = (struct file_entry *)
411 xrealloc (files, (i + 32) * sizeof (struct file_entry));
413 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
416 files[i].filename = num ? file : xstrdup (file);
417 files[i].dir = dir;
418 files_in_use = i + 1;
419 last_used = i;
420 last_used_dir_len = dir_len;
422 return i;
425 /* Handle two forms of .file directive:
426 - Pass .file "source.c" to s_app_file
427 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
429 If an entry is added to the file table, return a pointer to the filename. */
431 char *
432 dwarf2_directive_file (dummy)
433 int dummy ATTRIBUTE_UNUSED;
435 offsetT num;
436 char *filename;
437 int filename_len;
439 /* Continue to accept a bare string and pass it off. */
440 SKIP_WHITESPACE ();
441 if (*input_line_pointer == '"')
443 s_app_file (0);
444 return NULL;
447 num = get_absolute_expression ();
448 filename = demand_copy_C_string (&filename_len);
449 demand_empty_rest_of_line ();
451 if (num < 1)
453 as_bad (_("file number less than one"));
454 return NULL;
457 if (num < (int) files_in_use && files[num].filename != 0)
459 as_bad (_("file number %ld already allocated"), (long) num);
460 return NULL;
463 get_filenum (filename, num);
465 return filename;
468 void
469 dwarf2_directive_loc (dummy)
470 int dummy ATTRIBUTE_UNUSED;
472 offsetT filenum, line, column;
474 filenum = get_absolute_expression ();
475 SKIP_WHITESPACE ();
476 line = get_absolute_expression ();
477 SKIP_WHITESPACE ();
478 column = get_absolute_expression ();
479 demand_empty_rest_of_line ();
481 if (filenum < 1)
483 as_bad (_("file number less than one"));
484 return;
486 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
488 as_bad (_("unassigned file number %ld"), (long) filenum);
489 return;
492 current.filenum = filenum;
493 current.line = line;
494 current.column = column;
495 current.flags = DWARF2_FLAG_BEGIN_STMT;
497 loc_directive_seen = TRUE;
499 #ifndef NO_LISTING
500 if (listing)
502 if (files[filenum].dir)
504 size_t dir_len = strlen (dirs[files[filenum].dir]);
505 size_t file_len = strlen (files[filenum].filename);
506 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
508 memcpy (cp, dirs[files[filenum].dir], dir_len);
509 cp[dir_len] = '/';
510 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
511 cp[dir_len + file_len + 1] = '\0';
512 listing_source_file (cp);
514 else
515 listing_source_file (files[filenum].filename);
516 listing_source_line (line);
518 #endif
521 static struct frag *
522 first_frag_for_seg (seg)
523 segT seg;
525 frchainS *f, *first = NULL;
527 for (f = frchain_root; f; f = f->frch_next)
528 if (f->frch_seg == seg
529 && (! first || first->frch_subseg > f->frch_subseg))
530 first = f;
532 return first ? first->frch_root : NULL;
535 static struct frag *
536 last_frag_for_seg (seg)
537 segT seg;
539 frchainS *f, *last = NULL;
541 for (f = frchain_root; f; f = f->frch_next)
542 if (f->frch_seg == seg
543 && (! last || last->frch_subseg < f->frch_subseg))
544 last= f;
546 return last ? last->frch_last : NULL;
549 /* Emit a single byte into the current segment. */
551 static inline void
552 out_byte (byte)
553 int byte;
555 FRAG_APPEND_1_CHAR (byte);
558 /* Emit a statement program opcode into the current segment. */
560 static inline void
561 out_opcode (opc)
562 int opc;
564 out_byte (opc);
567 /* Emit a two-byte word into the current segment. */
569 static inline void
570 out_two (data)
571 int data;
573 md_number_to_chars (frag_more (2), data, 2);
576 /* Emit a four byte word into the current segment. */
578 static inline void
579 out_four (data)
580 int data;
582 md_number_to_chars (frag_more (4), data, 4);
585 /* Emit an unsigned "little-endian base 128" number. */
587 static void
588 out_uleb128 (value)
589 addressT value;
591 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
594 /* Emit a tuple for .debug_abbrev. */
596 static inline void
597 out_abbrev (name, form)
598 int name, form;
600 out_uleb128 (name);
601 out_uleb128 (form);
604 /* Create a new fake symbol whose value is the current position. */
606 static symbolS *
607 symbol_new_now ()
609 return symbol_new (fake_label_name, now_seg, frag_now_fix (), frag_now);
612 /* Set the value of SYM to the current position in the current segment. */
614 static void
615 set_symbol_value_now (sym)
616 symbolS *sym;
618 S_SET_SEGMENT (sym, now_seg);
619 S_SET_VALUE (sym, frag_now_fix ());
620 symbol_set_frag (sym, frag_now);
623 /* Get the size of a fragment. */
625 static offsetT
626 get_frag_fix (frag)
627 fragS *frag;
629 frchainS *fr;
631 if (frag->fr_next)
632 return frag->fr_fix;
634 /* If a fragment is the last in the chain, special measures must be
635 taken to find its size before relaxation, since it may be pending
636 on some subsegment chain. */
637 for (fr = frchain_root; fr; fr = fr->frch_next)
638 if (fr->frch_last == frag)
640 long align_mask = -1 << get_recorded_alignment (fr->frch_seg);
641 return (((char *) obstack_next_free (&fr->frch_obstack)
642 - frag->fr_literal) + ~align_mask) & align_mask;
645 abort ();
648 /* Set an absolute address (may result in a relocation entry). */
650 static void
651 out_set_addr (seg, frag, ofs)
652 segT seg;
653 fragS *frag;
654 addressT ofs;
656 expressionS expr;
657 symbolS *sym;
659 sym = symbol_new (fake_label_name, seg, ofs, frag);
661 out_opcode (DW_LNS_extended_op);
662 out_uleb128 (sizeof_address + 1);
664 out_opcode (DW_LNE_set_address);
665 expr.X_op = O_symbol;
666 expr.X_add_symbol = sym;
667 expr.X_add_number = 0;
668 emit_expr (&expr, sizeof_address);
671 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
672 static void scale_addr_delta PARAMS ((addressT *));
674 static void
675 scale_addr_delta (addr_delta)
676 addressT *addr_delta;
678 static int printed_this = 0;
679 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
681 if (!printed_this)
682 as_bad("unaligned opcodes detected in executable segment");
683 printed_this = 1;
685 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
687 #else
688 #define scale_addr_delta(A)
689 #endif
691 /* Encode a pair of line and address skips as efficiently as possible.
692 Note that the line skip is signed, whereas the address skip is unsigned.
694 The following two routines *must* be kept in sync. This is
695 enforced by making emit_inc_line_addr abort if we do not emit
696 exactly the expected number of bytes. */
698 static int
699 size_inc_line_addr (line_delta, addr_delta)
700 int line_delta;
701 addressT addr_delta;
703 unsigned int tmp, opcode;
704 int len = 0;
706 /* Scale the address delta by the minimum instruction length. */
707 scale_addr_delta (&addr_delta);
709 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
710 We cannot use special opcodes here, since we want the end_sequence
711 to emit the matrix entry. */
712 if (line_delta == INT_MAX)
714 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
715 len = 1;
716 else
717 len = 1 + sizeof_leb128 (addr_delta, 0);
718 return len + 3;
721 /* Bias the line delta by the base. */
722 tmp = line_delta - DWARF2_LINE_BASE;
724 /* If the line increment is out of range of a special opcode, we
725 must encode it with DW_LNS_advance_line. */
726 if (tmp >= DWARF2_LINE_RANGE)
728 len = 1 + sizeof_leb128 (line_delta, 1);
729 line_delta = 0;
730 tmp = 0 - DWARF2_LINE_BASE;
733 /* Bias the opcode by the special opcode base. */
734 tmp += DWARF2_LINE_OPCODE_BASE;
736 /* Avoid overflow when addr_delta is large. */
737 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
739 /* Try using a special opcode. */
740 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
741 if (opcode <= 255)
742 return len + 1;
744 /* Try using DW_LNS_const_add_pc followed by special op. */
745 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
746 if (opcode <= 255)
747 return len + 2;
750 /* Otherwise use DW_LNS_advance_pc. */
751 len += 1 + sizeof_leb128 (addr_delta, 0);
753 /* DW_LNS_copy or special opcode. */
754 len += 1;
756 return len;
759 static void
760 emit_inc_line_addr (line_delta, addr_delta, p, len)
761 int line_delta;
762 addressT addr_delta;
763 char *p;
764 int len;
766 unsigned int tmp, opcode;
767 int need_copy = 0;
768 char *end = p + len;
770 /* Scale the address delta by the minimum instruction length. */
771 scale_addr_delta (&addr_delta);
773 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
774 We cannot use special opcodes here, since we want the end_sequence
775 to emit the matrix entry. */
776 if (line_delta == INT_MAX)
778 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
779 *p++ = DW_LNS_const_add_pc;
780 else
782 *p++ = DW_LNS_advance_pc;
783 p += output_leb128 (p, addr_delta, 0);
786 *p++ = DW_LNS_extended_op;
787 *p++ = 1;
788 *p++ = DW_LNE_end_sequence;
789 goto done;
792 /* Bias the line delta by the base. */
793 tmp = line_delta - DWARF2_LINE_BASE;
795 /* If the line increment is out of range of a special opcode, we
796 must encode it with DW_LNS_advance_line. */
797 if (tmp >= DWARF2_LINE_RANGE)
799 *p++ = DW_LNS_advance_line;
800 p += output_leb128 (p, line_delta, 1);
802 /* Prettier, I think, to use DW_LNS_copy instead of a
803 "line +0, addr +0" special opcode. */
804 if (addr_delta == 0)
806 *p++ = DW_LNS_copy;
807 goto done;
810 line_delta = 0;
811 tmp = 0 - DWARF2_LINE_BASE;
812 need_copy = 1;
815 /* Bias the opcode by the special opcode base. */
816 tmp += DWARF2_LINE_OPCODE_BASE;
818 /* Avoid overflow when addr_delta is large. */
819 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
821 /* Try using a special opcode. */
822 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
823 if (opcode <= 255)
825 *p++ = opcode;
826 goto done;
829 /* Try using DW_LNS_const_add_pc followed by special op. */
830 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
831 if (opcode <= 255)
833 *p++ = DW_LNS_const_add_pc;
834 *p++ = opcode;
835 goto done;
839 /* Otherwise use DW_LNS_advance_pc. */
840 *p++ = DW_LNS_advance_pc;
841 p += output_leb128 (p, addr_delta, 0);
843 if (need_copy)
844 *p++ = DW_LNS_copy;
845 else
846 *p++ = tmp;
848 done:
849 assert (p == end);
852 /* Handy routine to combine calls to the above two routines. */
854 static void
855 out_inc_line_addr (line_delta, addr_delta)
856 int line_delta;
857 addressT addr_delta;
859 int len = size_inc_line_addr (line_delta, addr_delta);
860 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
863 /* Generate a variant frag that we can use to relax address/line
864 increments between fragments of the target segment. */
866 static void
867 relax_inc_line_addr (line_delta, seg, to_frag, to_ofs, from_frag, from_ofs)
868 int line_delta;
869 segT seg;
870 fragS *to_frag, *from_frag;
871 addressT to_ofs, from_ofs;
873 symbolS *to_sym, *from_sym;
874 expressionS expr;
875 int max_chars;
877 to_sym = symbol_new (fake_label_name, seg, to_ofs, to_frag);
878 from_sym = symbol_new (fake_label_name, seg, from_ofs, from_frag);
880 expr.X_op = O_subtract;
881 expr.X_add_symbol = to_sym;
882 expr.X_op_symbol = from_sym;
883 expr.X_add_number = 0;
885 /* The maximum size of the frag is the line delta with a maximum
886 sized address delta. */
887 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
889 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
890 make_expr_symbol (&expr), line_delta, NULL);
893 /* The function estimates the size of a rs_dwarf2dbg variant frag
894 based on the current values of the symbols. It is called before
895 the relaxation loop. We set fr_subtype to the expected length. */
898 dwarf2dbg_estimate_size_before_relax (frag)
899 fragS *frag;
901 offsetT addr_delta;
902 int size;
904 addr_delta = resolve_symbol_value (frag->fr_symbol);
905 size = size_inc_line_addr (frag->fr_offset, addr_delta);
907 frag->fr_subtype = size;
909 return size;
912 /* This function relaxes a rs_dwarf2dbg variant frag based on the
913 current values of the symbols. fr_subtype is the current length
914 of the frag. This returns the change in frag length. */
917 dwarf2dbg_relax_frag (frag)
918 fragS *frag;
920 int old_size, new_size;
922 old_size = frag->fr_subtype;
923 new_size = dwarf2dbg_estimate_size_before_relax (frag);
925 return new_size - old_size;
928 /* This function converts a rs_dwarf2dbg variant frag into a normal
929 fill frag. This is called after all relaxation has been done.
930 fr_subtype will be the desired length of the frag. */
932 void
933 dwarf2dbg_convert_frag (frag)
934 fragS *frag;
936 offsetT addr_diff;
938 addr_diff = resolve_symbol_value (frag->fr_symbol);
940 /* fr_var carries the max_chars that we created the fragment with.
941 fr_subtype carries the current expected length. We must, of
942 course, have allocated enough memory earlier. */
943 assert (frag->fr_var >= (int) frag->fr_subtype);
945 emit_inc_line_addr (frag->fr_offset, addr_diff,
946 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
948 frag->fr_fix += frag->fr_subtype;
949 frag->fr_type = rs_fill;
950 frag->fr_var = 0;
951 frag->fr_offset = 0;
954 /* Generate .debug_line content for the chain of line number entries
955 beginning at E, for segment SEG. */
957 static void
958 process_entries (seg, e)
959 segT seg;
960 struct line_entry *e;
962 unsigned filenum = 1;
963 unsigned line = 1;
964 unsigned column = 0;
965 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_BEGIN_STMT : 0;
966 fragS *frag = NULL;
967 fragS *last_frag;
968 addressT frag_ofs = 0;
969 addressT last_frag_ofs;
970 struct line_entry *next;
972 while (e)
974 int changed = 0;
976 if (filenum != e->loc.filenum)
978 filenum = e->loc.filenum;
979 out_opcode (DW_LNS_set_file);
980 out_uleb128 (filenum);
981 changed = 1;
984 if (column != e->loc.column)
986 column = e->loc.column;
987 out_opcode (DW_LNS_set_column);
988 out_uleb128 (column);
989 changed = 1;
992 if ((e->loc.flags ^ flags) & DWARF2_FLAG_BEGIN_STMT)
994 flags = e->loc.flags;
995 out_opcode (DW_LNS_negate_stmt);
996 changed = 1;
999 if (e->loc.flags & DWARF2_FLAG_BEGIN_BLOCK)
1001 out_opcode (DW_LNS_set_basic_block);
1002 changed = 1;
1005 /* Don't try to optimize away redundant entries; gdb wants two
1006 entries for a function where the code starts on the same line as
1007 the {, and there's no way to identify that case here. Trust gcc
1008 to optimize appropriately. */
1009 if (1 /* line != e->loc.line || changed */)
1011 int line_delta = e->loc.line - line;
1012 if (frag == NULL)
1014 out_set_addr (seg, e->frag, e->frag_ofs);
1015 out_inc_line_addr (line_delta, 0);
1017 else if (frag == e->frag)
1018 out_inc_line_addr (line_delta, e->frag_ofs - frag_ofs);
1019 else
1020 relax_inc_line_addr (line_delta, seg, e->frag, e->frag_ofs,
1021 frag, frag_ofs);
1023 frag = e->frag;
1024 frag_ofs = e->frag_ofs;
1025 line = e->loc.line;
1027 else if (frag == NULL)
1029 out_set_addr (seg, e->frag, e->frag_ofs);
1030 frag = e->frag;
1031 frag_ofs = e->frag_ofs;
1034 next = e->next;
1035 free (e);
1036 e = next;
1039 /* Emit a DW_LNE_end_sequence for the end of the section. */
1040 last_frag = last_frag_for_seg (seg);
1041 last_frag_ofs = get_frag_fix (last_frag);
1042 if (frag == last_frag)
1043 out_inc_line_addr (INT_MAX, last_frag_ofs - frag_ofs);
1044 else
1045 relax_inc_line_addr (INT_MAX, seg, last_frag, last_frag_ofs,
1046 frag, frag_ofs);
1049 /* Emit the directory and file tables for .debug_line. */
1051 static void
1052 out_file_list ()
1054 size_t size;
1055 char *cp;
1056 unsigned int i;
1058 /* Emit directory list. */
1059 for (i = 1; i < dirs_in_use; ++i)
1061 size = strlen (dirs[i]) + 1;
1062 cp = frag_more (size);
1063 memcpy (cp, dirs[i], size);
1065 /* Terminate it. */
1066 out_byte ('\0');
1068 for (i = 1; i < files_in_use; ++i)
1070 if (files[i].filename == NULL)
1072 as_bad (_("unassigned file number %ld"), (long) i);
1073 continue;
1076 size = strlen (files[i].filename) + 1;
1077 cp = frag_more (size);
1078 memcpy (cp, files[i].filename, size);
1080 out_uleb128 (files[i].dir); /* directory number */
1081 out_uleb128 (0); /* last modification timestamp */
1082 out_uleb128 (0); /* filesize */
1085 /* Terminate filename list. */
1086 out_byte (0);
1089 /* Emit the collected .debug_line data. */
1091 static void
1092 out_debug_line (line_seg)
1093 segT line_seg;
1095 expressionS expr;
1096 symbolS *line_start;
1097 symbolS *prologue_end;
1098 symbolS *line_end;
1099 struct line_seg *s;
1100 enum dwarf2_format d2f;
1101 int sizeof_offset;
1103 subseg_set (line_seg, 0);
1105 line_start = symbol_new_now ();
1106 prologue_end = symbol_make (fake_label_name);
1107 line_end = symbol_make (fake_label_name);
1109 /* Total length of the information for this compilation unit. */
1110 expr.X_op = O_subtract;
1111 expr.X_add_symbol = line_end;
1112 expr.X_op_symbol = line_start;
1114 d2f = DWARF2_FORMAT ();
1115 if (d2f == dwarf2_format_32bit)
1117 expr.X_add_number = -4;
1118 emit_expr (&expr, 4);
1119 sizeof_offset = 4;
1121 else if (d2f == dwarf2_format_64bit)
1123 expr.X_add_number = -12;
1124 out_four (-1);
1125 emit_expr (&expr, 8);
1126 sizeof_offset = 8;
1128 else if (d2f == dwarf2_format_64bit_irix)
1130 expr.X_add_number = -8;
1131 emit_expr (&expr, 8);
1132 sizeof_offset = 8;
1134 else
1136 as_fatal (_("internal error: unknown dwarf2 format"));
1139 /* Version. */
1140 out_two (2);
1142 /* Length of the prologue following this length. */
1143 expr.X_op = O_subtract;
1144 expr.X_add_symbol = prologue_end;
1145 expr.X_op_symbol = line_start;
1146 expr.X_add_number = - (4 + 2 + 4);
1147 emit_expr (&expr, sizeof_offset);
1149 /* Parameters of the state machine. */
1150 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1151 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1152 out_byte (DWARF2_LINE_BASE);
1153 out_byte (DWARF2_LINE_RANGE);
1154 out_byte (DWARF2_LINE_OPCODE_BASE);
1156 /* Standard opcode lengths. */
1157 out_byte (0); /* DW_LNS_copy */
1158 out_byte (1); /* DW_LNS_advance_pc */
1159 out_byte (1); /* DW_LNS_advance_line */
1160 out_byte (1); /* DW_LNS_set_file */
1161 out_byte (1); /* DW_LNS_set_column */
1162 out_byte (0); /* DW_LNS_negate_stmt */
1163 out_byte (0); /* DW_LNS_set_basic_block */
1164 out_byte (0); /* DW_LNS_const_add_pc */
1165 out_byte (1); /* DW_LNS_fixed_advance_pc */
1167 out_file_list ();
1169 set_symbol_value_now (prologue_end);
1171 /* For each section, emit a statement program. */
1172 for (s = all_segs; s; s = s->next)
1173 process_entries (s->seg, s->head->head);
1175 set_symbol_value_now (line_end);
1178 /* Emit data for .debug_aranges. */
1180 static void
1181 out_debug_aranges (aranges_seg, info_seg)
1182 segT aranges_seg;
1183 segT info_seg;
1185 unsigned int addr_size = sizeof_address;
1186 addressT size, skip;
1187 struct line_seg *s;
1188 expressionS expr;
1189 char *p;
1191 size = 4 + 2 + 4 + 1 + 1;
1193 skip = 2 * addr_size - (size & (2 * addr_size - 1));
1194 if (skip == 2 * addr_size)
1195 skip = 0;
1196 size += skip;
1198 for (s = all_segs; s; s = s->next)
1199 size += 2 * addr_size;
1201 size += 2 * addr_size;
1203 subseg_set (aranges_seg, 0);
1205 /* Length of the compilation unit. */
1206 out_four (size - 4);
1208 /* Version. */
1209 out_two (2);
1211 /* Offset to .debug_info. */
1212 expr.X_op = O_symbol;
1213 expr.X_add_symbol = section_symbol (info_seg);
1214 expr.X_add_number = 0;
1215 emit_expr (&expr, 4);
1217 /* Size of an address (offset portion). */
1218 out_byte (addr_size);
1220 /* Size of a segment descriptor. */
1221 out_byte (0);
1223 /* Align the header. */
1224 if (skip)
1225 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1227 for (s = all_segs; s; s = s->next)
1229 fragS *frag;
1230 symbolS *beg, *end;
1232 frag = first_frag_for_seg (s->seg);
1233 beg = symbol_new (fake_label_name, s->seg, 0, frag);
1234 s->text_start = beg;
1236 frag = last_frag_for_seg (s->seg);
1237 end = symbol_new (fake_label_name, s->seg, get_frag_fix (frag), frag);
1238 s->text_end = end;
1240 expr.X_op = O_symbol;
1241 expr.X_add_symbol = beg;
1242 expr.X_add_number = 0;
1243 emit_expr (&expr, addr_size);
1245 expr.X_op = O_subtract;
1246 expr.X_add_symbol = end;
1247 expr.X_op_symbol = beg;
1248 expr.X_add_number = 0;
1249 emit_expr (&expr, addr_size);
1252 p = frag_more (2 * addr_size);
1253 md_number_to_chars (p, 0, addr_size);
1254 md_number_to_chars (p + addr_size, 0, addr_size);
1257 /* Emit data for .debug_abbrev. Note that this must be kept in
1258 sync with out_debug_info below. */
1260 static void
1261 out_debug_abbrev (abbrev_seg)
1262 segT abbrev_seg;
1264 subseg_set (abbrev_seg, 0);
1266 out_uleb128 (1);
1267 out_uleb128 (DW_TAG_compile_unit);
1268 out_byte (DW_CHILDREN_no);
1269 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1270 if (all_segs->next == NULL)
1272 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1273 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1275 out_abbrev (DW_AT_name, DW_FORM_string);
1276 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1277 out_abbrev (DW_AT_producer, DW_FORM_string);
1278 out_abbrev (DW_AT_language, DW_FORM_data2);
1279 out_abbrev (0, 0);
1281 /* Terminate the abbreviations for this compilation unit. */
1282 out_byte (0);
1285 /* Emit a description of this compilation unit for .debug_info. */
1287 static void
1288 out_debug_info (info_seg, abbrev_seg, line_seg)
1289 segT info_seg;
1290 segT abbrev_seg;
1291 segT line_seg;
1293 char producer[128];
1294 char *comp_dir;
1295 expressionS expr;
1296 symbolS *info_start;
1297 symbolS *info_end;
1298 char *p;
1299 int len;
1300 enum dwarf2_format d2f;
1301 int sizeof_offset;
1303 subseg_set (info_seg, 0);
1305 info_start = symbol_new_now ();
1306 info_end = symbol_make (fake_label_name);
1308 /* Compilation Unit length. */
1309 expr.X_op = O_subtract;
1310 expr.X_add_symbol = info_end;
1311 expr.X_op_symbol = info_start;
1313 d2f = DWARF2_FORMAT ();
1314 if (d2f == dwarf2_format_32bit)
1316 expr.X_add_number = -4;
1317 emit_expr (&expr, 4);
1318 sizeof_offset = 4;
1320 else if (d2f == dwarf2_format_64bit)
1322 expr.X_add_number = -12;
1323 out_four (-1);
1324 emit_expr (&expr, 8);
1325 sizeof_offset = 8;
1327 else if (d2f == dwarf2_format_64bit_irix)
1329 expr.X_add_number = -8;
1330 emit_expr (&expr, 8);
1331 sizeof_offset = 8;
1333 else
1335 as_fatal (_("internal error: unknown dwarf2 format"));
1338 /* DWARF version. */
1339 out_two (2);
1341 /* .debug_abbrev offset */
1342 expr.X_op = O_symbol;
1343 expr.X_add_symbol = section_symbol (abbrev_seg);
1344 expr.X_add_number = 0;
1345 emit_expr (&expr, sizeof_offset);
1347 /* Target address size. */
1348 out_byte (sizeof_address);
1350 /* DW_TAG_compile_unit DIE abbrev */
1351 out_uleb128 (1);
1353 /* DW_AT_stmt_list */
1354 expr.X_op = O_symbol;
1355 expr.X_add_symbol = section_symbol (line_seg);
1356 expr.X_add_number = 0;
1357 emit_expr (&expr, 4);
1359 /* These two attributes may only be emitted if all of the code is
1360 contiguous. Multiple sections are not that. */
1361 if (all_segs->next == NULL)
1363 /* DW_AT_low_pc */
1364 expr.X_op = O_symbol;
1365 expr.X_add_symbol = all_segs->text_start;
1366 expr.X_add_number = 0;
1367 emit_expr (&expr, sizeof_address);
1369 /* DW_AT_high_pc */
1370 expr.X_op = O_symbol;
1371 expr.X_add_symbol = all_segs->text_end;
1372 expr.X_add_number = 0;
1373 emit_expr (&expr, sizeof_address);
1376 /* DW_AT_name. We don't have the actual file name that was present
1377 on the command line, so assume files[1] is the main input file.
1378 We're not supposed to get called unless at least one line number
1379 entry was emitted, so this should always be defined. */
1380 if (!files || files_in_use < 1)
1381 abort ();
1382 if (files[1].dir)
1384 len = strlen (dirs[files[1].dir]);
1385 p = frag_more (len + 1);
1386 memcpy (p, dirs[files[1].dir], len);
1387 p[len] = '/';
1389 len = strlen (files[1].filename) + 1;
1390 p = frag_more (len);
1391 memcpy (p, files[1].filename, len);
1393 /* DW_AT_comp_dir */
1394 comp_dir = getpwd ();
1395 len = strlen (comp_dir) + 1;
1396 p = frag_more (len);
1397 memcpy (p, comp_dir, len);
1399 /* DW_AT_producer */
1400 sprintf (producer, "GNU AS %s", VERSION);
1401 len = strlen (producer) + 1;
1402 p = frag_more (len);
1403 memcpy (p, producer, len);
1405 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1406 dwarf2 draft has no standard code for assembler. */
1407 out_two (DW_LANG_Mips_Assembler);
1409 set_symbol_value_now (info_end);
1412 void
1413 dwarf2_finish ()
1415 segT line_seg;
1416 struct line_seg *s;
1418 /* We don't need to do anything unless:
1419 - Some debug information was recorded via .file/.loc
1420 - or, we are generating DWARF2 information ourself (--gdwarf2)
1421 - or, there is a user-provided .debug_info section which could
1422 reference the file table in the .debug_line section we generate
1423 below. */
1424 if (all_segs == NULL
1425 && debug_type != DEBUG_DWARF2
1426 && bfd_get_section_by_name (stdoutput, ".debug_info") == NULL)
1427 return;
1429 /* Calculate the size of an address for the target machine. */
1430 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1432 /* Create and switch to the line number section. */
1433 line_seg = subseg_new (".debug_line", 0);
1434 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY);
1436 /* For each subsection, chain the debug entries together. */
1437 for (s = all_segs; s; s = s->next)
1439 struct line_subseg *ss = s->head;
1440 struct line_entry **ptail = ss->ptail;
1442 while ((ss = ss->next) != NULL)
1444 *ptail = ss->head;
1445 ptail = ss->ptail;
1449 out_debug_line (line_seg);
1451 /* If this is assembler generated line info, we need .debug_info
1452 and .debug_abbrev sections as well. */
1453 if (all_segs != NULL && debug_type == DEBUG_DWARF2)
1455 segT abbrev_seg;
1456 segT info_seg;
1457 segT aranges_seg;
1459 info_seg = subseg_new (".debug_info", 0);
1460 abbrev_seg = subseg_new (".debug_abbrev", 0);
1461 aranges_seg = subseg_new (".debug_aranges", 0);
1463 bfd_set_section_flags (stdoutput, info_seg, SEC_READONLY);
1464 bfd_set_section_flags (stdoutput, abbrev_seg, SEC_READONLY);
1465 bfd_set_section_flags (stdoutput, aranges_seg, SEC_READONLY);
1467 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1469 out_debug_aranges (aranges_seg, info_seg);
1470 out_debug_abbrev (abbrev_seg);
1471 out_debug_info (info_seg, abbrev_seg, line_seg);
1475 #else
1476 void
1477 dwarf2_finish ()
1482 dwarf2dbg_estimate_size_before_relax (frag)
1483 fragS *frag ATTRIBUTE_UNUSED;
1485 as_fatal (_("dwarf2 is not supported for this object file format"));
1486 return 0;
1490 dwarf2dbg_relax_frag (frag)
1491 fragS *frag ATTRIBUTE_UNUSED;
1493 as_fatal (_("dwarf2 is not supported for this object file format"));
1494 return 0;
1497 void
1498 dwarf2dbg_convert_frag (frag)
1499 fragS *frag ATTRIBUTE_UNUSED;
1501 as_fatal (_("dwarf2 is not supported for this object file format"));
1504 void
1505 dwarf2_emit_insn (size)
1506 int size ATTRIBUTE_UNUSED;
1510 char *
1511 dwarf2_directive_file (dummy)
1512 int dummy ATTRIBUTE_UNUSED;
1514 s_app_file (0);
1515 return NULL;
1518 void
1519 dwarf2_directive_loc (dummy)
1520 int dummy ATTRIBUTE_UNUSED;
1522 as_fatal (_("dwarf2 is not supported for this object file format"));
1524 #endif /* BFD_ASSEMBLER */