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