| 1 | /* DWARF 2 support. |
| 2 | Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, |
| 3 | 2004, 2005, 2006, 2007 Free Software Foundation, Inc. |
| 4 | |
| 5 | Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions |
| 6 | (gavin@cygnus.com). |
| 7 | |
| 8 | From the dwarf2read.c header: |
| 9 | Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology, |
| 10 | Inc. with support from Florida State University (under contract |
| 11 | with the Ada Joint Program Office), and Silicon Graphics, Inc. |
| 12 | Initial contribution by Brent Benson, Harris Computer Systems, Inc., |
| 13 | based on Fred Fish's (Cygnus Support) implementation of DWARF 1 |
| 14 | support in dwarfread.c |
| 15 | |
| 16 | This file is part of BFD. |
| 17 | |
| 18 | This program is free software; you can redistribute it and/or modify |
| 19 | it under the terms of the GNU General Public License as published by |
| 20 | the Free Software Foundation; either version 2 of the License, or (at |
| 21 | your option) any later version. |
| 22 | |
| 23 | This program is distributed in the hope that it will be useful, but |
| 24 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 26 | General Public License for more details. |
| 27 | |
| 28 | You should have received a copy of the GNU General Public License |
| 29 | along with this program; if not, write to the Free Software |
| 30 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 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 "elf/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 default_is_stmt; |
| 48 | int line_base; |
| 49 | unsigned char line_range; |
| 50 | unsigned char opcode_base; |
| 51 | unsigned char *standard_opcode_lengths; |
| 52 | }; |
| 53 | |
| 54 | /* Attributes have a name and a value. */ |
| 55 | |
| 56 | struct attribute |
| 57 | { |
| 58 | enum dwarf_attribute name; |
| 59 | enum dwarf_form form; |
| 60 | union |
| 61 | { |
| 62 | char *str; |
| 63 | struct dwarf_block *blk; |
| 64 | bfd_uint64_t val; |
| 65 | bfd_int64_t sval; |
| 66 | } |
| 67 | u; |
| 68 | }; |
| 69 | |
| 70 | /* Blocks are a bunch of untyped bytes. */ |
| 71 | struct dwarf_block |
| 72 | { |
| 73 | unsigned int size; |
| 74 | bfd_byte *data; |
| 75 | }; |
| 76 | |
| 77 | struct loadable_section |
| 78 | { |
| 79 | asection *section; |
| 80 | bfd_vma adj_vma; |
| 81 | }; |
| 82 | |
| 83 | struct dwarf2_debug |
| 84 | { |
| 85 | /* A list of all previously read comp_units. */ |
| 86 | struct comp_unit *all_comp_units; |
| 87 | |
| 88 | /* The next unread compilation unit within the .debug_info section. |
| 89 | Zero indicates that the .debug_info section has not been loaded |
| 90 | into a buffer yet. */ |
| 91 | bfd_byte *info_ptr; |
| 92 | |
| 93 | /* Pointer to the end of the .debug_info section memory buffer. */ |
| 94 | bfd_byte *info_ptr_end; |
| 95 | |
| 96 | /* Pointer to the bfd, section and address of the beginning of the |
| 97 | section. The bfd might be different than expected because of |
| 98 | gnu_debuglink sections. */ |
| 99 | bfd * bfd; |
| 100 | asection *sec; |
| 101 | bfd_byte *sec_info_ptr; |
| 102 | |
| 103 | /* Pointer to the symbol table. */ |
| 104 | asymbol **syms; |
| 105 | |
| 106 | /* Pointer to the .debug_abbrev section loaded into memory. */ |
| 107 | bfd_byte *dwarf_abbrev_buffer; |
| 108 | |
| 109 | /* Length of the loaded .debug_abbrev section. */ |
| 110 | unsigned long dwarf_abbrev_size; |
| 111 | |
| 112 | /* Buffer for decode_line_info. */ |
| 113 | bfd_byte *dwarf_line_buffer; |
| 114 | |
| 115 | /* Length of the loaded .debug_line section. */ |
| 116 | unsigned long dwarf_line_size; |
| 117 | |
| 118 | /* Pointer to the .debug_str section loaded into memory. */ |
| 119 | bfd_byte *dwarf_str_buffer; |
| 120 | |
| 121 | /* Length of the loaded .debug_str section. */ |
| 122 | unsigned long dwarf_str_size; |
| 123 | |
| 124 | /* Pointer to the .debug_ranges section loaded into memory. */ |
| 125 | bfd_byte *dwarf_ranges_buffer; |
| 126 | |
| 127 | /* Length of the loaded .debug_ranges section. */ |
| 128 | unsigned long dwarf_ranges_size; |
| 129 | |
| 130 | /* If the most recent call to bfd_find_nearest_line was given an |
| 131 | address in an inlined function, preserve a pointer into the |
| 132 | calling chain for subsequent calls to bfd_find_inliner_info to |
| 133 | use. */ |
| 134 | struct funcinfo *inliner_chain; |
| 135 | |
| 136 | /* Number of loadable sections. */ |
| 137 | unsigned int loadable_section_count; |
| 138 | |
| 139 | /* Array of loadable sections. */ |
| 140 | struct loadable_section *loadable_sections; |
| 141 | }; |
| 142 | |
| 143 | struct arange |
| 144 | { |
| 145 | struct arange *next; |
| 146 | bfd_vma low; |
| 147 | bfd_vma high; |
| 148 | }; |
| 149 | |
| 150 | /* A minimal decoding of DWARF2 compilation units. We only decode |
| 151 | what's needed to get to the line number information. */ |
| 152 | |
| 153 | struct comp_unit |
| 154 | { |
| 155 | /* Chain the previously read compilation units. */ |
| 156 | struct comp_unit *next_unit; |
| 157 | |
| 158 | /* Keep the bfd convenient (for memory allocation). */ |
| 159 | bfd *abfd; |
| 160 | |
| 161 | /* The lowest and highest addresses contained in this compilation |
| 162 | unit as specified in the compilation unit header. */ |
| 163 | struct arange arange; |
| 164 | |
| 165 | /* The DW_AT_name attribute (for error messages). */ |
| 166 | char *name; |
| 167 | |
| 168 | /* The abbrev hash table. */ |
| 169 | struct abbrev_info **abbrevs; |
| 170 | |
| 171 | /* Note that an error was found by comp_unit_find_nearest_line. */ |
| 172 | int error; |
| 173 | |
| 174 | /* The DW_AT_comp_dir attribute. */ |
| 175 | char *comp_dir; |
| 176 | |
| 177 | /* TRUE if there is a line number table associated with this comp. unit. */ |
| 178 | int stmtlist; |
| 179 | |
| 180 | /* Pointer to the current comp_unit so that we can find a given entry |
| 181 | by its reference. */ |
| 182 | bfd_byte *info_ptr_unit; |
| 183 | |
| 184 | /* The offset into .debug_line of the line number table. */ |
| 185 | unsigned long line_offset; |
| 186 | |
| 187 | /* Pointer to the first child die for the comp unit. */ |
| 188 | bfd_byte *first_child_die_ptr; |
| 189 | |
| 190 | /* The end of the comp unit. */ |
| 191 | bfd_byte *end_ptr; |
| 192 | |
| 193 | /* The decoded line number, NULL if not yet decoded. */ |
| 194 | struct line_info_table *line_table; |
| 195 | |
| 196 | /* A list of the functions found in this comp. unit. */ |
| 197 | struct funcinfo *function_table; |
| 198 | |
| 199 | /* A list of the variables found in this comp. unit. */ |
| 200 | struct varinfo *variable_table; |
| 201 | |
| 202 | /* Pointer to dwarf2_debug structure. */ |
| 203 | struct dwarf2_debug *stash; |
| 204 | |
| 205 | /* Address size for this unit - from unit header. */ |
| 206 | unsigned char addr_size; |
| 207 | |
| 208 | /* Offset size for this unit - from unit header. */ |
| 209 | unsigned char offset_size; |
| 210 | |
| 211 | /* Base address for this unit - from DW_AT_low_pc attribute of |
| 212 | DW_TAG_compile_unit DIE */ |
| 213 | bfd_vma base_address; |
| 214 | }; |
| 215 | |
| 216 | /* This data structure holds the information of an abbrev. */ |
| 217 | struct abbrev_info |
| 218 | { |
| 219 | unsigned int number; /* Number identifying abbrev. */ |
| 220 | enum dwarf_tag tag; /* DWARF tag. */ |
| 221 | int has_children; /* Boolean. */ |
| 222 | unsigned int num_attrs; /* Number of attributes. */ |
| 223 | struct attr_abbrev *attrs; /* An array of attribute descriptions. */ |
| 224 | struct abbrev_info *next; /* Next in chain. */ |
| 225 | }; |
| 226 | |
| 227 | struct attr_abbrev |
| 228 | { |
| 229 | enum dwarf_attribute name; |
| 230 | enum dwarf_form form; |
| 231 | }; |
| 232 | |
| 233 | #ifndef ABBREV_HASH_SIZE |
| 234 | #define ABBREV_HASH_SIZE 121 |
| 235 | #endif |
| 236 | #ifndef ATTR_ALLOC_CHUNK |
| 237 | #define ATTR_ALLOC_CHUNK 4 |
| 238 | #endif |
| 239 | |
| 240 | /* VERBATIM |
| 241 | The following function up to the END VERBATIM mark are |
| 242 | copied directly from dwarf2read.c. */ |
| 243 | |
| 244 | /* Read dwarf information from a buffer. */ |
| 245 | |
| 246 | static unsigned int |
| 247 | read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf) |
| 248 | { |
| 249 | return bfd_get_8 (abfd, buf); |
| 250 | } |
| 251 | |
| 252 | static int |
| 253 | read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf) |
| 254 | { |
| 255 | return bfd_get_signed_8 (abfd, buf); |
| 256 | } |
| 257 | |
| 258 | static unsigned int |
| 259 | read_2_bytes (bfd *abfd, bfd_byte *buf) |
| 260 | { |
| 261 | return bfd_get_16 (abfd, buf); |
| 262 | } |
| 263 | |
| 264 | static unsigned int |
| 265 | read_4_bytes (bfd *abfd, bfd_byte *buf) |
| 266 | { |
| 267 | return bfd_get_32 (abfd, buf); |
| 268 | } |
| 269 | |
| 270 | static bfd_uint64_t |
| 271 | read_8_bytes (bfd *abfd, bfd_byte *buf) |
| 272 | { |
| 273 | return bfd_get_64 (abfd, buf); |
| 274 | } |
| 275 | |
| 276 | static bfd_byte * |
| 277 | read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED, |
| 278 | bfd_byte *buf, |
| 279 | unsigned int size ATTRIBUTE_UNUSED) |
| 280 | { |
| 281 | /* If the size of a host char is 8 bits, we can return a pointer |
| 282 | to the buffer, otherwise we have to copy the data to a buffer |
| 283 | allocated on the temporary obstack. */ |
| 284 | return buf; |
| 285 | } |
| 286 | |
| 287 | static char * |
| 288 | read_string (bfd *abfd ATTRIBUTE_UNUSED, |
| 289 | bfd_byte *buf, |
| 290 | unsigned int *bytes_read_ptr) |
| 291 | { |
| 292 | /* Return a pointer to the embedded string. */ |
| 293 | char *str = (char *) buf; |
| 294 | if (*str == '\0') |
| 295 | { |
| 296 | *bytes_read_ptr = 1; |
| 297 | return NULL; |
| 298 | } |
| 299 | |
| 300 | *bytes_read_ptr = strlen (str) + 1; |
| 301 | return str; |
| 302 | } |
| 303 | |
| 304 | static char * |
| 305 | read_indirect_string (struct comp_unit* unit, |
| 306 | bfd_byte *buf, |
| 307 | unsigned int *bytes_read_ptr) |
| 308 | { |
| 309 | bfd_uint64_t offset; |
| 310 | struct dwarf2_debug *stash = unit->stash; |
| 311 | char *str; |
| 312 | |
| 313 | if (unit->offset_size == 4) |
| 314 | offset = read_4_bytes (unit->abfd, buf); |
| 315 | else |
| 316 | offset = read_8_bytes (unit->abfd, buf); |
| 317 | *bytes_read_ptr = unit->offset_size; |
| 318 | |
| 319 | if (! stash->dwarf_str_buffer) |
| 320 | { |
| 321 | asection *msec; |
| 322 | bfd *abfd = unit->abfd; |
| 323 | bfd_size_type sz; |
| 324 | |
| 325 | msec = bfd_get_section_by_name (abfd, ".debug_str"); |
| 326 | if (! msec) |
| 327 | { |
| 328 | (*_bfd_error_handler) |
| 329 | (_("Dwarf Error: Can't find .debug_str section.")); |
| 330 | bfd_set_error (bfd_error_bad_value); |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | sz = msec->rawsize ? msec->rawsize : msec->size; |
| 335 | stash->dwarf_str_size = sz; |
| 336 | stash->dwarf_str_buffer = bfd_alloc (abfd, sz); |
| 337 | if (! stash->dwarf_str_buffer) |
| 338 | return NULL; |
| 339 | |
| 340 | if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer, |
| 341 | 0, sz)) |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | if (offset >= stash->dwarf_str_size) |
| 346 | { |
| 347 | (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."), |
| 348 | (unsigned long) offset, stash->dwarf_str_size); |
| 349 | bfd_set_error (bfd_error_bad_value); |
| 350 | return NULL; |
| 351 | } |
| 352 | |
| 353 | str = (char *) stash->dwarf_str_buffer + offset; |
| 354 | if (*str == '\0') |
| 355 | return NULL; |
| 356 | return str; |
| 357 | } |
| 358 | |
| 359 | /* END VERBATIM */ |
| 360 | |
| 361 | static bfd_uint64_t |
| 362 | read_address (struct comp_unit *unit, bfd_byte *buf) |
| 363 | { |
| 364 | int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma; |
| 365 | |
| 366 | if (signed_vma) |
| 367 | { |
| 368 | switch (unit->addr_size) |
| 369 | { |
| 370 | case 8: |
| 371 | return bfd_get_signed_64 (unit->abfd, buf); |
| 372 | case 4: |
| 373 | return bfd_get_signed_32 (unit->abfd, buf); |
| 374 | case 2: |
| 375 | return bfd_get_signed_16 (unit->abfd, buf); |
| 376 | default: |
| 377 | abort (); |
| 378 | } |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | switch (unit->addr_size) |
| 383 | { |
| 384 | case 8: |
| 385 | return bfd_get_64 (unit->abfd, buf); |
| 386 | case 4: |
| 387 | return bfd_get_32 (unit->abfd, buf); |
| 388 | case 2: |
| 389 | return bfd_get_16 (unit->abfd, buf); |
| 390 | default: |
| 391 | abort (); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /* Lookup an abbrev_info structure in the abbrev hash table. */ |
| 397 | |
| 398 | static struct abbrev_info * |
| 399 | lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs) |
| 400 | { |
| 401 | unsigned int hash_number; |
| 402 | struct abbrev_info *abbrev; |
| 403 | |
| 404 | hash_number = number % ABBREV_HASH_SIZE; |
| 405 | abbrev = abbrevs[hash_number]; |
| 406 | |
| 407 | while (abbrev) |
| 408 | { |
| 409 | if (abbrev->number == number) |
| 410 | return abbrev; |
| 411 | else |
| 412 | abbrev = abbrev->next; |
| 413 | } |
| 414 | |
| 415 | return NULL; |
| 416 | } |
| 417 | |
| 418 | /* In DWARF version 2, the description of the debugging information is |
| 419 | stored in a separate .debug_abbrev section. Before we read any |
| 420 | dies from a section we read in all abbreviations and install them |
| 421 | in a hash table. */ |
| 422 | |
| 423 | static struct abbrev_info** |
| 424 | read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash) |
| 425 | { |
| 426 | struct abbrev_info **abbrevs; |
| 427 | bfd_byte *abbrev_ptr; |
| 428 | struct abbrev_info *cur_abbrev; |
| 429 | unsigned int abbrev_number, bytes_read, abbrev_name; |
| 430 | unsigned int abbrev_form, hash_number; |
| 431 | bfd_size_type amt; |
| 432 | |
| 433 | if (! stash->dwarf_abbrev_buffer) |
| 434 | { |
| 435 | asection *msec; |
| 436 | |
| 437 | msec = bfd_get_section_by_name (abfd, ".debug_abbrev"); |
| 438 | if (! msec) |
| 439 | { |
| 440 | (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section.")); |
| 441 | bfd_set_error (bfd_error_bad_value); |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | stash->dwarf_abbrev_size = msec->size; |
| 446 | stash->dwarf_abbrev_buffer |
| 447 | = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, |
| 448 | stash->syms); |
| 449 | if (! stash->dwarf_abbrev_buffer) |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | if (offset >= stash->dwarf_abbrev_size) |
| 454 | { |
| 455 | (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."), |
| 456 | (unsigned long) offset, stash->dwarf_abbrev_size); |
| 457 | bfd_set_error (bfd_error_bad_value); |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE; |
| 462 | abbrevs = bfd_zalloc (abfd, amt); |
| 463 | |
| 464 | abbrev_ptr = stash->dwarf_abbrev_buffer + offset; |
| 465 | abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 466 | abbrev_ptr += bytes_read; |
| 467 | |
| 468 | /* Loop until we reach an abbrev number of 0. */ |
| 469 | while (abbrev_number) |
| 470 | { |
| 471 | amt = sizeof (struct abbrev_info); |
| 472 | cur_abbrev = bfd_zalloc (abfd, amt); |
| 473 | |
| 474 | /* Read in abbrev header. */ |
| 475 | cur_abbrev->number = abbrev_number; |
| 476 | cur_abbrev->tag = (enum dwarf_tag) |
| 477 | read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 478 | abbrev_ptr += bytes_read; |
| 479 | cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr); |
| 480 | abbrev_ptr += 1; |
| 481 | |
| 482 | /* Now read in declarations. */ |
| 483 | abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 484 | abbrev_ptr += bytes_read; |
| 485 | abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 486 | abbrev_ptr += bytes_read; |
| 487 | |
| 488 | while (abbrev_name) |
| 489 | { |
| 490 | if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0) |
| 491 | { |
| 492 | struct attr_abbrev *tmp; |
| 493 | |
| 494 | amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK; |
| 495 | amt *= sizeof (struct attr_abbrev); |
| 496 | tmp = bfd_realloc (cur_abbrev->attrs, amt); |
| 497 | if (tmp == NULL) |
| 498 | { |
| 499 | size_t i; |
| 500 | |
| 501 | for (i = 0; i < ABBREV_HASH_SIZE; i++) |
| 502 | { |
| 503 | struct abbrev_info *abbrev = abbrevs[i]; |
| 504 | |
| 505 | while (abbrev) |
| 506 | { |
| 507 | free (abbrev->attrs); |
| 508 | abbrev = abbrev->next; |
| 509 | } |
| 510 | } |
| 511 | return NULL; |
| 512 | } |
| 513 | cur_abbrev->attrs = tmp; |
| 514 | } |
| 515 | |
| 516 | cur_abbrev->attrs[cur_abbrev->num_attrs].name |
| 517 | = (enum dwarf_attribute) abbrev_name; |
| 518 | cur_abbrev->attrs[cur_abbrev->num_attrs++].form |
| 519 | = (enum dwarf_form) abbrev_form; |
| 520 | abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 521 | abbrev_ptr += bytes_read; |
| 522 | abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 523 | abbrev_ptr += bytes_read; |
| 524 | } |
| 525 | |
| 526 | hash_number = abbrev_number % ABBREV_HASH_SIZE; |
| 527 | cur_abbrev->next = abbrevs[hash_number]; |
| 528 | abbrevs[hash_number] = cur_abbrev; |
| 529 | |
| 530 | /* Get next abbreviation. |
| 531 | Under Irix6 the abbreviations for a compilation unit are not |
| 532 | always properly terminated with an abbrev number of 0. |
| 533 | Exit loop if we encounter an abbreviation which we have |
| 534 | already read (which means we are about to read the abbreviations |
| 535 | for the next compile unit) or if the end of the abbreviation |
| 536 | table is reached. */ |
| 537 | if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer) |
| 538 | >= stash->dwarf_abbrev_size) |
| 539 | break; |
| 540 | abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 541 | abbrev_ptr += bytes_read; |
| 542 | if (lookup_abbrev (abbrev_number,abbrevs) != NULL) |
| 543 | break; |
| 544 | } |
| 545 | |
| 546 | return abbrevs; |
| 547 | } |
| 548 | |
| 549 | /* Read an attribute value described by an attribute form. */ |
| 550 | |
| 551 | static bfd_byte * |
| 552 | read_attribute_value (struct attribute *attr, |
| 553 | unsigned form, |
| 554 | struct comp_unit *unit, |
| 555 | bfd_byte *info_ptr) |
| 556 | { |
| 557 | bfd *abfd = unit->abfd; |
| 558 | unsigned int bytes_read; |
| 559 | struct dwarf_block *blk; |
| 560 | bfd_size_type amt; |
| 561 | |
| 562 | attr->form = (enum dwarf_form) form; |
| 563 | |
| 564 | switch (form) |
| 565 | { |
| 566 | case DW_FORM_addr: |
| 567 | /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */ |
| 568 | case DW_FORM_ref_addr: |
| 569 | attr->u.val = read_address (unit, info_ptr); |
| 570 | info_ptr += unit->addr_size; |
| 571 | break; |
| 572 | case DW_FORM_block2: |
| 573 | amt = sizeof (struct dwarf_block); |
| 574 | blk = bfd_alloc (abfd, amt); |
| 575 | blk->size = read_2_bytes (abfd, info_ptr); |
| 576 | info_ptr += 2; |
| 577 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); |
| 578 | info_ptr += blk->size; |
| 579 | attr->u.blk = blk; |
| 580 | break; |
| 581 | case DW_FORM_block4: |
| 582 | amt = sizeof (struct dwarf_block); |
| 583 | blk = bfd_alloc (abfd, amt); |
| 584 | blk->size = read_4_bytes (abfd, info_ptr); |
| 585 | info_ptr += 4; |
| 586 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); |
| 587 | info_ptr += blk->size; |
| 588 | attr->u.blk = blk; |
| 589 | break; |
| 590 | case DW_FORM_data2: |
| 591 | attr->u.val = read_2_bytes (abfd, info_ptr); |
| 592 | info_ptr += 2; |
| 593 | break; |
| 594 | case DW_FORM_data4: |
| 595 | attr->u.val = read_4_bytes (abfd, info_ptr); |
| 596 | info_ptr += 4; |
| 597 | break; |
| 598 | case DW_FORM_data8: |
| 599 | attr->u.val = read_8_bytes (abfd, info_ptr); |
| 600 | info_ptr += 8; |
| 601 | break; |
| 602 | case DW_FORM_string: |
| 603 | attr->u.str = read_string (abfd, info_ptr, &bytes_read); |
| 604 | info_ptr += bytes_read; |
| 605 | break; |
| 606 | case DW_FORM_strp: |
| 607 | attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read); |
| 608 | info_ptr += bytes_read; |
| 609 | break; |
| 610 | case DW_FORM_block: |
| 611 | amt = sizeof (struct dwarf_block); |
| 612 | blk = bfd_alloc (abfd, amt); |
| 613 | blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 614 | info_ptr += bytes_read; |
| 615 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); |
| 616 | info_ptr += blk->size; |
| 617 | attr->u.blk = blk; |
| 618 | break; |
| 619 | case DW_FORM_block1: |
| 620 | amt = sizeof (struct dwarf_block); |
| 621 | blk = bfd_alloc (abfd, amt); |
| 622 | blk->size = read_1_byte (abfd, info_ptr); |
| 623 | info_ptr += 1; |
| 624 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); |
| 625 | info_ptr += blk->size; |
| 626 | attr->u.blk = blk; |
| 627 | break; |
| 628 | case DW_FORM_data1: |
| 629 | attr->u.val = read_1_byte (abfd, info_ptr); |
| 630 | info_ptr += 1; |
| 631 | break; |
| 632 | case DW_FORM_flag: |
| 633 | attr->u.val = read_1_byte (abfd, info_ptr); |
| 634 | info_ptr += 1; |
| 635 | break; |
| 636 | case DW_FORM_sdata: |
| 637 | attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read); |
| 638 | info_ptr += bytes_read; |
| 639 | break; |
| 640 | case DW_FORM_udata: |
| 641 | attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 642 | info_ptr += bytes_read; |
| 643 | break; |
| 644 | case DW_FORM_ref1: |
| 645 | attr->u.val = read_1_byte (abfd, info_ptr); |
| 646 | info_ptr += 1; |
| 647 | break; |
| 648 | case DW_FORM_ref2: |
| 649 | attr->u.val = read_2_bytes (abfd, info_ptr); |
| 650 | info_ptr += 2; |
| 651 | break; |
| 652 | case DW_FORM_ref4: |
| 653 | attr->u.val = read_4_bytes (abfd, info_ptr); |
| 654 | info_ptr += 4; |
| 655 | break; |
| 656 | case DW_FORM_ref8: |
| 657 | attr->u.val = read_8_bytes (abfd, info_ptr); |
| 658 | info_ptr += 8; |
| 659 | break; |
| 660 | case DW_FORM_ref_udata: |
| 661 | attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 662 | info_ptr += bytes_read; |
| 663 | break; |
| 664 | case DW_FORM_indirect: |
| 665 | form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 666 | info_ptr += bytes_read; |
| 667 | info_ptr = read_attribute_value (attr, form, unit, info_ptr); |
| 668 | break; |
| 669 | default: |
| 670 | (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."), |
| 671 | form); |
| 672 | bfd_set_error (bfd_error_bad_value); |
| 673 | } |
| 674 | return info_ptr; |
| 675 | } |
| 676 | |
| 677 | /* Read an attribute described by an abbreviated attribute. */ |
| 678 | |
| 679 | static bfd_byte * |
| 680 | read_attribute (struct attribute *attr, |
| 681 | struct attr_abbrev *abbrev, |
| 682 | struct comp_unit *unit, |
| 683 | bfd_byte *info_ptr) |
| 684 | { |
| 685 | attr->name = abbrev->name; |
| 686 | info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr); |
| 687 | return info_ptr; |
| 688 | } |
| 689 | |
| 690 | /* Source line information table routines. */ |
| 691 | |
| 692 | #define FILE_ALLOC_CHUNK 5 |
| 693 | #define DIR_ALLOC_CHUNK 5 |
| 694 | |
| 695 | struct line_info |
| 696 | { |
| 697 | struct line_info* prev_line; |
| 698 | bfd_vma address; |
| 699 | char *filename; |
| 700 | unsigned int line; |
| 701 | unsigned int column; |
| 702 | int end_sequence; /* End of (sequential) code sequence. */ |
| 703 | }; |
| 704 | |
| 705 | struct fileinfo |
| 706 | { |
| 707 | char *name; |
| 708 | unsigned int dir; |
| 709 | unsigned int time; |
| 710 | unsigned int size; |
| 711 | }; |
| 712 | |
| 713 | struct line_info_table |
| 714 | { |
| 715 | bfd* abfd; |
| 716 | unsigned int num_files; |
| 717 | unsigned int num_dirs; |
| 718 | char *comp_dir; |
| 719 | char **dirs; |
| 720 | struct fileinfo* files; |
| 721 | struct line_info* last_line; /* largest VMA */ |
| 722 | struct line_info* lcl_head; /* local head; used in 'add_line_info' */ |
| 723 | }; |
| 724 | |
| 725 | /* Remember some information about each function. If the function is |
| 726 | inlined (DW_TAG_inlined_subroutine) it may have two additional |
| 727 | attributes, DW_AT_call_file and DW_AT_call_line, which specify the |
| 728 | source code location where this function was inlined. */ |
| 729 | |
| 730 | struct funcinfo |
| 731 | { |
| 732 | struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */ |
| 733 | struct funcinfo *caller_func; /* Pointer to function one scope higher */ |
| 734 | char *caller_file; /* Source location file name where caller_func inlines this func */ |
| 735 | int caller_line; /* Source location line number where caller_func inlines this func */ |
| 736 | char *file; /* Source location file name */ |
| 737 | int line; /* Source location line number */ |
| 738 | int tag; |
| 739 | char *name; |
| 740 | struct arange arange; |
| 741 | asection *sec; /* Where the symbol is defined */ |
| 742 | }; |
| 743 | |
| 744 | struct varinfo |
| 745 | { |
| 746 | /* Pointer to previous variable in list of all variables */ |
| 747 | struct varinfo *prev_var; |
| 748 | /* Source location file name */ |
| 749 | char *file; |
| 750 | /* Source location line number */ |
| 751 | int line; |
| 752 | int tag; |
| 753 | char *name; |
| 754 | bfd_vma addr; |
| 755 | /* Where the symbol is defined */ |
| 756 | asection *sec; |
| 757 | /* Is this a stack variable? */ |
| 758 | unsigned int stack: 1; |
| 759 | }; |
| 760 | |
| 761 | /* Return TRUE if NEW_LINE should sort after LINE. */ |
| 762 | |
| 763 | static inline bfd_boolean |
| 764 | new_line_sorts_after (struct line_info *new_line, struct line_info *line) |
| 765 | { |
| 766 | return (new_line->address > line->address |
| 767 | || (new_line->address == line->address |
| 768 | && new_line->end_sequence < line->end_sequence)); |
| 769 | } |
| 770 | |
| 771 | |
| 772 | /* Adds a new entry to the line_info list in the line_info_table, ensuring |
| 773 | that the list is sorted. Note that the line_info list is sorted from |
| 774 | highest to lowest VMA (with possible duplicates); that is, |
| 775 | line_info->prev_line always accesses an equal or smaller VMA. */ |
| 776 | |
| 777 | static void |
| 778 | add_line_info (struct line_info_table *table, |
| 779 | bfd_vma address, |
| 780 | char *filename, |
| 781 | unsigned int line, |
| 782 | unsigned int column, |
| 783 | int end_sequence) |
| 784 | { |
| 785 | bfd_size_type amt = sizeof (struct line_info); |
| 786 | struct line_info* info = bfd_alloc (table->abfd, amt); |
| 787 | |
| 788 | /* Set member data of 'info'. */ |
| 789 | info->address = address; |
| 790 | info->line = line; |
| 791 | info->column = column; |
| 792 | info->end_sequence = end_sequence; |
| 793 | |
| 794 | if (filename && filename[0]) |
| 795 | { |
| 796 | info->filename = bfd_alloc (table->abfd, strlen (filename) + 1); |
| 797 | if (info->filename) |
| 798 | strcpy (info->filename, filename); |
| 799 | } |
| 800 | else |
| 801 | info->filename = NULL; |
| 802 | |
| 803 | /* Find the correct location for 'info'. Normally we will receive |
| 804 | new line_info data 1) in order and 2) with increasing VMAs. |
| 805 | However some compilers break the rules (cf. decode_line_info) and |
| 806 | so we include some heuristics for quickly finding the correct |
| 807 | location for 'info'. In particular, these heuristics optimize for |
| 808 | the common case in which the VMA sequence that we receive is a |
| 809 | list of locally sorted VMAs such as |
| 810 | p...z a...j (where a < j < p < z) |
| 811 | |
| 812 | Note: table->lcl_head is used to head an *actual* or *possible* |
| 813 | sequence within the list (such as a...j) that is not directly |
| 814 | headed by table->last_line |
| 815 | |
| 816 | Note: we may receive duplicate entries from 'decode_line_info'. */ |
| 817 | |
| 818 | if (!table->last_line |
| 819 | || new_line_sorts_after (info, table->last_line)) |
| 820 | { |
| 821 | /* Normal case: add 'info' to the beginning of the list */ |
| 822 | info->prev_line = table->last_line; |
| 823 | table->last_line = info; |
| 824 | |
| 825 | /* lcl_head: initialize to head a *possible* sequence at the end. */ |
| 826 | if (!table->lcl_head) |
| 827 | table->lcl_head = info; |
| 828 | } |
| 829 | else if (!new_line_sorts_after (info, table->lcl_head) |
| 830 | && (!table->lcl_head->prev_line |
| 831 | || new_line_sorts_after (info, table->lcl_head->prev_line))) |
| 832 | { |
| 833 | /* Abnormal but easy: lcl_head is the head of 'info'. */ |
| 834 | info->prev_line = table->lcl_head->prev_line; |
| 835 | table->lcl_head->prev_line = info; |
| 836 | } |
| 837 | else |
| 838 | { |
| 839 | /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid |
| 840 | heads for 'info'. Reset 'lcl_head'. */ |
| 841 | struct line_info* li2 = table->last_line; /* always non-NULL */ |
| 842 | struct line_info* li1 = li2->prev_line; |
| 843 | |
| 844 | while (li1) |
| 845 | { |
| 846 | if (!new_line_sorts_after (info, li2) |
| 847 | && new_line_sorts_after (info, li1)) |
| 848 | break; |
| 849 | |
| 850 | li2 = li1; /* always non-NULL */ |
| 851 | li1 = li1->prev_line; |
| 852 | } |
| 853 | table->lcl_head = li2; |
| 854 | info->prev_line = table->lcl_head->prev_line; |
| 855 | table->lcl_head->prev_line = info; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /* Extract a fully qualified filename from a line info table. |
| 860 | The returned string has been malloc'ed and it is the caller's |
| 861 | responsibility to free it. */ |
| 862 | |
| 863 | static char * |
| 864 | concat_filename (struct line_info_table *table, unsigned int file) |
| 865 | { |
| 866 | char *filename; |
| 867 | |
| 868 | if (file - 1 >= table->num_files) |
| 869 | { |
| 870 | /* FILE == 0 means unknown. */ |
| 871 | if (file) |
| 872 | (*_bfd_error_handler) |
| 873 | (_("Dwarf Error: mangled line number section (bad file number).")); |
| 874 | return strdup ("<unknown>"); |
| 875 | } |
| 876 | |
| 877 | filename = table->files[file - 1].name; |
| 878 | |
| 879 | if (!IS_ABSOLUTE_PATH (filename)) |
| 880 | { |
| 881 | char *dirname = NULL; |
| 882 | char *subdirname = NULL; |
| 883 | char *name; |
| 884 | size_t len; |
| 885 | |
| 886 | if (table->files[file - 1].dir) |
| 887 | subdirname = table->dirs[table->files[file - 1].dir - 1]; |
| 888 | |
| 889 | if (!subdirname || !IS_ABSOLUTE_PATH (subdirname)) |
| 890 | dirname = table->comp_dir; |
| 891 | |
| 892 | if (!dirname) |
| 893 | { |
| 894 | dirname = subdirname; |
| 895 | subdirname = NULL; |
| 896 | } |
| 897 | |
| 898 | if (!dirname) |
| 899 | return strdup (filename); |
| 900 | |
| 901 | len = strlen (dirname) + strlen (filename) + 2; |
| 902 | |
| 903 | if (subdirname) |
| 904 | { |
| 905 | len += strlen (subdirname) + 1; |
| 906 | name = bfd_malloc (len); |
| 907 | if (name) |
| 908 | sprintf (name, "%s/%s/%s", dirname, subdirname, filename); |
| 909 | } |
| 910 | else |
| 911 | { |
| 912 | name = bfd_malloc (len); |
| 913 | if (name) |
| 914 | sprintf (name, "%s/%s", dirname, filename); |
| 915 | } |
| 916 | |
| 917 | return name; |
| 918 | } |
| 919 | |
| 920 | return strdup (filename); |
| 921 | } |
| 922 | |
| 923 | static void |
| 924 | arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc) |
| 925 | { |
| 926 | struct arange *arange; |
| 927 | |
| 928 | /* If the first arange is empty, use it. */ |
| 929 | if (first_arange->high == 0) |
| 930 | { |
| 931 | first_arange->low = low_pc; |
| 932 | first_arange->high = high_pc; |
| 933 | return; |
| 934 | } |
| 935 | |
| 936 | /* Next see if we can cheaply extend an existing range. */ |
| 937 | arange = first_arange; |
| 938 | do |
| 939 | { |
| 940 | if (low_pc == arange->high) |
| 941 | { |
| 942 | arange->high = high_pc; |
| 943 | return; |
| 944 | } |
| 945 | if (high_pc == arange->low) |
| 946 | { |
| 947 | arange->low = low_pc; |
| 948 | return; |
| 949 | } |
| 950 | arange = arange->next; |
| 951 | } |
| 952 | while (arange); |
| 953 | |
| 954 | /* Need to allocate a new arange and insert it into the arange list. |
| 955 | Order isn't significant, so just insert after the first arange. */ |
| 956 | arange = bfd_zalloc (abfd, sizeof (*arange)); |
| 957 | arange->low = low_pc; |
| 958 | arange->high = high_pc; |
| 959 | arange->next = first_arange->next; |
| 960 | first_arange->next = arange; |
| 961 | } |
| 962 | |
| 963 | /* Decode the line number information for UNIT. */ |
| 964 | |
| 965 | static struct line_info_table* |
| 966 | decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash) |
| 967 | { |
| 968 | bfd *abfd = unit->abfd; |
| 969 | struct line_info_table* table; |
| 970 | bfd_byte *line_ptr; |
| 971 | bfd_byte *line_end; |
| 972 | struct line_head lh; |
| 973 | unsigned int i, bytes_read, offset_size; |
| 974 | char *cur_file, *cur_dir; |
| 975 | unsigned char op_code, extended_op, adj_opcode; |
| 976 | bfd_size_type amt; |
| 977 | |
| 978 | if (! stash->dwarf_line_buffer) |
| 979 | { |
| 980 | asection *msec; |
| 981 | |
| 982 | msec = bfd_get_section_by_name (abfd, ".debug_line"); |
| 983 | if (! msec) |
| 984 | { |
| 985 | (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section.")); |
| 986 | bfd_set_error (bfd_error_bad_value); |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | stash->dwarf_line_size = msec->size; |
| 991 | stash->dwarf_line_buffer |
| 992 | = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, |
| 993 | stash->syms); |
| 994 | if (! stash->dwarf_line_buffer) |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | /* It is possible to get a bad value for the line_offset. Validate |
| 999 | it here so that we won't get a segfault below. */ |
| 1000 | if (unit->line_offset >= stash->dwarf_line_size) |
| 1001 | { |
| 1002 | (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."), |
| 1003 | unit->line_offset, stash->dwarf_line_size); |
| 1004 | bfd_set_error (bfd_error_bad_value); |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | amt = sizeof (struct line_info_table); |
| 1009 | table = bfd_alloc (abfd, amt); |
| 1010 | table->abfd = abfd; |
| 1011 | table->comp_dir = unit->comp_dir; |
| 1012 | |
| 1013 | table->num_files = 0; |
| 1014 | table->files = NULL; |
| 1015 | |
| 1016 | table->num_dirs = 0; |
| 1017 | table->dirs = NULL; |
| 1018 | |
| 1019 | table->files = NULL; |
| 1020 | table->last_line = NULL; |
| 1021 | table->lcl_head = NULL; |
| 1022 | |
| 1023 | line_ptr = stash->dwarf_line_buffer + unit->line_offset; |
| 1024 | |
| 1025 | /* Read in the prologue. */ |
| 1026 | lh.total_length = read_4_bytes (abfd, line_ptr); |
| 1027 | line_ptr += 4; |
| 1028 | offset_size = 4; |
| 1029 | if (lh.total_length == 0xffffffff) |
| 1030 | { |
| 1031 | lh.total_length = read_8_bytes (abfd, line_ptr); |
| 1032 | line_ptr += 8; |
| 1033 | offset_size = 8; |
| 1034 | } |
| 1035 | else if (lh.total_length == 0 && unit->addr_size == 8) |
| 1036 | { |
| 1037 | /* Handle (non-standard) 64-bit DWARF2 formats. */ |
| 1038 | lh.total_length = read_4_bytes (abfd, line_ptr); |
| 1039 | line_ptr += 4; |
| 1040 | offset_size = 8; |
| 1041 | } |
| 1042 | line_end = line_ptr + lh.total_length; |
| 1043 | lh.version = read_2_bytes (abfd, line_ptr); |
| 1044 | line_ptr += 2; |
| 1045 | if (offset_size == 4) |
| 1046 | lh.prologue_length = read_4_bytes (abfd, line_ptr); |
| 1047 | else |
| 1048 | lh.prologue_length = read_8_bytes (abfd, line_ptr); |
| 1049 | line_ptr += offset_size; |
| 1050 | lh.minimum_instruction_length = read_1_byte (abfd, line_ptr); |
| 1051 | line_ptr += 1; |
| 1052 | lh.default_is_stmt = read_1_byte (abfd, line_ptr); |
| 1053 | line_ptr += 1; |
| 1054 | lh.line_base = read_1_signed_byte (abfd, line_ptr); |
| 1055 | line_ptr += 1; |
| 1056 | lh.line_range = read_1_byte (abfd, line_ptr); |
| 1057 | line_ptr += 1; |
| 1058 | lh.opcode_base = read_1_byte (abfd, line_ptr); |
| 1059 | line_ptr += 1; |
| 1060 | amt = lh.opcode_base * sizeof (unsigned char); |
| 1061 | lh.standard_opcode_lengths = bfd_alloc (abfd, amt); |
| 1062 | |
| 1063 | lh.standard_opcode_lengths[0] = 1; |
| 1064 | |
| 1065 | for (i = 1; i < lh.opcode_base; ++i) |
| 1066 | { |
| 1067 | lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr); |
| 1068 | line_ptr += 1; |
| 1069 | } |
| 1070 | |
| 1071 | /* Read directory table. */ |
| 1072 | while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL) |
| 1073 | { |
| 1074 | line_ptr += bytes_read; |
| 1075 | |
| 1076 | if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0) |
| 1077 | { |
| 1078 | char **tmp; |
| 1079 | |
| 1080 | amt = table->num_dirs + DIR_ALLOC_CHUNK; |
| 1081 | amt *= sizeof (char *); |
| 1082 | |
| 1083 | tmp = bfd_realloc (table->dirs, amt); |
| 1084 | if (tmp == NULL) |
| 1085 | { |
| 1086 | free (table->dirs); |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | table->dirs = tmp; |
| 1090 | } |
| 1091 | |
| 1092 | table->dirs[table->num_dirs++] = cur_dir; |
| 1093 | } |
| 1094 | |
| 1095 | line_ptr += bytes_read; |
| 1096 | |
| 1097 | /* Read file name table. */ |
| 1098 | while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL) |
| 1099 | { |
| 1100 | line_ptr += bytes_read; |
| 1101 | |
| 1102 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) |
| 1103 | { |
| 1104 | struct fileinfo *tmp; |
| 1105 | |
| 1106 | amt = table->num_files + FILE_ALLOC_CHUNK; |
| 1107 | amt *= sizeof (struct fileinfo); |
| 1108 | |
| 1109 | tmp = bfd_realloc (table->files, amt); |
| 1110 | if (tmp == NULL) |
| 1111 | { |
| 1112 | free (table->files); |
| 1113 | free (table->dirs); |
| 1114 | return NULL; |
| 1115 | } |
| 1116 | table->files = tmp; |
| 1117 | } |
| 1118 | |
| 1119 | table->files[table->num_files].name = cur_file; |
| 1120 | table->files[table->num_files].dir = |
| 1121 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1122 | line_ptr += bytes_read; |
| 1123 | table->files[table->num_files].time = |
| 1124 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1125 | line_ptr += bytes_read; |
| 1126 | table->files[table->num_files].size = |
| 1127 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1128 | line_ptr += bytes_read; |
| 1129 | table->num_files++; |
| 1130 | } |
| 1131 | |
| 1132 | line_ptr += bytes_read; |
| 1133 | |
| 1134 | /* Read the statement sequences until there's nothing left. */ |
| 1135 | while (line_ptr < line_end) |
| 1136 | { |
| 1137 | /* State machine registers. */ |
| 1138 | bfd_vma address = 0; |
| 1139 | char * filename = table->num_files ? concat_filename (table, 1) : NULL; |
| 1140 | unsigned int line = 1; |
| 1141 | unsigned int column = 0; |
| 1142 | int is_stmt = lh.default_is_stmt; |
| 1143 | int end_sequence = 0; |
| 1144 | /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some |
| 1145 | compilers generate address sequences that are wildly out of |
| 1146 | order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler |
| 1147 | for ia64-Linux). Thus, to determine the low and high |
| 1148 | address, we must compare on every DW_LNS_copy, etc. */ |
| 1149 | bfd_vma low_pc = (bfd_vma) -1; |
| 1150 | bfd_vma high_pc = 0; |
| 1151 | |
| 1152 | /* Decode the table. */ |
| 1153 | while (! end_sequence) |
| 1154 | { |
| 1155 | op_code = read_1_byte (abfd, line_ptr); |
| 1156 | line_ptr += 1; |
| 1157 | |
| 1158 | if (op_code >= lh.opcode_base) |
| 1159 | { |
| 1160 | /* Special operand. */ |
| 1161 | adj_opcode = op_code - lh.opcode_base; |
| 1162 | address += (adj_opcode / lh.line_range) |
| 1163 | * lh.minimum_instruction_length; |
| 1164 | line += lh.line_base + (adj_opcode % lh.line_range); |
| 1165 | /* Append row to matrix using current values. */ |
| 1166 | add_line_info (table, address, filename, line, column, 0); |
| 1167 | if (address < low_pc) |
| 1168 | low_pc = address; |
| 1169 | if (address > high_pc) |
| 1170 | high_pc = address; |
| 1171 | } |
| 1172 | else switch (op_code) |
| 1173 | { |
| 1174 | case DW_LNS_extended_op: |
| 1175 | /* Ignore length. */ |
| 1176 | line_ptr += 1; |
| 1177 | extended_op = read_1_byte (abfd, line_ptr); |
| 1178 | line_ptr += 1; |
| 1179 | |
| 1180 | switch (extended_op) |
| 1181 | { |
| 1182 | case DW_LNE_end_sequence: |
| 1183 | end_sequence = 1; |
| 1184 | add_line_info (table, address, filename, line, column, |
| 1185 | end_sequence); |
| 1186 | if (address < low_pc) |
| 1187 | low_pc = address; |
| 1188 | if (address > high_pc) |
| 1189 | high_pc = address; |
| 1190 | arange_add (unit->abfd, &unit->arange, low_pc, high_pc); |
| 1191 | break; |
| 1192 | case DW_LNE_set_address: |
| 1193 | address = read_address (unit, line_ptr); |
| 1194 | line_ptr += unit->addr_size; |
| 1195 | break; |
| 1196 | case DW_LNE_define_file: |
| 1197 | cur_file = read_string (abfd, line_ptr, &bytes_read); |
| 1198 | line_ptr += bytes_read; |
| 1199 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) |
| 1200 | { |
| 1201 | struct fileinfo *tmp; |
| 1202 | |
| 1203 | amt = table->num_files + FILE_ALLOC_CHUNK; |
| 1204 | amt *= sizeof (struct fileinfo); |
| 1205 | tmp = bfd_realloc (table->files, amt); |
| 1206 | if (tmp == NULL) |
| 1207 | { |
| 1208 | free (table->files); |
| 1209 | free (table->dirs); |
| 1210 | free (filename); |
| 1211 | return NULL; |
| 1212 | } |
| 1213 | table->files = tmp; |
| 1214 | } |
| 1215 | table->files[table->num_files].name = cur_file; |
| 1216 | table->files[table->num_files].dir = |
| 1217 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1218 | line_ptr += bytes_read; |
| 1219 | table->files[table->num_files].time = |
| 1220 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1221 | line_ptr += bytes_read; |
| 1222 | table->files[table->num_files].size = |
| 1223 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1224 | line_ptr += bytes_read; |
| 1225 | table->num_files++; |
| 1226 | break; |
| 1227 | default: |
| 1228 | (*_bfd_error_handler) (_("Dwarf Error: mangled line number section.")); |
| 1229 | bfd_set_error (bfd_error_bad_value); |
| 1230 | free (filename); |
| 1231 | free (table->files); |
| 1232 | free (table->dirs); |
| 1233 | return NULL; |
| 1234 | } |
| 1235 | break; |
| 1236 | case DW_LNS_copy: |
| 1237 | add_line_info (table, address, filename, line, column, 0); |
| 1238 | if (address < low_pc) |
| 1239 | low_pc = address; |
| 1240 | if (address > high_pc) |
| 1241 | high_pc = address; |
| 1242 | break; |
| 1243 | case DW_LNS_advance_pc: |
| 1244 | address += lh.minimum_instruction_length |
| 1245 | * read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1246 | line_ptr += bytes_read; |
| 1247 | break; |
| 1248 | case DW_LNS_advance_line: |
| 1249 | line += read_signed_leb128 (abfd, line_ptr, &bytes_read); |
| 1250 | line_ptr += bytes_read; |
| 1251 | break; |
| 1252 | case DW_LNS_set_file: |
| 1253 | { |
| 1254 | unsigned int file; |
| 1255 | |
| 1256 | /* The file and directory tables are 0 |
| 1257 | based, the references are 1 based. */ |
| 1258 | file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1259 | line_ptr += bytes_read; |
| 1260 | if (filename) |
| 1261 | free (filename); |
| 1262 | filename = concat_filename (table, file); |
| 1263 | break; |
| 1264 | } |
| 1265 | case DW_LNS_set_column: |
| 1266 | column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1267 | line_ptr += bytes_read; |
| 1268 | break; |
| 1269 | case DW_LNS_negate_stmt: |
| 1270 | is_stmt = (!is_stmt); |
| 1271 | break; |
| 1272 | case DW_LNS_set_basic_block: |
| 1273 | break; |
| 1274 | case DW_LNS_const_add_pc: |
| 1275 | address += lh.minimum_instruction_length |
| 1276 | * ((255 - lh.opcode_base) / lh.line_range); |
| 1277 | break; |
| 1278 | case DW_LNS_fixed_advance_pc: |
| 1279 | address += read_2_bytes (abfd, line_ptr); |
| 1280 | line_ptr += 2; |
| 1281 | break; |
| 1282 | default: |
| 1283 | { |
| 1284 | int i; |
| 1285 | |
| 1286 | /* Unknown standard opcode, ignore it. */ |
| 1287 | for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++) |
| 1288 | { |
| 1289 | (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1290 | line_ptr += bytes_read; |
| 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | if (filename) |
| 1297 | free (filename); |
| 1298 | } |
| 1299 | |
| 1300 | return table; |
| 1301 | } |
| 1302 | |
| 1303 | /* If ADDR is within TABLE set the output parameters and return TRUE, |
| 1304 | otherwise return FALSE. The output parameters, FILENAME_PTR and |
| 1305 | LINENUMBER_PTR, are pointers to the objects to be filled in. */ |
| 1306 | |
| 1307 | static bfd_boolean |
| 1308 | lookup_address_in_line_info_table (struct line_info_table *table, |
| 1309 | bfd_vma addr, |
| 1310 | struct funcinfo *function, |
| 1311 | const char **filename_ptr, |
| 1312 | unsigned int *linenumber_ptr) |
| 1313 | { |
| 1314 | /* Note: table->last_line should be a descendingly sorted list. */ |
| 1315 | struct line_info* next_line = table->last_line; |
| 1316 | struct line_info* each_line = NULL; |
| 1317 | *filename_ptr = NULL; |
| 1318 | |
| 1319 | if (!next_line) |
| 1320 | return FALSE; |
| 1321 | |
| 1322 | each_line = next_line->prev_line; |
| 1323 | |
| 1324 | /* Check for large addresses */ |
| 1325 | if (addr > next_line->address) |
| 1326 | each_line = NULL; /* ensure we skip over the normal case */ |
| 1327 | |
| 1328 | /* Normal case: search the list; save */ |
| 1329 | while (each_line && next_line) |
| 1330 | { |
| 1331 | /* If we have an address match, save this info. This allows us |
| 1332 | to return as good as results as possible for strange debugging |
| 1333 | info. */ |
| 1334 | bfd_boolean addr_match = FALSE; |
| 1335 | if (each_line->address <= addr && addr < next_line->address) |
| 1336 | { |
| 1337 | addr_match = TRUE; |
| 1338 | |
| 1339 | /* If this line appears to span functions, and addr is in the |
| 1340 | later function, return the first line of that function instead |
| 1341 | of the last line of the earlier one. This check is for GCC |
| 1342 | 2.95, which emits the first line number for a function late. */ |
| 1343 | |
| 1344 | if (function != NULL) |
| 1345 | { |
| 1346 | bfd_vma lowest_pc; |
| 1347 | struct arange *arange; |
| 1348 | |
| 1349 | /* Find the lowest address in the function's range list */ |
| 1350 | lowest_pc = function->arange.low; |
| 1351 | for (arange = &function->arange; |
| 1352 | arange; |
| 1353 | arange = arange->next) |
| 1354 | { |
| 1355 | if (function->arange.low < lowest_pc) |
| 1356 | lowest_pc = function->arange.low; |
| 1357 | } |
| 1358 | /* Check for spanning function and set outgoing line info */ |
| 1359 | if (addr >= lowest_pc |
| 1360 | && each_line->address < lowest_pc |
| 1361 | && next_line->address > lowest_pc) |
| 1362 | { |
| 1363 | *filename_ptr = next_line->filename; |
| 1364 | *linenumber_ptr = next_line->line; |
| 1365 | } |
| 1366 | else |
| 1367 | { |
| 1368 | *filename_ptr = each_line->filename; |
| 1369 | *linenumber_ptr = each_line->line; |
| 1370 | } |
| 1371 | } |
| 1372 | else |
| 1373 | { |
| 1374 | *filename_ptr = each_line->filename; |
| 1375 | *linenumber_ptr = each_line->line; |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | if (addr_match && !each_line->end_sequence) |
| 1380 | return TRUE; /* we have definitely found what we want */ |
| 1381 | |
| 1382 | next_line = each_line; |
| 1383 | each_line = each_line->prev_line; |
| 1384 | } |
| 1385 | |
| 1386 | /* At this point each_line is NULL but next_line is not. If we found |
| 1387 | a candidate end-of-sequence point in the loop above, we can return |
| 1388 | that (compatibility with a bug in the Intel compiler); otherwise, |
| 1389 | assuming that we found the containing function for this address in |
| 1390 | this compilation unit, return the first line we have a number for |
| 1391 | (compatibility with GCC 2.95). */ |
| 1392 | if (*filename_ptr == NULL && function != NULL) |
| 1393 | { |
| 1394 | *filename_ptr = next_line->filename; |
| 1395 | *linenumber_ptr = next_line->line; |
| 1396 | return TRUE; |
| 1397 | } |
| 1398 | |
| 1399 | return FALSE; |
| 1400 | } |
| 1401 | |
| 1402 | /* Read in the .debug_ranges section for future reference */ |
| 1403 | |
| 1404 | static bfd_boolean |
| 1405 | read_debug_ranges (struct comp_unit *unit) |
| 1406 | { |
| 1407 | struct dwarf2_debug *stash = unit->stash; |
| 1408 | if (! stash->dwarf_ranges_buffer) |
| 1409 | { |
| 1410 | bfd *abfd = unit->abfd; |
| 1411 | asection *msec; |
| 1412 | |
| 1413 | msec = bfd_get_section_by_name (abfd, ".debug_ranges"); |
| 1414 | if (! msec) |
| 1415 | { |
| 1416 | (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section.")); |
| 1417 | bfd_set_error (bfd_error_bad_value); |
| 1418 | return FALSE; |
| 1419 | } |
| 1420 | |
| 1421 | stash->dwarf_ranges_size = msec->size; |
| 1422 | stash->dwarf_ranges_buffer |
| 1423 | = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, |
| 1424 | stash->syms); |
| 1425 | if (! stash->dwarf_ranges_buffer) |
| 1426 | return FALSE; |
| 1427 | } |
| 1428 | return TRUE; |
| 1429 | } |
| 1430 | |
| 1431 | /* Function table functions. */ |
| 1432 | |
| 1433 | /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. |
| 1434 | Note that we need to find the function that has the smallest |
| 1435 | range that contains ADDR, to handle inlined functions without |
| 1436 | depending upon them being ordered in TABLE by increasing range. */ |
| 1437 | |
| 1438 | static bfd_boolean |
| 1439 | lookup_address_in_function_table (struct comp_unit *unit, |
| 1440 | bfd_vma addr, |
| 1441 | struct funcinfo **function_ptr, |
| 1442 | const char **functionname_ptr) |
| 1443 | { |
| 1444 | struct funcinfo* each_func; |
| 1445 | struct funcinfo* best_fit = NULL; |
| 1446 | struct arange *arange; |
| 1447 | |
| 1448 | for (each_func = unit->function_table; |
| 1449 | each_func; |
| 1450 | each_func = each_func->prev_func) |
| 1451 | { |
| 1452 | for (arange = &each_func->arange; |
| 1453 | arange; |
| 1454 | arange = arange->next) |
| 1455 | { |
| 1456 | if (addr >= arange->low && addr < arange->high) |
| 1457 | { |
| 1458 | if (!best_fit || |
| 1459 | ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low))) |
| 1460 | best_fit = each_func; |
| 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | if (best_fit) |
| 1466 | { |
| 1467 | *functionname_ptr = best_fit->name; |
| 1468 | *function_ptr = best_fit; |
| 1469 | return TRUE; |
| 1470 | } |
| 1471 | else |
| 1472 | { |
| 1473 | return FALSE; |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR |
| 1478 | and LINENUMBER_PTR, and return TRUE. */ |
| 1479 | |
| 1480 | static bfd_boolean |
| 1481 | lookup_symbol_in_function_table (struct comp_unit *unit, |
| 1482 | asymbol *sym, |
| 1483 | bfd_vma addr, |
| 1484 | const char **filename_ptr, |
| 1485 | unsigned int *linenumber_ptr) |
| 1486 | { |
| 1487 | struct funcinfo* each_func; |
| 1488 | struct funcinfo* best_fit = NULL; |
| 1489 | struct arange *arange; |
| 1490 | const char *name = bfd_asymbol_name (sym); |
| 1491 | asection *sec = bfd_get_section (sym); |
| 1492 | |
| 1493 | for (each_func = unit->function_table; |
| 1494 | each_func; |
| 1495 | each_func = each_func->prev_func) |
| 1496 | { |
| 1497 | for (arange = &each_func->arange; |
| 1498 | arange; |
| 1499 | arange = arange->next) |
| 1500 | { |
| 1501 | if ((!each_func->sec || each_func->sec == sec) |
| 1502 | && addr >= arange->low |
| 1503 | && addr < arange->high |
| 1504 | && each_func->name |
| 1505 | && strcmp (name, each_func->name) == 0 |
| 1506 | && (!best_fit |
| 1507 | || ((arange->high - arange->low) |
| 1508 | < (best_fit->arange.high - best_fit->arange.low)))) |
| 1509 | best_fit = each_func; |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | if (best_fit) |
| 1514 | { |
| 1515 | best_fit->sec = sec; |
| 1516 | *filename_ptr = best_fit->file; |
| 1517 | *linenumber_ptr = best_fit->line; |
| 1518 | return TRUE; |
| 1519 | } |
| 1520 | else |
| 1521 | return FALSE; |
| 1522 | } |
| 1523 | |
| 1524 | /* Variable table functions. */ |
| 1525 | |
| 1526 | /* If SYM is within variable table of UNIT, set FILENAME_PTR and |
| 1527 | LINENUMBER_PTR, and return TRUE. */ |
| 1528 | |
| 1529 | static bfd_boolean |
| 1530 | lookup_symbol_in_variable_table (struct comp_unit *unit, |
| 1531 | asymbol *sym, |
| 1532 | bfd_vma addr, |
| 1533 | const char **filename_ptr, |
| 1534 | unsigned int *linenumber_ptr) |
| 1535 | { |
| 1536 | const char *name = bfd_asymbol_name (sym); |
| 1537 | asection *sec = bfd_get_section (sym); |
| 1538 | struct varinfo* each; |
| 1539 | |
| 1540 | for (each = unit->variable_table; each; each = each->prev_var) |
| 1541 | if (each->stack == 0 |
| 1542 | && each->file != NULL |
| 1543 | && each->name != NULL |
| 1544 | && each->addr == addr |
| 1545 | && (!each->sec || each->sec == sec) |
| 1546 | && strcmp (name, each->name) == 0) |
| 1547 | break; |
| 1548 | |
| 1549 | if (each) |
| 1550 | { |
| 1551 | each->sec = sec; |
| 1552 | *filename_ptr = each->file; |
| 1553 | *linenumber_ptr = each->line; |
| 1554 | return TRUE; |
| 1555 | } |
| 1556 | else |
| 1557 | return FALSE; |
| 1558 | } |
| 1559 | |
| 1560 | static char * |
| 1561 | find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref) |
| 1562 | { |
| 1563 | bfd *abfd = unit->abfd; |
| 1564 | bfd_byte *info_ptr; |
| 1565 | unsigned int abbrev_number, bytes_read, i; |
| 1566 | struct abbrev_info *abbrev; |
| 1567 | struct attribute attr; |
| 1568 | char *name = 0; |
| 1569 | |
| 1570 | info_ptr = unit->info_ptr_unit + die_ref; |
| 1571 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 1572 | info_ptr += bytes_read; |
| 1573 | |
| 1574 | if (abbrev_number) |
| 1575 | { |
| 1576 | abbrev = lookup_abbrev (abbrev_number, unit->abbrevs); |
| 1577 | if (! abbrev) |
| 1578 | { |
| 1579 | (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), |
| 1580 | abbrev_number); |
| 1581 | bfd_set_error (bfd_error_bad_value); |
| 1582 | } |
| 1583 | else |
| 1584 | { |
| 1585 | for (i = 0; i < abbrev->num_attrs; ++i) |
| 1586 | { |
| 1587 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); |
| 1588 | switch (attr.name) |
| 1589 | { |
| 1590 | case DW_AT_name: |
| 1591 | /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */ |
| 1592 | if (name == NULL) |
| 1593 | name = attr.u.str; |
| 1594 | break; |
| 1595 | case DW_AT_specification: |
| 1596 | name = find_abstract_instance_name (unit, attr.u.val); |
| 1597 | break; |
| 1598 | case DW_AT_MIPS_linkage_name: |
| 1599 | name = attr.u.str; |
| 1600 | break; |
| 1601 | default: |
| 1602 | break; |
| 1603 | } |
| 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | return (name); |
| 1608 | } |
| 1609 | |
| 1610 | static void |
| 1611 | read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset) |
| 1612 | { |
| 1613 | bfd_byte *ranges_ptr; |
| 1614 | bfd_vma base_address = unit->base_address; |
| 1615 | |
| 1616 | if (! unit->stash->dwarf_ranges_buffer) |
| 1617 | { |
| 1618 | if (! read_debug_ranges (unit)) |
| 1619 | return; |
| 1620 | } |
| 1621 | ranges_ptr = unit->stash->dwarf_ranges_buffer + offset; |
| 1622 | |
| 1623 | for (;;) |
| 1624 | { |
| 1625 | bfd_vma low_pc; |
| 1626 | bfd_vma high_pc; |
| 1627 | |
| 1628 | if (unit->addr_size == 4) |
| 1629 | { |
| 1630 | low_pc = read_4_bytes (unit->abfd, ranges_ptr); |
| 1631 | ranges_ptr += 4; |
| 1632 | high_pc = read_4_bytes (unit->abfd, ranges_ptr); |
| 1633 | ranges_ptr += 4; |
| 1634 | } |
| 1635 | else |
| 1636 | { |
| 1637 | low_pc = read_8_bytes (unit->abfd, ranges_ptr); |
| 1638 | ranges_ptr += 8; |
| 1639 | high_pc = read_8_bytes (unit->abfd, ranges_ptr); |
| 1640 | ranges_ptr += 8; |
| 1641 | } |
| 1642 | if (low_pc == 0 && high_pc == 0) |
| 1643 | break; |
| 1644 | if (low_pc == -1UL && high_pc != -1UL) |
| 1645 | base_address = high_pc; |
| 1646 | else |
| 1647 | arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc); |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | /* DWARF2 Compilation unit functions. */ |
| 1652 | |
| 1653 | /* Scan over each die in a comp. unit looking for functions to add |
| 1654 | to the function table and variables to the variable table. */ |
| 1655 | |
| 1656 | static bfd_boolean |
| 1657 | scan_unit_for_symbols (struct comp_unit *unit) |
| 1658 | { |
| 1659 | bfd *abfd = unit->abfd; |
| 1660 | bfd_byte *info_ptr = unit->first_child_die_ptr; |
| 1661 | int nesting_level = 1; |
| 1662 | struct funcinfo **nested_funcs; |
| 1663 | int nested_funcs_size; |
| 1664 | |
| 1665 | /* Maintain a stack of in-scope functions and inlined functions, which we |
| 1666 | can use to set the caller_func field. */ |
| 1667 | nested_funcs_size = 32; |
| 1668 | nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *)); |
| 1669 | if (nested_funcs == NULL) |
| 1670 | return FALSE; |
| 1671 | nested_funcs[nesting_level] = 0; |
| 1672 | |
| 1673 | while (nesting_level) |
| 1674 | { |
| 1675 | unsigned int abbrev_number, bytes_read, i; |
| 1676 | struct abbrev_info *abbrev; |
| 1677 | struct attribute attr; |
| 1678 | struct funcinfo *func; |
| 1679 | struct varinfo *var; |
| 1680 | bfd_vma low_pc = 0; |
| 1681 | bfd_vma high_pc = 0; |
| 1682 | |
| 1683 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 1684 | info_ptr += bytes_read; |
| 1685 | |
| 1686 | if (! abbrev_number) |
| 1687 | { |
| 1688 | nesting_level--; |
| 1689 | continue; |
| 1690 | } |
| 1691 | |
| 1692 | abbrev = lookup_abbrev (abbrev_number,unit->abbrevs); |
| 1693 | if (! abbrev) |
| 1694 | { |
| 1695 | (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), |
| 1696 | abbrev_number); |
| 1697 | bfd_set_error (bfd_error_bad_value); |
| 1698 | free (nested_funcs); |
| 1699 | return FALSE; |
| 1700 | } |
| 1701 | |
| 1702 | var = NULL; |
| 1703 | if (abbrev->tag == DW_TAG_subprogram |
| 1704 | || abbrev->tag == DW_TAG_entry_point |
| 1705 | || abbrev->tag == DW_TAG_inlined_subroutine) |
| 1706 | { |
| 1707 | bfd_size_type amt = sizeof (struct funcinfo); |
| 1708 | func = bfd_zalloc (abfd, amt); |
| 1709 | func->tag = abbrev->tag; |
| 1710 | func->prev_func = unit->function_table; |
| 1711 | unit->function_table = func; |
| 1712 | |
| 1713 | if (func->tag == DW_TAG_inlined_subroutine) |
| 1714 | for (i = nesting_level - 1; i >= 1; i--) |
| 1715 | if (nested_funcs[i]) |
| 1716 | { |
| 1717 | func->caller_func = nested_funcs[i]; |
| 1718 | break; |
| 1719 | } |
| 1720 | nested_funcs[nesting_level] = func; |
| 1721 | } |
| 1722 | else |
| 1723 | { |
| 1724 | func = NULL; |
| 1725 | if (abbrev->tag == DW_TAG_variable) |
| 1726 | { |
| 1727 | bfd_size_type amt = sizeof (struct varinfo); |
| 1728 | var = bfd_zalloc (abfd, amt); |
| 1729 | var->tag = abbrev->tag; |
| 1730 | var->stack = 1; |
| 1731 | var->prev_var = unit->variable_table; |
| 1732 | unit->variable_table = var; |
| 1733 | } |
| 1734 | |
| 1735 | /* No inline function in scope at this nesting level. */ |
| 1736 | nested_funcs[nesting_level] = 0; |
| 1737 | } |
| 1738 | |
| 1739 | for (i = 0; i < abbrev->num_attrs; ++i) |
| 1740 | { |
| 1741 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); |
| 1742 | |
| 1743 | if (func) |
| 1744 | { |
| 1745 | switch (attr.name) |
| 1746 | { |
| 1747 | case DW_AT_call_file: |
| 1748 | func->caller_file = concat_filename (unit->line_table, attr.u.val); |
| 1749 | break; |
| 1750 | |
| 1751 | case DW_AT_call_line: |
| 1752 | func->caller_line = attr.u.val; |
| 1753 | break; |
| 1754 | |
| 1755 | case DW_AT_abstract_origin: |
| 1756 | func->name = find_abstract_instance_name (unit, attr.u.val); |
| 1757 | break; |
| 1758 | |
| 1759 | case DW_AT_name: |
| 1760 | /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */ |
| 1761 | if (func->name == NULL) |
| 1762 | func->name = attr.u.str; |
| 1763 | break; |
| 1764 | |
| 1765 | case DW_AT_MIPS_linkage_name: |
| 1766 | func->name = attr.u.str; |
| 1767 | break; |
| 1768 | |
| 1769 | case DW_AT_low_pc: |
| 1770 | low_pc = attr.u.val; |
| 1771 | break; |
| 1772 | |
| 1773 | case DW_AT_high_pc: |
| 1774 | high_pc = attr.u.val; |
| 1775 | break; |
| 1776 | |
| 1777 | case DW_AT_ranges: |
| 1778 | read_rangelist (unit, &func->arange, attr.u.val); |
| 1779 | break; |
| 1780 | |
| 1781 | case DW_AT_decl_file: |
| 1782 | func->file = concat_filename (unit->line_table, |
| 1783 | attr.u.val); |
| 1784 | break; |
| 1785 | |
| 1786 | case DW_AT_decl_line: |
| 1787 | func->line = attr.u.val; |
| 1788 | break; |
| 1789 | |
| 1790 | default: |
| 1791 | break; |
| 1792 | } |
| 1793 | } |
| 1794 | else if (var) |
| 1795 | { |
| 1796 | switch (attr.name) |
| 1797 | { |
| 1798 | case DW_AT_name: |
| 1799 | var->name = attr.u.str; |
| 1800 | break; |
| 1801 | |
| 1802 | case DW_AT_decl_file: |
| 1803 | var->file = concat_filename (unit->line_table, |
| 1804 | attr.u.val); |
| 1805 | break; |
| 1806 | |
| 1807 | case DW_AT_decl_line: |
| 1808 | var->line = attr.u.val; |
| 1809 | break; |
| 1810 | |
| 1811 | case DW_AT_external: |
| 1812 | if (attr.u.val != 0) |
| 1813 | var->stack = 0; |
| 1814 | break; |
| 1815 | |
| 1816 | case DW_AT_location: |
| 1817 | switch (attr.form) |
| 1818 | { |
| 1819 | case DW_FORM_block: |
| 1820 | case DW_FORM_block1: |
| 1821 | case DW_FORM_block2: |
| 1822 | case DW_FORM_block4: |
| 1823 | if (*attr.u.blk->data == DW_OP_addr) |
| 1824 | { |
| 1825 | var->stack = 0; |
| 1826 | |
| 1827 | /* Verify that DW_OP_addr is the only opcode in the |
| 1828 | location, in which case the block size will be 1 |
| 1829 | plus the address size. */ |
| 1830 | /* ??? For TLS variables, gcc can emit |
| 1831 | DW_OP_addr <addr> DW_OP_GNU_push_tls_address |
| 1832 | which we don't handle here yet. */ |
| 1833 | if (attr.u.blk->size == unit->addr_size + 1U) |
| 1834 | var->addr = bfd_get (unit->addr_size * 8, |
| 1835 | unit->abfd, |
| 1836 | attr.u.blk->data + 1); |
| 1837 | } |
| 1838 | break; |
| 1839 | |
| 1840 | default: |
| 1841 | break; |
| 1842 | } |
| 1843 | break; |
| 1844 | |
| 1845 | default: |
| 1846 | break; |
| 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | if (func && high_pc != 0) |
| 1852 | { |
| 1853 | arange_add (unit->abfd, &func->arange, low_pc, high_pc); |
| 1854 | } |
| 1855 | |
| 1856 | if (abbrev->has_children) |
| 1857 | { |
| 1858 | nesting_level++; |
| 1859 | |
| 1860 | if (nesting_level >= nested_funcs_size) |
| 1861 | { |
| 1862 | struct funcinfo **tmp; |
| 1863 | |
| 1864 | nested_funcs_size *= 2; |
| 1865 | tmp = bfd_realloc (nested_funcs, |
| 1866 | (nested_funcs_size |
| 1867 | * sizeof (struct funcinfo *))); |
| 1868 | if (tmp == NULL) |
| 1869 | { |
| 1870 | free (nested_funcs); |
| 1871 | return FALSE; |
| 1872 | } |
| 1873 | nested_funcs = tmp; |
| 1874 | } |
| 1875 | nested_funcs[nesting_level] = 0; |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | free (nested_funcs); |
| 1880 | return TRUE; |
| 1881 | } |
| 1882 | |
| 1883 | /* Parse a DWARF2 compilation unit starting at INFO_PTR. This |
| 1884 | includes the compilation unit header that proceeds the DIE's, but |
| 1885 | does not include the length field that precedes each compilation |
| 1886 | unit header. END_PTR points one past the end of this comp unit. |
| 1887 | OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes). |
| 1888 | |
| 1889 | This routine does not read the whole compilation unit; only enough |
| 1890 | to get to the line number information for the compilation unit. */ |
| 1891 | |
| 1892 | static struct comp_unit * |
| 1893 | parse_comp_unit (struct dwarf2_debug *stash, |
| 1894 | bfd_vma unit_length, |
| 1895 | bfd_byte *info_ptr_unit, |
| 1896 | unsigned int offset_size) |
| 1897 | { |
| 1898 | struct comp_unit* unit; |
| 1899 | unsigned int version; |
| 1900 | bfd_uint64_t abbrev_offset = 0; |
| 1901 | unsigned int addr_size; |
| 1902 | struct abbrev_info** abbrevs; |
| 1903 | unsigned int abbrev_number, bytes_read, i; |
| 1904 | struct abbrev_info *abbrev; |
| 1905 | struct attribute attr; |
| 1906 | bfd_byte *info_ptr = stash->info_ptr; |
| 1907 | bfd_byte *end_ptr = info_ptr + unit_length; |
| 1908 | bfd_size_type amt; |
| 1909 | bfd_vma low_pc = 0; |
| 1910 | bfd_vma high_pc = 0; |
| 1911 | bfd *abfd = stash->bfd; |
| 1912 | |
| 1913 | version = read_2_bytes (abfd, info_ptr); |
| 1914 | info_ptr += 2; |
| 1915 | BFD_ASSERT (offset_size == 4 || offset_size == 8); |
| 1916 | if (offset_size == 4) |
| 1917 | abbrev_offset = read_4_bytes (abfd, info_ptr); |
| 1918 | else |
| 1919 | abbrev_offset = read_8_bytes (abfd, info_ptr); |
| 1920 | info_ptr += offset_size; |
| 1921 | addr_size = read_1_byte (abfd, info_ptr); |
| 1922 | info_ptr += 1; |
| 1923 | |
| 1924 | if (version != 2) |
| 1925 | { |
| 1926 | (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version); |
| 1927 | bfd_set_error (bfd_error_bad_value); |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
| 1931 | if (addr_size > sizeof (bfd_vma)) |
| 1932 | { |
| 1933 | (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."), |
| 1934 | addr_size, |
| 1935 | (unsigned int) sizeof (bfd_vma)); |
| 1936 | bfd_set_error (bfd_error_bad_value); |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
| 1940 | if (addr_size != 2 && addr_size != 4 && addr_size != 8) |
| 1941 | { |
| 1942 | (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size); |
| 1943 | bfd_set_error (bfd_error_bad_value); |
| 1944 | return 0; |
| 1945 | } |
| 1946 | |
| 1947 | /* Read the abbrevs for this compilation unit into a table. */ |
| 1948 | abbrevs = read_abbrevs (abfd, abbrev_offset, stash); |
| 1949 | if (! abbrevs) |
| 1950 | return 0; |
| 1951 | |
| 1952 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 1953 | info_ptr += bytes_read; |
| 1954 | if (! abbrev_number) |
| 1955 | { |
| 1956 | (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."), |
| 1957 | abbrev_number); |
| 1958 | bfd_set_error (bfd_error_bad_value); |
| 1959 | return 0; |
| 1960 | } |
| 1961 | |
| 1962 | abbrev = lookup_abbrev (abbrev_number, abbrevs); |
| 1963 | if (! abbrev) |
| 1964 | { |
| 1965 | (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), |
| 1966 | abbrev_number); |
| 1967 | bfd_set_error (bfd_error_bad_value); |
| 1968 | return 0; |
| 1969 | } |
| 1970 | |
| 1971 | amt = sizeof (struct comp_unit); |
| 1972 | unit = bfd_zalloc (abfd, amt); |
| 1973 | unit->abfd = abfd; |
| 1974 | unit->addr_size = addr_size; |
| 1975 | unit->offset_size = offset_size; |
| 1976 | unit->abbrevs = abbrevs; |
| 1977 | unit->end_ptr = end_ptr; |
| 1978 | unit->stash = stash; |
| 1979 | unit->info_ptr_unit = info_ptr_unit; |
| 1980 | |
| 1981 | for (i = 0; i < abbrev->num_attrs; ++i) |
| 1982 | { |
| 1983 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); |
| 1984 | |
| 1985 | /* Store the data if it is of an attribute we want to keep in a |
| 1986 | partial symbol table. */ |
| 1987 | switch (attr.name) |
| 1988 | { |
| 1989 | case DW_AT_stmt_list: |
| 1990 | unit->stmtlist = 1; |
| 1991 | unit->line_offset = attr.u.val; |
| 1992 | break; |
| 1993 | |
| 1994 | case DW_AT_name: |
| 1995 | unit->name = attr.u.str; |
| 1996 | break; |
| 1997 | |
| 1998 | case DW_AT_low_pc: |
| 1999 | low_pc = attr.u.val; |
| 2000 | /* If the compilation unit DIE has a DW_AT_low_pc attribute, |
| 2001 | this is the base address to use when reading location |
| 2002 | lists or range lists. */ |
| 2003 | unit->base_address = low_pc; |
| 2004 | break; |
| 2005 | |
| 2006 | case DW_AT_high_pc: |
| 2007 | high_pc = attr.u.val; |
| 2008 | break; |
| 2009 | |
| 2010 | case DW_AT_ranges: |
| 2011 | read_rangelist (unit, &unit->arange, attr.u.val); |
| 2012 | break; |
| 2013 | |
| 2014 | case DW_AT_comp_dir: |
| 2015 | { |
| 2016 | char *comp_dir = attr.u.str; |
| 2017 | if (comp_dir) |
| 2018 | { |
| 2019 | /* Irix 6.2 native cc prepends <machine>.: to the compilation |
| 2020 | directory, get rid of it. */ |
| 2021 | char *cp = strchr (comp_dir, ':'); |
| 2022 | |
| 2023 | if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/') |
| 2024 | comp_dir = cp + 1; |
| 2025 | } |
| 2026 | unit->comp_dir = comp_dir; |
| 2027 | break; |
| 2028 | } |
| 2029 | |
| 2030 | default: |
| 2031 | break; |
| 2032 | } |
| 2033 | } |
| 2034 | if (high_pc != 0) |
| 2035 | { |
| 2036 | arange_add (unit->abfd, &unit->arange, low_pc, high_pc); |
| 2037 | } |
| 2038 | |
| 2039 | unit->first_child_die_ptr = info_ptr; |
| 2040 | return unit; |
| 2041 | } |
| 2042 | |
| 2043 | /* Return TRUE if UNIT may contain the address given by ADDR. When |
| 2044 | there are functions written entirely with inline asm statements, the |
| 2045 | range info in the compilation unit header may not be correct. We |
| 2046 | need to consult the line info table to see if a compilation unit |
| 2047 | really contains the given address. */ |
| 2048 | |
| 2049 | static bfd_boolean |
| 2050 | comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr) |
| 2051 | { |
| 2052 | struct arange *arange; |
| 2053 | |
| 2054 | if (unit->error) |
| 2055 | return FALSE; |
| 2056 | |
| 2057 | arange = &unit->arange; |
| 2058 | do |
| 2059 | { |
| 2060 | if (addr >= arange->low && addr < arange->high) |
| 2061 | return TRUE; |
| 2062 | arange = arange->next; |
| 2063 | } |
| 2064 | while (arange); |
| 2065 | |
| 2066 | return FALSE; |
| 2067 | } |
| 2068 | |
| 2069 | /* If UNIT contains ADDR, set the output parameters to the values for |
| 2070 | the line containing ADDR. The output parameters, FILENAME_PTR, |
| 2071 | FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects |
| 2072 | to be filled in. |
| 2073 | |
| 2074 | Return TRUE if UNIT contains ADDR, and no errors were encountered; |
| 2075 | FALSE otherwise. */ |
| 2076 | |
| 2077 | static bfd_boolean |
| 2078 | comp_unit_find_nearest_line (struct comp_unit *unit, |
| 2079 | bfd_vma addr, |
| 2080 | const char **filename_ptr, |
| 2081 | const char **functionname_ptr, |
| 2082 | unsigned int *linenumber_ptr, |
| 2083 | struct dwarf2_debug *stash) |
| 2084 | { |
| 2085 | bfd_boolean line_p; |
| 2086 | bfd_boolean func_p; |
| 2087 | struct funcinfo *function; |
| 2088 | |
| 2089 | if (unit->error) |
| 2090 | return FALSE; |
| 2091 | |
| 2092 | if (! unit->line_table) |
| 2093 | { |
| 2094 | if (! unit->stmtlist) |
| 2095 | { |
| 2096 | unit->error = 1; |
| 2097 | return FALSE; |
| 2098 | } |
| 2099 | |
| 2100 | unit->line_table = decode_line_info (unit, stash); |
| 2101 | |
| 2102 | if (! unit->line_table) |
| 2103 | { |
| 2104 | unit->error = 1; |
| 2105 | return FALSE; |
| 2106 | } |
| 2107 | |
| 2108 | if (unit->first_child_die_ptr < unit->end_ptr |
| 2109 | && ! scan_unit_for_symbols (unit)) |
| 2110 | { |
| 2111 | unit->error = 1; |
| 2112 | return FALSE; |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | function = NULL; |
| 2117 | func_p = lookup_address_in_function_table (unit, addr, |
| 2118 | &function, functionname_ptr); |
| 2119 | if (func_p && (function->tag == DW_TAG_inlined_subroutine)) |
| 2120 | stash->inliner_chain = function; |
| 2121 | line_p = lookup_address_in_line_info_table (unit->line_table, addr, |
| 2122 | function, filename_ptr, |
| 2123 | linenumber_ptr); |
| 2124 | return line_p || func_p; |
| 2125 | } |
| 2126 | |
| 2127 | /* If UNIT contains SYM at ADDR, set the output parameters to the |
| 2128 | values for the line containing SYM. The output parameters, |
| 2129 | FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be |
| 2130 | filled in. |
| 2131 | |
| 2132 | Return TRUE if UNIT contains SYM, and no errors were encountered; |
| 2133 | FALSE otherwise. */ |
| 2134 | |
| 2135 | static bfd_boolean |
| 2136 | comp_unit_find_line (struct comp_unit *unit, |
| 2137 | asymbol *sym, |
| 2138 | bfd_vma addr, |
| 2139 | const char **filename_ptr, |
| 2140 | unsigned int *linenumber_ptr, |
| 2141 | struct dwarf2_debug *stash) |
| 2142 | { |
| 2143 | if (unit->error) |
| 2144 | return FALSE; |
| 2145 | |
| 2146 | if (! unit->line_table) |
| 2147 | { |
| 2148 | if (! unit->stmtlist) |
| 2149 | { |
| 2150 | unit->error = 1; |
| 2151 | return FALSE; |
| 2152 | } |
| 2153 | |
| 2154 | unit->line_table = decode_line_info (unit, stash); |
| 2155 | |
| 2156 | if (! unit->line_table) |
| 2157 | { |
| 2158 | unit->error = 1; |
| 2159 | return FALSE; |
| 2160 | } |
| 2161 | |
| 2162 | if (unit->first_child_die_ptr < unit->end_ptr |
| 2163 | && ! scan_unit_for_symbols (unit)) |
| 2164 | { |
| 2165 | unit->error = 1; |
| 2166 | return FALSE; |
| 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | if (sym->flags & BSF_FUNCTION) |
| 2171 | return lookup_symbol_in_function_table (unit, sym, addr, |
| 2172 | filename_ptr, |
| 2173 | linenumber_ptr); |
| 2174 | else |
| 2175 | return lookup_symbol_in_variable_table (unit, sym, addr, |
| 2176 | filename_ptr, |
| 2177 | linenumber_ptr); |
| 2178 | } |
| 2179 | |
| 2180 | /* Locate a section in a BFD containing debugging info. The search starts |
| 2181 | from the section after AFTER_SEC, or from the first section in the BFD if |
| 2182 | AFTER_SEC is NULL. The search works by examining the names of the |
| 2183 | sections. There are two permissiable names. The first is .debug_info. |
| 2184 | This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi. |
| 2185 | This is a variation on the .debug_info section which has a checksum |
| 2186 | describing the contents appended onto the name. This allows the linker to |
| 2187 | identify and discard duplicate debugging sections for different |
| 2188 | compilation units. */ |
| 2189 | #define DWARF2_DEBUG_INFO ".debug_info" |
| 2190 | #define GNU_LINKONCE_INFO ".gnu.linkonce.wi." |
| 2191 | |
| 2192 | static asection * |
| 2193 | find_debug_info (bfd *abfd, asection *after_sec) |
| 2194 | { |
| 2195 | asection * msec; |
| 2196 | |
| 2197 | msec = after_sec != NULL ? after_sec->next : abfd->sections; |
| 2198 | |
| 2199 | while (msec) |
| 2200 | { |
| 2201 | if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0) |
| 2202 | return msec; |
| 2203 | |
| 2204 | if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO)) |
| 2205 | return msec; |
| 2206 | |
| 2207 | msec = msec->next; |
| 2208 | } |
| 2209 | |
| 2210 | return NULL; |
| 2211 | } |
| 2212 | |
| 2213 | /* Unset vmas for loadable sections in STASH. */ |
| 2214 | |
| 2215 | static void |
| 2216 | unset_sections (struct dwarf2_debug *stash) |
| 2217 | { |
| 2218 | unsigned int i; |
| 2219 | struct loadable_section *p; |
| 2220 | |
| 2221 | i = stash->loadable_section_count; |
| 2222 | p = stash->loadable_sections; |
| 2223 | for (; i > 0; i--, p++) |
| 2224 | p->section->vma = 0; |
| 2225 | } |
| 2226 | |
| 2227 | /* Set unique vmas for loadable sections in ABFD and save vmas in |
| 2228 | STASH for unset_sections. */ |
| 2229 | |
| 2230 | static bfd_boolean |
| 2231 | place_sections (bfd *abfd, struct dwarf2_debug *stash) |
| 2232 | { |
| 2233 | struct loadable_section *p; |
| 2234 | unsigned int i; |
| 2235 | |
| 2236 | if (stash->loadable_section_count != 0) |
| 2237 | { |
| 2238 | i = stash->loadable_section_count; |
| 2239 | p = stash->loadable_sections; |
| 2240 | for (; i > 0; i--, p++) |
| 2241 | p->section->vma = p->adj_vma; |
| 2242 | } |
| 2243 | else |
| 2244 | { |
| 2245 | asection *sect; |
| 2246 | bfd_vma last_vma = 0; |
| 2247 | bfd_size_type amt; |
| 2248 | struct loadable_section *p; |
| 2249 | |
| 2250 | i = 0; |
| 2251 | for (sect = abfd->sections; sect != NULL; sect = sect->next) |
| 2252 | { |
| 2253 | bfd_size_type sz; |
| 2254 | |
| 2255 | if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0) |
| 2256 | continue; |
| 2257 | |
| 2258 | sz = sect->rawsize ? sect->rawsize : sect->size; |
| 2259 | if (sz == 0) |
| 2260 | continue; |
| 2261 | |
| 2262 | i++; |
| 2263 | } |
| 2264 | |
| 2265 | amt = i * sizeof (struct loadable_section); |
| 2266 | p = (struct loadable_section *) bfd_zalloc (abfd, amt); |
| 2267 | if (! p) |
| 2268 | return FALSE; |
| 2269 | |
| 2270 | stash->loadable_sections = p; |
| 2271 | stash->loadable_section_count = i; |
| 2272 | |
| 2273 | for (sect = abfd->sections; sect != NULL; sect = sect->next) |
| 2274 | { |
| 2275 | bfd_size_type sz; |
| 2276 | |
| 2277 | if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0) |
| 2278 | continue; |
| 2279 | |
| 2280 | sz = sect->rawsize ? sect->rawsize : sect->size; |
| 2281 | if (sz == 0) |
| 2282 | continue; |
| 2283 | |
| 2284 | p->section = sect; |
| 2285 | if (last_vma != 0) |
| 2286 | { |
| 2287 | /* Align the new address to the current section |
| 2288 | alignment. */ |
| 2289 | last_vma = ((last_vma |
| 2290 | + ~((bfd_vma) -1 << sect->alignment_power)) |
| 2291 | & ((bfd_vma) -1 << sect->alignment_power)); |
| 2292 | sect->vma = last_vma; |
| 2293 | } |
| 2294 | p->adj_vma = sect->vma; |
| 2295 | last_vma += sect->vma + sz; |
| 2296 | |
| 2297 | p++; |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | return TRUE; |
| 2302 | } |
| 2303 | |
| 2304 | /* Find the source code location of SYMBOL. If SYMBOL is NULL |
| 2305 | then find the nearest source code location corresponding to |
| 2306 | the address SECTION + OFFSET. |
| 2307 | Returns TRUE if the line is found without error and fills in |
| 2308 | FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was |
| 2309 | NULL the FUNCTIONNAME_PTR is also filled in. |
| 2310 | SYMBOLS contains the symbol table for ABFD. |
| 2311 | ADDR_SIZE is the number of bytes in the initial .debug_info length |
| 2312 | field and in the abbreviation offset, or zero to indicate that the |
| 2313 | default value should be used. */ |
| 2314 | |
| 2315 | static bfd_boolean |
| 2316 | find_line (bfd *abfd, |
| 2317 | asection *section, |
| 2318 | bfd_vma offset, |
| 2319 | asymbol *symbol, |
| 2320 | asymbol **symbols, |
| 2321 | const char **filename_ptr, |
| 2322 | const char **functionname_ptr, |
| 2323 | unsigned int *linenumber_ptr, |
| 2324 | unsigned int addr_size, |
| 2325 | void **pinfo) |
| 2326 | { |
| 2327 | /* Read each compilation unit from the section .debug_info, and check |
| 2328 | to see if it contains the address we are searching for. If yes, |
| 2329 | lookup the address, and return the line number info. If no, go |
| 2330 | on to the next compilation unit. |
| 2331 | |
| 2332 | We keep a list of all the previously read compilation units, and |
| 2333 | a pointer to the next un-read compilation unit. Check the |
| 2334 | previously read units before reading more. */ |
| 2335 | struct dwarf2_debug *stash; |
| 2336 | /* What address are we looking for? */ |
| 2337 | bfd_vma addr; |
| 2338 | struct comp_unit* each; |
| 2339 | bfd_vma found = FALSE; |
| 2340 | bfd_boolean do_line; |
| 2341 | |
| 2342 | stash = *pinfo; |
| 2343 | |
| 2344 | if (! stash) |
| 2345 | { |
| 2346 | bfd_size_type amt = sizeof (struct dwarf2_debug); |
| 2347 | |
| 2348 | stash = bfd_zalloc (abfd, amt); |
| 2349 | if (! stash) |
| 2350 | return FALSE; |
| 2351 | } |
| 2352 | |
| 2353 | /* In a relocatable file, 2 functions may have the same address. |
| 2354 | We change the section vma so that they won't overlap. */ |
| 2355 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) |
| 2356 | { |
| 2357 | if (! place_sections (abfd, stash)) |
| 2358 | return FALSE; |
| 2359 | } |
| 2360 | |
| 2361 | do_line = (section == NULL |
| 2362 | && offset == 0 |
| 2363 | && functionname_ptr == NULL |
| 2364 | && symbol != NULL); |
| 2365 | if (do_line) |
| 2366 | { |
| 2367 | addr = symbol->value; |
| 2368 | section = bfd_get_section (symbol); |
| 2369 | } |
| 2370 | else if (section != NULL |
| 2371 | && functionname_ptr != NULL |
| 2372 | && symbol == NULL) |
| 2373 | addr = offset; |
| 2374 | else |
| 2375 | abort (); |
| 2376 | |
| 2377 | if (section->output_section) |
| 2378 | addr += section->output_section->vma + section->output_offset; |
| 2379 | else |
| 2380 | addr += section->vma; |
| 2381 | *filename_ptr = NULL; |
| 2382 | *functionname_ptr = NULL; |
| 2383 | *linenumber_ptr = 0; |
| 2384 | |
| 2385 | if (! *pinfo) |
| 2386 | { |
| 2387 | bfd *debug_bfd; |
| 2388 | bfd_size_type total_size; |
| 2389 | asection *msec; |
| 2390 | |
| 2391 | *pinfo = stash; |
| 2392 | |
| 2393 | msec = find_debug_info (abfd, NULL); |
| 2394 | if (msec == NULL) |
| 2395 | { |
| 2396 | char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR); |
| 2397 | |
| 2398 | if (debug_filename == NULL) |
| 2399 | /* No dwarf2 info, and no gnu_debuglink to follow. |
| 2400 | Note that at this point the stash has been allocated, but |
| 2401 | contains zeros. This lets future calls to this function |
| 2402 | fail more quickly. */ |
| 2403 | goto done; |
| 2404 | |
| 2405 | if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL |
| 2406 | || ! bfd_check_format (debug_bfd, bfd_object) |
| 2407 | || (msec = find_debug_info (debug_bfd, NULL)) == NULL) |
| 2408 | { |
| 2409 | if (debug_bfd) |
| 2410 | bfd_close (debug_bfd); |
| 2411 | /* FIXME: Should we report our failure to follow the debuglink ? */ |
| 2412 | free (debug_filename); |
| 2413 | goto done; |
| 2414 | } |
| 2415 | } |
| 2416 | else |
| 2417 | debug_bfd = abfd; |
| 2418 | |
| 2419 | /* There can be more than one DWARF2 info section in a BFD these days. |
| 2420 | Read them all in and produce one large stash. We do this in two |
| 2421 | passes - in the first pass we just accumulate the section sizes. |
| 2422 | In the second pass we read in the section's contents. The allows |
| 2423 | us to avoid reallocing the data as we add sections to the stash. */ |
| 2424 | for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec)) |
| 2425 | total_size += msec->size; |
| 2426 | |
| 2427 | stash->info_ptr = bfd_alloc (debug_bfd, total_size); |
| 2428 | if (stash->info_ptr == NULL) |
| 2429 | goto done; |
| 2430 | |
| 2431 | stash->info_ptr_end = stash->info_ptr; |
| 2432 | |
| 2433 | for (msec = find_debug_info (debug_bfd, NULL); |
| 2434 | msec; |
| 2435 | msec = find_debug_info (debug_bfd, msec)) |
| 2436 | { |
| 2437 | bfd_size_type size; |
| 2438 | bfd_size_type start; |
| 2439 | |
| 2440 | size = msec->size; |
| 2441 | if (size == 0) |
| 2442 | continue; |
| 2443 | |
| 2444 | start = stash->info_ptr_end - stash->info_ptr; |
| 2445 | |
| 2446 | if ((bfd_simple_get_relocated_section_contents |
| 2447 | (debug_bfd, msec, stash->info_ptr + start, symbols)) == NULL) |
| 2448 | continue; |
| 2449 | |
| 2450 | stash->info_ptr_end = stash->info_ptr + start + size; |
| 2451 | } |
| 2452 | |
| 2453 | BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size); |
| 2454 | |
| 2455 | stash->sec = find_debug_info (debug_bfd, NULL); |
| 2456 | stash->sec_info_ptr = stash->info_ptr; |
| 2457 | stash->syms = symbols; |
| 2458 | stash->bfd = debug_bfd; |
| 2459 | } |
| 2460 | |
| 2461 | /* A null info_ptr indicates that there is no dwarf2 info |
| 2462 | (or that an error occured while setting up the stash). */ |
| 2463 | if (! stash->info_ptr) |
| 2464 | goto done; |
| 2465 | |
| 2466 | stash->inliner_chain = NULL; |
| 2467 | |
| 2468 | /* Check the previously read comp. units first. */ |
| 2469 | for (each = stash->all_comp_units; each; each = each->next_unit) |
| 2470 | { |
| 2471 | if (do_line) |
| 2472 | found = (((symbol->flags & BSF_FUNCTION) == 0 |
| 2473 | || comp_unit_contains_address (each, addr)) |
| 2474 | && comp_unit_find_line (each, symbol, addr, |
| 2475 | filename_ptr, linenumber_ptr, |
| 2476 | stash)); |
| 2477 | else |
| 2478 | found = (comp_unit_contains_address (each, addr) |
| 2479 | && comp_unit_find_nearest_line (each, addr, |
| 2480 | filename_ptr, |
| 2481 | functionname_ptr, |
| 2482 | linenumber_ptr, |
| 2483 | stash)); |
| 2484 | if (found) |
| 2485 | goto done; |
| 2486 | } |
| 2487 | |
| 2488 | /* The DWARF2 spec says that the initial length field, and the |
| 2489 | offset of the abbreviation table, should both be 4-byte values. |
| 2490 | However, some compilers do things differently. */ |
| 2491 | if (addr_size == 0) |
| 2492 | addr_size = 4; |
| 2493 | BFD_ASSERT (addr_size == 4 || addr_size == 8); |
| 2494 | |
| 2495 | /* Read each remaining comp. units checking each as they are read. */ |
| 2496 | while (stash->info_ptr < stash->info_ptr_end) |
| 2497 | { |
| 2498 | bfd_vma length; |
| 2499 | unsigned int offset_size = addr_size; |
| 2500 | bfd_byte *info_ptr_unit = stash->info_ptr; |
| 2501 | |
| 2502 | length = read_4_bytes (stash->bfd, stash->info_ptr); |
| 2503 | /* A 0xffffff length is the DWARF3 way of indicating |
| 2504 | we use 64-bit offsets, instead of 32-bit offsets. */ |
| 2505 | if (length == 0xffffffff) |
| 2506 | { |
| 2507 | offset_size = 8; |
| 2508 | length = read_8_bytes (stash->bfd, stash->info_ptr + 4); |
| 2509 | stash->info_ptr += 12; |
| 2510 | } |
| 2511 | /* A zero length is the IRIX way of indicating 64-bit offsets, |
| 2512 | mostly because the 64-bit length will generally fit in 32 |
| 2513 | bits, and the endianness helps. */ |
| 2514 | else if (length == 0) |
| 2515 | { |
| 2516 | offset_size = 8; |
| 2517 | length = read_4_bytes (stash->bfd, stash->info_ptr + 4); |
| 2518 | stash->info_ptr += 8; |
| 2519 | } |
| 2520 | /* In the absence of the hints above, we assume 32-bit DWARF2 |
| 2521 | offsets even for targets with 64-bit addresses, because: |
| 2522 | a) most of the time these targets will not have generated |
| 2523 | more than 2Gb of debug info and so will not need 64-bit |
| 2524 | offsets, |
| 2525 | and |
| 2526 | b) if they do use 64-bit offsets but they are not using |
| 2527 | the size hints that are tested for above then they are |
| 2528 | not conforming to the DWARF3 standard anyway. */ |
| 2529 | else if (addr_size == 8) |
| 2530 | { |
| 2531 | offset_size = 4; |
| 2532 | stash->info_ptr += 4; |
| 2533 | } |
| 2534 | else |
| 2535 | stash->info_ptr += 4; |
| 2536 | |
| 2537 | if (length > 0) |
| 2538 | { |
| 2539 | each = parse_comp_unit (stash, length, info_ptr_unit, |
| 2540 | offset_size); |
| 2541 | stash->info_ptr += length; |
| 2542 | |
| 2543 | if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr) |
| 2544 | == stash->sec->size) |
| 2545 | { |
| 2546 | stash->sec = find_debug_info (stash->bfd, stash->sec); |
| 2547 | stash->sec_info_ptr = stash->info_ptr; |
| 2548 | } |
| 2549 | |
| 2550 | if (each) |
| 2551 | { |
| 2552 | each->next_unit = stash->all_comp_units; |
| 2553 | stash->all_comp_units = each; |
| 2554 | |
| 2555 | /* DW_AT_low_pc and DW_AT_high_pc are optional for |
| 2556 | compilation units. If we don't have them (i.e., |
| 2557 | unit->high == 0), we need to consult the line info |
| 2558 | table to see if a compilation unit contains the given |
| 2559 | address. */ |
| 2560 | if (do_line) |
| 2561 | found = (((symbol->flags & BSF_FUNCTION) == 0 |
| 2562 | || each->arange.high == 0 |
| 2563 | || comp_unit_contains_address (each, addr)) |
| 2564 | && comp_unit_find_line (each, symbol, addr, |
| 2565 | filename_ptr, |
| 2566 | linenumber_ptr, |
| 2567 | stash)); |
| 2568 | else |
| 2569 | found = ((each->arange.high == 0 |
| 2570 | || comp_unit_contains_address (each, addr)) |
| 2571 | && comp_unit_find_nearest_line (each, addr, |
| 2572 | filename_ptr, |
| 2573 | functionname_ptr, |
| 2574 | linenumber_ptr, |
| 2575 | stash)); |
| 2576 | if (found) |
| 2577 | goto done; |
| 2578 | } |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | done: |
| 2583 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) |
| 2584 | unset_sections (stash); |
| 2585 | |
| 2586 | return found; |
| 2587 | } |
| 2588 | |
| 2589 | /* The DWARF2 version of find_nearest_line. |
| 2590 | Return TRUE if the line is found without error. */ |
| 2591 | |
| 2592 | bfd_boolean |
| 2593 | _bfd_dwarf2_find_nearest_line (bfd *abfd, |
| 2594 | asection *section, |
| 2595 | asymbol **symbols, |
| 2596 | bfd_vma offset, |
| 2597 | const char **filename_ptr, |
| 2598 | const char **functionname_ptr, |
| 2599 | unsigned int *linenumber_ptr, |
| 2600 | unsigned int addr_size, |
| 2601 | void **pinfo) |
| 2602 | { |
| 2603 | return find_line (abfd, section, offset, NULL, symbols, filename_ptr, |
| 2604 | functionname_ptr, linenumber_ptr, addr_size, |
| 2605 | pinfo); |
| 2606 | } |
| 2607 | |
| 2608 | /* The DWARF2 version of find_line. |
| 2609 | Return TRUE if the line is found without error. */ |
| 2610 | |
| 2611 | bfd_boolean |
| 2612 | _bfd_dwarf2_find_line (bfd *abfd, |
| 2613 | asymbol **symbols, |
| 2614 | asymbol *symbol, |
| 2615 | const char **filename_ptr, |
| 2616 | unsigned int *linenumber_ptr, |
| 2617 | unsigned int addr_size, |
| 2618 | void **pinfo) |
| 2619 | { |
| 2620 | return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr, |
| 2621 | NULL, linenumber_ptr, addr_size, |
| 2622 | pinfo); |
| 2623 | } |
| 2624 | |
| 2625 | bfd_boolean |
| 2626 | _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED, |
| 2627 | const char **filename_ptr, |
| 2628 | const char **functionname_ptr, |
| 2629 | unsigned int *linenumber_ptr, |
| 2630 | void **pinfo) |
| 2631 | { |
| 2632 | struct dwarf2_debug *stash; |
| 2633 | |
| 2634 | stash = *pinfo; |
| 2635 | if (stash) |
| 2636 | { |
| 2637 | struct funcinfo *func = stash->inliner_chain; |
| 2638 | |
| 2639 | if (func && func->caller_func) |
| 2640 | { |
| 2641 | *filename_ptr = func->caller_file; |
| 2642 | *functionname_ptr = func->caller_func->name; |
| 2643 | *linenumber_ptr = func->caller_line; |
| 2644 | stash->inliner_chain = func->caller_func; |
| 2645 | return TRUE; |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | return FALSE; |
| 2650 | } |
| 2651 | |
| 2652 | void |
| 2653 | _bfd_dwarf2_cleanup_debug_info (bfd *abfd) |
| 2654 | { |
| 2655 | struct comp_unit *each; |
| 2656 | struct dwarf2_debug *stash; |
| 2657 | |
| 2658 | if (abfd == NULL || elf_tdata (abfd) == NULL) |
| 2659 | return; |
| 2660 | |
| 2661 | stash = elf_tdata (abfd)->dwarf2_find_line_info; |
| 2662 | |
| 2663 | if (stash == NULL) |
| 2664 | return; |
| 2665 | |
| 2666 | for (each = stash->all_comp_units; each; each = each->next_unit) |
| 2667 | { |
| 2668 | struct abbrev_info **abbrevs = each->abbrevs; |
| 2669 | size_t i; |
| 2670 | |
| 2671 | for (i = 0; i < ABBREV_HASH_SIZE; i++) |
| 2672 | { |
| 2673 | struct abbrev_info *abbrev = abbrevs[i]; |
| 2674 | |
| 2675 | while (abbrev) |
| 2676 | { |
| 2677 | free (abbrev->attrs); |
| 2678 | abbrev = abbrev->next; |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | if (each->line_table) |
| 2683 | { |
| 2684 | free (each->line_table->dirs); |
| 2685 | free (each->line_table->files); |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | free (stash->dwarf_abbrev_buffer); |
| 2690 | free (stash->dwarf_line_buffer); |
| 2691 | free (stash->dwarf_ranges_buffer); |
| 2692 | } |