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