Coverage Report

Created: 2026-11-21 09:00

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