Coverage Report

Created: 2023-06-29 07:09

/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
34
# define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
58
#endif
59
60
#ifndef DWARF2_FORMAT
61
791
# 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
242
#define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME
70
#endif
71
72
#ifndef DWARF2_FILE_TIME_NAME
73
200
#define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) -1
74
#endif
75
76
#ifndef DWARF2_FILE_SIZE_NAME
77
200
#define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) -1
78
#endif
79
80
#ifndef DWARF2_VERSION
81
1.45M
#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
197
#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.45M
#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.40k
# 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
15.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
14.2k
# 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
25.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
11.4k
# 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.03k
#define DWARF2_LINE_DEFAULT_IS_STMT 1
143
144
#ifndef DWARF2_LINE_MAX_OPS_PER_INSN
145
200
#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.30k
#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.30k
#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
394
#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
862
#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
1.78k
#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
1.78k
{
256
1.78k
  expressionS exp;
257
258
1.78k
  memset (&exp, 0, sizeof exp);
259
1.78k
  exp.X_op = O_symbol;
260
1.78k
  exp.X_add_symbol = symbol;
261
1.78k
  exp.X_add_number = 0;
262
1.78k
  emit_expr (&exp, size);
263
1.78k
}
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.40k
{
271
5.40k
  struct line_seg *s = seg_info (seg)->dwarf2_line_seg;
272
5.40k
  struct line_subseg **pss, *lss;
273
274
5.40k
  if (s == NULL)
275
197
    {
276
197
      if (!create_p)
277
0
  return NULL;
278
279
197
      s = XNEW (struct line_seg);
280
197
      s->next = NULL;
281
197
      s->seg = seg;
282
197
      s->head = NULL;
283
197
      *last_seg_ptr = s;
284
197
      last_seg_ptr = &s->next;
285
197
      seg_info (seg)->dwarf2_line_seg = s;
286
197
    }
287
288
5.40k
  gas_assert (seg == s->seg);
289
290
9.01k
  for (pss = &s->head; (lss = *pss) != NULL ; pss = &lss->next)
291
8.76k
    {
292
8.76k
      if (lss->subseg == subseg)
293
5.06k
  goto found_subseg;
294
3.70k
      if (lss->subseg > subseg)
295
91
  break;
296
3.70k
    }
297
298
343
  lss = XNEW (struct line_subseg);
299
343
  lss->next = *pss;
300
343
  lss->subseg = subseg;
301
343
  lss->head = NULL;
302
343
  lss->ptail = &lss->head;
303
343
  lss->pmove_tail = &lss->head;
304
343
  *pss = lss;
305
306
5.40k
 found_subseg:
307
5.40k
  return lss;
308
343
}
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
5.81k
{
503
5.81k
  struct line_subseg *lss;
504
5.81k
  struct line_entry *e;
505
5.81k
  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
5.81k
  if (IS_ELF)
511
5.81k
    need_flags |= SEC_ALLOC;
512
5.81k
  if ((now_seg->flags & need_flags) != need_flags)
513
412
    {
514
      /* FIXME: Add code to suppress multiple warnings ?  */
515
412
      if (debug_type != DEBUG_DWARF2)
516
412
  as_warn ("dwarf line number information for %s ignored",
517
412
     segment_name (now_seg));
518
412
      return;
519
412
    }
520
521
5.40k
  e = XNEW (struct line_entry);
522
5.40k
  e->next = NULL;
523
5.40k
  e->label = label;
524
5.40k
  e->loc = *loc;
525
526
5.40k
  lss = get_line_subseg (now_seg, now_subseg, true);
527
528
  /* Subseg heads are chained to previous subsegs in
529
     dwarf2_finish.  */
530
5.40k
  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.40k
  *lss->ptail = e;
534
5.40k
  lss->ptail = &e->next;
535
5.40k
}
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
11.2k
{
546
11.2k
  symbolS *sym;
547
548
  /* Early out for as-yet incomplete location information.  */
549
11.2k
  if (loc->line == 0)
550
5.47k
    return;
551
5.81k
  if (loc->filenum == 0)
552
5.81k
    {
553
5.81k
      if (dwarf_level < 5)
554
0
  dwarf_level = 5;
555
5.81k
      if (DWARF2_LINE_VERSION < 5)
556
0
  return;
557
5.81k
    }
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
5.81k
  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
5.81k
  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
5.81k
  else
591
5.81k
    sym = symbol_temp_new (now_seg, frag_now, ofs);
592
5.81k
  dwarf2_gen_line_info_1 (sym, loc);
593
5.81k
}
594
595
static const char *
596
get_basename (const char * pathname)
597
11.3k
{
598
11.3k
  const char * file;
599
600
11.3k
  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
11.3k
  if (file == pathname + 1)
607
39
    file = pathname;
608
11.3k
#endif
609
11.3k
  return file;
610
11.3k
}
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
2.18k
{
618
2.18k
  unsigned int d;
619
620
2.18k
  if (dirlen == 0)
621
549
    return 0;
622
623
1.63k
#ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR
624
1.63k
  if (IS_DIR_SEPARATOR (dirname[dirlen - 1]))
625
1.17k
    {
626
1.17k
      -- dirlen;
627
1.17k
      if (dirlen == 0)
628
0
  return 0;
629
1.17k
    }
630
1.63k
#endif
631
632
7.87k
  for (d = 0; d < dirs_in_use; ++d)
633
7.29k
    {
634
7.29k
      if (dirs[d] != NULL
635
7.29k
    && filename_ncmp (dirname, dirs[d], dirlen) == 0
636
7.29k
    && dirs[d][dirlen] == '\0')
637
1.05k
  return d;
638
7.29k
    }
639
640
578
  if (can_use_zero)
641
144
    {
642
144
      if (dirs == NULL || dirs[0] == NULL)
643
144
  {
644
144
    const char * pwd = file0_dirname ? file0_dirname : getpwd ();
645
646
144
    if (dwarf_level >= 5 && filename_cmp (dirname, pwd) != 0)
647
47
      {
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
47
        (void) get_directory_table_entry (pwd, file0_dirname,
654
47
            strlen (pwd), true);
655
47
        d = 1;
656
47
      }
657
97
    else
658
97
      d = 0;
659
144
  }
660
144
    }
661
434
  else if (d == 0)
662
90
    d = 1;
663
664
578
  if (d >= dirs_allocated)
665
177
    {
666
177
      unsigned int old = dirs_allocated;
667
179
#define DIR_TABLE_INCREMENT 32
668
177
      dirs_allocated = d + DIR_TABLE_INCREMENT;
669
177
      dirs = XRESIZEVEC (char *, dirs, dirs_allocated);
670
177
      memset (dirs + old, 0, (dirs_allocated - old) * sizeof (char *));
671
177
    }
672
673
578
  dirs[d] = xmemdup0 (dirname, dirlen);
674
578
  if (dirs_in_use <= d)
675
561
    dirs_in_use = d + 1;
676
677
578
  return d;  
678
1.63k
}
679
680
static bool
681
assign_file_to_slot (unsigned int i, const char *file, unsigned int dir)
682
2.13k
{
683
2.13k
  if (i >= files_allocated)
684
2.09k
    {
685
2.09k
      unsigned int want = i + 32;
686
687
      /* Catch wraparound.  */
688
2.09k
      if (want < files_allocated
689
2.09k
    || want < i
690
2.09k
    || want > UINT_MAX / sizeof (struct file_entry))
691
1.70k
  {
692
1.70k
    as_bad (_("file number %u is too big"), i);
693
1.70k
    return false;
694
1.70k
  }
695
696
396
      files = XRESIZEVEC (struct file_entry, files, want);
697
396
      memset (files + files_allocated, 0,
698
396
        (want - files_allocated) * sizeof (struct file_entry));
699
396
      files_allocated = want;
700
396
    }
701
702
431
  files[i].filename = file;
703
431
  files[i].dir = dir;
704
431
  memset (files[i].md5, 0, NUM_MD5_BYTES);
705
706
431
  if (files_in_use < i + 1)
707
408
    files_in_use = i + 1;
708
709
431
  return true;
710
2.13k
}
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
343
{
784
343
  do
785
5.40k
    {
786
5.40k
      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.40k
      e = e->next;
792
5.40k
    }
793
5.40k
  while (e);
794
343
}
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
633
{
803
633
  struct line_seg *s, *nexts;
804
805
830
  for (s = all_segs; s; s = nexts)
806
197
    {
807
197
      struct line_subseg *lss, *nextlss;
808
809
540
      for (lss = s->head; lss; lss = nextlss)
810
343
  {
811
343
    struct line_entry *e, *next;
812
813
5.74k
    for (e = lss->head; e; e = next)
814
5.40k
      {
815
5.40k
        if (!thelot)
816
5.40k
    know (e->loc.filenum == -1u);
817
0
        next = e->next;
818
5.40k
        free (e);
819
5.40k
      }
820
821
343
    lss->head = NULL;
822
343
    lss->ptail = &lss->head;
823
343
    lss->pmove_tail = &lss->head;
824
343
    nextlss = lss->next;
825
343
    if (thelot)
826
343
      free (lss);
827
343
  }
828
197
      nexts = s->next;
829
197
      if (thelot)
830
197
  {
831
197
    seg_info (s->seg)->dwarf2_line_seg = NULL;
832
197
    free (s);
833
197
  }
834
197
    }
835
633
}
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
16.2k
{
849
16.2k
  const char *file;
850
16.2k
  size_t dirlen;
851
16.2k
  unsigned int i, d;
852
16.2k
  const char *file0_dirname;
853
854
  /* Short circuit the common case of adding the same pathname
855
     as last time.  */
856
16.2k
  if (num < files_allocated && files[num].filename != NULL)
857
14.0k
    {
858
14.0k
      const char * dir = NULL;
859
860
14.0k
      if (dirs != NULL)
861
6.12k
  dir = dirs[files[num].dir];
862
863
14.0k
      if (with_md5
864
14.0k
    && memcmp (generic_bignum, files[num].md5, NUM_MD5_BYTES) != 0)
865
0
  goto fail;
866
867
14.0k
      if (dirname != NULL)
868
1.50k
  {
869
1.50k
    if (dir != NULL && filename_cmp (dir, dirname) != 0)
870
666
      goto fail;
871
      
872
839
    if (filename_cmp (filename, files[num].filename) != 0)
873
582
      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
257
    if (dir == NULL)
878
2
      {
879
2
        if (dirs == NULL)
880
2
    {
881
2
      dirs_allocated = files[num].dir + DIR_TABLE_INCREMENT;
882
2
      dirs = XCNEWVEC (char *, dirs_allocated);
883
2
    }
884
        
885
2
        dirs[files[num].dir] = xmemdup0 (dirname, strlen (dirname));
886
2
      }
887
      
888
257
    return true;
889
839
  }
890
12.5k
      else if (dir != NULL) 
891
2.97k
  {
892
2.97k
    dirlen = strlen (dir);
893
2.97k
    if (filename_ncmp (filename, dir, dirlen) == 0
894
2.97k
        && IS_DIR_SEPARATOR (filename [dirlen])
895
2.97k
        && filename_cmp (filename + dirlen + 1, files[num].filename) == 0)
896
154
      return true;
897
2.97k
  }
898
9.59k
      else /* dir == NULL  */
899
9.59k
  {
900
9.59k
    file = get_basename (filename);
901
9.59k
    if (filename_cmp (file, files[num].filename) == 0)
902
2.78k
      {
903
        /* The filenames match, but the directory table entry is empty.
904
     Fill it with the provided directory name.  */
905
2.78k
        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
2.78k
        return true;
916
2.78k
      }
917
9.59k
  }
918
919
10.8k
    fail:
920
10.8k
      as_bad (_("file table slot %u is already occupied by a different file (%s%s%s vs %s%s%s)"),
921
10.8k
        num,
922
10.8k
        dir == NULL ? "" : dir,
923
10.8k
        dir == NULL ? "" : "/",
924
10.8k
        files[num].filename,
925
10.8k
        dirname == NULL ? "" : dirname,
926
10.8k
        dirname == NULL ? "" : "/",
927
10.8k
        filename);
928
10.8k
      return false;
929
14.0k
    }
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
2.13k
  if (num == 0)
934
339
    {
935
339
      file0_dirname = dirname;
936
937
339
      file = get_basename (filename);
938
939
339
      if (dirname && file == filename)
940
50
  dirlen = strlen (dirname);
941
289
      else
942
289
  {
943
289
    dirname = filename;
944
289
    dirlen = file - filename;
945
289
  }
946
339
    }
947
1.79k
  else
948
1.79k
    {
949
1.79k
      file0_dirname = NULL;
950
951
1.79k
      if (dirname == NULL)
952
1.43k
  {
953
1.43k
    dirname = filename;
954
1.43k
    file = get_basename (filename);
955
1.43k
    dirlen = file - filename;
956
1.43k
  }
957
356
      else
958
356
  {
959
356
    dirlen = strlen (dirname);
960
356
    file = filename;
961
356
  }
962
1.79k
    }
963
964
2.13k
  d = get_directory_table_entry (dirname, file0_dirname, dirlen, num == 0);
965
2.13k
  i = num;
966
967
2.13k
  if (! assign_file_to_slot (i, file, d))
968
1.70k
    return false;
969
970
431
  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
431
  else
1018
431
    memset (files[i].md5, 0, NUM_MD5_BYTES);
1019
1020
431
  return true;
1021
2.13k
}
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
11.2k
{
1031
11.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
11.2k
  else
1041
11.2k
    *line = current;
1042
11.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
197k
{
1061
197k
  struct dwarf2_line_info loc;
1062
1063
197k
  if (debug_type != DEBUG_DWARF2
1064
197k
      ? !dwarf2_loc_directive_seen
1065
197k
      : !seen_at_least_1_file ())
1066
185k
    return;
1067
1068
11.2k
  dwarf2_where (&loc);
1069
1070
11.2k
  dwarf2_gen_line_info ((frag_now_fix_octets () - size) / OCTETS_PER_BYTE, &loc);
1071
11.2k
  dwarf2_consume_line_info ();
1072
11.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
11.2k
{
1109
  /* Unless we generate DWARF2 debugging information for each
1110
     assembler line, we only emit one line symbol for one LOC.  */
1111
11.2k
  dwarf2_loc_directive_seen = false;
1112
1113
11.2k
  current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
1114
11.2k
         | DWARF2_FLAG_PROLOGUE_END
1115
11.2k
         | DWARF2_FLAG_EPILOGUE_BEGIN);
1116
11.2k
  current.discriminator = 0;
1117
11.2k
  current.u.view = NULL;
1118
11.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
97.2k
{
1126
97.2k
  struct dwarf2_line_info loc;
1127
1128
97.2k
  if (!dwarf2_loc_mark_labels)
1129
97.2k
    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
27.1k
{
1154
27.1k
  bool with_md5 = false;
1155
27.1k
  valueT num;
1156
27.1k
  char *filename;
1157
27.1k
  const char * dirname = NULL;
1158
27.1k
  int filename_len;
1159
1160
  /* Continue to accept a bare string and pass it off.  */
1161
27.1k
  SKIP_WHITESPACE ();
1162
27.1k
  if (*input_line_pointer == '"')
1163
1.37k
    {
1164
1.37k
      s_file (0);
1165
1.37k
      return NULL;
1166
1.37k
    }
1167
1168
25.8k
  num = get_absolute_expression ();
1169
1170
25.8k
  if ((offsetT) num < 1)
1171
22.9k
    {
1172
22.9k
      if (num == 0 && dwarf_level < 5)
1173
1
  dwarf_level = 5;
1174
22.9k
      if ((offsetT) num < 0 || DWARF2_LINE_VERSION < 5)
1175
74
  {
1176
74
    as_bad (_("file number less than one"));
1177
74
    ignore_rest_of_line ();
1178
74
    return NULL;
1179
74
  }
1180
22.9k
    }
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
25.7k
  filename = demand_copy_C_string (&filename_len);
1187
25.7k
  if (filename == NULL)
1188
    /* demand_copy_C_string will have already generated an error message.  */
1189
9.28k
    return NULL;
1190
1191
  /* For DWARF-5 support we also accept:
1192
     .file <NUM> ["<dir>"] "<file>" [md5 <NUM>]  */
1193
16.4k
  if (DWARF2_LINE_VERSION > 4)
1194
16.4k
    {
1195
16.4k
      SKIP_WHITESPACE ();
1196
16.4k
      if (*input_line_pointer == '"')
1197
1.94k
  {
1198
1.94k
    dirname = filename;
1199
1.94k
    filename = demand_copy_C_string (&filename_len);
1200
1.94k
    if (filename == NULL)
1201
1
      return NULL;
1202
1.93k
    SKIP_WHITESPACE ();
1203
1.93k
  }
1204
1205
16.4k
      if (startswith (input_line_pointer, "md5"))
1206
12
  {
1207
12
    input_line_pointer += 3;
1208
12
    SKIP_WHITESPACE ();
1209
1210
12
    expressionS exp;
1211
12
    expression_and_evaluate (& exp);
1212
12
    if (exp.X_op != O_big)
1213
12
      as_bad (_("md5 value too small or not a constant"));
1214
0
    else
1215
0
      with_md5 = true;
1216
12
  }
1217
16.4k
    }
1218
1219
16.4k
  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
16.4k
  if (debug_type == DEBUG_DWARF2)
1224
0
    purge_generated_debug (false);
1225
16.4k
  debug_type = DEBUG_NONE;
1226
1227
16.4k
  if (num != (unsigned int) num
1228
16.4k
      || num >= (size_t) -1 / sizeof (struct file_entry) - 32)
1229
242
    {
1230
242
      as_bad (_("file number %lu is too big"), (unsigned long) num);
1231
242
      return NULL;
1232
242
    }
1233
1234
16.2k
  if (! allocate_filename_to_slot (dirname, filename, (unsigned int) num,
1235
16.2k
           with_md5))
1236
12.5k
    return NULL;
1237
1238
3.62k
  return filename;
1239
16.2k
}
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
27.1k
{
1247
27.1k
  (void) dwarf2_directive_filename ();
1248
27.1k
}
1249
1250
void
1251
dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
1252
16.9k
{
1253
16.9k
  offsetT filenum, line;
1254
1255
  /* If we see two .loc directives in a row, force the first one to be
1256
     output now.  */
1257
16.9k
  if (dwarf2_loc_directive_seen)
1258
9.95k
    dwarf2_emit_insn (0);
1259
1260
16.9k
  filenum = get_absolute_expression ();
1261
16.9k
  SKIP_WHITESPACE ();
1262
16.9k
  line = get_absolute_expression ();
1263
1264
16.9k
  if (filenum < 1)
1265
14.7k
    {
1266
14.7k
      if (filenum == 0 && dwarf_level < 5)
1267
0
  dwarf_level = 5;
1268
14.7k
      if (filenum < 0 || DWARF2_LINE_VERSION < 5)
1269
2.40k
  {
1270
2.40k
    as_bad (_("file number less than one"));
1271
2.40k
    return;
1272
2.40k
  }
1273
14.7k
    }
1274
1275
14.5k
  if ((valueT) filenum >= files_in_use || files[filenum].filename == NULL)
1276
2.87k
    {
1277
2.87k
      as_bad (_("unassigned file number %ld"), (long) filenum);
1278
2.87k
      return;
1279
2.87k
    }
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
11.6k
  gas_assert (debug_type == DEBUG_NONE);
1285
1286
0
  current.filenum = filenum;
1287
11.6k
  current.line = line;
1288
11.6k
  current.discriminator = 0;
1289
1290
11.6k
#ifndef NO_LISTING
1291
11.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
11.6k
#endif
1311
1312
11.6k
  SKIP_WHITESPACE ();
1313
11.6k
  if (ISDIGIT (*input_line_pointer))
1314
14
    {
1315
14
      current.column = get_absolute_expression ();
1316
14
      SKIP_WHITESPACE ();
1317
14
    }
1318
1319
11.6k
  while (ISALPHA (*input_line_pointer))
1320
274
    {
1321
274
      char *p, c;
1322
274
      offsetT value;
1323
1324
274
      c = get_symbol_name (& p);
1325
1326
274
      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
274
      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
274
      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
274
      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
274
      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
274
      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
274
      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
274
      else
1439
274
  {
1440
274
    as_bad (_("unknown .loc sub-directive `%s'"), p);
1441
274
    (void) restore_line_pointer (c);
1442
274
    return;
1443
274
  }
1444
1445
0
      SKIP_WHITESPACE_AFTER_NAME ();
1446
0
    }
1447
1448
11.3k
  demand_empty_rest_of_line ();
1449
11.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
11.3k
  if (current.u.view)
1453
0
    dwarf2_emit_insn (0);
1454
11.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
197
{
1476
197
  return seg_info (seg)->frchainP->frch_root;
1477
197
}
1478
1479
static struct frag *
1480
last_frag_for_seg (segT seg)
1481
394
{
1482
394
  frchainS *f = seg_info (seg)->frchainP;
1483
1484
1.43k
  while (f->frch_next != NULL)
1485
1.04k
    f = f->frch_next;
1486
1487
394
  return f->frch_last;
1488
394
}
1489

1490
/* Emit a single byte into the current segment.  */
1491
1492
static inline void
1493
out_byte (int byte)
1494
6.97k
{
1495
6.97k
  FRAG_APPEND_1_CHAR (byte);
1496
6.97k
}
1497
1498
/* Emit a statement program opcode into the current segment.  */
1499
1500
static inline void
1501
out_opcode (int opc)
1502
603
{
1503
603
  out_byte (opc);
1504
603
}
1505
1506
/* Emit a two-byte word into the current segment.  */
1507
1508
static inline void
1509
out_two (int data)
1510
791
{
1511
791
  md_number_to_chars (frag_more (2), data, 2);
1512
791
}
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
5.99k
{
1527
5.99k
  output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
1528
5.99k
}
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
1.57k
{
1543
1.57k
  out_uleb128 (name);
1544
1.57k
  out_uleb128 (form);
1545
1.57k
}
1546
1547
/* Get the size of a fragment.  */
1548
1549
static offsetT
1550
get_frag_fix (fragS *frag, segT seg)
1551
394
{
1552
394
  frchainS *fr;
1553
1554
394
  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.43k
  for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
1561
1.43k
    if (fr->frch_last == frag)
1562
394
      return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
1563
1564
394
  abort ();
1565
394
}
1566
1567
/* Set an absolute address (may result in a relocation entry).  */
1568
1569
static void
1570
out_set_addr (symbolS *sym)
1571
197
{
1572
197
  expressionS exp;
1573
1574
197
  memset (&exp, 0, sizeof exp);
1575
197
  out_opcode (DW_LNS_extended_op);
1576
197
  out_uleb128 (sizeof_address + 1);
1577
1578
197
  out_opcode (DW_LNE_set_address);
1579
197
  exp.X_op = O_symbol;
1580
197
  exp.X_add_symbol = sym;
1581
197
  exp.X_add_number = 0;
1582
197
  emit_expr (&exp, sizeof_address);
1583
197
}
1584
1585
static void
1586
scale_addr_delta (int line_delta, addressT *addr_delta)
1587
10.3k
{
1588
10.3k
  static int printed_this = 0;
1589
10.3k
  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
10.3k
}
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
5.60k
{
1612
5.60k
  unsigned int tmp, opcode;
1613
5.60k
  int len = 0;
1614
1615
  /* Scale the address delta by the minimum instruction length.  */
1616
5.60k
  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
5.60k
  if (line_delta == INT_MAX)
1622
197
    {
1623
197
      if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
1624
1
  len = 1;
1625
196
      else if (addr_delta)
1626
188
  len = 1 + sizeof_leb128 (addr_delta, 0);
1627
197
      return len + 3;
1628
197
    }
1629
1630
  /* Bias the line delta by the base.  */
1631
5.40k
  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.40k
  if (tmp >= DWARF2_LINE_RANGE)
1636
2.17k
    {
1637
2.17k
      len = 1 + sizeof_leb128 (line_delta, 1);
1638
2.17k
      line_delta = 0;
1639
2.17k
      tmp = 0 - DWARF2_LINE_BASE;
1640
2.17k
    }
1641
1642
  /* Bias the opcode by the special opcode base.  */
1643
5.40k
  tmp += DWARF2_LINE_OPCODE_BASE;
1644
1645
  /* Avoid overflow when addr_delta is large.  */
1646
5.40k
  if (addr_delta < 256U + MAX_SPECIAL_ADDR_DELTA)
1647
4.63k
    {
1648
      /* Try using a special opcode.  */
1649
4.63k
      opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
1650
4.63k
      if (opcode <= 255)
1651
3.95k
  return len + 1;
1652
1653
      /* Try using DW_LNS_const_add_pc followed by special op.  */
1654
674
      opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
1655
674
      if (opcode <= 255)
1656
88
  return len + 2;
1657
674
    }
1658
1659
  /* Otherwise use DW_LNS_advance_pc.  */
1660
1.35k
  len += 1 + sizeof_leb128 (addr_delta, 0);
1661
1662
  /* DW_LNS_copy or special opcode.  */
1663
1.35k
  len += 1;
1664
1665
1.35k
  return len;
1666
5.40k
}
1667
1668
static void
1669
emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
1670
4.70k
{
1671
4.70k
  unsigned int tmp, opcode;
1672
4.70k
  int need_copy = 0;
1673
4.70k
  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.70k
  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.70k
  if (line_delta == INT_MAX)
1686
63
    {
1687
63
      if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
1688
1
  *p++ = DW_LNS_const_add_pc;
1689
62
      else if (addr_delta)
1690
54
  {
1691
54
    *p++ = DW_LNS_advance_pc;
1692
54
    p += output_leb128 (p, addr_delta, 0);
1693
54
  }
1694
1695
63
      *p++ = DW_LNS_extended_op;
1696
63
      *p++ = 1;
1697
63
      *p++ = DW_LNE_end_sequence;
1698
63
      goto done;
1699
63
    }
1700
1701
  /* Bias the line delta by the base.  */
1702
4.64k
  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.64k
  if (tmp >= DWARF2_LINE_RANGE)
1707
1.81k
    {
1708
1.81k
      *p++ = DW_LNS_advance_line;
1709
1.81k
      p += output_leb128 (p, line_delta, 1);
1710
1711
1.81k
      line_delta = 0;
1712
1.81k
      tmp = 0 - DWARF2_LINE_BASE;
1713
1.81k
      need_copy = 1;
1714
1.81k
    }
1715
1716
  /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
1717
     special opcode.  */
1718
4.64k
  if (line_delta == 0 && addr_delta == 0)
1719
3.35k
    {
1720
3.35k
      *p++ = DW_LNS_copy;
1721
3.35k
      goto done;
1722
3.35k
    }
1723
1724
  /* Bias the opcode by the special opcode base.  */
1725
1.29k
  tmp += DWARF2_LINE_OPCODE_BASE;
1726
1727
  /* Avoid overflow when addr_delta is large.  */
1728
1.29k
  if (addr_delta < 256U + MAX_SPECIAL_ADDR_DELTA)
1729
1.28k
    {
1730
      /* Try using a special opcode.  */
1731
1.28k
      opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
1732
1.28k
      if (opcode <= 255)
1733
607
  {
1734
607
    *p++ = opcode;
1735
607
    goto done;
1736
607
  }
1737
1738
      /* Try using DW_LNS_const_add_pc followed by special op.  */
1739
674
      opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
1740
674
      if (opcode <= 255)
1741
88
  {
1742
88
    *p++ = DW_LNS_const_add_pc;
1743
88
    *p++ = opcode;
1744
88
    goto done;
1745
88
  }
1746
674
    }
1747
1748
  /* Otherwise use DW_LNS_advance_pc.  */
1749
595
  *p++ = DW_LNS_advance_pc;
1750
595
  p += output_leb128 (p, addr_delta, 0);
1751
1752
595
  if (need_copy)
1753
180
    *p++ = DW_LNS_copy;
1754
415
  else
1755
415
    *p++ = tmp;
1756
1757
4.70k
 done:
1758
4.70k
  gas_assert (p == end);
1759
4.70k
}
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.70k
{
1766
4.70k
  int len = size_inc_line_addr (line_delta, addr_delta);
1767
4.70k
  emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
1768
4.70k
}
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
898
{
1878
898
  expressionS exp;
1879
898
  int max_chars;
1880
1881
898
  memset (&exp, 0, sizeof exp);
1882
898
  exp.X_op = O_subtract;
1883
898
  exp.X_add_symbol = to_sym;
1884
898
  exp.X_op_symbol = from_sym;
1885
898
  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
898
  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
898
  else
1893
898
    max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
1894
1895
898
  frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
1896
898
      make_expr_symbol (&exp), line_delta, NULL);
1897
898
}
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
197
{
1988
197
  unsigned filenum = 1;
1989
197
  unsigned line = 1;
1990
197
  unsigned column = 0;
1991
197
  unsigned isa = 0;
1992
197
  unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1993
197
  fragS *last_frag = NULL, *frag;
1994
197
  addressT last_frag_ofs = 0, frag_ofs;
1995
197
  symbolS *last_lab = NULL, *lab;
1996
1997
197
  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
197
  do
2028
5.40k
    {
2029
5.40k
      int line_delta;
2030
2031
5.40k
      if (filenum != e->loc.filenum)
2032
197
  {
2033
197
    filenum = e->loc.filenum;
2034
197
    out_opcode (DW_LNS_set_file);
2035
197
    out_uleb128 (filenum);
2036
197
  }
2037
2038
5.40k
      if (column != e->loc.column)
2039
12
  {
2040
12
    column = e->loc.column;
2041
12
    out_opcode (DW_LNS_set_column);
2042
12
    out_uleb128 (column);
2043
12
  }
2044
2045
5.40k
      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.40k
      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.40k
      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.40k
      if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
2067
0
  out_opcode (DW_LNS_set_basic_block);
2068
2069
5.40k
      if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
2070
0
  out_opcode (DW_LNS_set_prologue_end);
2071
2072
5.40k
      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.40k
      line_delta = e->loc.line - line;
2080
5.40k
      lab = e->label;
2081
5.40k
      frag = symbol_get_frag (lab);
2082
5.40k
      frag_ofs = S_GET_VALUE (lab);
2083
2084
5.40k
      if (last_frag == NULL
2085
5.40k
    || (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.20k
        && (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
197
  {
2098
197
    out_set_addr (lab);
2099
197
    out_inc_line_addr (line_delta, 0);
2100
197
  }
2101
5.20k
      else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
2102
4.44k
  out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
2103
764
      else
2104
764
  relax_inc_line_addr (line_delta, lab, last_lab);
2105
2106
5.40k
      line = e->loc.line;
2107
5.40k
      last_lab = lab;
2108
5.40k
      last_frag = frag;
2109
5.40k
      last_frag_ofs = frag_ofs;
2110
2111
5.40k
      e = e->next;
2112
5.40k
    }
2113
5.40k
  while (e);
2114
2115
  /* Emit a DW_LNE_end_sequence for the end of the section.  */
2116
197
  frag = last_frag_for_seg (seg);
2117
197
  frag_ofs = get_frag_fix (frag, seg);
2118
197
  if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
2119
63
    out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
2120
134
  else
2121
134
    {
2122
134
      lab = symbol_temp_new (seg, frag, frag_ofs);
2123
134
      relax_inc_line_addr (INT_MAX, lab, last_lab);
2124
134
    }
2125
197
}
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
603
{
2133
603
  char *cp;
2134
603
  size_t size;
2135
603
  symbolS *sym;
2136
2137
603
  subseg_set (line_str_seg, 0);
2138
2139
603
  sym = symbol_temp_new_now_octets ();
2140
2141
603
  size = strlen (str) + 1;
2142
603
  cp = frag_more (size);
2143
603
  memcpy (cp, str, size);
2144
2145
603
  return sym;
2146
603
}
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
200
{
2154
200
  size_t size;
2155
200
  char *dir;
2156
200
  char *cp;
2157
200
  unsigned int i, j;
2158
200
  bool emit_md5 = false;
2159
200
  bool emit_timestamps = true;
2160
200
  bool emit_filesize = true;
2161
200
  segT line_str_seg = NULL;
2162
200
  symbolS *line_strp, *file0_strp = NULL;
2163
2164
  /* Output the Directory Table.  */
2165
200
  if (DWARF2_LINE_VERSION >= 5)
2166
200
    {
2167
      /* We only have one column in the directory table.  */
2168
200
      out_byte (1);
2169
2170
      /* Describe the purpose and format of the column.  */
2171
200
      out_uleb128 (DW_LNCT_path);
2172
      /* Store these strings in the .debug_line_str section so they
2173
   can be shared.  */
2174
200
      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
200
      if (dirs_in_use == 0 && files_in_use > 0)
2180
134
  out_uleb128 (1);
2181
66
      else
2182
66
  out_uleb128 (dirs_in_use);
2183
200
    }
2184
      
2185
  /* Emit directory list.  */
2186
200
  if (DWARF2_LINE_VERSION >= 5 && (dirs_in_use > 0 || files_in_use > 0))
2187
200
    {
2188
200
      line_str_seg = subseg_new (".debug_line_str", 0);
2189
200
      bfd_set_section_flags (line_str_seg,
2190
200
           SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS
2191
200
           | SEC_MERGE | SEC_STRINGS);
2192
200
      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
200
      if (dirs_in_use > 0 && dirs[0] != NULL)
2198
41
  dir = remap_debug_filename (dirs[0]);
2199
159
      else
2200
159
  dir = remap_debug_filename (getpwd ());
2201
2202
200
      line_strp = add_line_strp (line_str_seg, dir);
2203
200
      free (dir);
2204
200
      subseg_set (line_seg, 0);
2205
200
      TC_DWARF2_EMIT_OFFSET (line_strp, sizeof_offset);
2206
200
    }
2207
361
  for (i = 1; i < dirs_in_use; ++i)
2208
161
    {
2209
161
      dir = remap_debug_filename (dirs[i]);
2210
161
      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
161
      else
2217
161
  {
2218
161
    line_strp = add_line_strp (line_str_seg, dir);
2219
161
    subseg_set (line_seg, 0);
2220
161
    TC_DWARF2_EMIT_OFFSET (line_strp, sizeof_offset);
2221
161
  }
2222
161
      free (dir);
2223
161
    }
2224
2225
200
  if (DWARF2_LINE_VERSION < 5)
2226
    /* Terminate it.  */
2227
0
    out_byte ('\0');
2228
2229
  /* Output the File Name Table.  */
2230
200
  if (DWARF2_LINE_VERSION >= 5)
2231
200
    {
2232
200
      unsigned int columns = 4;
2233
2234
200
      if (((unsigned long) DWARF2_FILE_TIME_NAME ("", "")) == -1UL)
2235
200
  {
2236
200
    emit_timestamps = false;
2237
200
    -- columns;
2238
200
  }
2239
2240
200
      if (DWARF2_FILE_SIZE_NAME ("", "") == -1)
2241
200
  {
2242
200
    emit_filesize = false;
2243
200
    -- columns;
2244
200
  }
2245
2246
1.38M
      for (i = 0; i < files_in_use; ++i)
2247
1.38M
  if (files[i].md5[0] != 0)
2248
0
    break;
2249
200
      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
200
      out_byte (columns);
2257
      /* The format of the file name.  */
2258
200
      out_uleb128 (DW_LNCT_path);
2259
      /* Store these strings in the .debug_line_str section so they
2260
   can be shared.  */
2261
200
      out_uleb128 (DW_FORM_line_strp);
2262
2263
      /* The format of the directory index.  */
2264
200
      out_uleb128 (DW_LNCT_directory_index);
2265
200
      out_uleb128 (DW_FORM_udata);
2266
2267
200
      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
200
      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
200
      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
200
      out_uleb128 (files_in_use);
2290
200
   }
2291
      
2292
1.38M
  for (i = DWARF2_LINE_VERSION > 4 ? 0 : 1; i < files_in_use; ++i)
2293
1.38M
    {
2294
1.38M
      const char *fullfilename;
2295
2296
1.38M
      if (files[i].filename == NULL)
2297
1.38M
  {
2298
1.38M
    if (DWARF2_LINE_VERSION < 5 || i != 0)
2299
1.38M
      {
2300
1.38M
        as_bad (_("unassigned file number %ld"), (long) i);
2301
1.38M
        continue;
2302
1.38M
      }
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
0
    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
0
    else
2315
0
      files[0].filename = "";
2316
0
  }
2317
2318
242
      fullfilename = DWARF2_FILE_NAME (files[i].filename,
2319
242
               files[i].dir ? dirs [files [i].dir] : "");
2320
242
      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
242
      else
2327
242
  {
2328
242
    if (!file0_strp)
2329
242
      line_strp = add_line_strp (line_str_seg, fullfilename);
2330
0
    else
2331
0
      line_strp = file0_strp;
2332
242
    subseg_set (line_seg, 0);
2333
242
    TC_DWARF2_EMIT_OFFSET (line_strp, sizeof_offset);
2334
242
    if (i == 0 && files_in_use > 1
2335
242
        && files[0].filename == files[1].filename)
2336
0
      file0_strp = line_strp;
2337
242
    else
2338
242
      file0_strp = NULL;
2339
242
  }
2340
2341
      /* Directory number.  */
2342
242
      out_uleb128 (files[i].dir);
2343
2344
      /* Output the last modification timestamp.  */
2345
242
      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
242
      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
242
      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
242
    }
2376
2377
200
  if (DWARF2_LINE_VERSION < 5)
2378
    /* Terminate filename list.  */
2379
0
    out_byte (0);
2380
200
}
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
594
{
2390
594
  symbolS *start_sym;
2391
594
  symbolS *end_sym;
2392
2393
594
  subseg_set (sec, 0);
2394
2395
594
  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
594
  else
2405
594
    {
2406
594
      start_sym = symbol_temp_new_now_octets ();
2407
594
      end_sym = symbol_temp_make ();
2408
594
    }
2409
2410
  /* Total length of the information.  */
2411
594
  exp->X_op = O_subtract;
2412
594
  exp->X_add_symbol = end_sym;
2413
594
  exp->X_op_symbol = start_sym;
2414
2415
594
  switch (DWARF2_FORMAT (sec))
2416
594
    {
2417
594
    case dwarf2_format_32bit:
2418
594
      exp->X_add_number = -4;
2419
594
      emit_expr (exp, 4);
2420
594
      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
594
    }
2433
2434
0
  as_fatal (_("internal error: unknown dwarf2 format"));
2435
0
  return 0;
2436
594
}
2437
2438
/* Emit the collected .debug_line data.  */
2439
2440
static void
2441
out_debug_line (segT line_seg)
2442
200
{
2443
200
  expressionS exp;
2444
200
  symbolS *prologue_start, *prologue_end;
2445
200
  symbolS *line_end;
2446
200
  struct line_seg *s;
2447
200
  int sizeof_offset;
2448
2449
200
  memset (&exp, 0, sizeof exp);
2450
200
  sizeof_offset = out_header (line_seg, &exp);
2451
200
  line_end = exp.X_add_symbol;
2452
2453
  /* Version.  */
2454
200
  out_two (DWARF2_LINE_VERSION);
2455
2456
200
  if (DWARF2_LINE_VERSION >= 5)
2457
200
    {
2458
200
      out_byte (sizeof_address);
2459
200
      out_byte (0); /* Segment Selector size.  */
2460
200
    }
2461
  /* Length of the prologue following this length.  */
2462
200
  prologue_start = symbol_temp_make ();
2463
200
  prologue_end = symbol_temp_make ();
2464
200
  exp.X_op = O_subtract;
2465
200
  exp.X_add_symbol = prologue_end;
2466
200
  exp.X_op_symbol = prologue_start;
2467
200
  exp.X_add_number = 0;
2468
200
  emit_expr (&exp, sizeof_offset);
2469
200
  symbol_set_value_now (prologue_start);
2470
2471
  /* Parameters of the state machine.  */
2472
200
  out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
2473
200
  if (DWARF2_LINE_VERSION >= 4)
2474
200
    out_byte (DWARF2_LINE_MAX_OPS_PER_INSN);
2475
200
  out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
2476
200
  out_byte (DWARF2_LINE_BASE);
2477
200
  out_byte (DWARF2_LINE_RANGE);
2478
200
  out_byte (DWARF2_LINE_OPCODE_BASE);
2479
2480
  /* Standard opcode lengths.  */
2481
200
  out_byte (0);     /* DW_LNS_copy */
2482
200
  out_byte (1);     /* DW_LNS_advance_pc */
2483
200
  out_byte (1);     /* DW_LNS_advance_line */
2484
200
  out_byte (1);     /* DW_LNS_set_file */
2485
200
  out_byte (1);     /* DW_LNS_set_column */
2486
200
  out_byte (0);     /* DW_LNS_negate_stmt */
2487
200
  out_byte (0);     /* DW_LNS_set_basic_block */
2488
200
  out_byte (0);     /* DW_LNS_const_add_pc */
2489
200
  out_byte (1);     /* DW_LNS_fixed_advance_pc */
2490
200
  if (DWARF2_LINE_VERSION >= 3)
2491
200
    {
2492
200
      out_byte (0);     /* DW_LNS_set_prologue_end */
2493
200
      out_byte (0);     /* DW_LNS_set_epilogue_begin */
2494
200
      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
200
      gas_assert (DWARF2_LINE_OPCODE_BASE == 13);
2498
200
    }
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
200
  symbol_set_value_now (prologue_end);
2505
2506
  /* For each section, emit a statement program.  */
2507
397
  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
197
    if (s->head->head && SEG_NORMAL (s->seg))
2511
197
      process_entries (s->seg, s->head->head);
2512
2513
200
  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
200
  symbol_set_value_now (line_end);
2524
200
}
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
197
{
2635
197
  unsigned int addr_size = sizeof_address;
2636
197
  offsetT size;
2637
197
  struct line_seg *s;
2638
197
  expressionS exp;
2639
197
  symbolS *aranges_end;
2640
197
  char *p;
2641
197
  int sizeof_offset;
2642
2643
197
  memset (&exp, 0, sizeof exp);
2644
197
  sizeof_offset = out_header (aranges_seg, &exp);
2645
197
  aranges_end = exp.X_add_symbol;
2646
197
  size = -exp.X_add_number;
2647
2648
  /* Version.  */
2649
197
  out_two (DWARF2_ARANGES_VERSION);
2650
197
  size += 2;
2651
2652
  /* Offset to .debug_info.  */
2653
197
  TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset);
2654
197
  size += sizeof_offset;
2655
2656
  /* Size of an address (offset portion).  */
2657
197
  out_byte (addr_size);
2658
197
  size++;
2659
2660
  /* Size of a segment descriptor.  */
2661
197
  out_byte (0);
2662
197
  size++;
2663
2664
  /* Align the header.  */
2665
985
  while ((size++ % (2 * addr_size)) > 0)
2666
788
    out_byte (0);
2667
2668
394
  for (s = all_segs; s; s = s->next)
2669
197
    {
2670
197
      fragS *frag;
2671
197
      symbolS *beg, *end;
2672
2673
197
      frag = first_frag_for_seg (s->seg);
2674
197
      beg = symbol_temp_new (s->seg, frag, 0);
2675
197
      s->text_start = beg;
2676
2677
197
      frag = last_frag_for_seg (s->seg);
2678
197
      end = symbol_temp_new (s->seg, frag, get_frag_fix (frag, s->seg));
2679
197
      s->text_end = end;
2680
2681
197
      exp.X_op = O_symbol;
2682
197
      exp.X_add_symbol = beg;
2683
197
      exp.X_add_number = 0;
2684
197
      emit_expr (&exp, addr_size);
2685
2686
197
      exp.X_op = O_subtract;
2687
197
      exp.X_add_symbol = end;
2688
197
      exp.X_op_symbol = beg;
2689
197
      exp.X_add_number = 0;
2690
197
      emit_expr (&exp, addr_size);
2691
197
    }
2692
2693
197
  p = frag_more (2 * addr_size);
2694
197
  md_number_to_chars (p, 0, addr_size);
2695
197
  md_number_to_chars (p + addr_size, 0, addr_size);
2696
2697
197
  symbol_set_value_now (aranges_end);
2698
197
}
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
197
{
2709
197
  int secoff_form;
2710
197
  bool have_efunc = false, have_lfunc = false;
2711
2712
  /* Check the symbol table for function symbols which also have their size
2713
     specified.  */
2714
197
  if (symbol_rootP)
2715
197
    {
2716
197
      symbolS *symp;
2717
2718
24.0k
      for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2719
23.8k
  {
2720
    /* A warning construct is a warning symbol followed by the
2721
       symbol warned about.  Skip this and the following symbol.  */
2722
23.8k
    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
23.8k
    if (!S_IS_DEFINED (symp) || !S_IS_FUNCTION (symp))
2731
23.8k
      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
197
    }
2749
2750
197
  subseg_set (abbrev_seg, 0);
2751
2752
197
  out_uleb128 (GAS_ABBREV_COMP_UNIT);
2753
197
  out_uleb128 (DW_TAG_compile_unit);
2754
197
  out_byte (have_efunc || have_lfunc ? DW_CHILDREN_yes : DW_CHILDREN_no);
2755
197
  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
197
  else
2763
197
    secoff_form = DW_FORM_sec_offset;
2764
197
  out_abbrev (DW_AT_stmt_list, secoff_form);
2765
197
  if (all_segs->next == NULL)
2766
197
    {
2767
197
      out_abbrev (DW_AT_low_pc, DW_FORM_addr);
2768
197
      if (DWARF2_VERSION < 4)
2769
0
  out_abbrev (DW_AT_high_pc, DW_FORM_addr);
2770
197
      else
2771
197
  out_abbrev (DW_AT_high_pc, DW_FORM_udata);
2772
197
    }
2773
0
  else
2774
0
    out_abbrev (DW_AT_ranges, secoff_form);
2775
197
  out_abbrev (DW_AT_name, DW_FORM_strp);
2776
197
  out_abbrev (DW_AT_comp_dir, DW_FORM_strp);
2777
197
  out_abbrev (DW_AT_producer, DW_FORM_strp);
2778
197
  out_abbrev (DW_AT_language, DW_FORM_data2);
2779
197
  out_abbrev (0, 0);
2780
2781
197
  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
197
  out_byte (0);
2822
197
}
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
197
{
2832
197
  expressionS exp;
2833
197
  symbolS *info_end;
2834
197
  int sizeof_offset;
2835
2836
197
  memset (&exp, 0, sizeof exp);
2837
197
  sizeof_offset = out_header (info_seg, &exp);
2838
197
  info_end = exp.X_add_symbol;
2839
2840
  /* DWARF version.  */
2841
197
  out_two (DWARF2_VERSION);
2842
2843
197
  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
197
  else
2849
197
    {
2850
      /* unit (header) type */
2851
197
      out_byte (DW_UT_compile);
2852
197
    }
2853
2854
  /* Target address size.  */
2855
197
  out_byte (sizeof_address);
2856
2857
197
  if (DWARF2_VERSION >= 5)
2858
197
    {
2859
      /* .debug_abbrev offset */
2860
197
      TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
2861
197
    }
2862
2863
  /* DW_TAG_compile_unit DIE abbrev */
2864
197
  out_uleb128 (GAS_ABBREV_COMP_UNIT);
2865
2866
  /* DW_AT_stmt_list */
2867
197
  TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg),
2868
197
       (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit
2869
197
        ? 4 : 8));
2870
2871
  /* These two attributes are emitted if all of the code is contiguous.  */
2872
197
  if (all_segs->next == NULL)
2873
197
    {
2874
      /* DW_AT_low_pc */
2875
197
      exp.X_op = O_symbol;
2876
197
      exp.X_add_symbol = all_segs->text_start;
2877
197
      exp.X_add_number = 0;
2878
197
      emit_expr (&exp, sizeof_address);
2879
2880
      /* DW_AT_high_pc */
2881
197
      if (DWARF2_VERSION < 4)
2882
0
  exp.X_op = O_symbol;
2883
197
      else
2884
197
  {
2885
197
    exp.X_op = O_subtract;
2886
197
    exp.X_op_symbol = all_segs->text_start;
2887
197
  }
2888
197
      exp.X_add_symbol = all_segs->text_end;
2889
197
      exp.X_add_number = 0;
2890
197
      if (DWARF2_VERSION < 4)
2891
0
  emit_expr (&exp, sizeof_address);
2892
197
      else
2893
197
  emit_leb128_expr (&exp, 0);
2894
197
    }
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
197
  TC_DWARF2_EMIT_OFFSET (name_sym, sizeof_offset);
2905
197
  TC_DWARF2_EMIT_OFFSET (comp_dir_sym, sizeof_offset);
2906
197
  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
197
  out_two (DW_LANG_Mips_Assembler);
2911
2912
197
  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
197
  symbol_set_value_now (info_end);
3014
197
}
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
197
{
3022
197
  char producer[128];
3023
197
  char *p;
3024
197
  int len;
3025
197
  int first_file = DWARF2_LINE_VERSION > 4 ? 0 : 1;
3026
3027
197
  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
197
  *name_sym = symbol_temp_new_now_octets ();
3034
197
  if (files_in_use == 0)
3035
0
    abort ();
3036
197
  if (files[first_file].dir)
3037
34
    {
3038
34
      char *dirname = remap_debug_filename (dirs[files[first_file].dir]);
3039
34
      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
34
      p = frag_more (len + 1);
3046
34
      memcpy (p, dirname, len);
3047
34
      INSERT_DIR_SEPARATOR (p, len);
3048
34
#endif
3049
34
      free (dirname);
3050
34
    }
3051
197
  len = strlen (files[first_file].filename) + 1;
3052
197
  p = frag_more (len);
3053
197
  memcpy (p, files[first_file].filename, len);
3054
3055
  /* DW_AT_comp_dir */
3056
197
  *comp_dir_sym = symbol_temp_new_now_octets ();
3057
197
  char *comp_dir = remap_debug_filename (getpwd ());
3058
197
  len = strlen (comp_dir) + 1;
3059
197
  p = frag_more (len);
3060
197
  memcpy (p, comp_dir, len);
3061
197
  free (comp_dir);
3062
3063
  /* DW_AT_producer */
3064
197
  *producer_sym = symbol_temp_new_now_octets ();
3065
197
  sprintf (producer, "GNU AS %s", VERSION);
3066
197
  len = strlen (producer) + 1;
3067
197
  p = frag_more (len);
3068
197
  memcpy (p, producer, len);
3069
197
}
3070
3071
void
3072
dwarf2_init (void)
3073
633
{
3074
633
  all_segs = NULL;
3075
633
  last_seg_ptr = &all_segs;
3076
633
  files = NULL;
3077
633
  files_in_use = 0;
3078
633
  files_allocated = 0;
3079
633
  dirs = NULL;
3080
633
  dirs_in_use = 0;
3081
633
  dirs_allocated = 0;
3082
633
  dwarf2_loc_directive_seen = false;
3083
633
  dwarf2_any_loc_directive_seen = false;
3084
633
  dwarf2_loc_mark_labels = false;
3085
633
  current.filenum = 1;
3086
633
  current.line = 1;
3087
633
  current.column = 0;
3088
633
  current.isa = 0;
3089
633
  current.flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
3090
633
  current.discriminator = 0;
3091
633
  current.u.view = NULL;
3092
633
  force_reset_view = NULL;
3093
633
  view_assert_failed = NULL;
3094
633
  dw2_line = -1;
3095
633
  dw2_filename = NULL;
3096
633
  label_num = 0;
3097
633
  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
633
  if (flag_dwarf_cie_version == -1)
3108
1
    flag_dwarf_cie_version = 1;
3109
633
}
3110
3111
static void
3112
dwarf2_cleanup (void)
3113
633
{
3114
633
  purge_generated_debug (true);
3115
633
  free (files);
3116
1.28k
  for (unsigned int i = 0; i < dirs_in_use; i++)
3117
651
    free (dirs[i]);
3118
633
  free (dirs);
3119
633
}
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
633
{
3133
633
  segT line_seg;
3134
633
  struct line_seg *s;
3135
633
  segT info_seg;
3136
633
  int emit_other_sections = 0;
3137
633
  int empty_debug_line = 0;
3138
3139
633
  info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
3140
633
  emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
3141
3142
633
  line_seg = bfd_get_section_by_name (stdoutput, ".debug_line");
3143
633
  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
633
  if (all_segs && !empty_debug_line && dwarf2_any_loc_directive_seen)
3150
0
    as_fatal ("duplicate .debug_line sections");
3151
3152
633
  if ((!all_segs && emit_other_sections)
3153
633
      || (!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
433
    {
3158
433
      dwarf2_cleanup ();
3159
433
      return;
3160
433
    }
3161
3162
  /* Calculate the size of an address for the target machine.  */
3163
200
  sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
3164
3165
  /* Create and switch to the line number section.  */
3166
200
  if (empty_debug_line)
3167
200
    {
3168
200
      line_seg = subseg_new (".debug_line", 0);
3169
200
      bfd_set_section_flags (line_seg,
3170
200
           SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS);
3171
200
    }
3172
3173
397
  for (s = all_segs; s; s = s->next)
3174
197
    {
3175
197
      struct line_subseg *lss;
3176
3177
540
      for (lss = s->head; lss; lss = lss->next)
3178
343
  if (lss->head)
3179
343
    do_allocate_filenum (lss->head);
3180
197
    }
3181
3182
  /* For each subsection, chain the debug entries together.  */
3183
397
  for (s = all_segs; s; s = s->next)
3184
197
    {
3185
197
      struct line_subseg *lss = s->head;
3186
197
      struct line_entry **ptail = lss->ptail;
3187
3188
      /* Reset the initial view of the first subsection of the
3189
   section.  */
3190
197
      if (lss->head && lss->head->loc.u.view)
3191
0
  set_or_check_view (lss->head, NULL, NULL);
3192
3193
343
      while ((lss = lss->next) != NULL)
3194
146
  {
3195
    /* Link the first view of subsequent subsections to the
3196
       previous view.  */
3197
146
    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
146
    *ptail = lss->head;
3202
146
    lss->head = NULL;
3203
146
    ptail = lss->ptail;
3204
146
  }
3205
197
    }
3206
3207
200
  if (empty_debug_line)
3208
200
    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
200
  if (emit_other_sections)
3214
197
    {
3215
197
      segT abbrev_seg;
3216
197
      segT aranges_seg;
3217
197
      segT str_seg;
3218
197
      symbolS *name_sym, *comp_dir_sym, *producer_sym, *ranges_sym;
3219
197
      unsigned char func_form = 0;
3220
3221
197
      gas_assert (all_segs);
3222
3223
0
      info_seg = subseg_new (".debug_info", 0);
3224
197
      abbrev_seg = subseg_new (".debug_abbrev", 0);
3225
197
      aranges_seg = subseg_new (".debug_aranges", 0);
3226
197
      str_seg = subseg_new (".debug_str", 0);
3227
3228
197
      bfd_set_section_flags (info_seg,
3229
197
            SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS);
3230
197
      bfd_set_section_flags (abbrev_seg,
3231
197
            SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS);
3232
197
      bfd_set_section_flags (aranges_seg,
3233
197
            SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS);
3234
197
      bfd_set_section_flags (str_seg,
3235
197
            SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS
3236
197
               | SEC_MERGE | SEC_STRINGS);
3237
197
      str_seg->entsize = 1;
3238
3239
197
      record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
3240
3241
197
      if (all_segs->next == NULL)
3242
197
  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
197
      out_debug_aranges (aranges_seg, info_seg);
3265
197
      out_debug_abbrev (abbrev_seg, info_seg, line_seg, &func_form);
3266
197
      out_debug_str (str_seg, &name_sym, &comp_dir_sym, &producer_sym);
3267
197
      out_debug_info (info_seg, abbrev_seg, line_seg, str_seg,
3268
197
          ranges_sym, name_sym, comp_dir_sym, producer_sym,
3269
197
          func_form);
3270
197
    }
3271
0
  dwarf2_cleanup ();
3272
200
}
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
}