Coverage Report

Created: 2026-10-02 09:53

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