Back to index

cell-binutils  2.17cvs20070401
dwarf2dbg.c
Go to the documentation of this file.
00001 /* dwarf2dbg.c - DWARF2 debug support
00002    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
00003    Free Software Foundation, Inc.
00004    Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
00005 
00006    This file is part of GAS, the GNU Assembler.
00007 
00008    GAS is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2, or (at your option)
00011    any later version.
00012 
00013    GAS is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with GAS; see the file COPYING.  If not, write to the Free
00020    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
00021    02110-1301, USA.  */
00022 
00023 /* Logical line numbers can be controlled by the compiler via the
00024    following directives:
00025 
00026        .file FILENO "file.c"
00027        .loc  FILENO LINENO [COLUMN] [basic_block] [prologue_end] \
00028              [epilogue_begin] [is_stmt VALUE] [isa VALUE]
00029 */
00030 
00031 #include "as.h"
00032 #include "safe-ctype.h"
00033 
00034 #ifdef HAVE_LIMITS_H
00035 #include <limits.h>
00036 #else
00037 #ifdef HAVE_SYS_PARAM_H
00038 #include <sys/param.h>
00039 #endif
00040 #ifndef INT_MAX
00041 #define INT_MAX (int) (((unsigned) (-1)) >> 1)
00042 #endif
00043 #endif
00044 
00045 #include "dwarf2dbg.h"
00046 #include <filenames.h>
00047 
00048 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
00049 /* We need to decide which character to use as a directory separator.
00050    Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not
00051    necessarily mean that the backslash character is the one to use.
00052    Some environments, eg Cygwin, can support both naming conventions.
00053    So we use the heuristic that we only need to use the backslash if
00054    the path is an absolute path starting with a DOS style drive
00055    selector.  eg C: or D:  */
00056 # define INSERT_DIR_SEPARATOR(string, offset) \
00057   do \
00058     { \
00059       if (offset > 1 \
00060           && string[0] != 0 \
00061           && string[1] == ':') \
00062        string [offset] = '\\'; \
00063       else \
00064        string [offset] = '/'; \
00065     } \
00066   while (0)
00067 #else
00068 # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
00069 #endif
00070 
00071 #ifndef DWARF2_FORMAT
00072 # define DWARF2_FORMAT() dwarf2_format_32bit
00073 #endif
00074 
00075 #ifndef DWARF2_ADDR_SIZE
00076 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
00077 #endif
00078 
00079 #include "subsegs.h"
00080 
00081 #include "elf/dwarf2.h"
00082 
00083 /* Since we can't generate the prolog until the body is complete, we
00084    use three different subsegments for .debug_line: one holding the
00085    prolog, one for the directory and filename info, and one for the
00086    body ("statement program").  */
00087 #define DL_PROLOG    0
00088 #define DL_FILES     1
00089 #define DL_BODY             2
00090 
00091 /* If linker relaxation might change offsets in the code, the DWARF special
00092    opcodes and variable-length operands cannot be used.  If this macro is
00093    nonzero, use the DW_LNS_fixed_advance_pc opcode instead.  */
00094 #ifndef DWARF2_USE_FIXED_ADVANCE_PC
00095 # define DWARF2_USE_FIXED_ADVANCE_PC      0
00096 #endif
00097 
00098 /* First special line opcde - leave room for the standard opcodes.
00099    Note: If you want to change this, you'll have to update the
00100    "standard_opcode_lengths" table that is emitted below in
00101    out_debug_line().  */
00102 #define DWARF2_LINE_OPCODE_BASE           13
00103 
00104 #ifndef DWARF2_LINE_BASE
00105   /* Minimum line offset in a special line info. opcode.  This value
00106      was chosen to give a reasonable range of values.  */
00107 # define DWARF2_LINE_BASE          -5
00108 #endif
00109 
00110 /* Range of line offsets in a special line info. opcode.  */
00111 #ifndef DWARF2_LINE_RANGE
00112 # define DWARF2_LINE_RANGE         14
00113 #endif
00114 
00115 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
00116   /* Define the architecture-dependent minimum instruction length (in
00117      bytes).  This value should be rather too small than too big.  */
00118 # define DWARF2_LINE_MIN_INSN_LENGTH      1
00119 #endif
00120 
00121 /* Flag that indicates the initial value of the is_stmt_start flag.  */
00122 #define       DWARF2_LINE_DEFAULT_IS_STMT 1
00123 
00124 /* Given a special op, return the line skip amount.  */
00125 #define SPECIAL_LINE(op) \
00126        (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
00127 
00128 /* Given a special op, return the address skip amount (in units of
00129    DWARF2_LINE_MIN_INSN_LENGTH.  */
00130 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
00131 
00132 /* The maximum address skip amount that can be encoded with a special op.  */
00133 #define MAX_SPECIAL_ADDR_DELTA            SPECIAL_ADDR(255)
00134 
00135 struct line_entry {
00136   struct line_entry *next;
00137   symbolS *label;
00138   struct dwarf2_line_info loc;
00139 };
00140 
00141 struct line_subseg {
00142   struct line_subseg *next;
00143   subsegT subseg;
00144   struct line_entry *head;
00145   struct line_entry **ptail;
00146 };
00147 
00148 struct line_seg {
00149   struct line_seg *next;
00150   segT seg;
00151   struct line_subseg *head;
00152   symbolS *text_start;
00153   symbolS *text_end;
00154 };
00155 
00156 /* Collects data for all line table entries during assembly.  */
00157 static struct line_seg *all_segs;
00158 
00159 struct file_entry {
00160   const char *filename;
00161   unsigned int dir;
00162 };
00163 
00164 /* Table of files used by .debug_line.  */
00165 static struct file_entry *files;
00166 static unsigned int files_in_use;
00167 static unsigned int files_allocated;
00168 
00169 /* Table of directories used by .debug_line.  */
00170 static char **dirs;
00171 static unsigned int dirs_in_use;
00172 static unsigned int dirs_allocated;
00173 
00174 /* TRUE when we've seen a .loc directive recently.  Used to avoid
00175    doing work when there's nothing to do.  */
00176 static bfd_boolean loc_directive_seen;
00177 
00178 /* TRUE when we're supposed to set the basic block mark whenever a
00179    label is seen.  */
00180 bfd_boolean dwarf2_loc_mark_labels;
00181 
00182 /* Current location as indicated by the most recent .loc directive.  */
00183 static struct dwarf2_line_info current = {
00184   1, 1, 0, 0,
00185   DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0
00186 };
00187 
00188 /* The size of an address on the target.  */
00189 static unsigned int sizeof_address;
00190 
00191 static struct line_subseg *get_line_subseg (segT, subsegT);
00192 static unsigned int get_filenum (const char *, unsigned int);
00193 static struct frag *first_frag_for_seg (segT);
00194 static struct frag *last_frag_for_seg (segT);
00195 static void out_byte (int);
00196 static void out_opcode (int);
00197 static void out_two (int);
00198 static void out_four (int);
00199 static void out_abbrev (int, int);
00200 static void out_uleb128 (addressT);
00201 static void out_sleb128 (addressT);
00202 static offsetT get_frag_fix (fragS *, segT);
00203 static void out_set_addr (symbolS *);
00204 static int size_inc_line_addr (int, addressT);
00205 static void emit_inc_line_addr (int, addressT, char *, int);
00206 static void out_inc_line_addr (int, addressT);
00207 static void out_fixed_inc_line_addr (int, symbolS *, symbolS *);
00208 static void relax_inc_line_addr (int, symbolS *, symbolS *);
00209 static void process_entries (segT, struct line_entry *);
00210 static void out_file_list (void);
00211 static void out_debug_line (segT);
00212 static void out_debug_aranges (segT, segT);
00213 static void out_debug_abbrev (segT);
00214 
00215 #ifndef TC_DWARF2_EMIT_OFFSET
00216 #define TC_DWARF2_EMIT_OFFSET  generic_dwarf2_emit_offset
00217 
00218 /* Create an offset to .dwarf2_*.  */
00219 
00220 static void
00221 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
00222 {
00223   expressionS expr;
00224 
00225   expr.X_op = O_symbol;
00226   expr.X_add_symbol = symbol;
00227   expr.X_add_number = 0;
00228   emit_expr (&expr, size);
00229 }
00230 #endif
00231 
00232 /* Find or create an entry for SEG+SUBSEG in ALL_SEGS.  */
00233 
00234 static struct line_subseg *
00235 get_line_subseg (segT seg, subsegT subseg)
00236 {
00237   static segT last_seg;
00238   static subsegT last_subseg;
00239   static struct line_subseg *last_line_subseg;
00240 
00241   struct line_seg **ps, *s;
00242   struct line_subseg **pss, *ss;
00243 
00244   if (seg == last_seg && subseg == last_subseg)
00245     return last_line_subseg;
00246 
00247   for (ps = &all_segs; (s = *ps) != NULL; ps = &s->next)
00248     if (s->seg == seg)
00249       goto found_seg;
00250 
00251   s = (struct line_seg *) xmalloc (sizeof (*s));
00252   s->next = NULL;
00253   s->seg = seg;
00254   s->head = NULL;
00255   *ps = s;
00256 
00257  found_seg:
00258   for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next)
00259     {
00260       if (ss->subseg == subseg)
00261        goto found_subseg;
00262       if (ss->subseg > subseg)
00263        break;
00264     }
00265 
00266   ss = (struct line_subseg *) xmalloc (sizeof (*ss));
00267   ss->next = *pss;
00268   ss->subseg = subseg;
00269   ss->head = NULL;
00270   ss->ptail = &ss->head;
00271   *pss = ss;
00272 
00273  found_subseg:
00274   last_seg = seg;
00275   last_subseg = subseg;
00276   last_line_subseg = ss;
00277 
00278   return ss;
00279 }
00280 
00281 /* Record an entry for LOC occurring at LABEL.  */
00282 
00283 static void
00284 dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
00285 {
00286   struct line_subseg *ss;
00287   struct line_entry *e;
00288 
00289   e = (struct line_entry *) xmalloc (sizeof (*e));
00290   e->next = NULL;
00291   e->label = label;
00292   e->loc = *loc;
00293 
00294   ss = get_line_subseg (now_seg, now_subseg);
00295   *ss->ptail = e;
00296   ss->ptail = &e->next;
00297 }
00298 
00299 /* Record an entry for LOC occurring at OFS within the current fragment.  */
00300 
00301 void
00302 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
00303 {
00304   static unsigned int line = -1;
00305   static unsigned int filenum = -1;
00306 
00307   symbolS *sym;
00308 
00309   /* Early out for as-yet incomplete location information.  */
00310   if (loc->filenum == 0 || loc->line == 0)
00311     return;
00312 
00313   /* Don't emit sequences of line symbols for the same line when the
00314      symbols apply to assembler code.  It is necessary to emit
00315      duplicate line symbols when a compiler asks for them, because GDB
00316      uses them to determine the end of the prologue.  */
00317   if (debug_type == DEBUG_DWARF2
00318       && line == loc->line && filenum == loc->filenum)
00319     return;
00320 
00321   line = loc->line;
00322   filenum = loc->filenum;
00323 
00324   sym = symbol_temp_new (now_seg, ofs, frag_now);
00325   dwarf2_gen_line_info_1 (sym, loc);
00326 }
00327 
00328 /* Returns the current source information.  If .file directives have
00329    been encountered, the info for the corresponding source file is
00330    returned.  Otherwise, the info for the assembly source file is
00331    returned.  */
00332 
00333 void
00334 dwarf2_where (struct dwarf2_line_info *line)
00335 {
00336   if (debug_type == DEBUG_DWARF2)
00337     {
00338       char *filename;
00339       as_where (&filename, &line->line);
00340       line->filenum = get_filenum (filename, 0);
00341       line->column = 0;
00342       line->flags = DWARF2_FLAG_IS_STMT;
00343       line->isa = current.isa;
00344     }
00345   else
00346     *line = current;
00347 }
00348 
00349 /* A hook to allow the target backend to inform the line number state 
00350    machine of isa changes when assembler debug info is enabled.  */
00351 
00352 void
00353 dwarf2_set_isa (unsigned int isa)
00354 {
00355   current.isa = isa;
00356 }
00357 
00358 /* Called for each machine instruction, or relatively atomic group of
00359    machine instructions (ie built-in macro).  The instruction or group
00360    is SIZE bytes in length.  If dwarf2 line number generation is called
00361    for, emit a line statement appropriately.  */
00362 
00363 void
00364 dwarf2_emit_insn (int size)
00365 {
00366   struct dwarf2_line_info loc;
00367 
00368   if (loc_directive_seen)
00369     {
00370       /* Use the last location established by a .loc directive, not
00371         the value returned by dwarf2_where().  That calls as_where()
00372         which will return either the logical input file name (foo.c)
00373        or the physical input file name (foo.s) and not the file name
00374        specified in the most recent .loc directive (eg foo.h).  */
00375       loc = current;
00376 
00377       /* Unless we generate DWARF2 debugging information for each
00378         assembler line, we only emit one line symbol for one LOC.  */
00379       if (debug_type != DEBUG_DWARF2)
00380        loc_directive_seen = FALSE;
00381     }
00382   else if (debug_type != DEBUG_DWARF2)
00383     return;
00384   else
00385     dwarf2_where (&loc);
00386 
00387   dwarf2_gen_line_info (frag_now_fix () - size, &loc);
00388 
00389   current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
00390                    | DWARF2_FLAG_PROLOGUE_END
00391                    | DWARF2_FLAG_EPILOGUE_BEGIN);
00392 }
00393 
00394 /* Called for each (preferably code) label.  If dwarf2_loc_mark_labels
00395    is enabled, emit a basic block marker.  */
00396 
00397 void
00398 dwarf2_emit_label (symbolS *label)
00399 {
00400   struct dwarf2_line_info loc;
00401 
00402   if (!dwarf2_loc_mark_labels)
00403     return;
00404   if (S_GET_SEGMENT (label) != now_seg)
00405     return;
00406   if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE))
00407     return;
00408   
00409   if (debug_type == DEBUG_DWARF2)
00410     dwarf2_where (&loc);
00411   else
00412     {
00413       loc = current;
00414       loc_directive_seen = FALSE;
00415     }
00416 
00417   loc.flags |= DWARF2_FLAG_BASIC_BLOCK;
00418 
00419   current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK
00420                    | DWARF2_FLAG_PROLOGUE_END
00421                    | DWARF2_FLAG_EPILOGUE_BEGIN);
00422 
00423   dwarf2_gen_line_info_1 (label, &loc);
00424 }
00425 
00426 /* Get a .debug_line file number for FILENAME.  If NUM is nonzero,
00427    allocate it on that file table slot, otherwise return the first
00428    empty one.  */
00429 
00430 static unsigned int
00431 get_filenum (const char *filename, unsigned int num)
00432 {
00433   static unsigned int last_used, last_used_dir_len;
00434   const char *file;
00435   size_t dir_len;
00436   unsigned int i, dir;
00437 
00438   if (num == 0 && last_used)
00439     {
00440       if (! files[last_used].dir
00441          && strcmp (filename, files[last_used].filename) == 0)
00442        return last_used;
00443       if (files[last_used].dir
00444          && strncmp (filename, dirs[files[last_used].dir],
00445                     last_used_dir_len) == 0
00446          && IS_DIR_SEPARATOR (filename [last_used_dir_len])
00447          && strcmp (filename + last_used_dir_len + 1,
00448                    files[last_used].filename) == 0)
00449        return last_used;
00450     }
00451 
00452   file = lbasename (filename);
00453   /* Don't make empty string from / or A: from A:/ .  */
00454 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
00455   if (file <= filename + 3)
00456     file = filename;
00457 #else
00458   if (file == filename + 1)
00459     file = filename;
00460 #endif
00461   dir_len = file - filename;
00462 
00463   dir = 0;
00464   if (dir_len)
00465     {
00466       --dir_len;
00467       for (dir = 1; dir < dirs_in_use; ++dir)
00468        if (strncmp (filename, dirs[dir], dir_len) == 0
00469            && dirs[dir][dir_len] == '\0')
00470          break;
00471 
00472       if (dir >= dirs_in_use)
00473        {
00474          if (dir >= dirs_allocated)
00475            {
00476              dirs_allocated = dir + 32;
00477              dirs = (char **)
00478                    xrealloc (dirs, (dir + 32) * sizeof (const char *));
00479            }
00480 
00481          dirs[dir] = xmalloc (dir_len + 1);
00482          memcpy (dirs[dir], filename, dir_len);
00483          dirs[dir][dir_len] = '\0';
00484          dirs_in_use = dir + 1;
00485        }
00486     }
00487 
00488   if (num == 0)
00489     {
00490       for (i = 1; i < files_in_use; ++i)
00491        if (files[i].dir == dir
00492            && files[i].filename
00493            && strcmp (file, files[i].filename) == 0)
00494          {
00495            last_used = i;
00496            last_used_dir_len = dir_len;
00497            return i;
00498          }
00499     }
00500   else
00501     i = num;
00502 
00503   if (i >= files_allocated)
00504     {
00505       unsigned int old = files_allocated;
00506 
00507       files_allocated = i + 32;
00508       files = (struct file_entry *)
00509        xrealloc (files, (i + 32) * sizeof (struct file_entry));
00510 
00511       memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry));
00512     }
00513 
00514   files[i].filename = num ? file : xstrdup (file);
00515   files[i].dir = dir;
00516   if (files_in_use < i + 1)
00517     files_in_use = i + 1;
00518   last_used = i;
00519   last_used_dir_len = dir_len;
00520 
00521   return i;
00522 }
00523 
00524 /* Handle two forms of .file directive:
00525    - Pass .file "source.c" to s_app_file
00526    - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
00527 
00528    If an entry is added to the file table, return a pointer to the filename. */
00529 
00530 char *
00531 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED)
00532 {
00533   offsetT num;
00534   char *filename;
00535   int filename_len;
00536 
00537   /* Continue to accept a bare string and pass it off.  */
00538   SKIP_WHITESPACE ();
00539   if (*input_line_pointer == '"')
00540     {
00541       s_app_file (0);
00542       return NULL;
00543     }
00544 
00545   num = get_absolute_expression ();
00546   filename = demand_copy_C_string (&filename_len);
00547   if (filename == NULL)
00548     return NULL;
00549   demand_empty_rest_of_line ();
00550 
00551   if (num < 1)
00552     {
00553       as_bad (_("file number less than one"));
00554       return NULL;
00555     }
00556 
00557   if (num < (int) files_in_use && files[num].filename != 0)
00558     {
00559       as_bad (_("file number %ld already allocated"), (long) num);
00560       return NULL;
00561     }
00562 
00563   get_filenum (filename, num);
00564 
00565   return filename;
00566 }
00567 
00568 void
00569 dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED)
00570 {
00571   offsetT filenum, line;
00572 
00573   filenum = get_absolute_expression ();
00574   SKIP_WHITESPACE ();
00575   line = get_absolute_expression ();
00576 
00577   if (filenum < 1)
00578     {
00579       as_bad (_("file number less than one"));
00580       return;
00581     }
00582   if (filenum >= (int) files_in_use || files[filenum].filename == 0)
00583     {
00584       as_bad (_("unassigned file number %ld"), (long) filenum);
00585       return;
00586     }
00587 
00588   current.filenum = filenum;
00589   current.line = line;
00590 
00591 #ifndef NO_LISTING
00592   if (listing)
00593     {
00594       if (files[filenum].dir)
00595        {
00596          size_t dir_len = strlen (dirs[files[filenum].dir]);
00597          size_t file_len = strlen (files[filenum].filename);
00598          char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
00599 
00600          memcpy (cp, dirs[files[filenum].dir], dir_len);
00601          INSERT_DIR_SEPARATOR (cp, dir_len);
00602          memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
00603          cp[dir_len + file_len + 1] = '\0';
00604          listing_source_file (cp);
00605        }
00606       else
00607        listing_source_file (files[filenum].filename);
00608       listing_source_line (line);
00609     }
00610 #endif
00611 
00612   SKIP_WHITESPACE ();
00613   if (ISDIGIT (*input_line_pointer))
00614     {
00615       current.column = get_absolute_expression ();
00616       SKIP_WHITESPACE ();
00617     }
00618 
00619   while (ISALPHA (*input_line_pointer))
00620     {
00621       char *p, c;
00622       offsetT value;
00623 
00624       p = input_line_pointer;
00625       c = get_symbol_end ();
00626 
00627       if (strcmp (p, "basic_block") == 0)
00628        {
00629          current.flags |= DWARF2_FLAG_BASIC_BLOCK;
00630          *input_line_pointer = c;
00631        }
00632       else if (strcmp (p, "prologue_end") == 0)
00633        {
00634          current.flags |= DWARF2_FLAG_PROLOGUE_END;
00635          *input_line_pointer = c;
00636        }
00637       else if (strcmp (p, "epilogue_begin") == 0)
00638        {
00639          current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
00640          *input_line_pointer = c;
00641        }
00642       else if (strcmp (p, "is_stmt") == 0)
00643        {
00644          *input_line_pointer = c;
00645          value = get_absolute_expression ();
00646          if (value == 0)
00647            current.flags &= ~DWARF2_FLAG_IS_STMT;
00648          else if (value == 1)
00649            current.flags |= DWARF2_FLAG_IS_STMT;
00650          else
00651            {
00652              as_bad (_("is_stmt value not 0 or 1"));
00653              return;
00654            }
00655        }
00656       else if (strcmp (p, "isa") == 0)
00657        {
00658           *input_line_pointer = c;
00659          value = get_absolute_expression ();
00660          if (value >= 0)
00661            current.isa = value;
00662          else
00663            {
00664              as_bad (_("isa number less than zero"));
00665              return;
00666            }
00667        }
00668       else
00669        {
00670          as_bad (_("unknown .loc sub-directive `%s'"), p);
00671           *input_line_pointer = c;
00672          return;
00673        }
00674 
00675       SKIP_WHITESPACE ();
00676     }
00677 
00678   demand_empty_rest_of_line ();
00679   loc_directive_seen = TRUE;
00680 }
00681 
00682 void
00683 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED)
00684 {
00685   offsetT value = get_absolute_expression ();
00686 
00687   if (value != 0 && value != 1)
00688     {
00689       as_bad (_("expected 0 or 1"));
00690       ignore_rest_of_line ();
00691     }
00692   else
00693     {
00694       dwarf2_loc_mark_labels = value != 0;
00695       demand_empty_rest_of_line ();
00696     }
00697 }
00698 
00699 static struct frag *
00700 first_frag_for_seg (segT seg)
00701 {
00702   return seg_info (seg)->frchainP->frch_root;
00703 }
00704 
00705 static struct frag *
00706 last_frag_for_seg (segT seg)
00707 {
00708   frchainS *f = seg_info (seg)->frchainP;
00709 
00710   while (f->frch_next != NULL)
00711     f = f->frch_next;
00712 
00713   return f->frch_last;
00714 }
00715 
00716 /* Emit a single byte into the current segment.  */
00717 
00718 static inline void
00719 out_byte (int byte)
00720 {
00721   FRAG_APPEND_1_CHAR (byte);
00722 }
00723 
00724 /* Emit a statement program opcode into the current segment.  */
00725 
00726 static inline void
00727 out_opcode (int opc)
00728 {
00729   out_byte (opc);
00730 }
00731 
00732 /* Emit a two-byte word into the current segment.  */
00733 
00734 static inline void
00735 out_two (int data)
00736 {
00737   md_number_to_chars (frag_more (2), data, 2);
00738 }
00739 
00740 /* Emit a four byte word into the current segment.  */
00741 
00742 static inline void
00743 out_four (int data)
00744 {
00745   md_number_to_chars (frag_more (4), data, 4);
00746 }
00747 
00748 /* Emit an unsigned "little-endian base 128" number.  */
00749 
00750 static void
00751 out_uleb128 (addressT value)
00752 {
00753   output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
00754 }
00755 
00756 /* Emit a signed "little-endian base 128" number.  */
00757 
00758 static void
00759 out_sleb128 (addressT value)
00760 {
00761   output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
00762 }
00763 
00764 /* Emit a tuple for .debug_abbrev.  */
00765 
00766 static inline void
00767 out_abbrev (int name, int form)
00768 {
00769   out_uleb128 (name);
00770   out_uleb128 (form);
00771 }
00772 
00773 /* Get the size of a fragment.  */
00774 
00775 static offsetT
00776 get_frag_fix (fragS *frag, segT seg)
00777 {
00778   frchainS *fr;
00779 
00780   if (frag->fr_next)
00781     return frag->fr_fix;
00782 
00783   /* If a fragment is the last in the chain, special measures must be
00784      taken to find its size before relaxation, since it may be pending
00785      on some subsegment chain.  */
00786   for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next)
00787     if (fr->frch_last == frag)
00788       return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal;
00789 
00790   abort ();
00791 }
00792 
00793 /* Set an absolute address (may result in a relocation entry).  */
00794 
00795 static void
00796 out_set_addr (symbolS *sym)
00797 {
00798   expressionS expr;
00799 
00800   out_opcode (DW_LNS_extended_op);
00801   out_uleb128 (sizeof_address + 1);
00802 
00803   out_opcode (DW_LNE_set_address);
00804   expr.X_op = O_symbol;
00805   expr.X_add_symbol = sym;
00806   expr.X_add_number = 0;
00807   emit_expr (&expr, sizeof_address);
00808 }
00809 
00810 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
00811 static void scale_addr_delta (addressT *);
00812 
00813 static void
00814 scale_addr_delta (addressT *addr_delta)
00815 {
00816   static int printed_this = 0;
00817   if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
00818     {
00819       if (!printed_this)
00820        as_bad("unaligned opcodes detected in executable segment");
00821       printed_this = 1;
00822     }
00823   *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
00824 }
00825 #else
00826 #define scale_addr_delta(A)
00827 #endif
00828 
00829 /* Encode a pair of line and address skips as efficiently as possible.
00830    Note that the line skip is signed, whereas the address skip is unsigned.
00831 
00832    The following two routines *must* be kept in sync.  This is
00833    enforced by making emit_inc_line_addr abort if we do not emit
00834    exactly the expected number of bytes.  */
00835 
00836 static int
00837 size_inc_line_addr (int line_delta, addressT addr_delta)
00838 {
00839   unsigned int tmp, opcode;
00840   int len = 0;
00841 
00842   /* Scale the address delta by the minimum instruction length.  */
00843   scale_addr_delta (&addr_delta);
00844 
00845   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
00846      We cannot use special opcodes here, since we want the end_sequence
00847      to emit the matrix entry.  */
00848   if (line_delta == INT_MAX)
00849     {
00850       if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
00851        len = 1;
00852       else
00853        len = 1 + sizeof_leb128 (addr_delta, 0);
00854       return len + 3;
00855     }
00856 
00857   /* Bias the line delta by the base.  */
00858   tmp = line_delta - DWARF2_LINE_BASE;
00859 
00860   /* If the line increment is out of range of a special opcode, we
00861      must encode it with DW_LNS_advance_line.  */
00862   if (tmp >= DWARF2_LINE_RANGE)
00863     {
00864       len = 1 + sizeof_leb128 (line_delta, 1);
00865       line_delta = 0;
00866       tmp = 0 - DWARF2_LINE_BASE;
00867     }
00868 
00869   /* Bias the opcode by the special opcode base.  */
00870   tmp += DWARF2_LINE_OPCODE_BASE;
00871 
00872   /* Avoid overflow when addr_delta is large.  */
00873   if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
00874     {
00875       /* Try using a special opcode.  */
00876       opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
00877       if (opcode <= 255)
00878        return len + 1;
00879 
00880       /* Try using DW_LNS_const_add_pc followed by special op.  */
00881       opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
00882       if (opcode <= 255)
00883        return len + 2;
00884     }
00885 
00886   /* Otherwise use DW_LNS_advance_pc.  */
00887   len += 1 + sizeof_leb128 (addr_delta, 0);
00888 
00889   /* DW_LNS_copy or special opcode.  */
00890   len += 1;
00891 
00892   return len;
00893 }
00894 
00895 static void
00896 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len)
00897 {
00898   unsigned int tmp, opcode;
00899   int need_copy = 0;
00900   char *end = p + len;
00901 
00902   /* Line number sequences cannot go backward in addresses.  This means
00903      we've incorrectly ordered the statements in the sequence.  */
00904   assert ((offsetT) addr_delta >= 0);
00905 
00906   /* Scale the address delta by the minimum instruction length.  */
00907   scale_addr_delta (&addr_delta);
00908 
00909   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
00910      We cannot use special opcodes here, since we want the end_sequence
00911      to emit the matrix entry.  */
00912   if (line_delta == INT_MAX)
00913     {
00914       if (addr_delta == MAX_SPECIAL_ADDR_DELTA)
00915        *p++ = DW_LNS_const_add_pc;
00916       else
00917        {
00918          *p++ = DW_LNS_advance_pc;
00919          p += output_leb128 (p, addr_delta, 0);
00920        }
00921 
00922       *p++ = DW_LNS_extended_op;
00923       *p++ = 1;
00924       *p++ = DW_LNE_end_sequence;
00925       goto done;
00926     }
00927 
00928   /* Bias the line delta by the base.  */
00929   tmp = line_delta - DWARF2_LINE_BASE;
00930 
00931   /* If the line increment is out of range of a special opcode, we
00932      must encode it with DW_LNS_advance_line.  */
00933   if (tmp >= DWARF2_LINE_RANGE)
00934     {
00935       *p++ = DW_LNS_advance_line;
00936       p += output_leb128 (p, line_delta, 1);
00937 
00938       line_delta = 0;
00939       tmp = 0 - DWARF2_LINE_BASE;
00940       need_copy = 1;
00941     }
00942 
00943   /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0"
00944      special opcode.  */
00945   if (line_delta == 0 && addr_delta == 0)
00946     {
00947       *p++ = DW_LNS_copy;
00948       goto done;
00949     }
00950 
00951   /* Bias the opcode by the special opcode base.  */
00952   tmp += DWARF2_LINE_OPCODE_BASE;
00953 
00954   /* Avoid overflow when addr_delta is large.  */
00955   if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA)
00956     {
00957       /* Try using a special opcode.  */
00958       opcode = tmp + addr_delta * DWARF2_LINE_RANGE;
00959       if (opcode <= 255)
00960        {
00961          *p++ = opcode;
00962          goto done;
00963        }
00964 
00965       /* Try using DW_LNS_const_add_pc followed by special op.  */
00966       opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
00967       if (opcode <= 255)
00968        {
00969          *p++ = DW_LNS_const_add_pc;
00970          *p++ = opcode;
00971          goto done;
00972        }
00973     }
00974 
00975   /* Otherwise use DW_LNS_advance_pc.  */
00976   *p++ = DW_LNS_advance_pc;
00977   p += output_leb128 (p, addr_delta, 0);
00978 
00979   if (need_copy)
00980     *p++ = DW_LNS_copy;
00981   else
00982     *p++ = tmp;
00983 
00984  done:
00985   assert (p == end);
00986 }
00987 
00988 /* Handy routine to combine calls to the above two routines.  */
00989 
00990 static void
00991 out_inc_line_addr (int line_delta, addressT addr_delta)
00992 {
00993   int len = size_inc_line_addr (line_delta, addr_delta);
00994   emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len);
00995 }
00996 
00997 /* Write out an alternative form of line and address skips using
00998    DW_LNS_fixed_advance_pc opcodes.  This uses more space than the default
00999    line and address information, but it helps support linker relaxation that
01000    changes the code offsets.  */
01001 
01002 static void
01003 out_fixed_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
01004 {
01005   expressionS expr;
01006 
01007   /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.  */
01008   if (line_delta == INT_MAX)
01009     {
01010       out_opcode (DW_LNS_fixed_advance_pc);
01011       expr.X_op = O_subtract;
01012       expr.X_add_symbol = to_sym;
01013       expr.X_op_symbol = from_sym;
01014       expr.X_add_number = 0;
01015       emit_expr (&expr, 2);
01016 
01017       out_opcode (DW_LNS_extended_op);
01018       out_byte (1);
01019       out_opcode (DW_LNE_end_sequence);
01020       return;
01021     }
01022 
01023   out_opcode (DW_LNS_advance_line);
01024   out_sleb128 (line_delta);
01025 
01026   out_opcode (DW_LNS_fixed_advance_pc);
01027   expr.X_op = O_subtract;
01028   expr.X_add_symbol = to_sym;
01029   expr.X_op_symbol = from_sym;
01030   expr.X_add_number = 0;
01031   emit_expr (&expr, 2);
01032 
01033   out_opcode (DW_LNS_copy);
01034 }
01035 
01036 /* Generate a variant frag that we can use to relax address/line
01037    increments between fragments of the target segment.  */
01038 
01039 static void
01040 relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
01041 {
01042   expressionS expr;
01043   int max_chars;
01044 
01045   expr.X_op = O_subtract;
01046   expr.X_add_symbol = to_sym;
01047   expr.X_op_symbol = from_sym;
01048   expr.X_add_number = 0;
01049 
01050   /* The maximum size of the frag is the line delta with a maximum
01051      sized address delta.  */
01052   max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH);
01053 
01054   frag_var (rs_dwarf2dbg, max_chars, max_chars, 1,
01055            make_expr_symbol (&expr), line_delta, NULL);
01056 }
01057 
01058 /* The function estimates the size of a rs_dwarf2dbg variant frag
01059    based on the current values of the symbols.  It is called before
01060    the relaxation loop.  We set fr_subtype to the expected length.  */
01061 
01062 int
01063 dwarf2dbg_estimate_size_before_relax (fragS *frag)
01064 {
01065   offsetT addr_delta;
01066   int size;
01067 
01068   addr_delta = resolve_symbol_value (frag->fr_symbol);
01069   size = size_inc_line_addr (frag->fr_offset, addr_delta);
01070 
01071   frag->fr_subtype = size;
01072 
01073   return size;
01074 }
01075 
01076 /* This function relaxes a rs_dwarf2dbg variant frag based on the
01077    current values of the symbols.  fr_subtype is the current length
01078    of the frag.  This returns the change in frag length.  */
01079 
01080 int
01081 dwarf2dbg_relax_frag (fragS *frag)
01082 {
01083   int old_size, new_size;
01084 
01085   old_size = frag->fr_subtype;
01086   new_size = dwarf2dbg_estimate_size_before_relax (frag);
01087 
01088   return new_size - old_size;
01089 }
01090 
01091 /* This function converts a rs_dwarf2dbg variant frag into a normal
01092    fill frag.  This is called after all relaxation has been done.
01093    fr_subtype will be the desired length of the frag.  */
01094 
01095 void
01096 dwarf2dbg_convert_frag (fragS *frag)
01097 {
01098   offsetT addr_diff;
01099 
01100   addr_diff = resolve_symbol_value (frag->fr_symbol);
01101 
01102   /* fr_var carries the max_chars that we created the fragment with.
01103      fr_subtype carries the current expected length.  We must, of
01104      course, have allocated enough memory earlier.  */
01105   assert (frag->fr_var >= (int) frag->fr_subtype);
01106 
01107   emit_inc_line_addr (frag->fr_offset, addr_diff,
01108                     frag->fr_literal + frag->fr_fix, frag->fr_subtype);
01109 
01110   frag->fr_fix += frag->fr_subtype;
01111   frag->fr_type = rs_fill;
01112   frag->fr_var = 0;
01113   frag->fr_offset = 0;
01114 }
01115 
01116 /* Generate .debug_line content for the chain of line number entries
01117    beginning at E, for segment SEG.  */
01118 
01119 static void
01120 process_entries (segT seg, struct line_entry *e)
01121 {
01122   unsigned filenum = 1;
01123   unsigned line = 1;
01124   unsigned column = 0;
01125   unsigned isa = 0;
01126   unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
01127   fragS *last_frag = NULL, *frag;
01128   addressT last_frag_ofs = 0, frag_ofs;
01129   symbolS *last_lab = NULL, *lab;
01130   struct line_entry *next;
01131 
01132   do
01133     {
01134       int line_delta;
01135 
01136       if (filenum != e->loc.filenum)
01137        {
01138          filenum = e->loc.filenum;
01139          out_opcode (DW_LNS_set_file);
01140          out_uleb128 (filenum);
01141        }
01142 
01143       if (column != e->loc.column)
01144        {
01145          column = e->loc.column;
01146          out_opcode (DW_LNS_set_column);
01147          out_uleb128 (column);
01148        }
01149 
01150       if (isa != e->loc.isa)
01151        {
01152          isa = e->loc.isa;
01153          out_opcode (DW_LNS_set_isa);
01154          out_uleb128 (isa);
01155        }
01156 
01157       if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
01158        {
01159          flags = e->loc.flags;
01160          out_opcode (DW_LNS_negate_stmt);
01161        }
01162 
01163       if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
01164        out_opcode (DW_LNS_set_basic_block);
01165 
01166       if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
01167        out_opcode (DW_LNS_set_prologue_end);
01168 
01169       if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
01170        out_opcode (DW_LNS_set_epilogue_begin);
01171 
01172       /* Don't try to optimize away redundant entries; gdb wants two
01173         entries for a function where the code starts on the same line as
01174         the {, and there's no way to identify that case here.  Trust gcc
01175         to optimize appropriately.  */
01176       line_delta = e->loc.line - line;
01177       lab = e->label;
01178       frag = symbol_get_frag (lab);
01179       frag_ofs = S_GET_VALUE (lab);
01180 
01181       if (last_frag == NULL)
01182        {
01183          out_set_addr (lab);
01184          out_inc_line_addr (line_delta, 0);
01185        }
01186       else if (DWARF2_USE_FIXED_ADVANCE_PC)
01187        out_fixed_inc_line_addr (line_delta, lab, last_lab);
01188       else if (frag == last_frag)
01189        out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
01190       else
01191        relax_inc_line_addr (line_delta, lab, last_lab);
01192 
01193       line = e->loc.line;
01194       last_lab = lab;
01195       last_frag = frag;
01196       last_frag_ofs = frag_ofs;
01197 
01198       next = e->next;
01199       free (e);
01200       e = next;
01201     }
01202   while (e);
01203 
01204   /* Emit a DW_LNE_end_sequence for the end of the section.  */
01205   frag = last_frag_for_seg (seg);
01206   frag_ofs = get_frag_fix (frag, seg);
01207   if (DWARF2_USE_FIXED_ADVANCE_PC)
01208     {
01209       lab = symbol_temp_new (seg, frag_ofs, frag);
01210       out_fixed_inc_line_addr (INT_MAX, lab, last_lab);
01211     }
01212   else if (frag == last_frag)
01213     out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
01214   else
01215     {
01216       lab = symbol_temp_new (seg, frag_ofs, frag);
01217       relax_inc_line_addr (INT_MAX, lab, last_lab);
01218     }
01219 }
01220 
01221 /* Emit the directory and file tables for .debug_line.  */
01222 
01223 static void
01224 out_file_list (void)
01225 {
01226   size_t size;
01227   char *cp;
01228   unsigned int i;
01229 
01230   /* Emit directory list.  */
01231   for (i = 1; i < dirs_in_use; ++i)
01232     {
01233       size = strlen (dirs[i]) + 1;
01234       cp = frag_more (size);
01235       memcpy (cp, dirs[i], size);
01236     }
01237   /* Terminate it.  */
01238   out_byte ('\0');
01239 
01240   for (i = 1; i < files_in_use; ++i)
01241     {
01242       if (files[i].filename == NULL)
01243        {
01244          as_bad (_("unassigned file number %ld"), (long) i);
01245          /* Prevent a crash later, particularly for file 1.  */
01246          files[i].filename = "";
01247          continue;
01248        }
01249 
01250       size = strlen (files[i].filename) + 1;
01251       cp = frag_more (size);
01252       memcpy (cp, files[i].filename, size);
01253 
01254       out_uleb128 (files[i].dir);  /* directory number */
01255       out_uleb128 (0);                    /* last modification timestamp */
01256       out_uleb128 (0);                    /* filesize */
01257     }
01258 
01259   /* Terminate filename list.  */
01260   out_byte (0);
01261 }
01262 
01263 /* Emit the collected .debug_line data.  */
01264 
01265 static void
01266 out_debug_line (segT line_seg)
01267 {
01268   expressionS expr;
01269   symbolS *line_start;
01270   symbolS *prologue_end;
01271   symbolS *line_end;
01272   struct line_seg *s;
01273   enum dwarf2_format d2f;
01274   int sizeof_offset;
01275 
01276   subseg_set (line_seg, 0);
01277 
01278   line_start = symbol_temp_new_now ();
01279   prologue_end = symbol_temp_make ();
01280   line_end = symbol_temp_make ();
01281 
01282   /* Total length of the information for this compilation unit.  */
01283   expr.X_op = O_subtract;
01284   expr.X_add_symbol = line_end;
01285   expr.X_op_symbol = line_start;
01286 
01287   d2f = DWARF2_FORMAT ();
01288   if (d2f == dwarf2_format_32bit)
01289     {
01290       expr.X_add_number = -4;
01291       emit_expr (&expr, 4);
01292       sizeof_offset = 4;
01293     }
01294   else if (d2f == dwarf2_format_64bit)
01295     {
01296       expr.X_add_number = -12;
01297       out_four (-1);
01298       emit_expr (&expr, 8);
01299       sizeof_offset = 8;
01300     }
01301   else if (d2f == dwarf2_format_64bit_irix)
01302     {
01303       expr.X_add_number = -8;
01304       emit_expr (&expr, 8);
01305       sizeof_offset = 8;
01306     }
01307   else
01308     {
01309       as_fatal (_("internal error: unknown dwarf2 format"));
01310     }
01311 
01312   /* Version.  */
01313   out_two (2);
01314 
01315   /* Length of the prologue following this length.  */
01316   expr.X_op = O_subtract;
01317   expr.X_add_symbol = prologue_end;
01318   expr.X_op_symbol = line_start;
01319   expr.X_add_number = - (4 + 2 + 4);
01320   emit_expr (&expr, sizeof_offset);
01321 
01322   /* Parameters of the state machine.  */
01323   out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
01324   out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
01325   out_byte (DWARF2_LINE_BASE);
01326   out_byte (DWARF2_LINE_RANGE);
01327   out_byte (DWARF2_LINE_OPCODE_BASE);
01328 
01329   /* Standard opcode lengths.  */
01330   out_byte (0);                    /* DW_LNS_copy */
01331   out_byte (1);                    /* DW_LNS_advance_pc */
01332   out_byte (1);                    /* DW_LNS_advance_line */
01333   out_byte (1);                    /* DW_LNS_set_file */
01334   out_byte (1);                    /* DW_LNS_set_column */
01335   out_byte (0);                    /* DW_LNS_negate_stmt */
01336   out_byte (0);                    /* DW_LNS_set_basic_block */
01337   out_byte (0);                    /* DW_LNS_const_add_pc */
01338   out_byte (1);                    /* DW_LNS_fixed_advance_pc */
01339   out_byte (0);                    /* DW_LNS_set_prologue_end */
01340   out_byte (0);                    /* DW_LNS_set_epilogue_begin */
01341   out_byte (1);                    /* DW_LNS_set_isa */
01342 
01343   out_file_list ();
01344 
01345   symbol_set_value_now (prologue_end);
01346 
01347   /* For each section, emit a statement program.  */
01348   for (s = all_segs; s; s = s->next)
01349     process_entries (s->seg, s->head->head);
01350 
01351   symbol_set_value_now (line_end);
01352 }
01353 
01354 static void
01355 out_debug_ranges (segT ranges_seg)
01356 {
01357   unsigned int addr_size = sizeof_address;
01358   struct line_seg *s;
01359   expressionS expr;
01360   unsigned int i;
01361 
01362   subseg_set (ranges_seg, 0);
01363 
01364   /* Base Address Entry.  */
01365   for (i = 0; i < addr_size; i++) 
01366     out_byte (0xff);
01367   for (i = 0; i < addr_size; i++) 
01368     out_byte (0);
01369 
01370   /* Range List Entry.  */
01371   for (s = all_segs; s; s = s->next)
01372     {
01373       fragS *frag;
01374       symbolS *beg, *end;
01375 
01376       frag = first_frag_for_seg (s->seg);
01377       beg = symbol_temp_new (s->seg, 0, frag);
01378       s->text_start = beg;
01379 
01380       frag = last_frag_for_seg (s->seg);
01381       end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
01382       s->text_end = end;
01383 
01384       expr.X_op = O_symbol;
01385       expr.X_add_symbol = beg;
01386       expr.X_add_number = 0;
01387       emit_expr (&expr, addr_size);
01388 
01389       expr.X_op = O_symbol;
01390       expr.X_add_symbol = end;
01391       expr.X_add_number = 0;
01392       emit_expr (&expr, addr_size);
01393     }
01394 
01395   /* End of Range Entry.   */
01396   for (i = 0; i < addr_size; i++) 
01397     out_byte (0);
01398   for (i = 0; i < addr_size; i++) 
01399     out_byte (0);
01400 }
01401 
01402 /* Emit data for .debug_aranges.  */
01403 
01404 static void
01405 out_debug_aranges (segT aranges_seg, segT info_seg)
01406 {
01407   unsigned int addr_size = sizeof_address;
01408   addressT size, skip;
01409   struct line_seg *s;
01410   expressionS expr;
01411   char *p;
01412 
01413   size = 4 + 2 + 4 + 1 + 1;
01414 
01415   skip = 2 * addr_size - (size & (2 * addr_size - 1));
01416   if (skip == 2 * addr_size)
01417     skip = 0;
01418   size += skip;
01419 
01420   for (s = all_segs; s; s = s->next)
01421     size += 2 * addr_size;
01422 
01423   size += 2 * addr_size;
01424 
01425   subseg_set (aranges_seg, 0);
01426 
01427   /* Length of the compilation unit.  */
01428   out_four (size - 4);
01429 
01430   /* Version.  */
01431   out_two (2);
01432 
01433   /* Offset to .debug_info.  */
01434   /* ??? sizeof_offset */
01435   TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), 4);
01436 
01437   /* Size of an address (offset portion).  */
01438   out_byte (addr_size);
01439 
01440   /* Size of a segment descriptor.  */
01441   out_byte (0);
01442 
01443   /* Align the header.  */
01444   if (skip)
01445     frag_align (ffs (2 * addr_size) - 1, 0, 0);
01446 
01447   for (s = all_segs; s; s = s->next)
01448     {
01449       fragS *frag;
01450       symbolS *beg, *end;
01451 
01452       frag = first_frag_for_seg (s->seg);
01453       beg = symbol_temp_new (s->seg, 0, frag);
01454       s->text_start = beg;
01455 
01456       frag = last_frag_for_seg (s->seg);
01457       end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
01458       s->text_end = end;
01459 
01460       expr.X_op = O_symbol;
01461       expr.X_add_symbol = beg;
01462       expr.X_add_number = 0;
01463       emit_expr (&expr, addr_size);
01464 
01465       expr.X_op = O_subtract;
01466       expr.X_add_symbol = end;
01467       expr.X_op_symbol = beg;
01468       expr.X_add_number = 0;
01469       emit_expr (&expr, addr_size);
01470     }
01471 
01472   p = frag_more (2 * addr_size);
01473   md_number_to_chars (p, 0, addr_size);
01474   md_number_to_chars (p + addr_size, 0, addr_size);
01475 }
01476 
01477 /* Emit data for .debug_abbrev.  Note that this must be kept in
01478    sync with out_debug_info below.  */
01479 
01480 static void
01481 out_debug_abbrev (segT abbrev_seg)
01482 {
01483   subseg_set (abbrev_seg, 0);
01484 
01485   out_uleb128 (1);
01486   out_uleb128 (DW_TAG_compile_unit);
01487   out_byte (DW_CHILDREN_no);
01488   out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
01489   if (all_segs->next == NULL)
01490     {
01491       out_abbrev (DW_AT_low_pc, DW_FORM_addr);
01492       out_abbrev (DW_AT_high_pc, DW_FORM_addr);
01493     }
01494   else
01495     {
01496       if (DWARF2_FORMAT () == dwarf2_format_32bit)
01497        out_abbrev (DW_AT_ranges, DW_FORM_data4);
01498       else
01499        out_abbrev (DW_AT_ranges, DW_FORM_data8);
01500     }
01501   out_abbrev (DW_AT_name, DW_FORM_string);
01502   out_abbrev (DW_AT_comp_dir, DW_FORM_string);
01503   out_abbrev (DW_AT_producer, DW_FORM_string);
01504   out_abbrev (DW_AT_language, DW_FORM_data2);
01505   out_abbrev (0, 0);
01506 
01507   /* Terminate the abbreviations for this compilation unit.  */
01508   out_byte (0);
01509 }
01510 
01511 /* Emit a description of this compilation unit for .debug_info.  */
01512 
01513 static void
01514 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg)
01515 {
01516   char producer[128];
01517   char *comp_dir;
01518   expressionS expr;
01519   symbolS *info_start;
01520   symbolS *info_end;
01521   char *p;
01522   int len;
01523   enum dwarf2_format d2f;
01524   int sizeof_offset;
01525 
01526   subseg_set (info_seg, 0);
01527 
01528   info_start = symbol_temp_new_now ();
01529   info_end = symbol_temp_make ();
01530 
01531   /* Compilation Unit length.  */
01532   expr.X_op = O_subtract;
01533   expr.X_add_symbol = info_end;
01534   expr.X_op_symbol = info_start;
01535 
01536   d2f = DWARF2_FORMAT ();
01537   if (d2f == dwarf2_format_32bit)
01538     {
01539       expr.X_add_number = -4;
01540       emit_expr (&expr, 4);
01541       sizeof_offset = 4;
01542     }
01543   else if (d2f == dwarf2_format_64bit)
01544     {
01545       expr.X_add_number = -12;
01546       out_four (-1);
01547       emit_expr (&expr, 8);
01548       sizeof_offset = 8;
01549     }
01550   else if (d2f == dwarf2_format_64bit_irix)
01551     {
01552       expr.X_add_number = -8;
01553       emit_expr (&expr, 8);
01554       sizeof_offset = 8;
01555     }
01556   else
01557     {
01558       as_fatal (_("internal error: unknown dwarf2 format"));
01559     }
01560 
01561   /* DWARF version.  */
01562   out_two (2);
01563 
01564   /* .debug_abbrev offset */
01565   TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
01566 
01567   /* Target address size.  */
01568   out_byte (sizeof_address);
01569 
01570   /* DW_TAG_compile_unit DIE abbrev */
01571   out_uleb128 (1);
01572 
01573   /* DW_AT_stmt_list */
01574   /* ??? sizeof_offset */
01575   TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 4);
01576 
01577   /* These two attributes are emitted if all of the code is contiguous.  */
01578   if (all_segs->next == NULL)
01579     {
01580       /* DW_AT_low_pc */
01581       expr.X_op = O_symbol;
01582       expr.X_add_symbol = all_segs->text_start;
01583       expr.X_add_number = 0;
01584       emit_expr (&expr, sizeof_address);
01585 
01586       /* DW_AT_high_pc */
01587       expr.X_op = O_symbol;
01588       expr.X_add_symbol = all_segs->text_end;
01589       expr.X_add_number = 0;
01590       emit_expr (&expr, sizeof_address);
01591     }
01592   else
01593     {
01594       /* This attribute is emitted if the code is disjoint.  */
01595       /* DW_AT_ranges.  */
01596       TC_DWARF2_EMIT_OFFSET (section_symbol (ranges_seg), sizeof_offset);
01597     }
01598 
01599   /* DW_AT_name.  We don't have the actual file name that was present
01600      on the command line, so assume files[1] is the main input file.
01601      We're not supposed to get called unless at least one line number
01602      entry was emitted, so this should always be defined.  */
01603   if (!files || files_in_use < 1)
01604     abort ();
01605   if (files[1].dir)
01606     {
01607       len = strlen (dirs[files[1].dir]);
01608       p = frag_more (len + 1);
01609       memcpy (p, dirs[files[1].dir], len);
01610       INSERT_DIR_SEPARATOR (p, len);
01611     }
01612   len = strlen (files[1].filename) + 1;
01613   p = frag_more (len);
01614   memcpy (p, files[1].filename, len);
01615 
01616   /* DW_AT_comp_dir */
01617   comp_dir = getpwd ();
01618   len = strlen (comp_dir) + 1;
01619   p = frag_more (len);
01620   memcpy (p, comp_dir, len);
01621 
01622   /* DW_AT_producer */
01623   sprintf (producer, "GNU AS %s", VERSION);
01624   len = strlen (producer) + 1;
01625   p = frag_more (len);
01626   memcpy (p, producer, len);
01627 
01628   /* DW_AT_language.  Yes, this is probably not really MIPS, but the
01629      dwarf2 draft has no standard code for assembler.  */
01630   out_two (DW_LANG_Mips_Assembler);
01631 
01632   symbol_set_value_now (info_end);
01633 }
01634 
01635 /* Finish the dwarf2 debug sections.  We emit .debug.line if there
01636    were any .file/.loc directives, or --gdwarf2 was given, or if the
01637    file has a non-empty .debug_info section.  If we emit .debug_line,
01638    and the .debug_info section is empty, we also emit .debug_info,
01639    .debug_aranges and .debug_abbrev.  ALL_SEGS will be non-null if
01640    there were any .file/.loc directives, or --gdwarf2 was given and
01641    there were any located instructions emitted.  */
01642 
01643 void
01644 dwarf2_finish (void)
01645 {
01646   segT line_seg;
01647   struct line_seg *s;
01648   segT info_seg;
01649   int emit_other_sections = 0;
01650 
01651   info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
01652   emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
01653 
01654   if (!all_segs && emit_other_sections)
01655     /* There is no line information and no non-empty .debug_info
01656        section.  */
01657     return;
01658 
01659   /* Calculate the size of an address for the target machine.  */
01660   sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
01661 
01662   /* Create and switch to the line number section.  */
01663   line_seg = subseg_new (".debug_line", 0);
01664   bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
01665 
01666   /* For each subsection, chain the debug entries together.  */
01667   for (s = all_segs; s; s = s->next)
01668     {
01669       struct line_subseg *ss = s->head;
01670       struct line_entry **ptail = ss->ptail;
01671 
01672       while ((ss = ss->next) != NULL)
01673        {
01674          *ptail = ss->head;
01675          ptail = ss->ptail;
01676        }
01677     }
01678 
01679   out_debug_line (line_seg);
01680 
01681   /* If this is assembler generated line info, and there is no
01682      debug_info already, we need .debug_info and .debug_abbrev
01683      sections as well.  */
01684   if (emit_other_sections)
01685     {
01686       segT abbrev_seg;
01687       segT aranges_seg;
01688       segT ranges_seg;
01689 
01690       assert (all_segs);
01691       
01692       info_seg = subseg_new (".debug_info", 0);
01693       abbrev_seg = subseg_new (".debug_abbrev", 0);
01694       aranges_seg = subseg_new (".debug_aranges", 0);
01695 
01696       bfd_set_section_flags (stdoutput, info_seg,
01697                           SEC_READONLY | SEC_DEBUGGING);
01698       bfd_set_section_flags (stdoutput, abbrev_seg,
01699                           SEC_READONLY | SEC_DEBUGGING);
01700       bfd_set_section_flags (stdoutput, aranges_seg,
01701                           SEC_READONLY | SEC_DEBUGGING);
01702 
01703       record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
01704 
01705       if (all_segs->next == NULL)
01706        ranges_seg = NULL;
01707       else
01708        {
01709          ranges_seg = subseg_new (".debug_ranges", 0);
01710          bfd_set_section_flags (stdoutput, ranges_seg, 
01711                              SEC_READONLY | SEC_DEBUGGING);
01712          record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1);
01713          out_debug_ranges (ranges_seg);
01714        }
01715 
01716       out_debug_aranges (aranges_seg, info_seg);
01717       out_debug_abbrev (abbrev_seg);
01718       out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg);
01719     }
01720 }