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