Coverage Report

Created: 2026-05-11 07:54

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