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.
5
5
Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
1008
1024
p...z a...j (where a < j < p < z)
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
1014
1030
Note: we may receive duplicate entries from 'decode_line_info'. */
1016
if (table->last_line
1017
&& table->last_line->address == address
1018
&& table->last_line->end_sequence == end_sequence)
1033
&& seq->last_line->address == address
1034
&& seq->last_line->end_sequence == end_sequence)
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;
1027
else if (!table->last_line
1028
|| new_line_sorts_after (info, table->last_line))
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;
1043
else if (!seq || seq->last_line->end_sequence)
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++;
1055
else if (new_line_sorts_after (info, seq->last_line))
1057
/* Normal case: add 'info' to the beginning of the current sequence. */
1058
info->prev_line = seq->last_line;
1059
seq->last_line = info;
1034
1061
/* lcl_head: initialize to head a *possible* sequence at the end. */
1035
1062
if (!table->lcl_head)
1088
1117
if (!IS_ABSOLUTE_PATH (filename))
1090
char *dirname = NULL;
1091
char *subdirname = NULL;
1119
char *dir_name = NULL;
1120
char *subdir_name = NULL;
1095
1124
if (table->files[file - 1].dir)
1096
subdirname = table->dirs[table->files[file - 1].dir - 1];
1098
if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
1099
dirname = table->comp_dir;
1125
subdir_name = table->dirs[table->files[file - 1].dir - 1];
1127
if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1128
dir_name = table->comp_dir;
1103
dirname = subdirname;
1132
dir_name = subdir_name;
1108
1137
return strdup (filename);
1110
len = strlen (dirname) + strlen (filename) + 2;
1139
len = strlen (dir_name) + strlen (filename) + 2;
1114
len += strlen (subdirname) + 1;
1143
len += strlen (subdir_name) + 1;
1115
1144
name = (char *) bfd_malloc (len);
1117
sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
1146
sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
1121
1150
name = (char *) bfd_malloc (len);
1123
sprintf (name, "%s/%s", dirname, filename);
1152
sprintf (name, "%s/%s", dir_name, filename);
1169
1198
first_arange->next = arange;
1201
/* Compare function for line sequences. */
1204
compare_sequences (const void* a, const void* b)
1206
const struct line_sequence* seq1 = a;
1207
const struct line_sequence* seq2 = b;
1209
/* Sort by low_pc as the primary key. */
1210
if (seq1->low_pc < seq2->low_pc)
1212
if (seq1->low_pc > seq2->low_pc)
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)
1219
if (seq1->last_line->address > seq2->last_line->address)
1225
/* Sort the line sequences for quick lookup. */
1228
sort_line_sequences (struct line_info_table* table)
1231
struct line_sequence* sequences;
1232
struct line_sequence* seq;
1234
unsigned int num_sequences = table->num_sequences;
1235
bfd_vma last_high_pc;
1237
if (num_sequences == 0)
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);
1244
/* Copy the linked list into the array, freeing the original nodes. */
1245
seq = table->sequences;
1246
for (n = 0; n < num_sequences; n++)
1248
struct line_sequence* last_seq = 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;
1257
BFD_ASSERT (seq == NULL);
1259
qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1261
/* Make the list binary-searchable by trimming overlapping entries
1262
and removing nested entries. */
1264
last_high_pc = sequences[0].last_line->address;
1265
for (n = 1; n < table->num_sequences; n++)
1267
if (sequences[n].low_pc < last_high_pc)
1269
if (sequences[n].last_line->address <= last_high_pc)
1270
/* Skip nested entries. */
1273
/* Trim overlapping entries. */
1274
sequences[n].low_pc = last_high_pc;
1276
last_high_pc = sequences[n].last_line->address;
1277
if (n > num_sequences)
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;
1286
table->sequences = sequences;
1287
table->num_sequences = num_sequences;
1172
1290
/* Decode the line number information for UNIT. */
1174
1292
static struct line_info_table*
1495
1613
static bfd_boolean
1496
1614
lookup_address_in_line_info_table (struct line_info_table *table,
1498
struct funcinfo *function,
1499
1616
const char **filename_ptr,
1500
1617
unsigned int *linenumber_ptr)
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;
1623
/* Binary search the array of sequences. */
1625
high = table->num_sequences;
1628
mid = (low + high) / 2;
1629
seq = &table->sequences[mid];
1630
if (addr < seq->low_pc)
1632
else if (addr >= seq->last_line->address)
1638
if (seq && addr >= seq->low_pc && addr < seq->last_line->address)
1640
/* Note: seq->last_line should be a descendingly sorted list. */
1641
for (each_line = seq->last_line;
1643
each_line = each_line->prev_line)
1644
if (addr >= each_line->address)
1648
&& !(each_line->end_sequence || each_line == seq->last_line))
1650
*filename_ptr = each_line->filename;
1651
*linenumber_ptr = each_line->line;
1505
1656
*filename_ptr = NULL;
1510
each_line = next_line->prev_line;
1512
/* Check for large addresses */
1513
if (addr > next_line->address)
1514
each_line = NULL; /* ensure we skip over the normal case */
1516
/* Normal case: search the list; save */
1517
while (each_line && next_line)
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
1522
bfd_boolean addr_match = FALSE;
1523
if (each_line->address <= addr && addr < next_line->address)
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. */
1532
if (function != NULL)
1535
struct arange *arange;
1537
/* Find the lowest address in the function's range list */
1538
lowest_pc = function->arange.low;
1539
for (arange = &function->arange;
1541
arange = arange->next)
1543
if (function->arange.low < lowest_pc)
1544
lowest_pc = function->arange.low;
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)
1551
*filename_ptr = next_line->filename;
1552
*linenumber_ptr = next_line->line;
1556
*filename_ptr = each_line->filename;
1557
*linenumber_ptr = each_line->line;
1562
*filename_ptr = each_line->filename;
1563
*linenumber_ptr = each_line->line;
1567
if (addr_match && !each_line->end_sequence)
1568
return TRUE; /* we have definitely found what we want */
1570
next_line = each_line;
1571
each_line = each_line->prev_line;
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)
1582
*filename_ptr = next_line->filename;
1583
*linenumber_ptr = next_line->line;
1590
/* Read in the .debug_ranges section for future reference */
1660
/* Read in the .debug_ranges section for future reference. */
1592
1662
static bfd_boolean
1593
1663
read_debug_ranges (struct comp_unit *unit)