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>
50 # define DWARF2_FORMAT() dwarf2_format_32bit
53 #ifndef DWARF2_ADDR_SIZE
54 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
59 #include "elf/dwarf2.h"
61 /* Since we can't generate the prolog until the body is complete, we
62 use three different subsegments for .debug_line: one holding the
63 prolog, one for the directory and filename info, and one for the
64 body ("statement program"). */
69 /* First special line opcde - leave room for the standard opcodes.
70 Note: If you want to change this, you'll have to update the
71 "standard_opcode_lengths" table that is emitted below in
73 #define DWARF2_LINE_OPCODE_BASE 13
75 #ifndef DWARF2_LINE_BASE
76 /* Minimum line offset in a special line info. opcode. This value
77 was chosen to give a reasonable range of values. */
78 # define DWARF2_LINE_BASE -5
81 /* Range of line offsets in a special line info. opcode. */
82 #ifndef DWARF2_LINE_RANGE
83 # define DWARF2_LINE_RANGE 14
86 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
87 /* Define the architecture-dependent minimum instruction length (in
88 bytes). This value should be rather too small than too big. */
89 # define DWARF2_LINE_MIN_INSN_LENGTH 1
92 /* Flag that indicates the initial value of the is_stmt_start flag. */
93 #define DWARF2_LINE_DEFAULT_IS_STMT 1
95 /* Given a special op, return the line skip amount. */
96 #define SPECIAL_LINE(op) \
97 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
99 /* Given a special op, return the address skip amount (in units of
100 DWARF2_LINE_MIN_INSN_LENGTH. */
101 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
103 /* The maximum address skip amount that can be encoded with a special op. */
104 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
107 struct line_entry
*next
;
109 struct dwarf2_line_info loc
;
113 struct line_subseg
*next
;
115 struct line_entry
*head
;
116 struct line_entry
**ptail
;
120 struct line_seg
*next
;
122 struct line_subseg
*head
;
127 /* Collects data for all line table entries during assembly. */
128 static struct line_seg
*all_segs
;
131 const char *filename
;
135 /* Table of files used by .debug_line. */
136 static struct file_entry
*files
;
137 static unsigned int files_in_use
;
138 static unsigned int files_allocated
;
140 /* Table of directories used by .debug_line. */
142 static unsigned int dirs_in_use
;
143 static unsigned int dirs_allocated
;
145 /* TRUE when we've seen a .loc directive recently. Used to avoid
146 doing work when there's nothing to do. */
147 static bfd_boolean loc_directive_seen
;
149 /* TRUE when we're supposed to set the basic block mark whenever a
151 bfd_boolean dwarf2_loc_mark_labels
;
153 /* Current location as indicated by the most recent .loc directive. */
154 static struct dwarf2_line_info current
= {
156 DWARF2_LINE_DEFAULT_IS_STMT
? DWARF2_FLAG_IS_STMT
: 0
159 /* The size of an address on the target. */
160 static unsigned int sizeof_address
;
162 static struct line_subseg
*get_line_subseg (segT
, subsegT
);
163 static unsigned int get_filenum (const char *, unsigned int);
164 static struct frag
*first_frag_for_seg (segT
);
165 static struct frag
*last_frag_for_seg (segT
);
166 static void out_byte (int);
167 static void out_opcode (int);
168 static void out_two (int);
169 static void out_four (int);
170 static void out_abbrev (int, int);
171 static void out_uleb128 (addressT
);
172 static offsetT
get_frag_fix (fragS
*, segT
);
173 static void out_set_addr (symbolS
*);
174 static int size_inc_line_addr (int, addressT
);
175 static void emit_inc_line_addr (int, addressT
, char *, int);
176 static void out_inc_line_addr (int, addressT
);
177 static void relax_inc_line_addr (int, symbolS
*, symbolS
*);
178 static void process_entries (segT
, struct line_entry
*);
179 static void out_file_list (void);
180 static void out_debug_line (segT
);
181 static void out_debug_aranges (segT
, segT
);
182 static void out_debug_abbrev (segT
);
183 static void out_debug_info (segT
, segT
, segT
);
185 #ifndef TC_DWARF2_EMIT_OFFSET
186 # define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
187 static void generic_dwarf2_emit_offset (symbolS
*, unsigned int);
189 /* Create an offset to .dwarf2_*. */
192 generic_dwarf2_emit_offset (symbolS
*symbol
, unsigned int size
)
196 expr
.X_op
= O_symbol
;
197 expr
.X_add_symbol
= symbol
;
198 expr
.X_add_number
= 0;
199 emit_expr (&expr
, size
);
203 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
205 static struct line_subseg
*
206 get_line_subseg (segT seg
, subsegT subseg
)
208 static segT last_seg
;
209 static subsegT last_subseg
;
210 static struct line_subseg
*last_line_subseg
;
212 struct line_seg
**ps
, *s
;
213 struct line_subseg
**pss
, *ss
;
215 if (seg
== last_seg
&& subseg
== last_subseg
)
216 return last_line_subseg
;
218 for (ps
= &all_segs
; (s
= *ps
) != NULL
; ps
= &s
->next
)
222 s
= (struct line_seg
*) xmalloc (sizeof (*s
));
229 for (pss
= &s
->head
; (ss
= *pss
) != NULL
; pss
= &ss
->next
)
231 if (ss
->subseg
== subseg
)
233 if (ss
->subseg
> subseg
)
237 ss
= (struct line_subseg
*) xmalloc (sizeof (*ss
));
241 ss
->ptail
= &ss
->head
;
246 last_subseg
= subseg
;
247 last_line_subseg
= ss
;
252 /* Record an entry for LOC occurring at LABEL. */
255 dwarf2_gen_line_info_1 (symbolS
*label
, struct dwarf2_line_info
*loc
)
257 struct line_subseg
*ss
;
258 struct line_entry
*e
;
260 e
= (struct line_entry
*) xmalloc (sizeof (*e
));
265 ss
= get_line_subseg (now_seg
, now_subseg
);
267 ss
->ptail
= &e
->next
;
270 /* Record an entry for LOC occurring at OFS within the current fragment. */
273 dwarf2_gen_line_info (addressT ofs
, struct dwarf2_line_info
*loc
)
275 static unsigned int line
= -1;
276 static unsigned int filenum
= -1;
280 /* Early out for as-yet incomplete location information. */
281 if (loc
->filenum
== 0 || loc
->line
== 0)
284 /* Don't emit sequences of line symbols for the same line when the
285 symbols apply to assembler code. It is necessary to emit
286 duplicate line symbols when a compiler asks for them, because GDB
287 uses them to determine the end of the prologue. */
288 if (debug_type
== DEBUG_DWARF2
289 && line
== loc
->line
&& filenum
== loc
->filenum
)
293 filenum
= loc
->filenum
;
295 sym
= symbol_temp_new (now_seg
, ofs
, frag_now
);
296 dwarf2_gen_line_info_1 (sym
, loc
);
299 /* Returns the current source information. If .file directives have
300 been encountered, the info for the corresponding source file is
301 returned. Otherwise, the info for the assembly source file is
305 dwarf2_where (struct dwarf2_line_info
*line
)
307 if (debug_type
== DEBUG_DWARF2
)
310 as_where (&filename
, &line
->line
);
311 line
->filenum
= get_filenum (filename
, 0);
313 line
->flags
= DWARF2_FLAG_IS_STMT
;
314 line
->isa
= current
.isa
;
320 /* A hook to allow the target backend to inform the line number state
321 machine of isa changes when assembler debug info is enabled. */
324 dwarf2_set_isa (unsigned int isa
)
329 /* Called for each machine instruction, or relatively atomic group of
330 machine instructions (ie built-in macro). The instruction or group
331 is SIZE bytes in length. If dwarf2 line number generation is called
332 for, emit a line statement appropriately. */
335 dwarf2_emit_insn (int size
)
337 struct dwarf2_line_info loc
;
339 if (loc_directive_seen
)
341 /* Use the last location established by a .loc directive, not
342 the value returned by dwarf2_where(). That calls as_where()
343 which will return either the logical input file name (foo.c)
344 or the physical input file name (foo.s) and not the file name
345 specified in the most recent .loc directive (eg foo.h). */
348 /* Unless we generate DWARF2 debugging information for each
349 assembler line, we only emit one line symbol for one LOC. */
350 if (debug_type
!= DEBUG_DWARF2
)
351 loc_directive_seen
= FALSE
;
353 else if (debug_type
!= DEBUG_DWARF2
)
358 dwarf2_gen_line_info (frag_now_fix () - size
, &loc
);
360 current
.flags
&= ~(DWARF2_FLAG_BASIC_BLOCK
361 | DWARF2_FLAG_PROLOGUE_END
362 | DWARF2_FLAG_EPILOGUE_BEGIN
);
365 /* Called for each (preferably code) label. If dwarf2_loc_mark_labels
366 is enabled, emit a basic block marker. */
369 dwarf2_emit_label (symbolS
*label
)
371 struct dwarf2_line_info loc
;
373 if (!dwarf2_loc_mark_labels
)
375 if (S_GET_SEGMENT (label
) != now_seg
)
377 if (!(bfd_get_section_flags (stdoutput
, now_seg
) & SEC_CODE
))
380 if (debug_type
== DEBUG_DWARF2
)
385 loc_directive_seen
= FALSE
;
388 loc
.flags
|= DWARF2_FLAG_BASIC_BLOCK
;
390 current
.flags
&= ~(DWARF2_FLAG_BASIC_BLOCK
391 | DWARF2_FLAG_PROLOGUE_END
392 | DWARF2_FLAG_EPILOGUE_BEGIN
);
394 dwarf2_gen_line_info_1 (label
, &loc
);
397 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
398 allocate it on that file table slot, otherwise return the first
402 get_filenum (const char *filename
, unsigned int num
)
404 static unsigned int last_used
, last_used_dir_len
;
409 if (num
== 0 && last_used
)
411 if (! files
[last_used
].dir
412 && strcmp (filename
, files
[last_used
].filename
) == 0)
414 if (files
[last_used
].dir
415 && strncmp (filename
, dirs
[files
[last_used
].dir
],
416 last_used_dir_len
) == 0
417 && IS_DIR_SEPARATOR (filename
[last_used_dir_len
])
418 && strcmp (filename
+ last_used_dir_len
+ 1,
419 files
[last_used
].filename
) == 0)
423 file
= lbasename (filename
);
424 /* Don't make empty string from / or A: from A:/ . */
425 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
426 if (file
<= filename
+ 3)
429 if (file
== filename
+ 1)
432 dir_len
= file
- filename
;
438 for (dir
= 1; dir
< dirs_in_use
; ++dir
)
439 if (strncmp (filename
, dirs
[dir
], dir_len
) == 0
440 && dirs
[dir
][dir_len
] == '\0')
443 if (dir
>= dirs_in_use
)
445 if (dir
>= dirs_allocated
)
447 dirs_allocated
= dir
+ 32;
449 xrealloc (dirs
, (dir
+ 32) * sizeof (const char *));
452 dirs
[dir
] = xmalloc (dir_len
+ 1);
453 memcpy (dirs
[dir
], filename
, dir_len
);
454 dirs
[dir
][dir_len
] = '\0';
455 dirs_in_use
= dir
+ 1;
461 for (i
= 1; i
< files_in_use
; ++i
)
462 if (files
[i
].dir
== dir
464 && strcmp (file
, files
[i
].filename
) == 0)
467 last_used_dir_len
= dir_len
;
474 if (i
>= files_allocated
)
476 unsigned int old
= files_allocated
;
478 files_allocated
= i
+ 32;
479 files
= (struct file_entry
*)
480 xrealloc (files
, (i
+ 32) * sizeof (struct file_entry
));
482 memset (files
+ old
, 0, (i
+ 32 - old
) * sizeof (struct file_entry
));
485 files
[i
].filename
= num
? file
: xstrdup (file
);
487 if (files_in_use
< i
+ 1)
488 files_in_use
= i
+ 1;
490 last_used_dir_len
= dir_len
;
495 /* Handle two forms of .file directive:
496 - Pass .file "source.c" to s_app_file
497 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
499 If an entry is added to the file table, return a pointer to the filename. */
502 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED
)
508 /* Continue to accept a bare string and pass it off. */
510 if (*input_line_pointer
== '"')
516 num
= get_absolute_expression ();
517 filename
= demand_copy_C_string (&filename_len
);
518 if (filename
== NULL
)
520 demand_empty_rest_of_line ();
524 as_bad (_("file number less than one"));
528 if (num
< (int) files_in_use
&& files
[num
].filename
!= 0)
530 as_bad (_("file number %ld already allocated"), (long) num
);
534 get_filenum (filename
, num
);
540 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED
)
542 offsetT filenum
, line
;
544 filenum
= get_absolute_expression ();
546 line
= get_absolute_expression ();
550 as_bad (_("file number less than one"));
553 if (filenum
>= (int) files_in_use
|| files
[filenum
].filename
== 0)
555 as_bad (_("unassigned file number %ld"), (long) filenum
);
559 current
.filenum
= filenum
;
565 if (files
[filenum
].dir
)
567 size_t dir_len
= strlen (dirs
[files
[filenum
].dir
]);
568 size_t file_len
= strlen (files
[filenum
].filename
);
569 char *cp
= (char *) alloca (dir_len
+ 1 + file_len
+ 1);
571 memcpy (cp
, dirs
[files
[filenum
].dir
], dir_len
);
573 memcpy (cp
+ dir_len
+ 1, files
[filenum
].filename
, file_len
);
574 cp
[dir_len
+ file_len
+ 1] = '\0';
575 listing_source_file (cp
);
578 listing_source_file (files
[filenum
].filename
);
579 listing_source_line (line
);
584 if (ISDIGIT (*input_line_pointer
))
586 current
.column
= get_absolute_expression ();
590 while (ISALPHA (*input_line_pointer
))
595 p
= input_line_pointer
;
596 c
= get_symbol_end ();
598 if (strcmp (p
, "basic_block") == 0)
600 current
.flags
|= DWARF2_FLAG_BASIC_BLOCK
;
601 *input_line_pointer
= c
;
603 else if (strcmp (p
, "prologue_end") == 0)
605 current
.flags
|= DWARF2_FLAG_PROLOGUE_END
;
606 *input_line_pointer
= c
;
608 else if (strcmp (p
, "epilogue_begin") == 0)
610 current
.flags
|= DWARF2_FLAG_EPILOGUE_BEGIN
;
611 *input_line_pointer
= c
;
613 else if (strcmp (p
, "is_stmt") == 0)
615 *input_line_pointer
= c
;
616 value
= get_absolute_expression ();
618 current
.flags
&= ~DWARF2_FLAG_IS_STMT
;
620 current
.flags
|= DWARF2_FLAG_IS_STMT
;
623 as_bad (_("is_stmt value not 0 or 1"));
627 else if (strcmp (p
, "isa") == 0)
629 *input_line_pointer
= c
;
630 value
= get_absolute_expression ();
635 as_bad (_("isa number less than zero"));
641 as_bad (_("unknown .loc sub-directive `%s'"), p
);
642 *input_line_pointer
= c
;
649 demand_empty_rest_of_line ();
650 loc_directive_seen
= TRUE
;
654 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED
)
656 offsetT value
= get_absolute_expression ();
658 if (value
!= 0 && value
!= 1)
660 as_bad (_("expected 0 or 1"));
661 ignore_rest_of_line ();
665 dwarf2_loc_mark_labels
= value
!= 0;
666 demand_empty_rest_of_line ();
671 first_frag_for_seg (segT seg
)
673 return seg_info (seg
)->frchainP
->frch_root
;
677 last_frag_for_seg (segT seg
)
679 frchainS
*f
= seg_info (seg
)->frchainP
;
681 while (f
->frch_next
!= NULL
)
687 /* Emit a single byte into the current segment. */
692 FRAG_APPEND_1_CHAR (byte
);
695 /* Emit a statement program opcode into the current segment. */
703 /* Emit a two-byte word into the current segment. */
708 md_number_to_chars (frag_more (2), data
, 2);
711 /* Emit a four byte word into the current segment. */
716 md_number_to_chars (frag_more (4), data
, 4);
719 /* Emit an unsigned "little-endian base 128" number. */
722 out_uleb128 (addressT value
)
724 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
727 /* Emit a tuple for .debug_abbrev. */
730 out_abbrev (int name
, int form
)
736 /* Get the size of a fragment. */
739 get_frag_fix (fragS
*frag
, segT seg
)
746 /* If a fragment is the last in the chain, special measures must be
747 taken to find its size before relaxation, since it may be pending
748 on some subsegment chain. */
749 for (fr
= seg_info (seg
)->frchainP
; fr
; fr
= fr
->frch_next
)
750 if (fr
->frch_last
== frag
)
751 return (char *) obstack_next_free (&fr
->frch_obstack
) - frag
->fr_literal
;
756 /* Set an absolute address (may result in a relocation entry). */
759 out_set_addr (symbolS
*sym
)
763 out_opcode (DW_LNS_extended_op
);
764 out_uleb128 (sizeof_address
+ 1);
766 out_opcode (DW_LNE_set_address
);
767 expr
.X_op
= O_symbol
;
768 expr
.X_add_symbol
= sym
;
769 expr
.X_add_number
= 0;
770 emit_expr (&expr
, sizeof_address
);
773 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
774 static void scale_addr_delta (addressT
*);
777 scale_addr_delta (addressT
*addr_delta
)
779 static int printed_this
= 0;
780 if (*addr_delta
% DWARF2_LINE_MIN_INSN_LENGTH
!= 0)
783 as_bad("unaligned opcodes detected in executable segment");
786 *addr_delta
/= DWARF2_LINE_MIN_INSN_LENGTH
;
789 #define scale_addr_delta(A)
792 /* Encode a pair of line and address skips as efficiently as possible.
793 Note that the line skip is signed, whereas the address skip is unsigned.
795 The following two routines *must* be kept in sync. This is
796 enforced by making emit_inc_line_addr abort if we do not emit
797 exactly the expected number of bytes. */
800 size_inc_line_addr (int line_delta
, addressT addr_delta
)
802 unsigned int tmp
, opcode
;
805 /* Scale the address delta by the minimum instruction length. */
806 scale_addr_delta (&addr_delta
);
808 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
809 We cannot use special opcodes here, since we want the end_sequence
810 to emit the matrix entry. */
811 if (line_delta
== INT_MAX
)
813 if (addr_delta
== MAX_SPECIAL_ADDR_DELTA
)
816 len
= 1 + sizeof_leb128 (addr_delta
, 0);
820 /* Bias the line delta by the base. */
821 tmp
= line_delta
- DWARF2_LINE_BASE
;
823 /* If the line increment is out of range of a special opcode, we
824 must encode it with DW_LNS_advance_line. */
825 if (tmp
>= DWARF2_LINE_RANGE
)
827 len
= 1 + sizeof_leb128 (line_delta
, 1);
829 tmp
= 0 - DWARF2_LINE_BASE
;
832 /* Bias the opcode by the special opcode base. */
833 tmp
+= DWARF2_LINE_OPCODE_BASE
;
835 /* Avoid overflow when addr_delta is large. */
836 if (addr_delta
< 256 + MAX_SPECIAL_ADDR_DELTA
)
838 /* Try using a special opcode. */
839 opcode
= tmp
+ addr_delta
* DWARF2_LINE_RANGE
;
843 /* Try using DW_LNS_const_add_pc followed by special op. */
844 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
849 /* Otherwise use DW_LNS_advance_pc. */
850 len
+= 1 + sizeof_leb128 (addr_delta
, 0);
852 /* DW_LNS_copy or special opcode. */
859 emit_inc_line_addr (int line_delta
, addressT addr_delta
, char *p
, int len
)
861 unsigned int tmp
, opcode
;
865 /* Line number sequences cannot go backward in addresses. This means
866 we've incorrectly ordered the statements in the sequence. */
867 assert ((offsetT
) addr_delta
>= 0);
869 /* Scale the address delta by the minimum instruction length. */
870 scale_addr_delta (&addr_delta
);
872 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
873 We cannot use special opcodes here, since we want the end_sequence
874 to emit the matrix entry. */
875 if (line_delta
== INT_MAX
)
877 if (addr_delta
== MAX_SPECIAL_ADDR_DELTA
)
878 *p
++ = DW_LNS_const_add_pc
;
881 *p
++ = DW_LNS_advance_pc
;
882 p
+= output_leb128 (p
, addr_delta
, 0);
885 *p
++ = DW_LNS_extended_op
;
887 *p
++ = DW_LNE_end_sequence
;
891 /* Bias the line delta by the base. */
892 tmp
= line_delta
- DWARF2_LINE_BASE
;
894 /* If the line increment is out of range of a special opcode, we
895 must encode it with DW_LNS_advance_line. */
896 if (tmp
>= DWARF2_LINE_RANGE
)
898 *p
++ = DW_LNS_advance_line
;
899 p
+= output_leb128 (p
, line_delta
, 1);
902 tmp
= 0 - DWARF2_LINE_BASE
;
906 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
908 if (line_delta
== 0 && addr_delta
== 0)
914 /* Bias the opcode by the special opcode base. */
915 tmp
+= DWARF2_LINE_OPCODE_BASE
;
917 /* Avoid overflow when addr_delta is large. */
918 if (addr_delta
< 256 + MAX_SPECIAL_ADDR_DELTA
)
920 /* Try using a special opcode. */
921 opcode
= tmp
+ addr_delta
* DWARF2_LINE_RANGE
;
928 /* Try using DW_LNS_const_add_pc followed by special op. */
929 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
932 *p
++ = DW_LNS_const_add_pc
;
938 /* Otherwise use DW_LNS_advance_pc. */
939 *p
++ = DW_LNS_advance_pc
;
940 p
+= output_leb128 (p
, addr_delta
, 0);
951 /* Handy routine to combine calls to the above two routines. */
954 out_inc_line_addr (int line_delta
, addressT addr_delta
)
956 int len
= size_inc_line_addr (line_delta
, addr_delta
);
957 emit_inc_line_addr (line_delta
, addr_delta
, frag_more (len
), len
);
960 /* Generate a variant frag that we can use to relax address/line
961 increments between fragments of the target segment. */
964 relax_inc_line_addr (int line_delta
, symbolS
*to_sym
, symbolS
*from_sym
)
969 expr
.X_op
= O_subtract
;
970 expr
.X_add_symbol
= to_sym
;
971 expr
.X_op_symbol
= from_sym
;
972 expr
.X_add_number
= 0;
974 /* The maximum size of the frag is the line delta with a maximum
975 sized address delta. */
976 max_chars
= size_inc_line_addr (line_delta
, -DWARF2_LINE_MIN_INSN_LENGTH
);
978 frag_var (rs_dwarf2dbg
, max_chars
, max_chars
, 1,
979 make_expr_symbol (&expr
), line_delta
, NULL
);
982 /* The function estimates the size of a rs_dwarf2dbg variant frag
983 based on the current values of the symbols. It is called before
984 the relaxation loop. We set fr_subtype to the expected length. */
987 dwarf2dbg_estimate_size_before_relax (fragS
*frag
)
992 addr_delta
= resolve_symbol_value (frag
->fr_symbol
);
993 size
= size_inc_line_addr (frag
->fr_offset
, addr_delta
);
995 frag
->fr_subtype
= size
;
1000 /* This function relaxes a rs_dwarf2dbg variant frag based on the
1001 current values of the symbols. fr_subtype is the current length
1002 of the frag. This returns the change in frag length. */
1005 dwarf2dbg_relax_frag (fragS
*frag
)
1007 int old_size
, new_size
;
1009 old_size
= frag
->fr_subtype
;
1010 new_size
= dwarf2dbg_estimate_size_before_relax (frag
);
1012 return new_size
- old_size
;
1015 /* This function converts a rs_dwarf2dbg variant frag into a normal
1016 fill frag. This is called after all relaxation has been done.
1017 fr_subtype will be the desired length of the frag. */
1020 dwarf2dbg_convert_frag (fragS
*frag
)
1024 addr_diff
= resolve_symbol_value (frag
->fr_symbol
);
1026 /* fr_var carries the max_chars that we created the fragment with.
1027 fr_subtype carries the current expected length. We must, of
1028 course, have allocated enough memory earlier. */
1029 assert (frag
->fr_var
>= (int) frag
->fr_subtype
);
1031 emit_inc_line_addr (frag
->fr_offset
, addr_diff
,
1032 frag
->fr_literal
+ frag
->fr_fix
, frag
->fr_subtype
);
1034 frag
->fr_fix
+= frag
->fr_subtype
;
1035 frag
->fr_type
= rs_fill
;
1037 frag
->fr_offset
= 0;
1040 /* Generate .debug_line content for the chain of line number entries
1041 beginning at E, for segment SEG. */
1044 process_entries (segT seg
, struct line_entry
*e
)
1046 unsigned filenum
= 1;
1048 unsigned column
= 0;
1050 unsigned flags
= DWARF2_LINE_DEFAULT_IS_STMT
? DWARF2_FLAG_IS_STMT
: 0;
1051 fragS
*last_frag
= NULL
, *frag
;
1052 addressT last_frag_ofs
= 0, frag_ofs
;
1053 symbolS
*last_lab
= NULL
, *lab
;
1054 struct line_entry
*next
;
1060 if (filenum
!= e
->loc
.filenum
)
1062 filenum
= e
->loc
.filenum
;
1063 out_opcode (DW_LNS_set_file
);
1064 out_uleb128 (filenum
);
1067 if (column
!= e
->loc
.column
)
1069 column
= e
->loc
.column
;
1070 out_opcode (DW_LNS_set_column
);
1071 out_uleb128 (column
);
1074 if (isa
!= e
->loc
.isa
)
1077 out_opcode (DW_LNS_set_isa
);
1081 if ((e
->loc
.flags
^ flags
) & DWARF2_FLAG_IS_STMT
)
1083 flags
= e
->loc
.flags
;
1084 out_opcode (DW_LNS_negate_stmt
);
1087 if (e
->loc
.flags
& DWARF2_FLAG_BASIC_BLOCK
)
1088 out_opcode (DW_LNS_set_basic_block
);
1090 if (e
->loc
.flags
& DWARF2_FLAG_PROLOGUE_END
)
1091 out_opcode (DW_LNS_set_prologue_end
);
1093 if (e
->loc
.flags
& DWARF2_FLAG_EPILOGUE_BEGIN
)
1094 out_opcode (DW_LNS_set_epilogue_begin
);
1096 /* Don't try to optimize away redundant entries; gdb wants two
1097 entries for a function where the code starts on the same line as
1098 the {, and there's no way to identify that case here. Trust gcc
1099 to optimize appropriately. */
1100 line_delta
= e
->loc
.line
- line
;
1102 frag
= symbol_get_frag (lab
);
1103 frag_ofs
= S_GET_VALUE (lab
);
1105 if (last_frag
== NULL
)
1108 out_inc_line_addr (line_delta
, 0);
1110 else if (frag
== last_frag
)
1111 out_inc_line_addr (line_delta
, frag_ofs
- last_frag_ofs
);
1113 relax_inc_line_addr (line_delta
, lab
, last_lab
);
1118 last_frag_ofs
= frag_ofs
;
1126 /* Emit a DW_LNE_end_sequence for the end of the section. */
1127 frag
= last_frag_for_seg (seg
);
1128 frag_ofs
= get_frag_fix (frag
, seg
);
1129 if (frag
== last_frag
)
1130 out_inc_line_addr (INT_MAX
, frag_ofs
- last_frag_ofs
);
1133 lab
= symbol_temp_new (seg
, frag_ofs
, frag
);
1134 relax_inc_line_addr (INT_MAX
, lab
, last_lab
);
1138 /* Emit the directory and file tables for .debug_line. */
1141 out_file_list (void)
1147 /* Emit directory list. */
1148 for (i
= 1; i
< dirs_in_use
; ++i
)
1150 size
= strlen (dirs
[i
]) + 1;
1151 cp
= frag_more (size
);
1152 memcpy (cp
, dirs
[i
], size
);
1157 for (i
= 1; i
< files_in_use
; ++i
)
1159 if (files
[i
].filename
== NULL
)
1161 as_bad (_("unassigned file number %ld"), (long) i
);
1162 /* Prevent a crash later, particularly for file 1. */
1163 files
[i
].filename
= "";
1167 size
= strlen (files
[i
].filename
) + 1;
1168 cp
= frag_more (size
);
1169 memcpy (cp
, files
[i
].filename
, size
);
1171 out_uleb128 (files
[i
].dir
); /* directory number */
1172 out_uleb128 (0); /* last modification timestamp */
1173 out_uleb128 (0); /* filesize */
1176 /* Terminate filename list. */
1180 /* Emit the collected .debug_line data. */
1183 out_debug_line (segT line_seg
)
1186 symbolS
*line_start
;
1187 symbolS
*prologue_end
;
1190 enum dwarf2_format d2f
;
1193 subseg_set (line_seg
, 0);
1195 line_start
= symbol_temp_new_now ();
1196 prologue_end
= symbol_temp_make ();
1197 line_end
= symbol_temp_make ();
1199 /* Total length of the information for this compilation unit. */
1200 expr
.X_op
= O_subtract
;
1201 expr
.X_add_symbol
= line_end
;
1202 expr
.X_op_symbol
= line_start
;
1204 d2f
= DWARF2_FORMAT ();
1205 if (d2f
== dwarf2_format_32bit
)
1207 expr
.X_add_number
= -4;
1208 emit_expr (&expr
, 4);
1211 else if (d2f
== dwarf2_format_64bit
)
1213 expr
.X_add_number
= -12;
1215 emit_expr (&expr
, 8);
1218 else if (d2f
== dwarf2_format_64bit_irix
)
1220 expr
.X_add_number
= -8;
1221 emit_expr (&expr
, 8);
1226 as_fatal (_("internal error: unknown dwarf2 format"));
1232 /* Length of the prologue following this length. */
1233 expr
.X_op
= O_subtract
;
1234 expr
.X_add_symbol
= prologue_end
;
1235 expr
.X_op_symbol
= line_start
;
1236 expr
.X_add_number
= - (4 + 2 + 4);
1237 emit_expr (&expr
, sizeof_offset
);
1239 /* Parameters of the state machine. */
1240 out_byte (DWARF2_LINE_MIN_INSN_LENGTH
);
1241 out_byte (DWARF2_LINE_DEFAULT_IS_STMT
);
1242 out_byte (DWARF2_LINE_BASE
);
1243 out_byte (DWARF2_LINE_RANGE
);
1244 out_byte (DWARF2_LINE_OPCODE_BASE
);
1246 /* Standard opcode lengths. */
1247 out_byte (0); /* DW_LNS_copy */
1248 out_byte (1); /* DW_LNS_advance_pc */
1249 out_byte (1); /* DW_LNS_advance_line */
1250 out_byte (1); /* DW_LNS_set_file */
1251 out_byte (1); /* DW_LNS_set_column */
1252 out_byte (0); /* DW_LNS_negate_stmt */
1253 out_byte (0); /* DW_LNS_set_basic_block */
1254 out_byte (0); /* DW_LNS_const_add_pc */
1255 out_byte (1); /* DW_LNS_fixed_advance_pc */
1256 out_byte (0); /* DW_LNS_set_prologue_end */
1257 out_byte (0); /* DW_LNS_set_epilogue_begin */
1258 out_byte (1); /* DW_LNS_set_isa */
1262 symbol_set_value_now (prologue_end
);
1264 /* For each section, emit a statement program. */
1265 for (s
= all_segs
; s
; s
= s
->next
)
1266 process_entries (s
->seg
, s
->head
->head
);
1268 symbol_set_value_now (line_end
);
1271 /* Emit data for .debug_aranges. */
1274 out_debug_aranges (segT aranges_seg
, segT info_seg
)
1276 unsigned int addr_size
= sizeof_address
;
1277 addressT size
, skip
;
1282 size
= 4 + 2 + 4 + 1 + 1;
1284 skip
= 2 * addr_size
- (size
& (2 * addr_size
- 1));
1285 if (skip
== 2 * addr_size
)
1289 for (s
= all_segs
; s
; s
= s
->next
)
1290 size
+= 2 * addr_size
;
1292 size
+= 2 * addr_size
;
1294 subseg_set (aranges_seg
, 0);
1296 /* Length of the compilation unit. */
1297 out_four (size
- 4);
1302 /* Offset to .debug_info. */
1303 /* ??? sizeof_offset */
1304 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg
), 4);
1306 /* Size of an address (offset portion). */
1307 out_byte (addr_size
);
1309 /* Size of a segment descriptor. */
1312 /* Align the header. */
1314 frag_align (ffs (2 * addr_size
) - 1, 0, 0);
1316 for (s
= all_segs
; s
; s
= s
->next
)
1321 frag
= first_frag_for_seg (s
->seg
);
1322 beg
= symbol_temp_new (s
->seg
, 0, frag
);
1323 s
->text_start
= beg
;
1325 frag
= last_frag_for_seg (s
->seg
);
1326 end
= symbol_temp_new (s
->seg
, get_frag_fix (frag
, s
->seg
), frag
);
1329 expr
.X_op
= O_symbol
;
1330 expr
.X_add_symbol
= beg
;
1331 expr
.X_add_number
= 0;
1332 emit_expr (&expr
, addr_size
);
1334 expr
.X_op
= O_subtract
;
1335 expr
.X_add_symbol
= end
;
1336 expr
.X_op_symbol
= beg
;
1337 expr
.X_add_number
= 0;
1338 emit_expr (&expr
, addr_size
);
1341 p
= frag_more (2 * addr_size
);
1342 md_number_to_chars (p
, 0, addr_size
);
1343 md_number_to_chars (p
+ addr_size
, 0, addr_size
);
1346 /* Emit data for .debug_abbrev. Note that this must be kept in
1347 sync with out_debug_info below. */
1350 out_debug_abbrev (segT abbrev_seg
)
1352 subseg_set (abbrev_seg
, 0);
1355 out_uleb128 (DW_TAG_compile_unit
);
1356 out_byte (DW_CHILDREN_no
);
1357 out_abbrev (DW_AT_stmt_list
, DW_FORM_data4
);
1358 if (all_segs
->next
== NULL
)
1360 out_abbrev (DW_AT_low_pc
, DW_FORM_addr
);
1361 out_abbrev (DW_AT_high_pc
, DW_FORM_addr
);
1363 out_abbrev (DW_AT_name
, DW_FORM_string
);
1364 out_abbrev (DW_AT_comp_dir
, DW_FORM_string
);
1365 out_abbrev (DW_AT_producer
, DW_FORM_string
);
1366 out_abbrev (DW_AT_language
, DW_FORM_data2
);
1369 /* Terminate the abbreviations for this compilation unit. */
1373 /* Emit a description of this compilation unit for .debug_info. */
1376 out_debug_info (segT info_seg
, segT abbrev_seg
, segT line_seg
)
1381 symbolS
*info_start
;
1385 enum dwarf2_format d2f
;
1388 subseg_set (info_seg
, 0);
1390 info_start
= symbol_temp_new_now ();
1391 info_end
= symbol_temp_make ();
1393 /* Compilation Unit length. */
1394 expr
.X_op
= O_subtract
;
1395 expr
.X_add_symbol
= info_end
;
1396 expr
.X_op_symbol
= info_start
;
1398 d2f
= DWARF2_FORMAT ();
1399 if (d2f
== dwarf2_format_32bit
)
1401 expr
.X_add_number
= -4;
1402 emit_expr (&expr
, 4);
1405 else if (d2f
== dwarf2_format_64bit
)
1407 expr
.X_add_number
= -12;
1409 emit_expr (&expr
, 8);
1412 else if (d2f
== dwarf2_format_64bit_irix
)
1414 expr
.X_add_number
= -8;
1415 emit_expr (&expr
, 8);
1420 as_fatal (_("internal error: unknown dwarf2 format"));
1423 /* DWARF version. */
1426 /* .debug_abbrev offset */
1427 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg
), sizeof_offset
);
1429 /* Target address size. */
1430 out_byte (sizeof_address
);
1432 /* DW_TAG_compile_unit DIE abbrev */
1435 /* DW_AT_stmt_list */
1436 /* ??? sizeof_offset */
1437 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg
), 4);
1439 /* These two attributes may only be emitted if all of the code is
1440 contiguous. Multiple sections are not that. */
1441 if (all_segs
->next
== NULL
)
1444 expr
.X_op
= O_symbol
;
1445 expr
.X_add_symbol
= all_segs
->text_start
;
1446 expr
.X_add_number
= 0;
1447 emit_expr (&expr
, sizeof_address
);
1450 expr
.X_op
= O_symbol
;
1451 expr
.X_add_symbol
= all_segs
->text_end
;
1452 expr
.X_add_number
= 0;
1453 emit_expr (&expr
, sizeof_address
);
1456 /* DW_AT_name. We don't have the actual file name that was present
1457 on the command line, so assume files[1] is the main input file.
1458 We're not supposed to get called unless at least one line number
1459 entry was emitted, so this should always be defined. */
1460 if (!files
|| files_in_use
< 1)
1464 len
= strlen (dirs
[files
[1].dir
]);
1465 p
= frag_more (len
+ 1);
1466 memcpy (p
, dirs
[files
[1].dir
], len
);
1469 len
= strlen (files
[1].filename
) + 1;
1470 p
= frag_more (len
);
1471 memcpy (p
, files
[1].filename
, len
);
1473 /* DW_AT_comp_dir */
1474 comp_dir
= getpwd ();
1475 len
= strlen (comp_dir
) + 1;
1476 p
= frag_more (len
);
1477 memcpy (p
, comp_dir
, len
);
1479 /* DW_AT_producer */
1480 sprintf (producer
, "GNU AS %s", VERSION
);
1481 len
= strlen (producer
) + 1;
1482 p
= frag_more (len
);
1483 memcpy (p
, producer
, len
);
1485 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1486 dwarf2 draft has no standard code for assembler. */
1487 out_two (DW_LANG_Mips_Assembler
);
1489 symbol_set_value_now (info_end
);
1492 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1493 were any .file/.loc directives, or --gdwarf2 was given, or if the
1494 file has a non-empty .debug_info section. If we emit .debug_line,
1495 and the .debug_info section is empty, we also emit .debug_info,
1496 .debug_aranges and .debug_abbrev. ALL_SEGS will be non-null if
1497 there were any .file/.loc directives, or --gdwarf2 was given and
1498 there were any located instructions emitted. */
1501 dwarf2_finish (void)
1506 int emit_other_sections
= 0;
1508 info_seg
= bfd_get_section_by_name (stdoutput
, ".debug_info");
1509 emit_other_sections
= info_seg
== NULL
|| !seg_not_empty_p (info_seg
);
1511 if (!all_segs
&& emit_other_sections
)
1512 /* There is no line information and no non-empty .debug_info
1516 /* Calculate the size of an address for the target machine. */
1517 sizeof_address
= DWARF2_ADDR_SIZE (stdoutput
);
1519 /* Create and switch to the line number section. */
1520 line_seg
= subseg_new (".debug_line", 0);
1521 bfd_set_section_flags (stdoutput
, line_seg
, SEC_READONLY
| SEC_DEBUGGING
);
1523 /* For each subsection, chain the debug entries together. */
1524 for (s
= all_segs
; s
; s
= s
->next
)
1526 struct line_subseg
*ss
= s
->head
;
1527 struct line_entry
**ptail
= ss
->ptail
;
1529 while ((ss
= ss
->next
) != NULL
)
1536 out_debug_line (line_seg
);
1538 /* If this is assembler generated line info, and there is no
1539 debug_info already, we need .debug_info and .debug_abbrev
1540 sections as well. */
1541 if (emit_other_sections
)
1548 info_seg
= subseg_new (".debug_info", 0);
1549 abbrev_seg
= subseg_new (".debug_abbrev", 0);
1550 aranges_seg
= subseg_new (".debug_aranges", 0);
1552 bfd_set_section_flags (stdoutput
, info_seg
,
1553 SEC_READONLY
| SEC_DEBUGGING
);
1554 bfd_set_section_flags (stdoutput
, abbrev_seg
,
1555 SEC_READONLY
| SEC_DEBUGGING
);
1556 bfd_set_section_flags (stdoutput
, aranges_seg
,
1557 SEC_READONLY
| SEC_DEBUGGING
);
1559 record_alignment (aranges_seg
, ffs (2 * sizeof_address
) - 1);
1561 out_debug_aranges (aranges_seg
, info_seg
);
1562 out_debug_abbrev (abbrev_seg
);
1563 out_debug_info (info_seg
, abbrev_seg
, line_seg
);