]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/dwarf2dbg.c
2002-11-30 Nathanael Nerode <neroden@gcc.gnu.org>
[thirdparty/binutils-gdb.git] / gas / dwarf2dbg.c
CommitLineData
fac0d250 1/* dwarf2dbg.c - DWARF2 debug support
70658493 2 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
fac0d250
RH
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
89b66cde 20 02111-1307, USA. */
fac0d250 21
89b66cde 22/* Logical line numbers can be controlled by the compiler via the
fac0d250
RH
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"
fac0d250 31#include "as.h"
92eb7b32 32
42dbf88c
NC
33#ifdef HAVE_LIMITS_H
34#include <limits.h>
92625c16 35#else
6717891c
NC
36#ifdef HAVE_SYS_PARAM_H
37#include <sys/param.h>
38#endif
6256f9dd 39#ifndef INT_MAX
ee515fb7 40#define INT_MAX (int) (((unsigned) (-1)) >> 1)
42dbf88c 41#endif
b8f080d6 42#endif
42dbf88c 43
70658493
AM
44#include "dwarf2dbg.h"
45
14e777e0
KB
46#ifndef DWARF2_FORMAT
47#define DWARF2_FORMAT() dwarf2_format_32bit
48#endif
49
92eb7b32
L
50#ifdef BFD_ASSEMBLER
51
fac0d250
RH
52#include "subsegs.h"
53
d9ac5a3b 54#include "elf/dwarf2.h"
fac0d250 55
fac0d250
RH
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. */
4dc7ead9 84# define DWARF2_LINE_MIN_INSN_LENGTH 1
fac0d250
RH
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
cb30237e 93/* Given a special op, return the line skip amount. */
fac0d250
RH
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
cb30237e 101/* The maximum address skip amount that can be encoded with a special op. */
fac0d250
RH
102#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
103
ee515fb7 104struct line_entry {
220e750f
RH
105 struct line_entry *next;
106 fragS *frag;
107 addressT frag_ofs;
108 struct dwarf2_line_info loc;
e6c774b4 109};
fac0d250 110
ee515fb7 111struct line_subseg {
220e750f
RH
112 struct line_subseg *next;
113 subsegT subseg;
114 struct line_entry *head;
115 struct line_entry **ptail;
116};
353e2c69 117
ee515fb7 118struct line_seg {
220e750f
RH
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. */
127static struct line_seg *all_segs;
128
ee515fb7 129struct file_entry {
220e750f
RH
130 char *filename;
131 unsigned int dir;
132};
133
134/* Table of files used by .debug_line. */
135static struct file_entry *files;
136static unsigned int files_in_use;
137static unsigned int files_allocated;
138
220e750f
RH
139/* True when we've seen a .loc directive recently. Used to avoid
140 doing work when there's nothing to do. */
141static boolean loc_directive_seen;
142
143/* Current location as indicated by the most recent .loc directive. */
144static struct dwarf2_line_info current;
145
146/* Fake label name. */
147static char const fake_label_name[] = ".L0\001";
148
149/* The size of an address on the target. */
150static unsigned int sizeof_address;
151\f
152static struct line_subseg *get_line_subseg PARAMS ((segT, subsegT));
153static unsigned int get_filenum PARAMS ((const char *));
154static struct frag *first_frag_for_seg PARAMS ((segT));
155static struct frag *last_frag_for_seg PARAMS ((segT));
156static void out_byte PARAMS ((int));
157static void out_opcode PARAMS ((int));
158static void out_two PARAMS ((int));
159static void out_four PARAMS ((int));
160static void out_abbrev PARAMS ((int, int));
161static void out_uleb128 PARAMS ((addressT));
162static symbolS *symbol_new_now PARAMS ((void));
163static void set_symbol_value_now PARAMS ((symbolS *));
164static offsetT get_frag_fix PARAMS ((fragS *));
165static void out_set_addr PARAMS ((segT, fragS *, addressT));
166static int size_inc_line_addr PARAMS ((int, addressT));
167static void emit_inc_line_addr PARAMS ((int, addressT, char *, int));
168static void out_inc_line_addr PARAMS ((int, addressT));
169static void relax_inc_line_addr PARAMS ((int, segT, fragS *, addressT,
170 fragS *, addressT));
171static void process_entries PARAMS ((segT, struct line_entry *));
172static void out_file_list PARAMS ((void));
173static void out_debug_line PARAMS ((segT));
174static void out_debug_aranges PARAMS ((segT, segT));
175static void out_debug_abbrev PARAMS ((segT));
176static void out_debug_info PARAMS ((segT, segT, segT));
a3b75434 177static void scale_addr_delta PARAMS ((int *));
220e750f
RH
178\f
179/* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */
180
181static struct line_subseg *
182get_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;
fac0d250 192
220e750f
RH
193 if (seg == last_seg && subseg == last_subseg)
194 return last_line_subseg;
195
ee515fb7 196 for (s = all_segs; s; s = s->next)
220e750f
RH
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)
fac0d250 208 {
220e750f 209 if (ss->subseg == subseg)
ee515fb7 210 goto found_subseg;
220e750f
RH
211 if (ss->subseg > subseg)
212 break;
fac0d250 213 }
220e750f
RH
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;
fac0d250
RH
228}
229
220e750f 230/* Record an entry for LOC ocurring at OFS within the current fragment. */
353e2c69 231
220e750f
RH
232void
233dwarf2_gen_line_info (ofs, loc)
234 addressT ofs;
235 struct dwarf2_line_info *loc;
fac0d250 236{
220e750f
RH
237 struct line_subseg *ss;
238 struct line_entry *e;
1ea5c325
MS
239 static unsigned int line = -1;
240 static unsigned int filenum = -1;
220e750f
RH
241
242 /* Early out for as-yet incomplete location information. */
243 if (loc->filenum == 0 || loc->line == 0)
244 return;
245
ffa554ed
GK
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. */
d1a6c242 250 if (debug_type == DEBUG_DWARF2
ffa554ed 251 && line == loc->line && filenum == loc->filenum)
1ea5c325
MS
252 return;
253
254 line = loc->line;
255 filenum = loc->filenum;
256
220e750f
RH
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}
fac0d250 267
220e750f
RH
268void
269dwarf2_where (line)
270 struct dwarf2_line_info *line;
271{
272 if (debug_type == DEBUG_DWARF2)
fac0d250 273 {
220e750f
RH
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;
fac0d250 279 }
220e750f
RH
280 else
281 *line = current;
fac0d250
RH
282}
283
220e750f
RH
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. */
353e2c69 288
220e750f
RH
289void
290dwarf2_emit_insn (size)
291 int size;
fac0d250 292{
220e750f 293 struct dwarf2_line_info loc;
fac0d250 294
b6675117 295 if (loc_directive_seen)
1080e97d
L
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 }
b6675117 309 else if (debug_type != DEBUG_DWARF2)
220e750f 310 return;
b6675117
NC
311 else
312 dwarf2_where (& loc);
313
220e750f
RH
314 dwarf2_gen_line_info (frag_now_fix () - size, &loc);
315}
fac0d250 316
220e750f
RH
317/* Get a .debug_line file number for FILENAME. */
318
319static unsigned int
320get_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)
fac0d250 335 {
249e3833
RH
336 unsigned int old = files_allocated;
337
220e750f
RH
338 files_allocated = i + 32;
339 files = (struct file_entry *)
ee515fb7 340 xrealloc (files, (i + 32) * sizeof (struct file_entry));
249e3833
RH
341
342 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
fac0d250
RH
343 }
344
ee515fb7 345 files[i].filename = xstrdup (filename);
220e750f
RH
346 files[i].dir = 0;
347 files_in_use = i + 1;
348 last_used = i;
349
350 return i;
351}
fac0d250 352
ecb4347a
DJ
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
220e750f 356
ecb4347a
DJ
357 If an entry is added to the file table, return a pointer to the filename. */
358
359char *
220e750f
RH
360dwarf2_directive_file (dummy)
361 int dummy ATTRIBUTE_UNUSED;
362{
363 offsetT num;
e46d99eb 364 char *filename;
220e750f
RH
365 int filename_len;
366
367 /* Continue to accept a bare string and pass it off. */
368 SKIP_WHITESPACE ();
369 if (*input_line_pointer == '"')
fac0d250 370 {
220e750f 371 s_app_file (0);
ecb4347a 372 return NULL;
fac0d250
RH
373 }
374
220e750f
RH
375 num = get_absolute_expression ();
376 filename = demand_copy_C_string (&filename_len);
377 demand_empty_rest_of_line ();
378
249e3833 379 if (num < 1)
fac0d250 380 {
0e389e77 381 as_bad (_("file number less than one"));
ecb4347a 382 return NULL;
fac0d250
RH
383 }
384
0e1a166b 385 if (num < (int) files_in_use && files[num].filename != 0)
220e750f 386 {
0e389e77 387 as_bad (_("file number %ld already allocated"), (long) num);
ecb4347a 388 return NULL;
249e3833 389 }
220e750f 390
249e3833
RH
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));
fac0d250 398
220e750f 399 /* Zero the new memory. */
249e3833 400 memset (files + old, 0, (num + 16 - old) * sizeof (struct file_entry));
220e750f
RH
401 }
402
249e3833
RH
403 files[num].filename = filename;
404 files[num].dir = 0;
405 files_in_use = num + 1;
ecb4347a
DJ
406
407 return filename;
fac0d250
RH
408}
409
220e750f
RH
410void
411dwarf2_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
249e3833 423 if (filenum < 1)
220e750f 424 {
0e389e77 425 as_bad (_("file number less than one"));
220e750f
RH
426 return;
427 }
249e3833 428 if (filenum >= (int) files_in_use || files[filenum].filename == 0)
220e750f 429 {
0e389e77 430 as_bad (_("unassigned file number %ld"), (long) filenum);
220e750f
RH
431 return;
432 }
433
249e3833 434 current.filenum = filenum;
220e750f
RH
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)
9d66a1d9
L
443 {
444 listing_source_file (files[filenum].filename);
445 listing_source_line (line);
446 }
220e750f
RH
447#endif
448}
220e750f
RH
449\f
450static struct frag *
451first_frag_for_seg (seg)
452 segT seg;
453{
454 frchainS *f, *first = NULL;
455
ee515fb7 456 for (f = frchain_root; f; f = f->frch_next)
220e750f
RH
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
464static struct frag *
465last_frag_for_seg (seg)
466 segT seg;
467{
468 frchainS *f, *last = NULL;
469
ee515fb7 470 for (f = frchain_root; f; f = f->frch_next)
220e750f
RH
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
480static inline void
481out_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
489static inline void
490out_opcode (opc)
491 int opc;
492{
493 out_byte (opc);
494}
495
496/* Emit a two-byte word into the current segment. */
497
498static inline void
499out_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
507static inline void
508out_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
fac0d250 516static void
220e750f
RH
517out_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
525static inline void
526out_abbrev (name, form)
527 int name, form;
fac0d250 528{
220e750f
RH
529 out_uleb128 (name);
530 out_uleb128 (form);
531}
fac0d250 532
220e750f
RH
533/* Create a new fake symbol whose value is the current position. */
534
535static symbolS *
536symbol_new_now ()
537{
538 return symbol_new (fake_label_name, now_seg, frag_now_fix (), frag_now);
fac0d250
RH
539}
540
220e750f 541/* Set the value of SYM to the current position in the current segment. */
353e2c69 542
fac0d250 543static void
220e750f
RH
544set_symbol_value_now (sym)
545 symbolS *sym;
fac0d250 546{
220e750f
RH
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. */
fac0d250 553
220e750f
RH
554static offsetT
555get_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. */
ee515fb7 566 for (fr = frchain_root; fr; fr = fr->frch_next)
220e750f
RH
567 if (fr->frch_last == frag)
568 {
7c2b59d0
AM
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;
220e750f
RH
572 }
573
574 abort ();
575}
fac0d250 576
220e750f 577/* Set an absolute address (may result in a relocation entry). */
fac0d250 578
220e750f
RH
579static void
580out_set_addr (seg, frag, ofs)
581 segT seg;
582 fragS *frag;
583 addressT ofs;
584{
585 expressionS expr;
586 symbolS *sym;
fac0d250 587
220e750f 588 sym = symbol_new (fake_label_name, seg, ofs, frag);
9e3af0e7 589
fac0d250 590 out_opcode (DW_LNS_extended_op);
220e750f 591 out_uleb128 (sizeof_address + 1);
fac0d250
RH
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;
220e750f 597 emit_expr (&expr, sizeof_address);
fac0d250
RH
598}
599
a3b75434
DD
600#if DWARF2_LINE_MIN_INSN_LENGTH > 1
601static void
602scale_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
220e750f
RH
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.
353e2c69 620
220e750f
RH
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
625static int
626size_inc_line_addr (line_delta, addr_delta)
627 int line_delta;
628 addressT addr_delta;
fac0d250 629{
220e750f
RH
630 unsigned int tmp, opcode;
631 int len = 0;
fac0d250 632
220e750f 633 /* Scale the address delta by the minimum instruction length. */
a3b75434 634 scale_addr_delta (&addr_delta);
220e750f
RH
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)
fac0d250 640 {
220e750f
RH
641 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
642 len = 1;
fac0d250 643 else
220e750f
RH
644 len = 1 + sizeof_leb128 (addr_delta, 0);
645 return len + 3;
fac0d250 646 }
fac0d250 647
220e750f
RH
648 /* Bias the line delta by the base. */
649 tmp = line_delta - DWARF2_LINE_BASE;
fac0d250 650
220e750f
RH
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 }
fac0d250 659
220e750f
RH
660 /* Bias the opcode by the special opcode base. */
661 tmp += DWARF2_LINE_OPCODE_BASE;
353e2c69 662
220e750f
RH
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}
fac0d250 685
220e750f
RH
686static void
687emit_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;
fac0d250 696
220e750f 697 /* Scale the address delta by the minimum instruction length. */
a3b75434
DD
698 scale_addr_delta (&addr_delta);
699
220e750f
RH
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)
fac0d250 704 {
220e750f
RH
705 if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
706 *p++ = DW_LNS_const_add_pc;
707 else
fac0d250 708 {
220e750f
RH
709 *p++ = DW_LNS_advance_pc;
710 p += output_leb128 (p, addr_delta, 0);
fac0d250 711 }
220e750f
RH
712
713 *p++ = DW_LNS_extended_op;
714 *p++ = 1;
715 *p++ = DW_LNE_end_sequence;
716 goto done;
fac0d250
RH
717 }
718
220e750f
RH
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)
fac0d250 725 {
220e750f
RH
726 *p++ = DW_LNS_advance_line;
727 p += output_leb128 (p, line_delta, 1);
fac0d250 728
220e750f
RH
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 }
cb30237e 736
220e750f
RH
737 line_delta = 0;
738 tmp = 0 - DWARF2_LINE_BASE;
739 need_copy = 1;
740 }
fac0d250 741
220e750f
RH
742 /* Bias the opcode by the special opcode base. */
743 tmp += DWARF2_LINE_OPCODE_BASE;
fac0d250 744
220e750f
RH
745 /* Avoid overflow when addr_delta is large. */
746 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
fac0d250 747 {
220e750f
RH
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)
fac0d250 759 {
220e750f
RH
760 *p++ = DW_LNS_const_add_pc;
761 *p++ = opcode;
762 goto done;
fac0d250
RH
763 }
764 }
220e750f
RH
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;
fac0d250 772 else
220e750f 773 *p++ = tmp;
fac0d250 774
220e750f
RH
775 done:
776 assert (p == end);
777}
a8316fe2 778
220e750f 779/* Handy routine to combine calls to the above two routines. */
e1c05f12 780
220e750f
RH
781static void
782out_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}
9de8d8f1 789
220e750f
RH
790/* Generate a variant frag that we can use to relax address/line
791 increments between fragments of the target segment. */
9e3af0e7 792
220e750f
RH
793static void
794relax_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;
6576f0b5 803
220e750f
RH
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);
fac0d250 806
220e750f
RH
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;
fac0d250 811
220e750f
RH
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);
fac0d250 815
220e750f
RH
816 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
817 make_expr_symbol (&expr), line_delta, NULL);
818}
fac0d250 819
220e750f
RH
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. */
fac0d250 823
220e750f
RH
824int
825dwarf2dbg_estimate_size_before_relax (frag)
826 fragS *frag;
827{
828 offsetT addr_delta;
829 int size;
fac0d250 830
6386f3a7 831 addr_delta = resolve_symbol_value (frag->fr_symbol);
220e750f 832 size = size_inc_line_addr (frag->fr_offset, addr_delta);
fac0d250 833
220e750f 834 frag->fr_subtype = size;
fac0d250 835
220e750f
RH
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
843int
844dwarf2dbg_relax_frag (frag)
845 fragS *frag;
846{
847 int old_size, new_size;
fac0d250 848
220e750f
RH
849 old_size = frag->fr_subtype;
850 new_size = dwarf2dbg_estimate_size_before_relax (frag);
ee515fb7 851
220e750f 852 return new_size - old_size;
fac0d250
RH
853}
854
220e750f
RH
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
859void
860dwarf2dbg_convert_frag (frag)
861 fragS *frag;
fac0d250 862{
220e750f
RH
863 offsetT addr_diff;
864
6386f3a7 865 addr_diff = resolve_symbol_value (frag->fr_symbol);
fac0d250 866
220e750f
RH
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. */
bccba5f0 870 assert (frag->fr_var >= (int) frag->fr_subtype);
fac0d250 871
ee515fb7 872 emit_inc_line_addr (frag->fr_offset, addr_diff,
220e750f
RH
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
884static void
885process_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)
fac0d250 900 {
220e750f
RH
901 int changed = 0;
902
903 if (filenum != e->loc.filenum)
fac0d250 904 {
220e750f
RH
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
fb81275c
JM
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 */)
220e750f
RH
937 {
938 int line_delta = e->loc.line - line;
939 if (frag == NULL)
fac0d250 940 {
220e750f
RH
941 out_set_addr (seg, e->frag, e->frag_ofs);
942 out_inc_line_addr (line_delta, 0);
fac0d250 943 }
220e750f
RH
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;
fac0d250 953 }
220e750f
RH
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;
fac0d250 964 }
353e2c69 965
220e750f
RH
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
ee515fb7 972 relax_inc_line_addr (INT_MAX, seg, last_frag, last_frag_ofs,
220e750f 973 frag, frag_ofs);
fac0d250
RH
974}
975
220e750f
RH
976/* Emit the directory and file tables for .debug_line. */
977
fac0d250 978static void
220e750f 979out_file_list ()
fac0d250
RH
980{
981 size_t size;
982 char *cp;
220e750f
RH
983 unsigned int i;
984
985 /* Terminate directory list. */
986 out_byte ('\0');
fac0d250 987
220e750f 988 for (i = 1; i < files_in_use; ++i)
fac0d250 989 {
249e3833
RH
990 if (files[i].filename == NULL)
991 {
0e389e77 992 as_bad (_("unassigned file number %ld"), (long) i);
249e3833
RH
993 continue;
994 }
995
220e750f 996 size = strlen (files[i].filename) + 1;
fac0d250 997 cp = frag_more (size);
220e750f 998 memcpy (cp, files[i].filename, size);
fac0d250 999
220e750f 1000 out_uleb128 (files[i].dir); /* directory number */
fac0d250
RH
1001 out_uleb128 (0); /* last modification timestamp */
1002 out_uleb128 (0); /* filesize */
1003 }
353e2c69
KH
1004
1005 /* Terminate filename list. */
1006 out_byte (0);
fac0d250
RH
1007}
1008
220e750f
RH
1009/* Emit the collected .debug_line data. */
1010
1011static void
1012out_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;
14e777e0
KB
1020 enum dwarf2_format d2f;
1021 int sizeof_offset;
220e750f
RH
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;
14e777e0
KB
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 }
220e750f
RH
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);
14e777e0 1067 emit_expr (&expr, sizeof_offset);
220e750f
RH
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. */
ee515fb7 1092 for (s = all_segs; s; s = s->next)
220e750f
RH
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
58b5739a 1100static void
220e750f
RH
1101out_debug_aranges (aranges_seg, info_seg)
1102 segT aranges_seg;
1103 segT info_seg;
fac0d250 1104{
220e750f
RH
1105 unsigned int addr_size = sizeof_address;
1106 addressT size, skip;
1107 struct line_seg *s;
1108 expressionS expr;
1109 char *p;
fac0d250 1110
220e750f 1111 size = 4 + 2 + 4 + 1 + 1;
fac0d250 1112
ee515fb7
KH
1113 skip = 2 * addr_size - (size & (2 * addr_size - 1));
1114 if (skip == 2 * addr_size)
220e750f
RH
1115 skip = 0;
1116 size += skip;
fac0d250 1117
ee515fb7
KH
1118 for (s = all_segs; s; s = s->next)
1119 size += 2 * addr_size;
fac0d250 1120
ee515fb7 1121 size += 2 * addr_size;
fac0d250 1122
220e750f 1123 subseg_set (aranges_seg, 0);
fac0d250 1124
220e750f
RH
1125 /* Length of the compilation unit. */
1126 out_four (size - 4);
fac0d250 1127
220e750f
RH
1128 /* Version. */
1129 out_two (2);
4dc7ead9 1130
220e750f
RH
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)
ee515fb7 1145 frag_align (ffs (2 * addr_size) - 1, 0, 0);
4dc7ead9 1146
ee515fb7 1147 for (s = all_segs; s; s = s->next)
220e750f
RH
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 }
4dc7ead9 1171
220e750f
RH
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);
4dc7ead9
RH
1175}
1176
220e750f
RH
1177/* Emit data for .debug_abbrev. Note that this must be kept in
1178 sync with out_debug_info below. */
fac0d250 1179
220e750f
RH
1180static void
1181out_debug_abbrev (abbrev_seg)
1182 segT abbrev_seg;
1183{
1184 subseg_set (abbrev_seg, 0);
fac0d250 1185
220e750f
RH
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)
4dc7ead9 1191 {
220e750f
RH
1192 out_abbrev (DW_AT_low_pc, DW_FORM_addr);
1193 out_abbrev (DW_AT_high_pc, DW_FORM_addr);
1194 }
48b91938 1195 out_abbrev (DW_AT_name, DW_FORM_string);
220e750f
RH
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);
a987bfc9
RH
1200
1201 /* Terminate the abbreviations for this compilation unit. */
1202 out_byte (0);
220e750f 1203}
4dc7ead9 1204
220e750f 1205/* Emit a description of this compilation unit for .debug_info. */
4dc7ead9 1206
220e750f
RH
1207static void
1208out_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;
14e777e0
KB
1220 enum dwarf2_format d2f;
1221 int sizeof_offset;
4dc7ead9 1222
220e750f 1223 subseg_set (info_seg, 0);
4dc7ead9 1224
ee515fb7 1225 info_start = symbol_new_now ();
220e750f 1226 info_end = symbol_make (fake_label_name);
4dc7ead9 1227
220e750f
RH
1228 /* Compilation Unit length. */
1229 expr.X_op = O_subtract;
1230 expr.X_add_symbol = info_end;
1231 expr.X_op_symbol = info_start;
14e777e0
KB
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 }
4dc7ead9 1257
220e750f
RH
1258 /* DWARF version. */
1259 out_two (2);
4dc7ead9 1260
220e750f
RH
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;
14e777e0 1265 emit_expr (&expr, sizeof_offset);
4dc7ead9 1266
220e750f
RH
1267 /* Target address size. */
1268 out_byte (sizeof_address);
fac0d250 1269
220e750f
RH
1270 /* DW_TAG_compile_unit DIE abbrev */
1271 out_uleb128 (1);
fac0d250 1272
220e750f
RH
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);
fac0d250 1278
220e750f
RH
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)
58b5739a 1282 {
220e750f
RH
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);
58b5739a
RH
1294 }
1295
48b91938
RH
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
220e750f
RH
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);
fac0d250 1311
220e750f
RH
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);
fac0d250 1317
220e750f
RH
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);
fac0d250
RH
1323}
1324
1325void
220e750f 1326dwarf2_finish ()
fac0d250 1327{
220e750f
RH
1328 segT line_seg;
1329 struct line_seg *s;
fac0d250 1330
70ee4658
DJ
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)
220e750f 1340 return;
fac0d250 1341
220e750f 1342 /* Calculate the size of an address for the target machine. */
220e750f 1343 sizeof_address = bfd_arch_bits_per_address (stdoutput) / 8;
fac0d250 1344
220e750f
RH
1345 /* Create and switch to the line number section. */
1346 line_seg = subseg_new (".debug_line", 0);
220e750f 1347 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY);
fac0d250 1348
220e750f 1349 /* For each subsection, chain the debug entries together. */
ee515fb7 1350 for (s = all_segs; s; s = s->next)
fac0d250 1351 {
220e750f
RH
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 }
fac0d250 1360 }
85a39694 1361
220e750f 1362 out_debug_line (line_seg);
85a39694 1363
220e750f
RH
1364 /* If this is assembler generated line info, we need .debug_info
1365 and .debug_abbrev sections as well. */
45c500fa 1366 if (all_segs != NULL && debug_type == DEBUG_DWARF2)
220e750f
RH
1367 {
1368 segT abbrev_seg;
1369 segT info_seg;
1370 segT aranges_seg;
4dc7ead9 1371
220e750f
RH
1372 info_seg = subseg_new (".debug_info", 0);
1373 abbrev_seg = subseg_new (".debug_abbrev", 0);
1374 aranges_seg = subseg_new (".debug_aranges", 0);
ef99799a 1375
220e750f
RH
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);
ef99799a 1379
ee515fb7 1380 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
ef99799a 1381
220e750f
RH
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 }
85a39694 1386}
92eb7b32
L
1387
1388#else
1389void
1390dwarf2_finish ()
1391{
1392}
1393
1394int
1395dwarf2dbg_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
1402int
1403dwarf2dbg_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
1410void
1411dwarf2dbg_convert_frag (frag)
1412 fragS *frag ATTRIBUTE_UNUSED;
1413{
1414 as_fatal (_("dwarf2 is not supported for this object file format"));
1415}
1416
1417void
1418dwarf2_emit_insn (size)
1419 int size ATTRIBUTE_UNUSED;
1420{
1421}
1422
70658493 1423char *
92eb7b32
L
1424dwarf2_directive_file (dummy)
1425 int dummy ATTRIBUTE_UNUSED;
1426{
3737d051 1427 s_app_file (0);
70658493 1428 return NULL;
92eb7b32
L
1429}
1430
1431void
1432dwarf2_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 */