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