Coverage Report

Created: 2026-11-21 09:00

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