Coverage Report

Created: 2026-07-12 09:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/gas/dwarf2dbg.c
Line
Count
Source
1
/* dwarf2dbg.c - DWARF2 debug support
2
   Copyright (C) 1999-2026 Free Software Foundation, Inc.
3
   Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5
   This file is part of GAS, the GNU Assembler.
6
7
   GAS is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3, or (at your option)
10
   any later version.
11
12
   GAS is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with GAS; see the file COPYING.  If not, write to the Free
19
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20
   02110-1301, USA.  */
21
22
/* Logical line numbers can be controlled by the compiler via the
23
   following directives:
24
25
  .file FILENO "file.c"
26
  .loc  FILENO LINENO [COLUMN] [basic_block] [prologue_end] \
27
        [epilogue_begin] [is_stmt VALUE] [isa VALUE] \
28
        [discriminator VALUE] [view VALUE]
29
*/
30
31
#include "as.h"
32
#include "safe-ctype.h"
33
#include <limits.h>
34
#include "dwarf2dbg.h"
35
#include <filenames.h>
36
37
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
38
/* We need to decide which character to use as a directory separator.
39
   Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not
40
   necessarily mean that the backslash character is the one to use.
41
   Some environments, eg Cygwin, can support both naming conventions.
42
   So we use the heuristic that we only need to use the backslash if
43
   the path is an absolute path starting with a DOS style drive
44
   selector.  eg C: or D:  */
45
# define INSERT_DIR_SEPARATOR(string, offset) \
46
  do \
47
    { \
48
      if (offset > 1 \
49
    && string[0] != 0 \
50
    && string[1] == ':') \
51
       string [offset] = '\\'; \
52
      else \
53
       string [offset] = '/'; \
54
    } \
55
  while (0)
56
#else
57
6
# define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
58
#endif
59
60
#ifndef DWARF2_FORMAT
61
153
# define DWARF2_FORMAT(SEC) dwarf2_format_32bit
62
#endif
63
64
#ifndef DWARF2_ADDR_SIZE
65
# define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
66
#endif
67
68
#ifndef DWARF2_FILE_NAME
69
44
#define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME
70
#endif
71
72
#ifndef DWARF2_FILE_TIME_NAME
73
41
#define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) -1
74
#endif
75
76
#ifndef DWARF2_FILE_SIZE_NAME
77
41
#define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) -1
78
#endif
79
80
#ifndef DWARF2_VERSION
81
441k
#define DWARF2_VERSION dwarf_level
82
#endif
83
84
/* The .debug_aranges version has been 2 in DWARF version 2, 3 and 4. */
85
#ifndef DWARF2_ARANGES_VERSION
86
37
#define DWARF2_ARANGES_VERSION 2
87
#endif
88
89
/* The .debug_line version is the same as the .debug_info version.  */
90
#ifndef DWARF2_LINE_VERSION
91
440k
#define DWARF2_LINE_VERSION DWARF2_VERSION
92
#endif
93
94
/* The .debug_rnglists has only been in DWARF version 5. */
95
#ifndef DWARF2_RNGLISTS_VERSION
96
1
#define DWARF2_RNGLISTS_VERSION 5
97
#endif
98
99
#include "subsegs.h"
100
101
#include "dwarf2.h"
102
103
/* Since we can't generate the prolog until the body is complete, we
104
   use three different subsegments for .debug_line: one holding the
105
   prolog, one for the directory and filename info, and one for the
106
   body ("statement program").  */
107
#define DL_PROLOG 0
108
#define DL_FILES  1
109
#define DL_BODY   2
110
111
/* If linker relaxation might change offsets in the code, the DWARF special
112
   opcodes and variable-length operands cannot be used.  If this macro is
113
   nonzero, use the DW_LNS_fixed_advance_pc opcode instead.  */
114
#ifndef DWARF2_USE_FIXED_ADVANCE_PC
115
547
# define DWARF2_USE_FIXED_ADVANCE_PC  linkrelax
116
#endif
117
118
/* First special line opcode - leave room for the standard opcodes.
119
   Note: If you want to change this, you'll have to update the
120
   "standard_opcode_lengths" table that is emitted below in
121
   out_debug_line().  */
122
2.75k
#define DWARF2_LINE_OPCODE_BASE   (DWARF2_LINE_VERSION == 2 ? 10 : 13)
123
124
#ifndef DWARF2_LINE_BASE
125
  /* Minimum line offset in a special line info. opcode.  This value
126
     was chosen to give a reasonable range of values.  */
127
2.43k
# define DWARF2_LINE_BASE   -5
128
#endif
129
130
/* Range of line offsets in a special line info. opcode.  */
131
#ifndef DWARF2_LINE_RANGE
132
4.83k
# define DWARF2_LINE_RANGE    14
133
#endif
134
135
#ifndef DWARF2_LINE_MIN_INSN_LENGTH
136
  /* Define the architecture-dependent minimum instruction length (in
137
     bytes).  This value should be rather too small than too big.  */
