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