1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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
27 .loc FILENO LINENO [COLUMN] [basic_block] [prologue_end] \
28 [epilogue_begin] [is_stmt VALUE] [isa VALUE]
33 #include "safe-ctype.h"
38 #ifdef HAVE_SYS_PARAM_H
39 #include <sys/param.h>
42 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
46 #include "dwarf2dbg.h"
47 #include <filenames.h>
49 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
50 /* We need to decide which character to use as a directory separator.
51 Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not
52 necessarily mean that the backslash character is the one to use.
53 Some environments, eg Cygwin, can support both naming conventions.
54 So we use the heuristic that we only need to use the backslash if
55 the path is an absolute path starting with a DOS style drive
56 selector. eg C: or D: */
57 # define INSERT_DIR_SEPARATOR(string, offset) \
62 && string[1] == ':') \
63 string [offset] = '\\'; \
65 string [offset] = '/'; \
69 # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
73 # define DWARF2_FORMAT() dwarf2_format_32bit
76 #ifndef DWARF2_ADDR_SIZE
77 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
82 #include "elf/dwarf2.h"
84 /* Since we can't generate the prolog until the body is complete, we
85 use three different subsegments for .debug_line: one holding the
86 prolog, one for the directory and filename info, and one for the
87 body ("statement program"). */
92 /* First special line opcde - leave room for the standard opcodes.
93 Note: If you want to change this, you'll have to update the
94 "standard_opcode_lengths" table that is emitted below in
96 #define DWARF2_LINE_OPCODE_BASE 13
98 #ifndef DWARF2_LINE_BASE
99 /* Minimum line offset in a special line info. opcode. This value
100 was chosen to give a reasonable range of values. */
101 # define DWARF2_LINE_BASE -5
104 /* Range of line offsets in a special line info. opcode. */
105 #ifndef DWARF2_LINE_RANGE
106 # define DWARF2_LINE_RANGE 14
109 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
110 /* Define the architecture-dependent minimum instruction length (in
111 bytes). This value should be rather too small than too big. */
112 # define DWARF2_LINE_MIN_INSN_LENGTH 1
115 /* Flag that indicates the initial value of the is_stmt_start flag. */
116 #define DWARF2_LINE_DEFAULT_IS_STMT 1
118 /* Given a special op, return the line skip amount. */
119 #define SPECIAL_LINE(op) \
120 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
122 /* Given a special op, return the address skip amount (in units of
123 DWARF2_LINE_MIN_INSN_LENGTH. */
124 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
126 /* The maximum address skip amount that can be encoded with a special op. */
127 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
130 struct line_entry
*next
;
132 struct dwarf2_line_info loc
;
136 struct line_subseg
*next
;
138 struct line_entry
*head
;
139 struct line_entry
**ptail
;
143 struct line_seg
*next
;
145 struct line_subseg
*head
;
150 /* Collects data for all line table entries during assembly. */
151 static struct line_seg
*all_segs
;
154 const char *filename
;
158 /* Table of files used by .debug_line. */
159 static struct file_entry
*files
;
160 static unsigned int files_in_use
;
161 static unsigned int files_allocated
;
163 /* Table of directories used by .debug_line. */
165 static unsigned int dirs_in_use
;
166 static unsigned int dirs_allocated
;
168 /* TRUE when we've seen a .loc directive recently. Used to avoid
169 doing work when there's nothing to do. */
170 static bfd_boolean loc_directive_seen
;
172 /* TRUE when we're supposed to set the basic block mark whenever a
174 bfd_boolean dwarf2_loc_mark_labels
;
176 /* Current location as indicated by the most recent .loc directive. */
177 static struct dwarf2_line_info current
= {
179 DWARF2_LINE_DEFAULT_IS_STMT
? DWARF2_FLAG_IS_STMT
: 0
182 /* The size of an address on the target. */
183 static unsigned int sizeof_address
;
185 static struct line_subseg
*get_line_subseg (segT
, subsegT
);
186 static unsigned int get_filenum (const char *, unsigned int);
187 static struct frag
*first_frag_for_seg (segT
);
188 static struct frag
*last_frag_for_seg (segT
);
189 static void out_byte (int);
190 static void out_opcode (int);
191 static void out_two (int);
192 static void out_four (int);
193 static void out_abbrev (int, int);
194 static void out_uleb128 (addressT
);
195 static offsetT
get_frag_fix (fragS
*, segT
);
196 static void out_set_addr (symbolS
*);
197 static int size_inc_line_addr (int, addressT
);
198 static void emit_inc_line_addr (int, addressT
, char *, int);
199 static void out_inc_line_addr (int, addressT
);
200 static void relax_inc_line_addr (int, symbolS
*, symbolS
*);
201 static void process_entries (segT
, struct line_entry
*);
202 static void out_file_list (void);
203 static void out_debug_line (segT
);
204 static void out_debug_aranges (segT
, segT
);
205 static void out_debug_abbrev (segT
);
206 static void out_debug_info (segT
, segT
, segT
);
208 #ifndef TC_DWARF2_EMIT_OFFSET
209 # define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
210 static void generic_dwarf2_emit_offset (symbolS
*, unsigned int);
212 /* Create an offset to .dwarf2_*. */
215 generic_dwarf2_emit_offset (symbolS
*symbol
, unsigned int size
)
219 expr
.X_op
= O_symbol
;
220 expr
.X_add_symbol
= symbol
;
221 expr
.X_add_number
= 0;
222 emit_expr (&expr
, size
);
226 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
228 static struct line_subseg
*
229 get_line_subseg (segT seg
, subsegT subseg
)
231 static segT last_seg
;
232 static subsegT last_subseg
;
233 static struct line_subseg
*last_line_subseg
;
235 struct line_seg
**ps
, *s
;
236 struct line_subseg
**pss
, *ss
;
238 if (seg
== last_seg
&& subseg
== last_subseg
)
239 return last_line_subseg
;
241 for (ps
= &all_segs
; (s
= *ps
) != NULL
; ps
= &s
->next
)
245 s
= (struct line_seg
*) xmalloc (sizeof (*s
));
252 for (pss
= &s
->head
; (ss
= *pss
) != NULL
; pss
= &ss
->next
)
254 if (ss
->subseg
== subseg
)
256 if (ss
->subseg
> subseg
)
260 ss
= (struct line_subseg
*) xmalloc (sizeof (*ss
));
264 ss
->ptail
= &ss
->head
;
269 last_subseg
= subseg
;
270 last_line_subseg
= ss
;
275 /* Record an entry for LOC occurring at LABEL. */
278 dwarf2_gen_line_info_1 (symbolS
*label
, struct dwarf2_line_info
*loc
)
280 struct line_subseg
*ss
;
281 struct line_entry
*e
;
283 e
= (struct line_entry
*) xmalloc (sizeof (*e
));
288 ss
= get_line_subseg (now_seg
, now_subseg
);
290 ss
->ptail
= &e
->next
;
293 /* Record an entry for LOC occurring at OFS within the current fragment. */
296 dwarf2_gen_line_info (addressT ofs
, struct dwarf2_line_info
*loc
)
298 static unsigned int line
= -1;
299 static unsigned int filenum
= -1;
303 /* Early out for as-yet incomplete location information. */
304 if (loc
->filenum
== 0 || loc
->line
== 0)
307 /* Don't emit sequences of line symbols for the same line when the
308 symbols apply to assembler code. It is necessary to emit
309 duplicate line symbols when a compiler asks for them, because GDB
310 uses them to determine the end of the prologue. */
311 if (debug_type
== DEBUG_DWARF2
312 && line
== loc
->line
&& filenum
== loc
->filenum
)
316 filenum
= loc
->filenum
;
318 sym
= symbol_temp_new (now_seg
, ofs
, frag_now
);
319 dwarf2_gen_line_info_1 (sym
, loc
);
322 /* Returns the current source information. If .file directives have
323 been encountered, the info for the corresponding source file is
324 returned. Otherwise, the info for the assembly source file is
328 dwarf2_where (struct dwarf2_line_info
*line
)
330 if (debug_type
== DEBUG_DWARF2
)
333 as_where (&filename
, &line
->line
);
334 line
->filenum
= get_filenum (filename
, 0);
336 line
->flags
= DWARF2_FLAG_IS_STMT
;
337 line
->isa
= current
.isa
;
343 /* A hook to allow the target backend to inform the line number state
344 machine of isa changes when assembler debug info is enabled. */
347 dwarf2_set_isa (unsigned int isa
)
352 /* Called for each machine instruction, or relatively atomic group of
353 machine instructions (ie built-in macro). The instruction or group
354 is SIZE bytes in length. If dwarf2 line number generation is called
355 for, emit a line statement appropriately. */
358 dwarf2_emit_insn (int size
)
360 struct dwarf2_line_info loc
;
362 if (loc_directive_seen
)
364 /* Use the last location established by a .loc directive, not
365 the value returned by dwarf2_where(). That calls as_where()
366 which will return either the logical input file name (foo.c)
367 or the physical input file name (foo.s) and not the file name
368 specified in the most recent .loc directive (eg foo.h). */
371 /* Unless we generate DWARF2 debugging information for each
372 assembler line, we only emit one line symbol for one LOC. */
373 if (debug_type
!= DEBUG_DWARF2
)
374 loc_directive_seen
= FALSE
;
376 else if (debug_type
!= DEBUG_DWARF2
)
381 dwarf2_gen_line_info (frag_now_fix () - size
, &loc
);
383 current
.flags
&= ~(DWARF2_FLAG_BASIC_BLOCK
384 | DWARF2_FLAG_PROLOGUE_END
385 | DWARF2_FLAG_EPILOGUE_BEGIN
);
388 /* Called for each (preferably code) label. If dwarf2_loc_mark_labels
389 is enabled, emit a basic block marker. */
392 dwarf2_emit_label (symbolS
*label
)
394 struct dwarf2_line_info loc
;
396 if (!dwarf2_loc_mark_labels
)
398 if (S_GET_SEGMENT (label
) != now_seg
)
400 if (!(bfd_get_section_flags (stdoutput
, now_seg
) & SEC_CODE
))
403 if (debug_type
== DEBUG_DWARF2
)
408 loc_directive_seen
= FALSE
;
411 loc
.flags
|= DWARF2_FLAG_BASIC_BLOCK
;
413 current
.flags
&= ~(DWARF2_FLAG_BASIC_BLOCK
414 | DWARF2_FLAG_PROLOGUE_END
415 | DWARF2_FLAG_EPILOGUE_BEGIN
);
417 dwarf2_gen_line_info_1 (label
, &loc
);
420 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
421 allocate it on that file table slot, otherwise return the first
425 get_filenum (const char *filename
, unsigned int num
)
427 static unsigned int last_used
, last_used_dir_len
;
432 if (num
== 0 && last_used
)
434 if (! files
[last_used
].dir
435 && strcmp (filename
, files
[last_used
].filename
) == 0)
437 if (files
[last_used
].dir
438 && strncmp (filename
, dirs
[files
[last_used
].dir
],
439 last_used_dir_len
) == 0
440 && IS_DIR_SEPARATOR (filename
[last_used_dir_len
])
441 && strcmp (filename
+ last_used_dir_len
+ 1,
442 files
[last_used
].filename
) == 0)
446 file
= lbasename (filename
);
447 /* Don't make empty string from / or A: from A:/ . */
448 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
449 if (file
<= filename
+ 3)
452 if (file
== filename
+ 1)
455 dir_len
= file
- filename
;
461 for (dir
= 1; dir
< dirs_in_use
; ++dir
)
462 if (strncmp (filename
, dirs
[dir
], dir_len
) == 0
463 && dirs
[dir
][dir_len
] == '\0')
466 if (dir
>= dirs_in_use
)
468 if (dir
>= dirs_allocated
)
470 dirs_allocated
= dir
+ 32;
472 xrealloc (dirs
, (dir
+ 32) * sizeof (const char *));
475 dirs
[dir
] = xmalloc (dir_len
+ 1);
476 memcpy (dirs
[dir
], filename
, dir_len
);
477 dirs
[dir
][dir_len
] = '\0';
478 dirs_in_use
= dir
+ 1;
484 for (i
= 1; i
< files_in_use
; ++i
)
485 if (files
[i
].dir
== dir
487 && strcmp (file
, files
[i
].filename
) == 0)
490 last_used_dir_len
= dir_len
;
497 if (i
>= files_allocated
)
499 unsigned int old
= files_allocated
;
501 files_allocated
= i
+ 32;
502 files
= (struct file_entry
*)
503 xrealloc (files
, (i
+ 32) * sizeof (struct file_entry
));
505 memset (files
+ old
, 0, (i
+ 32 - old
) * sizeof (struct file_entry
));
508 files
[i
].filename
= num
? file
: xstrdup (file
);
510 if (files_in_use
< i
+ 1)
511 files_in_use
= i
+ 1;
513 last_used_dir_len
= dir_len
;
518 /* Handle two forms of .file directive:
519 - Pass .file "source.c" to s_app_file
520 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
522 If an entry is added to the file table, return a pointer to the filename. */
525 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED
)
531 /* Continue to accept a bare string and pass it off. */
533 if (*input_line_pointer
== '"')
539 num
= get_absolute_expression ();
540 filename
= demand_copy_C_string (&filename_len
);
541 if (filename
== NULL
)
543 demand_empty_rest_of_line ();
547 as_bad (_("file number less than one"));
551 if (num
< (int) files_in_use
&& files
[num
].filename
!= 0)
553 as_bad (_("file number %ld already allocated"), (long) num
);
557 get_filenum (filename
, num
);
563 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED
)
565 offsetT filenum
, line
;
567 filenum
= get_absolute_expression ();
569 line
= get_absolute_expression ();
573 as_bad (_("file number less than one"));
576 if (filenum
>= (int) files_in_use
|| files
[filenum
].filename
== 0)
578 as_bad (_("unassigned file number %ld"), (long) filenum
);
582 current
.filenum
= filenum
;
588 if (files
[filenum
].dir
)
590 size_t dir_len
= strlen (dirs
[files
[filenum
].dir
]);
591 size_t file_len
= strlen (files
[filenum
].filename
);
592 char *cp
= (char *) alloca (dir_len
+ 1 + file_len
+ 1);
594 memcpy (cp
, dirs
[files
[filenum
].dir
], dir_len
);
595 INSERT_DIR_SEPARATOR (cp
, dir_len
);
596 memcpy (cp
+ dir_len
+ 1, files
[filenum
].filename
, file_len
);
597 cp
[dir_len
+ file_len
+ 1] = '\0';
598 listing_source_file (cp
);
601 listing_source_file (files
[filenum
].filename
);
602 listing_source_line (line
);
607 if (ISDIGIT (*input_line_pointer
))
609 current
.column
= get_absolute_expression ();
613 while (ISALPHA (*input_line_pointer
))
618 p
= input_line_pointer
;
619 c
= get_symbol_end ();
621 if (strcmp (p
, "basic_block") == 0)
623 current
.flags
|= DWARF2_FLAG_BASIC_BLOCK
;
624 *input_line_pointer
= c
;
626 else if (strcmp (p
, "prologue_end") == 0)
628 current
.flags
|= DWARF2_FLAG_PROLOGUE_END
;
629 *input_line_pointer
= c
;
631 else if (strcmp (p
, "epilogue_begin") == 0)
633 current
.flags
|= DWARF2_FLAG_EPILOGUE_BEGIN
;
634 *input_line_pointer
= c
;
636 else if (strcmp (p
, "is_stmt") == 0)
638 *input_line_pointer
= c
;
639 value
= get_absolute_expression ();
641 current
.flags
&= ~DWARF2_FLAG_IS_STMT
;
643 current
.flags
|= DWARF2_FLAG_IS_STMT
;
646 as_bad (_("is_stmt value not 0 or 1"));
650 else if (strcmp (p
, "isa") == 0)
652 *input_line_pointer
= c
;
653 value
= get_absolute_expression ();
658 as_bad (_("isa number less than zero"));
664 as_bad (_("unknown .loc sub-directive `%s'"), p
);
665 *input_line_pointer
= c
;
672 demand_empty_rest_of_line ();
673 loc_directive_seen
= TRUE
;
677 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED
)
679 offsetT value
= get_absolute_expression ();
681 if (value
!= 0 && value
!= 1)
683 as_bad (_("expected 0 or 1"));
684 ignore_rest_of_line ();
688 dwarf2_loc_mark_labels
= value
!= 0;
689 demand_empty_rest_of_line ();
694 first_frag_for_seg (segT seg
)
696 return seg_info (seg
)->frchainP
->frch_root
;
700 last_frag_for_seg (segT seg
)
702 frchainS
*f
= seg_info (seg
)->frchainP
;
704 while (f
->frch_next
!= NULL
)
710 /* Emit a single byte into the current segment. */
715 FRAG_APPEND_1_CHAR (byte
);
718 /* Emit a statement program opcode into the current segment. */
726 /* Emit a two-byte word into the current segment. */
731 md_number_to_chars (frag_more (2), data
, 2);
734 /* Emit a four byte word into the current segment. */
739 md_number_to_chars (frag_more (4), data
, 4);
742 /* Emit an unsigned "little-endian base 128" number. */
745 out_uleb128 (addressT value
)
747 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
750 /* Emit a tuple for .debug_abbrev. */
753 out_abbrev (int name
, int form
)
759 /* Get the size of a fragment. */
762 get_frag_fix (fragS
*frag
, segT seg
)
769 /* If a fragment is the last in the chain, special measures must be
770 taken to find its size before relaxation, since it may be pending
771 on some subsegment chain. */
772 for (fr
= seg_info (seg
)->frchainP
; fr
; fr
= fr
->frch_next
)
773 if (fr
->frch_last
== frag
)
774 return (char *) obstack_next_free (&fr
->frch_obstack
) - frag
->fr_literal
;
779 /* Set an absolute address (may result in a relocation entry). */
782 out_set_addr (symbolS
*sym
)
786 out_opcode (DW_LNS_extended_op
);
787 out_uleb128 (sizeof_address
+ 1);
789 out_opcode (DW_LNE_set_address
);
790 expr
.X_op
= O_symbol
;
791 expr
.X_add_symbol
= sym
;
792 expr
.X_add_number
= 0;
793 emit_expr (&expr
, sizeof_address
);
796 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
797 static void scale_addr_delta (addressT
*);
800 scale_addr_delta (addressT
*addr_delta
)
802 static int printed_this
= 0;
803 if (*addr_delta
% DWARF2_LINE_MIN_INSN_LENGTH
!= 0)
806 as_bad("unaligned opcodes detected in executable segment");
809 *addr_delta
/= DWARF2_LINE_MIN_INSN_LENGTH
;
812 #define scale_addr_delta(A)
815 /* Encode a pair of line and address skips as efficiently as possible.
816 Note that the line skip is signed, whereas the address skip is unsigned.
818 The following two routines *must* be kept in sync. This is
819 enforced by making emit_inc_line_addr abort if we do not emit
820 exactly the expected number of bytes. */
823 size_inc_line_addr (int line_delta
, addressT addr_delta
)
825 unsigned int tmp
, opcode
;
828 /* Scale the address delta by the minimum instruction length. */
829 scale_addr_delta (&addr_delta
);
831 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
832 We cannot use special opcodes here, since we want the end_sequence
833 to emit the matrix entry. */
834 if (line_delta
== INT_MAX
)
836 if (addr_delta
== MAX_SPECIAL_ADDR_DELTA
)
839 len
= 1 + sizeof_leb128 (addr_delta
, 0);
843 /* Bias the line delta by the base. */
844 tmp
= line_delta
- DWARF2_LINE_BASE
;
846 /* If the line increment is out of range of a special opcode, we
847 must encode it with DW_LNS_advance_line. */
848 if (tmp
>= DWARF2_LINE_RANGE
)
850 len
= 1 + sizeof_leb128 (line_delta
, 1);
852 tmp
= 0 - DWARF2_LINE_BASE
;
855 /* Bias the opcode by the special opcode base. */
856 tmp
+= DWARF2_LINE_OPCODE_BASE
;
858 /* Avoid overflow when addr_delta is large. */
859 if (addr_delta
< 256 + MAX_SPECIAL_ADDR_DELTA
)
861 /* Try using a special opcode. */
862 opcode
= tmp
+ addr_delta
* DWARF2_LINE_RANGE
;
866 /* Try using DW_LNS_const_add_pc followed by special op. */
867 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
872 /* Otherwise use DW_LNS_advance_pc. */
873 len
+= 1 + sizeof_leb128 (addr_delta
, 0);
875 /* DW_LNS_copy or special opcode. */
882 emit_inc_line_addr (int line_delta
, addressT addr_delta
, char *p
, int len
)
884 unsigned int tmp
, opcode
;
888 /* Line number sequences cannot go backward in addresses. This means
889 we've incorrectly ordered the statements in the sequence. */
890 assert ((offsetT
) addr_delta
>= 0);
892 /* Scale the address delta by the minimum instruction length. */
893 scale_addr_delta (&addr_delta
);
895 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
896 We cannot use special opcodes here, since we want the end_sequence
897 to emit the matrix entry. */
898 if (line_delta
== INT_MAX
)
900 if (addr_delta
== MAX_SPECIAL_ADDR_DELTA
)
901 *p
++ = DW_LNS_const_add_pc
;
904 *p
++ = DW_LNS_advance_pc
;
905 p
+= output_leb128 (p
, addr_delta
, 0);
908 *p
++ = DW_LNS_extended_op
;
910 *p
++ = DW_LNE_end_sequence
;
914 /* Bias the line delta by the base. */
915 tmp
= line_delta
- DWARF2_LINE_BASE
;
917 /* If the line increment is out of range of a special opcode, we
918 must encode it with DW_LNS_advance_line. */
919 if (tmp
>= DWARF2_LINE_RANGE
)
921 *p
++ = DW_LNS_advance_line
;
922 p
+= output_leb128 (p
, line_delta
, 1);
925 tmp
= 0 - DWARF2_LINE_BASE
;
929 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
931 if (line_delta
== 0 && addr_delta
== 0)
937 /* Bias the opcode by the special opcode base. */
938 tmp
+= DWARF2_LINE_OPCODE_BASE
;
940 /* Avoid overflow when addr_delta is large. */
941 if (addr_delta
< 256 + MAX_SPECIAL_ADDR_DELTA
)
943 /* Try using a special opcode. */
944 opcode
= tmp
+ addr_delta
* DWARF2_LINE_RANGE
;
951 /* Try using DW_LNS_const_add_pc followed by special op. */
952 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
955 *p
++ = DW_LNS_const_add_pc
;
961 /* Otherwise use DW_LNS_advance_pc. */
962 *p
++ = DW_LNS_advance_pc
;
963 p
+= output_leb128 (p
, addr_delta
, 0);
974 /* Handy routine to combine calls to the above two routines. */
977 out_inc_line_addr (int line_delta
, addressT addr_delta
)
979 int len
= size_inc_line_addr (line_delta
, addr_delta
);
980 emit_inc_line_addr (line_delta
, addr_delta
, frag_more (len
), len
);
983 /* Generate a variant frag that we can use to relax address/line
984 increments between fragments of the target segment. */
987 relax_inc_line_addr (int line_delta
, symbolS
*to_sym
, symbolS
*from_sym
)
992 expr
.X_op
= O_subtract
;
993 expr
.X_add_symbol
= to_sym
;
994 expr
.X_op_symbol
= from_sym
;
995 expr
.X_add_number
= 0;
997 /* The maximum size of the frag is the line delta with a maximum
998 sized address delta. */
999 max_chars
= size_inc_line_addr (line_delta
, -DWARF2_LINE_MIN_INSN_LENGTH
);
1001 frag_var (rs_dwarf2dbg
, max_chars
, max_chars
, 1,
1002 make_expr_symbol (&expr
), line_delta
, NULL
);
1005 /* The function estimates the size of a rs_dwarf2dbg variant frag
1006 based on the current values of the symbols. It is called before
1007 the relaxation loop. We set fr_subtype to the expected length. */
1010 dwarf2dbg_estimate_size_before_relax (fragS
*frag
)
1015 addr_delta
= resolve_symbol_value (frag
->fr_symbol
);
1016 size
= size_inc_line_addr (frag
->fr_offset
, addr_delta
);
1018 frag
->fr_subtype
= size
;
1023 /* This function relaxes a rs_dwarf2dbg variant frag based on the
1024 current values of the symbols. fr_subtype is the current length
1025 of the frag. This returns the change in frag length. */
1028 dwarf2dbg_relax_frag (fragS
*frag
)
1030 int old_size
, new_size
;
1032 old_size
= frag
->fr_subtype
;
1033 new_size
= dwarf2dbg_estimate_size_before_relax (frag
);
1035 return new_size
- old_size
;
1038 /* This function converts a rs_dwarf2dbg variant frag into a normal
1039 fill frag. This is called after all relaxation has been done.
1040 fr_subtype will be the desired length of the frag. */
1043 dwarf2dbg_convert_frag (fragS
*frag
)
1047 addr_diff
= resolve_symbol_value (frag
->fr_symbol
);
1049 /* fr_var carries the max_chars that we created the fragment with.
1050 fr_subtype carries the current expected length. We must, of
1051 course, have allocated enough memory earlier. */
1052 assert (frag
->fr_var
>= (int) frag
->fr_subtype
);
1054 emit_inc_line_addr (frag
->fr_offset
, addr_diff
,
1055 frag
->fr_literal
+ frag
->fr_fix
, frag
->fr_subtype
);
1057 frag
->fr_fix
+= frag
->fr_subtype
;
1058 frag
->fr_type
= rs_fill
;
1060 frag
->fr_offset
= 0;
1063 /* Generate .debug_line content for the chain of line number entries
1064 beginning at E, for segment SEG. */
1067 process_entries (segT seg
, struct line_entry
*e
)
1069 unsigned filenum
= 1;
1071 unsigned column
= 0;
1073 unsigned flags
= DWARF2_LINE_DEFAULT_IS_STMT
? DWARF2_FLAG_IS_STMT
: 0;
1074 fragS
*last_frag
= NULL
, *frag
;
1075 addressT last_frag_ofs
= 0, frag_ofs
;
1076 symbolS
*last_lab
= NULL
, *lab
;
1077 struct line_entry
*next
;
1083 if (filenum
!= e
->loc
.filenum
)
1085 filenum
= e
->loc
.filenum
;
1086 out_opcode (DW_LNS_set_file
);
1087 out_uleb128 (filenum
);
1090 if (column
!= e
->loc
.column
)
1092 column
= e
->loc
.column
;
1093 out_opcode (DW_LNS_set_column
);
1094 out_uleb128 (column
);
1097 if (isa
!= e
->loc
.isa
)
1100 out_opcode (DW_LNS_set_isa
);
1104 if ((e
->loc
.flags
^ flags
) & DWARF2_FLAG_IS_STMT
)
1106 flags
= e
->loc
.flags
;
1107 out_opcode (DW_LNS_negate_stmt
);
1110 if (e
->loc
.flags
& DWARF2_FLAG_BASIC_BLOCK
)
1111 out_opcode (DW_LNS_set_basic_block
);
1113 if (e
->loc
.flags
& DWARF2_FLAG_PROLOGUE_END
)
1114 out_opcode (DW_LNS_set_prologue_end
);
1116 if (e
->loc
.flags
& DWARF2_FLAG_EPILOGUE_BEGIN
)
1117 out_opcode (DW_LNS_set_epilogue_begin
);
1119 /* Don't try to optimize away redundant entries; gdb wants two
1120 entries for a function where the code starts on the same line as
1121 the {, and there's no way to identify that case here. Trust gcc
1122 to optimize appropriately. */
1123 line_delta
= e
->loc
.line
- line
;
1125 frag
= symbol_get_frag (lab
);
1126 frag_ofs
= S_GET_VALUE (lab
);
1128 if (last_frag
== NULL
)
1131 out_inc_line_addr (line_delta
, 0);
1133 else if (frag
== last_frag
)
1134 out_inc_line_addr (line_delta
, frag_ofs
- last_frag_ofs
);
1136 relax_inc_line_addr (line_delta
, lab
, last_lab
);
1141 last_frag_ofs
= frag_ofs
;
1149 /* Emit a DW_LNE_end_sequence for the end of the section. */
1150 frag
= last_frag_for_seg (seg
);
1151 frag_ofs
= get_frag_fix (frag
, seg
);
1152 if (frag
== last_frag
)
1153 out_inc_line_addr (INT_MAX
, frag_ofs
- last_frag_ofs
);
1156 lab
= symbol_temp_new (seg
, frag_ofs
, frag
);
1157 relax_inc_line_addr (INT_MAX
, lab
, last_lab
);
1161 /* Emit the directory and file tables for .debug_line. */
1164 out_file_list (void)
1170 /* Emit directory list. */
1171 for (i
= 1; i
< dirs_in_use
; ++i
)
1173 size
= strlen (dirs
[i
]) + 1;
1174 cp
= frag_more (size
);
1175 memcpy (cp
, dirs
[i
], size
);
1180 for (i
= 1; i
< files_in_use
; ++i
)
1182 if (files
[i
].filename
== NULL
)
1184 as_bad (_("unassigned file number %ld"), (long) i
);
1185 /* Prevent a crash later, particularly for file 1. */
1186 files
[i
].filename
= "";
1190 size
= strlen (files
[i
].filename
) + 1;
1191 cp
= frag_more (size
);
1192 memcpy (cp
, files
[i
].filename
, size
);
1194 out_uleb128 (files
[i
].dir
); /* directory number */
1195 out_uleb128 (0); /* last modification timestamp */
1196 out_uleb128 (0); /* filesize */
1199 /* Terminate filename list. */
1203 /* Emit the collected .debug_line data. */
1206 out_debug_line (segT line_seg
)
1209 symbolS
*line_start
;
1210 symbolS
*prologue_end
;
1213 enum dwarf2_format d2f
;
1216 subseg_set (line_seg
, 0);
1218 line_start
= symbol_temp_new_now ();
1219 prologue_end
= symbol_temp_make ();
1220 line_end
= symbol_temp_make ();
1222 /* Total length of the information for this compilation unit. */
1223 expr
.X_op
= O_subtract
;
1224 expr
.X_add_symbol
= line_end
;
1225 expr
.X_op_symbol
= line_start
;
1227 d2f
= DWARF2_FORMAT ();
1228 if (d2f
== dwarf2_format_32bit
)
1230 expr
.X_add_number
= -4;
1231 emit_expr (&expr
, 4);
1234 else if (d2f
== dwarf2_format_64bit
)
1236 expr
.X_add_number
= -12;
1238 emit_expr (&expr
, 8);
1241 else if (d2f
== dwarf2_format_64bit_irix
)
1243 expr
.X_add_number
= -8;
1244 emit_expr (&expr
, 8);
1249 as_fatal (_("internal error: unknown dwarf2 format"));
1255 /* Length of the prologue following this length. */
1256 expr
.X_op
= O_subtract
;
1257 expr
.X_add_symbol
= prologue_end
;
1258 expr
.X_op_symbol
= line_start
;
1259 expr
.X_add_number
= - (4 + 2 + 4);
1260 emit_expr (&expr
, sizeof_offset
);
1262 /* Parameters of the state machine. */
1263 out_byte (DWARF2_LINE_MIN_INSN_LENGTH
);
1264 out_byte (DWARF2_LINE_DEFAULT_IS_STMT
);
1265 out_byte (DWARF2_LINE_BASE
);
1266 out_byte (DWARF2_LINE_RANGE
);
1267 out_byte (DWARF2_LINE_OPCODE_BASE
);
1269 /* Standard opcode lengths. */
1270 out_byte (0); /* DW_LNS_copy */
1271 out_byte (1); /* DW_LNS_advance_pc */
1272 out_byte (1); /* DW_LNS_advance_line */
1273 out_byte (1); /* DW_LNS_set_file */
1274 out_byte (1); /* DW_LNS_set_column */
1275 out_byte (0); /* DW_LNS_negate_stmt */
1276 out_byte (0); /* DW_LNS_set_basic_block */
1277 out_byte (0); /* DW_LNS_const_add_pc */
1278 out_byte (1); /* DW_LNS_fixed_advance_pc */
1279 out_byte (0); /* DW_LNS_set_prologue_end */
1280 out_byte (0); /* DW_LNS_set_epilogue_begin */
1281 out_byte (1); /* DW_LNS_set_isa */
1285 symbol_set_value_now (prologue_end
);
1287 /* For each section, emit a statement program. */
1288 for (s
= all_segs
; s
; s
= s
->next
)
1289 process_entries (s
->seg
, s
->head
->head
);
1291 symbol_set_value_now (line_end
);
1294 /* Emit data for .debug_aranges. */
1297 out_debug_aranges (segT aranges_seg
, segT info_seg
)
1299 unsigned int addr_size
= sizeof_address
;
1300 addressT size
, skip
;
1305 size
= 4 + 2 + 4 + 1 + 1;
1307 skip
= 2 * addr_size
- (size
& (2 * addr_size
- 1));
1308 if (skip
== 2 * addr_size
)
1312 for (s
= all_segs
; s
; s
= s
->next
)
1313 size
+= 2 * addr_size
;
1315 size
+= 2 * addr_size
;
1317 subseg_set (aranges_seg
, 0);
1319 /* Length of the compilation unit. */
1320 out_four (size
- 4);
1325 /* Offset to .debug_info. */
1326 /* ??? sizeof_offset */
1327 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg
), 4);
1329 /* Size of an address (offset portion). */
1330 out_byte (addr_size
);
1332 /* Size of a segment descriptor. */
1335 /* Align the header. */
1337 frag_align (ffs (2 * addr_size
) - 1, 0, 0);
1339 for (s
= all_segs
; s
; s
= s
->next
)
1344 frag
= first_frag_for_seg (s
->seg
);
1345 beg
= symbol_temp_new (s
->seg
, 0, frag
);
1346 s
->text_start
= beg
;
1348 frag
= last_frag_for_seg (s
->seg
);
1349 end
= symbol_temp_new (s
->seg
, get_frag_fix (frag
, s
->seg
), frag
);
1352 expr
.X_op
= O_symbol
;
1353 expr
.X_add_symbol
= beg
;
1354 expr
.X_add_number
= 0;
1355 emit_expr (&expr
, addr_size
);
1357 expr
.X_op
= O_subtract
;
1358 expr
.X_add_symbol
= end
;
1359 expr
.X_op_symbol
= beg
;
1360 expr
.X_add_number
= 0;
1361 emit_expr (&expr
, addr_size
);
1364 p
= frag_more (2 * addr_size
);
1365 md_number_to_chars (p
, 0, addr_size
);
1366 md_number_to_chars (p
+ addr_size
, 0, addr_size
);
1369 /* Emit data for .debug_abbrev. Note that this must be kept in
1370 sync with out_debug_info below. */
1373 out_debug_abbrev (segT abbrev_seg
)
1375 subseg_set (abbrev_seg
, 0);
1378 out_uleb128 (DW_TAG_compile_unit
);
1379 out_byte (DW_CHILDREN_no
);
1380 out_abbrev (DW_AT_stmt_list
, DW_FORM_data4
);
1381 if (all_segs
->next
== NULL
)
1383 out_abbrev (DW_AT_low_pc
, DW_FORM_addr
);
1384 out_abbrev (DW_AT_high_pc
, DW_FORM_addr
);
1386 out_abbrev (DW_AT_name
, DW_FORM_string
);
1387 out_abbrev (DW_AT_comp_dir
, DW_FORM_string
);
1388 out_abbrev (DW_AT_producer
, DW_FORM_string
);
1389 out_abbrev (DW_AT_language
, DW_FORM_data2
);
1392 /* Terminate the abbreviations for this compilation unit. */
1396 /* Emit a description of this compilation unit for .debug_info. */
1399 out_debug_info (segT info_seg
, segT abbrev_seg
, segT line_seg
)
1404 symbolS
*info_start
;
1408 enum dwarf2_format d2f
;
1411 subseg_set (info_seg
, 0);
1413 info_start
= symbol_temp_new_now ();
1414 info_end
= symbol_temp_make ();
1416 /* Compilation Unit length. */
1417 expr
.X_op
= O_subtract
;
1418 expr
.X_add_symbol
= info_end
;
1419 expr
.X_op_symbol
= info_start
;
1421 d2f
= DWARF2_FORMAT ();
1422 if (d2f
== dwarf2_format_32bit
)
1424 expr
.X_add_number
= -4;
1425 emit_expr (&expr
, 4);
1428 else if (d2f
== dwarf2_format_64bit
)
1430 expr
.X_add_number
= -12;
1432 emit_expr (&expr
, 8);
1435 else if (d2f
== dwarf2_format_64bit_irix
)
1437 expr
.X_add_number
= -8;
1438 emit_expr (&expr
, 8);
1443 as_fatal (_("internal error: unknown dwarf2 format"));
1446 /* DWARF version. */
1449 /* .debug_abbrev offset */
1450 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg
), sizeof_offset
);
1452 /* Target address size. */
1453 out_byte (sizeof_address
);
1455 /* DW_TAG_compile_unit DIE abbrev */
1458 /* DW_AT_stmt_list */
1459 /* ??? sizeof_offset */
1460 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg
), 4);
1462 /* These two attributes may only be emitted if all of the code is
1463 contiguous. Multiple sections are not that. */
1464 if (all_segs
->next
== NULL
)
1467 expr
.X_op
= O_symbol
;
1468 expr
.X_add_symbol
= all_segs
->text_start
;
1469 expr
.X_add_number
= 0;
1470 emit_expr (&expr
, sizeof_address
);
1473 expr
.X_op
= O_symbol
;
1474 expr
.X_add_symbol
= all_segs
->text_end
;
1475 expr
.X_add_number
= 0;
1476 emit_expr (&expr
, sizeof_address
);
1479 /* DW_AT_name. We don't have the actual file name that was present
1480 on the command line, so assume files[1] is the main input file.
1481 We're not supposed to get called unless at least one line number
1482 entry was emitted, so this should always be defined. */
1483 if (!files
|| files_in_use
< 1)
1487 len
= strlen (dirs
[files
[1].dir
]);
1488 p
= frag_more (len
+ 1);
1489 memcpy (p
, dirs
[files
[1].dir
], len
);
1490 INSERT_DIR_SEPARATOR (p
, len
);
1492 len
= strlen (files
[1].filename
) + 1;
1493 p
= frag_more (len
);
1494 memcpy (p
, files
[1].filename
, len
);
1496 /* DW_AT_comp_dir */
1497 comp_dir
= getpwd ();
1498 len
= strlen (comp_dir
) + 1;
1499 p
= frag_more (len
);
1500 memcpy (p
, comp_dir
, len
);
1502 /* DW_AT_producer */
1503 sprintf (producer
, "GNU AS %s", VERSION
);
1504 len
= strlen (producer
) + 1;
1505 p
= frag_more (len
);
1506 memcpy (p
, producer
, len
);
1508 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1509 dwarf2 draft has no standard code for assembler. */
1510 out_two (DW_LANG_Mips_Assembler
);
1512 symbol_set_value_now (info_end
);
1515 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1516 were any .file/.loc directives, or --gdwarf2 was given, or if the
1517 file has a non-empty .debug_info section. If we emit .debug_line,
1518 and the .debug_info section is empty, we also emit .debug_info,
1519 .debug_aranges and .debug_abbrev. ALL_SEGS will be non-null if
1520 there were any .file/.loc directives, or --gdwarf2 was given and
1521 there were any located instructions emitted. */
1524 dwarf2_finish (void)
1529 int emit_other_sections
= 0;
1531 info_seg
= bfd_get_section_by_name (stdoutput
, ".debug_info");
1532 emit_other_sections
= info_seg
== NULL
|| !seg_not_empty_p (info_seg
);
1534 if (!all_segs
&& emit_other_sections
)
1535 /* There is no line information and no non-empty .debug_info
1539 /* Calculate the size of an address for the target machine. */
1540 sizeof_address
= DWARF2_ADDR_SIZE (stdoutput
);
1542 /* Create and switch to the line number section. */
1543 line_seg
= subseg_new (".debug_line", 0);
1544 bfd_set_section_flags (stdoutput
, line_seg
, SEC_READONLY
| SEC_DEBUGGING
);
1546 /* For each subsection, chain the debug entries together. */
1547 for (s
= all_segs
; s
; s
= s
->next
)
1549 struct line_subseg
*ss
= s
->head
;
1550 struct line_entry
**ptail
= ss
->ptail
;
1552 while ((ss
= ss
->next
) != NULL
)
1559 out_debug_line (line_seg
);
1561 /* If this is assembler generated line info, and there is no
1562 debug_info already, we need .debug_info and .debug_abbrev
1563 sections as well. */
1564 if (emit_other_sections
)
1571 info_seg
= subseg_new (".debug_info", 0);
1572 abbrev_seg
= subseg_new (".debug_abbrev", 0);
1573 aranges_seg
= subseg_new (".debug_aranges", 0);
1575 bfd_set_section_flags (stdoutput
, info_seg
,
1576 SEC_READONLY
| SEC_DEBUGGING
);
1577 bfd_set_section_flags (stdoutput
, abbrev_seg
,
1578 SEC_READONLY
| SEC_DEBUGGING
);
1579 bfd_set_section_flags (stdoutput
, aranges_seg
,
1580 SEC_READONLY
| SEC_DEBUGGING
);
1582 record_alignment (aranges_seg
, ffs (2 * sizeof_address
) - 1);
1584 out_debug_aranges (aranges_seg
, info_seg
);
1585 out_debug_abbrev (abbrev_seg
);
1586 out_debug_info (info_seg
, abbrev_seg
, line_seg
);