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