Coverage Report

Created: 2024-05-21 06:29

/src/binutils-gdb/bfd/dwarf2.c
Line
Count
Source (jump to first uncovered line)
1
/* DWARF 2 support.
2
   Copyright (C) 1994-2024 Free Software Foundation, Inc.
3
4
   Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
5
   (gavin@cygnus.com).
6
7
   From the dwarf2read.c header:
8
   Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9
   Inc.  with support from Florida State University (under contract
10
   with the Ada Joint Program Office), and Silicon Graphics, Inc.
11
   Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12
   based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13
   support in dwarfread.c
14
15
   This file is part of BFD.
16
17
   This program is free software; you can redistribute it and/or modify
18
   it under the terms of the GNU General Public License as published by
19
   the Free Software Foundation; either version 3 of the License, or (at
20
   your option) any later version.
21
22
   This program is distributed in the hope that it will be useful, but
23
   WITHOUT ANY WARRANTY; without even the implied warranty of
24
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25
   General Public License for more details.
26
27
   You should have received a copy of the GNU General Public License
28
   along with this program; if not, write to the Free Software
29
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30
   MA 02110-1301, USA.  */
31
32
#include "sysdep.h"
33
#include "bfd.h"
34
#include "libiberty.h"
35
#include "demangle.h"
36
#include "libbfd.h"
37
#include "elf-bfd.h"
38
#include "dwarf2.h"
39
#include "hashtab.h"
40
#include "splay-tree.h"
41
42
/* The data in the .debug_line statement prologue looks like this.  */
43
44
struct line_head
45
{
46
  bfd_vma total_length;
47
  unsigned short version;
48
  bfd_vma prologue_length;
49
  unsigned char minimum_instruction_length;
50
  unsigned char maximum_ops_per_insn;
51
  unsigned char default_is_stmt;
52
  int line_base;
53
  unsigned char line_range;
54
  unsigned char opcode_base;
55
  unsigned char *standard_opcode_lengths;
56
};
57
58
/* Attributes have a name and a value.  */
59
60
struct attribute
61
{
62
  enum dwarf_attribute name;
63
  enum dwarf_form form;
64
  union
65
  {
66
    char *str;
67
    struct dwarf_block *blk;
68
    uint64_t val;
69
    int64_t sval;
70
  }
71
  u;
72
};
73
74
/* Blocks are a bunch of untyped bytes.  */
75
struct dwarf_block
76
{
77
  unsigned int size;
78
  bfd_byte *data;
79
};
80
81
struct adjusted_section
82
{
83
  asection *section;
84
  bfd_vma adj_vma;
85
  bfd_vma orig_vma;
86
};
87
88
/* A trie to map quickly from address range to compilation unit.
89
90
   This is a fairly standard radix-256 trie, used to quickly locate which
91
   compilation unit any given address belongs to.  Given that each compilation
92
   unit may register hundreds of very small and unaligned ranges (which may
93
   potentially overlap, due to inlining and other concerns), and a large
94
   program may end up containing hundreds of thousands of such ranges, we cannot
95
   scan through them linearly without undue slowdown.
96
97
   We use a hybrid trie to avoid memory explosion: There are two types of trie
98
   nodes, leaves and interior nodes.  (Almost all nodes are leaves, so they
99
   take up the bulk of the memory usage.) Leaves contain a simple array of
100
   ranges (high/low address) and which compilation unit contains those ranges,
101
   and when we get to a leaf, we scan through it linearly.  Interior nodes
102
   contain pointers to 256 other nodes, keyed by the next byte of the address.
103
   So for a 64-bit address like 0x1234567abcd, we would start at the root and go
104
   down child[0x00]->child[0x00]->child[0x01]->child[0x23]->child[0x45] etc.,
105
   until we hit a leaf.  (Nodes are, in general, leaves until they exceed the
106
   default allocation of 16 elements, at which point they are converted to
107
   interior node if possible.) This gives us near-constant lookup times;
108
   the only thing that can be costly is if there are lots of overlapping ranges
109
   within a single 256-byte segment of the binary, in which case we have to
110
   scan through them all to find the best match.
111
112
   For a binary with few ranges, we will in practice only have a single leaf
113
   node at the root, containing a simple array.  Thus, the scheme is efficient
114
   for both small and large binaries.
115
 */
116
117
/* Experiments have shown 16 to be a memory-efficient default leaf size.
118
   The only case where a leaf will hold more memory than this, is at the
119
   bottomost level (covering 256 bytes in the binary), where we'll expand
120
   the leaf to be able to hold more ranges if needed.
121
 */
122
111k
#define TRIE_LEAF_SIZE 16
123
124
/* All trie_node pointers will really be trie_leaf or trie_interior,
125
   but they have this common head.  */
126
struct trie_node
127
{
128
  /* If zero, we are an interior node.
129
     Otherwise, how many ranges we have room for in this leaf.  */
130
  unsigned int num_room_in_leaf;
131
};
132
133
struct trie_leaf
134
{
135
  struct trie_node head;
136
  unsigned int num_stored_in_leaf;
137
  struct {
138
    struct comp_unit *unit;
139
    bfd_vma low_pc, high_pc;
140
  } ranges[];
141
};
142
143
struct trie_interior
144
{
145
  struct trie_node head;
146
  struct trie_node *children[256];
147
};
148
149
static struct trie_node *alloc_trie_leaf (bfd *abfd)
150
55.8k
{
151
55.8k
  struct trie_leaf *leaf;
152
55.8k
  size_t amt = sizeof (*leaf) + TRIE_LEAF_SIZE * sizeof (leaf->ranges[0]);
153
55.8k
  leaf = bfd_zalloc (abfd, amt);
154
55.8k
  if (leaf == NULL)
155
0
    return NULL;
156
55.8k
  leaf->head.num_room_in_leaf = TRIE_LEAF_SIZE;
157
55.8k
  return &leaf->head;
158
55.8k
}
159
160
struct addr_range
161
{
162
  bfd_byte *start;
163
  bfd_byte *end;
164
};
165
166
/* Return true if address range do intersect.  */
167
168
static bool
169
addr_range_intersects (struct addr_range *r1, struct addr_range *r2)
170
14.4k
{
171
14.4k
  return (r1->start <= r2->start && r2->start < r1->end)
172
14.4k
    || (r1->start <= (r2->end - 1) && (r2->end - 1) < r1->end);
173
14.4k
}
174
175
/* Compare function for splay tree of addr_ranges.  */
176
177
static int
178
splay_tree_compare_addr_range (splay_tree_key xa, splay_tree_key xb)
179
7.24k
{
180
7.24k
  struct addr_range *r1 = (struct addr_range *) xa;
181
7.24k
  struct addr_range *r2 = (struct addr_range *) xb;
182
183
7.24k
  if (addr_range_intersects (r1, r2) || addr_range_intersects (r2, r1))
184
0
    return 0;
185
7.24k
  else if (r1->end <= r2->start)
186
3.62k
    return -1;
187
3.62k
  else
188
3.62k
    return 1;
189
7.24k
}
190
191
/* Splay tree release function for keys (addr_range).  */
192
193
static void
194
splay_tree_free_addr_range (splay_tree_key key)
195
2.29k
{
196
2.29k
  free ((struct addr_range *)key);
197
2.29k
}
198
199
struct dwarf2_debug_file
200
{
201
  /* The actual bfd from which debug info was loaded.  Might be
202
     different to orig_bfd because of gnu_debuglink sections.  */
203
  bfd *bfd_ptr;
204
205
  /* Pointer to the symbol table.  */
206
  asymbol **syms;
207
208
  /* The current info pointer for the .debug_info section being parsed.  */
209
  bfd_byte *info_ptr;
210
211
  /* A pointer to the memory block allocated for .debug_info sections.  */
212
  bfd_byte *dwarf_info_buffer;
213
214
  /* Length of the loaded .debug_info sections.  */
215
  bfd_size_type dwarf_info_size;
216
217
  /* Pointer to the .debug_abbrev section loaded into memory.  */
218
  bfd_byte *dwarf_abbrev_buffer;
219
220
  /* Length of the loaded .debug_abbrev section.  */
221
  bfd_size_type dwarf_abbrev_size;
222
223
  /* Buffer for decode_line_info.  */
224
  bfd_byte *dwarf_line_buffer;
225
226
  /* Length of the loaded .debug_line section.  */
227
  bfd_size_type dwarf_line_size;
228
229
  /* Pointer to the .debug_str section loaded into memory.  */
230
  bfd_byte *dwarf_str_buffer;
231
232
  /* Length of the loaded .debug_str section.  */
233
  bfd_size_type dwarf_str_size;
234
235
  /* Pointer to the .debug_str_offsets section loaded into memory.  */
236
  bfd_byte *dwarf_str_offsets_buffer;
237
238
  /* Length of the loaded .debug_str_offsets section.  */
239
  bfd_size_type dwarf_str_offsets_size;
240
241
  /* Pointer to the .debug_addr section loaded into memory.  */
242
  bfd_byte *dwarf_addr_buffer;
243
244
  /* Length of the loaded .debug_addr section.  */
245
  bfd_size_type dwarf_addr_size;
246
247
  /* Pointer to the .debug_line_str section loaded into memory.  */
248
  bfd_byte *dwarf_line_str_buffer;
249
250
  /* Length of the loaded .debug_line_str section.  */
251
  bfd_size_type dwarf_line_str_size;
252
253
  /* Pointer to the .debug_ranges section loaded into memory.  */
254
  bfd_byte *dwarf_ranges_buffer;
255
256
  /* Length of the loaded .debug_ranges section.  */
257
  bfd_size_type dwarf_ranges_size;
258
259
  /* Pointer to the .debug_rnglists section loaded into memory.  */
260
  bfd_byte *dwarf_rnglists_buffer;
261
262
  /* Length of the loaded .debug_rnglists section.  */
263
  bfd_size_type dwarf_rnglists_size;
264
265
  /* A list of all previously read comp_units.  */
266
  struct comp_unit *all_comp_units;
267
268
  /* A list of all previously read comp_units with no ranges (yet).  */
269
  struct comp_unit *all_comp_units_without_ranges;
270
271
  /* Last comp unit in list above.  */
272
  struct comp_unit *last_comp_unit;
273
274
  /* Line table at line_offset zero.  */
275
  struct line_info_table *line_table;
276
277
  /* Hash table to map offsets to decoded abbrevs.  */
278
  htab_t abbrev_offsets;
279
280
  /* Root of a trie to map addresses to compilation units.  */
281
  struct trie_node *trie_root;
282
283
  /* Splay tree to map info_ptr address to compilation units.  */
284
  splay_tree comp_unit_tree;
285
};
286
287
struct dwarf2_debug
288
{
289
  /* Names of the debug sections.  */
290
  const struct dwarf_debug_section *debug_sections;
291
292
  /* Per-file stuff.  */
293
  struct dwarf2_debug_file f, alt;
294
295
  /* Pointer to the original bfd for which debug was loaded.  This is what
296
     we use to compare and so check that the cached debug data is still
297
     valid - it saves having to possibly dereference the gnu_debuglink each
298
     time.  */
299
  bfd *orig_bfd;
300
301
  /* If the most recent call to bfd_find_nearest_line was given an
302
     address in an inlined function, preserve a pointer into the
303
     calling chain for subsequent calls to bfd_find_inliner_info to
304
     use.  */
305
  struct funcinfo *inliner_chain;
306
307
  /* Section VMAs at the time the stash was built.  */
308
  bfd_vma *sec_vma;
309
  /* Number of sections in the SEC_VMA table.  */
310
  unsigned int sec_vma_count;
311
312
  /* Number of sections whose VMA we must adjust.  */
313
  int adjusted_section_count;
314
315
  /* Array of sections with adjusted VMA.  */
316
  struct adjusted_section *adjusted_sections;
317
318
  /* Number of times find_line is called.  This is used in
319
     the heuristic for enabling the info hash tables.  */
320
  int info_hash_count;
321
322
15.8k
#define STASH_INFO_HASH_TRIGGER    100
323
324
  /* Hash table mapping symbol names to function infos.  */
325
  struct info_hash_table *funcinfo_hash_table;
326
327
  /* Hash table mapping symbol names to variable infos.  */
328
  struct info_hash_table *varinfo_hash_table;
329
330
  /* Head of comp_unit list in the last hash table update.  */
331
  struct comp_unit *hash_units_head;
332
333
  /* Status of info hash.  */
334
  int info_hash_status;
335
44.9k
#define STASH_INFO_HASH_OFF    0
336
89.8k
#define STASH_INFO_HASH_ON     1
337
34
#define STASH_INFO_HASH_DISABLED   2
338
339
  /* True if we opened bfd_ptr.  */
340
  bool close_on_cleanup;
341
};
342
343
struct arange
344
{
345
  struct arange *next;
346
  bfd_vma low;
347
  bfd_vma high;
348
};
349
350
/* A minimal decoding of DWARF2 compilation units.  We only decode
351
   what's needed to get to the line number information.  */
352
353
struct comp_unit
354
{
355
  /* Chain the previously read compilation units.  */
356
  struct comp_unit *next_unit;
357
358
  /* Chain the previously read compilation units that have no ranges yet.
359
     We scan these separately when we have a trie over the ranges.
360
     Unused if arange.high != 0. */
361
  struct comp_unit *next_unit_without_ranges;
362
363
  /* Likewise, chain the compilation unit read after this one.
364
     The comp units are stored in reversed reading order.  */
365
  struct comp_unit *prev_unit;
366
367
  /* Keep the bfd convenient (for memory allocation).  */
368
  bfd *abfd;
369
370
  /* The lowest and highest addresses contained in this compilation
371
     unit as specified in the compilation unit header.  */
372
  struct arange arange;
373
374
  /* The DW_AT_name attribute (for error messages).  */
375
  char *name;
376
377
  /* The abbrev hash table.  */
378
  struct abbrev_info **abbrevs;
379
380
  /* DW_AT_language.  */
381
  int lang;
382
383
  /* Note that an error was found by comp_unit_find_nearest_line.  */
384
  int error;
385
386
  /* The DW_AT_comp_dir attribute.  */
387
  char *comp_dir;
388
389
  /* TRUE if there is a line number table associated with this comp. unit.  */
390
  int stmtlist;
391
392
  /* Pointer to the current comp_unit so that we can find a given entry
393
     by its reference.  */
394
  bfd_byte *info_ptr_unit;
395
396
  /* The offset into .debug_line of the line number table.  */
397
  unsigned long line_offset;
398
399
  /* Pointer to the first child die for the comp unit.  */
400
  bfd_byte *first_child_die_ptr;
401
402
  /* The end of the comp unit.  */
403
  bfd_byte *end_ptr;
404
405
  /* The decoded line number, NULL if not yet decoded.  */
406
  struct line_info_table *line_table;
407
408
  /* A list of the functions found in this comp. unit.  */
409
  struct funcinfo *function_table;
410
411
  /* A table of function information references searchable by address.  */
412
  struct lookup_funcinfo *lookup_funcinfo_table;
413
414
  /* Number of functions in the function_table and sorted_function_table.  */
415
  bfd_size_type number_of_functions;
416
417
  /* A list of the variables found in this comp. unit.  */
418
  struct varinfo *variable_table;
419
420
  /* Pointers to dwarf2_debug structures.  */
421
  struct dwarf2_debug *stash;
422
  struct dwarf2_debug_file *file;
423
424
  /* DWARF format version for this unit - from unit header.  */
425
  int version;
426
427
  /* Address size for this unit - from unit header.  */
428
  unsigned char addr_size;
429
430
  /* Offset size for this unit - from unit header.  */
431
  unsigned char offset_size;
432
433
  /* Base address for this unit - from DW_AT_low_pc attribute of
434
     DW_TAG_compile_unit DIE */
435
  bfd_vma base_address;
436
437
  /* TRUE if symbols are cached in hash table for faster lookup by name.  */
438
  bool cached;
439
440
  /* Used when iterating over trie leaves to know which units we have
441
     already seen in this iteration.  */
442
  bool mark;
443
444
 /* Base address of debug_addr section.  */
445
  size_t dwarf_addr_offset;
446
447
  /* Base address of string offset table.  */
448
  size_t dwarf_str_offset;
449
};
450
451
/* This data structure holds the information of an abbrev.  */
452
struct abbrev_info
453
{
454
  unsigned int         number;    /* Number identifying abbrev.  */
455
  enum dwarf_tag       tag;   /* DWARF tag.  */
456
  bool                 has_children;  /* TRUE if the abbrev has children.  */
457
  unsigned int         num_attrs; /* Number of attributes.  */
458
  struct attr_abbrev * attrs;   /* An array of attribute descriptions.  */
459
  struct abbrev_info * next;    /* Next in chain.  */
460
};
461
462
struct attr_abbrev
463
{
464
  enum dwarf_attribute name;
465
  enum dwarf_form form;
466
  bfd_vma implicit_const;
467
};
468
469
/* Map of uncompressed DWARF debug section name to compressed one.  It
470
   is terminated by NULL uncompressed_name.  */
471
472
const struct dwarf_debug_section dwarf_debug_sections[] =
473
{
474
  { ".debug_abbrev",    ".zdebug_abbrev" },
475
  { ".debug_aranges",   ".zdebug_aranges" },
476
  { ".debug_frame",   ".zdebug_frame" },
477
  { ".debug_info",    ".zdebug_info" },
478
  { ".debug_info",    ".zdebug_info" },
479
  { ".debug_line",    ".zdebug_line" },
480
  { ".debug_loc",   ".zdebug_loc" },
481
  { ".debug_macinfo",   ".zdebug_macinfo" },
482
  { ".debug_macro",   ".zdebug_macro" },
483
  { ".debug_pubnames",    ".zdebug_pubnames" },
484
  { ".debug_pubtypes",    ".zdebug_pubtypes" },
485
  { ".debug_ranges",    ".zdebug_ranges" },
486
  { ".debug_rnglists",    ".zdebug_rnglist" },
487
  { ".debug_static_func", ".zdebug_static_func" },
488
  { ".debug_static_vars", ".zdebug_static_vars" },
489
  { ".debug_str",   ".zdebug_str", },
490
  { ".debug_str",   ".zdebug_str", },
491
  { ".debug_str_offsets", ".zdebug_str_offsets", },
492
  { ".debug_addr",    ".zdebug_addr", },
493
  { ".debug_line_str",    ".zdebug_line_str", },
494
  { ".debug_types",   ".zdebug_types" },
495
  /* GNU DWARF 1 extensions */
496
  { ".debug_sfnames",   ".zdebug_sfnames" },
497
  { ".debug_srcinfo",   ".zebug_srcinfo" },
498
  /* SGI/MIPS DWARF 2 extensions */
499
  { ".debug_funcnames",   ".zdebug_funcnames" },
500
  { ".debug_typenames",   ".zdebug_typenames" },
501
  { ".debug_varnames",    ".zdebug_varnames" },
502
  { ".debug_weaknames",   ".zdebug_weaknames" },
503
  { NULL,     NULL },
504
};
505
506
/* NB/ Numbers in this enum must match up with indices
507
   into the dwarf_debug_sections[] array above.  */
508
enum dwarf_debug_section_enum
509
{
510
  debug_abbrev = 0,
511
  debug_aranges,
512
  debug_frame,
513
  debug_info,
514
  debug_info_alt,
515
  debug_line,
516
  debug_loc,
517
  debug_macinfo,
518
  debug_macro,
519
  debug_pubnames,
520
  debug_pubtypes,
521
  debug_ranges,
522
  debug_rnglists,
523
  debug_static_func,
524
  debug_static_vars,
525
  debug_str,
526
  debug_str_alt,
527
  debug_str_offsets,
528
  debug_addr,
529
  debug_line_str,
530
  debug_types,
531
  debug_sfnames,
532
  debug_srcinfo,
533
  debug_funcnames,
534
  debug_typenames,
535
  debug_varnames,
536
  debug_weaknames,
537
  debug_max
538
};
539
540
/* A static assertion.  */
541
extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
542
              == debug_max + 1 ? 1 : -1];
543
544
#ifndef ABBREV_HASH_SIZE
545
699k
#define ABBREV_HASH_SIZE 121
546
#endif
547
#ifndef ATTR_ALLOC_CHUNK
548
236k
#define ATTR_ALLOC_CHUNK 4
549
#endif
550
551
/* Variable and function hash tables.  This is used to speed up look-up
552
   in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
553
   In order to share code between variable and function infos, we use
554
   a list of untyped pointer for all variable/function info associated with
555
   a symbol.  We waste a bit of memory for list with one node but that
556
   simplifies the code.  */
557
558
struct info_list_node
559
{
560
  struct info_list_node *next;
561
  void *info;
562
};
563
564
/* Info hash entry.  */
565
struct info_hash_entry
566
{
567
  struct bfd_hash_entry root;
568
  struct info_list_node *head;
569
};
570
571
struct info_hash_table
572
{
573
  struct bfd_hash_table base;
574
};
575
576
/* Function to create a new entry in info hash table.  */
577
578
static struct bfd_hash_entry *
579
info_hash_table_newfunc (struct bfd_hash_entry *entry,
580
       struct bfd_hash_table *table,
581
       const char *string)
582
7.92k
{
583
7.92k
  struct info_hash_entry *ret = (struct info_hash_entry *) entry;
584
585
  /* Allocate the structure if it has not already been allocated by a
586
     derived class.  */
587
7.92k
  if (ret == NULL)
588
7.92k
    {
589
7.92k
      ret = (struct info_hash_entry *) bfd_hash_allocate (table,
590
7.92k
                sizeof (* ret));
591
7.92k
      if (ret == NULL)
592
0
  return NULL;
593
7.92k
    }
594
595
  /* Call the allocation method of the base class.  */
596
7.92k
  ret = ((struct info_hash_entry *)
597
7.92k
   bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
598
599
  /* Initialize the local fields here.  */
600
7.92k
  if (ret)
601
7.92k
    ret->head = NULL;
602
603
7.92k
  return (struct bfd_hash_entry *) ret;
604
7.92k
}
605
606
/* Function to create a new info hash table.  It returns a pointer to the
607
   newly created table or NULL if there is any error.  We need abfd
608
   solely for memory allocation.  */
609
610
static struct info_hash_table *
611
create_info_hash_table (bfd *abfd)
612
194
{
613
194
  struct info_hash_table *hash_table;
614
615
194
  hash_table = ((struct info_hash_table *)
616
194
    bfd_alloc (abfd, sizeof (struct info_hash_table)));
617
194
  if (!hash_table)
618
0
    return hash_table;
619
620
194
  if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
621
194
          sizeof (struct info_hash_entry)))
622
0
    {
623
0
      bfd_release (abfd, hash_table);
624
0
      return NULL;
625
0
    }
626
627
194
  return hash_table;
628
194
}
629
630
/* Insert an info entry into an info hash table.  We do not check of
631
   duplicate entries.  Also, the caller need to guarantee that the
632
   right type of info in inserted as info is passed as a void* pointer.
633
   This function returns true if there is no error.  */
634
635
static bool
636
insert_info_hash_table (struct info_hash_table *hash_table,
637
      const char *key,
638
      void *info,
639
      bool copy_p)
640
16.6k
{
641
16.6k
  struct info_hash_entry *entry;
642
16.6k
  struct info_list_node *node;
643
644
16.6k
  entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
645
16.6k
                 key, true, copy_p);
646
16.6k
  if (!entry)
647
0
    return false;
648
649
16.6k
  node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
650
16.6k
                  sizeof (*node));
651
16.6k
  if (!node)
652
0
    return false;
653
654
16.6k
  node->info = info;
655
16.6k
  node->next = entry->head;
656
16.6k
  entry->head = node;
657
658
16.6k
  return true;
659
16.6k
}
660
661
/* Look up an info entry list from an info hash table.  Return NULL
662
   if there is none.  */
663
664
static struct info_list_node *
665
lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
666
16.4k
{
667
16.4k
  struct info_hash_entry *entry;
668
669
16.4k
  entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
670
16.4k
                 false, false);
671
16.4k
  return entry ? entry->head : NULL;
672
16.4k
}
673
674
/* Read a section into its appropriate place in the dwarf2_debug
675
   struct (indicated by SECTION_BUFFER and SECTION_SIZE).  If SYMS is
676
   not NULL, use bfd_simple_get_relocated_section_contents to read the
677
   section contents, otherwise use bfd_get_section_contents.  Fail if
678
   the located section does not contain at least OFFSET bytes.  */
679
680
static bool
681
read_section (bfd *abfd,
682
        const struct dwarf_debug_section *sec,
683
        asymbol **syms,
684
        uint64_t offset,
685
        bfd_byte **section_buffer,
686
        bfd_size_type *section_size)
687
162k
{
688
162k
  const char *section_name = sec->uncompressed_name;
689
162k
  bfd_byte *contents = *section_buffer;
690
691
  /* The section may have already been read.  */
692
162k
  if (contents == NULL)
693
3.82k
    {
694
3.82k
      bfd_size_type amt;
695
3.82k
      asection *msec;
696
697
3.82k
      msec = bfd_get_section_by_name (abfd, section_name);
698
3.82k
      if (msec == NULL)
699
445
  {
700
445
    section_name = sec->compressed_name;
701
445
          msec = bfd_get_section_by_name (abfd, section_name);
702
445
  }
703
3.82k
      if (msec == NULL)
704
445
  {
705
445
    _bfd_error_handler (_("DWARF error: can't find %s section."),
706
445
            sec->uncompressed_name);
707
445
    bfd_set_error (bfd_error_bad_value);
708
445
    return false;
709
445
  }
710
711
3.38k
      if ((msec->flags & SEC_HAS_CONTENTS) == 0)
712
6
  {
713
6
    _bfd_error_handler (_("DWARF error: section %s has no contents"),
714
6
            section_name);
715
6
    bfd_set_error (bfd_error_no_contents);
716
6
    return false;
717
6
  }
718
719
3.37k
      if (bfd_section_size_insane (abfd, msec))
720
548
  {
721
    /* PR 26946 */
722
548
    _bfd_error_handler (_("DWARF error: section %s is too big"),
723
548
            section_name);
724
548
    return false;
725
548
  }
726
2.82k
      amt = bfd_get_section_limit_octets (abfd, msec);
727
2.82k
      *section_size = amt;
728
      /* Paranoia - alloc one extra so that we can make sure a string
729
   section is NUL terminated.  */
730
2.82k
      amt += 1;
731
2.82k
      if (amt == 0)
732
0
  {
733
    /* Paranoia - this should never happen.  */
734
0
    bfd_set_error (bfd_error_no_memory);
735
0
    return false;
736
0
  }
737
2.82k
      contents = (bfd_byte *) bfd_malloc (amt);
738
2.82k
      if (contents == NULL)
739
0
  return false;
740
2.82k
      if (syms
741
2.82k
    ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
742
2.69k
              syms)
743
2.82k
    : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
744
619
  {
745
619
    free (contents);
746
619
    return false;
747
619
  }
748
2.20k
      contents[*section_size] = 0;
749
2.20k
      *section_buffer = contents;
750
2.20k
    }
751
752
  /* It is possible to get a bad value for the offset into the section
753
     that the client wants.  Validate it here to avoid trouble later.  */
754
160k
  if (offset != 0 && offset >= *section_size)
755
359
    {
756
      /* xgettext: c-format */
757
359
      _bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
758
359
          " greater than or equal to %s size (%" PRIu64 ")"),
759
359
        (uint64_t) offset, section_name,
760
359
        (uint64_t) *section_size);
761
359
      bfd_set_error (bfd_error_bad_value);
762
359
      return false;
763
359
    }
764
765
160k
  return true;
766
160k
}
767
768
/* Read dwarf information from a buffer.  */
769
770
static inline uint64_t
771
read_n_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end, int n)
772
2.82M
{
773
2.82M
  bfd_byte *buf = *ptr;
774
2.82M
  if (end - buf < n)
775
115
    {
776
115
      *ptr = end;
777
115
      return 0;
778
115
    }
779
2.82M
  *ptr = buf + n;
780
2.82M
  return bfd_get (n * 8, abfd, buf);
781
2.82M
}
782
783
static unsigned int
784
read_1_byte (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
785
2.20M
{
786
2.20M
  return read_n_bytes (abfd, ptr, end, 1);
787
2.20M
}
788
789
static int
790
read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte **ptr, bfd_byte *end)
791
1.64k
{
792
1.64k
  bfd_byte *buf = *ptr;
793
1.64k
  if (end - buf < 1)
794
0
    {
795
0
      *ptr = end;
796
0
      return 0;
797
0
    }
798
1.64k
  *ptr = buf + 1;
799
1.64k
  return bfd_get_signed_8 (abfd, buf);
800
1.64k
}
801
802
static unsigned int
803
read_2_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
804
100k
{
805
100k
  return read_n_bytes (abfd, ptr, end, 2);
806
100k
}
807
808
static unsigned int
809
read_3_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
810
52
{
811
52
  unsigned int val = read_1_byte (abfd, ptr, end);
812
52
  val <<= 8;
813
52
  val |= read_1_byte (abfd, ptr, end);
814
52
  val <<= 8;
815
52
  val |= read_1_byte (abfd, ptr, end);
816
52
  if (bfd_little_endian (abfd))
817
50
    val = (((val >> 16) & 0xff)
818
50
     | (val & 0xff00)
819
50
     | ((val & 0xff) << 16));
820
52
  return val;
821
52
}
822
823
static unsigned int
824
read_4_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
825
519k
{
826
519k
  return read_n_bytes (abfd, ptr, end, 4);
827
519k
}
828
829
static uint64_t
830
read_8_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
831
428
{
832
428
  return read_n_bytes (abfd, ptr, end, 8);
833
428
}
834
835
static struct dwarf_block *
836
read_blk (bfd *abfd, bfd_byte **ptr, bfd_byte *end, size_t size)
837
61.4k
{
838
61.4k
  bfd_byte *buf = *ptr;
839
61.4k
  struct dwarf_block *block;
840
841
61.4k
  block = (struct dwarf_block *) bfd_alloc (abfd, sizeof (*block));
842
61.4k
  if (block == NULL)
843
0
    return NULL;
844
845
61.4k
  if (size > (size_t) (end - buf))
846
16
    {
847
16
      *ptr = end;
848
16
      block->data = NULL;
849
16
      block->size = 0;
850
16
    }
851
61.4k
  else
852
61.4k
    {
853
61.4k
      *ptr = buf + size;
854
61.4k
      block->data = buf;
855
61.4k
      block->size = size;
856
61.4k
    }
857
61.4k
  return block;
858
61.4k
}
859
860
/* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
861
   Bytes at or beyond BUF_END will not be read.  Returns NULL if the
862
   terminator is not found or if the string is empty.  *PTR is
863
   incremented over the bytes scanned, including the terminator.  */
864
865
static char *
866
read_string (bfd_byte **ptr,
867
       bfd_byte *buf_end)
868
60.2k
{
869
60.2k
  bfd_byte *buf = *ptr;
870
60.2k
  bfd_byte *str = buf;
871
872
433k
  while (buf < buf_end)
873
433k
    if (*buf++ == 0)
874
60.1k
      {
875
60.1k
  if (str == buf - 1)
876
2.95k
    break;
877
57.2k
  *ptr = buf;
878
57.2k
  return (char *) str;
879
60.1k
      }
880
881
2.99k
  *ptr = buf;
882
2.99k
  return NULL;
883
60.2k
}
884
885
/* Reads an offset from *PTR and then locates the string at this offset
886
   inside the debug string section.  Returns a pointer to the string.
887
   Increments *PTR by the number of bytes read for the offset.  This
888
   value is set even if the function fails.  Bytes at or beyond
889
   BUF_END will not be read.  Returns NULL if there was a problem, or
890
   if the string is empty.  Does not check for NUL termination of the
891
   string.  */
