xref: /dflybsd-src/contrib/gdb-7/bfd/dwarf2.c (revision c50c785cb49e9377ca78104c5540c7b33f768771)
15796c8dcSSimon Schubert /* DWARF 2 support.
25796c8dcSSimon Schubert    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3cf7f2e2dSJohn Marino    2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
65796c8dcSSimon Schubert    (gavin@cygnus.com).
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    From the dwarf2read.c header:
95796c8dcSSimon Schubert    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
105796c8dcSSimon Schubert    Inc.  with support from Florida State University (under contract
115796c8dcSSimon Schubert    with the Ada Joint Program Office), and Silicon Graphics, Inc.
125796c8dcSSimon Schubert    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
135796c8dcSSimon Schubert    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
145796c8dcSSimon Schubert    support in dwarfread.c
155796c8dcSSimon Schubert 
165796c8dcSSimon Schubert    This file is part of BFD.
175796c8dcSSimon Schubert 
185796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
195796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
205796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or (at
215796c8dcSSimon Schubert    your option) any later version.
225796c8dcSSimon Schubert 
235796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful, but
245796c8dcSSimon Schubert    WITHOUT ANY WARRANTY; without even the implied warranty of
255796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
265796c8dcSSimon Schubert    General Public License for more details.
275796c8dcSSimon Schubert 
285796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
295796c8dcSSimon Schubert    along with this program; if not, write to the Free Software
305796c8dcSSimon Schubert    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
315796c8dcSSimon Schubert    MA 02110-1301, USA.  */
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert #include "sysdep.h"
345796c8dcSSimon Schubert #include "bfd.h"
355796c8dcSSimon Schubert #include "libiberty.h"
365796c8dcSSimon Schubert #include "libbfd.h"
375796c8dcSSimon Schubert #include "elf-bfd.h"
385796c8dcSSimon Schubert #include "dwarf2.h"
395796c8dcSSimon Schubert 
405796c8dcSSimon Schubert /* The data in the .debug_line statement prologue looks like this.  */
415796c8dcSSimon Schubert 
425796c8dcSSimon Schubert struct line_head
435796c8dcSSimon Schubert {
445796c8dcSSimon Schubert   bfd_vma total_length;
455796c8dcSSimon Schubert   unsigned short version;
465796c8dcSSimon Schubert   bfd_vma prologue_length;
475796c8dcSSimon Schubert   unsigned char minimum_instruction_length;
48cf7f2e2dSJohn Marino   unsigned char maximum_ops_per_insn;
495796c8dcSSimon Schubert   unsigned char default_is_stmt;
505796c8dcSSimon Schubert   int line_base;
515796c8dcSSimon Schubert   unsigned char line_range;
525796c8dcSSimon Schubert   unsigned char opcode_base;
535796c8dcSSimon Schubert   unsigned char *standard_opcode_lengths;
545796c8dcSSimon Schubert };
555796c8dcSSimon Schubert 
565796c8dcSSimon Schubert /* Attributes have a name and a value.  */
575796c8dcSSimon Schubert 
585796c8dcSSimon Schubert struct attribute
595796c8dcSSimon Schubert {
605796c8dcSSimon Schubert   enum dwarf_attribute name;
615796c8dcSSimon Schubert   enum dwarf_form form;
625796c8dcSSimon Schubert   union
635796c8dcSSimon Schubert   {
645796c8dcSSimon Schubert     char *str;
655796c8dcSSimon Schubert     struct dwarf_block *blk;
665796c8dcSSimon Schubert     bfd_uint64_t val;
675796c8dcSSimon Schubert     bfd_int64_t sval;
685796c8dcSSimon Schubert   }
695796c8dcSSimon Schubert   u;
705796c8dcSSimon Schubert };
715796c8dcSSimon Schubert 
725796c8dcSSimon Schubert /* Blocks are a bunch of untyped bytes.  */
735796c8dcSSimon Schubert struct dwarf_block
745796c8dcSSimon Schubert {
755796c8dcSSimon Schubert   unsigned int size;
765796c8dcSSimon Schubert   bfd_byte *data;
775796c8dcSSimon Schubert };
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert struct adjusted_section
805796c8dcSSimon Schubert {
815796c8dcSSimon Schubert   asection *section;
825796c8dcSSimon Schubert   bfd_vma adj_vma;
835796c8dcSSimon Schubert };
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert struct dwarf2_debug
865796c8dcSSimon Schubert {
875796c8dcSSimon Schubert   /* A list of all previously read comp_units.  */
885796c8dcSSimon Schubert   struct comp_unit *all_comp_units;
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert   /* Last comp unit in list above.  */
915796c8dcSSimon Schubert   struct comp_unit *last_comp_unit;
925796c8dcSSimon Schubert 
935796c8dcSSimon Schubert   /* The next unread compilation unit within the .debug_info section.
945796c8dcSSimon Schubert      Zero indicates that the .debug_info section has not been loaded
955796c8dcSSimon Schubert      into a buffer yet.  */
965796c8dcSSimon Schubert   bfd_byte *info_ptr;
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert   /* Pointer to the end of the .debug_info section memory buffer.  */
995796c8dcSSimon Schubert   bfd_byte *info_ptr_end;
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert   /* Pointer to the bfd, section and address of the beginning of the
1025796c8dcSSimon Schubert      section.  The bfd might be different than expected because of
1035796c8dcSSimon Schubert      gnu_debuglink sections.  */
1045796c8dcSSimon Schubert   bfd *bfd_ptr;
1055796c8dcSSimon Schubert   asection *sec;
1065796c8dcSSimon Schubert   bfd_byte *sec_info_ptr;
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert   /* A pointer to the memory block allocated for info_ptr.  Neither
1095796c8dcSSimon Schubert      info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
1105796c8dcSSimon Schubert      beginning of the malloc block.  This is used only to free the
1115796c8dcSSimon Schubert      memory later.  */
1125796c8dcSSimon Schubert   bfd_byte *info_ptr_memory;
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert   /* Pointer to the symbol table.  */
1155796c8dcSSimon Schubert   asymbol **syms;
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert   /* Pointer to the .debug_abbrev section loaded into memory.  */
1185796c8dcSSimon Schubert   bfd_byte *dwarf_abbrev_buffer;
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert   /* Length of the loaded .debug_abbrev section.  */
1215796c8dcSSimon Schubert   bfd_size_type dwarf_abbrev_size;
1225796c8dcSSimon Schubert 
1235796c8dcSSimon Schubert   /* Buffer for decode_line_info.  */
1245796c8dcSSimon Schubert   bfd_byte *dwarf_line_buffer;
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert   /* Length of the loaded .debug_line section.  */
1275796c8dcSSimon Schubert   bfd_size_type dwarf_line_size;
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert   /* Pointer to the .debug_str section loaded into memory.  */
1305796c8dcSSimon Schubert   bfd_byte *dwarf_str_buffer;
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert   /* Length of the loaded .debug_str section.  */
1335796c8dcSSimon Schubert   bfd_size_type dwarf_str_size;
1345796c8dcSSimon Schubert 
1355796c8dcSSimon Schubert   /* Pointer to the .debug_ranges section loaded into memory. */
1365796c8dcSSimon Schubert   bfd_byte *dwarf_ranges_buffer;
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert   /* Length of the loaded .debug_ranges section. */
1395796c8dcSSimon Schubert   bfd_size_type dwarf_ranges_size;
1405796c8dcSSimon Schubert 
1415796c8dcSSimon Schubert   /* If the most recent call to bfd_find_nearest_line was given an
1425796c8dcSSimon Schubert      address in an inlined function, preserve a pointer into the
1435796c8dcSSimon Schubert      calling chain for subsequent calls to bfd_find_inliner_info to
1445796c8dcSSimon Schubert      use. */
1455796c8dcSSimon Schubert   struct funcinfo *inliner_chain;
1465796c8dcSSimon Schubert 
1475796c8dcSSimon Schubert   /* Number of sections whose VMA we must adjust.  */
1485796c8dcSSimon Schubert   unsigned int adjusted_section_count;
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert   /* Array of sections with adjusted VMA.  */
1515796c8dcSSimon Schubert   struct adjusted_section *adjusted_sections;
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert   /* Number of times find_line is called.  This is used in
1545796c8dcSSimon Schubert      the heuristic for enabling the info hash tables.  */
1555796c8dcSSimon Schubert   int info_hash_count;
1565796c8dcSSimon Schubert 
1575796c8dcSSimon Schubert #define STASH_INFO_HASH_TRIGGER    100
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert   /* Hash table mapping symbol names to function infos.  */
1605796c8dcSSimon Schubert   struct info_hash_table *funcinfo_hash_table;
1615796c8dcSSimon Schubert 
1625796c8dcSSimon Schubert   /* Hash table mapping symbol names to variable infos.  */
1635796c8dcSSimon Schubert   struct info_hash_table *varinfo_hash_table;
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert   /* Head of comp_unit list in the last hash table update.  */
1665796c8dcSSimon Schubert   struct comp_unit *hash_units_head;
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert   /* Status of info hash.  */
1695796c8dcSSimon Schubert   int info_hash_status;
1705796c8dcSSimon Schubert #define STASH_INFO_HASH_OFF        0
1715796c8dcSSimon Schubert #define STASH_INFO_HASH_ON         1
1725796c8dcSSimon Schubert #define STASH_INFO_HASH_DISABLED   2
1735796c8dcSSimon Schubert };
1745796c8dcSSimon Schubert 
1755796c8dcSSimon Schubert struct arange
1765796c8dcSSimon Schubert {
1775796c8dcSSimon Schubert   struct arange *next;
1785796c8dcSSimon Schubert   bfd_vma low;
1795796c8dcSSimon Schubert   bfd_vma high;
1805796c8dcSSimon Schubert };
1815796c8dcSSimon Schubert 
1825796c8dcSSimon Schubert /* A minimal decoding of DWARF2 compilation units.  We only decode
1835796c8dcSSimon Schubert    what's needed to get to the line number information.  */
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert struct comp_unit
1865796c8dcSSimon Schubert {
1875796c8dcSSimon Schubert   /* Chain the previously read compilation units.  */
1885796c8dcSSimon Schubert   struct comp_unit *next_unit;
1895796c8dcSSimon Schubert 
1905796c8dcSSimon Schubert   /* Likewise, chain the compilation unit read after this one.
1915796c8dcSSimon Schubert      The comp units are stored in reversed reading order.  */
1925796c8dcSSimon Schubert   struct comp_unit *prev_unit;
1935796c8dcSSimon Schubert 
1945796c8dcSSimon Schubert   /* Keep the bfd convenient (for memory allocation).  */
1955796c8dcSSimon Schubert   bfd *abfd;
1965796c8dcSSimon Schubert 
1975796c8dcSSimon Schubert   /* The lowest and highest addresses contained in this compilation
1985796c8dcSSimon Schubert      unit as specified in the compilation unit header.  */
1995796c8dcSSimon Schubert   struct arange arange;
2005796c8dcSSimon Schubert 
2015796c8dcSSimon Schubert   /* The DW_AT_name attribute (for error messages).  */
2025796c8dcSSimon Schubert   char *name;
2035796c8dcSSimon Schubert 
2045796c8dcSSimon Schubert   /* The abbrev hash table.  */
2055796c8dcSSimon Schubert   struct abbrev_info **abbrevs;
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert   /* Note that an error was found by comp_unit_find_nearest_line.  */
2085796c8dcSSimon Schubert   int error;
2095796c8dcSSimon Schubert 
2105796c8dcSSimon Schubert   /* The DW_AT_comp_dir attribute.  */
2115796c8dcSSimon Schubert   char *comp_dir;
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert   /* TRUE if there is a line number table associated with this comp. unit.  */
2145796c8dcSSimon Schubert   int stmtlist;
2155796c8dcSSimon Schubert 
2165796c8dcSSimon Schubert   /* Pointer to the current comp_unit so that we can find a given entry
2175796c8dcSSimon Schubert      by its reference.  */
2185796c8dcSSimon Schubert   bfd_byte *info_ptr_unit;
2195796c8dcSSimon Schubert 
220cf7f2e2dSJohn Marino   /* Pointer to the start of the debug section, for DW_FORM_ref_addr.  */
221cf7f2e2dSJohn Marino   bfd_byte *sec_info_ptr;
222cf7f2e2dSJohn Marino 
2235796c8dcSSimon Schubert   /* The offset into .debug_line of the line number table.  */
2245796c8dcSSimon Schubert   unsigned long line_offset;
2255796c8dcSSimon Schubert 
2265796c8dcSSimon Schubert   /* Pointer to the first child die for the comp unit.  */
2275796c8dcSSimon Schubert   bfd_byte *first_child_die_ptr;
2285796c8dcSSimon Schubert 
2295796c8dcSSimon Schubert   /* The end of the comp unit.  */
2305796c8dcSSimon Schubert   bfd_byte *end_ptr;
2315796c8dcSSimon Schubert 
2325796c8dcSSimon Schubert   /* The decoded line number, NULL if not yet decoded.  */
2335796c8dcSSimon Schubert   struct line_info_table *line_table;
2345796c8dcSSimon Schubert 
2355796c8dcSSimon Schubert   /* A list of the functions found in this comp. unit.  */
2365796c8dcSSimon Schubert   struct funcinfo *function_table;
2375796c8dcSSimon Schubert 
2385796c8dcSSimon Schubert   /* A list of the variables found in this comp. unit.  */
2395796c8dcSSimon Schubert   struct varinfo *variable_table;
2405796c8dcSSimon Schubert 
2415796c8dcSSimon Schubert   /* Pointer to dwarf2_debug structure.  */
2425796c8dcSSimon Schubert   struct dwarf2_debug *stash;
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert   /* DWARF format version for this unit - from unit header.  */
2455796c8dcSSimon Schubert   int version;
2465796c8dcSSimon Schubert 
2475796c8dcSSimon Schubert   /* Address size for this unit - from unit header.  */
2485796c8dcSSimon Schubert   unsigned char addr_size;
2495796c8dcSSimon Schubert 
2505796c8dcSSimon Schubert   /* Offset size for this unit - from unit header.  */
2515796c8dcSSimon Schubert   unsigned char offset_size;
2525796c8dcSSimon Schubert 
2535796c8dcSSimon Schubert   /* Base address for this unit - from DW_AT_low_pc attribute of
2545796c8dcSSimon Schubert      DW_TAG_compile_unit DIE */
2555796c8dcSSimon Schubert   bfd_vma base_address;
2565796c8dcSSimon Schubert 
2575796c8dcSSimon Schubert   /* TRUE if symbols are cached in hash table for faster lookup by name.  */
2585796c8dcSSimon Schubert   bfd_boolean cached;
2595796c8dcSSimon Schubert };
2605796c8dcSSimon Schubert 
2615796c8dcSSimon Schubert /* This data structure holds the information of an abbrev.  */
2625796c8dcSSimon Schubert struct abbrev_info
2635796c8dcSSimon Schubert {
2645796c8dcSSimon Schubert   unsigned int number;		/* Number identifying abbrev.  */
2655796c8dcSSimon Schubert   enum dwarf_tag tag;		/* DWARF tag.  */
2665796c8dcSSimon Schubert   int has_children;		/* Boolean.  */
2675796c8dcSSimon Schubert   unsigned int num_attrs;	/* Number of attributes.  */
2685796c8dcSSimon Schubert   struct attr_abbrev *attrs;	/* An array of attribute descriptions.  */
2695796c8dcSSimon Schubert   struct abbrev_info *next;	/* Next in chain.  */
2705796c8dcSSimon Schubert };
2715796c8dcSSimon Schubert 
2725796c8dcSSimon Schubert struct attr_abbrev
2735796c8dcSSimon Schubert {
2745796c8dcSSimon Schubert   enum dwarf_attribute name;
2755796c8dcSSimon Schubert   enum dwarf_form form;
2765796c8dcSSimon Schubert };
2775796c8dcSSimon Schubert 
278*c50c785cSJohn Marino /* Map of uncompressed DWARF debug section name to compressed one.  It
279*c50c785cSJohn Marino    is terminated by NULL uncompressed_name.  */
280*c50c785cSJohn Marino 
281*c50c785cSJohn Marino struct dwarf_debug_section dwarf_debug_sections[] =
282*c50c785cSJohn Marino {
283*c50c785cSJohn Marino   { ".debug_abbrev",		".zdebug_abbrev" },
284*c50c785cSJohn Marino   { ".debug_aranges",		".zdebug_aranges" },
285*c50c785cSJohn Marino   { ".debug_frame",		".zdebug_frame" },
286*c50c785cSJohn Marino   { ".debug_info",		".zdebug_info" },
287*c50c785cSJohn Marino   { ".debug_line",		".zdebug_line" },
288*c50c785cSJohn Marino   { ".debug_loc",		".zdebug_loc" },
289*c50c785cSJohn Marino   { ".debug_macinfo",		".zdebug_macinfo" },
290*c50c785cSJohn Marino   { ".debug_pubnames",		".zdebug_pubnames" },
291*c50c785cSJohn Marino   { ".debug_pubtypes",		".zdebug_pubtypes" },
292*c50c785cSJohn Marino   { ".debug_ranges",		".zdebug_ranges" },
293*c50c785cSJohn Marino   { ".debug_static_func",	".zdebug_static_func" },
294*c50c785cSJohn Marino   { ".debug_static_vars",	".zdebug_static_vars" },
295*c50c785cSJohn Marino   { ".debug_str",		".zdebug_str", },
296*c50c785cSJohn Marino   { ".debug_types",		".zdebug_types" },
297*c50c785cSJohn Marino   /* GNU DWARF 1 extensions */
298*c50c785cSJohn Marino   { ".debug_sfnames",		".zdebug_sfnames" },
299*c50c785cSJohn Marino   { ".debug_srcinfo",		".zebug_srcinfo" },
300*c50c785cSJohn Marino   /* SGI/MIPS DWARF 2 extensions */
301*c50c785cSJohn Marino   { ".debug_funcnames",		".zdebug_funcnames" },
302*c50c785cSJohn Marino   { ".debug_typenames",		".zdebug_typenames" },
303*c50c785cSJohn Marino   { ".debug_varnames",		".zdebug_varnames" },
304*c50c785cSJohn Marino   { ".debug_weaknames",		".zdebug_weaknames" },
305*c50c785cSJohn Marino   { NULL,			NULL },
306*c50c785cSJohn Marino };
307*c50c785cSJohn Marino 
308*c50c785cSJohn Marino enum dwarf_debug_section_enum
309*c50c785cSJohn Marino {
310*c50c785cSJohn Marino   debug_abbrev = 0,
311*c50c785cSJohn Marino   debug_aranges,
312*c50c785cSJohn Marino   debug_frame,
313*c50c785cSJohn Marino   debug_info,
314*c50c785cSJohn Marino   debug_line,
315*c50c785cSJohn Marino   debug_loc,
316*c50c785cSJohn Marino   debug_macinfo,
317*c50c785cSJohn Marino   debug_pubnames,
318*c50c785cSJohn Marino   debug_pubtypes,
319*c50c785cSJohn Marino   debug_ranges,
320*c50c785cSJohn Marino   debug_static_func,
321*c50c785cSJohn Marino   debug_static_vars,
322*c50c785cSJohn Marino   debug_str,
323*c50c785cSJohn Marino   debug_types,
324*c50c785cSJohn Marino   debug_sfnames,
325*c50c785cSJohn Marino   debug_srcinfo,
326*c50c785cSJohn Marino   debug_funcnames,
327*c50c785cSJohn Marino   debug_typenames,
328*c50c785cSJohn Marino   debug_varnames,
329*c50c785cSJohn Marino   debug_weaknames
330*c50c785cSJohn Marino };
331*c50c785cSJohn Marino 
3325796c8dcSSimon Schubert #ifndef ABBREV_HASH_SIZE
3335796c8dcSSimon Schubert #define ABBREV_HASH_SIZE 121
3345796c8dcSSimon Schubert #endif
3355796c8dcSSimon Schubert #ifndef ATTR_ALLOC_CHUNK
3365796c8dcSSimon Schubert #define ATTR_ALLOC_CHUNK 4
3375796c8dcSSimon Schubert #endif
3385796c8dcSSimon Schubert 
3395796c8dcSSimon Schubert /* Variable and function hash tables.  This is used to speed up look-up
3405796c8dcSSimon Schubert    in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
3415796c8dcSSimon Schubert    In order to share code between variable and function infos, we use
3425796c8dcSSimon Schubert    a list of untyped pointer for all variable/function info associated with
3435796c8dcSSimon Schubert    a symbol.  We waste a bit of memory for list with one node but that
3445796c8dcSSimon Schubert    simplifies the code.  */
3455796c8dcSSimon Schubert 
3465796c8dcSSimon Schubert struct info_list_node
3475796c8dcSSimon Schubert {
3485796c8dcSSimon Schubert   struct info_list_node *next;
3495796c8dcSSimon Schubert   void *info;
3505796c8dcSSimon Schubert };
3515796c8dcSSimon Schubert 
3525796c8dcSSimon Schubert /* Info hash entry.  */
3535796c8dcSSimon Schubert struct info_hash_entry
3545796c8dcSSimon Schubert {
3555796c8dcSSimon Schubert   struct bfd_hash_entry root;
3565796c8dcSSimon Schubert   struct info_list_node *head;
3575796c8dcSSimon Schubert };
3585796c8dcSSimon Schubert 
3595796c8dcSSimon Schubert struct info_hash_table
3605796c8dcSSimon Schubert {
3615796c8dcSSimon Schubert   struct bfd_hash_table base;
3625796c8dcSSimon Schubert };
3635796c8dcSSimon Schubert 
3645796c8dcSSimon Schubert /* Function to create a new entry in info hash table. */
3655796c8dcSSimon Schubert 
3665796c8dcSSimon Schubert static struct bfd_hash_entry *
3675796c8dcSSimon Schubert info_hash_table_newfunc (struct bfd_hash_entry *entry,
3685796c8dcSSimon Schubert 			 struct bfd_hash_table *table,
3695796c8dcSSimon Schubert 			 const char *string)
3705796c8dcSSimon Schubert {
3715796c8dcSSimon Schubert   struct info_hash_entry *ret = (struct info_hash_entry *) entry;
3725796c8dcSSimon Schubert 
3735796c8dcSSimon Schubert   /* Allocate the structure if it has not already been allocated by a
3745796c8dcSSimon Schubert      derived class.  */
3755796c8dcSSimon Schubert   if (ret == NULL)
3765796c8dcSSimon Schubert     {
3775796c8dcSSimon Schubert       ret = (struct info_hash_entry *) bfd_hash_allocate (table,
3785796c8dcSSimon Schubert                                                           sizeof (* ret));
3795796c8dcSSimon Schubert       if (ret == NULL)
3805796c8dcSSimon Schubert 	return NULL;
3815796c8dcSSimon Schubert     }
3825796c8dcSSimon Schubert 
3835796c8dcSSimon Schubert   /* Call the allocation method of the base class.  */
3845796c8dcSSimon Schubert   ret = ((struct info_hash_entry *)
3855796c8dcSSimon Schubert 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
3865796c8dcSSimon Schubert 
3875796c8dcSSimon Schubert   /* Initialize the local fields here.  */
3885796c8dcSSimon Schubert   if (ret)
3895796c8dcSSimon Schubert     ret->head = NULL;
3905796c8dcSSimon Schubert 
3915796c8dcSSimon Schubert   return (struct bfd_hash_entry *) ret;
3925796c8dcSSimon Schubert }
3935796c8dcSSimon Schubert 
3945796c8dcSSimon Schubert /* Function to create a new info hash table.  It returns a pointer to the
3955796c8dcSSimon Schubert    newly created table or NULL if there is any error.  We need abfd
3965796c8dcSSimon Schubert    solely for memory allocation.  */
3975796c8dcSSimon Schubert 
3985796c8dcSSimon Schubert static struct info_hash_table *
3995796c8dcSSimon Schubert create_info_hash_table (bfd *abfd)
4005796c8dcSSimon Schubert {
4015796c8dcSSimon Schubert   struct info_hash_table *hash_table;
4025796c8dcSSimon Schubert 
4035796c8dcSSimon Schubert   hash_table = (struct info_hash_table *)
4045796c8dcSSimon Schubert       bfd_alloc (abfd, sizeof (struct info_hash_table));
4055796c8dcSSimon Schubert   if (!hash_table)
4065796c8dcSSimon Schubert     return hash_table;
4075796c8dcSSimon Schubert 
4085796c8dcSSimon Schubert   if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
4095796c8dcSSimon Schubert 			    sizeof (struct info_hash_entry)))
4105796c8dcSSimon Schubert     {
4115796c8dcSSimon Schubert       bfd_release (abfd, hash_table);
4125796c8dcSSimon Schubert       return NULL;
4135796c8dcSSimon Schubert     }
4145796c8dcSSimon Schubert 
4155796c8dcSSimon Schubert   return hash_table;
4165796c8dcSSimon Schubert }
4175796c8dcSSimon Schubert 
4185796c8dcSSimon Schubert /* Insert an info entry into an info hash table.  We do not check of
4195796c8dcSSimon Schubert    duplicate entries.  Also, the caller need to guarantee that the
4205796c8dcSSimon Schubert    right type of info in inserted as info is passed as a void* pointer.
4215796c8dcSSimon Schubert    This function returns true if there is no error.  */
4225796c8dcSSimon Schubert 
4235796c8dcSSimon Schubert static bfd_boolean
4245796c8dcSSimon Schubert insert_info_hash_table (struct info_hash_table *hash_table,
4255796c8dcSSimon Schubert 			const char *key,
4265796c8dcSSimon Schubert 			void *info,
4275796c8dcSSimon Schubert 			bfd_boolean copy_p)
4285796c8dcSSimon Schubert {
4295796c8dcSSimon Schubert   struct info_hash_entry *entry;
4305796c8dcSSimon Schubert   struct info_list_node *node;
4315796c8dcSSimon Schubert 
4325796c8dcSSimon Schubert   entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
4335796c8dcSSimon Schubert 						     key, TRUE, copy_p);
4345796c8dcSSimon Schubert   if (!entry)
4355796c8dcSSimon Schubert     return FALSE;
4365796c8dcSSimon Schubert 
4375796c8dcSSimon Schubert   node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
4385796c8dcSSimon Schubert                                                       sizeof (*node));
4395796c8dcSSimon Schubert   if (!node)
4405796c8dcSSimon Schubert     return FALSE;
4415796c8dcSSimon Schubert 
4425796c8dcSSimon Schubert   node->info = info;
4435796c8dcSSimon Schubert   node->next = entry->head;
4445796c8dcSSimon Schubert   entry->head = node;
4455796c8dcSSimon Schubert 
4465796c8dcSSimon Schubert   return TRUE;
4475796c8dcSSimon Schubert }
4485796c8dcSSimon Schubert 
4495796c8dcSSimon Schubert /* Look up an info entry list from an info hash table.  Return NULL
4505796c8dcSSimon Schubert    if there is none. */
4515796c8dcSSimon Schubert 
4525796c8dcSSimon Schubert static struct info_list_node *
4535796c8dcSSimon Schubert lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
4545796c8dcSSimon Schubert {
4555796c8dcSSimon Schubert   struct info_hash_entry *entry;
4565796c8dcSSimon Schubert 
4575796c8dcSSimon Schubert   entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
4585796c8dcSSimon Schubert 						     FALSE, FALSE);
4595796c8dcSSimon Schubert   return entry ? entry->head : NULL;
4605796c8dcSSimon Schubert }
4615796c8dcSSimon Schubert 
4625796c8dcSSimon Schubert /* Read a section into its appropriate place in the dwarf2_debug
4635796c8dcSSimon Schubert    struct (indicated by SECTION_BUFFER and SECTION_SIZE).  If SYMS is
4645796c8dcSSimon Schubert    not NULL, use bfd_simple_get_relocated_section_contents to read the
4655796c8dcSSimon Schubert    section contents, otherwise use bfd_get_section_contents.  Fail if
4665796c8dcSSimon Schubert    the located section does not contain at least OFFSET bytes.  */
4675796c8dcSSimon Schubert 
4685796c8dcSSimon Schubert static bfd_boolean
4695796c8dcSSimon Schubert read_section (bfd *           abfd,
470*c50c785cSJohn Marino 	      enum dwarf_debug_section_enum sec,
4715796c8dcSSimon Schubert 	      asymbol **      syms,
4725796c8dcSSimon Schubert 	      bfd_uint64_t    offset,
4735796c8dcSSimon Schubert 	      bfd_byte **     section_buffer,
4745796c8dcSSimon Schubert 	      bfd_size_type * section_size)
4755796c8dcSSimon Schubert {
4765796c8dcSSimon Schubert   asection *msec;
477*c50c785cSJohn Marino   const char *section_name = dwarf_debug_sections[sec].uncompressed_name;
4785796c8dcSSimon Schubert 
4795796c8dcSSimon Schubert   /* read_section is a noop if the section has already been read.  */
4805796c8dcSSimon Schubert   if (!*section_buffer)
4815796c8dcSSimon Schubert     {
4825796c8dcSSimon Schubert       msec = bfd_get_section_by_name (abfd, section_name);
483*c50c785cSJohn Marino       if (! msec)
4845796c8dcSSimon Schubert 	{
485*c50c785cSJohn Marino 	  section_name = dwarf_debug_sections[sec].compressed_name;
486*c50c785cSJohn Marino 	  msec = bfd_get_section_by_name (abfd, section_name);
4875796c8dcSSimon Schubert 	}
4885796c8dcSSimon Schubert       if (! msec)
4895796c8dcSSimon Schubert 	{
4905796c8dcSSimon Schubert 	  (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."), section_name);
4915796c8dcSSimon Schubert 	  bfd_set_error (bfd_error_bad_value);
4925796c8dcSSimon Schubert 	  return FALSE;
4935796c8dcSSimon Schubert 	}
4945796c8dcSSimon Schubert 
4955796c8dcSSimon Schubert       *section_size = msec->rawsize ? msec->rawsize : msec->size;
496*c50c785cSJohn Marino       if (syms)
497*c50c785cSJohn Marino 	{
498*c50c785cSJohn Marino 	  *section_buffer
499*c50c785cSJohn Marino 	      = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
500*c50c785cSJohn Marino 	  if (! *section_buffer)
5015796c8dcSSimon Schubert 	    return FALSE;
5025796c8dcSSimon Schubert 	}
503*c50c785cSJohn Marino       else
504*c50c785cSJohn Marino 	{
505*c50c785cSJohn Marino 	  *section_buffer = (bfd_byte *) bfd_malloc (*section_size);
506*c50c785cSJohn Marino 	  if (! *section_buffer)
507*c50c785cSJohn Marino 	    return FALSE;
508*c50c785cSJohn Marino 	  if (! bfd_get_section_contents (abfd, msec, *section_buffer,
509*c50c785cSJohn Marino 					  0, *section_size))
510*c50c785cSJohn Marino 	    return FALSE;
511*c50c785cSJohn Marino 	}
512*c50c785cSJohn Marino     }
5135796c8dcSSimon Schubert 
5145796c8dcSSimon Schubert   /* It is possible to get a bad value for the offset into the section
5155796c8dcSSimon Schubert      that the client wants.  Validate it here to avoid trouble later.  */
5165796c8dcSSimon Schubert   if (offset != 0 && offset >= *section_size)
5175796c8dcSSimon Schubert     {
5185796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
5195796c8dcSSimon Schubert 			     (long) offset, section_name, *section_size);
5205796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
5215796c8dcSSimon Schubert       return FALSE;
5225796c8dcSSimon Schubert     }
5235796c8dcSSimon Schubert 
5245796c8dcSSimon Schubert   return TRUE;
5255796c8dcSSimon Schubert }
5265796c8dcSSimon Schubert 
5275796c8dcSSimon Schubert /* VERBATIM
5285796c8dcSSimon Schubert    The following function up to the END VERBATIM mark are
5295796c8dcSSimon Schubert    copied directly from dwarf2read.c.  */
5305796c8dcSSimon Schubert 
5315796c8dcSSimon Schubert /* Read dwarf information from a buffer.  */
5325796c8dcSSimon Schubert 
5335796c8dcSSimon Schubert static unsigned int
5345796c8dcSSimon Schubert read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
5355796c8dcSSimon Schubert {
5365796c8dcSSimon Schubert   return bfd_get_8 (abfd, buf);
5375796c8dcSSimon Schubert }
5385796c8dcSSimon Schubert 
5395796c8dcSSimon Schubert static int
5405796c8dcSSimon Schubert read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
5415796c8dcSSimon Schubert {
5425796c8dcSSimon Schubert   return bfd_get_signed_8 (abfd, buf);
5435796c8dcSSimon Schubert }
5445796c8dcSSimon Schubert 
5455796c8dcSSimon Schubert static unsigned int
5465796c8dcSSimon Schubert read_2_bytes (bfd *abfd, bfd_byte *buf)
5475796c8dcSSimon Schubert {
5485796c8dcSSimon Schubert   return bfd_get_16 (abfd, buf);
5495796c8dcSSimon Schubert }
5505796c8dcSSimon Schubert 
5515796c8dcSSimon Schubert static unsigned int
5525796c8dcSSimon Schubert read_4_bytes (bfd *abfd, bfd_byte *buf)
5535796c8dcSSimon Schubert {
5545796c8dcSSimon Schubert   return bfd_get_32 (abfd, buf);
5555796c8dcSSimon Schubert }
5565796c8dcSSimon Schubert 
5575796c8dcSSimon Schubert static bfd_uint64_t
5585796c8dcSSimon Schubert read_8_bytes (bfd *abfd, bfd_byte *buf)
5595796c8dcSSimon Schubert {
5605796c8dcSSimon Schubert   return bfd_get_64 (abfd, buf);
5615796c8dcSSimon Schubert }
5625796c8dcSSimon Schubert 
5635796c8dcSSimon Schubert static bfd_byte *
5645796c8dcSSimon Schubert read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
5655796c8dcSSimon Schubert 	      bfd_byte *buf,
5665796c8dcSSimon Schubert 	      unsigned int size ATTRIBUTE_UNUSED)
5675796c8dcSSimon Schubert {
5685796c8dcSSimon Schubert   return buf;
5695796c8dcSSimon Schubert }
5705796c8dcSSimon Schubert 
5715796c8dcSSimon Schubert static char *
5725796c8dcSSimon Schubert read_string (bfd *abfd ATTRIBUTE_UNUSED,
5735796c8dcSSimon Schubert 	     bfd_byte *buf,
5745796c8dcSSimon Schubert 	     unsigned int *bytes_read_ptr)
5755796c8dcSSimon Schubert {
5765796c8dcSSimon Schubert   /* Return a pointer to the embedded string.  */
5775796c8dcSSimon Schubert   char *str = (char *) buf;
5785796c8dcSSimon Schubert 
5795796c8dcSSimon Schubert   if (*str == '\0')
5805796c8dcSSimon Schubert     {
5815796c8dcSSimon Schubert       *bytes_read_ptr = 1;
5825796c8dcSSimon Schubert       return NULL;
5835796c8dcSSimon Schubert     }
5845796c8dcSSimon Schubert 
5855796c8dcSSimon Schubert   *bytes_read_ptr = strlen (str) + 1;
5865796c8dcSSimon Schubert   return str;
5875796c8dcSSimon Schubert }
5885796c8dcSSimon Schubert 
5895796c8dcSSimon Schubert /* END VERBATIM */
5905796c8dcSSimon Schubert 
5915796c8dcSSimon Schubert static char *
5925796c8dcSSimon Schubert read_indirect_string (struct comp_unit * unit,
5935796c8dcSSimon Schubert 		      bfd_byte *         buf,
5945796c8dcSSimon Schubert 		      unsigned int *     bytes_read_ptr)
5955796c8dcSSimon Schubert {
5965796c8dcSSimon Schubert   bfd_uint64_t offset;
5975796c8dcSSimon Schubert   struct dwarf2_debug *stash = unit->stash;
5985796c8dcSSimon Schubert   char *str;
5995796c8dcSSimon Schubert 
6005796c8dcSSimon Schubert   if (unit->offset_size == 4)
6015796c8dcSSimon Schubert     offset = read_4_bytes (unit->abfd, buf);
6025796c8dcSSimon Schubert   else
6035796c8dcSSimon Schubert     offset = read_8_bytes (unit->abfd, buf);
6045796c8dcSSimon Schubert 
6055796c8dcSSimon Schubert   *bytes_read_ptr = unit->offset_size;
6065796c8dcSSimon Schubert 
607*c50c785cSJohn Marino   if (! read_section (unit->abfd, debug_str, stash->syms, offset,
6085796c8dcSSimon Schubert 		      &stash->dwarf_str_buffer, &stash->dwarf_str_size))
6095796c8dcSSimon Schubert     return NULL;
6105796c8dcSSimon Schubert 
6115796c8dcSSimon Schubert   str = (char *) stash->dwarf_str_buffer + offset;
6125796c8dcSSimon Schubert   if (*str == '\0')
6135796c8dcSSimon Schubert     return NULL;
6145796c8dcSSimon Schubert   return str;
6155796c8dcSSimon Schubert }
6165796c8dcSSimon Schubert 
6175796c8dcSSimon Schubert static bfd_uint64_t
6185796c8dcSSimon Schubert read_address (struct comp_unit *unit, bfd_byte *buf)
6195796c8dcSSimon Schubert {
6205796c8dcSSimon Schubert   int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
6215796c8dcSSimon Schubert 
6225796c8dcSSimon Schubert   if (signed_vma)
6235796c8dcSSimon Schubert     {
6245796c8dcSSimon Schubert       switch (unit->addr_size)
6255796c8dcSSimon Schubert 	{
6265796c8dcSSimon Schubert 	case 8:
6275796c8dcSSimon Schubert 	  return bfd_get_signed_64 (unit->abfd, buf);
6285796c8dcSSimon Schubert 	case 4:
6295796c8dcSSimon Schubert 	  return bfd_get_signed_32 (unit->abfd, buf);
6305796c8dcSSimon Schubert 	case 2:
6315796c8dcSSimon Schubert 	  return bfd_get_signed_16 (unit->abfd, buf);
6325796c8dcSSimon Schubert 	default:
6335796c8dcSSimon Schubert 	  abort ();
6345796c8dcSSimon Schubert 	}
6355796c8dcSSimon Schubert     }
6365796c8dcSSimon Schubert   else
6375796c8dcSSimon Schubert     {
6385796c8dcSSimon Schubert       switch (unit->addr_size)
6395796c8dcSSimon Schubert 	{
6405796c8dcSSimon Schubert 	case 8:
6415796c8dcSSimon Schubert 	  return bfd_get_64 (unit->abfd, buf);
6425796c8dcSSimon Schubert 	case 4:
6435796c8dcSSimon Schubert 	  return bfd_get_32 (unit->abfd, buf);
6445796c8dcSSimon Schubert 	case 2:
6455796c8dcSSimon Schubert 	  return bfd_get_16 (unit->abfd, buf);
6465796c8dcSSimon Schubert 	default:
6475796c8dcSSimon Schubert 	  abort ();
6485796c8dcSSimon Schubert 	}
6495796c8dcSSimon Schubert     }
6505796c8dcSSimon Schubert }
6515796c8dcSSimon Schubert 
6525796c8dcSSimon Schubert /* Lookup an abbrev_info structure in the abbrev hash table.  */
6535796c8dcSSimon Schubert 
6545796c8dcSSimon Schubert static struct abbrev_info *
6555796c8dcSSimon Schubert lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
6565796c8dcSSimon Schubert {
6575796c8dcSSimon Schubert   unsigned int hash_number;
6585796c8dcSSimon Schubert   struct abbrev_info *abbrev;
6595796c8dcSSimon Schubert 
6605796c8dcSSimon Schubert   hash_number = number % ABBREV_HASH_SIZE;
6615796c8dcSSimon Schubert   abbrev = abbrevs[hash_number];
6625796c8dcSSimon Schubert 
6635796c8dcSSimon Schubert   while (abbrev)
6645796c8dcSSimon Schubert     {
6655796c8dcSSimon Schubert       if (abbrev->number == number)
6665796c8dcSSimon Schubert 	return abbrev;
6675796c8dcSSimon Schubert       else
6685796c8dcSSimon Schubert 	abbrev = abbrev->next;
6695796c8dcSSimon Schubert     }
6705796c8dcSSimon Schubert 
6715796c8dcSSimon Schubert   return NULL;
6725796c8dcSSimon Schubert }
6735796c8dcSSimon Schubert 
6745796c8dcSSimon Schubert /* In DWARF version 2, the description of the debugging information is
6755796c8dcSSimon Schubert    stored in a separate .debug_abbrev section.  Before we read any
6765796c8dcSSimon Schubert    dies from a section we read in all abbreviations and install them
6775796c8dcSSimon Schubert    in a hash table.  */
6785796c8dcSSimon Schubert 
6795796c8dcSSimon Schubert static struct abbrev_info**
6805796c8dcSSimon Schubert read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
6815796c8dcSSimon Schubert {
6825796c8dcSSimon Schubert   struct abbrev_info **abbrevs;
6835796c8dcSSimon Schubert   bfd_byte *abbrev_ptr;
6845796c8dcSSimon Schubert   struct abbrev_info *cur_abbrev;
6855796c8dcSSimon Schubert   unsigned int abbrev_number, bytes_read, abbrev_name;
6865796c8dcSSimon Schubert   unsigned int abbrev_form, hash_number;
6875796c8dcSSimon Schubert   bfd_size_type amt;
6885796c8dcSSimon Schubert 
689*c50c785cSJohn Marino   if (! read_section (abfd, debug_abbrev, stash->syms, offset,
6905796c8dcSSimon Schubert 		      &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
691cf7f2e2dSJohn Marino     return NULL;
6925796c8dcSSimon Schubert 
6935796c8dcSSimon Schubert   amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
6945796c8dcSSimon Schubert   abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
695cf7f2e2dSJohn Marino   if (abbrevs == NULL)
696cf7f2e2dSJohn Marino     return NULL;
6975796c8dcSSimon Schubert 
6985796c8dcSSimon Schubert   abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
6995796c8dcSSimon Schubert   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7005796c8dcSSimon Schubert   abbrev_ptr += bytes_read;
7015796c8dcSSimon Schubert 
7025796c8dcSSimon Schubert   /* Loop until we reach an abbrev number of 0.  */
7035796c8dcSSimon Schubert   while (abbrev_number)
7045796c8dcSSimon Schubert     {
7055796c8dcSSimon Schubert       amt = sizeof (struct abbrev_info);
7065796c8dcSSimon Schubert       cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
707cf7f2e2dSJohn Marino       if (cur_abbrev == NULL)
708cf7f2e2dSJohn Marino 	return NULL;
7095796c8dcSSimon Schubert 
7105796c8dcSSimon Schubert       /* Read in abbrev header.  */
7115796c8dcSSimon Schubert       cur_abbrev->number = abbrev_number;
7125796c8dcSSimon Schubert       cur_abbrev->tag = (enum dwarf_tag)
7135796c8dcSSimon Schubert 	read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7145796c8dcSSimon Schubert       abbrev_ptr += bytes_read;
7155796c8dcSSimon Schubert       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
7165796c8dcSSimon Schubert       abbrev_ptr += 1;
7175796c8dcSSimon Schubert 
7185796c8dcSSimon Schubert       /* Now read in declarations.  */
7195796c8dcSSimon Schubert       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7205796c8dcSSimon Schubert       abbrev_ptr += bytes_read;
7215796c8dcSSimon Schubert       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7225796c8dcSSimon Schubert       abbrev_ptr += bytes_read;
7235796c8dcSSimon Schubert 
7245796c8dcSSimon Schubert       while (abbrev_name)
7255796c8dcSSimon Schubert 	{
7265796c8dcSSimon Schubert 	  if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
7275796c8dcSSimon Schubert 	    {
7285796c8dcSSimon Schubert 	      struct attr_abbrev *tmp;
7295796c8dcSSimon Schubert 
7305796c8dcSSimon Schubert 	      amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
7315796c8dcSSimon Schubert 	      amt *= sizeof (struct attr_abbrev);
7325796c8dcSSimon Schubert 	      tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
7335796c8dcSSimon Schubert 	      if (tmp == NULL)
7345796c8dcSSimon Schubert 		{
7355796c8dcSSimon Schubert 		  size_t i;
7365796c8dcSSimon Schubert 
7375796c8dcSSimon Schubert 		  for (i = 0; i < ABBREV_HASH_SIZE; i++)
7385796c8dcSSimon Schubert 		    {
7395796c8dcSSimon Schubert 		      struct abbrev_info *abbrev = abbrevs[i];
7405796c8dcSSimon Schubert 
7415796c8dcSSimon Schubert 		      while (abbrev)
7425796c8dcSSimon Schubert 			{
7435796c8dcSSimon Schubert 			  free (abbrev->attrs);
7445796c8dcSSimon Schubert 			  abbrev = abbrev->next;
7455796c8dcSSimon Schubert 			}
7465796c8dcSSimon Schubert 		    }
7475796c8dcSSimon Schubert 		  return NULL;
7485796c8dcSSimon Schubert 		}
7495796c8dcSSimon Schubert 	      cur_abbrev->attrs = tmp;
7505796c8dcSSimon Schubert 	    }
7515796c8dcSSimon Schubert 
7525796c8dcSSimon Schubert 	  cur_abbrev->attrs[cur_abbrev->num_attrs].name
7535796c8dcSSimon Schubert 	    = (enum dwarf_attribute) abbrev_name;
7545796c8dcSSimon Schubert 	  cur_abbrev->attrs[cur_abbrev->num_attrs++].form
7555796c8dcSSimon Schubert 	    = (enum dwarf_form) abbrev_form;
7565796c8dcSSimon Schubert 	  abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7575796c8dcSSimon Schubert 	  abbrev_ptr += bytes_read;
7585796c8dcSSimon Schubert 	  abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7595796c8dcSSimon Schubert 	  abbrev_ptr += bytes_read;
7605796c8dcSSimon Schubert 	}
7615796c8dcSSimon Schubert 
7625796c8dcSSimon Schubert       hash_number = abbrev_number % ABBREV_HASH_SIZE;
7635796c8dcSSimon Schubert       cur_abbrev->next = abbrevs[hash_number];
7645796c8dcSSimon Schubert       abbrevs[hash_number] = cur_abbrev;
7655796c8dcSSimon Schubert 
7665796c8dcSSimon Schubert       /* Get next abbreviation.
7675796c8dcSSimon Schubert 	 Under Irix6 the abbreviations for a compilation unit are not
7685796c8dcSSimon Schubert 	 always properly terminated with an abbrev number of 0.
7695796c8dcSSimon Schubert 	 Exit loop if we encounter an abbreviation which we have
7705796c8dcSSimon Schubert 	 already read (which means we are about to read the abbreviations
7715796c8dcSSimon Schubert 	 for the next compile unit) or if the end of the abbreviation
7725796c8dcSSimon Schubert 	 table is reached.  */
7735796c8dcSSimon Schubert       if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
7745796c8dcSSimon Schubert 	  >= stash->dwarf_abbrev_size)
7755796c8dcSSimon Schubert 	break;
7765796c8dcSSimon Schubert       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7775796c8dcSSimon Schubert       abbrev_ptr += bytes_read;
7785796c8dcSSimon Schubert       if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
7795796c8dcSSimon Schubert 	break;
7805796c8dcSSimon Schubert     }
7815796c8dcSSimon Schubert 
7825796c8dcSSimon Schubert   return abbrevs;
7835796c8dcSSimon Schubert }
7845796c8dcSSimon Schubert 
7855796c8dcSSimon Schubert /* Read an attribute value described by an attribute form.  */
7865796c8dcSSimon Schubert 
7875796c8dcSSimon Schubert static bfd_byte *
7885796c8dcSSimon Schubert read_attribute_value (struct attribute *attr,
7895796c8dcSSimon Schubert 		      unsigned form,
7905796c8dcSSimon Schubert 		      struct comp_unit *unit,
7915796c8dcSSimon Schubert 		      bfd_byte *info_ptr)
7925796c8dcSSimon Schubert {
7935796c8dcSSimon Schubert   bfd *abfd = unit->abfd;
7945796c8dcSSimon Schubert   unsigned int bytes_read;
7955796c8dcSSimon Schubert   struct dwarf_block *blk;
7965796c8dcSSimon Schubert   bfd_size_type amt;
7975796c8dcSSimon Schubert 
7985796c8dcSSimon Schubert   attr->form = (enum dwarf_form) form;
7995796c8dcSSimon Schubert 
8005796c8dcSSimon Schubert   switch (form)
8015796c8dcSSimon Schubert     {
8025796c8dcSSimon Schubert     case DW_FORM_ref_addr:
8035796c8dcSSimon Schubert       /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
8045796c8dcSSimon Schubert 	 DWARF3.  */
805cf7f2e2dSJohn Marino       if (unit->version == 3 || unit->version == 4)
8065796c8dcSSimon Schubert 	{
8075796c8dcSSimon Schubert 	  if (unit->offset_size == 4)
8085796c8dcSSimon Schubert 	    attr->u.val = read_4_bytes (unit->abfd, info_ptr);
8095796c8dcSSimon Schubert 	  else
8105796c8dcSSimon Schubert 	    attr->u.val = read_8_bytes (unit->abfd, info_ptr);
8115796c8dcSSimon Schubert 	  info_ptr += unit->offset_size;
8125796c8dcSSimon Schubert 	  break;
8135796c8dcSSimon Schubert 	}
8145796c8dcSSimon Schubert       /* FALLTHROUGH */
8155796c8dcSSimon Schubert     case DW_FORM_addr:
8165796c8dcSSimon Schubert       attr->u.val = read_address (unit, info_ptr);
8175796c8dcSSimon Schubert       info_ptr += unit->addr_size;
8185796c8dcSSimon Schubert       break;
819cf7f2e2dSJohn Marino     case DW_FORM_sec_offset:
820cf7f2e2dSJohn Marino       if (unit->offset_size == 4)
821cf7f2e2dSJohn Marino 	attr->u.val = read_4_bytes (unit->abfd, info_ptr);
822cf7f2e2dSJohn Marino       else
823cf7f2e2dSJohn Marino 	attr->u.val = read_8_bytes (unit->abfd, info_ptr);
824cf7f2e2dSJohn Marino       info_ptr += unit->offset_size;
825cf7f2e2dSJohn Marino       break;
8265796c8dcSSimon Schubert     case DW_FORM_block2:
8275796c8dcSSimon Schubert       amt = sizeof (struct dwarf_block);
8285796c8dcSSimon Schubert       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
829cf7f2e2dSJohn Marino       if (blk == NULL)
830cf7f2e2dSJohn Marino 	return NULL;
8315796c8dcSSimon Schubert       blk->size = read_2_bytes (abfd, info_ptr);
8325796c8dcSSimon Schubert       info_ptr += 2;
8335796c8dcSSimon Schubert       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8345796c8dcSSimon Schubert       info_ptr += blk->size;
8355796c8dcSSimon Schubert       attr->u.blk = blk;
8365796c8dcSSimon Schubert       break;
8375796c8dcSSimon Schubert     case DW_FORM_block4:
8385796c8dcSSimon Schubert       amt = sizeof (struct dwarf_block);
8395796c8dcSSimon Schubert       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
840cf7f2e2dSJohn Marino       if (blk == NULL)
841cf7f2e2dSJohn Marino 	return NULL;
8425796c8dcSSimon Schubert       blk->size = read_4_bytes (abfd, info_ptr);
8435796c8dcSSimon Schubert       info_ptr += 4;
8445796c8dcSSimon Schubert       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8455796c8dcSSimon Schubert       info_ptr += blk->size;
8465796c8dcSSimon Schubert       attr->u.blk = blk;
8475796c8dcSSimon Schubert       break;
8485796c8dcSSimon Schubert     case DW_FORM_data2:
8495796c8dcSSimon Schubert       attr->u.val = read_2_bytes (abfd, info_ptr);
8505796c8dcSSimon Schubert       info_ptr += 2;
8515796c8dcSSimon Schubert       break;
8525796c8dcSSimon Schubert     case DW_FORM_data4:
8535796c8dcSSimon Schubert       attr->u.val = read_4_bytes (abfd, info_ptr);
8545796c8dcSSimon Schubert       info_ptr += 4;
8555796c8dcSSimon Schubert       break;
8565796c8dcSSimon Schubert     case DW_FORM_data8:
8575796c8dcSSimon Schubert       attr->u.val = read_8_bytes (abfd, info_ptr);
8585796c8dcSSimon Schubert       info_ptr += 8;
8595796c8dcSSimon Schubert       break;
8605796c8dcSSimon Schubert     case DW_FORM_string:
8615796c8dcSSimon Schubert       attr->u.str = read_string (abfd, info_ptr, &bytes_read);
8625796c8dcSSimon Schubert       info_ptr += bytes_read;
8635796c8dcSSimon Schubert       break;
8645796c8dcSSimon Schubert     case DW_FORM_strp:
8655796c8dcSSimon Schubert       attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
8665796c8dcSSimon Schubert       info_ptr += bytes_read;
8675796c8dcSSimon Schubert       break;
868cf7f2e2dSJohn Marino     case DW_FORM_exprloc:
8695796c8dcSSimon Schubert     case DW_FORM_block:
8705796c8dcSSimon Schubert       amt = sizeof (struct dwarf_block);
8715796c8dcSSimon Schubert       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
872cf7f2e2dSJohn Marino       if (blk == NULL)
873cf7f2e2dSJohn Marino 	return NULL;
8745796c8dcSSimon Schubert       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8755796c8dcSSimon Schubert       info_ptr += bytes_read;
8765796c8dcSSimon Schubert       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8775796c8dcSSimon Schubert       info_ptr += blk->size;
8785796c8dcSSimon Schubert       attr->u.blk = blk;
8795796c8dcSSimon Schubert       break;
8805796c8dcSSimon Schubert     case DW_FORM_block1:
8815796c8dcSSimon Schubert       amt = sizeof (struct dwarf_block);
8825796c8dcSSimon Schubert       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
883cf7f2e2dSJohn Marino       if (blk == NULL)
884cf7f2e2dSJohn Marino 	return NULL;
8855796c8dcSSimon Schubert       blk->size = read_1_byte (abfd, info_ptr);
8865796c8dcSSimon Schubert       info_ptr += 1;
8875796c8dcSSimon Schubert       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8885796c8dcSSimon Schubert       info_ptr += blk->size;
8895796c8dcSSimon Schubert       attr->u.blk = blk;
8905796c8dcSSimon Schubert       break;
8915796c8dcSSimon Schubert     case DW_FORM_data1:
8925796c8dcSSimon Schubert       attr->u.val = read_1_byte (abfd, info_ptr);
8935796c8dcSSimon Schubert       info_ptr += 1;
8945796c8dcSSimon Schubert       break;
8955796c8dcSSimon Schubert     case DW_FORM_flag:
8965796c8dcSSimon Schubert       attr->u.val = read_1_byte (abfd, info_ptr);
8975796c8dcSSimon Schubert       info_ptr += 1;
8985796c8dcSSimon Schubert       break;
899cf7f2e2dSJohn Marino     case DW_FORM_flag_present:
900cf7f2e2dSJohn Marino       attr->u.val = 1;
901cf7f2e2dSJohn Marino       break;
9025796c8dcSSimon Schubert     case DW_FORM_sdata:
9035796c8dcSSimon Schubert       attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9045796c8dcSSimon Schubert       info_ptr += bytes_read;
9055796c8dcSSimon Schubert       break;
9065796c8dcSSimon Schubert     case DW_FORM_udata:
9075796c8dcSSimon Schubert       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9085796c8dcSSimon Schubert       info_ptr += bytes_read;
9095796c8dcSSimon Schubert       break;
9105796c8dcSSimon Schubert     case DW_FORM_ref1:
9115796c8dcSSimon Schubert       attr->u.val = read_1_byte (abfd, info_ptr);
9125796c8dcSSimon Schubert       info_ptr += 1;
9135796c8dcSSimon Schubert       break;
9145796c8dcSSimon Schubert     case DW_FORM_ref2:
9155796c8dcSSimon Schubert       attr->u.val = read_2_bytes (abfd, info_ptr);
9165796c8dcSSimon Schubert       info_ptr += 2;
9175796c8dcSSimon Schubert       break;
9185796c8dcSSimon Schubert     case DW_FORM_ref4:
9195796c8dcSSimon Schubert       attr->u.val = read_4_bytes (abfd, info_ptr);
9205796c8dcSSimon Schubert       info_ptr += 4;
9215796c8dcSSimon Schubert       break;
9225796c8dcSSimon Schubert     case DW_FORM_ref8:
9235796c8dcSSimon Schubert       attr->u.val = read_8_bytes (abfd, info_ptr);
9245796c8dcSSimon Schubert       info_ptr += 8;
9255796c8dcSSimon Schubert       break;
926cf7f2e2dSJohn Marino     case DW_FORM_ref_sig8:
927cf7f2e2dSJohn Marino       attr->u.val = read_8_bytes (abfd, info_ptr);
928cf7f2e2dSJohn Marino       info_ptr += 8;
929cf7f2e2dSJohn Marino       break;
9305796c8dcSSimon Schubert     case DW_FORM_ref_udata:
9315796c8dcSSimon Schubert       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9325796c8dcSSimon Schubert       info_ptr += bytes_read;
9335796c8dcSSimon Schubert       break;
9345796c8dcSSimon Schubert     case DW_FORM_indirect:
9355796c8dcSSimon Schubert       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9365796c8dcSSimon Schubert       info_ptr += bytes_read;
9375796c8dcSSimon Schubert       info_ptr = read_attribute_value (attr, form, unit, info_ptr);
9385796c8dcSSimon Schubert       break;
9395796c8dcSSimon Schubert     default:
9405796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
9415796c8dcSSimon Schubert 			     form);
9425796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
943cf7f2e2dSJohn Marino       return NULL;
9445796c8dcSSimon Schubert     }
9455796c8dcSSimon Schubert   return info_ptr;
9465796c8dcSSimon Schubert }
9475796c8dcSSimon Schubert 
9485796c8dcSSimon Schubert /* Read an attribute described by an abbreviated attribute.  */
9495796c8dcSSimon Schubert 
9505796c8dcSSimon Schubert static bfd_byte *
9515796c8dcSSimon Schubert read_attribute (struct attribute *attr,
9525796c8dcSSimon Schubert 		struct attr_abbrev *abbrev,
9535796c8dcSSimon Schubert 		struct comp_unit *unit,
9545796c8dcSSimon Schubert 		bfd_byte *info_ptr)
9555796c8dcSSimon Schubert {
9565796c8dcSSimon Schubert   attr->name = abbrev->name;
9575796c8dcSSimon Schubert   info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
9585796c8dcSSimon Schubert   return info_ptr;
9595796c8dcSSimon Schubert }
9605796c8dcSSimon Schubert 
9615796c8dcSSimon Schubert /* Source line information table routines.  */
9625796c8dcSSimon Schubert 
9635796c8dcSSimon Schubert #define FILE_ALLOC_CHUNK 5
9645796c8dcSSimon Schubert #define DIR_ALLOC_CHUNK 5
9655796c8dcSSimon Schubert 
9665796c8dcSSimon Schubert struct line_info
9675796c8dcSSimon Schubert {
9685796c8dcSSimon Schubert   struct line_info* prev_line;
9695796c8dcSSimon Schubert   bfd_vma address;
9705796c8dcSSimon Schubert   char *filename;
9715796c8dcSSimon Schubert   unsigned int line;
9725796c8dcSSimon Schubert   unsigned int column;
973cf7f2e2dSJohn Marino   unsigned char op_index;
974cf7f2e2dSJohn Marino   unsigned char end_sequence;		/* End of (sequential) code sequence.  */
9755796c8dcSSimon Schubert };
9765796c8dcSSimon Schubert 
9775796c8dcSSimon Schubert struct fileinfo
9785796c8dcSSimon Schubert {
9795796c8dcSSimon Schubert   char *name;
9805796c8dcSSimon Schubert   unsigned int dir;
9815796c8dcSSimon Schubert   unsigned int time;
9825796c8dcSSimon Schubert   unsigned int size;
9835796c8dcSSimon Schubert };
9845796c8dcSSimon Schubert 
985cf7f2e2dSJohn Marino struct line_sequence
986cf7f2e2dSJohn Marino {
987cf7f2e2dSJohn Marino   bfd_vma               low_pc;
988cf7f2e2dSJohn Marino   struct line_sequence* prev_sequence;
989cf7f2e2dSJohn Marino   struct line_info*     last_line;  /* Largest VMA.  */
990cf7f2e2dSJohn Marino };
991cf7f2e2dSJohn Marino 
9925796c8dcSSimon Schubert struct line_info_table
9935796c8dcSSimon Schubert {
9945796c8dcSSimon Schubert   bfd*                  abfd;
9955796c8dcSSimon Schubert   unsigned int          num_files;
9965796c8dcSSimon Schubert   unsigned int          num_dirs;
997cf7f2e2dSJohn Marino   unsigned int          num_sequences;
9985796c8dcSSimon Schubert   char *                comp_dir;
9995796c8dcSSimon Schubert   char **               dirs;
10005796c8dcSSimon Schubert   struct fileinfo*      files;
1001cf7f2e2dSJohn Marino   struct line_sequence* sequences;
1002cf7f2e2dSJohn Marino   struct line_info*     lcl_head;   /* Local head; used in 'add_line_info'.  */
10035796c8dcSSimon Schubert };
10045796c8dcSSimon Schubert 
10055796c8dcSSimon Schubert /* Remember some information about each function.  If the function is
10065796c8dcSSimon Schubert    inlined (DW_TAG_inlined_subroutine) it may have two additional
10075796c8dcSSimon Schubert    attributes, DW_AT_call_file and DW_AT_call_line, which specify the
10085796c8dcSSimon Schubert    source code location where this function was inlined. */
10095796c8dcSSimon Schubert 
10105796c8dcSSimon Schubert struct funcinfo
10115796c8dcSSimon Schubert {
10125796c8dcSSimon Schubert   struct funcinfo *prev_func;		/* Pointer to previous function in list of all functions */
10135796c8dcSSimon Schubert   struct funcinfo *caller_func;		/* Pointer to function one scope higher */
10145796c8dcSSimon Schubert   char *caller_file;			/* Source location file name where caller_func inlines this func */
10155796c8dcSSimon Schubert   int caller_line;			/* Source location line number where caller_func inlines this func */
10165796c8dcSSimon Schubert   char *file;				/* Source location file name */
10175796c8dcSSimon Schubert   int line;				/* Source location line number */
10185796c8dcSSimon Schubert   int tag;
10195796c8dcSSimon Schubert   char *name;
10205796c8dcSSimon Schubert   struct arange arange;
10215796c8dcSSimon Schubert   asection *sec;			/* Where the symbol is defined */
10225796c8dcSSimon Schubert };
10235796c8dcSSimon Schubert 
10245796c8dcSSimon Schubert struct varinfo
10255796c8dcSSimon Schubert {
10265796c8dcSSimon Schubert   /* Pointer to previous variable in list of all variables */
10275796c8dcSSimon Schubert   struct varinfo *prev_var;
10285796c8dcSSimon Schubert   /* Source location file name */
10295796c8dcSSimon Schubert   char *file;
10305796c8dcSSimon Schubert   /* Source location line number */
10315796c8dcSSimon Schubert   int line;
10325796c8dcSSimon Schubert   int tag;
10335796c8dcSSimon Schubert   char *name;
10345796c8dcSSimon Schubert   bfd_vma addr;
10355796c8dcSSimon Schubert   /* Where the symbol is defined */
10365796c8dcSSimon Schubert   asection *sec;
10375796c8dcSSimon Schubert   /* Is this a stack variable? */
10385796c8dcSSimon Schubert   unsigned int stack: 1;
10395796c8dcSSimon Schubert };
10405796c8dcSSimon Schubert 
10415796c8dcSSimon Schubert /* Return TRUE if NEW_LINE should sort after LINE.  */
10425796c8dcSSimon Schubert 
10435796c8dcSSimon Schubert static inline bfd_boolean
10445796c8dcSSimon Schubert new_line_sorts_after (struct line_info *new_line, struct line_info *line)
10455796c8dcSSimon Schubert {
10465796c8dcSSimon Schubert   return (new_line->address > line->address
10475796c8dcSSimon Schubert 	  || (new_line->address == line->address
1048cf7f2e2dSJohn Marino 	      && (new_line->op_index > line->op_index
1049cf7f2e2dSJohn Marino 		  || (new_line->op_index == line->op_index
1050cf7f2e2dSJohn Marino 		      && new_line->end_sequence < line->end_sequence))));
10515796c8dcSSimon Schubert }
10525796c8dcSSimon Schubert 
10535796c8dcSSimon Schubert 
10545796c8dcSSimon Schubert /* Adds a new entry to the line_info list in the line_info_table, ensuring
10555796c8dcSSimon Schubert    that the list is sorted.  Note that the line_info list is sorted from
10565796c8dcSSimon Schubert    highest to lowest VMA (with possible duplicates); that is,
10575796c8dcSSimon Schubert    line_info->prev_line always accesses an equal or smaller VMA.  */
10585796c8dcSSimon Schubert 
1059cf7f2e2dSJohn Marino static bfd_boolean
10605796c8dcSSimon Schubert add_line_info (struct line_info_table *table,
10615796c8dcSSimon Schubert 	       bfd_vma address,
1062cf7f2e2dSJohn Marino 	       unsigned char op_index,
10635796c8dcSSimon Schubert 	       char *filename,
10645796c8dcSSimon Schubert 	       unsigned int line,
10655796c8dcSSimon Schubert 	       unsigned int column,
10665796c8dcSSimon Schubert 	       int end_sequence)
10675796c8dcSSimon Schubert {
10685796c8dcSSimon Schubert   bfd_size_type amt = sizeof (struct line_info);
1069cf7f2e2dSJohn Marino   struct line_sequence* seq = table->sequences;
10705796c8dcSSimon Schubert   struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
10715796c8dcSSimon Schubert 
1072cf7f2e2dSJohn Marino   if (info == NULL)
1073cf7f2e2dSJohn Marino     return FALSE;
1074cf7f2e2dSJohn Marino 
10755796c8dcSSimon Schubert   /* Set member data of 'info'.  */
1076cf7f2e2dSJohn Marino   info->prev_line = NULL;
10775796c8dcSSimon Schubert   info->address = address;
1078cf7f2e2dSJohn Marino   info->op_index = op_index;
10795796c8dcSSimon Schubert   info->line = line;
10805796c8dcSSimon Schubert   info->column = column;
10815796c8dcSSimon Schubert   info->end_sequence = end_sequence;
10825796c8dcSSimon Schubert 
10835796c8dcSSimon Schubert   if (filename && filename[0])
10845796c8dcSSimon Schubert     {
10855796c8dcSSimon Schubert       info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1086cf7f2e2dSJohn Marino       if (info->filename == NULL)
1087cf7f2e2dSJohn Marino 	return FALSE;
10885796c8dcSSimon Schubert       strcpy (info->filename, filename);
10895796c8dcSSimon Schubert     }
10905796c8dcSSimon Schubert   else
10915796c8dcSSimon Schubert     info->filename = NULL;
10925796c8dcSSimon Schubert 
10935796c8dcSSimon Schubert   /* Find the correct location for 'info'.  Normally we will receive
10945796c8dcSSimon Schubert      new line_info data 1) in order and 2) with increasing VMAs.
10955796c8dcSSimon Schubert      However some compilers break the rules (cf. decode_line_info) and
10965796c8dcSSimon Schubert      so we include some heuristics for quickly finding the correct
10975796c8dcSSimon Schubert      location for 'info'. In particular, these heuristics optimize for
10985796c8dcSSimon Schubert      the common case in which the VMA sequence that we receive is a
10995796c8dcSSimon Schubert      list of locally sorted VMAs such as
11005796c8dcSSimon Schubert        p...z a...j  (where a < j < p < z)
11015796c8dcSSimon Schubert 
11025796c8dcSSimon Schubert      Note: table->lcl_head is used to head an *actual* or *possible*
1103cf7f2e2dSJohn Marino      sub-sequence within the list (such as a...j) that is not directly
11045796c8dcSSimon Schubert      headed by table->last_line
11055796c8dcSSimon Schubert 
11065796c8dcSSimon Schubert      Note: we may receive duplicate entries from 'decode_line_info'.  */
11075796c8dcSSimon Schubert 
1108cf7f2e2dSJohn Marino   if (seq
1109cf7f2e2dSJohn Marino       && seq->last_line->address == address
1110cf7f2e2dSJohn Marino       && seq->last_line->op_index == op_index
1111cf7f2e2dSJohn Marino       && seq->last_line->end_sequence == end_sequence)
11125796c8dcSSimon Schubert     {
11135796c8dcSSimon Schubert       /* We only keep the last entry with the same address and end
11145796c8dcSSimon Schubert 	 sequence.  See PR ld/4986.  */
1115cf7f2e2dSJohn Marino       if (table->lcl_head == seq->last_line)
11165796c8dcSSimon Schubert 	table->lcl_head = info;
1117cf7f2e2dSJohn Marino       info->prev_line = seq->last_line->prev_line;
1118cf7f2e2dSJohn Marino       seq->last_line = info;
11195796c8dcSSimon Schubert     }
1120cf7f2e2dSJohn Marino   else if (!seq || seq->last_line->end_sequence)
11215796c8dcSSimon Schubert     {
1122cf7f2e2dSJohn Marino       /* Start a new line sequence.  */
1123cf7f2e2dSJohn Marino       amt = sizeof (struct line_sequence);
1124cf7f2e2dSJohn Marino       seq = (struct line_sequence *) bfd_malloc (amt);
1125cf7f2e2dSJohn Marino       if (seq == NULL)
1126cf7f2e2dSJohn Marino 	return FALSE;
1127cf7f2e2dSJohn Marino       seq->low_pc = address;
1128cf7f2e2dSJohn Marino       seq->prev_sequence = table->sequences;
1129cf7f2e2dSJohn Marino       seq->last_line = info;
1130cf7f2e2dSJohn Marino       table->lcl_head = info;
1131cf7f2e2dSJohn Marino       table->sequences = seq;
1132cf7f2e2dSJohn Marino       table->num_sequences++;
1133cf7f2e2dSJohn Marino     }
1134cf7f2e2dSJohn Marino   else if (new_line_sorts_after (info, seq->last_line))
1135cf7f2e2dSJohn Marino     {
1136cf7f2e2dSJohn Marino       /* Normal case: add 'info' to the beginning of the current sequence.  */
1137cf7f2e2dSJohn Marino       info->prev_line = seq->last_line;
1138cf7f2e2dSJohn Marino       seq->last_line = info;
11395796c8dcSSimon Schubert 
11405796c8dcSSimon Schubert       /* lcl_head: initialize to head a *possible* sequence at the end.  */
11415796c8dcSSimon Schubert       if (!table->lcl_head)
11425796c8dcSSimon Schubert 	table->lcl_head = info;
11435796c8dcSSimon Schubert     }
11445796c8dcSSimon Schubert   else if (!new_line_sorts_after (info, table->lcl_head)
11455796c8dcSSimon Schubert 	   && (!table->lcl_head->prev_line
11465796c8dcSSimon Schubert 	       || new_line_sorts_after (info, table->lcl_head->prev_line)))
11475796c8dcSSimon Schubert     {
11485796c8dcSSimon Schubert       /* Abnormal but easy: lcl_head is the head of 'info'.  */
11495796c8dcSSimon Schubert       info->prev_line = table->lcl_head->prev_line;
11505796c8dcSSimon Schubert       table->lcl_head->prev_line = info;
11515796c8dcSSimon Schubert     }
11525796c8dcSSimon Schubert   else
11535796c8dcSSimon Schubert     {
1154cf7f2e2dSJohn Marino       /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1155cf7f2e2dSJohn Marino 	 are valid heads for 'info'.  Reset 'lcl_head'.  */
1156cf7f2e2dSJohn Marino       struct line_info* li2 = seq->last_line; /* Always non-NULL.  */
11575796c8dcSSimon Schubert       struct line_info* li1 = li2->prev_line;
11585796c8dcSSimon Schubert 
11595796c8dcSSimon Schubert       while (li1)
11605796c8dcSSimon Schubert 	{
11615796c8dcSSimon Schubert 	  if (!new_line_sorts_after (info, li2)
11625796c8dcSSimon Schubert 	      && new_line_sorts_after (info, li1))
11635796c8dcSSimon Schubert 	    break;
11645796c8dcSSimon Schubert 
11655796c8dcSSimon Schubert 	  li2 = li1; /* always non-NULL */
11665796c8dcSSimon Schubert 	  li1 = li1->prev_line;
11675796c8dcSSimon Schubert 	}
11685796c8dcSSimon Schubert       table->lcl_head = li2;
11695796c8dcSSimon Schubert       info->prev_line = table->lcl_head->prev_line;
11705796c8dcSSimon Schubert       table->lcl_head->prev_line = info;
1171cf7f2e2dSJohn Marino       if (address < seq->low_pc)
1172cf7f2e2dSJohn Marino         seq->low_pc = address;
11735796c8dcSSimon Schubert     }
1174cf7f2e2dSJohn Marino   return TRUE;
11755796c8dcSSimon Schubert }
11765796c8dcSSimon Schubert 
11775796c8dcSSimon Schubert /* Extract a fully qualified filename from a line info table.
11785796c8dcSSimon Schubert    The returned string has been malloc'ed and it is the caller's
11795796c8dcSSimon Schubert    responsibility to free it.  */
11805796c8dcSSimon Schubert 
11815796c8dcSSimon Schubert static char *
11825796c8dcSSimon Schubert concat_filename (struct line_info_table *table, unsigned int file)
11835796c8dcSSimon Schubert {
11845796c8dcSSimon Schubert   char *filename;
11855796c8dcSSimon Schubert 
11865796c8dcSSimon Schubert   if (file - 1 >= table->num_files)
11875796c8dcSSimon Schubert     {
11885796c8dcSSimon Schubert       /* FILE == 0 means unknown.  */
11895796c8dcSSimon Schubert       if (file)
11905796c8dcSSimon Schubert 	(*_bfd_error_handler)
11915796c8dcSSimon Schubert 	  (_("Dwarf Error: mangled line number section (bad file number)."));
11925796c8dcSSimon Schubert       return strdup ("<unknown>");
11935796c8dcSSimon Schubert     }
11945796c8dcSSimon Schubert 
11955796c8dcSSimon Schubert   filename = table->files[file - 1].name;
11965796c8dcSSimon Schubert 
11975796c8dcSSimon Schubert   if (!IS_ABSOLUTE_PATH (filename))
11985796c8dcSSimon Schubert     {
1199cf7f2e2dSJohn Marino       char *dir_name = NULL;
1200cf7f2e2dSJohn Marino       char *subdir_name = NULL;
12015796c8dcSSimon Schubert       char *name;
12025796c8dcSSimon Schubert       size_t len;
12035796c8dcSSimon Schubert 
12045796c8dcSSimon Schubert       if (table->files[file - 1].dir)
1205cf7f2e2dSJohn Marino 	subdir_name = table->dirs[table->files[file - 1].dir - 1];
12065796c8dcSSimon Schubert 
1207cf7f2e2dSJohn Marino       if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1208cf7f2e2dSJohn Marino 	dir_name = table->comp_dir;
12095796c8dcSSimon Schubert 
1210cf7f2e2dSJohn Marino       if (!dir_name)
12115796c8dcSSimon Schubert 	{
1212cf7f2e2dSJohn Marino 	  dir_name = subdir_name;
1213cf7f2e2dSJohn Marino 	  subdir_name = NULL;
12145796c8dcSSimon Schubert 	}
12155796c8dcSSimon Schubert 
1216cf7f2e2dSJohn Marino       if (!dir_name)
12175796c8dcSSimon Schubert 	return strdup (filename);
12185796c8dcSSimon Schubert 
1219cf7f2e2dSJohn Marino       len = strlen (dir_name) + strlen (filename) + 2;
12205796c8dcSSimon Schubert 
1221cf7f2e2dSJohn Marino       if (subdir_name)
12225796c8dcSSimon Schubert 	{
1223cf7f2e2dSJohn Marino 	  len += strlen (subdir_name) + 1;
12245796c8dcSSimon Schubert 	  name = (char *) bfd_malloc (len);
12255796c8dcSSimon Schubert 	  if (name)
1226cf7f2e2dSJohn Marino 	    sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
12275796c8dcSSimon Schubert 	}
12285796c8dcSSimon Schubert       else
12295796c8dcSSimon Schubert 	{
12305796c8dcSSimon Schubert 	  name = (char *) bfd_malloc (len);
12315796c8dcSSimon Schubert 	  if (name)
1232cf7f2e2dSJohn Marino 	    sprintf (name, "%s/%s", dir_name, filename);
12335796c8dcSSimon Schubert 	}
12345796c8dcSSimon Schubert 
12355796c8dcSSimon Schubert       return name;
12365796c8dcSSimon Schubert     }
12375796c8dcSSimon Schubert 
12385796c8dcSSimon Schubert   return strdup (filename);
12395796c8dcSSimon Schubert }
12405796c8dcSSimon Schubert 
1241cf7f2e2dSJohn Marino static bfd_boolean
1242cf7f2e2dSJohn Marino arange_add (bfd *abfd, struct arange *first_arange,
1243cf7f2e2dSJohn Marino 	    bfd_vma low_pc, bfd_vma high_pc)
12445796c8dcSSimon Schubert {
12455796c8dcSSimon Schubert   struct arange *arange;
12465796c8dcSSimon Schubert 
12475796c8dcSSimon Schubert   /* If the first arange is empty, use it. */
12485796c8dcSSimon Schubert   if (first_arange->high == 0)
12495796c8dcSSimon Schubert     {
12505796c8dcSSimon Schubert       first_arange->low = low_pc;
12515796c8dcSSimon Schubert       first_arange->high = high_pc;
1252cf7f2e2dSJohn Marino       return TRUE;
12535796c8dcSSimon Schubert     }
12545796c8dcSSimon Schubert 
12555796c8dcSSimon Schubert   /* Next see if we can cheaply extend an existing range.  */
12565796c8dcSSimon Schubert   arange = first_arange;
12575796c8dcSSimon Schubert   do
12585796c8dcSSimon Schubert     {
12595796c8dcSSimon Schubert       if (low_pc == arange->high)
12605796c8dcSSimon Schubert 	{
12615796c8dcSSimon Schubert 	  arange->high = high_pc;
1262cf7f2e2dSJohn Marino 	  return TRUE;
12635796c8dcSSimon Schubert 	}
12645796c8dcSSimon Schubert       if (high_pc == arange->low)
12655796c8dcSSimon Schubert 	{
12665796c8dcSSimon Schubert 	  arange->low = low_pc;
1267cf7f2e2dSJohn Marino 	  return TRUE;
12685796c8dcSSimon Schubert 	}
12695796c8dcSSimon Schubert       arange = arange->next;
12705796c8dcSSimon Schubert     }
12715796c8dcSSimon Schubert   while (arange);
12725796c8dcSSimon Schubert 
12735796c8dcSSimon Schubert   /* Need to allocate a new arange and insert it into the arange list.
12745796c8dcSSimon Schubert      Order isn't significant, so just insert after the first arange. */
12755796c8dcSSimon Schubert   arange = (struct arange *) bfd_zalloc (abfd, sizeof (*arange));
1276cf7f2e2dSJohn Marino   if (arange == NULL)
1277cf7f2e2dSJohn Marino     return FALSE;
12785796c8dcSSimon Schubert   arange->low = low_pc;
12795796c8dcSSimon Schubert   arange->high = high_pc;
12805796c8dcSSimon Schubert   arange->next = first_arange->next;
12815796c8dcSSimon Schubert   first_arange->next = arange;
1282cf7f2e2dSJohn Marino   return TRUE;
1283cf7f2e2dSJohn Marino }
1284cf7f2e2dSJohn Marino 
1285cf7f2e2dSJohn Marino /* Compare function for line sequences.  */
1286cf7f2e2dSJohn Marino 
1287cf7f2e2dSJohn Marino static int
1288cf7f2e2dSJohn Marino compare_sequences (const void* a, const void* b)
1289cf7f2e2dSJohn Marino {
1290cf7f2e2dSJohn Marino   const struct line_sequence* seq1 = a;
1291cf7f2e2dSJohn Marino   const struct line_sequence* seq2 = b;
1292cf7f2e2dSJohn Marino 
1293cf7f2e2dSJohn Marino   /* Sort by low_pc as the primary key.  */
1294cf7f2e2dSJohn Marino   if (seq1->low_pc < seq2->low_pc)
1295cf7f2e2dSJohn Marino     return -1;
1296cf7f2e2dSJohn Marino   if (seq1->low_pc > seq2->low_pc)
1297cf7f2e2dSJohn Marino     return 1;
1298cf7f2e2dSJohn Marino 
1299cf7f2e2dSJohn Marino   /* If low_pc values are equal, sort in reverse order of
1300cf7f2e2dSJohn Marino      high_pc, so that the largest region comes first.  */
1301cf7f2e2dSJohn Marino   if (seq1->last_line->address < seq2->last_line->address)
1302cf7f2e2dSJohn Marino     return 1;
1303cf7f2e2dSJohn Marino   if (seq1->last_line->address > seq2->last_line->address)
1304cf7f2e2dSJohn Marino     return -1;
1305cf7f2e2dSJohn Marino 
1306cf7f2e2dSJohn Marino   if (seq1->last_line->op_index < seq2->last_line->op_index)
1307cf7f2e2dSJohn Marino     return 1;
1308cf7f2e2dSJohn Marino   if (seq1->last_line->op_index > seq2->last_line->op_index)
1309cf7f2e2dSJohn Marino     return -1;
1310cf7f2e2dSJohn Marino 
1311cf7f2e2dSJohn Marino   return 0;
1312cf7f2e2dSJohn Marino }
1313cf7f2e2dSJohn Marino 
1314cf7f2e2dSJohn Marino /* Sort the line sequences for quick lookup.  */
1315cf7f2e2dSJohn Marino 
1316cf7f2e2dSJohn Marino static bfd_boolean
1317cf7f2e2dSJohn Marino sort_line_sequences (struct line_info_table* table)
1318cf7f2e2dSJohn Marino {
1319cf7f2e2dSJohn Marino   bfd_size_type amt;
1320cf7f2e2dSJohn Marino   struct line_sequence* sequences;
1321cf7f2e2dSJohn Marino   struct line_sequence* seq;
1322cf7f2e2dSJohn Marino   unsigned int n = 0;
1323cf7f2e2dSJohn Marino   unsigned int num_sequences = table->num_sequences;
1324cf7f2e2dSJohn Marino   bfd_vma last_high_pc;
1325cf7f2e2dSJohn Marino 
1326cf7f2e2dSJohn Marino   if (num_sequences == 0)
1327cf7f2e2dSJohn Marino     return TRUE;
1328cf7f2e2dSJohn Marino 
1329cf7f2e2dSJohn Marino   /* Allocate space for an array of sequences.  */
1330cf7f2e2dSJohn Marino   amt = sizeof (struct line_sequence) * num_sequences;
1331cf7f2e2dSJohn Marino   sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
1332cf7f2e2dSJohn Marino   if (sequences == NULL)
1333cf7f2e2dSJohn Marino     return FALSE;
1334cf7f2e2dSJohn Marino 
1335cf7f2e2dSJohn Marino   /* Copy the linked list into the array, freeing the original nodes.  */
1336cf7f2e2dSJohn Marino   seq = table->sequences;
1337cf7f2e2dSJohn Marino   for (n = 0; n < num_sequences; n++)
1338cf7f2e2dSJohn Marino     {
1339cf7f2e2dSJohn Marino       struct line_sequence* last_seq = seq;
1340cf7f2e2dSJohn Marino 
1341cf7f2e2dSJohn Marino       BFD_ASSERT (seq);
1342cf7f2e2dSJohn Marino       sequences[n].low_pc = seq->low_pc;
1343cf7f2e2dSJohn Marino       sequences[n].prev_sequence = NULL;
1344cf7f2e2dSJohn Marino       sequences[n].last_line = seq->last_line;
1345cf7f2e2dSJohn Marino       seq = seq->prev_sequence;
1346cf7f2e2dSJohn Marino       free (last_seq);
1347cf7f2e2dSJohn Marino     }
1348cf7f2e2dSJohn Marino   BFD_ASSERT (seq == NULL);
1349cf7f2e2dSJohn Marino 
1350cf7f2e2dSJohn Marino   qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1351cf7f2e2dSJohn Marino 
1352cf7f2e2dSJohn Marino   /* Make the list binary-searchable by trimming overlapping entries
1353cf7f2e2dSJohn Marino      and removing nested entries.  */
1354cf7f2e2dSJohn Marino   num_sequences = 1;
1355cf7f2e2dSJohn Marino   last_high_pc = sequences[0].last_line->address;
1356cf7f2e2dSJohn Marino   for (n = 1; n < table->num_sequences; n++)
1357cf7f2e2dSJohn Marino     {
1358cf7f2e2dSJohn Marino       if (sequences[n].low_pc < last_high_pc)
1359cf7f2e2dSJohn Marino         {
1360cf7f2e2dSJohn Marino 	  if (sequences[n].last_line->address <= last_high_pc)
1361cf7f2e2dSJohn Marino 	    /* Skip nested entries.  */
1362cf7f2e2dSJohn Marino 	    continue;
1363cf7f2e2dSJohn Marino 
1364cf7f2e2dSJohn Marino 	  /* Trim overlapping entries.  */
1365cf7f2e2dSJohn Marino 	  sequences[n].low_pc = last_high_pc;
1366cf7f2e2dSJohn Marino         }
1367cf7f2e2dSJohn Marino       last_high_pc = sequences[n].last_line->address;
1368cf7f2e2dSJohn Marino       if (n > num_sequences)
1369cf7f2e2dSJohn Marino         {
1370cf7f2e2dSJohn Marino           /* Close up the gap.  */
1371cf7f2e2dSJohn Marino           sequences[num_sequences].low_pc = sequences[n].low_pc;
1372cf7f2e2dSJohn Marino           sequences[num_sequences].last_line = sequences[n].last_line;
1373cf7f2e2dSJohn Marino         }
1374cf7f2e2dSJohn Marino       num_sequences++;
1375cf7f2e2dSJohn Marino     }
1376cf7f2e2dSJohn Marino 
1377cf7f2e2dSJohn Marino   table->sequences = sequences;
1378cf7f2e2dSJohn Marino   table->num_sequences = num_sequences;
1379cf7f2e2dSJohn Marino   return TRUE;
13805796c8dcSSimon Schubert }
13815796c8dcSSimon Schubert 
13825796c8dcSSimon Schubert /* Decode the line number information for UNIT.  */
13835796c8dcSSimon Schubert 
13845796c8dcSSimon Schubert static struct line_info_table*
13855796c8dcSSimon Schubert decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
13865796c8dcSSimon Schubert {
13875796c8dcSSimon Schubert   bfd *abfd = unit->abfd;
13885796c8dcSSimon Schubert   struct line_info_table* table;
13895796c8dcSSimon Schubert   bfd_byte *line_ptr;
13905796c8dcSSimon Schubert   bfd_byte *line_end;
13915796c8dcSSimon Schubert   struct line_head lh;
13925796c8dcSSimon Schubert   unsigned int i, bytes_read, offset_size;
13935796c8dcSSimon Schubert   char *cur_file, *cur_dir;
13945796c8dcSSimon Schubert   unsigned char op_code, extended_op, adj_opcode;
13955796c8dcSSimon Schubert   bfd_size_type amt;
13965796c8dcSSimon Schubert 
1397*c50c785cSJohn Marino   if (! read_section (abfd, debug_line, stash->syms, unit->line_offset,
13985796c8dcSSimon Schubert 		      &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1399cf7f2e2dSJohn Marino     return NULL;
14005796c8dcSSimon Schubert 
14015796c8dcSSimon Schubert   amt = sizeof (struct line_info_table);
14025796c8dcSSimon Schubert   table = (struct line_info_table *) bfd_alloc (abfd, amt);
1403cf7f2e2dSJohn Marino   if (table == NULL)
1404cf7f2e2dSJohn Marino     return NULL;
14055796c8dcSSimon Schubert   table->abfd = abfd;
14065796c8dcSSimon Schubert   table->comp_dir = unit->comp_dir;
14075796c8dcSSimon Schubert 
14085796c8dcSSimon Schubert   table->num_files = 0;
14095796c8dcSSimon Schubert   table->files = NULL;
14105796c8dcSSimon Schubert 
14115796c8dcSSimon Schubert   table->num_dirs = 0;
14125796c8dcSSimon Schubert   table->dirs = NULL;
14135796c8dcSSimon Schubert 
1414cf7f2e2dSJohn Marino   table->num_sequences = 0;
1415cf7f2e2dSJohn Marino   table->sequences = NULL;
1416cf7f2e2dSJohn Marino 
14175796c8dcSSimon Schubert   table->lcl_head = NULL;
14185796c8dcSSimon Schubert 
14195796c8dcSSimon Schubert   line_ptr = stash->dwarf_line_buffer + unit->line_offset;
14205796c8dcSSimon Schubert 
14215796c8dcSSimon Schubert   /* Read in the prologue.  */
14225796c8dcSSimon Schubert   lh.total_length = read_4_bytes (abfd, line_ptr);
14235796c8dcSSimon Schubert   line_ptr += 4;
14245796c8dcSSimon Schubert   offset_size = 4;
14255796c8dcSSimon Schubert   if (lh.total_length == 0xffffffff)
14265796c8dcSSimon Schubert     {
14275796c8dcSSimon Schubert       lh.total_length = read_8_bytes (abfd, line_ptr);
14285796c8dcSSimon Schubert       line_ptr += 8;
14295796c8dcSSimon Schubert       offset_size = 8;
14305796c8dcSSimon Schubert     }
14315796c8dcSSimon Schubert   else if (lh.total_length == 0 && unit->addr_size == 8)
14325796c8dcSSimon Schubert     {
14335796c8dcSSimon Schubert       /* Handle (non-standard) 64-bit DWARF2 formats.  */
14345796c8dcSSimon Schubert       lh.total_length = read_4_bytes (abfd, line_ptr);
14355796c8dcSSimon Schubert       line_ptr += 4;
14365796c8dcSSimon Schubert       offset_size = 8;
14375796c8dcSSimon Schubert     }
14385796c8dcSSimon Schubert   line_end = line_ptr + lh.total_length;
14395796c8dcSSimon Schubert   lh.version = read_2_bytes (abfd, line_ptr);
1440cf7f2e2dSJohn Marino   if (lh.version < 2 || lh.version > 4)
1441cf7f2e2dSJohn Marino     {
1442cf7f2e2dSJohn Marino       (*_bfd_error_handler)
1443cf7f2e2dSJohn Marino 	(_("Dwarf Error: Unhandled .debug_line version %d."), lh.version);
1444cf7f2e2dSJohn Marino       bfd_set_error (bfd_error_bad_value);
1445cf7f2e2dSJohn Marino       return NULL;
1446cf7f2e2dSJohn Marino     }
14475796c8dcSSimon Schubert   line_ptr += 2;
14485796c8dcSSimon Schubert   if (offset_size == 4)
14495796c8dcSSimon Schubert     lh.prologue_length = read_4_bytes (abfd, line_ptr);
14505796c8dcSSimon Schubert   else
14515796c8dcSSimon Schubert     lh.prologue_length = read_8_bytes (abfd, line_ptr);
14525796c8dcSSimon Schubert   line_ptr += offset_size;
14535796c8dcSSimon Schubert   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
14545796c8dcSSimon Schubert   line_ptr += 1;
1455cf7f2e2dSJohn Marino   if (lh.version >= 4)
1456cf7f2e2dSJohn Marino     {
1457cf7f2e2dSJohn Marino       lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr);
1458cf7f2e2dSJohn Marino       line_ptr += 1;
1459cf7f2e2dSJohn Marino     }
1460cf7f2e2dSJohn Marino   else
1461cf7f2e2dSJohn Marino     lh.maximum_ops_per_insn = 1;
1462cf7f2e2dSJohn Marino   if (lh.maximum_ops_per_insn == 0)
1463cf7f2e2dSJohn Marino     {
1464cf7f2e2dSJohn Marino       (*_bfd_error_handler)
1465cf7f2e2dSJohn Marino 	(_("Dwarf Error: Invalid maximum operations per instruction."));
1466cf7f2e2dSJohn Marino       bfd_set_error (bfd_error_bad_value);
1467cf7f2e2dSJohn Marino       return NULL;
1468cf7f2e2dSJohn Marino     }
14695796c8dcSSimon Schubert   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
14705796c8dcSSimon Schubert   line_ptr += 1;
14715796c8dcSSimon Schubert   lh.line_base = read_1_signed_byte (abfd, line_ptr);
14725796c8dcSSimon Schubert   line_ptr += 1;
14735796c8dcSSimon Schubert   lh.line_range = read_1_byte (abfd, line_ptr);
14745796c8dcSSimon Schubert   line_ptr += 1;
14755796c8dcSSimon Schubert   lh.opcode_base = read_1_byte (abfd, line_ptr);
14765796c8dcSSimon Schubert   line_ptr += 1;
14775796c8dcSSimon Schubert   amt = lh.opcode_base * sizeof (unsigned char);
14785796c8dcSSimon Schubert   lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
14795796c8dcSSimon Schubert 
14805796c8dcSSimon Schubert   lh.standard_opcode_lengths[0] = 1;
14815796c8dcSSimon Schubert 
14825796c8dcSSimon Schubert   for (i = 1; i < lh.opcode_base; ++i)
14835796c8dcSSimon Schubert     {
14845796c8dcSSimon Schubert       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14855796c8dcSSimon Schubert       line_ptr += 1;
14865796c8dcSSimon Schubert     }
14875796c8dcSSimon Schubert 
14885796c8dcSSimon Schubert   /* Read directory table.  */
14895796c8dcSSimon Schubert   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
14905796c8dcSSimon Schubert     {
14915796c8dcSSimon Schubert       line_ptr += bytes_read;
14925796c8dcSSimon Schubert 
14935796c8dcSSimon Schubert       if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
14945796c8dcSSimon Schubert 	{
14955796c8dcSSimon Schubert 	  char **tmp;
14965796c8dcSSimon Schubert 
14975796c8dcSSimon Schubert 	  amt = table->num_dirs + DIR_ALLOC_CHUNK;
14985796c8dcSSimon Schubert 	  amt *= sizeof (char *);
14995796c8dcSSimon Schubert 
15005796c8dcSSimon Schubert 	  tmp = (char **) bfd_realloc (table->dirs, amt);
15015796c8dcSSimon Schubert 	  if (tmp == NULL)
1502cf7f2e2dSJohn Marino 	    goto fail;
15035796c8dcSSimon Schubert 	  table->dirs = tmp;
15045796c8dcSSimon Schubert 	}
15055796c8dcSSimon Schubert 
15065796c8dcSSimon Schubert       table->dirs[table->num_dirs++] = cur_dir;
15075796c8dcSSimon Schubert     }
15085796c8dcSSimon Schubert 
15095796c8dcSSimon Schubert   line_ptr += bytes_read;
15105796c8dcSSimon Schubert 
15115796c8dcSSimon Schubert   /* Read file name table.  */
15125796c8dcSSimon Schubert   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
15135796c8dcSSimon Schubert     {
15145796c8dcSSimon Schubert       line_ptr += bytes_read;
15155796c8dcSSimon Schubert 
15165796c8dcSSimon Schubert       if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
15175796c8dcSSimon Schubert 	{
15185796c8dcSSimon Schubert 	  struct fileinfo *tmp;
15195796c8dcSSimon Schubert 
15205796c8dcSSimon Schubert 	  amt = table->num_files + FILE_ALLOC_CHUNK;
15215796c8dcSSimon Schubert 	  amt *= sizeof (struct fileinfo);
15225796c8dcSSimon Schubert 
15235796c8dcSSimon Schubert 	  tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
15245796c8dcSSimon Schubert 	  if (tmp == NULL)
1525cf7f2e2dSJohn Marino 	    goto fail;
15265796c8dcSSimon Schubert 	  table->files = tmp;
15275796c8dcSSimon Schubert 	}
15285796c8dcSSimon Schubert 
15295796c8dcSSimon Schubert       table->files[table->num_files].name = cur_file;
15305796c8dcSSimon Schubert       table->files[table->num_files].dir =
15315796c8dcSSimon Schubert 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15325796c8dcSSimon Schubert       line_ptr += bytes_read;
15335796c8dcSSimon Schubert       table->files[table->num_files].time =
15345796c8dcSSimon Schubert 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15355796c8dcSSimon Schubert       line_ptr += bytes_read;
15365796c8dcSSimon Schubert       table->files[table->num_files].size =
15375796c8dcSSimon Schubert 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15385796c8dcSSimon Schubert       line_ptr += bytes_read;
15395796c8dcSSimon Schubert       table->num_files++;
15405796c8dcSSimon Schubert     }
15415796c8dcSSimon Schubert 
15425796c8dcSSimon Schubert   line_ptr += bytes_read;
15435796c8dcSSimon Schubert 
15445796c8dcSSimon Schubert   /* Read the statement sequences until there's nothing left.  */
15455796c8dcSSimon Schubert   while (line_ptr < line_end)
15465796c8dcSSimon Schubert     {
15475796c8dcSSimon Schubert       /* State machine registers.  */
15485796c8dcSSimon Schubert       bfd_vma address = 0;
1549cf7f2e2dSJohn Marino       unsigned char op_index = 0;
15505796c8dcSSimon Schubert       char * filename = table->num_files ? concat_filename (table, 1) : NULL;
15515796c8dcSSimon Schubert       unsigned int line = 1;
15525796c8dcSSimon Schubert       unsigned int column = 0;
15535796c8dcSSimon Schubert       int is_stmt = lh.default_is_stmt;
15545796c8dcSSimon Schubert       int end_sequence = 0;
15555796c8dcSSimon Schubert       /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
15565796c8dcSSimon Schubert 	 compilers generate address sequences that are wildly out of
15575796c8dcSSimon Schubert 	 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
15585796c8dcSSimon Schubert 	 for ia64-Linux).  Thus, to determine the low and high
15595796c8dcSSimon Schubert 	 address, we must compare on every DW_LNS_copy, etc.  */
15605796c8dcSSimon Schubert       bfd_vma low_pc  = (bfd_vma) -1;
15615796c8dcSSimon Schubert       bfd_vma high_pc = 0;
15625796c8dcSSimon Schubert 
15635796c8dcSSimon Schubert       /* Decode the table.  */
15645796c8dcSSimon Schubert       while (! end_sequence)
15655796c8dcSSimon Schubert 	{
15665796c8dcSSimon Schubert 	  op_code = read_1_byte (abfd, line_ptr);
15675796c8dcSSimon Schubert 	  line_ptr += 1;
15685796c8dcSSimon Schubert 
15695796c8dcSSimon Schubert 	  if (op_code >= lh.opcode_base)
15705796c8dcSSimon Schubert 	    {
15715796c8dcSSimon Schubert 	      /* Special operand.  */
15725796c8dcSSimon Schubert 	      adj_opcode = op_code - lh.opcode_base;
1573cf7f2e2dSJohn Marino 	      if (lh.maximum_ops_per_insn == 1)
15745796c8dcSSimon Schubert 		address += (adj_opcode / lh.line_range)
15755796c8dcSSimon Schubert 			   * lh.minimum_instruction_length;
1576cf7f2e2dSJohn Marino 	      else
1577cf7f2e2dSJohn Marino 		{
1578cf7f2e2dSJohn Marino 		  address += ((op_index + (adj_opcode / lh.line_range))
1579cf7f2e2dSJohn Marino 			      / lh.maximum_ops_per_insn)
1580cf7f2e2dSJohn Marino 			     * lh.minimum_instruction_length;
1581cf7f2e2dSJohn Marino 		  op_index = (op_index + (adj_opcode / lh.line_range))
1582cf7f2e2dSJohn Marino 			     % lh.maximum_ops_per_insn;
1583cf7f2e2dSJohn Marino 		}
15845796c8dcSSimon Schubert 	      line += lh.line_base + (adj_opcode % lh.line_range);
15855796c8dcSSimon Schubert 	      /* Append row to matrix using current values.  */
1586cf7f2e2dSJohn Marino 	      if (!add_line_info (table, address, op_index, filename,
1587cf7f2e2dSJohn Marino 				  line, column, 0))
1588cf7f2e2dSJohn Marino 		goto line_fail;
15895796c8dcSSimon Schubert 	      if (address < low_pc)
15905796c8dcSSimon Schubert 		low_pc = address;
15915796c8dcSSimon Schubert 	      if (address > high_pc)
15925796c8dcSSimon Schubert 		high_pc = address;
15935796c8dcSSimon Schubert 	    }
15945796c8dcSSimon Schubert 	  else switch (op_code)
15955796c8dcSSimon Schubert 	    {
15965796c8dcSSimon Schubert 	    case DW_LNS_extended_op:
15975796c8dcSSimon Schubert 	      /* Ignore length.  */
15985796c8dcSSimon Schubert 	      line_ptr += 1;
15995796c8dcSSimon Schubert 	      extended_op = read_1_byte (abfd, line_ptr);
16005796c8dcSSimon Schubert 	      line_ptr += 1;
16015796c8dcSSimon Schubert 
16025796c8dcSSimon Schubert 	      switch (extended_op)
16035796c8dcSSimon Schubert 		{
16045796c8dcSSimon Schubert 		case DW_LNE_end_sequence:
16055796c8dcSSimon Schubert 		  end_sequence = 1;
1606cf7f2e2dSJohn Marino 		  if (!add_line_info (table, address, op_index, filename,
1607cf7f2e2dSJohn Marino 				      line, column, end_sequence))
1608cf7f2e2dSJohn Marino 		    goto line_fail;
16095796c8dcSSimon Schubert 		  if (address < low_pc)
16105796c8dcSSimon Schubert 		    low_pc = address;
16115796c8dcSSimon Schubert 		  if (address > high_pc)
16125796c8dcSSimon Schubert 		    high_pc = address;
1613cf7f2e2dSJohn Marino 		  if (!arange_add (unit->abfd, &unit->arange, low_pc, high_pc))
1614cf7f2e2dSJohn Marino 		    goto line_fail;
16155796c8dcSSimon Schubert 		  break;
16165796c8dcSSimon Schubert 		case DW_LNE_set_address:
16175796c8dcSSimon Schubert 		  address = read_address (unit, line_ptr);
1618cf7f2e2dSJohn Marino 		  op_index = 0;
16195796c8dcSSimon Schubert 		  line_ptr += unit->addr_size;
16205796c8dcSSimon Schubert 		  break;
16215796c8dcSSimon Schubert 		case DW_LNE_define_file:
16225796c8dcSSimon Schubert 		  cur_file = read_string (abfd, line_ptr, &bytes_read);
16235796c8dcSSimon Schubert 		  line_ptr += bytes_read;
16245796c8dcSSimon Schubert 		  if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
16255796c8dcSSimon Schubert 		    {
16265796c8dcSSimon Schubert 		      struct fileinfo *tmp;
16275796c8dcSSimon Schubert 
16285796c8dcSSimon Schubert 		      amt = table->num_files + FILE_ALLOC_CHUNK;
16295796c8dcSSimon Schubert 		      amt *= sizeof (struct fileinfo);
16305796c8dcSSimon Schubert 		      tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
16315796c8dcSSimon Schubert 		      if (tmp == NULL)
1632cf7f2e2dSJohn Marino 			goto line_fail;
16335796c8dcSSimon Schubert 		      table->files = tmp;
16345796c8dcSSimon Schubert 		    }
16355796c8dcSSimon Schubert 		  table->files[table->num_files].name = cur_file;
16365796c8dcSSimon Schubert 		  table->files[table->num_files].dir =
16375796c8dcSSimon Schubert 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16385796c8dcSSimon Schubert 		  line_ptr += bytes_read;
16395796c8dcSSimon Schubert 		  table->files[table->num_files].time =
16405796c8dcSSimon Schubert 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16415796c8dcSSimon Schubert 		  line_ptr += bytes_read;
16425796c8dcSSimon Schubert 		  table->files[table->num_files].size =
16435796c8dcSSimon Schubert 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16445796c8dcSSimon Schubert 		  line_ptr += bytes_read;
16455796c8dcSSimon Schubert 		  table->num_files++;
16465796c8dcSSimon Schubert 		  break;
16475796c8dcSSimon Schubert 		case DW_LNE_set_discriminator:
16485796c8dcSSimon Schubert 		  (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16495796c8dcSSimon Schubert 		  line_ptr += bytes_read;
16505796c8dcSSimon Schubert 		  break;
16515796c8dcSSimon Schubert 		default:
16525796c8dcSSimon Schubert 		  (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
16535796c8dcSSimon Schubert 		  bfd_set_error (bfd_error_bad_value);
1654cf7f2e2dSJohn Marino 		line_fail:
1655cf7f2e2dSJohn Marino 		  if (filename != NULL)
16565796c8dcSSimon Schubert 		    free (filename);
1657cf7f2e2dSJohn Marino 		  goto fail;
16585796c8dcSSimon Schubert 		}
16595796c8dcSSimon Schubert 	      break;
16605796c8dcSSimon Schubert 	    case DW_LNS_copy:
1661cf7f2e2dSJohn Marino 	      if (!add_line_info (table, address, op_index,
1662cf7f2e2dSJohn Marino 				  filename, line, column, 0))
1663cf7f2e2dSJohn Marino 		goto line_fail;
16645796c8dcSSimon Schubert 	      if (address < low_pc)
16655796c8dcSSimon Schubert 		low_pc = address;
16665796c8dcSSimon Schubert 	      if (address > high_pc)
16675796c8dcSSimon Schubert 		high_pc = address;
16685796c8dcSSimon Schubert 	      break;
16695796c8dcSSimon Schubert 	    case DW_LNS_advance_pc:
1670cf7f2e2dSJohn Marino 	      if (lh.maximum_ops_per_insn == 1)
16715796c8dcSSimon Schubert 		address += lh.minimum_instruction_length
1672cf7f2e2dSJohn Marino 			   * read_unsigned_leb128 (abfd, line_ptr,
1673cf7f2e2dSJohn Marino 						   &bytes_read);
1674cf7f2e2dSJohn Marino 	      else
1675cf7f2e2dSJohn Marino 		{
1676cf7f2e2dSJohn Marino 		  bfd_vma adjust = read_unsigned_leb128 (abfd, line_ptr,
1677cf7f2e2dSJohn Marino 							 &bytes_read);
1678cf7f2e2dSJohn Marino 		  address = ((op_index + adjust) / lh.maximum_ops_per_insn)
1679cf7f2e2dSJohn Marino 			    * lh.minimum_instruction_length;
1680cf7f2e2dSJohn Marino 		  op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
1681cf7f2e2dSJohn Marino 		}
16825796c8dcSSimon Schubert 	      line_ptr += bytes_read;
16835796c8dcSSimon Schubert 	      break;
16845796c8dcSSimon Schubert 	    case DW_LNS_advance_line:
16855796c8dcSSimon Schubert 	      line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16865796c8dcSSimon Schubert 	      line_ptr += bytes_read;
16875796c8dcSSimon Schubert 	      break;
16885796c8dcSSimon Schubert 	    case DW_LNS_set_file:
16895796c8dcSSimon Schubert 	      {
16905796c8dcSSimon Schubert 		unsigned int file;
16915796c8dcSSimon Schubert 
16925796c8dcSSimon Schubert 		/* The file and directory tables are 0
16935796c8dcSSimon Schubert 		   based, the references are 1 based.  */
16945796c8dcSSimon Schubert 		file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16955796c8dcSSimon Schubert 		line_ptr += bytes_read;
16965796c8dcSSimon Schubert 		if (filename)
16975796c8dcSSimon Schubert 		  free (filename);
16985796c8dcSSimon Schubert 		filename = concat_filename (table, file);
16995796c8dcSSimon Schubert 		break;
17005796c8dcSSimon Schubert 	      }
17015796c8dcSSimon Schubert 	    case DW_LNS_set_column:
17025796c8dcSSimon Schubert 	      column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17035796c8dcSSimon Schubert 	      line_ptr += bytes_read;
17045796c8dcSSimon Schubert 	      break;
17055796c8dcSSimon Schubert 	    case DW_LNS_negate_stmt:
17065796c8dcSSimon Schubert 	      is_stmt = (!is_stmt);
17075796c8dcSSimon Schubert 	      break;
17085796c8dcSSimon Schubert 	    case DW_LNS_set_basic_block:
17095796c8dcSSimon Schubert 	      break;
17105796c8dcSSimon Schubert 	    case DW_LNS_const_add_pc:
1711cf7f2e2dSJohn Marino 	      if (lh.maximum_ops_per_insn == 1)
17125796c8dcSSimon Schubert 		address += lh.minimum_instruction_length
17135796c8dcSSimon Schubert 			   * ((255 - lh.opcode_base) / lh.line_range);
1714cf7f2e2dSJohn Marino 	      else
1715cf7f2e2dSJohn Marino 		{
1716cf7f2e2dSJohn Marino 		  bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
1717cf7f2e2dSJohn Marino 		  address += lh.minimum_instruction_length
1718cf7f2e2dSJohn Marino 			     * ((op_index + adjust) / lh.maximum_ops_per_insn);
1719cf7f2e2dSJohn Marino 		  op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
1720cf7f2e2dSJohn Marino 		}
17215796c8dcSSimon Schubert 	      break;
17225796c8dcSSimon Schubert 	    case DW_LNS_fixed_advance_pc:
17235796c8dcSSimon Schubert 	      address += read_2_bytes (abfd, line_ptr);
1724cf7f2e2dSJohn Marino 	      op_index = 0;
17255796c8dcSSimon Schubert 	      line_ptr += 2;
17265796c8dcSSimon Schubert 	      break;
17275796c8dcSSimon Schubert 	    default:
17285796c8dcSSimon Schubert 	      /* Unknown standard opcode, ignore it.  */
17295796c8dcSSimon Schubert 	      for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
17305796c8dcSSimon Schubert 		{
17315796c8dcSSimon Schubert 		  (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17325796c8dcSSimon Schubert 		  line_ptr += bytes_read;
17335796c8dcSSimon Schubert 		}
1734cf7f2e2dSJohn Marino 	      break;
17355796c8dcSSimon Schubert 	    }
17365796c8dcSSimon Schubert 	}
17375796c8dcSSimon Schubert 
17385796c8dcSSimon Schubert       if (filename)
17395796c8dcSSimon Schubert 	free (filename);
17405796c8dcSSimon Schubert     }
17415796c8dcSSimon Schubert 
1742cf7f2e2dSJohn Marino   if (sort_line_sequences (table))
17435796c8dcSSimon Schubert     return table;
1744cf7f2e2dSJohn Marino 
1745cf7f2e2dSJohn Marino  fail:
1746cf7f2e2dSJohn Marino   if (table->sequences != NULL)
1747cf7f2e2dSJohn Marino     free (table->sequences);
1748cf7f2e2dSJohn Marino   if (table->files != NULL)
1749cf7f2e2dSJohn Marino     free (table->files);
1750cf7f2e2dSJohn Marino   if (table->dirs != NULL)
1751cf7f2e2dSJohn Marino     free (table->dirs);
1752cf7f2e2dSJohn Marino   return NULL;
17535796c8dcSSimon Schubert }
17545796c8dcSSimon Schubert 
17555796c8dcSSimon Schubert /* If ADDR is within TABLE set the output parameters and return TRUE,
17565796c8dcSSimon Schubert    otherwise return FALSE.  The output parameters, FILENAME_PTR and
17575796c8dcSSimon Schubert    LINENUMBER_PTR, are pointers to the objects to be filled in.  */
17585796c8dcSSimon Schubert 
17595796c8dcSSimon Schubert static bfd_boolean
17605796c8dcSSimon Schubert lookup_address_in_line_info_table (struct line_info_table *table,
17615796c8dcSSimon Schubert 				   bfd_vma addr,
17625796c8dcSSimon Schubert 				   const char **filename_ptr,
17635796c8dcSSimon Schubert 				   unsigned int *linenumber_ptr)
17645796c8dcSSimon Schubert {
1765cf7f2e2dSJohn Marino   struct line_sequence *seq = NULL;
1766cf7f2e2dSJohn Marino   struct line_info *each_line;
1767cf7f2e2dSJohn Marino   int low, high, mid;
17685796c8dcSSimon Schubert 
1769cf7f2e2dSJohn Marino   /* Binary search the array of sequences.  */
1770cf7f2e2dSJohn Marino   low = 0;
1771cf7f2e2dSJohn Marino   high = table->num_sequences;
1772cf7f2e2dSJohn Marino   while (low < high)
17735796c8dcSSimon Schubert     {
1774cf7f2e2dSJohn Marino       mid = (low + high) / 2;
1775cf7f2e2dSJohn Marino       seq = &table->sequences[mid];
1776cf7f2e2dSJohn Marino       if (addr < seq->low_pc)
1777cf7f2e2dSJohn Marino 	high = mid;
1778cf7f2e2dSJohn Marino       else if (addr >= seq->last_line->address)
1779cf7f2e2dSJohn Marino 	low = mid + 1;
17805796c8dcSSimon Schubert       else
1781cf7f2e2dSJohn Marino 	break;
1782cf7f2e2dSJohn Marino     }
1783cf7f2e2dSJohn Marino 
1784cf7f2e2dSJohn Marino   if (seq && addr >= seq->low_pc && addr < seq->last_line->address)
1785cf7f2e2dSJohn Marino     {
1786cf7f2e2dSJohn Marino       /* Note: seq->last_line should be a descendingly sorted list.  */
1787cf7f2e2dSJohn Marino       for (each_line = seq->last_line;
1788cf7f2e2dSJohn Marino            each_line;
1789cf7f2e2dSJohn Marino            each_line = each_line->prev_line)
1790cf7f2e2dSJohn Marino         if (addr >= each_line->address)
1791cf7f2e2dSJohn Marino           break;
1792cf7f2e2dSJohn Marino 
1793cf7f2e2dSJohn Marino       if (each_line
1794cf7f2e2dSJohn Marino           && !(each_line->end_sequence || each_line == seq->last_line))
17955796c8dcSSimon Schubert         {
17965796c8dcSSimon Schubert           *filename_ptr = each_line->filename;
17975796c8dcSSimon Schubert           *linenumber_ptr = each_line->line;
17985796c8dcSSimon Schubert           return TRUE;
17995796c8dcSSimon Schubert         }
1800cf7f2e2dSJohn Marino     }
18015796c8dcSSimon Schubert 
1802cf7f2e2dSJohn Marino   *filename_ptr = NULL;
18035796c8dcSSimon Schubert   return FALSE;
18045796c8dcSSimon Schubert }
18055796c8dcSSimon Schubert 
1806cf7f2e2dSJohn Marino /* Read in the .debug_ranges section for future reference.  */
18075796c8dcSSimon Schubert 
18085796c8dcSSimon Schubert static bfd_boolean
18095796c8dcSSimon Schubert read_debug_ranges (struct comp_unit *unit)
18105796c8dcSSimon Schubert {
18115796c8dcSSimon Schubert   struct dwarf2_debug *stash = unit->stash;
1812*c50c785cSJohn Marino   return read_section (unit->abfd, debug_ranges, stash->syms, 0,
18135796c8dcSSimon Schubert 		       &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
18145796c8dcSSimon Schubert }
18155796c8dcSSimon Schubert 
18165796c8dcSSimon Schubert /* Function table functions.  */
18175796c8dcSSimon Schubert 
18185796c8dcSSimon Schubert /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
18195796c8dcSSimon Schubert    Note that we need to find the function that has the smallest
18205796c8dcSSimon Schubert    range that contains ADDR, to handle inlined functions without
18215796c8dcSSimon Schubert    depending upon them being ordered in TABLE by increasing range. */
18225796c8dcSSimon Schubert 
18235796c8dcSSimon Schubert static bfd_boolean
18245796c8dcSSimon Schubert lookup_address_in_function_table (struct comp_unit *unit,
18255796c8dcSSimon Schubert 				  bfd_vma addr,
18265796c8dcSSimon Schubert 				  struct funcinfo **function_ptr,
18275796c8dcSSimon Schubert 				  const char **functionname_ptr)
18285796c8dcSSimon Schubert {
18295796c8dcSSimon Schubert   struct funcinfo* each_func;
18305796c8dcSSimon Schubert   struct funcinfo* best_fit = NULL;
18315796c8dcSSimon Schubert   struct arange *arange;
18325796c8dcSSimon Schubert 
18335796c8dcSSimon Schubert   for (each_func = unit->function_table;
18345796c8dcSSimon Schubert        each_func;
18355796c8dcSSimon Schubert        each_func = each_func->prev_func)
18365796c8dcSSimon Schubert     {
18375796c8dcSSimon Schubert       for (arange = &each_func->arange;
18385796c8dcSSimon Schubert 	   arange;
18395796c8dcSSimon Schubert 	   arange = arange->next)
18405796c8dcSSimon Schubert 	{
18415796c8dcSSimon Schubert 	  if (addr >= arange->low && addr < arange->high)
18425796c8dcSSimon Schubert 	    {
18435796c8dcSSimon Schubert 	      if (!best_fit ||
18445796c8dcSSimon Schubert 		  ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
18455796c8dcSSimon Schubert 		best_fit = each_func;
18465796c8dcSSimon Schubert 	    }
18475796c8dcSSimon Schubert 	}
18485796c8dcSSimon Schubert     }
18495796c8dcSSimon Schubert 
18505796c8dcSSimon Schubert   if (best_fit)
18515796c8dcSSimon Schubert     {
18525796c8dcSSimon Schubert       *functionname_ptr = best_fit->name;
18535796c8dcSSimon Schubert       *function_ptr = best_fit;
18545796c8dcSSimon Schubert       return TRUE;
18555796c8dcSSimon Schubert     }
18565796c8dcSSimon Schubert   else
18575796c8dcSSimon Schubert     {
18585796c8dcSSimon Schubert       return FALSE;
18595796c8dcSSimon Schubert     }
18605796c8dcSSimon Schubert }
18615796c8dcSSimon Schubert 
18625796c8dcSSimon Schubert /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
18635796c8dcSSimon Schubert    and LINENUMBER_PTR, and return TRUE.  */
18645796c8dcSSimon Schubert 
18655796c8dcSSimon Schubert static bfd_boolean
18665796c8dcSSimon Schubert lookup_symbol_in_function_table (struct comp_unit *unit,
18675796c8dcSSimon Schubert 				 asymbol *sym,
18685796c8dcSSimon Schubert 				 bfd_vma addr,
18695796c8dcSSimon Schubert 				 const char **filename_ptr,
18705796c8dcSSimon Schubert 				 unsigned int *linenumber_ptr)
18715796c8dcSSimon Schubert {
18725796c8dcSSimon Schubert   struct funcinfo* each_func;
18735796c8dcSSimon Schubert   struct funcinfo* best_fit = NULL;
18745796c8dcSSimon Schubert   struct arange *arange;
18755796c8dcSSimon Schubert   const char *name = bfd_asymbol_name (sym);
18765796c8dcSSimon Schubert   asection *sec = bfd_get_section (sym);
18775796c8dcSSimon Schubert 
18785796c8dcSSimon Schubert   for (each_func = unit->function_table;
18795796c8dcSSimon Schubert        each_func;
18805796c8dcSSimon Schubert        each_func = each_func->prev_func)
18815796c8dcSSimon Schubert     {
18825796c8dcSSimon Schubert       for (arange = &each_func->arange;
18835796c8dcSSimon Schubert 	   arange;
18845796c8dcSSimon Schubert 	   arange = arange->next)
18855796c8dcSSimon Schubert 	{
18865796c8dcSSimon Schubert 	  if ((!each_func->sec || each_func->sec == sec)
18875796c8dcSSimon Schubert 	      && addr >= arange->low
18885796c8dcSSimon Schubert 	      && addr < arange->high
18895796c8dcSSimon Schubert 	      && each_func->name
18905796c8dcSSimon Schubert 	      && strcmp (name, each_func->name) == 0
18915796c8dcSSimon Schubert 	      && (!best_fit
18925796c8dcSSimon Schubert 		  || ((arange->high - arange->low)
18935796c8dcSSimon Schubert 		      < (best_fit->arange.high - best_fit->arange.low))))
18945796c8dcSSimon Schubert 	    best_fit = each_func;
18955796c8dcSSimon Schubert 	}
18965796c8dcSSimon Schubert     }
18975796c8dcSSimon Schubert 
18985796c8dcSSimon Schubert   if (best_fit)
18995796c8dcSSimon Schubert     {
19005796c8dcSSimon Schubert       best_fit->sec = sec;
19015796c8dcSSimon Schubert       *filename_ptr = best_fit->file;
19025796c8dcSSimon Schubert       *linenumber_ptr = best_fit->line;
19035796c8dcSSimon Schubert       return TRUE;
19045796c8dcSSimon Schubert     }
19055796c8dcSSimon Schubert   else
19065796c8dcSSimon Schubert     return FALSE;
19075796c8dcSSimon Schubert }
19085796c8dcSSimon Schubert 
19095796c8dcSSimon Schubert /* Variable table functions.  */
19105796c8dcSSimon Schubert 
19115796c8dcSSimon Schubert /* If SYM is within variable table of UNIT, set FILENAME_PTR and
19125796c8dcSSimon Schubert    LINENUMBER_PTR, and return TRUE.  */
19135796c8dcSSimon Schubert 
19145796c8dcSSimon Schubert static bfd_boolean
19155796c8dcSSimon Schubert lookup_symbol_in_variable_table (struct comp_unit *unit,
19165796c8dcSSimon Schubert 				 asymbol *sym,
19175796c8dcSSimon Schubert 				 bfd_vma addr,
19185796c8dcSSimon Schubert 				 const char **filename_ptr,
19195796c8dcSSimon Schubert 				 unsigned int *linenumber_ptr)
19205796c8dcSSimon Schubert {
19215796c8dcSSimon Schubert   const char *name = bfd_asymbol_name (sym);
19225796c8dcSSimon Schubert   asection *sec = bfd_get_section (sym);
19235796c8dcSSimon Schubert   struct varinfo* each;
19245796c8dcSSimon Schubert 
19255796c8dcSSimon Schubert   for (each = unit->variable_table; each; each = each->prev_var)
19265796c8dcSSimon Schubert     if (each->stack == 0
19275796c8dcSSimon Schubert 	&& each->file != NULL
19285796c8dcSSimon Schubert 	&& each->name != NULL
19295796c8dcSSimon Schubert 	&& each->addr == addr
19305796c8dcSSimon Schubert 	&& (!each->sec || each->sec == sec)
19315796c8dcSSimon Schubert 	&& strcmp (name, each->name) == 0)
19325796c8dcSSimon Schubert       break;
19335796c8dcSSimon Schubert 
19345796c8dcSSimon Schubert   if (each)
19355796c8dcSSimon Schubert     {
19365796c8dcSSimon Schubert       each->sec = sec;
19375796c8dcSSimon Schubert       *filename_ptr = each->file;
19385796c8dcSSimon Schubert       *linenumber_ptr = each->line;
19395796c8dcSSimon Schubert       return TRUE;
19405796c8dcSSimon Schubert     }
19415796c8dcSSimon Schubert   else
19425796c8dcSSimon Schubert     return FALSE;
19435796c8dcSSimon Schubert }
19445796c8dcSSimon Schubert 
19455796c8dcSSimon Schubert static char *
19465796c8dcSSimon Schubert find_abstract_instance_name (struct comp_unit *unit,
19475796c8dcSSimon Schubert 			     struct attribute *attr_ptr)
19485796c8dcSSimon Schubert {
19495796c8dcSSimon Schubert   bfd *abfd = unit->abfd;
19505796c8dcSSimon Schubert   bfd_byte *info_ptr;
19515796c8dcSSimon Schubert   unsigned int abbrev_number, bytes_read, i;
19525796c8dcSSimon Schubert   struct abbrev_info *abbrev;
19535796c8dcSSimon Schubert   bfd_uint64_t die_ref = attr_ptr->u.val;
19545796c8dcSSimon Schubert   struct attribute attr;
19555796c8dcSSimon Schubert   char *name = 0;
19565796c8dcSSimon Schubert 
19575796c8dcSSimon Schubert   /* DW_FORM_ref_addr can reference an entry in a different CU. It
19585796c8dcSSimon Schubert      is an offset from the .debug_info section, not the current CU.  */
19595796c8dcSSimon Schubert   if (attr_ptr->form == DW_FORM_ref_addr)
19605796c8dcSSimon Schubert     {
19615796c8dcSSimon Schubert       /* We only support DW_FORM_ref_addr within the same file, so
19625796c8dcSSimon Schubert 	 any relocations should be resolved already.  */
19635796c8dcSSimon Schubert       if (!die_ref)
19645796c8dcSSimon Schubert 	abort ();
19655796c8dcSSimon Schubert 
1966cf7f2e2dSJohn Marino       info_ptr = unit->sec_info_ptr + die_ref;
19675796c8dcSSimon Schubert     }
19685796c8dcSSimon Schubert   else
19695796c8dcSSimon Schubert     info_ptr = unit->info_ptr_unit + die_ref;
19705796c8dcSSimon Schubert   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19715796c8dcSSimon Schubert   info_ptr += bytes_read;
19725796c8dcSSimon Schubert 
19735796c8dcSSimon Schubert   if (abbrev_number)
19745796c8dcSSimon Schubert     {
19755796c8dcSSimon Schubert       abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
19765796c8dcSSimon Schubert       if (! abbrev)
19775796c8dcSSimon Schubert 	{
19785796c8dcSSimon Schubert 	  (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
19795796c8dcSSimon Schubert 				 abbrev_number);
19805796c8dcSSimon Schubert 	  bfd_set_error (bfd_error_bad_value);
19815796c8dcSSimon Schubert 	}
19825796c8dcSSimon Schubert       else
19835796c8dcSSimon Schubert 	{
19845796c8dcSSimon Schubert 	  for (i = 0; i < abbrev->num_attrs; ++i)
19855796c8dcSSimon Schubert 	    {
1986cf7f2e2dSJohn Marino 	      info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
1987cf7f2e2dSJohn Marino 					 info_ptr);
1988cf7f2e2dSJohn Marino 	      if (info_ptr == NULL)
1989cf7f2e2dSJohn Marino 		break;
19905796c8dcSSimon Schubert 	      switch (attr.name)
19915796c8dcSSimon Schubert 		{
19925796c8dcSSimon Schubert 		case DW_AT_name:
1993cf7f2e2dSJohn Marino 		  /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
1994cf7f2e2dSJohn Marino 		     over DW_AT_name.  */
19955796c8dcSSimon Schubert 		  if (name == NULL)
19965796c8dcSSimon Schubert 		    name = attr.u.str;
19975796c8dcSSimon Schubert 		  break;
19985796c8dcSSimon Schubert 		case DW_AT_specification:
19995796c8dcSSimon Schubert 		  name = find_abstract_instance_name (unit, &attr);
20005796c8dcSSimon Schubert 		  break;
2001cf7f2e2dSJohn Marino 		case DW_AT_linkage_name:
20025796c8dcSSimon Schubert 		case DW_AT_MIPS_linkage_name:
20035796c8dcSSimon Schubert 		  name = attr.u.str;
20045796c8dcSSimon Schubert 		  break;
20055796c8dcSSimon Schubert 		default:
20065796c8dcSSimon Schubert 		  break;
20075796c8dcSSimon Schubert 		}
20085796c8dcSSimon Schubert 	    }
20095796c8dcSSimon Schubert 	}
20105796c8dcSSimon Schubert     }
2011cf7f2e2dSJohn Marino   return name;
20125796c8dcSSimon Schubert }
20135796c8dcSSimon Schubert 
2014cf7f2e2dSJohn Marino static bfd_boolean
2015cf7f2e2dSJohn Marino read_rangelist (struct comp_unit *unit, struct arange *arange,
2016cf7f2e2dSJohn Marino 		bfd_uint64_t offset)
20175796c8dcSSimon Schubert {
20185796c8dcSSimon Schubert   bfd_byte *ranges_ptr;
20195796c8dcSSimon Schubert   bfd_vma base_address = unit->base_address;
20205796c8dcSSimon Schubert 
20215796c8dcSSimon Schubert   if (! unit->stash->dwarf_ranges_buffer)
20225796c8dcSSimon Schubert     {
20235796c8dcSSimon Schubert       if (! read_debug_ranges (unit))
2024cf7f2e2dSJohn Marino 	return FALSE;
20255796c8dcSSimon Schubert     }
20265796c8dcSSimon Schubert   ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
20275796c8dcSSimon Schubert 
20285796c8dcSSimon Schubert   for (;;)
20295796c8dcSSimon Schubert     {
20305796c8dcSSimon Schubert       bfd_vma low_pc;
20315796c8dcSSimon Schubert       bfd_vma high_pc;
20325796c8dcSSimon Schubert 
20335796c8dcSSimon Schubert       low_pc = read_address (unit, ranges_ptr);
20345796c8dcSSimon Schubert       ranges_ptr += unit->addr_size;
20355796c8dcSSimon Schubert       high_pc = read_address (unit, ranges_ptr);
20365796c8dcSSimon Schubert       ranges_ptr += unit->addr_size;
20375796c8dcSSimon Schubert 
20385796c8dcSSimon Schubert       if (low_pc == 0 && high_pc == 0)
20395796c8dcSSimon Schubert 	break;
20405796c8dcSSimon Schubert       if (low_pc == -1UL && high_pc != -1UL)
20415796c8dcSSimon Schubert 	base_address = high_pc;
20425796c8dcSSimon Schubert       else
2043cf7f2e2dSJohn Marino 	{
2044cf7f2e2dSJohn Marino 	  if (!arange_add (unit->abfd, arange,
2045cf7f2e2dSJohn Marino 			   base_address + low_pc, base_address + high_pc))
2046cf7f2e2dSJohn Marino 	    return FALSE;
20475796c8dcSSimon Schubert 	}
20485796c8dcSSimon Schubert     }
2049cf7f2e2dSJohn Marino   return TRUE;
2050cf7f2e2dSJohn Marino }
20515796c8dcSSimon Schubert 
20525796c8dcSSimon Schubert /* DWARF2 Compilation unit functions.  */
20535796c8dcSSimon Schubert 
20545796c8dcSSimon Schubert /* Scan over each die in a comp. unit looking for functions to add
20555796c8dcSSimon Schubert    to the function table and variables to the variable table.  */
20565796c8dcSSimon Schubert 
20575796c8dcSSimon Schubert static bfd_boolean
20585796c8dcSSimon Schubert scan_unit_for_symbols (struct comp_unit *unit)
20595796c8dcSSimon Schubert {
20605796c8dcSSimon Schubert   bfd *abfd = unit->abfd;
20615796c8dcSSimon Schubert   bfd_byte *info_ptr = unit->first_child_die_ptr;
20625796c8dcSSimon Schubert   int nesting_level = 1;
20635796c8dcSSimon Schubert   struct funcinfo **nested_funcs;
20645796c8dcSSimon Schubert   int nested_funcs_size;
20655796c8dcSSimon Schubert 
20665796c8dcSSimon Schubert   /* Maintain a stack of in-scope functions and inlined functions, which we
20675796c8dcSSimon Schubert      can use to set the caller_func field.  */
20685796c8dcSSimon Schubert   nested_funcs_size = 32;
20695796c8dcSSimon Schubert   nested_funcs = (struct funcinfo **)
20705796c8dcSSimon Schubert       bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
20715796c8dcSSimon Schubert   if (nested_funcs == NULL)
20725796c8dcSSimon Schubert     return FALSE;
20735796c8dcSSimon Schubert   nested_funcs[nesting_level] = 0;
20745796c8dcSSimon Schubert 
20755796c8dcSSimon Schubert   while (nesting_level)
20765796c8dcSSimon Schubert     {
20775796c8dcSSimon Schubert       unsigned int abbrev_number, bytes_read, i;
20785796c8dcSSimon Schubert       struct abbrev_info *abbrev;
20795796c8dcSSimon Schubert       struct attribute attr;
20805796c8dcSSimon Schubert       struct funcinfo *func;
20815796c8dcSSimon Schubert       struct varinfo *var;
20825796c8dcSSimon Schubert       bfd_vma low_pc = 0;
20835796c8dcSSimon Schubert       bfd_vma high_pc = 0;
20845796c8dcSSimon Schubert 
20855796c8dcSSimon Schubert       abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20865796c8dcSSimon Schubert       info_ptr += bytes_read;
20875796c8dcSSimon Schubert 
20885796c8dcSSimon Schubert       if (! abbrev_number)
20895796c8dcSSimon Schubert 	{
20905796c8dcSSimon Schubert 	  nesting_level--;
20915796c8dcSSimon Schubert 	  continue;
20925796c8dcSSimon Schubert 	}
20935796c8dcSSimon Schubert 
20945796c8dcSSimon Schubert       abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
20955796c8dcSSimon Schubert       if (! abbrev)
20965796c8dcSSimon Schubert 	{
2097cf7f2e2dSJohn Marino 	  (*_bfd_error_handler)
2098cf7f2e2dSJohn Marino 	    (_("Dwarf Error: Could not find abbrev number %u."),
20995796c8dcSSimon Schubert 	     abbrev_number);
21005796c8dcSSimon Schubert 	  bfd_set_error (bfd_error_bad_value);
2101cf7f2e2dSJohn Marino 	  goto fail;
21025796c8dcSSimon Schubert 	}
21035796c8dcSSimon Schubert 
21045796c8dcSSimon Schubert       var = NULL;
21055796c8dcSSimon Schubert       if (abbrev->tag == DW_TAG_subprogram
21065796c8dcSSimon Schubert 	  || abbrev->tag == DW_TAG_entry_point
21075796c8dcSSimon Schubert 	  || abbrev->tag == DW_TAG_inlined_subroutine)
21085796c8dcSSimon Schubert 	{
21095796c8dcSSimon Schubert 	  bfd_size_type amt = sizeof (struct funcinfo);
21105796c8dcSSimon Schubert 	  func = (struct funcinfo *) bfd_zalloc (abfd, amt);
2111cf7f2e2dSJohn Marino 	  if (func == NULL)
2112cf7f2e2dSJohn Marino 	    goto fail;
21135796c8dcSSimon Schubert 	  func->tag = abbrev->tag;
21145796c8dcSSimon Schubert 	  func->prev_func = unit->function_table;
21155796c8dcSSimon Schubert 	  unit->function_table = func;
21165796c8dcSSimon Schubert 	  BFD_ASSERT (!unit->cached);
21175796c8dcSSimon Schubert 
21185796c8dcSSimon Schubert 	  if (func->tag == DW_TAG_inlined_subroutine)
21195796c8dcSSimon Schubert 	    for (i = nesting_level - 1; i >= 1; i--)
21205796c8dcSSimon Schubert 	      if (nested_funcs[i])
21215796c8dcSSimon Schubert 		{
21225796c8dcSSimon Schubert 		  func->caller_func = nested_funcs[i];
21235796c8dcSSimon Schubert 		  break;
21245796c8dcSSimon Schubert 		}
21255796c8dcSSimon Schubert 	  nested_funcs[nesting_level] = func;
21265796c8dcSSimon Schubert 	}
21275796c8dcSSimon Schubert       else
21285796c8dcSSimon Schubert 	{
21295796c8dcSSimon Schubert 	  func = NULL;
21305796c8dcSSimon Schubert 	  if (abbrev->tag == DW_TAG_variable)
21315796c8dcSSimon Schubert 	    {
21325796c8dcSSimon Schubert 	      bfd_size_type amt = sizeof (struct varinfo);
21335796c8dcSSimon Schubert 	      var = (struct varinfo *) bfd_zalloc (abfd, amt);
2134cf7f2e2dSJohn Marino 	      if (var == NULL)
2135cf7f2e2dSJohn Marino 		goto fail;
21365796c8dcSSimon Schubert 	      var->tag = abbrev->tag;
21375796c8dcSSimon Schubert 	      var->stack = 1;
21385796c8dcSSimon Schubert 	      var->prev_var = unit->variable_table;
21395796c8dcSSimon Schubert 	      unit->variable_table = var;
21405796c8dcSSimon Schubert 	      BFD_ASSERT (!unit->cached);
21415796c8dcSSimon Schubert 	    }
21425796c8dcSSimon Schubert 
21435796c8dcSSimon Schubert 	  /* No inline function in scope at this nesting level.  */
21445796c8dcSSimon Schubert 	  nested_funcs[nesting_level] = 0;
21455796c8dcSSimon Schubert 	}
21465796c8dcSSimon Schubert 
21475796c8dcSSimon Schubert       for (i = 0; i < abbrev->num_attrs; ++i)
21485796c8dcSSimon Schubert 	{
21495796c8dcSSimon Schubert 	  info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2150cf7f2e2dSJohn Marino 	  if (info_ptr == NULL)
2151*c50c785cSJohn Marino 	    goto fail;
21525796c8dcSSimon Schubert 
21535796c8dcSSimon Schubert 	  if (func)
21545796c8dcSSimon Schubert 	    {
21555796c8dcSSimon Schubert 	      switch (attr.name)
21565796c8dcSSimon Schubert 		{
21575796c8dcSSimon Schubert 		case DW_AT_call_file:
2158cf7f2e2dSJohn Marino 		  func->caller_file = concat_filename (unit->line_table,
2159cf7f2e2dSJohn Marino 						       attr.u.val);
21605796c8dcSSimon Schubert 		  break;
21615796c8dcSSimon Schubert 
21625796c8dcSSimon Schubert 		case DW_AT_call_line:
21635796c8dcSSimon Schubert 		  func->caller_line = attr.u.val;
21645796c8dcSSimon Schubert 		  break;
21655796c8dcSSimon Schubert 
21665796c8dcSSimon Schubert 		case DW_AT_abstract_origin:
21675796c8dcSSimon Schubert 		  func->name = find_abstract_instance_name (unit, &attr);
21685796c8dcSSimon Schubert 		  break;
21695796c8dcSSimon Schubert 
21705796c8dcSSimon Schubert 		case DW_AT_name:
2171cf7f2e2dSJohn Marino 		  /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2172cf7f2e2dSJohn Marino 		     over DW_AT_name.  */
21735796c8dcSSimon Schubert 		  if (func->name == NULL)
21745796c8dcSSimon Schubert 		    func->name = attr.u.str;
21755796c8dcSSimon Schubert 		  break;
21765796c8dcSSimon Schubert 
2177cf7f2e2dSJohn Marino 		case DW_AT_linkage_name:
21785796c8dcSSimon Schubert 		case DW_AT_MIPS_linkage_name:
21795796c8dcSSimon Schubert 		  func->name = attr.u.str;
21805796c8dcSSimon Schubert 		  break;
21815796c8dcSSimon Schubert 
21825796c8dcSSimon Schubert 		case DW_AT_low_pc:
21835796c8dcSSimon Schubert 		  low_pc = attr.u.val;
21845796c8dcSSimon Schubert 		  break;
21855796c8dcSSimon Schubert 
21865796c8dcSSimon Schubert 		case DW_AT_high_pc:
21875796c8dcSSimon Schubert 		  high_pc = attr.u.val;
21885796c8dcSSimon Schubert 		  break;
21895796c8dcSSimon Schubert 
21905796c8dcSSimon Schubert 		case DW_AT_ranges:
2191cf7f2e2dSJohn Marino 		  if (!read_rangelist (unit, &func->arange, attr.u.val))
2192cf7f2e2dSJohn Marino 		    goto fail;
21935796c8dcSSimon Schubert 		  break;
21945796c8dcSSimon Schubert 
21955796c8dcSSimon Schubert 		case DW_AT_decl_file:
21965796c8dcSSimon Schubert 		  func->file = concat_filename (unit->line_table,
21975796c8dcSSimon Schubert 						attr.u.val);
21985796c8dcSSimon Schubert 		  break;
21995796c8dcSSimon Schubert 
22005796c8dcSSimon Schubert 		case DW_AT_decl_line:
22015796c8dcSSimon Schubert 		  func->line = attr.u.val;
22025796c8dcSSimon Schubert 		  break;
22035796c8dcSSimon Schubert 
22045796c8dcSSimon Schubert 		default:
22055796c8dcSSimon Schubert 		  break;
22065796c8dcSSimon Schubert 		}
22075796c8dcSSimon Schubert 	    }
22085796c8dcSSimon Schubert 	  else if (var)
22095796c8dcSSimon Schubert 	    {
22105796c8dcSSimon Schubert 	      switch (attr.name)
22115796c8dcSSimon Schubert 		{
22125796c8dcSSimon Schubert 		case DW_AT_name:
22135796c8dcSSimon Schubert 		  var->name = attr.u.str;
22145796c8dcSSimon Schubert 		  break;
22155796c8dcSSimon Schubert 
22165796c8dcSSimon Schubert 		case DW_AT_decl_file:
22175796c8dcSSimon Schubert 		  var->file = concat_filename (unit->line_table,
22185796c8dcSSimon Schubert 					       attr.u.val);
22195796c8dcSSimon Schubert 		  break;
22205796c8dcSSimon Schubert 
22215796c8dcSSimon Schubert 		case DW_AT_decl_line:
22225796c8dcSSimon Schubert 		  var->line = attr.u.val;
22235796c8dcSSimon Schubert 		  break;
22245796c8dcSSimon Schubert 
22255796c8dcSSimon Schubert 		case DW_AT_external:
22265796c8dcSSimon Schubert 		  if (attr.u.val != 0)
22275796c8dcSSimon Schubert 		    var->stack = 0;
22285796c8dcSSimon Schubert 		  break;
22295796c8dcSSimon Schubert 
22305796c8dcSSimon Schubert 		case DW_AT_location:
22315796c8dcSSimon Schubert 		  switch (attr.form)
22325796c8dcSSimon Schubert 		    {
22335796c8dcSSimon Schubert 		    case DW_FORM_block:
22345796c8dcSSimon Schubert 		    case DW_FORM_block1:
22355796c8dcSSimon Schubert 		    case DW_FORM_block2:
22365796c8dcSSimon Schubert 		    case DW_FORM_block4:
2237cf7f2e2dSJohn Marino 		    case DW_FORM_exprloc:
22385796c8dcSSimon Schubert 		      if (*attr.u.blk->data == DW_OP_addr)
22395796c8dcSSimon Schubert 			{
22405796c8dcSSimon Schubert 			  var->stack = 0;
22415796c8dcSSimon Schubert 
22425796c8dcSSimon Schubert 			  /* Verify that DW_OP_addr is the only opcode in the
22435796c8dcSSimon Schubert 			     location, in which case the block size will be 1
22445796c8dcSSimon Schubert 			     plus the address size.  */
22455796c8dcSSimon Schubert 			  /* ??? For TLS variables, gcc can emit
22465796c8dcSSimon Schubert 			     DW_OP_addr <addr> DW_OP_GNU_push_tls_address
22475796c8dcSSimon Schubert 			     which we don't handle here yet.  */
22485796c8dcSSimon Schubert 			  if (attr.u.blk->size == unit->addr_size + 1U)
22495796c8dcSSimon Schubert 			    var->addr = bfd_get (unit->addr_size * 8,
22505796c8dcSSimon Schubert 						 unit->abfd,
22515796c8dcSSimon Schubert 						 attr.u.blk->data + 1);
22525796c8dcSSimon Schubert 			}
22535796c8dcSSimon Schubert 		      break;
22545796c8dcSSimon Schubert 
22555796c8dcSSimon Schubert 		    default:
22565796c8dcSSimon Schubert 		      break;
22575796c8dcSSimon Schubert 		    }
22585796c8dcSSimon Schubert 		  break;
22595796c8dcSSimon Schubert 
22605796c8dcSSimon Schubert 		default:
22615796c8dcSSimon Schubert 		  break;
22625796c8dcSSimon Schubert 		}
22635796c8dcSSimon Schubert 	    }
22645796c8dcSSimon Schubert 	}
22655796c8dcSSimon Schubert 
22665796c8dcSSimon Schubert       if (func && high_pc != 0)
22675796c8dcSSimon Schubert 	{
2268cf7f2e2dSJohn Marino 	  if (!arange_add (unit->abfd, &func->arange, low_pc, high_pc))
2269cf7f2e2dSJohn Marino 	    goto fail;
22705796c8dcSSimon Schubert 	}
22715796c8dcSSimon Schubert 
22725796c8dcSSimon Schubert       if (abbrev->has_children)
22735796c8dcSSimon Schubert 	{
22745796c8dcSSimon Schubert 	  nesting_level++;
22755796c8dcSSimon Schubert 
22765796c8dcSSimon Schubert 	  if (nesting_level >= nested_funcs_size)
22775796c8dcSSimon Schubert 	    {
22785796c8dcSSimon Schubert 	      struct funcinfo **tmp;
22795796c8dcSSimon Schubert 
22805796c8dcSSimon Schubert 	      nested_funcs_size *= 2;
22815796c8dcSSimon Schubert 	      tmp = (struct funcinfo **)
22825796c8dcSSimon Schubert                  bfd_realloc (nested_funcs,
22835796c8dcSSimon Schubert                               (nested_funcs_size * sizeof (struct funcinfo *)));
22845796c8dcSSimon Schubert 	      if (tmp == NULL)
2285cf7f2e2dSJohn Marino 		goto fail;
22865796c8dcSSimon Schubert 	      nested_funcs = tmp;
22875796c8dcSSimon Schubert 	    }
22885796c8dcSSimon Schubert 	  nested_funcs[nesting_level] = 0;
22895796c8dcSSimon Schubert 	}
22905796c8dcSSimon Schubert     }
22915796c8dcSSimon Schubert 
22925796c8dcSSimon Schubert   free (nested_funcs);
22935796c8dcSSimon Schubert   return TRUE;
2294cf7f2e2dSJohn Marino 
2295cf7f2e2dSJohn Marino  fail:
2296cf7f2e2dSJohn Marino   free (nested_funcs);
2297cf7f2e2dSJohn Marino   return FALSE;
22985796c8dcSSimon Schubert }
22995796c8dcSSimon Schubert 
23005796c8dcSSimon Schubert /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
23015796c8dcSSimon Schubert    includes the compilation unit header that proceeds the DIE's, but
23025796c8dcSSimon Schubert    does not include the length field that precedes each compilation
23035796c8dcSSimon Schubert    unit header.  END_PTR points one past the end of this comp unit.
23045796c8dcSSimon Schubert    OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
23055796c8dcSSimon Schubert 
23065796c8dcSSimon Schubert    This routine does not read the whole compilation unit; only enough
23075796c8dcSSimon Schubert    to get to the line number information for the compilation unit.  */
23085796c8dcSSimon Schubert 
23095796c8dcSSimon Schubert static struct comp_unit *
23105796c8dcSSimon Schubert parse_comp_unit (struct dwarf2_debug *stash,
23115796c8dcSSimon Schubert 		 bfd_vma unit_length,
23125796c8dcSSimon Schubert 		 bfd_byte *info_ptr_unit,
23135796c8dcSSimon Schubert 		 unsigned int offset_size)
23145796c8dcSSimon Schubert {
23155796c8dcSSimon Schubert   struct comp_unit* unit;
23165796c8dcSSimon Schubert   unsigned int version;
23175796c8dcSSimon Schubert   bfd_uint64_t abbrev_offset = 0;
23185796c8dcSSimon Schubert   unsigned int addr_size;
23195796c8dcSSimon Schubert   struct abbrev_info** abbrevs;
23205796c8dcSSimon Schubert   unsigned int abbrev_number, bytes_read, i;
23215796c8dcSSimon Schubert   struct abbrev_info *abbrev;
23225796c8dcSSimon Schubert   struct attribute attr;
23235796c8dcSSimon Schubert   bfd_byte *info_ptr = stash->info_ptr;
23245796c8dcSSimon Schubert   bfd_byte *end_ptr = info_ptr + unit_length;
23255796c8dcSSimon Schubert   bfd_size_type amt;
23265796c8dcSSimon Schubert   bfd_vma low_pc = 0;
23275796c8dcSSimon Schubert   bfd_vma high_pc = 0;
23285796c8dcSSimon Schubert   bfd *abfd = stash->bfd_ptr;
23295796c8dcSSimon Schubert 
23305796c8dcSSimon Schubert   version = read_2_bytes (abfd, info_ptr);
23315796c8dcSSimon Schubert   info_ptr += 2;
23325796c8dcSSimon Schubert   BFD_ASSERT (offset_size == 4 || offset_size == 8);
23335796c8dcSSimon Schubert   if (offset_size == 4)
23345796c8dcSSimon Schubert     abbrev_offset = read_4_bytes (abfd, info_ptr);
23355796c8dcSSimon Schubert   else
23365796c8dcSSimon Schubert     abbrev_offset = read_8_bytes (abfd, info_ptr);
23375796c8dcSSimon Schubert   info_ptr += offset_size;
23385796c8dcSSimon Schubert   addr_size = read_1_byte (abfd, info_ptr);
23395796c8dcSSimon Schubert   info_ptr += 1;
23405796c8dcSSimon Schubert 
2341cf7f2e2dSJohn Marino   if (version != 2 && version != 3 && version != 4)
23425796c8dcSSimon Schubert     {
2343cf7f2e2dSJohn Marino       (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2, 3 and 4 information."), version);
23445796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
23455796c8dcSSimon Schubert       return 0;
23465796c8dcSSimon Schubert     }
23475796c8dcSSimon Schubert 
23485796c8dcSSimon Schubert   if (addr_size > sizeof (bfd_vma))
23495796c8dcSSimon Schubert     {
23505796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
23515796c8dcSSimon Schubert 			 addr_size,
23525796c8dcSSimon Schubert 			 (unsigned int) sizeof (bfd_vma));
23535796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
23545796c8dcSSimon Schubert       return 0;
23555796c8dcSSimon Schubert     }
23565796c8dcSSimon Schubert 
23575796c8dcSSimon Schubert   if (addr_size != 2 && addr_size != 4 && addr_size != 8)
23585796c8dcSSimon Schubert     {
23595796c8dcSSimon Schubert       (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
23605796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
23615796c8dcSSimon Schubert       return 0;
23625796c8dcSSimon Schubert     }
23635796c8dcSSimon Schubert 
23645796c8dcSSimon Schubert   /* Read the abbrevs for this compilation unit into a table.  */
23655796c8dcSSimon Schubert   abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
23665796c8dcSSimon Schubert   if (! abbrevs)
23675796c8dcSSimon Schubert       return 0;
23685796c8dcSSimon Schubert 
23695796c8dcSSimon Schubert   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
23705796c8dcSSimon Schubert   info_ptr += bytes_read;
23715796c8dcSSimon Schubert   if (! abbrev_number)
23725796c8dcSSimon Schubert     {
23735796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
23745796c8dcSSimon Schubert 			 abbrev_number);
23755796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
23765796c8dcSSimon Schubert       return 0;
23775796c8dcSSimon Schubert     }
23785796c8dcSSimon Schubert 
23795796c8dcSSimon Schubert   abbrev = lookup_abbrev (abbrev_number, abbrevs);
23805796c8dcSSimon Schubert   if (! abbrev)
23815796c8dcSSimon Schubert     {
23825796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
23835796c8dcSSimon Schubert 			 abbrev_number);
23845796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
23855796c8dcSSimon Schubert       return 0;
23865796c8dcSSimon Schubert     }
23875796c8dcSSimon Schubert 
23885796c8dcSSimon Schubert   amt = sizeof (struct comp_unit);
23895796c8dcSSimon Schubert   unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
2390cf7f2e2dSJohn Marino   if (unit == NULL)
2391cf7f2e2dSJohn Marino     return NULL;
23925796c8dcSSimon Schubert   unit->abfd = abfd;
23935796c8dcSSimon Schubert   unit->version = version;
23945796c8dcSSimon Schubert   unit->addr_size = addr_size;
23955796c8dcSSimon Schubert   unit->offset_size = offset_size;
23965796c8dcSSimon Schubert   unit->abbrevs = abbrevs;
23975796c8dcSSimon Schubert   unit->end_ptr = end_ptr;
23985796c8dcSSimon Schubert   unit->stash = stash;
23995796c8dcSSimon Schubert   unit->info_ptr_unit = info_ptr_unit;
2400cf7f2e2dSJohn Marino   unit->sec_info_ptr = stash->sec_info_ptr;
24015796c8dcSSimon Schubert 
24025796c8dcSSimon Schubert   for (i = 0; i < abbrev->num_attrs; ++i)
24035796c8dcSSimon Schubert     {
24045796c8dcSSimon Schubert       info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2405cf7f2e2dSJohn Marino       if (info_ptr == NULL)
2406cf7f2e2dSJohn Marino 	return NULL;
24075796c8dcSSimon Schubert 
24085796c8dcSSimon Schubert       /* Store the data if it is of an attribute we want to keep in a
24095796c8dcSSimon Schubert 	 partial symbol table.  */
24105796c8dcSSimon Schubert       switch (attr.name)
24115796c8dcSSimon Schubert 	{
24125796c8dcSSimon Schubert 	case DW_AT_stmt_list:
24135796c8dcSSimon Schubert 	  unit->stmtlist = 1;
24145796c8dcSSimon Schubert 	  unit->line_offset = attr.u.val;
24155796c8dcSSimon Schubert 	  break;
24165796c8dcSSimon Schubert 
24175796c8dcSSimon Schubert 	case DW_AT_name:
24185796c8dcSSimon Schubert 	  unit->name = attr.u.str;
24195796c8dcSSimon Schubert 	  break;
24205796c8dcSSimon Schubert 
24215796c8dcSSimon Schubert 	case DW_AT_low_pc:
24225796c8dcSSimon Schubert 	  low_pc = attr.u.val;
24235796c8dcSSimon Schubert 	  /* If the compilation unit DIE has a DW_AT_low_pc attribute,
24245796c8dcSSimon Schubert 	     this is the base address to use when reading location
24255796c8dcSSimon Schubert 	     lists or range lists. */
24265796c8dcSSimon Schubert 	  unit->base_address = low_pc;
24275796c8dcSSimon Schubert 	  break;
24285796c8dcSSimon Schubert 
24295796c8dcSSimon Schubert 	case DW_AT_high_pc:
24305796c8dcSSimon Schubert 	  high_pc = attr.u.val;
24315796c8dcSSimon Schubert 	  break;
24325796c8dcSSimon Schubert 
24335796c8dcSSimon Schubert 	case DW_AT_ranges:
2434cf7f2e2dSJohn Marino 	  if (!read_rangelist (unit, &unit->arange, attr.u.val))
2435cf7f2e2dSJohn Marino 	    return NULL;
24365796c8dcSSimon Schubert 	  break;
24375796c8dcSSimon Schubert 
24385796c8dcSSimon Schubert 	case DW_AT_comp_dir:
24395796c8dcSSimon Schubert 	  {
24405796c8dcSSimon Schubert 	    char *comp_dir = attr.u.str;
24415796c8dcSSimon Schubert 	    if (comp_dir)
24425796c8dcSSimon Schubert 	      {
24435796c8dcSSimon Schubert 		/* Irix 6.2 native cc prepends <machine>.: to the compilation
24445796c8dcSSimon Schubert 		   directory, get rid of it.  */
24455796c8dcSSimon Schubert 		char *cp = strchr (comp_dir, ':');
24465796c8dcSSimon Schubert 
24475796c8dcSSimon Schubert 		if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
24485796c8dcSSimon Schubert 		  comp_dir = cp + 1;
24495796c8dcSSimon Schubert 	      }
24505796c8dcSSimon Schubert 	    unit->comp_dir = comp_dir;
24515796c8dcSSimon Schubert 	    break;
24525796c8dcSSimon Schubert 	  }
24535796c8dcSSimon Schubert 
24545796c8dcSSimon Schubert 	default:
24555796c8dcSSimon Schubert 	  break;
24565796c8dcSSimon Schubert 	}
24575796c8dcSSimon Schubert     }
24585796c8dcSSimon Schubert   if (high_pc != 0)
24595796c8dcSSimon Schubert     {
2460cf7f2e2dSJohn Marino       if (!arange_add (unit->abfd, &unit->arange, low_pc, high_pc))
2461cf7f2e2dSJohn Marino 	return NULL;
24625796c8dcSSimon Schubert     }
24635796c8dcSSimon Schubert 
24645796c8dcSSimon Schubert   unit->first_child_die_ptr = info_ptr;
24655796c8dcSSimon Schubert   return unit;
24665796c8dcSSimon Schubert }
24675796c8dcSSimon Schubert 
24685796c8dcSSimon Schubert /* Return TRUE if UNIT may contain the address given by ADDR.  When
24695796c8dcSSimon Schubert    there are functions written entirely with inline asm statements, the
24705796c8dcSSimon Schubert    range info in the compilation unit header may not be correct.  We
24715796c8dcSSimon Schubert    need to consult the line info table to see if a compilation unit
24725796c8dcSSimon Schubert    really contains the given address.  */
24735796c8dcSSimon Schubert 
24745796c8dcSSimon Schubert static bfd_boolean
24755796c8dcSSimon Schubert comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
24765796c8dcSSimon Schubert {
24775796c8dcSSimon Schubert   struct arange *arange;
24785796c8dcSSimon Schubert 
24795796c8dcSSimon Schubert   if (unit->error)
24805796c8dcSSimon Schubert     return FALSE;
24815796c8dcSSimon Schubert 
24825796c8dcSSimon Schubert   arange = &unit->arange;
24835796c8dcSSimon Schubert   do
24845796c8dcSSimon Schubert     {
24855796c8dcSSimon Schubert       if (addr >= arange->low && addr < arange->high)
24865796c8dcSSimon Schubert 	return TRUE;
24875796c8dcSSimon Schubert       arange = arange->next;
24885796c8dcSSimon Schubert     }
24895796c8dcSSimon Schubert   while (arange);
24905796c8dcSSimon Schubert 
24915796c8dcSSimon Schubert   return FALSE;
24925796c8dcSSimon Schubert }
24935796c8dcSSimon Schubert 
24945796c8dcSSimon Schubert /* If UNIT contains ADDR, set the output parameters to the values for
24955796c8dcSSimon Schubert    the line containing ADDR.  The output parameters, FILENAME_PTR,
24965796c8dcSSimon Schubert    FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
24975796c8dcSSimon Schubert    to be filled in.
24985796c8dcSSimon Schubert 
24995796c8dcSSimon Schubert    Return TRUE if UNIT contains ADDR, and no errors were encountered;
25005796c8dcSSimon Schubert    FALSE otherwise.  */
25015796c8dcSSimon Schubert 
25025796c8dcSSimon Schubert static bfd_boolean
25035796c8dcSSimon Schubert comp_unit_find_nearest_line (struct comp_unit *unit,
25045796c8dcSSimon Schubert 			     bfd_vma addr,
25055796c8dcSSimon Schubert 			     const char **filename_ptr,
25065796c8dcSSimon Schubert 			     const char **functionname_ptr,
25075796c8dcSSimon Schubert 			     unsigned int *linenumber_ptr,
25085796c8dcSSimon Schubert 			     struct dwarf2_debug *stash)
25095796c8dcSSimon Schubert {
25105796c8dcSSimon Schubert   bfd_boolean line_p;
25115796c8dcSSimon Schubert   bfd_boolean func_p;
25125796c8dcSSimon Schubert   struct funcinfo *function;
25135796c8dcSSimon Schubert 
25145796c8dcSSimon Schubert   if (unit->error)
25155796c8dcSSimon Schubert     return FALSE;
25165796c8dcSSimon Schubert 
25175796c8dcSSimon Schubert   if (! unit->line_table)
25185796c8dcSSimon Schubert     {
25195796c8dcSSimon Schubert       if (! unit->stmtlist)
25205796c8dcSSimon Schubert 	{
25215796c8dcSSimon Schubert 	  unit->error = 1;
25225796c8dcSSimon Schubert 	  return FALSE;
25235796c8dcSSimon Schubert 	}
25245796c8dcSSimon Schubert 
25255796c8dcSSimon Schubert       unit->line_table = decode_line_info (unit, stash);
25265796c8dcSSimon Schubert 
25275796c8dcSSimon Schubert       if (! unit->line_table)
25285796c8dcSSimon Schubert 	{
25295796c8dcSSimon Schubert 	  unit->error = 1;
25305796c8dcSSimon Schubert 	  return FALSE;
25315796c8dcSSimon Schubert 	}
25325796c8dcSSimon Schubert 
25335796c8dcSSimon Schubert       if (unit->first_child_die_ptr < unit->end_ptr
25345796c8dcSSimon Schubert 	  && ! scan_unit_for_symbols (unit))
25355796c8dcSSimon Schubert 	{
25365796c8dcSSimon Schubert 	  unit->error = 1;
25375796c8dcSSimon Schubert 	  return FALSE;
25385796c8dcSSimon Schubert 	}
25395796c8dcSSimon Schubert     }
25405796c8dcSSimon Schubert 
25415796c8dcSSimon Schubert   function = NULL;
25425796c8dcSSimon Schubert   func_p = lookup_address_in_function_table (unit, addr,
25435796c8dcSSimon Schubert 					     &function, functionname_ptr);
25445796c8dcSSimon Schubert   if (func_p && (function->tag == DW_TAG_inlined_subroutine))
25455796c8dcSSimon Schubert     stash->inliner_chain = function;
25465796c8dcSSimon Schubert   line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2547cf7f2e2dSJohn Marino 					      filename_ptr,
25485796c8dcSSimon Schubert 					      linenumber_ptr);
25495796c8dcSSimon Schubert   return line_p || func_p;
25505796c8dcSSimon Schubert }
25515796c8dcSSimon Schubert 
25525796c8dcSSimon Schubert /* Check to see if line info is already decoded in a comp_unit.
25535796c8dcSSimon Schubert    If not, decode it.  Returns TRUE if no errors were encountered;
25545796c8dcSSimon Schubert    FALSE otherwise.  */
25555796c8dcSSimon Schubert 
25565796c8dcSSimon Schubert static bfd_boolean
25575796c8dcSSimon Schubert comp_unit_maybe_decode_line_info (struct comp_unit *unit,
25585796c8dcSSimon Schubert 				  struct dwarf2_debug *stash)
25595796c8dcSSimon Schubert {
25605796c8dcSSimon Schubert   if (unit->error)
25615796c8dcSSimon Schubert     return FALSE;
25625796c8dcSSimon Schubert 
25635796c8dcSSimon Schubert   if (! unit->line_table)
25645796c8dcSSimon Schubert     {
25655796c8dcSSimon Schubert       if (! unit->stmtlist)
25665796c8dcSSimon Schubert 	{
25675796c8dcSSimon Schubert 	  unit->error = 1;
25685796c8dcSSimon Schubert 	  return FALSE;
25695796c8dcSSimon Schubert 	}
25705796c8dcSSimon Schubert 
25715796c8dcSSimon Schubert       unit->line_table = decode_line_info (unit, stash);
25725796c8dcSSimon Schubert 
25735796c8dcSSimon Schubert       if (! unit->line_table)
25745796c8dcSSimon Schubert 	{
25755796c8dcSSimon Schubert 	  unit->error = 1;
25765796c8dcSSimon Schubert 	  return FALSE;
25775796c8dcSSimon Schubert 	}
25785796c8dcSSimon Schubert 
25795796c8dcSSimon Schubert       if (unit->first_child_die_ptr < unit->end_ptr
25805796c8dcSSimon Schubert 	  && ! scan_unit_for_symbols (unit))
25815796c8dcSSimon Schubert 	{
25825796c8dcSSimon Schubert 	  unit->error = 1;
25835796c8dcSSimon Schubert 	  return FALSE;
25845796c8dcSSimon Schubert 	}
25855796c8dcSSimon Schubert     }
25865796c8dcSSimon Schubert 
25875796c8dcSSimon Schubert   return TRUE;
25885796c8dcSSimon Schubert }
25895796c8dcSSimon Schubert 
25905796c8dcSSimon Schubert /* If UNIT contains SYM at ADDR, set the output parameters to the
25915796c8dcSSimon Schubert    values for the line containing SYM.  The output parameters,
25925796c8dcSSimon Schubert    FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
25935796c8dcSSimon Schubert    filled in.
25945796c8dcSSimon Schubert 
25955796c8dcSSimon Schubert    Return TRUE if UNIT contains SYM, and no errors were encountered;
25965796c8dcSSimon Schubert    FALSE otherwise.  */
25975796c8dcSSimon Schubert 
25985796c8dcSSimon Schubert static bfd_boolean
25995796c8dcSSimon Schubert comp_unit_find_line (struct comp_unit *unit,
26005796c8dcSSimon Schubert 		     asymbol *sym,
26015796c8dcSSimon Schubert 		     bfd_vma addr,
26025796c8dcSSimon Schubert 		     const char **filename_ptr,
26035796c8dcSSimon Schubert 		     unsigned int *linenumber_ptr,
26045796c8dcSSimon Schubert 		     struct dwarf2_debug *stash)
26055796c8dcSSimon Schubert {
26065796c8dcSSimon Schubert   if (!comp_unit_maybe_decode_line_info (unit, stash))
26075796c8dcSSimon Schubert     return FALSE;
26085796c8dcSSimon Schubert 
26095796c8dcSSimon Schubert   if (sym->flags & BSF_FUNCTION)
26105796c8dcSSimon Schubert     return lookup_symbol_in_function_table (unit, sym, addr,
26115796c8dcSSimon Schubert 					    filename_ptr,
26125796c8dcSSimon Schubert 					    linenumber_ptr);
26135796c8dcSSimon Schubert 
26145796c8dcSSimon Schubert   return lookup_symbol_in_variable_table (unit, sym, addr,
26155796c8dcSSimon Schubert 					  filename_ptr,
26165796c8dcSSimon Schubert 					  linenumber_ptr);
26175796c8dcSSimon Schubert }
26185796c8dcSSimon Schubert 
26195796c8dcSSimon Schubert static struct funcinfo *
26205796c8dcSSimon Schubert reverse_funcinfo_list (struct funcinfo *head)
26215796c8dcSSimon Schubert {
26225796c8dcSSimon Schubert   struct funcinfo *rhead;
26235796c8dcSSimon Schubert   struct funcinfo *temp;
26245796c8dcSSimon Schubert 
26255796c8dcSSimon Schubert   for (rhead = NULL; head; head = temp)
26265796c8dcSSimon Schubert     {
26275796c8dcSSimon Schubert       temp = head->prev_func;
26285796c8dcSSimon Schubert       head->prev_func = rhead;
26295796c8dcSSimon Schubert       rhead = head;
26305796c8dcSSimon Schubert     }
26315796c8dcSSimon Schubert   return rhead;
26325796c8dcSSimon Schubert }
26335796c8dcSSimon Schubert 
26345796c8dcSSimon Schubert static struct varinfo *
26355796c8dcSSimon Schubert reverse_varinfo_list (struct varinfo *head)
26365796c8dcSSimon Schubert {
26375796c8dcSSimon Schubert   struct varinfo *rhead;
26385796c8dcSSimon Schubert   struct varinfo *temp;
26395796c8dcSSimon Schubert 
26405796c8dcSSimon Schubert   for (rhead = NULL; head; head = temp)
26415796c8dcSSimon Schubert     {
26425796c8dcSSimon Schubert       temp = head->prev_var;
26435796c8dcSSimon Schubert       head->prev_var = rhead;
26445796c8dcSSimon Schubert       rhead = head;
26455796c8dcSSimon Schubert     }
26465796c8dcSSimon Schubert   return rhead;
26475796c8dcSSimon Schubert }
26485796c8dcSSimon Schubert 
26495796c8dcSSimon Schubert /* Extract all interesting funcinfos and varinfos of a compilation
26505796c8dcSSimon Schubert    unit into hash tables for faster lookup.  Returns TRUE if no
26515796c8dcSSimon Schubert    errors were enountered; FALSE otherwise.  */
26525796c8dcSSimon Schubert 
26535796c8dcSSimon Schubert static bfd_boolean
26545796c8dcSSimon Schubert comp_unit_hash_info (struct dwarf2_debug *stash,
26555796c8dcSSimon Schubert 		     struct comp_unit *unit,
26565796c8dcSSimon Schubert 		     struct info_hash_table *funcinfo_hash_table,
26575796c8dcSSimon Schubert 		     struct info_hash_table *varinfo_hash_table)
26585796c8dcSSimon Schubert {
26595796c8dcSSimon Schubert   struct funcinfo* each_func;
26605796c8dcSSimon Schubert   struct varinfo* each_var;
26615796c8dcSSimon Schubert   bfd_boolean okay = TRUE;
26625796c8dcSSimon Schubert 
26635796c8dcSSimon Schubert   BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
26645796c8dcSSimon Schubert 
26655796c8dcSSimon Schubert   if (!comp_unit_maybe_decode_line_info (unit, stash))
26665796c8dcSSimon Schubert     return FALSE;
26675796c8dcSSimon Schubert 
26685796c8dcSSimon Schubert   BFD_ASSERT (!unit->cached);
26695796c8dcSSimon Schubert 
26705796c8dcSSimon Schubert   /* To preserve the original search order, we went to visit the function
26715796c8dcSSimon Schubert      infos in the reversed order of the list.  However, making the list
26725796c8dcSSimon Schubert      bi-directional use quite a bit of extra memory.  So we reverse
26735796c8dcSSimon Schubert      the list first, traverse the list in the now reversed order and
26745796c8dcSSimon Schubert      finally reverse the list again to get back the original order.  */
26755796c8dcSSimon Schubert   unit->function_table = reverse_funcinfo_list (unit->function_table);
26765796c8dcSSimon Schubert   for (each_func = unit->function_table;
26775796c8dcSSimon Schubert        each_func && okay;
26785796c8dcSSimon Schubert        each_func = each_func->prev_func)
26795796c8dcSSimon Schubert     {
26805796c8dcSSimon Schubert       /* Skip nameless functions. */
26815796c8dcSSimon Schubert       if (each_func->name)
26825796c8dcSSimon Schubert 	/* There is no need to copy name string into hash table as
26835796c8dcSSimon Schubert 	   name string is either in the dwarf string buffer or
26845796c8dcSSimon Schubert 	   info in the stash.  */
26855796c8dcSSimon Schubert 	okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
26865796c8dcSSimon Schubert 				       (void*) each_func, FALSE);
26875796c8dcSSimon Schubert     }
26885796c8dcSSimon Schubert   unit->function_table = reverse_funcinfo_list (unit->function_table);
26895796c8dcSSimon Schubert   if (!okay)
26905796c8dcSSimon Schubert     return FALSE;
26915796c8dcSSimon Schubert 
26925796c8dcSSimon Schubert   /* We do the same for variable infos.  */
26935796c8dcSSimon Schubert   unit->variable_table = reverse_varinfo_list (unit->variable_table);
26945796c8dcSSimon Schubert   for (each_var = unit->variable_table;
26955796c8dcSSimon Schubert        each_var && okay;
26965796c8dcSSimon Schubert        each_var = each_var->prev_var)
26975796c8dcSSimon Schubert     {
26985796c8dcSSimon Schubert       /* Skip stack vars and vars with no files or names.  */
26995796c8dcSSimon Schubert       if (each_var->stack == 0
27005796c8dcSSimon Schubert 	  && each_var->file != NULL
27015796c8dcSSimon Schubert 	  && each_var->name != NULL)
27025796c8dcSSimon Schubert 	/* There is no need to copy name string into hash table as
27035796c8dcSSimon Schubert 	   name string is either in the dwarf string buffer or
27045796c8dcSSimon Schubert 	   info in the stash.  */
27055796c8dcSSimon Schubert 	okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
27065796c8dcSSimon Schubert 				       (void*) each_var, FALSE);
27075796c8dcSSimon Schubert     }
27085796c8dcSSimon Schubert 
27095796c8dcSSimon Schubert   unit->variable_table = reverse_varinfo_list (unit->variable_table);
27105796c8dcSSimon Schubert   unit->cached = TRUE;
27115796c8dcSSimon Schubert   return okay;
27125796c8dcSSimon Schubert }
27135796c8dcSSimon Schubert 
27145796c8dcSSimon Schubert /* Locate a section in a BFD containing debugging info.  The search starts
27155796c8dcSSimon Schubert    from the section after AFTER_SEC, or from the first section in the BFD if
27165796c8dcSSimon Schubert    AFTER_SEC is NULL.  The search works by examining the names of the
27175796c8dcSSimon Schubert    sections.  There are two permissiable names.  The first is .debug_info.
27185796c8dcSSimon Schubert    This is the standard DWARF2 name.  The second is a prefix .gnu.linkonce.wi.
27195796c8dcSSimon Schubert    This is a variation on the .debug_info section which has a checksum
27205796c8dcSSimon Schubert    describing the contents appended onto the name.  This allows the linker to
27215796c8dcSSimon Schubert    identify and discard duplicate debugging sections for different
27225796c8dcSSimon Schubert    compilation units.  */
27235796c8dcSSimon Schubert #define DWARF2_DEBUG_INFO ".debug_info"
27245796c8dcSSimon Schubert #define DWARF2_COMPRESSED_DEBUG_INFO ".zdebug_info"
27255796c8dcSSimon Schubert #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
27265796c8dcSSimon Schubert 
27275796c8dcSSimon Schubert static asection *
27285796c8dcSSimon Schubert find_debug_info (bfd *abfd, asection *after_sec)
27295796c8dcSSimon Schubert {
27305796c8dcSSimon Schubert   asection * msec;
27315796c8dcSSimon Schubert 
27325796c8dcSSimon Schubert   msec = after_sec != NULL ? after_sec->next : abfd->sections;
27335796c8dcSSimon Schubert 
27345796c8dcSSimon Schubert   while (msec)
27355796c8dcSSimon Schubert     {
27365796c8dcSSimon Schubert       if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
27375796c8dcSSimon Schubert 	return msec;
27385796c8dcSSimon Schubert 
27395796c8dcSSimon Schubert       if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
27405796c8dcSSimon Schubert 	return msec;
27415796c8dcSSimon Schubert 
27425796c8dcSSimon Schubert       if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
27435796c8dcSSimon Schubert 	return msec;
27445796c8dcSSimon Schubert 
27455796c8dcSSimon Schubert       msec = msec->next;
27465796c8dcSSimon Schubert     }
27475796c8dcSSimon Schubert 
27485796c8dcSSimon Schubert   return NULL;
27495796c8dcSSimon Schubert }
27505796c8dcSSimon Schubert 
27515796c8dcSSimon Schubert /* Unset vmas for adjusted sections in STASH.  */
27525796c8dcSSimon Schubert 
27535796c8dcSSimon Schubert static void
27545796c8dcSSimon Schubert unset_sections (struct dwarf2_debug *stash)
27555796c8dcSSimon Schubert {
27565796c8dcSSimon Schubert   unsigned int i;
27575796c8dcSSimon Schubert   struct adjusted_section *p;
27585796c8dcSSimon Schubert 
27595796c8dcSSimon Schubert   i = stash->adjusted_section_count;
27605796c8dcSSimon Schubert   p = stash->adjusted_sections;
27615796c8dcSSimon Schubert   for (; i > 0; i--, p++)
27625796c8dcSSimon Schubert     p->section->vma = 0;
27635796c8dcSSimon Schubert }
27645796c8dcSSimon Schubert 
27655796c8dcSSimon Schubert /* Set unique VMAs for loadable and DWARF sections in ABFD and save
27665796c8dcSSimon Schubert    VMAs in STASH for unset_sections.  */
27675796c8dcSSimon Schubert 
27685796c8dcSSimon Schubert static bfd_boolean
27695796c8dcSSimon Schubert place_sections (bfd *abfd, struct dwarf2_debug *stash)
27705796c8dcSSimon Schubert {
27715796c8dcSSimon Schubert   struct adjusted_section *p;
27725796c8dcSSimon Schubert   unsigned int i;
27735796c8dcSSimon Schubert 
27745796c8dcSSimon Schubert   if (stash->adjusted_section_count != 0)
27755796c8dcSSimon Schubert     {
27765796c8dcSSimon Schubert       i = stash->adjusted_section_count;
27775796c8dcSSimon Schubert       p = stash->adjusted_sections;
27785796c8dcSSimon Schubert       for (; i > 0; i--, p++)
27795796c8dcSSimon Schubert 	p->section->vma = p->adj_vma;
27805796c8dcSSimon Schubert     }
27815796c8dcSSimon Schubert   else
27825796c8dcSSimon Schubert     {
27835796c8dcSSimon Schubert       asection *sect;
27845796c8dcSSimon Schubert       bfd_vma last_vma = 0, last_dwarf = 0;
27855796c8dcSSimon Schubert       bfd_size_type amt;
27865796c8dcSSimon Schubert 
27875796c8dcSSimon Schubert       i = 0;
27885796c8dcSSimon Schubert       for (sect = abfd->sections; sect != NULL; sect = sect->next)
27895796c8dcSSimon Schubert 	{
27905796c8dcSSimon Schubert 	  bfd_size_type sz;
27915796c8dcSSimon Schubert 	  int is_debug_info;
27925796c8dcSSimon Schubert 
27935796c8dcSSimon Schubert 	  if (sect->vma != 0)
27945796c8dcSSimon Schubert 	    continue;
27955796c8dcSSimon Schubert 
27965796c8dcSSimon Schubert 	  /* We need to adjust the VMAs of any .debug_info sections.
27975796c8dcSSimon Schubert 	     Skip compressed ones, since no relocations could target
27985796c8dcSSimon Schubert 	     them - they should not appear in object files anyway.  */
27995796c8dcSSimon Schubert 	  if (strcmp (sect->name, DWARF2_DEBUG_INFO) == 0)
28005796c8dcSSimon Schubert 	    is_debug_info = 1;
28015796c8dcSSimon Schubert 	  else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
28025796c8dcSSimon Schubert 	    is_debug_info = 1;
28035796c8dcSSimon Schubert 	  else
28045796c8dcSSimon Schubert 	    is_debug_info = 0;
28055796c8dcSSimon Schubert 
28065796c8dcSSimon Schubert 	  if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
28075796c8dcSSimon Schubert 	    continue;
28085796c8dcSSimon Schubert 
28095796c8dcSSimon Schubert 	  sz = sect->rawsize ? sect->rawsize : sect->size;
28105796c8dcSSimon Schubert 	  if (sz == 0)
28115796c8dcSSimon Schubert 	    continue;
28125796c8dcSSimon Schubert 
28135796c8dcSSimon Schubert 	  i++;
28145796c8dcSSimon Schubert 	}
28155796c8dcSSimon Schubert 
28165796c8dcSSimon Schubert       amt = i * sizeof (struct adjusted_section);
28175796c8dcSSimon Schubert       p = (struct adjusted_section *) bfd_zalloc (abfd, amt);
28185796c8dcSSimon Schubert       if (! p)
28195796c8dcSSimon Schubert 	return FALSE;
28205796c8dcSSimon Schubert 
28215796c8dcSSimon Schubert       stash->adjusted_sections = p;
28225796c8dcSSimon Schubert       stash->adjusted_section_count = i;
28235796c8dcSSimon Schubert 
28245796c8dcSSimon Schubert       for (sect = abfd->sections; sect != NULL; sect = sect->next)
28255796c8dcSSimon Schubert 	{
28265796c8dcSSimon Schubert 	  bfd_size_type sz;
28275796c8dcSSimon Schubert 	  int is_debug_info;
28285796c8dcSSimon Schubert 
28295796c8dcSSimon Schubert 	  if (sect->vma != 0)
28305796c8dcSSimon Schubert 	    continue;
28315796c8dcSSimon Schubert 
28325796c8dcSSimon Schubert 	  /* We need to adjust the VMAs of any .debug_info sections.
28335796c8dcSSimon Schubert 	     Skip compressed ones, since no relocations could target
28345796c8dcSSimon Schubert 	     them - they should not appear in object files anyway.  */
28355796c8dcSSimon Schubert 	  if (strcmp (sect->name, DWARF2_DEBUG_INFO) == 0)
28365796c8dcSSimon Schubert 	    is_debug_info = 1;
28375796c8dcSSimon Schubert 	  else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
28385796c8dcSSimon Schubert 	    is_debug_info = 1;
28395796c8dcSSimon Schubert 	  else
28405796c8dcSSimon Schubert 	    is_debug_info = 0;
28415796c8dcSSimon Schubert 
28425796c8dcSSimon Schubert 	  if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
28435796c8dcSSimon Schubert 	    continue;
28445796c8dcSSimon Schubert 
28455796c8dcSSimon Schubert 	  sz = sect->rawsize ? sect->rawsize : sect->size;
28465796c8dcSSimon Schubert 	  if (sz == 0)
28475796c8dcSSimon Schubert 	    continue;
28485796c8dcSSimon Schubert 
28495796c8dcSSimon Schubert 	  p->section = sect;
28505796c8dcSSimon Schubert 	  if (is_debug_info)
28515796c8dcSSimon Schubert 	    {
28525796c8dcSSimon Schubert 	      BFD_ASSERT (sect->alignment_power == 0);
28535796c8dcSSimon Schubert 	      sect->vma = last_dwarf;
28545796c8dcSSimon Schubert 	      last_dwarf += sz;
28555796c8dcSSimon Schubert 	    }
28565796c8dcSSimon Schubert 	  else if (last_vma != 0)
28575796c8dcSSimon Schubert 	    {
28585796c8dcSSimon Schubert 	      /* Align the new address to the current section
28595796c8dcSSimon Schubert 		 alignment.  */
28605796c8dcSSimon Schubert 	      last_vma = ((last_vma
28615796c8dcSSimon Schubert 			   + ~((bfd_vma) -1 << sect->alignment_power))
28625796c8dcSSimon Schubert 			  & ((bfd_vma) -1 << sect->alignment_power));
28635796c8dcSSimon Schubert 	      sect->vma = last_vma;
28645796c8dcSSimon Schubert 	      last_vma += sect->vma + sz;
28655796c8dcSSimon Schubert 	    }
28665796c8dcSSimon Schubert 	  else
28675796c8dcSSimon Schubert 	    last_vma += sect->vma + sz;
28685796c8dcSSimon Schubert 
28695796c8dcSSimon Schubert 	  p->adj_vma = sect->vma;
28705796c8dcSSimon Schubert 
28715796c8dcSSimon Schubert 	  p++;
28725796c8dcSSimon Schubert 	}
28735796c8dcSSimon Schubert     }
28745796c8dcSSimon Schubert 
28755796c8dcSSimon Schubert   return TRUE;
28765796c8dcSSimon Schubert }
28775796c8dcSSimon Schubert 
28785796c8dcSSimon Schubert /* Look up a funcinfo by name using the given info hash table.  If found,
28795796c8dcSSimon Schubert    also update the locations pointed to by filename_ptr and linenumber_ptr.
28805796c8dcSSimon Schubert 
28815796c8dcSSimon Schubert    This function returns TRUE if a funcinfo that matches the given symbol
28825796c8dcSSimon Schubert    and address is found with any error; otherwise it returns FALSE.  */
28835796c8dcSSimon Schubert 
28845796c8dcSSimon Schubert static bfd_boolean
28855796c8dcSSimon Schubert info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
28865796c8dcSSimon Schubert 			   asymbol *sym,
28875796c8dcSSimon Schubert 			   bfd_vma addr,
28885796c8dcSSimon Schubert 			   const char **filename_ptr,
28895796c8dcSSimon Schubert 			   unsigned int *linenumber_ptr)
28905796c8dcSSimon Schubert {
28915796c8dcSSimon Schubert   struct funcinfo* each_func;
28925796c8dcSSimon Schubert   struct funcinfo* best_fit = NULL;
28935796c8dcSSimon Schubert   struct info_list_node *node;
28945796c8dcSSimon Schubert   struct arange *arange;
28955796c8dcSSimon Schubert   const char *name = bfd_asymbol_name (sym);
28965796c8dcSSimon Schubert   asection *sec = bfd_get_section (sym);
28975796c8dcSSimon Schubert 
28985796c8dcSSimon Schubert   for (node = lookup_info_hash_table (hash_table, name);
28995796c8dcSSimon Schubert        node;
29005796c8dcSSimon Schubert        node = node->next)
29015796c8dcSSimon Schubert     {
29025796c8dcSSimon Schubert       each_func = (struct funcinfo *) node->info;
29035796c8dcSSimon Schubert       for (arange = &each_func->arange;
29045796c8dcSSimon Schubert 	   arange;
29055796c8dcSSimon Schubert 	   arange = arange->next)
29065796c8dcSSimon Schubert 	{
29075796c8dcSSimon Schubert 	  if ((!each_func->sec || each_func->sec == sec)
29085796c8dcSSimon Schubert 	      && addr >= arange->low
29095796c8dcSSimon Schubert 	      && addr < arange->high
29105796c8dcSSimon Schubert 	      && (!best_fit
29115796c8dcSSimon Schubert 		  || ((arange->high - arange->low)
29125796c8dcSSimon Schubert 		      < (best_fit->arange.high - best_fit->arange.low))))
29135796c8dcSSimon Schubert 	    best_fit = each_func;
29145796c8dcSSimon Schubert 	}
29155796c8dcSSimon Schubert     }
29165796c8dcSSimon Schubert 
29175796c8dcSSimon Schubert   if (best_fit)
29185796c8dcSSimon Schubert     {
29195796c8dcSSimon Schubert       best_fit->sec = sec;
29205796c8dcSSimon Schubert       *filename_ptr = best_fit->file;
29215796c8dcSSimon Schubert       *linenumber_ptr = best_fit->line;
29225796c8dcSSimon Schubert       return TRUE;
29235796c8dcSSimon Schubert     }
29245796c8dcSSimon Schubert 
29255796c8dcSSimon Schubert   return FALSE;
29265796c8dcSSimon Schubert }
29275796c8dcSSimon Schubert 
29285796c8dcSSimon Schubert /* Look up a varinfo by name using the given info hash table.  If found,
29295796c8dcSSimon Schubert    also update the locations pointed to by filename_ptr and linenumber_ptr.
29305796c8dcSSimon Schubert 
29315796c8dcSSimon Schubert    This function returns TRUE if a varinfo that matches the given symbol
29325796c8dcSSimon Schubert    and address is found with any error; otherwise it returns FALSE.  */
29335796c8dcSSimon Schubert 
29345796c8dcSSimon Schubert static bfd_boolean
29355796c8dcSSimon Schubert info_hash_lookup_varinfo (struct info_hash_table *hash_table,
29365796c8dcSSimon Schubert 			  asymbol *sym,
29375796c8dcSSimon Schubert 			  bfd_vma addr,
29385796c8dcSSimon Schubert 			  const char **filename_ptr,
29395796c8dcSSimon Schubert 			  unsigned int *linenumber_ptr)
29405796c8dcSSimon Schubert {
29415796c8dcSSimon Schubert   const char *name = bfd_asymbol_name (sym);
29425796c8dcSSimon Schubert   asection *sec = bfd_get_section (sym);
29435796c8dcSSimon Schubert   struct varinfo* each;
29445796c8dcSSimon Schubert   struct info_list_node *node;
29455796c8dcSSimon Schubert 
29465796c8dcSSimon Schubert   for (node = lookup_info_hash_table (hash_table, name);
29475796c8dcSSimon Schubert        node;
29485796c8dcSSimon Schubert        node = node->next)
29495796c8dcSSimon Schubert     {
29505796c8dcSSimon Schubert       each = (struct varinfo *) node->info;
29515796c8dcSSimon Schubert       if (each->addr == addr
29525796c8dcSSimon Schubert 	  && (!each->sec || each->sec == sec))
29535796c8dcSSimon Schubert 	{
29545796c8dcSSimon Schubert 	  each->sec = sec;
29555796c8dcSSimon Schubert 	  *filename_ptr = each->file;
29565796c8dcSSimon Schubert 	  *linenumber_ptr = each->line;
29575796c8dcSSimon Schubert 	  return TRUE;
29585796c8dcSSimon Schubert 	}
29595796c8dcSSimon Schubert     }
29605796c8dcSSimon Schubert 
29615796c8dcSSimon Schubert   return FALSE;
29625796c8dcSSimon Schubert }
29635796c8dcSSimon Schubert 
29645796c8dcSSimon Schubert /* Update the funcinfo and varinfo info hash tables if they are
29655796c8dcSSimon Schubert    not up to date.  Returns TRUE if there is no error; otherwise
29665796c8dcSSimon Schubert    returns FALSE and disable the info hash tables.  */
29675796c8dcSSimon Schubert 
29685796c8dcSSimon Schubert static bfd_boolean
29695796c8dcSSimon Schubert stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
29705796c8dcSSimon Schubert {
29715796c8dcSSimon Schubert   struct comp_unit *each;
29725796c8dcSSimon Schubert 
29735796c8dcSSimon Schubert   /* Exit if hash tables are up-to-date.  */
29745796c8dcSSimon Schubert   if (stash->all_comp_units == stash->hash_units_head)
29755796c8dcSSimon Schubert     return TRUE;
29765796c8dcSSimon Schubert 
29775796c8dcSSimon Schubert   if (stash->hash_units_head)
29785796c8dcSSimon Schubert     each = stash->hash_units_head->prev_unit;
29795796c8dcSSimon Schubert   else
29805796c8dcSSimon Schubert     each = stash->last_comp_unit;
29815796c8dcSSimon Schubert 
29825796c8dcSSimon Schubert   while (each)
29835796c8dcSSimon Schubert     {
29845796c8dcSSimon Schubert       if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
29855796c8dcSSimon Schubert 				stash->varinfo_hash_table))
29865796c8dcSSimon Schubert 	{
29875796c8dcSSimon Schubert 	  stash->info_hash_status = STASH_INFO_HASH_DISABLED;
29885796c8dcSSimon Schubert 	  return FALSE;
29895796c8dcSSimon Schubert 	}
29905796c8dcSSimon Schubert       each = each->prev_unit;
29915796c8dcSSimon Schubert     }
29925796c8dcSSimon Schubert 
29935796c8dcSSimon Schubert   stash->hash_units_head = stash->all_comp_units;
29945796c8dcSSimon Schubert   return TRUE;
29955796c8dcSSimon Schubert }
29965796c8dcSSimon Schubert 
29975796c8dcSSimon Schubert /* Check consistency of info hash tables.  This is for debugging only. */
29985796c8dcSSimon Schubert 
29995796c8dcSSimon Schubert static void ATTRIBUTE_UNUSED
30005796c8dcSSimon Schubert stash_verify_info_hash_table (struct dwarf2_debug *stash)
30015796c8dcSSimon Schubert {
30025796c8dcSSimon Schubert   struct comp_unit *each_unit;
30035796c8dcSSimon Schubert   struct funcinfo *each_func;
30045796c8dcSSimon Schubert   struct varinfo *each_var;
30055796c8dcSSimon Schubert   struct info_list_node *node;
30065796c8dcSSimon Schubert   bfd_boolean found;
30075796c8dcSSimon Schubert 
30085796c8dcSSimon Schubert   for (each_unit = stash->all_comp_units;
30095796c8dcSSimon Schubert        each_unit;
30105796c8dcSSimon Schubert        each_unit = each_unit->next_unit)
30115796c8dcSSimon Schubert     {
30125796c8dcSSimon Schubert       for (each_func = each_unit->function_table;
30135796c8dcSSimon Schubert 	   each_func;
30145796c8dcSSimon Schubert 	   each_func = each_func->prev_func)
30155796c8dcSSimon Schubert 	{
30165796c8dcSSimon Schubert 	  if (!each_func->name)
30175796c8dcSSimon Schubert 	    continue;
30185796c8dcSSimon Schubert 	  node = lookup_info_hash_table (stash->funcinfo_hash_table,
30195796c8dcSSimon Schubert 					 each_func->name);
30205796c8dcSSimon Schubert 	  BFD_ASSERT (node);
30215796c8dcSSimon Schubert 	  found = FALSE;
30225796c8dcSSimon Schubert 	  while (node && !found)
30235796c8dcSSimon Schubert 	    {
30245796c8dcSSimon Schubert 	      found = node->info == each_func;
30255796c8dcSSimon Schubert 	      node = node->next;
30265796c8dcSSimon Schubert 	    }
30275796c8dcSSimon Schubert 	  BFD_ASSERT (found);
30285796c8dcSSimon Schubert 	}
30295796c8dcSSimon Schubert 
30305796c8dcSSimon Schubert       for (each_var = each_unit->variable_table;
30315796c8dcSSimon Schubert 	   each_var;
30325796c8dcSSimon Schubert 	   each_var = each_var->prev_var)
30335796c8dcSSimon Schubert 	{
30345796c8dcSSimon Schubert 	  if (!each_var->name || !each_var->file || each_var->stack)
30355796c8dcSSimon Schubert 	    continue;
30365796c8dcSSimon Schubert 	  node = lookup_info_hash_table (stash->varinfo_hash_table,
30375796c8dcSSimon Schubert 					 each_var->name);
30385796c8dcSSimon Schubert 	  BFD_ASSERT (node);
30395796c8dcSSimon Schubert 	  found = FALSE;
30405796c8dcSSimon Schubert 	  while (node && !found)
30415796c8dcSSimon Schubert 	    {
30425796c8dcSSimon Schubert 	      found = node->info == each_var;
30435796c8dcSSimon Schubert 	      node = node->next;
30445796c8dcSSimon Schubert 	    }
30455796c8dcSSimon Schubert 	  BFD_ASSERT (found);
30465796c8dcSSimon Schubert 	}
30475796c8dcSSimon Schubert     }
30485796c8dcSSimon Schubert }
30495796c8dcSSimon Schubert 
30505796c8dcSSimon Schubert /* Check to see if we want to enable the info hash tables, which consume
30515796c8dcSSimon Schubert    quite a bit of memory.  Currently we only check the number times
30525796c8dcSSimon Schubert    bfd_dwarf2_find_line is called.  In the future, we may also want to
30535796c8dcSSimon Schubert    take the number of symbols into account.  */
30545796c8dcSSimon Schubert 
30555796c8dcSSimon Schubert static void
30565796c8dcSSimon Schubert stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
30575796c8dcSSimon Schubert {
30585796c8dcSSimon Schubert   BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
30595796c8dcSSimon Schubert 
30605796c8dcSSimon Schubert   if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
30615796c8dcSSimon Schubert     return;
30625796c8dcSSimon Schubert 
30635796c8dcSSimon Schubert   /* FIXME: Maybe we should check the reduce_memory_overheads
30645796c8dcSSimon Schubert      and optimize fields in the bfd_link_info structure ?  */
30655796c8dcSSimon Schubert 
30665796c8dcSSimon Schubert   /* Create hash tables.  */
30675796c8dcSSimon Schubert   stash->funcinfo_hash_table = create_info_hash_table (abfd);
30685796c8dcSSimon Schubert   stash->varinfo_hash_table = create_info_hash_table (abfd);
30695796c8dcSSimon Schubert   if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
30705796c8dcSSimon Schubert     {
30715796c8dcSSimon Schubert       /* Turn off info hashes if any allocation above fails.  */
30725796c8dcSSimon Schubert       stash->info_hash_status = STASH_INFO_HASH_DISABLED;
30735796c8dcSSimon Schubert       return;
30745796c8dcSSimon Schubert     }
30755796c8dcSSimon Schubert   /* We need a forced update so that the info hash tables will
30765796c8dcSSimon Schubert      be created even though there is no compilation unit.  That
30775796c8dcSSimon Schubert      happens if STASH_INFO_HASH_TRIGGER is 0.  */
30785796c8dcSSimon Schubert   stash_maybe_update_info_hash_tables (stash);
30795796c8dcSSimon Schubert   stash->info_hash_status = STASH_INFO_HASH_ON;
30805796c8dcSSimon Schubert }
30815796c8dcSSimon Schubert 
30825796c8dcSSimon Schubert /* Find the file and line associated with a symbol and address using the
30835796c8dcSSimon Schubert    info hash tables of a stash. If there is a match, the function returns
30845796c8dcSSimon Schubert    TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
30855796c8dcSSimon Schubert    otherwise it returns FALSE.  */
30865796c8dcSSimon Schubert 
30875796c8dcSSimon Schubert static bfd_boolean
30885796c8dcSSimon Schubert stash_find_line_fast (struct dwarf2_debug *stash,
30895796c8dcSSimon Schubert 		      asymbol *sym,
30905796c8dcSSimon Schubert 		      bfd_vma addr,
30915796c8dcSSimon Schubert 		      const char **filename_ptr,
30925796c8dcSSimon Schubert 		      unsigned int *linenumber_ptr)
30935796c8dcSSimon Schubert {
30945796c8dcSSimon Schubert   BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
30955796c8dcSSimon Schubert 
30965796c8dcSSimon Schubert   if (sym->flags & BSF_FUNCTION)
30975796c8dcSSimon Schubert     return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
30985796c8dcSSimon Schubert 				      filename_ptr, linenumber_ptr);
30995796c8dcSSimon Schubert   return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
31005796c8dcSSimon Schubert 				   filename_ptr, linenumber_ptr);
31015796c8dcSSimon Schubert }
31025796c8dcSSimon Schubert 
31035796c8dcSSimon Schubert /* Find the source code location of SYMBOL.  If SYMBOL is NULL
31045796c8dcSSimon Schubert    then find the nearest source code location corresponding to
31055796c8dcSSimon Schubert    the address SECTION + OFFSET.
31065796c8dcSSimon Schubert    Returns TRUE if the line is found without error and fills in
31075796c8dcSSimon Schubert    FILENAME_PTR and LINENUMBER_PTR.  In the case where SYMBOL was
31085796c8dcSSimon Schubert    NULL the FUNCTIONNAME_PTR is also filled in.
31095796c8dcSSimon Schubert    SYMBOLS contains the symbol table for ABFD.
31105796c8dcSSimon Schubert    ADDR_SIZE is the number of bytes in the initial .debug_info length
31115796c8dcSSimon Schubert    field and in the abbreviation offset, or zero to indicate that the
31125796c8dcSSimon Schubert    default value should be used.  */
31135796c8dcSSimon Schubert 
31145796c8dcSSimon Schubert static bfd_boolean
31155796c8dcSSimon Schubert find_line (bfd *abfd,
31165796c8dcSSimon Schubert 	   asection *section,
31175796c8dcSSimon Schubert 	   bfd_vma offset,
31185796c8dcSSimon Schubert 	   asymbol *symbol,
31195796c8dcSSimon Schubert 	   asymbol **symbols,
31205796c8dcSSimon Schubert 	   const char **filename_ptr,
31215796c8dcSSimon Schubert 	   const char **functionname_ptr,
31225796c8dcSSimon Schubert 	   unsigned int *linenumber_ptr,
31235796c8dcSSimon Schubert 	   unsigned int addr_size,
31245796c8dcSSimon Schubert 	   void **pinfo)
31255796c8dcSSimon Schubert {
31265796c8dcSSimon Schubert   /* Read each compilation unit from the section .debug_info, and check
31275796c8dcSSimon Schubert      to see if it contains the address we are searching for.  If yes,
31285796c8dcSSimon Schubert      lookup the address, and return the line number info.  If no, go
31295796c8dcSSimon Schubert      on to the next compilation unit.
31305796c8dcSSimon Schubert 
31315796c8dcSSimon Schubert      We keep a list of all the previously read compilation units, and
31325796c8dcSSimon Schubert      a pointer to the next un-read compilation unit.  Check the
31335796c8dcSSimon Schubert      previously read units before reading more.  */
31345796c8dcSSimon Schubert   struct dwarf2_debug *stash;
31355796c8dcSSimon Schubert   /* What address are we looking for?  */
31365796c8dcSSimon Schubert   bfd_vma addr;
31375796c8dcSSimon Schubert   struct comp_unit* each;
31385796c8dcSSimon Schubert   bfd_vma found = FALSE;
31395796c8dcSSimon Schubert   bfd_boolean do_line;
31405796c8dcSSimon Schubert 
31415796c8dcSSimon Schubert   stash = (struct dwarf2_debug *) *pinfo;
31425796c8dcSSimon Schubert 
31435796c8dcSSimon Schubert   if (! stash)
31445796c8dcSSimon Schubert     {
31455796c8dcSSimon Schubert       bfd_size_type amt = sizeof (struct dwarf2_debug);
31465796c8dcSSimon Schubert 
31475796c8dcSSimon Schubert       stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
31485796c8dcSSimon Schubert       if (! stash)
31495796c8dcSSimon Schubert 	return FALSE;
31505796c8dcSSimon Schubert     }
31515796c8dcSSimon Schubert 
31525796c8dcSSimon Schubert   /* In a relocatable file, 2 functions may have the same address.
31535796c8dcSSimon Schubert      We change the section vma so that they won't overlap.  */
31545796c8dcSSimon Schubert   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
31555796c8dcSSimon Schubert     {
31565796c8dcSSimon Schubert       if (! place_sections (abfd, stash))
31575796c8dcSSimon Schubert 	return FALSE;
31585796c8dcSSimon Schubert     }
31595796c8dcSSimon Schubert 
31605796c8dcSSimon Schubert   do_line = (section == NULL
31615796c8dcSSimon Schubert 	     && offset == 0
31625796c8dcSSimon Schubert 	     && functionname_ptr == NULL
31635796c8dcSSimon Schubert 	     && symbol != NULL);
31645796c8dcSSimon Schubert   if (do_line)
31655796c8dcSSimon Schubert     {
31665796c8dcSSimon Schubert       addr = symbol->value;
31675796c8dcSSimon Schubert       section = bfd_get_section (symbol);
31685796c8dcSSimon Schubert     }
31695796c8dcSSimon Schubert   else if (section != NULL
31705796c8dcSSimon Schubert 	   && functionname_ptr != NULL
31715796c8dcSSimon Schubert 	   && symbol == NULL)
31725796c8dcSSimon Schubert     addr = offset;
31735796c8dcSSimon Schubert   else
31745796c8dcSSimon Schubert     abort ();
31755796c8dcSSimon Schubert 
31765796c8dcSSimon Schubert   if (section->output_section)
31775796c8dcSSimon Schubert     addr += section->output_section->vma + section->output_offset;
31785796c8dcSSimon Schubert   else
31795796c8dcSSimon Schubert     addr += section->vma;
31805796c8dcSSimon Schubert   *filename_ptr = NULL;
31815796c8dcSSimon Schubert   if (! do_line)
31825796c8dcSSimon Schubert     *functionname_ptr = NULL;
31835796c8dcSSimon Schubert   *linenumber_ptr = 0;
31845796c8dcSSimon Schubert 
31855796c8dcSSimon Schubert   if (! *pinfo)
31865796c8dcSSimon Schubert     {
31875796c8dcSSimon Schubert       bfd *debug_bfd;
31885796c8dcSSimon Schubert       bfd_size_type total_size;
31895796c8dcSSimon Schubert       asection *msec;
31905796c8dcSSimon Schubert 
31915796c8dcSSimon Schubert       *pinfo = stash;
31925796c8dcSSimon Schubert 
31935796c8dcSSimon Schubert       msec = find_debug_info (abfd, NULL);
31945796c8dcSSimon Schubert       if (msec == NULL)
31955796c8dcSSimon Schubert 	{
31965796c8dcSSimon Schubert 	  char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
31975796c8dcSSimon Schubert 
31985796c8dcSSimon Schubert 	  if (debug_filename == NULL)
31995796c8dcSSimon Schubert 	    /* No dwarf2 info, and no gnu_debuglink to follow.
32005796c8dcSSimon Schubert 	       Note that at this point the stash has been allocated, but
32015796c8dcSSimon Schubert 	       contains zeros.  This lets future calls to this function
32025796c8dcSSimon Schubert 	       fail more quickly.  */
32035796c8dcSSimon Schubert 	    goto done;
32045796c8dcSSimon Schubert 
32055796c8dcSSimon Schubert 	  if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
32065796c8dcSSimon Schubert 	      || ! bfd_check_format (debug_bfd, bfd_object)
32075796c8dcSSimon Schubert 	      || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
32085796c8dcSSimon Schubert 	    {
32095796c8dcSSimon Schubert 	      if (debug_bfd)
32105796c8dcSSimon Schubert 		bfd_close (debug_bfd);
32115796c8dcSSimon Schubert 	      /* FIXME: Should we report our failure to follow the debuglink ?  */
32125796c8dcSSimon Schubert 	      free (debug_filename);
32135796c8dcSSimon Schubert 	      goto done;
32145796c8dcSSimon Schubert 	    }
32155796c8dcSSimon Schubert 	}
32165796c8dcSSimon Schubert       else
32175796c8dcSSimon Schubert 	debug_bfd = abfd;
32185796c8dcSSimon Schubert 
32195796c8dcSSimon Schubert       /* There can be more than one DWARF2 info section in a BFD these
32205796c8dcSSimon Schubert 	 days.  First handle the easy case when there's only one.  If
32215796c8dcSSimon Schubert 	 there's more than one, try case two: none of the sections is
32225796c8dcSSimon Schubert 	 compressed.  In that case, read them all in and produce one
32235796c8dcSSimon Schubert 	 large stash.  We do this in two passes - in the first pass we
32245796c8dcSSimon Schubert 	 just accumulate the section sizes, and in the second pass we
32255796c8dcSSimon Schubert 	 read in the section's contents.  (The allows us to avoid
32265796c8dcSSimon Schubert 	 reallocing the data as we add sections to the stash.)  If
32275796c8dcSSimon Schubert 	 some or all sections are compressed, then do things the slow
32285796c8dcSSimon Schubert 	 way, with a bunch of reallocs.  */
32295796c8dcSSimon Schubert 
32305796c8dcSSimon Schubert       if (! find_debug_info (debug_bfd, msec))
32315796c8dcSSimon Schubert 	{
32325796c8dcSSimon Schubert 	  /* Case 1: only one info section.  */
32335796c8dcSSimon Schubert 	  total_size = msec->size;
3234*c50c785cSJohn Marino 	  if (! read_section (debug_bfd, debug_info, symbols, 0,
32355796c8dcSSimon Schubert 			      &stash->info_ptr_memory, &total_size))
32365796c8dcSSimon Schubert 	    goto done;
32375796c8dcSSimon Schubert 	}
32385796c8dcSSimon Schubert       else
32395796c8dcSSimon Schubert 	{
3240*c50c785cSJohn Marino 	  /* Case 2: multiple sections.  */
32415796c8dcSSimon Schubert 	  for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
32425796c8dcSSimon Schubert 	    total_size += msec->size;
3243*c50c785cSJohn Marino 
32445796c8dcSSimon Schubert 	  stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
32455796c8dcSSimon Schubert 	  if (stash->info_ptr_memory == NULL)
32465796c8dcSSimon Schubert 	    goto done;
32475796c8dcSSimon Schubert 
32485796c8dcSSimon Schubert 	  total_size = 0;
32495796c8dcSSimon Schubert 	  for (msec = find_debug_info (debug_bfd, NULL);
32505796c8dcSSimon Schubert 	       msec;
32515796c8dcSSimon Schubert 	       msec = find_debug_info (debug_bfd, msec))
32525796c8dcSSimon Schubert 	    {
32535796c8dcSSimon Schubert 	      bfd_size_type size;
32545796c8dcSSimon Schubert 
32555796c8dcSSimon Schubert 	      size = msec->size;
32565796c8dcSSimon Schubert 	      if (size == 0)
32575796c8dcSSimon Schubert 		continue;
32585796c8dcSSimon Schubert 
32595796c8dcSSimon Schubert 	      if (!(bfd_simple_get_relocated_section_contents
32605796c8dcSSimon Schubert 		    (debug_bfd, msec, stash->info_ptr_memory + total_size,
32615796c8dcSSimon Schubert 		     symbols)))
32625796c8dcSSimon Schubert 		goto done;
32635796c8dcSSimon Schubert 
32645796c8dcSSimon Schubert 	      total_size += size;
32655796c8dcSSimon Schubert 	    }
32665796c8dcSSimon Schubert 	}
32675796c8dcSSimon Schubert 
32685796c8dcSSimon Schubert       stash->info_ptr = stash->info_ptr_memory;
32695796c8dcSSimon Schubert       stash->info_ptr_end = stash->info_ptr + total_size;
32705796c8dcSSimon Schubert       stash->sec = find_debug_info (debug_bfd, NULL);
32715796c8dcSSimon Schubert       stash->sec_info_ptr = stash->info_ptr;
32725796c8dcSSimon Schubert       stash->syms = symbols;
32735796c8dcSSimon Schubert       stash->bfd_ptr = debug_bfd;
32745796c8dcSSimon Schubert     }
32755796c8dcSSimon Schubert 
32765796c8dcSSimon Schubert   /* A null info_ptr indicates that there is no dwarf2 info
32775796c8dcSSimon Schubert      (or that an error occured while setting up the stash).  */
32785796c8dcSSimon Schubert   if (! stash->info_ptr)
32795796c8dcSSimon Schubert     goto done;
32805796c8dcSSimon Schubert 
32815796c8dcSSimon Schubert   stash->inliner_chain = NULL;
32825796c8dcSSimon Schubert 
32835796c8dcSSimon Schubert   /* Check the previously read comp. units first.  */
32845796c8dcSSimon Schubert   if (do_line)
32855796c8dcSSimon Schubert     {
32865796c8dcSSimon Schubert       /* The info hash tables use quite a bit of memory.  We may not want to
32875796c8dcSSimon Schubert 	 always use them.  We use some heuristics to decide if and when to
32885796c8dcSSimon Schubert 	 turn it on.  */
32895796c8dcSSimon Schubert       if (stash->info_hash_status == STASH_INFO_HASH_OFF)
32905796c8dcSSimon Schubert 	stash_maybe_enable_info_hash_tables (abfd, stash);
32915796c8dcSSimon Schubert 
32925796c8dcSSimon Schubert       /* Keep info hash table up to date if they are available.  Note that we
32935796c8dcSSimon Schubert 	 may disable the hash tables if there is any error duing update. */
32945796c8dcSSimon Schubert       if (stash->info_hash_status == STASH_INFO_HASH_ON)
32955796c8dcSSimon Schubert 	stash_maybe_update_info_hash_tables (stash);
32965796c8dcSSimon Schubert 
32975796c8dcSSimon Schubert       if (stash->info_hash_status == STASH_INFO_HASH_ON)
32985796c8dcSSimon Schubert 	{
32995796c8dcSSimon Schubert 	  found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
33005796c8dcSSimon Schubert 					linenumber_ptr);
33015796c8dcSSimon Schubert 	  if (found)
33025796c8dcSSimon Schubert 	    goto done;
33035796c8dcSSimon Schubert 	}
33045796c8dcSSimon Schubert       else
33055796c8dcSSimon Schubert 	{
33065796c8dcSSimon Schubert 	  /* Check the previously read comp. units first.  */
33075796c8dcSSimon Schubert 	  for (each = stash->all_comp_units; each; each = each->next_unit)
33085796c8dcSSimon Schubert 	    if ((symbol->flags & BSF_FUNCTION) == 0
33095796c8dcSSimon Schubert 		|| comp_unit_contains_address (each, addr))
33105796c8dcSSimon Schubert 	      {
33115796c8dcSSimon Schubert 		found = comp_unit_find_line (each, symbol, addr, filename_ptr,
33125796c8dcSSimon Schubert 					     linenumber_ptr, stash);
33135796c8dcSSimon Schubert 		if (found)
33145796c8dcSSimon Schubert 		  goto done;
33155796c8dcSSimon Schubert 	      }
33165796c8dcSSimon Schubert 	}
33175796c8dcSSimon Schubert     }
33185796c8dcSSimon Schubert   else
33195796c8dcSSimon Schubert     {
33205796c8dcSSimon Schubert       for (each = stash->all_comp_units; each; each = each->next_unit)
33215796c8dcSSimon Schubert 	{
33225796c8dcSSimon Schubert 	  found = (comp_unit_contains_address (each, addr)
33235796c8dcSSimon Schubert 		   && comp_unit_find_nearest_line (each, addr,
33245796c8dcSSimon Schubert 						   filename_ptr,
33255796c8dcSSimon Schubert 						   functionname_ptr,
33265796c8dcSSimon Schubert 						   linenumber_ptr,
33275796c8dcSSimon Schubert 						   stash));
33285796c8dcSSimon Schubert 	  if (found)
33295796c8dcSSimon Schubert 	    goto done;
33305796c8dcSSimon Schubert 	}
33315796c8dcSSimon Schubert     }
33325796c8dcSSimon Schubert 
33335796c8dcSSimon Schubert   /* The DWARF2 spec says that the initial length field, and the
33345796c8dcSSimon Schubert      offset of the abbreviation table, should both be 4-byte values.
33355796c8dcSSimon Schubert      However, some compilers do things differently.  */
33365796c8dcSSimon Schubert   if (addr_size == 0)
33375796c8dcSSimon Schubert     addr_size = 4;
33385796c8dcSSimon Schubert   BFD_ASSERT (addr_size == 4 || addr_size == 8);
33395796c8dcSSimon Schubert 
33405796c8dcSSimon Schubert   /* Read each remaining comp. units checking each as they are read.  */
33415796c8dcSSimon Schubert   while (stash->info_ptr < stash->info_ptr_end)
33425796c8dcSSimon Schubert     {
33435796c8dcSSimon Schubert       bfd_vma length;
33445796c8dcSSimon Schubert       unsigned int offset_size = addr_size;
33455796c8dcSSimon Schubert       bfd_byte *info_ptr_unit = stash->info_ptr;
33465796c8dcSSimon Schubert 
33475796c8dcSSimon Schubert       length = read_4_bytes (stash->bfd_ptr, stash->info_ptr);
33485796c8dcSSimon Schubert       /* A 0xffffff length is the DWARF3 way of indicating
33495796c8dcSSimon Schubert 	 we use 64-bit offsets, instead of 32-bit offsets.  */
33505796c8dcSSimon Schubert       if (length == 0xffffffff)
33515796c8dcSSimon Schubert 	{
33525796c8dcSSimon Schubert 	  offset_size = 8;
33535796c8dcSSimon Schubert 	  length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4);
33545796c8dcSSimon Schubert 	  stash->info_ptr += 12;
33555796c8dcSSimon Schubert 	}
33565796c8dcSSimon Schubert       /* A zero length is the IRIX way of indicating 64-bit offsets,
33575796c8dcSSimon Schubert 	 mostly because the 64-bit length will generally fit in 32
33585796c8dcSSimon Schubert 	 bits, and the endianness helps.  */
33595796c8dcSSimon Schubert       else if (length == 0)
33605796c8dcSSimon Schubert 	{
33615796c8dcSSimon Schubert 	  offset_size = 8;
33625796c8dcSSimon Schubert 	  length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4);
33635796c8dcSSimon Schubert 	  stash->info_ptr += 8;
33645796c8dcSSimon Schubert 	}
33655796c8dcSSimon Schubert       /* In the absence of the hints above, we assume 32-bit DWARF2
33665796c8dcSSimon Schubert 	 offsets even for targets with 64-bit addresses, because:
33675796c8dcSSimon Schubert 	   a) most of the time these targets will not have generated
33685796c8dcSSimon Schubert 	      more than 2Gb of debug info and so will not need 64-bit
33695796c8dcSSimon Schubert 	      offsets,
33705796c8dcSSimon Schubert 	 and
33715796c8dcSSimon Schubert 	   b) if they do use 64-bit offsets but they are not using
33725796c8dcSSimon Schubert 	      the size hints that are tested for above then they are
33735796c8dcSSimon Schubert 	      not conforming to the DWARF3 standard anyway.  */
33745796c8dcSSimon Schubert       else if (addr_size == 8)
33755796c8dcSSimon Schubert 	{
33765796c8dcSSimon Schubert 	  offset_size = 4;
33775796c8dcSSimon Schubert 	  stash->info_ptr += 4;
33785796c8dcSSimon Schubert 	}
33795796c8dcSSimon Schubert       else
33805796c8dcSSimon Schubert 	stash->info_ptr += 4;
33815796c8dcSSimon Schubert 
33825796c8dcSSimon Schubert       if (length > 0)
33835796c8dcSSimon Schubert 	{
33845796c8dcSSimon Schubert 	  each = parse_comp_unit (stash, length, info_ptr_unit,
33855796c8dcSSimon Schubert 				  offset_size);
33865796c8dcSSimon Schubert 	  if (!each)
33875796c8dcSSimon Schubert 	    /* The dwarf information is damaged, don't trust it any
33885796c8dcSSimon Schubert 	       more.  */
33895796c8dcSSimon Schubert 	    break;
33905796c8dcSSimon Schubert 	  stash->info_ptr += length;
33915796c8dcSSimon Schubert 
33925796c8dcSSimon Schubert 	  if (stash->all_comp_units)
33935796c8dcSSimon Schubert 	    stash->all_comp_units->prev_unit = each;
33945796c8dcSSimon Schubert 	  else
33955796c8dcSSimon Schubert 	    stash->last_comp_unit = each;
33965796c8dcSSimon Schubert 
33975796c8dcSSimon Schubert 	  each->next_unit = stash->all_comp_units;
33985796c8dcSSimon Schubert 	  stash->all_comp_units = each;
33995796c8dcSSimon Schubert 
34005796c8dcSSimon Schubert 	  /* DW_AT_low_pc and DW_AT_high_pc are optional for
34015796c8dcSSimon Schubert 	     compilation units.  If we don't have them (i.e.,
34025796c8dcSSimon Schubert 	     unit->high == 0), we need to consult the line info table
34035796c8dcSSimon Schubert 	     to see if a compilation unit contains the given
34045796c8dcSSimon Schubert 	     address.  */
34055796c8dcSSimon Schubert 	  if (do_line)
34065796c8dcSSimon Schubert 	    found = (((symbol->flags & BSF_FUNCTION) == 0
34075796c8dcSSimon Schubert 		      || each->arange.high == 0
34085796c8dcSSimon Schubert 		      || comp_unit_contains_address (each, addr))
34095796c8dcSSimon Schubert 		     && comp_unit_find_line (each, symbol, addr,
34105796c8dcSSimon Schubert 					     filename_ptr,
34115796c8dcSSimon Schubert 					     linenumber_ptr,
34125796c8dcSSimon Schubert 					     stash));
34135796c8dcSSimon Schubert 	  else
34145796c8dcSSimon Schubert 	    found = ((each->arange.high == 0
34155796c8dcSSimon Schubert 		      || comp_unit_contains_address (each, addr))
34165796c8dcSSimon Schubert 		     && comp_unit_find_nearest_line (each, addr,
34175796c8dcSSimon Schubert 						     filename_ptr,
34185796c8dcSSimon Schubert 						     functionname_ptr,
34195796c8dcSSimon Schubert 						     linenumber_ptr,
34205796c8dcSSimon Schubert 						     stash));
34215796c8dcSSimon Schubert 
34225796c8dcSSimon Schubert 	  if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
34235796c8dcSSimon Schubert 	      == stash->sec->size)
34245796c8dcSSimon Schubert 	    {
34255796c8dcSSimon Schubert 	      stash->sec = find_debug_info (stash->bfd_ptr, stash->sec);
34265796c8dcSSimon Schubert 	      stash->sec_info_ptr = stash->info_ptr;
34275796c8dcSSimon Schubert 	    }
34285796c8dcSSimon Schubert 
34295796c8dcSSimon Schubert 	  if (found)
34305796c8dcSSimon Schubert 	    goto done;
34315796c8dcSSimon Schubert 	}
34325796c8dcSSimon Schubert     }
34335796c8dcSSimon Schubert 
34345796c8dcSSimon Schubert done:
34355796c8dcSSimon Schubert   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
34365796c8dcSSimon Schubert     unset_sections (stash);
34375796c8dcSSimon Schubert 
34385796c8dcSSimon Schubert   return found;
34395796c8dcSSimon Schubert }
34405796c8dcSSimon Schubert 
34415796c8dcSSimon Schubert /* The DWARF2 version of find_nearest_line.
34425796c8dcSSimon Schubert    Return TRUE if the line is found without error.  */
34435796c8dcSSimon Schubert 
34445796c8dcSSimon Schubert bfd_boolean
34455796c8dcSSimon Schubert _bfd_dwarf2_find_nearest_line (bfd *abfd,
34465796c8dcSSimon Schubert 			       asection *section,
34475796c8dcSSimon Schubert 			       asymbol **symbols,
34485796c8dcSSimon Schubert 			       bfd_vma offset,
34495796c8dcSSimon Schubert 			       const char **filename_ptr,
34505796c8dcSSimon Schubert 			       const char **functionname_ptr,
34515796c8dcSSimon Schubert 			       unsigned int *linenumber_ptr,
34525796c8dcSSimon Schubert 			       unsigned int addr_size,
34535796c8dcSSimon Schubert 			       void **pinfo)
34545796c8dcSSimon Schubert {
34555796c8dcSSimon Schubert   return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
34565796c8dcSSimon Schubert 		    functionname_ptr, linenumber_ptr, addr_size,
34575796c8dcSSimon Schubert 		    pinfo);
34585796c8dcSSimon Schubert }
34595796c8dcSSimon Schubert 
34605796c8dcSSimon Schubert /* The DWARF2 version of find_line.
34615796c8dcSSimon Schubert    Return TRUE if the line is found without error.  */
34625796c8dcSSimon Schubert 
34635796c8dcSSimon Schubert bfd_boolean
34645796c8dcSSimon Schubert _bfd_dwarf2_find_line (bfd *abfd,
34655796c8dcSSimon Schubert 		       asymbol **symbols,
34665796c8dcSSimon Schubert 		       asymbol *symbol,
34675796c8dcSSimon Schubert 		       const char **filename_ptr,
34685796c8dcSSimon Schubert 		       unsigned int *linenumber_ptr,
34695796c8dcSSimon Schubert 		       unsigned int addr_size,
34705796c8dcSSimon Schubert 		       void **pinfo)
34715796c8dcSSimon Schubert {
34725796c8dcSSimon Schubert   return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
34735796c8dcSSimon Schubert 		    NULL, linenumber_ptr, addr_size,
34745796c8dcSSimon Schubert 		    pinfo);
34755796c8dcSSimon Schubert }
34765796c8dcSSimon Schubert 
34775796c8dcSSimon Schubert bfd_boolean
34785796c8dcSSimon Schubert _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
34795796c8dcSSimon Schubert 			       const char **filename_ptr,
34805796c8dcSSimon Schubert 			       const char **functionname_ptr,
34815796c8dcSSimon Schubert 			       unsigned int *linenumber_ptr,
34825796c8dcSSimon Schubert 			       void **pinfo)
34835796c8dcSSimon Schubert {
34845796c8dcSSimon Schubert   struct dwarf2_debug *stash;
34855796c8dcSSimon Schubert 
34865796c8dcSSimon Schubert   stash = (struct dwarf2_debug *) *pinfo;
34875796c8dcSSimon Schubert   if (stash)
34885796c8dcSSimon Schubert     {
34895796c8dcSSimon Schubert       struct funcinfo *func = stash->inliner_chain;
34905796c8dcSSimon Schubert 
34915796c8dcSSimon Schubert       if (func && func->caller_func)
34925796c8dcSSimon Schubert 	{
34935796c8dcSSimon Schubert 	  *filename_ptr = func->caller_file;
34945796c8dcSSimon Schubert 	  *functionname_ptr = func->caller_func->name;
34955796c8dcSSimon Schubert 	  *linenumber_ptr = func->caller_line;
34965796c8dcSSimon Schubert 	  stash->inliner_chain = func->caller_func;
34975796c8dcSSimon Schubert 	  return TRUE;
34985796c8dcSSimon Schubert 	}
34995796c8dcSSimon Schubert     }
35005796c8dcSSimon Schubert 
35015796c8dcSSimon Schubert   return FALSE;
35025796c8dcSSimon Schubert }
35035796c8dcSSimon Schubert 
35045796c8dcSSimon Schubert void
35055796c8dcSSimon Schubert _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
35065796c8dcSSimon Schubert {
35075796c8dcSSimon Schubert   struct comp_unit *each;
35085796c8dcSSimon Schubert   struct dwarf2_debug *stash;
35095796c8dcSSimon Schubert 
35105796c8dcSSimon Schubert   if (abfd == NULL || elf_tdata (abfd) == NULL)
35115796c8dcSSimon Schubert     return;
35125796c8dcSSimon Schubert 
35135796c8dcSSimon Schubert   stash = (struct dwarf2_debug *) elf_tdata (abfd)->dwarf2_find_line_info;
35145796c8dcSSimon Schubert 
35155796c8dcSSimon Schubert   if (stash == NULL)
35165796c8dcSSimon Schubert     return;
35175796c8dcSSimon Schubert 
35185796c8dcSSimon Schubert   for (each = stash->all_comp_units; each; each = each->next_unit)
35195796c8dcSSimon Schubert     {
35205796c8dcSSimon Schubert       struct abbrev_info **abbrevs = each->abbrevs;
35215796c8dcSSimon Schubert       struct funcinfo *function_table = each->function_table;
35225796c8dcSSimon Schubert       struct varinfo *variable_table = each->variable_table;
35235796c8dcSSimon Schubert       size_t i;
35245796c8dcSSimon Schubert 
35255796c8dcSSimon Schubert       for (i = 0; i < ABBREV_HASH_SIZE; i++)
35265796c8dcSSimon Schubert 	{
35275796c8dcSSimon Schubert 	  struct abbrev_info *abbrev = abbrevs[i];
35285796c8dcSSimon Schubert 
35295796c8dcSSimon Schubert 	  while (abbrev)
35305796c8dcSSimon Schubert 	    {
35315796c8dcSSimon Schubert 	      free (abbrev->attrs);
35325796c8dcSSimon Schubert 	      abbrev = abbrev->next;
35335796c8dcSSimon Schubert 	    }
35345796c8dcSSimon Schubert 	}
35355796c8dcSSimon Schubert 
35365796c8dcSSimon Schubert       if (each->line_table)
35375796c8dcSSimon Schubert 	{
35385796c8dcSSimon Schubert 	  free (each->line_table->dirs);
35395796c8dcSSimon Schubert 	  free (each->line_table->files);
35405796c8dcSSimon Schubert 	}
35415796c8dcSSimon Schubert 
35425796c8dcSSimon Schubert       while (function_table)
35435796c8dcSSimon Schubert 	{
35445796c8dcSSimon Schubert 	  if (function_table->file)
35455796c8dcSSimon Schubert 	    {
35465796c8dcSSimon Schubert 	      free (function_table->file);
35475796c8dcSSimon Schubert 	      function_table->file = NULL;
35485796c8dcSSimon Schubert 	    }
35495796c8dcSSimon Schubert 
35505796c8dcSSimon Schubert 	  if (function_table->caller_file)
35515796c8dcSSimon Schubert 	    {
35525796c8dcSSimon Schubert 	      free (function_table->caller_file);
35535796c8dcSSimon Schubert 	      function_table->caller_file = NULL;
35545796c8dcSSimon Schubert 	    }
35555796c8dcSSimon Schubert 	  function_table = function_table->prev_func;
35565796c8dcSSimon Schubert 	}
35575796c8dcSSimon Schubert 
35585796c8dcSSimon Schubert       while (variable_table)
35595796c8dcSSimon Schubert 	{
35605796c8dcSSimon Schubert 	  if (variable_table->file)
35615796c8dcSSimon Schubert 	    {
35625796c8dcSSimon Schubert 	      free (variable_table->file);
35635796c8dcSSimon Schubert 	      variable_table->file = NULL;
35645796c8dcSSimon Schubert 	    }
35655796c8dcSSimon Schubert 
35665796c8dcSSimon Schubert 	  variable_table = variable_table->prev_var;
35675796c8dcSSimon Schubert 	}
35685796c8dcSSimon Schubert     }
35695796c8dcSSimon Schubert 
35705796c8dcSSimon Schubert   if (stash->dwarf_abbrev_buffer)
35715796c8dcSSimon Schubert     free (stash->dwarf_abbrev_buffer);
35725796c8dcSSimon Schubert   if (stash->dwarf_line_buffer)
35735796c8dcSSimon Schubert     free (stash->dwarf_line_buffer);
35745796c8dcSSimon Schubert   if (stash->dwarf_str_buffer)
35755796c8dcSSimon Schubert     free (stash->dwarf_str_buffer);
35765796c8dcSSimon Schubert   if (stash->dwarf_ranges_buffer)
35775796c8dcSSimon Schubert     free (stash->dwarf_ranges_buffer);
35785796c8dcSSimon Schubert   if (stash->info_ptr_memory)
35795796c8dcSSimon Schubert     free (stash->info_ptr_memory);
35805796c8dcSSimon Schubert }
3581