2008-04-23 Mike Frysinger <vapier@gentoo.org>
[binutils.git] / gas / dwarf2dbg.c
blob220a17585ecabbea4462ea3d89bfe902134370d6
1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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]
31 #include "as.h"
32 #include "safe-ctype.h"
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
45 #include "dwarf2dbg.h"
46 #include <filenames.h>
48 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
49 /* We need to decide which character to use as a directory separator.
50 Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not
51 necessarily mean that the backslash character is the one to use.
52 Some environments, eg Cygwin, can support both naming conventions.
53 So we use the heuristic that we only need to use the backslash if
54 the path is an absolute path starting with a DOS style drive
55 selector. eg C: or D: */
56 # define INSERT_DIR_SEPARATOR(string, offset) \
57 do \
58 { \
59 if (offset > 1 \
60 && string[0] != 0 \
61 && string[1] == ':') \
62 string [offset] = '\\'; \
63 else \
64 string [offset] = '/'; \
65 } \
66 while (0)
67 #else
68 # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
69 #endif
71 #ifndef DWARF2_FORMAT
72 # define DWARF2_FORMAT() dwarf2_format_32bit
73 #endif
75 #ifndef DWARF2_ADDR_SIZE
76 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
77 #endif
79 #include "subsegs.h"
81 #include "elf/dwarf2.h"
83 /* Since we can't generate the prolog until the body is complete, we
84 use three different subsegments for .debug_line: one holding the
85 prolog, one for the directory and filename info, and one for the
86 body ("statement program"). */
87 #define DL_PROLOG 0
88 #define DL_FILES 1
89 #define DL_BODY 2
91 /* If linker relaxation might change offsets in the code, the DWARF special
92 opcodes and variable-length operands cannot be used. If this macro is
93 nonzero, use the DW_LNS_fixed_advance_pc opcode instead. */
94 #ifndef DWARF2_USE_FIXED_ADVANCE_PC
95 # define DWARF2_USE_FIXED_ADVANCE_PC 0
96 #endif
98 /* First special line opcde - leave room for the standard opcodes.
99 Note: If you want to change this, you'll have to update the
100 "standard_opcode_lengths" table that is emitted below in
101 out_debug_line(). */
102 #define DWARF2_LINE_OPCODE_BASE 13
104 #ifndef DWARF2_LINE_BASE
105 /* Minimum line offset in a special line info. opcode. This value
106 was chosen to give a reasonable range of values. */
107 # define DWARF2_LINE_BASE -5
108 #endif
110 /* Range of line offsets in a special line info. opcode. */
111 #ifndef DWARF2_LINE_RANGE
112 # define DWARF2_LINE_RANGE 14
113 #endif
115 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
116 /* Define the architecture-dependent minimum instruction length (in
117 bytes). This value should be rather too small than too big. */
118 # define DWARF2_LINE_MIN_INSN_LENGTH 1
119 #endif
121 /* Flag that indicates the initial value of the is_stmt_start flag. */
122 #define DWARF2_LINE_DEFAULT_IS_STMT 1
124 /* Given a special op, return the line skip amount. */
125 #define SPECIAL_LINE(op) \
126 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
128 /* Given a special op, return the address skip amount (in units of
129 DWARF2_LINE_MIN_INSN_LENGTH. */
130 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
132 /* The maximum address skip amount that can be encoded with a special op. */
133 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
135 struct line_entry {
136 struct line_entry *next;
137 symbolS *label;
138 struct dwarf2_line_info loc;
141 struct line_subseg {
142 struct line_subseg *next;
143 subsegT subseg;
144 struct line_entry *head;
145 struct line_entry **ptail;
148 struct line_seg {
149 struct line_seg *next;
150 segT seg;
151 struct line_subseg *head;
152 symbolS *text_start;
153 symbolS *text_end;
156 /* Collects data for all line table entries during assembly. */
157 static struct line_seg *all_segs;
159 struct file_entry {
160 const char *filename;
161 unsigned int dir;
164 /* Table of files used by .debug_line. */
165 static struct file_entry *files;
166 static unsigned int files_in_use;
167 static unsigned int files_allocated;
169 /* Table of directories used by .debug_line. */
170 static char **dirs;
171 static unsigned int dirs_in_use;
172 static unsigned int dirs_allocated;
174 /* TRUE when we've seen a .loc directive recently. Used to avoid
175 doing work when there's nothing to do. */
176 bfd_boolean dwarf2_loc_directive_seen;
178 /* TRUE when we're supposed to set the basic block mark whenever a
179 label is seen. */
180 bfd_boolean dwarf2_loc_mark_labels;
182 /* Current location as indicated by the most recent .loc directive. */
183 static struct dwarf2_line_info current = {
184 1, 1, 0, 0,
185 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0
188 /* The size of an address on the target. */
189 static unsigned int sizeof_address;
191 static struct line_subseg *get_line_subseg (segT, subsegT);
192 static unsigned int get_filenum (const char *, unsigned int);
193 static struct frag *first_frag_for_seg (segT);
194 static struct frag *last_frag_for_seg (segT);
195 static void out_byte (int);
196 static void out_opcode (int);
197 static void out_two (int);
198 static void out_four (int);
199 static void out_abbrev (int, int);
200 static void out_uleb128 (addressT);
201 static offsetT get_frag_fix (fragS *, segT);
202 static void out_set_addr (symbolS *);
203 static int size_inc_line_addr (int, addressT);
204 static void emit_inc_line_addr (int, addressT, char *, int);
205 static int size_fixed_inc_line_addr (int, addressT);
206 static void emit_fixed_inc_line_addr (int, addressT, fragS *, char *, int);
207 static void out_inc_line_addr (int, addressT);
208 static void relax_inc_line_addr (int, symbolS *, symbolS *);
209 static void process_entries (segT, struct line_entry *);
210 static void out_file_list (void);
211 static void out_debug_line (segT);
212 static void out_debug_aranges (segT, segT);
213 static void out_debug_abbrev (segT);
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 expr;
225 expr.X_op = O_symbol;
226 expr.X_add_symbol = symbol;
227 expr.X_add_number = 0;
228 emit_expr (&expr, 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 **ps, *s;
242 struct line_subseg **pss, *ss;
244 if (seg == last_seg && subseg == last_subseg)
245 return last_line_subseg;
247 for (ps = &all_segs; (s = *ps) != NULL; ps = &s->next)
248 if (s->seg == seg)
249 goto found_seg;
251 s = (struct line_seg *) xmalloc (sizeof (*s));
252 s->next = NULL;
253 s->seg = seg;
254 s->head = NULL;
255 *ps = s;
257 found_seg:
258 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
260 if (ss->subseg == subseg)
261 goto found_subseg;
262 if (ss->subseg > subseg)
263 break;
266 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
267 ss->next = *pss;
268 ss->subseg = subseg;
269 ss->head = NULL;
270 ss->ptail = &ss->head;
271 *pss = ss;
273 found_subseg:
274 last_seg = seg;
275 last_subseg = subseg;
276 last_line_subseg = ss;
278 return ss;
281 /* Record an entry for LOC occurring at LABEL. */
283 static void
284 dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
286 struct line_subseg *ss;
287 struct line_entry *e;
289 e = (struct line_entry *) xmalloc (sizeof (*e));
290 e->next = NULL;
291 e->label = label;
292 e->loc = *loc;
294 ss = get_line_subseg (now_seg, now_subseg);
295 *ss->ptail = e;
296 ss->ptail = &e->next;
299 /* Record an entry for LOC occurring at OFS within the current fragment. */
301 void
302 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
304 static unsigned int line = -1;
305 static unsigned int filenum = -1;
307 symbolS *sym;
309 /* Early out for as-yet incomplete location information. */
310 if (loc->filenum == 0 || loc->line == 0)
311 return;
313 /* Don't emit sequences of line symbols for the same line when the
314 symbols apply to assembler code. It is necessary to emit
315 duplicate line symbols when a compiler asks for them, because GDB
316 uses them to determine the end of the prologue. */
317 if (debug_type == DEBUG_DWARF2
318 && line == loc->line && filenum == loc->filenum)
319 return;
321 line = loc->line;
322 filenum = loc->filenum;
324 sym = symbol_temp_new (now_seg, ofs, frag_now);
325 dwarf2_gen_line_info_1 (sym, loc);
328 /* Returns the current source information. If .file directives have
329 been encountered, the info for the corresponding source file is
330 returned. Otherwise, the info for the assembly source file is
331 returned. */
333 void
334 dwarf2_where (struct dwarf2_line_info *line)
336 if (debug_type == DEBUG_DWARF2)
338 char *filename;
339 as_where (&filename, &line->line);
340 line->filenum = get_filenum (filename, 0);
341 line->column = 0;
342 line->flags = DWARF2_FLAG_IS_STMT;
343 line->isa = current.isa;
345 else
346 *line = current;
349 /* A hook to allow the target backend to inform the line number state
350 machine of isa changes when assembler debug info is enabled. */
352 void
353 dwarf2_set_isa (unsigned int isa)
355 current.isa = isa;
358 /* Called for each machine instruction, or relatively atomic group of
359 machine instructions (ie built-in macro). The instruction or group
360 is SIZE bytes in length. If dwarf2 line number generation is called
361 for, emit a line statement appropriately. */
363 void
364 dwarf2_emit_insn (int size)
366 struct dwarf2_line_info loc;
368 if (!dwarf2_loc_directive_seen && debug_type != DEBUG_DWARF2)
369 return;
371 dwarf2_where (&loc);
373 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
374 dwarf2_consume_line_info ();
377 /* Called after the current line information has been either used with
378 dwarf2_gen_line_info or saved with a machine instruction for later use.
379 This resets the state of the line number information to reflect that
380 it has been used. */
382 void
383 dwarf2_consume_line_info (void)
385 /* Unless we generate DWARF2 debugging information for each
386 assembler line, we only emit one line symbol for one LOC. */
387 dwarf2_loc_directive_seen = FALSE;
389 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
390 | DWARF2_FLAG_PROLOGUE_END
391 | DWARF2_FLAG_EPILOGUE_BEGIN);
394 /* Called for each (preferably code) label. If dwarf2_loc_mark_labels
395 is enabled, emit a basic block marker. */
397 void
398 dwarf2_emit_label (symbolS *label)
400 struct dwarf2_line_info loc;
402 if (!dwarf2_loc_mark_labels)
403 return;
404 if (S_GET_SEGMENT (label) != now_seg)
405 return;
406 if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE))
407 return;
408 if (files_in_use == 0 && debug_type != DEBUG_DWARF2)
409 return;
411 dwarf2_where (&loc);
413 loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
415 dwarf2_gen_line_info_1 (label, &loc);
416 dwarf2_consume_line_info ();
419 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
420 allocate it on that file table slot, otherwise return the first
421 empty one. */
423 static unsigned int
424 get_filenum (const char *filename, unsigned int num)
426 static unsigned int last_used, last_used_dir_len;
427 const char *file;
428 size_t dir_len;
429 unsigned int i, dir;
431 if (num == 0 && last_used)
433 if (! files[last_used].dir
434 && strcmp (filename, files[last_used].filename) == 0)
435 return last_used;
436 if (files[last_used].dir
437 && strncmp (filename, dirs[files[last_used].dir],
438 last_used_dir_len) == 0
439 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
440 && strcmp (filename + last_used_dir_len + 1,
441 files[last_used].filename) == 0)
442 return last_used;
445 file = lbasename (filename);
446 /* Don't make empty string from / or A: from A:/ . */
447 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
448 if (file <= filename + 3)
449 file = filename;
450 #else
451 if (file == filename + 1)
452 file = filename;
453 #endif
454 dir_len = file - filename;
456 dir = 0;
457 if (dir_len)
459 --dir_len;
460 for (dir = 1; dir < dirs_in_use; ++dir)
461 if (strncmp (filename, dirs[dir], dir_len) == 0
462 && dirs[dir][dir_len] == '\0')
463 break;
465 if (dir >= dirs_in_use)
467 if (dir >= dirs_allocated)
469 dirs_allocated = dir + 32;
470 dirs = (char **)
471 xrealloc (dirs, (dir + 32) * sizeof (const char *));
474 dirs[dir] = xmalloc (dir_len + 1);
475 memcpy (dirs[dir], filename, dir_len);
476 dirs[dir][dir_len] = '\0';
477 dirs_in_use = dir + 1;
481 if (num == 0)
483 for (i = 1; i < files_in_use; ++i)
484 if (files[i].dir == dir
485 && files[i].filename
486 && strcmp (file, files[i].filename) == 0)
488 last_used = i;
489 last_used_dir_len = dir_len;
490 return i;
493 else
494 i = num;
496 if (i >= files_allocated)
498 unsigned int old = files_allocated;
500 files_allocated = i + 32;
501 files = (struct file_entry *)
502 xrealloc (files, (i + 32) * sizeof (struct file_entry));
504 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
507 files[i].filename = num ? file : xstrdup (file);
508 files[i].dir = dir;
509 if (files_in_use < i + 1)
510 files_in_use = i + 1;
511 last_used = i;
512 last_used_dir_len = dir_len;
514 return i;
517 /* Handle two forms of .file directive:
518 - Pass .file "source.c" to s_app_file
519 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
521 If an entry is added to the file table, return a pointer to the filename. */
523 char *
524 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
526 offsetT num;
527 char *filename;
528 int filename_len;
530 /* Continue to accept a bare string and pass it off. */
531 SKIP_WHITESPACE ();
532 if (*input_line_pointer == '"')
534 s_app_file (0);
535 return NULL;
538 num = get_absolute_expression ();
539 filename = demand_copy_C_string (&filename_len);
540 if (filename == NULL)
541 return NULL;
542 demand_empty_rest_of_line ();
544 if (num < 1)
546 as_bad (_("file number less than one"));
547 return NULL;
550 if (num < (int) files_in_use && files[num].filename != 0)
552 as_bad (_("file number %ld already allocated"), (long) num);
553 return NULL;
556 get_filenum (filename, num);
558 return filename;
561 void
562 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
564 offsetT filenum, line;
566 /* If we see two .loc directives in a row, force the first one to be
567 output now. */
568 if (dwarf2_loc_directive_seen && debug_type != DEBUG_DWARF2)
569 dwarf2_emit_insn (0);
571 filenum = get_absolute_expression ();
572 SKIP_WHITESPACE ();
573 line = get_absolute_expression ();
575 if (filenum < 1)
577 as_bad (_("file number less than one"));
578 return;
580 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
582 as_bad (_("unassigned file number %ld"), (long) filenum);
583 return;
586 current.filenum = filenum;
587 current.line = line;
589 #ifndef NO_LISTING
590 if (listing)
592 if (files[filenum].dir)
594 size_t dir_len = strlen (dirs[files[filenum].dir]);
595 size_t file_len = strlen (files[filenum].filename);
596 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
598 memcpy (cp, dirs[files[filenum].dir], dir_len);
599 INSERT_DIR_SEPARATOR (cp, dir_len);
600 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
601 cp[dir_len + file_len + 1] = '\0';
602 listing_source_file (cp);
604 else
605 listing_source_file (files[filenum].filename);
606 listing_source_line (line);
608 #endif
610 SKIP_WHITESPACE ();
611 if (ISDIGIT (*input_line_pointer))
613 current.column = get_absolute_expression ();
614 SKIP_WHITESPACE ();
617 while (ISALPHA (*input_line_pointer))
619 char *p, c;
620 offsetT value;
622 p = input_line_pointer;
623 c = get_symbol_end ();
625 if (strcmp (p, "basic_block") == 0)
627 current.flags |= DWARF2_FLAG_BASIC_BLOCK;
628 *input_line_pointer = c;
630 else if (strcmp (p, "prologue_end") == 0)
632 current.flags |= DWARF2_FLAG_PROLOGUE_END;
633 *input_line_pointer = c;
635 else if (strcmp (p, "epilogue_begin") == 0)
637 current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
638 *input_line_pointer = c;
640 else if (strcmp (p, "is_stmt") == 0)
642 *input_line_pointer = c;
643 value = get_absolute_expression ();
644 if (value == 0)
645 current.flags &= ~DWARF2_FLAG_IS_STMT;
646 else if (value == 1)
647 current.flags |= DWARF2_FLAG_IS_STMT;
648 else
650 as_bad (_("is_stmt value not 0 or 1"));
651 return;
654 else if (strcmp (p, "isa") == 0)
656 *input_line_pointer = c;
657 value = get_absolute_expression ();
658 if (value >= 0)
659 current.isa = value;
660 else
662 as_bad (_("isa number less than zero"));
663 return;
666 else
668 as_bad (_("unknown .loc sub-directive `%s'"), p);
669 *input_line_pointer = c;
670 return;
673 SKIP_WHITESPACE ();
676 demand_empty_rest_of_line ();
677 dwarf2_loc_directive_seen = TRUE;
680 void
681 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
683 offsetT value = get_absolute_expression ();
685 if (value != 0 && value != 1)
687 as_bad (_("expected 0 or 1"));
688 ignore_rest_of_line ();
690 else
692 dwarf2_loc_mark_labels = value != 0;
693 demand_empty_rest_of_line ();
697 static struct frag *
698 first_frag_for_seg (segT seg)
700 return seg_info (seg)->frchainP->frch_root;
703 static struct frag *
704 last_frag_for_seg (segT seg)
706 frchainS *f = seg_info (seg)->frchainP;
708 while (f->frch_next != NULL)
709 f = f->frch_next;
711 return f->frch_last;
714 /* Emit a single byte into the current segment. */
716 static inline void
717 out_byte (int byte)
719 FRAG_APPEND_1_CHAR (byte);
722 /* Emit a statement program opcode into the current segment. */
724 static inline void
725 out_opcode (int opc)
727 out_byte (opc);
730 /* Emit a two-byte word into the current segment. */
732 static inline void
733 out_two (int data)
735 md_number_to_chars (frag_more (2), data, 2);
738 /* Emit a four byte word into the current segment. */
740 static inline void
741 out_four (int data)
743 md_number_to_chars (frag_more (4), data, 4);
746 /* Emit an unsigned "little-endian base 128" number. */
748 static void
749 out_uleb128 (addressT value)
751 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
754 /* Emit a tuple for .debug_abbrev. */
756 static inline void
757 out_abbrev (int name, int form)
759 out_uleb128 (name);
760 out_uleb128 (form);
763 /* Get the size of a fragment. */
765 static offsetT
766 get_frag_fix (fragS *frag, segT seg)
768 frchainS *fr;
770 if (frag->fr_next)
771 return frag->fr_fix;
773 /* If a fragment is the last in the chain, special measures must be
774 taken to find its size before relaxation, since it may be pending
775 on some subsegment chain. */
776 for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
777 if (fr->frch_last == frag)
778 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
780 abort ();
783 /* Set an absolute address (may result in a relocation entry). */
785 static void
786 out_set_addr (symbolS *sym)
788 expressionS expr;
790 out_opcode (DW_LNS_extended_op);
791 out_uleb128 (sizeof_address + 1);
793 out_opcode (DW_LNE_set_address);
794 expr.X_op = O_symbol;
795 expr.X_add_symbol = sym;
796 expr.X_add_number = 0;
797 emit_expr (&expr, sizeof_address);
800 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
801 static void scale_addr_delta (addressT *);
803 static void
804 scale_addr_delta (addressT *addr_delta)
806 static int printed_this = 0;
807 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
809 if (!printed_this)
810 as_bad("unaligned opcodes detected in executable segment");
811 printed_this = 1;
813 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
815 #else
816 #define scale_addr_delta(A)
817 #endif
819 /* Encode a pair of line and address skips as efficiently as possible.
820 Note that the line skip is signed, whereas the address skip is unsigned.
822 The following two routines *must* be kept in sync. This is
823 enforced by making emit_inc_line_addr abort if we do not emit
824 exactly the expected number of bytes. */
826 static int
827 size_inc_line_addr (int line_delta, addressT addr_delta)
829 unsigned int tmp, opcode;
830 int len = 0;
832 /* Scale the address delta by the minimum instruction length. */
833 scale_addr_delta (&addr_delta);
835 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
836 We cannot use special opcodes here, since we want the end_sequence
837 to emit the matrix entry. */
838 if (line_delta == INT_MAX)
840 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
841 len = 1;
842 else
843 len = 1 + sizeof_leb128 (addr_delta, 0);
844 return len + 3;
847 /* Bias the line delta by the base. */
848 tmp = line_delta - DWARF2_LINE_BASE;
850 /* If the line increment is out of range of a special opcode, we
851 must encode it with DW_LNS_advance_line. */
852 if (tmp >= DWARF2_LINE_RANGE)
854 len = 1 + sizeof_leb128 (line_delta, 1);
855 line_delta = 0;
856 tmp = 0 - DWARF2_LINE_BASE;
859 /* Bias the opcode by the special opcode base. */
860 tmp += DWARF2_LINE_OPCODE_BASE;
862 /* Avoid overflow when addr_delta is large. */
863 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
865 /* Try using a special opcode. */
866 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
867 if (opcode <= 255)
868 return len + 1;
870 /* Try using DW_LNS_const_add_pc followed by special op. */
871 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
872 if (opcode <= 255)
873 return len + 2;
876 /* Otherwise use DW_LNS_advance_pc. */
877 len += 1 + sizeof_leb128 (addr_delta, 0);
879 /* DW_LNS_copy or special opcode. */
880 len += 1;
882 return len;
885 static void
886 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
888 unsigned int tmp, opcode;
889 int need_copy = 0;
890 char *end = p + len;
892 /* Line number sequences cannot go backward in addresses. This means
893 we've incorrectly ordered the statements in the sequence. */
894 assert ((offsetT) addr_delta >= 0);
896 /* Scale the address delta by the minimum instruction length. */
897 scale_addr_delta (&addr_delta);
899 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
900 We cannot use special opcodes here, since we want the end_sequence
901 to emit the matrix entry. */
902 if (line_delta == INT_MAX)
904 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
905 *p++ = DW_LNS_const_add_pc;
906 else
908 *p++ = DW_LNS_advance_pc;
909 p += output_leb128 (p, addr_delta, 0);
912 *p++ = DW_LNS_extended_op;
913 *p++ = 1;
914 *p++ = DW_LNE_end_sequence;
915 goto done;
918 /* Bias the line delta by the base. */
919 tmp = line_delta - DWARF2_LINE_BASE;
921 /* If the line increment is out of range of a special opcode, we
922 must encode it with DW_LNS_advance_line. */
923 if (tmp >= DWARF2_LINE_RANGE)
925 *p++ = DW_LNS_advance_line;
926 p += output_leb128 (p, line_delta, 1);
928 line_delta = 0;
929 tmp = 0 - DWARF2_LINE_BASE;
930 need_copy = 1;
933 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
934 special opcode. */
935 if (line_delta == 0 && addr_delta == 0)
937 *p++ = DW_LNS_copy;
938 goto done;
941 /* Bias the opcode by the special opcode base. */
942 tmp += DWARF2_LINE_OPCODE_BASE;
944 /* Avoid overflow when addr_delta is large. */
945 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
947 /* Try using a special opcode. */
948 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
949 if (opcode <= 255)
951 *p++ = opcode;
952 goto done;
955 /* Try using DW_LNS_const_add_pc followed by special op. */
956 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
957 if (opcode <= 255)
959 *p++ = DW_LNS_const_add_pc;
960 *p++ = opcode;
961 goto done;
965 /* Otherwise use DW_LNS_advance_pc. */
966 *p++ = DW_LNS_advance_pc;
967 p += output_leb128 (p, addr_delta, 0);
969 if (need_copy)
970 *p++ = DW_LNS_copy;
971 else
972 *p++ = tmp;
974 done:
975 assert (p == end);
978 /* Handy routine to combine calls to the above two routines. */
980 static void
981 out_inc_line_addr (int line_delta, addressT addr_delta)
983 int len = size_inc_line_addr (line_delta, addr_delta);
984 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
987 /* Write out an alternative form of line and address skips using
988 DW_LNS_fixed_advance_pc opcodes. This uses more space than the default
989 line and address information, but it is required if linker relaxation
990 could change the code offsets. The following two routines *must* be
991 kept in sync. */
993 static int
994 size_fixed_inc_line_addr (int line_delta, addressT addr_delta)
996 int len = 0;
998 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */
999 if (line_delta != INT_MAX)
1000 len = 1 + sizeof_leb128 (line_delta, 1);
1002 if (addr_delta > 50000)
1004 /* DW_LNS_extended_op */
1005 len += 1 + sizeof_leb128 (sizeof_address + 1, 0);
1006 /* DW_LNE_set_address */
1007 len += 1 + sizeof_address;
1009 else
1010 /* DW_LNS_fixed_advance_pc */
1011 len += 3;
1013 if (line_delta == INT_MAX)
1014 /* DW_LNS_extended_op + DW_LNE_end_sequence */
1015 len += 3;
1016 else
1017 /* DW_LNS_copy */
1018 len += 1;
1020 return len;
1023 static void
1024 emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag,
1025 char *p, int len)
1027 expressionS *exp;
1028 segT line_seg;
1029 char *end = p + len;
1031 /* Line number sequences cannot go backward in addresses. This means
1032 we've incorrectly ordered the statements in the sequence. */
1033 assert ((offsetT) addr_delta >= 0);
1035 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */
1036 if (line_delta != INT_MAX)
1038 *p++ = DW_LNS_advance_line;
1039 p += output_leb128 (p, line_delta, 1);
1042 exp = symbol_get_value_expression (frag->fr_symbol);
1043 line_seg = subseg_get (".debug_line", 0);
1045 /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can
1046 advance the address by at most 64K. Linker relaxation (without
1047 which this function would not be used) could change the operand by
1048 an unknown amount. If the address increment is getting close to
1049 the limit, just reset the address. */
1050 if (addr_delta > 50000)
1052 symbolS *to_sym;
1053 expressionS expr;
1055 assert (exp->X_op = O_subtract);
1056 to_sym = exp->X_add_symbol;
1058 *p++ = DW_LNS_extended_op;
1059 p += output_leb128 (p, sizeof_address + 1, 0);
1060 *p++ = DW_LNE_set_address;
1061 expr.X_op = O_symbol;
1062 expr.X_add_symbol = to_sym;
1063 expr.X_add_number = 0;
1064 subseg_change (line_seg, 0);
1065 emit_expr_fix (&expr, sizeof_address, frag, p);
1066 p += sizeof_address;
1068 else
1070 *p++ = DW_LNS_fixed_advance_pc;
1071 subseg_change (line_seg, 0);
1072 emit_expr_fix (exp, 2, frag, p);
1073 p += 2;
1076 if (line_delta == INT_MAX)
1078 *p++ = DW_LNS_extended_op;
1079 *p++ = 1;
1080 *p++ = DW_LNE_end_sequence;
1082 else
1083 *p++ = DW_LNS_copy;
1085 assert (p == end);
1088 /* Generate a variant frag that we can use to relax address/line
1089 increments between fragments of the target segment. */
1091 static void
1092 relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
1094 expressionS expr;
1095 int max_chars;
1097 expr.X_op = O_subtract;
1098 expr.X_add_symbol = to_sym;
1099 expr.X_op_symbol = from_sym;
1100 expr.X_add_number = 0;
1102 /* The maximum size of the frag is the line delta with a maximum
1103 sized address delta. */
1104 if (DWARF2_USE_FIXED_ADVANCE_PC)
1105 max_chars = size_fixed_inc_line_addr (line_delta,
1106 -DWARF2_LINE_MIN_INSN_LENGTH);
1107 else
1108 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
1110 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
1111 make_expr_symbol (&expr), line_delta, NULL);
1114 /* The function estimates the size of a rs_dwarf2dbg variant frag
1115 based on the current values of the symbols. It is called before
1116 the relaxation loop. We set fr_subtype to the expected length. */
1119 dwarf2dbg_estimate_size_before_relax (fragS *frag)
1121 offsetT addr_delta;
1122 int size;
1124 addr_delta = resolve_symbol_value (frag->fr_symbol);
1125 if (DWARF2_USE_FIXED_ADVANCE_PC)
1126 size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta);
1127 else
1128 size = size_inc_line_addr (frag->fr_offset, addr_delta);
1130 frag->fr_subtype = size;
1132 return size;
1135 /* This function relaxes a rs_dwarf2dbg variant frag based on the
1136 current values of the symbols. fr_subtype is the current length
1137 of the frag. This returns the change in frag length. */
1140 dwarf2dbg_relax_frag (fragS *frag)
1142 int old_size, new_size;
1144 old_size = frag->fr_subtype;
1145 new_size = dwarf2dbg_estimate_size_before_relax (frag);
1147 return new_size - old_size;
1150 /* This function converts a rs_dwarf2dbg variant frag into a normal
1151 fill frag. This is called after all relaxation has been done.
1152 fr_subtype will be the desired length of the frag. */
1154 void
1155 dwarf2dbg_convert_frag (fragS *frag)
1157 offsetT addr_diff;
1159 addr_diff = resolve_symbol_value (frag->fr_symbol);
1161 /* fr_var carries the max_chars that we created the fragment with.
1162 fr_subtype carries the current expected length. We must, of
1163 course, have allocated enough memory earlier. */
1164 assert (frag->fr_var >= (int) frag->fr_subtype);
1166 if (DWARF2_USE_FIXED_ADVANCE_PC)
1167 emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag,
1168 frag->fr_literal + frag->fr_fix,
1169 frag->fr_subtype);
1170 else
1171 emit_inc_line_addr (frag->fr_offset, addr_diff,
1172 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
1174 frag->fr_fix += frag->fr_subtype;
1175 frag->fr_type = rs_fill;
1176 frag->fr_var = 0;
1177 frag->fr_offset = 0;
1180 /* Generate .debug_line content for the chain of line number entries
1181 beginning at E, for segment SEG. */
1183 static void
1184 process_entries (segT seg, struct line_entry *e)
1186 unsigned filenum = 1;
1187 unsigned line = 1;
1188 unsigned column = 0;
1189 unsigned isa = 0;
1190 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1191 fragS *last_frag = NULL, *frag;
1192 addressT last_frag_ofs = 0, frag_ofs;
1193 symbolS *last_lab = NULL, *lab;
1194 struct line_entry *next;
1198 int line_delta;
1200 if (filenum != e->loc.filenum)
1202 filenum = e->loc.filenum;
1203 out_opcode (DW_LNS_set_file);
1204 out_uleb128 (filenum);
1207 if (column != e->loc.column)
1209 column = e->loc.column;
1210 out_opcode (DW_LNS_set_column);
1211 out_uleb128 (column);
1214 if (isa != e->loc.isa)
1216 isa = e->loc.isa;
1217 out_opcode (DW_LNS_set_isa);
1218 out_uleb128 (isa);
1221 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
1223 flags = e->loc.flags;
1224 out_opcode (DW_LNS_negate_stmt);
1227 if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
1228 out_opcode (DW_LNS_set_basic_block);
1230 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
1231 out_opcode (DW_LNS_set_prologue_end);
1233 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1234 out_opcode (DW_LNS_set_epilogue_begin);
1236 /* Don't try to optimize away redundant entries; gdb wants two
1237 entries for a function where the code starts on the same line as
1238 the {, and there's no way to identify that case here. Trust gcc
1239 to optimize appropriately. */
1240 line_delta = e->loc.line - line;
1241 lab = e->label;
1242 frag = symbol_get_frag (lab);
1243 frag_ofs = S_GET_VALUE (lab);
1245 if (last_frag == NULL)
1247 out_set_addr (lab);
1248 out_inc_line_addr (line_delta, 0);
1250 else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1251 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
1252 else
1253 relax_inc_line_addr (line_delta, lab, last_lab);
1255 line = e->loc.line;
1256 last_lab = lab;
1257 last_frag = frag;
1258 last_frag_ofs = frag_ofs;
1260 next = e->next;
1261 free (e);
1262 e = next;
1264 while (e);
1266 /* Emit a DW_LNE_end_sequence for the end of the section. */
1267 frag = last_frag_for_seg (seg);
1268 frag_ofs = get_frag_fix (frag, seg);
1269 if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1270 out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
1271 else
1273 lab = symbol_temp_new (seg, frag_ofs, frag);
1274 relax_inc_line_addr (INT_MAX, lab, last_lab);
1278 /* Emit the directory and file tables for .debug_line. */
1280 static void
1281 out_file_list (void)
1283 size_t size;
1284 const char *dir;
1285 char *cp;
1286 unsigned int i;
1288 /* Emit directory list. */
1289 for (i = 1; i < dirs_in_use; ++i)
1291 dir = remap_debug_filename (dirs[i]);
1292 size = strlen (dir) + 1;
1293 cp = frag_more (size);
1294 memcpy (cp, dir, size);
1296 /* Terminate it. */
1297 out_byte ('\0');
1299 for (i = 1; i < files_in_use; ++i)
1301 if (files[i].filename == NULL)
1303 as_bad (_("unassigned file number %ld"), (long) i);
1304 /* Prevent a crash later, particularly for file 1. */
1305 files[i].filename = "";
1306 continue;
1309 size = strlen (files[i].filename) + 1;
1310 cp = frag_more (size);
1311 memcpy (cp, files[i].filename, size);
1313 out_uleb128 (files[i].dir); /* directory number */
1314 out_uleb128 (0); /* last modification timestamp */
1315 out_uleb128 (0); /* filesize */
1318 /* Terminate filename list. */
1319 out_byte (0);
1322 /* Emit the collected .debug_line data. */
1324 static void
1325 out_debug_line (segT line_seg)
1327 expressionS expr;
1328 symbolS *line_start;
1329 symbolS *prologue_end;
1330 symbolS *line_end;
1331 struct line_seg *s;
1332 enum dwarf2_format d2f;
1333 int sizeof_offset;
1335 subseg_set (line_seg, 0);
1337 line_start = symbol_temp_new_now ();
1338 prologue_end = symbol_temp_make ();
1339 line_end = symbol_temp_make ();
1341 /* Total length of the information for this compilation unit. */
1342 expr.X_op = O_subtract;
1343 expr.X_add_symbol = line_end;
1344 expr.X_op_symbol = line_start;
1346 d2f = DWARF2_FORMAT ();
1347 if (d2f == dwarf2_format_32bit)
1349 expr.X_add_number = -4;
1350 emit_expr (&expr, 4);
1351 sizeof_offset = 4;
1353 else if (d2f == dwarf2_format_64bit)
1355 expr.X_add_number = -12;
1356 out_four (-1);
1357 emit_expr (&expr, 8);
1358 sizeof_offset = 8;
1360 else if (d2f == dwarf2_format_64bit_irix)
1362 expr.X_add_number = -8;
1363 emit_expr (&expr, 8);
1364 sizeof_offset = 8;
1366 else
1368 as_fatal (_("internal error: unknown dwarf2 format"));
1371 /* Version. */
1372 out_two (2);
1374 /* Length of the prologue following this length. */
1375 expr.X_op = O_subtract;
1376 expr.X_add_symbol = prologue_end;
1377 expr.X_op_symbol = line_start;
1378 expr.X_add_number = - (4 + 2 + 4);
1379 emit_expr (&expr, sizeof_offset);
1381 /* Parameters of the state machine. */
1382 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1383 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1384 out_byte (DWARF2_LINE_BASE);
1385 out_byte (DWARF2_LINE_RANGE);
1386 out_byte (DWARF2_LINE_OPCODE_BASE);
1388 /* Standard opcode lengths. */
1389 out_byte (0); /* DW_LNS_copy */
1390 out_byte (1); /* DW_LNS_advance_pc */
1391 out_byte (1); /* DW_LNS_advance_line */
1392 out_byte (1); /* DW_LNS_set_file */
1393 out_byte (1); /* DW_LNS_set_column */
1394 out_byte (0); /* DW_LNS_negate_stmt */
1395 out_byte (0); /* DW_LNS_set_basic_block */
1396 out_byte (0); /* DW_LNS_const_add_pc */
1397 out_byte (1); /* DW_LNS_fixed_advance_pc */
1398 out_byte (0); /* DW_LNS_set_prologue_end */
1399 out_byte (0); /* DW_LNS_set_epilogue_begin */
1400 out_byte (1); /* DW_LNS_set_isa */
1402 out_file_list ();
1404 symbol_set_value_now (prologue_end);
1406 /* For each section, emit a statement program. */
1407 for (s = all_segs; s; s = s->next)
1408 process_entries (s->seg, s->head->head);
1410 symbol_set_value_now (line_end);
1413 static void
1414 out_debug_ranges (segT ranges_seg)
1416 unsigned int addr_size = sizeof_address;
1417 struct line_seg *s;
1418 expressionS expr;
1419 unsigned int i;
1421 subseg_set (ranges_seg, 0);
1423 /* Base Address Entry. */
1424 for (i = 0; i < addr_size; i++)
1425 out_byte (0xff);
1426 for (i = 0; i < addr_size; i++)
1427 out_byte (0);
1429 /* Range List Entry. */
1430 for (s = all_segs; s; s = s->next)
1432 fragS *frag;
1433 symbolS *beg, *end;
1435 frag = first_frag_for_seg (s->seg);
1436 beg = symbol_temp_new (s->seg, 0, frag);
1437 s->text_start = beg;
1439 frag = last_frag_for_seg (s->seg);
1440 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1441 s->text_end = end;
1443 expr.X_op = O_symbol;
1444 expr.X_add_symbol = beg;
1445 expr.X_add_number = 0;
1446 emit_expr (&expr, addr_size);
1448 expr.X_op = O_symbol;
1449 expr.X_add_symbol = end;
1450 expr.X_add_number = 0;
1451 emit_expr (&expr, addr_size);
1454 /* End of Range Entry. */
1455 for (i = 0; i < addr_size; i++)
1456 out_byte (0);
1457 for (i = 0; i < addr_size; i++)
1458 out_byte (0);
1461 /* Emit data for .debug_aranges. */
1463 static void
1464 out_debug_aranges (segT aranges_seg, segT info_seg)
1466 unsigned int addr_size = sizeof_address;
1467 addressT size, skip;
1468 struct line_seg *s;
1469 expressionS expr;
1470 char *p;
1472 size = 4 + 2 + 4 + 1 + 1;
1474 skip = 2 * addr_size - (size & (2 * addr_size - 1));
1475 if (skip == 2 * addr_size)
1476 skip = 0;
1477 size += skip;
1479 for (s = all_segs; s; s = s->next)
1480 size += 2 * addr_size;
1482 size += 2 * addr_size;
1484 subseg_set (aranges_seg, 0);
1486 /* Length of the compilation unit. */
1487 out_four (size - 4);
1489 /* Version. */
1490 out_two (2);
1492 /* Offset to .debug_info. */
1493 /* ??? sizeof_offset */
1494 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), 4);
1496 /* Size of an address (offset portion). */
1497 out_byte (addr_size);
1499 /* Size of a segment descriptor. */
1500 out_byte (0);
1502 /* Align the header. */
1503 if (skip)
1504 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1506 for (s = all_segs; s; s = s->next)
1508 fragS *frag;
1509 symbolS *beg, *end;
1511 frag = first_frag_for_seg (s->seg);
1512 beg = symbol_temp_new (s->seg, 0, frag);
1513 s->text_start = beg;
1515 frag = last_frag_for_seg (s->seg);
1516 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1517 s->text_end = end;
1519 expr.X_op = O_symbol;
1520 expr.X_add_symbol = beg;
1521 expr.X_add_number = 0;
1522 emit_expr (&expr, addr_size);
1524 expr.X_op = O_subtract;
1525 expr.X_add_symbol = end;
1526 expr.X_op_symbol = beg;
1527 expr.X_add_number = 0;
1528 emit_expr (&expr, addr_size);
1531 p = frag_more (2 * addr_size);
1532 md_number_to_chars (p, 0, addr_size);
1533 md_number_to_chars (p + addr_size, 0, addr_size);
1536 /* Emit data for .debug_abbrev. Note that this must be kept in
1537 sync with out_debug_info below. */
1539 static void
1540 out_debug_abbrev (segT abbrev_seg)
1542 subseg_set (abbrev_seg, 0);
1544 out_uleb128 (1);
1545 out_uleb128 (DW_TAG_compile_unit);
1546 out_byte (DW_CHILDREN_no);
1547 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1548 if (all_segs->next == NULL)
1550 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1551 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1553 else
1555 if (DWARF2_FORMAT () == dwarf2_format_32bit)
1556 out_abbrev (DW_AT_ranges, DW_FORM_data4);
1557 else
1558 out_abbrev (DW_AT_ranges, DW_FORM_data8);
1560 out_abbrev (DW_AT_name, DW_FORM_string);
1561 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1562 out_abbrev (DW_AT_producer, DW_FORM_string);
1563 out_abbrev (DW_AT_language, DW_FORM_data2);
1564 out_abbrev (0, 0);
1566 /* Terminate the abbreviations for this compilation unit. */
1567 out_byte (0);
1570 /* Emit a description of this compilation unit for .debug_info. */
1572 static void
1573 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg)
1575 char producer[128];
1576 const char *comp_dir;
1577 const char *dirname;
1578 expressionS expr;
1579 symbolS *info_start;
1580 symbolS *info_end;
1581 char *p;
1582 int len;
1583 enum dwarf2_format d2f;
1584 int sizeof_offset;
1586 subseg_set (info_seg, 0);
1588 info_start = symbol_temp_new_now ();
1589 info_end = symbol_temp_make ();
1591 /* Compilation Unit length. */
1592 expr.X_op = O_subtract;
1593 expr.X_add_symbol = info_end;
1594 expr.X_op_symbol = info_start;
1596 d2f = DWARF2_FORMAT ();
1597 if (d2f == dwarf2_format_32bit)
1599 expr.X_add_number = -4;
1600 emit_expr (&expr, 4);
1601 sizeof_offset = 4;
1603 else if (d2f == dwarf2_format_64bit)
1605 expr.X_add_number = -12;
1606 out_four (-1);
1607 emit_expr (&expr, 8);
1608 sizeof_offset = 8;
1610 else if (d2f == dwarf2_format_64bit_irix)
1612 expr.X_add_number = -8;
1613 emit_expr (&expr, 8);
1614 sizeof_offset = 8;
1616 else
1618 as_fatal (_("internal error: unknown dwarf2 format"));
1621 /* DWARF version. */
1622 out_two (2);
1624 /* .debug_abbrev offset */
1625 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1627 /* Target address size. */
1628 out_byte (sizeof_address);
1630 /* DW_TAG_compile_unit DIE abbrev */
1631 out_uleb128 (1);
1633 /* DW_AT_stmt_list */
1634 /* ??? sizeof_offset */
1635 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 4);
1637 /* These two attributes are emitted if all of the code is contiguous. */
1638 if (all_segs->next == NULL)
1640 /* DW_AT_low_pc */
1641 expr.X_op = O_symbol;
1642 expr.X_add_symbol = all_segs->text_start;
1643 expr.X_add_number = 0;
1644 emit_expr (&expr, sizeof_address);
1646 /* DW_AT_high_pc */
1647 expr.X_op = O_symbol;
1648 expr.X_add_symbol = all_segs->text_end;
1649 expr.X_add_number = 0;
1650 emit_expr (&expr, sizeof_address);
1652 else
1654 /* This attribute is emitted if the code is disjoint. */
1655 /* DW_AT_ranges. */
1656 TC_DWARF2_EMIT_OFFSET (section_symbol (ranges_seg), sizeof_offset);
1659 /* DW_AT_name. We don't have the actual file name that was present
1660 on the command line, so assume files[1] is the main input file.
1661 We're not supposed to get called unless at least one line number
1662 entry was emitted, so this should always be defined. */
1663 if (files_in_use == 0)
1664 abort ();
1665 if (files[1].dir)
1667 dirname = remap_debug_filename (dirs[files[1].dir]);
1668 len = strlen (dirname);
1669 p = frag_more (len + 1);
1670 memcpy (p, dirname, len);
1671 INSERT_DIR_SEPARATOR (p, len);
1673 len = strlen (files[1].filename) + 1;
1674 p = frag_more (len);
1675 memcpy (p, files[1].filename, len);
1677 /* DW_AT_comp_dir */
1678 comp_dir = remap_debug_filename (getpwd ());
1679 len = strlen (comp_dir) + 1;
1680 p = frag_more (len);
1681 memcpy (p, comp_dir, len);
1683 /* DW_AT_producer */
1684 sprintf (producer, "GNU AS %s", VERSION);
1685 len = strlen (producer) + 1;
1686 p = frag_more (len);
1687 memcpy (p, producer, len);
1689 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1690 dwarf2 draft has no standard code for assembler. */
1691 out_two (DW_LANG_Mips_Assembler);
1693 symbol_set_value_now (info_end);
1696 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1697 were any .file/.loc directives, or --gdwarf2 was given, or if the
1698 file has a non-empty .debug_info section. If we emit .debug_line,
1699 and the .debug_info section is empty, we also emit .debug_info,
1700 .debug_aranges and .debug_abbrev. ALL_SEGS will be non-null if
1701 there were any .file/.loc directives, or --gdwarf2 was given and
1702 there were any located instructions emitted. */
1704 void
1705 dwarf2_finish (void)
1707 segT line_seg;
1708 struct line_seg *s;
1709 segT info_seg;
1710 int emit_other_sections = 0;
1712 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
1713 emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
1715 if (!all_segs && emit_other_sections)
1716 /* There is no line information and no non-empty .debug_info
1717 section. */
1718 return;
1720 /* Calculate the size of an address for the target machine. */
1721 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1723 /* Create and switch to the line number section. */
1724 line_seg = subseg_new (".debug_line", 0);
1725 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
1727 /* For each subsection, chain the debug entries together. */
1728 for (s = all_segs; s; s = s->next)
1730 struct line_subseg *ss = s->head;
1731 struct line_entry **ptail = ss->ptail;
1733 while ((ss = ss->next) != NULL)
1735 *ptail = ss->head;
1736 ptail = ss->ptail;
1740 out_debug_line (line_seg);
1742 /* If this is assembler generated line info, and there is no
1743 debug_info already, we need .debug_info and .debug_abbrev
1744 sections as well. */
1745 if (emit_other_sections)
1747 segT abbrev_seg;
1748 segT aranges_seg;
1749 segT ranges_seg;
1751 assert (all_segs);
1753 info_seg = subseg_new (".debug_info", 0);
1754 abbrev_seg = subseg_new (".debug_abbrev", 0);
1755 aranges_seg = subseg_new (".debug_aranges", 0);
1757 bfd_set_section_flags (stdoutput, info_seg,
1758 SEC_READONLY | SEC_DEBUGGING);
1759 bfd_set_section_flags (stdoutput, abbrev_seg,
1760 SEC_READONLY | SEC_DEBUGGING);
1761 bfd_set_section_flags (stdoutput, aranges_seg,
1762 SEC_READONLY | SEC_DEBUGGING);
1764 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1766 if (all_segs->next == NULL)
1767 ranges_seg = NULL;
1768 else
1770 ranges_seg = subseg_new (".debug_ranges", 0);
1771 bfd_set_section_flags (stdoutput, ranges_seg,
1772 SEC_READONLY | SEC_DEBUGGING);
1773 record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1);
1774 out_debug_ranges (ranges_seg);
1777 out_debug_aranges (aranges_seg, info_seg);
1778 out_debug_abbrev (abbrev_seg);
1779 out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg);