3c223e880fa74a2cdf06b75eff2b3859f3d4b0ee
[deliverable/binutils-gdb.git] / gas / dwarf2dbg.c
1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright (C) 1999 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 This file is part of GAS, the GNU Assembler.
6
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)
10 any later version.
11
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.
16
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
20 02111-1307, USA.
21
22 Logical line numbers can be controlled by the compiler via the
23 following two directives:
24
25 .file FILENO "file.c"
26 .loc FILENO LINENO [COLUMN]
27
28 FILENO is the filenumber. */
29
30 #include "ansidecl.h"
31
32 #include "as.h"
33 #include "dwarf2dbg.h"
34 #include "subsegs.h"
35
36 #include <elf/dwarf2.h>
37
38 #define BYTES_PER_ADDRESS (BFD_ARCH_SIZE / 8)
39
40 /* Since we can't generate the prolog until the body is complete, we
41 use three different subsegments for .debug_line: one holding the
42 prolog, one for the directory and filename info, and one for the
43 body ("statement program"). */
44 #define DL_PROLOG 0
45 #define DL_FILES 1
46 #define DL_BODY 2
47
48 /* First special line opcde - leave room for the standard opcodes.
49 Note: If you want to change this, you'll have to update the
50 "standard_opcode_lengths" table that is emitted below in
51 dwarf2_finish(). */
52 #define DWARF2_LINE_OPCODE_BASE 10
53
54 #ifndef DWARF2_LINE_BASE
55 /* Minimum line offset in a special line info. opcode. This value
56 was chosen to give a reasonable range of values. */
57 # define DWARF2_LINE_BASE -5
58 #endif
59
60 /* Range of line offsets in a special line info. opcode. */
61 #ifndef DWARF2_LINE_RANGE
62 # define DWARF2_LINE_RANGE 14
63 #endif
64
65 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
66 /* Define the architecture-dependent minimum instruction length (in
67 bytes). This value should be rather too small than too big. */
68 # define DWARF2_LINE_MIN_INSN_LENGTH 4
69 #endif
70
71 /* Flag that indicates the initial value of the is_stmt_start flag.
72 In the present implementation, we do not mark any lines as
73 the beginning of a source statement, because that information
74 is not made available by the GCC front-end. */
75 #define DWARF2_LINE_DEFAULT_IS_STMT 1
76
77 /* Flag that indicates the initial value of the is_stmt_start flag.
78 In the present implementation, we do not mark any lines as
79 the beginning of a source statement, because that information
80 is not made available by the GCC front-end. */
81 #define DWARF2_LINE_DEFAULT_IS_STMT 1
82
83 /* Given a special op, return the line skip amount: */
84 #define SPECIAL_LINE(op) \
85 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
86
87 /* Given a special op, return the address skip amount (in units of
88 DWARF2_LINE_MIN_INSN_LENGTH. */
89 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
90
91 /* The maximum address skip amont that can be encoded with a special op: */
92 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
93
94 #define INITIAL_STATE \
95 /* initialize as per DWARF2.0 standard: */ \
96 0, /* address */ \
97 1, /* file */ \
98 1, /* line */ \
99 0, /* column */ \
100 DWARF2_LINE_DEFAULT_IS_STMT, /* is_stmt */ \
101 0, /* basic_block */ \
102 1 /* empty_sequence */
103
104 static struct
105 {
106 /* state machine state as per DWARF2 manual: */
107 struct dwarf2_sm
108 {
109 bfd_vma addr;
110 unsigned int filenum;
111 unsigned int line;
112 unsigned int column;
113 unsigned int
114 is_stmt : 1,
115 basic_block : 1,
116 empty_sequence : 1; /* current code sequence has no DWARF2 directives? */
117 }
118 sm;
119
120 unsigned int
121 any_dwarf2_directives : 1; /* did we emit any DWARF2 line debug directives? */
122
123 segT text_seg; /* text segment "addr" is relative to */
124 subsegT text_subseg;
125 segT line_seg; /* ".debug_line" segment */
126 int last_filename; /* index of last filename that was used */
127 int num_filenames; /* index of last filename in use */
128 int filename_len; /* length of the filename array */
129 struct
130 {
131 int dir; /* valid after gen_dir_list() only */
132 char *name; /* full path before gen_dir_list(), filename afterwards */
133 }
134 *file;
135
136 struct dwarf2_line_info current; /* current source info: */
137
138 /* counters for statistical purposes: */
139 unsigned int num_line_entries;
140 unsigned int opcode_hist[256]; /* histogram of opcode frequencies */
141 }
142 ls =
143 {
144 {
145 INITIAL_STATE
146 },
147 };
148
149 #define out_byte(byte) FRAG_APPEND_1_CHAR(byte)
150 #define out_opcode(opc) (out_byte ((opc)), ++ls.opcode_hist[(opc) & 0xff])
151
152 /* Output an unsigned "little-endian base 128" number. */
153 static void
154 out_uleb128 (bfd_vma value)
155 {
156 unsigned char byte, more = 0x80;
157
158 do
159 {
160 byte = value & 0x7f;
161 value >>= 7;
162 if (value == 0)
163 more = 0;
164 out_byte (more | byte);
165 }
166 while (more);
167 }
168
169 /* Output a signed "little-endian base 128" number. */
170 static void
171 out_sleb128 (bfd_signed_vma value)
172 {
173 unsigned char byte, more = 0x80;
174
175 do
176 {
177 byte = value & 0x7f;
178 value >>= 7;
179 if (((value == 0) && ((byte & 0x40) == 0))
180 || ((value == -1) && ((byte & 0x40) != 0)))
181 more = 0;
182 out_byte (more | byte);
183 }
184 while (more);
185 }
186
187 /* Encode a pair of line and address skips as efficiently as possible.
188 Note that the line skip is signed, whereas the address skip is
189 unsigned. */
190 static void
191 gen_addr_line (int line_delta, bfd_vma addr_delta)
192 {
193 unsigned int tmp, opcode;
194
195 tmp = line_delta - DWARF2_LINE_BASE;
196
197 if (tmp >= DWARF2_LINE_RANGE)
198 {
199 out_opcode (DW_LNS_advance_line);
200 out_sleb128 (line_delta);
201 tmp = 0 - DWARF2_LINE_BASE;
202 line_delta = 0;
203 }
204
205 tmp += DWARF2_LINE_OPCODE_BASE;
206
207 /* try using a special opcode: */
208 opcode = tmp + addr_delta*DWARF2_LINE_RANGE;
209 if (opcode <= 255)
210 {
211 out_opcode (opcode);
212 return;
213 }
214
215 /* try using DW_LNS_const_add_pc followed by special op: */
216 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA)*DWARF2_LINE_RANGE;
217 if (opcode <= 255)
218 {
219 out_opcode (DW_LNS_const_add_pc);
220 out_opcode (opcode);
221 return;
222 }
223
224 out_opcode (DW_LNS_advance_pc);
225 out_uleb128 (addr_delta);
226
227 if (line_delta)
228 out_opcode (tmp); /* output line-delta */
229 else
230 out_opcode (DW_LNS_copy); /* append new row with current info */
231 }
232
233 static void
234 reset_state_machine (void)
235 {
236 static const struct dwarf2_sm initial_state = { INITIAL_STATE };
237
238 ls.sm = initial_state;
239 }
240
241 /* Set an absolute address (may results in a relocation entry): */
242 static void
243 out_set_addr (bfd_vma addr)
244 {
245 subsegT saved_subseg;
246 segT saved_seg;
247 expressionS expr;
248 symbolS *sym;
249
250 saved_seg = now_seg;
251 saved_subseg = now_subseg;
252
253 subseg_set (ls.text_seg, ls.text_subseg);
254 sym = symbol_new (".L0\001", now_seg, addr, frag_now);
255
256 subseg_set (saved_seg, saved_subseg);
257
258 out_opcode (DW_LNS_extended_op);
259 out_uleb128 (BYTES_PER_ADDRESS + 1);
260
261 out_opcode (DW_LNE_set_address);
262 expr.X_op = O_symbol;
263 expr.X_add_symbol = sym;
264 expr.X_add_number = 0;
265 emit_expr (&expr, BYTES_PER_ADDRESS);
266 }
267
268 /* Emit DW_LNS_end_sequence and reset state machine. Does not
269 preserve the current segment/sub-segment! */
270 static void
271 out_end_sequence (void)
272 {
273 bfd_vma addr, delta;
274
275 if (ls.text_seg)
276 {
277 subseg_set (ls.text_seg, ls.text_subseg);
278 #ifdef md_current_text_addr
279 addr = md_current_text_addr ();
280 #else
281 addr = frag_now_fix ();
282 #endif
283 subseg_set (ls.line_seg, DL_BODY);
284 if (addr < ls.sm.addr)
285 {
286 out_set_addr (addr);
287 ls.sm.addr = addr;
288 }
289 else
290 {
291 delta = addr - ls.sm.addr;
292 if (delta > 0)
293 gen_addr_line (0, delta / DWARF2_LINE_MIN_INSN_LENGTH);
294 }
295 }
296 else
297 subseg_set (ls.line_seg, DL_BODY);
298
299 out_opcode (DW_LNS_extended_op);
300 out_uleb128 (1);
301 out_byte (DW_LNE_end_sequence);
302
303 reset_state_machine ();
304 }
305
306 /* Look up a filenumber either by filename or by filenumber. If both
307 a filenumber and a filename are specified, lookup by filename takes
308 precedence. If the filename cannot be found, it is added to the
309 filetable the filenumber for the new entry is returned. */
310 static int
311 get_filenum (int filenum, char *file)
312 {
313 int i, last = filenum - 1;
314 char char0 = file[0];
315
316 if ((unsigned) last >= ls.num_filenames)
317 last = ls.last_filename;
318
319 /* do a quick check against the previously used filename: */
320 if (ls.num_filenames > 0 && ls.file[last].name[0] == char0
321 && strcmp (ls.file[last].name + 1, file + 1) == 0)
322 return last + 1;
323
324 /* no match, fall back to simple linear scan: */
325 for (i = 0; i < ls.num_filenames; ++i)
326 {
327 if (ls.file[i].name[0] == char0
328 && strcmp (ls.file[i].name + 1, file + 1) == 0)
329 {
330 ls.last_filename = i;
331 return i + 1;
332 }
333 }
334
335 /* no match: enter new filename */
336 if (ls.num_filenames >= ls.filename_len)
337 {
338 ls.filename_len += 13;
339 ls.file = xrealloc (ls.file, ls.filename_len * sizeof (ls.file[0]));
340 }
341 ls.file[ls.num_filenames].dir = 0;
342 ls.file[ls.num_filenames].name = file;
343 return ++ls.num_filenames;
344 }
345
346 void
347 dwarf2_gen_line_info (bfd_vma addr, struct dwarf2_line_info *l)
348 {
349 unsigned int filenum = l->filenum;
350 unsigned int any_output = 0;
351 subsegT saved_subseg;
352 segT saved_seg;
353 char *frag;
354
355 if (flag_debug)
356 fprintf (stderr, "line: addr %llx file `%s' line %u col %u flags %lx\n",
357 (long long) addr, l->filename, l->line, l->column, l->flags);
358
359 if (filenum > 0 && !l->filename)
360 {
361 if (filenum >= ls.num_filenames)
362 {
363 as_warn ("Encountered bad file number in line number debug info!");
364 return;
365 }
366 }
367 else if (l->filename)
368 filenum = get_filenum (filenum, l->filename);
369 else
370 return; /* no filename, no filnum => no play */
371
372 if (!ls.line_seg)
373 {
374 ls.line_seg = subseg_new (".debug_line", 0);
375 bfd_set_section_flags (stdoutput, ls.line_seg, SEC_READONLY);
376
377 /* We're going to need this symbol. */
378 (void) section_symbol (ls.line_seg);
379 }
380
381 saved_seg = now_seg;
382 saved_subseg = now_subseg;
383 subseg_set (ls.line_seg, DL_BODY);
384
385 if (ls.text_seg != saved_seg || ls.text_subseg != saved_subseg)
386 {
387 if (!ls.sm.empty_sequence)
388 {
389 out_end_sequence (); /* terminate previous sequence */
390 ls.sm.empty_sequence = 1;
391 }
392 any_output = 1;
393 ls.text_seg = saved_seg;
394 ls.text_subseg = saved_subseg;
395 out_set_addr (addr);
396 ls.sm.addr = addr;
397 }
398
399 if (ls.sm.filenum != filenum)
400 {
401 any_output = 1;
402 out_opcode (DW_LNS_set_file);
403 out_uleb128 (filenum);
404 ls.sm.filenum = filenum;
405 }
406
407 if (ls.sm.column != l->column)
408 {
409 any_output = 1;
410 out_opcode (DW_LNS_set_column);
411 out_uleb128 (l->column);
412 ls.sm.column = l->column;
413 }
414
415 if (((l->flags & DWARF2_FLAG_BEGIN_STMT) != 0) != ls.sm.is_stmt)
416 {
417 any_output = 1;
418 out_opcode (DW_LNS_negate_stmt);
419 }
420
421 if (l->flags & DWARF2_FLAG_BEGIN_BLOCK)
422 {
423 any_output = 1;
424 out_opcode (DW_LNS_set_basic_block);
425 }
426
427 if (ls.sm.line != l->line)
428 {
429 any_output = 1;
430 if (addr < ls.sm.addr)
431 {
432 if (!ls.sm.empty_sequence)
433 {
434 out_end_sequence ();
435 ls.sm.empty_sequence = 1;
436 }
437 out_set_addr (addr);
438 ls.sm.addr = addr;
439 }
440 gen_addr_line (l->line - ls.sm.line,
441 (addr - ls.sm.addr) / DWARF2_LINE_MIN_INSN_LENGTH);
442 ls.sm.basic_block = 0;
443 ls.sm.line = l->line;
444 ls.sm.addr = addr;
445 }
446
447 subseg_set (saved_seg, saved_subseg);
448
449 ls.num_line_entries += any_output;
450 if (any_output)
451 ls.sm.empty_sequence = 0;
452 }
453
454 static void
455 gen_dir_list (void)
456 {
457 char *str, *slash, *dir_list, *dp, *cp;
458 int i, j, num_dirs;
459
460 dir_list = frag_more (0);
461 num_dirs = 0;
462
463 for (i = 0; i < ls.num_filenames; ++i)
464 {
465 str = ls.file[i].name;
466 slash = strrchr (str, '/');
467 if (slash)
468 {
469 *slash = '\0';
470 for (j = 0, dp = dir_list; j < num_dirs; ++j)
471 {
472 if (strcmp (str, dp) == 0)
473 {
474 ls.file[i].dir = j;
475 break;
476 }
477 dp += strlen (dp);
478 }
479 if (j >= num_dirs)
480 {
481 /* didn't find this directory: append it to the list */
482 size_t size = strlen (str) + 1;
483 cp = frag_more (size);
484 memcpy (cp, str, size);
485 ls.file[i].dir = ++num_dirs;
486 }
487 *slash = '/';
488 ls.file[i].name = slash + 1;
489 }
490 }
491 out_byte ('\0'); /* terminate directory list */
492 }
493
494 static void
495 gen_file_list (void)
496 {
497 size_t size;
498 char *cp;
499 int i;
500
501 for (i = 0; i < ls.num_filenames; ++i)
502 {
503 size = strlen (ls.file[i].name) + 1;
504 cp = frag_more (size);
505 memcpy (cp, ls.file[i].name, size);
506
507 out_uleb128 (ls.file[i].dir); /* directory number */
508 out_uleb128 (0); /* last modification timestamp */
509 out_uleb128 (0); /* filesize */
510 }
511 out_byte (0); /* terminate filename list */
512 }
513
514 void
515 print_stats (unsigned long total_size)
516 {
517 static const char *opc_name[] =
518 {
519 "extended", "copy", "advance_pc", "advance_line", "set_file",
520 "set_column", "negate_stmt", "set_basic_block", "const_add_pc",
521 "fixed_advance_pc"
522 };
523 int i, j;
524
525 fprintf (stderr, "Average size: %g bytes/line\n",
526 total_size / (double) ls.num_line_entries);
527
528 fprintf (stderr, "\nStandard opcode histogram:\n");
529
530 for (i = 0; i < sizeof (opc_name)/sizeof (opc_name[0]); ++i)
531 {
532 fprintf (stderr, "%s", opc_name[i]);
533 for (j = strlen (opc_name[i]); j < 16; ++j)
534 fprintf (stderr, " ");
535 fprintf (stderr, ": %u\n", ls.opcode_hist[i]);
536 }
537
538 fprintf (stderr, "\nSpecial opcodes:\naddr\t\t\t\tline skip\n");
539
540 fprintf (stderr, "skip: ");
541 for (j = DWARF2_LINE_BASE; j < DWARF2_LINE_BASE + DWARF2_LINE_RANGE; ++j)
542 fprintf (stderr, "%3d", j);
543 fprintf (stderr, "\n-----");
544
545 for (; i < 256; ++i)
546 {
547 j = SPECIAL_LINE (i);
548 if (j == DWARF2_LINE_BASE)
549 fprintf (stderr, "\n%4u: ",
550 DWARF2_LINE_MIN_INSN_LENGTH*SPECIAL_ADDR (i));
551 fprintf (stderr, " %2u", ls.opcode_hist[i]);
552 }
553 fprintf (stderr, "\n");
554 }
555
556 void
557 dwarf2_finish (void)
558 {
559 bfd_vma addr, body_size, total_size, prolog_size;
560 subsegT saved_subseg, line_prolog;
561 segT saved_seg;
562 char *cp;
563
564 if (!ls.line_seg)
565 /* no .debug_line segment, no work to do... */
566 return;
567
568 saved_seg = now_seg;
569 saved_subseg = now_subseg;
570
571 if (!ls.sm.empty_sequence)
572 out_end_sequence ();
573 total_size = body_size = frag_now_fix ();
574
575 /* now generate the directory and file lists: */
576 subseg_set (ls.line_seg, DL_FILES);
577 gen_dir_list ();
578 gen_file_list ();
579 total_size += frag_now_fix ();
580
581 /* and now the header ("statement program prolog", in DWARF2 lingo...) */
582 subseg_set (ls.line_seg, DL_PROLOG);
583
584 cp = frag_more (15 + DWARF2_LINE_OPCODE_BASE - 1);
585
586 total_size += frag_now_fix ();
587 prolog_size = total_size - body_size - 10;
588
589 # define STUFF(val,size) md_number_to_chars (cp, val, size); cp += size;
590 STUFF (total_size - 4, 4); /* length */
591 STUFF (2, 2); /* version */
592 STUFF (prolog_size, 4); /* prologue_length */
593 STUFF (DWARF2_LINE_MIN_INSN_LENGTH, 1);
594 STUFF (DWARF2_LINE_DEFAULT_IS_STMT, 1);
595 STUFF (DWARF2_LINE_BASE, 1);
596 STUFF (DWARF2_LINE_RANGE, 1);
597 STUFF (DWARF2_LINE_OPCODE_BASE, 1);
598
599 /* standard_opcode_lengths: */
600 STUFF (0, 1); /* DW_LNS_copy */
601 STUFF (1, 1); /* DW_LNS_advance_pc */
602 STUFF (1, 1); /* DW_LNS_advance_line */
603 STUFF (1, 1); /* DW_LNS_set_file */
604 STUFF (1, 1); /* DW_LNS_set_column */
605 STUFF (0, 1); /* DW_LNS_negate_stmt */
606 STUFF (0, 1); /* DW_LNS_set_basic_block */
607 STUFF (0, 1); /* DW_LNS_const_add_pc */
608 STUFF (1, 1); /* DW_LNS_fixed_advance_pc */
609
610 subseg_set (saved_seg, saved_subseg);
611
612 if (flag_debug)
613 print_stats (total_size);
614 }
615
616 void
617 dwarf2_directive_file (int dummy)
618 {
619 int len;
620
621 ls.any_dwarf2_directives = 1;
622
623 if (debug_type == DEBUG_NONE)
624 /* Automatically turn on DWARF2 debug info unless something else
625 has been selected. */
626 debug_type = DEBUG_DWARF2;
627
628 ls.current.filenum = get_absolute_expression ();
629 ls.current.filename = demand_copy_C_string (&len);
630
631 demand_empty_rest_of_line ();
632 }
633
634 void
635 dwarf2_directive_loc (int dummy)
636 {
637 ls.any_dwarf2_directives = 1;
638
639 ls.current.filenum = get_absolute_expression ();
640 SKIP_WHITESPACE ();
641 ls.current.line = get_absolute_expression ();
642 SKIP_WHITESPACE ();
643 ls.current.column = get_absolute_expression ();
644 demand_empty_rest_of_line ();
645
646 ls.current.flags = DWARF2_FLAG_BEGIN_STMT;
647
648 #ifndef NO_LISTING
649 if (listing)
650 listing_source_line (ls.current.line);
651 #endif
652 }
653
654 void
655 dwarf2_where (struct dwarf2_line_info *line)
656 {
657 if (ls.any_dwarf2_directives)
658 *line = ls.current;
659 else
660 {
661 char *filename;
662
663 as_where (&line->filename, &line->line);
664 line->filenum = 0;
665 line->column = 0;
666 line->flags = DWARF2_FLAG_BEGIN_STMT;
667 }
668 }
This page took 0.05648 seconds and 3 git commands to generate.