* output-file.c (output_file_create): Don't try to open using
[deliverable/binutils-gdb.git] / bfd / dwarf2.c
CommitLineData
252b5132 1/* DWARF 2 support.
7898deda
NC
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 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
16This file is part of BFD.
17
18This program is free software; you can redistribute it and/or modify
19it under the terms of the GNU General Public License as published by
20the Free Software Foundation; either version 2 of the License, or (at
21your option) any later version.
22
23This program is distributed in the hope that it will be useful, but
24WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26General Public License for more details.
27
28You should have received a copy of the GNU General Public License
29along with this program; if not, write to the Free Software
30Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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
NC
42{
43 unsigned int total_length;
44 unsigned short version;
45 unsigned int prologue_length;
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;
64 unsigned int unsnd;
65 int snd;
66 bfd_vma addr;
67 }
68 u;
69};
70
71/* Get at parts of an attribute structure. */
252b5132
RH
72
73#define DW_STRING(attr) ((attr)->u.str)
74#define DW_UNSND(attr) ((attr)->u.unsnd)
75#define DW_BLOCK(attr) ((attr)->u.blk)
76#define DW_SND(attr) ((attr)->u.snd)
77#define DW_ADDR(attr) ((attr)->u.addr)
78
98591c73 79/* Blocks are a bunch of untyped bytes. */
252b5132 80struct dwarf_block
a092b084
NC
81{
82 unsigned int size;
83 char *data;
84};
252b5132 85
a092b084
NC
86struct dwarf2_debug
87{
88 /* A list of all previously read comp_units. */
252b5132
RH
89 struct comp_unit* all_comp_units;
90
91 /* The next unread compilation unit within the .debug_info section.
92 Zero indicates that the .debug_info section has not been loaded
a092b084 93 into a buffer yet. */
252b5132
RH
94 char* info_ptr;
95
a092b084 96 /* Pointer to the end of the .debug_info section memory buffer. */
252b5132
RH
97 char* info_ptr_end;
98
a092b084 99 /* Pointer to the .debug_abbrev section loaded into memory. */
252b5132
RH
100 char* dwarf_abbrev_buffer;
101
a092b084 102 /* Length of the loaded .debug_abbrev section. */
252b5132 103 unsigned long dwarf_abbrev_size;
69dd2e2d
RH
104
105 /* Buffer for decode_line_info. */
106 char *dwarf_line_buffer;
ccdb16fc
JW
107
108 /* Length of the loaded .debug_line section. */
109 unsigned long dwarf_line_size;
252b5132
RH
110};
111
a092b084
NC
112struct arange
113{
f623be2b
RH
114 struct arange *next;
115 bfd_vma low;
116 bfd_vma high;
117};
252b5132 118
252b5132 119/* A minimal decoding of DWARF2 compilation units. We only decode
a092b084 120 what's needed to get to the line number information. */
252b5132 121
a092b084
NC
122struct comp_unit
123{
124 /* Chain the previously read compilation units. */
252b5132
RH
125 struct comp_unit* next_unit;
126
a092b084 127 /* Keep the bdf convenient (for memory allocation). */
252b5132
RH
128 bfd* abfd;
129
130 /* The lowest and higest addresses contained in this compilation
a092b084 131 unit as specified in the compilation unit header. */
f623be2b 132 struct arange arange;
252b5132 133
a092b084 134 /* The DW_AT_name attribute (for error messages). */
252b5132
RH
135 char* name;
136
a092b084 137 /* The abbrev hash table. */
252b5132
RH
138 struct abbrev_info** abbrevs;
139
a092b084 140 /* Note that an error was found by comp_unit_find_nearest_line. */
252b5132
RH
141 int error;
142
a092b084 143 /* The DW_AT_comp_dir attribute. */
252b5132
RH
144 char* comp_dir;
145
a092b084 146 /* True if there is a line number table associated with this comp. unit. */
252b5132 147 int stmtlist;
98591c73 148
a092b084 149 /* The offset into .debug_line of the line number table. */
252b5132
RH
150 unsigned long line_offset;
151
a092b084 152 /* Pointer to the first child die for the comp unit. */
252b5132
RH
153 char *first_child_die_ptr;
154
a092b084 155 /* The end of the comp unit. */
252b5132
RH
156 char *end_ptr;
157
a092b084 158 /* The decoded line number, NULL if not yet decoded. */
252b5132
RH
159 struct line_info_table* line_table;
160
a092b084 161 /* A list of the functions found in this comp. unit. */
98591c73 162 struct funcinfo* function_table;
252b5132 163
a092b084 164 /* Address size for this unit - from unit header. */
252b5132
RH
165 unsigned char addr_size;
166};
167
98591c73
KH
168/* VERBATIM
169 The following function up to the END VERBATIM mark are
a092b084 170 copied directly from dwarf2read.c. */
252b5132 171
a092b084 172/* Read dwarf information from a buffer. */
252b5132
RH
173
174static unsigned int
175read_1_byte (abfd, buf)
7442e600 176 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
177 char *buf;
178{
179 return bfd_get_8 (abfd, (bfd_byte *) buf);
180}
181
182static int
183read_1_signed_byte (abfd, buf)
7442e600 184 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
185 char *buf;
186{
187 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
188}
189
190static unsigned int
191read_2_bytes (abfd, buf)
192 bfd *abfd;
193 char *buf;
194{
195 return bfd_get_16 (abfd, (bfd_byte *) buf);
196}
197
a092b084 198#if 0 /* This is not used. */
252b5132
RH
199
200static int
201read_2_signed_bytes (abfd, buf)
202 bfd *abfd;
203 char *buf;
204{
205 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
206}
207
208#endif
209
210static unsigned int
211read_4_bytes (abfd, buf)
212 bfd *abfd;
213 char *buf;
214{
215 return bfd_get_32 (abfd, (bfd_byte *) buf);
216}
217
a092b084 218#if 0 /* This is not used. */
252b5132
RH
219
220static int
221read_4_signed_bytes (abfd, buf)
222 bfd *abfd;
223 char *buf;
224{
225 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
226}
227
228#endif
229
230static unsigned int
231read_8_bytes (abfd, buf)
232 bfd *abfd;
233 char *buf;
234{
235 return bfd_get_64 (abfd, (bfd_byte *) buf);
236}
237
238static char *
239read_n_bytes (abfd, buf, size)
7442e600 240 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 241 char *buf;
7442e600 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 *
251read_string (abfd, buf, bytes_read_ptr)
7442e600 252 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
253 char *buf;
254 unsigned int *bytes_read_ptr;
255{
256 /* If the size of a host char is 8 bits, we can return a pointer
257 to the string, otherwise we have to copy the string to a buffer
258 allocated on the temporary obstack. */
259 if (*buf == '\0')
260 {
261 *bytes_read_ptr = 1;
262 return NULL;
263 }
98591c73 264
252b5132
RH
265 *bytes_read_ptr = strlen (buf) + 1;
266 return buf;
267}
268
269static unsigned int
270read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
7442e600 271 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
272 char *buf;
273 unsigned int *bytes_read_ptr;
274{
275 unsigned int result;
276 unsigned int num_read;
277 int shift;
278 unsigned char byte;
279
280 result = 0;
281 shift = 0;
282 num_read = 0;
98591c73 283
252b5132
RH
284 do
285 {
286 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
287 buf ++;
288 num_read ++;
289 result |= ((byte & 0x7f) << shift);
290 shift += 7;
291 }
292 while (byte & 0x80);
98591c73 293
252b5132 294 * bytes_read_ptr = num_read;
98591c73 295
252b5132
RH
296 return result;
297}
298
299static int
300read_signed_leb128 (abfd, buf, bytes_read_ptr)
7442e600
ILT
301 bfd *abfd ATTRIBUTE_UNUSED;
302 char *buf;
252b5132
RH
303 unsigned int * bytes_read_ptr;
304{
305 int result;
306 int shift;
307 int num_read;
308 unsigned char byte;
309
310 result = 0;
311 shift = 0;
312 num_read = 0;
313
314 do
315 {
316 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
317 buf ++;
318 num_read ++;
319 result |= ((byte & 0x7f) << shift);
320 shift += 7;
321 }
322 while (byte & 0x80);
98591c73 323
252b5132
RH
324 if ((shift < 32) && (byte & 0x40))
325 result |= -(1 << shift);
326
327 * bytes_read_ptr = num_read;
98591c73 328
252b5132
RH
329 return result;
330}
331
332/* END VERBATIM */
333
334static bfd_vma
335read_address (unit, buf)
336 struct comp_unit* unit;
337 char *buf;
338{
ecb651f0 339 switch (unit->addr_size)
252b5132 340 {
ecb651f0
NC
341 case 8:
342 return bfd_get_64 (unit->abfd, (bfd_byte *) buf);
343 case 4:
344 return bfd_get_32 (unit->abfd, (bfd_byte *) buf);
345 case 2:
346 return bfd_get_16 (unit->abfd, (bfd_byte *) buf);
347 default:
348 abort ();
252b5132 349 }
252b5132
RH
350}
351
98591c73 352/* This data structure holds the information of an abbrev. */
252b5132 353struct abbrev_info
a092b084
NC
354{
355 unsigned int number; /* Number identifying abbrev. */
356 enum dwarf_tag tag; /* DWARF tag. */
357 int has_children; /* Boolean. */
358 unsigned int num_attrs; /* Number of attributes. */
359 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
360 struct abbrev_info *next; /* Next in chain. */
361};
252b5132
RH
362
363struct attr_abbrev
a092b084
NC
364{
365 enum dwarf_attribute name;
366 enum dwarf_form form;
367};
252b5132
RH
368
369#ifndef ABBREV_HASH_SIZE
370#define ABBREV_HASH_SIZE 121
371#endif
372#ifndef ATTR_ALLOC_CHUNK
373#define ATTR_ALLOC_CHUNK 4
374#endif
375
376/* Lookup an abbrev_info structure in the abbrev hash table. */
377
378static struct abbrev_info *
379lookup_abbrev (number,abbrevs)
380 unsigned int number;
381 struct abbrev_info **abbrevs;
382{
383 unsigned int hash_number;
384 struct abbrev_info *abbrev;
385
386 hash_number = number % ABBREV_HASH_SIZE;
387 abbrev = abbrevs[hash_number];
388
389 while (abbrev)
390 {
391 if (abbrev->number == number)
392 return abbrev;
393 else
394 abbrev = abbrev->next;
395 }
98591c73 396
252b5132
RH
397 return NULL;
398}
399
400/* In DWARF version 2, the description of the debugging information is
401 stored in a separate .debug_abbrev section. Before we read any
402 dies from a section we read in all abbreviations and install them
403 in a hash table. */
404
405static struct abbrev_info**
51db3708 406read_abbrevs (abfd, offset, stash)
252b5132
RH
407 bfd * abfd;
408 unsigned int offset;
51db3708 409 struct dwarf2_debug *stash;
252b5132
RH
410{
411 struct abbrev_info **abbrevs;
412 char *abbrev_ptr;
413 struct abbrev_info *cur_abbrev;
414 unsigned int abbrev_number, bytes_read, abbrev_name;
415 unsigned int abbrev_form, hash_number;
252b5132
RH
416
417 if (! stash->dwarf_abbrev_buffer)
418 {
419 asection *msec;
420
421 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
422 if (! msec)
423 {
424 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
425 bfd_set_error (bfd_error_bad_value);
426 return 0;
427 }
98591c73 428
67d83c76 429 stash->dwarf_abbrev_size = msec->_raw_size;
7fafc0fd 430 stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, stash->dwarf_abbrev_size);
252b5132
RH
431 if (! stash->dwarf_abbrev_buffer)
432 return 0;
98591c73
KH
433
434 if (! bfd_get_section_contents (abfd, msec,
252b5132
RH
435 stash->dwarf_abbrev_buffer, 0,
436 stash->dwarf_abbrev_size))
437 return 0;
438 }
439
f5198f61 440 if (offset >= stash->dwarf_abbrev_size)
252b5132 441 {
f5198f61 442 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%u) greater than or equal to abbrev size (%u)."),
252b5132
RH
443 offset, stash->dwarf_abbrev_size );
444 bfd_set_error (bfd_error_bad_value);
445 return 0;
446 }
447
98591c73 448 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE);
252b5132
RH
449
450 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
451 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
452 abbrev_ptr += bytes_read;
453
a092b084 454 /* Loop until we reach an abbrev number of 0. */
252b5132
RH
455 while (abbrev_number)
456 {
457 cur_abbrev = (struct abbrev_info*)bfd_zalloc (abfd, sizeof (struct abbrev_info));
458
a092b084 459 /* Read in abbrev header. */
252b5132
RH
460 cur_abbrev->number = abbrev_number;
461 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
462 abbrev_ptr += bytes_read;
463 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
464 abbrev_ptr += 1;
465
a092b084 466 /* Now read in declarations. */
252b5132
RH
467 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
468 abbrev_ptr += bytes_read;
469 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
470 abbrev_ptr += bytes_read;
98591c73 471
252b5132
RH
472 while (abbrev_name)
473 {
474 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
475 {
476 cur_abbrev->attrs = (struct attr_abbrev *)
477 bfd_realloc (cur_abbrev->attrs,
478 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
479 * sizeof (struct attr_abbrev));
480 if (! cur_abbrev->attrs)
481 return 0;
482 }
98591c73 483
252b5132
RH
484 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
485 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
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;
490 }
491
492 hash_number = abbrev_number % ABBREV_HASH_SIZE;
493 cur_abbrev->next = abbrevs[hash_number];
494 abbrevs[hash_number] = cur_abbrev;
495
496 /* Get next abbreviation.
497 Under Irix6 the abbreviations for a compilation unit are not
498 always properly terminated with an abbrev number of 0.
499 Exit loop if we encounter an abbreviation which we have
500 already read (which means we are about to read the abbreviations
501 for the next compile unit) or if the end of the abbreviation
502 table is reached. */
503 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
504 >= stash->dwarf_abbrev_size)
505 break;
506 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
507 abbrev_ptr += bytes_read;
508 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
509 break;
510 }
511
512 return abbrevs;
513}
514
515/* Read an attribute described by an abbreviated attribute. */
516
517static char *
518read_attribute (attr, abbrev, unit, info_ptr)
519 struct attribute *attr;
520 struct attr_abbrev *abbrev;
521 struct comp_unit *unit;
522 char *info_ptr;
523{
524 bfd *abfd = unit->abfd;
525 unsigned int bytes_read;
526 struct dwarf_block *blk;
527
528 attr->name = abbrev->name;
529 attr->form = abbrev->form;
98591c73 530
252b5132
RH
531 switch (abbrev->form)
532 {
533 case DW_FORM_addr:
534 case DW_FORM_ref_addr:
535 DW_ADDR (attr) = read_address (unit, info_ptr);
536 info_ptr += unit->addr_size;
537 break;
538 case DW_FORM_block2:
539 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
540 blk->size = read_2_bytes (abfd, info_ptr);
541 info_ptr += 2;
542 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
543 info_ptr += blk->size;
544 DW_BLOCK (attr) = blk;
545 break;
546 case DW_FORM_block4:
547 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
548 blk->size = read_4_bytes (abfd, info_ptr);
549 info_ptr += 4;
550 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
551 info_ptr += blk->size;
552 DW_BLOCK (attr) = blk;
553 break;
554 case DW_FORM_data2:
555 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
556 info_ptr += 2;
557 break;
558 case DW_FORM_data4:
559 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
560 info_ptr += 4;
561 break;
562 case DW_FORM_data8:
563 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
564 info_ptr += 8;
565 break;
566 case DW_FORM_string:
567 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
568 info_ptr += bytes_read;
569 break;
570 case DW_FORM_block:
571 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
572 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
573 info_ptr += bytes_read;
574 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
575 info_ptr += blk->size;
576 DW_BLOCK (attr) = blk;
577 break;
578 case DW_FORM_block1:
579 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
580 blk->size = read_1_byte (abfd, info_ptr);
581 info_ptr += 1;
582 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
583 info_ptr += blk->size;
584 DW_BLOCK (attr) = blk;
585 break;
586 case DW_FORM_data1:
587 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
588 info_ptr += 1;
589 break;
590 case DW_FORM_flag:
591 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
592 info_ptr += 1;
593 break;
594 case DW_FORM_sdata:
595 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
596 info_ptr += bytes_read;
597 break;
598 case DW_FORM_udata:
599 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
600 info_ptr += bytes_read;
601 break;
602 case DW_FORM_ref1:
603 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
604 info_ptr += 1;
605 break;
606 case DW_FORM_ref2:
607 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
608 info_ptr += 2;
609 break;
610 case DW_FORM_ref4:
611 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
612 info_ptr += 4;
613 break;
81edd86d
MM
614 case DW_FORM_ref8:
615 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
616 info_ptr += 8;
617 break;
252b5132
RH
618 case DW_FORM_ref_udata:
619 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
620 info_ptr += bytes_read;
621 break;
622 case DW_FORM_strp:
623 case DW_FORM_indirect:
624 default:
625 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
626 abbrev->form);
627 bfd_set_error (bfd_error_bad_value);
628 }
629 return info_ptr;
630}
631
a092b084 632/* Source line information table routines. */
252b5132
RH
633
634#define FILE_ALLOC_CHUNK 5
635#define DIR_ALLOC_CHUNK 5
636
a092b084
NC
637struct line_info
638{
252b5132 639 struct line_info* prev_line;
252b5132
RH
640 bfd_vma address;
641 char* filename;
642 unsigned int line;
643 unsigned int column;
a092b084 644 int end_sequence; /* End of (sequential) code sequence. */
252b5132
RH
645};
646
a092b084
NC
647struct fileinfo
648{
252b5132
RH
649 char *name;
650 unsigned int dir;
651 unsigned int time;
652 unsigned int size;
653};
654
a092b084
NC
655struct line_info_table
656{
252b5132 657 bfd* abfd;
252b5132
RH
658 unsigned int num_files;
659 unsigned int num_dirs;
252b5132
RH
660 char* comp_dir;
661 char** dirs;
662 struct fileinfo* files;
663 struct line_info* last_line;
664};
665
98591c73 666static void
159002ff 667add_line_info (table, address, filename, line, column, end_sequence)
252b5132
RH
668 struct line_info_table* table;
669 bfd_vma address;
670 char* filename;
671 unsigned int line;
672 unsigned int column;
159002ff 673 int end_sequence;
252b5132
RH
674{
675 struct line_info* info = (struct line_info*)
676 bfd_alloc (table->abfd, sizeof (struct line_info));
677
678 info->prev_line = table->last_line;
679 table->last_line = info;
680
681 info->address = address;
682 info->filename = filename;
683 info->line = line;
684 info->column = column;
159002ff 685 info->end_sequence = end_sequence;
252b5132
RH
686}
687
a092b084 688static char *
252b5132
RH
689concat_filename (table, file)
690 struct line_info_table* table;
691 unsigned int file;
692{
159002ff
RH
693 char* filename;
694
695 if (file - 1 >= table->num_files)
696 {
dcdea4f4
AM
697 (*_bfd_error_handler)
698 (_("Dwarf Error: mangled line number section (bad file number)."));
159002ff
RH
699 return "<unknown>";
700 }
701
702 filename = table->files[file - 1].name;
51db3708 703 if (IS_ABSOLUTE_PATH(filename))
252b5132
RH
704 return filename;
705
706 else
707 {
708 char* dirname = (table->files[file - 1].dir
709 ? table->dirs[table->files[file - 1].dir - 1]
710 : table->comp_dir);
711 return (char*) concat (dirname, "/", filename, NULL);
712 }
713}
714
f623be2b
RH
715static void
716arange_add (unit, low_pc, high_pc)
717 struct comp_unit *unit;
718 bfd_vma low_pc;
719 bfd_vma high_pc;
720{
721 struct arange *arange;
722
a092b084 723 /* First see if we can cheaply extend an existing range. */
f623be2b 724 arange = &unit->arange;
98591c73 725
f623be2b
RH
726 do
727 {
728 if (low_pc == arange->high)
729 {
730 arange->high = high_pc;
731 return;
732 }
733 if (high_pc == arange->low)
734 {
735 arange->low = low_pc;
736 return;
737 }
738 arange = arange->next;
739 }
740 while (arange);
741
742 if (unit->arange.high == 0)
743 {
a092b084 744 /* This is the first address range: store it in unit->arange. */
f623be2b
RH
745 unit->arange.next = 0;
746 unit->arange.low = low_pc;
747 unit->arange.high = high_pc;
748 return;
749 }
750
a092b084 751 /* Need to allocate a new arange and insert it into the arange list. */
f623be2b
RH
752 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
753 arange->low = low_pc;
754 arange->high = high_pc;
755
756 arange->next = unit->arange.next;
757 unit->arange.next = arange;
758}
759
a092b084 760/* Decode the line number information for UNIT. */
252b5132
RH
761
762static struct line_info_table*
51db3708 763decode_line_info (unit, stash)
252b5132 764 struct comp_unit *unit;
51db3708 765 struct dwarf2_debug *stash;
252b5132
RH
766{
767 bfd *abfd = unit->abfd;
252b5132 768 struct line_info_table* table;
252b5132
RH
769 char *line_ptr;
770 char *line_end;
771 struct line_head lh;
772 unsigned int i, bytes_read;
773 char *cur_file, *cur_dir;
774 unsigned char op_code, extended_op, adj_opcode;
775
69dd2e2d 776 if (! stash->dwarf_line_buffer)
252b5132
RH
777 {
778 asection *msec;
252b5132
RH
779
780 msec = bfd_get_section_by_name (abfd, ".debug_line");
781 if (! msec)
782 {
783 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
784 bfd_set_error (bfd_error_bad_value);
785 return 0;
786 }
98591c73 787
ccdb16fc
JW
788 stash->dwarf_line_size = msec->_raw_size;
789 stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, stash->dwarf_line_size);
f623be2b 790 if (! stash->dwarf_line_buffer)
252b5132
RH
791 return 0;
792
98591c73 793 if (! bfd_get_section_contents (abfd, msec,
f623be2b 794 stash->dwarf_line_buffer, 0,
ccdb16fc 795 stash->dwarf_line_size))
252b5132
RH
796 return 0;
797
798 /* FIXME: We ought to apply the relocs against this section before
a092b084 799 we process it... */
252b5132
RH
800 }
801
ccdb16fc
JW
802 /* Since we are using un-relocated data, it is possible to get a bad value
803 for the line_offset. Validate it here so that we won't get a segfault
804 below. */
805 if (unit->line_offset >= stash->dwarf_line_size)
806 {
f5198f61 807 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%u) greater than or equal to line size (%u)."),
ccdb16fc
JW
808 unit->line_offset, stash->dwarf_line_size);
809 bfd_set_error (bfd_error_bad_value);
810 return 0;
811 }
812
98591c73 813 table = (struct line_info_table*) bfd_alloc (abfd,
252b5132
RH
814 sizeof (struct line_info_table));
815 table->abfd = abfd;
816 table->comp_dir = unit->comp_dir;
817
818 table->num_files = 0;
819 table->files = NULL;
820
821 table->num_dirs = 0;
822 table->dirs = NULL;
823
159002ff
RH
824 table->files = NULL;
825 table->last_line = NULL;
826
69dd2e2d 827 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
252b5132 828
a092b084 829 /* Read in the prologue. */
252b5132
RH
830 lh.total_length = read_4_bytes (abfd, line_ptr);
831 line_ptr += 4;
832 line_end = line_ptr + lh.total_length;
833 lh.version = read_2_bytes (abfd, line_ptr);
834 line_ptr += 2;
835 lh.prologue_length = read_4_bytes (abfd, line_ptr);
836 line_ptr += 4;
837 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
838 line_ptr += 1;
839 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
840 line_ptr += 1;
841 lh.line_base = read_1_signed_byte (abfd, line_ptr);
842 line_ptr += 1;
843 lh.line_range = read_1_byte (abfd, line_ptr);
844 line_ptr += 1;
845 lh.opcode_base = read_1_byte (abfd, line_ptr);
846 line_ptr += 1;
847 lh.standard_opcode_lengths = (unsigned char *)
848 bfd_alloc (abfd, lh.opcode_base * sizeof (unsigned char));
849
850 lh.standard_opcode_lengths[0] = 1;
98591c73 851
252b5132
RH
852 for (i = 1; i < lh.opcode_base; ++i)
853 {
854 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
855 line_ptr += 1;
856 }
857
a092b084 858 /* Read directory table. */
252b5132
RH
859 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
860 {
861 line_ptr += bytes_read;
98591c73 862
252b5132
RH
863 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
864 {
865 table->dirs = (char **)
866 bfd_realloc (table->dirs,
867 (table->num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
868 if (! table->dirs)
869 return 0;
870 }
98591c73 871
252b5132
RH
872 table->dirs[table->num_dirs++] = cur_dir;
873 }
98591c73 874
252b5132
RH
875 line_ptr += bytes_read;
876
a092b084 877 /* Read file name table. */
252b5132
RH
878 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
879 {
880 line_ptr += bytes_read;
98591c73 881
252b5132
RH
882 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
883 {
884 table->files = (struct fileinfo *)
885 bfd_realloc (table->files,
886 (table->num_files + FILE_ALLOC_CHUNK)
887 * sizeof (struct fileinfo));
888 if (! table->files)
889 return 0;
890 }
98591c73 891
252b5132
RH
892 table->files[table->num_files].name = cur_file;
893 table->files[table->num_files].dir =
894 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
895 line_ptr += bytes_read;
896 table->files[table->num_files].time =
897 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
898 line_ptr += bytes_read;
899 table->files[table->num_files].size =
900 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
901 line_ptr += bytes_read;
902 table->num_files++;
903 }
98591c73 904
252b5132
RH
905 line_ptr += bytes_read;
906
907 /* Read the statement sequences until there's nothing left. */
908 while (line_ptr < line_end)
909 {
a092b084 910 /* State machine registers. */
252b5132
RH
911 bfd_vma address = 0;
912 char* filename = concat_filename (table, 1);
913 unsigned int line = 1;
914 unsigned int column = 0;
915 int is_stmt = lh.default_is_stmt;
916 int basic_block = 0;
f623be2b
RH
917 int end_sequence = 0, need_low_pc = 1;
918 bfd_vma low_pc = 0;
252b5132 919
a092b084 920 /* Decode the table. */
252b5132
RH
921 while (! end_sequence)
922 {
923 op_code = read_1_byte (abfd, line_ptr);
924 line_ptr += 1;
98591c73 925
252b5132
RH
926 switch (op_code)
927 {
928 case DW_LNS_extended_op:
a092b084 929 line_ptr += 1; /* Ignore length. */
252b5132
RH
930 extended_op = read_1_byte (abfd, line_ptr);
931 line_ptr += 1;
932 switch (extended_op)
933 {
934 case DW_LNE_end_sequence:
935 end_sequence = 1;
f623be2b
RH
936 add_line_info (table, address, filename, line, column,
937 end_sequence);
938 if (need_low_pc)
939 {
940 need_low_pc = 0;
941 low_pc = address;
942 }
943 arange_add (unit, low_pc, address);
252b5132
RH
944 break;
945 case DW_LNE_set_address:
946 address = read_address (unit, line_ptr);
947 line_ptr += unit->addr_size;
948 break;
949 case DW_LNE_define_file:
950 cur_file = read_string (abfd, line_ptr, &bytes_read);
951 line_ptr += bytes_read;
952 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
953 {
954 table->files = (struct fileinfo *)
955 bfd_realloc (table->files,
956 (table->num_files + FILE_ALLOC_CHUNK)
957 * sizeof (struct fileinfo));
958 if (! table->files)
959 return 0;
960 }
961 table->files[table->num_files].name = cur_file;
962 table->files[table->num_files].dir =
963 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
964 line_ptr += bytes_read;
965 table->files[table->num_files].time =
966 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
967 line_ptr += bytes_read;
968 table->files[table->num_files].size =
969 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
970 line_ptr += bytes_read;
971 table->num_files++;
972 break;
973 default:
974 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
975 bfd_set_error (bfd_error_bad_value);
976 return 0;
977 }
978 break;
979 case DW_LNS_copy:
159002ff 980 add_line_info (table, address, filename, line, column, 0);
252b5132 981 basic_block = 0;
f623be2b
RH
982 if (need_low_pc)
983 {
984 need_low_pc = 0;
985 low_pc = address;
986 }
252b5132
RH
987 break;
988 case DW_LNS_advance_pc:
989 address += lh.minimum_instruction_length
990 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
991 line_ptr += bytes_read;
992 break;
993 case DW_LNS_advance_line:
994 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
995 line_ptr += bytes_read;
996 break;
997 case DW_LNS_set_file:
998 {
999 unsigned int file;
1000
1001 /* The file and directory tables are 0 based, the references
1002 are 1 based. */
1003 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1004 line_ptr += bytes_read;
1005 filename = concat_filename (table, file);
1006 break;
1007 }
1008 case DW_LNS_set_column:
1009 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1010 line_ptr += bytes_read;
1011 break;
1012 case DW_LNS_negate_stmt:
1013 is_stmt = (!is_stmt);
1014 break;
1015 case DW_LNS_set_basic_block:
1016 basic_block = 1;
1017 break;
1018 case DW_LNS_const_add_pc:
159002ff
RH
1019 address += lh.minimum_instruction_length
1020 * ((255 - lh.opcode_base) / lh.line_range);
252b5132
RH
1021 break;
1022 case DW_LNS_fixed_advance_pc:
1023 address += read_2_bytes (abfd, line_ptr);
1024 line_ptr += 2;
1025 break;
a092b084 1026 default: /* Special operand. */
252b5132
RH
1027 adj_opcode = op_code - lh.opcode_base;
1028 address += (adj_opcode / lh.line_range)
1029 * lh.minimum_instruction_length;
1030 line += lh.line_base + (adj_opcode % lh.line_range);
a092b084 1031 /* Append row to matrix using current values. */
159002ff 1032 add_line_info (table, address, filename, line, column, 0);
252b5132 1033 basic_block = 1;
f623be2b
RH
1034 if (need_low_pc)
1035 {
1036 need_low_pc = 0;
1037 low_pc = address;
1038 }
252b5132
RH
1039 }
1040 }
1041 }
1042
1043 return table;
1044}
1045
252b5132
RH
1046/* If ADDR is within TABLE set the output parameters and return true,
1047 otherwise return false. The output parameters, FILENAME_PTR and
a092b084 1048 LINENUMBER_PTR, are pointers to the objects to be filled in. */
252b5132
RH
1049
1050static boolean
98591c73 1051lookup_address_in_line_info_table (table,
252b5132 1052 addr,
98591c73 1053 filename_ptr,
252b5132
RH
1054 linenumber_ptr)
1055 struct line_info_table* table;
1056 bfd_vma addr;
1057 const char **filename_ptr;
1058 unsigned int *linenumber_ptr;
1059{
159002ff 1060 struct line_info* next_line = table->last_line;
252b5132 1061 struct line_info* each_line;
98591c73 1062
159002ff
RH
1063 if (!next_line)
1064 return false;
1065
1066 each_line = next_line->prev_line;
1067
1068 while (each_line && next_line)
252b5132 1069 {
159002ff
RH
1070 if (!each_line->end_sequence
1071 && addr >= each_line->address && addr < next_line->address)
252b5132
RH
1072 {
1073 *filename_ptr = each_line->filename;
1074 *linenumber_ptr = each_line->line;
1075 return true;
1076 }
159002ff
RH
1077 next_line = each_line;
1078 each_line = each_line->prev_line;
252b5132 1079 }
98591c73 1080
252b5132
RH
1081 return false;
1082}
98591c73 1083
a092b084 1084/* Function table functions. */
252b5132 1085
a092b084
NC
1086struct funcinfo
1087{
252b5132 1088 struct funcinfo *prev_func;
252b5132
RH
1089 char* name;
1090 bfd_vma low;
1091 bfd_vma high;
1092};
1093
98591c73 1094/* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
252b5132
RH
1095
1096static boolean
98591c73 1097lookup_address_in_function_table (table,
252b5132
RH
1098 addr,
1099 functionname_ptr)
1100 struct funcinfo* table;
1101 bfd_vma addr;
1102 const char **functionname_ptr;
1103{
1104 struct funcinfo* each_func;
1105
1106 for (each_func = table;
1107 each_func;
1108 each_func = each_func->prev_func)
1109 {
1110 if (addr >= each_func->low && addr < each_func->high)
1111 {
1112 *functionname_ptr = each_func->name;
1113 return true;
1114 }
1115 }
98591c73 1116
252b5132
RH
1117 return false;
1118}
1119
a092b084 1120/* DWARF2 Compilation unit functions. */
252b5132
RH
1121
1122/* Scan over each die in a comp. unit looking for functions to add
a092b084 1123 to the function table. */
252b5132
RH
1124
1125static boolean
1126scan_unit_for_functions (unit)
1127 struct comp_unit *unit;
1128{
1129 bfd *abfd = unit->abfd;
1130 char *info_ptr = unit->first_child_die_ptr;
1131 int nesting_level = 1;
1132
1133 while (nesting_level)
1134 {
1135 unsigned int abbrev_number, bytes_read, i;
1136 struct abbrev_info *abbrev;
1137 struct attribute attr;
1138 struct funcinfo *func;
1139 char* name = 0;
1140
1141 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1142 info_ptr += bytes_read;
1143
1144 if (! abbrev_number)
1145 {
1146 nesting_level--;
1147 continue;
1148 }
98591c73 1149
252b5132
RH
1150 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1151 if (! abbrev)
1152 {
98591c73 1153 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
252b5132
RH
1154 abbrev_number);
1155 bfd_set_error (bfd_error_bad_value);
1156 return false;
1157 }
98591c73 1158
252b5132
RH
1159 if (abbrev->tag == DW_TAG_subprogram)
1160 {
1161 func = (struct funcinfo*) bfd_zalloc (abfd, sizeof (struct funcinfo));
1162 func->prev_func = unit->function_table;
1163 unit->function_table = func;
1164 }
1165 else
1166 func = NULL;
98591c73 1167
252b5132
RH
1168 for (i = 0; i < abbrev->num_attrs; ++i)
1169 {
1170 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
98591c73 1171
252b5132
RH
1172 if (func)
1173 {
1174 switch (attr.name)
1175 {
1176 case DW_AT_name:
98591c73 1177
252b5132
RH
1178 name = DW_STRING (&attr);
1179
1180 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1181 if (func->name == NULL)
1182 func->name = DW_STRING (&attr);
1183 break;
98591c73 1184
252b5132
RH
1185 case DW_AT_MIPS_linkage_name:
1186 func->name = DW_STRING (&attr);
1187 break;
1188
1189 case DW_AT_low_pc:
1190 func->low = DW_ADDR (&attr);
1191 break;
1192
1193 case DW_AT_high_pc:
1194 func->high = DW_ADDR (&attr);
1195 break;
1196
1197 default:
1198 break;
1199 }
1200 }
1201 else
1202 {
1203 switch (attr.name)
1204 {
1205 case DW_AT_name:
1206 name = DW_STRING (&attr);
1207 break;
98591c73 1208
252b5132
RH
1209 default:
1210 break;
1211 }
1212 }
1213 }
1214
1215 if (abbrev->has_children)
1216 nesting_level++;
1217 }
1218
1219 return true;
1220}
1221
5e38c3b8
MM
1222/* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1223 includes the compilation unit header that proceeds the DIE's, but
1224 does not include the length field that preceeds each compilation
1225 unit header. END_PTR points one past the end of this comp unit.
1226 If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1227 is assumed to be four bytes. Otherwise, it it is the size given.
252b5132
RH
1228
1229 This routine does not read the whole compilation unit; only enough
1230 to get to the line number information for the compilation unit. */
1231
1232static struct comp_unit *
51db3708 1233parse_comp_unit (abfd, stash, unit_length, abbrev_length)
252b5132 1234 bfd* abfd;
51db3708
NC
1235 struct dwarf2_debug *stash;
1236 bfd_vma unit_length;
5e38c3b8 1237 unsigned int abbrev_length;
252b5132
RH
1238{
1239 struct comp_unit* unit;
1240
1241 unsigned short version;
7442e600 1242 unsigned int abbrev_offset = 0;
252b5132
RH
1243 unsigned char addr_size;
1244 struct abbrev_info** abbrevs;
1245
1246 unsigned int abbrev_number, bytes_read, i;
1247 struct abbrev_info *abbrev;
1248 struct attribute attr;
1249
51db3708
NC
1250 char *info_ptr = stash->info_ptr;
1251 char *end_ptr = info_ptr + unit_length;
3fde5a36 1252
252b5132
RH
1253 version = read_2_bytes (abfd, info_ptr);
1254 info_ptr += 2;
5e38c3b8
MM
1255 BFD_ASSERT (abbrev_length == 0
1256 || abbrev_length == 4
1257 || abbrev_length == 8);
1258 if (abbrev_length == 0 || abbrev_length == 4)
1259 abbrev_offset = read_4_bytes (abfd, info_ptr);
1260 else if (abbrev_length == 8)
1261 abbrev_offset = read_8_bytes (abfd, info_ptr);
1262 info_ptr += abbrev_length;
252b5132
RH
1263 addr_size = read_1_byte (abfd, info_ptr);
1264 info_ptr += 1;
1265
1266 if (version != 2)
1267 {
1268 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version );
1269 bfd_set_error (bfd_error_bad_value);
1270 return 0;
1271 }
1272
1273 if (addr_size > sizeof (bfd_vma))
1274 {
1275 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1276 addr_size,
1277 sizeof (bfd_vma));
1278 bfd_set_error (bfd_error_bad_value);
1279 return 0;
1280 }
1281
ecb651f0 1282 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
252b5132 1283 {
ecb651f0 1284 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size );
252b5132
RH
1285 bfd_set_error (bfd_error_bad_value);
1286 return 0;
1287 }
1288
a092b084 1289 /* Read the abbrevs for this compilation unit into a table. */
51db3708 1290 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
252b5132
RH
1291 if (! abbrevs)
1292 return 0;
1293
1294 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1295 info_ptr += bytes_read;
1296 if (! abbrev_number)
1297 {
1298 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %d."),
1299 abbrev_number);
1300 bfd_set_error (bfd_error_bad_value);
1301 return 0;
1302 }
1303
1304 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1305 if (! abbrev)
1306 {
1307 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1308 abbrev_number);
1309 bfd_set_error (bfd_error_bad_value);
1310 return 0;
1311 }
98591c73 1312
252b5132
RH
1313 unit = (struct comp_unit*) bfd_zalloc (abfd, sizeof (struct comp_unit));
1314 unit->abfd = abfd;
98591c73 1315 unit->addr_size = addr_size;
252b5132
RH
1316 unit->abbrevs = abbrevs;
1317 unit->end_ptr = end_ptr;
1318
1319 for (i = 0; i < abbrev->num_attrs; ++i)
1320 {
1321 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1322
1323 /* Store the data if it is of an attribute we want to keep in a
1324 partial symbol table. */
1325 switch (attr.name)
1326 {
1327 case DW_AT_stmt_list:
1328 unit->stmtlist = 1;
1329 unit->line_offset = DW_UNSND (&attr);
1330 break;
1331
1332 case DW_AT_name:
1333 unit->name = DW_STRING (&attr);
1334 break;
1335
1336 case DW_AT_low_pc:
f623be2b 1337 unit->arange.low = DW_ADDR (&attr);
252b5132
RH
1338 break;
1339
1340 case DW_AT_high_pc:
f623be2b 1341 unit->arange.high = DW_ADDR (&attr);
252b5132
RH
1342 break;
1343
1344 case DW_AT_comp_dir:
1345 {
1346 char* comp_dir = DW_STRING (&attr);
1347 if (comp_dir)
1348 {
1349 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1350 directory, get rid of it. */
1351 char *cp = (char*) strchr (comp_dir, ':');
1352
1353 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1354 comp_dir = cp + 1;
1355 }
1356 unit->comp_dir = comp_dir;
1357 break;
1358 }
1359
1360 default:
1361 break;
1362 }
1363 }
1364
1365 unit->first_child_die_ptr = info_ptr;
1366 return unit;
1367}
1368
a092b084 1369/* Return true if UNIT contains the address given by ADDR. */
252b5132
RH
1370
1371static boolean
1372comp_unit_contains_address (unit, addr)
1373 struct comp_unit* unit;
1374 bfd_vma addr;
1375{
f623be2b
RH
1376 struct arange *arange;
1377
1378 if (unit->error)
1379 return 0;
1380
1381 arange = &unit->arange;
1382 do
1383 {
1384 if (addr >= arange->low && addr < arange->high)
1385 return 1;
1386 arange = arange->next;
1387 }
1388 while (arange);
98591c73 1389
f623be2b 1390 return 0;
252b5132
RH
1391}
1392
252b5132
RH
1393/* If UNIT contains ADDR, set the output parameters to the values for
1394 the line containing ADDR. The output parameters, FILENAME_PTR,
1395 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
98591c73 1396 to be filled in.
252b5132
RH
1397
1398 Return true of UNIT contains ADDR, and no errors were encountered;
1399 false otherwise. */
1400
1401static boolean
1402comp_unit_find_nearest_line (unit, addr,
51db3708
NC
1403 filename_ptr, functionname_ptr, linenumber_ptr,
1404 stash)
252b5132
RH
1405 struct comp_unit* unit;
1406 bfd_vma addr;
1407 const char **filename_ptr;
1408 const char **functionname_ptr;
1409 unsigned int *linenumber_ptr;
51db3708 1410 struct dwarf2_debug *stash;
252b5132
RH
1411{
1412 boolean line_p;
1413 boolean func_p;
98591c73 1414
252b5132
RH
1415 if (unit->error)
1416 return false;
1417
1418 if (! unit->line_table)
1419 {
1420 if (! unit->stmtlist)
1421 {
1422 unit->error = 1;
1423 return false;
1424 }
98591c73 1425
51db3708 1426 unit->line_table = decode_line_info (unit, stash);
252b5132
RH
1427
1428 if (! unit->line_table)
1429 {
1430 unit->error = 1;
1431 return false;
1432 }
98591c73 1433
252b5132
RH
1434 if (! scan_unit_for_functions (unit))
1435 {
1436 unit->error = 1;
1437 return false;
1438 }
1439 }
1440
1441 line_p = lookup_address_in_line_info_table (unit->line_table,
1442 addr,
98591c73 1443 filename_ptr,
252b5132 1444 linenumber_ptr);
98591c73 1445 func_p = lookup_address_in_function_table (unit->function_table,
252b5132
RH
1446 addr,
1447 functionname_ptr);
1448 return line_p || func_p;
1449}
1450
a092b084
NC
1451/* Locate a section in a BFD containing debugging info. The search starts from the
1452 section after AFTER_SEC, or from the first section in the BFD if AFTER_SEC is
1453 NULL. The search works by examining the names of the sections. There are two
1454 permissiable names. The first is .debug_info. This is the standard DWARF2 name.
1455 The second is a prefix .gnu.linkonce.wi. This is a variation on the .debug_info
1456 section which has a checksum describing the contents appended onto the name. This
1457 allows the linker to identify and discard duplicate debugging sections for
1458 different compilation units. */
1459#define DWARF2_DEBUG_INFO ".debug_info"
1460#define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1461
1462static asection *
1463find_debug_info (abfd, after_sec)
1464 bfd * abfd;
1465 asection * after_sec;
1466{
1467 asection * msec;
1468
1469 if (after_sec)
1470 msec = after_sec->next;
1471 else
1472 msec = abfd->sections;
1473
1474 while (msec)
1475 {
1476 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1477 return msec;
1478
1479 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1480 return msec;
1481
1482 msec = msec->next;
1483 }
1484
1485 return NULL;
1486}
1487
5e38c3b8
MM
1488/* The DWARF2 version of find_nearest line. Return true if the line
1489 is found without error. ADDR_SIZE is the number of bytes in the
1490 initial .debug_info length field and in the abbreviation offset.
1491 You may use zero to indicate that the default value should be
1492 used. */
252b5132
RH
1493
1494boolean
1495_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5e38c3b8
MM
1496 filename_ptr, functionname_ptr,
1497 linenumber_ptr,
51db3708 1498 addr_size, pinfo)
252b5132
RH
1499 bfd *abfd;
1500 asection *section;
7442e600 1501 asymbol **symbols ATTRIBUTE_UNUSED;
252b5132
RH
1502 bfd_vma offset;
1503 const char **filename_ptr;
1504 const char **functionname_ptr;
1505 unsigned int *linenumber_ptr;
5e38c3b8 1506 unsigned int addr_size;
51db3708 1507 PTR *pinfo;
252b5132
RH
1508{
1509 /* Read each compilation unit from the section .debug_info, and check
1510 to see if it contains the address we are searching for. If yes,
1511 lookup the address, and return the line number info. If no, go
98591c73 1512 on to the next compilation unit.
252b5132
RH
1513
1514 We keep a list of all the previously read compilation units, and
98591c73 1515 a pointer to the next un-read compilation unit. Check the
a092b084 1516 previously read units before reading more. */
51db3708 1517 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
252b5132 1518
a092b084 1519 /* What address are we looking for? */
252b5132
RH
1520 bfd_vma addr = offset + section->vma;
1521
1522 struct comp_unit* each;
98591c73 1523
252b5132
RH
1524 *filename_ptr = NULL;
1525 *functionname_ptr = NULL;
1526 *linenumber_ptr = 0;
1527
5e38c3b8
MM
1528 /* The DWARF2 spec says that the initial length field, and the
1529 offset of the abbreviation table, should both be 4-byte values.
1530 However, some compilers do things differently. */
1531 if (addr_size == 0)
1532 addr_size = 4;
1efe4b10 1533 BFD_ASSERT (addr_size == 4 || addr_size == 8);
98591c73 1534
252b5132
RH
1535 if (! stash)
1536 {
a092b084 1537 unsigned long total_size;
252b5132 1538 asection *msec;
a092b084 1539
51db3708 1540 stash =
252b5132 1541 (struct dwarf2_debug*) bfd_zalloc (abfd, sizeof (struct dwarf2_debug));
252b5132
RH
1542 if (! stash)
1543 return false;
252b5132 1544
51db3708 1545 *pinfo = (PTR) stash;
3fde5a36 1546
a092b084
NC
1547 msec = find_debug_info (abfd, NULL);
1548 if (! msec)
1549 /* No dwarf2 info. Note that at this point the stash
1550 has been allocated, but contains zeros, this lets
1551 future calls to this function fail quicker. */
51db3708 1552 return false;
a092b084
NC
1553
1554 /* There can be more than one DWARF2 info section in a BFD these days.
1555 Read them all in and produce one large stash. We do this in two
1556 passes - in the first pass we just accumulate the section sizes.
1557 In the second pass we read in the section's contents. The allows
1558 us to avoid reallocing the data as we add sections to the stash. */
1559 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1560 total_size += msec->_raw_size;
98591c73 1561
a092b084
NC
1562 stash->info_ptr = (char *) bfd_alloc (abfd, total_size);
1563 if (stash->info_ptr == NULL)
252b5132
RH
1564 return false;
1565
a092b084
NC
1566 stash->info_ptr_end = stash->info_ptr;
1567
1568 for (msec = find_debug_info (abfd, NULL);
1569 msec;
1570 msec = find_debug_info (abfd, msec))
252b5132 1571 {
a092b084
NC
1572 unsigned long size;
1573 unsigned long start;
1574
1575 size = msec->_raw_size;
1576 if (size == 0)
1577 continue;
1578
1579 start = stash->info_ptr_end - stash->info_ptr;
1580
1581 if (! bfd_get_section_contents (abfd, msec, stash->info_ptr + start, 0, size))
1582 continue;
1583
1584 stash->info_ptr_end = stash->info_ptr + start + size;
252b5132
RH
1585 }
1586
a092b084 1587 BFD_ASSERT (stash->info_ptr_end = stash->info_ptr + total_size);
252b5132 1588 }
a092b084
NC
1589
1590 /* FIXME: There is a problem with the contents of the
1591 .debug_info section. The 'low' and 'high' addresses of the
1592 comp_units are computed by relocs against symbols in the
1593 .text segment. We need these addresses in order to determine
1594 the nearest line number, and so we have to resolve the
1595 relocs. There is a similar problem when the .debug_line
1596 section is processed as well (e.g., there may be relocs
1597 against the operand of the DW_LNE_set_address operator).
98591c73 1598
a092b084 1599 Unfortunately getting hold of the reloc information is hard...
98591c73 1600
a092b084
NC
1601 For now, this means that disassembling object files (as
1602 opposed to fully executables) does not always work as well as
1603 we would like. */
98591c73
KH
1604
1605 /* A null info_ptr indicates that there is no dwarf2 info
a092b084 1606 (or that an error occured while setting up the stash). */
252b5132
RH
1607 if (! stash->info_ptr)
1608 return false;
1609
a092b084 1610 /* Check the previously read comp. units first. */
252b5132 1611 for (each = stash->all_comp_units; each; each = each->next_unit)
f623be2b 1612 if (comp_unit_contains_address (each, addr))
98591c73 1613 return comp_unit_find_nearest_line (each, addr, filename_ptr,
51db3708
NC
1614 functionname_ptr, linenumber_ptr,
1615 stash);
252b5132 1616
a092b084 1617 /* Read each remaining comp. units checking each as they are read. */
252b5132
RH
1618 while (stash->info_ptr < stash->info_ptr_end)
1619 {
1620 struct comp_unit* each;
5e38c3b8 1621 bfd_vma length;
f623be2b 1622 boolean found;
252b5132 1623
5e38c3b8
MM
1624 if (addr_size == 4)
1625 length = read_4_bytes (abfd, stash->info_ptr);
1626 else
1627 length = read_8_bytes (abfd, stash->info_ptr);
1628 stash->info_ptr += addr_size;
252b5132
RH
1629
1630 if (length > 0)
1631 {
51db3708 1632 each = parse_comp_unit (abfd, stash, length, addr_size);
252b5132
RH
1633 stash->info_ptr += length;
1634
1635 if (each)
1636 {
1637 each->next_unit = stash->all_comp_units;
1638 stash->all_comp_units = each;
1639
159002ff
RH
1640 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1641 compilation units. If we don't have them (i.e.,
1642 unit->high == 0), we need to consult the line info
1643 table to see if a compilation unit contains the given
a092b084 1644 address. */
f623be2b 1645 if (each->arange.high > 0)
159002ff
RH
1646 {
1647 if (comp_unit_contains_address (each, addr))
1648 return comp_unit_find_nearest_line (each, addr,
1649 filename_ptr,
1650 functionname_ptr,
51db3708
NC
1651 linenumber_ptr,
1652 stash);
159002ff
RH
1653 }
1654 else
1655 {
1656 found = comp_unit_find_nearest_line (each, addr,
1657 filename_ptr,
1658 functionname_ptr,
51db3708
NC
1659 linenumber_ptr,
1660 stash);
159002ff
RH
1661 if (found)
1662 return true;
1663 }
252b5132
RH
1664 }
1665 }
1666 }
1667
1668 return false;
1669}
This page took 0.185172 seconds and 4 git commands to generate.