Ignore:
Timestamp:
Aug 16, 2003, 6:59:22 PM (22 years ago)
Author:
bird
Message:

binutils v2.14 - offical sources.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GNU/src/binutils/gas/dwarf2dbg.c

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.1.1.2
    r608 r609  
    11/* dwarf2dbg.c - DWARF2 debug support
    2    Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
     2   Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
    33   Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
    44
     
    4242#endif
    4343
     44#include "dwarf2dbg.h"
     45#include <filenames.h>
     46
     47#ifndef DWARF2_FORMAT
     48# define DWARF2_FORMAT() dwarf2_format_32bit
     49#endif
     50
     51#ifndef DWARF2_ADDR_SIZE
     52# define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8);
     53#endif
     54
     55#ifndef TC_DWARF2_EMIT_OFFSET
     56# define TC_DWARF2_EMIT_OFFSET  generic_dwarf2_emit_offset
     57#endif
     58
    4459#ifdef BFD_ASSEMBLER
    4560
    46 #include "dwarf2dbg.h"
    4761#include "subsegs.h"
    4862
     
    123137
    124138struct file_entry {
    125   char *filename;
     139  const char *filename;
    126140  unsigned int dir;
    127141};
     
    132146static unsigned int files_allocated;
    133147
    134 /* True when we've seen a .loc directive recently.  Used to avoid
     148/* Table of directories used by .debug_line.  */
     149static char **dirs;
     150static unsigned int dirs_in_use;
     151static unsigned int dirs_allocated;
     152
     153/* TRUE when we've seen a .loc directive recently.  Used to avoid
    135154   doing work when there's nothing to do.  */
    136 static boolean loc_directive_seen;
     155static bfd_boolean loc_directive_seen;
    137156
    138157/* Current location as indicated by the most recent .loc directive.  */
     
    146165
    147166
     167static void generic_dwarf2_emit_offset PARAMS((symbolS *, unsigned int));
    148168static struct line_subseg *get_line_subseg PARAMS ((segT, subsegT));
    149 static unsigned int get_filenum PARAMS ((const char *));
     169static unsigned int get_filenum PARAMS ((const char *, unsigned int));
    150170static struct frag *first_frag_for_seg PARAMS ((segT));
    151171static struct frag *last_frag_for_seg PARAMS ((segT));
     
    173193
    174194
     195/* Create an offset to .dwarf2_*.  */
     196
     197static void
     198generic_dwarf2_emit_offset (symbol, size)
     199     symbolS *symbol;
     200     unsigned int size;
     201{
     202  expressionS expr;
     203
     204  expr.X_op = O_symbol;
     205  expr.X_add_symbol = symbol;
     206  expr.X_add_number = 0;
     207  emit_expr (&expr, size);
     208}
     209
    175210/* Find or create an entry for SEG+SUBSEG in ALL_SEGS.  */
    176211
     
    233268  struct line_subseg *ss;
    234269  struct line_entry *e;
     270  static unsigned int line = -1;
     271  static unsigned int filenum = -1;
    235272
    236273  /* Early out for as-yet incomplete location information.  */
    237274  if (loc->filenum == 0 || loc->line == 0)
    238275    return;
     276
     277  /* Don't emit sequences of line symbols for the same line when the
     278     symbols apply to assembler code.  It is necessary to emit
     279     duplicate line symbols when a compiler asks for them, because GDB
     280     uses them to determine the end of the prologue.  */
     281  if (debug_type == DEBUG_DWARF2
     282      && line == loc->line && filenum == loc->filenum)
     283    return;
     284
     285  line = loc->line;
     286  filenum = loc->filenum;
    239287
    240288  e = (struct line_entry *) xmalloc (sizeof (*e));
     
    257305      char *filename;
    258306      as_where (&filename, &line->line);
    259       line->filenum = get_filenum (filename);
     307      line->filenum = get_filenum (filename, 0);
    260308      line->column = 0;
    261309      line->flags = DWARF2_FLAG_BEGIN_STMT;
     
    276324  struct dwarf2_line_info loc;
    277325
    278   if (debug_type != DEBUG_DWARF2 && ! loc_directive_seen)
     326  if (loc_directive_seen)
     327    {
     328      /* Use the last location established by a .loc directive, not
     329         the value returned by dwarf2_where().  That calls as_where()
     330         which will return either the logical input file name (foo.c)
     331        or the physical input file name (foo.s) and not the file name
     332        specified in the most recent .loc directive (eg foo.h).  */
     333      loc = current;
     334
     335      /* Unless we generate DWARF2 debugging information for each
     336         assembler line, we only emit one line symbol for one LOC.  */
     337      if (debug_type != DEBUG_DWARF2)
     338        loc_directive_seen = FALSE;
     339    }
     340  else if (debug_type != DEBUG_DWARF2)
    279341    return;
    280   loc_directive_seen = false;
    281 
    282   dwarf2_where (&loc);
     342  else
     343    dwarf2_where (& loc);
     344
    283345  dwarf2_gen_line_info (frag_now_fix () - size, &loc);
    284346}
    285347
    286 /* Get a .debug_line file number for FILENAME.  */
     348/* Get a .debug_line file number for FILENAME.  If NUM is nonzero,
     349   allocate it on that file table slot, otherwise return the first
     350   empty one.  */
    287351
    288352static unsigned int
    289 get_filenum (filename)
     353get_filenum (filename, num)
    290354     const char *filename;
    291 {
    292   static unsigned int last_used;
    293   unsigned int i;
    294 
    295   if (last_used)
    296     if (strcmp (filename, files[last_used].filename) == 0)
    297       return last_used;
    298 
    299   for (i = 1; i < files_in_use; ++i)
    300     if (strcmp (filename, files[i].filename) == 0)
    301       return i;
     355     unsigned int num;
     356{
     357  static unsigned int last_used, last_used_dir_len;
     358  const char *file;
     359  size_t dir_len;
     360  unsigned int i, dir;
     361
     362  if (num == 0 && last_used)
     363    {
     364      if (! files[last_used].dir
     365          && strcmp (filename, files[last_used].filename) == 0)
     366        return last_used;
     367      if (files[last_used].dir
     368          && strncmp (filename, dirs[files[last_used].dir],
     369                      last_used_dir_len) == 0
     370          && IS_DIR_SEPARATOR (filename [last_used_dir_len])
     371          && strcmp (filename + last_used_dir_len + 1,
     372                     files[last_used].filename) == 0)
     373        return last_used;
     374    }
     375
     376  file = lbasename (filename);
     377  /* Don't make empty string from / or A: from A:/ .  */
     378#ifdef HAVE_DOS_BASED_FILE_SYSTEM
     379  if (file <= filename + 3)
     380    file = filename;
     381#else
     382  if (file == filename + 1)
     383    file = filename;
     384#endif
     385  dir_len = file - filename;
     386
     387  dir = 0;
     388  if (dir_len)
     389    {
     390      --dir_len;
     391      for (dir = 1; dir < dirs_in_use; ++dir)
     392        if (memcmp (filename, dirs[dir], dir_len) == 0
     393            && dirs[dir][dir_len] == '\0')
     394          break;
     395
     396      if (dir >= dirs_in_use)
     397        {
     398          if (dir >= dirs_allocated)
     399            {
     400              dirs_allocated = dir + 32;
     401              dirs = (char **)
     402                     xrealloc (dirs, (dir + 32) * sizeof (const char *));
     403            }
     404
     405          dirs[dir] = xmalloc (dir_len + 1);
     406          memcpy (dirs[dir], filename, dir_len);
     407          dirs[dir][dir_len] = '\0';
     408          dirs_in_use = dir + 1;
     409        }
     410    }
     411
     412  if (num == 0)
     413    {
     414      for (i = 1; i < files_in_use; ++i)
     415        if (files[i].dir == dir
     416            && files[i].filename
     417            && strcmp (file, files[i].filename) == 0)
     418          {
     419            last_used = i;
     420            last_used_dir_len = dir_len;
     421            return i;
     422          }
     423    }
     424  else
     425    i = num;
    302426
    303427  if (i >= files_allocated)
     
    312436    }
    313437
    314   files[i].filename = xstrdup (filename);
    315   files[i].dir = 0;
     438  files[i].filename = num ? file : xstrdup (file);
     439  files[i].dir = dir;
    316440  files_in_use = i + 1;
    317441  last_used = i;
     442  last_used_dir_len = dir_len;
    318443
    319444  return i;
    320445}
    321446
    322 /* Handle the .file directive.  */
    323 
    324 void
     447/* Handle two forms of .file directive:
     448   - Pass .file "source.c" to s_app_file
     449   - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table
     450
     451   If an entry is added to the file table, return a pointer to the filename. */
     452
     453char *
    325454dwarf2_directive_file (dummy)
    326455     int dummy ATTRIBUTE_UNUSED;
     
    335464    {
    336465      s_app_file (0);
    337       return;
     466      return NULL;
    338467    }
    339468
     
    344473  if (num < 1)
    345474    {
    346       as_bad (_("File number less than one"));
    347       return;
    348     }
    349 
    350   if (num < files_in_use && files[num].filename != 0)
    351     {
    352       as_bad (_("File number %ld already allocated"), (long) num);
    353       return;
    354     }
    355 
    356   if (num >= (int) files_allocated)
    357     {
    358       unsigned int old = files_allocated;
    359 
    360       files_allocated = num + 16;
    361       files = (struct file_entry *)
    362         xrealloc (files, (num + 16) * sizeof (struct file_entry));
    363 
    364       /* Zero the new memory.  */
    365       memset (files + old, 0, (num + 16 - old) * sizeof (struct file_entry));
    366     }
    367 
    368   files[num].filename = filename;
    369   files[num].dir = 0;
    370   files_in_use = num + 1;
     475      as_bad (_("file number less than one"));
     476      return NULL;
     477    }
     478
     479  if (num < (int) files_in_use && files[num].filename != 0)
     480    {
     481      as_bad (_("file number %ld already allocated"), (long) num);
     482      return NULL;
     483    }
     484
     485  get_filenum (filename, num);
     486
     487  return filename;
    371488}
    372489
     
    386503  if (filenum < 1)
    387504    {
    388       as_bad (_("File number less than one"));
     505      as_bad (_("file number less than one"));
    389506      return;
    390507    }
    391508  if (filenum >= (int) files_in_use || files[filenum].filename == 0)
    392509    {
    393       as_bad (_("Unassigned file number %ld"), (long) filenum);
     510      as_bad (_("unassigned file number %ld"), (long) filenum);
    394511      return;
    395512    }
     
    400517  current.flags = DWARF2_FLAG_BEGIN_STMT;
    401518
    402   loc_directive_seen = true;
     519  loc_directive_seen = TRUE;
    403520
    404521#ifndef NO_LISTING
    405522  if (listing)
    406     listing_source_line (line);
     523    {
     524      if (files[filenum].dir)
     525        {
     526          size_t dir_len = strlen (dirs[files[filenum].dir]);
     527          size_t file_len = strlen (files[filenum].filename);
     528          char *cp = (char *) alloca (dir_len + 1 + file_len + 1);
     529
     530          memcpy (cp, dirs[files[filenum].dir], dir_len);
     531          cp[dir_len] = '/';
     532          memcpy (cp + dir_len + 1, files[filenum].filename, file_len);
     533          cp[dir_len + file_len + 1] = '\0';
     534          listing_source_file (cp);
     535        }
     536      else
     537        listing_source_file (files[filenum].filename);
     538      listing_source_line (line);
     539    }
    407540#endif
    408541}
     
    529662    if (fr->frch_last == frag)
    530663      {
    531         return ((char *) obstack_next_free (&fr->frch_obstack)
    532                 - frag->fr_literal);
     664        long align_mask = -1 << get_recorded_alignment (fr->frch_seg);
     665        return (((char *) obstack_next_free (&fr->frch_obstack)
     666                 - frag->fr_literal) + ~align_mask) & align_mask;
    533667      }
    534668
     
    559693}
    560694
     695#if DWARF2_LINE_MIN_INSN_LENGTH > 1
     696static void scale_addr_delta PARAMS ((addressT *));
     697
     698static void
     699scale_addr_delta (addr_delta)
     700     addressT *addr_delta;
     701{
     702  static int printed_this = 0;
     703  if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0)
     704    {
     705      if (!printed_this)
     706        as_bad("unaligned opcodes detected in executable segment");
     707      printed_this = 1;
     708    }
     709  *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
     710}
     711#else
     712#define scale_addr_delta(A)
     713#endif
     714
    561715/* Encode a pair of line and address skips as efficiently as possible.
    562716   Note that the line skip is signed, whereas the address skip is unsigned.
     
    575729
    576730  /* Scale the address delta by the minimum instruction length.  */
    577 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
    578   assert (addr_delta % DWARF2_LINE_MIN_INSN_LENGTH == 0);
    579   addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
    580 #endif
     731  scale_addr_delta (&addr_delta);
    581732
    582733  /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
     
    641792  char *end = p + len;
    642793
    643 #if DWARF2_LINE_MIN_INSN_LENGTH > 1
    644794  /* Scale the address delta by the minimum instruction length.  */
    645   assert (addr_delta % DWARF2_LINE_MIN_INSN_LENGTH == 0);
    646   addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH;
    647 #endif
     795  scale_addr_delta (&addr_delta);
     796
    648797  /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence.
    649798     We cannot use special opcodes here, since we want the end_sequence
     
    777926  int size;
    778927
    779   addr_delta = resolve_symbol_value (frag->fr_symbol, 0);
     928  addr_delta = resolve_symbol_value (frag->fr_symbol);
    780929  size = size_inc_line_addr (frag->fr_offset, addr_delta);
    781930
     
    811960  offsetT addr_diff;
    812961
    813   addr_diff = resolve_symbol_value (frag->fr_symbol, 1);
     962  addr_diff = resolve_symbol_value (frag->fr_symbol);
    814963
    815964  /* fr_var carries the max_chars that we created the fragment with.
     
    9311080  unsigned int i;
    9321081
    933   /* Terminate directory list.  */
     1082  /* Emit directory list.  */
     1083  for (i = 1; i < dirs_in_use; ++i)
     1084    {
     1085      size = strlen (dirs[i]) + 1;
     1086      cp = frag_more (size);
     1087      memcpy (cp, dirs[i], size);
     1088    }
     1089  /* Terminate it.  */
    9341090  out_byte ('\0');
    9351091
     
    9381094      if (files[i].filename == NULL)
    9391095        {
    940           as_bad (_("Unassigned file number %u"), i);
     1096          as_bad (_("unassigned file number %ld"), (long) i);
     1097          /* Prevent a crash later, particularly for file 1.  */
     1098          files[i].filename = "";
    9411099          continue;
    9421100        }
     
    9661124  symbolS *line_end;
    9671125  struct line_seg *s;
     1126  enum dwarf2_format d2f;
     1127  int sizeof_offset;
    9681128
    9691129  subseg_set (line_seg, 0);
     
    9771137  expr.X_add_symbol = line_end;
    9781138  expr.X_op_symbol = line_start;
    979   expr.X_add_number = -4;
    980   emit_expr (&expr, 4);
     1139
     1140  d2f = DWARF2_FORMAT ();
     1141  if (d2f == dwarf2_format_32bit)
     1142    {
     1143      expr.X_add_number = -4;
     1144      emit_expr (&expr, 4);
     1145      sizeof_offset = 4;
     1146    }
     1147  else if (d2f == dwarf2_format_64bit)
     1148    {
     1149      expr.X_add_number = -12;
     1150      out_four (-1);
     1151      emit_expr (&expr, 8);
     1152      sizeof_offset = 8;
     1153    }
     1154  else if (d2f == dwarf2_format_64bit_irix)
     1155    {
     1156      expr.X_add_number = -8;
     1157      emit_expr (&expr, 8);
     1158      sizeof_offset = 8;
     1159    }
     1160  else
     1161    {
     1162      as_fatal (_("internal error: unknown dwarf2 format"));
     1163    }
    9811164
    9821165  /* Version.  */
     
    9881171  expr.X_op_symbol = line_start;
    9891172  expr.X_add_number = - (4 + 2 + 4);
    990   emit_expr (&expr, 4);
     1173  emit_expr (&expr, sizeof_offset);
    9911174
    9921175  /* Parameters of the state machine.  */
     
    10531236
    10541237  /* Offset to .debug_info.  */
    1055   expr.X_op = O_symbol;
    1056   expr.X_add_symbol = section_symbol (info_seg);
    1057   expr.X_add_number = 0;
    1058   emit_expr (&expr, 4);
     1238  /* ??? sizeof_offset */
     1239  TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), 4);
    10591240
    10601241  /* Size of an address (offset portion).  */
     
    11161297      out_abbrev (DW_AT_high_pc, DW_FORM_addr);
    11171298    }
     1299  out_abbrev (DW_AT_name, DW_FORM_string);
    11181300  out_abbrev (DW_AT_comp_dir, DW_FORM_string);
    11191301  out_abbrev (DW_AT_producer, DW_FORM_string);
     
    11401322  char *p;
    11411323  int len;
     1324  enum dwarf2_format d2f;
     1325  int sizeof_offset;
    11421326
    11431327  subseg_set (info_seg, 0);
     
    11501334  expr.X_add_symbol = info_end;
    11511335  expr.X_op_symbol = info_start;
    1152   expr.X_add_number = -4;
    1153   emit_expr (&expr, 4);
     1336
     1337  d2f = DWARF2_FORMAT ();
     1338  if (d2f == dwarf2_format_32bit)
     1339    {
     1340      expr.X_add_number = -4;
     1341      emit_expr (&expr, 4);
     1342      sizeof_offset = 4;
     1343    }
     1344  else if (d2f == dwarf2_format_64bit)
     1345    {
     1346      expr.X_add_number = -12;
     1347      out_four (-1);
     1348      emit_expr (&expr, 8);
     1349      sizeof_offset = 8;
     1350    }
     1351  else if (d2f == dwarf2_format_64bit_irix)
     1352    {
     1353      expr.X_add_number = -8;
     1354      emit_expr (&expr, 8);
     1355      sizeof_offset = 8;
     1356    }
     1357  else
     1358    {
     1359      as_fatal (_("internal error: unknown dwarf2 format"));
     1360    }
    11541361
    11551362  /* DWARF version.  */
     
    11571364
    11581365  /* .debug_abbrev offset */
    1159   expr.X_op = O_symbol;
    1160   expr.X_add_symbol = section_symbol (abbrev_seg);
    1161   expr.X_add_number = 0;
    1162   emit_expr (&expr, 4);
     1366  TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
    11631367
    11641368  /* Target address size.  */
     
    11691373
    11701374  /* DW_AT_stmt_list */
    1171   expr.X_op = O_symbol;
    1172   expr.X_add_symbol = section_symbol (line_seg);
    1173   expr.X_add_number = 0;
    1174   emit_expr (&expr, 4);
     1375  /* ??? sizeof_offset */
     1376  TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 4);
    11751377
    11761378  /* These two attributes may only be emitted if all of the code is
     
    11911393    }
    11921394
     1395  /* DW_AT_name.  We don't have the actual file name that was present
     1396     on the command line, so assume files[1] is the main input file.
     1397     We're not supposed to get called unless at least one line number
     1398     entry was emitted, so this should always be defined.  */
     1399  if (!files || files_in_use < 1)
     1400    abort ();
     1401  if (files[1].dir)
     1402    {
     1403      len = strlen (dirs[files[1].dir]);
     1404      p = frag_more (len + 1);
     1405      memcpy (p, dirs[files[1].dir], len);
     1406      p[len] = '/';
     1407    }
     1408  len = strlen (files[1].filename) + 1;
     1409  p = frag_more (len);
     1410  memcpy (p, files[1].filename, len);
     1411
    11931412  /* DW_AT_comp_dir */
    11941413  comp_dir = getpwd ();
     
    12161435  struct line_seg *s;
    12171436
    1218   /* If no debug information was recorded, nothing to do.  */
    1219   if (all_segs == NULL)
     1437  /* We don't need to do anything unless:
     1438     - Some debug information was recorded via .file/.loc
     1439     - or, we are generating DWARF2 information ourself (--gdwarf2)
     1440     - or, there is a user-provided .debug_info section which could
     1441       reference the file table in the .debug_line section we generate
     1442       below.  */
     1443  if (all_segs == NULL
     1444      && debug_type != DEBUG_DWARF2
     1445      && bfd_get_section_by_name (stdoutput, ".debug_info") == NULL)
    12201446    return;
    12211447
    12221448  /* Calculate the size of an address for the target machine.  */
    1223   sizeof_address = bfd_arch_bits_per_address (stdoutput) / 8;
     1449  sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
    12241450
    12251451  /* Create and switch to the line number section.  */
     
    12441470  /* If this is assembler generated line info, we need .debug_info
    12451471     and .debug_abbrev sections as well.  */
    1246   if (debug_type == DEBUG_DWARF2)
     1472  if (all_segs != NULL && debug_type == DEBUG_DWARF2)
    12471473    {
    12481474      segT abbrev_seg;
     
    13011527}
    13021528
    1303 void
     1529char *
    13041530dwarf2_directive_file (dummy)
    13051531     int dummy ATTRIBUTE_UNUSED;
    13061532{
    13071533  s_app_file (0);
     1534  return NULL;
    13081535}
    13091536
Note: See TracChangeset for help on using the changeset viewer.