PR 44
[deliverable/binutils-gdb.git] / bfd / dwarf2.c
CommitLineData
252b5132 1/* DWARF 2 support.
818a27ac
AM
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004 Free Software Foundation, Inc.
252b5132
RH
4
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6 (gavin@cygnus.com).
7
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
15
e2f6d277 16 This file is part of BFD.
252b5132 17
e2f6d277
NC
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or (at
21 your option) any later version.
252b5132 22
e2f6d277
NC
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
252b5132 27
e2f6d277
NC
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
252b5132
RH
31
32#include "bfd.h"
33#include "sysdep.h"
34#include "libiberty.h"
35#include "libbfd.h"
36#include "elf-bfd.h"
37#include "elf/dwarf2.h"
38
39/* The data in the .debug_line statement prologue looks like this. */
a092b084 40
252b5132 41struct line_head
a092b084 42{
d03ba2a1 43 bfd_vma total_length;
a092b084 44 unsigned short version;
f46c2da6 45 bfd_vma prologue_length;
a092b084
NC
46 unsigned char minimum_instruction_length;
47 unsigned char default_is_stmt;
48 int line_base;
49 unsigned char line_range;
50 unsigned char opcode_base;
51 unsigned char *standard_opcode_lengths;
52};
53
54/* Attributes have a name and a value. */
55
252b5132 56struct attribute
a092b084
NC
57{
58 enum dwarf_attribute name;
59 enum dwarf_form form;
60 union
252b5132 61 {
a092b084
NC
62 char *str;
63 struct dwarf_block *blk;
8ce8c090
AM
64 bfd_uint64_t val;
65 bfd_int64_t sval;
a092b084
NC
66 }
67 u;
68};
69
98591c73 70/* Blocks are a bunch of untyped bytes. */
252b5132 71struct dwarf_block
a092b084
NC
72{
73 unsigned int size;
74 char *data;
75};
252b5132 76
a092b084
NC
77struct dwarf2_debug
78{
79 /* A list of all previously read comp_units. */
252b5132
RH
80 struct comp_unit* all_comp_units;
81
82 /* The next unread compilation unit within the .debug_info section.
83 Zero indicates that the .debug_info section has not been loaded
a092b084 84 into a buffer yet. */
252b5132
RH
85 char* info_ptr;
86
a092b084 87 /* Pointer to the end of the .debug_info section memory buffer. */
252b5132
RH
88 char* info_ptr_end;
89
f2363ce5
AO
90 /* Pointer to the section and address of the beginning of the
91 section. */
92 asection* sec;
93 char* sec_info_ptr;
94
95 /* Pointer to the symbol table. */
96 asymbol** syms;
97
a092b084 98 /* Pointer to the .debug_abbrev section loaded into memory. */
252b5132
RH
99 char* dwarf_abbrev_buffer;
100
a092b084 101 /* Length of the loaded .debug_abbrev section. */
252b5132 102 unsigned long dwarf_abbrev_size;
69dd2e2d
RH
103
104 /* Buffer for decode_line_info. */
105 char *dwarf_line_buffer;
ccdb16fc
JW
106
107 /* Length of the loaded .debug_line section. */
108 unsigned long dwarf_line_size;
d03ba2a1
JJ
109
110 /* Pointer to the .debug_str section loaded into memory. */
111 char* dwarf_str_buffer;
112
113 /* Length of the loaded .debug_str section. */
114 unsigned long dwarf_str_size;
252b5132
RH
115};
116
a092b084
NC
117struct arange
118{
f623be2b
RH
119 struct arange *next;
120 bfd_vma low;
121 bfd_vma high;
122};
252b5132 123
252b5132 124/* A minimal decoding of DWARF2 compilation units. We only decode
a092b084 125 what's needed to get to the line number information. */
252b5132 126
a092b084
NC
127struct comp_unit
128{
129 /* Chain the previously read compilation units. */
252b5132
RH
130 struct comp_unit* next_unit;
131
a092b084 132 /* Keep the bdf convenient (for memory allocation). */
252b5132
RH
133 bfd* abfd;
134
135 /* The lowest and higest addresses contained in this compilation
a092b084 136 unit as specified in the compilation unit header. */
f623be2b 137 struct arange arange;
252b5132 138
a092b084 139 /* The DW_AT_name attribute (for error messages). */
252b5132
RH
140 char* name;
141
a092b084 142 /* The abbrev hash table. */
252b5132
RH
143 struct abbrev_info** abbrevs;
144
a092b084 145 /* Note that an error was found by comp_unit_find_nearest_line. */
252b5132
RH
146 int error;
147
a092b084 148 /* The DW_AT_comp_dir attribute. */
252b5132
RH
149 char* comp_dir;
150
b34976b6 151 /* TRUE if there is a line number table associated with this comp. unit. */
252b5132 152 int stmtlist;
98591c73 153
a092b084 154 /* The offset into .debug_line of the line number table. */
252b5132
RH
155 unsigned long line_offset;
156
a092b084 157 /* Pointer to the first child die for the comp unit. */
252b5132
RH
158 char *first_child_die_ptr;
159
a092b084 160 /* The end of the comp unit. */
252b5132
RH
161 char *end_ptr;
162
a092b084 163 /* The decoded line number, NULL if not yet decoded. */
252b5132
RH
164 struct line_info_table* line_table;
165
a092b084 166 /* A list of the functions found in this comp. unit. */
98591c73 167 struct funcinfo* function_table;
252b5132 168
d03ba2a1
JJ
169 /* Pointer to dwarf2_debug structure. */
170 struct dwarf2_debug *stash;
171
a092b084 172 /* Address size for this unit - from unit header. */
252b5132 173 unsigned char addr_size;
d03ba2a1
JJ
174
175 /* Offset size for this unit - from unit header. */
176 unsigned char offset_size;
252b5132
RH
177};
178
a7b97311
AM
179/* This data structure holds the information of an abbrev. */
180struct abbrev_info
181{
182 unsigned int number; /* Number identifying abbrev. */
183 enum dwarf_tag tag; /* DWARF tag. */
184 int has_children; /* Boolean. */
185 unsigned int num_attrs; /* Number of attributes. */
186 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
187 struct abbrev_info *next; /* Next in chain. */
188};
189
190struct attr_abbrev
191{
192 enum dwarf_attribute name;
193 enum dwarf_form form;
194};
195
196#ifndef ABBREV_HASH_SIZE
197#define ABBREV_HASH_SIZE 121
198#endif
199#ifndef ATTR_ALLOC_CHUNK
200#define ATTR_ALLOC_CHUNK 4
201#endif
202
98591c73
KH
203/* VERBATIM
204 The following function up to the END VERBATIM mark are
a092b084 205 copied directly from dwarf2read.c. */
252b5132 206
a092b084 207/* Read dwarf information from a buffer. */
252b5132
RH
208
209static unsigned int
818a27ac 210read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, char *buf)
252b5132 211{
818a27ac 212 return bfd_get_8 (abfd, buf);
252b5132
RH
213}
214
215static int
818a27ac 216read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, char *buf)
252b5132 217{
818a27ac 218 return bfd_get_signed_8 (abfd, buf);
252b5132
RH
219}
220
221static unsigned int
818a27ac 222read_2_bytes (bfd *abfd, char *buf)
252b5132 223{
818a27ac 224 return bfd_get_16 (abfd, buf);
252b5132
RH
225}
226
252b5132 227static unsigned int
818a27ac 228read_4_bytes (bfd *abfd, char *buf)
252b5132 229{
818a27ac 230 return bfd_get_32 (abfd, buf);
252b5132
RH
231}
232
8ce8c090 233static bfd_uint64_t
818a27ac 234read_8_bytes (bfd *abfd, char *buf)
252b5132 235{
818a27ac 236 return bfd_get_64 (abfd, buf);
252b5132
RH
237}
238
239static char *
818a27ac
AM
240read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
241 char *buf,
242 unsigned int size ATTRIBUTE_UNUSED)
252b5132
RH
243{
244 /* If the size of a host char is 8 bits, we can return a pointer
245 to the buffer, otherwise we have to copy the data to a buffer
246 allocated on the temporary obstack. */
247 return buf;
248}
249
250static char *
818a27ac
AM
251read_string (bfd *abfd ATTRIBUTE_UNUSED,
252 char *buf,
253 unsigned int *bytes_read_ptr)
252b5132 254{
d03ba2a1 255 /* Return a pointer to the embedded string. */
252b5132
RH
256 if (*buf == '\0')
257 {
258 *bytes_read_ptr = 1;
259 return NULL;
260 }
98591c73 261
252b5132
RH
262 *bytes_read_ptr = strlen (buf) + 1;
263 return buf;
264}
265
d03ba2a1 266static char *
818a27ac
AM
267read_indirect_string (struct comp_unit* unit,
268 char *buf,
269 unsigned int *bytes_read_ptr)
d03ba2a1 270{
8ce8c090 271 bfd_uint64_t offset;
d03ba2a1
JJ
272 struct dwarf2_debug *stash = unit->stash;
273
274 if (unit->offset_size == 4)
275 offset = read_4_bytes (unit->abfd, buf);
276 else
277 offset = read_8_bytes (unit->abfd, buf);
278 *bytes_read_ptr = unit->offset_size;
279
280 if (! stash->dwarf_str_buffer)
281 {
282 asection *msec;
283 bfd *abfd = unit->abfd;
eea6121a 284 bfd_size_type sz;
d03ba2a1
JJ
285
286 msec = bfd_get_section_by_name (abfd, ".debug_str");
287 if (! msec)
288 {
289 (*_bfd_error_handler)
290 (_("Dwarf Error: Can't find .debug_str section."));
291 bfd_set_error (bfd_error_bad_value);
292 return NULL;
293 }
294
eea6121a
AM
295 sz = msec->rawsize ? msec->rawsize : msec->size;
296 stash->dwarf_str_size = sz;
297 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
d03ba2a1
JJ
298 if (! stash->dwarf_abbrev_buffer)
299 return NULL;
300
9f632188 301 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
eea6121a 302 0, sz))
d03ba2a1
JJ
303 return NULL;
304 }
305
306 if (offset >= stash->dwarf_str_size)
307 {
f46c2da6
AM
308 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
309 (unsigned long) offset, stash->dwarf_str_size);
d03ba2a1
JJ
310 bfd_set_error (bfd_error_bad_value);
311 return NULL;
312 }
313
e82ce529 314 buf = stash->dwarf_str_buffer + offset;
d03ba2a1
JJ
315 if (*buf == '\0')
316 return NULL;
317 return buf;
318}
319
252b5132 320static unsigned int
818a27ac
AM
321read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
322 char *buf,
323 unsigned int *bytes_read_ptr)
252b5132
RH
324{
325 unsigned int result;
326 unsigned int num_read;
327 int shift;
328 unsigned char byte;
329
330 result = 0;
331 shift = 0;
332 num_read = 0;
98591c73 333
252b5132
RH
334 do
335 {
818a27ac 336 byte = bfd_get_8 (abfd, buf);
252b5132
RH
337 buf ++;
338 num_read ++;
339 result |= ((byte & 0x7f) << shift);
340 shift += 7;
341 }
342 while (byte & 0x80);
98591c73 343
252b5132 344 * bytes_read_ptr = num_read;
98591c73 345
252b5132
RH
346 return result;
347}
348
349static int
818a27ac
AM
350read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
351 char *buf,
352 unsigned int * bytes_read_ptr)
252b5132
RH
353{
354 int result;
355 int shift;
356 int num_read;
357 unsigned char byte;
358
359 result = 0;
360 shift = 0;
361 num_read = 0;
362
363 do
364 {
818a27ac 365 byte = bfd_get_8 (abfd, buf);
252b5132
RH
366 buf ++;
367 num_read ++;
368 result |= ((byte & 0x7f) << shift);
369 shift += 7;
370 }
371 while (byte & 0x80);
98591c73 372
252b5132
RH
373 if ((shift < 32) && (byte & 0x40))
374 result |= -(1 << shift);
375
376 * bytes_read_ptr = num_read;
98591c73 377
252b5132
RH
378 return result;
379}
380
381/* END VERBATIM */
382
8ce8c090 383static bfd_uint64_t
818a27ac 384read_address (struct comp_unit *unit, char *buf)
252b5132 385{
ecb651f0 386 switch (unit->addr_size)
252b5132 387 {
ecb651f0 388 case 8:
818a27ac 389 return bfd_get_64 (unit->abfd, buf);
ecb651f0 390 case 4:
818a27ac 391 return bfd_get_32 (unit->abfd, buf);
ecb651f0 392 case 2:
818a27ac 393 return bfd_get_16 (unit->abfd, buf);
ecb651f0
NC
394 default:
395 abort ();
252b5132 396 }
252b5132
RH
397}
398
252b5132
RH
399/* Lookup an abbrev_info structure in the abbrev hash table. */
400
401static struct abbrev_info *
818a27ac 402lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
252b5132
RH
403{
404 unsigned int hash_number;
405 struct abbrev_info *abbrev;
406
407 hash_number = number % ABBREV_HASH_SIZE;
408 abbrev = abbrevs[hash_number];
409
410 while (abbrev)
411 {
412 if (abbrev->number == number)
413 return abbrev;
414 else
415 abbrev = abbrev->next;
416 }
98591c73 417
252b5132
RH
418 return NULL;
419}
420
421/* In DWARF version 2, the description of the debugging information is
422 stored in a separate .debug_abbrev section. Before we read any
423 dies from a section we read in all abbreviations and install them
424 in a hash table. */
425
426static struct abbrev_info**
8ce8c090 427read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
252b5132
RH
428{
429 struct abbrev_info **abbrevs;
430 char *abbrev_ptr;
431 struct abbrev_info *cur_abbrev;
432 unsigned int abbrev_number, bytes_read, abbrev_name;
433 unsigned int abbrev_form, hash_number;
dc810e39 434 bfd_size_type amt;
252b5132
RH
435
436 if (! stash->dwarf_abbrev_buffer)
437 {
438 asection *msec;
439
440 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
441 if (! msec)
442 {
443 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
444 bfd_set_error (bfd_error_bad_value);
445 return 0;
446 }
98591c73 447
eea6121a 448 stash->dwarf_abbrev_size = msec->size;
6e84a906
DJ
449 stash->dwarf_abbrev_buffer
450 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
451 stash->syms);
252b5132
RH
452 if (! stash->dwarf_abbrev_buffer)
453 return 0;
252b5132
RH
454 }
455
f5198f61 456 if (offset >= stash->dwarf_abbrev_size)
252b5132 457 {
f46c2da6
AM
458 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
459 (unsigned long) offset, stash->dwarf_abbrev_size);
252b5132
RH
460 bfd_set_error (bfd_error_bad_value);
461 return 0;
462 }
463
dc810e39 464 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
818a27ac 465 abbrevs = bfd_zalloc (abfd, amt);
252b5132
RH
466
467 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
468 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
469 abbrev_ptr += bytes_read;
470
a092b084 471 /* Loop until we reach an abbrev number of 0. */
252b5132
RH
472 while (abbrev_number)
473 {
dc810e39 474 amt = sizeof (struct abbrev_info);
818a27ac 475 cur_abbrev = bfd_zalloc (abfd, amt);
252b5132 476
a092b084 477 /* Read in abbrev header. */
252b5132 478 cur_abbrev->number = abbrev_number;
d45913a0
DA
479 cur_abbrev->tag = (enum dwarf_tag)
480 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
252b5132
RH
481 abbrev_ptr += bytes_read;
482 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
483 abbrev_ptr += 1;
484
a092b084 485 /* Now read in declarations. */
252b5132
RH
486 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
487 abbrev_ptr += bytes_read;
488 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
489 abbrev_ptr += bytes_read;
98591c73 490
252b5132
RH
491 while (abbrev_name)
492 {
493 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
494 {
dc810e39
AM
495 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
496 amt *= sizeof (struct attr_abbrev);
818a27ac 497 cur_abbrev->attrs = bfd_realloc (cur_abbrev->attrs, amt);
252b5132
RH
498 if (! cur_abbrev->attrs)
499 return 0;
500 }
98591c73 501
d45913a0
DA
502 cur_abbrev->attrs[cur_abbrev->num_attrs].name
503 = (enum dwarf_attribute) abbrev_name;
504 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
505 = (enum dwarf_form) abbrev_form;
252b5132
RH
506 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
507 abbrev_ptr += bytes_read;
508 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
509 abbrev_ptr += bytes_read;
510 }
511
512 hash_number = abbrev_number % ABBREV_HASH_SIZE;
513 cur_abbrev->next = abbrevs[hash_number];
514 abbrevs[hash_number] = cur_abbrev;
515
516 /* Get next abbreviation.
e82ce529 517 Under Irix6 the abbreviations for a compilation unit are not
252b5132
RH
518 always properly terminated with an abbrev number of 0.
519 Exit loop if we encounter an abbreviation which we have
520 already read (which means we are about to read the abbreviations
521 for the next compile unit) or if the end of the abbreviation
522 table is reached. */
523 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
524 >= stash->dwarf_abbrev_size)
525 break;
526 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
527 abbrev_ptr += bytes_read;
528 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
529 break;
530 }
531
532 return abbrevs;
533}
534
cf716c56 535/* Read an attribute value described by an attribute form. */
252b5132
RH
536
537static char *
818a27ac
AM
538read_attribute_value (struct attribute *attr,
539 unsigned form,
540 struct comp_unit *unit,
541 char *info_ptr)
252b5132
RH
542{
543 bfd *abfd = unit->abfd;
544 unsigned int bytes_read;
545 struct dwarf_block *blk;
dc810e39 546 bfd_size_type amt;
252b5132 547
d45913a0 548 attr->form = (enum dwarf_form) form;
98591c73 549
cf716c56 550 switch (form)
252b5132
RH
551 {
552 case DW_FORM_addr:
0cc1cf99 553 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
252b5132 554 case DW_FORM_ref_addr:
482e2e37 555 attr->u.val = read_address (unit, info_ptr);
252b5132
RH
556 info_ptr += unit->addr_size;
557 break;
558 case DW_FORM_block2:
dc810e39 559 amt = sizeof (struct dwarf_block);
818a27ac 560 blk = bfd_alloc (abfd, amt);
252b5132
RH
561 blk->size = read_2_bytes (abfd, info_ptr);
562 info_ptr += 2;
563 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
564 info_ptr += blk->size;
482e2e37 565 attr->u.blk = blk;
252b5132
RH
566 break;
567 case DW_FORM_block4:
dc810e39 568 amt = sizeof (struct dwarf_block);
818a27ac 569 blk = bfd_alloc (abfd, amt);
252b5132
RH
570 blk->size = read_4_bytes (abfd, info_ptr);
571 info_ptr += 4;
572 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
573 info_ptr += blk->size;
482e2e37 574 attr->u.blk = blk;
252b5132
RH
575 break;
576 case DW_FORM_data2:
482e2e37 577 attr->u.val = read_2_bytes (abfd, info_ptr);
252b5132
RH
578 info_ptr += 2;
579 break;
580 case DW_FORM_data4:
482e2e37 581 attr->u.val = read_4_bytes (abfd, info_ptr);
252b5132
RH
582 info_ptr += 4;
583 break;
584 case DW_FORM_data8:
482e2e37 585 attr->u.val = read_8_bytes (abfd, info_ptr);
252b5132
RH
586 info_ptr += 8;
587 break;
588 case DW_FORM_string:
482e2e37 589 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
252b5132
RH
590 info_ptr += bytes_read;
591 break;
d03ba2a1 592 case DW_FORM_strp:
482e2e37 593 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
d03ba2a1
JJ
594 info_ptr += bytes_read;
595 break;
252b5132 596 case DW_FORM_block:
dc810e39 597 amt = sizeof (struct dwarf_block);
818a27ac 598 blk = bfd_alloc (abfd, amt);
252b5132
RH
599 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
600 info_ptr += bytes_read;
601 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
602 info_ptr += blk->size;
482e2e37 603 attr->u.blk = blk;
252b5132
RH
604 break;
605 case DW_FORM_block1:
dc810e39 606 amt = sizeof (struct dwarf_block);
818a27ac 607 blk = bfd_alloc (abfd, amt);
252b5132
RH
608 blk->size = read_1_byte (abfd, info_ptr);
609 info_ptr += 1;
610 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
611 info_ptr += blk->size;
482e2e37 612 attr->u.blk = blk;
252b5132
RH
613 break;
614 case DW_FORM_data1:
482e2e37 615 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
616 info_ptr += 1;
617 break;
618 case DW_FORM_flag:
482e2e37 619 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
620 info_ptr += 1;
621 break;
622 case DW_FORM_sdata:
482e2e37 623 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
624 info_ptr += bytes_read;
625 break;
626 case DW_FORM_udata:
482e2e37 627 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
628 info_ptr += bytes_read;
629 break;
630 case DW_FORM_ref1:
482e2e37 631 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
632 info_ptr += 1;
633 break;
634 case DW_FORM_ref2:
482e2e37 635 attr->u.val = read_2_bytes (abfd, info_ptr);
252b5132
RH
636 info_ptr += 2;
637 break;
638 case DW_FORM_ref4:
482e2e37 639 attr->u.val = read_4_bytes (abfd, info_ptr);
252b5132
RH
640 info_ptr += 4;
641 break;
81edd86d 642 case DW_FORM_ref8:
482e2e37 643 attr->u.val = read_8_bytes (abfd, info_ptr);
81edd86d
MM
644 info_ptr += 8;
645 break;
252b5132 646 case DW_FORM_ref_udata:
482e2e37 647 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
648 info_ptr += bytes_read;
649 break;
252b5132 650 case DW_FORM_indirect:
cf716c56
RH
651 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
652 info_ptr += bytes_read;
653 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
654 break;
252b5132 655 default:
f46c2da6 656 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
cf716c56 657 form);
252b5132
RH
658 bfd_set_error (bfd_error_bad_value);
659 }
660 return info_ptr;
661}
662
cf716c56
RH
663/* Read an attribute described by an abbreviated attribute. */
664
665static char *
818a27ac
AM
666read_attribute (struct attribute *attr,
667 struct attr_abbrev *abbrev,
668 struct comp_unit *unit,
669 char *info_ptr)
cf716c56
RH
670{
671 attr->name = abbrev->name;
672 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
673 return info_ptr;
674}
675
a092b084 676/* Source line information table routines. */
252b5132
RH
677
678#define FILE_ALLOC_CHUNK 5
679#define DIR_ALLOC_CHUNK 5
680
a092b084
NC
681struct line_info
682{
252b5132 683 struct line_info* prev_line;
252b5132
RH
684 bfd_vma address;
685 char* filename;
686 unsigned int line;
687 unsigned int column;
a092b084 688 int end_sequence; /* End of (sequential) code sequence. */
252b5132
RH
689};
690
a092b084
NC
691struct fileinfo
692{
252b5132
RH
693 char *name;
694 unsigned int dir;
695 unsigned int time;
696 unsigned int size;
697};
698
a092b084
NC
699struct line_info_table
700{
252b5132 701 bfd* abfd;
252b5132
RH
702 unsigned int num_files;
703 unsigned int num_dirs;
252b5132
RH
704 char* comp_dir;
705 char** dirs;
706 struct fileinfo* files;
e82ce529
AM
707 struct line_info* last_line; /* largest VMA */
708 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
252b5132
RH
709};
710
1ee24f27
DJ
711struct funcinfo
712{
713 struct funcinfo *prev_func;
714 char* name;
715 bfd_vma low;
716 bfd_vma high;
717};
718
af3ef9fe
NC
719/* Adds a new entry to the line_info list in the line_info_table, ensuring
720 that the list is sorted. Note that the line_info list is sorted from
721 highest to lowest VMA (with possible duplicates); that is,
722 line_info->prev_line always accesses an equal or smaller VMA. */
723
98591c73 724static void
818a27ac
AM
725add_line_info (struct line_info_table *table,
726 bfd_vma address,
727 char *filename,
728 unsigned int line,
729 unsigned int column,
730 int end_sequence)
252b5132 731{
dc810e39 732 bfd_size_type amt = sizeof (struct line_info);
818a27ac 733 struct line_info* info = bfd_alloc (table->abfd, amt);
252b5132 734
e82ce529
AM
735 /* Find the correct location for 'info'. Normally we will receive
736 new line_info data 1) in order and 2) with increasing VMAs.
737 However some compilers break the rules (cf. decode_line_info) and
738 so we include some heuristics for quickly finding the correct
739 location for 'info'. In particular, these heuristics optimize for
740 the common case in which the VMA sequence that we receive is a
741 list of locally sorted VMAs such as
742 p...z a...j (where a < j < p < z)
252b5132 743
e82ce529
AM
744 Note: table->lcl_head is used to head an *actual* or *possible*
745 sequence within the list (such as a...j) that is not directly
746 headed by table->last_line
747
748 Note: we may receive duplicate entries from 'decode_line_info'. */
749
750 while (1)
751 if (!table->last_line
752 || address >= table->last_line->address)
753 {
754 /* Normal case: add 'info' to the beginning of the list */
755 info->prev_line = table->last_line;
756 table->last_line = info;
757
758 /* lcl_head: initialize to head a *possible* sequence at the end. */
759 if (!table->lcl_head)
760 table->lcl_head = info;
761 break;
762 }
763 else if (!table->lcl_head->prev_line
764 && table->lcl_head->address > address)
765 {
766 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
767 list and 2) the head of 'info'. */
768 info->prev_line = NULL;
769 table->lcl_head->prev_line = info;
770 break;
771 }
772 else if (table->lcl_head->prev_line
773 && table->lcl_head->address > address
774 && address >= table->lcl_head->prev_line->address)
775 {
776 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
777 list and 2) the head of 'info'. */
778 info->prev_line = table->lcl_head->prev_line;
779 table->lcl_head->prev_line = info;
780 break;
781 }
782 else
783 {
784 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
785 heads for 'info'. Reset 'lcl_head' and repeat. */
786 struct line_info* li2 = table->last_line; /* always non-NULL */
787 struct line_info* li1 = li2->prev_line;
788
789 while (li1)
790 {
791 if (li2->address > address && address >= li1->address)
792 break;
793
794 li2 = li1; /* always non-NULL */
795 li1 = li1->prev_line;
796 }
797 table->lcl_head = li2;
798 }
799
800 /* Set member data of 'info'. */
252b5132 801 info->address = address;
252b5132
RH
802 info->line = line;
803 info->column = column;
159002ff 804 info->end_sequence = end_sequence;
d63fd5d1 805
eb61d2d6 806 if (filename && filename[0])
d63fd5d1 807 {
eb61d2d6 808 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
d63fd5d1
NC
809 if (info->filename)
810 strcpy (info->filename, filename);
811 }
812 else
813 info->filename = NULL;
252b5132
RH
814}
815
5ed6aba4 816/* Extract a fully qualified filename from a line info table.
af3ef9fe
NC
817 The returned string has been malloc'ed and it is the caller's
818 responsibility to free it. */
5ed6aba4 819
a092b084 820static char *
818a27ac 821concat_filename (struct line_info_table *table, unsigned int file)
252b5132 822{
159002ff
RH
823 char* filename;
824
825 if (file - 1 >= table->num_files)
826 {
dcdea4f4
AM
827 (*_bfd_error_handler)
828 (_("Dwarf Error: mangled line number section (bad file number)."));
af3ef9fe 829 return strdup ("<unknown>");
159002ff
RH
830 }
831
832 filename = table->files[file - 1].name;
5ed6aba4 833
af3ef9fe 834 if (! IS_ABSOLUTE_PATH (filename))
252b5132
RH
835 {
836 char* dirname = (table->files[file - 1].dir
837 ? table->dirs[table->files[file - 1].dir - 1]
838 : table->comp_dir);
0dafd5f6 839
af3ef9fe
NC
840 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
841 The best we can do is return the filename part. */
842 if (dirname != NULL)
843 {
844 unsigned int len = strlen (dirname) + strlen (filename) + 2;
845 char * name;
846
847 name = bfd_malloc (len);
848 if (name)
849 sprintf (name, "%s/%s", dirname, filename);
850 return name;
851 }
252b5132 852 }
af3ef9fe
NC
853
854 return strdup (filename);
252b5132
RH
855}
856
f623be2b 857static void
818a27ac 858arange_add (struct comp_unit *unit, bfd_vma low_pc, bfd_vma high_pc)
f623be2b
RH
859{
860 struct arange *arange;
861
a092b084 862 /* First see if we can cheaply extend an existing range. */
f623be2b 863 arange = &unit->arange;
98591c73 864
f623be2b
RH
865 do
866 {
867 if (low_pc == arange->high)
868 {
869 arange->high = high_pc;
870 return;
871 }
872 if (high_pc == arange->low)
873 {
874 arange->low = low_pc;
875 return;
876 }
877 arange = arange->next;
878 }
879 while (arange);
880
881 if (unit->arange.high == 0)
882 {
a092b084 883 /* This is the first address range: store it in unit->arange. */
f623be2b
RH
884 unit->arange.next = 0;
885 unit->arange.low = low_pc;
886 unit->arange.high = high_pc;
887 return;
888 }
889
a092b084 890 /* Need to allocate a new arange and insert it into the arange list. */
818a27ac 891 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
f623be2b
RH
892 arange->low = low_pc;
893 arange->high = high_pc;
894
895 arange->next = unit->arange.next;
896 unit->arange.next = arange;
897}
898
a092b084 899/* Decode the line number information for UNIT. */
252b5132
RH
900
901static struct line_info_table*
818a27ac 902decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
252b5132
RH
903{
904 bfd *abfd = unit->abfd;
252b5132 905 struct line_info_table* table;
252b5132
RH
906 char *line_ptr;
907 char *line_end;
908 struct line_head lh;
d03ba2a1 909 unsigned int i, bytes_read, offset_size;
252b5132
RH
910 char *cur_file, *cur_dir;
911 unsigned char op_code, extended_op, adj_opcode;
dc810e39 912 bfd_size_type amt;
252b5132 913
69dd2e2d 914 if (! stash->dwarf_line_buffer)
252b5132
RH
915 {
916 asection *msec;
252b5132
RH
917
918 msec = bfd_get_section_by_name (abfd, ".debug_line");
919 if (! msec)
920 {
921 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
922 bfd_set_error (bfd_error_bad_value);
923 return 0;
924 }
98591c73 925
eea6121a 926 stash->dwarf_line_size = msec->size;
6e84a906
DJ
927 stash->dwarf_line_buffer
928 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
929 stash->syms);
f623be2b 930 if (! stash->dwarf_line_buffer)
252b5132 931 return 0;
252b5132
RH
932 }
933
6e84a906
DJ
934 /* It is possible to get a bad value for the line_offset. Validate
935 it here so that we won't get a segfault below. */
ccdb16fc
JW
936 if (unit->line_offset >= stash->dwarf_line_size)
937 {
f46c2da6 938 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
ccdb16fc
JW
939 unit->line_offset, stash->dwarf_line_size);
940 bfd_set_error (bfd_error_bad_value);
941 return 0;
942 }
943
dc810e39 944 amt = sizeof (struct line_info_table);
818a27ac 945 table = bfd_alloc (abfd, amt);
252b5132
RH
946 table->abfd = abfd;
947 table->comp_dir = unit->comp_dir;
948
949 table->num_files = 0;
950 table->files = NULL;
951
952 table->num_dirs = 0;
953 table->dirs = NULL;
954
159002ff
RH
955 table->files = NULL;
956 table->last_line = NULL;
e82ce529 957 table->lcl_head = NULL;
159002ff 958
69dd2e2d 959 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
252b5132 960
a092b084 961 /* Read in the prologue. */
91a4d569
AM
962 lh.total_length = read_4_bytes (abfd, line_ptr);
963 line_ptr += 4;
964 offset_size = 4;
965 if (lh.total_length == 0xffffffff)
dae2dd0d 966 {
dae2dd0d
NC
967 lh.total_length = read_8_bytes (abfd, line_ptr);
968 line_ptr += 8;
969 offset_size = 8;
970 }
91a4d569 971 else if (lh.total_length == 0 && unit->addr_size == 8)
d03ba2a1 972 {
91a4d569
AM
973 /* Handle (non-standard) 64-bit DWARF2 formats. */
974 lh.total_length = read_4_bytes (abfd, line_ptr);
975 line_ptr += 4;
d03ba2a1
JJ
976 offset_size = 8;
977 }
252b5132
RH
978 line_end = line_ptr + lh.total_length;
979 lh.version = read_2_bytes (abfd, line_ptr);
980 line_ptr += 2;
d03ba2a1
JJ
981 if (offset_size == 4)
982 lh.prologue_length = read_4_bytes (abfd, line_ptr);
983 else
984 lh.prologue_length = read_8_bytes (abfd, line_ptr);
985 line_ptr += offset_size;
252b5132
RH
986 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
987 line_ptr += 1;
988 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
989 line_ptr += 1;
990 lh.line_base = read_1_signed_byte (abfd, line_ptr);
991 line_ptr += 1;
992 lh.line_range = read_1_byte (abfd, line_ptr);
993 line_ptr += 1;
994 lh.opcode_base = read_1_byte (abfd, line_ptr);
995 line_ptr += 1;
dc810e39 996 amt = lh.opcode_base * sizeof (unsigned char);
818a27ac 997 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
252b5132
RH
998
999 lh.standard_opcode_lengths[0] = 1;
98591c73 1000
252b5132
RH
1001 for (i = 1; i < lh.opcode_base; ++i)
1002 {
1003 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1004 line_ptr += 1;
1005 }
1006
a092b084 1007 /* Read directory table. */
252b5132
RH
1008 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1009 {
1010 line_ptr += bytes_read;
98591c73 1011
252b5132
RH
1012 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1013 {
dc810e39
AM
1014 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1015 amt *= sizeof (char *);
818a27ac 1016 table->dirs = bfd_realloc (table->dirs, amt);
252b5132
RH
1017 if (! table->dirs)
1018 return 0;
1019 }
98591c73 1020
252b5132
RH
1021 table->dirs[table->num_dirs++] = cur_dir;
1022 }
98591c73 1023
252b5132
RH
1024 line_ptr += bytes_read;
1025
a092b084 1026 /* Read file name table. */
252b5132
RH
1027 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1028 {
1029 line_ptr += bytes_read;
98591c73 1030
252b5132
RH
1031 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1032 {
dc810e39
AM
1033 amt = table->num_files + FILE_ALLOC_CHUNK;
1034 amt *= sizeof (struct fileinfo);
818a27ac 1035 table->files = bfd_realloc (table->files, amt);
252b5132
RH
1036 if (! table->files)
1037 return 0;
1038 }
98591c73 1039
252b5132
RH
1040 table->files[table->num_files].name = cur_file;
1041 table->files[table->num_files].dir =
1042 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1043 line_ptr += bytes_read;
1044 table->files[table->num_files].time =
1045 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1046 line_ptr += bytes_read;
1047 table->files[table->num_files].size =
1048 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1049 line_ptr += bytes_read;
1050 table->num_files++;
1051 }
98591c73 1052
252b5132
RH
1053 line_ptr += bytes_read;
1054
1055 /* Read the statement sequences until there's nothing left. */
1056 while (line_ptr < line_end)
1057 {
a092b084 1058 /* State machine registers. */
252b5132 1059 bfd_vma address = 0;
8bfd78b3 1060 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
252b5132
RH
1061 unsigned int line = 1;
1062 unsigned int column = 0;
1063 int is_stmt = lh.default_is_stmt;
1064 int basic_block = 0;
e2f6d277
NC
1065 int end_sequence = 0;
1066 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
e82ce529
AM
1067 compilers generate address sequences that are wildly out of
1068 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1069 for ia64-Linux). Thus, to determine the low and high
1070 address, we must compare on every DW_LNS_copy, etc. */
e2f6d277
NC
1071 bfd_vma low_pc = 0;
1072 bfd_vma high_pc = 0;
252b5132 1073
a092b084 1074 /* Decode the table. */
252b5132
RH
1075 while (! end_sequence)
1076 {
1077 op_code = read_1_byte (abfd, line_ptr);
1078 line_ptr += 1;
98591c73 1079
1a509dcc 1080 if (op_code >= lh.opcode_base)
e2f6d277
NC
1081 {
1082 /* Special operand. */
1a509dcc
GK
1083 adj_opcode = op_code - lh.opcode_base;
1084 address += (adj_opcode / lh.line_range)
1085 * lh.minimum_instruction_length;
1086 line += lh.line_base + (adj_opcode % lh.line_range);
1087 /* Append row to matrix using current values. */
1088 add_line_info (table, address, filename, line, column, 0);
1089 basic_block = 1;
e2f6d277
NC
1090 if (low_pc == 0 || address < low_pc)
1091 low_pc = address;
1092 if (address > high_pc)
1093 high_pc = address;
1a509dcc
GK
1094 }
1095 else switch (op_code)
252b5132
RH
1096 {
1097 case DW_LNS_extended_op:
e2f6d277
NC
1098 /* Ignore length. */
1099 line_ptr += 1;
252b5132
RH
1100 extended_op = read_1_byte (abfd, line_ptr);
1101 line_ptr += 1;
e2f6d277 1102
252b5132
RH
1103 switch (extended_op)
1104 {
1105 case DW_LNE_end_sequence:
1106 end_sequence = 1;
f623be2b
RH
1107 add_line_info (table, address, filename, line, column,
1108 end_sequence);
e2f6d277
NC
1109 if (low_pc == 0 || address < low_pc)
1110 low_pc = address;
1111 if (address > high_pc)
1112 high_pc = address;
a2ce5bdc 1113 arange_add (unit, low_pc, high_pc);
252b5132
RH
1114 break;
1115 case DW_LNE_set_address:
1116 address = read_address (unit, line_ptr);
1117 line_ptr += unit->addr_size;
1118 break;
1119 case DW_LNE_define_file:
1120 cur_file = read_string (abfd, line_ptr, &bytes_read);
1121 line_ptr += bytes_read;
1122 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1123 {
dc810e39
AM
1124 amt = table->num_files + FILE_ALLOC_CHUNK;
1125 amt *= sizeof (struct fileinfo);
818a27ac 1126 table->files = bfd_realloc (table->files, amt);
252b5132
RH
1127 if (! table->files)
1128 return 0;
1129 }
1130 table->files[table->num_files].name = cur_file;
1131 table->files[table->num_files].dir =
1132 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1133 line_ptr += bytes_read;
1134 table->files[table->num_files].time =
1135 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1136 line_ptr += bytes_read;
1137 table->files[table->num_files].size =
1138 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1139 line_ptr += bytes_read;
1140 table->num_files++;
1141 break;
1142 default:
1143 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1144 bfd_set_error (bfd_error_bad_value);
1145 return 0;
1146 }
1147 break;
1148 case DW_LNS_copy:
159002ff 1149 add_line_info (table, address, filename, line, column, 0);
252b5132 1150 basic_block = 0;
e2f6d277
NC
1151 if (low_pc == 0 || address < low_pc)
1152 low_pc = address;
1153 if (address > high_pc)
1154 high_pc = address;
252b5132
RH
1155 break;
1156 case DW_LNS_advance_pc:
1157 address += lh.minimum_instruction_length
1158 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1159 line_ptr += bytes_read;
1160 break;
1161 case DW_LNS_advance_line:
1162 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1163 line_ptr += bytes_read;
1164 break;
1165 case DW_LNS_set_file:
1166 {
1167 unsigned int file;
1168
e2f6d277
NC
1169 /* The file and directory tables are 0
1170 based, the references are 1 based. */
252b5132
RH
1171 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1172 line_ptr += bytes_read;
af3ef9fe
NC
1173 if (filename)
1174 free (filename);
252b5132
RH
1175 filename = concat_filename (table, file);
1176 break;
1177 }
1178 case DW_LNS_set_column:
1179 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1180 line_ptr += bytes_read;
1181 break;
1182 case DW_LNS_negate_stmt:
1183 is_stmt = (!is_stmt);
1184 break;
1185 case DW_LNS_set_basic_block:
1186 basic_block = 1;
1187 break;
1188 case DW_LNS_const_add_pc:
159002ff
RH
1189 address += lh.minimum_instruction_length
1190 * ((255 - lh.opcode_base) / lh.line_range);
252b5132
RH
1191 break;
1192 case DW_LNS_fixed_advance_pc:
1193 address += read_2_bytes (abfd, line_ptr);
1194 line_ptr += 2;
1195 break;
1a509dcc 1196 default:
e2f6d277 1197 {
1a509dcc 1198 int i;
5ed6aba4 1199
e2f6d277 1200 /* Unknown standard opcode, ignore it. */
1a509dcc
GK
1201 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1202 {
1203 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1204 line_ptr += bytes_read;
1205 }
1206 }
252b5132
RH
1207 }
1208 }
5ed6aba4 1209
af3ef9fe
NC
1210 if (filename)
1211 free (filename);
252b5132
RH
1212 }
1213
1214 return table;
1215}
1216
b34976b6
AM
1217/* If ADDR is within TABLE set the output parameters and return TRUE,
1218 otherwise return FALSE. The output parameters, FILENAME_PTR and
a092b084 1219 LINENUMBER_PTR, are pointers to the objects to be filled in. */
252b5132 1220
b34976b6 1221static bfd_boolean
818a27ac
AM
1222lookup_address_in_line_info_table (struct line_info_table *table,
1223 bfd_vma addr,
1224 struct funcinfo *function,
1225 const char **filename_ptr,
1226 unsigned int *linenumber_ptr)
252b5132 1227{
e82ce529 1228 /* Note: table->last_line should be a descendingly sorted list. */
159002ff 1229 struct line_info* next_line = table->last_line;
e82ce529
AM
1230 struct line_info* each_line = NULL;
1231 *filename_ptr = NULL;
98591c73 1232
159002ff 1233 if (!next_line)
b34976b6 1234 return FALSE;
159002ff
RH
1235
1236 each_line = next_line->prev_line;
1237
e82ce529
AM
1238 /* Check for large addresses */
1239 if (addr > next_line->address)
1240 each_line = NULL; /* ensure we skip over the normal case */
1241
1242 /* Normal case: search the list; save */
159002ff 1243 while (each_line && next_line)
252b5132 1244 {
e82ce529
AM
1245 /* If we have an address match, save this info. This allows us
1246 to return as good as results as possible for strange debugging
1247 info. */
b34976b6 1248 bfd_boolean addr_match = FALSE;
e82ce529 1249 if (each_line->address <= addr && addr <= next_line->address)
252b5132 1250 {
b34976b6 1251 addr_match = TRUE;
e82ce529 1252
1ee24f27
DJ
1253 /* If this line appears to span functions, and addr is in the
1254 later function, return the first line of that function instead
1255 of the last line of the earlier one. This check is for GCC
1256 2.95, which emits the first line number for a function late. */
1257 if (function != NULL
1258 && each_line->address < function->low
1259 && next_line->address > function->low)
1260 {
1261 *filename_ptr = next_line->filename;
1262 *linenumber_ptr = next_line->line;
1263 }
1264 else
1265 {
1266 *filename_ptr = each_line->filename;
1267 *linenumber_ptr = each_line->line;
1268 }
252b5132 1269 }
e82ce529
AM
1270
1271 if (addr_match && !each_line->end_sequence)
b34976b6 1272 return TRUE; /* we have definitely found what we want */
e82ce529 1273
159002ff
RH
1274 next_line = each_line;
1275 each_line = each_line->prev_line;
252b5132 1276 }
98591c73 1277
e82ce529
AM
1278 /* At this point each_line is NULL but next_line is not. If we found
1279 a candidate end-of-sequence point in the loop above, we can return
1280 that (compatibility with a bug in the Intel compiler); otherwise,
1281 assuming that we found the containing function for this address in
1282 this compilation unit, return the first line we have a number for
1283 (compatibility with GCC 2.95). */
1284 if (*filename_ptr == NULL && function != NULL)
1ee24f27
DJ
1285 {
1286 *filename_ptr = next_line->filename;
1287 *linenumber_ptr = next_line->line;
b34976b6 1288 return TRUE;
1ee24f27
DJ
1289 }
1290
b34976b6 1291 return FALSE;
252b5132 1292}
98591c73 1293
a092b084 1294/* Function table functions. */
252b5132 1295
b34976b6 1296/* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. */
252b5132 1297
b34976b6 1298static bfd_boolean
818a27ac
AM
1299lookup_address_in_function_table (struct funcinfo *table,
1300 bfd_vma addr,
1301 struct funcinfo **function_ptr,
1302 const char **functionname_ptr)
252b5132
RH
1303{
1304 struct funcinfo* each_func;
1305
1306 for (each_func = table;
1307 each_func;
1308 each_func = each_func->prev_func)
1309 {
1310 if (addr >= each_func->low && addr < each_func->high)
1311 {
1312 *functionname_ptr = each_func->name;
1ee24f27 1313 *function_ptr = each_func;
b34976b6 1314 return TRUE;
252b5132
RH
1315 }
1316 }
98591c73 1317
b34976b6 1318 return FALSE;
252b5132
RH
1319}
1320
a092b084 1321/* DWARF2 Compilation unit functions. */
252b5132
RH
1322
1323/* Scan over each die in a comp. unit looking for functions to add
a092b084 1324 to the function table. */
252b5132 1325
b34976b6 1326static bfd_boolean
818a27ac 1327scan_unit_for_functions (struct comp_unit *unit)
252b5132
RH
1328{
1329 bfd *abfd = unit->abfd;
1330 char *info_ptr = unit->first_child_die_ptr;
1331 int nesting_level = 1;
1332
1333 while (nesting_level)
1334 {
1335 unsigned int abbrev_number, bytes_read, i;
1336 struct abbrev_info *abbrev;
1337 struct attribute attr;
1338 struct funcinfo *func;
1339 char* name = 0;
1340
1341 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1342 info_ptr += bytes_read;
1343
1344 if (! abbrev_number)
1345 {
1346 nesting_level--;
1347 continue;
1348 }
98591c73 1349
252b5132
RH
1350 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1351 if (! abbrev)
1352 {
f46c2da6 1353 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
252b5132
RH
1354 abbrev_number);
1355 bfd_set_error (bfd_error_bad_value);
b34976b6 1356 return FALSE;
252b5132 1357 }
98591c73 1358
252b5132
RH
1359 if (abbrev->tag == DW_TAG_subprogram)
1360 {
dc810e39 1361 bfd_size_type amt = sizeof (struct funcinfo);
818a27ac 1362 func = bfd_zalloc (abfd, amt);
252b5132
RH
1363 func->prev_func = unit->function_table;
1364 unit->function_table = func;
1365 }
1366 else
1367 func = NULL;
98591c73 1368
252b5132
RH
1369 for (i = 0; i < abbrev->num_attrs; ++i)
1370 {
1371 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
98591c73 1372
252b5132
RH
1373 if (func)
1374 {
1375 switch (attr.name)
1376 {
1377 case DW_AT_name:
98591c73 1378
482e2e37 1379 name = attr.u.str;
252b5132
RH
1380
1381 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1382 if (func->name == NULL)
482e2e37 1383 func->name = attr.u.str;
252b5132 1384 break;
98591c73 1385
252b5132 1386 case DW_AT_MIPS_linkage_name:
482e2e37 1387 func->name = attr.u.str;
252b5132
RH
1388 break;
1389
1390 case DW_AT_low_pc:
482e2e37 1391 func->low = attr.u.val;
252b5132
RH
1392 break;
1393
1394 case DW_AT_high_pc:
482e2e37 1395 func->high = attr.u.val;
252b5132
RH
1396 break;
1397
1398 default:
1399 break;
1400 }
1401 }
1402 else
1403 {
1404 switch (attr.name)
1405 {
1406 case DW_AT_name:
482e2e37 1407 name = attr.u.str;
252b5132 1408 break;
98591c73 1409
252b5132
RH
1410 default:
1411 break;
1412 }
1413 }
1414 }
1415
1416 if (abbrev->has_children)
1417 nesting_level++;
1418 }
1419
b34976b6 1420 return TRUE;
252b5132
RH
1421}
1422
5e38c3b8
MM
1423/* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1424 includes the compilation unit header that proceeds the DIE's, but
5c4491d3 1425 does not include the length field that precedes each compilation
5e38c3b8 1426 unit header. END_PTR points one past the end of this comp unit.
d03ba2a1 1427 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
252b5132
RH
1428
1429 This routine does not read the whole compilation unit; only enough
1430 to get to the line number information for the compilation unit. */
1431
1432static struct comp_unit *
818a27ac
AM
1433parse_comp_unit (bfd *abfd,
1434 struct dwarf2_debug *stash,
1435 bfd_vma unit_length,
1436 unsigned int offset_size)
252b5132
RH
1437{
1438 struct comp_unit* unit;
f46c2da6 1439 unsigned int version;
8ce8c090 1440 bfd_uint64_t abbrev_offset = 0;
f46c2da6 1441 unsigned int addr_size;
252b5132 1442 struct abbrev_info** abbrevs;
252b5132
RH
1443 unsigned int abbrev_number, bytes_read, i;
1444 struct abbrev_info *abbrev;
1445 struct attribute attr;
51db3708
NC
1446 char *info_ptr = stash->info_ptr;
1447 char *end_ptr = info_ptr + unit_length;
dc810e39 1448 bfd_size_type amt;
3fde5a36 1449
252b5132
RH
1450 version = read_2_bytes (abfd, info_ptr);
1451 info_ptr += 2;
d03ba2a1
JJ
1452 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1453 if (offset_size == 4)
5e38c3b8 1454 abbrev_offset = read_4_bytes (abfd, info_ptr);
d03ba2a1 1455 else
5e38c3b8 1456 abbrev_offset = read_8_bytes (abfd, info_ptr);
d03ba2a1 1457 info_ptr += offset_size;
252b5132
RH
1458 addr_size = read_1_byte (abfd, info_ptr);
1459 info_ptr += 1;
1460
1461 if (version != 2)
1462 {
f46c2da6 1463 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
252b5132
RH
1464 bfd_set_error (bfd_error_bad_value);
1465 return 0;
1466 }
1467
1468 if (addr_size > sizeof (bfd_vma))
1469 {
1470 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1471 addr_size,
f46c2da6 1472 (unsigned int) sizeof (bfd_vma));
252b5132
RH
1473 bfd_set_error (bfd_error_bad_value);
1474 return 0;
1475 }
1476
ecb651f0 1477 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
252b5132 1478 {
f5a3e38a 1479 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
252b5132
RH
1480 bfd_set_error (bfd_error_bad_value);
1481 return 0;
1482 }
1483
a092b084 1484 /* Read the abbrevs for this compilation unit into a table. */
51db3708 1485 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
252b5132
RH
1486 if (! abbrevs)
1487 return 0;
1488
1489 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1490 info_ptr += bytes_read;
1491 if (! abbrev_number)
1492 {
f46c2da6 1493 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
252b5132
RH
1494 abbrev_number);
1495 bfd_set_error (bfd_error_bad_value);
1496 return 0;
1497 }
1498
1499 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1500 if (! abbrev)
1501 {
f46c2da6 1502 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
252b5132
RH
1503 abbrev_number);
1504 bfd_set_error (bfd_error_bad_value);
1505 return 0;
1506 }
98591c73 1507
dc810e39 1508 amt = sizeof (struct comp_unit);
818a27ac 1509 unit = bfd_zalloc (abfd, amt);
252b5132 1510 unit->abfd = abfd;
98591c73 1511 unit->addr_size = addr_size;
d03ba2a1 1512 unit->offset_size = offset_size;
252b5132
RH
1513 unit->abbrevs = abbrevs;
1514 unit->end_ptr = end_ptr;
d03ba2a1 1515 unit->stash = stash;
252b5132
RH
1516
1517 for (i = 0; i < abbrev->num_attrs; ++i)
1518 {
1519 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1520
1521 /* Store the data if it is of an attribute we want to keep in a
1522 partial symbol table. */
1523 switch (attr.name)
1524 {
1525 case DW_AT_stmt_list:
1526 unit->stmtlist = 1;
482e2e37 1527 unit->line_offset = attr.u.val;
252b5132
RH
1528 break;
1529
1530 case DW_AT_name:
482e2e37 1531 unit->name = attr.u.str;
252b5132
RH
1532 break;
1533
1534 case DW_AT_low_pc:
482e2e37 1535 unit->arange.low = attr.u.val;
252b5132
RH
1536 break;
1537
1538 case DW_AT_high_pc:
482e2e37 1539 unit->arange.high = attr.u.val;
252b5132
RH
1540 break;
1541
1542 case DW_AT_comp_dir:
1543 {
482e2e37 1544 char* comp_dir = attr.u.str;
252b5132
RH
1545 if (comp_dir)
1546 {
1547 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1548 directory, get rid of it. */
818a27ac 1549 char *cp = strchr (comp_dir, ':');
252b5132
RH
1550
1551 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1552 comp_dir = cp + 1;
1553 }
1554 unit->comp_dir = comp_dir;
1555 break;
1556 }
1557
1558 default:
1559 break;
1560 }
1561 }
1562
1563 unit->first_child_die_ptr = info_ptr;
1564 return unit;
1565}
1566
b34976b6 1567/* Return TRUE if UNIT contains the address given by ADDR. */
252b5132 1568
b34976b6 1569static bfd_boolean
818a27ac 1570comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
252b5132 1571{
f623be2b
RH
1572 struct arange *arange;
1573
1574 if (unit->error)
b34976b6 1575 return FALSE;
f623be2b
RH
1576
1577 arange = &unit->arange;
1578 do
1579 {
1580 if (addr >= arange->low && addr < arange->high)
b34976b6 1581 return TRUE;
f623be2b
RH
1582 arange = arange->next;
1583 }
1584 while (arange);
98591c73 1585
b34976b6 1586 return FALSE;
252b5132
RH
1587}
1588
252b5132
RH
1589/* If UNIT contains ADDR, set the output parameters to the values for
1590 the line containing ADDR. The output parameters, FILENAME_PTR,
1591 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
98591c73 1592 to be filled in.
252b5132 1593
ab3acfbe 1594 Return TRUE if UNIT contains ADDR, and no errors were encountered;
b34976b6 1595 FALSE otherwise. */
252b5132 1596
b34976b6 1597static bfd_boolean
818a27ac
AM
1598comp_unit_find_nearest_line (struct comp_unit *unit,
1599 bfd_vma addr,
1600 const char **filename_ptr,
1601 const char **functionname_ptr,
1602 unsigned int *linenumber_ptr,
1603 struct dwarf2_debug *stash)
252b5132 1604{
b34976b6
AM
1605 bfd_boolean line_p;
1606 bfd_boolean func_p;
1ee24f27 1607 struct funcinfo *function;
98591c73 1608
252b5132 1609 if (unit->error)
b34976b6 1610 return FALSE;
252b5132
RH
1611
1612 if (! unit->line_table)
1613 {
1614 if (! unit->stmtlist)
1615 {
1616 unit->error = 1;
b34976b6 1617 return FALSE;
252b5132 1618 }
98591c73 1619
51db3708 1620 unit->line_table = decode_line_info (unit, stash);
252b5132
RH
1621
1622 if (! unit->line_table)
1623 {
1624 unit->error = 1;
b34976b6 1625 return FALSE;
252b5132 1626 }
98591c73 1627
3f5864e1 1628 if (unit->first_child_die_ptr < unit->end_ptr
e82ce529 1629 && ! scan_unit_for_functions (unit))
252b5132
RH
1630 {
1631 unit->error = 1;
b34976b6 1632 return FALSE;
252b5132
RH
1633 }
1634 }
1635
1ee24f27 1636 function = NULL;
e2f6d277
NC
1637 func_p = lookup_address_in_function_table (unit->function_table, addr,
1638 &function, functionname_ptr);
1639 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1640 function, filename_ptr,
252b5132 1641 linenumber_ptr);
b34976b6 1642 return line_p || func_p;
252b5132
RH
1643}
1644
e2f6d277
NC
1645/* Locate a section in a BFD containing debugging info. The search starts
1646 from the section after AFTER_SEC, or from the first section in the BFD if
1647 AFTER_SEC is NULL. The search works by examining the names of the
1648 sections. There are two permissiable names. The first is .debug_info.
1649 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1650 This is a variation on the .debug_info section which has a checksum
1651 describing the contents appended onto the name. This allows the linker to
1652 identify and discard duplicate debugging sections for different
1653 compilation units. */
a092b084
NC
1654#define DWARF2_DEBUG_INFO ".debug_info"
1655#define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1656
1657static asection *
818a27ac 1658find_debug_info (bfd *abfd, asection *after_sec)
a092b084
NC
1659{
1660 asection * msec;
1661
1662 if (after_sec)
1663 msec = after_sec->next;
1664 else
1665 msec = abfd->sections;
1666
1667 while (msec)
1668 {
1669 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1670 return msec;
1671
1672 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1673 return msec;
1674
1675 msec = msec->next;
1676 }
1677
1678 return NULL;
1679}
1680
cd917290 1681/* The DWARF2 version of find_nearest_line. Return TRUE if the line
5e38c3b8
MM
1682 is found without error. ADDR_SIZE is the number of bytes in the
1683 initial .debug_info length field and in the abbreviation offset.
1684 You may use zero to indicate that the default value should be
1685 used. */
252b5132 1686
b34976b6 1687bfd_boolean
818a27ac
AM
1688_bfd_dwarf2_find_nearest_line (bfd *abfd,
1689 asection *section,
1690 asymbol **symbols,
1691 bfd_vma offset,
1692 const char **filename_ptr,
1693 const char **functionname_ptr,
1694 unsigned int *linenumber_ptr,
1695 unsigned int addr_size,
1696 void **pinfo)
252b5132
RH
1697{
1698 /* Read each compilation unit from the section .debug_info, and check
1699 to see if it contains the address we are searching for. If yes,
1700 lookup the address, and return the line number info. If no, go
98591c73 1701 on to the next compilation unit.
252b5132
RH
1702
1703 We keep a list of all the previously read compilation units, and
98591c73 1704 a pointer to the next un-read compilation unit. Check the
a092b084 1705 previously read units before reading more. */
1ba54ee0 1706 struct dwarf2_debug *stash;
252b5132 1707
a092b084 1708 /* What address are we looking for? */
1ba54ee0 1709 bfd_vma addr;
252b5132
RH
1710
1711 struct comp_unit* each;
98591c73 1712
1ba54ee0
AM
1713 stash = *pinfo;
1714 addr = offset;
1715 if (section->output_section)
1716 addr += section->output_section->vma + section->output_offset;
1717 else
1718 addr += section->vma;
252b5132
RH
1719 *filename_ptr = NULL;
1720 *functionname_ptr = NULL;
1721 *linenumber_ptr = 0;
1722
5e38c3b8
MM
1723 /* The DWARF2 spec says that the initial length field, and the
1724 offset of the abbreviation table, should both be 4-byte values.
1725 However, some compilers do things differently. */
1726 if (addr_size == 0)
1727 addr_size = 4;
1efe4b10 1728 BFD_ASSERT (addr_size == 4 || addr_size == 8);
98591c73 1729
252b5132
RH
1730 if (! stash)
1731 {
dc810e39 1732 bfd_size_type total_size;
252b5132 1733 asection *msec;
dc810e39 1734 bfd_size_type amt = sizeof (struct dwarf2_debug);
a092b084 1735
818a27ac 1736 stash = bfd_zalloc (abfd, amt);
252b5132 1737 if (! stash)
b34976b6 1738 return FALSE;
252b5132 1739
818a27ac 1740 *pinfo = stash;
3fde5a36 1741
a092b084
NC
1742 msec = find_debug_info (abfd, NULL);
1743 if (! msec)
1744 /* No dwarf2 info. Note that at this point the stash
1745 has been allocated, but contains zeros, this lets
1746 future calls to this function fail quicker. */
b34976b6 1747 return FALSE;
a092b084
NC
1748
1749 /* There can be more than one DWARF2 info section in a BFD these days.
e82ce529 1750 Read them all in and produce one large stash. We do this in two
a092b084
NC
1751 passes - in the first pass we just accumulate the section sizes.
1752 In the second pass we read in the section's contents. The allows
1753 us to avoid reallocing the data as we add sections to the stash. */
1754 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
eea6121a 1755 total_size += msec->size;
98591c73 1756
818a27ac 1757 stash->info_ptr = bfd_alloc (abfd, total_size);
a092b084 1758 if (stash->info_ptr == NULL)
b34976b6 1759 return FALSE;
252b5132 1760
a092b084
NC
1761 stash->info_ptr_end = stash->info_ptr;
1762
1763 for (msec = find_debug_info (abfd, NULL);
1764 msec;
1765 msec = find_debug_info (abfd, msec))
252b5132 1766 {
dc810e39
AM
1767 bfd_size_type size;
1768 bfd_size_type start;
a092b084 1769
eea6121a 1770 size = msec->size;
a092b084
NC
1771 if (size == 0)
1772 continue;
1773
1774 start = stash->info_ptr_end - stash->info_ptr;
1775
6e84a906
DJ
1776 if ((bfd_simple_get_relocated_section_contents
1777 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
a092b084
NC
1778 continue;
1779
1780 stash->info_ptr_end = stash->info_ptr + start + size;
252b5132
RH
1781 }
1782
f2363ce5
AO
1783 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1784
1785 stash->sec = find_debug_info (abfd, NULL);
1786 stash->sec_info_ptr = stash->info_ptr;
1787 stash->syms = symbols;
252b5132 1788 }
a092b084 1789
98591c73 1790 /* A null info_ptr indicates that there is no dwarf2 info
a092b084 1791 (or that an error occured while setting up the stash). */
252b5132 1792 if (! stash->info_ptr)
b34976b6 1793 return FALSE;
252b5132 1794
a092b084 1795 /* Check the previously read comp. units first. */
252b5132 1796 for (each = stash->all_comp_units; each; each = each->next_unit)
f623be2b 1797 if (comp_unit_contains_address (each, addr))
98591c73 1798 return comp_unit_find_nearest_line (each, addr, filename_ptr,
51db3708
NC
1799 functionname_ptr, linenumber_ptr,
1800 stash);
252b5132 1801
a092b084 1802 /* Read each remaining comp. units checking each as they are read. */
252b5132
RH
1803 while (stash->info_ptr < stash->info_ptr_end)
1804 {
5e38c3b8 1805 bfd_vma length;
b34976b6 1806 bfd_boolean found;
d03ba2a1 1807 unsigned int offset_size = addr_size;
252b5132 1808
a3805e4e
AO
1809 length = read_4_bytes (abfd, stash->info_ptr);
1810 /* A 0xffffff length is the DWARF3 way of indicating we use
1811 64-bit offsets, instead of 32-bit offsets. */
1812 if (length == 0xffffffff)
d03ba2a1 1813 {
a3805e4e
AO
1814 offset_size = 8;
1815 length = read_8_bytes (abfd, stash->info_ptr + 4);
1816 stash->info_ptr += 12;
1817 }
1818 /* A zero length is the IRIX way of indicating 64-bit offsets,
1819 mostly because the 64-bit length will generally fit in 32
1820 bits, and the endianness helps. */
1821 else if (length == 0)
1822 {
1823 offset_size = 8;
1824 length = read_4_bytes (abfd, stash->info_ptr + 4);
1825 stash->info_ptr += 8;
1826 }
1827 /* In the absence of the hints above, we assume addr_size-sized
1828 offsets, for backward-compatibility with pre-DWARF3 64-bit
1829 platforms. */
1830 else if (addr_size == 8)
1831 {
1832 length = read_8_bytes (abfd, stash->info_ptr);
060dc71d 1833 stash->info_ptr += 8;
d03ba2a1 1834 }
5e38c3b8 1835 else
a3805e4e 1836 stash->info_ptr += 4;
252b5132
RH
1837
1838 if (length > 0)
e82ce529 1839 {
d03ba2a1 1840 each = parse_comp_unit (abfd, stash, length, offset_size);
252b5132
RH
1841 stash->info_ptr += length;
1842
f2363ce5 1843 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
eea6121a 1844 == stash->sec->size)
f2363ce5
AO
1845 {
1846 stash->sec = find_debug_info (abfd, stash->sec);
1847 stash->sec_info_ptr = stash->info_ptr;
1848 }
1849
252b5132
RH
1850 if (each)
1851 {
1852 each->next_unit = stash->all_comp_units;
1853 stash->all_comp_units = each;
1854
159002ff
RH
1855 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1856 compilation units. If we don't have them (i.e.,
1857 unit->high == 0), we need to consult the line info
1858 table to see if a compilation unit contains the given
a092b084 1859 address. */
f623be2b 1860 if (each->arange.high > 0)
159002ff
RH
1861 {
1862 if (comp_unit_contains_address (each, addr))
1863 return comp_unit_find_nearest_line (each, addr,
6e84a906
DJ
1864 filename_ptr,
1865 functionname_ptr,
1866 linenumber_ptr,
1867 stash);
159002ff
RH
1868 }
1869 else
1870 {
1871 found = comp_unit_find_nearest_line (each, addr,
1872 filename_ptr,
1873 functionname_ptr,
51db3708
NC
1874 linenumber_ptr,
1875 stash);
159002ff 1876 if (found)
b34976b6 1877 return TRUE;
159002ff 1878 }
252b5132
RH
1879 }
1880 }
1881 }
1882
b34976b6 1883 return FALSE;
252b5132 1884}
This page took 0.360182 seconds and 4 git commands to generate.