892
893
static char *
894
read_indirect_string (struct comp_unit *unit,
895
          bfd_byte **ptr,
896
          bfd_byte *buf_end)
897
151k
{
898
151k
  uint64_t offset;
899
151k
  struct dwarf2_debug *stash = unit->stash;
900
151k
  struct dwarf2_debug_file *file = unit->file;
901
151k
  char *str;
902
903
151k
  if (unit->offset_size > (size_t) (buf_end - *ptr))
904
0
    {
905
0
      *ptr = buf_end;
906
0
      return NULL;
907
0
    }
908
909
151k
  if (unit->offset_size == 4)
910
151k
    offset = read_4_bytes (unit->abfd, ptr, buf_end);
911
0
  else
912
0
    offset = read_8_bytes (unit->abfd, ptr, buf_end);
913
914
151k
  if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
915
151k
          file->syms, offset,
916
151k
          &file->dwarf_str_buffer, &file->dwarf_str_size))
917
1.05k
    return NULL;
918
919
150k
  str = (char *) file->dwarf_str_buffer + offset;
920
150k
  if (*str == '\0')
921
8.91k
    return NULL;
922
141k
  return str;
923
150k
}
924
925
/* Like read_indirect_string but from .debug_line_str section.  */
926
927
static char *
928
read_indirect_line_string (struct comp_unit *unit,
929
         bfd_byte **ptr,
930
         bfd_byte *buf_end)
931
3.23k
{
932
3.23k
  uint64_t offset;
933
3.23k
  struct dwarf2_debug *stash = unit->stash;
934
3.23k
  struct dwarf2_debug_file *file = unit->file;
935
3.23k
  char *str;
936
937
3.23k
  if (unit->offset_size > (size_t) (buf_end - *ptr))
938
0
    {
939
0
      *ptr = buf_end;
940
0
      return NULL;
941
0
    }
942
943
3.23k
  if (unit->offset_size == 4)
944
3.23k
    offset = read_4_bytes (unit->abfd, ptr, buf_end);
945
0
  else
946
0
    offset = read_8_bytes (unit->abfd, ptr, buf_end);
947
948
3.23k
  if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
949
3.23k
          file->syms, offset,
950
3.23k
          &file->dwarf_line_str_buffer,
951
3.23k
          &file->dwarf_line_str_size))
952
150
    return NULL;
953
954
3.08k
  str = (char *) file->dwarf_line_str_buffer + offset;
955
3.08k
  if (*str == '\0')
956
251
    return NULL;
957
2.83k
  return str;
958
3.08k
}
959
960
/* Like read_indirect_string but uses a .debug_str located in
961
   an alternate file pointed to by the .gnu_debugaltlink section.
962
   Used to impement DW_FORM_GNU_strp_alt.  */
963
964
static char *
965
read_alt_indirect_string (struct comp_unit *unit,
966
        bfd_byte **ptr,
967
        bfd_byte *buf_end)
968
0
{
969
0
  uint64_t offset;
970
0
  struct dwarf2_debug *stash = unit->stash;
971
0
  char *str;
972
973
0
  if (unit->offset_size > (size_t) (buf_end - *ptr))
974
0
    {
975
0
      *ptr = buf_end;
976
0
      return NULL;
977
0
    }
978
979
0
  if (unit->offset_size == 4)
980
0
    offset = read_4_bytes (unit->abfd, ptr, buf_end);
981
0
  else
982
0
    offset = read_8_bytes (unit->abfd, ptr, buf_end);
983
984
0
  if (stash->alt.bfd_ptr == NULL)
985
0
    {
986
0
      bfd *debug_bfd;
987
0
      char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
988
989
0
      if (debug_filename == NULL)
990
0
  return NULL;
991
992
0
      debug_bfd = bfd_openr (debug_filename, NULL);
993
0
      free (debug_filename);
994
0
      if (debug_bfd == NULL)
995
  /* FIXME: Should we report our failure to follow the debuglink ?  */
996
0
  return NULL;
997
998
0
      if (!bfd_check_format (debug_bfd, bfd_object))
999
0
  {
1000
0
    bfd_close (debug_bfd);
1001
0
    return NULL;
1002
0
  }
1003
0
      stash->alt.bfd_ptr = debug_bfd;
1004
0
    }
1005
1006
0
  if (! read_section (unit->stash->alt.bfd_ptr,
1007
0
          stash->debug_sections + debug_str_alt,
1008
0
          stash->alt.syms, offset,
1009
0
          &stash->alt.dwarf_str_buffer,
1010
0
          &stash->alt.dwarf_str_size))
1011
0
    return NULL;
1012
1013
0
  str = (char *) stash->alt.dwarf_str_buffer + offset;
1014
0
  if (*str == '\0')
1015
0
    return NULL;
1016
1017
0
  return str;
1018
0
}
1019
1020
/* Resolve an alternate reference from UNIT at OFFSET.
1021
   Returns a pointer into the loaded alternate CU upon success
1022
   or NULL upon failure.  */
1023
1024
static bfd_byte *
1025
read_alt_indirect_ref (struct comp_unit *unit, uint64_t offset)
1026
0
{
1027
0
  struct dwarf2_debug *stash = unit->stash;
1028
1029
0
  if (stash->alt.bfd_ptr == NULL)
1030
0
    {
1031
0
      bfd *debug_bfd;
1032
0
      char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
1033
1034
0
      if (debug_filename == NULL)
1035
0
  return NULL;
1036
1037
0
      debug_bfd = bfd_openr (debug_filename, NULL);
1038
0
      free (debug_filename);
1039
0
      if (debug_bfd == NULL)
1040
  /* FIXME: Should we report our failure to follow the debuglink ?  */
1041
0
  return NULL;
1042
1043
0
      if (!bfd_check_format (debug_bfd, bfd_object))
1044
0
  {
1045
0
    bfd_close (debug_bfd);
1046
0
    return NULL;
1047
0
  }
1048
0
      stash->alt.bfd_ptr = debug_bfd;
1049
0
    }
1050
1051
0
  if (! read_section (unit->stash->alt.bfd_ptr,
1052
0
          stash->debug_sections + debug_info_alt,
1053
0
          stash->alt.syms, offset,
1054
0
          &stash->alt.dwarf_info_buffer,
1055
0
          &stash->alt.dwarf_info_size))
1056
0
    return NULL;
1057
1058
0
  return stash->alt.dwarf_info_buffer + offset;
1059
0
}
1060
1061
static uint64_t
1062
read_address (struct comp_unit *unit, bfd_byte **ptr, bfd_byte *buf_end)
1063
1.30M
{
1064
1.30M
  bfd_byte *buf = *ptr;
1065
1.30M
  int signed_vma = 0;
1066
1067
1.30M
  if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
1068
1.30M
    signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
1069
1070
1.30M
  if (unit->addr_size > (size_t) (buf_end - buf))
1071
2
    {
1072
2
      *ptr = buf_end;
1073
2
      return 0;
1074
2
    }
1075
1076
1.30M
  *ptr = buf + unit->addr_size;
1077
1.30M
  if (signed_vma)
1078
467
    {
1079
467
      switch (unit->addr_size)
1080
467
  {
1081
465
  case 8:
1082
465
    return bfd_get_signed_64 (unit->abfd, buf);
1083
2
  case 4:
1084
2
    return bfd_get_signed_32 (unit->abfd, buf);
1085
0
  case 2:
1086
0
    return bfd_get_signed_16 (unit->abfd, buf);
1087
0
  default:
1088
0
    abort ();
1089
467
  }
1090
467
    }
1091
1.30M
  else
1092
1.30M
    {
1093
1.30M
      switch (unit->addr_size)
1094
1.30M
  {
1095
138k
  case 8:
1096
138k
    return bfd_get_64 (unit->abfd, buf);
1097
1.16M
  case 4:
1098
1.16M
    return bfd_get_32 (unit->abfd, buf);
1099
16
  case 2:
1100
16
    return bfd_get_16 (unit->abfd, buf);
1101
0
  default:
1102
0
    abort ();
1103
1.30M
  }
1104
1.30M
    }
1105
1.30M
}
1106
1107
/* Lookup an abbrev_info structure in the abbrev hash table.  */
1108
1109
static struct abbrev_info *
1110
lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
1111
383k
{
1112
383k
  unsigned int hash_number;
1113
383k
  struct abbrev_info *abbrev;
1114
1115
383k
  hash_number = number % ABBREV_HASH_SIZE;
1116
383k
  abbrev = abbrevs[hash_number];
1117
1118
383k
  while (abbrev)
1119
341k
    {
1120
341k
      if (abbrev->number == number)
1121
341k
  return abbrev;
1122
65
      else
1123
65
  abbrev = abbrev->next;
1124
341k
    }
1125
1126
42.1k
  return NULL;
1127
383k
}
1128
1129
/* We keep a hash table to map .debug_abbrev section offsets to the
1130
   array of abbrevs, so that compilation units using the same set of
1131
   abbrevs do not waste memory.  */
1132
1133
struct abbrev_offset_entry
1134
{
1135
  size_t offset;
1136
  struct abbrev_info **abbrevs;
1137
};
1138
1139
static hashval_t
1140
hash_abbrev (const void *p)
1141
4.34k
{
1142
4.34k
  const struct abbrev_offset_entry *ent = p;
1143
4.34k
  return htab_hash_pointer ((void *) ent->offset);
1144
4.34k
}
1145
1146
static int
1147
eq_abbrev (const void *pa, const void *pb)
1148
1.58k
{
1149
1.58k
  const struct abbrev_offset_entry *a = pa;
1150
1.58k
  const struct abbrev_offset_entry *b = pb;
1151
1.58k
  return a->offset == b->offset;
1152
1.58k
}
1153
1154
static void
1155
del_abbrev (void *p)
1156
2.22k
{
1157
2.22k
  struct abbrev_offset_entry *ent = p;
1158
2.22k
  struct abbrev_info **abbrevs = ent->abbrevs;
1159
2.22k
  size_t i;
1160
1161
271k
  for (i = 0; i < ABBREV_HASH_SIZE; i++)
1162
268k
    {
1163
268k
      struct abbrev_info *abbrev = abbrevs[i];
1164
1165
311k
      while (abbrev)
1166
42.1k
  {
1167
42.1k
    free (abbrev->attrs);
1168
42.1k
    abbrev = abbrev->next;
1169
42.1k
  }
1170
268k
    }
1171
2.22k
  free (ent);
1172
2.22k
}
1173
1174
/* In DWARF version 2, the description of the debugging information is
1175
   stored in a separate .debug_abbrev section.  Before we read any
1176
   dies from a section we read in all abbreviations and install them
1177
   in a hash table.  */
1178
1179
static struct abbrev_info**
1180
read_abbrevs (bfd *abfd, uint64_t offset, struct dwarf2_debug *stash,
1181
        struct dwarf2_debug_file *file)
1182
2.35k
{
1183
2.35k
  struct abbrev_info **abbrevs;
1184
2.35k
  bfd_byte *abbrev_ptr;
1185
2.35k
  bfd_byte *abbrev_end;
1186
2.35k
  struct abbrev_info *cur_abbrev;
1187
2.35k
  unsigned int abbrev_number, abbrev_name;
1188
2.35k
  unsigned int abbrev_form, hash_number;
1189
2.35k
  size_t amt;
1190
2.35k
  void **slot;
1191
2.35k
  struct abbrev_offset_entry ent = { offset, NULL };
1192
1193
2.35k
  if (ent.offset != offset)
1194
0
    return NULL;
1195
1196
2.35k
  slot = htab_find_slot (file->abbrev_offsets, &ent, INSERT);
1197
2.35k
  if (slot == NULL)
1198
0
    return NULL;
1199
2.35k
  if (*slot != NULL)
1200
123
    return ((struct abbrev_offset_entry *) (*slot))->abbrevs;
1201
1202
2.23k
  if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
1203
2.23k
          file->syms, offset,
1204
2.23k
          &file->dwarf_abbrev_buffer,
1205
2.23k
          &file->dwarf_abbrev_size))
1206
7
    return NULL;
1207
1208
2.22k
  amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
1209
2.22k
  abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
1210
2.22k
  if (abbrevs == NULL)
1211
0
    return NULL;
1212
1213
2.22k
  abbrev_ptr = file->dwarf_abbrev_buffer + offset;
1214
2.22k
  abbrev_end = file->dwarf_abbrev_buffer + file->dwarf_abbrev_size;
1215
2.22k
  abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1216
2.22k
           false, abbrev_end);
1217
1218
  /* Loop until we reach an abbrev number of 0.  */
1219
44.2k
  while (abbrev_number)
1220
42.1k
    {
1221
42.1k
      amt = sizeof (struct abbrev_info);
1222
42.1k
      cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
1223
42.1k
      if (cur_abbrev == NULL)
1224
0
  goto fail;
1225
1226
      /* Read in abbrev header.  */
1227
42.1k
      cur_abbrev->number = abbrev_number;
1228
42.1k
      cur_abbrev->tag = (enum dwarf_tag)
1229
42.1k
  _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1230
42.1k
             false, abbrev_end);
1231
42.1k
      cur_abbrev->has_children = read_1_byte (abfd, &abbrev_ptr, abbrev_end);
1232
1233
      /* Now read in declarations.  */
1234
42.1k
      for (;;)
1235
216k
  {
1236
    /* Initialize it just to avoid a GCC false warning.  */
1237
216k
    bfd_vma implicit_const = -1;
1238
1239
216k
    abbrev_name = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1240
216k
                 false, abbrev_end);
1241
216k
    abbrev_form = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1242
216k
                 false, abbrev_end);
1243
216k
    if (abbrev_form == DW_FORM_implicit_const)
1244
330
      implicit_const = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1245
330
                true, abbrev_end);
1246
216k
    if (abbrev_name == 0)
1247
42.1k
      break;
1248
1249
174k
    if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
1250
62.4k
      {
1251
62.4k
        struct attr_abbrev *tmp;
1252
1253
62.4k
        amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
1254
62.4k
        amt *= sizeof (struct attr_abbrev);
1255
62.4k
        tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
1256
62.4k
        if (tmp == NULL)
1257
0
    goto fail;
1258
62.4k
        cur_abbrev->attrs = tmp;
1259
62.4k
      }
1260
1261
174k
    cur_abbrev->attrs[cur_abbrev->num_attrs].name
1262
174k
      = (enum dwarf_attribute) abbrev_name;
1263
174k
    cur_abbrev->attrs[cur_abbrev->num_attrs].form
1264
174k
      = (enum dwarf_form) abbrev_form;
1265
174k
    cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
1266
174k
      = implicit_const;
1267
174k
    ++cur_abbrev->num_attrs;
1268
174k
  }
1269
1270
42.1k
      hash_number = abbrev_number % ABBREV_HASH_SIZE;
1271
42.1k
      cur_abbrev->next = abbrevs[hash_number];
1272
42.1k
      abbrevs[hash_number] = cur_abbrev;
1273
1274
      /* Get next abbreviation.
1275
   Under Irix6 the abbreviations for a compilation unit are not
1276
   always properly terminated with an abbrev number of 0.
1277
   Exit loop if we encounter an abbreviation which we have
1278
   already read (which means we are about to read the abbreviations
1279
   for the next compile unit) or if the end of the abbreviation
1280
   table is reached.  */
1281
42.1k
      if ((size_t) (abbrev_ptr - file->dwarf_abbrev_buffer)
1282
42.1k
    >= file->dwarf_abbrev_size)
1283
34
  break;
1284
42.1k
      abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1285
42.1k
               false, abbrev_end);
1286
42.1k
      if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
1287
60
  break;
1288
42.1k
    }
1289
1290
2.22k
  *slot = bfd_malloc (sizeof ent);
1291
2.22k
  if (!*slot)
1292
0
    goto fail;
1293
2.22k
  ent.abbrevs = abbrevs;
1294
2.22k
  memcpy (*slot, &ent, sizeof ent);
1295
2.22k
  return abbrevs;
1296
1297
0
 fail:
1298
0
  if (abbrevs != NULL)
1299
0
    {
1300
0
      size_t i;
1301
1302
0
      for (i = 0; i < ABBREV_HASH_SIZE; i++)
1303
0
  {
1304
0
    struct abbrev_info *abbrev = abbrevs[i];
1305
1306
0
    while (abbrev)
1307
0
      {
1308
0
        free (abbrev->attrs);
1309
0
        abbrev = abbrev->next;
1310
0
      }
1311
0
  }
1312
0
      free (abbrevs);
1313
0
    }
1314
0
  return NULL;
1315
2.22k
}
1316
1317
/* Returns true if the form is one which has a string value.  */
1318
1319
static bool
1320
is_str_form (const struct attribute *attr)
1321
67.7k
{
1322
67.7k
  switch (attr->form)
1323
67.7k
    {
1324
10.3k
    case DW_FORM_string:
1325
67.4k
    case DW_FORM_strp:
1326
67.4k
    case DW_FORM_strx:
1327
67.5k
    case DW_FORM_strx1:
1328
67.5k
    case DW_FORM_strx2:
1329
67.5k
    case DW_FORM_strx3:
1330
67.6k
    case DW_FORM_strx4:
1331
67.7k
    case DW_FORM_line_strp:
1332
67.7k
    case DW_FORM_GNU_strp_alt:
1333
67.7k
      return true;
1334
1335
82
    default:
1336
82
      return false;
1337
67.7k
    }
1338
67.7k
}
1339
1340
/* Returns true if the form is one which has an integer value.  */
1341
1342
static bool
1343
is_int_form (const struct attribute *attr)
1344
183k
{
1345
183k
  switch (attr->form)
1346
183k
    {
1347
22.3k
    case DW_FORM_addr:
1348
59.9k
    case DW_FORM_data2:
1349
68.2k
    case DW_FORM_data4:
1350
68.3k
    case DW_FORM_data8:
1351
156k
    case DW_FORM_data1:
1352
158k
    case DW_FORM_flag:
1353
158k
    case DW_FORM_sdata:
1354
158k
    case DW_FORM_udata:
1355
158k
    case DW_FORM_ref_addr:
1356
158k
    case DW_FORM_ref1:
1357
158k
    case DW_FORM_ref2:
1358
174k
    case DW_FORM_ref4:
1359
174k
    case DW_FORM_ref8:
1360
174k
    case DW_FORM_ref_udata:
1361
183k
    case DW_FORM_sec_offset:
1362
183k
    case DW_FORM_flag_present:
1363
183k
    case DW_FORM_ref_sig8:
1364
183k
    case DW_FORM_addrx:
1365
183k
    case DW_FORM_implicit_const:
1366
183k
    case DW_FORM_addrx1:
1367
183k
    case DW_FORM_addrx2:
1368
183k
    case DW_FORM_addrx3:
1369
183k
    case DW_FORM_addrx4:
1370
183k
    case DW_FORM_GNU_ref_alt:
1371
183k
      return true;
1372
1373
83
    default:
1374
83
      return false;
1375
183k
    }
1376
183k
}
1377
1378
/* Returns true if the form is strx[1-4].  */
1379
1380
static inline bool
1381
is_strx_form (enum dwarf_form form)
1382
15.7k
{
1383
15.7k
  return (form == DW_FORM_strx
1384
15.7k
    || form == DW_FORM_strx1
1385
15.7k
    || form == DW_FORM_strx2
1386
15.7k
    || form == DW_FORM_strx3
1387
15.7k
    || form == DW_FORM_strx4);
1388
15.7k
}
1389
1390
/* Return true if the form is addrx[1-4].  */
1391
1392
static inline bool
1393
is_addrx_form (enum dwarf_form form)
1394
15.6k
{
1395
15.6k
  return (form == DW_FORM_addrx
1396
15.6k
    || form == DW_FORM_addrx1
1397
15.6k
    || form == DW_FORM_addrx2
1398
15.6k
    || form == DW_FORM_addrx3
1399
15.6k
    || form == DW_FORM_addrx4);
1400
15.6k
}
1401
1402
/* Returns the address in .debug_addr section using DW_AT_addr_base.
1403
   Used to implement DW_FORM_addrx*.  */
1404
static uint64_t
1405
read_indexed_address (uint64_t idx, struct comp_unit *unit)
1406
82
{
1407
82
  struct dwarf2_debug *stash = unit->stash;
1408
82
  struct dwarf2_debug_file *file = unit->file;
1409
82
  bfd_byte *info_ptr;
1410
82
  size_t offset;
1411
1412
82
  if (stash == NULL)
1413
0
    return 0;
1414
1415
82
  if (!read_section (unit->abfd, &stash->debug_sections[debug_addr],
1416
82
         file->syms, 0,
1417
82
         &file->dwarf_addr_buffer, &file->dwarf_addr_size))
1418
33
    return 0;
1419
1420
49
  if (_bfd_mul_overflow (idx, unit->addr_size, &offset))
1421
0
    return 0;
1422
1423
49
  offset += unit->dwarf_addr_offset;
1424
49
  if (offset < unit->dwarf_addr_offset
1425
49
      || offset > file->dwarf_addr_size
1426
49
      || file->dwarf_addr_size - offset < unit->addr_size)
1427
10
    return 0;
1428
1429
39
  info_ptr = file->dwarf_addr_buffer + offset;
1430
1431
39
  if (unit->addr_size == 4)
1432
0
    return bfd_get_32 (unit->abfd, info_ptr);
1433
39
  else if (unit->addr_size == 8)
1434
39
    return bfd_get_64 (unit->abfd, info_ptr);
1435
0
  else
1436
0
    return 0;
1437
39
}
1438
1439
/* Returns the string using DW_AT_str_offsets_base.
1440
   Used to implement DW_FORM_strx*.  */
1441
static const char *
1442
read_indexed_string (uint64_t idx, struct comp_unit *unit)
1443
882
{
1444
882
  struct dwarf2_debug *stash = unit->stash;
1445
882
  struct dwarf2_debug_file *file = unit->file;
1446
882
  bfd_byte *info_ptr;
1447
882
  uint64_t str_offset;
1448
882
  size_t offset;
1449
1450
882
  if (stash == NULL)
1451
0
    return NULL;
1452
1453
882
  if (!read_section (unit->abfd, &stash->debug_sections[debug_str],
1454
882
         file->syms, 0,
1455
882
         &file->dwarf_str_buffer, &file->dwarf_str_size))
1456
134
    return NULL;
1457
1458
748
  if (!read_section (unit->abfd, &stash->debug_sections[debug_str_offsets],
1459
748
         file->syms, 0,
1460
748
         &file->dwarf_str_offsets_buffer,
1461
748
         &file->dwarf_str_offsets_size))
1462
427
    return NULL;
1463
1464
321
  if (_bfd_mul_overflow (idx, unit->offset_size, &offset))
1465
0
    return NULL;
1466
1467
321
  offset += unit->dwarf_str_offset;
1468
321
  if (offset < unit->dwarf_str_offset
1469
321
      || offset > file->dwarf_str_offsets_size
1470
321
      || file->dwarf_str_offsets_size - offset < unit->offset_size)
1471
259
    return NULL;
1472
1473
62
  info_ptr = file->dwarf_str_offsets_buffer + offset;
1474
1475
62
  if (unit->offset_size == 4)
1476
62
    str_offset = bfd_get_32 (unit->abfd, info_ptr);
1477
0
  else if (unit->offset_size == 8)
1478
0
    str_offset = bfd_get_64 (unit->abfd, info_ptr);
1479
0
  else
1480
0
    return NULL;
1481
1482
62
  if (str_offset >= file->dwarf_str_size)
1483
10
    return NULL;
1484
52
  return (const char *) file->dwarf_str_buffer + str_offset;
1485
62
}
1486
1487
/* Read and fill in the value of attribute ATTR as described by FORM.
1488
   Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1489
   Returns an updated INFO_PTR taking into account the amount of data read.  */
1490
1491
static bfd_byte *
1492
read_attribute_value (struct attribute *  attr,
1493
          unsigned      form,
1494
          bfd_vma     implicit_const,
1495
          struct comp_unit *  unit,
1496
          bfd_byte *    info_ptr,
1497
          bfd_byte *    info_ptr_end)
1498
1.23M
{
1499
1.23M
  bfd *abfd = unit->abfd;
1500
1.23M
  size_t amt;
1501
1502
1.23M
  if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
1503
19
    {
1504
19
      _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1505
19
      bfd_set_error (bfd_error_bad_value);
1506
19
      return NULL;
1507
19
    }
1508
1509
1.23M
  attr->form = (enum dwarf_form) form;
1510
1511
1.23M
  switch (form)
1512
1.23M
    {
1513
26.0k
    case DW_FORM_flag_present:
1514
26.0k
      attr->u.val = 1;
1515
26.0k
      break;
1516
72
    case DW_FORM_ref_addr:
1517
      /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1518
   DWARF3.  */
1519
72
      if (unit->version >= 3)
1520
72
  {
1521
72
    if (unit->offset_size == 4)
1522
72
      attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1523
0
    else
1524
0
      attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1525
72
    break;
1526
72
  }
1527
      /* FALLTHROUGH */
1528
93.8k
    case DW_FORM_addr:
1529
93.8k
      attr->u.val = read_address (unit, &info_ptr, info_ptr_end);
1530
93.8k
      break;
1531
0
    case DW_FORM_GNU_ref_alt:
1532
18.7k
    case DW_FORM_sec_offset:
1533
18.7k
      if (unit->offset_size == 4)
1534
18.7k
  attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1535
0
      else
1536
0
  attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1537
18.7k
      break;
1538
9
    case DW_FORM_block2:
1539
9
      amt = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1540
9
      attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1541
9
      if (attr->u.blk == NULL)
1542
0
  return NULL;
1543
9
      break;
1544
32
    case DW_FORM_block4:
1545
32
      amt = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1546
32
      attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1547
32
      if (attr->u.blk == NULL)
1548
0
  return NULL;
1549
32
      break;
1550
32
    case DW_FORM_ref1:
1551
26.1k
    case DW_FORM_flag:
1552
394k
    case DW_FORM_data1:
1553
394k
      attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1554
394k
      break;
1555
44
    case DW_FORM_addrx1:
1556
44
      attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1557
      /* dwarf_addr_offset value 0 indicates the attribute DW_AT_addr_base
1558
   is not yet read.  */
1559
44
      if (unit->dwarf_addr_offset != 0)
1560
6
  attr->u.val = read_indexed_address (attr->u.val, unit);
1561
44
      break;
1562
95.6k
    case DW_FORM_data2:
1563
95.7k
    case DW_FORM_ref2:
1564
95.7k
      attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1565
95.7k
      break;
1566
55
    case DW_FORM_addrx2:
1567
55
      attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1568
55
      if (unit->dwarf_addr_offset != 0)
1569
0
  attr->u.val = read_indexed_address (attr->u.val, unit);
1570
55
      break;
1571
19
    case DW_FORM_addrx3:
1572
19
      attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1573
19
      if (unit->dwarf_addr_offset != 0)
1574
15
  attr->u.val = read_indexed_address(attr->u.val, unit);
1575
19
      break;
1576
284k
    case DW_FORM_ref4:
1577
336k
    case DW_FORM_data4:
1578
336k
      attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1579
336k
      break;
1580
5
    case DW_FORM_addrx4:
1581
5
      attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1582
5
      if (unit->dwarf_addr_offset != 0)
1583
4
  attr->u.val = read_indexed_address (attr->u.val, unit);
1584
5
      break;
1585
254
    case DW_FORM_data8:
1586
284
    case DW_FORM_ref8:
1587
413
    case DW_FORM_ref_sig8:
1588
413
      attr->u.val = read_8_bytes (abfd, &info_ptr, info_ptr_end);
1589
413
      break;
1590
39.5k
    case DW_FORM_string:
1591
39.5k
      attr->u.str = read_string (&info_ptr, info_ptr_end);
1592
39.5k
      break;
1593
151k
    case DW_FORM_strp:
1594
151k
      attr->u.str = read_indirect_string (unit, &info_ptr, info_ptr_end);
1595
151k
      break;
1596
3.23k
    case DW_FORM_line_strp:
1597
3.23k
      attr->u.str = read_indirect_line_string (unit, &info_ptr, info_ptr_end);
1598
3.23k
      break;
1599
0
    case DW_FORM_GNU_strp_alt:
1600
0
      attr->u.str = read_alt_indirect_string (unit, &info_ptr, info_ptr_end);
1601
0
      break;
1602
859
    case DW_FORM_strx1:
1603
859
      attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1604
      /* dwarf_str_offset value 0 indicates the attribute DW_AT_str_offsets_base
1605
   is not yet read.  */
1606
859
      if (unit->dwarf_str_offset != 0)
1607
762
  attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1608
97
      else
1609
97
  attr->u.str = NULL;
1610
859
      break;
1611
18
    case DW_FORM_strx2:
1612
18
      attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1613
18
      if (unit->dwarf_str_offset != 0)
1614
0
  attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1615
18
      else
1616
18
  attr->u.str = NULL;
1617
18
      break;
1618
33
    case DW_FORM_strx3:
1619
33
      attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1620
33
      if (unit->dwarf_str_offset != 0)
1621
0
  attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1622
33
      else
1623
33
  attr->u.str = NULL;
1624
33
      break;
1625
36
    case DW_FORM_strx4:
1626
36
      attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1627
36
      if (unit->dwarf_str_offset != 0)
1628
8
  attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1629
28
      else
1630
28
  attr->u.str = NULL;
1631
36
      break;
1632
10
    case DW_FORM_strx:
1633
10
      attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1634
10
             false, info_ptr_end);
1635
10
      if (unit->dwarf_str_offset != 0)
1636
0
  attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1637
10
      else
1638
10
  attr->u.str = NULL;
1639
10
      break;
1640
3.68k
    case DW_FORM_exprloc:
1641
3.71k
    case DW_FORM_block:
1642
3.71k
      amt = _bfd_safe_read_leb128 (abfd, &info_ptr,
1643
3.71k
           false, info_ptr_end);
1644
3.71k
      attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1645
3.71k
      if (attr->u.blk == NULL)
1646
0
  return NULL;
1647
3.71k
      break;
1648
57.5k
    case DW_FORM_block1:
1649
57.5k
      amt = read_1_byte (abfd, &info_ptr, info_ptr_end);
1650
57.5k
      attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1651
57.5k
      if (attr->u.blk == NULL)
1652
0
  return NULL;
1653
57.5k
      break;
1654
57.5k
    case DW_FORM_sdata:
1655
7.45k
      attr->u.sval = _bfd_safe_read_leb128 (abfd, &info_ptr,
1656
7.45k
              true, info_ptr_end);
1657
7.45k
      break;
1658
1659
2
    case DW_FORM_rnglistx:
1660
24
    case DW_FORM_loclistx:
1661
      /* FIXME: Add support for these forms!  */
1662
      /* Fall through.  */
1663
53
    case DW_FORM_ref_udata:
1664
2.36k
    case DW_FORM_udata:
1665
2.36k
      attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1666
2.36k
             false, info_ptr_end);
1667
2.36k
      break;
1668
82
    case DW_FORM_addrx:
1669
82
      attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1670
82
             false, info_ptr_end);
1671
82
      if (unit->dwarf_addr_offset != 0)
1672
10
  attr->u.val = read_indexed_address (attr->u.val, unit);
1673
82
      break;
1674
1
    case DW_FORM_indirect:
1675
1
      form = _bfd_safe_read_leb128 (abfd, &info_ptr,
1676
1
            false, info_ptr_end);
1677
1
      if (form == DW_FORM_implicit_const)
