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