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