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