| 1 | /* DWARF 2 support. |
| 2 | Copyright 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc. |
| 3 | |
| 4 | Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions |
| 5 | (gavin@cygnus.com). |
| 6 | |
| 7 | From the dwarf2read.c header: |
| 8 | Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology, |
| 9 | Inc. with support from Florida State University (under contract |
| 10 | with the Ada Joint Program Office), and Silicon Graphics, Inc. |
| 11 | Initial contribution by Brent Benson, Harris Computer Systems, Inc., |
| 12 | based on Fred Fish's (Cygnus Support) implementation of DWARF 1 |
| 13 | support in dwarfread.c |
| 14 | |
| 15 | This file is part of BFD. |
| 16 | |
| 17 | This program is free software; you can redistribute it and/or modify |
| 18 | it under the terms of the GNU General Public License as published by |
| 19 | the Free Software Foundation; either version 2 of the License, or (at |
| 20 | your option) any later version. |
| 21 | |
| 22 | This program is distributed in the hope that it will be useful, but |
| 23 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 | General Public License for more details. |
| 26 | |
| 27 | You should have received a copy of the GNU General Public License |
| 28 | along with this program; if not, write to the Free Software |
| 29 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 30 | |
| 31 | #include "bfd.h" |
| 32 | #include "sysdep.h" |
| 33 | #include "libiberty.h" |
| 34 | #include "libbfd.h" |
| 35 | #include "elf-bfd.h" |
| 36 | #include "elf/dwarf2.h" |
| 37 | |
| 38 | /* The data in the .debug_line statement prologue looks like this. */ |
| 39 | struct line_head |
| 40 | { |
| 41 | unsigned int total_length; |
| 42 | unsigned short version; |
| 43 | unsigned int prologue_length; |
| 44 | unsigned char minimum_instruction_length; |
| 45 | unsigned char default_is_stmt; |
| 46 | int line_base; |
| 47 | unsigned char line_range; |
| 48 | unsigned char opcode_base; |
| 49 | unsigned char *standard_opcode_lengths; |
| 50 | }; |
| 51 | |
| 52 | /* Attributes have a name and a value */ |
| 53 | struct attribute |
| 54 | { |
| 55 | enum dwarf_attribute name; |
| 56 | enum dwarf_form form; |
| 57 | union |
| 58 | { |
| 59 | char *str; |
| 60 | struct dwarf_block *blk; |
| 61 | unsigned int unsnd; |
| 62 | int snd; |
| 63 | bfd_vma addr; |
| 64 | } |
| 65 | u; |
| 66 | }; |
| 67 | |
| 68 | /* Get at parts of an attribute structure */ |
| 69 | |
| 70 | #define DW_STRING(attr) ((attr)->u.str) |
| 71 | #define DW_UNSND(attr) ((attr)->u.unsnd) |
| 72 | #define DW_BLOCK(attr) ((attr)->u.blk) |
| 73 | #define DW_SND(attr) ((attr)->u.snd) |
| 74 | #define DW_ADDR(attr) ((attr)->u.addr) |
| 75 | |
| 76 | /* Blocks are a bunch of untyped bytes. */ |
| 77 | struct dwarf_block |
| 78 | { |
| 79 | unsigned int size; |
| 80 | char *data; |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | struct dwarf2_debug { |
| 85 | |
| 86 | /* A list of all previously read comp_units. */ |
| 87 | struct comp_unit* all_comp_units; |
| 88 | |
| 89 | /* The next unread compilation unit within the .debug_info section. |
| 90 | Zero indicates that the .debug_info section has not been loaded |
| 91 | into a buffer yet.*/ |
| 92 | char* info_ptr; |
| 93 | |
| 94 | /* Pointer to the end of the .debug_info section memory buffer. */ |
| 95 | char* info_ptr_end; |
| 96 | |
| 97 | /* Pointer to the .debug_abbrev section loaded into memory. */ |
| 98 | char* dwarf_abbrev_buffer; |
| 99 | |
| 100 | /* Length of the loaded .debug_abbrev section. */ |
| 101 | unsigned long dwarf_abbrev_size; |
| 102 | }; |
| 103 | |
| 104 | |
| 105 | |
| 106 | /* A minimal decoding of DWARF2 compilation units. We only decode |
| 107 | what's needed to get to the line number information. */ |
| 108 | |
| 109 | struct comp_unit { |
| 110 | |
| 111 | /* Chain the previously read compilation units. */ |
| 112 | struct comp_unit* next_unit; |
| 113 | |
| 114 | /* Keep the bdf convenient (for memory allocation). */ |
| 115 | bfd* abfd; |
| 116 | |
| 117 | /* The lowest and higest addresses contained in this compilation |
| 118 | unit as specified in the compilation unit header. */ |
| 119 | bfd_vma low; |
| 120 | bfd_vma high; |
| 121 | |
| 122 | /* The DW_AT_name attribute (for error messages). */ |
| 123 | char* name; |
| 124 | |
| 125 | /* The abbrev hash table. */ |
| 126 | struct abbrev_info** abbrevs; |
| 127 | |
| 128 | /* Note that an error was found by comp_unit_find_nearest_line. */ |
| 129 | int error; |
| 130 | |
| 131 | /* The DW_AT_comp_dir attribute */ |
| 132 | char* comp_dir; |
| 133 | |
| 134 | /* True if there is a line number table associated with this comp. unit. */ |
| 135 | int stmtlist; |
| 136 | |
| 137 | /* The offset into .debug_line of the line number table. */ |
| 138 | unsigned long line_offset; |
| 139 | |
| 140 | /* Pointer to the first child die for the comp unit. */ |
| 141 | char *first_child_die_ptr; |
| 142 | |
| 143 | /* The end of the comp unit. */ |
| 144 | char *end_ptr; |
| 145 | |
| 146 | /* The decoded line number, NULL if not yet decoded. */ |
| 147 | struct line_info_table* line_table; |
| 148 | |
| 149 | /* A list of the functions found in this comp. unit. */ |
| 150 | struct funcinfo* function_table; |
| 151 | |
| 152 | /* Address size for this unit - from unit header */ |
| 153 | unsigned char addr_size; |
| 154 | }; |
| 155 | |
| 156 | |
| 157 | |
| 158 | /* VERBATUM |
| 159 | The following function up to the END VERBATUM mark are |
| 160 | copied directly from dwarf2read.c. */ |
| 161 | |
| 162 | /* read dwarf information from a buffer */ |
| 163 | |
| 164 | static unsigned int |
| 165 | read_1_byte (abfd, buf) |
| 166 | bfd *abfd; |
| 167 | char *buf; |
| 168 | { |
| 169 | return bfd_get_8 (abfd, (bfd_byte *) buf); |
| 170 | } |
| 171 | |
| 172 | static int |
| 173 | read_1_signed_byte (abfd, buf) |
| 174 | bfd *abfd; |
| 175 | char *buf; |
| 176 | { |
| 177 | return bfd_get_signed_8 (abfd, (bfd_byte *) buf); |
| 178 | } |
| 179 | |
| 180 | static unsigned int |
| 181 | read_2_bytes (abfd, buf) |
| 182 | bfd *abfd; |
| 183 | char *buf; |
| 184 | { |
| 185 | return bfd_get_16 (abfd, (bfd_byte *) buf); |
| 186 | } |
| 187 | |
| 188 | #if 0 |
| 189 | |
| 190 | /* This is not used. */ |
| 191 | |
| 192 | static int |
| 193 | read_2_signed_bytes (abfd, buf) |
| 194 | bfd *abfd; |
| 195 | char *buf; |
| 196 | { |
| 197 | return bfd_get_signed_16 (abfd, (bfd_byte *) buf); |
| 198 | } |
| 199 | |
| 200 | #endif |
| 201 | |
| 202 | static unsigned int |
| 203 | read_4_bytes (abfd, buf) |
| 204 | bfd *abfd; |
| 205 | char *buf; |
| 206 | { |
| 207 | return bfd_get_32 (abfd, (bfd_byte *) buf); |
| 208 | } |
| 209 | |
| 210 | #if 0 |
| 211 | |
| 212 | /* This is not used. */ |
| 213 | |
| 214 | static int |
| 215 | read_4_signed_bytes (abfd, buf) |
| 216 | bfd *abfd; |
| 217 | char *buf; |
| 218 | { |
| 219 | return bfd_get_signed_32 (abfd, (bfd_byte *) buf); |
| 220 | } |
| 221 | |
| 222 | #endif |
| 223 | |
| 224 | static unsigned int |
| 225 | read_8_bytes (abfd, buf) |
| 226 | bfd *abfd; |
| 227 | char *buf; |
| 228 | { |
| 229 | return bfd_get_64 (abfd, (bfd_byte *) buf); |
| 230 | } |
| 231 | |
| 232 | static char * |
| 233 | read_n_bytes (abfd, buf, size) |
| 234 | bfd * abfd; |
| 235 | char *buf; |
| 236 | unsigned int size; |
| 237 | { |
| 238 | /* If the size of a host char is 8 bits, we can return a pointer |
| 239 | to the buffer, otherwise we have to copy the data to a buffer |
| 240 | allocated on the temporary obstack. */ |
| 241 | return buf; |
| 242 | } |
| 243 | |
| 244 | static char * |
| 245 | read_string (abfd, buf, bytes_read_ptr) |
| 246 | bfd *abfd; |
| 247 | char *buf; |
| 248 | unsigned int *bytes_read_ptr; |
| 249 | { |
| 250 | /* If the size of a host char is 8 bits, we can return a pointer |
| 251 | to the string, otherwise we have to copy the string to a buffer |
| 252 | allocated on the temporary obstack. */ |
| 253 | if (*buf == '\0') |
| 254 | { |
| 255 | *bytes_read_ptr = 1; |
| 256 | return NULL; |
| 257 | } |
| 258 | *bytes_read_ptr = strlen (buf) + 1; |
| 259 | return buf; |
| 260 | } |
| 261 | |
| 262 | static unsigned int |
| 263 | read_unsigned_leb128 (abfd, buf, bytes_read_ptr) |
| 264 | bfd *abfd; |
| 265 | char *buf; |
| 266 | unsigned int *bytes_read_ptr; |
| 267 | { |
| 268 | unsigned int result, num_read; |
| 269 | int i, shift; |
| 270 | unsigned char byte; |
| 271 | |
| 272 | result = 0; |
| 273 | shift = 0; |
| 274 | num_read = 0; |
| 275 | i = 0; |
| 276 | while (1) |
| 277 | { |
| 278 | byte = bfd_get_8 (abfd, (bfd_byte *) buf); |
| 279 | buf++; |
| 280 | num_read++; |
| 281 | result |= ((byte & 127) << shift); |
| 282 | if ((byte & 128) == 0) |
| 283 | { |
| 284 | break; |
| 285 | } |
| 286 | shift += 7; |
| 287 | } |
| 288 | *bytes_read_ptr = num_read; |
| 289 | return result; |
| 290 | } |
| 291 | |
| 292 | static int |
| 293 | read_signed_leb128 (abfd, buf, bytes_read_ptr) |
| 294 | bfd *abfd; |
| 295 | char *buf; |
| 296 | unsigned int *bytes_read_ptr; |
| 297 | { |
| 298 | int result; |
| 299 | int i, shift, size, num_read; |
| 300 | unsigned char byte; |
| 301 | |
| 302 | result = 0; |
| 303 | shift = 0; |
| 304 | size = 32; |
| 305 | num_read = 0; |
| 306 | i = 0; |
| 307 | while (1) |
| 308 | { |
| 309 | byte = bfd_get_8 (abfd, (bfd_byte *) buf); |
| 310 | buf++; |
| 311 | num_read++; |
| 312 | result |= ((byte & 127) << shift); |
| 313 | shift += 7; |
| 314 | if ((byte & 128) == 0) |
| 315 | { |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | if ((shift < size) && (byte & 0x40)) |
| 320 | { |
| 321 | result |= -(1 << shift); |
| 322 | } |
| 323 | *bytes_read_ptr = num_read; |
| 324 | return result; |
| 325 | } |
| 326 | |
| 327 | /* END VERBATUM */ |
| 328 | |
| 329 | static bfd_vma |
| 330 | read_address (unit, buf) |
| 331 | struct comp_unit* unit; |
| 332 | char *buf; |
| 333 | { |
| 334 | bfd_vma retval = 0; |
| 335 | |
| 336 | if (unit->addr_size == 4) |
| 337 | { |
| 338 | retval = bfd_get_32 (unit->abfd, (bfd_byte *) buf); |
| 339 | } else { |
| 340 | retval = bfd_get_64 (unit->abfd, (bfd_byte *) buf); |
| 341 | } |
| 342 | return retval; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | /* This data structure holds the information of an abbrev. */ |
| 350 | struct abbrev_info |
| 351 | { |
| 352 | unsigned int number; /* number identifying abbrev */ |
| 353 | enum dwarf_tag tag; /* dwarf tag */ |
| 354 | int has_children; /* boolean */ |
| 355 | unsigned int num_attrs; /* number of attributes */ |
| 356 | struct attr_abbrev *attrs; /* an array of attribute descriptions */ |
| 357 | struct abbrev_info *next; /* next in chain */ |
| 358 | }; |
| 359 | |
| 360 | struct attr_abbrev |
| 361 | { |
| 362 | enum dwarf_attribute name; |
| 363 | enum dwarf_form form; |
| 364 | }; |
| 365 | |
| 366 | #ifndef ABBREV_HASH_SIZE |
| 367 | #define ABBREV_HASH_SIZE 121 |
| 368 | #endif |
| 369 | #ifndef ATTR_ALLOC_CHUNK |
| 370 | #define ATTR_ALLOC_CHUNK 4 |
| 371 | #endif |
| 372 | |
| 373 | /* Lookup an abbrev_info structure in the abbrev hash table. */ |
| 374 | |
| 375 | static struct abbrev_info * |
| 376 | lookup_abbrev (number,abbrevs) |
| 377 | unsigned int number; |
| 378 | struct abbrev_info **abbrevs; |
| 379 | { |
| 380 | unsigned int hash_number; |
| 381 | struct abbrev_info *abbrev; |
| 382 | |
| 383 | hash_number = number % ABBREV_HASH_SIZE; |
| 384 | abbrev = abbrevs[hash_number]; |
| 385 | |
| 386 | while (abbrev) |
| 387 | { |
| 388 | if (abbrev->number == number) |
| 389 | return abbrev; |
| 390 | else |
| 391 | abbrev = abbrev->next; |
| 392 | } |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | /* In DWARF version 2, the description of the debugging information is |
| 397 | stored in a separate .debug_abbrev section. Before we read any |
| 398 | dies from a section we read in all abbreviations and install them |
| 399 | in a hash table. */ |
| 400 | |
| 401 | static struct abbrev_info** |
| 402 | read_abbrevs (abfd, offset) |
| 403 | bfd * abfd; |
| 404 | unsigned int offset; |
| 405 | { |
| 406 | struct abbrev_info **abbrevs; |
| 407 | char *abbrev_ptr; |
| 408 | struct abbrev_info *cur_abbrev; |
| 409 | unsigned int abbrev_number, bytes_read, abbrev_name; |
| 410 | unsigned int abbrev_form, hash_number; |
| 411 | struct dwarf2_debug *stash; |
| 412 | |
| 413 | stash = elf_tdata(abfd)->dwarf2_find_line_info; |
| 414 | |
| 415 | if (! stash->dwarf_abbrev_buffer) |
| 416 | { |
| 417 | asection *msec; |
| 418 | |
| 419 | msec = bfd_get_section_by_name (abfd, ".debug_abbrev"); |
| 420 | if (! msec) |
| 421 | { |
| 422 | (*_bfd_error_handler) ("Dwarf Error: Can't find .debug_abbrev section."); |
| 423 | bfd_set_error (bfd_error_bad_value); |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | stash->dwarf_abbrev_size = bfd_get_section_size_before_reloc (msec); |
| 428 | stash->dwarf_abbrev_buffer = (unsigned char*) bfd_alloc (abfd, stash->dwarf_abbrev_size); |
| 429 | if (! stash->dwarf_abbrev_buffer) |
| 430 | return 0; |
| 431 | |
| 432 | if (! bfd_get_section_contents (abfd, msec, |
| 433 | stash->dwarf_abbrev_buffer, 0, |
| 434 | stash->dwarf_abbrev_size)) |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | if (offset > stash->dwarf_abbrev_size) |
| 439 | { |
| 440 | (*_bfd_error_handler) ("Dwarf Error: Abbrev offset (%u) bigger than abbrev size (%u).", |
| 441 | offset, stash->dwarf_abbrev_size ); |
| 442 | bfd_set_error (bfd_error_bad_value); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, sizeof(struct abbrev_info*) * ABBREV_HASH_SIZE); |
| 447 | |
| 448 | abbrev_ptr = stash->dwarf_abbrev_buffer + offset; |
| 449 | abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 450 | abbrev_ptr += bytes_read; |
| 451 | |
| 452 | /* loop until we reach an abbrev number of 0 */ |
| 453 | while (abbrev_number) |
| 454 | { |
| 455 | cur_abbrev = (struct abbrev_info*)bfd_zalloc (abfd, sizeof (struct abbrev_info)); |
| 456 | |
| 457 | /* read in abbrev header */ |
| 458 | cur_abbrev->number = abbrev_number; |
| 459 | cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 460 | abbrev_ptr += bytes_read; |
| 461 | cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr); |
| 462 | abbrev_ptr += 1; |
| 463 | |
| 464 | /* now read in declarations */ |
| 465 | abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 466 | abbrev_ptr += bytes_read; |
| 467 | abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 468 | abbrev_ptr += bytes_read; |
| 469 | while (abbrev_name) |
| 470 | { |
| 471 | if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0) |
| 472 | { |
| 473 | cur_abbrev->attrs = (struct attr_abbrev *) |
| 474 | bfd_realloc (cur_abbrev->attrs, |
| 475 | (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK) |
| 476 | * sizeof (struct attr_abbrev)); |
| 477 | if (! cur_abbrev->attrs) |
| 478 | return 0; |
| 479 | } |
| 480 | cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name; |
| 481 | cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form; |
| 482 | abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 483 | abbrev_ptr += bytes_read; |
| 484 | abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 485 | abbrev_ptr += bytes_read; |
| 486 | } |
| 487 | |
| 488 | hash_number = abbrev_number % ABBREV_HASH_SIZE; |
| 489 | cur_abbrev->next = abbrevs[hash_number]; |
| 490 | abbrevs[hash_number] = cur_abbrev; |
| 491 | |
| 492 | /* Get next abbreviation. |
| 493 | Under Irix6 the abbreviations for a compilation unit are not |
| 494 | always properly terminated with an abbrev number of 0. |
| 495 | Exit loop if we encounter an abbreviation which we have |
| 496 | already read (which means we are about to read the abbreviations |
| 497 | for the next compile unit) or if the end of the abbreviation |
| 498 | table is reached. */ |
| 499 | if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer) |
| 500 | >= stash->dwarf_abbrev_size) |
| 501 | break; |
| 502 | abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
| 503 | abbrev_ptr += bytes_read; |
| 504 | if (lookup_abbrev (abbrev_number,abbrevs) != NULL) |
| 505 | break; |
| 506 | } |
| 507 | |
| 508 | return abbrevs; |
| 509 | } |
| 510 | |
| 511 | /* Read an attribute described by an abbreviated attribute. */ |
| 512 | |
| 513 | static char * |
| 514 | read_attribute (attr, abbrev, unit, info_ptr) |
| 515 | struct attribute *attr; |
| 516 | struct attr_abbrev *abbrev; |
| 517 | struct comp_unit *unit; |
| 518 | char *info_ptr; |
| 519 | { |
| 520 | bfd *abfd = unit->abfd; |
| 521 | unsigned int bytes_read; |
| 522 | struct dwarf_block *blk; |
| 523 | |
| 524 | attr->name = abbrev->name; |
| 525 | attr->form = abbrev->form; |
| 526 | switch (abbrev->form) |
| 527 | { |
| 528 | case DW_FORM_addr: |
| 529 | case DW_FORM_ref_addr: |
| 530 | DW_ADDR (attr) = read_address (unit, info_ptr); |
| 531 | info_ptr += unit->addr_size; |
| 532 | break; |
| 533 | case DW_FORM_block2: |
| 534 | blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block)); |
| 535 | blk->size = read_2_bytes (abfd, info_ptr); |
| 536 | info_ptr += 2; |
| 537 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); |
| 538 | info_ptr += blk->size; |
| 539 | DW_BLOCK (attr) = blk; |
| 540 | break; |
| 541 | case DW_FORM_block4: |
| 542 | blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block)); |
| 543 | blk->size = read_4_bytes (abfd, info_ptr); |
| 544 | info_ptr += 4; |
| 545 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); |
| 546 | info_ptr += blk->size; |
| 547 | DW_BLOCK (attr) = blk; |
| 548 | break; |
| 549 | case DW_FORM_data2: |
| 550 | DW_UNSND (attr) = read_2_bytes (abfd, info_ptr); |
| 551 | info_ptr += 2; |
| 552 | break; |
| 553 | case DW_FORM_data4: |
| 554 | DW_UNSND (attr) = read_4_bytes (abfd, info_ptr); |
| 555 | info_ptr += 4; |
| 556 | break; |
| 557 | case DW_FORM_data8: |
| 558 | DW_UNSND (attr) = read_8_bytes (abfd, info_ptr); |
| 559 | info_ptr += 8; |
| 560 | break; |
| 561 | case DW_FORM_string: |
| 562 | DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read); |
| 563 | info_ptr += bytes_read; |
| 564 | break; |
| 565 | case DW_FORM_block: |
| 566 | blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block)); |
| 567 | blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 568 | info_ptr += bytes_read; |
| 569 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); |
| 570 | info_ptr += blk->size; |
| 571 | DW_BLOCK (attr) = blk; |
| 572 | break; |
| 573 | case DW_FORM_block1: |
| 574 | blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block)); |
| 575 | blk->size = read_1_byte (abfd, info_ptr); |
| 576 | info_ptr += 1; |
| 577 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); |
| 578 | info_ptr += blk->size; |
| 579 | DW_BLOCK (attr) = blk; |
| 580 | break; |
| 581 | case DW_FORM_data1: |
| 582 | DW_UNSND (attr) = read_1_byte (abfd, info_ptr); |
| 583 | info_ptr += 1; |
| 584 | break; |
| 585 | case DW_FORM_flag: |
| 586 | DW_UNSND (attr) = read_1_byte (abfd, info_ptr); |
| 587 | info_ptr += 1; |
| 588 | break; |
| 589 | case DW_FORM_sdata: |
| 590 | DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read); |
| 591 | info_ptr += bytes_read; |
| 592 | break; |
| 593 | case DW_FORM_udata: |
| 594 | DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 595 | info_ptr += bytes_read; |
| 596 | break; |
| 597 | case DW_FORM_ref1: |
| 598 | DW_UNSND (attr) = read_1_byte (abfd, info_ptr); |
| 599 | info_ptr += 1; |
| 600 | break; |
| 601 | case DW_FORM_ref2: |
| 602 | DW_UNSND (attr) = read_2_bytes (abfd, info_ptr); |
| 603 | info_ptr += 2; |
| 604 | break; |
| 605 | case DW_FORM_ref4: |
| 606 | DW_UNSND (attr) = read_4_bytes (abfd, info_ptr); |
| 607 | info_ptr += 4; |
| 608 | break; |
| 609 | case DW_FORM_ref_udata: |
| 610 | DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 611 | info_ptr += bytes_read; |
| 612 | break; |
| 613 | case DW_FORM_strp: |
| 614 | case DW_FORM_indirect: |
| 615 | default: |
| 616 | (*_bfd_error_handler) ("Dwarf Error: Invalid or unhandled FORM value: %d.", |
| 617 | abbrev->form); |
| 618 | bfd_set_error (bfd_error_bad_value); |
| 619 | } |
| 620 | return info_ptr; |
| 621 | } |
| 622 | |
| 623 | |
| 624 | /* Source line information table routines. */ |
| 625 | |
| 626 | #define FILE_ALLOC_CHUNK 5 |
| 627 | #define DIR_ALLOC_CHUNK 5 |
| 628 | |
| 629 | struct line_info { |
| 630 | struct line_info* prev_line; |
| 631 | |
| 632 | bfd_vma address; |
| 633 | char* filename; |
| 634 | unsigned int line; |
| 635 | unsigned int column; |
| 636 | }; |
| 637 | |
| 638 | struct fileinfo { |
| 639 | char *name; |
| 640 | unsigned int dir; |
| 641 | unsigned int time; |
| 642 | unsigned int size; |
| 643 | }; |
| 644 | |
| 645 | struct line_info_table { |
| 646 | bfd* abfd; |
| 647 | |
| 648 | unsigned int num_files; |
| 649 | unsigned int num_dirs; |
| 650 | |
| 651 | char* comp_dir; |
| 652 | char** dirs; |
| 653 | struct fileinfo* files; |
| 654 | struct line_info* last_line; |
| 655 | }; |
| 656 | |
| 657 | static void |
| 658 | add_line_info (table, address, filename, line, column) |
| 659 | struct line_info_table* table; |
| 660 | bfd_vma address; |
| 661 | char* filename; |
| 662 | unsigned int line; |
| 663 | unsigned int column; |
| 664 | { |
| 665 | struct line_info* info = (struct line_info*) |
| 666 | bfd_alloc (table->abfd, sizeof (struct line_info)); |
| 667 | |
| 668 | info->prev_line = table->last_line; |
| 669 | table->last_line = info; |
| 670 | |
| 671 | info->address = address; |
| 672 | info->filename = filename; |
| 673 | info->line = line; |
| 674 | info->column = column; |
| 675 | } |
| 676 | |
| 677 | static char* |
| 678 | concat_filename (table, file) |
| 679 | struct line_info_table* table; |
| 680 | unsigned int file; |
| 681 | { |
| 682 | char* filename = table->files[file - 1].name; |
| 683 | if (*filename == '/') |
| 684 | return filename; |
| 685 | |
| 686 | else |
| 687 | { |
| 688 | char* dirname = (table->files[file - 1].dir |
| 689 | ? table->dirs[table->files[file - 1].dir - 1] |
| 690 | : table->comp_dir); |
| 691 | return (char*) concat (dirname, "/", filename, NULL); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /* Decode the line number information for UNIT. */ |
| 696 | |
| 697 | static struct line_info_table* |
| 698 | decode_line_info (unit) |
| 699 | struct comp_unit *unit; |
| 700 | { |
| 701 | bfd *abfd = unit->abfd; |
| 702 | |
| 703 | static char* dwarf_line_buffer = 0; |
| 704 | |
| 705 | struct line_info_table* table; |
| 706 | |
| 707 | char *line_ptr; |
| 708 | char *line_end; |
| 709 | struct line_head lh; |
| 710 | unsigned int i, bytes_read; |
| 711 | char *cur_file, *cur_dir; |
| 712 | unsigned char op_code, extended_op, adj_opcode; |
| 713 | |
| 714 | if (! dwarf_line_buffer) |
| 715 | { |
| 716 | asection *msec; |
| 717 | unsigned long size; |
| 718 | |
| 719 | msec = bfd_get_section_by_name (abfd, ".debug_line"); |
| 720 | if (! msec) |
| 721 | { |
| 722 | (*_bfd_error_handler) ("Dwarf Error: Can't find .debug_line section."); |
| 723 | bfd_set_error (bfd_error_bad_value); |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | size = bfd_get_section_size_before_reloc (msec); |
| 728 | dwarf_line_buffer = (unsigned char*) bfd_alloc (abfd, size); |
| 729 | if (! dwarf_line_buffer) |
| 730 | return 0; |
| 731 | |
| 732 | if (! bfd_get_section_contents (abfd, msec, |
| 733 | dwarf_line_buffer, 0, |
| 734 | size)) |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | table = (struct line_info_table*) bfd_alloc (abfd, |
| 739 | sizeof (struct line_info_table)); |
| 740 | table->abfd = abfd; |
| 741 | table->comp_dir = unit->comp_dir; |
| 742 | |
| 743 | table->num_files = 0; |
| 744 | table->files = NULL; |
| 745 | |
| 746 | table->num_dirs = 0; |
| 747 | table->dirs = NULL; |
| 748 | |
| 749 | line_ptr = dwarf_line_buffer + unit->line_offset; |
| 750 | |
| 751 | /* read in the prologue */ |
| 752 | lh.total_length = read_4_bytes (abfd, line_ptr); |
| 753 | line_ptr += 4; |
| 754 | line_end = line_ptr + lh.total_length; |
| 755 | lh.version = read_2_bytes (abfd, line_ptr); |
| 756 | line_ptr += 2; |
| 757 | lh.prologue_length = read_4_bytes (abfd, line_ptr); |
| 758 | line_ptr += 4; |
| 759 | lh.minimum_instruction_length = read_1_byte (abfd, line_ptr); |
| 760 | line_ptr += 1; |
| 761 | lh.default_is_stmt = read_1_byte (abfd, line_ptr); |
| 762 | line_ptr += 1; |
| 763 | lh.line_base = read_1_signed_byte (abfd, line_ptr); |
| 764 | line_ptr += 1; |
| 765 | lh.line_range = read_1_byte (abfd, line_ptr); |
| 766 | line_ptr += 1; |
| 767 | lh.opcode_base = read_1_byte (abfd, line_ptr); |
| 768 | line_ptr += 1; |
| 769 | lh.standard_opcode_lengths = (unsigned char *) |
| 770 | bfd_alloc (abfd, lh.opcode_base * sizeof (unsigned char)); |
| 771 | |
| 772 | lh.standard_opcode_lengths[0] = 1; |
| 773 | for (i = 1; i < lh.opcode_base; ++i) |
| 774 | { |
| 775 | lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr); |
| 776 | line_ptr += 1; |
| 777 | } |
| 778 | |
| 779 | /* Read directory table */ |
| 780 | while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL) |
| 781 | { |
| 782 | line_ptr += bytes_read; |
| 783 | if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0) |
| 784 | { |
| 785 | table->dirs = (char **) |
| 786 | bfd_realloc (table->dirs, |
| 787 | (table->num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *)); |
| 788 | if (! table->dirs) |
| 789 | return 0; |
| 790 | } |
| 791 | table->dirs[table->num_dirs++] = cur_dir; |
| 792 | } |
| 793 | line_ptr += bytes_read; |
| 794 | |
| 795 | /* Read file name table */ |
| 796 | while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL) |
| 797 | { |
| 798 | line_ptr += bytes_read; |
| 799 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) |
| 800 | { |
| 801 | table->files = (struct fileinfo *) |
| 802 | bfd_realloc (table->files, |
| 803 | (table->num_files + FILE_ALLOC_CHUNK) |
| 804 | * sizeof (struct fileinfo)); |
| 805 | if (! table->files) |
| 806 | return 0; |
| 807 | } |
| 808 | table->files[table->num_files].name = cur_file; |
| 809 | table->files[table->num_files].dir = |
| 810 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 811 | line_ptr += bytes_read; |
| 812 | table->files[table->num_files].time = |
| 813 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 814 | line_ptr += bytes_read; |
| 815 | table->files[table->num_files].size = |
| 816 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 817 | line_ptr += bytes_read; |
| 818 | table->num_files++; |
| 819 | } |
| 820 | line_ptr += bytes_read; |
| 821 | |
| 822 | /* Read the statement sequences until there's nothing left. */ |
| 823 | while (line_ptr < line_end) |
| 824 | { |
| 825 | /* state machine registers */ |
| 826 | bfd_vma address = 0; |
| 827 | char* filename = concat_filename (table, 1); |
| 828 | unsigned int line = 1; |
| 829 | unsigned int column = 0; |
| 830 | int is_stmt = lh.default_is_stmt; |
| 831 | int basic_block = 0; |
| 832 | int end_sequence = 0; |
| 833 | |
| 834 | /* Decode the table. */ |
| 835 | while (! end_sequence) |
| 836 | { |
| 837 | op_code = read_1_byte (abfd, line_ptr); |
| 838 | line_ptr += 1; |
| 839 | switch (op_code) |
| 840 | { |
| 841 | case DW_LNS_extended_op: |
| 842 | line_ptr += 1; /* ignore length */ |
| 843 | extended_op = read_1_byte (abfd, line_ptr); |
| 844 | line_ptr += 1; |
| 845 | switch (extended_op) |
| 846 | { |
| 847 | case DW_LNE_end_sequence: |
| 848 | end_sequence = 1; |
| 849 | add_line_info (table, address, filename, line, column); |
| 850 | break; |
| 851 | case DW_LNE_set_address: |
| 852 | address = read_address (unit, line_ptr); |
| 853 | address &= 0xffffffff; |
| 854 | line_ptr += unit->addr_size; |
| 855 | break; |
| 856 | case DW_LNE_define_file: |
| 857 | cur_file = read_string (abfd, line_ptr, &bytes_read); |
| 858 | line_ptr += bytes_read; |
| 859 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) |
| 860 | { |
| 861 | table->files = (struct fileinfo *) |
| 862 | bfd_realloc (table->files, |
| 863 | (table->num_files + FILE_ALLOC_CHUNK) |
| 864 | * sizeof (struct fileinfo)); |
| 865 | if (! table->files) |
| 866 | return 0; |
| 867 | } |
| 868 | table->files[table->num_files].name = cur_file; |
| 869 | table->files[table->num_files].dir = |
| 870 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 871 | line_ptr += bytes_read; |
| 872 | table->files[table->num_files].time = |
| 873 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 874 | line_ptr += bytes_read; |
| 875 | table->files[table->num_files].size = |
| 876 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 877 | line_ptr += bytes_read; |
| 878 | table->num_files++; |
| 879 | break; |
| 880 | default: |
| 881 | (*_bfd_error_handler) ("Dwarf Error: mangled line number section."); |
| 882 | bfd_set_error (bfd_error_bad_value); |
| 883 | return 0; |
| 884 | } |
| 885 | break; |
| 886 | case DW_LNS_copy: |
| 887 | add_line_info (table, address, filename, line, column); |
| 888 | basic_block = 0; |
| 889 | break; |
| 890 | case DW_LNS_advance_pc: |
| 891 | address += lh.minimum_instruction_length |
| 892 | * read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 893 | line_ptr += bytes_read; |
| 894 | break; |
| 895 | case DW_LNS_advance_line: |
| 896 | line += read_signed_leb128 (abfd, line_ptr, &bytes_read); |
| 897 | line_ptr += bytes_read; |
| 898 | break; |
| 899 | case DW_LNS_set_file: |
| 900 | { |
| 901 | unsigned int file; |
| 902 | |
| 903 | /* The file and directory tables are 0 based, the references |
| 904 | are 1 based. */ |
| 905 | file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 906 | line_ptr += bytes_read; |
| 907 | filename = concat_filename (table, file); |
| 908 | break; |
| 909 | } |
| 910 | case DW_LNS_set_column: |
| 911 | column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
| 912 | line_ptr += bytes_read; |
| 913 | break; |
| 914 | case DW_LNS_negate_stmt: |
| 915 | is_stmt = (!is_stmt); |
| 916 | break; |
| 917 | case DW_LNS_set_basic_block: |
| 918 | basic_block = 1; |
| 919 | break; |
| 920 | case DW_LNS_const_add_pc: |
| 921 | address += (255 - lh.opcode_base) / lh.line_range; |
| 922 | break; |
| 923 | case DW_LNS_fixed_advance_pc: |
| 924 | address += read_2_bytes (abfd, line_ptr); |
| 925 | line_ptr += 2; |
| 926 | break; |
| 927 | default: /* special operand */ |
| 928 | adj_opcode = op_code - lh.opcode_base; |
| 929 | address += (adj_opcode / lh.line_range) |
| 930 | * lh.minimum_instruction_length; |
| 931 | line += lh.line_base + (adj_opcode % lh.line_range); |
| 932 | /* append row to matrix using current values */ |
| 933 | add_line_info (table, address, filename, line, column); |
| 934 | basic_block = 1; |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | return table; |
| 940 | } |
| 941 | |
| 942 | |
| 943 | /* If ADDR is within TABLE set the output parameters and return true, |
| 944 | otherwise return false. The output parameters, FILENAME_PTR and |
| 945 | LINENUMBER_PTR, are pointers to the objects to be filled in. */ |
| 946 | |
| 947 | static boolean |
| 948 | lookup_address_in_line_info_table (table, |
| 949 | addr, |
| 950 | filename_ptr, |
| 951 | linenumber_ptr) |
| 952 | struct line_info_table* table; |
| 953 | bfd_vma addr; |
| 954 | const char **filename_ptr; |
| 955 | unsigned int *linenumber_ptr; |
| 956 | { |
| 957 | struct line_info* each_line; |
| 958 | struct line_info* next_line; |
| 959 | |
| 960 | for (next_line = 0, each_line = table->last_line; |
| 961 | each_line; |
| 962 | next_line = each_line, each_line = each_line->prev_line) |
| 963 | { |
| 964 | if (addr >= each_line->address |
| 965 | && (next_line == 0 |
| 966 | || addr < next_line->address)) |
| 967 | { |
| 968 | *filename_ptr = each_line->filename; |
| 969 | *linenumber_ptr = each_line->line; |
| 970 | return true; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | return false; |
| 975 | } |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | /* Function table functions. */ |
| 981 | |
| 982 | struct funcinfo { |
| 983 | struct funcinfo *prev_func; |
| 984 | |
| 985 | char* name; |
| 986 | bfd_vma low; |
| 987 | bfd_vma high; |
| 988 | }; |
| 989 | |
| 990 | |
| 991 | /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */ |
| 992 | |
| 993 | static boolean |
| 994 | lookup_address_in_function_table (table, |
| 995 | addr, |
| 996 | functionname_ptr) |
| 997 | struct funcinfo* table; |
| 998 | bfd_vma addr; |
| 999 | const char **functionname_ptr; |
| 1000 | { |
| 1001 | struct funcinfo* each_func; |
| 1002 | |
| 1003 | for (each_func = table; |
| 1004 | each_func; |
| 1005 | each_func = each_func->prev_func) |
| 1006 | { |
| 1007 | if (addr >= (each_func->low & 0xffffffff) |
| 1008 | && addr < (each_func->high & 0xffffffff)) |
| 1009 | { |
| 1010 | *functionname_ptr = each_func->name; |
| 1011 | return true; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | /* DWARF2 Compilation unit functions. */ |
| 1022 | |
| 1023 | |
| 1024 | /* Scan over each die in a comp. unit looking for functions to add |
| 1025 | to the function table. */ |
| 1026 | |
| 1027 | static boolean |
| 1028 | scan_unit_for_functions (unit) |
| 1029 | struct comp_unit *unit; |
| 1030 | { |
| 1031 | bfd *abfd = unit->abfd; |
| 1032 | char *info_ptr = unit->first_child_die_ptr; |
| 1033 | int nesting_level = 1; |
| 1034 | |
| 1035 | while (nesting_level) |
| 1036 | { |
| 1037 | unsigned int abbrev_number, bytes_read, i; |
| 1038 | struct abbrev_info *abbrev; |
| 1039 | struct attribute attr; |
| 1040 | struct funcinfo *func; |
| 1041 | char* name = 0; |
| 1042 | |
| 1043 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 1044 | info_ptr += bytes_read; |
| 1045 | |
| 1046 | if (! abbrev_number) |
| 1047 | { |
| 1048 | nesting_level--; |
| 1049 | continue; |
| 1050 | } |
| 1051 | |
| 1052 | abbrev = lookup_abbrev (abbrev_number,unit->abbrevs); |
| 1053 | if (! abbrev) |
| 1054 | { |
| 1055 | (*_bfd_error_handler) ("Dwarf Error: Could not find abbrev number %d.", |
| 1056 | abbrev_number); |
| 1057 | bfd_set_error (bfd_error_bad_value); |
| 1058 | return false; |
| 1059 | } |
| 1060 | |
| 1061 | if (abbrev->tag == DW_TAG_subprogram) |
| 1062 | { |
| 1063 | func = (struct funcinfo*) bfd_zalloc (abfd, sizeof (struct funcinfo)); |
| 1064 | func->prev_func = unit->function_table; |
| 1065 | unit->function_table = func; |
| 1066 | } |
| 1067 | else |
| 1068 | func = NULL; |
| 1069 | |
| 1070 | for (i = 0; i < abbrev->num_attrs; ++i) |
| 1071 | { |
| 1072 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); |
| 1073 | |
| 1074 | if (func) |
| 1075 | { |
| 1076 | switch (attr.name) |
| 1077 | { |
| 1078 | case DW_AT_name: |
| 1079 | |
| 1080 | name = DW_STRING (&attr); |
| 1081 | |
| 1082 | /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */ |
| 1083 | if (func->name == NULL) |
| 1084 | func->name = DW_STRING (&attr); |
| 1085 | break; |
| 1086 | |
| 1087 | case DW_AT_MIPS_linkage_name: |
| 1088 | func->name = DW_STRING (&attr); |
| 1089 | break; |
| 1090 | |
| 1091 | case DW_AT_low_pc: |
| 1092 | func->low = DW_ADDR (&attr); |
| 1093 | break; |
| 1094 | |
| 1095 | case DW_AT_high_pc: |
| 1096 | func->high = DW_ADDR (&attr); |
| 1097 | break; |
| 1098 | |
| 1099 | default: |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
| 1103 | else |
| 1104 | { |
| 1105 | switch (attr.name) |
| 1106 | { |
| 1107 | case DW_AT_name: |
| 1108 | name = DW_STRING (&attr); |
| 1109 | break; |
| 1110 | |
| 1111 | default: |
| 1112 | break; |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | if (abbrev->has_children) |
| 1118 | nesting_level++; |
| 1119 | } |
| 1120 | |
| 1121 | return true; |
| 1122 | } |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | /* Parse a DWARF2 compilation unit starting at INFO_PTR. This includes |
| 1130 | the compilation unit header that proceeds the DIE's, but does not |
| 1131 | include the length field that preceeds each compilation unit header. |
| 1132 | END_PTR points one past the end of this comp unit. |
| 1133 | |
| 1134 | This routine does not read the whole compilation unit; only enough |
| 1135 | to get to the line number information for the compilation unit. |
| 1136 | */ |
| 1137 | |
| 1138 | static struct comp_unit* |
| 1139 | parse_comp_unit (abfd, info_ptr, end_ptr) |
| 1140 | bfd* abfd; |
| 1141 | char* info_ptr; |
| 1142 | char* end_ptr; |
| 1143 | { |
| 1144 | struct comp_unit* unit; |
| 1145 | |
| 1146 | unsigned short version; |
| 1147 | unsigned int abbrev_offset; |
| 1148 | unsigned char addr_size; |
| 1149 | struct abbrev_info** abbrevs; |
| 1150 | |
| 1151 | unsigned int abbrev_number, bytes_read, i; |
| 1152 | struct abbrev_info *abbrev; |
| 1153 | struct attribute attr; |
| 1154 | |
| 1155 | version = read_2_bytes (abfd, info_ptr); |
| 1156 | info_ptr += 2; |
| 1157 | abbrev_offset = read_4_bytes (abfd, info_ptr); |
| 1158 | info_ptr += 4; |
| 1159 | addr_size = read_1_byte (abfd, info_ptr); |
| 1160 | info_ptr += 1; |
| 1161 | |
| 1162 | if (version != 2) |
| 1163 | { |
| 1164 | (*_bfd_error_handler) ("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information.", version ); |
| 1165 | bfd_set_error (bfd_error_bad_value); |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | if (addr_size > sizeof (bfd_vma)) |
| 1170 | { |
| 1171 | (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'.", |
| 1172 | addr_size, |
| 1173 | sizeof (bfd_vma)); |
| 1174 | bfd_set_error (bfd_error_bad_value); |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | if (addr_size != 4 && addr_size != 8) |
| 1179 | { |
| 1180 | (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '4' and '8'.", addr_size ); |
| 1181 | bfd_set_error (bfd_error_bad_value); |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | /* Read the abbrevs for this compilation unit into a table */ |
| 1186 | abbrevs = read_abbrevs (abfd, abbrev_offset); |
| 1187 | if (! abbrevs) |
| 1188 | return 0; |
| 1189 | |
| 1190 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
| 1191 | info_ptr += bytes_read; |
| 1192 | if (! abbrev_number) |
| 1193 | { |
| 1194 | (*_bfd_error_handler) ("Dwarf Error: Bad abbrev number: %d.", |
| 1195 | abbrev_number); |
| 1196 | bfd_set_error (bfd_error_bad_value); |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | abbrev = lookup_abbrev (abbrev_number, abbrevs); |
| 1201 | if (! abbrev) |
| 1202 | { |
| 1203 | (*_bfd_error_handler) ("Dwarf Error: Could not find abbrev number %d.", |
| 1204 | abbrev_number); |
| 1205 | bfd_set_error (bfd_error_bad_value); |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | unit = (struct comp_unit*) bfd_zalloc (abfd, sizeof (struct comp_unit)); |
| 1210 | unit->abfd = abfd; |
| 1211 | unit->addr_size = addr_size; |
| 1212 | unit->abbrevs = abbrevs; |
| 1213 | unit->end_ptr = end_ptr; |
| 1214 | |
| 1215 | for (i = 0; i < abbrev->num_attrs; ++i) |
| 1216 | { |
| 1217 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); |
| 1218 | |
| 1219 | /* Store the data if it is of an attribute we want to keep in a |
| 1220 | partial symbol table. */ |
| 1221 | switch (attr.name) |
| 1222 | { |
| 1223 | case DW_AT_stmt_list: |
| 1224 | unit->stmtlist = 1; |
| 1225 | unit->line_offset = DW_UNSND (&attr); |
| 1226 | break; |
| 1227 | |
| 1228 | case DW_AT_name: |
| 1229 | unit->name = DW_STRING (&attr); |
| 1230 | break; |
| 1231 | |
| 1232 | case DW_AT_low_pc: |
| 1233 | unit->low = DW_ADDR (&attr); |
| 1234 | break; |
| 1235 | |
| 1236 | case DW_AT_high_pc: |
| 1237 | unit->high = DW_ADDR (&attr); |
| 1238 | break; |
| 1239 | |
| 1240 | case DW_AT_comp_dir: |
| 1241 | { |
| 1242 | char* comp_dir = DW_STRING (&attr); |
| 1243 | if (comp_dir) |
| 1244 | { |
| 1245 | /* Irix 6.2 native cc prepends <machine>.: to the compilation |
| 1246 | directory, get rid of it. */ |
| 1247 | char *cp = (char*) strchr (comp_dir, ':'); |
| 1248 | |
| 1249 | if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/') |
| 1250 | comp_dir = cp + 1; |
| 1251 | } |
| 1252 | unit->comp_dir = comp_dir; |
| 1253 | break; |
| 1254 | } |
| 1255 | |
| 1256 | default: |
| 1257 | break; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | unit->first_child_die_ptr = info_ptr; |
| 1262 | return unit; |
| 1263 | } |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | |
| 1268 | |
| 1269 | /* Return true if UNIT contains the address given by ADDR. */ |
| 1270 | |
| 1271 | static boolean |
| 1272 | comp_unit_contains_address (unit, addr) |
| 1273 | struct comp_unit* unit; |
| 1274 | bfd_vma addr; |
| 1275 | { |
| 1276 | return ! unit->error |
| 1277 | && ( addr >= (unit->low & 0xffffffff) |
| 1278 | && addr <= (unit->high & 0xffffffff)); |
| 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | /* If UNIT contains ADDR, set the output parameters to the values for |
| 1283 | the line containing ADDR. The output parameters, FILENAME_PTR, |
| 1284 | FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects |
| 1285 | to be filled in. |
| 1286 | |
| 1287 | Return true of UNIT contains ADDR, and no errors were encountered; |
| 1288 | false otherwise. */ |
| 1289 | |
| 1290 | static boolean |
| 1291 | comp_unit_find_nearest_line (unit, addr, |
| 1292 | filename_ptr, functionname_ptr, linenumber_ptr) |
| 1293 | struct comp_unit* unit; |
| 1294 | bfd_vma addr; |
| 1295 | const char **filename_ptr; |
| 1296 | const char **functionname_ptr; |
| 1297 | unsigned int *linenumber_ptr; |
| 1298 | { |
| 1299 | boolean line_p; |
| 1300 | boolean func_p; |
| 1301 | |
| 1302 | if (unit->error) |
| 1303 | return false; |
| 1304 | |
| 1305 | if (! unit->line_table) |
| 1306 | { |
| 1307 | if (! unit->stmtlist) |
| 1308 | { |
| 1309 | unit->error = 1; |
| 1310 | return false; |
| 1311 | } |
| 1312 | |
| 1313 | unit->line_table = decode_line_info (unit); |
| 1314 | |
| 1315 | if (! unit->line_table) |
| 1316 | { |
| 1317 | unit->error = 1; |
| 1318 | return false; |
| 1319 | } |
| 1320 | |
| 1321 | if (! scan_unit_for_functions (unit)) |
| 1322 | { |
| 1323 | unit->error = 1; |
| 1324 | return false; |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | line_p = lookup_address_in_line_info_table (unit->line_table, |
| 1329 | addr, |
| 1330 | filename_ptr, |
| 1331 | linenumber_ptr); |
| 1332 | func_p = lookup_address_in_function_table (unit->function_table, |
| 1333 | addr, |
| 1334 | functionname_ptr); |
| 1335 | return line_p || func_p; |
| 1336 | } |
| 1337 | |
| 1338 | /* The DWARF2 version of find_nearest line. |
| 1339 | Return true if the line is found without error. */ |
| 1340 | |
| 1341 | boolean |
| 1342 | _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, |
| 1343 | filename_ptr, functionname_ptr, linenumber_ptr) |
| 1344 | bfd *abfd; |
| 1345 | asection *section; |
| 1346 | asymbol **symbols; |
| 1347 | bfd_vma offset; |
| 1348 | const char **filename_ptr; |
| 1349 | const char **functionname_ptr; |
| 1350 | unsigned int *linenumber_ptr; |
| 1351 | { |
| 1352 | /* Read each compilation unit from the section .debug_info, and check |
| 1353 | to see if it contains the address we are searching for. If yes, |
| 1354 | lookup the address, and return the line number info. If no, go |
| 1355 | on to the next compilation unit. |
| 1356 | |
| 1357 | We keep a list of all the previously read compilation units, and |
| 1358 | a pointer to the next un-read compilation unit. Check the |
| 1359 | previously read units before reading more. |
| 1360 | */ |
| 1361 | |
| 1362 | struct dwarf2_debug *stash = elf_tdata (abfd)->dwarf2_find_line_info; |
| 1363 | |
| 1364 | /* What address are we looking for? */ |
| 1365 | bfd_vma addr = offset + section->vma; |
| 1366 | |
| 1367 | struct comp_unit* each; |
| 1368 | |
| 1369 | *filename_ptr = NULL; |
| 1370 | *functionname_ptr = NULL; |
| 1371 | *linenumber_ptr = 0; |
| 1372 | |
| 1373 | if (! stash) |
| 1374 | { |
| 1375 | asection *msec; |
| 1376 | unsigned long size; |
| 1377 | |
| 1378 | stash = elf_tdata (abfd)->dwarf2_find_line_info = |
| 1379 | (struct dwarf2_debug*) bfd_zalloc (abfd, sizeof (struct dwarf2_debug)); |
| 1380 | |
| 1381 | if (! stash) |
| 1382 | return false; |
| 1383 | |
| 1384 | msec = bfd_get_section_by_name (abfd, ".debug_info"); |
| 1385 | if (! msec) |
| 1386 | { |
| 1387 | /* No dwarf2 info. Note that at this point the stash |
| 1388 | has been allocated, but contains zeros, this lets |
| 1389 | future calls to this function fail quicker. */ |
| 1390 | return false; |
| 1391 | } |
| 1392 | |
| 1393 | size = bfd_get_section_size_before_reloc (msec); |
| 1394 | stash->info_ptr = (unsigned char*) bfd_alloc (abfd, size); |
| 1395 | |
| 1396 | if (! stash->info_ptr) |
| 1397 | return false; |
| 1398 | |
| 1399 | if (! bfd_get_section_contents (abfd, msec, stash->info_ptr, 0, size)) |
| 1400 | { |
| 1401 | stash->info_ptr = 0; |
| 1402 | return false; |
| 1403 | } |
| 1404 | |
| 1405 | stash->info_ptr_end = stash->info_ptr + size; |
| 1406 | } |
| 1407 | |
| 1408 | |
| 1409 | /* A null info_ptr indicates that there is no dwarf2 info |
| 1410 | (or that an error occured while setting up the stash). */ |
| 1411 | |
| 1412 | if (! stash->info_ptr) |
| 1413 | return false; |
| 1414 | |
| 1415 | |
| 1416 | |
| 1417 | /* Check the previously read comp. units first. */ |
| 1418 | |
| 1419 | for (each = stash->all_comp_units; each; each = each->next_unit) |
| 1420 | { |
| 1421 | if (comp_unit_contains_address (each, addr)) |
| 1422 | return comp_unit_find_nearest_line (each, addr, |
| 1423 | filename_ptr, |
| 1424 | functionname_ptr, |
| 1425 | linenumber_ptr); |
| 1426 | } |
| 1427 | |
| 1428 | |
| 1429 | /* Read each remaining comp. units checking each as they are read. */ |
| 1430 | while (stash->info_ptr < stash->info_ptr_end) |
| 1431 | { |
| 1432 | struct comp_unit* each; |
| 1433 | unsigned int length; |
| 1434 | |
| 1435 | length = read_4_bytes (abfd, stash->info_ptr); |
| 1436 | stash->info_ptr += 4; |
| 1437 | |
| 1438 | if (length > 0) |
| 1439 | { |
| 1440 | each = parse_comp_unit (abfd, stash->info_ptr, |
| 1441 | stash->info_ptr + length); |
| 1442 | stash->info_ptr += length; |
| 1443 | |
| 1444 | if (each) |
| 1445 | { |
| 1446 | each->next_unit = stash->all_comp_units; |
| 1447 | stash->all_comp_units = each; |
| 1448 | |
| 1449 | if (comp_unit_contains_address (each, addr)) |
| 1450 | return comp_unit_find_nearest_line (each, addr, |
| 1451 | filename_ptr, |
| 1452 | functionname_ptr, |
| 1453 | linenumber_ptr); |
| 1454 | } |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | return false; |
| 1459 | } |
| 1460 | |
| 1461 | /* end of file */ |