1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright (C) 1999 Free Software Foundation, Inc.
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* Logical line numbers can be controlled by the compiler via the
23 following two directives:
26 .loc FILENO LINENO [COLUMN]
28 FILENO is the filenumber. */
33 #include "dwarf2dbg.h"
36 #include "elf/dwarf2.h"
38 /* Since we can't generate the prolog until the body is complete, we
39 use three different subsegments for .debug_line: one holding the
40 prolog, one for the directory and filename info, and one for the
41 body ("statement program"). */
46 /* First special line opcde - leave room for the standard opcodes.
47 Note: If you want to change this, you'll have to update the
48 "standard_opcode_lengths" table that is emitted below in
50 #define DWARF2_LINE_OPCODE_BASE 10
52 #ifndef DWARF2_LINE_BASE
53 /* Minimum line offset in a special line info. opcode. This value
54 was chosen to give a reasonable range of values. */
55 # define DWARF2_LINE_BASE -5
58 /* Range of line offsets in a special line info. opcode. */
59 #ifndef DWARF2_LINE_RANGE
60 # define DWARF2_LINE_RANGE 14
63 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
64 /* Define the architecture-dependent minimum instruction length (in
65 bytes). This value should be rather too small than too big. */
66 # define DWARF2_LINE_MIN_INSN_LENGTH 4
69 /* Flag that indicates the initial value of the is_stmt_start flag.
70 In the present implementation, we do not mark any lines as
71 the beginning of a source statement, because that information
72 is not made available by the GCC front-end. */
73 #define DWARF2_LINE_DEFAULT_IS_STMT 1
75 /* Flag that indicates the initial value of the is_stmt_start flag.
76 In the present implementation, we do not mark any lines as
77 the beginning of a source statement, because that information
78 is not made available by the GCC front-end. */
79 #define DWARF2_LINE_DEFAULT_IS_STMT 1
81 /* Given a special op, return the line skip amount: */
82 #define SPECIAL_LINE(op) \
83 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
85 /* Given a special op, return the address skip amount (in units of
86 DWARF2_LINE_MIN_INSN_LENGTH. */
87 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
89 /* The maximum address skip amont that can be encoded with a special op: */
90 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
92 #define INITIAL_STATE \
93 /* initialize as per DWARF2.0 standard: */ \
98 DWARF2_LINE_DEFAULT_IS_STMT, /* is_stmt */ \
99 0, /* basic_block */ \
100 1 /* empty_sequence */
104 /* state machine state as per DWARF2 manual: */
108 unsigned int filenum
;
114 empty_sequence
: 1; /* current code sequence has no DWARF2 directives? */
119 any_dwarf2_directives
: 1; /* did we emit any DWARF2 line debug directives? */
121 segT text_seg
; /* text segment "addr" is relative to */
123 segT line_seg
; /* ".debug_line" segment */
124 int last_filename
; /* index of last filename that was used */
125 int num_filenames
; /* index of last filename in use */
126 int filename_len
; /* length of the filename array */
129 int dir
; /* valid after gen_dir_list() only */
130 char *name
; /* full path before gen_dir_list(), filename afterwards */
134 struct dwarf2_line_info current
; /* current source info: */
136 /* counters for statistical purposes: */
137 unsigned int num_line_entries
;
138 unsigned int opcode_hist
[256]; /* histogram of opcode frequencies */
153 { NULL
, 0, 0, 0, 0 },
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
157 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
161 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
162 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
166 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
167 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
168 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
176 /* Function prototypes: */
177 static void out_uleb128
PARAMS ((addressT
));
178 static void out_sleb128
PARAMS ((offsetT
));
179 static void gen_addr_line
PARAMS ((int, addressT
));
180 static void reset_state_machine
PARAMS ((void));
181 static void out_set_addr
PARAMS ((addressT
));
182 static void out_end_sequence
PARAMS ((void));
183 static int get_filenum
PARAMS ((int, char *));
184 static void gen_dir_list
PARAMS ((void));
185 static void gen_file_list
PARAMS ((void));
186 static void print_stats
PARAMS ((unsigned long));
189 #define out_byte(byte) FRAG_APPEND_1_CHAR(byte)
190 #define out_opcode(opc) (out_byte ((opc)), ++ls.opcode_hist[(opc) & 0xff])
192 /* Output an unsigned "little-endian base 128" number. */
197 unsigned char byte
, more
= 0x80;
205 out_byte (more
| byte
);
210 /* Output a signed "little-endian base 128" number. */
215 unsigned char byte
, more
= 0x80;
221 if (((value
== 0) && ((byte
& 0x40) == 0))
222 || ((value
== -1) && ((byte
& 0x40) != 0)))
224 out_byte (more
| byte
);
229 /* Encode a pair of line and address skips as efficiently as possible.
230 Note that the line skip is signed, whereas the address skip is
233 gen_addr_line (line_delta
, addr_delta
)
237 unsigned int tmp
, opcode
;
239 tmp
= line_delta
- DWARF2_LINE_BASE
;
241 if (tmp
>= DWARF2_LINE_RANGE
)
243 out_opcode (DW_LNS_advance_line
);
244 out_sleb128 (line_delta
);
245 tmp
= 0 - DWARF2_LINE_BASE
;
249 tmp
+= DWARF2_LINE_OPCODE_BASE
;
251 /* try using a special opcode: */
252 opcode
= tmp
+ addr_delta
*DWARF2_LINE_RANGE
;
259 /* try using DW_LNS_const_add_pc followed by special op: */
260 opcode
= tmp
+ (addr_delta
- MAX_SPECIAL_ADDR_DELTA
)*DWARF2_LINE_RANGE
;
263 out_opcode (DW_LNS_const_add_pc
);
268 out_opcode (DW_LNS_advance_pc
);
269 out_uleb128 (addr_delta
);
272 out_opcode (tmp
); /* output line-delta */
274 out_opcode (DW_LNS_copy
); /* append new row with current info */
278 reset_state_machine ()
280 static const struct dwarf2_sm initial_state
= { INITIAL_STATE
};
282 ls
.sm
= initial_state
;
285 /* Set an absolute address (may results in a relocation entry): */
290 subsegT saved_subseg
;
294 int bytes_per_address
;
297 saved_subseg
= now_subseg
;
299 subseg_set (ls
.text_seg
, ls
.text_subseg
);
300 sym
= symbol_new (".L0\001", now_seg
, addr
, frag_now
);
302 subseg_set (saved_seg
, saved_subseg
);
305 bytes_per_address
= bfd_arch_bits_per_address (stdoutput
) / 8;
308 bytes_per_address
= 4;
311 out_opcode (DW_LNS_extended_op
);
312 out_uleb128 (bytes_per_address
+ 1);
314 out_opcode (DW_LNE_set_address
);
315 expr
.X_op
= O_symbol
;
316 expr
.X_add_symbol
= sym
;
317 expr
.X_add_number
= 0;
318 emit_expr (&expr
, bytes_per_address
);
321 /* Emit DW_LNS_end_sequence and reset state machine. Does not
322 preserve the current segment/sub-segment! */
326 addressT addr
, delta
;
330 subseg_set (ls
.text_seg
, ls
.text_subseg
);
331 #ifdef md_current_text_addr
332 addr
= md_current_text_addr ();
334 addr
= frag_now_fix ();
336 subseg_set (ls
.line_seg
, DL_BODY
);
337 if (addr
< ls
.sm
.addr
)
344 delta
= addr
- ls
.sm
.addr
;
346 gen_addr_line (0, delta
/ DWARF2_LINE_MIN_INSN_LENGTH
);
350 subseg_set (ls
.line_seg
, DL_BODY
);
352 out_opcode (DW_LNS_extended_op
);
354 out_byte (DW_LNE_end_sequence
);
356 reset_state_machine ();
359 /* Look up a filenumber either by filename or by filenumber. If both
360 a filenumber and a filename are specified, lookup by filename takes
361 precedence. If the filename cannot be found, it is added to the
362 filetable and the filenumber for the new entry is returned. */
364 get_filenum (filenum
, file
)
368 int i
, last
= filenum
- 1;
369 char char0
= file
[0];
371 /* If filenum is out of range of the filename table, then try using the
372 table entry returned from the previous call. */
373 if (last
>= ls
.num_filenames
|| last
< 0)
374 last
= ls
.last_filename
;
376 /* Do a quick check against the specified or previously used filenum. */
377 if (ls
.num_filenames
> 0 && ls
.file
[last
].name
[0] == char0
378 && strcmp (ls
.file
[last
].name
+ 1, file
+ 1) == 0)
381 /* no match, fall back to simple linear scan: */
382 for (i
= 0; i
< ls
.num_filenames
; ++i
)
384 if (ls
.file
[i
].name
[0] == char0
385 && strcmp (ls
.file
[i
].name
+ 1, file
+ 1) == 0)
387 ls
.last_filename
= i
;
392 /* no match: enter new filename */
393 if (ls
.num_filenames
>= ls
.filename_len
)
395 ls
.filename_len
+= 13;
396 ls
.file
= xrealloc (ls
.file
, ls
.filename_len
* sizeof (ls
.file
[0]));
398 ls
.file
[ls
.num_filenames
].dir
= 0;
399 ls
.file
[ls
.num_filenames
].name
= file
;
400 ls
.last_filename
= ls
.num_filenames
;
401 return ++ls
.num_filenames
;
405 dwarf2_gen_line_info (addr
, l
)
407 struct dwarf2_line_info
*l
;
409 unsigned int filenum
= l
->filenum
;
410 unsigned int any_output
= 0;
411 subsegT saved_subseg
;
415 fprintf (stderr
, "line: addr %lx file `%s' line %u col %u flags %x\n",
416 (unsigned long) addr
, l
->filename
, l
->line
, l
->column
, l
->flags
);
418 if (filenum
> 0 && !l
->filename
)
420 if (filenum
>= (unsigned int) ls
.num_filenames
)
422 as_warn ("Encountered bad file number in line number debug info!");
426 else if (l
->filename
)
427 filenum
= get_filenum (filenum
, l
->filename
);
429 return; /* no filename, no filnum => no play */
431 /* Must save these before the subseg_new call, as that call will change
434 saved_subseg
= now_subseg
;
442 ls
.line_seg
= subseg_new (".debug_line", 0);
445 bfd_set_section_flags (stdoutput
, ls
.line_seg
, SEC_READONLY
);
447 /* We're going to need this symbol. */
448 secsym
= symbol_find (".debug_line");
450 symbol_set_bfdsym (secsym
, ls
.line_seg
->symbol
);
452 symbol_table_insert (section_symbol (ls
.line_seg
));
456 subseg_set (ls
.line_seg
, DL_BODY
);
458 if (ls
.text_seg
!= saved_seg
|| ls
.text_subseg
!= saved_subseg
)
460 if (!ls
.sm
.empty_sequence
)
462 out_end_sequence (); /* terminate previous sequence */
463 ls
.sm
.empty_sequence
= 1;
466 ls
.text_seg
= saved_seg
;
467 ls
.text_subseg
= saved_subseg
;
472 if (ls
.sm
.filenum
!= filenum
)
475 out_opcode (DW_LNS_set_file
);
476 out_uleb128 (filenum
);
477 ls
.sm
.filenum
= filenum
;
480 if (ls
.sm
.column
!= l
->column
)
483 out_opcode (DW_LNS_set_column
);
484 out_uleb128 (l
->column
);
485 ls
.sm
.column
= l
->column
;
488 if (((l
->flags
& DWARF2_FLAG_BEGIN_STMT
) != 0) != ls
.sm
.is_stmt
)
491 out_opcode (DW_LNS_negate_stmt
);
494 if (l
->flags
& DWARF2_FLAG_BEGIN_BLOCK
)
497 out_opcode (DW_LNS_set_basic_block
);
500 if (ls
.sm
.line
!= l
->line
)
503 if (addr
< ls
.sm
.addr
)
505 /* This happens when a new frag got allocated (for whatever
506 reason). Deal with it by generating a reference symbol.
507 Note: no end_sequence needs to be generated because the
508 address did not really decrease (only the reference point
511 ??? Perhaps we should directly check for a change of
516 gen_addr_line (l
->line
- ls
.sm
.line
,
517 (addr
- ls
.sm
.addr
) / DWARF2_LINE_MIN_INSN_LENGTH
);
518 ls
.sm
.basic_block
= 0;
519 ls
.sm
.line
= l
->line
;
523 subseg_set (saved_seg
, saved_subseg
);
525 ls
.num_line_entries
+= any_output
;
527 ls
.sm
.empty_sequence
= 0;
533 char *str
, *slash
, *dir_list
, *dp
, *cp
;
536 dir_list
= frag_more (0);
539 for (i
= 0; i
< ls
.num_filenames
; ++i
)
541 str
= ls
.file
[i
].name
;
542 slash
= strrchr (str
, '/');
546 for (j
= 0, dp
= dir_list
; j
< num_dirs
; ++j
)
548 if (strcmp (str
, dp
) == 0)
550 ls
.file
[i
].dir
= j
+ 1;
557 /* didn't find this directory: append it to the list */
558 size_t size
= strlen (str
) + 1;
559 cp
= frag_more (size
);
560 memcpy (cp
, str
, size
);
561 ls
.file
[i
].dir
= ++num_dirs
;
564 ls
.file
[i
].name
= slash
+ 1;
567 out_byte ('\0'); /* terminate directory list */
577 for (i
= 0; i
< ls
.num_filenames
; ++i
)
579 size
= strlen (ls
.file
[i
].name
) + 1;
580 cp
= frag_more (size
);
581 memcpy (cp
, ls
.file
[i
].name
, size
);
583 out_uleb128 (ls
.file
[i
].dir
); /* directory number */
584 out_uleb128 (0); /* last modification timestamp */
585 out_uleb128 (0); /* filesize */
587 out_byte (0); /* terminate filename list */
591 print_stats (total_size
)
592 unsigned long total_size
;
594 static const char *opc_name
[] =
596 "extended", "copy", "advance_pc", "advance_line", "set_file",
597 "set_column", "negate_stmt", "set_basic_block", "const_add_pc",
603 fprintf (stderr
, "Average size: %g bytes/line\n",
604 total_size
/ (double) ls
.num_line_entries
);
606 fprintf (stderr
, "\nStandard opcode histogram:\n");
608 for (i
= 0; i
< sizeof (opc_name
)/sizeof (opc_name
[0]); ++i
)
610 fprintf (stderr
, "%s", opc_name
[i
]);
611 for (j
= strlen (opc_name
[i
]); j
< 16; ++j
)
612 fprintf (stderr
, " ");
613 fprintf (stderr
, ": %u\n", ls
.opcode_hist
[i
]);
616 fprintf (stderr
, "\nSpecial opcodes:\naddr\t\t\t\tline skip\n");
618 fprintf (stderr
, "skip: ");
619 for (j
= DWARF2_LINE_BASE
; j
< DWARF2_LINE_BASE
+ DWARF2_LINE_RANGE
; ++j
)
620 fprintf (stderr
, "%3d", j
);
621 fprintf (stderr
, "\n-----");
625 j
= SPECIAL_LINE (i
);
626 if (j
== DWARF2_LINE_BASE
)
627 fprintf (stderr
, "\n%4u: ",
628 DWARF2_LINE_MIN_INSN_LENGTH
*SPECIAL_ADDR (i
));
629 fprintf (stderr
, " %2u", ls
.opcode_hist
[i
]);
631 fprintf (stderr
, "\n");
637 addressT body_size
, total_size
, prolog_size
;
638 subsegT saved_subseg
;
643 /* no .debug_line segment, no work to do... */
647 saved_subseg
= now_subseg
;
649 if (!ls
.sm
.empty_sequence
)
651 total_size
= body_size
= frag_now_fix ();
653 /* now generate the directory and file lists: */
654 subseg_set (ls
.line_seg
, DL_FILES
);
657 total_size
+= frag_now_fix ();
659 /* and now the header ("statement program prolog", in DWARF2 lingo...) */
660 subseg_set (ls
.line_seg
, DL_PROLOG
);
662 cp
= frag_more (15 + DWARF2_LINE_OPCODE_BASE
- 1);
664 total_size
+= frag_now_fix ();
665 prolog_size
= total_size
- body_size
- 10;
667 # define STUFF(val,size) md_number_to_chars (cp, val, size); cp += size;
668 STUFF (total_size
- 4, 4); /* length */
669 STUFF (2, 2); /* version */
670 STUFF (prolog_size
, 4); /* prologue_length */
671 STUFF (DWARF2_LINE_MIN_INSN_LENGTH
, 1);
672 STUFF (DWARF2_LINE_DEFAULT_IS_STMT
, 1);
673 STUFF (DWARF2_LINE_BASE
, 1);
674 STUFF (DWARF2_LINE_RANGE
, 1);
675 STUFF (DWARF2_LINE_OPCODE_BASE
, 1);
677 /* standard_opcode_lengths: */
678 STUFF (0, 1); /* DW_LNS_copy */
679 STUFF (1, 1); /* DW_LNS_advance_pc */
680 STUFF (1, 1); /* DW_LNS_advance_line */
681 STUFF (1, 1); /* DW_LNS_set_file */
682 STUFF (1, 1); /* DW_LNS_set_column */
683 STUFF (0, 1); /* DW_LNS_negate_stmt */
684 STUFF (0, 1); /* DW_LNS_set_basic_block */
685 STUFF (0, 1); /* DW_LNS_const_add_pc */
686 STUFF (1, 1); /* DW_LNS_fixed_advance_pc */
688 subseg_set (saved_seg
, saved_subseg
);
691 print_stats (total_size
);
695 dwarf2_directive_file (dummy
)
696 int dummy ATTRIBUTE_UNUSED
;
700 /* Continue to accept a bare string and pass it off. */
702 if (*input_line_pointer
== '"')
708 ls
.any_dwarf2_directives
= 1;
710 if (debug_type
== DEBUG_NONE
)
711 /* Automatically turn on DWARF2 debug info unless something else
712 has been selected. */
713 debug_type
= DEBUG_DWARF2
;
715 ls
.current
.filenum
= get_absolute_expression ();
716 ls
.current
.filename
= demand_copy_C_string (&len
);
718 demand_empty_rest_of_line ();
722 dwarf2_directive_loc (dummy
)
723 int dummy ATTRIBUTE_UNUSED
;
725 ls
.any_dwarf2_directives
= 1;
727 ls
.current
.filenum
= get_absolute_expression ();
729 ls
.current
.line
= get_absolute_expression ();
731 ls
.current
.column
= get_absolute_expression ();
732 demand_empty_rest_of_line ();
734 ls
.current
.flags
= DWARF2_FLAG_BEGIN_STMT
;
738 listing_source_line (ls
.current
.line
);
744 struct dwarf2_line_info
*line
;
746 if (ls
.any_dwarf2_directives
)
750 as_where (&line
->filename
, &line
->line
);
753 line
->flags
= DWARF2_FLAG_BEGIN_STMT
;