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