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