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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [bfd/] [dwarf2.c] - Blame information for rev 161

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

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

powered by: WebSVN 2.1.0

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