OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gas/] [dwarf2dbg.c] - Blame information for rev 299

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.