Coverage Report

Created: 2025-06-24 06:45

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