dwarf2.c 103 KB
Newer Older
Richard Henderson's avatar
Richard Henderson committed
1
/* DWARF 2 support.
2
   Copyright 1994-2013 Free Software Foundation, Inc.
Richard Henderson's avatar
Richard Henderson committed
3 4 5 6 7 8 9 10 11 12 13 14

   Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
   (gavin@cygnus.com).

   From the dwarf2read.c header:
   Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
   Inc.  with support from Florida State University (under contract
   with the Ada Joint Program Office), and Silicon Graphics, Inc.
   Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
   based on Fred Fish's (Cygnus Support) implementation of DWARF 1
   support in dwarfread.c

15
   This file is part of BFD.
Richard Henderson's avatar
Richard Henderson committed
16

17 18
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
19
   the Free Software Foundation; either version 3 of the License, or (at
20
   your option) any later version.
Richard Henderson's avatar
Richard Henderson committed
21

22 23 24 25
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
Richard Henderson's avatar
Richard Henderson committed
26

27 28
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
29 30
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */
Richard Henderson's avatar
Richard Henderson committed
31 32

#include "sysdep.h"
Alan Modra's avatar
 
Alan Modra committed
33
#include "bfd.h"
Richard Henderson's avatar
Richard Henderson committed
34 35 36
#include "libiberty.h"
#include "libbfd.h"
#include "elf-bfd.h"
Tom Tromey's avatar
bfd  
Tom Tromey committed
37
#include "dwarf2.h"
Richard Henderson's avatar
Richard Henderson committed
38 39

/* The data in the .debug_line statement prologue looks like this.  */
40

Richard Henderson's avatar
Richard Henderson committed
41
struct line_head
42
{
43
  bfd_vma total_length;
44
  unsigned short version;
45
  bfd_vma prologue_length;
46
  unsigned char minimum_instruction_length;
Jakub Jelinek's avatar
Jakub Jelinek committed
47
  unsigned char maximum_ops_per_insn;
48 49 50 51 52 53 54 55 56
  unsigned char default_is_stmt;
  int line_base;
  unsigned char line_range;
  unsigned char opcode_base;
  unsigned char *standard_opcode_lengths;
};

/* Attributes have a name and a value.  */

Richard Henderson's avatar
Richard Henderson committed
57
struct attribute
58 59 60 61
{
  enum dwarf_attribute name;
  enum dwarf_form form;
  union
Richard Henderson's avatar
Richard Henderson committed
62
  {
63 64
    char *str;
    struct dwarf_block *blk;
65 66
    bfd_uint64_t val;
    bfd_int64_t sval;
67 68 69 70
  }
  u;
};

71
/* Blocks are a bunch of untyped bytes.  */
Richard Henderson's avatar
Richard Henderson committed
72
struct dwarf_block
73 74
{
  unsigned int size;
Alan Modra's avatar
Alan Modra committed
75
  bfd_byte *data;
76
};
Richard Henderson's avatar
Richard Henderson committed
77

Daniel Jacobowitz's avatar
Daniel Jacobowitz committed
78
struct adjusted_section
79 80 81 82 83
{
  asection *section;
  bfd_vma adj_vma;
};

84 85 86
struct dwarf2_debug
{
  /* A list of all previously read comp_units.  */
Alan Modra's avatar
Alan Modra committed
87
  struct comp_unit *all_comp_units;
Richard Henderson's avatar
Richard Henderson committed
88

89 90 91
  /* Last comp unit in list above.  */
  struct comp_unit *last_comp_unit;

92 93 94
  /* Names of the debug sections.  */
  const struct dwarf_debug_section *debug_sections;

Richard Henderson's avatar
Richard Henderson committed
95 96
  /* The next unread compilation unit within the .debug_info section.
     Zero indicates that the .debug_info section has not been loaded
97
     into a buffer yet.  */
Alan Modra's avatar
Alan Modra committed
98
  bfd_byte *info_ptr;
Richard Henderson's avatar
Richard Henderson committed
99

100
  /* Pointer to the end of the .debug_info section memory buffer.  */
Alan Modra's avatar
Alan Modra committed
101
  bfd_byte *info_ptr_end;
Richard Henderson's avatar
Richard Henderson committed
102

103 104 105
  /* Pointer to the bfd, section and address of the beginning of the
     section.  The bfd might be different than expected because of
     gnu_debuglink sections.  */
106
  bfd *bfd_ptr;
Alan Modra's avatar
Alan Modra committed
107 108
  asection *sec;
  bfd_byte *sec_info_ptr;
109

110 111 112 113 114 115 116 117 118 119
  /* Support for alternate debug info sections created by the DWZ utility:
     This includes a pointer to an alternate bfd which contains *extra*,
     possibly duplicate debug sections, and pointers to the loaded
     .debug_str and .debug_info sections from this bfd.  */
  bfd *          alt_bfd_ptr;
  bfd_byte *     alt_dwarf_str_buffer;
  bfd_size_type  alt_dwarf_str_size;
  bfd_byte *     alt_dwarf_info_buffer;
  bfd_size_type  alt_dwarf_info_size;

120 121 122 123 124 125
  /* A pointer to the memory block allocated for info_ptr.  Neither
     info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
     beginning of the malloc block.  This is used only to free the
     memory later.  */
  bfd_byte *info_ptr_memory;

126
  /* Pointer to the symbol table.  */
Alan Modra's avatar
Alan Modra committed
127
  asymbol **syms;
128

129
  /* Pointer to the .debug_abbrev section loaded into memory.  */
Alan Modra's avatar
Alan Modra committed
130
  bfd_byte *dwarf_abbrev_buffer;
Richard Henderson's avatar
Richard Henderson committed
131

132
  /* Length of the loaded .debug_abbrev section.  */
133
  bfd_size_type dwarf_abbrev_size;
134 135

  /* Buffer for decode_line_info.  */
Alan Modra's avatar
Alan Modra committed
136
  bfd_byte *dwarf_line_buffer;
137 138

  /* Length of the loaded .debug_line section.  */
139
  bfd_size_type dwarf_line_size;
140 141

  /* Pointer to the .debug_str section loaded into memory.  */
Alan Modra's avatar
Alan Modra committed
142
  bfd_byte *dwarf_str_buffer;
143 144

  /* Length of the loaded .debug_str section.  */
145
  bfd_size_type dwarf_str_size;
146 147 148 149 150

  /* Pointer to the .debug_ranges section loaded into memory. */
  bfd_byte *dwarf_ranges_buffer;

  /* Length of the loaded .debug_ranges section. */
151
  bfd_size_type dwarf_ranges_size;
Fred Fish's avatar
Fred Fish committed
152 153 154 155 156 157

  /* If the most recent call to bfd_find_nearest_line was given an
     address in an inlined function, preserve a pointer into the
     calling chain for subsequent calls to bfd_find_inliner_info to
     use. */
  struct funcinfo *inliner_chain;
158

Daniel Jacobowitz's avatar
Daniel Jacobowitz committed
159 160
  /* Number of sections whose VMA we must adjust.  */
  unsigned int adjusted_section_count;
161

Daniel Jacobowitz's avatar
Daniel Jacobowitz committed
162 163
  /* Array of sections with adjusted VMA.  */
  struct adjusted_section *adjusted_sections;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

  /* Number of times find_line is called.  This is used in
     the heuristic for enabling the info hash tables.  */
  int info_hash_count;

#define STASH_INFO_HASH_TRIGGER    100

  /* Hash table mapping symbol names to function infos.  */
  struct info_hash_table *funcinfo_hash_table;

  /* Hash table mapping symbol names to variable infos.  */
  struct info_hash_table *varinfo_hash_table;

  /* Head of comp_unit list in the last hash table update.  */
  struct comp_unit *hash_units_head;

  /* Status of info hash.  */
  int info_hash_status;
#define STASH_INFO_HASH_OFF        0
#define STASH_INFO_HASH_ON         1
#define STASH_INFO_HASH_DISABLED   2
185 186 187

  /* True if we opened bfd_ptr.  */
  bfd_boolean close_on_cleanup;
Richard Henderson's avatar
Richard Henderson committed
188 189
};

190 191
struct arange
{
192 193 194 195
  struct arange *next;
  bfd_vma low;
  bfd_vma high;
};
Richard Henderson's avatar
Richard Henderson committed
196 197

/* A minimal decoding of DWARF2 compilation units.  We only decode
198
   what's needed to get to the line number information.  */
Richard Henderson's avatar
Richard Henderson committed
199

200 201 202
struct comp_unit
{
  /* Chain the previously read compilation units.  */
Alan Modra's avatar
Alan Modra committed
203
  struct comp_unit *next_unit;
Richard Henderson's avatar
Richard Henderson committed
204

205 206 207 208
  /* Likewise, chain the compilation unit read after this one.
     The comp units are stored in reversed reading order.  */
  struct comp_unit *prev_unit;

209
  /* Keep the bfd convenient (for memory allocation).  */
Alan Modra's avatar
Alan Modra committed
210
  bfd *abfd;
Richard Henderson's avatar
Richard Henderson committed
211

Alan Modra's avatar
Alan Modra committed
212 213 214
  /* The lowest and highest addresses contained in this compilation
     unit as specified in the compilation unit header.  */
  struct arange arange;
Richard Henderson's avatar
Richard Henderson committed
215

216
  /* The DW_AT_name attribute (for error messages).  */
Alan Modra's avatar
Alan Modra committed
217
  char *name;
Richard Henderson's avatar
Richard Henderson committed
218

219
  /* The abbrev hash table.  */
Alan Modra's avatar
Alan Modra committed
220
  struct abbrev_info **abbrevs;
Richard Henderson's avatar
Richard Henderson committed
221

222
  /* Note that an error was found by comp_unit_find_nearest_line.  */
Richard Henderson's avatar
Richard Henderson committed
223 224
  int error;

225
  /* The DW_AT_comp_dir attribute.  */
Alan Modra's avatar
Alan Modra committed
226
  char *comp_dir;
Richard Henderson's avatar
Richard Henderson committed
227

228
  /* TRUE if there is a line number table associated with this comp. unit.  */
Richard Henderson's avatar
Richard Henderson committed
229
  int stmtlist;
230

231 232
  /* Pointer to the current comp_unit so that we can find a given entry
     by its reference.  */
Alan Modra's avatar
Alan Modra committed
233
  bfd_byte *info_ptr_unit;
234

Alan Modra's avatar
Alan Modra committed
235 236 237
  /* Pointer to the start of the debug section, for DW_FORM_ref_addr.  */
  bfd_byte *sec_info_ptr;

238
  /* The offset into .debug_line of the line number table.  */
Richard Henderson's avatar
Richard Henderson committed
239 240
  unsigned long line_offset;

241
  /* Pointer to the first child die for the comp unit.  */
Alan Modra's avatar
Alan Modra committed
242
  bfd_byte *first_child_die_ptr;
Richard Henderson's avatar
Richard Henderson committed
243

244
  /* The end of the comp unit.  */
Alan Modra's avatar
Alan Modra committed
245
  bfd_byte *end_ptr;
Richard Henderson's avatar
Richard Henderson committed
246

247
  /* The decoded line number, NULL if not yet decoded.  */
Alan Modra's avatar
Alan Modra committed
248
  struct line_info_table *line_table;
Richard Henderson's avatar
Richard Henderson committed
249

250
  /* A list of the functions found in this comp. unit.  */
Alan Modra's avatar
Alan Modra committed
251
  struct funcinfo *function_table;
Richard Henderson's avatar
Richard Henderson committed
252

H.J. Lu's avatar
bfd/  
H.J. Lu committed
253 254 255
  /* A list of the variables found in this comp. unit.  */
  struct varinfo *variable_table;

256 257 258
  /* Pointer to dwarf2_debug structure.  */
  struct dwarf2_debug *stash;

Daniel Jacobowitz's avatar
Daniel Jacobowitz committed
259 260 261
  /* DWARF format version for this unit - from unit header.  */
  int version;

262
  /* Address size for this unit - from unit header.  */
Richard Henderson's avatar
Richard Henderson committed
263
  unsigned char addr_size;
264 265 266

  /* Offset size for this unit - from unit header.  */
  unsigned char offset_size;
267 268 269 270

  /* Base address for this unit - from DW_AT_low_pc attribute of
     DW_TAG_compile_unit DIE */
  bfd_vma base_address;
271 272 273

  /* TRUE if symbols are cached in hash table for faster lookup by name.  */
  bfd_boolean cached;
Richard Henderson's avatar
Richard Henderson committed
274 275
};

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
/* This data structure holds the information of an abbrev.  */
struct abbrev_info
{
  unsigned int number;		/* Number identifying abbrev.  */
  enum dwarf_tag tag;		/* DWARF tag.  */
  int has_children;		/* Boolean.  */
  unsigned int num_attrs;	/* Number of attributes.  */
  struct attr_abbrev *attrs;	/* An array of attribute descriptions.  */
  struct abbrev_info *next;	/* Next in chain.  */
};

