* README-vms: Delete.
[deliverable/binutils-gdb.git] / gas / dwarf2dbg.c
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>
5
6 This file is part of GAS, the GNU Assembler.
7
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)
11 any later version.
12
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.
17
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
21 02110-1301, USA. */
22
23 /* Logical line numbers can be controlled by the compiler via the
24 following two directives:
25
26 .file FILENO "file.c"
27 .loc FILENO LINENO [COLUMN]
28
29 FILENO is the filenumber. */
30
31 #include "ansidecl.h"
32 #include "as.h"
33
34 #ifdef HAVE_LIMITS_H
35 #include <limits.h>
36 #else
37 #ifdef HAVE_SYS_PARAM_H
38 #include <sys/param.h>
39 #endif
40 #ifndef INT_MAX
41 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
42 #endif
43 #endif
44
45 #include "dwarf2dbg.h"
46 #include <filenames.h>
47
48 #ifndef DWARF2_FORMAT
49 # define DWARF2_FORMAT() dwarf2_format_32bit
50 #endif
51
52 #ifndef DWARF2_ADDR_SIZE
53 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
54 #endif
55
56 #include "subsegs.h"
57
58 #include "elf/dwarf2.h"
59
60 /* Since we can't generate the prolog until the body is complete, we
61 use three different subsegments for .debug_line: one holding the
62 prolog, one for the directory and filename info, and one for the
63 body ("statement program"). */
64 #define DL_PROLOG 0
65 #define DL_FILES 1
66 #define DL_BODY 2
67
68 /* First special line opcde - leave room for the standard opcodes.
69 Note: If you want to change this, you'll have to update the
70 "standard_opcode_lengths" table that is emitted below in
71 dwarf2_finish(). */
72 #define DWARF2_LINE_OPCODE_BASE 10
73
74 #ifndef DWARF2_LINE_BASE
75 /* Minimum line offset in a special line info. opcode. This value
76 was chosen to give a reasonable range of values. */
77 # define DWARF2_LINE_BASE -5
78 #endif
79
80 /* Range of line offsets in a special line info. opcode. */
81 #ifndef DWARF2_LINE_RANGE
82 # define DWARF2_LINE_RANGE 14
83 #endif
84
85 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
86 /* Define the architecture-dependent minimum instruction length (in
87 bytes). This value should be rather too small than too big. */
88 # define DWARF2_LINE_MIN_INSN_LENGTH 1
89 #endif
90
91 /* Flag that indicates the initial value of the is_stmt_start flag.
92 In the present implementation, we do not mark any lines as
93 the beginning of a source statement, because that information
94 is not made available by the GCC front-end. */
95 #define DWARF2_LINE_DEFAULT_IS_STMT 1
96
97 /* Given a special op, return the line skip amount. */
98 #define SPECIAL_LINE(op) \
99 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
100
101 /* Given a special op, return the address skip amount (in units of
102 DWARF2_LINE_MIN_INSN_LENGTH. */
103 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
104
105 /* The maximum address skip amount that can be encoded with a special op. */
106 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
107
108 struct line_entry {
109 struct line_entry *next;
110 fragS *frag;
111 addressT frag_ofs;
112 struct dwarf2_line_info loc;
113 };
114
115 struct line_subseg {
116 struct line_subseg *next;
117 subsegT subseg;
118 struct line_entry *head;
119 struct line_entry **ptail;
120 };
121
122 struct line_seg {
123 struct line_seg *next;
124 segT seg;
125 struct line_subseg *head;
126 symbolS *text_start;
127 symbolS *text_end;
128 };
129
130 /* Collects data for all line table entries during assembly. */
131 static struct line_seg *all_segs;
132
133 struct file_entry {
134 const char *filename;
135 unsigned int dir;
136 };
137
138 /* Table of files used by .debug_line. */
139 static struct file_entry *files;
140 static unsigned int files_in_use;
141 static unsigned int files_allocated;
142
143 /* Table of directories used by .debug_line. */
144 static char **dirs;
145 static unsigned int dirs_in_use;
146 static unsigned int dirs_allocated;
147
148 /* TRUE when we've seen a .loc directive recently. Used to avoid
149 doing work when there's nothing to do. */
150 static bfd_boolean loc_directive_seen;
151
152 /* Current location as indicated by the most recent .loc directive. */
153 static struct dwarf2_line_info current;
154
155 /* The size of an address on the target. */
156 static unsigned int sizeof_address;
157 \f
158 static struct line_subseg *get_line_subseg (segT, subsegT);
159 static unsigned int get_filenum (const char *, unsigned int);
160 static struct frag *first_frag_for_seg (segT);
161 static struct frag *last_frag_for_seg (segT);
162 static void out_byte (int);
163 static void out_opcode (int);
164 static void out_two (int);
165 static void out_four (int);
166 static void out_abbrev (int, int);
167 static void out_uleb128 (addressT);
168 static offsetT get_frag_fix (fragS *);
169 static void out_set_addr (segT, fragS *, addressT);
170 static int size_inc_line_addr (int, addressT);
171 static void emit_inc_line_addr (int, addressT, char *, int);
172 static void out_inc_line_addr (int, addressT);
173 static void relax_inc_line_addr (int, segT, fragS *, addressT,
174 fragS *, addressT);
175 static void process_entries (segT, struct line_entry *);
176 static void out_file_list (void);
177 static void out_debug_line (segT);
178 static void out_debug_aranges (segT, segT);
179 static void out_debug_abbrev (segT);
180 static void out_debug_info (segT, segT, segT);
181 \f
182 #ifndef TC_DWARF2_EMIT_OFFSET
183 # define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
184 static void generic_dwarf2_emit_offset (symbolS *, unsigned int);
185
186 /* Create an offset to .dwarf2_*. */
187
188 static void
189 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
190 {
191 expressionS expr;
192
193 expr.X_op = O_symbol;
194 expr.X_add_symbol = symbol;
195 expr.X_add_number = 0;
196 emit_expr (&expr, size);
197 }
198 #endif
199
200 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
201
202 static struct line_subseg *
203 get_line_subseg (segT seg, subsegT subseg)
204 {
205 static segT last_seg;
206 static subsegT last_subseg;
207 static struct line_subseg *last_line_subseg;
208
209 struct line_seg *s;
210 struct line_subseg **pss, *ss;
211
212 if (seg == last_seg && subseg == last_subseg)
213 return last_line_subseg;
214
215 for (s = all_segs; s; s = s->next)
216 if (s->seg == seg)
217 goto found_seg;
218
219 s = (struct line_seg *) xmalloc (sizeof (*s));
220 s->next = all_segs;
221 s->seg = seg;
222 s->head = NULL;
223 all_segs = s;
224
225 found_seg:
226 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
227 {
228 if (ss->subseg == subseg)
229 goto found_subseg;
230 if (ss->subseg > subseg)
231 break;
232 }
233
234 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
235 ss->next = *pss;
236 ss->subseg = subseg;
237 ss->head = NULL;
238 ss->ptail = &ss->head;
239 *pss = ss;
240
241 found_subseg:
242 last_seg = seg;
243 last_subseg = subseg;
244 last_line_subseg = ss;
245
246 return ss;
247 }
248
249 /* Record an entry for LOC occurring at OFS within the current fragment. */
250
251 void
252 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
253 {
254 struct line_subseg *ss;
255 struct line_entry *e;
256 static unsigned int line = -1;
257 static unsigned int filenum = -1;
258
259 /* Early out for as-yet incomplete location information. */
260 if (loc->filenum == 0 || loc->line == 0)
261 return;
262
263 /* Don't emit sequences of line symbols for the same line when the
264 symbols apply to assembler code. It is necessary to emit
265 duplicate line symbols when a compiler asks for them, because GDB
266 uses them to determine the end of the prologue. */
267 if (debug_type == DEBUG_DWARF2
268 && line == loc->line && filenum == loc->filenum)
269 return;
270
271 line = loc->line;
272 filenum = loc->filenum;
273
274 e = (struct line_entry *) xmalloc (sizeof (*e));
275 e->next = NULL;
276 e->frag = frag_now;
277 e->frag_ofs = ofs;
278 e->loc = *loc;
279
280 ss = get_line_subseg (now_seg, now_subseg);
281 *ss->ptail = e;
282 ss->ptail = &e->next;
283 }
284
285 void
286 dwarf2_where (struct dwarf2_line_info *line)
287 {
288 if (debug_type == DEBUG_DWARF2)
289 {
290 char *filename;
291 as_where (&filename, &line->line);
292 line->filenum = get_filenum (filename, 0);
293 line->column = 0;
294 line->flags = DWARF2_FLAG_BEGIN_STMT;
295 }
296 else
297 *line = current;
298 }
299
300 /* Called for each machine instruction, or relatively atomic group of
301 machine instructions (ie built-in macro). The instruction or group
302 is SIZE bytes in length. If dwarf2 line number generation is called
303 for, emit a line statement appropriately. */
304
305 void
306 dwarf2_emit_insn (int size)
307 {
308 struct dwarf2_line_info loc;
309
310 if (loc_directive_seen)
311 {
312 /* Use the last location established by a .loc directive, not
313 the value returned by dwarf2_where(). That calls as_where()
314 which will return either the logical input file name (foo.c)
315 or the physical input file name (foo.s) and not the file name
316 specified in the most recent .loc directive (eg foo.h). */
317 loc = current;
318
319 /* Unless we generate DWARF2 debugging information for each
320 assembler line, we only emit one line symbol for one LOC. */
321 if (debug_type != DEBUG_DWARF2)
322 loc_directive_seen = FALSE;
323 }
324 else if (debug_type != DEBUG_DWARF2)
325 return;
326 else
327 dwarf2_where (& loc);
328
329 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
330 }
331
332 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
333 allocate it on that file table slot, otherwise return the first
334 empty one. */
335
336 static unsigned int
337 get_filenum (const char *filename, unsigned int num)
338 {
339 static unsigned int last_used, last_used_dir_len;
340 const char *file;
341 size_t dir_len;
342 unsigned int i, dir;
343
344 if (num == 0 && last_used)
345 {
346 if (! files[last_used].dir
347 && strcmp (filename, files[last_used].filename) == 0)
348 return last_used;
349 if (files[last_used].dir
350 && strncmp (filename, dirs[files[last_used].dir],
351 last_used_dir_len) == 0
352 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
353 && strcmp (filename + last_used_dir_len + 1,
354 files[last_used].filename) == 0)
355 return last_used;
356 }
357
358 file = lbasename (filename);
359 /* Don't make empty string from / or A: from A:/ . */
360 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
361 if (file <= filename + 3)
362 file = filename;
363 #else
364 if (file == filename + 1)
365 file = filename;
366 #endif
367 dir_len = file - filename;
368
369 dir = 0;
370 if (dir_len)
371 {
372 --dir_len;
373 for (dir = 1; dir < dirs_in_use; ++dir)
374 if (strncmp (filename, dirs[dir], dir_len) == 0
375 && dirs[dir][dir_len] == '\0')
376 break;
377
378 if (dir >= dirs_in_use)
379 {
380 if (dir >= dirs_allocated)
381 {
382 dirs_allocated = dir + 32;
383 dirs = (char **)
384 xrealloc (dirs, (dir + 32) * sizeof (const char *));
385 }
386
387 dirs[dir] = xmalloc (dir_len + 1);
388 memcpy (dirs[dir], filename, dir_len);
389 dirs[dir][dir_len] = '\0';
390 dirs_in_use = dir + 1;
391 }
392 }
393
394 if (num == 0)
395 {
396 for (i = 1; i < files_in_use; ++i)
397 if (files[i].dir == dir
398 && files[i].filename
399 && strcmp (file, files[i].filename) == 0)
400 {
401 last_used = i;
402 last_used_dir_len = dir_len;
403 return i;
404 }
405 }
406 else
407 i = num;
408
409 if (i >= files_allocated)
410 {
411 unsigned int old = files_allocated;
412
413 files_allocated = i + 32;
414 files = (struct file_entry *)
415 xrealloc (files, (i + 32) * sizeof (struct file_entry));
416
417 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
418 }
419
420 files[i].filename = num ? file : xstrdup (file);
421 files[i].dir = dir;
422 files_in_use = i + 1;
423 last_used = i;
424 last_used_dir_len = dir_len;
425
426 return i;
427 }
428
429 /* Handle two forms of .file directive:
430 - Pass .file "source.c" to s_app_file
431 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
432
433 If an entry is added to the file table, return a pointer to the filename. */
434
435 char *
436 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
437 {
438 offsetT num;
439 char *filename;
440 int filename_len;
441
442 /* Continue to accept a bare string and pass it off. */
443 SKIP_WHITESPACE ();
444 if (*input_line_pointer == '"')
445 {
446 s_app_file (0);
447 return NULL;
448 }
449
450 num = get_absolute_expression ();
451 filename = demand_copy_C_string (&filename_len);
452 demand_empty_rest_of_line ();
453
454 if (num < 1)
455 {
456 as_bad (_("file number less than one"));
457 return NULL;
458 }
459
460 if (num < (int) files_in_use && files[num].filename != 0)
461 {
462 as_bad (_("file number %ld already allocated"), (long) num);
463 return NULL;
464 }
465
466 get_filenum (filename, num);
467
468 return filename;
469 }
470
471 void
472 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
473 {
474 offsetT filenum, line, column;
475
476 filenum = get_absolute_expression ();
477 SKIP_WHITESPACE ();
478 line = get_absolute_expression ();
479 SKIP_WHITESPACE ();
480 column = get_absolute_expression ();
481 demand_empty_rest_of_line ();
482
483 if (filenum < 1)
484 {
485 as_bad (_("file number less than one"));
486 return;
487 }
488 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
489 {
490 as_bad (_("unassigned file number %ld"), (long) filenum);
491 return;
492 }
493
494 current.filenum = filenum;
495 current.line = line;
496 current.column = column;
497 current.flags = DWARF2_FLAG_BEGIN_STMT;
498
499 loc_directive_seen = TRUE;
500
501 #ifndef NO_LISTING
502 if (listing)
503 {
504 if (files[filenum].dir)
505 {
506 size_t dir_len = strlen (dirs[files[filenum].dir]);
507 size_t file_len = strlen (files[filenum].filename);
508 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
509
510 memcpy (cp, dirs[files[filenum].dir], dir_len);
511 cp[dir_len] = '/';
512 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
513 cp[dir_len + file_len + 1] = '\0';
514 listing_source_file (cp);
515 }
516 else
517 listing_source_file (files[filenum].filename);
518 listing_source_line (line);
519 }
520 #endif
521 }
522 \f
523 static struct frag *
524 first_frag_for_seg (segT seg)
525 {
526 frchainS *f, *first = NULL;
527
528 for (f = frchain_root; f; f = f->frch_next)
529 if (f->frch_seg == seg
530 && (! first || first->frch_subseg > f->frch_subseg))
531 first = f;
532
533 return first ? first->frch_root : NULL;
534 }
535
536 static struct frag *
537 last_frag_for_seg (segT seg)
538 {
539 frchainS *f, *last = NULL;
540
541 for (f = frchain_root; f; f = f->frch_next)
542 if (f->frch_seg == seg
543 && (! last || last->frch_subseg < f->frch_subseg))
544 last= f;
545
546 return last ? last->frch_last : NULL;
547 }
548 \f
549 /* Emit a single byte into the current segment. */
550
551 static inline void
552 out_byte (int byte)
553 {
554 FRAG_APPEND_1_CHAR (byte);
555 }
556
557 /* Emit a statement program opcode into the current segment. */
558
559 static inline void
560 out_opcode (int opc)
561 {
562 out_byte (opc);
563 }
564
565 /* Emit a two-byte word into the current segment. */
566
567 static inline void
568 out_two (int data)
569 {
570 md_number_to_chars (frag_more (2), data, 2);
571 }
572
573 /* Emit a four byte word into the current segment. */
574
575 static inline void
576 out_four (int data)
577 {
578 md_number_to_chars (frag_more (4), data, 4);
579 }
580
581 /* Emit an unsigned "little-endian base 128" number. */
582
583 static void
584 out_uleb128 (addressT value)
585 {
586 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
587 }
588
589 /* Emit a tuple for .debug_abbrev. */
590
591 static inline void
592 out_abbrev (int name, int form)
593 {
594 out_uleb128 (name);
595 out_uleb128 (form);
596 }
597
598 /* Get the size of a fragment. */
599
600 static offsetT
601 get_frag_fix (fragS *frag)
602 {
603 frchainS *fr;
604
605 if (frag->fr_next)
606 return frag->fr_fix;
607
608 /* If a fragment is the last in the chain, special measures must be
609 taken to find its size before relaxation, since it may be pending
610 on some subsegment chain. */
611 for (fr = frchain_root; fr; fr = fr->frch_next)
612 if (fr->frch_last == frag)
613 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
614
615 abort ();
616 }
617
618 /* Set an absolute address (may result in a relocation entry). */
619
620 static void
621 out_set_addr (segT seg, fragS *frag, addressT ofs)
622 {
623 expressionS expr;
624 symbolS *sym;
625
626 sym = symbol_temp_new (seg, ofs, frag);
627
628 out_opcode (DW_LNS_extended_op);
629 out_uleb128 (sizeof_address + 1);
630
631 out_opcode (DW_LNE_set_address);
632 expr.X_op = O_symbol;
633 expr.X_add_symbol = sym;
634 expr.X_add_number = 0;
635 emit_expr (&expr, sizeof_address);
636 }
637
638 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
639 static void scale_addr_delta (addressT *);
640
641 static void
642 scale_addr_delta (addressT *addr_delta)
643 {
644 static int printed_this = 0;
645 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
646 {
647 if (!printed_this)
648 as_bad("unaligned opcodes detected in executable segment");
649 printed_this = 1;
650 }
651 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
652 }
653 #else
654 #define scale_addr_delta(A)
655 #endif
656
657 /* Encode a pair of line and address skips as efficiently as possible.
658 Note that the line skip is signed, whereas the address skip is unsigned.
659
660 The following two routines *must* be kept in sync. This is
661 enforced by making emit_inc_line_addr abort if we do not emit
662 exactly the expected number of bytes. */
663
664 static int
665 size_inc_line_addr (int line_delta, addressT addr_delta)
666 {
667 unsigned int tmp, opcode;
668 int len = 0;
669
670 /* Scale the address delta by the minimum instruction length. */
671 scale_addr_delta (&addr_delta);
672
673 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
674 We cannot use special opcodes here, since we want the end_sequence
675 to emit the matrix entry. */
676 if (line_delta == INT_MAX)
677 {
678 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
679 len = 1;
680 else
681 len = 1 + sizeof_leb128 (addr_delta, 0);
682 return len + 3;
683 }
684
685 /* Bias the line delta by the base. */
686 tmp = line_delta - DWARF2_LINE_BASE;
687
688 /* If the line increment is out of range of a special opcode, we
689 must encode it with DW_LNS_advance_line. */
690 if (tmp >= DWARF2_LINE_RANGE)
691 {
692 len = 1 + sizeof_leb128 (line_delta, 1);
693 line_delta = 0;
694 tmp = 0 - DWARF2_LINE_BASE;
695 }
696
697 /* Bias the opcode by the special opcode base. */
698 tmp += DWARF2_LINE_OPCODE_BASE;
699
700 /* Avoid overflow when addr_delta is large. */
701 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
702 {
703 /* Try using a special opcode. */
704 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
705 if (opcode <= 255)
706 return len + 1;
707
708 /* Try using DW_LNS_const_add_pc followed by special op. */
709 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
710 if (opcode <= 255)
711 return len + 2;
712 }
713
714 /* Otherwise use DW_LNS_advance_pc. */
715 len += 1 + sizeof_leb128 (addr_delta, 0);
716
717 /* DW_LNS_copy or special opcode. */
718 len += 1;
719
720 return len;
721 }
722
723 static void
724 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
725 {
726 unsigned int tmp, opcode;
727 int need_copy = 0;
728 char *end = p + len;
729
730 /* Scale the address delta by the minimum instruction length. */
731 scale_addr_delta (&addr_delta);
732
733 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
734 We cannot use special opcodes here, since we want the end_sequence
735 to emit the matrix entry. */
736 if (line_delta == INT_MAX)
737 {
738 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
739 *p++ = DW_LNS_const_add_pc;
740 else
741 {
742 *p++ = DW_LNS_advance_pc;
743 p += output_leb128 (p, addr_delta, 0);
744 }
745
746 *p++ = DW_LNS_extended_op;
747 *p++ = 1;
748 *p++ = DW_LNE_end_sequence;
749 goto done;
750 }
751
752 /* Bias the line delta by the base. */
753 tmp = line_delta - DWARF2_LINE_BASE;
754
755 /* If the line increment is out of range of a special opcode, we
756 must encode it with DW_LNS_advance_line. */
757 if (tmp >= DWARF2_LINE_RANGE)
758 {
759 *p++ = DW_LNS_advance_line;
760 p += output_leb128 (p, line_delta, 1);
761
762 /* Prettier, I think, to use DW_LNS_copy instead of a
763 "line +0, addr +0" special opcode. */
764 if (addr_delta == 0)
765 {
766 *p++ = DW_LNS_copy;
767 goto done;
768 }
769
770 line_delta = 0;
771 tmp = 0 - DWARF2_LINE_BASE;
772 need_copy = 1;
773 }
774
775 /* Bias the opcode by the special opcode base. */
776 tmp += DWARF2_LINE_OPCODE_BASE;
777
778 /* Avoid overflow when addr_delta is large. */
779 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
780 {
781 /* Try using a special opcode. */
782 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
783 if (opcode <= 255)
784 {
785 *p++ = opcode;
786 goto done;
787 }
788
789 /* Try using DW_LNS_const_add_pc followed by special op. */
790 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
791 if (opcode <= 255)
792 {
793 *p++ = DW_LNS_const_add_pc;
794 *p++ = opcode;
795 goto done;
796 }
797 }
798
799 /* Otherwise use DW_LNS_advance_pc. */
800 *p++ = DW_LNS_advance_pc;
801 p += output_leb128 (p, addr_delta, 0);
802
803 if (need_copy)
804 *p++ = DW_LNS_copy;
805 else
806 *p++ = tmp;
807
808 done:
809 assert (p == end);
810 }
811
812 /* Handy routine to combine calls to the above two routines. */
813
814 static void
815 out_inc_line_addr (int line_delta, addressT addr_delta)
816 {
817 int len = size_inc_line_addr (line_delta, addr_delta);
818 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
819 }
820
821 /* Generate a variant frag that we can use to relax address/line
822 increments between fragments of the target segment. */
823
824 static void
825 relax_inc_line_addr (int line_delta, segT seg,
826 fragS *to_frag, addressT to_ofs,
827 fragS *from_frag, addressT from_ofs)
828 {
829 symbolS *to_sym, *from_sym;
830 expressionS expr;
831 int max_chars;
832
833 to_sym = symbol_temp_new (seg, to_ofs, to_frag);
834 from_sym = symbol_temp_new (seg, from_ofs, from_frag);
835
836 expr.X_op = O_subtract;
837 expr.X_add_symbol = to_sym;
838 expr.X_op_symbol = from_sym;
839 expr.X_add_number = 0;
840
841 /* The maximum size of the frag is the line delta with a maximum
842 sized address delta. */
843 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
844
845 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
846 make_expr_symbol (&expr), line_delta, NULL);
847 }
848
849 /* The function estimates the size of a rs_dwarf2dbg variant frag
850 based on the current values of the symbols. It is called before
851 the relaxation loop. We set fr_subtype to the expected length. */
852
853 int
854 dwarf2dbg_estimate_size_before_relax (fragS *frag)
855 {
856 offsetT addr_delta;
857 int size;
858
859 addr_delta = resolve_symbol_value (frag->fr_symbol);
860 size = size_inc_line_addr (frag->fr_offset, addr_delta);
861
862 frag->fr_subtype = size;
863
864 return size;
865 }
866
867 /* This function relaxes a rs_dwarf2dbg variant frag based on the
868 current values of the symbols. fr_subtype is the current length
869 of the frag. This returns the change in frag length. */
870
871 int
872 dwarf2dbg_relax_frag (fragS *frag)
873 {
874 int old_size, new_size;
875
876 old_size = frag->fr_subtype;
877 new_size = dwarf2dbg_estimate_size_before_relax (frag);
878
879 return new_size - old_size;
880 }
881
882 /* This function converts a rs_dwarf2dbg variant frag into a normal
883 fill frag. This is called after all relaxation has been done.
884 fr_subtype will be the desired length of the frag. */
885
886 void
887 dwarf2dbg_convert_frag (fragS *frag)
888 {
889 offsetT addr_diff;
890
891 addr_diff = resolve_symbol_value (frag->fr_symbol);
892
893 /* fr_var carries the max_chars that we created the fragment with.
894 fr_subtype carries the current expected length. We must, of
895 course, have allocated enough memory earlier. */
896 assert (frag->fr_var >= (int) frag->fr_subtype);
897
898 emit_inc_line_addr (frag->fr_offset, addr_diff,
899 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
900
901 frag->fr_fix += frag->fr_subtype;
902 frag->fr_type = rs_fill;
903 frag->fr_var = 0;
904 frag->fr_offset = 0;
905 }
906
907 /* Generate .debug_line content for the chain of line number entries
908 beginning at E, for segment SEG. */
909
910 static void
911 process_entries (segT seg, struct line_entry *e)
912 {
913 unsigned filenum = 1;
914 unsigned line = 1;
915 unsigned column = 0;
916 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_BEGIN_STMT : 0;
917 fragS *frag = NULL;
918 fragS *last_frag;
919 addressT frag_ofs = 0;
920 addressT last_frag_ofs;
921 struct line_entry *next;
922
923 while (e)
924 {
925 int changed = 0;
926
927 if (filenum != e->loc.filenum)
928 {
929 filenum = e->loc.filenum;
930 out_opcode (DW_LNS_set_file);
931 out_uleb128 (filenum);
932 changed = 1;
933 }
934
935 if (column != e->loc.column)
936 {
937 column = e->loc.column;
938 out_opcode (DW_LNS_set_column);
939 out_uleb128 (column);
940 changed = 1;
941 }
942
943 if ((e->loc.flags ^ flags) & DWARF2_FLAG_BEGIN_STMT)
944 {
945 flags = e->loc.flags;
946 out_opcode (DW_LNS_negate_stmt);
947 changed = 1;
948 }
949
950 if (e->loc.flags & DWARF2_FLAG_BEGIN_BLOCK)
951 {
952 out_opcode (DW_LNS_set_basic_block);
953 changed = 1;
954 }
955
956 /* Don't try to optimize away redundant entries; gdb wants two
957 entries for a function where the code starts on the same line as
958 the {, and there's no way to identify that case here. Trust gcc
959 to optimize appropriately. */
960 if (1 /* line != e->loc.line || changed */)
961 {
962 int line_delta = e->loc.line - line;
963 if (frag == NULL)
964 {
965 out_set_addr (seg, e->frag, e->frag_ofs);
966 out_inc_line_addr (line_delta, 0);
967 }
968 else if (frag == e->frag)
969 out_inc_line_addr (line_delta, e->frag_ofs - frag_ofs);
970 else
971 relax_inc_line_addr (line_delta, seg, e->frag, e->frag_ofs,
972 frag, frag_ofs);
973
974 frag = e->frag;
975 frag_ofs = e->frag_ofs;
976 line = e->loc.line;
977 }
978 else if (frag == NULL)
979 {
980 out_set_addr (seg, e->frag, e->frag_ofs);
981 frag = e->frag;
982 frag_ofs = e->frag_ofs;
983 }
984
985 next = e->next;
986 free (e);
987 e = next;
988 }
989
990 /* Emit a DW_LNE_end_sequence for the end of the section. */
991 last_frag = last_frag_for_seg (seg);
992 last_frag_ofs = get_frag_fix (last_frag);
993 if (frag == last_frag)
994 out_inc_line_addr (INT_MAX, last_frag_ofs - frag_ofs);
995 else
996 relax_inc_line_addr (INT_MAX, seg, last_frag, last_frag_ofs,
997 frag, frag_ofs);
998 }
999
1000 /* Emit the directory and file tables for .debug_line. */
1001
1002 static void
1003 out_file_list (void)
1004 {
1005 size_t size;
1006 char *cp;
1007 unsigned int i;
1008
1009 /* Emit directory list. */
1010 for (i = 1; i < dirs_in_use; ++i)
1011 {
1012 size = strlen (dirs[i]) + 1;
1013 cp = frag_more (size);
1014 memcpy (cp, dirs[i], size);
1015 }
1016 /* Terminate it. */
1017 out_byte ('\0');
1018
1019 for (i = 1; i < files_in_use; ++i)
1020 {
1021 if (files[i].filename == NULL)
1022 {
1023 as_bad (_("unassigned file number %ld"), (long) i);
1024 /* Prevent a crash later, particularly for file 1. */
1025 files[i].filename = "";
1026 continue;
1027 }
1028
1029 size = strlen (files[i].filename) + 1;
1030 cp = frag_more (size);
1031 memcpy (cp, files[i].filename, size);
1032
1033 out_uleb128 (files[i].dir); /* directory number */
1034 out_uleb128 (0); /* last modification timestamp */
1035 out_uleb128 (0); /* filesize */
1036 }
1037
1038 /* Terminate filename list. */
1039 out_byte (0);
1040 }
1041
1042 /* Emit the collected .debug_line data. */
1043
1044 static void
1045 out_debug_line (segT line_seg)
1046 {
1047 expressionS expr;
1048 symbolS *line_start;
1049 symbolS *prologue_end;
1050 symbolS *line_end;
1051 struct line_seg *s;
1052 enum dwarf2_format d2f;
1053 int sizeof_offset;
1054
1055 subseg_set (line_seg, 0);
1056
1057 line_start = symbol_temp_new_now ();
1058 prologue_end = symbol_temp_make ();
1059 line_end = symbol_temp_make ();
1060
1061 /* Total length of the information for this compilation unit. */
1062 expr.X_op = O_subtract;
1063 expr.X_add_symbol = line_end;
1064 expr.X_op_symbol = line_start;
1065
1066 d2f = DWARF2_FORMAT ();
1067 if (d2f == dwarf2_format_32bit)
1068 {
1069 expr.X_add_number = -4;
1070 emit_expr (&expr, 4);
1071 sizeof_offset = 4;
1072 }
1073 else if (d2f == dwarf2_format_64bit)
1074 {
1075 expr.X_add_number = -12;
1076 out_four (-1);
1077 emit_expr (&expr, 8);
1078 sizeof_offset = 8;
1079 }
1080 else if (d2f == dwarf2_format_64bit_irix)
1081 {
1082 expr.X_add_number = -8;
1083 emit_expr (&expr, 8);
1084 sizeof_offset = 8;
1085 }
1086 else
1087 {
1088 as_fatal (_("internal error: unknown dwarf2 format"));
1089 }
1090
1091 /* Version. */
1092 out_two (2);
1093
1094 /* Length of the prologue following this length. */
1095 expr.X_op = O_subtract;
1096 expr.X_add_symbol = prologue_end;
1097 expr.X_op_symbol = line_start;
1098 expr.X_add_number = - (4 + 2 + 4);
1099 emit_expr (&expr, sizeof_offset);
1100
1101 /* Parameters of the state machine. */
1102 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1103 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1104 out_byte (DWARF2_LINE_BASE);
1105 out_byte (DWARF2_LINE_RANGE);
1106 out_byte (DWARF2_LINE_OPCODE_BASE);
1107
1108 /* Standard opcode lengths. */
1109 out_byte (0); /* DW_LNS_copy */
1110 out_byte (1); /* DW_LNS_advance_pc */
1111 out_byte (1); /* DW_LNS_advance_line */
1112 out_byte (1); /* DW_LNS_set_file */
1113 out_byte (1); /* DW_LNS_set_column */
1114 out_byte (0); /* DW_LNS_negate_stmt */
1115 out_byte (0); /* DW_LNS_set_basic_block */
1116 out_byte (0); /* DW_LNS_const_add_pc */
1117 out_byte (1); /* DW_LNS_fixed_advance_pc */
1118
1119 out_file_list ();
1120
1121 symbol_set_value_now (prologue_end);
1122
1123 /* For each section, emit a statement program. */
1124 for (s = all_segs; s; s = s->next)
1125 process_entries (s->seg, s->head->head);
1126
1127 symbol_set_value_now (line_end);
1128 }
1129
1130 /* Emit data for .debug_aranges. */
1131
1132 static void
1133 out_debug_aranges (segT aranges_seg, segT info_seg)
1134 {
1135 unsigned int addr_size = sizeof_address;
1136 addressT size, skip;
1137 struct line_seg *s;
1138 expressionS expr;
1139 char *p;
1140
1141 size = 4 + 2 + 4 + 1 + 1;
1142
1143 skip = 2 * addr_size - (size & (2 * addr_size - 1));
1144 if (skip == 2 * addr_size)
1145 skip = 0;
1146 size += skip;
1147
1148 for (s = all_segs; s; s = s->next)
1149 size += 2 * addr_size;
1150
1151 size += 2 * addr_size;
1152
1153 subseg_set (aranges_seg, 0);
1154
1155 /* Length of the compilation unit. */
1156 out_four (size - 4);
1157
1158 /* Version. */
1159 out_two (2);
1160
1161 /* Offset to .debug_info. */
1162 /* ??? sizeof_offset */
1163 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), 4);
1164
1165 /* Size of an address (offset portion). */
1166 out_byte (addr_size);
1167
1168 /* Size of a segment descriptor. */
1169 out_byte (0);
1170
1171 /* Align the header. */
1172 if (skip)
1173 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1174
1175 for (s = all_segs; s; s = s->next)
1176 {
1177 fragS *frag;
1178 symbolS *beg, *end;
1179
1180 frag = first_frag_for_seg (s->seg);
1181 beg = symbol_temp_new (s->seg, 0, frag);
1182 s->text_start = beg;
1183
1184 frag = last_frag_for_seg (s->seg);
1185 end = symbol_temp_new (s->seg, get_frag_fix (frag), frag);
1186 s->text_end = end;
1187
1188 expr.X_op = O_symbol;
1189 expr.X_add_symbol = beg;
1190 expr.X_add_number = 0;
1191 emit_expr (&expr, addr_size);
1192
1193 expr.X_op = O_subtract;
1194 expr.X_add_symbol = end;
1195 expr.X_op_symbol = beg;
1196 expr.X_add_number = 0;
1197 emit_expr (&expr, addr_size);
1198 }
1199
1200 p = frag_more (2 * addr_size);
1201 md_number_to_chars (p, 0, addr_size);
1202 md_number_to_chars (p + addr_size, 0, addr_size);
1203 }
1204
1205 /* Emit data for .debug_abbrev. Note that this must be kept in
1206 sync with out_debug_info below. */
1207
1208 static void
1209 out_debug_abbrev (segT abbrev_seg)
1210 {
1211 subseg_set (abbrev_seg, 0);
1212
1213 out_uleb128 (1);
1214 out_uleb128 (DW_TAG_compile_unit);
1215 out_byte (DW_CHILDREN_no);
1216 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1217 if (all_segs->next == NULL)
1218 {
1219 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1220 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1221 }
1222 out_abbrev (DW_AT_name, DW_FORM_string);
1223 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1224 out_abbrev (DW_AT_producer, DW_FORM_string);
1225 out_abbrev (DW_AT_language, DW_FORM_data2);
1226 out_abbrev (0, 0);
1227
1228 /* Terminate the abbreviations for this compilation unit. */
1229 out_byte (0);
1230 }
1231
1232 /* Emit a description of this compilation unit for .debug_info. */
1233
1234 static void
1235 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg)
1236 {
1237 char producer[128];
1238 char *comp_dir;
1239 expressionS expr;
1240 symbolS *info_start;
1241 symbolS *info_end;
1242 char *p;
1243 int len;
1244 enum dwarf2_format d2f;
1245 int sizeof_offset;
1246
1247 subseg_set (info_seg, 0);
1248
1249 info_start = symbol_temp_new_now ();
1250 info_end = symbol_temp_make ();
1251
1252 /* Compilation Unit length. */
1253 expr.X_op = O_subtract;
1254 expr.X_add_symbol = info_end;
1255 expr.X_op_symbol = info_start;
1256
1257 d2f = DWARF2_FORMAT ();
1258 if (d2f == dwarf2_format_32bit)
1259 {
1260 expr.X_add_number = -4;
1261 emit_expr (&expr, 4);
1262 sizeof_offset = 4;
1263 }
1264 else if (d2f == dwarf2_format_64bit)
1265 {
1266 expr.X_add_number = -12;
1267 out_four (-1);
1268 emit_expr (&expr, 8);
1269 sizeof_offset = 8;
1270 }
1271 else if (d2f == dwarf2_format_64bit_irix)
1272 {
1273 expr.X_add_number = -8;
1274 emit_expr (&expr, 8);
1275 sizeof_offset = 8;
1276 }
1277 else
1278 {
1279 as_fatal (_("internal error: unknown dwarf2 format"));
1280 }
1281
1282 /* DWARF version. */
1283 out_two (2);
1284
1285 /* .debug_abbrev offset */
1286 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1287
1288 /* Target address size. */
1289 out_byte (sizeof_address);
1290
1291 /* DW_TAG_compile_unit DIE abbrev */
1292 out_uleb128 (1);
1293
1294 /* DW_AT_stmt_list */
1295 /* ??? sizeof_offset */
1296 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 4);
1297
1298 /* These two attributes may only be emitted if all of the code is
1299 contiguous. Multiple sections are not that. */
1300 if (all_segs->next == NULL)
1301 {
1302 /* DW_AT_low_pc */
1303 expr.X_op = O_symbol;
1304 expr.X_add_symbol = all_segs->text_start;
1305 expr.X_add_number = 0;
1306 emit_expr (&expr, sizeof_address);
1307
1308 /* DW_AT_high_pc */
1309 expr.X_op = O_symbol;
1310 expr.X_add_symbol = all_segs->text_end;
1311 expr.X_add_number = 0;
1312 emit_expr (&expr, sizeof_address);
1313 }
1314
1315 /* DW_AT_name. We don't have the actual file name that was present
1316 on the command line, so assume files[1] is the main input file.
1317 We're not supposed to get called unless at least one line number
1318 entry was emitted, so this should always be defined. */
1319 if (!files || files_in_use < 1)
1320 abort ();
1321 if (files[1].dir)
1322 {
1323 len = strlen (dirs[files[1].dir]);
1324 p = frag_more (len + 1);
1325 memcpy (p, dirs[files[1].dir], len);
1326 p[len] = '/';
1327 }
1328 len = strlen (files[1].filename) + 1;
1329 p = frag_more (len);
1330 memcpy (p, files[1].filename, len);
1331
1332 /* DW_AT_comp_dir */
1333 comp_dir = getpwd ();
1334 len = strlen (comp_dir) + 1;
1335 p = frag_more (len);
1336 memcpy (p, comp_dir, len);
1337
1338 /* DW_AT_producer */
1339 sprintf (producer, "GNU AS %s", VERSION);
1340 len = strlen (producer) + 1;
1341 p = frag_more (len);
1342 memcpy (p, producer, len);
1343
1344 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1345 dwarf2 draft has no standard code for assembler. */
1346 out_two (DW_LANG_Mips_Assembler);
1347
1348 symbol_set_value_now (info_end);
1349 }
1350
1351 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1352 were any .file/.loc directives, or --gdwarf2 was given, or if the
1353 file has a non-empty .debug_info section. If we emit .debug_line,
1354 and the .debug_info section is empty, we also emit .debug_info,
1355 .debug_aranges and .debug_abbrev. ALL_SEGS will be non-null if
1356 there were any .file/.loc directives, or --gdwarf2 was given and
1357 there were any located instructions emitted. */
1358
1359 void
1360 dwarf2_finish (void)
1361 {
1362 segT line_seg;
1363 struct line_seg *s;
1364 segT info_seg;
1365 int emit_other_sections = 0;
1366
1367 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
1368 emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
1369
1370 if (!all_segs && emit_other_sections)
1371 /* There is no line information and no non-empty .debug_info
1372 section. */
1373 return;
1374
1375 /* Calculate the size of an address for the target machine. */
1376 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1377
1378 /* Create and switch to the line number section. */
1379 line_seg = subseg_new (".debug_line", 0);
1380 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
1381
1382 /* For each subsection, chain the debug entries together. */
1383 for (s = all_segs; s; s = s->next)
1384 {
1385 struct line_subseg *ss = s->head;
1386 struct line_entry **ptail = ss->ptail;
1387
1388 while ((ss = ss->next) != NULL)
1389 {
1390 *ptail = ss->head;
1391 ptail = ss->ptail;
1392 }
1393 }
1394
1395 out_debug_line (line_seg);
1396
1397 /* If this is assembler generated line info, and there is no
1398 debug_info already, we need .debug_info and .debug_abbrev
1399 sections as well. */
1400 if (emit_other_sections)
1401 {
1402 segT abbrev_seg;
1403 segT aranges_seg;
1404
1405 assert (all_segs);
1406
1407 info_seg = subseg_new (".debug_info", 0);
1408 abbrev_seg = subseg_new (".debug_abbrev", 0);
1409 aranges_seg = subseg_new (".debug_aranges", 0);
1410
1411 bfd_set_section_flags (stdoutput, info_seg,
1412 SEC_READONLY | SEC_DEBUGGING);
1413 bfd_set_section_flags (stdoutput, abbrev_seg,
1414 SEC_READONLY | SEC_DEBUGGING);
1415 bfd_set_section_flags (stdoutput, aranges_seg,
1416 SEC_READONLY | SEC_DEBUGGING);
1417
1418 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1419
1420 out_debug_aranges (aranges_seg, info_seg);
1421 out_debug_abbrev (abbrev_seg);
1422 out_debug_info (info_seg, abbrev_seg, line_seg);
1423 }
1424 }
This page took 0.066807 seconds and 5 git commands to generate.