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