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