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