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