binutils/
[binutils.git] / gas / dwarf2dbg.c
blobf332865d5a6befc91454c997bbe760fa265cef88
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(SEC) 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 #ifndef DWARF2_FILE_NAME
80 #define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME
81 #endif
83 #ifndef DWARF2_FILE_TIME_NAME
84 #define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) 0
85 #endif
87 #ifndef DWARF2_FILE_SIZE_NAME
88 #define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) 0
89 #endif
91 #include "subsegs.h"
93 #include "elf/dwarf2.h"
95 /* Since we can't generate the prolog until the body is complete, we
96 use three different subsegments for .debug_line: one holding the
97 prolog, one for the directory and filename info, and one for the
98 body ("statement program"). */
99 #define DL_PROLOG 0
100 #define DL_FILES 1
101 #define DL_BODY 2
103 /* If linker relaxation might change offsets in the code, the DWARF special
104 opcodes and variable-length operands cannot be used. If this macro is
105 nonzero, use the DW_LNS_fixed_advance_pc opcode instead. */
106 #ifndef DWARF2_USE_FIXED_ADVANCE_PC
107 # define DWARF2_USE_FIXED_ADVANCE_PC 0
108 #endif
110 /* First special line opcde - leave room for the standard opcodes.
111 Note: If you want to change this, you'll have to update the
112 "standard_opcode_lengths" table that is emitted below in
113 out_debug_line(). */
114 #define DWARF2_LINE_OPCODE_BASE 13
116 #ifndef DWARF2_LINE_BASE
117 /* Minimum line offset in a special line info. opcode. This value
118 was chosen to give a reasonable range of values. */
119 # define DWARF2_LINE_BASE -5
120 #endif
122 /* Range of line offsets in a special line info. opcode. */
123 #ifndef DWARF2_LINE_RANGE
124 # define DWARF2_LINE_RANGE 14
125 #endif
127 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
128 /* Define the architecture-dependent minimum instruction length (in
129 bytes). This value should be rather too small than too big. */
130 # define DWARF2_LINE_MIN_INSN_LENGTH 1
131 #endif
133 /* Flag that indicates the initial value of the is_stmt_start flag. */
134 #define DWARF2_LINE_DEFAULT_IS_STMT 1
136 /* Given a special op, return the line skip amount. */
137 #define SPECIAL_LINE(op) \
138 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
140 /* Given a special op, return the address skip amount (in units of
141 DWARF2_LINE_MIN_INSN_LENGTH. */
142 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
144 /* The maximum address skip amount that can be encoded with a special op. */
145 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
147 struct line_entry {
148 struct line_entry *next;
149 symbolS *label;
150 struct dwarf2_line_info loc;
153 struct line_subseg {
154 struct line_subseg *next;
155 subsegT subseg;
156 struct line_entry *head;
157 struct line_entry **ptail;
160 struct line_seg {
161 struct line_seg *next;
162 segT seg;
163 struct line_subseg *head;
164 symbolS *text_start;
165 symbolS *text_end;
168 /* Collects data for all line table entries during assembly. */
169 static struct line_seg *all_segs;
171 struct file_entry {
172 const char *filename;
173 unsigned int dir;
176 /* Table of files used by .debug_line. */
177 static struct file_entry *files;
178 static unsigned int files_in_use;
179 static unsigned int files_allocated;
181 /* Table of directories used by .debug_line. */
182 static char **dirs;
183 static unsigned int dirs_in_use;
184 static unsigned int dirs_allocated;
186 /* TRUE when we've seen a .loc directive recently. Used to avoid
187 doing work when there's nothing to do. */
188 bfd_boolean dwarf2_loc_directive_seen;
190 /* TRUE when we're supposed to set the basic block mark whenever a
191 label is seen. */
192 bfd_boolean dwarf2_loc_mark_labels;
194 /* Current location as indicated by the most recent .loc directive. */
195 static struct dwarf2_line_info current = {
196 1, 1, 0, 0,
197 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0
200 /* The size of an address on the target. */
201 static unsigned int sizeof_address;
203 static unsigned int get_filenum (const char *, unsigned int);
205 #ifndef TC_DWARF2_EMIT_OFFSET
206 #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
208 /* Create an offset to .dwarf2_*. */
210 static void
211 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
213 expressionS expr;
215 expr.X_op = O_symbol;
216 expr.X_add_symbol = symbol;
217 expr.X_add_number = 0;
218 emit_expr (&expr, size);
220 #endif
222 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
224 static struct line_subseg *
225 get_line_subseg (segT seg, subsegT subseg)
227 static segT last_seg;
228 static subsegT last_subseg;
229 static struct line_subseg *last_line_subseg;
231 struct line_seg **ps, *s;
232 struct line_subseg **pss, *ss;
234 if (seg == last_seg && subseg == last_subseg)
235 return last_line_subseg;
237 for (ps = &all_segs; (s = *ps) != NULL; ps = &s->next)
238 if (s->seg == seg)
239 goto found_seg;
241 s = (struct line_seg *) xmalloc (sizeof (*s));
242 s->next = NULL;
243 s->seg = seg;
244 s->head = NULL;
245 *ps = s;
247 found_seg:
248 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
250 if (ss->subseg == subseg)
251 goto found_subseg;
252 if (ss->subseg > subseg)
253 break;
256 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
257 ss->next = *pss;
258 ss->subseg = subseg;
259 ss->head = NULL;
260 ss->ptail = &ss->head;
261 *pss = ss;
263 found_subseg:
264 last_seg = seg;
265 last_subseg = subseg;
266 last_line_subseg = ss;
268 return ss;
271 /* Record an entry for LOC occurring at LABEL. */
273 static void
274 dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
276 struct line_subseg *ss;
277 struct line_entry *e;
279 e = (struct line_entry *) xmalloc (sizeof (*e));
280 e->next = NULL;
281 e->label = label;
282 e->loc = *loc;
284 ss = get_line_subseg (now_seg, now_subseg);
285 *ss->ptail = e;
286 ss->ptail = &e->next;
289 /* Record an entry for LOC occurring at OFS within the current fragment. */
291 void
292 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
294 static unsigned int line = -1;
295 static unsigned int filenum = -1;
297 symbolS *sym;
299 /* Early out for as-yet incomplete location information. */
300 if (loc->filenum == 0 || loc->line == 0)
301 return;
303 /* Don't emit sequences of line symbols for the same line when the
304 symbols apply to assembler code. It is necessary to emit
305 duplicate line symbols when a compiler asks for them, because GDB
306 uses them to determine the end of the prologue. */
307 if (debug_type == DEBUG_DWARF2
308 && line == loc->line && filenum == loc->filenum)
309 return;
311 line = loc->line;
312 filenum = loc->filenum;
314 sym = symbol_temp_new (now_seg, ofs, frag_now);
315 dwarf2_gen_line_info_1 (sym, loc);
318 /* Returns the current source information. If .file directives have
319 been encountered, the info for the corresponding source file is
320 returned. Otherwise, the info for the assembly source file is
321 returned. */
323 void
324 dwarf2_where (struct dwarf2_line_info *line)
326 if (debug_type == DEBUG_DWARF2)
328 char *filename;
329 as_where (&filename, &line->line);
330 line->filenum = get_filenum (filename, 0);
331 line->column = 0;
332 line->flags = DWARF2_FLAG_IS_STMT;
333 line->isa = current.isa;
335 else
336 *line = current;
339 /* A hook to allow the target backend to inform the line number state
340 machine of isa changes when assembler debug info is enabled. */
342 void
343 dwarf2_set_isa (unsigned int isa)
345 current.isa = isa;
348 /* Called for each machine instruction, or relatively atomic group of
349 machine instructions (ie built-in macro). The instruction or group
350 is SIZE bytes in length. If dwarf2 line number generation is called
351 for, emit a line statement appropriately. */
353 void
354 dwarf2_emit_insn (int size)
356 struct dwarf2_line_info loc;
358 if (!dwarf2_loc_directive_seen && debug_type != DEBUG_DWARF2)
359 return;
361 dwarf2_where (&loc);
363 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
364 dwarf2_consume_line_info ();
367 /* Called after the current line information has been either used with
368 dwarf2_gen_line_info or saved with a machine instruction for later use.
369 This resets the state of the line number information to reflect that
370 it has been used. */
372 void
373 dwarf2_consume_line_info (void)
375 /* Unless we generate DWARF2 debugging information for each
376 assembler line, we only emit one line symbol for one LOC. */
377 dwarf2_loc_directive_seen = FALSE;
379 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
380 | DWARF2_FLAG_PROLOGUE_END
381 | DWARF2_FLAG_EPILOGUE_BEGIN);
384 /* Called for each (preferably code) label. If dwarf2_loc_mark_labels
385 is enabled, emit a basic block marker. */
387 void
388 dwarf2_emit_label (symbolS *label)
390 struct dwarf2_line_info loc;
392 if (!dwarf2_loc_mark_labels)
393 return;
394 if (S_GET_SEGMENT (label) != now_seg)
395 return;
396 if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE))
397 return;
398 if (files_in_use == 0 && debug_type != DEBUG_DWARF2)
399 return;
401 dwarf2_where (&loc);
403 loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
405 dwarf2_gen_line_info_1 (label, &loc);
406 dwarf2_consume_line_info ();
409 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
410 allocate it on that file table slot, otherwise return the first
411 empty one. */
413 static unsigned int
414 get_filenum (const char *filename, unsigned int num)
416 static unsigned int last_used, last_used_dir_len;
417 const char *file;
418 size_t dir_len;
419 unsigned int i, dir;
421 if (num == 0 && last_used)
423 if (! files[last_used].dir
424 && strcmp (filename, files[last_used].filename) == 0)
425 return last_used;
426 if (files[last_used].dir
427 && strncmp (filename, dirs[files[last_used].dir],
428 last_used_dir_len) == 0
429 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
430 && strcmp (filename + last_used_dir_len + 1,
431 files[last_used].filename) == 0)
432 return last_used;
435 file = lbasename (filename);
436 /* Don't make empty string from / or A: from A:/ . */
437 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
438 if (file <= filename + 3)
439 file = filename;
440 #else
441 if (file == filename + 1)
442 file = filename;
443 #endif
444 dir_len = file - filename;
446 dir = 0;
447 if (dir_len)
449 #ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR
450 --dir_len;
451 #endif
452 for (dir = 1; dir < dirs_in_use; ++dir)
453 if (strncmp (filename, dirs[dir], dir_len) == 0
454 && dirs[dir][dir_len] == '\0')
455 break;
457 if (dir >= dirs_in_use)
459 if (dir >= dirs_allocated)
461 dirs_allocated = dir + 32;
462 dirs = (char **)
463 xrealloc (dirs, (dir + 32) * sizeof (const char *));
466 dirs[dir] = xmalloc (dir_len + 1);
467 memcpy (dirs[dir], filename, dir_len);
468 dirs[dir][dir_len] = '\0';
469 dirs_in_use = dir + 1;
473 if (num == 0)
475 for (i = 1; i < files_in_use; ++i)
476 if (files[i].dir == dir
477 && files[i].filename
478 && strcmp (file, files[i].filename) == 0)
480 last_used = i;
481 last_used_dir_len = dir_len;
482 return i;
485 else
486 i = num;
488 if (i >= files_allocated)
490 unsigned int old = files_allocated;
492 files_allocated = i + 32;
493 files = (struct file_entry *)
494 xrealloc (files, (i + 32) * sizeof (struct file_entry));
496 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
499 files[i].filename = num ? file : xstrdup (file);
500 files[i].dir = dir;
501 if (files_in_use < i + 1)
502 files_in_use = i + 1;
503 last_used = i;
504 last_used_dir_len = dir_len;
506 return i;
509 /* Handle two forms of .file directive:
510 - Pass .file "source.c" to s_app_file
511 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
513 If an entry is added to the file table, return a pointer to the filename. */
515 char *
516 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
518 offsetT num;
519 char *filename;
520 int filename_len;
522 /* Continue to accept a bare string and pass it off. */
523 SKIP_WHITESPACE ();
524 if (*input_line_pointer == '"')
526 s_app_file (0);
527 return NULL;
530 num = get_absolute_expression ();
531 filename = demand_copy_C_string (&filename_len);
532 if (filename == NULL)
533 return NULL;
534 demand_empty_rest_of_line ();
536 if (num < 1)
538 as_bad (_("file number less than one"));
539 return NULL;
542 /* A .file directive implies compiler generated debug information is
543 being supplied. Turn off gas generated debug info. */
544 debug_type = DEBUG_NONE;
546 if (num < (int) files_in_use && files[num].filename != 0)
548 as_bad (_("file number %ld already allocated"), (long) num);
549 return NULL;
552 get_filenum (filename, num);
554 return filename;
557 void
558 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
560 offsetT filenum, line;
562 /* If we see two .loc directives in a row, force the first one to be
563 output now. */
564 if (dwarf2_loc_directive_seen)
565 dwarf2_emit_insn (0);
567 filenum = get_absolute_expression ();
568 SKIP_WHITESPACE ();
569 line = get_absolute_expression ();
571 if (filenum < 1)
573 as_bad (_("file number less than one"));
574 return;
576 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
578 as_bad (_("unassigned file number %ld"), (long) filenum);
579 return;
582 current.filenum = filenum;
583 current.line = line;
585 #ifndef NO_LISTING
586 if (listing)
588 if (files[filenum].dir)
590 size_t dir_len = strlen (dirs[files[filenum].dir]);
591 size_t file_len = strlen (files[filenum].filename);
592 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
594 memcpy (cp, dirs[files[filenum].dir], dir_len);
595 INSERT_DIR_SEPARATOR (cp, dir_len);
596 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
597 cp[dir_len + file_len + 1] = '\0';
598 listing_source_file (cp);
600 else
601 listing_source_file (files[filenum].filename);
602 listing_source_line (line);
604 #endif
606 SKIP_WHITESPACE ();
607 if (ISDIGIT (*input_line_pointer))
609 current.column = get_absolute_expression ();
610 SKIP_WHITESPACE ();
613 while (ISALPHA (*input_line_pointer))
615 char *p, c;
616 offsetT value;
618 p = input_line_pointer;
619 c = get_symbol_end ();
621 if (strcmp (p, "basic_block") == 0)
623 current.flags |= DWARF2_FLAG_BASIC_BLOCK;
624 *input_line_pointer = c;
626 else if (strcmp (p, "prologue_end") == 0)
628 current.flags |= DWARF2_FLAG_PROLOGUE_END;
629 *input_line_pointer = c;
631 else if (strcmp (p, "epilogue_begin") == 0)
633 current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
634 *input_line_pointer = c;
636 else if (strcmp (p, "is_stmt") == 0)
638 *input_line_pointer = c;
639 value = get_absolute_expression ();
640 if (value == 0)
641 current.flags &= ~DWARF2_FLAG_IS_STMT;
642 else if (value == 1)
643 current.flags |= DWARF2_FLAG_IS_STMT;
644 else
646 as_bad (_("is_stmt value not 0 or 1"));
647 return;
650 else if (strcmp (p, "isa") == 0)
652 *input_line_pointer = c;
653 value = get_absolute_expression ();
654 if (value >= 0)
655 current.isa = value;
656 else
658 as_bad (_("isa number less than zero"));
659 return;
662 else
664 as_bad (_("unknown .loc sub-directive `%s'"), p);
665 *input_line_pointer = c;
666 return;
669 SKIP_WHITESPACE ();
672 demand_empty_rest_of_line ();
673 dwarf2_loc_directive_seen = TRUE;
674 debug_type = DEBUG_NONE;
677 void
678 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
680 offsetT value = get_absolute_expression ();
682 if (value != 0 && value != 1)
684 as_bad (_("expected 0 or 1"));
685 ignore_rest_of_line ();
687 else
689 dwarf2_loc_mark_labels = value != 0;
690 demand_empty_rest_of_line ();
694 static struct frag *
695 first_frag_for_seg (segT seg)
697 return seg_info (seg)->frchainP->frch_root;
700 static struct frag *
701 last_frag_for_seg (segT seg)
703 frchainS *f = seg_info (seg)->frchainP;
705 while (f->frch_next != NULL)
706 f = f->frch_next;
708 return f->frch_last;
711 /* Emit a single byte into the current segment. */
713 static inline void
714 out_byte (int byte)
716 FRAG_APPEND_1_CHAR (byte);
719 /* Emit a statement program opcode into the current segment. */
721 static inline void
722 out_opcode (int opc)
724 out_byte (opc);
727 /* Emit a two-byte word into the current segment. */
729 static inline void
730 out_two (int data)
732 md_number_to_chars (frag_more (2), data, 2);
735 /* Emit a four byte word into the current segment. */
737 static inline void
738 out_four (int data)
740 md_number_to_chars (frag_more (4), data, 4);
743 /* Emit an unsigned "little-endian base 128" number. */
745 static void
746 out_uleb128 (addressT value)
748 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
751 /* Emit a tuple for .debug_abbrev. */
753 static inline void
754 out_abbrev (int name, int form)
756 out_uleb128 (name);
757 out_uleb128 (form);
760 /* Get the size of a fragment. */
762 static offsetT
763 get_frag_fix (fragS *frag, segT seg)
765 frchainS *fr;
767 if (frag->fr_next)
768 return frag->fr_fix;
770 /* If a fragment is the last in the chain, special measures must be
771 taken to find its size before relaxation, since it may be pending
772 on some subsegment chain. */
773 for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
774 if (fr->frch_last == frag)
775 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
777 abort ();
780 /* Set an absolute address (may result in a relocation entry). */
782 static void
783 out_set_addr (symbolS *sym)
785 expressionS expr;
787 out_opcode (DW_LNS_extended_op);
788 out_uleb128 (sizeof_address + 1);
790 out_opcode (DW_LNE_set_address);
791 expr.X_op = O_symbol;
792 expr.X_add_symbol = sym;
793 expr.X_add_number = 0;
794 emit_expr (&expr, sizeof_address);
797 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
798 static void scale_addr_delta (addressT *);
800 static void
801 scale_addr_delta (addressT *addr_delta)
803 static int printed_this = 0;
804 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
806 if (!printed_this)
807 as_bad("unaligned opcodes detected in executable segment");
808 printed_this = 1;
810 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
812 #else
813 #define scale_addr_delta(A)
814 #endif
816 /* Encode a pair of line and address skips as efficiently as possible.
817 Note that the line skip is signed, whereas the address skip is unsigned.
819 The following two routines *must* be kept in sync. This is
820 enforced by making emit_inc_line_addr abort if we do not emit
821 exactly the expected number of bytes. */
823 static int
824 size_inc_line_addr (int line_delta, addressT addr_delta)
826 unsigned int tmp, opcode;
827 int len = 0;
829 /* Scale the address delta by the minimum instruction length. */
830 scale_addr_delta (&addr_delta);
832 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
833 We cannot use special opcodes here, since we want the end_sequence
834 to emit the matrix entry. */
835 if (line_delta == INT_MAX)
837 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
838 len = 1;
839 else
840 len = 1 + sizeof_leb128 (addr_delta, 0);
841 return len + 3;
844 /* Bias the line delta by the base. */
845 tmp = line_delta - DWARF2_LINE_BASE;
847 /* If the line increment is out of range of a special opcode, we
848 must encode it with DW_LNS_advance_line. */
849 if (tmp >= DWARF2_LINE_RANGE)
851 len = 1 + sizeof_leb128 (line_delta, 1);
852 line_delta = 0;
853 tmp = 0 - DWARF2_LINE_BASE;
856 /* Bias the opcode by the special opcode base. */
857 tmp += DWARF2_LINE_OPCODE_BASE;
859 /* Avoid overflow when addr_delta is large. */
860 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
862 /* Try using a special opcode. */
863 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
864 if (opcode <= 255)
865 return len + 1;
867 /* Try using DW_LNS_const_add_pc followed by special op. */
868 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
869 if (opcode <= 255)
870 return len + 2;
873 /* Otherwise use DW_LNS_advance_pc. */
874 len += 1 + sizeof_leb128 (addr_delta, 0);
876 /* DW_LNS_copy or special opcode. */
877 len += 1;
879 return len;
882 static void
883 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
885 unsigned int tmp, opcode;
886 int need_copy = 0;
887 char *end = p + len;
889 /* Line number sequences cannot go backward in addresses. This means
890 we've incorrectly ordered the statements in the sequence. */
891 assert ((offsetT) addr_delta >= 0);
893 /* Scale the address delta by the minimum instruction length. */
894 scale_addr_delta (&addr_delta);
896 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
897 We cannot use special opcodes here, since we want the end_sequence
898 to emit the matrix entry. */
899 if (line_delta == INT_MAX)
901 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
902 *p++ = DW_LNS_const_add_pc;
903 else
905 *p++ = DW_LNS_advance_pc;
906 p += output_leb128 (p, addr_delta, 0);
909 *p++ = DW_LNS_extended_op;
910 *p++ = 1;
911 *p++ = DW_LNE_end_sequence;
912 goto done;
915 /* Bias the line delta by the base. */
916 tmp = line_delta - DWARF2_LINE_BASE;
918 /* If the line increment is out of range of a special opcode, we
919 must encode it with DW_LNS_advance_line. */
920 if (tmp >= DWARF2_LINE_RANGE)
922 *p++ = DW_LNS_advance_line;
923 p += output_leb128 (p, line_delta, 1);
925 line_delta = 0;
926 tmp = 0 - DWARF2_LINE_BASE;
927 need_copy = 1;
930 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
931 special opcode. */
932 if (line_delta == 0 && addr_delta == 0)
934 *p++ = DW_LNS_copy;
935 goto done;
938 /* Bias the opcode by the special opcode base. */
939 tmp += DWARF2_LINE_OPCODE_BASE;
941 /* Avoid overflow when addr_delta is large. */
942 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
944 /* Try using a special opcode. */
945 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
946 if (opcode <= 255)
948 *p++ = opcode;
949 goto done;
952 /* Try using DW_LNS_const_add_pc followed by special op. */
953 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
954 if (opcode <= 255)
956 *p++ = DW_LNS_const_add_pc;
957 *p++ = opcode;
958 goto done;
962 /* Otherwise use DW_LNS_advance_pc. */
963 *p++ = DW_LNS_advance_pc;
964 p += output_leb128 (p, addr_delta, 0);
966 if (need_copy)
967 *p++ = DW_LNS_copy;
968 else
969 *p++ = tmp;
971 done:
972 assert (p == end);
975 /* Handy routine to combine calls to the above two routines. */
977 static void
978 out_inc_line_addr (int line_delta, addressT addr_delta)
980 int len = size_inc_line_addr (line_delta, addr_delta);
981 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
984 /* Write out an alternative form of line and address skips using
985 DW_LNS_fixed_advance_pc opcodes. This uses more space than the default
986 line and address information, but it is required if linker relaxation
987 could change the code offsets. The following two routines *must* be
988 kept in sync. */
990 static int
991 size_fixed_inc_line_addr (int line_delta, addressT addr_delta)
993 int len = 0;
995 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */
996 if (line_delta != INT_MAX)
997 len = 1 + sizeof_leb128 (line_delta, 1);
999 if (addr_delta > 50000)
1001 /* DW_LNS_extended_op */
1002 len += 1 + sizeof_leb128 (sizeof_address + 1, 0);
1003 /* DW_LNE_set_address */
1004 len += 1 + sizeof_address;
1006 else
1007 /* DW_LNS_fixed_advance_pc */
1008 len += 3;
1010 if (line_delta == INT_MAX)
1011 /* DW_LNS_extended_op + DW_LNE_end_sequence */
1012 len += 3;
1013 else
1014 /* DW_LNS_copy */
1015 len += 1;
1017 return len;
1020 static void
1021 emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag,
1022 char *p, int len)
1024 expressionS *exp;
1025 segT line_seg;
1026 char *end = p + len;
1028 /* Line number sequences cannot go backward in addresses. This means
1029 we've incorrectly ordered the statements in the sequence. */
1030 assert ((offsetT) addr_delta >= 0);
1032 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */
1033 if (line_delta != INT_MAX)
1035 *p++ = DW_LNS_advance_line;
1036 p += output_leb128 (p, line_delta, 1);
1039 exp = symbol_get_value_expression (frag->fr_symbol);
1040 line_seg = subseg_get (".debug_line", 0);
1042 /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can
1043 advance the address by at most 64K. Linker relaxation (without
1044 which this function would not be used) could change the operand by
1045 an unknown amount. If the address increment is getting close to
1046 the limit, just reset the address. */
1047 if (addr_delta > 50000)
1049 symbolS *to_sym;
1050 expressionS expr;
1052 assert (exp->X_op = O_subtract);
1053 to_sym = exp->X_add_symbol;
1055 *p++ = DW_LNS_extended_op;
1056 p += output_leb128 (p, sizeof_address + 1, 0);
1057 *p++ = DW_LNE_set_address;
1058 expr.X_op = O_symbol;
1059 expr.X_add_symbol = to_sym;
1060 expr.X_add_number = 0;
1061 subseg_change (line_seg, 0);
1062 emit_expr_fix (&expr, sizeof_address, frag, p);
1063 p += sizeof_address;
1065 else
1067 *p++ = DW_LNS_fixed_advance_pc;
1068 subseg_change (line_seg, 0);
1069 emit_expr_fix (exp, 2, frag, p);
1070 p += 2;
1073 if (line_delta == INT_MAX)
1075 *p++ = DW_LNS_extended_op;
1076 *p++ = 1;
1077 *p++ = DW_LNE_end_sequence;
1079 else
1080 *p++ = DW_LNS_copy;
1082 assert (p == end);
1085 /* Generate a variant frag that we can use to relax address/line
1086 increments between fragments of the target segment. */
1088 static void
1089 relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
1091 expressionS expr;
1092 int max_chars;
1094 expr.X_op = O_subtract;
1095 expr.X_add_symbol = to_sym;
1096 expr.X_op_symbol = from_sym;
1097 expr.X_add_number = 0;
1099 /* The maximum size of the frag is the line delta with a maximum
1100 sized address delta. */
1101 if (DWARF2_USE_FIXED_ADVANCE_PC)
1102 max_chars = size_fixed_inc_line_addr (line_delta,
1103 -DWARF2_LINE_MIN_INSN_LENGTH);
1104 else
1105 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
1107 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
1108 make_expr_symbol (&expr), line_delta, NULL);
1111 /* The function estimates the size of a rs_dwarf2dbg variant frag
1112 based on the current values of the symbols. It is called before
1113 the relaxation loop. We set fr_subtype to the expected length. */
1116 dwarf2dbg_estimate_size_before_relax (fragS *frag)
1118 offsetT addr_delta;
1119 int size;
1121 addr_delta = resolve_symbol_value (frag->fr_symbol);
1122 if (DWARF2_USE_FIXED_ADVANCE_PC)
1123 size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta);
1124 else
1125 size = size_inc_line_addr (frag->fr_offset, addr_delta);
1127 frag->fr_subtype = size;
1129 return size;
1132 /* This function relaxes a rs_dwarf2dbg variant frag based on the
1133 current values of the symbols. fr_subtype is the current length
1134 of the frag. This returns the change in frag length. */
1137 dwarf2dbg_relax_frag (fragS *frag)
1139 int old_size, new_size;
1141 old_size = frag->fr_subtype;
1142 new_size = dwarf2dbg_estimate_size_before_relax (frag);
1144 return new_size - old_size;
1147 /* This function converts a rs_dwarf2dbg variant frag into a normal
1148 fill frag. This is called after all relaxation has been done.
1149 fr_subtype will be the desired length of the frag. */
1151 void
1152 dwarf2dbg_convert_frag (fragS *frag)
1154 offsetT addr_diff;
1156 addr_diff = resolve_symbol_value (frag->fr_symbol);
1158 /* fr_var carries the max_chars that we created the fragment with.
1159 fr_subtype carries the current expected length. We must, of
1160 course, have allocated enough memory earlier. */
1161 assert (frag->fr_var >= (int) frag->fr_subtype);
1163 if (DWARF2_USE_FIXED_ADVANCE_PC)
1164 emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag,
1165 frag->fr_literal + frag->fr_fix,
1166 frag->fr_subtype);
1167 else
1168 emit_inc_line_addr (frag->fr_offset, addr_diff,
1169 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
1171 frag->fr_fix += frag->fr_subtype;
1172 frag->fr_type = rs_fill;
1173 frag->fr_var = 0;
1174 frag->fr_offset = 0;
1177 /* Generate .debug_line content for the chain of line number entries
1178 beginning at E, for segment SEG. */
1180 static void
1181 process_entries (segT seg, struct line_entry *e)
1183 unsigned filenum = 1;
1184 unsigned line = 1;
1185 unsigned column = 0;
1186 unsigned isa = 0;
1187 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1188 fragS *last_frag = NULL, *frag;
1189 addressT last_frag_ofs = 0, frag_ofs;
1190 symbolS *last_lab = NULL, *lab;
1191 struct line_entry *next;
1195 int line_delta;
1197 if (filenum != e->loc.filenum)
1199 filenum = e->loc.filenum;
1200 out_opcode (DW_LNS_set_file);
1201 out_uleb128 (filenum);
1204 if (column != e->loc.column)
1206 column = e->loc.column;
1207 out_opcode (DW_LNS_set_column);
1208 out_uleb128 (column);
1211 if (isa != e->loc.isa)
1213 isa = e->loc.isa;
1214 out_opcode (DW_LNS_set_isa);
1215 out_uleb128 (isa);
1218 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
1220 flags = e->loc.flags;
1221 out_opcode (DW_LNS_negate_stmt);
1224 if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
1225 out_opcode (DW_LNS_set_basic_block);
1227 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
1228 out_opcode (DW_LNS_set_prologue_end);
1230 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1231 out_opcode (DW_LNS_set_epilogue_begin);
1233 /* Don't try to optimize away redundant entries; gdb wants two
1234 entries for a function where the code starts on the same line as
1235 the {, and there's no way to identify that case here. Trust gcc
1236 to optimize appropriately. */
1237 line_delta = e->loc.line - line;
1238 lab = e->label;
1239 frag = symbol_get_frag (lab);
1240 frag_ofs = S_GET_VALUE (lab);
1242 if (last_frag == NULL)
1244 out_set_addr (lab);
1245 out_inc_line_addr (line_delta, 0);
1247 else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1248 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
1249 else
1250 relax_inc_line_addr (line_delta, lab, last_lab);
1252 line = e->loc.line;
1253 last_lab = lab;
1254 last_frag = frag;
1255 last_frag_ofs = frag_ofs;
1257 next = e->next;
1258 free (e);
1259 e = next;
1261 while (e);
1263 /* Emit a DW_LNE_end_sequence for the end of the section. */
1264 frag = last_frag_for_seg (seg);
1265 frag_ofs = get_frag_fix (frag, seg);
1266 if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1267 out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
1268 else
1270 lab = symbol_temp_new (seg, frag_ofs, frag);
1271 relax_inc_line_addr (INT_MAX, lab, last_lab);
1275 /* Emit the directory and file tables for .debug_line. */
1277 static void
1278 out_file_list (void)
1280 size_t size;
1281 const char *dir;
1282 char *cp;
1283 unsigned int i;
1285 /* Emit directory list. */
1286 for (i = 1; i < dirs_in_use; ++i)
1288 dir = remap_debug_filename (dirs[i]);
1289 size = strlen (dir) + 1;
1290 cp = frag_more (size);
1291 memcpy (cp, dir, size);
1293 /* Terminate it. */
1294 out_byte ('\0');
1296 for (i = 1; i < files_in_use; ++i)
1298 const char *fullfilename;
1300 if (files[i].filename == NULL)
1302 as_bad (_("unassigned file number %ld"), (long) i);
1303 /* Prevent a crash later, particularly for file 1. */
1304 files[i].filename = "";
1305 continue;
1308 fullfilename = DWARF2_FILE_NAME (files[i].filename,
1309 files[i].dir ? dirs [files [i].dir] : "");
1310 size = strlen (fullfilename) + 1;
1311 cp = frag_more (size);
1312 memcpy (cp, fullfilename, size);
1314 out_uleb128 (files[i].dir); /* directory number */
1315 /* Output the last modification timestamp. */
1316 out_uleb128 (DWARF2_FILE_TIME_NAME (files[i].filename,
1317 files[i].dir ? dirs [files [i].dir] : ""));
1318 /* Output the filesize. */
1319 out_uleb128 (DWARF2_FILE_SIZE_NAME (files[i].filename,
1320 files[i].dir ? dirs [files [i].dir] : ""));
1323 /* Terminate filename list. */
1324 out_byte (0);
1327 /* Switch to SEC and output a header length field. Return the size of
1328 offsets used in SEC. The caller must set EXPR->X_add_symbol value
1329 to the end of the section. */
1331 static int
1332 out_header (asection *sec, expressionS *expr)
1334 symbolS *start_sym;
1335 symbolS *end_sym;
1337 subseg_set (sec, 0);
1338 start_sym = symbol_temp_new_now ();;
1339 end_sym = symbol_temp_make ();
1341 /* Total length of the information. */
1342 expr->X_op = O_subtract;
1343 expr->X_add_symbol = end_sym;
1344 expr->X_op_symbol = start_sym;
1346 switch (DWARF2_FORMAT (sec))
1348 case dwarf2_format_32bit:
1349 expr->X_add_number = -4;
1350 emit_expr (expr, 4);
1351 return 4;
1353 case dwarf2_format_64bit:
1354 expr->X_add_number = -12;
1355 out_four (-1);
1356 emit_expr (expr, 8);
1357 return 8;
1359 case dwarf2_format_64bit_irix:
1360 expr->X_add_number = -8;
1361 emit_expr (expr, 8);
1362 return 8;
1365 as_fatal (_("internal error: unknown dwarf2 format"));
1366 return 0;
1369 /* Emit the collected .debug_line data. */
1371 static void
1372 out_debug_line (segT line_seg)
1374 expressionS expr;
1375 symbolS *prologue_end;
1376 symbolS *line_end;
1377 struct line_seg *s;
1378 int sizeof_offset;
1380 sizeof_offset = out_header (line_seg, &expr);
1381 line_end = expr.X_add_symbol;
1383 /* Version. */
1384 out_two (2);
1386 /* Length of the prologue following this length. */
1387 prologue_end = symbol_temp_make ();
1388 expr.X_add_symbol = prologue_end;
1389 expr.X_add_number = - (4 + 2 + 4);
1390 emit_expr (&expr, sizeof_offset);
1392 /* Parameters of the state machine. */
1393 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1394 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1395 out_byte (DWARF2_LINE_BASE);
1396 out_byte (DWARF2_LINE_RANGE);
1397 out_byte (DWARF2_LINE_OPCODE_BASE);
1399 /* Standard opcode lengths. */
1400 out_byte (0); /* DW_LNS_copy */
1401 out_byte (1); /* DW_LNS_advance_pc */
1402 out_byte (1); /* DW_LNS_advance_line */
1403 out_byte (1); /* DW_LNS_set_file */
1404 out_byte (1); /* DW_LNS_set_column */
1405 out_byte (0); /* DW_LNS_negate_stmt */
1406 out_byte (0); /* DW_LNS_set_basic_block */
1407 out_byte (0); /* DW_LNS_const_add_pc */
1408 out_byte (1); /* DW_LNS_fixed_advance_pc */
1409 out_byte (0); /* DW_LNS_set_prologue_end */
1410 out_byte (0); /* DW_LNS_set_epilogue_begin */
1411 out_byte (1); /* DW_LNS_set_isa */
1413 out_file_list ();
1415 symbol_set_value_now (prologue_end);
1417 /* For each section, emit a statement program. */
1418 for (s = all_segs; s; s = s->next)
1419 process_entries (s->seg, s->head->head);
1421 symbol_set_value_now (line_end);
1424 static void
1425 out_debug_ranges (segT ranges_seg)
1427 unsigned int addr_size = sizeof_address;
1428 struct line_seg *s;
1429 expressionS expr;
1430 unsigned int i;
1432 subseg_set (ranges_seg, 0);
1434 /* Base Address Entry. */
1435 for (i = 0; i < addr_size; i++)
1436 out_byte (0xff);
1437 for (i = 0; i < addr_size; i++)
1438 out_byte (0);
1440 /* Range List Entry. */
1441 for (s = all_segs; s; s = s->next)
1443 fragS *frag;
1444 symbolS *beg, *end;
1446 frag = first_frag_for_seg (s->seg);
1447 beg = symbol_temp_new (s->seg, 0, frag);
1448 s->text_start = beg;
1450 frag = last_frag_for_seg (s->seg);
1451 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1452 s->text_end = end;
1454 expr.X_op = O_symbol;
1455 expr.X_add_symbol = beg;
1456 expr.X_add_number = 0;
1457 emit_expr (&expr, addr_size);
1459 expr.X_op = O_symbol;
1460 expr.X_add_symbol = end;
1461 expr.X_add_number = 0;
1462 emit_expr (&expr, addr_size);
1465 /* End of Range Entry. */
1466 for (i = 0; i < addr_size; i++)
1467 out_byte (0);
1468 for (i = 0; i < addr_size; i++)
1469 out_byte (0);
1472 /* Emit data for .debug_aranges. */
1474 static void
1475 out_debug_aranges (segT aranges_seg, segT info_seg)
1477 unsigned int addr_size = sizeof_address;
1478 struct line_seg *s;
1479 expressionS expr;
1480 symbolS *aranges_end;
1481 char *p;
1482 int sizeof_offset;
1484 sizeof_offset = out_header (aranges_seg, &expr);
1485 aranges_end = expr.X_add_symbol;
1487 /* Version. */
1488 out_two (2);
1490 /* Offset to .debug_info. */
1491 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset);
1493 /* Size of an address (offset portion). */
1494 out_byte (addr_size);
1496 /* Size of a segment descriptor. */
1497 out_byte (0);
1499 /* Align the header. */
1500 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1502 for (s = all_segs; s; s = s->next)
1504 fragS *frag;
1505 symbolS *beg, *end;
1507 frag = first_frag_for_seg (s->seg);
1508 beg = symbol_temp_new (s->seg, 0, frag);
1509 s->text_start = beg;
1511 frag = last_frag_for_seg (s->seg);
1512 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1513 s->text_end = end;
1515 expr.X_op = O_symbol;
1516 expr.X_add_symbol = beg;
1517 expr.X_add_number = 0;
1518 emit_expr (&expr, addr_size);
1520 expr.X_op = O_subtract;
1521 expr.X_add_symbol = end;
1522 expr.X_op_symbol = beg;
1523 expr.X_add_number = 0;
1524 emit_expr (&expr, addr_size);
1527 p = frag_more (2 * addr_size);
1528 md_number_to_chars (p, 0, addr_size);
1529 md_number_to_chars (p + addr_size, 0, addr_size);
1531 symbol_set_value_now (aranges_end);
1534 /* Emit data for .debug_abbrev. Note that this must be kept in
1535 sync with out_debug_info below. */
1537 static void
1538 out_debug_abbrev (segT abbrev_seg,
1539 segT info_seg ATTRIBUTE_UNUSED,
1540 segT line_seg ATTRIBUTE_UNUSED)
1542 subseg_set (abbrev_seg, 0);
1544 out_uleb128 (1);
1545 out_uleb128 (DW_TAG_compile_unit);
1546 out_byte (DW_CHILDREN_no);
1547 if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit)
1548 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1549 else
1550 out_abbrev (DW_AT_stmt_list, DW_FORM_data8);
1551 if (all_segs->next == NULL)
1553 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1554 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1556 else
1558 if (DWARF2_FORMAT (info_seg) == dwarf2_format_32bit)
1559 out_abbrev (DW_AT_ranges, DW_FORM_data4);
1560 else
1561 out_abbrev (DW_AT_ranges, DW_FORM_data8);
1563 out_abbrev (DW_AT_name, DW_FORM_string);
1564 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1565 out_abbrev (DW_AT_producer, DW_FORM_string);
1566 out_abbrev (DW_AT_language, DW_FORM_data2);
1567 out_abbrev (0, 0);
1569 /* Terminate the abbreviations for this compilation unit. */
1570 out_byte (0);
1573 /* Emit a description of this compilation unit for .debug_info. */
1575 static void
1576 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg)
1578 char producer[128];
1579 const char *comp_dir;
1580 const char *dirname;
1581 expressionS expr;
1582 symbolS *info_end;
1583 char *p;
1584 int len;
1585 int sizeof_offset;
1587 sizeof_offset = out_header (info_seg, &expr);
1588 info_end = expr.X_add_symbol;
1590 /* DWARF version. */
1591 out_two (2);
1593 /* .debug_abbrev offset */
1594 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1596 /* Target address size. */
1597 out_byte (sizeof_address);
1599 /* DW_TAG_compile_unit DIE abbrev */
1600 out_uleb128 (1);
1602 /* DW_AT_stmt_list */
1603 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg),
1604 (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit
1605 ? 4 : 8));
1607 /* These two attributes are emitted if all of the code is contiguous. */
1608 if (all_segs->next == NULL)
1610 /* DW_AT_low_pc */
1611 expr.X_op = O_symbol;
1612 expr.X_add_symbol = all_segs->text_start;
1613 expr.X_add_number = 0;
1614 emit_expr (&expr, sizeof_address);
1616 /* DW_AT_high_pc */
1617 expr.X_op = O_symbol;
1618 expr.X_add_symbol = all_segs->text_end;
1619 expr.X_add_number = 0;
1620 emit_expr (&expr, sizeof_address);
1622 else
1624 /* This attribute is emitted if the code is disjoint. */
1625 /* DW_AT_ranges. */
1626 TC_DWARF2_EMIT_OFFSET (section_symbol (ranges_seg), sizeof_offset);
1629 /* DW_AT_name. We don't have the actual file name that was present
1630 on the command line, so assume files[1] is the main input file.
1631 We're not supposed to get called unless at least one line number
1632 entry was emitted, so this should always be defined. */
1633 if (files_in_use == 0)
1634 abort ();
1635 if (files[1].dir)
1637 dirname = remap_debug_filename (dirs[files[1].dir]);
1638 len = strlen (dirname);
1639 #ifdef TE_VMS
1640 /* Already has trailing slash. */
1641 p = frag_more (len);
1642 memcpy (p, dirname, len);
1643 #else
1644 p = frag_more (len + 1);
1645 memcpy (p, dirname, len);
1646 INSERT_DIR_SEPARATOR (p, len);
1647 #endif
1649 len = strlen (files[1].filename) + 1;
1650 p = frag_more (len);
1651 memcpy (p, files[1].filename, len);
1653 /* DW_AT_comp_dir */
1654 comp_dir = remap_debug_filename (getpwd ());
1655 len = strlen (comp_dir) + 1;
1656 p = frag_more (len);
1657 memcpy (p, comp_dir, len);
1659 /* DW_AT_producer */
1660 sprintf (producer, "GNU AS %s", VERSION);
1661 len = strlen (producer) + 1;
1662 p = frag_more (len);
1663 memcpy (p, producer, len);
1665 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1666 dwarf2 draft has no standard code for assembler. */
1667 out_two (DW_LANG_Mips_Assembler);
1669 symbol_set_value_now (info_end);
1672 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1673 were any .file/.loc directives, or --gdwarf2 was given, or if the
1674 file has a non-empty .debug_info section. If we emit .debug_line,
1675 and the .debug_info section is empty, we also emit .debug_info,
1676 .debug_aranges and .debug_abbrev. ALL_SEGS will be non-null if
1677 there were any .file/.loc directives, or --gdwarf2 was given and
1678 there were any located instructions emitted. */
1680 void
1681 dwarf2_finish (void)
1683 segT line_seg;
1684 struct line_seg *s;
1685 segT info_seg;
1686 int emit_other_sections = 0;
1688 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
1689 emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
1691 if (!all_segs && emit_other_sections)
1692 /* There is no line information and no non-empty .debug_info
1693 section. */
1694 return;
1696 /* Calculate the size of an address for the target machine. */
1697 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1699 /* Create and switch to the line number section. */
1700 line_seg = subseg_new (".debug_line", 0);
1701 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
1703 /* For each subsection, chain the debug entries together. */
1704 for (s = all_segs; s; s = s->next)
1706 struct line_subseg *ss = s->head;
1707 struct line_entry **ptail = ss->ptail;
1709 while ((ss = ss->next) != NULL)
1711 *ptail = ss->head;
1712 ptail = ss->ptail;
1716 out_debug_line (line_seg);
1718 /* If this is assembler generated line info, and there is no
1719 debug_info already, we need .debug_info and .debug_abbrev
1720 sections as well. */
1721 if (emit_other_sections)
1723 segT abbrev_seg;
1724 segT aranges_seg;
1725 segT ranges_seg;
1727 assert (all_segs);
1729 info_seg = subseg_new (".debug_info", 0);
1730 abbrev_seg = subseg_new (".debug_abbrev", 0);
1731 aranges_seg = subseg_new (".debug_aranges", 0);
1733 bfd_set_section_flags (stdoutput, info_seg,
1734 SEC_READONLY | SEC_DEBUGGING);
1735 bfd_set_section_flags (stdoutput, abbrev_seg,
1736 SEC_READONLY | SEC_DEBUGGING);
1737 bfd_set_section_flags (stdoutput, aranges_seg,
1738 SEC_READONLY | SEC_DEBUGGING);
1740 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1742 if (all_segs->next == NULL)
1743 ranges_seg = NULL;
1744 else
1746 ranges_seg = subseg_new (".debug_ranges", 0);
1747 bfd_set_section_flags (stdoutput, ranges_seg,
1748 SEC_READONLY | SEC_DEBUGGING);
1749 record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1);
1750 out_debug_ranges (ranges_seg);
1753 out_debug_aranges (aranges_seg, info_seg);
1754 out_debug_abbrev (abbrev_seg, info_seg, line_seg);
1755 out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg);