~ubuntu-branches/debian/jessie/gdb/jessie

« back to all changes in this revision

Viewing changes to bfd/dwarf2.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Jacobowitz
  • Date: 2010-03-20 01:21:29 UTC
  • mfrom: (1.3.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100320012129-t7h25y8zgr8c2369
Tags: 7.1-1
* New upstream release, including:
  - PIE support (Closes: #346409).
  - C++ improvements, including static_cast<> et al, namespace imports,
    and bug fixes in printing virtual base classes.
  - Multi-program debugging.  One GDB can now debug multiple programs
    at the same time.
  - Python scripting improvements, including gdb.parse_and_eval.
  - Updated MIPS Linux signal frame layout (Closes: #570875).
  - No internal error stepping over _dl_debug_state (Closes: #569551).
* Update to Standards-Version: 3.8.4 (no changes required).
* Include more relevant (and smaller) docs in the gdbserver package
  (Closes: #571132).
* Do not duplicate documentation in gdb64, gdb-source, and libgdb-dev.
* Fix crash when switching into TUI mode (Closes: #568489).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* DWARF 2 support.
2
2
   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3
 
   2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
 
3
   2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
4
 
5
5
   Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6
6
   (gavin@cygnus.com).
216
216
     by its reference.  */
217
217
  bfd_byte *info_ptr_unit;
218
218
 
 
219
  /* Pointer to the start of the debug section, for DW_FORM_ref_addr.  */
 
220
  bfd_byte *sec_info_ptr;
 
221
 
219
222
  /* The offset into .debug_line of the line number table.  */
220
223
  unsigned long line_offset;
221
224
 
855
858
      attr->u.val = read_8_bytes (abfd, info_ptr);
856
859
      info_ptr += 8;
857
860
      break;
 
861
    case DW_FORM_ref_sig8:
 
862
      attr->u.val = read_8_bytes (abfd, info_ptr);
 
863
      info_ptr += 8;
 
864
      break;
858
865
    case DW_FORM_ref_udata:
859
866
      attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
860
867
      info_ptr += bytes_read;
908
915
  unsigned int size;
909
916
};
910
917
 
 
918
struct line_sequence
 
919
{
 
920
  bfd_vma               low_pc;
 
921
  struct line_sequence* prev_sequence;
 
922
  struct line_info*     last_line;  /* Largest VMA.  */
 
923
};
 
924
 
911
925
struct line_info_table
912
926
{
913
 
  bfd* abfd;
914
 
  unsigned int num_files;
915
 
  unsigned int num_dirs;
916
 
  char *comp_dir;
917
 
  char **dirs;
918
 
  struct fileinfo* files;
919
 
  struct line_info* last_line;  /* largest VMA */
920
 
  struct line_info* lcl_head;   /* local head; used in 'add_line_info' */
 
927
  bfd*                  abfd;
 
928
  unsigned int          num_files;
 
929
  unsigned int          num_dirs;
 
930
  unsigned int          num_sequences;
 
931
  char *                comp_dir;
 
932
  char **               dirs;
 
933
  struct fileinfo*      files;
 
934
  struct line_sequence* sequences;
 
935
  struct line_info*     lcl_head;   /* Local head; used in 'add_line_info'.  */
921
936
};
922
937
 
923
938
/* Remember some information about each function.  If the function is
981
996
               int end_sequence)
982
997
{
983
998
  bfd_size_type amt = sizeof (struct line_info);
 
999
  struct line_sequence* seq = table->sequences;
984
1000
  struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
985
1001
 
986
1002
  /* Set member data of 'info'.  */
1008
1024
       p...z a...j  (where a < j < p < z)
1009
1025
 
1010
1026
     Note: table->lcl_head is used to head an *actual* or *possible*
1011
 
     sequence within the list (such as a...j) that is not directly
 
1027
     sub-sequence within the list (such as a...j) that is not directly
1012
1028
     headed by table->last_line
1013
1029
 
1014
1030
     Note: we may receive duplicate entries from 'decode_line_info'.  */
1015
1031
 
1016
 
  if (table->last_line
1017
 
      && table->last_line->address == address
1018
 
      && table->last_line->end_sequence == end_sequence)
 
1032
  if (seq
 
1033
      && seq->last_line->address == address
 
1034
      && seq->last_line->end_sequence == end_sequence)
1019
1035
    {
1020
1036
      /* We only keep the last entry with the same address and end
1021
1037
         sequence.  See PR ld/4986.  */
1022
 
      if (table->lcl_head == table->last_line)
 
1038
      if (table->lcl_head == seq->last_line)
1023
1039
        table->lcl_head = info;
1024
 
      info->prev_line = table->last_line->prev_line;
1025
 
      table->last_line = info;
1026
 
    }
1027
 
  else if (!table->last_line
1028
 
      || new_line_sorts_after (info, table->last_line))
1029
 
    {
1030
 
      /* Normal case: add 'info' to the beginning of the list */
1031
 
      info->prev_line = table->last_line;
1032
 
      table->last_line = info;
 
1040
      info->prev_line = seq->last_line->prev_line;
 
1041
      seq->last_line = info;
 
1042
    }
 
1043
  else if (!seq || seq->last_line->end_sequence)
 
1044
    {
 
1045
      /* Start a new line sequence.  */
 
1046
      amt = sizeof (struct line_sequence);
 
1047
      seq = (struct line_sequence *) bfd_malloc (amt);
 
1048
      seq->low_pc = address;
 
1049
      seq->prev_sequence = table->sequences;
 
1050
      seq->last_line = info;
 
1051
      table->lcl_head = info;
 
1052
      table->sequences = seq;
 
1053
      table->num_sequences++;
 
1054
    }
 
1055
  else if (new_line_sorts_after (info, seq->last_line))
 
1056
    {
 
1057
      /* Normal case: add 'info' to the beginning of the current sequence.  */
 
1058
      info->prev_line = seq->last_line;
 
1059
      seq->last_line = info;
1033
1060
 
1034
1061
      /* lcl_head: initialize to head a *possible* sequence at the end.  */
1035
1062
      if (!table->lcl_head)
1045
1072
    }
1046
1073
  else
1047
1074
    {
1048
 
      /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
1049
 
         heads for 'info'.  Reset 'lcl_head'.  */
1050
 
      struct line_info* li2 = table->last_line; /* always non-NULL */
 
1075
      /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
 
1076
         are valid heads for 'info'.  Reset 'lcl_head'.  */
 
1077
      struct line_info* li2 = seq->last_line; /* Always non-NULL.  */
1051
1078
      struct line_info* li1 = li2->prev_line;
1052
1079
 
1053
1080
      while (li1)
1062
1089
      table->lcl_head = li2;
1063
1090
      info->prev_line = table->lcl_head->prev_line;
1064
1091
      table->lcl_head->prev_line = info;
 
1092
      if (address < seq->low_pc)
 
1093
        seq->low_pc = address;
1065
1094
    }
1066
1095
}
1067
1096
 
1087
1116
 
1088
1117
  if (!IS_ABSOLUTE_PATH (filename))
1089
1118
    {
1090
 
      char *dirname = NULL;
1091
 
      char *subdirname = NULL;
 
1119
      char *dir_name = NULL;
 
1120
      char *subdir_name = NULL;
1092
1121
      char *name;
1093
1122
      size_t len;
1094
1123
 
1095
1124
      if (table->files[file - 1].dir)
1096
 
        subdirname = table->dirs[table->files[file - 1].dir - 1];
1097
 
 
1098
 
      if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
1099
 
        dirname = table->comp_dir;
1100
 
 
1101
 
      if (!dirname)
 
1125
        subdir_name = table->dirs[table->files[file - 1].dir - 1];
 
1126
 
 
1127
      if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
 
1128
        dir_name = table->comp_dir;
 
1129
 
 
1130
      if (!dir_name)
1102
1131
        {
1103
 
          dirname = subdirname;
1104
 
          subdirname = NULL;
 
1132
          dir_name = subdir_name;
 
1133
          subdir_name = NULL;
1105
1134
        }
1106
1135
 
1107
 
      if (!dirname)
 
1136
      if (!dir_name)
1108
1137
        return strdup (filename);
1109
1138
 
1110
 
      len = strlen (dirname) + strlen (filename) + 2;
 
1139
      len = strlen (dir_name) + strlen (filename) + 2;
1111
1140
 
1112
 
      if (subdirname)
 
1141
      if (subdir_name)
1113
1142
        {
1114
 
          len += strlen (subdirname) + 1;
 
1143
          len += strlen (subdir_name) + 1;
1115
1144
          name = (char *) bfd_malloc (len);
1116
1145
          if (name)
1117
 
            sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
 
1146
            sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
1118
1147
        }
1119
1148
      else
1120
1149
        {
1121
1150
          name = (char *) bfd_malloc (len);
1122
1151
          if (name)
1123
 
            sprintf (name, "%s/%s", dirname, filename);
 
1152
            sprintf (name, "%s/%s", dir_name, filename);
1124
1153
        }
1125
1154
 
1126
1155
      return name;
1169
1198
  first_arange->next = arange;
1170
1199
}
1171
1200
 
 
1201
/* Compare function for line sequences.  */
 
1202
 
 
1203
static int
 
1204
compare_sequences (const void* a, const void* b)
 
1205
{
 
1206
  const struct line_sequence* seq1 = a;
 
1207
  const struct line_sequence* seq2 = b;
 
1208
 
 
1209
  /* Sort by low_pc as the primary key.  */
 
1210
  if (seq1->low_pc < seq2->low_pc)
 
1211
    return -1;
 
1212
  if (seq1->low_pc > seq2->low_pc)
 
1213
    return 1;
 
1214
 
 
1215
  /* If low_pc values are equal, sort in reverse order of
 
1216
     high_pc, so that the largest region comes first.  */
 
1217
  if (seq1->last_line->address < seq2->last_line->address)
 
1218
    return 1;
 
1219
  if (seq1->last_line->address > seq2->last_line->address)
 
1220
    return -1;
 
1221
 
 
1222
  return 0;
 
1223
}
 
1224
 
 
1225
/* Sort the line sequences for quick lookup.  */
 
1226
 
 
1227
static void
 
1228
sort_line_sequences (struct line_info_table* table)
 
1229
{
 
1230
  bfd_size_type amt;
 
1231
  struct line_sequence* sequences;
 
1232
  struct line_sequence* seq;
 
1233
  unsigned int n = 0;
 
1234
  unsigned int num_sequences = table->num_sequences;
 
1235
  bfd_vma last_high_pc;
 
1236
 
 
1237
  if (num_sequences == 0)
 
1238
    return;
 
1239
 
 
1240
  /* Allocate space for an array of sequences.  */
 
1241
  amt = sizeof (struct line_sequence) * num_sequences;
 
1242
  sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
 
1243
 
 
1244
  /* Copy the linked list into the array, freeing the original nodes.  */
 
1245
  seq = table->sequences;
 
1246
  for (n = 0; n < num_sequences; n++)
 
1247
    {
 
1248
      struct line_sequence* last_seq = seq;
 
1249
 
 
1250
      BFD_ASSERT (seq);
 
1251
      sequences[n].low_pc = seq->low_pc;
 
1252
      sequences[n].prev_sequence = NULL;
 
1253
      sequences[n].last_line = seq->last_line;
 
1254
      seq = seq->prev_sequence;
 
1255
      free (last_seq);
 
1256
    }
 
1257
  BFD_ASSERT (seq == NULL);
 
1258
 
 
1259
  qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
 
1260
 
 
1261
  /* Make the list binary-searchable by trimming overlapping entries
 
1262
     and removing nested entries.  */
 
1263
  num_sequences = 1;
 
1264
  last_high_pc = sequences[0].last_line->address;
 
1265
  for (n = 1; n < table->num_sequences; n++)
 
1266
    {
 
1267
      if (sequences[n].low_pc < last_high_pc)
 
1268
        {
 
1269
          if (sequences[n].last_line->address <= last_high_pc)
 
1270
            /* Skip nested entries.  */
 
1271
            continue;
 
1272
 
 
1273
          /* Trim overlapping entries.  */
 
1274
          sequences[n].low_pc = last_high_pc;
 
1275
        }
 
1276
      last_high_pc = sequences[n].last_line->address;
 
1277
      if (n > num_sequences)
 
1278
        {
 
1279
          /* Close up the gap.  */
 
1280
          sequences[num_sequences].low_pc = sequences[n].low_pc;
 
1281
          sequences[num_sequences].last_line = sequences[n].last_line;
 
1282
        }
 
1283
      num_sequences++;
 
1284
    }
 
1285
 
 
1286
  table->sequences = sequences;
 
1287
  table->num_sequences = num_sequences;
 
1288
}
 
1289
 
1172
1290
/* Decode the line number information for UNIT.  */
1173
1291
 
1174
1292
static struct line_info_table*
1200
1318
  table->num_dirs = 0;
1201
1319
  table->dirs = NULL;
1202
1320
 
1203
 
  table->files = NULL;
1204
 
  table->last_line = NULL;
 
1321
  table->num_sequences = 0;
 
1322
  table->sequences = NULL;
 
1323
 
1205
1324
  table->lcl_head = NULL;
1206
1325
 
1207
1326
  line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1468
1587
              line_ptr += 2;
1469
1588
              break;
1470
1589
            default:
1471
 
              {
1472
 
                int i;
1473
 
 
1474
 
                /* Unknown standard opcode, ignore it.  */
1475
 
                for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1476
 
                  {
1477
 
                    (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1478
 
                    line_ptr += bytes_read;
1479
 
                  }
1480
 
              }
 
1590
              /* Unknown standard opcode, ignore it.  */
 
1591
              for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
 
1592
                {
 
1593
                  (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
 
1594
                  line_ptr += bytes_read;
 
1595
                }
 
1596
              break;
1481
1597
            }
1482
1598
        }
1483
1599
 
1485
1601
        free (filename);
1486
1602
    }
1487
1603
 
 
1604
  sort_line_sequences (table);
 
1605
 
1488
1606
  return table;
1489
1607
}
1490
1608
 
1495
1613
static bfd_boolean
1496
1614
lookup_address_in_line_info_table (struct line_info_table *table,
1497
1615
                                   bfd_vma addr,
1498
 
                                   struct funcinfo *function,
1499
1616
                                   const char **filename_ptr,
1500
1617
                                   unsigned int *linenumber_ptr)
1501
1618
{
1502
 
  /* Note: table->last_line should be a descendingly sorted list. */
1503
 
  struct line_info* next_line = table->last_line;
1504
 
  struct line_info* each_line = NULL;
 
1619
  struct line_sequence *seq = NULL;
 
1620
  struct line_info *each_line;
 
1621
  int low, high, mid;
 
1622
 
 
1623
  /* Binary search the array of sequences.  */
 
1624
  low = 0;
 
1625
  high = table->num_sequences;
 
1626
  while (low < high)
 
1627
    {
 
1628
      mid = (low + high) / 2;
 
1629
      seq = &table->sequences[mid];
 
1630
      if (addr < seq->low_pc)
 
1631
        high = mid;
 
1632
      else if (addr >= seq->last_line->address)
 
1633
        low = mid + 1;
 
1634
      else
 
1635
        break;
 
1636
    }
 
1637
 
 
1638
  if (seq && addr >= seq->low_pc && addr < seq->last_line->address)
 
1639
    {
 
1640
      /* Note: seq->last_line should be a descendingly sorted list.  */
 
1641
      for (each_line = seq->last_line;
 
1642
           each_line;
 
1643
           each_line = each_line->prev_line)
 
1644
        if (addr >= each_line->address)
 
1645
          break;
 
1646
 
 
1647
      if (each_line
 
1648
          && !(each_line->end_sequence || each_line == seq->last_line))
 
1649
        {
 
1650
          *filename_ptr = each_line->filename;
 
1651
          *linenumber_ptr = each_line->line;
 
1652
          return TRUE;
 
1653
        }
 
1654
    }
 
1655
 
1505
1656
  *filename_ptr = NULL;
1506
 
 
1507
 
  if (!next_line)
1508
 
    return FALSE;
1509
 
 
1510
 
  each_line = next_line->prev_line;
1511
 
 
1512
 
  /* Check for large addresses */
1513
 
  if (addr > next_line->address)
1514
 
    each_line = NULL; /* ensure we skip over the normal case */
1515
 
 
1516
 
  /* Normal case: search the list; save  */
1517
 
  while (each_line && next_line)
1518
 
    {
1519
 
      /* If we have an address match, save this info.  This allows us
1520
 
         to return as good as results as possible for strange debugging
1521
 
         info.  */
1522
 
      bfd_boolean addr_match = FALSE;
1523
 
      if (each_line->address <= addr && addr < next_line->address)
1524
 
        {
1525
 
          addr_match = TRUE;
1526
 
 
1527
 
          /* If this line appears to span functions, and addr is in the
1528
 
             later function, return the first line of that function instead
1529
 
             of the last line of the earlier one.  This check is for GCC
1530
 
             2.95, which emits the first line number for a function late.  */
1531
 
 
1532
 
          if (function != NULL)
1533
 
            {
1534
 
              bfd_vma lowest_pc;
1535
 
              struct arange *arange;
1536
 
 
1537
 
              /* Find the lowest address in the function's range list */
1538
 
              lowest_pc = function->arange.low;
1539
 
              for (arange = &function->arange;
1540
 
                   arange;
1541
 
                   arange = arange->next)
1542
 
                {
1543
 
                  if (function->arange.low < lowest_pc)
1544
 
                    lowest_pc = function->arange.low;
1545
 
                }
1546
 
              /* Check for spanning function and set outgoing line info */
1547
 
              if (addr >= lowest_pc
1548
 
                  && each_line->address < lowest_pc
1549
 
                  && next_line->address > lowest_pc)
1550
 
                {
1551
 
                  *filename_ptr = next_line->filename;
1552
 
                  *linenumber_ptr = next_line->line;
1553
 
                }
1554
 
              else
1555
 
                {
1556
 
                  *filename_ptr = each_line->filename;
1557
 
                  *linenumber_ptr = each_line->line;
1558
 
                }
1559
 
            }
1560
 
          else
1561
 
            {
1562
 
              *filename_ptr = each_line->filename;
1563
 
              *linenumber_ptr = each_line->line;
1564
 
            }
1565
 
        }
1566
 
 
1567
 
      if (addr_match && !each_line->end_sequence)
1568
 
        return TRUE; /* we have definitely found what we want */
1569
 
 
1570
 
      next_line = each_line;
1571
 
      each_line = each_line->prev_line;
1572
 
    }
1573
 
 
1574
 
  /* At this point each_line is NULL but next_line is not.  If we found
1575
 
     a candidate end-of-sequence point in the loop above, we can return
1576
 
     that (compatibility with a bug in the Intel compiler); otherwise,
1577
 
     assuming that we found the containing function for this address in
1578
 
     this compilation unit, return the first line we have a number for
1579
 
     (compatibility with GCC 2.95).  */
1580
 
  if (*filename_ptr == NULL && function != NULL)
1581
 
    {
1582
 
      *filename_ptr = next_line->filename;
1583
 
      *linenumber_ptr = next_line->line;
1584
 
      return TRUE;
1585
 
    }
1586
 
 
1587
1657
  return FALSE;
1588
1658
}
1589
1659
 
1590
 
/* Read in the .debug_ranges section for future reference */
 
1660
/* Read in the .debug_ranges section for future reference.  */
1591
1661
 
1592
1662
static bfd_boolean
1593
1663
read_debug_ranges (struct comp_unit *unit)
1748
1818
      if (!die_ref)
1749
1819
        abort ();
1750
1820
 
1751
 
      info_ptr = unit->stash->sec_info_ptr + die_ref;
 
1821
      info_ptr = unit->sec_info_ptr + die_ref;
1752
1822
    }
1753
1823
  else 
1754
1824
    info_ptr = unit->info_ptr_unit + die_ref;
2156
2226
  unit->end_ptr = end_ptr;
2157
2227
  unit->stash = stash;
2158
2228
  unit->info_ptr_unit = info_ptr_unit;
 
2229
  unit->sec_info_ptr = stash->sec_info_ptr;
2159
2230
 
2160
2231
  for (i = 0; i < abbrev->num_attrs; ++i)
2161
2232
    {
2298
2369
  if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2299
2370
    stash->inliner_chain = function;
2300
2371
  line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2301
 
                                              function, filename_ptr,
 
2372
                                              filename_ptr,
2302
2373
                                              linenumber_ptr);
2303
2374
  return line_p || func_p;
2304
2375
}
2537
2608
      asection *sect;
2538
2609
      bfd_vma last_vma = 0, last_dwarf = 0;
2539
2610
      bfd_size_type amt;
2540
 
      struct adjusted_section *p;
2541
2611
 
2542
2612
      i = 0;
2543
2613
      for (sect = abfd->sections; sect != NULL; sect = sect->next)