2011-08-17 Tristan Gingold <gingold@adacore.com>
[binutils.git] / gas / dwarf2dbg.c
blob31bf31085a120d09054c3fa194419f7fa9977f22
1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* Logical line numbers can be controlled by the compiler via the
24 following directives:
26 .file FILENO "file.c"
27 .loc FILENO LINENO [COLUMN] [basic_block] [prologue_end] \
28 [epilogue_begin] [is_stmt VALUE] [isa VALUE] \
29 [discriminator VALUE]
32 #include "as.h"
33 #include "safe-ctype.h"
35 #ifdef HAVE_LIMITS_H
36 #include <limits.h>
37 #else
38 #ifdef HAVE_SYS_PARAM_H
39 #include <sys/param.h>
40 #endif
41 #ifndef INT_MAX
42 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
43 #endif
44 #endif
46 #include "dwarf2dbg.h"
47 #include <filenames.h>
49 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
50 /* We need to decide which character to use as a directory separator.
51 Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not
52 necessarily mean that the backslash character is the one to use.
53 Some environments, eg Cygwin, can support both naming conventions.
54 So we use the heuristic that we only need to use the backslash if
55 the path is an absolute path starting with a DOS style drive
56 selector. eg C: or D: */
57 # define INSERT_DIR_SEPARATOR(string, offset) \
58 do \
59 { \
60 if (offset > 1 \
61 && string[0] != 0 \
62 && string[1] == ':') \
63 string [offset] = '\\'; \
64 else \
65 string [offset] = '/'; \
66 } \
67 while (0)
68 #else
69 # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
70 #endif
72 #ifndef DWARF2_FORMAT
73 # define DWARF2_FORMAT(SEC) dwarf2_format_32bit
74 #endif
76 #ifndef DWARF2_ADDR_SIZE
77 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
78 #endif
80 #ifndef DWARF2_FILE_NAME
81 #define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME
82 #endif
84 #ifndef DWARF2_FILE_TIME_NAME
85 #define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) 0
86 #endif
88 #ifndef DWARF2_FILE_SIZE_NAME
89 #define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) 0
90 #endif
92 #ifndef DWARF2_VERSION
93 #define DWARF2_VERSION 2
94 #endif
96 #include "subsegs.h"
98 #include "dwarf2.h"
100 /* Since we can't generate the prolog until the body is complete, we
101 use three different subsegments for .debug_line: one holding the
102 prolog, one for the directory and filename info, and one for the
103 body ("statement program"). */
104 #define DL_PROLOG 0
105 #define DL_FILES 1
106 #define DL_BODY 2
108 /* If linker relaxation might change offsets in the code, the DWARF special
109 opcodes and variable-length operands cannot be used. If this macro is
110 nonzero, use the DW_LNS_fixed_advance_pc opcode instead. */
111 #ifndef DWARF2_USE_FIXED_ADVANCE_PC
112 # define DWARF2_USE_FIXED_ADVANCE_PC 0
113 #endif
115 /* First special line opcde - leave room for the standard opcodes.
116 Note: If you want to change this, you'll have to update the
117 "standard_opcode_lengths" table that is emitted below in
118 out_debug_line(). */
119 #define DWARF2_LINE_OPCODE_BASE 13
121 #ifndef DWARF2_LINE_BASE
122 /* Minimum line offset in a special line info. opcode. This value
123 was chosen to give a reasonable range of values. */
124 # define DWARF2_LINE_BASE -5
125 #endif
127 /* Range of line offsets in a special line info. opcode. */
128 #ifndef DWARF2_LINE_RANGE
129 # define DWARF2_LINE_RANGE 14
130 #endif
132 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
133 /* Define the architecture-dependent minimum instruction length (in
134 bytes). This value should be rather too small than too big. */
135 # define DWARF2_LINE_MIN_INSN_LENGTH 1
136 #endif
138 /* Flag that indicates the initial value of the is_stmt_start flag. */
139 #define DWARF2_LINE_DEFAULT_IS_STMT 1
141 /* Given a special op, return the line skip amount. */
142 #define SPECIAL_LINE(op) \
143 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
145 /* Given a special op, return the address skip amount (in units of
146 DWARF2_LINE_MIN_INSN_LENGTH. */
147 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
149 /* The maximum address skip amount that can be encoded with a special op. */
150 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
152 struct line_entry {
153 struct line_entry *next;
154 symbolS *label;
155 struct dwarf2_line_info loc;
158 struct line_subseg {
159 struct line_subseg *next;
160 subsegT subseg;
161 struct line_entry *head;
162 struct line_entry **ptail;
165 struct line_seg {
166 struct line_seg *next;
167 segT seg;
168 struct line_subseg *head;
169 symbolS *text_start;
170 symbolS *text_end;
173 /* Collects data for all line table entries during assembly. */
174 static struct line_seg *all_segs;
175 /* Hash used to quickly lookup a segment by name, avoiding the need to search
176 through the all_segs list. */
177 static struct hash_control *all_segs_hash;
178 static struct line_seg **last_seg_ptr;
180 struct file_entry {
181 const char *filename;
182 unsigned int dir;
185 /* Table of files used by .debug_line. */
186 static struct file_entry *files;
187 static unsigned int files_in_use;
188 static unsigned int files_allocated;
190 /* Table of directories used by .debug_line. */
191 static char **dirs;
192 static unsigned int dirs_in_use;
193 static unsigned int dirs_allocated;
195 /* TRUE when we've seen a .loc directive recently. Used to avoid
196 doing work when there's nothing to do. */
197 bfd_boolean dwarf2_loc_directive_seen;
199 /* TRUE when we're supposed to set the basic block mark whenever a
200 label is seen. */
201 bfd_boolean dwarf2_loc_mark_labels;
203 /* Current location as indicated by the most recent .loc directive. */
204 static struct dwarf2_line_info current = {
205 1, 1, 0, 0,
206 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0,
210 /* The size of an address on the target. */
211 static unsigned int sizeof_address;
213 static unsigned int get_filenum (const char *, unsigned int);
215 #ifndef TC_DWARF2_EMIT_OFFSET
216 #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
218 /* Create an offset to .dwarf2_*. */
220 static void
221 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
223 expressionS exp;
225 exp.X_op = O_symbol;
226 exp.X_add_symbol = symbol;
227 exp.X_add_number = 0;
228 emit_expr (&exp, size);
230 #endif
232 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
234 static struct line_subseg *
235 get_line_subseg (segT seg, subsegT subseg)
237 static segT last_seg;
238 static subsegT last_subseg;
239 static struct line_subseg *last_line_subseg;
241 struct line_seg *s;
242 struct line_subseg **pss, *lss;
244 if (seg == last_seg && subseg == last_subseg)
245 return last_line_subseg;
247 s = (struct line_seg *) hash_find (all_segs_hash, seg->name);
248 if (s == NULL)
250 s = (struct line_seg *) xmalloc (sizeof (*s));
251 s->next = NULL;
252 s->seg = seg;
253 s->head = NULL;
254 *last_seg_ptr = s;
255 last_seg_ptr = &s->next;
256 hash_insert (all_segs_hash, seg->name, s);
258 gas_assert (seg == s->seg);
260 for (pss = &s->head; (lss = *pss) != NULL ; pss = &lss->next)
262 if (lss->subseg == subseg)
263 goto found_subseg;
264 if (lss->subseg > subseg)
265 break;
268 lss = (struct line_subseg *) xmalloc (sizeof (*lss));
269 lss->next = *pss;
270 lss->subseg = subseg;
271 lss->head = NULL;
272 lss->ptail = &lss->head;
273 *pss = lss;
275 found_subseg:
276 last_seg = seg;
277 last_subseg = subseg;
278 last_line_subseg = lss;
280 return lss;
283 /* Record an entry for LOC occurring at LABEL. */
285 static void
286 dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
288 struct line_subseg *lss;
289 struct line_entry *e;
291 e = (struct line_entry *) xmalloc (sizeof (*e));
292 e->next = NULL;
293 e->label = label;
294 e->loc = *loc;
296 lss = get_line_subseg (now_seg, now_subseg);
297 *lss->ptail = e;
298 lss->ptail = &e->next;
301 /* Record an entry for LOC occurring at OFS within the current fragment. */
303 void
304 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
306 static unsigned int line = -1;
307 static unsigned int filenum = -1;
309 symbolS *sym;
311 /* Early out for as-yet incomplete location information. */
312 if (loc->filenum == 0 || loc->line == 0)
313 return;
315 /* Don't emit sequences of line symbols for the same line when the
316 symbols apply to assembler code. It is necessary to emit
317 duplicate line symbols when a compiler asks for them, because GDB
318 uses them to determine the end of the prologue. */
319 if (debug_type == DEBUG_DWARF2
320 && line == loc->line && filenum == loc->filenum)
321 return;
323 line = loc->line;
324 filenum = loc->filenum;
326 sym = symbol_temp_new (now_seg, ofs, frag_now);
327 dwarf2_gen_line_info_1 (sym, loc);
330 /* Returns the current source information. If .file directives have
331 been encountered, the info for the corresponding source file is
332 returned. Otherwise, the info for the assembly source file is
333 returned. */
335 void
336 dwarf2_where (struct dwarf2_line_info *line)
338 if (debug_type == DEBUG_DWARF2)
340 char *filename;
341 as_where (&filename, &line->line);
342 line->filenum = get_filenum (filename, 0);
343 line->column = 0;
344 line->flags = DWARF2_FLAG_IS_STMT;
345 line->isa = current.isa;
346 line->discriminator = current.discriminator;
348 else
349 *line = current;
352 /* A hook to allow the target backend to inform the line number state
353 machine of isa changes when assembler debug info is enabled. */
355 void
356 dwarf2_set_isa (unsigned int isa)
358 current.isa = isa;
361 /* Called for each machine instruction, or relatively atomic group of
362 machine instructions (ie built-in macro). The instruction or group
363 is SIZE bytes in length. If dwarf2 line number generation is called
364 for, emit a line statement appropriately. */
366 void
367 dwarf2_emit_insn (int size)
369 struct dwarf2_line_info loc;
371 if (!dwarf2_loc_directive_seen && debug_type != DEBUG_DWARF2)
372 return;
374 dwarf2_where (&loc);
376 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
377 dwarf2_consume_line_info ();
380 /* Called after the current line information has been either used with
381 dwarf2_gen_line_info or saved with a machine instruction for later use.
382 This resets the state of the line number information to reflect that
383 it has been used. */
385 void
386 dwarf2_consume_line_info (void)
388 /* Unless we generate DWARF2 debugging information for each
389 assembler line, we only emit one line symbol for one LOC. */
390 dwarf2_loc_directive_seen = FALSE;
392 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
393 | DWARF2_FLAG_PROLOGUE_END
394 | DWARF2_FLAG_EPILOGUE_BEGIN);
395 current.discriminator = 0;
398 /* Called for each (preferably code) label. If dwarf2_loc_mark_labels
399 is enabled, emit a basic block marker. */
401 void
402 dwarf2_emit_label (symbolS *label)
404 struct dwarf2_line_info loc;
406 if (!dwarf2_loc_mark_labels)
407 return;
408 if (S_GET_SEGMENT (label) != now_seg)
409 return;
410 if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE))
411 return;
412 if (files_in_use == 0 && debug_type != DEBUG_DWARF2)
413 return;
415 dwarf2_where (&loc);
417 loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
419 dwarf2_gen_line_info_1 (label, &loc);
420 dwarf2_consume_line_info ();
423 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
424 allocate it on that file table slot, otherwise return the first
425 empty one. */
427 static unsigned int
428 get_filenum (const char *filename, unsigned int num)
430 static unsigned int last_used, last_used_dir_len;
431 const char *file;
432 size_t dir_len;
433 unsigned int i, dir;
435 if (num == 0 && last_used)
437 if (! files[last_used].dir
438 && filename_cmp (filename, files[last_used].filename) == 0)
439 return last_used;
440 if (files[last_used].dir
441 && filename_ncmp (filename, dirs[files[last_used].dir],
442 last_used_dir_len) == 0
443 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
444 && filename_cmp (filename + last_used_dir_len + 1,
445 files[last_used].filename) == 0)
446 return last_used;
449 file = lbasename (filename);
450 /* Don't make empty string from / or A: from A:/ . */
451 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
452 if (file <= filename + 3)
453 file = filename;
454 #else
455 if (file == filename + 1)
456 file = filename;
457 #endif
458 dir_len = file - filename;
460 dir = 0;
461 if (dir_len)
463 #ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR
464 --dir_len;
465 #endif
466 for (dir = 1; dir < dirs_in_use; ++dir)
467 if (filename_ncmp (filename, dirs[dir], dir_len) == 0
468 && dirs[dir][dir_len] == '\0')
469 break;
471 if (dir >= dirs_in_use)
473 if (dir >= dirs_allocated)
475 dirs_allocated = dir + 32;
476 dirs = (char **)
477 xrealloc (dirs, (dir + 32) * sizeof (const char *));
480 dirs[dir] = (char *) xmalloc (dir_len + 1);
481 memcpy (dirs[dir], filename, dir_len);
482 dirs[dir][dir_len] = '\0';
483 dirs_in_use = dir + 1;
487 if (num == 0)
489 for (i = 1; i < files_in_use; ++i)
490 if (files[i].dir == dir
491 && files[i].filename
492 && filename_cmp (file, files[i].filename) == 0)
494 last_used = i;
495 last_used_dir_len = dir_len;
496 return i;
499 else
500 i = num;
502 if (i >= files_allocated)
504 unsigned int old = files_allocated;
506 files_allocated = i + 32;
507 files = (struct file_entry *)
508 xrealloc (files, (i + 32) * sizeof (struct file_entry));
510 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
513 files[i].filename = num ? file : xstrdup (file);
514 files[i].dir = dir;
515 if (files_in_use < i + 1)
516 files_in_use = i + 1;
517 last_used = i;
518 last_used_dir_len = dir_len;
520 return i;
523 /* Handle two forms of .file directive:
524 - Pass .file "source.c" to s_app_file
525 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
527 If an entry is added to the file table, return a pointer to the filename. */
529 char *
530 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
532 offsetT num;
533 char *filename;
534 int filename_len;
536 /* Continue to accept a bare string and pass it off. */
537 SKIP_WHITESPACE ();
538 if (*input_line_pointer == '"')
540 s_app_file (0);
541 return NULL;
544 num = get_absolute_expression ();
545 filename = demand_copy_C_string (&filename_len);
546 if (filename == NULL)
547 return NULL;
548 demand_empty_rest_of_line ();
550 if (num < 1)
552 as_bad (_("file number less than one"));
553 return NULL;
556 /* A .file directive implies compiler generated debug information is
557 being supplied. Turn off gas generated debug info. */
558 debug_type = DEBUG_NONE;
560 if (num < (int) files_in_use && files[num].filename != 0)
562 as_bad (_("file number %ld already allocated"), (long) num);
563 return NULL;
566 get_filenum (filename, num);
568 return filename;
571 void
572 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
574 offsetT filenum, line;
576 /* If we see two .loc directives in a row, force the first one to be
577 output now. */
578 if (dwarf2_loc_directive_seen)
579 dwarf2_emit_insn (0);
581 filenum = get_absolute_expression ();
582 SKIP_WHITESPACE ();
583 line = get_absolute_expression ();
585 if (filenum < 1)
587 as_bad (_("file number less than one"));
588 return;
590 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
592 as_bad (_("unassigned file number %ld"), (long) filenum);
593 return;
596 current.filenum = filenum;
597 current.line = line;
598 current.discriminator = 0;
600 #ifndef NO_LISTING
601 if (listing)
603 if (files[filenum].dir)
605 size_t dir_len = strlen (dirs[files[filenum].dir]);
606 size_t file_len = strlen (files[filenum].filename);
607 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
609 memcpy (cp, dirs[files[filenum].dir], dir_len);
610 INSERT_DIR_SEPARATOR (cp, dir_len);
611 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
612 cp[dir_len + file_len + 1] = '\0';
613 listing_source_file (cp);
615 else
616 listing_source_file (files[filenum].filename);
617 listing_source_line (line);
619 #endif
621 SKIP_WHITESPACE ();
622 if (ISDIGIT (*input_line_pointer))
624 current.column = get_absolute_expression ();
625 SKIP_WHITESPACE ();
628 while (ISALPHA (*input_line_pointer))
630 char *p, c;
631 offsetT value;
633 p = input_line_pointer;
634 c = get_symbol_end ();
636 if (strcmp (p, "basic_block") == 0)
638 current.flags |= DWARF2_FLAG_BASIC_BLOCK;
639 *input_line_pointer = c;
641 else if (strcmp (p, "prologue_end") == 0)
643 current.flags |= DWARF2_FLAG_PROLOGUE_END;
644 *input_line_pointer = c;
646 else if (strcmp (p, "epilogue_begin") == 0)
648 current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
649 *input_line_pointer = c;
651 else if (strcmp (p, "is_stmt") == 0)
653 *input_line_pointer = c;
654 value = get_absolute_expression ();
655 if (value == 0)
656 current.flags &= ~DWARF2_FLAG_IS_STMT;
657 else if (value == 1)
658 current.flags |= DWARF2_FLAG_IS_STMT;
659 else
661 as_bad (_("is_stmt value not 0 or 1"));
662 return;
665 else if (strcmp (p, "isa") == 0)
667 *input_line_pointer = c;
668 value = get_absolute_expression ();
669 if (value >= 0)
670 current.isa = value;
671 else
673 as_bad (_("isa number less than zero"));
674 return;
677 else if (strcmp (p, "discriminator") == 0)
679 *input_line_pointer = c;
680 value = get_absolute_expression ();
681 if (value >= 0)
682 current.discriminator = value;
683 else
685 as_bad (_("discriminator less than zero"));
686 return;
689 else
691 as_bad (_("unknown .loc sub-directive `%s'"), p);
692 *input_line_pointer = c;
693 return;
696 SKIP_WHITESPACE ();
699 demand_empty_rest_of_line ();
700 dwarf2_loc_directive_seen = TRUE;
701 debug_type = DEBUG_NONE;
704 void
705 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
707 offsetT value = get_absolute_expression ();
709 if (value != 0 && value != 1)
711 as_bad (_("expected 0 or 1"));
712 ignore_rest_of_line ();
714 else
716 dwarf2_loc_mark_labels = value != 0;
717 demand_empty_rest_of_line ();
721 static struct frag *
722 first_frag_for_seg (segT seg)
724 return seg_info (seg)->frchainP->frch_root;
727 static struct frag *
728 last_frag_for_seg (segT seg)
730 frchainS *f = seg_info (seg)->frchainP;
732 while (f->frch_next != NULL)
733 f = f->frch_next;
735 return f->frch_last;
738 /* Emit a single byte into the current segment. */
740 static inline void
741 out_byte (int byte)
743 FRAG_APPEND_1_CHAR (byte);
746 /* Emit a statement program opcode into the current segment. */
748 static inline void
749 out_opcode (int opc)
751 out_byte (opc);
754 /* Emit a two-byte word into the current segment. */
756 static inline void
757 out_two (int data)
759 md_number_to_chars (frag_more (2), data, 2);
762 /* Emit a four byte word into the current segment. */
764 static inline void
765 out_four (int data)
767 md_number_to_chars (frag_more (4), data, 4);
770 /* Emit an unsigned "little-endian base 128" number. */
772 static void
773 out_uleb128 (addressT value)
775 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
778 /* Emit a signed "little-endian base 128" number. */
780 static void
781 out_leb128 (addressT value)
783 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
786 /* Emit a tuple for .debug_abbrev. */
788 static inline void
789 out_abbrev (int name, int form)
791 out_uleb128 (name);
792 out_uleb128 (form);
795 /* Get the size of a fragment. */
797 static offsetT
798 get_frag_fix (fragS *frag, segT seg)
800 frchainS *fr;
802 if (frag->fr_next)
803 return frag->fr_fix;
805 /* If a fragment is the last in the chain, special measures must be
806 taken to find its size before relaxation, since it may be pending
807 on some subsegment chain. */
808 for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
809 if (fr->frch_last == frag)
810 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
812 abort ();
815 /* Set an absolute address (may result in a relocation entry). */
817 static void
818 out_set_addr (symbolS *sym)
820 expressionS exp;
822 out_opcode (DW_LNS_extended_op);
823 out_uleb128 (sizeof_address + 1);
825 out_opcode (DW_LNE_set_address);
826 exp.X_op = O_symbol;
827 exp.X_add_symbol = sym;
828 exp.X_add_number = 0;
829 emit_expr (&exp, sizeof_address);
832 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
833 static void scale_addr_delta (addressT *);
835 static void
836 scale_addr_delta (addressT *addr_delta)
838 static int printed_this = 0;
839 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
841 if (!printed_this)
842 as_bad("unaligned opcodes detected in executable segment");
843 printed_this = 1;
845 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
847 #else
848 #define scale_addr_delta(A)
849 #endif
851 /* Encode a pair of line and address skips as efficiently as possible.
852 Note that the line skip is signed, whereas the address skip is unsigned.
854 The following two routines *must* be kept in sync. This is
855 enforced by making emit_inc_line_addr abort if we do not emit
856 exactly the expected number of bytes. */
858 static int
859 size_inc_line_addr (int line_delta, addressT addr_delta)
861 unsigned int tmp, opcode;
862 int len = 0;
864 /* Scale the address delta by the minimum instruction length. */
865 scale_addr_delta (&addr_delta);
867 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
868 We cannot use special opcodes here, since we want the end_sequence
869 to emit the matrix entry. */
870 if (line_delta == INT_MAX)
872 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
873 len = 1;
874 else
875 len = 1 + sizeof_leb128 (addr_delta, 0);
876 return len + 3;
879 /* Bias the line delta by the base. */
880 tmp = line_delta - DWARF2_LINE_BASE;
882 /* If the line increment is out of range of a special opcode, we
883 must encode it with DW_LNS_advance_line. */
884 if (tmp >= DWARF2_LINE_RANGE)
886 len = 1 + sizeof_leb128 (line_delta, 1);
887 line_delta = 0;
888 tmp = 0 - DWARF2_LINE_BASE;
891 /* Bias the opcode by the special opcode base. */
892 tmp += DWARF2_LINE_OPCODE_BASE;
894 /* Avoid overflow when addr_delta is large. */
895 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
897 /* Try using a special opcode. */
898 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
899 if (opcode <= 255)
900 return len + 1;
902 /* Try using DW_LNS_const_add_pc followed by special op. */
903 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
904 if (opcode <= 255)
905 return len + 2;
908 /* Otherwise use DW_LNS_advance_pc. */
909 len += 1 + sizeof_leb128 (addr_delta, 0);
911 /* DW_LNS_copy or special opcode. */
912 len += 1;
914 return len;
917 static void
918 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
920 unsigned int tmp, opcode;
921 int need_copy = 0;
922 char *end = p + len;
924 /* Line number sequences cannot go backward in addresses. This means
925 we've incorrectly ordered the statements in the sequence. */
926 gas_assert ((offsetT) addr_delta >= 0);
928 /* Scale the address delta by the minimum instruction length. */
929 scale_addr_delta (&addr_delta);
931 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
932 We cannot use special opcodes here, since we want the end_sequence
933 to emit the matrix entry. */
934 if (line_delta == INT_MAX)
936 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
937 *p++ = DW_LNS_const_add_pc;
938 else
940 *p++ = DW_LNS_advance_pc;
941 p += output_leb128 (p, addr_delta, 0);
944 *p++ = DW_LNS_extended_op;
945 *p++ = 1;
946 *p++ = DW_LNE_end_sequence;
947 goto done;
950 /* Bias the line delta by the base. */
951 tmp = line_delta - DWARF2_LINE_BASE;
953 /* If the line increment is out of range of a special opcode, we
954 must encode it with DW_LNS_advance_line. */
955 if (tmp >= DWARF2_LINE_RANGE)
957 *p++ = DW_LNS_advance_line;
958 p += output_leb128 (p, line_delta, 1);
960 line_delta = 0;
961 tmp = 0 - DWARF2_LINE_BASE;
962 need_copy = 1;
965 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
966 special opcode. */
967 if (line_delta == 0 && addr_delta == 0)
969 *p++ = DW_LNS_copy;
970 goto done;
973 /* Bias the opcode by the special opcode base. */
974 tmp += DWARF2_LINE_OPCODE_BASE;
976 /* Avoid overflow when addr_delta is large. */
977 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
979 /* Try using a special opcode. */
980 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
981 if (opcode <= 255)
983 *p++ = opcode;
984 goto done;
987 /* Try using DW_LNS_const_add_pc followed by special op. */
988 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
989 if (opcode <= 255)
991 *p++ = DW_LNS_const_add_pc;
992 *p++ = opcode;
993 goto done;
997 /* Otherwise use DW_LNS_advance_pc. */
998 *p++ = DW_LNS_advance_pc;
999 p += output_leb128 (p, addr_delta, 0);
1001 if (need_copy)
1002 *p++ = DW_LNS_copy;
1003 else
1004 *p++ = tmp;
1006 done:
1007 gas_assert (p == end);
1010 /* Handy routine to combine calls to the above two routines. */
1012 static void
1013 out_inc_line_addr (int line_delta, addressT addr_delta)
1015 int len = size_inc_line_addr (line_delta, addr_delta);
1016 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
1019 /* Write out an alternative form of line and address skips using
1020 DW_LNS_fixed_advance_pc opcodes. This uses more space than the default
1021 line and address information, but it is required if linker relaxation
1022 could change the code offsets. The following two routines *must* be
1023 kept in sync. */
1025 static int
1026 size_fixed_inc_line_addr (int line_delta, addressT addr_delta)
1028 int len = 0;
1030 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */
1031 if (line_delta != INT_MAX)
1032 len = 1 + sizeof_leb128 (line_delta, 1);
1034 if (addr_delta > 50000)
1036 /* DW_LNS_extended_op */
1037 len += 1 + sizeof_leb128 (sizeof_address + 1, 0);
1038 /* DW_LNE_set_address */
1039 len += 1 + sizeof_address;
1041 else
1042 /* DW_LNS_fixed_advance_pc */
1043 len += 3;
1045 if (line_delta == INT_MAX)
1046 /* DW_LNS_extended_op + DW_LNE_end_sequence */
1047 len += 3;
1048 else
1049 /* DW_LNS_copy */
1050 len += 1;
1052 return len;
1055 static void
1056 emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag,
1057 char *p, int len)
1059 expressionS *pexp;
1060 segT line_seg;
1061 char *end = p + len;
1063 /* Line number sequences cannot go backward in addresses. This means
1064 we've incorrectly ordered the statements in the sequence. */
1065 gas_assert ((offsetT) addr_delta >= 0);
1067 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */
1068 if (line_delta != INT_MAX)
1070 *p++ = DW_LNS_advance_line;
1071 p += output_leb128 (p, line_delta, 1);
1074 pexp = symbol_get_value_expression (frag->fr_symbol);
1075 line_seg = subseg_get (".debug_line", 0);
1077 /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can
1078 advance the address by at most 64K. Linker relaxation (without
1079 which this function would not be used) could change the operand by
1080 an unknown amount. If the address increment is getting close to
1081 the limit, just reset the address. */
1082 if (addr_delta > 50000)
1084 symbolS *to_sym;
1085 expressionS exp;
1087 gas_assert (pexp->X_op == O_subtract);
1088 to_sym = pexp->X_add_symbol;
1090 *p++ = DW_LNS_extended_op;
1091 p += output_leb128 (p, sizeof_address + 1, 0);
1092 *p++ = DW_LNE_set_address;
1093 exp.X_op = O_symbol;
1094 exp.X_add_symbol = to_sym;
1095 exp.X_add_number = 0;
1096 subseg_change (line_seg, 0);
1097 emit_expr_fix (&exp, sizeof_address, frag, p);
1098 p += sizeof_address;
1100 else
1102 *p++ = DW_LNS_fixed_advance_pc;
1103 subseg_change (line_seg, 0);
1104 emit_expr_fix (pexp, 2, frag, p);
1105 p += 2;
1108 if (line_delta == INT_MAX)
1110 *p++ = DW_LNS_extended_op;
1111 *p++ = 1;
1112 *p++ = DW_LNE_end_sequence;
1114 else
1115 *p++ = DW_LNS_copy;
1117 gas_assert (p == end);
1120 /* Generate a variant frag that we can use to relax address/line
1121 increments between fragments of the target segment. */
1123 static void
1124 relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
1126 expressionS exp;
1127 int max_chars;
1129 exp.X_op = O_subtract;
1130 exp.X_add_symbol = to_sym;
1131 exp.X_op_symbol = from_sym;
1132 exp.X_add_number = 0;
1134 /* The maximum size of the frag is the line delta with a maximum
1135 sized address delta. */
1136 if (DWARF2_USE_FIXED_ADVANCE_PC)
1137 max_chars = size_fixed_inc_line_addr (line_delta,
1138 -DWARF2_LINE_MIN_INSN_LENGTH);
1139 else
1140 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
1142 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
1143 make_expr_symbol (&exp), line_delta, NULL);
1146 /* The function estimates the size of a rs_dwarf2dbg variant frag
1147 based on the current values of the symbols. It is called before
1148 the relaxation loop. We set fr_subtype to the expected length. */
1151 dwarf2dbg_estimate_size_before_relax (fragS *frag)
1153 offsetT addr_delta;
1154 int size;
1156 addr_delta = resolve_symbol_value (frag->fr_symbol);
1157 if (DWARF2_USE_FIXED_ADVANCE_PC)
1158 size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta);
1159 else
1160 size = size_inc_line_addr (frag->fr_offset, addr_delta);
1162 frag->fr_subtype = size;
1164 return size;
1167 /* This function relaxes a rs_dwarf2dbg variant frag based on the
1168 current values of the symbols. fr_subtype is the current length
1169 of the frag. This returns the change in frag length. */
1172 dwarf2dbg_relax_frag (fragS *frag)
1174 int old_size, new_size;
1176 old_size = frag->fr_subtype;
1177 new_size = dwarf2dbg_estimate_size_before_relax (frag);
1179 return new_size - old_size;
1182 /* This function converts a rs_dwarf2dbg variant frag into a normal
1183 fill frag. This is called after all relaxation has been done.
1184 fr_subtype will be the desired length of the frag. */
1186 void
1187 dwarf2dbg_convert_frag (fragS *frag)
1189 offsetT addr_diff;
1191 addr_diff = resolve_symbol_value (frag->fr_symbol);
1193 /* fr_var carries the max_chars that we created the fragment with.
1194 fr_subtype carries the current expected length. We must, of
1195 course, have allocated enough memory earlier. */
1196 gas_assert (frag->fr_var >= (int) frag->fr_subtype);
1198 if (DWARF2_USE_FIXED_ADVANCE_PC)
1199 emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag,
1200 frag->fr_literal + frag->fr_fix,
1201 frag->fr_subtype);
1202 else
1203 emit_inc_line_addr (frag->fr_offset, addr_diff,
1204 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
1206 frag->fr_fix += frag->fr_subtype;
1207 frag->fr_type = rs_fill;
1208 frag->fr_var = 0;
1209 frag->fr_offset = 0;
1212 /* Generate .debug_line content for the chain of line number entries
1213 beginning at E, for segment SEG. */
1215 static void
1216 process_entries (segT seg, struct line_entry *e)
1218 unsigned filenum = 1;
1219 unsigned line = 1;
1220 unsigned column = 0;
1221 unsigned isa = 0;
1222 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1223 fragS *last_frag = NULL, *frag;
1224 addressT last_frag_ofs = 0, frag_ofs;
1225 symbolS *last_lab = NULL, *lab;
1226 struct line_entry *next;
1230 int line_delta;
1232 if (filenum != e->loc.filenum)
1234 filenum = e->loc.filenum;
1235 out_opcode (DW_LNS_set_file);
1236 out_uleb128 (filenum);
1239 if (column != e->loc.column)
1241 column = e->loc.column;
1242 out_opcode (DW_LNS_set_column);
1243 out_uleb128 (column);
1246 if (e->loc.discriminator != 0)
1248 out_opcode (DW_LNS_extended_op);
1249 out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0));
1250 out_opcode (DW_LNE_set_discriminator);
1251 out_uleb128 (e->loc.discriminator);
1254 if (isa != e->loc.isa)
1256 isa = e->loc.isa;
1257 out_opcode (DW_LNS_set_isa);
1258 out_uleb128 (isa);
1261 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
1263 flags = e->loc.flags;
1264 out_opcode (DW_LNS_negate_stmt);
1267 if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
1268 out_opcode (DW_LNS_set_basic_block);
1270 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
1271 out_opcode (DW_LNS_set_prologue_end);
1273 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1274 out_opcode (DW_LNS_set_epilogue_begin);
1276 /* Don't try to optimize away redundant entries; gdb wants two
1277 entries for a function where the code starts on the same line as
1278 the {, and there's no way to identify that case here. Trust gcc
1279 to optimize appropriately. */
1280 line_delta = e->loc.line - line;
1281 lab = e->label;
1282 frag = symbol_get_frag (lab);
1283 frag_ofs = S_GET_VALUE (lab);
1285 if (last_frag == NULL)
1287 out_set_addr (lab);
1288 out_inc_line_addr (line_delta, 0);
1290 else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1291 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
1292 else
1293 relax_inc_line_addr (line_delta, lab, last_lab);
1295 line = e->loc.line;
1296 last_lab = lab;
1297 last_frag = frag;
1298 last_frag_ofs = frag_ofs;
1300 next = e->next;
1301 free (e);
1302 e = next;
1304 while (e);
1306 /* Emit a DW_LNE_end_sequence for the end of the section. */
1307 frag = last_frag_for_seg (seg);
1308 frag_ofs = get_frag_fix (frag, seg);
1309 if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1310 out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
1311 else
1313 lab = symbol_temp_new (seg, frag_ofs, frag);
1314 relax_inc_line_addr (INT_MAX, lab, last_lab);
1318 /* Emit the directory and file tables for .debug_line. */
1320 static void
1321 out_file_list (void)
1323 size_t size;
1324 const char *dir;
1325 char *cp;
1326 unsigned int i;
1328 /* Emit directory list. */
1329 for (i = 1; i < dirs_in_use; ++i)
1331 dir = remap_debug_filename (dirs[i]);
1332 size = strlen (dir) + 1;
1333 cp = frag_more (size);
1334 memcpy (cp, dir, size);
1336 /* Terminate it. */
1337 out_byte ('\0');
1339 for (i = 1; i < files_in_use; ++i)
1341 const char *fullfilename;
1343 if (files[i].filename == NULL)
1345 as_bad (_("unassigned file number %ld"), (long) i);
1346 /* Prevent a crash later, particularly for file 1. */
1347 files[i].filename = "";
1348 continue;
1351 fullfilename = DWARF2_FILE_NAME (files[i].filename,
1352 files[i].dir ? dirs [files [i].dir] : "");
1353 size = strlen (fullfilename) + 1;
1354 cp = frag_more (size);
1355 memcpy (cp, fullfilename, size);
1357 out_uleb128 (files[i].dir); /* directory number */
1358 /* Output the last modification timestamp. */
1359 out_uleb128 (DWARF2_FILE_TIME_NAME (files[i].filename,
1360 files[i].dir ? dirs [files [i].dir] : ""));
1361 /* Output the filesize. */
1362 out_uleb128 (DWARF2_FILE_SIZE_NAME (files[i].filename,
1363 files[i].dir ? dirs [files [i].dir] : ""));
1366 /* Terminate filename list. */
1367 out_byte (0);
1370 /* Switch to SEC and output a header length field. Return the size of
1371 offsets used in SEC. The caller must set EXPR->X_add_symbol value
1372 to the end of the section. */
1374 static int
1375 out_header (asection *sec, expressionS *exp)
1377 symbolS *start_sym;
1378 symbolS *end_sym;
1380 subseg_set (sec, 0);
1381 start_sym = symbol_temp_new_now ();;
1382 end_sym = symbol_temp_make ();
1384 /* Total length of the information. */
1385 exp->X_op = O_subtract;
1386 exp->X_add_symbol = end_sym;
1387 exp->X_op_symbol = start_sym;
1389 switch (DWARF2_FORMAT (sec))
1391 case dwarf2_format_32bit:
1392 exp->X_add_number = -4;
1393 emit_expr (exp, 4);
1394 return 4;
1396 case dwarf2_format_64bit:
1397 exp->X_add_number = -12;
1398 out_four (-1);
1399 emit_expr (exp, 8);
1400 return 8;
1402 case dwarf2_format_64bit_irix:
1403 exp->X_add_number = -8;
1404 emit_expr (exp, 8);
1405 return 8;
1408 as_fatal (_("internal error: unknown dwarf2 format"));
1409 return 0;
1412 /* Emit the collected .debug_line data. */
1414 static void
1415 out_debug_line (segT line_seg)
1417 expressionS exp;
1418 symbolS *prologue_end;
1419 symbolS *line_end;
1420 struct line_seg *s;
1421 int sizeof_offset;
1423 sizeof_offset = out_header (line_seg, &exp);
1424 line_end = exp.X_add_symbol;
1426 /* Version. */
1427 out_two (DWARF2_VERSION);
1429 /* Length of the prologue following this length. */
1430 prologue_end = symbol_temp_make ();
1431 exp.X_add_symbol = prologue_end;
1432 exp.X_add_number = - (4 + 2 + 4);
1433 emit_expr (&exp, sizeof_offset);
1435 /* Parameters of the state machine. */
1436 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1437 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1438 out_byte (DWARF2_LINE_BASE);
1439 out_byte (DWARF2_LINE_RANGE);
1440 out_byte (DWARF2_LINE_OPCODE_BASE);
1442 /* Standard opcode lengths. */
1443 out_byte (0); /* DW_LNS_copy */
1444 out_byte (1); /* DW_LNS_advance_pc */
1445 out_byte (1); /* DW_LNS_advance_line */
1446 out_byte (1); /* DW_LNS_set_file */
1447 out_byte (1); /* DW_LNS_set_column */
1448 out_byte (0); /* DW_LNS_negate_stmt */
1449 out_byte (0); /* DW_LNS_set_basic_block */
1450 out_byte (0); /* DW_LNS_const_add_pc */
1451 out_byte (1); /* DW_LNS_fixed_advance_pc */
1452 out_byte (0); /* DW_LNS_set_prologue_end */
1453 out_byte (0); /* DW_LNS_set_epilogue_begin */
1454 out_byte (1); /* DW_LNS_set_isa */
1456 out_file_list ();
1458 symbol_set_value_now (prologue_end);
1460 /* For each section, emit a statement program. */
1461 for (s = all_segs; s; s = s->next)
1462 if (SEG_NORMAL (s->seg))
1463 process_entries (s->seg, s->head->head);
1464 else
1465 as_warn ("dwarf line number information for %s ignored",
1466 segment_name (s->seg));
1468 symbol_set_value_now (line_end);
1471 static void
1472 out_debug_ranges (segT ranges_seg)
1474 unsigned int addr_size = sizeof_address;
1475 struct line_seg *s;
1476 expressionS exp;
1477 unsigned int i;
1479 subseg_set (ranges_seg, 0);
1481 /* Base Address Entry. */
1482 for (i = 0; i < addr_size; i++)
1483 out_byte (0xff);
1484 for (i = 0; i < addr_size; i++)
1485 out_byte (0);
1487 /* Range List Entry. */
1488 for (s = all_segs; s; s = s->next)
1490 fragS *frag;
1491 symbolS *beg, *end;
1493 frag = first_frag_for_seg (s->seg);
1494 beg = symbol_temp_new (s->seg, 0, frag);
1495 s->text_start = beg;
1497 frag = last_frag_for_seg (s->seg);
1498 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1499 s->text_end = end;
1501 exp.X_op = O_symbol;
1502 exp.X_add_symbol = beg;
1503 exp.X_add_number = 0;
1504 emit_expr (&exp, addr_size);
1506 exp.X_op = O_symbol;
1507 exp.X_add_symbol = end;
1508 exp.X_add_number = 0;
1509 emit_expr (&exp, addr_size);
1512 /* End of Range Entry. */
1513 for (i = 0; i < addr_size; i++)
1514 out_byte (0);
1515 for (i = 0; i < addr_size; i++)
1516 out_byte (0);
1519 /* Emit data for .debug_aranges. */
1521 static void
1522 out_debug_aranges (segT aranges_seg, segT info_seg)
1524 unsigned int addr_size = sizeof_address;
1525 struct line_seg *s;
1526 expressionS exp;
1527 symbolS *aranges_end;
1528 char *p;
1529 int sizeof_offset;
1531 sizeof_offset = out_header (aranges_seg, &exp);
1532 aranges_end = exp.X_add_symbol;
1534 /* Version. */
1535 out_two (DWARF2_VERSION);
1537 /* Offset to .debug_info. */
1538 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset);
1540 /* Size of an address (offset portion). */
1541 out_byte (addr_size);
1543 /* Size of a segment descriptor. */
1544 out_byte (0);
1546 /* Align the header. */
1547 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1549 for (s = all_segs; s; s = s->next)
1551 fragS *frag;
1552 symbolS *beg, *end;
1554 frag = first_frag_for_seg (s->seg);
1555 beg = symbol_temp_new (s->seg, 0, frag);
1556 s->text_start = beg;
1558 frag = last_frag_for_seg (s->seg);
1559 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1560 s->text_end = end;
1562 exp.X_op = O_symbol;
1563 exp.X_add_symbol = beg;
1564 exp.X_add_number = 0;
1565 emit_expr (&exp, addr_size);
1567 exp.X_op = O_subtract;
1568 exp.X_add_symbol = end;
1569 exp.X_op_symbol = beg;
1570 exp.X_add_number = 0;
1571 emit_expr (&exp, addr_size);
1574 p = frag_more (2 * addr_size);
1575 md_number_to_chars (p, 0, addr_size);
1576 md_number_to_chars (p + addr_size, 0, addr_size);
1578 symbol_set_value_now (aranges_end);
1581 /* Emit data for .debug_abbrev. Note that this must be kept in
1582 sync with out_debug_info below. */
1584 static void
1585 out_debug_abbrev (segT abbrev_seg,
1586 segT info_seg ATTRIBUTE_UNUSED,
1587 segT line_seg ATTRIBUTE_UNUSED)
1589 subseg_set (abbrev_seg, 0);
1591 out_uleb128 (1);
1592 out_uleb128 (DW_TAG_compile_unit);
1593 out_byte (DW_CHILDREN_no);
1594 if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit)
1595 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1596 else
1597 out_abbrev (DW_AT_stmt_list, DW_FORM_data8);
1598 if (all_segs->next == NULL)
1600 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1601 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1603 else
1605 if (DWARF2_FORMAT (info_seg) == dwarf2_format_32bit)
1606 out_abbrev (DW_AT_ranges, DW_FORM_data4);
1607 else
1608 out_abbrev (DW_AT_ranges, DW_FORM_data8);
1610 out_abbrev (DW_AT_name, DW_FORM_string);
1611 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1612 out_abbrev (DW_AT_producer, DW_FORM_string);
1613 out_abbrev (DW_AT_language, DW_FORM_data2);
1614 out_abbrev (0, 0);
1616 /* Terminate the abbreviations for this compilation unit. */
1617 out_byte (0);
1620 /* Emit a description of this compilation unit for .debug_info. */
1622 static void
1623 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg)
1625 char producer[128];
1626 const char *comp_dir;
1627 const char *dirname;
1628 expressionS exp;
1629 symbolS *info_end;
1630 char *p;
1631 int len;
1632 int sizeof_offset;
1634 sizeof_offset = out_header (info_seg, &exp);
1635 info_end = exp.X_add_symbol;
1637 /* DWARF version. */
1638 out_two (DWARF2_VERSION);
1640 /* .debug_abbrev offset */
1641 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1643 /* Target address size. */
1644 out_byte (sizeof_address);
1646 /* DW_TAG_compile_unit DIE abbrev */
1647 out_uleb128 (1);
1649 /* DW_AT_stmt_list */
1650 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg),
1651 (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit
1652 ? 4 : 8));
1654 /* These two attributes are emitted if all of the code is contiguous. */
1655 if (all_segs->next == NULL)
1657 /* DW_AT_low_pc */
1658 exp.X_op = O_symbol;
1659 exp.X_add_symbol = all_segs->text_start;
1660 exp.X_add_number = 0;
1661 emit_expr (&exp, sizeof_address);
1663 /* DW_AT_high_pc */
1664 exp.X_op = O_symbol;
1665 exp.X_add_symbol = all_segs->text_end;
1666 exp.X_add_number = 0;
1667 emit_expr (&exp, sizeof_address);
1669 else
1671 /* This attribute is emitted if the code is disjoint. */
1672 /* DW_AT_ranges. */
1673 TC_DWARF2_EMIT_OFFSET (section_symbol (ranges_seg), sizeof_offset);
1676 /* DW_AT_name. We don't have the actual file name that was present
1677 on the command line, so assume files[1] is the main input file.
1678 We're not supposed to get called unless at least one line number
1679 entry was emitted, so this should always be defined. */
1680 if (files_in_use == 0)
1681 abort ();
1682 if (files[1].dir)
1684 dirname = remap_debug_filename (dirs[files[1].dir]);
1685 len = strlen (dirname);
1686 #ifdef TE_VMS
1687 /* Already has trailing slash. */
1688 p = frag_more (len);
1689 memcpy (p, dirname, len);
1690 #else
1691 p = frag_more (len + 1);
1692 memcpy (p, dirname, len);
1693 INSERT_DIR_SEPARATOR (p, len);
1694 #endif
1696 len = strlen (files[1].filename) + 1;
1697 p = frag_more (len);
1698 memcpy (p, files[1].filename, len);
1700 /* DW_AT_comp_dir */
1701 comp_dir = remap_debug_filename (getpwd ());
1702 len = strlen (comp_dir) + 1;
1703 p = frag_more (len);
1704 memcpy (p, comp_dir, len);
1706 /* DW_AT_producer */
1707 sprintf (producer, "GNU AS %s", VERSION);
1708 len = strlen (producer) + 1;
1709 p = frag_more (len);
1710 memcpy (p, producer, len);
1712 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1713 dwarf2 draft has no standard code for assembler. */
1714 out_two (DW_LANG_Mips_Assembler);
1716 symbol_set_value_now (info_end);
1719 void
1720 dwarf2_init (void)
1722 all_segs_hash = hash_new ();
1723 last_seg_ptr = &all_segs;
1727 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1728 were any .file/.loc directives, or --gdwarf2 was given, or if the
1729 file has a non-empty .debug_info section and an empty .debug_line
1730 section. If we emit .debug_line, and the .debug_info section is
1731 empty, we also emit .debug_info, .debug_aranges and .debug_abbrev.
1732 ALL_SEGS will be non-null if there were any .file/.loc directives,
1733 or --gdwarf2 was given and there were any located instructions
1734 emitted. */
1736 void
1737 dwarf2_finish (void)
1739 segT line_seg;
1740 struct line_seg *s;
1741 segT info_seg;
1742 int emit_other_sections = 0;
1743 int empty_debug_line = 0;
1745 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
1746 emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
1748 line_seg = bfd_get_section_by_name (stdoutput, ".debug_line");
1749 empty_debug_line = line_seg == NULL || !seg_not_empty_p (line_seg);
1751 /* We can't construct a new debug_line section if we already have one.
1752 Give an error. */
1753 if (all_segs && !empty_debug_line)
1754 as_fatal ("duplicate .debug_line sections");
1756 if ((!all_segs && emit_other_sections)
1757 || (!emit_other_sections && !empty_debug_line))
1758 /* If there is no line information and no non-empty .debug_info
1759 section, or if there is both a non-empty .debug_info and a non-empty
1760 .debug_line, then we do nothing. */
1761 return;
1763 /* Calculate the size of an address for the target machine. */
1764 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1766 /* Create and switch to the line number section. */
1767 line_seg = subseg_new (".debug_line", 0);
1768 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
1770 /* For each subsection, chain the debug entries together. */
1771 for (s = all_segs; s; s = s->next)
1773 struct line_subseg *lss = s->head;
1774 struct line_entry **ptail = lss->ptail;
1776 while ((lss = lss->next) != NULL)
1778 *ptail = lss->head;
1779 ptail = lss->ptail;
1783 out_debug_line (line_seg);
1785 /* If this is assembler generated line info, and there is no
1786 debug_info already, we need .debug_info and .debug_abbrev
1787 sections as well. */
1788 if (emit_other_sections)
1790 segT abbrev_seg;
1791 segT aranges_seg;
1792 segT ranges_seg;
1794 gas_assert (all_segs);
1796 info_seg = subseg_new (".debug_info", 0);
1797 abbrev_seg = subseg_new (".debug_abbrev", 0);
1798 aranges_seg = subseg_new (".debug_aranges", 0);
1800 bfd_set_section_flags (stdoutput, info_seg,
1801 SEC_READONLY | SEC_DEBUGGING);
1802 bfd_set_section_flags (stdoutput, abbrev_seg,
1803 SEC_READONLY | SEC_DEBUGGING);
1804 bfd_set_section_flags (stdoutput, aranges_seg,
1805 SEC_READONLY | SEC_DEBUGGING);
1807 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1809 if (all_segs->next == NULL)
1810 ranges_seg = NULL;
1811 else
1813 ranges_seg = subseg_new (".debug_ranges", 0);
1814 bfd_set_section_flags (stdoutput, ranges_seg,
1815 SEC_READONLY | SEC_DEBUGGING);
1816 record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1);
1817 out_debug_ranges (ranges_seg);
1820 out_debug_aranges (aranges_seg, info_seg);
1821 out_debug_abbrev (abbrev_seg, info_seg, line_seg);
1822 out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg);