Coverage Report

Created: 2026-10-02 09:53

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