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