ldmain.c (ld_canon_sysroot, ld_canon_sysroot_len): Define. (main): Initialize them.
[binutils.git] / gas / dwarf2dbg.c
blob3b863cd2ec0cc545d4f8f24864b0df6a73207c0e
1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* Logical line numbers can be controlled by the compiler via the
23 following two directives:
25 .file FILENO "file.c"
26 .loc FILENO LINENO [COLUMN]
28 FILENO is the filenumber. */
30 #include "ansidecl.h"
31 #include "as.h"
33 #ifdef HAVE_LIMITS_H
34 #include <limits.h>
35 #else
36 #ifdef HAVE_SYS_PARAM_H
37 #include <sys/param.h>
38 #endif
39 #ifndef INT_MAX
40 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
41 #endif
42 #endif
44 #include "dwarf2dbg.h"
45 #include <filenames.h>
47 #ifndef DWARF2_FORMAT
48 # define DWARF2_FORMAT() dwarf2_format_32bit
49 #endif
51 #ifndef DWARF2_ADDR_SIZE
52 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8);
53 #endif
55 #ifndef TC_DWARF2_EMIT_OFFSET
56 # define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
57 #endif
59 #ifdef BFD_ASSEMBLER
61 #include "subsegs.h"
63 #include "elf/dwarf2.h"
65 /* Since we can't generate the prolog until the body is complete, we
66 use three different subsegments for .debug_line: one holding the
67 prolog, one for the directory and filename info, and one for the
68 body ("statement program"). */
69 #define DL_PROLOG 0
70 #define DL_FILES 1
71 #define DL_BODY 2
73 /* First special line opcde - leave room for the standard opcodes.
74 Note: If you want to change this, you'll have to update the
75 "standard_opcode_lengths" table that is emitted below in
76 dwarf2_finish(). */
77 #define DWARF2_LINE_OPCODE_BASE 10
79 #ifndef DWARF2_LINE_BASE
80 /* Minimum line offset in a special line info. opcode. This value
81 was chosen to give a reasonable range of values. */
82 # define DWARF2_LINE_BASE -5
83 #endif
85 /* Range of line offsets in a special line info. opcode. */
86 #ifndef DWARF2_LINE_RANGE
87 # define DWARF2_LINE_RANGE 14
88 #endif
90 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
91 /* Define the architecture-dependent minimum instruction length (in
92 bytes). This value should be rather too small than too big. */
93 # define DWARF2_LINE_MIN_INSN_LENGTH 1
94 #endif
96 /* Flag that indicates the initial value of the is_stmt_start flag.
97 In the present implementation, we do not mark any lines as
98 the beginning of a source statement, because that information
99 is not made available by the GCC front-end. */
100 #define DWARF2_LINE_DEFAULT_IS_STMT 1
102 /* Given a special op, return the line skip amount. */
103 #define SPECIAL_LINE(op) \
104 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
106 /* Given a special op, return the address skip amount (in units of
107 DWARF2_LINE_MIN_INSN_LENGTH. */
108 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
110 /* The maximum address skip amount that can be encoded with a special op. */
111 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
113 struct line_entry {
114 struct line_entry *next;
115 fragS *frag;
116 addressT frag_ofs;
117 struct dwarf2_line_info loc;
120 struct line_subseg {
121 struct line_subseg *next;
122 subsegT subseg;
123 struct line_entry *head;
124 struct line_entry **ptail;
127 struct line_seg {
128 struct line_seg *next;
129 segT seg;
130 struct line_subseg *head;
131 symbolS *text_start;
132 symbolS *text_end;
135 /* Collects data for all line table entries during assembly. */
136 static struct line_seg *all_segs;
138 struct file_entry {
139 const char *filename;
140 unsigned int dir;
143 /* Table of files used by .debug_line. */
144 static struct file_entry *files;
145 static unsigned int files_in_use;
146 static unsigned int files_allocated;
148 /* Table of directories used by .debug_line. */
149 static char **dirs;
150 static unsigned int dirs_in_use;
151 static unsigned int dirs_allocated;
153 /* TRUE when we've seen a .loc directive recently. Used to avoid
154 doing work when there's nothing to do. */
155 static bfd_boolean loc_directive_seen;
157 /* Current location as indicated by the most recent .loc directive. */
158 static struct dwarf2_line_info current;
160 /* Fake label name. */
161 static char const fake_label_name[] = ".L0\001";
163 /* The size of an address on the target. */
164 static unsigned int sizeof_address;
166 static void generic_dwarf2_emit_offset PARAMS((symbolS *, unsigned int));
167 static struct line_subseg *get_line_subseg PARAMS ((segT, subsegT));
168 static unsigned int get_filenum PARAMS ((const char *, unsigned int));
169 static struct frag *first_frag_for_seg PARAMS ((segT));
170 static struct frag *last_frag_for_seg PARAMS ((segT));
171 static void out_byte PARAMS ((int));
172 static void out_opcode PARAMS ((int));
173 static void out_two PARAMS ((int));
174 static void out_four PARAMS ((int));
175 static void out_abbrev PARAMS ((int, int));
176 static void out_uleb128 PARAMS ((addressT));
177 static symbolS *symbol_new_now PARAMS ((void));
178 static void set_symbol_value_now PARAMS ((symbolS *));
179 static offsetT get_frag_fix PARAMS ((fragS *));
180 static void out_set_addr PARAMS ((segT, fragS *, addressT));
181 static int size_inc_line_addr PARAMS ((int, addressT));
182 static void emit_inc_line_addr PARAMS ((int, addressT, char *, int));
183 static void out_inc_line_addr PARAMS ((int, addressT));
184 static void relax_inc_line_addr PARAMS ((int, segT, fragS *, addressT,
185 fragS *, addressT));
186 static void process_entries PARAMS ((segT, struct line_entry *));
187 static void out_file_list PARAMS ((void));
188 static void out_debug_line PARAMS ((segT));
189 static void out_debug_aranges PARAMS ((segT, segT));
190 static void out_debug_abbrev PARAMS ((segT));
191 static void out_debug_info PARAMS ((segT, segT, segT));
193 /* Create an offset to .dwarf2_*. */
195 static void
196 generic_dwarf2_emit_offset (symbol, size)
197 symbolS *symbol;
198 unsigned int size;
200 expressionS expr;
202 expr.X_op = O_symbol;
203 expr.X_add_symbol = symbol;
204 expr.X_add_number = 0;
205 emit_expr (&expr, size);
208 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
210 static struct line_subseg *
211 get_line_subseg (seg, subseg)
212 segT seg;
213 subsegT subseg;
215 static segT last_seg;
216 static subsegT last_subseg;
217 static struct line_subseg *last_line_subseg;
219 struct line_seg *s;
220 struct line_subseg **pss, *ss;
222 if (seg == last_seg && subseg == last_subseg)
223 return last_line_subseg;
225 for (s = all_segs; s; s = s->next)
226 if (s->seg == seg)
227 goto found_seg;
229 s = (struct line_seg *) xmalloc (sizeof (*s));
230 s->next = all_segs;
231 s->seg = seg;
232 s->head = NULL;
233 all_segs = s;
235 found_seg:
236 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
238 if (ss->subseg == subseg)
239 goto found_subseg;
240 if (ss->subseg > subseg)
241 break;
244 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
245 ss->next = *pss;
246 ss->subseg = subseg;
247 ss->head = NULL;
248 ss->ptail = &ss->head;
249 *pss = ss;
251 found_subseg:
252 last_seg = seg;
253 last_subseg = subseg;
254 last_line_subseg = ss;
256 return ss;
259 /* Record an entry for LOC ocurring at OFS within the current fragment. */
261 void
262 dwarf2_gen_line_info (ofs, loc)
263 addressT ofs;
264 struct dwarf2_line_info *loc;
266 struct line_subseg *ss;
267 struct line_entry *e;
268 static unsigned int line = -1;
269 static unsigned int filenum = -1;
271 /* Early out for as-yet incomplete location information. */
272 if (loc->filenum == 0 || loc->line == 0)
273 return;
275 /* Don't emit sequences of line symbols for the same line when the
276 symbols apply to assembler code. It is necessary to emit
277 duplicate line symbols when a compiler asks for them, because GDB
278 uses them to determine the end of the prologue. */
279 if (debug_type == DEBUG_DWARF2
280 && line == loc->line && filenum == loc->filenum)
281 return;
283 line = loc->line;
284 filenum = loc->filenum;
286 e = (struct line_entry *) xmalloc (sizeof (*e));
287 e->next = NULL;
288 e->frag = frag_now;
289 e->frag_ofs = ofs;
290 e->loc = *loc;
292 ss = get_line_subseg (now_seg, now_subseg);
293 *ss->ptail = e;
294 ss->ptail = &e->next;
297 void
298 dwarf2_where (line)
299 struct dwarf2_line_info *line;
301 if (debug_type == DEBUG_DWARF2)
303 char *filename;
304 as_where (&filename, &line->line);
305 line->filenum = get_filenum (filename, 0);
306 line->column = 0;
307 line->flags = DWARF2_FLAG_BEGIN_STMT;
309 else
310 *line = current;
313 /* Called for each machine instruction, or relatively atomic group of
314 machine instructions (ie built-in macro). The instruction or group
315 is SIZE bytes in length. If dwarf2 line number generation is called
316 for, emit a line statement appropriately. */
318 void
319 dwarf2_emit_insn (size)
320 int size;
322 struct dwarf2_line_info loc;
324 if (loc_directive_seen)
326 /* Use the last location established by a .loc directive, not
327 the value returned by dwarf2_where(). That calls as_where()
328 which will return either the logical input file name (foo.c)
329 or the physical input file name (foo.s) and not the file name
330 specified in the most recent .loc directive (eg foo.h). */
331 loc = current;
333 /* Unless we generate DWARF2 debugging information for each
334 assembler line, we only emit one line symbol for one LOC. */
335 if (debug_type != DEBUG_DWARF2)
336 loc_directive_seen = FALSE;
338 else if (debug_type != DEBUG_DWARF2)
339 return;
340 else
341 dwarf2_where (& loc);
343 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
346 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
347 allocate it on that file table slot, otherwise return the first
348 empty one. */
350 static unsigned int
351 get_filenum (filename, num)
352 const char *filename;
353 unsigned int num;
355 static unsigned int last_used, last_used_dir_len;
356 const char *file;
357 size_t dir_len;
358 unsigned int i, dir;
360 if (num == 0 && last_used)
362 if (! files[last_used].dir
363 && strcmp (filename, files[last_used].filename) == 0)
364 return last_used;
365 if (files[last_used].dir
366 && strncmp (filename, dirs[files[last_used].dir],
367 last_used_dir_len) == 0
368 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
369 && strcmp (filename + last_used_dir_len + 1,
370 files[last_used].filename) == 0)
371 return last_used;
374 file = lbasename (filename);
375 /* Don't make empty string from / or A: from A:/ . */
376 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
377 if (file <= filename + 3)
378 file = filename;
379 #else
380 if (file == filename + 1)
381 file = filename;
382 #endif
383 dir_len = file - filename;
385 dir = 0;
386 if (dir_len)
388 --dir_len;
389 for (dir = 1; dir < dirs_in_use; ++dir)
390 if (memcmp (filename, dirs[dir], dir_len) == 0
391 && dirs[dir][dir_len] == '\0')
392 break;
394 if (dir >= dirs_in_use)
396 if (dir >= dirs_allocated)
398 dirs_allocated = dir + 32;
399 dirs = (char **)
400 xrealloc (dirs, (dir + 32) * sizeof (const char *));
403 dirs[dir] = xmalloc (dir_len + 1);
404 memcpy (dirs[dir], filename, dir_len);
405 dirs[dir][dir_len] = '\0';
406 dirs_in_use = dir + 1;
410 if (num == 0)
412 for (i = 1; i < files_in_use; ++i)
413 if (files[i].dir == dir
414 && strcmp (file, files[i].filename) == 0)
416 last_used = i;
417 last_used_dir_len = dir_len;
418 return i;
421 else
422 i = num;
424 if (i >= files_allocated)
426 unsigned int old = files_allocated;
428 files_allocated = i + 32;
429 files = (struct file_entry *)
430 xrealloc (files, (i + 32) * sizeof (struct file_entry));
432 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
435 files[i].filename = num ? file : xstrdup (file);
436 files[i].dir = dir;
437 files_in_use = i + 1;
438 last_used = i;
439 last_used_dir_len = dir_len;
441 return i;
444 /* Handle two forms of .file directive:
445 - Pass .file "source.c" to s_app_file
446 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
448 If an entry is added to the file table, return a pointer to the filename. */
450 char *
451 dwarf2_directive_file (dummy)
452 int dummy ATTRIBUTE_UNUSED;
454 offsetT num;
455 char *filename;
456 int filename_len;
458 /* Continue to accept a bare string and pass it off. */
459 SKIP_WHITESPACE ();
460 if (*input_line_pointer == '"')
462 s_app_file (0);
463 return NULL;
466 num = get_absolute_expression ();
467 filename = demand_copy_C_string (&filename_len);
468 demand_empty_rest_of_line ();
470 if (num < 1)
472 as_bad (_("file number less than one"));
473 return NULL;
476 if (num < (int) files_in_use && files[num].filename != 0)
478 as_bad (_("file number %ld already allocated"), (long) num);
479 return NULL;
482 get_filenum (filename, num);
484 return filename;
487 void
488 dwarf2_directive_loc (dummy)
489 int dummy ATTRIBUTE_UNUSED;
491 offsetT filenum, line, column;
493 filenum = get_absolute_expression ();
494 SKIP_WHITESPACE ();
495 line = get_absolute_expression ();
496 SKIP_WHITESPACE ();
497 column = get_absolute_expression ();
498 demand_empty_rest_of_line ();
500 if (filenum < 1)
502 as_bad (_("file number less than one"));
503 return;
505 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
507 as_bad (_("unassigned file number %ld"), (long) filenum);
508 return;
511 current.filenum = filenum;
512 current.line = line;
513 current.column = column;
514 current.flags = DWARF2_FLAG_BEGIN_STMT;
516 loc_directive_seen = TRUE;
518 #ifndef NO_LISTING
519 if (listing)
521 if (files[filenum].dir)
523 size_t dir_len = strlen (dirs[files[filenum].dir]);
524 size_t file_len = strlen (files[filenum].filename);
525 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
527 memcpy (cp, dirs[files[filenum].dir], dir_len);
528 cp[dir_len] = '/';
529 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
530 cp[dir_len + file_len + 1] = '\0';
531 listing_source_file (cp);
533 else
534 listing_source_file (files[filenum].filename);
535 listing_source_line (line);
537 #endif
540 static struct frag *
541 first_frag_for_seg (seg)
542 segT seg;
544 frchainS *f, *first = NULL;
546 for (f = frchain_root; f; f = f->frch_next)
547 if (f->frch_seg == seg
548 && (! first || first->frch_subseg > f->frch_subseg))
549 first = f;
551 return first ? first->frch_root : NULL;
554 static struct frag *
555 last_frag_for_seg (seg)
556 segT seg;
558 frchainS *f, *last = NULL;
560 for (f = frchain_root; f; f = f->frch_next)
561 if (f->frch_seg == seg
562 && (! last || last->frch_subseg < f->frch_subseg))
563 last= f;
565 return last ? last->frch_last : NULL;
568 /* Emit a single byte into the current segment. */
570 static inline void
571 out_byte (byte)
572 int byte;
574 FRAG_APPEND_1_CHAR (byte);
577 /* Emit a statement program opcode into the current segment. */
579 static inline void
580 out_opcode (opc)
581 int opc;
583 out_byte (opc);
586 /* Emit a two-byte word into the current segment. */
588 static inline void
589 out_two (data)
590 int data;
592 md_number_to_chars (frag_more (2), data, 2);
595 /* Emit a four byte word into the current segment. */
597 static inline void
598 out_four (data)
599 int data;
601 md_number_to_chars (frag_more (4), data, 4);
604 /* Emit an unsigned "little-endian base 128" number. */
606 static void
607 out_uleb128 (value)
608 addressT value;
610 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
613 /* Emit a tuple for .debug_abbrev. */
615 static inline void
616 out_abbrev (name, form)
617 int name, form;
619 out_uleb128 (name);
620 out_uleb128 (form);
623 /* Create a new fake symbol whose value is the current position. */
625 static symbolS *
626 symbol_new_now ()
628 return symbol_new (fake_label_name, now_seg, frag_now_fix (), frag_now);
631 /* Set the value of SYM to the current position in the current segment. */
633 static void
634 set_symbol_value_now (sym)
635 symbolS *sym;
637 S_SET_SEGMENT (sym, now_seg);
638 S_SET_VALUE (sym, frag_now_fix ());
639 symbol_set_frag (sym, frag_now);
642 /* Get the size of a fragment. */
644 static offsetT
645 get_frag_fix (frag)
646 fragS *frag;
648 frchainS *fr;
650 if (frag->fr_next)
651 return frag->fr_fix;
653 /* If a fragment is the last in the chain, special measures must be
654 taken to find its size before relaxation, since it may be pending
655 on some subsegment chain. */
656 for (fr = frchain_root; fr; fr = fr->frch_next)
657 if (fr->frch_last == frag)
659 long align_mask = -1 << get_recorded_alignment (fr->frch_seg);
660 return (((char *) obstack_next_free (&fr->frch_obstack)
661 - frag->fr_literal) + ~align_mask) & align_mask;
664 abort ();
667 /* Set an absolute address (may result in a relocation entry). */
669 static void
670 out_set_addr (seg, frag, ofs)
671 segT seg;
672 fragS *frag;
673 addressT ofs;
675 expressionS expr;
676 symbolS *sym;
678 sym = symbol_new (fake_label_name, seg, ofs, frag);
680 out_opcode (DW_LNS_extended_op);
681 out_uleb128 (sizeof_address + 1);
683 out_opcode (DW_LNE_set_address);
684 expr.X_op = O_symbol;
685 expr.X_add_symbol = sym;
686 expr.X_add_number = 0;
687 emit_expr (&expr, sizeof_address);
690 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
691 static void scale_addr_delta PARAMS ((addressT *));
693 static void
694 scale_addr_delta (addr_delta)
695 addressT *addr_delta;
697 static int printed_this = 0;
698 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
700 if (!printed_this)
701 as_bad("unaligned opcodes detected in executable segment");
702 printed_this = 1;
704 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
706 #else
707 #define scale_addr_delta(A)
708 #endif
710 /* Encode a pair of line and address skips as efficiently as possible.
711 Note that the line skip is signed, whereas the address skip is unsigned.
713 The following two routines *must* be kept in sync. This is
714 enforced by making emit_inc_line_addr abort if we do not emit
715 exactly the expected number of bytes. */
717 static int
718 size_inc_line_addr (line_delta, addr_delta)
719 int line_delta;
720 addressT addr_delta;
722 unsigned int tmp, opcode;
723 int len = 0;
725 /* Scale the address delta by the minimum instruction length. */
726 scale_addr_delta (&addr_delta);
728 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
729 We cannot use special opcodes here, since we want the end_sequence
730 to emit the matrix entry. */
731 if (line_delta == INT_MAX)
733 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
734 len = 1;
735 else
736 len = 1 + sizeof_leb128 (addr_delta, 0);
737 return len + 3;
740 /* Bias the line delta by the base. */
741 tmp = line_delta - DWARF2_LINE_BASE;
743 /* If the line increment is out of range of a special opcode, we
744 must encode it with DW_LNS_advance_line. */
745 if (tmp >= DWARF2_LINE_RANGE)
747 len = 1 + sizeof_leb128 (line_delta, 1);
748 line_delta = 0;
749 tmp = 0 - DWARF2_LINE_BASE;
752 /* Bias the opcode by the special opcode base. */
753 tmp += DWARF2_LINE_OPCODE_BASE;
755 /* Avoid overflow when addr_delta is large. */
756 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
758 /* Try using a special opcode. */
759 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
760 if (opcode <= 255)
761 return len + 1;
763 /* Try using DW_LNS_const_add_pc followed by special op. */
764 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
765 if (opcode <= 255)
766 return len + 2;
769 /* Otherwise use DW_LNS_advance_pc. */
770 len += 1 + sizeof_leb128 (addr_delta, 0);
772 /* DW_LNS_copy or special opcode. */
773 len += 1;
775 return len;
778 static void
779 emit_inc_line_addr (line_delta, addr_delta, p, len)
780 int line_delta;
781 addressT addr_delta;
782 char *p;
783 int len;
785 unsigned int tmp, opcode;
786 int need_copy = 0;
787 char *end = p + len;
789 /* Scale the address delta by the minimum instruction length. */
790 scale_addr_delta (&addr_delta);
792 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
793 We cannot use special opcodes here, since we want the end_sequence
794 to emit the matrix entry. */
795 if (line_delta == INT_MAX)
797 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
798 *p++ = DW_LNS_const_add_pc;
799 else
801 *p++ = DW_LNS_advance_pc;
802 p += output_leb128 (p, addr_delta, 0);
805 *p++ = DW_LNS_extended_op;
806 *p++ = 1;
807 *p++ = DW_LNE_end_sequence;
808 goto done;
811 /* Bias the line delta by the base. */
812 tmp = line_delta - DWARF2_LINE_BASE;
814 /* If the line increment is out of range of a special opcode, we
815 must encode it with DW_LNS_advance_line. */
816 if (tmp >= DWARF2_LINE_RANGE)
818 *p++ = DW_LNS_advance_line;
819 p += output_leb128 (p, line_delta, 1);
821 /* Prettier, I think, to use DW_LNS_copy instead of a
822 "line +0, addr +0" special opcode. */
823 if (addr_delta == 0)
825 *p++ = DW_LNS_copy;
826 goto done;
829 line_delta = 0;
830 tmp = 0 - DWARF2_LINE_BASE;
831 need_copy = 1;
834 /* Bias the opcode by the special opcode base. */
835 tmp += DWARF2_LINE_OPCODE_BASE;
837 /* Avoid overflow when addr_delta is large. */
838 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
840 /* Try using a special opcode. */
841 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
842 if (opcode <= 255)
844 *p++ = opcode;
845 goto done;
848 /* Try using DW_LNS_const_add_pc followed by special op. */
849 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
850 if (opcode <= 255)
852 *p++ = DW_LNS_const_add_pc;
853 *p++ = opcode;
854 goto done;
858 /* Otherwise use DW_LNS_advance_pc. */
859 *p++ = DW_LNS_advance_pc;
860 p += output_leb128 (p, addr_delta, 0);
862 if (need_copy)
863 *p++ = DW_LNS_copy;
864 else
865 *p++ = tmp;
867 done:
868 assert (p == end);
871 /* Handy routine to combine calls to the above two routines. */
873 static void
874 out_inc_line_addr (line_delta, addr_delta)
875 int line_delta;
876 addressT addr_delta;
878 int len = size_inc_line_addr (line_delta, addr_delta);
879 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
882 /* Generate a variant frag that we can use to relax address/line
883 increments between fragments of the target segment. */
885 static void
886 relax_inc_line_addr (line_delta, seg, to_frag, to_ofs, from_frag, from_ofs)
887 int line_delta;
888 segT seg;
889 fragS *to_frag, *from_frag;
890 addressT to_ofs, from_ofs;
892 symbolS *to_sym, *from_sym;
893 expressionS expr;
894 int max_chars;
896 to_sym = symbol_new (fake_label_name, seg, to_ofs, to_frag);
897 from_sym = symbol_new (fake_label_name, seg, from_ofs, from_frag);
899 expr.X_op = O_subtract;
900 expr.X_add_symbol = to_sym;
901 expr.X_op_symbol = from_sym;
902 expr.X_add_number = 0;
904 /* The maximum size of the frag is the line delta with a maximum
905 sized address delta. */
906 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
908 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
909 make_expr_symbol (&expr), line_delta, NULL);
912 /* The function estimates the size of a rs_dwarf2dbg variant frag
913 based on the current values of the symbols. It is called before
914 the relaxation loop. We set fr_subtype to the expected length. */
917 dwarf2dbg_estimate_size_before_relax (frag)
918 fragS *frag;
920 offsetT addr_delta;
921 int size;
923 addr_delta = resolve_symbol_value (frag->fr_symbol);
924 size = size_inc_line_addr (frag->fr_offset, addr_delta);
926 frag->fr_subtype = size;
928 return size;
931 /* This function relaxes a rs_dwarf2dbg variant frag based on the
932 current values of the symbols. fr_subtype is the current length
933 of the frag. This returns the change in frag length. */
936 dwarf2dbg_relax_frag (frag)
937 fragS *frag;
939 int old_size, new_size;
941 old_size = frag->fr_subtype;
942 new_size = dwarf2dbg_estimate_size_before_relax (frag);
944 return new_size - old_size;
947 /* This function converts a rs_dwarf2dbg variant frag into a normal
948 fill frag. This is called after all relaxation has been done.
949 fr_subtype will be the desired length of the frag. */
951 void
952 dwarf2dbg_convert_frag (frag)
953 fragS *frag;
955 offsetT addr_diff;
957 addr_diff = resolve_symbol_value (frag->fr_symbol);
959 /* fr_var carries the max_chars that we created the fragment with.
960 fr_subtype carries the current expected length. We must, of
961 course, have allocated enough memory earlier. */
962 assert (frag->fr_var >= (int) frag->fr_subtype);
964 emit_inc_line_addr (frag->fr_offset, addr_diff,
965 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
967 frag->fr_fix += frag->fr_subtype;
968 frag->fr_type = rs_fill;
969 frag->fr_var = 0;
970 frag->fr_offset = 0;
973 /* Generate .debug_line content for the chain of line number entries
974 beginning at E, for segment SEG. */
976 static void
977 process_entries (seg, e)
978 segT seg;
979 struct line_entry *e;
981 unsigned filenum = 1;
982 unsigned line = 1;
983 unsigned column = 0;
984 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_BEGIN_STMT : 0;
985 fragS *frag = NULL;
986 fragS *last_frag;
987 addressT frag_ofs = 0;
988 addressT last_frag_ofs;
989 struct line_entry *next;
991 while (e)
993 int changed = 0;
995 if (filenum != e->loc.filenum)
997 filenum = e->loc.filenum;
998 out_opcode (DW_LNS_set_file);
999 out_uleb128 (filenum);
1000 changed = 1;
1003 if (column != e->loc.column)
1005 column = e->loc.column;
1006 out_opcode (DW_LNS_set_column);
1007 out_uleb128 (column);
1008 changed = 1;
1011 if ((e->loc.flags ^ flags) & DWARF2_FLAG_BEGIN_STMT)
1013 flags = e->loc.flags;
1014 out_opcode (DW_LNS_negate_stmt);
1015 changed = 1;
1018 if (e->loc.flags & DWARF2_FLAG_BEGIN_BLOCK)
1020 out_opcode (DW_LNS_set_basic_block);
1021 changed = 1;
1024 /* Don't try to optimize away redundant entries; gdb wants two
1025 entries for a function where the code starts on the same line as
1026 the {, and there's no way to identify that case here. Trust gcc
1027 to optimize appropriately. */
1028 if (1 /* line != e->loc.line || changed */)
1030 int line_delta = e->loc.line - line;
1031 if (frag == NULL)
1033 out_set_addr (seg, e->frag, e->frag_ofs);
1034 out_inc_line_addr (line_delta, 0);
1036 else if (frag == e->frag)
1037 out_inc_line_addr (line_delta, e->frag_ofs - frag_ofs);
1038 else
1039 relax_inc_line_addr (line_delta, seg, e->frag, e->frag_ofs,
1040 frag, frag_ofs);
1042 frag = e->frag;
1043 frag_ofs = e->frag_ofs;
1044 line = e->loc.line;
1046 else if (frag == NULL)
1048 out_set_addr (seg, e->frag, e->frag_ofs);
1049 frag = e->frag;
1050 frag_ofs = e->frag_ofs;
1053 next = e->next;
1054 free (e);
1055 e = next;
1058 /* Emit a DW_LNE_end_sequence for the end of the section. */
1059 last_frag = last_frag_for_seg (seg);
1060 last_frag_ofs = get_frag_fix (last_frag);
1061 if (frag == last_frag)
1062 out_inc_line_addr (INT_MAX, last_frag_ofs - frag_ofs);
1063 else
1064 relax_inc_line_addr (INT_MAX, seg, last_frag, last_frag_ofs,
1065 frag, frag_ofs);
1068 /* Emit the directory and file tables for .debug_line. */
1070 static void
1071 out_file_list ()
1073 size_t size;
1074 char *cp;
1075 unsigned int i;
1077 /* Emit directory list. */
1078 for (i = 1; i < dirs_in_use; ++i)
1080 size = strlen (dirs[i]) + 1;
1081 cp = frag_more (size);
1082 memcpy (cp, dirs[i], size);
1084 /* Terminate it. */
1085 out_byte ('\0');
1087 for (i = 1; i < files_in_use; ++i)
1089 if (files[i].filename == NULL)
1091 as_bad (_("unassigned file number %ld"), (long) i);
1092 continue;
1095 size = strlen (files[i].filename) + 1;
1096 cp = frag_more (size);
1097 memcpy (cp, files[i].filename, size);
1099 out_uleb128 (files[i].dir); /* directory number */
1100 out_uleb128 (0); /* last modification timestamp */
1101 out_uleb128 (0); /* filesize */
1104 /* Terminate filename list. */
1105 out_byte (0);
1108 /* Emit the collected .debug_line data. */
1110 static void
1111 out_debug_line (line_seg)
1112 segT line_seg;
1114 expressionS expr;
1115 symbolS *line_start;
1116 symbolS *prologue_end;
1117 symbolS *line_end;
1118 struct line_seg *s;
1119 enum dwarf2_format d2f;
1120 int sizeof_offset;
1122 subseg_set (line_seg, 0);
1124 line_start = symbol_new_now ();
1125 prologue_end = symbol_make (fake_label_name);
1126 line_end = symbol_make (fake_label_name);
1128 /* Total length of the information for this compilation unit. */
1129 expr.X_op = O_subtract;
1130 expr.X_add_symbol = line_end;
1131 expr.X_op_symbol = line_start;
1133 d2f = DWARF2_FORMAT ();
1134 if (d2f == dwarf2_format_32bit)
1136 expr.X_add_number = -4;
1137 emit_expr (&expr, 4);
1138 sizeof_offset = 4;
1140 else if (d2f == dwarf2_format_64bit)
1142 expr.X_add_number = -12;
1143 out_four (-1);
1144 emit_expr (&expr, 8);
1145 sizeof_offset = 8;
1147 else if (d2f == dwarf2_format_64bit_irix)
1149 expr.X_add_number = -8;
1150 emit_expr (&expr, 8);
1151 sizeof_offset = 8;
1153 else
1155 as_fatal (_("internal error: unknown dwarf2 format"));
1158 /* Version. */
1159 out_two (2);
1161 /* Length of the prologue following this length. */
1162 expr.X_op = O_subtract;
1163 expr.X_add_symbol = prologue_end;
1164 expr.X_op_symbol = line_start;
1165 expr.X_add_number = - (4 + 2 + 4);
1166 emit_expr (&expr, sizeof_offset);
1168 /* Parameters of the state machine. */
1169 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1170 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1171 out_byte (DWARF2_LINE_BASE);
1172 out_byte (DWARF2_LINE_RANGE);
1173 out_byte (DWARF2_LINE_OPCODE_BASE);
1175 /* Standard opcode lengths. */
1176 out_byte (0); /* DW_LNS_copy */
1177 out_byte (1); /* DW_LNS_advance_pc */
1178 out_byte (1); /* DW_LNS_advance_line */
1179 out_byte (1); /* DW_LNS_set_file */
1180 out_byte (1); /* DW_LNS_set_column */
1181 out_byte (0); /* DW_LNS_negate_stmt */
1182 out_byte (0); /* DW_LNS_set_basic_block */
1183 out_byte (0); /* DW_LNS_const_add_pc */
1184 out_byte (1); /* DW_LNS_fixed_advance_pc */
1186 out_file_list ();
1188 set_symbol_value_now (prologue_end);
1190 /* For each section, emit a statement program. */
1191 for (s = all_segs; s; s = s->next)
1192 process_entries (s->seg, s->head->head);
1194 set_symbol_value_now (line_end);
1197 /* Emit data for .debug_aranges. */
1199 static void
1200 out_debug_aranges (aranges_seg, info_seg)
1201 segT aranges_seg;
1202 segT info_seg;
1204 unsigned int addr_size = sizeof_address;
1205 addressT size, skip;
1206 struct line_seg *s;
1207 expressionS expr;
1208 char *p;
1210 size = 4 + 2 + 4 + 1 + 1;
1212 skip = 2 * addr_size - (size & (2 * addr_size - 1));
1213 if (skip == 2 * addr_size)
1214 skip = 0;
1215 size += skip;
1217 for (s = all_segs; s; s = s->next)
1218 size += 2 * addr_size;
1220 size += 2 * addr_size;
1222 subseg_set (aranges_seg, 0);
1224 /* Length of the compilation unit. */
1225 out_four (size - 4);
1227 /* Version. */
1228 out_two (2);
1230 /* Offset to .debug_info. */
1231 /* ??? sizeof_offset */
1232 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), 4);
1234 /* Size of an address (offset portion). */
1235 out_byte (addr_size);
1237 /* Size of a segment descriptor. */
1238 out_byte (0);
1240 /* Align the header. */
1241 if (skip)
1242 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1244 for (s = all_segs; s; s = s->next)
1246 fragS *frag;
1247 symbolS *beg, *end;
1249 frag = first_frag_for_seg (s->seg);
1250 beg = symbol_new (fake_label_name, s->seg, 0, frag);
1251 s->text_start = beg;
1253 frag = last_frag_for_seg (s->seg);
1254 end = symbol_new (fake_label_name, s->seg, get_frag_fix (frag), frag);
1255 s->text_end = end;
1257 expr.X_op = O_symbol;
1258 expr.X_add_symbol = beg;
1259 expr.X_add_number = 0;
1260 emit_expr (&expr, addr_size);
1262 expr.X_op = O_subtract;
1263 expr.X_add_symbol = end;
1264 expr.X_op_symbol = beg;
1265 expr.X_add_number = 0;
1266 emit_expr (&expr, addr_size);
1269 p = frag_more (2 * addr_size);
1270 md_number_to_chars (p, 0, addr_size);
1271 md_number_to_chars (p + addr_size, 0, addr_size);
1274 /* Emit data for .debug_abbrev. Note that this must be kept in
1275 sync with out_debug_info below. */
1277 static void
1278 out_debug_abbrev (abbrev_seg)
1279 segT abbrev_seg;
1281 subseg_set (abbrev_seg, 0);
1283 out_uleb128 (1);
1284 out_uleb128 (DW_TAG_compile_unit);
1285 out_byte (DW_CHILDREN_no);
1286 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1287 if (all_segs->next == NULL)
1289 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1290 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1292 out_abbrev (DW_AT_name, DW_FORM_string);
1293 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1294 out_abbrev (DW_AT_producer, DW_FORM_string);
1295 out_abbrev (DW_AT_language, DW_FORM_data2);
1296 out_abbrev (0, 0);
1298 /* Terminate the abbreviations for this compilation unit. */
1299 out_byte (0);
1302 /* Emit a description of this compilation unit for .debug_info. */
1304 static void
1305 out_debug_info (info_seg, abbrev_seg, line_seg)
1306 segT info_seg;
1307 segT abbrev_seg;
1308 segT line_seg;
1310 char producer[128];
1311 char *comp_dir;
1312 expressionS expr;
1313 symbolS *info_start;
1314 symbolS *info_end;
1315 char *p;
1316 int len;
1317 enum dwarf2_format d2f;
1318 int sizeof_offset;
1320 subseg_set (info_seg, 0);
1322 info_start = symbol_new_now ();
1323 info_end = symbol_make (fake_label_name);
1325 /* Compilation Unit length. */
1326 expr.X_op = O_subtract;
1327 expr.X_add_symbol = info_end;
1328 expr.X_op_symbol = info_start;
1330 d2f = DWARF2_FORMAT ();
1331 if (d2f == dwarf2_format_32bit)
1333 expr.X_add_number = -4;
1334 emit_expr (&expr, 4);
1335 sizeof_offset = 4;
1337 else if (d2f == dwarf2_format_64bit)
1339 expr.X_add_number = -12;
1340 out_four (-1);
1341 emit_expr (&expr, 8);
1342 sizeof_offset = 8;
1344 else if (d2f == dwarf2_format_64bit_irix)
1346 expr.X_add_number = -8;
1347 emit_expr (&expr, 8);
1348 sizeof_offset = 8;
1350 else
1352 as_fatal (_("internal error: unknown dwarf2 format"));
1355 /* DWARF version. */
1356 out_two (2);
1358 /* .debug_abbrev offset */
1359 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1361 /* Target address size. */
1362 out_byte (sizeof_address);
1364 /* DW_TAG_compile_unit DIE abbrev */
1365 out_uleb128 (1);
1367 /* DW_AT_stmt_list */
1368 /* ??? sizeof_offset */
1369 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 4);
1371 /* These two attributes may only be emitted if all of the code is
1372 contiguous. Multiple sections are not that. */
1373 if (all_segs->next == NULL)
1375 /* DW_AT_low_pc */
1376 expr.X_op = O_symbol;
1377 expr.X_add_symbol = all_segs->text_start;
1378 expr.X_add_number = 0;
1379 emit_expr (&expr, sizeof_address);
1381 /* DW_AT_high_pc */
1382 expr.X_op = O_symbol;
1383 expr.X_add_symbol = all_segs->text_end;
1384 expr.X_add_number = 0;
1385 emit_expr (&expr, sizeof_address);
1388 /* DW_AT_name. We don't have the actual file name that was present
1389 on the command line, so assume files[1] is the main input file.
1390 We're not supposed to get called unless at least one line number
1391 entry was emitted, so this should always be defined. */
1392 if (!files || files_in_use < 1)
1393 abort ();
1394 if (files[1].dir)
1396 len = strlen (dirs[files[1].dir]);
1397 p = frag_more (len + 1);
1398 memcpy (p, dirs[files[1].dir], len);
1399 p[len] = '/';
1401 len = strlen (files[1].filename) + 1;
1402 p = frag_more (len);
1403 memcpy (p, files[1].filename, len);
1405 /* DW_AT_comp_dir */
1406 comp_dir = getpwd ();
1407 len = strlen (comp_dir) + 1;
1408 p = frag_more (len);
1409 memcpy (p, comp_dir, len);
1411 /* DW_AT_producer */
1412 sprintf (producer, "GNU AS %s", VERSION);
1413 len = strlen (producer) + 1;
1414 p = frag_more (len);
1415 memcpy (p, producer, len);
1417 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1418 dwarf2 draft has no standard code for assembler. */
1419 out_two (DW_LANG_Mips_Assembler);
1421 set_symbol_value_now (info_end);
1424 void
1425 dwarf2_finish ()
1427 segT line_seg;
1428 struct line_seg *s;
1430 /* We don't need to do anything unless:
1431 - Some debug information was recorded via .file/.loc
1432 - or, we are generating DWARF2 information ourself (--gdwarf2)
1433 - or, there is a user-provided .debug_info section which could
1434 reference the file table in the .debug_line section we generate
1435 below. */
1436 if (all_segs == NULL
1437 && debug_type != DEBUG_DWARF2
1438 && bfd_get_section_by_name (stdoutput, ".debug_info") == NULL)
1439 return;
1441 /* Calculate the size of an address for the target machine. */
1442 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1444 /* Create and switch to the line number section. */
1445 line_seg = subseg_new (".debug_line", 0);
1446 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY);
1448 /* For each subsection, chain the debug entries together. */
1449 for (s = all_segs; s; s = s->next)
1451 struct line_subseg *ss = s->head;
1452 struct line_entry **ptail = ss->ptail;
1454 while ((ss = ss->next) != NULL)
1456 *ptail = ss->head;
1457 ptail = ss->ptail;
1461 out_debug_line (line_seg);
1463 /* If this is assembler generated line info, we need .debug_info
1464 and .debug_abbrev sections as well. */
1465 if (all_segs != NULL && debug_type == DEBUG_DWARF2)
1467 segT abbrev_seg;
1468 segT info_seg;
1469 segT aranges_seg;
1471 info_seg = subseg_new (".debug_info", 0);
1472 abbrev_seg = subseg_new (".debug_abbrev", 0);
1473 aranges_seg = subseg_new (".debug_aranges", 0);
1475 bfd_set_section_flags (stdoutput, info_seg, SEC_READONLY);
1476 bfd_set_section_flags (stdoutput, abbrev_seg, SEC_READONLY);
1477 bfd_set_section_flags (stdoutput, aranges_seg, SEC_READONLY);
1479 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1481 out_debug_aranges (aranges_seg, info_seg);
1482 out_debug_abbrev (abbrev_seg);
1483 out_debug_info (info_seg, abbrev_seg, line_seg);
1487 #else
1488 void
1489 dwarf2_finish ()
1494 dwarf2dbg_estimate_size_before_relax (frag)
1495 fragS *frag ATTRIBUTE_UNUSED;
1497 as_fatal (_("dwarf2 is not supported for this object file format"));
1498 return 0;
1502 dwarf2dbg_relax_frag (frag)
1503 fragS *frag ATTRIBUTE_UNUSED;
1505 as_fatal (_("dwarf2 is not supported for this object file format"));
1506 return 0;
1509 void
1510 dwarf2dbg_convert_frag (frag)
1511 fragS *frag ATTRIBUTE_UNUSED;
1513 as_fatal (_("dwarf2 is not supported for this object file format"));
1516 void
1517 dwarf2_emit_insn (size)
1518 int size ATTRIBUTE_UNUSED;
1522 char *
1523 dwarf2_directive_file (dummy)
1524 int dummy ATTRIBUTE_UNUSED;
1526 s_app_file (0);
1527 return NULL;
1530 void
1531 dwarf2_directive_loc (dummy)
1532 int dummy ATTRIBUTE_UNUSED;
1534 as_fatal (_("dwarf2 is not supported for this object file format"));
1536 #endif /* BFD_ASSEMBLER */