struct attr_abbrev
{
  enum dwarf_attribute name;
  enum dwarf_form form;
};

293 294 295
/* Map of uncompressed DWARF debug section name to compressed one.  It
   is terminated by NULL uncompressed_name.  */

Mike Frysinger's avatar
Mike Frysinger committed
296
const struct dwarf_debug_section dwarf_debug_sections[] =
297 298 299 300 301
{
  { ".debug_abbrev",		".zdebug_abbrev" },
  { ".debug_aranges",		".zdebug_aranges" },
  { ".debug_frame",		".zdebug_frame" },
  { ".debug_info",		".zdebug_info" },
302
  { ".debug_info",		".zdebug_info" },
303 304 305
  { ".debug_line",		".zdebug_line" },
  { ".debug_loc",		".zdebug_loc" },
  { ".debug_macinfo",		".zdebug_macinfo" },
306
  { ".debug_macro",		".zdebug_macro" },
307 308 309 310 311 312
  { ".debug_pubnames",		".zdebug_pubnames" },
  { ".debug_pubtypes",		".zdebug_pubtypes" },
  { ".debug_ranges",		".zdebug_ranges" },
  { ".debug_static_func",	".zdebug_static_func" },
  { ".debug_static_vars",	".zdebug_static_vars" },
  { ".debug_str",		".zdebug_str", },
313
  { ".debug_str",		".zdebug_str", },
314 315 316 317 318 319 320 321 322 323 324 325
  { ".debug_types",		".zdebug_types" },
  /* GNU DWARF 1 extensions */
  { ".debug_sfnames",		".zdebug_sfnames" },
  { ".debug_srcinfo",		".zebug_srcinfo" },
  /* SGI/MIPS DWARF 2 extensions */
  { ".debug_funcnames",		".zdebug_funcnames" },
  { ".debug_typenames",		".zdebug_typenames" },
  { ".debug_varnames",		".zdebug_varnames" },
  { ".debug_weaknames",		".zdebug_weaknames" },
  { NULL,			NULL },
};

326 327
/* NB/ Numbers in this enum must match up with indicies
   into the dwarf_debug_sections[] array above.  */
328 329 330 331 332 333
enum dwarf_debug_section_enum
{
  debug_abbrev = 0,
  debug_aranges,
  debug_frame,
  debug_info,
334
  debug_info_alt,
335 336 337
  debug_line,
  debug_loc,
  debug_macinfo,
338
  debug_macro,
339 340 341 342 343 344
  debug_pubnames,
  debug_pubtypes,
  debug_ranges,
  debug_static_func,
  debug_static_vars,
  debug_str,
345
  debug_str_alt,
346 347 348 349 350 351 352 353 354
  debug_types,
  debug_sfnames,
  debug_srcinfo,
  debug_funcnames,
  debug_typenames,
  debug_varnames,
  debug_weaknames
};

355 356 357 358 359 360 361
#ifndef ABBREV_HASH_SIZE
#define ABBREV_HASH_SIZE 121
#endif
#ifndef ATTR_ALLOC_CHUNK
#define ATTR_ALLOC_CHUNK 4
#endif

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
/* Variable and function hash tables.  This is used to speed up look-up
   in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
   In order to share code between variable and function infos, we use
   a list of untyped pointer for all variable/function info associated with
   a symbol.  We waste a bit of memory for list with one node but that
   simplifies the code.  */

struct info_list_node
{
  struct info_list_node *next;
  void *info;
};

/* Info hash entry.  */
struct info_hash_entry
{
  struct bfd_hash_entry root;
  struct info_list_node *head;
};

struct info_hash_table
{
  struct bfd_hash_table base;
};

/* Function to create a new entry in info hash table. */

static struct bfd_hash_entry *
info_hash_table_newfunc (struct bfd_hash_entry *entry,
			 struct bfd_hash_table *table,
			 const char *string)
{
  struct info_hash_entry *ret = (struct info_hash_entry *) entry;

  /* Allocate the structure if it has not already been allocated by a
     derived class.  */
  if (ret == NULL)
    {
400 401
      ret = (struct info_hash_entry *) bfd_hash_allocate (table,
                                                          sizeof (* ret));
402 403 404 405 406 407
      if (ret == NULL)
	return NULL;
    }

  /* Call the allocation method of the base class.  */
  ret = ((struct info_hash_entry *)
408
	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

  /* Initialize the local fields here.  */
  if (ret)
    ret->head = NULL;

  return (struct bfd_hash_entry *) ret;
}

/* Function to create a new info hash table.  It returns a pointer to the
   newly created table or NULL if there is any error.  We need abfd
   solely for memory allocation.  */

static struct info_hash_table *
create_info_hash_table (bfd *abfd)
{
  struct info_hash_table *hash_table;

Alan Modra's avatar
Alan Modra committed
426 427
  hash_table = ((struct info_hash_table *)
		bfd_alloc (abfd, sizeof (struct info_hash_table)));
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
  if (!hash_table)
    return hash_table;

  if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
			    sizeof (struct info_hash_entry)))
    {
      bfd_release (abfd, hash_table);
      return NULL;
    }

  return hash_table;
}

/* Insert an info entry into an info hash table.  We do not check of
   duplicate entries.  Also, the caller need to guarantee that the
   right type of info in inserted as info is passed as a void* pointer.
   This function returns true if there is no error.  */

static bfd_boolean
insert_info_hash_table (struct info_hash_table *hash_table,
			const char *key,
			void *info,
			bfd_boolean copy_p)
{
  struct info_hash_entry *entry;
  struct info_list_node *node;

  entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
						     key, TRUE, copy_p);
  if (!entry)
    return FALSE;

460 461
  node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
                                                      sizeof (*node));
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
  if (!node)
    return FALSE;

  node->info = info;
  node->next = entry->head;
  entry->head = node;

  return TRUE;
}

/* Look up an info entry list from an info hash table.  Return NULL
   if there is none. */

static struct info_list_node *
lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
{
  struct info_hash_entry *entry;

  entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
						     FALSE, FALSE);
  return entry ? entry->head : NULL;
}