1678
0
  implicit_const = _bfd_safe_read_leb128 (abfd, &info_ptr,
1679
0
            true, info_ptr_end);
1680
1
      info_ptr = read_attribute_value (attr, form, implicit_const, unit,
1681
1
               info_ptr, info_ptr_end);
1682
1
      break;
1683
1.82k
    case DW_FORM_implicit_const:
1684
1.82k
      attr->form = DW_FORM_sdata;
1685
1.82k
      attr->u.sval = implicit_const;
1686
1.82k
      break;
1687
115
    case DW_FORM_data16:
1688
      /* This is really a "constant", but there is no way to store that
1689
         so pretend it is a 16 byte block instead.  */
1690
115
      attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, 16);
1691
115
      if (attr->u.blk == NULL)
1692
0
  return NULL;
1693
115
      break;
1694
1695
115
    default:
1696
41
      _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1697
41
        form);
1698
41
      bfd_set_error (bfd_error_bad_value);
1699
41
      return NULL;
1700
1.23M
    }
1701
1.23M
  return info_ptr;
1702
1.23M
}
1703
1704
/* Read an attribute described by an abbreviated attribute.  */
1705
1706
static bfd_byte *
1707
read_attribute (struct attribute *    attr,
1708
    struct attr_abbrev *  abbrev,
1709
    struct comp_unit *    unit,
1710
    bfd_byte *        info_ptr,
1711
    bfd_byte *        info_ptr_end)
1712
1.22M
{
1713
1.22M
  attr->name = abbrev->name;
1714
1.22M
  info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
1715
1.22M
           unit, info_ptr, info_ptr_end);
1716
1.22M
  return info_ptr;
1717
1.22M
}
1718
1719
/* Return mangling style given LANG.  */
1720
1721
static int
1722
mangle_style (int lang)
1723
29.4k
{
1724
29.4k
  switch (lang)
1725
29.4k
    {
1726
0
    case DW_LANG_Ada83:
1727
4
    case DW_LANG_Ada95:
1728
4
      return DMGL_GNAT;
1729
1730
2
    case DW_LANG_C_plus_plus:
1731
2
    case DW_LANG_C_plus_plus_03:
1732
4
    case DW_LANG_C_plus_plus_11:
1733
7
    case DW_LANG_C_plus_plus_14:
1734
7
      return DMGL_GNU_V3;
1735
1736
7
    case DW_LANG_Java:
1737
7
      return DMGL_JAVA;
1738
1739
14
    case DW_LANG_D:
1740
14
      return DMGL_DLANG;
1741
1742
2
    case DW_LANG_Rust:
1743
20
    case DW_LANG_Rust_old:
1744
20
      return DMGL_RUST;
1745
1746
52
    default:
1747
52
      return DMGL_AUTO;
1748
1749
10.5k
    case DW_LANG_C89:
1750
10.6k
    case DW_LANG_C:
1751
10.6k
    case DW_LANG_Cobol74:
1752
10.6k
    case DW_LANG_Cobol85:
1753
10.6k
    case DW_LANG_Fortran77:
1754
10.6k
    case DW_LANG_Pascal83:
1755
10.6k
    case DW_LANG_PLI:
1756
29.1k
    case DW_LANG_C99:
1757
29.1k
    case DW_LANG_UPC:
1758
29.3k
    case DW_LANG_C11:
1759
29.3k
    case DW_LANG_Mips_Assembler:
1760
29.3k
    case DW_LANG_Upc:
1761
29.3k
    case DW_LANG_HP_Basic91:
1762
29.3k
    case DW_LANG_HP_IMacro:
1763
29.3k
    case DW_LANG_HP_Assembler:
1764
29.3k
      return 0;
1765
29.4k
    }
1766
29.4k
}
1767
1768
/* Source line information table routines.  */
1769
1770
20.3k
#define FILE_ALLOC_CHUNK 5
1771
6.12k
#define DIR_ALLOC_CHUNK 5
1772
1773
struct line_info
1774
{
1775
  struct line_info *  prev_line;
1776
  bfd_vma   address;
1777
  char *    filename;
1778
  unsigned int    line;
1779
  unsigned int    column;
1780
  unsigned int    discriminator;
1781
  unsigned char   op_index;
1782
  unsigned char   end_sequence;   /* End of (sequential) code sequence.  */
1783
};
1784
1785
struct fileinfo
1786
{
1787
  char *    name;
1788
  unsigned int    dir;
1789
  unsigned int    time;
1790
  unsigned int    size;
1791
};
1792
1793
struct line_sequence
1794
{
1795
  bfd_vma   low_pc;
1796
  struct line_sequence* prev_sequence;
1797
  struct line_info* last_line;  /* Largest VMA.  */
1798
  struct line_info**  line_info_lookup;
1799
  bfd_size_type   num_lines;
1800
};
1801
1802
struct line_info_table
1803
{
1804
  bfd *     abfd;
1805
  unsigned int    num_files;
1806
  unsigned int    num_dirs;
1807
  unsigned int    num_sequences;
1808
  bool                  use_dir_and_file_0;
1809
  char *    comp_dir;
1810
  char **   dirs;
1811
  struct fileinfo*  files;
1812
  struct line_sequence* sequences;
1813
  struct line_info* lcl_head;   /* Local head; used in 'add_line_info'.  */
1814
};
1815
1816
/* Remember some information about each function.  If the function is
1817
   inlined (DW_TAG_inlined_subroutine) it may have two additional
1818
   attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1819
   source code location where this function was inlined.  */
1820
1821
struct funcinfo
1822
{
1823
  /* Pointer to previous function in list of all functions.  */
1824
  struct funcinfo *prev_func;
1825
  /* Pointer to function one scope higher.  */
1826
  struct funcinfo *caller_func;
1827
  /* Source location file name where caller_func inlines this func.  */
1828
  char *caller_file;
1829
  /* Source location file name.  */
1830
  char *file;
1831
  /* Source location line number where caller_func inlines this func.  */
1832
  int caller_line;
1833
  /* Source location line number.  */
1834
  int line;
1835
  int tag;
1836
  bool is_linkage;
1837
  const char *name;
1838
  struct arange arange;
1839
  /* The offset of the funcinfo from the start of the unit.  */
1840
  uint64_t unit_offset;
1841
};
1842
1843
struct lookup_funcinfo
1844
{
1845
  /* Function information corresponding to this lookup table entry.  */
1846
  struct funcinfo *funcinfo;
1847
1848
  /* The lowest address for this specific function.  */
1849
  bfd_vma low_addr;
1850
1851
  /* The highest address of this function before the lookup table is sorted.
1852
     The highest address of all prior functions after the lookup table is
1853
     sorted, which is used for binary search.  */
1854
  bfd_vma high_addr;
1855
  /* Index of this function, used to ensure qsort is stable.  */
1856
  unsigned int idx;
1857
};
1858
1859
struct varinfo
1860
{
1861
  /* Pointer to previous variable in list of all variables.  */
1862
  struct varinfo *prev_var;
1863
  /* The offset of the varinfo from the start of the unit.  */
1864
  uint64_t unit_offset;
1865
  /* Source location file name.  */
1866
  char *file;
1867
  /* Source location line number.  */
1868
  int line;
1869
  /* The type of this variable.  */
1870
  int tag;
1871
  /* The name of the variable, if it has one.  */
1872
  const char *name;
1873
  /* The address of the variable.  */
1874
  bfd_vma addr;
1875
  /* Is this a stack variable?  */
1876
  bool stack;
1877
};
1878
1879
/* Return TRUE if NEW_LINE should sort after LINE.  */
1880
1881
static inline bool
1882
new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1883
804k
{
1884
804k
  return (new_line->address > line->address
1885
804k
    || (new_line->address == line->address
1886
254k
        && new_line->op_index > line->op_index));
1887
804k
}
1888
1889
1890
/* Adds a new entry to the line_info list in the line_info_table, ensuring
1891
   that the list is sorted.  Note that the line_info list is sorted from
1892
   highest to lowest VMA (with possible duplicates); that is,
1893
   line_info->prev_line always accesses an equal or smaller VMA.  */
1894
1895
static bool
1896
add_line_info (struct line_info_table *table,
1897
         bfd_vma address,
1898
         unsigned char op_index,
1899
         char *filename,
1900
         unsigned int line,
1901
         unsigned int column,
1902
         unsigned int discriminator,
1903
         int end_sequence)
1904
582k
{
1905
582k
  size_t amt = sizeof (struct line_info);
1906
582k
  struct line_sequence* seq = table->sequences;
1907
582k
  struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1908
1909
582k
  if (info == NULL)
1910
0
    return false;
1911
1912
  /* Set member data of 'info'.  */
1913
582k
  info->prev_line = NULL;
1914
582k
  info->address = address;
1915
582k
  info->op_index = op_index;
1916
582k
  info->line = line;
1917
582k
  info->column = column;
1918
582k
  info->discriminator = discriminator;
1919
582k
  info->end_sequence = end_sequence;
1920
1921
582k
  if (filename && filename[0])
1922
573k
    {
1923
573k
      info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1924
573k
      if (info->filename == NULL)
1925
0
  return false;
1926
573k
      strcpy (info->filename, filename);
1927
573k
    }
1928
9.12k
  else
1929
9.12k
    info->filename = NULL;
1930
1931
  /* Find the correct location for 'info'.  Normally we will receive
1932
     new line_info data 1) in order and 2) with increasing VMAs.
1933
     However some compilers break the rules (cf. decode_line_info) and
1934
     so we include some heuristics for quickly finding the correct
1935
     location for 'info'. In particular, these heuristics optimize for
1936
     the common case in which the VMA sequence that we receive is a
1937
     list of locally sorted VMAs such as
1938
       p...z a...j  (where a < j < p < z)
1939
1940
     Note: table->lcl_head is used to head an *actual* or *possible*
1941
     sub-sequence within the list (such as a...j) that is not directly
1942
     headed by table->last_line
1943
1944
     Note: we may receive duplicate entries from 'decode_line_info'.  */
1945
1946
582k
  if (seq
1947
582k
      && seq->last_line->address == address
1948
582k
      && seq->last_line->op_index == op_index
1949
582k
      && seq->last_line->end_sequence == end_sequence)
1950
20.5k
    {
1951
      /* We only keep the last entry with the same address and end
1952
   sequence.  See PR ld/4986.  */
1953
20.5k
      if (table->lcl_head == seq->last_line)
1954
1.14k
  table->lcl_head = info;
1955
20.5k
      info->prev_line = seq->last_line->prev_line;
1956
20.5k
      seq->last_line = info;
1957
20.5k
    }
1958
562k
  else if (!seq || seq->last_line->end_sequence)
1959
4.52k
    {
1960
      /* Start a new line sequence.  */
1961
4.52k
      amt = sizeof (struct line_sequence);
1962
4.52k
      seq = (struct line_sequence *) bfd_malloc (amt);
1963
4.52k
      if (seq == NULL)
1964
0
  return false;
1965
4.52k
      seq->low_pc = address;
1966
4.52k
      seq->prev_sequence = table->sequences;
1967
4.52k
      seq->last_line = info;
1968
4.52k
      table->lcl_head = info;
1969
4.52k
      table->sequences = seq;
1970
4.52k
      table->num_sequences++;
1971
4.52k
    }
1972
557k
  else if (info->end_sequence
1973
557k
     || new_line_sorts_after (info, seq->last_line))
1974
554k
    {
1975
      /* Normal case: add 'info' to the beginning of the current sequence.  */
1976
554k
      info->prev_line = seq->last_line;
1977
554k
      seq->last_line = info;
1978
1979
      /* lcl_head: initialize to head a *possible* sequence at the end.  */
1980
554k
      if (!table->lcl_head)
1981
0
  table->lcl_head = info;
1982
554k
    }
1983
3.74k
  else if (!new_line_sorts_after (info, table->lcl_head)
1984
3.74k
     && (!table->lcl_head->prev_line
1985
2.72k
         || new_line_sorts_after (info, table->lcl_head->prev_line)))
1986
1.63k
    {
1987
      /* Abnormal but easy: lcl_head is the head of 'info'.  */
1988
1.63k
      info->prev_line = table->lcl_head->prev_line;
1989
1.63k
      table->lcl_head->prev_line = info;
1990
1.63k
    }
1991
2.10k
  else
1992
2.10k
    {
1993
      /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1994
   are valid heads for 'info'.  Reset 'lcl_head'.  */
1995
2.10k
      struct line_info* li2 = seq->last_line; /* Always non-NULL.  */
1996
2.10k
      struct line_info* li1 = li2->prev_line;
1997
1998
122k
      while (li1)
1999
122k
  {
2000
122k
    if (!new_line_sorts_after (info, li2)
2001
122k
        && new_line_sorts_after (info, li1))
2002
1.81k
      break;
2003
2004
120k
    li2 = li1; /* always non-NULL */
2005
120k
    li1 = li1->prev_line;
2006
120k
  }
2007
2.10k
      table->lcl_head = li2;
2008
2.10k
      info->prev_line = table->lcl_head->prev_line;
2009
2.10k
      table->lcl_head->prev_line = info;
2010
2.10k
      if (address < seq->low_pc)
2011
45
  seq->low_pc = address;
2012
2.10k
    }
2013
582k
  return true;
2014
582k
}
2015
2016
/* Extract a fully qualified filename from a line info table.
2017
   The returned string has been malloc'ed and it is the caller's
2018
   responsibility to free it.  */
2019
2020
static char *
2021
concat_filename (struct line_info_table *table, unsigned int file)
2022
96.1k
{
2023
96.1k
  char *filename;
2024
2025
  /* Pre DWARF-5 entry 0 in the directory and filename tables was not used.
2026
     So in order to save space in the tables used here the info for, eg
2027
     directory 1 is stored in slot 0 of the directory table, directory 2
2028
     in slot 1 and so on.
2029
2030
     Starting with DWARF-5 the 0'th entry is used so there is a one to one
2031
     mapping between DWARF slots and internal table entries.  */
2032
96.1k
  if (! table->use_dir_and_file_0)
2033
93.5k
    {
2034
      /* Pre DWARF-5, FILE == 0 means unknown.  */
2035
93.5k
      if (file == 0)
2036
30
  return strdup ("<unknown>");
2037
93.5k
      -- file;
2038
93.5k
    }
2039
2040
96.0k
  if (table == NULL || file >= table->num_files)
2041
117
    {
2042
117
      _bfd_error_handler
2043
117
  (_("DWARF error: mangled line number section (bad file number)"));
2044
117
      return strdup ("<unknown>");
2045
117
    }
2046
2047
95.9k
  filename = table->files[file].name;
2048
2049
95.9k
  if (filename == NULL)
2050
678
    return strdup ("<unknown>");
2051
2052
95.2k
  if (!IS_ABSOLUTE_PATH (filename))
2053
95.2k
    {
2054
95.2k
      char *dir_name = NULL;
2055
95.2k
      char *subdir_name = NULL;
2056
95.2k
      char *name;
2057
95.2k
      size_t len;
2058
95.2k
      unsigned int dir = table->files[file].dir;
2059
2060
95.2k
      if (!table->use_dir_and_file_0)
2061
93.3k
  --dir;
2062
      /* Wrapping from 0 to -1u above gives the intended result with
2063
   the test below of leaving subdir_name NULL for pre-DWARF5 dir
2064
   of 0.  */
2065
      /* PR 17512: file: 0317e960, file: 7f3d2e4b.  */
2066
95.2k
      if (dir < table->num_dirs)
2067
41.0k
  subdir_name = table->dirs[dir];
2068
2069
95.2k
      if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
2070
84.8k
  dir_name = table->comp_dir;
2071
2072
95.2k
      if (!dir_name)
2073
15.7k
  {
2074
15.7k
    dir_name = subdir_name;
2075
15.7k
    subdir_name = NULL;
2076
15.7k
  }
2077
2078
95.2k
      if (!dir_name)
2079
4.28k
  return strdup (filename);
2080
2081
90.9k
      len = strlen (dir_name) + strlen (filename) + 2;
2082
2083
90.9k
      if (subdir_name)
2084
29.4k
  {
2085
29.4k
    len += strlen (subdir_name) + 1;
2086
29.4k
    name = (char *) bfd_malloc (len);
2087
29.4k
    if (name)
2088
29.4k
      sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
2089
29.4k
  }
2090
61.4k
      else
2091
61.4k
  {
2092
61.4k
    name = (char *) bfd_malloc (len);
2093
61.4k
    if (name)
2094
61.4k
      sprintf (name, "%s/%s", dir_name, filename);
2095
61.4k
  }
2096
2097
90.9k
      return name;
2098
95.2k
    }
2099
2100
41
  return strdup (filename);
2101
95.2k
}
2102
2103
/* Number of bits in a bfd_vma.  */
2104
5.83M
#define VMA_BITS (8 * sizeof (bfd_vma))
2105
2106
/* Check whether [low1, high1) can be combined with [low2, high2),
2107
   i.e., they touch or overlap.  */
2108
2109
static bool
2110
ranges_overlap (bfd_vma low1,
2111
    bfd_vma high1,
2112
    bfd_vma low2,
2113
    bfd_vma high2)
2114
4.61M
{
2115
4.61M
  if (low1 == low2 || high1 == high2)
2116
1.10M
    return true;
2117
2118
  /* Sort so that low1 is below low2. */
2119
3.50M
  if (low1 > low2)
2120
3.42M
    {
2121
3.42M
      bfd_vma tmp;
2122
2123
3.42M
      tmp = low1;
2124
3.42M
      low1 = low2;
2125
3.42M
      low2 = tmp;
2126
2127
3.42M
      tmp = high1;
2128
3.42M
      high1 = high2;
2129
3.42M
      high2 = tmp;
2130
3.42M
    }
2131
2132
  /* We touch iff low2 == high1.
2133
     We overlap iff low2 is within [low1, high1). */
2134
3.50M
  return low2 <= high1;
2135
4.61M
}
2136
2137
/* Insert an address range in the trie mapping addresses to compilation units.
2138
   Will return the new trie node (usually the same as is being sent in, but
2139
   in case of a leaf-to-interior conversion, or expansion of a leaf, it may be
2140
   different), or NULL on failure.  */
2141
2142
static struct trie_node *
2143
insert_arange_in_trie (bfd *abfd,
2144
           struct trie_node *trie,
2145
           bfd_vma trie_pc,
2146
           unsigned int trie_pc_bits,
2147
           struct comp_unit *unit,
2148
           bfd_vma low_pc,
2149
           bfd_vma high_pc)
2150
5.31M
{
2151
5.31M
  bfd_vma clamped_low_pc, clamped_high_pc;
2152
5.31M
  int ch, from_ch, to_ch;
2153
5.31M
  bool is_full_leaf = false;
2154
5.31M
  bool splitting_leaf_will_help = false;
2155
2156
  /* See if we can extend any of the existing ranges.  This merging
2157
     isn't perfect (if merging opens up the possibility of merging two existing
2158
     ranges, we won't find them), but it takes the majority of the cases.  */
2159
5.31M
  if (trie->num_room_in_leaf > 0)
2160
4.80M
    {
2161
4.80M
      struct trie_leaf *leaf = (struct trie_leaf *) trie;
2162
4.80M
      unsigned int i;
2163
2164
36.8M
      for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2165
36.5M
  {
2166
36.5M
    if (leaf->ranges[i].unit == unit
2167
36.5M
        && ranges_overlap (low_pc, high_pc,
2168
4.61M
         leaf->ranges[i].low_pc,
2169
4.61M
         leaf->ranges[i].high_pc))
2170
4.49M
      {
2171
4.49M
        if (low_pc < leaf->ranges[i].low_pc)
2172
89.9k
    leaf->ranges[i].low_pc = low_pc;
2173
4.49M
        if (high_pc > leaf->ranges[i].high_pc)
2174
339k
    leaf->ranges[i].high_pc = high_pc;
2175
4.49M
        return trie;
2176
4.49M
      }
2177
36.5M
  }
2178
2179
311k
      is_full_leaf = leaf->num_stored_in_leaf == trie->num_room_in_leaf;
2180
2181
311k
      if (is_full_leaf && trie_pc_bits < VMA_BITS)
2182
3.88k
  {
2183
    /* See if we have at least one leaf that does _not_ cover the
2184
       entire bucket, so that splitting will actually reduce the number
2185
       of elements in at least one of the child nodes.  (For simplicity,
2186
       we don't test the range we're inserting, but it will be counted
2187
       on the next insertion where we're full, if any.)   */
2188
3.88k
    bfd_vma bucket_high_pc =
2189
3.88k
      trie_pc + ((bfd_vma) -1 >> trie_pc_bits);  /* Inclusive.  */
2190
54.4k
    for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2191
51.3k
      {
2192
51.3k
        if (leaf->ranges[i].low_pc > trie_pc
2193
51.3k
      || leaf->ranges[i].high_pc <= bucket_high_pc)
2194
750
    {
2195
750
      splitting_leaf_will_help = true;
2196
750
      break;
2197
750
    }
2198
51.3k
      }
2199
3.88k
  }
2200
311k
    }
2201
2202
  /* If we're a leaf with no more room and we're _not_ at the bottom,
2203
     convert to an interior node.  */
2204
823k
  if (is_full_leaf && splitting_leaf_will_help)
2205
750
    {
2206
750
      const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2207
750
      unsigned int i;
2208
2209
750
      trie = bfd_zalloc (abfd, sizeof (struct trie_interior));
2210
750
      if (!trie)
2211
0
  return NULL;
2212
750
      is_full_leaf = false;
2213
2214
      /* TODO: If we wanted to save a little more memory at the cost of
2215
   complexity, we could have reused the old leaf node as one of the
2216
   children of the new interior node, instead of throwing it away.  */
2217
12.7k
      for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2218
12.0k
        {
2219
12.0k
    if (!insert_arange_in_trie (abfd, trie, trie_pc, trie_pc_bits,
2220
12.0k
              leaf->ranges[i].unit, leaf->ranges[i].low_pc,
2221
12.0k
              leaf->ranges[i].high_pc))
2222
0
      return NULL;
2223
12.0k
  }
2224
750
    }
2225
2226
  /* If we're a leaf with no more room and we _are_ at the bottom
2227
     (or splitting it won't help), we have no choice but to just
2228
     make it larger.  */
2229
823k
  if (is_full_leaf)
2230
5.79k
    {
2231
5.79k
      const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2232
5.79k
      unsigned int new_room_in_leaf = trie->num_room_in_leaf * 2;
2233
5.79k
      struct trie_leaf *new_leaf;
2234
5.79k
      size_t amt = sizeof (*leaf) + new_room_in_leaf * sizeof (leaf->ranges[0]);
2235
5.79k
      new_leaf = bfd_zalloc (abfd, amt);
2236
5.79k
      new_leaf->head.num_room_in_leaf = new_room_in_leaf;
2237
5.79k
      new_leaf->num_stored_in_leaf = leaf->num_stored_in_leaf;
2238
2239
5.79k
      memcpy (new_leaf->ranges,
2240
5.79k
        leaf->ranges,
2241
5.79k
        leaf->num_stored_in_leaf * sizeof (leaf->ranges[0]));
2242
5.79k
      trie = &new_leaf->head;
2243
5.79k
      is_full_leaf = false;
2244
2245
      /* Now the insert below will go through.  */
2246
5.79k
    }
2247
2248
  /* If we're a leaf (now with room), we can just insert at the end.  */
2249
823k
  if (trie->num_room_in_leaf > 0)
2250
311k
    {
2251
311k
      struct trie_leaf *leaf = (struct trie_leaf *) trie;
2252
2253
311k
      unsigned int i = leaf->num_stored_in_leaf++;
2254
311k
      leaf->ranges[i].unit = unit;
2255
311k
      leaf->ranges[i].low_pc = low_pc;
2256
311k
      leaf->ranges[i].high_pc = high_pc;
2257
311k
      return trie;
2258
311k
    }
2259
2260
  /* Now we are definitely an interior node, so recurse into all
2261
     the relevant buckets.  */
2262
2263
  /* Clamp the range to the current trie bucket.  */
2264
512k
  clamped_low_pc = low_pc;
2265
512k
  clamped_high_pc = high_pc;
2266
512k
  if (trie_pc_bits > 0)
2267
419k
    {
2268
419k
      bfd_vma bucket_high_pc =
2269
419k
  trie_pc + ((bfd_vma) -1 >> trie_pc_bits);  /* Inclusive.  */
2270
419k
      if (clamped_low_pc < trie_pc)
2271
2.06k
  clamped_low_pc = trie_pc;
2272
419k
      if (clamped_high_pc > bucket_high_pc)
2273
14.7k
  clamped_high_pc = bucket_high_pc;
2274
419k
    }
2275
2276
  /* Insert the ranges in all buckets that it spans.  */
2277
512k
  from_ch = (clamped_low_pc >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2278
512k
  to_ch = ((clamped_high_pc - 1) >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2279
5.24M
  for (ch = from_ch; ch <= to_ch; ++ch)
2280
4.73M
    {
2281
4.73M
      struct trie_interior *interior = (struct trie_interior *) trie;
2282
4.73M
      struct trie_node *child = interior->children[ch];
2283
2284
4.73M
      if (child == NULL)
2285
48.2k
        {
2286
48.2k
    child = alloc_trie_leaf (abfd);
2287
48.2k
    if (!child)
2288
0
      return NULL;
2289
48.2k
  }
2290
4.73M
      bfd_vma bucket = (bfd_vma) ch << (VMA_BITS - trie_pc_bits - 8);
2291
4.73M
      child = insert_arange_in_trie (abfd,
2292
4.73M
             child,
2293
4.73M
             trie_pc + bucket,
2294
4.73M
             trie_pc_bits + 8,
2295
4.73M
             unit,
2296
4.73M
             low_pc,
2297
4.73M
             high_pc);
2298
4.73M
      if (!child)
2299
0
  return NULL;
2300
2301
4.73M
      interior->children[ch] = child;
2302
4.73M
    }
2303
2304
512k
    return trie;
2305
512k
}
2306
2307
static bool
2308
arange_add (struct comp_unit *unit, struct arange *first_arange,
2309
      struct trie_node **trie_root, bfd_vma low_pc, bfd_vma high_pc)
2310
570k
{
2311
570k
  struct arange *arange;
2312
2313
  /* Ignore empty ranges.  */
2314
570k
  if (low_pc == high_pc)
2315
2.74k
    return true;
2316
2317
568k
  if (trie_root != NULL)
2318
568k
    {
2319
568k
      *trie_root = insert_arange_in_trie (unit->file->bfd_ptr,
2320
568k
            *trie_root,
2321
568k
            0,
2322
568k
            0,
2323
568k
            unit,
2324
568k
            low_pc,
2325
568k
            high_pc);
2326
568k
      if (*trie_root == NULL)
2327
0
  return false;
2328
568k
    }
2329
2330
  /* If the first arange is empty, use it.  */
2331
568k
  if (first_arange->high == 0)
2332
23.4k
    {
2333
23.4k
      first_arange->low = low_pc;
2334
23.4k
      first_arange->high = high_pc;
2335
23.4k
      return true;
2336
23.4k
    }
2337
2338
  /* Next see if we can cheaply extend an existing range.  */
2339
544k
  arange = first_arange;
2340
544k
  do
2341
217M
    {
2342
217M
      if (low_pc == arange->high)
2343
45.1k
  {
2344
45.1k
    arange->high = high_pc;
2345
45.1k
    return true;
2346
45.1k
  }
2347
217M
      if (high_pc == arange->low)
2348
53.1k
  {
2349
53.1k
    arange->low = low_pc;
2350
53.1k
    return true;
2351
53.1k
  }
2352
217M
      arange = arange->next;
2353
217M
    }
2354
217M
  while (arange);
2355
2356
  /* Need to allocate a new arange and insert it into the arange list.
2357
     Order isn't significant, so just insert after the first arange.  */
2358
446k
  arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
2359
446k
  if (arange == NULL)
2360
0
    return false;
2361
446k
  arange->low = low_pc;
2362
446k
  arange->high = high_pc;
2363
446k
  arange->next = first_arange->next;
2364
446k
  first_arange->next = arange;
2365
446k
  return true;
2366
446k
}
2367
2368
/* Compare function for line sequences.  */
2369
2370
static int
2371
compare_sequences (const void* a, const void* b)
2372
8.45k
{
2373
8.45k
  const struct line_sequence* seq1 = a;
2374
8.45k
  const struct line_sequence* seq2 = b;
2375
2376
  /* Sort by low_pc as the primary key.  */
2377
8.45k
  if (seq1->low_pc < seq2->low_pc)
2378
91
    return -1;
2379
8.36k
  if (seq1->low_pc > seq2->low_pc)
2380
7.41k
    return 1;
2381
2382
  /* If low_pc values are equal, sort in reverse order of
2383
     high_pc, so that the largest region comes first.  */
2384
949
  if (seq1->last_line->address < seq2->last_line->address)
2385
466
    return 1;
2386
483
  if (seq1->last_line->address > seq2->last_line->address)
2387
441
    return -1;
2388
2389
42
  if (seq1->last_line->op_index < seq2->last_line->op_index)
2390
5
    return 1;
2391
37
  if (seq1->last_line->op_index > seq2->last_line->op_index)
2392
13
    return -1;
2393
2394
  /* num_lines is initially an index, to make the sort stable.  */
2395
24
  if (seq1->num_lines < seq2->num_lines)
2396
24
    return -1;
2397
0
  if (seq1->num_lines > seq2->num_lines)
2398
0
    return 1;
2399
0
  return 0;
2400
0
}
2401
2402
/* Construct the line information table for quick lookup.  */
2403
2404
static bool
2405
build_line_info_table (struct line_info_table *  table,
2406
           struct line_sequence *    seq)
2407
6.80k
{
2408
6.80k
  size_t amt;
2409
6.80k
  struct line_info **line_info_lookup;
2410
6.80k
  struct line_info *each_line;
2411
6.80k
  unsigned int num_lines;
2412
6.80k
  unsigned int line_index;
2413
2414
6.80k
  if (seq->line_info_lookup != NULL)
2415
5.28k
    return true;
2416
2417
  /* Count the number of line information entries.  We could do this while
2418
     scanning the debug information, but some entries may be added via
2419
     lcl_head without having a sequence handy to increment the number of
2420
     lines.  */
2421
1.52k
  num_lines = 0;
2422
246k
  for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2423
245k
    num_lines++;
2424
2425
1.52k
  seq->num_lines = num_lines;
2426
1.52k
  if (num_lines == 0)
2427
0
    return true;
2428
2429
  /* Allocate space for the line information lookup table.  */
2430
1.52k
  amt = sizeof (struct line_info*) * num_lines;
2431
1.52k
  line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
2432
1.52k
  seq->line_info_lookup = line_info_lookup;
2433
1.52k
  if (line_info_lookup == NULL)
2434
0
    return false;
2435
2436
  /* Create the line information lookup table.  */
2437
1.52k
  line_index = num_lines;
2438
246k
  for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2439
245k
    line_info_lookup[--line_index] = each_line;
2440
2441
1.52k
  BFD_ASSERT (line_index == 0);
2442
1.52k
  return true;
2443
1.52k
}
2444
2445
/* Sort the line sequences for quick lookup.  */
2446
2447
static bool
2448
sort_line_sequences (struct line_info_table* table)
2449
1.51k
{
2450
1.51k
  size_t amt;
2451
1.51k
  struct line_sequence *sequences;
2452
1.51k
  struct line_sequence *seq;
2453
1.51k
  unsigned int n = 0;
2454
1.51k
  unsigned int num_sequences = table->num_sequences;
2455
1.51k
  bfd_vma last_high_pc;
2456
2457
1.51k
  if (num_sequences == 0)
2458
106
    return true;
2459
2460
  /* Allocate space for an array of sequences.  */
2461
1.40k
  amt = sizeof (struct line_sequence) * num_sequences;
2462
1.40k
  sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
2463
1.40k
  if (sequences == NULL)
2464
0
    return false;
2465
2466
  /* Copy the linked list into the array, freeing the original nodes.  */
2467
1.40k
  seq = table->sequences;
2468
5.71k
  for (n = 0; n < num_sequences; n++)
2469
4.30k
    {
2470
4.30k
      struct line_sequence* last_seq = seq;
2471
2472
4.30k
      BFD_ASSERT (seq);
2473
4.30k
      sequences[n].low_pc = seq->low_pc;
2474
4.30k
      sequences[n].prev_sequence = NULL;
2475
4.30k
      sequences[n].last_line = seq->last_line;
2476
4.30k
      sequences[n].line_info_lookup = NULL;
2477
4.30k
      sequences[n].num_lines = n;
2478
4.30k
      seq = seq->prev_sequence;
2479
4.30k
      free (last_seq);
2480
4.30k
    }
2481
1.40k
  BFD_ASSERT (seq == NULL);
2482
2483
1.40k
  qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
2484
2485
  /* Make the list binary-searchable by trimming overlapping entries
2486
     and removing nested entries.  */
2487
1.40k
  num_sequences = 1;
2488
1.40k
  last_high_pc = sequences[0].last_line->address;
2489
4.30k
  for (n = 1; n < table->num_sequences; n++)
2490
2.89k
    {
2491
2.89k
      if (sequences[n].low_pc < last_high_pc)
2492
296
  {
2493
296
    if (sequences[n].last_line->address <= last_high_pc)
2494
      /* Skip nested entries.  */
2495
281
      continue;
2496
2497
    /* Trim overlapping entries.  */
2498
15
    sequences[n].low_pc = last_high_pc;
2499
15
  }
2500
2.61k
      last_high_pc = sequences[n].last_line->address;
2501
2.61k
      if (n > num_sequences)
2502
83
  {
2503
    /* Close up the gap.  */
2504
83
    sequences[num_sequences].low_pc = sequences[n].low_pc;
2505
83
    sequences[num_sequences].last_line = sequences[n].last_line;
2506
83
  }
2507
2.61k
      num_sequences++;
2508
2.61k
    }
2509
2510
1.40k
  table->sequences = sequences;
2511
1.40k
  table->num_sequences = num_sequences;
2512
1.40k
  return true;
2513
1.40k
}
2514
2515
/* Add directory to TABLE.  CUR_DIR memory ownership is taken by TABLE.  */
2516
2517
static bool
2518
line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
2519
4.57k
{
2520
4.57k
  if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
2521
1.54k
    {
2522
1.54k
      char **tmp;
2523
1.54k
      size_t amt;
2524
2525
1.54k
      amt = table->num_dirs + DIR_ALLOC_CHUNK;
2526
1.54k
      amt *= sizeof (char *);
2527
2528
1.54k
      tmp = (char **) bfd_realloc (table->dirs, amt);
2529
1.54k
      if (tmp == NULL)
2530
0
  return false;
2531
1.54k
      table->dirs = tmp;
2532
1.54k
    }
2533
2534
4.57k
  table->dirs[table->num_dirs++] = cur_dir;
2535
4.57k
  return true;
2536
4.57k
}
2537
2538
static bool
2539
line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
2540
        unsigned int dir ATTRIBUTE_UNUSED,
2541
        unsigned int xtime ATTRIBUTE_UNUSED,
2542
        unsigned int size ATTRIBUTE_UNUSED)
2543
924
{
2544
924
  return line_info_add_include_dir (table, cur_dir);
2545
924
}
2546
2547
/* Add file to TABLE.  CUR_FILE memory ownership is taken by TABLE.  */
2548
2549
static bool
2550
line_info_add_file_name (struct line_info_table *table, char *cur_file,
2551
       unsigned int dir, unsigned int xtime,
2552
       unsigned int size)
2553
16.3k
{
2554
16.3k
  if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
2555
3.95k
    {
2556
3.95k
      struct fileinfo *tmp;
2557
3.95k
      size_t amt;
2558
2559
3.95k
      amt = table->num_files + FILE_ALLOC_CHUNK;
2560
3.95k
      amt *= sizeof (struct fileinfo);
2561
2562
3.95k
      tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
2563
3.95k
      if (tmp == NULL)
2564
0
  return false;
2565
3.95k
      table->files = tmp;
2566
3.95k
    }
2567
2568
16.3k
  table->files[table->num_files].name = cur_file;
2569
16.3k
  table->files[table->num_files].dir = dir;
2570
16.3k
  table->files[table->num_files].time = xtime;
2571
16.3k
  table->files[table->num_files].size = size;
2572
16.3k
  table->num_files++;
2573
16.3k
  return true;
2574
16.3k
}
2575
2576
/* Read directory or file name entry format, starting with byte of
2577
   format count entries, ULEB128 pairs of entry formats, ULEB128 of
2578
   entries count and the entries themselves in the described entry
2579
   format.  */
2580
2581
static bool
2582
read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
2583
      bfd_byte *buf_end, struct line_info_table *table,
2584
      bool (*callback) (struct line_info_table *table,
2585
            char *cur_file,
2586
            unsigned int dir,
2587
            unsigned int time,
2588
            unsigned int size))
