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