138
1.73k
# define DWARF2_LINE_MIN_INSN_LENGTH  1
139
#endif
140
141
/* Flag that indicates the initial value of the is_stmt_start flag.  */
142
663
#define DWARF2_LINE_DEFAULT_IS_STMT 1
143
144
#ifndef DWARF2_LINE_MAX_OPS_PER_INSN
145
41
#define DWARF2_LINE_MAX_OPS_PER_INSN  1
146
#endif
147
148
/* Given a special op, return the line skip amount.  */
149
#define SPECIAL_LINE(op) \
150
  (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
151
152
/* Given a special op, return the address skip amount (in units of
153
   DWARF2_LINE_MIN_INSN_LENGTH.  */
154
1.63k
#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
155
156
/* The maximum address skip amount that can be encoded with a special op.  */
157
1.63k
#define MAX_SPECIAL_ADDR_DELTA    SPECIAL_ADDR(255)
158
159
#ifndef TC_PARSE_CONS_RETURN_NONE
160
#define TC_PARSE_CONS_RETURN_NONE BFD_RELOC_NONE
161
#endif
162
163
74
#define GAS_ABBREV_COMP_UNIT 1
164
10
#define GAS_ABBREV_SUBPROG   2
165
10
#define GAS_ABBREV_NO_TYPE   3
166
167
struct line_entry
168
{
169
  struct line_entry *next;
170
  symbolS *label;
171
  struct dwarf2_line_info loc;
172
};
173
174
/* Given line_entry list HEAD and PTAIL pointers, return a pointer to
175
   the last line_entry on the list.  */
176
static inline struct line_entry *
177
line_entry_at_tail (void *head, struct line_entry **ptail)
178
313
{
179
  /* If the list is empty ptail points at head.  */
180
313
  if (head == NULL)
181
0
    return NULL;
182
  /* Otherwise ptail points to line_entry.next of the last entry.  */
183
313
  void *p = (char *) ptail - offsetof (struct line_entry, next);
184
313
  return p;
185
313
}
186
187
struct line_subseg
188
{
189
  struct line_subseg *next;
190
  subsegT subseg;
191
  struct line_entry *head;
192
  struct line_entry **ptail;
193
  struct line_entry **pmove_tail;
194
};
195
196
struct line_seg
197
{
198
  struct line_seg *next;
199
  segT seg;
200
  struct line_subseg *head;
201
  symbolS *text_start;
202
  symbolS *text_end;
203
};
204
205
/* Collects data for all line table entries during assembly.  */
206
static struct line_seg *all_segs;
207
static struct line_seg **last_seg_ptr;
208
209
210
#define NUM_MD5_BYTES       16
210
211
struct file_entry
212
{
213
  const char *   filename;
214
  unsigned int   dir;
215
  unsigned char  md5[NUM_MD5_BYTES];
216
};
217
218
/* Table of files used by .debug_line.  */
219
static struct file_entry *files;
220
static unsigned int files_in_use;
221
static unsigned int files_allocated;
222
223
/* Table of directories used by .debug_line.  */
224
static char **       dirs;
225
static unsigned int  dirs_in_use;
226
static unsigned int  dirs_allocated;
227
228
/* TRUE when we've seen a .loc directive recently.  Used to avoid
229
   doing work when there's nothing to do.  Will be reset by
230
   dwarf2_consume_line_info.  */
231
bool dwarf2_loc_directive_seen;
232
233
/* TRUE when we've seen any .loc directive at any time during parsing.
234
   Indicates the user wants us to generate a .debug_line section.
235
   Used in dwarf2_finish as sanity check.  */
236
static bool dwarf2_any_loc_directive_seen;
237
238
/* TRUE when we're supposed to set the basic block mark whenever a
239
   label is seen.  */
240
bool dwarf2_loc_mark_labels;
241
242
/* Current location as indicated by the most recent .loc directive.  */
243
static struct dwarf2_line_info current;
244
245
/* This symbol is used to recognize view number forced resets in loc
246
   lists.  */
247
static symbolS *force_reset_view;
248
249
/* This symbol evaluates to an expression that, if nonzero, indicates
250
   some view assert check failed.  */
251
static symbolS *view_assert_failed;
252
253
/* The size of an address on the target.  */
254
static unsigned int sizeof_address;
255

256
#ifndef TC_DWARF2_EMIT_OFFSET
257
330
#define TC_DWARF2_EMIT_OFFSET  generic_dwarf2_emit_offset
258
259
/* Create an offset to .dwarf2_*.  */
260
261
static void
262
generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
263
330
{
264
330
  expressionS exp;
265
266
330
  memset (&exp, 0, sizeof exp);
267
330
  exp.X_op = O_symbol;
268
330
  exp.X_add_symbol = symbol;
269
330
  exp.X_add_number = 0;
270
330
  emit_expr (&exp, size);
271
330
}
272
#endif
273
274
/* Find or create (if CREATE_P) an entry for SEG+SUBSEG in ALL_SEGS.  */
275
276
static struct line_subseg *
277
get_line_subseg (segT seg, subsegT subseg, bool create_p)
278
810
{
279
810
  struct line_seg *s = seg_info (seg)->dwarf2_line_seg;
280
810
  struct line_subseg **pss, *lss;
281
282
810
  if (s == NULL)
283
38
    {
284
38
      if (!create_p)
285
0
  return NULL;
286
287
38
      s = XNEW (struct line_seg);
288
38
      s->next = NULL;
289
38
      s->seg = seg;
290
38
      s->head = NULL;
291
38
      *last_seg_ptr = s;
292
38
      last_seg_ptr = &s->next;
293
38
      seg_info (seg)->dwarf2_line_seg = s;
294
38
    }
295
296
810
  gas_assert (seg == s->seg);
297
298
813
  for (pss = &s->head; (lss = *pss) != NULL ; pss = &lss->next)
299
774
    {
300
774
      if (lss->subseg == subseg)
301
770
  goto found_subseg;
302
4
      if (lss->subseg > subseg)
303
1
  break;
304
4
    }
305
306
40
  lss = XNEW (struct line_subseg);
307
40
  lss->next = *pss;
308
40
  lss->subseg = subseg;
309
40
  lss->head = NULL;
310
40
  lss->ptail = &lss->head;
311
40
  lss->pmove_tail = &lss->head;
312
40
  *pss = lss;
313
314
810
 found_subseg:
315
810
  return lss;
316
40
}
317
318
/* (Un)reverse the line_entry list starting from H.  */
319
320
static struct line_entry *
321
reverse_line_entry_list (struct line_entry *h)
322
30
{
323
30
  struct line_entry *p = NULL, *e, *n;
324
325
270
  for (e = h; e; e = n)
326
240
    {
327
240
      n = e->next;
328
240
      e->next = p;
329
240
      p = e;
330
240
    }
331
30
  return p;
332
30
}
333
334
/* Compute the view for E based on the previous entry P.  If we
335
   introduce an (undefined) view symbol for P, and H is given (P must
336
   be the tail in this case), introduce view symbols for earlier list
337
   entries as well, until one of them is constant.  */
338
339
static void
340
set_or_check_view (struct line_entry *e, struct line_entry *p,
341
       struct line_entry *h)
342
340
{
343
340
  expressionS viewx;
344
345
340
  memset (&viewx, 0, sizeof (viewx));
346
340
  viewx.X_unsigned = 1;
347
348
  /* First, compute !(E->label > P->label), to tell whether or not
349
     we're to reset the view number.  If we can't resolve it to a
350
     constant, keep it symbolic.  */
351
340
  if (!p || (e->loc.u.view == force_reset_view && force_reset_view))
352
303
    {
353
303
      viewx.X_op = O_constant;
354
303
      viewx.X_add_number = 0;
355
303
      viewx.X_add_symbol = NULL;
356
303
      viewx.X_op_symbol = NULL;
357
303
    }
358
37
  else
359
37
    {
360
37
      viewx.X_op = O_gt;
361
37
      viewx.X_add_number = 0;
362
37
      viewx.X_add_symbol = e->label;
363
37
      viewx.X_op_symbol = p->label;
364
37
      resolve_expression (&viewx);
365
37
      if (viewx.X_op == O_constant)
366
35
  viewx.X_add_number = !viewx.X_add_number;
367
2
      else
368
2
  {
369
2
    viewx.X_add_symbol = make_expr_symbol (&viewx);
370
2
    viewx.X_add_number = 0;
371
2
    viewx.X_op_symbol = NULL;
372
2
    viewx.X_op = O_logical_not;
373
2
  }
374
37
    }
375
376
340
  if (S_IS_DEFINED (e->loc.u.view) && symbol_constant_p (e->loc.u.view))
377
300
    {
378
300
      expressionS *value = symbol_get_value_expression (e->loc.u.view);
379
      /* We can't compare the view numbers at this point, because in
380
   VIEWX we've only determined whether we're to reset it so
381
   far.  */
382
300
      if (viewx.X_op == O_constant)
383
299
  {
384
299
    if (!value->X_add_number != !viewx.X_add_number)
385
7
      as_bad (_("view number mismatch"));
386
299
  }
387
      /* Record the expression to check it later.  It is the result of
388
   a logical not, thus 0 or 1.  We just add up all such deferred
389
   expressions, and resolve it at the end.  */
390
1
      else if (!value->X_add_number)
391
0
  {
392
0
    symbolS *deferred = make_expr_symbol (&viewx);
393
0
    if (view_assert_failed)
394
0
      {
395
0
        expressionS chk;
396
397
0
        memset (&chk, 0, sizeof (chk));
398
0
        chk.X_unsigned = 1;
399
0
        chk.X_op = O_add;
400
0
        chk.X_add_number = 0;
401
0
        chk.X_add_symbol = view_assert_failed;
402
0
        chk.X_op_symbol = deferred;
403
0
        deferred = make_expr_symbol (&chk);
404
0
      }
405
0
    view_assert_failed = deferred;
406
0
  }
407
300
    }
408
409
340
  if (viewx.X_op != O_constant || viewx.X_add_number)
410
31
    {
411
31
      expressionS incv;
412
31
      expressionS *p_view;
413
414
31
      if (!p->loc.u.view)
415
13
  p->loc.u.view = symbol_temp_make ();
416
417
31
      memset (&incv, 0, sizeof (incv));
418
31
      incv.X_unsigned = 1;
419
31
      incv.X_op = O_symbol;
420
31
      incv.X_add_symbol = p->loc.u.view;
421
31
      incv.X_add_number = 1;
422
31
      p_view = symbol_get_value_expression (p->loc.u.view);
423
31
      if (p_view->X_op == O_constant || p_view->X_op == O_symbol)
424
31
  {
425
    /* If we can, constant fold increments so that a chain of
426
       expressions v + 1 + 1 ... + 1 is not created.
427
       resolve_expression isn't ideal for this purpose.  The
428
       base v might not be resolvable until later.  */
429
31
    incv.X_op = p_view->X_op;
430
31
    incv.X_add_symbol = p_view->X_add_symbol;
431
31
    incv.X_add_number = p_view->X_add_number + 1;
432
31
  }
433
434
31
      if (viewx.X_op == O_constant)
435
29
  {
436
29
    gas_assert (viewx.X_add_number == 1);
437
29
    viewx = incv;
438
29
  }
439
2
      else
440
2
  {
441
2
    viewx.X_add_symbol = make_expr_symbol (&viewx);
442
2
    viewx.X_add_number = 0;
443
2
    viewx.X_op_symbol = make_expr_symbol (&incv);
444
2
    viewx.X_op = O_multiply;
445
2
  }
446
31
    }
447
448
340
  if (!S_IS_DEFINED (e->loc.u.view))
449
40
    {
450
40
      symbol_set_value_expression (e->loc.u.view, &viewx);
451
40
      S_SET_SEGMENT (e->loc.u.view, expr_section);
452
40
      symbol_set_frag (e->loc.u.view, &zero_address_frag);
453
40
    }
454
455
  /* Define and attempt to simplify any earlier views needed to
456
     compute E's.  */
457
340
  if (h && p && p->loc.u.view && !S_IS_DEFINED (p->loc.u.view))
458
15
    {
459
15
      struct line_entry *h2;
460
      /* Reverse the list to avoid quadratic behavior going backwards
461
   in a single-linked list.  */
462
15
      struct line_entry *r = reverse_line_entry_list (h);
463
464
15
      gas_assert (r == p);
465
      /* Set or check views until we find a defined or absent view.  */
466
15
      do
467
17
  {
468
    /* Do not define the head of a (sub?)segment view while
469
       handling others.  It would be defined too early, without
470
       regard to the last view of other subsegments.
471
       set_or_check_view will be called for every head segment
472
       that needs it.  */
473
17
    if (r == h)
474
6
      break;
475
11
    set_or_check_view (r, r->next, NULL);
476
11
  }
477
15
      while (r->next
478
11
       && r->next->loc.u.view
479
6
       && !S_IS_DEFINED (r->next->loc.u.view)
480
2
       && (r = r->next));
481
482
      /* Unreverse the list, so that we can go forward again.  */
483
15
      h2 = reverse_line_entry_list (p);
484
15
      gas_assert (h2 == h);
485
486
      /* Starting from the last view we just defined, attempt to
487
   simplify the view expressions, until we do so to P.  */
488
15
      do
489
17
  {
490
    /* The head view of a subsegment may remain undefined while
491
       handling other elements, before it is linked to the last
492
       view of the previous subsegment.  */
493
17
    if (r == h)
494
6
      continue;
495
11
    gas_assert (S_IS_DEFINED (r->loc.u.view));
496
11
    resolve_expression (symbol_get_value_expression (r->loc.u.view));
497
11
  }
498
17
      while (r != p && (r = r->next));
499
500
      /* Now that we've defined and computed all earlier views that might
501
   be needed to compute E's, attempt to simplify it.  */
502
15
      resolve_expression (symbol_get_value_expression (e->loc.u.view));
503
15
    }
504
340
}
505
506
/* Record an entry for LOC occurring at LABEL.  */
507
508
static void
509
dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
510
810
{
511
810
  struct line_subseg *lss;
512
810
  struct line_entry *e;
513
810
  flagword need_flags = SEC_LOAD | SEC_CODE;
514
515
  /* PR 26850: Do not record LOCs in non-executable or non-loaded
516
     sections.  SEC_ALLOC isn't tested for non-ELF because obj-coff.c
517
     obj_coff_section is careless in setting SEC_ALLOC.  */
518
810
  if (IS_ELF)
519
810
    need_flags |= SEC_ALLOC;
520
810
  if ((now_seg->flags & need_flags) != need_flags)
521
0
    {
522
      /* FIXME: Add code to suppress multiple warnings ?  */
523
0
      if (debug_type != DEBUG_DWARF2)
524
0
  as_warn ("dwarf line number information for %s ignored",
525
0
     segment_name (now_seg));
526
0
      return;
527
0
    }
528
529
810
  e = XNEW (struct line_entry);
530
810
  e->next = NULL;
531
810
  e->label = label;
532
810
  e->loc = *loc;
533
534
810
  lss = get_line_subseg (now_seg, now_subseg, true);
535
536
  /* Subseg heads are chained to previous subsegs in
537
     dwarf2_finish.  */
538
810
  if (loc->filenum != -1u && loc->u.view && lss->head)
539
311
    set_or_check_view (e, line_entry_at_tail (lss->head, lss->ptail),
540
311
           lss->head);
541
542
810
  *lss->ptail = e;
543
810
  lss->ptail = &e->next;
544
810
}
545
546
/* Record an entry for LOC occurring at OFS within the current fragment.  */
547
548
static unsigned int dw2_line;
549
static const char *dw2_filename;
550
static int label_num;
551
552
void
553
dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
554
1.00k
{
555
1.00k
  symbolS *sym;
556
557
  /* Early out for as-yet incomplete location information.  */
558
1.00k
  if (loc->line == 0)
559
196
    return;
560
809
  if (loc->filenum == 0)
561
797
    {
562
797
      if (dwarf_level < 5)
563
0
  dwarf_level = 5;
564
797
      if (DWARF2_LINE_VERSION < 5)
565
0
  return;
566
797
    }
567
568
  /* Don't emit sequences of line symbols for the same line when the
569
     symbols apply to assembler code.  It is necessary to emit
570
     duplicate line symbols when a compiler asks for them, because GDB
571
     uses them to determine the end of the prologue.  */
572
809
  if (debug_type == DEBUG_DWARF2)
573
0
    {
574
0
      if (dw2_line == loc->line)
575
0
  {
576
0
    if (dw2_filename == loc->u.filename)
577
0
      return;
578
0
    if (filename_cmp (dw2_filename, loc->u.filename) == 0)
579
0
      {
580
0
        dw2_filename = loc->u.filename;
581
0
        return;
582
0
      }
583
0
  }
584
585
0
      dw2_line = loc->line;
586
0
      dw2_filename = loc->u.filename;
587
0
    }
588
589
809
  if (linkrelax)
590
0
    {
591
0
      char name[32];
592
593
      /* Use a non-fake name for the line number location,
594
   so that it can be referred to by relocations.  */
595
0
      sprintf (name, ".Loc.%u", label_num);
596
0
      label_num++;
597
0
      sym = symbol_new (name, now_seg, frag_now, ofs);
598
0
    }
599
809
  else
600
809
    sym = symbol_temp_new (now_seg, frag_now, ofs);
601
809
  dwarf2_gen_line_info_1 (sym, loc);
602
809
}
603
604
static const char *
605
get_basename (const char * pathname)
606
256
{
607
256
  const char * file;
608
609
256
  file = lbasename (pathname);
610
  /* Don't make empty string from / or A: from A:/ .  */
611
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
612
  if (file <= pathname + 3)
613
    file = pathname;
614
#else
615
256
  if (file == pathname + 1)
616
3
    file = pathname;
617
256
#endif
618
256
  return file;
619
256
}
620
621
static unsigned int
622
get_directory_table_entry (const char *dirname,
623
         const char *file0_dirname,
624
         size_t dirlen,
625
         bool can_use_zero)
626
159
{
627
159
  unsigned int d;
628
629
159
  if (dirlen == 0)
630
61
    return 0;
631
632
98
#ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR
633
98
  if (IS_DIR_SEPARATOR (dirname[dirlen - 1]))
634
62
    {
635
62
      -- dirlen;
636
62
      if (dirlen == 0)
637
0
  return 0;
638
62
    }
639
98
#endif
640
641
240
  for (d = 0; d < dirs_in_use; ++d)
642
154
    {
643
154
      if (dirs[d] != NULL
644
120
    && filename_ncmp (dirname, dirs[d], dirlen) == 0
645
12
    && dirs[d][dirlen] == '\0')
646
12
  return d;
647
154
    }
648
649
86
  if (can_use_zero)
650
50
    {
651
50
      if (dirs == NULL || dirs[0] == NULL)
652
50
  {
653
50
    const char * pwd = file0_dirname ? file0_dirname : getpwd ();
654
655
50
    if (dwarf_level >= 5 && filename_cmp (dirname, pwd) != 0)
656
16
      {
657
        /* In DWARF-5 the 0 entry in the directory table is
658
     expected to be the same as the DW_AT_comp_dir (which
659
     is set to the current build directory).  Since we are
660
     about to create a directory entry that is not the
661
     same, allocate the current directory first.  */
662
16
        (void) get_directory_table_entry (pwd, pwd, strlen (pwd), true);
663
16
        d = dirs_in_use;
664
16
      }
665
34
    else
666
34
      d = 0;
667
50
  }
668
50
    }
669
36
  else if (d == 0)
670
10
    d = 1;
671
672
86
  if (d >= dirs_allocated)
673
41
    {
674
41
      unsigned int old = dirs_allocated;
675
41
#define DIR_TABLE_INCREMENT 32
676
41
      dirs_allocated = d + DIR_TABLE_INCREMENT;
677
41
      dirs = XRESIZEVEC (char *, dirs, dirs_allocated);
678
41
      memset (dirs + old, 0, (dirs_allocated - old) * sizeof (char *));
679
41
    }
680
681
86
  dirs[d] = xmemdup0 (dirname, dirlen);
682
86
  if (dirs_in_use <= d)
683
82
    dirs_in_use = d + 1;
684
685
86
  return d;  
686
98
}
687
688
static bool
689
assign_file_to_slot (valueT i, const char *file, unsigned int dir)
690
143
{
691
143
  if (i >= files_allocated)
692
137
    {
693
137
      unsigned int want = i + 32;
694
695
      /* If this array is taking 1G or more, someone is using silly
696
   file numbers.  */
697
137
      if (want < i || want > UINT_MAX / 4 / sizeof (struct file_entry))
698
44
  {
699
44
    as_bad (_("file number %" PRIu64 " is too big"), (uint64_t) i);
700
44
    return false;
701
44
  }
702
703
93
      files = XRESIZEVEC (struct file_entry, files, want);
704
93
      memset (files + files_allocated, 0,
705
93
        (want - files_allocated) * sizeof (struct file_entry));
706
93
      files_allocated = want;
707
93
    }
708
709
99
  files[i].filename = file;
710
99
  files[i].dir = dir;
711
99
  memset (files[i].md5, 0, NUM_MD5_BYTES);
712
713
99
  if (files_in_use < i + 1)
714
94
    files_in_use = i + 1;
715
716
99
  return true;
717
143
}
718
719
/* Get a .debug_line file number for PATHNAME.  If there is a
720
   directory component to PATHNAME, then this will be stored
721
   in the directory table, if it is not already present.
722
   Returns the slot number allocated to that filename or -1
723
   if there was a problem.  */
724
725
static int last_used;
726
static int last_used_dir_len;
727
728
static signed int
729
allocate_filenum (const char * pathname)
730
0
{
731
0
  const char *file;
732
0
  size_t dir_len;
733
0
  unsigned int i, dir;
734
735
  /* Short circuit the common case of adding the same pathname
736
     as last time.  */
737
0
  if (last_used != -1)
738
0
    {
739
0
      const char * dirname = NULL;
740
741
0
      if (dirs != NULL)
742
0
  dirname = dirs[files[last_used].dir];
743
744
0
      if (dirname == NULL)
745
0
  {
746
0
    if (filename_cmp (pathname, files[last_used].filename) == 0)
747
0
      return last_used;
748
0
  }
749
0
      else
750
0
  {
751
0
    if (filename_ncmp (pathname, dirname, last_used_dir_len - 1) == 0
752
0
        && IS_DIR_SEPARATOR (pathname [last_used_dir_len - 1])
753
0
        && filename_cmp (pathname + last_used_dir_len,
754
0
             files[last_used].filename) == 0)
755
0
      return last_used;
756
0
  }
757
0
    }
758
759
0
  file = get_basename (pathname);
760
0
  dir_len = file - pathname;
761
762
0
  dir = get_directory_table_entry (pathname, NULL, dir_len, false);
763
764
  /* Do not use slot-0.  That is specifically reserved for use by
765
     the '.file 0 "name"' directive.  */
766
0
  for (i = 1; i < files_in_use; ++i)
767
0
    if (files[i].dir == dir
768
0
  && files[i].filename
769
0
  && filename_cmp (file, files[i].filename) == 0)
770
0
      {
771
0
  last_used = i;
772
0
  last_used_dir_len = dir_len;
773
0
  return i;
774
0
      }
775
776
0
  if (!assign_file_to_slot (i, file, dir))
777
0
    return -1;
778
779
0
  last_used = i;
780
0
  last_used_dir_len = dir_len;
781
782
0
  return i;
783
0
}
784
785
/* Run through the list of line entries starting at E, allocating
786
   file entries for gas generated debug.  */
787
788
static void
789
do_allocate_filenum (struct line_entry *e)
790
40
{
791
40
  do
792
810
    {
793
810
      if (e->loc.filenum == -1u)
794
0
  {
795
0
    e->loc.filenum = allocate_filenum (e->loc.u.filename);
796
0
    e->loc.u.view = NULL;
797
0
  }
798
810
      e = e->next;
799
810
    }
800
810
  while (e);
801
40
}
802
803
/* Remove any generated line entries.  These don't live comfortably
804
   with compiler generated line info.  If THELOT then remove
805
   everything, freeing all list entries we have created.  */
806
807
static void
808
purge_generated_debug (bool thelot)
809
584
{
810
584
  struct line_seg *s, *nexts;
811
812
622
  for (s = all_segs; s; s = nexts)
813
38
    {
814
38
      struct line_subseg *lss, *nextlss;
815
816
78
      for (lss = s->head; lss; lss = nextlss)
817
40
  {
818
40
    struct line_entry *e, *next;
819
820
850
    for (e = lss->head; e; e = next)
821
810
      {
822
810
        if (!thelot)
823
810
    know (e->loc.filenum == -1u);
824
810
        next = e->next;
825
810
        free (e);
826
810
      }
827
828
40
    lss->head = NULL;
829
40
    lss->ptail = &lss->head;
830
40
    lss->pmove_tail = &lss->head;
831
40
    nextlss = lss->next;
832
40
    if (thelot)
833
40
      free (lss);
834
40
  }
835
38
      nexts = s->next;
836
38
      if (thelot)
837
38
  {
838
38
    seg_info (s->seg)->dwarf2_line_seg = NULL;
839
38
    free (s);
840
38
  }
841
38
    }
842
584
}
843
844
/* Allocate slot NUM in the .debug_line file table to FILENAME.
845
   If DIRNAME is not NULL or there is a directory component to FILENAME
846
   then this will be stored in the directory table, if not already present.
847
   if WITH_MD5 is TRUE then there is a md5 value in generic_bignum.
848
   Returns TRUE if allocation succeeded, FALSE otherwise.  */
849
850
static bool
851
allocate_filename_to_slot (const char *dirname,
852
         const char *filename,
853
         valueT num,
854
         bool with_md5)
855
682
{
856
682
  const char *file;
857
682
  size_t dirlen;
858
682
  unsigned int i, d;
859
682
  const char *file0_dirname;
860
861
  /* Short circuit the common case of adding the same pathname
862
     as last time.  */
863
682
  if (num < files_allocated && files[num].filename != NULL)
864
539
    {
865
539
      const char * dir = NULL;
866
867
539
      if (dirs != NULL)
868
422
  dir = dirs[files[num].dir];
869
870
539
      if (with_md5
871
12
    && memcmp (generic_bignum, files[num].md5, NUM_MD5_BYTES) != 0)
872
3
  goto fail;
873
874
536
      if (dirname != NULL)
875
142
  {
876
142
    if (dir != NULL && filename_cmp (dir, dirname) != 0)
877
95
      goto fail;
878
      
879
47
    if (filename_cmp (filename, files[num].filename) != 0)
880
24
      goto fail;
881
882
    /* If the filenames match, but the directory table entry was
883
       empty, then fill it with the provided directory name.  */
884
23
    if (dir == NULL)
885
0
      {
886
0
        if (dirs == NULL)
887
0
    {
888
0
      dirs_allocated = files[num].dir + DIR_TABLE_INCREMENT;
889
0
      dirs = XCNEWVEC (char *, dirs_allocated);
890
0
    }
891
        
892
0
        dirs[files[num].dir] = xmemdup0 (dirname, strlen (dirname));
893
0
        if (dirs_in_use <= files[num].dir)
894
0
    dirs_in_use = files[num].dir + 1;
895
0
      }
896
      
897
23
    return true;
898
47
  }
899
394
      else if (dir != NULL) 
900
279
  {
901
279
    dirlen = strlen (dir);
902
279
    if (filename_ncmp (filename, dir, dirlen) == 0
903
279
        && IS_DIR_SEPARATOR (filename [dirlen])
904
152
        && filename_cmp (filename + dirlen + 1, files[num].filename) == 0)
905
150
      return true;
906
279
  }
907
115
      else /* dir == NULL  */
908
115
  {
909
115
    file = get_basename (filename);
910
115
    if (filename_cmp (file, files[num].filename) == 0)
911
75
      {
912
        /* The filenames match, but the directory table entry is empty.
913
     Fill it with the provided directory name.  */
914
75
        if (file > filename)
915
0
    {
916
0
      if (dirs == NULL)
917
0
        {
918
0
          dirs_allocated = files[num].dir + DIR_TABLE_INCREMENT;
919
0
          dirs = XCNEWVEC (char *, dirs_allocated);
920
0
        }
921
922
0
      dirs[files[num].dir] = xmemdup0 (filename, file - filename);
923
0
      if (dirs_in_use <= files[num].dir)
924
0
        dirs_in_use = files[num].dir + 1;
925
0
    }
926
75
        return true;
927
75
      }
928
115
  }
929
930
291
    fail:
931
291
      as_bad (_("file table slot %u is already occupied by a different file"
932
291
    " (%s%s%s vs %s%s%s)"),
933
291
        (unsigned int) num,
934
291
        dir == NULL ? "" : dir,
935
291
        dir == NULL ? "" : "/",
936
291
        files[num].filename,
937
291
        dirname == NULL ? "" : dirname,
938
291
        dirname == NULL ? "" : "/",
939
291
        filename);
940
291
      return false;
941
536
    }
942
943
  /* For file .0, the directory name is the current directory and the file
944
     may be in another directory contained in the file name.  */
945
143
  if (num == 0)
946
80
    {
947
80
      file0_dirname = dirname;
948
949
80
      file = get_basename (filename);
950
951
80
      if (dirname && file == filename)
952
21
  dirlen = strlen (dirname);
953
59
      else
954
59
  {
955
59
    dirname = filename;
956
59
    dirlen = file - filename;
957
59
  }
958
80
    }
959
63
  else
960
63
    {
961
63
      file0_dirname = NULL;
962
963
63
      if (dirname == NULL)
964
61
  {
965
61
    dirname = filename;
966
61
    file = get_basename (filename);
967
61
    dirlen = file - filename;
968
61
  }
969
2
      else
970
2
  {
971
2
    dirlen = strlen (dirname);
972
2
    file = filename;
973
2
  }
974
63
    }
975
976
143
  d = get_directory_table_entry (dirname, file0_dirname, dirlen, num == 0);
977
143
  i = num;
978
979
143
  if (!assign_file_to_slot (num, file, d))
980
44
    return false;
981
982
99
  if (with_md5)
983
1
    {
984
1
      if (target_big_endian)
985
0
  {
986
    /* md5's are stored in litte endian format.  */
987
0
    unsigned int     bits_remaining = NUM_MD5_BYTES * BITS_PER_CHAR;
988
0
    unsigned int     byte = NUM_MD5_BYTES;
989
0
    unsigned int     bignum_index = 0;
990
991
0
    while (bits_remaining)
992
0
      {
993
0
        unsigned int bignum_bits_remaining = LITTLENUM_NUMBER_OF_BITS;
994
0
        valueT       bignum_value = generic_bignum [bignum_index];
995
0
        bignum_index ++;
996
997
0
        while (bignum_bits_remaining)
998
0
    {
999
0
      files[i].md5[--byte] = bignum_value & 0xff;
1000
0
      bignum_value >>= 8;
1001
0
      bignum_bits_remaining -= 8;
1002
0
      bits_remaining -= 8;
1003
0
    }
1004
0
      }
1005
0
  }
1006
1
      else
1007
1
  {
1008
1
    unsigned int     bits_remaining = NUM_MD5_BYTES * BITS_PER_CHAR;
1009
1
    unsigned int     byte = 0;
1010
1
    unsigned int     bignum_index = 0;
1011
1012
9
    while (bits_remaining)
1013
8
      {
1014
8
        unsigned int bignum_bits_remaining = LITTLENUM_NUMBER_OF_BITS;
1015
8
        valueT       bignum_value = generic_bignum [bignum_index];
1016
1017
8
        bignum_index ++;
1018
1019
24
        while (bignum_bits_remaining)
1020
16
    {
1021
16
      files[i].md5[byte++] = bignum_value & 0xff;
1022
16
      bignum_value >>= 8;
1023
16
      bignum_bits_remaining -= 8;
1024
16
      bits_remaining -= 8;
1025
16
    }
1026
8
      }
1027
1
  }
1028
1
    }
1029
98
  else
1030
98
    memset (files[i].md5, 0, NUM_MD5_BYTES);
1031
1032
99
  return true;
1033
143
}
1034
1035
/* Returns the current source information.  If .file directives have
1036
   been encountered, the info for the corresponding source file is
1037
   returned.  Otherwise, the info for the assembly source file is
1038
   returned.  */
1039
1040
void
1041
dwarf2_where (struct dwarf2_line_info *line)
1042
1.00k
{
1043
1.00k
  if (debug_type == DEBUG_DWARF2)
1044
0
    {
1045
0
      line->u.filename = as_where (&line->line);
1046
0
      line->filenum = -1u;
1047
0
      line->column = 0;
1048
0
      line->flags = DWARF2_FLAG_IS_STMT;
1049
0
      line->isa = current.isa;
1050
0
      line->discriminator = current.discriminator;
1051
0
    }
1052
1.00k
  else
1053
1.00k
    *line = current;
1054
1.00k
}
1055
1056
/* A hook to allow the target backend to inform the line number state
1057
   machine of isa changes when assembler debug info is enabled.  */
1058
1059
void
1060
dwarf2_set_isa (unsigned int isa)
1061
0
{
1062
0
  current.isa = isa;
1063
0
}
1064
1065
/* Called for each machine instruction, or relatively atomic group of
1066
   machine instructions (ie built-in macro).  The instruction or group
1067
   is SIZE bytes in length.  If dwarf2 line number generation is called
1068
   for, emit a line statement appropriately.  */
1069
1070
void
1071
dwarf2_emit_insn (int size)
1072
7.71k
{
1073
7.71k
  struct dwarf2_line_info loc;
1074
1075
7.71k
  seg_info (now_seg)->insn_seen = 1;
1076
1077
7.71k
  if (debug_type != DEBUG_DWARF2
1078
7.71k
      ? !dwarf2_loc_directive_seen
1079
7.71k
      : !seen_at_least_1_file ())
1080
6.70k
    return;
1081
1082
1.00k
  dwarf2_where (&loc);
1083
1084
1.00k
  dwarf2_gen_line_info ((frag_now_fix_octets () - size) / OCTETS_PER_BYTE, &loc);
1085
1.00k
  dwarf2_consume_line_info ();
1086
1.00k
}
1087
1088
/* Move all previously-emitted line entries for the current position by
1089
   DELTA bytes.  This function cannot be used to move the same entries
1090
   twice.  */
1091
1092
void
1093
dwarf2_move_insn (int delta)
1094
0
{
1095
0
  struct line_subseg *lss;
1096
0
  struct line_entry *e;
1097
0
  valueT now;
1098
1099
0
  if (delta == 0)
1100
0
    return;
1101
1102
0
  lss = get_line_subseg (now_seg, now_subseg, false);
1103
0
  if (!lss)
1104
0
    return;
1105
1106
0
  now = frag_now_fix ();
1107
0
  while ((e = *lss->pmove_tail))
1108
0
    {
1109
0
      if (S_GET_VALUE (e->label) == now)
1110
0
  S_SET_VALUE (e->label, now + delta);
1111
0
      lss->pmove_tail = &e->next;
1112
0
    }
1113
0
}
1114
1115
/* Called after the current line information has been either used with
1116
   dwarf2_gen_line_info or saved with a machine instruction for later use.
1117
   This resets the state of the line number information to reflect that
1118
   it has been used.  */
1119
1120
void
1121
dwarf2_consume_line_info (void)
1122
1.00k
{
1123
  /* Unless we generate DWARF2 debugging information for each
1124
     assembler line, we only emit one line symbol for one LOC.  */
1125
1.00k
  dwarf2_loc_directive_seen = false;
1126
1127
1.00k
  current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
1128
1.00k
         | DWARF2_FLAG_PROLOGUE_END
1129
1.00k
         | DWARF2_FLAG_EPILOGUE_BEGIN);
1130
1.00k
  current.discriminator = 0;
1131
1.00k
  current.u.view = NULL;
1132
1.00k
}
1133
1134
/* Called for each (preferably code) label.  If dwarf2_loc_mark_labels
1135
   is enabled, emit a basic block marker.  */
