Coverage Report

Created: 2024-05-21 06:29

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