Craig Silverstein's avatar
bfd/  
Craig Silverstein committed
485
/* Read a section into its appropriate place in the dwarf2_debug
486
   struct (indicated by SECTION_BUFFER and SECTION_SIZE).  If SYMS is
Craig Silverstein's avatar
bfd/  
Craig Silverstein committed
487
   not NULL, use bfd_simple_get_relocated_section_contents to read the
488 489
   section contents, otherwise use bfd_get_section_contents.  Fail if
   the located section does not contain at least OFFSET bytes.  */
Craig Silverstein's avatar
bfd/  
Craig Silverstein committed
490 491

static bfd_boolean
492
read_section (bfd *           abfd,
493
	      const struct dwarf_debug_section *sec,
494 495 496 497
	      asymbol **      syms,
	      bfd_uint64_t    offset,
	      bfd_byte **     section_buffer,
	      bfd_size_type * section_size)
Craig Silverstein's avatar
bfd/  
Craig Silverstein committed
498 499
{
  asection *msec;
500
  const char *section_name = sec->uncompressed_name;
Craig Silverstein's avatar
bfd/  
Craig Silverstein committed
501

502 503
  /* The section may have already been read.  */
  if (*section_buffer == NULL)
Craig Silverstein's avatar
bfd/  
Craig Silverstein committed
504
    {
505
      msec = bfd_get_section_by_name (abfd, section_name);
506
      if (! msec)
507
	{
508 509 510
	  section_name = sec->compressed_name;
          if (section_name != NULL)
            msec = bfd_get_section_by_name (abfd, section_name);
511 512
	}
      if (! msec)
513
	{
514 515
	  (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."),
                                 sec->uncompressed_name);
516 517 518
	  bfd_set_error (bfd_error_bad_value);
	  return FALSE;
	}
519

520
      *section_size = msec->rawsize ? msec->rawsize : msec->size;
Cary Coutant's avatar
Cary Coutant committed
521 522 523
      if (syms)
	{
	  *section_buffer
Alan Modra's avatar
Alan Modra committed
524
	    = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
Cary Coutant's avatar
Cary Coutant committed
525 526 527 528 529 530 531 532 533 534 535 536
	  if (! *section_buffer)
	    return FALSE;
	}
      else
	{
	  *section_buffer = (bfd_byte *) bfd_malloc (*section_size);
	  if (! *section_buffer)
	    return FALSE;
	  if (! bfd_get_section_contents (abfd, msec, *section_buffer,
					  0, *section_size))
	    return FALSE;
	}
Craig Silverstein's avatar
bfd/  
Craig Silverstein committed
537 538 539
    }

  /* It is possible to get a bad value for the offset into the section
540
     that the client wants.  Validate it here to avoid trouble later.  */
Craig Silverstein's avatar
bfd/  
Craig Silverstein committed
541 542
  if (offset != 0 && offset >= *section_size)
    {
Alan Modra's avatar
Alan Modra committed
543 544
      (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu)"
			       " greater than or equal to %s size (%lu)."),
545
			     (long) offset, section_name, *section_size);
Craig Silverstein's avatar
bfd/  
Craig Silverstein committed
546 547 548 549 550 551 552
      bfd_set_error (bfd_error_bad_value);
      return FALSE;
    }

  return TRUE;
}

553 554
/* VERBATIM
   The following function up to the END VERBATIM mark are
555
   copied directly from dwarf2read.c.  */
Richard Henderson's avatar
Richard Henderson committed
556

557
/* Read dwarf information from a buffer.  */
Richard Henderson's avatar
Richard Henderson committed
558 559

static unsigned int
Alan Modra's avatar
Alan Modra committed
560
read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
Richard Henderson's avatar
Richard Henderson committed
561
{
562
  return bfd_get_8 (abfd, buf);
Richard Henderson's avatar
Richard Henderson committed
563 564 565
}

static int
Alan Modra's avatar
Alan Modra committed
566
read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
Richard Henderson's avatar
Richard Henderson committed
567
{
568
  return bfd_get_signed_8 (abfd, buf);
Richard Henderson's avatar
Richard Henderson committed
569 570 571
}

static unsigned int
Alan Modra's avatar
Alan Modra committed
572
read_2_bytes (bfd *abfd, bfd_byte *buf)
Richard Henderson's avatar
Richard Henderson committed
573
{
574
  return bfd_get_16 (abfd, buf);
Richard Henderson's avatar
Richard Henderson committed
575 576 577
}

static unsigned int
Alan Modra's avatar
Alan Modra committed
578
read_4_bytes (bfd *abfd, bfd_byte *buf)
Richard Henderson's avatar
Richard Henderson committed
579
{
580
  return bfd_get_32 (abfd, buf);
Richard Henderson's avatar
Richard Henderson committed
581 582
}

583
static bfd_uint64_t
Alan Modra's avatar
Alan Modra committed
584
read_8_bytes (bfd *abfd, bfd_byte *buf)
Richard Henderson's avatar
Richard Henderson committed
585
{
586
  return bfd_get_64 (abfd, buf);
Richard Henderson's avatar
Richard Henderson committed
587 588
}

Alan Modra's avatar
Alan Modra committed
589
static bfd_byte *
590
read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
Alan Modra's avatar
Alan Modra committed
591
	      bfd_byte *buf,
592
	      unsigned int size ATTRIBUTE_UNUSED)
Richard Henderson's avatar
Richard Henderson committed
593 594 595 596 597
{
  return buf;
}

static char *
598
read_string (bfd *abfd ATTRIBUTE_UNUSED,
Alan Modra's avatar
Alan Modra committed
599
	     bfd_byte *buf,
600
	     unsigned int *bytes_read_ptr)
Richard Henderson's avatar
Richard Henderson committed
601
{
602
  /* Return a pointer to the embedded string.  */
Alan Modra's avatar
Alan Modra committed
603
  char *str = (char *) buf;
604

Alan Modra's avatar
Alan Modra committed
605
  if (*str == '\0')
Richard Henderson's avatar
Richard Henderson committed
606 607 608 609
    {
      *bytes_read_ptr = 1;
      return NULL;
    }
610

Alan Modra's avatar
Alan Modra committed
611 612
  *bytes_read_ptr = strlen (str) + 1;
  return str;
Richard Henderson's avatar
Richard Henderson committed
613 614
}

615 616
/* END VERBATIM */

617
static char *
618 619 620
read_indirect_string (struct comp_unit * unit,
		      bfd_byte *         buf,
		      unsigned int *     bytes_read_ptr)