1136
1137
void
1138
dwarf2_emit_label (symbolS *label)
1139
4.36k
{
1140
4.36k
  struct dwarf2_line_info loc;
1141
1142
4.36k
  if (!dwarf2_loc_mark_labels)
1143
4.28k
    return;
1144
80
  if (S_GET_SEGMENT (label) != now_seg)
1145
15
    return;
1146
65
  if (!(bfd_section_flags (now_seg) & SEC_CODE))
1147
0
    return;
1148
65
  if (files_in_use == 0 && debug_type != DEBUG_DWARF2)
1149
64
    return;
1150
1151
1
  dwarf2_where (&loc);
1152
1153
1
  loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
1154
1155
1
  dwarf2_gen_line_info_1 (label, &loc);
1156
1
  dwarf2_consume_line_info ();
1157
1
}
1158
1159
/* Handle two forms of .file directive:
1160
   - Pass .file "source.c" to s_file
1161
   - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
1162
1163
   If an entry is added to the file table, return a pointer to the filename.  */
1164
1165
char *
1166
dwarf2_directive_filename (void)
1167
1.29k
{
1168
1.29k
  bool with_md5 = false;
1169
1.29k
  valueT num;
1170
1.29k
  char *filename;
1171
1.29k
  const char * dirname = NULL;
1172
1.29k
  int filename_len;
1173
1174
  /* Continue to accept a bare string and pass it off.  */
1175
1.29k
  SKIP_WHITESPACE ();
1176
1.29k
  if (*input_line_pointer == '"')
1177
411
    {
1178
411
      s_file (0);
1179
411
      return NULL;
1180
411
    }
1181
1182
882
  num = get_absolute_expression ();
1183
1184
882
  if ((offsetT) num < 1)
1185
782
    {
1186
782
      if (num == 0 && dwarf_level < 5)
1187
1
  dwarf_level = 5;
1188
782
      if ((offsetT) num < 0 || DWARF2_LINE_VERSION < 5)
1189
2
  {
1190
2
    as_bad (_("file number less than one"));
1191
2
    ignore_rest_of_line ();
1192
2
    return NULL;
1193
2
  }
1194
782
    }
1195
1196
  /* FIXME: Should we allow ".file <N>\n" as an expression meaning
1197
     "switch back to the already allocated file <N> as the current
1198
     file" ?  */
1199
1200
880
  filename = demand_copy_C_string (&filename_len);
1201
880
  if (filename == NULL)
1202
197
    {
1203
      /* demand_copy_C_string will have already generated an error.  */
1204
197
      ignore_rest_of_line ();
1205
197
      return NULL;
1206
197
    }
1207
1208
  /* For DWARF-5 support we also accept:
1209
     .file <NUM> ["<dir>"] "<file>" [md5 <NUM>]  */
1210
683
  if (DWARF2_LINE_VERSION > 4)
1211
683
    {
1212
683
      SKIP_WHITESPACE ();
1213
683
      if (*input_line_pointer == '"')
1214
171
  {
1215
171
    dirname = filename;
1216
171
    filename = demand_copy_C_string (&filename_len);
1217
171
    if (filename == NULL)
1218
1
      {
1219
1
        ignore_rest_of_line ();
1220
1
        return NULL;
1221
1
      }
1222
170
    SKIP_WHITESPACE ();
1223
170
  }
1224
1225
682
      if (startswith (input_line_pointer, "md5"))
1226
14
  {
1227
14
    input_line_pointer += 3;
1228
14
    SKIP_WHITESPACE ();
1229
1230
14
    expressionS exp;
1231
14
    expression_and_evaluate (& exp);
1232
14
    if (exp.X_op != O_big)
1233
1
      as_bad (_("md5 value too small or not a constant"));
1234
13
    else
1235
13
      with_md5 = true;
1236
14
  }
1237
682
    }
1238
1239
682
  demand_empty_rest_of_line ();
1240
1241
  /* A .file directive implies compiler generated debug information is
1242
     being supplied.  Turn off gas generated debug info.  */
1243
682
  if (debug_type == DEBUG_DWARF2)
1244
0
    purge_generated_debug (false);
1245
682
  debug_type = DEBUG_NONE;
1246
1247
682
  if (!allocate_filename_to_slot (dirname, filename, num, with_md5))
1248
335
    return NULL;
1249
1250
347
  return filename;
1251
682
}
1252
1253
/* Calls dwarf2_directive_filename, but discards its result.
1254
   Used in pseudo-op tables where the function result is ignored.  */
