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