621
{
622
  bfd_uint64_t offset;
623
  struct dwarf2_debug *stash = unit->stash;
Alan Modra's avatar
Alan Modra committed
624
  char *str;
625 626 627 628 629

  if (unit->offset_size == 4)
    offset = read_4_bytes (unit->abfd, buf);
  else
    offset = read_8_bytes (unit->abfd, buf);
630

631 632
  *bytes_read_ptr = unit->offset_size;

633 634
  if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
                      stash->syms, offset,
Daniel Jacobowitz's avatar
Daniel Jacobowitz committed
635
		      &stash->dwarf_str_buffer, &stash->dwarf_str_size))
636
    return NULL;
637

Alan Modra's avatar
Alan Modra committed
638 639
  str = (char *) stash->dwarf_str_buffer + offset;
  if (*str == '\0')
640
    return NULL;
Alan Modra's avatar
Alan Modra committed
641
  return str;
642 643
}

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
/* Like read_indirect_string but uses a .debug_str located in
   an alternate filepointed to by the .gnu_debuglink section.
   Used to impement DW_FORM_GNU_strp_alt.  */

static char *
read_alt_indirect_string (struct comp_unit * unit,
			  bfd_byte *         buf,
			  unsigned int *     bytes_read_ptr)
{
  bfd_uint64_t offset;
  struct dwarf2_debug *stash = unit->stash;
  char *str;

  if (unit->offset_size == 4)
    offset = read_4_bytes (unit->abfd, buf);
  else
    offset = read_8_bytes (unit->abfd, buf);

  *bytes_read_ptr = unit->offset_size;

  if (stash->alt_bfd_ptr == NULL)
    {
      bfd *  debug_bfd;
      char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);

      if (debug_filename == NULL)
	return NULL;

      if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
	  || ! bfd_check_format (debug_bfd, bfd_object))
	{
	  if (debug_bfd)
	    bfd_close (debug_bfd);

	  /* FIXME: Should we report our failure to follow the debuglink ?  */
	  free (debug_filename);
	  return NULL;
	}
      stash->alt_bfd_ptr = debug_bfd;
    }
  
  if (! read_section (unit->stash->alt_bfd_ptr,
		      stash->debug_sections + debug_str_alt,
		      NULL, /* FIXME: Do we need to load alternate symbols ?  */
		      offset,
		      &stash->alt_dwarf_str_buffer,
		      &stash->alt_dwarf_str_size))
    return NULL;

  str = (char *) stash->alt_dwarf_str_buffer + offset;
  if (*str == '\0')
    return NULL;

  return str;
}

/* Resolve an alternate reference from UNIT at OFFSET.
   Returns a pointer into the loaded alternate CU upon success
   or NULL upon failure.  */

static bfd_byte *
read_alt_indirect_ref (struct comp_unit * unit,
		       bfd_uint64_t       offset)
{
  struct dwarf2_debug *stash = unit->stash;

  if (stash->alt_bfd_ptr == NULL)
    {
      bfd *  debug_bfd;
      char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);

      if (debug_filename == NULL)
	return FALSE;

      if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
	  || ! bfd_check_format (debug_bfd, bfd_object))
	{
	  if (debug_bfd)
	    bfd_close (debug_bfd);

	  /* FIXME: Should we report our failure to follow the debuglink ?  */
	  free (debug_filename);
	  return NULL;
	}
      stash->alt_bfd_ptr = debug_bfd;
    }
  
  if (! read_section (unit->stash->alt_bfd_ptr,
		      stash->debug_sections + debug_info_alt,
		      NULL, /* FIXME: Do we need to load alternate symbols ?  */
		      offset,
		      &stash->alt_dwarf_info_buffer,
		      &stash->alt_dwarf_info_size))
    return NULL;

  return stash->alt_dwarf_info_buffer + offset;
}

742
static bfd_uint64_t
Alan Modra's avatar
Alan Modra committed
743
read_address (struct comp_unit *unit, bfd_byte *buf)
Richard Henderson's avatar
Richard Henderson committed
744
{
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
  int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;

  if (signed_vma)
    {
      switch (unit->addr_size)
	{
	case 8:
	  return bfd_get_signed_64 (unit->abfd, buf);
	case 4:
	  return bfd_get_signed_32 (unit->abfd, buf);
	case 2:
	  return bfd_get_signed_16 (unit->abfd, buf);
	default:
	  abort ();
	}
    }
  else
Richard Henderson's avatar
Richard Henderson committed
762
    {
763 764 765 766 767 768 769 770 771 772 773
      switch (unit->addr_size)
	{
	case 8:
	  return bfd_get_64 (unit->abfd, buf);
	case 4:
	  return bfd_get_32 (unit->abfd, buf);
	case 2:
	  return bfd_get_16 (unit->abfd, buf);
	default:
	  abort ();
	}
Richard Henderson's avatar
Richard Henderson committed
774 775 776 777 778 779
    }
}

/* Lookup an abbrev_info structure in the abbrev hash table.  */

static struct abbrev_info *
780
lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
Richard Henderson's avatar
Richard Henderson committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794
{
  unsigned int hash_number;
  struct abbrev_info *abbrev;

  hash_number = number % ABBREV_HASH_SIZE;
  abbrev = abbrevs[hash_number];

  while (abbrev)
    {
      if (abbrev->number == number)
	return abbrev;
      else
	abbrev = abbrev->next;
    }
795

Richard Henderson's avatar
Richard Henderson committed
796 797 798 799 800 801 802 803 804
  return NULL;
}

/* In DWARF version 2, the description of the debugging information is
   stored in a separate .debug_abbrev section.  Before we read any
   dies from a section we read in all abbreviations and install them
   in a hash table.  */

