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