2589
344
{
2590
344
  bfd *abfd = unit->abfd;
2591
344
  bfd_byte format_count, formati;
2592
344
  bfd_vma data_count, datai;
2593
344
  bfd_byte *buf = *bufp;
2594
344
  bfd_byte *format_header_data;
2595
2596
344
  format_count = read_1_byte (abfd, &buf, buf_end);
2597
344
  format_header_data = buf;
2598
4.66k
  for (formati = 0; formati < format_count; formati++)
2599
4.31k
    {
2600
4.31k
      _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2601
4.31k
      _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2602
4.31k
    }
2603
2604
344
  data_count = _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2605
344
  if (format_count == 0 && data_count != 0)
2606
1
    {
2607
1
      _bfd_error_handler (_("DWARF error: zero format count"));
2608
1
      bfd_set_error (bfd_error_bad_value);
2609
1
      return false;
2610
1
    }
2611
2612
  /* PR 22210.  Paranoia check.  Don't bother running the loop
2613
     if we know that we are going to run out of buffer.  */
2614
343
  if (data_count > (bfd_vma) (buf_end - buf))
2615
4
    {
2616
4
      _bfd_error_handler
2617
4
  (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
2618
4
   (uint64_t) data_count);
2619
4
      bfd_set_error (bfd_error_bad_value);
2620
4
      return false;
2621
4
    }
2622
2623
3.57k
  for (datai = 0; datai < data_count; datai++)
2624
3.24k
    {
2625
3.24k
      bfd_byte *format = format_header_data;
2626
3.24k
      struct fileinfo fe;
2627
2628
3.24k
      memset (&fe, 0, sizeof fe);
2629
9.00k
      for (formati = 0; formati < format_count; formati++)
2630
5.77k
  {
2631
5.77k
    bfd_vma content_type, form;
2632
5.77k
    char *string_trash;
2633
5.77k
    char **stringp = &string_trash;
2634
5.77k
    unsigned int uint_trash, *uintp = &uint_trash;
2635
5.77k
    struct attribute attr;
2636
2637
5.77k
    content_type = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2638
5.77k
    switch (content_type)
2639
5.77k
      {
2640
3.20k
      case DW_LNCT_path:
2641
3.20k
        stringp = &fe.name;
2642
3.20k
        break;
2643
2.30k
      case DW_LNCT_directory_index:
2644
2.30k
        uintp = &fe.dir;
2645
2.30k
        break;
2646
19
      case DW_LNCT_timestamp:
2647
19
        uintp = &fe.time;
2648
19
        break;
2649
29
      case DW_LNCT_size:
2650
29
        uintp = &fe.size;
2651
29
        break;
2652
199
      case DW_LNCT_MD5:
2653
199
        break;
2654
7
      default:
2655
7
        _bfd_error_handler
2656
7
    (_("DWARF error: unknown format content type %" PRIu64),
2657
7
     (uint64_t) content_type);
2658
7
        bfd_set_error (bfd_error_bad_value);
2659
7
        return false;
2660
5.77k
      }
2661
2662
5.76k
    form = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2663
5.76k
    buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
2664
5.76k
    if (buf == NULL)
2665
7
      return false;
2666
5.75k
    switch (form)
2667
5.75k
      {
2668
0
      case DW_FORM_string:
2669
3.12k
      case DW_FORM_line_strp:
2670
3.12k
      case DW_FORM_strx:
2671
3.12k
      case DW_FORM_strx1:
2672
3.12k
      case DW_FORM_strx2:
2673
3.15k
      case DW_FORM_strx3:
2674
3.15k
      case DW_FORM_strx4:
2675
3.15k
        *stringp = attr.u.str;
2676
3.15k
        break;
2677
2678
0
      case DW_FORM_data1:
2679
48
      case DW_FORM_data2:
2680
49
      case DW_FORM_data4:
2681
131
      case DW_FORM_data8:
2682
2.42k
      case DW_FORM_udata:
2683
2.42k
        *uintp = attr.u.val;
2684
2.42k
        break;
2685
2686
104
      case DW_FORM_data16:
2687
        /* MD5 data is in the attr.blk, but we are ignoring those.  */
2688
104
        break;
2689
5.75k
      }
2690
5.75k
  }
2691
2692
3.23k
      if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
2693
0
  return false;
2694
3.23k
    }
2695
2696
325
  *bufp = buf;
2697
325
  return true;
2698
339
}
2699
2700
/* Decode the line number information for UNIT.  */
2701
2702
static struct line_info_table*
2703
decode_line_info (struct comp_unit *unit)
2704
2.27k
{
2705
2.27k
  bfd *abfd = unit->abfd;
2706
2.27k
  struct dwarf2_debug *stash = unit->stash;
2707
2.27k
  struct dwarf2_debug_file *file = unit->file;
2708
2.27k
  struct line_info_table* table;
2709
2.27k
  bfd_byte *line_ptr;
2710
2.27k
  bfd_byte *line_end;
2711
2.27k
  struct line_head lh;
2712
2.27k
  unsigned int i, offset_size;
2713
2.27k
  char *cur_file, *cur_dir;
2714
2.27k
  unsigned char op_code, extended_op, adj_opcode;
2715
2.27k
  unsigned int exop_len;
2716
2.27k
  size_t amt;
2717
2718
2.27k
  if (unit->line_offset == 0 && file->line_table)
2719
28
    return file->line_table;
2720
2721
2.24k
  if (! read_section (abfd, &stash->debug_sections[debug_line],
2722
2.24k
          file->syms, unit->line_offset,
2723
2.24k
          &file->dwarf_line_buffer, &file->dwarf_line_size))
2724
24
    return NULL;
2725
2726
2.21k
  if (file->dwarf_line_size < 16)
2727
3
    {
2728
3
      _bfd_error_handler
2729
3
  (_("DWARF error: line info section is too small (%" PRId64 ")"),
2730
3
   (int64_t) file->dwarf_line_size);
2731
3
      bfd_set_error (bfd_error_bad_value);
2732
3
      return NULL;
2733
3
    }
2734
2.21k
  line_ptr = file->dwarf_line_buffer + unit->line_offset;
2735
2.21k
  line_end = file->dwarf_line_buffer + file->dwarf_line_size;
2736
2737
  /* Read in the prologue.  */
2738
2.21k
  lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2739
2.21k
  offset_size = 4;
2740
2.21k
  if (lh.total_length == 0xffffffff)
2741
2
    {
2742
2
      lh.total_length = read_8_bytes (abfd, &line_ptr, line_end);
2743
2
      offset_size = 8;
2744
2
    }
2745
2.21k
  else if (lh.total_length == 0 && unit->addr_size == 8)
2746
14
    {
2747
      /* Handle (non-standard) 64-bit DWARF2 formats.  */
2748
14
      lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2749
14
      offset_size = 8;
2750
14
    }
2751
2752
2.21k
  if (lh.total_length > (size_t) (line_end - line_ptr))
2753
433
    {
2754
433
      _bfd_error_handler
2755
  /* xgettext: c-format */
2756
433
  (_("DWARF error: line info data is bigger (%#" PRIx64 ")"
2757
433
     " than the space remaining in the section (%#lx)"),
2758
433
   (uint64_t) lh.total_length, (unsigned long) (line_end - line_ptr));
2759
433
      bfd_set_error (bfd_error_bad_value);
2760
433
      return NULL;
2761
433
    }
2762
2763
1.78k
  line_end = line_ptr + lh.total_length;
2764
2765
1.78k
  lh.version = read_2_bytes (abfd, &line_ptr, line_end);
2766
1.78k
  if (lh.version < 2 || lh.version > 5)
2767
126
    {
2768
126
      _bfd_error_handler
2769
126
  (_("DWARF error: unhandled .debug_line version %d"), lh.version);
2770
126
      bfd_set_error (bfd_error_bad_value);
2771
126
      return NULL;
2772
126
    }
2773
2774
1.65k
  if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
2775
1.65k
      >= line_end)
2776
2
    {
2777
2
      _bfd_error_handler
2778
2
  (_("DWARF error: ran out of room reading prologue"));
2779
2
      bfd_set_error (bfd_error_bad_value);
2780
2
      return NULL;
2781
2
    }
2782
2783
1.65k
  if (lh.version >= 5)
2784
176
    {
2785
176
      unsigned int segment_selector_size;
2786
2787
      /* Skip address size.  */
2788
176
      read_1_byte (abfd, &line_ptr, line_end);
2789
2790
176
      segment_selector_size = read_1_byte (abfd, &line_ptr, line_end);
2791
176
      if (segment_selector_size != 0)
2792
1
  {
2793
1
    _bfd_error_handler
2794
1
      (_("DWARF error: line info unsupported segment selector size %u"),
2795
1
       segment_selector_size);
2796
1
    bfd_set_error (bfd_error_bad_value);
2797
1
    return NULL;
2798
1
  }
2799
176
    }
2800
2801
1.65k
  if (offset_size == 4)
2802
1.64k
    lh.prologue_length = read_4_bytes (abfd, &line_ptr, line_end);
2803
6
  else
2804
6
    lh.prologue_length = read_8_bytes (abfd, &line_ptr, line_end);
2805
2806
1.65k
  lh.minimum_instruction_length = read_1_byte (abfd, &line_ptr, line_end);
2807
2808
1.65k
  if (lh.version >= 4)
2809
502
    lh.maximum_ops_per_insn = read_1_byte (abfd, &line_ptr, line_end);
2810
1.15k
  else
2811
1.15k
    lh.maximum_ops_per_insn = 1;
2812
2813
1.65k
  if (lh.maximum_ops_per_insn == 0)
2814
6
    {
2815
6
      _bfd_error_handler
2816
6
  (_("DWARF error: invalid maximum operations per instruction"));
2817
6
      bfd_set_error (bfd_error_bad_value);
2818
6
      return NULL;
2819
6
    }
2820
2821
1.64k
  lh.default_is_stmt = read_1_byte (abfd, &line_ptr, line_end);
2822
1.64k
  lh.line_base = read_1_signed_byte (abfd, &line_ptr, line_end);
2823
1.64k
  lh.line_range = read_1_byte (abfd, &line_ptr, line_end);
2824
1.64k
  lh.opcode_base = read_1_byte (abfd, &line_ptr, line_end);
2825
2826
1.64k
  if (line_ptr + (lh.opcode_base - 1) >= line_end)
2827
8
    {
2828
8
      _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2829
8
      bfd_set_error (bfd_error_bad_value);
2830
8
      return NULL;
2831
8
    }
2832
2833
1.64k
  amt = lh.opcode_base * sizeof (unsigned char);
2834
1.64k
  lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
2835
2836
1.64k
  lh.standard_opcode_lengths[0] = 1;
2837
2838
21.0k
  for (i = 1; i < lh.opcode_base; ++i)
2839
19.4k
    lh.standard_opcode_lengths[i] = read_1_byte (abfd, &line_ptr, line_end);
2840
2841
1.64k
  amt = sizeof (struct line_info_table);
2842
1.64k
  table = (struct line_info_table *) bfd_alloc (abfd, amt);
2843
1.64k
  if (table == NULL)
2844
0
    return NULL;
2845
1.64k
  table->abfd = abfd;
2846
1.64k
  table->comp_dir = unit->comp_dir;
2847
2848
1.64k
  table->num_files = 0;
2849
1.64k
  table->files = NULL;
2850
2851
1.64k
  table->num_dirs = 0;
2852
1.64k
  table->dirs = NULL;
2853
2854
1.64k
  table->num_sequences = 0;
2855
1.64k
  table->sequences = NULL;
2856
2857
1.64k
  table->lcl_head = NULL;
2858
2859
1.64k
  if (lh.version >= 5)
2860
175
    {
2861
      /* Read directory table.  */
2862
175
      if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2863
175
           line_info_add_include_dir_stub))
2864
6
  goto fail;
2865
2866
      /* Read file name table.  */
2867
169
      if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2868
169
           line_info_add_file_name))
2869
13
  goto fail;
2870
156
      table->use_dir_and_file_0 = true;
2871
156
    }
2872
1.46k
  else
2873
1.46k
    {
2874
      /* Read directory table.  */
2875
5.11k
      while ((cur_dir = read_string (&line_ptr, line_end)) != NULL)
2876
3.65k
  {
2877
3.65k
    if (!line_info_add_include_dir (table, cur_dir))
2878
0
      goto fail;
2879
3.65k
  }
2880
2881
      /* Read file name table.  */
2882
15.5k
      while ((cur_file = read_string (&line_ptr, line_end)) != NULL)
2883
14.0k
  {
2884
14.0k
    unsigned int dir, xtime, size;
2885
2886
14.0k
    dir = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2887
14.0k
    xtime = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2888
14.0k
    size = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2889
2890
14.0k
    if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
2891
0
      goto fail;
2892
14.0k
  }
2893
1.46k
      table->use_dir_and_file_0 = false;
2894
1.46k
    }
2895
2896
  /* Read the statement sequences until there's nothing left.  */
2897
6.07k
  while (line_ptr < line_end)
2898
4.56k
    {
2899
      /* State machine registers.  */
2900
4.56k
      bfd_vma address = 0;
2901
4.56k
      unsigned char op_index = 0;
2902
4.56k
      char * filename = NULL;
2903
4.56k
      unsigned int line = 1;
2904
4.56k
      unsigned int column = 0;
2905
4.56k
      unsigned int discriminator = 0;
2906
4.56k
      int is_stmt = lh.default_is_stmt;
2907
4.56k
      int end_sequence = 0;
2908
4.56k
      unsigned int dir, xtime, size;
2909
      /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2910
   compilers generate address sequences that are wildly out of
2911
   order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2912
   for ia64-Linux).  Thus, to determine the low and high
2913
   address, we must compare on every DW_LNS_copy, etc.  */
2914
4.56k
      bfd_vma low_pc  = (bfd_vma) -1;
2915
4.56k
      bfd_vma high_pc = 0;
2916
2917
4.56k
      if (table->num_files)
2918
4.51k
  {
2919
    /* PR 30783: Always start with a file index of 1, even
2920
       for DWARF-5.  */
2921
4.51k
    filename = concat_filename (table, 1);
2922
4.51k
  }
2923
2924
      /* Decode the table.  */
2925
1.59M
      while (!end_sequence && line_ptr < line_end)
2926
1.59M
  {
2927
1.59M
    op_code = read_1_byte (abfd, &line_ptr, line_end);
2928
2929
1.59M
    if (op_code >= lh.opcode_base)
2930
547k
      {
2931
        /* Special operand.  */
2932
547k
        adj_opcode = op_code - lh.opcode_base;
2933
547k
        if (lh.line_range == 0)
2934
5
    goto line_fail;
2935
547k
        if (lh.maximum_ops_per_insn == 1)
2936
538k
    address += (adj_opcode / lh.line_range
2937
538k
          * lh.minimum_instruction_length);
2938
8.73k
        else
2939
8.73k
    {
2940
8.73k
      address += ((op_index + adj_opcode / lh.line_range)
2941
8.73k
            / lh.maximum_ops_per_insn
2942
8.73k
            * lh.minimum_instruction_length);
2943
8.73k
      op_index = ((op_index + adj_opcode / lh.line_range)
2944
8.73k
            % lh.maximum_ops_per_insn);
2945
8.73k
    }
2946
547k
        line += lh.line_base + (adj_opcode % lh.line_range);
2947
        /* Append row to matrix using current values.  */
2948
547k
        if (!add_line_info (table, address, op_index, filename,
2949
547k
          line, column, discriminator, 0))
2950
0
    goto line_fail;
2951
547k
        discriminator = 0;
2952
547k
        if (address < low_pc)
2953
571
    low_pc = address;
2954
547k
        if (address > high_pc)
2955
525k
    high_pc = address;
2956
547k
      }
2957
1.04M
    else switch (op_code)
2958
1.04M
      {
2959
89.2k
      case DW_LNS_extended_op:
2960
89.2k
        exop_len = _bfd_safe_read_leb128 (abfd, &line_ptr,
2961
89.2k
            false, line_end);
2962
89.2k
        extended_op = read_1_byte (abfd, &line_ptr, line_end);
2963
2964
89.2k
        switch (extended_op)
2965
89.2k
    {
2966
4.38k
    case DW_LNE_end_sequence:
2967
4.38k
      end_sequence = 1;
2968
4.38k
      if (!add_line_info (table, address, op_index, filename, line,
2969
4.38k
              column, discriminator, end_sequence))
2970
0
        goto line_fail;
2971
4.38k
      discriminator = 0;
2972
4.38k
      if (address < low_pc)
2973
81
        low_pc = address;
2974
4.38k
      if (address > high_pc)
2975
4.31k
        high_pc = address;
2976
4.38k
      if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
2977
4.38k
           low_pc, high_pc))
2978
0
        goto line_fail;
2979
4.38k
      break;
2980
80.7k
    case DW_LNE_set_address:
2981
80.7k
      address = read_address (unit, &line_ptr, line_end);
2982
80.7k
      op_index = 0;
2983
80.7k
      break;
2984
26
    case DW_LNE_define_file:
2985
26
      cur_file = read_string (&line_ptr, line_end);
2986
26
      dir = _bfd_safe_read_leb128 (abfd, &line_ptr,
2987
26
                 false, line_end);
2988
26
      xtime = _bfd_safe_read_leb128 (abfd, &line_ptr,
2989
26
             false, line_end);
2990
26
      size = _bfd_safe_read_leb128 (abfd, &line_ptr,
2991
26
            false, line_end);
2992
26
      if (!line_info_add_file_name (table, cur_file, dir,
2993
26
            xtime, size))
2994
0
        goto line_fail;
2995
26
      break;
2996
3.98k
    case DW_LNE_set_discriminator:
2997
3.98k
      discriminator = _bfd_safe_read_leb128 (abfd, &line_ptr,
2998
3.98k
               false, line_end);
2999
3.98k
      break;
3000
10
    case DW_LNE_HP_source_file_correlation:
3001
10
      line_ptr += exop_len - 1;
3002
10
      break;
3003
101
    default:
3004
101
      _bfd_error_handler
3005
101
        (_("DWARF error: mangled line number section"));
3006
101
      bfd_set_error (bfd_error_bad_value);
3007
106
    line_fail:
3008
106
      free (filename);
3009
106
      goto fail;
3010
89.2k
    }
3011
89.1k
        break;
3012
89.1k
      case DW_LNS_copy:
3013
30.9k
        if (!add_line_info (table, address, op_index,
3014
30.9k
          filename, line, column, discriminator, 0))
3015
0
    goto line_fail;
3016
30.9k
        discriminator = 0;
3017
30.9k
        if (address < low_pc)
3018
3.91k
    low_pc = address;
3019
30.9k
        if (address > high_pc)
3020
25.9k
    high_pc = address;
3021
30.9k
        break;
3022
27.1k
      case DW_LNS_advance_pc:
3023
27.1k
        if (lh.maximum_ops_per_insn == 1)
3024
27.0k
    address += (lh.minimum_instruction_length
3025
27.0k
          * _bfd_safe_read_leb128 (abfd, &line_ptr,
3026
27.0k
                 false, line_end));
3027
126
        else
3028
126
    {
3029
126
      bfd_vma adjust = _bfd_safe_read_leb128 (abfd, &line_ptr,
3030
126
                false, line_end);
3031
126
      address = ((op_index + adjust) / lh.maximum_ops_per_insn
3032
126
           * lh.minimum_instruction_length);
3033
126
      op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
3034
126
    }
3035
27.1k
        break;
3036
239k
      case DW_LNS_advance_line:
3037
239k
        line += _bfd_safe_read_leb128 (abfd, &line_ptr,
3038
239k
               true, line_end);
3039
239k
        break;
3040
29.4k
      case DW_LNS_set_file:
3041
29.4k
        {
3042
29.4k
    unsigned int filenum;
3043
3044
    /* The file and directory tables are 0
3045
       based, the references are 1 based.  */
3046
29.4k
    filenum = _bfd_safe_read_leb128 (abfd, &line_ptr,
3047
29.4k
             false, line_end);
3048
29.4k
    free (filename);
3049
29.4k
    filename = concat_filename (table, filenum);
3050
29.4k
    break;
3051
30.9k
        }
3052
307k
      case DW_LNS_set_column:
3053
307k
        column = _bfd_safe_read_leb128 (abfd, &line_ptr,
3054
307k
                false, line_end);
3055
307k
        break;
3056
193k
      case DW_LNS_negate_stmt:
3057
193k
        is_stmt = (!is_stmt);
3058
193k
        break;
3059
51
      case DW_LNS_set_basic_block:
3060
51
        break;
3061
125k
      case DW_LNS_const_add_pc:
3062
125k
        if (lh.line_range == 0)
3063
0
    goto line_fail;
3064
125k
        if (lh.maximum_ops_per_insn == 1)
3065
124k
    address += (lh.minimum_instruction_length
3066
124k
          * ((255 - lh.opcode_base) / lh.line_range));
3067
574
        else
3068
574
    {
3069
574
      bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
3070
574
      address += (lh.minimum_instruction_length
3071
574
            * ((op_index + adjust)
3072
574
         / lh.maximum_ops_per_insn));
3073
574
      op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
3074
574
    }
3075
125k
        break;
3076
24
      case DW_LNS_fixed_advance_pc:
3077
24
        address += read_2_bytes (abfd, &line_ptr, line_end);
3078
24
        op_index = 0;
3079
24
        break;
3080
3.39k
      default:
3081
        /* Unknown standard opcode, ignore it.  */
3082
9.68k
        for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
3083
6.29k
    (void) _bfd_safe_read_leb128 (abfd, &line_ptr,
3084
6.29k
                false, line_end);
3085
3.39k
        break;
3086
1.04M
      }
3087
1.59M
  }
3088
3089
4.45k
      free (filename);
3090
4.45k
    }
3091
3092
1.51k
  if (unit->line_offset == 0)
3093
327
    file->line_table = table;
3094
1.51k
  if (sort_line_sequences (table))
3095
1.51k
    return table;
3096
3097
125
 fail:
3098
344
  while (table->sequences != NULL)
3099
219
    {
3100
219
      struct line_sequence* seq = table->sequences;
3101
219
      table->sequences = table->sequences->prev_sequence;
3102
219
      free (seq);
3103
219
    }
3104
125
  free (table->files);
3105
125
  free (table->dirs);
3106
125
  return NULL;
3107
1.51k
}
3108
3109
/* If ADDR is within TABLE set the output parameters and return TRUE,
3110
   otherwise set *FILENAME_PTR to NULL and return FALSE.
3111
   The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
3112
   are pointers to the objects to be filled in.  */
3113
3114
static bool
3115
lookup_address_in_line_info_table (struct line_info_table *table,
3116
           bfd_vma addr,
3117
           const char **filename_ptr,
3118
           unsigned int *linenumber_ptr,
3119
           unsigned int *discriminator_ptr)
3120
11.9k
{
3121
11.9k
  struct line_sequence *seq = NULL;
3122
11.9k
  struct line_info *info;
3123
11.9k
  int low, high, mid;
3124
3125
  /* Binary search the array of sequences.  */
3126
11.9k
  low = 0;
3127
11.9k
  high = table->num_sequences;
3128
33.5k
  while (low < high)
3129
28.4k
    {
3130
28.4k
      mid = (low + high) / 2;
3131
28.4k
      seq = &table->sequences[mid];
3132
28.4k
      if (addr < seq->low_pc)
3133
10.5k
  high = mid;
3134
17.9k
      else if (addr >= seq->last_line->address)
3135
11.1k
  low = mid + 1;
3136
6.80k
      else
3137
6.80k
  break;
3138
28.4k
    }
3139
3140
  /* Check for a valid sequence.  */
3141
11.9k
  if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
3142
5.10k
    goto fail;
3143
3144
6.80k
  if (!build_line_info_table (table, seq))
3145
0
    goto fail;
3146
3147
  /* Binary search the array of line information.  */
3148
6.80k
  low = 0;
3149
6.80k
  high = seq->num_lines;
3150
6.80k
  info = NULL;
3151
48.2k
  while (low < high)
3152
48.2k
    {
3153
48.2k
      mid = (low + high) / 2;
3154
48.2k
      info = seq->line_info_lookup[mid];
3155
48.2k
      if (addr < info->address)
3156
27.8k
  high = mid;
3157
20.4k
      else if (addr >= seq->line_info_lookup[mid + 1]->address)
3158
13.6k
  low = mid + 1;
3159
6.80k
      else
3160
6.80k
  break;
3161
48.2k
    }
3162
3163
  /* Check for a valid line information entry.  */
3164
6.80k
  if (info
3165
6.80k
      && addr >= info->address
3166
6.80k
      && addr < seq->line_info_lookup[mid + 1]->address
3167
6.80k
      && !(info->end_sequence || info == seq->last_line))
3168
6.80k
    {
3169
6.80k
      *filename_ptr = info->filename;
3170
6.80k
      *linenumber_ptr = info->line;
3171
6.80k
      if (discriminator_ptr)
3172
63
  *discriminator_ptr = info->discriminator;
3173
6.80k
      return true;
3174
6.80k
    }
3175
3176
5.10k
 fail:
3177
5.10k
  *filename_ptr = NULL;
3178
5.10k
  return false;
3179
6.80k
}
3180
3181
/* Read in the .debug_ranges section for future reference.  */
3182
3183
static bool
3184
read_debug_ranges (struct comp_unit * unit)
3185
268
{
3186
268
  struct dwarf2_debug *stash = unit->stash;
3187
268
  struct dwarf2_debug_file *file = unit->file;
3188
3189
268
  return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
3190
268
           file->syms, 0,
3191
268
           &file->dwarf_ranges_buffer, &file->dwarf_ranges_size);