static struct abbrev_info**
805
read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
Richard Henderson's avatar
Richard Henderson committed
806 807
{
  struct abbrev_info **abbrevs;
Alan Modra's avatar
Alan Modra committed
808
  bfd_byte *abbrev_ptr;
Richard Henderson's avatar
Richard Henderson committed
809 810 811
  struct abbrev_info *cur_abbrev;
  unsigned int abbrev_number, bytes_read, abbrev_name;
  unsigned int abbrev_form, hash_number;
812
  bfd_size_type amt;
Richard Henderson's avatar
Richard Henderson committed
813

814 815
  if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
                      stash->syms, offset,
Daniel Jacobowitz's avatar
Daniel Jacobowitz committed
816
		      &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
Alan Modra's avatar
Alan Modra committed
817
    return NULL;
Richard Henderson's avatar
Richard Henderson committed
818

819
  amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
820
  abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
Alan Modra's avatar
Alan Modra committed
821 822
  if (abbrevs == NULL)
    return NULL;
Richard Henderson's avatar
Richard Henderson committed
823 824 825 826 827

  abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
  abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
  abbrev_ptr += bytes_read;

828
  /* Loop until we reach an abbrev number of 0.  */
Richard Henderson's avatar
Richard Henderson committed
829 830
  while (abbrev_number)
    {
831
      amt = sizeof (struct abbrev_info);
832
      cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
Alan Modra's avatar
Alan Modra committed
833 834
      if (cur_abbrev == NULL)
	return NULL;
Richard Henderson's avatar
Richard Henderson committed
835

836
      /* Read in abbrev header.  */
Richard Henderson's avatar
Richard Henderson committed
837
      cur_abbrev->number = abbrev_number;
838 839
      cur_abbrev->tag = (enum dwarf_tag)
	read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
Richard Henderson's avatar
Richard Henderson committed
840 841 842 843
      abbrev_ptr += bytes_read;
      cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
      abbrev_ptr += 1;

844
      /* Now read in declarations.  */
Richard Henderson's avatar
Richard Henderson committed
845 846 847 848
      abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
      abbrev_ptr += bytes_read;
      abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
      abbrev_ptr += bytes_read;
849

Richard Henderson's avatar
Richard Henderson committed
850 851 852 853
      while (abbrev_name)
	{
	  if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
	    {
854 855
	      struct attr_abbrev *tmp;

856 857
	      amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
	      amt *= sizeof (struct attr_abbrev);
858
	      tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
859
	      if (tmp == NULL)
Alan Modra's avatar
Alan Modra committed
860 861 862 863 864 865 866 867 868
		{
		  size_t i;

		  for (i = 0; i < ABBREV_HASH_SIZE; i++)
		    {
		      struct abbrev_info *abbrev = abbrevs[i];

		      while (abbrev)
			{
Nick Clifton's avatar
Nick Clifton committed
869 870
			  free (abbrev->attrs);
			  abbrev = abbrev->next;
Alan Modra's avatar
Alan Modra committed
871 872 873 874
			}
		    }
		  return NULL;
		}
875
	      cur_abbrev->attrs = tmp;
Richard Henderson's avatar
Richard Henderson committed
876
	    }
877

878 879 880 881
	  cur_abbrev->attrs[cur_abbrev->num_attrs].name
	    = (enum dwarf_attribute) abbrev_name;
	  cur_abbrev->attrs[cur_abbrev->num_attrs++].form
	    = (enum dwarf_form) abbrev_form;
Richard Henderson's avatar
Richard Henderson committed
882 883 884 885 886 887 888 889 890 891 892
	  abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
	  abbrev_ptr += bytes_read;
	  abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
	  abbrev_ptr += bytes_read;
	}

      hash_number = abbrev_number % ABBREV_HASH_SIZE;
      cur_abbrev->next = abbrevs[hash_number];
      abbrevs[hash_number] = cur_abbrev;

      /* Get next abbreviation.
893
	 Under Irix6 the abbreviations for a compilation unit are not
Richard Henderson's avatar
Richard Henderson committed
894 895 896 897 898 899
	 always properly terminated with an abbrev number of 0.
	 Exit loop if we encounter an abbreviation which we have
	 already read (which means we are about to read the abbreviations
	 for the next compile unit) or if the end of the abbreviation
	 table is reached.  */
      if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
Alan Modra's avatar
Alan Modra committed
900
	  >= stash->dwarf_abbrev_size)
Richard Henderson's avatar
Richard Henderson committed
901 902 903 904 905 906 907 908 909 910
	break;
      abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
      abbrev_ptr += bytes_read;
      if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
	break;
    }

  return abbrevs;
}

911
/* Read an attribute value described by an attribute form.  */
Richard Henderson's avatar
Richard Henderson committed
912

Alan Modra's avatar
Alan Modra committed
913
static bfd_byte *
914 915 916
read_attribute_value (struct attribute *attr,
		      unsigned form,
		      struct comp_unit *unit,
Alan Modra's avatar
Alan Modra committed
917
		      bfd_byte *info_ptr)