1255
1256
void
1257
dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
1258
1.29k
{
1259
1.29k
  (void) dwarf2_directive_filename ();
1260
1.29k
}
1261
1262
void
1263
dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
1264
5.94k
{
1265
5.94k
  offsetT filenum, line;
1266
1267
  /* If we see two .loc directives in a row, force the first one to be
1268
     output now.  */
1269
5.94k
  if (dwarf2_loc_directive_seen)
1270
659
    dwarf2_emit_insn (0);
1271
1272
5.94k
  filenum = get_absolute_expression ();
1273
5.94k
  SKIP_WHITESPACE ();
1274
5.94k
  line = get_absolute_expression ();
1275
1276
5.94k
  if (filenum < 1)
1277
5.76k
    {
1278
5.76k
      if (filenum == 0 && dwarf_level < 5)
1279
0
  dwarf_level = 5;
1280
5.76k
      if (filenum < 0 || DWARF2_LINE_VERSION < 5)
1281
481
  {
1282
481
    as_bad (_("file number less than one"));
1283
481
    return;
1284
481
  }
1285
5.76k
    }
1286
1287
5.46k
  if ((valueT) filenum >= files_in_use || files[filenum].filename == NULL)
1288
4.25k
    {
1289
4.25k
      as_bad (_("unassigned file number %ld"), (long) filenum);
1290
4.25k
      return;
1291
4.25k
    }
1292
1293
  /* debug_type will be turned off by dwarf2_directive_filename, and
1294
     if we don't have a dwarf style .file then files_in_use will be
1295
     zero and the above error will trigger.  */
1296
1.20k
  gas_assert (debug_type == DEBUG_NONE);
1297
1298
1.20k
  current.filenum = filenum;
1299
1.20k
  current.line = line;
1300
1.20k
  current.discriminator = 0;
1301
1302
1.20k
#ifndef NO_LISTING
1303
1.20k
  if (listing)
1304
0
    {
1305
0
      if (files[filenum].dir)
1306
0
  {
1307
0
    size_t dir_len = strlen (dirs[files[filenum].dir]);
1308
0
    size_t file_len = strlen (files[filenum].filename);
1309
0
    char *cp = XNEWVEC (char, dir_len + 1 + file_len + 1);
1310
1311
0
    memcpy (cp, dirs[files[filenum].dir], dir_len);
1312
0
    INSERT_DIR_SEPARATOR (cp, dir_len);
1313
0
    memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
1314
0
    cp[dir_len + file_len + 1] = '\0';
1315
0
    listing_source_file (cp);
1316
0
    free (cp);
1317
0
  }
1318
0
      else
1319
0
  listing_source_file (files[filenum].filename);
1320
0
      listing_source_line (line);
1321
0
    }
1322
1.20k
#endif
1323
1324
1.20k
  SKIP_WHITESPACE ();
1325
1.20k
  if (ISDIGIT (*input_line_pointer))
1326
191
    {
1327
191
      current.column = get_absolute_expression ();
1328
191
      SKIP_WHITESPACE ();
1329
191
    }
1330
1331
1.20k
  while (ISALPHA (*input_line_pointer))
1332
541
    {
1333
541
      char *p, c;
1334
541
      offsetT value;
1335
1336
541
      c = get_symbol_name (& p);
1337
1338
541
      if (strcmp (p, "basic_block") == 0)
1339
4
  {
1340
4
    current.flags |= DWARF2_FLAG_BASIC_BLOCK;
1341
4
    restore_line_pointer (c);
1342
4
  }
1343
537
      else if (strcmp (p, "prologue_end") == 0)
1344
0
  {
1345
0
    if (dwarf_level < 3)
1346
0
      dwarf_level = 3;
1347
0
    current.flags |= DWARF2_FLAG_PROLOGUE_END;
1348
0
    restore_line_pointer (c);
1349
0
  }
1350
537
      else if (strcmp (p, "epilogue_begin") == 0)
1351
11
  {
1352
11
    if (dwarf_level < 3)
1353
0
      dwarf_level = 3;
1354
11
    current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
1355
11
    restore_line_pointer (c);
1356
11
  }
1357
526
      else if (strcmp (p, "is_stmt") == 0)
1358
3
  {
1359
3
    (void) restore_line_pointer (c);
1360
3
    value = get_absolute_expression ();
1361
3
    if (value == 0)
1362
2
      current.flags &= ~DWARF2_FLAG_IS_STMT;
1363
1
    else if (value == 1)
1364
1
      current.flags |= DWARF2_FLAG_IS_STMT;
1365
0
    else
1366
0
      {
1367
0
        as_bad (_("is_stmt value not 0 or 1"));
1368
0
        return;
1369
0
      }
1370
3
  }
1371
523
      else if (strcmp (p, "isa") == 0)
1372
9
  {
1373
9
    if (dwarf_level < 3)
1374
0
      dwarf_level = 3;
1375
9
    (void) restore_line_pointer (c);
1376
9
    value = get_absolute_expression ();
1377
9
    if (value >= 0)
1378
4
      current.isa = value;
1379
5
    else
1380
5
      {
1381
5
        as_bad (_("isa number less than zero"));
1382
5
        return;
1383
5
      }
1384
9
  }
1385
514
      else if (strcmp (p, "discriminator") == 0)
1386
6
  {
1387
6
    (void) restore_line_pointer (c);
1388
6
    value = get_absolute_expression ();
1389
6
    if (value >= 0)
1390
2
      current.discriminator = value;
1391
4
    else
1392
4
      {
1393
4
        as_bad (_("discriminator less than zero"));
1394
4
        return;
1395
4
      }
1396
6
  }
1397
508
      else if (strcmp (p, "view") == 0)
1398
411
  {
1399
411
    symbolS *sym;
1400
1401
411
    (void) restore_line_pointer (c);
1402
411
    SKIP_WHITESPACE ();
1403
1404
411
    if (ISDIGIT (*input_line_pointer)
1405
406
        || *input_line_pointer == '-')
1406
298
      {
1407
298
        bool force_reset = *input_line_pointer == '-';
1408
1409
298
        value = get_absolute_expression ();
1410
298
        if (value != 0)
1411
1
    {
1412
1
      as_bad (_("numeric view can only be asserted to zero"));
1413
1
      return;
1414
1
    }
1415
297
        if (force_reset && force_reset_view)
1416
284
    sym = force_reset_view;
1417
13
        else
1418
13
    {
1419
13
      sym = symbol_temp_new (absolute_section, &zero_address_frag,
1420
13
           value);
1421
13
      if (force_reset)
1422
8
        force_reset_view = sym;
1423
13
    }
1424
297
      }
1425
113
    else
1426
113
      {
1427
113
        char *name = read_symbol_name ();
1428
1429
113
        if (!name)
1430
30
    return;
1431
83
        sym = symbol_find_or_make (name);
1432
83
        free (name);
1433
83
        if (S_IS_DEFINED (sym) || symbol_equated_p (sym))
1434
53
    {
1435
53
      if (S_IS_VOLATILE (sym))
1436
5
        sym = symbol_clone (sym, 1);
1437
48
      else if (!S_CAN_BE_REDEFINED (sym))
1438
48
        {
1439
48
          as_bad (_("symbol `%s' is already defined"),
1440
48
            S_GET_NAME (sym));
1441
48
          return;
1442
48
        }
1443
53
    }
1444
35
        S_SET_SEGMENT (sym, undefined_section);
1445
35
        S_SET_VALUE (sym, 0);
1446
35
        symbol_set_frag (sym, &zero_address_frag);
1447
35
      }
1448
332
    current.u.view = sym;
1449
332
  }
1450
97
      else
1451
97
  {
1452
97
    as_bad (_("unknown .loc sub-directive `%s'"), p);
1453
97
    (void) restore_line_pointer (c);
1454
97
    return;
1455
97
  }
1456
1457
356
      SKIP_WHITESPACE ();
1458
356
    }
1459
1460
1.02k
  demand_empty_rest_of_line ();
1461
1.02k
  dwarf2_any_loc_directive_seen = dwarf2_loc_directive_seen = true;
1462
1463
  /* If we were given a view id, emit the row right away.  */
1464
1.02k
  if (current.u.view)
1465
328
    dwarf2_emit_insn (0);
1466
1.02k
}
1467
1468
void
1469
dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
1470
175
{
1471
175
  offsetT value = get_absolute_expression ();
1472
1473
175
  if (value != 0 && value != 1)
1474
4
    {
1475
4
      as_bad (_("expected 0 or 1"));
1476
4
      ignore_rest_of_line ();
1477
4
    }
1478
171
  else
1479
171
    {
1480
171
      dwarf2_loc_mark_labels = value != 0;
1481
171
      demand_empty_rest_of_line ();
1482
171
    }
1483
175
}
1484

1485
static struct frag *
1486
first_frag_for_seg (segT seg)
1487
40
{
1488
40
  return seg_info (seg)->frchainP->frch_root;
1489
40
}
1490
1491
static struct frag *
1492
last_frag_for_seg (segT seg)
1493
78
{
1494
78
  frchainS *f = seg_info (seg)->frchainP;
1495
1496
88
  while (f->frch_next != NULL)
1497
10
    f = f->frch_next;
1498
1499
78
  return f->frch_last;
1500
78
}
1501

1502
/* Emit a single byte into the current segment.  */
1503
1504
static inline void
1505
out_byte (int byte)
1506
1.94k
{
1507
1.94k
  FRAG_APPEND_1_CHAR (byte);
1508
1.94k
}
1509
1510
/* Emit a statement program opcode into the current segment.  */
1511
1512
static inline void
1513
out_opcode (int opc)
1514
656
{
1515
656
  out_byte (opc);
1516
656
}
1517
1518
/* Emit a two-byte word into the current segment.  */
1519
1520
static inline void
1521
out_two (int data)
1522
153
{
1523
153
  md_number_to_chars (frag_more (2), data, 2);
1524
153
}
1525
1526
/* Emit a four byte word into the current segment.  */
1527
1528
static inline void
1529
out_four (int data)
1530
1
{
1531
1
  md_number_to_chars (frag_more (4), data, 4);
1532
1
}
1533
1534
/* Emit an unsigned "little-endian base 128" number.  */
1535
1536
static void
1537
out_uleb128 (addressT value)
1538
1.51k
{
1539
1.51k
  output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
1540
1.51k
}
1541
1542
/* Emit a signed "little-endian base 128" number.  */
1543
1544
static void
1545
out_leb128 (addressT value)
1546
5
{
1547
5
  output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
1548
5
}
1549
1550
/* Emit a tuple for .debug_abbrev.  */
1551
1552
static inline void
1553
out_abbrev (int name, int form)
1554
329
{
1555
329
  out_uleb128 (name);
1556
329
  out_uleb128 (form);
1557
329
}
1558
1559
/* Get the size of a fragment.  */
1560
1561
static offsetT
1562
get_frag_fix (fragS *frag, segT seg)
1563
321
{
1564
321
  frchainS *fr;
1565
1566
321
  if (frag->fr_next)
1567
243
    return frag->fr_fix;
1568
1569
  /* If a fragment is the last in the chain, special measures must be
1570
     taken to find its size before relaxation, since it may be pending
1571
     on some subsegment chain.  */
1572
88
  for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
1573
88
    if (fr->frch_last == frag)
1574
78
      return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
1575
1576
78
  abort ();
1577
78
}
1578
1579
/* Set an absolute address (may result in a relocation entry).  */
1580
1581
static void
1582
out_set_addr (symbolS *sym)
1583
301
{
1584
301
  expressionS exp;
1585
1586
301
  memset (&exp, 0, sizeof exp);
1587
301
  out_opcode (DW_LNS_extended_op);
1588
301
  out_uleb128 (sizeof_address + 1);
1589
1590
301
  out_opcode (DW_LNE_set_address);
1591
301
  exp.X_op = O_symbol;
1592
301
  exp.X_add_symbol = sym;
1593
301
  exp.X_add_number = 0;
1594
301
  emit_expr (&exp, sizeof_address);
1595
301
}
1596
1597
static void
1598
scale_addr_delta (int line_delta, addressT *addr_delta)
1599
1.67k
{
1600
1.67k
  static int printed_this = 0;
1601
1.67k
  if (DWARF2_LINE_MIN_INSN_LENGTH > 1)
1602
0
    {
1603
      /* Don't error on non-instruction bytes at end of section.  */
1604
0
      if (line_delta != INT_MAX
1605
0
    && *addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0  && !printed_this)
1606
0
  {
1607
0
    as_bad("unaligned opcodes detected in executable segment");
1608
0
    printed_this = 1;
1609
0
  }
1610
0
      *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
1611
0
    }
1612
1.67k
}
1613
1614
/* Encode a pair of line and address skips as efficiently as possible.
1615
   Note that the line skip is signed, whereas the address skip is unsigned.
1616
1617
   The following two routines *must* be kept in sync.  This is
1618
   enforced by making emit_inc_line_addr abort if we do not emit
1619
   exactly the expected number of bytes.  */
1620
1621
static int
1622
size_inc_line_addr (int line_delta, addressT addr_delta)
1623
848
{
1624
848
  unsigned int tmp, opcode;
1625
848
  int len = 0;
1626
1627
  /* Scale the address delta by the minimum instruction length.  */
1628
848
  scale_addr_delta (line_delta, &addr_delta);
1629
1630
  /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
1631
     We cannot use special opcodes here, since we want the end_sequence
1632
     to emit the matrix entry.  */
1633
848
  if (line_delta == INT_MAX)
1634
38
    {
1635
38
      if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
1636
0
  len = 1;
1637
38
      else if (addr_delta)
1638
17
  len = 1 + sizeof_leb128 (addr_delta, 0);
1639
38
      return len + 3;
1640
38
    }
1641
1642
  /* Bias the line delta by the base.  */
1643
810
  tmp = (unsigned) line_delta - DWARF2_LINE_BASE;
1644
1645
  /* If the line increment is out of range of a special opcode, we
1646
     must encode it with DW_LNS_advance_line.  */
1647
810
  if (tmp >= DWARF2_LINE_RANGE)
1648
402
    {
1649
402
      len = 1 + sizeof_leb128 (line_delta, 1);
1650
402
      line_delta = 0;
1651
402
      tmp = 0 - DWARF2_LINE_BASE;
1652
402
    }
1653
1654
  /* Bias the opcode by the special opcode base.  */
1655
810
  tmp += DWARF2_LINE_OPCODE_BASE;
1656
1657
  /* Avoid overflow when addr_delta is large.  */
1658
810
  if (addr_delta < 256U + MAX_SPECIAL_ADDR_DELTA)
1659
796
    {
1660
      /* Try using a special opcode.  */
1661
796
      opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
1662
796
      if (opcode <= 255)
1663
555
  return len + 1;
1664
1665
      /* Try using DW_LNS_const_add_pc followed by special op.  */
1666
241
      opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
1667
241
      if (opcode <= 255)
1668
168
  return len + 2;
1669
241
    }
1670
1671
  /* Otherwise use DW_LNS_advance_pc.  */
1672
87
  len += 1 + sizeof_leb128 (addr_delta, 0);
1673
1674
  /* DW_LNS_copy or special opcode.  */
1675
87
  len += 1;
1676
1677
87
  return len;
1678
810
}
1679
1680
static void
1681
emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
1682
827
{
1683
827
  unsigned int tmp, opcode;
1684
827
  int need_copy = 0;
1685
827
  char *end = p + len;
1686
1687
  /* Line number sequences cannot go backward in addresses.  This means
1688
     we've incorrectly ordered the statements in the sequence.  */
1689
827
  gas_assert ((offsetT) addr_delta >= 0);
1690
1691
  /* Scale the address delta by the minimum instruction length.  */
1692
827
  scale_addr_delta (line_delta, &addr_delta);
1693
1694
  /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
1695
     We cannot use special opcodes here, since we want the end_sequence
1696
     to emit the matrix entry.  */
1697
827
  if (line_delta == INT_MAX)
1698
31
    {
1699
31
      if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
1700
0
  *p++ = DW_LNS_const_add_pc;
1701
31
      else if (addr_delta)
1702
10
  {
1703
10
    *p++ = DW_LNS_advance_pc;
1704
10
    p += output_leb128 (p, addr_delta, 0);
1705
10
  }
1706
1707
31
      *p++ = DW_LNS_extended_op;
1708
31
      *p++ = 1;
1709
31
      *p++ = DW_LNE_end_sequence;
1710
31
      goto done;
1711
31
    }
1712
1713
  /* Bias the line delta by the base.  */
1714
796
  tmp = (unsigned) line_delta - DWARF2_LINE_BASE;
1715
1716
  /* If the line increment is out of range of a special opcode, we
1717
     must encode it with DW_LNS_advance_line.  */
1718
796
  if (tmp >= DWARF2_LINE_RANGE)
1719
390
    {
1720
390
      *p++ = DW_LNS_advance_line;
1721
390
      p += output_leb128 (p, line_delta, 1);
1722
1723
390
      line_delta = 0;
1724
390
      tmp = 0 - DWARF2_LINE_BASE;
1725
390
      need_copy = 1;
1726
390
    }
1727
1728
  /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
1729
     special opcode.  */
1730
796
  if (line_delta == 0 && addr_delta == 0)
1731
524
    {
1732
524
      *p++ = DW_LNS_copy;
1733
524
      goto done;
1734
524
    }
1735
1736
  /* Bias the opcode by the special opcode base.  */
1737
272
  tmp += DWARF2_LINE_OPCODE_BASE;
1738
1739
  /* Avoid overflow when addr_delta is large.  */
1740
272
  if (addr_delta < 256U + MAX_SPECIAL_ADDR_DELTA)
1741
272
    {
1742
      /* Try using a special opcode.  */
1743
272
      opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
1744
272
      if (opcode <= 255)
1745
31
  {
1746
31
    *p++ = opcode;
1747
31
    goto done;
1748
31
  }
1749
1750
      /* Try using DW_LNS_const_add_pc followed by special op.  */
1751
241
      opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
1752
241
      if (opcode <= 255)
1753
168
  {
1754
168
    *p++ = DW_LNS_const_add_pc;
1755
168
    *p++ = opcode;
1756
168
    goto done;
1757
168
  }
1758
241
    }
1759
1760
  /* Otherwise use DW_LNS_advance_pc.  */
1761
73
  *p++ = DW_LNS_advance_pc;
1762
73
  p += output_leb128 (p, addr_delta, 0);
1763
1764
73
  if (need_copy)
1765
73
    *p++ = DW_LNS_copy;
1766
0
  else
1767
0
    *p++ = tmp;
1768
1769
827
 done:
1770
827
  gas_assert (p == end);
1771
827
}
1772
1773
/* Handy routine to combine calls to the above two routines.  */
1774
1775
static void
1776
out_inc_line_addr (int line_delta, addressT addr_delta)
1777
827
{
1778
827
  int len = size_inc_line_addr (line_delta, addr_delta);
1779
827
  emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
1780
827
}
1781
1782
/* Write out an alternative form of line and address skips using
1783
   DW_LNS_fixed_advance_pc opcodes.  This uses more space than the default
1784
   line and address information, but it is required if linker relaxation
1785
   could change the code offsets.  The following two routines *must* be
1786
   kept in sync.  */
