1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 /* Logical line numbers can be controlled by the compiler via the
24 following two directives:
27 .loc FILENO LINENO [COLUMN]
29 FILENO is the filenumber. */
37 #ifdef HAVE_SYS_PARAM_H
38 #include <sys/param.h>
41 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
45 #include "dwarf2dbg.h"
46 #include <filenames.h>
49 # define DWARF2_FORMAT() dwarf2_format_32bit
52 #ifndef DWARF2_ADDR_SIZE
53 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
58 #include "elf/dwarf2.h"
60 /* Since we can't generate the prolog until the body is complete, we
61 use three different subsegments for .debug_line: one holding the
62 prolog, one for the directory and filename info, and one for the
63 body ("statement program"). */
68 /* First special line opcde - leave room for the standard opcodes.
69 Note: If you want to change this, you'll have to update the
70 "standard_opcode_lengths" table that is emitted below in
72 #define DWARF2_LINE_OPCODE_BASE 10
74 #ifndef DWARF2_LINE_BASE
75 /* Minimum line offset in a special line info. opcode. This value
76 was chosen to give a reasonable range of values. */
77 # define DWARF2_LINE_BASE -5
80 /* Range of line offsets in a special line info. opcode. */
81 #ifndef DWARF2_LINE_RANGE
82 # define DWARF2_LINE_RANGE 14
85 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
86 /* Define the architecture-dependent minimum instruction length (in
87 bytes). This value should be rather too small than too big. */
88 # define DWARF2_LINE_MIN_INSN_LENGTH 1
91 /* Flag that indicates the initial value of the is_stmt_start flag.
92 In the present implementation, we do not mark any lines as
93 the beginning of a source statement, because that information
94 is not made available by the GCC front-end. */
95 #define DWARF2_LINE_DEFAULT_IS_STMT 1
97 /* Given a special op, return the line skip amount. */
98 #define SPECIAL_LINE(op) \
99 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
101 /* Given a special op, return the address skip amount (in units of
102 DWARF2_LINE_MIN_INSN_LENGTH. */
103 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
105 /* The maximum address skip amount that can be encoded with a special op. */
106 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
109 struct line_entry
*next
;
112 struct dwarf2_line_info loc
;
116 struct line_subseg
*next
;
118 struct line_entry
*head
;
119 struct line_entry
**ptail
;
123 struct line_seg
*next
;
125 struct line_subseg
*head
;
130 /* Collects data for all line table entries during assembly. */
131 static struct line_seg
*all_segs
;
134 const char *filename
;
138 /* Table of files used by .debug_line. */
139 static struct file_entry
*files
;
140 static unsigned int files_in_use
;
141 static unsigned int files_allocated
;
143 /* Table of directories used by .debug_line. */
145 static unsigned int dirs_in_use
;
146 static unsigned int dirs_allocated
;
148 /* TRUE when we've seen a .loc directive recently. Used to avoid
149 doing work when there's nothing to do. */
150 static bfd_boolean loc_directive_seen
;
152 /* Current location as indicated by the most recent .loc directive. */
153 static struct dwarf2_line_info current
;
155 /* The size of an address on the target. */
156 static unsigned int sizeof_address
;
158 static struct line_subseg
*get_line_subseg (segT
, subsegT
);
159 static unsigned int get_filenum (const char *, unsigned int);
160 static struct frag
*first_frag_for_seg (segT
);
161 static struct frag
*last_frag_for_seg (segT
);
162 static void out_byte (int);
163 static void out_opcode (int);
164 static void out_two (int);
165 static void out_four (int);
166 static void out_abbrev (int, int);
167 static void out_uleb128 (addressT
);
168 static offsetT
get_frag_fix (fragS
*);
169 static void out_set_addr (segT
, fragS
*, addressT
);
170 static int size_inc_line_addr (int, addressT
);
171 static void emit_inc_line_addr (int, addressT
, char *, int);
172 static void out_inc_line_addr (int, addressT
);
173 static void relax_inc_line_addr (int, segT
, fragS
*, addressT
,
175 static void process_entries (segT
, struct line_entry
*);
176 static void out_file_list (void);
177 static void out_debug_line (segT
);
178 static void out_debug_aranges (segT
, segT
);
179 static void out_debug_abbrev (segT
);
180 static void out_debug_info (segT
, segT
, segT
);
182 #ifndef TC_DWARF2_EMIT_OFFSET
183 # define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
184 static void generic_dwarf2_emit_offset (symbolS
*, unsigned int);
186 /* Create an offset to .dwarf2_*. */
189 generic_dwarf2_emit_offset (symbolS
*symbol
, unsigned int size
)
193 expr
.X_op
= O_symbol
;
194 expr
.X_add_symbol
= symbol
;
195 expr
.X_add_number
= 0;
196 emit_expr (&expr
, size
);
200 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
202 static struct line_subseg
*
203 get_line_subseg (segT seg
, subsegT subseg
)
205 static segT last_seg
;
206 static subsegT last_subseg
;
207 static struct line_subseg
*last_line_subseg
;
210 struct line_subseg
**pss
, *ss
;
212 if (seg
== last_seg
&& subseg
== last_subseg
)
213 return last_line_subseg
;
215 for (s
= all_segs
; s
; s
= s
->next
)
219 s
= (struct line_seg
*) xmalloc (sizeof (*s
));
226 for (pss
= &s
->head
; (ss
= *pss
) != NULL
; pss
= &ss
->next
)
228 if (ss
->subseg
== subseg
)
230 if (ss
->subseg
> subseg
)
234 ss
= (struct line_subseg
*) xmalloc (sizeof (*ss
));
238 ss
->ptail
= &ss
->head
;
243 last_subseg
= subseg
;
244 last_line_subseg
= ss
;
249 /* Record an entry for LOC occurring at OFS within the current fragment. */
252 dwarf2_gen_line_info (addressT ofs
, struct dwarf2_line_info
*loc
)
254 struct line_subseg
*ss
;
255 struct line_entry
*e
;
256 static unsigned int line
= -1;
257 static unsigned int filenum
= -1;
259 /* Early out for as-yet incomplete location information. */
260 if (loc
->filenum
== 0 || loc
->line
== 0)
263 /* Don't emit sequences of line symbols for the same line when the
264 symbols apply to assembler code. It is necessary to emit
265 duplicate line symbols when a compiler asks for them, because GDB
266 uses them to determine the end of the prologue. */
267 if (debug_type
== DEBUG_DWARF2
268 && line
== loc
->line
&& filenum
== loc
->filenum
)
272 filenum
= loc
->filenum
;
274 e
= (struct line_entry
*) xmalloc (sizeof (*e
));
280 ss
= get_line_subseg (now_seg
, now_subseg
);
282 ss
->ptail
= &e
->next
;
286 dwarf2_where (struct dwarf2_line_info
*line
)
288 if (debug_type
== DEBUG_DWARF2
)
291 as_where (&filename
, &line
->line
);
292 line
->filenum
= get_filenum (filename
, 0);
294 line
->flags
= DWARF2_FLAG_BEGIN_STMT
;
300 /* Called for each machine instruction, or relatively atomic group of
301 machine instructions (ie built-in macro). The instruction or group
302 is SIZE bytes in length. If dwarf2 line number generation is called
303 for, emit a line statement appropriately. */
306 dwarf2_emit_insn (int size
)
308 struct dwarf2_line_info loc
;
310 if (loc_directive_seen
)
312 /* Use the last location established by a .loc directive, not
313 the value returned by dwarf2_where(). That calls as_where()
314 which will return either the logical input file name (foo.c)
315 or the physical input file name (foo.s) and not the file name
316 specified in the most recent .loc directive (eg foo.h). */
319 /* Unless we generate DWARF2 debugging information for each
320 assembler line, we only emit one line symbol for one LOC. */
321 if (debug_type
!= DEBUG_DWARF2
)
322 loc_directive_seen
= FALSE
;
324 else if (debug_type
!= DEBUG_DWARF2
)
327 dwarf2_where (& loc
);
329 dwarf2_gen_line_info (frag_now_fix () - size
, &loc
);
332 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
333 allocate it on that file table slot, otherwise return the first
337 get_filenum (const char *filename
, unsigned int num
)
339 static unsigned int last_used
, last_used_dir_len
;
344 if (num
== 0 && last_used
)
346 if (! files
[last_used
].dir
347 && strcmp (filename
, files
[last_used
].filename
) == 0)
349 if (files
[last_used
].dir
350 && strncmp (filename
, dirs
[files
[last_used
].dir
],
351 last_used_dir_len
) == 0
352 && IS_DIR_SEPARATOR (filename
[last_used_dir_len
])
353 && strcmp (filename
+ last_used_dir_len
+ 1,
354 files
[last_used
].filename
) == 0)
358 file
= lbasename (filename
);
359 /* Don't make empty string from / or A: from A:/ . */
360 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
361 if (file
<= filename
+ 3)
364 if (file
== filename
+ 1)
367 dir_len
= file
- filename
;
373 for (dir
= 1; dir
< dirs_in_use
; ++dir
)
374 if (strncmp (filename
, dirs
[dir
], dir_len
) == 0
375 && dirs
[dir
][dir_len
] == '\0')
378 if (dir
>= dirs_in_use
)
380 if (dir
>= dirs_allocated
)
382 dirs_allocated
= dir
+ 32;
384 xrealloc (dirs
, (dir
+ 32) * sizeof (const char *));
387 dirs
[dir
] = xmalloc (dir_len
+ 1);
388 memcpy (dirs
[dir
], filename
, dir_len
);
389 dirs
[dir
][dir_len
] = '\0';
390 dirs_in_use
= dir
+ 1;
396 for (i
= 1; i
< files_in_use
; ++i
)
397 if (files
[i
].dir
== dir
399 && strcmp (file
, files
[i
].filename
) == 0)
402 last_used_dir_len
= dir_len
;
409 if (i
>= files_allocated
)
411 unsigned int old
= files_allocated
;
413 files_allocated
= i
+ 32;
414 files
= (struct file_entry
*)
415 xrealloc (files
, (i
+ 32) * sizeof (struct file_entry
));
417 memset (files
+ old
, 0, (i
+ 32 - old
) * sizeof (struct file_entry
));
420 files
[i
].filename
= num
? file
: xstrdup (file
);
422 files_in_use
= i
+ 1;
424 last_used_dir_len
= dir_len
;
429 /* Handle two forms of .file directive:
430 - Pass .file "source.c" to s_app_file
431 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
433 If an entry is added to the file table, return a pointer to the filename. */
436 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED
)
442 /* Continue to accept a bare string and pass it off. */
444 if (*input_line_pointer
== '"')
450 num
= get_absolute_expression ();
451 filename
= demand_copy_C_string (&filename_len
);
452 demand_empty_rest_of_line ();
456 as_bad (_("file number less than one"));
460 if (num
< (int) files_in_use
&& files
[num
].filename
!= 0)
462 as_bad (_("file number %ld already allocated"), (long) num
);
466 get_filenum (filename
, num
);
472 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED
)
474 offsetT filenum
, line
, column
;
476 filenum
= get_absolute_expression ();
478 line
= get_absolute_expression ();
480 column
= get_absolute_expression ();
481 demand_empty_rest_of_line ();
485 as_bad (_("file number less than one"));
488 if (filenum
>= (int) files_in_use
|| files
[filenum
].filename
== 0)
490 as_bad (_("unassigned file number %ld"), (long) filenum
);
494 current
.filenum
= filenum
;
496 current
.column
= column
;
497 current
.flags
= DWARF2_FLAG_BEGIN_STMT
;
499 loc_directive_seen
= TRUE
;
504 if (files
[filenum
].dir
)
506 size_t dir_len
= strlen (dirs
[files
[filenum
].dir
]);
507 size_t file_len
= strlen (files
[filenum
].filename
);
508 char *cp
= (char *) alloca (dir_len
+ 1 + file_len
+ 1);
510 memcpy (cp
, dirs
[files
[filenum
].dir
], dir_len
);
512 memcpy (cp
+ dir_len
+ 1, files
[filenum
].filename
, file_len
);
513 cp
[dir_len
+ file_len
+ 1] = '\0';
514 listing_source_file (cp
);
517 listing_source_file (files
[filenum
].filename
);
518 listing_source_line (line
);
524 first_frag_for_seg (segT seg
)
526 frchainS
*f
, *first
= NULL
;
528 for (f
= frchain_root
; f
; f
= f
->frch_next
)
529 if (f
->frch_seg
== seg
530 && (! first
|| first
->frch_subseg
> f
->frch_subseg
))
533 return first
? first
->frch_root
: NULL
;
537 last_frag_for_seg (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
))
546 return last
? last
->frch_last
: NULL
;
549 /* Emit a single byte into the current segment. */
554 FRAG_APPEND_1_CHAR (byte
);
557 /* Emit a statement program opcode into the current segment. */
565 /* Emit a two-byte word into the current segment. */
570 md_number_to_chars (frag_more (2), data
, 2);
573 /* Emit a four byte word into the current segment. */
578 md_number_to_chars (frag_more (4), data
, 4);
581 /* Emit an unsigned "little-endian base 128" number. */
584 out_uleb128 (addressT value
)
586 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
589 /* Emit a tuple for .debug_abbrev. */
592 out_abbrev (int name
, int form
)
598 /* Get the size of a fragment. */
601 get_frag_fix (fragS
*frag
)
608 /* If a fragment is the last in the chain, special measures must be
609 taken to find its size before relaxation, since it may be pending
610 on some subsegment chain. */
611 for (fr
= frchain_root
; fr
; fr
= fr
->frch_next
)
612 if (fr
->frch_last
== frag
)
613 return (char *) obstack_next_free (&fr
->frch_obstack
) - frag
->fr_literal
;
618 /* Set an absolute address (may result in a relocation entry). */
621 out_set_addr (segT seg
, fragS
*frag
, addressT ofs
)
626 sym
= symbol_temp_new (seg
, ofs
, frag
);
628 out_opcode (DW_LNS_extended_op
);
629 out_uleb128 (sizeof_address
+ 1);
631 out_opcode (DW_LNE_set_address
);
632 expr
.X_op
= O_symbol
;
633 expr
.X_add_symbol
= sym
;
634 expr
.X_add_number
= 0;
635 emit_expr (&expr
, sizeof_address
);
638 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
639 static void scale_addr_delta (addressT
*);
642 scale_addr_delta (addressT
*addr_delta
)
644 static int printed_this
= 0;
645 if (*addr_delta
% DWARF2_LINE_MIN_INSN_LENGTH
!= 0)
648 as_bad("unaligned opcodes detected in executable segment");
651 *addr_delta
/= DWARF2_LINE_MIN_INSN_LENGTH
;
654 #define scale_addr_delta(A)
657 /* Encode a pair of line and address skips as efficiently as possible.
658 Note that the line skip is signed, whereas the address skip is unsigned.
660 The following two routines *must* be kept in sync. This is
661 enforced by making emit_inc_line_addr abort if we do not emit
662 exactly the expected number of bytes. */
665 size_inc_line_addr (int line_delta
, addressT addr_delta
)
667 unsigned int tmp
, opcode
;
670 /* Scale the address delta by the minimum instruction length. */
671 scale_addr_delta (&addr_delta
);
673 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
674 We cannot use special opcodes here, since we want the end_sequence
675 to emit the matrix entry. */
676 if (line_delta
== INT_MAX
)
678 if (addr_delta
== MAX_SPECIAL_ADDR_DELTA
)
681 len
= 1 + sizeof_leb128 (addr_delta
, 0);
685 /* Bias the line delta by the base. */
686 tmp
= line_delta
- DWARF2_LINE_BASE
;
688 /* If the line increment is out of range of a special opcode, we
689 must encode it with DW_LNS_advance_line. */
690 if (tmp
>= DWARF2_LINE_RANGE
)
692 len
= 1 + sizeof_leb128 (line_delta
, 1);
694 tmp
= 0 - DWARF2_LINE_BASE
;
697 /* Bias the opcode by the special opcode base. */
698 tmp
+= DWARF2_LINE_OPCODE_BASE
;
700 /* Avoid overflow when addr_delta is large. */
701 if (addr_delta
< 256 + MAX_SPECIAL_ADDR_DELTA
)
703 /* Try using a special opcode. */
704 opcode
= tmp
+ addr_delta
* DWARF2_LINE_RANGE
;
708 /* Try using DW_LNS_const_add_pc followed by special op. */
709 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
714 /* Otherwise use DW_LNS_advance_pc. */
715 len
+= 1 + sizeof_leb128 (addr_delta
, 0);
717 /* DW_LNS_copy or special opcode. */
724 emit_inc_line_addr (int line_delta
, addressT addr_delta
, char *p
, int len
)
726 unsigned int tmp
, opcode
;
730 /* Scale the address delta by the minimum instruction length. */
731 scale_addr_delta (&addr_delta
);
733 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
734 We cannot use special opcodes here, since we want the end_sequence
735 to emit the matrix entry. */
736 if (line_delta
== INT_MAX
)
738 if (addr_delta
== MAX_SPECIAL_ADDR_DELTA
)
739 *p
++ = DW_LNS_const_add_pc
;
742 *p
++ = DW_LNS_advance_pc
;
743 p
+= output_leb128 (p
, addr_delta
, 0);
746 *p
++ = DW_LNS_extended_op
;
748 *p
++ = DW_LNE_end_sequence
;
752 /* Bias the line delta by the base. */
753 tmp
= line_delta
- DWARF2_LINE_BASE
;
755 /* If the line increment is out of range of a special opcode, we
756 must encode it with DW_LNS_advance_line. */
757 if (tmp
>= DWARF2_LINE_RANGE
)
759 *p
++ = DW_LNS_advance_line
;
760 p
+= output_leb128 (p
, line_delta
, 1);
762 /* Prettier, I think, to use DW_LNS_copy instead of a
763 "line +0, addr +0" special opcode. */
771 tmp
= 0 - DWARF2_LINE_BASE
;
775 /* Bias the opcode by the special opcode base. */
776 tmp
+= DWARF2_LINE_OPCODE_BASE
;
778 /* Avoid overflow when addr_delta is large. */
779 if (addr_delta
< 256 + MAX_SPECIAL_ADDR_DELTA
)
781 /* Try using a special opcode. */
782 opcode
= tmp
+ addr_delta
* DWARF2_LINE_RANGE
;
789 /* Try using DW_LNS_const_add_pc followed by special op. */
790 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
793 *p
++ = DW_LNS_const_add_pc
;
799 /* Otherwise use DW_LNS_advance_pc. */
800 *p
++ = DW_LNS_advance_pc
;
801 p
+= output_leb128 (p
, addr_delta
, 0);
812 /* Handy routine to combine calls to the above two routines. */
815 out_inc_line_addr (int line_delta
, addressT addr_delta
)
817 int len
= size_inc_line_addr (line_delta
, addr_delta
);
818 emit_inc_line_addr (line_delta
, addr_delta
, frag_more (len
), len
);
821 /* Generate a variant frag that we can use to relax address/line
822 increments between fragments of the target segment. */
825 relax_inc_line_addr (int line_delta
, segT seg
,
826 fragS
*to_frag
, addressT to_ofs
,
827 fragS
*from_frag
, addressT from_ofs
)
829 symbolS
*to_sym
, *from_sym
;
833 to_sym
= symbol_temp_new (seg
, to_ofs
, to_frag
);
834 from_sym
= symbol_temp_new (seg
, from_ofs
, from_frag
);
836 expr
.X_op
= O_subtract
;
837 expr
.X_add_symbol
= to_sym
;
838 expr
.X_op_symbol
= from_sym
;
839 expr
.X_add_number
= 0;
841 /* The maximum size of the frag is the line delta with a maximum
842 sized address delta. */
843 max_chars
= size_inc_line_addr (line_delta
, -DWARF2_LINE_MIN_INSN_LENGTH
);
845 frag_var (rs_dwarf2dbg
, max_chars
, max_chars
, 1,
846 make_expr_symbol (&expr
), line_delta
, NULL
);
849 /* The function estimates the size of a rs_dwarf2dbg variant frag
850 based on the current values of the symbols. It is called before
851 the relaxation loop. We set fr_subtype to the expected length. */
854 dwarf2dbg_estimate_size_before_relax (fragS
*frag
)
859 addr_delta
= resolve_symbol_value (frag
->fr_symbol
);
860 size
= size_inc_line_addr (frag
->fr_offset
, addr_delta
);
862 frag
->fr_subtype
= size
;
867 /* This function relaxes a rs_dwarf2dbg variant frag based on the
868 current values of the symbols. fr_subtype is the current length
869 of the frag. This returns the change in frag length. */
872 dwarf2dbg_relax_frag (fragS
*frag
)
874 int old_size
, new_size
;
876 old_size
= frag
->fr_subtype
;
877 new_size
= dwarf2dbg_estimate_size_before_relax (frag
);
879 return new_size
- old_size
;
882 /* This function converts a rs_dwarf2dbg variant frag into a normal
883 fill frag. This is called after all relaxation has been done.
884 fr_subtype will be the desired length of the frag. */
887 dwarf2dbg_convert_frag (fragS
*frag
)
891 addr_diff
= resolve_symbol_value (frag
->fr_symbol
);
893 /* fr_var carries the max_chars that we created the fragment with.
894 fr_subtype carries the current expected length. We must, of
895 course, have allocated enough memory earlier. */
896 assert (frag
->fr_var
>= (int) frag
->fr_subtype
);
898 emit_inc_line_addr (frag
->fr_offset
, addr_diff
,
899 frag
->fr_literal
+ frag
->fr_fix
, frag
->fr_subtype
);
901 frag
->fr_fix
+= frag
->fr_subtype
;
902 frag
->fr_type
= rs_fill
;
907 /* Generate .debug_line content for the chain of line number entries
908 beginning at E, for segment SEG. */
911 process_entries (segT seg
, struct line_entry
*e
)
913 unsigned filenum
= 1;
916 unsigned flags
= DWARF2_LINE_DEFAULT_IS_STMT
? DWARF2_FLAG_BEGIN_STMT
: 0;
919 addressT frag_ofs
= 0;
920 addressT last_frag_ofs
;
921 struct line_entry
*next
;
927 if (filenum
!= e
->loc
.filenum
)
929 filenum
= e
->loc
.filenum
;
930 out_opcode (DW_LNS_set_file
);
931 out_uleb128 (filenum
);
935 if (column
!= e
->loc
.column
)
937 column
= e
->loc
.column
;
938 out_opcode (DW_LNS_set_column
);
939 out_uleb128 (column
);
943 if ((e
->loc
.flags
^ flags
) & DWARF2_FLAG_BEGIN_STMT
)
945 flags
= e
->loc
.flags
;
946 out_opcode (DW_LNS_negate_stmt
);
950 if (e
->loc
.flags
& DWARF2_FLAG_BEGIN_BLOCK
)
952 out_opcode (DW_LNS_set_basic_block
);
956 /* Don't try to optimize away redundant entries; gdb wants two
957 entries for a function where the code starts on the same line as
958 the {, and there's no way to identify that case here. Trust gcc
959 to optimize appropriately. */
960 if (1 /* line != e->loc.line || changed */)
962 int line_delta
= e
->loc
.line
- line
;
965 out_set_addr (seg
, e
->frag
, e
->frag_ofs
);
966 out_inc_line_addr (line_delta
, 0);
968 else if (frag
== e
->frag
)
969 out_inc_line_addr (line_delta
, e
->frag_ofs
- frag_ofs
);
971 relax_inc_line_addr (line_delta
, seg
, e
->frag
, e
->frag_ofs
,
975 frag_ofs
= e
->frag_ofs
;
978 else if (frag
== NULL
)
980 out_set_addr (seg
, e
->frag
, e
->frag_ofs
);
982 frag_ofs
= e
->frag_ofs
;
990 /* Emit a DW_LNE_end_sequence for the end of the section. */
991 last_frag
= last_frag_for_seg (seg
);
992 last_frag_ofs
= get_frag_fix (last_frag
);
993 if (frag
== last_frag
)
994 out_inc_line_addr (INT_MAX
, last_frag_ofs
- frag_ofs
);
996 relax_inc_line_addr (INT_MAX
, seg
, last_frag
, last_frag_ofs
,
1000 /* Emit the directory and file tables for .debug_line. */
1003 out_file_list (void)
1009 /* Emit directory list. */
1010 for (i
= 1; i
< dirs_in_use
; ++i
)
1012 size
= strlen (dirs
[i
]) + 1;
1013 cp
= frag_more (size
);
1014 memcpy (cp
, dirs
[i
], size
);
1019 for (i
= 1; i
< files_in_use
; ++i
)
1021 if (files
[i
].filename
== NULL
)
1023 as_bad (_("unassigned file number %ld"), (long) i
);
1024 /* Prevent a crash later, particularly for file 1. */
1025 files
[i
].filename
= "";
1029 size
= strlen (files
[i
].filename
) + 1;
1030 cp
= frag_more (size
);
1031 memcpy (cp
, files
[i
].filename
, size
);
1033 out_uleb128 (files
[i
].dir
); /* directory number */
1034 out_uleb128 (0); /* last modification timestamp */
1035 out_uleb128 (0); /* filesize */
1038 /* Terminate filename list. */
1042 /* Emit the collected .debug_line data. */
1045 out_debug_line (segT line_seg
)
1048 symbolS
*line_start
;
1049 symbolS
*prologue_end
;
1052 enum dwarf2_format d2f
;
1055 subseg_set (line_seg
, 0);
1057 line_start
= symbol_temp_new_now ();
1058 prologue_end
= symbol_temp_make ();
1059 line_end
= symbol_temp_make ();
1061 /* Total length of the information for this compilation unit. */
1062 expr
.X_op
= O_subtract
;
1063 expr
.X_add_symbol
= line_end
;
1064 expr
.X_op_symbol
= line_start
;
1066 d2f
= DWARF2_FORMAT ();
1067 if (d2f
== dwarf2_format_32bit
)
1069 expr
.X_add_number
= -4;
1070 emit_expr (&expr
, 4);
1073 else if (d2f
== dwarf2_format_64bit
)
1075 expr
.X_add_number
= -12;
1077 emit_expr (&expr
, 8);
1080 else if (d2f
== dwarf2_format_64bit_irix
)
1082 expr
.X_add_number
= -8;
1083 emit_expr (&expr
, 8);
1088 as_fatal (_("internal error: unknown dwarf2 format"));
1094 /* Length of the prologue following this length. */
1095 expr
.X_op
= O_subtract
;
1096 expr
.X_add_symbol
= prologue_end
;
1097 expr
.X_op_symbol
= line_start
;
1098 expr
.X_add_number
= - (4 + 2 + 4);
1099 emit_expr (&expr
, sizeof_offset
);
1101 /* Parameters of the state machine. */
1102 out_byte (DWARF2_LINE_MIN_INSN_LENGTH
);
1103 out_byte (DWARF2_LINE_DEFAULT_IS_STMT
);
1104 out_byte (DWARF2_LINE_BASE
);
1105 out_byte (DWARF2_LINE_RANGE
);
1106 out_byte (DWARF2_LINE_OPCODE_BASE
);
1108 /* Standard opcode lengths. */
1109 out_byte (0); /* DW_LNS_copy */
1110 out_byte (1); /* DW_LNS_advance_pc */
1111 out_byte (1); /* DW_LNS_advance_line */
1112 out_byte (1); /* DW_LNS_set_file */
1113 out_byte (1); /* DW_LNS_set_column */
1114 out_byte (0); /* DW_LNS_negate_stmt */
1115 out_byte (0); /* DW_LNS_set_basic_block */
1116 out_byte (0); /* DW_LNS_const_add_pc */
1117 out_byte (1); /* DW_LNS_fixed_advance_pc */
1121 symbol_set_value_now (prologue_end
);
1123 /* For each section, emit a statement program. */
1124 for (s
= all_segs
; s
; s
= s
->next
)
1125 process_entries (s
->seg
, s
->head
->head
);
1127 symbol_set_value_now (line_end
);
1130 /* Emit data for .debug_aranges. */
1133 out_debug_aranges (segT aranges_seg
, segT info_seg
)
1135 unsigned int addr_size
= sizeof_address
;
1136 addressT size
, skip
;
1141 size
= 4 + 2 + 4 + 1 + 1;
1143 skip
= 2 * addr_size
- (size
& (2 * addr_size
- 1));
1144 if (skip
== 2 * addr_size
)
1148 for (s
= all_segs
; s
; s
= s
->next
)
1149 size
+= 2 * addr_size
;
1151 size
+= 2 * addr_size
;
1153 subseg_set (aranges_seg
, 0);
1155 /* Length of the compilation unit. */
1156 out_four (size
- 4);
1161 /* Offset to .debug_info. */
1162 /* ??? sizeof_offset */
1163 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg
), 4);
1165 /* Size of an address (offset portion). */
1166 out_byte (addr_size
);
1168 /* Size of a segment descriptor. */
1171 /* Align the header. */
1173 frag_align (ffs (2 * addr_size
) - 1, 0, 0);
1175 for (s
= all_segs
; s
; s
= s
->next
)
1180 frag
= first_frag_for_seg (s
->seg
);
1181 beg
= symbol_temp_new (s
->seg
, 0, frag
);
1182 s
->text_start
= beg
;
1184 frag
= last_frag_for_seg (s
->seg
);
1185 end
= symbol_temp_new (s
->seg
, get_frag_fix (frag
), frag
);
1188 expr
.X_op
= O_symbol
;
1189 expr
.X_add_symbol
= beg
;
1190 expr
.X_add_number
= 0;
1191 emit_expr (&expr
, addr_size
);
1193 expr
.X_op
= O_subtract
;
1194 expr
.X_add_symbol
= end
;
1195 expr
.X_op_symbol
= beg
;
1196 expr
.X_add_number
= 0;
1197 emit_expr (&expr
, addr_size
);
1200 p
= frag_more (2 * addr_size
);
1201 md_number_to_chars (p
, 0, addr_size
);
1202 md_number_to_chars (p
+ addr_size
, 0, addr_size
);
1205 /* Emit data for .debug_abbrev. Note that this must be kept in
1206 sync with out_debug_info below. */
1209 out_debug_abbrev (segT abbrev_seg
)
1211 subseg_set (abbrev_seg
, 0);
1214 out_uleb128 (DW_TAG_compile_unit
);
1215 out_byte (DW_CHILDREN_no
);
1216 out_abbrev (DW_AT_stmt_list
, DW_FORM_data4
);
1217 if (all_segs
->next
== NULL
)
1219 out_abbrev (DW_AT_low_pc
, DW_FORM_addr
);
1220 out_abbrev (DW_AT_high_pc
, DW_FORM_addr
);
1222 out_abbrev (DW_AT_name
, DW_FORM_string
);
1223 out_abbrev (DW_AT_comp_dir
, DW_FORM_string
);
1224 out_abbrev (DW_AT_producer
, DW_FORM_string
);
1225 out_abbrev (DW_AT_language
, DW_FORM_data2
);
1228 /* Terminate the abbreviations for this compilation unit. */
1232 /* Emit a description of this compilation unit for .debug_info. */
1235 out_debug_info (segT info_seg
, segT abbrev_seg
, segT line_seg
)
1240 symbolS
*info_start
;
1244 enum dwarf2_format d2f
;
1247 subseg_set (info_seg
, 0);
1249 info_start
= symbol_temp_new_now ();
1250 info_end
= symbol_temp_make ();
1252 /* Compilation Unit length. */
1253 expr
.X_op
= O_subtract
;
1254 expr
.X_add_symbol
= info_end
;
1255 expr
.X_op_symbol
= info_start
;
1257 d2f
= DWARF2_FORMAT ();
1258 if (d2f
== dwarf2_format_32bit
)
1260 expr
.X_add_number
= -4;
1261 emit_expr (&expr
, 4);
1264 else if (d2f
== dwarf2_format_64bit
)
1266 expr
.X_add_number
= -12;
1268 emit_expr (&expr
, 8);
1271 else if (d2f
== dwarf2_format_64bit_irix
)
1273 expr
.X_add_number
= -8;
1274 emit_expr (&expr
, 8);
1279 as_fatal (_("internal error: unknown dwarf2 format"));
1282 /* DWARF version. */
1285 /* .debug_abbrev offset */
1286 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg
), sizeof_offset
);
1288 /* Target address size. */
1289 out_byte (sizeof_address
);
1291 /* DW_TAG_compile_unit DIE abbrev */
1294 /* DW_AT_stmt_list */
1295 /* ??? sizeof_offset */
1296 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg
), 4);
1298 /* These two attributes may only be emitted if all of the code is
1299 contiguous. Multiple sections are not that. */
1300 if (all_segs
->next
== NULL
)
1303 expr
.X_op
= O_symbol
;
1304 expr
.X_add_symbol
= all_segs
->text_start
;
1305 expr
.X_add_number
= 0;
1306 emit_expr (&expr
, sizeof_address
);
1309 expr
.X_op
= O_symbol
;
1310 expr
.X_add_symbol
= all_segs
->text_end
;
1311 expr
.X_add_number
= 0;
1312 emit_expr (&expr
, sizeof_address
);
1315 /* DW_AT_name. We don't have the actual file name that was present
1316 on the command line, so assume files[1] is the main input file.
1317 We're not supposed to get called unless at least one line number
1318 entry was emitted, so this should always be defined. */
1319 if (!files
|| files_in_use
< 1)
1323 len
= strlen (dirs
[files
[1].dir
]);
1324 p
= frag_more (len
+ 1);
1325 memcpy (p
, dirs
[files
[1].dir
], len
);
1328 len
= strlen (files
[1].filename
) + 1;
1329 p
= frag_more (len
);
1330 memcpy (p
, files
[1].filename
, len
);
1332 /* DW_AT_comp_dir */
1333 comp_dir
= getpwd ();
1334 len
= strlen (comp_dir
) + 1;
1335 p
= frag_more (len
);
1336 memcpy (p
, comp_dir
, len
);
1338 /* DW_AT_producer */
1339 sprintf (producer
, "GNU AS %s", VERSION
);
1340 len
= strlen (producer
) + 1;
1341 p
= frag_more (len
);
1342 memcpy (p
, producer
, len
);
1344 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1345 dwarf2 draft has no standard code for assembler. */
1346 out_two (DW_LANG_Mips_Assembler
);
1348 symbol_set_value_now (info_end
);
1351 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1352 were any .file/.loc directives, or --gdwarf2 was given, or if the
1353 file has a non-empty .debug_info section. If we emit .debug_line,
1354 and the .debug_info section is empty, we also emit .debug_info,
1355 .debug_aranges and .debug_abbrev. ALL_SEGS will be non-null if
1356 there were any .file/.loc directives, or --gdwarf2 was given and
1357 there were any located instructions emitted. */
1360 dwarf2_finish (void)
1365 int emit_other_sections
= 0;
1367 info_seg
= bfd_get_section_by_name (stdoutput
, ".debug_info");
1368 emit_other_sections
= info_seg
== NULL
|| !seg_not_empty_p (info_seg
);
1370 if (!all_segs
&& emit_other_sections
)
1371 /* There is no line information and no non-empty .debug_info
1375 /* Calculate the size of an address for the target machine. */
1376 sizeof_address
= DWARF2_ADDR_SIZE (stdoutput
);
1378 /* Create and switch to the line number section. */
1379 line_seg
= subseg_new (".debug_line", 0);
1380 bfd_set_section_flags (stdoutput
, line_seg
, SEC_READONLY
| SEC_DEBUGGING
);
1382 /* For each subsection, chain the debug entries together. */
1383 for (s
= all_segs
; s
; s
= s
->next
)
1385 struct line_subseg
*ss
= s
->head
;
1386 struct line_entry
**ptail
= ss
->ptail
;
1388 while ((ss
= ss
->next
) != NULL
)
1395 out_debug_line (line_seg
);
1397 /* If this is assembler generated line info, and there is no
1398 debug_info already, we need .debug_info and .debug_abbrev
1399 sections as well. */
1400 if (emit_other_sections
)
1407 info_seg
= subseg_new (".debug_info", 0);
1408 abbrev_seg
= subseg_new (".debug_abbrev", 0);
1409 aranges_seg
= subseg_new (".debug_aranges", 0);
1411 bfd_set_section_flags (stdoutput
, info_seg
,
1412 SEC_READONLY
| SEC_DEBUGGING
);
1413 bfd_set_section_flags (stdoutput
, abbrev_seg
,
1414 SEC_READONLY
| SEC_DEBUGGING
);
1415 bfd_set_section_flags (stdoutput
, aranges_seg
,
1416 SEC_READONLY
| SEC_DEBUGGING
);
1418 record_alignment (aranges_seg
, ffs (2 * sizeof_address
) - 1);
1420 out_debug_aranges (aranges_seg
, info_seg
);
1421 out_debug_abbrev (abbrev_seg
);
1422 out_debug_info (info_seg
, abbrev_seg
, line_seg
);