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