3192
268
}
3193
3194
/* Read in the .debug_rnglists section for future reference.  */
3195
3196
static bool
3197
read_debug_rnglists (struct comp_unit * unit)
3198
0
{
3199
0
  struct dwarf2_debug *stash = unit->stash;
3200
0
  struct dwarf2_debug_file *file = unit->file;
3201
3202
0
  return read_section (unit->abfd, &stash->debug_sections[debug_rnglists],
3203
0
           file->syms, 0,
3204
0
           &file->dwarf_rnglists_buffer, &file->dwarf_rnglists_size);
3205
0
}
3206
3207
/* Function table functions.  */
3208
3209
static int
3210
compare_lookup_funcinfos (const void * a, const void * b)
3211
110k
{
3212
110k
  const struct lookup_funcinfo * lookup1 = a;
3213
110k
  const struct lookup_funcinfo * lookup2 = b;
3214
3215
110k
  if (lookup1->low_addr < lookup2->low_addr)
3216
56.8k
    return -1;
3217
54.1k
  if (lookup1->low_addr > lookup2->low_addr)
3218
23.4k
    return 1;
3219
30.6k
  if (lookup1->high_addr < lookup2->high_addr)
3220
566
    return -1;
3221
30.1k
  if (lookup1->high_addr > lookup2->high_addr)
3222
4.55k
    return 1;
3223
3224
25.5k
  if (lookup1->idx < lookup2->idx)
3225
25.5k
    return -1;
3226
0
  if (lookup1->idx > lookup2->idx)
3227
0
    return 1;
3228
0
  return 0;
3229
0
}
3230
3231
static bool
3232
build_lookup_funcinfo_table (struct comp_unit * unit)
3233
11.1k
{
3234
11.1k
  struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
3235
11.1k
  unsigned int number_of_functions = unit->number_of_functions;
3236
11.1k
  struct funcinfo *each;
3237
11.1k
  struct lookup_funcinfo *entry;
3238
11.1k
  size_t func_index;
3239
11.1k
  struct arange *range;
3240
11.1k
  bfd_vma low_addr, high_addr;
3241
3242
11.1k
  if (lookup_funcinfo_table || number_of_functions == 0)
3243
10.7k
    return true;
3244
3245
  /* Create the function info lookup table.  */
3246
406
  lookup_funcinfo_table = (struct lookup_funcinfo *)
3247
406
    bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
3248
406
  if (lookup_funcinfo_table == NULL)
3249
0
    return false;
3250
3251
  /* Populate the function info lookup table.  */
3252
406
  func_index = number_of_functions;
3253
23.5k
  for (each = unit->function_table; each; each = each->prev_func)
3254
23.1k
    {
3255
23.1k
      entry = &lookup_funcinfo_table[--func_index];
3256
23.1k
      entry->funcinfo = each;
3257
23.1k
      entry->idx = func_index;
3258
3259
      /* Calculate the lowest and highest address for this function entry.  */
3260
23.1k
      low_addr  = entry->funcinfo->arange.low;
3261
23.1k
      high_addr = entry->funcinfo->arange.high;
3262
3263
416k
      for (range = entry->funcinfo->arange.next; range; range = range->next)
3264
393k
  {
3265
393k
    if (range->low < low_addr)
3266
2.29k
      low_addr = range->low;
3267
393k
    if (range->high > high_addr)
3268
10.2k
      high_addr = range->high;
3269
393k
  }
3270
3271
23.1k
      entry->low_addr = low_addr;
3272
23.1k
      entry->high_addr = high_addr;
3273
23.1k
    }
3274
3275
406
  BFD_ASSERT (func_index == 0);
3276
3277
  /* Sort the function by address.  */
3278
406
  qsort (lookup_funcinfo_table,
3279
406
   number_of_functions,
3280
406
   sizeof (struct lookup_funcinfo),
3281
406
   compare_lookup_funcinfos);
3282
3283
  /* Calculate the high watermark for each function in the lookup table.  */
3284
406
  high_addr = lookup_funcinfo_table[0].high_addr;
3285
23.1k
  for (func_index = 1; func_index < number_of_functions; func_index++)
3286
22.7k
    {
3287
22.7k
      entry = &lookup_funcinfo_table[func_index];
3288
22.7k
      if (entry->high_addr > high_addr)
3289
2.71k
  high_addr = entry->high_addr;
3290
20.0k
      else
3291
20.0k
  entry->high_addr = high_addr;
3292
22.7k
    }
3293
3294
406
  unit->lookup_funcinfo_table = lookup_funcinfo_table;
3295
406
  return true;
3296
406
}
3297
3298
/* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
3299
   TRUE.  Note that we need to find the function that has the smallest range
3300
   that contains ADDR, to handle inlined functions without depending upon
3301
   them being ordered in TABLE by increasing range.  */
3302
3303
static bool
3304
lookup_address_in_function_table (struct comp_unit *unit,
3305
          bfd_vma addr,
3306
          struct funcinfo **function_ptr)
3307
11.9k
{
3308
11.9k
  unsigned int number_of_functions = unit->number_of_functions;
3309
11.9k
  struct lookup_funcinfo* lookup_funcinfo = NULL;
3310
11.9k
  struct funcinfo* funcinfo = NULL;
3311
11.9k
  struct funcinfo* best_fit = NULL;
3312
11.9k
  bfd_vma best_fit_len = (bfd_vma) -1;
3313
11.9k
  bfd_size_type low, high, mid, first;
3314
11.9k
  struct arange *arange;
3315
3316
11.9k
  if (number_of_functions == 0)
3317
774
    return false;
3318
3319
11.1k
  if (!build_lookup_funcinfo_table (unit))
3320
0
    return false;
3321
3322
11.1k
  if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
3323
2.55k
    return false;
3324
3325
  /* Find the first function in the lookup table which may contain the
3326
     specified address.  */
3327
8.58k
  low = 0;
3328
8.58k
  high = number_of_functions;
3329
8.58k
  first = high;
3330
63.1k
  while (low < high)
3331
54.5k
    {
3332
54.5k
      mid = (low + high) / 2;
3333
54.5k
      lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
3334
54.5k
      if (addr < lookup_funcinfo->low_addr)
3335
12.4k
  high = mid;
3336
42.1k
      else if (addr >= lookup_funcinfo->high_addr)
3337
26.7k
  low = mid + 1;
3338
15.4k
      else
3339
15.4k
  high = first = mid;
3340
54.5k
    }
3341
3342
  /* Find the 'best' match for the address.  The prior algorithm defined the
3343
     best match as the function with the smallest address range containing
3344
     the specified address.  This definition should probably be changed to the
3345
     innermost inline routine containing the address, but right now we want
3346
     to get the same results we did before.  */
3347
247k
  while (first < number_of_functions)
3348
243k
    {
3349
243k
      if (addr < unit->lookup_funcinfo_table[first].low_addr)
3350
4.99k
  break;
3351
238k
      funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
3352
3353
65.6M
      for (arange = &funcinfo->arange; arange; arange = arange->next)
3354
65.4M
  {
3355
65.4M
    if (addr < arange->low || addr >= arange->high)
3356
63.2M
      continue;
3357
3358
2.21M
    if (arange->high - arange->low < best_fit_len
3359
        /* The following comparison is designed to return the same
3360
     match as the previous algorithm for routines which have the
3361
     same best fit length.  */
3362
2.21M
        || (arange->high - arange->low == best_fit_len
3363
2.20M
      && funcinfo > best_fit))
3364
52.8k
      {
3365
52.8k
        best_fit = funcinfo;
3366
52.8k
        best_fit_len = arange->high - arange->low;
3367
52.8k
      }
3368
2.21M
  }
3369
3370
238k
      first++;
3371
238k
    }
3372
3373
8.58k
  if (!best_fit)
3374
1.24k
    return false;
3375
3376
7.34k
  *function_ptr = best_fit;
3377
7.34k
  return true;
3378
8.58k
}
3379
3380
/* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
3381
   and LINENUMBER_PTR, and return TRUE.  */
3382
3383
static bool
3384
lookup_symbol_in_function_table (struct comp_unit *unit,
3385
         asymbol *sym,
3386
         bfd_vma addr,
3387
         const char **filename_ptr,
3388
         unsigned int *linenumber_ptr)
3389
25.4k
{
3390
25.4k
  struct funcinfo* each;
3391
25.4k
  struct funcinfo* best_fit = NULL;
3392
25.4k
  bfd_vma best_fit_len = (bfd_vma) -1;
3393
25.4k
  struct arange *arange;
3394
25.4k
  const char *name = bfd_asymbol_name (sym);
3395
3396
849k
  for (each = unit->function_table; each; each = each->prev_func)
3397
34.5M
    for (arange = &each->arange; arange; arange = arange->next)
3398
33.7M
      if (addr >= arange->low
3399
33.7M
    && addr < arange->high
3400
33.7M
    && arange->high - arange->low < best_fit_len
3401
33.7M
    && each->file
3402
33.7M
    && each->name
3403
33.7M
    && strstr (name, each->name) != NULL)
3404
1.78k
  {
3405
1.78k
    best_fit = each;
3406
1.78k
    best_fit_len = arange->high - arange->low;
3407
1.78k
  }
3408
3409
25.4k
  if (best_fit)
3410
1.57k
    {
3411
1.57k
      *filename_ptr = best_fit->file;
3412
1.57k
      *linenumber_ptr = best_fit->line;
3413
1.57k
      return true;
3414
1.57k
    }
3415
3416
23.8k
  return false;
3417
25.4k
}
3418
3419
/* Variable table functions.  */
3420
3421
/* If SYM is within variable table of UNIT, set FILENAME_PTR and
3422
   LINENUMBER_PTR, and return TRUE.  */
3423
3424
static bool
3425
lookup_symbol_in_variable_table (struct comp_unit *unit,
3426
         asymbol *sym,
3427
         bfd_vma addr,
3428
         const char **filename_ptr,
3429
         unsigned int *linenumber_ptr)
3430
256k
{
3431
256k
  struct varinfo* each;
3432
256k
  const char *name = bfd_asymbol_name (sym);
3433
3434
8.04M
  for (each = unit->variable_table; each; each = each->prev_var)
3435
7.78M
    if (each->addr == addr
3436
7.78M
  && !each->stack
3437
7.78M
  && each->file != NULL
3438
7.78M
  && each->name != NULL
3439
7.78M
  && strstr (name, each->name) != NULL)
3440
550
      break;
3441
3442
256k
  if (each)
3443
550
    {
3444
550
      *filename_ptr = each->file;
3445
550
      *linenumber_ptr = each->line;
3446
550
      return true;
3447
550
    }
3448
3449
256k
  return false;
3450
256k
}
3451
3452
static struct comp_unit *stash_comp_unit (struct dwarf2_debug *,
3453
            struct dwarf2_debug_file *);
3454
static bool comp_unit_maybe_decode_line_info (struct comp_unit *);
3455
3456
static bool
3457
find_abstract_instance (struct comp_unit *unit,
3458
      struct attribute *attr_ptr,
3459
      unsigned int recur_count,
3460
      const char **pname,
3461
      bool *is_linkage,
3462
      char **filename_ptr,
3463
      int *linenumber_ptr)
3464
16.0k
{
3465
16.0k
  bfd *abfd = unit->abfd;
3466
16.0k
  bfd_byte *info_ptr = NULL;
3467
16.0k
  bfd_byte *info_ptr_end;
3468
16.0k
  unsigned int abbrev_number, i;
3469
16.0k
  struct abbrev_info *abbrev;
3470
16.0k
  uint64_t die_ref = attr_ptr->u.val;
3471
16.0k
  struct attribute attr;
3472
3473
16.0k
  if (recur_count == 100)
3474
0
    {
3475
0
      _bfd_error_handler
3476
0
  (_("DWARF error: abstract instance recursion detected"));
3477
0
      bfd_set_error (bfd_error_bad_value);
3478
0
      return false;
3479
0
    }
3480
3481
  /* DW_FORM_ref_addr can reference an entry in a different CU. It
3482
     is an offset from the .debug_info section, not the current CU.  */
3483
16.0k
  if (attr_ptr->form == DW_FORM_ref_addr)
3484
0
    {
3485
      /* We only support DW_FORM_ref_addr within the same file, so
3486
   any relocations should be resolved already.  Check this by
3487
   testing for a zero die_ref;  There can't be a valid reference
3488
   to the header of a .debug_info section.
3489
   DW_FORM_ref_addr is an offset relative to .debug_info.
3490
   Normally when using the GNU linker this is accomplished by
3491
   emitting a symbolic reference to a label, because .debug_info
3492
   sections are linked at zero.  When there are multiple section
3493
   groups containing .debug_info, as there might be in a
3494
   relocatable object file, it would be reasonable to assume that
3495
   a symbolic reference to a label in any .debug_info section
3496
   might be used.  Since we lay out multiple .debug_info
3497
   sections at non-zero VMAs (see place_sections), and read
3498
   them contiguously into dwarf_info_buffer, that means the
3499
   reference is relative to dwarf_info_buffer.  */
3500
0
      size_t total;
3501
3502
0
      info_ptr = unit->file->dwarf_info_buffer;
3503
0
      info_ptr_end = info_ptr + unit->file->dwarf_info_size;
3504
0
      total = info_ptr_end - info_ptr;
3505
0
      if (!die_ref)
3506
0
  return true;
3507
0
      else if (die_ref >= total)
3508
0
  {
3509
0
    _bfd_error_handler
3510
0
      (_("DWARF error: invalid abstract instance DIE ref"));
3511
0
    bfd_set_error (bfd_error_bad_value);
3512
0
    return false;
3513
0
  }
3514
0
      info_ptr += die_ref;
3515
0
    }
3516
16.0k
  else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3517
0
    {
3518
0
      bool first_time = unit->stash->alt.dwarf_info_buffer == NULL;
3519
3520
0
      info_ptr = read_alt_indirect_ref (unit, die_ref);
3521
0
      if (first_time)
3522
0
  unit->stash->alt.info_ptr = unit->stash->alt.dwarf_info_buffer;
3523
0
      if (info_ptr == NULL)
3524
0
  {
3525
0
    _bfd_error_handler
3526
0
      (_("DWARF error: unable to read alt ref %" PRIu64),
3527
0
       (uint64_t) die_ref);
3528
0
    bfd_set_error (bfd_error_bad_value);
3529
0
    return false;
3530
0
  }
3531
0
      info_ptr_end = (unit->stash->alt.dwarf_info_buffer
3532
0
          + unit->stash->alt.dwarf_info_size);
3533
0
      if (unit->stash->alt.all_comp_units)
3534
0
  unit = unit->stash->alt.all_comp_units;
3535
0
    }
3536
3537
16.0k
  if (attr_ptr->form == DW_FORM_ref_addr
3538
16.0k
      || attr_ptr->form == DW_FORM_GNU_ref_alt)
3539
0
    {
3540
      /* Now find the CU containing this pointer.  */
3541
0
      if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
3542
0
  info_ptr_end = unit->end_ptr;
3543
0
      else
3544
0
  {
3545
    /* Check other CUs to see if they contain the abbrev.  */
3546
0
    struct comp_unit *u = NULL;
3547
0
    struct addr_range range = { info_ptr, info_ptr };
3548
0
    splay_tree_node v = splay_tree_lookup (unit->file->comp_unit_tree,
3549
0
             (splay_tree_key)&range);
3550
0
    if (v != NULL)
3551
0
      u = (struct comp_unit *)v->value;
3552
3553
0
    if (attr_ptr->form == DW_FORM_ref_addr)
3554
0
      while (u == NULL)
3555
0
        {
3556
0
    u = stash_comp_unit (unit->stash, &unit->stash->f);
3557
0
    if (u == NULL)
3558
0
      break;
3559
0
    if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3560
0
      break;
3561
0
    u = NULL;
3562
0
        }
3563
3564
0
    if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3565
0
      while (u == NULL)
3566
0
        {
3567
0
    u = stash_comp_unit (unit->stash, &unit->stash->alt);
3568
0
    if (u == NULL)
3569
0
      break;
3570
0
    if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3571
0
      break;
3572
0
    u = NULL;
3573
0
        }
3574
3575
0
    if (u == NULL)
3576
0
      {
3577
0
        _bfd_error_handler
3578
0
    (_("DWARF error: unable to locate abstract instance DIE ref %"
3579
0
       PRIu64), (uint64_t) die_ref);
3580
0
        bfd_set_error (bfd_error_bad_value);
3581
0
        return false;
3582
0
      }
3583
0
    unit = u;
3584
0
    info_ptr_end = unit->end_ptr;
3585
0
  }
3586
0
    }
3587
16.0k
  else
3588
16.0k
    {
3589
      /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3590
   DW_FORM_ref_udata.  These are all references relative to the
3591
   start of the current CU.  */
3592
16.0k
      size_t total;
3593
3594
16.0k
      info_ptr = unit->info_ptr_unit;
3595
16.0k
      info_ptr_end = unit->end_ptr;
3596
16.0k
      total = info_ptr_end - info_ptr;
3597
16.0k
      if (!die_ref || die_ref >= total)
3598
13
  {
3599
13
    _bfd_error_handler
3600
13
      (_("DWARF error: invalid abstract instance DIE ref"));
3601
13
    bfd_set_error (bfd_error_bad_value);
3602
13
    return false;
3603
13
  }
3604
16.0k
      info_ptr += die_ref;
3605
16.0k
    }
3606
3607
16.0k
  abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3608
16.0k
           false, info_ptr_end);
3609
16.0k
  if (abbrev_number)
3610
15.9k
    {
3611
15.9k
      abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3612
15.9k
      if (! abbrev)
3613
2
  {
3614
2
    _bfd_error_handler
3615
2
      (_("DWARF error: could not find abbrev number %u"), abbrev_number);
3616
2
    bfd_set_error (bfd_error_bad_value);
3617
2
    return false;
3618
2
  }
3619
15.9k
      else
3620
15.9k
  {
3621
71.7k
    for (i = 0; i < abbrev->num_attrs; ++i)
3622
55.7k
      {
3623
55.7k
        info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
3624
55.7k
           info_ptr, info_ptr_end);
3625
55.7k
        if (info_ptr == NULL)
3626
0
    break;
3627
55.7k
        switch (attr.name)
3628
55.7k
    {
3629
15.9k
    case DW_AT_name:
3630
      /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3631
         over DW_AT_name.  */
3632
15.9k
      if (*pname == NULL && is_str_form (&attr))
3633
15.9k
        {
3634
15.9k
          *pname = attr.u.str;
3635
15.9k
          if (mangle_style (unit->lang) == 0)
3636
15.9k
      *is_linkage = true;
3637
15.9k
        }
3638
15.9k
      break;
3639
0
    case DW_AT_specification:
3640
0
      if (is_int_form (&attr)
3641
0
          && !find_abstract_instance (unit, &attr, recur_count + 1,
3642
0
              pname, is_linkage,
3643
0
              filename_ptr, linenumber_ptr))
3644
0
        return false;
3645
0
      break;
3646
2
    case DW_AT_linkage_name:
3647
2
    case DW_AT_MIPS_linkage_name:
3648
      /* PR 16949:  Corrupt debug info can place
3649
         non-string forms into these attributes.  */
3650
2
      if (is_str_form (&attr))
3651
0
        {
3652
0
          *pname = attr.u.str;
3653
0
          *is_linkage = true;
3654
0
        }
3655
2
      break;
3656
4.82k
    case DW_AT_decl_file:
3657
4.82k
      if (!comp_unit_maybe_decode_line_info (unit))
3658
0
        return false;
3659
4.82k
      if (is_int_form (&attr))
3660
4.82k
        {
3661
4.82k
          free (*filename_ptr);
3662
4.82k
          *filename_ptr = concat_filename (unit->line_table,
3663
4.82k
                   attr.u.val);
3664
4.82k
        }
3665
4.82k
      break;
3666
4.82k
    case DW_AT_decl_line:
3667
4.82k
      if (is_int_form (&attr))
3668
4.82k
        *linenumber_ptr = attr.u.val;
3669
4.82k
      break;
3670
30.1k
    default:
3671
30.1k
      break;
3672
55.7k
    }
3673
55.7k
      }
3674
15.9k
  }
3675
15.9k
    }
3676
16.0k
  return true;
3677
16.0k
}
3678
3679
static bool
3680
read_ranges (struct comp_unit *unit, struct arange *arange,
3681
       struct trie_node **trie_root, uint64_t offset)
3682
10.6k
{
3683
10.6k
  bfd_byte *ranges_ptr;
3684
10.6k
  bfd_byte *ranges_end;
3685
10.6k
  bfd_vma base_address = unit->base_address;
3686
3687
10.6k
  if (! unit->file->dwarf_ranges_buffer)
3688
268
    {
3689
268
      if (! read_debug_ranges (unit))
3690
12
  return false;
3691
268
    }
3692
3693
10.5k
  if (offset > unit->file->dwarf_ranges_size)
3694
5
    return false;
3695
10.5k
  ranges_ptr = unit->file->dwarf_ranges_buffer + offset;
3696
10.5k
  ranges_end = unit->file->dwarf_ranges_buffer + unit->file->dwarf_ranges_size;
3697
3698
10.5k
  for (;;)
3699
563k
    {
3700
563k
      bfd_vma low_pc;
3701
563k
      bfd_vma high_pc;
3702
3703
      /* PR 17512: file: 62cada7d.  */
3704
563k
      if (2u * unit->addr_size > (size_t) (ranges_end - ranges_ptr))
3705
43
  return false;
3706
3707
563k
      low_pc = read_address (unit, &ranges_ptr, ranges_end);
3708
563k
      high_pc = read_address (unit, &ranges_ptr, ranges_end);
3709
3710
563k
      if (low_pc == 0 && high_pc == 0)
3711
10.5k
  break;
3712
553k
      if (low_pc == (bfd_vma) -1 && high_pc != (bfd_vma) -1)
3713
5
  base_address = high_pc;
3714
552k
      else
3715
552k
  {
3716
552k
    if (!arange_add (unit, arange, trie_root,
3717
552k
         base_address + low_pc, base_address + high_pc))
3718
0
      return false;
3719
552k
  }
3720
553k
    }
3721
10.5k
  return true;
3722
10.5k
}
3723
3724
static bool
3725
read_rnglists (struct comp_unit *unit, struct arange *arange,
3726
         struct trie_node **trie_root, uint64_t offset)
3727
0
{
3728
0
  bfd_byte *rngs_ptr;
3729
0
  bfd_byte *rngs_end;
3730
0
  bfd_vma base_address = unit->base_address;
3731
0
  bfd_vma low_pc;
3732
0
  bfd_vma high_pc;
3733
0
  bfd *abfd = unit->abfd;
3734
3735
0
  if (! unit->file->dwarf_rnglists_buffer)
3736
0
    {
3737
0
      if (! read_debug_rnglists (unit))
3738
0
  return false;
3739
0
    }
3740
3741
0
  rngs_ptr = unit->file->dwarf_rnglists_buffer + offset;
3742
0
  if (rngs_ptr < unit->file->dwarf_rnglists_buffer)
3743
0
    return false;
3744
0
  rngs_end = unit->file->dwarf_rnglists_buffer;
3745
0
  rngs_end +=  unit->file->dwarf_rnglists_size;
3746
3747
0
  for (;;)
3748
0
    {
3749
0
      enum dwarf_range_list_entry rlet;
3750
3751
0
      if (rngs_ptr >= rngs_end)
3752
0
  return false;
3753
3754
0
      rlet = read_1_byte (abfd, &rngs_ptr, rngs_end);
3755
3756
0
      switch (rlet)
3757
0
  {
3758
0
  case DW_RLE_end_of_list:
3759
0
    return true;
3760
3761
0
  case DW_RLE_base_address:
3762
0
    if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3763
0
      return false;
3764
0
    base_address = read_address (unit, &rngs_ptr, rngs_end);
3765
0
    continue;
3766
3767
0
  case DW_RLE_start_length:
3768
0
    if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3769
0
      return false;
3770
0
    low_pc = read_address (unit, &rngs_ptr, rngs_end);
3771
0
    high_pc = low_pc;
3772
0
    high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3773
0
              false, rngs_end);
3774
0
    break;
3775
3776
0
  case DW_RLE_offset_pair:
3777
0
    low_pc = base_address;
3778
0
    low_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3779
0
             false, rngs_end);
3780
0
    high_pc = base_address;
3781
0
    high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3782
0
              false, rngs_end);
3783
0
    break;
3784
3785
0
  case DW_RLE_start_end:
3786
0
    if (2u * unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3787
0
      return false;
3788
0
    low_pc = read_address (unit, &rngs_ptr, rngs_end);
3789
0
    high_pc = read_address (unit, &rngs_ptr, rngs_end);
3790
0
    break;
3791
3792
  /* TODO x-variants need .debug_addr support used for split-dwarf.  */
3793
0
  case DW_RLE_base_addressx:
3794
0
  case DW_RLE_startx_endx:
3795
0
  case DW_RLE_startx_length:
3796
0
  default:
3797
0
    return false;
3798
0
  }
3799
3800
0
      if (!arange_add (unit, arange, trie_root, low_pc, high_pc))
3801
0
  return false;
3802
0
    }
3803
0
}
3804
3805
static bool
3806
read_rangelist (struct comp_unit *unit, struct arange *arange,
3807
    struct trie_node **trie_root, uint64_t offset)
3808
10.6k
{
3809
10.6k
  if (unit->version <= 4)
3810
10.6k
    return read_ranges (unit, arange, trie_root, offset);
3811
0
  else
3812
0
    return read_rnglists (unit, arange, trie_root, offset);
3813
10.6k
}
3814
3815
static struct funcinfo *
3816
lookup_func_by_offset (uint64_t offset, struct funcinfo * table)
3817
1.18k
{
3818
1.18k
  for (; table != NULL; table = table->prev_func)
3819
1.18k
    if (table->unit_offset == offset)
3820
1.18k
      return table;
3821
0
  return NULL;
3822
1.18k
}
3823
3824
static struct varinfo *
3825
lookup_var_by_offset (uint64_t offset, struct varinfo * table)
3826
1.13k
{
3827
1.13k
  while (table)
3828
1.13k
    {
3829
1.13k
      if (table->unit_offset == offset)
3830
1.13k
  return table;
3831
0
      table = table->prev_var;
3832
0
    }
3833
3834
0
  return NULL;
3835
1.13k
}
3836
3837
3838
/* DWARF2 Compilation unit functions.  */
3839
3840
static struct funcinfo *
3841
reverse_funcinfo_list (struct funcinfo *head)
3842
4.12k
{
3843
4.12k
  struct funcinfo *rhead;
3844
4.12k
  struct funcinfo *temp;
3845
3846
95.4k
  for (rhead = NULL; head; head = temp)
3847
91.3k
    {
3848
91.3k
      temp = head->prev_func;
3849
91.3k
      head->prev_func = rhead;
3850
91.3k
      rhead = head;
3851
91.3k
    }
3852
4.12k
  return rhead;
3853
4.12k
}
3854
3855
static struct varinfo *
3856
reverse_varinfo_list (struct varinfo *head)
3857
4.12k
{
3858
4.12k
  struct varinfo *rhead;
3859
4.12k
  struct varinfo *temp;
3860
3861
132k
  for (rhead = NULL; head; head = temp)
3862
128k
    {
3863
128k
      temp = head->prev_var;
3864
128k
      head->prev_var = rhead;
3865
128k
      rhead = head;
3866
128k
    }
3867
4.12k
  return rhead;
3868
4.12k
}
3869
3870
/* Scan over each die in a comp. unit looking for functions to add
3871
   to the function table and variables to the variable table.  */
3872
3873
static bool
3874
scan_unit_for_symbols (struct comp_unit *unit)
3875
1.52k
{
3876
1.52k
  bfd *abfd = unit->abfd;
3877
1.52k
  bfd_byte *info_ptr = unit->first_child_die_ptr;
3878
1.52k
  bfd_byte *info_ptr_end = unit->end_ptr;
3879
1.52k
  int nesting_level = 0;
3880
1.52k
  struct nest_funcinfo
3881
1.52k
  {
3882
1.52k
    struct funcinfo *func;
3883
1.52k
  } *nested_funcs;
3884
1.52k
  int nested_funcs_size;
3885
1.52k
  struct funcinfo *last_func;
3886
1.52k
  struct varinfo *last_var;
3887
  
3888
  /* Maintain a stack of in-scope functions and inlined functions, which we
3889
     can use to set the caller_func field.  */
3890
1.52k
  nested_funcs_size = 32;
3891
1.52k
  nested_funcs = (struct nest_funcinfo *)
3892
1.52k
    bfd_malloc (nested_funcs_size * sizeof (*nested_funcs));
3893
1.52k
  if (nested_funcs == NULL)
3894
0
    return false;
3895
1.52k
  nested_funcs[nesting_level].func = 0;
3896
3897
  /* PR 27484: We must scan the DIEs twice.  The first time we look for
3898
     function and variable tags and accumulate them into their respective
3899
     tables.  The second time through we process the attributes of the
3900
     functions/variables and augment the table entries.  */
3901
198k
  while (nesting_level >= 0)
3902
196k
    {
3903
196k
      unsigned int abbrev_number, i;
3904
196k
      struct abbrev_info *abbrev;
3905
196k
      struct funcinfo *func;
3906
196k
      struct varinfo *var;
3907
196k
      uint64_t current_offset;
3908
3909
      /* PR 17512: file: 9f405d9d.  */
3910
196k
      if (info_ptr >= info_ptr_end)
3911
3
  goto fail;
3912
3913
196k
      current_offset = info_ptr - unit->info_ptr_unit;
3914
196k
      abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3915
196k
               false, info_ptr_end);
3916
196k
      if (abbrev_number == 0)
3917
30.9k
  {
3918
30.9k
    nesting_level--;
3919
30.9k
    continue;
3920
30.9k
  }
3921
3922
165k
      abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3923
165k
      if (! abbrev)
3924
55
  {
3925
55
    static unsigned int previous_failed_abbrev = -1U;
3926
3927
    /* Avoid multiple reports of the same missing abbrev.  */
3928
55
    if (abbrev_number != previous_failed_abbrev)
3929
52
      {
3930
52
        _bfd_error_handler
3931
52
    (_("DWARF error: could not find abbrev number %u"),
3932
52
     abbrev_number);
3933
52
        previous_failed_abbrev = abbrev_number;
3934
52
      }
3935
55
    bfd_set_error (bfd_error_bad_value);
3936
55
    goto fail;
3937
55
  }
3938
3939
165k
      if (abbrev->tag == DW_TAG_subprogram
3940
165k
    || abbrev->tag == DW_TAG_entry_point
3941
165k
    || abbrev->tag == DW_TAG_inlined_subroutine)
3942
31.9k
  {
3943
31.9k
    size_t amt = sizeof (struct funcinfo);
3944
3945
31.9k
    var = NULL;
3946
31.9k
    func = (struct funcinfo *) bfd_zalloc (abfd, amt);
3947
31.9k
    if (func == NULL)
3948
0
      goto fail;
3949
31.9k
    func->tag = abbrev->tag;
3950
31.9k
    func->prev_func = unit->function_table;
3951
31.9k
    func->unit_offset = current_offset;
3952
31.9k
    unit->function_table = func;
3953
31.9k
    unit->number_of_functions++;
3954
31.9k
    BFD_ASSERT (!unit->cached);
3955
3956
31.9k
    if (func->tag == DW_TAG_inlined_subroutine)
3957
20.6k
      for (i = nesting_level; i-- != 0; )
3958
20.5k
        if (nested_funcs[i].func)
3959
17.1k
    {
3960
17.1k
      func->caller_func = nested_funcs[i].func;
3961
17.1k
      break;
3962
17.1k
    }
3963
31.9k
    nested_funcs[nesting_level].func = func;
3964
31.9k
  }
3965
133k
      else
3966
133k
  {
3967
133k
    func = NULL;
3968
133k
    if (abbrev->tag == DW_TAG_variable
3969
133k
        || abbrev->tag == DW_TAG_member)
3970
41.8k
      {
3971
41.8k
        size_t amt = sizeof (struct varinfo);
3972
3973
41.8k
        var = (struct varinfo *) bfd_zalloc (abfd, amt);
3974
41.8k
        if (var == NULL)
3975
0
    goto fail;
3976
41.8k
        var->tag = abbrev->tag;
3977
41.8k
        var->stack = true;
3978
41.8k
        var->prev_var = unit->variable_table;
3979
41.8k
        unit->variable_table = var;
3980
41.8k
        var->unit_offset = current_offset;
3981
        /* PR 18205: Missing debug information can cause this
3982
     var to be attached to an already cached unit.  */
3983
41.8k
      }
3984
91.8k
    else
3985
91.8k
      var = NULL;
3986
3987
    /* No inline function in scope at this nesting level.  */
3988
133k
    nested_funcs[nesting_level].func = 0;
3989
133k
  }
3990
3991
759k
      for (i = 0; i < abbrev->num_attrs; ++i)
3992
593k
  {
3993
593k
    struct attribute attr;
3994
3995
593k
    info_ptr = read_attribute (&attr, &abbrev->attrs[i],
3996
593k
             unit, info_ptr, info_ptr_end);
3997
593k
    if (info_ptr == NULL)
3998
26
      goto fail;
3999
593k
  }
4000
4001
165k
      if (abbrev->has_children)
4002
29.6k
  {
4003
29.6k
    nesting_level++;
4004
4005
29.6k
    if (nesting_level >= nested_funcs_size)
4006
2
      {
4007
2
        struct nest_funcinfo *tmp;
4008
4009
2
        nested_funcs_size *= 2;
4010
2
        tmp = (struct nest_funcinfo *)
4011
2
    bfd_realloc (nested_funcs,
4012
2
           nested_funcs_size * sizeof (*nested_funcs));
4013
2
        if (tmp == NULL)
4014
0
    goto fail;
4015
2
        nested_funcs = tmp;
4016
2
      }
4017
29.6k
    nested_funcs[nesting_level].func = 0;
4018
29.6k
  }
4019
165k
    }
