2003-10-06 Dave Brolley <brolley@redhat.com>
[binutils.git] / gas / dwarf2dbg.c
blobd1b3cf803439e4c637fbed852642c6eb7e85b497
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
55 #ifdef BFD_ASSEMBLER
57 #include "subsegs.h"
59 #include "elf/dwarf2.h"
61 /* Since we can't generate the prolog until the body is complete, we
62 use three different subsegments for .debug_line: one holding the
63 prolog, one for the directory and filename info, and one for the
64 body ("statement program"). */
65 #define DL_PROLOG 0
66 #define DL_FILES 1
67 #define DL_BODY 2
69 /* First special line opcde - leave room for the standard opcodes.
70 Note: If you want to change this, you'll have to update the
71 "standard_opcode_lengths" table that is emitted below in
72 dwarf2_finish(). */
73 #define DWARF2_LINE_OPCODE_BASE 10
75 #ifndef DWARF2_LINE_BASE
76 /* Minimum line offset in a special line info. opcode. This value
77 was chosen to give a reasonable range of values. */
78 # define DWARF2_LINE_BASE -5
79 #endif
81 /* Range of line offsets in a special line info. opcode. */
82 #ifndef DWARF2_LINE_RANGE
83 # define DWARF2_LINE_RANGE 14
84 #endif
86 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
87 /* Define the architecture-dependent minimum instruction length (in
88 bytes). This value should be rather too small than too big. */
89 # define DWARF2_LINE_MIN_INSN_LENGTH 1
90 #endif
92 /* Flag that indicates the initial value of the is_stmt_start flag.
93 In the present implementation, we do not mark any lines as
94 the beginning of a source statement, because that information
95 is not made available by the GCC front-end. */
96 #define DWARF2_LINE_DEFAULT_IS_STMT 1
98 /* Given a special op, return the line skip amount. */
99 #define SPECIAL_LINE(op) \
100 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
102 /* Given a special op, return the address skip amount (in units of
103 DWARF2_LINE_MIN_INSN_LENGTH. */
104 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
106 /* The maximum address skip amount that can be encoded with a special op. */
107 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
109 struct line_entry {
110 struct line_entry *next;
111 fragS *frag;
112 addressT frag_ofs;
113 struct dwarf2_line_info loc;
116 struct line_subseg {
117 struct line_subseg *next;
118 subsegT subseg;
119 struct line_entry *head;
120 struct line_entry **ptail;
123 struct line_seg {
124 struct line_seg *next;
125 segT seg;
126 struct line_subseg *head;
127 symbolS *text_start;
128 symbolS *text_end;
131 /* Collects data for all line table entries during assembly. */
132 static struct line_seg *all_segs;
134 struct file_entry {
135 const char *filename;
136 unsigned int dir;
139 /* Table of files used by .debug_line. */
140 static struct file_entry *files;
141 static unsigned int files_in_use;
142 static unsigned int files_allocated;
144 /* Table of directories used by .debug_line. */
145 static char **dirs;
146 static unsigned int dirs_in_use;
147 static unsigned int dirs_allocated;
149 /* TRUE when we've seen a .loc directive recently. Used to avoid
150 doing work when there's nothing to do. */
151 static bfd_boolean loc_directive_seen;
153 /* Current location as indicated by the most recent .loc directive. */
154 static struct dwarf2_line_info current;
156 /* The size of an address on the target. */
157 static unsigned int sizeof_address;
159 static struct line_subseg *get_line_subseg PARAMS ((segT, subsegT));
160 static unsigned int get_filenum PARAMS ((const char *, unsigned int));
161 static struct frag *first_frag_for_seg PARAMS ((segT));
162 static struct frag *last_frag_for_seg PARAMS ((segT));
163 static void out_byte PARAMS ((int));
164 static void out_opcode PARAMS ((int));
165 static void out_two PARAMS ((int));
166 static void out_four PARAMS ((int));
167 static void out_abbrev PARAMS ((int, int));
168 static void out_uleb128 PARAMS ((addressT));
169 static offsetT get_frag_fix PARAMS ((fragS *));
170 static void out_set_addr PARAMS ((segT, fragS *, addressT));
171 static int size_inc_line_addr PARAMS ((int, addressT));
172 static void emit_inc_line_addr PARAMS ((int, addressT, char *, int));
173 static void out_inc_line_addr PARAMS ((int, addressT));
174 static void relax_inc_line_addr PARAMS ((int, segT, fragS *, addressT,
175 fragS *, addressT));
176 static void process_entries PARAMS ((segT, struct line_entry *));
177 static void out_file_list PARAMS ((void));
178 static void out_debug_line PARAMS ((segT));
179 static void out_debug_aranges PARAMS ((segT, segT));
180 static void out_debug_abbrev PARAMS ((segT));
181 static void out_debug_info PARAMS ((segT, segT, segT));
183 #ifndef TC_DWARF2_EMIT_OFFSET
184 # define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
185 static void generic_dwarf2_emit_offset PARAMS ((symbolS *, unsigned int));
187 /* Create an offset to .dwarf2_*. */
189 static void
190 generic_dwarf2_emit_offset (symbol, size)
191 symbolS *symbol;
192 unsigned int size;
194 expressionS expr;
196 expr.X_op = O_symbol;
197 expr.X_add_symbol = symbol;
198 expr.X_add_number = 0;
199 emit_expr (&expr, size);
201 #endif
203 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
205 static struct line_subseg *
206 get_line_subseg (seg, subseg)
207 segT seg;
208 subsegT subseg;
210 static segT last_seg;
211 static subsegT last_subseg;
212 static struct line_subseg *last_line_subseg;
214 struct line_seg *s;
215 struct line_subseg **pss, *ss;
217 if (seg == last_seg && subseg == last_subseg)
218 return last_line_subseg;
220 for (s = all_segs; s; s = s->next)
221 if (s->seg == seg)
222 goto found_seg;
224 s = (struct line_seg *) xmalloc (sizeof (*s));
225 s->next = all_segs;
226 s->seg = seg;
227 s->head = NULL;
228 all_segs = s;
230 found_seg:
231 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
233 if (ss->subseg == subseg)
234 goto found_subseg;
235 if (ss->subseg > subseg)
236 break;
239 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
240 ss->next = *pss;
241 ss->subseg = subseg;
242 ss->head = NULL;
243 ss->ptail = &ss->head;
244 *pss = ss;
246 found_subseg:
247 last_seg = seg;
248 last_subseg = subseg;
249 last_line_subseg = ss;
251 return ss;
254 /* Record an entry for LOC ocurring at OFS within the current fragment. */
256 void
257 dwarf2_gen_line_info (ofs, loc)
258 addressT ofs;
259 struct dwarf2_line_info *loc;
261 struct line_subseg *ss;
262 struct line_entry *e;
263 static unsigned int line = -1;
264 static unsigned int filenum = -1;
266 /* Early out for as-yet incomplete location information. */
267 if (loc->filenum == 0 || loc->line == 0)
268 return;
270 /* Don't emit sequences of line symbols for the same line when the
271 symbols apply to assembler code. It is necessary to emit
272 duplicate line symbols when a compiler asks for them, because GDB
273 uses them to determine the end of the prologue. */
274 if (debug_type == DEBUG_DWARF2
275 && line == loc->line && filenum == loc->filenum)
276 return;
278 line = loc->line;
279 filenum = loc->filenum;
281 e = (struct line_entry *) xmalloc (sizeof (*e));
282 e->next = NULL;
283 e->frag = frag_now;
284 e->frag_ofs = ofs;
285 e->loc = *loc;
287 ss = get_line_subseg (now_seg, now_subseg);
288 *ss->ptail = e;
289 ss->ptail = &e->next;
292 void
293 dwarf2_where (line)
294 struct dwarf2_line_info *line;
296 if (debug_type == DEBUG_DWARF2)
298 char *filename;
299 as_where (&filename, &line->line);
300 line->filenum = get_filenum (filename, 0);
301 line->column = 0;
302 line->flags = DWARF2_FLAG_BEGIN_STMT;
304 else
305 *line = current;
308 /* Called for each machine instruction, or relatively atomic group of
309 machine instructions (ie built-in macro). The instruction or group
310 is SIZE bytes in length. If dwarf2 line number generation is called
311 for, emit a line statement appropriately. */
313 void
314 dwarf2_emit_insn (size)
315 int size;
317 struct dwarf2_line_info loc;
319 if (loc_directive_seen)
321 /* Use the last location established by a .loc directive, not
322 the value returned by dwarf2_where(). That calls as_where()
323 which will return either the logical input file name (foo.c)
324 or the physical input file name (foo.s) and not the file name
325 specified in the most recent .loc directive (eg foo.h). */
326 loc = current;
328 /* Unless we generate DWARF2 debugging information for each
329 assembler line, we only emit one line symbol for one LOC. */
330 if (debug_type != DEBUG_DWARF2)
331 loc_directive_seen = FALSE;
333 else if (debug_type != DEBUG_DWARF2)
334 return;
335 else
336 dwarf2_where (& loc);
338 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
341 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
342 allocate it on that file table slot, otherwise return the first
343 empty one. */
345 static unsigned int
346 get_filenum (filename, num)
347 const char *filename;
348 unsigned int num;
350 static unsigned int last_used, last_used_dir_len;
351 const char *file;
352 size_t dir_len;
353 unsigned int i, dir;
355 if (num == 0 && last_used)
357 if (! files[last_used].dir
358 && strcmp (filename, files[last_used].filename) == 0)
359 return last_used;
360 if (files[last_used].dir
361 && strncmp (filename, dirs[files[last_used].dir],
362 last_used_dir_len) == 0
363 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
364 && strcmp (filename + last_used_dir_len + 1,
365 files[last_used].filename) == 0)
366 return last_used;
369 file = lbasename (filename);
370 /* Don't make empty string from / or A: from A:/ . */
371 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
372 if (file <= filename + 3)
373 file = filename;
374 #else
375 if (file == filename + 1)
376 file = filename;
377 #endif
378 dir_len = file - filename;
380 dir = 0;
381 if (dir_len)
383 --dir_len;
384 for (dir = 1; dir < dirs_in_use; ++dir)
385 if (memcmp (filename, dirs[dir], dir_len) == 0
386 && dirs[dir][dir_len] == '\0')
387 break;
389 if (dir >= dirs_in_use)
391 if (dir >= dirs_allocated)
393 dirs_allocated = dir + 32;
394 dirs = (char **)
395 xrealloc (dirs, (dir + 32) * sizeof (const char *));
398 dirs[dir] = xmalloc (dir_len + 1);
399 memcpy (dirs[dir], filename, dir_len);
400 dirs[dir][dir_len] = '\0';
401 dirs_in_use = dir + 1;
405 if (num == 0)
407 for (i = 1; i < files_in_use; ++i)
408 if (files[i].dir == dir
409 && files[i].filename
410 && strcmp (file, files[i].filename) == 0)
412 last_used = i;
413 last_used_dir_len = dir_len;
414 return i;
417 else
418 i = num;
420 if (i >= files_allocated)
422 unsigned int old = files_allocated;
424 files_allocated = i + 32;
425 files = (struct file_entry *)
426 xrealloc (files, (i + 32) * sizeof (struct file_entry));
428 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
431 files[i].filename = num ? file : xstrdup (file);
432 files[i].dir = dir;
433 files_in_use = i + 1;
434 last_used = i;
435 last_used_dir_len = dir_len;
437 return i;
440 /* Handle two forms of .file directive:
441 - Pass .file "source.c" to s_app_file
442 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
444 If an entry is added to the file table, return a pointer to the filename. */
446 char *
447 dwarf2_directive_file (dummy)
448 int dummy ATTRIBUTE_UNUSED;
450 offsetT num;
451 char *filename;
452 int filename_len;
454 /* Continue to accept a bare string and pass it off. */
455 SKIP_WHITESPACE ();
456 if (*input_line_pointer == '"')
458 s_app_file (0);
459 return NULL;
462 num = get_absolute_expression ();
463 filename = demand_copy_C_string (&filename_len);
464 demand_empty_rest_of_line ();
466 if (num < 1)
468 as_bad (_("file number less than one"));
469 return NULL;
472 if (num < (int) files_in_use && files[num].filename != 0)
474 as_bad (_("file number %ld already allocated"), (long) num);
475 return NULL;
478 get_filenum (filename, num);
480 return filename;
483 void
484 dwarf2_directive_loc (dummy)
485 int dummy ATTRIBUTE_UNUSED;
487 offsetT filenum, line, column;
489 filenum = get_absolute_expression ();
490 SKIP_WHITESPACE ();
491 line = get_absolute_expression ();
492 SKIP_WHITESPACE ();
493 column = get_absolute_expression ();
494 demand_empty_rest_of_line ();
496 if (filenum < 1)
498 as_bad (_("file number less than one"));
499 return;
501 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
503 as_bad (_("unassigned file number %ld"), (long) filenum);
504 return;
507 current.filenum = filenum;
508 current.line = line;
509 current.column = column;
510 current.flags = DWARF2_FLAG_BEGIN_STMT;
512 loc_directive_seen = TRUE;
514 #ifndef NO_LISTING
515 if (listing)
517 if (files[filenum].dir)
519 size_t dir_len = strlen (dirs[files[filenum].dir]);
520 size_t file_len = strlen (files[filenum].filename);
521 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
523 memcpy (cp, dirs[files[filenum].dir], dir_len);
524 cp[dir_len] = '/';
525 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
526 cp[dir_len + file_len + 1] = '\0';
527 listing_source_file (cp);
529 else
530 listing_source_file (files[filenum].filename);
531 listing_source_line (line);
533 #endif
536 static struct frag *
537 first_frag_for_seg (seg)
538 segT seg;
540 frchainS *f, *first = NULL;
542 for (f = frchain_root; f; f = f->frch_next)
543 if (f->frch_seg == seg
544 && (! first || first->frch_subseg > f->frch_subseg))
545 first = f;
547 return first ? first->frch_root : NULL;
550 static struct frag *
551 last_frag_for_seg (seg)
552 segT seg;
554 frchainS *f, *last = NULL;
556 for (f = frchain_root; f; f = f->frch_next)
557 if (f->frch_seg == seg
558 && (! last || last->frch_subseg < f->frch_subseg))
559 last= f;
561 return last ? last->frch_last : NULL;
564 /* Emit a single byte into the current segment. */
566 static inline void
567 out_byte (byte)
568 int byte;
570 FRAG_APPEND_1_CHAR (byte);
573 /* Emit a statement program opcode into the current segment. */
575 static inline void
576 out_opcode (opc)
577 int opc;
579 out_byte (opc);
582 /* Emit a two-byte word into the current segment. */
584 static inline void
585 out_two (data)
586 int data;
588 md_number_to_chars (frag_more (2), data, 2);
591 /* Emit a four byte word into the current segment. */
593 static inline void
594 out_four (data)
595 int data;
597 md_number_to_chars (frag_more (4), data, 4);
600 /* Emit an unsigned "little-endian base 128" number. */
602 static void
603 out_uleb128 (value)
604 addressT value;
606 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
609 /* Emit a tuple for .debug_abbrev. */
611 static inline void
612 out_abbrev (name, form)
613 int name, form;
615 out_uleb128 (name);
616 out_uleb128 (form);
619 /* Get the size of a fragment. */
621 static offsetT
622 get_frag_fix (frag)
623 fragS *frag;
625 frchainS *fr;
627 if (frag->fr_next)
628 return frag->fr_fix;
630 /* If a fragment is the last in the chain, special measures must be
631 taken to find its size before relaxation, since it may be pending
632 on some subsegment chain. */
633 for (fr = frchain_root; fr; fr = fr->frch_next)
634 if (fr->frch_last == frag)
635 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
637 abort ();
640 /* Set an absolute address (may result in a relocation entry). */
642 static void
643 out_set_addr (seg, frag, ofs)
644 segT seg;
645 fragS *frag;
646 addressT ofs;
648 expressionS expr;
649 symbolS *sym;
651 sym = symbol_temp_new (seg, ofs, frag);
653 out_opcode (DW_LNS_extended_op);
654 out_uleb128 (sizeof_address + 1);
656 out_opcode (DW_LNE_set_address);
657 expr.X_op = O_symbol;
658 expr.X_add_symbol = sym;
659 expr.X_add_number = 0;
660 emit_expr (&expr, sizeof_address);
663 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
664 static void scale_addr_delta PARAMS ((addressT *));
666 static void
667 scale_addr_delta (addr_delta)
668 addressT *addr_delta;
670 static int printed_this = 0;
671 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
673 if (!printed_this)
674 as_bad("unaligned opcodes detected in executable segment");
675 printed_this = 1;
677 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
679 #else
680 #define scale_addr_delta(A)
681 #endif
683 /* Encode a pair of line and address skips as efficiently as possible.
684 Note that the line skip is signed, whereas the address skip is unsigned.
686 The following two routines *must* be kept in sync. This is
687 enforced by making emit_inc_line_addr abort if we do not emit
688 exactly the expected number of bytes. */
690 static int
691 size_inc_line_addr (line_delta, addr_delta)
692 int line_delta;
693 addressT addr_delta;
695 unsigned int tmp, opcode;
696 int len = 0;
698 /* Scale the address delta by the minimum instruction length. */
699 scale_addr_delta (&addr_delta);
701 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
702 We cannot use special opcodes here, since we want the end_sequence
703 to emit the matrix entry. */
704 if (line_delta == INT_MAX)
706 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
707 len = 1;
708 else
709 len = 1 + sizeof_leb128 (addr_delta, 0);
710 return len + 3;
713 /* Bias the line delta by the base. */
714 tmp = line_delta - DWARF2_LINE_BASE;
716 /* If the line increment is out of range of a special opcode, we
717 must encode it with DW_LNS_advance_line. */
718 if (tmp >= DWARF2_LINE_RANGE)
720 len = 1 + sizeof_leb128 (line_delta, 1);
721 line_delta = 0;
722 tmp = 0 - DWARF2_LINE_BASE;
725 /* Bias the opcode by the special opcode base. */
726 tmp += DWARF2_LINE_OPCODE_BASE;
728 /* Avoid overflow when addr_delta is large. */
729 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
731 /* Try using a special opcode. */
732 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
733 if (opcode <= 255)
734 return len + 1;
736 /* Try using DW_LNS_const_add_pc followed by special op. */
737 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
738 if (opcode <= 255)
739 return len + 2;
742 /* Otherwise use DW_LNS_advance_pc. */
743 len += 1 + sizeof_leb128 (addr_delta, 0);
745 /* DW_LNS_copy or special opcode. */
746 len += 1;
748 return len;
751 static void
752 emit_inc_line_addr (line_delta, addr_delta, p, len)
753 int line_delta;
754 addressT addr_delta;
755 char *p;
756 int len;
758 unsigned int tmp, opcode;
759 int need_copy = 0;
760 char *end = p + len;
762 /* Scale the address delta by the minimum instruction length. */
763 scale_addr_delta (&addr_delta);
765 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
766 We cannot use special opcodes here, since we want the end_sequence
767 to emit the matrix entry. */
768 if (line_delta == INT_MAX)
770 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
771 *p++ = DW_LNS_const_add_pc;
772 else
774 *p++ = DW_LNS_advance_pc;
775 p += output_leb128 (p, addr_delta, 0);
778 *p++ = DW_LNS_extended_op;
779 *p++ = 1;
780 *p++ = DW_LNE_end_sequence;
781 goto done;
784 /* Bias the line delta by the base. */
785 tmp = line_delta - DWARF2_LINE_BASE;
787 /* If the line increment is out of range of a special opcode, we
788 must encode it with DW_LNS_advance_line. */
789 if (tmp >= DWARF2_LINE_RANGE)
791 *p++ = DW_LNS_advance_line;
792 p += output_leb128 (p, line_delta, 1);
794 /* Prettier, I think, to use DW_LNS_copy instead of a
795 "line +0, addr +0" special opcode. */
796 if (addr_delta == 0)
798 *p++ = DW_LNS_copy;
799 goto done;
802 line_delta = 0;
803 tmp = 0 - DWARF2_LINE_BASE;
804 need_copy = 1;
807 /* Bias the opcode by the special opcode base. */
808 tmp += DWARF2_LINE_OPCODE_BASE;
810 /* Avoid overflow when addr_delta is large. */
811 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
813 /* Try using a special opcode. */
814 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
815 if (opcode <= 255)
817 *p++ = opcode;
818 goto done;
821 /* Try using DW_LNS_const_add_pc followed by special op. */
822 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
823 if (opcode <= 255)
825 *p++ = DW_LNS_const_add_pc;
826 *p++ = opcode;
827 goto done;
831 /* Otherwise use DW_LNS_advance_pc. */
832 *p++ = DW_LNS_advance_pc;
833 p += output_leb128 (p, addr_delta, 0);
835 if (need_copy)
836 *p++ = DW_LNS_copy;
837 else
838 *p++ = tmp;
840 done:
841 assert (p == end);
844 /* Handy routine to combine calls to the above two routines. */
846 static void
847 out_inc_line_addr (line_delta, addr_delta)
848 int line_delta;
849 addressT addr_delta;
851 int len = size_inc_line_addr (line_delta, addr_delta);
852 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
855 /* Generate a variant frag that we can use to relax address/line
856 increments between fragments of the target segment. */
858 static void
859 relax_inc_line_addr (line_delta, seg, to_frag, to_ofs, from_frag, from_ofs)
860 int line_delta;
861 segT seg;
862 fragS *to_frag, *from_frag;
863 addressT to_ofs, from_ofs;
865 symbolS *to_sym, *from_sym;
866 expressionS expr;
867 int max_chars;
869 to_sym = symbol_temp_new (seg, to_ofs, to_frag);
870 from_sym = symbol_temp_new (seg, from_ofs, from_frag);
872 expr.X_op = O_subtract;
873 expr.X_add_symbol = to_sym;
874 expr.X_op_symbol = from_sym;
875 expr.X_add_number = 0;
877 /* The maximum size of the frag is the line delta with a maximum
878 sized address delta. */
879 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
881 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
882 make_expr_symbol (&expr), line_delta, NULL);
885 /* The function estimates the size of a rs_dwarf2dbg variant frag
886 based on the current values of the symbols. It is called before
887 the relaxation loop. We set fr_subtype to the expected length. */
890 dwarf2dbg_estimate_size_before_relax (frag)
891 fragS *frag;
893 offsetT addr_delta;
894 int size;
896 addr_delta = resolve_symbol_value (frag->fr_symbol);
897 size = size_inc_line_addr (frag->fr_offset, addr_delta);
899 frag->fr_subtype = size;
901 return size;
904 /* This function relaxes a rs_dwarf2dbg variant frag based on the
905 current values of the symbols. fr_subtype is the current length
906 of the frag. This returns the change in frag length. */
909 dwarf2dbg_relax_frag (frag)
910 fragS *frag;
912 int old_size, new_size;
914 old_size = frag->fr_subtype;
915 new_size = dwarf2dbg_estimate_size_before_relax (frag);
917 return new_size - old_size;
920 /* This function converts a rs_dwarf2dbg variant frag into a normal
921 fill frag. This is called after all relaxation has been done.
922 fr_subtype will be the desired length of the frag. */
924 void
925 dwarf2dbg_convert_frag (frag)
926 fragS *frag;
928 offsetT addr_diff;
930 addr_diff = resolve_symbol_value (frag->fr_symbol);
932 /* fr_var carries the max_chars that we created the fragment with.
933 fr_subtype carries the current expected length. We must, of
934 course, have allocated enough memory earlier. */
935 assert (frag->fr_var >= (int) frag->fr_subtype);
937 emit_inc_line_addr (frag->fr_offset, addr_diff,
938 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
940 frag->fr_fix += frag->fr_subtype;
941 frag->fr_type = rs_fill;
942 frag->fr_var = 0;
943 frag->fr_offset = 0;
946 /* Generate .debug_line content for the chain of line number entries
947 beginning at E, for segment SEG. */
949 static void
950 process_entries (seg, e)
951 segT seg;
952 struct line_entry *e;
954 unsigned filenum = 1;
955 unsigned line = 1;
956 unsigned column = 0;
957 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_BEGIN_STMT : 0;
958 fragS *frag = NULL;
959 fragS *last_frag;
960 addressT frag_ofs = 0;
961 addressT last_frag_ofs;
962 struct line_entry *next;
964 while (e)
966 int changed = 0;
968 if (filenum != e->loc.filenum)
970 filenum = e->loc.filenum;
971 out_opcode (DW_LNS_set_file);
972 out_uleb128 (filenum);
973 changed = 1;
976 if (column != e->loc.column)
978 column = e->loc.column;
979 out_opcode (DW_LNS_set_column);
980 out_uleb128 (column);
981 changed = 1;
984 if ((e->loc.flags ^ flags) & DWARF2_FLAG_BEGIN_STMT)
986 flags = e->loc.flags;
987 out_opcode (DW_LNS_negate_stmt);
988 changed = 1;
991 if (e->loc.flags & DWARF2_FLAG_BEGIN_BLOCK)
993 out_opcode (DW_LNS_set_basic_block);
994 changed = 1;
997 /* Don't try to optimize away redundant entries; gdb wants two
998 entries for a function where the code starts on the same line as
999 the {, and there's no way to identify that case here. Trust gcc
1000 to optimize appropriately. */
1001 if (1 /* line != e->loc.line || changed */)
1003 int line_delta = e->loc.line - line;
1004 if (frag == NULL)
1006 out_set_addr (seg, e->frag, e->frag_ofs);
1007 out_inc_line_addr (line_delta, 0);
1009 else if (frag == e->frag)
1010 out_inc_line_addr (line_delta, e->frag_ofs - frag_ofs);
1011 else
1012 relax_inc_line_addr (line_delta, seg, e->frag, e->frag_ofs,
1013 frag, frag_ofs);
1015 frag = e->frag;
1016 frag_ofs = e->frag_ofs;
1017 line = e->loc.line;
1019 else if (frag == NULL)
1021 out_set_addr (seg, e->frag, e->frag_ofs);
1022 frag = e->frag;
1023 frag_ofs = e->frag_ofs;
1026 next = e->next;
1027 free (e);
1028 e = next;
1031 /* Emit a DW_LNE_end_sequence for the end of the section. */
1032 last_frag = last_frag_for_seg (seg);
1033 last_frag_ofs = get_frag_fix (last_frag);
1034 if (frag == last_frag)
1035 out_inc_line_addr (INT_MAX, last_frag_ofs - frag_ofs);
1036 else
1037 relax_inc_line_addr (INT_MAX, seg, last_frag, last_frag_ofs,
1038 frag, frag_ofs);
1041 /* Emit the directory and file tables for .debug_line. */
1043 static void
1044 out_file_list ()
1046 size_t size;
1047 char *cp;
1048 unsigned int i;
1050 /* Emit directory list. */
1051 for (i = 1; i < dirs_in_use; ++i)
1053 size = strlen (dirs[i]) + 1;
1054 cp = frag_more (size);
1055 memcpy (cp, dirs[i], size);
1057 /* Terminate it. */
1058 out_byte ('\0');
1060 for (i = 1; i < files_in_use; ++i)
1062 if (files[i].filename == NULL)
1064 as_bad (_("unassigned file number %ld"), (long) i);
1065 /* Prevent a crash later, particularly for file 1. */
1066 files[i].filename = "";
1067 continue;
1070 size = strlen (files[i].filename) + 1;
1071 cp = frag_more (size);
1072 memcpy (cp, files[i].filename, size);
1074 out_uleb128 (files[i].dir); /* directory number */
1075 out_uleb128 (0); /* last modification timestamp */
1076 out_uleb128 (0); /* filesize */
1079 /* Terminate filename list. */
1080 out_byte (0);
1083 /* Emit the collected .debug_line data. */
1085 static void
1086 out_debug_line (line_seg)
1087 segT line_seg;
1089 expressionS expr;
1090 symbolS *line_start;
1091 symbolS *prologue_end;
1092 symbolS *line_end;
1093 struct line_seg *s;
1094 enum dwarf2_format d2f;
1095 int sizeof_offset;
1097 subseg_set (line_seg, 0);
1099 line_start = symbol_temp_new_now ();
1100 prologue_end = symbol_temp_make ();
1101 line_end = symbol_temp_make ();
1103 /* Total length of the information for this compilation unit. */
1104 expr.X_op = O_subtract;
1105 expr.X_add_symbol = line_end;
1106 expr.X_op_symbol = line_start;
1108 d2f = DWARF2_FORMAT ();
1109 if (d2f == dwarf2_format_32bit)
1111 expr.X_add_number = -4;
1112 emit_expr (&expr, 4);
1113 sizeof_offset = 4;
1115 else if (d2f == dwarf2_format_64bit)
1117 expr.X_add_number = -12;
1118 out_four (-1);
1119 emit_expr (&expr, 8);
1120 sizeof_offset = 8;
1122 else if (d2f == dwarf2_format_64bit_irix)
1124 expr.X_add_number = -8;
1125 emit_expr (&expr, 8);
1126 sizeof_offset = 8;
1128 else
1130 as_fatal (_("internal error: unknown dwarf2 format"));
1133 /* Version. */
1134 out_two (2);
1136 /* Length of the prologue following this length. */
1137 expr.X_op = O_subtract;
1138 expr.X_add_symbol = prologue_end;
1139 expr.X_op_symbol = line_start;
1140 expr.X_add_number = - (4 + 2 + 4);
1141 emit_expr (&expr, sizeof_offset);
1143 /* Parameters of the state machine. */
1144 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1145 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1146 out_byte (DWARF2_LINE_BASE);
1147 out_byte (DWARF2_LINE_RANGE);
1148 out_byte (DWARF2_LINE_OPCODE_BASE);
1150 /* Standard opcode lengths. */
1151 out_byte (0); /* DW_LNS_copy */
1152 out_byte (1); /* DW_LNS_advance_pc */
1153 out_byte (1); /* DW_LNS_advance_line */
1154 out_byte (1); /* DW_LNS_set_file */
1155 out_byte (1); /* DW_LNS_set_column */
1156 out_byte (0); /* DW_LNS_negate_stmt */
1157 out_byte (0); /* DW_LNS_set_basic_block */
1158 out_byte (0); /* DW_LNS_const_add_pc */
1159 out_byte (1); /* DW_LNS_fixed_advance_pc */
1161 out_file_list ();
1163 symbol_set_value_now (prologue_end);
1165 /* For each section, emit a statement program. */
1166 for (s = all_segs; s; s = s->next)
1167 process_entries (s->seg, s->head->head);
1169 symbol_set_value_now (line_end);
1172 /* Emit data for .debug_aranges. */
1174 static void
1175 out_debug_aranges (aranges_seg, info_seg)
1176 segT aranges_seg;
1177 segT info_seg;
1179 unsigned int addr_size = sizeof_address;
1180 addressT size, skip;
1181 struct line_seg *s;
1182 expressionS expr;
1183 char *p;
1185 size = 4 + 2 + 4 + 1 + 1;
1187 skip = 2 * addr_size - (size & (2 * addr_size - 1));
1188 if (skip == 2 * addr_size)
1189 skip = 0;
1190 size += skip;
1192 for (s = all_segs; s; s = s->next)
1193 size += 2 * addr_size;
1195 size += 2 * addr_size;
1197 subseg_set (aranges_seg, 0);
1199 /* Length of the compilation unit. */
1200 out_four (size - 4);
1202 /* Version. */
1203 out_two (2);
1205 /* Offset to .debug_info. */
1206 /* ??? sizeof_offset */
1207 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), 4);
1209 /* Size of an address (offset portion). */
1210 out_byte (addr_size);
1212 /* Size of a segment descriptor. */
1213 out_byte (0);
1215 /* Align the header. */
1216 if (skip)
1217 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1219 for (s = all_segs; s; s = s->next)
1221 fragS *frag;
1222 symbolS *beg, *end;
1224 frag = first_frag_for_seg (s->seg);
1225 beg = symbol_temp_new (s->seg, 0, frag);
1226 s->text_start = beg;
1228 frag = last_frag_for_seg (s->seg);
1229 end = symbol_temp_new (s->seg, get_frag_fix (frag), frag);
1230 s->text_end = end;
1232 expr.X_op = O_symbol;
1233 expr.X_add_symbol = beg;
1234 expr.X_add_number = 0;
1235 emit_expr (&expr, addr_size);
1237 expr.X_op = O_subtract;
1238 expr.X_add_symbol = end;
1239 expr.X_op_symbol = beg;
1240 expr.X_add_number = 0;
1241 emit_expr (&expr, addr_size);
1244 p = frag_more (2 * addr_size);
1245 md_number_to_chars (p, 0, addr_size);
1246 md_number_to_chars (p + addr_size, 0, addr_size);
1249 /* Emit data for .debug_abbrev. Note that this must be kept in
1250 sync with out_debug_info below. */
1252 static void
1253 out_debug_abbrev (abbrev_seg)
1254 segT abbrev_seg;
1256 subseg_set (abbrev_seg, 0);
1258 out_uleb128 (1);
1259 out_uleb128 (DW_TAG_compile_unit);
1260 out_byte (DW_CHILDREN_no);
1261 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1262 if (all_segs->next == NULL)
1264 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1265 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1267 out_abbrev (DW_AT_name, DW_FORM_string);
1268 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1269 out_abbrev (DW_AT_producer, DW_FORM_string);
1270 out_abbrev (DW_AT_language, DW_FORM_data2);
1271 out_abbrev (0, 0);
1273 /* Terminate the abbreviations for this compilation unit. */
1274 out_byte (0);
1277 /* Emit a description of this compilation unit for .debug_info. */
1279 static void
1280 out_debug_info (info_seg, abbrev_seg, line_seg)
1281 segT info_seg;
1282 segT abbrev_seg;
1283 segT line_seg;
1285 char producer[128];
1286 char *comp_dir;
1287 expressionS expr;
1288 symbolS *info_start;
1289 symbolS *info_end;
1290 char *p;
1291 int len;
1292 enum dwarf2_format d2f;
1293 int sizeof_offset;
1295 subseg_set (info_seg, 0);
1297 info_start = symbol_temp_new_now ();
1298 info_end = symbol_temp_make ();
1300 /* Compilation Unit length. */
1301 expr.X_op = O_subtract;
1302 expr.X_add_symbol = info_end;
1303 expr.X_op_symbol = info_start;
1305 d2f = DWARF2_FORMAT ();
1306 if (d2f == dwarf2_format_32bit)
1308 expr.X_add_number = -4;
1309 emit_expr (&expr, 4);
1310 sizeof_offset = 4;
1312 else if (d2f == dwarf2_format_64bit)
1314 expr.X_add_number = -12;
1315 out_four (-1);
1316 emit_expr (&expr, 8);
1317 sizeof_offset = 8;
1319 else if (d2f == dwarf2_format_64bit_irix)
1321 expr.X_add_number = -8;
1322 emit_expr (&expr, 8);
1323 sizeof_offset = 8;
1325 else
1327 as_fatal (_("internal error: unknown dwarf2 format"));
1330 /* DWARF version. */
1331 out_two (2);
1333 /* .debug_abbrev offset */
1334 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1336 /* Target address size. */
1337 out_byte (sizeof_address);
1339 /* DW_TAG_compile_unit DIE abbrev */
1340 out_uleb128 (1);
1342 /* DW_AT_stmt_list */
1343 /* ??? sizeof_offset */
1344 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 4);
1346 /* These two attributes may only be emitted if all of the code is
1347 contiguous. Multiple sections are not that. */
1348 if (all_segs->next == NULL)
1350 /* DW_AT_low_pc */
1351 expr.X_op = O_symbol;
1352 expr.X_add_symbol = all_segs->text_start;
1353 expr.X_add_number = 0;
1354 emit_expr (&expr, sizeof_address);
1356 /* DW_AT_high_pc */
1357 expr.X_op = O_symbol;
1358 expr.X_add_symbol = all_segs->text_end;
1359 expr.X_add_number = 0;
1360 emit_expr (&expr, sizeof_address);
1363 /* DW_AT_name. We don't have the actual file name that was present
1364 on the command line, so assume files[1] is the main input file.
1365 We're not supposed to get called unless at least one line number
1366 entry was emitted, so this should always be defined. */
1367 if (!files || files_in_use < 1)
1368 abort ();
1369 if (files[1].dir)
1371 len = strlen (dirs[files[1].dir]);
1372 p = frag_more (len + 1);
1373 memcpy (p, dirs[files[1].dir], len);
1374 p[len] = '/';
1376 len = strlen (files[1].filename) + 1;
1377 p = frag_more (len);
1378 memcpy (p, files[1].filename, len);
1380 /* DW_AT_comp_dir */
1381 comp_dir = getpwd ();
1382 len = strlen (comp_dir) + 1;
1383 p = frag_more (len);
1384 memcpy (p, comp_dir, len);
1386 /* DW_AT_producer */
1387 sprintf (producer, "GNU AS %s", VERSION);
1388 len = strlen (producer) + 1;
1389 p = frag_more (len);
1390 memcpy (p, producer, len);
1392 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1393 dwarf2 draft has no standard code for assembler. */
1394 out_two (DW_LANG_Mips_Assembler);
1396 symbol_set_value_now (info_end);
1399 void
1400 dwarf2_finish ()
1402 segT line_seg;
1403 struct line_seg *s;
1405 /* We don't need to do anything unless:
1406 - Some debug information was recorded via .file/.loc
1407 - or, we are generating DWARF2 information ourself (--gdwarf2)
1408 - or, there is a user-provided .debug_info section which could
1409 reference the file table in the .debug_line section we generate
1410 below. */
1411 if (all_segs == NULL
1412 && debug_type != DEBUG_DWARF2
1413 && bfd_get_section_by_name (stdoutput, ".debug_info") == NULL)
1414 return;
1416 /* Calculate the size of an address for the target machine. */
1417 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1419 /* Create and switch to the line number section. */
1420 line_seg = subseg_new (".debug_line", 0);
1421 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY);
1423 /* For each subsection, chain the debug entries together. */
1424 for (s = all_segs; s; s = s->next)
1426 struct line_subseg *ss = s->head;
1427 struct line_entry **ptail = ss->ptail;
1429 while ((ss = ss->next) != NULL)
1431 *ptail = ss->head;
1432 ptail = ss->ptail;
1436 out_debug_line (line_seg);
1438 /* If this is assembler generated line info, we need .debug_info
1439 and .debug_abbrev sections as well. */
1440 if (all_segs != NULL && debug_type == DEBUG_DWARF2)
1442 segT abbrev_seg;
1443 segT info_seg;
1444 segT aranges_seg;
1446 info_seg = subseg_new (".debug_info", 0);
1447 abbrev_seg = subseg_new (".debug_abbrev", 0);
1448 aranges_seg = subseg_new (".debug_aranges", 0);
1450 bfd_set_section_flags (stdoutput, info_seg, SEC_READONLY);
1451 bfd_set_section_flags (stdoutput, abbrev_seg, SEC_READONLY);
1452 bfd_set_section_flags (stdoutput, aranges_seg, SEC_READONLY);
1454 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1456 out_debug_aranges (aranges_seg, info_seg);
1457 out_debug_abbrev (abbrev_seg);
1458 out_debug_info (info_seg, abbrev_seg, line_seg);
1462 #else
1463 void
1464 dwarf2_finish ()
1469 dwarf2dbg_estimate_size_before_relax (frag)
1470 fragS *frag ATTRIBUTE_UNUSED;
1472 as_fatal (_("dwarf2 is not supported for this object file format"));
1473 return 0;
1477 dwarf2dbg_relax_frag (frag)
1478 fragS *frag ATTRIBUTE_UNUSED;
1480 as_fatal (_("dwarf2 is not supported for this object file format"));
1481 return 0;
1484 void
1485 dwarf2dbg_convert_frag (frag)
1486 fragS *frag ATTRIBUTE_UNUSED;
1488 as_fatal (_("dwarf2 is not supported for this object file format"));
1491 void
1492 dwarf2_emit_insn (size)
1493 int size ATTRIBUTE_UNUSED;
1497 char *
1498 dwarf2_directive_file (dummy)
1499 int dummy ATTRIBUTE_UNUSED;
1501 s_app_file (0);
1502 return NULL;
1505 void
1506 dwarf2_directive_loc (dummy)
1507 int dummy ATTRIBUTE_UNUSED;
1509 as_fatal (_("dwarf2 is not supported for this object file format"));
1511 #endif /* BFD_ASSEMBLER */