1787
0
#define ADDR_DELTA_LIMIT 50000
1788
1789
static int
1790
size_fixed_inc_line_addr (int line_delta, addressT addr_delta)
1791
0
{
1792
0
  int len = 0;
1793
1794
  /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.  */
1795
0
  if (line_delta != INT_MAX)
1796
0
    len = 1 + sizeof_leb128 (line_delta, 1);
1797
1798
0
  if (addr_delta > ADDR_DELTA_LIMIT)
1799
0
    {
1800
      /* DW_LNS_extended_op */
1801
0
      len += 1 + sizeof_leb128 (sizeof_address + 1, 0);
1802
      /* DW_LNE_set_address */
1803
0
      len += 1 + sizeof_address;
1804
0
    }
1805
0
  else
1806
    /* DW_LNS_fixed_advance_pc */
1807
0
    len += 3;
1808
1809
0
  if (line_delta == INT_MAX)
1810
    /* DW_LNS_extended_op + DW_LNE_end_sequence */
1811
0
    len += 3;
1812
0
  else
1813
    /* DW_LNS_copy */
1814
0
    len += 1;
1815
1816
0
  return len;
1817
0
}
1818
1819
static void
1820
emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag,
1821
        char *p, int len)
1822
0
{
1823
0
  expressionS *pexp;
1824
0
  char *end = p + len;
1825
1826
  /* Line number sequences cannot go backward in addresses.  This means
1827
     we've incorrectly ordered the statements in the sequence.  */
1828
0
  gas_assert ((offsetT) addr_delta >= 0);
1829
1830
  /* Verify that we have kept in sync with size_fixed_inc_line_addr.  */
1831
0
  gas_assert (len == size_fixed_inc_line_addr (line_delta, addr_delta));
1832
1833
  /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.  */
1834
0
  if (line_delta != INT_MAX)
1835
0
    {
1836
0
      *p++ = DW_LNS_advance_line;
1837
0
      p += output_leb128 (p, line_delta, 1);
1838
0
    }
1839
1840
0
  pexp = symbol_get_value_expression (frag->fr_symbol);
1841
1842
  /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can
1843
     advance the address by at most 64K.  Linker relaxation (without
1844
     which this function would not be used) could change the operand by
1845
     an unknown amount.  If the address increment is getting close to
1846
     the limit, just reset the address.  */
1847
0
  if (addr_delta > ADDR_DELTA_LIMIT)
1848
0
    {
1849
0
      symbolS *to_sym;
1850
0
      expressionS exp;
1851
1852
0
      memset (&exp, 0, sizeof exp);
1853
0
      gas_assert (pexp->X_op == O_subtract);
1854
0
      to_sym = pexp->X_add_symbol;
1855
1856
0
      *p++ = DW_LNS_extended_op;
1857
0
      p += output_leb128 (p, sizeof_address + 1, 0);
1858
0
      *p++ = DW_LNE_set_address;
1859
0
      exp.X_op = O_symbol;
1860
0
      exp.X_add_symbol = to_sym;
1861
0
      exp.X_add_number = 0;
1862
0
      emit_expr_fix (&exp, sizeof_address, frag, p, TC_PARSE_CONS_RETURN_NONE);
1863
0
      p += sizeof_address;
1864
0
    }
1865
0
  else
1866
0
    {
1867
0
      *p++ = DW_LNS_fixed_advance_pc;
1868
0
      emit_expr_fix (pexp, 2, frag, p, TC_PARSE_CONS_RETURN_NONE);
1869
0
      p += 2;
1870
0
    }
1871
1872
0
  if (line_delta == INT_MAX)
1873
0
    {
1874
0
      *p++ = DW_LNS_extended_op;
1875
0
      *p++ = 1;
1876
0
      *p++ = DW_LNE_end_sequence;
1877
0
    }
1878
0
  else
1879
0
    *p++ = DW_LNS_copy;
1880
1881
0
  gas_assert (p == end);
1882
0
}
1883
1884
/* Generate a variant frag that we can use to relax address/line
1885
   increments between fragments of the target segment.  */
1886
1887
static void
1888
relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
1889
21
{
1890
21
  expressionS exp;
1891
21
  int max_chars;
1892
1893
21
  memset (&exp, 0, sizeof exp);
1894
21
  exp.X_op = O_subtract;
1895
21
  exp.X_add_symbol = to_sym;
1896
21
  exp.X_op_symbol = from_sym;
1897
21
  exp.X_add_number = 0;
1898
1899
  /* The maximum size of the frag is the line delta with a maximum
1900
     sized address delta.  */
1901
21
  if (DWARF2_USE_FIXED_ADVANCE_PC)
1902
0
    max_chars = size_fixed_inc_line_addr (line_delta,
1903
0
            -DWARF2_LINE_MIN_INSN_LENGTH);
1904
21
  else
1905
21
    max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
1906
1907
21
  frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
1908
21
      make_expr_symbol (&exp), line_delta, NULL);
1909
21
}
1910
1911
/* The function estimates the size of a rs_dwarf2dbg variant frag
1912
   based on the current values of the symbols.  It is called before
1913
   the relaxation loop.  We set fr_subtype to the expected length.  */
1914
1915
int
1916
dwarf2dbg_estimate_size_before_relax (fragS *frag)
1917
0
{
1918
0
  offsetT addr_delta;
1919
0
  int size;
1920
1921
0
  addr_delta = resolve_symbol_value (frag->fr_symbol);
1922
0
  if (DWARF2_USE_FIXED_ADVANCE_PC)
1923
0
    size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta);
1924
0
  else
1925
0
    size = size_inc_line_addr (frag->fr_offset, addr_delta);
1926
1927
0
  frag->fr_subtype = size;
1928
1929
0
  return size;
1930
0
}
1931
1932
/* This function relaxes a rs_dwarf2dbg variant frag based on the
1933
   current values of the symbols.  fr_subtype is the current length
1934
   of the frag.  This returns the change in frag length.  */
1935
1936
int
1937
dwarf2dbg_relax_frag (fragS *frag)
1938
0
{
1939
0
  int old_size, new_size;
1940
1941
0
  old_size = frag->fr_subtype;
1942
0
  new_size = dwarf2dbg_estimate_size_before_relax (frag);
1943
1944
0
  return new_size - old_size;
1945
0
}
1946
1947
/* This function converts a rs_dwarf2dbg variant frag into a normal
1948
   fill frag.  This is called after all relaxation has been done.
1949
   fr_subtype will be the desired length of the frag.  */
1950
1951
void
1952
dwarf2dbg_convert_frag (fragS *frag)
1953
0
{
1954
0
  offsetT addr_diff;
1955
1956
0
  if (DWARF2_USE_FIXED_ADVANCE_PC)
1957
0
    {
1958
      /* If linker relaxation is enabled then the distance between the two
1959
   symbols in the frag->fr_symbol expression might change.  Hence we
1960
   cannot rely upon the value computed by resolve_symbol_value.
1961
   Instead we leave the expression unfinalized and allow
1962
   emit_fixed_inc_line_addr to create a fixup (which later becomes a
1963
   relocation) that will allow the linker to correctly compute the
1964
   actual address difference.  We have to use a fixed line advance for
1965
   this as we cannot (easily) relocate leb128 encoded values.  */
1966
0
      int saved_finalize_syms = finalize_syms;
1967
1968
0
      finalize_syms = 0;
1969
0
      addr_diff = resolve_symbol_value (frag->fr_symbol);
1970
0
      finalize_syms = saved_finalize_syms;
1971
0
    }
1972
0
  else
1973
0
    addr_diff = resolve_symbol_value (frag->fr_symbol);
1974
1975
  /* fr_var carries the max_chars that we created the fragment with.
1976
     fr_subtype carries the current expected length.  We must, of
1977
     course, have allocated enough memory earlier.  */
1978
0
  gas_assert (frag->fr_var >= (int) frag->fr_subtype);
1979
1980
0
  if (DWARF2_USE_FIXED_ADVANCE_PC)
1981
0
    emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag,
1982
0
            frag->fr_literal + frag->fr_fix,
1983
0
            frag->fr_subtype);
1984
0
  else
1985
0
    emit_inc_line_addr (frag->fr_offset, addr_diff,
1986
0
      frag->fr_literal + frag->fr_fix, frag->fr_subtype);
1987
1988
0
  frag->fr_fix += frag->fr_subtype;
1989
0
  frag->fr_type = rs_fill;
1990
0
  frag->fr_var = 0;
1991
0
  frag->fr_offset = 0;
1992
0
}
1993
1994
/* Generate .debug_line content for the chain of line number entries
1995
   beginning at E, for segment SEG.  */
1996
1997
static void
1998
process_entries (segT seg, struct line_entry *e)
1999
38
{
2000
38
  unsigned filenum = 1;
2001
38
  unsigned line = 1;
2002
38
  unsigned column = 0;
2003
38
  unsigned isa = 0;
2004
38
  unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2005
38
  fragS *last_frag = NULL, *frag;
2006
38
  addressT last_frag_ofs = 0, frag_ofs;
2007
38
  symbolS *last_lab = NULL, *lab;
2008
2009
38
  if (flag_dwarf_sections)
2010
0
    {
2011
0
      char * name;
2012
0
      const char * sec_name;
2013
2014
      /* Switch to the relevant sub-section before we start to emit
2015
   the line number table.
2016
2017
   FIXME: These sub-sections do not have a normal Line Number
2018
   Program Header, thus strictly speaking they are not valid
2019
   DWARF sections.  Unfortunately the DWARF standard assumes
2020
   a one-to-one relationship between compilation units and
2021
   line number tables.  Thus we have to have a .debug_line
2022
   section, as well as our sub-sections, and we have to ensure
2023
   that all of the sub-sections are merged into a proper
2024
   .debug_line section before a debugger sees them.  */
2025
2026
0
      sec_name = bfd_section_name (seg);
2027
0
      if (strcmp (sec_name, ".text") != 0)
2028
0
  {
2029
0
    name = concat (".debug_line", sec_name, (char *) NULL);
2030
0
    subseg_set (subseg_get (name, false), 0);
2031
0
  }
2032
0
      else
2033
  /* Don't create a .debug_line.text section -
2034
     that is redundant.  Instead just switch back to the
2035
     normal .debug_line section.  */
2036
0
  subseg_set (subseg_get (".debug_line", false), 0);
2037
0
    }
2038
2039
38
  do
2040
810
    {
2041
810
      int line_delta;
2042
2043
810
      if (filenum != e->loc.filenum)
2044
31
  {
2045
31
    filenum = e->loc.filenum;
2046
31
    out_opcode (DW_LNS_set_file);
2047
31
    out_uleb128 (filenum);
2048
31
  }
2049
2050
810
      if (column != e->loc.column)
2051
10
  {
2052
10
    column = e->loc.column;
2053
10
    out_opcode (DW_LNS_set_column);
2054
10
    out_uleb128 (column);
2055
10
  }
2056
2057
810
      if (e->loc.discriminator != 0)
2058
0
  {
2059
0
    out_opcode (DW_LNS_extended_op);
2060
0
    out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0));
2061
0
    out_opcode (DW_LNE_set_discriminator);
2062
0
    out_uleb128 (e->loc.discriminator);
2063
0
  }
2064
2065
810
      if (isa != e->loc.isa)
2066
0
  {
2067
0
    isa = e->loc.isa;
2068
0
    out_opcode (DW_LNS_set_isa);
2069
0
    out_uleb128 (isa);
2070
0
  }
2071
2072
810
      if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
2073
2
  {
2074
2
    flags = e->loc.flags;
2075
2
    out_opcode (DW_LNS_negate_stmt);
2076
2
  }
2077
2078
810
      if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
2079
1
  out_opcode (DW_LNS_set_basic_block);
2080
2081
810
      if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
2082
0
  out_opcode (DW_LNS_set_prologue_end);
2083
2084
810
      if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
2085
10
  out_opcode (DW_LNS_set_epilogue_begin);
2086
2087
      /* Don't try to optimize away redundant entries; gdb wants two
2088
   entries for a function where the code starts on the same line as
2089
   the {, and there's no way to identify that case here.  Trust gcc
2090
   to optimize appropriately.  */
2091
810
      line_delta = e->loc.line - line;
2092
810
      lab = e->label;
2093
810
      frag = symbol_get_frag (lab);
2094
810
      frag_ofs = S_GET_VALUE (lab);
2095
2096
810
      if (last_frag == NULL
2097
772
    || (e->loc.u.view == force_reset_view && force_reset_view
2098
        /* If we're going to reset the view, but we know we're
2099
     advancing the PC, we don't have to force with
2100
     set_address.  We know we do when we're at the same
2101
     address of the same frag, and we know we might when
2102
     we're in the beginning of a frag, and we were at the
2103
     end of the previous frag.  */
2104
287
        && (frag == last_frag
2105
287
      ? (last_frag_ofs == frag_ofs)
2106
287
      : (frag_ofs == 0
2107
243
         && ((offsetT)last_frag_ofs
2108
243
       >= get_frag_fix (last_frag, seg))))))
2109
301
  {
2110
301
    out_set_addr (lab);
2111
301
    out_inc_line_addr (line_delta, 0);
2112
301
  }
2113
509
      else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
2114
495
  out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
2115
14
      else
2116
14
  relax_inc_line_addr (line_delta, lab, last_lab);
2117
2118
810
      line = e->loc.line;
2119
810
      last_lab = lab;
2120
810
      last_frag = frag;
2121
810
      last_frag_ofs = frag_ofs;
2122
2123
810
      e = e->next;
2124
810
    }
2125
810
  while (e);
2126
2127
  /* Emit a DW_LNE_end_sequence for the end of the section.  */
2128
38
  frag = last_frag_for_seg (seg);
2129
38
  frag_ofs = get_frag_fix (frag, seg);
2130
38
  if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
2131
31
    out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
2132
7
  else
2133
7
    {
2134
7
      lab = symbol_temp_new (seg, frag, frag_ofs);
2135
7
      relax_inc_line_addr (INT_MAX, lab, last_lab);
2136
7
    }
2137
38
}
2138
2139
/* Switch to LINE_STR_SEG and output the given STR.  Return the
2140
   symbol pointing to the new string in the section.  */