4020
4021
1.44k
  unit->function_table = reverse_funcinfo_list (unit->function_table);
4022
1.44k
  unit->variable_table = reverse_varinfo_list (unit->variable_table);
4023
4024
  /* This is the second pass over the abbrevs.  */      
4025
1.44k
  info_ptr = unit->first_child_die_ptr;
4026
1.44k
  nesting_level = 0;
4027
  
4028
1.44k
  last_func = NULL;
4029
1.44k
  last_var = NULL;
4030
4031
188k
  while (nesting_level >= 0)
4032
186k
    {
4033
186k
      unsigned int abbrev_number, i;
4034
186k
      struct abbrev_info *abbrev;
4035
186k
      struct attribute attr;
4036
186k
      struct funcinfo *func;
4037
186k
      struct varinfo *var;
4038
186k
      bfd_vma low_pc = 0;
4039
186k
      bfd_vma high_pc = 0;
4040
186k
      bool high_pc_relative = false;
4041
186k
      uint64_t current_offset;
4042
4043
      /* PR 17512: file: 9f405d9d.  */
4044
186k
      if (info_ptr >= info_ptr_end)
4045
0
  goto fail;
4046
4047
186k
      current_offset = info_ptr - unit->info_ptr_unit;
4048
186k
      abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
4049
186k
               false, info_ptr_end);
4050
186k
      if (! abbrev_number)
4051
29.3k
  {
4052
29.3k
    nesting_level--;
4053
29.3k
    continue;
4054
29.3k
  }
4055
4056
157k
      abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
4057
      /* This should have been handled above.  */
4058
157k
      BFD_ASSERT (abbrev != NULL);
4059
4060
157k
      func = NULL;
4061
157k
      var = NULL;
4062
157k
      if (abbrev->tag == DW_TAG_subprogram
4063
157k
    || abbrev->tag == DW_TAG_entry_point
4064
157k
    || abbrev->tag == DW_TAG_inlined_subroutine)
4065
29.8k
  {
4066
29.8k
    if (last_func
4067
29.8k
        && last_func->prev_func
4068
29.8k
        && last_func->prev_func->unit_offset == current_offset)
4069
28.6k
      func = last_func->prev_func;
4070
1.18k
    else
4071
1.18k
      func = lookup_func_by_offset (current_offset, unit->function_table);
4072
4073
29.8k
    if (func == NULL)
4074
0
      goto fail;
4075
4076
29.8k
    last_func = func;
4077
29.8k
  }
4078
127k
      else if (abbrev->tag == DW_TAG_variable
4079
127k
         || abbrev->tag == DW_TAG_member)
4080
39.7k
  {
4081
39.7k
    if (last_var
4082
39.7k
        && last_var->prev_var
4083
39.7k
        && last_var->prev_var->unit_offset == current_offset)
4084
38.6k
      var = last_var->prev_var;
4085
1.13k
    else
4086
1.13k
      var = lookup_var_by_offset (current_offset, unit->variable_table);
4087
4088
39.7k
    if (var == NULL)
4089
0
      goto fail;
4090
4091
39.7k
    last_var = var;
4092
39.7k
  }
4093
4094
721k
      for (i = 0; i < abbrev->num_attrs; ++i)
4095
564k
  {
4096
564k
    info_ptr = read_attribute (&attr, &abbrev->attrs[i],
4097
564k
             unit, info_ptr, info_ptr_end);
4098
564k
    if (info_ptr == NULL)
4099
0
      goto fail;
4100
4101
564k
    if (func)
4102
162k
      {
4103
162k
        switch (attr.name)
4104
162k
    {
4105
14.1k
    case DW_AT_call_file:
4106
14.1k
      if (is_int_form (&attr))
4107
14.1k
        {
4108
14.1k
          free (func->caller_file);
4109
14.1k
          func->caller_file = concat_filename (unit->line_table,
4110
14.1k
                 attr.u.val);
4111
14.1k
        }
4112
14.1k
      break;
4113
4114
14.1k
    case DW_AT_call_line:
4115
14.1k
      if (is_int_form (&attr))
4116
14.1k
        func->caller_line = attr.u.val;
4117
14.1k
      break;
4118
4119
16.0k
    case DW_AT_abstract_origin:
4120
16.0k
    case DW_AT_specification:
4121
16.0k
      if (is_int_form (&attr)
4122
16.0k
          && !find_abstract_instance (unit, &attr, 0,
4123
15.9k
              &func->name,
4124
15.9k
              &func->is_linkage,
4125
15.9k
              &func->file,
4126
15.9k
              &func->line))
4127
14
        goto fail;
4128
15.9k
      break;
4129
4130
15.9k
    case DW_AT_name:
4131
      /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
4132
         over DW_AT_name.  */
4133
13.4k
      if (func->name == NULL && is_str_form (&attr))
4134
13.4k
        {
4135
13.4k
          func->name = attr.u.str;
4136
13.4k
          if (mangle_style (unit->lang) == 0)
4137
13.3k
      func->is_linkage = true;
4138
13.4k
        }
4139
13.4k
      break;
4140
4141
9
    case DW_AT_linkage_name:
4142
9
    case DW_AT_MIPS_linkage_name:
4143
      /* PR 16949:  Corrupt debug info can place
4144
         non-string forms into these attributes.  */
4145
9
      if (is_str_form (&attr))
4146
9
        {
4147
9
          func->name = attr.u.str;
4148
9
          func->is_linkage = true;
4149
9
        }
4150
9
      break;
4151
4152
11.5k
    case DW_AT_low_pc:
4153
11.5k
      if (is_int_form (&attr))
4154
11.4k
        low_pc = attr.u.val;
4155
11.5k
      break;
4156
4157
11.5k
    case DW_AT_high_pc:
4158
11.5k
      if (is_int_form (&attr))
4159
11.5k
        {
4160
11.5k
          high_pc = attr.u.val;
4161
11.5k
          high_pc_relative = attr.form != DW_FORM_addr;
4162
11.5k
        }
4163
11.5k
      break;
4164
4165
10.3k
    case DW_AT_ranges:
4166
10.3k
      if (is_int_form (&attr)
4167
10.3k
          && !read_rangelist (unit, &func->arange,
4168
10.3k
            &unit->file->trie_root, attr.u.val))
4169
48
        goto fail;
4170
10.3k
      break;
4171
4172
10.3k
    case DW_AT_decl_file:
4173
9.38k
      if (is_int_form (&attr))
4174
9.38k
        {
4175
9.38k
          free (func->file);
4176
9.38k
          func->file = concat_filename (unit->line_table,
4177
9.38k
                attr.u.val);
4178
9.38k
        }
4179
9.38k
      break;
4180
4181
9.36k
    case DW_AT_decl_line:
4182
9.36k
      if (is_int_form (&attr))
4183
9.36k
        func->line = attr.u.val;
4184
9.36k
      break;
4185
4186
52.4k
    default:
4187
52.4k
      break;
4188
162k
    }
4189
162k
      }
4190
402k
    else if (var)
4191
178k
      {
4192
178k
        switch (attr.name)
4193
178k
    {
4194
22
    case DW_AT_specification:
4195
22
      if (is_int_form (&attr) && attr.u.val)
4196
21
        {
4197
21
          bool is_linkage;
4198
21
          if (!find_abstract_instance (unit, &attr, 0,
4199
21
               &var->name,
4200
21
               &is_linkage,
4201
21
               &var->file,
4202
21
               &var->line))
4203
1
      {
4204
1
        _bfd_error_handler (_("DWARF error: could not find "
4205
1
            "variable specification "
4206
1
            "at offset 0x%lx"),
4207
1
                (unsigned long) attr.u.val);
4208
1
        break;
4209
1
      }
4210
21
        }
4211
21
      break;
4212
4213
33.9k
    case DW_AT_name:
4214
33.9k
      if (is_str_form (&attr))
4215
33.9k
        var->name = attr.u.str;
4216
33.9k
      break;
4217
4218
33.8k
    case DW_AT_decl_file:
4219
33.8k
      if (is_int_form (&attr))
4220
33.8k
        {
4221
33.8k
          free (var->file);
4222
33.8k
          var->file = concat_filename (unit->line_table,
4223
33.8k
               attr.u.val);
4224
33.8k
        }
4225
33.8k
      break;
4226
4227
33.8k
    case DW_AT_decl_line:
4228
33.8k
      if (is_int_form (&attr))
4229
33.8k
        var->line = attr.u.val;
4230
33.8k
      break;
4231
4232
1.12k
    case DW_AT_external:
4233
1.12k
      if (is_int_form (&attr) && attr.u.val != 0)
4234
1.12k
        var->stack = false;
4235
1.12k
      break;
4236
4237
17.4k
    case DW_AT_location:
4238
17.4k
      switch (attr.form)
4239
17.4k
        {
4240
0
        case DW_FORM_block:
4241
10.4k
        case DW_FORM_block1:
4242
10.4k
        case DW_FORM_block2:
4243
10.4k
        case DW_FORM_block4:
4244
10.4k
        case DW_FORM_exprloc:
4245
10.4k
          if (attr.u.blk->data != NULL
4246
10.4k
        && *attr.u.blk->data == DW_OP_addr)
4247
2.60k
      {
4248
2.60k
        var->stack = false;
4249
4250
        /* Verify that DW_OP_addr is the only opcode in the
4251
           location, in which case the block size will be 1
4252
           plus the address size.  */
4253
        /* ??? For TLS variables, gcc can emit
4254
           DW_OP_addr <addr> DW_OP_GNU_push_tls_address
4255
           which we don't handle here yet.  */
4256
2.60k
        if (attr.u.blk->size == unit->addr_size + 1U)
4257
2.60k
          var->addr = bfd_get (unit->addr_size * 8,
4258
2.60k
             unit->abfd,
4259
2.60k
             attr.u.blk->data + 1);
4260
2.60k
      }
4261
10.4k
          break;
4262
4263
10.4k
        default:
4264
6.93k
          break;
4265
17.4k
        }
4266
17.4k
      break;
4267
4268
58.2k
    default:
4269
58.2k
      break;
4270
178k
    }
4271
178k
      }
4272
564k
  }
4273
4274
157k
      if (abbrev->has_children)
4275
28.1k
  nesting_level++;
4276
4277
157k
      if (high_pc_relative)
4278
4.48k
  high_pc += low_pc;
4279
4280
157k
      if (func && high_pc != 0)
4281
11.5k
  {
4282
11.5k
    if (!arange_add (unit, &func->arange, &unit->file->trie_root,
4283
11.5k
         low_pc, high_pc))
4284
0
      goto fail;
4285
11.5k
  }
4286
157k
    }
4287
4288
1.38k
  unit->function_table = reverse_funcinfo_list (unit->function_table);
4289
1.38k
  unit->variable_table = reverse_varinfo_list (unit->variable_table);
4290
4291
1.38k
  free (nested_funcs);
4292
1.38k
  return true;
4293
4294
146
 fail:
4295
146
  free (nested_funcs);
4296
146
  return false;
4297
1.44k
}
4298
4299
/* Read the attributes of the form strx and addrx.  */
4300
4301
static void
4302
reread_attribute (struct comp_unit *unit,
4303
      struct attribute *attr,
4304
      bfd_vma *low_pc,
4305
      bfd_vma *high_pc,
4306
      bool *high_pc_relative,
4307
      bool compunit)
4308
159
{
4309
159
  if (is_strx_form (attr->form))
4310
112
    attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
4311
159
  if (is_addrx_form (attr->form))
4312
47
    attr->u.val = read_indexed_address (attr->u.val, unit);
4313
4314
159
  switch (attr->name)
4315
159
    {
4316
5
    case DW_AT_stmt_list:
4317
5
      unit->stmtlist = 1;
4318
5
      unit->line_offset = attr->u.val;
4319
5
      break;
4320
4321
47
    case DW_AT_name:
4322
47
      if (is_str_form (attr))
4323
47
  unit->name = attr->u.str;
4324
47
      break;
4325
4326
25
    case DW_AT_low_pc:
4327
25
      *low_pc = attr->u.val;
4328
25
      if (compunit)
4329
21
  unit->base_address = *low_pc;
4330
25
      break;
4331
4332
6
    case DW_AT_high_pc:
4333
6
      *high_pc = attr->u.val;
4334
6
      *high_pc_relative = attr->form != DW_FORM_addr;
4335
6
      break;
4336
4337
3
    case DW_AT_ranges:
4338
3
      if (!read_rangelist (unit, &unit->arange,
4339
3
         &unit->file->trie_root, attr->u.val))
4340
0
  return;
4341
3
      break;
4342
4343
28
    case DW_AT_comp_dir:
4344
28
      {
4345
28
  char *comp_dir = attr->u.str;
4346
4347
28
  if (!is_str_form (attr))
4348
18
    {
4349
18
      _bfd_error_handler
4350
18
        (_("DWARF error: DW_AT_comp_dir attribute encountered "
4351
18
     "with a non-string form"));
4352
18
      comp_dir = NULL;
4353
18
    }
4354
4355
28
  if (comp_dir)
4356
4
    {
4357
4
      char *cp = strchr (comp_dir, ':');
4358
4359
4
      if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4360
0
        comp_dir = cp + 1;
4361
4
    }
4362
28
  unit->comp_dir = comp_dir;
4363
28
  break;
4364
3
      }
4365
4366
11
    case DW_AT_language:
4367
11
      unit->lang = attr->u.val;
4368
45
    default:
4369
45
      break;
4370
159
    }
4371
159
}
4372
4373
/* Parse a DWARF2 compilation unit starting at INFO_PTR.  UNIT_LENGTH
4374
   includes the compilation unit header that proceeds the DIE's, but
4375
   does not include the length field that precedes each compilation
4376
   unit header.  END_PTR points one past the end of this comp unit.
4377
   OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
4378
4379
   This routine does not read the whole compilation unit; only enough
4380
   to get to the line number information for the compilation unit.  */
4381
4382
static struct comp_unit *
4383
parse_comp_unit (struct dwarf2_debug *stash,
4384
     struct dwarf2_debug_file *file,
4385
     bfd_byte *info_ptr,
4386
     bfd_vma unit_length,
4387
     bfd_byte *info_ptr_unit,
4388
     unsigned int offset_size)
4389
2.39k
{
4390
2.39k
  struct comp_unit* unit;
4391
2.39k
  unsigned int version;
4392
2.39k
  uint64_t abbrev_offset = 0;
4393
  /* Initialize it just to avoid a GCC false warning.  */
4394
2.39k
  unsigned int addr_size = -1;
4395
2.39k
  struct abbrev_info** abbrevs;
4396
2.39k
  unsigned int abbrev_number, i;
4397
2.39k
  struct abbrev_info *abbrev;
4398
2.39k
  struct attribute attr;
4399
2.39k
  bfd_byte *end_ptr = info_ptr + unit_length;
4400
2.39k
  size_t amt;
4401
2.39k
  bfd_vma low_pc = 0;
4402
2.39k
  bfd_vma high_pc = 0;
4403
2.39k
  bfd *abfd = file->bfd_ptr;
4404
2.39k
  bool high_pc_relative = false;
4405
2.39k
  enum dwarf_unit_type unit_type;
4406
2.39k
  struct attribute *str_addrp = NULL;
4407
2.39k
  size_t str_count = 0;
4408
2.39k
  size_t str_alloc = 0;
4409
2.39k
  bool compunit_flag = false;
4410
4411
2.39k
  version = read_2_bytes (abfd, &info_ptr, end_ptr);
4412
2.39k
  if (version < 2 || version > 5)
4413
35
    {
4414
      /* PR 19872: A version number of 0 probably means that there is padding
4415
   at the end of the .debug_info section.  Gold puts it there when
4416
   performing an incremental link, for example.  So do not generate
4417
   an error, just return a NULL.  */
4418
35
      if (version)
4419
26
  {
4420
26
    _bfd_error_handler
4421
26
      (_("DWARF error: found dwarf version '%u', this reader"
4422
26
         " only handles version 2, 3, 4 and 5 information"), version);
4423
26
    bfd_set_error (bfd_error_bad_value);
4424
26
  }
4425
35
      return NULL;
4426
35
    }
4427
4428
2.35k
  if (version < 5)
4429
2.20k
    unit_type = DW_UT_compile;
4430
159
  else
4431
159
    {
4432
159
      unit_type = read_1_byte (abfd, &info_ptr, end_ptr);
4433
159
      addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4434
159
    }
4435
4436
2.35k
  BFD_ASSERT (offset_size == 4 || offset_size == 8);
4437
2.35k
  if (offset_size == 4)
4438
2.35k
    abbrev_offset = read_4_bytes (abfd, &info_ptr, end_ptr);
4439
2
  else
4440
2
    abbrev_offset = read_8_bytes (abfd, &info_ptr, end_ptr);
4441
4442
2.35k
  if (version < 5)
4443
2.20k
    addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4444
4445
2.35k
  switch (unit_type)
4446
2.35k
    {
4447
72
    case DW_UT_type:
4448
      /* Skip type signature.  */
4449
72
      info_ptr += 8;
4450
4451
      /* Skip type offset.  */
4452
72
      info_ptr += offset_size;
4453
72
      break;
4454
4455
0
    case DW_UT_skeleton:
4456
      /* Skip DWO_id field.  */
4457
0
      info_ptr += 8;
4458
0
      break;
4459
4460
2.28k
    default:
4461
2.28k
      break;
4462
2.35k
    }
4463
4464
2.35k
  if (addr_size > sizeof (bfd_vma))
4465
3
    {
4466
3
      _bfd_error_handler
4467
  /* xgettext: c-format */
4468
3
  (_("DWARF error: found address size '%u', this reader"
4469
3
     " can not handle sizes greater than '%u'"),
4470
3
   addr_size,
4471
3
   (unsigned int) sizeof (bfd_vma));
4472
3
      bfd_set_error (bfd_error_bad_value);
4473
3
      return NULL;
4474
3
    }
4475
4476
2.35k
  if (addr_size != 2 && addr_size != 4 && addr_size != 8)
4477
3
    {
4478
3
      _bfd_error_handler
4479
3
  ("DWARF error: found address size '%u', this reader"
4480
3
   " can only handle address sizes '2', '4' and '8'", addr_size);
4481
3
      bfd_set_error (bfd_error_bad_value);
4482
3
      return NULL;
4483
3
    }
4484
4485
  /* Read the abbrevs for this compilation unit into a table.  */
4486
2.35k
  abbrevs = read_abbrevs (abfd, abbrev_offset, stash, file);
4487
2.35k
  if (! abbrevs)
4488
7
    return NULL;
4489
4490
2.34k
  abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
4491
2.34k
           false, end_ptr);
4492
2.34k
  if (! abbrev_number)
4493
3
    {
4494
      /* PR 19872: An abbrev number of 0 probably means that there is padding
4495
   at the end of the .debug_abbrev section.  Gold puts it there when
4496
   performing an incremental link, for example.  So do not generate
4497
   an error, just return a NULL.  */
4498
3
      return NULL;
4499
3
    }
4500
4501
2.34k
  abbrev = lookup_abbrev (abbrev_number, abbrevs);
4502
2.34k
  if (! abbrev)
4503
13
    {
4504
13
      _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
4505
13
        abbrev_number);
4506
13
      bfd_set_error (bfd_error_bad_value);
4507
13
      return NULL;
4508
13
    }
4509
4510
2.33k
  amt = sizeof (struct comp_unit);
4511
2.33k
  unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
4512
2.33k
  if (unit == NULL)
4513
0
    return NULL;
4514
2.33k
  unit->abfd = abfd;
4515
2.33k
  unit->version = version;
4516
2.33k
  unit->addr_size = addr_size;
4517
2.33k
  unit->offset_size = offset_size;
4518
2.33k
  unit->abbrevs = abbrevs;
4519
2.33k
  unit->end_ptr = end_ptr;
4520
2.33k
  unit->stash = stash;
4521
2.33k
  unit->file = file;
4522
2.33k
  unit->info_ptr_unit = info_ptr_unit;
4523
4524
2.33k
  if (abbrev->tag == DW_TAG_compile_unit)
4525
2.17k
    compunit_flag = true;
4526
4527
18.0k
  for (i = 0; i < abbrev->num_attrs; ++i)
4528
15.7k
    {
4529
15.7k
      info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
4530
15.7k
      if (info_ptr == NULL)
4531
27
  goto err_exit;
4532
4533
      /* Identify attributes of the form strx* and addrx* which come before
4534
   DW_AT_str_offsets_base and DW_AT_addr_base respectively in the CU.
4535
   Store the attributes in an array and process them later.  */
4536
15.7k
      if ((unit->dwarf_str_offset == 0 && is_strx_form (attr.form))
4537
15.7k
    || (unit->dwarf_addr_offset == 0 && is_addrx_form (attr.form)))
4538
189
  {
4539
189
    if (str_count <= str_alloc)
4540
189
      {
4541
189
        str_alloc = 2 * str_alloc + 200;
4542
189
        str_addrp = bfd_realloc (str_addrp,
4543
189
               str_alloc * sizeof (*str_addrp));
4544
189
        if (str_addrp == NULL)
4545
0
    goto err_exit;
4546
189
      }
4547
189
    str_addrp[str_count] = attr;
4548
189
    str_count++;
4549
189
    continue;
4550
189
  }
4551
4552
      /* Store the data if it is of an attribute we want to keep in a
4553
   partial symbol table.  */
4554
15.5k
      switch (attr.name)
4555
15.5k
  {
4556
2.28k
  case DW_AT_stmt_list:
4557
2.28k
    if (is_int_form (&attr))
4558
2.28k
      {
4559
2.28k
        unit->stmtlist = 1;
4560
2.28k
        unit->line_offset = attr.u.val;
4561
2.28k
      }
4562
2.28k
    break;
4563
4564
2.13k
  case DW_AT_name:
4565
2.13k
    if (is_str_form (&attr))
4566
2.12k
      unit->name = attr.u.str;
4567
2.13k
    break;
4568
4569
2.10k
  case DW_AT_low_pc:
4570
2.10k
    if (is_int_form (&attr))
4571
2.10k
      {
4572
2.10k
        low_pc = attr.u.val;
4573
        /* If the compilation unit DIE has a DW_AT_low_pc attribute,
4574
     this is the base address to use when reading location
4575
     lists or range lists.  */
4576
2.10k
        if (compunit_flag)
4577
2.05k
    unit->base_address = low_pc;
4578
2.10k
      }
4579
2.10k
    break;
4580
4581
1.90k
  case DW_AT_high_pc:
4582
1.90k
    if (is_int_form (&attr))
4583
1.88k
      {
4584
1.88k
        high_pc = attr.u.val;
4585
1.88k
        high_pc_relative = attr.form != DW_FORM_addr;
4586
1.88k
      }
4587
1.90k
    break;
4588
4589
230
  case DW_AT_ranges:
4590
230
    if (is_int_form (&attr)
4591
230
        && !read_rangelist (unit, &unit->arange,
4592
222
          &unit->file->trie_root, attr.u.val))
4593
12
      goto err_exit;
4594
218
    break;
4595
4596
2.15k
  case DW_AT_comp_dir:
4597
2.15k
    {
4598
2.15k
      char *comp_dir = attr.u.str;
4599
4600
      /* PR 17512: file: 1fe726be.  */
4601
2.15k
      if (!is_str_form (&attr))
4602
13
        {
4603
13
    _bfd_error_handler
4604
13
      (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
4605
13
    comp_dir = NULL;
4606
13
        }
4607
4608
2.15k
      if (comp_dir)
4609
1.77k
        {
4610
    /* Irix 6.2 native cc prepends <machine>.: to the compilation
4611
       directory, get rid of it.  */
4612
1.77k
    char *cp = strchr (comp_dir, ':');
4613
4614
1.77k
    if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4615
0
      comp_dir = cp + 1;
4616
1.77k
        }
4617
2.15k
      unit->comp_dir = comp_dir;
4618
2.15k
      break;
4619
230
    }
4620
4621
2.27k
  case DW_AT_language:
4622
2.27k
    if (is_int_form (&attr))
4623
2.25k
      unit->lang = attr.u.val;
4624
2.27k
    break;
4625
4626
39
  case DW_AT_addr_base:
4627
39
    unit->dwarf_addr_offset = attr.u.val;
4628
39
    break;
4629
4630
28
  case DW_AT_str_offsets_base:
4631
28
    unit->dwarf_str_offset = attr.u.val;
4632
28
    break;
4633
4634
2.37k
  default:
4635
2.37k
    break;
4636
15.5k
  }
4637
15.5k
    }
4638
4639
2.45k
  for (i = 0; i < str_count; ++i)
4640
159
    reread_attribute (unit, &str_addrp[i], &low_pc, &high_pc,
4641
159
          &high_pc_relative, compunit_flag);
4642
4643
2.29k
  if (high_pc_relative)
4644
194
    high_pc += low_pc;
4645
2.29k
  if (high_pc != 0)
4646
1.87k
    {
4647
1.87k
      if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
4648
1.87k
           low_pc, high_pc))
4649
0
  goto err_exit;
4650
1.87k
    }
4651
4652
2.29k
  unit->first_child_die_ptr = info_ptr;
4653
4654
2.29k
  free (str_addrp);
4655
2.29k
  return unit;
4656
4657
39
 err_exit:
4658
39
  unit->error = 1;
4659
39
  free (str_addrp);
4660
39
  return NULL;
4661
2.29k
}
4662
4663
/* Return TRUE if UNIT may contain the address given by ADDR.  When
4664
   there are functions written entirely with inline asm statements, the
4665
   range info in the compilation unit header may not be correct.  We
4666
   need to consult the line info table to see if a compilation unit
4667
   really contains the given address.  */
4668
4669
static bool
4670
comp_unit_may_contain_address (struct comp_unit *unit, bfd_vma addr)
4671
253k
{
4672
253k
  struct arange *arange;
4673
4674
253k
  if (unit->error)
4675
96.8k
    return false;
4676
4677
156k
  if (unit->arange.high == 0 /* No ranges have been computed yet.  */
4678
156k
      || unit->line_table == NULL) /* The line info table has not been loaded.  */
4679
20.4k
    return true;
4680
4681
428k
  for (arange = &unit->arange; arange != NULL; arange = arange->next)
4682
298k
    if (addr >= arange->low && addr < arange->high)
4683
5.65k
      return true;
4684
4685
130k
  return false;
4686
136k
}
4687
4688
/* If UNIT contains ADDR, set the output parameters to the values for
4689
   the line containing ADDR and return TRUE.  Otherwise return FALSE.
4690
   The output parameters, FILENAME_PTR, FUNCTION_PTR, and
4691
   LINENUMBER_PTR, are pointers to the objects to be filled in.  */
4692
4693
static bool
4694
comp_unit_find_nearest_line (struct comp_unit *unit,
4695
           bfd_vma addr,
4696
           const char **filename_ptr,
4697
           struct funcinfo **function_ptr,
4698
           unsigned int *linenumber_ptr,
4699
           unsigned int *discriminator_ptr)
4700
41.6k
{
4701
41.6k
  bool line_p, func_p;
4702
4703
41.6k
  if (!comp_unit_maybe_decode_line_info (unit))
4704
29.7k
    return false;
4705
4706
11.9k
  *function_ptr = NULL;
4707
11.9k
  func_p = lookup_address_in_function_table (unit, addr, function_ptr);
4708
4709
11.9k
  if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
4710
2.38k
    unit->stash->inliner_chain = *function_ptr;
4711
4712
11.9k
  line_p = lookup_address_in_line_info_table (unit->line_table, addr,
4713
11.9k
                filename_ptr,
4714
11.9k
                linenumber_ptr,
4715
11.9k
                discriminator_ptr);
4716
11.9k
  return line_p || func_p;
4717
41.6k
}
4718
4719
/* Check to see if line info is already decoded in a comp_unit.
4720
   If not, decode it.  Returns TRUE if no errors were encountered;
4721
   FALSE otherwise.  */
4722
4723
static bool
4724
comp_unit_maybe_decode_line_info (struct comp_unit *unit)
4725
491k
{
4726
491k
  if (unit->error)
4727
190k
    return false;
4728
4729
301k
  if (! unit->line_table)
4730
2.29k
    {
4731
2.29k
      if (! unit->stmtlist)
4732
20
  {
4733
20
    unit->error = 1;
4734
20
    return false;
4735
20
  }
4736
4737
2.27k
      unit->line_table = decode_line_info (unit);
4738
4739
2.27k
      if (! unit->line_table)
4740
728
  {
4741
728
    unit->error = 1;
4742
728
    return false;
4743
728
  }
4744
4745
1.54k
      if (unit->first_child_die_ptr < unit->end_ptr
4746
1.54k
    && ! scan_unit_for_symbols (unit))
4747
146
  {
4748
146
    unit->error = 1;
4749
146
    return false;
4750
146
  }
4751
1.54k
    }
4752
4753
300k
  return true;
4754
301k
}
4755
4756
/* If UNIT contains SYM at ADDR, set the output parameters to the
4757
   values for the line containing SYM.  The output parameters,
4758
   FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
4759
   filled in.
4760
4761
   Return TRUE if UNIT contains SYM, and no errors were encountered;
4762
   FALSE otherwise.  */
4763
4764
static bool
4765
comp_unit_find_line (struct comp_unit *unit,
4766
         asymbol *sym,
4767
         bfd_vma addr,
4768
         const char **filename_ptr,
4769
         unsigned int *linenumber_ptr)
4770
443k
{
4771
443k
  if (!comp_unit_maybe_decode_line_info (unit))
4772
161k
    return false;
4773
4774
282k
  if (sym->flags & BSF_FUNCTION)
4775
25.4k
    return lookup_symbol_in_function_table (unit, sym, addr,
4776
25.4k
              filename_ptr,
4777
25.4k
              linenumber_ptr);
4778
4779
256k
  return lookup_symbol_in_variable_table (unit, sym, addr,
4780
256k
            filename_ptr,
4781
256k
            linenumber_ptr);
4782
282k
}
4783
4784
/* Extract all interesting funcinfos and varinfos of a compilation
4785
   unit into hash tables for faster lookup.  Returns TRUE if no
4786
   errors were enountered; FALSE otherwise.  */
