xref: /trueos/contrib/binutils/bfd/dwarf2.c (revision 17d83a70d11062ccf00ec19e142b61af05794ef2)
1 /* DWARF 2 support.
2    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004, 2005, 2006, 2007 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 2 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, MA 02110-1301, USA.  */
31 
32 #include "sysdep.h"
33 #include "bfd.h"
34 #include "libiberty.h"
35 #include "libbfd.h"
36 #include "elf-bfd.h"
37 #include "elf/dwarf2.h"
38 
39 /* The data in the .debug_line statement prologue looks like this.  */
40 
41 struct line_head
42 {
43   bfd_vma total_length;
44   unsigned short version;
45   bfd_vma prologue_length;
46   unsigned char minimum_instruction_length;
47   unsigned char default_is_stmt;
48   int line_base;
49   unsigned char line_range;
50   unsigned char opcode_base;
51   unsigned char *standard_opcode_lengths;
52 };
53 
54 /* Attributes have a name and a value.  */
55 
56 struct attribute
57 {
58   enum dwarf_attribute name;
59   enum dwarf_form form;
60   union
61   {
62     char *str;
63     struct dwarf_block *blk;
64     bfd_uint64_t val;
65     bfd_int64_t sval;
66   }
67   u;
68 };
69 
70 /* Blocks are a bunch of untyped bytes.  */
71 struct dwarf_block
72 {
73   unsigned int size;
74   bfd_byte *data;
75 };
76 
77 struct loadable_section
78 {
79   asection *section;
80   bfd_vma adj_vma;
81 };
82 
83 struct dwarf2_debug
84 {
85   /* A list of all previously read comp_units.  */
86   struct comp_unit *all_comp_units;
87 
88   /* The next unread compilation unit within the .debug_info section.
89      Zero indicates that the .debug_info section has not been loaded
90      into a buffer yet.  */
91   bfd_byte *info_ptr;
92 
93   /* Pointer to the end of the .debug_info section memory buffer.  */
94   bfd_byte *info_ptr_end;
95 
96   /* Pointer to the bfd, section and address of the beginning of the
97      section.  The bfd might be different than expected because of
98      gnu_debuglink sections.  */
99   bfd * bfd;
100   asection *sec;
101   bfd_byte *sec_info_ptr;
102 
103   /* Pointer to the symbol table.  */
104   asymbol **syms;
105 
106   /* Pointer to the .debug_abbrev section loaded into memory.  */
107   bfd_byte *dwarf_abbrev_buffer;
108 
109   /* Length of the loaded .debug_abbrev section.  */
110   unsigned long dwarf_abbrev_size;
111 
112   /* Buffer for decode_line_info.  */
113   bfd_byte *dwarf_line_buffer;
114 
115   /* Length of the loaded .debug_line section.  */
116   unsigned long dwarf_line_size;
117 
118   /* Pointer to the .debug_str section loaded into memory.  */
119   bfd_byte *dwarf_str_buffer;
120 
121   /* Length of the loaded .debug_str section.  */
122   unsigned long dwarf_str_size;
123 
124   /* Pointer to the .debug_ranges section loaded into memory. */
125   bfd_byte *dwarf_ranges_buffer;
126 
127   /* Length of the loaded .debug_ranges section. */
128   unsigned long dwarf_ranges_size;
129 
130   /* If the most recent call to bfd_find_nearest_line was given an
131      address in an inlined function, preserve a pointer into the
132      calling chain for subsequent calls to bfd_find_inliner_info to
133      use. */
134   struct funcinfo *inliner_chain;
135 
136   /* Number of loadable sections.  */
137   unsigned int loadable_section_count;
138 
139   /* Array of loadable sections.  */
140   struct loadable_section *loadable_sections;
141 };
142 
143 struct arange
144 {
145   struct arange *next;
146   bfd_vma low;
147   bfd_vma high;
148 };
149 
150 /* A minimal decoding of DWARF2 compilation units.  We only decode
151    what's needed to get to the line number information.  */
152 
153 struct comp_unit
154 {
155   /* Chain the previously read compilation units.  */
156   struct comp_unit *next_unit;
157 
158   /* Keep the bfd convenient (for memory allocation).  */
159   bfd *abfd;
160 
161   /* The lowest and highest addresses contained in this compilation
162      unit as specified in the compilation unit header.  */
163   struct arange arange;
164 
165   /* The DW_AT_name attribute (for error messages).  */
166   char *name;
167 
168   /* The abbrev hash table.  */
169   struct abbrev_info **abbrevs;
170 
171   /* Note that an error was found by comp_unit_find_nearest_line.  */
172   int error;
173 
174   /* The DW_AT_comp_dir attribute.  */
175   char *comp_dir;
176 
177   /* TRUE if there is a line number table associated with this comp. unit.  */
178   int stmtlist;
179 
180   /* Pointer to the current comp_unit so that we can find a given entry
181      by its reference.  */
182   bfd_byte *info_ptr_unit;
183 
184   /* The offset into .debug_line of the line number table.  */
185   unsigned long line_offset;
186 
187   /* Pointer to the first child die for the comp unit.  */
188   bfd_byte *first_child_die_ptr;
189 
190   /* The end of the comp unit.  */
191   bfd_byte *end_ptr;
192 
193   /* The decoded line number, NULL if not yet decoded.  */
194   struct line_info_table *line_table;
195 
196   /* A list of the functions found in this comp. unit.  */
197   struct funcinfo *function_table;
198 
199   /* A list of the variables found in this comp. unit.  */
200   struct varinfo *variable_table;
201 
202   /* Pointer to dwarf2_debug structure.  */
203   struct dwarf2_debug *stash;
204 
205   /* Address size for this unit - from unit header.  */
206   unsigned char addr_size;
207 
208   /* Offset size for this unit - from unit header.  */
209   unsigned char offset_size;
210 
211   /* Base address for this unit - from DW_AT_low_pc attribute of
212      DW_TAG_compile_unit DIE */
213   bfd_vma base_address;
214 };
215 
216 /* This data structure holds the information of an abbrev.  */
217 struct abbrev_info
218 {
219   unsigned int number;		/* Number identifying abbrev.  */
220   enum dwarf_tag tag;		/* DWARF tag.  */
221   int has_children;		/* Boolean.  */
222   unsigned int num_attrs;	/* Number of attributes.  */
223   struct attr_abbrev *attrs;	/* An array of attribute descriptions.  */
224   struct abbrev_info *next;	/* Next in chain.  */
225 };
226 
227 struct attr_abbrev
228 {
229   enum dwarf_attribute name;
230   enum dwarf_form form;
231 };
232 
233 #ifndef ABBREV_HASH_SIZE
234 #define ABBREV_HASH_SIZE 121
235 #endif
236 #ifndef ATTR_ALLOC_CHUNK
237 #define ATTR_ALLOC_CHUNK 4
238 #endif
239 
240 /* VERBATIM
241    The following function up to the END VERBATIM mark are
242    copied directly from dwarf2read.c.  */
243 
244 /* Read dwarf information from a buffer.  */
245 
246 static unsigned int
read_1_byte(bfd * abfd ATTRIBUTE_UNUSED,bfd_byte * buf)247 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
248 {
249   return bfd_get_8 (abfd, buf);
250 }
251 
252 static int
read_1_signed_byte(bfd * abfd ATTRIBUTE_UNUSED,bfd_byte * buf)253 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
254 {
255   return bfd_get_signed_8 (abfd, buf);
256 }
257 
258 static unsigned int
read_2_bytes(bfd * abfd,bfd_byte * buf)259 read_2_bytes (bfd *abfd, bfd_byte *buf)
260 {
261   return bfd_get_16 (abfd, buf);
262 }
263 
264 static unsigned int
read_4_bytes(bfd * abfd,bfd_byte * buf)265 read_4_bytes (bfd *abfd, bfd_byte *buf)
266 {
267   return bfd_get_32 (abfd, buf);
268 }
269 
270 static bfd_uint64_t
read_8_bytes(bfd * abfd,bfd_byte * buf)271 read_8_bytes (bfd *abfd, bfd_byte *buf)
272 {
273   return bfd_get_64 (abfd, buf);
274 }
275 
276 static bfd_byte *
read_n_bytes(bfd * abfd ATTRIBUTE_UNUSED,bfd_byte * buf,unsigned int size ATTRIBUTE_UNUSED)277 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
278 	      bfd_byte *buf,
279 	      unsigned int size ATTRIBUTE_UNUSED)
280 {
281   /* If the size of a host char is 8 bits, we can return a pointer
282      to the buffer, otherwise we have to copy the data to a buffer
283      allocated on the temporary obstack.  */
284   return buf;
285 }
286 
287 static char *
read_string(bfd * abfd ATTRIBUTE_UNUSED,bfd_byte * buf,unsigned int * bytes_read_ptr)288 read_string (bfd *abfd ATTRIBUTE_UNUSED,
289 	     bfd_byte *buf,
290 	     unsigned int *bytes_read_ptr)
291 {
292   /* Return a pointer to the embedded string.  */
293   char *str = (char *) buf;
294   if (*str == '\0')
295     {
296       *bytes_read_ptr = 1;
297       return NULL;
298     }
299 
300   *bytes_read_ptr = strlen (str) + 1;
301   return str;
302 }
303 
304 static char *
read_indirect_string(struct comp_unit * unit,bfd_byte * buf,unsigned int * bytes_read_ptr)305 read_indirect_string (struct comp_unit* unit,
306 		      bfd_byte *buf,
307 		      unsigned int *bytes_read_ptr)
308 {
309   bfd_uint64_t offset;
310   struct dwarf2_debug *stash = unit->stash;
311   char *str;
312 
313   if (unit->offset_size == 4)
314     offset = read_4_bytes (unit->abfd, buf);
315   else
316     offset = read_8_bytes (unit->abfd, buf);
317   *bytes_read_ptr = unit->offset_size;
318 
319   if (! stash->dwarf_str_buffer)
320     {
321       asection *msec;
322       bfd *abfd = unit->abfd;
323       bfd_size_type sz;
324 
325       msec = bfd_get_section_by_name (abfd, ".debug_str");
326       if (! msec)
327 	{
328 	  (*_bfd_error_handler)
329 	    (_("Dwarf Error: Can't find .debug_str section."));
330 	  bfd_set_error (bfd_error_bad_value);
331 	  return NULL;
332 	}
333 
334       sz = msec->rawsize ? msec->rawsize : msec->size;
335       stash->dwarf_str_size = sz;
336       stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
337       if (! stash->dwarf_str_buffer)
338 	return NULL;
339 
340       if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
341 				      0, sz))
342 	return NULL;
343     }
344 
345   if (offset >= stash->dwarf_str_size)
346     {
347       (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
348 			     (unsigned long) offset, stash->dwarf_str_size);
349       bfd_set_error (bfd_error_bad_value);
350       return NULL;
351     }
352 
353   str = (char *) stash->dwarf_str_buffer + offset;
354   if (*str == '\0')
355     return NULL;
356   return str;
357 }
358 
359 /* END VERBATIM */
360 
361 static bfd_uint64_t
read_address(struct comp_unit * unit,bfd_byte * buf)362 read_address (struct comp_unit *unit, bfd_byte *buf)
363 {
364   int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
365 
366   if (signed_vma)
367     {
368       switch (unit->addr_size)
369 	{
370 	case 8:
371 	  return bfd_get_signed_64 (unit->abfd, buf);
372 	case 4:
373 	  return bfd_get_signed_32 (unit->abfd, buf);
374 	case 2:
375 	  return bfd_get_signed_16 (unit->abfd, buf);
376 	default:
377 	  abort ();
378 	}
379     }
380   else
381     {
382       switch (unit->addr_size)
383 	{
384 	case 8:
385 	  return bfd_get_64 (unit->abfd, buf);
386 	case 4:
387 	  return bfd_get_32 (unit->abfd, buf);
388 	case 2:
389 	  return bfd_get_16 (unit->abfd, buf);
390 	default:
391 	  abort ();
392 	}
393     }
394 }
395 
396 /* Lookup an abbrev_info structure in the abbrev hash table.  */
397 
398 static struct abbrev_info *
lookup_abbrev(unsigned int number,struct abbrev_info ** abbrevs)399 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
400 {
401   unsigned int hash_number;
402   struct abbrev_info *abbrev;
403 
404   hash_number = number % ABBREV_HASH_SIZE;
405   abbrev = abbrevs[hash_number];
406 
407   while (abbrev)
408     {
409       if (abbrev->number == number)
410 	return abbrev;
411       else
412 	abbrev = abbrev->next;
413     }
414 
415   return NULL;
416 }
417 
418 /* In DWARF version 2, the description of the debugging information is
419    stored in a separate .debug_abbrev section.  Before we read any
420    dies from a section we read in all abbreviations and install them
421    in a hash table.  */
422 
423 static struct abbrev_info**
read_abbrevs(bfd * abfd,bfd_uint64_t offset,struct dwarf2_debug * stash)424 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
425 {
426   struct abbrev_info **abbrevs;
427   bfd_byte *abbrev_ptr;
428   struct abbrev_info *cur_abbrev;
429   unsigned int abbrev_number, bytes_read, abbrev_name;
430   unsigned int abbrev_form, hash_number;
431   bfd_size_type amt;
432 
433   if (! stash->dwarf_abbrev_buffer)
434     {
435       asection *msec;
436 
437       msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
438       if (! msec)
439 	{
440 	  (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
441 	  bfd_set_error (bfd_error_bad_value);
442 	  return 0;
443 	}
444 
445       stash->dwarf_abbrev_size = msec->size;
446       stash->dwarf_abbrev_buffer
447 	= bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
448 						     stash->syms);
449       if (! stash->dwarf_abbrev_buffer)
450 	  return 0;
451     }
452 
453   if (offset >= stash->dwarf_abbrev_size)
454     {
455       (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
456 			     (unsigned long) offset, stash->dwarf_abbrev_size);
457       bfd_set_error (bfd_error_bad_value);
458       return 0;
459     }
460 
461   amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
462   abbrevs = bfd_zalloc (abfd, amt);
463 
464   abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
465   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
466   abbrev_ptr += bytes_read;
467 
468   /* Loop until we reach an abbrev number of 0.  */
469   while (abbrev_number)
470     {
471       amt = sizeof (struct abbrev_info);
472       cur_abbrev = bfd_zalloc (abfd, amt);
473 
474       /* Read in abbrev header.  */
475       cur_abbrev->number = abbrev_number;
476       cur_abbrev->tag = (enum dwarf_tag)
477 	read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
478       abbrev_ptr += bytes_read;
479       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
480       abbrev_ptr += 1;
481 
482       /* Now read in declarations.  */
483       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
484       abbrev_ptr += bytes_read;
485       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
486       abbrev_ptr += bytes_read;
487 
488       while (abbrev_name)
489 	{
490 	  if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
491 	    {
492 	      struct attr_abbrev *tmp;
493 
494 	      amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
495 	      amt *= sizeof (struct attr_abbrev);
496 	      tmp = bfd_realloc (cur_abbrev->attrs, amt);
497 	      if (tmp == NULL)
498 		{
499 		  size_t i;
500 
501 		  for (i = 0; i < ABBREV_HASH_SIZE; i++)
502 		    {
503 		      struct abbrev_info *abbrev = abbrevs[i];
504 
505 		      while (abbrev)
506 			{
507 			  free (abbrev->attrs);
508 			  abbrev = abbrev->next;
509 			}
510 		    }
511 		  return NULL;
512 		}
513 	      cur_abbrev->attrs = tmp;
514 	    }
515 
516 	  cur_abbrev->attrs[cur_abbrev->num_attrs].name
517 	    = (enum dwarf_attribute) abbrev_name;
518 	  cur_abbrev->attrs[cur_abbrev->num_attrs++].form
519 	    = (enum dwarf_form) abbrev_form;
520 	  abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
521 	  abbrev_ptr += bytes_read;
522 	  abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
523 	  abbrev_ptr += bytes_read;
524 	}
525 
526       hash_number = abbrev_number % ABBREV_HASH_SIZE;
527       cur_abbrev->next = abbrevs[hash_number];
528       abbrevs[hash_number] = cur_abbrev;
529 
530       /* Get next abbreviation.
531 	 Under Irix6 the abbreviations for a compilation unit are not
532 	 always properly terminated with an abbrev number of 0.
533 	 Exit loop if we encounter an abbreviation which we have
534 	 already read (which means we are about to read the abbreviations
535 	 for the next compile unit) or if the end of the abbreviation
536 	 table is reached.  */
537       if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
538 	  >= stash->dwarf_abbrev_size)
539 	break;
540       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
541       abbrev_ptr += bytes_read;
542       if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
543 	break;
544     }
545 
546   return abbrevs;
547 }
548 
549 /* Read an attribute value described by an attribute form.  */
550 
551 static bfd_byte *
read_attribute_value(struct attribute * attr,unsigned form,struct comp_unit * unit,bfd_byte * info_ptr)552 read_attribute_value (struct attribute *attr,
553 		      unsigned form,
554 		      struct comp_unit *unit,
555 		      bfd_byte *info_ptr)
556 {
557   bfd *abfd = unit->abfd;
558   unsigned int bytes_read;
559   struct dwarf_block *blk;
560   bfd_size_type amt;
561 
562   attr->form = (enum dwarf_form) form;
563 
564   switch (form)
565     {
566     case DW_FORM_addr:
567       /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size.  */
568     case DW_FORM_ref_addr:
569       attr->u.val = read_address (unit, info_ptr);
570       info_ptr += unit->addr_size;
571       break;
572     case DW_FORM_block2:
573       amt = sizeof (struct dwarf_block);
574       blk = bfd_alloc (abfd, amt);
575       blk->size = read_2_bytes (abfd, info_ptr);
576       info_ptr += 2;
577       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
578       info_ptr += blk->size;
579       attr->u.blk = blk;
580       break;
581     case DW_FORM_block4:
582       amt = sizeof (struct dwarf_block);
583       blk = bfd_alloc (abfd, amt);
584       blk->size = read_4_bytes (abfd, info_ptr);
585       info_ptr += 4;
586       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
587       info_ptr += blk->size;
588       attr->u.blk = blk;
589       break;
590     case DW_FORM_data2:
591       attr->u.val = read_2_bytes (abfd, info_ptr);
592       info_ptr += 2;
593       break;
594     case DW_FORM_data4:
595       attr->u.val = read_4_bytes (abfd, info_ptr);
596       info_ptr += 4;
597       break;
598     case DW_FORM_data8:
599       attr->u.val = read_8_bytes (abfd, info_ptr);
600       info_ptr += 8;
601       break;
602     case DW_FORM_string:
603       attr->u.str = read_string (abfd, info_ptr, &bytes_read);
604       info_ptr += bytes_read;
605       break;
606     case DW_FORM_strp:
607       attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
608       info_ptr += bytes_read;
609       break;
610     case DW_FORM_block:
611       amt = sizeof (struct dwarf_block);
612       blk = bfd_alloc (abfd, amt);
613       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
614       info_ptr += bytes_read;
615       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
616       info_ptr += blk->size;
617       attr->u.blk = blk;
618       break;
619     case DW_FORM_block1:
620       amt = sizeof (struct dwarf_block);
621       blk = bfd_alloc (abfd, amt);
622       blk->size = read_1_byte (abfd, info_ptr);
623       info_ptr += 1;
624       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
625       info_ptr += blk->size;
626       attr->u.blk = blk;
627       break;
628     case DW_FORM_data1:
629       attr->u.val = read_1_byte (abfd, info_ptr);
630       info_ptr += 1;
631       break;
632     case DW_FORM_flag:
633       attr->u.val = read_1_byte (abfd, info_ptr);
634       info_ptr += 1;
635       break;
636     case DW_FORM_flag_present:
637       attr->u.val = 1;
638       break;
639     case DW_FORM_sdata:
640       attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
641       info_ptr += bytes_read;
642       break;
643     case DW_FORM_udata:
644       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
645       info_ptr += bytes_read;
646       break;
647     case DW_FORM_ref1:
648       attr->u.val = read_1_byte (abfd, info_ptr);
649       info_ptr += 1;
650       break;
651     case DW_FORM_ref2:
652       attr->u.val = read_2_bytes (abfd, info_ptr);
653       info_ptr += 2;
654       break;
655     case DW_FORM_ref4:
656       attr->u.val = read_4_bytes (abfd, info_ptr);
657       info_ptr += 4;
658       break;
659     case DW_FORM_ref8:
660       attr->u.val = read_8_bytes (abfd, info_ptr);
661       info_ptr += 8;
662       break;
663     case DW_FORM_ref_udata:
664       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
665       info_ptr += bytes_read;
666       break;
667     case DW_FORM_indirect:
668       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
669       info_ptr += bytes_read;
670       info_ptr = read_attribute_value (attr, form, unit, info_ptr);
671       break;
672     default:
673       (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
674 			     form);
675       bfd_set_error (bfd_error_bad_value);
676     }
677   return info_ptr;
678 }
679 
680 /* Read an attribute described by an abbreviated attribute.  */
681 
682 static bfd_byte *
read_attribute(struct attribute * attr,struct attr_abbrev * abbrev,struct comp_unit * unit,bfd_byte * info_ptr)683 read_attribute (struct attribute *attr,
684 		struct attr_abbrev *abbrev,
685 		struct comp_unit *unit,
686 		bfd_byte *info_ptr)
687 {
688   attr->name = abbrev->name;
689   info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
690   return info_ptr;
691 }
692 
693 /* Source line information table routines.  */
694 
695 #define FILE_ALLOC_CHUNK 5
696 #define DIR_ALLOC_CHUNK 5
697 
698 struct line_info
699 {
700   struct line_info* prev_line;
701   bfd_vma address;
702   char *filename;
703   unsigned int line;
704   unsigned int column;
705   int end_sequence;		/* End of (sequential) code sequence.  */
706 };
707 
708 struct fileinfo
709 {
710   char *name;
711   unsigned int dir;
712   unsigned int time;
713   unsigned int size;
714 };
715 
716 struct line_info_table
717 {
718   bfd* abfd;
719   unsigned int num_files;
720   unsigned int num_dirs;
721   char *comp_dir;
722   char **dirs;
723   struct fileinfo* files;
724   struct line_info* last_line;  /* largest VMA */
725   struct line_info* lcl_head;   /* local head; used in 'add_line_info' */
726 };
727 
728 /* Remember some information about each function.  If the function is
729    inlined (DW_TAG_inlined_subroutine) it may have two additional
730    attributes, DW_AT_call_file and DW_AT_call_line, which specify the
731    source code location where this function was inlined. */
732 
733 struct funcinfo
734 {
735   struct funcinfo *prev_func;		/* Pointer to previous function in list of all functions */
736   struct funcinfo *caller_func;		/* Pointer to function one scope higher */
737   char *caller_file;			/* Source location file name where caller_func inlines this func */
738   int caller_line;			/* Source location line number where caller_func inlines this func */
739   char *file;				/* Source location file name */
740   int line;				/* Source location line number */
741   int tag;
742   char *name;
743   struct arange arange;
744   asection *sec;			/* Where the symbol is defined */
745 };
746 
747 struct varinfo
748 {
749   /* Pointer to previous variable in list of all variables */
750   struct varinfo *prev_var;
751   /* Source location file name */
752   char *file;
753   /* Source location line number */
754   int line;
755   int tag;
756   char *name;
757   bfd_vma addr;
758   /* Where the symbol is defined */
759   asection *sec;
760   /* Is this a stack variable? */
761   unsigned int stack: 1;
762 };
763 
764 /* Return TRUE if NEW_LINE should sort after LINE.  */
765 
766 static inline bfd_boolean
new_line_sorts_after(struct line_info * new_line,struct line_info * line)767 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
768 {
769   return (new_line->address > line->address
770 	  || (new_line->address == line->address
771 	      && new_line->end_sequence < line->end_sequence));
772 }
773 
774 
775 /* Adds a new entry to the line_info list in the line_info_table, ensuring
776    that the list is sorted.  Note that the line_info list is sorted from
777    highest to lowest VMA (with possible duplicates); that is,
778    line_info->prev_line always accesses an equal or smaller VMA.  */
779 
780 static void
add_line_info(struct line_info_table * table,bfd_vma address,char * filename,unsigned int line,unsigned int column,int end_sequence)781 add_line_info (struct line_info_table *table,
782 	       bfd_vma address,
783 	       char *filename,
784 	       unsigned int line,
785 	       unsigned int column,
786 	       int end_sequence)
787 {
788   bfd_size_type amt = sizeof (struct line_info);
789   struct line_info* info = bfd_alloc (table->abfd, amt);
790 
791   /* Set member data of 'info'.  */
792   info->address = address;
793   info->line = line;
794   info->column = column;
795   info->end_sequence = end_sequence;
796 
797   if (filename && filename[0])
798     {
799       info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
800       if (info->filename)
801 	strcpy (info->filename, filename);
802     }
803   else
804     info->filename = NULL;
805 
806   /* Find the correct location for 'info'.  Normally we will receive
807      new line_info data 1) in order and 2) with increasing VMAs.
808      However some compilers break the rules (cf. decode_line_info) and
809      so we include some heuristics for quickly finding the correct
810      location for 'info'. In particular, these heuristics optimize for
811      the common case in which the VMA sequence that we receive is a
812      list of locally sorted VMAs such as
813        p...z a...j  (where a < j < p < z)
814 
815      Note: table->lcl_head is used to head an *actual* or *possible*
816      sequence within the list (such as a...j) that is not directly
817      headed by table->last_line
818 
819      Note: we may receive duplicate entries from 'decode_line_info'.  */
820 
821   if (!table->last_line
822       || new_line_sorts_after (info, table->last_line))
823     {
824       /* Normal case: add 'info' to the beginning of the list */
825       info->prev_line = table->last_line;
826       table->last_line = info;
827 
828       /* lcl_head: initialize to head a *possible* sequence at the end.  */
829       if (!table->lcl_head)
830 	table->lcl_head = info;
831     }
832   else if (!new_line_sorts_after (info, table->lcl_head)
833 	   && (!table->lcl_head->prev_line
834 	       || new_line_sorts_after (info, table->lcl_head->prev_line)))
835     {
836       /* Abnormal but easy: lcl_head is the head of 'info'.  */
837       info->prev_line = table->lcl_head->prev_line;
838       table->lcl_head->prev_line = info;
839     }
840   else
841     {
842       /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
843 	 heads for 'info'.  Reset 'lcl_head'.  */
844       struct line_info* li2 = table->last_line; /* always non-NULL */
845       struct line_info* li1 = li2->prev_line;
846 
847       while (li1)
848 	{
849 	  if (!new_line_sorts_after (info, li2)
850 	      && new_line_sorts_after (info, li1))
851 	    break;
852 
853 	  li2 = li1; /* always non-NULL */
854 	  li1 = li1->prev_line;
855 	}
856       table->lcl_head = li2;
857       info->prev_line = table->lcl_head->prev_line;
858       table->lcl_head->prev_line = info;
859     }
860 }
861 
862 /* Extract a fully qualified filename from a line info table.
863    The returned string has been malloc'ed and it is the caller's
864    responsibility to free it.  */
865 
866 static char *
concat_filename(struct line_info_table * table,unsigned int file)867 concat_filename (struct line_info_table *table, unsigned int file)
868 {
869   char *filename;
870 
871   if (file - 1 >= table->num_files)
872     {
873       /* FILE == 0 means unknown.  */
874       if (file)
875 	(*_bfd_error_handler)
876 	  (_("Dwarf Error: mangled line number section (bad file number)."));
877       return strdup ("<unknown>");
878     }
879 
880   filename = table->files[file - 1].name;
881 
882   if (!IS_ABSOLUTE_PATH (filename))
883     {
884       char *dirname = NULL;
885       char *subdirname = NULL;
886       char *name;
887       size_t len;
888 
889       if (table->files[file - 1].dir)
890 	subdirname = table->dirs[table->files[file - 1].dir - 1];
891 
892       if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
893 	dirname = table->comp_dir;
894 
895       if (!dirname)
896 	{
897 	  dirname = subdirname;
898 	  subdirname = NULL;
899 	}
900 
901       if (!dirname)
902 	return strdup (filename);
903 
904       len = strlen (dirname) + strlen (filename) + 2;
905 
906       if (subdirname)
907 	{
908 	  len += strlen (subdirname) + 1;
909 	  name = bfd_malloc (len);
910 	  if (name)
911 	    sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
912 	}
913       else
914 	{
915 	  name = bfd_malloc (len);
916 	  if (name)
917 	    sprintf (name, "%s/%s", dirname, filename);
918 	}
919 
920       return name;
921     }
922 
923   return strdup (filename);
924 }
925 
926 static void
arange_add(bfd * abfd,struct arange * first_arange,bfd_vma low_pc,bfd_vma high_pc)927 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
928 {
929   struct arange *arange;
930 
931   /* If the first arange is empty, use it. */
932   if (first_arange->high == 0)
933     {
934       first_arange->low = low_pc;
935       first_arange->high = high_pc;
936       return;
937     }
938 
939   /* Next see if we can cheaply extend an existing range.  */
940   arange = first_arange;
941   do
942     {
943       if (low_pc == arange->high)
944 	{
945 	  arange->high = high_pc;
946 	  return;
947 	}
948       if (high_pc == arange->low)
949 	{
950 	  arange->low = low_pc;
951 	  return;
952 	}
953       arange = arange->next;
954     }
955   while (arange);
956 
957   /* Need to allocate a new arange and insert it into the arange list.
958      Order isn't significant, so just insert after the first arange. */
959   arange = bfd_zalloc (abfd, sizeof (*arange));
960   arange->low = low_pc;
961   arange->high = high_pc;
962   arange->next = first_arange->next;
963   first_arange->next = arange;
964 }
965 
966 /* Decode the line number information for UNIT.  */
967 
968 static struct line_info_table*
decode_line_info(struct comp_unit * unit,struct dwarf2_debug * stash)969 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
970 {
971   bfd *abfd = unit->abfd;
972   struct line_info_table* table;
973   bfd_byte *line_ptr;
974   bfd_byte *line_end;
975   struct line_head lh;
976   unsigned int i, bytes_read, offset_size;
977   char *cur_file, *cur_dir;
978   unsigned char op_code, extended_op, adj_opcode;
979   bfd_size_type amt;
980 
981   if (! stash->dwarf_line_buffer)
982     {
983       asection *msec;
984 
985       msec = bfd_get_section_by_name (abfd, ".debug_line");
986       if (! msec)
987 	{
988 	  (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
989 	  bfd_set_error (bfd_error_bad_value);
990 	  return 0;
991 	}
992 
993       stash->dwarf_line_size = msec->size;
994       stash->dwarf_line_buffer
995 	= bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
996 						     stash->syms);
997       if (! stash->dwarf_line_buffer)
998 	return 0;
999     }
1000 
1001   /* It is possible to get a bad value for the line_offset.  Validate
1002      it here so that we won't get a segfault below.  */
1003   if (unit->line_offset >= stash->dwarf_line_size)
1004     {
1005       (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
1006 			     unit->line_offset, stash->dwarf_line_size);
1007       bfd_set_error (bfd_error_bad_value);
1008       return 0;
1009     }
1010 
1011   amt = sizeof (struct line_info_table);
1012   table = bfd_alloc (abfd, amt);
1013   table->abfd = abfd;
1014   table->comp_dir = unit->comp_dir;
1015 
1016   table->num_files = 0;
1017   table->files = NULL;
1018 
1019   table->num_dirs = 0;
1020   table->dirs = NULL;
1021 
1022   table->files = NULL;
1023   table->last_line = NULL;
1024   table->lcl_head = NULL;
1025 
1026   line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1027 
1028   /* Read in the prologue.  */
1029   lh.total_length = read_4_bytes (abfd, line_ptr);
1030   line_ptr += 4;
1031   offset_size = 4;
1032   if (lh.total_length == 0xffffffff)
1033     {
1034       lh.total_length = read_8_bytes (abfd, line_ptr);
1035       line_ptr += 8;
1036       offset_size = 8;
1037     }
1038   else if (lh.total_length == 0 && unit->addr_size == 8)
1039     {
1040       /* Handle (non-standard) 64-bit DWARF2 formats.  */
1041       lh.total_length = read_4_bytes (abfd, line_ptr);
1042       line_ptr += 4;
1043       offset_size = 8;
1044     }
1045   line_end = line_ptr + lh.total_length;
1046   lh.version = read_2_bytes (abfd, line_ptr);
1047   line_ptr += 2;
1048   if (offset_size == 4)
1049     lh.prologue_length = read_4_bytes (abfd, line_ptr);
1050   else
1051     lh.prologue_length = read_8_bytes (abfd, line_ptr);
1052   line_ptr += offset_size;
1053   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1054   line_ptr += 1;
1055   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1056   line_ptr += 1;
1057   lh.line_base = read_1_signed_byte (abfd, line_ptr);
1058   line_ptr += 1;
1059   lh.line_range = read_1_byte (abfd, line_ptr);
1060   line_ptr += 1;
1061   lh.opcode_base = read_1_byte (abfd, line_ptr);
1062   line_ptr += 1;
1063   amt = lh.opcode_base * sizeof (unsigned char);
1064   lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1065 
1066   lh.standard_opcode_lengths[0] = 1;
1067 
1068   for (i = 1; i < lh.opcode_base; ++i)
1069     {
1070       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1071       line_ptr += 1;
1072     }
1073 
1074   /* Read directory table.  */
1075   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1076     {
1077       line_ptr += bytes_read;
1078 
1079       if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1080 	{
1081 	  char **tmp;
1082 
1083 	  amt = table->num_dirs + DIR_ALLOC_CHUNK;
1084 	  amt *= sizeof (char *);
1085 
1086 	  tmp = bfd_realloc (table->dirs, amt);
1087 	  if (tmp == NULL)
1088 	    {
1089 	      free (table->dirs);
1090 	      return NULL;
1091 	    }
1092 	  table->dirs = tmp;
1093 	}
1094 
1095       table->dirs[table->num_dirs++] = cur_dir;
1096     }
1097 
1098   line_ptr += bytes_read;
1099 
1100   /* Read file name table.  */
1101   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1102     {
1103       line_ptr += bytes_read;
1104 
1105       if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1106 	{
1107 	  struct fileinfo *tmp;
1108 
1109 	  amt = table->num_files + FILE_ALLOC_CHUNK;
1110 	  amt *= sizeof (struct fileinfo);
1111 
1112 	  tmp = bfd_realloc (table->files, amt);
1113 	  if (tmp == NULL)
1114 	    {
1115 	      free (table->files);
1116 	      free (table->dirs);
1117 	      return NULL;
1118 	    }
1119 	  table->files = tmp;
1120 	}
1121 
1122       table->files[table->num_files].name = cur_file;
1123       table->files[table->num_files].dir =
1124 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1125       line_ptr += bytes_read;
1126       table->files[table->num_files].time =
1127 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1128       line_ptr += bytes_read;
1129       table->files[table->num_files].size =
1130 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1131       line_ptr += bytes_read;
1132       table->num_files++;
1133     }
1134 
1135   line_ptr += bytes_read;
1136 
1137   /* Read the statement sequences until there's nothing left.  */
1138   while (line_ptr < line_end)
1139     {
1140       /* State machine registers.  */
1141       bfd_vma address = 0;
1142       char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1143       unsigned int line = 1;
1144       unsigned int column = 0;
1145       int is_stmt = lh.default_is_stmt;
1146       int end_sequence = 0;
1147       /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1148 	 compilers generate address sequences that are wildly out of
1149 	 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1150 	 for ia64-Linux).  Thus, to determine the low and high
1151 	 address, we must compare on every DW_LNS_copy, etc.  */
1152       bfd_vma low_pc  = (bfd_vma) -1;
1153       bfd_vma high_pc = 0;
1154 
1155       /* Decode the table.  */
1156       while (! end_sequence)
1157 	{
1158 	  op_code = read_1_byte (abfd, line_ptr);
1159 	  line_ptr += 1;
1160 
1161 	  if (op_code >= lh.opcode_base)
1162 	    {
1163 	      /* Special operand.  */
1164 	      adj_opcode = op_code - lh.opcode_base;
1165 	      address += (adj_opcode / lh.line_range)
1166 		* lh.minimum_instruction_length;
1167 	      line += lh.line_base + (adj_opcode % lh.line_range);
1168 	      /* Append row to matrix using current values.  */
1169 	      add_line_info (table, address, filename, line, column, 0);
1170 	      if (address < low_pc)
1171 		low_pc = address;
1172 	      if (address > high_pc)
1173 		high_pc = address;
1174 	    }
1175 	  else switch (op_code)
1176 	    {
1177 	    case DW_LNS_extended_op:
1178 	      /* Ignore length.  */
1179 	      line_ptr += 1;
1180 	      extended_op = read_1_byte (abfd, line_ptr);
1181 	      line_ptr += 1;
1182 
1183 	      switch (extended_op)
1184 		{
1185 		case DW_LNE_end_sequence:
1186 		  end_sequence = 1;
1187 		  add_line_info (table, address, filename, line, column,
1188 				 end_sequence);
1189 		  if (address < low_pc)
1190 		    low_pc = address;
1191 		  if (address > high_pc)
1192 		    high_pc = address;
1193 		  arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1194 		  break;
1195 		case DW_LNE_set_address:
1196 		  address = read_address (unit, line_ptr);
1197 		  line_ptr += unit->addr_size;
1198 		  break;
1199 		case DW_LNE_define_file:
1200 		  cur_file = read_string (abfd, line_ptr, &bytes_read);
1201 		  line_ptr += bytes_read;
1202 		  if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1203 		    {
1204 		      struct fileinfo *tmp;
1205 
1206 		      amt = table->num_files + FILE_ALLOC_CHUNK;
1207 		      amt *= sizeof (struct fileinfo);
1208 		      tmp = bfd_realloc (table->files, amt);
1209 		      if (tmp == NULL)
1210 			{
1211 			  free (table->files);
1212 			  free (table->dirs);
1213 			  free (filename);
1214 			  return NULL;
1215 			}
1216 		      table->files = tmp;
1217 		    }
1218 		  table->files[table->num_files].name = cur_file;
1219 		  table->files[table->num_files].dir =
1220 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1221 		  line_ptr += bytes_read;
1222 		  table->files[table->num_files].time =
1223 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1224 		  line_ptr += bytes_read;
1225 		  table->files[table->num_files].size =
1226 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1227 		  line_ptr += bytes_read;
1228 		  table->num_files++;
1229 		  break;
1230 		default:
1231 		  (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1232 		  bfd_set_error (bfd_error_bad_value);
1233 		  free (filename);
1234 		  free (table->files);
1235 		  free (table->dirs);
1236 		  return NULL;
1237 		}
1238 	      break;
1239 	    case DW_LNS_copy:
1240 	      add_line_info (table, address, filename, line, column, 0);
1241 	      if (address < low_pc)
1242 		low_pc = address;
1243 	      if (address > high_pc)
1244 		high_pc = address;
1245 	      break;
1246 	    case DW_LNS_advance_pc:
1247 	      address += lh.minimum_instruction_length
1248 		* read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1249 	      line_ptr += bytes_read;
1250 	      break;
1251 	    case DW_LNS_advance_line:
1252 	      line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1253 	      line_ptr += bytes_read;
1254 	      break;
1255 	    case DW_LNS_set_file:
1256 	      {
1257 		unsigned int file;
1258 
1259 		/* The file and directory tables are 0
1260 		   based, the references are 1 based.  */
1261 		file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1262 		line_ptr += bytes_read;
1263 		if (filename)
1264 		  free (filename);
1265 		filename = concat_filename (table, file);
1266 		break;
1267 	      }
1268 	    case DW_LNS_set_column:
1269 	      column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1270 	      line_ptr += bytes_read;
1271 	      break;
1272 	    case DW_LNS_negate_stmt:
1273 	      is_stmt = (!is_stmt);
1274 	      break;
1275 	    case DW_LNS_set_basic_block:
1276 	      break;
1277 	    case DW_LNS_const_add_pc:
1278 	      address += lh.minimum_instruction_length
1279 		      * ((255 - lh.opcode_base) / lh.line_range);
1280 	      break;
1281 	    case DW_LNS_fixed_advance_pc:
1282 	      address += read_2_bytes (abfd, line_ptr);
1283 	      line_ptr += 2;
1284 	      break;
1285 	    default:
1286 	      {
1287 		int i;
1288 
1289 		/* Unknown standard opcode, ignore it.  */
1290 		for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1291 		  {
1292 		    (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1293 		    line_ptr += bytes_read;
1294 		  }
1295 	      }
1296 	    }
1297 	}
1298 
1299       if (filename)
1300 	free (filename);
1301     }
1302 
1303   return table;
1304 }
1305 
1306 /* If ADDR is within TABLE set the output parameters and return TRUE,
1307    otherwise return FALSE.  The output parameters, FILENAME_PTR and
1308    LINENUMBER_PTR, are pointers to the objects to be filled in.  */
1309 
1310 static bfd_boolean
lookup_address_in_line_info_table(struct line_info_table * table,bfd_vma addr,struct funcinfo * function,const char ** filename_ptr,unsigned int * linenumber_ptr)1311 lookup_address_in_line_info_table (struct line_info_table *table,
1312 				   bfd_vma addr,
1313 				   struct funcinfo *function,
1314 				   const char **filename_ptr,
1315 				   unsigned int *linenumber_ptr)
1316 {
1317   /* Note: table->last_line should be a descendingly sorted list. */
1318   struct line_info* next_line = table->last_line;
1319   struct line_info* each_line = NULL;
1320   *filename_ptr = NULL;
1321 
1322   if (!next_line)
1323     return FALSE;
1324 
1325   each_line = next_line->prev_line;
1326 
1327   /* Check for large addresses */
1328   if (addr > next_line->address)
1329     each_line = NULL; /* ensure we skip over the normal case */
1330 
1331   /* Normal case: search the list; save  */
1332   while (each_line && next_line)
1333     {
1334       /* If we have an address match, save this info.  This allows us
1335 	 to return as good as results as possible for strange debugging
1336 	 info.  */
1337       bfd_boolean addr_match = FALSE;
1338       if (each_line->address <= addr && addr < next_line->address)
1339 	{
1340 	  addr_match = TRUE;
1341 
1342 	  /* If this line appears to span functions, and addr is in the
1343 	     later function, return the first line of that function instead
1344 	     of the last line of the earlier one.  This check is for GCC
1345 	     2.95, which emits the first line number for a function late.  */
1346 
1347 	  if (function != NULL)
1348 	    {
1349 	      bfd_vma lowest_pc;
1350 	      struct arange *arange;
1351 
1352 	      /* Find the lowest address in the function's range list */
1353 	      lowest_pc = function->arange.low;
1354 	      for (arange = &function->arange;
1355 		   arange;
1356 		   arange = arange->next)
1357 		{
1358 		  if (function->arange.low < lowest_pc)
1359 		    lowest_pc = function->arange.low;
1360 		}
1361 	      /* Check for spanning function and set outgoing line info */
1362 	      if (addr >= lowest_pc
1363 		  && each_line->address < lowest_pc
1364 		  && next_line->address > lowest_pc)
1365 		{
1366 		  *filename_ptr = next_line->filename;
1367 		  *linenumber_ptr = next_line->line;
1368 		}
1369 	      else
1370 		{
1371 		  *filename_ptr = each_line->filename;
1372 		  *linenumber_ptr = each_line->line;
1373 		}
1374 	    }
1375 	  else
1376 	    {
1377 	      *filename_ptr = each_line->filename;
1378 	      *linenumber_ptr = each_line->line;
1379 	    }
1380 	}
1381 
1382       if (addr_match && !each_line->end_sequence)
1383 	return TRUE; /* we have definitely found what we want */
1384 
1385       next_line = each_line;
1386       each_line = each_line->prev_line;
1387     }
1388 
1389   /* At this point each_line is NULL but next_line is not.  If we found
1390      a candidate end-of-sequence point in the loop above, we can return
1391      that (compatibility with a bug in the Intel compiler); otherwise,
1392      assuming that we found the containing function for this address in
1393      this compilation unit, return the first line we have a number for
1394      (compatibility with GCC 2.95).  */
1395   if (*filename_ptr == NULL && function != NULL)
1396     {
1397       *filename_ptr = next_line->filename;
1398       *linenumber_ptr = next_line->line;
1399       return TRUE;
1400     }
1401 
1402   return FALSE;
1403 }
1404 
1405 /* Read in the .debug_ranges section for future reference */
1406 
1407 static bfd_boolean
read_debug_ranges(struct comp_unit * unit)1408 read_debug_ranges (struct comp_unit *unit)
1409 {
1410   struct dwarf2_debug *stash = unit->stash;
1411   if (! stash->dwarf_ranges_buffer)
1412     {
1413       bfd *abfd = unit->abfd;
1414       asection *msec;
1415 
1416       msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1417       if (! msec)
1418 	{
1419 	  (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1420 	  bfd_set_error (bfd_error_bad_value);
1421 	  return FALSE;
1422 	}
1423 
1424       stash->dwarf_ranges_size = msec->size;
1425       stash->dwarf_ranges_buffer
1426 	= bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1427 						     stash->syms);
1428       if (! stash->dwarf_ranges_buffer)
1429 	return FALSE;
1430     }
1431   return TRUE;
1432 }
1433 
1434 /* Function table functions.  */
1435 
1436 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1437    Note that we need to find the function that has the smallest
1438    range that contains ADDR, to handle inlined functions without
1439    depending upon them being ordered in TABLE by increasing range. */
1440 
1441 static bfd_boolean
lookup_address_in_function_table(struct comp_unit * unit,bfd_vma addr,struct funcinfo ** function_ptr,const char ** functionname_ptr)1442 lookup_address_in_function_table (struct comp_unit *unit,
1443 				  bfd_vma addr,
1444 				  struct funcinfo **function_ptr,
1445 				  const char **functionname_ptr)
1446 {
1447   struct funcinfo* each_func;
1448   struct funcinfo* best_fit = NULL;
1449   struct arange *arange;
1450 
1451   for (each_func = unit->function_table;
1452        each_func;
1453        each_func = each_func->prev_func)
1454     {
1455       for (arange = &each_func->arange;
1456 	   arange;
1457 	   arange = arange->next)
1458 	{
1459 	  if (addr >= arange->low && addr < arange->high)
1460 	    {
1461 	      if (!best_fit ||
1462 		  ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1463 		best_fit = each_func;
1464 	    }
1465 	}
1466     }
1467 
1468   if (best_fit)
1469     {
1470       *functionname_ptr = best_fit->name;
1471       *function_ptr = best_fit;
1472       return TRUE;
1473     }
1474   else
1475     {
1476       return FALSE;
1477     }
1478 }
1479 
1480 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1481    and LINENUMBER_PTR, and return TRUE.  */
1482 
1483 static bfd_boolean
lookup_symbol_in_function_table(struct comp_unit * unit,asymbol * sym,bfd_vma addr,const char ** filename_ptr,unsigned int * linenumber_ptr)1484 lookup_symbol_in_function_table (struct comp_unit *unit,
1485 				 asymbol *sym,
1486 				 bfd_vma addr,
1487 				 const char **filename_ptr,
1488 				 unsigned int *linenumber_ptr)
1489 {
1490   struct funcinfo* each_func;
1491   struct funcinfo* best_fit = NULL;
1492   struct arange *arange;
1493   const char *name = bfd_asymbol_name (sym);
1494   asection *sec = bfd_get_section (sym);
1495 
1496   for (each_func = unit->function_table;
1497        each_func;
1498        each_func = each_func->prev_func)
1499     {
1500       for (arange = &each_func->arange;
1501 	   arange;
1502 	   arange = arange->next)
1503 	{
1504 	  if ((!each_func->sec || each_func->sec == sec)
1505 	      && addr >= arange->low
1506 	      && addr < arange->high
1507 	      && each_func->name
1508 	      && strcmp (name, each_func->name) == 0
1509 	      && (!best_fit
1510 		  || ((arange->high - arange->low)
1511 		      < (best_fit->arange.high - best_fit->arange.low))))
1512 	    best_fit = each_func;
1513 	}
1514     }
1515 
1516   if (best_fit)
1517     {
1518       best_fit->sec = sec;
1519       *filename_ptr = best_fit->file;
1520       *linenumber_ptr = best_fit->line;
1521       return TRUE;
1522     }
1523   else
1524     return FALSE;
1525 }
1526 
1527 /* Variable table functions.  */
1528 
1529 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1530    LINENUMBER_PTR, and return TRUE.  */
1531 
1532 static bfd_boolean
lookup_symbol_in_variable_table(struct comp_unit * unit,asymbol * sym,bfd_vma addr,const char ** filename_ptr,unsigned int * linenumber_ptr)1533 lookup_symbol_in_variable_table (struct comp_unit *unit,
1534 				 asymbol *sym,
1535 				 bfd_vma addr,
1536 				 const char **filename_ptr,
1537 				 unsigned int *linenumber_ptr)
1538 {
1539   const char *name = bfd_asymbol_name (sym);
1540   asection *sec = bfd_get_section (sym);
1541   struct varinfo* each;
1542 
1543   for (each = unit->variable_table; each; each = each->prev_var)
1544     if (each->stack == 0
1545 	&& each->file != NULL
1546 	&& each->name != NULL
1547 	&& each->addr == addr
1548 	&& (!each->sec || each->sec == sec)
1549 	&& strcmp (name, each->name) == 0)
1550       break;
1551 
1552   if (each)
1553     {
1554       each->sec = sec;
1555       *filename_ptr = each->file;
1556       *linenumber_ptr = each->line;
1557       return TRUE;
1558     }
1559   else
1560     return FALSE;
1561 }
1562 
1563 static char *
find_abstract_instance_name(struct comp_unit * unit,bfd_uint64_t die_ref)1564 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1565 {
1566   bfd *abfd = unit->abfd;
1567   bfd_byte *info_ptr;
1568   unsigned int abbrev_number, bytes_read, i;
1569   struct abbrev_info *abbrev;
1570   struct attribute attr;
1571   char *name = 0;
1572 
1573   info_ptr = unit->info_ptr_unit + die_ref;
1574   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1575   info_ptr += bytes_read;
1576 
1577   if (abbrev_number)
1578     {
1579       abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1580       if (! abbrev)
1581 	{
1582 	  (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1583 				 abbrev_number);
1584 	  bfd_set_error (bfd_error_bad_value);
1585 	}
1586       else
1587 	{
1588 	  for (i = 0; i < abbrev->num_attrs; ++i)
1589 	    {
1590 	      info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1591 	      switch (attr.name)
1592 		{
1593 		case DW_AT_name:
1594 		  /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
1595 		  if (name == NULL)
1596 		    name = attr.u.str;
1597 		  break;
1598 		case DW_AT_specification:
1599 		  name = find_abstract_instance_name (unit, attr.u.val);
1600 		  break;
1601 		case DW_AT_MIPS_linkage_name:
1602 		  name = attr.u.str;
1603 		  break;
1604 		default:
1605 		  break;
1606 		}
1607 	    }
1608 	}
1609     }
1610   return (name);
1611 }
1612 
1613 static void
read_rangelist(struct comp_unit * unit,struct arange * arange,bfd_uint64_t offset)1614 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1615 {
1616   bfd_byte *ranges_ptr;
1617   bfd_vma base_address = unit->base_address;
1618 
1619   if (! unit->stash->dwarf_ranges_buffer)
1620     {
1621       if (! read_debug_ranges (unit))
1622 	return;
1623     }
1624   ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1625 
1626   for (;;)
1627     {
1628       bfd_vma low_pc;
1629       bfd_vma high_pc;
1630 
1631       if (unit->addr_size == 4)
1632 	{
1633 	  low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1634 	  ranges_ptr += 4;
1635 	  high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1636 	  ranges_ptr += 4;
1637 	}
1638       else
1639 	{
1640 	  low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1641 	  ranges_ptr += 8;
1642 	  high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1643 	  ranges_ptr += 8;
1644 	}
1645       if (low_pc == 0 && high_pc == 0)
1646 	break;
1647       if (low_pc == -1UL && high_pc != -1UL)
1648 	base_address = high_pc;
1649       else
1650 	arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1651     }
1652 }
1653 
1654 /* DWARF2 Compilation unit functions.  */
1655 
1656 /* Scan over each die in a comp. unit looking for functions to add
1657    to the function table and variables to the variable table.  */
1658 
1659 static bfd_boolean
scan_unit_for_symbols(struct comp_unit * unit)1660 scan_unit_for_symbols (struct comp_unit *unit)
1661 {
1662   bfd *abfd = unit->abfd;
1663   bfd_byte *info_ptr = unit->first_child_die_ptr;
1664   int nesting_level = 1;
1665   struct funcinfo **nested_funcs;
1666   int nested_funcs_size;
1667 
1668   /* Maintain a stack of in-scope functions and inlined functions, which we
1669      can use to set the caller_func field.  */
1670   nested_funcs_size = 32;
1671   nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1672   if (nested_funcs == NULL)
1673     return FALSE;
1674   nested_funcs[nesting_level] = 0;
1675 
1676   while (nesting_level)
1677     {
1678       unsigned int abbrev_number, bytes_read, i;
1679       struct abbrev_info *abbrev;
1680       struct attribute attr;
1681       struct funcinfo *func;
1682       struct varinfo *var;
1683       bfd_vma low_pc = 0;
1684       bfd_vma high_pc = 0;
1685 
1686       abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1687       info_ptr += bytes_read;
1688 
1689       if (! abbrev_number)
1690 	{
1691 	  nesting_level--;
1692 	  continue;
1693 	}
1694 
1695       abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1696       if (! abbrev)
1697 	{
1698 	  (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1699 			     abbrev_number);
1700 	  bfd_set_error (bfd_error_bad_value);
1701 	  free (nested_funcs);
1702 	  return FALSE;
1703 	}
1704 
1705       var = NULL;
1706       if (abbrev->tag == DW_TAG_subprogram
1707 	  || abbrev->tag == DW_TAG_entry_point
1708 	  || abbrev->tag == DW_TAG_inlined_subroutine)
1709 	{
1710 	  bfd_size_type amt = sizeof (struct funcinfo);
1711 	  func = bfd_zalloc (abfd, amt);
1712 	  func->tag = abbrev->tag;
1713 	  func->prev_func = unit->function_table;
1714 	  unit->function_table = func;
1715 
1716 	  if (func->tag == DW_TAG_inlined_subroutine)
1717 	    for (i = nesting_level - 1; i >= 1; i--)
1718 	      if (nested_funcs[i])
1719 		{
1720 		  func->caller_func = nested_funcs[i];
1721 		  break;
1722 		}
1723 	  nested_funcs[nesting_level] = func;
1724 	}
1725       else
1726 	{
1727 	  func = NULL;
1728 	  if (abbrev->tag == DW_TAG_variable)
1729 	    {
1730 	      bfd_size_type amt = sizeof (struct varinfo);
1731 	      var = bfd_zalloc (abfd, amt);
1732 	      var->tag = abbrev->tag;
1733 	      var->stack = 1;
1734 	      var->prev_var = unit->variable_table;
1735 	      unit->variable_table = var;
1736 	    }
1737 
1738 	  /* No inline function in scope at this nesting level.  */
1739 	  nested_funcs[nesting_level] = 0;
1740 	}
1741 
1742       for (i = 0; i < abbrev->num_attrs; ++i)
1743 	{
1744 	  info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1745 
1746 	  if (func)
1747 	    {
1748 	      switch (attr.name)
1749 		{
1750 		case DW_AT_call_file:
1751 		  func->caller_file = concat_filename (unit->line_table, attr.u.val);
1752 		  break;
1753 
1754 		case DW_AT_call_line:
1755 		  func->caller_line = attr.u.val;
1756 		  break;
1757 
1758 		case DW_AT_abstract_origin:
1759 		  func->name = find_abstract_instance_name (unit, attr.u.val);
1760 		  break;
1761 
1762 		case DW_AT_name:
1763 		  /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
1764 		  if (func->name == NULL)
1765 		    func->name = attr.u.str;
1766 		  break;
1767 
1768 		case DW_AT_MIPS_linkage_name:
1769 		  func->name = attr.u.str;
1770 		  break;
1771 
1772 		case DW_AT_low_pc:
1773 		  low_pc = attr.u.val;
1774 		  break;
1775 
1776 		case DW_AT_high_pc:
1777 		  high_pc = attr.u.val;
1778 		  break;
1779 
1780 		case DW_AT_ranges:
1781 		  read_rangelist (unit, &func->arange, attr.u.val);
1782 		  break;
1783 
1784 		case DW_AT_decl_file:
1785 		  func->file = concat_filename (unit->line_table,
1786 						attr.u.val);
1787 		  break;
1788 
1789 		case DW_AT_decl_line:
1790 		  func->line = attr.u.val;
1791 		  break;
1792 
1793 		default:
1794 		  break;
1795 		}
1796 	    }
1797 	  else if (var)
1798 	    {
1799 	      switch (attr.name)
1800 		{
1801 		case DW_AT_name:
1802 		  var->name = attr.u.str;
1803 		  break;
1804 
1805 		case DW_AT_decl_file:
1806 		  var->file = concat_filename (unit->line_table,
1807 					       attr.u.val);
1808 		  break;
1809 
1810 		case DW_AT_decl_line:
1811 		  var->line = attr.u.val;
1812 		  break;
1813 
1814 		case DW_AT_external:
1815 		  if (attr.u.val != 0)
1816 		    var->stack = 0;
1817 		  break;
1818 
1819 		case DW_AT_location:
1820 		  switch (attr.form)
1821 		    {
1822 		    case DW_FORM_block:
1823 		    case DW_FORM_block1:
1824 		    case DW_FORM_block2:
1825 		    case DW_FORM_block4:
1826 		      if (*attr.u.blk->data == DW_OP_addr)
1827 			{
1828 			  var->stack = 0;
1829 
1830 			  /* Verify that DW_OP_addr is the only opcode in the
1831 			     location, in which case the block size will be 1
1832 			     plus the address size.  */
1833 			  /* ??? For TLS variables, gcc can emit
1834 			     DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1835 			     which we don't handle here yet.  */
1836 			  if (attr.u.blk->size == unit->addr_size + 1U)
1837 			    var->addr = bfd_get (unit->addr_size * 8,
1838 						 unit->abfd,
1839 						 attr.u.blk->data + 1);
1840 			}
1841 		      break;
1842 
1843 		    default:
1844 		      break;
1845 		    }
1846 		  break;
1847 
1848 		default:
1849 		  break;
1850 		}
1851 	    }
1852 	}
1853 
1854       if (func && high_pc != 0)
1855 	{
1856 	  arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1857 	}
1858 
1859       if (abbrev->has_children)
1860 	{
1861 	  nesting_level++;
1862 
1863 	  if (nesting_level >= nested_funcs_size)
1864 	    {
1865 	      struct funcinfo **tmp;
1866 
1867 	      nested_funcs_size *= 2;
1868 	      tmp = bfd_realloc (nested_funcs,
1869 				 (nested_funcs_size
1870 				  * sizeof (struct funcinfo *)));
1871 	      if (tmp == NULL)
1872 		{
1873 		  free (nested_funcs);
1874 		  return FALSE;
1875 		}
1876 	      nested_funcs = tmp;
1877 	    }
1878 	  nested_funcs[nesting_level] = 0;
1879 	}
1880     }
1881 
1882   free (nested_funcs);
1883   return TRUE;
1884 }
1885 
1886 /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
1887    includes the compilation unit header that proceeds the DIE's, but
1888    does not include the length field that precedes each compilation
1889    unit header.  END_PTR points one past the end of this comp unit.
1890    OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1891 
1892    This routine does not read the whole compilation unit; only enough
1893    to get to the line number information for the compilation unit.  */
1894 
1895 static struct comp_unit *
parse_comp_unit(struct dwarf2_debug * stash,bfd_vma unit_length,bfd_byte * info_ptr_unit,unsigned int offset_size)1896 parse_comp_unit (struct dwarf2_debug *stash,
1897 		 bfd_vma unit_length,
1898 		 bfd_byte *info_ptr_unit,
1899 		 unsigned int offset_size)
1900 {
1901   struct comp_unit* unit;
1902   unsigned int version;
1903   bfd_uint64_t abbrev_offset = 0;
1904   unsigned int addr_size;
1905   struct abbrev_info** abbrevs;
1906   unsigned int abbrev_number, bytes_read, i;
1907   struct abbrev_info *abbrev;
1908   struct attribute attr;
1909   bfd_byte *info_ptr = stash->info_ptr;
1910   bfd_byte *end_ptr = info_ptr + unit_length;
1911   bfd_size_type amt;
1912   bfd_vma low_pc = 0;
1913   bfd_vma high_pc = 0;
1914   bfd *abfd = stash->bfd;
1915 
1916   version = read_2_bytes (abfd, info_ptr);
1917   info_ptr += 2;
1918   BFD_ASSERT (offset_size == 4 || offset_size == 8);
1919   if (offset_size == 4)
1920     abbrev_offset = read_4_bytes (abfd, info_ptr);
1921   else
1922     abbrev_offset = read_8_bytes (abfd, info_ptr);
1923   info_ptr += offset_size;
1924   addr_size = read_1_byte (abfd, info_ptr);
1925   info_ptr += 1;
1926 
1927   if (version != 2)
1928     {
1929       (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1930       bfd_set_error (bfd_error_bad_value);
1931       return 0;
1932     }
1933 
1934   if (addr_size > sizeof (bfd_vma))
1935     {
1936       (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1937 			 addr_size,
1938 			 (unsigned int) sizeof (bfd_vma));
1939       bfd_set_error (bfd_error_bad_value);
1940       return 0;
1941     }
1942 
1943   if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1944     {
1945       (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1946       bfd_set_error (bfd_error_bad_value);
1947       return 0;
1948     }
1949 
1950   /* Read the abbrevs for this compilation unit into a table.  */
1951   abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1952   if (! abbrevs)
1953       return 0;
1954 
1955   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1956   info_ptr += bytes_read;
1957   if (! abbrev_number)
1958     {
1959       (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1960 			 abbrev_number);
1961       bfd_set_error (bfd_error_bad_value);
1962       return 0;
1963     }
1964 
1965   abbrev = lookup_abbrev (abbrev_number, abbrevs);
1966   if (! abbrev)
1967     {
1968       (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1969 			 abbrev_number);
1970       bfd_set_error (bfd_error_bad_value);
1971       return 0;
1972     }
1973 
1974   amt = sizeof (struct comp_unit);
1975   unit = bfd_zalloc (abfd, amt);
1976   unit->abfd = abfd;
1977   unit->addr_size = addr_size;
1978   unit->offset_size = offset_size;
1979   unit->abbrevs = abbrevs;
1980   unit->end_ptr = end_ptr;
1981   unit->stash = stash;
1982   unit->info_ptr_unit = info_ptr_unit;
1983 
1984   for (i = 0; i < abbrev->num_attrs; ++i)
1985     {
1986       info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1987 
1988       /* Store the data if it is of an attribute we want to keep in a
1989 	 partial symbol table.  */
1990       switch (attr.name)
1991 	{
1992 	case DW_AT_stmt_list:
1993 	  unit->stmtlist = 1;
1994 	  unit->line_offset = attr.u.val;
1995 	  break;
1996 
1997 	case DW_AT_name:
1998 	  unit->name = attr.u.str;
1999 	  break;
2000 
2001 	case DW_AT_low_pc:
2002 	  low_pc = attr.u.val;
2003 	  /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2004 	     this is the base address to use when reading location
2005 	     lists or range lists. */
2006 	  unit->base_address = low_pc;
2007 	  break;
2008 
2009 	case DW_AT_high_pc:
2010 	  high_pc = attr.u.val;
2011 	  break;
2012 
2013 	case DW_AT_ranges:
2014 	  read_rangelist (unit, &unit->arange, attr.u.val);
2015 	  break;
2016 
2017 	case DW_AT_comp_dir:
2018 	  {
2019 	    char *comp_dir = attr.u.str;
2020 	    if (comp_dir)
2021 	      {
2022 		/* Irix 6.2 native cc prepends <machine>.: to the compilation
2023 		   directory, get rid of it.  */
2024 		char *cp = strchr (comp_dir, ':');
2025 
2026 		if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2027 		  comp_dir = cp + 1;
2028 	      }
2029 	    unit->comp_dir = comp_dir;
2030 	    break;
2031 	  }
2032 
2033 	default:
2034 	  break;
2035 	}
2036     }
2037   if (high_pc != 0)
2038     {
2039       arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2040     }
2041 
2042   unit->first_child_die_ptr = info_ptr;
2043   return unit;
2044 }
2045 
2046 /* Return TRUE if UNIT may contain the address given by ADDR.  When
2047    there are functions written entirely with inline asm statements, the
2048    range info in the compilation unit header may not be correct.  We
2049    need to consult the line info table to see if a compilation unit
2050    really contains the given address.  */
2051 
2052 static bfd_boolean
comp_unit_contains_address(struct comp_unit * unit,bfd_vma addr)2053 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2054 {
2055   struct arange *arange;
2056 
2057   if (unit->error)
2058     return FALSE;
2059 
2060   arange = &unit->arange;
2061   do
2062     {
2063       if (addr >= arange->low && addr < arange->high)
2064 	return TRUE;
2065       arange = arange->next;
2066     }
2067   while (arange);
2068 
2069   return FALSE;
2070 }
2071 
2072 /* If UNIT contains ADDR, set the output parameters to the values for
2073    the line containing ADDR.  The output parameters, FILENAME_PTR,
2074    FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2075    to be filled in.
2076 
2077    Return TRUE if UNIT contains ADDR, and no errors were encountered;
2078    FALSE otherwise.  */
2079 
2080 static bfd_boolean
comp_unit_find_nearest_line(struct comp_unit * unit,bfd_vma addr,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * linenumber_ptr,struct dwarf2_debug * stash)2081 comp_unit_find_nearest_line (struct comp_unit *unit,
2082 			     bfd_vma addr,
2083 			     const char **filename_ptr,
2084 			     const char **functionname_ptr,
2085 			     unsigned int *linenumber_ptr,
2086 			     struct dwarf2_debug *stash)
2087 {
2088   bfd_boolean line_p;
2089   bfd_boolean func_p;
2090   struct funcinfo *function;
2091 
2092   if (unit->error)
2093     return FALSE;
2094 
2095   if (! unit->line_table)
2096     {
2097       if (! unit->stmtlist)
2098 	{
2099 	  unit->error = 1;
2100 	  return FALSE;
2101 	}
2102 
2103       unit->line_table = decode_line_info (unit, stash);
2104 
2105       if (! unit->line_table)
2106 	{
2107 	  unit->error = 1;
2108 	  return FALSE;
2109 	}
2110 
2111       if (unit->first_child_die_ptr < unit->end_ptr
2112 	  && ! scan_unit_for_symbols (unit))
2113 	{
2114 	  unit->error = 1;
2115 	  return FALSE;
2116 	}
2117     }
2118 
2119   function = NULL;
2120   func_p = lookup_address_in_function_table (unit, addr,
2121 					     &function, functionname_ptr);
2122   if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2123     stash->inliner_chain = function;
2124   line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2125 					      function, filename_ptr,
2126 					      linenumber_ptr);
2127   return line_p || func_p;
2128 }
2129 
2130 /* If UNIT contains SYM at ADDR, set the output parameters to the
2131    values for the line containing SYM.  The output parameters,
2132    FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2133    filled in.
2134 
2135    Return TRUE if UNIT contains SYM, and no errors were encountered;
2136    FALSE otherwise.  */
2137 
2138 static bfd_boolean
comp_unit_find_line(struct comp_unit * unit,asymbol * sym,bfd_vma addr,const char ** filename_ptr,unsigned int * linenumber_ptr,struct dwarf2_debug * stash)2139 comp_unit_find_line (struct comp_unit *unit,
2140 		     asymbol *sym,
2141 		     bfd_vma addr,
2142 		     const char **filename_ptr,
2143 		     unsigned int *linenumber_ptr,
2144 		     struct dwarf2_debug *stash)
2145 {
2146   if (unit->error)
2147     return FALSE;
2148 
2149   if (! unit->line_table)
2150     {
2151       if (! unit->stmtlist)
2152 	{
2153 	  unit->error = 1;
2154 	  return FALSE;
2155 	}
2156 
2157       unit->line_table = decode_line_info (unit, stash);
2158 
2159       if (! unit->line_table)
2160 	{
2161 	  unit->error = 1;
2162 	  return FALSE;
2163 	}
2164 
2165       if (unit->first_child_die_ptr < unit->end_ptr
2166 	  && ! scan_unit_for_symbols (unit))
2167 	{
2168 	  unit->error = 1;
2169 	  return FALSE;
2170 	}
2171     }
2172 
2173   if (sym->flags & BSF_FUNCTION)
2174     return lookup_symbol_in_function_table (unit, sym, addr,
2175 					    filename_ptr,
2176 					    linenumber_ptr);
2177   else
2178     return lookup_symbol_in_variable_table (unit, sym, addr,
2179 					    filename_ptr,
2180 					    linenumber_ptr);
2181 }
2182 
2183 /* Locate a section in a BFD containing debugging info.  The search starts
2184    from the section after AFTER_SEC, or from the first section in the BFD if
2185    AFTER_SEC is NULL.  The search works by examining the names of the
2186    sections.  There are two permissiable names.  The first is .debug_info.
2187    This is the standard DWARF2 name.  The second is a prefix .gnu.linkonce.wi.
2188    This is a variation on the .debug_info section which has a checksum
2189    describing the contents appended onto the name.  This allows the linker to
2190    identify and discard duplicate debugging sections for different
2191    compilation units.  */
2192 #define DWARF2_DEBUG_INFO ".debug_info"
2193 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2194 
2195 static asection *
find_debug_info(bfd * abfd,asection * after_sec)2196 find_debug_info (bfd *abfd, asection *after_sec)
2197 {
2198   asection * msec;
2199 
2200   msec = after_sec != NULL ? after_sec->next : abfd->sections;
2201 
2202   while (msec)
2203     {
2204       if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2205 	return msec;
2206 
2207       if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2208 	return msec;
2209 
2210       msec = msec->next;
2211     }
2212 
2213   return NULL;
2214 }
2215 
2216 /* Unset vmas for loadable sections in STASH.  */
2217 
2218 static void
unset_sections(struct dwarf2_debug * stash)2219 unset_sections (struct dwarf2_debug *stash)
2220 {
2221   unsigned int i;
2222   struct loadable_section *p;
2223 
2224   i = stash->loadable_section_count;
2225   p = stash->loadable_sections;
2226   for (; i > 0; i--, p++)
2227     p->section->vma = 0;
2228 }
2229 
2230 /* Set unique vmas for loadable sections in ABFD and save vmas in
2231    STASH for unset_sections.  */
2232 
2233 static bfd_boolean
place_sections(bfd * abfd,struct dwarf2_debug * stash)2234 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2235 {
2236   struct loadable_section *p;
2237   unsigned int i;
2238 
2239   if (stash->loadable_section_count != 0)
2240     {
2241       i = stash->loadable_section_count;
2242       p = stash->loadable_sections;
2243       for (; i > 0; i--, p++)
2244 	p->section->vma = p->adj_vma;
2245     }
2246   else
2247     {
2248       asection *sect;
2249       bfd_vma last_vma = 0;
2250       bfd_size_type amt;
2251       struct loadable_section *p;
2252 
2253       i = 0;
2254       for (sect = abfd->sections; sect != NULL; sect = sect->next)
2255 	{
2256 	  bfd_size_type sz;
2257 
2258 	  if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2259 	    continue;
2260 
2261 	  sz = sect->rawsize ? sect->rawsize : sect->size;
2262 	  if (sz == 0)
2263 	    continue;
2264 
2265 	  i++;
2266 	}
2267 
2268       amt = i * sizeof (struct loadable_section);
2269       p = (struct loadable_section *) bfd_zalloc (abfd, amt);
2270       if (! p)
2271 	return FALSE;
2272 
2273       stash->loadable_sections = p;
2274       stash->loadable_section_count = i;
2275 
2276       for (sect = abfd->sections; sect != NULL; sect = sect->next)
2277 	{
2278 	  bfd_size_type sz;
2279 
2280 	  if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2281 	    continue;
2282 
2283 	  sz = sect->rawsize ? sect->rawsize : sect->size;
2284 	  if (sz == 0)
2285 	    continue;
2286 
2287 	  p->section = sect;
2288 	  if (last_vma != 0)
2289 	    {
2290 	      /* Align the new address to the current section
2291 		 alignment.  */
2292 	      last_vma = ((last_vma
2293 			   + ~((bfd_vma) -1 << sect->alignment_power))
2294 			  & ((bfd_vma) -1 << sect->alignment_power));
2295 	      sect->vma = last_vma;
2296 	    }
2297 	  p->adj_vma = sect->vma;
2298 	  last_vma += sect->vma + sz;
2299 
2300 	  p++;
2301 	}
2302     }
2303 
2304   return TRUE;
2305 }
2306 
2307 /* Find the source code location of SYMBOL.  If SYMBOL is NULL
2308    then find the nearest source code location corresponding to
2309    the address SECTION + OFFSET.
2310    Returns TRUE if the line is found without error and fills in
2311    FILENAME_PTR and LINENUMBER_PTR.  In the case where SYMBOL was
2312    NULL the FUNCTIONNAME_PTR is also filled in.
2313    SYMBOLS contains the symbol table for ABFD.
2314    ADDR_SIZE is the number of bytes in the initial .debug_info length
2315    field and in the abbreviation offset, or zero to indicate that the
2316    default value should be used.  */
2317 
2318 static bfd_boolean
find_line(bfd * abfd,asection * section,bfd_vma offset,asymbol * symbol,asymbol ** symbols,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * linenumber_ptr,unsigned int addr_size,void ** pinfo)2319 find_line (bfd *abfd,
2320 	   asection *section,
2321 	   bfd_vma offset,
2322 	   asymbol *symbol,
2323 	   asymbol **symbols,
2324 	   const char **filename_ptr,
2325 	   const char **functionname_ptr,
2326 	   unsigned int *linenumber_ptr,
2327 	   unsigned int addr_size,
2328 	   void **pinfo)
2329 {
2330   /* Read each compilation unit from the section .debug_info, and check
2331      to see if it contains the address we are searching for.  If yes,
2332      lookup the address, and return the line number info.  If no, go
2333      on to the next compilation unit.
2334 
2335      We keep a list of all the previously read compilation units, and
2336      a pointer to the next un-read compilation unit.  Check the
2337      previously read units before reading more.  */
2338   struct dwarf2_debug *stash;
2339   /* What address are we looking for?  */
2340   bfd_vma addr;
2341   struct comp_unit* each;
2342   bfd_vma found = FALSE;
2343   bfd_boolean do_line;
2344 
2345   stash = *pinfo;
2346 
2347   if (! stash)
2348     {
2349       bfd_size_type amt = sizeof (struct dwarf2_debug);
2350 
2351       stash = bfd_zalloc (abfd, amt);
2352       if (! stash)
2353 	return FALSE;
2354     }
2355 
2356   /* In a relocatable file, 2 functions may have the same address.
2357      We change the section vma so that they won't overlap.  */
2358   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2359     {
2360       if (! place_sections (abfd, stash))
2361 	return FALSE;
2362     }
2363 
2364   do_line = (section == NULL
2365 	     && offset == 0
2366 	     && functionname_ptr == NULL
2367 	     && symbol != NULL);
2368   if (do_line)
2369     {
2370       addr = symbol->value;
2371       section = bfd_get_section (symbol);
2372     }
2373   else if (section != NULL
2374 	   && functionname_ptr != NULL
2375 	   && symbol == NULL)
2376     addr = offset;
2377   else
2378     abort ();
2379 
2380   if (section->output_section)
2381     addr += section->output_section->vma + section->output_offset;
2382   else
2383     addr += section->vma;
2384   *filename_ptr = NULL;
2385   if (!do_line)
2386     *functionname_ptr = NULL;
2387   *linenumber_ptr = 0;
2388 
2389   if (! *pinfo)
2390     {
2391       bfd *debug_bfd;
2392       bfd_size_type total_size;
2393       asection *msec;
2394 
2395       *pinfo = stash;
2396 
2397       msec = find_debug_info (abfd, NULL);
2398       if (msec == NULL)
2399 	{
2400 	  char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
2401 
2402 	  if (debug_filename == NULL)
2403 	    /* No dwarf2 info, and no gnu_debuglink to follow.
2404 	       Note that at this point the stash has been allocated, but
2405 	       contains zeros.  This lets future calls to this function
2406 	       fail more quickly.  */
2407 	    goto done;
2408 
2409 	  if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
2410 	      || ! bfd_check_format (debug_bfd, bfd_object)
2411 	      || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
2412 	    {
2413 	      if (debug_bfd)
2414 		bfd_close (debug_bfd);
2415 	      /* FIXME: Should we report our failure to follow the debuglink ?  */
2416 	      free (debug_filename);
2417 	      goto done;
2418 	    }
2419 	}
2420       else
2421 	debug_bfd = abfd;
2422 
2423       /* There can be more than one DWARF2 info section in a BFD these days.
2424 	 Read them all in and produce one large stash.  We do this in two
2425 	 passes - in the first pass we just accumulate the section sizes.
2426 	 In the second pass we read in the section's contents.  The allows
2427 	 us to avoid reallocing the data as we add sections to the stash.  */
2428       for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
2429 	total_size += msec->size;
2430 
2431       stash->info_ptr = bfd_alloc (debug_bfd, total_size);
2432       if (stash->info_ptr == NULL)
2433 	goto done;
2434 
2435       stash->info_ptr_end = stash->info_ptr;
2436 
2437       for (msec = find_debug_info (debug_bfd, NULL);
2438 	   msec;
2439 	   msec = find_debug_info (debug_bfd, msec))
2440 	{
2441 	  bfd_size_type size;
2442 	  bfd_size_type start;
2443 
2444 	  size = msec->size;
2445 	  if (size == 0)
2446 	    continue;
2447 
2448 	  start = stash->info_ptr_end - stash->info_ptr;
2449 
2450 	  if ((bfd_simple_get_relocated_section_contents
2451 	       (debug_bfd, msec, stash->info_ptr + start, symbols)) == NULL)
2452 	    continue;
2453 
2454 	  stash->info_ptr_end = stash->info_ptr + start + size;
2455 	}
2456 
2457       BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2458 
2459       stash->sec = find_debug_info (debug_bfd, NULL);
2460       stash->sec_info_ptr = stash->info_ptr;
2461       stash->syms = symbols;
2462       stash->bfd = debug_bfd;
2463     }
2464 
2465   /* A null info_ptr indicates that there is no dwarf2 info
2466      (or that an error occured while setting up the stash).  */
2467   if (! stash->info_ptr)
2468     goto done;
2469 
2470   stash->inliner_chain = NULL;
2471 
2472   /* Check the previously read comp. units first.  */
2473   for (each = stash->all_comp_units; each; each = each->next_unit)
2474     {
2475       if (do_line)
2476 	found = (((symbol->flags & BSF_FUNCTION) == 0
2477 		  || comp_unit_contains_address (each, addr))
2478 		 && comp_unit_find_line (each, symbol, addr,
2479 					 filename_ptr, linenumber_ptr,
2480 					 stash));
2481       else
2482 	found = (comp_unit_contains_address (each, addr)
2483 		 && comp_unit_find_nearest_line (each, addr,
2484 						 filename_ptr,
2485 						 functionname_ptr,
2486 						 linenumber_ptr,
2487 						 stash));
2488       if (found)
2489 	goto done;
2490     }
2491 
2492   /* The DWARF2 spec says that the initial length field, and the
2493      offset of the abbreviation table, should both be 4-byte values.
2494      However, some compilers do things differently.  */
2495   if (addr_size == 0)
2496     addr_size = 4;
2497   BFD_ASSERT (addr_size == 4 || addr_size == 8);
2498 
2499   /* Read each remaining comp. units checking each as they are read.  */
2500   while (stash->info_ptr < stash->info_ptr_end)
2501     {
2502       bfd_vma length;
2503       unsigned int offset_size = addr_size;
2504       bfd_byte *info_ptr_unit = stash->info_ptr;
2505 
2506       length = read_4_bytes (stash->bfd, stash->info_ptr);
2507       /* A 0xffffff length is the DWARF3 way of indicating
2508 	 we use 64-bit offsets, instead of 32-bit offsets.  */
2509       if (length == 0xffffffff)
2510 	{
2511 	  offset_size = 8;
2512 	  length = read_8_bytes (stash->bfd, stash->info_ptr + 4);
2513 	  stash->info_ptr += 12;
2514 	}
2515       /* A zero length is the IRIX way of indicating 64-bit offsets,
2516 	 mostly because the 64-bit length will generally fit in 32
2517 	 bits, and the endianness helps.  */
2518       else if (length == 0)
2519 	{
2520 	  offset_size = 8;
2521 	  length = read_4_bytes (stash->bfd, stash->info_ptr + 4);
2522 	  stash->info_ptr += 8;
2523 	}
2524       /* In the absence of the hints above, we assume 32-bit DWARF2
2525 	 offsets even for targets with 64-bit addresses, because:
2526 	   a) most of the time these targets will not have generated
2527 	      more than 2Gb of debug info and so will not need 64-bit
2528 	      offsets,
2529 	 and
2530 	   b) if they do use 64-bit offsets but they are not using
2531 	      the size hints that are tested for above then they are
2532 	      not conforming to the DWARF3 standard anyway.  */
2533       else if (addr_size == 8)
2534 	{
2535 	  offset_size = 4;
2536           stash->info_ptr += 4;
2537 	}
2538       else
2539 	stash->info_ptr += 4;
2540 
2541       if (length > 0)
2542 	{
2543 	  each = parse_comp_unit (stash, length, info_ptr_unit,
2544 				  offset_size);
2545 	  stash->info_ptr += length;
2546 
2547 	  if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2548 	      == stash->sec->size)
2549 	    {
2550 	      stash->sec = find_debug_info (stash->bfd, stash->sec);
2551 	      stash->sec_info_ptr = stash->info_ptr;
2552 	    }
2553 
2554 	  if (each)
2555 	    {
2556 	      each->next_unit = stash->all_comp_units;
2557 	      stash->all_comp_units = each;
2558 
2559 	      /* DW_AT_low_pc and DW_AT_high_pc are optional for
2560 		 compilation units.  If we don't have them (i.e.,
2561 		 unit->high == 0), we need to consult the line info
2562 		 table to see if a compilation unit contains the given
2563 		 address.  */
2564 	      if (do_line)
2565 		found = (((symbol->flags & BSF_FUNCTION) == 0
2566 			  || each->arange.high == 0
2567 			  || comp_unit_contains_address (each, addr))
2568 			 && comp_unit_find_line (each, symbol, addr,
2569 						 filename_ptr,
2570 						 linenumber_ptr,
2571 						 stash));
2572 	      else
2573 		found = ((each->arange.high == 0
2574 			  || comp_unit_contains_address (each, addr))
2575 			 && comp_unit_find_nearest_line (each, addr,
2576 							 filename_ptr,
2577 							 functionname_ptr,
2578 							 linenumber_ptr,
2579 							 stash));
2580 	      if (found)
2581 		goto done;
2582 	    }
2583 	}
2584     }
2585 
2586 done:
2587   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2588     unset_sections (stash);
2589 
2590   return found;
2591 }
2592 
2593 /* The DWARF2 version of find_nearest_line.
2594    Return TRUE if the line is found without error.  */
2595 
2596 bfd_boolean
_bfd_dwarf2_find_nearest_line(bfd * abfd,asection * section,asymbol ** symbols,bfd_vma offset,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * linenumber_ptr,unsigned int addr_size,void ** pinfo)2597 _bfd_dwarf2_find_nearest_line (bfd *abfd,
2598 			       asection *section,
2599 			       asymbol **symbols,
2600 			       bfd_vma offset,
2601 			       const char **filename_ptr,
2602 			       const char **functionname_ptr,
2603 			       unsigned int *linenumber_ptr,
2604 			       unsigned int addr_size,
2605 			       void **pinfo)
2606 {
2607   return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
2608 		    functionname_ptr, linenumber_ptr, addr_size,
2609 		    pinfo);
2610 }
2611 
2612 /* The DWARF2 version of find_line.
2613    Return TRUE if the line is found without error.  */
2614 
2615 bfd_boolean
_bfd_dwarf2_find_line(bfd * abfd,asymbol ** symbols,asymbol * symbol,const char ** filename_ptr,unsigned int * linenumber_ptr,unsigned int addr_size,void ** pinfo)2616 _bfd_dwarf2_find_line (bfd *abfd,
2617 		       asymbol **symbols,
2618 		       asymbol *symbol,
2619 		       const char **filename_ptr,
2620 		       unsigned int *linenumber_ptr,
2621 		       unsigned int addr_size,
2622 		       void **pinfo)
2623 {
2624   return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
2625 		    NULL, linenumber_ptr, addr_size,
2626 		    pinfo);
2627 }
2628 
2629 bfd_boolean
_bfd_dwarf2_find_inliner_info(bfd * abfd ATTRIBUTE_UNUSED,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * linenumber_ptr,void ** pinfo)2630 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
2631 			       const char **filename_ptr,
2632 			       const char **functionname_ptr,
2633 			       unsigned int *linenumber_ptr,
2634 			       void **pinfo)
2635 {
2636   struct dwarf2_debug *stash;
2637 
2638   stash = *pinfo;
2639   if (stash)
2640     {
2641       struct funcinfo *func = stash->inliner_chain;
2642 
2643       if (func && func->caller_func)
2644 	{
2645 	  *filename_ptr = func->caller_file;
2646 	  *functionname_ptr = func->caller_func->name;
2647 	  *linenumber_ptr = func->caller_line;
2648 	  stash->inliner_chain = func->caller_func;
2649 	  return TRUE;
2650 	}
2651     }
2652 
2653   return FALSE;
2654 }
2655 
2656 void
_bfd_dwarf2_cleanup_debug_info(bfd * abfd)2657 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
2658 {
2659   struct comp_unit *each;
2660   struct dwarf2_debug *stash;
2661 
2662   if (abfd == NULL || elf_tdata (abfd) == NULL)
2663     return;
2664 
2665   stash = elf_tdata (abfd)->dwarf2_find_line_info;
2666 
2667   if (stash == NULL)
2668     return;
2669 
2670   for (each = stash->all_comp_units; each; each = each->next_unit)
2671     {
2672       struct abbrev_info **abbrevs = each->abbrevs;
2673       size_t i;
2674 
2675       for (i = 0; i < ABBREV_HASH_SIZE; i++)
2676 	{
2677 	  struct abbrev_info *abbrev = abbrevs[i];
2678 
2679 	  while (abbrev)
2680 	    {
2681 	      free (abbrev->attrs);
2682 	      abbrev = abbrev->next;
2683 	    }
2684 	}
2685 
2686       if (each->line_table)
2687 	{
2688 	  free (each->line_table->dirs);
2689 	  free (each->line_table->files);
2690 	}
2691     }
2692 
2693   free (stash->dwarf_abbrev_buffer);
2694   free (stash->dwarf_line_buffer);
2695   free (stash->dwarf_ranges_buffer);
2696 }
2697