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)
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
22 /* Logical line numbers can be controlled by the compiler via the
23 following two directives:
26 .loc FILENO LINENO [COLUMN]
28 FILENO is the filenumber. */
36 #ifdef HAVE_SYS_PARAM_H
37 #include <sys/param.h>
40 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
44 #include "dwarf2dbg.h"
45 #include <filenames.h>
48 # define DWARF2_FORMAT() dwarf2_format_32bit
51 #ifndef DWARF2_ADDR_SIZE
52 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8);
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"). */
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
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
81 /* Range of line offsets in a special line info. opcode. */
82 #ifndef DWARF2_LINE_RANGE
83 # define DWARF2_LINE_RANGE 14
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
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)
110 struct line_entry
*next
;
113 struct dwarf2_line_info loc
;
117 struct line_subseg
*next
;
119 struct line_entry
*head
;
120 struct line_entry
**ptail
;
124 struct line_seg
*next
;
126 struct line_subseg
*head
;
131 /* Collects data for all line table entries during assembly. */
132 static struct line_seg
*all_segs
;
135 const char *filename
;
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. */
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 (segT
, subsegT
);
160 static unsigned int get_filenum (const char *, unsigned int);
161 static struct frag
*first_frag_for_seg (segT
);
162 static struct frag
*last_frag_for_seg (segT
);
163 static void out_byte (int);
164 static void out_opcode (int);
165 static void out_two (int);
166 static void out_four (int);
167 static void out_abbrev (int, int);
168 static void out_uleb128 (addressT
);
169 static offsetT
get_frag_fix (fragS
*);
170 static void out_set_addr (segT
, fragS
*, addressT
);
171 static int size_inc_line_addr (int, addressT
);
172 static void emit_inc_line_addr (int, addressT
, char *, int);
173 static void out_inc_line_addr (int, addressT
);
174 static void relax_inc_line_addr (int, segT
, fragS
*, addressT
,
176 static void process_entries (segT
, struct line_entry
*);
177 static void out_file_list (void);
178 static void out_debug_line (segT
);
179 static void out_debug_aranges (segT
, segT
);
180 static void out_debug_abbrev (segT
);
181 static void out_debug_info (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 (symbolS
*, unsigned int);
187 /* Create an offset to .dwarf2_*. */
190 generic_dwarf2_emit_offset (symbolS
*symbol
, unsigned int size
)
194 expr
.X_op
= O_symbol
;
195 expr
.X_add_symbol
= symbol
;
196 expr
.X_add_number
= 0;
197 emit_expr (&expr
, size
);
201 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
203 static struct line_subseg
*
204 get_line_subseg (segT seg
, subsegT subseg
)
206 static segT last_seg
;
207 static subsegT last_subseg
;
208 static struct line_subseg
*last_line_subseg
;
211 struct line_subseg
**pss
, *ss
;
213 if (seg
== last_seg
&& subseg
== last_subseg
)
214 return last_line_subseg
;
216 for (s
= all_segs
; s
; s
= s
->next
)
220 s
= (struct line_seg
*) xmalloc (sizeof (*s
));
227 for (pss
= &s
->head
; (ss
= *pss
) != NULL
; pss
= &ss
->next
)
229 if (ss
->subseg
== subseg
)
231 if (ss
->subseg
> subseg
)
235 ss
= (struct line_subseg
*) xmalloc (sizeof (*ss
));
239 ss
->ptail
= &ss
->head
;
244 last_subseg
= subseg
;
245 last_line_subseg
= ss
;
250 /* Record an entry for LOC occurring at OFS within the current fragment. */
253 dwarf2_gen_line_info (addressT ofs
, struct dwarf2_line_info
*loc
)
255 struct line_subseg
*ss
;
256 struct line_entry
*e
;
257 static unsigned int line
= -1;
258 static unsigned int filenum
= -1;
260 /* Early out for as-yet incomplete location information. */
261 if (loc
->filenum
== 0 || loc
->line
== 0)
264 /* Don't emit sequences of line symbols for the same line when the
265 symbols apply to assembler code. It is necessary to emit
266 duplicate line symbols when a compiler asks for them, because GDB
267 uses them to determine the end of the prologue. */
268 if (debug_type
== DEBUG_DWARF2
269 && line
== loc
->line
&& filenum
== loc
->filenum
)
273 filenum
= loc
->filenum
;
275 e
= (struct line_entry
*) xmalloc (sizeof (*e
));
281 ss
= get_line_subseg (now_seg
, now_subseg
);
283 ss
->ptail
= &e
->next
;
287 dwarf2_where (struct dwarf2_line_info
*line
)
289 if (debug_type
== DEBUG_DWARF2
)
292 as_where (&filename
, &line
->line
);
293 line
->filenum
= get_filenum (filename
, 0);
295 line
->flags
= DWARF2_FLAG_BEGIN_STMT
;
301 /* Called for each machine instruction, or relatively atomic group of
302 machine instructions (ie built-in macro). The instruction or group
303 is SIZE bytes in length. If dwarf2 line number generation is called
304 for, emit a line statement appropriately. */
307 dwarf2_emit_insn (int size
)
309 struct dwarf2_line_info loc
;
311 if (loc_directive_seen
)
313 /* Use the last location established by a .loc directive, not
314 the value returned by dwarf2_where(). That calls as_where()
315 which will return either the logical input file name (foo.c)
316 or the physical input file name (foo.s) and not the file name
317 specified in the most recent .loc directive (eg foo.h). */
320 /* Unless we generate DWARF2 debugging information for each
321 assembler line, we only emit one line symbol for one LOC. */
322 if (debug_type
!= DEBUG_DWARF2
)
323 loc_directive_seen
= FALSE
;
325 else if (debug_type
!= DEBUG_DWARF2
)
328 dwarf2_where (& loc
);
330 dwarf2_gen_line_info (frag_now_fix () - size
, &loc
);
333 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
334 allocate it on that file table slot, otherwise return the first
338 get_filenum (const char *filename
, unsigned int num
)
340 static unsigned int last_used
, last_used_dir_len
;
345 if (num
== 0 && last_used
)
347 if (! files
[last_used
].dir
348 && strcmp (filename
, files
[last_used
].filename
) == 0)
350 if (files
[last_used
].dir
351 && strncmp (filename
, dirs
[files
[last_used
].dir
],
352 last_used_dir_len
) == 0
353 && IS_DIR_SEPARATOR (filename
[last_used_dir_len
])
354 && strcmp (filename
+ last_used_dir_len
+ 1,
355 files
[last_used
].filename
) == 0)
359 file
= lbasename (filename
);
360 /* Don't make empty string from / or A: from A:/ . */
361 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
362 if (file
<= filename
+ 3)
365 if (file
== filename
+ 1)
368 dir_len
= file
- filename
;
374 for (dir
= 1; dir
< dirs_in_use
; ++dir
)
375 if (memcmp (filename
, dirs
[dir
], dir_len
) == 0
376 && dirs
[dir
][dir_len
] == '\0')
379 if (dir
>= dirs_in_use
)
381 if (dir
>= dirs_allocated
)
383 dirs_allocated
= dir
+ 32;
385 xrealloc (dirs
, (dir
+ 32) * sizeof (const char *));
388 dirs
[dir
] = xmalloc (dir_len
+ 1);
389 memcpy (dirs
[dir
], filename
, dir_len
);
390 dirs
[dir
][dir_len
] = '\0';
391 dirs_in_use
= dir
+ 1;
397 for (i
= 1; i
< files_in_use
; ++i
)
398 if (files
[i
].dir
== dir
400 && strcmp (file
, files
[i
].filename
) == 0)
403 last_used_dir_len
= dir_len
;
410 if (i
>= files_allocated
)
412 unsigned int old
= files_allocated
;
414 files_allocated
= i
+ 32;
415 files
= (struct file_entry
*)
416 xrealloc (files
, (i
+ 32) * sizeof (struct file_entry
));
418 memset (files
+ old
, 0, (i
+ 32 - old
) * sizeof (struct file_entry
));
421 files
[i
].filename
= num
? file
: xstrdup (file
);
423 files_in_use
= i
+ 1;
425 last_used_dir_len
= dir_len
;
430 /* Handle two forms of .file directive:
431 - Pass .file "source.c" to s_app_file
432 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
434 If an entry is added to the file table, return a pointer to the filename. */
437 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED
)
443 /* Continue to accept a bare string and pass it off. */
445 if (*input_line_pointer
== '"')
451 num
= get_absolute_expression ();
452 filename
= demand_copy_C_string (&filename_len
);
453 demand_empty_rest_of_line ();
457 as_bad (_("file number less than one"));
461 if (num
< (int) files_in_use
&& files
[num
].filename
!= 0)
463 as_bad (_("file number %ld already allocated"), (long) num
);
467 get_filenum (filename
, num
);
473 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED
)
475 offsetT filenum
, line
, column
;
477 filenum
= get_absolute_expression ();
479 line
= get_absolute_expression ();
481 column
= get_absolute_expression ();
482 demand_empty_rest_of_line ();
486 as_bad (_("file number less than one"));
489 if (filenum
>= (int) files_in_use
|| files
[filenum
].filename
== 0)
491 as_bad (_("unassigned file number %ld"), (long) filenum
);
495 current
.filenum
= filenum
;
497 current
.column
= column
;
498 current
.flags
= DWARF2_FLAG_BEGIN_STMT
;
500 loc_directive_seen
= TRUE
;
505 if (files
[filenum
].dir
)
507 size_t dir_len
= strlen (dirs
[files
[filenum
].dir
]);
508 size_t file_len
= strlen (files
[filenum
].filename
);
509 char *cp
= (char *) alloca (dir_len
+ 1 + file_len
+ 1);
511 memcpy (cp
, dirs
[files
[filenum
].dir
], dir_len
);
513 memcpy (cp
+ dir_len
+ 1, files
[filenum
].filename
, file_len
);
514 cp
[dir_len
+ file_len
+ 1] = '\0';
515 listing_source_file (cp
);
518 listing_source_file (files
[filenum
].filename
);
519 listing_source_line (line
);
525 first_frag_for_seg (segT seg
)
527 frchainS
*f
, *first
= NULL
;
529 for (f
= frchain_root
; f
; f
= f
->frch_next
)
530 if (f
->frch_seg
== seg
531 && (! first
|| first
->frch_subseg
> f
->frch_subseg
))
534 return first
? first
->frch_root
: NULL
;
538 last_frag_for_seg (segT seg
)
540 frchainS
*f
, *last
= NULL
;
542 for (f
= frchain_root
; f
; f
= f
->frch_next
)
543 if (f
->frch_seg
== seg
544 && (! last
|| last
->frch_subseg
< f
->frch_subseg
))
547 return last
? last
->frch_last
: NULL
;
550 /* Emit a single byte into the current segment. */
555 FRAG_APPEND_1_CHAR (byte
);
558 /* Emit a statement program opcode into the current segment. */
566 /* Emit a two-byte word into the current segment. */
571 md_number_to_chars (frag_more (2), data
, 2);
574 /* Emit a four byte word into the current segment. */
579 md_number_to_chars (frag_more (4), data
, 4);
582 /* Emit an unsigned "little-endian base 128" number. */
585 out_uleb128 (addressT value
)
587 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
590 /* Emit a tuple for .debug_abbrev. */
593 out_abbrev (int name
, int form
)
599 /* Get the size of a fragment. */
602 get_frag_fix (fragS
*frag
)
609 /* If a fragment is the last in the chain, special measures must be
610 taken to find its size before relaxation, since it may be pending
611 on some subsegment chain. */
612 for (fr
= frchain_root
; fr
; fr
= fr
->frch_next
)
613 if (fr
->frch_last
== frag
)
614 return (char *) obstack_next_free (&fr
->frch_obstack
) - frag
->fr_literal
;
619 /* Set an absolute address (may result in a relocation entry). */
622 out_set_addr (segT seg
, fragS
*frag
, addressT ofs
)
627 sym
= symbol_temp_new (seg
, ofs
, frag
);
629 out_opcode (DW_LNS_extended_op
);
630 out_uleb128 (sizeof_address
+ 1);
632 out_opcode (DW_LNE_set_address
);
633 expr
.X_op
= O_symbol
;
634 expr
.X_add_symbol
= sym
;
635 expr
.X_add_number
= 0;
636 emit_expr (&expr
, sizeof_address
);
639 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
640 static void scale_addr_delta (addressT
*);
643 scale_addr_delta (addr_delta
)
644 addressT
*addr_delta
;
646 static int printed_this
= 0;
647 if (*addr_delta
% DWARF2_LINE_MIN_INSN_LENGTH
!= 0)
650 as_bad("unaligned opcodes detected in executable segment");
653 *addr_delta
/= DWARF2_LINE_MIN_INSN_LENGTH
;
656 #define scale_addr_delta(A)
659 /* Encode a pair of line and address skips as efficiently as possible.
660 Note that the line skip is signed, whereas the address skip is unsigned.
662 The following two routines *must* be kept in sync. This is
663 enforced by making emit_inc_line_addr abort if we do not emit
664 exactly the expected number of bytes. */
667 size_inc_line_addr (int line_delta
, addressT addr_delta
)
669 unsigned int tmp
, opcode
;
672 /* Scale the address delta by the minimum instruction length. */
673 scale_addr_delta (&addr_delta
);
675 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
676 We cannot use special opcodes here, since we want the end_sequence
677 to emit the matrix entry. */
678 if (line_delta
== INT_MAX
)
680 if (addr_delta
== MAX_SPECIAL_ADDR_DELTA
)
683 len
= 1 + sizeof_leb128 (addr_delta
, 0);
687 /* Bias the line delta by the base. */
688 tmp
= line_delta
- DWARF2_LINE_BASE
;
690 /* If the line increment is out of range of a special opcode, we
691 must encode it with DW_LNS_advance_line. */
692 if (tmp
>= DWARF2_LINE_RANGE
)
694 len
= 1 + sizeof_leb128 (line_delta
, 1);
696 tmp
= 0 - DWARF2_LINE_BASE
;
699 /* Bias the opcode by the special opcode base. */
700 tmp
+= DWARF2_LINE_OPCODE_BASE
;
702 /* Avoid overflow when addr_delta is large. */
703 if (addr_delta
< 256 + MAX_SPECIAL_ADDR_DELTA
)
705 /* Try using a special opcode. */
706 opcode
= tmp
+ addr_delta
* DWARF2_LINE_RANGE
;
710 /* Try using DW_LNS_const_add_pc followed by special op. */
711 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
716 /* Otherwise use DW_LNS_advance_pc. */
717 len
+= 1 + sizeof_leb128 (addr_delta
, 0);
719 /* DW_LNS_copy or special opcode. */
726 emit_inc_line_addr (int line_delta
, addressT addr_delta
, char *p
, int len
)
728 unsigned int tmp
, opcode
;
732 /* Scale the address delta by the minimum instruction length. */
733 scale_addr_delta (&addr_delta
);
735 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
736 We cannot use special opcodes here, since we want the end_sequence
737 to emit the matrix entry. */
738 if (line_delta
== INT_MAX
)
740 if (addr_delta
== MAX_SPECIAL_ADDR_DELTA
)
741 *p
++ = DW_LNS_const_add_pc
;
744 *p
++ = DW_LNS_advance_pc
;
745 p
+= output_leb128 (p
, addr_delta
, 0);
748 *p
++ = DW_LNS_extended_op
;
750 *p
++ = DW_LNE_end_sequence
;
754 /* Bias the line delta by the base. */
755 tmp
= line_delta
- DWARF2_LINE_BASE
;
757 /* If the line increment is out of range of a special opcode, we
758 must encode it with DW_LNS_advance_line. */
759 if (tmp
>= DWARF2_LINE_RANGE
)
761 *p
++ = DW_LNS_advance_line
;
762 p
+= output_leb128 (p
, line_delta
, 1);
764 /* Prettier, I think, to use DW_LNS_copy instead of a
765 "line +0, addr +0" special opcode. */
773 tmp
= 0 - DWARF2_LINE_BASE
;
777 /* Bias the opcode by the special opcode base. */
778 tmp
+= DWARF2_LINE_OPCODE_BASE
;
780 /* Avoid overflow when addr_delta is large. */
781 if (addr_delta
< 256 + MAX_SPECIAL_ADDR_DELTA
)
783 /* Try using a special opcode. */
784 opcode
= tmp
+ addr_delta
* DWARF2_LINE_RANGE
;
791 /* Try using DW_LNS_const_add_pc followed by special op. */
792 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
795 *p
++ = DW_LNS_const_add_pc
;
801 /* Otherwise use DW_LNS_advance_pc. */
802 *p
++ = DW_LNS_advance_pc
;
803 p
+= output_leb128 (p
, addr_delta
, 0);
814 /* Handy routine to combine calls to the above two routines. */
817 out_inc_line_addr (int line_delta
, addressT addr_delta
)
819 int len
= size_inc_line_addr (line_delta
, addr_delta
);
820 emit_inc_line_addr (line_delta
, addr_delta
, frag_more (len
), len
);
823 /* Generate a variant frag that we can use to relax address/line
824 increments between fragments of the target segment. */
827 relax_inc_line_addr (int line_delta
, segT seg
,
828 fragS
*to_frag
, addressT to_ofs
,
829 fragS
*from_frag
, addressT from_ofs
)
831 symbolS
*to_sym
, *from_sym
;
835 to_sym
= symbol_temp_new (seg
, to_ofs
, to_frag
);
836 from_sym
= symbol_temp_new (seg
, from_ofs
, from_frag
);
838 expr
.X_op
= O_subtract
;
839 expr
.X_add_symbol
= to_sym
;
840 expr
.X_op_symbol
= from_sym
;
841 expr
.X_add_number
= 0;
843 /* The maximum size of the frag is the line delta with a maximum
844 sized address delta. */
845 max_chars
= size_inc_line_addr (line_delta
, -DWARF2_LINE_MIN_INSN_LENGTH
);
847 frag_var (rs_dwarf2dbg
, max_chars
, max_chars
, 1,
848 make_expr_symbol (&expr
), line_delta
, NULL
);
851 /* The function estimates the size of a rs_dwarf2dbg variant frag
852 based on the current values of the symbols. It is called before
853 the relaxation loop. We set fr_subtype to the expected length. */
856 dwarf2dbg_estimate_size_before_relax (fragS
*frag
)
861 addr_delta
= resolve_symbol_value (frag
->fr_symbol
);
862 size
= size_inc_line_addr (frag
->fr_offset
, addr_delta
);
864 frag
->fr_subtype
= size
;
869 /* This function relaxes a rs_dwarf2dbg variant frag based on the
870 current values of the symbols. fr_subtype is the current length
871 of the frag. This returns the change in frag length. */
874 dwarf2dbg_relax_frag (fragS
*frag
)
876 int old_size
, new_size
;
878 old_size
= frag
->fr_subtype
;
879 new_size
= dwarf2dbg_estimate_size_before_relax (frag
);
881 return new_size
- old_size
;
884 /* This function converts a rs_dwarf2dbg variant frag into a normal
885 fill frag. This is called after all relaxation has been done.
886 fr_subtype will be the desired length of the frag. */
889 dwarf2dbg_convert_frag (fragS
*frag
)
893 addr_diff
= resolve_symbol_value (frag
->fr_symbol
);
895 /* fr_var carries the max_chars that we created the fragment with.
896 fr_subtype carries the current expected length. We must, of
897 course, have allocated enough memory earlier. */
898 assert (frag
->fr_var
>= (int) frag
->fr_subtype
);
900 emit_inc_line_addr (frag
->fr_offset
, addr_diff
,
901 frag
->fr_literal
+ frag
->fr_fix
, frag
->fr_subtype
);
903 frag
->fr_fix
+= frag
->fr_subtype
;
904 frag
->fr_type
= rs_fill
;
909 /* Generate .debug_line content for the chain of line number entries
910 beginning at E, for segment SEG. */
913 process_entries (segT seg
, struct line_entry
*e
)
915 unsigned filenum
= 1;
918 unsigned flags
= DWARF2_LINE_DEFAULT_IS_STMT
? DWARF2_FLAG_BEGIN_STMT
: 0;
921 addressT frag_ofs
= 0;
922 addressT last_frag_ofs
;
923 struct line_entry
*next
;
929 if (filenum
!= e
->loc
.filenum
)
931 filenum
= e
->loc
.filenum
;
932 out_opcode (DW_LNS_set_file
);
933 out_uleb128 (filenum
);
937 if (column
!= e
->loc
.column
)
939 column
= e
->loc
.column
;
940 out_opcode (DW_LNS_set_column
);
941 out_uleb128 (column
);
945 if ((e
->loc
.flags
^ flags
) & DWARF2_FLAG_BEGIN_STMT
)
947 flags
= e
->loc
.flags
;
948 out_opcode (DW_LNS_negate_stmt
);
952 if (e
->loc
.flags
& DWARF2_FLAG_BEGIN_BLOCK
)
954 out_opcode (DW_LNS_set_basic_block
);
958 /* Don't try to optimize away redundant entries; gdb wants two
959 entries for a function where the code starts on the same line as
960 the {, and there's no way to identify that case here. Trust gcc
961 to optimize appropriately. */
962 if (1 /* line != e->loc.line || changed */)
964 int line_delta
= e
->loc
.line
- line
;
967 out_set_addr (seg
, e
->frag
, e
->frag_ofs
);
968 out_inc_line_addr (line_delta
, 0);
970 else if (frag
== e
->frag
)
971 out_inc_line_addr (line_delta
, e
->frag_ofs
- frag_ofs
);
973 relax_inc_line_addr (line_delta
, seg
, e
->frag
, e
->frag_ofs
,
977 frag_ofs
= e
->frag_ofs
;
980 else if (frag
== NULL
)
982 out_set_addr (seg
, e
->frag
, e
->frag_ofs
);
984 frag_ofs
= e
->frag_ofs
;
992 /* Emit a DW_LNE_end_sequence for the end of the section. */
993 last_frag
= last_frag_for_seg (seg
);
994 last_frag_ofs
= get_frag_fix (last_frag
);
995 if (frag
== last_frag
)
996 out_inc_line_addr (INT_MAX
, last_frag_ofs
- frag_ofs
);
998 relax_inc_line_addr (INT_MAX
, seg
, last_frag
, last_frag_ofs
,
1002 /* Emit the directory and file tables for .debug_line. */
1005 out_file_list (void)
1011 /* Emit directory list. */
1012 for (i
= 1; i
< dirs_in_use
; ++i
)
1014 size
= strlen (dirs
[i
]) + 1;
1015 cp
= frag_more (size
);
1016 memcpy (cp
, dirs
[i
], size
);
1021 for (i
= 1; i
< files_in_use
; ++i
)
1023 if (files
[i
].filename
== NULL
)
1025 as_bad (_("unassigned file number %ld"), (long) i
);
1026 /* Prevent a crash later, particularly for file 1. */
1027 files
[i
].filename
= "";
1031 size
= strlen (files
[i
].filename
) + 1;
1032 cp
= frag_more (size
);
1033 memcpy (cp
, files
[i
].filename
, size
);
1035 out_uleb128 (files
[i
].dir
); /* directory number */
1036 out_uleb128 (0); /* last modification timestamp */
1037 out_uleb128 (0); /* filesize */
1040 /* Terminate filename list. */
1044 /* Emit the collected .debug_line data. */
1047 out_debug_line (segT line_seg
)
1050 symbolS
*line_start
;
1051 symbolS
*prologue_end
;
1054 enum dwarf2_format d2f
;
1057 subseg_set (line_seg
, 0);
1059 line_start
= symbol_temp_new_now ();
1060 prologue_end
= symbol_temp_make ();
1061 line_end
= symbol_temp_make ();
1063 /* Total length of the information for this compilation unit. */
1064 expr
.X_op
= O_subtract
;
1065 expr
.X_add_symbol
= line_end
;
1066 expr
.X_op_symbol
= line_start
;
1068 d2f
= DWARF2_FORMAT ();
1069 if (d2f
== dwarf2_format_32bit
)
1071 expr
.X_add_number
= -4;
1072 emit_expr (&expr
, 4);
1075 else if (d2f
== dwarf2_format_64bit
)
1077 expr
.X_add_number
= -12;
1079 emit_expr (&expr
, 8);
1082 else if (d2f
== dwarf2_format_64bit_irix
)
1084 expr
.X_add_number
= -8;
1085 emit_expr (&expr
, 8);
1090 as_fatal (_("internal error: unknown dwarf2 format"));
1096 /* Length of the prologue following this length. */
1097 expr
.X_op
= O_subtract
;
1098 expr
.X_add_symbol
= prologue_end
;
1099 expr
.X_op_symbol
= line_start
;
1100 expr
.X_add_number
= - (4 + 2 + 4);
1101 emit_expr (&expr
, sizeof_offset
);
1103 /* Parameters of the state machine. */
1104 out_byte (DWARF2_LINE_MIN_INSN_LENGTH
);
1105 out_byte (DWARF2_LINE_DEFAULT_IS_STMT
);
1106 out_byte (DWARF2_LINE_BASE
);
1107 out_byte (DWARF2_LINE_RANGE
);
1108 out_byte (DWARF2_LINE_OPCODE_BASE
);
1110 /* Standard opcode lengths. */
1111 out_byte (0); /* DW_LNS_copy */
1112 out_byte (1); /* DW_LNS_advance_pc */
1113 out_byte (1); /* DW_LNS_advance_line */
1114 out_byte (1); /* DW_LNS_set_file */
1115 out_byte (1); /* DW_LNS_set_column */
1116 out_byte (0); /* DW_LNS_negate_stmt */
1117 out_byte (0); /* DW_LNS_set_basic_block */
1118 out_byte (0); /* DW_LNS_const_add_pc */
1119 out_byte (1); /* DW_LNS_fixed_advance_pc */
1123 symbol_set_value_now (prologue_end
);
1125 /* For each section, emit a statement program. */
1126 for (s
= all_segs
; s
; s
= s
->next
)
1127 process_entries (s
->seg
, s
->head
->head
);
1129 symbol_set_value_now (line_end
);
1132 /* Emit data for .debug_aranges. */
1135 out_debug_aranges (segT aranges_seg
, segT info_seg
)
1137 unsigned int addr_size
= sizeof_address
;
1138 addressT size
, skip
;
1143 size
= 4 + 2 + 4 + 1 + 1;
1145 skip
= 2 * addr_size
- (size
& (2 * addr_size
- 1));
1146 if (skip
== 2 * addr_size
)
1150 for (s
= all_segs
; s
; s
= s
->next
)
1151 size
+= 2 * addr_size
;
1153 size
+= 2 * addr_size
;
1155 subseg_set (aranges_seg
, 0);
1157 /* Length of the compilation unit. */
1158 out_four (size
- 4);
1163 /* Offset to .debug_info. */
1164 /* ??? sizeof_offset */
1165 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg
), 4);
1167 /* Size of an address (offset portion). */
1168 out_byte (addr_size
);
1170 /* Size of a segment descriptor. */
1173 /* Align the header. */
1175 frag_align (ffs (2 * addr_size
) - 1, 0, 0);
1177 for (s
= all_segs
; s
; s
= s
->next
)
1182 frag
= first_frag_for_seg (s
->seg
);
1183 beg
= symbol_temp_new (s
->seg
, 0, frag
);
1184 s
->text_start
= beg
;
1186 frag
= last_frag_for_seg (s
->seg
);
1187 end
= symbol_temp_new (s
->seg
, get_frag_fix (frag
), frag
);
1190 expr
.X_op
= O_symbol
;
1191 expr
.X_add_symbol
= beg
;
1192 expr
.X_add_number
= 0;
1193 emit_expr (&expr
, addr_size
);
1195 expr
.X_op
= O_subtract
;
1196 expr
.X_add_symbol
= end
;
1197 expr
.X_op_symbol
= beg
;
1198 expr
.X_add_number
= 0;
1199 emit_expr (&expr
, addr_size
);
1202 p
= frag_more (2 * addr_size
);
1203 md_number_to_chars (p
, 0, addr_size
);
1204 md_number_to_chars (p
+ addr_size
, 0, addr_size
);
1207 /* Emit data for .debug_abbrev. Note that this must be kept in
1208 sync with out_debug_info below. */
1211 out_debug_abbrev (segT abbrev_seg
)
1213 subseg_set (abbrev_seg
, 0);
1216 out_uleb128 (DW_TAG_compile_unit
);
1217 out_byte (DW_CHILDREN_no
);
1218 out_abbrev (DW_AT_stmt_list
, DW_FORM_data4
);
1219 if (all_segs
->next
== NULL
)
1221 out_abbrev (DW_AT_low_pc
, DW_FORM_addr
);
1222 out_abbrev (DW_AT_high_pc
, DW_FORM_addr
);
1224 out_abbrev (DW_AT_name
, DW_FORM_string
);
1225 out_abbrev (DW_AT_comp_dir
, DW_FORM_string
);
1226 out_abbrev (DW_AT_producer
, DW_FORM_string
);
1227 out_abbrev (DW_AT_language
, DW_FORM_data2
);
1230 /* Terminate the abbreviations for this compilation unit. */
1234 /* Emit a description of this compilation unit for .debug_info. */
1237 out_debug_info (segT info_seg
, segT abbrev_seg
, segT line_seg
)
1242 symbolS
*info_start
;
1246 enum dwarf2_format d2f
;
1249 subseg_set (info_seg
, 0);
1251 info_start
= symbol_temp_new_now ();
1252 info_end
= symbol_temp_make ();
1254 /* Compilation Unit length. */
1255 expr
.X_op
= O_subtract
;
1256 expr
.X_add_symbol
= info_end
;
1257 expr
.X_op_symbol
= info_start
;
1259 d2f
= DWARF2_FORMAT ();
1260 if (d2f
== dwarf2_format_32bit
)
1262 expr
.X_add_number
= -4;
1263 emit_expr (&expr
, 4);
1266 else if (d2f
== dwarf2_format_64bit
)
1268 expr
.X_add_number
= -12;
1270 emit_expr (&expr
, 8);
1273 else if (d2f
== dwarf2_format_64bit_irix
)
1275 expr
.X_add_number
= -8;
1276 emit_expr (&expr
, 8);
1281 as_fatal (_("internal error: unknown dwarf2 format"));
1284 /* DWARF version. */
1287 /* .debug_abbrev offset */
1288 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg
), sizeof_offset
);
1290 /* Target address size. */
1291 out_byte (sizeof_address
);
1293 /* DW_TAG_compile_unit DIE abbrev */
1296 /* DW_AT_stmt_list */
1297 /* ??? sizeof_offset */
1298 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg
), 4);
1300 /* These two attributes may only be emitted if all of the code is
1301 contiguous. Multiple sections are not that. */
1302 if (all_segs
->next
== NULL
)
1305 expr
.X_op
= O_symbol
;
1306 expr
.X_add_symbol
= all_segs
->text_start
;
1307 expr
.X_add_number
= 0;
1308 emit_expr (&expr
, sizeof_address
);
1311 expr
.X_op
= O_symbol
;
1312 expr
.X_add_symbol
= all_segs
->text_end
;
1313 expr
.X_add_number
= 0;
1314 emit_expr (&expr
, sizeof_address
);
1317 /* DW_AT_name. We don't have the actual file name that was present
1318 on the command line, so assume files[1] is the main input file.
1319 We're not supposed to get called unless at least one line number
1320 entry was emitted, so this should always be defined. */
1321 if (!files
|| files_in_use
< 1)
1325 len
= strlen (dirs
[files
[1].dir
]);
1326 p
= frag_more (len
+ 1);
1327 memcpy (p
, dirs
[files
[1].dir
], len
);
1330 len
= strlen (files
[1].filename
) + 1;
1331 p
= frag_more (len
);
1332 memcpy (p
, files
[1].filename
, len
);
1334 /* DW_AT_comp_dir */
1335 comp_dir
= getpwd ();
1336 len
= strlen (comp_dir
) + 1;
1337 p
= frag_more (len
);
1338 memcpy (p
, comp_dir
, len
);
1340 /* DW_AT_producer */
1341 sprintf (producer
, "GNU AS %s", VERSION
);
1342 len
= strlen (producer
) + 1;
1343 p
= frag_more (len
);
1344 memcpy (p
, producer
, len
);
1346 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1347 dwarf2 draft has no standard code for assembler. */
1348 out_two (DW_LANG_Mips_Assembler
);
1350 symbol_set_value_now (info_end
);
1354 dwarf2_finish (void)
1359 /* We don't need to do anything unless:
1360 - Some debug information was recorded via .file/.loc
1361 - or, we are generating DWARF2 information ourself (--gdwarf2)
1362 - or, there is a user-provided .debug_info section which could
1363 reference the file table in the .debug_line section we generate
1365 if (all_segs
== NULL
1366 && debug_type
!= DEBUG_DWARF2
1367 && bfd_get_section_by_name (stdoutput
, ".debug_info") == NULL
)
1370 /* Calculate the size of an address for the target machine. */
1371 sizeof_address
= DWARF2_ADDR_SIZE (stdoutput
);
1373 /* Create and switch to the line number section. */
1374 line_seg
= subseg_new (".debug_line", 0);
1375 bfd_set_section_flags (stdoutput
, line_seg
, SEC_READONLY
);
1377 /* For each subsection, chain the debug entries together. */
1378 for (s
= all_segs
; s
; s
= s
->next
)
1380 struct line_subseg
*ss
= s
->head
;
1381 struct line_entry
**ptail
= ss
->ptail
;
1383 while ((ss
= ss
->next
) != NULL
)
1390 out_debug_line (line_seg
);
1392 /* If this is assembler generated line info, we need .debug_info
1393 and .debug_abbrev sections as well. */
1394 if (all_segs
!= NULL
&& debug_type
== DEBUG_DWARF2
)
1400 info_seg
= subseg_new (".debug_info", 0);
1401 abbrev_seg
= subseg_new (".debug_abbrev", 0);
1402 aranges_seg
= subseg_new (".debug_aranges", 0);
1404 bfd_set_section_flags (stdoutput
, info_seg
, SEC_READONLY
);
1405 bfd_set_section_flags (stdoutput
, abbrev_seg
, SEC_READONLY
);
1406 bfd_set_section_flags (stdoutput
, aranges_seg
, SEC_READONLY
);
1408 record_alignment (aranges_seg
, ffs (2 * sizeof_address
) - 1);
1410 out_debug_aranges (aranges_seg
, info_seg
);
1411 out_debug_abbrev (abbrev_seg
);
1412 out_debug_info (info_seg
, abbrev_seg
, line_seg
);
1423 dwarf2dbg_estimate_size_before_relax (frag
)
1424 fragS
*frag ATTRIBUTE_UNUSED
;
1426 as_fatal (_("dwarf2 is not supported for this object file format"));
1431 dwarf2dbg_relax_frag (frag
)
1432 fragS
*frag ATTRIBUTE_UNUSED
;
1434 as_fatal (_("dwarf2 is not supported for this object file format"));
1439 dwarf2dbg_convert_frag (frag
)
1440 fragS
*frag ATTRIBUTE_UNUSED
;
1442 as_fatal (_("dwarf2 is not supported for this object file format"));
1446 dwarf2_emit_insn (size
)
1447 int size ATTRIBUTE_UNUSED
;
1452 dwarf2_directive_file (dummy
)
1453 int dummy ATTRIBUTE_UNUSED
;
1460 dwarf2_directive_loc (dummy
)
1461 int dummy ATTRIBUTE_UNUSED
;
1463 as_fatal (_("dwarf2 is not supported for this object file format"));
1465 #endif /* BFD_ASSEMBLER */