Richard Henderson's avatar
Richard Henderson committed
918 919 920 921
{
  bfd *abfd = unit->abfd;
  unsigned int bytes_read;
  struct dwarf_block *blk;
922
  bfd_size_type amt;
Richard Henderson's avatar
Richard Henderson committed
923

924
  attr->form = (enum dwarf_form) form;
925

926
  switch (form)
Richard Henderson's avatar
Richard Henderson committed
927 928
    {
    case DW_FORM_ref_addr:
Daniel Jacobowitz's avatar
Daniel Jacobowitz committed
929 930
      /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
	 DWARF3.  */
931
      if (unit->version == 3 || unit->version == 4)
Daniel Jacobowitz's avatar
Daniel Jacobowitz committed
932 933 934 935 936 937 938 939 940 941
	{
	  if (unit->offset_size == 4)
	    attr->u.val = read_4_bytes (unit->abfd, info_ptr);
	  else
	    attr->u.val = read_8_bytes (unit->abfd, info_ptr);
	  info_ptr += unit->offset_size;
	  break;
	}
      /* FALLTHROUGH */
    case DW_FORM_addr:
942
      attr->u.val = read_address (unit, info_ptr);
Richard Henderson's avatar
Richard Henderson committed
943 944
      info_ptr += unit->addr_size;
      break;
945
    case DW_FORM_GNU_ref_alt:
946 947 948 949 950 951 952
    case DW_FORM_sec_offset:
      if (unit->offset_size == 4)
	attr->u.val = read_4_bytes (unit->abfd, info_ptr);
      else
	attr->u.val = read_8_bytes (unit->abfd, info_ptr);
      info_ptr += unit->offset_size;
      break;
Richard Henderson's avatar
Richard Henderson committed
953
    case DW_FORM_block2:
954
      amt = sizeof (struct dwarf_block);
955
      blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
Alan Modra's avatar
Alan Modra committed
956 957
      if (blk == NULL)
	return NULL;
Richard Henderson's avatar
Richard Henderson committed
958 959 960 961
      blk->size = read_2_bytes (abfd, info_ptr);
      info_ptr += 2;
      blk->data = read_n_bytes (abfd, info_ptr, blk->size);
      info_ptr += blk->size;
962
      attr->u.blk = blk;
Richard Henderson's avatar
Richard Henderson committed
963 964
      break;
    case DW_FORM_block4:
965
      amt = sizeof (struct dwarf_block);
966
      blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
Alan Modra's avatar
Alan Modra committed
967 968
      if (blk == NULL)
	return NULL;
Richard Henderson's avatar
Richard Henderson committed
969 970 971 972
      blk->size = read_4_bytes (abfd, info_ptr);
      info_ptr += 4;
      blk->data = read_n_bytes (abfd, info_ptr, blk->size);
      info_ptr += blk->size;
973
      attr->u.blk = blk;
Richard Henderson's avatar
Richard Henderson committed
974 975
      break;
    case DW_FORM_data2:
976
      attr->u.val = read_2_bytes (abfd, info_ptr);
Richard Henderson's avatar
Richard Henderson committed
977 978 979
      info_ptr += 2;
      break;
    case DW_FORM_data4:
980
      attr->u.val = read_4_bytes (abfd, info_ptr);
Richard Henderson's avatar
Richard Henderson committed
981 982 983
      info_ptr += 4;
      break;
    case DW_FORM_data8:
984
      attr->u.val = read_8_bytes (abfd, info_ptr);
Richard Henderson's avatar
Richard Henderson committed
985 986 987
      info_ptr += 8;
      break;
    case DW_FORM_string:
988
      attr->u.str = read_string (abfd, info_ptr, &bytes_read);
Richard Henderson's avatar
Richard Henderson committed
989 990
      info_ptr += bytes_read;
      break;
991
    case DW_FORM_strp:
992
      attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
993 994
      info_ptr += bytes_read;
      break;
995 996 997 998
    case DW_FORM_GNU_strp_alt:
      attr->u.str = read_alt_indirect_string (unit, info_ptr, &bytes_read);
      info_ptr += bytes_read;
      break;
999
    case DW_FORM_exprloc:
Richard Henderson's avatar
Richard Henderson committed
1000
    case DW_FORM_block:
1001
      amt = sizeof (struct dwarf_block);
1002
      blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
Alan Modra's avatar
Alan Modra committed
1003 1004
      if (blk == NULL)
	return NULL;
Richard Henderson's avatar
Richard Henderson committed
1005 1006 1007 1008
      blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
      info_ptr += bytes_read;
      blk->data = read_n_bytes (abfd, info_ptr, blk->size);
      info_ptr += blk->size;
1009
      attr->u.blk = blk;
Richard Henderson's avatar
Richard Henderson committed
1010 1011
      break;
    case DW_FORM_block1:
1012
      amt = sizeof (struct dwarf_block);
1013
      blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
Alan Modra's avatar
Alan Modra committed
1014 1015
      if (blk == NULL)
	return NULL;
Richard Henderson's avatar
Richard Henderson committed
1016 1017 1018 1019
      blk->size = read_1_byte (abfd, info_ptr);
      info_ptr += 1;
      blk->data = read_n_bytes (abfd, info_ptr, blk->size);
      info_ptr += blk->size;
1020
      attr->u.blk = blk;
Richard Henderson's avatar
Richard Henderson committed
1021 1022
      break;
    case DW_FORM_data1:
1023
      attr->u.val = read_1_byte (abfd, info_ptr);
Richard Henderson's avatar
Richard Henderson committed
1024 1025 1026
      info_ptr += 1;
      break;
    case DW_FORM_flag:
1027
      attr->u.val = read_1_byte (abfd, info_ptr);
Richard Henderson's avatar
Richard Henderson committed
1028 1029
      info_ptr += 1;
      break;
1030 1031 1032
    case DW_FORM_flag_present:
      attr->u.val = 1;
      break;
Richard Henderson's avatar
Richard Henderson committed
1033
    case DW_FORM_sdata:
1034
      attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
Richard Henderson's avatar
Richard Henderson committed
1035 1036 1037
      info_ptr += bytes_read;
      break;
    case DW_FORM_udata:
1038
      attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
Richard Henderson's avatar
Richard Henderson committed
1039 1040 1041
      info_ptr += bytes_read;
      break;
    case DW_FORM_ref1:
1042
      attr->u.val = read_1_byte (abfd, info_ptr);
Richard Henderson's avatar
Richard Henderson committed
1043 1044 1045
      info_ptr += 1;
      break;
    case DW_FORM_ref2:
1046
      attr->u.val = read_2_bytes (abfd, info_ptr);
Richard Henderson's avatar
Richard Henderson committed
1047 1048 1049
      info_ptr += 2;
      break;
    case DW_FORM_ref4:
1050
      attr->u.val = read_4_bytes (abfd, info_ptr);
Richard Henderson's avatar
Richard Henderson committed
1051 1052
      info_ptr += 4;
      break;
1053
    case DW_FORM_ref8:
1054
      attr->u.val = read_8_bytes (abfd, info_ptr);
1055 1056
      info_ptr += 8;
      break;
1057 1058 1059 1060
    case DW_FORM_ref_sig8:
      attr->u.val = read_8_bytes (abfd, info_ptr);
      info_ptr += 8;
      break;
Richard Henderson's avatar
Richard Henderson committed
1061
    case DW_FORM_ref_udata:
1062
      attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
Richard Henderson's avatar
Richard Henderson committed
1063 1064 1065
      info_ptr += bytes_read;
      break;
    case DW_FORM_indirect:
1066 1067 1068 1069
      form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
      info_ptr += bytes_read;
      info_ptr = read_attribute_value (attr, form, unit, info_ptr);
      break;
Richard Henderson's avatar
Richard Henderson committed
1070
    default:
1071
      (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %#x."),
1072
			     form);
Richard Henderson's avatar
Richard Henderson committed
1073
      bfd_set_error (bfd_error_bad_value);
1074
      return NULL;
Richard Henderson's avatar
Richard Henderson committed
1075 1076 1077 1078
    }
  return info_ptr;
}

1079 1080
/* Read an attribute described by an abbreviated attribute.  */

Alan Modra's avatar
Alan Modra committed
1081
static bfd_byte *
1082 1083 1084
read_attribute (struct attribute *attr,
		struct attr_abbrev *abbrev,
		struct comp_unit *unit,
Alan Modra's avatar
Alan Modra committed
1085
		bfd_byte *info_ptr)
1086 1087 1088 1089 1090 1091
{
  attr->name = abbrev->name;
  info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
  return info_ptr;
}

1092
/* Source line information table routines.  */
Richard Henderson's avatar
Richard Henderson committed
1093 1094 1095 1096

#define FILE_ALLOC_CHUNK 5
#define DIR_ALLOC_CHUNK 5

