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