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