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