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