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);
60 #include "elf/dwarf2.h"
62 /* Since we can't generate the prolog until the body is complete, we
63 use three different subsegments for .debug_line: one holding the
64 prolog, one for the directory and filename info, and one for the
65 body ("statement program"). */
70 /* First special line opcde - leave room for the standard opcodes.
71 Note: If you want to change this, you'll have to update the
72 "standard_opcode_lengths" table that is emitted below in
74 #define DWARF2_LINE_OPCODE_BASE 10
76 #ifndef DWARF2_LINE_BASE
77 /* Minimum line offset in a special line info. opcode. This value
78 was chosen to give a reasonable range of values. */
79 # define DWARF2_LINE_BASE -5
82 /* Range of line offsets in a special line info. opcode. */
83 #ifndef DWARF2_LINE_RANGE
84 # define DWARF2_LINE_RANGE 14
87 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
88 /* Define the architecture-dependent minimum instruction length (in
89 bytes). This value should be rather too small than too big. */
90 # define DWARF2_LINE_MIN_INSN_LENGTH 1
93 /* Flag that indicates the initial value of the is_stmt_start flag.
94 In the present implementation, we do not mark any lines as
95 the beginning of a source statement, because that information
96 is not made available by the GCC front-end. */
97 #define DWARF2_LINE_DEFAULT_IS_STMT 1
99 /* Given a special op, return the line skip amount. */
100 #define SPECIAL_LINE(op) \
101 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
103 /* Given a special op, return the address skip amount (in units of
104 DWARF2_LINE_MIN_INSN_LENGTH. */
105 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
107 /* The maximum address skip amount that can be encoded with a special op. */
108 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
111 struct line_entry
*next
;
114 struct dwarf2_line_info loc
;
118 struct line_subseg
*next
;
120 struct line_entry
*head
;
121 struct line_entry
**ptail
;
125 struct line_seg
*next
;
127 struct line_subseg
*head
;
132 /* Collects data for all line table entries during assembly. */
133 static struct line_seg
*all_segs
;
136 const char *filename
;
140 /* Table of files used by .debug_line. */
141 static struct file_entry
*files
;
142 static unsigned int files_in_use
;
143 static unsigned int files_allocated
;
145 /* Table of directories used by .debug_line. */
147 static unsigned int dirs_in_use
;
148 static unsigned int dirs_allocated
;
150 /* TRUE when we've seen a .loc directive recently. Used to avoid
151 doing work when there's nothing to do. */
152 static bfd_boolean loc_directive_seen
;
154 /* Current location as indicated by the most recent .loc directive. */
155 static struct dwarf2_line_info current
;
157 /* Fake label name. */
158 static char const fake_label_name
[] = ".L0\001";
160 /* The size of an address on the target. */
161 static unsigned int sizeof_address
;
163 static struct line_subseg
*get_line_subseg
PARAMS ((segT
, subsegT
));
164 static unsigned int get_filenum
PARAMS ((const char *, unsigned int));
165 static struct frag
*first_frag_for_seg
PARAMS ((segT
));
166 static struct frag
*last_frag_for_seg
PARAMS ((segT
));
167 static void out_byte
PARAMS ((int));
168 static void out_opcode
PARAMS ((int));
169 static void out_two
PARAMS ((int));
170 static void out_four
PARAMS ((int));
171 static void out_abbrev
PARAMS ((int, int));
172 static void out_uleb128
PARAMS ((addressT
));
173 static symbolS
*symbol_new_now
PARAMS ((void));
174 static void set_symbol_value_now
PARAMS ((symbolS
*));
175 static offsetT get_frag_fix
PARAMS ((fragS
*));
176 static void out_set_addr
PARAMS ((segT
, fragS
*, addressT
));
177 static int size_inc_line_addr
PARAMS ((int, addressT
));
178 static void emit_inc_line_addr
PARAMS ((int, addressT
, char *, int));
179 static void out_inc_line_addr
PARAMS ((int, addressT
));
180 static void relax_inc_line_addr
PARAMS ((int, segT
, fragS
*, addressT
,
182 static void process_entries
PARAMS ((segT
, struct line_entry
*));
183 static void out_file_list
PARAMS ((void));
184 static void out_debug_line
PARAMS ((segT
));
185 static void out_debug_aranges
PARAMS ((segT
, segT
));
186 static void out_debug_abbrev
PARAMS ((segT
));
187 static void out_debug_info
PARAMS ((segT
, segT
, segT
));
189 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
191 static struct line_subseg
*
192 get_line_subseg (seg
, subseg
)
196 static segT last_seg
;
197 static subsegT last_subseg
;
198 static struct line_subseg
*last_line_subseg
;
201 struct line_subseg
**pss
, *ss
;
203 if (seg
== last_seg
&& subseg
== last_subseg
)
204 return last_line_subseg
;
206 for (s
= all_segs
; s
; s
= s
->next
)
210 s
= (struct line_seg
*) xmalloc (sizeof (*s
));
217 for (pss
= &s
->head
; (ss
= *pss
) != NULL
; pss
= &ss
->next
)
219 if (ss
->subseg
== subseg
)
221 if (ss
->subseg
> subseg
)
225 ss
= (struct line_subseg
*) xmalloc (sizeof (*ss
));
229 ss
->ptail
= &ss
->head
;
234 last_subseg
= subseg
;
235 last_line_subseg
= ss
;
240 /* Record an entry for LOC ocurring at OFS within the current fragment. */
243 dwarf2_gen_line_info (ofs
, loc
)
245 struct dwarf2_line_info
*loc
;
247 struct line_subseg
*ss
;
248 struct line_entry
*e
;
249 static unsigned int line
= -1;
250 static unsigned int filenum
= -1;
252 /* Early out for as-yet incomplete location information. */
253 if (loc
->filenum
== 0 || loc
->line
== 0)
256 /* Don't emit sequences of line symbols for the same line when the
257 symbols apply to assembler code. It is necessary to emit
258 duplicate line symbols when a compiler asks for them, because GDB
259 uses them to determine the end of the prologue. */
260 if (debug_type
== DEBUG_DWARF2
261 && line
== loc
->line
&& filenum
== loc
->filenum
)
265 filenum
= loc
->filenum
;
267 e
= (struct line_entry
*) xmalloc (sizeof (*e
));
273 ss
= get_line_subseg (now_seg
, now_subseg
);
275 ss
->ptail
= &e
->next
;
280 struct dwarf2_line_info
*line
;
282 if (debug_type
== DEBUG_DWARF2
)
285 as_where (&filename
, &line
->line
);
286 line
->filenum
= get_filenum (filename
, 0);
288 line
->flags
= DWARF2_FLAG_BEGIN_STMT
;
294 /* Called for each machine instruction, or relatively atomic group of
295 machine instructions (ie built-in macro). The instruction or group
296 is SIZE bytes in length. If dwarf2 line number generation is called
297 for, emit a line statement appropriately. */
300 dwarf2_emit_insn (size
)
303 struct dwarf2_line_info loc
;
305 if (loc_directive_seen
)
307 /* Use the last location established by a .loc directive, not
308 the value returned by dwarf2_where(). That calls as_where()
309 which will return either the logical input file name (foo.c)
310 or the physical input file name (foo.s) and not the file name
311 specified in the most recent .loc directive (eg foo.h). */
314 /* Unless we generate DWARF2 debugging information for each
315 assembler line, we only emit one line symbol for one LOC. */
316 if (debug_type
!= DEBUG_DWARF2
)
317 loc_directive_seen
= FALSE
;
319 else if (debug_type
!= DEBUG_DWARF2
)
322 dwarf2_where (& loc
);
324 dwarf2_gen_line_info (frag_now_fix () - size
, &loc
);
327 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
328 allocate it on that file table slot, otherwise return the first
332 get_filenum (filename
, num
)
333 const char *filename
;
336 static unsigned int last_used
, last_used_dir_len
;
341 if (num
== 0 && last_used
)
343 if (! files
[last_used
].dir
344 && strcmp (filename
, files
[last_used
].filename
) == 0)
346 if (files
[last_used
].dir
347 && strncmp (filename
, dirs
[files
[last_used
].dir
],
348 last_used_dir_len
) == 0
349 && IS_DIR_SEPARATOR (filename
[last_used_dir_len
])
350 && strcmp (filename
+ last_used_dir_len
+ 1,
351 files
[last_used
].filename
) == 0)
355 file
= lbasename (filename
);
356 /* Don't make empty string from / or A: from A:/ . */
357 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
358 if (file
<= filename
+ 3)
361 if (file
== filename
+ 1)
364 dir_len
= file
- filename
;
370 for (dir
= 1; dir
< dirs_in_use
; ++dir
)
371 if (memcmp (filename
, dirs
[dir
], dir_len
) == 0
372 && dirs
[dir
][dir_len
] == '\0')
375 if (dir
>= dirs_in_use
)
377 if (dir
>= dirs_allocated
)
379 dirs_allocated
= dir
+ 32;
381 xrealloc (dirs
, (dir
+ 32) * sizeof (const char *));
384 dirs
[dir
] = xmalloc (dir_len
+ 1);
385 memcpy (dirs
[dir
], filename
, dir_len
);
386 dirs
[dir
][dir_len
] = '\0';
387 dirs_in_use
= dir
+ 1;
393 for (i
= 1; i
< files_in_use
; ++i
)
394 if (files
[i
].dir
== dir
395 && strcmp (file
, files
[i
].filename
) == 0)
398 last_used_dir_len
= dir_len
;
405 if (i
>= files_allocated
)
407 unsigned int old
= files_allocated
;
409 files_allocated
= i
+ 32;
410 files
= (struct file_entry
*)
411 xrealloc (files
, (i
+ 32) * sizeof (struct file_entry
));
413 memset (files
+ old
, 0, (i
+ 32 - old
) * sizeof (struct file_entry
));
416 files
[i
].filename
= num
? file
: xstrdup (file
);
418 files_in_use
= i
+ 1;
420 last_used_dir_len
= dir_len
;
425 /* Handle two forms of .file directive:
426 - Pass .file "source.c" to s_app_file
427 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
429 If an entry is added to the file table, return a pointer to the filename. */
432 dwarf2_directive_file (dummy
)
433 int dummy ATTRIBUTE_UNUSED
;
439 /* Continue to accept a bare string and pass it off. */
441 if (*input_line_pointer
== '"')
447 num
= get_absolute_expression ();
448 filename
= demand_copy_C_string (&filename_len
);
449 demand_empty_rest_of_line ();
453 as_bad (_("file number less than one"));
457 if (num
< (int) files_in_use
&& files
[num
].filename
!= 0)
459 as_bad (_("file number %ld already allocated"), (long) num
);
463 get_filenum (filename
, num
);
469 dwarf2_directive_loc (dummy
)
470 int dummy ATTRIBUTE_UNUSED
;
472 offsetT filenum
, line
, column
;
474 filenum
= get_absolute_expression ();
476 line
= get_absolute_expression ();
478 column
= get_absolute_expression ();
479 demand_empty_rest_of_line ();
483 as_bad (_("file number less than one"));
486 if (filenum
>= (int) files_in_use
|| files
[filenum
].filename
== 0)
488 as_bad (_("unassigned file number %ld"), (long) filenum
);
492 current
.filenum
= filenum
;
494 current
.column
= column
;
495 current
.flags
= DWARF2_FLAG_BEGIN_STMT
;
497 loc_directive_seen
= TRUE
;
502 if (files
[filenum
].dir
)
504 size_t dir_len
= strlen (dirs
[files
[filenum
].dir
]);
505 size_t file_len
= strlen (files
[filenum
].filename
);
506 char *cp
= (char *) alloca (dir_len
+ 1 + file_len
+ 1);
508 memcpy (cp
, dirs
[files
[filenum
].dir
], dir_len
);
510 memcpy (cp
+ dir_len
+ 1, files
[filenum
].filename
, file_len
);
511 cp
[dir_len
+ file_len
+ 1] = '\0';
512 listing_source_file (cp
);
515 listing_source_file (files
[filenum
].filename
);
516 listing_source_line (line
);
522 first_frag_for_seg (seg
)
525 frchainS
*f
, *first
= NULL
;
527 for (f
= frchain_root
; f
; f
= f
->frch_next
)
528 if (f
->frch_seg
== seg
529 && (! first
|| first
->frch_subseg
> f
->frch_subseg
))
532 return first
? first
->frch_root
: NULL
;
536 last_frag_for_seg (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. */
555 FRAG_APPEND_1_CHAR (byte
);
558 /* Emit a statement program opcode into the current segment. */
567 /* Emit a two-byte word into the current segment. */
573 md_number_to_chars (frag_more (2), data
, 2);
576 /* Emit a four byte word into the current segment. */
582 md_number_to_chars (frag_more (4), data
, 4);
585 /* Emit an unsigned "little-endian base 128" number. */
591 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
594 /* Emit a tuple for .debug_abbrev. */
597 out_abbrev (name
, form
)
604 /* Create a new fake symbol whose value is the current position. */
609 return symbol_new (fake_label_name
, now_seg
, frag_now_fix (), frag_now
);
612 /* Set the value of SYM to the current position in the current segment. */
615 set_symbol_value_now (sym
)
618 S_SET_SEGMENT (sym
, now_seg
);
619 S_SET_VALUE (sym
, frag_now_fix ());
620 symbol_set_frag (sym
, frag_now
);
623 /* Get the size of a fragment. */
634 /* If a fragment is the last in the chain, special measures must be
635 taken to find its size before relaxation, since it may be pending
636 on some subsegment chain. */
637 for (fr
= frchain_root
; fr
; fr
= fr
->frch_next
)
638 if (fr
->frch_last
== frag
)
640 long align_mask
= -1 << get_recorded_alignment (fr
->frch_seg
);
641 return (((char *) obstack_next_free (&fr
->frch_obstack
)
642 - frag
->fr_literal
) + ~align_mask
) & align_mask
;
648 /* Set an absolute address (may result in a relocation entry). */
651 out_set_addr (seg
, frag
, ofs
)
659 sym
= symbol_new (fake_label_name
, seg
, ofs
, frag
);
661 out_opcode (DW_LNS_extended_op
);
662 out_uleb128 (sizeof_address
+ 1);
664 out_opcode (DW_LNE_set_address
);
665 expr
.X_op
= O_symbol
;
666 expr
.X_add_symbol
= sym
;
667 expr
.X_add_number
= 0;
668 emit_expr (&expr
, sizeof_address
);
671 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
672 static void scale_addr_delta
PARAMS ((addressT
*));
675 scale_addr_delta (addr_delta
)
676 addressT
*addr_delta
;
678 static int printed_this
= 0;
679 if (*addr_delta
% DWARF2_LINE_MIN_INSN_LENGTH
!= 0)
682 as_bad("unaligned opcodes detected in executable segment");
685 *addr_delta
/= DWARF2_LINE_MIN_INSN_LENGTH
;
688 #define scale_addr_delta(A)
691 /* Encode a pair of line and address skips as efficiently as possible.
692 Note that the line skip is signed, whereas the address skip is unsigned.
694 The following two routines *must* be kept in sync. This is
695 enforced by making emit_inc_line_addr abort if we do not emit
696 exactly the expected number of bytes. */
699 size_inc_line_addr (line_delta
, addr_delta
)
703 unsigned int tmp
, opcode
;
706 /* Scale the address delta by the minimum instruction length. */
707 scale_addr_delta (&addr_delta
);
709 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
710 We cannot use special opcodes here, since we want the end_sequence
711 to emit the matrix entry. */
712 if (line_delta
== INT_MAX
)
714 if (addr_delta
== MAX_SPECIAL_ADDR_DELTA
)
717 len
= 1 + sizeof_leb128 (addr_delta
, 0);
721 /* Bias the line delta by the base. */
722 tmp
= line_delta
- DWARF2_LINE_BASE
;
724 /* If the line increment is out of range of a special opcode, we
725 must encode it with DW_LNS_advance_line. */
726 if (tmp
>= DWARF2_LINE_RANGE
)
728 len
= 1 + sizeof_leb128 (line_delta
, 1);
730 tmp
= 0 - DWARF2_LINE_BASE
;
733 /* Bias the opcode by the special opcode base. */
734 tmp
+= DWARF2_LINE_OPCODE_BASE
;
736 /* Avoid overflow when addr_delta is large. */
737 if (addr_delta
< 256 + MAX_SPECIAL_ADDR_DELTA
)
739 /* Try using a special opcode. */
740 opcode
= tmp
+ addr_delta
* DWARF2_LINE_RANGE
;
744 /* Try using DW_LNS_const_add_pc followed by special op. */
745 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
750 /* Otherwise use DW_LNS_advance_pc. */
751 len
+= 1 + sizeof_leb128 (addr_delta
, 0);
753 /* DW_LNS_copy or special opcode. */
760 emit_inc_line_addr (line_delta
, addr_delta
, p
, len
)
766 unsigned int tmp
, opcode
;
770 /* Scale the address delta by the minimum instruction length. */
771 scale_addr_delta (&addr_delta
);
773 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
774 We cannot use special opcodes here, since we want the end_sequence
775 to emit the matrix entry. */
776 if (line_delta
== INT_MAX
)
778 if (addr_delta
== MAX_SPECIAL_ADDR_DELTA
)
779 *p
++ = DW_LNS_const_add_pc
;
782 *p
++ = DW_LNS_advance_pc
;
783 p
+= output_leb128 (p
, addr_delta
, 0);
786 *p
++ = DW_LNS_extended_op
;
788 *p
++ = DW_LNE_end_sequence
;
792 /* Bias the line delta by the base. */
793 tmp
= line_delta
- DWARF2_LINE_BASE
;
795 /* If the line increment is out of range of a special opcode, we
796 must encode it with DW_LNS_advance_line. */
797 if (tmp
>= DWARF2_LINE_RANGE
)
799 *p
++ = DW_LNS_advance_line
;
800 p
+= output_leb128 (p
, line_delta
, 1);
802 /* Prettier, I think, to use DW_LNS_copy instead of a
803 "line +0, addr +0" special opcode. */
811 tmp
= 0 - DWARF2_LINE_BASE
;
815 /* Bias the opcode by the special opcode base. */
816 tmp
+= DWARF2_LINE_OPCODE_BASE
;
818 /* Avoid overflow when addr_delta is large. */
819 if (addr_delta
< 256 + MAX_SPECIAL_ADDR_DELTA
)
821 /* Try using a special opcode. */
822 opcode
= tmp
+ addr_delta
* DWARF2_LINE_RANGE
;
829 /* Try using DW_LNS_const_add_pc followed by special op. */
830 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
833 *p
++ = DW_LNS_const_add_pc
;
839 /* Otherwise use DW_LNS_advance_pc. */
840 *p
++ = DW_LNS_advance_pc
;
841 p
+= output_leb128 (p
, addr_delta
, 0);
852 /* Handy routine to combine calls to the above two routines. */
855 out_inc_line_addr (line_delta
, addr_delta
)
859 int len
= size_inc_line_addr (line_delta
, addr_delta
);
860 emit_inc_line_addr (line_delta
, addr_delta
, frag_more (len
), len
);
863 /* Generate a variant frag that we can use to relax address/line
864 increments between fragments of the target segment. */
867 relax_inc_line_addr (line_delta
, seg
, to_frag
, to_ofs
, from_frag
, from_ofs
)
870 fragS
*to_frag
, *from_frag
;
871 addressT to_ofs
, from_ofs
;
873 symbolS
*to_sym
, *from_sym
;
877 to_sym
= symbol_new (fake_label_name
, seg
, to_ofs
, to_frag
);
878 from_sym
= symbol_new (fake_label_name
, seg
, from_ofs
, from_frag
);
880 expr
.X_op
= O_subtract
;
881 expr
.X_add_symbol
= to_sym
;
882 expr
.X_op_symbol
= from_sym
;
883 expr
.X_add_number
= 0;
885 /* The maximum size of the frag is the line delta with a maximum
886 sized address delta. */
887 max_chars
= size_inc_line_addr (line_delta
, -DWARF2_LINE_MIN_INSN_LENGTH
);
889 frag_var (rs_dwarf2dbg
, max_chars
, max_chars
, 1,
890 make_expr_symbol (&expr
), line_delta
, NULL
);
893 /* The function estimates the size of a rs_dwarf2dbg variant frag
894 based on the current values of the symbols. It is called before
895 the relaxation loop. We set fr_subtype to the expected length. */
898 dwarf2dbg_estimate_size_before_relax (frag
)
904 addr_delta
= resolve_symbol_value (frag
->fr_symbol
);
905 size
= size_inc_line_addr (frag
->fr_offset
, addr_delta
);
907 frag
->fr_subtype
= size
;
912 /* This function relaxes a rs_dwarf2dbg variant frag based on the
913 current values of the symbols. fr_subtype is the current length
914 of the frag. This returns the change in frag length. */
917 dwarf2dbg_relax_frag (frag
)
920 int old_size
, new_size
;
922 old_size
= frag
->fr_subtype
;
923 new_size
= dwarf2dbg_estimate_size_before_relax (frag
);
925 return new_size
- old_size
;
928 /* This function converts a rs_dwarf2dbg variant frag into a normal
929 fill frag. This is called after all relaxation has been done.
930 fr_subtype will be the desired length of the frag. */
933 dwarf2dbg_convert_frag (frag
)
938 addr_diff
= resolve_symbol_value (frag
->fr_symbol
);
940 /* fr_var carries the max_chars that we created the fragment with.
941 fr_subtype carries the current expected length. We must, of
942 course, have allocated enough memory earlier. */
943 assert (frag
->fr_var
>= (int) frag
->fr_subtype
);
945 emit_inc_line_addr (frag
->fr_offset
, addr_diff
,
946 frag
->fr_literal
+ frag
->fr_fix
, frag
->fr_subtype
);
948 frag
->fr_fix
+= frag
->fr_subtype
;
949 frag
->fr_type
= rs_fill
;
954 /* Generate .debug_line content for the chain of line number entries
955 beginning at E, for segment SEG. */
958 process_entries (seg
, e
)
960 struct line_entry
*e
;
962 unsigned filenum
= 1;
965 unsigned flags
= DWARF2_LINE_DEFAULT_IS_STMT
? DWARF2_FLAG_BEGIN_STMT
: 0;
968 addressT frag_ofs
= 0;
969 addressT last_frag_ofs
;
970 struct line_entry
*next
;
976 if (filenum
!= e
->loc
.filenum
)
978 filenum
= e
->loc
.filenum
;
979 out_opcode (DW_LNS_set_file
);
980 out_uleb128 (filenum
);
984 if (column
!= e
->loc
.column
)
986 column
= e
->loc
.column
;
987 out_opcode (DW_LNS_set_column
);
988 out_uleb128 (column
);
992 if ((e
->loc
.flags
^ flags
) & DWARF2_FLAG_BEGIN_STMT
)
994 flags
= e
->loc
.flags
;
995 out_opcode (DW_LNS_negate_stmt
);
999 if (e
->loc
.flags
& DWARF2_FLAG_BEGIN_BLOCK
)
1001 out_opcode (DW_LNS_set_basic_block
);
1005 /* Don't try to optimize away redundant entries; gdb wants two
1006 entries for a function where the code starts on the same line as
1007 the {, and there's no way to identify that case here. Trust gcc
1008 to optimize appropriately. */
1009 if (1 /* line != e->loc.line || changed */)
1011 int line_delta
= e
->loc
.line
- line
;
1014 out_set_addr (seg
, e
->frag
, e
->frag_ofs
);
1015 out_inc_line_addr (line_delta
, 0);
1017 else if (frag
== e
->frag
)
1018 out_inc_line_addr (line_delta
, e
->frag_ofs
- frag_ofs
);
1020 relax_inc_line_addr (line_delta
, seg
, e
->frag
, e
->frag_ofs
,
1024 frag_ofs
= e
->frag_ofs
;
1027 else if (frag
== NULL
)
1029 out_set_addr (seg
, e
->frag
, e
->frag_ofs
);
1031 frag_ofs
= e
->frag_ofs
;
1039 /* Emit a DW_LNE_end_sequence for the end of the section. */
1040 last_frag
= last_frag_for_seg (seg
);
1041 last_frag_ofs
= get_frag_fix (last_frag
);
1042 if (frag
== last_frag
)
1043 out_inc_line_addr (INT_MAX
, last_frag_ofs
- frag_ofs
);
1045 relax_inc_line_addr (INT_MAX
, seg
, last_frag
, last_frag_ofs
,
1049 /* Emit the directory and file tables for .debug_line. */
1058 /* Emit directory list. */
1059 for (i
= 1; i
< dirs_in_use
; ++i
)
1061 size
= strlen (dirs
[i
]) + 1;
1062 cp
= frag_more (size
);
1063 memcpy (cp
, dirs
[i
], size
);
1068 for (i
= 1; i
< files_in_use
; ++i
)
1070 if (files
[i
].filename
== NULL
)
1072 as_bad (_("unassigned file number %ld"), (long) i
);
1076 size
= strlen (files
[i
].filename
) + 1;
1077 cp
= frag_more (size
);
1078 memcpy (cp
, files
[i
].filename
, size
);
1080 out_uleb128 (files
[i
].dir
); /* directory number */
1081 out_uleb128 (0); /* last modification timestamp */
1082 out_uleb128 (0); /* filesize */
1085 /* Terminate filename list. */
1089 /* Emit the collected .debug_line data. */
1092 out_debug_line (line_seg
)
1096 symbolS
*line_start
;
1097 symbolS
*prologue_end
;
1100 enum dwarf2_format d2f
;
1103 subseg_set (line_seg
, 0);
1105 line_start
= symbol_new_now ();
1106 prologue_end
= symbol_make (fake_label_name
);
1107 line_end
= symbol_make (fake_label_name
);
1109 /* Total length of the information for this compilation unit. */
1110 expr
.X_op
= O_subtract
;
1111 expr
.X_add_symbol
= line_end
;
1112 expr
.X_op_symbol
= line_start
;
1114 d2f
= DWARF2_FORMAT ();
1115 if (d2f
== dwarf2_format_32bit
)
1117 expr
.X_add_number
= -4;
1118 emit_expr (&expr
, 4);
1121 else if (d2f
== dwarf2_format_64bit
)
1123 expr
.X_add_number
= -12;
1125 emit_expr (&expr
, 8);
1128 else if (d2f
== dwarf2_format_64bit_irix
)
1130 expr
.X_add_number
= -8;
1131 emit_expr (&expr
, 8);
1136 as_fatal (_("internal error: unknown dwarf2 format"));
1142 /* Length of the prologue following this length. */
1143 expr
.X_op
= O_subtract
;
1144 expr
.X_add_symbol
= prologue_end
;
1145 expr
.X_op_symbol
= line_start
;
1146 expr
.X_add_number
= - (4 + 2 + 4);
1147 emit_expr (&expr
, sizeof_offset
);
1149 /* Parameters of the state machine. */
1150 out_byte (DWARF2_LINE_MIN_INSN_LENGTH
);
1151 out_byte (DWARF2_LINE_DEFAULT_IS_STMT
);
1152 out_byte (DWARF2_LINE_BASE
);
1153 out_byte (DWARF2_LINE_RANGE
);
1154 out_byte (DWARF2_LINE_OPCODE_BASE
);
1156 /* Standard opcode lengths. */
1157 out_byte (0); /* DW_LNS_copy */
1158 out_byte (1); /* DW_LNS_advance_pc */
1159 out_byte (1); /* DW_LNS_advance_line */
1160 out_byte (1); /* DW_LNS_set_file */
1161 out_byte (1); /* DW_LNS_set_column */
1162 out_byte (0); /* DW_LNS_negate_stmt */
1163 out_byte (0); /* DW_LNS_set_basic_block */
1164 out_byte (0); /* DW_LNS_const_add_pc */
1165 out_byte (1); /* DW_LNS_fixed_advance_pc */
1169 set_symbol_value_now (prologue_end
);
1171 /* For each section, emit a statement program. */
1172 for (s
= all_segs
; s
; s
= s
->next
)
1173 process_entries (s
->seg
, s
->head
->head
);
1175 set_symbol_value_now (line_end
);
1178 /* Emit data for .debug_aranges. */
1181 out_debug_aranges (aranges_seg
, info_seg
)
1185 unsigned int addr_size
= sizeof_address
;
1186 addressT size
, skip
;
1191 size
= 4 + 2 + 4 + 1 + 1;
1193 skip
= 2 * addr_size
- (size
& (2 * addr_size
- 1));
1194 if (skip
== 2 * addr_size
)
1198 for (s
= all_segs
; s
; s
= s
->next
)
1199 size
+= 2 * addr_size
;
1201 size
+= 2 * addr_size
;
1203 subseg_set (aranges_seg
, 0);
1205 /* Length of the compilation unit. */
1206 out_four (size
- 4);
1211 /* Offset to .debug_info. */
1212 expr
.X_op
= O_symbol
;
1213 expr
.X_add_symbol
= section_symbol (info_seg
);
1214 expr
.X_add_number
= 0;
1215 emit_expr (&expr
, 4);
1217 /* Size of an address (offset portion). */
1218 out_byte (addr_size
);
1220 /* Size of a segment descriptor. */
1223 /* Align the header. */
1225 frag_align (ffs (2 * addr_size
) - 1, 0, 0);
1227 for (s
= all_segs
; s
; s
= s
->next
)
1232 frag
= first_frag_for_seg (s
->seg
);
1233 beg
= symbol_new (fake_label_name
, s
->seg
, 0, frag
);
1234 s
->text_start
= beg
;
1236 frag
= last_frag_for_seg (s
->seg
);
1237 end
= symbol_new (fake_label_name
, s
->seg
, get_frag_fix (frag
), frag
);
1240 expr
.X_op
= O_symbol
;
1241 expr
.X_add_symbol
= beg
;
1242 expr
.X_add_number
= 0;
1243 emit_expr (&expr
, addr_size
);
1245 expr
.X_op
= O_subtract
;
1246 expr
.X_add_symbol
= end
;
1247 expr
.X_op_symbol
= beg
;
1248 expr
.X_add_number
= 0;
1249 emit_expr (&expr
, addr_size
);
1252 p
= frag_more (2 * addr_size
);
1253 md_number_to_chars (p
, 0, addr_size
);
1254 md_number_to_chars (p
+ addr_size
, 0, addr_size
);
1257 /* Emit data for .debug_abbrev. Note that this must be kept in
1258 sync with out_debug_info below. */
1261 out_debug_abbrev (abbrev_seg
)
1264 subseg_set (abbrev_seg
, 0);
1267 out_uleb128 (DW_TAG_compile_unit
);
1268 out_byte (DW_CHILDREN_no
);
1269 out_abbrev (DW_AT_stmt_list
, DW_FORM_data4
);
1270 if (all_segs
->next
== NULL
)
1272 out_abbrev (DW_AT_low_pc
, DW_FORM_addr
);
1273 out_abbrev (DW_AT_high_pc
, DW_FORM_addr
);
1275 out_abbrev (DW_AT_name
, DW_FORM_string
);
1276 out_abbrev (DW_AT_comp_dir
, DW_FORM_string
);
1277 out_abbrev (DW_AT_producer
, DW_FORM_string
);
1278 out_abbrev (DW_AT_language
, DW_FORM_data2
);
1281 /* Terminate the abbreviations for this compilation unit. */
1285 /* Emit a description of this compilation unit for .debug_info. */
1288 out_debug_info (info_seg
, abbrev_seg
, line_seg
)
1296 symbolS
*info_start
;
1300 enum dwarf2_format d2f
;
1303 subseg_set (info_seg
, 0);
1305 info_start
= symbol_new_now ();
1306 info_end
= symbol_make (fake_label_name
);
1308 /* Compilation Unit length. */
1309 expr
.X_op
= O_subtract
;
1310 expr
.X_add_symbol
= info_end
;
1311 expr
.X_op_symbol
= info_start
;
1313 d2f
= DWARF2_FORMAT ();
1314 if (d2f
== dwarf2_format_32bit
)
1316 expr
.X_add_number
= -4;
1317 emit_expr (&expr
, 4);
1320 else if (d2f
== dwarf2_format_64bit
)
1322 expr
.X_add_number
= -12;
1324 emit_expr (&expr
, 8);
1327 else if (d2f
== dwarf2_format_64bit_irix
)
1329 expr
.X_add_number
= -8;
1330 emit_expr (&expr
, 8);
1335 as_fatal (_("internal error: unknown dwarf2 format"));
1338 /* DWARF version. */
1341 /* .debug_abbrev offset */
1342 expr
.X_op
= O_symbol
;
1343 expr
.X_add_symbol
= section_symbol (abbrev_seg
);
1344 expr
.X_add_number
= 0;
1345 emit_expr (&expr
, sizeof_offset
);
1347 /* Target address size. */
1348 out_byte (sizeof_address
);
1350 /* DW_TAG_compile_unit DIE abbrev */
1353 /* DW_AT_stmt_list */
1354 expr
.X_op
= O_symbol
;
1355 expr
.X_add_symbol
= section_symbol (line_seg
);
1356 expr
.X_add_number
= 0;
1357 emit_expr (&expr
, 4);
1359 /* These two attributes may only be emitted if all of the code is
1360 contiguous. Multiple sections are not that. */
1361 if (all_segs
->next
== NULL
)
1364 expr
.X_op
= O_symbol
;
1365 expr
.X_add_symbol
= all_segs
->text_start
;
1366 expr
.X_add_number
= 0;
1367 emit_expr (&expr
, sizeof_address
);
1370 expr
.X_op
= O_symbol
;
1371 expr
.X_add_symbol
= all_segs
->text_end
;
1372 expr
.X_add_number
= 0;
1373 emit_expr (&expr
, sizeof_address
);
1376 /* DW_AT_name. We don't have the actual file name that was present
1377 on the command line, so assume files[1] is the main input file.
1378 We're not supposed to get called unless at least one line number
1379 entry was emitted, so this should always be defined. */
1380 if (!files
|| files_in_use
< 1)
1384 len
= strlen (dirs
[files
[1].dir
]);
1385 p
= frag_more (len
+ 1);
1386 memcpy (p
, dirs
[files
[1].dir
], len
);
1389 len
= strlen (files
[1].filename
) + 1;
1390 p
= frag_more (len
);
1391 memcpy (p
, files
[1].filename
, len
);
1393 /* DW_AT_comp_dir */
1394 comp_dir
= getpwd ();
1395 len
= strlen (comp_dir
) + 1;
1396 p
= frag_more (len
);
1397 memcpy (p
, comp_dir
, len
);
1399 /* DW_AT_producer */
1400 sprintf (producer
, "GNU AS %s", VERSION
);
1401 len
= strlen (producer
) + 1;
1402 p
= frag_more (len
);
1403 memcpy (p
, producer
, len
);
1405 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1406 dwarf2 draft has no standard code for assembler. */
1407 out_two (DW_LANG_Mips_Assembler
);
1409 set_symbol_value_now (info_end
);
1418 /* We don't need to do anything unless:
1419 - Some debug information was recorded via .file/.loc
1420 - or, we are generating DWARF2 information ourself (--gdwarf2)
1421 - or, there is a user-provided .debug_info section which could
1422 reference the file table in the .debug_line section we generate
1424 if (all_segs
== NULL
1425 && debug_type
!= DEBUG_DWARF2
1426 && bfd_get_section_by_name (stdoutput
, ".debug_info") == NULL
)
1429 /* Calculate the size of an address for the target machine. */
1430 sizeof_address
= DWARF2_ADDR_SIZE (stdoutput
);
1432 /* Create and switch to the line number section. */
1433 line_seg
= subseg_new (".debug_line", 0);
1434 bfd_set_section_flags (stdoutput
, line_seg
, SEC_READONLY
);
1436 /* For each subsection, chain the debug entries together. */
1437 for (s
= all_segs
; s
; s
= s
->next
)
1439 struct line_subseg
*ss
= s
->head
;
1440 struct line_entry
**ptail
= ss
->ptail
;
1442 while ((ss
= ss
->next
) != NULL
)
1449 out_debug_line (line_seg
);
1451 /* If this is assembler generated line info, we need .debug_info
1452 and .debug_abbrev sections as well. */
1453 if (all_segs
!= NULL
&& debug_type
== DEBUG_DWARF2
)
1459 info_seg
= subseg_new (".debug_info", 0);
1460 abbrev_seg
= subseg_new (".debug_abbrev", 0);
1461 aranges_seg
= subseg_new (".debug_aranges", 0);
1463 bfd_set_section_flags (stdoutput
, info_seg
, SEC_READONLY
);
1464 bfd_set_section_flags (stdoutput
, abbrev_seg
, SEC_READONLY
);
1465 bfd_set_section_flags (stdoutput
, aranges_seg
, SEC_READONLY
);
1467 record_alignment (aranges_seg
, ffs (2 * sizeof_address
) - 1);
1469 out_debug_aranges (aranges_seg
, info_seg
);
1470 out_debug_abbrev (abbrev_seg
);
1471 out_debug_info (info_seg
, abbrev_seg
, line_seg
);
1482 dwarf2dbg_estimate_size_before_relax (frag
)
1483 fragS
*frag ATTRIBUTE_UNUSED
;
1485 as_fatal (_("dwarf2 is not supported for this object file format"));
1490 dwarf2dbg_relax_frag (frag
)
1491 fragS
*frag ATTRIBUTE_UNUSED
;
1493 as_fatal (_("dwarf2 is not supported for this object file format"));
1498 dwarf2dbg_convert_frag (frag
)
1499 fragS
*frag ATTRIBUTE_UNUSED
;
1501 as_fatal (_("dwarf2 is not supported for this object file format"));
1505 dwarf2_emit_insn (size
)
1506 int size ATTRIBUTE_UNUSED
;
1511 dwarf2_directive_file (dummy
)
1512 int dummy ATTRIBUTE_UNUSED
;
1519 dwarf2_directive_loc (dummy
)
1520 int dummy ATTRIBUTE_UNUSED
;
1522 as_fatal (_("dwarf2 is not supported for this object file format"));
1524 #endif /* BFD_ASSEMBLER */