OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [binutils-2.20.1/] [bfd/] [dwarf2.c] - Blame information for rev 862

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.