* elf32-spu.c (build_stub): Fix malloc under-allocation.
[binutils.git] / gas / dwarf2dbg.c
blob76f5e84f53fa903547e08504fab5682a8fa3450c
1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* Logical line numbers can be controlled by the compiler via the
24 following directives:
26 .file FILENO "file.c"
27 .loc FILENO LINENO [COLUMN] [basic_block] [prologue_end] \
28 [epilogue_begin] [is_stmt VALUE] [isa VALUE] \
29 [discriminator VALUE]
32 #include "as.h"
33 #include "safe-ctype.h"
35 #ifdef HAVE_LIMITS_H
36 #include <limits.h>
37 #else
38 #ifdef HAVE_SYS_PARAM_H
39 #include <sys/param.h>
40 #endif
41 #ifndef INT_MAX
42 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
43 #endif
44 #endif
46 #include "dwarf2dbg.h"
47 #include <filenames.h>
49 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
50 /* We need to decide which character to use as a directory separator.
51 Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not
52 necessarily mean that the backslash character is the one to use.
53 Some environments, eg Cygwin, can support both naming conventions.
54 So we use the heuristic that we only need to use the backslash if
55 the path is an absolute path starting with a DOS style drive
56 selector. eg C: or D: */
57 # define INSERT_DIR_SEPARATOR(string, offset) \
58 do \
59 { \
60 if (offset > 1 \
61 && string[0] != 0 \
62 && string[1] == ':') \
63 string [offset] = '\\'; \
64 else \
65 string [offset] = '/'; \
66 } \
67 while (0)
68 #else
69 # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
70 #endif
72 #ifndef DWARF2_FORMAT
73 # define DWARF2_FORMAT(SEC) dwarf2_format_32bit
74 #endif
76 #ifndef DWARF2_ADDR_SIZE
77 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
78 #endif
80 #ifndef DWARF2_FILE_NAME
81 #define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME
82 #endif
84 #ifndef DWARF2_FILE_TIME_NAME
85 #define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) 0
86 #endif
88 #ifndef DWARF2_FILE_SIZE_NAME
89 #define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) 0
90 #endif
92 #ifndef DWARF2_VERSION
93 #define DWARF2_VERSION 2
94 #endif
96 #include "subsegs.h"
98 #include "dwarf2.h"
100 /* Since we can't generate the prolog until the body is complete, we
101 use three different subsegments for .debug_line: one holding the
102 prolog, one for the directory and filename info, and one for the
103 body ("statement program"). */
104 #define DL_PROLOG 0
105 #define DL_FILES 1
106 #define DL_BODY 2
108 /* If linker relaxation might change offsets in the code, the DWARF special
109 opcodes and variable-length operands cannot be used. If this macro is
110 nonzero, use the DW_LNS_fixed_advance_pc opcode instead. */
111 #ifndef DWARF2_USE_FIXED_ADVANCE_PC
112 # define DWARF2_USE_FIXED_ADVANCE_PC 0
113 #endif
115 /* First special line opcde - leave room for the standard opcodes.
116 Note: If you want to change this, you'll have to update the
117 "standard_opcode_lengths" table that is emitted below in
118 out_debug_line(). */
119 #define DWARF2_LINE_OPCODE_BASE 13
121 #ifndef DWARF2_LINE_BASE
122 /* Minimum line offset in a special line info. opcode. This value
123 was chosen to give a reasonable range of values. */
124 # define DWARF2_LINE_BASE -5
125 #endif
127 /* Range of line offsets in a special line info. opcode. */
128 #ifndef DWARF2_LINE_RANGE
129 # define DWARF2_LINE_RANGE 14
130 #endif
132 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
133 /* Define the architecture-dependent minimum instruction length (in
134 bytes). This value should be rather too small than too big. */
135 # define DWARF2_LINE_MIN_INSN_LENGTH 1
136 #endif
138 /* Flag that indicates the initial value of the is_stmt_start flag. */
139 #define DWARF2_LINE_DEFAULT_IS_STMT 1
141 /* Given a special op, return the line skip amount. */
142 #define SPECIAL_LINE(op) \
143 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
145 /* Given a special op, return the address skip amount (in units of
146 DWARF2_LINE_MIN_INSN_LENGTH. */
147 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
149 /* The maximum address skip amount that can be encoded with a special op. */
150 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
152 struct line_entry {
153 struct line_entry *next;
154 symbolS *label;
155 struct dwarf2_line_info loc;
158 struct line_subseg {
159 struct line_subseg *next;
160 subsegT subseg;
161 struct line_entry *head;
162 struct line_entry **ptail;
165 struct line_seg {
166 struct line_seg *next;
167 segT seg;
168 struct line_subseg *head;
169 symbolS *text_start;
170 symbolS *text_end;
173 /* Collects data for all line table entries during assembly. */
174 static struct line_seg *all_segs;
175 /* Hash used to quickly lookup a segment by name, avoiding the need to search
176 through the all_segs list. */
177 static struct hash_control *all_segs_hash;
178 static struct line_seg **last_seg_ptr;
180 struct file_entry {
181 const char *filename;
182 unsigned int dir;
185 /* Table of files used by .debug_line. */
186 static struct file_entry *files;
187 static unsigned int files_in_use;
188 static unsigned int files_allocated;
190 /* Table of directories used by .debug_line. */
191 static char **dirs;
192 static unsigned int dirs_in_use;
193 static unsigned int dirs_allocated;
195 /* TRUE when we've seen a .loc directive recently. Used to avoid
196 doing work when there's nothing to do. */
197 bfd_boolean dwarf2_loc_directive_seen;
199 /* TRUE when we're supposed to set the basic block mark whenever a
200 label is seen. */
201 bfd_boolean dwarf2_loc_mark_labels;
203 /* Current location as indicated by the most recent .loc directive. */
204 static struct dwarf2_line_info current = {
205 1, 1, 0, 0,
206 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0,
210 /* Lines that are at the same location as CURRENT, and which are waiting
211 for a label. */
212 static struct line_entry *pending_lines, **pending_lines_tail = &pending_lines;
214 /* The size of an address on the target. */
215 static unsigned int sizeof_address;
217 static unsigned int get_filenum (const char *, unsigned int);
219 #ifndef TC_DWARF2_EMIT_OFFSET
220 #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
222 /* Create an offset to .dwarf2_*. */
224 static void
225 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
227 expressionS exp;
229 exp.X_op = O_symbol;
230 exp.X_add_symbol = symbol;
231 exp.X_add_number = 0;
232 emit_expr (&exp, size);
234 #endif
236 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
238 static struct line_subseg *
239 get_line_subseg (segT seg, subsegT subseg)
241 static segT last_seg;
242 static subsegT last_subseg;
243 static struct line_subseg *last_line_subseg;
245 struct line_seg *s;
246 struct line_subseg **pss, *lss;
248 if (seg == last_seg && subseg == last_subseg)
249 return last_line_subseg;
251 s = (struct line_seg *) hash_find (all_segs_hash, seg->name);
252 if (s == NULL)
254 s = (struct line_seg *) xmalloc (sizeof (*s));
255 s->next = NULL;
256 s->seg = seg;
257 s->head = NULL;
258 *last_seg_ptr = s;
259 last_seg_ptr = &s->next;
260 hash_insert (all_segs_hash, seg->name, s);
262 gas_assert (seg == s->seg);
264 for (pss = &s->head; (lss = *pss) != NULL ; pss = &lss->next)
266 if (lss->subseg == subseg)
267 goto found_subseg;
268 if (lss->subseg > subseg)
269 break;
272 lss = (struct line_subseg *) xmalloc (sizeof (*lss));
273 lss->next = *pss;
274 lss->subseg = subseg;
275 lss->head = NULL;
276 lss->ptail = &lss->head;
277 *pss = lss;
279 found_subseg:
280 last_seg = seg;
281 last_subseg = subseg;
282 last_line_subseg = lss;
284 return lss;
287 /* Push LOC onto the pending lines list. */
289 static void
290 dwarf2_push_line (struct dwarf2_line_info *loc)
292 struct line_entry *e;
294 e = (struct line_entry *) xmalloc (sizeof (*e));
295 e->next = NULL;
296 e->label = NULL;
297 e->loc = *loc;
299 *pending_lines_tail = e;
300 pending_lines_tail = &(*pending_lines_tail)->next;
303 /* Emit all pending line information. LABEL is the label with which the
304 lines should be associated, or null if they should be associated with
305 the current position. */
307 static void
308 dwarf2_flush_pending_lines (symbolS *label)
310 if (pending_lines)
312 struct line_subseg *lss;
313 struct line_entry *e;
315 if (!label)
316 label = symbol_temp_new_now ();
318 for (e = pending_lines; e; e = e->next)
319 e->label = label;
321 lss = get_line_subseg (now_seg, now_subseg);
322 *lss->ptail = pending_lines;
323 lss->ptail = pending_lines_tail;
325 pending_lines = NULL;
326 pending_lines_tail = &pending_lines;
330 /* Record an entry for LOC occurring at OFS within the current fragment. */
332 void
333 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
335 static unsigned int line = -1;
336 static unsigned int filenum = -1;
338 /* Early out for as-yet incomplete location information. */
339 if (loc->filenum == 0 || loc->line == 0)
340 return;
342 /* Don't emit sequences of line symbols for the same line when the
343 symbols apply to assembler code. It is necessary to emit
344 duplicate line symbols when a compiler asks for them, because GDB
345 uses them to determine the end of the prologue. */
346 if (debug_type == DEBUG_DWARF2
347 && line == loc->line && filenum == loc->filenum)
348 return;
350 line = loc->line;
351 filenum = loc->filenum;
353 dwarf2_push_line (loc);
354 dwarf2_flush_pending_lines (symbol_temp_new (now_seg, ofs, frag_now));
357 /* Returns the current source information. If .file directives have
358 been encountered, the info for the corresponding source file is
359 returned. Otherwise, the info for the assembly source file is
360 returned. */
362 void
363 dwarf2_where (struct dwarf2_line_info *line)
365 if (debug_type == DEBUG_DWARF2)
367 char *filename;
368 as_where (&filename, &line->line);
369 line->filenum = get_filenum (filename, 0);
370 line->column = 0;
371 line->flags = DWARF2_FLAG_IS_STMT;
372 line->isa = current.isa;
373 line->discriminator = current.discriminator;
375 else
376 *line = current;
379 /* A hook to allow the target backend to inform the line number state
380 machine of isa changes when assembler debug info is enabled. */
382 void
383 dwarf2_set_isa (unsigned int isa)
385 current.isa = isa;
388 /* Called for each machine instruction, or relatively atomic group of
389 machine instructions (ie built-in macro). The instruction or group
390 is SIZE bytes in length. If dwarf2 line number generation is called
391 for, emit a line statement appropriately. */
393 void
394 dwarf2_emit_insn (int size)
396 struct dwarf2_line_info loc;
398 if (!dwarf2_loc_directive_seen && debug_type != DEBUG_DWARF2)
399 return;
401 dwarf2_where (&loc);
403 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
404 dwarf2_consume_line_info ();
407 /* Called after the current line information has been either used with
408 dwarf2_gen_line_info or saved with a machine instruction for later use.
409 This resets the state of the line number information to reflect that
410 it has been used. */
412 void
413 dwarf2_consume_line_info (void)
415 /* If the consumer has stashed the current location away for later use,
416 assume that any earlier location information should be associated
417 with ".". */
418 dwarf2_flush_pending_lines (NULL);
420 /* Unless we generate DWARF2 debugging information for each
421 assembler line, we only emit one line symbol for one LOC. */
422 dwarf2_loc_directive_seen = FALSE;
424 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
425 | DWARF2_FLAG_PROLOGUE_END
426 | DWARF2_FLAG_EPILOGUE_BEGIN);
427 current.discriminator = 0;
430 /* Called for each (preferably code) label. If dwarf2_loc_mark_labels
431 is enabled, emit a basic block marker. */
433 void
434 dwarf2_emit_label (symbolS *label)
436 struct dwarf2_line_info loc;
438 if (!dwarf2_loc_mark_labels)
439 return;
440 if (S_GET_SEGMENT (label) != now_seg)
441 return;
442 if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE))
443 return;
444 if (files_in_use == 0 && debug_type != DEBUG_DWARF2)
445 return;
447 dwarf2_where (&loc);
449 loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
451 dwarf2_push_line (&loc);
452 dwarf2_flush_pending_lines (label);
453 dwarf2_consume_line_info ();
456 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
457 allocate it on that file table slot, otherwise return the first
458 empty one. */
460 static unsigned int
461 get_filenum (const char *filename, unsigned int num)
463 static unsigned int last_used, last_used_dir_len;
464 const char *file;
465 size_t dir_len;
466 unsigned int i, dir;
468 if (num == 0 && last_used)
470 if (! files[last_used].dir
471 && filename_cmp (filename, files[last_used].filename) == 0)
472 return last_used;
473 if (files[last_used].dir
474 && filename_ncmp (filename, dirs[files[last_used].dir],
475 last_used_dir_len) == 0
476 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
477 && filename_cmp (filename + last_used_dir_len + 1,
478 files[last_used].filename) == 0)
479 return last_used;
482 file = lbasename (filename);
483 /* Don't make empty string from / or A: from A:/ . */
484 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
485 if (file <= filename + 3)
486 file = filename;
487 #else
488 if (file == filename + 1)
489 file = filename;
490 #endif
491 dir_len = file - filename;
493 dir = 0;
494 if (dir_len)
496 #ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR
497 --dir_len;
498 #endif
499 for (dir = 1; dir < dirs_in_use; ++dir)
500 if (filename_ncmp (filename, dirs[dir], dir_len) == 0
501 && dirs[dir][dir_len] == '\0')
502 break;
504 if (dir >= dirs_in_use)
506 if (dir >= dirs_allocated)
508 dirs_allocated = dir + 32;
509 dirs = (char **)
510 xrealloc (dirs, (dir + 32) * sizeof (const char *));
513 dirs[dir] = (char *) xmalloc (dir_len + 1);
514 memcpy (dirs[dir], filename, dir_len);
515 dirs[dir][dir_len] = '\0';
516 dirs_in_use = dir + 1;
520 if (num == 0)
522 for (i = 1; i < files_in_use; ++i)
523 if (files[i].dir == dir
524 && files[i].filename
525 && filename_cmp (file, files[i].filename) == 0)
527 last_used = i;
528 last_used_dir_len = dir_len;
529 return i;
532 else
533 i = num;
535 if (i >= files_allocated)
537 unsigned int old = files_allocated;
539 files_allocated = i + 32;
540 files = (struct file_entry *)
541 xrealloc (files, (i + 32) * sizeof (struct file_entry));
543 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
546 files[i].filename = num ? file : xstrdup (file);
547 files[i].dir = dir;
548 if (files_in_use < i + 1)
549 files_in_use = i + 1;
550 last_used = i;
551 last_used_dir_len = dir_len;
553 return i;
556 /* Handle two forms of .file directive:
557 - Pass .file "source.c" to s_app_file
558 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
560 If an entry is added to the file table, return a pointer to the filename. */
562 char *
563 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
565 offsetT num;
566 char *filename;
567 int filename_len;
569 /* Continue to accept a bare string and pass it off. */
570 SKIP_WHITESPACE ();
571 if (*input_line_pointer == '"')
573 s_app_file (0);
574 return NULL;
577 num = get_absolute_expression ();
578 filename = demand_copy_C_string (&filename_len);
579 if (filename == NULL)
580 return NULL;
581 demand_empty_rest_of_line ();
583 if (num < 1)
585 as_bad (_("file number less than one"));
586 return NULL;
589 /* A .file directive implies compiler generated debug information is
590 being supplied. Turn off gas generated debug info. */
591 debug_type = DEBUG_NONE;
593 if (num < (int) files_in_use && files[num].filename != 0)
595 as_bad (_("file number %ld already allocated"), (long) num);
596 return NULL;
599 get_filenum (filename, num);
601 return filename;
604 void
605 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
607 offsetT filenum, line;
609 /* If we see two .loc directives in a row, force the first one to be
610 output now. */
611 if (dwarf2_loc_directive_seen)
612 dwarf2_push_line (&current);
614 filenum = get_absolute_expression ();
615 SKIP_WHITESPACE ();
616 line = get_absolute_expression ();
618 if (filenum < 1)
620 as_bad (_("file number less than one"));
621 return;
623 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
625 as_bad (_("unassigned file number %ld"), (long) filenum);
626 return;
629 current.filenum = filenum;
630 current.line = line;
631 current.discriminator = 0;
633 #ifndef NO_LISTING
634 if (listing)
636 if (files[filenum].dir)
638 size_t dir_len = strlen (dirs[files[filenum].dir]);
639 size_t file_len = strlen (files[filenum].filename);
640 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
642 memcpy (cp, dirs[files[filenum].dir], dir_len);
643 INSERT_DIR_SEPARATOR (cp, dir_len);
644 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
645 cp[dir_len + file_len + 1] = '\0';
646 listing_source_file (cp);
648 else
649 listing_source_file (files[filenum].filename);
650 listing_source_line (line);
652 #endif
654 SKIP_WHITESPACE ();
655 if (ISDIGIT (*input_line_pointer))
657 current.column = get_absolute_expression ();
658 SKIP_WHITESPACE ();
661 while (ISALPHA (*input_line_pointer))
663 char *p, c;
664 offsetT value;
666 p = input_line_pointer;
667 c = get_symbol_end ();
669 if (strcmp (p, "basic_block") == 0)
671 current.flags |= DWARF2_FLAG_BASIC_BLOCK;
672 *input_line_pointer = c;
674 else if (strcmp (p, "prologue_end") == 0)
676 current.flags |= DWARF2_FLAG_PROLOGUE_END;
677 *input_line_pointer = c;
679 else if (strcmp (p, "epilogue_begin") == 0)
681 current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
682 *input_line_pointer = c;
684 else if (strcmp (p, "is_stmt") == 0)
686 *input_line_pointer = c;
687 value = get_absolute_expression ();
688 if (value == 0)
689 current.flags &= ~DWARF2_FLAG_IS_STMT;
690 else if (value == 1)
691 current.flags |= DWARF2_FLAG_IS_STMT;
692 else
694 as_bad (_("is_stmt value not 0 or 1"));
695 return;
698 else if (strcmp (p, "isa") == 0)
700 *input_line_pointer = c;
701 value = get_absolute_expression ();
702 if (value >= 0)
703 current.isa = value;
704 else
706 as_bad (_("isa number less than zero"));
707 return;
710 else if (strcmp (p, "discriminator") == 0)
712 *input_line_pointer = c;
713 value = get_absolute_expression ();
714 if (value >= 0)
715 current.discriminator = value;
716 else
718 as_bad (_("discriminator less than zero"));
719 return;
722 else
724 as_bad (_("unknown .loc sub-directive `%s'"), p);
725 *input_line_pointer = c;
726 return;
729 SKIP_WHITESPACE ();
732 demand_empty_rest_of_line ();
733 dwarf2_loc_directive_seen = TRUE;
734 debug_type = DEBUG_NONE;
737 void
738 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
740 offsetT value = get_absolute_expression ();
742 if (value != 0 && value != 1)
744 as_bad (_("expected 0 or 1"));
745 ignore_rest_of_line ();
747 else
749 dwarf2_loc_mark_labels = value != 0;
750 demand_empty_rest_of_line ();
754 static struct frag *
755 first_frag_for_seg (segT seg)
757 return seg_info (seg)->frchainP->frch_root;
760 static struct frag *
761 last_frag_for_seg (segT seg)
763 frchainS *f = seg_info (seg)->frchainP;
765 while (f->frch_next != NULL)
766 f = f->frch_next;
768 return f->frch_last;
771 /* Emit a single byte into the current segment. */
773 static inline void
774 out_byte (int byte)
776 FRAG_APPEND_1_CHAR (byte);
779 /* Emit a statement program opcode into the current segment. */
781 static inline void
782 out_opcode (int opc)
784 out_byte (opc);
787 /* Emit a two-byte word into the current segment. */
789 static inline void
790 out_two (int data)
792 md_number_to_chars (frag_more (2), data, 2);
795 /* Emit a four byte word into the current segment. */
797 static inline void
798 out_four (int data)
800 md_number_to_chars (frag_more (4), data, 4);
803 /* Emit an unsigned "little-endian base 128" number. */
805 static void
806 out_uleb128 (addressT value)
808 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
811 /* Emit a signed "little-endian base 128" number. */
813 static void
814 out_leb128 (addressT value)
816 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
819 /* Emit a tuple for .debug_abbrev. */
821 static inline void
822 out_abbrev (int name, int form)
824 out_uleb128 (name);
825 out_uleb128 (form);
828 /* Get the size of a fragment. */
830 static offsetT
831 get_frag_fix (fragS *frag, segT seg)
833 frchainS *fr;
835 if (frag->fr_next)
836 return frag->fr_fix;
838 /* If a fragment is the last in the chain, special measures must be
839 taken to find its size before relaxation, since it may be pending
840 on some subsegment chain. */
841 for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
842 if (fr->frch_last == frag)
843 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
845 abort ();
848 /* Set an absolute address (may result in a relocation entry). */
850 static void
851 out_set_addr (symbolS *sym)
853 expressionS exp;
855 out_opcode (DW_LNS_extended_op);
856 out_uleb128 (sizeof_address + 1);
858 out_opcode (DW_LNE_set_address);
859 exp.X_op = O_symbol;
860 exp.X_add_symbol = sym;
861 exp.X_add_number = 0;
862 emit_expr (&exp, sizeof_address);
865 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
866 static void scale_addr_delta (addressT *);
868 static void
869 scale_addr_delta (addressT *addr_delta)
871 static int printed_this = 0;
872 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
874 if (!printed_this)
875 as_bad("unaligned opcodes detected in executable segment");
876 printed_this = 1;
878 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
880 #else
881 #define scale_addr_delta(A)
882 #endif
884 /* Encode a pair of line and address skips as efficiently as possible.
885 Note that the line skip is signed, whereas the address skip is unsigned.
887 The following two routines *must* be kept in sync. This is
888 enforced by making emit_inc_line_addr abort if we do not emit
889 exactly the expected number of bytes. */
891 static int
892 size_inc_line_addr (int line_delta, addressT addr_delta)
894 unsigned int tmp, opcode;
895 int len = 0;
897 /* Scale the address delta by the minimum instruction length. */
898 scale_addr_delta (&addr_delta);
900 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
901 We cannot use special opcodes here, since we want the end_sequence
902 to emit the matrix entry. */
903 if (line_delta == INT_MAX)
905 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
906 len = 1;
907 else
908 len = 1 + sizeof_leb128 (addr_delta, 0);
909 return len + 3;
912 /* Bias the line delta by the base. */
913 tmp = line_delta - DWARF2_LINE_BASE;
915 /* If the line increment is out of range of a special opcode, we
916 must encode it with DW_LNS_advance_line. */
917 if (tmp >= DWARF2_LINE_RANGE)
919 len = 1 + sizeof_leb128 (line_delta, 1);
920 line_delta = 0;
921 tmp = 0 - DWARF2_LINE_BASE;
924 /* Bias the opcode by the special opcode base. */
925 tmp += DWARF2_LINE_OPCODE_BASE;
927 /* Avoid overflow when addr_delta is large. */
928 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
930 /* Try using a special opcode. */
931 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
932 if (opcode <= 255)
933 return len + 1;
935 /* Try using DW_LNS_const_add_pc followed by special op. */
936 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
937 if (opcode <= 255)
938 return len + 2;
941 /* Otherwise use DW_LNS_advance_pc. */
942 len += 1 + sizeof_leb128 (addr_delta, 0);
944 /* DW_LNS_copy or special opcode. */
945 len += 1;
947 return len;
950 static void
951 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
953 unsigned int tmp, opcode;
954 int need_copy = 0;
955 char *end = p + len;
957 /* Line number sequences cannot go backward in addresses. This means
958 we've incorrectly ordered the statements in the sequence. */
959 gas_assert ((offsetT) addr_delta >= 0);
961 /* Scale the address delta by the minimum instruction length. */
962 scale_addr_delta (&addr_delta);
964 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
965 We cannot use special opcodes here, since we want the end_sequence
966 to emit the matrix entry. */
967 if (line_delta == INT_MAX)
969 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
970 *p++ = DW_LNS_const_add_pc;
971 else
973 *p++ = DW_LNS_advance_pc;
974 p += output_leb128 (p, addr_delta, 0);
977 *p++ = DW_LNS_extended_op;
978 *p++ = 1;
979 *p++ = DW_LNE_end_sequence;
980 goto done;
983 /* Bias the line delta by the base. */
984 tmp = line_delta - DWARF2_LINE_BASE;
986 /* If the line increment is out of range of a special opcode, we
987 must encode it with DW_LNS_advance_line. */
988 if (tmp >= DWARF2_LINE_RANGE)
990 *p++ = DW_LNS_advance_line;
991 p += output_leb128 (p, line_delta, 1);
993 line_delta = 0;
994 tmp = 0 - DWARF2_LINE_BASE;
995 need_copy = 1;
998 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
999 special opcode. */
1000 if (line_delta == 0 && addr_delta == 0)
1002 *p++ = DW_LNS_copy;
1003 goto done;
1006 /* Bias the opcode by the special opcode base. */
1007 tmp += DWARF2_LINE_OPCODE_BASE;
1009 /* Avoid overflow when addr_delta is large. */
1010 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
1012 /* Try using a special opcode. */
1013 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
1014 if (opcode <= 255)
1016 *p++ = opcode;
1017 goto done;
1020 /* Try using DW_LNS_const_add_pc followed by special op. */
1021 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
1022 if (opcode <= 255)
1024 *p++ = DW_LNS_const_add_pc;
1025 *p++ = opcode;
1026 goto done;
1030 /* Otherwise use DW_LNS_advance_pc. */
1031 *p++ = DW_LNS_advance_pc;
1032 p += output_leb128 (p, addr_delta, 0);
1034 if (need_copy)
1035 *p++ = DW_LNS_copy;
1036 else
1037 *p++ = tmp;
1039 done:
1040 gas_assert (p == end);
1043 /* Handy routine to combine calls to the above two routines. */
1045 static void
1046 out_inc_line_addr (int line_delta, addressT addr_delta)
1048 int len = size_inc_line_addr (line_delta, addr_delta);
1049 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
1052 /* Write out an alternative form of line and address skips using
1053 DW_LNS_fixed_advance_pc opcodes. This uses more space than the default
1054 line and address information, but it is required if linker relaxation
1055 could change the code offsets. The following two routines *must* be
1056 kept in sync. */
1058 static int
1059 size_fixed_inc_line_addr (int line_delta, addressT addr_delta)
1061 int len = 0;
1063 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */
1064 if (line_delta != INT_MAX)
1065 len = 1 + sizeof_leb128 (line_delta, 1);
1067 if (addr_delta > 50000)
1069 /* DW_LNS_extended_op */
1070 len += 1 + sizeof_leb128 (sizeof_address + 1, 0);
1071 /* DW_LNE_set_address */
1072 len += 1 + sizeof_address;
1074 else
1075 /* DW_LNS_fixed_advance_pc */
1076 len += 3;
1078 if (line_delta == INT_MAX)
1079 /* DW_LNS_extended_op + DW_LNE_end_sequence */
1080 len += 3;
1081 else
1082 /* DW_LNS_copy */
1083 len += 1;
1085 return len;
1088 static void
1089 emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag,
1090 char *p, int len)
1092 expressionS *pexp;
1093 segT line_seg;
1094 char *end = p + len;
1096 /* Line number sequences cannot go backward in addresses. This means
1097 we've incorrectly ordered the statements in the sequence. */
1098 gas_assert ((offsetT) addr_delta >= 0);
1100 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */
1101 if (line_delta != INT_MAX)
1103 *p++ = DW_LNS_advance_line;
1104 p += output_leb128 (p, line_delta, 1);
1107 pexp = symbol_get_value_expression (frag->fr_symbol);
1108 line_seg = subseg_get (".debug_line", 0);
1110 /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can
1111 advance the address by at most 64K. Linker relaxation (without
1112 which this function would not be used) could change the operand by
1113 an unknown amount. If the address increment is getting close to
1114 the limit, just reset the address. */
1115 if (addr_delta > 50000)
1117 symbolS *to_sym;
1118 expressionS exp;
1120 gas_assert (pexp->X_op == O_subtract);
1121 to_sym = pexp->X_add_symbol;
1123 *p++ = DW_LNS_extended_op;
1124 p += output_leb128 (p, sizeof_address + 1, 0);
1125 *p++ = DW_LNE_set_address;
1126 exp.X_op = O_symbol;
1127 exp.X_add_symbol = to_sym;
1128 exp.X_add_number = 0;
1129 subseg_change (line_seg, 0);
1130 emit_expr_fix (&exp, sizeof_address, frag, p);
1131 p += sizeof_address;
1133 else
1135 *p++ = DW_LNS_fixed_advance_pc;
1136 subseg_change (line_seg, 0);
1137 emit_expr_fix (pexp, 2, frag, p);
1138 p += 2;
1141 if (line_delta == INT_MAX)
1143 *p++ = DW_LNS_extended_op;
1144 *p++ = 1;
1145 *p++ = DW_LNE_end_sequence;
1147 else
1148 *p++ = DW_LNS_copy;
1150 gas_assert (p == end);
1153 /* Generate a variant frag that we can use to relax address/line
1154 increments between fragments of the target segment. */
1156 static void
1157 relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
1159 expressionS exp;
1160 int max_chars;
1162 exp.X_op = O_subtract;
1163 exp.X_add_symbol = to_sym;
1164 exp.X_op_symbol = from_sym;
1165 exp.X_add_number = 0;
1167 /* The maximum size of the frag is the line delta with a maximum
1168 sized address delta. */
1169 if (DWARF2_USE_FIXED_ADVANCE_PC)
1170 max_chars = size_fixed_inc_line_addr (line_delta,
1171 -DWARF2_LINE_MIN_INSN_LENGTH);
1172 else
1173 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
1175 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
1176 make_expr_symbol (&exp), line_delta, NULL);
1179 /* The function estimates the size of a rs_dwarf2dbg variant frag
1180 based on the current values of the symbols. It is called before
1181 the relaxation loop. We set fr_subtype to the expected length. */
1184 dwarf2dbg_estimate_size_before_relax (fragS *frag)
1186 offsetT addr_delta;
1187 int size;
1189 addr_delta = resolve_symbol_value (frag->fr_symbol);
1190 if (DWARF2_USE_FIXED_ADVANCE_PC)
1191 size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta);
1192 else
1193 size = size_inc_line_addr (frag->fr_offset, addr_delta);
1195 frag->fr_subtype = size;
1197 return size;
1200 /* This function relaxes a rs_dwarf2dbg variant frag based on the
1201 current values of the symbols. fr_subtype is the current length
1202 of the frag. This returns the change in frag length. */
1205 dwarf2dbg_relax_frag (fragS *frag)
1207 int old_size, new_size;
1209 old_size = frag->fr_subtype;
1210 new_size = dwarf2dbg_estimate_size_before_relax (frag);
1212 return new_size - old_size;
1215 /* This function converts a rs_dwarf2dbg variant frag into a normal
1216 fill frag. This is called after all relaxation has been done.
1217 fr_subtype will be the desired length of the frag. */
1219 void
1220 dwarf2dbg_convert_frag (fragS *frag)
1222 offsetT addr_diff;
1224 addr_diff = resolve_symbol_value (frag->fr_symbol);
1226 /* fr_var carries the max_chars that we created the fragment with.
1227 fr_subtype carries the current expected length. We must, of
1228 course, have allocated enough memory earlier. */
1229 gas_assert (frag->fr_var >= (int) frag->fr_subtype);
1231 if (DWARF2_USE_FIXED_ADVANCE_PC)
1232 emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag,
1233 frag->fr_literal + frag->fr_fix,
1234 frag->fr_subtype);
1235 else
1236 emit_inc_line_addr (frag->fr_offset, addr_diff,
1237 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
1239 frag->fr_fix += frag->fr_subtype;
1240 frag->fr_type = rs_fill;
1241 frag->fr_var = 0;
1242 frag->fr_offset = 0;
1245 /* Generate .debug_line content for the chain of line number entries
1246 beginning at E, for segment SEG. */
1248 static void
1249 process_entries (segT seg, struct line_entry *e)
1251 unsigned filenum = 1;
1252 unsigned line = 1;
1253 unsigned column = 0;
1254 unsigned isa = 0;
1255 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1256 fragS *last_frag = NULL, *frag;
1257 addressT last_frag_ofs = 0, frag_ofs;
1258 symbolS *last_lab = NULL, *lab;
1259 struct line_entry *next;
1263 int line_delta;
1265 if (filenum != e->loc.filenum)
1267 filenum = e->loc.filenum;
1268 out_opcode (DW_LNS_set_file);
1269 out_uleb128 (filenum);
1272 if (column != e->loc.column)
1274 column = e->loc.column;
1275 out_opcode (DW_LNS_set_column);
1276 out_uleb128 (column);
1279 if (e->loc.discriminator != 0)
1281 out_opcode (DW_LNS_extended_op);
1282 out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0));
1283 out_opcode (DW_LNE_set_discriminator);
1284 out_uleb128 (e->loc.discriminator);
1287 if (isa != e->loc.isa)
1289 isa = e->loc.isa;
1290 out_opcode (DW_LNS_set_isa);
1291 out_uleb128 (isa);
1294 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
1296 flags = e->loc.flags;
1297 out_opcode (DW_LNS_negate_stmt);
1300 if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
1301 out_opcode (DW_LNS_set_basic_block);
1303 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
1304 out_opcode (DW_LNS_set_prologue_end);
1306 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1307 out_opcode (DW_LNS_set_epilogue_begin);
1309 /* Don't try to optimize away redundant entries; gdb wants two
1310 entries for a function where the code starts on the same line as
1311 the {, and there's no way to identify that case here. Trust gcc
1312 to optimize appropriately. */
1313 line_delta = e->loc.line - line;
1314 lab = e->label;
1315 frag = symbol_get_frag (lab);
1316 frag_ofs = S_GET_VALUE (lab);
1318 if (last_frag == NULL)
1320 out_set_addr (lab);
1321 out_inc_line_addr (line_delta, 0);
1323 else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1324 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
1325 else
1326 relax_inc_line_addr (line_delta, lab, last_lab);
1328 line = e->loc.line;
1329 last_lab = lab;
1330 last_frag = frag;
1331 last_frag_ofs = frag_ofs;
1333 next = e->next;
1334 free (e);
1335 e = next;
1337 while (e);
1339 /* Emit a DW_LNE_end_sequence for the end of the section. */
1340 frag = last_frag_for_seg (seg);
1341 frag_ofs = get_frag_fix (frag, seg);
1342 if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
1343 out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
1344 else
1346 lab = symbol_temp_new (seg, frag_ofs, frag);
1347 relax_inc_line_addr (INT_MAX, lab, last_lab);
1351 /* Emit the directory and file tables for .debug_line. */
1353 static void
1354 out_file_list (void)
1356 size_t size;
1357 const char *dir;
1358 char *cp;
1359 unsigned int i;
1361 /* Emit directory list. */
1362 for (i = 1; i < dirs_in_use; ++i)
1364 dir = remap_debug_filename (dirs[i]);
1365 size = strlen (dir) + 1;
1366 cp = frag_more (size);
1367 memcpy (cp, dir, size);
1369 /* Terminate it. */
1370 out_byte ('\0');
1372 for (i = 1; i < files_in_use; ++i)
1374 const char *fullfilename;
1376 if (files[i].filename == NULL)
1378 as_bad (_("unassigned file number %ld"), (long) i);
1379 /* Prevent a crash later, particularly for file 1. */
1380 files[i].filename = "";
1381 continue;
1384 fullfilename = DWARF2_FILE_NAME (files[i].filename,
1385 files[i].dir ? dirs [files [i].dir] : "");
1386 size = strlen (fullfilename) + 1;
1387 cp = frag_more (size);
1388 memcpy (cp, fullfilename, size);
1390 out_uleb128 (files[i].dir); /* directory number */
1391 /* Output the last modification timestamp. */
1392 out_uleb128 (DWARF2_FILE_TIME_NAME (files[i].filename,
1393 files[i].dir ? dirs [files [i].dir] : ""));
1394 /* Output the filesize. */
1395 out_uleb128 (DWARF2_FILE_SIZE_NAME (files[i].filename,
1396 files[i].dir ? dirs [files [i].dir] : ""));
1399 /* Terminate filename list. */
1400 out_byte (0);
1403 /* Switch to SEC and output a header length field. Return the size of
1404 offsets used in SEC. The caller must set EXPR->X_add_symbol value
1405 to the end of the section. */
1407 static int
1408 out_header (asection *sec, expressionS *exp)
1410 symbolS *start_sym;
1411 symbolS *end_sym;
1413 subseg_set (sec, 0);
1414 start_sym = symbol_temp_new_now ();;
1415 end_sym = symbol_temp_make ();
1417 /* Total length of the information. */
1418 exp->X_op = O_subtract;
1419 exp->X_add_symbol = end_sym;
1420 exp->X_op_symbol = start_sym;
1422 switch (DWARF2_FORMAT (sec))
1424 case dwarf2_format_32bit:
1425 exp->X_add_number = -4;
1426 emit_expr (exp, 4);
1427 return 4;
1429 case dwarf2_format_64bit:
1430 exp->X_add_number = -12;
1431 out_four (-1);
1432 emit_expr (exp, 8);
1433 return 8;
1435 case dwarf2_format_64bit_irix:
1436 exp->X_add_number = -8;
1437 emit_expr (exp, 8);
1438 return 8;
1441 as_fatal (_("internal error: unknown dwarf2 format"));
1442 return 0;
1445 /* Emit the collected .debug_line data. */
1447 static void
1448 out_debug_line (segT line_seg)
1450 expressionS exp;
1451 symbolS *prologue_end;
1452 symbolS *line_end;
1453 struct line_seg *s;
1454 int sizeof_offset;
1456 sizeof_offset = out_header (line_seg, &exp);
1457 line_end = exp.X_add_symbol;
1459 /* Version. */
1460 out_two (DWARF2_VERSION);
1462 /* Length of the prologue following this length. */
1463 prologue_end = symbol_temp_make ();
1464 exp.X_add_symbol = prologue_end;
1465 exp.X_add_number = - (4 + 2 + 4);
1466 emit_expr (&exp, sizeof_offset);
1468 /* Parameters of the state machine. */
1469 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1470 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1471 out_byte (DWARF2_LINE_BASE);
1472 out_byte (DWARF2_LINE_RANGE);
1473 out_byte (DWARF2_LINE_OPCODE_BASE);
1475 /* Standard opcode lengths. */
1476 out_byte (0); /* DW_LNS_copy */
1477 out_byte (1); /* DW_LNS_advance_pc */
1478 out_byte (1); /* DW_LNS_advance_line */
1479 out_byte (1); /* DW_LNS_set_file */
1480 out_byte (1); /* DW_LNS_set_column */
1481 out_byte (0); /* DW_LNS_negate_stmt */
1482 out_byte (0); /* DW_LNS_set_basic_block */
1483 out_byte (0); /* DW_LNS_const_add_pc */
1484 out_byte (1); /* DW_LNS_fixed_advance_pc */
1485 out_byte (0); /* DW_LNS_set_prologue_end */
1486 out_byte (0); /* DW_LNS_set_epilogue_begin */
1487 out_byte (1); /* DW_LNS_set_isa */
1489 out_file_list ();
1491 symbol_set_value_now (prologue_end);
1493 /* For each section, emit a statement program. */
1494 for (s = all_segs; s; s = s->next)
1495 if (SEG_NORMAL (s->seg))
1496 process_entries (s->seg, s->head->head);
1497 else
1498 as_warn ("dwarf line number information for %s ignored",
1499 segment_name (s->seg));
1501 symbol_set_value_now (line_end);
1504 static void
1505 out_debug_ranges (segT ranges_seg)
1507 unsigned int addr_size = sizeof_address;
1508 struct line_seg *s;
1509 expressionS exp;
1510 unsigned int i;
1512 subseg_set (ranges_seg, 0);
1514 /* Base Address Entry. */
1515 for (i = 0; i < addr_size; i++)
1516 out_byte (0xff);
1517 for (i = 0; i < addr_size; i++)
1518 out_byte (0);
1520 /* Range List Entry. */
1521 for (s = all_segs; s; s = s->next)
1523 fragS *frag;
1524 symbolS *beg, *end;
1526 frag = first_frag_for_seg (s->seg);
1527 beg = symbol_temp_new (s->seg, 0, frag);
1528 s->text_start = beg;
1530 frag = last_frag_for_seg (s->seg);
1531 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1532 s->text_end = end;
1534 exp.X_op = O_symbol;
1535 exp.X_add_symbol = beg;
1536 exp.X_add_number = 0;
1537 emit_expr (&exp, addr_size);
1539 exp.X_op = O_symbol;
1540 exp.X_add_symbol = end;
1541 exp.X_add_number = 0;
1542 emit_expr (&exp, addr_size);
1545 /* End of Range Entry. */
1546 for (i = 0; i < addr_size; i++)
1547 out_byte (0);
1548 for (i = 0; i < addr_size; i++)
1549 out_byte (0);
1552 /* Emit data for .debug_aranges. */
1554 static void
1555 out_debug_aranges (segT aranges_seg, segT info_seg)
1557 unsigned int addr_size = sizeof_address;
1558 struct line_seg *s;
1559 expressionS exp;
1560 symbolS *aranges_end;
1561 char *p;
1562 int sizeof_offset;
1564 sizeof_offset = out_header (aranges_seg, &exp);
1565 aranges_end = exp.X_add_symbol;
1567 /* Version. */
1568 out_two (DWARF2_VERSION);
1570 /* Offset to .debug_info. */
1571 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset);
1573 /* Size of an address (offset portion). */
1574 out_byte (addr_size);
1576 /* Size of a segment descriptor. */
1577 out_byte (0);
1579 /* Align the header. */
1580 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1582 for (s = all_segs; s; s = s->next)
1584 fragS *frag;
1585 symbolS *beg, *end;
1587 frag = first_frag_for_seg (s->seg);
1588 beg = symbol_temp_new (s->seg, 0, frag);
1589 s->text_start = beg;
1591 frag = last_frag_for_seg (s->seg);
1592 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1593 s->text_end = end;
1595 exp.X_op = O_symbol;
1596 exp.X_add_symbol = beg;
1597 exp.X_add_number = 0;
1598 emit_expr (&exp, addr_size);
1600 exp.X_op = O_subtract;
1601 exp.X_add_symbol = end;
1602 exp.X_op_symbol = beg;
1603 exp.X_add_number = 0;
1604 emit_expr (&exp, addr_size);
1607 p = frag_more (2 * addr_size);
1608 md_number_to_chars (p, 0, addr_size);
1609 md_number_to_chars (p + addr_size, 0, addr_size);
1611 symbol_set_value_now (aranges_end);
1614 /* Emit data for .debug_abbrev. Note that this must be kept in
1615 sync with out_debug_info below. */
1617 static void
1618 out_debug_abbrev (segT abbrev_seg,
1619 segT info_seg ATTRIBUTE_UNUSED,
1620 segT line_seg ATTRIBUTE_UNUSED)
1622 subseg_set (abbrev_seg, 0);
1624 out_uleb128 (1);
1625 out_uleb128 (DW_TAG_compile_unit);
1626 out_byte (DW_CHILDREN_no);
1627 if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit)
1628 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1629 else
1630 out_abbrev (DW_AT_stmt_list, DW_FORM_data8);
1631 if (all_segs->next == NULL)
1633 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1634 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1636 else
1638 if (DWARF2_FORMAT (info_seg) == dwarf2_format_32bit)
1639 out_abbrev (DW_AT_ranges, DW_FORM_data4);
1640 else
1641 out_abbrev (DW_AT_ranges, DW_FORM_data8);
1643 out_abbrev (DW_AT_name, DW_FORM_string);
1644 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1645 out_abbrev (DW_AT_producer, DW_FORM_string);
1646 out_abbrev (DW_AT_language, DW_FORM_data2);
1647 out_abbrev (0, 0);
1649 /* Terminate the abbreviations for this compilation unit. */
1650 out_byte (0);
1653 /* Emit a description of this compilation unit for .debug_info. */
1655 static void
1656 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg)
1658 char producer[128];
1659 const char *comp_dir;
1660 const char *dirname;
1661 expressionS exp;
1662 symbolS *info_end;
1663 char *p;
1664 int len;
1665 int sizeof_offset;
1667 sizeof_offset = out_header (info_seg, &exp);
1668 info_end = exp.X_add_symbol;
1670 /* DWARF version. */
1671 out_two (DWARF2_VERSION);
1673 /* .debug_abbrev offset */
1674 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1676 /* Target address size. */
1677 out_byte (sizeof_address);
1679 /* DW_TAG_compile_unit DIE abbrev */
1680 out_uleb128 (1);
1682 /* DW_AT_stmt_list */
1683 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg),
1684 (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit
1685 ? 4 : 8));
1687 /* These two attributes are emitted if all of the code is contiguous. */
1688 if (all_segs->next == NULL)
1690 /* DW_AT_low_pc */
1691 exp.X_op = O_symbol;
1692 exp.X_add_symbol = all_segs->text_start;
1693 exp.X_add_number = 0;
1694 emit_expr (&exp, sizeof_address);
1696 /* DW_AT_high_pc */
1697 exp.X_op = O_symbol;
1698 exp.X_add_symbol = all_segs->text_end;
1699 exp.X_add_number = 0;
1700 emit_expr (&exp, sizeof_address);
1702 else
1704 /* This attribute is emitted if the code is disjoint. */
1705 /* DW_AT_ranges. */
1706 TC_DWARF2_EMIT_OFFSET (section_symbol (ranges_seg), sizeof_offset);
1709 /* DW_AT_name. We don't have the actual file name that was present
1710 on the command line, so assume files[1] is the main input file.
1711 We're not supposed to get called unless at least one line number
1712 entry was emitted, so this should always be defined. */
1713 if (files_in_use == 0)
1714 abort ();
1715 if (files[1].dir)
1717 dirname = remap_debug_filename (dirs[files[1].dir]);
1718 len = strlen (dirname);
1719 #ifdef TE_VMS
1720 /* Already has trailing slash. */
1721 p = frag_more (len);
1722 memcpy (p, dirname, len);
1723 #else
1724 p = frag_more (len + 1);
1725 memcpy (p, dirname, len);
1726 INSERT_DIR_SEPARATOR (p, len);
1727 #endif
1729 len = strlen (files[1].filename) + 1;
1730 p = frag_more (len);
1731 memcpy (p, files[1].filename, len);
1733 /* DW_AT_comp_dir */
1734 comp_dir = remap_debug_filename (getpwd ());
1735 len = strlen (comp_dir) + 1;
1736 p = frag_more (len);
1737 memcpy (p, comp_dir, len);
1739 /* DW_AT_producer */
1740 sprintf (producer, "GNU AS %s", VERSION);
1741 len = strlen (producer) + 1;
1742 p = frag_more (len);
1743 memcpy (p, producer, len);
1745 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1746 dwarf2 draft has no standard code for assembler. */
1747 out_two (DW_LANG_Mips_Assembler);
1749 symbol_set_value_now (info_end);
1752 void
1753 dwarf2_init (void)
1755 all_segs_hash = hash_new ();
1756 last_seg_ptr = &all_segs;
1760 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1761 were any .file/.loc directives, or --gdwarf2 was given, or if the
1762 file has a non-empty .debug_info section and an empty .debug_line
1763 section. If we emit .debug_line, and the .debug_info section is
1764 empty, we also emit .debug_info, .debug_aranges and .debug_abbrev.
1765 ALL_SEGS will be non-null if there were any .file/.loc directives,
1766 or --gdwarf2 was given and there were any located instructions
1767 emitted. */
1769 void
1770 dwarf2_finish (void)
1772 segT line_seg;
1773 struct line_seg *s;
1774 segT info_seg;
1775 int emit_other_sections = 0;
1776 int empty_debug_line = 0;
1778 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
1779 emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
1781 line_seg = bfd_get_section_by_name (stdoutput, ".debug_line");
1782 empty_debug_line = line_seg == NULL || !seg_not_empty_p (line_seg);
1784 /* We can't construct a new debug_line section if we already have one.
1785 Give an error. */
1786 if (all_segs && !empty_debug_line)
1787 as_fatal ("duplicate .debug_line sections");
1789 if ((!all_segs && emit_other_sections)
1790 || (!emit_other_sections && !empty_debug_line))
1791 /* If there is no line information and no non-empty .debug_info
1792 section, or if there is both a non-empty .debug_info and a non-empty
1793 .debug_line, then we do nothing. */
1794 return;
1796 /* Calculate the size of an address for the target machine. */
1797 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1799 /* Create and switch to the line number section. */
1800 line_seg = subseg_new (".debug_line", 0);
1801 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
1803 /* For each subsection, chain the debug entries together. */
1804 for (s = all_segs; s; s = s->next)
1806 struct line_subseg *lss = s->head;
1807 struct line_entry **ptail = lss->ptail;
1809 while ((lss = lss->next) != NULL)
1811 *ptail = lss->head;
1812 ptail = lss->ptail;
1816 out_debug_line (line_seg);
1818 /* If this is assembler generated line info, and there is no
1819 debug_info already, we need .debug_info and .debug_abbrev
1820 sections as well. */
1821 if (emit_other_sections)
1823 segT abbrev_seg;
1824 segT aranges_seg;
1825 segT ranges_seg;
1827 gas_assert (all_segs);
1829 info_seg = subseg_new (".debug_info", 0);
1830 abbrev_seg = subseg_new (".debug_abbrev", 0);
1831 aranges_seg = subseg_new (".debug_aranges", 0);
1833 bfd_set_section_flags (stdoutput, info_seg,
1834 SEC_READONLY | SEC_DEBUGGING);
1835 bfd_set_section_flags (stdoutput, abbrev_seg,
1836 SEC_READONLY | SEC_DEBUGGING);
1837 bfd_set_section_flags (stdoutput, aranges_seg,
1838 SEC_READONLY | SEC_DEBUGGING);
1840 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1842 if (all_segs->next == NULL)
1843 ranges_seg = NULL;
1844 else
1846 ranges_seg = subseg_new (".debug_ranges", 0);
1847 bfd_set_section_flags (stdoutput, ranges_seg,
1848 SEC_READONLY | SEC_DEBUGGING);
1849 record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1);
1850 out_debug_ranges (ranges_seg);
1853 out_debug_aranges (aranges_seg, info_seg);
1854 out_debug_abbrev (abbrev_seg, info_seg, line_seg);
1855 out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg);