xref: /dflybsd-src/contrib/gdb-7/bfd/dwarf2.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
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 
93a45ae5f8SJohn Marino   /* Names of the debug sections.  */
94a45ae5f8SJohn Marino   const struct dwarf_debug_section *debug_sections;
95a45ae5f8SJohn Marino 
965796c8dcSSimon Schubert   /* The next unread compilation unit within the .debug_info section.
975796c8dcSSimon Schubert      Zero indicates that the .debug_info section has not been loaded
985796c8dcSSimon Schubert      into a buffer yet.  */
995796c8dcSSimon Schubert   bfd_byte *info_ptr;
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert   /* Pointer to the end of the .debug_info section memory buffer.  */
1025796c8dcSSimon Schubert   bfd_byte *info_ptr_end;
1035796c8dcSSimon Schubert 
1045796c8dcSSimon Schubert   /* Pointer to the bfd, section and address of the beginning of the
1055796c8dcSSimon Schubert      section.  The bfd might be different than expected because of
1065796c8dcSSimon Schubert      gnu_debuglink sections.  */
1075796c8dcSSimon Schubert   bfd *bfd_ptr;
1085796c8dcSSimon Schubert   asection *sec;
1095796c8dcSSimon Schubert   bfd_byte *sec_info_ptr;
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert   /* A pointer to the memory block allocated for info_ptr.  Neither
1125796c8dcSSimon Schubert      info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
1135796c8dcSSimon Schubert      beginning of the malloc block.  This is used only to free the
1145796c8dcSSimon Schubert      memory later.  */
1155796c8dcSSimon Schubert   bfd_byte *info_ptr_memory;
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert   /* Pointer to the symbol table.  */
1185796c8dcSSimon Schubert   asymbol **syms;
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert   /* Pointer to the .debug_abbrev section loaded into memory.  */
1215796c8dcSSimon Schubert   bfd_byte *dwarf_abbrev_buffer;
1225796c8dcSSimon Schubert 
1235796c8dcSSimon Schubert   /* Length of the loaded .debug_abbrev section.  */
1245796c8dcSSimon Schubert   bfd_size_type dwarf_abbrev_size;
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert   /* Buffer for decode_line_info.  */
1275796c8dcSSimon Schubert   bfd_byte *dwarf_line_buffer;
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert   /* Length of the loaded .debug_line section.  */
1305796c8dcSSimon Schubert   bfd_size_type dwarf_line_size;
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert   /* Pointer to the .debug_str section loaded into memory.  */
1335796c8dcSSimon Schubert   bfd_byte *dwarf_str_buffer;
1345796c8dcSSimon Schubert 
1355796c8dcSSimon Schubert   /* Length of the loaded .debug_str section.  */
1365796c8dcSSimon Schubert   bfd_size_type dwarf_str_size;
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert   /* Pointer to the .debug_ranges section loaded into memory. */
1395796c8dcSSimon Schubert   bfd_byte *dwarf_ranges_buffer;
1405796c8dcSSimon Schubert 
1415796c8dcSSimon Schubert   /* Length of the loaded .debug_ranges section. */
1425796c8dcSSimon Schubert   bfd_size_type dwarf_ranges_size;
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert   /* If the most recent call to bfd_find_nearest_line was given an
1455796c8dcSSimon Schubert      address in an inlined function, preserve a pointer into the
1465796c8dcSSimon Schubert      calling chain for subsequent calls to bfd_find_inliner_info to
1475796c8dcSSimon Schubert      use. */
1485796c8dcSSimon Schubert   struct funcinfo *inliner_chain;
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert   /* Number of sections whose VMA we must adjust.  */
1515796c8dcSSimon Schubert   unsigned int adjusted_section_count;
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert   /* Array of sections with adjusted VMA.  */
1545796c8dcSSimon Schubert   struct adjusted_section *adjusted_sections;
1555796c8dcSSimon Schubert 
1565796c8dcSSimon Schubert   /* Number of times find_line is called.  This is used in
1575796c8dcSSimon Schubert      the heuristic for enabling the info hash tables.  */
1585796c8dcSSimon Schubert   int info_hash_count;
1595796c8dcSSimon Schubert 
1605796c8dcSSimon Schubert #define STASH_INFO_HASH_TRIGGER    100
1615796c8dcSSimon Schubert 
1625796c8dcSSimon Schubert   /* Hash table mapping symbol names to function infos.  */
1635796c8dcSSimon Schubert   struct info_hash_table *funcinfo_hash_table;
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert   /* Hash table mapping symbol names to variable infos.  */
1665796c8dcSSimon Schubert   struct info_hash_table *varinfo_hash_table;
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert   /* Head of comp_unit list in the last hash table update.  */
1695796c8dcSSimon Schubert   struct comp_unit *hash_units_head;
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert   /* Status of info hash.  */
1725796c8dcSSimon Schubert   int info_hash_status;
1735796c8dcSSimon Schubert #define STASH_INFO_HASH_OFF        0
1745796c8dcSSimon Schubert #define STASH_INFO_HASH_ON         1
1755796c8dcSSimon Schubert #define STASH_INFO_HASH_DISABLED   2
176*ef5ccd6cSJohn Marino 
177*ef5ccd6cSJohn Marino   /* True if we opened bfd_ptr.  */
178*ef5ccd6cSJohn Marino   bfd_boolean close_on_cleanup;
1795796c8dcSSimon Schubert };
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert struct arange
1825796c8dcSSimon Schubert {
1835796c8dcSSimon Schubert   struct arange *next;
1845796c8dcSSimon Schubert   bfd_vma low;
1855796c8dcSSimon Schubert   bfd_vma high;
1865796c8dcSSimon Schubert };
1875796c8dcSSimon Schubert 
1885796c8dcSSimon Schubert /* A minimal decoding of DWARF2 compilation units.  We only decode
1895796c8dcSSimon Schubert    what's needed to get to the line number information.  */
1905796c8dcSSimon Schubert 
1915796c8dcSSimon Schubert struct comp_unit
1925796c8dcSSimon Schubert {
1935796c8dcSSimon Schubert   /* Chain the previously read compilation units.  */
1945796c8dcSSimon Schubert   struct comp_unit *next_unit;
1955796c8dcSSimon Schubert 
1965796c8dcSSimon Schubert   /* Likewise, chain the compilation unit read after this one.
1975796c8dcSSimon Schubert      The comp units are stored in reversed reading order.  */
1985796c8dcSSimon Schubert   struct comp_unit *prev_unit;
1995796c8dcSSimon Schubert 
2005796c8dcSSimon Schubert   /* Keep the bfd convenient (for memory allocation).  */
2015796c8dcSSimon Schubert   bfd *abfd;
2025796c8dcSSimon Schubert 
2035796c8dcSSimon Schubert   /* The lowest and highest addresses contained in this compilation
2045796c8dcSSimon Schubert      unit as specified in the compilation unit header.  */
2055796c8dcSSimon Schubert   struct arange arange;
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert   /* The DW_AT_name attribute (for error messages).  */
2085796c8dcSSimon Schubert   char *name;
2095796c8dcSSimon Schubert 
2105796c8dcSSimon Schubert   /* The abbrev hash table.  */
2115796c8dcSSimon Schubert   struct abbrev_info **abbrevs;
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert   /* Note that an error was found by comp_unit_find_nearest_line.  */
2145796c8dcSSimon Schubert   int error;
2155796c8dcSSimon Schubert 
2165796c8dcSSimon Schubert   /* The DW_AT_comp_dir attribute.  */
2175796c8dcSSimon Schubert   char *comp_dir;
2185796c8dcSSimon Schubert 
2195796c8dcSSimon Schubert   /* TRUE if there is a line number table associated with this comp. unit.  */
2205796c8dcSSimon Schubert   int stmtlist;
2215796c8dcSSimon Schubert 
2225796c8dcSSimon Schubert   /* Pointer to the current comp_unit so that we can find a given entry
2235796c8dcSSimon Schubert      by its reference.  */
2245796c8dcSSimon Schubert   bfd_byte *info_ptr_unit;
2255796c8dcSSimon Schubert 
226cf7f2e2dSJohn Marino   /* Pointer to the start of the debug section, for DW_FORM_ref_addr.  */
227cf7f2e2dSJohn Marino   bfd_byte *sec_info_ptr;
228cf7f2e2dSJohn Marino 
2295796c8dcSSimon Schubert   /* The offset into .debug_line of the line number table.  */
2305796c8dcSSimon Schubert   unsigned long line_offset;
2315796c8dcSSimon Schubert 
2325796c8dcSSimon Schubert   /* Pointer to the first child die for the comp unit.  */
2335796c8dcSSimon Schubert   bfd_byte *first_child_die_ptr;
2345796c8dcSSimon Schubert 
2355796c8dcSSimon Schubert   /* The end of the comp unit.  */
2365796c8dcSSimon Schubert   bfd_byte *end_ptr;
2375796c8dcSSimon Schubert 
2385796c8dcSSimon Schubert   /* The decoded line number, NULL if not yet decoded.  */
2395796c8dcSSimon Schubert   struct line_info_table *line_table;
2405796c8dcSSimon Schubert 
2415796c8dcSSimon Schubert   /* A list of the functions found in this comp. unit.  */
2425796c8dcSSimon Schubert   struct funcinfo *function_table;
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert   /* A list of the variables found in this comp. unit.  */
2455796c8dcSSimon Schubert   struct varinfo *variable_table;
2465796c8dcSSimon Schubert 
2475796c8dcSSimon Schubert   /* Pointer to dwarf2_debug structure.  */
2485796c8dcSSimon Schubert   struct dwarf2_debug *stash;
2495796c8dcSSimon Schubert 
2505796c8dcSSimon Schubert   /* DWARF format version for this unit - from unit header.  */
2515796c8dcSSimon Schubert   int version;
2525796c8dcSSimon Schubert 
2535796c8dcSSimon Schubert   /* Address size for this unit - from unit header.  */
2545796c8dcSSimon Schubert   unsigned char addr_size;
2555796c8dcSSimon Schubert 
2565796c8dcSSimon Schubert   /* Offset size for this unit - from unit header.  */
2575796c8dcSSimon Schubert   unsigned char offset_size;
2585796c8dcSSimon Schubert 
2595796c8dcSSimon Schubert   /* Base address for this unit - from DW_AT_low_pc attribute of
2605796c8dcSSimon Schubert      DW_TAG_compile_unit DIE */
2615796c8dcSSimon Schubert   bfd_vma base_address;
2625796c8dcSSimon Schubert 
2635796c8dcSSimon Schubert   /* TRUE if symbols are cached in hash table for faster lookup by name.  */
2645796c8dcSSimon Schubert   bfd_boolean cached;
2655796c8dcSSimon Schubert };
2665796c8dcSSimon Schubert 
2675796c8dcSSimon Schubert /* This data structure holds the information of an abbrev.  */
2685796c8dcSSimon Schubert struct abbrev_info
2695796c8dcSSimon Schubert {
2705796c8dcSSimon Schubert   unsigned int number;		/* Number identifying abbrev.  */
2715796c8dcSSimon Schubert   enum dwarf_tag tag;		/* DWARF tag.  */
2725796c8dcSSimon Schubert   int has_children;		/* Boolean.  */
2735796c8dcSSimon Schubert   unsigned int num_attrs;	/* Number of attributes.  */
2745796c8dcSSimon Schubert   struct attr_abbrev *attrs;	/* An array of attribute descriptions.  */
2755796c8dcSSimon Schubert   struct abbrev_info *next;	/* Next in chain.  */
2765796c8dcSSimon Schubert };
2775796c8dcSSimon Schubert 
2785796c8dcSSimon Schubert struct attr_abbrev
2795796c8dcSSimon Schubert {
2805796c8dcSSimon Schubert   enum dwarf_attribute name;
2815796c8dcSSimon Schubert   enum dwarf_form form;
2825796c8dcSSimon Schubert };
2835796c8dcSSimon Schubert 
284c50c785cSJohn Marino /* Map of uncompressed DWARF debug section name to compressed one.  It
285c50c785cSJohn Marino    is terminated by NULL uncompressed_name.  */
286c50c785cSJohn Marino 
287a45ae5f8SJohn Marino const struct dwarf_debug_section dwarf_debug_sections[] =
288c50c785cSJohn Marino {
289c50c785cSJohn Marino   { ".debug_abbrev",		".zdebug_abbrev" },
290c50c785cSJohn Marino   { ".debug_aranges",		".zdebug_aranges" },
291c50c785cSJohn Marino   { ".debug_frame",		".zdebug_frame" },
292c50c785cSJohn Marino   { ".debug_info",		".zdebug_info" },
293c50c785cSJohn Marino   { ".debug_line",		".zdebug_line" },
294c50c785cSJohn Marino   { ".debug_loc",		".zdebug_loc" },
295c50c785cSJohn Marino   { ".debug_macinfo",		".zdebug_macinfo" },
296a45ae5f8SJohn Marino   { ".debug_macro",		".zdebug_macro" },
297c50c785cSJohn Marino   { ".debug_pubnames",		".zdebug_pubnames" },
298c50c785cSJohn Marino   { ".debug_pubtypes",		".zdebug_pubtypes" },
299c50c785cSJohn Marino   { ".debug_ranges",		".zdebug_ranges" },
300c50c785cSJohn Marino   { ".debug_static_func",	".zdebug_static_func" },
301c50c785cSJohn Marino   { ".debug_static_vars",	".zdebug_static_vars" },
302c50c785cSJohn Marino   { ".debug_str",		".zdebug_str", },
303c50c785cSJohn Marino   { ".debug_types",		".zdebug_types" },
304c50c785cSJohn Marino   /* GNU DWARF 1 extensions */
305c50c785cSJohn Marino   { ".debug_sfnames",		".zdebug_sfnames" },
306c50c785cSJohn Marino   { ".debug_srcinfo",		".zebug_srcinfo" },
307c50c785cSJohn Marino   /* SGI/MIPS DWARF 2 extensions */
308c50c785cSJohn Marino   { ".debug_funcnames",		".zdebug_funcnames" },
309c50c785cSJohn Marino   { ".debug_typenames",		".zdebug_typenames" },
310c50c785cSJohn Marino   { ".debug_varnames",		".zdebug_varnames" },
311c50c785cSJohn Marino   { ".debug_weaknames",		".zdebug_weaknames" },
312c50c785cSJohn Marino   { NULL,			NULL },
313c50c785cSJohn Marino };
314c50c785cSJohn Marino 
315c50c785cSJohn Marino enum dwarf_debug_section_enum
316c50c785cSJohn Marino {
317c50c785cSJohn Marino   debug_abbrev = 0,
318c50c785cSJohn Marino   debug_aranges,
319c50c785cSJohn Marino   debug_frame,
320c50c785cSJohn Marino   debug_info,
321c50c785cSJohn Marino   debug_line,
322c50c785cSJohn Marino   debug_loc,
323c50c785cSJohn Marino   debug_macinfo,
324a45ae5f8SJohn Marino   debug_macro,
325c50c785cSJohn Marino   debug_pubnames,
326c50c785cSJohn Marino   debug_pubtypes,
327c50c785cSJohn Marino   debug_ranges,
328c50c785cSJohn Marino   debug_static_func,
329c50c785cSJohn Marino   debug_static_vars,
330c50c785cSJohn Marino   debug_str,
331c50c785cSJohn Marino   debug_types,
332c50c785cSJohn Marino   debug_sfnames,
333c50c785cSJohn Marino   debug_srcinfo,
334c50c785cSJohn Marino   debug_funcnames,
335c50c785cSJohn Marino   debug_typenames,
336c50c785cSJohn Marino   debug_varnames,
337c50c785cSJohn Marino   debug_weaknames
338c50c785cSJohn Marino };
339c50c785cSJohn Marino 
3405796c8dcSSimon Schubert #ifndef ABBREV_HASH_SIZE
3415796c8dcSSimon Schubert #define ABBREV_HASH_SIZE 121
3425796c8dcSSimon Schubert #endif
3435796c8dcSSimon Schubert #ifndef ATTR_ALLOC_CHUNK
3445796c8dcSSimon Schubert #define ATTR_ALLOC_CHUNK 4
3455796c8dcSSimon Schubert #endif
3465796c8dcSSimon Schubert 
3475796c8dcSSimon Schubert /* Variable and function hash tables.  This is used to speed up look-up
3485796c8dcSSimon Schubert    in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
3495796c8dcSSimon Schubert    In order to share code between variable and function infos, we use
3505796c8dcSSimon Schubert    a list of untyped pointer for all variable/function info associated with
3515796c8dcSSimon Schubert    a symbol.  We waste a bit of memory for list with one node but that
3525796c8dcSSimon Schubert    simplifies the code.  */
3535796c8dcSSimon Schubert 
3545796c8dcSSimon Schubert struct info_list_node
3555796c8dcSSimon Schubert {
3565796c8dcSSimon Schubert   struct info_list_node *next;
3575796c8dcSSimon Schubert   void *info;
3585796c8dcSSimon Schubert };
3595796c8dcSSimon Schubert 
3605796c8dcSSimon Schubert /* Info hash entry.  */
3615796c8dcSSimon Schubert struct info_hash_entry
3625796c8dcSSimon Schubert {
3635796c8dcSSimon Schubert   struct bfd_hash_entry root;
3645796c8dcSSimon Schubert   struct info_list_node *head;
3655796c8dcSSimon Schubert };
3665796c8dcSSimon Schubert 
3675796c8dcSSimon Schubert struct info_hash_table
3685796c8dcSSimon Schubert {
3695796c8dcSSimon Schubert   struct bfd_hash_table base;
3705796c8dcSSimon Schubert };
3715796c8dcSSimon Schubert 
3725796c8dcSSimon Schubert /* Function to create a new entry in info hash table. */
3735796c8dcSSimon Schubert 
3745796c8dcSSimon Schubert static struct bfd_hash_entry *
info_hash_table_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)3755796c8dcSSimon Schubert info_hash_table_newfunc (struct bfd_hash_entry *entry,
3765796c8dcSSimon Schubert 			 struct bfd_hash_table *table,
3775796c8dcSSimon Schubert 			 const char *string)
3785796c8dcSSimon Schubert {
3795796c8dcSSimon Schubert   struct info_hash_entry *ret = (struct info_hash_entry *) entry;
3805796c8dcSSimon Schubert 
3815796c8dcSSimon Schubert   /* Allocate the structure if it has not already been allocated by a
3825796c8dcSSimon Schubert      derived class.  */
3835796c8dcSSimon Schubert   if (ret == NULL)
3845796c8dcSSimon Schubert     {
3855796c8dcSSimon Schubert       ret = (struct info_hash_entry *) bfd_hash_allocate (table,
3865796c8dcSSimon Schubert                                                           sizeof (* ret));
3875796c8dcSSimon Schubert       if (ret == NULL)
3885796c8dcSSimon Schubert 	return NULL;
3895796c8dcSSimon Schubert     }
3905796c8dcSSimon Schubert 
3915796c8dcSSimon Schubert   /* Call the allocation method of the base class.  */
3925796c8dcSSimon Schubert   ret = ((struct info_hash_entry *)
3935796c8dcSSimon Schubert 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
3945796c8dcSSimon Schubert 
3955796c8dcSSimon Schubert   /* Initialize the local fields here.  */
3965796c8dcSSimon Schubert   if (ret)
3975796c8dcSSimon Schubert     ret->head = NULL;
3985796c8dcSSimon Schubert 
3995796c8dcSSimon Schubert   return (struct bfd_hash_entry *) ret;
4005796c8dcSSimon Schubert }
4015796c8dcSSimon Schubert 
4025796c8dcSSimon Schubert /* Function to create a new info hash table.  It returns a pointer to the
4035796c8dcSSimon Schubert    newly created table or NULL if there is any error.  We need abfd
4045796c8dcSSimon Schubert    solely for memory allocation.  */
4055796c8dcSSimon Schubert 
4065796c8dcSSimon Schubert static struct info_hash_table *
create_info_hash_table(bfd * abfd)4075796c8dcSSimon Schubert create_info_hash_table (bfd *abfd)
4085796c8dcSSimon Schubert {
4095796c8dcSSimon Schubert   struct info_hash_table *hash_table;
4105796c8dcSSimon Schubert 
411*ef5ccd6cSJohn Marino   hash_table = ((struct info_hash_table *)
412*ef5ccd6cSJohn Marino 		bfd_alloc (abfd, sizeof (struct info_hash_table)));
4135796c8dcSSimon Schubert   if (!hash_table)
4145796c8dcSSimon Schubert     return hash_table;
4155796c8dcSSimon Schubert 
4165796c8dcSSimon Schubert   if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
4175796c8dcSSimon Schubert 			    sizeof (struct info_hash_entry)))
4185796c8dcSSimon Schubert     {
4195796c8dcSSimon Schubert       bfd_release (abfd, hash_table);
4205796c8dcSSimon Schubert       return NULL;
4215796c8dcSSimon Schubert     }
4225796c8dcSSimon Schubert 
4235796c8dcSSimon Schubert   return hash_table;
4245796c8dcSSimon Schubert }
4255796c8dcSSimon Schubert 
4265796c8dcSSimon Schubert /* Insert an info entry into an info hash table.  We do not check of
4275796c8dcSSimon Schubert    duplicate entries.  Also, the caller need to guarantee that the
4285796c8dcSSimon Schubert    right type of info in inserted as info is passed as a void* pointer.
4295796c8dcSSimon Schubert    This function returns true if there is no error.  */
4305796c8dcSSimon Schubert 
4315796c8dcSSimon Schubert static bfd_boolean
insert_info_hash_table(struct info_hash_table * hash_table,const char * key,void * info,bfd_boolean copy_p)4325796c8dcSSimon Schubert insert_info_hash_table (struct info_hash_table *hash_table,
4335796c8dcSSimon Schubert 			const char *key,
4345796c8dcSSimon Schubert 			void *info,
4355796c8dcSSimon Schubert 			bfd_boolean copy_p)
4365796c8dcSSimon Schubert {
4375796c8dcSSimon Schubert   struct info_hash_entry *entry;
4385796c8dcSSimon Schubert   struct info_list_node *node;
4395796c8dcSSimon Schubert 
4405796c8dcSSimon Schubert   entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
4415796c8dcSSimon Schubert 						     key, TRUE, copy_p);
4425796c8dcSSimon Schubert   if (!entry)
4435796c8dcSSimon Schubert     return FALSE;
4445796c8dcSSimon Schubert 
4455796c8dcSSimon Schubert   node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
4465796c8dcSSimon Schubert                                                       sizeof (*node));
4475796c8dcSSimon Schubert   if (!node)
4485796c8dcSSimon Schubert     return FALSE;
4495796c8dcSSimon Schubert 
4505796c8dcSSimon Schubert   node->info = info;
4515796c8dcSSimon Schubert   node->next = entry->head;
4525796c8dcSSimon Schubert   entry->head = node;
4535796c8dcSSimon Schubert 
4545796c8dcSSimon Schubert   return TRUE;
4555796c8dcSSimon Schubert }
4565796c8dcSSimon Schubert 
4575796c8dcSSimon Schubert /* Look up an info entry list from an info hash table.  Return NULL
4585796c8dcSSimon Schubert    if there is none. */
4595796c8dcSSimon Schubert 
4605796c8dcSSimon Schubert static struct info_list_node *
lookup_info_hash_table(struct info_hash_table * hash_table,const char * key)4615796c8dcSSimon Schubert lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
4625796c8dcSSimon Schubert {
4635796c8dcSSimon Schubert   struct info_hash_entry *entry;
4645796c8dcSSimon Schubert 
4655796c8dcSSimon Schubert   entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
4665796c8dcSSimon Schubert 						     FALSE, FALSE);
4675796c8dcSSimon Schubert   return entry ? entry->head : NULL;
4685796c8dcSSimon Schubert }
4695796c8dcSSimon Schubert 
4705796c8dcSSimon Schubert /* Read a section into its appropriate place in the dwarf2_debug
4715796c8dcSSimon Schubert    struct (indicated by SECTION_BUFFER and SECTION_SIZE).  If SYMS is
4725796c8dcSSimon Schubert    not NULL, use bfd_simple_get_relocated_section_contents to read the
4735796c8dcSSimon Schubert    section contents, otherwise use bfd_get_section_contents.  Fail if
4745796c8dcSSimon Schubert    the located section does not contain at least OFFSET bytes.  */
4755796c8dcSSimon Schubert 
4765796c8dcSSimon Schubert static bfd_boolean
read_section(bfd * abfd,const struct dwarf_debug_section * sec,asymbol ** syms,bfd_uint64_t offset,bfd_byte ** section_buffer,bfd_size_type * section_size)4775796c8dcSSimon Schubert read_section (bfd *           abfd,
478a45ae5f8SJohn Marino 	      const struct dwarf_debug_section *sec,
4795796c8dcSSimon Schubert 	      asymbol **      syms,
4805796c8dcSSimon Schubert 	      bfd_uint64_t    offset,
4815796c8dcSSimon Schubert 	      bfd_byte **     section_buffer,
4825796c8dcSSimon Schubert 	      bfd_size_type * section_size)
4835796c8dcSSimon Schubert {
4845796c8dcSSimon Schubert   asection *msec;
485a45ae5f8SJohn Marino   const char *section_name = sec->uncompressed_name;
4865796c8dcSSimon Schubert 
4875796c8dcSSimon Schubert   /* read_section is a noop if the section has already been read.  */
4885796c8dcSSimon Schubert   if (!*section_buffer)
4895796c8dcSSimon Schubert     {
4905796c8dcSSimon Schubert       msec = bfd_get_section_by_name (abfd, section_name);
491c50c785cSJohn Marino       if (! msec)
4925796c8dcSSimon Schubert 	{
493a45ae5f8SJohn Marino 	  section_name = sec->compressed_name;
494a45ae5f8SJohn Marino           if (section_name != NULL)
495c50c785cSJohn Marino             msec = bfd_get_section_by_name (abfd, section_name);
4965796c8dcSSimon Schubert 	}
4975796c8dcSSimon Schubert       if (! msec)
4985796c8dcSSimon Schubert 	{
499a45ae5f8SJohn Marino 	  (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."),
500a45ae5f8SJohn Marino                                  sec->uncompressed_name);
5015796c8dcSSimon Schubert 	  bfd_set_error (bfd_error_bad_value);
5025796c8dcSSimon Schubert 	  return FALSE;
5035796c8dcSSimon Schubert 	}
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert       *section_size = msec->rawsize ? msec->rawsize : msec->size;
506c50c785cSJohn Marino       if (syms)
507c50c785cSJohn Marino 	{
508c50c785cSJohn Marino 	  *section_buffer
509c50c785cSJohn Marino 	    = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
510c50c785cSJohn Marino 	  if (! *section_buffer)
5115796c8dcSSimon Schubert 	    return FALSE;
5125796c8dcSSimon Schubert 	}
513c50c785cSJohn Marino       else
514c50c785cSJohn Marino 	{
515c50c785cSJohn Marino 	  *section_buffer = (bfd_byte *) bfd_malloc (*section_size);
516c50c785cSJohn Marino 	  if (! *section_buffer)
517c50c785cSJohn Marino 	    return FALSE;
518c50c785cSJohn Marino 	  if (! bfd_get_section_contents (abfd, msec, *section_buffer,
519c50c785cSJohn Marino 					  0, *section_size))
520c50c785cSJohn Marino 	    return FALSE;
521c50c785cSJohn Marino 	}
522c50c785cSJohn Marino     }
5235796c8dcSSimon Schubert 
5245796c8dcSSimon Schubert   /* It is possible to get a bad value for the offset into the section
5255796c8dcSSimon Schubert      that the client wants.  Validate it here to avoid trouble later.  */
5265796c8dcSSimon Schubert   if (offset != 0 && offset >= *section_size)
5275796c8dcSSimon Schubert     {
528*ef5ccd6cSJohn Marino       (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu)"
529*ef5ccd6cSJohn Marino 			       " greater than or equal to %s size (%lu)."),
5305796c8dcSSimon Schubert 			     (long) offset, section_name, *section_size);
5315796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
5325796c8dcSSimon Schubert       return FALSE;
5335796c8dcSSimon Schubert     }
5345796c8dcSSimon Schubert 
5355796c8dcSSimon Schubert   return TRUE;
5365796c8dcSSimon Schubert }
5375796c8dcSSimon Schubert 
5385796c8dcSSimon Schubert /* VERBATIM
5395796c8dcSSimon Schubert    The following function up to the END VERBATIM mark are
5405796c8dcSSimon Schubert    copied directly from dwarf2read.c.  */
5415796c8dcSSimon Schubert 
5425796c8dcSSimon Schubert /* Read dwarf information from a buffer.  */
5435796c8dcSSimon Schubert 
5445796c8dcSSimon Schubert static unsigned int
read_1_byte(bfd * abfd ATTRIBUTE_UNUSED,bfd_byte * buf)5455796c8dcSSimon Schubert read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
5465796c8dcSSimon Schubert {
5475796c8dcSSimon Schubert   return bfd_get_8 (abfd, buf);
5485796c8dcSSimon Schubert }
5495796c8dcSSimon Schubert 
5505796c8dcSSimon Schubert static int
read_1_signed_byte(bfd * abfd ATTRIBUTE_UNUSED,bfd_byte * buf)5515796c8dcSSimon Schubert read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
5525796c8dcSSimon Schubert {
5535796c8dcSSimon Schubert   return bfd_get_signed_8 (abfd, buf);
5545796c8dcSSimon Schubert }
5555796c8dcSSimon Schubert 
5565796c8dcSSimon Schubert static unsigned int
read_2_bytes(bfd * abfd,bfd_byte * buf)5575796c8dcSSimon Schubert read_2_bytes (bfd *abfd, bfd_byte *buf)
5585796c8dcSSimon Schubert {
5595796c8dcSSimon Schubert   return bfd_get_16 (abfd, buf);
5605796c8dcSSimon Schubert }
5615796c8dcSSimon Schubert 
5625796c8dcSSimon Schubert static unsigned int
read_4_bytes(bfd * abfd,bfd_byte * buf)5635796c8dcSSimon Schubert read_4_bytes (bfd *abfd, bfd_byte *buf)
5645796c8dcSSimon Schubert {
5655796c8dcSSimon Schubert   return bfd_get_32 (abfd, buf);
5665796c8dcSSimon Schubert }
5675796c8dcSSimon Schubert 
5685796c8dcSSimon Schubert static bfd_uint64_t
read_8_bytes(bfd * abfd,bfd_byte * buf)5695796c8dcSSimon Schubert read_8_bytes (bfd *abfd, bfd_byte *buf)
5705796c8dcSSimon Schubert {
5715796c8dcSSimon Schubert   return bfd_get_64 (abfd, buf);
5725796c8dcSSimon Schubert }
5735796c8dcSSimon Schubert 
5745796c8dcSSimon Schubert static bfd_byte *
read_n_bytes(bfd * abfd ATTRIBUTE_UNUSED,bfd_byte * buf,unsigned int size ATTRIBUTE_UNUSED)5755796c8dcSSimon Schubert read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
5765796c8dcSSimon Schubert 	      bfd_byte *buf,
5775796c8dcSSimon Schubert 	      unsigned int size ATTRIBUTE_UNUSED)
5785796c8dcSSimon Schubert {
5795796c8dcSSimon Schubert   return buf;
5805796c8dcSSimon Schubert }
5815796c8dcSSimon Schubert 
5825796c8dcSSimon Schubert static char *
read_string(bfd * abfd ATTRIBUTE_UNUSED,bfd_byte * buf,unsigned int * bytes_read_ptr)5835796c8dcSSimon Schubert read_string (bfd *abfd ATTRIBUTE_UNUSED,
5845796c8dcSSimon Schubert 	     bfd_byte *buf,
5855796c8dcSSimon Schubert 	     unsigned int *bytes_read_ptr)
5865796c8dcSSimon Schubert {
5875796c8dcSSimon Schubert   /* Return a pointer to the embedded string.  */
5885796c8dcSSimon Schubert   char *str = (char *) buf;
5895796c8dcSSimon Schubert 
5905796c8dcSSimon Schubert   if (*str == '\0')
5915796c8dcSSimon Schubert     {
5925796c8dcSSimon Schubert       *bytes_read_ptr = 1;
5935796c8dcSSimon Schubert       return NULL;
5945796c8dcSSimon Schubert     }
5955796c8dcSSimon Schubert 
5965796c8dcSSimon Schubert   *bytes_read_ptr = strlen (str) + 1;
5975796c8dcSSimon Schubert   return str;
5985796c8dcSSimon Schubert }
5995796c8dcSSimon Schubert 
6005796c8dcSSimon Schubert /* END VERBATIM */
6015796c8dcSSimon Schubert 
6025796c8dcSSimon Schubert static char *
read_indirect_string(struct comp_unit * unit,bfd_byte * buf,unsigned int * bytes_read_ptr)6035796c8dcSSimon Schubert read_indirect_string (struct comp_unit * unit,
6045796c8dcSSimon Schubert 		      bfd_byte *         buf,
6055796c8dcSSimon Schubert 		      unsigned int *     bytes_read_ptr)
6065796c8dcSSimon Schubert {
6075796c8dcSSimon Schubert   bfd_uint64_t offset;
6085796c8dcSSimon Schubert   struct dwarf2_debug *stash = unit->stash;
6095796c8dcSSimon Schubert   char *str;
6105796c8dcSSimon Schubert 
6115796c8dcSSimon Schubert   if (unit->offset_size == 4)
6125796c8dcSSimon Schubert     offset = read_4_bytes (unit->abfd, buf);
6135796c8dcSSimon Schubert   else
6145796c8dcSSimon Schubert     offset = read_8_bytes (unit->abfd, buf);
6155796c8dcSSimon Schubert 
6165796c8dcSSimon Schubert   *bytes_read_ptr = unit->offset_size;
6175796c8dcSSimon Schubert 
618a45ae5f8SJohn Marino   if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
619a45ae5f8SJohn Marino                       stash->syms, offset,
6205796c8dcSSimon Schubert 		      &stash->dwarf_str_buffer, &stash->dwarf_str_size))
6215796c8dcSSimon Schubert     return NULL;
6225796c8dcSSimon Schubert 
6235796c8dcSSimon Schubert   str = (char *) stash->dwarf_str_buffer + offset;
6245796c8dcSSimon Schubert   if (*str == '\0')
6255796c8dcSSimon Schubert     return NULL;
6265796c8dcSSimon Schubert   return str;
6275796c8dcSSimon Schubert }
6285796c8dcSSimon Schubert 
6295796c8dcSSimon Schubert static bfd_uint64_t
read_address(struct comp_unit * unit,bfd_byte * buf)6305796c8dcSSimon Schubert read_address (struct comp_unit *unit, bfd_byte *buf)
6315796c8dcSSimon Schubert {
6325796c8dcSSimon Schubert   int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
6335796c8dcSSimon Schubert 
6345796c8dcSSimon Schubert   if (signed_vma)
6355796c8dcSSimon Schubert     {
6365796c8dcSSimon Schubert       switch (unit->addr_size)
6375796c8dcSSimon Schubert 	{
6385796c8dcSSimon Schubert 	case 8:
6395796c8dcSSimon Schubert 	  return bfd_get_signed_64 (unit->abfd, buf);
6405796c8dcSSimon Schubert 	case 4:
6415796c8dcSSimon Schubert 	  return bfd_get_signed_32 (unit->abfd, buf);
6425796c8dcSSimon Schubert 	case 2:
6435796c8dcSSimon Schubert 	  return bfd_get_signed_16 (unit->abfd, buf);
6445796c8dcSSimon Schubert 	default:
6455796c8dcSSimon Schubert 	  abort ();
6465796c8dcSSimon Schubert 	}
6475796c8dcSSimon Schubert     }
6485796c8dcSSimon Schubert   else
6495796c8dcSSimon Schubert     {
6505796c8dcSSimon Schubert       switch (unit->addr_size)
6515796c8dcSSimon Schubert 	{
6525796c8dcSSimon Schubert 	case 8:
6535796c8dcSSimon Schubert 	  return bfd_get_64 (unit->abfd, buf);
6545796c8dcSSimon Schubert 	case 4:
6555796c8dcSSimon Schubert 	  return bfd_get_32 (unit->abfd, buf);
6565796c8dcSSimon Schubert 	case 2:
6575796c8dcSSimon Schubert 	  return bfd_get_16 (unit->abfd, buf);
6585796c8dcSSimon Schubert 	default:
6595796c8dcSSimon Schubert 	  abort ();
6605796c8dcSSimon Schubert 	}
6615796c8dcSSimon Schubert     }
6625796c8dcSSimon Schubert }
6635796c8dcSSimon Schubert 
6645796c8dcSSimon Schubert /* Lookup an abbrev_info structure in the abbrev hash table.  */
6655796c8dcSSimon Schubert 
6665796c8dcSSimon Schubert static struct abbrev_info *
lookup_abbrev(unsigned int number,struct abbrev_info ** abbrevs)6675796c8dcSSimon Schubert lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
6685796c8dcSSimon Schubert {
6695796c8dcSSimon Schubert   unsigned int hash_number;
6705796c8dcSSimon Schubert   struct abbrev_info *abbrev;
6715796c8dcSSimon Schubert 
6725796c8dcSSimon Schubert   hash_number = number % ABBREV_HASH_SIZE;
6735796c8dcSSimon Schubert   abbrev = abbrevs[hash_number];
6745796c8dcSSimon Schubert 
6755796c8dcSSimon Schubert   while (abbrev)
6765796c8dcSSimon Schubert     {
6775796c8dcSSimon Schubert       if (abbrev->number == number)
6785796c8dcSSimon Schubert 	return abbrev;
6795796c8dcSSimon Schubert       else
6805796c8dcSSimon Schubert 	abbrev = abbrev->next;
6815796c8dcSSimon Schubert     }
6825796c8dcSSimon Schubert 
6835796c8dcSSimon Schubert   return NULL;
6845796c8dcSSimon Schubert }
6855796c8dcSSimon Schubert 
6865796c8dcSSimon Schubert /* In DWARF version 2, the description of the debugging information is
6875796c8dcSSimon Schubert    stored in a separate .debug_abbrev section.  Before we read any
6885796c8dcSSimon Schubert    dies from a section we read in all abbreviations and install them
6895796c8dcSSimon Schubert    in a hash table.  */
6905796c8dcSSimon Schubert 
6915796c8dcSSimon Schubert static struct abbrev_info**
read_abbrevs(bfd * abfd,bfd_uint64_t offset,struct dwarf2_debug * stash)6925796c8dcSSimon Schubert read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
6935796c8dcSSimon Schubert {
6945796c8dcSSimon Schubert   struct abbrev_info **abbrevs;
6955796c8dcSSimon Schubert   bfd_byte *abbrev_ptr;
6965796c8dcSSimon Schubert   struct abbrev_info *cur_abbrev;
6975796c8dcSSimon Schubert   unsigned int abbrev_number, bytes_read, abbrev_name;
6985796c8dcSSimon Schubert   unsigned int abbrev_form, hash_number;
6995796c8dcSSimon Schubert   bfd_size_type amt;
7005796c8dcSSimon Schubert 
701a45ae5f8SJohn Marino   if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
702a45ae5f8SJohn Marino                       stash->syms, offset,
7035796c8dcSSimon Schubert 		      &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
704cf7f2e2dSJohn Marino     return NULL;
7055796c8dcSSimon Schubert 
7065796c8dcSSimon Schubert   amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
7075796c8dcSSimon Schubert   abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
708cf7f2e2dSJohn Marino   if (abbrevs == NULL)
709cf7f2e2dSJohn Marino     return NULL;
7105796c8dcSSimon Schubert 
7115796c8dcSSimon Schubert   abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
7125796c8dcSSimon Schubert   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7135796c8dcSSimon Schubert   abbrev_ptr += bytes_read;
7145796c8dcSSimon Schubert 
7155796c8dcSSimon Schubert   /* Loop until we reach an abbrev number of 0.  */
7165796c8dcSSimon Schubert   while (abbrev_number)
7175796c8dcSSimon Schubert     {
7185796c8dcSSimon Schubert       amt = sizeof (struct abbrev_info);
7195796c8dcSSimon Schubert       cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
720cf7f2e2dSJohn Marino       if (cur_abbrev == NULL)
721cf7f2e2dSJohn Marino 	return NULL;
7225796c8dcSSimon Schubert 
7235796c8dcSSimon Schubert       /* Read in abbrev header.  */
7245796c8dcSSimon Schubert       cur_abbrev->number = abbrev_number;
7255796c8dcSSimon Schubert       cur_abbrev->tag = (enum dwarf_tag)
7265796c8dcSSimon Schubert 	read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7275796c8dcSSimon Schubert       abbrev_ptr += bytes_read;
7285796c8dcSSimon Schubert       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
7295796c8dcSSimon Schubert       abbrev_ptr += 1;
7305796c8dcSSimon Schubert 
7315796c8dcSSimon Schubert       /* Now read in declarations.  */
7325796c8dcSSimon Schubert       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7335796c8dcSSimon Schubert       abbrev_ptr += bytes_read;
7345796c8dcSSimon Schubert       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7355796c8dcSSimon Schubert       abbrev_ptr += bytes_read;
7365796c8dcSSimon Schubert 
7375796c8dcSSimon Schubert       while (abbrev_name)
7385796c8dcSSimon Schubert 	{
7395796c8dcSSimon Schubert 	  if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
7405796c8dcSSimon Schubert 	    {
7415796c8dcSSimon Schubert 	      struct attr_abbrev *tmp;
7425796c8dcSSimon Schubert 
7435796c8dcSSimon Schubert 	      amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
7445796c8dcSSimon Schubert 	      amt *= sizeof (struct attr_abbrev);
7455796c8dcSSimon Schubert 	      tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
7465796c8dcSSimon Schubert 	      if (tmp == NULL)
7475796c8dcSSimon Schubert 		{
7485796c8dcSSimon Schubert 		  size_t i;
7495796c8dcSSimon Schubert 
7505796c8dcSSimon Schubert 		  for (i = 0; i < ABBREV_HASH_SIZE; i++)
7515796c8dcSSimon Schubert 		    {
7525796c8dcSSimon Schubert 		      struct abbrev_info *abbrev = abbrevs[i];
7535796c8dcSSimon Schubert 
7545796c8dcSSimon Schubert 		      while (abbrev)
7555796c8dcSSimon Schubert 			{
7565796c8dcSSimon Schubert 			  free (abbrev->attrs);
7575796c8dcSSimon Schubert 			  abbrev = abbrev->next;
7585796c8dcSSimon Schubert 			}
7595796c8dcSSimon Schubert 		    }
7605796c8dcSSimon Schubert 		  return NULL;
7615796c8dcSSimon Schubert 		}
7625796c8dcSSimon Schubert 	      cur_abbrev->attrs = tmp;
7635796c8dcSSimon Schubert 	    }
7645796c8dcSSimon Schubert 
7655796c8dcSSimon Schubert 	  cur_abbrev->attrs[cur_abbrev->num_attrs].name
7665796c8dcSSimon Schubert 	    = (enum dwarf_attribute) abbrev_name;
7675796c8dcSSimon Schubert 	  cur_abbrev->attrs[cur_abbrev->num_attrs++].form
7685796c8dcSSimon Schubert 	    = (enum dwarf_form) abbrev_form;
7695796c8dcSSimon Schubert 	  abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7705796c8dcSSimon Schubert 	  abbrev_ptr += bytes_read;
7715796c8dcSSimon Schubert 	  abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7725796c8dcSSimon Schubert 	  abbrev_ptr += bytes_read;
7735796c8dcSSimon Schubert 	}
7745796c8dcSSimon Schubert 
7755796c8dcSSimon Schubert       hash_number = abbrev_number % ABBREV_HASH_SIZE;
7765796c8dcSSimon Schubert       cur_abbrev->next = abbrevs[hash_number];
7775796c8dcSSimon Schubert       abbrevs[hash_number] = cur_abbrev;
7785796c8dcSSimon Schubert 
7795796c8dcSSimon Schubert       /* Get next abbreviation.
7805796c8dcSSimon Schubert 	 Under Irix6 the abbreviations for a compilation unit are not
7815796c8dcSSimon Schubert 	 always properly terminated with an abbrev number of 0.
7825796c8dcSSimon Schubert 	 Exit loop if we encounter an abbreviation which we have
7835796c8dcSSimon Schubert 	 already read (which means we are about to read the abbreviations
7845796c8dcSSimon Schubert 	 for the next compile unit) or if the end of the abbreviation
7855796c8dcSSimon Schubert 	 table is reached.  */
7865796c8dcSSimon Schubert       if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
7875796c8dcSSimon Schubert 	  >= stash->dwarf_abbrev_size)
7885796c8dcSSimon Schubert 	break;
7895796c8dcSSimon Schubert       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7905796c8dcSSimon Schubert       abbrev_ptr += bytes_read;
7915796c8dcSSimon Schubert       if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
7925796c8dcSSimon Schubert 	break;
7935796c8dcSSimon Schubert     }
7945796c8dcSSimon Schubert 
7955796c8dcSSimon Schubert   return abbrevs;
7965796c8dcSSimon Schubert }
7975796c8dcSSimon Schubert 
7985796c8dcSSimon Schubert /* Read an attribute value described by an attribute form.  */
7995796c8dcSSimon Schubert 
8005796c8dcSSimon Schubert static bfd_byte *
read_attribute_value(struct attribute * attr,unsigned form,struct comp_unit * unit,bfd_byte * info_ptr)8015796c8dcSSimon Schubert read_attribute_value (struct attribute *attr,
8025796c8dcSSimon Schubert 		      unsigned form,
8035796c8dcSSimon Schubert 		      struct comp_unit *unit,
8045796c8dcSSimon Schubert 		      bfd_byte *info_ptr)
8055796c8dcSSimon Schubert {
8065796c8dcSSimon Schubert   bfd *abfd = unit->abfd;
8075796c8dcSSimon Schubert   unsigned int bytes_read;
8085796c8dcSSimon Schubert   struct dwarf_block *blk;
8095796c8dcSSimon Schubert   bfd_size_type amt;
8105796c8dcSSimon Schubert 
8115796c8dcSSimon Schubert   attr->form = (enum dwarf_form) form;
8125796c8dcSSimon Schubert 
8135796c8dcSSimon Schubert   switch (form)
8145796c8dcSSimon Schubert     {
8155796c8dcSSimon Schubert     case DW_FORM_ref_addr:
8165796c8dcSSimon Schubert       /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
8175796c8dcSSimon Schubert 	 DWARF3.  */
818cf7f2e2dSJohn Marino       if (unit->version == 3 || unit->version == 4)
8195796c8dcSSimon Schubert 	{
8205796c8dcSSimon Schubert 	  if (unit->offset_size == 4)
8215796c8dcSSimon Schubert 	    attr->u.val = read_4_bytes (unit->abfd, info_ptr);
8225796c8dcSSimon Schubert 	  else
8235796c8dcSSimon Schubert 	    attr->u.val = read_8_bytes (unit->abfd, info_ptr);
8245796c8dcSSimon Schubert 	  info_ptr += unit->offset_size;
8255796c8dcSSimon Schubert 	  break;
8265796c8dcSSimon Schubert 	}
8275796c8dcSSimon Schubert       /* FALLTHROUGH */
8285796c8dcSSimon Schubert     case DW_FORM_addr:
8295796c8dcSSimon Schubert       attr->u.val = read_address (unit, info_ptr);
8305796c8dcSSimon Schubert       info_ptr += unit->addr_size;
8315796c8dcSSimon Schubert       break;
832cf7f2e2dSJohn Marino     case DW_FORM_sec_offset:
833cf7f2e2dSJohn Marino       if (unit->offset_size == 4)
834cf7f2e2dSJohn Marino 	attr->u.val = read_4_bytes (unit->abfd, info_ptr);
835cf7f2e2dSJohn Marino       else
836cf7f2e2dSJohn Marino 	attr->u.val = read_8_bytes (unit->abfd, info_ptr);
837cf7f2e2dSJohn Marino       info_ptr += unit->offset_size;
838cf7f2e2dSJohn Marino       break;
8395796c8dcSSimon Schubert     case DW_FORM_block2:
8405796c8dcSSimon Schubert       amt = sizeof (struct dwarf_block);
8415796c8dcSSimon Schubert       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
842cf7f2e2dSJohn Marino       if (blk == NULL)
843cf7f2e2dSJohn Marino 	return NULL;
8445796c8dcSSimon Schubert       blk->size = read_2_bytes (abfd, info_ptr);
8455796c8dcSSimon Schubert       info_ptr += 2;
8465796c8dcSSimon Schubert       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8475796c8dcSSimon Schubert       info_ptr += blk->size;
8485796c8dcSSimon Schubert       attr->u.blk = blk;
8495796c8dcSSimon Schubert       break;
8505796c8dcSSimon Schubert     case DW_FORM_block4:
8515796c8dcSSimon Schubert       amt = sizeof (struct dwarf_block);
8525796c8dcSSimon Schubert       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
853cf7f2e2dSJohn Marino       if (blk == NULL)
854cf7f2e2dSJohn Marino 	return NULL;
8555796c8dcSSimon Schubert       blk->size = read_4_bytes (abfd, info_ptr);
8565796c8dcSSimon Schubert       info_ptr += 4;
8575796c8dcSSimon Schubert       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8585796c8dcSSimon Schubert       info_ptr += blk->size;
8595796c8dcSSimon Schubert       attr->u.blk = blk;
8605796c8dcSSimon Schubert       break;
8615796c8dcSSimon Schubert     case DW_FORM_data2:
8625796c8dcSSimon Schubert       attr->u.val = read_2_bytes (abfd, info_ptr);
8635796c8dcSSimon Schubert       info_ptr += 2;
8645796c8dcSSimon Schubert       break;
8655796c8dcSSimon Schubert     case DW_FORM_data4:
8665796c8dcSSimon Schubert       attr->u.val = read_4_bytes (abfd, info_ptr);
8675796c8dcSSimon Schubert       info_ptr += 4;
8685796c8dcSSimon Schubert       break;
8695796c8dcSSimon Schubert     case DW_FORM_data8:
8705796c8dcSSimon Schubert       attr->u.val = read_8_bytes (abfd, info_ptr);
8715796c8dcSSimon Schubert       info_ptr += 8;
8725796c8dcSSimon Schubert       break;
8735796c8dcSSimon Schubert     case DW_FORM_string:
8745796c8dcSSimon Schubert       attr->u.str = read_string (abfd, info_ptr, &bytes_read);
8755796c8dcSSimon Schubert       info_ptr += bytes_read;
8765796c8dcSSimon Schubert       break;
8775796c8dcSSimon Schubert     case DW_FORM_strp:
8785796c8dcSSimon Schubert       attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
8795796c8dcSSimon Schubert       info_ptr += bytes_read;
8805796c8dcSSimon Schubert       break;
881cf7f2e2dSJohn Marino     case DW_FORM_exprloc:
8825796c8dcSSimon Schubert     case DW_FORM_block:
8835796c8dcSSimon Schubert       amt = sizeof (struct dwarf_block);
8845796c8dcSSimon Schubert       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
885cf7f2e2dSJohn Marino       if (blk == NULL)
886cf7f2e2dSJohn Marino 	return NULL;
8875796c8dcSSimon Schubert       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8885796c8dcSSimon Schubert       info_ptr += bytes_read;
8895796c8dcSSimon Schubert       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8905796c8dcSSimon Schubert       info_ptr += blk->size;
8915796c8dcSSimon Schubert       attr->u.blk = blk;
8925796c8dcSSimon Schubert       break;
8935796c8dcSSimon Schubert     case DW_FORM_block1:
8945796c8dcSSimon Schubert       amt = sizeof (struct dwarf_block);
8955796c8dcSSimon Schubert       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
896cf7f2e2dSJohn Marino       if (blk == NULL)
897cf7f2e2dSJohn Marino 	return NULL;
8985796c8dcSSimon Schubert       blk->size = read_1_byte (abfd, info_ptr);
8995796c8dcSSimon Schubert       info_ptr += 1;
9005796c8dcSSimon Schubert       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9015796c8dcSSimon Schubert       info_ptr += blk->size;
9025796c8dcSSimon Schubert       attr->u.blk = blk;
9035796c8dcSSimon Schubert       break;
9045796c8dcSSimon Schubert     case DW_FORM_data1:
9055796c8dcSSimon Schubert       attr->u.val = read_1_byte (abfd, info_ptr);
9065796c8dcSSimon Schubert       info_ptr += 1;
9075796c8dcSSimon Schubert       break;
9085796c8dcSSimon Schubert     case DW_FORM_flag:
9095796c8dcSSimon Schubert       attr->u.val = read_1_byte (abfd, info_ptr);
9105796c8dcSSimon Schubert       info_ptr += 1;
9115796c8dcSSimon Schubert       break;
912cf7f2e2dSJohn Marino     case DW_FORM_flag_present:
913cf7f2e2dSJohn Marino       attr->u.val = 1;
914cf7f2e2dSJohn Marino       break;
9155796c8dcSSimon Schubert     case DW_FORM_sdata:
9165796c8dcSSimon Schubert       attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9175796c8dcSSimon Schubert       info_ptr += bytes_read;
9185796c8dcSSimon Schubert       break;
9195796c8dcSSimon Schubert     case DW_FORM_udata:
9205796c8dcSSimon Schubert       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9215796c8dcSSimon Schubert       info_ptr += bytes_read;
9225796c8dcSSimon Schubert       break;
9235796c8dcSSimon Schubert     case DW_FORM_ref1:
9245796c8dcSSimon Schubert       attr->u.val = read_1_byte (abfd, info_ptr);
9255796c8dcSSimon Schubert       info_ptr += 1;
9265796c8dcSSimon Schubert       break;
9275796c8dcSSimon Schubert     case DW_FORM_ref2:
9285796c8dcSSimon Schubert       attr->u.val = read_2_bytes (abfd, info_ptr);
9295796c8dcSSimon Schubert       info_ptr += 2;
9305796c8dcSSimon Schubert       break;
9315796c8dcSSimon Schubert     case DW_FORM_ref4:
9325796c8dcSSimon Schubert       attr->u.val = read_4_bytes (abfd, info_ptr);
9335796c8dcSSimon Schubert       info_ptr += 4;
9345796c8dcSSimon Schubert       break;
9355796c8dcSSimon Schubert     case DW_FORM_ref8:
9365796c8dcSSimon Schubert       attr->u.val = read_8_bytes (abfd, info_ptr);
9375796c8dcSSimon Schubert       info_ptr += 8;
9385796c8dcSSimon Schubert       break;
939cf7f2e2dSJohn Marino     case DW_FORM_ref_sig8:
940cf7f2e2dSJohn Marino       attr->u.val = read_8_bytes (abfd, info_ptr);
941cf7f2e2dSJohn Marino       info_ptr += 8;
942cf7f2e2dSJohn Marino       break;
9435796c8dcSSimon Schubert     case DW_FORM_ref_udata:
9445796c8dcSSimon Schubert       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9455796c8dcSSimon Schubert       info_ptr += bytes_read;
9465796c8dcSSimon Schubert       break;
9475796c8dcSSimon Schubert     case DW_FORM_indirect:
9485796c8dcSSimon Schubert       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9495796c8dcSSimon Schubert       info_ptr += bytes_read;
9505796c8dcSSimon Schubert       info_ptr = read_attribute_value (attr, form, unit, info_ptr);
9515796c8dcSSimon Schubert       break;
9525796c8dcSSimon Schubert     default:
9535796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
9545796c8dcSSimon Schubert 			     form);
9555796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
956cf7f2e2dSJohn Marino       return NULL;
9575796c8dcSSimon Schubert     }
9585796c8dcSSimon Schubert   return info_ptr;
9595796c8dcSSimon Schubert }
9605796c8dcSSimon Schubert 
9615796c8dcSSimon Schubert /* Read an attribute described by an abbreviated attribute.  */
9625796c8dcSSimon Schubert 
9635796c8dcSSimon Schubert static bfd_byte *
read_attribute(struct attribute * attr,struct attr_abbrev * abbrev,struct comp_unit * unit,bfd_byte * info_ptr)9645796c8dcSSimon Schubert read_attribute (struct attribute *attr,
9655796c8dcSSimon Schubert 		struct attr_abbrev *abbrev,
9665796c8dcSSimon Schubert 		struct comp_unit *unit,
9675796c8dcSSimon Schubert 		bfd_byte *info_ptr)
9685796c8dcSSimon Schubert {
9695796c8dcSSimon Schubert   attr->name = abbrev->name;
9705796c8dcSSimon Schubert   info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
9715796c8dcSSimon Schubert   return info_ptr;
9725796c8dcSSimon Schubert }
9735796c8dcSSimon Schubert 
9745796c8dcSSimon Schubert /* Source line information table routines.  */
9755796c8dcSSimon Schubert 
9765796c8dcSSimon Schubert #define FILE_ALLOC_CHUNK 5
9775796c8dcSSimon Schubert #define DIR_ALLOC_CHUNK 5
9785796c8dcSSimon Schubert 
9795796c8dcSSimon Schubert struct line_info
9805796c8dcSSimon Schubert {
9815796c8dcSSimon Schubert   struct line_info* prev_line;
9825796c8dcSSimon Schubert   bfd_vma address;
9835796c8dcSSimon Schubert   char *filename;
9845796c8dcSSimon Schubert   unsigned int line;
9855796c8dcSSimon Schubert   unsigned int column;
986*ef5ccd6cSJohn Marino   unsigned int discriminator;
987cf7f2e2dSJohn Marino   unsigned char op_index;
988cf7f2e2dSJohn Marino   unsigned char end_sequence;		/* End of (sequential) code sequence.  */
9895796c8dcSSimon Schubert };
9905796c8dcSSimon Schubert 
9915796c8dcSSimon Schubert struct fileinfo
9925796c8dcSSimon Schubert {
9935796c8dcSSimon Schubert   char *name;
9945796c8dcSSimon Schubert   unsigned int dir;
9955796c8dcSSimon Schubert   unsigned int time;
9965796c8dcSSimon Schubert   unsigned int size;
9975796c8dcSSimon Schubert };
9985796c8dcSSimon Schubert 
999cf7f2e2dSJohn Marino struct line_sequence
1000cf7f2e2dSJohn Marino {
1001cf7f2e2dSJohn Marino   bfd_vma               low_pc;
1002cf7f2e2dSJohn Marino   struct line_sequence* prev_sequence;
1003cf7f2e2dSJohn Marino   struct line_info*     last_line;  /* Largest VMA.  */
1004cf7f2e2dSJohn Marino };
1005cf7f2e2dSJohn Marino 
10065796c8dcSSimon Schubert struct line_info_table
10075796c8dcSSimon Schubert {
10085796c8dcSSimon Schubert   bfd*                  abfd;
10095796c8dcSSimon Schubert   unsigned int          num_files;
10105796c8dcSSimon Schubert   unsigned int          num_dirs;
1011cf7f2e2dSJohn Marino   unsigned int          num_sequences;
10125796c8dcSSimon Schubert   char *                comp_dir;
10135796c8dcSSimon Schubert   char **               dirs;
10145796c8dcSSimon Schubert   struct fileinfo*      files;
1015cf7f2e2dSJohn Marino   struct line_sequence* sequences;
1016cf7f2e2dSJohn Marino   struct line_info*     lcl_head;   /* Local head; used in 'add_line_info'.  */
10175796c8dcSSimon Schubert };
10185796c8dcSSimon Schubert 
10195796c8dcSSimon Schubert /* Remember some information about each function.  If the function is
10205796c8dcSSimon Schubert    inlined (DW_TAG_inlined_subroutine) it may have two additional
10215796c8dcSSimon Schubert    attributes, DW_AT_call_file and DW_AT_call_line, which specify the
10225796c8dcSSimon Schubert    source code location where this function was inlined.  */
10235796c8dcSSimon Schubert 
10245796c8dcSSimon Schubert struct funcinfo
10255796c8dcSSimon Schubert {
1026*ef5ccd6cSJohn Marino   /* Pointer to previous function in list of all functions.  */
1027*ef5ccd6cSJohn Marino   struct funcinfo *prev_func;
1028*ef5ccd6cSJohn Marino   /* Pointer to function one scope higher.  */
1029*ef5ccd6cSJohn Marino   struct funcinfo *caller_func;
1030*ef5ccd6cSJohn Marino   /* Source location file name where caller_func inlines this func.  */
1031*ef5ccd6cSJohn Marino   char *caller_file;
1032*ef5ccd6cSJohn Marino   /* Source location line number where caller_func inlines this func.  */
1033*ef5ccd6cSJohn Marino   int caller_line;
1034*ef5ccd6cSJohn Marino   /* Source location file name.  */
1035*ef5ccd6cSJohn Marino   char *file;
1036*ef5ccd6cSJohn Marino   /* Source location line number.  */
1037*ef5ccd6cSJohn Marino   int line;
10385796c8dcSSimon Schubert   int tag;
10395796c8dcSSimon Schubert   char *name;
10405796c8dcSSimon Schubert   struct arange arange;
1041*ef5ccd6cSJohn Marino   /* Where the symbol is defined.  */
1042*ef5ccd6cSJohn Marino   asection *sec;
10435796c8dcSSimon Schubert };
10445796c8dcSSimon Schubert 
10455796c8dcSSimon Schubert struct varinfo
10465796c8dcSSimon Schubert {
10475796c8dcSSimon Schubert   /* Pointer to previous variable in list of all variables */
10485796c8dcSSimon Schubert   struct varinfo *prev_var;
10495796c8dcSSimon Schubert   /* Source location file name */
10505796c8dcSSimon Schubert   char *file;
10515796c8dcSSimon Schubert   /* Source location line number */
10525796c8dcSSimon Schubert   int line;
10535796c8dcSSimon Schubert   int tag;
10545796c8dcSSimon Schubert   char *name;
10555796c8dcSSimon Schubert   bfd_vma addr;
10565796c8dcSSimon Schubert   /* Where the symbol is defined */
10575796c8dcSSimon Schubert   asection *sec;
10585796c8dcSSimon Schubert   /* Is this a stack variable? */
10595796c8dcSSimon Schubert   unsigned int stack: 1;
10605796c8dcSSimon Schubert };
10615796c8dcSSimon Schubert 
10625796c8dcSSimon Schubert /* Return TRUE if NEW_LINE should sort after LINE.  */
10635796c8dcSSimon Schubert 
10645796c8dcSSimon Schubert static inline bfd_boolean
new_line_sorts_after(struct line_info * new_line,struct line_info * line)10655796c8dcSSimon Schubert new_line_sorts_after (struct line_info *new_line, struct line_info *line)
10665796c8dcSSimon Schubert {
10675796c8dcSSimon Schubert   return (new_line->address > line->address
10685796c8dcSSimon Schubert 	  || (new_line->address == line->address
1069cf7f2e2dSJohn Marino 	      && (new_line->op_index > line->op_index
1070cf7f2e2dSJohn Marino 		  || (new_line->op_index == line->op_index
1071cf7f2e2dSJohn Marino 		      && new_line->end_sequence < line->end_sequence))));
10725796c8dcSSimon Schubert }
10735796c8dcSSimon Schubert 
10745796c8dcSSimon Schubert 
10755796c8dcSSimon Schubert /* Adds a new entry to the line_info list in the line_info_table, ensuring
10765796c8dcSSimon Schubert    that the list is sorted.  Note that the line_info list is sorted from
10775796c8dcSSimon Schubert    highest to lowest VMA (with possible duplicates); that is,
10785796c8dcSSimon Schubert    line_info->prev_line always accesses an equal or smaller VMA.  */
10795796c8dcSSimon Schubert 
1080cf7f2e2dSJohn Marino static bfd_boolean
add_line_info(struct line_info_table * table,bfd_vma address,unsigned char op_index,char * filename,unsigned int line,unsigned int column,unsigned int discriminator,int end_sequence)10815796c8dcSSimon Schubert add_line_info (struct line_info_table *table,
10825796c8dcSSimon Schubert 	       bfd_vma address,
1083cf7f2e2dSJohn Marino 	       unsigned char op_index,
10845796c8dcSSimon Schubert 	       char *filename,
10855796c8dcSSimon Schubert 	       unsigned int line,
10865796c8dcSSimon Schubert 	       unsigned int column,
1087*ef5ccd6cSJohn Marino 	       unsigned int discriminator,
10885796c8dcSSimon Schubert 	       int end_sequence)
10895796c8dcSSimon Schubert {
10905796c8dcSSimon Schubert   bfd_size_type amt = sizeof (struct line_info);
1091cf7f2e2dSJohn Marino   struct line_sequence* seq = table->sequences;
10925796c8dcSSimon Schubert   struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
10935796c8dcSSimon Schubert 
1094cf7f2e2dSJohn Marino   if (info == NULL)
1095cf7f2e2dSJohn Marino     return FALSE;
1096cf7f2e2dSJohn Marino 
10975796c8dcSSimon Schubert   /* Set member data of 'info'.  */
1098cf7f2e2dSJohn Marino   info->prev_line = NULL;
10995796c8dcSSimon Schubert   info->address = address;
1100cf7f2e2dSJohn Marino   info->op_index = op_index;
11015796c8dcSSimon Schubert   info->line = line;
11025796c8dcSSimon Schubert   info->column = column;
1103*ef5ccd6cSJohn Marino   info->discriminator = discriminator;
11045796c8dcSSimon Schubert   info->end_sequence = end_sequence;
11055796c8dcSSimon Schubert 
11065796c8dcSSimon Schubert   if (filename && filename[0])
11075796c8dcSSimon Schubert     {
11085796c8dcSSimon Schubert       info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1109cf7f2e2dSJohn Marino       if (info->filename == NULL)
1110cf7f2e2dSJohn Marino 	return FALSE;
11115796c8dcSSimon Schubert       strcpy (info->filename, filename);
11125796c8dcSSimon Schubert     }
11135796c8dcSSimon Schubert   else
11145796c8dcSSimon Schubert     info->filename = NULL;
11155796c8dcSSimon Schubert 
11165796c8dcSSimon Schubert   /* Find the correct location for 'info'.  Normally we will receive
11175796c8dcSSimon Schubert      new line_info data 1) in order and 2) with increasing VMAs.
11185796c8dcSSimon Schubert      However some compilers break the rules (cf. decode_line_info) and
11195796c8dcSSimon Schubert      so we include some heuristics for quickly finding the correct
11205796c8dcSSimon Schubert      location for 'info'. In particular, these heuristics optimize for
11215796c8dcSSimon Schubert      the common case in which the VMA sequence that we receive is a
11225796c8dcSSimon Schubert      list of locally sorted VMAs such as
11235796c8dcSSimon Schubert        p...z a...j  (where a < j < p < z)
11245796c8dcSSimon Schubert 
11255796c8dcSSimon Schubert      Note: table->lcl_head is used to head an *actual* or *possible*
1126cf7f2e2dSJohn Marino      sub-sequence within the list (such as a...j) that is not directly
11275796c8dcSSimon Schubert      headed by table->last_line
11285796c8dcSSimon Schubert 
11295796c8dcSSimon Schubert      Note: we may receive duplicate entries from 'decode_line_info'.  */
11305796c8dcSSimon Schubert 
1131cf7f2e2dSJohn Marino   if (seq
1132cf7f2e2dSJohn Marino       && seq->last_line->address == address
1133cf7f2e2dSJohn Marino       && seq->last_line->op_index == op_index
1134cf7f2e2dSJohn Marino       && seq->last_line->end_sequence == end_sequence)
11355796c8dcSSimon Schubert     {
11365796c8dcSSimon Schubert       /* We only keep the last entry with the same address and end
11375796c8dcSSimon Schubert 	 sequence.  See PR ld/4986.  */
1138cf7f2e2dSJohn Marino       if (table->lcl_head == seq->last_line)
11395796c8dcSSimon Schubert 	table->lcl_head = info;
1140cf7f2e2dSJohn Marino       info->prev_line = seq->last_line->prev_line;
1141cf7f2e2dSJohn Marino       seq->last_line = info;
11425796c8dcSSimon Schubert     }
1143cf7f2e2dSJohn Marino   else if (!seq || seq->last_line->end_sequence)
11445796c8dcSSimon Schubert     {
1145cf7f2e2dSJohn Marino       /* Start a new line sequence.  */
1146cf7f2e2dSJohn Marino       amt = sizeof (struct line_sequence);
1147cf7f2e2dSJohn Marino       seq = (struct line_sequence *) bfd_malloc (amt);
1148cf7f2e2dSJohn Marino       if (seq == NULL)
1149cf7f2e2dSJohn Marino 	return FALSE;
1150cf7f2e2dSJohn Marino       seq->low_pc = address;
1151cf7f2e2dSJohn Marino       seq->prev_sequence = table->sequences;
1152cf7f2e2dSJohn Marino       seq->last_line = info;
1153cf7f2e2dSJohn Marino       table->lcl_head = info;
1154cf7f2e2dSJohn Marino       table->sequences = seq;
1155cf7f2e2dSJohn Marino       table->num_sequences++;
1156cf7f2e2dSJohn Marino     }
1157cf7f2e2dSJohn Marino   else if (new_line_sorts_after (info, seq->last_line))
1158cf7f2e2dSJohn Marino     {
1159cf7f2e2dSJohn Marino       /* Normal case: add 'info' to the beginning of the current sequence.  */
1160cf7f2e2dSJohn Marino       info->prev_line = seq->last_line;
1161cf7f2e2dSJohn Marino       seq->last_line = info;
11625796c8dcSSimon Schubert 
11635796c8dcSSimon Schubert       /* lcl_head: initialize to head a *possible* sequence at the end.  */
11645796c8dcSSimon Schubert       if (!table->lcl_head)
11655796c8dcSSimon Schubert 	table->lcl_head = info;
11665796c8dcSSimon Schubert     }
11675796c8dcSSimon Schubert   else if (!new_line_sorts_after (info, table->lcl_head)
11685796c8dcSSimon Schubert 	   && (!table->lcl_head->prev_line
11695796c8dcSSimon Schubert 	       || new_line_sorts_after (info, table->lcl_head->prev_line)))
11705796c8dcSSimon Schubert     {
11715796c8dcSSimon Schubert       /* Abnormal but easy: lcl_head is the head of 'info'.  */
11725796c8dcSSimon Schubert       info->prev_line = table->lcl_head->prev_line;
11735796c8dcSSimon Schubert       table->lcl_head->prev_line = info;
11745796c8dcSSimon Schubert     }
11755796c8dcSSimon Schubert   else
11765796c8dcSSimon Schubert     {
1177cf7f2e2dSJohn Marino       /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1178cf7f2e2dSJohn Marino 	 are valid heads for 'info'.  Reset 'lcl_head'.  */
1179cf7f2e2dSJohn Marino       struct line_info* li2 = seq->last_line; /* Always non-NULL.  */
11805796c8dcSSimon Schubert       struct line_info* li1 = li2->prev_line;
11815796c8dcSSimon Schubert 
11825796c8dcSSimon Schubert       while (li1)
11835796c8dcSSimon Schubert 	{
11845796c8dcSSimon Schubert 	  if (!new_line_sorts_after (info, li2)
11855796c8dcSSimon Schubert 	      && new_line_sorts_after (info, li1))
11865796c8dcSSimon Schubert 	    break;
11875796c8dcSSimon Schubert 
11885796c8dcSSimon Schubert 	  li2 = li1; /* always non-NULL */
11895796c8dcSSimon Schubert 	  li1 = li1->prev_line;
11905796c8dcSSimon Schubert 	}
11915796c8dcSSimon Schubert       table->lcl_head = li2;
11925796c8dcSSimon Schubert       info->prev_line = table->lcl_head->prev_line;
11935796c8dcSSimon Schubert       table->lcl_head->prev_line = info;
1194cf7f2e2dSJohn Marino       if (address < seq->low_pc)
1195cf7f2e2dSJohn Marino         seq->low_pc = address;
11965796c8dcSSimon Schubert     }
1197cf7f2e2dSJohn Marino   return TRUE;
11985796c8dcSSimon Schubert }
11995796c8dcSSimon Schubert 
12005796c8dcSSimon Schubert /* Extract a fully qualified filename from a line info table.
12015796c8dcSSimon Schubert    The returned string has been malloc'ed and it is the caller's
12025796c8dcSSimon Schubert    responsibility to free it.  */
12035796c8dcSSimon Schubert 
12045796c8dcSSimon Schubert static char *
concat_filename(struct line_info_table * table,unsigned int file)12055796c8dcSSimon Schubert concat_filename (struct line_info_table *table, unsigned int file)
12065796c8dcSSimon Schubert {
12075796c8dcSSimon Schubert   char *filename;
12085796c8dcSSimon Schubert 
12095796c8dcSSimon Schubert   if (file - 1 >= table->num_files)
12105796c8dcSSimon Schubert     {
12115796c8dcSSimon Schubert       /* FILE == 0 means unknown.  */
12125796c8dcSSimon Schubert       if (file)
12135796c8dcSSimon Schubert 	(*_bfd_error_handler)
12145796c8dcSSimon Schubert 	  (_("Dwarf Error: mangled line number section (bad file number)."));
12155796c8dcSSimon Schubert       return strdup ("<unknown>");
12165796c8dcSSimon Schubert     }
12175796c8dcSSimon Schubert 
12185796c8dcSSimon Schubert   filename = table->files[file - 1].name;
12195796c8dcSSimon Schubert 
12205796c8dcSSimon Schubert   if (!IS_ABSOLUTE_PATH (filename))
12215796c8dcSSimon Schubert     {
1222cf7f2e2dSJohn Marino       char *dir_name = NULL;
1223cf7f2e2dSJohn Marino       char *subdir_name = NULL;
12245796c8dcSSimon Schubert       char *name;
12255796c8dcSSimon Schubert       size_t len;
12265796c8dcSSimon Schubert 
12275796c8dcSSimon Schubert       if (table->files[file - 1].dir)
1228cf7f2e2dSJohn Marino 	subdir_name = table->dirs[table->files[file - 1].dir - 1];
12295796c8dcSSimon Schubert 
1230cf7f2e2dSJohn Marino       if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1231cf7f2e2dSJohn Marino 	dir_name = table->comp_dir;
12325796c8dcSSimon Schubert 
1233cf7f2e2dSJohn Marino       if (!dir_name)
12345796c8dcSSimon Schubert 	{
1235cf7f2e2dSJohn Marino 	  dir_name = subdir_name;
1236cf7f2e2dSJohn Marino 	  subdir_name = NULL;
12375796c8dcSSimon Schubert 	}
12385796c8dcSSimon Schubert 
1239cf7f2e2dSJohn Marino       if (!dir_name)
12405796c8dcSSimon Schubert 	return strdup (filename);
12415796c8dcSSimon Schubert 
1242cf7f2e2dSJohn Marino       len = strlen (dir_name) + strlen (filename) + 2;
12435796c8dcSSimon Schubert 
1244cf7f2e2dSJohn Marino       if (subdir_name)
12455796c8dcSSimon Schubert 	{
1246cf7f2e2dSJohn Marino 	  len += strlen (subdir_name) + 1;
12475796c8dcSSimon Schubert 	  name = (char *) bfd_malloc (len);
12485796c8dcSSimon Schubert 	  if (name)
1249cf7f2e2dSJohn Marino 	    sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
12505796c8dcSSimon Schubert 	}
12515796c8dcSSimon Schubert       else
12525796c8dcSSimon Schubert 	{
12535796c8dcSSimon Schubert 	  name = (char *) bfd_malloc (len);
12545796c8dcSSimon Schubert 	  if (name)
1255cf7f2e2dSJohn Marino 	    sprintf (name, "%s/%s", dir_name, filename);
12565796c8dcSSimon Schubert 	}
12575796c8dcSSimon Schubert 
12585796c8dcSSimon Schubert       return name;
12595796c8dcSSimon Schubert     }
12605796c8dcSSimon Schubert 
12615796c8dcSSimon Schubert   return strdup (filename);
12625796c8dcSSimon Schubert }
12635796c8dcSSimon Schubert 
1264cf7f2e2dSJohn Marino static bfd_boolean
arange_add(const struct comp_unit * unit,struct arange * first_arange,bfd_vma low_pc,bfd_vma high_pc)1265*ef5ccd6cSJohn Marino arange_add (const struct comp_unit *unit, struct arange *first_arange,
1266cf7f2e2dSJohn Marino 	    bfd_vma low_pc, bfd_vma high_pc)
12675796c8dcSSimon Schubert {
12685796c8dcSSimon Schubert   struct arange *arange;
12695796c8dcSSimon Schubert 
1270*ef5ccd6cSJohn Marino   /* Ignore empty ranges.  */
1271*ef5ccd6cSJohn Marino   if (low_pc == high_pc)
1272*ef5ccd6cSJohn Marino     return TRUE;
1273*ef5ccd6cSJohn Marino 
12745796c8dcSSimon Schubert   /* If the first arange is empty, use it.  */
12755796c8dcSSimon Schubert   if (first_arange->high == 0)
12765796c8dcSSimon Schubert     {
12775796c8dcSSimon Schubert       first_arange->low = low_pc;
12785796c8dcSSimon Schubert       first_arange->high = high_pc;
1279cf7f2e2dSJohn Marino       return TRUE;
12805796c8dcSSimon Schubert     }
12815796c8dcSSimon Schubert 
12825796c8dcSSimon Schubert   /* Next see if we can cheaply extend an existing range.  */
12835796c8dcSSimon Schubert   arange = first_arange;
12845796c8dcSSimon Schubert   do
12855796c8dcSSimon Schubert     {
12865796c8dcSSimon Schubert       if (low_pc == arange->high)
12875796c8dcSSimon Schubert 	{
12885796c8dcSSimon Schubert 	  arange->high = high_pc;
1289cf7f2e2dSJohn Marino 	  return TRUE;
12905796c8dcSSimon Schubert 	}
12915796c8dcSSimon Schubert       if (high_pc == arange->low)
12925796c8dcSSimon Schubert 	{
12935796c8dcSSimon Schubert 	  arange->low = low_pc;
1294cf7f2e2dSJohn Marino 	  return TRUE;
12955796c8dcSSimon Schubert 	}
12965796c8dcSSimon Schubert       arange = arange->next;
12975796c8dcSSimon Schubert     }
12985796c8dcSSimon Schubert   while (arange);
12995796c8dcSSimon Schubert 
13005796c8dcSSimon Schubert   /* Need to allocate a new arange and insert it into the arange list.
13015796c8dcSSimon Schubert      Order isn't significant, so just insert after the first arange. */
1302*ef5ccd6cSJohn Marino   arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
1303cf7f2e2dSJohn Marino   if (arange == NULL)
1304cf7f2e2dSJohn Marino     return FALSE;
13055796c8dcSSimon Schubert   arange->low = low_pc;
13065796c8dcSSimon Schubert   arange->high = high_pc;
13075796c8dcSSimon Schubert   arange->next = first_arange->next;
13085796c8dcSSimon Schubert   first_arange->next = arange;
1309cf7f2e2dSJohn Marino   return TRUE;
1310cf7f2e2dSJohn Marino }
1311cf7f2e2dSJohn Marino 
1312cf7f2e2dSJohn Marino /* Compare function for line sequences.  */
1313cf7f2e2dSJohn Marino 
1314cf7f2e2dSJohn Marino static int
compare_sequences(const void * a,const void * b)1315cf7f2e2dSJohn Marino compare_sequences (const void* a, const void* b)
1316cf7f2e2dSJohn Marino {
1317cf7f2e2dSJohn Marino   const struct line_sequence* seq1 = a;
1318cf7f2e2dSJohn Marino   const struct line_sequence* seq2 = b;
1319cf7f2e2dSJohn Marino 
1320cf7f2e2dSJohn Marino   /* Sort by low_pc as the primary key.  */
1321cf7f2e2dSJohn Marino   if (seq1->low_pc < seq2->low_pc)
1322cf7f2e2dSJohn Marino     return -1;
1323cf7f2e2dSJohn Marino   if (seq1->low_pc > seq2->low_pc)
1324cf7f2e2dSJohn Marino     return 1;
1325cf7f2e2dSJohn Marino 
1326cf7f2e2dSJohn Marino   /* If low_pc values are equal, sort in reverse order of
1327cf7f2e2dSJohn Marino      high_pc, so that the largest region comes first.  */
1328cf7f2e2dSJohn Marino   if (seq1->last_line->address < seq2->last_line->address)
1329cf7f2e2dSJohn Marino     return 1;
1330cf7f2e2dSJohn Marino   if (seq1->last_line->address > seq2->last_line->address)
1331cf7f2e2dSJohn Marino     return -1;
1332cf7f2e2dSJohn Marino 
1333cf7f2e2dSJohn Marino   if (seq1->last_line->op_index < seq2->last_line->op_index)
1334cf7f2e2dSJohn Marino     return 1;
1335cf7f2e2dSJohn Marino   if (seq1->last_line->op_index > seq2->last_line->op_index)
1336cf7f2e2dSJohn Marino     return -1;
1337cf7f2e2dSJohn Marino 
1338cf7f2e2dSJohn Marino   return 0;
1339cf7f2e2dSJohn Marino }
1340cf7f2e2dSJohn Marino 
1341cf7f2e2dSJohn Marino /* Sort the line sequences for quick lookup.  */
1342cf7f2e2dSJohn Marino 
1343cf7f2e2dSJohn Marino static bfd_boolean
sort_line_sequences(struct line_info_table * table)1344cf7f2e2dSJohn Marino sort_line_sequences (struct line_info_table* table)
1345cf7f2e2dSJohn Marino {
1346cf7f2e2dSJohn Marino   bfd_size_type amt;
1347cf7f2e2dSJohn Marino   struct line_sequence* sequences;
1348cf7f2e2dSJohn Marino   struct line_sequence* seq;
1349cf7f2e2dSJohn Marino   unsigned int n = 0;
1350cf7f2e2dSJohn Marino   unsigned int num_sequences = table->num_sequences;
1351cf7f2e2dSJohn Marino   bfd_vma last_high_pc;
1352cf7f2e2dSJohn Marino 
1353cf7f2e2dSJohn Marino   if (num_sequences == 0)
1354cf7f2e2dSJohn Marino     return TRUE;
1355cf7f2e2dSJohn Marino 
1356cf7f2e2dSJohn Marino   /* Allocate space for an array of sequences.  */
1357cf7f2e2dSJohn Marino   amt = sizeof (struct line_sequence) * num_sequences;
1358cf7f2e2dSJohn Marino   sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
1359cf7f2e2dSJohn Marino   if (sequences == NULL)
1360cf7f2e2dSJohn Marino     return FALSE;
1361cf7f2e2dSJohn Marino 
1362cf7f2e2dSJohn Marino   /* Copy the linked list into the array, freeing the original nodes.  */
1363cf7f2e2dSJohn Marino   seq = table->sequences;
1364cf7f2e2dSJohn Marino   for (n = 0; n < num_sequences; n++)
1365cf7f2e2dSJohn Marino     {
1366cf7f2e2dSJohn Marino       struct line_sequence* last_seq = seq;
1367cf7f2e2dSJohn Marino 
1368cf7f2e2dSJohn Marino       BFD_ASSERT (seq);
1369cf7f2e2dSJohn Marino       sequences[n].low_pc = seq->low_pc;
1370cf7f2e2dSJohn Marino       sequences[n].prev_sequence = NULL;
1371cf7f2e2dSJohn Marino       sequences[n].last_line = seq->last_line;
1372cf7f2e2dSJohn Marino       seq = seq->prev_sequence;
1373cf7f2e2dSJohn Marino       free (last_seq);
1374cf7f2e2dSJohn Marino     }
1375cf7f2e2dSJohn Marino   BFD_ASSERT (seq == NULL);
1376cf7f2e2dSJohn Marino 
1377cf7f2e2dSJohn Marino   qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1378cf7f2e2dSJohn Marino 
1379cf7f2e2dSJohn Marino   /* Make the list binary-searchable by trimming overlapping entries
1380cf7f2e2dSJohn Marino      and removing nested entries.  */
1381cf7f2e2dSJohn Marino   num_sequences = 1;
1382cf7f2e2dSJohn Marino   last_high_pc = sequences[0].last_line->address;
1383cf7f2e2dSJohn Marino   for (n = 1; n < table->num_sequences; n++)
1384cf7f2e2dSJohn Marino     {
1385cf7f2e2dSJohn Marino       if (sequences[n].low_pc < last_high_pc)
1386cf7f2e2dSJohn Marino         {
1387cf7f2e2dSJohn Marino 	  if (sequences[n].last_line->address <= last_high_pc)
1388cf7f2e2dSJohn Marino 	    /* Skip nested entries.  */
1389cf7f2e2dSJohn Marino 	    continue;
1390cf7f2e2dSJohn Marino 
1391cf7f2e2dSJohn Marino 	  /* Trim overlapping entries.  */
1392cf7f2e2dSJohn Marino 	  sequences[n].low_pc = last_high_pc;
1393cf7f2e2dSJohn Marino         }
1394cf7f2e2dSJohn Marino       last_high_pc = sequences[n].last_line->address;
1395cf7f2e2dSJohn Marino       if (n > num_sequences)
1396cf7f2e2dSJohn Marino         {
1397cf7f2e2dSJohn Marino           /* Close up the gap.  */
1398cf7f2e2dSJohn Marino           sequences[num_sequences].low_pc = sequences[n].low_pc;
1399cf7f2e2dSJohn Marino           sequences[num_sequences].last_line = sequences[n].last_line;
1400cf7f2e2dSJohn Marino         }
1401cf7f2e2dSJohn Marino       num_sequences++;
1402cf7f2e2dSJohn Marino     }
1403cf7f2e2dSJohn Marino 
1404cf7f2e2dSJohn Marino   table->sequences = sequences;
1405cf7f2e2dSJohn Marino   table->num_sequences = num_sequences;
1406cf7f2e2dSJohn Marino   return TRUE;
14075796c8dcSSimon Schubert }
14085796c8dcSSimon Schubert 
14095796c8dcSSimon Schubert /* Decode the line number information for UNIT.  */
14105796c8dcSSimon Schubert 
14115796c8dcSSimon Schubert static struct line_info_table*
decode_line_info(struct comp_unit * unit,struct dwarf2_debug * stash)14125796c8dcSSimon Schubert decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
14135796c8dcSSimon Schubert {
14145796c8dcSSimon Schubert   bfd *abfd = unit->abfd;
14155796c8dcSSimon Schubert   struct line_info_table* table;
14165796c8dcSSimon Schubert   bfd_byte *line_ptr;
14175796c8dcSSimon Schubert   bfd_byte *line_end;
14185796c8dcSSimon Schubert   struct line_head lh;
14195796c8dcSSimon Schubert   unsigned int i, bytes_read, offset_size;
14205796c8dcSSimon Schubert   char *cur_file, *cur_dir;
14215796c8dcSSimon Schubert   unsigned char op_code, extended_op, adj_opcode;
1422*ef5ccd6cSJohn Marino   unsigned int exop_len;
14235796c8dcSSimon Schubert   bfd_size_type amt;
14245796c8dcSSimon Schubert 
1425a45ae5f8SJohn Marino   if (! read_section (abfd, &stash->debug_sections[debug_line],
1426a45ae5f8SJohn Marino                       stash->syms, unit->line_offset,
14275796c8dcSSimon Schubert 		      &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1428cf7f2e2dSJohn Marino     return NULL;
14295796c8dcSSimon Schubert 
14305796c8dcSSimon Schubert   amt = sizeof (struct line_info_table);
14315796c8dcSSimon Schubert   table = (struct line_info_table *) bfd_alloc (abfd, amt);
1432cf7f2e2dSJohn Marino   if (table == NULL)
1433cf7f2e2dSJohn Marino     return NULL;
14345796c8dcSSimon Schubert   table->abfd = abfd;
14355796c8dcSSimon Schubert   table->comp_dir = unit->comp_dir;
14365796c8dcSSimon Schubert 
14375796c8dcSSimon Schubert   table->num_files = 0;
14385796c8dcSSimon Schubert   table->files = NULL;
14395796c8dcSSimon Schubert 
14405796c8dcSSimon Schubert   table->num_dirs = 0;
14415796c8dcSSimon Schubert   table->dirs = NULL;
14425796c8dcSSimon Schubert 
1443cf7f2e2dSJohn Marino   table->num_sequences = 0;
1444cf7f2e2dSJohn Marino   table->sequences = NULL;
1445cf7f2e2dSJohn Marino 
14465796c8dcSSimon Schubert   table->lcl_head = NULL;
14475796c8dcSSimon Schubert 
14485796c8dcSSimon Schubert   line_ptr = stash->dwarf_line_buffer + unit->line_offset;
14495796c8dcSSimon Schubert 
14505796c8dcSSimon Schubert   /* Read in the prologue.  */
14515796c8dcSSimon Schubert   lh.total_length = read_4_bytes (abfd, line_ptr);
14525796c8dcSSimon Schubert   line_ptr += 4;
14535796c8dcSSimon Schubert   offset_size = 4;
14545796c8dcSSimon Schubert   if (lh.total_length == 0xffffffff)
14555796c8dcSSimon Schubert     {
14565796c8dcSSimon Schubert       lh.total_length = read_8_bytes (abfd, line_ptr);
14575796c8dcSSimon Schubert       line_ptr += 8;
14585796c8dcSSimon Schubert       offset_size = 8;
14595796c8dcSSimon Schubert     }
14605796c8dcSSimon Schubert   else if (lh.total_length == 0 && unit->addr_size == 8)
14615796c8dcSSimon Schubert     {
14625796c8dcSSimon Schubert       /* Handle (non-standard) 64-bit DWARF2 formats.  */
14635796c8dcSSimon Schubert       lh.total_length = read_4_bytes (abfd, line_ptr);
14645796c8dcSSimon Schubert       line_ptr += 4;
14655796c8dcSSimon Schubert       offset_size = 8;
14665796c8dcSSimon Schubert     }
14675796c8dcSSimon Schubert   line_end = line_ptr + lh.total_length;
14685796c8dcSSimon Schubert   lh.version = read_2_bytes (abfd, line_ptr);
1469cf7f2e2dSJohn Marino   if (lh.version < 2 || lh.version > 4)
1470cf7f2e2dSJohn Marino     {
1471cf7f2e2dSJohn Marino       (*_bfd_error_handler)
1472cf7f2e2dSJohn Marino 	(_("Dwarf Error: Unhandled .debug_line version %d."), lh.version);
1473cf7f2e2dSJohn Marino       bfd_set_error (bfd_error_bad_value);
1474cf7f2e2dSJohn Marino       return NULL;
1475cf7f2e2dSJohn Marino     }
14765796c8dcSSimon Schubert   line_ptr += 2;
14775796c8dcSSimon Schubert   if (offset_size == 4)
14785796c8dcSSimon Schubert     lh.prologue_length = read_4_bytes (abfd, line_ptr);
14795796c8dcSSimon Schubert   else
14805796c8dcSSimon Schubert     lh.prologue_length = read_8_bytes (abfd, line_ptr);
14815796c8dcSSimon Schubert   line_ptr += offset_size;
14825796c8dcSSimon Schubert   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
14835796c8dcSSimon Schubert   line_ptr += 1;
1484cf7f2e2dSJohn Marino   if (lh.version >= 4)
1485cf7f2e2dSJohn Marino     {
1486cf7f2e2dSJohn Marino       lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr);
1487cf7f2e2dSJohn Marino       line_ptr += 1;
1488cf7f2e2dSJohn Marino     }
1489cf7f2e2dSJohn Marino   else
1490cf7f2e2dSJohn Marino     lh.maximum_ops_per_insn = 1;
1491cf7f2e2dSJohn Marino   if (lh.maximum_ops_per_insn == 0)
1492cf7f2e2dSJohn Marino     {
1493cf7f2e2dSJohn Marino       (*_bfd_error_handler)
1494cf7f2e2dSJohn Marino 	(_("Dwarf Error: Invalid maximum operations per instruction."));
1495cf7f2e2dSJohn Marino       bfd_set_error (bfd_error_bad_value);
1496cf7f2e2dSJohn Marino       return NULL;
1497cf7f2e2dSJohn Marino     }
14985796c8dcSSimon Schubert   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
14995796c8dcSSimon Schubert   line_ptr += 1;
15005796c8dcSSimon Schubert   lh.line_base = read_1_signed_byte (abfd, line_ptr);
15015796c8dcSSimon Schubert   line_ptr += 1;
15025796c8dcSSimon Schubert   lh.line_range = read_1_byte (abfd, line_ptr);
15035796c8dcSSimon Schubert   line_ptr += 1;
15045796c8dcSSimon Schubert   lh.opcode_base = read_1_byte (abfd, line_ptr);
15055796c8dcSSimon Schubert   line_ptr += 1;
15065796c8dcSSimon Schubert   amt = lh.opcode_base * sizeof (unsigned char);
15075796c8dcSSimon Schubert   lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
15085796c8dcSSimon Schubert 
15095796c8dcSSimon Schubert   lh.standard_opcode_lengths[0] = 1;
15105796c8dcSSimon Schubert 
15115796c8dcSSimon Schubert   for (i = 1; i < lh.opcode_base; ++i)
15125796c8dcSSimon Schubert     {
15135796c8dcSSimon Schubert       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15145796c8dcSSimon Schubert       line_ptr += 1;
15155796c8dcSSimon Schubert     }
15165796c8dcSSimon Schubert 
15175796c8dcSSimon Schubert   /* Read directory table.  */
15185796c8dcSSimon Schubert   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
15195796c8dcSSimon Schubert     {
15205796c8dcSSimon Schubert       line_ptr += bytes_read;
15215796c8dcSSimon Schubert 
15225796c8dcSSimon Schubert       if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
15235796c8dcSSimon Schubert 	{
15245796c8dcSSimon Schubert 	  char **tmp;
15255796c8dcSSimon Schubert 
15265796c8dcSSimon Schubert 	  amt = table->num_dirs + DIR_ALLOC_CHUNK;
15275796c8dcSSimon Schubert 	  amt *= sizeof (char *);
15285796c8dcSSimon Schubert 
15295796c8dcSSimon Schubert 	  tmp = (char **) bfd_realloc (table->dirs, amt);
15305796c8dcSSimon Schubert 	  if (tmp == NULL)
1531cf7f2e2dSJohn Marino 	    goto fail;
15325796c8dcSSimon Schubert 	  table->dirs = tmp;
15335796c8dcSSimon Schubert 	}
15345796c8dcSSimon Schubert 
15355796c8dcSSimon Schubert       table->dirs[table->num_dirs++] = cur_dir;
15365796c8dcSSimon Schubert     }
15375796c8dcSSimon Schubert 
15385796c8dcSSimon Schubert   line_ptr += bytes_read;
15395796c8dcSSimon Schubert 
15405796c8dcSSimon Schubert   /* Read file name table.  */
15415796c8dcSSimon Schubert   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
15425796c8dcSSimon Schubert     {
15435796c8dcSSimon Schubert       line_ptr += bytes_read;
15445796c8dcSSimon Schubert 
15455796c8dcSSimon Schubert       if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
15465796c8dcSSimon Schubert 	{
15475796c8dcSSimon Schubert 	  struct fileinfo *tmp;
15485796c8dcSSimon Schubert 
15495796c8dcSSimon Schubert 	  amt = table->num_files + FILE_ALLOC_CHUNK;
15505796c8dcSSimon Schubert 	  amt *= sizeof (struct fileinfo);
15515796c8dcSSimon Schubert 
15525796c8dcSSimon Schubert 	  tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
15535796c8dcSSimon Schubert 	  if (tmp == NULL)
1554cf7f2e2dSJohn Marino 	    goto fail;
15555796c8dcSSimon Schubert 	  table->files = tmp;
15565796c8dcSSimon Schubert 	}
15575796c8dcSSimon Schubert 
15585796c8dcSSimon Schubert       table->files[table->num_files].name = cur_file;
15595796c8dcSSimon Schubert       table->files[table->num_files].dir =
15605796c8dcSSimon Schubert 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15615796c8dcSSimon Schubert       line_ptr += bytes_read;
15625796c8dcSSimon Schubert       table->files[table->num_files].time =
15635796c8dcSSimon Schubert 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15645796c8dcSSimon Schubert       line_ptr += bytes_read;
15655796c8dcSSimon Schubert       table->files[table->num_files].size =
15665796c8dcSSimon Schubert 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15675796c8dcSSimon Schubert       line_ptr += bytes_read;
15685796c8dcSSimon Schubert       table->num_files++;
15695796c8dcSSimon Schubert     }
15705796c8dcSSimon Schubert 
15715796c8dcSSimon Schubert   line_ptr += bytes_read;
15725796c8dcSSimon Schubert 
15735796c8dcSSimon Schubert   /* Read the statement sequences until there's nothing left.  */
15745796c8dcSSimon Schubert   while (line_ptr < line_end)
15755796c8dcSSimon Schubert     {
15765796c8dcSSimon Schubert       /* State machine registers.  */
15775796c8dcSSimon Schubert       bfd_vma address = 0;
1578cf7f2e2dSJohn Marino       unsigned char op_index = 0;
15795796c8dcSSimon Schubert       char * filename = table->num_files ? concat_filename (table, 1) : NULL;
15805796c8dcSSimon Schubert       unsigned int line = 1;
15815796c8dcSSimon Schubert       unsigned int column = 0;
1582*ef5ccd6cSJohn Marino       unsigned int discriminator = 0;
15835796c8dcSSimon Schubert       int is_stmt = lh.default_is_stmt;
15845796c8dcSSimon Schubert       int end_sequence = 0;
15855796c8dcSSimon Schubert       /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
15865796c8dcSSimon Schubert 	 compilers generate address sequences that are wildly out of
15875796c8dcSSimon Schubert 	 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
15885796c8dcSSimon Schubert 	 for ia64-Linux).  Thus, to determine the low and high
15895796c8dcSSimon Schubert 	 address, we must compare on every DW_LNS_copy, etc.  */
15905796c8dcSSimon Schubert       bfd_vma low_pc  = (bfd_vma) -1;
15915796c8dcSSimon Schubert       bfd_vma high_pc = 0;
15925796c8dcSSimon Schubert 
15935796c8dcSSimon Schubert       /* Decode the table.  */
15945796c8dcSSimon Schubert       while (! end_sequence)
15955796c8dcSSimon Schubert 	{
15965796c8dcSSimon Schubert 	  op_code = read_1_byte (abfd, line_ptr);
15975796c8dcSSimon Schubert 	  line_ptr += 1;
15985796c8dcSSimon Schubert 
15995796c8dcSSimon Schubert 	  if (op_code >= lh.opcode_base)
16005796c8dcSSimon Schubert 	    {
16015796c8dcSSimon Schubert 	      /* Special operand.  */
16025796c8dcSSimon Schubert 	      adj_opcode = op_code - lh.opcode_base;
1603cf7f2e2dSJohn Marino 	      if (lh.maximum_ops_per_insn == 1)
1604*ef5ccd6cSJohn Marino 		address += (adj_opcode / lh.line_range
1605*ef5ccd6cSJohn Marino 			    * lh.minimum_instruction_length);
1606cf7f2e2dSJohn Marino 	      else
1607cf7f2e2dSJohn Marino 		{
1608*ef5ccd6cSJohn Marino 		  address += ((op_index + adj_opcode / lh.line_range)
1609*ef5ccd6cSJohn Marino 			      / lh.maximum_ops_per_insn
1610*ef5ccd6cSJohn Marino 			      * lh.minimum_instruction_length);
1611*ef5ccd6cSJohn Marino 		  op_index = ((op_index + adj_opcode / lh.line_range)
1612*ef5ccd6cSJohn Marino 			      % lh.maximum_ops_per_insn);
1613cf7f2e2dSJohn Marino 		}
16145796c8dcSSimon Schubert 	      line += lh.line_base + (adj_opcode % lh.line_range);
16155796c8dcSSimon Schubert 	      /* Append row to matrix using current values.  */
1616cf7f2e2dSJohn Marino 	      if (!add_line_info (table, address, op_index, filename,
1617*ef5ccd6cSJohn Marino 				  line, column, discriminator, 0))
1618cf7f2e2dSJohn Marino 		goto line_fail;
1619*ef5ccd6cSJohn Marino               discriminator = 0;
16205796c8dcSSimon Schubert 	      if (address < low_pc)
16215796c8dcSSimon Schubert 		low_pc = address;
16225796c8dcSSimon Schubert 	      if (address > high_pc)
16235796c8dcSSimon Schubert 		high_pc = address;
16245796c8dcSSimon Schubert 	    }
16255796c8dcSSimon Schubert 	  else switch (op_code)
16265796c8dcSSimon Schubert 	    {
16275796c8dcSSimon Schubert 	    case DW_LNS_extended_op:
1628*ef5ccd6cSJohn Marino 	      exop_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1629*ef5ccd6cSJohn Marino 	      line_ptr += bytes_read;
16305796c8dcSSimon Schubert 	      extended_op = read_1_byte (abfd, line_ptr);
16315796c8dcSSimon Schubert 	      line_ptr += 1;
16325796c8dcSSimon Schubert 
16335796c8dcSSimon Schubert 	      switch (extended_op)
16345796c8dcSSimon Schubert 		{
16355796c8dcSSimon Schubert 		case DW_LNE_end_sequence:
16365796c8dcSSimon Schubert 		  end_sequence = 1;
1637*ef5ccd6cSJohn Marino 		  if (!add_line_info (table, address, op_index, filename, line,
1638*ef5ccd6cSJohn Marino 				      column, discriminator, end_sequence))
1639cf7f2e2dSJohn Marino 		    goto line_fail;
1640*ef5ccd6cSJohn Marino                   discriminator = 0;
16415796c8dcSSimon Schubert 		  if (address < low_pc)
16425796c8dcSSimon Schubert 		    low_pc = address;
16435796c8dcSSimon Schubert 		  if (address > high_pc)
16445796c8dcSSimon Schubert 		    high_pc = address;
1645*ef5ccd6cSJohn Marino 		  if (!arange_add (unit, &unit->arange, low_pc, high_pc))
1646cf7f2e2dSJohn Marino 		    goto line_fail;
16475796c8dcSSimon Schubert 		  break;
16485796c8dcSSimon Schubert 		case DW_LNE_set_address:
16495796c8dcSSimon Schubert 		  address = read_address (unit, line_ptr);
1650cf7f2e2dSJohn Marino 		  op_index = 0;
16515796c8dcSSimon Schubert 		  line_ptr += unit->addr_size;
16525796c8dcSSimon Schubert 		  break;
16535796c8dcSSimon Schubert 		case DW_LNE_define_file:
16545796c8dcSSimon Schubert 		  cur_file = read_string (abfd, line_ptr, &bytes_read);
16555796c8dcSSimon Schubert 		  line_ptr += bytes_read;
16565796c8dcSSimon Schubert 		  if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
16575796c8dcSSimon Schubert 		    {
16585796c8dcSSimon Schubert 		      struct fileinfo *tmp;
16595796c8dcSSimon Schubert 
16605796c8dcSSimon Schubert 		      amt = table->num_files + FILE_ALLOC_CHUNK;
16615796c8dcSSimon Schubert 		      amt *= sizeof (struct fileinfo);
16625796c8dcSSimon Schubert 		      tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
16635796c8dcSSimon Schubert 		      if (tmp == NULL)
1664cf7f2e2dSJohn Marino 			goto line_fail;
16655796c8dcSSimon Schubert 		      table->files = tmp;
16665796c8dcSSimon Schubert 		    }
16675796c8dcSSimon Schubert 		  table->files[table->num_files].name = cur_file;
16685796c8dcSSimon Schubert 		  table->files[table->num_files].dir =
16695796c8dcSSimon Schubert 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16705796c8dcSSimon Schubert 		  line_ptr += bytes_read;
16715796c8dcSSimon Schubert 		  table->files[table->num_files].time =
16725796c8dcSSimon Schubert 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16735796c8dcSSimon Schubert 		  line_ptr += bytes_read;
16745796c8dcSSimon Schubert 		  table->files[table->num_files].size =
16755796c8dcSSimon Schubert 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16765796c8dcSSimon Schubert 		  line_ptr += bytes_read;
16775796c8dcSSimon Schubert 		  table->num_files++;
16785796c8dcSSimon Schubert 		  break;
16795796c8dcSSimon Schubert 		case DW_LNE_set_discriminator:
1680*ef5ccd6cSJohn Marino 		  discriminator =
1681*ef5ccd6cSJohn Marino                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16825796c8dcSSimon Schubert 		  line_ptr += bytes_read;
16835796c8dcSSimon Schubert 		  break;
1684*ef5ccd6cSJohn Marino 		case DW_LNE_HP_source_file_correlation:
1685*ef5ccd6cSJohn Marino 		  line_ptr += exop_len - 1;
1686*ef5ccd6cSJohn Marino 		  break;
16875796c8dcSSimon Schubert 		default:
1688*ef5ccd6cSJohn Marino 		  (*_bfd_error_handler)
1689*ef5ccd6cSJohn Marino 		    (_("Dwarf Error: mangled line number section."));
16905796c8dcSSimon Schubert 		  bfd_set_error (bfd_error_bad_value);
1691cf7f2e2dSJohn Marino 		line_fail:
1692cf7f2e2dSJohn Marino 		  if (filename != NULL)
16935796c8dcSSimon Schubert 		    free (filename);
1694cf7f2e2dSJohn Marino 		  goto fail;
16955796c8dcSSimon Schubert 		}
16965796c8dcSSimon Schubert 	      break;
16975796c8dcSSimon Schubert 	    case DW_LNS_copy:
1698cf7f2e2dSJohn Marino 	      if (!add_line_info (table, address, op_index,
1699*ef5ccd6cSJohn Marino 				  filename, line, column, discriminator, 0))
1700cf7f2e2dSJohn Marino 		goto line_fail;
1701*ef5ccd6cSJohn Marino               discriminator = 0;
17025796c8dcSSimon Schubert 	      if (address < low_pc)
17035796c8dcSSimon Schubert 		low_pc = address;
17045796c8dcSSimon Schubert 	      if (address > high_pc)
17055796c8dcSSimon Schubert 		high_pc = address;
17065796c8dcSSimon Schubert 	      break;
17075796c8dcSSimon Schubert 	    case DW_LNS_advance_pc:
1708cf7f2e2dSJohn Marino 	      if (lh.maximum_ops_per_insn == 1)
1709*ef5ccd6cSJohn Marino 		address += (lh.minimum_instruction_length
1710cf7f2e2dSJohn Marino 			    * read_unsigned_leb128 (abfd, line_ptr,
1711*ef5ccd6cSJohn Marino 						    &bytes_read));
1712cf7f2e2dSJohn Marino 	      else
1713cf7f2e2dSJohn Marino 		{
1714cf7f2e2dSJohn Marino 		  bfd_vma adjust = read_unsigned_leb128 (abfd, line_ptr,
1715cf7f2e2dSJohn Marino 							 &bytes_read);
1716*ef5ccd6cSJohn Marino 		  address = ((op_index + adjust) / lh.maximum_ops_per_insn
1717*ef5ccd6cSJohn Marino 			     * lh.minimum_instruction_length);
1718cf7f2e2dSJohn Marino 		  op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
1719cf7f2e2dSJohn Marino 		}
17205796c8dcSSimon Schubert 	      line_ptr += bytes_read;
17215796c8dcSSimon Schubert 	      break;
17225796c8dcSSimon Schubert 	    case DW_LNS_advance_line:
17235796c8dcSSimon Schubert 	      line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
17245796c8dcSSimon Schubert 	      line_ptr += bytes_read;
17255796c8dcSSimon Schubert 	      break;
17265796c8dcSSimon Schubert 	    case DW_LNS_set_file:
17275796c8dcSSimon Schubert 	      {
17285796c8dcSSimon Schubert 		unsigned int file;
17295796c8dcSSimon Schubert 
17305796c8dcSSimon Schubert 		/* The file and directory tables are 0
17315796c8dcSSimon Schubert 		   based, the references are 1 based.  */
17325796c8dcSSimon Schubert 		file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17335796c8dcSSimon Schubert 		line_ptr += bytes_read;
17345796c8dcSSimon Schubert 		if (filename)
17355796c8dcSSimon Schubert 		  free (filename);
17365796c8dcSSimon Schubert 		filename = concat_filename (table, file);
17375796c8dcSSimon Schubert 		break;
17385796c8dcSSimon Schubert 	      }
17395796c8dcSSimon Schubert 	    case DW_LNS_set_column:
17405796c8dcSSimon Schubert 	      column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17415796c8dcSSimon Schubert 	      line_ptr += bytes_read;
17425796c8dcSSimon Schubert 	      break;
17435796c8dcSSimon Schubert 	    case DW_LNS_negate_stmt:
17445796c8dcSSimon Schubert 	      is_stmt = (!is_stmt);
17455796c8dcSSimon Schubert 	      break;
17465796c8dcSSimon Schubert 	    case DW_LNS_set_basic_block:
17475796c8dcSSimon Schubert 	      break;
17485796c8dcSSimon Schubert 	    case DW_LNS_const_add_pc:
1749cf7f2e2dSJohn Marino 	      if (lh.maximum_ops_per_insn == 1)
1750*ef5ccd6cSJohn Marino 		address += (lh.minimum_instruction_length
1751*ef5ccd6cSJohn Marino 			    * ((255 - lh.opcode_base) / lh.line_range));
1752cf7f2e2dSJohn Marino 	      else
1753cf7f2e2dSJohn Marino 		{
1754cf7f2e2dSJohn Marino 		  bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
1755*ef5ccd6cSJohn Marino 		  address += (lh.minimum_instruction_length
1756*ef5ccd6cSJohn Marino 			      * ((op_index + adjust)
1757*ef5ccd6cSJohn Marino 				 / lh.maximum_ops_per_insn));
1758cf7f2e2dSJohn Marino 		  op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
1759cf7f2e2dSJohn Marino 		}
17605796c8dcSSimon Schubert 	      break;
17615796c8dcSSimon Schubert 	    case DW_LNS_fixed_advance_pc:
17625796c8dcSSimon Schubert 	      address += read_2_bytes (abfd, line_ptr);
1763cf7f2e2dSJohn Marino 	      op_index = 0;
17645796c8dcSSimon Schubert 	      line_ptr += 2;
17655796c8dcSSimon Schubert 	      break;
17665796c8dcSSimon Schubert 	    default:
17675796c8dcSSimon Schubert 	      /* Unknown standard opcode, ignore it.  */
17685796c8dcSSimon Schubert 	      for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
17695796c8dcSSimon Schubert 		{
17705796c8dcSSimon Schubert 		  (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17715796c8dcSSimon Schubert 		  line_ptr += bytes_read;
17725796c8dcSSimon Schubert 		}
1773cf7f2e2dSJohn Marino 	      break;
17745796c8dcSSimon Schubert 	    }
17755796c8dcSSimon Schubert 	}
17765796c8dcSSimon Schubert 
17775796c8dcSSimon Schubert       if (filename)
17785796c8dcSSimon Schubert 	free (filename);
17795796c8dcSSimon Schubert     }
17805796c8dcSSimon Schubert 
1781cf7f2e2dSJohn Marino   if (sort_line_sequences (table))
17825796c8dcSSimon Schubert     return table;
1783cf7f2e2dSJohn Marino 
1784cf7f2e2dSJohn Marino  fail:
1785cf7f2e2dSJohn Marino   if (table->sequences != NULL)
1786cf7f2e2dSJohn Marino     free (table->sequences);
1787cf7f2e2dSJohn Marino   if (table->files != NULL)
1788cf7f2e2dSJohn Marino     free (table->files);
1789cf7f2e2dSJohn Marino   if (table->dirs != NULL)
1790cf7f2e2dSJohn Marino     free (table->dirs);
1791cf7f2e2dSJohn Marino   return NULL;
17925796c8dcSSimon Schubert }
17935796c8dcSSimon Schubert 
17945796c8dcSSimon Schubert /* If ADDR is within TABLE set the output parameters and return TRUE,
17955796c8dcSSimon Schubert    otherwise return FALSE.  The output parameters, FILENAME_PTR and
17965796c8dcSSimon Schubert    LINENUMBER_PTR, are pointers to the objects to be filled in.  */
17975796c8dcSSimon Schubert 
17985796c8dcSSimon Schubert static bfd_boolean
lookup_address_in_line_info_table(struct line_info_table * table,bfd_vma addr,const char ** filename_ptr,unsigned int * linenumber_ptr,unsigned int * discriminator_ptr)17995796c8dcSSimon Schubert lookup_address_in_line_info_table (struct line_info_table *table,
18005796c8dcSSimon Schubert 				   bfd_vma addr,
18015796c8dcSSimon Schubert 				   const char **filename_ptr,
1802*ef5ccd6cSJohn Marino 				   unsigned int *linenumber_ptr,
1803*ef5ccd6cSJohn Marino 				   unsigned int *discriminator_ptr)
18045796c8dcSSimon Schubert {
1805cf7f2e2dSJohn Marino   struct line_sequence *seq = NULL;
1806cf7f2e2dSJohn Marino   struct line_info *each_line;
1807cf7f2e2dSJohn Marino   int low, high, mid;
18085796c8dcSSimon Schubert 
1809cf7f2e2dSJohn Marino   /* Binary search the array of sequences.  */
1810cf7f2e2dSJohn Marino   low = 0;
1811cf7f2e2dSJohn Marino   high = table->num_sequences;
1812cf7f2e2dSJohn Marino   while (low < high)
18135796c8dcSSimon Schubert     {
1814cf7f2e2dSJohn Marino       mid = (low + high) / 2;
1815cf7f2e2dSJohn Marino       seq = &table->sequences[mid];
1816cf7f2e2dSJohn Marino       if (addr < seq->low_pc)
1817cf7f2e2dSJohn Marino 	high = mid;
1818cf7f2e2dSJohn Marino       else if (addr >= seq->last_line->address)
1819cf7f2e2dSJohn Marino 	low = mid + 1;
18205796c8dcSSimon Schubert       else
1821cf7f2e2dSJohn Marino 	break;
1822cf7f2e2dSJohn Marino     }
1823cf7f2e2dSJohn Marino 
1824cf7f2e2dSJohn Marino   if (seq && addr >= seq->low_pc && addr < seq->last_line->address)
1825cf7f2e2dSJohn Marino     {
1826cf7f2e2dSJohn Marino       /* Note: seq->last_line should be a descendingly sorted list.  */
1827cf7f2e2dSJohn Marino       for (each_line = seq->last_line;
1828cf7f2e2dSJohn Marino            each_line;
1829cf7f2e2dSJohn Marino            each_line = each_line->prev_line)
1830cf7f2e2dSJohn Marino         if (addr >= each_line->address)
1831cf7f2e2dSJohn Marino           break;
1832cf7f2e2dSJohn Marino 
1833cf7f2e2dSJohn Marino       if (each_line
1834cf7f2e2dSJohn Marino           && !(each_line->end_sequence || each_line == seq->last_line))
18355796c8dcSSimon Schubert         {
18365796c8dcSSimon Schubert           *filename_ptr = each_line->filename;
18375796c8dcSSimon Schubert           *linenumber_ptr = each_line->line;
1838*ef5ccd6cSJohn Marino           if (discriminator_ptr)
1839*ef5ccd6cSJohn Marino             *discriminator_ptr = each_line->discriminator;
18405796c8dcSSimon Schubert           return TRUE;
18415796c8dcSSimon Schubert         }
1842cf7f2e2dSJohn Marino     }
18435796c8dcSSimon Schubert 
1844cf7f2e2dSJohn Marino   *filename_ptr = NULL;
18455796c8dcSSimon Schubert   return FALSE;
18465796c8dcSSimon Schubert }
18475796c8dcSSimon Schubert 
1848cf7f2e2dSJohn Marino /* Read in the .debug_ranges section for future reference.  */
18495796c8dcSSimon Schubert 
18505796c8dcSSimon Schubert static bfd_boolean
read_debug_ranges(struct comp_unit * unit)18515796c8dcSSimon Schubert read_debug_ranges (struct comp_unit *unit)
18525796c8dcSSimon Schubert {
18535796c8dcSSimon Schubert   struct dwarf2_debug *stash = unit->stash;
1854a45ae5f8SJohn Marino   return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
1855a45ae5f8SJohn Marino                        stash->syms, 0,
18565796c8dcSSimon Schubert 		       &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
18575796c8dcSSimon Schubert }
18585796c8dcSSimon Schubert 
18595796c8dcSSimon Schubert /* Function table functions.  */
18605796c8dcSSimon Schubert 
18615796c8dcSSimon Schubert /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
18625796c8dcSSimon Schubert    Note that we need to find the function that has the smallest
18635796c8dcSSimon Schubert    range that contains ADDR, to handle inlined functions without
18645796c8dcSSimon Schubert    depending upon them being ordered in TABLE by increasing range. */
18655796c8dcSSimon Schubert 
18665796c8dcSSimon Schubert static bfd_boolean
lookup_address_in_function_table(struct comp_unit * unit,bfd_vma addr,struct funcinfo ** function_ptr,const char ** functionname_ptr)18675796c8dcSSimon Schubert lookup_address_in_function_table (struct comp_unit *unit,
18685796c8dcSSimon Schubert 				  bfd_vma addr,
18695796c8dcSSimon Schubert 				  struct funcinfo **function_ptr,
18705796c8dcSSimon Schubert 				  const char **functionname_ptr)
18715796c8dcSSimon Schubert {
18725796c8dcSSimon Schubert   struct funcinfo* each_func;
18735796c8dcSSimon Schubert   struct funcinfo* best_fit = NULL;
18745796c8dcSSimon Schubert   struct arange *arange;
18755796c8dcSSimon Schubert 
18765796c8dcSSimon Schubert   for (each_func = unit->function_table;
18775796c8dcSSimon Schubert        each_func;
18785796c8dcSSimon Schubert        each_func = each_func->prev_func)
18795796c8dcSSimon Schubert     {
18805796c8dcSSimon Schubert       for (arange = &each_func->arange;
18815796c8dcSSimon Schubert 	   arange;
18825796c8dcSSimon Schubert 	   arange = arange->next)
18835796c8dcSSimon Schubert 	{
18845796c8dcSSimon Schubert 	  if (addr >= arange->low && addr < arange->high)
18855796c8dcSSimon Schubert 	    {
1886*ef5ccd6cSJohn Marino 	      if (!best_fit
1887*ef5ccd6cSJohn Marino 		  || (arange->high - arange->low
1888*ef5ccd6cSJohn Marino 		      < best_fit->arange.high - best_fit->arange.low))
18895796c8dcSSimon Schubert 		best_fit = each_func;
18905796c8dcSSimon Schubert 	    }
18915796c8dcSSimon Schubert 	}
18925796c8dcSSimon Schubert     }
18935796c8dcSSimon Schubert 
18945796c8dcSSimon Schubert   if (best_fit)
18955796c8dcSSimon Schubert     {
18965796c8dcSSimon Schubert       *functionname_ptr = best_fit->name;
18975796c8dcSSimon Schubert       *function_ptr = best_fit;
18985796c8dcSSimon Schubert       return TRUE;
18995796c8dcSSimon Schubert     }
19005796c8dcSSimon Schubert   else
19015796c8dcSSimon Schubert     {
19025796c8dcSSimon Schubert       return FALSE;
19035796c8dcSSimon Schubert     }
19045796c8dcSSimon Schubert }
19055796c8dcSSimon Schubert 
19065796c8dcSSimon Schubert /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
19075796c8dcSSimon Schubert    and LINENUMBER_PTR, and return TRUE.  */
19085796c8dcSSimon Schubert 
19095796c8dcSSimon Schubert static bfd_boolean
lookup_symbol_in_function_table(struct comp_unit * unit,asymbol * sym,bfd_vma addr,const char ** filename_ptr,unsigned int * linenumber_ptr)19105796c8dcSSimon Schubert lookup_symbol_in_function_table (struct comp_unit *unit,
19115796c8dcSSimon Schubert 				 asymbol *sym,
19125796c8dcSSimon Schubert 				 bfd_vma addr,
19135796c8dcSSimon Schubert 				 const char **filename_ptr,
19145796c8dcSSimon Schubert 				 unsigned int *linenumber_ptr)
19155796c8dcSSimon Schubert {
19165796c8dcSSimon Schubert   struct funcinfo* each_func;
19175796c8dcSSimon Schubert   struct funcinfo* best_fit = NULL;
19185796c8dcSSimon Schubert   struct arange *arange;
19195796c8dcSSimon Schubert   const char *name = bfd_asymbol_name (sym);
19205796c8dcSSimon Schubert   asection *sec = bfd_get_section (sym);
19215796c8dcSSimon Schubert 
19225796c8dcSSimon Schubert   for (each_func = unit->function_table;
19235796c8dcSSimon Schubert        each_func;
19245796c8dcSSimon Schubert        each_func = each_func->prev_func)
19255796c8dcSSimon Schubert     {
19265796c8dcSSimon Schubert       for (arange = &each_func->arange;
19275796c8dcSSimon Schubert 	   arange;
19285796c8dcSSimon Schubert 	   arange = arange->next)
19295796c8dcSSimon Schubert 	{
19305796c8dcSSimon Schubert 	  if ((!each_func->sec || each_func->sec == sec)
19315796c8dcSSimon Schubert 	      && addr >= arange->low
19325796c8dcSSimon Schubert 	      && addr < arange->high
19335796c8dcSSimon Schubert 	      && each_func->name
19345796c8dcSSimon Schubert 	      && strcmp (name, each_func->name) == 0
19355796c8dcSSimon Schubert 	      && (!best_fit
1936*ef5ccd6cSJohn Marino 		  || (arange->high - arange->low
1937*ef5ccd6cSJohn Marino 		      < best_fit->arange.high - best_fit->arange.low)))
19385796c8dcSSimon Schubert 	    best_fit = each_func;
19395796c8dcSSimon Schubert 	}
19405796c8dcSSimon Schubert     }
19415796c8dcSSimon Schubert 
19425796c8dcSSimon Schubert   if (best_fit)
19435796c8dcSSimon Schubert     {
19445796c8dcSSimon Schubert       best_fit->sec = sec;
19455796c8dcSSimon Schubert       *filename_ptr = best_fit->file;
19465796c8dcSSimon Schubert       *linenumber_ptr = best_fit->line;
19475796c8dcSSimon Schubert       return TRUE;
19485796c8dcSSimon Schubert     }
19495796c8dcSSimon Schubert   else
19505796c8dcSSimon Schubert     return FALSE;
19515796c8dcSSimon Schubert }
19525796c8dcSSimon Schubert 
19535796c8dcSSimon Schubert /* Variable table functions.  */
19545796c8dcSSimon Schubert 
19555796c8dcSSimon Schubert /* If SYM is within variable table of UNIT, set FILENAME_PTR and
19565796c8dcSSimon Schubert    LINENUMBER_PTR, and return TRUE.  */
19575796c8dcSSimon Schubert 
19585796c8dcSSimon Schubert static bfd_boolean
lookup_symbol_in_variable_table(struct comp_unit * unit,asymbol * sym,bfd_vma addr,const char ** filename_ptr,unsigned int * linenumber_ptr)19595796c8dcSSimon Schubert lookup_symbol_in_variable_table (struct comp_unit *unit,
19605796c8dcSSimon Schubert 				 asymbol *sym,
19615796c8dcSSimon Schubert 				 bfd_vma addr,
19625796c8dcSSimon Schubert 				 const char **filename_ptr,
19635796c8dcSSimon Schubert 				 unsigned int *linenumber_ptr)
19645796c8dcSSimon Schubert {
19655796c8dcSSimon Schubert   const char *name = bfd_asymbol_name (sym);
19665796c8dcSSimon Schubert   asection *sec = bfd_get_section (sym);
19675796c8dcSSimon Schubert   struct varinfo* each;
19685796c8dcSSimon Schubert 
19695796c8dcSSimon Schubert   for (each = unit->variable_table; each; each = each->prev_var)
19705796c8dcSSimon Schubert     if (each->stack == 0
19715796c8dcSSimon Schubert 	&& each->file != NULL
19725796c8dcSSimon Schubert 	&& each->name != NULL
19735796c8dcSSimon Schubert 	&& each->addr == addr
19745796c8dcSSimon Schubert 	&& (!each->sec || each->sec == sec)
19755796c8dcSSimon Schubert 	&& strcmp (name, each->name) == 0)
19765796c8dcSSimon Schubert       break;
19775796c8dcSSimon Schubert 
19785796c8dcSSimon Schubert   if (each)
19795796c8dcSSimon Schubert     {
19805796c8dcSSimon Schubert       each->sec = sec;
19815796c8dcSSimon Schubert       *filename_ptr = each->file;
19825796c8dcSSimon Schubert       *linenumber_ptr = each->line;
19835796c8dcSSimon Schubert       return TRUE;
19845796c8dcSSimon Schubert     }
19855796c8dcSSimon Schubert   else
19865796c8dcSSimon Schubert     return FALSE;
19875796c8dcSSimon Schubert }
19885796c8dcSSimon Schubert 
19895796c8dcSSimon Schubert static char *
find_abstract_instance_name(struct comp_unit * unit,struct attribute * attr_ptr)19905796c8dcSSimon Schubert find_abstract_instance_name (struct comp_unit *unit,
19915796c8dcSSimon Schubert 			     struct attribute *attr_ptr)
19925796c8dcSSimon Schubert {
19935796c8dcSSimon Schubert   bfd *abfd = unit->abfd;
19945796c8dcSSimon Schubert   bfd_byte *info_ptr;
19955796c8dcSSimon Schubert   unsigned int abbrev_number, bytes_read, i;
19965796c8dcSSimon Schubert   struct abbrev_info *abbrev;
19975796c8dcSSimon Schubert   bfd_uint64_t die_ref = attr_ptr->u.val;
19985796c8dcSSimon Schubert   struct attribute attr;
19995796c8dcSSimon Schubert   char *name = 0;
20005796c8dcSSimon Schubert 
20015796c8dcSSimon Schubert   /* DW_FORM_ref_addr can reference an entry in a different CU. It
20025796c8dcSSimon Schubert      is an offset from the .debug_info section, not the current CU.  */
20035796c8dcSSimon Schubert   if (attr_ptr->form == DW_FORM_ref_addr)
20045796c8dcSSimon Schubert     {
20055796c8dcSSimon Schubert       /* We only support DW_FORM_ref_addr within the same file, so
20065796c8dcSSimon Schubert 	 any relocations should be resolved already.  */
20075796c8dcSSimon Schubert       if (!die_ref)
20085796c8dcSSimon Schubert 	abort ();
20095796c8dcSSimon Schubert 
2010cf7f2e2dSJohn Marino       info_ptr = unit->sec_info_ptr + die_ref;
20115796c8dcSSimon Schubert     }
20125796c8dcSSimon Schubert   else
20135796c8dcSSimon Schubert     info_ptr = unit->info_ptr_unit + die_ref;
20145796c8dcSSimon Schubert   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20155796c8dcSSimon Schubert   info_ptr += bytes_read;
20165796c8dcSSimon Schubert 
20175796c8dcSSimon Schubert   if (abbrev_number)
20185796c8dcSSimon Schubert     {
20195796c8dcSSimon Schubert       abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
20205796c8dcSSimon Schubert       if (! abbrev)
20215796c8dcSSimon Schubert 	{
2022*ef5ccd6cSJohn Marino 	  (*_bfd_error_handler)
2023*ef5ccd6cSJohn Marino 	    (_("Dwarf Error: Could not find abbrev number %u."), abbrev_number);
20245796c8dcSSimon Schubert 	  bfd_set_error (bfd_error_bad_value);
20255796c8dcSSimon Schubert 	}
20265796c8dcSSimon Schubert       else
20275796c8dcSSimon Schubert 	{
20285796c8dcSSimon Schubert 	  for (i = 0; i < abbrev->num_attrs; ++i)
20295796c8dcSSimon Schubert 	    {
2030cf7f2e2dSJohn Marino 	      info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
2031cf7f2e2dSJohn Marino 					 info_ptr);
2032cf7f2e2dSJohn Marino 	      if (info_ptr == NULL)
2033cf7f2e2dSJohn Marino 		break;
20345796c8dcSSimon Schubert 	      switch (attr.name)
20355796c8dcSSimon Schubert 		{
20365796c8dcSSimon Schubert 		case DW_AT_name:
2037cf7f2e2dSJohn Marino 		  /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2038cf7f2e2dSJohn Marino 		     over DW_AT_name.  */
20395796c8dcSSimon Schubert 		  if (name == NULL)
20405796c8dcSSimon Schubert 		    name = attr.u.str;
20415796c8dcSSimon Schubert 		  break;
20425796c8dcSSimon Schubert 		case DW_AT_specification:
20435796c8dcSSimon Schubert 		  name = find_abstract_instance_name (unit, &attr);
20445796c8dcSSimon Schubert 		  break;
2045cf7f2e2dSJohn Marino 		case DW_AT_linkage_name:
20465796c8dcSSimon Schubert 		case DW_AT_MIPS_linkage_name:
20475796c8dcSSimon Schubert 		  name = attr.u.str;
20485796c8dcSSimon Schubert 		  break;
20495796c8dcSSimon Schubert 		default:
20505796c8dcSSimon Schubert 		  break;
20515796c8dcSSimon Schubert 		}
20525796c8dcSSimon Schubert 	    }
20535796c8dcSSimon Schubert 	}
20545796c8dcSSimon Schubert     }
2055cf7f2e2dSJohn Marino   return name;
20565796c8dcSSimon Schubert }
20575796c8dcSSimon Schubert 
2058cf7f2e2dSJohn Marino static bfd_boolean
read_rangelist(struct comp_unit * unit,struct arange * arange,bfd_uint64_t offset)2059cf7f2e2dSJohn Marino read_rangelist (struct comp_unit *unit, struct arange *arange,
2060cf7f2e2dSJohn Marino 		bfd_uint64_t offset)
20615796c8dcSSimon Schubert {
20625796c8dcSSimon Schubert   bfd_byte *ranges_ptr;
20635796c8dcSSimon Schubert   bfd_vma base_address = unit->base_address;
20645796c8dcSSimon Schubert 
20655796c8dcSSimon Schubert   if (! unit->stash->dwarf_ranges_buffer)
20665796c8dcSSimon Schubert     {
20675796c8dcSSimon Schubert       if (! read_debug_ranges (unit))
2068cf7f2e2dSJohn Marino 	return FALSE;
20695796c8dcSSimon Schubert     }
20705796c8dcSSimon Schubert   ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
20715796c8dcSSimon Schubert 
20725796c8dcSSimon Schubert   for (;;)
20735796c8dcSSimon Schubert     {
20745796c8dcSSimon Schubert       bfd_vma low_pc;
20755796c8dcSSimon Schubert       bfd_vma high_pc;
20765796c8dcSSimon Schubert 
20775796c8dcSSimon Schubert       low_pc = read_address (unit, ranges_ptr);
20785796c8dcSSimon Schubert       ranges_ptr += unit->addr_size;
20795796c8dcSSimon Schubert       high_pc = read_address (unit, ranges_ptr);
20805796c8dcSSimon Schubert       ranges_ptr += unit->addr_size;
20815796c8dcSSimon Schubert 
20825796c8dcSSimon Schubert       if (low_pc == 0 && high_pc == 0)
20835796c8dcSSimon Schubert 	break;
20845796c8dcSSimon Schubert       if (low_pc == -1UL && high_pc != -1UL)
20855796c8dcSSimon Schubert 	base_address = high_pc;
20865796c8dcSSimon Schubert       else
2087cf7f2e2dSJohn Marino 	{
2088*ef5ccd6cSJohn Marino 	  if (!arange_add (unit, arange,
2089cf7f2e2dSJohn Marino 			   base_address + low_pc, base_address + high_pc))
2090cf7f2e2dSJohn Marino 	    return FALSE;
20915796c8dcSSimon Schubert 	}
20925796c8dcSSimon Schubert     }
2093cf7f2e2dSJohn Marino   return TRUE;
2094cf7f2e2dSJohn Marino }
20955796c8dcSSimon Schubert 
20965796c8dcSSimon Schubert /* DWARF2 Compilation unit functions.  */
20975796c8dcSSimon Schubert 
20985796c8dcSSimon Schubert /* Scan over each die in a comp. unit looking for functions to add
20995796c8dcSSimon Schubert    to the function table and variables to the variable table.  */
21005796c8dcSSimon Schubert 
21015796c8dcSSimon Schubert static bfd_boolean
scan_unit_for_symbols(struct comp_unit * unit)21025796c8dcSSimon Schubert scan_unit_for_symbols (struct comp_unit *unit)
21035796c8dcSSimon Schubert {
21045796c8dcSSimon Schubert   bfd *abfd = unit->abfd;
21055796c8dcSSimon Schubert   bfd_byte *info_ptr = unit->first_child_die_ptr;
21065796c8dcSSimon Schubert   int nesting_level = 1;
21075796c8dcSSimon Schubert   struct funcinfo **nested_funcs;
21085796c8dcSSimon Schubert   int nested_funcs_size;
21095796c8dcSSimon Schubert 
21105796c8dcSSimon Schubert   /* Maintain a stack of in-scope functions and inlined functions, which we
21115796c8dcSSimon Schubert      can use to set the caller_func field.  */
21125796c8dcSSimon Schubert   nested_funcs_size = 32;
21135796c8dcSSimon Schubert   nested_funcs = (struct funcinfo **)
21145796c8dcSSimon Schubert     bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
21155796c8dcSSimon Schubert   if (nested_funcs == NULL)
21165796c8dcSSimon Schubert     return FALSE;
21175796c8dcSSimon Schubert   nested_funcs[nesting_level] = 0;
21185796c8dcSSimon Schubert 
21195796c8dcSSimon Schubert   while (nesting_level)
21205796c8dcSSimon Schubert     {
21215796c8dcSSimon Schubert       unsigned int abbrev_number, bytes_read, i;
21225796c8dcSSimon Schubert       struct abbrev_info *abbrev;
21235796c8dcSSimon Schubert       struct attribute attr;
21245796c8dcSSimon Schubert       struct funcinfo *func;
21255796c8dcSSimon Schubert       struct varinfo *var;
21265796c8dcSSimon Schubert       bfd_vma low_pc = 0;
21275796c8dcSSimon Schubert       bfd_vma high_pc = 0;
2128*ef5ccd6cSJohn Marino       bfd_boolean high_pc_relative = FALSE;
21295796c8dcSSimon Schubert 
21305796c8dcSSimon Schubert       abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
21315796c8dcSSimon Schubert       info_ptr += bytes_read;
21325796c8dcSSimon Schubert 
21335796c8dcSSimon Schubert       if (! abbrev_number)
21345796c8dcSSimon Schubert 	{
21355796c8dcSSimon Schubert 	  nesting_level--;
21365796c8dcSSimon Schubert 	  continue;
21375796c8dcSSimon Schubert 	}
21385796c8dcSSimon Schubert 
21395796c8dcSSimon Schubert       abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
21405796c8dcSSimon Schubert       if (! abbrev)
21415796c8dcSSimon Schubert 	{
2142cf7f2e2dSJohn Marino 	  (*_bfd_error_handler)
2143cf7f2e2dSJohn Marino 	    (_("Dwarf Error: Could not find abbrev number %u."),
21445796c8dcSSimon Schubert 	     abbrev_number);
21455796c8dcSSimon Schubert 	  bfd_set_error (bfd_error_bad_value);
2146cf7f2e2dSJohn Marino 	  goto fail;
21475796c8dcSSimon Schubert 	}
21485796c8dcSSimon Schubert 
21495796c8dcSSimon Schubert       var = NULL;
21505796c8dcSSimon Schubert       if (abbrev->tag == DW_TAG_subprogram
21515796c8dcSSimon Schubert 	  || abbrev->tag == DW_TAG_entry_point
21525796c8dcSSimon Schubert 	  || abbrev->tag == DW_TAG_inlined_subroutine)
21535796c8dcSSimon Schubert 	{
21545796c8dcSSimon Schubert 	  bfd_size_type amt = sizeof (struct funcinfo);
21555796c8dcSSimon Schubert 	  func = (struct funcinfo *) bfd_zalloc (abfd, amt);
2156cf7f2e2dSJohn Marino 	  if (func == NULL)
2157cf7f2e2dSJohn Marino 	    goto fail;
21585796c8dcSSimon Schubert 	  func->tag = abbrev->tag;
21595796c8dcSSimon Schubert 	  func->prev_func = unit->function_table;
21605796c8dcSSimon Schubert 	  unit->function_table = func;
21615796c8dcSSimon Schubert 	  BFD_ASSERT (!unit->cached);
21625796c8dcSSimon Schubert 
21635796c8dcSSimon Schubert 	  if (func->tag == DW_TAG_inlined_subroutine)
21645796c8dcSSimon Schubert 	    for (i = nesting_level - 1; i >= 1; i--)
21655796c8dcSSimon Schubert 	      if (nested_funcs[i])
21665796c8dcSSimon Schubert 		{
21675796c8dcSSimon Schubert 		  func->caller_func = nested_funcs[i];
21685796c8dcSSimon Schubert 		  break;
21695796c8dcSSimon Schubert 		}
21705796c8dcSSimon Schubert 	  nested_funcs[nesting_level] = func;
21715796c8dcSSimon Schubert 	}
21725796c8dcSSimon Schubert       else
21735796c8dcSSimon Schubert 	{
21745796c8dcSSimon Schubert 	  func = NULL;
21755796c8dcSSimon Schubert 	  if (abbrev->tag == DW_TAG_variable)
21765796c8dcSSimon Schubert 	    {
21775796c8dcSSimon Schubert 	      bfd_size_type amt = sizeof (struct varinfo);
21785796c8dcSSimon Schubert 	      var = (struct varinfo *) bfd_zalloc (abfd, amt);
2179cf7f2e2dSJohn Marino 	      if (var == NULL)
2180cf7f2e2dSJohn Marino 		goto fail;
21815796c8dcSSimon Schubert 	      var->tag = abbrev->tag;
21825796c8dcSSimon Schubert 	      var->stack = 1;
21835796c8dcSSimon Schubert 	      var->prev_var = unit->variable_table;
21845796c8dcSSimon Schubert 	      unit->variable_table = var;
21855796c8dcSSimon Schubert 	      BFD_ASSERT (!unit->cached);
21865796c8dcSSimon Schubert 	    }
21875796c8dcSSimon Schubert 
21885796c8dcSSimon Schubert 	  /* No inline function in scope at this nesting level.  */
21895796c8dcSSimon Schubert 	  nested_funcs[nesting_level] = 0;
21905796c8dcSSimon Schubert 	}
21915796c8dcSSimon Schubert 
21925796c8dcSSimon Schubert       for (i = 0; i < abbrev->num_attrs; ++i)
21935796c8dcSSimon Schubert 	{
21945796c8dcSSimon Schubert 	  info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2195cf7f2e2dSJohn Marino 	  if (info_ptr == NULL)
2196c50c785cSJohn Marino 	    goto fail;
21975796c8dcSSimon Schubert 
21985796c8dcSSimon Schubert 	  if (func)
21995796c8dcSSimon Schubert 	    {
22005796c8dcSSimon Schubert 	      switch (attr.name)
22015796c8dcSSimon Schubert 		{
22025796c8dcSSimon Schubert 		case DW_AT_call_file:
2203cf7f2e2dSJohn Marino 		  func->caller_file = concat_filename (unit->line_table,
2204cf7f2e2dSJohn Marino 						       attr.u.val);
22055796c8dcSSimon Schubert 		  break;
22065796c8dcSSimon Schubert 
22075796c8dcSSimon Schubert 		case DW_AT_call_line:
22085796c8dcSSimon Schubert 		  func->caller_line = attr.u.val;
22095796c8dcSSimon Schubert 		  break;
22105796c8dcSSimon Schubert 
22115796c8dcSSimon Schubert 		case DW_AT_abstract_origin:
2212a45ae5f8SJohn Marino 		case DW_AT_specification:
22135796c8dcSSimon Schubert 		  func->name = find_abstract_instance_name (unit, &attr);
22145796c8dcSSimon Schubert 		  break;
22155796c8dcSSimon Schubert 
22165796c8dcSSimon Schubert 		case DW_AT_name:
2217cf7f2e2dSJohn Marino 		  /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2218cf7f2e2dSJohn Marino 		     over DW_AT_name.  */
22195796c8dcSSimon Schubert 		  if (func->name == NULL)
22205796c8dcSSimon Schubert 		    func->name = attr.u.str;
22215796c8dcSSimon Schubert 		  break;
22225796c8dcSSimon Schubert 
2223cf7f2e2dSJohn Marino 		case DW_AT_linkage_name:
22245796c8dcSSimon Schubert 		case DW_AT_MIPS_linkage_name:
22255796c8dcSSimon Schubert 		  func->name = attr.u.str;
22265796c8dcSSimon Schubert 		  break;
22275796c8dcSSimon Schubert 
22285796c8dcSSimon Schubert 		case DW_AT_low_pc:
22295796c8dcSSimon Schubert 		  low_pc = attr.u.val;
22305796c8dcSSimon Schubert 		  break;
22315796c8dcSSimon Schubert 
22325796c8dcSSimon Schubert 		case DW_AT_high_pc:
22335796c8dcSSimon Schubert 		  high_pc = attr.u.val;
2234*ef5ccd6cSJohn Marino 		  high_pc_relative = attr.form != DW_FORM_addr;
22355796c8dcSSimon Schubert 		  break;
22365796c8dcSSimon Schubert 
22375796c8dcSSimon Schubert 		case DW_AT_ranges:
2238cf7f2e2dSJohn Marino 		  if (!read_rangelist (unit, &func->arange, attr.u.val))
2239cf7f2e2dSJohn Marino 		    goto fail;
22405796c8dcSSimon Schubert 		  break;
22415796c8dcSSimon Schubert 
22425796c8dcSSimon Schubert 		case DW_AT_decl_file:
22435796c8dcSSimon Schubert 		  func->file = concat_filename (unit->line_table,
22445796c8dcSSimon Schubert 						attr.u.val);
22455796c8dcSSimon Schubert 		  break;
22465796c8dcSSimon Schubert 
22475796c8dcSSimon Schubert 		case DW_AT_decl_line:
22485796c8dcSSimon Schubert 		  func->line = attr.u.val;
22495796c8dcSSimon Schubert 		  break;
22505796c8dcSSimon Schubert 
22515796c8dcSSimon Schubert 		default:
22525796c8dcSSimon Schubert 		  break;
22535796c8dcSSimon Schubert 		}
22545796c8dcSSimon Schubert 	    }
22555796c8dcSSimon Schubert 	  else if (var)
22565796c8dcSSimon Schubert 	    {
22575796c8dcSSimon Schubert 	      switch (attr.name)
22585796c8dcSSimon Schubert 		{
22595796c8dcSSimon Schubert 		case DW_AT_name:
22605796c8dcSSimon Schubert 		  var->name = attr.u.str;
22615796c8dcSSimon Schubert 		  break;
22625796c8dcSSimon Schubert 
22635796c8dcSSimon Schubert 		case DW_AT_decl_file:
22645796c8dcSSimon Schubert 		  var->file = concat_filename (unit->line_table,
22655796c8dcSSimon Schubert 					       attr.u.val);
22665796c8dcSSimon Schubert 		  break;
22675796c8dcSSimon Schubert 
22685796c8dcSSimon Schubert 		case DW_AT_decl_line:
22695796c8dcSSimon Schubert 		  var->line = attr.u.val;
22705796c8dcSSimon Schubert 		  break;
22715796c8dcSSimon Schubert 
22725796c8dcSSimon Schubert 		case DW_AT_external:
22735796c8dcSSimon Schubert 		  if (attr.u.val != 0)
22745796c8dcSSimon Schubert 		    var->stack = 0;
22755796c8dcSSimon Schubert 		  break;
22765796c8dcSSimon Schubert 
22775796c8dcSSimon Schubert 		case DW_AT_location:
22785796c8dcSSimon Schubert 		  switch (attr.form)
22795796c8dcSSimon Schubert 		    {
22805796c8dcSSimon Schubert 		    case DW_FORM_block:
22815796c8dcSSimon Schubert 		    case DW_FORM_block1:
22825796c8dcSSimon Schubert 		    case DW_FORM_block2:
22835796c8dcSSimon Schubert 		    case DW_FORM_block4:
2284cf7f2e2dSJohn Marino 		    case DW_FORM_exprloc:
22855796c8dcSSimon Schubert 		      if (*attr.u.blk->data == DW_OP_addr)
22865796c8dcSSimon Schubert 			{
22875796c8dcSSimon Schubert 			  var->stack = 0;
22885796c8dcSSimon Schubert 
22895796c8dcSSimon Schubert 			  /* Verify that DW_OP_addr is the only opcode in the
22905796c8dcSSimon Schubert 			     location, in which case the block size will be 1
22915796c8dcSSimon Schubert 			     plus the address size.  */
22925796c8dcSSimon Schubert 			  /* ??? For TLS variables, gcc can emit
22935796c8dcSSimon Schubert 			     DW_OP_addr <addr> DW_OP_GNU_push_tls_address
22945796c8dcSSimon Schubert 			     which we don't handle here yet.  */
22955796c8dcSSimon Schubert 			  if (attr.u.blk->size == unit->addr_size + 1U)
22965796c8dcSSimon Schubert 			    var->addr = bfd_get (unit->addr_size * 8,
22975796c8dcSSimon Schubert 						 unit->abfd,
22985796c8dcSSimon Schubert 						 attr.u.blk->data + 1);
22995796c8dcSSimon Schubert 			}
23005796c8dcSSimon Schubert 		      break;
23015796c8dcSSimon Schubert 
23025796c8dcSSimon Schubert 		    default:
23035796c8dcSSimon Schubert 		      break;
23045796c8dcSSimon Schubert 		    }
23055796c8dcSSimon Schubert 		  break;
23065796c8dcSSimon Schubert 
23075796c8dcSSimon Schubert 		default:
23085796c8dcSSimon Schubert 		  break;
23095796c8dcSSimon Schubert 		}
23105796c8dcSSimon Schubert 	    }
23115796c8dcSSimon Schubert 	}
23125796c8dcSSimon Schubert 
2313*ef5ccd6cSJohn Marino       if (high_pc_relative)
2314*ef5ccd6cSJohn Marino 	high_pc += low_pc;
2315*ef5ccd6cSJohn Marino 
23165796c8dcSSimon Schubert       if (func && high_pc != 0)
23175796c8dcSSimon Schubert 	{
2318*ef5ccd6cSJohn Marino 	  if (!arange_add (unit, &func->arange, low_pc, high_pc))
2319cf7f2e2dSJohn Marino 	    goto fail;
23205796c8dcSSimon Schubert 	}
23215796c8dcSSimon Schubert 
23225796c8dcSSimon Schubert       if (abbrev->has_children)
23235796c8dcSSimon Schubert 	{
23245796c8dcSSimon Schubert 	  nesting_level++;
23255796c8dcSSimon Schubert 
23265796c8dcSSimon Schubert 	  if (nesting_level >= nested_funcs_size)
23275796c8dcSSimon Schubert 	    {
23285796c8dcSSimon Schubert 	      struct funcinfo **tmp;
23295796c8dcSSimon Schubert 
23305796c8dcSSimon Schubert 	      nested_funcs_size *= 2;
23315796c8dcSSimon Schubert 	      tmp = (struct funcinfo **)
23325796c8dcSSimon Schubert 		bfd_realloc (nested_funcs,
2333*ef5ccd6cSJohn Marino 			     nested_funcs_size * sizeof (struct funcinfo *));
23345796c8dcSSimon Schubert 	      if (tmp == NULL)
2335cf7f2e2dSJohn Marino 		goto fail;
23365796c8dcSSimon Schubert 	      nested_funcs = tmp;
23375796c8dcSSimon Schubert 	    }
23385796c8dcSSimon Schubert 	  nested_funcs[nesting_level] = 0;
23395796c8dcSSimon Schubert 	}
23405796c8dcSSimon Schubert     }
23415796c8dcSSimon Schubert 
23425796c8dcSSimon Schubert   free (nested_funcs);
23435796c8dcSSimon Schubert   return TRUE;
2344cf7f2e2dSJohn Marino 
2345cf7f2e2dSJohn Marino  fail:
2346cf7f2e2dSJohn Marino   free (nested_funcs);
2347cf7f2e2dSJohn Marino   return FALSE;
23485796c8dcSSimon Schubert }
23495796c8dcSSimon Schubert 
23505796c8dcSSimon Schubert /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
23515796c8dcSSimon Schubert    includes the compilation unit header that proceeds the DIE's, but
23525796c8dcSSimon Schubert    does not include the length field that precedes each compilation
23535796c8dcSSimon Schubert    unit header.  END_PTR points one past the end of this comp unit.
23545796c8dcSSimon Schubert    OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
23555796c8dcSSimon Schubert 
23565796c8dcSSimon Schubert    This routine does not read the whole compilation unit; only enough
23575796c8dcSSimon Schubert    to get to the line number information for the compilation unit.  */
23585796c8dcSSimon Schubert 
23595796c8dcSSimon Schubert static struct comp_unit *
parse_comp_unit(struct dwarf2_debug * stash,bfd_vma unit_length,bfd_byte * info_ptr_unit,unsigned int offset_size)23605796c8dcSSimon Schubert parse_comp_unit (struct dwarf2_debug *stash,
23615796c8dcSSimon Schubert 		 bfd_vma unit_length,
23625796c8dcSSimon Schubert 		 bfd_byte *info_ptr_unit,
23635796c8dcSSimon Schubert 		 unsigned int offset_size)
23645796c8dcSSimon Schubert {
23655796c8dcSSimon Schubert   struct comp_unit* unit;
23665796c8dcSSimon Schubert   unsigned int version;
23675796c8dcSSimon Schubert   bfd_uint64_t abbrev_offset = 0;
23685796c8dcSSimon Schubert   unsigned int addr_size;
23695796c8dcSSimon Schubert   struct abbrev_info** abbrevs;
23705796c8dcSSimon Schubert   unsigned int abbrev_number, bytes_read, i;
23715796c8dcSSimon Schubert   struct abbrev_info *abbrev;
23725796c8dcSSimon Schubert   struct attribute attr;
23735796c8dcSSimon Schubert   bfd_byte *info_ptr = stash->info_ptr;
23745796c8dcSSimon Schubert   bfd_byte *end_ptr = info_ptr + unit_length;
23755796c8dcSSimon Schubert   bfd_size_type amt;
23765796c8dcSSimon Schubert   bfd_vma low_pc = 0;
23775796c8dcSSimon Schubert   bfd_vma high_pc = 0;
23785796c8dcSSimon Schubert   bfd *abfd = stash->bfd_ptr;
2379*ef5ccd6cSJohn Marino   bfd_boolean high_pc_relative = FALSE;
23805796c8dcSSimon Schubert 
23815796c8dcSSimon Schubert   version = read_2_bytes (abfd, info_ptr);
23825796c8dcSSimon Schubert   info_ptr += 2;
23835796c8dcSSimon Schubert   BFD_ASSERT (offset_size == 4 || offset_size == 8);
23845796c8dcSSimon Schubert   if (offset_size == 4)
23855796c8dcSSimon Schubert     abbrev_offset = read_4_bytes (abfd, info_ptr);
23865796c8dcSSimon Schubert   else
23875796c8dcSSimon Schubert     abbrev_offset = read_8_bytes (abfd, info_ptr);
23885796c8dcSSimon Schubert   info_ptr += offset_size;
23895796c8dcSSimon Schubert   addr_size = read_1_byte (abfd, info_ptr);
23905796c8dcSSimon Schubert   info_ptr += 1;
23915796c8dcSSimon Schubert 
2392cf7f2e2dSJohn Marino   if (version != 2 && version != 3 && version != 4)
23935796c8dcSSimon Schubert     {
2394*ef5ccd6cSJohn Marino       (*_bfd_error_handler)
2395*ef5ccd6cSJohn Marino 	(_("Dwarf Error: found dwarf version '%u', this reader"
2396*ef5ccd6cSJohn Marino 	   " only handles version 2, 3 and 4 information."), version);
23975796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
23985796c8dcSSimon Schubert       return 0;
23995796c8dcSSimon Schubert     }
24005796c8dcSSimon Schubert 
24015796c8dcSSimon Schubert   if (addr_size > sizeof (bfd_vma))
24025796c8dcSSimon Schubert     {
2403*ef5ccd6cSJohn Marino       (*_bfd_error_handler)
2404*ef5ccd6cSJohn Marino 	(_("Dwarf Error: found address size '%u', this reader"
2405*ef5ccd6cSJohn Marino 	   " can not handle sizes greater than '%u'."),
24065796c8dcSSimon Schubert 	 addr_size,
24075796c8dcSSimon Schubert 	 (unsigned int) sizeof (bfd_vma));
24085796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
24095796c8dcSSimon Schubert       return 0;
24105796c8dcSSimon Schubert     }
24115796c8dcSSimon Schubert 
24125796c8dcSSimon Schubert   if (addr_size != 2 && addr_size != 4 && addr_size != 8)
24135796c8dcSSimon Schubert     {
2414*ef5ccd6cSJohn Marino       (*_bfd_error_handler)
2415*ef5ccd6cSJohn Marino 	("Dwarf Error: found address size '%u', this reader"
2416*ef5ccd6cSJohn Marino 	 " can only handle address sizes '2', '4' and '8'.", addr_size);
24175796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
24185796c8dcSSimon Schubert       return 0;
24195796c8dcSSimon Schubert     }
24205796c8dcSSimon Schubert 
24215796c8dcSSimon Schubert   /* Read the abbrevs for this compilation unit into a table.  */
24225796c8dcSSimon Schubert   abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
24235796c8dcSSimon Schubert   if (! abbrevs)
24245796c8dcSSimon Schubert     return 0;
24255796c8dcSSimon Schubert 
24265796c8dcSSimon Schubert   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
24275796c8dcSSimon Schubert   info_ptr += bytes_read;
24285796c8dcSSimon Schubert   if (! abbrev_number)
24295796c8dcSSimon Schubert     {
24305796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
24315796c8dcSSimon Schubert 			     abbrev_number);
24325796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
24335796c8dcSSimon Schubert       return 0;
24345796c8dcSSimon Schubert     }
24355796c8dcSSimon Schubert 
24365796c8dcSSimon Schubert   abbrev = lookup_abbrev (abbrev_number, abbrevs);
24375796c8dcSSimon Schubert   if (! abbrev)
24385796c8dcSSimon Schubert     {
24395796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
24405796c8dcSSimon Schubert 			     abbrev_number);
24415796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
24425796c8dcSSimon Schubert       return 0;
24435796c8dcSSimon Schubert     }
24445796c8dcSSimon Schubert 
24455796c8dcSSimon Schubert   amt = sizeof (struct comp_unit);
24465796c8dcSSimon Schubert   unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
2447cf7f2e2dSJohn Marino   if (unit == NULL)
2448cf7f2e2dSJohn Marino     return NULL;
24495796c8dcSSimon Schubert   unit->abfd = abfd;
24505796c8dcSSimon Schubert   unit->version = version;
24515796c8dcSSimon Schubert   unit->addr_size = addr_size;
24525796c8dcSSimon Schubert   unit->offset_size = offset_size;
24535796c8dcSSimon Schubert   unit->abbrevs = abbrevs;
24545796c8dcSSimon Schubert   unit->end_ptr = end_ptr;
24555796c8dcSSimon Schubert   unit->stash = stash;
24565796c8dcSSimon Schubert   unit->info_ptr_unit = info_ptr_unit;
2457cf7f2e2dSJohn Marino   unit->sec_info_ptr = stash->sec_info_ptr;
24585796c8dcSSimon Schubert 
24595796c8dcSSimon Schubert   for (i = 0; i < abbrev->num_attrs; ++i)
24605796c8dcSSimon Schubert     {
24615796c8dcSSimon Schubert       info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2462cf7f2e2dSJohn Marino       if (info_ptr == NULL)
2463cf7f2e2dSJohn Marino 	return NULL;
24645796c8dcSSimon Schubert 
24655796c8dcSSimon Schubert       /* Store the data if it is of an attribute we want to keep in a
24665796c8dcSSimon Schubert 	 partial symbol table.  */
24675796c8dcSSimon Schubert       switch (attr.name)
24685796c8dcSSimon Schubert 	{
24695796c8dcSSimon Schubert 	case DW_AT_stmt_list:
24705796c8dcSSimon Schubert 	  unit->stmtlist = 1;
24715796c8dcSSimon Schubert 	  unit->line_offset = attr.u.val;
24725796c8dcSSimon Schubert 	  break;
24735796c8dcSSimon Schubert 
24745796c8dcSSimon Schubert 	case DW_AT_name:
24755796c8dcSSimon Schubert 	  unit->name = attr.u.str;
24765796c8dcSSimon Schubert 	  break;
24775796c8dcSSimon Schubert 
24785796c8dcSSimon Schubert 	case DW_AT_low_pc:
24795796c8dcSSimon Schubert 	  low_pc = attr.u.val;
24805796c8dcSSimon Schubert 	  /* If the compilation unit DIE has a DW_AT_low_pc attribute,
24815796c8dcSSimon Schubert 	     this is the base address to use when reading location
24825796c8dcSSimon Schubert 	     lists or range lists. */
2483*ef5ccd6cSJohn Marino 	  if (abbrev->tag == DW_TAG_compile_unit)
24845796c8dcSSimon Schubert 	    unit->base_address = low_pc;
24855796c8dcSSimon Schubert 	  break;
24865796c8dcSSimon Schubert 
24875796c8dcSSimon Schubert 	case DW_AT_high_pc:
24885796c8dcSSimon Schubert 	  high_pc = attr.u.val;
2489*ef5ccd6cSJohn Marino 	  high_pc_relative = attr.form != DW_FORM_addr;
24905796c8dcSSimon Schubert 	  break;
24915796c8dcSSimon Schubert 
24925796c8dcSSimon Schubert 	case DW_AT_ranges:
2493cf7f2e2dSJohn Marino 	  if (!read_rangelist (unit, &unit->arange, attr.u.val))
2494cf7f2e2dSJohn Marino 	    return NULL;
24955796c8dcSSimon Schubert 	  break;
24965796c8dcSSimon Schubert 
24975796c8dcSSimon Schubert 	case DW_AT_comp_dir:
24985796c8dcSSimon Schubert 	  {
24995796c8dcSSimon Schubert 	    char *comp_dir = attr.u.str;
25005796c8dcSSimon Schubert 	    if (comp_dir)
25015796c8dcSSimon Schubert 	      {
25025796c8dcSSimon Schubert 		/* Irix 6.2 native cc prepends <machine>.: to the compilation
25035796c8dcSSimon Schubert 		   directory, get rid of it.  */
25045796c8dcSSimon Schubert 		char *cp = strchr (comp_dir, ':');
25055796c8dcSSimon Schubert 
25065796c8dcSSimon Schubert 		if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
25075796c8dcSSimon Schubert 		  comp_dir = cp + 1;
25085796c8dcSSimon Schubert 	      }
25095796c8dcSSimon Schubert 	    unit->comp_dir = comp_dir;
25105796c8dcSSimon Schubert 	    break;
25115796c8dcSSimon Schubert 	  }
25125796c8dcSSimon Schubert 
25135796c8dcSSimon Schubert 	default:
25145796c8dcSSimon Schubert 	  break;
25155796c8dcSSimon Schubert 	}
25165796c8dcSSimon Schubert     }
2517*ef5ccd6cSJohn Marino   if (high_pc_relative)
2518*ef5ccd6cSJohn Marino     high_pc += low_pc;
25195796c8dcSSimon Schubert   if (high_pc != 0)
25205796c8dcSSimon Schubert     {
2521*ef5ccd6cSJohn Marino       if (!arange_add (unit, &unit->arange, low_pc, high_pc))
2522cf7f2e2dSJohn Marino 	return NULL;
25235796c8dcSSimon Schubert     }
25245796c8dcSSimon Schubert 
25255796c8dcSSimon Schubert   unit->first_child_die_ptr = info_ptr;
25265796c8dcSSimon Schubert   return unit;
25275796c8dcSSimon Schubert }
25285796c8dcSSimon Schubert 
25295796c8dcSSimon Schubert /* Return TRUE if UNIT may contain the address given by ADDR.  When
25305796c8dcSSimon Schubert    there are functions written entirely with inline asm statements, the
25315796c8dcSSimon Schubert    range info in the compilation unit header may not be correct.  We
25325796c8dcSSimon Schubert    need to consult the line info table to see if a compilation unit
25335796c8dcSSimon Schubert    really contains the given address.  */
25345796c8dcSSimon Schubert 
25355796c8dcSSimon Schubert static bfd_boolean
comp_unit_contains_address(struct comp_unit * unit,bfd_vma addr)25365796c8dcSSimon Schubert comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
25375796c8dcSSimon Schubert {
25385796c8dcSSimon Schubert   struct arange *arange;
25395796c8dcSSimon Schubert 
25405796c8dcSSimon Schubert   if (unit->error)
25415796c8dcSSimon Schubert     return FALSE;
25425796c8dcSSimon Schubert 
25435796c8dcSSimon Schubert   arange = &unit->arange;
25445796c8dcSSimon Schubert   do
25455796c8dcSSimon Schubert     {
25465796c8dcSSimon Schubert       if (addr >= arange->low && addr < arange->high)
25475796c8dcSSimon Schubert 	return TRUE;
25485796c8dcSSimon Schubert       arange = arange->next;
25495796c8dcSSimon Schubert     }
25505796c8dcSSimon Schubert   while (arange);
25515796c8dcSSimon Schubert 
25525796c8dcSSimon Schubert   return FALSE;
25535796c8dcSSimon Schubert }
25545796c8dcSSimon Schubert 
25555796c8dcSSimon Schubert /* If UNIT contains ADDR, set the output parameters to the values for
25565796c8dcSSimon Schubert    the line containing ADDR.  The output parameters, FILENAME_PTR,
25575796c8dcSSimon Schubert    FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
25585796c8dcSSimon Schubert    to be filled in.
25595796c8dcSSimon Schubert 
25605796c8dcSSimon Schubert    Return TRUE if UNIT contains ADDR, and no errors were encountered;
25615796c8dcSSimon Schubert    FALSE otherwise.  */
25625796c8dcSSimon Schubert 
25635796c8dcSSimon Schubert static bfd_boolean
comp_unit_find_nearest_line(struct comp_unit * unit,bfd_vma addr,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * linenumber_ptr,unsigned int * discriminator_ptr,struct dwarf2_debug * stash)25645796c8dcSSimon Schubert comp_unit_find_nearest_line (struct comp_unit *unit,
25655796c8dcSSimon Schubert 			     bfd_vma addr,
25665796c8dcSSimon Schubert 			     const char **filename_ptr,
25675796c8dcSSimon Schubert 			     const char **functionname_ptr,
25685796c8dcSSimon Schubert 			     unsigned int *linenumber_ptr,
2569*ef5ccd6cSJohn Marino 			     unsigned int *discriminator_ptr,
25705796c8dcSSimon Schubert 			     struct dwarf2_debug *stash)
25715796c8dcSSimon Schubert {
25725796c8dcSSimon Schubert   bfd_boolean line_p;
25735796c8dcSSimon Schubert   bfd_boolean func_p;
25745796c8dcSSimon Schubert   struct funcinfo *function;
25755796c8dcSSimon Schubert 
25765796c8dcSSimon Schubert   if (unit->error)
25775796c8dcSSimon Schubert     return FALSE;
25785796c8dcSSimon Schubert 
25795796c8dcSSimon Schubert   if (! unit->line_table)
25805796c8dcSSimon Schubert     {
25815796c8dcSSimon Schubert       if (! unit->stmtlist)
25825796c8dcSSimon Schubert 	{
25835796c8dcSSimon Schubert 	  unit->error = 1;
25845796c8dcSSimon Schubert 	  return FALSE;
25855796c8dcSSimon Schubert 	}
25865796c8dcSSimon Schubert 
25875796c8dcSSimon Schubert       unit->line_table = decode_line_info (unit, stash);
25885796c8dcSSimon Schubert 
25895796c8dcSSimon Schubert       if (! unit->line_table)
25905796c8dcSSimon Schubert 	{
25915796c8dcSSimon Schubert 	  unit->error = 1;
25925796c8dcSSimon Schubert 	  return FALSE;
25935796c8dcSSimon Schubert 	}
25945796c8dcSSimon Schubert 
25955796c8dcSSimon Schubert       if (unit->first_child_die_ptr < unit->end_ptr
25965796c8dcSSimon Schubert 	  && ! scan_unit_for_symbols (unit))
25975796c8dcSSimon Schubert 	{
25985796c8dcSSimon Schubert 	  unit->error = 1;
25995796c8dcSSimon Schubert 	  return FALSE;
26005796c8dcSSimon Schubert 	}
26015796c8dcSSimon Schubert     }
26025796c8dcSSimon Schubert 
26035796c8dcSSimon Schubert   function = NULL;
26045796c8dcSSimon Schubert   func_p = lookup_address_in_function_table (unit, addr,
26055796c8dcSSimon Schubert 					     &function, functionname_ptr);
26065796c8dcSSimon Schubert   if (func_p && (function->tag == DW_TAG_inlined_subroutine))
26075796c8dcSSimon Schubert     stash->inliner_chain = function;
26085796c8dcSSimon Schubert   line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2609cf7f2e2dSJohn Marino 					      filename_ptr,
2610*ef5ccd6cSJohn Marino 					      linenumber_ptr,
2611*ef5ccd6cSJohn Marino 					      discriminator_ptr);
26125796c8dcSSimon Schubert   return line_p || func_p;
26135796c8dcSSimon Schubert }
26145796c8dcSSimon Schubert 
26155796c8dcSSimon Schubert /* Check to see if line info is already decoded in a comp_unit.
26165796c8dcSSimon Schubert    If not, decode it.  Returns TRUE if no errors were encountered;
26175796c8dcSSimon Schubert    FALSE otherwise.  */
26185796c8dcSSimon Schubert 
26195796c8dcSSimon Schubert static bfd_boolean
comp_unit_maybe_decode_line_info(struct comp_unit * unit,struct dwarf2_debug * stash)26205796c8dcSSimon Schubert comp_unit_maybe_decode_line_info (struct comp_unit *unit,
26215796c8dcSSimon Schubert 				  struct dwarf2_debug *stash)
26225796c8dcSSimon Schubert {
26235796c8dcSSimon Schubert   if (unit->error)
26245796c8dcSSimon Schubert     return FALSE;
26255796c8dcSSimon Schubert 
26265796c8dcSSimon Schubert   if (! unit->line_table)
26275796c8dcSSimon Schubert     {
26285796c8dcSSimon Schubert       if (! unit->stmtlist)
26295796c8dcSSimon Schubert 	{
26305796c8dcSSimon Schubert 	  unit->error = 1;
26315796c8dcSSimon Schubert 	  return FALSE;
26325796c8dcSSimon Schubert 	}
26335796c8dcSSimon Schubert 
26345796c8dcSSimon Schubert       unit->line_table = decode_line_info (unit, stash);
26355796c8dcSSimon Schubert 
26365796c8dcSSimon Schubert       if (! unit->line_table)
26375796c8dcSSimon Schubert 	{
26385796c8dcSSimon Schubert 	  unit->error = 1;
26395796c8dcSSimon Schubert 	  return FALSE;
26405796c8dcSSimon Schubert 	}
26415796c8dcSSimon Schubert 
26425796c8dcSSimon Schubert       if (unit->first_child_die_ptr < unit->end_ptr
26435796c8dcSSimon Schubert 	  && ! scan_unit_for_symbols (unit))
26445796c8dcSSimon Schubert 	{
26455796c8dcSSimon Schubert 	  unit->error = 1;
26465796c8dcSSimon Schubert 	  return FALSE;
26475796c8dcSSimon Schubert 	}
26485796c8dcSSimon Schubert     }
26495796c8dcSSimon Schubert 
26505796c8dcSSimon Schubert   return TRUE;
26515796c8dcSSimon Schubert }
26525796c8dcSSimon Schubert 
26535796c8dcSSimon Schubert /* If UNIT contains SYM at ADDR, set the output parameters to the
26545796c8dcSSimon Schubert    values for the line containing SYM.  The output parameters,
26555796c8dcSSimon Schubert    FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
26565796c8dcSSimon Schubert    filled in.
26575796c8dcSSimon Schubert 
26585796c8dcSSimon Schubert    Return TRUE if UNIT contains SYM, and no errors were encountered;
26595796c8dcSSimon Schubert    FALSE otherwise.  */
26605796c8dcSSimon Schubert 
26615796c8dcSSimon Schubert static bfd_boolean
comp_unit_find_line(struct comp_unit * unit,asymbol * sym,bfd_vma addr,const char ** filename_ptr,unsigned int * linenumber_ptr,struct dwarf2_debug * stash)26625796c8dcSSimon Schubert comp_unit_find_line (struct comp_unit *unit,
26635796c8dcSSimon Schubert 		     asymbol *sym,
26645796c8dcSSimon Schubert 		     bfd_vma addr,
26655796c8dcSSimon Schubert 		     const char **filename_ptr,
26665796c8dcSSimon Schubert 		     unsigned int *linenumber_ptr,
26675796c8dcSSimon Schubert 		     struct dwarf2_debug *stash)
26685796c8dcSSimon Schubert {
26695796c8dcSSimon Schubert   if (!comp_unit_maybe_decode_line_info (unit, stash))
26705796c8dcSSimon Schubert     return FALSE;
26715796c8dcSSimon Schubert 
26725796c8dcSSimon Schubert   if (sym->flags & BSF_FUNCTION)
26735796c8dcSSimon Schubert     return lookup_symbol_in_function_table (unit, sym, addr,
26745796c8dcSSimon Schubert 					    filename_ptr,
26755796c8dcSSimon Schubert 					    linenumber_ptr);
26765796c8dcSSimon Schubert 
26775796c8dcSSimon Schubert   return lookup_symbol_in_variable_table (unit, sym, addr,
26785796c8dcSSimon Schubert 					  filename_ptr,
26795796c8dcSSimon Schubert 					  linenumber_ptr);
26805796c8dcSSimon Schubert }
26815796c8dcSSimon Schubert 
26825796c8dcSSimon Schubert static struct funcinfo *
reverse_funcinfo_list(struct funcinfo * head)26835796c8dcSSimon Schubert reverse_funcinfo_list (struct funcinfo *head)
26845796c8dcSSimon Schubert {
26855796c8dcSSimon Schubert   struct funcinfo *rhead;
26865796c8dcSSimon Schubert   struct funcinfo *temp;
26875796c8dcSSimon Schubert 
26885796c8dcSSimon Schubert   for (rhead = NULL; head; head = temp)
26895796c8dcSSimon Schubert     {
26905796c8dcSSimon Schubert       temp = head->prev_func;
26915796c8dcSSimon Schubert       head->prev_func = rhead;
26925796c8dcSSimon Schubert       rhead = head;
26935796c8dcSSimon Schubert     }
26945796c8dcSSimon Schubert   return rhead;
26955796c8dcSSimon Schubert }
26965796c8dcSSimon Schubert 
26975796c8dcSSimon Schubert static struct varinfo *
reverse_varinfo_list(struct varinfo * head)26985796c8dcSSimon Schubert reverse_varinfo_list (struct varinfo *head)
26995796c8dcSSimon Schubert {
27005796c8dcSSimon Schubert   struct varinfo *rhead;
27015796c8dcSSimon Schubert   struct varinfo *temp;
27025796c8dcSSimon Schubert 
27035796c8dcSSimon Schubert   for (rhead = NULL; head; head = temp)
27045796c8dcSSimon Schubert     {
27055796c8dcSSimon Schubert       temp = head->prev_var;
27065796c8dcSSimon Schubert       head->prev_var = rhead;
27075796c8dcSSimon Schubert       rhead = head;
27085796c8dcSSimon Schubert     }
27095796c8dcSSimon Schubert   return rhead;
27105796c8dcSSimon Schubert }
27115796c8dcSSimon Schubert 
27125796c8dcSSimon Schubert /* Extract all interesting funcinfos and varinfos of a compilation
27135796c8dcSSimon Schubert    unit into hash tables for faster lookup.  Returns TRUE if no
27145796c8dcSSimon Schubert    errors were enountered; FALSE otherwise.  */
27155796c8dcSSimon Schubert 
27165796c8dcSSimon Schubert static bfd_boolean
comp_unit_hash_info(struct dwarf2_debug * stash,struct comp_unit * unit,struct info_hash_table * funcinfo_hash_table,struct info_hash_table * varinfo_hash_table)27175796c8dcSSimon Schubert comp_unit_hash_info (struct dwarf2_debug *stash,
27185796c8dcSSimon Schubert 		     struct comp_unit *unit,
27195796c8dcSSimon Schubert 		     struct info_hash_table *funcinfo_hash_table,
27205796c8dcSSimon Schubert 		     struct info_hash_table *varinfo_hash_table)
27215796c8dcSSimon Schubert {
27225796c8dcSSimon Schubert   struct funcinfo* each_func;
27235796c8dcSSimon Schubert   struct varinfo* each_var;
27245796c8dcSSimon Schubert   bfd_boolean okay = TRUE;
27255796c8dcSSimon Schubert 
27265796c8dcSSimon Schubert   BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
27275796c8dcSSimon Schubert 
27285796c8dcSSimon Schubert   if (!comp_unit_maybe_decode_line_info (unit, stash))
27295796c8dcSSimon Schubert     return FALSE;
27305796c8dcSSimon Schubert 
27315796c8dcSSimon Schubert   BFD_ASSERT (!unit->cached);
27325796c8dcSSimon Schubert 
27335796c8dcSSimon Schubert   /* To preserve the original search order, we went to visit the function
27345796c8dcSSimon Schubert      infos in the reversed order of the list.  However, making the list
27355796c8dcSSimon Schubert      bi-directional use quite a bit of extra memory.  So we reverse
27365796c8dcSSimon Schubert      the list first, traverse the list in the now reversed order and
27375796c8dcSSimon Schubert      finally reverse the list again to get back the original order.  */
27385796c8dcSSimon Schubert   unit->function_table = reverse_funcinfo_list (unit->function_table);
27395796c8dcSSimon Schubert   for (each_func = unit->function_table;
27405796c8dcSSimon Schubert        each_func && okay;
27415796c8dcSSimon Schubert        each_func = each_func->prev_func)
27425796c8dcSSimon Schubert     {
27435796c8dcSSimon Schubert       /* Skip nameless functions. */
27445796c8dcSSimon Schubert       if (each_func->name)
27455796c8dcSSimon Schubert 	/* There is no need to copy name string into hash table as
27465796c8dcSSimon Schubert 	   name string is either in the dwarf string buffer or
27475796c8dcSSimon Schubert 	   info in the stash.  */
27485796c8dcSSimon Schubert 	okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
27495796c8dcSSimon Schubert 				       (void*) each_func, FALSE);
27505796c8dcSSimon Schubert     }
27515796c8dcSSimon Schubert   unit->function_table = reverse_funcinfo_list (unit->function_table);
27525796c8dcSSimon Schubert   if (!okay)
27535796c8dcSSimon Schubert     return FALSE;
27545796c8dcSSimon Schubert 
27555796c8dcSSimon Schubert   /* We do the same for variable infos.  */
27565796c8dcSSimon Schubert   unit->variable_table = reverse_varinfo_list (unit->variable_table);
27575796c8dcSSimon Schubert   for (each_var = unit->variable_table;
27585796c8dcSSimon Schubert        each_var && okay;
27595796c8dcSSimon Schubert        each_var = each_var->prev_var)
27605796c8dcSSimon Schubert     {
27615796c8dcSSimon Schubert       /* Skip stack vars and vars with no files or names.  */
27625796c8dcSSimon Schubert       if (each_var->stack == 0
27635796c8dcSSimon Schubert 	  && each_var->file != NULL
27645796c8dcSSimon Schubert 	  && each_var->name != NULL)
27655796c8dcSSimon Schubert 	/* There is no need to copy name string into hash table as
27665796c8dcSSimon Schubert 	   name string is either in the dwarf string buffer or
27675796c8dcSSimon Schubert 	   info in the stash.  */
27685796c8dcSSimon Schubert 	okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
27695796c8dcSSimon Schubert 				       (void*) each_var, FALSE);
27705796c8dcSSimon Schubert     }
27715796c8dcSSimon Schubert 
27725796c8dcSSimon Schubert   unit->variable_table = reverse_varinfo_list (unit->variable_table);
27735796c8dcSSimon Schubert   unit->cached = TRUE;
27745796c8dcSSimon Schubert   return okay;
27755796c8dcSSimon Schubert }
27765796c8dcSSimon Schubert 
27775796c8dcSSimon Schubert /* Locate a section in a BFD containing debugging info.  The search starts
27785796c8dcSSimon Schubert    from the section after AFTER_SEC, or from the first section in the BFD if
27795796c8dcSSimon Schubert    AFTER_SEC is NULL.  The search works by examining the names of the
2780a45ae5f8SJohn Marino    sections.  There are three permissiable names.  The first two are given
2781a45ae5f8SJohn Marino    by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
2782a45ae5f8SJohn Marino    and .zdebug_info).  The third is a prefix .gnu.linkonce.wi.
27835796c8dcSSimon Schubert    This is a variation on the .debug_info section which has a checksum
27845796c8dcSSimon Schubert    describing the contents appended onto the name.  This allows the linker to
27855796c8dcSSimon Schubert    identify and discard duplicate debugging sections for different
27865796c8dcSSimon Schubert    compilation units.  */
27875796c8dcSSimon Schubert #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
27885796c8dcSSimon Schubert 
27895796c8dcSSimon Schubert static asection *
find_debug_info(bfd * abfd,const struct dwarf_debug_section * debug_sections,asection * after_sec)2790a45ae5f8SJohn Marino find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
2791a45ae5f8SJohn Marino                  asection *after_sec)
27925796c8dcSSimon Schubert {
27935796c8dcSSimon Schubert   asection *msec;
2794*ef5ccd6cSJohn Marino   const char *look;
27955796c8dcSSimon Schubert 
2796*ef5ccd6cSJohn Marino   if (after_sec == NULL)
27975796c8dcSSimon Schubert     {
2798*ef5ccd6cSJohn Marino       look = debug_sections[debug_info].uncompressed_name;
2799*ef5ccd6cSJohn Marino       msec = bfd_get_section_by_name (abfd, look);
2800*ef5ccd6cSJohn Marino       if (msec != NULL)
28015796c8dcSSimon Schubert 	return msec;
28025796c8dcSSimon Schubert 
2803*ef5ccd6cSJohn Marino       look = debug_sections[debug_info].compressed_name;
2804*ef5ccd6cSJohn Marino       if (look != NULL)
2805*ef5ccd6cSJohn Marino 	{
2806*ef5ccd6cSJohn Marino 	  msec = bfd_get_section_by_name (abfd, look);
2807*ef5ccd6cSJohn Marino 	  if (msec != NULL)
2808*ef5ccd6cSJohn Marino 	    return msec;
2809*ef5ccd6cSJohn Marino 	}
2810*ef5ccd6cSJohn Marino 
2811*ef5ccd6cSJohn Marino       for (msec = abfd->sections; msec != NULL; msec = msec->next)
2812*ef5ccd6cSJohn Marino 	if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2813*ef5ccd6cSJohn Marino 	  return msec;
2814*ef5ccd6cSJohn Marino 
2815*ef5ccd6cSJohn Marino       return NULL;
2816*ef5ccd6cSJohn Marino     }
2817*ef5ccd6cSJohn Marino 
2818*ef5ccd6cSJohn Marino   for (msec = after_sec->next; msec != NULL; msec = msec->next)
2819*ef5ccd6cSJohn Marino     {
2820*ef5ccd6cSJohn Marino       look = debug_sections[debug_info].uncompressed_name;
2821*ef5ccd6cSJohn Marino       if (strcmp (msec->name, look) == 0)
2822*ef5ccd6cSJohn Marino 	return msec;
2823*ef5ccd6cSJohn Marino 
2824*ef5ccd6cSJohn Marino       look = debug_sections[debug_info].compressed_name;
2825*ef5ccd6cSJohn Marino       if (look != NULL && strcmp (msec->name, look) == 0)
28265796c8dcSSimon Schubert 	return msec;
28275796c8dcSSimon Schubert 
28285796c8dcSSimon Schubert       if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
28295796c8dcSSimon Schubert 	return msec;
28305796c8dcSSimon Schubert     }
28315796c8dcSSimon Schubert 
28325796c8dcSSimon Schubert   return NULL;
28335796c8dcSSimon Schubert }
28345796c8dcSSimon Schubert 
28355796c8dcSSimon Schubert /* Unset vmas for adjusted sections in STASH.  */
28365796c8dcSSimon Schubert 
28375796c8dcSSimon Schubert static void
unset_sections(struct dwarf2_debug * stash)28385796c8dcSSimon Schubert unset_sections (struct dwarf2_debug *stash)
28395796c8dcSSimon Schubert {
28405796c8dcSSimon Schubert   unsigned int i;
28415796c8dcSSimon Schubert   struct adjusted_section *p;
28425796c8dcSSimon Schubert 
28435796c8dcSSimon Schubert   i = stash->adjusted_section_count;
28445796c8dcSSimon Schubert   p = stash->adjusted_sections;
28455796c8dcSSimon Schubert   for (; i > 0; i--, p++)
28465796c8dcSSimon Schubert     p->section->vma = 0;
28475796c8dcSSimon Schubert }
28485796c8dcSSimon Schubert 
28495796c8dcSSimon Schubert /* Set unique VMAs for loadable and DWARF sections in ABFD and save
28505796c8dcSSimon Schubert    VMAs in STASH for unset_sections.  */
28515796c8dcSSimon Schubert 
28525796c8dcSSimon Schubert static bfd_boolean
place_sections(bfd * abfd,struct dwarf2_debug * stash)28535796c8dcSSimon Schubert place_sections (bfd *abfd, struct dwarf2_debug *stash)
28545796c8dcSSimon Schubert {
28555796c8dcSSimon Schubert   struct adjusted_section *p;
28565796c8dcSSimon Schubert   unsigned int i;
28575796c8dcSSimon Schubert 
28585796c8dcSSimon Schubert   if (stash->adjusted_section_count != 0)
28595796c8dcSSimon Schubert     {
28605796c8dcSSimon Schubert       i = stash->adjusted_section_count;
28615796c8dcSSimon Schubert       p = stash->adjusted_sections;
28625796c8dcSSimon Schubert       for (; i > 0; i--, p++)
28635796c8dcSSimon Schubert 	p->section->vma = p->adj_vma;
28645796c8dcSSimon Schubert     }
28655796c8dcSSimon Schubert   else
28665796c8dcSSimon Schubert     {
28675796c8dcSSimon Schubert       asection *sect;
28685796c8dcSSimon Schubert       bfd_vma last_vma = 0, last_dwarf = 0;
28695796c8dcSSimon Schubert       bfd_size_type amt;
2870a45ae5f8SJohn Marino       const char *debug_info_name;
28715796c8dcSSimon Schubert 
2872a45ae5f8SJohn Marino       debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
28735796c8dcSSimon Schubert       i = 0;
28745796c8dcSSimon Schubert       for (sect = abfd->sections; sect != NULL; sect = sect->next)
28755796c8dcSSimon Schubert 	{
28765796c8dcSSimon Schubert 	  bfd_size_type sz;
28775796c8dcSSimon Schubert 	  int is_debug_info;
28785796c8dcSSimon Schubert 
28795796c8dcSSimon Schubert 	  if (sect->vma != 0)
28805796c8dcSSimon Schubert 	    continue;
28815796c8dcSSimon Schubert 
28825796c8dcSSimon Schubert 	  /* We need to adjust the VMAs of any .debug_info sections.
28835796c8dcSSimon Schubert 	     Skip compressed ones, since no relocations could target
28845796c8dcSSimon Schubert 	     them - they should not appear in object files anyway.  */
2885a45ae5f8SJohn Marino 	  if (strcmp (sect->name, debug_info_name) == 0)
28865796c8dcSSimon Schubert 	    is_debug_info = 1;
28875796c8dcSSimon Schubert 	  else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
28885796c8dcSSimon Schubert 	    is_debug_info = 1;
28895796c8dcSSimon Schubert 	  else
28905796c8dcSSimon Schubert 	    is_debug_info = 0;
28915796c8dcSSimon Schubert 
28925796c8dcSSimon Schubert 	  if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
28935796c8dcSSimon Schubert 	    continue;
28945796c8dcSSimon Schubert 
28955796c8dcSSimon Schubert 	  sz = sect->rawsize ? sect->rawsize : sect->size;
28965796c8dcSSimon Schubert 	  if (sz == 0)
28975796c8dcSSimon Schubert 	    continue;
28985796c8dcSSimon Schubert 
28995796c8dcSSimon Schubert 	  i++;
29005796c8dcSSimon Schubert 	}
29015796c8dcSSimon Schubert 
29025796c8dcSSimon Schubert       amt = i * sizeof (struct adjusted_section);
2903*ef5ccd6cSJohn Marino       p = (struct adjusted_section *) bfd_alloc (abfd, amt);
29045796c8dcSSimon Schubert       if (! p)
29055796c8dcSSimon Schubert 	return FALSE;
29065796c8dcSSimon Schubert 
29075796c8dcSSimon Schubert       stash->adjusted_sections = p;
29085796c8dcSSimon Schubert       stash->adjusted_section_count = i;
29095796c8dcSSimon Schubert 
29105796c8dcSSimon Schubert       for (sect = abfd->sections; sect != NULL; sect = sect->next)
29115796c8dcSSimon Schubert 	{
29125796c8dcSSimon Schubert 	  bfd_size_type sz;
29135796c8dcSSimon Schubert 	  int is_debug_info;
29145796c8dcSSimon Schubert 
29155796c8dcSSimon Schubert 	  if (sect->vma != 0)
29165796c8dcSSimon Schubert 	    continue;
29175796c8dcSSimon Schubert 
29185796c8dcSSimon Schubert 	  /* We need to adjust the VMAs of any .debug_info sections.
29195796c8dcSSimon Schubert 	     Skip compressed ones, since no relocations could target
29205796c8dcSSimon Schubert 	     them - they should not appear in object files anyway.  */
2921a45ae5f8SJohn Marino 	  if (strcmp (sect->name, debug_info_name) == 0)
29225796c8dcSSimon Schubert 	    is_debug_info = 1;
29235796c8dcSSimon Schubert 	  else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
29245796c8dcSSimon Schubert 	    is_debug_info = 1;
29255796c8dcSSimon Schubert 	  else
29265796c8dcSSimon Schubert 	    is_debug_info = 0;
29275796c8dcSSimon Schubert 
29285796c8dcSSimon Schubert 	  if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
29295796c8dcSSimon Schubert 	    continue;
29305796c8dcSSimon Schubert 
29315796c8dcSSimon Schubert 	  sz = sect->rawsize ? sect->rawsize : sect->size;
29325796c8dcSSimon Schubert 	  if (sz == 0)
29335796c8dcSSimon Schubert 	    continue;
29345796c8dcSSimon Schubert 
29355796c8dcSSimon Schubert 	  p->section = sect;
29365796c8dcSSimon Schubert 	  if (is_debug_info)
29375796c8dcSSimon Schubert 	    {
29385796c8dcSSimon Schubert 	      BFD_ASSERT (sect->alignment_power == 0);
29395796c8dcSSimon Schubert 	      sect->vma = last_dwarf;
29405796c8dcSSimon Schubert 	      last_dwarf += sz;
29415796c8dcSSimon Schubert 	    }
29425796c8dcSSimon Schubert 	  else if (last_vma != 0)
29435796c8dcSSimon Schubert 	    {
29445796c8dcSSimon Schubert 	      /* Align the new address to the current section
29455796c8dcSSimon Schubert 		 alignment.  */
29465796c8dcSSimon Schubert 	      last_vma = ((last_vma
29475796c8dcSSimon Schubert 			   + ~((bfd_vma) -1 << sect->alignment_power))
29485796c8dcSSimon Schubert 			  & ((bfd_vma) -1 << sect->alignment_power));
29495796c8dcSSimon Schubert 	      sect->vma = last_vma;
29505796c8dcSSimon Schubert 	      last_vma += sect->vma + sz;
29515796c8dcSSimon Schubert 	    }
29525796c8dcSSimon Schubert 	  else
29535796c8dcSSimon Schubert 	    last_vma += sect->vma + sz;
29545796c8dcSSimon Schubert 
29555796c8dcSSimon Schubert 	  p->adj_vma = sect->vma;
29565796c8dcSSimon Schubert 
29575796c8dcSSimon Schubert 	  p++;
29585796c8dcSSimon Schubert 	}
29595796c8dcSSimon Schubert     }
29605796c8dcSSimon Schubert 
29615796c8dcSSimon Schubert   return TRUE;
29625796c8dcSSimon Schubert }
29635796c8dcSSimon Schubert 
29645796c8dcSSimon Schubert /* Look up a funcinfo by name using the given info hash table.  If found,
29655796c8dcSSimon Schubert    also update the locations pointed to by filename_ptr and linenumber_ptr.
29665796c8dcSSimon Schubert 
29675796c8dcSSimon Schubert    This function returns TRUE if a funcinfo that matches the given symbol
29685796c8dcSSimon Schubert    and address is found with any error; otherwise it returns FALSE.  */
29695796c8dcSSimon Schubert 
29705796c8dcSSimon Schubert static bfd_boolean
info_hash_lookup_funcinfo(struct info_hash_table * hash_table,asymbol * sym,bfd_vma addr,const char ** filename_ptr,unsigned int * linenumber_ptr)29715796c8dcSSimon Schubert info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
29725796c8dcSSimon Schubert 			   asymbol *sym,
29735796c8dcSSimon Schubert 			   bfd_vma addr,
29745796c8dcSSimon Schubert 			   const char **filename_ptr,
29755796c8dcSSimon Schubert 			   unsigned int *linenumber_ptr)
29765796c8dcSSimon Schubert {
29775796c8dcSSimon Schubert   struct funcinfo* each_func;
29785796c8dcSSimon Schubert   struct funcinfo* best_fit = NULL;
29795796c8dcSSimon Schubert   struct info_list_node *node;
29805796c8dcSSimon Schubert   struct arange *arange;
29815796c8dcSSimon Schubert   const char *name = bfd_asymbol_name (sym);
29825796c8dcSSimon Schubert   asection *sec = bfd_get_section (sym);
29835796c8dcSSimon Schubert 
29845796c8dcSSimon Schubert   for (node = lookup_info_hash_table (hash_table, name);
29855796c8dcSSimon Schubert        node;
29865796c8dcSSimon Schubert        node = node->next)
29875796c8dcSSimon Schubert     {
29885796c8dcSSimon Schubert       each_func = (struct funcinfo *) node->info;
29895796c8dcSSimon Schubert       for (arange = &each_func->arange;
29905796c8dcSSimon Schubert 	   arange;
29915796c8dcSSimon Schubert 	   arange = arange->next)
29925796c8dcSSimon Schubert 	{
29935796c8dcSSimon Schubert 	  if ((!each_func->sec || each_func->sec == sec)
29945796c8dcSSimon Schubert 	      && addr >= arange->low
29955796c8dcSSimon Schubert 	      && addr < arange->high
29965796c8dcSSimon Schubert 	      && (!best_fit
2997*ef5ccd6cSJohn Marino 		  || (arange->high - arange->low
2998*ef5ccd6cSJohn Marino 		      < best_fit->arange.high - best_fit->arange.low)))
29995796c8dcSSimon Schubert 	    best_fit = each_func;
30005796c8dcSSimon Schubert 	}
30015796c8dcSSimon Schubert     }
30025796c8dcSSimon Schubert 
30035796c8dcSSimon Schubert   if (best_fit)
30045796c8dcSSimon Schubert     {
30055796c8dcSSimon Schubert       best_fit->sec = sec;
30065796c8dcSSimon Schubert       *filename_ptr = best_fit->file;
30075796c8dcSSimon Schubert       *linenumber_ptr = best_fit->line;
30085796c8dcSSimon Schubert       return TRUE;
30095796c8dcSSimon Schubert     }
30105796c8dcSSimon Schubert 
30115796c8dcSSimon Schubert   return FALSE;
30125796c8dcSSimon Schubert }
30135796c8dcSSimon Schubert 
30145796c8dcSSimon Schubert /* Look up a varinfo by name using the given info hash table.  If found,
30155796c8dcSSimon Schubert    also update the locations pointed to by filename_ptr and linenumber_ptr.
30165796c8dcSSimon Schubert 
30175796c8dcSSimon Schubert    This function returns TRUE if a varinfo that matches the given symbol
30185796c8dcSSimon Schubert    and address is found with any error; otherwise it returns FALSE.  */
30195796c8dcSSimon Schubert 
30205796c8dcSSimon Schubert static bfd_boolean
info_hash_lookup_varinfo(struct info_hash_table * hash_table,asymbol * sym,bfd_vma addr,const char ** filename_ptr,unsigned int * linenumber_ptr)30215796c8dcSSimon Schubert info_hash_lookup_varinfo (struct info_hash_table *hash_table,
30225796c8dcSSimon Schubert 			  asymbol *sym,
30235796c8dcSSimon Schubert 			  bfd_vma addr,
30245796c8dcSSimon Schubert 			  const char **filename_ptr,
30255796c8dcSSimon Schubert 			  unsigned int *linenumber_ptr)
30265796c8dcSSimon Schubert {
30275796c8dcSSimon Schubert   const char *name = bfd_asymbol_name (sym);
30285796c8dcSSimon Schubert   asection *sec = bfd_get_section (sym);
30295796c8dcSSimon Schubert   struct varinfo* each;
30305796c8dcSSimon Schubert   struct info_list_node *node;
30315796c8dcSSimon Schubert 
30325796c8dcSSimon Schubert   for (node = lookup_info_hash_table (hash_table, name);
30335796c8dcSSimon Schubert        node;
30345796c8dcSSimon Schubert        node = node->next)
30355796c8dcSSimon Schubert     {
30365796c8dcSSimon Schubert       each = (struct varinfo *) node->info;
30375796c8dcSSimon Schubert       if (each->addr == addr
30385796c8dcSSimon Schubert 	  && (!each->sec || each->sec == sec))
30395796c8dcSSimon Schubert 	{
30405796c8dcSSimon Schubert 	  each->sec = sec;
30415796c8dcSSimon Schubert 	  *filename_ptr = each->file;
30425796c8dcSSimon Schubert 	  *linenumber_ptr = each->line;
30435796c8dcSSimon Schubert 	  return TRUE;
30445796c8dcSSimon Schubert 	}
30455796c8dcSSimon Schubert     }
30465796c8dcSSimon Schubert 
30475796c8dcSSimon Schubert   return FALSE;
30485796c8dcSSimon Schubert }
30495796c8dcSSimon Schubert 
30505796c8dcSSimon Schubert /* Update the funcinfo and varinfo info hash tables if they are
30515796c8dcSSimon Schubert    not up to date.  Returns TRUE if there is no error; otherwise
30525796c8dcSSimon Schubert    returns FALSE and disable the info hash tables.  */
30535796c8dcSSimon Schubert 
30545796c8dcSSimon Schubert static bfd_boolean
stash_maybe_update_info_hash_tables(struct dwarf2_debug * stash)30555796c8dcSSimon Schubert stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
30565796c8dcSSimon Schubert {
30575796c8dcSSimon Schubert   struct comp_unit *each;
30585796c8dcSSimon Schubert 
30595796c8dcSSimon Schubert   /* Exit if hash tables are up-to-date.  */
30605796c8dcSSimon Schubert   if (stash->all_comp_units == stash->hash_units_head)
30615796c8dcSSimon Schubert     return TRUE;
30625796c8dcSSimon Schubert 
30635796c8dcSSimon Schubert   if (stash->hash_units_head)
30645796c8dcSSimon Schubert     each = stash->hash_units_head->prev_unit;
30655796c8dcSSimon Schubert   else
30665796c8dcSSimon Schubert     each = stash->last_comp_unit;
30675796c8dcSSimon Schubert 
30685796c8dcSSimon Schubert   while (each)
30695796c8dcSSimon Schubert     {
30705796c8dcSSimon Schubert       if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
30715796c8dcSSimon Schubert 				stash->varinfo_hash_table))
30725796c8dcSSimon Schubert 	{
30735796c8dcSSimon Schubert 	  stash->info_hash_status = STASH_INFO_HASH_DISABLED;
30745796c8dcSSimon Schubert 	  return FALSE;
30755796c8dcSSimon Schubert 	}
30765796c8dcSSimon Schubert       each = each->prev_unit;
30775796c8dcSSimon Schubert     }
30785796c8dcSSimon Schubert 
30795796c8dcSSimon Schubert   stash->hash_units_head = stash->all_comp_units;
30805796c8dcSSimon Schubert   return TRUE;
30815796c8dcSSimon Schubert }
30825796c8dcSSimon Schubert 
30835796c8dcSSimon Schubert /* Check consistency of info hash tables.  This is for debugging only. */
30845796c8dcSSimon Schubert 
30855796c8dcSSimon Schubert static void ATTRIBUTE_UNUSED
stash_verify_info_hash_table(struct dwarf2_debug * stash)30865796c8dcSSimon Schubert stash_verify_info_hash_table (struct dwarf2_debug *stash)
30875796c8dcSSimon Schubert {
30885796c8dcSSimon Schubert   struct comp_unit *each_unit;
30895796c8dcSSimon Schubert   struct funcinfo *each_func;
30905796c8dcSSimon Schubert   struct varinfo *each_var;
30915796c8dcSSimon Schubert   struct info_list_node *node;
30925796c8dcSSimon Schubert   bfd_boolean found;
30935796c8dcSSimon Schubert 
30945796c8dcSSimon Schubert   for (each_unit = stash->all_comp_units;
30955796c8dcSSimon Schubert        each_unit;
30965796c8dcSSimon Schubert        each_unit = each_unit->next_unit)
30975796c8dcSSimon Schubert     {
30985796c8dcSSimon Schubert       for (each_func = each_unit->function_table;
30995796c8dcSSimon Schubert 	   each_func;
31005796c8dcSSimon Schubert 	   each_func = each_func->prev_func)
31015796c8dcSSimon Schubert 	{
31025796c8dcSSimon Schubert 	  if (!each_func->name)
31035796c8dcSSimon Schubert 	    continue;
31045796c8dcSSimon Schubert 	  node = lookup_info_hash_table (stash->funcinfo_hash_table,
31055796c8dcSSimon Schubert 					 each_func->name);
31065796c8dcSSimon Schubert 	  BFD_ASSERT (node);
31075796c8dcSSimon Schubert 	  found = FALSE;
31085796c8dcSSimon Schubert 	  while (node && !found)
31095796c8dcSSimon Schubert 	    {
31105796c8dcSSimon Schubert 	      found = node->info == each_func;
31115796c8dcSSimon Schubert 	      node = node->next;
31125796c8dcSSimon Schubert 	    }
31135796c8dcSSimon Schubert 	  BFD_ASSERT (found);
31145796c8dcSSimon Schubert 	}
31155796c8dcSSimon Schubert 
31165796c8dcSSimon Schubert       for (each_var = each_unit->variable_table;
31175796c8dcSSimon Schubert 	   each_var;
31185796c8dcSSimon Schubert 	   each_var = each_var->prev_var)
31195796c8dcSSimon Schubert 	{
31205796c8dcSSimon Schubert 	  if (!each_var->name || !each_var->file || each_var->stack)
31215796c8dcSSimon Schubert 	    continue;
31225796c8dcSSimon Schubert 	  node = lookup_info_hash_table (stash->varinfo_hash_table,
31235796c8dcSSimon Schubert 					 each_var->name);
31245796c8dcSSimon Schubert 	  BFD_ASSERT (node);
31255796c8dcSSimon Schubert 	  found = FALSE;
31265796c8dcSSimon Schubert 	  while (node && !found)
31275796c8dcSSimon Schubert 	    {
31285796c8dcSSimon Schubert 	      found = node->info == each_var;
31295796c8dcSSimon Schubert 	      node = node->next;
31305796c8dcSSimon Schubert 	    }
31315796c8dcSSimon Schubert 	  BFD_ASSERT (found);
31325796c8dcSSimon Schubert 	}
31335796c8dcSSimon Schubert     }
31345796c8dcSSimon Schubert }
31355796c8dcSSimon Schubert 
31365796c8dcSSimon Schubert /* Check to see if we want to enable the info hash tables, which consume
31375796c8dcSSimon Schubert    quite a bit of memory.  Currently we only check the number times
31385796c8dcSSimon Schubert    bfd_dwarf2_find_line is called.  In the future, we may also want to
31395796c8dcSSimon Schubert    take the number of symbols into account.  */
31405796c8dcSSimon Schubert 
31415796c8dcSSimon Schubert static void
stash_maybe_enable_info_hash_tables(bfd * abfd,struct dwarf2_debug * stash)31425796c8dcSSimon Schubert stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
31435796c8dcSSimon Schubert {
31445796c8dcSSimon Schubert   BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
31455796c8dcSSimon Schubert 
31465796c8dcSSimon Schubert   if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
31475796c8dcSSimon Schubert     return;
31485796c8dcSSimon Schubert 
31495796c8dcSSimon Schubert   /* FIXME: Maybe we should check the reduce_memory_overheads
31505796c8dcSSimon Schubert      and optimize fields in the bfd_link_info structure ?  */
31515796c8dcSSimon Schubert 
31525796c8dcSSimon Schubert   /* Create hash tables.  */
31535796c8dcSSimon Schubert   stash->funcinfo_hash_table = create_info_hash_table (abfd);
31545796c8dcSSimon Schubert   stash->varinfo_hash_table = create_info_hash_table (abfd);
31555796c8dcSSimon Schubert   if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
31565796c8dcSSimon Schubert     {
31575796c8dcSSimon Schubert       /* Turn off info hashes if any allocation above fails.  */
31585796c8dcSSimon Schubert       stash->info_hash_status = STASH_INFO_HASH_DISABLED;
31595796c8dcSSimon Schubert       return;
31605796c8dcSSimon Schubert     }
31615796c8dcSSimon Schubert   /* We need a forced update so that the info hash tables will
31625796c8dcSSimon Schubert      be created even though there is no compilation unit.  That
31635796c8dcSSimon Schubert      happens if STASH_INFO_HASH_TRIGGER is 0.  */
31645796c8dcSSimon Schubert   stash_maybe_update_info_hash_tables (stash);
31655796c8dcSSimon Schubert   stash->info_hash_status = STASH_INFO_HASH_ON;
31665796c8dcSSimon Schubert }
31675796c8dcSSimon Schubert 
31685796c8dcSSimon Schubert /* Find the file and line associated with a symbol and address using the
31695796c8dcSSimon Schubert    info hash tables of a stash. If there is a match, the function returns
31705796c8dcSSimon Schubert    TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
31715796c8dcSSimon Schubert    otherwise it returns FALSE.  */
31725796c8dcSSimon Schubert 
31735796c8dcSSimon Schubert static bfd_boolean
stash_find_line_fast(struct dwarf2_debug * stash,asymbol * sym,bfd_vma addr,const char ** filename_ptr,unsigned int * linenumber_ptr)31745796c8dcSSimon Schubert stash_find_line_fast (struct dwarf2_debug *stash,
31755796c8dcSSimon Schubert 		      asymbol *sym,
31765796c8dcSSimon Schubert 		      bfd_vma addr,
31775796c8dcSSimon Schubert 		      const char **filename_ptr,
31785796c8dcSSimon Schubert 		      unsigned int *linenumber_ptr)
31795796c8dcSSimon Schubert {
31805796c8dcSSimon Schubert   BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
31815796c8dcSSimon Schubert 
31825796c8dcSSimon Schubert   if (sym->flags & BSF_FUNCTION)
31835796c8dcSSimon Schubert     return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
31845796c8dcSSimon Schubert 				      filename_ptr, linenumber_ptr);
31855796c8dcSSimon Schubert   return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
31865796c8dcSSimon Schubert 				   filename_ptr, linenumber_ptr);
31875796c8dcSSimon Schubert }
31885796c8dcSSimon Schubert 
3189*ef5ccd6cSJohn Marino /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
3190*ef5ccd6cSJohn Marino    If DEBUG_BFD is not specified, we read debug information from ABFD
3191*ef5ccd6cSJohn Marino    or its gnu_debuglink. The results will be stored in PINFO.
3192*ef5ccd6cSJohn Marino    The function returns TRUE iff debug information is ready.  */
3193*ef5ccd6cSJohn Marino 
3194*ef5ccd6cSJohn Marino bfd_boolean
_bfd_dwarf2_slurp_debug_info(bfd * abfd,bfd * debug_bfd,const struct dwarf_debug_section * debug_sections,asymbol ** symbols,void ** pinfo)3195*ef5ccd6cSJohn Marino _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
3196*ef5ccd6cSJohn Marino                               const struct dwarf_debug_section *debug_sections,
3197*ef5ccd6cSJohn Marino                               asymbol **symbols,
3198*ef5ccd6cSJohn Marino                               void **pinfo)
3199*ef5ccd6cSJohn Marino {
3200*ef5ccd6cSJohn Marino   bfd_size_type amt = sizeof (struct dwarf2_debug);
3201*ef5ccd6cSJohn Marino   bfd_size_type total_size;
3202*ef5ccd6cSJohn Marino   asection *msec;
3203*ef5ccd6cSJohn Marino   struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
3204*ef5ccd6cSJohn Marino 
3205*ef5ccd6cSJohn Marino   if (stash != NULL)
3206*ef5ccd6cSJohn Marino     return TRUE;
3207*ef5ccd6cSJohn Marino 
3208*ef5ccd6cSJohn Marino   stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
3209*ef5ccd6cSJohn Marino   if (! stash)
3210*ef5ccd6cSJohn Marino     return FALSE;
3211*ef5ccd6cSJohn Marino   stash->debug_sections = debug_sections;
3212*ef5ccd6cSJohn Marino   stash->syms = symbols;
3213*ef5ccd6cSJohn Marino 
3214*ef5ccd6cSJohn Marino   *pinfo = stash;
3215*ef5ccd6cSJohn Marino 
3216*ef5ccd6cSJohn Marino   if (debug_bfd == NULL)
3217*ef5ccd6cSJohn Marino     debug_bfd = abfd;
3218*ef5ccd6cSJohn Marino 
3219*ef5ccd6cSJohn Marino   msec = find_debug_info (debug_bfd, debug_sections, NULL);
3220*ef5ccd6cSJohn Marino   if (msec == NULL && abfd == debug_bfd)
3221*ef5ccd6cSJohn Marino     {
3222*ef5ccd6cSJohn Marino       char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
3223*ef5ccd6cSJohn Marino 
3224*ef5ccd6cSJohn Marino       if (debug_filename == NULL)
3225*ef5ccd6cSJohn Marino 	/* No dwarf2 info, and no gnu_debuglink to follow.
3226*ef5ccd6cSJohn Marino 	   Note that at this point the stash has been allocated, but
3227*ef5ccd6cSJohn Marino 	   contains zeros.  This lets future calls to this function
3228*ef5ccd6cSJohn Marino 	   fail more quickly.  */
3229*ef5ccd6cSJohn Marino 	return FALSE;
3230*ef5ccd6cSJohn Marino 
3231*ef5ccd6cSJohn Marino       if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
3232*ef5ccd6cSJohn Marino 	  || ! bfd_check_format (debug_bfd, bfd_object)
3233*ef5ccd6cSJohn Marino 	  || (msec = find_debug_info (debug_bfd,
3234*ef5ccd6cSJohn Marino 				      debug_sections, NULL)) == NULL)
3235*ef5ccd6cSJohn Marino 	{
3236*ef5ccd6cSJohn Marino 	  if (debug_bfd)
3237*ef5ccd6cSJohn Marino 	    bfd_close (debug_bfd);
3238*ef5ccd6cSJohn Marino 	  /* FIXME: Should we report our failure to follow the debuglink ?  */
3239*ef5ccd6cSJohn Marino 	  free (debug_filename);
3240*ef5ccd6cSJohn Marino 	  return FALSE;
3241*ef5ccd6cSJohn Marino 	}
3242*ef5ccd6cSJohn Marino       stash->close_on_cleanup = TRUE;
3243*ef5ccd6cSJohn Marino     }
3244*ef5ccd6cSJohn Marino   stash->bfd_ptr = debug_bfd;
3245*ef5ccd6cSJohn Marino 
3246*ef5ccd6cSJohn Marino   /* There can be more than one DWARF2 info section in a BFD these
3247*ef5ccd6cSJohn Marino      days.  First handle the easy case when there's only one.  If
3248*ef5ccd6cSJohn Marino      there's more than one, try case two: none of the sections is
3249*ef5ccd6cSJohn Marino      compressed.  In that case, read them all in and produce one
3250*ef5ccd6cSJohn Marino      large stash.  We do this in two passes - in the first pass we
3251*ef5ccd6cSJohn Marino      just accumulate the section sizes, and in the second pass we
3252*ef5ccd6cSJohn Marino      read in the section's contents.  (The allows us to avoid
3253*ef5ccd6cSJohn Marino      reallocing the data as we add sections to the stash.)  If
3254*ef5ccd6cSJohn Marino      some or all sections are compressed, then do things the slow
3255*ef5ccd6cSJohn Marino      way, with a bunch of reallocs.  */
3256*ef5ccd6cSJohn Marino 
3257*ef5ccd6cSJohn Marino   if (! find_debug_info (debug_bfd, debug_sections, msec))
3258*ef5ccd6cSJohn Marino     {
3259*ef5ccd6cSJohn Marino       /* Case 1: only one info section.  */
3260*ef5ccd6cSJohn Marino       total_size = msec->size;
3261*ef5ccd6cSJohn Marino       if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
3262*ef5ccd6cSJohn Marino 			  symbols, 0,
3263*ef5ccd6cSJohn Marino 			  &stash->info_ptr_memory, &total_size))
3264*ef5ccd6cSJohn Marino 	return FALSE;
3265*ef5ccd6cSJohn Marino     }
3266*ef5ccd6cSJohn Marino   else
3267*ef5ccd6cSJohn Marino     {
3268*ef5ccd6cSJohn Marino       /* Case 2: multiple sections.  */
3269*ef5ccd6cSJohn Marino       for (total_size = 0;
3270*ef5ccd6cSJohn Marino 	   msec;
3271*ef5ccd6cSJohn Marino 	   msec = find_debug_info (debug_bfd, debug_sections, msec))
3272*ef5ccd6cSJohn Marino 	total_size += msec->size;
3273*ef5ccd6cSJohn Marino 
3274*ef5ccd6cSJohn Marino       stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
3275*ef5ccd6cSJohn Marino       if (stash->info_ptr_memory == NULL)
3276*ef5ccd6cSJohn Marino 	return FALSE;
3277*ef5ccd6cSJohn Marino 
3278*ef5ccd6cSJohn Marino       total_size = 0;
3279*ef5ccd6cSJohn Marino       for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
3280*ef5ccd6cSJohn Marino 	   msec;
3281*ef5ccd6cSJohn Marino 	   msec = find_debug_info (debug_bfd, debug_sections, msec))
3282*ef5ccd6cSJohn Marino 	{
3283*ef5ccd6cSJohn Marino 	  bfd_size_type size;
3284*ef5ccd6cSJohn Marino 
3285*ef5ccd6cSJohn Marino 	  size = msec->size;
3286*ef5ccd6cSJohn Marino 	  if (size == 0)
3287*ef5ccd6cSJohn Marino 	    continue;
3288*ef5ccd6cSJohn Marino 
3289*ef5ccd6cSJohn Marino 	  if (!(bfd_simple_get_relocated_section_contents
3290*ef5ccd6cSJohn Marino 		(debug_bfd, msec, stash->info_ptr_memory + total_size,
3291*ef5ccd6cSJohn Marino 		 symbols)))
3292*ef5ccd6cSJohn Marino 	    return FALSE;
3293*ef5ccd6cSJohn Marino 
3294*ef5ccd6cSJohn Marino 	  total_size += size;
3295*ef5ccd6cSJohn Marino 	}
3296*ef5ccd6cSJohn Marino     }
3297*ef5ccd6cSJohn Marino 
3298*ef5ccd6cSJohn Marino   stash->info_ptr = stash->info_ptr_memory;
3299*ef5ccd6cSJohn Marino   stash->info_ptr_end = stash->info_ptr + total_size;
3300*ef5ccd6cSJohn Marino   stash->sec = find_debug_info (debug_bfd, debug_sections, NULL);
3301*ef5ccd6cSJohn Marino   stash->sec_info_ptr = stash->info_ptr;
3302*ef5ccd6cSJohn Marino   return TRUE;
3303*ef5ccd6cSJohn Marino }
3304*ef5ccd6cSJohn Marino 
33055796c8dcSSimon Schubert /* Find the source code location of SYMBOL.  If SYMBOL is NULL
33065796c8dcSSimon Schubert    then find the nearest source code location corresponding to
33075796c8dcSSimon Schubert    the address SECTION + OFFSET.
33085796c8dcSSimon Schubert    Returns TRUE if the line is found without error and fills in
33095796c8dcSSimon Schubert    FILENAME_PTR and LINENUMBER_PTR.  In the case where SYMBOL was
33105796c8dcSSimon Schubert    NULL the FUNCTIONNAME_PTR is also filled in.
33115796c8dcSSimon Schubert    SYMBOLS contains the symbol table for ABFD.
3312a45ae5f8SJohn Marino    DEBUG_SECTIONS contains the name of the dwarf debug sections.
33135796c8dcSSimon Schubert    ADDR_SIZE is the number of bytes in the initial .debug_info length
33145796c8dcSSimon Schubert    field and in the abbreviation offset, or zero to indicate that the
33155796c8dcSSimon Schubert    default value should be used.  */
33165796c8dcSSimon Schubert 
33175796c8dcSSimon Schubert static bfd_boolean
find_line(bfd * abfd,const struct dwarf_debug_section * debug_sections,asection * section,bfd_vma offset,asymbol * symbol,asymbol ** symbols,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * linenumber_ptr,unsigned int * discriminator_ptr,unsigned int addr_size,void ** pinfo)33185796c8dcSSimon Schubert find_line (bfd *abfd,
3319a45ae5f8SJohn Marino            const struct dwarf_debug_section *debug_sections,
33205796c8dcSSimon Schubert 	   asection *section,
33215796c8dcSSimon Schubert 	   bfd_vma offset,
33225796c8dcSSimon Schubert 	   asymbol *symbol,
33235796c8dcSSimon Schubert 	   asymbol **symbols,
33245796c8dcSSimon Schubert 	   const char **filename_ptr,
33255796c8dcSSimon Schubert 	   const char **functionname_ptr,
33265796c8dcSSimon Schubert 	   unsigned int *linenumber_ptr,
3327*ef5ccd6cSJohn Marino 	   unsigned int *discriminator_ptr,
33285796c8dcSSimon Schubert 	   unsigned int addr_size,
33295796c8dcSSimon Schubert 	   void **pinfo)
33305796c8dcSSimon Schubert {
33315796c8dcSSimon Schubert   /* Read each compilation unit from the section .debug_info, and check
33325796c8dcSSimon Schubert      to see if it contains the address we are searching for.  If yes,
33335796c8dcSSimon Schubert      lookup the address, and return the line number info.  If no, go
33345796c8dcSSimon Schubert      on to the next compilation unit.
33355796c8dcSSimon Schubert 
33365796c8dcSSimon Schubert      We keep a list of all the previously read compilation units, and
33375796c8dcSSimon Schubert      a pointer to the next un-read compilation unit.  Check the
33385796c8dcSSimon Schubert      previously read units before reading more.  */
33395796c8dcSSimon Schubert   struct dwarf2_debug *stash;
33405796c8dcSSimon Schubert   /* What address are we looking for?  */
33415796c8dcSSimon Schubert   bfd_vma addr;
33425796c8dcSSimon Schubert   struct comp_unit* each;
33435796c8dcSSimon Schubert   bfd_vma found = FALSE;
33445796c8dcSSimon Schubert   bfd_boolean do_line;
33455796c8dcSSimon Schubert 
3346*ef5ccd6cSJohn Marino   *filename_ptr = NULL;
3347*ef5ccd6cSJohn Marino   if (functionname_ptr != NULL)
3348*ef5ccd6cSJohn Marino     *functionname_ptr = NULL;
3349*ef5ccd6cSJohn Marino   *linenumber_ptr = 0;
3350*ef5ccd6cSJohn Marino   if (discriminator_ptr)
3351*ef5ccd6cSJohn Marino     *discriminator_ptr = 0;
33525796c8dcSSimon Schubert 
3353*ef5ccd6cSJohn Marino   if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL,
3354*ef5ccd6cSJohn Marino 				      debug_sections, symbols, pinfo))
33555796c8dcSSimon Schubert     return FALSE;
3356*ef5ccd6cSJohn Marino 
3357*ef5ccd6cSJohn Marino   stash = (struct dwarf2_debug *) *pinfo;
33585796c8dcSSimon Schubert 
33595796c8dcSSimon Schubert   /* In a relocatable file, 2 functions may have the same address.
33605796c8dcSSimon Schubert      We change the section vma so that they won't overlap.  */
33615796c8dcSSimon Schubert   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
33625796c8dcSSimon Schubert     {
33635796c8dcSSimon Schubert       if (! place_sections (abfd, stash))
33645796c8dcSSimon Schubert 	return FALSE;
33655796c8dcSSimon Schubert     }
33665796c8dcSSimon Schubert 
33675796c8dcSSimon Schubert   do_line = (section == NULL
33685796c8dcSSimon Schubert 	     && offset == 0
33695796c8dcSSimon Schubert 	     && functionname_ptr == NULL
33705796c8dcSSimon Schubert 	     && symbol != NULL);
33715796c8dcSSimon Schubert   if (do_line)
33725796c8dcSSimon Schubert     {
33735796c8dcSSimon Schubert       addr = symbol->value;
33745796c8dcSSimon Schubert       section = bfd_get_section (symbol);
33755796c8dcSSimon Schubert     }
33765796c8dcSSimon Schubert   else if (section != NULL
33775796c8dcSSimon Schubert 	   && functionname_ptr != NULL
33785796c8dcSSimon Schubert 	   && symbol == NULL)
33795796c8dcSSimon Schubert     addr = offset;
33805796c8dcSSimon Schubert   else
33815796c8dcSSimon Schubert     abort ();
33825796c8dcSSimon Schubert 
33835796c8dcSSimon Schubert   if (section->output_section)
33845796c8dcSSimon Schubert     addr += section->output_section->vma + section->output_offset;
33855796c8dcSSimon Schubert   else
33865796c8dcSSimon Schubert     addr += section->vma;
33875796c8dcSSimon Schubert 
33885796c8dcSSimon Schubert   /* A null info_ptr indicates that there is no dwarf2 info
33895796c8dcSSimon Schubert      (or that an error occured while setting up the stash).  */
33905796c8dcSSimon Schubert   if (! stash->info_ptr)
3391*ef5ccd6cSJohn Marino     return FALSE;
33925796c8dcSSimon Schubert 
33935796c8dcSSimon Schubert   stash->inliner_chain = NULL;
33945796c8dcSSimon Schubert 
33955796c8dcSSimon Schubert   /* Check the previously read comp. units first.  */
33965796c8dcSSimon Schubert   if (do_line)
33975796c8dcSSimon Schubert     {
33985796c8dcSSimon Schubert       /* The info hash tables use quite a bit of memory.  We may not want to
33995796c8dcSSimon Schubert 	 always use them.  We use some heuristics to decide if and when to
34005796c8dcSSimon Schubert 	 turn it on.  */
34015796c8dcSSimon Schubert       if (stash->info_hash_status == STASH_INFO_HASH_OFF)
34025796c8dcSSimon Schubert 	stash_maybe_enable_info_hash_tables (abfd, stash);
34035796c8dcSSimon Schubert 
34045796c8dcSSimon Schubert       /* Keep info hash table up to date if they are available.  Note that we
34055796c8dcSSimon Schubert 	 may disable the hash tables if there is any error duing update. */
34065796c8dcSSimon Schubert       if (stash->info_hash_status == STASH_INFO_HASH_ON)
34075796c8dcSSimon Schubert 	stash_maybe_update_info_hash_tables (stash);
34085796c8dcSSimon Schubert 
34095796c8dcSSimon Schubert       if (stash->info_hash_status == STASH_INFO_HASH_ON)
34105796c8dcSSimon Schubert 	{
34115796c8dcSSimon Schubert 	  found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
34125796c8dcSSimon Schubert 					linenumber_ptr);
34135796c8dcSSimon Schubert 	  if (found)
34145796c8dcSSimon Schubert 	    goto done;
34155796c8dcSSimon Schubert 	}
34165796c8dcSSimon Schubert       else
34175796c8dcSSimon Schubert 	{
34185796c8dcSSimon Schubert 	  /* Check the previously read comp. units first.  */
34195796c8dcSSimon Schubert 	  for (each = stash->all_comp_units; each; each = each->next_unit)
34205796c8dcSSimon Schubert 	    if ((symbol->flags & BSF_FUNCTION) == 0
3421*ef5ccd6cSJohn Marino 		|| each->arange.high == 0
34225796c8dcSSimon Schubert 		|| comp_unit_contains_address (each, addr))
34235796c8dcSSimon Schubert 	      {
34245796c8dcSSimon Schubert 		found = comp_unit_find_line (each, symbol, addr, filename_ptr,
34255796c8dcSSimon Schubert 					     linenumber_ptr, stash);
34265796c8dcSSimon Schubert 		if (found)
34275796c8dcSSimon Schubert 		  goto done;
34285796c8dcSSimon Schubert 	      }
34295796c8dcSSimon Schubert 	}
34305796c8dcSSimon Schubert     }
34315796c8dcSSimon Schubert   else
34325796c8dcSSimon Schubert     {
34335796c8dcSSimon Schubert       for (each = stash->all_comp_units; each; each = each->next_unit)
34345796c8dcSSimon Schubert 	{
3435*ef5ccd6cSJohn Marino 	  found = ((each->arange.high == 0
3436*ef5ccd6cSJohn Marino 		    || comp_unit_contains_address (each, addr))
34375796c8dcSSimon Schubert 		   && comp_unit_find_nearest_line (each, addr,
34385796c8dcSSimon Schubert 						   filename_ptr,
34395796c8dcSSimon Schubert 						   functionname_ptr,
34405796c8dcSSimon Schubert 						   linenumber_ptr,
3441*ef5ccd6cSJohn Marino 						   discriminator_ptr,
34425796c8dcSSimon Schubert 						   stash));
34435796c8dcSSimon Schubert 	  if (found)
34445796c8dcSSimon Schubert 	    goto done;
34455796c8dcSSimon Schubert 	}
34465796c8dcSSimon Schubert     }
34475796c8dcSSimon Schubert 
34485796c8dcSSimon Schubert   /* The DWARF2 spec says that the initial length field, and the
34495796c8dcSSimon Schubert      offset of the abbreviation table, should both be 4-byte values.
34505796c8dcSSimon Schubert      However, some compilers do things differently.  */
34515796c8dcSSimon Schubert   if (addr_size == 0)
34525796c8dcSSimon Schubert     addr_size = 4;
34535796c8dcSSimon Schubert   BFD_ASSERT (addr_size == 4 || addr_size == 8);
34545796c8dcSSimon Schubert 
34555796c8dcSSimon Schubert   /* Read each remaining comp. units checking each as they are read.  */
34565796c8dcSSimon Schubert   while (stash->info_ptr < stash->info_ptr_end)
34575796c8dcSSimon Schubert     {
34585796c8dcSSimon Schubert       bfd_vma length;
34595796c8dcSSimon Schubert       unsigned int offset_size = addr_size;
34605796c8dcSSimon Schubert       bfd_byte *info_ptr_unit = stash->info_ptr;
34615796c8dcSSimon Schubert 
34625796c8dcSSimon Schubert       length = read_4_bytes (stash->bfd_ptr, stash->info_ptr);
34635796c8dcSSimon Schubert       /* A 0xffffff length is the DWARF3 way of indicating
34645796c8dcSSimon Schubert 	 we use 64-bit offsets, instead of 32-bit offsets.  */
34655796c8dcSSimon Schubert       if (length == 0xffffffff)
34665796c8dcSSimon Schubert 	{
34675796c8dcSSimon Schubert 	  offset_size = 8;
34685796c8dcSSimon Schubert 	  length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4);
34695796c8dcSSimon Schubert 	  stash->info_ptr += 12;
34705796c8dcSSimon Schubert 	}
34715796c8dcSSimon Schubert       /* A zero length is the IRIX way of indicating 64-bit offsets,
34725796c8dcSSimon Schubert 	 mostly because the 64-bit length will generally fit in 32
34735796c8dcSSimon Schubert 	 bits, and the endianness helps.  */
34745796c8dcSSimon Schubert       else if (length == 0)
34755796c8dcSSimon Schubert 	{
34765796c8dcSSimon Schubert 	  offset_size = 8;
34775796c8dcSSimon Schubert 	  length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4);
34785796c8dcSSimon Schubert 	  stash->info_ptr += 8;
34795796c8dcSSimon Schubert 	}
34805796c8dcSSimon Schubert       /* In the absence of the hints above, we assume 32-bit DWARF2
34815796c8dcSSimon Schubert 	 offsets even for targets with 64-bit addresses, because:
34825796c8dcSSimon Schubert 	   a) most of the time these targets will not have generated
34835796c8dcSSimon Schubert 	      more than 2Gb of debug info and so will not need 64-bit
34845796c8dcSSimon Schubert 	      offsets,
34855796c8dcSSimon Schubert 	 and
34865796c8dcSSimon Schubert 	   b) if they do use 64-bit offsets but they are not using
34875796c8dcSSimon Schubert 	      the size hints that are tested for above then they are
34885796c8dcSSimon Schubert 	      not conforming to the DWARF3 standard anyway.  */
34895796c8dcSSimon Schubert       else if (addr_size == 8)
34905796c8dcSSimon Schubert 	{
34915796c8dcSSimon Schubert 	  offset_size = 4;
34925796c8dcSSimon Schubert 	  stash->info_ptr += 4;
34935796c8dcSSimon Schubert 	}
34945796c8dcSSimon Schubert       else
34955796c8dcSSimon Schubert 	stash->info_ptr += 4;
34965796c8dcSSimon Schubert 
34975796c8dcSSimon Schubert       if (length > 0)
34985796c8dcSSimon Schubert 	{
34995796c8dcSSimon Schubert 	  each = parse_comp_unit (stash, length, info_ptr_unit,
35005796c8dcSSimon Schubert 				  offset_size);
35015796c8dcSSimon Schubert 	  if (!each)
35025796c8dcSSimon Schubert 	    /* The dwarf information is damaged, don't trust it any
35035796c8dcSSimon Schubert 	       more.  */
35045796c8dcSSimon Schubert 	    break;
35055796c8dcSSimon Schubert 	  stash->info_ptr += length;
35065796c8dcSSimon Schubert 
35075796c8dcSSimon Schubert 	  if (stash->all_comp_units)
35085796c8dcSSimon Schubert 	    stash->all_comp_units->prev_unit = each;
35095796c8dcSSimon Schubert 	  else
35105796c8dcSSimon Schubert 	    stash->last_comp_unit = each;
35115796c8dcSSimon Schubert 
35125796c8dcSSimon Schubert 	  each->next_unit = stash->all_comp_units;
35135796c8dcSSimon Schubert 	  stash->all_comp_units = each;
35145796c8dcSSimon Schubert 
35155796c8dcSSimon Schubert 	  /* DW_AT_low_pc and DW_AT_high_pc are optional for
35165796c8dcSSimon Schubert 	     compilation units.  If we don't have them (i.e.,
35175796c8dcSSimon Schubert 	     unit->high == 0), we need to consult the line info table
35185796c8dcSSimon Schubert 	     to see if a compilation unit contains the given
35195796c8dcSSimon Schubert 	     address.  */
35205796c8dcSSimon Schubert 	  if (do_line)
35215796c8dcSSimon Schubert 	    found = (((symbol->flags & BSF_FUNCTION) == 0
35225796c8dcSSimon Schubert 		      || each->arange.high == 0
35235796c8dcSSimon Schubert 		      || comp_unit_contains_address (each, addr))
35245796c8dcSSimon Schubert 		     && comp_unit_find_line (each, symbol, addr,
35255796c8dcSSimon Schubert 					     filename_ptr,
35265796c8dcSSimon Schubert 					     linenumber_ptr,
35275796c8dcSSimon Schubert 					     stash));
35285796c8dcSSimon Schubert 	  else
35295796c8dcSSimon Schubert 	    found = ((each->arange.high == 0
35305796c8dcSSimon Schubert 		      || comp_unit_contains_address (each, addr))
35315796c8dcSSimon Schubert 		     && comp_unit_find_nearest_line (each, addr,
35325796c8dcSSimon Schubert 						     filename_ptr,
35335796c8dcSSimon Schubert 						     functionname_ptr,
35345796c8dcSSimon Schubert 						     linenumber_ptr,
3535*ef5ccd6cSJohn Marino 						     discriminator_ptr,
35365796c8dcSSimon Schubert 						     stash));
35375796c8dcSSimon Schubert 
35385796c8dcSSimon Schubert 	  if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
35395796c8dcSSimon Schubert 	      == stash->sec->size)
35405796c8dcSSimon Schubert 	    {
3541a45ae5f8SJohn Marino 	      stash->sec = find_debug_info (stash->bfd_ptr, debug_sections,
3542a45ae5f8SJohn Marino                                             stash->sec);
35435796c8dcSSimon Schubert 	      stash->sec_info_ptr = stash->info_ptr;
35445796c8dcSSimon Schubert 	    }
35455796c8dcSSimon Schubert 
35465796c8dcSSimon Schubert 	  if (found)
35475796c8dcSSimon Schubert 	    goto done;
35485796c8dcSSimon Schubert 	}
35495796c8dcSSimon Schubert     }
35505796c8dcSSimon Schubert 
35515796c8dcSSimon Schubert  done:
35525796c8dcSSimon Schubert   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
35535796c8dcSSimon Schubert     unset_sections (stash);
35545796c8dcSSimon Schubert 
35555796c8dcSSimon Schubert   return found;
35565796c8dcSSimon Schubert }
35575796c8dcSSimon Schubert 
35585796c8dcSSimon Schubert /* The DWARF2 version of find_nearest_line.
35595796c8dcSSimon Schubert    Return TRUE if the line is found without error.  */
35605796c8dcSSimon Schubert 
35615796c8dcSSimon Schubert bfd_boolean
_bfd_dwarf2_find_nearest_line(bfd * abfd,const struct dwarf_debug_section * debug_sections,asection * section,asymbol ** symbols,bfd_vma offset,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * linenumber_ptr,unsigned int * discriminator_ptr,unsigned int addr_size,void ** pinfo)35625796c8dcSSimon Schubert _bfd_dwarf2_find_nearest_line (bfd *abfd,
3563a45ae5f8SJohn Marino                                const struct dwarf_debug_section *debug_sections,
35645796c8dcSSimon Schubert 			       asection *section,
35655796c8dcSSimon Schubert 			       asymbol **symbols,
35665796c8dcSSimon Schubert 			       bfd_vma offset,
35675796c8dcSSimon Schubert 			       const char **filename_ptr,
35685796c8dcSSimon Schubert 			       const char **functionname_ptr,
35695796c8dcSSimon Schubert 			       unsigned int *linenumber_ptr,
3570*ef5ccd6cSJohn Marino                                unsigned int *discriminator_ptr,
35715796c8dcSSimon Schubert 			       unsigned int addr_size,
35725796c8dcSSimon Schubert 			       void **pinfo)
35735796c8dcSSimon Schubert {
3574a45ae5f8SJohn Marino   return find_line (abfd, debug_sections, section, offset, NULL, symbols,
3575*ef5ccd6cSJohn Marino                     filename_ptr, functionname_ptr, linenumber_ptr,
3576*ef5ccd6cSJohn Marino                     discriminator_ptr, addr_size, pinfo);
35775796c8dcSSimon Schubert }
35785796c8dcSSimon Schubert 
35795796c8dcSSimon Schubert /* The DWARF2 version of find_line.
35805796c8dcSSimon Schubert    Return TRUE if the line is found without error.  */
35815796c8dcSSimon Schubert 
35825796c8dcSSimon Schubert bfd_boolean
_bfd_dwarf2_find_line(bfd * abfd,asymbol ** symbols,asymbol * symbol,const char ** filename_ptr,unsigned int * linenumber_ptr,unsigned int * discriminator_ptr,unsigned int addr_size,void ** pinfo)35835796c8dcSSimon Schubert _bfd_dwarf2_find_line (bfd *abfd,
35845796c8dcSSimon Schubert 		       asymbol **symbols,
35855796c8dcSSimon Schubert 		       asymbol *symbol,
35865796c8dcSSimon Schubert 		       const char **filename_ptr,
35875796c8dcSSimon Schubert 		       unsigned int *linenumber_ptr,
3588*ef5ccd6cSJohn Marino                        unsigned int *discriminator_ptr,
35895796c8dcSSimon Schubert 		       unsigned int addr_size,
35905796c8dcSSimon Schubert 		       void **pinfo)
35915796c8dcSSimon Schubert {
3592a45ae5f8SJohn Marino   return find_line (abfd, dwarf_debug_sections, NULL, 0, symbol, symbols,
3593*ef5ccd6cSJohn Marino                     filename_ptr, NULL, linenumber_ptr, discriminator_ptr,
3594*ef5ccd6cSJohn Marino                     addr_size, pinfo);
35955796c8dcSSimon Schubert }
35965796c8dcSSimon Schubert 
35975796c8dcSSimon Schubert bfd_boolean
_bfd_dwarf2_find_inliner_info(bfd * abfd ATTRIBUTE_UNUSED,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * linenumber_ptr,void ** pinfo)35985796c8dcSSimon Schubert _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
35995796c8dcSSimon Schubert 			       const char **filename_ptr,
36005796c8dcSSimon Schubert 			       const char **functionname_ptr,
36015796c8dcSSimon Schubert 			       unsigned int *linenumber_ptr,
36025796c8dcSSimon Schubert 			       void **pinfo)
36035796c8dcSSimon Schubert {
36045796c8dcSSimon Schubert   struct dwarf2_debug *stash;
36055796c8dcSSimon Schubert 
36065796c8dcSSimon Schubert   stash = (struct dwarf2_debug *) *pinfo;
36075796c8dcSSimon Schubert   if (stash)
36085796c8dcSSimon Schubert     {
36095796c8dcSSimon Schubert       struct funcinfo *func = stash->inliner_chain;
36105796c8dcSSimon Schubert 
36115796c8dcSSimon Schubert       if (func && func->caller_func)
36125796c8dcSSimon Schubert 	{
36135796c8dcSSimon Schubert 	  *filename_ptr = func->caller_file;
36145796c8dcSSimon Schubert 	  *functionname_ptr = func->caller_func->name;
36155796c8dcSSimon Schubert 	  *linenumber_ptr = func->caller_line;
36165796c8dcSSimon Schubert 	  stash->inliner_chain = func->caller_func;
36175796c8dcSSimon Schubert 	  return TRUE;
36185796c8dcSSimon Schubert 	}
36195796c8dcSSimon Schubert     }
36205796c8dcSSimon Schubert 
36215796c8dcSSimon Schubert   return FALSE;
36225796c8dcSSimon Schubert }
36235796c8dcSSimon Schubert 
36245796c8dcSSimon Schubert void
_bfd_dwarf2_cleanup_debug_info(bfd * abfd,void ** pinfo)3625*ef5ccd6cSJohn Marino _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
36265796c8dcSSimon Schubert {
3627*ef5ccd6cSJohn Marino   struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
36285796c8dcSSimon Schubert   struct comp_unit *each;
36295796c8dcSSimon Schubert 
3630*ef5ccd6cSJohn Marino   if (abfd == NULL || stash == NULL)
36315796c8dcSSimon Schubert     return;
36325796c8dcSSimon Schubert 
36335796c8dcSSimon Schubert   for (each = stash->all_comp_units; each; each = each->next_unit)
36345796c8dcSSimon Schubert     {
36355796c8dcSSimon Schubert       struct abbrev_info **abbrevs = each->abbrevs;
36365796c8dcSSimon Schubert       struct funcinfo *function_table = each->function_table;
36375796c8dcSSimon Schubert       struct varinfo *variable_table = each->variable_table;
36385796c8dcSSimon Schubert       size_t i;
36395796c8dcSSimon Schubert 
36405796c8dcSSimon Schubert       for (i = 0; i < ABBREV_HASH_SIZE; i++)
36415796c8dcSSimon Schubert 	{
36425796c8dcSSimon Schubert 	  struct abbrev_info *abbrev = abbrevs[i];
36435796c8dcSSimon Schubert 
36445796c8dcSSimon Schubert 	  while (abbrev)
36455796c8dcSSimon Schubert 	    {
36465796c8dcSSimon Schubert 	      free (abbrev->attrs);
36475796c8dcSSimon Schubert 	      abbrev = abbrev->next;
36485796c8dcSSimon Schubert 	    }
36495796c8dcSSimon Schubert 	}
36505796c8dcSSimon Schubert 
36515796c8dcSSimon Schubert       if (each->line_table)
36525796c8dcSSimon Schubert 	{
36535796c8dcSSimon Schubert 	  free (each->line_table->dirs);
36545796c8dcSSimon Schubert 	  free (each->line_table->files);
36555796c8dcSSimon Schubert 	}
36565796c8dcSSimon Schubert 
36575796c8dcSSimon Schubert       while (function_table)
36585796c8dcSSimon Schubert 	{
36595796c8dcSSimon Schubert 	  if (function_table->file)
36605796c8dcSSimon Schubert 	    {
36615796c8dcSSimon Schubert 	      free (function_table->file);
36625796c8dcSSimon Schubert 	      function_table->file = NULL;
36635796c8dcSSimon Schubert 	    }
36645796c8dcSSimon Schubert 
36655796c8dcSSimon Schubert 	  if (function_table->caller_file)
36665796c8dcSSimon Schubert 	    {
36675796c8dcSSimon Schubert 	      free (function_table->caller_file);
36685796c8dcSSimon Schubert 	      function_table->caller_file = NULL;
36695796c8dcSSimon Schubert 	    }
36705796c8dcSSimon Schubert 	  function_table = function_table->prev_func;
36715796c8dcSSimon Schubert 	}
36725796c8dcSSimon Schubert 
36735796c8dcSSimon Schubert       while (variable_table)
36745796c8dcSSimon Schubert 	{
36755796c8dcSSimon Schubert 	  if (variable_table->file)
36765796c8dcSSimon Schubert 	    {
36775796c8dcSSimon Schubert 	      free (variable_table->file);
36785796c8dcSSimon Schubert 	      variable_table->file = NULL;
36795796c8dcSSimon Schubert 	    }
36805796c8dcSSimon Schubert 
36815796c8dcSSimon Schubert 	  variable_table = variable_table->prev_var;
36825796c8dcSSimon Schubert 	}
36835796c8dcSSimon Schubert     }
36845796c8dcSSimon Schubert 
36855796c8dcSSimon Schubert   if (stash->dwarf_abbrev_buffer)
36865796c8dcSSimon Schubert     free (stash->dwarf_abbrev_buffer);
36875796c8dcSSimon Schubert   if (stash->dwarf_line_buffer)
36885796c8dcSSimon Schubert     free (stash->dwarf_line_buffer);
36895796c8dcSSimon Schubert   if (stash->dwarf_str_buffer)
36905796c8dcSSimon Schubert     free (stash->dwarf_str_buffer);
36915796c8dcSSimon Schubert   if (stash->dwarf_ranges_buffer)
36925796c8dcSSimon Schubert     free (stash->dwarf_ranges_buffer);
36935796c8dcSSimon Schubert   if (stash->info_ptr_memory)
36945796c8dcSSimon Schubert     free (stash->info_ptr_memory);
3695*ef5ccd6cSJohn Marino   if (stash->close_on_cleanup)
3696*ef5ccd6cSJohn Marino     bfd_close (stash->bfd_ptr);
36975796c8dcSSimon Schubert }
3698