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