* dwarf2dbg.c (out_debug_info): Add new parameter ranges_seg and emit
[deliverable/binutils-gdb.git] / gas / dwarf2dbg.c
1 /* dwarf2dbg.c - DWARF2 debug support
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
4 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5
6 This file is part of GAS, the GNU Assembler.
7
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 2, or (at your option)
11 any later version.
12
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.
17
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. */
22
23 /* Logical line numbers can be controlled by the compiler via the
24 following directives:
25
26 .file FILENO "file.c"
27 .loc FILENO LINENO [COLUMN] [basic_block] [prologue_end] \
28 [epilogue_begin] [is_stmt VALUE] [isa VALUE]
29 */
30
31 #include "as.h"
32 #include "safe-ctype.h"
33
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
44
45 #include "dwarf2dbg.h"
46 #include <filenames.h>
47
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
70
71 #ifndef DWARF2_FORMAT
72 # define DWARF2_FORMAT() dwarf2_format_32bit
73 #endif
74
75 #ifndef DWARF2_ADDR_SIZE
76 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
77 #endif
78
79 #include "subsegs.h"
80
81 #include "elf/dwarf2.h"
82
83 /* Since we can't generate the prolog until the body is complete, we
84 use three different subsegments for .debug_line: one holding the
85 prolog, one for the directory and filename info, and one for the
86 body ("statement program"). */
87 #define DL_PROLOG 0
88 #define DL_FILES 1
89 #define DL_BODY 2
90
91 /* First special line opcde - leave room for the standard opcodes.
92 Note: If you want to change this, you'll have to update the
93 "standard_opcode_lengths" table that is emitted below in
94 out_debug_line(). */
95 #define DWARF2_LINE_OPCODE_BASE 13
96
97 #ifndef DWARF2_LINE_BASE
98 /* Minimum line offset in a special line info. opcode. This value
99 was chosen to give a reasonable range of values. */
100 # define DWARF2_LINE_BASE -5
101 #endif
102
103 /* Range of line offsets in a special line info. opcode. */
104 #ifndef DWARF2_LINE_RANGE
105 # define DWARF2_LINE_RANGE 14
106 #endif
107
108 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
109 /* Define the architecture-dependent minimum instruction length (in
110 bytes). This value should be rather too small than too big. */
111 # define DWARF2_LINE_MIN_INSN_LENGTH 1
112 #endif
113
114 /* Flag that indicates the initial value of the is_stmt_start flag. */
115 #define DWARF2_LINE_DEFAULT_IS_STMT 1
116
117 /* Given a special op, return the line skip amount. */
118 #define SPECIAL_LINE(op) \
119 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
120
121 /* Given a special op, return the address skip amount (in units of
122 DWARF2_LINE_MIN_INSN_LENGTH. */
123 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
124
125 /* The maximum address skip amount that can be encoded with a special op. */
126 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
127
128 struct line_entry {
129 struct line_entry *next;
130 symbolS *label;
131 struct dwarf2_line_info loc;
132 };
133
134 struct line_subseg {
135 struct line_subseg *next;
136 subsegT subseg;
137 struct line_entry *head;
138 struct line_entry **ptail;
139 };
140
141 struct line_seg {
142 struct line_seg *next;
143 segT seg;
144 struct line_subseg *head;
145 symbolS *text_start;
146 symbolS *text_end;
147 };
148
149 /* Collects data for all line table entries during assembly. */
150 static struct line_seg *all_segs;
151
152 struct file_entry {
153 const char *filename;
154 unsigned int dir;
155 };
156
157 /* Table of files used by .debug_line. */
158 static struct file_entry *files;
159 static unsigned int files_in_use;
160 static unsigned int files_allocated;
161
162 /* Table of directories used by .debug_line. */
163 static char **dirs;
164 static unsigned int dirs_in_use;
165 static unsigned int dirs_allocated;
166
167 /* TRUE when we've seen a .loc directive recently. Used to avoid
168 doing work when there's nothing to do. */
169 static bfd_boolean loc_directive_seen;
170
171 /* TRUE when we're supposed to set the basic block mark whenever a
172 label is seen. */
173 bfd_boolean dwarf2_loc_mark_labels;
174
175 /* Current location as indicated by the most recent .loc directive. */
176 static struct dwarf2_line_info current = {
177 1, 1, 0, 0,
178 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0
179 };
180
181 /* The size of an address on the target. */
182 static unsigned int sizeof_address;
183 \f
184 static struct line_subseg *get_line_subseg (segT, subsegT);
185 static unsigned int get_filenum (const char *, unsigned int);
186 static struct frag *first_frag_for_seg (segT);
187 static struct frag *last_frag_for_seg (segT);
188 static void out_byte (int);
189 static void out_opcode (int);
190 static void out_two (int);
191 static void out_four (int);
192 static void out_abbrev (int, int);
193 static void out_uleb128 (addressT);
194 static offsetT get_frag_fix (fragS *, segT);
195 static void out_set_addr (symbolS *);
196 static int size_inc_line_addr (int, addressT);
197 static void emit_inc_line_addr (int, addressT, char *, int);
198 static void out_inc_line_addr (int, addressT);
199 static void relax_inc_line_addr (int, symbolS *, symbolS *);
200 static void process_entries (segT, struct line_entry *);
201 static void out_file_list (void);
202 static void out_debug_line (segT);
203 static void out_debug_aranges (segT, segT);
204 static void out_debug_abbrev (segT);
205 \f
206 #ifndef TC_DWARF2_EMIT_OFFSET
207 #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
208
209 /* Create an offset to .dwarf2_*. */
210
211 static void
212 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
213 {
214 expressionS expr;
215
216 expr.X_op = O_symbol;
217 expr.X_add_symbol = symbol;
218 expr.X_add_number = 0;
219 emit_expr (&expr, size);
220 }
221 #endif
222
223 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
224
225 static struct line_subseg *
226 get_line_subseg (segT seg, subsegT subseg)
227 {
228 static segT last_seg;
229 static subsegT last_subseg;
230 static struct line_subseg *last_line_subseg;
231
232 struct line_seg **ps, *s;
233 struct line_subseg **pss, *ss;
234
235 if (seg == last_seg && subseg == last_subseg)
236 return last_line_subseg;
237
238 for (ps = &all_segs; (s = *ps) != NULL; ps = &s->next)
239 if (s->seg == seg)
240 goto found_seg;
241
242 s = (struct line_seg *) xmalloc (sizeof (*s));
243 s->next = NULL;
244 s->seg = seg;
245 s->head = NULL;
246 *ps = s;
247
248 found_seg:
249 for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
250 {
251 if (ss->subseg == subseg)
252 goto found_subseg;
253 if (ss->subseg > subseg)
254 break;
255 }
256
257 ss = (struct line_subseg *) xmalloc (sizeof (*ss));
258 ss->next = *pss;
259 ss->subseg = subseg;
260 ss->head = NULL;
261 ss->ptail = &ss->head;
262 *pss = ss;
263
264 found_subseg:
265 last_seg = seg;
266 last_subseg = subseg;
267 last_line_subseg = ss;
268
269 return ss;
270 }
271
272 /* Record an entry for LOC occurring at LABEL. */
273
274 static void
275 dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
276 {
277 struct line_subseg *ss;
278 struct line_entry *e;
279
280 e = (struct line_entry *) xmalloc (sizeof (*e));
281 e->next = NULL;
282 e->label = label;
283 e->loc = *loc;
284
285 ss = get_line_subseg (now_seg, now_subseg);
286 *ss->ptail = e;
287 ss->ptail = &e->next;
288 }
289
290 /* Record an entry for LOC occurring at OFS within the current fragment. */
291
292 void
293 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
294 {
295 static unsigned int line = -1;
296 static unsigned int filenum = -1;
297
298 symbolS *sym;
299
300 /* Early out for as-yet incomplete location information. */
301 if (loc->filenum == 0 || loc->line == 0)
302 return;
303
304 /* Don't emit sequences of line symbols for the same line when the
305 symbols apply to assembler code. It is necessary to emit
306 duplicate line symbols when a compiler asks for them, because GDB
307 uses them to determine the end of the prologue. */
308 if (debug_type == DEBUG_DWARF2
309 && line == loc->line && filenum == loc->filenum)
310 return;
311
312 line = loc->line;
313 filenum = loc->filenum;
314
315 sym = symbol_temp_new (now_seg, ofs, frag_now);
316 dwarf2_gen_line_info_1 (sym, loc);
317 }
318
319 /* Returns the current source information. If .file directives have
320 been encountered, the info for the corresponding source file is
321 returned. Otherwise, the info for the assembly source file is
322 returned. */
323
324 void
325 dwarf2_where (struct dwarf2_line_info *line)
326 {
327 if (debug_type == DEBUG_DWARF2)
328 {
329 char *filename;
330 as_where (&filename, &line->line);
331 line->filenum = get_filenum (filename, 0);
332 line->column = 0;
333 line->flags = DWARF2_FLAG_IS_STMT;
334 line->isa = current.isa;
335 }
336 else
337 *line = current;
338 }
339
340 /* A hook to allow the target backend to inform the line number state
341 machine of isa changes when assembler debug info is enabled. */
342
343 void
344 dwarf2_set_isa (unsigned int isa)
345 {
346 current.isa = isa;
347 }
348
349 /* Called for each machine instruction, or relatively atomic group of
350 machine instructions (ie built-in macro). The instruction or group
351 is SIZE bytes in length. If dwarf2 line number generation is called
352 for, emit a line statement appropriately. */
353
354 void
355 dwarf2_emit_insn (int size)
356 {
357 struct dwarf2_line_info loc;
358
359 if (loc_directive_seen)
360 {
361 /* Use the last location established by a .loc directive, not
362 the value returned by dwarf2_where(). That calls as_where()
363 which will return either the logical input file name (foo.c)
364 or the physical input file name (foo.s) and not the file name
365 specified in the most recent .loc directive (eg foo.h). */
366 loc = current;
367
368 /* Unless we generate DWARF2 debugging information for each
369 assembler line, we only emit one line symbol for one LOC. */
370 if (debug_type != DEBUG_DWARF2)
371 loc_directive_seen = FALSE;
372 }
373 else if (debug_type != DEBUG_DWARF2)
374 return;
375 else
376 dwarf2_where (&loc);
377
378 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
379
380 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
381 | DWARF2_FLAG_PROLOGUE_END
382 | DWARF2_FLAG_EPILOGUE_BEGIN);
383 }
384
385 /* Called for each (preferably code) label. If dwarf2_loc_mark_labels
386 is enabled, emit a basic block marker. */
387
388 void
389 dwarf2_emit_label (symbolS *label)
390 {
391 struct dwarf2_line_info loc;
392
393 if (!dwarf2_loc_mark_labels)
394 return;
395 if (S_GET_SEGMENT (label) != now_seg)
396 return;
397 if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE))
398 return;
399
400 if (debug_type == DEBUG_DWARF2)
401 dwarf2_where (&loc);
402 else
403 {
404 loc = current;
405 loc_directive_seen = FALSE;
406 }
407
408 loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
409
410 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
411 | DWARF2_FLAG_PROLOGUE_END
412 | DWARF2_FLAG_EPILOGUE_BEGIN);
413
414 dwarf2_gen_line_info_1 (label, &loc);
415 }
416
417 /* Get a .debug_line file number for FILENAME. If NUM is nonzero,
418 allocate it on that file table slot, otherwise return the first
419 empty one. */
420
421 static unsigned int
422 get_filenum (const char *filename, unsigned int num)
423 {
424 static unsigned int last_used, last_used_dir_len;
425 const char *file;
426 size_t dir_len;
427 unsigned int i, dir;
428
429 if (num == 0 && last_used)
430 {
431 if (! files[last_used].dir
432 && strcmp (filename, files[last_used].filename) == 0)
433 return last_used;
434 if (files[last_used].dir
435 && strncmp (filename, dirs[files[last_used].dir],
436 last_used_dir_len) == 0
437 && IS_DIR_SEPARATOR (filename [last_used_dir_len])
438 && strcmp (filename + last_used_dir_len + 1,
439 files[last_used].filename) == 0)
440 return last_used;
441 }
442
443 file = lbasename (filename);
444 /* Don't make empty string from / or A: from A:/ . */
445 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
446 if (file <= filename + 3)
447 file = filename;
448 #else
449 if (file == filename + 1)
450 file = filename;
451 #endif
452 dir_len = file - filename;
453
454 dir = 0;
455 if (dir_len)
456 {
457 --dir_len;
458 for (dir = 1; dir < dirs_in_use; ++dir)
459 if (strncmp (filename, dirs[dir], dir_len) == 0
460 && dirs[dir][dir_len] == '\0')
461 break;
462
463 if (dir >= dirs_in_use)
464 {
465 if (dir >= dirs_allocated)
466 {
467 dirs_allocated = dir + 32;
468 dirs = (char **)
469 xrealloc (dirs, (dir + 32) * sizeof (const char *));
470 }
471
472 dirs[dir] = xmalloc (dir_len + 1);
473 memcpy (dirs[dir], filename, dir_len);
474 dirs[dir][dir_len] = '\0';
475 dirs_in_use = dir + 1;
476 }
477 }
478
479 if (num == 0)
480 {
481 for (i = 1; i < files_in_use; ++i)
482 if (files[i].dir == dir
483 && files[i].filename
484 && strcmp (file, files[i].filename) == 0)
485 {
486 last_used = i;
487 last_used_dir_len = dir_len;
488 return i;
489 }
490 }
491 else
492 i = num;
493
494 if (i >= files_allocated)
495 {
496 unsigned int old = files_allocated;
497
498 files_allocated = i + 32;
499 files = (struct file_entry *)
500 xrealloc (files, (i + 32) * sizeof (struct file_entry));
501
502 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
503 }
504
505 files[i].filename = num ? file : xstrdup (file);
506 files[i].dir = dir;
507 if (files_in_use < i + 1)
508 files_in_use = i + 1;
509 last_used = i;
510 last_used_dir_len = dir_len;
511
512 return i;
513 }
514
515 /* Handle two forms of .file directive:
516 - Pass .file "source.c" to s_app_file
517 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
518
519 If an entry is added to the file table, return a pointer to the filename. */
520
521 char *
522 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
523 {
524 offsetT num;
525 char *filename;
526 int filename_len;
527
528 /* Continue to accept a bare string and pass it off. */
529 SKIP_WHITESPACE ();
530 if (*input_line_pointer == '"')
531 {
532 s_app_file (0);
533 return NULL;
534 }
535
536 num = get_absolute_expression ();
537 filename = demand_copy_C_string (&filename_len);
538 if (filename == NULL)
539 return NULL;
540 demand_empty_rest_of_line ();
541
542 if (num < 1)
543 {
544 as_bad (_("file number less than one"));
545 return NULL;
546 }
547
548 if (num < (int) files_in_use && files[num].filename != 0)
549 {
550 as_bad (_("file number %ld already allocated"), (long) num);
551 return NULL;
552 }
553
554 get_filenum (filename, num);
555
556 return filename;
557 }
558
559 void
560 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
561 {
562 offsetT filenum, line;
563
564 filenum = get_absolute_expression ();
565 SKIP_WHITESPACE ();
566 line = get_absolute_expression ();
567
568 if (filenum < 1)
569 {
570 as_bad (_("file number less than one"));
571 return;
572 }
573 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
574 {
575 as_bad (_("unassigned file number %ld"), (long) filenum);
576 return;
577 }
578
579 current.filenum = filenum;
580 current.line = line;
581
582 #ifndef NO_LISTING
583 if (listing)
584 {
585 if (files[filenum].dir)
586 {
587 size_t dir_len = strlen (dirs[files[filenum].dir]);
588 size_t file_len = strlen (files[filenum].filename);
589 char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
590
591 memcpy (cp, dirs[files[filenum].dir], dir_len);
592 INSERT_DIR_SEPARATOR (cp, dir_len);
593 memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
594 cp[dir_len + file_len + 1] = '\0';
595 listing_source_file (cp);
596 }
597 else
598 listing_source_file (files[filenum].filename);
599 listing_source_line (line);
600 }
601 #endif
602
603 SKIP_WHITESPACE ();
604 if (ISDIGIT (*input_line_pointer))
605 {
606 current.column = get_absolute_expression ();
607 SKIP_WHITESPACE ();
608 }
609
610 while (ISALPHA (*input_line_pointer))
611 {
612 char *p, c;
613 offsetT value;
614
615 p = input_line_pointer;
616 c = get_symbol_end ();
617
618 if (strcmp (p, "basic_block") == 0)
619 {
620 current.flags |= DWARF2_FLAG_BASIC_BLOCK;
621 *input_line_pointer = c;
622 }
623 else if (strcmp (p, "prologue_end") == 0)
624 {
625 current.flags |= DWARF2_FLAG_PROLOGUE_END;
626 *input_line_pointer = c;
627 }
628 else if (strcmp (p, "epilogue_begin") == 0)
629 {
630 current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
631 *input_line_pointer = c;
632 }
633 else if (strcmp (p, "is_stmt") == 0)
634 {
635 *input_line_pointer = c;
636 value = get_absolute_expression ();
637 if (value == 0)
638 current.flags &= ~DWARF2_FLAG_IS_STMT;
639 else if (value == 1)
640 current.flags |= DWARF2_FLAG_IS_STMT;
641 else
642 {
643 as_bad (_("is_stmt value not 0 or 1"));
644 return;
645 }
646 }
647 else if (strcmp (p, "isa") == 0)
648 {
649 *input_line_pointer = c;
650 value = get_absolute_expression ();
651 if (value >= 0)
652 current.isa = value;
653 else
654 {
655 as_bad (_("isa number less than zero"));
656 return;
657 }
658 }
659 else
660 {
661 as_bad (_("unknown .loc sub-directive `%s'"), p);
662 *input_line_pointer = c;
663 return;
664 }
665
666 SKIP_WHITESPACE ();
667 }
668
669 demand_empty_rest_of_line ();
670 loc_directive_seen = TRUE;
671 }
672
673 void
674 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
675 {
676 offsetT value = get_absolute_expression ();
677
678 if (value != 0 && value != 1)
679 {
680 as_bad (_("expected 0 or 1"));
681 ignore_rest_of_line ();
682 }
683 else
684 {
685 dwarf2_loc_mark_labels = value != 0;
686 demand_empty_rest_of_line ();
687 }
688 }
689 \f
690 static struct frag *
691 first_frag_for_seg (segT seg)
692 {
693 return seg_info (seg)->frchainP->frch_root;
694 }
695
696 static struct frag *
697 last_frag_for_seg (segT seg)
698 {
699 frchainS *f = seg_info (seg)->frchainP;
700
701 while (f->frch_next != NULL)
702 f = f->frch_next;
703
704 return f->frch_last;
705 }
706 \f
707 /* Emit a single byte into the current segment. */
708
709 static inline void
710 out_byte (int byte)
711 {
712 FRAG_APPEND_1_CHAR (byte);
713 }
714
715 /* Emit a statement program opcode into the current segment. */
716
717 static inline void
718 out_opcode (int opc)
719 {
720 out_byte (opc);
721 }
722
723 /* Emit a two-byte word into the current segment. */
724
725 static inline void
726 out_two (int data)
727 {
728 md_number_to_chars (frag_more (2), data, 2);
729 }
730
731 /* Emit a four byte word into the current segment. */
732
733 static inline void
734 out_four (int data)
735 {
736 md_number_to_chars (frag_more (4), data, 4);
737 }
738
739 /* Emit an unsigned "little-endian base 128" number. */
740
741 static void
742 out_uleb128 (addressT value)
743 {
744 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
745 }
746
747 /* Emit a tuple for .debug_abbrev. */
748
749 static inline void
750 out_abbrev (int name, int form)
751 {
752 out_uleb128 (name);
753 out_uleb128 (form);
754 }
755
756 /* Get the size of a fragment. */
757
758 static offsetT
759 get_frag_fix (fragS *frag, segT seg)
760 {
761 frchainS *fr;
762
763 if (frag->fr_next)
764 return frag->fr_fix;
765
766 /* If a fragment is the last in the chain, special measures must be
767 taken to find its size before relaxation, since it may be pending
768 on some subsegment chain. */
769 for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
770 if (fr->frch_last == frag)
771 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
772
773 abort ();
774 }
775
776 /* Set an absolute address (may result in a relocation entry). */
777
778 static void
779 out_set_addr (symbolS *sym)
780 {
781 expressionS expr;
782
783 out_opcode (DW_LNS_extended_op);
784 out_uleb128 (sizeof_address + 1);
785
786 out_opcode (DW_LNE_set_address);
787 expr.X_op = O_symbol;
788 expr.X_add_symbol = sym;
789 expr.X_add_number = 0;
790 emit_expr (&expr, sizeof_address);
791 }
792
793 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
794 static void scale_addr_delta (addressT *);
795
796 static void
797 scale_addr_delta (addressT *addr_delta)
798 {
799 static int printed_this = 0;
800 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
801 {
802 if (!printed_this)
803 as_bad("unaligned opcodes detected in executable segment");
804 printed_this = 1;
805 }
806 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
807 }
808 #else
809 #define scale_addr_delta(A)
810 #endif
811
812 /* Encode a pair of line and address skips as efficiently as possible.
813 Note that the line skip is signed, whereas the address skip is unsigned.
814
815 The following two routines *must* be kept in sync. This is
816 enforced by making emit_inc_line_addr abort if we do not emit
817 exactly the expected number of bytes. */
818
819 static int
820 size_inc_line_addr (int line_delta, addressT addr_delta)
821 {
822 unsigned int tmp, opcode;
823 int len = 0;
824
825 /* Scale the address delta by the minimum instruction length. */
826 scale_addr_delta (&addr_delta);
827
828 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
829 We cannot use special opcodes here, since we want the end_sequence
830 to emit the matrix entry. */
831 if (line_delta == INT_MAX)
832 {
833 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
834 len = 1;
835 else
836 len = 1 + sizeof_leb128 (addr_delta, 0);
837 return len + 3;
838 }
839
840 /* Bias the line delta by the base. */
841 tmp = line_delta - DWARF2_LINE_BASE;
842
843 /* If the line increment is out of range of a special opcode, we
844 must encode it with DW_LNS_advance_line. */
845 if (tmp >= DWARF2_LINE_RANGE)
846 {
847 len = 1 + sizeof_leb128 (line_delta, 1);
848 line_delta = 0;
849 tmp = 0 - DWARF2_LINE_BASE;
850 }
851
852 /* Bias the opcode by the special opcode base. */
853 tmp += DWARF2_LINE_OPCODE_BASE;
854
855 /* Avoid overflow when addr_delta is large. */
856 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
857 {
858 /* Try using a special opcode. */
859 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
860 if (opcode <= 255)
861 return len + 1;
862
863 /* Try using DW_LNS_const_add_pc followed by special op. */
864 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
865 if (opcode <= 255)
866 return len + 2;
867 }
868
869 /* Otherwise use DW_LNS_advance_pc. */
870 len += 1 + sizeof_leb128 (addr_delta, 0);
871
872 /* DW_LNS_copy or special opcode. */
873 len += 1;
874
875 return len;
876 }
877
878 static void
879 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
880 {
881 unsigned int tmp, opcode;
882 int need_copy = 0;
883 char *end = p + len;
884
885 /* Line number sequences cannot go backward in addresses. This means
886 we've incorrectly ordered the statements in the sequence. */
887 assert ((offsetT) addr_delta >= 0);
888
889 /* Scale the address delta by the minimum instruction length. */
890 scale_addr_delta (&addr_delta);
891
892 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
893 We cannot use special opcodes here, since we want the end_sequence
894 to emit the matrix entry. */
895 if (line_delta == INT_MAX)
896 {
897 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
898 *p++ = DW_LNS_const_add_pc;
899 else
900 {
901 *p++ = DW_LNS_advance_pc;
902 p += output_leb128 (p, addr_delta, 0);
903 }
904
905 *p++ = DW_LNS_extended_op;
906 *p++ = 1;
907 *p++ = DW_LNE_end_sequence;
908 goto done;
909 }
910
911 /* Bias the line delta by the base. */
912 tmp = line_delta - DWARF2_LINE_BASE;
913
914 /* If the line increment is out of range of a special opcode, we
915 must encode it with DW_LNS_advance_line. */
916 if (tmp >= DWARF2_LINE_RANGE)
917 {
918 *p++ = DW_LNS_advance_line;
919 p += output_leb128 (p, line_delta, 1);
920
921 line_delta = 0;
922 tmp = 0 - DWARF2_LINE_BASE;
923 need_copy = 1;
924 }
925
926 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
927 special opcode. */
928 if (line_delta == 0 && addr_delta == 0)
929 {
930 *p++ = DW_LNS_copy;
931 goto done;
932 }
933
934 /* Bias the opcode by the special opcode base. */
935 tmp += DWARF2_LINE_OPCODE_BASE;
936
937 /* Avoid overflow when addr_delta is large. */
938 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
939 {
940 /* Try using a special opcode. */
941 opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
942 if (opcode <= 255)
943 {
944 *p++ = opcode;
945 goto done;
946 }
947
948 /* Try using DW_LNS_const_add_pc followed by special op. */
949 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
950 if (opcode <= 255)
951 {
952 *p++ = DW_LNS_const_add_pc;
953 *p++ = opcode;
954 goto done;
955 }
956 }
957
958 /* Otherwise use DW_LNS_advance_pc. */
959 *p++ = DW_LNS_advance_pc;
960 p += output_leb128 (p, addr_delta, 0);
961
962 if (need_copy)
963 *p++ = DW_LNS_copy;
964 else
965 *p++ = tmp;
966
967 done:
968 assert (p == end);
969 }
970
971 /* Handy routine to combine calls to the above two routines. */
972
973 static void
974 out_inc_line_addr (int line_delta, addressT addr_delta)
975 {
976 int len = size_inc_line_addr (line_delta, addr_delta);
977 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
978 }
979
980 /* Generate a variant frag that we can use to relax address/line
981 increments between fragments of the target segment. */
982
983 static void
984 relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
985 {
986 expressionS expr;
987 int max_chars;
988
989 expr.X_op = O_subtract;
990 expr.X_add_symbol = to_sym;
991 expr.X_op_symbol = from_sym;
992 expr.X_add_number = 0;
993
994 /* The maximum size of the frag is the line delta with a maximum
995 sized address delta. */
996 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
997
998 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
999 make_expr_symbol (&expr), line_delta, NULL);
1000 }
1001
1002 /* The function estimates the size of a rs_dwarf2dbg variant frag
1003 based on the current values of the symbols. It is called before
1004 the relaxation loop. We set fr_subtype to the expected length. */
1005
1006 int
1007 dwarf2dbg_estimate_size_before_relax (fragS *frag)
1008 {
1009 offsetT addr_delta;
1010 int size;
1011
1012 addr_delta = resolve_symbol_value (frag->fr_symbol);
1013 size = size_inc_line_addr (frag->fr_offset, addr_delta);
1014
1015 frag->fr_subtype = size;
1016
1017 return size;
1018 }
1019
1020 /* This function relaxes a rs_dwarf2dbg variant frag based on the
1021 current values of the symbols. fr_subtype is the current length
1022 of the frag. This returns the change in frag length. */
1023
1024 int
1025 dwarf2dbg_relax_frag (fragS *frag)
1026 {
1027 int old_size, new_size;
1028
1029 old_size = frag->fr_subtype;
1030 new_size = dwarf2dbg_estimate_size_before_relax (frag);
1031
1032 return new_size - old_size;
1033 }
1034
1035 /* This function converts a rs_dwarf2dbg variant frag into a normal
1036 fill frag. This is called after all relaxation has been done.
1037 fr_subtype will be the desired length of the frag. */
1038
1039 void
1040 dwarf2dbg_convert_frag (fragS *frag)
1041 {
1042 offsetT addr_diff;
1043
1044 addr_diff = resolve_symbol_value (frag->fr_symbol);
1045
1046 /* fr_var carries the max_chars that we created the fragment with.
1047 fr_subtype carries the current expected length. We must, of
1048 course, have allocated enough memory earlier. */
1049 assert (frag->fr_var >= (int) frag->fr_subtype);
1050
1051 emit_inc_line_addr (frag->fr_offset, addr_diff,
1052 frag->fr_literal + frag->fr_fix, frag->fr_subtype);
1053
1054 frag->fr_fix += frag->fr_subtype;
1055 frag->fr_type = rs_fill;
1056 frag->fr_var = 0;
1057 frag->fr_offset = 0;
1058 }
1059
1060 /* Generate .debug_line content for the chain of line number entries
1061 beginning at E, for segment SEG. */
1062
1063 static void
1064 process_entries (segT seg, struct line_entry *e)
1065 {
1066 unsigned filenum = 1;
1067 unsigned line = 1;
1068 unsigned column = 0;
1069 unsigned isa = 0;
1070 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1071 fragS *last_frag = NULL, *frag;
1072 addressT last_frag_ofs = 0, frag_ofs;
1073 symbolS *last_lab = NULL, *lab;
1074 struct line_entry *next;
1075
1076 do
1077 {
1078 int line_delta;
1079
1080 if (filenum != e->loc.filenum)
1081 {
1082 filenum = e->loc.filenum;
1083 out_opcode (DW_LNS_set_file);
1084 out_uleb128 (filenum);
1085 }
1086
1087 if (column != e->loc.column)
1088 {
1089 column = e->loc.column;
1090 out_opcode (DW_LNS_set_column);
1091 out_uleb128 (column);
1092 }
1093
1094 if (isa != e->loc.isa)
1095 {
1096 isa = e->loc.isa;
1097 out_opcode (DW_LNS_set_isa);
1098 out_uleb128 (isa);
1099 }
1100
1101 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
1102 {
1103 flags = e->loc.flags;
1104 out_opcode (DW_LNS_negate_stmt);
1105 }
1106
1107 if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
1108 out_opcode (DW_LNS_set_basic_block);
1109
1110 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
1111 out_opcode (DW_LNS_set_prologue_end);
1112
1113 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1114 out_opcode (DW_LNS_set_epilogue_begin);
1115
1116 /* Don't try to optimize away redundant entries; gdb wants two
1117 entries for a function where the code starts on the same line as
1118 the {, and there's no way to identify that case here. Trust gcc
1119 to optimize appropriately. */
1120 line_delta = e->loc.line - line;
1121 lab = e->label;
1122 frag = symbol_get_frag (lab);
1123 frag_ofs = S_GET_VALUE (lab);
1124
1125 if (last_frag == NULL)
1126 {
1127 out_set_addr (lab);
1128 out_inc_line_addr (line_delta, 0);
1129 }
1130 else if (frag == last_frag)
1131 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
1132 else
1133 relax_inc_line_addr (line_delta, lab, last_lab);
1134
1135 line = e->loc.line;
1136 last_lab = lab;
1137 last_frag = frag;
1138 last_frag_ofs = frag_ofs;
1139
1140 next = e->next;
1141 free (e);
1142 e = next;
1143 }
1144 while (e);
1145
1146 /* Emit a DW_LNE_end_sequence for the end of the section. */
1147 frag = last_frag_for_seg (seg);
1148 frag_ofs = get_frag_fix (frag, seg);
1149 if (frag == last_frag)
1150 out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
1151 else
1152 {
1153 lab = symbol_temp_new (seg, frag_ofs, frag);
1154 relax_inc_line_addr (INT_MAX, lab, last_lab);
1155 }
1156 }
1157
1158 /* Emit the directory and file tables for .debug_line. */
1159
1160 static void
1161 out_file_list (void)
1162 {
1163 size_t size;
1164 char *cp;
1165 unsigned int i;
1166
1167 /* Emit directory list. */
1168 for (i = 1; i < dirs_in_use; ++i)
1169 {
1170 size = strlen (dirs[i]) + 1;
1171 cp = frag_more (size);
1172 memcpy (cp, dirs[i], size);
1173 }
1174 /* Terminate it. */
1175 out_byte ('\0');
1176
1177 for (i = 1; i < files_in_use; ++i)
1178 {
1179 if (files[i].filename == NULL)
1180 {
1181 as_bad (_("unassigned file number %ld"), (long) i);
1182 /* Prevent a crash later, particularly for file 1. */
1183 files[i].filename = "";
1184 continue;
1185 }
1186
1187 size = strlen (files[i].filename) + 1;
1188 cp = frag_more (size);
1189 memcpy (cp, files[i].filename, size);
1190
1191 out_uleb128 (files[i].dir); /* directory number */
1192 out_uleb128 (0); /* last modification timestamp */
1193 out_uleb128 (0); /* filesize */
1194 }
1195
1196 /* Terminate filename list. */
1197 out_byte (0);
1198 }
1199
1200 /* Emit the collected .debug_line data. */
1201
1202 static void
1203 out_debug_line (segT line_seg)
1204 {
1205 expressionS expr;
1206 symbolS *line_start;
1207 symbolS *prologue_end;
1208 symbolS *line_end;
1209 struct line_seg *s;
1210 enum dwarf2_format d2f;
1211 int sizeof_offset;
1212
1213 subseg_set (line_seg, 0);
1214
1215 line_start = symbol_temp_new_now ();
1216 prologue_end = symbol_temp_make ();
1217 line_end = symbol_temp_make ();
1218
1219 /* Total length of the information for this compilation unit. */
1220 expr.X_op = O_subtract;
1221 expr.X_add_symbol = line_end;
1222 expr.X_op_symbol = line_start;
1223
1224 d2f = DWARF2_FORMAT ();
1225 if (d2f == dwarf2_format_32bit)
1226 {
1227 expr.X_add_number = -4;
1228 emit_expr (&expr, 4);
1229 sizeof_offset = 4;
1230 }
1231 else if (d2f == dwarf2_format_64bit)
1232 {
1233 expr.X_add_number = -12;
1234 out_four (-1);
1235 emit_expr (&expr, 8);
1236 sizeof_offset = 8;
1237 }
1238 else if (d2f == dwarf2_format_64bit_irix)
1239 {
1240 expr.X_add_number = -8;
1241 emit_expr (&expr, 8);
1242 sizeof_offset = 8;
1243 }
1244 else
1245 {
1246 as_fatal (_("internal error: unknown dwarf2 format"));
1247 }
1248
1249 /* Version. */
1250 out_two (2);
1251
1252 /* Length of the prologue following this length. */
1253 expr.X_op = O_subtract;
1254 expr.X_add_symbol = prologue_end;
1255 expr.X_op_symbol = line_start;
1256 expr.X_add_number = - (4 + 2 + 4);
1257 emit_expr (&expr, sizeof_offset);
1258
1259 /* Parameters of the state machine. */
1260 out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
1261 out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
1262 out_byte (DWARF2_LINE_BASE);
1263 out_byte (DWARF2_LINE_RANGE);
1264 out_byte (DWARF2_LINE_OPCODE_BASE);
1265
1266 /* Standard opcode lengths. */
1267 out_byte (0); /* DW_LNS_copy */
1268 out_byte (1); /* DW_LNS_advance_pc */
1269 out_byte (1); /* DW_LNS_advance_line */
1270 out_byte (1); /* DW_LNS_set_file */
1271 out_byte (1); /* DW_LNS_set_column */
1272 out_byte (0); /* DW_LNS_negate_stmt */
1273 out_byte (0); /* DW_LNS_set_basic_block */
1274 out_byte (0); /* DW_LNS_const_add_pc */
1275 out_byte (1); /* DW_LNS_fixed_advance_pc */
1276 out_byte (0); /* DW_LNS_set_prologue_end */
1277 out_byte (0); /* DW_LNS_set_epilogue_begin */
1278 out_byte (1); /* DW_LNS_set_isa */
1279
1280 out_file_list ();
1281
1282 symbol_set_value_now (prologue_end);
1283
1284 /* For each section, emit a statement program. */
1285 for (s = all_segs; s; s = s->next)
1286 process_entries (s->seg, s->head->head);
1287
1288 symbol_set_value_now (line_end);
1289 }
1290
1291 static void
1292 out_debug_ranges (segT ranges_seg)
1293 {
1294 unsigned int addr_size = sizeof_address;
1295 struct line_seg *s;
1296 expressionS expr;
1297 unsigned int i;
1298
1299 subseg_set (ranges_seg, 0);
1300
1301 /* Base Address Entry. */
1302 for (i = 0; i < addr_size; i++)
1303 out_byte (0xff);
1304 for (i = 0; i < addr_size; i++)
1305 out_byte (0);
1306
1307 /* Range List Entry. */
1308 for (s = all_segs; s; s = s->next)
1309 {
1310 fragS *frag;
1311 symbolS *beg, *end;
1312
1313 frag = first_frag_for_seg (s->seg);
1314 beg = symbol_temp_new (s->seg, 0, frag);
1315 s->text_start = beg;
1316
1317 frag = last_frag_for_seg (s->seg);
1318 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1319 s->text_end = end;
1320
1321 expr.X_op = O_symbol;
1322 expr.X_add_symbol = beg;
1323 expr.X_add_number = 0;
1324 emit_expr (&expr, addr_size);
1325
1326 expr.X_op = O_symbol;
1327 expr.X_add_symbol = end;
1328 expr.X_add_number = 0;
1329 emit_expr (&expr, addr_size);
1330 }
1331
1332 /* End of Range Entry. */
1333 for (i = 0; i < addr_size; i++)
1334 out_byte (0);
1335 for (i = 0; i < addr_size; i++)
1336 out_byte (0);
1337 }
1338
1339 /* Emit data for .debug_aranges. */
1340
1341 static void
1342 out_debug_aranges (segT aranges_seg, segT info_seg)
1343 {
1344 unsigned int addr_size = sizeof_address;
1345 addressT size, skip;
1346 struct line_seg *s;
1347 expressionS expr;
1348 char *p;
1349
1350 size = 4 + 2 + 4 + 1 + 1;
1351
1352 skip = 2 * addr_size - (size & (2 * addr_size - 1));
1353 if (skip == 2 * addr_size)
1354 skip = 0;
1355 size += skip;
1356
1357 for (s = all_segs; s; s = s->next)
1358 size += 2 * addr_size;
1359
1360 size += 2 * addr_size;
1361
1362 subseg_set (aranges_seg, 0);
1363
1364 /* Length of the compilation unit. */
1365 out_four (size - 4);
1366
1367 /* Version. */
1368 out_two (2);
1369
1370 /* Offset to .debug_info. */
1371 /* ??? sizeof_offset */
1372 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), 4);
1373
1374 /* Size of an address (offset portion). */
1375 out_byte (addr_size);
1376
1377 /* Size of a segment descriptor. */
1378 out_byte (0);
1379
1380 /* Align the header. */
1381 if (skip)
1382 frag_align (ffs (2 * addr_size) - 1, 0, 0);
1383
1384 for (s = all_segs; s; s = s->next)
1385 {
1386 fragS *frag;
1387 symbolS *beg, *end;
1388
1389 frag = first_frag_for_seg (s->seg);
1390 beg = symbol_temp_new (s->seg, 0, frag);
1391 s->text_start = beg;
1392
1393 frag = last_frag_for_seg (s->seg);
1394 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
1395 s->text_end = end;
1396
1397 expr.X_op = O_symbol;
1398 expr.X_add_symbol = beg;
1399 expr.X_add_number = 0;
1400 emit_expr (&expr, addr_size);
1401
1402 expr.X_op = O_subtract;
1403 expr.X_add_symbol = end;
1404 expr.X_op_symbol = beg;
1405 expr.X_add_number = 0;
1406 emit_expr (&expr, addr_size);
1407 }
1408
1409 p = frag_more (2 * addr_size);
1410 md_number_to_chars (p, 0, addr_size);
1411 md_number_to_chars (p + addr_size, 0, addr_size);
1412 }
1413
1414 /* Emit data for .debug_abbrev. Note that this must be kept in
1415 sync with out_debug_info below. */
1416
1417 static void
1418 out_debug_abbrev (segT abbrev_seg)
1419 {
1420 subseg_set (abbrev_seg, 0);
1421
1422 out_uleb128 (1);
1423 out_uleb128 (DW_TAG_compile_unit);
1424 out_byte (DW_CHILDREN_no);
1425 out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
1426 if (all_segs->next == NULL)
1427 {
1428 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1429 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1430 }
1431 else
1432 {
1433 if (DWARF2_FORMAT () == dwarf2_format_32bit)
1434 out_abbrev (DW_AT_ranges, DW_FORM_data4);
1435 else
1436 out_abbrev (DW_AT_ranges, DW_FORM_data8);
1437 }
1438 out_abbrev (DW_AT_name, DW_FORM_string);
1439 out_abbrev (DW_AT_comp_dir, DW_FORM_string);
1440 out_abbrev (DW_AT_producer, DW_FORM_string);
1441 out_abbrev (DW_AT_language, DW_FORM_data2);
1442 out_abbrev (0, 0);
1443
1444 /* Terminate the abbreviations for this compilation unit. */
1445 out_byte (0);
1446 }
1447
1448 /* Emit a description of this compilation unit for .debug_info. */
1449
1450 static void
1451 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg)
1452 {
1453 char producer[128];
1454 char *comp_dir;
1455 expressionS expr;
1456 symbolS *info_start;
1457 symbolS *info_end;
1458 char *p;
1459 int len;
1460 enum dwarf2_format d2f;
1461 int sizeof_offset;
1462
1463 subseg_set (info_seg, 0);
1464
1465 info_start = symbol_temp_new_now ();
1466 info_end = symbol_temp_make ();
1467
1468 /* Compilation Unit length. */
1469 expr.X_op = O_subtract;
1470 expr.X_add_symbol = info_end;
1471 expr.X_op_symbol = info_start;
1472
1473 d2f = DWARF2_FORMAT ();
1474 if (d2f == dwarf2_format_32bit)
1475 {
1476 expr.X_add_number = -4;
1477 emit_expr (&expr, 4);
1478 sizeof_offset = 4;
1479 }
1480 else if (d2f == dwarf2_format_64bit)
1481 {
1482 expr.X_add_number = -12;
1483 out_four (-1);
1484 emit_expr (&expr, 8);
1485 sizeof_offset = 8;
1486 }
1487 else if (d2f == dwarf2_format_64bit_irix)
1488 {
1489 expr.X_add_number = -8;
1490 emit_expr (&expr, 8);
1491 sizeof_offset = 8;
1492 }
1493 else
1494 {
1495 as_fatal (_("internal error: unknown dwarf2 format"));
1496 }
1497
1498 /* DWARF version. */
1499 out_two (2);
1500
1501 /* .debug_abbrev offset */
1502 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
1503
1504 /* Target address size. */
1505 out_byte (sizeof_address);
1506
1507 /* DW_TAG_compile_unit DIE abbrev */
1508 out_uleb128 (1);
1509
1510 /* DW_AT_stmt_list */
1511 /* ??? sizeof_offset */
1512 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 4);
1513
1514 /* These two attributes are emitted if all of the code is contiguous. */
1515 if (all_segs->next == NULL)
1516 {
1517 /* DW_AT_low_pc */
1518 expr.X_op = O_symbol;
1519 expr.X_add_symbol = all_segs->text_start;
1520 expr.X_add_number = 0;
1521 emit_expr (&expr, sizeof_address);
1522
1523 /* DW_AT_high_pc */
1524 expr.X_op = O_symbol;
1525 expr.X_add_symbol = all_segs->text_end;
1526 expr.X_add_number = 0;
1527 emit_expr (&expr, sizeof_address);
1528 }
1529 else
1530 {
1531 /* This attributes is emitted if the code is disjoint. */
1532
1533 /* DW_AT_ranges */
1534 expr.X_op = O_symbol;
1535 expr.X_add_symbol = section_symbol (ranges_seg);
1536 expr.X_add_number = 0;
1537 emit_expr (&expr, sizeof_address);
1538 }
1539
1540 /* DW_AT_name. We don't have the actual file name that was present
1541 on the command line, so assume files[1] is the main input file.
1542 We're not supposed to get called unless at least one line number
1543 entry was emitted, so this should always be defined. */
1544 if (!files || files_in_use < 1)
1545 abort ();
1546 if (files[1].dir)
1547 {
1548 len = strlen (dirs[files[1].dir]);
1549 p = frag_more (len + 1);
1550 memcpy (p, dirs[files[1].dir], len);
1551 INSERT_DIR_SEPARATOR (p, len);
1552 }
1553 len = strlen (files[1].filename) + 1;
1554 p = frag_more (len);
1555 memcpy (p, files[1].filename, len);
1556
1557 /* DW_AT_comp_dir */
1558 comp_dir = getpwd ();
1559 len = strlen (comp_dir) + 1;
1560 p = frag_more (len);
1561 memcpy (p, comp_dir, len);
1562
1563 /* DW_AT_producer */
1564 sprintf (producer, "GNU AS %s", VERSION);
1565 len = strlen (producer) + 1;
1566 p = frag_more (len);
1567 memcpy (p, producer, len);
1568
1569 /* DW_AT_language. Yes, this is probably not really MIPS, but the
1570 dwarf2 draft has no standard code for assembler. */
1571 out_two (DW_LANG_Mips_Assembler);
1572
1573 symbol_set_value_now (info_end);
1574 }
1575
1576 /* Finish the dwarf2 debug sections. We emit .debug.line if there
1577 were any .file/.loc directives, or --gdwarf2 was given, or if the
1578 file has a non-empty .debug_info section. If we emit .debug_line,
1579 and the .debug_info section is empty, we also emit .debug_info,
1580 .debug_aranges and .debug_abbrev. ALL_SEGS will be non-null if
1581 there were any .file/.loc directives, or --gdwarf2 was given and
1582 there were any located instructions emitted. */
1583
1584 void
1585 dwarf2_finish (void)
1586 {
1587 segT line_seg;
1588 struct line_seg *s;
1589 segT info_seg;
1590 int emit_other_sections = 0;
1591
1592 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
1593 emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
1594
1595 if (!all_segs && emit_other_sections)
1596 /* There is no line information and no non-empty .debug_info
1597 section. */
1598 return;
1599
1600 /* Calculate the size of an address for the target machine. */
1601 sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
1602
1603 /* Create and switch to the line number section. */
1604 line_seg = subseg_new (".debug_line", 0);
1605 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
1606
1607 /* For each subsection, chain the debug entries together. */
1608 for (s = all_segs; s; s = s->next)
1609 {
1610 struct line_subseg *ss = s->head;
1611 struct line_entry **ptail = ss->ptail;
1612
1613 while ((ss = ss->next) != NULL)
1614 {
1615 *ptail = ss->head;
1616 ptail = ss->ptail;
1617 }
1618 }
1619
1620 out_debug_line (line_seg);
1621
1622 /* If this is assembler generated line info, and there is no
1623 debug_info already, we need .debug_info and .debug_abbrev
1624 sections as well. */
1625 if (emit_other_sections)
1626 {
1627 segT abbrev_seg;
1628 segT aranges_seg;
1629 segT ranges_seg;
1630
1631 assert (all_segs);
1632
1633 info_seg = subseg_new (".debug_info", 0);
1634 abbrev_seg = subseg_new (".debug_abbrev", 0);
1635 aranges_seg = subseg_new (".debug_aranges", 0);
1636
1637 bfd_set_section_flags (stdoutput, info_seg,
1638 SEC_READONLY | SEC_DEBUGGING);
1639 bfd_set_section_flags (stdoutput, abbrev_seg,
1640 SEC_READONLY | SEC_DEBUGGING);
1641 bfd_set_section_flags (stdoutput, aranges_seg,
1642 SEC_READONLY | SEC_DEBUGGING);
1643
1644 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
1645
1646 if (all_segs->next == NULL)
1647 ranges_seg = NULL;
1648 else
1649 {
1650 ranges_seg = subseg_new (".debug_ranges", 0);
1651 bfd_set_section_flags (stdoutput, ranges_seg,
1652 SEC_READONLY | SEC_DEBUGGING);
1653 record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1);
1654 out_debug_ranges (ranges_seg);
1655 }
1656
1657 out_debug_aranges (aranges_seg, info_seg);
1658 out_debug_abbrev (abbrev_seg);
1659 out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg);
1660 }
1661 }
This page took 0.077981 seconds and 5 git commands to generate.