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