2141
2142
static symbolS *
2143
add_line_strp (segT line_str_seg, const char *str)
2144
97
{
2145
97
  char *cp;
2146
97
  size_t size;
2147
97
  symbolS *sym;
2148
2149
97
  subseg_set (line_str_seg, 0);
2150
2151
97
  sym = symbol_temp_new_now_octets ();
2152
2153
97
  size = strlen (str) + 1;
2154
97
  cp = frag_more (size);
2155
97
  memcpy (cp, str, size);
2156
2157
97
  return sym;
2158
97
}
2159
2160
2161
/* Emit the directory and file tables for .debug_line.  */
2162
2163
static void
2164
out_dir_and_file_list (segT line_seg, int sizeof_offset)
2165
41
{
2166
41
  size_t size;
2167
41
  char *dir;
2168
41
  char *cp;
2169
41
  unsigned int i, j;
2170
41
  bool emit_md5 = false;
2171
41
  bool emit_timestamps = true;
2172
41
  bool emit_filesize = true;
2173
41
  segT line_str_seg = NULL;
2174
41
  symbolS *line_strp, *file0_strp = NULL;
2175
2176
  /* Output the Directory Table.  */
2177
41
  if (DWARF2_LINE_VERSION >= 5)
2178
41
    {
2179
      /* We only have one column in the directory table.  */
2180
41
      out_byte (1);
2181
2182
      /* Describe the purpose and format of the column.  */
2183
41
      out_uleb128 (DW_LNCT_path);
2184
      /* Store these strings in the .debug_line_str section so they
2185
   can be shared.  */
2186
41
      out_uleb128 (DW_FORM_line_strp);
2187
2188
      /* Now state how many rows there are in the table.  We need at
2189
   least 1 if there is one or more file names to store the
2190
   "working directory".  */
2191
41
      if (dirs_in_use == 0 && files_in_use > 0)
2192
25
  out_uleb128 (1);
2193
16
      else
2194
16
  out_uleb128 (dirs_in_use);
2195
41
    }
2196
      
2197
  /* Emit directory list.  */
2198
41
  if (DWARF2_LINE_VERSION >= 5 && (dirs_in_use > 0 || files_in_use > 0))
2199
37
    {
2200
37
      line_str_seg = subseg_new (".debug_line_str", 0);
2201
37
      bfd_set_section_flags (line_str_seg,
2202
37
           SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS
2203
37
           | SEC_MERGE | SEC_STRINGS);
2204
37
      line_str_seg->entsize = 1;
2205
2206
      /* DWARF5 uses slot zero, but that is only set explicitly
2207
   using a .file 0 directive.  Otherwise use pwd as main file
2208
   directory.  */
2209
37
      if (dirs_in_use > 0 && dirs[0] != NULL)
2210
12
  dir = remap_debug_filename (dirs[0]);
2211
25
      else
2212
25
  dir = remap_debug_filename (getpwd ());
2213
2214
37
      line_strp = add_line_strp (line_str_seg, dir);
2215
37
      free (dir);
2216
37
      subseg_set (line_seg, 0);
2217
37
      TC_DWARF2_EMIT_OFFSET (line_strp, sizeof_offset);
2218
37
    }
2219
62
  for (i = 1; i < dirs_in_use; ++i)
2220
21
    {
2221
21
      dir = remap_debug_filename (dirs[i]);
2222
21
      if (DWARF2_LINE_VERSION < 5)
2223
0
  {
2224
0
    size = strlen (dir) + 1;
2225
0
    cp = frag_more (size);
2226
0
    memcpy (cp, dir, size);
2227
0
  }
2228
21
      else
2229
21
  {
2230
21
    line_strp = add_line_strp (line_str_seg, dir);
2231
21
    subseg_set (line_seg, 0);
2232
21
    TC_DWARF2_EMIT_OFFSET (line_strp, sizeof_offset);
2233
21
  }
2234
21
      free (dir);
2235
21
    }
2236
2237
41
  if (DWARF2_LINE_VERSION < 5)
2238
    /* Terminate it.  */
2239
0
    out_byte ('\0');
2240
2241
  /* Output the File Name Table.  */
2242
41
  if (DWARF2_LINE_VERSION >= 5)
2243
41
    {
2244
41
      unsigned int columns = 4;
2245
2246
41
      if (((unsigned long) DWARF2_FILE_TIME_NAME ("", "")) == -1UL)
2247
41
  {
2248
41
    emit_timestamps = false;
2249
41
    -- columns;
2250
41
  }
2251
2252
41
      if (DWARF2_FILE_SIZE_NAME ("", "") == -1)
2253
41
  {
2254
41
    emit_filesize = false;
2255
41
    -- columns;
2256
41
  }
2257
2258
430k
      for (i = 0; i < files_in_use; ++i)
2259
430k
  if (files[i].md5[0] != 0)
2260
0
    break;
2261
41
      if (i < files_in_use)
2262
0
  {
2263
0
    emit_md5 = true;
2264
0
    ++ columns;
2265
0
  }
2266
      
2267
      /* The number of format entries to follow.  */
2268
41
      out_byte (columns);
2269
      /* The format of the file name.  */
2270
41
      out_uleb128 (DW_LNCT_path);
2271
      /* Store these strings in the .debug_line_str section so they
2272
   can be shared.  */
2273
41
      out_uleb128 (DW_FORM_line_strp);
2274
2275
      /* The format of the directory index.  */
2276
41
      out_uleb128 (DW_LNCT_directory_index);
2277
41
      out_uleb128 (DW_FORM_udata);
2278
2279
41
      if (emit_timestamps)
2280
0
  {
2281
    /* The format of the timestamp.  */
2282
0
    out_uleb128 (DW_LNCT_timestamp);
2283
0
    out_uleb128 (DW_FORM_udata);
2284
0
  }
2285
2286
41
      if (emit_filesize)
2287
0
  {
2288
    /* The format of the file size.  */
2289
0
    out_uleb128 (DW_LNCT_size);
2290
0
    out_uleb128 (DW_FORM_udata);
2291
0
  }
2292
2293
41
      if (emit_md5)
2294
0
  {
2295
    /* The format of the MD5 sum.  */
2296
0
    out_uleb128 (DW_LNCT_MD5);
2297
0
    out_uleb128 (DW_FORM_data16);
2298
0
  }
2299
2300
      /* The number of entries in the table.  */
2301
41
      out_uleb128 (files_in_use);
2302
41
   }
2303
      
2304
430k
  for (i = DWARF2_LINE_VERSION > 4 ? 0 : 1; i < files_in_use; ++i)
2305
430k
    {
2306
430k
      const char *fullfilename;
2307
2308
430k
      if (files[i].filename == NULL)
2309
430k
  {
2310
430k
    if (DWARF2_LINE_VERSION < 5 || i != 0)
2311
430k
      {
2312
430k
        as_bad (_("unassigned file number %ld"), (long) i);
2313
430k
        continue;
2314
430k
      }
2315
    /* DWARF5 uses slot zero, but that is only set explicitly using
2316
       a .file 0 directive.  If that isn't used, but file 1 is, then
2317
       use that as main file name.  */
2318
5
    if (files_in_use > 1 && files[1].filename != NULL)
2319
5
      {
2320
5
        files[0].filename = files[1].filename;
2321
5
        files[0].dir = files[1].dir;
2322
5
        if (emit_md5)
2323
0
    for (j = 0; j < NUM_MD5_BYTES; ++j)
2324
0
      files[0].md5[j] = files[1].md5[j];
2325
5
      }
2326
0
    else
2327
0
      files[0].filename = "";
2328
5
  }
2329
2330
44
      fullfilename = DWARF2_FILE_NAME (files[i].filename,
2331
44
               files[i].dir ? dirs [files [i].dir] : "");
2332
44
      if (DWARF2_LINE_VERSION < 5)
2333
0
  {
2334
0
    size = strlen (fullfilename) + 1;
2335
0
    cp = frag_more (size);
2336
0
    memcpy (cp, fullfilename, size);
2337
0
  }
2338
44
      else
2339
44
  {
2340
44
    if (!file0_strp)
2341
39
      line_strp = add_line_strp (line_str_seg, fullfilename);
2342
5
    else
2343
5
      line_strp = file0_strp;
2344
44
    subseg_set (line_seg, 0);
2345
44
    TC_DWARF2_EMIT_OFFSET (line_strp, sizeof_offset);
2346
44
    if (i == 0 && files_in_use > 1
2347
7
        && files[0].filename == files[1].filename)
2348
5
      file0_strp = line_strp;
2349
39
    else
2350
39
      file0_strp = NULL;
2351
44
  }
2352
2353
      /* Directory number.  */
2354
44
      out_uleb128 (files[i].dir);
2355
2356
      /* Output the last modification timestamp.  */
2357
44
      if (emit_timestamps)
2358
0
  {
2359
0
    offsetT timestamp;
2360
2361
0
    timestamp = DWARF2_FILE_TIME_NAME (files[i].filename,
2362
0
               files[i].dir ? dirs [files [i].dir] : "");
2363
0
    if (timestamp == -1)
2364
0
      timestamp = 0;
2365
0
    out_uleb128 (timestamp);
2366
0
  }
2367
2368
      /* Output the filesize.  */
2369
44
      if (emit_filesize)
2370
0
  {
2371
0
    offsetT filesize;
2372
0
    filesize = DWARF2_FILE_SIZE_NAME (files[i].filename,
2373
0
              files[i].dir ? dirs [files [i].dir] : "");
2374
0
    if (filesize == -1)
2375
0
      filesize = 0;
2376
0
    out_uleb128 (filesize);
2377
0
  }
2378
2379
      /* Output the md5 sum.  */
2380
44
      if (emit_md5)
2381
0
  {
2382
0
    int b;
2383
2384
0
    for (b = 0; b < NUM_MD5_BYTES; b++)
2385
0
      out_byte (files[i].md5[b]);
2386
0
  }
2387
44
    }
2388
2389
41
  if (DWARF2_LINE_VERSION < 5)
2390
    /* Terminate filename list.  */
2391
0
    out_byte (0);
2392
41
}
2393
2394
/* Switch to SEC and output a header length field.  Return the size of
2395
   offsets used in SEC.  The caller must set EXPR->X_add_symbol value
2396
   to the end of the section.  EXPR->X_add_number will be set to the
2397
   negative size of the header.  */
2398
2399
static int
2400
out_header (asection *sec, expressionS *exp)
2401
116
{
2402
116
  symbolS *start_sym;
2403
116
  symbolS *end_sym;
2404
2405
116
  subseg_set (sec, 0);
2406
2407
116
  if (flag_dwarf_sections)
2408
0
    {
2409
      /* If we are going to put the start and end symbols in different
2410
   sections, then we need real symbols, not just fake, local ones.  */
2411
0
      frag_now_fix ();
2412
0
      start_sym = symbol_make (".Ldebug_line_start");
2413
0
      end_sym = symbol_make (".Ldebug_line_end");
2414
0
      symbol_set_value_now (start_sym);
2415
0
    }
2416
116
  else
2417
116
    {
2418
116
      start_sym = symbol_temp_new_now_octets ();
2419
116
      end_sym = symbol_temp_make ();
2420
116
    }
2421
2422
  /* Total length of the information.  */
2423
116
  exp->X_op = O_subtract;
2424
116
  exp->X_add_symbol = end_sym;
2425
116
  exp->X_op_symbol = start_sym;
2426
2427
116
  switch (DWARF2_FORMAT (sec))
2428
116
    {
2429
116
    case dwarf2_format_32bit:
2430
116
      exp->X_add_number = -4;
2431
116
      emit_expr (exp, 4);
2432
116
      return 4;
2433
2434
0
    case dwarf2_format_64bit:
2435
0
      exp->X_add_number = -12;
2436
0
      out_four (-1);
2437
0
      emit_expr (exp, 8);
2438
0
      return 8;
2439
2440
0
    case dwarf2_format_64bit_irix:
2441
0
      exp->X_add_number = -8;
2442
0
      emit_expr (exp, 8);
2443
0
      return 8;
2444
116
    }
2445
2446
0
  as_fatal (_("internal error: unknown dwarf2 format"));
2447
0
  return 0;
2448
116
}
2449
2450
/* Emit the collected .debug_line data.  */
2451
2452
static void
2453
out_debug_line (segT line_seg)
2454
41
{
2455
41
  expressionS exp;
2456
41
  symbolS *prologue_start, *prologue_end;
2457
41
  symbolS *line_end;
2458
41
  struct line_seg *s;
2459
41
  int sizeof_offset;
2460
2461
41
  memset (&exp, 0, sizeof exp);
2462
41
  sizeof_offset = out_header (line_seg, &exp);
2463
41
  line_end = exp.X_add_symbol;
2464
2465
  /* Version.  */
2466
41
  out_two (DWARF2_LINE_VERSION);
2467
2468
41
  if (DWARF2_LINE_VERSION >= 5)
2469
41
    {
2470
41
      out_byte (sizeof_address);
2471
41
      out_byte (0); /* Segment Selector size.  */
2472
41
    }
2473
  /* Length of the prologue following this length.  */
2474
41
  prologue_start = symbol_temp_make ();
2475
41
  prologue_end = symbol_temp_make ();
2476
41
  exp.X_op = O_subtract;
2477
41
  exp.X_add_symbol = prologue_end;
2478
41
  exp.X_op_symbol = prologue_start;
2479
41
  exp.X_add_number = 0;
2480
41
  emit_expr (&exp, sizeof_offset);
2481
41
  symbol_set_value_now (prologue_start);
2482
2483
  /* Parameters of the state machine.  */
2484
41
  out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
2485
41
  if (DWARF2_LINE_VERSION >= 4)
2486
41
    out_byte (DWARF2_LINE_MAX_OPS_PER_INSN);
2487
41
  out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
2488
41
  out_byte (DWARF2_LINE_BASE);
2489
41
  out_byte (DWARF2_LINE_RANGE);
2490
41
  out_byte (DWARF2_LINE_OPCODE_BASE);
2491
2492
  /* Standard opcode lengths.  */
2493
41
  out_byte (0);     /* DW_LNS_copy */
2494
41
  out_byte (1);     /* DW_LNS_advance_pc */
2495
41
  out_byte (1);     /* DW_LNS_advance_line */
2496
41
  out_byte (1);     /* DW_LNS_set_file */
2497
41
  out_byte (1);     /* DW_LNS_set_column */
2498
41
  out_byte (0);     /* DW_LNS_negate_stmt */
2499
41
  out_byte (0);     /* DW_LNS_set_basic_block */
2500
41
  out_byte (0);     /* DW_LNS_const_add_pc */
2501
41
  out_byte (1);     /* DW_LNS_fixed_advance_pc */
2502
41
  if (DWARF2_LINE_VERSION >= 3)
2503
41
    {
2504
41
      out_byte (0);     /* DW_LNS_set_prologue_end */
2505
41
      out_byte (0);     /* DW_LNS_set_epilogue_begin */
2506
41
      out_byte (1);     /* DW_LNS_set_isa */
2507
      /* We have emitted 12 opcode lengths, so make that this
2508
   matches up to the opcode base value we have been using.  */
2509
41
      gas_assert (DWARF2_LINE_OPCODE_BASE == 13);
2510
41
    }
2511
0
  else
2512
0
    gas_assert (DWARF2_LINE_OPCODE_BASE == 10);
2513
2514
41
  out_dir_and_file_list (line_seg, sizeof_offset);
2515
2516
41
  symbol_set_value_now (prologue_end);
2517
2518
  /* For each section, emit a statement program.  */
2519
79
  for (s = all_segs; s; s = s->next)
2520
    /* Paranoia - this check should have already have
2521
       been handled in dwarf2_gen_line_info_1().  */
2522
38
    if (s->head->head && SEG_NORMAL (s->seg))
2523
38
      process_entries (s->seg, s->head->head);
2524
2525
41
  if (flag_dwarf_sections)
2526
    /* We have to switch to the special .debug_line_end section
2527
       before emitting the end-of-debug_line symbol.  The linker
2528
       script arranges for this section to be placed after all the
2529
       (potentially garbage collected) .debug_line.<foo> sections.
2530
       This section contains the line_end symbol which is used to
2531
       compute the size of the linked .debug_line section, as seen
2532
       in the DWARF Line Number header.  */
2533
0
    subseg_set (subseg_get (".debug_line_end", false), 0);
2534
2535
41
  symbol_set_value_now (line_end);
2536
41
}
2537
2538
static void
2539
out_debug_ranges (segT ranges_seg, symbolS **ranges_sym)
2540
0
{
2541
0
  unsigned int addr_size = sizeof_address;
2542
0
  struct line_seg *s;
2543
0
  expressionS exp;
2544
0
  unsigned int i;
2545
2546
0
  memset (&exp, 0, sizeof exp);
2547
0
  subseg_set (ranges_seg, 0);
2548
2549
  /* For DW_AT_ranges to point at (there is no header, so really start
2550
     of section, but see out_debug_rnglists).  */
2551
0
  *ranges_sym = symbol_temp_new_now_octets ();
2552
2553
  /* Base Address Entry.  */
2554
0
  for (i = 0; i < addr_size; i++)
2555
0
    out_byte (0xff);
2556
0
  for (i = 0; i < addr_size; i++)
2557
0
    out_byte (0);
2558
2559
  /* Range List Entry.  */
2560
0
  for (s = all_segs; s; s = s->next)
2561
0
    {
2562
0
      fragS *frag;
2563
0
      symbolS *beg, *end;
2564
2565
0
      frag = first_frag_for_seg (s->seg);
2566
0
      beg = symbol_temp_new (s->seg, frag, 0);
2567
0
      s->text_start = beg;
2568
2569
0
      frag = last_frag_for_seg (s->seg);
2570
0
      end = symbol_temp_new (s->seg, frag, get_frag_fix (frag, s->seg));
2571
0
      s->text_end = end;
2572
2573
0
      exp.X_op = O_symbol;
2574
0
      exp.X_add_symbol = beg;
2575
0
      exp.X_add_number = 0;
2576
0
      emit_expr (&exp, addr_size);
2577
2578
0
      exp.X_op = O_symbol;
2579
0
      exp.X_add_symbol = end;
2580
0
      exp.X_add_number = 0;
2581
0
      emit_expr (&exp, addr_size);
2582
0
    }
2583
2584
  /* End of Range Entry.   */
2585
0
  for (i = 0; i < addr_size; i++)
2586
0
    out_byte (0);
2587
0
  for (i = 0; i < addr_size; i++)
2588
0
    out_byte (0);
2589
0
}
2590
2591
static void
2592
out_debug_rnglists (segT ranges_seg, symbolS **ranges_sym)
2593
1
{
2594
1
  expressionS exp;
2595
1
  symbolS *ranges_end;
2596
1
  struct line_seg *s;
2597
2598
  /* Unit length.  */
2599
1
  memset (&exp, 0, sizeof exp);
2600
1
  out_header (ranges_seg, &exp);
2601
1
  ranges_end = exp.X_add_symbol;
2602
2603
1
  out_two (DWARF2_RNGLISTS_VERSION);
2604
1
  out_byte (sizeof_address);
2605
1
  out_byte (0); /* Segment Selector size.  */
2606
1
  out_four (0); /* Offset entry count.  */
2607
2608
  /* For DW_AT_ranges to point at (must be after the header).   */
2609
1
  *ranges_sym = symbol_temp_new_now_octets ();
2610
2611
3
  for (s = all_segs; s; s = s->next)
2612
2
    {
2613
2
      fragS *frag;
2614
2
      symbolS *beg, *end;
2615
2616
2
      out_byte (DW_RLE_start_length);
2617
2618
2
      frag = first_frag_for_seg (s->seg);
2619
2
      beg = symbol_temp_new (s->seg, frag, 0);
2620
2
      s->text_start = beg;
2621
2622
2
      frag = last_frag_for_seg (s->seg);
2623
2
      end = symbol_temp_new (s->seg, frag, get_frag_fix (frag, s->seg));
2624
2
      s->text_end = end;
2625
2626
2
      exp.X_op = O_symbol;
2627
2
      exp.X_add_symbol = beg;
2628
2
      exp.X_add_number = 0;
2629
2
      emit_expr (&exp, sizeof_address);
2630
2631
2
      exp.X_op = O_symbol;
2632
2
      exp.X_add_symbol = end;
2633
2
      exp.X_add_number = 0;
2634
2
      emit_leb128_expr (&exp, 0);
2635
2
    }
2636
2637
1
  out_byte (DW_RLE_end_of_list);
2638
2639
1
  symbol_set_value_now (ranges_end);
2640
1
}
2641
2642
/* Emit data for .debug_aranges.  */
2643
2644
static void
2645
out_debug_aranges (segT aranges_seg, segT info_seg)
2646
37
{
2647
37
  unsigned int addr_size = sizeof_address;
2648
37
  offsetT size;
2649
37
  struct line_seg *s;
2650
37
  expressionS exp;
2651
37
  symbolS *aranges_end;
2652
37
  char *p;
2653
37
  int sizeof_offset;
2654
2655
37
  memset (&exp, 0, sizeof exp);
2656
37
  sizeof_offset = out_header (aranges_seg, &exp);
2657
37
  aranges_end = exp.X_add_symbol;
2658
37
  size = -exp.X_add_number;
2659
2660
  /* Version.  */
2661
37
  out_two (DWARF2_ARANGES_VERSION);
2662
37
  size += 2;
2663
2664
  /* Offset to .debug_info.  */
2665
37
  TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset);
