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