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