| 1 | /* DWARF 2 support. |
| 2 | Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, |
| 3 | 2004, 2005 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 "bfd.h" |
| 33 | #include "sysdep.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 dwarf2_debug |
| 78 | { |
| 79 | /* A list of all previously read comp_units. */ |
| 80 | struct comp_unit *all_comp_units; |
| 81 | |
| 82 | /* The next unread compilation unit within the .debug_info section. |
| 83 | Zero indicates that the .debug_info section has not been loaded |
| 84 | into a buffer yet. */ |
| 85 | bfd_byte *info_ptr; |
| 86 | |
| 87 | /* Pointer to the end of the .debug_info section memory buffer. */ |
| 88 | bfd_byte *info_ptr_end; |
| 89 | |
| 90 | /* Pointer to the section and address of the beginning of the |
| 91 | section. */ |
| 92 | asection *sec; |
| 93 | bfd_byte *sec_info_ptr; |
| 94 | |
| 95 | /* Pointer to the symbol table. */ |
| 96 | asymbol **syms; |
| 97 | |
| 98 | /* Pointer to the .debug_abbrev section loaded into memory. */ |
| 99 | bfd_byte *dwarf_abbrev_buffer; |
| 100 | |
| 101 | /* Length of the loaded .debug_abbrev section. */ |
| 102 | unsigned long dwarf_abbrev_size; |
| 103 | |
| 104 | /* Buffer for decode_line_info. */ |
| 105 | bfd_byte *dwarf_line_buffer; |
| 106 | |
| 107 | /* Length of the loaded .debug_line section. */ |
| 108 | unsigned long dwarf_line_size; |
| 109 | |
| 110 | /* Pointer to the .debug_str section loaded into memory. */ |
| 111 | bfd_byte *dwarf_str_buffer; |
| 112 | |
| 113 | /* Length of the loaded .debug_str section. */ |
| 114 | unsigned long dwarf_str_size; |
| 115 | |
| 116 | /* Pointer to the .debug_ranges section loaded into memory. */ |
| 117 | bfd_byte *dwarf_ranges_buffer; |
| 118 | |
| 119 | /* Length of the loaded .debug_ranges section. */ |
| 120 | unsigned long dwarf_ranges_size; |
| 121 | }; |
| 122 | |
| 123 | struct arange |
| 124 | { |
| 125 | struct arange *next; |
| 126 | bfd_vma low; |
| 127 | bfd_vma high; |
| 128 | }; |
| 129 | |
| 130 | /* A minimal decoding of DWARF2 compilation units. We only decode |
| 131 | what's needed to get to the line number information. */ |
| 132 | |
| 133 | struct comp_unit |
| 134 | { |
| 135 | /* Chain the previously read compilation units. */ |
| 136 | struct comp_unit *next_unit; |
| 137 | |
| 138 | /* Keep the bdf convenient (for memory allocation). */ |
| 139 | bfd *abfd; |
| 140 | |
| 141 | /* The lowest and highest addresses contained in this compilation |
| 142 | unit as specified in the compilation unit header. */ |
| 143 | struct arange arange; |
| 144 | |
| 145 | /* The DW_AT_name attribute (for error messages). */ |
| 146 | char *name; |
| 147 | |
| 148 | /* The abbrev hash table. */ |
| 149 | struct abbrev_info **abbrevs; |
| 150 | |
| 151 | /* Note that an error was found by comp_unit_find_nearest_line. */ |
| 152 | int error; |
| 153 | |
| 154 | /* The DW_AT_comp_dir attribute. */ |
| 155 | char *comp_dir; |
| 156 | |
| 157 | /* TRUE if there is a line number table associated with this comp. unit. */ |
| 158 | int stmtlist; |
| 159 | |
| 160 | /* Pointer to the current comp_unit so that we can find a given entry |
| 161 | by its reference. */ |
| 162 | bfd_byte *info_ptr_unit; |
| 163 | |
| 164 | /* The offset into .debug_line of the line number table. */ |
| 165 | unsigned long line_offset; |
| 166 | |
| 167 | /* Pointer to the first child die for the comp unit. */ |
| 168 | bfd_byte *first_child_die_ptr; |
| 169 | |
| 170 | /* The end of the comp unit. */ |
| 171 | bfd_byte *end_ptr; |
| 172 | |
| 173 | /* The decoded line number, NULL if not yet decoded. */ |
| 174 | struct line_info_table *line_table; |
| 175 | |
| 176 | /* A list of the functions found in this comp. unit. */ |
| 177 | struct funcinfo *function_table; |
| 178 | |
| 179 | /* Pointer to dwarf2_debug structure. */ |
| 180 | struct dwarf2_debug *stash; |
| 181 | |
| 182 | /* Address size for this unit - from unit header. */ |
| 183 | unsigned char addr_size; |
| 184 | |
| 185 | /* Offset size for this unit - from unit header. */ |
| 186 | unsigned char offset_size; |
| 187 | |
| 188 | /* Base address for this unit - from DW_AT_low_pc attribute of |
| 189 | DW_TAG_compile_unit DIE */ |
| 190 | bfd_vma base_address; |
| 191 | }; |
| 192 | |
| 193 | /* This data structure holds the information of an abbrev. */ |
| 194 | struct abbrev_info |
| 195 | { |
| 196 | unsigned int number; /* Number identifying abbrev. */ |
| 197 | enum dwarf_tag tag; /* DWARF tag. */ |
| 198 | int has_children; /* Boolean. */ |
| 199 | unsigned int num_attrs; /* Number of attributes. */ |
| 200 | struct attr_abbrev *attrs; /* An array of attribute descriptions. */ |
| 201 | struct abbrev_info *next; /* Next in chain. */ |
| 202 | }; |
| 203 | |
| 204 | struct attr_abbrev |
| 205 | { |
| 206 | enum dwarf_attribute name; |
| 207 | enum dwarf_form form; |
| 208 | }; |
| 209 | |
| 210 | #ifndef ABBREV_HASH_SIZE |
| 211 | #define ABBREV_HASH_SIZE 121 |
| 212 | #endif |
| 213 | #ifndef ATTR_ALLOC_CHUNK |
| 214 | #define ATTR_ALLOC_CHUNK 4 |
| 215 | #endif |
| 216 | |
| 217 | /* VERBATIM |
| 218 | The following function up to the END VERBATIM mark are |
| 219 | copied directly from dwarf2read.c. */ |
| 220 | |
| 221 | /* Read dwarf information from a buffer. */ |
| 222 | |
| 223 | static unsigned int |
| 224 | read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf) |
| 225 | { |
| 226 | return bfd_get_8 (abfd, buf); |
| 227 | } |
| 228 | |
| 229 | static int |
| 230 | read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf) |
| 231 | { |
| 232 | return bfd_get_signed_8 (abfd, buf); |
| 233 | } |
| 234 | |
| 235 | static unsigned int |
| 236 | read_2_bytes (bfd *abfd, bfd_byte *buf) |
| 237 | { |
| 238 | return bfd_get_16 (abfd, buf); |
| 239 | } |
| 240 | |
| 241 | static unsigned int |
| 242 | read_4_bytes (bfd *abfd, bfd_byte *buf) |
| 243 | { |
| 244 | return bfd_get_32 (abfd, buf); |
| 245 | } |
| 246 | |
| 247 | static bfd_uint64_t |
| 248 | read_8_bytes (bfd *abfd, bfd_byte *buf) |
| 249 | { |
| 250 | return bfd_get_64 (abfd, buf); |
| 251 | } |
| 252 | |
| 253 | static bfd_byte * |
| 254 | read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED, |
| 255 | bfd_byte *buf, |
| 256 | unsigned int size ATTRIBUTE_UNUSED) |
| 257 | { |
| 258 | /* If the size of a host char is 8 bits, we can return a pointer |
| 259 | to the buffer, otherwise we have to copy the data to a buffer |
| 260 | allocated on the temporary obstack. */ |
| 261 | return buf; |
| 262 | } |
| 263 | |
| 264 | static char * |
| 265 | read_string (bfd *abfd ATTRIBUTE_UNUSED, |
| 266 | bfd_byte *buf, |
| 267 | unsigned int *bytes_read_ptr) |
| 268 | { |
| 269 | /* Return a pointer to the embedded string. */ |
| 270 | char *str = (char *) buf; |
| 271 | if (*str == '\0') |
| 272 | { |
| 273 | *bytes_read_ptr = 1; |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | *bytes_read_ptr = strlen (str) + 1; |
| 278 | return str; |
| 279 | } |
| 280 | |
| 281 | static char * |
| 282 | read_indirect_string (struct comp_unit* unit, |
| 283 | bfd_byte *buf, |
| 284 | unsigned int *bytes_read_ptr) |
| 285 | { |
| 286 | bfd_uint64_t offset; |
| 287 | struct dwarf2_debug *stash = unit->stash; |
| 288 | char *str; |
| 289 | |
| 290 | if (unit->offset_size == 4) |
| 291 | offset = read_4_bytes (unit->abfd, buf); |
| 292 | else |
| 293 | offset = read_8_bytes (unit->abfd, buf); |
| 294 | *bytes_read_ptr = unit->offset_size; |
| 295 | |
| 296 | if (! stash->dwarf_str_buffer) |
| 297 | { |
| 298 | asection *msec; |
| 299 | bfd *abfd = unit->abfd; |
| 300 | bfd_size_type sz; |
| 301 | |
| 302 | msec = bfd_get_section_by_name (abfd, ".debug_str"); |
| 303 | if (! msec) |
| 304 | { |
| 305 | (*_bfd_error_handler) |
| 306 | (_("Dwarf Error: Can't find .debug_str section.")); |
| 307 | bfd_set_error (bfd_error_bad_value); |
| 308 | return NULL; |
| 309 | } |
| 310 | |
| 311 | sz = msec->rawsize ? msec->rawsize : msec->size; |
| 312 | stash->dwarf_str_size = sz; |
| 313 | stash->dwarf_str_buffer = bfd_alloc (abfd, sz); |
| 314 | if (! stash->dwarf_str_buffer) |
| 315 | return NULL; |
| 316 | |
| 317 | if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer, |
| 318 | 0, sz)) |
| 319 | return NULL; |
| 320 | } |
| 321 | |
| 322 | if (offset >= stash->dwarf_str_size) |
| 323 | { |
| 324 | (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."), |
| 325 | (unsigned long) offset, stash->dwarf_str_size); |
| 326 | bfd_set_error (bfd_error_bad_value); |
| 327 | return NULL; |
| 328 | } |
| 329 | |
| 330 | str = (char *) stash->dwarf_str_buffer + offset; |
| 331 | if (*str == '\0') |
| 332 | return NULL; |
| 333 | return str; |
| 334 | } |
| 335 | |
| 336 | /* END VERBATIM */ |
| 337 | |
| 338 | static bfd_uint64_t |
| 339 | read_address (struct comp_unit *unit, bfd_byte *buf) |
| 340 | { |
| 341 | switch (unit->addr_size) |
| 342 | { |
| 343 | case 8: |
| 344 | return bfd_get_64 (unit->abfd, buf); |
| 345 | case 4: |
| 346 | return bfd_get_32 (unit->abfd, buf); |
| 347 | case 2: |
| 348 | return bfd_get_16 (unit->abfd, buf); |
| 349 | default: |
| 350 | abort (); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /* Lookup an abbrev_info structure in the abbrev hash table. */ |
| 355 | |
| 356 | static struct abbrev_info * |
| 357 | lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs) |
| 358 | { |
| 359 | unsigned int hash_number; |
| 360 | struct abbrev_info *abbrev; |
| 361 | |
| 362 | hash_number = number % ABBREV_HASH_SIZE; |
| 363 | abbrev = abbrevs[hash_number]; |
| 364 | |
| 365 | while (abbrev) |
| 366 | { |
| 367 | if (abbrev->number == number) |
| 368 | return abbrev; |
| 369 | else |
| 370 | abbrev = abbrev->next; |
| 371 | } |
| 372 | |
| 373 | return NULL; |
| 374 | } |
| 375 | |
| 376 | /* In DWARF version 2, the description of the debugging information is |
| 377 | stored in a separate .debug_abbrev section. Before we read any |
| 378 | dies from a section we read in all abbreviations and install them |
| 379 | in a hash table. */ |
| 380 | |
| 381 | static struct abbrev_info** |
| 382 | read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash) |
| 383 | { |
| 384 | struct abbrev_info **abbrevs; |
| 385 | bfd_byte *abbrev_ptr; |
| 386 | struct abbrev_info *cur_abbrev; |
| 387 | unsigned int abbrev_number, bytes_read, abbrev_name; |
| 388 | unsigned int abbrev_form, hash_number; |
| 389 | bfd_size_type amt; |
| 390 | |
| 391 | if (! stash->dwarf_abbrev_buffer) |
| 392 | { |
| 393 | asection *msec; |
| 394 | |
| 395 | msec = bfd_get_section_by_name (abfd, ".debug_abbrev"); |
| 396 | if (! msec) |
| 397 | { |
| 398 | (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section.")); |
| 399 | bfd_set_error (bfd_error_bad_value); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | stash->dwarf_abbrev_size = msec->size; |
| 404 | stash->dwarf_abbrev_buffer |
| 405 | = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, |
| 406 | stash->syms); |
| 407 | if (! stash->dwarf_abbrev_buffer) |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | if (offset >= stash->dwarf_abbrev_size) |
| 412 | { |
| 413 | (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."), |
| 414 | (unsigned long) offset, stash->dwarf_abbrev_size); |
| 415 | bfd_set_error (bfd_error_bad_value); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE; |
| 420 | abbrevs = bfd_zalloc (abfd, amt); |
| 421 | |
| 422 | abbrev_ptr = stash->dwarf_abbrev_buffer + offset; |
| 423 | abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 424 | abbrev_ptr += bytes_read; |
| 425 | |
| 426 | /* Loop until we reach an abbrev number of 0. */ |
| 427 | while (abbrev_number) |
| 428 | { |
| 429 | amt = sizeof (struct abbrev_info); |
| 430 | cur_abbrev = bfd_zalloc (abfd, amt); |
| 431 | |
| 432 | /* Read in abbrev header. */ |
| 433 | cur_abbrev->number = abbrev_number; |
| 434 | cur_abbrev->tag = (enum dwarf_tag) |
| 435 | read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 436 | abbrev_ptr += bytes_read; |
| 437 | cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr); |
| 438 | abbrev_ptr += 1; |
| 439 | |
| 440 | /* Now read in declarations. */ |
| 441 | abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 442 | abbrev_ptr += bytes_read; |
| 443 | abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 444 | abbrev_ptr += bytes_read; |
| 445 | |
| 446 | while (abbrev_name) |
| 447 | { |
| 448 | if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0) |
| 449 | { |
| 450 | struct attr_abbrev *tmp; |
| 451 | |
| 452 | amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK; |
| 453 | amt *= sizeof (struct attr_abbrev); |
| 454 | tmp = bfd_realloc (cur_abbrev->attrs, amt); |
| 455 | if (tmp == NULL) |
| 456 | { |
| 457 | size_t i; |
| 458 | |
| 459 | for (i = 0; i < ABBREV_HASH_SIZE; i++) |
| 460 | { |
| 461 | struct abbrev_info *abbrev = abbrevs[i]; |
| 462 | |
| 463 | while (abbrev) |
| 464 | { |
| 465 | free (abbrev->attrs); |
| 466 | abbrev = abbrev->next; |
| 467 | } |
| 468 | } |
| 469 | return NULL; |
| 470 | } |
| 471 | cur_abbrev->attrs = tmp; |
| 472 | } |
| 473 | |
| 474 | cur_abbrev->attrs[cur_abbrev->num_attrs].name |
| 475 | = (enum dwarf_attribute) abbrev_name; |
| 476 | cur_abbrev->attrs[cur_abbrev->num_attrs++].form |
| 477 | = (enum dwarf_form) abbrev_form; |
| 478 | abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 479 | abbrev_ptr += bytes_read; |
| 480 | abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 481 | abbrev_ptr += bytes_read; |
| 482 | } |
| 483 | |
| 484 | hash_number = abbrev_number % ABBREV_HASH_SIZE; |
| 485 | cur_abbrev->next = abbrevs[hash_number]; |
| 486 | abbrevs[hash_number] = cur_abbrev; |
| 487 | |
| 488 | /* Get next abbreviation. |
| 489 | Under Irix6 the abbreviations for a compilation unit are not |
| 490 | always properly terminated with an abbrev number of 0. |
| 491 | Exit loop if we encounter an abbreviation which we have |
| 492 | already read (which means we are about to read the abbreviations |
| 493 | for the next compile unit) or if the end of the abbreviation |
| 494 | table is reached. */ |
| 495 | if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer) |
| 496 | >= stash->dwarf_abbrev_size) |
| 497 | break; |
| 498 | abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 499 | abbrev_ptr += bytes_read; |
| 500 | if (lookup_abbrev (abbrev_number,abbrevs) != NULL) |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | return abbrevs; |
| 505 | } |
| 506 | |
| 507 | /* Read an attribute value described by an attribute form. */ |
| 508 | |
| 509 | static bfd_byte * |
| 510 | read_attribute_value (struct attribute *attr, |
| 511 | unsigned form, |
| 512 | struct comp_unit *unit, |
| 513 | bfd_byte *info_ptr) |
| 514 | { |
| 515 | bfd *abfd = unit->abfd; |
| 516 | unsigned int bytes_read; |
| 517 | struct dwarf_block *blk; |
| 518 | bfd_size_type amt; |
| 519 | |
| 520 | attr->form = (enum dwarf_form) form; |
| 521 | |
| 522 | switch (form) |
| 523 | { |
| 524 | case DW_FORM_addr: |
| 525 | /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */ |
| 526 | case DW_FORM_ref_addr: |
| 527 | attr->u.val = read_address (unit, info_ptr); |
| 528 | info_ptr += unit->addr_size; |
| 529 | break; |
| 530 | case DW_FORM_block2: |
| 531 | amt = sizeof (struct dwarf_block); |
| 532 | blk = bfd_alloc (abfd, amt); |
| 533 | blk->size = read_2_bytes (abfd, info_ptr); |
| 534 | info_ptr += 2; |
| 535 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); |
| 536 | info_ptr += blk->size; |
| 537 | attr->u.blk = blk; |
| 538 | break; |
| 539 | case DW_FORM_block4: |
| 540 | amt = sizeof (struct dwarf_block); |
| 541 | blk = bfd_alloc (abfd, amt); |
| 542 | blk->size = read_4_bytes (abfd, info_ptr); |
| 543 | info_ptr += 4; |
| 544 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); |
| 545 | info_ptr += blk->size; |
| 546 | attr->u.blk = blk; |
| 547 | break; |
| 548 | case DW_FORM_data2: |
| 549 | attr->u.val = read_2_bytes (abfd, info_ptr); |
| 550 | info_ptr += 2; |
| 551 | break; |
| 552 | case DW_FORM_data4: |
| 553 | attr->u.val = read_4_bytes (abfd, info_ptr); |
| 554 | info_ptr += 4; |
| 555 | break; |
| 556 | case DW_FORM_data8: |
| 557 | attr->u.val = read_8_bytes (abfd, info_ptr); |
| 558 | info_ptr += 8; |
| 559 | break; |
| 560 | case DW_FORM_string: |
| 561 | attr->u.str = read_string (abfd, info_ptr, &bytes_read); |
| 562 | info_ptr += bytes_read; |
| 563 | break; |
| 564 | case DW_FORM_strp: |
| 565 | attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read); |
| 566 | info_ptr += bytes_read; |
| 567 | break; |
| 568 | case DW_FORM_block: |
| 569 | amt = sizeof (struct dwarf_block); |
| 570 | blk = bfd_alloc (abfd, amt); |
| 571 | blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 572 | info_ptr += bytes_read; |
| 573 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); |
| 574 | info_ptr += blk->size; |
| 575 | attr->u.blk = blk; |
| 576 | break; |
| 577 | case DW_FORM_block1: |
| 578 | amt = sizeof (struct dwarf_block); |
| 579 | blk = bfd_alloc (abfd, amt); |
| 580 | blk->size = read_1_byte (abfd, info_ptr); |
| 581 | info_ptr += 1; |
| 582 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); |
| 583 | info_ptr += blk->size; |
| 584 | attr->u.blk = blk; |
| 585 | break; |
| 586 | case DW_FORM_data1: |
| 587 | attr->u.val = read_1_byte (abfd, info_ptr); |
| 588 | info_ptr += 1; |
| 589 | break; |
| 590 | case DW_FORM_flag: |
| 591 | attr->u.val = read_1_byte (abfd, info_ptr); |
| 592 | info_ptr += 1; |
| 593 | break; |
| 594 | case DW_FORM_sdata: |
| 595 | attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read); |
| 596 | info_ptr += bytes_read; |
| 597 | break; |
| 598 | case DW_FORM_udata: |
| 599 | attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 600 | info_ptr += bytes_read; |
| 601 | break; |
| 602 | case DW_FORM_ref1: |
| 603 | attr->u.val = read_1_byte (abfd, info_ptr); |
| 604 | info_ptr += 1; |
| 605 | break; |
| 606 | case DW_FORM_ref2: |
| 607 | attr->u.val = read_2_bytes (abfd, info_ptr); |
| 608 | info_ptr += 2; |
| 609 | break; |
| 610 | case DW_FORM_ref4: |
| 611 | attr->u.val = read_4_bytes (abfd, info_ptr); |
| 612 | info_ptr += 4; |
| 613 | break; |
| 614 | case DW_FORM_ref8: |
| 615 | attr->u.val = read_8_bytes (abfd, info_ptr); |
| 616 | info_ptr += 8; |
| 617 | break; |
| 618 | case DW_FORM_ref_udata: |
| 619 | attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 620 | info_ptr += bytes_read; |
| 621 | break; |
| 622 | case DW_FORM_indirect: |
| 623 | form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 624 | info_ptr += bytes_read; |
| 625 | info_ptr = read_attribute_value (attr, form, unit, info_ptr); |
| 626 | break; |
| 627 | default: |
| 628 | (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."), |
| 629 | form); |
| 630 | bfd_set_error (bfd_error_bad_value); |
| 631 | } |
| 632 | return info_ptr; |
| 633 | } |
| 634 | |
| 635 | /* Read an attribute described by an abbreviated attribute. */ |
| 636 | |
| 637 | static bfd_byte * |
| 638 | read_attribute (struct attribute *attr, |
| 639 | struct attr_abbrev *abbrev, |
| 640 | struct comp_unit *unit, |
| 641 | bfd_byte *info_ptr) |
| 642 | { |
| 643 | attr->name = abbrev->name; |
| 644 | info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr); |
| 645 | return info_ptr; |
| 646 | } |
| 647 | |
| 648 | /* Source line information table routines. */ |
| 649 | |
| 650 | #define FILE_ALLOC_CHUNK 5 |
| 651 | #define DIR_ALLOC_CHUNK 5 |
| 652 | |
| 653 | struct line_info |
| 654 | { |
| 655 | struct line_info* prev_line; |
| 656 | bfd_vma address; |
| 657 | char *filename; |
| 658 | unsigned int line; |
| 659 | unsigned int column; |
| 660 | int end_sequence; /* End of (sequential) code sequence. */ |
| 661 | }; |
| 662 | |
| 663 | struct fileinfo |
| 664 | { |
| 665 | char *name; |
| 666 | unsigned int dir; |
| 667 | unsigned int time; |
| 668 | unsigned int size; |
| 669 | }; |
| 670 | |
| 671 | struct line_info_table |
| 672 | { |
| 673 | bfd* abfd; |
| 674 | unsigned int num_files; |
| 675 | unsigned int num_dirs; |
| 676 | char *comp_dir; |
| 677 | char **dirs; |
| 678 | struct fileinfo* files; |
| 679 | struct line_info* last_line; /* largest VMA */ |
| 680 | struct line_info* lcl_head; /* local head; used in 'add_line_info' */ |
| 681 | }; |
| 682 | |
| 683 | struct funcinfo |
| 684 | { |
| 685 | struct funcinfo *prev_func; |
| 686 | char *name; |
| 687 | struct arange arange; |
| 688 | }; |
| 689 | |
| 690 | /* Adds a new entry to the line_info list in the line_info_table, ensuring |
| 691 | that the list is sorted. Note that the line_info list is sorted from |
| 692 | highest to lowest VMA (with possible duplicates); that is, |
| 693 | line_info->prev_line always accesses an equal or smaller VMA. */ |
| 694 | |
| 695 | static void |
| 696 | add_line_info (struct line_info_table *table, |
| 697 | bfd_vma address, |
| 698 | char *filename, |
| 699 | unsigned int line, |
| 700 | unsigned int column, |
| 701 | int end_sequence) |
| 702 | { |
| 703 | bfd_size_type amt = sizeof (struct line_info); |
| 704 | struct line_info* info = bfd_alloc (table->abfd, amt); |
| 705 | |
| 706 | /* Find the correct location for 'info'. Normally we will receive |
| 707 | new line_info data 1) in order and 2) with increasing VMAs. |
| 708 | However some compilers break the rules (cf. decode_line_info) and |
| 709 | so we include some heuristics for quickly finding the correct |
| 710 | location for 'info'. In particular, these heuristics optimize for |
| 711 | the common case in which the VMA sequence that we receive is a |
| 712 | list of locally sorted VMAs such as |
| 713 | p...z a...j (where a < j < p < z) |
| 714 | |
| 715 | Note: table->lcl_head is used to head an *actual* or *possible* |
| 716 | sequence within the list (such as a...j) that is not directly |
| 717 | headed by table->last_line |
| 718 | |
| 719 | Note: we may receive duplicate entries from 'decode_line_info'. */ |
| 720 | |
| 721 | while (1) |
| 722 | if (!table->last_line |
| 723 | || address >= table->last_line->address) |
| 724 | { |
| 725 | /* Normal case: add 'info' to the beginning of the list */ |
| 726 | info->prev_line = table->last_line; |
| 727 | table->last_line = info; |
| 728 | |
| 729 | /* lcl_head: initialize to head a *possible* sequence at the end. */ |
| 730 | if (!table->lcl_head) |
| 731 | table->lcl_head = info; |
| 732 | break; |
| 733 | } |
| 734 | else if (!table->lcl_head->prev_line |
| 735 | && table->lcl_head->address > address) |
| 736 | { |
| 737 | /* Abnormal but easy: lcl_head is 1) at the *end* of the line |
| 738 | list and 2) the head of 'info'. */ |
| 739 | info->prev_line = NULL; |
| 740 | table->lcl_head->prev_line = info; |
| 741 | break; |
| 742 | } |
| 743 | else if (table->lcl_head->prev_line |
| 744 | && table->lcl_head->address > address |
| 745 | && address >= table->lcl_head->prev_line->address) |
| 746 | { |
| 747 | /* Abnormal but easy: lcl_head is 1) in the *middle* of the line |
| 748 | list and 2) the head of 'info'. */ |
| 749 | info->prev_line = table->lcl_head->prev_line; |
| 750 | table->lcl_head->prev_line = info; |
| 751 | break; |
| 752 | } |
| 753 | else |
| 754 | { |
| 755 | /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid |
| 756 | heads for 'info'. Reset 'lcl_head' and repeat. */ |
| 757 | struct line_info* li2 = table->last_line; /* always non-NULL */ |
| 758 | struct line_info* li1 = li2->prev_line; |
| 759 | |
| 760 | while (li1) |
| 761 | { |
| 762 | if (li2->address > address && address >= li1->address) |
| 763 | break; |
| 764 | |
| 765 | li2 = li1; /* always non-NULL */ |
| 766 | li1 = li1->prev_line; |
| 767 | } |
| 768 | table->lcl_head = li2; |
| 769 | } |
| 770 | |
| 771 | /* Set member data of 'info'. */ |
| 772 | info->address = address; |
| 773 | info->line = line; |
| 774 | info->column = column; |
| 775 | info->end_sequence = end_sequence; |
| 776 | |
| 777 | if (filename && filename[0]) |
| 778 | { |
| 779 | info->filename = bfd_alloc (table->abfd, strlen (filename) + 1); |
| 780 | if (info->filename) |
| 781 | strcpy (info->filename, filename); |
| 782 | } |
| 783 | else |
| 784 | info->filename = NULL; |
| 785 | } |
| 786 | |
| 787 | /* Extract a fully qualified filename from a line info table. |
| 788 | The returned string has been malloc'ed and it is the caller's |
| 789 | responsibility to free it. */ |
| 790 | |
| 791 | static char * |
| 792 | concat_filename (struct line_info_table *table, unsigned int file) |
| 793 | { |
| 794 | char *filename; |
| 795 | |
| 796 | if (file - 1 >= table->num_files) |
| 797 | { |
| 798 | (*_bfd_error_handler) |
| 799 | (_("Dwarf Error: mangled line number section (bad file number).")); |
| 800 | return strdup ("<unknown>"); |
| 801 | } |
| 802 | |
| 803 | filename = table->files[file - 1].name; |
| 804 | |
| 805 | if (! IS_ABSOLUTE_PATH (filename)) |
| 806 | { |
| 807 | char *dirname = (table->files[file - 1].dir |
| 808 | ? table->dirs[table->files[file - 1].dir - 1] |
| 809 | : table->comp_dir); |
| 810 | |
| 811 | /* Not all tools set DW_AT_comp_dir, so dirname may be unknown. |
| 812 | The best we can do is return the filename part. */ |
| 813 | if (dirname != NULL) |
| 814 | { |
| 815 | unsigned int len = strlen (dirname) + strlen (filename) + 2; |
| 816 | char * name; |
| 817 | |
| 818 | name = bfd_malloc (len); |
| 819 | if (name) |
| 820 | sprintf (name, "%s/%s", dirname, filename); |
| 821 | return name; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | return strdup (filename); |
| 826 | } |
| 827 | |
| 828 | static void |
| 829 | arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc) |
| 830 | { |
| 831 | struct arange *arange; |
| 832 | |
| 833 | /* If the first arange is empty, use it. */ |
| 834 | if (first_arange->high == 0) |
| 835 | { |
| 836 | first_arange->low = low_pc; |
| 837 | first_arange->high = high_pc; |
| 838 | return; |
| 839 | } |
| 840 | |
| 841 | /* Next see if we can cheaply extend an existing range. */ |
| 842 | arange = first_arange; |
| 843 | do |
| 844 | { |
| 845 | if (low_pc == arange->high) |
| 846 | { |
| 847 | arange->high = high_pc; |
| 848 | return; |
| 849 | } |
| 850 | if (high_pc == arange->low) |
| 851 | { |
| 852 | arange->low = low_pc; |
| 853 | return; |
| 854 | } |
| 855 | arange = arange->next; |
| 856 | } |
| 857 | while (arange); |
| 858 | |
| 859 | /* Need to allocate a new arange and insert it into the arange list. |
| 860 | Order isn't significant, so just insert after the first arange. */ |
| 861 | arange = bfd_zalloc (abfd, sizeof (*arange)); |
| 862 | arange->low = low_pc; |
| 863 | arange->high = high_pc; |
| 864 | arange->next = first_arange->next; |
| 865 | first_arange->next = arange; |
| 866 | } |
| 867 | |
| 868 | /* Decode the line number information for UNIT. */ |
| 869 | |
| 870 | static struct line_info_table* |
| 871 | decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash) |
| 872 | { |
| 873 | bfd *abfd = unit->abfd; |
| 874 | struct line_info_table* table; |
| 875 | bfd_byte *line_ptr; |
| 876 | bfd_byte *line_end; |
| 877 | struct line_head lh; |
| 878 | unsigned int i, bytes_read, offset_size; |
| 879 | char *cur_file, *cur_dir; |
| 880 | unsigned char op_code, extended_op, adj_opcode; |
| 881 | bfd_size_type amt; |
| 882 | |
| 883 | if (! stash->dwarf_line_buffer) |
| 884 | { |
| 885 | asection *msec; |
| 886 | |
| 887 | msec = bfd_get_section_by_name (abfd, ".debug_line"); |
| 888 | if (! msec) |
| 889 | { |
| 890 | (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section.")); |
| 891 | bfd_set_error (bfd_error_bad_value); |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | stash->dwarf_line_size = msec->size; |
| 896 | stash->dwarf_line_buffer |
| 897 | = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, |
| 898 | stash->syms); |
| 899 | if (! stash->dwarf_line_buffer) |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | /* It is possible to get a bad value for the line_offset. Validate |
| 904 | it here so that we won't get a segfault below. */ |
| 905 | if (unit->line_offset >= stash->dwarf_line_size) |
| 906 | { |
| 907 | (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."), |
| 908 | unit->line_offset, stash->dwarf_line_size); |
| 909 | bfd_set_error (bfd_error_bad_value); |
| 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | amt = sizeof (struct line_info_table); |
| 914 | table = bfd_alloc (abfd, amt); |
| 915 | table->abfd = abfd; |
| 916 | table->comp_dir = unit->comp_dir; |
| 917 | |
| 918 | table->num_files = 0; |
| 919 | table->files = NULL; |
| 920 | |
| 921 | table->num_dirs = 0; |
| 922 | table->dirs = NULL; |
| 923 | |
| 924 | table->files = NULL; |
| 925 | table->last_line = NULL; |
| 926 | table->lcl_head = NULL; |
| 927 | |
| 928 | line_ptr = stash->dwarf_line_buffer + unit->line_offset; |
| 929 | |
| 930 | /* Read in the prologue. */ |
| 931 | lh.total_length = read_4_bytes (abfd, line_ptr); |
| 932 | line_ptr += 4; |
| 933 | offset_size = 4; |
| 934 | if (lh.total_length == 0xffffffff) |
| 935 | { |
| 936 | lh.total_length = read_8_bytes (abfd, line_ptr); |
| 937 | line_ptr += 8; |
| 938 | offset_size = 8; |
| 939 | } |
| 940 | else if (lh.total_length == 0 && unit->addr_size == 8) |
| 941 | { |
| 942 | /* Handle (non-standard) 64-bit DWARF2 formats. */ |
| 943 | lh.total_length = read_4_bytes (abfd, line_ptr); |
| 944 | line_ptr += 4; |
| 945 | offset_size = 8; |
| 946 | } |
| 947 | line_end = line_ptr + lh.total_length; |
| 948 | lh.version = read_2_bytes (abfd, line_ptr); |
| 949 | line_ptr += 2; |
| 950 | if (offset_size == 4) |
| 951 | lh.prologue_length = read_4_bytes (abfd, line_ptr); |
| 952 | else |
| 953 | lh.prologue_length = read_8_bytes (abfd, line_ptr); |
| 954 | line_ptr += offset_size; |
| 955 | lh.minimum_instruction_length = read_1_byte (abfd, line_ptr); |
| 956 | line_ptr += 1; |
| 957 | lh.default_is_stmt = read_1_byte (abfd, line_ptr); |
| 958 | line_ptr += 1; |
| 959 | lh.line_base = read_1_signed_byte (abfd, line_ptr); |
| 960 | line_ptr += 1; |
| 961 | lh.line_range = read_1_byte (abfd, line_ptr); |
| 962 | line_ptr += 1; |
| 963 | lh.opcode_base = read_1_byte (abfd, line_ptr); |
| 964 | line_ptr += 1; |
| 965 | amt = lh.opcode_base * sizeof (unsigned char); |
| 966 | lh.standard_opcode_lengths = bfd_alloc (abfd, amt); |
| 967 | |
| 968 | lh.standard_opcode_lengths[0] = 1; |
| 969 | |
| 970 | for (i = 1; i < lh.opcode_base; ++i) |
| 971 | { |
| 972 | lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr); |
| 973 | line_ptr += 1; |
| 974 | } |
| 975 | |
| 976 | /* Read directory table. */ |
| 977 | while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL) |
| 978 | { |
| 979 | line_ptr += bytes_read; |
| 980 | |
| 981 | if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0) |
| 982 | { |
| 983 | char **tmp; |
| 984 | |
| 985 | amt = table->num_dirs + DIR_ALLOC_CHUNK; |
| 986 | amt *= sizeof (char *); |
| 987 | |
| 988 | tmp = bfd_realloc (table->dirs, amt); |
| 989 | if (tmp == NULL) |
| 990 | { |
| 991 | free (table->dirs); |
| 992 | return NULL; |
| 993 | } |
| 994 | table->dirs = tmp; |
| 995 | } |
| 996 | |
| 997 | table->dirs[table->num_dirs++] = cur_dir; |
| 998 | } |
| 999 | |
| 1000 | line_ptr += bytes_read; |
| 1001 | |
| 1002 | /* Read file name table. */ |
| 1003 | while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL) |
| 1004 | { |
| 1005 | line_ptr += bytes_read; |
| 1006 | |
| 1007 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) |
| 1008 | { |
| 1009 | struct fileinfo *tmp; |
| 1010 | |
| 1011 | amt = table->num_files + FILE_ALLOC_CHUNK; |
| 1012 | amt *= sizeof (struct fileinfo); |
| 1013 | |
| 1014 | tmp = bfd_realloc (table->files, amt); |
| 1015 | if (tmp == NULL) |
| 1016 | { |
| 1017 | free (table->files); |
| 1018 | free (table->dirs); |
| 1019 | return NULL; |
| 1020 | } |
| 1021 | table->files = tmp; |
| 1022 | } |
| 1023 | |
| 1024 | table->files[table->num_files].name = cur_file; |
| 1025 | table->files[table->num_files].dir = |
| 1026 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1027 | line_ptr += bytes_read; |
| 1028 | table->files[table->num_files].time = |
| 1029 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1030 | line_ptr += bytes_read; |
| 1031 | table->files[table->num_files].size = |
| 1032 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1033 | line_ptr += bytes_read; |
| 1034 | table->num_files++; |
| 1035 | } |
| 1036 | |
| 1037 | line_ptr += bytes_read; |
| 1038 | |
| 1039 | /* Read the statement sequences until there's nothing left. */ |
| 1040 | while (line_ptr < line_end) |
| 1041 | { |
| 1042 | /* State machine registers. */ |
| 1043 | bfd_vma address = 0; |
| 1044 | char * filename = table->num_files ? concat_filename (table, 1) : NULL; |
| 1045 | unsigned int line = 1; |
| 1046 | unsigned int column = 0; |
| 1047 | int is_stmt = lh.default_is_stmt; |
| 1048 | int basic_block = 0; |
| 1049 | int end_sequence = 0; |
| 1050 | /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some |
| 1051 | compilers generate address sequences that are wildly out of |
| 1052 | order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler |
| 1053 | for ia64-Linux). Thus, to determine the low and high |
| 1054 | address, we must compare on every DW_LNS_copy, etc. */ |
| 1055 | bfd_vma low_pc = 0; |
| 1056 | bfd_vma high_pc = 0; |
| 1057 | |
| 1058 | /* Decode the table. */ |
| 1059 | while (! end_sequence) |
| 1060 | { |
| 1061 | op_code = read_1_byte (abfd, line_ptr); |
| 1062 | line_ptr += 1; |
| 1063 | |
| 1064 | if (op_code >= lh.opcode_base) |
| 1065 | { |
| 1066 | /* Special operand. */ |
| 1067 | adj_opcode = op_code - lh.opcode_base; |
| 1068 | address += (adj_opcode / lh.line_range) |
| 1069 | * lh.minimum_instruction_length; |
| 1070 | line += lh.line_base + (adj_opcode % lh.line_range); |
| 1071 | /* Append row to matrix using current values. */ |
| 1072 | add_line_info (table, address, filename, line, column, 0); |
| 1073 | basic_block = 1; |
| 1074 | if (low_pc == 0 || address < low_pc) |
| 1075 | low_pc = address; |
| 1076 | if (address > high_pc) |
| 1077 | high_pc = address; |
| 1078 | } |
| 1079 | else switch (op_code) |
| 1080 | { |
| 1081 | case DW_LNS_extended_op: |
| 1082 | /* Ignore length. */ |
| 1083 | line_ptr += 1; |
| 1084 | extended_op = read_1_byte (abfd, line_ptr); |
| 1085 | line_ptr += 1; |
| 1086 | |
| 1087 | switch (extended_op) |
| 1088 | { |
| 1089 | case DW_LNE_end_sequence: |
| 1090 | end_sequence = 1; |
| 1091 | add_line_info (table, address, filename, line, column, |
| 1092 | end_sequence); |
| 1093 | if (low_pc == 0 || address < low_pc) |
| 1094 | low_pc = address; |
| 1095 | if (address > high_pc) |
| 1096 | high_pc = address; |
| 1097 | arange_add (unit->abfd, &unit->arange, low_pc, high_pc); |
| 1098 | break; |
| 1099 | case DW_LNE_set_address: |
| 1100 | address = read_address (unit, line_ptr); |
| 1101 | line_ptr += unit->addr_size; |
| 1102 | break; |
| 1103 | case DW_LNE_define_file: |
| 1104 | cur_file = read_string (abfd, line_ptr, &bytes_read); |
| 1105 | line_ptr += bytes_read; |
| 1106 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) |
| 1107 | { |
| 1108 | struct fileinfo *tmp; |
| 1109 | |
| 1110 | amt = table->num_files + FILE_ALLOC_CHUNK; |
| 1111 | amt *= sizeof (struct fileinfo); |
| 1112 | tmp = bfd_realloc (table->files, amt); |
| 1113 | if (tmp == NULL) |
| 1114 | { |
| 1115 | free (table->files); |
| 1116 | free (table->dirs); |
| 1117 | free (filename); |
| 1118 | return NULL; |
| 1119 | } |
| 1120 | table->files = tmp; |
| 1121 | } |
| 1122 | table->files[table->num_files].name = cur_file; |
| 1123 | table->files[table->num_files].dir = |
| 1124 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1125 | line_ptr += bytes_read; |
| 1126 | table->files[table->num_files].time = |
| 1127 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1128 | line_ptr += bytes_read; |
| 1129 | table->files[table->num_files].size = |
| 1130 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1131 | line_ptr += bytes_read; |
| 1132 | table->num_files++; |
| 1133 | break; |
| 1134 | default: |
| 1135 | (*_bfd_error_handler) (_("Dwarf Error: mangled line number section.")); |
| 1136 | bfd_set_error (bfd_error_bad_value); |
| 1137 | free (filename); |
| 1138 | free (table->files); |
| 1139 | free (table->dirs); |
| 1140 | return NULL; |
| 1141 | } |
| 1142 | break; |
| 1143 | case DW_LNS_copy: |
| 1144 | add_line_info (table, address, filename, line, column, 0); |
| 1145 | basic_block = 0; |
| 1146 | if (low_pc == 0 || address < low_pc) |
| 1147 | low_pc = address; |
| 1148 | if (address > high_pc) |
| 1149 | high_pc = address; |
| 1150 | break; |
| 1151 | case DW_LNS_advance_pc: |
| 1152 | address += lh.minimum_instruction_length |
| 1153 | * read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1154 | line_ptr += bytes_read; |
| 1155 | break; |
| 1156 | case DW_LNS_advance_line: |
| 1157 | line += read_signed_leb128 (abfd, line_ptr, &bytes_read); |
| 1158 | line_ptr += bytes_read; |
| 1159 | break; |
| 1160 | case DW_LNS_set_file: |
| 1161 | { |
| 1162 | unsigned int file; |
| 1163 | |
| 1164 | /* The file and directory tables are 0 |
| 1165 | based, the references are 1 based. */ |
| 1166 | file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1167 | line_ptr += bytes_read; |
| 1168 | if (filename) |
| 1169 | free (filename); |
| 1170 | filename = concat_filename (table, file); |
| 1171 | break; |
| 1172 | } |
| 1173 | case DW_LNS_set_column: |
| 1174 | column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1175 | line_ptr += bytes_read; |
| 1176 | break; |
| 1177 | case DW_LNS_negate_stmt: |
| 1178 | is_stmt = (!is_stmt); |
| 1179 | break; |
| 1180 | case DW_LNS_set_basic_block: |
| 1181 | basic_block = 1; |
| 1182 | break; |
| 1183 | case DW_LNS_const_add_pc: |
| 1184 | address += lh.minimum_instruction_length |
| 1185 | * ((255 - lh.opcode_base) / lh.line_range); |
| 1186 | break; |
| 1187 | case DW_LNS_fixed_advance_pc: |
| 1188 | address += read_2_bytes (abfd, line_ptr); |
| 1189 | line_ptr += 2; |
| 1190 | break; |
| 1191 | default: |
| 1192 | { |
| 1193 | int i; |
| 1194 | |
| 1195 | /* Unknown standard opcode, ignore it. */ |
| 1196 | for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++) |
| 1197 | { |
| 1198 | (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 1199 | line_ptr += bytes_read; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | if (filename) |
| 1206 | free (filename); |
| 1207 | } |
| 1208 | |
| 1209 | return table; |
| 1210 | } |
| 1211 | |
| 1212 | /* If ADDR is within TABLE set the output parameters and return TRUE, |
| 1213 | otherwise return FALSE. The output parameters, FILENAME_PTR and |
| 1214 | LINENUMBER_PTR, are pointers to the objects to be filled in. */ |
| 1215 | |
| 1216 | static bfd_boolean |
| 1217 | lookup_address_in_line_info_table (struct line_info_table *table, |
| 1218 | bfd_vma addr, |
| 1219 | struct funcinfo *function, |
| 1220 | const char **filename_ptr, |
| 1221 | unsigned int *linenumber_ptr) |
| 1222 | { |
| 1223 | /* Note: table->last_line should be a descendingly sorted list. */ |
| 1224 | struct line_info* next_line = table->last_line; |
| 1225 | struct line_info* each_line = NULL; |
| 1226 | *filename_ptr = NULL; |
| 1227 | |
| 1228 | if (!next_line) |
| 1229 | return FALSE; |
| 1230 | |
| 1231 | each_line = next_line->prev_line; |
| 1232 | |
| 1233 | /* Check for large addresses */ |
| 1234 | if (addr > next_line->address) |
| 1235 | each_line = NULL; /* ensure we skip over the normal case */ |
| 1236 | |
| 1237 | /* Normal case: search the list; save */ |
| 1238 | while (each_line && next_line) |
| 1239 | { |
| 1240 | /* If we have an address match, save this info. This allows us |
| 1241 | to return as good as results as possible for strange debugging |
| 1242 | info. */ |
| 1243 | bfd_boolean addr_match = FALSE; |
| 1244 | if (each_line->address <= addr && addr < next_line->address) |
| 1245 | { |
| 1246 | addr_match = TRUE; |
| 1247 | |
| 1248 | /* If this line appears to span functions, and addr is in the |
| 1249 | later function, return the first line of that function instead |
| 1250 | of the last line of the earlier one. This check is for GCC |
| 1251 | 2.95, which emits the first line number for a function late. */ |
| 1252 | |
| 1253 | if (function != NULL) |
| 1254 | { |
| 1255 | bfd_vma lowest_pc; |
| 1256 | struct arange *arange; |
| 1257 | |
| 1258 | /* Find the lowest address in the function's range list */ |
| 1259 | lowest_pc = function->arange.low; |
| 1260 | for (arange = &function->arange; |
| 1261 | arange; |
| 1262 | arange = arange->next) |
| 1263 | { |
| 1264 | if (function->arange.low < lowest_pc) |
| 1265 | lowest_pc = function->arange.low; |
| 1266 | } |
| 1267 | /* Check for spanning function and set outgoing line info */ |
| 1268 | if (addr >= lowest_pc |
| 1269 | && each_line->address < lowest_pc |
| 1270 | && next_line->address > lowest_pc) |
| 1271 | { |
| 1272 | *filename_ptr = next_line->filename; |
| 1273 | *linenumber_ptr = next_line->line; |
| 1274 | } |
| 1275 | else |
| 1276 | { |
| 1277 | *filename_ptr = each_line->filename; |
| 1278 | *linenumber_ptr = each_line->line; |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | if (addr_match && !each_line->end_sequence) |
| 1284 | return TRUE; /* we have definitely found what we want */ |
| 1285 | |
| 1286 | next_line = each_line; |
| 1287 | each_line = each_line->prev_line; |
| 1288 | } |
| 1289 | |
| 1290 | /* At this point each_line is NULL but next_line is not. If we found |
| 1291 | a candidate end-of-sequence point in the loop above, we can return |
| 1292 | that (compatibility with a bug in the Intel compiler); otherwise, |
| 1293 | assuming that we found the containing function for this address in |
| 1294 | this compilation unit, return the first line we have a number for |
| 1295 | (compatibility with GCC 2.95). */ |
| 1296 | if (*filename_ptr == NULL && function != NULL) |
| 1297 | { |
| 1298 | *filename_ptr = next_line->filename; |
| 1299 | *linenumber_ptr = next_line->line; |
| 1300 | return TRUE; |
| 1301 | } |
| 1302 | |
| 1303 | return FALSE; |
| 1304 | } |
| 1305 | |
| 1306 | /* Read in the .debug_ranges section for future reference */ |
| 1307 | |
| 1308 | static bfd_boolean |
| 1309 | read_debug_ranges (struct comp_unit *unit) |
| 1310 | { |
| 1311 | struct dwarf2_debug *stash = unit->stash; |
| 1312 | if (! stash->dwarf_ranges_buffer) |
| 1313 | { |
| 1314 | bfd *abfd = unit->abfd; |
| 1315 | asection *msec; |
| 1316 | |
| 1317 | msec = bfd_get_section_by_name (abfd, ".debug_ranges"); |
| 1318 | if (! msec) |
| 1319 | { |
| 1320 | (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section.")); |
| 1321 | bfd_set_error (bfd_error_bad_value); |
| 1322 | return FALSE; |
| 1323 | } |
| 1324 | |
| 1325 | stash->dwarf_ranges_size = msec->size; |
| 1326 | stash->dwarf_ranges_buffer |
| 1327 | = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, |
| 1328 | stash->syms); |
| 1329 | if (! stash->dwarf_ranges_buffer) |
| 1330 | return FALSE; |
| 1331 | } |
| 1332 | return TRUE; |
| 1333 | } |
| 1334 | |
| 1335 | /* Function table functions. */ |
| 1336 | |
| 1337 | /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. |
| 1338 | Note that we need to find the function that has the smallest |
| 1339 | range that contains ADDR, to handle inlined functions without |
| 1340 | depending upon them being ordered in TABLE by increasing range. */ |
| 1341 | |
| 1342 | static bfd_boolean |
| 1343 | lookup_address_in_function_table (struct funcinfo *table, |
| 1344 | bfd_vma addr, |
| 1345 | struct funcinfo **function_ptr, |
| 1346 | const char **functionname_ptr) |
| 1347 | { |
| 1348 | struct funcinfo* each_func; |
| 1349 | struct funcinfo* best_fit = NULL; |
| 1350 | struct arange *arange; |
| 1351 | |
| 1352 | for (each_func = table; |
| 1353 | each_func; |
| 1354 | each_func = each_func->prev_func) |
| 1355 | { |
| 1356 | for (arange = &each_func->arange; |
| 1357 | arange; |
| 1358 | arange = arange->next) |
| 1359 | { |
| 1360 | if (addr >= arange->low && addr < arange->high) |
| 1361 | { |
| 1362 | if (!best_fit || |
| 1363 | ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low))) |
| 1364 | best_fit = each_func; |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | if (best_fit) |
| 1370 | { |
| 1371 | *functionname_ptr = best_fit->name; |
| 1372 | *function_ptr = best_fit; |
| 1373 | return TRUE; |
| 1374 | } |
| 1375 | else |
| 1376 | { |
| 1377 | return FALSE; |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | static char * |
| 1382 | find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref) |
| 1383 | { |
| 1384 | bfd *abfd = unit->abfd; |
| 1385 | bfd_byte *info_ptr; |
| 1386 | unsigned int abbrev_number, bytes_read, i; |
| 1387 | struct abbrev_info *abbrev; |
| 1388 | struct attribute attr; |
| 1389 | char *name = 0; |
| 1390 | |
| 1391 | info_ptr = unit->info_ptr_unit + die_ref; |
| 1392 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 1393 | info_ptr += bytes_read; |
| 1394 | |
| 1395 | if (abbrev_number) |
| 1396 | { |
| 1397 | abbrev = lookup_abbrev (abbrev_number, unit->abbrevs); |
| 1398 | if (! abbrev) |
| 1399 | { |
| 1400 | (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), |
| 1401 | abbrev_number); |
| 1402 | bfd_set_error (bfd_error_bad_value); |
| 1403 | } |
| 1404 | else |
| 1405 | { |
| 1406 | for (i = 0; i < abbrev->num_attrs && !name; ++i) |
| 1407 | { |
| 1408 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); |
| 1409 | switch (attr.name) |
| 1410 | { |
| 1411 | case DW_AT_name: |
| 1412 | name = attr.u.str; |
| 1413 | break; |
| 1414 | case DW_AT_specification: |
| 1415 | name = find_abstract_instance_name (unit, attr.u.val); |
| 1416 | break; |
| 1417 | default: |
| 1418 | break; |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | return (name); |
| 1424 | } |
| 1425 | |
| 1426 | static void |
| 1427 | read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset) |
| 1428 | { |
| 1429 | bfd_byte *ranges_ptr; |
| 1430 | bfd_vma base_address = unit->base_address; |
| 1431 | |
| 1432 | if (! unit->stash->dwarf_ranges_buffer) |
| 1433 | { |
| 1434 | if (! read_debug_ranges (unit)) |
| 1435 | return; |
| 1436 | } |
| 1437 | ranges_ptr = unit->stash->dwarf_ranges_buffer + offset; |
| 1438 | |
| 1439 | for (;;) |
| 1440 | { |
| 1441 | bfd_vma low_pc; |
| 1442 | bfd_vma high_pc; |
| 1443 | |
| 1444 | if (unit->offset_size == 4) |
| 1445 | { |
| 1446 | low_pc = read_4_bytes (unit->abfd, ranges_ptr); |
| 1447 | ranges_ptr += 4; |
| 1448 | high_pc = read_4_bytes (unit->abfd, ranges_ptr); |
| 1449 | ranges_ptr += 4; |
| 1450 | } |
| 1451 | else |
| 1452 | { |
| 1453 | low_pc = read_8_bytes (unit->abfd, ranges_ptr); |
| 1454 | ranges_ptr += 8; |
| 1455 | high_pc = read_8_bytes (unit->abfd, ranges_ptr); |
| 1456 | ranges_ptr += 8; |
| 1457 | } |
| 1458 | if (low_pc == 0 && high_pc == 0) |
| 1459 | break; |
| 1460 | if (low_pc == -1UL && high_pc != -1UL) |
| 1461 | base_address = high_pc; |
| 1462 | else |
| 1463 | arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc); |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | /* DWARF2 Compilation unit functions. */ |
| 1468 | |
| 1469 | /* Scan over each die in a comp. unit looking for functions to add |
| 1470 | to the function table. */ |
| 1471 | |
| 1472 | static bfd_boolean |
| 1473 | scan_unit_for_functions (struct comp_unit *unit) |
| 1474 | { |
| 1475 | bfd *abfd = unit->abfd; |
| 1476 | bfd_byte *info_ptr = unit->first_child_die_ptr; |
| 1477 | int nesting_level = 1; |
| 1478 | |
| 1479 | while (nesting_level) |
| 1480 | { |
| 1481 | unsigned int abbrev_number, bytes_read, i; |
| 1482 | struct abbrev_info *abbrev; |
| 1483 | struct attribute attr; |
| 1484 | struct funcinfo *func; |
| 1485 | bfd_vma low_pc = 0; |
| 1486 | bfd_vma high_pc = 0; |
| 1487 | |
| 1488 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 1489 | info_ptr += bytes_read; |
| 1490 | |
| 1491 | if (! abbrev_number) |
| 1492 | { |
| 1493 | nesting_level--; |
| 1494 | continue; |
| 1495 | } |
| 1496 | |
| 1497 | abbrev = lookup_abbrev (abbrev_number,unit->abbrevs); |
| 1498 | if (! abbrev) |
| 1499 | { |
| 1500 | (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), |
| 1501 | abbrev_number); |
| 1502 | bfd_set_error (bfd_error_bad_value); |
| 1503 | return FALSE; |
| 1504 | } |
| 1505 | |
| 1506 | if (abbrev->tag == DW_TAG_subprogram |
| 1507 | || abbrev->tag == DW_TAG_inlined_subroutine) |
| 1508 | { |
| 1509 | bfd_size_type amt = sizeof (struct funcinfo); |
| 1510 | func = bfd_zalloc (abfd, amt); |
| 1511 | func->prev_func = unit->function_table; |
| 1512 | unit->function_table = func; |
| 1513 | } |
| 1514 | else |
| 1515 | func = NULL; |
| 1516 | |
| 1517 | for (i = 0; i < abbrev->num_attrs; ++i) |
| 1518 | { |
| 1519 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); |
| 1520 | |
| 1521 | if (func) |
| 1522 | { |
| 1523 | switch (attr.name) |
| 1524 | { |
| 1525 | case DW_AT_abstract_origin: |
| 1526 | func->name = find_abstract_instance_name (unit, attr.u.val); |
| 1527 | break; |
| 1528 | |
| 1529 | case DW_AT_name: |
| 1530 | /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */ |
| 1531 | if (func->name == NULL) |
| 1532 | func->name = attr.u.str; |
| 1533 | break; |
| 1534 | |
| 1535 | case DW_AT_MIPS_linkage_name: |
| 1536 | func->name = attr.u.str; |
| 1537 | break; |
| 1538 | |
| 1539 | case DW_AT_low_pc: |
| 1540 | low_pc = attr.u.val; |
| 1541 | break; |
| 1542 | |
| 1543 | case DW_AT_high_pc: |
| 1544 | high_pc = attr.u.val; |
| 1545 | break; |
| 1546 | |
| 1547 | case DW_AT_ranges: |
| 1548 | read_rangelist (unit, &func->arange, attr.u.val); |
| 1549 | break; |
| 1550 | |
| 1551 | default: |
| 1552 | break; |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | if (func && high_pc != 0) |
| 1558 | { |
| 1559 | arange_add (unit->abfd, &func->arange, low_pc, high_pc); |
| 1560 | } |
| 1561 | |
| 1562 | if (abbrev->has_children) |
| 1563 | nesting_level++; |
| 1564 | } |
| 1565 | |
| 1566 | return TRUE; |
| 1567 | } |
| 1568 | |
| 1569 | /* Parse a DWARF2 compilation unit starting at INFO_PTR. This |
| 1570 | includes the compilation unit header that proceeds the DIE's, but |
| 1571 | does not include the length field that precedes each compilation |
| 1572 | unit header. END_PTR points one past the end of this comp unit. |
| 1573 | OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes). |
| 1574 | |
| 1575 | This routine does not read the whole compilation unit; only enough |
| 1576 | to get to the line number information for the compilation unit. */ |
| 1577 | |
| 1578 | static struct comp_unit * |
| 1579 | parse_comp_unit (bfd *abfd, |
| 1580 | struct dwarf2_debug *stash, |
| 1581 | bfd_vma unit_length, |
| 1582 | bfd_byte *info_ptr_unit, |
| 1583 | unsigned int offset_size) |
| 1584 | { |
| 1585 | struct comp_unit* unit; |
| 1586 | unsigned int version; |
| 1587 | bfd_uint64_t abbrev_offset = 0; |
| 1588 | unsigned int addr_size; |
| 1589 | struct abbrev_info** abbrevs; |
| 1590 | unsigned int abbrev_number, bytes_read, i; |
| 1591 | struct abbrev_info *abbrev; |
| 1592 | struct attribute attr; |
| 1593 | bfd_byte *info_ptr = stash->info_ptr; |
| 1594 | bfd_byte *end_ptr = info_ptr + unit_length; |
| 1595 | bfd_size_type amt; |
| 1596 | bfd_vma low_pc = 0; |
| 1597 | bfd_vma high_pc = 0; |
| 1598 | |
| 1599 | version = read_2_bytes (abfd, info_ptr); |
| 1600 | info_ptr += 2; |
| 1601 | BFD_ASSERT (offset_size == 4 || offset_size == 8); |
| 1602 | if (offset_size == 4) |
| 1603 | abbrev_offset = read_4_bytes (abfd, info_ptr); |
| 1604 | else |
| 1605 | abbrev_offset = read_8_bytes (abfd, info_ptr); |
| 1606 | info_ptr += offset_size; |
| 1607 | addr_size = read_1_byte (abfd, info_ptr); |
| 1608 | info_ptr += 1; |
| 1609 | |
| 1610 | if (version != 2) |
| 1611 | { |
| 1612 | (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version); |
| 1613 | bfd_set_error (bfd_error_bad_value); |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | if (addr_size > sizeof (bfd_vma)) |
| 1618 | { |
| 1619 | (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."), |
| 1620 | addr_size, |
| 1621 | (unsigned int) sizeof (bfd_vma)); |
| 1622 | bfd_set_error (bfd_error_bad_value); |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
| 1626 | if (addr_size != 2 && addr_size != 4 && addr_size != 8) |
| 1627 | { |
| 1628 | (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size); |
| 1629 | bfd_set_error (bfd_error_bad_value); |
| 1630 | return 0; |
| 1631 | } |
| 1632 | |
| 1633 | /* Read the abbrevs for this compilation unit into a table. */ |
| 1634 | abbrevs = read_abbrevs (abfd, abbrev_offset, stash); |
| 1635 | if (! abbrevs) |
| 1636 | return 0; |
| 1637 | |
| 1638 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 1639 | info_ptr += bytes_read; |
| 1640 | if (! abbrev_number) |
| 1641 | { |
| 1642 | (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."), |
| 1643 | abbrev_number); |
| 1644 | bfd_set_error (bfd_error_bad_value); |
| 1645 | return 0; |
| 1646 | } |
| 1647 | |
| 1648 | abbrev = lookup_abbrev (abbrev_number, abbrevs); |
| 1649 | if (! abbrev) |
| 1650 | { |
| 1651 | (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), |
| 1652 | abbrev_number); |
| 1653 | bfd_set_error (bfd_error_bad_value); |
| 1654 | return 0; |
| 1655 | } |
| 1656 | |
| 1657 | amt = sizeof (struct comp_unit); |
| 1658 | unit = bfd_zalloc (abfd, amt); |
| 1659 | unit->abfd = abfd; |
| 1660 | unit->addr_size = addr_size; |
| 1661 | unit->offset_size = offset_size; |
| 1662 | unit->abbrevs = abbrevs; |
| 1663 | unit->end_ptr = end_ptr; |
| 1664 | unit->stash = stash; |
| 1665 | unit->info_ptr_unit = info_ptr_unit; |
| 1666 | |
| 1667 | for (i = 0; i < abbrev->num_attrs; ++i) |
| 1668 | { |
| 1669 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); |
| 1670 | |
| 1671 | /* Store the data if it is of an attribute we want to keep in a |
| 1672 | partial symbol table. */ |
| 1673 | switch (attr.name) |
| 1674 | { |
| 1675 | case DW_AT_stmt_list: |
| 1676 | unit->stmtlist = 1; |
| 1677 | unit->line_offset = attr.u.val; |
| 1678 | break; |
| 1679 | |
| 1680 | case DW_AT_name: |
| 1681 | unit->name = attr.u.str; |
| 1682 | break; |
| 1683 | |
| 1684 | case DW_AT_low_pc: |
| 1685 | low_pc = attr.u.val; |
| 1686 | /* If the compilation unit DIE has a DW_AT_low_pc attribute, |
| 1687 | this is the base address to use when reading location |
| 1688 | lists or range lists. */ |
| 1689 | unit->base_address = low_pc; |
| 1690 | break; |
| 1691 | |
| 1692 | case DW_AT_high_pc: |
| 1693 | high_pc = attr.u.val; |
| 1694 | break; |
| 1695 | |
| 1696 | case DW_AT_ranges: |
| 1697 | read_rangelist (unit, &unit->arange, attr.u.val); |
| 1698 | break; |
| 1699 | |
| 1700 | case DW_AT_comp_dir: |
| 1701 | { |
| 1702 | char *comp_dir = attr.u.str; |
| 1703 | if (comp_dir) |
| 1704 | { |
| 1705 | /* Irix 6.2 native cc prepends <machine>.: to the compilation |
| 1706 | directory, get rid of it. */ |
| 1707 | char *cp = strchr (comp_dir, ':'); |
| 1708 | |
| 1709 | if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/') |
| 1710 | comp_dir = cp + 1; |
| 1711 | } |
| 1712 | unit->comp_dir = comp_dir; |
| 1713 | break; |
| 1714 | } |
| 1715 | |
| 1716 | default: |
| 1717 | break; |
| 1718 | } |
| 1719 | } |
| 1720 | if (high_pc != 0) |
| 1721 | { |
| 1722 | arange_add (unit->abfd, &unit->arange, low_pc, high_pc); |
| 1723 | } |
| 1724 | |
| 1725 | unit->first_child_die_ptr = info_ptr; |
| 1726 | return unit; |
| 1727 | } |
| 1728 | |
| 1729 | /* Return TRUE if UNIT contains the address given by ADDR. */ |
| 1730 | |
| 1731 | static bfd_boolean |
| 1732 | comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr) |
| 1733 | { |
| 1734 | struct arange *arange; |
| 1735 | |
| 1736 | if (unit->error) |
| 1737 | return FALSE; |
| 1738 | |
| 1739 | arange = &unit->arange; |
| 1740 | do |
| 1741 | { |
| 1742 | if (addr >= arange->low && addr < arange->high) |
| 1743 | return TRUE; |
| 1744 | arange = arange->next; |
| 1745 | } |
| 1746 | while (arange); |
| 1747 | |
| 1748 | return FALSE; |
| 1749 | } |
| 1750 | |
| 1751 | /* If UNIT contains ADDR, set the output parameters to the values for |
| 1752 | the line containing ADDR. The output parameters, FILENAME_PTR, |
| 1753 | FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects |
| 1754 | to be filled in. |
| 1755 | |
| 1756 | Return TRUE if UNIT contains ADDR, and no errors were encountered; |
| 1757 | FALSE otherwise. */ |
| 1758 | |
| 1759 | static bfd_boolean |
| 1760 | comp_unit_find_nearest_line (struct comp_unit *unit, |
| 1761 | bfd_vma addr, |
| 1762 | const char **filename_ptr, |
| 1763 | const char **functionname_ptr, |
| 1764 | unsigned int *linenumber_ptr, |
| 1765 | struct dwarf2_debug *stash) |
| 1766 | { |
| 1767 | bfd_boolean line_p; |
| 1768 | bfd_boolean func_p; |
| 1769 | struct funcinfo *function; |
| 1770 | |
| 1771 | if (unit->error) |
| 1772 | return FALSE; |
| 1773 | |
| 1774 | if (! unit->line_table) |
| 1775 | { |
| 1776 | if (! unit->stmtlist) |
| 1777 | { |
| 1778 | unit->error = 1; |
| 1779 | return FALSE; |
| 1780 | } |
| 1781 | |
| 1782 | unit->line_table = decode_line_info (unit, stash); |
| 1783 | |
| 1784 | if (! unit->line_table) |
| 1785 | { |
| 1786 | unit->error = 1; |
| 1787 | return FALSE; |
| 1788 | } |
| 1789 | |
| 1790 | if (unit->first_child_die_ptr < unit->end_ptr |
| 1791 | && ! scan_unit_for_functions (unit)) |
| 1792 | { |
| 1793 | unit->error = 1; |
| 1794 | return FALSE; |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | function = NULL; |
| 1799 | func_p = lookup_address_in_function_table (unit->function_table, addr, |
| 1800 | &function, functionname_ptr); |
| 1801 | line_p = lookup_address_in_line_info_table (unit->line_table, addr, |
| 1802 | function, filename_ptr, |
| 1803 | linenumber_ptr); |
| 1804 | return line_p || func_p; |
| 1805 | } |
| 1806 | |
| 1807 | /* Locate a section in a BFD containing debugging info. The search starts |
| 1808 | from the section after AFTER_SEC, or from the first section in the BFD if |
| 1809 | AFTER_SEC is NULL. The search works by examining the names of the |
| 1810 | sections. There are two permissiable names. The first is .debug_info. |
| 1811 | This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi. |
| 1812 | This is a variation on the .debug_info section which has a checksum |
| 1813 | describing the contents appended onto the name. This allows the linker to |
| 1814 | identify and discard duplicate debugging sections for different |
| 1815 | compilation units. */ |
| 1816 | #define DWARF2_DEBUG_INFO ".debug_info" |
| 1817 | #define GNU_LINKONCE_INFO ".gnu.linkonce.wi." |
| 1818 | |
| 1819 | static asection * |
| 1820 | find_debug_info (bfd *abfd, asection *after_sec) |
| 1821 | { |
| 1822 | asection * msec; |
| 1823 | |
| 1824 | if (after_sec) |
| 1825 | msec = after_sec->next; |
| 1826 | else |
| 1827 | msec = abfd->sections; |
| 1828 | |
| 1829 | while (msec) |
| 1830 | { |
| 1831 | if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0) |
| 1832 | return msec; |
| 1833 | |
| 1834 | if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0) |
| 1835 | return msec; |
| 1836 | |
| 1837 | msec = msec->next; |
| 1838 | } |
| 1839 | |
| 1840 | return NULL; |
| 1841 | } |
| 1842 | |
| 1843 | /* The DWARF2 version of find_nearest_line. Return TRUE if the line |
| 1844 | is found without error. ADDR_SIZE is the number of bytes in the |
| 1845 | initial .debug_info length field and in the abbreviation offset. |
| 1846 | You may use zero to indicate that the default value should be |
| 1847 | used. */ |
| 1848 | |
| 1849 | bfd_boolean |
| 1850 | _bfd_dwarf2_find_nearest_line (bfd *abfd, |
| 1851 | asection *section, |
| 1852 | asymbol **symbols, |
| 1853 | bfd_vma offset, |
| 1854 | const char **filename_ptr, |
| 1855 | const char **functionname_ptr, |
| 1856 | unsigned int *linenumber_ptr, |
| 1857 | unsigned int addr_size, |
| 1858 | void **pinfo) |
| 1859 | { |
| 1860 | /* Read each compilation unit from the section .debug_info, and check |
| 1861 | to see if it contains the address we are searching for. If yes, |
| 1862 | lookup the address, and return the line number info. If no, go |
| 1863 | on to the next compilation unit. |
| 1864 | |
| 1865 | We keep a list of all the previously read compilation units, and |
| 1866 | a pointer to the next un-read compilation unit. Check the |
| 1867 | previously read units before reading more. */ |
| 1868 | struct dwarf2_debug *stash; |
| 1869 | |
| 1870 | /* What address are we looking for? */ |
| 1871 | bfd_vma addr; |
| 1872 | |
| 1873 | struct comp_unit* each; |
| 1874 | |
| 1875 | stash = *pinfo; |
| 1876 | addr = offset; |
| 1877 | if (section->output_section) |
| 1878 | addr += section->output_section->vma + section->output_offset; |
| 1879 | else |
| 1880 | addr += section->vma; |
| 1881 | *filename_ptr = NULL; |
| 1882 | *functionname_ptr = NULL; |
| 1883 | *linenumber_ptr = 0; |
| 1884 | |
| 1885 | /* The DWARF2 spec says that the initial length field, and the |
| 1886 | offset of the abbreviation table, should both be 4-byte values. |
| 1887 | However, some compilers do things differently. */ |
| 1888 | if (addr_size == 0) |
| 1889 | addr_size = 4; |
| 1890 | BFD_ASSERT (addr_size == 4 || addr_size == 8); |
| 1891 | |
| 1892 | if (! stash) |
| 1893 | { |
| 1894 | bfd_size_type total_size; |
| 1895 | asection *msec; |
| 1896 | bfd_size_type amt = sizeof (struct dwarf2_debug); |
| 1897 | |
| 1898 | stash = bfd_zalloc (abfd, amt); |
| 1899 | if (! stash) |
| 1900 | return FALSE; |
| 1901 | |
| 1902 | *pinfo = stash; |
| 1903 | |
| 1904 | msec = find_debug_info (abfd, NULL); |
| 1905 | if (! msec) |
| 1906 | /* No dwarf2 info. Note that at this point the stash |
| 1907 | has been allocated, but contains zeros, this lets |
| 1908 | future calls to this function fail quicker. */ |
| 1909 | return FALSE; |
| 1910 | |
| 1911 | /* There can be more than one DWARF2 info section in a BFD these days. |
| 1912 | Read them all in and produce one large stash. We do this in two |
| 1913 | passes - in the first pass we just accumulate the section sizes. |
| 1914 | In the second pass we read in the section's contents. The allows |
| 1915 | us to avoid reallocing the data as we add sections to the stash. */ |
| 1916 | for (total_size = 0; msec; msec = find_debug_info (abfd, msec)) |
| 1917 | total_size += msec->size; |
| 1918 | |
| 1919 | stash->info_ptr = bfd_alloc (abfd, total_size); |
| 1920 | if (stash->info_ptr == NULL) |
| 1921 | return FALSE; |
| 1922 | |
| 1923 | stash->info_ptr_end = stash->info_ptr; |
| 1924 | |
| 1925 | for (msec = find_debug_info (abfd, NULL); |
| 1926 | msec; |
| 1927 | msec = find_debug_info (abfd, msec)) |
| 1928 | { |
| 1929 | bfd_size_type size; |
| 1930 | bfd_size_type start; |
| 1931 | |
| 1932 | size = msec->size; |
| 1933 | if (size == 0) |
| 1934 | continue; |
| 1935 | |
| 1936 | start = stash->info_ptr_end - stash->info_ptr; |
| 1937 | |
| 1938 | if ((bfd_simple_get_relocated_section_contents |
| 1939 | (abfd, msec, stash->info_ptr + start, symbols)) == NULL) |
| 1940 | continue; |
| 1941 | |
| 1942 | stash->info_ptr_end = stash->info_ptr + start + size; |
| 1943 | } |
| 1944 | |
| 1945 | BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size); |
| 1946 | |
| 1947 | stash->sec = find_debug_info (abfd, NULL); |
| 1948 | stash->sec_info_ptr = stash->info_ptr; |
| 1949 | stash->syms = symbols; |
| 1950 | } |
| 1951 | |
| 1952 | /* A null info_ptr indicates that there is no dwarf2 info |
| 1953 | (or that an error occured while setting up the stash). */ |
| 1954 | if (! stash->info_ptr) |
| 1955 | return FALSE; |
| 1956 | |
| 1957 | /* Check the previously read comp. units first. */ |
| 1958 | for (each = stash->all_comp_units; each; each = each->next_unit) |
| 1959 | if (comp_unit_contains_address (each, addr)) |
| 1960 | return comp_unit_find_nearest_line (each, addr, filename_ptr, |
| 1961 | functionname_ptr, linenumber_ptr, |
| 1962 | stash); |
| 1963 | |
| 1964 | /* Read each remaining comp. units checking each as they are read. */ |
| 1965 | while (stash->info_ptr < stash->info_ptr_end) |
| 1966 | { |
| 1967 | bfd_vma length; |
| 1968 | bfd_boolean found; |
| 1969 | unsigned int offset_size = addr_size; |
| 1970 | bfd_byte *info_ptr_unit = stash->info_ptr; |
| 1971 | |
| 1972 | length = read_4_bytes (abfd, stash->info_ptr); |
| 1973 | /* A 0xffffff length is the DWARF3 way of indicating we use |
| 1974 | 64-bit offsets, instead of 32-bit offsets. */ |
| 1975 | if (length == 0xffffffff) |
| 1976 | { |
| 1977 | offset_size = 8; |
| 1978 | length = read_8_bytes (abfd, stash->info_ptr + 4); |
| 1979 | stash->info_ptr += 12; |
| 1980 | } |
| 1981 | /* A zero length is the IRIX way of indicating 64-bit offsets, |
| 1982 | mostly because the 64-bit length will generally fit in 32 |
| 1983 | bits, and the endianness helps. */ |
| 1984 | else if (length == 0) |
| 1985 | { |
| 1986 | offset_size = 8; |
| 1987 | length = read_4_bytes (abfd, stash->info_ptr + 4); |
| 1988 | stash->info_ptr += 8; |
| 1989 | } |
| 1990 | /* In the absence of the hints above, we assume addr_size-sized |
| 1991 | offsets, for backward-compatibility with pre-DWARF3 64-bit |
| 1992 | platforms. */ |
| 1993 | else if (addr_size == 8) |
| 1994 | { |
| 1995 | length = read_8_bytes (abfd, stash->info_ptr); |
| 1996 | stash->info_ptr += 8; |
| 1997 | } |
| 1998 | else |
| 1999 | stash->info_ptr += 4; |
| 2000 | |
| 2001 | if (length > 0) |
| 2002 | { |
| 2003 | each = parse_comp_unit (abfd, stash, length, info_ptr_unit, |
| 2004 | offset_size); |
| 2005 | stash->info_ptr += length; |
| 2006 | |
| 2007 | if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr) |
| 2008 | == stash->sec->size) |
| 2009 | { |
| 2010 | stash->sec = find_debug_info (abfd, stash->sec); |
| 2011 | stash->sec_info_ptr = stash->info_ptr; |
| 2012 | } |
| 2013 | |
| 2014 | if (each) |
| 2015 | { |
| 2016 | each->next_unit = stash->all_comp_units; |
| 2017 | stash->all_comp_units = each; |
| 2018 | |
| 2019 | /* DW_AT_low_pc and DW_AT_high_pc are optional for |
| 2020 | compilation units. If we don't have them (i.e., |
| 2021 | unit->high == 0), we need to consult the line info |
| 2022 | table to see if a compilation unit contains the given |
| 2023 | address. */ |
| 2024 | if (each->arange.high > 0) |
| 2025 | { |
| 2026 | if (comp_unit_contains_address (each, addr)) |
| 2027 | return comp_unit_find_nearest_line (each, addr, |
| 2028 | filename_ptr, |
| 2029 | functionname_ptr, |
| 2030 | linenumber_ptr, |
| 2031 | stash); |
| 2032 | } |
| 2033 | else |
| 2034 | { |
| 2035 | found = comp_unit_find_nearest_line (each, addr, |
| 2036 | filename_ptr, |
| 2037 | functionname_ptr, |
| 2038 | linenumber_ptr, |
| 2039 | stash); |
| 2040 | if (found) |
| 2041 | return TRUE; |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | return FALSE; |
| 2048 | } |
| 2049 | |
| 2050 | void |
| 2051 | _bfd_dwarf2_cleanup_debug_info (bfd *abfd) |
| 2052 | { |
| 2053 | struct comp_unit *each; |
| 2054 | struct dwarf2_debug *stash; |
| 2055 | |
| 2056 | if (abfd == NULL || elf_tdata (abfd) == NULL) |
| 2057 | return; |
| 2058 | |
| 2059 | stash = elf_tdata (abfd)->dwarf2_find_line_info; |
| 2060 | |
| 2061 | if (stash == NULL) |
| 2062 | return; |
| 2063 | |
| 2064 | for (each = stash->all_comp_units; each; each = each->next_unit) |
| 2065 | { |
| 2066 | struct abbrev_info **abbrevs = each->abbrevs; |
| 2067 | size_t i; |
| 2068 | |
| 2069 | for (i = 0; i < ABBREV_HASH_SIZE; i++) |
| 2070 | { |
| 2071 | struct abbrev_info *abbrev = abbrevs[i]; |
| 2072 | |
| 2073 | while (abbrev) |
| 2074 | { |
| 2075 | free (abbrev->attrs); |
| 2076 | abbrev = abbrev->next; |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | if (each->line_table) |
| 2081 | { |
| 2082 | free (each->line_table->dirs); |
| 2083 | free (each->line_table->files); |
| 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | free (stash->dwarf_abbrev_buffer); |
| 2088 | free (stash->dwarf_line_buffer); |
| 2089 | free (stash->dwarf_ranges_buffer); |
| 2090 | } |