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