Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* dwarf2dbg.c - DWARF2 debug support
  2.    Copyright 1999-2013 Free Software Foundation, Inc.
  3.    Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4.  
  5.    This file is part of GAS, the GNU Assembler.
  6.  
  7.    GAS is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3, or (at your option)
  10.    any later version.
  11.  
  12.    GAS is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with GAS; see the file COPYING.  If not, write to the Free
  19.    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  20.    02110-1301, USA.  */
  21.  
  22. /* Logical line numbers can be controlled by the compiler via the
  23.    following directives:
  24.  
  25.         .file FILENO "file.c"
  26.         .loc  FILENO LINENO [COLUMN] [basic_block] [prologue_end] \
  27.               [epilogue_begin] [is_stmt VALUE] [isa VALUE] \
  28.               [discriminator VALUE]
  29. */
  30.  
  31. #include "as.h"
  32. #include "safe-ctype.h"
  33.  
  34. #ifdef HAVE_LIMITS_H
  35. #include <limits.h>
  36. #else
  37. #ifdef HAVE_SYS_PARAM_H
  38. #include <sys/param.h>
  39. #endif
  40. #ifndef INT_MAX
  41. #define INT_MAX (int) (((unsigned) (-1)) >> 1)
  42. #endif
  43. #endif
  44.  
  45. #include "dwarf2dbg.h"
  46. #include <filenames.h>
  47.  
  48. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  49. /* We need to decide which character to use as a directory separator.
  50.    Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not
  51.    necessarily mean that the backslash character is the one to use.
  52.    Some environments, eg Cygwin, can support both naming conventions.
  53.    So we use the heuristic that we only need to use the backslash if
  54.    the path is an absolute path starting with a DOS style drive
  55.    selector.  eg C: or D:  */
  56. # define INSERT_DIR_SEPARATOR(string, offset) \
  57.   do \
  58.     { \
  59.       if (offset > 1 \
  60.           && string[0] != 0 \
  61.           && string[1] == ':') \
  62.        string [offset] = '\\'; \
  63.       else \
  64.        string [offset] = '/'; \
  65.     } \
  66.   while (0)
  67. #else
  68. # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
  69. #endif
  70.  
  71. #ifndef DWARF2_FORMAT
  72. # define DWARF2_FORMAT(SEC) dwarf2_format_32bit
  73. #endif
  74.  
  75. #ifndef DWARF2_ADDR_SIZE
  76. # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
  77. #endif
  78.  
  79. #ifndef DWARF2_FILE_NAME
  80. #define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME
  81. #endif
  82.  
  83. #ifndef DWARF2_FILE_TIME_NAME
  84. #define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) 0
  85. #endif
  86.  
  87. #ifndef DWARF2_FILE_SIZE_NAME
  88. #define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) 0
  89. #endif
  90.  
  91. #ifndef DWARF2_VERSION
  92. #define DWARF2_VERSION 2
  93. #endif
  94.  
  95. /* The .debug_aranges version has been 2 in DWARF version 2, 3 and 4. */
  96. #ifndef DWARF2_ARANGES_VERSION
  97. #define DWARF2_ARANGES_VERSION 2
  98. #endif
  99.  
  100. /* This implementation output version 2 .debug_line information. */
  101. #ifndef DWARF2_LINE_VERSION
  102. #define DWARF2_LINE_VERSION 2
  103. #endif
  104.  
  105. #include "subsegs.h"
  106.  
  107. #include "dwarf2.h"
  108.  
  109. /* Since we can't generate the prolog until the body is complete, we
  110.    use three different subsegments for .debug_line: one holding the
  111.    prolog, one for the directory and filename info, and one for the
  112.    body ("statement program").  */
  113. #define DL_PROLOG       0
  114. #define DL_FILES        1
  115. #define DL_BODY         2
  116.  
  117. /* If linker relaxation might change offsets in the code, the DWARF special
  118.    opcodes and variable-length operands cannot be used.  If this macro is
  119.    nonzero, use the DW_LNS_fixed_advance_pc opcode instead.  */
  120. #ifndef DWARF2_USE_FIXED_ADVANCE_PC
  121. # define DWARF2_USE_FIXED_ADVANCE_PC    linkrelax
  122. #endif
  123.  
  124. /* First special line opcde - leave room for the standard opcodes.
  125.    Note: If you want to change this, you'll have to update the
  126.    "standard_opcode_lengths" table that is emitted below in
  127.    out_debug_line().  */
  128. #define DWARF2_LINE_OPCODE_BASE         13
  129.  
  130. #ifndef DWARF2_LINE_BASE
  131.   /* Minimum line offset in a special line info. opcode.  This value
  132.      was chosen to give a reasonable range of values.  */
  133. # define DWARF2_LINE_BASE               -5
  134. #endif
  135.  
  136. /* Range of line offsets in a special line info. opcode.  */
  137. #ifndef DWARF2_LINE_RANGE
  138. # define DWARF2_LINE_RANGE              14
  139. #endif
  140.  
  141. #ifndef DWARF2_LINE_MIN_INSN_LENGTH
  142.   /* Define the architecture-dependent minimum instruction length (in
  143.      bytes).  This value should be rather too small than too big.  */
  144. # define DWARF2_LINE_MIN_INSN_LENGTH    1
  145. #endif
  146.  
  147. /* Flag that indicates the initial value of the is_stmt_start flag.  */
  148. #define DWARF2_LINE_DEFAULT_IS_STMT     1
  149.  
  150. /* Given a special op, return the line skip amount.  */
  151. #define SPECIAL_LINE(op) \
  152.         (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
  153.  
  154. /* Given a special op, return the address skip amount (in units of
  155.    DWARF2_LINE_MIN_INSN_LENGTH.  */
  156. #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
  157.  
  158. /* The maximum address skip amount that can be encoded with a special op.  */
  159. #define MAX_SPECIAL_ADDR_DELTA          SPECIAL_ADDR(255)
  160.  
  161. struct line_entry {
  162.   struct line_entry *next;
  163.   symbolS *label;
  164.   struct dwarf2_line_info loc;
  165. };
  166.  
  167. struct line_subseg {
  168.   struct line_subseg *next;
  169.   subsegT subseg;
  170.   struct line_entry *head;
  171.   struct line_entry **ptail;
  172.   struct line_entry **pmove_tail;
  173. };
  174.  
  175. struct line_seg {
  176.   struct line_seg *next;
  177.   segT seg;
  178.   struct line_subseg *head;
  179.   symbolS *text_start;
  180.   symbolS *text_end;
  181. };
  182.  
  183. /* Collects data for all line table entries during assembly.  */
  184. static struct line_seg *all_segs;
  185. /* Hash used to quickly lookup a segment by name, avoiding the need to search
  186.    through the all_segs list.  */
  187. static struct hash_control *all_segs_hash;
  188. static struct line_seg **last_seg_ptr;
  189.  
  190. struct file_entry {
  191.   const char *filename;
  192.   unsigned int dir;
  193. };
  194.  
  195. /* Table of files used by .debug_line.  */
  196. static struct file_entry *files;
  197. static unsigned int files_in_use;
  198. static unsigned int files_allocated;
  199.  
  200. /* Table of directories used by .debug_line.  */
  201. static char **dirs;
  202. static unsigned int dirs_in_use;
  203. static unsigned int dirs_allocated;
  204.  
  205. /* TRUE when we've seen a .loc directive recently.  Used to avoid
  206.    doing work when there's nothing to do.  */
  207. bfd_boolean dwarf2_loc_directive_seen;
  208.  
  209. /* TRUE when we're supposed to set the basic block mark whenever a
  210.    label is seen.  */
  211. bfd_boolean dwarf2_loc_mark_labels;
  212.  
  213. /* Current location as indicated by the most recent .loc directive.  */
  214. static struct dwarf2_line_info current = {
  215.   1, 1, 0, 0,
  216.   DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0,
  217.   0
  218. };
  219.  
  220. /* The size of an address on the target.  */
  221. static unsigned int sizeof_address;
  222. static unsigned int get_filenum (const char *, unsigned int);
  223.  
  224. #ifndef TC_DWARF2_EMIT_OFFSET
  225. #define TC_DWARF2_EMIT_OFFSET  generic_dwarf2_emit_offset
  226.  
  227. /* Create an offset to .dwarf2_*.  */
  228.  
  229. static void
  230. generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
  231. {
  232.   expressionS exp;
  233.  
  234.   exp.X_op = O_symbol;
  235.   exp.X_add_symbol = symbol;
  236.   exp.X_add_number = 0;
  237.   emit_expr (&exp, size);
  238. }
  239. #endif
  240.  
  241. /* Find or create (if CREATE_P) an entry for SEG+SUBSEG in ALL_SEGS.  */
  242.  
  243. static struct line_subseg *
  244. get_line_subseg (segT seg, subsegT subseg, bfd_boolean create_p)
  245. {
  246.   static segT last_seg;
  247.   static subsegT last_subseg;
  248.   static struct line_subseg *last_line_subseg;
  249.  
  250.   struct line_seg *s;
  251.   struct line_subseg **pss, *lss;
  252.  
  253.   if (seg == last_seg && subseg == last_subseg)
  254.     return last_line_subseg;
  255.  
  256.   s = (struct line_seg *) hash_find (all_segs_hash, seg->name);
  257.   if (s == NULL)
  258.     {
  259.       if (!create_p)
  260.         return NULL;
  261.  
  262.       s = (struct line_seg *) xmalloc (sizeof (*s));
  263.       s->next = NULL;
  264.       s->seg = seg;
  265.       s->head = NULL;
  266.       *last_seg_ptr = s;
  267.       last_seg_ptr = &s->next;
  268.       hash_insert (all_segs_hash, seg->name, s);
  269.     }
  270.   gas_assert (seg == s->seg);
  271.  
  272.   for (pss = &s->head; (lss = *pss) != NULL ; pss = &lss->next)
  273.     {
  274.       if (lss->subseg == subseg)
  275.         goto found_subseg;
  276.       if (lss->subseg > subseg)
  277.         break;
  278.     }
  279.  
  280.   lss = (struct line_subseg *) xmalloc (sizeof (*lss));
  281.   lss->next = *pss;
  282.   lss->subseg = subseg;
  283.   lss->head = NULL;
  284.   lss->ptail = &lss->head;
  285.   lss->pmove_tail = &lss->head;
  286.   *pss = lss;
  287.  
  288.  found_subseg:
  289.   last_seg = seg;
  290.   last_subseg = subseg;
  291.   last_line_subseg = lss;
  292.  
  293.   return lss;
  294. }
  295.  
  296. /* Record an entry for LOC occurring at LABEL.  */
  297.  
  298. static void
  299. dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
  300. {
  301.   struct line_subseg *lss;
  302.   struct line_entry *e;
  303.  
  304.   e = (struct line_entry *) xmalloc (sizeof (*e));
  305.   e->next = NULL;
  306.   e->label = label;
  307.   e->loc = *loc;
  308.  
  309.   lss = get_line_subseg (now_seg, now_subseg, TRUE);
  310.   *lss->ptail = e;
  311.   lss->ptail = &e->next;
  312. }
  313.  
  314. /* Record an entry for LOC occurring at OFS within the current fragment.  */
  315.  
  316. void
  317. dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
  318. {
  319.   static unsigned int line = -1;
  320.   static unsigned int filenum = -1;
  321.  
  322.   symbolS *sym;
  323.  
  324.   /* Early out for as-yet incomplete location information.  */
  325.   if (loc->filenum == 0 || loc->line == 0)
  326.     return;
  327.  
  328.   /* Don't emit sequences of line symbols for the same line when the
  329.      symbols apply to assembler code.  It is necessary to emit
  330.      duplicate line symbols when a compiler asks for them, because GDB
  331.      uses them to determine the end of the prologue.  */
  332.   if (debug_type == DEBUG_DWARF2
  333.       && line == loc->line && filenum == loc->filenum)
  334.     return;
  335.  
  336.   line = loc->line;
  337.   filenum = loc->filenum;
  338.  
  339.   if (linkrelax)
  340.     {
  341.       char name[120];
  342.  
  343.       /* Use a non-fake name for the line number location,
  344.          so that it can be referred to by relocations.  */
  345.       sprintf (name, ".Loc.%u.%u", line, filenum);
  346.       sym = symbol_new (name, now_seg, ofs, frag_now);
  347.     }
  348.   else
  349.     sym = symbol_temp_new (now_seg, ofs, frag_now);
  350.   dwarf2_gen_line_info_1 (sym, loc);
  351. }
  352.  
  353. /* Returns the current source information.  If .file directives have
  354.    been encountered, the info for the corresponding source file is
  355.    returned.  Otherwise, the info for the assembly source file is
  356.    returned.  */
  357.  
  358. void
  359. dwarf2_where (struct dwarf2_line_info *line)
  360. {
  361.   if (debug_type == DEBUG_DWARF2)
  362.     {
  363.       char *filename;
  364.       as_where (&filename, &line->line);
  365.       line->filenum = get_filenum (filename, 0);
  366.       line->column = 0;
  367.       line->flags = DWARF2_FLAG_IS_STMT;
  368.       line->isa = current.isa;
  369.       line->discriminator = current.discriminator;
  370.     }
  371.   else
  372.     *line = current;
  373. }
  374.  
  375. /* A hook to allow the target backend to inform the line number state
  376.    machine of isa changes when assembler debug info is enabled.  */
  377.  
  378. void
  379. dwarf2_set_isa (unsigned int isa)
  380. {
  381.   current.isa = isa;
  382. }
  383.  
  384. /* Called for each machine instruction, or relatively atomic group of
  385.    machine instructions (ie built-in macro).  The instruction or group
  386.    is SIZE bytes in length.  If dwarf2 line number generation is called
  387.    for, emit a line statement appropriately.  */
  388.  
  389. void
  390. dwarf2_emit_insn (int size)
  391. {
  392.   struct dwarf2_line_info loc;
  393.  
  394.   if (!dwarf2_loc_directive_seen && debug_type != DEBUG_DWARF2)
  395.     return;
  396.  
  397.   dwarf2_where (&loc);
  398.  
  399.   dwarf2_gen_line_info (frag_now_fix () - size, &loc);
  400.   dwarf2_consume_line_info ();
  401. }
  402.  
  403. /* Move all previously-emitted line entries for the current position by
  404.    DELTA bytes.  This function cannot be used to move the same entries
  405.    twice.  */
  406.  
  407. void
  408. dwarf2_move_insn (int delta)
  409. {
  410.   struct line_subseg *lss;
  411.   struct line_entry *e;
  412.   valueT now;
  413.  
  414.   if (delta == 0)
  415.     return;
  416.  
  417.   lss = get_line_subseg (now_seg, now_subseg, FALSE);
  418.   if (!lss)
  419.     return;
  420.  
  421.   now = frag_now_fix ();
  422.   while ((e = *lss->pmove_tail))
  423.     {
  424.       if (S_GET_VALUE (e->label) == now)
  425.         S_SET_VALUE (e->label, now + delta);
  426.       lss->pmove_tail = &e->next;
  427.     }
  428. }
  429.  
  430. /* Called after the current line information has been either used with
  431.    dwarf2_gen_line_info or saved with a machine instruction for later use.
  432.    This resets the state of the line number information to reflect that
  433.    it has been used.  */
  434.  
  435. void
  436. dwarf2_consume_line_info (void)
  437. {
  438.   /* Unless we generate DWARF2 debugging information for each
  439.      assembler line, we only emit one line symbol for one LOC.  */
  440.   dwarf2_loc_directive_seen = FALSE;
  441.  
  442.   current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
  443.                      | DWARF2_FLAG_PROLOGUE_END
  444.                      | DWARF2_FLAG_EPILOGUE_BEGIN);
  445.   current.discriminator = 0;
  446. }
  447.  
  448. /* Called for each (preferably code) label.  If dwarf2_loc_mark_labels
  449.    is enabled, emit a basic block marker.  */
  450.  
  451. void
  452. dwarf2_emit_label (symbolS *label)
  453. {
  454.   struct dwarf2_line_info loc;
  455.  
  456.   if (!dwarf2_loc_mark_labels)
  457.     return;
  458.   if (S_GET_SEGMENT (label) != now_seg)
  459.     return;
  460.   if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE))
  461.     return;
  462.   if (files_in_use == 0 && debug_type != DEBUG_DWARF2)
  463.     return;
  464.  
  465.   dwarf2_where (&loc);
  466.  
  467.   loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
  468.  
  469.   dwarf2_gen_line_info_1 (label, &loc);
  470.   dwarf2_consume_line_info ();
  471. }
  472.  
  473. /* Get a .debug_line file number for FILENAME.  If NUM is nonzero,
  474.    allocate it on that file table slot, otherwise return the first
  475.    empty one.  */
  476.  
  477. static unsigned int
  478. get_filenum (const char *filename, unsigned int num)
  479. {
  480.   static unsigned int last_used, last_used_dir_len;
  481.   const char *file;
  482.   size_t dir_len;
  483.   unsigned int i, dir;
  484.  
  485.   if (num == 0 && last_used)
  486.     {
  487.       if (! files[last_used].dir
  488.           && filename_cmp (filename, files[last_used].filename) == 0)
  489.         return last_used;
  490.       if (files[last_used].dir
  491.           && filename_ncmp (filename, dirs[files[last_used].dir],
  492.                             last_used_dir_len) == 0
  493.           && IS_DIR_SEPARATOR (filename [last_used_dir_len])
  494.           && filename_cmp (filename + last_used_dir_len + 1,
  495.                            files[last_used].filename) == 0)
  496.         return last_used;
  497.     }
  498.  
  499.   file = lbasename (filename);
  500.   /* Don't make empty string from / or A: from A:/ .  */
  501. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  502.   if (file <= filename + 3)
  503.     file = filename;
  504. #else
  505.   if (file == filename + 1)
  506.     file = filename;
  507. #endif
  508.   dir_len = file - filename;
  509.  
  510.   dir = 0;
  511.   if (dir_len)
  512.     {
  513. #ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR
  514.       --dir_len;
  515. #endif
  516.       for (dir = 1; dir < dirs_in_use; ++dir)
  517.         if (filename_ncmp (filename, dirs[dir], dir_len) == 0
  518.             && dirs[dir][dir_len] == '\0')
  519.           break;
  520.  
  521.       if (dir >= dirs_in_use)
  522.         {
  523.           if (dir >= dirs_allocated)
  524.             {
  525.               dirs_allocated = dir + 32;
  526.               dirs = (char **)
  527.                      xrealloc (dirs, (dir + 32) * sizeof (const char *));
  528.             }
  529.  
  530.           dirs[dir] = (char *) xmalloc (dir_len + 1);
  531.           memcpy (dirs[dir], filename, dir_len);
  532.           dirs[dir][dir_len] = '\0';
  533.           dirs_in_use = dir + 1;
  534.         }
  535.     }
  536.  
  537.   if (num == 0)
  538.     {
  539.       for (i = 1; i < files_in_use; ++i)
  540.         if (files[i].dir == dir
  541.             && files[i].filename
  542.             && filename_cmp (file, files[i].filename) == 0)
  543.           {
  544.             last_used = i;
  545.             last_used_dir_len = dir_len;
  546.             return i;
  547.           }
  548.     }
  549.   else
  550.     i = num;
  551.  
  552.   if (i >= files_allocated)
  553.     {
  554.       unsigned int old = files_allocated;
  555.  
  556.       files_allocated = i + 32;
  557.       files = (struct file_entry *)
  558.         xrealloc (files, (i + 32) * sizeof (struct file_entry));
  559.  
  560.       memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
  561.     }
  562.  
  563.   files[i].filename = num ? file : xstrdup (file);
  564.   files[i].dir = dir;
  565.   if (files_in_use < i + 1)
  566.     files_in_use = i + 1;
  567.   last_used = i;
  568.   last_used_dir_len = dir_len;
  569.  
  570.   return i;
  571. }
  572.  
  573. /* Handle two forms of .file directive:
  574.    - Pass .file "source.c" to s_app_file
  575.    - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
  576.  
  577.    If an entry is added to the file table, return a pointer to the filename. */
  578.  
  579. char *
  580. dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
  581. {
  582.   offsetT num;
  583.   char *filename;
  584.   int filename_len;
  585.  
  586.   /* Continue to accept a bare string and pass it off.  */
  587.   SKIP_WHITESPACE ();
  588.   if (*input_line_pointer == '"')
  589.     {
  590.       s_app_file (0);
  591.       return NULL;
  592.     }
  593.  
  594.   num = get_absolute_expression ();
  595.   filename = demand_copy_C_string (&filename_len);
  596.   if (filename == NULL)
  597.     return NULL;
  598.   demand_empty_rest_of_line ();
  599.  
  600.   if (num < 1)
  601.     {
  602.       as_bad (_("file number less than one"));
  603.       return NULL;
  604.     }
  605.  
  606.   /* A .file directive implies compiler generated debug information is
  607.      being supplied.  Turn off gas generated debug info.  */
  608.   debug_type = DEBUG_NONE;
  609.  
  610.   if (num < (int) files_in_use && files[num].filename != 0)
  611.     {
  612.       as_bad (_("file number %ld already allocated"), (long) num);
  613.       return NULL;
  614.     }
  615.  
  616.   get_filenum (filename, num);
  617.  
  618.   return filename;
  619. }
  620.  
  621. void
  622. dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
  623. {
  624.   offsetT filenum, line;
  625.  
  626.   /* If we see two .loc directives in a row, force the first one to be
  627.      output now.  */
  628.   if (dwarf2_loc_directive_seen)
  629.     dwarf2_emit_insn (0);
  630.  
  631.   filenum = get_absolute_expression ();
  632.   SKIP_WHITESPACE ();
  633.   line = get_absolute_expression ();
  634.  
  635.   if (filenum < 1)
  636.     {
  637.       as_bad (_("file number less than one"));
  638.       return;
  639.     }
  640.   if (filenum >= (int) files_in_use || files[filenum].filename == 0)
  641.     {
  642.       as_bad (_("unassigned file number %ld"), (long) filenum);
  643.       return;
  644.     }
  645.  
  646.   current.filenum = filenum;
  647.   current.line = line;
  648.   current.discriminator = 0;
  649.  
  650. #ifndef NO_LISTING
  651.   if (listing)
  652.     {
  653.       if (files[filenum].dir)
  654.         {
  655.           size_t dir_len = strlen (dirs[files[filenum].dir]);
  656.           size_t file_len = strlen (files[filenum].filename);
  657.           char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
  658.  
  659.           memcpy (cp, dirs[files[filenum].dir], dir_len);
  660.           INSERT_DIR_SEPARATOR (cp, dir_len);
  661.           memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
  662.           cp[dir_len + file_len + 1] = '\0';
  663.           listing_source_file (cp);
  664.         }
  665.       else
  666.         listing_source_file (files[filenum].filename);
  667.       listing_source_line (line);
  668.     }
  669. #endif
  670.  
  671.   SKIP_WHITESPACE ();
  672.   if (ISDIGIT (*input_line_pointer))
  673.     {
  674.       current.column = get_absolute_expression ();
  675.       SKIP_WHITESPACE ();
  676.     }
  677.  
  678.   while (ISALPHA (*input_line_pointer))
  679.     {
  680.       char *p, c;
  681.       offsetT value;
  682.  
  683.       p = input_line_pointer;
  684.       c = get_symbol_end ();
  685.  
  686.       if (strcmp (p, "basic_block") == 0)
  687.         {
  688.           current.flags |= DWARF2_FLAG_BASIC_BLOCK;
  689.           *input_line_pointer = c;
  690.         }
  691.       else if (strcmp (p, "prologue_end") == 0)
  692.         {
  693.           current.flags |= DWARF2_FLAG_PROLOGUE_END;
  694.           *input_line_pointer = c;
  695.         }
  696.       else if (strcmp (p, "epilogue_begin") == 0)
  697.         {
  698.           current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
  699.           *input_line_pointer = c;
  700.         }
  701.       else if (strcmp (p, "is_stmt") == 0)
  702.         {
  703.           *input_line_pointer = c;
  704.           value = get_absolute_expression ();
  705.           if (value == 0)
  706.             current.flags &= ~DWARF2_FLAG_IS_STMT;
  707.           else if (value == 1)
  708.             current.flags |= DWARF2_FLAG_IS_STMT;
  709.           else
  710.             {
  711.               as_bad (_("is_stmt value not 0 or 1"));
  712.               return;
  713.             }
  714.         }
  715.       else if (strcmp (p, "isa") == 0)
  716.         {
  717.           *input_line_pointer = c;
  718.           value = get_absolute_expression ();
  719.           if (value >= 0)
  720.             current.isa = value;
  721.           else
  722.             {
  723.               as_bad (_("isa number less than zero"));
  724.               return;
  725.             }
  726.         }
  727.       else if (strcmp (p, "discriminator") == 0)
  728.         {
  729.           *input_line_pointer = c;
  730.           value = get_absolute_expression ();
  731.           if (value >= 0)
  732.             current.discriminator = value;
  733.           else
  734.             {
  735.               as_bad (_("discriminator less than zero"));
  736.               return;
  737.             }
  738.         }
  739.       else
  740.         {
  741.           as_bad (_("unknown .loc sub-directive `%s'"), p);
  742.           *input_line_pointer = c;
  743.           return;
  744.         }
  745.  
  746.       SKIP_WHITESPACE ();
  747.     }
  748.  
  749.   demand_empty_rest_of_line ();
  750.   dwarf2_loc_directive_seen = TRUE;
  751.   debug_type = DEBUG_NONE;
  752. }
  753.  
  754. void
  755. dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
  756. {
  757.   offsetT value = get_absolute_expression ();
  758.  
  759.   if (value != 0 && value != 1)
  760.     {
  761.       as_bad (_("expected 0 or 1"));
  762.       ignore_rest_of_line ();
  763.     }
  764.   else
  765.     {
  766.       dwarf2_loc_mark_labels = value != 0;
  767.       demand_empty_rest_of_line ();
  768.     }
  769. }
  770. static struct frag *
  771. first_frag_for_seg (segT seg)
  772. {
  773.   return seg_info (seg)->frchainP->frch_root;
  774. }
  775.  
  776. static struct frag *
  777. last_frag_for_seg (segT seg)
  778. {
  779.   frchainS *f = seg_info (seg)->frchainP;
  780.  
  781.   while (f->frch_next != NULL)
  782.     f = f->frch_next;
  783.  
  784.   return f->frch_last;
  785. }
  786. /* Emit a single byte into the current segment.  */
  787.  
  788. static inline void
  789. out_byte (int byte)
  790. {
  791.   FRAG_APPEND_1_CHAR (byte);
  792. }
  793.  
  794. /* Emit a statement program opcode into the current segment.  */
  795.  
  796. static inline void
  797. out_opcode (int opc)
  798. {
  799.   out_byte (opc);
  800. }
  801.  
  802. /* Emit a two-byte word into the current segment.  */
  803.  
  804. static inline void
  805. out_two (int data)
  806. {
  807.   md_number_to_chars (frag_more (2), data, 2);
  808. }
  809.  
  810. /* Emit a four byte word into the current segment.  */
  811.  
  812. static inline void
  813. out_four (int data)
  814. {
  815.   md_number_to_chars (frag_more (4), data, 4);
  816. }
  817.  
  818. /* Emit an unsigned "little-endian base 128" number.  */
  819.  
  820. static void
  821. out_uleb128 (addressT value)
  822. {
  823.   output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
  824. }
  825.  
  826. /* Emit a signed "little-endian base 128" number.  */
  827.  
  828. static void
  829. out_leb128 (addressT value)
  830. {
  831.   output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
  832. }
  833.  
  834. /* Emit a tuple for .debug_abbrev.  */
  835.  
  836. static inline void
  837. out_abbrev (int name, int form)
  838. {
  839.   out_uleb128 (name);
  840.   out_uleb128 (form);
  841. }
  842.  
  843. /* Get the size of a fragment.  */
  844.  
  845. static offsetT
  846. get_frag_fix (fragS *frag, segT seg)
  847. {
  848.   frchainS *fr;
  849.  
  850.   if (frag->fr_next)
  851.     return frag->fr_fix;
  852.  
  853.   /* If a fragment is the last in the chain, special measures must be
  854.      taken to find its size before relaxation, since it may be pending
  855.      on some subsegment chain.  */
  856.   for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
  857.     if (fr->frch_last == frag)
  858.       return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
  859.  
  860.   abort ();
  861. }
  862.  
  863. /* Set an absolute address (may result in a relocation entry).  */
  864.  
  865. static void
  866. out_set_addr (symbolS *sym)
  867. {
  868.   expressionS exp;
  869.  
  870.   out_opcode (DW_LNS_extended_op);
  871.   out_uleb128 (sizeof_address + 1);
  872.  
  873.   out_opcode (DW_LNE_set_address);
  874.   exp.X_op = O_symbol;
  875.   exp.X_add_symbol = sym;
  876.   exp.X_add_number = 0;
  877.   emit_expr (&exp, sizeof_address);
  878. }
  879.  
  880. static void scale_addr_delta (addressT *);
  881.  
  882. static void
  883. scale_addr_delta (addressT *addr_delta)
  884. {
  885.   static int printed_this = 0;
  886.   if (DWARF2_LINE_MIN_INSN_LENGTH > 1)
  887.     {
  888.       if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0  && !printed_this)
  889.         {
  890.           as_bad("unaligned opcodes detected in executable segment");
  891.           printed_this = 1;
  892.         }
  893.       *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
  894.     }
  895. }
  896.  
  897. /* Encode a pair of line and address skips as efficiently as possible.
  898.    Note that the line skip is signed, whereas the address skip is unsigned.
  899.  
  900.    The following two routines *must* be kept in sync.  This is
  901.    enforced by making emit_inc_line_addr abort if we do not emit
  902.    exactly the expected number of bytes.  */
  903.  
  904. static int
  905. size_inc_line_addr (int line_delta, addressT addr_delta)
  906. {
  907.   unsigned int tmp, opcode;
  908.   int len = 0;
  909.  
  910.   /* Scale the address delta by the minimum instruction length.  */
  911.   scale_addr_delta (&addr_delta);
  912.  
  913.   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
  914.      We cannot use special opcodes here, since we want the end_sequence
  915.      to emit the matrix entry.  */
  916.   if (line_delta == INT_MAX)
  917.     {
  918.       if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
  919.         len = 1;
  920.       else
  921.         len = 1 + sizeof_leb128 (addr_delta, 0);
  922.       return len + 3;
  923.     }
  924.  
  925.   /* Bias the line delta by the base.  */
  926.   tmp = line_delta - DWARF2_LINE_BASE;
  927.  
  928.   /* If the line increment is out of range of a special opcode, we
  929.      must encode it with DW_LNS_advance_line.  */
  930.   if (tmp >= DWARF2_LINE_RANGE)
  931.     {
  932.       len = 1 + sizeof_leb128 (line_delta, 1);
  933.       line_delta = 0;
  934.       tmp = 0 - DWARF2_LINE_BASE;
  935.     }
  936.  
  937.   /* Bias the opcode by the special opcode base.  */
  938.   tmp += DWARF2_LINE_OPCODE_BASE;
  939.  
  940.   /* Avoid overflow when addr_delta is large.  */
  941.   if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
  942.     {
  943.       /* Try using a special opcode.  */
  944.       opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
  945.       if (opcode <= 255)
  946.         return len + 1;
  947.  
  948.       /* Try using DW_LNS_const_add_pc followed by special op.  */
  949.       opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
  950.       if (opcode <= 255)
  951.         return len + 2;
  952.     }
  953.  
  954.   /* Otherwise use DW_LNS_advance_pc.  */
  955.   len += 1 + sizeof_leb128 (addr_delta, 0);
  956.  
  957.   /* DW_LNS_copy or special opcode.  */
  958.   len += 1;
  959.  
  960.   return len;
  961. }
  962.  
  963. static void
  964. emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
  965. {
  966.   unsigned int tmp, opcode;
  967.   int need_copy = 0;
  968.   char *end = p + len;
  969.  
  970.   /* Line number sequences cannot go backward in addresses.  This means
  971.      we've incorrectly ordered the statements in the sequence.  */
  972.   gas_assert ((offsetT) addr_delta >= 0);
  973.  
  974.   /* Scale the address delta by the minimum instruction length.  */
  975.   scale_addr_delta (&addr_delta);
  976.  
  977.   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
  978.      We cannot use special opcodes here, since we want the end_sequence
  979.      to emit the matrix entry.  */
  980.   if (line_delta == INT_MAX)
  981.     {
  982.       if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
  983.         *p++ = DW_LNS_const_add_pc;
  984.       else
  985.         {
  986.           *p++ = DW_LNS_advance_pc;
  987.           p += output_leb128 (p, addr_delta, 0);
  988.         }
  989.  
  990.       *p++ = DW_LNS_extended_op;
  991.       *p++ = 1;
  992.       *p++ = DW_LNE_end_sequence;
  993.       goto done;
  994.     }
  995.  
  996.   /* Bias the line delta by the base.  */
  997.   tmp = line_delta - DWARF2_LINE_BASE;
  998.  
  999.   /* If the line increment is out of range of a special opcode, we
  1000.      must encode it with DW_LNS_advance_line.  */
  1001.   if (tmp >= DWARF2_LINE_RANGE)
  1002.     {
  1003.       *p++ = DW_LNS_advance_line;
  1004.       p += output_leb128 (p, line_delta, 1);
  1005.  
  1006.       line_delta = 0;
  1007.       tmp = 0 - DWARF2_LINE_BASE;
  1008.       need_copy = 1;
  1009.     }
  1010.  
  1011.   /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
  1012.      special opcode.  */
  1013.   if (line_delta == 0 && addr_delta == 0)
  1014.     {
  1015.       *p++ = DW_LNS_copy;
  1016.       goto done;
  1017.     }
  1018.  
  1019.   /* Bias the opcode by the special opcode base.  */
  1020.   tmp += DWARF2_LINE_OPCODE_BASE;
  1021.  
  1022.   /* Avoid overflow when addr_delta is large.  */
  1023.   if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
  1024.     {
  1025.       /* Try using a special opcode.  */
  1026.       opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
  1027.       if (opcode <= 255)
  1028.         {
  1029.           *p++ = opcode;
  1030.           goto done;
  1031.         }
  1032.  
  1033.       /* Try using DW_LNS_const_add_pc followed by special op.  */
  1034.       opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
  1035.       if (opcode <= 255)
  1036.         {
  1037.           *p++ = DW_LNS_const_add_pc;
  1038.           *p++ = opcode;
  1039.           goto done;
  1040.         }
  1041.     }
  1042.  
  1043.   /* Otherwise use DW_LNS_advance_pc.  */
  1044.   *p++ = DW_LNS_advance_pc;
  1045.   p += output_leb128 (p, addr_delta, 0);
  1046.  
  1047.   if (need_copy)
  1048.     *p++ = DW_LNS_copy;
  1049.   else
  1050.     *p++ = tmp;
  1051.  
  1052.  done:
  1053.   gas_assert (p == end);
  1054. }
  1055.  
  1056. /* Handy routine to combine calls to the above two routines.  */
  1057.  
  1058. static void
  1059. out_inc_line_addr (int line_delta, addressT addr_delta)
  1060. {
  1061.   int len = size_inc_line_addr (line_delta, addr_delta);
  1062.   emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
  1063. }
  1064.  
  1065. /* Write out an alternative form of line and address skips using
  1066.    DW_LNS_fixed_advance_pc opcodes.  This uses more space than the default
  1067.    line and address information, but it is required if linker relaxation
  1068.    could change the code offsets.  The following two routines *must* be
  1069.    kept in sync.  */
  1070. #define ADDR_DELTA_LIMIT 50000
  1071.  
  1072. static int
  1073. size_fixed_inc_line_addr (int line_delta, addressT addr_delta)
  1074. {
  1075.   int len = 0;
  1076.  
  1077.   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.  */
  1078.   if (line_delta != INT_MAX)
  1079.     len = 1 + sizeof_leb128 (line_delta, 1);
  1080.  
  1081.   if (addr_delta > ADDR_DELTA_LIMIT)
  1082.     {
  1083.       /* DW_LNS_extended_op */
  1084.       len += 1 + sizeof_leb128 (sizeof_address + 1, 0);
  1085.       /* DW_LNE_set_address */
  1086.       len += 1 + sizeof_address;
  1087.     }
  1088.   else
  1089.     /* DW_LNS_fixed_advance_pc */
  1090.     len += 3;
  1091.  
  1092.   if (line_delta == INT_MAX)
  1093.     /* DW_LNS_extended_op + DW_LNE_end_sequence */
  1094.     len += 3;
  1095.   else
  1096.     /* DW_LNS_copy */
  1097.     len += 1;
  1098.  
  1099.   return len;
  1100. }
  1101.  
  1102. static void
  1103. emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag,
  1104.                           char *p, int len)
  1105. {
  1106.   expressionS *pexp;
  1107.   char *end = p + len;
  1108.  
  1109.   /* Line number sequences cannot go backward in addresses.  This means
  1110.      we've incorrectly ordered the statements in the sequence.  */
  1111.   gas_assert ((offsetT) addr_delta >= 0);
  1112.  
  1113.   /* Verify that we have kept in sync with size_fixed_inc_line_addr.  */
  1114.   gas_assert (len == size_fixed_inc_line_addr (line_delta, addr_delta));
  1115.  
  1116.   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.  */
  1117.   if (line_delta != INT_MAX)
  1118.     {
  1119.       *p++ = DW_LNS_advance_line;
  1120.       p += output_leb128 (p, line_delta, 1);
  1121.     }
  1122.  
  1123.   pexp = symbol_get_value_expression (frag->fr_symbol);
  1124.  
  1125.   /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can
  1126.      advance the address by at most 64K.  Linker relaxation (without
  1127.      which this function would not be used) could change the operand by
  1128.      an unknown amount.  If the address increment is getting close to
  1129.      the limit, just reset the address.  */
  1130.   if (addr_delta > ADDR_DELTA_LIMIT)
  1131.     {
  1132.       symbolS *to_sym;
  1133.       expressionS exp;
  1134.  
  1135.       gas_assert (pexp->X_op == O_subtract);
  1136.       to_sym = pexp->X_add_symbol;
  1137.  
  1138.       *p++ = DW_LNS_extended_op;
  1139.       p += output_leb128 (p, sizeof_address + 1, 0);
  1140.       *p++ = DW_LNE_set_address;
  1141.       exp.X_op = O_symbol;
  1142.       exp.X_add_symbol = to_sym;
  1143.       exp.X_add_number = 0;
  1144.       emit_expr_fix (&exp, sizeof_address, frag, p);
  1145.       p += sizeof_address;
  1146.     }
  1147.   else
  1148.     {
  1149.       *p++ = DW_LNS_fixed_advance_pc;
  1150.       emit_expr_fix (pexp, 2, frag, p);
  1151.       p += 2;
  1152.     }
  1153.  
  1154.   if (line_delta == INT_MAX)
  1155.     {
  1156.       *p++ = DW_LNS_extended_op;
  1157.       *p++ = 1;
  1158.       *p++ = DW_LNE_end_sequence;
  1159.     }
  1160.   else
  1161.     *p++ = DW_LNS_copy;
  1162.  
  1163.   gas_assert (p == end);
  1164. }
  1165.  
  1166. /* Generate a variant frag that we can use to relax address/line
  1167.    increments between fragments of the target segment.  */
  1168.  
  1169. static void
  1170. relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
  1171. {
  1172.   expressionS exp;
  1173.   int max_chars;
  1174.  
  1175.   exp.X_op = O_subtract;
  1176.   exp.X_add_symbol = to_sym;
  1177.   exp.X_op_symbol = from_sym;
  1178.   exp.X_add_number = 0;
  1179.  
  1180.   /* The maximum size of the frag is the line delta with a maximum
  1181.      sized address delta.  */
  1182.   if (DWARF2_USE_FIXED_ADVANCE_PC)
  1183.     max_chars = size_fixed_inc_line_addr (line_delta,
  1184.                                           -DWARF2_LINE_MIN_INSN_LENGTH);
  1185.   else
  1186.     max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
  1187.  
  1188.   frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
  1189.             make_expr_symbol (&exp), line_delta, NULL);
  1190. }
  1191.  
  1192. /* The function estimates the size of a rs_dwarf2dbg variant frag
  1193.    based on the current values of the symbols.  It is called before
  1194.    the relaxation loop.  We set fr_subtype to the expected length.  */
  1195.  
  1196. int
  1197. dwarf2dbg_estimate_size_before_relax (fragS *frag)
  1198. {
  1199.   offsetT addr_delta;
  1200.   int size;
  1201.  
  1202.   addr_delta = resolve_symbol_value (frag->fr_symbol);
  1203.   if (DWARF2_USE_FIXED_ADVANCE_PC)
  1204.     size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta);
  1205.   else
  1206.     size = size_inc_line_addr (frag->fr_offset, addr_delta);
  1207.  
  1208.   frag->fr_subtype = size;
  1209.  
  1210.   return size;
  1211. }
  1212.  
  1213. /* This function relaxes a rs_dwarf2dbg variant frag based on the
  1214.    current values of the symbols.  fr_subtype is the current length
  1215.    of the frag.  This returns the change in frag length.  */
  1216.  
  1217. int
  1218. dwarf2dbg_relax_frag (fragS *frag)
  1219. {
  1220.   int old_size, new_size;
  1221.  
  1222.   old_size = frag->fr_subtype;
  1223.   new_size = dwarf2dbg_estimate_size_before_relax (frag);
  1224.  
  1225.   return new_size - old_size;
  1226. }
  1227.  
  1228. /* This function converts a rs_dwarf2dbg variant frag into a normal
  1229.    fill frag.  This is called after all relaxation has been done.
  1230.    fr_subtype will be the desired length of the frag.  */
  1231.  
  1232. void
  1233. dwarf2dbg_convert_frag (fragS *frag)
  1234. {
  1235.   offsetT addr_diff;
  1236.  
  1237.   if (DWARF2_USE_FIXED_ADVANCE_PC)
  1238.     {
  1239.       /* If linker relaxation is enabled then the distance bewteen the two
  1240.          symbols in the frag->fr_symbol expression might change.  Hence we
  1241.          cannot rely upon the value computed by resolve_symbol_value.
  1242.          Instead we leave the expression unfinalized and allow
  1243.          emit_fixed_inc_line_addr to create a fixup (which later becomes a
  1244.          relocation) that will allow the linker to correctly compute the
  1245.          actual address difference.  We have to use a fixed line advance for
  1246.          this as we cannot (easily) relocate leb128 encoded values.  */
  1247.       int saved_finalize_syms = finalize_syms;
  1248.  
  1249.       finalize_syms = 0;
  1250.       addr_diff = resolve_symbol_value (frag->fr_symbol);
  1251.       finalize_syms = saved_finalize_syms;
  1252.     }
  1253.   else
  1254.     addr_diff = resolve_symbol_value (frag->fr_symbol);
  1255.  
  1256.   /* fr_var carries the max_chars that we created the fragment with.
  1257.      fr_subtype carries the current expected length.  We must, of
  1258.      course, have allocated enough memory earlier.  */
  1259.   gas_assert (frag->fr_var >= (int) frag->fr_subtype);
  1260.  
  1261.   if (DWARF2_USE_FIXED_ADVANCE_PC)
  1262.     emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag,
  1263.                               frag->fr_literal + frag->fr_fix,
  1264.                               frag->fr_subtype);
  1265.   else
  1266.     emit_inc_line_addr (frag->fr_offset, addr_diff,
  1267.                         frag->fr_literal + frag->fr_fix, frag->fr_subtype);
  1268.  
  1269.   frag->fr_fix += frag->fr_subtype;
  1270.   frag->fr_type = rs_fill;
  1271.   frag->fr_var = 0;
  1272.   frag->fr_offset = 0;
  1273. }
  1274.  
  1275. /* Generate .debug_line content for the chain of line number entries
  1276.    beginning at E, for segment SEG.  */
  1277.  
  1278. static void
  1279. process_entries (segT seg, struct line_entry *e)
  1280. {
  1281.   unsigned filenum = 1;
  1282.   unsigned line = 1;
  1283.   unsigned column = 0;
  1284.   unsigned isa = 0;
  1285.   unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
  1286.   fragS *last_frag = NULL, *frag;
  1287.   addressT last_frag_ofs = 0, frag_ofs;
  1288.   symbolS *last_lab = NULL, *lab;
  1289.   struct line_entry *next;
  1290.  
  1291.   if (flag_dwarf_sections)
  1292.     {
  1293.       char * name;
  1294.       const char * sec_name;
  1295.  
  1296.       /* Switch to the relevent sub-section before we start to emit
  1297.          the line number table.
  1298.  
  1299.          FIXME: These sub-sections do not have a normal Line Number
  1300.          Program Header, thus strictly speaking they are not valid
  1301.          DWARF sections.  Unfortunately the DWARF standard assumes
  1302.          a one-to-one relationship between compilation units and
  1303.          line number tables.  Thus we have to have a .debug_line
  1304.          section, as well as our sub-sections, and we have to ensure
  1305.          that all of the sub-sections are merged into a proper
  1306.          .debug_line section before a debugger sees them.  */
  1307.          
  1308.       sec_name = bfd_get_section_name (stdoutput, seg);
  1309.       if (strcmp (sec_name, ".text") != 0)
  1310.         {
  1311.           unsigned int len;
  1312.  
  1313.           len = strlen (sec_name);
  1314.           name = xmalloc (len + 11 + 2);
  1315.           sprintf (name, ".debug_line%s", sec_name);
  1316.           subseg_set (subseg_get (name, FALSE), 0);
  1317.         }
  1318.       else
  1319.         /* Don't create a .debug_line.text section -
  1320.            that is redundant.  Instead just switch back to the
  1321.            normal .debug_line section.  */
  1322.         subseg_set (subseg_get (".debug_line", FALSE), 0);
  1323.     }
  1324.  
  1325.   do
  1326.     {
  1327.       int line_delta;
  1328.  
  1329.       if (filenum != e->loc.filenum)
  1330.         {
  1331.           filenum = e->loc.filenum;
  1332.           out_opcode (DW_LNS_set_file);
  1333.           out_uleb128 (filenum);
  1334.         }
  1335.  
  1336.       if (column != e->loc.column)
  1337.         {
  1338.           column = e->loc.column;
  1339.           out_opcode (DW_LNS_set_column);
  1340.           out_uleb128 (column);
  1341.         }
  1342.  
  1343.       if (e->loc.discriminator != 0)
  1344.         {
  1345.           out_opcode (DW_LNS_extended_op);
  1346.           out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0));
  1347.           out_opcode (DW_LNE_set_discriminator);
  1348.           out_uleb128 (e->loc.discriminator);
  1349.         }
  1350.  
  1351.       if (isa != e->loc.isa)
  1352.         {
  1353.           isa = e->loc.isa;
  1354.           out_opcode (DW_LNS_set_isa);
  1355.           out_uleb128 (isa);
  1356.         }
  1357.  
  1358.       if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
  1359.         {
  1360.           flags = e->loc.flags;
  1361.           out_opcode (DW_LNS_negate_stmt);
  1362.         }
  1363.  
  1364.       if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
  1365.         out_opcode (DW_LNS_set_basic_block);
  1366.  
  1367.       if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
  1368.         out_opcode (DW_LNS_set_prologue_end);
  1369.  
  1370.       if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
  1371.         out_opcode (DW_LNS_set_epilogue_begin);
  1372.  
  1373.       /* Don't try to optimize away redundant entries; gdb wants two
  1374.          entries for a function where the code starts on the same line as
  1375.          the {, and there's no way to identify that case here.  Trust gcc
  1376.          to optimize appropriately.  */
  1377.       line_delta = e->loc.line - line;
  1378.       lab = e->label;
  1379.       frag = symbol_get_frag (lab);
  1380.       frag_ofs = S_GET_VALUE (lab);
  1381.  
  1382.       if (last_frag == NULL)
  1383.         {
  1384.           out_set_addr (lab);
  1385.           out_inc_line_addr (line_delta, 0);
  1386.         }
  1387.       else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
  1388.         out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
  1389.       else
  1390.         relax_inc_line_addr (line_delta, lab, last_lab);
  1391.  
  1392.       line = e->loc.line;
  1393.       last_lab = lab;
  1394.       last_frag = frag;
  1395.       last_frag_ofs = frag_ofs;
  1396.  
  1397.       next = e->next;
  1398.       free (e);
  1399.       e = next;
  1400.     }
  1401.   while (e);
  1402.  
  1403.   /* Emit a DW_LNE_end_sequence for the end of the section.  */
  1404.   frag = last_frag_for_seg (seg);
  1405.   frag_ofs = get_frag_fix (frag, seg);
  1406.   if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
  1407.     out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
  1408.   else
  1409.     {
  1410.       lab = symbol_temp_new (seg, frag_ofs, frag);
  1411.       relax_inc_line_addr (INT_MAX, lab, last_lab);
  1412.     }
  1413. }
  1414.  
  1415. /* Emit the directory and file tables for .debug_line.  */
  1416.  
  1417. static void
  1418. out_file_list (void)
  1419. {
  1420.   size_t size;
  1421.   const char *dir;
  1422.   char *cp;
  1423.   unsigned int i;
  1424.  
  1425.   /* Emit directory list.  */
  1426.   for (i = 1; i < dirs_in_use; ++i)
  1427.     {
  1428.       dir = remap_debug_filename (dirs[i]);
  1429.       size = strlen (dir) + 1;
  1430.       cp = frag_more (size);
  1431.       memcpy (cp, dir, size);
  1432.     }
  1433.   /* Terminate it.  */
  1434.   out_byte ('\0');
  1435.  
  1436.   for (i = 1; i < files_in_use; ++i)
  1437.     {
  1438.       const char *fullfilename;
  1439.  
  1440.       if (files[i].filename == NULL)
  1441.         {
  1442.           as_bad (_("unassigned file number %ld"), (long) i);
  1443.           /* Prevent a crash later, particularly for file 1.  */
  1444.           files[i].filename = "";
  1445.           continue;
  1446.         }
  1447.  
  1448.       fullfilename = DWARF2_FILE_NAME (files[i].filename,
  1449.                                        files[i].dir ? dirs [files [i].dir] : "");
  1450.       size = strlen (fullfilename) + 1;
  1451.       cp = frag_more (size);
  1452.       memcpy (cp, fullfilename, size);
  1453.  
  1454.       out_uleb128 (files[i].dir);       /* directory number */
  1455.       /* Output the last modification timestamp.  */
  1456.       out_uleb128 (DWARF2_FILE_TIME_NAME (files[i].filename,
  1457.                                           files[i].dir ? dirs [files [i].dir] : ""));
  1458.       /* Output the filesize.  */
  1459.       out_uleb128 (DWARF2_FILE_SIZE_NAME (files[i].filename,
  1460.                                           files[i].dir ? dirs [files [i].dir] : ""));
  1461.     }
  1462.  
  1463.   /* Terminate filename list.  */
  1464.   out_byte (0);
  1465. }
  1466.  
  1467. /* Switch to SEC and output a header length field.  Return the size of
  1468.    offsets used in SEC.  The caller must set EXPR->X_add_symbol value
  1469.    to the end of the section.  */
  1470.  
  1471. static int
  1472. out_header (asection *sec, expressionS *exp)
  1473. {
  1474.   symbolS *start_sym;
  1475.   symbolS *end_sym;
  1476.  
  1477.   subseg_set (sec, 0);
  1478.   start_sym = symbol_temp_new_now ();
  1479.   end_sym = symbol_temp_make ();
  1480.  
  1481.   /* Total length of the information.  */
  1482.   exp->X_op = O_subtract;
  1483.   exp->X_add_symbol = end_sym;
  1484.   exp->X_op_symbol = start_sym;
  1485.  
  1486.   switch (DWARF2_FORMAT (sec))
  1487.     {
  1488.     case dwarf2_format_32bit:
  1489.       exp->X_add_number = -4;
  1490.       emit_expr (exp, 4);
  1491.       return 4;
  1492.  
  1493.     case dwarf2_format_64bit:
  1494.       exp->X_add_number = -12;
  1495.       out_four (-1);
  1496.       emit_expr (exp, 8);
  1497.       return 8;
  1498.  
  1499.     case dwarf2_format_64bit_irix:
  1500.       exp->X_add_number = -8;
  1501.       emit_expr (exp, 8);
  1502.       return 8;
  1503.     }
  1504.  
  1505.   as_fatal (_("internal error: unknown dwarf2 format"));
  1506.   return 0;
  1507. }
  1508.  
  1509. /* Emit the collected .debug_line data.  */
  1510.  
  1511. static void
  1512. out_debug_line (segT line_seg)
  1513. {
  1514.   expressionS exp;
  1515.   symbolS *prologue_end;
  1516.   symbolS *line_end;
  1517.   struct line_seg *s;
  1518.   int sizeof_offset;
  1519.  
  1520.   sizeof_offset = out_header (line_seg, &exp);
  1521.   line_end = exp.X_add_symbol;
  1522.  
  1523.   /* Version.  */
  1524.   out_two (DWARF2_LINE_VERSION);
  1525.  
  1526.   /* Length of the prologue following this length.  */
  1527.   prologue_end = symbol_temp_make ();
  1528.   exp.X_add_symbol = prologue_end;
  1529.   exp.X_add_number = - (4 + 2 + 4);
  1530.   emit_expr (&exp, sizeof_offset);
  1531.  
  1532.   /* Parameters of the state machine.  */
  1533.   out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
  1534.   out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
  1535.   out_byte (DWARF2_LINE_BASE);
  1536.   out_byte (DWARF2_LINE_RANGE);
  1537.   out_byte (DWARF2_LINE_OPCODE_BASE);
  1538.  
  1539.   /* Standard opcode lengths.  */
  1540.   out_byte (0);                 /* DW_LNS_copy */
  1541.   out_byte (1);                 /* DW_LNS_advance_pc */
  1542.   out_byte (1);                 /* DW_LNS_advance_line */
  1543.   out_byte (1);                 /* DW_LNS_set_file */
  1544.   out_byte (1);                 /* DW_LNS_set_column */
  1545.   out_byte (0);                 /* DW_LNS_negate_stmt */
  1546.   out_byte (0);                 /* DW_LNS_set_basic_block */
  1547.   out_byte (0);                 /* DW_LNS_const_add_pc */
  1548.   out_byte (1);                 /* DW_LNS_fixed_advance_pc */
  1549.   out_byte (0);                 /* DW_LNS_set_prologue_end */
  1550.   out_byte (0);                 /* DW_LNS_set_epilogue_begin */
  1551.   out_byte (1);                 /* DW_LNS_set_isa */
  1552.  
  1553.   out_file_list ();
  1554.  
  1555.   symbol_set_value_now (prologue_end);
  1556.  
  1557.   /* For each section, emit a statement program.  */
  1558.   for (s = all_segs; s; s = s->next)
  1559.     if (SEG_NORMAL (s->seg))
  1560.       process_entries (s->seg, s->head->head);
  1561.     else
  1562.       as_warn ("dwarf line number information for %s ignored",
  1563.                segment_name (s->seg));
  1564.  
  1565.   if (flag_dwarf_sections)
  1566.     /* We have to switch to the special .debug_line_end section
  1567.        before emitting the end-of-debug_line symbol.  The linker
  1568.        script arranges for this section to be placed after all the
  1569.        (potentially garbage collected) .debug_line.<foo> sections.
  1570.        This section contains the line_end symbol which is used to
  1571.        compute the size of the linked .debug_line section, as seen
  1572.        in the DWARF Line Number header.  */
  1573.     subseg_set (subseg_get (".debug_line_end", FALSE), 0);
  1574.  
  1575.   symbol_set_value_now (line_end);
  1576. }
  1577.  
  1578. static void
  1579. out_debug_ranges (segT ranges_seg)
  1580. {
  1581.   unsigned int addr_size = sizeof_address;
  1582.   struct line_seg *s;
  1583.   expressionS exp;
  1584.   unsigned int i;
  1585.  
  1586.   subseg_set (ranges_seg, 0);
  1587.  
  1588.   /* Base Address Entry.  */
  1589.   for (i = 0; i < addr_size; i++)
  1590.     out_byte (0xff);
  1591.   for (i = 0; i < addr_size; i++)
  1592.     out_byte (0);
  1593.  
  1594.   /* Range List Entry.  */
  1595.   for (s = all_segs; s; s = s->next)
  1596.     {
  1597.       fragS *frag;
  1598.       symbolS *beg, *end;
  1599.  
  1600.       frag = first_frag_for_seg (s->seg);
  1601.       beg = symbol_temp_new (s->seg, 0, frag);
  1602.       s->text_start = beg;
  1603.  
  1604.       frag = last_frag_for_seg (s->seg);
  1605.       end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
  1606.       s->text_end = end;
  1607.  
  1608.       exp.X_op = O_symbol;
  1609.       exp.X_add_symbol = beg;
  1610.       exp.X_add_number = 0;
  1611.       emit_expr (&exp, addr_size);
  1612.  
  1613.       exp.X_op = O_symbol;
  1614.       exp.X_add_symbol = end;
  1615.       exp.X_add_number = 0;
  1616.       emit_expr (&exp, addr_size);
  1617.     }
  1618.  
  1619.   /* End of Range Entry.   */
  1620.   for (i = 0; i < addr_size; i++)
  1621.     out_byte (0);
  1622.   for (i = 0; i < addr_size; i++)
  1623.     out_byte (0);
  1624. }
  1625.  
  1626. /* Emit data for .debug_aranges.  */
  1627.  
  1628. static void
  1629. out_debug_aranges (segT aranges_seg, segT info_seg)
  1630. {
  1631.   unsigned int addr_size = sizeof_address;
  1632.   struct line_seg *s;
  1633.   expressionS exp;
  1634.   symbolS *aranges_end;
  1635.   char *p;
  1636.   int sizeof_offset;
  1637.  
  1638.   sizeof_offset = out_header (aranges_seg, &exp);
  1639.   aranges_end = exp.X_add_symbol;
  1640.  
  1641.   /* Version.  */
  1642.   out_two (DWARF2_ARANGES_VERSION);
  1643.  
  1644.   /* Offset to .debug_info.  */
  1645.   TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset);
  1646.  
  1647.   /* Size of an address (offset portion).  */
  1648.   out_byte (addr_size);
  1649.  
  1650.   /* Size of a segment descriptor.  */
  1651.   out_byte (0);
  1652.  
  1653.   /* Align the header.  */
  1654.   frag_align (ffs (2 * addr_size) - 1, 0, 0);
  1655.  
  1656.   for (s = all_segs; s; s = s->next)
  1657.     {
  1658.       fragS *frag;
  1659.       symbolS *beg, *end;
  1660.  
  1661.       frag = first_frag_for_seg (s->seg);
  1662.       beg = symbol_temp_new (s->seg, 0, frag);
  1663.       s->text_start = beg;
  1664.  
  1665.       frag = last_frag_for_seg (s->seg);
  1666.       end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
  1667.       s->text_end = end;
  1668.  
  1669.       exp.X_op = O_symbol;
  1670.       exp.X_add_symbol = beg;
  1671.       exp.X_add_number = 0;
  1672.       emit_expr (&exp, addr_size);
  1673.  
  1674.       exp.X_op = O_subtract;
  1675.       exp.X_add_symbol = end;
  1676.       exp.X_op_symbol = beg;
  1677.       exp.X_add_number = 0;
  1678.       emit_expr (&exp, addr_size);
  1679.     }
  1680.  
  1681.   p = frag_more (2 * addr_size);
  1682.   md_number_to_chars (p, 0, addr_size);
  1683.   md_number_to_chars (p + addr_size, 0, addr_size);
  1684.  
  1685.   symbol_set_value_now (aranges_end);
  1686. }
  1687.  
  1688. /* Emit data for .debug_abbrev.  Note that this must be kept in
  1689.    sync with out_debug_info below.  */
  1690.  
  1691. static void
  1692. out_debug_abbrev (segT abbrev_seg,
  1693.                   segT info_seg ATTRIBUTE_UNUSED,
  1694.                   segT line_seg ATTRIBUTE_UNUSED)
  1695. {
  1696.   subseg_set (abbrev_seg, 0);
  1697.  
  1698.   out_uleb128 (1);
  1699.   out_uleb128 (DW_TAG_compile_unit);
  1700.   out_byte (DW_CHILDREN_no);
  1701.   if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit)
  1702.     out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
  1703.   else
  1704.     out_abbrev (DW_AT_stmt_list, DW_FORM_data8);
  1705.   if (all_segs->next == NULL)
  1706.     {
  1707.       out_abbrev (DW_AT_low_pc, DW_FORM_addr);
  1708.       if (DWARF2_VERSION < 4)
  1709.         out_abbrev (DW_AT_high_pc, DW_FORM_addr);
  1710.       else
  1711.         out_abbrev (DW_AT_high_pc, (sizeof_address == 4
  1712.                                     ? DW_FORM_data4 : DW_FORM_data8));
  1713.     }
  1714.   else
  1715.     {
  1716.       if (DWARF2_FORMAT (info_seg) == dwarf2_format_32bit)
  1717.         out_abbrev (DW_AT_ranges, DW_FORM_data4);
  1718.       else
  1719.         out_abbrev (DW_AT_ranges, DW_FORM_data8);
  1720.     }
  1721.   out_abbrev (DW_AT_name, DW_FORM_string);
  1722.   out_abbrev (DW_AT_comp_dir, DW_FORM_string);
  1723.   out_abbrev (DW_AT_producer, DW_FORM_string);
  1724.   out_abbrev (DW_AT_language, DW_FORM_data2);
  1725.   out_abbrev (0, 0);
  1726.  
  1727.   /* Terminate the abbreviations for this compilation unit.  */
  1728.   out_byte (0);
  1729. }
  1730.  
  1731. /* Emit a description of this compilation unit for .debug_info.  */
  1732.  
  1733. static void
  1734. out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg)
  1735. {
  1736.   char producer[128];
  1737.   const char *comp_dir;
  1738.   const char *dirname;
  1739.   expressionS exp;
  1740.   symbolS *info_end;
  1741.   char *p;
  1742.   int len;
  1743.   int sizeof_offset;
  1744.  
  1745.   sizeof_offset = out_header (info_seg, &exp);
  1746.   info_end = exp.X_add_symbol;
  1747.  
  1748.   /* DWARF version.  */
  1749.   out_two (DWARF2_VERSION);
  1750.  
  1751.   /* .debug_abbrev offset */
  1752.   TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
  1753.  
  1754.   /* Target address size.  */
  1755.   out_byte (sizeof_address);
  1756.  
  1757.   /* DW_TAG_compile_unit DIE abbrev */
  1758.   out_uleb128 (1);
  1759.  
  1760.   /* DW_AT_stmt_list */
  1761.   TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg),
  1762.                          (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit
  1763.                           ? 4 : 8));
  1764.  
  1765.   /* These two attributes are emitted if all of the code is contiguous.  */
  1766.   if (all_segs->next == NULL)
  1767.     {
  1768.       /* DW_AT_low_pc */
  1769.       exp.X_op = O_symbol;
  1770.       exp.X_add_symbol = all_segs->text_start;
  1771.       exp.X_add_number = 0;
  1772.       emit_expr (&exp, sizeof_address);
  1773.  
  1774.       /* DW_AT_high_pc */
  1775.       if (DWARF2_VERSION < 4)
  1776.         exp.X_op = O_symbol;
  1777.       else
  1778.         {
  1779.           exp.X_op = O_subtract;
  1780.           exp.X_op_symbol = all_segs->text_start;
  1781.         }
  1782.       exp.X_add_symbol = all_segs->text_end;
  1783.       exp.X_add_number = 0;
  1784.       emit_expr (&exp, sizeof_address);
  1785.     }
  1786.   else
  1787.     {
  1788.       /* This attribute is emitted if the code is disjoint.  */
  1789.       /* DW_AT_ranges.  */
  1790.       TC_DWARF2_EMIT_OFFSET (section_symbol (ranges_seg), sizeof_offset);
  1791.     }
  1792.  
  1793.   /* DW_AT_name.  We don't have the actual file name that was present
  1794.      on the command line, so assume files[1] is the main input file.
  1795.      We're not supposed to get called unless at least one line number
  1796.      entry was emitted, so this should always be defined.  */
  1797.   if (files_in_use == 0)
  1798.     abort ();
  1799.   if (files[1].dir)
  1800.     {
  1801.       dirname = remap_debug_filename (dirs[files[1].dir]);
  1802.       len = strlen (dirname);
  1803. #ifdef TE_VMS
  1804.       /* Already has trailing slash.  */
  1805.       p = frag_more (len);
  1806.       memcpy (p, dirname, len);
  1807. #else
  1808.       p = frag_more (len + 1);
  1809.       memcpy (p, dirname, len);
  1810.       INSERT_DIR_SEPARATOR (p, len);
  1811. #endif
  1812.     }
  1813.   len = strlen (files[1].filename) + 1;
  1814.   p = frag_more (len);
  1815.   memcpy (p, files[1].filename, len);
  1816.  
  1817.   /* DW_AT_comp_dir */
  1818.   comp_dir = remap_debug_filename (getpwd ());
  1819.   len = strlen (comp_dir) + 1;
  1820.   p = frag_more (len);
  1821.   memcpy (p, comp_dir, len);
  1822.  
  1823.   /* DW_AT_producer */
  1824.   sprintf (producer, "GNU AS %s", VERSION);
  1825.   len = strlen (producer) + 1;
  1826.   p = frag_more (len);
  1827.   memcpy (p, producer, len);
  1828.  
  1829.   /* DW_AT_language.  Yes, this is probably not really MIPS, but the
  1830.      dwarf2 draft has no standard code for assembler.  */
  1831.   out_two (DW_LANG_Mips_Assembler);
  1832.  
  1833.   symbol_set_value_now (info_end);
  1834. }
  1835.  
  1836. void
  1837. dwarf2_init (void)
  1838. {
  1839.   all_segs_hash = hash_new ();
  1840.   last_seg_ptr = &all_segs;
  1841. }
  1842.  
  1843.  
  1844. /* Finish the dwarf2 debug sections.  We emit .debug.line if there
  1845.    were any .file/.loc directives, or --gdwarf2 was given, or if the
  1846.    file has a non-empty .debug_info section and an empty .debug_line
  1847.    section.  If we emit .debug_line, and the .debug_info section is
  1848.    empty, we also emit .debug_info, .debug_aranges and .debug_abbrev.
  1849.    ALL_SEGS will be non-null if there were any .file/.loc directives,
  1850.    or --gdwarf2 was given and there were any located instructions
  1851.    emitted.  */
  1852.  
  1853. void
  1854. dwarf2_finish (void)
  1855. {
  1856.   segT line_seg;
  1857.   struct line_seg *s;
  1858.   segT info_seg;
  1859.   int emit_other_sections = 0;
  1860.   int empty_debug_line = 0;
  1861.  
  1862.   info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
  1863.   emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
  1864.  
  1865.   line_seg = bfd_get_section_by_name (stdoutput, ".debug_line");
  1866.   empty_debug_line = line_seg == NULL || !seg_not_empty_p (line_seg);
  1867.  
  1868.   /* We can't construct a new debug_line section if we already have one.
  1869.      Give an error.  */
  1870.   if (all_segs && !empty_debug_line)
  1871.     as_fatal ("duplicate .debug_line sections");
  1872.  
  1873.   if ((!all_segs && emit_other_sections)
  1874.       || (!emit_other_sections && !empty_debug_line))
  1875.     /* If there is no line information and no non-empty .debug_info
  1876.        section, or if there is both a non-empty .debug_info and a non-empty
  1877.        .debug_line, then we do nothing.  */
  1878.     return;
  1879.  
  1880.   /* Calculate the size of an address for the target machine.  */
  1881.   sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
  1882.  
  1883.   /* Create and switch to the line number section.  */
  1884.   line_seg = subseg_new (".debug_line", 0);
  1885.   bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
  1886.  
  1887.   /* For each subsection, chain the debug entries together.  */
  1888.   for (s = all_segs; s; s = s->next)
  1889.     {
  1890.       struct line_subseg *lss = s->head;
  1891.       struct line_entry **ptail = lss->ptail;
  1892.  
  1893.       while ((lss = lss->next) != NULL)
  1894.         {
  1895.           *ptail = lss->head;
  1896.           ptail = lss->ptail;
  1897.         }
  1898.     }
  1899.  
  1900.   out_debug_line (line_seg);
  1901.  
  1902.   /* If this is assembler generated line info, and there is no
  1903.      debug_info already, we need .debug_info and .debug_abbrev
  1904.      sections as well.  */
  1905.   if (emit_other_sections)
  1906.     {
  1907.       segT abbrev_seg;
  1908.       segT aranges_seg;
  1909.       segT ranges_seg;
  1910.  
  1911.       gas_assert (all_segs);
  1912.  
  1913.       info_seg = subseg_new (".debug_info", 0);
  1914.       abbrev_seg = subseg_new (".debug_abbrev", 0);
  1915.       aranges_seg = subseg_new (".debug_aranges", 0);
  1916.  
  1917.       bfd_set_section_flags (stdoutput, info_seg,
  1918.                              SEC_READONLY | SEC_DEBUGGING);
  1919.       bfd_set_section_flags (stdoutput, abbrev_seg,
  1920.                              SEC_READONLY | SEC_DEBUGGING);
  1921.       bfd_set_section_flags (stdoutput, aranges_seg,
  1922.                              SEC_READONLY | SEC_DEBUGGING);
  1923.  
  1924.       record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
  1925.  
  1926.       if (all_segs->next == NULL)
  1927.         ranges_seg = NULL;
  1928.       else
  1929.         {
  1930.           ranges_seg = subseg_new (".debug_ranges", 0);
  1931.           bfd_set_section_flags (stdoutput, ranges_seg,
  1932.                                  SEC_READONLY | SEC_DEBUGGING);
  1933.           record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1);
  1934.           out_debug_ranges (ranges_seg);
  1935.         }
  1936.  
  1937.       out_debug_aranges (aranges_seg, info_seg);
  1938.       out_debug_abbrev (abbrev_seg, info_seg, line_seg);
  1939.       out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg);
  1940.     }
  1941. }
  1942.