2666
37
  size += sizeof_offset;
2667
2668
  /* Size of an address (offset portion).  */
2669
37
  out_byte (addr_size);
2670
37
  size++;
2671
2672
  /* Size of a segment descriptor.  */
2673
37
  out_byte (0);
2674
37
  size++;
2675
2676
  /* Align the header.  */
2677
185
  while ((size++ % (2 * addr_size)) > 0)
2678
148
    out_byte (0);
2679
2680
75
  for (s = all_segs; s; s = s->next)
2681
38
    {
2682
38
      fragS *frag;
2683
38
      symbolS *beg, *end;
2684
2685
38
      frag = first_frag_for_seg (s->seg);
2686
38
      beg = symbol_temp_new (s->seg, frag, 0);
2687
38
      s->text_start = beg;
2688
2689
38
      frag = last_frag_for_seg (s->seg);
2690
38
      end = symbol_temp_new (s->seg, frag, get_frag_fix (frag, s->seg));
2691
38
      s->text_end = end;
2692
2693
38
      exp.X_op = O_symbol;
2694
38
      exp.X_add_symbol = beg;
2695
38
      exp.X_add_number = 0;
2696
38
      emit_expr (&exp, addr_size);
2697
2698
38
      exp.X_op = O_subtract;
2699
38
      exp.X_add_symbol = end;
2700
38
      exp.X_op_symbol = beg;
2701
38
      exp.X_add_number = 0;
2702
38
      emit_expr (&exp, addr_size);
2703
38
    }
2704
2705
37
  p = frag_more (2 * addr_size);
2706
37
  md_number_to_chars (p, 0, addr_size);
2707
37
  md_number_to_chars (p + addr_size, 0, addr_size);
2708
2709
37
  symbol_set_value_now (aranges_end);
2710
37
}
2711
2712
/* Emit data for .debug_abbrev.  Note that this must be kept in
2713
   sync with out_debug_info below.  */
2714
2715
static void
2716
out_debug_abbrev (segT abbrev_seg,
2717
      segT info_seg ATTRIBUTE_UNUSED,
2718
      segT line_seg ATTRIBUTE_UNUSED,
2719
      unsigned char *func_formP)
2720
37
{
2721
37
  int secoff_form;
2722
37
  bool have_efunc = false, have_lfunc = false;
2723
2724
  /* Check the symbol table for function symbols which also have their size
2725
     specified.  */
2726
37
  if (symbol_rootP)
2727
37
    {
2728
37
      symbolS *symp;
2729
2730
2.19k
      for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2731
2.15k
  {
2732
    /* A warning construct is a warning symbol followed by the
2733
       symbol warned about.  Skip this and the following symbol.  */
2734
2.15k
    if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
2735
0
      {
2736
0
        symp = symbol_next (symp);
2737
0
        if (!symp)
2738
0
          break;
2739
0
        continue;
2740
0
      }
2741
2742
2.15k
    if (!S_IS_DEFINED (symp) || !S_IS_FUNCTION (symp))
2743
2.15k
      continue;
2744
2745
5
#if defined (OBJ_ELF) /* || defined (OBJ_MAYBE_ELF) */
2746
5
    if (S_GET_SIZE (symp) == 0)
2747
4
      {
2748
4
        if (!IS_ELF || symbol_get_obj (symp)->size == NULL)
2749
0
    continue;
2750
4
      }
2751
#else
2752
    continue;
2753
#endif
2754
2755
5
    if (S_IS_EXTERNAL (symp))
2756
4
      have_efunc = true;
2757
1
    else
2758
1
      have_lfunc = true;
2759
5
  }
2760
37
    }
2761
2762
37
  subseg_set (abbrev_seg, 0);
2763
2764
37
  out_uleb128 (GAS_ABBREV_COMP_UNIT);
2765
37
  out_uleb128 (DW_TAG_compile_unit);
2766
37
  out_byte (have_efunc || have_lfunc ? DW_CHILDREN_yes : DW_CHILDREN_no);
2767
37
  if (DWARF2_VERSION < 4)
2768
0
    {
2769
0
      if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit)
2770
0
  secoff_form = DW_FORM_data4;
2771
0
      else
2772
0
  secoff_form = DW_FORM_data8;
2773
0
    }
2774
37
  else
2775
37
    secoff_form = DW_FORM_sec_offset;
2776
37
  out_abbrev (DW_AT_stmt_list, secoff_form);
2777
37
  if (all_segs->next == NULL)
2778
36
    {
2779
36
      out_abbrev (DW_AT_low_pc, DW_FORM_addr);
2780
36
      if (DWARF2_VERSION < 4)
2781
0
  out_abbrev (DW_AT_high_pc, DW_FORM_addr);
2782
36
      else
2783
36
  out_abbrev (DW_AT_high_pc, DW_FORM_udata);
2784
36
    }
2785
1
  else
2786
1
    out_abbrev (DW_AT_ranges, secoff_form);
2787
37
  out_abbrev (DW_AT_name, DW_FORM_strp);
2788
37
  out_abbrev (DW_AT_comp_dir, DW_FORM_strp);
2789
37
  out_abbrev (DW_AT_producer, DW_FORM_strp);
2790
37
  out_abbrev (DW_AT_language, DW_FORM_data2);
2791
37
  out_abbrev (0, 0);
2792
2793
37
  if (have_efunc || have_lfunc)
2794
5
    {
2795
5
      out_uleb128 (GAS_ABBREV_SUBPROG);
2796
5
      out_uleb128 (DW_TAG_subprogram);
2797
5
      out_byte (DW_CHILDREN_no);
2798
5
      out_abbrev (DW_AT_name, DW_FORM_strp);
2799
5
      if (have_efunc)
2800
4
  {
2801
4
    if (have_lfunc || DWARF2_VERSION < 4)
2802
0
      *func_formP = DW_FORM_flag;
2803
4
    else
2804
4
      *func_formP = DW_FORM_flag_present;
2805
4
    out_abbrev (DW_AT_external, *func_formP);
2806
4
  }
2807
1
      else
2808
  /* Any non-zero value other than DW_FORM_flag will do.  */
2809
1
  *func_formP = DW_FORM_block;
2810
2811
      /* PR 29517: Provide a return type for the function.  */
2812
5
      if (DWARF2_VERSION > 2)
2813
5
  out_abbrev (DW_AT_type, DW_FORM_ref_udata);
2814
2815
5
      out_abbrev (DW_AT_low_pc, DW_FORM_addr);
2816
5
      out_abbrev (DW_AT_high_pc,
2817
5
      DWARF2_VERSION < 4 ? DW_FORM_addr : DW_FORM_udata);
2818
5
      out_abbrev (0, 0);
2819
2820
5
      if (DWARF2_VERSION > 2)
2821
5
  {
2822
    /* PR 29517: We do not actually know the return type of these
2823
       functions, so provide an abbrev that uses DWARF's unspecified
2824
       type.  */
2825
5
    out_uleb128 (GAS_ABBREV_NO_TYPE);
2826
5
    out_uleb128 (DW_TAG_unspecified_type);
2827
5
    out_byte (DW_CHILDREN_no);
2828
5
    out_abbrev (0, 0);
2829
5
  }
2830
5
    }
2831
2832
  /* Terminate the abbreviations for this compilation unit.  */
2833
37
  out_byte (0);
2834
37
}
2835
2836
/* Emit a description of this compilation unit for .debug_info.  */
2837
2838
static void
2839
out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT str_seg,
2840
    symbolS *ranges_sym, symbolS *name_sym,
2841
    symbolS *comp_dir_sym, symbolS *producer_sym,
2842
    unsigned char func_form)
2843
37
{
2844
37
  expressionS exp;
2845
37
  symbolS *info_end;
2846
37
  int sizeof_offset;
2847
2848
37
  memset (&exp, 0, sizeof exp);
2849
37
  sizeof_offset = out_header (info_seg, &exp);
2850
37
  info_end = exp.X_add_symbol;
2851
2852
  /* DWARF version.  */
2853
37
  out_two (DWARF2_VERSION);
2854
2855
37
  if (DWARF2_VERSION < 5)
2856
0
    {
2857
      /* .debug_abbrev offset */
2858
0
      TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
2859
0
    }
2860
37
  else
2861
37
    {
2862
      /* unit (header) type */
2863
37
      out_byte (DW_UT_compile);
2864
37
    }
2865
2866
  /* Target address size.  */
2867
37
  out_byte (sizeof_address);
2868
2869
37
  if (DWARF2_VERSION >= 5)
2870
37
    {
2871
      /* .debug_abbrev offset */
2872
37
      TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
2873
37
    }
2874
2875
  /* DW_TAG_compile_unit DIE abbrev */
2876
37
  out_uleb128 (GAS_ABBREV_COMP_UNIT);
2877
2878
  /* DW_AT_stmt_list */
2879
37
  TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg),
2880
37
       (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit
2881
37
        ? 4 : 8));
2882
2883
  /* These two attributes are emitted if all of the code is contiguous.  */
2884
37
  if (all_segs->next == NULL)
2885
36
    {
2886
      /* DW_AT_low_pc */
2887
36
      exp.X_op = O_symbol;
2888
36
      exp.X_add_symbol = all_segs->text_start;
2889
36
      exp.X_add_number = 0;
2890
36
      emit_expr (&exp, sizeof_address);
2891
2892
      /* DW_AT_high_pc */
2893
36
      if (DWARF2_VERSION < 4)
2894
0
  exp.X_op = O_symbol;
2895
36
      else
2896
36
  {
2897
36
    exp.X_op = O_subtract;
2898
36
    exp.X_op_symbol = all_segs->text_start;
2899
36
  }
2900
36
      exp.X_add_symbol = all_segs->text_end;
2901
36
      exp.X_add_number = 0;
2902
36
      if (DWARF2_VERSION < 4)
2903
0
  emit_expr (&exp, sizeof_address);
2904
36
      else
2905
36
  emit_leb128_expr (&exp, 0);
2906
36
    }
2907
1
  else
2908
1
    {
2909
      /* This attribute is emitted if the code is disjoint.  */
2910
      /* DW_AT_ranges.  */
2911
1
      TC_DWARF2_EMIT_OFFSET (ranges_sym, sizeof_offset);
2912
1
    }
2913
2914
  /* DW_AT_name, DW_AT_comp_dir and DW_AT_producer.  Symbols in .debug_str
2915
     setup in out_debug_str below.  */
2916
37
  TC_DWARF2_EMIT_OFFSET (name_sym, sizeof_offset);
2917
37
  TC_DWARF2_EMIT_OFFSET (comp_dir_sym, sizeof_offset);
2918
37
  TC_DWARF2_EMIT_OFFSET (producer_sym, sizeof_offset);
2919
2920
  /* DW_AT_language.  Yes, this is probably not really MIPS, but the
2921
     dwarf2 draft has no standard code for assembler.  */
2922
37
  out_two (DW_LANG_Mips_Assembler);
2923
2924
37
  if (func_form)
