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