Coverage Report

Created: 2023-08-28 06:31

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