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