source: trunk/src/binutils/gas/dwarf2dbg.c@ 10

Last change on this file since 10 was 10, checked in by bird, 22 years ago

Initial revision

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