Home | History | Annotate | Line # | Download | only in bfd
dwarf2.c revision 1.1
      1  1.1  christos /* DWARF 2 support.
      2  1.1  christos    Copyright 1994-2013 Free Software Foundation, Inc.
      3  1.1  christos 
      4  1.1  christos    Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
      5  1.1  christos    (gavin (at) cygnus.com).
      6  1.1  christos 
      7  1.1  christos    From the dwarf2read.c header:
      8  1.1  christos    Adapted by Gary Funck (gary (at) intrepid.com), Intrepid Technology,
      9  1.1  christos    Inc.  with support from Florida State University (under contract
     10  1.1  christos    with the Ada Joint Program Office), and Silicon Graphics, Inc.
     11  1.1  christos    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
     12  1.1  christos    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
     13  1.1  christos    support in dwarfread.c
     14  1.1  christos 
     15  1.1  christos    This file is part of BFD.
     16  1.1  christos 
     17  1.1  christos    This program is free software; you can redistribute it and/or modify
     18  1.1  christos    it under the terms of the GNU General Public License as published by
     19  1.1  christos    the Free Software Foundation; either version 3 of the License, or (at
     20  1.1  christos    your option) any later version.
     21  1.1  christos 
     22  1.1  christos    This program is distributed in the hope that it will be useful, but
     23  1.1  christos    WITHOUT ANY WARRANTY; without even the implied warranty of
     24  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     25  1.1  christos    General Public License for more details.
     26  1.1  christos 
     27  1.1  christos    You should have received a copy of the GNU General Public License
     28  1.1  christos    along with this program; if not, write to the Free Software
     29  1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     30  1.1  christos    MA 02110-1301, USA.  */
     31  1.1  christos 
     32  1.1  christos #include "sysdep.h"
     33  1.1  christos #include "bfd.h"
     34  1.1  christos #include "libiberty.h"
     35  1.1  christos #include "libbfd.h"
     36  1.1  christos #include "elf-bfd.h"
     37  1.1  christos #include "dwarf2.h"
     38  1.1  christos 
     39  1.1  christos /* The data in the .debug_line statement prologue looks like this.  */
     40  1.1  christos 
     41  1.1  christos struct line_head
     42  1.1  christos {
     43  1.1  christos   bfd_vma total_length;
     44  1.1  christos   unsigned short version;
     45  1.1  christos   bfd_vma prologue_length;
     46  1.1  christos   unsigned char minimum_instruction_length;
     47  1.1  christos   unsigned char maximum_ops_per_insn;
     48  1.1  christos   unsigned char default_is_stmt;
     49  1.1  christos   int line_base;
     50  1.1  christos   unsigned char line_range;
     51  1.1  christos   unsigned char opcode_base;
     52  1.1  christos   unsigned char *standard_opcode_lengths;
     53  1.1  christos };
     54  1.1  christos 
     55  1.1  christos /* Attributes have a name and a value.  */
     56  1.1  christos 
     57  1.1  christos struct attribute
     58  1.1  christos {
     59  1.1  christos   enum dwarf_attribute name;
     60  1.1  christos   enum dwarf_form form;
     61  1.1  christos   union
     62  1.1  christos   {
     63  1.1  christos     char *str;
     64  1.1  christos     struct dwarf_block *blk;
     65  1.1  christos     bfd_uint64_t val;
     66  1.1  christos     bfd_int64_t sval;
     67  1.1  christos   }
     68  1.1  christos   u;
     69  1.1  christos };
     70  1.1  christos 
     71  1.1  christos /* Blocks are a bunch of untyped bytes.  */
     72  1.1  christos struct dwarf_block
     73  1.1  christos {
     74  1.1  christos   unsigned int size;
     75  1.1  christos   bfd_byte *data;
     76  1.1  christos };
     77  1.1  christos 
     78  1.1  christos struct adjusted_section
     79  1.1  christos {
     80  1.1  christos   asection *section;
     81  1.1  christos   bfd_vma adj_vma;
     82  1.1  christos };
     83  1.1  christos 
     84  1.1  christos struct dwarf2_debug
     85  1.1  christos {
     86  1.1  christos   /* A list of all previously read comp_units.  */
     87  1.1  christos   struct comp_unit *all_comp_units;
     88  1.1  christos 
     89  1.1  christos   /* Last comp unit in list above.  */
     90  1.1  christos   struct comp_unit *last_comp_unit;
     91  1.1  christos 
     92  1.1  christos   /* Names of the debug sections.  */
     93  1.1  christos   const struct dwarf_debug_section *debug_sections;
     94  1.1  christos 
     95  1.1  christos   /* The next unread compilation unit within the .debug_info section.
     96  1.1  christos      Zero indicates that the .debug_info section has not been loaded
     97  1.1  christos      into a buffer yet.  */
     98  1.1  christos   bfd_byte *info_ptr;
     99  1.1  christos 
    100  1.1  christos   /* Pointer to the end of the .debug_info section memory buffer.  */
    101  1.1  christos   bfd_byte *info_ptr_end;
    102  1.1  christos 
    103  1.1  christos   /* Pointer to the bfd, section and address of the beginning of the
    104  1.1  christos      section.  The bfd might be different than expected because of
    105  1.1  christos      gnu_debuglink sections.  */
    106  1.1  christos   bfd *bfd_ptr;
    107  1.1  christos   asection *sec;
    108  1.1  christos   bfd_byte *sec_info_ptr;
    109  1.1  christos 
    110  1.1  christos   /* Support for alternate debug info sections created by the DWZ utility:
    111  1.1  christos      This includes a pointer to an alternate bfd which contains *extra*,
    112  1.1  christos      possibly duplicate debug sections, and pointers to the loaded
    113  1.1  christos      .debug_str and .debug_info sections from this bfd.  */
    114  1.1  christos   bfd *          alt_bfd_ptr;
    115  1.1  christos   bfd_byte *     alt_dwarf_str_buffer;
    116  1.1  christos   bfd_size_type  alt_dwarf_str_size;
    117  1.1  christos   bfd_byte *     alt_dwarf_info_buffer;
    118  1.1  christos   bfd_size_type  alt_dwarf_info_size;
    119  1.1  christos 
    120  1.1  christos   /* A pointer to the memory block allocated for info_ptr.  Neither
    121  1.1  christos      info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
    122  1.1  christos      beginning of the malloc block.  This is used only to free the
    123  1.1  christos      memory later.  */
    124  1.1  christos   bfd_byte *info_ptr_memory;
    125  1.1  christos 
    126  1.1  christos   /* Pointer to the symbol table.  */
    127  1.1  christos   asymbol **syms;
    128  1.1  christos 
    129  1.1  christos   /* Pointer to the .debug_abbrev section loaded into memory.  */
    130  1.1  christos   bfd_byte *dwarf_abbrev_buffer;
    131  1.1  christos 
    132  1.1  christos   /* Length of the loaded .debug_abbrev section.  */
    133  1.1  christos   bfd_size_type dwarf_abbrev_size;
    134  1.1  christos 
    135  1.1  christos   /* Buffer for decode_line_info.  */
    136  1.1  christos   bfd_byte *dwarf_line_buffer;
    137  1.1  christos 
    138  1.1  christos   /* Length of the loaded .debug_line section.  */
    139  1.1  christos   bfd_size_type dwarf_line_size;
    140  1.1  christos 
    141  1.1  christos   /* Pointer to the .debug_str section loaded into memory.  */
    142  1.1  christos   bfd_byte *dwarf_str_buffer;
    143  1.1  christos 
    144  1.1  christos   /* Length of the loaded .debug_str section.  */
    145  1.1  christos   bfd_size_type dwarf_str_size;
    146  1.1  christos 
    147  1.1  christos   /* Pointer to the .debug_ranges section loaded into memory. */
    148  1.1  christos   bfd_byte *dwarf_ranges_buffer;
    149  1.1  christos 
    150  1.1  christos   /* Length of the loaded .debug_ranges section. */
    151  1.1  christos   bfd_size_type dwarf_ranges_size;
    152  1.1  christos 
    153  1.1  christos   /* If the most recent call to bfd_find_nearest_line was given an
    154  1.1  christos      address in an inlined function, preserve a pointer into the
    155  1.1  christos      calling chain for subsequent calls to bfd_find_inliner_info to
    156  1.1  christos      use. */
    157  1.1  christos   struct funcinfo *inliner_chain;
    158  1.1  christos 
    159  1.1  christos   /* Number of sections whose VMA we must adjust.  */
    160  1.1  christos   unsigned int adjusted_section_count;
    161  1.1  christos 
    162  1.1  christos   /* Array of sections with adjusted VMA.  */
    163  1.1  christos   struct adjusted_section *adjusted_sections;
    164  1.1  christos 
    165  1.1  christos   /* Number of times find_line is called.  This is used in
    166  1.1  christos      the heuristic for enabling the info hash tables.  */
    167  1.1  christos   int info_hash_count;
    168  1.1  christos 
    169  1.1  christos #define STASH_INFO_HASH_TRIGGER    100
    170  1.1  christos 
    171  1.1  christos   /* Hash table mapping symbol names to function infos.  */
    172  1.1  christos   struct info_hash_table *funcinfo_hash_table;
    173  1.1  christos 
    174  1.1  christos   /* Hash table mapping symbol names to variable infos.  */
    175  1.1  christos   struct info_hash_table *varinfo_hash_table;
    176  1.1  christos 
    177  1.1  christos   /* Head of comp_unit list in the last hash table update.  */
    178  1.1  christos   struct comp_unit *hash_units_head;
    179  1.1  christos 
    180  1.1  christos   /* Status of info hash.  */
    181  1.1  christos   int info_hash_status;
    182  1.1  christos #define STASH_INFO_HASH_OFF        0
    183  1.1  christos #define STASH_INFO_HASH_ON         1
    184  1.1  christos #define STASH_INFO_HASH_DISABLED   2
    185  1.1  christos 
    186  1.1  christos   /* True if we opened bfd_ptr.  */
    187  1.1  christos   bfd_boolean close_on_cleanup;
    188  1.1  christos };
    189  1.1  christos 
    190  1.1  christos struct arange
    191  1.1  christos {
    192  1.1  christos   struct arange *next;
    193  1.1  christos   bfd_vma low;
    194  1.1  christos   bfd_vma high;
    195  1.1  christos };
    196  1.1  christos 
    197  1.1  christos /* A minimal decoding of DWARF2 compilation units.  We only decode
    198  1.1  christos    what's needed to get to the line number information.  */
    199  1.1  christos 
    200  1.1  christos struct comp_unit
    201  1.1  christos {
    202  1.1  christos   /* Chain the previously read compilation units.  */
    203  1.1  christos   struct comp_unit *next_unit;
    204  1.1  christos 
    205  1.1  christos   /* Likewise, chain the compilation unit read after this one.
    206  1.1  christos      The comp units are stored in reversed reading order.  */
    207  1.1  christos   struct comp_unit *prev_unit;
    208  1.1  christos 
    209  1.1  christos   /* Keep the bfd convenient (for memory allocation).  */
    210  1.1  christos   bfd *abfd;
    211  1.1  christos 
    212  1.1  christos   /* The lowest and highest addresses contained in this compilation
    213  1.1  christos      unit as specified in the compilation unit header.  */
    214  1.1  christos   struct arange arange;
    215  1.1  christos 
    216  1.1  christos   /* The DW_AT_name attribute (for error messages).  */
    217  1.1  christos   char *name;
    218  1.1  christos 
    219  1.1  christos   /* The abbrev hash table.  */
    220  1.1  christos   struct abbrev_info **abbrevs;
    221  1.1  christos 
    222  1.1  christos   /* Note that an error was found by comp_unit_find_nearest_line.  */
    223  1.1  christos   int error;
    224  1.1  christos 
    225  1.1  christos   /* The DW_AT_comp_dir attribute.  */
    226  1.1  christos   char *comp_dir;
    227  1.1  christos 
    228  1.1  christos   /* TRUE if there is a line number table associated with this comp. unit.  */
    229  1.1  christos   int stmtlist;
    230  1.1  christos 
    231  1.1  christos   /* Pointer to the current comp_unit so that we can find a given entry
    232  1.1  christos      by its reference.  */
    233  1.1  christos   bfd_byte *info_ptr_unit;
    234  1.1  christos 
    235  1.1  christos   /* Pointer to the start of the debug section, for DW_FORM_ref_addr.  */
    236  1.1  christos   bfd_byte *sec_info_ptr;
    237  1.1  christos 
    238  1.1  christos   /* The offset into .debug_line of the line number table.  */
    239  1.1  christos   unsigned long line_offset;
    240  1.1  christos 
    241  1.1  christos   /* Pointer to the first child die for the comp unit.  */
    242  1.1  christos   bfd_byte *first_child_die_ptr;
    243  1.1  christos 
    244  1.1  christos   /* The end of the comp unit.  */
    245  1.1  christos   bfd_byte *end_ptr;
    246  1.1  christos 
    247  1.1  christos   /* The decoded line number, NULL if not yet decoded.  */
    248  1.1  christos   struct line_info_table *line_table;
    249  1.1  christos 
    250  1.1  christos   /* A list of the functions found in this comp. unit.  */
    251  1.1  christos   struct funcinfo *function_table;
    252  1.1  christos 
    253  1.1  christos   /* A list of the variables found in this comp. unit.  */
    254  1.1  christos   struct varinfo *variable_table;
    255  1.1  christos 
    256  1.1  christos   /* Pointer to dwarf2_debug structure.  */
    257  1.1  christos   struct dwarf2_debug *stash;
    258  1.1  christos 
    259  1.1  christos   /* DWARF format version for this unit - from unit header.  */
    260  1.1  christos   int version;
    261  1.1  christos 
    262  1.1  christos   /* Address size for this unit - from unit header.  */
    263  1.1  christos   unsigned char addr_size;
    264  1.1  christos 
    265  1.1  christos   /* Offset size for this unit - from unit header.  */
    266  1.1  christos   unsigned char offset_size;
    267  1.1  christos 
    268  1.1  christos   /* Base address for this unit - from DW_AT_low_pc attribute of
    269  1.1  christos      DW_TAG_compile_unit DIE */
    270  1.1  christos   bfd_vma base_address;
    271  1.1  christos 
    272  1.1  christos   /* TRUE if symbols are cached in hash table for faster lookup by name.  */
    273  1.1  christos   bfd_boolean cached;
    274  1.1  christos };
    275  1.1  christos 
    276  1.1  christos /* This data structure holds the information of an abbrev.  */
    277  1.1  christos struct abbrev_info
    278  1.1  christos {
    279  1.1  christos   unsigned int number;		/* Number identifying abbrev.  */
    280  1.1  christos   enum dwarf_tag tag;		/* DWARF tag.  */
    281  1.1  christos   int has_children;		/* Boolean.  */
    282  1.1  christos   unsigned int num_attrs;	/* Number of attributes.  */
    283  1.1  christos   struct attr_abbrev *attrs;	/* An array of attribute descriptions.  */
    284  1.1  christos   struct abbrev_info *next;	/* Next in chain.  */
    285  1.1  christos };
    286  1.1  christos 
    287  1.1  christos struct attr_abbrev
    288  1.1  christos {
    289  1.1  christos   enum dwarf_attribute name;
    290  1.1  christos   enum dwarf_form form;
    291  1.1  christos };
    292  1.1  christos 
    293  1.1  christos /* Map of uncompressed DWARF debug section name to compressed one.  It
    294  1.1  christos    is terminated by NULL uncompressed_name.  */
    295  1.1  christos 
    296  1.1  christos const struct dwarf_debug_section dwarf_debug_sections[] =
    297  1.1  christos {
    298  1.1  christos   { ".debug_abbrev",		".zdebug_abbrev" },
    299  1.1  christos   { ".debug_aranges",		".zdebug_aranges" },
    300  1.1  christos   { ".debug_frame",		".zdebug_frame" },
    301  1.1  christos   { ".debug_info",		".zdebug_info" },
    302  1.1  christos   { ".debug_info",		".zdebug_info" },
    303  1.1  christos   { ".debug_line",		".zdebug_line" },
    304  1.1  christos   { ".debug_loc",		".zdebug_loc" },
    305  1.1  christos   { ".debug_macinfo",		".zdebug_macinfo" },
    306  1.1  christos   { ".debug_macro",		".zdebug_macro" },
    307  1.1  christos   { ".debug_pubnames",		".zdebug_pubnames" },
    308  1.1  christos   { ".debug_pubtypes",		".zdebug_pubtypes" },
    309  1.1  christos   { ".debug_ranges",		".zdebug_ranges" },
    310  1.1  christos   { ".debug_static_func",	".zdebug_static_func" },
    311  1.1  christos   { ".debug_static_vars",	".zdebug_static_vars" },
    312  1.1  christos   { ".debug_str",		".zdebug_str", },
    313  1.1  christos   { ".debug_str",		".zdebug_str", },
    314  1.1  christos   { ".debug_types",		".zdebug_types" },
    315  1.1  christos   /* GNU DWARF 1 extensions */
    316  1.1  christos   { ".debug_sfnames",		".zdebug_sfnames" },
    317  1.1  christos   { ".debug_srcinfo",		".zebug_srcinfo" },
    318  1.1  christos   /* SGI/MIPS DWARF 2 extensions */
    319  1.1  christos   { ".debug_funcnames",		".zdebug_funcnames" },
    320  1.1  christos   { ".debug_typenames",		".zdebug_typenames" },
    321  1.1  christos   { ".debug_varnames",		".zdebug_varnames" },
    322  1.1  christos   { ".debug_weaknames",		".zdebug_weaknames" },
    323  1.1  christos   { NULL,			NULL },
    324  1.1  christos };
    325  1.1  christos 
    326  1.1  christos /* NB/ Numbers in this enum must match up with indicies
    327  1.1  christos    into the dwarf_debug_sections[] array above.  */
    328  1.1  christos enum dwarf_debug_section_enum
    329  1.1  christos {
    330  1.1  christos   debug_abbrev = 0,
    331  1.1  christos   debug_aranges,
    332  1.1  christos   debug_frame,
    333  1.1  christos   debug_info,
    334  1.1  christos   debug_info_alt,
    335  1.1  christos   debug_line,
    336  1.1  christos   debug_loc,
    337  1.1  christos   debug_macinfo,
    338  1.1  christos   debug_macro,
    339  1.1  christos   debug_pubnames,
    340  1.1  christos   debug_pubtypes,
    341  1.1  christos   debug_ranges,
    342  1.1  christos   debug_static_func,
    343  1.1  christos   debug_static_vars,
    344  1.1  christos   debug_str,
    345  1.1  christos   debug_str_alt,
    346  1.1  christos   debug_types,
    347  1.1  christos   debug_sfnames,
    348  1.1  christos   debug_srcinfo,
    349  1.1  christos   debug_funcnames,
    350  1.1  christos   debug_typenames,
    351  1.1  christos   debug_varnames,
    352  1.1  christos   debug_weaknames
    353  1.1  christos };
    354  1.1  christos 
    355  1.1  christos #ifndef ABBREV_HASH_SIZE
    356  1.1  christos #define ABBREV_HASH_SIZE 121
    357  1.1  christos #endif
    358  1.1  christos #ifndef ATTR_ALLOC_CHUNK
    359  1.1  christos #define ATTR_ALLOC_CHUNK 4
    360  1.1  christos #endif
    361  1.1  christos 
    362  1.1  christos /* Variable and function hash tables.  This is used to speed up look-up
    363  1.1  christos    in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
    364  1.1  christos    In order to share code between variable and function infos, we use
    365  1.1  christos    a list of untyped pointer for all variable/function info associated with
    366  1.1  christos    a symbol.  We waste a bit of memory for list with one node but that
    367  1.1  christos    simplifies the code.  */
    368  1.1  christos 
    369  1.1  christos struct info_list_node
    370  1.1  christos {
    371  1.1  christos   struct info_list_node *next;
    372  1.1  christos   void *info;
    373  1.1  christos };
    374  1.1  christos 
    375  1.1  christos /* Info hash entry.  */
    376  1.1  christos struct info_hash_entry
    377  1.1  christos {
    378  1.1  christos   struct bfd_hash_entry root;
    379  1.1  christos   struct info_list_node *head;
    380  1.1  christos };
    381  1.1  christos 
    382  1.1  christos struct info_hash_table
    383  1.1  christos {
    384  1.1  christos   struct bfd_hash_table base;
    385  1.1  christos };
    386  1.1  christos 
    387  1.1  christos /* Function to create a new entry in info hash table. */
    388  1.1  christos 
    389  1.1  christos static struct bfd_hash_entry *
    390  1.1  christos info_hash_table_newfunc (struct bfd_hash_entry *entry,
    391  1.1  christos 			 struct bfd_hash_table *table,
    392  1.1  christos 			 const char *string)
    393  1.1  christos {
    394  1.1  christos   struct info_hash_entry *ret = (struct info_hash_entry *) entry;
    395  1.1  christos 
    396  1.1  christos   /* Allocate the structure if it has not already been allocated by a
    397  1.1  christos      derived class.  */
    398  1.1  christos   if (ret == NULL)
    399  1.1  christos     {
    400  1.1  christos       ret = (struct info_hash_entry *) bfd_hash_allocate (table,
    401  1.1  christos                                                           sizeof (* ret));
    402  1.1  christos       if (ret == NULL)
    403  1.1  christos 	return NULL;
    404  1.1  christos     }
    405  1.1  christos 
    406  1.1  christos   /* Call the allocation method of the base class.  */
    407  1.1  christos   ret = ((struct info_hash_entry *)
    408  1.1  christos 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
    409  1.1  christos 
    410  1.1  christos   /* Initialize the local fields here.  */
    411  1.1  christos   if (ret)
    412  1.1  christos     ret->head = NULL;
    413  1.1  christos 
    414  1.1  christos   return (struct bfd_hash_entry *) ret;
    415  1.1  christos }
    416  1.1  christos 
    417  1.1  christos /* Function to create a new info hash table.  It returns a pointer to the
    418  1.1  christos    newly created table or NULL if there is any error.  We need abfd
    419  1.1  christos    solely for memory allocation.  */
    420  1.1  christos 
    421  1.1  christos static struct info_hash_table *
    422  1.1  christos create_info_hash_table (bfd *abfd)
    423  1.1  christos {
    424  1.1  christos   struct info_hash_table *hash_table;
    425  1.1  christos 
    426  1.1  christos   hash_table = ((struct info_hash_table *)
    427  1.1  christos 		bfd_alloc (abfd, sizeof (struct info_hash_table)));
    428  1.1  christos   if (!hash_table)
    429  1.1  christos     return hash_table;
    430  1.1  christos 
    431  1.1  christos   if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
    432  1.1  christos 			    sizeof (struct info_hash_entry)))
    433  1.1  christos     {
    434  1.1  christos       bfd_release (abfd, hash_table);
    435  1.1  christos       return NULL;
    436  1.1  christos     }
    437  1.1  christos 
    438  1.1  christos   return hash_table;
    439  1.1  christos }
    440  1.1  christos 
    441  1.1  christos /* Insert an info entry into an info hash table.  We do not check of
    442  1.1  christos    duplicate entries.  Also, the caller need to guarantee that the
    443  1.1  christos    right type of info in inserted as info is passed as a void* pointer.
    444  1.1  christos    This function returns true if there is no error.  */
    445  1.1  christos 
    446  1.1  christos static bfd_boolean
    447  1.1  christos insert_info_hash_table (struct info_hash_table *hash_table,
    448  1.1  christos 			const char *key,
    449  1.1  christos 			void *info,
    450  1.1  christos 			bfd_boolean copy_p)
    451  1.1  christos {
    452  1.1  christos   struct info_hash_entry *entry;
    453  1.1  christos   struct info_list_node *node;
    454  1.1  christos 
    455  1.1  christos   entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
    456  1.1  christos 						     key, TRUE, copy_p);
    457  1.1  christos   if (!entry)
    458  1.1  christos     return FALSE;
    459  1.1  christos 
    460  1.1  christos   node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
    461  1.1  christos                                                       sizeof (*node));
    462  1.1  christos   if (!node)
    463  1.1  christos     return FALSE;
    464  1.1  christos 
    465  1.1  christos   node->info = info;
    466  1.1  christos   node->next = entry->head;
    467  1.1  christos   entry->head = node;
    468  1.1  christos 
    469  1.1  christos   return TRUE;
    470  1.1  christos }
    471  1.1  christos 
    472  1.1  christos /* Look up an info entry list from an info hash table.  Return NULL
    473  1.1  christos    if there is none. */
    474  1.1  christos 
    475  1.1  christos static struct info_list_node *
    476  1.1  christos lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
    477  1.1  christos {
    478  1.1  christos   struct info_hash_entry *entry;
    479  1.1  christos 
    480  1.1  christos   entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
    481  1.1  christos 						     FALSE, FALSE);
    482  1.1  christos   return entry ? entry->head : NULL;
    483  1.1  christos }
    484  1.1  christos 
    485  1.1  christos /* Read a section into its appropriate place in the dwarf2_debug
    486  1.1  christos    struct (indicated by SECTION_BUFFER and SECTION_SIZE).  If SYMS is
    487  1.1  christos    not NULL, use bfd_simple_get_relocated_section_contents to read the
    488  1.1  christos    section contents, otherwise use bfd_get_section_contents.  Fail if
    489  1.1  christos    the located section does not contain at least OFFSET bytes.  */
    490  1.1  christos 
    491  1.1  christos static bfd_boolean
    492  1.1  christos read_section (bfd *           abfd,
    493  1.1  christos 	      const struct dwarf_debug_section *sec,
    494  1.1  christos 	      asymbol **      syms,
    495  1.1  christos 	      bfd_uint64_t    offset,
    496  1.1  christos 	      bfd_byte **     section_buffer,
    497  1.1  christos 	      bfd_size_type * section_size)
    498  1.1  christos {
    499  1.1  christos   asection *msec;
    500  1.1  christos   const char *section_name = sec->uncompressed_name;
    501  1.1  christos 
    502  1.1  christos   /* The section may have already been read.  */
    503  1.1  christos   if (*section_buffer == NULL)
    504  1.1  christos     {
    505  1.1  christos       msec = bfd_get_section_by_name (abfd, section_name);
    506  1.1  christos       if (! msec)
    507  1.1  christos 	{
    508  1.1  christos 	  section_name = sec->compressed_name;
    509  1.1  christos           if (section_name != NULL)
    510  1.1  christos             msec = bfd_get_section_by_name (abfd, section_name);
    511  1.1  christos 	}
    512  1.1  christos       if (! msec)
    513  1.1  christos 	{
    514  1.1  christos 	  (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."),
    515  1.1  christos                                  sec->uncompressed_name);
    516  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
    517  1.1  christos 	  return FALSE;
    518  1.1  christos 	}
    519  1.1  christos 
    520  1.1  christos       *section_size = msec->rawsize ? msec->rawsize : msec->size;
    521  1.1  christos       if (syms)
    522  1.1  christos 	{
    523  1.1  christos 	  *section_buffer
    524  1.1  christos 	    = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
    525  1.1  christos 	  if (! *section_buffer)
    526  1.1  christos 	    return FALSE;
    527  1.1  christos 	}
    528  1.1  christos       else
    529  1.1  christos 	{
    530  1.1  christos 	  *section_buffer = (bfd_byte *) bfd_malloc (*section_size);
    531  1.1  christos 	  if (! *section_buffer)
    532  1.1  christos 	    return FALSE;
    533  1.1  christos 	  if (! bfd_get_section_contents (abfd, msec, *section_buffer,
    534  1.1  christos 					  0, *section_size))
    535  1.1  christos 	    return FALSE;
    536  1.1  christos 	}
    537  1.1  christos     }
    538  1.1  christos 
    539  1.1  christos   /* It is possible to get a bad value for the offset into the section
    540  1.1  christos      that the client wants.  Validate it here to avoid trouble later.  */
    541  1.1  christos   if (offset != 0 && offset >= *section_size)
    542  1.1  christos     {
    543  1.1  christos       (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu)"
    544  1.1  christos 			       " greater than or equal to %s size (%lu)."),
    545  1.1  christos 			     (long) offset, section_name, *section_size);
    546  1.1  christos       bfd_set_error (bfd_error_bad_value);
    547  1.1  christos       return FALSE;
    548  1.1  christos     }
    549  1.1  christos 
    550  1.1  christos   return TRUE;
    551  1.1  christos }
    552  1.1  christos 
    553  1.1  christos /* VERBATIM
    554  1.1  christos    The following function up to the END VERBATIM mark are
    555  1.1  christos    copied directly from dwarf2read.c.  */
    556  1.1  christos 
    557  1.1  christos /* Read dwarf information from a buffer.  */
    558  1.1  christos 
    559  1.1  christos static unsigned int
    560  1.1  christos read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
    561  1.1  christos {
    562  1.1  christos   return bfd_get_8 (abfd, buf);
    563  1.1  christos }
    564  1.1  christos 
    565  1.1  christos static int
    566  1.1  christos read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
    567  1.1  christos {
    568  1.1  christos   return bfd_get_signed_8 (abfd, buf);
    569  1.1  christos }
    570  1.1  christos 
    571  1.1  christos static unsigned int
    572  1.1  christos read_2_bytes (bfd *abfd, bfd_byte *buf)
    573  1.1  christos {
    574  1.1  christos   return bfd_get_16 (abfd, buf);
    575  1.1  christos }
    576  1.1  christos 
    577  1.1  christos static unsigned int
    578  1.1  christos read_4_bytes (bfd *abfd, bfd_byte *buf)
    579  1.1  christos {
    580  1.1  christos   return bfd_get_32 (abfd, buf);
    581  1.1  christos }
    582  1.1  christos 
    583  1.1  christos static bfd_uint64_t
    584  1.1  christos read_8_bytes (bfd *abfd, bfd_byte *buf)
    585  1.1  christos {
    586  1.1  christos   return bfd_get_64 (abfd, buf);
    587  1.1  christos }
    588  1.1  christos 
    589  1.1  christos static bfd_byte *
    590  1.1  christos read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
    591  1.1  christos 	      bfd_byte *buf,
    592  1.1  christos 	      unsigned int size ATTRIBUTE_UNUSED)
    593  1.1  christos {
    594  1.1  christos   return buf;
    595  1.1  christos }
    596  1.1  christos 
    597  1.1  christos static char *
    598  1.1  christos read_string (bfd *abfd ATTRIBUTE_UNUSED,
    599  1.1  christos 	     bfd_byte *buf,
    600  1.1  christos 	     unsigned int *bytes_read_ptr)
    601  1.1  christos {
    602  1.1  christos   /* Return a pointer to the embedded string.  */
    603  1.1  christos   char *str = (char *) buf;
    604  1.1  christos 
    605  1.1  christos   if (*str == '\0')
    606  1.1  christos     {
    607  1.1  christos       *bytes_read_ptr = 1;
    608  1.1  christos       return NULL;
    609  1.1  christos     }
    610  1.1  christos 
    611  1.1  christos   *bytes_read_ptr = strlen (str) + 1;
    612  1.1  christos   return str;
    613  1.1  christos }
    614  1.1  christos 
    615  1.1  christos /* END VERBATIM */
    616  1.1  christos 
    617  1.1  christos static char *
    618  1.1  christos read_indirect_string (struct comp_unit * unit,
    619  1.1  christos 		      bfd_byte *         buf,
    620  1.1  christos 		      unsigned int *     bytes_read_ptr)
    621  1.1  christos {
    622  1.1  christos   bfd_uint64_t offset;
    623  1.1  christos   struct dwarf2_debug *stash = unit->stash;
    624  1.1  christos   char *str;
    625  1.1  christos 
    626  1.1  christos   if (unit->offset_size == 4)
    627  1.1  christos     offset = read_4_bytes (unit->abfd, buf);
    628  1.1  christos   else
    629  1.1  christos     offset = read_8_bytes (unit->abfd, buf);
    630  1.1  christos 
    631  1.1  christos   *bytes_read_ptr = unit->offset_size;
    632  1.1  christos 
    633  1.1  christos   if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
    634  1.1  christos                       stash->syms, offset,
    635  1.1  christos 		      &stash->dwarf_str_buffer, &stash->dwarf_str_size))
    636  1.1  christos     return NULL;
    637  1.1  christos 
    638  1.1  christos   str = (char *) stash->dwarf_str_buffer + offset;
    639  1.1  christos   if (*str == '\0')
    640  1.1  christos     return NULL;
    641  1.1  christos   return str;
    642  1.1  christos }
    643  1.1  christos 
    644  1.1  christos /* Like read_indirect_string but uses a .debug_str located in
    645  1.1  christos    an alternate filepointed to by the .gnu_debuglink section.
    646  1.1  christos    Used to impement DW_FORM_GNU_strp_alt.  */
    647  1.1  christos 
    648  1.1  christos static char *
    649  1.1  christos read_alt_indirect_string (struct comp_unit * unit,
    650  1.1  christos 			  bfd_byte *         buf,
    651  1.1  christos 			  unsigned int *     bytes_read_ptr)
    652  1.1  christos {
    653  1.1  christos   bfd_uint64_t offset;
    654  1.1  christos   struct dwarf2_debug *stash = unit->stash;
    655  1.1  christos   char *str;
    656  1.1  christos 
    657  1.1  christos   if (unit->offset_size == 4)
    658  1.1  christos     offset = read_4_bytes (unit->abfd, buf);
    659  1.1  christos   else
    660  1.1  christos     offset = read_8_bytes (unit->abfd, buf);
    661  1.1  christos 
    662  1.1  christos   *bytes_read_ptr = unit->offset_size;
    663  1.1  christos 
    664  1.1  christos   if (stash->alt_bfd_ptr == NULL)
    665  1.1  christos     {
    666  1.1  christos       bfd *  debug_bfd;
    667  1.1  christos       char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
    668  1.1  christos 
    669  1.1  christos       if (debug_filename == NULL)
    670  1.1  christos 	return NULL;
    671  1.1  christos 
    672  1.1  christos       if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
    673  1.1  christos 	  || ! bfd_check_format (debug_bfd, bfd_object))
    674  1.1  christos 	{
    675  1.1  christos 	  if (debug_bfd)
    676  1.1  christos 	    bfd_close (debug_bfd);
    677  1.1  christos 
    678  1.1  christos 	  /* FIXME: Should we report our failure to follow the debuglink ?  */
    679  1.1  christos 	  free (debug_filename);
    680  1.1  christos 	  return NULL;
    681  1.1  christos 	}
    682  1.1  christos       stash->alt_bfd_ptr = debug_bfd;
    683  1.1  christos     }
    684  1.1  christos 
    685  1.1  christos   if (! read_section (unit->stash->alt_bfd_ptr,
    686  1.1  christos 		      stash->debug_sections + debug_str_alt,
    687  1.1  christos 		      NULL, /* FIXME: Do we need to load alternate symbols ?  */
    688  1.1  christos 		      offset,
    689  1.1  christos 		      &stash->alt_dwarf_str_buffer,
    690  1.1  christos 		      &stash->alt_dwarf_str_size))
    691  1.1  christos     return NULL;
    692  1.1  christos 
    693  1.1  christos   str = (char *) stash->alt_dwarf_str_buffer + offset;
    694  1.1  christos   if (*str == '\0')
    695  1.1  christos     return NULL;
    696  1.1  christos 
    697  1.1  christos   return str;
    698  1.1  christos }
    699  1.1  christos 
    700  1.1  christos /* Resolve an alternate reference from UNIT at OFFSET.
    701  1.1  christos    Returns a pointer into the loaded alternate CU upon success
    702  1.1  christos    or NULL upon failure.  */
    703  1.1  christos 
    704  1.1  christos static bfd_byte *
    705  1.1  christos read_alt_indirect_ref (struct comp_unit * unit,
    706  1.1  christos 		       bfd_uint64_t       offset)
    707  1.1  christos {
    708  1.1  christos   struct dwarf2_debug *stash = unit->stash;
    709  1.1  christos 
    710  1.1  christos   if (stash->alt_bfd_ptr == NULL)
    711  1.1  christos     {
    712  1.1  christos       bfd *  debug_bfd;
    713  1.1  christos       char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
    714  1.1  christos 
    715  1.1  christos       if (debug_filename == NULL)
    716  1.1  christos 	return FALSE;
    717  1.1  christos 
    718  1.1  christos       if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
    719  1.1  christos 	  || ! bfd_check_format (debug_bfd, bfd_object))
    720  1.1  christos 	{
    721  1.1  christos 	  if (debug_bfd)
    722  1.1  christos 	    bfd_close (debug_bfd);
    723  1.1  christos 
    724  1.1  christos 	  /* FIXME: Should we report our failure to follow the debuglink ?  */
    725  1.1  christos 	  free (debug_filename);
    726  1.1  christos 	  return NULL;
    727  1.1  christos 	}
    728  1.1  christos       stash->alt_bfd_ptr = debug_bfd;
    729  1.1  christos     }
    730  1.1  christos 
    731  1.1  christos   if (! read_section (unit->stash->alt_bfd_ptr,
    732  1.1  christos 		      stash->debug_sections + debug_info_alt,
    733  1.1  christos 		      NULL, /* FIXME: Do we need to load alternate symbols ?  */
    734  1.1  christos 		      offset,
    735  1.1  christos 		      &stash->alt_dwarf_info_buffer,
    736  1.1  christos 		      &stash->alt_dwarf_info_size))
    737  1.1  christos     return NULL;
    738  1.1  christos 
    739  1.1  christos   return stash->alt_dwarf_info_buffer + offset;
    740  1.1  christos }
    741  1.1  christos 
    742  1.1  christos static bfd_uint64_t
    743  1.1  christos read_address (struct comp_unit *unit, bfd_byte *buf)
    744  1.1  christos {
    745  1.1  christos   int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
    746  1.1  christos 
    747  1.1  christos   if (signed_vma)
    748  1.1  christos     {
    749  1.1  christos       switch (unit->addr_size)
    750  1.1  christos 	{
    751  1.1  christos 	case 8:
    752  1.1  christos 	  return bfd_get_signed_64 (unit->abfd, buf);
    753  1.1  christos 	case 4:
    754  1.1  christos 	  return bfd_get_signed_32 (unit->abfd, buf);
    755  1.1  christos 	case 2:
    756  1.1  christos 	  return bfd_get_signed_16 (unit->abfd, buf);
    757  1.1  christos 	default:
    758  1.1  christos 	  abort ();
    759  1.1  christos 	}
    760  1.1  christos     }
    761  1.1  christos   else
    762  1.1  christos     {
    763  1.1  christos       switch (unit->addr_size)
    764  1.1  christos 	{
    765  1.1  christos 	case 8:
    766  1.1  christos 	  return bfd_get_64 (unit->abfd, buf);
    767  1.1  christos 	case 4:
    768  1.1  christos 	  return bfd_get_32 (unit->abfd, buf);
    769  1.1  christos 	case 2:
    770  1.1  christos 	  return bfd_get_16 (unit->abfd, buf);
    771  1.1  christos 	default:
    772  1.1  christos 	  abort ();
    773  1.1  christos 	}
    774  1.1  christos     }
    775  1.1  christos }
    776  1.1  christos 
    777  1.1  christos /* Lookup an abbrev_info structure in the abbrev hash table.  */
    778  1.1  christos 
    779  1.1  christos static struct abbrev_info *
    780  1.1  christos lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
    781  1.1  christos {
    782  1.1  christos   unsigned int hash_number;
    783  1.1  christos   struct abbrev_info *abbrev;
    784  1.1  christos 
    785  1.1  christos   hash_number = number % ABBREV_HASH_SIZE;
    786  1.1  christos   abbrev = abbrevs[hash_number];
    787  1.1  christos 
    788  1.1  christos   while (abbrev)
    789  1.1  christos     {
    790  1.1  christos       if (abbrev->number == number)
    791  1.1  christos 	return abbrev;
    792  1.1  christos       else
    793  1.1  christos 	abbrev = abbrev->next;
    794  1.1  christos     }
    795  1.1  christos 
    796  1.1  christos   return NULL;
    797  1.1  christos }
    798  1.1  christos 
    799  1.1  christos /* In DWARF version 2, the description of the debugging information is
    800  1.1  christos    stored in a separate .debug_abbrev section.  Before we read any
    801  1.1  christos    dies from a section we read in all abbreviations and install them
    802  1.1  christos    in a hash table.  */
    803  1.1  christos 
    804  1.1  christos static struct abbrev_info**
    805  1.1  christos read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
    806  1.1  christos {
    807  1.1  christos   struct abbrev_info **abbrevs;
    808  1.1  christos   bfd_byte *abbrev_ptr;
    809  1.1  christos   struct abbrev_info *cur_abbrev;
    810  1.1  christos   unsigned int abbrev_number, bytes_read, abbrev_name;
    811  1.1  christos   unsigned int abbrev_form, hash_number;
    812  1.1  christos   bfd_size_type amt;
    813  1.1  christos 
    814  1.1  christos   if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
    815  1.1  christos                       stash->syms, offset,
    816  1.1  christos 		      &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
    817  1.1  christos     return NULL;
    818  1.1  christos 
    819  1.1  christos   amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
    820  1.1  christos   abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
    821  1.1  christos   if (abbrevs == NULL)
    822  1.1  christos     return NULL;
    823  1.1  christos 
    824  1.1  christos   abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
    825  1.1  christos   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
    826  1.1  christos   abbrev_ptr += bytes_read;
    827  1.1  christos 
    828  1.1  christos   /* Loop until we reach an abbrev number of 0.  */
    829  1.1  christos   while (abbrev_number)
    830  1.1  christos     {
    831  1.1  christos       amt = sizeof (struct abbrev_info);
    832  1.1  christos       cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
    833  1.1  christos       if (cur_abbrev == NULL)
    834  1.1  christos 	return NULL;
    835  1.1  christos 
    836  1.1  christos       /* Read in abbrev header.  */
    837  1.1  christos       cur_abbrev->number = abbrev_number;
    838  1.1  christos       cur_abbrev->tag = (enum dwarf_tag)
    839  1.1  christos 	read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
    840  1.1  christos       abbrev_ptr += bytes_read;
    841  1.1  christos       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
    842  1.1  christos       abbrev_ptr += 1;
    843  1.1  christos 
    844  1.1  christos       /* Now read in declarations.  */
    845  1.1  christos       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
    846  1.1  christos       abbrev_ptr += bytes_read;
    847  1.1  christos       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
    848  1.1  christos       abbrev_ptr += bytes_read;
    849  1.1  christos 
    850  1.1  christos       while (abbrev_name)
    851  1.1  christos 	{
    852  1.1  christos 	  if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
    853  1.1  christos 	    {
    854  1.1  christos 	      struct attr_abbrev *tmp;
    855  1.1  christos 
    856  1.1  christos 	      amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
    857  1.1  christos 	      amt *= sizeof (struct attr_abbrev);
    858  1.1  christos 	      tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
    859  1.1  christos 	      if (tmp == NULL)
    860  1.1  christos 		{
    861  1.1  christos 		  size_t i;
    862  1.1  christos 
    863  1.1  christos 		  for (i = 0; i < ABBREV_HASH_SIZE; i++)
    864  1.1  christos 		    {
    865  1.1  christos 		      struct abbrev_info *abbrev = abbrevs[i];
    866  1.1  christos 
    867  1.1  christos 		      while (abbrev)
    868  1.1  christos 			{
    869  1.1  christos 			  free (abbrev->attrs);
    870  1.1  christos 			  abbrev = abbrev->next;
    871  1.1  christos 			}
    872  1.1  christos 		    }
    873  1.1  christos 		  return NULL;
    874  1.1  christos 		}
    875  1.1  christos 	      cur_abbrev->attrs = tmp;
    876  1.1  christos 	    }
    877  1.1  christos 
    878  1.1  christos 	  cur_abbrev->attrs[cur_abbrev->num_attrs].name
    879  1.1  christos 	    = (enum dwarf_attribute) abbrev_name;
    880  1.1  christos 	  cur_abbrev->attrs[cur_abbrev->num_attrs++].form
    881  1.1  christos 	    = (enum dwarf_form) abbrev_form;
    882  1.1  christos 	  abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
    883  1.1  christos 	  abbrev_ptr += bytes_read;
    884  1.1  christos 	  abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
    885  1.1  christos 	  abbrev_ptr += bytes_read;
    886  1.1  christos 	}
    887  1.1  christos 
    888  1.1  christos       hash_number = abbrev_number % ABBREV_HASH_SIZE;
    889  1.1  christos       cur_abbrev->next = abbrevs[hash_number];
    890  1.1  christos       abbrevs[hash_number] = cur_abbrev;
    891  1.1  christos 
    892  1.1  christos       /* Get next abbreviation.
    893  1.1  christos 	 Under Irix6 the abbreviations for a compilation unit are not
    894  1.1  christos 	 always properly terminated with an abbrev number of 0.
    895  1.1  christos 	 Exit loop if we encounter an abbreviation which we have
    896  1.1  christos 	 already read (which means we are about to read the abbreviations
    897  1.1  christos 	 for the next compile unit) or if the end of the abbreviation
    898  1.1  christos 	 table is reached.  */
    899  1.1  christos       if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
    900  1.1  christos 	  >= stash->dwarf_abbrev_size)
    901  1.1  christos 	break;
    902  1.1  christos       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
    903  1.1  christos       abbrev_ptr += bytes_read;
    904  1.1  christos       if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
    905  1.1  christos 	break;
    906  1.1  christos     }
    907  1.1  christos 
    908  1.1  christos   return abbrevs;
    909  1.1  christos }
    910  1.1  christos 
    911  1.1  christos /* Read an attribute value described by an attribute form.  */
    912  1.1  christos 
    913  1.1  christos static bfd_byte *
    914  1.1  christos read_attribute_value (struct attribute *attr,
    915  1.1  christos 		      unsigned form,
    916  1.1  christos 		      struct comp_unit *unit,
    917  1.1  christos 		      bfd_byte *info_ptr)
    918  1.1  christos {
    919  1.1  christos   bfd *abfd = unit->abfd;
    920  1.1  christos   unsigned int bytes_read;
    921  1.1  christos   struct dwarf_block *blk;
    922  1.1  christos   bfd_size_type amt;
    923  1.1  christos 
    924  1.1  christos   attr->form = (enum dwarf_form) form;
    925  1.1  christos 
    926  1.1  christos   switch (form)
    927  1.1  christos     {
    928  1.1  christos     case DW_FORM_ref_addr:
    929  1.1  christos       /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
    930  1.1  christos 	 DWARF3.  */
    931  1.1  christos       if (unit->version == 3 || unit->version == 4)
    932  1.1  christos 	{
    933  1.1  christos 	  if (unit->offset_size == 4)
    934  1.1  christos 	    attr->u.val = read_4_bytes (unit->abfd, info_ptr);
    935  1.1  christos 	  else
    936  1.1  christos 	    attr->u.val = read_8_bytes (unit->abfd, info_ptr);
    937  1.1  christos 	  info_ptr += unit->offset_size;
    938  1.1  christos 	  break;
    939  1.1  christos 	}
    940  1.1  christos       /* FALLTHROUGH */
    941  1.1  christos     case DW_FORM_addr:
    942  1.1  christos       attr->u.val = read_address (unit, info_ptr);
    943  1.1  christos       info_ptr += unit->addr_size;
    944  1.1  christos       break;
    945  1.1  christos     case DW_FORM_GNU_ref_alt:
    946  1.1  christos     case DW_FORM_sec_offset:
    947  1.1  christos       if (unit->offset_size == 4)
    948  1.1  christos 	attr->u.val = read_4_bytes (unit->abfd, info_ptr);
    949  1.1  christos       else
    950  1.1  christos 	attr->u.val = read_8_bytes (unit->abfd, info_ptr);
    951  1.1  christos       info_ptr += unit->offset_size;
    952  1.1  christos       break;
    953  1.1  christos     case DW_FORM_block2:
    954  1.1  christos       amt = sizeof (struct dwarf_block);
    955  1.1  christos       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
    956  1.1  christos       if (blk == NULL)
    957  1.1  christos 	return NULL;
    958  1.1  christos       blk->size = read_2_bytes (abfd, info_ptr);
    959  1.1  christos       info_ptr += 2;
    960  1.1  christos       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
    961  1.1  christos       info_ptr += blk->size;
    962  1.1  christos       attr->u.blk = blk;
    963  1.1  christos       break;
    964  1.1  christos     case DW_FORM_block4:
    965  1.1  christos       amt = sizeof (struct dwarf_block);
    966  1.1  christos       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
    967  1.1  christos       if (blk == NULL)
    968  1.1  christos 	return NULL;
    969  1.1  christos       blk->size = read_4_bytes (abfd, info_ptr);
    970  1.1  christos       info_ptr += 4;
    971  1.1  christos       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
    972  1.1  christos       info_ptr += blk->size;
    973  1.1  christos       attr->u.blk = blk;
    974  1.1  christos       break;
    975  1.1  christos     case DW_FORM_data2:
    976  1.1  christos       attr->u.val = read_2_bytes (abfd, info_ptr);
    977  1.1  christos       info_ptr += 2;
    978  1.1  christos       break;
    979  1.1  christos     case DW_FORM_data4:
    980  1.1  christos       attr->u.val = read_4_bytes (abfd, info_ptr);
    981  1.1  christos       info_ptr += 4;
    982  1.1  christos       break;
    983  1.1  christos     case DW_FORM_data8:
    984  1.1  christos       attr->u.val = read_8_bytes (abfd, info_ptr);
    985  1.1  christos       info_ptr += 8;
    986  1.1  christos       break;
    987  1.1  christos     case DW_FORM_string:
    988  1.1  christos       attr->u.str = read_string (abfd, info_ptr, &bytes_read);
    989  1.1  christos       info_ptr += bytes_read;
    990  1.1  christos       break;
    991  1.1  christos     case DW_FORM_strp:
    992  1.1  christos       attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
    993  1.1  christos       info_ptr += bytes_read;
    994  1.1  christos       break;
    995  1.1  christos     case DW_FORM_GNU_strp_alt:
    996  1.1  christos       attr->u.str = read_alt_indirect_string (unit, info_ptr, &bytes_read);
    997  1.1  christos       info_ptr += bytes_read;
    998  1.1  christos       break;
    999  1.1  christos     case DW_FORM_exprloc:
   1000  1.1  christos     case DW_FORM_block:
   1001  1.1  christos       amt = sizeof (struct dwarf_block);
   1002  1.1  christos       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
   1003  1.1  christos       if (blk == NULL)
   1004  1.1  christos 	return NULL;
   1005  1.1  christos       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
   1006  1.1  christos       info_ptr += bytes_read;
   1007  1.1  christos       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
   1008  1.1  christos       info_ptr += blk->size;
   1009  1.1  christos       attr->u.blk = blk;
   1010  1.1  christos       break;
   1011  1.1  christos     case DW_FORM_block1:
   1012  1.1  christos       amt = sizeof (struct dwarf_block);
   1013  1.1  christos       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
   1014  1.1  christos       if (blk == NULL)
   1015  1.1  christos 	return NULL;
   1016  1.1  christos       blk->size = read_1_byte (abfd, info_ptr);
   1017  1.1  christos       info_ptr += 1;
   1018  1.1  christos       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
   1019  1.1  christos       info_ptr += blk->size;
   1020  1.1  christos       attr->u.blk = blk;
   1021  1.1  christos       break;
   1022  1.1  christos     case DW_FORM_data1:
   1023  1.1  christos       attr->u.val = read_1_byte (abfd, info_ptr);
   1024  1.1  christos       info_ptr += 1;
   1025  1.1  christos       break;
   1026  1.1  christos     case DW_FORM_flag:
   1027  1.1  christos       attr->u.val = read_1_byte (abfd, info_ptr);
   1028  1.1  christos       info_ptr += 1;
   1029  1.1  christos       break;
   1030  1.1  christos     case DW_FORM_flag_present:
   1031  1.1  christos       attr->u.val = 1;
   1032  1.1  christos       break;
   1033  1.1  christos     case DW_FORM_sdata:
   1034  1.1  christos       attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
   1035  1.1  christos       info_ptr += bytes_read;
   1036  1.1  christos       break;
   1037  1.1  christos     case DW_FORM_udata:
   1038  1.1  christos       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
   1039  1.1  christos       info_ptr += bytes_read;
   1040  1.1  christos       break;
   1041  1.1  christos     case DW_FORM_ref1:
   1042  1.1  christos       attr->u.val = read_1_byte (abfd, info_ptr);
   1043  1.1  christos       info_ptr += 1;
   1044  1.1  christos       break;
   1045  1.1  christos     case DW_FORM_ref2:
   1046  1.1  christos       attr->u.val = read_2_bytes (abfd, info_ptr);
   1047  1.1  christos       info_ptr += 2;
   1048  1.1  christos       break;
   1049  1.1  christos     case DW_FORM_ref4:
   1050  1.1  christos       attr->u.val = read_4_bytes (abfd, info_ptr);
   1051  1.1  christos       info_ptr += 4;
   1052  1.1  christos       break;
   1053  1.1  christos     case DW_FORM_ref8:
   1054  1.1  christos       attr->u.val = read_8_bytes (abfd, info_ptr);
   1055  1.1  christos       info_ptr += 8;
   1056  1.1  christos       break;
   1057  1.1  christos     case DW_FORM_ref_sig8:
   1058  1.1  christos       attr->u.val = read_8_bytes (abfd, info_ptr);
   1059  1.1  christos       info_ptr += 8;
   1060  1.1  christos       break;
   1061  1.1  christos     case DW_FORM_ref_udata:
   1062  1.1  christos       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
   1063  1.1  christos       info_ptr += bytes_read;
   1064  1.1  christos       break;
   1065  1.1  christos     case DW_FORM_indirect:
   1066  1.1  christos       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
   1067  1.1  christos       info_ptr += bytes_read;
   1068  1.1  christos       info_ptr = read_attribute_value (attr, form, unit, info_ptr);
   1069  1.1  christos       break;
   1070  1.1  christos     default:
   1071  1.1  christos       (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %#x."),
   1072  1.1  christos 			     form);
   1073  1.1  christos       bfd_set_error (bfd_error_bad_value);
   1074  1.1  christos       return NULL;
   1075  1.1  christos     }
   1076  1.1  christos   return info_ptr;
   1077  1.1  christos }
   1078  1.1  christos 
   1079  1.1  christos /* Read an attribute described by an abbreviated attribute.  */
   1080  1.1  christos 
   1081  1.1  christos static bfd_byte *
   1082  1.1  christos read_attribute (struct attribute *attr,
   1083  1.1  christos 		struct attr_abbrev *abbrev,
   1084  1.1  christos 		struct comp_unit *unit,
   1085  1.1  christos 		bfd_byte *info_ptr)
   1086  1.1  christos {
   1087  1.1  christos   attr->name = abbrev->name;
   1088  1.1  christos   info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
   1089  1.1  christos   return info_ptr;
   1090  1.1  christos }
   1091  1.1  christos 
   1092  1.1  christos /* Source line information table routines.  */
   1093  1.1  christos 
   1094  1.1  christos #define FILE_ALLOC_CHUNK 5
   1095  1.1  christos #define DIR_ALLOC_CHUNK 5
   1096  1.1  christos 
   1097  1.1  christos struct line_info
   1098  1.1  christos {
   1099  1.1  christos   struct line_info* prev_line;
   1100  1.1  christos   bfd_vma address;
   1101  1.1  christos   char *filename;
   1102  1.1  christos   unsigned int line;
   1103  1.1  christos   unsigned int column;
   1104  1.1  christos   unsigned int discriminator;
   1105  1.1  christos   unsigned char op_index;
   1106  1.1  christos   unsigned char end_sequence;		/* End of (sequential) code sequence.  */
   1107  1.1  christos };
   1108  1.1  christos 
   1109  1.1  christos struct fileinfo
   1110  1.1  christos {
   1111  1.1  christos   char *name;
   1112  1.1  christos   unsigned int dir;
   1113  1.1  christos   unsigned int time;
   1114  1.1  christos   unsigned int size;
   1115  1.1  christos };
   1116  1.1  christos 
   1117  1.1  christos struct line_sequence
   1118  1.1  christos {
   1119  1.1  christos   bfd_vma               low_pc;
   1120  1.1  christos   struct line_sequence* prev_sequence;
   1121  1.1  christos   struct line_info*     last_line;  /* Largest VMA.  */
   1122  1.1  christos };
   1123  1.1  christos 
   1124  1.1  christos struct line_info_table
   1125  1.1  christos {
   1126  1.1  christos   bfd*                  abfd;
   1127  1.1  christos   unsigned int          num_files;
   1128  1.1  christos   unsigned int          num_dirs;
   1129  1.1  christos   unsigned int          num_sequences;
   1130  1.1  christos   char *                comp_dir;
   1131  1.1  christos   char **               dirs;
   1132  1.1  christos   struct fileinfo*      files;
   1133  1.1  christos   struct line_sequence* sequences;
   1134  1.1  christos   struct line_info*     lcl_head;   /* Local head; used in 'add_line_info'.  */
   1135  1.1  christos };
   1136  1.1  christos 
   1137  1.1  christos /* Remember some information about each function.  If the function is
   1138  1.1  christos    inlined (DW_TAG_inlined_subroutine) it may have two additional
   1139  1.1  christos    attributes, DW_AT_call_file and DW_AT_call_line, which specify the
   1140  1.1  christos    source code location where this function was inlined.  */
   1141  1.1  christos 
   1142  1.1  christos struct funcinfo
   1143  1.1  christos {
   1144  1.1  christos   /* Pointer to previous function in list of all functions.  */
   1145  1.1  christos   struct funcinfo *prev_func;
   1146  1.1  christos   /* Pointer to function one scope higher.  */
   1147  1.1  christos   struct funcinfo *caller_func;
   1148  1.1  christos   /* Source location file name where caller_func inlines this func.  */
   1149  1.1  christos   char *caller_file;
   1150  1.1  christos   /* Source location line number where caller_func inlines this func.  */
   1151  1.1  christos   int caller_line;
   1152  1.1  christos   /* Source location file name.  */
   1153  1.1  christos   char *file;
   1154  1.1  christos   /* Source location line number.  */
   1155  1.1  christos   int line;
   1156  1.1  christos   int tag;
   1157  1.1  christos   char *name;
   1158  1.1  christos   struct arange arange;
   1159  1.1  christos   /* Where the symbol is defined.  */
   1160  1.1  christos   asection *sec;
   1161  1.1  christos };
   1162  1.1  christos 
   1163  1.1  christos struct varinfo
   1164  1.1  christos {
   1165  1.1  christos   /* Pointer to previous variable in list of all variables */
   1166  1.1  christos   struct varinfo *prev_var;
   1167  1.1  christos   /* Source location file name */
   1168  1.1  christos   char *file;
   1169  1.1  christos   /* Source location line number */
   1170  1.1  christos   int line;
   1171  1.1  christos   int tag;
   1172  1.1  christos   char *name;
   1173  1.1  christos   bfd_vma addr;
   1174  1.1  christos   /* Where the symbol is defined */
   1175  1.1  christos   asection *sec;
   1176  1.1  christos   /* Is this a stack variable? */
   1177  1.1  christos   unsigned int stack: 1;
   1178  1.1  christos };
   1179  1.1  christos 
   1180  1.1  christos /* Return TRUE if NEW_LINE should sort after LINE.  */
   1181  1.1  christos 
   1182  1.1  christos static inline bfd_boolean
   1183  1.1  christos new_line_sorts_after (struct line_info *new_line, struct line_info *line)
   1184  1.1  christos {
   1185  1.1  christos   return (new_line->address > line->address
   1186  1.1  christos 	  || (new_line->address == line->address
   1187  1.1  christos 	      && (new_line->op_index > line->op_index
   1188  1.1  christos 		  || (new_line->op_index == line->op_index
   1189  1.1  christos 		      && new_line->end_sequence < line->end_sequence))));
   1190  1.1  christos }
   1191  1.1  christos 
   1192  1.1  christos 
   1193  1.1  christos /* Adds a new entry to the line_info list in the line_info_table, ensuring
   1194  1.1  christos    that the list is sorted.  Note that the line_info list is sorted from
   1195  1.1  christos    highest to lowest VMA (with possible duplicates); that is,
   1196  1.1  christos    line_info->prev_line always accesses an equal or smaller VMA.  */
   1197  1.1  christos 
   1198  1.1  christos static bfd_boolean
   1199  1.1  christos add_line_info (struct line_info_table *table,
   1200  1.1  christos 	       bfd_vma address,
   1201  1.1  christos 	       unsigned char op_index,
   1202  1.1  christos 	       char *filename,
   1203  1.1  christos 	       unsigned int line,
   1204  1.1  christos 	       unsigned int column,
   1205  1.1  christos 	       unsigned int discriminator,
   1206  1.1  christos 	       int end_sequence)
   1207  1.1  christos {
   1208  1.1  christos   bfd_size_type amt = sizeof (struct line_info);
   1209  1.1  christos   struct line_sequence* seq = table->sequences;
   1210  1.1  christos   struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
   1211  1.1  christos 
   1212  1.1  christos   if (info == NULL)
   1213  1.1  christos     return FALSE;
   1214  1.1  christos 
   1215  1.1  christos   /* Set member data of 'info'.  */
   1216  1.1  christos   info->prev_line = NULL;
   1217  1.1  christos   info->address = address;
   1218  1.1  christos   info->op_index = op_index;
   1219  1.1  christos   info->line = line;
   1220  1.1  christos   info->column = column;
   1221  1.1  christos   info->discriminator = discriminator;
   1222  1.1  christos   info->end_sequence = end_sequence;
   1223  1.1  christos 
   1224  1.1  christos   if (filename && filename[0])
   1225  1.1  christos     {
   1226  1.1  christos       info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
   1227  1.1  christos       if (info->filename == NULL)
   1228  1.1  christos 	return FALSE;
   1229  1.1  christos       strcpy (info->filename, filename);
   1230  1.1  christos     }
   1231  1.1  christos   else
   1232  1.1  christos     info->filename = NULL;
   1233  1.1  christos 
   1234  1.1  christos   /* Find the correct location for 'info'.  Normally we will receive
   1235  1.1  christos      new line_info data 1) in order and 2) with increasing VMAs.
   1236  1.1  christos      However some compilers break the rules (cf. decode_line_info) and
   1237  1.1  christos      so we include some heuristics for quickly finding the correct
   1238  1.1  christos      location for 'info'. In particular, these heuristics optimize for
   1239  1.1  christos      the common case in which the VMA sequence that we receive is a
   1240  1.1  christos      list of locally sorted VMAs such as
   1241  1.1  christos        p...z a...j  (where a < j < p < z)
   1242  1.1  christos 
   1243  1.1  christos      Note: table->lcl_head is used to head an *actual* or *possible*
   1244  1.1  christos      sub-sequence within the list (such as a...j) that is not directly
   1245  1.1  christos      headed by table->last_line
   1246  1.1  christos 
   1247  1.1  christos      Note: we may receive duplicate entries from 'decode_line_info'.  */
   1248  1.1  christos 
   1249  1.1  christos   if (seq
   1250  1.1  christos       && seq->last_line->address == address
   1251  1.1  christos       && seq->last_line->op_index == op_index
   1252  1.1  christos       && seq->last_line->end_sequence == end_sequence)
   1253  1.1  christos     {
   1254  1.1  christos       /* We only keep the last entry with the same address and end
   1255  1.1  christos 	 sequence.  See PR ld/4986.  */
   1256  1.1  christos       if (table->lcl_head == seq->last_line)
   1257  1.1  christos 	table->lcl_head = info;
   1258  1.1  christos       info->prev_line = seq->last_line->prev_line;
   1259  1.1  christos       seq->last_line = info;
   1260  1.1  christos     }
   1261  1.1  christos   else if (!seq || seq->last_line->end_sequence)
   1262  1.1  christos     {
   1263  1.1  christos       /* Start a new line sequence.  */
   1264  1.1  christos       amt = sizeof (struct line_sequence);
   1265  1.1  christos       seq = (struct line_sequence *) bfd_malloc (amt);
   1266  1.1  christos       if (seq == NULL)
   1267  1.1  christos 	return FALSE;
   1268  1.1  christos       seq->low_pc = address;
   1269  1.1  christos       seq->prev_sequence = table->sequences;
   1270  1.1  christos       seq->last_line = info;
   1271  1.1  christos       table->lcl_head = info;
   1272  1.1  christos       table->sequences = seq;
   1273  1.1  christos       table->num_sequences++;
   1274  1.1  christos     }
   1275  1.1  christos   else if (new_line_sorts_after (info, seq->last_line))
   1276  1.1  christos     {
   1277  1.1  christos       /* Normal case: add 'info' to the beginning of the current sequence.  */
   1278  1.1  christos       info->prev_line = seq->last_line;
   1279  1.1  christos       seq->last_line = info;
   1280  1.1  christos 
   1281  1.1  christos       /* lcl_head: initialize to head a *possible* sequence at the end.  */
   1282  1.1  christos       if (!table->lcl_head)
   1283  1.1  christos 	table->lcl_head = info;
   1284  1.1  christos     }
   1285  1.1  christos   else if (!new_line_sorts_after (info, table->lcl_head)
   1286  1.1  christos 	   && (!table->lcl_head->prev_line
   1287  1.1  christos 	       || new_line_sorts_after (info, table->lcl_head->prev_line)))
   1288  1.1  christos     {
   1289  1.1  christos       /* Abnormal but easy: lcl_head is the head of 'info'.  */
   1290  1.1  christos       info->prev_line = table->lcl_head->prev_line;
   1291  1.1  christos       table->lcl_head->prev_line = info;
   1292  1.1  christos     }
   1293  1.1  christos   else
   1294  1.1  christos     {
   1295  1.1  christos       /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
   1296  1.1  christos 	 are valid heads for 'info'.  Reset 'lcl_head'.  */
   1297  1.1  christos       struct line_info* li2 = seq->last_line; /* Always non-NULL.  */
   1298  1.1  christos       struct line_info* li1 = li2->prev_line;
   1299  1.1  christos 
   1300  1.1  christos       while (li1)
   1301  1.1  christos 	{
   1302  1.1  christos 	  if (!new_line_sorts_after (info, li2)
   1303  1.1  christos 	      && new_line_sorts_after (info, li1))
   1304  1.1  christos 	    break;
   1305  1.1  christos 
   1306  1.1  christos 	  li2 = li1; /* always non-NULL */
   1307  1.1  christos 	  li1 = li1->prev_line;
   1308  1.1  christos 	}
   1309  1.1  christos       table->lcl_head = li2;
   1310  1.1  christos       info->prev_line = table->lcl_head->prev_line;
   1311  1.1  christos       table->lcl_head->prev_line = info;
   1312  1.1  christos       if (address < seq->low_pc)
   1313  1.1  christos         seq->low_pc = address;
   1314  1.1  christos     }
   1315  1.1  christos   return TRUE;
   1316  1.1  christos }
   1317  1.1  christos 
   1318  1.1  christos /* Extract a fully qualified filename from a line info table.
   1319  1.1  christos    The returned string has been malloc'ed and it is the caller's
   1320  1.1  christos    responsibility to free it.  */
   1321  1.1  christos 
   1322  1.1  christos static char *
   1323  1.1  christos concat_filename (struct line_info_table *table, unsigned int file)
   1324  1.1  christos {
   1325  1.1  christos   char *filename;
   1326  1.1  christos 
   1327  1.1  christos   if (file - 1 >= table->num_files)
   1328  1.1  christos     {
   1329  1.1  christos       /* FILE == 0 means unknown.  */
   1330  1.1  christos       if (file)
   1331  1.1  christos 	(*_bfd_error_handler)
   1332  1.1  christos 	  (_("Dwarf Error: mangled line number section (bad file number)."));
   1333  1.1  christos       return strdup ("<unknown>");
   1334  1.1  christos     }
   1335  1.1  christos 
   1336  1.1  christos   filename = table->files[file - 1].name;
   1337  1.1  christos 
   1338  1.1  christos   if (!IS_ABSOLUTE_PATH (filename))
   1339  1.1  christos     {
   1340  1.1  christos       char *dir_name = NULL;
   1341  1.1  christos       char *subdir_name = NULL;
   1342  1.1  christos       char *name;
   1343  1.1  christos       size_t len;
   1344  1.1  christos 
   1345  1.1  christos       if (table->files[file - 1].dir)
   1346  1.1  christos 	subdir_name = table->dirs[table->files[file - 1].dir - 1];
   1347  1.1  christos 
   1348  1.1  christos       if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
   1349  1.1  christos 	dir_name = table->comp_dir;
   1350  1.1  christos 
   1351  1.1  christos       if (!dir_name)
   1352  1.1  christos 	{
   1353  1.1  christos 	  dir_name = subdir_name;
   1354  1.1  christos 	  subdir_name = NULL;
   1355  1.1  christos 	}
   1356  1.1  christos 
   1357  1.1  christos       if (!dir_name)
   1358  1.1  christos 	return strdup (filename);
   1359  1.1  christos 
   1360  1.1  christos       len = strlen (dir_name) + strlen (filename) + 2;
   1361  1.1  christos 
   1362  1.1  christos       if (subdir_name)
   1363  1.1  christos 	{
   1364  1.1  christos 	  len += strlen (subdir_name) + 1;
   1365  1.1  christos 	  name = (char *) bfd_malloc (len);
   1366  1.1  christos 	  if (name)
   1367  1.1  christos 	    sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
   1368  1.1  christos 	}
   1369  1.1  christos       else
   1370  1.1  christos 	{
   1371  1.1  christos 	  name = (char *) bfd_malloc (len);
   1372  1.1  christos 	  if (name)
   1373  1.1  christos 	    sprintf (name, "%s/%s", dir_name, filename);
   1374  1.1  christos 	}
   1375  1.1  christos 
   1376  1.1  christos       return name;
   1377  1.1  christos     }
   1378  1.1  christos 
   1379  1.1  christos   return strdup (filename);
   1380  1.1  christos }
   1381  1.1  christos 
   1382  1.1  christos static bfd_boolean
   1383  1.1  christos arange_add (const struct comp_unit *unit, struct arange *first_arange,
   1384  1.1  christos 	    bfd_vma low_pc, bfd_vma high_pc)
   1385  1.1  christos {
   1386  1.1  christos   struct arange *arange;
   1387  1.1  christos 
   1388  1.1  christos   /* Ignore empty ranges.  */
   1389  1.1  christos   if (low_pc == high_pc)
   1390  1.1  christos     return TRUE;
   1391  1.1  christos 
   1392  1.1  christos   /* If the first arange is empty, use it.  */
   1393  1.1  christos   if (first_arange->high == 0)
   1394  1.1  christos     {
   1395  1.1  christos       first_arange->low = low_pc;
   1396  1.1  christos       first_arange->high = high_pc;
   1397  1.1  christos       return TRUE;
   1398  1.1  christos     }
   1399  1.1  christos 
   1400  1.1  christos   /* Next see if we can cheaply extend an existing range.  */
   1401  1.1  christos   arange = first_arange;
   1402  1.1  christos   do
   1403  1.1  christos     {
   1404  1.1  christos       if (low_pc == arange->high)
   1405  1.1  christos 	{
   1406  1.1  christos 	  arange->high = high_pc;
   1407  1.1  christos 	  return TRUE;
   1408  1.1  christos 	}
   1409  1.1  christos       if (high_pc == arange->low)
   1410  1.1  christos 	{
   1411  1.1  christos 	  arange->low = low_pc;
   1412  1.1  christos 	  return TRUE;
   1413  1.1  christos 	}
   1414  1.1  christos       arange = arange->next;
   1415  1.1  christos     }
   1416  1.1  christos   while (arange);
   1417  1.1  christos 
   1418  1.1  christos   /* Need to allocate a new arange and insert it into the arange list.
   1419  1.1  christos      Order isn't significant, so just insert after the first arange. */
   1420  1.1  christos   arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
   1421  1.1  christos   if (arange == NULL)
   1422  1.1  christos     return FALSE;
   1423  1.1  christos   arange->low = low_pc;
   1424  1.1  christos   arange->high = high_pc;
   1425  1.1  christos   arange->next = first_arange->next;
   1426  1.1  christos   first_arange->next = arange;
   1427  1.1  christos   return TRUE;
   1428  1.1  christos }
   1429  1.1  christos 
   1430  1.1  christos /* Compare function for line sequences.  */
   1431  1.1  christos 
   1432  1.1  christos static int
   1433  1.1  christos compare_sequences (const void* a, const void* b)
   1434  1.1  christos {
   1435  1.1  christos   const struct line_sequence* seq1 = a;
   1436  1.1  christos   const struct line_sequence* seq2 = b;
   1437  1.1  christos 
   1438  1.1  christos   /* Sort by low_pc as the primary key.  */
   1439  1.1  christos   if (seq1->low_pc < seq2->low_pc)
   1440  1.1  christos     return -1;
   1441  1.1  christos   if (seq1->low_pc > seq2->low_pc)
   1442  1.1  christos     return 1;
   1443  1.1  christos 
   1444  1.1  christos   /* If low_pc values are equal, sort in reverse order of
   1445  1.1  christos      high_pc, so that the largest region comes first.  */
   1446  1.1  christos   if (seq1->last_line->address < seq2->last_line->address)
   1447  1.1  christos     return 1;
   1448  1.1  christos   if (seq1->last_line->address > seq2->last_line->address)
   1449  1.1  christos     return -1;
   1450  1.1  christos 
   1451  1.1  christos   if (seq1->last_line->op_index < seq2->last_line->op_index)
   1452  1.1  christos     return 1;
   1453  1.1  christos   if (seq1->last_line->op_index > seq2->last_line->op_index)
   1454  1.1  christos     return -1;
   1455  1.1  christos 
   1456  1.1  christos   return 0;
   1457  1.1  christos }
   1458  1.1  christos 
   1459  1.1  christos /* Sort the line sequences for quick lookup.  */
   1460  1.1  christos 
   1461  1.1  christos static bfd_boolean
   1462  1.1  christos sort_line_sequences (struct line_info_table* table)
   1463  1.1  christos {
   1464  1.1  christos   bfd_size_type amt;
   1465  1.1  christos   struct line_sequence* sequences;
   1466  1.1  christos   struct line_sequence* seq;
   1467  1.1  christos   unsigned int n = 0;
   1468  1.1  christos   unsigned int num_sequences = table->num_sequences;
   1469  1.1  christos   bfd_vma last_high_pc;
   1470  1.1  christos 
   1471  1.1  christos   if (num_sequences == 0)
   1472  1.1  christos     return TRUE;
   1473  1.1  christos 
   1474  1.1  christos   /* Allocate space for an array of sequences.  */
   1475  1.1  christos   amt = sizeof (struct line_sequence) * num_sequences;
   1476  1.1  christos   sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
   1477  1.1  christos   if (sequences == NULL)
   1478  1.1  christos     return FALSE;
   1479  1.1  christos 
   1480  1.1  christos   /* Copy the linked list into the array, freeing the original nodes.  */
   1481  1.1  christos   seq = table->sequences;
   1482  1.1  christos   for (n = 0; n < num_sequences; n++)
   1483  1.1  christos     {
   1484  1.1  christos       struct line_sequence* last_seq = seq;
   1485  1.1  christos 
   1486  1.1  christos       BFD_ASSERT (seq);
   1487  1.1  christos       sequences[n].low_pc = seq->low_pc;
   1488  1.1  christos       sequences[n].prev_sequence = NULL;
   1489  1.1  christos       sequences[n].last_line = seq->last_line;
   1490  1.1  christos       seq = seq->prev_sequence;
   1491  1.1  christos       free (last_seq);
   1492  1.1  christos     }
   1493  1.1  christos   BFD_ASSERT (seq == NULL);
   1494  1.1  christos 
   1495  1.1  christos   qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
   1496  1.1  christos 
   1497  1.1  christos   /* Make the list binary-searchable by trimming overlapping entries
   1498  1.1  christos      and removing nested entries.  */
   1499  1.1  christos   num_sequences = 1;
   1500  1.1  christos   last_high_pc = sequences[0].last_line->address;
   1501  1.1  christos   for (n = 1; n < table->num_sequences; n++)
   1502  1.1  christos     {
   1503  1.1  christos       if (sequences[n].low_pc < last_high_pc)
   1504  1.1  christos         {
   1505  1.1  christos 	  if (sequences[n].last_line->address <= last_high_pc)
   1506  1.1  christos 	    /* Skip nested entries.  */
   1507  1.1  christos 	    continue;
   1508  1.1  christos 
   1509  1.1  christos 	  /* Trim overlapping entries.  */
   1510  1.1  christos 	  sequences[n].low_pc = last_high_pc;
   1511  1.1  christos         }
   1512  1.1  christos       last_high_pc = sequences[n].last_line->address;
   1513  1.1  christos       if (n > num_sequences)
   1514  1.1  christos         {
   1515  1.1  christos           /* Close up the gap.  */
   1516  1.1  christos           sequences[num_sequences].low_pc = sequences[n].low_pc;
   1517  1.1  christos           sequences[num_sequences].last_line = sequences[n].last_line;
   1518  1.1  christos         }
   1519  1.1  christos       num_sequences++;
   1520  1.1  christos     }
   1521  1.1  christos 
   1522  1.1  christos   table->sequences = sequences;
   1523  1.1  christos   table->num_sequences = num_sequences;
   1524  1.1  christos   return TRUE;
   1525  1.1  christos }
   1526  1.1  christos 
   1527  1.1  christos /* Decode the line number information for UNIT.  */
   1528  1.1  christos 
   1529  1.1  christos static struct line_info_table*
   1530  1.1  christos decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
   1531  1.1  christos {
   1532  1.1  christos   bfd *abfd = unit->abfd;
   1533  1.1  christos   struct line_info_table* table;
   1534  1.1  christos   bfd_byte *line_ptr;
   1535  1.1  christos   bfd_byte *line_end;
   1536  1.1  christos   struct line_head lh;
   1537  1.1  christos   unsigned int i, bytes_read, offset_size;
   1538  1.1  christos   char *cur_file, *cur_dir;
   1539  1.1  christos   unsigned char op_code, extended_op, adj_opcode;
   1540  1.1  christos   unsigned int exop_len;
   1541  1.1  christos   bfd_size_type amt;
   1542  1.1  christos 
   1543  1.1  christos   if (! read_section (abfd, &stash->debug_sections[debug_line],
   1544  1.1  christos                       stash->syms, unit->line_offset,
   1545  1.1  christos 		      &stash->dwarf_line_buffer, &stash->dwarf_line_size))
   1546  1.1  christos     return NULL;
   1547  1.1  christos 
   1548  1.1  christos   amt = sizeof (struct line_info_table);
   1549  1.1  christos   table = (struct line_info_table *) bfd_alloc (abfd, amt);
   1550  1.1  christos   if (table == NULL)
   1551  1.1  christos     return NULL;
   1552  1.1  christos   table->abfd = abfd;
   1553  1.1  christos   table->comp_dir = unit->comp_dir;
   1554  1.1  christos 
   1555  1.1  christos   table->num_files = 0;
   1556  1.1  christos   table->files = NULL;
   1557  1.1  christos 
   1558  1.1  christos   table->num_dirs = 0;
   1559  1.1  christos   table->dirs = NULL;
   1560  1.1  christos 
   1561  1.1  christos   table->num_sequences = 0;
   1562  1.1  christos   table->sequences = NULL;
   1563  1.1  christos 
   1564  1.1  christos   table->lcl_head = NULL;
   1565  1.1  christos 
   1566  1.1  christos   line_ptr = stash->dwarf_line_buffer + unit->line_offset;
   1567  1.1  christos 
   1568  1.1  christos   /* Read in the prologue.  */
   1569  1.1  christos   lh.total_length = read_4_bytes (abfd, line_ptr);
   1570  1.1  christos   line_ptr += 4;
   1571  1.1  christos   offset_size = 4;
   1572  1.1  christos   if (lh.total_length == 0xffffffff)
   1573  1.1  christos     {
   1574  1.1  christos       lh.total_length = read_8_bytes (abfd, line_ptr);
   1575  1.1  christos       line_ptr += 8;
   1576  1.1  christos       offset_size = 8;
   1577  1.1  christos     }
   1578  1.1  christos   else if (lh.total_length == 0 && unit->addr_size == 8)
   1579  1.1  christos     {
   1580  1.1  christos       /* Handle (non-standard) 64-bit DWARF2 formats.  */
   1581  1.1  christos       lh.total_length = read_4_bytes (abfd, line_ptr);
   1582  1.1  christos       line_ptr += 4;
   1583  1.1  christos       offset_size = 8;
   1584  1.1  christos     }
   1585  1.1  christos   line_end = line_ptr + lh.total_length;
   1586  1.1  christos   lh.version = read_2_bytes (abfd, line_ptr);
   1587  1.1  christos   if (lh.version < 2 || lh.version > 4)
   1588  1.1  christos     {
   1589  1.1  christos       (*_bfd_error_handler)
   1590  1.1  christos 	(_("Dwarf Error: Unhandled .debug_line version %d."), lh.version);
   1591  1.1  christos       bfd_set_error (bfd_error_bad_value);
   1592  1.1  christos       return NULL;
   1593  1.1  christos     }
   1594  1.1  christos   line_ptr += 2;
   1595  1.1  christos   if (offset_size == 4)
   1596  1.1  christos     lh.prologue_length = read_4_bytes (abfd, line_ptr);
   1597  1.1  christos   else
   1598  1.1  christos     lh.prologue_length = read_8_bytes (abfd, line_ptr);
   1599  1.1  christos   line_ptr += offset_size;
   1600  1.1  christos   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
   1601  1.1  christos   line_ptr += 1;
   1602  1.1  christos   if (lh.version >= 4)
   1603  1.1  christos     {
   1604  1.1  christos       lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr);
   1605  1.1  christos       line_ptr += 1;
   1606  1.1  christos     }
   1607  1.1  christos   else
   1608  1.1  christos     lh.maximum_ops_per_insn = 1;
   1609  1.1  christos   if (lh.maximum_ops_per_insn == 0)
   1610  1.1  christos     {
   1611  1.1  christos       (*_bfd_error_handler)
   1612  1.1  christos 	(_("Dwarf Error: Invalid maximum operations per instruction."));
   1613  1.1  christos       bfd_set_error (bfd_error_bad_value);
   1614  1.1  christos       return NULL;
   1615  1.1  christos     }
   1616  1.1  christos   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
   1617  1.1  christos   line_ptr += 1;
   1618  1.1  christos   lh.line_base = read_1_signed_byte (abfd, line_ptr);
   1619  1.1  christos   line_ptr += 1;
   1620  1.1  christos   lh.line_range = read_1_byte (abfd, line_ptr);
   1621  1.1  christos   line_ptr += 1;
   1622  1.1  christos   lh.opcode_base = read_1_byte (abfd, line_ptr);
   1623  1.1  christos   line_ptr += 1;
   1624  1.1  christos   amt = lh.opcode_base * sizeof (unsigned char);
   1625  1.1  christos   lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
   1626  1.1  christos 
   1627  1.1  christos   lh.standard_opcode_lengths[0] = 1;
   1628  1.1  christos 
   1629  1.1  christos   for (i = 1; i < lh.opcode_base; ++i)
   1630  1.1  christos     {
   1631  1.1  christos       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
   1632  1.1  christos       line_ptr += 1;
   1633  1.1  christos     }
   1634  1.1  christos 
   1635  1.1  christos   /* Read directory table.  */
   1636  1.1  christos   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
   1637  1.1  christos     {
   1638  1.1  christos       line_ptr += bytes_read;
   1639  1.1  christos 
   1640  1.1  christos       if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
   1641  1.1  christos 	{
   1642  1.1  christos 	  char **tmp;
   1643  1.1  christos 
   1644  1.1  christos 	  amt = table->num_dirs + DIR_ALLOC_CHUNK;
   1645  1.1  christos 	  amt *= sizeof (char *);
   1646  1.1  christos 
   1647  1.1  christos 	  tmp = (char **) bfd_realloc (table->dirs, amt);
   1648  1.1  christos 	  if (tmp == NULL)
   1649  1.1  christos 	    goto fail;
   1650  1.1  christos 	  table->dirs = tmp;
   1651  1.1  christos 	}
   1652  1.1  christos 
   1653  1.1  christos       table->dirs[table->num_dirs++] = cur_dir;
   1654  1.1  christos     }
   1655  1.1  christos 
   1656  1.1  christos   line_ptr += bytes_read;
   1657  1.1  christos 
   1658  1.1  christos   /* Read file name table.  */
   1659  1.1  christos   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
   1660  1.1  christos     {
   1661  1.1  christos       line_ptr += bytes_read;
   1662  1.1  christos 
   1663  1.1  christos       if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
   1664  1.1  christos 	{
   1665  1.1  christos 	  struct fileinfo *tmp;
   1666  1.1  christos 
   1667  1.1  christos 	  amt = table->num_files + FILE_ALLOC_CHUNK;
   1668  1.1  christos 	  amt *= sizeof (struct fileinfo);
   1669  1.1  christos 
   1670  1.1  christos 	  tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
   1671  1.1  christos 	  if (tmp == NULL)
   1672  1.1  christos 	    goto fail;
   1673  1.1  christos 	  table->files = tmp;
   1674  1.1  christos 	}
   1675  1.1  christos 
   1676  1.1  christos       table->files[table->num_files].name = cur_file;
   1677  1.1  christos       table->files[table->num_files].dir =
   1678  1.1  christos 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
   1679  1.1  christos       line_ptr += bytes_read;
   1680  1.1  christos       table->files[table->num_files].time =
   1681  1.1  christos 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
   1682  1.1  christos       line_ptr += bytes_read;
   1683  1.1  christos       table->files[table->num_files].size =
   1684  1.1  christos 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
   1685  1.1  christos       line_ptr += bytes_read;
   1686  1.1  christos       table->num_files++;
   1687  1.1  christos     }
   1688  1.1  christos 
   1689  1.1  christos   line_ptr += bytes_read;
   1690  1.1  christos 
   1691  1.1  christos   /* Read the statement sequences until there's nothing left.  */
   1692  1.1  christos   while (line_ptr < line_end)
   1693  1.1  christos     {
   1694  1.1  christos       /* State machine registers.  */
   1695  1.1  christos       bfd_vma address = 0;
   1696  1.1  christos       unsigned char op_index = 0;
   1697  1.1  christos       char * filename = table->num_files ? concat_filename (table, 1) : NULL;
   1698  1.1  christos       unsigned int line = 1;
   1699  1.1  christos       unsigned int column = 0;
   1700  1.1  christos       unsigned int discriminator = 0;
   1701  1.1  christos       int is_stmt = lh.default_is_stmt;
   1702  1.1  christos       int end_sequence = 0;
   1703  1.1  christos       /* eraxxon (at) alumni.rice.edu: Against the DWARF2 specs, some
   1704  1.1  christos 	 compilers generate address sequences that are wildly out of
   1705  1.1  christos 	 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
   1706  1.1  christos 	 for ia64-Linux).  Thus, to determine the low and high
   1707  1.1  christos 	 address, we must compare on every DW_LNS_copy, etc.  */
   1708  1.1  christos       bfd_vma low_pc  = (bfd_vma) -1;
   1709  1.1  christos       bfd_vma high_pc = 0;
   1710  1.1  christos 
   1711  1.1  christos       /* Decode the table.  */
   1712  1.1  christos       while (! end_sequence)
   1713  1.1  christos 	{
   1714  1.1  christos 	  op_code = read_1_byte (abfd, line_ptr);
   1715  1.1  christos 	  line_ptr += 1;
   1716  1.1  christos 
   1717  1.1  christos 	  if (op_code >= lh.opcode_base)
   1718  1.1  christos 	    {
   1719  1.1  christos 	      /* Special operand.  */
   1720  1.1  christos 	      adj_opcode = op_code - lh.opcode_base;
   1721  1.1  christos 	      if (lh.maximum_ops_per_insn == 1)
   1722  1.1  christos 		address += (adj_opcode / lh.line_range
   1723  1.1  christos 			    * lh.minimum_instruction_length);
   1724  1.1  christos 	      else
   1725  1.1  christos 		{
   1726  1.1  christos 		  address += ((op_index + adj_opcode / lh.line_range)
   1727  1.1  christos 			      / lh.maximum_ops_per_insn
   1728  1.1  christos 			      * lh.minimum_instruction_length);
   1729  1.1  christos 		  op_index = ((op_index + adj_opcode / lh.line_range)
   1730  1.1  christos 			      % lh.maximum_ops_per_insn);
   1731  1.1  christos 		}
   1732  1.1  christos 	      line += lh.line_base + (adj_opcode % lh.line_range);
   1733  1.1  christos 	      /* Append row to matrix using current values.  */
   1734  1.1  christos 	      if (!add_line_info (table, address, op_index, filename,
   1735  1.1  christos 				  line, column, discriminator, 0))
   1736  1.1  christos 		goto line_fail;
   1737  1.1  christos               discriminator = 0;
   1738  1.1  christos 	      if (address < low_pc)
   1739  1.1  christos 		low_pc = address;
   1740  1.1  christos 	      if (address > high_pc)
   1741  1.1  christos 		high_pc = address;
   1742  1.1  christos 	    }
   1743  1.1  christos 	  else switch (op_code)
   1744  1.1  christos 	    {
   1745  1.1  christos 	    case DW_LNS_extended_op:
   1746  1.1  christos 	      exop_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
   1747  1.1  christos 	      line_ptr += bytes_read;
   1748  1.1  christos 	      extended_op = read_1_byte (abfd, line_ptr);
   1749  1.1  christos 	      line_ptr += 1;
   1750  1.1  christos 
   1751  1.1  christos 	      switch (extended_op)
   1752  1.1  christos 		{
   1753  1.1  christos 		case DW_LNE_end_sequence:
   1754  1.1  christos 		  end_sequence = 1;
   1755  1.1  christos 		  if (!add_line_info (table, address, op_index, filename, line,
   1756  1.1  christos 				      column, discriminator, end_sequence))
   1757  1.1  christos 		    goto line_fail;
   1758  1.1  christos                   discriminator = 0;
   1759  1.1  christos 		  if (address < low_pc)
   1760  1.1  christos 		    low_pc = address;
   1761  1.1  christos 		  if (address > high_pc)
   1762  1.1  christos 		    high_pc = address;
   1763  1.1  christos 		  if (!arange_add (unit, &unit->arange, low_pc, high_pc))
   1764  1.1  christos 		    goto line_fail;
   1765  1.1  christos 		  break;
   1766  1.1  christos 		case DW_LNE_set_address:
   1767  1.1  christos 		  address = read_address (unit, line_ptr);
   1768  1.1  christos 		  op_index = 0;
   1769  1.1  christos 		  line_ptr += unit->addr_size;
   1770  1.1  christos 		  break;
   1771  1.1  christos 		case DW_LNE_define_file:
   1772  1.1  christos 		  cur_file = read_string (abfd, line_ptr, &bytes_read);
   1773  1.1  christos 		  line_ptr += bytes_read;
   1774  1.1  christos 		  if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
   1775  1.1  christos 		    {
   1776  1.1  christos 		      struct fileinfo *tmp;
   1777  1.1  christos 
   1778  1.1  christos 		      amt = table->num_files + FILE_ALLOC_CHUNK;
   1779  1.1  christos 		      amt *= sizeof (struct fileinfo);
   1780  1.1  christos 		      tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
   1781  1.1  christos 		      if (tmp == NULL)
   1782  1.1  christos 			goto line_fail;
   1783  1.1  christos 		      table->files = tmp;
   1784  1.1  christos 		    }
   1785  1.1  christos 		  table->files[table->num_files].name = cur_file;
   1786  1.1  christos 		  table->files[table->num_files].dir =
   1787  1.1  christos 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
   1788  1.1  christos 		  line_ptr += bytes_read;
   1789  1.1  christos 		  table->files[table->num_files].time =
   1790  1.1  christos 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
   1791  1.1  christos 		  line_ptr += bytes_read;
   1792  1.1  christos 		  table->files[table->num_files].size =
   1793  1.1  christos 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
   1794  1.1  christos 		  line_ptr += bytes_read;
   1795  1.1  christos 		  table->num_files++;
   1796  1.1  christos 		  break;
   1797  1.1  christos 		case DW_LNE_set_discriminator:
   1798  1.1  christos 		  discriminator =
   1799  1.1  christos                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
   1800  1.1  christos 		  line_ptr += bytes_read;
   1801  1.1  christos 		  break;
   1802  1.1  christos 		case DW_LNE_HP_source_file_correlation:
   1803  1.1  christos 		  line_ptr += exop_len - 1;
   1804  1.1  christos 		  break;
   1805  1.1  christos 		default:
   1806  1.1  christos 		  (*_bfd_error_handler)
   1807  1.1  christos 		    (_("Dwarf Error: mangled line number section."));
   1808  1.1  christos 		  bfd_set_error (bfd_error_bad_value);
   1809  1.1  christos 		line_fail:
   1810  1.1  christos 		  if (filename != NULL)
   1811  1.1  christos 		    free (filename);
   1812  1.1  christos 		  goto fail;
   1813  1.1  christos 		}
   1814  1.1  christos 	      break;
   1815  1.1  christos 	    case DW_LNS_copy:
   1816  1.1  christos 	      if (!add_line_info (table, address, op_index,
   1817  1.1  christos 				  filename, line, column, discriminator, 0))
   1818  1.1  christos 		goto line_fail;
   1819  1.1  christos               discriminator = 0;
   1820  1.1  christos 	      if (address < low_pc)
   1821  1.1  christos 		low_pc = address;
   1822  1.1  christos 	      if (address > high_pc)
   1823  1.1  christos 		high_pc = address;
   1824  1.1  christos 	      break;
   1825  1.1  christos 	    case DW_LNS_advance_pc:
   1826  1.1  christos 	      if (lh.maximum_ops_per_insn == 1)
   1827  1.1  christos 		address += (lh.minimum_instruction_length
   1828  1.1  christos 			    * read_unsigned_leb128 (abfd, line_ptr,
   1829  1.1  christos 						    &bytes_read));
   1830  1.1  christos 	      else
   1831  1.1  christos 		{
   1832  1.1  christos 		  bfd_vma adjust = read_unsigned_leb128 (abfd, line_ptr,
   1833  1.1  christos 							 &bytes_read);
   1834  1.1  christos 		  address = ((op_index + adjust) / lh.maximum_ops_per_insn
   1835  1.1  christos 			     * lh.minimum_instruction_length);
   1836  1.1  christos 		  op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
   1837  1.1  christos 		}
   1838  1.1  christos 	      line_ptr += bytes_read;
   1839  1.1  christos 	      break;
   1840  1.1  christos 	    case DW_LNS_advance_line:
   1841  1.1  christos 	      line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
   1842  1.1  christos 	      line_ptr += bytes_read;
   1843  1.1  christos 	      break;
   1844  1.1  christos 	    case DW_LNS_set_file:
   1845  1.1  christos 	      {
   1846  1.1  christos 		unsigned int file;
   1847  1.1  christos 
   1848  1.1  christos 		/* The file and directory tables are 0
   1849  1.1  christos 		   based, the references are 1 based.  */
   1850  1.1  christos 		file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
   1851  1.1  christos 		line_ptr += bytes_read;
   1852  1.1  christos 		if (filename)
   1853  1.1  christos 		  free (filename);
   1854  1.1  christos 		filename = concat_filename (table, file);
   1855  1.1  christos 		break;
   1856  1.1  christos 	      }
   1857  1.1  christos 	    case DW_LNS_set_column:
   1858  1.1  christos 	      column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
   1859  1.1  christos 	      line_ptr += bytes_read;
   1860  1.1  christos 	      break;
   1861  1.1  christos 	    case DW_LNS_negate_stmt:
   1862  1.1  christos 	      is_stmt = (!is_stmt);
   1863  1.1  christos 	      break;
   1864  1.1  christos 	    case DW_LNS_set_basic_block:
   1865  1.1  christos 	      break;
   1866  1.1  christos 	    case DW_LNS_const_add_pc:
   1867  1.1  christos 	      if (lh.maximum_ops_per_insn == 1)
   1868  1.1  christos 		address += (lh.minimum_instruction_length
   1869  1.1  christos 			    * ((255 - lh.opcode_base) / lh.line_range));
   1870  1.1  christos 	      else
   1871  1.1  christos 		{
   1872  1.1  christos 		  bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
   1873  1.1  christos 		  address += (lh.minimum_instruction_length
   1874  1.1  christos 			      * ((op_index + adjust)
   1875  1.1  christos 				 / lh.maximum_ops_per_insn));
   1876  1.1  christos 		  op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
   1877  1.1  christos 		}
   1878  1.1  christos 	      break;
   1879  1.1  christos 	    case DW_LNS_fixed_advance_pc:
   1880  1.1  christos 	      address += read_2_bytes (abfd, line_ptr);
   1881  1.1  christos 	      op_index = 0;
   1882  1.1  christos 	      line_ptr += 2;
   1883  1.1  christos 	      break;
   1884  1.1  christos 	    default:
   1885  1.1  christos 	      /* Unknown standard opcode, ignore it.  */
   1886  1.1  christos 	      for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
   1887  1.1  christos 		{
   1888  1.1  christos 		  (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
   1889  1.1  christos 		  line_ptr += bytes_read;
   1890  1.1  christos 		}
   1891  1.1  christos 	      break;
   1892  1.1  christos 	    }
   1893  1.1  christos 	}
   1894  1.1  christos 
   1895  1.1  christos       if (filename)
   1896  1.1  christos 	free (filename);
   1897  1.1  christos     }
   1898  1.1  christos 
   1899  1.1  christos   if (sort_line_sequences (table))
   1900  1.1  christos     return table;
   1901  1.1  christos 
   1902  1.1  christos  fail:
   1903  1.1  christos   if (table->sequences != NULL)
   1904  1.1  christos     free (table->sequences);
   1905  1.1  christos   if (table->files != NULL)
   1906  1.1  christos     free (table->files);
   1907  1.1  christos   if (table->dirs != NULL)
   1908  1.1  christos     free (table->dirs);
   1909  1.1  christos   return NULL;
   1910  1.1  christos }
   1911  1.1  christos 
   1912  1.1  christos /* If ADDR is within TABLE set the output parameters and return the
   1913  1.1  christos    range of addresses covered by the entry used to fill them out.
   1914  1.1  christos    Otherwise set * FILENAME_PTR to NULL and return 0.
   1915  1.1  christos    The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
   1916  1.1  christos    are pointers to the objects to be filled in.  */
   1917  1.1  christos 
   1918  1.1  christos static bfd_vma
   1919  1.1  christos lookup_address_in_line_info_table (struct line_info_table *table,
   1920  1.1  christos 				   bfd_vma addr,
   1921  1.1  christos 				   const char **filename_ptr,
   1922  1.1  christos 				   unsigned int *linenumber_ptr,
   1923  1.1  christos 				   unsigned int *discriminator_ptr)
   1924  1.1  christos {
   1925  1.1  christos   struct line_sequence *seq = NULL;
   1926  1.1  christos   struct line_info *each_line;
   1927  1.1  christos   int low, high, mid;
   1928  1.1  christos 
   1929  1.1  christos   /* Binary search the array of sequences.  */
   1930  1.1  christos   low = 0;
   1931  1.1  christos   high = table->num_sequences;
   1932  1.1  christos   while (low < high)
   1933  1.1  christos     {
   1934  1.1  christos       mid = (low + high) / 2;
   1935  1.1  christos       seq = &table->sequences[mid];
   1936  1.1  christos       if (addr < seq->low_pc)
   1937  1.1  christos 	high = mid;
   1938  1.1  christos       else if (addr >= seq->last_line->address)
   1939  1.1  christos 	low = mid + 1;
   1940  1.1  christos       else
   1941  1.1  christos 	break;
   1942  1.1  christos     }
   1943  1.1  christos 
   1944  1.1  christos   if (seq && addr >= seq->low_pc && addr < seq->last_line->address)
   1945  1.1  christos     {
   1946  1.1  christos       /* Note: seq->last_line should be a descendingly sorted list.  */
   1947  1.1  christos       for (each_line = seq->last_line;
   1948  1.1  christos            each_line;
   1949  1.1  christos            each_line = each_line->prev_line)
   1950  1.1  christos         if (addr >= each_line->address)
   1951  1.1  christos           break;
   1952  1.1  christos 
   1953  1.1  christos       if (each_line
   1954  1.1  christos           && !(each_line->end_sequence || each_line == seq->last_line))
   1955  1.1  christos         {
   1956  1.1  christos           *filename_ptr = each_line->filename;
   1957  1.1  christos           *linenumber_ptr = each_line->line;
   1958  1.1  christos           if (discriminator_ptr)
   1959  1.1  christos             *discriminator_ptr = each_line->discriminator;
   1960  1.1  christos           return seq->last_line->address - seq->low_pc;
   1961  1.1  christos         }
   1962  1.1  christos     }
   1963  1.1  christos 
   1964  1.1  christos   *filename_ptr = NULL;
   1965  1.1  christos   return 0;
   1966  1.1  christos }
   1967  1.1  christos 
   1968  1.1  christos /* Read in the .debug_ranges section for future reference.  */
   1969  1.1  christos 
   1970  1.1  christos static bfd_boolean
   1971  1.1  christos read_debug_ranges (struct comp_unit *unit)
   1972  1.1  christos {
   1973  1.1  christos   struct dwarf2_debug *stash = unit->stash;
   1974  1.1  christos   return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
   1975  1.1  christos                        stash->syms, 0,
   1976  1.1  christos 		       &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
   1977  1.1  christos }
   1978  1.1  christos 
   1979  1.1  christos /* Function table functions.  */
   1980  1.1  christos 
   1981  1.1  christos /* If ADDR is within UNIT's function tables, set FUNCTIONNAME_PTR, and return
   1982  1.1  christos    TRUE.  Note that we need to find the function that has the smallest range
   1983  1.1  christos    that contains ADDR, to handle inlined functions without depending upon
   1984  1.1  christos    them being ordered in TABLE by increasing range.  */
   1985  1.1  christos 
   1986  1.1  christos static bfd_boolean
   1987  1.1  christos lookup_address_in_function_table (struct comp_unit *unit,
   1988  1.1  christos 				  bfd_vma addr,
   1989  1.1  christos 				  struct funcinfo **function_ptr,
   1990  1.1  christos 				  const char **functionname_ptr)
   1991  1.1  christos {
   1992  1.1  christos   struct funcinfo* each_func;
   1993  1.1  christos   struct funcinfo* best_fit = NULL;
   1994  1.1  christos   struct arange *arange;
   1995  1.1  christos 
   1996  1.1  christos   for (each_func = unit->function_table;
   1997  1.1  christos        each_func;
   1998  1.1  christos        each_func = each_func->prev_func)
   1999  1.1  christos     {
   2000  1.1  christos       for (arange = &each_func->arange;
   2001  1.1  christos 	   arange;
   2002  1.1  christos 	   arange = arange->next)
   2003  1.1  christos 	{
   2004  1.1  christos 	  if (addr >= arange->low && addr < arange->high)
   2005  1.1  christos 	    {
   2006  1.1  christos 	      if (!best_fit
   2007  1.1  christos 		  || (arange->high - arange->low
   2008  1.1  christos 		      < best_fit->arange.high - best_fit->arange.low))
   2009  1.1  christos 		best_fit = each_func;
   2010  1.1  christos 	    }
   2011  1.1  christos 	}
   2012  1.1  christos     }
   2013  1.1  christos 
   2014  1.1  christos   if (best_fit)
   2015  1.1  christos     {
   2016  1.1  christos       *functionname_ptr = best_fit->name;
   2017  1.1  christos       *function_ptr = best_fit;
   2018  1.1  christos       return TRUE;
   2019  1.1  christos     }
   2020  1.1  christos   else
   2021  1.1  christos     {
   2022  1.1  christos       return FALSE;
   2023  1.1  christos     }
   2024  1.1  christos }
   2025  1.1  christos 
   2026  1.1  christos /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
   2027  1.1  christos    and LINENUMBER_PTR, and return TRUE.  */
   2028  1.1  christos 
   2029  1.1  christos static bfd_boolean
   2030  1.1  christos lookup_symbol_in_function_table (struct comp_unit *unit,
   2031  1.1  christos 				 asymbol *sym,
   2032  1.1  christos 				 bfd_vma addr,
   2033  1.1  christos 				 const char **filename_ptr,
   2034  1.1  christos 				 unsigned int *linenumber_ptr)
   2035  1.1  christos {
   2036  1.1  christos   struct funcinfo* each_func;
   2037  1.1  christos   struct funcinfo* best_fit = NULL;
   2038  1.1  christos   struct arange *arange;
   2039  1.1  christos   const char *name = bfd_asymbol_name (sym);
   2040  1.1  christos   asection *sec = bfd_get_section (sym);
   2041  1.1  christos 
   2042  1.1  christos   for (each_func = unit->function_table;
   2043  1.1  christos        each_func;
   2044  1.1  christos        each_func = each_func->prev_func)
   2045  1.1  christos     {
   2046  1.1  christos       for (arange = &each_func->arange;
   2047  1.1  christos 	   arange;
   2048  1.1  christos 	   arange = arange->next)
   2049  1.1  christos 	{
   2050  1.1  christos 	  if ((!each_func->sec || each_func->sec == sec)
   2051  1.1  christos 	      && addr >= arange->low
   2052  1.1  christos 	      && addr < arange->high
   2053  1.1  christos 	      && each_func->name
   2054  1.1  christos 	      && strcmp (name, each_func->name) == 0
   2055  1.1  christos 	      && (!best_fit
   2056  1.1  christos 		  || (arange->high - arange->low
   2057  1.1  christos 		      < best_fit->arange.high - best_fit->arange.low)))
   2058  1.1  christos 	    best_fit = each_func;
   2059  1.1  christos 	}
   2060  1.1  christos     }
   2061  1.1  christos 
   2062  1.1  christos   if (best_fit)
   2063  1.1  christos     {
   2064  1.1  christos       best_fit->sec = sec;
   2065  1.1  christos       *filename_ptr = best_fit->file;
   2066  1.1  christos       *linenumber_ptr = best_fit->line;
   2067  1.1  christos       return TRUE;
   2068  1.1  christos     }
   2069  1.1  christos   else
   2070  1.1  christos     return FALSE;
   2071  1.1  christos }
   2072  1.1  christos 
   2073  1.1  christos /* Variable table functions.  */
   2074  1.1  christos 
   2075  1.1  christos /* If SYM is within variable table of UNIT, set FILENAME_PTR and
   2076  1.1  christos    LINENUMBER_PTR, and return TRUE.  */
   2077  1.1  christos 
   2078  1.1  christos static bfd_boolean
   2079  1.1  christos lookup_symbol_in_variable_table (struct comp_unit *unit,
   2080  1.1  christos 				 asymbol *sym,
   2081  1.1  christos 				 bfd_vma addr,
   2082  1.1  christos 				 const char **filename_ptr,
   2083  1.1  christos 				 unsigned int *linenumber_ptr)
   2084  1.1  christos {
   2085  1.1  christos   const char *name = bfd_asymbol_name (sym);
   2086  1.1  christos   asection *sec = bfd_get_section (sym);
   2087  1.1  christos   struct varinfo* each;
   2088  1.1  christos 
   2089  1.1  christos   for (each = unit->variable_table; each; each = each->prev_var)
   2090  1.1  christos     if (each->stack == 0
   2091  1.1  christos 	&& each->file != NULL
   2092  1.1  christos 	&& each->name != NULL
   2093  1.1  christos 	&& each->addr == addr
   2094  1.1  christos 	&& (!each->sec || each->sec == sec)
   2095  1.1  christos 	&& strcmp (name, each->name) == 0)
   2096  1.1  christos       break;
   2097  1.1  christos 
   2098  1.1  christos   if (each)
   2099  1.1  christos     {
   2100  1.1  christos       each->sec = sec;
   2101  1.1  christos       *filename_ptr = each->file;
   2102  1.1  christos       *linenumber_ptr = each->line;
   2103  1.1  christos       return TRUE;
   2104  1.1  christos     }
   2105  1.1  christos   else
   2106  1.1  christos     return FALSE;
   2107  1.1  christos }
   2108  1.1  christos 
   2109  1.1  christos static char *
   2110  1.1  christos find_abstract_instance_name (struct comp_unit *unit,
   2111  1.1  christos 			     struct attribute *attr_ptr)
   2112  1.1  christos {
   2113  1.1  christos   bfd *abfd = unit->abfd;
   2114  1.1  christos   bfd_byte *info_ptr;
   2115  1.1  christos   unsigned int abbrev_number, bytes_read, i;
   2116  1.1  christos   struct abbrev_info *abbrev;
   2117  1.1  christos   bfd_uint64_t die_ref = attr_ptr->u.val;
   2118  1.1  christos   struct attribute attr;
   2119  1.1  christos   char *name = NULL;
   2120  1.1  christos 
   2121  1.1  christos   /* DW_FORM_ref_addr can reference an entry in a different CU. It
   2122  1.1  christos      is an offset from the .debug_info section, not the current CU.  */
   2123  1.1  christos   if (attr_ptr->form == DW_FORM_ref_addr)
   2124  1.1  christos     {
   2125  1.1  christos       /* We only support DW_FORM_ref_addr within the same file, so
   2126  1.1  christos 	 any relocations should be resolved already.  */
   2127  1.1  christos       if (!die_ref)
   2128  1.1  christos 	abort ();
   2129  1.1  christos 
   2130  1.1  christos       info_ptr = unit->sec_info_ptr + die_ref;
   2131  1.1  christos     }
   2132  1.1  christos   else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
   2133  1.1  christos     {
   2134  1.1  christos       info_ptr = read_alt_indirect_ref (unit, die_ref);
   2135  1.1  christos       if (info_ptr == NULL)
   2136  1.1  christos 	{
   2137  1.1  christos 	  (*_bfd_error_handler)
   2138  1.1  christos 	    (_("Dwarf Error: Unable to read alt ref %u."), die_ref);
   2139  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   2140  1.1  christos 	  return name;
   2141  1.1  christos 	}
   2142  1.1  christos     }
   2143  1.1  christos   else
   2144  1.1  christos     info_ptr = unit->info_ptr_unit + die_ref;
   2145  1.1  christos 
   2146  1.1  christos   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
   2147  1.1  christos   info_ptr += bytes_read;
   2148  1.1  christos 
   2149  1.1  christos   if (abbrev_number)
   2150  1.1  christos     {
   2151  1.1  christos       abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
   2152  1.1  christos       if (! abbrev)
   2153  1.1  christos 	{
   2154  1.1  christos 	  (*_bfd_error_handler)
   2155  1.1  christos 	    (_("Dwarf Error: Could not find abbrev number %u."), abbrev_number);
   2156  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   2157  1.1  christos 	}
   2158  1.1  christos       else
   2159  1.1  christos 	{
   2160  1.1  christos 	  for (i = 0; i < abbrev->num_attrs; ++i)
   2161  1.1  christos 	    {
   2162  1.1  christos 	      info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
   2163  1.1  christos 					 info_ptr);
   2164  1.1  christos 	      if (info_ptr == NULL)
   2165  1.1  christos 		break;
   2166  1.1  christos 	      switch (attr.name)
   2167  1.1  christos 		{
   2168  1.1  christos 		case DW_AT_name:
   2169  1.1  christos 		  /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
   2170  1.1  christos 		     over DW_AT_name.  */
   2171  1.1  christos 		  if (name == NULL)
   2172  1.1  christos 		    name = attr.u.str;
   2173  1.1  christos 		  break;
   2174  1.1  christos 		case DW_AT_specification:
   2175  1.1  christos 		  name = find_abstract_instance_name (unit, &attr);
   2176  1.1  christos 		  break;
   2177  1.1  christos 		case DW_AT_linkage_name:
   2178  1.1  christos 		case DW_AT_MIPS_linkage_name:
   2179  1.1  christos 		  name = attr.u.str;
   2180  1.1  christos 		  break;
   2181  1.1  christos 		default:
   2182  1.1  christos 		  break;
   2183  1.1  christos 		}
   2184  1.1  christos 	    }
   2185  1.1  christos 	}
   2186  1.1  christos     }
   2187  1.1  christos   return name;
   2188  1.1  christos }
   2189  1.1  christos 
   2190  1.1  christos static bfd_boolean
   2191  1.1  christos read_rangelist (struct comp_unit *unit, struct arange *arange,
   2192  1.1  christos 		bfd_uint64_t offset)
   2193  1.1  christos {
   2194  1.1  christos   bfd_byte *ranges_ptr;
   2195  1.1  christos   bfd_vma base_address = unit->base_address;
   2196  1.1  christos 
   2197  1.1  christos   if (! unit->stash->dwarf_ranges_buffer)
   2198  1.1  christos     {
   2199  1.1  christos       if (! read_debug_ranges (unit))
   2200  1.1  christos 	return FALSE;
   2201  1.1  christos     }
   2202  1.1  christos   ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
   2203  1.1  christos 
   2204  1.1  christos   for (;;)
   2205  1.1  christos     {
   2206  1.1  christos       bfd_vma low_pc;
   2207  1.1  christos       bfd_vma high_pc;
   2208  1.1  christos 
   2209  1.1  christos       low_pc = read_address (unit, ranges_ptr);
   2210  1.1  christos       ranges_ptr += unit->addr_size;
   2211  1.1  christos       high_pc = read_address (unit, ranges_ptr);
   2212  1.1  christos       ranges_ptr += unit->addr_size;
   2213  1.1  christos 
   2214  1.1  christos       if (low_pc == 0 && high_pc == 0)
   2215  1.1  christos 	break;
   2216  1.1  christos       if (low_pc == -1UL && high_pc != -1UL)
   2217  1.1  christos 	base_address = high_pc;
   2218  1.1  christos       else
   2219  1.1  christos 	{
   2220  1.1  christos 	  if (!arange_add (unit, arange,
   2221  1.1  christos 			   base_address + low_pc, base_address + high_pc))
   2222  1.1  christos 	    return FALSE;
   2223  1.1  christos 	}
   2224  1.1  christos     }
   2225  1.1  christos   return TRUE;
   2226  1.1  christos }
   2227  1.1  christos 
   2228  1.1  christos /* DWARF2 Compilation unit functions.  */
   2229  1.1  christos 
   2230  1.1  christos /* Scan over each die in a comp. unit looking for functions to add
   2231  1.1  christos    to the function table and variables to the variable table.  */
   2232  1.1  christos 
   2233  1.1  christos static bfd_boolean
   2234  1.1  christos scan_unit_for_symbols (struct comp_unit *unit)
   2235  1.1  christos {
   2236  1.1  christos   bfd *abfd = unit->abfd;
   2237  1.1  christos   bfd_byte *info_ptr = unit->first_child_die_ptr;
   2238  1.1  christos   int nesting_level = 1;
   2239  1.1  christos   struct funcinfo **nested_funcs;
   2240  1.1  christos   int nested_funcs_size;
   2241  1.1  christos 
   2242  1.1  christos   /* Maintain a stack of in-scope functions and inlined functions, which we
   2243  1.1  christos      can use to set the caller_func field.  */
   2244  1.1  christos   nested_funcs_size = 32;
   2245  1.1  christos   nested_funcs = (struct funcinfo **)
   2246  1.1  christos     bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
   2247  1.1  christos   if (nested_funcs == NULL)
   2248  1.1  christos     return FALSE;
   2249  1.1  christos   nested_funcs[nesting_level] = 0;
   2250  1.1  christos 
   2251  1.1  christos   while (nesting_level)
   2252  1.1  christos     {
   2253  1.1  christos       unsigned int abbrev_number, bytes_read, i;
   2254  1.1  christos       struct abbrev_info *abbrev;
   2255  1.1  christos       struct attribute attr;
   2256  1.1  christos       struct funcinfo *func;
   2257  1.1  christos       struct varinfo *var;
   2258  1.1  christos       bfd_vma low_pc = 0;
   2259  1.1  christos       bfd_vma high_pc = 0;
   2260  1.1  christos       bfd_boolean high_pc_relative = FALSE;
   2261  1.1  christos 
   2262  1.1  christos       abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
   2263  1.1  christos       info_ptr += bytes_read;
   2264  1.1  christos 
   2265  1.1  christos       if (! abbrev_number)
   2266  1.1  christos 	{
   2267  1.1  christos 	  nesting_level--;
   2268  1.1  christos 	  continue;
   2269  1.1  christos 	}
   2270  1.1  christos 
   2271  1.1  christos       abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
   2272  1.1  christos       if (! abbrev)
   2273  1.1  christos 	{
   2274  1.1  christos 	  (*_bfd_error_handler)
   2275  1.1  christos 	    (_("Dwarf Error: Could not find abbrev number %u."),
   2276  1.1  christos 	     abbrev_number);
   2277  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   2278  1.1  christos 	  goto fail;
   2279  1.1  christos 	}
   2280  1.1  christos 
   2281  1.1  christos       var = NULL;
   2282  1.1  christos       if (abbrev->tag == DW_TAG_subprogram
   2283  1.1  christos 	  || abbrev->tag == DW_TAG_entry_point
   2284  1.1  christos 	  || abbrev->tag == DW_TAG_inlined_subroutine)
   2285  1.1  christos 	{
   2286  1.1  christos 	  bfd_size_type amt = sizeof (struct funcinfo);
   2287  1.1  christos 	  func = (struct funcinfo *) bfd_zalloc (abfd, amt);
   2288  1.1  christos 	  if (func == NULL)
   2289  1.1  christos 	    goto fail;
   2290  1.1  christos 	  func->tag = abbrev->tag;
   2291  1.1  christos 	  func->prev_func = unit->function_table;
   2292  1.1  christos 	  unit->function_table = func;
   2293  1.1  christos 	  BFD_ASSERT (!unit->cached);
   2294  1.1  christos 
   2295  1.1  christos 	  if (func->tag == DW_TAG_inlined_subroutine)
   2296  1.1  christos 	    for (i = nesting_level - 1; i >= 1; i--)
   2297  1.1  christos 	      if (nested_funcs[i])
   2298  1.1  christos 		{
   2299  1.1  christos 		  func->caller_func = nested_funcs[i];
   2300  1.1  christos 		  break;
   2301  1.1  christos 		}
   2302  1.1  christos 	  nested_funcs[nesting_level] = func;
   2303  1.1  christos 	}
   2304  1.1  christos       else
   2305  1.1  christos 	{
   2306  1.1  christos 	  func = NULL;
   2307  1.1  christos 	  if (abbrev->tag == DW_TAG_variable)
   2308  1.1  christos 	    {
   2309  1.1  christos 	      bfd_size_type amt = sizeof (struct varinfo);
   2310  1.1  christos 	      var = (struct varinfo *) bfd_zalloc (abfd, amt);
   2311  1.1  christos 	      if (var == NULL)
   2312  1.1  christos 		goto fail;
   2313  1.1  christos 	      var->tag = abbrev->tag;
   2314  1.1  christos 	      var->stack = 1;
   2315  1.1  christos 	      var->prev_var = unit->variable_table;
   2316  1.1  christos 	      unit->variable_table = var;
   2317  1.1  christos 	      BFD_ASSERT (!unit->cached);
   2318  1.1  christos 	    }
   2319  1.1  christos 
   2320  1.1  christos 	  /* No inline function in scope at this nesting level.  */
   2321  1.1  christos 	  nested_funcs[nesting_level] = 0;
   2322  1.1  christos 	}
   2323  1.1  christos 
   2324  1.1  christos       for (i = 0; i < abbrev->num_attrs; ++i)
   2325  1.1  christos 	{
   2326  1.1  christos 	  info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
   2327  1.1  christos 	  if (info_ptr == NULL)
   2328  1.1  christos 	    goto fail;
   2329  1.1  christos 
   2330  1.1  christos 	  if (func)
   2331  1.1  christos 	    {
   2332  1.1  christos 	      switch (attr.name)
   2333  1.1  christos 		{
   2334  1.1  christos 		case DW_AT_call_file:
   2335  1.1  christos 		  func->caller_file = concat_filename (unit->line_table,
   2336  1.1  christos 						       attr.u.val);
   2337  1.1  christos 		  break;
   2338  1.1  christos 
   2339  1.1  christos 		case DW_AT_call_line:
   2340  1.1  christos 		  func->caller_line = attr.u.val;
   2341  1.1  christos 		  break;
   2342  1.1  christos 
   2343  1.1  christos 		case DW_AT_abstract_origin:
   2344  1.1  christos 		case DW_AT_specification:
   2345  1.1  christos 		  func->name = find_abstract_instance_name (unit, &attr);
   2346  1.1  christos 		  break;
   2347  1.1  christos 
   2348  1.1  christos 		case DW_AT_name:
   2349  1.1  christos 		  /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
   2350  1.1  christos 		     over DW_AT_name.  */
   2351  1.1  christos 		  if (func->name == NULL)
   2352  1.1  christos 		    func->name = attr.u.str;
   2353  1.1  christos 		  break;
   2354  1.1  christos 
   2355  1.1  christos 		case DW_AT_linkage_name:
   2356  1.1  christos 		case DW_AT_MIPS_linkage_name:
   2357  1.1  christos 		  func->name = attr.u.str;
   2358  1.1  christos 		  break;
   2359  1.1  christos 
   2360  1.1  christos 		case DW_AT_low_pc:
   2361  1.1  christos 		  low_pc = attr.u.val;
   2362  1.1  christos 		  break;
   2363  1.1  christos 
   2364  1.1  christos 		case DW_AT_high_pc:
   2365  1.1  christos 		  high_pc = attr.u.val;
   2366  1.1  christos 		  high_pc_relative = attr.form != DW_FORM_addr;
   2367  1.1  christos 		  break;
   2368  1.1  christos 
   2369  1.1  christos 		case DW_AT_ranges:
   2370  1.1  christos 		  if (!read_rangelist (unit, &func->arange, attr.u.val))
   2371  1.1  christos 		    goto fail;
   2372  1.1  christos 		  break;
   2373  1.1  christos 
   2374  1.1  christos 		case DW_AT_decl_file:
   2375  1.1  christos 		  func->file = concat_filename (unit->line_table,
   2376  1.1  christos 						attr.u.val);
   2377  1.1  christos 		  break;
   2378  1.1  christos 
   2379  1.1  christos 		case DW_AT_decl_line:
   2380  1.1  christos 		  func->line = attr.u.val;
   2381  1.1  christos 		  break;
   2382  1.1  christos 
   2383  1.1  christos 		default:
   2384  1.1  christos 		  break;
   2385  1.1  christos 		}
   2386  1.1  christos 	    }
   2387  1.1  christos 	  else if (var)
   2388  1.1  christos 	    {
   2389  1.1  christos 	      switch (attr.name)
   2390  1.1  christos 		{
   2391  1.1  christos 		case DW_AT_name:
   2392  1.1  christos 		  var->name = attr.u.str;
   2393  1.1  christos 		  break;
   2394  1.1  christos 
   2395  1.1  christos 		case DW_AT_decl_file:
   2396  1.1  christos 		  var->file = concat_filename (unit->line_table,
   2397  1.1  christos 					       attr.u.val);
   2398  1.1  christos 		  break;
   2399  1.1  christos 
   2400  1.1  christos 		case DW_AT_decl_line:
   2401  1.1  christos 		  var->line = attr.u.val;
   2402  1.1  christos 		  break;
   2403  1.1  christos 
   2404  1.1  christos 		case DW_AT_external:
   2405  1.1  christos 		  if (attr.u.val != 0)
   2406  1.1  christos 		    var->stack = 0;
   2407  1.1  christos 		  break;
   2408  1.1  christos 
   2409  1.1  christos 		case DW_AT_location:
   2410  1.1  christos 		  switch (attr.form)
   2411  1.1  christos 		    {
   2412  1.1  christos 		    case DW_FORM_block:
   2413  1.1  christos 		    case DW_FORM_block1:
   2414  1.1  christos 		    case DW_FORM_block2:
   2415  1.1  christos 		    case DW_FORM_block4:
   2416  1.1  christos 		    case DW_FORM_exprloc:
   2417  1.1  christos 		      if (*attr.u.blk->data == DW_OP_addr)
   2418  1.1  christos 			{
   2419  1.1  christos 			  var->stack = 0;
   2420  1.1  christos 
   2421  1.1  christos 			  /* Verify that DW_OP_addr is the only opcode in the
   2422  1.1  christos 			     location, in which case the block size will be 1
   2423  1.1  christos 			     plus the address size.  */
   2424  1.1  christos 			  /* ??? For TLS variables, gcc can emit
   2425  1.1  christos 			     DW_OP_addr <addr> DW_OP_GNU_push_tls_address
   2426  1.1  christos 			     which we don't handle here yet.  */
   2427  1.1  christos 			  if (attr.u.blk->size == unit->addr_size + 1U)
   2428  1.1  christos 			    var->addr = bfd_get (unit->addr_size * 8,
   2429  1.1  christos 						 unit->abfd,
   2430  1.1  christos 						 attr.u.blk->data + 1);
   2431  1.1  christos 			}
   2432  1.1  christos 		      break;
   2433  1.1  christos 
   2434  1.1  christos 		    default:
   2435  1.1  christos 		      break;
   2436  1.1  christos 		    }
   2437  1.1  christos 		  break;
   2438  1.1  christos 
   2439  1.1  christos 		default:
   2440  1.1  christos 		  break;
   2441  1.1  christos 		}
   2442  1.1  christos 	    }
   2443  1.1  christos 	}
   2444  1.1  christos 
   2445  1.1  christos       if (high_pc_relative)
   2446  1.1  christos 	high_pc += low_pc;
   2447  1.1  christos 
   2448  1.1  christos       if (func && high_pc != 0)
   2449  1.1  christos 	{
   2450  1.1  christos 	  if (!arange_add (unit, &func->arange, low_pc, high_pc))
   2451  1.1  christos 	    goto fail;
   2452  1.1  christos 	}
   2453  1.1  christos 
   2454  1.1  christos       if (abbrev->has_children)
   2455  1.1  christos 	{
   2456  1.1  christos 	  nesting_level++;
   2457  1.1  christos 
   2458  1.1  christos 	  if (nesting_level >= nested_funcs_size)
   2459  1.1  christos 	    {
   2460  1.1  christos 	      struct funcinfo **tmp;
   2461  1.1  christos 
   2462  1.1  christos 	      nested_funcs_size *= 2;
   2463  1.1  christos 	      tmp = (struct funcinfo **)
   2464  1.1  christos 		bfd_realloc (nested_funcs,
   2465  1.1  christos 			     nested_funcs_size * sizeof (struct funcinfo *));
   2466  1.1  christos 	      if (tmp == NULL)
   2467  1.1  christos 		goto fail;
   2468  1.1  christos 	      nested_funcs = tmp;
   2469  1.1  christos 	    }
   2470  1.1  christos 	  nested_funcs[nesting_level] = 0;
   2471  1.1  christos 	}
   2472  1.1  christos     }
   2473  1.1  christos 
   2474  1.1  christos   free (nested_funcs);
   2475  1.1  christos   return TRUE;
   2476  1.1  christos 
   2477  1.1  christos  fail:
   2478  1.1  christos   free (nested_funcs);
   2479  1.1  christos   return FALSE;
   2480  1.1  christos }
   2481  1.1  christos 
   2482  1.1  christos /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
   2483  1.1  christos    includes the compilation unit header that proceeds the DIE's, but
   2484  1.1  christos    does not include the length field that precedes each compilation
   2485  1.1  christos    unit header.  END_PTR points one past the end of this comp unit.
   2486  1.1  christos    OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
   2487  1.1  christos 
   2488  1.1  christos    This routine does not read the whole compilation unit; only enough
   2489  1.1  christos    to get to the line number information for the compilation unit.  */
   2490  1.1  christos 
   2491  1.1  christos static struct comp_unit *
   2492  1.1  christos parse_comp_unit (struct dwarf2_debug *stash,
   2493  1.1  christos 		 bfd_vma unit_length,
   2494  1.1  christos 		 bfd_byte *info_ptr_unit,
   2495  1.1  christos 		 unsigned int offset_size)
   2496  1.1  christos {
   2497  1.1  christos   struct comp_unit* unit;
   2498  1.1  christos   unsigned int version;
   2499  1.1  christos   bfd_uint64_t abbrev_offset = 0;
   2500  1.1  christos   unsigned int addr_size;
   2501  1.1  christos   struct abbrev_info** abbrevs;
   2502  1.1  christos   unsigned int abbrev_number, bytes_read, i;
   2503  1.1  christos   struct abbrev_info *abbrev;
   2504  1.1  christos   struct attribute attr;
   2505  1.1  christos   bfd_byte *info_ptr = stash->info_ptr;
   2506  1.1  christos   bfd_byte *end_ptr = info_ptr + unit_length;
   2507  1.1  christos   bfd_size_type amt;
   2508  1.1  christos   bfd_vma low_pc = 0;
   2509  1.1  christos   bfd_vma high_pc = 0;
   2510  1.1  christos   bfd *abfd = stash->bfd_ptr;
   2511  1.1  christos   bfd_boolean high_pc_relative = FALSE;
   2512  1.1  christos 
   2513  1.1  christos   version = read_2_bytes (abfd, info_ptr);
   2514  1.1  christos   info_ptr += 2;
   2515  1.1  christos   BFD_ASSERT (offset_size == 4 || offset_size == 8);
   2516  1.1  christos   if (offset_size == 4)
   2517  1.1  christos     abbrev_offset = read_4_bytes (abfd, info_ptr);
   2518  1.1  christos   else
   2519  1.1  christos     abbrev_offset = read_8_bytes (abfd, info_ptr);
   2520  1.1  christos   info_ptr += offset_size;
   2521  1.1  christos   addr_size = read_1_byte (abfd, info_ptr);
   2522  1.1  christos   info_ptr += 1;
   2523  1.1  christos 
   2524  1.1  christos   if (version != 2 && version != 3 && version != 4)
   2525  1.1  christos     {
   2526  1.1  christos       (*_bfd_error_handler)
   2527  1.1  christos 	(_("Dwarf Error: found dwarf version '%u', this reader"
   2528  1.1  christos 	   " only handles version 2, 3 and 4 information."), version);
   2529  1.1  christos       bfd_set_error (bfd_error_bad_value);
   2530  1.1  christos       return 0;
   2531  1.1  christos     }
   2532  1.1  christos 
   2533  1.1  christos   if (addr_size > sizeof (bfd_vma))
   2534  1.1  christos     {
   2535  1.1  christos       (*_bfd_error_handler)
   2536  1.1  christos 	(_("Dwarf Error: found address size '%u', this reader"
   2537  1.1  christos 	   " can not handle sizes greater than '%u'."),
   2538  1.1  christos 	 addr_size,
   2539  1.1  christos 	 (unsigned int) sizeof (bfd_vma));
   2540  1.1  christos       bfd_set_error (bfd_error_bad_value);
   2541  1.1  christos       return 0;
   2542  1.1  christos     }
   2543  1.1  christos 
   2544  1.1  christos   if (addr_size != 2 && addr_size != 4 && addr_size != 8)
   2545  1.1  christos     {
   2546  1.1  christos       (*_bfd_error_handler)
   2547  1.1  christos 	("Dwarf Error: found address size '%u', this reader"
   2548  1.1  christos 	 " can only handle address sizes '2', '4' and '8'.", addr_size);
   2549  1.1  christos       bfd_set_error (bfd_error_bad_value);
   2550  1.1  christos       return 0;
   2551  1.1  christos     }
   2552  1.1  christos 
   2553  1.1  christos   /* Read the abbrevs for this compilation unit into a table.  */
   2554  1.1  christos   abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
   2555  1.1  christos   if (! abbrevs)
   2556  1.1  christos     return 0;
   2557  1.1  christos 
   2558  1.1  christos   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
   2559  1.1  christos   info_ptr += bytes_read;
   2560  1.1  christos   if (! abbrev_number)
   2561  1.1  christos     {
   2562  1.1  christos       (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
   2563  1.1  christos 			     abbrev_number);
   2564  1.1  christos       bfd_set_error (bfd_error_bad_value);
   2565  1.1  christos       return 0;
   2566  1.1  christos     }
   2567  1.1  christos 
   2568  1.1  christos   abbrev = lookup_abbrev (abbrev_number, abbrevs);
   2569  1.1  christos   if (! abbrev)
   2570  1.1  christos     {
   2571  1.1  christos       (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
   2572  1.1  christos 			     abbrev_number);
   2573  1.1  christos       bfd_set_error (bfd_error_bad_value);
   2574  1.1  christos       return 0;
   2575  1.1  christos     }
   2576  1.1  christos 
   2577  1.1  christos   amt = sizeof (struct comp_unit);
   2578  1.1  christos   unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
   2579  1.1  christos   if (unit == NULL)
   2580  1.1  christos     return NULL;
   2581  1.1  christos   unit->abfd = abfd;
   2582  1.1  christos   unit->version = version;
   2583  1.1  christos   unit->addr_size = addr_size;
   2584  1.1  christos   unit->offset_size = offset_size;
   2585  1.1  christos   unit->abbrevs = abbrevs;
   2586  1.1  christos   unit->end_ptr = end_ptr;
   2587  1.1  christos   unit->stash = stash;
   2588  1.1  christos   unit->info_ptr_unit = info_ptr_unit;
   2589  1.1  christos   unit->sec_info_ptr = stash->sec_info_ptr;
   2590  1.1  christos 
   2591  1.1  christos   for (i = 0; i < abbrev->num_attrs; ++i)
   2592  1.1  christos     {
   2593  1.1  christos       info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
   2594  1.1  christos       if (info_ptr == NULL)
   2595  1.1  christos 	return NULL;
   2596  1.1  christos 
   2597  1.1  christos       /* Store the data if it is of an attribute we want to keep in a
   2598  1.1  christos 	 partial symbol table.  */
   2599  1.1  christos       switch (attr.name)
   2600  1.1  christos 	{
   2601  1.1  christos 	case DW_AT_stmt_list:
   2602  1.1  christos 	  unit->stmtlist = 1;
   2603  1.1  christos 	  unit->line_offset = attr.u.val;
   2604  1.1  christos 	  break;
   2605  1.1  christos 
   2606  1.1  christos 	case DW_AT_name:
   2607  1.1  christos 	  unit->name = attr.u.str;
   2608  1.1  christos 	  break;
   2609  1.1  christos 
   2610  1.1  christos 	case DW_AT_low_pc:
   2611  1.1  christos 	  low_pc = attr.u.val;
   2612  1.1  christos 	  /* If the compilation unit DIE has a DW_AT_low_pc attribute,
   2613  1.1  christos 	     this is the base address to use when reading location
   2614  1.1  christos 	     lists or range lists. */
   2615  1.1  christos 	  if (abbrev->tag == DW_TAG_compile_unit)
   2616  1.1  christos 	    unit->base_address = low_pc;
   2617  1.1  christos 	  break;
   2618  1.1  christos 
   2619  1.1  christos 	case DW_AT_high_pc:
   2620  1.1  christos 	  high_pc = attr.u.val;
   2621  1.1  christos 	  high_pc_relative = attr.form != DW_FORM_addr;
   2622  1.1  christos 	  break;
   2623  1.1  christos 
   2624  1.1  christos 	case DW_AT_ranges:
   2625  1.1  christos 	  if (!read_rangelist (unit, &unit->arange, attr.u.val))
   2626  1.1  christos 	    return NULL;
   2627  1.1  christos 	  break;
   2628  1.1  christos 
   2629  1.1  christos 	case DW_AT_comp_dir:
   2630  1.1  christos 	  {
   2631  1.1  christos 	    char *comp_dir = attr.u.str;
   2632  1.1  christos 	    if (comp_dir)
   2633  1.1  christos 	      {
   2634  1.1  christos 		/* Irix 6.2 native cc prepends <machine>.: to the compilation
   2635  1.1  christos 		   directory, get rid of it.  */
   2636  1.1  christos 		char *cp = strchr (comp_dir, ':');
   2637  1.1  christos 
   2638  1.1  christos 		if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
   2639  1.1  christos 		  comp_dir = cp + 1;
   2640  1.1  christos 	      }
   2641  1.1  christos 	    unit->comp_dir = comp_dir;
   2642  1.1  christos 	    break;
   2643  1.1  christos 	  }
   2644  1.1  christos 
   2645  1.1  christos 	default:
   2646  1.1  christos 	  break;
   2647  1.1  christos 	}
   2648  1.1  christos     }
   2649  1.1  christos   if (high_pc_relative)
   2650  1.1  christos     high_pc += low_pc;
   2651  1.1  christos   if (high_pc != 0)
   2652  1.1  christos     {
   2653  1.1  christos       if (!arange_add (unit, &unit->arange, low_pc, high_pc))
   2654  1.1  christos 	return NULL;
   2655  1.1  christos     }
   2656  1.1  christos 
   2657  1.1  christos   unit->first_child_die_ptr = info_ptr;
   2658  1.1  christos   return unit;
   2659  1.1  christos }
   2660  1.1  christos 
   2661  1.1  christos /* Return TRUE if UNIT may contain the address given by ADDR.  When
   2662  1.1  christos    there are functions written entirely with inline asm statements, the
   2663  1.1  christos    range info in the compilation unit header may not be correct.  We
   2664  1.1  christos    need to consult the line info table to see if a compilation unit
   2665  1.1  christos    really contains the given address.  */
   2666  1.1  christos 
   2667  1.1  christos static bfd_boolean
   2668  1.1  christos comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
   2669  1.1  christos {
   2670  1.1  christos   struct arange *arange;
   2671  1.1  christos 
   2672  1.1  christos   if (unit->error)
   2673  1.1  christos     return FALSE;
   2674  1.1  christos 
   2675  1.1  christos   arange = &unit->arange;
   2676  1.1  christos   do
   2677  1.1  christos     {
   2678  1.1  christos       if (addr >= arange->low && addr < arange->high)
   2679  1.1  christos 	return TRUE;
   2680  1.1  christos       arange = arange->next;
   2681  1.1  christos     }
   2682  1.1  christos   while (arange);
   2683  1.1  christos 
   2684  1.1  christos   return FALSE;
   2685  1.1  christos }
   2686  1.1  christos 
   2687  1.1  christos /* If UNIT contains ADDR, set the output parameters to the values for
   2688  1.1  christos    the line containing ADDR.  The output parameters, FILENAME_PTR,
   2689  1.1  christos    FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
   2690  1.1  christos    to be filled in.
   2691  1.1  christos 
   2692  1.1  christos    Returns the range of addresses covered by the entry that was used
   2693  1.1  christos    to fill in *LINENUMBER_PTR or 0 if it was not filled in.  */
   2694  1.1  christos 
   2695  1.1  christos static bfd_vma
   2696  1.1  christos comp_unit_find_nearest_line (struct comp_unit *unit,
   2697  1.1  christos 			     bfd_vma addr,
   2698  1.1  christos 			     const char **filename_ptr,
   2699  1.1  christos 			     const char **functionname_ptr,
   2700  1.1  christos 			     unsigned int *linenumber_ptr,
   2701  1.1  christos 			     unsigned int *discriminator_ptr,
   2702  1.1  christos 			     struct dwarf2_debug *stash)
   2703  1.1  christos {
   2704  1.1  christos   bfd_boolean func_p;
   2705  1.1  christos   struct funcinfo *function;
   2706  1.1  christos 
   2707  1.1  christos   if (unit->error)
   2708  1.1  christos     return FALSE;
   2709  1.1  christos 
   2710  1.1  christos   if (! unit->line_table)
   2711  1.1  christos     {
   2712  1.1  christos       if (! unit->stmtlist)
   2713  1.1  christos 	{
   2714  1.1  christos 	  unit->error = 1;
   2715  1.1  christos 	  return FALSE;
   2716  1.1  christos 	}
   2717  1.1  christos 
   2718  1.1  christos       unit->line_table = decode_line_info (unit, stash);
   2719  1.1  christos 
   2720  1.1  christos       if (! unit->line_table)
   2721  1.1  christos 	{
   2722  1.1  christos 	  unit->error = 1;
   2723  1.1  christos 	  return FALSE;
   2724  1.1  christos 	}
   2725  1.1  christos 
   2726  1.1  christos       if (unit->first_child_die_ptr < unit->end_ptr
   2727  1.1  christos 	  && ! scan_unit_for_symbols (unit))
   2728  1.1  christos 	{
   2729  1.1  christos 	  unit->error = 1;
   2730  1.1  christos 	  return FALSE;
   2731  1.1  christos 	}
   2732  1.1  christos     }
   2733  1.1  christos 
   2734  1.1  christos   function = NULL;
   2735  1.1  christos   func_p = lookup_address_in_function_table (unit, addr,
   2736  1.1  christos 					     &function, functionname_ptr);
   2737  1.1  christos   if (func_p && (function->tag == DW_TAG_inlined_subroutine))
   2738  1.1  christos     stash->inliner_chain = function;
   2739  1.1  christos 
   2740  1.1  christos   return lookup_address_in_line_info_table (unit->line_table, addr,
   2741  1.1  christos 					    filename_ptr,
   2742  1.1  christos 					    linenumber_ptr,
   2743  1.1  christos 					    discriminator_ptr);
   2744  1.1  christos }
   2745  1.1  christos 
   2746  1.1  christos /* Check to see if line info is already decoded in a comp_unit.
   2747  1.1  christos    If not, decode it.  Returns TRUE if no errors were encountered;
   2748  1.1  christos    FALSE otherwise.  */
   2749  1.1  christos 
   2750  1.1  christos static bfd_boolean
   2751  1.1  christos comp_unit_maybe_decode_line_info (struct comp_unit *unit,
   2752  1.1  christos 				  struct dwarf2_debug *stash)
   2753  1.1  christos {
   2754  1.1  christos   if (unit->error)
   2755  1.1  christos     return FALSE;
   2756  1.1  christos 
   2757  1.1  christos   if (! unit->line_table)
   2758  1.1  christos     {
   2759  1.1  christos       if (! unit->stmtlist)
   2760  1.1  christos 	{
   2761  1.1  christos 	  unit->error = 1;
   2762  1.1  christos 	  return FALSE;
   2763  1.1  christos 	}
   2764  1.1  christos 
   2765  1.1  christos       unit->line_table = decode_line_info (unit, stash);
   2766  1.1  christos 
   2767  1.1  christos       if (! unit->line_table)
   2768  1.1  christos 	{
   2769  1.1  christos 	  unit->error = 1;
   2770  1.1  christos 	  return FALSE;
   2771  1.1  christos 	}
   2772  1.1  christos 
   2773  1.1  christos       if (unit->first_child_die_ptr < unit->end_ptr
   2774  1.1  christos 	  && ! scan_unit_for_symbols (unit))
   2775  1.1  christos 	{
   2776  1.1  christos 	  unit->error = 1;
   2777  1.1  christos 	  return FALSE;
   2778  1.1  christos 	}
   2779  1.1  christos     }
   2780  1.1  christos 
   2781  1.1  christos   return TRUE;
   2782  1.1  christos }
   2783  1.1  christos 
   2784  1.1  christos /* If UNIT contains SYM at ADDR, set the output parameters to the
   2785  1.1  christos    values for the line containing SYM.  The output parameters,
   2786  1.1  christos    FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
   2787  1.1  christos    filled in.
   2788  1.1  christos 
   2789  1.1  christos    Return TRUE if UNIT contains SYM, and no errors were encountered;
   2790  1.1  christos    FALSE otherwise.  */
   2791  1.1  christos 
   2792  1.1  christos static bfd_boolean
   2793  1.1  christos comp_unit_find_line (struct comp_unit *unit,
   2794  1.1  christos 		     asymbol *sym,
   2795  1.1  christos 		     bfd_vma addr,
   2796  1.1  christos 		     const char **filename_ptr,
   2797  1.1  christos 		     unsigned int *linenumber_ptr,
   2798  1.1  christos 		     struct dwarf2_debug *stash)
   2799  1.1  christos {
   2800  1.1  christos   if (!comp_unit_maybe_decode_line_info (unit, stash))
   2801  1.1  christos     return FALSE;
   2802  1.1  christos 
   2803  1.1  christos   if (sym->flags & BSF_FUNCTION)
   2804  1.1  christos     return lookup_symbol_in_function_table (unit, sym, addr,
   2805  1.1  christos 					    filename_ptr,
   2806  1.1  christos 					    linenumber_ptr);
   2807  1.1  christos 
   2808  1.1  christos   return lookup_symbol_in_variable_table (unit, sym, addr,
   2809  1.1  christos 					  filename_ptr,
   2810  1.1  christos 					  linenumber_ptr);
   2811  1.1  christos }
   2812  1.1  christos 
   2813  1.1  christos static struct funcinfo *
   2814  1.1  christos reverse_funcinfo_list (struct funcinfo *head)
   2815  1.1  christos {
   2816  1.1  christos   struct funcinfo *rhead;
   2817  1.1  christos   struct funcinfo *temp;
   2818  1.1  christos 
   2819  1.1  christos   for (rhead = NULL; head; head = temp)
   2820  1.1  christos     {
   2821  1.1  christos       temp = head->prev_func;
   2822  1.1  christos       head->prev_func = rhead;
   2823  1.1  christos       rhead = head;
   2824  1.1  christos     }
   2825  1.1  christos   return rhead;
   2826  1.1  christos }
   2827  1.1  christos 
   2828  1.1  christos static struct varinfo *
   2829  1.1  christos reverse_varinfo_list (struct varinfo *head)
   2830  1.1  christos {
   2831  1.1  christos   struct varinfo *rhead;
   2832  1.1  christos   struct varinfo *temp;
   2833  1.1  christos 
   2834  1.1  christos   for (rhead = NULL; head; head = temp)
   2835  1.1  christos     {
   2836  1.1  christos       temp = head->prev_var;
   2837  1.1  christos       head->prev_var = rhead;
   2838  1.1  christos       rhead = head;
   2839  1.1  christos     }
   2840  1.1  christos   return rhead;
   2841  1.1  christos }
   2842  1.1  christos 
   2843  1.1  christos /* Extract all interesting funcinfos and varinfos of a compilation
   2844  1.1  christos    unit into hash tables for faster lookup.  Returns TRUE if no
   2845  1.1  christos    errors were enountered; FALSE otherwise.  */
   2846  1.1  christos 
   2847  1.1  christos static bfd_boolean
   2848  1.1  christos comp_unit_hash_info (struct dwarf2_debug *stash,
   2849  1.1  christos 		     struct comp_unit *unit,
   2850  1.1  christos 		     struct info_hash_table *funcinfo_hash_table,
   2851  1.1  christos 		     struct info_hash_table *varinfo_hash_table)
   2852  1.1  christos {
   2853  1.1  christos   struct funcinfo* each_func;
   2854  1.1  christos   struct varinfo* each_var;
   2855  1.1  christos   bfd_boolean okay = TRUE;
   2856  1.1  christos 
   2857  1.1  christos   BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
   2858  1.1  christos 
   2859  1.1  christos   if (!comp_unit_maybe_decode_line_info (unit, stash))
   2860  1.1  christos     return FALSE;
   2861  1.1  christos 
   2862  1.1  christos   BFD_ASSERT (!unit->cached);
   2863  1.1  christos 
   2864  1.1  christos   /* To preserve the original search order, we went to visit the function
   2865  1.1  christos      infos in the reversed order of the list.  However, making the list
   2866  1.1  christos      bi-directional use quite a bit of extra memory.  So we reverse
   2867  1.1  christos      the list first, traverse the list in the now reversed order and
   2868  1.1  christos      finally reverse the list again to get back the original order.  */
   2869  1.1  christos   unit->function_table = reverse_funcinfo_list (unit->function_table);
   2870  1.1  christos   for (each_func = unit->function_table;
   2871  1.1  christos        each_func && okay;
   2872  1.1  christos        each_func = each_func->prev_func)
   2873  1.1  christos     {
   2874  1.1  christos       /* Skip nameless functions. */
   2875  1.1  christos       if (each_func->name)
   2876  1.1  christos 	/* There is no need to copy name string into hash table as
   2877  1.1  christos 	   name string is either in the dwarf string buffer or
   2878  1.1  christos 	   info in the stash.  */
   2879  1.1  christos 	okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
   2880  1.1  christos 				       (void*) each_func, FALSE);
   2881  1.1  christos     }
   2882  1.1  christos   unit->function_table = reverse_funcinfo_list (unit->function_table);
   2883  1.1  christos   if (!okay)
   2884  1.1  christos     return FALSE;
   2885  1.1  christos 
   2886  1.1  christos   /* We do the same for variable infos.  */
   2887  1.1  christos   unit->variable_table = reverse_varinfo_list (unit->variable_table);
   2888  1.1  christos   for (each_var = unit->variable_table;
   2889  1.1  christos        each_var && okay;
   2890  1.1  christos        each_var = each_var->prev_var)
   2891  1.1  christos     {
   2892  1.1  christos       /* Skip stack vars and vars with no files or names.  */
   2893  1.1  christos       if (each_var->stack == 0
   2894  1.1  christos 	  && each_var->file != NULL
   2895  1.1  christos 	  && each_var->name != NULL)
   2896  1.1  christos 	/* There is no need to copy name string into hash table as
   2897  1.1  christos 	   name string is either in the dwarf string buffer or
   2898  1.1  christos 	   info in the stash.  */
   2899  1.1  christos 	okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
   2900  1.1  christos 				       (void*) each_var, FALSE);
   2901  1.1  christos     }
   2902  1.1  christos 
   2903  1.1  christos   unit->variable_table = reverse_varinfo_list (unit->variable_table);
   2904  1.1  christos   unit->cached = TRUE;
   2905  1.1  christos   return okay;
   2906  1.1  christos }
   2907  1.1  christos 
   2908  1.1  christos /* Locate a section in a BFD containing debugging info.  The search starts
   2909  1.1  christos    from the section after AFTER_SEC, or from the first section in the BFD if
   2910  1.1  christos    AFTER_SEC is NULL.  The search works by examining the names of the
   2911  1.1  christos    sections.  There are three permissiable names.  The first two are given
   2912  1.1  christos    by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
   2913  1.1  christos    and .zdebug_info).  The third is a prefix .gnu.linkonce.wi.
   2914  1.1  christos    This is a variation on the .debug_info section which has a checksum
   2915  1.1  christos    describing the contents appended onto the name.  This allows the linker to
   2916  1.1  christos    identify and discard duplicate debugging sections for different
   2917  1.1  christos    compilation units.  */
   2918  1.1  christos #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
   2919  1.1  christos 
   2920  1.1  christos static asection *
   2921  1.1  christos find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
   2922  1.1  christos                  asection *after_sec)
   2923  1.1  christos {
   2924  1.1  christos   asection *msec;
   2925  1.1  christos   const char *look;
   2926  1.1  christos 
   2927  1.1  christos   if (after_sec == NULL)
   2928  1.1  christos     {
   2929  1.1  christos       look = debug_sections[debug_info].uncompressed_name;
   2930  1.1  christos       msec = bfd_get_section_by_name (abfd, look);
   2931  1.1  christos       if (msec != NULL)
   2932  1.1  christos 	return msec;
   2933  1.1  christos 
   2934  1.1  christos       look = debug_sections[debug_info].compressed_name;
   2935  1.1  christos       if (look != NULL)
   2936  1.1  christos 	{
   2937  1.1  christos 	  msec = bfd_get_section_by_name (abfd, look);
   2938  1.1  christos 	  if (msec != NULL)
   2939  1.1  christos 	    return msec;
   2940  1.1  christos 	}
   2941  1.1  christos 
   2942  1.1  christos       for (msec = abfd->sections; msec != NULL; msec = msec->next)
   2943  1.1  christos 	if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
   2944  1.1  christos 	  return msec;
   2945  1.1  christos 
   2946  1.1  christos       return NULL;
   2947  1.1  christos     }
   2948  1.1  christos 
   2949  1.1  christos   for (msec = after_sec->next; msec != NULL; msec = msec->next)
   2950  1.1  christos     {
   2951  1.1  christos       look = debug_sections[debug_info].uncompressed_name;
   2952  1.1  christos       if (strcmp (msec->name, look) == 0)
   2953  1.1  christos 	return msec;
   2954  1.1  christos 
   2955  1.1  christos       look = debug_sections[debug_info].compressed_name;
   2956  1.1  christos       if (look != NULL && strcmp (msec->name, look) == 0)
   2957  1.1  christos 	return msec;
   2958  1.1  christos 
   2959  1.1  christos       if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
   2960  1.1  christos 	return msec;
   2961  1.1  christos     }
   2962  1.1  christos 
   2963  1.1  christos   return NULL;
   2964  1.1  christos }
   2965  1.1  christos 
   2966  1.1  christos /* Unset vmas for adjusted sections in STASH.  */
   2967  1.1  christos 
   2968  1.1  christos static void
   2969  1.1  christos unset_sections (struct dwarf2_debug *stash)
   2970  1.1  christos {
   2971  1.1  christos   unsigned int i;
   2972  1.1  christos   struct adjusted_section *p;
   2973  1.1  christos 
   2974  1.1  christos   i = stash->adjusted_section_count;
   2975  1.1  christos   p = stash->adjusted_sections;
   2976  1.1  christos   for (; i > 0; i--, p++)
   2977  1.1  christos     p->section->vma = 0;
   2978  1.1  christos }
   2979  1.1  christos 
   2980  1.1  christos /* Set unique VMAs for loadable and DWARF sections in ABFD and save
   2981  1.1  christos    VMAs in STASH for unset_sections.  */
   2982  1.1  christos 
   2983  1.1  christos static bfd_boolean
   2984  1.1  christos place_sections (bfd *abfd, struct dwarf2_debug *stash)
   2985  1.1  christos {
   2986  1.1  christos   struct adjusted_section *p;
   2987  1.1  christos   unsigned int i;
   2988  1.1  christos 
   2989  1.1  christos   if (stash->adjusted_section_count != 0)
   2990  1.1  christos     {
   2991  1.1  christos       i = stash->adjusted_section_count;
   2992  1.1  christos       p = stash->adjusted_sections;
   2993  1.1  christos       for (; i > 0; i--, p++)
   2994  1.1  christos 	p->section->vma = p->adj_vma;
   2995  1.1  christos     }
   2996  1.1  christos   else
   2997  1.1  christos     {
   2998  1.1  christos       asection *sect;
   2999  1.1  christos       bfd_vma last_vma = 0, last_dwarf = 0;
   3000  1.1  christos       bfd_size_type amt;
   3001  1.1  christos       const char *debug_info_name;
   3002  1.1  christos 
   3003  1.1  christos       debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
   3004  1.1  christos       i = 0;
   3005  1.1  christos       for (sect = abfd->sections; sect != NULL; sect = sect->next)
   3006  1.1  christos 	{
   3007  1.1  christos 	  bfd_size_type sz;
   3008  1.1  christos 	  int is_debug_info;
   3009  1.1  christos 
   3010  1.1  christos 	  if (sect->vma != 0)
   3011  1.1  christos 	    continue;
   3012  1.1  christos 
   3013  1.1  christos 	  /* We need to adjust the VMAs of any .debug_info sections.
   3014  1.1  christos 	     Skip compressed ones, since no relocations could target
   3015  1.1  christos 	     them - they should not appear in object files anyway.  */
   3016  1.1  christos 	  if (strcmp (sect->name, debug_info_name) == 0)
   3017  1.1  christos 	    is_debug_info = 1;
   3018  1.1  christos 	  else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
   3019  1.1  christos 	    is_debug_info = 1;
   3020  1.1  christos 	  else
   3021  1.1  christos 	    is_debug_info = 0;
   3022  1.1  christos 
   3023  1.1  christos 	  if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
   3024  1.1  christos 	    continue;
   3025  1.1  christos 
   3026  1.1  christos 	  sz = sect->rawsize ? sect->rawsize : sect->size;
   3027  1.1  christos 	  if (sz == 0)
   3028  1.1  christos 	    continue;
   3029  1.1  christos 
   3030  1.1  christos 	  i++;
   3031  1.1  christos 	}
   3032  1.1  christos 
   3033  1.1  christos       amt = i * sizeof (struct adjusted_section);
   3034  1.1  christos       p = (struct adjusted_section *) bfd_alloc (abfd, amt);
   3035  1.1  christos       if (! p)
   3036  1.1  christos 	return FALSE;
   3037  1.1  christos 
   3038  1.1  christos       stash->adjusted_sections = p;
   3039  1.1  christos       stash->adjusted_section_count = i;
   3040  1.1  christos 
   3041  1.1  christos       for (sect = abfd->sections; sect != NULL; sect = sect->next)
   3042  1.1  christos 	{
   3043  1.1  christos 	  bfd_size_type sz;
   3044  1.1  christos 	  int is_debug_info;
   3045  1.1  christos 
   3046  1.1  christos 	  if (sect->vma != 0)
   3047  1.1  christos 	    continue;
   3048  1.1  christos 
   3049  1.1  christos 	  /* We need to adjust the VMAs of any .debug_info sections.
   3050  1.1  christos 	     Skip compressed ones, since no relocations could target
   3051  1.1  christos 	     them - they should not appear in object files anyway.  */
   3052  1.1  christos 	  if (strcmp (sect->name, debug_info_name) == 0)
   3053  1.1  christos 	    is_debug_info = 1;
   3054  1.1  christos 	  else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
   3055  1.1  christos 	    is_debug_info = 1;
   3056  1.1  christos 	  else
   3057  1.1  christos 	    is_debug_info = 0;
   3058  1.1  christos 
   3059  1.1  christos 	  if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
   3060  1.1  christos 	    continue;
   3061  1.1  christos 
   3062  1.1  christos 	  sz = sect->rawsize ? sect->rawsize : sect->size;
   3063  1.1  christos 	  if (sz == 0)
   3064  1.1  christos 	    continue;
   3065  1.1  christos 
   3066  1.1  christos 	  p->section = sect;
   3067  1.1  christos 	  if (is_debug_info)
   3068  1.1  christos 	    {
   3069  1.1  christos 	      BFD_ASSERT (sect->alignment_power == 0);
   3070  1.1  christos 	      sect->vma = last_dwarf;
   3071  1.1  christos 	      last_dwarf += sz;
   3072  1.1  christos 	    }
   3073  1.1  christos 	  else if (last_vma != 0)
   3074  1.1  christos 	    {
   3075  1.1  christos 	      /* Align the new address to the current section
   3076  1.1  christos 		 alignment.  */
   3077  1.1  christos 	      last_vma = ((last_vma
   3078  1.1  christos 			   + ~((bfd_vma) -1 << sect->alignment_power))
   3079  1.1  christos 			  & ((bfd_vma) -1 << sect->alignment_power));
   3080  1.1  christos 	      sect->vma = last_vma;
   3081  1.1  christos 	      last_vma += sect->vma + sz;
   3082  1.1  christos 	    }
   3083  1.1  christos 	  else
   3084  1.1  christos 	    last_vma += sect->vma + sz;
   3085  1.1  christos 
   3086  1.1  christos 	  p->adj_vma = sect->vma;
   3087  1.1  christos 
   3088  1.1  christos 	  p++;
   3089  1.1  christos 	}
   3090  1.1  christos     }
   3091  1.1  christos 
   3092  1.1  christos   return TRUE;
   3093  1.1  christos }
   3094  1.1  christos 
   3095  1.1  christos /* Look up a funcinfo by name using the given info hash table.  If found,
   3096  1.1  christos    also update the locations pointed to by filename_ptr and linenumber_ptr.
   3097  1.1  christos 
   3098  1.1  christos    This function returns TRUE if a funcinfo that matches the given symbol
   3099  1.1  christos    and address is found with any error; otherwise it returns FALSE.  */
   3100  1.1  christos 
   3101  1.1  christos static bfd_boolean
   3102  1.1  christos info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
   3103  1.1  christos 			   asymbol *sym,
   3104  1.1  christos 			   bfd_vma addr,
   3105  1.1  christos 			   const char **filename_ptr,
   3106  1.1  christos 			   unsigned int *linenumber_ptr)
   3107  1.1  christos {
   3108  1.1  christos   struct funcinfo* each_func;
   3109  1.1  christos   struct funcinfo* best_fit = NULL;
   3110  1.1  christos   struct info_list_node *node;
   3111  1.1  christos   struct arange *arange;
   3112  1.1  christos   const char *name = bfd_asymbol_name (sym);
   3113  1.1  christos   asection *sec = bfd_get_section (sym);
   3114  1.1  christos 
   3115  1.1  christos   for (node = lookup_info_hash_table (hash_table, name);
   3116  1.1  christos        node;
   3117  1.1  christos        node = node->next)
   3118  1.1  christos     {
   3119  1.1  christos       each_func = (struct funcinfo *) node->info;
   3120  1.1  christos       for (arange = &each_func->arange;
   3121  1.1  christos 	   arange;
   3122  1.1  christos 	   arange = arange->next)
   3123  1.1  christos 	{
   3124  1.1  christos 	  if ((!each_func->sec || each_func->sec == sec)
   3125  1.1  christos 	      && addr >= arange->low
   3126  1.1  christos 	      && addr < arange->high
   3127  1.1  christos 	      && (!best_fit
   3128  1.1  christos 		  || (arange->high - arange->low
   3129  1.1  christos 		      < best_fit->arange.high - best_fit->arange.low)))
   3130  1.1  christos 	    best_fit = each_func;
   3131  1.1  christos 	}
   3132  1.1  christos     }
   3133  1.1  christos 
   3134  1.1  christos   if (best_fit)
   3135  1.1  christos     {
   3136  1.1  christos       best_fit->sec = sec;
   3137  1.1  christos       *filename_ptr = best_fit->file;
   3138  1.1  christos       *linenumber_ptr = best_fit->line;
   3139  1.1  christos       return TRUE;
   3140  1.1  christos     }
   3141  1.1  christos 
   3142  1.1  christos   return FALSE;
   3143  1.1  christos }
   3144  1.1  christos 
   3145  1.1  christos /* Look up a varinfo by name using the given info hash table.  If found,
   3146  1.1  christos    also update the locations pointed to by filename_ptr and linenumber_ptr.
   3147  1.1  christos 
   3148  1.1  christos    This function returns TRUE if a varinfo that matches the given symbol
   3149  1.1  christos    and address is found with any error; otherwise it returns FALSE.  */
   3150  1.1  christos 
   3151  1.1  christos static bfd_boolean
   3152  1.1  christos info_hash_lookup_varinfo (struct info_hash_table *hash_table,
   3153  1.1  christos 			  asymbol *sym,
   3154  1.1  christos 			  bfd_vma addr,
   3155  1.1  christos 			  const char **filename_ptr,
   3156  1.1  christos 			  unsigned int *linenumber_ptr)
   3157  1.1  christos {
   3158  1.1  christos   const char *name = bfd_asymbol_name (sym);
   3159  1.1  christos   asection *sec = bfd_get_section (sym);
   3160  1.1  christos   struct varinfo* each;
   3161  1.1  christos   struct info_list_node *node;
   3162  1.1  christos 
   3163  1.1  christos   for (node = lookup_info_hash_table (hash_table, name);
   3164  1.1  christos        node;
   3165  1.1  christos        node = node->next)
   3166  1.1  christos     {
   3167  1.1  christos       each = (struct varinfo *) node->info;
   3168  1.1  christos       if (each->addr == addr
   3169  1.1  christos 	  && (!each->sec || each->sec == sec))
   3170  1.1  christos 	{
   3171  1.1  christos 	  each->sec = sec;
   3172  1.1  christos 	  *filename_ptr = each->file;
   3173  1.1  christos 	  *linenumber_ptr = each->line;
   3174  1.1  christos 	  return TRUE;
   3175  1.1  christos 	}
   3176  1.1  christos     }
   3177  1.1  christos 
   3178  1.1  christos   return FALSE;
   3179  1.1  christos }
   3180  1.1  christos 
   3181  1.1  christos /* Update the funcinfo and varinfo info hash tables if they are
   3182  1.1  christos    not up to date.  Returns TRUE if there is no error; otherwise
   3183  1.1  christos    returns FALSE and disable the info hash tables.  */
   3184  1.1  christos 
   3185  1.1  christos static bfd_boolean
   3186  1.1  christos stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
   3187  1.1  christos {
   3188  1.1  christos   struct comp_unit *each;
   3189  1.1  christos 
   3190  1.1  christos   /* Exit if hash tables are up-to-date.  */
   3191  1.1  christos   if (stash->all_comp_units == stash->hash_units_head)
   3192  1.1  christos     return TRUE;
   3193  1.1  christos 
   3194  1.1  christos   if (stash->hash_units_head)
   3195  1.1  christos     each = stash->hash_units_head->prev_unit;
   3196  1.1  christos   else
   3197  1.1  christos     each = stash->last_comp_unit;
   3198  1.1  christos 
   3199  1.1  christos   while (each)
   3200  1.1  christos     {
   3201  1.1  christos       if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
   3202  1.1  christos 				stash->varinfo_hash_table))
   3203  1.1  christos 	{
   3204  1.1  christos 	  stash->info_hash_status = STASH_INFO_HASH_DISABLED;
   3205  1.1  christos 	  return FALSE;
   3206  1.1  christos 	}
   3207  1.1  christos       each = each->prev_unit;
   3208  1.1  christos     }
   3209  1.1  christos 
   3210  1.1  christos   stash->hash_units_head = stash->all_comp_units;
   3211  1.1  christos   return TRUE;
   3212  1.1  christos }
   3213  1.1  christos 
   3214  1.1  christos /* Check consistency of info hash tables.  This is for debugging only. */
   3215  1.1  christos 
   3216  1.1  christos static void ATTRIBUTE_UNUSED
   3217  1.1  christos stash_verify_info_hash_table (struct dwarf2_debug *stash)
   3218  1.1  christos {
   3219  1.1  christos   struct comp_unit *each_unit;
   3220  1.1  christos   struct funcinfo *each_func;
   3221  1.1  christos   struct varinfo *each_var;
   3222  1.1  christos   struct info_list_node *node;
   3223  1.1  christos   bfd_boolean found;
   3224  1.1  christos 
   3225  1.1  christos   for (each_unit = stash->all_comp_units;
   3226  1.1  christos        each_unit;
   3227  1.1  christos        each_unit = each_unit->next_unit)
   3228  1.1  christos     {
   3229  1.1  christos       for (each_func = each_unit->function_table;
   3230  1.1  christos 	   each_func;
   3231  1.1  christos 	   each_func = each_func->prev_func)
   3232  1.1  christos 	{
   3233  1.1  christos 	  if (!each_func->name)
   3234  1.1  christos 	    continue;
   3235  1.1  christos 	  node = lookup_info_hash_table (stash->funcinfo_hash_table,
   3236  1.1  christos 					 each_func->name);
   3237  1.1  christos 	  BFD_ASSERT (node);
   3238  1.1  christos 	  found = FALSE;
   3239  1.1  christos 	  while (node && !found)
   3240  1.1  christos 	    {
   3241  1.1  christos 	      found = node->info == each_func;
   3242  1.1  christos 	      node = node->next;
   3243  1.1  christos 	    }
   3244  1.1  christos 	  BFD_ASSERT (found);
   3245  1.1  christos 	}
   3246  1.1  christos 
   3247  1.1  christos       for (each_var = each_unit->variable_table;
   3248  1.1  christos 	   each_var;
   3249  1.1  christos 	   each_var = each_var->prev_var)
   3250  1.1  christos 	{
   3251  1.1  christos 	  if (!each_var->name || !each_var->file || each_var->stack)
   3252  1.1  christos 	    continue;
   3253  1.1  christos 	  node = lookup_info_hash_table (stash->varinfo_hash_table,
   3254  1.1  christos 					 each_var->name);
   3255  1.1  christos 	  BFD_ASSERT (node);
   3256  1.1  christos 	  found = FALSE;
   3257  1.1  christos 	  while (node && !found)
   3258  1.1  christos 	    {
   3259  1.1  christos 	      found = node->info == each_var;
   3260  1.1  christos 	      node = node->next;
   3261  1.1  christos 	    }
   3262  1.1  christos 	  BFD_ASSERT (found);
   3263  1.1  christos 	}
   3264  1.1  christos     }
   3265  1.1  christos }
   3266  1.1  christos 
   3267  1.1  christos /* Check to see if we want to enable the info hash tables, which consume
   3268  1.1  christos    quite a bit of memory.  Currently we only check the number times
   3269  1.1  christos    bfd_dwarf2_find_line is called.  In the future, we may also want to
   3270  1.1  christos    take the number of symbols into account.  */
   3271  1.1  christos 
   3272  1.1  christos static void
   3273  1.1  christos stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
   3274  1.1  christos {
   3275  1.1  christos   BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
   3276  1.1  christos 
   3277  1.1  christos   if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
   3278  1.1  christos     return;
   3279  1.1  christos 
   3280  1.1  christos   /* FIXME: Maybe we should check the reduce_memory_overheads
   3281  1.1  christos      and optimize fields in the bfd_link_info structure ?  */
   3282  1.1  christos 
   3283  1.1  christos   /* Create hash tables.  */
   3284  1.1  christos   stash->funcinfo_hash_table = create_info_hash_table (abfd);
   3285  1.1  christos   stash->varinfo_hash_table = create_info_hash_table (abfd);
   3286  1.1  christos   if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
   3287  1.1  christos     {
   3288  1.1  christos       /* Turn off info hashes if any allocation above fails.  */
   3289  1.1  christos       stash->info_hash_status = STASH_INFO_HASH_DISABLED;
   3290  1.1  christos       return;
   3291  1.1  christos     }
   3292  1.1  christos   /* We need a forced update so that the info hash tables will
   3293  1.1  christos      be created even though there is no compilation unit.  That
   3294  1.1  christos      happens if STASH_INFO_HASH_TRIGGER is 0.  */
   3295  1.1  christos   stash_maybe_update_info_hash_tables (stash);
   3296  1.1  christos   stash->info_hash_status = STASH_INFO_HASH_ON;
   3297  1.1  christos }
   3298  1.1  christos 
   3299  1.1  christos /* Find the file and line associated with a symbol and address using the
   3300  1.1  christos    info hash tables of a stash. If there is a match, the function returns
   3301  1.1  christos    TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
   3302  1.1  christos    otherwise it returns FALSE.  */
   3303  1.1  christos 
   3304  1.1  christos static bfd_boolean
   3305  1.1  christos stash_find_line_fast (struct dwarf2_debug *stash,
   3306  1.1  christos 		      asymbol *sym,
   3307  1.1  christos 		      bfd_vma addr,
   3308  1.1  christos 		      const char **filename_ptr,
   3309  1.1  christos 		      unsigned int *linenumber_ptr)
   3310  1.1  christos {
   3311  1.1  christos   BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
   3312  1.1  christos 
   3313  1.1  christos   if (sym->flags & BSF_FUNCTION)
   3314  1.1  christos     return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
   3315  1.1  christos 				      filename_ptr, linenumber_ptr);
   3316  1.1  christos   return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
   3317  1.1  christos 				   filename_ptr, linenumber_ptr);
   3318  1.1  christos }
   3319  1.1  christos 
   3320  1.1  christos /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
   3321  1.1  christos    If DEBUG_BFD is not specified, we read debug information from ABFD
   3322  1.1  christos    or its gnu_debuglink. The results will be stored in PINFO.
   3323  1.1  christos    The function returns TRUE iff debug information is ready.  */
   3324  1.1  christos 
   3325  1.1  christos bfd_boolean
   3326  1.1  christos _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
   3327  1.1  christos                               const struct dwarf_debug_section *debug_sections,
   3328  1.1  christos                               asymbol **symbols,
   3329  1.1  christos                               void **pinfo)
   3330  1.1  christos {
   3331  1.1  christos   bfd_size_type amt = sizeof (struct dwarf2_debug);
   3332  1.1  christos   bfd_size_type total_size;
   3333  1.1  christos   asection *msec;
   3334  1.1  christos   struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
   3335  1.1  christos 
   3336  1.1  christos   if (stash != NULL)
   3337  1.1  christos     return TRUE;
   3338  1.1  christos 
   3339  1.1  christos   stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
   3340  1.1  christos   if (! stash)
   3341  1.1  christos     return FALSE;
   3342  1.1  christos   stash->debug_sections = debug_sections;
   3343  1.1  christos   stash->syms = symbols;
   3344  1.1  christos 
   3345  1.1  christos   *pinfo = stash;
   3346  1.1  christos 
   3347  1.1  christos   if (debug_bfd == NULL)
   3348  1.1  christos     debug_bfd = abfd;
   3349  1.1  christos 
   3350  1.1  christos   msec = find_debug_info (debug_bfd, debug_sections, NULL);
   3351  1.1  christos   if (msec == NULL && abfd == debug_bfd)
   3352  1.1  christos     {
   3353  1.1  christos       char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
   3354  1.1  christos 
   3355  1.1  christos       if (debug_filename == NULL)
   3356  1.1  christos 	/* No dwarf2 info, and no gnu_debuglink to follow.
   3357  1.1  christos 	   Note that at this point the stash has been allocated, but
   3358  1.1  christos 	   contains zeros.  This lets future calls to this function
   3359  1.1  christos 	   fail more quickly.  */
   3360  1.1  christos 	return FALSE;
   3361  1.1  christos 
   3362  1.1  christos       if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
   3363  1.1  christos 	  || ! bfd_check_format (debug_bfd, bfd_object)
   3364  1.1  christos 	  || (msec = find_debug_info (debug_bfd,
   3365  1.1  christos 				      debug_sections, NULL)) == NULL)
   3366  1.1  christos 	{
   3367  1.1  christos 	  if (debug_bfd)
   3368  1.1  christos 	    bfd_close (debug_bfd);
   3369  1.1  christos 	  /* FIXME: Should we report our failure to follow the debuglink ?  */
   3370  1.1  christos 	  free (debug_filename);
   3371  1.1  christos 	  return FALSE;
   3372  1.1  christos 	}
   3373  1.1  christos       stash->close_on_cleanup = TRUE;
   3374  1.1  christos     }
   3375  1.1  christos   stash->bfd_ptr = debug_bfd;
   3376  1.1  christos 
   3377  1.1  christos   /* There can be more than one DWARF2 info section in a BFD these
   3378  1.1  christos      days.  First handle the easy case when there's only one.  If
   3379  1.1  christos      there's more than one, try case two: none of the sections is
   3380  1.1  christos      compressed.  In that case, read them all in and produce one
   3381  1.1  christos      large stash.  We do this in two passes - in the first pass we
   3382  1.1  christos      just accumulate the section sizes, and in the second pass we
   3383  1.1  christos      read in the section's contents.  (The allows us to avoid
   3384  1.1  christos      reallocing the data as we add sections to the stash.)  If
   3385  1.1  christos      some or all sections are compressed, then do things the slow
   3386  1.1  christos      way, with a bunch of reallocs.  */
   3387  1.1  christos 
   3388  1.1  christos   if (! find_debug_info (debug_bfd, debug_sections, msec))
   3389  1.1  christos     {
   3390  1.1  christos       /* Case 1: only one info section.  */
   3391  1.1  christos       total_size = msec->size;
   3392  1.1  christos       if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
   3393  1.1  christos 			  symbols, 0,
   3394  1.1  christos 			  &stash->info_ptr_memory, &total_size))
   3395  1.1  christos 	return FALSE;
   3396  1.1  christos     }
   3397  1.1  christos   else
   3398  1.1  christos     {
   3399  1.1  christos       /* Case 2: multiple sections.  */
   3400  1.1  christos       for (total_size = 0;
   3401  1.1  christos 	   msec;
   3402  1.1  christos 	   msec = find_debug_info (debug_bfd, debug_sections, msec))
   3403  1.1  christos 	total_size += msec->size;
   3404  1.1  christos 
   3405  1.1  christos       stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
   3406  1.1  christos       if (stash->info_ptr_memory == NULL)
   3407  1.1  christos 	return FALSE;
   3408  1.1  christos 
   3409  1.1  christos       total_size = 0;
   3410  1.1  christos       for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
   3411  1.1  christos 	   msec;
   3412  1.1  christos 	   msec = find_debug_info (debug_bfd, debug_sections, msec))
   3413  1.1  christos 	{
   3414  1.1  christos 	  bfd_size_type size;
   3415  1.1  christos 
   3416  1.1  christos 	  size = msec->size;
   3417  1.1  christos 	  if (size == 0)
   3418  1.1  christos 	    continue;
   3419  1.1  christos 
   3420  1.1  christos 	  if (!(bfd_simple_get_relocated_section_contents
   3421  1.1  christos 		(debug_bfd, msec, stash->info_ptr_memory + total_size,
   3422  1.1  christos 		 symbols)))
   3423  1.1  christos 	    return FALSE;
   3424  1.1  christos 
   3425  1.1  christos 	  total_size += size;
   3426  1.1  christos 	}
   3427  1.1  christos     }
   3428  1.1  christos 
   3429  1.1  christos   stash->info_ptr = stash->info_ptr_memory;
   3430  1.1  christos   stash->info_ptr_end = stash->info_ptr + total_size;
   3431  1.1  christos   stash->sec = find_debug_info (debug_bfd, debug_sections, NULL);
   3432  1.1  christos   stash->sec_info_ptr = stash->info_ptr;
   3433  1.1  christos   return TRUE;
   3434  1.1  christos }
   3435  1.1  christos 
   3436  1.1  christos /* Find the source code location of SYMBOL.  If SYMBOL is NULL
   3437  1.1  christos    then find the nearest source code location corresponding to
   3438  1.1  christos    the address SECTION + OFFSET.
   3439  1.1  christos    Returns TRUE if the line is found without error and fills in
   3440  1.1  christos    FILENAME_PTR and LINENUMBER_PTR.  In the case where SYMBOL was
   3441  1.1  christos    NULL the FUNCTIONNAME_PTR is also filled in.
   3442  1.1  christos    SYMBOLS contains the symbol table for ABFD.
   3443  1.1  christos    DEBUG_SECTIONS contains the name of the dwarf debug sections.
   3444  1.1  christos    ADDR_SIZE is the number of bytes in the initial .debug_info length
   3445  1.1  christos    field and in the abbreviation offset, or zero to indicate that the
   3446  1.1  christos    default value should be used.  */
   3447  1.1  christos 
   3448  1.1  christos static bfd_boolean
   3449  1.1  christos find_line (bfd *abfd,
   3450  1.1  christos            const struct dwarf_debug_section *debug_sections,
   3451  1.1  christos 	   asection *section,
   3452  1.1  christos 	   bfd_vma offset,
   3453  1.1  christos 	   asymbol *symbol,
   3454  1.1  christos 	   asymbol **symbols,
   3455  1.1  christos 	   const char **filename_ptr,
   3456  1.1  christos 	   const char **functionname_ptr,
   3457  1.1  christos 	   unsigned int *linenumber_ptr,
   3458  1.1  christos 	   unsigned int *discriminator_ptr,
   3459  1.1  christos 	   unsigned int addr_size,
   3460  1.1  christos 	   void **pinfo)
   3461  1.1  christos {
   3462  1.1  christos   /* Read each compilation unit from the section .debug_info, and check
   3463  1.1  christos      to see if it contains the address we are searching for.  If yes,
   3464  1.1  christos      lookup the address, and return the line number info.  If no, go
   3465  1.1  christos      on to the next compilation unit.
   3466  1.1  christos 
   3467  1.1  christos      We keep a list of all the previously read compilation units, and
   3468  1.1  christos      a pointer to the next un-read compilation unit.  Check the
   3469  1.1  christos      previously read units before reading more.  */
   3470  1.1  christos   struct dwarf2_debug *stash;
   3471  1.1  christos   /* What address are we looking for?  */
   3472  1.1  christos   bfd_vma addr;
   3473  1.1  christos   struct comp_unit* each;
   3474  1.1  christos   bfd_boolean found = FALSE;
   3475  1.1  christos   bfd_boolean do_line;
   3476  1.1  christos 
   3477  1.1  christos   *filename_ptr = NULL;
   3478  1.1  christos   if (functionname_ptr != NULL)
   3479  1.1  christos     *functionname_ptr = NULL;
   3480  1.1  christos   *linenumber_ptr = 0;
   3481  1.1  christos   if (discriminator_ptr)
   3482  1.1  christos     *discriminator_ptr = 0;
   3483  1.1  christos 
   3484  1.1  christos   if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL,
   3485  1.1  christos 				      debug_sections, symbols, pinfo))
   3486  1.1  christos     return FALSE;
   3487  1.1  christos 
   3488  1.1  christos   stash = (struct dwarf2_debug *) *pinfo;
   3489  1.1  christos 
   3490  1.1  christos   /* In a relocatable file, 2 functions may have the same address.
   3491  1.1  christos      We change the section vma so that they won't overlap.  */
   3492  1.1  christos   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
   3493  1.1  christos     {
   3494  1.1  christos       if (! place_sections (abfd, stash))
   3495  1.1  christos 	return FALSE;
   3496  1.1  christos     }
   3497  1.1  christos 
   3498  1.1  christos   do_line = (section == NULL
   3499  1.1  christos 	     && offset == 0
   3500  1.1  christos 	     && functionname_ptr == NULL
   3501  1.1  christos 	     && symbol != NULL);
   3502  1.1  christos   if (do_line)
   3503  1.1  christos     {
   3504  1.1  christos       addr = symbol->value;
   3505  1.1  christos       section = bfd_get_section (symbol);
   3506  1.1  christos     }
   3507  1.1  christos   else if (section != NULL
   3508  1.1  christos 	   && functionname_ptr != NULL
   3509  1.1  christos 	   && symbol == NULL)
   3510  1.1  christos     addr = offset;
   3511  1.1  christos   else
   3512  1.1  christos     abort ();
   3513  1.1  christos 
   3514  1.1  christos   if (section->output_section)
   3515  1.1  christos     addr += section->output_section->vma + section->output_offset;
   3516  1.1  christos   else
   3517  1.1  christos     addr += section->vma;
   3518  1.1  christos 
   3519  1.1  christos   /* A null info_ptr indicates that there is no dwarf2 info
   3520  1.1  christos      (or that an error occured while setting up the stash).  */
   3521  1.1  christos   if (! stash->info_ptr)
   3522  1.1  christos     return FALSE;
   3523  1.1  christos 
   3524  1.1  christos   stash->inliner_chain = NULL;
   3525  1.1  christos 
   3526  1.1  christos   /* Check the previously read comp. units first.  */
   3527  1.1  christos   if (do_line)
   3528  1.1  christos     {
   3529  1.1  christos       /* The info hash tables use quite a bit of memory.  We may not want to
   3530  1.1  christos 	 always use them.  We use some heuristics to decide if and when to
   3531  1.1  christos 	 turn it on.  */
   3532  1.1  christos       if (stash->info_hash_status == STASH_INFO_HASH_OFF)
   3533  1.1  christos 	stash_maybe_enable_info_hash_tables (abfd, stash);
   3534  1.1  christos 
   3535  1.1  christos       /* Keep info hash table up to date if they are available.  Note that we
   3536  1.1  christos 	 may disable the hash tables if there is any error duing update. */
   3537  1.1  christos       if (stash->info_hash_status == STASH_INFO_HASH_ON)
   3538  1.1  christos 	stash_maybe_update_info_hash_tables (stash);
   3539  1.1  christos 
   3540  1.1  christos       if (stash->info_hash_status == STASH_INFO_HASH_ON)
   3541  1.1  christos 	{
   3542  1.1  christos 	  found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
   3543  1.1  christos 					linenumber_ptr);
   3544  1.1  christos 	  if (found)
   3545  1.1  christos 	    goto done;
   3546  1.1  christos 	}
   3547  1.1  christos       else
   3548  1.1  christos 	{
   3549  1.1  christos 	  /* Check the previously read comp. units first.  */
   3550  1.1  christos 	  for (each = stash->all_comp_units; each; each = each->next_unit)
   3551  1.1  christos 	    if ((symbol->flags & BSF_FUNCTION) == 0
   3552  1.1  christos 		|| each->arange.high == 0
   3553  1.1  christos 		|| comp_unit_contains_address (each, addr))
   3554  1.1  christos 	      {
   3555  1.1  christos 		found = comp_unit_find_line (each, symbol, addr, filename_ptr,
   3556  1.1  christos 					     linenumber_ptr, stash);
   3557  1.1  christos 		if (found)
   3558  1.1  christos 		  goto done;
   3559  1.1  christos 	      }
   3560  1.1  christos 	}
   3561  1.1  christos     }
   3562  1.1  christos   else
   3563  1.1  christos     {
   3564  1.1  christos       bfd_vma min_range = (bfd_vma) -1;
   3565  1.1  christos       const char * local_filename = NULL;
   3566  1.1  christos       const char * local_functionname = NULL;
   3567  1.1  christos       unsigned int local_linenumber = 0;
   3568  1.1  christos       unsigned int local_discriminator = 0;
   3569  1.1  christos 
   3570  1.1  christos       for (each = stash->all_comp_units; each; each = each->next_unit)
   3571  1.1  christos 	{
   3572  1.1  christos 	  bfd_vma range = (bfd_vma) -1;
   3573  1.1  christos 
   3574  1.1  christos 	  found = ((each->arange.high == 0
   3575  1.1  christos 		    || comp_unit_contains_address (each, addr))
   3576  1.1  christos 		   && (range = comp_unit_find_nearest_line (each, addr,
   3577  1.1  christos 							    & local_filename,
   3578  1.1  christos 							    & local_functionname,
   3579  1.1  christos 							    & local_linenumber,
   3580  1.1  christos 							    & local_discriminator,
   3581  1.1  christos 							    stash)) != 0);
   3582  1.1  christos 	  if (found)
   3583  1.1  christos 	    {
   3584  1.1  christos 	      /* PRs 15935 15994: Bogus debug information may have provided us
   3585  1.1  christos 		 with an erroneous match.  We attempt to counter this by
   3586  1.1  christos 		 selecting the match that has the smallest address range
   3587  1.1  christos 		 associated with it.  (We are assuming that corrupt debug info
   3588  1.1  christos 		 will tend to result in extra large address ranges rather than
   3589  1.1  christos 		 extra small ranges).
   3590  1.1  christos 
   3591  1.1  christos 		 This does mean that we scan through all of the CUs associated
   3592  1.1  christos 		 with the bfd each time this function is called.  But this does
   3593  1.1  christos 		 have the benefit of producing consistent results every time the
   3594  1.1  christos 		 function is called.  */
   3595  1.1  christos 	      if (range <= min_range)
   3596  1.1  christos 		{
   3597  1.1  christos 		  if (filename_ptr && local_filename)
   3598  1.1  christos 		    * filename_ptr = local_filename;
   3599  1.1  christos 		  if (functionname_ptr && local_functionname)
   3600  1.1  christos 		    * functionname_ptr = local_functionname;
   3601  1.1  christos 		  if (discriminator_ptr && local_discriminator)
   3602  1.1  christos 		    * discriminator_ptr = local_discriminator;
   3603  1.1  christos 		  if (local_linenumber)
   3604  1.1  christos 		    * linenumber_ptr = local_linenumber;
   3605  1.1  christos 		  min_range = range;
   3606  1.1  christos 		}
   3607  1.1  christos 	    }
   3608  1.1  christos 	}
   3609  1.1  christos 
   3610  1.1  christos       if (* linenumber_ptr)
   3611  1.1  christos 	{
   3612  1.1  christos 	  found = TRUE;
   3613  1.1  christos 	  goto done;
   3614  1.1  christos 	}
   3615  1.1  christos     }
   3616  1.1  christos 
   3617  1.1  christos   /* The DWARF2 spec says that the initial length field, and the
   3618  1.1  christos      offset of the abbreviation table, should both be 4-byte values.
   3619  1.1  christos      However, some compilers do things differently.  */
   3620  1.1  christos   if (addr_size == 0)
   3621  1.1  christos     addr_size = 4;
   3622  1.1  christos   BFD_ASSERT (addr_size == 4 || addr_size == 8);
   3623  1.1  christos 
   3624  1.1  christos   /* Read each remaining comp. units checking each as they are read.  */
   3625  1.1  christos   while (stash->info_ptr < stash->info_ptr_end)
   3626  1.1  christos     {
   3627  1.1  christos       bfd_vma length;
   3628  1.1  christos       unsigned int offset_size = addr_size;
   3629  1.1  christos       bfd_byte *info_ptr_unit = stash->info_ptr;
   3630  1.1  christos 
   3631  1.1  christos       length = read_4_bytes (stash->bfd_ptr, stash->info_ptr);
   3632  1.1  christos       /* A 0xffffff length is the DWARF3 way of indicating
   3633  1.1  christos 	 we use 64-bit offsets, instead of 32-bit offsets.  */
   3634  1.1  christos       if (length == 0xffffffff)
   3635  1.1  christos 	{
   3636  1.1  christos 	  offset_size = 8;
   3637  1.1  christos 	  length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4);
   3638  1.1  christos 	  stash->info_ptr += 12;
   3639  1.1  christos 	}
   3640  1.1  christos       /* A zero length is the IRIX way of indicating 64-bit offsets,
   3641  1.1  christos 	 mostly because the 64-bit length will generally fit in 32
   3642  1.1  christos 	 bits, and the endianness helps.  */
   3643  1.1  christos       else if (length == 0)
   3644  1.1  christos 	{
   3645  1.1  christos 	  offset_size = 8;
   3646  1.1  christos 	  length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4);
   3647  1.1  christos 	  stash->info_ptr += 8;
   3648  1.1  christos 	}
   3649  1.1  christos       /* In the absence of the hints above, we assume 32-bit DWARF2
   3650  1.1  christos 	 offsets even for targets with 64-bit addresses, because:
   3651  1.1  christos 	   a) most of the time these targets will not have generated
   3652  1.1  christos 	      more than 2Gb of debug info and so will not need 64-bit
   3653  1.1  christos 	      offsets,
   3654  1.1  christos 	 and
   3655  1.1  christos 	   b) if they do use 64-bit offsets but they are not using
   3656  1.1  christos 	      the size hints that are tested for above then they are
   3657  1.1  christos 	      not conforming to the DWARF3 standard anyway.  */
   3658  1.1  christos       else if (addr_size == 8)
   3659  1.1  christos 	{
   3660  1.1  christos 	  offset_size = 4;
   3661  1.1  christos 	  stash->info_ptr += 4;
   3662  1.1  christos 	}
   3663  1.1  christos       else
   3664  1.1  christos 	stash->info_ptr += 4;
   3665  1.1  christos 
   3666  1.1  christos       if (length > 0)
   3667  1.1  christos 	{
   3668  1.1  christos 	  each = parse_comp_unit (stash, length, info_ptr_unit,
   3669  1.1  christos 				  offset_size);
   3670  1.1  christos 	  if (!each)
   3671  1.1  christos 	    /* The dwarf information is damaged, don't trust it any
   3672  1.1  christos 	       more.  */
   3673  1.1  christos 	    break;
   3674  1.1  christos 	  stash->info_ptr += length;
   3675  1.1  christos 
   3676  1.1  christos 	  if (stash->all_comp_units)
   3677  1.1  christos 	    stash->all_comp_units->prev_unit = each;
   3678  1.1  christos 	  else
   3679  1.1  christos 	    stash->last_comp_unit = each;
   3680  1.1  christos 
   3681  1.1  christos 	  each->next_unit = stash->all_comp_units;
   3682  1.1  christos 	  stash->all_comp_units = each;
   3683  1.1  christos 
   3684  1.1  christos 	  /* DW_AT_low_pc and DW_AT_high_pc are optional for
   3685  1.1  christos 	     compilation units.  If we don't have them (i.e.,
   3686  1.1  christos 	     unit->high == 0), we need to consult the line info table
   3687  1.1  christos 	     to see if a compilation unit contains the given
   3688  1.1  christos 	     address.  */
   3689  1.1  christos 	  if (do_line)
   3690  1.1  christos 	    found = (((symbol->flags & BSF_FUNCTION) == 0
   3691  1.1  christos 		      || each->arange.high == 0
   3692  1.1  christos 		      || comp_unit_contains_address (each, addr))
   3693  1.1  christos 		     && comp_unit_find_line (each, symbol, addr,
   3694  1.1  christos 					     filename_ptr,
   3695  1.1  christos 					     linenumber_ptr,
   3696  1.1  christos 					     stash));
   3697  1.1  christos 	  else
   3698  1.1  christos 	    found = ((each->arange.high == 0
   3699  1.1  christos 		      || comp_unit_contains_address (each, addr))
   3700  1.1  christos 		     && comp_unit_find_nearest_line (each, addr,
   3701  1.1  christos 						     filename_ptr,
   3702  1.1  christos 						     functionname_ptr,
   3703  1.1  christos 						     linenumber_ptr,
   3704  1.1  christos 						     discriminator_ptr,
   3705  1.1  christos 						     stash)) > 0;
   3706  1.1  christos 
   3707  1.1  christos 	  if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
   3708  1.1  christos 	      == stash->sec->size)
   3709  1.1  christos 	    {
   3710  1.1  christos 	      stash->sec = find_debug_info (stash->bfd_ptr, debug_sections,
   3711  1.1  christos                                             stash->sec);
   3712  1.1  christos 	      stash->sec_info_ptr = stash->info_ptr;
   3713  1.1  christos 	    }
   3714  1.1  christos 
   3715  1.1  christos 	  if (found)
   3716  1.1  christos 	    goto done;
   3717  1.1  christos 	}
   3718  1.1  christos     }
   3719  1.1  christos 
   3720  1.1  christos  done:
   3721  1.1  christos   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
   3722  1.1  christos     unset_sections (stash);
   3723  1.1  christos 
   3724  1.1  christos   return found;
   3725  1.1  christos }
   3726  1.1  christos 
   3727  1.1  christos /* The DWARF2 version of find_nearest_line.
   3728  1.1  christos    Return TRUE if the line is found without error.  */
   3729  1.1  christos 
   3730  1.1  christos bfd_boolean
   3731  1.1  christos _bfd_dwarf2_find_nearest_line (bfd *abfd,
   3732  1.1  christos                                const struct dwarf_debug_section *debug_sections,
   3733  1.1  christos 			       asection *section,
   3734  1.1  christos 			       asymbol **symbols,
   3735  1.1  christos 			       bfd_vma offset,
   3736  1.1  christos 			       const char **filename_ptr,
   3737  1.1  christos 			       const char **functionname_ptr,
   3738  1.1  christos 			       unsigned int *linenumber_ptr,
   3739  1.1  christos                                unsigned int *discriminator_ptr,
   3740  1.1  christos 			       unsigned int addr_size,
   3741  1.1  christos 			       void **pinfo)
   3742  1.1  christos {
   3743  1.1  christos   return find_line (abfd, debug_sections, section, offset, NULL, symbols,
   3744  1.1  christos                     filename_ptr, functionname_ptr, linenumber_ptr,
   3745  1.1  christos                     discriminator_ptr, addr_size, pinfo);
   3746  1.1  christos }
   3747  1.1  christos 
   3748  1.1  christos /* The DWARF2 version of find_line.
   3749  1.1  christos    Return TRUE if the line is found without error.  */
   3750  1.1  christos 
   3751  1.1  christos bfd_boolean
   3752  1.1  christos _bfd_dwarf2_find_line (bfd *abfd,
   3753  1.1  christos 		       asymbol **symbols,
   3754  1.1  christos 		       asymbol *symbol,
   3755  1.1  christos 		       const char **filename_ptr,
   3756  1.1  christos 		       unsigned int *linenumber_ptr,
   3757  1.1  christos                        unsigned int *discriminator_ptr,
   3758  1.1  christos 		       unsigned int addr_size,
   3759  1.1  christos 		       void **pinfo)
   3760  1.1  christos {
   3761  1.1  christos   return find_line (abfd, dwarf_debug_sections, NULL, 0, symbol, symbols,
   3762  1.1  christos                     filename_ptr, NULL, linenumber_ptr, discriminator_ptr,
   3763  1.1  christos                     addr_size, pinfo);
   3764  1.1  christos }
   3765  1.1  christos 
   3766  1.1  christos bfd_boolean
   3767  1.1  christos _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
   3768  1.1  christos 			       const char **filename_ptr,
   3769  1.1  christos 			       const char **functionname_ptr,
   3770  1.1  christos 			       unsigned int *linenumber_ptr,
   3771  1.1  christos 			       void **pinfo)
   3772  1.1  christos {
   3773  1.1  christos   struct dwarf2_debug *stash;
   3774  1.1  christos 
   3775  1.1  christos   stash = (struct dwarf2_debug *) *pinfo;
   3776  1.1  christos   if (stash)
   3777  1.1  christos     {
   3778  1.1  christos       struct funcinfo *func = stash->inliner_chain;
   3779  1.1  christos 
   3780  1.1  christos       if (func && func->caller_func)
   3781  1.1  christos 	{
   3782  1.1  christos 	  *filename_ptr = func->caller_file;
   3783  1.1  christos 	  *functionname_ptr = func->caller_func->name;
   3784  1.1  christos 	  *linenumber_ptr = func->caller_line;
   3785  1.1  christos 	  stash->inliner_chain = func->caller_func;
   3786  1.1  christos 	  return TRUE;
   3787  1.1  christos 	}
   3788  1.1  christos     }
   3789  1.1  christos 
   3790  1.1  christos   return FALSE;
   3791  1.1  christos }
   3792  1.1  christos 
   3793  1.1  christos void
   3794  1.1  christos _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
   3795  1.1  christos {
   3796  1.1  christos   struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
   3797  1.1  christos   struct comp_unit *each;
   3798  1.1  christos 
   3799  1.1  christos   if (abfd == NULL || stash == NULL)
   3800  1.1  christos     return;
   3801  1.1  christos 
   3802  1.1  christos   for (each = stash->all_comp_units; each; each = each->next_unit)
   3803  1.1  christos     {
   3804  1.1  christos       struct abbrev_info **abbrevs = each->abbrevs;
   3805  1.1  christos       struct funcinfo *function_table = each->function_table;
   3806  1.1  christos       struct varinfo *variable_table = each->variable_table;
   3807  1.1  christos       size_t i;
   3808  1.1  christos 
   3809  1.1  christos       for (i = 0; i < ABBREV_HASH_SIZE; i++)
   3810  1.1  christos 	{
   3811  1.1  christos 	  struct abbrev_info *abbrev = abbrevs[i];
   3812  1.1  christos 
   3813  1.1  christos 	  while (abbrev)
   3814  1.1  christos 	    {
   3815  1.1  christos 	      free (abbrev->attrs);
   3816  1.1  christos 	      abbrev = abbrev->next;
   3817  1.1  christos 	    }
   3818  1.1  christos 	}
   3819  1.1  christos 
   3820  1.1  christos       if (each->line_table)
   3821  1.1  christos 	{
   3822  1.1  christos 	  free (each->line_table->dirs);
   3823  1.1  christos 	  free (each->line_table->files);
   3824  1.1  christos 	}
   3825  1.1  christos 
   3826  1.1  christos       while (function_table)
   3827  1.1  christos 	{
   3828  1.1  christos 	  if (function_table->file)
   3829  1.1  christos 	    {
   3830  1.1  christos 	      free (function_table->file);
   3831  1.1  christos 	      function_table->file = NULL;
   3832  1.1  christos 	    }
   3833  1.1  christos 
   3834  1.1  christos 	  if (function_table->caller_file)
   3835  1.1  christos 	    {
   3836  1.1  christos 	      free (function_table->caller_file);
   3837  1.1  christos 	      function_table->caller_file = NULL;
   3838  1.1  christos 	    }
   3839  1.1  christos 	  function_table = function_table->prev_func;
   3840  1.1  christos 	}
   3841  1.1  christos 
   3842  1.1  christos       while (variable_table)
   3843  1.1  christos 	{
   3844  1.1  christos 	  if (variable_table->file)
   3845  1.1  christos 	    {
   3846  1.1  christos 	      free (variable_table->file);
   3847  1.1  christos 	      variable_table->file = NULL;
   3848  1.1  christos 	    }
   3849  1.1  christos 
   3850  1.1  christos 	  variable_table = variable_table->prev_var;
   3851  1.1  christos 	}
   3852  1.1  christos     }
   3853  1.1  christos 
   3854  1.1  christos   if (stash->dwarf_abbrev_buffer)
   3855  1.1  christos     free (stash->dwarf_abbrev_buffer);
   3856  1.1  christos   if (stash->dwarf_line_buffer)
   3857  1.1  christos     free (stash->dwarf_line_buffer);
   3858  1.1  christos   if (stash->dwarf_str_buffer)
   3859  1.1  christos     free (stash->dwarf_str_buffer);
   3860  1.1  christos   if (stash->dwarf_ranges_buffer)
   3861  1.1  christos     free (stash->dwarf_ranges_buffer);
   3862  1.1  christos   if (stash->info_ptr_memory)
   3863  1.1  christos     free (stash->info_ptr_memory);
   3864  1.1  christos   if (stash->close_on_cleanup)
   3865  1.1  christos     bfd_close (stash->bfd_ptr);
   3866  1.1  christos   if (stash->alt_dwarf_str_buffer)
   3867  1.1  christos     free (stash->alt_dwarf_str_buffer);
   3868  1.1  christos   if (stash->alt_dwarf_info_buffer)
   3869  1.1  christos     free (stash->alt_dwarf_info_buffer);
   3870  1.1  christos   if (stash->alt_bfd_ptr)
   3871  1.1  christos     bfd_close (stash->alt_bfd_ptr);
   3872  1.1  christos }
   3873