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