Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* DWARF 2 support. |
b90efa5b | 2 | Copyright (C) 1994-2015 Free Software Foundation, Inc. |
252b5132 RH |
3 | |
4 | Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions | |
5 | (gavin@cygnus.com). | |
6 | ||
7 | From the dwarf2read.c header: | |
8 | Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology, | |
9 | Inc. with support from Florida State University (under contract | |
10 | with the Ada Joint Program Office), and Silicon Graphics, Inc. | |
11 | Initial contribution by Brent Benson, Harris Computer Systems, Inc., | |
12 | based on Fred Fish's (Cygnus Support) implementation of DWARF 1 | |
13 | support in dwarfread.c | |
14 | ||
e2f6d277 | 15 | This file is part of BFD. |
252b5132 | 16 | |
e2f6d277 NC |
17 | This program is free software; you can redistribute it and/or modify |
18 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 19 | the Free Software Foundation; either version 3 of the License, or (at |
e2f6d277 | 20 | your option) any later version. |
252b5132 | 21 | |
e2f6d277 NC |
22 | This program is distributed in the hope that it will be useful, but |
23 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
25 | General Public License for more details. | |
252b5132 | 26 | |
e2f6d277 NC |
27 | You should have received a copy of the GNU General Public License |
28 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
29 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
30 | MA 02110-1301, USA. */ | |
252b5132 | 31 | |
252b5132 | 32 | #include "sysdep.h" |
3db64b00 | 33 | #include "bfd.h" |
252b5132 RH |
34 | #include "libiberty.h" |
35 | #include "libbfd.h" | |
36 | #include "elf-bfd.h" | |
fa8f86ff | 37 | #include "dwarf2.h" |
252b5132 RH |
38 | |
39 | /* The data in the .debug_line statement prologue looks like this. */ | |
a092b084 | 40 | |
252b5132 | 41 | struct line_head |
a092b084 | 42 | { |
d03ba2a1 | 43 | bfd_vma total_length; |
a092b084 | 44 | unsigned short version; |
f46c2da6 | 45 | bfd_vma prologue_length; |
a092b084 | 46 | unsigned char minimum_instruction_length; |
a233b20c | 47 | unsigned char maximum_ops_per_insn; |
a092b084 NC |
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 | ||
fc28f9aa TG |
92 | /* Names of the debug sections. */ |
93 | const struct dwarf_debug_section *debug_sections; | |
94 | ||
252b5132 RH |
95 | /* The next unread compilation unit within the .debug_info section. |
96 | Zero indicates that the .debug_info section has not been loaded | |
a092b084 | 97 | into a buffer yet. */ |
f075ee0c | 98 | bfd_byte *info_ptr; |
252b5132 | 99 | |
a092b084 | 100 | /* Pointer to the end of the .debug_info section memory buffer. */ |
f075ee0c | 101 | bfd_byte *info_ptr_end; |
252b5132 | 102 | |
0d161102 NC |
103 | /* Pointer to the bfd, section and address of the beginning of the |
104 | section. The bfd might be different than expected because of | |
105 | gnu_debuglink sections. */ | |
a50b1753 | 106 | bfd *bfd_ptr; |
f075ee0c AM |
107 | asection *sec; |
108 | bfd_byte *sec_info_ptr; | |
f2363ce5 | 109 | |
95e34fb4 NC |
110 | /* Support for alternate debug info sections created by the DWZ utility: |
111 | This includes a pointer to an alternate bfd which contains *extra*, | |
112 | possibly duplicate debug sections, and pointers to the loaded | |
113 | .debug_str and .debug_info sections from this bfd. */ | |
114 | bfd * alt_bfd_ptr; | |
115 | bfd_byte * alt_dwarf_str_buffer; | |
116 | bfd_size_type alt_dwarf_str_size; | |
117 | bfd_byte * alt_dwarf_info_buffer; | |
118 | bfd_size_type alt_dwarf_info_size; | |
119 | ||
aaf30c25 CS |
120 | /* A pointer to the memory block allocated for info_ptr. Neither |
121 | info_ptr nor sec_info_ptr are guaranteed to stay pointing to the | |
122 | beginning of the malloc block. This is used only to free the | |
123 | memory later. */ | |
124 | bfd_byte *info_ptr_memory; | |
125 | ||
f2363ce5 | 126 | /* Pointer to the symbol table. */ |
f075ee0c | 127 | asymbol **syms; |
f2363ce5 | 128 | |
a092b084 | 129 | /* Pointer to the .debug_abbrev section loaded into memory. */ |
f075ee0c | 130 | bfd_byte *dwarf_abbrev_buffer; |
252b5132 | 131 | |
a092b084 | 132 | /* Length of the loaded .debug_abbrev section. */ |
3076cd1f | 133 | bfd_size_type dwarf_abbrev_size; |
69dd2e2d RH |
134 | |
135 | /* Buffer for decode_line_info. */ | |
f075ee0c | 136 | bfd_byte *dwarf_line_buffer; |
ccdb16fc JW |
137 | |
138 | /* Length of the loaded .debug_line section. */ | |
3076cd1f | 139 | bfd_size_type dwarf_line_size; |
d03ba2a1 JJ |
140 | |
141 | /* Pointer to the .debug_str section loaded into memory. */ | |
f075ee0c | 142 | bfd_byte *dwarf_str_buffer; |
d03ba2a1 JJ |
143 | |
144 | /* Length of the loaded .debug_str section. */ | |
3076cd1f | 145 | bfd_size_type dwarf_str_size; |
a13afe8e FF |
146 | |
147 | /* Pointer to the .debug_ranges section loaded into memory. */ | |
148 | bfd_byte *dwarf_ranges_buffer; | |
149 | ||
150 | /* Length of the loaded .debug_ranges section. */ | |
3076cd1f | 151 | bfd_size_type dwarf_ranges_size; |
4ab527b0 FF |
152 | |
153 | /* If the most recent call to bfd_find_nearest_line was given an | |
154 | address in an inlined function, preserve a pointer into the | |
155 | calling chain for subsequent calls to bfd_find_inliner_info to | |
156 | use. */ | |
157 | struct funcinfo *inliner_chain; | |
d4c32a81 | 158 | |
cd0449ab AM |
159 | /* Section VMAs at the time the stash was built. */ |
160 | bfd_vma *sec_vma; | |
161 | ||
5609a71e | 162 | /* Number of sections whose VMA we must adjust. */ |
93ee1e36 | 163 | int adjusted_section_count; |
d4c32a81 | 164 | |
5609a71e DJ |
165 | /* Array of sections with adjusted VMA. */ |
166 | struct adjusted_section *adjusted_sections; | |
bd210d54 NC |
167 | |
168 | /* Number of times find_line is called. This is used in | |
169 | the heuristic for enabling the info hash tables. */ | |
170 | int info_hash_count; | |
171 | ||
172 | #define STASH_INFO_HASH_TRIGGER 100 | |
173 | ||
174 | /* Hash table mapping symbol names to function infos. */ | |
175 | struct info_hash_table *funcinfo_hash_table; | |
176 | ||
177 | /* Hash table mapping symbol names to variable infos. */ | |
178 | struct info_hash_table *varinfo_hash_table; | |
179 | ||
180 | /* Head of comp_unit list in the last hash table update. */ | |
181 | struct comp_unit *hash_units_head; | |
182 | ||
183 | /* Status of info hash. */ | |
184 | int info_hash_status; | |
185 | #define STASH_INFO_HASH_OFF 0 | |
186 | #define STASH_INFO_HASH_ON 1 | |
187 | #define STASH_INFO_HASH_DISABLED 2 | |
1c37913d AM |
188 | |
189 | /* True if we opened bfd_ptr. */ | |
190 | bfd_boolean close_on_cleanup; | |
252b5132 RH |
191 | }; |
192 | ||
a092b084 NC |
193 | struct arange |
194 | { | |
f623be2b RH |
195 | struct arange *next; |
196 | bfd_vma low; | |
197 | bfd_vma high; | |
198 | }; | |
252b5132 | 199 | |
252b5132 | 200 | /* A minimal decoding of DWARF2 compilation units. We only decode |
a092b084 | 201 | what's needed to get to the line number information. */ |
252b5132 | 202 | |
a092b084 NC |
203 | struct comp_unit |
204 | { | |
205 | /* Chain the previously read compilation units. */ | |
f075ee0c | 206 | struct comp_unit *next_unit; |
252b5132 | 207 | |
bd210d54 NC |
208 | /* Likewise, chain the compilation unit read after this one. |
209 | The comp units are stored in reversed reading order. */ | |
210 | struct comp_unit *prev_unit; | |
211 | ||
2ae727ad | 212 | /* Keep the bfd convenient (for memory allocation). */ |
f075ee0c | 213 | bfd *abfd; |
252b5132 | 214 | |
709d67f1 AM |
215 | /* The lowest and highest addresses contained in this compilation |
216 | unit as specified in the compilation unit header. */ | |
217 | struct arange arange; | |
252b5132 | 218 | |
a092b084 | 219 | /* The DW_AT_name attribute (for error messages). */ |
f075ee0c | 220 | char *name; |
252b5132 | 221 | |
a092b084 | 222 | /* The abbrev hash table. */ |
f075ee0c | 223 | struct abbrev_info **abbrevs; |
252b5132 | 224 | |
e00e8198 AM |
225 | /* DW_AT_language. */ |
226 | int lang; | |
227 | ||
a092b084 | 228 | /* Note that an error was found by comp_unit_find_nearest_line. */ |
252b5132 RH |
229 | int error; |
230 | ||
a092b084 | 231 | /* The DW_AT_comp_dir attribute. */ |
f075ee0c | 232 | char *comp_dir; |
252b5132 | 233 | |
b34976b6 | 234 | /* TRUE if there is a line number table associated with this comp. unit. */ |
252b5132 | 235 | int stmtlist; |
98591c73 | 236 | |
c0c28ab8 L |
237 | /* Pointer to the current comp_unit so that we can find a given entry |
238 | by its reference. */ | |
f075ee0c | 239 | bfd_byte *info_ptr_unit; |
c0c28ab8 | 240 | |
a358ecb8 AM |
241 | /* Pointer to the start of the debug section, for DW_FORM_ref_addr. */ |
242 | bfd_byte *sec_info_ptr; | |
243 | ||
a092b084 | 244 | /* The offset into .debug_line of the line number table. */ |
252b5132 RH |
245 | unsigned long line_offset; |
246 | ||
a092b084 | 247 | /* Pointer to the first child die for the comp unit. */ |
f075ee0c | 248 | bfd_byte *first_child_die_ptr; |
252b5132 | 249 | |
a092b084 | 250 | /* The end of the comp unit. */ |
f075ee0c | 251 | bfd_byte *end_ptr; |
252b5132 | 252 | |
a092b084 | 253 | /* The decoded line number, NULL if not yet decoded. */ |
f075ee0c | 254 | struct line_info_table *line_table; |
252b5132 | 255 | |
a092b084 | 256 | /* A list of the functions found in this comp. unit. */ |
f075ee0c | 257 | struct funcinfo *function_table; |
252b5132 | 258 | |
5420f73d L |
259 | /* A list of the variables found in this comp. unit. */ |
260 | struct varinfo *variable_table; | |
261 | ||
d03ba2a1 JJ |
262 | /* Pointer to dwarf2_debug structure. */ |
263 | struct dwarf2_debug *stash; | |
264 | ||
5609a71e DJ |
265 | /* DWARF format version for this unit - from unit header. */ |
266 | int version; | |
267 | ||
a092b084 | 268 | /* Address size for this unit - from unit header. */ |
252b5132 | 269 | unsigned char addr_size; |
d03ba2a1 JJ |
270 | |
271 | /* Offset size for this unit - from unit header. */ | |
272 | unsigned char offset_size; | |
a13afe8e FF |
273 | |
274 | /* Base address for this unit - from DW_AT_low_pc attribute of | |
275 | DW_TAG_compile_unit DIE */ | |
276 | bfd_vma base_address; | |
bd210d54 NC |
277 | |
278 | /* TRUE if symbols are cached in hash table for faster lookup by name. */ | |
279 | bfd_boolean cached; | |
252b5132 RH |
280 | }; |
281 | ||
a7b97311 AM |
282 | /* This data structure holds the information of an abbrev. */ |
283 | struct abbrev_info | |
284 | { | |
285 | unsigned int number; /* Number identifying abbrev. */ | |
286 | enum dwarf_tag tag; /* DWARF tag. */ | |
287 | int has_children; /* Boolean. */ | |
288 | unsigned int num_attrs; /* Number of attributes. */ | |
289 | struct attr_abbrev *attrs; /* An array of attribute descriptions. */ | |
290 | struct abbrev_info *next; /* Next in chain. */ | |
291 | }; | |
292 | ||
293 | struct attr_abbrev | |
294 | { | |
295 | enum dwarf_attribute name; | |
296 | enum dwarf_form form; | |
297 | }; | |
298 | ||
4a114e3e L |
299 | /* Map of uncompressed DWARF debug section name to compressed one. It |
300 | is terminated by NULL uncompressed_name. */ | |
301 | ||
e4c93b56 | 302 | const struct dwarf_debug_section dwarf_debug_sections[] = |
4a114e3e L |
303 | { |
304 | { ".debug_abbrev", ".zdebug_abbrev" }, | |
305 | { ".debug_aranges", ".zdebug_aranges" }, | |
306 | { ".debug_frame", ".zdebug_frame" }, | |
307 | { ".debug_info", ".zdebug_info" }, | |
95e34fb4 | 308 | { ".debug_info", ".zdebug_info" }, |
4a114e3e L |
309 | { ".debug_line", ".zdebug_line" }, |
310 | { ".debug_loc", ".zdebug_loc" }, | |
311 | { ".debug_macinfo", ".zdebug_macinfo" }, | |
4ccf1e31 | 312 | { ".debug_macro", ".zdebug_macro" }, |
4a114e3e L |
313 | { ".debug_pubnames", ".zdebug_pubnames" }, |
314 | { ".debug_pubtypes", ".zdebug_pubtypes" }, | |
315 | { ".debug_ranges", ".zdebug_ranges" }, | |
316 | { ".debug_static_func", ".zdebug_static_func" }, | |
317 | { ".debug_static_vars", ".zdebug_static_vars" }, | |
318 | { ".debug_str", ".zdebug_str", }, | |
95e34fb4 | 319 | { ".debug_str", ".zdebug_str", }, |
4a114e3e L |
320 | { ".debug_types", ".zdebug_types" }, |
321 | /* GNU DWARF 1 extensions */ | |
322 | { ".debug_sfnames", ".zdebug_sfnames" }, | |
323 | { ".debug_srcinfo", ".zebug_srcinfo" }, | |
324 | /* SGI/MIPS DWARF 2 extensions */ | |
325 | { ".debug_funcnames", ".zdebug_funcnames" }, | |
326 | { ".debug_typenames", ".zdebug_typenames" }, | |
327 | { ".debug_varnames", ".zdebug_varnames" }, | |
328 | { ".debug_weaknames", ".zdebug_weaknames" }, | |
329 | { NULL, NULL }, | |
330 | }; | |
331 | ||
95e34fb4 NC |
332 | /* NB/ Numbers in this enum must match up with indicies |
333 | into the dwarf_debug_sections[] array above. */ | |
4a114e3e L |
334 | enum dwarf_debug_section_enum |
335 | { | |
336 | debug_abbrev = 0, | |
337 | debug_aranges, | |
338 | debug_frame, | |
339 | debug_info, | |
95e34fb4 | 340 | debug_info_alt, |
4a114e3e L |
341 | debug_line, |
342 | debug_loc, | |
343 | debug_macinfo, | |
4ccf1e31 | 344 | debug_macro, |
4a114e3e L |
345 | debug_pubnames, |
346 | debug_pubtypes, | |
347 | debug_ranges, | |
348 | debug_static_func, | |
349 | debug_static_vars, | |
350 | debug_str, | |
95e34fb4 | 351 | debug_str_alt, |
4a114e3e L |
352 | debug_types, |
353 | debug_sfnames, | |
354 | debug_srcinfo, | |
355 | debug_funcnames, | |
356 | debug_typenames, | |
357 | debug_varnames, | |
358 | debug_weaknames | |
359 | }; | |
360 | ||
a7b97311 AM |
361 | #ifndef ABBREV_HASH_SIZE |
362 | #define ABBREV_HASH_SIZE 121 | |
363 | #endif | |
364 | #ifndef ATTR_ALLOC_CHUNK | |
365 | #define ATTR_ALLOC_CHUNK 4 | |
366 | #endif | |
367 | ||
bd210d54 NC |
368 | /* Variable and function hash tables. This is used to speed up look-up |
369 | in lookup_symbol_in_var_table() and lookup_symbol_in_function_table(). | |
370 | In order to share code between variable and function infos, we use | |
371 | a list of untyped pointer for all variable/function info associated with | |
372 | a symbol. We waste a bit of memory for list with one node but that | |
373 | simplifies the code. */ | |
374 | ||
375 | struct info_list_node | |
376 | { | |
377 | struct info_list_node *next; | |
378 | void *info; | |
379 | }; | |
380 | ||
381 | /* Info hash entry. */ | |
382 | struct info_hash_entry | |
383 | { | |
384 | struct bfd_hash_entry root; | |
385 | struct info_list_node *head; | |
386 | }; | |
387 | ||
388 | struct info_hash_table | |
389 | { | |
390 | struct bfd_hash_table base; | |
391 | }; | |
392 | ||
393 | /* Function to create a new entry in info hash table. */ | |
394 | ||
395 | static struct bfd_hash_entry * | |
396 | info_hash_table_newfunc (struct bfd_hash_entry *entry, | |
397 | struct bfd_hash_table *table, | |
398 | const char *string) | |
399 | { | |
400 | struct info_hash_entry *ret = (struct info_hash_entry *) entry; | |
401 | ||
402 | /* Allocate the structure if it has not already been allocated by a | |
403 | derived class. */ | |
404 | if (ret == NULL) | |
405 | { | |
a50b1753 | 406 | ret = (struct info_hash_entry *) bfd_hash_allocate (table, |
93ee1e36 | 407 | sizeof (* ret)); |
bd210d54 NC |
408 | if (ret == NULL) |
409 | return NULL; | |
410 | } | |
411 | ||
412 | /* Call the allocation method of the base class. */ | |
413 | ret = ((struct info_hash_entry *) | |
2d47a72c | 414 | bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string)); |
bd210d54 NC |
415 | |
416 | /* Initialize the local fields here. */ | |
417 | if (ret) | |
418 | ret->head = NULL; | |
419 | ||
420 | return (struct bfd_hash_entry *) ret; | |
421 | } | |
422 | ||
423 | /* Function to create a new info hash table. It returns a pointer to the | |
424 | newly created table or NULL if there is any error. We need abfd | |
425 | solely for memory allocation. */ | |
426 | ||
427 | static struct info_hash_table * | |
428 | create_info_hash_table (bfd *abfd) | |
429 | { | |
430 | struct info_hash_table *hash_table; | |
431 | ||
a2a50954 AM |
432 | hash_table = ((struct info_hash_table *) |
433 | bfd_alloc (abfd, sizeof (struct info_hash_table))); | |
bd210d54 NC |
434 | if (!hash_table) |
435 | return hash_table; | |
436 | ||
437 | if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc, | |
438 | sizeof (struct info_hash_entry))) | |
439 | { | |
440 | bfd_release (abfd, hash_table); | |
441 | return NULL; | |
442 | } | |
443 | ||
444 | return hash_table; | |
445 | } | |
446 | ||
447 | /* Insert an info entry into an info hash table. We do not check of | |
448 | duplicate entries. Also, the caller need to guarantee that the | |
449 | right type of info in inserted as info is passed as a void* pointer. | |
450 | This function returns true if there is no error. */ | |
451 | ||
452 | static bfd_boolean | |
453 | insert_info_hash_table (struct info_hash_table *hash_table, | |
454 | const char *key, | |
455 | void *info, | |
456 | bfd_boolean copy_p) | |
457 | { | |
458 | struct info_hash_entry *entry; | |
459 | struct info_list_node *node; | |
460 | ||
461 | entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, | |
462 | key, TRUE, copy_p); | |
463 | if (!entry) | |
464 | return FALSE; | |
465 | ||
a50b1753 | 466 | node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base, |
93ee1e36 | 467 | sizeof (*node)); |
bd210d54 NC |
468 | if (!node) |
469 | return FALSE; | |
470 | ||
471 | node->info = info; | |
472 | node->next = entry->head; | |
473 | entry->head = node; | |
474 | ||
475 | return TRUE; | |
476 | } | |
477 | ||
478 | /* Look up an info entry list from an info hash table. Return NULL | |
479 | if there is none. */ | |
480 | ||
481 | static struct info_list_node * | |
482 | lookup_info_hash_table (struct info_hash_table *hash_table, const char *key) | |
483 | { | |
484 | struct info_hash_entry *entry; | |
485 | ||
486 | entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key, | |
487 | FALSE, FALSE); | |
488 | return entry ? entry->head : NULL; | |
489 | } | |
490 | ||
1b315056 | 491 | /* Read a section into its appropriate place in the dwarf2_debug |
dc80fd5c | 492 | struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is |
1b315056 | 493 | not NULL, use bfd_simple_get_relocated_section_contents to read the |
dc80fd5c NC |
494 | section contents, otherwise use bfd_get_section_contents. Fail if |
495 | the located section does not contain at least OFFSET bytes. */ | |
1b315056 CS |
496 | |
497 | static bfd_boolean | |
dc80fd5c | 498 | read_section (bfd * abfd, |
fc28f9aa | 499 | const struct dwarf_debug_section *sec, |
dc80fd5c NC |
500 | asymbol ** syms, |
501 | bfd_uint64_t offset, | |
502 | bfd_byte ** section_buffer, | |
503 | bfd_size_type * section_size) | |
1b315056 CS |
504 | { |
505 | asection *msec; | |
fc28f9aa | 506 | const char *section_name = sec->uncompressed_name; |
1b315056 | 507 | |
95e34fb4 NC |
508 | /* The section may have already been read. */ |
509 | if (*section_buffer == NULL) | |
1b315056 | 510 | { |
53638231 | 511 | msec = bfd_get_section_by_name (abfd, section_name); |
4a114e3e | 512 | if (! msec) |
53638231 | 513 | { |
fc28f9aa | 514 | section_name = sec->compressed_name; |
93ee1e36 AM |
515 | if (section_name != NULL) |
516 | msec = bfd_get_section_by_name (abfd, section_name); | |
53638231 AS |
517 | } |
518 | if (! msec) | |
2d47a72c | 519 | { |
fc28f9aa | 520 | (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."), |
93ee1e36 | 521 | sec->uncompressed_name); |
2d47a72c DJ |
522 | bfd_set_error (bfd_error_bad_value); |
523 | return FALSE; | |
524 | } | |
53638231 | 525 | |
bc664799 | 526 | *section_size = msec->rawsize ? msec->rawsize : msec->size; |
8c2ccebd CC |
527 | if (syms) |
528 | { | |
529 | *section_buffer | |
a2a50954 | 530 | = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms); |
8c2ccebd CC |
531 | if (! *section_buffer) |
532 | return FALSE; | |
533 | } | |
534 | else | |
535 | { | |
536 | *section_buffer = (bfd_byte *) bfd_malloc (*section_size); | |
537 | if (! *section_buffer) | |
538 | return FALSE; | |
539 | if (! bfd_get_section_contents (abfd, msec, *section_buffer, | |
540 | 0, *section_size)) | |
541 | return FALSE; | |
542 | } | |
1b315056 CS |
543 | } |
544 | ||
545 | /* It is possible to get a bad value for the offset into the section | |
dc80fd5c | 546 | that the client wants. Validate it here to avoid trouble later. */ |
1b315056 CS |
547 | if (offset != 0 && offset >= *section_size) |
548 | { | |
a2a50954 AM |
549 | (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu)" |
550 | " greater than or equal to %s size (%lu)."), | |
dc80fd5c | 551 | (long) offset, section_name, *section_size); |
1b315056 CS |
552 | bfd_set_error (bfd_error_bad_value); |
553 | return FALSE; | |
554 | } | |
555 | ||
556 | return TRUE; | |
557 | } | |
558 | ||
a092b084 | 559 | /* Read dwarf information from a buffer. */ |
252b5132 RH |
560 | |
561 | static unsigned int | |
dbb3fbbb | 562 | read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end) |
252b5132 | 563 | { |
dbb3fbbb NC |
564 | if (buf + 1 > end) |
565 | return 0; | |
818a27ac | 566 | return bfd_get_8 (abfd, buf); |
252b5132 RH |
567 | } |
568 | ||
569 | static int | |
dbb3fbbb | 570 | read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end) |
252b5132 | 571 | { |
dbb3fbbb NC |
572 | if (buf + 1 > end) |
573 | return 0; | |
818a27ac | 574 | return bfd_get_signed_8 (abfd, buf); |
252b5132 RH |
575 | } |
576 | ||
577 | static unsigned int | |
dbb3fbbb | 578 | read_2_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end) |
252b5132 | 579 | { |
dbb3fbbb NC |
580 | if (buf + 2 > end) |
581 | return 0; | |
818a27ac | 582 | return bfd_get_16 (abfd, buf); |
252b5132 RH |
583 | } |
584 | ||
252b5132 | 585 | static unsigned int |
dbb3fbbb | 586 | read_4_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end) |
252b5132 | 587 | { |
dbb3fbbb NC |
588 | if (buf + 4 > end) |
589 | return 0; | |
818a27ac | 590 | return bfd_get_32 (abfd, buf); |
252b5132 RH |
591 | } |
592 | ||
8ce8c090 | 593 | static bfd_uint64_t |
dbb3fbbb | 594 | read_8_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end) |
252b5132 | 595 | { |
dbb3fbbb NC |
596 | if (buf + 8 > end) |
597 | return 0; | |
818a27ac | 598 | return bfd_get_64 (abfd, buf); |
252b5132 RH |
599 | } |
600 | ||
f075ee0c | 601 | static bfd_byte * |
818a27ac | 602 | read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED, |
f075ee0c | 603 | bfd_byte *buf, |
dbb3fbbb | 604 | bfd_byte *end, |
818a27ac | 605 | unsigned int size ATTRIBUTE_UNUSED) |
252b5132 | 606 | { |
dbb3fbbb NC |
607 | if (buf + size > end) |
608 | return NULL; | |
252b5132 RH |
609 | return buf; |
610 | } | |
611 | ||
dbb3fbbb NC |
612 | /* Scans a NUL terminated string starting at BUF, returning a pointer to it. |
613 | Returns the number of characters in the string, *including* the NUL byte, | |
614 | in BYTES_READ_PTR. This value is set even if the function fails. Bytes | |
615 | at or beyond BUF_END will not be read. Returns NULL if there was a | |
616 | problem, or if the string is empty. */ | |
617 | ||
252b5132 | 618 | static char * |
dbb3fbbb NC |
619 | read_string (bfd * abfd ATTRIBUTE_UNUSED, |
620 | bfd_byte * buf, | |
621 | bfd_byte * buf_end, | |
622 | unsigned int * bytes_read_ptr) | |
252b5132 | 623 | { |
dbb3fbbb NC |
624 | bfd_byte *str = buf; |
625 | ||
626 | if (buf >= buf_end) | |
627 | { | |
628 | * bytes_read_ptr = 0; | |
629 | return NULL; | |
630 | } | |
dc80fd5c | 631 | |
f075ee0c | 632 | if (*str == '\0') |
252b5132 | 633 | { |
dbb3fbbb | 634 | * bytes_read_ptr = 1; |
252b5132 RH |
635 | return NULL; |
636 | } | |
98591c73 | 637 | |
dbb3fbbb NC |
638 | while (buf < buf_end) |
639 | if (* buf ++ == 0) | |
640 | { | |
641 | * bytes_read_ptr = buf - str; | |
642 | return (char *) str; | |
643 | } | |
644 | ||
645 | * bytes_read_ptr = buf - str; | |
646 | return NULL; | |
252b5132 RH |
647 | } |
648 | ||
dbb3fbbb NC |
649 | /* Reads an offset from BUF and then locates the string at this offset |
650 | inside the debug string section. Returns a pointer to the string. | |
651 | Returns the number of bytes read from BUF, *not* the length of the string, | |
652 | in BYTES_READ_PTR. This value is set even if the function fails. Bytes | |
653 | at or beyond BUF_END will not be read from BUF. Returns NULL if there was | |
654 | a problem, or if the string is empty. Does not check for NUL termination | |
655 | of the string. */ | |
dc80fd5c | 656 | |
d03ba2a1 | 657 | static char * |
dc80fd5c NC |
658 | read_indirect_string (struct comp_unit * unit, |
659 | bfd_byte * buf, | |
dbb3fbbb | 660 | bfd_byte * buf_end, |
dc80fd5c | 661 | unsigned int * bytes_read_ptr) |
d03ba2a1 | 662 | { |
8ce8c090 | 663 | bfd_uint64_t offset; |
d03ba2a1 | 664 | struct dwarf2_debug *stash = unit->stash; |
f075ee0c | 665 | char *str; |
d03ba2a1 | 666 | |
dbb3fbbb NC |
667 | if (buf + unit->offset_size > buf_end) |
668 | { | |
669 | * bytes_read_ptr = 0; | |
670 | return NULL; | |
671 | } | |
62f8d217 | 672 | |
d03ba2a1 | 673 | if (unit->offset_size == 4) |
dbb3fbbb | 674 | offset = read_4_bytes (unit->abfd, buf, buf_end); |
d03ba2a1 | 675 | else |
dbb3fbbb | 676 | offset = read_8_bytes (unit->abfd, buf, buf_end); |
dc80fd5c | 677 | |
d03ba2a1 JJ |
678 | *bytes_read_ptr = unit->offset_size; |
679 | ||
fc28f9aa | 680 | if (! read_section (unit->abfd, &stash->debug_sections[debug_str], |
93ee1e36 | 681 | stash->syms, offset, |
9e32b19f | 682 | &stash->dwarf_str_buffer, &stash->dwarf_str_size)) |
dc80fd5c | 683 | return NULL; |
d03ba2a1 | 684 | |
dbb3fbbb NC |
685 | if (offset >= stash->dwarf_str_size) |
686 | return NULL; | |
f075ee0c AM |
687 | str = (char *) stash->dwarf_str_buffer + offset; |
688 | if (*str == '\0') | |
d03ba2a1 | 689 | return NULL; |
f075ee0c | 690 | return str; |
d03ba2a1 JJ |
691 | } |
692 | ||
95e34fb4 | 693 | /* Like read_indirect_string but uses a .debug_str located in |
93ee1e36 | 694 | an alternate file pointed to by the .gnu_debugaltlink section. |
95e34fb4 NC |
695 | Used to impement DW_FORM_GNU_strp_alt. */ |
696 | ||
697 | static char * | |
698 | read_alt_indirect_string (struct comp_unit * unit, | |
699 | bfd_byte * buf, | |
dbb3fbbb | 700 | bfd_byte * buf_end, |
95e34fb4 NC |
701 | unsigned int * bytes_read_ptr) |
702 | { | |
703 | bfd_uint64_t offset; | |
704 | struct dwarf2_debug *stash = unit->stash; | |
705 | char *str; | |
706 | ||
dbb3fbbb NC |
707 | if (buf + unit->offset_size > buf_end) |
708 | { | |
709 | * bytes_read_ptr = 0; | |
710 | return NULL; | |
711 | } | |
62f8d217 | 712 | |
95e34fb4 | 713 | if (unit->offset_size == 4) |
dbb3fbbb | 714 | offset = read_4_bytes (unit->abfd, buf, buf_end); |
95e34fb4 | 715 | else |
dbb3fbbb | 716 | offset = read_8_bytes (unit->abfd, buf, buf_end); |
95e34fb4 NC |
717 | |
718 | *bytes_read_ptr = unit->offset_size; | |
719 | ||
720 | if (stash->alt_bfd_ptr == NULL) | |
721 | { | |
722 | bfd * debug_bfd; | |
723 | char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR); | |
724 | ||
725 | if (debug_filename == NULL) | |
726 | return NULL; | |
727 | ||
728 | if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL | |
729 | || ! bfd_check_format (debug_bfd, bfd_object)) | |
730 | { | |
731 | if (debug_bfd) | |
732 | bfd_close (debug_bfd); | |
733 | ||
734 | /* FIXME: Should we report our failure to follow the debuglink ? */ | |
735 | free (debug_filename); | |
736 | return NULL; | |
737 | } | |
738 | stash->alt_bfd_ptr = debug_bfd; | |
739 | } | |
62f8d217 | 740 | |
95e34fb4 NC |
741 | if (! read_section (unit->stash->alt_bfd_ptr, |
742 | stash->debug_sections + debug_str_alt, | |
743 | NULL, /* FIXME: Do we need to load alternate symbols ? */ | |
744 | offset, | |
745 | &stash->alt_dwarf_str_buffer, | |
746 | &stash->alt_dwarf_str_size)) | |
747 | return NULL; | |
748 | ||
dbb3fbbb NC |
749 | if (offset >= stash->alt_dwarf_str_size) |
750 | return NULL; | |
95e34fb4 NC |
751 | str = (char *) stash->alt_dwarf_str_buffer + offset; |
752 | if (*str == '\0') | |
753 | return NULL; | |
754 | ||
755 | return str; | |
756 | } | |
757 | ||
758 | /* Resolve an alternate reference from UNIT at OFFSET. | |
759 | Returns a pointer into the loaded alternate CU upon success | |
760 | or NULL upon failure. */ | |
761 | ||
762 | static bfd_byte * | |
763 | read_alt_indirect_ref (struct comp_unit * unit, | |
764 | bfd_uint64_t offset) | |
765 | { | |
766 | struct dwarf2_debug *stash = unit->stash; | |
767 | ||
768 | if (stash->alt_bfd_ptr == NULL) | |
769 | { | |
770 | bfd * debug_bfd; | |
771 | char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR); | |
772 | ||
773 | if (debug_filename == NULL) | |
774 | return FALSE; | |
775 | ||
776 | if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL | |
777 | || ! bfd_check_format (debug_bfd, bfd_object)) | |
778 | { | |
779 | if (debug_bfd) | |
780 | bfd_close (debug_bfd); | |
781 | ||
782 | /* FIXME: Should we report our failure to follow the debuglink ? */ | |
783 | free (debug_filename); | |
784 | return NULL; | |
785 | } | |
786 | stash->alt_bfd_ptr = debug_bfd; | |
787 | } | |
62f8d217 | 788 | |
95e34fb4 NC |
789 | if (! read_section (unit->stash->alt_bfd_ptr, |
790 | stash->debug_sections + debug_info_alt, | |
791 | NULL, /* FIXME: Do we need to load alternate symbols ? */ | |
792 | offset, | |
793 | &stash->alt_dwarf_info_buffer, | |
794 | &stash->alt_dwarf_info_size)) | |
795 | return NULL; | |
796 | ||
dbb3fbbb NC |
797 | if (offset >= stash->alt_dwarf_info_size) |
798 | return NULL; | |
95e34fb4 NC |
799 | return stash->alt_dwarf_info_buffer + offset; |
800 | } | |
801 | ||
8ce8c090 | 802 | static bfd_uint64_t |
dbb3fbbb | 803 | read_address (struct comp_unit *unit, bfd_byte *buf, bfd_byte * buf_end) |
252b5132 | 804 | { |
fa15f18d AM |
805 | int signed_vma = 0; |
806 | ||
807 | if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour) | |
808 | signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma; | |
0af4cd7c | 809 | |
dbb3fbbb NC |
810 | if (buf + unit->addr_size > buf_end) |
811 | return 0; | |
812 | ||
0af4cd7c PK |
813 | if (signed_vma) |
814 | { | |
815 | switch (unit->addr_size) | |
816 | { | |
817 | case 8: | |
818 | return bfd_get_signed_64 (unit->abfd, buf); | |
819 | case 4: | |
820 | return bfd_get_signed_32 (unit->abfd, buf); | |
821 | case 2: | |
822 | return bfd_get_signed_16 (unit->abfd, buf); | |
823 | default: | |
824 | abort (); | |
825 | } | |
826 | } | |
827 | else | |
252b5132 | 828 | { |
0af4cd7c PK |
829 | switch (unit->addr_size) |
830 | { | |
831 | case 8: | |
832 | return bfd_get_64 (unit->abfd, buf); | |
833 | case 4: | |
834 | return bfd_get_32 (unit->abfd, buf); | |
835 | case 2: | |
836 | return bfd_get_16 (unit->abfd, buf); | |
837 | default: | |
838 | abort (); | |
839 | } | |
252b5132 | 840 | } |
252b5132 RH |
841 | } |
842 | ||
252b5132 RH |
843 | /* Lookup an abbrev_info structure in the abbrev hash table. */ |
844 | ||
845 | static struct abbrev_info * | |
818a27ac | 846 | lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs) |
252b5132 RH |
847 | { |
848 | unsigned int hash_number; | |
849 | struct abbrev_info *abbrev; | |
850 | ||
851 | hash_number = number % ABBREV_HASH_SIZE; | |
852 | abbrev = abbrevs[hash_number]; | |
853 | ||
854 | while (abbrev) | |
855 | { | |
856 | if (abbrev->number == number) | |
857 | return abbrev; | |
858 | else | |
859 | abbrev = abbrev->next; | |
860 | } | |
98591c73 | 861 | |
252b5132 RH |
862 | return NULL; |
863 | } | |
864 | ||
865 | /* In DWARF version 2, the description of the debugging information is | |
866 | stored in a separate .debug_abbrev section. Before we read any | |
867 | dies from a section we read in all abbreviations and install them | |
868 | in a hash table. */ | |
869 | ||
870 | static struct abbrev_info** | |
8ce8c090 | 871 | read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash) |
252b5132 RH |
872 | { |
873 | struct abbrev_info **abbrevs; | |
f075ee0c | 874 | bfd_byte *abbrev_ptr; |
dbb3fbbb | 875 | bfd_byte *abbrev_end; |
252b5132 RH |
876 | struct abbrev_info *cur_abbrev; |
877 | unsigned int abbrev_number, bytes_read, abbrev_name; | |
878 | unsigned int abbrev_form, hash_number; | |
dc810e39 | 879 | bfd_size_type amt; |
252b5132 | 880 | |
fc28f9aa | 881 | if (! read_section (abfd, &stash->debug_sections[debug_abbrev], |
93ee1e36 | 882 | stash->syms, offset, |
9e32b19f | 883 | &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size)) |
8af6b354 | 884 | return NULL; |
252b5132 | 885 | |
dbb3fbbb NC |
886 | if (offset >= stash->dwarf_abbrev_size) |
887 | return NULL; | |
888 | ||
dc810e39 | 889 | amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE; |
a50b1753 | 890 | abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt); |
8af6b354 AM |
891 | if (abbrevs == NULL) |
892 | return NULL; | |
252b5132 RH |
893 | |
894 | abbrev_ptr = stash->dwarf_abbrev_buffer + offset; | |
dbb3fbbb NC |
895 | abbrev_end = stash->dwarf_abbrev_buffer + stash->dwarf_abbrev_size; |
896 | abbrev_number = safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end); | |
252b5132 RH |
897 | abbrev_ptr += bytes_read; |
898 | ||
a092b084 | 899 | /* Loop until we reach an abbrev number of 0. */ |
252b5132 RH |
900 | while (abbrev_number) |
901 | { | |
dc810e39 | 902 | amt = sizeof (struct abbrev_info); |
a50b1753 | 903 | cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt); |
8af6b354 AM |
904 | if (cur_abbrev == NULL) |
905 | return NULL; | |
252b5132 | 906 | |
a092b084 | 907 | /* Read in abbrev header. */ |
252b5132 | 908 | cur_abbrev->number = abbrev_number; |
d45913a0 | 909 | cur_abbrev->tag = (enum dwarf_tag) |
dbb3fbbb | 910 | safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end); |
252b5132 | 911 | abbrev_ptr += bytes_read; |
dbb3fbbb | 912 | cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr, abbrev_end); |
252b5132 RH |
913 | abbrev_ptr += 1; |
914 | ||
a092b084 | 915 | /* Now read in declarations. */ |
dbb3fbbb | 916 | abbrev_name = safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end); |
252b5132 | 917 | abbrev_ptr += bytes_read; |
dbb3fbbb | 918 | abbrev_form = safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end); |
252b5132 | 919 | abbrev_ptr += bytes_read; |
98591c73 | 920 | |
252b5132 RH |
921 | while (abbrev_name) |
922 | { | |
923 | if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0) | |
924 | { | |
35330cce NC |
925 | struct attr_abbrev *tmp; |
926 | ||
dc810e39 AM |
927 | amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK; |
928 | amt *= sizeof (struct attr_abbrev); | |
a50b1753 | 929 | tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt); |
35330cce | 930 | if (tmp == NULL) |
d8d1c398 AM |
931 | { |
932 | size_t i; | |
933 | ||
934 | for (i = 0; i < ABBREV_HASH_SIZE; i++) | |
935 | { | |
936 | struct abbrev_info *abbrev = abbrevs[i]; | |
937 | ||
938 | while (abbrev) | |
939 | { | |
34b5e0b2 NC |
940 | free (abbrev->attrs); |
941 | abbrev = abbrev->next; | |
d8d1c398 AM |
942 | } |
943 | } | |
944 | return NULL; | |
945 | } | |
35330cce | 946 | cur_abbrev->attrs = tmp; |
252b5132 | 947 | } |
98591c73 | 948 | |
d45913a0 DA |
949 | cur_abbrev->attrs[cur_abbrev->num_attrs].name |
950 | = (enum dwarf_attribute) abbrev_name; | |
951 | cur_abbrev->attrs[cur_abbrev->num_attrs++].form | |
952 | = (enum dwarf_form) abbrev_form; | |
dbb3fbbb | 953 | abbrev_name = safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end); |
252b5132 | 954 | abbrev_ptr += bytes_read; |
dbb3fbbb | 955 | abbrev_form = safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end); |
252b5132 RH |
956 | abbrev_ptr += bytes_read; |
957 | } | |
958 | ||
959 | hash_number = abbrev_number % ABBREV_HASH_SIZE; | |
960 | cur_abbrev->next = abbrevs[hash_number]; | |
961 | abbrevs[hash_number] = cur_abbrev; | |
962 | ||
963 | /* Get next abbreviation. | |
e82ce529 | 964 | Under Irix6 the abbreviations for a compilation unit are not |
252b5132 RH |
965 | always properly terminated with an abbrev number of 0. |
966 | Exit loop if we encounter an abbreviation which we have | |
967 | already read (which means we are about to read the abbreviations | |
968 | for the next compile unit) or if the end of the abbreviation | |
969 | table is reached. */ | |
970 | if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer) | |
d8d1c398 | 971 | >= stash->dwarf_abbrev_size) |
252b5132 | 972 | break; |
dbb3fbbb | 973 | abbrev_number = safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end); |
252b5132 | 974 | abbrev_ptr += bytes_read; |
dbb3fbbb | 975 | if (lookup_abbrev (abbrev_number, abbrevs) != NULL) |
252b5132 RH |
976 | break; |
977 | } | |
978 | ||
979 | return abbrevs; | |
980 | } | |
981 | ||
60d77146 NC |
982 | /* Returns true if the form is one which has a string value. */ |
983 | ||
984 | static inline bfd_boolean | |
985 | is_str_attr (enum dwarf_form form) | |
986 | { | |
987 | return form == DW_FORM_string || form == DW_FORM_strp || form == DW_FORM_GNU_strp_alt; | |
988 | } | |
989 | ||
dbb3fbbb NC |
990 | /* Read and fill in the value of attribute ATTR as described by FORM. |
991 | Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END. | |
992 | Returns an updated INFO_PTR taking into account the amount of data read. */ | |
252b5132 | 993 | |
f075ee0c | 994 | static bfd_byte * |
dbb3fbbb NC |
995 | read_attribute_value (struct attribute * attr, |
996 | unsigned form, | |
997 | struct comp_unit * unit, | |
998 | bfd_byte * info_ptr, | |
999 | bfd_byte * info_ptr_end) | |
252b5132 RH |
1000 | { |
1001 | bfd *abfd = unit->abfd; | |
1002 | unsigned int bytes_read; | |
1003 | struct dwarf_block *blk; | |
dc810e39 | 1004 | bfd_size_type amt; |
252b5132 | 1005 | |
dbb3fbbb NC |
1006 | if (info_ptr >= info_ptr_end) |
1007 | { | |
1008 | (*_bfd_error_handler) (_("Dwarf Error: Info pointer extends beyond end of attributes")); | |
1009 | bfd_set_error (bfd_error_bad_value); | |
1010 | return info_ptr; | |
1011 | } | |
1012 | ||
d45913a0 | 1013 | attr->form = (enum dwarf_form) form; |
98591c73 | 1014 | |
cf716c56 | 1015 | switch (form) |
252b5132 | 1016 | { |
252b5132 | 1017 | case DW_FORM_ref_addr: |
5609a71e DJ |
1018 | /* DW_FORM_ref_addr is an address in DWARF2, and an offset in |
1019 | DWARF3. */ | |
c07cbdd7 | 1020 | if (unit->version == 3 || unit->version == 4) |
5609a71e DJ |
1021 | { |
1022 | if (unit->offset_size == 4) | |
dbb3fbbb | 1023 | attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end); |
5609a71e | 1024 | else |
dbb3fbbb | 1025 | attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end); |
5609a71e DJ |
1026 | info_ptr += unit->offset_size; |
1027 | break; | |
1028 | } | |
1029 | /* FALLTHROUGH */ | |
1030 | case DW_FORM_addr: | |
dbb3fbbb | 1031 | attr->u.val = read_address (unit, info_ptr, info_ptr_end); |
252b5132 RH |
1032 | info_ptr += unit->addr_size; |
1033 | break; | |
95e34fb4 | 1034 | case DW_FORM_GNU_ref_alt: |
c07cbdd7 JJ |
1035 | case DW_FORM_sec_offset: |
1036 | if (unit->offset_size == 4) | |
dbb3fbbb | 1037 | attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end); |
c07cbdd7 | 1038 | else |
dbb3fbbb | 1039 | attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end); |
c07cbdd7 JJ |
1040 | info_ptr += unit->offset_size; |
1041 | break; | |
252b5132 | 1042 | case DW_FORM_block2: |
dc810e39 | 1043 | amt = sizeof (struct dwarf_block); |
a50b1753 | 1044 | blk = (struct dwarf_block *) bfd_alloc (abfd, amt); |
8af6b354 AM |
1045 | if (blk == NULL) |
1046 | return NULL; | |
dbb3fbbb | 1047 | blk->size = read_2_bytes (abfd, info_ptr, info_ptr_end); |
252b5132 | 1048 | info_ptr += 2; |
dbb3fbbb | 1049 | blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size); |
252b5132 | 1050 | info_ptr += blk->size; |
482e2e37 | 1051 | attr->u.blk = blk; |
252b5132 RH |
1052 | break; |
1053 | case DW_FORM_block4: | |
dc810e39 | 1054 | amt = sizeof (struct dwarf_block); |
a50b1753 | 1055 | blk = (struct dwarf_block *) bfd_alloc (abfd, amt); |
8af6b354 AM |
1056 | if (blk == NULL) |
1057 | return NULL; | |
dbb3fbbb | 1058 | blk->size = read_4_bytes (abfd, info_ptr, info_ptr_end); |
252b5132 | 1059 | info_ptr += 4; |
dbb3fbbb | 1060 | blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size); |
252b5132 | 1061 | info_ptr += blk->size; |
482e2e37 | 1062 | attr->u.blk = blk; |
252b5132 RH |
1063 | break; |
1064 | case DW_FORM_data2: | |
dbb3fbbb | 1065 | attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end); |
252b5132 RH |
1066 | info_ptr += 2; |
1067 | break; | |
1068 | case DW_FORM_data4: | |
dbb3fbbb | 1069 | attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end); |
252b5132 RH |
1070 | info_ptr += 4; |
1071 | break; | |
1072 | case DW_FORM_data8: | |
dbb3fbbb | 1073 | attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end); |
252b5132 RH |
1074 | info_ptr += 8; |
1075 | break; | |
1076 | case DW_FORM_string: | |
dbb3fbbb | 1077 | attr->u.str = read_string (abfd, info_ptr, info_ptr_end, &bytes_read); |
252b5132 RH |
1078 | info_ptr += bytes_read; |
1079 | break; | |
d03ba2a1 | 1080 | case DW_FORM_strp: |
dbb3fbbb | 1081 | attr->u.str = read_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read); |
d03ba2a1 JJ |
1082 | info_ptr += bytes_read; |
1083 | break; | |
95e34fb4 | 1084 | case DW_FORM_GNU_strp_alt: |
dbb3fbbb | 1085 | attr->u.str = read_alt_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read); |
95e34fb4 NC |
1086 | info_ptr += bytes_read; |
1087 | break; | |
c07cbdd7 | 1088 | case DW_FORM_exprloc: |
252b5132 | 1089 | case DW_FORM_block: |
dc810e39 | 1090 | amt = sizeof (struct dwarf_block); |
a50b1753 | 1091 | blk = (struct dwarf_block *) bfd_alloc (abfd, amt); |
8af6b354 AM |
1092 | if (blk == NULL) |
1093 | return NULL; | |
dbb3fbbb | 1094 | blk->size = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end); |
252b5132 | 1095 | info_ptr += bytes_read; |
dbb3fbbb | 1096 | blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size); |
252b5132 | 1097 | info_ptr += blk->size; |
482e2e37 | 1098 | attr->u.blk = blk; |
252b5132 RH |
1099 | break; |
1100 | case DW_FORM_block1: | |
dc810e39 | 1101 | amt = sizeof (struct dwarf_block); |
a50b1753 | 1102 | blk = (struct dwarf_block *) bfd_alloc (abfd, amt); |
8af6b354 AM |
1103 | if (blk == NULL) |
1104 | return NULL; | |
dbb3fbbb | 1105 | blk->size = read_1_byte (abfd, info_ptr, info_ptr_end); |
252b5132 | 1106 | info_ptr += 1; |
dbb3fbbb | 1107 | blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size); |
252b5132 | 1108 | info_ptr += blk->size; |
482e2e37 | 1109 | attr->u.blk = blk; |
252b5132 RH |
1110 | break; |
1111 | case DW_FORM_data1: | |
dbb3fbbb | 1112 | attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end); |
252b5132 RH |
1113 | info_ptr += 1; |
1114 | break; | |
1115 | case DW_FORM_flag: | |
dbb3fbbb | 1116 | attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end); |
252b5132 RH |
1117 | info_ptr += 1; |
1118 | break; | |
c07cbdd7 JJ |
1119 | case DW_FORM_flag_present: |
1120 | attr->u.val = 1; | |
1121 | break; | |
252b5132 | 1122 | case DW_FORM_sdata: |
dbb3fbbb | 1123 | attr->u.sval = safe_read_leb128 (abfd, info_ptr, &bytes_read, TRUE, info_ptr_end); |
252b5132 RH |
1124 | info_ptr += bytes_read; |
1125 | break; | |
1126 | case DW_FORM_udata: | |
dbb3fbbb | 1127 | attr->u.val = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end); |
252b5132 RH |
1128 | info_ptr += bytes_read; |
1129 | break; | |
1130 | case DW_FORM_ref1: | |
dbb3fbbb | 1131 | attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end); |
252b5132 RH |
1132 | info_ptr += 1; |
1133 | break; | |
1134 | case DW_FORM_ref2: | |
dbb3fbbb | 1135 | attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end); |
252b5132 RH |
1136 | info_ptr += 2; |
1137 | break; | |
1138 | case DW_FORM_ref4: | |
dbb3fbbb | 1139 | attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end); |
252b5132 RH |
1140 | info_ptr += 4; |
1141 | break; | |
81edd86d | 1142 | case DW_FORM_ref8: |
dbb3fbbb | 1143 | attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end); |
81edd86d MM |
1144 | info_ptr += 8; |
1145 | break; | |
a37a68dd | 1146 | case DW_FORM_ref_sig8: |
dbb3fbbb | 1147 | attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end); |
a37a68dd CC |
1148 | info_ptr += 8; |
1149 | break; | |
252b5132 | 1150 | case DW_FORM_ref_udata: |
dbb3fbbb | 1151 | attr->u.val = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end); |
252b5132 RH |
1152 | info_ptr += bytes_read; |
1153 | break; | |
252b5132 | 1154 | case DW_FORM_indirect: |
dbb3fbbb | 1155 | form = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end); |
cf716c56 | 1156 | info_ptr += bytes_read; |
dbb3fbbb | 1157 | info_ptr = read_attribute_value (attr, form, unit, info_ptr, info_ptr_end); |
cf716c56 | 1158 | break; |
252b5132 | 1159 | default: |
95e34fb4 | 1160 | (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %#x."), |
cf716c56 | 1161 | form); |
252b5132 | 1162 | bfd_set_error (bfd_error_bad_value); |
c07cbdd7 | 1163 | return NULL; |
252b5132 RH |
1164 | } |
1165 | return info_ptr; | |
1166 | } | |
1167 | ||
cf716c56 RH |
1168 | /* Read an attribute described by an abbreviated attribute. */ |
1169 | ||
f075ee0c | 1170 | static bfd_byte * |
dbb3fbbb NC |
1171 | read_attribute (struct attribute * attr, |
1172 | struct attr_abbrev * abbrev, | |
1173 | struct comp_unit * unit, | |
1174 | bfd_byte * info_ptr, | |
1175 | bfd_byte * info_ptr_end) | |
cf716c56 RH |
1176 | { |
1177 | attr->name = abbrev->name; | |
dbb3fbbb | 1178 | info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr, info_ptr_end); |
cf716c56 RH |
1179 | return info_ptr; |
1180 | } | |
1181 | ||
e00e8198 AM |
1182 | /* Return whether DW_AT_name will return the same as DW_AT_linkage_name |
1183 | for a function. */ | |
1184 | ||
1185 | static bfd_boolean | |
1186 | non_mangled (int lang) | |
1187 | { | |
1188 | switch (lang) | |
1189 | { | |
1190 | default: | |
1191 | return FALSE; | |
1192 | ||
1193 | case DW_LANG_C89: | |
1194 | case DW_LANG_C: | |
1195 | case DW_LANG_Ada83: | |
1196 | case DW_LANG_Cobol74: | |
1197 | case DW_LANG_Cobol85: | |
1198 | case DW_LANG_Fortran77: | |
1199 | case DW_LANG_Pascal83: | |
1200 | case DW_LANG_C99: | |
1201 | case DW_LANG_Ada95: | |
1202 | case DW_LANG_PLI: | |
1203 | case DW_LANG_UPC: | |
1204 | case DW_LANG_C11: | |
1205 | return TRUE; | |
1206 | } | |
1207 | } | |
1208 | ||
a092b084 | 1209 | /* Source line information table routines. */ |
252b5132 RH |
1210 | |
1211 | #define FILE_ALLOC_CHUNK 5 | |
1212 | #define DIR_ALLOC_CHUNK 5 | |
1213 | ||
a092b084 NC |
1214 | struct line_info |
1215 | { | |
252b5132 | 1216 | struct line_info* prev_line; |
252b5132 | 1217 | bfd_vma address; |
f075ee0c | 1218 | char *filename; |
252b5132 RH |
1219 | unsigned int line; |
1220 | unsigned int column; | |
9b8d1a36 | 1221 | unsigned int discriminator; |
a233b20c JJ |
1222 | unsigned char op_index; |
1223 | unsigned char end_sequence; /* End of (sequential) code sequence. */ | |
252b5132 RH |
1224 | }; |
1225 | ||
a092b084 NC |
1226 | struct fileinfo |
1227 | { | |
252b5132 RH |
1228 | char *name; |
1229 | unsigned int dir; | |
1230 | unsigned int time; | |
1231 | unsigned int size; | |
1232 | }; | |
1233 | ||
0ee19663 NC |
1234 | struct line_sequence |
1235 | { | |
1236 | bfd_vma low_pc; | |
1237 | struct line_sequence* prev_sequence; | |
1238 | struct line_info* last_line; /* Largest VMA. */ | |
1239 | }; | |
1240 | ||
a092b084 NC |
1241 | struct line_info_table |
1242 | { | |
0ee19663 NC |
1243 | bfd* abfd; |
1244 | unsigned int num_files; | |
1245 | unsigned int num_dirs; | |
1246 | unsigned int num_sequences; | |
1247 | char * comp_dir; | |
1248 | char ** dirs; | |
1249 | struct fileinfo* files; | |
1250 | struct line_sequence* sequences; | |
1251 | struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */ | |
252b5132 RH |
1252 | }; |
1253 | ||
4ab527b0 FF |
1254 | /* Remember some information about each function. If the function is |
1255 | inlined (DW_TAG_inlined_subroutine) it may have two additional | |
1256 | attributes, DW_AT_call_file and DW_AT_call_line, which specify the | |
a2a50954 | 1257 | source code location where this function was inlined. */ |
4ab527b0 | 1258 | |
1ee24f27 DJ |
1259 | struct funcinfo |
1260 | { | |
a2a50954 AM |
1261 | /* Pointer to previous function in list of all functions. */ |
1262 | struct funcinfo *prev_func; | |
1263 | /* Pointer to function one scope higher. */ | |
1264 | struct funcinfo *caller_func; | |
1265 | /* Source location file name where caller_func inlines this func. */ | |
1266 | char *caller_file; | |
a2a50954 AM |
1267 | /* Source location file name. */ |
1268 | char *file; | |
e00e8198 AM |
1269 | /* Source location line number where caller_func inlines this func. */ |
1270 | int caller_line; | |
a2a50954 AM |
1271 | /* Source location line number. */ |
1272 | int line; | |
4ab527b0 | 1273 | int tag; |
e00e8198 AM |
1274 | bfd_boolean is_linkage; |
1275 | const char *name; | |
a13afe8e | 1276 | struct arange arange; |
a2a50954 AM |
1277 | /* Where the symbol is defined. */ |
1278 | asection *sec; | |
5420f73d L |
1279 | }; |
1280 | ||
1281 | struct varinfo | |
1282 | { | |
709d67f1 | 1283 | /* Pointer to previous variable in list of all variables */ |
5420f73d | 1284 | struct varinfo *prev_var; |
709d67f1 | 1285 | /* Source location file name */ |
5420f73d | 1286 | char *file; |
709d67f1 | 1287 | /* Source location line number */ |
5420f73d L |
1288 | int line; |
1289 | int tag; | |
1290 | char *name; | |
5cf2e3f0 | 1291 | bfd_vma addr; |
709d67f1 | 1292 | /* Where the symbol is defined */ |
5420f73d | 1293 | asection *sec; |
709d67f1 | 1294 | /* Is this a stack variable? */ |
5420f73d | 1295 | unsigned int stack: 1; |
1ee24f27 DJ |
1296 | }; |
1297 | ||
d4c32a81 L |
1298 | /* Return TRUE if NEW_LINE should sort after LINE. */ |
1299 | ||
1300 | static inline bfd_boolean | |
1301 | new_line_sorts_after (struct line_info *new_line, struct line_info *line) | |
1302 | { | |
1303 | return (new_line->address > line->address | |
1304 | || (new_line->address == line->address | |
a233b20c JJ |
1305 | && (new_line->op_index > line->op_index |
1306 | || (new_line->op_index == line->op_index | |
1307 | && new_line->end_sequence < line->end_sequence)))); | |
d4c32a81 L |
1308 | } |
1309 | ||
1310 | ||
af3ef9fe NC |
1311 | /* Adds a new entry to the line_info list in the line_info_table, ensuring |
1312 | that the list is sorted. Note that the line_info list is sorted from | |
1313 | highest to lowest VMA (with possible duplicates); that is, | |
1314 | line_info->prev_line always accesses an equal or smaller VMA. */ | |
1315 | ||
8af6b354 | 1316 | static bfd_boolean |
818a27ac AM |
1317 | add_line_info (struct line_info_table *table, |
1318 | bfd_vma address, | |
a233b20c | 1319 | unsigned char op_index, |
818a27ac AM |
1320 | char *filename, |
1321 | unsigned int line, | |
1322 | unsigned int column, | |
9b8d1a36 | 1323 | unsigned int discriminator, |
818a27ac | 1324 | int end_sequence) |
252b5132 | 1325 | { |
dc810e39 | 1326 | bfd_size_type amt = sizeof (struct line_info); |
0ee19663 | 1327 | struct line_sequence* seq = table->sequences; |
a50b1753 | 1328 | struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt); |
252b5132 | 1329 | |
8af6b354 AM |
1330 | if (info == NULL) |
1331 | return FALSE; | |
1332 | ||
d4c32a81 | 1333 | /* Set member data of 'info'. */ |
f5296ddc | 1334 | info->prev_line = NULL; |
d4c32a81 | 1335 | info->address = address; |
a233b20c | 1336 | info->op_index = op_index; |
d4c32a81 L |
1337 | info->line = line; |
1338 | info->column = column; | |
9b8d1a36 | 1339 | info->discriminator = discriminator; |
d4c32a81 L |
1340 | info->end_sequence = end_sequence; |
1341 | ||
1342 | if (filename && filename[0]) | |
1343 | { | |
a50b1753 | 1344 | info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1); |
8af6b354 AM |
1345 | if (info->filename == NULL) |
1346 | return FALSE; | |
1347 | strcpy (info->filename, filename); | |
d4c32a81 L |
1348 | } |
1349 | else | |
1350 | info->filename = NULL; | |
1351 | ||
e82ce529 AM |
1352 | /* Find the correct location for 'info'. Normally we will receive |
1353 | new line_info data 1) in order and 2) with increasing VMAs. | |
1354 | However some compilers break the rules (cf. decode_line_info) and | |
1355 | so we include some heuristics for quickly finding the correct | |
1356 | location for 'info'. In particular, these heuristics optimize for | |
1357 | the common case in which the VMA sequence that we receive is a | |
1358 | list of locally sorted VMAs such as | |
1359 | p...z a...j (where a < j < p < z) | |
252b5132 | 1360 | |
e82ce529 | 1361 | Note: table->lcl_head is used to head an *actual* or *possible* |
0ee19663 | 1362 | sub-sequence within the list (such as a...j) that is not directly |
e82ce529 AM |
1363 | headed by table->last_line |
1364 | ||
1365 | Note: we may receive duplicate entries from 'decode_line_info'. */ | |
1366 | ||
0ee19663 NC |
1367 | if (seq |
1368 | && seq->last_line->address == address | |
a233b20c | 1369 | && seq->last_line->op_index == op_index |
0ee19663 | 1370 | && seq->last_line->end_sequence == end_sequence) |
aff90a5f L |
1371 | { |
1372 | /* We only keep the last entry with the same address and end | |
1373 | sequence. See PR ld/4986. */ | |
0ee19663 | 1374 | if (table->lcl_head == seq->last_line) |
aff90a5f | 1375 | table->lcl_head = info; |
0ee19663 NC |
1376 | info->prev_line = seq->last_line->prev_line; |
1377 | seq->last_line = info; | |
aff90a5f | 1378 | } |
0ee19663 | 1379 | else if (!seq || seq->last_line->end_sequence) |
d8d1c398 | 1380 | { |
0ee19663 NC |
1381 | /* Start a new line sequence. */ |
1382 | amt = sizeof (struct line_sequence); | |
1383 | seq = (struct line_sequence *) bfd_malloc (amt); | |
8af6b354 AM |
1384 | if (seq == NULL) |
1385 | return FALSE; | |
0ee19663 NC |
1386 | seq->low_pc = address; |
1387 | seq->prev_sequence = table->sequences; | |
1388 | seq->last_line = info; | |
1389 | table->lcl_head = info; | |
1390 | table->sequences = seq; | |
1391 | table->num_sequences++; | |
1392 | } | |
1393 | else if (new_line_sorts_after (info, seq->last_line)) | |
1394 | { | |
1395 | /* Normal case: add 'info' to the beginning of the current sequence. */ | |
1396 | info->prev_line = seq->last_line; | |
1397 | seq->last_line = info; | |
e82ce529 | 1398 | |
d8d1c398 AM |
1399 | /* lcl_head: initialize to head a *possible* sequence at the end. */ |
1400 | if (!table->lcl_head) | |
1401 | table->lcl_head = info; | |
1402 | } | |
1403 | else if (!new_line_sorts_after (info, table->lcl_head) | |
1404 | && (!table->lcl_head->prev_line | |
1405 | || new_line_sorts_after (info, table->lcl_head->prev_line))) | |
1406 | { | |
1407 | /* Abnormal but easy: lcl_head is the head of 'info'. */ | |
1408 | info->prev_line = table->lcl_head->prev_line; | |
1409 | table->lcl_head->prev_line = info; | |
1410 | } | |
1411 | else | |
1412 | { | |
0ee19663 NC |
1413 | /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' |
1414 | are valid heads for 'info'. Reset 'lcl_head'. */ | |
1415 | struct line_info* li2 = seq->last_line; /* Always non-NULL. */ | |
d8d1c398 | 1416 | struct line_info* li1 = li2->prev_line; |
e82ce529 | 1417 | |
d8d1c398 AM |
1418 | while (li1) |
1419 | { | |
1420 | if (!new_line_sorts_after (info, li2) | |
1421 | && new_line_sorts_after (info, li1)) | |
1422 | break; | |
e82ce529 | 1423 | |
709d67f1 | 1424 | li2 = li1; /* always non-NULL */ |
d8d1c398 AM |
1425 | li1 = li1->prev_line; |
1426 | } | |
1427 | table->lcl_head = li2; | |
1428 | info->prev_line = table->lcl_head->prev_line; | |
1429 | table->lcl_head->prev_line = info; | |
0ee19663 | 1430 | if (address < seq->low_pc) |
93ee1e36 | 1431 | seq->low_pc = address; |
d8d1c398 | 1432 | } |
8af6b354 | 1433 | return TRUE; |
252b5132 RH |
1434 | } |
1435 | ||
5ed6aba4 | 1436 | /* Extract a fully qualified filename from a line info table. |
af3ef9fe NC |
1437 | The returned string has been malloc'ed and it is the caller's |
1438 | responsibility to free it. */ | |
5ed6aba4 | 1439 | |
a092b084 | 1440 | static char * |
818a27ac | 1441 | concat_filename (struct line_info_table *table, unsigned int file) |
252b5132 | 1442 | { |
f075ee0c | 1443 | char *filename; |
159002ff RH |
1444 | |
1445 | if (file - 1 >= table->num_files) | |
1446 | { | |
75a657ba L |
1447 | /* FILE == 0 means unknown. */ |
1448 | if (file) | |
1449 | (*_bfd_error_handler) | |
1450 | (_("Dwarf Error: mangled line number section (bad file number).")); | |
af3ef9fe | 1451 | return strdup ("<unknown>"); |
159002ff RH |
1452 | } |
1453 | ||
1454 | filename = table->files[file - 1].name; | |
5ed6aba4 | 1455 | |
7421a730 | 1456 | if (!IS_ABSOLUTE_PATH (filename)) |
252b5132 | 1457 | { |
608fa8d3 JB |
1458 | char *dir_name = NULL; |
1459 | char *subdir_name = NULL; | |
7421a730 AM |
1460 | char *name; |
1461 | size_t len; | |
0dafd5f6 | 1462 | |
877a8638 | 1463 | if (table->files[file - 1].dir |
dbb3fbbb NC |
1464 | /* PR 17512: file: 0317e960. */ |
1465 | && table->files[file - 1].dir <= table->num_dirs | |
877a8638 NC |
1466 | /* PR 17512: file: 7f3d2e4b. */ |
1467 | && table->dirs != NULL) | |
608fa8d3 | 1468 | subdir_name = table->dirs[table->files[file - 1].dir - 1]; |
7421a730 | 1469 | |
608fa8d3 JB |
1470 | if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name)) |
1471 | dir_name = table->comp_dir; | |
7421a730 | 1472 | |
608fa8d3 | 1473 | if (!dir_name) |
af3ef9fe | 1474 | { |
608fa8d3 JB |
1475 | dir_name = subdir_name; |
1476 | subdir_name = NULL; | |
7421a730 | 1477 | } |
af3ef9fe | 1478 | |
608fa8d3 | 1479 | if (!dir_name) |
7421a730 AM |
1480 | return strdup (filename); |
1481 | ||
608fa8d3 | 1482 | len = strlen (dir_name) + strlen (filename) + 2; |
7421a730 | 1483 | |
608fa8d3 | 1484 | if (subdir_name) |
7421a730 | 1485 | { |
608fa8d3 | 1486 | len += strlen (subdir_name) + 1; |
a50b1753 | 1487 | name = (char *) bfd_malloc (len); |
7421a730 | 1488 | if (name) |
608fa8d3 | 1489 | sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename); |
7421a730 AM |
1490 | } |
1491 | else | |
1492 | { | |
a50b1753 | 1493 | name = (char *) bfd_malloc (len); |
af3ef9fe | 1494 | if (name) |
608fa8d3 | 1495 | sprintf (name, "%s/%s", dir_name, filename); |
af3ef9fe | 1496 | } |
7421a730 AM |
1497 | |
1498 | return name; | |
252b5132 | 1499 | } |
af3ef9fe NC |
1500 | |
1501 | return strdup (filename); | |
252b5132 RH |
1502 | } |
1503 | ||
8af6b354 | 1504 | static bfd_boolean |
a2a50954 | 1505 | arange_add (const struct comp_unit *unit, struct arange *first_arange, |
8af6b354 | 1506 | bfd_vma low_pc, bfd_vma high_pc) |
f623be2b RH |
1507 | { |
1508 | struct arange *arange; | |
1509 | ||
a2a50954 AM |
1510 | /* Ignore empty ranges. */ |
1511 | if (low_pc == high_pc) | |
1512 | return TRUE; | |
1513 | ||
1514 | /* If the first arange is empty, use it. */ | |
a13afe8e FF |
1515 | if (first_arange->high == 0) |
1516 | { | |
1517 | first_arange->low = low_pc; | |
1518 | first_arange->high = high_pc; | |
8af6b354 | 1519 | return TRUE; |
a13afe8e | 1520 | } |
98591c73 | 1521 | |
a13afe8e FF |
1522 | /* Next see if we can cheaply extend an existing range. */ |
1523 | arange = first_arange; | |
f623be2b RH |
1524 | do |
1525 | { | |
1526 | if (low_pc == arange->high) | |
1527 | { | |
1528 | arange->high = high_pc; | |
8af6b354 | 1529 | return TRUE; |
f623be2b RH |
1530 | } |
1531 | if (high_pc == arange->low) | |
1532 | { | |
1533 | arange->low = low_pc; | |
8af6b354 | 1534 | return TRUE; |
f623be2b RH |
1535 | } |
1536 | arange = arange->next; | |
1537 | } | |
1538 | while (arange); | |
1539 | ||
a13afe8e | 1540 | /* Need to allocate a new arange and insert it into the arange list. |
709d67f1 | 1541 | Order isn't significant, so just insert after the first arange. */ |
a2a50954 | 1542 | arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange)); |
8af6b354 AM |
1543 | if (arange == NULL) |
1544 | return FALSE; | |
f623be2b RH |
1545 | arange->low = low_pc; |
1546 | arange->high = high_pc; | |
a13afe8e FF |
1547 | arange->next = first_arange->next; |
1548 | first_arange->next = arange; | |
8af6b354 | 1549 | return TRUE; |
f623be2b RH |
1550 | } |
1551 | ||
0ee19663 NC |
1552 | /* Compare function for line sequences. */ |
1553 | ||
1554 | static int | |
1555 | compare_sequences (const void* a, const void* b) | |
1556 | { | |
1557 | const struct line_sequence* seq1 = a; | |
1558 | const struct line_sequence* seq2 = b; | |
1559 | ||
1560 | /* Sort by low_pc as the primary key. */ | |
1561 | if (seq1->low_pc < seq2->low_pc) | |
1562 | return -1; | |
1563 | if (seq1->low_pc > seq2->low_pc) | |
1564 | return 1; | |
1565 | ||
1566 | /* If low_pc values are equal, sort in reverse order of | |
1567 | high_pc, so that the largest region comes first. */ | |
1568 | if (seq1->last_line->address < seq2->last_line->address) | |
1569 | return 1; | |
1570 | if (seq1->last_line->address > seq2->last_line->address) | |
1571 | return -1; | |
1572 | ||
a233b20c JJ |
1573 | if (seq1->last_line->op_index < seq2->last_line->op_index) |
1574 | return 1; | |
1575 | if (seq1->last_line->op_index > seq2->last_line->op_index) | |
1576 | return -1; | |
1577 | ||
0ee19663 NC |
1578 | return 0; |
1579 | } | |
1580 | ||
1581 | /* Sort the line sequences for quick lookup. */ | |
1582 | ||
8af6b354 | 1583 | static bfd_boolean |
0ee19663 NC |
1584 | sort_line_sequences (struct line_info_table* table) |
1585 | { | |
1586 | bfd_size_type amt; | |
1587 | struct line_sequence* sequences; | |
1588 | struct line_sequence* seq; | |
1589 | unsigned int n = 0; | |
1590 | unsigned int num_sequences = table->num_sequences; | |
1591 | bfd_vma last_high_pc; | |
1592 | ||
1593 | if (num_sequences == 0) | |
8af6b354 | 1594 | return TRUE; |
0ee19663 NC |
1595 | |
1596 | /* Allocate space for an array of sequences. */ | |
1597 | amt = sizeof (struct line_sequence) * num_sequences; | |
1598 | sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt); | |
8af6b354 AM |
1599 | if (sequences == NULL) |
1600 | return FALSE; | |
0ee19663 NC |
1601 | |
1602 | /* Copy the linked list into the array, freeing the original nodes. */ | |
1603 | seq = table->sequences; | |
1604 | for (n = 0; n < num_sequences; n++) | |
1605 | { | |
1606 | struct line_sequence* last_seq = seq; | |
1607 | ||
1608 | BFD_ASSERT (seq); | |
1609 | sequences[n].low_pc = seq->low_pc; | |
1610 | sequences[n].prev_sequence = NULL; | |
1611 | sequences[n].last_line = seq->last_line; | |
1612 | seq = seq->prev_sequence; | |
1613 | free (last_seq); | |
1614 | } | |
1615 | BFD_ASSERT (seq == NULL); | |
1616 | ||
1617 | qsort (sequences, n, sizeof (struct line_sequence), compare_sequences); | |
1618 | ||
1619 | /* Make the list binary-searchable by trimming overlapping entries | |
1620 | and removing nested entries. */ | |
1621 | num_sequences = 1; | |
1622 | last_high_pc = sequences[0].last_line->address; | |
1623 | for (n = 1; n < table->num_sequences; n++) | |
1624 | { | |
1625 | if (sequences[n].low_pc < last_high_pc) | |
93ee1e36 | 1626 | { |
0ee19663 NC |
1627 | if (sequences[n].last_line->address <= last_high_pc) |
1628 | /* Skip nested entries. */ | |
1629 | continue; | |
1630 | ||
1631 | /* Trim overlapping entries. */ | |
1632 | sequences[n].low_pc = last_high_pc; | |
93ee1e36 | 1633 | } |
0ee19663 NC |
1634 | last_high_pc = sequences[n].last_line->address; |
1635 | if (n > num_sequences) | |
93ee1e36 AM |
1636 | { |
1637 | /* Close up the gap. */ | |
1638 | sequences[num_sequences].low_pc = sequences[n].low_pc; | |
1639 | sequences[num_sequences].last_line = sequences[n].last_line; | |
1640 | } | |
0ee19663 NC |
1641 | num_sequences++; |
1642 | } | |
1643 | ||
1644 | table->sequences = sequences; | |
1645 | table->num_sequences = num_sequences; | |
8af6b354 | 1646 | return TRUE; |
0ee19663 NC |
1647 | } |
1648 | ||
34b5e0b2 | 1649 | /* Decode the line number information for UNIT. */ |
252b5132 | 1650 | |
34b5e0b2 | 1651 | static struct line_info_table* |
818a27ac | 1652 | decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash) |
252b5132 RH |
1653 | { |
1654 | bfd *abfd = unit->abfd; | |
252b5132 | 1655 | struct line_info_table* table; |
f075ee0c AM |
1656 | bfd_byte *line_ptr; |
1657 | bfd_byte *line_end; | |
252b5132 | 1658 | struct line_head lh; |
d03ba2a1 | 1659 | unsigned int i, bytes_read, offset_size; |
252b5132 RH |
1660 | char *cur_file, *cur_dir; |
1661 | unsigned char op_code, extended_op, adj_opcode; | |
fec16237 | 1662 | unsigned int exop_len; |
dc810e39 | 1663 | bfd_size_type amt; |
252b5132 | 1664 | |
fc28f9aa | 1665 | if (! read_section (abfd, &stash->debug_sections[debug_line], |
93ee1e36 | 1666 | stash->syms, unit->line_offset, |
9e32b19f | 1667 | &stash->dwarf_line_buffer, &stash->dwarf_line_size)) |
8af6b354 | 1668 | return NULL; |
ccdb16fc | 1669 | |
dc810e39 | 1670 | amt = sizeof (struct line_info_table); |
a50b1753 | 1671 | table = (struct line_info_table *) bfd_alloc (abfd, amt); |
8af6b354 AM |
1672 | if (table == NULL) |
1673 | return NULL; | |
252b5132 RH |
1674 | table->abfd = abfd; |
1675 | table->comp_dir = unit->comp_dir; | |
1676 | ||
1677 | table->num_files = 0; | |
1678 | table->files = NULL; | |
1679 | ||
1680 | table->num_dirs = 0; | |
1681 | table->dirs = NULL; | |
1682 | ||
0ee19663 NC |
1683 | table->num_sequences = 0; |
1684 | table->sequences = NULL; | |
1685 | ||
e82ce529 | 1686 | table->lcl_head = NULL; |
159002ff | 1687 | |
dbb3fbbb NC |
1688 | if (stash->dwarf_line_size < 16) |
1689 | { | |
1690 | (*_bfd_error_handler) | |
1691 | (_("Dwarf Error: Line info section is too small (%ld)"), | |
1692 | (long) stash->dwarf_line_size); | |
1693 | bfd_set_error (bfd_error_bad_value); | |
1694 | return NULL; | |
1695 | } | |
69dd2e2d | 1696 | line_ptr = stash->dwarf_line_buffer + unit->line_offset; |
dbb3fbbb | 1697 | line_end = stash->dwarf_line_buffer + stash->dwarf_line_size; |
252b5132 | 1698 | |
a092b084 | 1699 | /* Read in the prologue. */ |
dbb3fbbb | 1700 | lh.total_length = read_4_bytes (abfd, line_ptr, line_end); |
91a4d569 AM |
1701 | line_ptr += 4; |
1702 | offset_size = 4; | |
1703 | if (lh.total_length == 0xffffffff) | |
dae2dd0d | 1704 | { |
dbb3fbbb | 1705 | lh.total_length = read_8_bytes (abfd, line_ptr, line_end); |
dae2dd0d NC |
1706 | line_ptr += 8; |
1707 | offset_size = 8; | |
1708 | } | |
91a4d569 | 1709 | else if (lh.total_length == 0 && unit->addr_size == 8) |
d03ba2a1 | 1710 | { |
91a4d569 | 1711 | /* Handle (non-standard) 64-bit DWARF2 formats. */ |
dbb3fbbb | 1712 | lh.total_length = read_4_bytes (abfd, line_ptr, line_end); |
91a4d569 | 1713 | line_ptr += 4; |
d03ba2a1 JJ |
1714 | offset_size = 8; |
1715 | } | |
dbb3fbbb NC |
1716 | |
1717 | if (lh.total_length > stash->dwarf_line_size) | |
1718 | { | |
1719 | (*_bfd_error_handler) | |
1720 | (_("Dwarf Error: Line info data is bigger (0x%lx) than the section (0x%lx)"), | |
1721 | (long) lh.total_length, (long) stash->dwarf_line_size); | |
1722 | bfd_set_error (bfd_error_bad_value); | |
1723 | return NULL; | |
1724 | } | |
62f8d217 | 1725 | |
252b5132 | 1726 | line_end = line_ptr + lh.total_length; |
62f8d217 | 1727 | |
dbb3fbbb | 1728 | lh.version = read_2_bytes (abfd, line_ptr, line_end); |
a233b20c JJ |
1729 | if (lh.version < 2 || lh.version > 4) |
1730 | { | |
1731 | (*_bfd_error_handler) | |
1732 | (_("Dwarf Error: Unhandled .debug_line version %d."), lh.version); | |
1733 | bfd_set_error (bfd_error_bad_value); | |
1734 | return NULL; | |
1735 | } | |
252b5132 | 1736 | line_ptr += 2; |
dbb3fbbb NC |
1737 | |
1738 | if (line_ptr + offset_size + (lh.version >=4 ? 6 : 5) >= line_end) | |
1739 | { | |
1740 | (*_bfd_error_handler) | |
1741 | (_("Dwarf Error: Ran out of room reading prologue")); | |
1742 | bfd_set_error (bfd_error_bad_value); | |
1743 | return NULL; | |
1744 | } | |
1745 | ||
d03ba2a1 | 1746 | if (offset_size == 4) |
dbb3fbbb | 1747 | lh.prologue_length = read_4_bytes (abfd, line_ptr, line_end); |
d03ba2a1 | 1748 | else |
dbb3fbbb | 1749 | lh.prologue_length = read_8_bytes (abfd, line_ptr, line_end); |
d03ba2a1 | 1750 | line_ptr += offset_size; |
dbb3fbbb NC |
1751 | |
1752 | lh.minimum_instruction_length = read_1_byte (abfd, line_ptr, line_end); | |
252b5132 | 1753 | line_ptr += 1; |
dbb3fbbb | 1754 | |
a233b20c JJ |
1755 | if (lh.version >= 4) |
1756 | { | |
dbb3fbbb | 1757 | lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr, line_end); |
a233b20c JJ |
1758 | line_ptr += 1; |
1759 | } | |
1760 | else | |
1761 | lh.maximum_ops_per_insn = 1; | |
dbb3fbbb | 1762 | |
a233b20c JJ |
1763 | if (lh.maximum_ops_per_insn == 0) |
1764 | { | |
1765 | (*_bfd_error_handler) | |
1766 | (_("Dwarf Error: Invalid maximum operations per instruction.")); | |
1767 | bfd_set_error (bfd_error_bad_value); | |
1768 | return NULL; | |
1769 | } | |
dbb3fbbb NC |
1770 | |
1771 | lh.default_is_stmt = read_1_byte (abfd, line_ptr, line_end); | |
252b5132 | 1772 | line_ptr += 1; |
dbb3fbbb NC |
1773 | |
1774 | lh.line_base = read_1_signed_byte (abfd, line_ptr, line_end); | |
252b5132 | 1775 | line_ptr += 1; |
dbb3fbbb NC |
1776 | |
1777 | lh.line_range = read_1_byte (abfd, line_ptr, line_end); | |
252b5132 | 1778 | line_ptr += 1; |
dbb3fbbb NC |
1779 | |
1780 | lh.opcode_base = read_1_byte (abfd, line_ptr, line_end); | |
252b5132 | 1781 | line_ptr += 1; |
dbb3fbbb NC |
1782 | |
1783 | if (line_ptr + (lh.opcode_base - 1) >= line_end) | |
1784 | { | |
1785 | (*_bfd_error_handler) (_("Dwarf Error: Ran out of room reading opcodes")); | |
1786 | bfd_set_error (bfd_error_bad_value); | |
1787 | return NULL; | |
1788 | } | |
62f8d217 | 1789 | |
dc810e39 | 1790 | amt = lh.opcode_base * sizeof (unsigned char); |
a50b1753 | 1791 | lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt); |
252b5132 RH |
1792 | |
1793 | lh.standard_opcode_lengths[0] = 1; | |
98591c73 | 1794 | |
252b5132 RH |
1795 | for (i = 1; i < lh.opcode_base; ++i) |
1796 | { | |
dbb3fbbb | 1797 | lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr, line_end); |
252b5132 RH |
1798 | line_ptr += 1; |
1799 | } | |
1800 | ||
a092b084 | 1801 | /* Read directory table. */ |
dbb3fbbb | 1802 | while ((cur_dir = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL) |
252b5132 RH |
1803 | { |
1804 | line_ptr += bytes_read; | |
98591c73 | 1805 | |
252b5132 RH |
1806 | if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0) |
1807 | { | |
35330cce NC |
1808 | char **tmp; |
1809 | ||
dc810e39 AM |
1810 | amt = table->num_dirs + DIR_ALLOC_CHUNK; |
1811 | amt *= sizeof (char *); | |
35330cce | 1812 | |
a50b1753 | 1813 | tmp = (char **) bfd_realloc (table->dirs, amt); |
35330cce | 1814 | if (tmp == NULL) |
8af6b354 | 1815 | goto fail; |
35330cce | 1816 | table->dirs = tmp; |
252b5132 | 1817 | } |
98591c73 | 1818 | |
252b5132 RH |
1819 | table->dirs[table->num_dirs++] = cur_dir; |
1820 | } | |
98591c73 | 1821 | |
252b5132 RH |
1822 | line_ptr += bytes_read; |
1823 | ||
a092b084 | 1824 | /* Read file name table. */ |
dbb3fbbb | 1825 | while ((cur_file = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL) |
252b5132 RH |
1826 | { |
1827 | line_ptr += bytes_read; | |
98591c73 | 1828 | |
252b5132 RH |
1829 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) |
1830 | { | |
35330cce NC |
1831 | struct fileinfo *tmp; |
1832 | ||
dc810e39 AM |
1833 | amt = table->num_files + FILE_ALLOC_CHUNK; |
1834 | amt *= sizeof (struct fileinfo); | |
35330cce | 1835 | |
a50b1753 | 1836 | tmp = (struct fileinfo *) bfd_realloc (table->files, amt); |
35330cce | 1837 | if (tmp == NULL) |
8af6b354 | 1838 | goto fail; |
35330cce | 1839 | table->files = tmp; |
252b5132 | 1840 | } |
98591c73 | 1841 | |
252b5132 RH |
1842 | table->files[table->num_files].name = cur_file; |
1843 | table->files[table->num_files].dir = | |
dbb3fbbb | 1844 | safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); |
252b5132 | 1845 | line_ptr += bytes_read; |
dbb3fbbb | 1846 | table->files[table->num_files].time = safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); |
252b5132 | 1847 | line_ptr += bytes_read; |
dbb3fbbb | 1848 | table->files[table->num_files].size = safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); |
252b5132 RH |
1849 | line_ptr += bytes_read; |
1850 | table->num_files++; | |
1851 | } | |
98591c73 | 1852 | |
252b5132 RH |
1853 | line_ptr += bytes_read; |
1854 | ||
1855 | /* Read the statement sequences until there's nothing left. */ | |
1856 | while (line_ptr < line_end) | |
1857 | { | |
a092b084 | 1858 | /* State machine registers. */ |
252b5132 | 1859 | bfd_vma address = 0; |
a233b20c | 1860 | unsigned char op_index = 0; |
8bfd78b3 | 1861 | char * filename = table->num_files ? concat_filename (table, 1) : NULL; |
252b5132 RH |
1862 | unsigned int line = 1; |
1863 | unsigned int column = 0; | |
9b8d1a36 | 1864 | unsigned int discriminator = 0; |
252b5132 | 1865 | int is_stmt = lh.default_is_stmt; |
e2f6d277 NC |
1866 | int end_sequence = 0; |
1867 | /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some | |
e82ce529 AM |
1868 | compilers generate address sequences that are wildly out of |
1869 | order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler | |
1870 | for ia64-Linux). Thus, to determine the low and high | |
1871 | address, we must compare on every DW_LNS_copy, etc. */ | |
75758e9d | 1872 | bfd_vma low_pc = (bfd_vma) -1; |
e2f6d277 | 1873 | bfd_vma high_pc = 0; |
252b5132 | 1874 | |
a092b084 | 1875 | /* Decode the table. */ |
252b5132 RH |
1876 | while (! end_sequence) |
1877 | { | |
dbb3fbbb | 1878 | op_code = read_1_byte (abfd, line_ptr, line_end); |
252b5132 | 1879 | line_ptr += 1; |
98591c73 | 1880 | |
1a509dcc | 1881 | if (op_code >= lh.opcode_base) |
e2f6d277 NC |
1882 | { |
1883 | /* Special operand. */ | |
1a509dcc | 1884 | adj_opcode = op_code - lh.opcode_base; |
dbb3fbbb NC |
1885 | if (lh.line_range == 0) |
1886 | goto line_fail; | |
a233b20c | 1887 | if (lh.maximum_ops_per_insn == 1) |
a2a50954 AM |
1888 | address += (adj_opcode / lh.line_range |
1889 | * lh.minimum_instruction_length); | |
a233b20c JJ |
1890 | else |
1891 | { | |
a2a50954 AM |
1892 | address += ((op_index + adj_opcode / lh.line_range) |
1893 | / lh.maximum_ops_per_insn | |
1894 | * lh.minimum_instruction_length); | |
1895 | op_index = ((op_index + adj_opcode / lh.line_range) | |
1896 | % lh.maximum_ops_per_insn); | |
a233b20c | 1897 | } |
1a509dcc GK |
1898 | line += lh.line_base + (adj_opcode % lh.line_range); |
1899 | /* Append row to matrix using current values. */ | |
a233b20c | 1900 | if (!add_line_info (table, address, op_index, filename, |
9b8d1a36 | 1901 | line, column, discriminator, 0)) |
8af6b354 | 1902 | goto line_fail; |
93ee1e36 | 1903 | discriminator = 0; |
75758e9d AM |
1904 | if (address < low_pc) |
1905 | low_pc = address; | |
e2f6d277 NC |
1906 | if (address > high_pc) |
1907 | high_pc = address; | |
1a509dcc GK |
1908 | } |
1909 | else switch (op_code) | |
252b5132 RH |
1910 | { |
1911 | case DW_LNS_extended_op: | |
dbb3fbbb | 1912 | exop_len = safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); |
fec16237 | 1913 | line_ptr += bytes_read; |
dbb3fbbb | 1914 | extended_op = read_1_byte (abfd, line_ptr, line_end); |
252b5132 | 1915 | line_ptr += 1; |
e2f6d277 | 1916 | |
252b5132 RH |
1917 | switch (extended_op) |
1918 | { | |
1919 | case DW_LNE_end_sequence: | |
1920 | end_sequence = 1; | |
9b8d1a36 CC |
1921 | if (!add_line_info (table, address, op_index, filename, line, |
1922 | column, discriminator, end_sequence)) | |
8af6b354 | 1923 | goto line_fail; |
93ee1e36 | 1924 | discriminator = 0; |
75758e9d AM |
1925 | if (address < low_pc) |
1926 | low_pc = address; | |
e2f6d277 NC |
1927 | if (address > high_pc) |
1928 | high_pc = address; | |
a2a50954 | 1929 | if (!arange_add (unit, &unit->arange, low_pc, high_pc)) |
8af6b354 | 1930 | goto line_fail; |
252b5132 RH |
1931 | break; |
1932 | case DW_LNE_set_address: | |
dbb3fbbb | 1933 | address = read_address (unit, line_ptr, line_end); |
a233b20c | 1934 | op_index = 0; |
252b5132 RH |
1935 | line_ptr += unit->addr_size; |
1936 | break; | |
1937 | case DW_LNE_define_file: | |
dbb3fbbb | 1938 | cur_file = read_string (abfd, line_ptr, line_end, &bytes_read); |
252b5132 RH |
1939 | line_ptr += bytes_read; |
1940 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) | |
1941 | { | |
35330cce NC |
1942 | struct fileinfo *tmp; |
1943 | ||
dc810e39 AM |
1944 | amt = table->num_files + FILE_ALLOC_CHUNK; |
1945 | amt *= sizeof (struct fileinfo); | |
a50b1753 | 1946 | tmp = (struct fileinfo *) bfd_realloc (table->files, amt); |
35330cce | 1947 | if (tmp == NULL) |
8af6b354 | 1948 | goto line_fail; |
35330cce | 1949 | table->files = tmp; |
252b5132 RH |
1950 | } |
1951 | table->files[table->num_files].name = cur_file; | |
1952 | table->files[table->num_files].dir = | |
dbb3fbbb | 1953 | safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); |
252b5132 RH |
1954 | line_ptr += bytes_read; |
1955 | table->files[table->num_files].time = | |
dbb3fbbb | 1956 | safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); |
252b5132 RH |
1957 | line_ptr += bytes_read; |
1958 | table->files[table->num_files].size = | |
dbb3fbbb | 1959 | safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); |
252b5132 RH |
1960 | line_ptr += bytes_read; |
1961 | table->num_files++; | |
1962 | break; | |
9e1f7c0e | 1963 | case DW_LNE_set_discriminator: |
9b8d1a36 | 1964 | discriminator = |
dbb3fbbb | 1965 | safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); |
9e1f7c0e DK |
1966 | line_ptr += bytes_read; |
1967 | break; | |
a2a50954 AM |
1968 | case DW_LNE_HP_source_file_correlation: |
1969 | line_ptr += exop_len - 1; | |
1970 | break; | |
252b5132 | 1971 | default: |
a2a50954 AM |
1972 | (*_bfd_error_handler) |
1973 | (_("Dwarf Error: mangled line number section.")); | |
252b5132 | 1974 | bfd_set_error (bfd_error_bad_value); |
8af6b354 AM |
1975 | line_fail: |
1976 | if (filename != NULL) | |
1977 | free (filename); | |
1978 | goto fail; | |
252b5132 RH |
1979 | } |
1980 | break; | |
1981 | case DW_LNS_copy: | |
a233b20c | 1982 | if (!add_line_info (table, address, op_index, |
9b8d1a36 | 1983 | filename, line, column, discriminator, 0)) |
8af6b354 | 1984 | goto line_fail; |
93ee1e36 | 1985 | discriminator = 0; |
75758e9d AM |
1986 | if (address < low_pc) |
1987 | low_pc = address; | |
e2f6d277 NC |
1988 | if (address > high_pc) |
1989 | high_pc = address; | |
252b5132 RH |
1990 | break; |
1991 | case DW_LNS_advance_pc: | |
a233b20c | 1992 | if (lh.maximum_ops_per_insn == 1) |
a2a50954 | 1993 | address += (lh.minimum_instruction_length |
dbb3fbbb NC |
1994 | * safe_read_leb128 (abfd, line_ptr, &bytes_read, |
1995 | FALSE, line_end)); | |
a233b20c JJ |
1996 | else |
1997 | { | |
dbb3fbbb NC |
1998 | bfd_vma adjust = safe_read_leb128 (abfd, line_ptr, &bytes_read, |
1999 | FALSE, line_end); | |
a2a50954 AM |
2000 | address = ((op_index + adjust) / lh.maximum_ops_per_insn |
2001 | * lh.minimum_instruction_length); | |
a233b20c JJ |
2002 | op_index = (op_index + adjust) % lh.maximum_ops_per_insn; |
2003 | } | |
252b5132 RH |
2004 | line_ptr += bytes_read; |
2005 | break; | |
2006 | case DW_LNS_advance_line: | |
dbb3fbbb | 2007 | line += safe_read_leb128 (abfd, line_ptr, &bytes_read, TRUE, line_end); |
252b5132 RH |
2008 | line_ptr += bytes_read; |
2009 | break; | |
2010 | case DW_LNS_set_file: | |
2011 | { | |
2012 | unsigned int file; | |
2013 | ||
e2f6d277 NC |
2014 | /* The file and directory tables are 0 |
2015 | based, the references are 1 based. */ | |
dbb3fbbb | 2016 | file = safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); |
252b5132 | 2017 | line_ptr += bytes_read; |
af3ef9fe NC |
2018 | if (filename) |
2019 | free (filename); | |
252b5132 RH |
2020 | filename = concat_filename (table, file); |
2021 | break; | |
2022 | } | |
2023 | case DW_LNS_set_column: | |
dbb3fbbb | 2024 | column = safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); |
252b5132 RH |
2025 | line_ptr += bytes_read; |
2026 | break; | |
2027 | case DW_LNS_negate_stmt: | |
2028 | is_stmt = (!is_stmt); | |
2029 | break; | |
2030 | case DW_LNS_set_basic_block: | |
252b5132 RH |
2031 | break; |
2032 | case DW_LNS_const_add_pc: | |
a233b20c | 2033 | if (lh.maximum_ops_per_insn == 1) |
a2a50954 AM |
2034 | address += (lh.minimum_instruction_length |
2035 | * ((255 - lh.opcode_base) / lh.line_range)); | |
a233b20c JJ |
2036 | else |
2037 | { | |
2038 | bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range); | |
a2a50954 AM |
2039 | address += (lh.minimum_instruction_length |
2040 | * ((op_index + adjust) | |
2041 | / lh.maximum_ops_per_insn)); | |
a233b20c JJ |
2042 | op_index = (op_index + adjust) % lh.maximum_ops_per_insn; |
2043 | } | |
252b5132 RH |
2044 | break; |
2045 | case DW_LNS_fixed_advance_pc: | |
dbb3fbbb | 2046 | address += read_2_bytes (abfd, line_ptr, line_end); |
a233b20c | 2047 | op_index = 0; |
252b5132 RH |
2048 | line_ptr += 2; |
2049 | break; | |
1a509dcc | 2050 | default: |
91d6fa6a NC |
2051 | /* Unknown standard opcode, ignore it. */ |
2052 | for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++) | |
2053 | { | |
dbb3fbbb | 2054 | (void) safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); |
91d6fa6a NC |
2055 | line_ptr += bytes_read; |
2056 | } | |
2057 | break; | |
252b5132 RH |
2058 | } |
2059 | } | |
5ed6aba4 | 2060 | |
af3ef9fe NC |
2061 | if (filename) |
2062 | free (filename); | |
252b5132 RH |
2063 | } |
2064 | ||
8af6b354 AM |
2065 | if (sort_line_sequences (table)) |
2066 | return table; | |
0ee19663 | 2067 | |
8af6b354 AM |
2068 | fail: |
2069 | if (table->sequences != NULL) | |
2070 | free (table->sequences); | |
2071 | if (table->files != NULL) | |
2072 | free (table->files); | |
2073 | if (table->dirs != NULL) | |
2074 | free (table->dirs); | |
2075 | return NULL; | |
252b5132 RH |
2076 | } |
2077 | ||
240d6706 NC |
2078 | /* If ADDR is within TABLE set the output parameters and return the |
2079 | range of addresses covered by the entry used to fill them out. | |
2080 | Otherwise set * FILENAME_PTR to NULL and return 0. | |
2081 | The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR | |
2082 | are pointers to the objects to be filled in. */ | |
252b5132 | 2083 | |
240d6706 | 2084 | static bfd_vma |
818a27ac AM |
2085 | lookup_address_in_line_info_table (struct line_info_table *table, |
2086 | bfd_vma addr, | |
818a27ac | 2087 | const char **filename_ptr, |
9b8d1a36 CC |
2088 | unsigned int *linenumber_ptr, |
2089 | unsigned int *discriminator_ptr) | |
252b5132 | 2090 | { |
0ee19663 | 2091 | struct line_sequence *seq = NULL; |
107601c8 | 2092 | struct line_info *each_line; |
0ee19663 | 2093 | int low, high, mid; |
e82ce529 | 2094 | |
0ee19663 NC |
2095 | /* Binary search the array of sequences. */ |
2096 | low = 0; | |
2097 | high = table->num_sequences; | |
2098 | while (low < high) | |
2099 | { | |
2100 | mid = (low + high) / 2; | |
2101 | seq = &table->sequences[mid]; | |
2102 | if (addr < seq->low_pc) | |
2103 | high = mid; | |
2104 | else if (addr >= seq->last_line->address) | |
2105 | low = mid + 1; | |
2106 | else | |
2107 | break; | |
2108 | } | |
98591c73 | 2109 | |
0ee19663 | 2110 | if (seq && addr >= seq->low_pc && addr < seq->last_line->address) |
1ee24f27 | 2111 | { |
0ee19663 NC |
2112 | /* Note: seq->last_line should be a descendingly sorted list. */ |
2113 | for (each_line = seq->last_line; | |
93ee1e36 AM |
2114 | each_line; |
2115 | each_line = each_line->prev_line) | |
2116 | if (addr >= each_line->address) | |
2117 | break; | |
0ee19663 NC |
2118 | |
2119 | if (each_line | |
93ee1e36 AM |
2120 | && !(each_line->end_sequence || each_line == seq->last_line)) |
2121 | { | |
2122 | *filename_ptr = each_line->filename; | |
2123 | *linenumber_ptr = each_line->line; | |
2124 | if (discriminator_ptr) | |
2125 | *discriminator_ptr = each_line->discriminator; | |
2126 | return seq->last_line->address - seq->low_pc; | |
2127 | } | |
1ee24f27 DJ |
2128 | } |
2129 | ||
107601c8 | 2130 | *filename_ptr = NULL; |
240d6706 | 2131 | return 0; |
252b5132 | 2132 | } |
98591c73 | 2133 | |
0ee19663 | 2134 | /* Read in the .debug_ranges section for future reference. */ |
a13afe8e FF |
2135 | |
2136 | static bfd_boolean | |
2137 | read_debug_ranges (struct comp_unit *unit) | |
2138 | { | |
2139 | struct dwarf2_debug *stash = unit->stash; | |
fc28f9aa | 2140 | return read_section (unit->abfd, &stash->debug_sections[debug_ranges], |
93ee1e36 | 2141 | stash->syms, 0, |
9e32b19f | 2142 | &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size); |
a13afe8e FF |
2143 | } |
2144 | ||
a092b084 | 2145 | /* Function table functions. */ |
252b5132 | 2146 | |
e00e8198 | 2147 | /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return |
240d6706 NC |
2148 | TRUE. Note that we need to find the function that has the smallest range |
2149 | that contains ADDR, to handle inlined functions without depending upon | |
2150 | them being ordered in TABLE by increasing range. */ | |
252b5132 | 2151 | |
b34976b6 | 2152 | static bfd_boolean |
4ab527b0 | 2153 | lookup_address_in_function_table (struct comp_unit *unit, |
818a27ac | 2154 | bfd_vma addr, |
e00e8198 | 2155 | struct funcinfo **function_ptr) |
252b5132 RH |
2156 | { |
2157 | struct funcinfo* each_func; | |
a13afe8e | 2158 | struct funcinfo* best_fit = NULL; |
4ba3b326 | 2159 | bfd_vma best_fit_len = 0; |
a13afe8e | 2160 | struct arange *arange; |
252b5132 | 2161 | |
4ab527b0 | 2162 | for (each_func = unit->function_table; |
252b5132 RH |
2163 | each_func; |
2164 | each_func = each_func->prev_func) | |
2165 | { | |
a13afe8e FF |
2166 | for (arange = &each_func->arange; |
2167 | arange; | |
2168 | arange = arange->next) | |
252b5132 | 2169 | { |
a13afe8e FF |
2170 | if (addr >= arange->low && addr < arange->high) |
2171 | { | |
a2a50954 | 2172 | if (!best_fit |
4ba3b326 TG |
2173 | || arange->high - arange->low < best_fit_len) |
2174 | { | |
2175 | best_fit = each_func; | |
2176 | best_fit_len = arange->high - arange->low; | |
2177 | } | |
a13afe8e | 2178 | } |
252b5132 RH |
2179 | } |
2180 | } | |
98591c73 | 2181 | |
a13afe8e FF |
2182 | if (best_fit) |
2183 | { | |
a13afe8e FF |
2184 | *function_ptr = best_fit; |
2185 | return TRUE; | |
2186 | } | |
e00e8198 | 2187 | return FALSE; |
252b5132 RH |
2188 | } |
2189 | ||
5420f73d L |
2190 | /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR |
2191 | and LINENUMBER_PTR, and return TRUE. */ | |
2192 | ||
2193 | static bfd_boolean | |
2194 | lookup_symbol_in_function_table (struct comp_unit *unit, | |
2195 | asymbol *sym, | |
2196 | bfd_vma addr, | |
2197 | const char **filename_ptr, | |
2198 | unsigned int *linenumber_ptr) | |
2199 | { | |
2200 | struct funcinfo* each_func; | |
2201 | struct funcinfo* best_fit = NULL; | |
4ba3b326 | 2202 | bfd_vma best_fit_len = 0; |
5420f73d L |
2203 | struct arange *arange; |
2204 | const char *name = bfd_asymbol_name (sym); | |
2205 | asection *sec = bfd_get_section (sym); | |
2206 | ||
2207 | for (each_func = unit->function_table; | |
2208 | each_func; | |
2209 | each_func = each_func->prev_func) | |
2210 | { | |
2211 | for (arange = &each_func->arange; | |
2212 | arange; | |
2213 | arange = arange->next) | |
2214 | { | |
2215 | if ((!each_func->sec || each_func->sec == sec) | |
2216 | && addr >= arange->low | |
2217 | && addr < arange->high | |
650f284e | 2218 | && each_func->name |
5420f73d L |
2219 | && strcmp (name, each_func->name) == 0 |
2220 | && (!best_fit | |
4ba3b326 TG |
2221 | || arange->high - arange->low < best_fit_len)) |
2222 | { | |
2223 | best_fit = each_func; | |
2224 | best_fit_len = arange->high - arange->low; | |
2225 | } | |
5420f73d L |
2226 | } |
2227 | } | |
2228 | ||
2229 | if (best_fit) | |
2230 | { | |
2231 | best_fit->sec = sec; | |
2232 | *filename_ptr = best_fit->file; | |
2233 | *linenumber_ptr = best_fit->line; | |
2234 | return TRUE; | |
2235 | } | |
2236 | else | |
2237 | return FALSE; | |
2238 | } | |
2239 | ||
2240 | /* Variable table functions. */ | |
2241 | ||
2242 | /* If SYM is within variable table of UNIT, set FILENAME_PTR and | |
2243 | LINENUMBER_PTR, and return TRUE. */ | |
2244 | ||
2245 | static bfd_boolean | |
2246 | lookup_symbol_in_variable_table (struct comp_unit *unit, | |
2247 | asymbol *sym, | |
5cf2e3f0 | 2248 | bfd_vma addr, |
5420f73d L |
2249 | const char **filename_ptr, |
2250 | unsigned int *linenumber_ptr) | |
2251 | { | |
2252 | const char *name = bfd_asymbol_name (sym); | |
2253 | asection *sec = bfd_get_section (sym); | |
2254 | struct varinfo* each; | |
2255 | ||
2256 | for (each = unit->variable_table; each; each = each->prev_var) | |
2257 | if (each->stack == 0 | |
5cf2e3f0 L |
2258 | && each->file != NULL |
2259 | && each->name != NULL | |
2260 | && each->addr == addr | |
5420f73d L |
2261 | && (!each->sec || each->sec == sec) |
2262 | && strcmp (name, each->name) == 0) | |
2263 | break; | |
2264 | ||
2265 | if (each) | |
2266 | { | |
2267 | each->sec = sec; | |
2268 | *filename_ptr = each->file; | |
2269 | *linenumber_ptr = each->line; | |
2270 | return TRUE; | |
2271 | } | |
2272 | else | |
2273 | return FALSE; | |
2274 | } | |
2275 | ||
06f22d7e | 2276 | static char * |
5609a71e | 2277 | find_abstract_instance_name (struct comp_unit *unit, |
e00e8198 AM |
2278 | struct attribute *attr_ptr, |
2279 | bfd_boolean *is_linkage) | |
06f22d7e FF |
2280 | { |
2281 | bfd *abfd = unit->abfd; | |
f075ee0c | 2282 | bfd_byte *info_ptr; |
dbb3fbbb | 2283 | bfd_byte *info_ptr_end; |
06f22d7e FF |
2284 | unsigned int abbrev_number, bytes_read, i; |
2285 | struct abbrev_info *abbrev; | |
5609a71e | 2286 | bfd_uint64_t die_ref = attr_ptr->u.val; |
06f22d7e | 2287 | struct attribute attr; |
95e34fb4 | 2288 | char *name = NULL; |
06f22d7e | 2289 | |
5609a71e DJ |
2290 | /* DW_FORM_ref_addr can reference an entry in a different CU. It |
2291 | is an offset from the .debug_info section, not the current CU. */ | |
2292 | if (attr_ptr->form == DW_FORM_ref_addr) | |
2293 | { | |
2294 | /* We only support DW_FORM_ref_addr within the same file, so | |
2295 | any relocations should be resolved already. */ | |
2296 | if (!die_ref) | |
2297 | abort (); | |
2298 | ||
a358ecb8 | 2299 | info_ptr = unit->sec_info_ptr + die_ref; |
dbb3fbbb | 2300 | info_ptr_end = unit->end_ptr; |
0a9c7b2b NC |
2301 | |
2302 | /* Now find the CU containing this pointer. */ | |
2303 | if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr) | |
2304 | ; | |
2305 | else | |
2306 | { | |
2307 | /* Check other CUs to see if they contain the abbrev. */ | |
2308 | struct comp_unit * u; | |
2309 | ||
2310 | for (u = unit->prev_unit; u != NULL; u = u->prev_unit) | |
2311 | if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr) | |
2312 | break; | |
2313 | ||
2314 | if (u == NULL) | |
2315 | for (u = unit->next_unit; u != NULL; u = u->next_unit) | |
2316 | if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr) | |
2317 | break; | |
2318 | ||
2319 | if (u) | |
2320 | unit = u; | |
2321 | /* else FIXME: What do we do now ? */ | |
2322 | } | |
5609a71e | 2323 | } |
95e34fb4 NC |
2324 | else if (attr_ptr->form == DW_FORM_GNU_ref_alt) |
2325 | { | |
2326 | info_ptr = read_alt_indirect_ref (unit, die_ref); | |
2327 | if (info_ptr == NULL) | |
2328 | { | |
2329 | (*_bfd_error_handler) | |
2330 | (_("Dwarf Error: Unable to read alt ref %u."), die_ref); | |
2331 | bfd_set_error (bfd_error_bad_value); | |
e00e8198 | 2332 | return NULL; |
95e34fb4 | 2333 | } |
dbb3fbbb NC |
2334 | info_ptr_end = unit->stash->alt_dwarf_info_buffer + unit->stash->alt_dwarf_info_size; |
2335 | ||
0a9c7b2b NC |
2336 | /* FIXME: Do we need to locate the correct CU, in a similar |
2337 | fashion to the code in the DW_FORM_ref_addr case above ? */ | |
95e34fb4 | 2338 | } |
68ffbac6 | 2339 | else |
dbb3fbbb NC |
2340 | { |
2341 | info_ptr = unit->info_ptr_unit + die_ref; | |
2342 | info_ptr_end = unit->end_ptr; | |
2343 | } | |
95e34fb4 | 2344 | |
dbb3fbbb | 2345 | abbrev_number = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end); |
06f22d7e FF |
2346 | info_ptr += bytes_read; |
2347 | ||
2348 | if (abbrev_number) | |
2349 | { | |
2350 | abbrev = lookup_abbrev (abbrev_number, unit->abbrevs); | |
2351 | if (! abbrev) | |
2352 | { | |
a2a50954 AM |
2353 | (*_bfd_error_handler) |
2354 | (_("Dwarf Error: Could not find abbrev number %u."), abbrev_number); | |
06f22d7e FF |
2355 | bfd_set_error (bfd_error_bad_value); |
2356 | } | |
2357 | else | |
2358 | { | |
d5cbaa15 | 2359 | for (i = 0; i < abbrev->num_attrs; ++i) |
06f22d7e | 2360 | { |
8af6b354 | 2361 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, |
dbb3fbbb | 2362 | info_ptr, info_ptr_end); |
8af6b354 AM |
2363 | if (info_ptr == NULL) |
2364 | break; | |
26bf4e33 FF |
2365 | switch (attr.name) |
2366 | { | |
2367 | case DW_AT_name: | |
643be349 JJ |
2368 | /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name |
2369 | over DW_AT_name. */ | |
60d77146 | 2370 | if (name == NULL && is_str_attr (attr.form)) |
e00e8198 AM |
2371 | { |
2372 | name = attr.u.str; | |
2373 | if (non_mangled (unit->lang)) | |
2374 | *is_linkage = TRUE; | |
2375 | } | |
26bf4e33 FF |
2376 | break; |
2377 | case DW_AT_specification: | |
e00e8198 | 2378 | name = find_abstract_instance_name (unit, &attr, is_linkage); |
26bf4e33 | 2379 | break; |
643be349 | 2380 | case DW_AT_linkage_name: |
d5cbaa15 | 2381 | case DW_AT_MIPS_linkage_name: |
60d77146 NC |
2382 | /* PR 16949: Corrupt debug info can place |
2383 | non-string forms into these attributes. */ | |
6d74e8a1 | 2384 | if (is_str_attr (attr.form)) |
e00e8198 AM |
2385 | { |
2386 | name = attr.u.str; | |
2387 | *is_linkage = TRUE; | |
2388 | } | |
d5cbaa15 | 2389 | break; |
26bf4e33 FF |
2390 | default: |
2391 | break; | |
2392 | } | |
06f22d7e FF |
2393 | } |
2394 | } | |
2395 | } | |
8af6b354 | 2396 | return name; |
06f22d7e FF |
2397 | } |
2398 | ||
8af6b354 AM |
2399 | static bfd_boolean |
2400 | read_rangelist (struct comp_unit *unit, struct arange *arange, | |
2401 | bfd_uint64_t offset) | |
a13afe8e FF |
2402 | { |
2403 | bfd_byte *ranges_ptr; | |
dbb3fbbb | 2404 | bfd_byte *ranges_end; |
a13afe8e FF |
2405 | bfd_vma base_address = unit->base_address; |
2406 | ||
2407 | if (! unit->stash->dwarf_ranges_buffer) | |
2408 | { | |
2409 | if (! read_debug_ranges (unit)) | |
8af6b354 | 2410 | return FALSE; |
a13afe8e | 2411 | } |
d8d1c398 | 2412 | |
dbb3fbbb NC |
2413 | ranges_ptr = unit->stash->dwarf_ranges_buffer + offset; |
2414 | if (ranges_ptr < unit->stash->dwarf_ranges_buffer) | |
2415 | return FALSE; | |
2416 | ranges_end = unit->stash->dwarf_ranges_buffer + unit->stash->dwarf_ranges_size; | |
62f8d217 | 2417 | |
a13afe8e FF |
2418 | for (;;) |
2419 | { | |
2420 | bfd_vma low_pc; | |
2421 | bfd_vma high_pc; | |
2422 | ||
dbb3fbbb | 2423 | /* PR 17512: file: 62cada7d. */ |
62f8d217 | 2424 | if (ranges_ptr + 2 * unit->addr_size > ranges_end) |
dbb3fbbb NC |
2425 | return FALSE; |
2426 | ||
2427 | low_pc = read_address (unit, ranges_ptr, ranges_end); | |
13d72a14 | 2428 | ranges_ptr += unit->addr_size; |
dbb3fbbb | 2429 | high_pc = read_address (unit, ranges_ptr, ranges_end); |
13d72a14 AN |
2430 | ranges_ptr += unit->addr_size; |
2431 | ||
a13afe8e FF |
2432 | if (low_pc == 0 && high_pc == 0) |
2433 | break; | |
2434 | if (low_pc == -1UL && high_pc != -1UL) | |
2435 | base_address = high_pc; | |
2436 | else | |
8af6b354 | 2437 | { |
a2a50954 | 2438 | if (!arange_add (unit, arange, |
8af6b354 AM |
2439 | base_address + low_pc, base_address + high_pc)) |
2440 | return FALSE; | |
2441 | } | |
a13afe8e | 2442 | } |
8af6b354 | 2443 | return TRUE; |
a13afe8e FF |
2444 | } |
2445 | ||
a092b084 | 2446 | /* DWARF2 Compilation unit functions. */ |
252b5132 RH |
2447 | |
2448 | /* Scan over each die in a comp. unit looking for functions to add | |
34b5e0b2 | 2449 | to the function table and variables to the variable table. */ |
252b5132 | 2450 | |
b34976b6 | 2451 | static bfd_boolean |
5420f73d | 2452 | scan_unit_for_symbols (struct comp_unit *unit) |
252b5132 RH |
2453 | { |
2454 | bfd *abfd = unit->abfd; | |
f075ee0c | 2455 | bfd_byte *info_ptr = unit->first_child_die_ptr; |
dbb3fbbb | 2456 | bfd_byte *info_ptr_end = unit->stash->info_ptr_end; |
252b5132 | 2457 | int nesting_level = 1; |
c955f9cd JW |
2458 | struct funcinfo **nested_funcs; |
2459 | int nested_funcs_size; | |
2460 | ||
2461 | /* Maintain a stack of in-scope functions and inlined functions, which we | |
2462 | can use to set the caller_func field. */ | |
2463 | nested_funcs_size = 32; | |
a50b1753 | 2464 | nested_funcs = (struct funcinfo **) |
a2a50954 | 2465 | bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *)); |
c955f9cd JW |
2466 | if (nested_funcs == NULL) |
2467 | return FALSE; | |
34b5e0b2 | 2468 | nested_funcs[nesting_level] = 0; |
252b5132 RH |
2469 | |
2470 | while (nesting_level) | |
2471 | { | |
2472 | unsigned int abbrev_number, bytes_read, i; | |
2473 | struct abbrev_info *abbrev; | |
2474 | struct attribute attr; | |
2475 | struct funcinfo *func; | |
5420f73d | 2476 | struct varinfo *var; |
a13afe8e FF |
2477 | bfd_vma low_pc = 0; |
2478 | bfd_vma high_pc = 0; | |
c49ead2f | 2479 | bfd_boolean high_pc_relative = FALSE; |
252b5132 | 2480 | |
877a8638 | 2481 | /* PR 17512: file: 9f405d9d. */ |
dbb3fbbb | 2482 | if (info_ptr >= info_ptr_end) |
877a8638 | 2483 | goto fail; |
62f8d217 | 2484 | |
dbb3fbbb | 2485 | abbrev_number = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end); |
252b5132 RH |
2486 | info_ptr += bytes_read; |
2487 | ||
2488 | if (! abbrev_number) | |
2489 | { | |
2490 | nesting_level--; | |
2491 | continue; | |
2492 | } | |
98591c73 | 2493 | |
252b5132 RH |
2494 | abbrev = lookup_abbrev (abbrev_number,unit->abbrevs); |
2495 | if (! abbrev) | |
2496 | { | |
8af6b354 AM |
2497 | (*_bfd_error_handler) |
2498 | (_("Dwarf Error: Could not find abbrev number %u."), | |
2499 | abbrev_number); | |
252b5132 | 2500 | bfd_set_error (bfd_error_bad_value); |
8af6b354 | 2501 | goto fail; |
252b5132 | 2502 | } |
98591c73 | 2503 | |
5420f73d | 2504 | var = NULL; |
06f22d7e | 2505 | if (abbrev->tag == DW_TAG_subprogram |
5420f73d | 2506 | || abbrev->tag == DW_TAG_entry_point |
06f22d7e | 2507 | || abbrev->tag == DW_TAG_inlined_subroutine) |
252b5132 | 2508 | { |
dc810e39 | 2509 | bfd_size_type amt = sizeof (struct funcinfo); |
a50b1753 | 2510 | func = (struct funcinfo *) bfd_zalloc (abfd, amt); |
8af6b354 AM |
2511 | if (func == NULL) |
2512 | goto fail; | |
4ab527b0 | 2513 | func->tag = abbrev->tag; |
252b5132 RH |
2514 | func->prev_func = unit->function_table; |
2515 | unit->function_table = func; | |
bd210d54 | 2516 | BFD_ASSERT (!unit->cached); |
c955f9cd JW |
2517 | |
2518 | if (func->tag == DW_TAG_inlined_subroutine) | |
2519 | for (i = nesting_level - 1; i >= 1; i--) | |
2520 | if (nested_funcs[i]) | |
2521 | { | |
2522 | func->caller_func = nested_funcs[i]; | |
2523 | break; | |
2524 | } | |
2525 | nested_funcs[nesting_level] = func; | |
252b5132 RH |
2526 | } |
2527 | else | |
5420f73d L |
2528 | { |
2529 | func = NULL; | |
2530 | if (abbrev->tag == DW_TAG_variable) | |
2531 | { | |
2532 | bfd_size_type amt = sizeof (struct varinfo); | |
a50b1753 | 2533 | var = (struct varinfo *) bfd_zalloc (abfd, amt); |
8af6b354 AM |
2534 | if (var == NULL) |
2535 | goto fail; | |
5420f73d L |
2536 | var->tag = abbrev->tag; |
2537 | var->stack = 1; | |
2538 | var->prev_var = unit->variable_table; | |
2539 | unit->variable_table = var; | |
bd210d54 | 2540 | BFD_ASSERT (!unit->cached); |
5420f73d | 2541 | } |
c955f9cd JW |
2542 | |
2543 | /* No inline function in scope at this nesting level. */ | |
2544 | nested_funcs[nesting_level] = 0; | |
5420f73d | 2545 | } |
98591c73 | 2546 | |
252b5132 RH |
2547 | for (i = 0; i < abbrev->num_attrs; ++i) |
2548 | { | |
dbb3fbbb | 2549 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, info_ptr_end); |
8af6b354 | 2550 | if (info_ptr == NULL) |
8ecc1f20 | 2551 | goto fail; |
98591c73 | 2552 | |
252b5132 RH |
2553 | if (func) |
2554 | { | |
2555 | switch (attr.name) | |
2556 | { | |
4ab527b0 | 2557 | case DW_AT_call_file: |
8af6b354 AM |
2558 | func->caller_file = concat_filename (unit->line_table, |
2559 | attr.u.val); | |
4ab527b0 FF |
2560 | break; |
2561 | ||
2562 | case DW_AT_call_line: | |
2563 | func->caller_line = attr.u.val; | |
2564 | break; | |
2565 | ||
06f22d7e | 2566 | case DW_AT_abstract_origin: |
5d8e6b4d | 2567 | case DW_AT_specification: |
e00e8198 AM |
2568 | func->name = find_abstract_instance_name (unit, &attr, |
2569 | &func->is_linkage); | |
06f22d7e FF |
2570 | break; |
2571 | ||
252b5132 | 2572 | case DW_AT_name: |
643be349 JJ |
2573 | /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name |
2574 | over DW_AT_name. */ | |
60d77146 | 2575 | if (func->name == NULL && is_str_attr (attr.form)) |
e00e8198 AM |
2576 | { |
2577 | func->name = attr.u.str; | |
2578 | if (non_mangled (unit->lang)) | |
2579 | func->is_linkage = TRUE; | |
2580 | } | |
252b5132 | 2581 | break; |
98591c73 | 2582 | |
643be349 | 2583 | case DW_AT_linkage_name: |
252b5132 | 2584 | case DW_AT_MIPS_linkage_name: |
60d77146 NC |
2585 | /* PR 16949: Corrupt debug info can place |
2586 | non-string forms into these attributes. */ | |
2587 | if (is_str_attr (attr.form)) | |
e00e8198 AM |
2588 | { |
2589 | func->name = attr.u.str; | |
2590 | func->is_linkage = TRUE; | |
2591 | } | |
252b5132 RH |
2592 | break; |
2593 | ||
2594 | case DW_AT_low_pc: | |
a13afe8e | 2595 | low_pc = attr.u.val; |
252b5132 RH |
2596 | break; |
2597 | ||
2598 | case DW_AT_high_pc: | |
a13afe8e | 2599 | high_pc = attr.u.val; |
c49ead2f | 2600 | high_pc_relative = attr.form != DW_FORM_addr; |
a13afe8e FF |
2601 | break; |
2602 | ||
2603 | case DW_AT_ranges: | |
8af6b354 AM |
2604 | if (!read_rangelist (unit, &func->arange, attr.u.val)) |
2605 | goto fail; | |
252b5132 RH |
2606 | break; |
2607 | ||
5420f73d L |
2608 | case DW_AT_decl_file: |
2609 | func->file = concat_filename (unit->line_table, | |
2610 | attr.u.val); | |
2611 | break; | |
2612 | ||
2613 | case DW_AT_decl_line: | |
2614 | func->line = attr.u.val; | |
2615 | break; | |
2616 | ||
2617 | default: | |
2618 | break; | |
2619 | } | |
2620 | } | |
2621 | else if (var) | |
2622 | { | |
2623 | switch (attr.name) | |
2624 | { | |
2625 | case DW_AT_name: | |
2626 | var->name = attr.u.str; | |
2627 | break; | |
2628 | ||
2629 | case DW_AT_decl_file: | |
2630 | var->file = concat_filename (unit->line_table, | |
2631 | attr.u.val); | |
2632 | break; | |
2633 | ||
2634 | case DW_AT_decl_line: | |
2635 | var->line = attr.u.val; | |
2636 | break; | |
2637 | ||
2638 | case DW_AT_external: | |
2639 | if (attr.u.val != 0) | |
2640 | var->stack = 0; | |
2641 | break; | |
2642 | ||
2643 | case DW_AT_location: | |
5cf2e3f0 | 2644 | switch (attr.form) |
5420f73d | 2645 | { |
5cf2e3f0 L |
2646 | case DW_FORM_block: |
2647 | case DW_FORM_block1: | |
2648 | case DW_FORM_block2: | |
2649 | case DW_FORM_block4: | |
c07cbdd7 | 2650 | case DW_FORM_exprloc: |
5cf2e3f0 | 2651 | if (*attr.u.blk->data == DW_OP_addr) |
5420f73d | 2652 | { |
5cf2e3f0 | 2653 | var->stack = 0; |
98b880f4 JW |
2654 | |
2655 | /* Verify that DW_OP_addr is the only opcode in the | |
2656 | location, in which case the block size will be 1 | |
2657 | plus the address size. */ | |
2658 | /* ??? For TLS variables, gcc can emit | |
2659 | DW_OP_addr <addr> DW_OP_GNU_push_tls_address | |
2660 | which we don't handle here yet. */ | |
2661 | if (attr.u.blk->size == unit->addr_size + 1U) | |
2662 | var->addr = bfd_get (unit->addr_size * 8, | |
2663 | unit->abfd, | |
2664 | attr.u.blk->data + 1); | |
5420f73d | 2665 | } |
5cf2e3f0 | 2666 | break; |
d8d1c398 | 2667 | |
5cf2e3f0 L |
2668 | default: |
2669 | break; | |
5420f73d L |
2670 | } |
2671 | break; | |
2672 | ||
252b5132 RH |
2673 | default: |
2674 | break; | |
2675 | } | |
2676 | } | |
2677 | } | |
2678 | ||
c49ead2f MW |
2679 | if (high_pc_relative) |
2680 | high_pc += low_pc; | |
2681 | ||
a13afe8e FF |
2682 | if (func && high_pc != 0) |
2683 | { | |
a2a50954 | 2684 | if (!arange_add (unit, &func->arange, low_pc, high_pc)) |
8af6b354 | 2685 | goto fail; |
a13afe8e FF |
2686 | } |
2687 | ||
252b5132 | 2688 | if (abbrev->has_children) |
c955f9cd JW |
2689 | { |
2690 | nesting_level++; | |
2691 | ||
2692 | if (nesting_level >= nested_funcs_size) | |
2693 | { | |
2694 | struct funcinfo **tmp; | |
2695 | ||
2696 | nested_funcs_size *= 2; | |
a50b1753 | 2697 | tmp = (struct funcinfo **) |
a2a50954 AM |
2698 | bfd_realloc (nested_funcs, |
2699 | nested_funcs_size * sizeof (struct funcinfo *)); | |
c955f9cd | 2700 | if (tmp == NULL) |
8af6b354 | 2701 | goto fail; |
c955f9cd JW |
2702 | nested_funcs = tmp; |
2703 | } | |
2704 | nested_funcs[nesting_level] = 0; | |
2705 | } | |
252b5132 RH |
2706 | } |
2707 | ||
c955f9cd | 2708 | free (nested_funcs); |
b34976b6 | 2709 | return TRUE; |
8af6b354 AM |
2710 | |
2711 | fail: | |
2712 | free (nested_funcs); | |
2713 | return FALSE; | |
252b5132 RH |
2714 | } |
2715 | ||
5e38c3b8 MM |
2716 | /* Parse a DWARF2 compilation unit starting at INFO_PTR. This |
2717 | includes the compilation unit header that proceeds the DIE's, but | |
5c4491d3 | 2718 | does not include the length field that precedes each compilation |
5e38c3b8 | 2719 | unit header. END_PTR points one past the end of this comp unit. |
d03ba2a1 | 2720 | OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes). |
252b5132 RH |
2721 | |
2722 | This routine does not read the whole compilation unit; only enough | |
2723 | to get to the line number information for the compilation unit. */ | |
2724 | ||
2725 | static struct comp_unit * | |
0d161102 | 2726 | parse_comp_unit (struct dwarf2_debug *stash, |
818a27ac | 2727 | bfd_vma unit_length, |
f075ee0c | 2728 | bfd_byte *info_ptr_unit, |
818a27ac | 2729 | unsigned int offset_size) |
252b5132 RH |
2730 | { |
2731 | struct comp_unit* unit; | |
f46c2da6 | 2732 | unsigned int version; |
8ce8c090 | 2733 | bfd_uint64_t abbrev_offset = 0; |
f46c2da6 | 2734 | unsigned int addr_size; |
252b5132 | 2735 | struct abbrev_info** abbrevs; |
252b5132 RH |
2736 | unsigned int abbrev_number, bytes_read, i; |
2737 | struct abbrev_info *abbrev; | |
2738 | struct attribute attr; | |
f075ee0c AM |
2739 | bfd_byte *info_ptr = stash->info_ptr; |
2740 | bfd_byte *end_ptr = info_ptr + unit_length; | |
dc810e39 | 2741 | bfd_size_type amt; |
a13afe8e FF |
2742 | bfd_vma low_pc = 0; |
2743 | bfd_vma high_pc = 0; | |
a50b1753 | 2744 | bfd *abfd = stash->bfd_ptr; |
c49ead2f | 2745 | bfd_boolean high_pc_relative = FALSE; |
3fde5a36 | 2746 | |
dbb3fbbb | 2747 | version = read_2_bytes (abfd, info_ptr, end_ptr); |
252b5132 | 2748 | info_ptr += 2; |
d03ba2a1 JJ |
2749 | BFD_ASSERT (offset_size == 4 || offset_size == 8); |
2750 | if (offset_size == 4) | |
dbb3fbbb | 2751 | abbrev_offset = read_4_bytes (abfd, info_ptr, end_ptr); |
d03ba2a1 | 2752 | else |
dbb3fbbb | 2753 | abbrev_offset = read_8_bytes (abfd, info_ptr, end_ptr); |
d03ba2a1 | 2754 | info_ptr += offset_size; |
dbb3fbbb | 2755 | addr_size = read_1_byte (abfd, info_ptr, end_ptr); |
252b5132 RH |
2756 | info_ptr += 1; |
2757 | ||
c07cbdd7 | 2758 | if (version != 2 && version != 3 && version != 4) |
252b5132 | 2759 | { |
a2a50954 AM |
2760 | (*_bfd_error_handler) |
2761 | (_("Dwarf Error: found dwarf version '%u', this reader" | |
2762 | " only handles version 2, 3 and 4 information."), version); | |
252b5132 RH |
2763 | bfd_set_error (bfd_error_bad_value); |
2764 | return 0; | |
2765 | } | |
2766 | ||
2767 | if (addr_size > sizeof (bfd_vma)) | |
2768 | { | |
a2a50954 AM |
2769 | (*_bfd_error_handler) |
2770 | (_("Dwarf Error: found address size '%u', this reader" | |
2771 | " can not handle sizes greater than '%u'."), | |
2772 | addr_size, | |
2773 | (unsigned int) sizeof (bfd_vma)); | |
252b5132 RH |
2774 | bfd_set_error (bfd_error_bad_value); |
2775 | return 0; | |
2776 | } | |
2777 | ||
ecb651f0 | 2778 | if (addr_size != 2 && addr_size != 4 && addr_size != 8) |
252b5132 | 2779 | { |
a2a50954 AM |
2780 | (*_bfd_error_handler) |
2781 | ("Dwarf Error: found address size '%u', this reader" | |
2782 | " can only handle address sizes '2', '4' and '8'.", addr_size); | |
252b5132 RH |
2783 | bfd_set_error (bfd_error_bad_value); |
2784 | return 0; | |
2785 | } | |
2786 | ||
a092b084 | 2787 | /* Read the abbrevs for this compilation unit into a table. */ |
51db3708 | 2788 | abbrevs = read_abbrevs (abfd, abbrev_offset, stash); |
252b5132 | 2789 | if (! abbrevs) |
a2a50954 | 2790 | return 0; |
252b5132 | 2791 | |
dbb3fbbb | 2792 | abbrev_number = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, end_ptr); |
252b5132 RH |
2793 | info_ptr += bytes_read; |
2794 | if (! abbrev_number) | |
2795 | { | |
f46c2da6 | 2796 | (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."), |
a2a50954 | 2797 | abbrev_number); |
252b5132 RH |
2798 | bfd_set_error (bfd_error_bad_value); |
2799 | return 0; | |
2800 | } | |
2801 | ||
2802 | abbrev = lookup_abbrev (abbrev_number, abbrevs); | |
2803 | if (! abbrev) | |
2804 | { | |
f46c2da6 | 2805 | (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), |
a2a50954 | 2806 | abbrev_number); |
252b5132 RH |
2807 | bfd_set_error (bfd_error_bad_value); |
2808 | return 0; | |
2809 | } | |
98591c73 | 2810 | |
dc810e39 | 2811 | amt = sizeof (struct comp_unit); |
a50b1753 | 2812 | unit = (struct comp_unit *) bfd_zalloc (abfd, amt); |
8af6b354 AM |
2813 | if (unit == NULL) |
2814 | return NULL; | |
252b5132 | 2815 | unit->abfd = abfd; |
5609a71e | 2816 | unit->version = version; |
98591c73 | 2817 | unit->addr_size = addr_size; |
d03ba2a1 | 2818 | unit->offset_size = offset_size; |
252b5132 RH |
2819 | unit->abbrevs = abbrevs; |
2820 | unit->end_ptr = end_ptr; | |
d03ba2a1 | 2821 | unit->stash = stash; |
c0c28ab8 | 2822 | unit->info_ptr_unit = info_ptr_unit; |
a358ecb8 | 2823 | unit->sec_info_ptr = stash->sec_info_ptr; |
252b5132 RH |
2824 | |
2825 | for (i = 0; i < abbrev->num_attrs; ++i) | |
2826 | { | |
dbb3fbbb | 2827 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr); |
8af6b354 AM |
2828 | if (info_ptr == NULL) |
2829 | return NULL; | |
252b5132 RH |
2830 | |
2831 | /* Store the data if it is of an attribute we want to keep in a | |
2832 | partial symbol table. */ | |
2833 | switch (attr.name) | |
2834 | { | |
2835 | case DW_AT_stmt_list: | |
2836 | unit->stmtlist = 1; | |
482e2e37 | 2837 | unit->line_offset = attr.u.val; |
252b5132 RH |
2838 | break; |
2839 | ||
2840 | case DW_AT_name: | |
482e2e37 | 2841 | unit->name = attr.u.str; |
252b5132 RH |
2842 | break; |
2843 | ||
2844 | case DW_AT_low_pc: | |
a13afe8e FF |
2845 | low_pc = attr.u.val; |
2846 | /* If the compilation unit DIE has a DW_AT_low_pc attribute, | |
2847 | this is the base address to use when reading location | |
2848 | lists or range lists. */ | |
a2a50954 AM |
2849 | if (abbrev->tag == DW_TAG_compile_unit) |
2850 | unit->base_address = low_pc; | |
252b5132 RH |
2851 | break; |
2852 | ||
2853 | case DW_AT_high_pc: | |
a13afe8e | 2854 | high_pc = attr.u.val; |
c49ead2f | 2855 | high_pc_relative = attr.form != DW_FORM_addr; |
a13afe8e FF |
2856 | break; |
2857 | ||
2858 | case DW_AT_ranges: | |
8af6b354 AM |
2859 | if (!read_rangelist (unit, &unit->arange, attr.u.val)) |
2860 | return NULL; | |
252b5132 RH |
2861 | break; |
2862 | ||
2863 | case DW_AT_comp_dir: | |
2864 | { | |
f075ee0c | 2865 | char *comp_dir = attr.u.str; |
877a8638 NC |
2866 | |
2867 | /* PR 17512: file: 1fe726be. */ | |
2868 | if (! is_str_attr (attr.form)) | |
2869 | { | |
2870 | (*_bfd_error_handler) | |
2871 | (_("Dwarf Error: DW_AT_comp_dir attribute encountered with a non-string form.")); | |
2872 | comp_dir = NULL; | |
2873 | } | |
2874 | ||
252b5132 RH |
2875 | if (comp_dir) |
2876 | { | |
2877 | /* Irix 6.2 native cc prepends <machine>.: to the compilation | |
2878 | directory, get rid of it. */ | |
818a27ac | 2879 | char *cp = strchr (comp_dir, ':'); |
252b5132 RH |
2880 | |
2881 | if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/') | |
2882 | comp_dir = cp + 1; | |
2883 | } | |
2884 | unit->comp_dir = comp_dir; | |
2885 | break; | |
2886 | } | |
2887 | ||
e00e8198 AM |
2888 | case DW_AT_language: |
2889 | unit->lang = attr.u.val; | |
2890 | break; | |
2891 | ||
252b5132 RH |
2892 | default: |
2893 | break; | |
2894 | } | |
2895 | } | |
c49ead2f MW |
2896 | if (high_pc_relative) |
2897 | high_pc += low_pc; | |
a13afe8e | 2898 | if (high_pc != 0) |
709d67f1 | 2899 | { |
a2a50954 | 2900 | if (!arange_add (unit, &unit->arange, low_pc, high_pc)) |
8af6b354 | 2901 | return NULL; |
709d67f1 | 2902 | } |
252b5132 RH |
2903 | |
2904 | unit->first_child_die_ptr = info_ptr; | |
2905 | return unit; | |
2906 | } | |
2907 | ||
6dd55cb7 L |
2908 | /* Return TRUE if UNIT may contain the address given by ADDR. When |
2909 | there are functions written entirely with inline asm statements, the | |
2910 | range info in the compilation unit header may not be correct. We | |
2911 | need to consult the line info table to see if a compilation unit | |
2912 | really contains the given address. */ | |
252b5132 | 2913 | |
b34976b6 | 2914 | static bfd_boolean |
818a27ac | 2915 | comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr) |
252b5132 | 2916 | { |
709d67f1 AM |
2917 | struct arange *arange; |
2918 | ||
2919 | if (unit->error) | |
2920 | return FALSE; | |
2921 | ||
2922 | arange = &unit->arange; | |
2923 | do | |
2924 | { | |
2925 | if (addr >= arange->low && addr < arange->high) | |
2926 | return TRUE; | |
2927 | arange = arange->next; | |
2928 | } | |
2929 | while (arange); | |
2930 | ||
2931 | return FALSE; | |
252b5132 RH |
2932 | } |
2933 | ||
252b5132 RH |
2934 | /* If UNIT contains ADDR, set the output parameters to the values for |
2935 | the line containing ADDR. The output parameters, FILENAME_PTR, | |
e00e8198 | 2936 | FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects |
98591c73 | 2937 | to be filled in. |
252b5132 | 2938 | |
240d6706 NC |
2939 | Returns the range of addresses covered by the entry that was used |
2940 | to fill in *LINENUMBER_PTR or 0 if it was not filled in. */ | |
252b5132 | 2941 | |
240d6706 | 2942 | static bfd_vma |
818a27ac AM |
2943 | comp_unit_find_nearest_line (struct comp_unit *unit, |
2944 | bfd_vma addr, | |
2945 | const char **filename_ptr, | |
e00e8198 | 2946 | struct funcinfo **function_ptr, |
818a27ac | 2947 | unsigned int *linenumber_ptr, |
9b8d1a36 | 2948 | unsigned int *discriminator_ptr, |
818a27ac | 2949 | struct dwarf2_debug *stash) |
252b5132 | 2950 | { |
b34976b6 | 2951 | bfd_boolean func_p; |
98591c73 | 2952 | |
252b5132 | 2953 | if (unit->error) |
b34976b6 | 2954 | return FALSE; |
252b5132 RH |
2955 | |
2956 | if (! unit->line_table) | |
2957 | { | |
2958 | if (! unit->stmtlist) | |
2959 | { | |
2960 | unit->error = 1; | |
b34976b6 | 2961 | return FALSE; |
252b5132 | 2962 | } |
98591c73 | 2963 | |
51db3708 | 2964 | unit->line_table = decode_line_info (unit, stash); |
252b5132 RH |
2965 | |
2966 | if (! unit->line_table) | |
2967 | { | |
2968 | unit->error = 1; | |
b34976b6 | 2969 | return FALSE; |
252b5132 | 2970 | } |
98591c73 | 2971 | |
3f5864e1 | 2972 | if (unit->first_child_die_ptr < unit->end_ptr |
5420f73d | 2973 | && ! scan_unit_for_symbols (unit)) |
252b5132 RH |
2974 | { |
2975 | unit->error = 1; | |
b34976b6 | 2976 | return FALSE; |
252b5132 RH |
2977 | } |
2978 | } | |
2979 | ||
e00e8198 AM |
2980 | *function_ptr = NULL; |
2981 | func_p = lookup_address_in_function_table (unit, addr, function_ptr); | |
2982 | if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine) | |
2983 | stash->inliner_chain = *function_ptr; | |
240d6706 NC |
2984 | |
2985 | return lookup_address_in_line_info_table (unit->line_table, addr, | |
2986 | filename_ptr, | |
2987 | linenumber_ptr, | |
2988 | discriminator_ptr); | |
252b5132 RH |
2989 | } |
2990 | ||
bd210d54 NC |
2991 | /* Check to see if line info is already decoded in a comp_unit. |
2992 | If not, decode it. Returns TRUE if no errors were encountered; | |
5420f73d L |
2993 | FALSE otherwise. */ |
2994 | ||
2995 | static bfd_boolean | |
bd210d54 NC |
2996 | comp_unit_maybe_decode_line_info (struct comp_unit *unit, |
2997 | struct dwarf2_debug *stash) | |
5420f73d L |
2998 | { |
2999 | if (unit->error) | |
3000 | return FALSE; | |
3001 | ||
3002 | if (! unit->line_table) | |
3003 | { | |
3004 | if (! unit->stmtlist) | |
3005 | { | |
3006 | unit->error = 1; | |
3007 | return FALSE; | |
3008 | } | |
3009 | ||
3010 | unit->line_table = decode_line_info (unit, stash); | |
3011 | ||
3012 | if (! unit->line_table) | |
3013 | { | |
3014 | unit->error = 1; | |
3015 | return FALSE; | |
3016 | } | |
3017 | ||
3018 | if (unit->first_child_die_ptr < unit->end_ptr | |
3019 | && ! scan_unit_for_symbols (unit)) | |
3020 | { | |
3021 | unit->error = 1; | |
3022 | return FALSE; | |
3023 | } | |
3024 | } | |
3025 | ||
bd210d54 NC |
3026 | return TRUE; |
3027 | } | |
3028 | ||
3029 | /* If UNIT contains SYM at ADDR, set the output parameters to the | |
3030 | values for the line containing SYM. The output parameters, | |
3031 | FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be | |
3032 | filled in. | |
3033 | ||
3034 | Return TRUE if UNIT contains SYM, and no errors were encountered; | |
3035 | FALSE otherwise. */ | |
3036 | ||
3037 | static bfd_boolean | |
3038 | comp_unit_find_line (struct comp_unit *unit, | |
3039 | asymbol *sym, | |
3040 | bfd_vma addr, | |
3041 | const char **filename_ptr, | |
3042 | unsigned int *linenumber_ptr, | |
3043 | struct dwarf2_debug *stash) | |
3044 | { | |
3045 | if (!comp_unit_maybe_decode_line_info (unit, stash)) | |
3046 | return FALSE; | |
3047 | ||
5420f73d L |
3048 | if (sym->flags & BSF_FUNCTION) |
3049 | return lookup_symbol_in_function_table (unit, sym, addr, | |
3050 | filename_ptr, | |
3051 | linenumber_ptr); | |
bd210d54 NC |
3052 | |
3053 | return lookup_symbol_in_variable_table (unit, sym, addr, | |
3054 | filename_ptr, | |
3055 | linenumber_ptr); | |
3056 | } | |
3057 | ||
3058 | static struct funcinfo * | |
3059 | reverse_funcinfo_list (struct funcinfo *head) | |
3060 | { | |
3061 | struct funcinfo *rhead; | |
3062 | struct funcinfo *temp; | |
3063 | ||
3064 | for (rhead = NULL; head; head = temp) | |
3065 | { | |
3066 | temp = head->prev_func; | |
3067 | head->prev_func = rhead; | |
3068 | rhead = head; | |
3069 | } | |
3070 | return rhead; | |
3071 | } | |
3072 | ||
3073 | static struct varinfo * | |
3074 | reverse_varinfo_list (struct varinfo *head) | |
3075 | { | |
3076 | struct varinfo *rhead; | |
3077 | struct varinfo *temp; | |
3078 | ||
3079 | for (rhead = NULL; head; head = temp) | |
3080 | { | |
3081 | temp = head->prev_var; | |
3082 | head->prev_var = rhead; | |
3083 | rhead = head; | |
3084 | } | |
3085 | return rhead; | |
3086 | } | |
3087 | ||
3088 | /* Extract all interesting funcinfos and varinfos of a compilation | |
3089 | unit into hash tables for faster lookup. Returns TRUE if no | |
3090 | errors were enountered; FALSE otherwise. */ | |
3091 | ||
3092 | static bfd_boolean | |
3093 | comp_unit_hash_info (struct dwarf2_debug *stash, | |
3094 | struct comp_unit *unit, | |
3095 | struct info_hash_table *funcinfo_hash_table, | |
3096 | struct info_hash_table *varinfo_hash_table) | |
3097 | { | |
3098 | struct funcinfo* each_func; | |
3099 | struct varinfo* each_var; | |
3100 | bfd_boolean okay = TRUE; | |
3101 | ||
3102 | BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED); | |
3103 | ||
3104 | if (!comp_unit_maybe_decode_line_info (unit, stash)) | |
3105 | return FALSE; | |
3106 | ||
3107 | BFD_ASSERT (!unit->cached); | |
3108 | ||
3109 | /* To preserve the original search order, we went to visit the function | |
3110 | infos in the reversed order of the list. However, making the list | |
3111 | bi-directional use quite a bit of extra memory. So we reverse | |
3112 | the list first, traverse the list in the now reversed order and | |
3113 | finally reverse the list again to get back the original order. */ | |
3114 | unit->function_table = reverse_funcinfo_list (unit->function_table); | |
3115 | for (each_func = unit->function_table; | |
3116 | each_func && okay; | |
3117 | each_func = each_func->prev_func) | |
3118 | { | |
3119 | /* Skip nameless functions. */ | |
3120 | if (each_func->name) | |
3121 | /* There is no need to copy name string into hash table as | |
3122 | name string is either in the dwarf string buffer or | |
3123 | info in the stash. */ | |
3124 | okay = insert_info_hash_table (funcinfo_hash_table, each_func->name, | |
3125 | (void*) each_func, FALSE); | |
3126 | } | |
3127 | unit->function_table = reverse_funcinfo_list (unit->function_table); | |
3128 | if (!okay) | |
3129 | return FALSE; | |
3130 | ||
3131 | /* We do the same for variable infos. */ | |
3132 | unit->variable_table = reverse_varinfo_list (unit->variable_table); | |
3133 | for (each_var = unit->variable_table; | |
3134 | each_var && okay; | |
3135 | each_var = each_var->prev_var) | |
3136 | { | |
3137 | /* Skip stack vars and vars with no files or names. */ | |
3138 | if (each_var->stack == 0 | |
3139 | && each_var->file != NULL | |
3140 | && each_var->name != NULL) | |
3141 | /* There is no need to copy name string into hash table as | |
3142 | name string is either in the dwarf string buffer or | |
3143 | info in the stash. */ | |
3144 | okay = insert_info_hash_table (varinfo_hash_table, each_var->name, | |
3145 | (void*) each_var, FALSE); | |
3146 | } | |
3147 | ||
3148 | unit->variable_table = reverse_varinfo_list (unit->variable_table); | |
3149 | unit->cached = TRUE; | |
3150 | return okay; | |
5420f73d L |
3151 | } |
3152 | ||
e2f6d277 NC |
3153 | /* Locate a section in a BFD containing debugging info. The search starts |
3154 | from the section after AFTER_SEC, or from the first section in the BFD if | |
3155 | AFTER_SEC is NULL. The search works by examining the names of the | |
fc28f9aa TG |
3156 | sections. There are three permissiable names. The first two are given |
3157 | by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info | |
3158 | and .zdebug_info). The third is a prefix .gnu.linkonce.wi. | |
e2f6d277 NC |
3159 | This is a variation on the .debug_info section which has a checksum |
3160 | describing the contents appended onto the name. This allows the linker to | |
3161 | identify and discard duplicate debugging sections for different | |
3162 | compilation units. */ | |
a092b084 NC |
3163 | #define GNU_LINKONCE_INFO ".gnu.linkonce.wi." |
3164 | ||
3165 | static asection * | |
fc28f9aa | 3166 | find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections, |
93ee1e36 | 3167 | asection *after_sec) |
a092b084 | 3168 | { |
a2a50954 AM |
3169 | asection *msec; |
3170 | const char *look; | |
3171 | ||
3172 | if (after_sec == NULL) | |
3173 | { | |
3174 | look = debug_sections[debug_info].uncompressed_name; | |
3175 | msec = bfd_get_section_by_name (abfd, look); | |
3176 | if (msec != NULL) | |
3177 | return msec; | |
a092b084 | 3178 | |
a2a50954 AM |
3179 | look = debug_sections[debug_info].compressed_name; |
3180 | if (look != NULL) | |
3181 | { | |
3182 | msec = bfd_get_section_by_name (abfd, look); | |
3183 | if (msec != NULL) | |
3184 | return msec; | |
3185 | } | |
a092b084 | 3186 | |
a2a50954 AM |
3187 | for (msec = abfd->sections; msec != NULL; msec = msec->next) |
3188 | if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO)) | |
3189 | return msec; | |
3190 | ||
3191 | return NULL; | |
3192 | } | |
3193 | ||
3194 | for (msec = after_sec->next; msec != NULL; msec = msec->next) | |
a092b084 | 3195 | { |
a2a50954 AM |
3196 | look = debug_sections[debug_info].uncompressed_name; |
3197 | if (strcmp (msec->name, look) == 0) | |
a092b084 NC |
3198 | return msec; |
3199 | ||
a2a50954 AM |
3200 | look = debug_sections[debug_info].compressed_name; |
3201 | if (look != NULL && strcmp (msec->name, look) == 0) | |
1b315056 CS |
3202 | return msec; |
3203 | ||
0112cd26 | 3204 | if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO)) |
a092b084 | 3205 | return msec; |
a092b084 NC |
3206 | } |
3207 | ||
3208 | return NULL; | |
3209 | } | |
3210 | ||
93ee1e36 AM |
3211 | /* Transfer VMAs from object file to separate debug file. */ |
3212 | ||
3213 | static void | |
3214 | set_debug_vma (bfd *orig_bfd, bfd *debug_bfd) | |
3215 | { | |
3216 | asection *s, *d; | |
3217 | ||
3218 | for (s = orig_bfd->sections, d = debug_bfd->sections; | |
3219 | s != NULL && d != NULL; | |
3220 | s = s->next, d = d->next) | |
3221 | { | |
3222 | if ((d->flags & SEC_DEBUGGING) != 0) | |
3223 | break; | |
3224 | /* ??? Assumes 1-1 correspondence between sections in the | |
3225 | two files. */ | |
3226 | if (strcmp (s->name, d->name) == 0) | |
3227 | { | |
3228 | d->output_section = s->output_section; | |
3229 | d->output_offset = s->output_offset; | |
3230 | d->vma = s->vma; | |
3231 | } | |
3232 | } | |
3233 | } | |
3234 | ||
5609a71e | 3235 | /* Unset vmas for adjusted sections in STASH. */ |
d4c32a81 L |
3236 | |
3237 | static void | |
3238 | unset_sections (struct dwarf2_debug *stash) | |
3239 | { | |
93ee1e36 | 3240 | int i; |
5609a71e | 3241 | struct adjusted_section *p; |
d4c32a81 | 3242 | |
5609a71e DJ |
3243 | i = stash->adjusted_section_count; |
3244 | p = stash->adjusted_sections; | |
d4c32a81 L |
3245 | for (; i > 0; i--, p++) |
3246 | p->section->vma = 0; | |
3247 | } | |
3248 | ||
93ee1e36 AM |
3249 | /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a |
3250 | relocatable object file. VMAs are normally all zero in relocatable | |
3251 | object files, so if we want to distinguish locations in sections by | |
3252 | address we need to set VMAs so the sections do not overlap. We | |
3253 | also set VMA on .debug_info so that when we have multiple | |
3254 | .debug_info sections (or the linkonce variant) they also do not | |
3255 | overlap. The multiple .debug_info sections make up a single | |
3256 | logical section. ??? We should probably do the same for other | |
3257 | debug sections. */ | |
35ccda9e L |
3258 | |
3259 | static bfd_boolean | |
93ee1e36 | 3260 | place_sections (bfd *orig_bfd, struct dwarf2_debug *stash) |
35ccda9e | 3261 | { |
93ee1e36 | 3262 | bfd *abfd; |
5609a71e | 3263 | struct adjusted_section *p; |
93ee1e36 AM |
3264 | int i; |
3265 | const char *debug_info_name; | |
d4c32a81 | 3266 | |
5609a71e | 3267 | if (stash->adjusted_section_count != 0) |
35ccda9e | 3268 | { |
5609a71e DJ |
3269 | i = stash->adjusted_section_count; |
3270 | p = stash->adjusted_sections; | |
d4c32a81 L |
3271 | for (; i > 0; i--, p++) |
3272 | p->section->vma = p->adj_vma; | |
93ee1e36 | 3273 | return TRUE; |
d4c32a81 | 3274 | } |
93ee1e36 AM |
3275 | |
3276 | debug_info_name = stash->debug_sections[debug_info].uncompressed_name; | |
3277 | i = 0; | |
3278 | abfd = orig_bfd; | |
3279 | while (1) | |
d4c32a81 L |
3280 | { |
3281 | asection *sect; | |
35ccda9e | 3282 | |
d4c32a81 | 3283 | for (sect = abfd->sections; sect != NULL; sect = sect->next) |
35ccda9e | 3284 | { |
5609a71e DJ |
3285 | int is_debug_info; |
3286 | ||
cd0449ab | 3287 | if ((sect->output_section != NULL |
93ee1e36 AM |
3288 | && sect->output_section != sect |
3289 | && (sect->flags & SEC_DEBUGGING) == 0) | |
cd0449ab | 3290 | || sect->vma != 0) |
5609a71e DJ |
3291 | continue; |
3292 | ||
93ee1e36 AM |
3293 | is_debug_info = (strcmp (sect->name, debug_info_name) == 0 |
3294 | || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO)); | |
d4c32a81 | 3295 | |
93ee1e36 AM |
3296 | if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd) |
3297 | && !is_debug_info) | |
d4c32a81 L |
3298 | continue; |
3299 | ||
3300 | i++; | |
3301 | } | |
93ee1e36 AM |
3302 | if (abfd == stash->bfd_ptr) |
3303 | break; | |
3304 | abfd = stash->bfd_ptr; | |
3305 | } | |
3306 | ||
3307 | if (i <= 1) | |
3308 | stash->adjusted_section_count = -1; | |
3309 | else | |
3310 | { | |
3311 | bfd_vma last_vma = 0, last_dwarf = 0; | |
3312 | bfd_size_type amt = i * sizeof (struct adjusted_section); | |
d4c32a81 | 3313 | |
93ee1e36 AM |
3314 | p = (struct adjusted_section *) bfd_malloc (amt); |
3315 | if (p == NULL) | |
d4c32a81 L |
3316 | return FALSE; |
3317 | ||
5609a71e DJ |
3318 | stash->adjusted_sections = p; |
3319 | stash->adjusted_section_count = i; | |
d4c32a81 | 3320 | |
93ee1e36 AM |
3321 | abfd = orig_bfd; |
3322 | while (1) | |
d4c32a81 | 3323 | { |
93ee1e36 | 3324 | asection *sect; |
d4c32a81 | 3325 | |
93ee1e36 AM |
3326 | for (sect = abfd->sections; sect != NULL; sect = sect->next) |
3327 | { | |
3328 | bfd_size_type sz; | |
3329 | int is_debug_info; | |
5609a71e | 3330 | |
93ee1e36 AM |
3331 | if ((sect->output_section != NULL |
3332 | && sect->output_section != sect | |
3333 | && (sect->flags & SEC_DEBUGGING) == 0) | |
3334 | || sect->vma != 0) | |
3335 | continue; | |
5609a71e | 3336 | |
93ee1e36 AM |
3337 | is_debug_info = (strcmp (sect->name, debug_info_name) == 0 |
3338 | || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO)); | |
d4c32a81 | 3339 | |
93ee1e36 AM |
3340 | if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd) |
3341 | && !is_debug_info) | |
3342 | continue; | |
d4c32a81 | 3343 | |
93ee1e36 | 3344 | sz = sect->rawsize ? sect->rawsize : sect->size; |
5609a71e | 3345 | |
93ee1e36 AM |
3346 | if (is_debug_info) |
3347 | { | |
3348 | BFD_ASSERT (sect->alignment_power == 0); | |
3349 | sect->vma = last_dwarf; | |
3350 | last_dwarf += sz; | |
3351 | } | |
3352 | else | |
3353 | { | |
3354 | /* Align the new address to the current section | |
3355 | alignment. */ | |
3356 | last_vma = ((last_vma | |
3357 | + ~((bfd_vma) -1 << sect->alignment_power)) | |
3358 | & ((bfd_vma) -1 << sect->alignment_power)); | |
3359 | sect->vma = last_vma; | |
3360 | last_vma += sz; | |
3361 | } | |
d4c32a81 | 3362 | |
93ee1e36 AM |
3363 | p->section = sect; |
3364 | p->adj_vma = sect->vma; | |
3365 | p++; | |
3366 | } | |
3367 | if (abfd == stash->bfd_ptr) | |
3368 | break; | |
3369 | abfd = stash->bfd_ptr; | |
35ccda9e L |
3370 | } |
3371 | } | |
3372 | ||
93ee1e36 AM |
3373 | if (orig_bfd != stash->bfd_ptr) |
3374 | set_debug_vma (orig_bfd, stash->bfd_ptr); | |
3375 | ||
35ccda9e L |
3376 | return TRUE; |
3377 | } | |
3378 | ||
bd210d54 NC |
3379 | /* Look up a funcinfo by name using the given info hash table. If found, |
3380 | also update the locations pointed to by filename_ptr and linenumber_ptr. | |
3381 | ||
3382 | This function returns TRUE if a funcinfo that matches the given symbol | |
3383 | and address is found with any error; otherwise it returns FALSE. */ | |
3384 | ||
3385 | static bfd_boolean | |
3386 | info_hash_lookup_funcinfo (struct info_hash_table *hash_table, | |
3387 | asymbol *sym, | |
3388 | bfd_vma addr, | |
3389 | const char **filename_ptr, | |
3390 | unsigned int *linenumber_ptr) | |
3391 | { | |
3392 | struct funcinfo* each_func; | |
3393 | struct funcinfo* best_fit = NULL; | |
4ba3b326 | 3394 | bfd_vma best_fit_len = 0; |
bd210d54 NC |
3395 | struct info_list_node *node; |
3396 | struct arange *arange; | |
3397 | const char *name = bfd_asymbol_name (sym); | |
3398 | asection *sec = bfd_get_section (sym); | |
3399 | ||
3400 | for (node = lookup_info_hash_table (hash_table, name); | |
3401 | node; | |
3402 | node = node->next) | |
3403 | { | |
a50b1753 | 3404 | each_func = (struct funcinfo *) node->info; |
bd210d54 NC |
3405 | for (arange = &each_func->arange; |
3406 | arange; | |
3407 | arange = arange->next) | |
3408 | { | |
3409 | if ((!each_func->sec || each_func->sec == sec) | |
3410 | && addr >= arange->low | |
3411 | && addr < arange->high | |
3412 | && (!best_fit | |
4ba3b326 TG |
3413 | || arange->high - arange->low < best_fit_len)) |
3414 | { | |
3415 | best_fit = each_func; | |
3416 | best_fit_len = arange->high - arange->low; | |
3417 | } | |
bd210d54 NC |
3418 | } |
3419 | } | |
3420 | ||
3421 | if (best_fit) | |
3422 | { | |
3423 | best_fit->sec = sec; | |
3424 | *filename_ptr = best_fit->file; | |
3425 | *linenumber_ptr = best_fit->line; | |
3426 | return TRUE; | |
3427 | } | |
3428 | ||
3429 | return FALSE; | |
3430 | } | |
3431 | ||
3432 | /* Look up a varinfo by name using the given info hash table. If found, | |
3433 | also update the locations pointed to by filename_ptr and linenumber_ptr. | |
3434 | ||
3435 | This function returns TRUE if a varinfo that matches the given symbol | |
3436 | and address is found with any error; otherwise it returns FALSE. */ | |
3437 | ||
3438 | static bfd_boolean | |
3439 | info_hash_lookup_varinfo (struct info_hash_table *hash_table, | |
3440 | asymbol *sym, | |
3441 | bfd_vma addr, | |
3442 | const char **filename_ptr, | |
3443 | unsigned int *linenumber_ptr) | |
3444 | { | |
3445 | const char *name = bfd_asymbol_name (sym); | |
3446 | asection *sec = bfd_get_section (sym); | |
3447 | struct varinfo* each; | |
3448 | struct info_list_node *node; | |
3449 | ||
3450 | for (node = lookup_info_hash_table (hash_table, name); | |
3451 | node; | |
3452 | node = node->next) | |
3453 | { | |
a50b1753 | 3454 | each = (struct varinfo *) node->info; |
bd210d54 NC |
3455 | if (each->addr == addr |
3456 | && (!each->sec || each->sec == sec)) | |
3457 | { | |
3458 | each->sec = sec; | |
3459 | *filename_ptr = each->file; | |
3460 | *linenumber_ptr = each->line; | |
3461 | return TRUE; | |
3462 | } | |
3463 | } | |
3464 | ||
3465 | return FALSE; | |
3466 | } | |
3467 | ||
3468 | /* Update the funcinfo and varinfo info hash tables if they are | |
3469 | not up to date. Returns TRUE if there is no error; otherwise | |
3470 | returns FALSE and disable the info hash tables. */ | |
3471 | ||
3472 | static bfd_boolean | |
3473 | stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash) | |
3474 | { | |
3475 | struct comp_unit *each; | |
3476 | ||
3477 | /* Exit if hash tables are up-to-date. */ | |
3478 | if (stash->all_comp_units == stash->hash_units_head) | |
3479 | return TRUE; | |
3480 | ||
3481 | if (stash->hash_units_head) | |
3482 | each = stash->hash_units_head->prev_unit; | |
3483 | else | |
3484 | each = stash->last_comp_unit; | |
3485 | ||
3486 | while (each) | |
3487 | { | |
3488 | if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table, | |
3489 | stash->varinfo_hash_table)) | |
3490 | { | |
3491 | stash->info_hash_status = STASH_INFO_HASH_DISABLED; | |
3492 | return FALSE; | |
3493 | } | |
3494 | each = each->prev_unit; | |
3495 | } | |
3496 | ||
3497 | stash->hash_units_head = stash->all_comp_units; | |
3498 | return TRUE; | |
3499 | } | |
3500 | ||
3501 | /* Check consistency of info hash tables. This is for debugging only. */ | |
3502 | ||
3503 | static void ATTRIBUTE_UNUSED | |
3504 | stash_verify_info_hash_table (struct dwarf2_debug *stash) | |
3505 | { | |
3506 | struct comp_unit *each_unit; | |
3507 | struct funcinfo *each_func; | |
3508 | struct varinfo *each_var; | |
3509 | struct info_list_node *node; | |
3510 | bfd_boolean found; | |
3511 | ||
3512 | for (each_unit = stash->all_comp_units; | |
3513 | each_unit; | |
3514 | each_unit = each_unit->next_unit) | |
3515 | { | |
3516 | for (each_func = each_unit->function_table; | |
3517 | each_func; | |
3518 | each_func = each_func->prev_func) | |
3519 | { | |
3520 | if (!each_func->name) | |
3521 | continue; | |
3522 | node = lookup_info_hash_table (stash->funcinfo_hash_table, | |
3523 | each_func->name); | |
3524 | BFD_ASSERT (node); | |
3525 | found = FALSE; | |
3526 | while (node && !found) | |
3527 | { | |
3528 | found = node->info == each_func; | |
3529 | node = node->next; | |
3530 | } | |
3531 | BFD_ASSERT (found); | |
3532 | } | |
3533 | ||
3534 | for (each_var = each_unit->variable_table; | |
3535 | each_var; | |
3536 | each_var = each_var->prev_var) | |
3537 | { | |
3538 | if (!each_var->name || !each_var->file || each_var->stack) | |
3539 | continue; | |
3540 | node = lookup_info_hash_table (stash->varinfo_hash_table, | |
3541 | each_var->name); | |
3542 | BFD_ASSERT (node); | |
3543 | found = FALSE; | |
3544 | while (node && !found) | |
3545 | { | |
3546 | found = node->info == each_var; | |
3547 | node = node->next; | |
3548 | } | |
3549 | BFD_ASSERT (found); | |
3550 | } | |
3551 | } | |
3552 | } | |
3553 | ||
3554 | /* Check to see if we want to enable the info hash tables, which consume | |
3555 | quite a bit of memory. Currently we only check the number times | |
3556 | bfd_dwarf2_find_line is called. In the future, we may also want to | |
3557 | take the number of symbols into account. */ | |
3558 | ||
3559 | static void | |
3560 | stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash) | |
3561 | { | |
3562 | BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF); | |
3563 | ||
3564 | if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER) | |
3565 | return; | |
3566 | ||
3567 | /* FIXME: Maybe we should check the reduce_memory_overheads | |
3568 | and optimize fields in the bfd_link_info structure ? */ | |
3569 | ||
3570 | /* Create hash tables. */ | |
3571 | stash->funcinfo_hash_table = create_info_hash_table (abfd); | |
3572 | stash->varinfo_hash_table = create_info_hash_table (abfd); | |
3573 | if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table) | |
3574 | { | |
3575 | /* Turn off info hashes if any allocation above fails. */ | |
3576 | stash->info_hash_status = STASH_INFO_HASH_DISABLED; | |
3577 | return; | |
3578 | } | |
3579 | /* We need a forced update so that the info hash tables will | |
3580 | be created even though there is no compilation unit. That | |
3581 | happens if STASH_INFO_HASH_TRIGGER is 0. */ | |
3582 | stash_maybe_update_info_hash_tables (stash); | |
3583 | stash->info_hash_status = STASH_INFO_HASH_ON; | |
3584 | } | |
3585 | ||
3586 | /* Find the file and line associated with a symbol and address using the | |
3587 | info hash tables of a stash. If there is a match, the function returns | |
3588 | TRUE and update the locations pointed to by filename_ptr and linenumber_ptr; | |
3589 | otherwise it returns FALSE. */ | |
3590 | ||
3591 | static bfd_boolean | |
3592 | stash_find_line_fast (struct dwarf2_debug *stash, | |
3593 | asymbol *sym, | |
3594 | bfd_vma addr, | |
3595 | const char **filename_ptr, | |
3596 | unsigned int *linenumber_ptr) | |
3597 | { | |
3598 | BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON); | |
3599 | ||
3600 | if (sym->flags & BSF_FUNCTION) | |
3601 | return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr, | |
3602 | filename_ptr, linenumber_ptr); | |
3603 | return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr, | |
3604 | filename_ptr, linenumber_ptr); | |
3605 | } | |
3606 | ||
cd0449ab AM |
3607 | /* Save current section VMAs. */ |
3608 | ||
3609 | static bfd_boolean | |
3610 | save_section_vma (const bfd *abfd, struct dwarf2_debug *stash) | |
3611 | { | |
3612 | asection *s; | |
3613 | unsigned int i; | |
3614 | ||
3615 | if (abfd->section_count == 0) | |
3616 | return TRUE; | |
3617 | stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count); | |
3618 | if (stash->sec_vma == NULL) | |
3619 | return FALSE; | |
3620 | for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next) | |
3621 | { | |
3622 | if (s->output_section != NULL) | |
3623 | stash->sec_vma[i] = s->output_section->vma + s->output_offset; | |
3624 | else | |
3625 | stash->sec_vma[i] = s->vma; | |
3626 | } | |
3627 | return TRUE; | |
3628 | } | |
3629 | ||
3630 | /* Compare current section VMAs against those at the time the stash | |
3631 | was created. If find_nearest_line is used in linker warnings or | |
3632 | errors early in the link process, the debug info stash will be | |
3633 | invalid for later calls. This is because we relocate debug info | |
3634 | sections, so the stashed section contents depend on symbol values, | |
3635 | which in turn depend on section VMAs. */ | |
3636 | ||
3637 | static bfd_boolean | |
3638 | section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash) | |
3639 | { | |
3640 | asection *s; | |
3641 | unsigned int i; | |
3642 | ||
3643 | for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next) | |
3644 | { | |
3645 | bfd_vma vma; | |
3646 | ||
3647 | if (s->output_section != NULL) | |
3648 | vma = s->output_section->vma + s->output_offset; | |
3649 | else | |
3650 | vma = s->vma; | |
3651 | if (vma != stash->sec_vma[i]) | |
3652 | return FALSE; | |
3653 | } | |
3654 | return TRUE; | |
3655 | } | |
3656 | ||
2ca7691a TG |
3657 | /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified. |
3658 | If DEBUG_BFD is not specified, we read debug information from ABFD | |
3659 | or its gnu_debuglink. The results will be stored in PINFO. | |
3660 | The function returns TRUE iff debug information is ready. */ | |
3661 | ||
3662 | bfd_boolean | |
3663 | _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd, | |
93ee1e36 AM |
3664 | const struct dwarf_debug_section *debug_sections, |
3665 | asymbol **symbols, | |
3666 | void **pinfo, | |
3667 | bfd_boolean do_place) | |
2ca7691a TG |
3668 | { |
3669 | bfd_size_type amt = sizeof (struct dwarf2_debug); | |
3670 | bfd_size_type total_size; | |
3671 | asection *msec; | |
3672 | struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo; | |
3673 | ||
3674 | if (stash != NULL) | |
cd0449ab | 3675 | { |
93ee1e36 | 3676 | if (section_vma_same (abfd, stash)) |
cd0449ab AM |
3677 | return TRUE; |
3678 | _bfd_dwarf2_cleanup_debug_info (abfd, pinfo); | |
3679 | memset (stash, 0, amt); | |
3680 | } | |
3681 | else | |
3682 | { | |
3683 | stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt); | |
3684 | if (! stash) | |
3685 | return FALSE; | |
3686 | } | |
2ca7691a | 3687 | stash->debug_sections = debug_sections; |
1c37913d | 3688 | stash->syms = symbols; |
cd0449ab AM |
3689 | if (!save_section_vma (abfd, stash)) |
3690 | return FALSE; | |
2ca7691a TG |
3691 | |
3692 | *pinfo = stash; | |
3693 | ||
3694 | if (debug_bfd == NULL) | |
3695 | debug_bfd = abfd; | |
3696 | ||
3697 | msec = find_debug_info (debug_bfd, debug_sections, NULL); | |
3698 | if (msec == NULL && abfd == debug_bfd) | |
3699 | { | |
3700 | char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR); | |
3701 | ||
3702 | if (debug_filename == NULL) | |
3703 | /* No dwarf2 info, and no gnu_debuglink to follow. | |
3704 | Note that at this point the stash has been allocated, but | |
3705 | contains zeros. This lets future calls to this function | |
3706 | fail more quickly. */ | |
3707 | return FALSE; | |
3708 | ||
3709 | if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL | |
3710 | || ! bfd_check_format (debug_bfd, bfd_object) | |
3711 | || (msec = find_debug_info (debug_bfd, | |
93ee1e36 AM |
3712 | debug_sections, NULL)) == NULL |
3713 | || !bfd_generic_link_read_symbols (debug_bfd)) | |
2ca7691a TG |
3714 | { |
3715 | if (debug_bfd) | |
3716 | bfd_close (debug_bfd); | |
3717 | /* FIXME: Should we report our failure to follow the debuglink ? */ | |
3718 | free (debug_filename); | |
3719 | return FALSE; | |
3720 | } | |
93ee1e36 AM |
3721 | |
3722 | symbols = bfd_get_outsymbols (debug_bfd); | |
3723 | stash->syms = symbols; | |
1c37913d | 3724 | stash->close_on_cleanup = TRUE; |
2ca7691a | 3725 | } |
1c37913d | 3726 | stash->bfd_ptr = debug_bfd; |
2ca7691a | 3727 | |
93ee1e36 AM |
3728 | if (do_place |
3729 | && !place_sections (abfd, stash)) | |
3730 | return FALSE; | |
3731 | ||
2ca7691a TG |
3732 | /* There can be more than one DWARF2 info section in a BFD these |
3733 | days. First handle the easy case when there's only one. If | |
3734 | there's more than one, try case two: none of the sections is | |
3735 | compressed. In that case, read them all in and produce one | |
3736 | large stash. We do this in two passes - in the first pass we | |
3737 | just accumulate the section sizes, and in the second pass we | |
3738 | read in the section's contents. (The allows us to avoid | |
3739 | reallocing the data as we add sections to the stash.) If | |
3740 | some or all sections are compressed, then do things the slow | |
3741 | way, with a bunch of reallocs. */ | |
3742 | ||
3743 | if (! find_debug_info (debug_bfd, debug_sections, msec)) | |
3744 | { | |
3745 | /* Case 1: only one info section. */ | |
3746 | total_size = msec->size; | |
3747 | if (! read_section (debug_bfd, &stash->debug_sections[debug_info], | |
3748 | symbols, 0, | |
3749 | &stash->info_ptr_memory, &total_size)) | |
3750 | return FALSE; | |
3751 | } | |
3752 | else | |
3753 | { | |
3754 | /* Case 2: multiple sections. */ | |
3755 | for (total_size = 0; | |
3756 | msec; | |
3757 | msec = find_debug_info (debug_bfd, debug_sections, msec)) | |
3758 | total_size += msec->size; | |
3759 | ||
3760 | stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size); | |
3761 | if (stash->info_ptr_memory == NULL) | |
3762 | return FALSE; | |
3763 | ||
3764 | total_size = 0; | |
3765 | for (msec = find_debug_info (debug_bfd, debug_sections, NULL); | |
3766 | msec; | |
3767 | msec = find_debug_info (debug_bfd, debug_sections, msec)) | |
3768 | { | |
3769 | bfd_size_type size; | |
3770 | ||
3771 | size = msec->size; | |
3772 | if (size == 0) | |
3773 | continue; | |
3774 | ||
3775 | if (!(bfd_simple_get_relocated_section_contents | |
3776 | (debug_bfd, msec, stash->info_ptr_memory + total_size, | |
3777 | symbols))) | |
3778 | return FALSE; | |
3779 | ||
3780 | total_size += size; | |
3781 | } | |
3782 | } | |
3783 | ||
3784 | stash->info_ptr = stash->info_ptr_memory; | |
3785 | stash->info_ptr_end = stash->info_ptr + total_size; | |
3786 | stash->sec = find_debug_info (debug_bfd, debug_sections, NULL); | |
3787 | stash->sec_info_ptr = stash->info_ptr; | |
2ca7691a TG |
3788 | return TRUE; |
3789 | } | |
3790 | ||
425bd9e1 NC |
3791 | /* Scan the debug information in PINFO looking for a DW_TAG_subprogram |
3792 | abbrev with a DW_AT_low_pc attached to it. Then lookup that same | |
3793 | symbol in SYMBOLS and return the difference between the low_pc and | |
3794 | the symbol's address. Returns 0 if no suitable symbol could be found. */ | |
3795 | ||
3796 | bfd_signed_vma | |
3797 | _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo) | |
3798 | { | |
3799 | struct dwarf2_debug *stash; | |
3800 | struct comp_unit * unit; | |
3801 | ||
3802 | stash = (struct dwarf2_debug *) *pinfo; | |
3803 | ||
3804 | if (stash == NULL) | |
3805 | return 0; | |
3806 | ||
3807 | for (unit = stash->all_comp_units; unit; unit = unit->next_unit) | |
3808 | { | |
3809 | struct funcinfo * func; | |
3810 | ||
3811 | if (unit->function_table == NULL) | |
3812 | { | |
3813 | if (unit->line_table == NULL) | |
3814 | unit->line_table = decode_line_info (unit, stash); | |
3815 | if (unit->line_table != NULL) | |
3816 | scan_unit_for_symbols (unit); | |
3817 | } | |
3818 | ||
3819 | for (func = unit->function_table; func != NULL; func = func->prev_func) | |
3820 | if (func->name && func->arange.low) | |
3821 | { | |
3822 | asymbol ** psym; | |
3823 | ||
3824 | /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */ | |
3825 | ||
3826 | for (psym = symbols; * psym != NULL; psym++) | |
3827 | { | |
3828 | asymbol * sym = * psym; | |
3829 | ||
3830 | if (sym->flags & BSF_FUNCTION | |
3831 | && sym->section != NULL | |
3832 | && strcmp (sym->name, func->name) == 0) | |
3833 | return ((bfd_signed_vma) func->arange.low) - | |
3834 | ((bfd_signed_vma) (sym->value + sym->section->vma)); | |
3835 | } | |
3836 | } | |
3837 | } | |
3838 | ||
3839 | return 0; | |
3840 | } | |
3841 | ||
bec42b15 NC |
3842 | /* Find the source code location of SYMBOL. If SYMBOL is NULL |
3843 | then find the nearest source code location corresponding to | |
3844 | the address SECTION + OFFSET. | |
3845 | Returns TRUE if the line is found without error and fills in | |
3846 | FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was | |
3847 | NULL the FUNCTIONNAME_PTR is also filled in. | |
3848 | SYMBOLS contains the symbol table for ABFD. | |
fc28f9aa | 3849 | DEBUG_SECTIONS contains the name of the dwarf debug sections. |
bec42b15 NC |
3850 | ADDR_SIZE is the number of bytes in the initial .debug_info length |
3851 | field and in the abbreviation offset, or zero to indicate that the | |
3852 | default value should be used. */ | |
252b5132 | 3853 | |
fb167eb2 AM |
3854 | bfd_boolean |
3855 | _bfd_dwarf2_find_nearest_line (bfd *abfd, | |
3856 | asymbol **symbols, | |
3857 | asymbol *symbol, | |
3858 | asection *section, | |
3859 | bfd_vma offset, | |
3860 | const char **filename_ptr, | |
3861 | const char **functionname_ptr, | |
3862 | unsigned int *linenumber_ptr, | |
3863 | unsigned int *discriminator_ptr, | |
3864 | const struct dwarf_debug_section *debug_sections, | |
3865 | unsigned int addr_size, | |
3866 | void **pinfo) | |
252b5132 RH |
3867 | { |
3868 | /* Read each compilation unit from the section .debug_info, and check | |
3869 | to see if it contains the address we are searching for. If yes, | |
3870 | lookup the address, and return the line number info. If no, go | |
98591c73 | 3871 | on to the next compilation unit. |
252b5132 RH |
3872 | |
3873 | We keep a list of all the previously read compilation units, and | |
98591c73 | 3874 | a pointer to the next un-read compilation unit. Check the |
a092b084 | 3875 | previously read units before reading more. */ |
1ba54ee0 | 3876 | struct dwarf2_debug *stash; |
a092b084 | 3877 | /* What address are we looking for? */ |
1ba54ee0 | 3878 | bfd_vma addr; |
252b5132 | 3879 | struct comp_unit* each; |
e00e8198 | 3880 | struct funcinfo *function = NULL; |
240d6706 | 3881 | bfd_boolean found = FALSE; |
bec42b15 | 3882 | bfd_boolean do_line; |
d4c32a81 | 3883 | |
2ca7691a TG |
3884 | *filename_ptr = NULL; |
3885 | if (functionname_ptr != NULL) | |
3886 | *functionname_ptr = NULL; | |
3887 | *linenumber_ptr = 0; | |
f725daa8 CC |
3888 | if (discriminator_ptr) |
3889 | *discriminator_ptr = 0; | |
d4c32a81 | 3890 | |
93ee1e36 AM |
3891 | if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections, |
3892 | symbols, pinfo, | |
3893 | (abfd->flags & (EXEC_P | DYNAMIC)) == 0)) | |
2ca7691a | 3894 | return FALSE; |
d4c32a81 | 3895 | |
2ca7691a | 3896 | stash = (struct dwarf2_debug *) *pinfo; |
d4c32a81 | 3897 | |
fb167eb2 | 3898 | do_line = symbol != NULL; |
bec42b15 NC |
3899 | if (do_line) |
3900 | { | |
fb167eb2 | 3901 | BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL); |
bec42b15 | 3902 | section = bfd_get_section (symbol); |
fb167eb2 | 3903 | addr = symbol->value; |
bec42b15 | 3904 | } |
bec42b15 | 3905 | else |
fb167eb2 AM |
3906 | { |
3907 | BFD_ASSERT (section != NULL && functionname_ptr != NULL); | |
3908 | addr = offset; | |
3909 | } | |
bec42b15 | 3910 | |
1ba54ee0 | 3911 | if (section->output_section) |
6dd55cb7 | 3912 | addr += section->output_section->vma + section->output_offset; |
1ba54ee0 | 3913 | else |
6dd55cb7 | 3914 | addr += section->vma; |
a092b084 | 3915 | |
98591c73 | 3916 | /* A null info_ptr indicates that there is no dwarf2 info |
a092b084 | 3917 | (or that an error occured while setting up the stash). */ |
252b5132 | 3918 | if (! stash->info_ptr) |
2ca7691a | 3919 | return FALSE; |
252b5132 | 3920 | |
4ab527b0 FF |
3921 | stash->inliner_chain = NULL; |
3922 | ||
a092b084 | 3923 | /* Check the previously read comp. units first. */ |
bd210d54 NC |
3924 | if (do_line) |
3925 | { | |
3926 | /* The info hash tables use quite a bit of memory. We may not want to | |
3927 | always use them. We use some heuristics to decide if and when to | |
3928 | turn it on. */ | |
3929 | if (stash->info_hash_status == STASH_INFO_HASH_OFF) | |
3930 | stash_maybe_enable_info_hash_tables (abfd, stash); | |
3931 | ||
3932 | /* Keep info hash table up to date if they are available. Note that we | |
3933 | may disable the hash tables if there is any error duing update. */ | |
3934 | if (stash->info_hash_status == STASH_INFO_HASH_ON) | |
3935 | stash_maybe_update_info_hash_tables (stash); | |
3936 | ||
3937 | if (stash->info_hash_status == STASH_INFO_HASH_ON) | |
3938 | { | |
3939 | found = stash_find_line_fast (stash, symbol, addr, filename_ptr, | |
3940 | linenumber_ptr); | |
3941 | if (found) | |
3942 | goto done; | |
3943 | } | |
0d161102 | 3944 | else |
bd210d54 NC |
3945 | { |
3946 | /* Check the previously read comp. units first. */ | |
3947 | for (each = stash->all_comp_units; each; each = each->next_unit) | |
3948 | if ((symbol->flags & BSF_FUNCTION) == 0 | |
a2a50954 | 3949 | || each->arange.high == 0 |
bd210d54 NC |
3950 | || comp_unit_contains_address (each, addr)) |
3951 | { | |
3952 | found = comp_unit_find_line (each, symbol, addr, filename_ptr, | |
3953 | linenumber_ptr, stash); | |
3954 | if (found) | |
3955 | goto done; | |
3956 | } | |
3957 | } | |
3958 | } | |
3959 | else | |
3960 | { | |
240d6706 NC |
3961 | bfd_vma min_range = (bfd_vma) -1; |
3962 | const char * local_filename = NULL; | |
e00e8198 | 3963 | struct funcinfo *local_function = NULL; |
240d6706 NC |
3964 | unsigned int local_linenumber = 0; |
3965 | unsigned int local_discriminator = 0; | |
96691246 | 3966 | |
709d67f1 AM |
3967 | for (each = stash->all_comp_units; each; each = each->next_unit) |
3968 | { | |
240d6706 NC |
3969 | bfd_vma range = (bfd_vma) -1; |
3970 | ||
a2a50954 AM |
3971 | found = ((each->arange.high == 0 |
3972 | || comp_unit_contains_address (each, addr)) | |
240d6706 NC |
3973 | && (range = comp_unit_find_nearest_line (each, addr, |
3974 | & local_filename, | |
e00e8198 | 3975 | & local_function, |
240d6706 NC |
3976 | & local_linenumber, |
3977 | & local_discriminator, | |
3978 | stash)) != 0); | |
709d67f1 | 3979 | if (found) |
240d6706 NC |
3980 | { |
3981 | /* PRs 15935 15994: Bogus debug information may have provided us | |
3982 | with an erroneous match. We attempt to counter this by | |
3983 | selecting the match that has the smallest address range | |
3984 | associated with it. (We are assuming that corrupt debug info | |
3985 | will tend to result in extra large address ranges rather than | |
3986 | extra small ranges). | |
3987 | ||
3988 | This does mean that we scan through all of the CUs associated | |
3989 | with the bfd each time this function is called. But this does | |
3990 | have the benefit of producing consistent results every time the | |
3991 | function is called. */ | |
3992 | if (range <= min_range) | |
3993 | { | |
3994 | if (filename_ptr && local_filename) | |
3995 | * filename_ptr = local_filename; | |
e00e8198 AM |
3996 | if (local_function) |
3997 | function = local_function; | |
240d6706 NC |
3998 | if (discriminator_ptr && local_discriminator) |
3999 | * discriminator_ptr = local_discriminator; | |
4000 | if (local_linenumber) | |
4001 | * linenumber_ptr = local_linenumber; | |
4002 | min_range = range; | |
4003 | } | |
4004 | } | |
4005 | } | |
4006 | ||
4007 | if (* linenumber_ptr) | |
4008 | { | |
4009 | found = TRUE; | |
4010 | goto done; | |
709d67f1 | 4011 | } |
5420f73d L |
4012 | } |
4013 | ||
5420f73d L |
4014 | /* The DWARF2 spec says that the initial length field, and the |
4015 | offset of the abbreviation table, should both be 4-byte values. | |
4016 | However, some compilers do things differently. */ | |
4017 | if (addr_size == 0) | |
4018 | addr_size = 4; | |
4019 | BFD_ASSERT (addr_size == 4 || addr_size == 8); | |
4020 | ||
4021 | /* Read each remaining comp. units checking each as they are read. */ | |
4022 | while (stash->info_ptr < stash->info_ptr_end) | |
4023 | { | |
4024 | bfd_vma length; | |
4025 | unsigned int offset_size = addr_size; | |
4026 | bfd_byte *info_ptr_unit = stash->info_ptr; | |
4027 | ||
dbb3fbbb | 4028 | length = read_4_bytes (stash->bfd_ptr, stash->info_ptr, stash->info_ptr_end); |
bec42b15 NC |
4029 | /* A 0xffffff length is the DWARF3 way of indicating |
4030 | we use 64-bit offsets, instead of 32-bit offsets. */ | |
5420f73d L |
4031 | if (length == 0xffffffff) |
4032 | { | |
4033 | offset_size = 8; | |
dbb3fbbb | 4034 | length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end); |
5420f73d L |
4035 | stash->info_ptr += 12; |
4036 | } | |
4037 | /* A zero length is the IRIX way of indicating 64-bit offsets, | |
4038 | mostly because the 64-bit length will generally fit in 32 | |
4039 | bits, and the endianness helps. */ | |
4040 | else if (length == 0) | |
4041 | { | |
4042 | offset_size = 8; | |
dbb3fbbb | 4043 | length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end); |
5420f73d L |
4044 | stash->info_ptr += 8; |
4045 | } | |
024b2372 CD |
4046 | /* In the absence of the hints above, we assume 32-bit DWARF2 |
4047 | offsets even for targets with 64-bit addresses, because: | |
4048 | a) most of the time these targets will not have generated | |
4049 | more than 2Gb of debug info and so will not need 64-bit | |
4050 | offsets, | |
4051 | and | |
4052 | b) if they do use 64-bit offsets but they are not using | |
4053 | the size hints that are tested for above then they are | |
4054 | not conforming to the DWARF3 standard anyway. */ | |
5420f73d L |
4055 | else if (addr_size == 8) |
4056 | { | |
024b2372 | 4057 | offset_size = 4; |
2d47a72c | 4058 | stash->info_ptr += 4; |
5420f73d L |
4059 | } |
4060 | else | |
4061 | stash->info_ptr += 4; | |
4062 | ||
4063 | if (length > 0) | |
4064 | { | |
dbb3fbbb NC |
4065 | bfd_byte * new_ptr; |
4066 | ||
0d161102 | 4067 | each = parse_comp_unit (stash, length, info_ptr_unit, |
5420f73d | 4068 | offset_size); |
d74e4b29 NS |
4069 | if (!each) |
4070 | /* The dwarf information is damaged, don't trust it any | |
4071 | more. */ | |
4072 | break; | |
5420f73d | 4073 | |
dbb3fbbb NC |
4074 | new_ptr = stash->info_ptr + length; |
4075 | /* PR 17512: file: 1500698c. */ | |
4076 | if (new_ptr < stash->info_ptr) | |
4077 | { | |
4078 | /* A corrupt length value - do not trust the info any more. */ | |
4079 | found = FALSE; | |
4080 | break; | |
4081 | } | |
62f8d217 | 4082 | else |
dbb3fbbb | 4083 | stash->info_ptr = new_ptr; |
62f8d217 | 4084 | |
d74e4b29 NS |
4085 | if (stash->all_comp_units) |
4086 | stash->all_comp_units->prev_unit = each; | |
4087 | else | |
4088 | stash->last_comp_unit = each; | |
68ffbac6 | 4089 | |
d74e4b29 NS |
4090 | each->next_unit = stash->all_comp_units; |
4091 | stash->all_comp_units = each; | |
68ffbac6 | 4092 | |
d74e4b29 NS |
4093 | /* DW_AT_low_pc and DW_AT_high_pc are optional for |
4094 | compilation units. If we don't have them (i.e., | |
4095 | unit->high == 0), we need to consult the line info table | |
4096 | to see if a compilation unit contains the given | |
4097 | address. */ | |
4098 | if (do_line) | |
4099 | found = (((symbol->flags & BSF_FUNCTION) == 0 | |
4100 | || each->arange.high == 0 | |
4101 | || comp_unit_contains_address (each, addr)) | |
4102 | && comp_unit_find_line (each, symbol, addr, | |
4103 | filename_ptr, | |
4104 | linenumber_ptr, | |
4105 | stash)); | |
4106 | else | |
4107 | found = ((each->arange.high == 0 | |
4108 | || comp_unit_contains_address (each, addr)) | |
4109 | && comp_unit_find_nearest_line (each, addr, | |
4110 | filename_ptr, | |
e00e8198 | 4111 | &function, |
d74e4b29 | 4112 | linenumber_ptr, |
9b8d1a36 | 4113 | discriminator_ptr, |
fa15f18d | 4114 | stash) != 0); |
be04437d AM |
4115 | |
4116 | if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr) | |
4117 | == stash->sec->size) | |
4118 | { | |
fc28f9aa | 4119 | stash->sec = find_debug_info (stash->bfd_ptr, debug_sections, |
93ee1e36 | 4120 | stash->sec); |
be04437d AM |
4121 | stash->sec_info_ptr = stash->info_ptr; |
4122 | } | |
4123 | ||
d74e4b29 NS |
4124 | if (found) |
4125 | goto done; | |
5420f73d L |
4126 | } |
4127 | } | |
4128 | ||
a2a50954 | 4129 | done: |
e00e8198 AM |
4130 | if (function) |
4131 | { | |
4132 | if (!function->is_linkage | |
4133 | && _bfd_elf_find_function (abfd, symbols, section, offset, | |
4134 | *filename_ptr ? NULL : filename_ptr, | |
4135 | functionname_ptr)) | |
4136 | { | |
4137 | function->name = *functionname_ptr; | |
4138 | function->is_linkage = TRUE; | |
4139 | } | |
4140 | else | |
4141 | *functionname_ptr = function->name; | |
4142 | } | |
d4c32a81 L |
4143 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) |
4144 | unset_sections (stash); | |
4145 | ||
4146 | return found; | |
5420f73d L |
4147 | } |
4148 | ||
4ab527b0 FF |
4149 | bfd_boolean |
4150 | _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED, | |
4151 | const char **filename_ptr, | |
4152 | const char **functionname_ptr, | |
4153 | unsigned int *linenumber_ptr, | |
4154 | void **pinfo) | |
4155 | { | |
4156 | struct dwarf2_debug *stash; | |
4157 | ||
a50b1753 | 4158 | stash = (struct dwarf2_debug *) *pinfo; |
4ab527b0 FF |
4159 | if (stash) |
4160 | { | |
4161 | struct funcinfo *func = stash->inliner_chain; | |
bec42b15 | 4162 | |
4ab527b0 FF |
4163 | if (func && func->caller_func) |
4164 | { | |
4165 | *filename_ptr = func->caller_file; | |
4166 | *functionname_ptr = func->caller_func->name; | |
4167 | *linenumber_ptr = func->caller_line; | |
4168 | stash->inliner_chain = func->caller_func; | |
bec42b15 | 4169 | return TRUE; |
4ab527b0 FF |
4170 | } |
4171 | } | |
4172 | ||
bec42b15 | 4173 | return FALSE; |
4ab527b0 FF |
4174 | } |
4175 | ||
35330cce | 4176 | void |
d9071b0c | 4177 | _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo) |
35330cce | 4178 | { |
5bb3703f | 4179 | struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo; |
35330cce | 4180 | struct comp_unit *each; |
35330cce | 4181 | |
d9071b0c | 4182 | if (abfd == NULL || stash == NULL) |
35330cce NC |
4183 | return; |
4184 | ||
4185 | for (each = stash->all_comp_units; each; each = each->next_unit) | |
4186 | { | |
34b5e0b2 | 4187 | struct abbrev_info **abbrevs = each->abbrevs; |
90b5b1a5 NC |
4188 | struct funcinfo *function_table = each->function_table; |
4189 | struct varinfo *variable_table = each->variable_table; | |
34b5e0b2 | 4190 | size_t i; |
35330cce | 4191 | |
34b5e0b2 | 4192 | for (i = 0; i < ABBREV_HASH_SIZE; i++) |
d8d1c398 | 4193 | { |
34b5e0b2 | 4194 | struct abbrev_info *abbrev = abbrevs[i]; |
35330cce | 4195 | |
34b5e0b2 | 4196 | while (abbrev) |
d8d1c398 | 4197 | { |
34b5e0b2 NC |
4198 | free (abbrev->attrs); |
4199 | abbrev = abbrev->next; | |
d8d1c398 AM |
4200 | } |
4201 | } | |
35330cce NC |
4202 | |
4203 | if (each->line_table) | |
d8d1c398 | 4204 | { |
34b5e0b2 NC |
4205 | free (each->line_table->dirs); |
4206 | free (each->line_table->files); | |
d8d1c398 | 4207 | } |
90b5b1a5 NC |
4208 | |
4209 | while (function_table) | |
4210 | { | |
4211 | if (function_table->file) | |
4212 | { | |
4213 | free (function_table->file); | |
4214 | function_table->file = NULL; | |
4215 | } | |
4216 | ||
4217 | if (function_table->caller_file) | |
4218 | { | |
4219 | free (function_table->caller_file); | |
4220 | function_table->caller_file = NULL; | |
4221 | } | |
4222 | function_table = function_table->prev_func; | |
4223 | } | |
4224 | ||
4225 | while (variable_table) | |
4226 | { | |
4227 | if (variable_table->file) | |
4228 | { | |
4229 | free (variable_table->file); | |
4230 | variable_table->file = NULL; | |
4231 | } | |
4232 | ||
4233 | variable_table = variable_table->prev_var; | |
4234 | } | |
35330cce NC |
4235 | } |
4236 | ||
5d0900eb AM |
4237 | if (stash->dwarf_abbrev_buffer) |
4238 | free (stash->dwarf_abbrev_buffer); | |
4239 | if (stash->dwarf_line_buffer) | |
4240 | free (stash->dwarf_line_buffer); | |
4241 | if (stash->dwarf_str_buffer) | |
4242 | free (stash->dwarf_str_buffer); | |
4243 | if (stash->dwarf_ranges_buffer) | |
4244 | free (stash->dwarf_ranges_buffer); | |
4245 | if (stash->info_ptr_memory) | |
4246 | free (stash->info_ptr_memory); | |
1c37913d AM |
4247 | if (stash->close_on_cleanup) |
4248 | bfd_close (stash->bfd_ptr); | |
95e34fb4 NC |
4249 | if (stash->alt_dwarf_str_buffer) |
4250 | free (stash->alt_dwarf_str_buffer); | |
4251 | if (stash->alt_dwarf_info_buffer) | |
4252 | free (stash->alt_dwarf_info_buffer); | |
cd0449ab AM |
4253 | if (stash->sec_vma) |
4254 | free (stash->sec_vma); | |
93ee1e36 AM |
4255 | if (stash->adjusted_sections) |
4256 | free (stash->adjusted_sections); | |
95e34fb4 NC |
4257 | if (stash->alt_bfd_ptr) |
4258 | bfd_close (stash->alt_bfd_ptr); | |
35330cce | 4259 | } |
e00e8198 AM |
4260 | |
4261 | /* Find the function to a particular section and offset, | |
4262 | for error reporting. */ | |
4263 | ||
4264 | bfd_boolean | |
4265 | _bfd_elf_find_function (bfd *abfd, | |
4266 | asymbol **symbols, | |
4267 | asection *section, | |
4268 | bfd_vma offset, | |
4269 | const char **filename_ptr, | |
4270 | const char **functionname_ptr) | |
4271 | { | |
4272 | struct elf_find_function_cache | |
4273 | { | |
4274 | asection *last_section; | |
4275 | asymbol *func; | |
4276 | const char *filename; | |
4277 | bfd_size_type func_size; | |
4278 | } *cache; | |
4279 | ||
4280 | if (symbols == NULL) | |
4281 | return FALSE; | |
4282 | ||
4283 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
4284 | return FALSE; | |
4285 | ||
4286 | cache = elf_tdata (abfd)->elf_find_function_cache; | |
4287 | if (cache == NULL) | |
4288 | { | |
4289 | cache = bfd_zalloc (abfd, sizeof (*cache)); | |
4290 | elf_tdata (abfd)->elf_find_function_cache = cache; | |
4291 | if (cache == NULL) | |
4292 | return FALSE; | |
4293 | } | |
4294 | if (cache->last_section != section | |
4295 | || cache->func == NULL | |
4296 | || offset < cache->func->value | |
4297 | || offset >= cache->func->value + cache->func_size) | |
4298 | { | |
4299 | asymbol *file; | |
4300 | bfd_vma low_func; | |
4301 | asymbol **p; | |
4302 | /* ??? Given multiple file symbols, it is impossible to reliably | |
4303 | choose the right file name for global symbols. File symbols are | |
4304 | local symbols, and thus all file symbols must sort before any | |
4305 | global symbols. The ELF spec may be interpreted to say that a | |
4306 | file symbol must sort before other local symbols, but currently | |
4307 | ld -r doesn't do this. So, for ld -r output, it is possible to | |
4308 | make a better choice of file name for local symbols by ignoring | |
4309 | file symbols appearing after a given local symbol. */ | |
4310 | enum { nothing_seen, symbol_seen, file_after_symbol_seen } state; | |
4311 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4312 | ||
4313 | file = NULL; | |
4314 | low_func = 0; | |
4315 | state = nothing_seen; | |
4316 | cache->filename = NULL; | |
4317 | cache->func = NULL; | |
4318 | cache->func_size = 0; | |
4319 | cache->last_section = section; | |
4320 | ||
4321 | for (p = symbols; *p != NULL; p++) | |
4322 | { | |
4323 | asymbol *sym = *p; | |
4324 | bfd_vma code_off; | |
4325 | bfd_size_type size; | |
4326 | ||
4327 | if ((sym->flags & BSF_FILE) != 0) | |
4328 | { | |
4329 | file = sym; | |
4330 | if (state == symbol_seen) | |
4331 | state = file_after_symbol_seen; | |
4332 | continue; | |
4333 | } | |
4334 | ||
4335 | size = bed->maybe_function_sym (sym, section, &code_off); | |
4336 | if (size != 0 | |
4337 | && code_off <= offset | |
4338 | && (code_off > low_func | |
4339 | || (code_off == low_func | |
4340 | && size > cache->func_size))) | |
4341 | { | |
4342 | cache->func = sym; | |
4343 | cache->func_size = size; | |
4344 | cache->filename = NULL; | |
4345 | low_func = code_off; | |
4346 | if (file != NULL | |
4347 | && ((sym->flags & BSF_LOCAL) != 0 | |
4348 | || state != file_after_symbol_seen)) | |
4349 | cache->filename = bfd_asymbol_name (file); | |
4350 | } | |
4351 | if (state == nothing_seen) | |
4352 | state = symbol_seen; | |
4353 | } | |
4354 | } | |
4355 | ||
4356 | if (cache->func == NULL) | |
4357 | return FALSE; | |
4358 | ||
4359 | if (filename_ptr) | |
4360 | *filename_ptr = cache->filename; | |
4361 | if (functionname_ptr) | |
4362 | *functionname_ptr = bfd_asymbol_name (cache->func); | |
4363 | ||
4364 | return TRUE; | |
4365 | } |