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