4787
4788
static bool
4789
comp_unit_hash_info (struct dwarf2_debug *stash,
4790
         struct comp_unit *unit,
4791
         struct info_hash_table *funcinfo_hash_table,
4792
         struct info_hash_table *varinfo_hash_table)
4793
683
{
4794
683
  struct funcinfo* each_func;
4795
683
  struct varinfo* each_var;
4796
683
  bool okay = true;
4797
4798
683
  BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
4799
4800
683
  if (!comp_unit_maybe_decode_line_info (unit))
4801
34
    return false;
4802
4803
649
  BFD_ASSERT (!unit->cached);
4804
4805
  /* To preserve the original search order, we went to visit the function
4806
     infos in the reversed order of the list.  However, making the list
4807
     bi-directional use quite a bit of extra memory.  So we reverse
4808
     the list first, traverse the list in the now reversed order and
4809
     finally reverse the list again to get back the original order.  */
4810
649
  unit->function_table = reverse_funcinfo_list (unit->function_table);
4811
649
  for (each_func = unit->function_table;
4812
16.0k
       each_func && okay;
4813
15.3k
       each_func = each_func->prev_func)
4814
15.3k
    {
4815
      /* Skip nameless functions.  */
4816
15.3k
      if (each_func->name)
4817
  /* There is no need to copy name string into hash table as
4818
     name string is either in the dwarf string buffer or
4819
     info in the stash.  */
4820
14.5k
  okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
4821
14.5k
               (void*) each_func, false);
4822
15.3k
    }
4823
649
  unit->function_table = reverse_funcinfo_list (unit->function_table);
4824
649
  if (!okay)
4825
0
    return false;
4826
4827
  /* We do the same for variable infos.  */
4828
649
  unit->variable_table = reverse_varinfo_list (unit->variable_table);
4829
649
  for (each_var = unit->variable_table;
4830
25.0k
       each_var && okay;
4831
24.4k
       each_var = each_var->prev_var)
4832
24.4k
    {
4833
      /* Skip stack vars and vars with no files or names.  */
4834
24.4k
      if (! each_var->stack
4835
24.4k
    && each_var->file != NULL
4836
24.4k
    && each_var->name != NULL)
4837
  /* There is no need to copy name string into hash table as
4838
     name string is either in the dwarf string buffer or
4839
     info in the stash.  */
4840
2.07k
  okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
4841
2.07k
               (void*) each_var, false);
4842
24.4k
    }
4843
4844
649
  unit->variable_table = reverse_varinfo_list (unit->variable_table);
4845
649
  unit->cached = true;
4846
649
  return okay;
4847
649
}
4848
4849
/* Locate a section in a BFD containing debugging info.  The search starts
4850
   from the section after AFTER_SEC, or from the first section in the BFD if
4851
   AFTER_SEC is NULL.  The search works by examining the names of the
4852
   sections.  There are three permissiable names.  The first two are given
4853
   by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4854
   and .zdebug_info).  The third is a prefix .gnu.linkonce.wi.
4855
   This is a variation on the .debug_info section which has a checksum
4856
   describing the contents appended onto the name.  This allows the linker to
4857
   identify and discard duplicate debugging sections for different
4858
   compilation units.  */
4859
492k
#define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4860
4861
static asection *
4862
find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
4863
     asection *after_sec)
4864
5.27k
{
4865
5.27k
  asection *msec;
4866
5.27k
  const char *look;
4867
4868
5.27k
  if (after_sec == NULL)
4869
3.92k
    {
4870
3.92k
      look = debug_sections[debug_info].uncompressed_name;
4871
3.92k
      msec = bfd_get_section_by_name (abfd, look);
4872
      /* Testing SEC_HAS_CONTENTS is an anti-fuzzer measure.  Of
4873
   course debug sections always have contents.  */
4874
3.92k
      if (msec != NULL && (msec->flags & SEC_HAS_CONTENTS) != 0)
4875
918
  return msec;
4876
4877
3.00k
      look = debug_sections[debug_info].compressed_name;
4878
3.00k
      msec = bfd_get_section_by_name (abfd, look);
4879
3.00k
      if (msec != NULL && (msec->flags & SEC_HAS_CONTENTS) != 0)
4880
0
        return msec;
4881
4882
557k
      for (msec = abfd->sections; msec != NULL; msec = msec->next)
4883
554k
  if ((msec->flags & SEC_HAS_CONTENTS) != 0
4884
554k
      && startswith (msec->name, GNU_LINKONCE_INFO))
4885
30
    return msec;
4886
4887
2.97k
      return NULL;
4888
3.00k
    }
4889
4890
8.01k
  for (msec = after_sec->next; msec != NULL; msec = msec->next)
4891
7.11k
    {
4892
7.11k
      if ((msec->flags & SEC_HAS_CONTENTS) == 0)
4893
34
  continue;
4894
4895
7.08k
      look = debug_sections[debug_info].uncompressed_name;
4896
7.08k
      if (strcmp (msec->name, look) == 0)
4897
438
  return msec;
4898
4899
6.64k
      look = debug_sections[debug_info].compressed_name;
4900
6.64k
      if (look != NULL && strcmp (msec->name, look) == 0)
4901
0
  return msec;
4902
4903
6.64k
      if (startswith (msec->name, GNU_LINKONCE_INFO))
4904
21
  return msec;
4905
6.64k
    }
4906
4907
899
  return NULL;
4908
1.35k
}
4909
4910
/* Transfer VMAs from object file to separate debug file.  */
4911
4912
static void
4913
set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
4914
0
{
4915
0
  asection *s, *d;
4916
4917
0
  for (s = orig_bfd->sections, d = debug_bfd->sections;
4918
0
       s != NULL && d != NULL;
4919
0
       s = s->next, d = d->next)
4920
0
    {
4921
0
      if ((d->flags & SEC_DEBUGGING) != 0)
4922
0
  break;
4923
      /* ??? Assumes 1-1 correspondence between sections in the
4924
   two files.  */
4925
0
      if (strcmp (s->name, d->name) == 0)
4926
0
  {
4927
0
    d->output_section = s->output_section;
4928
0
    d->output_offset = s->output_offset;
4929
0
    d->vma = s->vma;
4930
0
  }
4931
0
    }
4932
0
}
4933
4934
/* If the dwarf2 info was found in a separate debug file, return the
4935
   debug file section corresponding to the section in the original file
4936
   and the debug file symbols.  */
4937
4938
static void
4939
_bfd_dwarf2_stash_syms (struct dwarf2_debug *stash, bfd *abfd,
4940
      asection **sec, asymbol ***syms)
4941
76.7k
{
4942
76.7k
  if (stash->f.bfd_ptr != abfd)
4943
0
    {
4944
0
      asection *s, *d;
4945
4946
0
      if (*sec == NULL)
4947
0
  {
4948
0
    *syms = stash->f.syms;
4949
0
    return;
4950
0
  }
4951
4952
0
      for (s = abfd->sections, d = stash->f.bfd_ptr->sections;
4953
0
     s != NULL && d != NULL;
4954
0
     s = s->next, d = d->next)
4955
0
  {
4956
0
    if ((d->flags & SEC_DEBUGGING) != 0)
4957
0
      break;
4958
0
    if (s == *sec
4959
0
        && strcmp (s->name, d->name) == 0)
4960
0
      {
4961
0
        *sec = d;
4962
0
        *syms = stash->f.syms;
4963
0
        break;
4964
0
      }
4965
0
  }
4966
0
    }
4967
76.7k
}
4968
4969
/* Unset vmas for adjusted sections in STASH.  */
4970
4971
static void
4972
unset_sections (struct dwarf2_debug *stash)
4973
111k
{
4974
111k
  int i;
4975
111k
  struct adjusted_section *p;
4976
4977
111k
  i = stash->adjusted_section_count;
4978
111k
  p = stash->adjusted_sections;
4979
7.44M
  for (; i > 0; i--, p++)
4980
7.33M
    p->section->vma = p->orig_vma;
4981
111k
}
4982
4983
/* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4984
   relocatable object file.  VMAs are normally all zero in relocatable
4985
   object files, so if we want to distinguish locations in sections by
4986
   address we need to set VMAs so the sections do not overlap.  We
4987
   also set VMA on .debug_info so that when we have multiple
4988
   .debug_info sections (or the linkonce variant) they also do not
4989
   overlap.  The multiple .debug_info sections make up a single
4990
   logical section.  ??? We should probably do the same for other
4991
   debug sections.  */
4992
4993
static bool
4994
place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
4995
81.4k
{
4996
81.4k
  bfd *abfd;
4997
81.4k
  struct adjusted_section *p;
4998
81.4k
  int i;
4999
81.4k
  const char *debug_info_name;
5000
5001
81.4k
  if (stash->adjusted_section_count != 0)
5002
80.7k
    {
5003
80.7k
      i = stash->adjusted_section_count;
5004
80.7k
      p = stash->adjusted_sections;
5005
7.39M
      for (; i > 0; i--, p++)
5006
7.31M
  p->section->vma = p->adj_vma;
5007
80.7k
      return true;
5008
80.7k
    }
5009
5010
686
  debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
5011
686
  i = 0;
5012
686
  abfd = orig_bfd;
5013
686
  while (1)
5014
686
    {
5015
686
      asection *sect;
5016
5017
30.6k
      for (sect = abfd->sections; sect != NULL; sect = sect->next)
5018
29.9k
  {
5019
29.9k
    int is_debug_info;
5020
5021
29.9k
    if (sect->output_section != NULL
5022
29.9k
        && sect->output_section != sect
5023
29.9k
        && (sect->flags & SEC_DEBUGGING) == 0)
5024
0
      continue;
5025
5026
29.9k
    is_debug_info = (strcmp (sect->name, debug_info_name) == 0
5027
29.9k
         || startswith (sect->name, GNU_LINKONCE_INFO));
5028
5029
29.9k
    if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
5030
29.9k
        && !is_debug_info)
5031
10.4k
      continue;
5032
5033
19.4k
    i++;
5034
19.4k
  }
5035
686
      if (abfd == stash->f.bfd_ptr)
5036
686
  break;
5037
0
      abfd = stash->f.bfd_ptr;
5038
0
    }
5039
5040
686
  if (i <= 1)
5041
0
    stash->adjusted_section_count = -1;
5042
686
  else
5043
686
    {
5044
686
      bfd_vma last_vma = 0, last_dwarf = 0;
5045
686
      size_t amt = i * sizeof (struct adjusted_section);
5046
5047
686
      p = (struct adjusted_section *) bfd_malloc (amt);
5048
686
      if (p == NULL)
5049
0
  return false;
5050
5051
686
      stash->adjusted_sections = p;
5052
686
      stash->adjusted_section_count = i;
5053
5054
686
      abfd = orig_bfd;
5055
686
      while (1)
5056
686
  {
5057
686
    asection *sect;
5058
5059
30.6k
    for (sect = abfd->sections; sect != NULL; sect = sect->next)
5060
29.9k
      {
5061
29.9k
        bfd_size_type sz;
5062
29.9k
        int is_debug_info;
5063
5064
29.9k
        if (sect->output_section != NULL
5065
29.9k
      && sect->output_section != sect
5066
29.9k
      && (sect->flags & SEC_DEBUGGING) == 0)
5067
0
    continue;
5068
5069
29.9k
        is_debug_info = (strcmp (sect->name, debug_info_name) == 0
5070
29.9k
             || startswith (sect->name, GNU_LINKONCE_INFO));
5071
5072
29.9k
        if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
5073
29.9k
      && !is_debug_info)
5074
10.4k
    continue;
5075
5076
19.4k
        sz = sect->rawsize ? sect->rawsize : sect->size;
5077
5078
19.4k
        p->section = sect;
5079
19.4k
        p->orig_vma = sect->vma;
5080
5081
19.4k
        bfd_vma *v = is_debug_info ? &last_dwarf : &last_vma;
5082
        /* Align the new address to the current section
5083
     alignment.  */
5084
19.4k
        bfd_vma mask = -(bfd_vma) 1 << sect->alignment_power;
5085
19.4k
        *v = (*v + ~mask) & mask;
5086
19.4k
        sect->vma = *v;
5087
19.4k
        *v += sz;
5088
5089
19.4k
        p->adj_vma = sect->vma;
5090
19.4k
        p++;
5091
19.4k
      }
5092
686
    if (abfd == stash->f.bfd_ptr)
5093
686
      break;
5094
0
    abfd = stash->f.bfd_ptr;
5095
0
  }
5096
686
    }
5097
5098
686
  if (orig_bfd != stash->f.bfd_ptr)
5099
0
    set_debug_vma (orig_bfd, stash->f.bfd_ptr);
5100
5101
686
  return true;
5102
686
}
5103
5104
/* Look up a funcinfo by name using the given info hash table.  If found,
5105
   also update the locations pointed to by filename_ptr and linenumber_ptr.
5106
5107
   This function returns TRUE if a funcinfo that matches the given symbol
5108
   and address is found with any error; otherwise it returns FALSE.  */
5109
5110
static bool
5111
info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
5112
         asymbol *sym,
5113
         bfd_vma addr,
5114
         const char **filename_ptr,
5115
         unsigned int *linenumber_ptr)
5116
1.29k
{
5117
1.29k
  struct funcinfo* each_func;
5118
1.29k
  struct funcinfo* best_fit = NULL;
5119
1.29k
  bfd_vma best_fit_len = (bfd_vma) -1;
5120
1.29k
  struct info_list_node *node;
5121
1.29k
  struct arange *arange;
5122
1.29k
  const char *name = bfd_asymbol_name (sym);
5123
5124
1.29k
  for (node = lookup_info_hash_table (hash_table, name);
5125
1.81k
       node;
5126
1.29k
       node = node->next)
5127
522
    {
5128
522
      each_func = (struct funcinfo *) node->info;
5129
522
      for (arange = &each_func->arange;
5130
4.64k
     arange;
5131
4.12k
     arange = arange->next)
5132
4.12k
  {
5133
4.12k
    if (addr >= arange->low
5134
4.12k
        && addr < arange->high
5135
4.12k
        && arange->high - arange->low < best_fit_len)
5136
483
      {
5137
483
        best_fit = each_func;
5138
483
        best_fit_len = arange->high - arange->low;
5139
483
      }
5140
4.12k
  }
5141
522
    }
5142
5143
1.29k
  if (best_fit)
5144
480
    {
5145
480
      *filename_ptr = best_fit->file;
5146
480
      *linenumber_ptr = best_fit->line;
5147
480
      return true;
5148
480
    }
5149
5150
814
  return false;
5151
1.29k
}
5152
5153
/* Look up a varinfo by name using the given info hash table.  If found,
5154
   also update the locations pointed to by filename_ptr and linenumber_ptr.
5155
5156
   This function returns TRUE if a varinfo that matches the given symbol
5157
   and address is found with any error; otherwise it returns FALSE.  */
5158
5159
static bool
5160
info_hash_lookup_varinfo (struct info_hash_table *hash_table,
5161
        asymbol *sym,
5162
        bfd_vma addr,
5163
        const char **filename_ptr,
5164
        unsigned int *linenumber_ptr)
5165
15.1k
{
5166
15.1k
  struct varinfo* each;
5167
15.1k
  struct info_list_node *node;
5168
15.1k
  const char *name = bfd_asymbol_name (sym);
5169
5170
15.1k
  for (node = lookup_info_hash_table (hash_table, name);
5171
15.3k
       node;
5172
15.1k
       node = node->next)
5173
706
    {
5174
706
      each = (struct varinfo *) node->info;
5175
706
      if (each->addr == addr)
5176
552
  {
5177
552
    *filename_ptr = each->file;
5178
552
    *linenumber_ptr = each->line;
5179
552
    return true;
5180
552
  }
5181
706
    }
5182
5183
14.6k
  return false;
5184
15.1k
}
5185
5186
/* Update the funcinfo and varinfo info hash tables if they are
5187
   not up to date.  Returns TRUE if there is no error; otherwise
5188
   returns FALSE and disable the info hash tables.  */
5189
5190
static bool
5191
stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
5192
16.5k
{
5193
16.5k
  struct comp_unit *each;
5194
5195
  /* Exit if hash tables are up-to-date.  */
5196
16.5k
  if (stash->f.all_comp_units == stash->hash_units_head)
5197
16.4k
    return true;
5198
5199
87
  if (stash->hash_units_head)
5200
0
    each = stash->hash_units_head->prev_unit;
5201
87
  else
5202
87
    each = stash->f.last_comp_unit;
5203
5204
736
  while (each)
5205
683
    {
5206
683
      if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
5207
683
        stash->varinfo_hash_table))
5208
34
  {
5209
34
    stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5210
34
    return false;
5211
34
  }
5212
649
      each = each->prev_unit;
5213
649
    }
5214
5215
53
  stash->hash_units_head = stash->f.all_comp_units;
5216
53
  return true;
5217
87
}
5218
5219
/* Check consistency of info hash tables.  This is for debugging only.  */
5220
5221
static void ATTRIBUTE_UNUSED
5222
stash_verify_info_hash_table (struct dwarf2_debug *stash)
5223
0
{
5224
0
  struct comp_unit *each_unit;
5225
0
  struct funcinfo *each_func;
5226
0
  struct varinfo *each_var;
5227
0
  struct info_list_node *node;
5228
0
  bool found;
5229
0
5230
0
  for (each_unit = stash->f.all_comp_units;
5231
0
       each_unit;
5232
0
       each_unit = each_unit->next_unit)
5233
0
    {
5234
0
      for (each_func = each_unit->function_table;
5235
0
     each_func;
5236
0
     each_func = each_func->prev_func)
5237
0
  {
5238
0
    if (!each_func->name)
5239
0
      continue;
5240
0
    node = lookup_info_hash_table (stash->funcinfo_hash_table,
5241
0
           each_func->name);
5242
0
    BFD_ASSERT (node);
5243
0
    found = false;
5244
0
    while (node && !found)
5245
0
      {
5246
0
        found = node->info == each_func;
5247
0
        node = node->next;
5248
0
      }
5249
0
    BFD_ASSERT (found);
5250
0
  }
5251
0
5252
0
      for (each_var = each_unit->variable_table;
5253
0
     each_var;
5254
0
     each_var = each_var->prev_var)
5255
0
  {
5256
0
    if (!each_var->name || !each_var->file || each_var->stack)
5257
0
      continue;
5258
0
    node = lookup_info_hash_table (stash->varinfo_hash_table,
5259
0
           each_var->name);
5260
0
    BFD_ASSERT (node);
5261
0
    found = false;
5262
0
    while (node && !found)
5263
0
      {
5264
0
        found = node->info == each_var;
5265
0
        node = node->next;
5266
0
      }
5267
0
    BFD_ASSERT (found);
5268
0
  }
5269
0
    }
5270
0
}
5271
5272
/* Check to see if we want to enable the info hash tables, which consume
5273
   quite a bit of memory.  Currently we only check the number times
5274
   bfd_dwarf2_find_line is called.  In the future, we may also want to
5275
   take the number of symbols into account.  */
5276
5277
static void
5278
stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
5279
15.8k
{
5280
15.8k
  BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
5281
5282
15.8k
  if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
5283
15.7k
    return;
5284
5285
  /* FIXME: Maybe we should check the reduce_memory_overheads
5286
     and optimize fields in the bfd_link_info structure ?  */
5287
5288
  /* Create hash tables.  */
5289
97
  stash->funcinfo_hash_table = create_info_hash_table (abfd);
5290
97
  stash->varinfo_hash_table = create_info_hash_table (abfd);
5291
97
  if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
5292
0
    {
5293
      /* Turn off info hashes if any allocation above fails.  */
5294
0
      stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5295
0
      return;
5296
0
    }
5297
  /* We need a forced update so that the info hash tables will
5298
     be created even though there is no compilation unit.  That
5299
     happens if STASH_INFO_HASH_TRIGGER is 0.  */
5300
97
  if (stash_maybe_update_info_hash_tables (stash))
5301
63
    stash->info_hash_status = STASH_INFO_HASH_ON;
5302
97
}
5303
5304
/* Find the file and line associated with a symbol and address using the
5305
   info hash tables of a stash. If there is a match, the function returns
5306
   TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
5307
   otherwise it returns FALSE.  */
5308
5309
static bool
5310
stash_find_line_fast (struct dwarf2_debug *stash,
5311
          asymbol *sym,
5312
          bfd_vma addr,
5313
          const char **filename_ptr,
5314
          unsigned int *linenumber_ptr)
5315
16.4k
{
5316
16.4k
  BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
5317
5318
16.4k
  if (sym->flags & BSF_FUNCTION)
5319
1.29k
    return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
5320
1.29k
              filename_ptr, linenumber_ptr);
5321
15.1k
  return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
5322
15.1k
           filename_ptr, linenumber_ptr);
5323
16.4k
}
5324
5325
/* Save current section VMAs.  */
5326
5327
static bool
5328
save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
5329
3.79k
{
5330
3.79k
  asection *s;
5331
3.79k
  unsigned int i;
5332
5333
3.79k
  if (abfd->section_count == 0)
5334
0
    return true;
5335
3.79k
  stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
5336
3.79k
  if (stash->sec_vma == NULL)
5337
0
    return false;
5338
3.79k
  stash->sec_vma_count = abfd->section_count;
5339
3.79k
  for (i = 0, s = abfd->sections;
5340
591k
       s != NULL && i < abfd->section_count;
5341
588k
       i++, s = s->next)
5342
588k
    {
5343
588k
      if (s->output_section != NULL)
5344
0
  stash->sec_vma[i] = s->output_section->vma + s->output_offset;
5345
588k
      else
5346
588k
  stash->sec_vma[i] = s->vma;
5347
588k
    }
5348
3.79k
  return true;
5349
3.79k
}
5350
5351
/* Compare current section VMAs against those at the time the stash
5352
   was created.  If find_nearest_line is used in linker warnings or
5353
   errors early in the link process, the debug info stash will be
5354
   invalid for later calls.  This is because we relocate debug info
5355
   sections, so the stashed section contents depend on symbol values,
5356
   which in turn depend on section VMAs.  */
5357
5358
static bool
5359
section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
5360
361k
{
5361
361k
  asection *s;
5362
361k
  unsigned int i;
5363
5364
  /* PR 24334: If the number of sections in ABFD has changed between
5365
     when the stash was created and now, then we cannot trust the
5366
     stashed vma information.  */
5367
361k
  if (abfd->section_count != stash->sec_vma_count)
5368
0
    return false;
5369
5370
361k
  for (i = 0, s = abfd->sections;
5371
400M
       s != NULL && i < abfd->section_count;
5372
399M
       i++, s = s->next)
5373
399M
    {
5374
399M
      bfd_vma vma;
5375
5376
399M
      if (s->output_section != NULL)
5377
0
  vma = s->output_section->vma + s->output_offset;
5378
399M
      else
5379
399M
  vma = s->vma;
5380
399M
      if (vma != stash->sec_vma[i])
5381
0
  return false;
5382
399M
    }
5383
361k
  return true;
5384
361k
}
5385
5386
/* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
5387
   If DEBUG_BFD is not specified, we read debug information from ABFD
5388
   or its gnu_debuglink. The results will be stored in PINFO.
5389
   The function returns TRUE iff debug information is ready.  */
5390
5391
bool
5392
_bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
5393
            const struct dwarf_debug_section *debug_sections,
5394
            asymbol **symbols,
5395
            void **pinfo,
5396
            bool do_place)
5397
365k
{
5398
365k
  bfd_size_type total_size;
5399
365k
  asection *msec;
5400
365k
  struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
5401
5402
365k
  if (stash != NULL)
5403
361k
    {
5404
361k
      if (stash->orig_bfd == abfd
5405
361k
    && section_vma_same (abfd, stash))
5406
361k
  {
5407
    /* Check that we did previously find some debug information
5408
       before attempting to make use of it.  */
5409
361k
    if (stash->f.dwarf_info_size != 0)
5410
111k
      {
5411
111k
        if (do_place && !place_sections (abfd, stash))
5412
0
    return false;
5413
111k
        return true;
5414
111k
      }
5415
5416
250k
    return false;
5417
361k
  }
5418
0
      _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
5419
0
      memset (stash, 0, sizeof (*stash));
5420
0
    }
5421
3.79k
  else
5422
3.79k
    {
5423
3.79k
      stash = (struct dwarf2_debug *) bfd_zalloc (abfd, sizeof (*stash));
5424
3.79k
      if (! stash)
5425
0
  return false;
5426
3.79k
      *pinfo = stash;
5427
3.79k
    }
5428
3.79k
  stash->orig_bfd = abfd;
5429
3.79k
  stash->debug_sections = debug_sections;
5430
3.79k
  stash->f.syms = symbols;
5431
3.79k
  if (!save_section_vma (abfd, stash))
5432
0
    return false;
5433
5434
3.79k
  stash->f.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5435
3.79k
                 del_abbrev, calloc, free);
5436
3.79k
  if (!stash->f.abbrev_offsets)
5437
0
    return false;
5438
5439
3.79k
  stash->alt.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5440
3.79k
             del_abbrev, calloc, free);
5441
3.79k
  if (!stash->alt.abbrev_offsets)
5442
0
    return false;
5443
5444
3.79k
  stash->f.trie_root = alloc_trie_leaf (abfd);
5445
3.79k
  if (!stash->f.trie_root)
5446
0
    return false;
5447
5448
3.79k
  stash->alt.trie_root = alloc_trie_leaf (abfd);
5449
3.79k
  if (!stash->alt.trie_root)
5450
0
    return false;
5451
5452
3.79k
  if (debug_bfd == NULL)
5453
3.79k
    debug_bfd = abfd;
5454
5455
3.79k
  msec = find_debug_info (debug_bfd, debug_sections, NULL);
5456
3.79k
  if (msec == NULL && abfd == debug_bfd)
5457
2.97k
    {
5458
2.97k
      char * debug_filename;
5459
5460
2.97k
      debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
5461
2.97k
      if (debug_filename == NULL)
5462
2.97k
  debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
5463
5464
2.97k
      if (debug_filename == NULL)
5465
  /* No dwarf2 info, and no gnu_debuglink to follow.
5466
     Note that at this point the stash has been allocated, but
5467
     contains zeros.  This lets future calls to this function
5468
     fail more quickly.  */
5469
2.97k
  return false;
5470
5471
0
      debug_bfd = bfd_openr (debug_filename, NULL);
5472
0
      free (debug_filename);
5473
0
      if (debug_bfd == NULL)
5474
  /* FIXME: Should we report our failure to follow the debuglink ?  */
5475
0
  return false;
5476
5477
      /* Set BFD_DECOMPRESS to decompress debug sections.  */
5478
0
      debug_bfd->flags |= BFD_DECOMPRESS;
5479
0
      if (!bfd_check_format (debug_bfd, bfd_object)
5480
0
    || (msec = find_debug_info (debug_bfd,
5481
0
              debug_sections, NULL)) == NULL
5482
0
    || !bfd_generic_link_read_symbols (debug_bfd))
5483
0
  {
5484
0
    bfd_close (debug_bfd);
5485
0
    return false;
5486
0
  }
5487
5488
0
      symbols = bfd_get_outsymbols (debug_bfd);
5489
0
      stash->f.syms = symbols;
5490
0
      stash->close_on_cleanup = true;
5491
0
    }
5492
827
  stash->f.bfd_ptr = debug_bfd;
5493
5494
827
  if (do_place
5495
827
      && !place_sections (abfd, stash))
5496
0
    return false;
5497
5498
  /* There can be more than one DWARF2 info section in a BFD these
5499
     days.  First handle the easy case when there's only one.  If
5500
     there's more than one, try case two: read them all in and produce
5501
     one large stash.  We do this in two passes - in the first pass we
5502
     just accumulate the section sizes, and in the second pass we
5503
     read in the section's contents.  (The allows us to avoid
5504
     reallocing the data as we add sections to the stash.)  */
5505
5506
827
  if (! find_debug_info (debug_bfd, debug_sections, msec))
5507
694
    {
5508
      /* Case 1: only one info section.  */
5509
694
      total_size = msec->size;
5510
694
      if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
5511
694
        symbols, 0,
5512
694
        &stash->f.dwarf_info_buffer, &total_size))
5513
136
  goto restore_vma;
5514
694
    }
5515
133
  else
5516
133
    {
5517
      /* Case 2: multiple sections.  */
5518
133
      for (total_size = 0;
5519
434
     msec;
5520
301
     msec = find_debug_info (debug_bfd, debug_sections, msec))
5521
313
  {
5522
313
    if (bfd_section_size_insane (debug_bfd, msec))
5523
12
      goto restore_vma;
5524
    /* Catch PR25070 testcase overflowing size calculation here.  */
5525
301
    if (total_size + msec->size < total_size)
5526
0
      {
5527
0
        bfd_set_error (bfd_error_no_memory);
5528
0
        goto restore_vma;
5529
0
      }
5530
301
    total_size += msec->size;
5531
301
  }
5532
5533
121
      stash->f.dwarf_info_buffer = (bfd_byte *) bfd_malloc (total_size);
5534
121
      if (stash->f.dwarf_info_buffer == NULL)
5535
0
  goto restore_vma;
5536
5537
121
      total_size = 0;
5538
121
      for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
5539
351
     msec;
5540
230
     msec = find_debug_info (debug_bfd, debug_sections, msec))
5541
267
  {
5542
267
    bfd_size_type size;
5543
5544
267
    size = msec->size;
5545
267
    if (size == 0)
5546
65
      continue;
5547
5548
202
    if (!(bfd_simple_get_relocated_section_contents
5549
202
    (debug_bfd, msec, stash->f.dwarf_info_buffer + total_size,
5550
202
     symbols)))
5551
37
      goto restore_vma;
5552
5553
165
    total_size += size;
5554
165
  }
5555
121
    }
5556
5557
642
  stash->f.info_ptr = stash->f.dwarf_info_buffer;
5558
642
  stash->f.dwarf_info_size = total_size;
5559
642
  return true;
5560
5561
185
 restore_vma:
5562
185
  unset_sections (stash);
5563
185
  return false;