1097 1098
struct line_info
{
Richard Henderson's avatar
Richard Henderson committed
1099 1100
  struct line_info* prev_line;
  bfd_vma address;
Alan Modra's avatar
Alan Modra committed
1101
  char *filename;
Richard Henderson's avatar
Richard Henderson committed
1102 1103
  unsigned int line;
  unsigned int column;
1104
  unsigned int discriminator;
Jakub Jelinek's avatar
Jakub Jelinek committed
1105 1106
  unsigned char op_index;
  unsigned char end_sequence;		/* End of (sequential) code sequence.  */
Richard Henderson's avatar
Richard Henderson committed
1107 1108
};

1109 1110
struct fileinfo
{
Richard Henderson's avatar
Richard Henderson committed
1111 1112 1113 1114 1115 1116
  char *name;
  unsigned int dir;
  unsigned int time;
  unsigned int size;
};

1117 1118 1119 1120 1121 1122 1123
struct line_sequence
{
  bfd_vma               low_pc;
  struct line_sequence* prev_sequence;
  struct line_info*     last_line;  /* Largest VMA.  */
};

1124 1125
struct line_info_table
{
1126 1127 1128 1129 1130 1131 1132 1133 1134
  bfd*                  abfd;
  unsigned int          num_files;
  unsigned int          num_dirs;
  unsigned int          num_sequences;
  char *                comp_dir;
  char **               dirs;
  struct fileinfo*      files;
  struct line_sequence* sequences;
  struct line_info*     lcl_head;   /* Local head; used in 'add_line_info'.  */
Richard Henderson's avatar
Richard Henderson committed
1135 1136
};

Fred Fish's avatar
Fred Fish committed
1137 1138 1139
/* Remember some information about each function.  If the function is
   inlined (DW_TAG_inlined_subroutine) it may have two additional
   attributes, DW_AT_call_file and DW_AT_call_line, which specify the
Alan Modra's avatar
Alan Modra committed
1140
   source code location where this function was inlined.  */
Fred Fish's avatar
Fred Fish committed
1141

1142 1143
struct funcinfo
{
Alan Modra's avatar
Alan Modra committed
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
  /* Pointer to previous function in list of all functions.  */
  struct funcinfo *prev_func;
  /* Pointer to function one scope higher.  */
  struct funcinfo *caller_func;
  /* Source location file name where caller_func inlines this func.  */
  char *caller_file;
  /* Source location line number where caller_func inlines this func.  */
  int caller_line;
  /* Source location file name.  */
  char *file;
  /* Source location line number.  */
  int line;
Fred Fish's avatar
Fred Fish committed
1156
  int tag;
Alan Modra's avatar
Alan Modra committed
1157
  char *name;
1158
  struct arange arange;
Alan Modra's avatar
Alan Modra committed
1159 1160
  /* Where the symbol is defined.  */
  asection *sec;
H.J. Lu's avatar
bfd/  
H.J. Lu committed
1161 1162 1163 1164
};

struct varinfo
{
Alan Modra's avatar
Alan Modra committed
1165
  /* Pointer to previous variable in list of all variables */
H.J. Lu's avatar
bfd/  
H.J. Lu committed
1166
  struct varinfo *prev_var;
Alan Modra's avatar
Alan Modra committed
1167
  /* Source location file name */
H.J. Lu's avatar
bfd/  
H.J. Lu committed
1168
  char *file;
Alan Modra's avatar
Alan Modra committed
1169
  /* Source location line number */
H.J. Lu's avatar
bfd/  
H.J. Lu committed
1170 1171 1172
  int line;
  int tag;
  char *name;
1173
  bfd_vma addr;
Alan Modra's avatar
Alan Modra committed
1174
  /* Where the symbol is defined */
H.J. Lu's avatar
bfd/  
H.J. Lu committed
1175
  asection *sec;
Alan Modra's avatar
Alan Modra committed
1176
  /* Is this a stack variable? */
H.J. Lu's avatar
bfd/  
H.J. Lu committed
1177
  unsigned int stack: 1;
1178 1179
};

1180 1181 1182 1183 1184 1185 1186
/* Return TRUE if NEW_LINE should sort after LINE.  */

static inline bfd_boolean
new_line_sorts_after (struct line_info *new_line, struct line_info *line)
{
  return (new_line->address > line->address
	  || (new_line->address == line->address
Jakub Jelinek's avatar
Jakub Jelinek committed
1187 1188 1189
	      && (new_line->op_index > line->op_index
		  || (new_line->op_index == line->op_index
		      && new_line->end_sequence < line->end_sequence))));
1190 1191 1192
}


1193 1194 1195 1196 1197
/* Adds a new entry to the line_info list in the line_info_table, ensuring
   that the list is sorted.  Note that the line_info list is sorted from
   highest to lowest VMA (with possible duplicates); that is,
   line_info->prev_line always accesses an equal or smaller VMA.  */

Alan Modra's avatar
Alan Modra committed
1198
static bfd_boolean
1199 1200
add_line_info (struct line_info_table *table,
	       bfd_vma address,
Jakub Jelinek's avatar
Jakub Jelinek committed
1201
	       unsigned char op_index,
1202 1203 1204
	       char *filename,
	       unsigned int line,
	       unsigned int column,
1205
	       unsigned int discriminator,
1206
	       int end_sequence)
Richard Henderson's avatar
Richard Henderson committed
1207
{
1208
  bfd_size_type amt = sizeof (struct line_info);
1209
  struct line_sequence* seq = table->sequences;
1210
  struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
Richard Henderson's avatar
Richard Henderson committed
1211

Alan Modra's avatar
Alan Modra committed
1212 1213 1214
  if (info == NULL)
    return FALSE;

1215
  /* Set member data of 'info'.  */
H.J. Lu's avatar
H.J. Lu committed
1216
  info->prev_line = NULL;
1217
  info->address = address;
Jakub Jelinek's avatar
Jakub Jelinek committed
1218
  info->op_index = op_index;
1219 1220
  info->line = line;
  info->column = column;
1221
  info->discriminator = discriminator;
1222 1223 1224 1225
  info->end_sequence = end_sequence;

  if (filename && filename[0])
    {
1226
      info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
Alan Modra's avatar
Alan Modra committed
1227 1228 1229
      if (info->filename == NULL)
	return FALSE;
      strcpy (info->filename, filename);
1230 1231 1232 1233
    }
  else
    info->filename = NULL;

Alan Modra's avatar