2925
5
    {
2926
5
      symbolS *symp;
2927
5
      symbolS *no_type_tag;
2928
2929
5
      if (DWARF2_VERSION > 2)
2930
5
  no_type_tag = symbol_make (".Ldebug_no_type_tag");
2931
0
      else
2932
0
  no_type_tag = NULL;
2933
2934
731
      for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2935
726
  {
2936
726
    const char *name;
2937
726
    size_t len;
2938
726
    expressionS size = { .X_op = O_constant };
2939
2940
    /* Skip warning constructs (see above).  */
2941
726
    if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
2942
0
      {
2943
0
        symp = symbol_next (symp);
2944
0
        if (!symp)
2945
0
          break;
2946
0
        continue;
2947
0
      }
2948
2949
726
    if (!S_IS_DEFINED (symp) || !S_IS_FUNCTION (symp))
2950
721
      continue;
2951
2952
5
#if defined (OBJ_ELF) /* || defined (OBJ_MAYBE_ELF) */
2953
5
    size.X_add_number = S_GET_SIZE (symp);
2954
5
    if (size.X_add_number == 0 && IS_ELF
2955
4
        && symbol_get_obj (symp)->size != NULL)
2956
4
      {
2957
4
        size.X_op = O_add;
2958
4
        size.X_op_symbol = make_expr_symbol (symbol_get_obj (symp)->size);
2959
4
      }
2960
5
#endif
2961
5
    if (size.X_op == O_constant && size.X_add_number == 0)
2962
0
      continue;
2963
2964
5
    subseg_set (str_seg, 0);
2965
5
    name_sym = symbol_temp_new_now_octets ();
2966
5
    name = S_GET_NAME (symp);
2967
5
    len = strlen (name) + 1;
2968
5
    memcpy (frag_more (len), name, len);
2969
2970
5
    subseg_set (info_seg, 0);
2971
2972
    /* DW_TAG_subprogram DIE abbrev */
2973
5
    out_uleb128 (GAS_ABBREV_SUBPROG);
2974
2975
    /* DW_AT_name */
2976
5
    TC_DWARF2_EMIT_OFFSET (name_sym, sizeof_offset);
2977
2978
    /* DW_AT_external.  */
2979
5
    if (func_form == DW_FORM_flag)
2980
0
      out_byte (S_IS_EXTERNAL (symp));
2981
2982
    /* PR 29517: Let consumers know that we do not have
2983
       return type information for this function.  */
2984
5
    if (DWARF2_VERSION > 2)
2985
5
      {
2986
5
        exp.X_op = O_symbol;
2987
5
        exp.X_add_symbol = no_type_tag;
2988
5
        exp.X_add_number = 0;
2989
5
        emit_leb128_expr (&exp, 0);
2990
5
      }
2991
2992
    /* DW_AT_low_pc */
2993
5
    exp.X_op = O_symbol;
2994
5
    exp.X_add_symbol = symp;
2995
5
    exp.X_add_number = 0;
2996
5
    emit_expr (&exp, sizeof_address);
2997
2998
    /* DW_AT_high_pc */
2999
5
    if (DWARF2_VERSION < 4)
3000
0
      {
3001
0
        if (size.X_op == O_constant)
3002
0
    size.X_op = O_symbol;
3003
0
        size.X_add_symbol = symp;
3004
0
        emit_expr (&size, sizeof_address);
3005
0
      }
3006
5
    else if (size.X_op == O_constant)
3007
1
      out_uleb128 (size.X_add_number);
3008
4
    else
3009
4
      emit_leb128_expr (symbol_get_value_expression (size.X_op_symbol), 0);
3010
5
  }
3011
3012
5
      if (DWARF2_VERSION > 2)
3013
5
  {
3014
    /* PR 29517: Generate a DIE for the unspecified type abbrev.
3015
       We do it here because it cannot be part of the top level DIE.   */
3016
5
    subseg_set (info_seg, 0);
3017
5
    symbol_set_value_now (no_type_tag);
3018
5
    out_uleb128 (GAS_ABBREV_NO_TYPE);
3019
5
  }
3020
3021
      /* End of children.  */
3022
5
      out_leb128 (0);
3023
5
    }
3024
3025
37
  symbol_set_value_now (info_end);
3026
37
}
3027
3028
/* Emit the three debug strings needed in .debug_str and setup symbols
3029
   to them for use in out_debug_info.  */
3030
static void
3031
out_debug_str (segT str_seg, symbolS **name_sym, symbolS **comp_dir_sym,
3032
         symbolS **producer_sym)
3033
37
{
3034
37
  char producer[128];
3035
37
  char *p;
3036
37
  int len;
3037
37
  int first_file = DWARF2_LINE_VERSION > 4 ? 0 : 1;
3038
3039
37
  if (files_in_use == 0)
3040
0
    abort ();
3041
37
  if (first_file == 0 && files[first_file].filename == NULL)
3042
0
    first_file = 1;
3043
3044
37
  subseg_set (str_seg, 0);
3045
3046
  /* DW_AT_name.  We don't have the actual file name that was present
3047
     on the command line, so assume files[first_file] is the main input file.
3048
     We're not supposed to get called unless at least one line number
3049
     entry was emitted, so this should always be defined.  */
3050
37
  *name_sym = symbol_temp_new_now_octets ();
3051
3052
37
  if (files[first_file].dir)
3053
6
    {
3054
6
      char *dirname = remap_debug_filename (dirs[files[first_file].dir]);
3055
6
      len = strlen (dirname);
3056
#ifdef TE_VMS
3057
      /* Already has trailing slash.  */
3058
      p = frag_more (len);
3059
      memcpy (p, dirname, len);
3060
#else
3061
6
      p = frag_more (len + 1);
3062
6
      memcpy (p, dirname, len);
3063
6
      INSERT_DIR_SEPARATOR (p, len);
3064
6
#endif
3065
6
      free (dirname);
3066
6
    }
3067
37
  if (files[first_file].filename)
3068
37
    {
3069
37
      len = strlen (files[first_file].filename) + 1;
3070
37
      p = frag_more (len);
3071
37
      memcpy (p, files[first_file].filename, len);
3072
37
    }
3073
0
  else
3074
0
    frag_append_1_char (0);
3075
3076
  /* DW_AT_comp_dir */
3077
37
  *comp_dir_sym = symbol_temp_new_now_octets ();
3078
37
  char *comp_dir = remap_debug_filename (getpwd ());
3079
37
  len = strlen (comp_dir) + 1;
3080
37
  p = frag_more (len);
3081
37
  memcpy (p, comp_dir, len);
3082
37
  free (comp_dir);
3083
3084
  /* DW_AT_producer */
3085
37
  *producer_sym = symbol_temp_new_now_octets ();
3086
37
  sprintf (producer, "GNU AS %s", VERSION);
3087
37
  len = strlen (producer) + 1;
3088
37
  p = frag_more (len);
3089
37
  memcpy (p, producer, len);
3090
37
}
3091
3092
void
3093
dwarf2_init (void)
3094
584
{
3095
584
  all_segs = NULL;
3096
584
  last_seg_ptr = &all_segs;
3097
584
  files = NULL;
3098
584
  files_in_use = 0;
3099
584
  files_allocated = 0;
3100
584
  dirs = NULL;
3101
584
  dirs_in_use = 0;
3102
584
  dirs_allocated = 0;
3103
584
  dwarf2_loc_directive_seen = false;
3104
584
  dwarf2_any_loc_directive_seen = false;
3105
584
  dwarf2_loc_mark_labels = false;
3106
584
  current.filenum = 1;
3107
584
  current.line = 1;
3108
584
  current.column = 0;
3109
584
  current.isa = 0;
3110
584
  current.flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
3111
584
  current.discriminator = 0;
3112
584
  current.u.view = NULL;
3113
584
  force_reset_view = NULL;
3114
584
  view_assert_failed = NULL;
3115
584
  dw2_line = -1;
3116
584
  dw2_filename = NULL;
3117
584
  label_num = 0;
3118
584
  last_used = -1;
3119
3120
  /* Select the default CIE version to produce here.  The global
3121
     starts with a value of -1 and will be modified to a valid value
3122
     either by the user providing a command line option, or some
3123
     targets will select their own default in md_after_parse_args.  If
3124
     we get here and the global still contains -1 then it is up to us
3125
     to pick a sane default.  The default we choose is 1, this is the
3126
     CIE version gas has produced for a long time, and there seems no
3127
     reason to change it yet.  */
3128
584
  if (flag_dwarf_cie_version == -1)
3129
1
    flag_dwarf_cie_version = 1;
3130
584
}
3131
3132
static void
3133
dwarf2_cleanup (void)
3134
584
{
3135
584
  purge_generated_debug (true);
3136
584
  free (files);
3137
676
  for (unsigned int i = 0; i < dirs_in_use; i++)
3138
92
    free (dirs[i]);
3139
584
  free (dirs);
3140
584
}
3141
3142
/* Finish the dwarf2 debug sections.  We emit .debug.line if there
3143
   were any .file/.loc directives, or --gdwarf2 was given, and if the
3144
   file has a non-empty .debug_info section and an empty .debug_line
3145
   section.  If we emit .debug_line, and the .debug_info section is
3146
   empty, we also emit .debug_info, .debug_aranges and .debug_abbrev.
3147
   ALL_SEGS will be non-null if there were any .file/.loc directives,
3148
   or --gdwarf2 was given and there were any located instructions
3149
   emitted.  */
3150
3151
void
3152
dwarf2_finish (void)
3153
584
{
3154
584
  segT line_seg;
3155
584
  struct line_seg *s;
3156
584
  segT info_seg;
3157
584
  int emit_other_sections = 0;
3158
584
  int empty_debug_line = 0;
3159
3160
584
  info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
3161
584
  emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
3162
3163
584
  line_seg = bfd_get_section_by_name (stdoutput, ".debug_line");
3164
584
  empty_debug_line = line_seg == NULL || !seg_not_empty_p (line_seg);
3165
3166
  /* We can't construct a new debug_line section if we already have one.
3167
     Give an error if we have seen any .loc, otherwise trust the user
3168
     knows what they are doing and want to generate the .debug_line
3169
     (and all other debug sections) themselves.  */
3170
584
  if (all_segs && !empty_debug_line && dwarf2_any_loc_directive_seen)
3171
0
    as_fatal ("duplicate .debug_line sections");
3172
3173
584
  if ((!all_segs && emit_other_sections)
3174
41
      || (!emit_other_sections && !empty_debug_line))
3175
    /* If there is no line information and no non-empty .debug_info
3176
       section, or if there is both a non-empty .debug_info and a non-empty
3177
       .debug_line, then we do nothing.  */
3178
543
    {
3179
543
      dwarf2_cleanup ();
3180
543
      return;
3181
543
    }
3182
3183
  /* Calculate the size of an address for the target machine.  */
3184
41
  sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
3185
3186
  /* Create and switch to the line number section.  */
3187
41
  if (empty_debug_line)
3188
41
    {
3189
41
      line_seg = subseg_new (".debug_line", 0);
3190
41
      bfd_set_section_flags (line_seg,
3191
41
           SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS);
3192
41
    }
3193
3194
79
  for (s = all_segs; s; s = s->next)
3195
38
    {
3196
38
      struct line_subseg *lss;
3197
3198
78
      for (lss = s->head; lss; lss = lss->next)
3199
40
  if (lss->head)
3200
40
    do_allocate_filenum (lss->head);
3201
38
    }
3202
3203
  /* For each subsection, chain the debug entries together.  */
3204
79
  for (s = all_segs; s; s = s->next)
3205
38
    {
3206
38
      struct line_subseg *lss = s->head;
3207
38
      struct line_entry **ptail = lss->ptail;
3208
3209
      /* Reset the initial view of the first subsection of the
3210
   section.  */
3211
38
      if (lss->head && lss->head->loc.u.view)
3212
16
  set_or_check_view (lss->head, NULL, NULL);
3213
3214
40
      while ((lss = lss->next) != NULL)
3215
2
  {
3216
    /* Link the first view of subsequent subsections to the
3217
       previous view.  */
3218
2
    if (lss->head && lss->head->loc.u.view)
3219
2
      set_or_check_view (lss->head, line_entry_at_tail (s->head, ptail),
3220
2
             s->head ? s->head->head : NULL);
3221
2
    *ptail = lss->head;
3222
2
    lss->head = NULL;
3223
2
    ptail = lss->ptail;
3224
2
  }
3225
38
    }
3226
3227
41
  if (empty_debug_line)
3228
41
    out_debug_line (line_seg);
3229
3230
  /* If this is assembler generated line info, and there is no
3231
     debug_info already, we need .debug_info, .debug_abbrev and
3232
     .debug_str sections as well.  */
3233
41
  if (emit_other_sections)
3234
37
    {
3235
37
      segT abbrev_seg;
3236
37
      segT aranges_seg;
3237
37
      segT str_seg;
3238
37
      symbolS *name_sym, *comp_dir_sym, *producer_sym, *ranges_sym;
3239
37
      unsigned char func_form = 0;
3240
3241
37
      gas_assert (all_segs);
3242
3243
37
      info_seg = subseg_new (".debug_info", 0);
3244
37
      abbrev_seg = subseg_new (".debug_abbrev", 0);
3245
37
      aranges_seg = subseg_new (".debug_aranges", 0);
3246
37
      str_seg = subseg_new (".debug_str", 0);
3247
3248
37
      bfd_set_section_flags (info_seg,
3249
37
            SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS);
3250
37
      bfd_set_section_flags (abbrev_seg,
3251
37
            SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS);
3252
37
      bfd_set_section_flags (aranges_seg,
3253
37
            SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS);
3254
37
      bfd_set_section_flags (str_seg,
3255
37
            SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS
3256
37
               | SEC_MERGE | SEC_STRINGS);
3257
37
      str_seg->entsize = 1;
3258
3259
37
      record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
3260
3261
37
      if (all_segs->next == NULL)
3262
36
  ranges_sym = NULL;
3263
1
      else
3264
1
  {
3265
1
    if (DWARF2_VERSION < 5)
3266
0
      {
3267
0
        segT ranges_seg = subseg_new (".debug_ranges", 0);
3268
0
        bfd_set_section_flags (ranges_seg, (SEC_READONLY
3269
0
              | SEC_DEBUGGING
3270
0
              | SEC_OCTETS));
3271
0
        record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1);
3272
0
        out_debug_ranges (ranges_seg, &ranges_sym);
3273
0
      }
3274
1
    else
3275
1
      {
3276
1
        segT rnglists_seg = subseg_new (".debug_rnglists", 0);
3277
1
        bfd_set_section_flags (rnglists_seg, (SEC_READONLY
3278
1
                | SEC_DEBUGGING
3279
1
                | SEC_OCTETS));
3280
1
        out_debug_rnglists (rnglists_seg, &ranges_sym);
3281
1
      }
3282
1
  }
3283
3284
37
      out_debug_aranges (aranges_seg, info_seg);
3285
37
      out_debug_abbrev (abbrev_seg, info_seg, line_seg, &func_form);
3286
37
      out_debug_str (str_seg, &name_sym, &comp_dir_sym, &producer_sym);
3287
37
      out_debug_info (info_seg, abbrev_seg, line_seg, str_seg,
3288
37
          ranges_sym, name_sym, comp_dir_sym, producer_sym,
3289
37
          func_form);
3290
37
    }
3291
41
  dwarf2_cleanup ();
3292
41
}
3293
3294
/* Perform any deferred checks pertaining to debug information.  */
3295
3296
void
3297
dwarf2dbg_final_check (void)
3298
0
{
3299
  /* Perform reset-view checks.  Don't evaluate view_assert_failed
3300
     recursively: it could be very deep.  It's a chain of adds, with
3301
     each chain element pointing to the next in X_add_symbol, and
3302
     holding the check value in X_op_symbol.  */
3303
0
  while (view_assert_failed)
3304
0
    {
3305
0
      expressionS *exp;
3306
0
      symbolS *sym;
3307
0
      offsetT failed;
3308
3309
0
      gas_assert (!symbol_resolved_p (view_assert_failed));
3310
3311
0
      exp = symbol_get_value_expression (view_assert_failed);
3312
0
      sym = view_assert_failed;
3313
3314
      /* If view_assert_failed looks like a compound check in the
3315
   chain, break it up.  */
3316
0
      if (exp->X_op == O_add && exp->X_add_number == 0 && exp->X_unsigned)
3317
0
  {
3318
0
    view_assert_failed = exp->X_add_symbol;
3319
0
    sym = exp->X_op_symbol;
3320
0
  }
3321
0
      else
3322
0
  view_assert_failed = NULL;
3323
3324
0
      failed = resolve_symbol_value (sym);
3325
0
      if (!symbol_resolved_p (sym) || failed)
3326
0
  {
3327
    as_bad (_("view number mismatch"));
3328
0
    break;
3329
0
  }
3330
0
    }
3331
0
}