Coverage Report

Created: 2023-06-29 07:06

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