1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 /* Logical line numbers can be controlled by the compiler via the
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
;
110 struct dwarf2_line_info loc
;
114 struct line_subseg
*next
;
116 struct line_entry
*head
;
117 struct line_entry
**ptail
;
121 struct line_seg
*next
;
123 struct line_subseg
*head
;
128 /* Collects data for all line table entries during assembly. */
129 static struct line_seg
*all_segs
;
132 const char *filename
;
136 /* Table of files used by .debug_line. */
137 static struct file_entry
*files
;
138 static unsigned int files_in_use
;
139 static unsigned int files_allocated
;
141 /* Table of directories used by .debug_line. */
143 static unsigned int dirs_in_use
;
144 static unsigned int dirs_allocated
;
146 /* TRUE when we've seen a .loc directive recently. Used to avoid
147 doing work when there's nothing to do. */
148 static bfd_boolean loc_directive_seen
;
150 /* Current location as indicated by the most recent .loc directive. */
151 static struct dwarf2_line_info current
= {
153 DWARF2_LINE_DEFAULT_IS_STMT
? DWARF2_FLAG_IS_STMT
: 0
156 /* The size of an address on the target. */
157 static unsigned int sizeof_address
;
159 static struct line_subseg
*get_line_subseg (segT
, subsegT
);
160 static unsigned int get_filenum (const char *, unsigned int);
161 static struct frag
*first_frag_for_seg (segT
);
162 static struct frag
*last_frag_for_seg (segT
);
163 static void out_byte (int);
164 static void out_opcode (int);
165 static void out_two (int);
166 static void out_four (int);
167 static void out_abbrev (int, int);
168 static void out_uleb128 (addressT
);
169 static offsetT
get_frag_fix (fragS
*);
170 static void out_set_addr (segT
, fragS
*, addressT
);
171 static int size_inc_line_addr (int, addressT
);
172 static void emit_inc_line_addr (int, addressT
, char *, int);
173 static void out_inc_line_addr (int, addressT
);
174 static void relax_inc_line_addr (int, segT
, fragS
*, addressT
,
176 static void process_entries (segT
, struct line_entry
*);
177 static void out_file_list (void);
178 static void out_debug_line (segT
);
179 static void out_debug_aranges (segT
, segT
);
180 static void out_debug_abbrev (segT
);
181 static void out_debug_info (segT
, segT
, segT
);
183 #ifndef TC_DWARF2_EMIT_OFFSET
184 # define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
185 static void generic_dwarf2_emit_offset (symbolS
*, unsigned int);
187 /* Create an offset to .dwarf2_*. */
190 generic_dwarf2_emit_offset (symbolS
*symbol
, unsigned int size
)
194 expr
.X_op
= O_symbol
;
195 expr
.X_add_symbol
= symbol
;
196 expr
.X_add_number
= 0;
197 emit_expr (&expr
, size
);
201 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
203 static struct line_subseg
*
204 get_line_subseg (segT seg
, subsegT subseg
)
206 static segT last_seg
;
207 static subsegT last_subseg
;
208 static struct line_subseg
*last_line_subseg
;
211 struct line_subseg
**pss
, *ss
;
213 if (seg
== last_seg
&& subseg
== last_subseg
)
214 return last_line_subseg
;
216 for (s
= all_segs
; s
; s
= s
->next
)
220 s
= (struct line_seg
*) xmalloc (sizeof (*s
));
227 for (pss
= &s
->head
; (ss
= *pss
) != NULL
; pss
= &ss
->next
)
229 if (ss
->subseg
== subseg
)
231 if (ss
->subseg
> subseg
)
235 ss
= (struct line_subseg
*) xmalloc (sizeof (*ss
));
239 ss
->ptail
= &ss
->head
;
244 last_subseg
= subseg
;
245 last_line_subseg
= ss
;
250 /* Record an entry for LOC occurring at OFS within the current fragment. */
253 dwarf2_gen_line_info (addressT ofs
, struct dwarf2_line_info
*loc
)
255 struct line_subseg
*ss
;
256 struct line_entry
*e
;
257 static unsigned int line
= -1;
258 static unsigned int filenum
= -1;
260 /* Early out for as-yet incomplete location information. */
261 if (loc
->filenum
== 0 || loc
->line
== 0)
264 /* Don't emit sequences of line symbols for the same line when the
265 symbols apply to assembler code. It is necessary to emit
266 duplicate line symbols when a compiler asks for them, because GDB
267 uses them to determine the end of the prologue. */
268 if (debug_type
== DEBUG_DWARF2
269 && line
== loc
->line
&& filenum
== loc
->filenum
)
273 filenum
= loc
->filenum
;
275 e
= (struct line_entry
*) xmalloc (sizeof (*e
));
281 ss
= get_line_subseg (now_seg
, now_subseg
);
283 ss
->ptail
= &e
->next
;
286 /* Returns the current source information. If .file directives have
287 been encountered, the info for the corresponding source file is
288 returned. Otherwise, the info for the assembly source file is
292 dwarf2_where (struct dwarf2_line_info
*line
)
294 if (debug_type
== DEBUG_DWARF2
)
297 as_where (&filename
, &line
->line
);
298 line
->filenum
= get_filenum (filename
, 0);
300 line
->flags
= DWARF2_FLAG_IS_STMT
;
301 line
->isa
= current
.isa
;
307 /* A hook to allow the target backend to inform the line number state
308 machine of isa changes when assembler debug info is enabled. */
311 dwarf2_set_isa (unsigned int isa
)
316 /* Called for each machine instruction, or relatively atomic group of
317 machine instructions (ie built-in macro). The instruction or group
318 is SIZE bytes in length. If dwarf2 line number generation is called
319 for, emit a line statement appropriately. */
322 dwarf2_emit_insn (int size
)
324 struct dwarf2_line_info loc
;
326 if (loc_directive_seen
)
328 /* Use the last location established by a .loc directive, not
329 the value returned by dwarf2_where(). That calls as_where()
330 which will return either the logical input file name (foo.c)
331 or the physical input file name (foo.s) and not the file name
332 specified in the most recent .loc directive (eg foo.h). */
335 /* Unless we generate DWARF2 debugging information for each
336 assembler line, we only emit one line symbol for one LOC. */
337 if (debug_type
!= DEBUG_DWARF2
)
338 loc_directive_seen
= FALSE
;
340 else if (debug_type
!= DEBUG_DWARF2
)
345 dwarf2_gen_line_info (frag_now_fix () - size
, &loc
);
347 current
.flags
&= ~(DWARF2_FLAG_BASIC_BLOCK
348 | DWARF2_FLAG_PROLOGUE_END
349 | DWARF2_FLAG_EPILOGUE_BEGIN
);
352 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
353 allocate it on that file table slot, otherwise return the first
357 get_filenum (const char *filename
, unsigned int num
)
359 static unsigned int last_used
, last_used_dir_len
;
364 if (num
== 0 && last_used
)
366 if (! files
[last_used
].dir
367 && strcmp (filename
, files
[last_used
].filename
) == 0)
369 if (files
[last_used
].dir
370 && strncmp (filename
, dirs
[files
[last_used
].dir
],
371 last_used_dir_len
) == 0
372 && IS_DIR_SEPARATOR (filename
[last_used_dir_len
])
373 && strcmp (filename
+ last_used_dir_len
+ 1,
374 files
[last_used
].filename
) == 0)
378 file
= lbasename (filename
);
379 /* Don't make empty string from / or A: from A:/ . */
380 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
381 if (file
<= filename
+ 3)
384 if (file
== filename
+ 1)
387 dir_len
= file
- filename
;
393 for (dir
= 1; dir
< dirs_in_use
; ++dir
)
394 if (strncmp (filename
, dirs
[dir
], dir_len
) == 0
395 && dirs
[dir
][dir_len
] == '\0')
398 if (dir
>= dirs_in_use
)
400 if (dir
>= dirs_allocated
)
402 dirs_allocated
= dir
+ 32;
404 xrealloc (dirs
, (dir
+ 32) * sizeof (const char *));
407 dirs
[dir
] = xmalloc (dir_len
+ 1);
408 memcpy (dirs
[dir
], filename
, dir_len
);
409 dirs
[dir
][dir_len
] = '\0';
410 dirs_in_use
= dir
+ 1;
416 for (i
= 1; i
< files_in_use
; ++i
)
417 if (files
[i
].dir
== dir
419 && strcmp (file
, files
[i
].filename
) == 0)
422 last_used_dir_len
= dir_len
;
429 if (i
>= files_allocated
)
431 unsigned int old
= files_allocated
;
433 files_allocated
= i
+ 32;
434 files
= (struct file_entry
*)
435 xrealloc (files
, (i
+ 32) * sizeof (struct file_entry
));
437 memset (files
+ old
, 0, (i
+ 32 - old
) * sizeof (struct file_entry
));
440 files
[i
].filename
= num
? file
: xstrdup (file
);
442 files_in_use
= i
+ 1;
444 last_used_dir_len
= dir_len
;
449 /* Handle two forms of .file directive:
450 - Pass .file "source.c" to s_app_file
451 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
453 If an entry is added to the file table, return a pointer to the filename. */
456 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED
)
462 /* Continue to accept a bare string and pass it off. */
464 if (*input_line_pointer
== '"')
470 num
= get_absolute_expression ();
471 filename
= demand_copy_C_string (&filename_len
);
472 if (filename
== NULL
)
474 demand_empty_rest_of_line ();
478 as_bad (_("file number less than one"));
482 if (num
< (int) files_in_use
&& files
[num
].filename
!= 0)
484 as_bad (_("file number %ld already allocated"), (long) num
);
488 get_filenum (filename
, num
);
494 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED
)
496 offsetT filenum
, line
;
498 filenum
= get_absolute_expression ();
500 line
= get_absolute_expression ();
504 as_bad (_("file number less than one"));
507 if (filenum
>= (int) files_in_use
|| files
[filenum
].filename
== 0)
509 as_bad (_("unassigned file number %ld"), (long) filenum
);
513 current
.filenum
= filenum
;
519 if (files
[filenum
].dir
)
521 size_t dir_len
= strlen (dirs
[files
[filenum
].dir
]);
522 size_t file_len
= strlen (files
[filenum
].filename
);
523 char *cp
= (char *) alloca (dir_len
+ 1 + file_len
+ 1);
525 memcpy (cp
, dirs
[files
[filenum
].dir
], dir_len
);
527 memcpy (cp
+ dir_len
+ 1, files
[filenum
].filename
, file_len
);
528 cp
[dir_len
+ file_len
+ 1] = '\0';
529 listing_source_file (cp
);
532 listing_source_file (files
[filenum
].filename
);
533 listing_source_line (line
);
538 if (ISDIGIT (*input_line_pointer
))
540 current
.column
= get_absolute_expression ();
544 while (ISALPHA (*input_line_pointer
))
549 p
= input_line_pointer
;
550 c
= get_symbol_end ();
552 if (strcmp (p
, "basic_block") == 0)
554 current
.flags
|= DWARF2_FLAG_BASIC_BLOCK
;
555 *input_line_pointer
= c
;
557 else if (strcmp (p
, "prologue_end") == 0)
559 current
.flags
|= DWARF2_FLAG_PROLOGUE_END
;
560 *input_line_pointer
= c
;
562 else if (strcmp (p
, "epilogue_begin") == 0)
564 current
.flags
|= DWARF2_FLAG_EPILOGUE_BEGIN
;
565 *input_line_pointer
= c
;
567 else if (strcmp (p
, "is_stmt") == 0)
569 *input_line_pointer
= c
;
570 value
= get_absolute_expression ();
572 current
.flags
&= ~DWARF2_FLAG_IS_STMT
;
574 current
.flags
|= DWARF2_FLAG_IS_STMT
;
577 as_bad (_("is_stmt value not 0 or 1"));
581 else if (strcmp (p
, "isa") == 0)
583 *input_line_pointer
= c
;
584 value
= get_absolute_expression ();
589 as_bad (_("isa number less than zero"));
595 as_bad (_("unknown .loc sub-directive `%s'"), p
);
596 *input_line_pointer
= c
;
603 demand_empty_rest_of_line ();
604 loc_directive_seen
= TRUE
;
608 first_frag_for_seg (segT seg
)
610 frchainS
*f
, *first
= NULL
;
612 for (f
= frchain_root
; f
; f
= f
->frch_next
)
613 if (f
->frch_seg
== seg
614 && (! first
|| first
->frch_subseg
> f
->frch_subseg
))
617 return first
? first
->frch_root
: NULL
;
621 last_frag_for_seg (segT seg
)
623 frchainS
*f
, *last
= NULL
;
625 for (f
= frchain_root
; f
; f
= f
->frch_next
)
626 if (f
->frch_seg
== seg
627 && (! last
|| last
->frch_subseg
< f
->frch_subseg
))
630 return last
? last
->frch_last
: NULL
;
633 /* Emit a single byte into the current segment. */
638 FRAG_APPEND_1_CHAR (byte
);
641 /* Emit a statement program opcode into the current segment. */
649 /* Emit a two-byte word into the current segment. */
654 md_number_to_chars (frag_more (2), data
, 2);
657 /* Emit a four byte word into the current segment. */
662 md_number_to_chars (frag_more (4), data
, 4);
665 /* Emit an unsigned "little-endian base 128" number. */
668 out_uleb128 (addressT value
)
670 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
673 /* Emit a tuple for .debug_abbrev. */
676 out_abbrev (int name
, int form
)
682 /* Get the size of a fragment. */
685 get_frag_fix (fragS
*frag
)
692 /* If a fragment is the last in the chain, special measures must be
693 taken to find its size before relaxation, since it may be pending
694 on some subsegment chain. */
695 for (fr
= frchain_root
; fr
; fr
= fr
->frch_next
)
696 if (fr
->frch_last
== frag
)
697 return (char *) obstack_next_free (&fr
->frch_obstack
) - frag
->fr_literal
;
702 /* Set an absolute address (may result in a relocation entry). */
705 out_set_addr (segT seg
, fragS
*frag
, addressT ofs
)
710 sym
= symbol_temp_new (seg
, ofs
, frag
);
712 out_opcode (DW_LNS_extended_op
);
713 out_uleb128 (sizeof_address
+ 1);
715 out_opcode (DW_LNE_set_address
);
716 expr
.X_op
= O_symbol
;
717 expr
.X_add_symbol
= sym
;
718 expr
.X_add_number
= 0;
719 emit_expr (&expr
, sizeof_address
);
722 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
723 static void scale_addr_delta (addressT
*);
726 scale_addr_delta (addressT
*addr_delta
)
728 static int printed_this
= 0;
729 if (*addr_delta
% DWARF2_LINE_MIN_INSN_LENGTH
!= 0)
732 as_bad("unaligned opcodes detected in executable segment");
735 *addr_delta
/= DWARF2_LINE_MIN_INSN_LENGTH
;
738 #define scale_addr_delta(A)
741 /* Encode a pair of line and address skips as efficiently as possible.
742 Note that the line skip is signed, whereas the address skip is unsigned.
744 The following two routines *must* be kept in sync. This is
745 enforced by making emit_inc_line_addr abort if we do not emit
746 exactly the expected number of bytes. */
749 size_inc_line_addr (int line_delta
, addressT addr_delta
)
751 unsigned int tmp
, opcode
;
754 /* Scale the address delta by the minimum instruction length. */
755 scale_addr_delta (&addr_delta
);
757 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
758 We cannot use special opcodes here, since we want the end_sequence
759 to emit the matrix entry. */
760 if (line_delta
== INT_MAX
)
762 if (addr_delta
== MAX_SPECIAL_ADDR_DELTA
)
765 len
= 1 + sizeof_leb128 (addr_delta
, 0);
769 /* Bias the line delta by the base. */
770 tmp
= line_delta
- DWARF2_LINE_BASE
;
772 /* If the line increment is out of range of a special opcode, we
773 must encode it with DW_LNS_advance_line. */
774 if (tmp
>= DWARF2_LINE_RANGE
)
776 len
= 1 + sizeof_leb128 (line_delta
, 1);
778 tmp
= 0 - DWARF2_LINE_BASE
;
781 /* Bias the opcode by the special opcode base. */
782 tmp
+= DWARF2_LINE_OPCODE_BASE
;
784 /* Avoid overflow when addr_delta is large. */
785 if (addr_delta
< 256 + MAX_SPECIAL_ADDR_DELTA
)
787 /* Try using a special opcode. */
788 opcode
= tmp
+ addr_delta
* DWARF2_LINE_RANGE
;
792 /* Try using DW_LNS_const_add_pc followed by special op. */
793 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
798 /* Otherwise use DW_LNS_advance_pc. */
799 len
+= 1 + sizeof_leb128 (addr_delta
, 0);
801 /* DW_LNS_copy or special opcode. */
808 emit_inc_line_addr (int line_delta
, addressT addr_delta
, char *p
, int len
)
810 unsigned int tmp
, opcode
;
814 /* Scale the address delta by the minimum instruction length. */
815 scale_addr_delta (&addr_delta
);
817 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
818 We cannot use special opcodes here, since we want the end_sequence
819 to emit the matrix entry. */
820 if (line_delta
== INT_MAX
)
822 if (addr_delta
== MAX_SPECIAL_ADDR_DELTA
)
823 *p
++ = DW_LNS_const_add_pc
;
826 *p
++ = DW_LNS_advance_pc
;
827 p
+= output_leb128 (p
, addr_delta
, 0);
830 *p
++ = DW_LNS_extended_op
;
832 *p
++ = DW_LNE_end_sequence
;
836 /* Bias the line delta by the base. */
837 tmp
= line_delta
- DWARF2_LINE_BASE
;
839 /* If the line increment is out of range of a special opcode, we
840 must encode it with DW_LNS_advance_line. */
841 if (tmp
>= DWARF2_LINE_RANGE
)
843 *p
++ = DW_LNS_advance_line
;
844 p
+= output_leb128 (p
, line_delta
, 1);
847 tmp
= 0 - DWARF2_LINE_BASE
;
851 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
853 if (line_delta
== 0 && addr_delta
== 0)
859 /* Bias the opcode by the special opcode base. */
860 tmp
+= DWARF2_LINE_OPCODE_BASE
;
862 /* Avoid overflow when addr_delta is large. */
863 if (addr_delta
< 256 + MAX_SPECIAL_ADDR_DELTA
)
865 /* Try using a special opcode. */
866 opcode
= tmp
+ addr_delta
* DWARF2_LINE_RANGE
;
873 /* Try using DW_LNS_const_add_pc followed by special op. */
874 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
877 *p
++ = DW_LNS_const_add_pc
;
883 /* Otherwise use DW_LNS_advance_pc. */
884 *p
++ = DW_LNS_advance_pc
;
885 p
+= output_leb128 (p
, addr_delta
, 0);
896 /* Handy routine to combine calls to the above two routines. */
899 out_inc_line_addr (int line_delta
, addressT addr_delta
)
901 int len
= size_inc_line_addr (line_delta
, addr_delta
);
902 emit_inc_line_addr (line_delta
, addr_delta
, frag_more (len
), len
);
905 /* Generate a variant frag that we can use to relax address/line
906 increments between fragments of the target segment. */
909 relax_inc_line_addr (int line_delta
, segT seg
,
910 fragS
*to_frag
, addressT to_ofs
,
911 fragS
*from_frag
, addressT from_ofs
)
913 symbolS
*to_sym
, *from_sym
;
917 to_sym
= symbol_temp_new (seg
, to_ofs
, to_frag
);
918 from_sym
= symbol_temp_new (seg
, from_ofs
, from_frag
);
920 expr
.X_op
= O_subtract
;
921 expr
.X_add_symbol
= to_sym
;
922 expr
.X_op_symbol
= from_sym
;
923 expr
.X_add_number
= 0;
925 /* The maximum size of the frag is the line delta with a maximum
926 sized address delta. */
927 max_chars
= size_inc_line_addr (line_delta
, -DWARF2_LINE_MIN_INSN_LENGTH
);
929 frag_var (rs_dwarf2dbg
, max_chars
, max_chars
, 1,
930 make_expr_symbol (&expr
), line_delta
, NULL
);
933 /* The function estimates the size of a rs_dwarf2dbg variant frag
934 based on the current values of the symbols. It is called before
935 the relaxation loop. We set fr_subtype to the expected length. */
938 dwarf2dbg_estimate_size_before_relax (fragS
*frag
)
943 addr_delta
= resolve_symbol_value (frag
->fr_symbol
);
944 size
= size_inc_line_addr (frag
->fr_offset
, addr_delta
);
946 frag
->fr_subtype
= size
;
951 /* This function relaxes a rs_dwarf2dbg variant frag based on the
952 current values of the symbols. fr_subtype is the current length
953 of the frag. This returns the change in frag length. */
956 dwarf2dbg_relax_frag (fragS
*frag
)
958 int old_size
, new_size
;
960 old_size
= frag
->fr_subtype
;
961 new_size
= dwarf2dbg_estimate_size_before_relax (frag
);
963 return new_size
- old_size
;
966 /* This function converts a rs_dwarf2dbg variant frag into a normal
967 fill frag. This is called after all relaxation has been done.
968 fr_subtype will be the desired length of the frag. */
971 dwarf2dbg_convert_frag (fragS
*frag
)
975 addr_diff
= resolve_symbol_value (frag
->fr_symbol
);
977 /* fr_var carries the max_chars that we created the fragment with.
978 fr_subtype carries the current expected length. We must, of
979 course, have allocated enough memory earlier. */
980 assert (frag
->fr_var
>= (int) frag
->fr_subtype
);
982 emit_inc_line_addr (frag
->fr_offset
, addr_diff
,
983 frag
->fr_literal
+ frag
->fr_fix
, frag
->fr_subtype
);
985 frag
->fr_fix
+= frag
->fr_subtype
;
986 frag
->fr_type
= rs_fill
;
991 /* Generate .debug_line content for the chain of line number entries
992 beginning at E, for segment SEG. */
995 process_entries (segT seg
, struct line_entry
*e
)
997 unsigned filenum
= 1;
1001 unsigned flags
= DWARF2_LINE_DEFAULT_IS_STMT
? DWARF2_FLAG_IS_STMT
: 0;
1004 addressT frag_ofs
= 0;
1005 addressT last_frag_ofs
;
1006 struct line_entry
*next
;
1012 if (filenum
!= e
->loc
.filenum
)
1014 filenum
= e
->loc
.filenum
;
1015 out_opcode (DW_LNS_set_file
);
1016 out_uleb128 (filenum
);
1020 if (column
!= e
->loc
.column
)
1022 column
= e
->loc
.column
;
1023 out_opcode (DW_LNS_set_column
);
1024 out_uleb128 (column
);
1028 if (isa
!= e
->loc
.isa
)
1031 out_opcode (DW_LNS_set_isa
);
1036 if ((e
->loc
.flags
^ flags
) & DWARF2_FLAG_IS_STMT
)
1038 flags
= e
->loc
.flags
;
1039 out_opcode (DW_LNS_negate_stmt
);
1043 if (e
->loc
.flags
& DWARF2_FLAG_BASIC_BLOCK
)
1045 out_opcode (DW_LNS_set_basic_block
);
1049 if (e
->loc
.flags
& DWARF2_FLAG_PROLOGUE_END
)
1051 out_opcode (DW_LNS_set_prologue_end
);
1055 if (e
->loc
.flags
& DWARF2_FLAG_EPILOGUE_BEGIN
)
1057 out_opcode (DW_LNS_set_epilogue_begin
);
1061 /* Don't try to optimize away redundant entries; gdb wants two
1062 entries for a function where the code starts on the same line as
1063 the {, and there's no way to identify that case here. Trust gcc
1064 to optimize appropriately. */
1065 if (1 /* line != e->loc.line || changed */)
1067 int line_delta
= e
->loc
.line
- line
;
1070 out_set_addr (seg
, e
->frag
, e
->frag_ofs
);
1071 out_inc_line_addr (line_delta
, 0);
1073 else if (frag
== e
->frag
)
1074 out_inc_line_addr (line_delta
, e
->frag_ofs
- frag_ofs
);
1076 relax_inc_line_addr (line_delta
, seg
, e
->frag
, e
->frag_ofs
,
1080 frag_ofs
= e
->frag_ofs
;
1083 else if (frag
== NULL
)
1085 out_set_addr (seg
, e
->frag
, e
->frag_ofs
);
1087 frag_ofs
= e
->frag_ofs
;
1095 /* Emit a DW_LNE_end_sequence for the end of the section. */
1096 last_frag
= last_frag_for_seg (seg
);
1097 last_frag_ofs
= get_frag_fix (last_frag
);
1098 if (frag
== last_frag
)
1099 out_inc_line_addr (INT_MAX
, last_frag_ofs
- frag_ofs
);
1101 relax_inc_line_addr (INT_MAX
, seg
, last_frag
, last_frag_ofs
,
1105 /* Emit the directory and file tables for .debug_line. */
1108 out_file_list (void)
1114 /* Emit directory list. */
1115 for (i
= 1; i
< dirs_in_use
; ++i
)
1117 size
= strlen (dirs
[i
]) + 1;
1118 cp
= frag_more (size
);
1119 memcpy (cp
, dirs
[i
], size
);
1124 for (i
= 1; i
< files_in_use
; ++i
)
1126 if (files
[i
].filename
== NULL
)
1128 as_bad (_("unassigned file number %ld"), (long) i
);
1129 /* Prevent a crash later, particularly for file 1. */
1130 files
[i
].filename
= "";
1134 size
= strlen (files
[i
].filename
) + 1;
1135 cp
= frag_more (size
);
1136 memcpy (cp
, files
[i
].filename
, size
);
1138 out_uleb128 (files
[i
].dir
); /* directory number */
1139 out_uleb128 (0); /* last modification timestamp */
1140 out_uleb128 (0); /* filesize */
1143 /* Terminate filename list. */
1147 /* Emit the collected .debug_line data. */
1150 out_debug_line (segT line_seg
)
1153 symbolS
*line_start
;
1154 symbolS
*prologue_end
;
1157 enum dwarf2_format d2f
;
1160 subseg_set (line_seg
, 0);
1162 line_start
= symbol_temp_new_now ();
1163 prologue_end
= symbol_temp_make ();
1164 line_end
= symbol_temp_make ();
1166 /* Total length of the information for this compilation unit. */
1167 expr
.X_op
= O_subtract
;
1168 expr
.X_add_symbol
= line_end
;
1169 expr
.X_op_symbol
= line_start
;
1171 d2f
= DWARF2_FORMAT ();
1172 if (d2f
== dwarf2_format_32bit
)
1174 expr
.X_add_number
= -4;
1175 emit_expr (&expr
, 4);
1178 else if (d2f
== dwarf2_format_64bit
)
1180 expr
.X_add_number
= -12;
1182 emit_expr (&expr
, 8);
1185 else if (d2f
== dwarf2_format_64bit_irix
)
1187 expr
.X_add_number
= -8;
1188 emit_expr (&expr
, 8);
1193 as_fatal (_("internal error: unknown dwarf2 format"));
1199 /* Length of the prologue following this length. */
1200 expr
.X_op
= O_subtract
;
1201 expr
.X_add_symbol
= prologue_end
;
1202 expr
.X_op_symbol
= line_start
;
1203 expr
.X_add_number
= - (4 + 2 + 4);
1204 emit_expr (&expr
, sizeof_offset
);
1206 /* Parameters of the state machine. */
1207 out_byte (DWARF2_LINE_MIN_INSN_LENGTH
);
1208 out_byte (DWARF2_LINE_DEFAULT_IS_STMT
);
1209 out_byte (DWARF2_LINE_BASE
);
1210 out_byte (DWARF2_LINE_RANGE
);
1211 out_byte (DWARF2_LINE_OPCODE_BASE
);
1213 /* Standard opcode lengths. */
1214 out_byte (0); /* DW_LNS_copy */
1215 out_byte (1); /* DW_LNS_advance_pc */
1216 out_byte (1); /* DW_LNS_advance_line */
1217 out_byte (1); /* DW_LNS_set_file */
1218 out_byte (1); /* DW_LNS_set_column */
1219 out_byte (0); /* DW_LNS_negate_stmt */
1220 out_byte (0); /* DW_LNS_set_basic_block */
1221 out_byte (0); /* DW_LNS_const_add_pc */
1222 out_byte (1); /* DW_LNS_fixed_advance_pc */
1223 out_byte (0); /* DW_LNS_set_prologue_end */
1224 out_byte (0); /* DW_LNS_set_epilogue_begin */
1225 out_byte (1); /* DW_LNS_set_isa */
1229 symbol_set_value_now (prologue_end
);
1231 /* For each section, emit a statement program. */
1232 for (s
= all_segs
; s
; s
= s
->next
)
1233 process_entries (s
->seg
, s
->head
->head
);
1235 symbol_set_value_now (line_end
);
1238 /* Emit data for .debug_aranges. */
1241 out_debug_aranges (segT aranges_seg
, segT info_seg
)
1243 unsigned int addr_size
= sizeof_address
;
1244 addressT size
, skip
;
1249 size
= 4 + 2 + 4 + 1 + 1;
1251 skip
= 2 * addr_size
- (size
& (2 * addr_size
- 1));
1252 if (skip
== 2 * addr_size
)
1256 for (s
= all_segs
; s
; s
= s
->next
)
1257 size
+= 2 * addr_size
;
1259 size
+= 2 * addr_size
;
1261 subseg_set (aranges_seg
, 0);
1263 /* Length of the compilation unit. */
1264 out_four (size
- 4);
1269 /* Offset to .debug_info. */
1270 /* ??? sizeof_offset */
1271 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg
), 4);
1273 /* Size of an address (offset portion). */
1274 out_byte (addr_size
);
1276 /* Size of a segment descriptor. */
1279 /* Align the header. */
1281 frag_align (ffs (2 * addr_size
) - 1, 0, 0);
1283 for (s
= all_segs
; s
; s
= s
->next
)
1288 frag
= first_frag_for_seg (s
->seg
);
1289 beg
= symbol_temp_new (s
->seg
, 0, frag
);
1290 s
->text_start
= beg
;
1292 frag
= last_frag_for_seg (s
->seg
);
1293 end
= symbol_temp_new (s
->seg
, get_frag_fix (frag
), frag
);
1296 expr
.X_op
= O_symbol
;
1297 expr
.X_add_symbol
= beg
;
1298 expr
.X_add_number
= 0;
1299 emit_expr (&expr
, addr_size
);
1301 expr
.X_op
= O_subtract
;
1302 expr
.X_add_symbol
= end
;
1303 expr
.X_op_symbol
= beg
;
1304 expr
.X_add_number
= 0;
1305 emit_expr (&expr
, addr_size
);
1308 p
= frag_more (2 * addr_size
);
1309 md_number_to_chars (p
, 0, addr_size
);
1310 md_number_to_chars (p
+ addr_size
, 0, addr_size
);
1313 /* Emit data for .debug_abbrev. Note that this must be kept in
1314 sync with out_debug_info below. */
1317 out_debug_abbrev (segT abbrev_seg
)
1319 subseg_set (abbrev_seg
, 0);
1322 out_uleb128 (DW_TAG_compile_unit
);
1323 out_byte (DW_CHILDREN_no
);
1324 out_abbrev (DW_AT_stmt_list
, DW_FORM_data4
);
1325 if (all_segs
->next
== NULL
)
1327 out_abbrev (DW_AT_low_pc
, DW_FORM_addr
);
1328 out_abbrev (DW_AT_high_pc
, DW_FORM_addr
);
1330 out_abbrev (DW_AT_name
, DW_FORM_string
);
1331 out_abbrev (DW_AT_comp_dir
, DW_FORM_string
);
1332 out_abbrev (DW_AT_producer
, DW_FORM_string
);
1333 out_abbrev (DW_AT_language
, DW_FORM_data2
);
1336 /* Terminate the abbreviations for this compilation unit. */
1340 /* Emit a description of this compilation unit for .debug_info. */
1343 out_debug_info (segT info_seg
, segT abbrev_seg
, segT line_seg
)
1348 symbolS
*info_start
;
1352 enum dwarf2_format d2f
;
1355 subseg_set (info_seg
, 0);
1357 info_start
= symbol_temp_new_now ();
1358 info_end
= symbol_temp_make ();
1360 /* Compilation Unit length. */
1361 expr
.X_op
= O_subtract
;
1362 expr
.X_add_symbol
= info_end
;
1363 expr
.X_op_symbol
= info_start
;
1365 d2f
= DWARF2_FORMAT ();
1366 if (d2f
== dwarf2_format_32bit
)
1368 expr
.X_add_number
= -4;
1369 emit_expr (&expr
, 4);
1372 else if (d2f
== dwarf2_format_64bit
)
1374 expr
.X_add_number
= -12;
1376 emit_expr (&expr
, 8);
1379 else if (d2f
== dwarf2_format_64bit_irix
)
1381 expr
.X_add_number
= -8;
1382 emit_expr (&expr
, 8);
1387 as_fatal (_("internal error: unknown dwarf2 format"));
1390 /* DWARF version. */
1393 /* .debug_abbrev offset */
1394 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg
), sizeof_offset
);
1396 /* Target address size. */
1397 out_byte (sizeof_address
);
1399 /* DW_TAG_compile_unit DIE abbrev */
1402 /* DW_AT_stmt_list */
1403 /* ??? sizeof_offset */
1404 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg
), 4);
1406 /* These two attributes may only be emitted if all of the code is
1407 contiguous. Multiple sections are not that. */
1408 if (all_segs
->next
== NULL
)
1411 expr
.X_op
= O_symbol
;
1412 expr
.X_add_symbol
= all_segs
->text_start
;
1413 expr
.X_add_number
= 0;
1414 emit_expr (&expr
, sizeof_address
);
1417 expr
.X_op
= O_symbol
;
1418 expr
.X_add_symbol
= all_segs
->text_end
;
1419 expr
.X_add_number
= 0;
1420 emit_expr (&expr
, sizeof_address
);
1423 /* DW_AT_name. We don't have the actual file name that was present
1424 on the command line, so assume files[1] is the main input file.
1425 We're not supposed to get called unless at least one line number
1426 entry was emitted, so this should always be defined. */
1427 if (!files
|| files_in_use
< 1)
1431 len
= strlen (dirs
[files
[1].dir
]);
1432 p
= frag_more (len
+ 1);
1433 memcpy (p
, dirs
[files
[1].dir
], len
);
1436 len
= strlen (files
[1].filename
) + 1;
1437 p
= frag_more (len
);
1438 memcpy (p
, files
[1].filename
, len
);
1440 /* DW_AT_comp_dir */
1441 comp_dir
= getpwd ();
1442 len
= strlen (comp_dir
) + 1;
1443 p
= frag_more (len
);
1444 memcpy (p
, comp_dir
, len
);
1446 /* DW_AT_producer */
1447 sprintf (producer
, "GNU AS %s", VERSION
);
1448 len
= strlen (producer
) + 1;
1449 p
= frag_more (len
);
1450 memcpy (p
, producer
, len
);
1452 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1453 dwarf2 draft has no standard code for assembler. */
1454 out_two (DW_LANG_Mips_Assembler
);
1456 symbol_set_value_now (info_end
);
1459 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1460 were any .file/.loc directives, or --gdwarf2 was given, or if the
1461 file has a non-empty .debug_info section. If we emit .debug_line,
1462 and the .debug_info section is empty, we also emit .debug_info,
1463 .debug_aranges and .debug_abbrev. ALL_SEGS will be non-null if
1464 there were any .file/.loc directives, or --gdwarf2 was given and
1465 there were any located instructions emitted. */
1468 dwarf2_finish (void)
1473 int emit_other_sections
= 0;
1475 info_seg
= bfd_get_section_by_name (stdoutput
, ".debug_info");
1476 emit_other_sections
= info_seg
== NULL
|| !seg_not_empty_p (info_seg
);
1478 if (!all_segs
&& emit_other_sections
)
1479 /* There is no line information and no non-empty .debug_info
1483 /* Calculate the size of an address for the target machine. */
1484 sizeof_address
= DWARF2_ADDR_SIZE (stdoutput
);
1486 /* Create and switch to the line number section. */
1487 line_seg
= subseg_new (".debug_line", 0);
1488 bfd_set_section_flags (stdoutput
, line_seg
, SEC_READONLY
| SEC_DEBUGGING
);
1490 /* For each subsection, chain the debug entries together. */
1491 for (s
= all_segs
; s
; s
= s
->next
)
1493 struct line_subseg
*ss
= s
->head
;
1494 struct line_entry
**ptail
= ss
->ptail
;
1496 while ((ss
= ss
->next
) != NULL
)
1503 out_debug_line (line_seg
);
1505 /* If this is assembler generated line info, and there is no
1506 debug_info already, we need .debug_info and .debug_abbrev
1507 sections as well. */
1508 if (emit_other_sections
)
1515 info_seg
= subseg_new (".debug_info", 0);
1516 abbrev_seg
= subseg_new (".debug_abbrev", 0);
1517 aranges_seg
= subseg_new (".debug_aranges", 0);
1519 bfd_set_section_flags (stdoutput
, info_seg
,
1520 SEC_READONLY
| SEC_DEBUGGING
);
1521 bfd_set_section_flags (stdoutput
, abbrev_seg
,
1522 SEC_READONLY
| SEC_DEBUGGING
);
1523 bfd_set_section_flags (stdoutput
, aranges_seg
,
1524 SEC_READONLY
| SEC_DEBUGGING
);
1526 record_alignment (aranges_seg
, ffs (2 * sizeof_address
) - 1);
1528 out_debug_aranges (aranges_seg
, info_seg
);
1529 out_debug_abbrev (abbrev_seg
);
1530 out_debug_info (info_seg
, abbrev_seg
, line_seg
);