5564
827
}
5565
5566
/* Parse the next DWARF2 compilation unit at FILE->INFO_PTR.  */
5567
5568
static struct comp_unit *
5569
stash_comp_unit (struct dwarf2_debug *stash, struct dwarf2_debug_file *file)
5570
101k
{
5571
101k
  bfd_size_type length;
5572
101k
  unsigned int offset_size;
5573
101k
  bfd_byte *info_ptr_unit = file->info_ptr;
5574
101k
  bfd_byte *info_ptr_end = file->dwarf_info_buffer + file->dwarf_info_size;
5575
5576
101k
  if (file->info_ptr >= info_ptr_end)
5577
99.4k
    return NULL;
5578
5579
2.54k
  length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5580
  /* A 0xffffff length is the DWARF3 way of indicating
5581
     we use 64-bit offsets, instead of 32-bit offsets.  */
5582
2.54k
  if (length == 0xffffffff)
5583
5
    {
5584
5
      offset_size = 8;
5585
5
      length = read_8_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5586
5
    }
5587
  /* A zero length is the IRIX way of indicating 64-bit offsets,
5588
     mostly because the 64-bit length will generally fit in 32
5589
     bits, and the endianness helps.  */
5590
2.53k
  else if (length == 0)
5591
86
    {
5592
86
      offset_size = 8;
5593
86
      length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5594
86
    }
5595
  /* In the absence of the hints above, we assume 32-bit DWARF2
5596
     offsets even for targets with 64-bit addresses, because:
5597
     a) most of the time these targets will not have generated
5598
     more than 2Gb of debug info and so will not need 64-bit
5599
     offsets,
5600
     and
5601
     b) if they do use 64-bit offsets but they are not using
5602
     the size hints that are tested for above then they are
5603
     not conforming to the DWARF3 standard anyway.  */
5604
2.45k
  else
5605
2.45k
    offset_size = 4;
5606
5607
2.54k
  if (length != 0
5608
2.54k
      && length <= (size_t) (info_ptr_end - file->info_ptr))
5609
2.39k
    {
5610
2.39k
      struct comp_unit *each = parse_comp_unit (stash, file,
5611
2.39k
            file->info_ptr, length,
5612
2.39k
            info_ptr_unit, offset_size);
5613
2.39k
      if (each)
5614
2.29k
  {
5615
2.29k
    if (file->comp_unit_tree == NULL)
5616
480
      file->comp_unit_tree
5617
480
        = splay_tree_new (splay_tree_compare_addr_range,
5618
480
        splay_tree_free_addr_range, NULL);
5619
5620
2.29k
    struct addr_range *r
5621
2.29k
      = (struct addr_range *)bfd_malloc (sizeof (struct addr_range));
5622
2.29k
    r->start = each->info_ptr_unit;
5623
2.29k
    r->end = each->end_ptr;
5624
2.29k
    splay_tree_node v = splay_tree_lookup (file->comp_unit_tree,
5625
2.29k
             (splay_tree_key)r);
5626
2.29k
    if (v != NULL || r->end <= r->start)
5627
0
      abort ();
5628
2.29k
    splay_tree_insert (file->comp_unit_tree, (splay_tree_key)r,
5629
2.29k
           (splay_tree_value)each);
5630
5631
2.29k
    if (file->all_comp_units)
5632
1.81k
      file->all_comp_units->prev_unit = each;
5633
480
    else
5634
480
      file->last_comp_unit = each;
5635
5636
2.29k
    each->next_unit = file->all_comp_units;
5637
2.29k
    file->all_comp_units = each;
5638
5639
2.29k
    if (each->arange.high == 0)
5640
410
      {
5641
410
        each->next_unit_without_ranges = file->all_comp_units_without_ranges;
5642
410
        file->all_comp_units_without_ranges = each->next_unit_without_ranges;
5643
410
      }
5644
5645
2.29k
    file->info_ptr += length;
5646
2.29k
    return each;
5647
2.29k
  }
5648
2.39k
    }
5649
5650
  /* Don't trust any of the DWARF info after a corrupted length or
5651
     parse error.  */
5652
252
  file->info_ptr = info_ptr_end;
5653
252
  return NULL;
5654
2.54k
}
5655
5656
/* Hash function for an asymbol.  */
5657
5658
static hashval_t
5659
hash_asymbol (const void *sym)
5660
3.08k
{
5661
3.08k
  const asymbol *asym = sym;
5662
3.08k
  return htab_hash_string (asym->name);
5663
3.08k
}
5664
5665
/* Equality function for asymbols.  */
5666
5667
static int
5668
eq_asymbol (const void *a, const void *b)
5669
1.75k
{
5670
1.75k
  const asymbol *sa = a;
5671
1.75k
  const asymbol *sb = b;
5672
1.75k
  return strcmp (sa->name, sb->name) == 0;
5673
1.75k
}
5674
5675
/* Scan the debug information in PINFO looking for a DW_TAG_subprogram
5676
   abbrev with a DW_AT_low_pc attached to it.  Then lookup that same
5677
   symbol in SYMBOLS and return the difference between the low_pc and
5678
   the symbol's address.  Returns 0 if no suitable symbol could be found.  */
5679
5680
bfd_signed_vma
5681
_bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
5682
739
{
5683
739
  struct dwarf2_debug *stash;
5684
739
  struct comp_unit * unit;
5685
739
  htab_t sym_hash;
5686
739
  bfd_signed_vma result = 0;
5687
739
  asymbol ** psym;
5688
5689
739
  stash = (struct dwarf2_debug *) *pinfo;
5690
5691
739
  if (stash == NULL || symbols == NULL)
5692
0
    return 0;
5693
5694
739
  sym_hash = htab_create_alloc (10, hash_asymbol, eq_asymbol,
5695
739
        NULL, xcalloc, free);
5696
36.1k
  for (psym = symbols; * psym != NULL; psym++)
5697
35.3k
    {
5698
35.3k
      asymbol * sym = * psym;
5699
5700
35.3k
      if (sym->flags & BSF_FUNCTION && sym->section != NULL)
5701
2.12k
  {
5702
2.12k
    void **slot = htab_find_slot (sym_hash, sym, INSERT);
5703
2.12k
    *slot = sym;
5704
2.12k
  }
5705
35.3k
    }
5706
5707
1.39k
  for (unit = stash->f.all_comp_units; unit; unit = unit->next_unit)
5708
728
    {
5709
728
      struct funcinfo * func;
5710
5711
728
      comp_unit_maybe_decode_line_info (unit);
5712
5713
1.24k
      for (func = unit->function_table; func != NULL; func = func->prev_func)
5714
584
  if (func->name && func->arange.low)
5715
276
    {
5716
276
      asymbol search, *sym;
5717
5718
      /* FIXME: Do we need to scan the aranges looking for the
5719
         lowest pc value?  */
5720
5721
276
      search.name = func->name;
5722
276
      sym = htab_find (sym_hash, &search);
5723
276
      if (sym != NULL)
5724
68
        {
5725
68
    result = func->arange.low - (sym->value + sym->section->vma);
5726
68
    goto done;
5727
68
        }
5728
276
    }
5729
728
    }
5730
5731
739
 done:
5732
739
  htab_delete (sym_hash);
5733
739
  return result;
5734
739
}
5735
5736
/* See _bfd_dwarf2_find_nearest_line_with_alt.  */
5737
5738
int
5739
_bfd_dwarf2_find_nearest_line (bfd *abfd,
5740
             asymbol **symbols,
5741
             asymbol *symbol,
5742
             asection *section,
5743
             bfd_vma offset,
5744
             const char **filename_ptr,
5745
             const char **functionname_ptr,
5746
             unsigned int *linenumber_ptr,
5747
             unsigned int *discriminator_ptr,
5748
             const struct dwarf_debug_section *debug_sections,
5749
             void **pinfo)
5750
231k
{
5751
231k
  return _bfd_dwarf2_find_nearest_line_with_alt
5752
231k
    (abfd, NULL, symbols, symbol, section, offset, filename_ptr,
5753
231k
     functionname_ptr, linenumber_ptr, discriminator_ptr, debug_sections,
5754
231k
     pinfo);
5755
231k
}
5756
5757
/* Find the source code location of SYMBOL.  If SYMBOL is NULL
5758
   then find the nearest source code location corresponding to
5759
   the address SECTION + OFFSET.
5760
   Returns 1 if the line is found without error and fills in
5761
   FILENAME_PTR and LINENUMBER_PTR.  In the case where SYMBOL was
5762
   NULL the FUNCTIONNAME_PTR is also filled in.
5763
   Returns 2 if partial information from _bfd_elf_find_function is
5764
   returned (function and maybe file) by looking at symbols.  DWARF2
5765
   info is present but not regarding the requested code location.
5766
   Returns 0 otherwise.
5767
   SYMBOLS contains the symbol table for ABFD.
5768
   DEBUG_SECTIONS contains the name of the dwarf debug sections.
5769
   If ALT_FILENAME is given, attempt to open the file and use it
5770
   as the .gnu_debugaltlink file. Otherwise this file will be
5771
   searched for when needed.  */
5772
5773
int
5774
_bfd_dwarf2_find_nearest_line_with_alt
5775
  (bfd *abfd,
5776
   const char *alt_filename,
5777
   asymbol **symbols,
5778
   asymbol *symbol,
5779
   asection *section,
5780
   bfd_vma offset,
5781
   const char **filename_ptr,
5782
   const char **functionname_ptr,
5783
   unsigned int *linenumber_ptr,
5784
   unsigned int *discriminator_ptr,
5785
   const struct dwarf_debug_section *debug_sections,
5786
   void **pinfo)
5787
365k
{
5788
  /* Read each compilation unit from the section .debug_info, and check
5789
     to see if it contains the address we are searching for.  If yes,
5790
     lookup the address, and return the line number info.  If no, go
5791
     on to the next compilation unit.
5792
5793
     We keep a list of all the previously read compilation units, and
5794
     a pointer to the next un-read compilation unit.  Check the
5795
     previously read units before reading more.  */
5796
365k
  struct dwarf2_debug *stash;
5797
  /* What address are we looking for?  */
5798
365k
  bfd_vma addr;
5799
365k
  struct comp_unit* each;
5800
365k
  struct funcinfo *function = NULL;
5801
365k
  int found = false;
5802
365k
  bool do_line;
5803
5804
365k
  *filename_ptr = NULL;
5805
365k
  if (functionname_ptr != NULL)
5806
330k
    *functionname_ptr = NULL;
5807
365k
  *linenumber_ptr = 0;
5808
365k
  if (discriminator_ptr)
5809
8.83k
    *discriminator_ptr = 0;
5810
5811
365k
  if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
5812
365k
              symbols, pinfo,
5813
365k
              (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
5814
253k
    return false;
5815
5816
111k
  stash = (struct dwarf2_debug *) *pinfo;
5817
5818
111k
  if (stash->alt.bfd_ptr == NULL && alt_filename != NULL)
5819
0
    {
5820
0
      bfd *alt_bfd = bfd_openr (alt_filename, NULL);
5821
5822
0
      if (alt_bfd == NULL)
5823
  /* bfd_openr will have set the bfd_error.  */
5824
0
  return false;
5825
0
      if (!bfd_check_format (alt_bfd, bfd_object))
5826
0
  {
5827
0
    bfd_set_error (bfd_error_wrong_format);
5828
0
    bfd_close (alt_bfd);
5829
0
    return false;
5830
0
  }
5831
5832
0
      stash->alt.bfd_ptr = alt_bfd;
5833
0
    }
5834
5835
111k
  do_line = symbol != NULL;
5836
111k
  if (do_line)
5837
27.8k
    {
5838
27.8k
      BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
5839
27.8k
      section = bfd_asymbol_section (symbol);
5840
27.8k
      addr = symbol->value;
5841
27.8k
    }
5842
83.9k
  else
5843
83.9k
    {
5844
83.9k
      BFD_ASSERT (section != NULL && functionname_ptr != NULL);
5845
83.9k
      addr = offset;
5846
5847
      /* If we have no SYMBOL but the section we're looking at is not a
5848
   code section, then take a look through the list of symbols to see
5849
   if we have a symbol at the address we're looking for.  If we do
5850
   then use this to look up line information.  This will allow us to
5851
   give file and line results for data symbols.  We exclude code
5852
   symbols here, if we look up a function symbol and then look up the
5853
   line information we'll actually return the line number for the
5854
   opening '{' rather than the function definition line.  This is
5855
   because looking up by symbol uses the line table, in which the
5856
   first line for a function is usually the opening '{', while
5857
   looking up the function by section + offset uses the
5858
   DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5859
   which will be the line of the function name.  */
5860
83.9k
      if (symbols != NULL && (section->flags & SEC_CODE) == 0)
5861
38.4k
  {
5862
38.4k
    asymbol **tmp;
5863
5864
12.3M
    for (tmp = symbols; (*tmp) != NULL; ++tmp)
5865
12.3M
      if ((*tmp)->the_bfd == abfd
5866
12.3M
    && (*tmp)->section == section
5867
12.3M
    && (*tmp)->value == offset
5868
12.3M
    && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
5869
33.7k
        {
5870
33.7k
    symbol = *tmp;
5871
33.7k
    do_line = true;
5872
    /* For local symbols, keep going in the hope we find a
5873
       global.  */
5874
33.7k
    if ((symbol->flags & BSF_GLOBAL) != 0)
5875
1.97k
      break;
5876
33.7k
        }
5877
38.4k
  }
5878
83.9k
    }
5879
5880
111k
  if (section->output_section)
5881
0
    addr += section->output_section->vma + section->output_offset;
5882
111k
  else
5883
111k
    addr += section->vma;
5884
5885
  /* A null info_ptr indicates that there is no dwarf2 info
5886
     (or that an error occured while setting up the stash).  */
5887
111k
  if (! stash->f.info_ptr)
5888
0
    return false;
5889
5890
111k
  stash->inliner_chain = NULL;
5891
5892
  /* Check the previously read comp. units first.  */
5893
111k
  if (do_line)
5894
44.9k
    {
5895
      /* The info hash tables use quite a bit of memory.  We may not want to
5896
   always use them.  We use some heuristics to decide if and when to
5897
   turn it on.  */
5898
44.9k
      if (stash->info_hash_status == STASH_INFO_HASH_OFF)
5899
15.8k
  stash_maybe_enable_info_hash_tables (abfd, stash);
5900
5901
      /* Keep info hash table up to date if they are available.  Note that we
5902
   may disable the hash tables if there is any error duing update.  */
5903
44.9k
      if (stash->info_hash_status == STASH_INFO_HASH_ON)
5904
16.4k
  stash_maybe_update_info_hash_tables (stash);
5905
5906
44.9k
      if (stash->info_hash_status == STASH_INFO_HASH_ON)
5907
16.4k
  {
5908
16.4k
    found = stash_find_line_fast (stash, symbol, addr,
5909
16.4k
          filename_ptr, linenumber_ptr);
5910
16.4k
    if (found)
5911
1.03k
      goto done;
5912
16.4k
  }
5913
5914
      /* Check the previously read comp. units first.  */
5915
710k
      for (each = stash->f.all_comp_units; each; each = each->next_unit)
5916
669k
  if ((symbol->flags & BSF_FUNCTION) == 0
5917
669k
      || comp_unit_may_contain_address (each, addr))
5918
442k
    {
5919
442k
      found = comp_unit_find_line (each, symbol, addr, filename_ptr,
5920
442k
           linenumber_ptr);
5921
442k
      if (found)
5922
2.12k
        goto done;
5923
442k
    }
5924
43.8k
    }
5925
66.8k
  else
5926
66.8k
    {
5927
66.8k
      struct trie_node *trie = stash->f.trie_root;
5928
66.8k
      unsigned int bits = VMA_BITS - 8;
5929
66.8k
      struct comp_unit **prev_each;
5930
5931
      /* Traverse interior nodes until we get to a leaf.  */
5932
332k
      while (trie && trie->num_room_in_leaf == 0)
5933
265k
  {
5934
265k
    int ch = (addr >> bits) & 0xff;
5935
265k
    trie = ((struct trie_interior *) trie)->children[ch];
5936
265k
    bits -= 8;
5937
265k
  }
5938
5939
66.8k
      if (trie)
5940
58.4k
  {
5941
58.4k
    const struct trie_leaf *leaf = (struct trie_leaf *) trie;
5942
58.4k
    unsigned int i;
5943
5944
170k
    for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5945
112k
      leaf->ranges[i].unit->mark = false;
5946
5947
141k
    for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5948
91.5k
      {
5949
91.5k
        struct comp_unit *unit = leaf->ranges[i].unit;
5950
91.5k
        if (unit->mark
5951
91.5k
      || addr < leaf->ranges[i].low_pc
5952
91.5k
      || addr >= leaf->ranges[i].high_pc)
5953
50.4k
          continue;
5954
41.1k
        unit->mark = true;
5955
5956
41.1k
        found = comp_unit_find_nearest_line (unit, addr,
5957
41.1k
               filename_ptr,
5958
41.1k
               &function,
5959
41.1k
               linenumber_ptr,
5960
41.1k
               discriminator_ptr);
5961
41.1k
        if (found)
5962
8.81k
    goto done;
5963
41.1k
     }
5964
58.4k
  }
5965
5966
      /* Also scan through all compilation units without any ranges,
5967
         taking them out of the list if they have acquired any since
5968
   last time.  */
5969
58.0k
      prev_each = &stash->f.all_comp_units_without_ranges;
5970
58.0k
      for (each = *prev_each; each; each = each->next_unit_without_ranges)
5971
0
        {
5972
0
    if (each->arange.high != 0)
5973
0
      {
5974
0
        *prev_each = each->next_unit_without_ranges;
5975
0
        continue;
5976
0
      }
5977
5978
0
    found = comp_unit_find_nearest_line (each, addr,
5979
0
                 filename_ptr,
5980
0
                 &function,
5981
0
                 linenumber_ptr,
5982
0
                 discriminator_ptr);
5983
0
    if (found)
5984
0
      goto done;
5985
0
    prev_each = &each->next_unit_without_ranges;
5986
0
  }
5987
58.0k
    }
5988
5989
  /* Read each remaining comp. units checking each as they are read.  */
5990
101k
  while ((each = stash_comp_unit (stash, &stash->f)) != NULL)
5991
2.29k
    {
5992
      /* DW_AT_low_pc and DW_AT_high_pc are optional for
5993
   compilation units.  If we don't have them (i.e.,
5994
   unit->high == 0), we need to consult the line info table
5995
   to see if a compilation unit contains the given
5996
   address.  */
5997
2.29k
      if (do_line)
5998
1.77k
  found = (((symbol->flags & BSF_FUNCTION) == 0
5999
1.77k
      || comp_unit_may_contain_address (each, addr))
6000
1.77k
     && comp_unit_find_line (each, symbol, addr,
6001
1.77k
           filename_ptr, linenumber_ptr));
6002
517
      else
6003
517
  found = (comp_unit_may_contain_address (each, addr)
6004
517
     && comp_unit_find_nearest_line (each, addr,
6005
517
             filename_ptr,
6006
517
             &function,
6007
517
             linenumber_ptr,
6008
517
             discriminator_ptr));
6009
6010
2.29k
      if (found)
6011
148
  break;
6012
2.29k
    }
6013
6014
111k
 done:
6015
111k
  if (functionname_ptr && function && function->is_linkage)
6016
7.29k
    {
6017
7.29k
      *functionname_ptr = function->name;
6018
7.29k
      if (!found)
6019
0
        found = 2;
6020
7.29k
    }
6021
104k
  else if (functionname_ptr
6022
104k
     && (!*functionname_ptr
6023
76.7k
         || (function && !function->is_linkage)))
6024
76.7k
    {
6025
76.7k
      asymbol *fun;
6026
76.7k
      asymbol **syms = symbols;
6027
76.7k
      asection *sec = section;
6028
6029
76.7k
      _bfd_dwarf2_stash_syms (stash, abfd, &sec, &syms);
6030
76.7k
      fun = _bfd_elf_find_function (abfd, syms, sec, offset,
6031
76.7k
            *filename_ptr ? NULL : filename_ptr,
6032
76.7k
            functionname_ptr);
6033
6034
76.7k
      if (!found && fun != NULL)
6035
12.4k
  found = 2;
6036
6037
76.7k
      if (function && !function->is_linkage)
6038
50
  {
6039
50
    bfd_vma sec_vma;
6040
6041
50
    sec_vma = section->vma;
6042
50
    if (section->output_section != NULL)
6043
0
      sec_vma = section->output_section->vma + section->output_offset;
6044
50
    if (fun == NULL)
6045
28
      *functionname_ptr = function->name;
6046
22
    else if (fun->value + sec_vma == function->arange.low)
6047
14
      function->name = *functionname_ptr;
6048
    /* Even if we didn't find a linkage name, say that we have
6049
       to stop a repeated search of symbols.  */
6050
50
    function->is_linkage = true;
6051
50
  }
6052
76.7k
    }
6053
6054
111k
  unset_sections (stash);
6055
6056
111k
  return found;
6057
99.8k
}
6058
6059
bool
6060
_bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
6061
             const char **filename_ptr,
6062
             const char **functionname_ptr,
6063
             unsigned int *linenumber_ptr,
6064
             void **pinfo)
6065
0
{
6066
0
  struct dwarf2_debug *stash;
6067
6068
0
  stash = (struct dwarf2_debug *) *pinfo;
6069
0
  if (stash)
6070
0
    {
6071
0
      struct funcinfo *func = stash->inliner_chain;
6072
6073
0
      if (func && func->caller_func)
6074
0
  {
6075
0
    *filename_ptr = func->caller_file;
6076
0
    *functionname_ptr = func->caller_func->name;
6077
0
    *linenumber_ptr = func->caller_line;
6078
0
    stash->inliner_chain = func->caller_func;
6079
0
    return true;
6080
0
  }
6081
0
    }
6082
6083
0
  return false;
6084
0
}
6085
6086
void
6087
_bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
6088
886k
{
6089
886k
  struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
6090
886k
  struct comp_unit *each;
6091
886k
  struct dwarf2_debug_file *file;
6092
6093
886k
  if (abfd == NULL || stash == NULL)
6094
882k
    return;
6095
6096
3.79k
  if (stash->varinfo_hash_table)
6097
97
    bfd_hash_table_free (&stash->varinfo_hash_table->base);
6098
3.79k
  if (stash->funcinfo_hash_table)
6099
97
    bfd_hash_table_free (&stash->funcinfo_hash_table->base);
6100
6101
3.79k
  file = &stash->f;
6102
7.59k
  while (1)
6103
7.59k
    {
6104
9.88k
      for (each = file->all_comp_units; each; each = each->next_unit)
6105
2.29k
  {
6106
2.29k
    struct funcinfo *function_table = each->function_table;
6107
2.29k
    struct varinfo *variable_table = each->variable_table;
6108
6109
2.29k
    if (each->line_table && each->line_table != file->line_table)
6110
1.18k
      {
6111
1.18k
        free (each->line_table->files);
6112
1.18k
        free (each->line_table->dirs);
6113
1.18k
      }
6114
6115
2.29k
    free (each->lookup_funcinfo_table);
6116
2.29k
    each->lookup_funcinfo_table = NULL;
6117
6118
34.1k
    while (function_table)
6119
31.9k
      {
6120
31.9k
        free (function_table->file);
6121
31.9k
        function_table->file = NULL;
6122
31.9k
        free (function_table->caller_file);
6123
31.9k
        function_table->caller_file = NULL;
6124
31.9k
        function_table = function_table->prev_func;
6125
31.9k
      }
6126
6127
44.1k
    while (variable_table)
6128
41.8k
      {
6129
41.8k
        free (variable_table->file);
6130
41.8k
        variable_table->file = NULL;
6131
41.8k
        variable_table = variable_table->prev_var;
6132
41.8k
      }
6133
2.29k
  }
6134
6135
7.59k
      if (file->line_table)
6136
327
  {
6137
327
    free (file->line_table->files);
6138
327
    free (file->line_table->dirs);
6139
327
  }
6140
7.59k
      htab_delete (file->abbrev_offsets);
6141
7.59k
      if (file->comp_unit_tree != NULL)
6142
480
  splay_tree_delete (file->comp_unit_tree);
6143
6144
7.59k
      free (file->dwarf_line_str_buffer);
6145
7.59k
      free (file->dwarf_str_buffer);
6146
7.59k
      free (file->dwarf_ranges_buffer);
6147
7.59k
      free (file->dwarf_rnglists_buffer);
6148
7.59k
      free (file->dwarf_line_buffer);
6149
7.59k
      free (file->dwarf_abbrev_buffer);
6150
7.59k
      free (file->dwarf_info_buffer);
6151
7.59k
      free (file->dwarf_addr_buffer);
6152
7.59k
      free (file->dwarf_str_offsets_buffer);
6153
7.59k
      if (file == &stash->alt)
6154
3.79k
  break;
6155
3.79k
      file = &stash->alt;
6156
3.79k
    }
6157
3.79k
  free (stash->sec_vma);
6158
3.79k
  free (stash->adjusted_sections);
6159
3.79k
  if (stash->close_on_cleanup)
6160
0
    bfd_close (stash->f.bfd_ptr);
6161
3.79k
  if (stash->alt.bfd_ptr)
6162
0
    bfd_close (stash->alt.bfd_ptr);
6163
3.79k
}
6164
6165
typedef struct elf_find_function_cache
6166
{
6167
  asection *     last_section;
6168
  asymbol *      func;
6169
  const char *   filename;
6170
  bfd_size_type  code_size;
6171
  bfd_vma        code_off;
6172
6173
} elf_find_function_cache;
6174
6175
6176
/* Returns TRUE if symbol SYM with address CODE_OFF and size CODE_SIZE
6177
   is a better fit to match OFFSET than whatever is currenly stored in
6178
   CACHE.  */
6179
6180
static inline bool
6181
better_fit (elf_find_function_cache *  cache,
6182
      asymbol *                  sym,
6183
      bfd_vma                    code_off,
6184
      bfd_size_type              code_size,
6185
      bfd_vma                    offset)
6186
2.44M
{
6187
  /* If the symbol is beyond the desired offset, ignore it.  */
6188
2.44M
  if (code_off > offset)
6189
1.18M
    return false;
6190
6191
  /* If the symbol is further away from the desired
6192
     offset than our current best, then ignore it.  */
6193
1.25M
  if (code_off < cache->code_off)
6194
1.01M
    return false;
6195
6196
  /* On the other hand, if it is closer, then use it.  */
6197
237k
  if (code_off > cache->code_off)
6198
221k
    return true;
6199
6200
  /* assert (code_off == cache->code_off);  */
6201
6202
  /* If our current best fit does not actually reach the desired
6203
     offset...  */
6204
16.5k
  if (cache->code_off + cache->code_size <= offset)
6205
    /* ... then return whichever candidate covers
6206
       more area and hence gets closer to OFFSET.  */
6207
14.4k
    return code_size > cache->code_size;
6208
6209
  /* The current cache'd symbol covers OFFSET.  */
6210
6211
  /* If the new symbol does not cover the desired offset then skip it.  */  
6212
2.08k
  if (code_off + code_size <= offset)
6213
85
    return false;
6214
6215
  /* Both symbols cover OFFSET.  */
6216
6217
  /* Prefer functions over non-functions.  */
6218
2.00k
  flagword cache_flags = cache->func->flags;
6219
2.00k
  flagword sym_flags   = sym->flags;
6220
6221
2.00k
  if ((cache_flags & BSF_FUNCTION) && ((sym_flags & BSF_FUNCTION) == 0))
6222
185
    return false;
6223
1.81k
  if ((sym_flags & BSF_FUNCTION) && ((cache_flags & BSF_FUNCTION) == 0))
6224
56
    return true;
6225
6226
  /* FIXME: Should we choose LOCAL over GLOBAL ?  */
6227
6228
  /* Prefer typed symbols over notyped.  */
6229
1.76k
  int cache_type = ELF_ST_TYPE (((elf_symbol_type *) cache->func)->internal_elf_sym.st_info);
6230
1.76k
  int sym_type   = ELF_ST_TYPE (((elf_symbol_type *) sym)->internal_elf_sym.st_info);
6231
6232
1.76k
  if (cache_type == STT_NOTYPE && sym_type != STT_NOTYPE)
6233
53
    return true;
6234
1.71k
  if (cache_type != STT_NOTYPE && sym_type == STT_NOTYPE)
6235
269
    return false;
6236
6237
  /* Otherwise choose whichever symbol covers a smaller area.  */
6238
1.44k
  return code_size < cache->code_size;
6239
1.71k
}
6240
6241
/* Find the function to a particular section and offset,
6242
   for error reporting.  */
6243
6244
asymbol *
6245
_bfd_elf_find_function (bfd *abfd,
6246
      asymbol **symbols,
6247
      asection *section,
6248
      bfd_vma offset,
6249
      const char **filename_ptr,
6250
      const char **functionname_ptr)
6251
187k
{
6252
187k
  if (symbols == NULL)
6253
796
    return NULL;
6254
6255
186k
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6256
2.33k
    return NULL;
6257
6258
184k
  elf_find_function_cache * cache = elf_tdata (abfd)->elf_find_function_cache;
6259
6260
184k
  if (cache == NULL)
6261
967
    {
6262
967
      cache = bfd_zalloc (abfd, sizeof (*cache));
6263
967
      elf_tdata (abfd)->elf_find_function_cache = cache;
6264
967
      if (cache == NULL)
6265
0
  return NULL;
6266
967
    }
6267
6268
184k
  if (cache->last_section != section
6269
184k
      || cache->func == NULL
6270
184k
      || offset < cache->func->value
6271
184k
      || offset >= cache->func->value + cache->code_size)
6272
181k
    {
6273
181k
      asymbol *file;
6274
181k
      asymbol **p;
6275
      /* ??? Given multiple file symbols, it is impossible to reliably
6276
   choose the right file name for global symbols.  File symbols are
6277
   local symbols, and thus all file symbols must sort before any
6278
   global symbols.  The ELF spec may be interpreted to say that a
6279
   file symbol must sort before other local symbols, but currently
6280
   ld -r doesn't do this.  So, for ld -r output, it is possible to
6281
   make a better choice of file name for local symbols by ignoring
6282
   file symbols appearing after a given local symbol.  */
6283
181k
      enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
6284
181k
      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6285
6286
181k
      file = NULL;
6287
181k
      state = nothing_seen;
6288
181k
      cache->filename = NULL;
6289
181k
      cache->func = NULL;
6290
181k
      cache->code_size = 0;
6291
181k
      cache->code_off = 0;
6292
181k
      cache->last_section = section;
6293
6294
63.7M
      for (p = symbols; *p != NULL; p++)
6295
63.5M
  {
6296
63.5M
    asymbol *sym = *p;
6297
63.5M
    bfd_vma code_off;
6298
63.5M
    bfd_size_type size;
6299
6300
63.5M
    if ((sym->flags & BSF_FILE) != 0)
6301
1.15M
      {
6302
1.15M
        file = sym;
6303
1.15M
        if (state == symbol_seen)
6304
36.5k
    state = file_after_symbol_seen;
6305
1.15M
        continue;
6306
1.15M
      }
6307
6308
62.3M
    if (state == nothing_seen)
6309
181k
      state = symbol_seen;
6310
6311
62.3M
    size = bed->maybe_function_sym (sym, section, &code_off);
6312
6313
62.3M
    if (size == 0)
6314
59.9M
      continue;
6315
6316
2.44M
    if (better_fit (cache, sym, code_off, size, offset))
6317
231k
      {
6318
231k
        cache->func = sym;
6319
231k
        cache->code_size = size;
6320
231k
        cache->code_off = code_off;
6321
231k
        cache->filename = NULL;
6322
6323
231k
        if (file != NULL
6324
231k
      && ((sym->flags & BSF_LOCAL) != 0
6325
212k
          || state != file_after_symbol_seen))
6326
196k
    cache->filename = bfd_asymbol_name (file);
6327
231k
      }
6328
    /* Otherwise, if the symbol is beyond the desired offset but it
6329
       lies within the bounds of the current best match then reduce
6330
       the size of the current best match so that future searches
6331
       will not not used the cached symbol by mistake.  */
6332
2.21M
    else if (code_off > offset 
6333
2.21M
       && code_off > cache->code_off
6334
2.21M
       && code_off < cache->code_off + cache->code_size)
6335
701
      {
6336
701
        cache->code_size = code_off - cache->code_off;
6337
701
      }
6338
2.44M
  }
6339
181k
    }
6340
6341
184k
  if (cache->func == NULL)
6342
156k
    return NULL;
6343
6344
27.4k
  if (filename_ptr)
6345
25.8k
    *filename_ptr = cache->filename;
6346
27.4k
  if (functionname_ptr)
6347
27.4k
    *functionname_ptr = bfd_asymbol_name (cache->func);
6348
6349
27.4k
  return cache->func;
6350
184k
}