Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* DWARF 2 support. |
818a27ac | 2 | Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, |
3db64b00 | 3 | 2004, 2005, 2006, 2007 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 | |
cd123cb7 | 20 | the Free Software Foundation; either version 3 of the License, or (at |
e2f6d277 | 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 | |
cd123cb7 NC |
30 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
31 | MA 02110-1301, USA. */ | |
252b5132 | 32 | |
252b5132 | 33 | #include "sysdep.h" |
3db64b00 | 34 | #include "bfd.h" |
252b5132 RH |
35 | #include "libiberty.h" |
36 | #include "libbfd.h" | |
37 | #include "elf-bfd.h" | |
38 | #include "elf/dwarf2.h" | |
39 | ||
40 | /* The data in the .debug_line statement prologue looks like this. */ | |
a092b084 | 41 | |
252b5132 | 42 | struct line_head |
a092b084 | 43 | { |
d03ba2a1 | 44 | bfd_vma total_length; |
a092b084 | 45 | unsigned short version; |
f46c2da6 | 46 | bfd_vma prologue_length; |
a092b084 NC |
47 | unsigned char minimum_instruction_length; |
48 | unsigned char default_is_stmt; | |
49 | int line_base; | |
50 | unsigned char line_range; | |
51 | unsigned char opcode_base; | |
52 | unsigned char *standard_opcode_lengths; | |
53 | }; | |
54 | ||
55 | /* Attributes have a name and a value. */ | |
56 | ||
252b5132 | 57 | struct attribute |
a092b084 NC |
58 | { |
59 | enum dwarf_attribute name; | |
60 | enum dwarf_form form; | |
61 | union | |
252b5132 | 62 | { |
a092b084 NC |
63 | char *str; |
64 | struct dwarf_block *blk; | |
8ce8c090 AM |
65 | bfd_uint64_t val; |
66 | bfd_int64_t sval; | |
a092b084 NC |
67 | } |
68 | u; | |
69 | }; | |
70 | ||
98591c73 | 71 | /* Blocks are a bunch of untyped bytes. */ |
252b5132 | 72 | struct dwarf_block |
a092b084 NC |
73 | { |
74 | unsigned int size; | |
f075ee0c | 75 | bfd_byte *data; |
a092b084 | 76 | }; |
252b5132 | 77 | |
d4c32a81 L |
78 | struct loadable_section |
79 | { | |
80 | asection *section; | |
81 | bfd_vma adj_vma; | |
82 | }; | |
83 | ||
a092b084 NC |
84 | struct dwarf2_debug |
85 | { | |
86 | /* A list of all previously read comp_units. */ | |
f075ee0c | 87 | struct comp_unit *all_comp_units; |
252b5132 | 88 | |
bd210d54 NC |
89 | /* Last comp unit in list above. */ |
90 | struct comp_unit *last_comp_unit; | |
91 | ||
252b5132 RH |
92 | /* The next unread compilation unit within the .debug_info section. |
93 | Zero indicates that the .debug_info section has not been loaded | |
a092b084 | 94 | into a buffer yet. */ |
f075ee0c | 95 | bfd_byte *info_ptr; |
252b5132 | 96 | |
a092b084 | 97 | /* Pointer to the end of the .debug_info section memory buffer. */ |
f075ee0c | 98 | bfd_byte *info_ptr_end; |
252b5132 | 99 | |
0d161102 NC |
100 | /* Pointer to the bfd, section and address of the beginning of the |
101 | section. The bfd might be different than expected because of | |
102 | gnu_debuglink sections. */ | |
103 | bfd * bfd; | |
f075ee0c AM |
104 | asection *sec; |
105 | bfd_byte *sec_info_ptr; | |
f2363ce5 AO |
106 | |
107 | /* Pointer to the symbol table. */ | |
f075ee0c | 108 | asymbol **syms; |
f2363ce5 | 109 | |
a092b084 | 110 | /* Pointer to the .debug_abbrev section loaded into memory. */ |
f075ee0c | 111 | bfd_byte *dwarf_abbrev_buffer; |
252b5132 | 112 | |
a092b084 | 113 | /* Length of the loaded .debug_abbrev section. */ |
252b5132 | 114 | unsigned long dwarf_abbrev_size; |
69dd2e2d RH |
115 | |
116 | /* Buffer for decode_line_info. */ | |
f075ee0c | 117 | bfd_byte *dwarf_line_buffer; |
ccdb16fc JW |
118 | |
119 | /* Length of the loaded .debug_line section. */ | |
120 | unsigned long dwarf_line_size; | |
d03ba2a1 JJ |
121 | |
122 | /* Pointer to the .debug_str section loaded into memory. */ | |
f075ee0c | 123 | bfd_byte *dwarf_str_buffer; |
d03ba2a1 JJ |
124 | |
125 | /* Length of the loaded .debug_str section. */ | |
126 | unsigned long dwarf_str_size; | |
a13afe8e FF |
127 | |
128 | /* Pointer to the .debug_ranges section loaded into memory. */ | |
129 | bfd_byte *dwarf_ranges_buffer; | |
130 | ||
131 | /* Length of the loaded .debug_ranges section. */ | |
132 | unsigned long dwarf_ranges_size; | |
4ab527b0 FF |
133 | |
134 | /* If the most recent call to bfd_find_nearest_line was given an | |
135 | address in an inlined function, preserve a pointer into the | |
136 | calling chain for subsequent calls to bfd_find_inliner_info to | |
137 | use. */ | |
138 | struct funcinfo *inliner_chain; | |
d4c32a81 L |
139 | |
140 | /* Number of loadable sections. */ | |
141 | unsigned int loadable_section_count; | |
142 | ||
143 | /* Array of loadable sections. */ | |
144 | struct loadable_section *loadable_sections; | |
bd210d54 NC |
145 | |
146 | /* Number of times find_line is called. This is used in | |
147 | the heuristic for enabling the info hash tables. */ | |
148 | int info_hash_count; | |
149 | ||
150 | #define STASH_INFO_HASH_TRIGGER 100 | |
151 | ||
152 | /* Hash table mapping symbol names to function infos. */ | |
153 | struct info_hash_table *funcinfo_hash_table; | |
154 | ||
155 | /* Hash table mapping symbol names to variable infos. */ | |
156 | struct info_hash_table *varinfo_hash_table; | |
157 | ||
158 | /* Head of comp_unit list in the last hash table update. */ | |
159 | struct comp_unit *hash_units_head; | |
160 | ||
161 | /* Status of info hash. */ | |
162 | int info_hash_status; | |
163 | #define STASH_INFO_HASH_OFF 0 | |
164 | #define STASH_INFO_HASH_ON 1 | |
165 | #define STASH_INFO_HASH_DISABLED 2 | |
252b5132 RH |
166 | }; |
167 | ||
a092b084 NC |
168 | struct arange |
169 | { | |
f623be2b RH |
170 | struct arange *next; |
171 | bfd_vma low; | |
172 | bfd_vma high; | |
173 | }; | |
252b5132 | 174 | |
252b5132 | 175 | /* A minimal decoding of DWARF2 compilation units. We only decode |
a092b084 | 176 | what's needed to get to the line number information. */ |
252b5132 | 177 | |
a092b084 NC |
178 | struct comp_unit |
179 | { | |
180 | /* Chain the previously read compilation units. */ | |
f075ee0c | 181 | struct comp_unit *next_unit; |
252b5132 | 182 | |
bd210d54 NC |
183 | /* Likewise, chain the compilation unit read after this one. |
184 | The comp units are stored in reversed reading order. */ | |
185 | struct comp_unit *prev_unit; | |
186 | ||
2ae727ad | 187 | /* Keep the bfd convenient (for memory allocation). */ |
f075ee0c | 188 | bfd *abfd; |
252b5132 | 189 | |
09a9d560 | 190 | /* The lowest and highest addresses contained in this compilation |
a092b084 | 191 | unit as specified in the compilation unit header. */ |
f623be2b | 192 | struct arange arange; |
252b5132 | 193 | |
a092b084 | 194 | /* The DW_AT_name attribute (for error messages). */ |
f075ee0c | 195 | char *name; |
252b5132 | 196 | |
a092b084 | 197 | /* The abbrev hash table. */ |
f075ee0c | 198 | struct abbrev_info **abbrevs; |
252b5132 | 199 | |
a092b084 | 200 | /* Note that an error was found by comp_unit_find_nearest_line. */ |
252b5132 RH |
201 | int error; |
202 | ||
a092b084 | 203 | /* The DW_AT_comp_dir attribute. */ |
f075ee0c | 204 | char *comp_dir; |
252b5132 | 205 | |
b34976b6 | 206 | /* TRUE if there is a line number table associated with this comp. unit. */ |
252b5132 | 207 | int stmtlist; |
98591c73 | 208 | |
c0c28ab8 L |
209 | /* Pointer to the current comp_unit so that we can find a given entry |
210 | by its reference. */ | |
f075ee0c | 211 | bfd_byte *info_ptr_unit; |
c0c28ab8 | 212 | |
a092b084 | 213 | /* The offset into .debug_line of the line number table. */ |
252b5132 RH |
214 | unsigned long line_offset; |
215 | ||
a092b084 | 216 | /* Pointer to the first child die for the comp unit. */ |
f075ee0c | 217 | bfd_byte *first_child_die_ptr; |
252b5132 | 218 | |
a092b084 | 219 | /* The end of the comp unit. */ |
f075ee0c | 220 | bfd_byte *end_ptr; |
252b5132 | 221 | |
a092b084 | 222 | /* The decoded line number, NULL if not yet decoded. */ |
f075ee0c | 223 | struct line_info_table *line_table; |
252b5132 | 224 | |
a092b084 | 225 | /* A list of the functions found in this comp. unit. */ |
f075ee0c | 226 | struct funcinfo *function_table; |
252b5132 | 227 | |
5420f73d L |
228 | /* A list of the variables found in this comp. unit. */ |
229 | struct varinfo *variable_table; | |
230 | ||
d03ba2a1 JJ |
231 | /* Pointer to dwarf2_debug structure. */ |
232 | struct dwarf2_debug *stash; | |
233 | ||
a092b084 | 234 | /* Address size for this unit - from unit header. */ |
252b5132 | 235 | unsigned char addr_size; |
d03ba2a1 JJ |
236 | |
237 | /* Offset size for this unit - from unit header. */ | |
238 | unsigned char offset_size; | |
a13afe8e FF |
239 | |
240 | /* Base address for this unit - from DW_AT_low_pc attribute of | |
241 | DW_TAG_compile_unit DIE */ | |
242 | bfd_vma base_address; | |
bd210d54 NC |
243 | |
244 | /* TRUE if symbols are cached in hash table for faster lookup by name. */ | |
245 | bfd_boolean cached; | |
252b5132 RH |
246 | }; |
247 | ||
a7b97311 AM |
248 | /* This data structure holds the information of an abbrev. */ |
249 | struct abbrev_info | |
250 | { | |
251 | unsigned int number; /* Number identifying abbrev. */ | |
252 | enum dwarf_tag tag; /* DWARF tag. */ | |
253 | int has_children; /* Boolean. */ | |
254 | unsigned int num_attrs; /* Number of attributes. */ | |
255 | struct attr_abbrev *attrs; /* An array of attribute descriptions. */ | |
256 | struct abbrev_info *next; /* Next in chain. */ | |
257 | }; | |
258 | ||
259 | struct attr_abbrev | |
260 | { | |
261 | enum dwarf_attribute name; | |
262 | enum dwarf_form form; | |
263 | }; | |
264 | ||
265 | #ifndef ABBREV_HASH_SIZE | |
266 | #define ABBREV_HASH_SIZE 121 | |
267 | #endif | |
268 | #ifndef ATTR_ALLOC_CHUNK | |
269 | #define ATTR_ALLOC_CHUNK 4 | |
270 | #endif | |
271 | ||
bd210d54 NC |
272 | /* Variable and function hash tables. This is used to speed up look-up |
273 | in lookup_symbol_in_var_table() and lookup_symbol_in_function_table(). | |
274 | In order to share code between variable and function infos, we use | |
275 | a list of untyped pointer for all variable/function info associated with | |
276 | a symbol. We waste a bit of memory for list with one node but that | |
277 | simplifies the code. */ | |
278 | ||
279 | struct info_list_node | |
280 | { | |
281 | struct info_list_node *next; | |
282 | void *info; | |
283 | }; | |
284 | ||
285 | /* Info hash entry. */ | |
286 | struct info_hash_entry | |
287 | { | |
288 | struct bfd_hash_entry root; | |
289 | struct info_list_node *head; | |
290 | }; | |
291 | ||
292 | struct info_hash_table | |
293 | { | |
294 | struct bfd_hash_table base; | |
295 | }; | |
296 | ||
297 | /* Function to create a new entry in info hash table. */ | |
298 | ||
299 | static struct bfd_hash_entry * | |
300 | info_hash_table_newfunc (struct bfd_hash_entry *entry, | |
301 | struct bfd_hash_table *table, | |
302 | const char *string) | |
303 | { | |
304 | struct info_hash_entry *ret = (struct info_hash_entry *) entry; | |
305 | ||
306 | /* Allocate the structure if it has not already been allocated by a | |
307 | derived class. */ | |
308 | if (ret == NULL) | |
309 | { | |
310 | ret = bfd_hash_allocate (table, sizeof (* ret)); | |
311 | if (ret == NULL) | |
312 | return NULL; | |
313 | } | |
314 | ||
315 | /* Call the allocation method of the base class. */ | |
316 | ret = ((struct info_hash_entry *) | |
317 | bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string)); | |
318 | ||
319 | /* Initialize the local fields here. */ | |
320 | if (ret) | |
321 | ret->head = NULL; | |
322 | ||
323 | return (struct bfd_hash_entry *) ret; | |
324 | } | |
325 | ||
326 | /* Function to create a new info hash table. It returns a pointer to the | |
327 | newly created table or NULL if there is any error. We need abfd | |
328 | solely for memory allocation. */ | |
329 | ||
330 | static struct info_hash_table * | |
331 | create_info_hash_table (bfd *abfd) | |
332 | { | |
333 | struct info_hash_table *hash_table; | |
334 | ||
335 | hash_table = bfd_alloc (abfd, sizeof (struct info_hash_table)); | |
336 | if (!hash_table) | |
337 | return hash_table; | |
338 | ||
339 | if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc, | |
340 | sizeof (struct info_hash_entry))) | |
341 | { | |
342 | bfd_release (abfd, hash_table); | |
343 | return NULL; | |
344 | } | |
345 | ||
346 | return hash_table; | |
347 | } | |
348 | ||
349 | /* Insert an info entry into an info hash table. We do not check of | |
350 | duplicate entries. Also, the caller need to guarantee that the | |
351 | right type of info in inserted as info is passed as a void* pointer. | |
352 | This function returns true if there is no error. */ | |
353 | ||
354 | static bfd_boolean | |
355 | insert_info_hash_table (struct info_hash_table *hash_table, | |
356 | const char *key, | |
357 | void *info, | |
358 | bfd_boolean copy_p) | |
359 | { | |
360 | struct info_hash_entry *entry; | |
361 | struct info_list_node *node; | |
362 | ||
363 | entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, | |
364 | key, TRUE, copy_p); | |
365 | if (!entry) | |
366 | return FALSE; | |
367 | ||
368 | node = bfd_hash_allocate (&hash_table->base, sizeof (*node)); | |
369 | if (!node) | |
370 | return FALSE; | |
371 | ||
372 | node->info = info; | |
373 | node->next = entry->head; | |
374 | entry->head = node; | |
375 | ||
376 | return TRUE; | |
377 | } | |
378 | ||
379 | /* Look up an info entry list from an info hash table. Return NULL | |
380 | if there is none. */ | |
381 | ||
382 | static struct info_list_node * | |
383 | lookup_info_hash_table (struct info_hash_table *hash_table, const char *key) | |
384 | { | |
385 | struct info_hash_entry *entry; | |
386 | ||
387 | entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key, | |
388 | FALSE, FALSE); | |
389 | return entry ? entry->head : NULL; | |
390 | } | |
391 | ||
98591c73 KH |
392 | /* VERBATIM |
393 | The following function up to the END VERBATIM mark are | |
a092b084 | 394 | copied directly from dwarf2read.c. */ |
252b5132 | 395 | |
a092b084 | 396 | /* Read dwarf information from a buffer. */ |
252b5132 RH |
397 | |
398 | static unsigned int | |
f075ee0c | 399 | read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf) |
252b5132 | 400 | { |
818a27ac | 401 | return bfd_get_8 (abfd, buf); |
252b5132 RH |
402 | } |
403 | ||
404 | static int | |
f075ee0c | 405 | read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf) |
252b5132 | 406 | { |
818a27ac | 407 | return bfd_get_signed_8 (abfd, buf); |
252b5132 RH |
408 | } |
409 | ||
410 | static unsigned int | |
f075ee0c | 411 | read_2_bytes (bfd *abfd, bfd_byte *buf) |
252b5132 | 412 | { |
818a27ac | 413 | return bfd_get_16 (abfd, buf); |
252b5132 RH |
414 | } |
415 | ||
252b5132 | 416 | static unsigned int |
f075ee0c | 417 | read_4_bytes (bfd *abfd, bfd_byte *buf) |
252b5132 | 418 | { |
818a27ac | 419 | return bfd_get_32 (abfd, buf); |
252b5132 RH |
420 | } |
421 | ||
8ce8c090 | 422 | static bfd_uint64_t |
f075ee0c | 423 | read_8_bytes (bfd *abfd, bfd_byte *buf) |
252b5132 | 424 | { |
818a27ac | 425 | return bfd_get_64 (abfd, buf); |
252b5132 RH |
426 | } |
427 | ||
f075ee0c | 428 | static bfd_byte * |
818a27ac | 429 | read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED, |
f075ee0c | 430 | bfd_byte *buf, |
818a27ac | 431 | unsigned int size ATTRIBUTE_UNUSED) |
252b5132 RH |
432 | { |
433 | /* If the size of a host char is 8 bits, we can return a pointer | |
434 | to the buffer, otherwise we have to copy the data to a buffer | |
435 | allocated on the temporary obstack. */ | |
436 | return buf; | |
437 | } | |
438 | ||
439 | static char * | |
818a27ac | 440 | read_string (bfd *abfd ATTRIBUTE_UNUSED, |
f075ee0c | 441 | bfd_byte *buf, |
818a27ac | 442 | unsigned int *bytes_read_ptr) |
252b5132 | 443 | { |
d03ba2a1 | 444 | /* Return a pointer to the embedded string. */ |
f075ee0c AM |
445 | char *str = (char *) buf; |
446 | if (*str == '\0') | |
252b5132 RH |
447 | { |
448 | *bytes_read_ptr = 1; | |
449 | return NULL; | |
450 | } | |
98591c73 | 451 | |
f075ee0c AM |
452 | *bytes_read_ptr = strlen (str) + 1; |
453 | return str; | |
252b5132 RH |
454 | } |
455 | ||
d03ba2a1 | 456 | static char * |
818a27ac | 457 | read_indirect_string (struct comp_unit* unit, |
f075ee0c | 458 | bfd_byte *buf, |
818a27ac | 459 | unsigned int *bytes_read_ptr) |
d03ba2a1 | 460 | { |
8ce8c090 | 461 | bfd_uint64_t offset; |
d03ba2a1 | 462 | struct dwarf2_debug *stash = unit->stash; |
f075ee0c | 463 | char *str; |
d03ba2a1 JJ |
464 | |
465 | if (unit->offset_size == 4) | |
466 | offset = read_4_bytes (unit->abfd, buf); | |
467 | else | |
468 | offset = read_8_bytes (unit->abfd, buf); | |
469 | *bytes_read_ptr = unit->offset_size; | |
470 | ||
471 | if (! stash->dwarf_str_buffer) | |
472 | { | |
473 | asection *msec; | |
474 | bfd *abfd = unit->abfd; | |
eea6121a | 475 | bfd_size_type sz; |
d03ba2a1 JJ |
476 | |
477 | msec = bfd_get_section_by_name (abfd, ".debug_str"); | |
478 | if (! msec) | |
479 | { | |
480 | (*_bfd_error_handler) | |
481 | (_("Dwarf Error: Can't find .debug_str section.")); | |
482 | bfd_set_error (bfd_error_bad_value); | |
483 | return NULL; | |
484 | } | |
485 | ||
eea6121a AM |
486 | sz = msec->rawsize ? msec->rawsize : msec->size; |
487 | stash->dwarf_str_size = sz; | |
488 | stash->dwarf_str_buffer = bfd_alloc (abfd, sz); | |
36868d45 | 489 | if (! stash->dwarf_str_buffer) |
d03ba2a1 JJ |
490 | return NULL; |
491 | ||
9f632188 | 492 | if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer, |
eea6121a | 493 | 0, sz)) |
d03ba2a1 JJ |
494 | return NULL; |
495 | } | |
496 | ||
497 | if (offset >= stash->dwarf_str_size) | |
498 | { | |
f46c2da6 AM |
499 | (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."), |
500 | (unsigned long) offset, stash->dwarf_str_size); | |
d03ba2a1 JJ |
501 | bfd_set_error (bfd_error_bad_value); |
502 | return NULL; | |
503 | } | |
504 | ||
f075ee0c AM |
505 | str = (char *) stash->dwarf_str_buffer + offset; |
506 | if (*str == '\0') | |
d03ba2a1 | 507 | return NULL; |
f075ee0c | 508 | return str; |
d03ba2a1 JJ |
509 | } |
510 | ||
252b5132 RH |
511 | /* END VERBATIM */ |
512 | ||
8ce8c090 | 513 | static bfd_uint64_t |
f075ee0c | 514 | read_address (struct comp_unit *unit, bfd_byte *buf) |
252b5132 | 515 | { |
0af4cd7c PK |
516 | int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma; |
517 | ||
518 | if (signed_vma) | |
519 | { | |
520 | switch (unit->addr_size) | |
521 | { | |
522 | case 8: | |
523 | return bfd_get_signed_64 (unit->abfd, buf); | |
524 | case 4: | |
525 | return bfd_get_signed_32 (unit->abfd, buf); | |
526 | case 2: | |
527 | return bfd_get_signed_16 (unit->abfd, buf); | |
528 | default: | |
529 | abort (); | |
530 | } | |
531 | } | |
532 | else | |
252b5132 | 533 | { |
0af4cd7c PK |
534 | switch (unit->addr_size) |
535 | { | |
536 | case 8: | |
537 | return bfd_get_64 (unit->abfd, buf); | |
538 | case 4: | |
539 | return bfd_get_32 (unit->abfd, buf); | |
540 | case 2: | |
541 | return bfd_get_16 (unit->abfd, buf); | |
542 | default: | |
543 | abort (); | |
544 | } | |
252b5132 | 545 | } |
252b5132 RH |
546 | } |
547 | ||
252b5132 RH |
548 | /* Lookup an abbrev_info structure in the abbrev hash table. */ |
549 | ||
550 | static struct abbrev_info * | |
818a27ac | 551 | lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs) |
252b5132 RH |
552 | { |
553 | unsigned int hash_number; | |
554 | struct abbrev_info *abbrev; | |
555 | ||
556 | hash_number = number % ABBREV_HASH_SIZE; | |
557 | abbrev = abbrevs[hash_number]; | |
558 | ||
559 | while (abbrev) | |
560 | { | |
561 | if (abbrev->number == number) | |
562 | return abbrev; | |
563 | else | |
564 | abbrev = abbrev->next; | |
565 | } | |
98591c73 | 566 | |
252b5132 RH |
567 | return NULL; |
568 | } | |
569 | ||
570 | /* In DWARF version 2, the description of the debugging information is | |
571 | stored in a separate .debug_abbrev section. Before we read any | |
572 | dies from a section we read in all abbreviations and install them | |
573 | in a hash table. */ | |
574 | ||
575 | static struct abbrev_info** | |
8ce8c090 | 576 | read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash) |
252b5132 RH |
577 | { |
578 | struct abbrev_info **abbrevs; | |
f075ee0c | 579 | bfd_byte *abbrev_ptr; |
252b5132 RH |
580 | struct abbrev_info *cur_abbrev; |
581 | unsigned int abbrev_number, bytes_read, abbrev_name; | |
582 | unsigned int abbrev_form, hash_number; | |
dc810e39 | 583 | bfd_size_type amt; |
252b5132 RH |
584 | |
585 | if (! stash->dwarf_abbrev_buffer) | |
586 | { | |
587 | asection *msec; | |
588 | ||
589 | msec = bfd_get_section_by_name (abfd, ".debug_abbrev"); | |
590 | if (! msec) | |
591 | { | |
592 | (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section.")); | |
593 | bfd_set_error (bfd_error_bad_value); | |
594 | return 0; | |
595 | } | |
98591c73 | 596 | |
eea6121a | 597 | stash->dwarf_abbrev_size = msec->size; |
6e84a906 DJ |
598 | stash->dwarf_abbrev_buffer |
599 | = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, | |
600 | stash->syms); | |
252b5132 RH |
601 | if (! stash->dwarf_abbrev_buffer) |
602 | return 0; | |
252b5132 RH |
603 | } |
604 | ||
f5198f61 | 605 | if (offset >= stash->dwarf_abbrev_size) |
252b5132 | 606 | { |
f46c2da6 AM |
607 | (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."), |
608 | (unsigned long) offset, stash->dwarf_abbrev_size); | |
252b5132 RH |
609 | bfd_set_error (bfd_error_bad_value); |
610 | return 0; | |
611 | } | |
612 | ||
dc810e39 | 613 | amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE; |
818a27ac | 614 | abbrevs = bfd_zalloc (abfd, amt); |
252b5132 RH |
615 | |
616 | abbrev_ptr = stash->dwarf_abbrev_buffer + offset; | |
617 | abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
618 | abbrev_ptr += bytes_read; | |
619 | ||
a092b084 | 620 | /* Loop until we reach an abbrev number of 0. */ |
252b5132 RH |
621 | while (abbrev_number) |
622 | { | |
dc810e39 | 623 | amt = sizeof (struct abbrev_info); |
818a27ac | 624 | cur_abbrev = bfd_zalloc (abfd, amt); |
252b5132 | 625 | |
a092b084 | 626 | /* Read in abbrev header. */ |
252b5132 | 627 | cur_abbrev->number = abbrev_number; |
d45913a0 DA |
628 | cur_abbrev->tag = (enum dwarf_tag) |
629 | read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
252b5132 RH |
630 | abbrev_ptr += bytes_read; |
631 | cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr); | |
632 | abbrev_ptr += 1; | |
633 | ||
a092b084 | 634 | /* Now read in declarations. */ |
252b5132 RH |
635 | abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
636 | abbrev_ptr += bytes_read; | |
637 | abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
638 | abbrev_ptr += bytes_read; | |
98591c73 | 639 | |
252b5132 RH |
640 | while (abbrev_name) |
641 | { | |
642 | if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0) | |
643 | { | |
35330cce NC |
644 | struct attr_abbrev *tmp; |
645 | ||
dc810e39 AM |
646 | amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK; |
647 | amt *= sizeof (struct attr_abbrev); | |
35330cce NC |
648 | tmp = bfd_realloc (cur_abbrev->attrs, amt); |
649 | if (tmp == NULL) | |
d8d1c398 AM |
650 | { |
651 | size_t i; | |
652 | ||
653 | for (i = 0; i < ABBREV_HASH_SIZE; i++) | |
654 | { | |
655 | struct abbrev_info *abbrev = abbrevs[i]; | |
656 | ||
657 | while (abbrev) | |
658 | { | |
659 | free (abbrev->attrs); | |
660 | abbrev = abbrev->next; | |
661 | } | |
662 | } | |
663 | return NULL; | |
664 | } | |
35330cce | 665 | cur_abbrev->attrs = tmp; |
252b5132 | 666 | } |
98591c73 | 667 | |
d45913a0 DA |
668 | cur_abbrev->attrs[cur_abbrev->num_attrs].name |
669 | = (enum dwarf_attribute) abbrev_name; | |
670 | cur_abbrev->attrs[cur_abbrev->num_attrs++].form | |
671 | = (enum dwarf_form) abbrev_form; | |
252b5132 RH |
672 | abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
673 | abbrev_ptr += bytes_read; | |
674 | abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
675 | abbrev_ptr += bytes_read; | |
676 | } | |
677 | ||
678 | hash_number = abbrev_number % ABBREV_HASH_SIZE; | |
679 | cur_abbrev->next = abbrevs[hash_number]; | |
680 | abbrevs[hash_number] = cur_abbrev; | |
681 | ||
682 | /* Get next abbreviation. | |
e82ce529 | 683 | Under Irix6 the abbreviations for a compilation unit are not |
252b5132 RH |
684 | always properly terminated with an abbrev number of 0. |
685 | Exit loop if we encounter an abbreviation which we have | |
686 | already read (which means we are about to read the abbreviations | |
687 | for the next compile unit) or if the end of the abbreviation | |
688 | table is reached. */ | |
689 | if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer) | |
d8d1c398 | 690 | >= stash->dwarf_abbrev_size) |
252b5132 RH |
691 | break; |
692 | abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
693 | abbrev_ptr += bytes_read; | |
694 | if (lookup_abbrev (abbrev_number,abbrevs) != NULL) | |
695 | break; | |
696 | } | |
697 | ||
698 | return abbrevs; | |
699 | } | |
700 | ||
cf716c56 | 701 | /* Read an attribute value described by an attribute form. */ |
252b5132 | 702 | |
f075ee0c | 703 | static bfd_byte * |
818a27ac AM |
704 | read_attribute_value (struct attribute *attr, |
705 | unsigned form, | |
706 | struct comp_unit *unit, | |
f075ee0c | 707 | bfd_byte *info_ptr) |
252b5132 RH |
708 | { |
709 | bfd *abfd = unit->abfd; | |
710 | unsigned int bytes_read; | |
711 | struct dwarf_block *blk; | |
dc810e39 | 712 | bfd_size_type amt; |
252b5132 | 713 | |
d45913a0 | 714 | attr->form = (enum dwarf_form) form; |
98591c73 | 715 | |
cf716c56 | 716 | switch (form) |
252b5132 RH |
717 | { |
718 | case DW_FORM_addr: | |
0cc1cf99 | 719 | /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */ |
252b5132 | 720 | case DW_FORM_ref_addr: |
482e2e37 | 721 | attr->u.val = read_address (unit, info_ptr); |
252b5132 RH |
722 | info_ptr += unit->addr_size; |
723 | break; | |
724 | case DW_FORM_block2: | |
dc810e39 | 725 | amt = sizeof (struct dwarf_block); |
818a27ac | 726 | blk = bfd_alloc (abfd, amt); |
252b5132 RH |
727 | blk->size = read_2_bytes (abfd, info_ptr); |
728 | info_ptr += 2; | |
729 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); | |
730 | info_ptr += blk->size; | |
482e2e37 | 731 | attr->u.blk = blk; |
252b5132 RH |
732 | break; |
733 | case DW_FORM_block4: | |
dc810e39 | 734 | amt = sizeof (struct dwarf_block); |
818a27ac | 735 | blk = bfd_alloc (abfd, amt); |
252b5132 RH |
736 | blk->size = read_4_bytes (abfd, info_ptr); |
737 | info_ptr += 4; | |
738 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); | |
739 | info_ptr += blk->size; | |
482e2e37 | 740 | attr->u.blk = blk; |
252b5132 RH |
741 | break; |
742 | case DW_FORM_data2: | |
482e2e37 | 743 | attr->u.val = read_2_bytes (abfd, info_ptr); |
252b5132 RH |
744 | info_ptr += 2; |
745 | break; | |
746 | case DW_FORM_data4: | |
482e2e37 | 747 | attr->u.val = read_4_bytes (abfd, info_ptr); |
252b5132 RH |
748 | info_ptr += 4; |
749 | break; | |
750 | case DW_FORM_data8: | |
482e2e37 | 751 | attr->u.val = read_8_bytes (abfd, info_ptr); |
252b5132 RH |
752 | info_ptr += 8; |
753 | break; | |
754 | case DW_FORM_string: | |
482e2e37 | 755 | attr->u.str = read_string (abfd, info_ptr, &bytes_read); |
252b5132 RH |
756 | info_ptr += bytes_read; |
757 | break; | |
d03ba2a1 | 758 | case DW_FORM_strp: |
482e2e37 | 759 | attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read); |
d03ba2a1 JJ |
760 | info_ptr += bytes_read; |
761 | break; | |
252b5132 | 762 | case DW_FORM_block: |
dc810e39 | 763 | amt = sizeof (struct dwarf_block); |
818a27ac | 764 | blk = bfd_alloc (abfd, amt); |
252b5132 RH |
765 | blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
766 | info_ptr += bytes_read; | |
767 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); | |
768 | info_ptr += blk->size; | |
482e2e37 | 769 | attr->u.blk = blk; |
252b5132 RH |
770 | break; |
771 | case DW_FORM_block1: | |
dc810e39 | 772 | amt = sizeof (struct dwarf_block); |
818a27ac | 773 | blk = bfd_alloc (abfd, amt); |
252b5132 RH |
774 | blk->size = read_1_byte (abfd, info_ptr); |
775 | info_ptr += 1; | |
776 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); | |
777 | info_ptr += blk->size; | |
482e2e37 | 778 | attr->u.blk = blk; |
252b5132 RH |
779 | break; |
780 | case DW_FORM_data1: | |
482e2e37 | 781 | attr->u.val = read_1_byte (abfd, info_ptr); |
252b5132 RH |
782 | info_ptr += 1; |
783 | break; | |
784 | case DW_FORM_flag: | |
482e2e37 | 785 | attr->u.val = read_1_byte (abfd, info_ptr); |
252b5132 RH |
786 | info_ptr += 1; |
787 | break; | |
788 | case DW_FORM_sdata: | |
482e2e37 | 789 | attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read); |
252b5132 RH |
790 | info_ptr += bytes_read; |
791 | break; | |
792 | case DW_FORM_udata: | |
482e2e37 | 793 | attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
252b5132 RH |
794 | info_ptr += bytes_read; |
795 | break; | |
796 | case DW_FORM_ref1: | |
482e2e37 | 797 | attr->u.val = read_1_byte (abfd, info_ptr); |
252b5132 RH |
798 | info_ptr += 1; |
799 | break; | |
800 | case DW_FORM_ref2: | |
482e2e37 | 801 | attr->u.val = read_2_bytes (abfd, info_ptr); |
252b5132 RH |
802 | info_ptr += 2; |
803 | break; | |
804 | case DW_FORM_ref4: | |
482e2e37 | 805 | attr->u.val = read_4_bytes (abfd, info_ptr); |
252b5132 RH |
806 | info_ptr += 4; |
807 | break; | |
81edd86d | 808 | case DW_FORM_ref8: |
482e2e37 | 809 | attr->u.val = read_8_bytes (abfd, info_ptr); |
81edd86d MM |
810 | info_ptr += 8; |
811 | break; | |
252b5132 | 812 | case DW_FORM_ref_udata: |
482e2e37 | 813 | attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
252b5132 RH |
814 | info_ptr += bytes_read; |
815 | break; | |
252b5132 | 816 | case DW_FORM_indirect: |
cf716c56 RH |
817 | form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
818 | info_ptr += bytes_read; | |
819 | info_ptr = read_attribute_value (attr, form, unit, info_ptr); | |
820 | break; | |
252b5132 | 821 | default: |
f46c2da6 | 822 | (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."), |
cf716c56 | 823 | form); |
252b5132 RH |
824 | bfd_set_error (bfd_error_bad_value); |
825 | } | |
826 | return info_ptr; | |
827 | } | |
828 | ||
cf716c56 RH |
829 | /* Read an attribute described by an abbreviated attribute. */ |
830 | ||
f075ee0c | 831 | static bfd_byte * |
818a27ac AM |
832 | read_attribute (struct attribute *attr, |
833 | struct attr_abbrev *abbrev, | |
834 | struct comp_unit *unit, | |
f075ee0c | 835 | bfd_byte *info_ptr) |
cf716c56 RH |
836 | { |
837 | attr->name = abbrev->name; | |
838 | info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr); | |
839 | return info_ptr; | |
840 | } | |
841 | ||
a092b084 | 842 | /* Source line information table routines. */ |
252b5132 RH |
843 | |
844 | #define FILE_ALLOC_CHUNK 5 | |
845 | #define DIR_ALLOC_CHUNK 5 | |
846 | ||
a092b084 NC |
847 | struct line_info |
848 | { | |
252b5132 | 849 | struct line_info* prev_line; |
252b5132 | 850 | bfd_vma address; |
f075ee0c | 851 | char *filename; |
252b5132 RH |
852 | unsigned int line; |
853 | unsigned int column; | |
a092b084 | 854 | int end_sequence; /* End of (sequential) code sequence. */ |
252b5132 RH |
855 | }; |
856 | ||
a092b084 NC |
857 | struct fileinfo |
858 | { | |
252b5132 RH |
859 | char *name; |
860 | unsigned int dir; | |
861 | unsigned int time; | |
862 | unsigned int size; | |
863 | }; | |
864 | ||
a092b084 NC |
865 | struct line_info_table |
866 | { | |
252b5132 | 867 | bfd* abfd; |
252b5132 RH |
868 | unsigned int num_files; |
869 | unsigned int num_dirs; | |
f075ee0c AM |
870 | char *comp_dir; |
871 | char **dirs; | |
252b5132 | 872 | struct fileinfo* files; |
e82ce529 AM |
873 | struct line_info* last_line; /* largest VMA */ |
874 | struct line_info* lcl_head; /* local head; used in 'add_line_info' */ | |
252b5132 RH |
875 | }; |
876 | ||
4ab527b0 FF |
877 | /* Remember some information about each function. If the function is |
878 | inlined (DW_TAG_inlined_subroutine) it may have two additional | |
879 | attributes, DW_AT_call_file and DW_AT_call_line, which specify the | |
880 | source code location where this function was inlined. */ | |
881 | ||
1ee24f27 DJ |
882 | struct funcinfo |
883 | { | |
4ab527b0 FF |
884 | struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */ |
885 | struct funcinfo *caller_func; /* Pointer to function one scope higher */ | |
886 | char *caller_file; /* Source location file name where caller_func inlines this func */ | |
887 | int caller_line; /* Source location line number where caller_func inlines this func */ | |
5420f73d L |
888 | char *file; /* Source location file name */ |
889 | int line; /* Source location line number */ | |
4ab527b0 | 890 | int tag; |
f075ee0c | 891 | char *name; |
a13afe8e | 892 | struct arange arange; |
5420f73d L |
893 | asection *sec; /* Where the symbol is defined */ |
894 | }; | |
895 | ||
896 | struct varinfo | |
897 | { | |
898 | /* Pointer to previous variable in list of all variables */ | |
899 | struct varinfo *prev_var; | |
900 | /* Source location file name */ | |
901 | char *file; | |
902 | /* Source location line number */ | |
903 | int line; | |
904 | int tag; | |
905 | char *name; | |
5cf2e3f0 | 906 | bfd_vma addr; |
5420f73d L |
907 | /* Where the symbol is defined */ |
908 | asection *sec; | |
909 | /* Is this a stack variable? */ | |
910 | unsigned int stack: 1; | |
1ee24f27 DJ |
911 | }; |
912 | ||
d4c32a81 L |
913 | /* Return TRUE if NEW_LINE should sort after LINE. */ |
914 | ||
915 | static inline bfd_boolean | |
916 | new_line_sorts_after (struct line_info *new_line, struct line_info *line) | |
917 | { | |
918 | return (new_line->address > line->address | |
919 | || (new_line->address == line->address | |
920 | && new_line->end_sequence < line->end_sequence)); | |
921 | } | |
922 | ||
923 | ||
af3ef9fe NC |
924 | /* Adds a new entry to the line_info list in the line_info_table, ensuring |
925 | that the list is sorted. Note that the line_info list is sorted from | |
926 | highest to lowest VMA (with possible duplicates); that is, | |
927 | line_info->prev_line always accesses an equal or smaller VMA. */ | |
928 | ||
98591c73 | 929 | static void |
818a27ac AM |
930 | add_line_info (struct line_info_table *table, |
931 | bfd_vma address, | |
932 | char *filename, | |
933 | unsigned int line, | |
934 | unsigned int column, | |
935 | int end_sequence) | |
252b5132 | 936 | { |
dc810e39 | 937 | bfd_size_type amt = sizeof (struct line_info); |
818a27ac | 938 | struct line_info* info = bfd_alloc (table->abfd, amt); |
252b5132 | 939 | |
d4c32a81 L |
940 | /* Set member data of 'info'. */ |
941 | info->address = address; | |
942 | info->line = line; | |
943 | info->column = column; | |
944 | info->end_sequence = end_sequence; | |
945 | ||
946 | if (filename && filename[0]) | |
947 | { | |
948 | info->filename = bfd_alloc (table->abfd, strlen (filename) + 1); | |
949 | if (info->filename) | |
950 | strcpy (info->filename, filename); | |
951 | } | |
952 | else | |
953 | info->filename = NULL; | |
954 | ||
e82ce529 AM |
955 | /* Find the correct location for 'info'. Normally we will receive |
956 | new line_info data 1) in order and 2) with increasing VMAs. | |
957 | However some compilers break the rules (cf. decode_line_info) and | |
958 | so we include some heuristics for quickly finding the correct | |
959 | location for 'info'. In particular, these heuristics optimize for | |
960 | the common case in which the VMA sequence that we receive is a | |
961 | list of locally sorted VMAs such as | |
962 | p...z a...j (where a < j < p < z) | |
252b5132 | 963 | |
e82ce529 AM |
964 | Note: table->lcl_head is used to head an *actual* or *possible* |
965 | sequence within the list (such as a...j) that is not directly | |
966 | headed by table->last_line | |
967 | ||
968 | Note: we may receive duplicate entries from 'decode_line_info'. */ | |
969 | ||
d8d1c398 AM |
970 | if (!table->last_line |
971 | || new_line_sorts_after (info, table->last_line)) | |
972 | { | |
973 | /* Normal case: add 'info' to the beginning of the list */ | |
974 | info->prev_line = table->last_line; | |
975 | table->last_line = info; | |
e82ce529 | 976 | |
d8d1c398 AM |
977 | /* lcl_head: initialize to head a *possible* sequence at the end. */ |
978 | if (!table->lcl_head) | |
979 | table->lcl_head = info; | |
980 | } | |
981 | else if (!new_line_sorts_after (info, table->lcl_head) | |
982 | && (!table->lcl_head->prev_line | |
983 | || new_line_sorts_after (info, table->lcl_head->prev_line))) | |
984 | { | |
985 | /* Abnormal but easy: lcl_head is the head of 'info'. */ | |
986 | info->prev_line = table->lcl_head->prev_line; | |
987 | table->lcl_head->prev_line = info; | |
988 | } | |
989 | else | |
990 | { | |
991 | /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid | |
992 | heads for 'info'. Reset 'lcl_head'. */ | |
993 | struct line_info* li2 = table->last_line; /* always non-NULL */ | |
994 | struct line_info* li1 = li2->prev_line; | |
e82ce529 | 995 | |
d8d1c398 AM |
996 | while (li1) |
997 | { | |
998 | if (!new_line_sorts_after (info, li2) | |
999 | && new_line_sorts_after (info, li1)) | |
1000 | break; | |
e82ce529 | 1001 | |
d8d1c398 AM |
1002 | li2 = li1; /* always non-NULL */ |
1003 | li1 = li1->prev_line; | |
1004 | } | |
1005 | table->lcl_head = li2; | |
1006 | info->prev_line = table->lcl_head->prev_line; | |
1007 | table->lcl_head->prev_line = info; | |
1008 | } | |
252b5132 RH |
1009 | } |
1010 | ||
5ed6aba4 | 1011 | /* Extract a fully qualified filename from a line info table. |
af3ef9fe NC |
1012 | The returned string has been malloc'ed and it is the caller's |
1013 | responsibility to free it. */ | |
5ed6aba4 | 1014 | |
a092b084 | 1015 | static char * |
818a27ac | 1016 | concat_filename (struct line_info_table *table, unsigned int file) |
252b5132 | 1017 | { |
f075ee0c | 1018 | char *filename; |
159002ff RH |
1019 | |
1020 | if (file - 1 >= table->num_files) | |
1021 | { | |
75a657ba L |
1022 | /* FILE == 0 means unknown. */ |
1023 | if (file) | |
1024 | (*_bfd_error_handler) | |
1025 | (_("Dwarf Error: mangled line number section (bad file number).")); | |
af3ef9fe | 1026 | return strdup ("<unknown>"); |
159002ff RH |
1027 | } |
1028 | ||
1029 | filename = table->files[file - 1].name; | |
5ed6aba4 | 1030 | |
7421a730 | 1031 | if (!IS_ABSOLUTE_PATH (filename)) |
252b5132 | 1032 | { |
7421a730 AM |
1033 | char *dirname = NULL; |
1034 | char *subdirname = NULL; | |
1035 | char *name; | |
1036 | size_t len; | |
0dafd5f6 | 1037 | |
7421a730 AM |
1038 | if (table->files[file - 1].dir) |
1039 | subdirname = table->dirs[table->files[file - 1].dir - 1]; | |
1040 | ||
1041 | if (!subdirname || !IS_ABSOLUTE_PATH (subdirname)) | |
1042 | dirname = table->comp_dir; | |
1043 | ||
1044 | if (!dirname) | |
af3ef9fe | 1045 | { |
7421a730 AM |
1046 | dirname = subdirname; |
1047 | subdirname = NULL; | |
1048 | } | |
af3ef9fe | 1049 | |
7421a730 AM |
1050 | if (!dirname) |
1051 | return strdup (filename); | |
1052 | ||
1053 | len = strlen (dirname) + strlen (filename) + 2; | |
1054 | ||
1055 | if (subdirname) | |
1056 | { | |
1057 | len += strlen (subdirname) + 1; | |
1058 | name = bfd_malloc (len); | |
1059 | if (name) | |
1060 | sprintf (name, "%s/%s/%s", dirname, subdirname, filename); | |
1061 | } | |
1062 | else | |
1063 | { | |
af3ef9fe NC |
1064 | name = bfd_malloc (len); |
1065 | if (name) | |
1066 | sprintf (name, "%s/%s", dirname, filename); | |
af3ef9fe | 1067 | } |
7421a730 AM |
1068 | |
1069 | return name; | |
252b5132 | 1070 | } |
af3ef9fe NC |
1071 | |
1072 | return strdup (filename); | |
252b5132 RH |
1073 | } |
1074 | ||
f623be2b | 1075 | static void |
a13afe8e | 1076 | arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc) |
f623be2b RH |
1077 | { |
1078 | struct arange *arange; | |
1079 | ||
a13afe8e FF |
1080 | /* If the first arange is empty, use it. */ |
1081 | if (first_arange->high == 0) | |
1082 | { | |
1083 | first_arange->low = low_pc; | |
1084 | first_arange->high = high_pc; | |
1085 | return; | |
1086 | } | |
98591c73 | 1087 | |
a13afe8e FF |
1088 | /* Next see if we can cheaply extend an existing range. */ |
1089 | arange = first_arange; | |
f623be2b RH |
1090 | do |
1091 | { | |
1092 | if (low_pc == arange->high) | |
1093 | { | |
1094 | arange->high = high_pc; | |
1095 | return; | |
1096 | } | |
1097 | if (high_pc == arange->low) | |
1098 | { | |
1099 | arange->low = low_pc; | |
1100 | return; | |
1101 | } | |
1102 | arange = arange->next; | |
1103 | } | |
1104 | while (arange); | |
1105 | ||
a13afe8e FF |
1106 | /* Need to allocate a new arange and insert it into the arange list. |
1107 | Order isn't significant, so just insert after the first arange. */ | |
1108 | arange = bfd_zalloc (abfd, sizeof (*arange)); | |
f623be2b RH |
1109 | arange->low = low_pc; |
1110 | arange->high = high_pc; | |
a13afe8e FF |
1111 | arange->next = first_arange->next; |
1112 | first_arange->next = arange; | |
f623be2b RH |
1113 | } |
1114 | ||
a092b084 | 1115 | /* Decode the line number information for UNIT. */ |
252b5132 RH |
1116 | |
1117 | static struct line_info_table* | |
818a27ac | 1118 | decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash) |
252b5132 RH |
1119 | { |
1120 | bfd *abfd = unit->abfd; | |
252b5132 | 1121 | struct line_info_table* table; |
f075ee0c AM |
1122 | bfd_byte *line_ptr; |
1123 | bfd_byte *line_end; | |
252b5132 | 1124 | struct line_head lh; |
d03ba2a1 | 1125 | unsigned int i, bytes_read, offset_size; |
252b5132 RH |
1126 | char *cur_file, *cur_dir; |
1127 | unsigned char op_code, extended_op, adj_opcode; | |
dc810e39 | 1128 | bfd_size_type amt; |
252b5132 | 1129 | |
69dd2e2d | 1130 | if (! stash->dwarf_line_buffer) |
252b5132 RH |
1131 | { |
1132 | asection *msec; | |
252b5132 RH |
1133 | |
1134 | msec = bfd_get_section_by_name (abfd, ".debug_line"); | |
1135 | if (! msec) | |
1136 | { | |
1137 | (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section.")); | |
1138 | bfd_set_error (bfd_error_bad_value); | |
1139 | return 0; | |
1140 | } | |
98591c73 | 1141 | |
eea6121a | 1142 | stash->dwarf_line_size = msec->size; |
6e84a906 DJ |
1143 | stash->dwarf_line_buffer |
1144 | = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, | |
1145 | stash->syms); | |
f623be2b | 1146 | if (! stash->dwarf_line_buffer) |
252b5132 | 1147 | return 0; |
252b5132 RH |
1148 | } |
1149 | ||
6e84a906 DJ |
1150 | /* It is possible to get a bad value for the line_offset. Validate |
1151 | it here so that we won't get a segfault below. */ | |
ccdb16fc JW |
1152 | if (unit->line_offset >= stash->dwarf_line_size) |
1153 | { | |
f46c2da6 | 1154 | (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."), |
ccdb16fc JW |
1155 | unit->line_offset, stash->dwarf_line_size); |
1156 | bfd_set_error (bfd_error_bad_value); | |
1157 | return 0; | |
1158 | } | |
1159 | ||
dc810e39 | 1160 | amt = sizeof (struct line_info_table); |
818a27ac | 1161 | table = bfd_alloc (abfd, amt); |
252b5132 RH |
1162 | table->abfd = abfd; |
1163 | table->comp_dir = unit->comp_dir; | |
1164 | ||
1165 | table->num_files = 0; | |
1166 | table->files = NULL; | |
1167 | ||
1168 | table->num_dirs = 0; | |
1169 | table->dirs = NULL; | |
1170 | ||
159002ff RH |
1171 | table->files = NULL; |
1172 | table->last_line = NULL; | |
e82ce529 | 1173 | table->lcl_head = NULL; |
159002ff | 1174 | |
69dd2e2d | 1175 | line_ptr = stash->dwarf_line_buffer + unit->line_offset; |
252b5132 | 1176 | |
a092b084 | 1177 | /* Read in the prologue. */ |
91a4d569 AM |
1178 | lh.total_length = read_4_bytes (abfd, line_ptr); |
1179 | line_ptr += 4; | |
1180 | offset_size = 4; | |
1181 | if (lh.total_length == 0xffffffff) | |
dae2dd0d | 1182 | { |
dae2dd0d NC |
1183 | lh.total_length = read_8_bytes (abfd, line_ptr); |
1184 | line_ptr += 8; | |
1185 | offset_size = 8; | |
1186 | } | |
91a4d569 | 1187 | else if (lh.total_length == 0 && unit->addr_size == 8) |
d03ba2a1 | 1188 | { |
91a4d569 AM |
1189 | /* Handle (non-standard) 64-bit DWARF2 formats. */ |
1190 | lh.total_length = read_4_bytes (abfd, line_ptr); | |
1191 | line_ptr += 4; | |
d03ba2a1 JJ |
1192 | offset_size = 8; |
1193 | } | |
252b5132 RH |
1194 | line_end = line_ptr + lh.total_length; |
1195 | lh.version = read_2_bytes (abfd, line_ptr); | |
1196 | line_ptr += 2; | |
d03ba2a1 JJ |
1197 | if (offset_size == 4) |
1198 | lh.prologue_length = read_4_bytes (abfd, line_ptr); | |
1199 | else | |
1200 | lh.prologue_length = read_8_bytes (abfd, line_ptr); | |
1201 | line_ptr += offset_size; | |
252b5132 RH |
1202 | lh.minimum_instruction_length = read_1_byte (abfd, line_ptr); |
1203 | line_ptr += 1; | |
1204 | lh.default_is_stmt = read_1_byte (abfd, line_ptr); | |
1205 | line_ptr += 1; | |
1206 | lh.line_base = read_1_signed_byte (abfd, line_ptr); | |
1207 | line_ptr += 1; | |
1208 | lh.line_range = read_1_byte (abfd, line_ptr); | |
1209 | line_ptr += 1; | |
1210 | lh.opcode_base = read_1_byte (abfd, line_ptr); | |
1211 | line_ptr += 1; | |
dc810e39 | 1212 | amt = lh.opcode_base * sizeof (unsigned char); |
818a27ac | 1213 | lh.standard_opcode_lengths = bfd_alloc (abfd, amt); |
252b5132 RH |
1214 | |
1215 | lh.standard_opcode_lengths[0] = 1; | |
98591c73 | 1216 | |
252b5132 RH |
1217 | for (i = 1; i < lh.opcode_base; ++i) |
1218 | { | |
1219 | lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr); | |
1220 | line_ptr += 1; | |
1221 | } | |
1222 | ||
a092b084 | 1223 | /* Read directory table. */ |
252b5132 RH |
1224 | while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL) |
1225 | { | |
1226 | line_ptr += bytes_read; | |
98591c73 | 1227 | |
252b5132 RH |
1228 | if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0) |
1229 | { | |
35330cce NC |
1230 | char **tmp; |
1231 | ||
dc810e39 AM |
1232 | amt = table->num_dirs + DIR_ALLOC_CHUNK; |
1233 | amt *= sizeof (char *); | |
35330cce NC |
1234 | |
1235 | tmp = bfd_realloc (table->dirs, amt); | |
1236 | if (tmp == NULL) | |
1237 | { | |
1238 | free (table->dirs); | |
1239 | return NULL; | |
1240 | } | |
1241 | table->dirs = tmp; | |
252b5132 | 1242 | } |
98591c73 | 1243 | |
252b5132 RH |
1244 | table->dirs[table->num_dirs++] = cur_dir; |
1245 | } | |
98591c73 | 1246 | |
252b5132 RH |
1247 | line_ptr += bytes_read; |
1248 | ||
a092b084 | 1249 | /* Read file name table. */ |
252b5132 RH |
1250 | while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL) |
1251 | { | |
1252 | line_ptr += bytes_read; | |
98591c73 | 1253 | |
252b5132 RH |
1254 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) |
1255 | { | |
35330cce NC |
1256 | struct fileinfo *tmp; |
1257 | ||
dc810e39 AM |
1258 | amt = table->num_files + FILE_ALLOC_CHUNK; |
1259 | amt *= sizeof (struct fileinfo); | |
35330cce NC |
1260 | |
1261 | tmp = bfd_realloc (table->files, amt); | |
1262 | if (tmp == NULL) | |
1263 | { | |
1264 | free (table->files); | |
1265 | free (table->dirs); | |
1266 | return NULL; | |
1267 | } | |
1268 | table->files = tmp; | |
252b5132 | 1269 | } |
98591c73 | 1270 | |
252b5132 RH |
1271 | table->files[table->num_files].name = cur_file; |
1272 | table->files[table->num_files].dir = | |
1273 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1274 | line_ptr += bytes_read; | |
1275 | table->files[table->num_files].time = | |
1276 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1277 | line_ptr += bytes_read; | |
1278 | table->files[table->num_files].size = | |
1279 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1280 | line_ptr += bytes_read; | |
1281 | table->num_files++; | |
1282 | } | |
98591c73 | 1283 | |
252b5132 RH |
1284 | line_ptr += bytes_read; |
1285 | ||
1286 | /* Read the statement sequences until there's nothing left. */ | |
1287 | while (line_ptr < line_end) | |
1288 | { | |
a092b084 | 1289 | /* State machine registers. */ |
252b5132 | 1290 | bfd_vma address = 0; |
8bfd78b3 | 1291 | char * filename = table->num_files ? concat_filename (table, 1) : NULL; |
252b5132 RH |
1292 | unsigned int line = 1; |
1293 | unsigned int column = 0; | |
1294 | int is_stmt = lh.default_is_stmt; | |
e2f6d277 NC |
1295 | int end_sequence = 0; |
1296 | /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some | |
e82ce529 AM |
1297 | compilers generate address sequences that are wildly out of |
1298 | order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler | |
1299 | for ia64-Linux). Thus, to determine the low and high | |
1300 | address, we must compare on every DW_LNS_copy, etc. */ | |
75758e9d | 1301 | bfd_vma low_pc = (bfd_vma) -1; |
e2f6d277 | 1302 | bfd_vma high_pc = 0; |
252b5132 | 1303 | |
a092b084 | 1304 | /* Decode the table. */ |
252b5132 RH |
1305 | while (! end_sequence) |
1306 | { | |
1307 | op_code = read_1_byte (abfd, line_ptr); | |
1308 | line_ptr += 1; | |
98591c73 | 1309 | |
1a509dcc | 1310 | if (op_code >= lh.opcode_base) |
e2f6d277 NC |
1311 | { |
1312 | /* Special operand. */ | |
1a509dcc GK |
1313 | adj_opcode = op_code - lh.opcode_base; |
1314 | address += (adj_opcode / lh.line_range) | |
1315 | * lh.minimum_instruction_length; | |
1316 | line += lh.line_base + (adj_opcode % lh.line_range); | |
1317 | /* Append row to matrix using current values. */ | |
1318 | add_line_info (table, address, filename, line, column, 0); | |
75758e9d AM |
1319 | if (address < low_pc) |
1320 | low_pc = address; | |
e2f6d277 NC |
1321 | if (address > high_pc) |
1322 | high_pc = address; | |
1a509dcc GK |
1323 | } |
1324 | else switch (op_code) | |
252b5132 RH |
1325 | { |
1326 | case DW_LNS_extended_op: | |
e2f6d277 NC |
1327 | /* Ignore length. */ |
1328 | line_ptr += 1; | |
252b5132 RH |
1329 | extended_op = read_1_byte (abfd, line_ptr); |
1330 | line_ptr += 1; | |
e2f6d277 | 1331 | |
252b5132 RH |
1332 | switch (extended_op) |
1333 | { | |
1334 | case DW_LNE_end_sequence: | |
1335 | end_sequence = 1; | |
f623be2b RH |
1336 | add_line_info (table, address, filename, line, column, |
1337 | end_sequence); | |
75758e9d AM |
1338 | if (address < low_pc) |
1339 | low_pc = address; | |
e2f6d277 NC |
1340 | if (address > high_pc) |
1341 | high_pc = address; | |
a13afe8e | 1342 | arange_add (unit->abfd, &unit->arange, low_pc, high_pc); |
252b5132 RH |
1343 | break; |
1344 | case DW_LNE_set_address: | |
1345 | address = read_address (unit, line_ptr); | |
1346 | line_ptr += unit->addr_size; | |
1347 | break; | |
1348 | case DW_LNE_define_file: | |
1349 | cur_file = read_string (abfd, line_ptr, &bytes_read); | |
1350 | line_ptr += bytes_read; | |
1351 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) | |
1352 | { | |
35330cce NC |
1353 | struct fileinfo *tmp; |
1354 | ||
dc810e39 AM |
1355 | amt = table->num_files + FILE_ALLOC_CHUNK; |
1356 | amt *= sizeof (struct fileinfo); | |
35330cce NC |
1357 | tmp = bfd_realloc (table->files, amt); |
1358 | if (tmp == NULL) | |
d8d1c398 | 1359 | { |
35330cce NC |
1360 | free (table->files); |
1361 | free (table->dirs); | |
1362 | free (filename); | |
1363 | return NULL; | |
d8d1c398 | 1364 | } |
35330cce | 1365 | table->files = tmp; |
252b5132 RH |
1366 | } |
1367 | table->files[table->num_files].name = cur_file; | |
1368 | table->files[table->num_files].dir = | |
1369 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1370 | line_ptr += bytes_read; | |
1371 | table->files[table->num_files].time = | |
1372 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1373 | line_ptr += bytes_read; | |
1374 | table->files[table->num_files].size = | |
1375 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1376 | line_ptr += bytes_read; | |
1377 | table->num_files++; | |
1378 | break; | |
1379 | default: | |
1380 | (*_bfd_error_handler) (_("Dwarf Error: mangled line number section.")); | |
1381 | bfd_set_error (bfd_error_bad_value); | |
35330cce NC |
1382 | free (filename); |
1383 | free (table->files); | |
1384 | free (table->dirs); | |
1385 | return NULL; | |
252b5132 RH |
1386 | } |
1387 | break; | |
1388 | case DW_LNS_copy: | |
159002ff | 1389 | add_line_info (table, address, filename, line, column, 0); |
75758e9d AM |
1390 | if (address < low_pc) |
1391 | low_pc = address; | |
e2f6d277 NC |
1392 | if (address > high_pc) |
1393 | high_pc = address; | |
252b5132 RH |
1394 | break; |
1395 | case DW_LNS_advance_pc: | |
1396 | address += lh.minimum_instruction_length | |
1397 | * read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1398 | line_ptr += bytes_read; | |
1399 | break; | |
1400 | case DW_LNS_advance_line: | |
1401 | line += read_signed_leb128 (abfd, line_ptr, &bytes_read); | |
1402 | line_ptr += bytes_read; | |
1403 | break; | |
1404 | case DW_LNS_set_file: | |
1405 | { | |
1406 | unsigned int file; | |
1407 | ||
e2f6d277 NC |
1408 | /* The file and directory tables are 0 |
1409 | based, the references are 1 based. */ | |
252b5132 RH |
1410 | file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
1411 | line_ptr += bytes_read; | |
af3ef9fe NC |
1412 | if (filename) |
1413 | free (filename); | |
252b5132 RH |
1414 | filename = concat_filename (table, file); |
1415 | break; | |
1416 | } | |
1417 | case DW_LNS_set_column: | |
1418 | column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1419 | line_ptr += bytes_read; | |
1420 | break; | |
1421 | case DW_LNS_negate_stmt: | |
1422 | is_stmt = (!is_stmt); | |
1423 | break; | |
1424 | case DW_LNS_set_basic_block: | |
252b5132 RH |
1425 | break; |
1426 | case DW_LNS_const_add_pc: | |
159002ff RH |
1427 | address += lh.minimum_instruction_length |
1428 | * ((255 - lh.opcode_base) / lh.line_range); | |
252b5132 RH |
1429 | break; |
1430 | case DW_LNS_fixed_advance_pc: | |
1431 | address += read_2_bytes (abfd, line_ptr); | |
1432 | line_ptr += 2; | |
1433 | break; | |
1a509dcc | 1434 | default: |
e2f6d277 | 1435 | { |
1a509dcc | 1436 | int i; |
5ed6aba4 | 1437 | |
e2f6d277 | 1438 | /* Unknown standard opcode, ignore it. */ |
1a509dcc GK |
1439 | for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++) |
1440 | { | |
1441 | (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1442 | line_ptr += bytes_read; | |
1443 | } | |
1444 | } | |
252b5132 RH |
1445 | } |
1446 | } | |
5ed6aba4 | 1447 | |
af3ef9fe NC |
1448 | if (filename) |
1449 | free (filename); | |
252b5132 RH |
1450 | } |
1451 | ||
1452 | return table; | |
1453 | } | |
1454 | ||
b34976b6 AM |
1455 | /* If ADDR is within TABLE set the output parameters and return TRUE, |
1456 | otherwise return FALSE. The output parameters, FILENAME_PTR and | |
a092b084 | 1457 | LINENUMBER_PTR, are pointers to the objects to be filled in. */ |
252b5132 | 1458 | |
b34976b6 | 1459 | static bfd_boolean |
818a27ac AM |
1460 | lookup_address_in_line_info_table (struct line_info_table *table, |
1461 | bfd_vma addr, | |
1462 | struct funcinfo *function, | |
1463 | const char **filename_ptr, | |
1464 | unsigned int *linenumber_ptr) | |
252b5132 | 1465 | { |
e82ce529 | 1466 | /* Note: table->last_line should be a descendingly sorted list. */ |
159002ff | 1467 | struct line_info* next_line = table->last_line; |
e82ce529 AM |
1468 | struct line_info* each_line = NULL; |
1469 | *filename_ptr = NULL; | |
98591c73 | 1470 | |
159002ff | 1471 | if (!next_line) |
b34976b6 | 1472 | return FALSE; |
159002ff RH |
1473 | |
1474 | each_line = next_line->prev_line; | |
1475 | ||
e82ce529 AM |
1476 | /* Check for large addresses */ |
1477 | if (addr > next_line->address) | |
1478 | each_line = NULL; /* ensure we skip over the normal case */ | |
1479 | ||
1480 | /* Normal case: search the list; save */ | |
159002ff | 1481 | while (each_line && next_line) |
252b5132 | 1482 | { |
e82ce529 AM |
1483 | /* If we have an address match, save this info. This allows us |
1484 | to return as good as results as possible for strange debugging | |
1485 | info. */ | |
b34976b6 | 1486 | bfd_boolean addr_match = FALSE; |
a13afe8e | 1487 | if (each_line->address <= addr && addr < next_line->address) |
252b5132 | 1488 | { |
b34976b6 | 1489 | addr_match = TRUE; |
e82ce529 | 1490 | |
1ee24f27 DJ |
1491 | /* If this line appears to span functions, and addr is in the |
1492 | later function, return the first line of that function instead | |
1493 | of the last line of the earlier one. This check is for GCC | |
1494 | 2.95, which emits the first line number for a function late. */ | |
a13afe8e FF |
1495 | |
1496 | if (function != NULL) | |
1ee24f27 | 1497 | { |
a13afe8e FF |
1498 | bfd_vma lowest_pc; |
1499 | struct arange *arange; | |
1500 | ||
1501 | /* Find the lowest address in the function's range list */ | |
1502 | lowest_pc = function->arange.low; | |
1503 | for (arange = &function->arange; | |
1504 | arange; | |
1505 | arange = arange->next) | |
1506 | { | |
1507 | if (function->arange.low < lowest_pc) | |
1508 | lowest_pc = function->arange.low; | |
1509 | } | |
1510 | /* Check for spanning function and set outgoing line info */ | |
1511 | if (addr >= lowest_pc | |
1512 | && each_line->address < lowest_pc | |
1513 | && next_line->address > lowest_pc) | |
1514 | { | |
1515 | *filename_ptr = next_line->filename; | |
1516 | *linenumber_ptr = next_line->line; | |
1517 | } | |
1518 | else | |
1519 | { | |
1520 | *filename_ptr = each_line->filename; | |
1521 | *linenumber_ptr = each_line->line; | |
1522 | } | |
1ee24f27 | 1523 | } |
6bd00c5d L |
1524 | else |
1525 | { | |
1526 | *filename_ptr = each_line->filename; | |
1527 | *linenumber_ptr = each_line->line; | |
1528 | } | |
252b5132 | 1529 | } |
e82ce529 AM |
1530 | |
1531 | if (addr_match && !each_line->end_sequence) | |
b34976b6 | 1532 | return TRUE; /* we have definitely found what we want */ |
e82ce529 | 1533 | |
159002ff RH |
1534 | next_line = each_line; |
1535 | each_line = each_line->prev_line; | |
252b5132 | 1536 | } |
98591c73 | 1537 | |
e82ce529 AM |
1538 | /* At this point each_line is NULL but next_line is not. If we found |
1539 | a candidate end-of-sequence point in the loop above, we can return | |
1540 | that (compatibility with a bug in the Intel compiler); otherwise, | |
1541 | assuming that we found the containing function for this address in | |
1542 | this compilation unit, return the first line we have a number for | |
1543 | (compatibility with GCC 2.95). */ | |
1544 | if (*filename_ptr == NULL && function != NULL) | |
1ee24f27 DJ |
1545 | { |
1546 | *filename_ptr = next_line->filename; | |
1547 | *linenumber_ptr = next_line->line; | |
b34976b6 | 1548 | return TRUE; |
1ee24f27 DJ |
1549 | } |
1550 | ||
b34976b6 | 1551 | return FALSE; |
252b5132 | 1552 | } |
98591c73 | 1553 | |
a13afe8e FF |
1554 | /* Read in the .debug_ranges section for future reference */ |
1555 | ||
1556 | static bfd_boolean | |
1557 | read_debug_ranges (struct comp_unit *unit) | |
1558 | { | |
1559 | struct dwarf2_debug *stash = unit->stash; | |
1560 | if (! stash->dwarf_ranges_buffer) | |
1561 | { | |
1562 | bfd *abfd = unit->abfd; | |
1563 | asection *msec; | |
1564 | ||
1565 | msec = bfd_get_section_by_name (abfd, ".debug_ranges"); | |
1566 | if (! msec) | |
1567 | { | |
1568 | (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section.")); | |
1569 | bfd_set_error (bfd_error_bad_value); | |
1570 | return FALSE; | |
1571 | } | |
1572 | ||
1573 | stash->dwarf_ranges_size = msec->size; | |
1574 | stash->dwarf_ranges_buffer | |
1575 | = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, | |
1576 | stash->syms); | |
1577 | if (! stash->dwarf_ranges_buffer) | |
1578 | return FALSE; | |
1579 | } | |
1580 | return TRUE; | |
1581 | } | |
1582 | ||
a092b084 | 1583 | /* Function table functions. */ |
252b5132 | 1584 | |
a13afe8e FF |
1585 | /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. |
1586 | Note that we need to find the function that has the smallest | |
1587 | range that contains ADDR, to handle inlined functions without | |
1588 | depending upon them being ordered in TABLE by increasing range. */ | |
252b5132 | 1589 | |
b34976b6 | 1590 | static bfd_boolean |
4ab527b0 | 1591 | lookup_address_in_function_table (struct comp_unit *unit, |
818a27ac AM |
1592 | bfd_vma addr, |
1593 | struct funcinfo **function_ptr, | |
1594 | const char **functionname_ptr) | |
252b5132 RH |
1595 | { |
1596 | struct funcinfo* each_func; | |
a13afe8e FF |
1597 | struct funcinfo* best_fit = NULL; |
1598 | struct arange *arange; | |
252b5132 | 1599 | |
4ab527b0 | 1600 | for (each_func = unit->function_table; |
252b5132 RH |
1601 | each_func; |
1602 | each_func = each_func->prev_func) | |
1603 | { | |
a13afe8e FF |
1604 | for (arange = &each_func->arange; |
1605 | arange; | |
1606 | arange = arange->next) | |
252b5132 | 1607 | { |
a13afe8e FF |
1608 | if (addr >= arange->low && addr < arange->high) |
1609 | { | |
1610 | if (!best_fit || | |
1611 | ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low))) | |
1612 | best_fit = each_func; | |
1613 | } | |
252b5132 RH |
1614 | } |
1615 | } | |
98591c73 | 1616 | |
a13afe8e FF |
1617 | if (best_fit) |
1618 | { | |
1619 | *functionname_ptr = best_fit->name; | |
1620 | *function_ptr = best_fit; | |
1621 | return TRUE; | |
1622 | } | |
1623 | else | |
1624 | { | |
1625 | return FALSE; | |
1626 | } | |
252b5132 RH |
1627 | } |
1628 | ||
5420f73d L |
1629 | /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR |
1630 | and LINENUMBER_PTR, and return TRUE. */ | |
1631 | ||
1632 | static bfd_boolean | |
1633 | lookup_symbol_in_function_table (struct comp_unit *unit, | |
1634 | asymbol *sym, | |
1635 | bfd_vma addr, | |
1636 | const char **filename_ptr, | |
1637 | unsigned int *linenumber_ptr) | |
1638 | { | |
1639 | struct funcinfo* each_func; | |
1640 | struct funcinfo* best_fit = NULL; | |
1641 | struct arange *arange; | |
1642 | const char *name = bfd_asymbol_name (sym); | |
1643 | asection *sec = bfd_get_section (sym); | |
1644 | ||
1645 | for (each_func = unit->function_table; | |
1646 | each_func; | |
1647 | each_func = each_func->prev_func) | |
1648 | { | |
1649 | for (arange = &each_func->arange; | |
1650 | arange; | |
1651 | arange = arange->next) | |
1652 | { | |
1653 | if ((!each_func->sec || each_func->sec == sec) | |
1654 | && addr >= arange->low | |
1655 | && addr < arange->high | |
650f284e | 1656 | && each_func->name |
5420f73d L |
1657 | && strcmp (name, each_func->name) == 0 |
1658 | && (!best_fit | |
1659 | || ((arange->high - arange->low) | |
1660 | < (best_fit->arange.high - best_fit->arange.low)))) | |
1661 | best_fit = each_func; | |
1662 | } | |
1663 | } | |
1664 | ||
1665 | if (best_fit) | |
1666 | { | |
1667 | best_fit->sec = sec; | |
1668 | *filename_ptr = best_fit->file; | |
1669 | *linenumber_ptr = best_fit->line; | |
1670 | return TRUE; | |
1671 | } | |
1672 | else | |
1673 | return FALSE; | |
1674 | } | |
1675 | ||
1676 | /* Variable table functions. */ | |
1677 | ||
1678 | /* If SYM is within variable table of UNIT, set FILENAME_PTR and | |
1679 | LINENUMBER_PTR, and return TRUE. */ | |
1680 | ||
1681 | static bfd_boolean | |
1682 | lookup_symbol_in_variable_table (struct comp_unit *unit, | |
1683 | asymbol *sym, | |
5cf2e3f0 | 1684 | bfd_vma addr, |
5420f73d L |
1685 | const char **filename_ptr, |
1686 | unsigned int *linenumber_ptr) | |
1687 | { | |
1688 | const char *name = bfd_asymbol_name (sym); | |
1689 | asection *sec = bfd_get_section (sym); | |
1690 | struct varinfo* each; | |
1691 | ||
1692 | for (each = unit->variable_table; each; each = each->prev_var) | |
1693 | if (each->stack == 0 | |
5cf2e3f0 L |
1694 | && each->file != NULL |
1695 | && each->name != NULL | |
1696 | && each->addr == addr | |
5420f73d L |
1697 | && (!each->sec || each->sec == sec) |
1698 | && strcmp (name, each->name) == 0) | |
1699 | break; | |
1700 | ||
1701 | if (each) | |
1702 | { | |
1703 | each->sec = sec; | |
1704 | *filename_ptr = each->file; | |
1705 | *linenumber_ptr = each->line; | |
1706 | return TRUE; | |
1707 | } | |
1708 | else | |
1709 | return FALSE; | |
1710 | } | |
1711 | ||
06f22d7e FF |
1712 | static char * |
1713 | find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref) | |
1714 | { | |
1715 | bfd *abfd = unit->abfd; | |
f075ee0c | 1716 | bfd_byte *info_ptr; |
06f22d7e FF |
1717 | unsigned int abbrev_number, bytes_read, i; |
1718 | struct abbrev_info *abbrev; | |
1719 | struct attribute attr; | |
1720 | char *name = 0; | |
1721 | ||
c0c28ab8 | 1722 | info_ptr = unit->info_ptr_unit + die_ref; |
06f22d7e FF |
1723 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
1724 | info_ptr += bytes_read; | |
1725 | ||
1726 | if (abbrev_number) | |
1727 | { | |
1728 | abbrev = lookup_abbrev (abbrev_number, unit->abbrevs); | |
1729 | if (! abbrev) | |
1730 | { | |
1731 | (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), | |
1732 | abbrev_number); | |
1733 | bfd_set_error (bfd_error_bad_value); | |
1734 | } | |
1735 | else | |
1736 | { | |
d5cbaa15 | 1737 | for (i = 0; i < abbrev->num_attrs; ++i) |
06f22d7e FF |
1738 | { |
1739 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); | |
26bf4e33 FF |
1740 | switch (attr.name) |
1741 | { | |
1742 | case DW_AT_name: | |
d5cbaa15 JW |
1743 | /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */ |
1744 | if (name == NULL) | |
1745 | name = attr.u.str; | |
26bf4e33 FF |
1746 | break; |
1747 | case DW_AT_specification: | |
1748 | name = find_abstract_instance_name (unit, attr.u.val); | |
1749 | break; | |
d5cbaa15 JW |
1750 | case DW_AT_MIPS_linkage_name: |
1751 | name = attr.u.str; | |
1752 | break; | |
26bf4e33 FF |
1753 | default: |
1754 | break; | |
1755 | } | |
06f22d7e FF |
1756 | } |
1757 | } | |
1758 | } | |
1759 | return (name); | |
1760 | } | |
1761 | ||
a13afe8e FF |
1762 | static void |
1763 | read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset) | |
1764 | { | |
1765 | bfd_byte *ranges_ptr; | |
1766 | bfd_vma base_address = unit->base_address; | |
1767 | ||
1768 | if (! unit->stash->dwarf_ranges_buffer) | |
1769 | { | |
1770 | if (! read_debug_ranges (unit)) | |
1771 | return; | |
1772 | } | |
1773 | ranges_ptr = unit->stash->dwarf_ranges_buffer + offset; | |
d8d1c398 | 1774 | |
a13afe8e FF |
1775 | for (;;) |
1776 | { | |
1777 | bfd_vma low_pc; | |
1778 | bfd_vma high_pc; | |
1779 | ||
a29b7e88 | 1780 | if (unit->addr_size == 4) |
a13afe8e FF |
1781 | { |
1782 | low_pc = read_4_bytes (unit->abfd, ranges_ptr); | |
1783 | ranges_ptr += 4; | |
1784 | high_pc = read_4_bytes (unit->abfd, ranges_ptr); | |
1785 | ranges_ptr += 4; | |
1786 | } | |
1787 | else | |
1788 | { | |
1789 | low_pc = read_8_bytes (unit->abfd, ranges_ptr); | |
1790 | ranges_ptr += 8; | |
1791 | high_pc = read_8_bytes (unit->abfd, ranges_ptr); | |
1792 | ranges_ptr += 8; | |
1793 | } | |
1794 | if (low_pc == 0 && high_pc == 0) | |
1795 | break; | |
1796 | if (low_pc == -1UL && high_pc != -1UL) | |
1797 | base_address = high_pc; | |
1798 | else | |
e460dd0d | 1799 | arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc); |
a13afe8e FF |
1800 | } |
1801 | } | |
1802 | ||
a092b084 | 1803 | /* DWARF2 Compilation unit functions. */ |
252b5132 RH |
1804 | |
1805 | /* Scan over each die in a comp. unit looking for functions to add | |
5420f73d | 1806 | to the function table and variables to the variable table. */ |
252b5132 | 1807 | |
b34976b6 | 1808 | static bfd_boolean |
5420f73d | 1809 | scan_unit_for_symbols (struct comp_unit *unit) |
252b5132 RH |
1810 | { |
1811 | bfd *abfd = unit->abfd; | |
f075ee0c | 1812 | bfd_byte *info_ptr = unit->first_child_die_ptr; |
252b5132 | 1813 | int nesting_level = 1; |
c955f9cd JW |
1814 | struct funcinfo **nested_funcs; |
1815 | int nested_funcs_size; | |
1816 | ||
1817 | /* Maintain a stack of in-scope functions and inlined functions, which we | |
1818 | can use to set the caller_func field. */ | |
1819 | nested_funcs_size = 32; | |
1820 | nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *)); | |
1821 | if (nested_funcs == NULL) | |
1822 | return FALSE; | |
1823 | nested_funcs[nesting_level] = 0; | |
252b5132 RH |
1824 | |
1825 | while (nesting_level) | |
1826 | { | |
1827 | unsigned int abbrev_number, bytes_read, i; | |
1828 | struct abbrev_info *abbrev; | |
1829 | struct attribute attr; | |
1830 | struct funcinfo *func; | |
5420f73d | 1831 | struct varinfo *var; |
a13afe8e FF |
1832 | bfd_vma low_pc = 0; |
1833 | bfd_vma high_pc = 0; | |
252b5132 RH |
1834 | |
1835 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); | |
1836 | info_ptr += bytes_read; | |
1837 | ||
1838 | if (! abbrev_number) | |
1839 | { | |
1840 | nesting_level--; | |
1841 | continue; | |
1842 | } | |
98591c73 | 1843 | |
252b5132 RH |
1844 | abbrev = lookup_abbrev (abbrev_number,unit->abbrevs); |
1845 | if (! abbrev) | |
1846 | { | |
f46c2da6 | 1847 | (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), |
252b5132 RH |
1848 | abbrev_number); |
1849 | bfd_set_error (bfd_error_bad_value); | |
c955f9cd | 1850 | free (nested_funcs); |
b34976b6 | 1851 | return FALSE; |
252b5132 | 1852 | } |
98591c73 | 1853 | |
5420f73d | 1854 | var = NULL; |
06f22d7e | 1855 | if (abbrev->tag == DW_TAG_subprogram |
5420f73d | 1856 | || abbrev->tag == DW_TAG_entry_point |
06f22d7e | 1857 | || abbrev->tag == DW_TAG_inlined_subroutine) |
252b5132 | 1858 | { |
dc810e39 | 1859 | bfd_size_type amt = sizeof (struct funcinfo); |
818a27ac | 1860 | func = bfd_zalloc (abfd, amt); |
4ab527b0 | 1861 | func->tag = abbrev->tag; |
252b5132 RH |
1862 | func->prev_func = unit->function_table; |
1863 | unit->function_table = func; | |
bd210d54 | 1864 | BFD_ASSERT (!unit->cached); |
c955f9cd JW |
1865 | |
1866 | if (func->tag == DW_TAG_inlined_subroutine) | |
1867 | for (i = nesting_level - 1; i >= 1; i--) | |
1868 | if (nested_funcs[i]) | |
1869 | { | |
1870 | func->caller_func = nested_funcs[i]; | |
1871 | break; | |
1872 | } | |
1873 | nested_funcs[nesting_level] = func; | |
252b5132 RH |
1874 | } |
1875 | else | |
5420f73d L |
1876 | { |
1877 | func = NULL; | |
1878 | if (abbrev->tag == DW_TAG_variable) | |
1879 | { | |
1880 | bfd_size_type amt = sizeof (struct varinfo); | |
1881 | var = bfd_zalloc (abfd, amt); | |
1882 | var->tag = abbrev->tag; | |
1883 | var->stack = 1; | |
1884 | var->prev_var = unit->variable_table; | |
1885 | unit->variable_table = var; | |
bd210d54 | 1886 | BFD_ASSERT (!unit->cached); |
5420f73d | 1887 | } |
c955f9cd JW |
1888 | |
1889 | /* No inline function in scope at this nesting level. */ | |
1890 | nested_funcs[nesting_level] = 0; | |
5420f73d | 1891 | } |
98591c73 | 1892 | |
252b5132 RH |
1893 | for (i = 0; i < abbrev->num_attrs; ++i) |
1894 | { | |
1895 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); | |
98591c73 | 1896 | |
252b5132 RH |
1897 | if (func) |
1898 | { | |
1899 | switch (attr.name) | |
1900 | { | |
4ab527b0 FF |
1901 | case DW_AT_call_file: |
1902 | func->caller_file = concat_filename (unit->line_table, attr.u.val); | |
1903 | break; | |
1904 | ||
1905 | case DW_AT_call_line: | |
1906 | func->caller_line = attr.u.val; | |
1907 | break; | |
1908 | ||
06f22d7e FF |
1909 | case DW_AT_abstract_origin: |
1910 | func->name = find_abstract_instance_name (unit, attr.u.val); | |
1911 | break; | |
1912 | ||
252b5132 | 1913 | case DW_AT_name: |
252b5132 RH |
1914 | /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */ |
1915 | if (func->name == NULL) | |
482e2e37 | 1916 | func->name = attr.u.str; |
252b5132 | 1917 | break; |
98591c73 | 1918 | |
252b5132 | 1919 | case DW_AT_MIPS_linkage_name: |
482e2e37 | 1920 | func->name = attr.u.str; |
252b5132 RH |
1921 | break; |
1922 | ||
1923 | case DW_AT_low_pc: | |
a13afe8e | 1924 | low_pc = attr.u.val; |
252b5132 RH |
1925 | break; |
1926 | ||
1927 | case DW_AT_high_pc: | |
a13afe8e FF |
1928 | high_pc = attr.u.val; |
1929 | break; | |
1930 | ||
1931 | case DW_AT_ranges: | |
1932 | read_rangelist (unit, &func->arange, attr.u.val); | |
252b5132 RH |
1933 | break; |
1934 | ||
5420f73d L |
1935 | case DW_AT_decl_file: |
1936 | func->file = concat_filename (unit->line_table, | |
1937 | attr.u.val); | |
1938 | break; | |
1939 | ||
1940 | case DW_AT_decl_line: | |
1941 | func->line = attr.u.val; | |
1942 | break; | |
1943 | ||
1944 | default: | |
1945 | break; | |
1946 | } | |
1947 | } | |
1948 | else if (var) | |
1949 | { | |
1950 | switch (attr.name) | |
1951 | { | |
1952 | case DW_AT_name: | |
1953 | var->name = attr.u.str; | |
1954 | break; | |
1955 | ||
1956 | case DW_AT_decl_file: | |
1957 | var->file = concat_filename (unit->line_table, | |
1958 | attr.u.val); | |
1959 | break; | |
1960 | ||
1961 | case DW_AT_decl_line: | |
1962 | var->line = attr.u.val; | |
1963 | break; | |
1964 | ||
1965 | case DW_AT_external: | |
1966 | if (attr.u.val != 0) | |
1967 | var->stack = 0; | |
1968 | break; | |
1969 | ||
1970 | case DW_AT_location: | |
5cf2e3f0 | 1971 | switch (attr.form) |
5420f73d | 1972 | { |
5cf2e3f0 L |
1973 | case DW_FORM_block: |
1974 | case DW_FORM_block1: | |
1975 | case DW_FORM_block2: | |
1976 | case DW_FORM_block4: | |
1977 | if (*attr.u.blk->data == DW_OP_addr) | |
5420f73d | 1978 | { |
5cf2e3f0 | 1979 | var->stack = 0; |
98b880f4 JW |
1980 | |
1981 | /* Verify that DW_OP_addr is the only opcode in the | |
1982 | location, in which case the block size will be 1 | |
1983 | plus the address size. */ | |
1984 | /* ??? For TLS variables, gcc can emit | |
1985 | DW_OP_addr <addr> DW_OP_GNU_push_tls_address | |
1986 | which we don't handle here yet. */ | |
1987 | if (attr.u.blk->size == unit->addr_size + 1U) | |
1988 | var->addr = bfd_get (unit->addr_size * 8, | |
1989 | unit->abfd, | |
1990 | attr.u.blk->data + 1); | |
5420f73d | 1991 | } |
5cf2e3f0 | 1992 | break; |
d8d1c398 | 1993 | |
5cf2e3f0 L |
1994 | default: |
1995 | break; | |
5420f73d L |
1996 | } |
1997 | break; | |
1998 | ||
252b5132 RH |
1999 | default: |
2000 | break; | |
2001 | } | |
2002 | } | |
2003 | } | |
2004 | ||
a13afe8e FF |
2005 | if (func && high_pc != 0) |
2006 | { | |
2007 | arange_add (unit->abfd, &func->arange, low_pc, high_pc); | |
2008 | } | |
2009 | ||
252b5132 | 2010 | if (abbrev->has_children) |
c955f9cd JW |
2011 | { |
2012 | nesting_level++; | |
2013 | ||
2014 | if (nesting_level >= nested_funcs_size) | |
2015 | { | |
2016 | struct funcinfo **tmp; | |
2017 | ||
2018 | nested_funcs_size *= 2; | |
2019 | tmp = bfd_realloc (nested_funcs, | |
2020 | (nested_funcs_size | |
2021 | * sizeof (struct funcinfo *))); | |
2022 | if (tmp == NULL) | |
2023 | { | |
2024 | free (nested_funcs); | |
2025 | return FALSE; | |
2026 | } | |
2027 | nested_funcs = tmp; | |
2028 | } | |
2029 | nested_funcs[nesting_level] = 0; | |
2030 | } | |
252b5132 RH |
2031 | } |
2032 | ||
c955f9cd | 2033 | free (nested_funcs); |
b34976b6 | 2034 | return TRUE; |
252b5132 RH |
2035 | } |
2036 | ||
5e38c3b8 MM |
2037 | /* Parse a DWARF2 compilation unit starting at INFO_PTR. This |
2038 | includes the compilation unit header that proceeds the DIE's, but | |
5c4491d3 | 2039 | does not include the length field that precedes each compilation |
5e38c3b8 | 2040 | unit header. END_PTR points one past the end of this comp unit. |
d03ba2a1 | 2041 | OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes). |
252b5132 RH |
2042 | |
2043 | This routine does not read the whole compilation unit; only enough | |
2044 | to get to the line number information for the compilation unit. */ | |
2045 | ||
2046 | static struct comp_unit * | |
0d161102 | 2047 | parse_comp_unit (struct dwarf2_debug *stash, |
818a27ac | 2048 | bfd_vma unit_length, |
f075ee0c | 2049 | bfd_byte *info_ptr_unit, |
818a27ac | 2050 | unsigned int offset_size) |
252b5132 RH |
2051 | { |
2052 | struct comp_unit* unit; | |
f46c2da6 | 2053 | unsigned int version; |
8ce8c090 | 2054 | bfd_uint64_t abbrev_offset = 0; |
f46c2da6 | 2055 | unsigned int addr_size; |
252b5132 | 2056 | struct abbrev_info** abbrevs; |
252b5132 RH |
2057 | unsigned int abbrev_number, bytes_read, i; |
2058 | struct abbrev_info *abbrev; | |
2059 | struct attribute attr; | |
f075ee0c AM |
2060 | bfd_byte *info_ptr = stash->info_ptr; |
2061 | bfd_byte *end_ptr = info_ptr + unit_length; | |
dc810e39 | 2062 | bfd_size_type amt; |
a13afe8e FF |
2063 | bfd_vma low_pc = 0; |
2064 | bfd_vma high_pc = 0; | |
0d161102 | 2065 | bfd *abfd = stash->bfd; |
3fde5a36 | 2066 | |
252b5132 RH |
2067 | version = read_2_bytes (abfd, info_ptr); |
2068 | info_ptr += 2; | |
d03ba2a1 JJ |
2069 | BFD_ASSERT (offset_size == 4 || offset_size == 8); |
2070 | if (offset_size == 4) | |
5e38c3b8 | 2071 | abbrev_offset = read_4_bytes (abfd, info_ptr); |
d03ba2a1 | 2072 | else |
5e38c3b8 | 2073 | abbrev_offset = read_8_bytes (abfd, info_ptr); |
d03ba2a1 | 2074 | info_ptr += offset_size; |
252b5132 RH |
2075 | addr_size = read_1_byte (abfd, info_ptr); |
2076 | info_ptr += 1; | |
2077 | ||
2078 | if (version != 2) | |
2079 | { | |
f46c2da6 | 2080 | (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version); |
252b5132 RH |
2081 | bfd_set_error (bfd_error_bad_value); |
2082 | return 0; | |
2083 | } | |
2084 | ||
2085 | if (addr_size > sizeof (bfd_vma)) | |
2086 | { | |
2087 | (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."), | |
2088 | addr_size, | |
f46c2da6 | 2089 | (unsigned int) sizeof (bfd_vma)); |
252b5132 RH |
2090 | bfd_set_error (bfd_error_bad_value); |
2091 | return 0; | |
2092 | } | |
2093 | ||
ecb651f0 | 2094 | if (addr_size != 2 && addr_size != 4 && addr_size != 8) |
252b5132 | 2095 | { |
f5a3e38a | 2096 | (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size); |
252b5132 RH |
2097 | bfd_set_error (bfd_error_bad_value); |
2098 | return 0; | |
2099 | } | |
2100 | ||
a092b084 | 2101 | /* Read the abbrevs for this compilation unit into a table. */ |
51db3708 | 2102 | abbrevs = read_abbrevs (abfd, abbrev_offset, stash); |
252b5132 RH |
2103 | if (! abbrevs) |
2104 | return 0; | |
2105 | ||
2106 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); | |
2107 | info_ptr += bytes_read; | |
2108 | if (! abbrev_number) | |
2109 | { | |
f46c2da6 | 2110 | (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."), |
252b5132 RH |
2111 | abbrev_number); |
2112 | bfd_set_error (bfd_error_bad_value); | |
2113 | return 0; | |
2114 | } | |
2115 | ||
2116 | abbrev = lookup_abbrev (abbrev_number, abbrevs); | |
2117 | if (! abbrev) | |
2118 | { | |
f46c2da6 | 2119 | (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), |
252b5132 RH |
2120 | abbrev_number); |
2121 | bfd_set_error (bfd_error_bad_value); | |
2122 | return 0; | |
2123 | } | |
98591c73 | 2124 | |
dc810e39 | 2125 | amt = sizeof (struct comp_unit); |
818a27ac | 2126 | unit = bfd_zalloc (abfd, amt); |
252b5132 | 2127 | unit->abfd = abfd; |
98591c73 | 2128 | unit->addr_size = addr_size; |
d03ba2a1 | 2129 | unit->offset_size = offset_size; |
252b5132 RH |
2130 | unit->abbrevs = abbrevs; |
2131 | unit->end_ptr = end_ptr; | |
d03ba2a1 | 2132 | unit->stash = stash; |
c0c28ab8 | 2133 | unit->info_ptr_unit = info_ptr_unit; |
252b5132 RH |
2134 | |
2135 | for (i = 0; i < abbrev->num_attrs; ++i) | |
2136 | { | |
2137 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); | |
2138 | ||
2139 | /* Store the data if it is of an attribute we want to keep in a | |
2140 | partial symbol table. */ | |
2141 | switch (attr.name) | |
2142 | { | |
2143 | case DW_AT_stmt_list: | |
2144 | unit->stmtlist = 1; | |
482e2e37 | 2145 | unit->line_offset = attr.u.val; |
252b5132 RH |
2146 | break; |
2147 | ||
2148 | case DW_AT_name: | |
482e2e37 | 2149 | unit->name = attr.u.str; |
252b5132 RH |
2150 | break; |
2151 | ||
2152 | case DW_AT_low_pc: | |
a13afe8e FF |
2153 | low_pc = attr.u.val; |
2154 | /* If the compilation unit DIE has a DW_AT_low_pc attribute, | |
2155 | this is the base address to use when reading location | |
2156 | lists or range lists. */ | |
2157 | unit->base_address = low_pc; | |
252b5132 RH |
2158 | break; |
2159 | ||
2160 | case DW_AT_high_pc: | |
a13afe8e FF |
2161 | high_pc = attr.u.val; |
2162 | break; | |
2163 | ||
2164 | case DW_AT_ranges: | |
2165 | read_rangelist (unit, &unit->arange, attr.u.val); | |
252b5132 RH |
2166 | break; |
2167 | ||
2168 | case DW_AT_comp_dir: | |
2169 | { | |
f075ee0c | 2170 | char *comp_dir = attr.u.str; |
252b5132 RH |
2171 | if (comp_dir) |
2172 | { | |
2173 | /* Irix 6.2 native cc prepends <machine>.: to the compilation | |
2174 | directory, get rid of it. */ | |
818a27ac | 2175 | char *cp = strchr (comp_dir, ':'); |
252b5132 RH |
2176 | |
2177 | if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/') | |
2178 | comp_dir = cp + 1; | |
2179 | } | |
2180 | unit->comp_dir = comp_dir; | |
2181 | break; | |
2182 | } | |
2183 | ||
2184 | default: | |
2185 | break; | |
2186 | } | |
2187 | } | |
a13afe8e FF |
2188 | if (high_pc != 0) |
2189 | { | |
2190 | arange_add (unit->abfd, &unit->arange, low_pc, high_pc); | |
2191 | } | |
252b5132 RH |
2192 | |
2193 | unit->first_child_die_ptr = info_ptr; | |
2194 | return unit; | |
2195 | } | |
2196 | ||
6dd55cb7 L |
2197 | /* Return TRUE if UNIT may contain the address given by ADDR. When |
2198 | there are functions written entirely with inline asm statements, the | |
2199 | range info in the compilation unit header may not be correct. We | |
2200 | need to consult the line info table to see if a compilation unit | |
2201 | really contains the given address. */ | |
252b5132 | 2202 | |
b34976b6 | 2203 | static bfd_boolean |
818a27ac | 2204 | comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr) |
252b5132 | 2205 | { |
f623be2b RH |
2206 | struct arange *arange; |
2207 | ||
2208 | if (unit->error) | |
b34976b6 | 2209 | return FALSE; |
f623be2b RH |
2210 | |
2211 | arange = &unit->arange; | |
2212 | do | |
2213 | { | |
2214 | if (addr >= arange->low && addr < arange->high) | |
b34976b6 | 2215 | return TRUE; |
f623be2b RH |
2216 | arange = arange->next; |
2217 | } | |
2218 | while (arange); | |
98591c73 | 2219 | |
b34976b6 | 2220 | return FALSE; |
252b5132 RH |
2221 | } |
2222 | ||
252b5132 RH |
2223 | /* If UNIT contains ADDR, set the output parameters to the values for |
2224 | the line containing ADDR. The output parameters, FILENAME_PTR, | |
2225 | FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects | |
98591c73 | 2226 | to be filled in. |
252b5132 | 2227 | |
ab3acfbe | 2228 | Return TRUE if UNIT contains ADDR, and no errors were encountered; |
b34976b6 | 2229 | FALSE otherwise. */ |
252b5132 | 2230 | |
b34976b6 | 2231 | static bfd_boolean |
818a27ac AM |
2232 | comp_unit_find_nearest_line (struct comp_unit *unit, |
2233 | bfd_vma addr, | |
2234 | const char **filename_ptr, | |
2235 | const char **functionname_ptr, | |
2236 | unsigned int *linenumber_ptr, | |
2237 | struct dwarf2_debug *stash) | |
252b5132 | 2238 | { |
b34976b6 AM |
2239 | bfd_boolean line_p; |
2240 | bfd_boolean func_p; | |
1ee24f27 | 2241 | struct funcinfo *function; |
98591c73 | 2242 | |
252b5132 | 2243 | if (unit->error) |
b34976b6 | 2244 | return FALSE; |
252b5132 RH |
2245 | |
2246 | if (! unit->line_table) | |
2247 | { | |
2248 | if (! unit->stmtlist) | |
2249 | { | |
2250 | unit->error = 1; | |
b34976b6 | 2251 | return FALSE; |
252b5132 | 2252 | } |
98591c73 | 2253 | |
51db3708 | 2254 | unit->line_table = decode_line_info (unit, stash); |
252b5132 RH |
2255 | |
2256 | if (! unit->line_table) | |
2257 | { | |
2258 | unit->error = 1; | |
b34976b6 | 2259 | return FALSE; |
252b5132 | 2260 | } |
98591c73 | 2261 | |
3f5864e1 | 2262 | if (unit->first_child_die_ptr < unit->end_ptr |
5420f73d | 2263 | && ! scan_unit_for_symbols (unit)) |
252b5132 RH |
2264 | { |
2265 | unit->error = 1; | |
b34976b6 | 2266 | return FALSE; |
252b5132 RH |
2267 | } |
2268 | } | |
2269 | ||
1ee24f27 | 2270 | function = NULL; |
4ab527b0 | 2271 | func_p = lookup_address_in_function_table (unit, addr, |
e2f6d277 | 2272 | &function, functionname_ptr); |
4ab527b0 FF |
2273 | if (func_p && (function->tag == DW_TAG_inlined_subroutine)) |
2274 | stash->inliner_chain = function; | |
e2f6d277 NC |
2275 | line_p = lookup_address_in_line_info_table (unit->line_table, addr, |
2276 | function, filename_ptr, | |
252b5132 | 2277 | linenumber_ptr); |
b34976b6 | 2278 | return line_p || func_p; |
252b5132 RH |
2279 | } |
2280 | ||
bd210d54 NC |
2281 | /* Check to see if line info is already decoded in a comp_unit. |
2282 | If not, decode it. Returns TRUE if no errors were encountered; | |
5420f73d L |
2283 | FALSE otherwise. */ |
2284 | ||
2285 | static bfd_boolean | |
bd210d54 NC |
2286 | comp_unit_maybe_decode_line_info (struct comp_unit *unit, |
2287 | struct dwarf2_debug *stash) | |
5420f73d L |
2288 | { |
2289 | if (unit->error) | |
2290 | return FALSE; | |
2291 | ||
2292 | if (! unit->line_table) | |
2293 | { | |
2294 | if (! unit->stmtlist) | |
2295 | { | |
2296 | unit->error = 1; | |
2297 | return FALSE; | |
2298 | } | |
2299 | ||
2300 | unit->line_table = decode_line_info (unit, stash); | |
2301 | ||
2302 | if (! unit->line_table) | |
2303 | { | |
2304 | unit->error = 1; | |
2305 | return FALSE; | |
2306 | } | |
2307 | ||
2308 | if (unit->first_child_die_ptr < unit->end_ptr | |
2309 | && ! scan_unit_for_symbols (unit)) | |
2310 | { | |
2311 | unit->error = 1; | |
2312 | return FALSE; | |
2313 | } | |
2314 | } | |
2315 | ||
bd210d54 NC |
2316 | return TRUE; |
2317 | } | |
2318 | ||
2319 | /* If UNIT contains SYM at ADDR, set the output parameters to the | |
2320 | values for the line containing SYM. The output parameters, | |
2321 | FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be | |
2322 | filled in. | |
2323 | ||
2324 | Return TRUE if UNIT contains SYM, and no errors were encountered; | |
2325 | FALSE otherwise. */ | |
2326 | ||
2327 | static bfd_boolean | |
2328 | comp_unit_find_line (struct comp_unit *unit, | |
2329 | asymbol *sym, | |
2330 | bfd_vma addr, | |
2331 | const char **filename_ptr, | |
2332 | unsigned int *linenumber_ptr, | |
2333 | struct dwarf2_debug *stash) | |
2334 | { | |
2335 | if (!comp_unit_maybe_decode_line_info (unit, stash)) | |
2336 | return FALSE; | |
2337 | ||
5420f73d L |
2338 | if (sym->flags & BSF_FUNCTION) |
2339 | return lookup_symbol_in_function_table (unit, sym, addr, | |
2340 | filename_ptr, | |
2341 | linenumber_ptr); | |
bd210d54 NC |
2342 | |
2343 | return lookup_symbol_in_variable_table (unit, sym, addr, | |
2344 | filename_ptr, | |
2345 | linenumber_ptr); | |
2346 | } | |
2347 | ||
2348 | static struct funcinfo * | |
2349 | reverse_funcinfo_list (struct funcinfo *head) | |
2350 | { | |
2351 | struct funcinfo *rhead; | |
2352 | struct funcinfo *temp; | |
2353 | ||
2354 | for (rhead = NULL; head; head = temp) | |
2355 | { | |
2356 | temp = head->prev_func; | |
2357 | head->prev_func = rhead; | |
2358 | rhead = head; | |
2359 | } | |
2360 | return rhead; | |
2361 | } | |
2362 | ||
2363 | static struct varinfo * | |
2364 | reverse_varinfo_list (struct varinfo *head) | |
2365 | { | |
2366 | struct varinfo *rhead; | |
2367 | struct varinfo *temp; | |
2368 | ||
2369 | for (rhead = NULL; head; head = temp) | |
2370 | { | |
2371 | temp = head->prev_var; | |
2372 | head->prev_var = rhead; | |
2373 | rhead = head; | |
2374 | } | |
2375 | return rhead; | |
2376 | } | |
2377 | ||
2378 | /* Extract all interesting funcinfos and varinfos of a compilation | |
2379 | unit into hash tables for faster lookup. Returns TRUE if no | |
2380 | errors were enountered; FALSE otherwise. */ | |
2381 | ||
2382 | static bfd_boolean | |
2383 | comp_unit_hash_info (struct dwarf2_debug *stash, | |
2384 | struct comp_unit *unit, | |
2385 | struct info_hash_table *funcinfo_hash_table, | |
2386 | struct info_hash_table *varinfo_hash_table) | |
2387 | { | |
2388 | struct funcinfo* each_func; | |
2389 | struct varinfo* each_var; | |
2390 | bfd_boolean okay = TRUE; | |
2391 | ||
2392 | BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED); | |
2393 | ||
2394 | if (!comp_unit_maybe_decode_line_info (unit, stash)) | |
2395 | return FALSE; | |
2396 | ||
2397 | BFD_ASSERT (!unit->cached); | |
2398 | ||
2399 | /* To preserve the original search order, we went to visit the function | |
2400 | infos in the reversed order of the list. However, making the list | |
2401 | bi-directional use quite a bit of extra memory. So we reverse | |
2402 | the list first, traverse the list in the now reversed order and | |
2403 | finally reverse the list again to get back the original order. */ | |
2404 | unit->function_table = reverse_funcinfo_list (unit->function_table); | |
2405 | for (each_func = unit->function_table; | |
2406 | each_func && okay; | |
2407 | each_func = each_func->prev_func) | |
2408 | { | |
2409 | /* Skip nameless functions. */ | |
2410 | if (each_func->name) | |
2411 | /* There is no need to copy name string into hash table as | |
2412 | name string is either in the dwarf string buffer or | |
2413 | info in the stash. */ | |
2414 | okay = insert_info_hash_table (funcinfo_hash_table, each_func->name, | |
2415 | (void*) each_func, FALSE); | |
2416 | } | |
2417 | unit->function_table = reverse_funcinfo_list (unit->function_table); | |
2418 | if (!okay) | |
2419 | return FALSE; | |
2420 | ||
2421 | /* We do the same for variable infos. */ | |
2422 | unit->variable_table = reverse_varinfo_list (unit->variable_table); | |
2423 | for (each_var = unit->variable_table; | |
2424 | each_var && okay; | |
2425 | each_var = each_var->prev_var) | |
2426 | { | |
2427 | /* Skip stack vars and vars with no files or names. */ | |
2428 | if (each_var->stack == 0 | |
2429 | && each_var->file != NULL | |
2430 | && each_var->name != NULL) | |
2431 | /* There is no need to copy name string into hash table as | |
2432 | name string is either in the dwarf string buffer or | |
2433 | info in the stash. */ | |
2434 | okay = insert_info_hash_table (varinfo_hash_table, each_var->name, | |
2435 | (void*) each_var, FALSE); | |
2436 | } | |
2437 | ||
2438 | unit->variable_table = reverse_varinfo_list (unit->variable_table); | |
2439 | unit->cached = TRUE; | |
2440 | return okay; | |
5420f73d L |
2441 | } |
2442 | ||
e2f6d277 NC |
2443 | /* Locate a section in a BFD containing debugging info. The search starts |
2444 | from the section after AFTER_SEC, or from the first section in the BFD if | |
2445 | AFTER_SEC is NULL. The search works by examining the names of the | |
2446 | sections. There are two permissiable names. The first is .debug_info. | |
2447 | This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi. | |
2448 | This is a variation on the .debug_info section which has a checksum | |
2449 | describing the contents appended onto the name. This allows the linker to | |
2450 | identify and discard duplicate debugging sections for different | |
2451 | compilation units. */ | |
a092b084 NC |
2452 | #define DWARF2_DEBUG_INFO ".debug_info" |
2453 | #define GNU_LINKONCE_INFO ".gnu.linkonce.wi." | |
2454 | ||
2455 | static asection * | |
818a27ac | 2456 | find_debug_info (bfd *abfd, asection *after_sec) |
a092b084 NC |
2457 | { |
2458 | asection * msec; | |
2459 | ||
0d161102 | 2460 | msec = after_sec != NULL ? after_sec->next : abfd->sections; |
a092b084 NC |
2461 | |
2462 | while (msec) | |
2463 | { | |
2464 | if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0) | |
2465 | return msec; | |
2466 | ||
0112cd26 | 2467 | if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO)) |
a092b084 NC |
2468 | return msec; |
2469 | ||
2470 | msec = msec->next; | |
2471 | } | |
2472 | ||
2473 | return NULL; | |
2474 | } | |
2475 | ||
d4c32a81 L |
2476 | /* Unset vmas for loadable sections in STASH. */ |
2477 | ||
2478 | static void | |
2479 | unset_sections (struct dwarf2_debug *stash) | |
2480 | { | |
2481 | unsigned int i; | |
2482 | struct loadable_section *p; | |
2483 | ||
2484 | i = stash->loadable_section_count; | |
2485 | p = stash->loadable_sections; | |
2486 | for (; i > 0; i--, p++) | |
2487 | p->section->vma = 0; | |
2488 | } | |
2489 | ||
2490 | /* Set unique vmas for loadable sections in ABFD and save vmas in | |
2491 | STASH for unset_sections. */ | |
35ccda9e L |
2492 | |
2493 | static bfd_boolean | |
d4c32a81 | 2494 | place_sections (bfd *abfd, struct dwarf2_debug *stash) |
35ccda9e | 2495 | { |
d4c32a81 L |
2496 | struct loadable_section *p; |
2497 | unsigned int i; | |
2498 | ||
2499 | if (stash->loadable_section_count != 0) | |
35ccda9e | 2500 | { |
d4c32a81 L |
2501 | i = stash->loadable_section_count; |
2502 | p = stash->loadable_sections; | |
2503 | for (; i > 0; i--, p++) | |
2504 | p->section->vma = p->adj_vma; | |
2505 | } | |
2506 | else | |
2507 | { | |
2508 | asection *sect; | |
2509 | bfd_vma last_vma = 0; | |
2510 | bfd_size_type amt; | |
2511 | struct loadable_section *p; | |
35ccda9e | 2512 | |
d4c32a81 L |
2513 | i = 0; |
2514 | for (sect = abfd->sections; sect != NULL; sect = sect->next) | |
35ccda9e | 2515 | { |
d4c32a81 L |
2516 | bfd_size_type sz; |
2517 | ||
2518 | if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0) | |
2519 | continue; | |
2520 | ||
2521 | sz = sect->rawsize ? sect->rawsize : sect->size; | |
2522 | if (sz == 0) | |
2523 | continue; | |
2524 | ||
2525 | i++; | |
2526 | } | |
2527 | ||
2528 | amt = i * sizeof (struct loadable_section); | |
2529 | p = (struct loadable_section *) bfd_zalloc (abfd, amt); | |
2530 | if (! p) | |
2531 | return FALSE; | |
2532 | ||
2533 | stash->loadable_sections = p; | |
2534 | stash->loadable_section_count = i; | |
2535 | ||
2536 | for (sect = abfd->sections; sect != NULL; sect = sect->next) | |
2537 | { | |
2538 | bfd_size_type sz; | |
2539 | ||
2540 | if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0) | |
2541 | continue; | |
2542 | ||
2543 | sz = sect->rawsize ? sect->rawsize : sect->size; | |
2544 | if (sz == 0) | |
2545 | continue; | |
2546 | ||
2547 | p->section = sect; | |
2548 | if (last_vma != 0) | |
2549 | { | |
2550 | /* Align the new address to the current section | |
2551 | alignment. */ | |
2552 | last_vma = ((last_vma | |
2553 | + ~((bfd_vma) -1 << sect->alignment_power)) | |
2554 | & ((bfd_vma) -1 << sect->alignment_power)); | |
2555 | sect->vma = last_vma; | |
2556 | } | |
2557 | p->adj_vma = sect->vma; | |
2558 | last_vma += sect->vma + sz; | |
2559 | ||
2560 | p++; | |
35ccda9e L |
2561 | } |
2562 | } | |
2563 | ||
2564 | return TRUE; | |
2565 | } | |
2566 | ||
bd210d54 NC |
2567 | /* Look up a funcinfo by name using the given info hash table. If found, |
2568 | also update the locations pointed to by filename_ptr and linenumber_ptr. | |
2569 | ||
2570 | This function returns TRUE if a funcinfo that matches the given symbol | |
2571 | and address is found with any error; otherwise it returns FALSE. */ | |
2572 | ||
2573 | static bfd_boolean | |
2574 | info_hash_lookup_funcinfo (struct info_hash_table *hash_table, | |
2575 | asymbol *sym, | |
2576 | bfd_vma addr, | |
2577 | const char **filename_ptr, | |
2578 | unsigned int *linenumber_ptr) | |
2579 | { | |
2580 | struct funcinfo* each_func; | |
2581 | struct funcinfo* best_fit = NULL; | |
2582 | struct info_list_node *node; | |
2583 | struct arange *arange; | |
2584 | const char *name = bfd_asymbol_name (sym); | |
2585 | asection *sec = bfd_get_section (sym); | |
2586 | ||
2587 | for (node = lookup_info_hash_table (hash_table, name); | |
2588 | node; | |
2589 | node = node->next) | |
2590 | { | |
2591 | each_func = node->info; | |
2592 | for (arange = &each_func->arange; | |
2593 | arange; | |
2594 | arange = arange->next) | |
2595 | { | |
2596 | if ((!each_func->sec || each_func->sec == sec) | |
2597 | && addr >= arange->low | |
2598 | && addr < arange->high | |
2599 | && (!best_fit | |
2600 | || ((arange->high - arange->low) | |
2601 | < (best_fit->arange.high - best_fit->arange.low)))) | |
2602 | best_fit = each_func; | |
2603 | } | |
2604 | } | |
2605 | ||
2606 | if (best_fit) | |
2607 | { | |
2608 | best_fit->sec = sec; | |
2609 | *filename_ptr = best_fit->file; | |
2610 | *linenumber_ptr = best_fit->line; | |
2611 | return TRUE; | |
2612 | } | |
2613 | ||
2614 | return FALSE; | |
2615 | } | |
2616 | ||
2617 | /* Look up a varinfo by name using the given info hash table. If found, | |
2618 | also update the locations pointed to by filename_ptr and linenumber_ptr. | |
2619 | ||
2620 | This function returns TRUE if a varinfo that matches the given symbol | |
2621 | and address is found with any error; otherwise it returns FALSE. */ | |
2622 | ||
2623 | static bfd_boolean | |
2624 | info_hash_lookup_varinfo (struct info_hash_table *hash_table, | |
2625 | asymbol *sym, | |
2626 | bfd_vma addr, | |
2627 | const char **filename_ptr, | |
2628 | unsigned int *linenumber_ptr) | |
2629 | { | |
2630 | const char *name = bfd_asymbol_name (sym); | |
2631 | asection *sec = bfd_get_section (sym); | |
2632 | struct varinfo* each; | |
2633 | struct info_list_node *node; | |
2634 | ||
2635 | for (node = lookup_info_hash_table (hash_table, name); | |
2636 | node; | |
2637 | node = node->next) | |
2638 | { | |
2639 | each = node->info; | |
2640 | if (each->addr == addr | |
2641 | && (!each->sec || each->sec == sec)) | |
2642 | { | |
2643 | each->sec = sec; | |
2644 | *filename_ptr = each->file; | |
2645 | *linenumber_ptr = each->line; | |
2646 | return TRUE; | |
2647 | } | |
2648 | } | |
2649 | ||
2650 | return FALSE; | |
2651 | } | |
2652 | ||
2653 | /* Update the funcinfo and varinfo info hash tables if they are | |
2654 | not up to date. Returns TRUE if there is no error; otherwise | |
2655 | returns FALSE and disable the info hash tables. */ | |
2656 | ||
2657 | static bfd_boolean | |
2658 | stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash) | |
2659 | { | |
2660 | struct comp_unit *each; | |
2661 | ||
2662 | /* Exit if hash tables are up-to-date. */ | |
2663 | if (stash->all_comp_units == stash->hash_units_head) | |
2664 | return TRUE; | |
2665 | ||
2666 | if (stash->hash_units_head) | |
2667 | each = stash->hash_units_head->prev_unit; | |
2668 | else | |
2669 | each = stash->last_comp_unit; | |
2670 | ||
2671 | while (each) | |
2672 | { | |
2673 | if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table, | |
2674 | stash->varinfo_hash_table)) | |
2675 | { | |
2676 | stash->info_hash_status = STASH_INFO_HASH_DISABLED; | |
2677 | return FALSE; | |
2678 | } | |
2679 | each = each->prev_unit; | |
2680 | } | |
2681 | ||
2682 | stash->hash_units_head = stash->all_comp_units; | |
2683 | return TRUE; | |
2684 | } | |
2685 | ||
2686 | /* Check consistency of info hash tables. This is for debugging only. */ | |
2687 | ||
2688 | static void ATTRIBUTE_UNUSED | |
2689 | stash_verify_info_hash_table (struct dwarf2_debug *stash) | |
2690 | { | |
2691 | struct comp_unit *each_unit; | |
2692 | struct funcinfo *each_func; | |
2693 | struct varinfo *each_var; | |
2694 | struct info_list_node *node; | |
2695 | bfd_boolean found; | |
2696 | ||
2697 | for (each_unit = stash->all_comp_units; | |
2698 | each_unit; | |
2699 | each_unit = each_unit->next_unit) | |
2700 | { | |
2701 | for (each_func = each_unit->function_table; | |
2702 | each_func; | |
2703 | each_func = each_func->prev_func) | |
2704 | { | |
2705 | if (!each_func->name) | |
2706 | continue; | |
2707 | node = lookup_info_hash_table (stash->funcinfo_hash_table, | |
2708 | each_func->name); | |
2709 | BFD_ASSERT (node); | |
2710 | found = FALSE; | |
2711 | while (node && !found) | |
2712 | { | |
2713 | found = node->info == each_func; | |
2714 | node = node->next; | |
2715 | } | |
2716 | BFD_ASSERT (found); | |
2717 | } | |
2718 | ||
2719 | for (each_var = each_unit->variable_table; | |
2720 | each_var; | |
2721 | each_var = each_var->prev_var) | |
2722 | { | |
2723 | if (!each_var->name || !each_var->file || each_var->stack) | |
2724 | continue; | |
2725 | node = lookup_info_hash_table (stash->varinfo_hash_table, | |
2726 | each_var->name); | |
2727 | BFD_ASSERT (node); | |
2728 | found = FALSE; | |
2729 | while (node && !found) | |
2730 | { | |
2731 | found = node->info == each_var; | |
2732 | node = node->next; | |
2733 | } | |
2734 | BFD_ASSERT (found); | |
2735 | } | |
2736 | } | |
2737 | } | |
2738 | ||
2739 | /* Check to see if we want to enable the info hash tables, which consume | |
2740 | quite a bit of memory. Currently we only check the number times | |
2741 | bfd_dwarf2_find_line is called. In the future, we may also want to | |
2742 | take the number of symbols into account. */ | |
2743 | ||
2744 | static void | |
2745 | stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash) | |
2746 | { | |
2747 | BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF); | |
2748 | ||
2749 | if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER) | |
2750 | return; | |
2751 | ||
2752 | /* FIXME: Maybe we should check the reduce_memory_overheads | |
2753 | and optimize fields in the bfd_link_info structure ? */ | |
2754 | ||
2755 | /* Create hash tables. */ | |
2756 | stash->funcinfo_hash_table = create_info_hash_table (abfd); | |
2757 | stash->varinfo_hash_table = create_info_hash_table (abfd); | |
2758 | if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table) | |
2759 | { | |
2760 | /* Turn off info hashes if any allocation above fails. */ | |
2761 | stash->info_hash_status = STASH_INFO_HASH_DISABLED; | |
2762 | return; | |
2763 | } | |
2764 | /* We need a forced update so that the info hash tables will | |
2765 | be created even though there is no compilation unit. That | |
2766 | happens if STASH_INFO_HASH_TRIGGER is 0. */ | |
2767 | stash_maybe_update_info_hash_tables (stash); | |
2768 | stash->info_hash_status = STASH_INFO_HASH_ON; | |
2769 | } | |
2770 | ||
2771 | /* Find the file and line associated with a symbol and address using the | |
2772 | info hash tables of a stash. If there is a match, the function returns | |
2773 | TRUE and update the locations pointed to by filename_ptr and linenumber_ptr; | |
2774 | otherwise it returns FALSE. */ | |
2775 | ||
2776 | static bfd_boolean | |
2777 | stash_find_line_fast (struct dwarf2_debug *stash, | |
2778 | asymbol *sym, | |
2779 | bfd_vma addr, | |
2780 | const char **filename_ptr, | |
2781 | unsigned int *linenumber_ptr) | |
2782 | { | |
2783 | BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON); | |
2784 | ||
2785 | if (sym->flags & BSF_FUNCTION) | |
2786 | return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr, | |
2787 | filename_ptr, linenumber_ptr); | |
2788 | return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr, | |
2789 | filename_ptr, linenumber_ptr); | |
2790 | } | |
2791 | ||
bec42b15 NC |
2792 | /* Find the source code location of SYMBOL. If SYMBOL is NULL |
2793 | then find the nearest source code location corresponding to | |
2794 | the address SECTION + OFFSET. | |
2795 | Returns TRUE if the line is found without error and fills in | |
2796 | FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was | |
2797 | NULL the FUNCTIONNAME_PTR is also filled in. | |
2798 | SYMBOLS contains the symbol table for ABFD. | |
2799 | ADDR_SIZE is the number of bytes in the initial .debug_info length | |
2800 | field and in the abbreviation offset, or zero to indicate that the | |
2801 | default value should be used. */ | |
252b5132 | 2802 | |
bec42b15 NC |
2803 | static bfd_boolean |
2804 | find_line (bfd *abfd, | |
2805 | asection *section, | |
2806 | bfd_vma offset, | |
2807 | asymbol *symbol, | |
2808 | asymbol **symbols, | |
2809 | const char **filename_ptr, | |
2810 | const char **functionname_ptr, | |
2811 | unsigned int *linenumber_ptr, | |
2812 | unsigned int addr_size, | |
2813 | void **pinfo) | |
252b5132 RH |
2814 | { |
2815 | /* Read each compilation unit from the section .debug_info, and check | |
2816 | to see if it contains the address we are searching for. If yes, | |
2817 | lookup the address, and return the line number info. If no, go | |
98591c73 | 2818 | on to the next compilation unit. |
252b5132 RH |
2819 | |
2820 | We keep a list of all the previously read compilation units, and | |
98591c73 | 2821 | a pointer to the next un-read compilation unit. Check the |
a092b084 | 2822 | previously read units before reading more. */ |
1ba54ee0 | 2823 | struct dwarf2_debug *stash; |
a092b084 | 2824 | /* What address are we looking for? */ |
1ba54ee0 | 2825 | bfd_vma addr; |
252b5132 | 2826 | struct comp_unit* each; |
d4c32a81 | 2827 | bfd_vma found = FALSE; |
bec42b15 | 2828 | bfd_boolean do_line; |
d4c32a81 | 2829 | |
1ba54ee0 | 2830 | stash = *pinfo; |
d4c32a81 L |
2831 | |
2832 | if (! stash) | |
2833 | { | |
2834 | bfd_size_type amt = sizeof (struct dwarf2_debug); | |
2835 | ||
2836 | stash = bfd_zalloc (abfd, amt); | |
2837 | if (! stash) | |
2838 | return FALSE; | |
2839 | } | |
2840 | ||
2841 | /* In a relocatable file, 2 functions may have the same address. | |
2842 | We change the section vma so that they won't overlap. */ | |
2843 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) | |
2844 | { | |
2845 | if (! place_sections (abfd, stash)) | |
2846 | return FALSE; | |
2847 | } | |
2848 | ||
bec42b15 NC |
2849 | do_line = (section == NULL |
2850 | && offset == 0 | |
2851 | && functionname_ptr == NULL | |
2852 | && symbol != NULL); | |
2853 | if (do_line) | |
2854 | { | |
2855 | addr = symbol->value; | |
2856 | section = bfd_get_section (symbol); | |
2857 | } | |
2858 | else if (section != NULL | |
2859 | && functionname_ptr != NULL | |
2860 | && symbol == NULL) | |
2861 | addr = offset; | |
2862 | else | |
2863 | abort (); | |
2864 | ||
1ba54ee0 | 2865 | if (section->output_section) |
6dd55cb7 | 2866 | addr += section->output_section->vma + section->output_offset; |
1ba54ee0 | 2867 | else |
6dd55cb7 | 2868 | addr += section->vma; |
252b5132 | 2869 | *filename_ptr = NULL; |
6b33789f NC |
2870 | if (! do_line) |
2871 | *functionname_ptr = NULL; | |
252b5132 RH |
2872 | *linenumber_ptr = 0; |
2873 | ||
d4c32a81 | 2874 | if (! *pinfo) |
252b5132 | 2875 | { |
0d161102 | 2876 | bfd *debug_bfd; |
dc810e39 | 2877 | bfd_size_type total_size; |
252b5132 | 2878 | asection *msec; |
252b5132 | 2879 | |
818a27ac | 2880 | *pinfo = stash; |
3fde5a36 | 2881 | |
a092b084 | 2882 | msec = find_debug_info (abfd, NULL); |
0d161102 NC |
2883 | if (msec == NULL) |
2884 | { | |
0d4a1476 | 2885 | char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR); |
0d161102 NC |
2886 | |
2887 | if (debug_filename == NULL) | |
2888 | /* No dwarf2 info, and no gnu_debuglink to follow. | |
2889 | Note that at this point the stash has been allocated, but | |
2890 | contains zeros. This lets future calls to this function | |
2891 | fail more quickly. */ | |
2892 | goto done; | |
2893 | ||
2894 | if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL | |
2895 | || ! bfd_check_format (debug_bfd, bfd_object) | |
2896 | || (msec = find_debug_info (debug_bfd, NULL)) == NULL) | |
2897 | { | |
2898 | if (debug_bfd) | |
2899 | bfd_close (debug_bfd); | |
2900 | /* FIXME: Should we report our failure to follow the debuglink ? */ | |
2901 | free (debug_filename); | |
2902 | goto done; | |
2903 | } | |
2904 | } | |
2905 | else | |
2906 | debug_bfd = abfd; | |
a092b084 NC |
2907 | |
2908 | /* There can be more than one DWARF2 info section in a BFD these days. | |
e82ce529 | 2909 | Read them all in and produce one large stash. We do this in two |
a092b084 NC |
2910 | passes - in the first pass we just accumulate the section sizes. |
2911 | In the second pass we read in the section's contents. The allows | |
2912 | us to avoid reallocing the data as we add sections to the stash. */ | |
0d161102 | 2913 | for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec)) |
eea6121a | 2914 | total_size += msec->size; |
98591c73 | 2915 | |
0d161102 | 2916 | stash->info_ptr = bfd_alloc (debug_bfd, total_size); |
a092b084 | 2917 | if (stash->info_ptr == NULL) |
d4c32a81 | 2918 | goto done; |
252b5132 | 2919 | |
a092b084 NC |
2920 | stash->info_ptr_end = stash->info_ptr; |
2921 | ||
0d161102 | 2922 | for (msec = find_debug_info (debug_bfd, NULL); |
a092b084 | 2923 | msec; |
0d161102 | 2924 | msec = find_debug_info (debug_bfd, msec)) |
252b5132 | 2925 | { |
dc810e39 AM |
2926 | bfd_size_type size; |
2927 | bfd_size_type start; | |
a092b084 | 2928 | |
eea6121a | 2929 | size = msec->size; |
a092b084 NC |
2930 | if (size == 0) |
2931 | continue; | |
2932 | ||
2933 | start = stash->info_ptr_end - stash->info_ptr; | |
2934 | ||
6e84a906 | 2935 | if ((bfd_simple_get_relocated_section_contents |
0d161102 | 2936 | (debug_bfd, msec, stash->info_ptr + start, symbols)) == NULL) |
a092b084 NC |
2937 | continue; |
2938 | ||
2939 | stash->info_ptr_end = stash->info_ptr + start + size; | |
252b5132 RH |
2940 | } |
2941 | ||
f2363ce5 AO |
2942 | BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size); |
2943 | ||
0d161102 | 2944 | stash->sec = find_debug_info (debug_bfd, NULL); |
f2363ce5 AO |
2945 | stash->sec_info_ptr = stash->info_ptr; |
2946 | stash->syms = symbols; | |
0d161102 | 2947 | stash->bfd = debug_bfd; |
252b5132 | 2948 | } |
a092b084 | 2949 | |
98591c73 | 2950 | /* A null info_ptr indicates that there is no dwarf2 info |
a092b084 | 2951 | (or that an error occured while setting up the stash). */ |
252b5132 | 2952 | if (! stash->info_ptr) |
d4c32a81 | 2953 | goto done; |
252b5132 | 2954 | |
4ab527b0 FF |
2955 | stash->inliner_chain = NULL; |
2956 | ||
a092b084 | 2957 | /* Check the previously read comp. units first. */ |
bd210d54 NC |
2958 | if (do_line) |
2959 | { | |
2960 | /* The info hash tables use quite a bit of memory. We may not want to | |
2961 | always use them. We use some heuristics to decide if and when to | |
2962 | turn it on. */ | |
2963 | if (stash->info_hash_status == STASH_INFO_HASH_OFF) | |
2964 | stash_maybe_enable_info_hash_tables (abfd, stash); | |
2965 | ||
2966 | /* Keep info hash table up to date if they are available. Note that we | |
2967 | may disable the hash tables if there is any error duing update. */ | |
2968 | if (stash->info_hash_status == STASH_INFO_HASH_ON) | |
2969 | stash_maybe_update_info_hash_tables (stash); | |
2970 | ||
2971 | if (stash->info_hash_status == STASH_INFO_HASH_ON) | |
2972 | { | |
2973 | found = stash_find_line_fast (stash, symbol, addr, filename_ptr, | |
2974 | linenumber_ptr); | |
2975 | if (found) | |
2976 | goto done; | |
2977 | } | |
0d161102 | 2978 | else |
bd210d54 NC |
2979 | { |
2980 | /* Check the previously read comp. units first. */ | |
2981 | for (each = stash->all_comp_units; each; each = each->next_unit) | |
2982 | if ((symbol->flags & BSF_FUNCTION) == 0 | |
2983 | || comp_unit_contains_address (each, addr)) | |
2984 | { | |
2985 | found = comp_unit_find_line (each, symbol, addr, filename_ptr, | |
2986 | linenumber_ptr, stash); | |
2987 | if (found) | |
2988 | goto done; | |
2989 | } | |
2990 | } | |
2991 | } | |
2992 | else | |
2993 | { | |
2994 | for (each = stash->all_comp_units; each; each = each->next_unit) | |
2995 | { | |
2996 | found = (comp_unit_contains_address (each, addr) | |
2997 | && comp_unit_find_nearest_line (each, addr, | |
2998 | filename_ptr, | |
2999 | functionname_ptr, | |
3000 | linenumber_ptr, | |
3001 | stash)); | |
3002 | if (found) | |
3003 | goto done; | |
3004 | } | |
5420f73d L |
3005 | } |
3006 | ||
5420f73d L |
3007 | /* The DWARF2 spec says that the initial length field, and the |
3008 | offset of the abbreviation table, should both be 4-byte values. | |
3009 | However, some compilers do things differently. */ | |
3010 | if (addr_size == 0) | |
3011 | addr_size = 4; | |
3012 | BFD_ASSERT (addr_size == 4 || addr_size == 8); | |
3013 | ||
3014 | /* Read each remaining comp. units checking each as they are read. */ | |
3015 | while (stash->info_ptr < stash->info_ptr_end) | |
3016 | { | |
3017 | bfd_vma length; | |
3018 | unsigned int offset_size = addr_size; | |
3019 | bfd_byte *info_ptr_unit = stash->info_ptr; | |
3020 | ||
0d161102 | 3021 | length = read_4_bytes (stash->bfd, stash->info_ptr); |
bec42b15 NC |
3022 | /* A 0xffffff length is the DWARF3 way of indicating |
3023 | we use 64-bit offsets, instead of 32-bit offsets. */ | |
5420f73d L |
3024 | if (length == 0xffffffff) |
3025 | { | |
3026 | offset_size = 8; | |
0d161102 | 3027 | length = read_8_bytes (stash->bfd, stash->info_ptr + 4); |
5420f73d L |
3028 | stash->info_ptr += 12; |
3029 | } | |
3030 | /* A zero length is the IRIX way of indicating 64-bit offsets, | |
3031 | mostly because the 64-bit length will generally fit in 32 | |
3032 | bits, and the endianness helps. */ | |
3033 | else if (length == 0) | |
3034 | { | |
3035 | offset_size = 8; | |
0d161102 | 3036 | length = read_4_bytes (stash->bfd, stash->info_ptr + 4); |
5420f73d L |
3037 | stash->info_ptr += 8; |
3038 | } | |
024b2372 CD |
3039 | /* In the absence of the hints above, we assume 32-bit DWARF2 |
3040 | offsets even for targets with 64-bit addresses, because: | |
3041 | a) most of the time these targets will not have generated | |
3042 | more than 2Gb of debug info and so will not need 64-bit | |
3043 | offsets, | |
3044 | and | |
3045 | b) if they do use 64-bit offsets but they are not using | |
3046 | the size hints that are tested for above then they are | |
3047 | not conforming to the DWARF3 standard anyway. */ | |
5420f73d L |
3048 | else if (addr_size == 8) |
3049 | { | |
024b2372 CD |
3050 | offset_size = 4; |
3051 | stash->info_ptr += 4; | |
5420f73d L |
3052 | } |
3053 | else | |
3054 | stash->info_ptr += 4; | |
3055 | ||
3056 | if (length > 0) | |
3057 | { | |
0d161102 | 3058 | each = parse_comp_unit (stash, length, info_ptr_unit, |
5420f73d L |
3059 | offset_size); |
3060 | stash->info_ptr += length; | |
3061 | ||
3062 | if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr) | |
3063 | == stash->sec->size) | |
3064 | { | |
0d161102 | 3065 | stash->sec = find_debug_info (stash->bfd, stash->sec); |
5420f73d L |
3066 | stash->sec_info_ptr = stash->info_ptr; |
3067 | } | |
3068 | ||
3069 | if (each) | |
3070 | { | |
bd210d54 NC |
3071 | if (stash->all_comp_units) |
3072 | stash->all_comp_units->prev_unit = each; | |
3073 | else | |
3074 | stash->last_comp_unit = each; | |
3075 | ||
5420f73d L |
3076 | each->next_unit = stash->all_comp_units; |
3077 | stash->all_comp_units = each; | |
3078 | ||
3079 | /* DW_AT_low_pc and DW_AT_high_pc are optional for | |
3080 | compilation units. If we don't have them (i.e., | |
3081 | unit->high == 0), we need to consult the line info | |
3082 | table to see if a compilation unit contains the given | |
3083 | address. */ | |
bec42b15 NC |
3084 | if (do_line) |
3085 | found = (((symbol->flags & BSF_FUNCTION) == 0 | |
3086 | || each->arange.high == 0 | |
3087 | || comp_unit_contains_address (each, addr)) | |
3088 | && comp_unit_find_line (each, symbol, addr, | |
3089 | filename_ptr, | |
3090 | linenumber_ptr, | |
3091 | stash)); | |
3092 | else | |
3093 | found = ((each->arange.high == 0 | |
3094 | || comp_unit_contains_address (each, addr)) | |
3095 | && comp_unit_find_nearest_line (each, addr, | |
3096 | filename_ptr, | |
3097 | functionname_ptr, | |
3098 | linenumber_ptr, | |
3099 | stash)); | |
5420f73d | 3100 | if (found) |
d4c32a81 | 3101 | goto done; |
5420f73d L |
3102 | } |
3103 | } | |
3104 | } | |
3105 | ||
d4c32a81 L |
3106 | done: |
3107 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) | |
3108 | unset_sections (stash); | |
3109 | ||
3110 | return found; | |
5420f73d L |
3111 | } |
3112 | ||
bec42b15 NC |
3113 | /* The DWARF2 version of find_nearest_line. |
3114 | Return TRUE if the line is found without error. */ | |
3115 | ||
3116 | bfd_boolean | |
3117 | _bfd_dwarf2_find_nearest_line (bfd *abfd, | |
3118 | asection *section, | |
3119 | asymbol **symbols, | |
3120 | bfd_vma offset, | |
3121 | const char **filename_ptr, | |
3122 | const char **functionname_ptr, | |
3123 | unsigned int *linenumber_ptr, | |
3124 | unsigned int addr_size, | |
3125 | void **pinfo) | |
3126 | { | |
3127 | return find_line (abfd, section, offset, NULL, symbols, filename_ptr, | |
3128 | functionname_ptr, linenumber_ptr, addr_size, | |
3129 | pinfo); | |
3130 | } | |
3131 | ||
3132 | /* The DWARF2 version of find_line. | |
3133 | Return TRUE if the line is found without error. */ | |
3134 | ||
3135 | bfd_boolean | |
3136 | _bfd_dwarf2_find_line (bfd *abfd, | |
3137 | asymbol **symbols, | |
3138 | asymbol *symbol, | |
3139 | const char **filename_ptr, | |
3140 | unsigned int *linenumber_ptr, | |
3141 | unsigned int addr_size, | |
3142 | void **pinfo) | |
3143 | { | |
3144 | return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr, | |
3145 | NULL, linenumber_ptr, addr_size, | |
3146 | pinfo); | |
3147 | } | |
3148 | ||
4ab527b0 FF |
3149 | bfd_boolean |
3150 | _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED, | |
3151 | const char **filename_ptr, | |
3152 | const char **functionname_ptr, | |
3153 | unsigned int *linenumber_ptr, | |
3154 | void **pinfo) | |
3155 | { | |
3156 | struct dwarf2_debug *stash; | |
3157 | ||
3158 | stash = *pinfo; | |
3159 | if (stash) | |
3160 | { | |
3161 | struct funcinfo *func = stash->inliner_chain; | |
bec42b15 | 3162 | |
4ab527b0 FF |
3163 | if (func && func->caller_func) |
3164 | { | |
3165 | *filename_ptr = func->caller_file; | |
3166 | *functionname_ptr = func->caller_func->name; | |
3167 | *linenumber_ptr = func->caller_line; | |
3168 | stash->inliner_chain = func->caller_func; | |
bec42b15 | 3169 | return TRUE; |
4ab527b0 FF |
3170 | } |
3171 | } | |
3172 | ||
bec42b15 | 3173 | return FALSE; |
4ab527b0 FF |
3174 | } |
3175 | ||
35330cce NC |
3176 | void |
3177 | _bfd_dwarf2_cleanup_debug_info (bfd *abfd) | |
3178 | { | |
3179 | struct comp_unit *each; | |
3180 | struct dwarf2_debug *stash; | |
3181 | ||
3182 | if (abfd == NULL || elf_tdata (abfd) == NULL) | |
3183 | return; | |
3184 | ||
3185 | stash = elf_tdata (abfd)->dwarf2_find_line_info; | |
3186 | ||
3187 | if (stash == NULL) | |
3188 | return; | |
3189 | ||
3190 | for (each = stash->all_comp_units; each; each = each->next_unit) | |
3191 | { | |
3192 | struct abbrev_info **abbrevs = each->abbrevs; | |
3193 | size_t i; | |
3194 | ||
3195 | for (i = 0; i < ABBREV_HASH_SIZE; i++) | |
d8d1c398 AM |
3196 | { |
3197 | struct abbrev_info *abbrev = abbrevs[i]; | |
35330cce | 3198 | |
d8d1c398 AM |
3199 | while (abbrev) |
3200 | { | |
3201 | free (abbrev->attrs); | |
3202 | abbrev = abbrev->next; | |
3203 | } | |
3204 | } | |
35330cce NC |
3205 | |
3206 | if (each->line_table) | |
d8d1c398 AM |
3207 | { |
3208 | free (each->line_table->dirs); | |
3209 | free (each->line_table->files); | |
3210 | } | |
35330cce NC |
3211 | } |
3212 | ||
3213 | free (stash->dwarf_abbrev_buffer); | |
3214 | free (stash->dwarf_line_buffer); | |
3215 | free (stash->dwarf_ranges_buffer); | |
3216 | } |