-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdwarf2.c
More file actions
6326 lines (5444 loc) · 171 KB
/
dwarf2.c
File metadata and controls
6326 lines (5444 loc) · 171 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* DWARF 2 support.
Copyright (C) 1994-2023 Free Software Foundation, Inc.
Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
(gavin@cygnus.com).
From the dwarf2read.c header:
Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
Inc. with support from Florida State University (under contract
with the Ada Joint Program Office), and Silicon Graphics, Inc.
Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
based on Fred Fish's (Cygnus Support) implementation of DWARF 1
support in dwarfread.c
This file is part of BFD.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "sysdep.h"
#include "bfd.h"
#include "libiberty.h"
#include "demangle.h"
#include "libbfd.h"
#include "elf-bfd.h"
#include "dwarf2.h"
#include "hashtab.h"
#include "splay-tree.h"
/* The data in the .debug_line statement prologue looks like this. */
struct line_head
{
bfd_vma total_length;
unsigned short version;
bfd_vma prologue_length;
unsigned char minimum_instruction_length;
unsigned char maximum_ops_per_insn;
unsigned char default_is_stmt;
int line_base;
unsigned char line_range;
unsigned char opcode_base;
unsigned char *standard_opcode_lengths;
};
/* Attributes have a name and a value. */
struct attribute
{
enum dwarf_attribute name;
enum dwarf_form form;
union
{
char *str;
struct dwarf_block *blk;
uint64_t val;
int64_t sval;
}
u;
};
/* Blocks are a bunch of untyped bytes. */
struct dwarf_block
{
unsigned int size;
bfd_byte *data;
};
struct adjusted_section
{
asection *section;
bfd_vma adj_vma;
bfd_vma orig_vma;
};
/* A trie to map quickly from address range to compilation unit.
This is a fairly standard radix-256 trie, used to quickly locate which
compilation unit any given address belongs to. Given that each compilation
unit may register hundreds of very small and unaligned ranges (which may
potentially overlap, due to inlining and other concerns), and a large
program may end up containing hundreds of thousands of such ranges, we cannot
scan through them linearly without undue slowdown.
We use a hybrid trie to avoid memory explosion: There are two types of trie
nodes, leaves and interior nodes. (Almost all nodes are leaves, so they
take up the bulk of the memory usage.) Leaves contain a simple array of
ranges (high/low address) and which compilation unit contains those ranges,
and when we get to a leaf, we scan through it linearly. Interior nodes
contain pointers to 256 other nodes, keyed by the next byte of the address.
So for a 64-bit address like 0x1234567abcd, we would start at the root and go
down child[0x00]->child[0x00]->child[0x01]->child[0x23]->child[0x45] etc.,
until we hit a leaf. (Nodes are, in general, leaves until they exceed the
default allocation of 16 elements, at which point they are converted to
interior node if possible.) This gives us near-constant lookup times;
the only thing that can be costly is if there are lots of overlapping ranges
within a single 256-byte segment of the binary, in which case we have to
scan through them all to find the best match.
For a binary with few ranges, we will in practice only have a single leaf
node at the root, containing a simple array. Thus, the scheme is efficient
for both small and large binaries.
*/
/* Experiments have shown 16 to be a memory-efficient default leaf size.
The only case where a leaf will hold more memory than this, is at the
bottomost level (covering 256 bytes in the binary), where we'll expand
the leaf to be able to hold more ranges if needed.
*/
#define TRIE_LEAF_SIZE 16
/* All trie_node pointers will really be trie_leaf or trie_interior,
but they have this common head. */
struct trie_node
{
/* If zero, we are an interior node.
Otherwise, how many ranges we have room for in this leaf. */
unsigned int num_room_in_leaf;
};
struct trie_leaf
{
struct trie_node head;
unsigned int num_stored_in_leaf;
struct {
struct comp_unit *unit;
bfd_vma low_pc, high_pc;
} ranges[];
};
struct trie_interior
{
struct trie_node head;
struct trie_node *children[256];
};
static struct trie_node *alloc_trie_leaf (bfd *abfd)
{
struct trie_leaf *leaf;
size_t amt = sizeof (*leaf) + TRIE_LEAF_SIZE * sizeof (leaf->ranges[0]);
leaf = bfd_zalloc (abfd, amt);
if (leaf == NULL)
return NULL;
leaf->head.num_room_in_leaf = TRIE_LEAF_SIZE;
return &leaf->head;
}
struct addr_range
{
bfd_byte *start;
bfd_byte *end;
};
/* Return true if address range do intersect. */
static bool
addr_range_intersects (struct addr_range *r1, struct addr_range *r2)
{
return (r1->start <= r2->start && r2->start < r1->end)
|| (r1->start <= (r2->end - 1) && (r2->end - 1) < r1->end);
}
/* Compare function for splay tree of addr_ranges. */
static int
splay_tree_compare_addr_range (splay_tree_key xa, splay_tree_key xb)
{
struct addr_range *r1 = (struct addr_range *) xa;
struct addr_range *r2 = (struct addr_range *) xb;
if (addr_range_intersects (r1, r2) || addr_range_intersects (r2, r1))
return 0;
else if (r1->end <= r2->start)
return -1;
else
return 1;
}
/* Splay tree release function for keys (addr_range). */
static void
splay_tree_free_addr_range (splay_tree_key key)
{
free ((struct addr_range *)key);
}
struct dwarf2_debug_file
{
/* The actual bfd from which debug info was loaded. Might be
different to orig_bfd because of gnu_debuglink sections. */
bfd *bfd_ptr;
/* Pointer to the symbol table. */
asymbol **syms;
/* The current info pointer for the .debug_info section being parsed. */
bfd_byte *info_ptr;
/* A pointer to the memory block allocated for .debug_info sections. */
bfd_byte *dwarf_info_buffer;
/* Length of the loaded .debug_info sections. */
bfd_size_type dwarf_info_size;
/* Pointer to the .debug_abbrev section loaded into memory. */
bfd_byte *dwarf_abbrev_buffer;
/* Length of the loaded .debug_abbrev section. */
bfd_size_type dwarf_abbrev_size;
/* Buffer for decode_line_info. */
bfd_byte *dwarf_line_buffer;
/* Length of the loaded .debug_line section. */
bfd_size_type dwarf_line_size;
/* Pointer to the .debug_str section loaded into memory. */
bfd_byte *dwarf_str_buffer;
/* Length of the loaded .debug_str section. */
bfd_size_type dwarf_str_size;
/* Pointer to the .debug_str_offsets section loaded into memory. */
bfd_byte *dwarf_str_offsets_buffer;
/* Length of the loaded .debug_str_offsets section. */
bfd_size_type dwarf_str_offsets_size;
/* Pointer to the .debug_addr section loaded into memory. */
bfd_byte *dwarf_addr_buffer;
/* Length of the loaded .debug_addr section. */
bfd_size_type dwarf_addr_size;
/* Pointer to the .debug_line_str section loaded into memory. */
bfd_byte *dwarf_line_str_buffer;
/* Length of the loaded .debug_line_str section. */
bfd_size_type dwarf_line_str_size;
/* Pointer to the .debug_ranges section loaded into memory. */
bfd_byte *dwarf_ranges_buffer;
/* Length of the loaded .debug_ranges section. */
bfd_size_type dwarf_ranges_size;
/* Pointer to the .debug_rnglists section loaded into memory. */
bfd_byte *dwarf_rnglists_buffer;
/* Length of the loaded .debug_rnglists section. */
bfd_size_type dwarf_rnglists_size;
/* A list of all previously read comp_units. */
struct comp_unit *all_comp_units;
/* A list of all previously read comp_units with no ranges (yet). */
struct comp_unit *all_comp_units_without_ranges;
/* Last comp unit in list above. */
struct comp_unit *last_comp_unit;
/* Line table at line_offset zero. */
struct line_info_table *line_table;
/* Hash table to map offsets to decoded abbrevs. */
htab_t abbrev_offsets;
/* Root of a trie to map addresses to compilation units. */
struct trie_node *trie_root;
/* Splay tree to map info_ptr address to compilation units. */
splay_tree comp_unit_tree;
};
struct dwarf2_debug
{
/* Names of the debug sections. */
const struct dwarf_debug_section *debug_sections;
/* Per-file stuff. */
struct dwarf2_debug_file f, alt;
/* Pointer to the original bfd for which debug was loaded. This is what
we use to compare and so check that the cached debug data is still
valid - it saves having to possibly dereference the gnu_debuglink each
time. */
bfd *orig_bfd;
/* If the most recent call to bfd_find_nearest_line was given an
address in an inlined function, preserve a pointer into the
calling chain for subsequent calls to bfd_find_inliner_info to
use. */
struct funcinfo *inliner_chain;
/* Section VMAs at the time the stash was built. */
bfd_vma *sec_vma;
/* Number of sections in the SEC_VMA table. */
unsigned int sec_vma_count;
/* Number of sections whose VMA we must adjust. */
int adjusted_section_count;
/* Array of sections with adjusted VMA. */
struct adjusted_section *adjusted_sections;
/* Number of times find_line is called. This is used in
the heuristic for enabling the info hash tables. */
int info_hash_count;
#define STASH_INFO_HASH_TRIGGER 100
/* Hash table mapping symbol names to function infos. */
struct info_hash_table *funcinfo_hash_table;
/* Hash table mapping symbol names to variable infos. */
struct info_hash_table *varinfo_hash_table;
/* Head of comp_unit list in the last hash table update. */
struct comp_unit *hash_units_head;
/* Status of info hash. */
int info_hash_status;
#define STASH_INFO_HASH_OFF 0
#define STASH_INFO_HASH_ON 1
#define STASH_INFO_HASH_DISABLED 2
/* True if we opened bfd_ptr. */
bool close_on_cleanup;
};
struct arange
{
struct arange *next;
bfd_vma low;
bfd_vma high;
};
/* A minimal decoding of DWARF2 compilation units. We only decode
what's needed to get to the line number information. */
struct comp_unit
{
/* Chain the previously read compilation units. */
struct comp_unit *next_unit;
/* Chain the previously read compilation units that have no ranges yet.
We scan these separately when we have a trie over the ranges.
Unused if arange.high != 0. */
struct comp_unit *next_unit_without_ranges;
/* Likewise, chain the compilation unit read after this one.
The comp units are stored in reversed reading order. */
struct comp_unit *prev_unit;
/* Keep the bfd convenient (for memory allocation). */
bfd *abfd;
/* The lowest and highest addresses contained in this compilation
unit as specified in the compilation unit header. */
struct arange arange;
/* The DW_AT_name attribute (for error messages). */
char *name;
/* The abbrev hash table. */
struct abbrev_info **abbrevs;
/* DW_AT_language. */
int lang;
/* Note that an error was found by comp_unit_find_nearest_line. */
int error;
/* The DW_AT_comp_dir attribute. */
char *comp_dir;
/* TRUE if there is a line number table associated with this comp. unit. */
int stmtlist;
/* Pointer to the current comp_unit so that we can find a given entry
by its reference. */
bfd_byte *info_ptr_unit;
/* The offset into .debug_line of the line number table. */
unsigned long long line_offset;
/* Pointer to the first child die for the comp unit. */
bfd_byte *first_child_die_ptr;
/* The end of the comp unit. */
bfd_byte *end_ptr;
/* The decoded line number, NULL if not yet decoded. */
struct line_info_table *line_table;
/* A list of the functions found in this comp. unit. */
struct funcinfo *function_table;
/* A table of function information references searchable by address. */
struct lookup_funcinfo *lookup_funcinfo_table;
/* Number of functions in the function_table and sorted_function_table. */
bfd_size_type number_of_functions;
/* A list of the variables found in this comp. unit. */
struct varinfo *variable_table;
/* Pointers to dwarf2_debug structures. */
struct dwarf2_debug *stash;
struct dwarf2_debug_file *file;
/* DWARF format version for this unit - from unit header. */
int version;
/* Address size for this unit - from unit header. */
unsigned char addr_size;
/* Offset size for this unit - from unit header. */
unsigned char offset_size;
/* Base address for this unit - from DW_AT_low_pc attribute of
DW_TAG_compile_unit DIE */
bfd_vma base_address;
/* TRUE if symbols are cached in hash table for faster lookup by name. */
bool cached;
/* Used when iterating over trie leaves to know which units we have
already seen in this iteration. */
bool mark;
/* Base address of debug_addr section. */
size_t dwarf_addr_offset;
/* Base address of string offset table. */
size_t dwarf_str_offset;
};
/* This data structure holds the information of an abbrev. */
struct abbrev_info
{
unsigned int number; /* Number identifying abbrev. */
enum dwarf_tag tag; /* DWARF tag. */
bool has_children; /* TRUE if the abbrev has children. */
unsigned int num_attrs; /* Number of attributes. */
struct attr_abbrev * attrs; /* An array of attribute descriptions. */
struct abbrev_info * next; /* Next in chain. */
};
struct attr_abbrev
{
enum dwarf_attribute name;
enum dwarf_form form;
bfd_vma implicit_const;
};
/* Map of uncompressed DWARF debug section name to compressed one. It
is terminated by NULL uncompressed_name. */
const struct dwarf_debug_section dwarf_debug_sections[] =
{
{ ".debug_abbrev", ".zdebug_abbrev" },
{ ".debug_aranges", ".zdebug_aranges" },
{ ".debug_frame", ".zdebug_frame" },
{ ".debug_info", ".zdebug_info" },
{ ".debug_info", ".zdebug_info" },
{ ".debug_line", ".zdebug_line" },
{ ".debug_loc", ".zdebug_loc" },
{ ".debug_macinfo", ".zdebug_macinfo" },
{ ".debug_macro", ".zdebug_macro" },
{ ".debug_pubnames", ".zdebug_pubnames" },
{ ".debug_pubtypes", ".zdebug_pubtypes" },
{ ".debug_ranges", ".zdebug_ranges" },
{ ".debug_rnglists", ".zdebug_rnglist" },
{ ".debug_static_func", ".zdebug_static_func" },
{ ".debug_static_vars", ".zdebug_static_vars" },
{ ".debug_str", ".zdebug_str", },
{ ".debug_str", ".zdebug_str", },
{ ".debug_str_offsets", ".zdebug_str_offsets", },
{ ".debug_addr", ".zdebug_addr", },
{ ".debug_line_str", ".zdebug_line_str", },
{ ".debug_types", ".zdebug_types" },
/* GNU DWARF 1 extensions */
{ ".debug_sfnames", ".zdebug_sfnames" },
{ ".debug_srcinfo", ".zebug_srcinfo" },
/* SGI/MIPS DWARF 2 extensions */
{ ".debug_funcnames", ".zdebug_funcnames" },
{ ".debug_typenames", ".zdebug_typenames" },
{ ".debug_varnames", ".zdebug_varnames" },
{ ".debug_weaknames", ".zdebug_weaknames" },
{ NULL, NULL },
};
/* NB/ Numbers in this enum must match up with indices
into the dwarf_debug_sections[] array above. */
enum dwarf_debug_section_enum
{
debug_abbrev = 0,
debug_aranges,
debug_frame,
debug_info,
debug_info_alt,
debug_line,
debug_loc,
debug_macinfo,
debug_macro,
debug_pubnames,
debug_pubtypes,
debug_ranges,
debug_rnglists,
debug_static_func,
debug_static_vars,
debug_str,
debug_str_alt,
debug_str_offsets,
debug_addr,
debug_line_str,
debug_types,
debug_sfnames,
debug_srcinfo,
debug_funcnames,
debug_typenames,
debug_varnames,
debug_weaknames,
debug_max
};
/* A static assertion. */
extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
== debug_max + 1 ? 1 : -1];
#ifndef ABBREV_HASH_SIZE
#define ABBREV_HASH_SIZE 121
#endif
#ifndef ATTR_ALLOC_CHUNK
#define ATTR_ALLOC_CHUNK 4
#endif
/* Variable and function hash tables. This is used to speed up look-up
in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
In order to share code between variable and function infos, we use
a list of untyped pointer for all variable/function info associated with
a symbol. We waste a bit of memory for list with one node but that
simplifies the code. */
struct info_list_node
{
struct info_list_node *next;
void *info;
};
/* Info hash entry. */
struct info_hash_entry
{
struct bfd_hash_entry root;
struct info_list_node *head;
};
struct info_hash_table
{
struct bfd_hash_table base;
};
/* Function to create a new entry in info hash table. */
static struct bfd_hash_entry *
info_hash_table_newfunc (struct bfd_hash_entry *entry,
struct bfd_hash_table *table,
const char *string)
{
struct info_hash_entry *ret = (struct info_hash_entry *) entry;
/* Allocate the structure if it has not already been allocated by a
derived class. */
if (ret == NULL)
{
ret = (struct info_hash_entry *) bfd_hash_allocate (table,
sizeof (* ret));
if (ret == NULL)
return NULL;
}
/* Call the allocation method of the base class. */
ret = ((struct info_hash_entry *)
bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
/* Initialize the local fields here. */
if (ret)
ret->head = NULL;
return (struct bfd_hash_entry *) ret;
}
/* Function to create a new info hash table. It returns a pointer to the
newly created table or NULL if there is any error. We need abfd
solely for memory allocation. */
static struct info_hash_table *
create_info_hash_table (bfd *abfd)
{
struct info_hash_table *hash_table;
hash_table = ((struct info_hash_table *)
bfd_alloc (abfd, sizeof (struct info_hash_table)));
if (!hash_table)
return hash_table;
if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
sizeof (struct info_hash_entry)))
{
bfd_release (abfd, hash_table);
return NULL;
}
return hash_table;
}
/* Insert an info entry into an info hash table. We do not check of
duplicate entries. Also, the caller need to guarantee that the
right type of info in inserted as info is passed as a void* pointer.
This function returns true if there is no error. */
static bool
insert_info_hash_table (struct info_hash_table *hash_table,
const char *key,
void *info,
bool copy_p)
{
struct info_hash_entry *entry;
struct info_list_node *node;
entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
key, true, copy_p);
if (!entry)
return false;
node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
sizeof (*node));
if (!node)
return false;
node->info = info;
node->next = entry->head;
entry->head = node;
return true;
}
/* Look up an info entry list from an info hash table. Return NULL
if there is none. */
static struct info_list_node *
lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
{
struct info_hash_entry *entry;
entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
false, false);
return entry ? entry->head : NULL;
}
/* Read a section into its appropriate place in the dwarf2_debug
struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
not NULL, use bfd_simple_get_relocated_section_contents to read the
section contents, otherwise use bfd_get_section_contents. Fail if
the located section does not contain at least OFFSET bytes. */
static bool
read_section (bfd *abfd,
const struct dwarf_debug_section *sec,
asymbol **syms,
uint64_t offset,
bfd_byte **section_buffer,
bfd_size_type *section_size)
{
const char *section_name = sec->uncompressed_name;
bfd_byte *contents = *section_buffer;
/* The section may have already been read. */
if (contents == NULL)
{
bfd_size_type amt;
asection *msec;
msec = bfd_get_section_by_name (abfd, section_name);
if (msec == NULL)
{
section_name = sec->compressed_name;
msec = bfd_get_section_by_name (abfd, section_name);
}
if (msec == NULL)
{
_bfd_error_handler (_("DWARF error: can't find %s section."),
sec->uncompressed_name);
bfd_set_error (bfd_error_bad_value);
return false;
}
if ((msec->flags & SEC_HAS_CONTENTS) == 0)
{
_bfd_error_handler (_("DWARF error: section %s has no contents"),
section_name);
bfd_set_error (bfd_error_no_contents);
return false;
}
if (_bfd_section_size_insane (abfd, msec))
{
/* PR 26946 */
_bfd_error_handler (_("DWARF error: section %s is too big"),
section_name);
return false;
}
amt = bfd_get_section_limit_octets (abfd, msec);
*section_size = amt;
/* Paranoia - alloc one extra so that we can make sure a string
section is NUL terminated. */
amt += 1;
if (amt == 0)
{
/* Paranoia - this should never happen. */
bfd_set_error (bfd_error_no_memory);
return false;
}
contents = (bfd_byte *) bfd_malloc (amt);
if (contents == NULL)
return false;
if (syms
? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
syms)
: !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
{
free (contents);
return false;
}
contents[*section_size] = 0;
*section_buffer = contents;
}
/* It is possible to get a bad value for the offset into the section
that the client wants. Validate it here to avoid trouble later. */
if (offset != 0 && offset >= *section_size)
{
/* xgettext: c-format */
_bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
" greater than or equal to %s size (%" PRIu64 ")"),
(uint64_t) offset, section_name,
(uint64_t) *section_size);
bfd_set_error (bfd_error_bad_value);
return false;
}
return true;
}
/* Read dwarf information from a buffer. */
static inline uint64_t
read_n_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end, int n)
{
bfd_byte *buf = *ptr;
if (end - buf < n)
{
*ptr = end;
return 0;
}
*ptr = buf + n;
return bfd_get (n * 8, abfd, buf);
}
static unsigned int
read_1_byte (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
{
return read_n_bytes (abfd, ptr, end, 1);
}
static int
read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte **ptr, bfd_byte *end)
{
bfd_byte *buf = *ptr;
if (end - buf < 1)
{
*ptr = end;
return 0;
}
*ptr = buf + 1;
return bfd_get_signed_8 (abfd, buf);
}
static unsigned int
read_2_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
{
return read_n_bytes (abfd, ptr, end, 2);
}
static unsigned int
read_3_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
{
unsigned int val = read_1_byte (abfd, ptr, end);
val <<= 8;
val |= read_1_byte (abfd, ptr, end);
val <<= 8;
val |= read_1_byte (abfd, ptr, end);
if (bfd_little_endian (abfd))
val = (((val >> 16) & 0xff)
| (val & 0xff00)
| ((val & 0xff) << 16));
return val;
}
static unsigned int
read_4_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
{
return read_n_bytes (abfd, ptr, end, 4);
}
static uint64_t
read_8_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
{
return read_n_bytes (abfd, ptr, end, 8);
}
static struct dwarf_block *
read_blk (bfd *abfd, bfd_byte **ptr, bfd_byte *end, size_t size)
{
bfd_byte *buf = *ptr;
struct dwarf_block *block;
block = (struct dwarf_block *) bfd_alloc (abfd, sizeof (*block));
if (block == NULL)
return NULL;
if (size > (size_t) (end - buf))
{
*ptr = end;
block->data = NULL;
block->size = 0;
}
else
{
*ptr = buf + size;
block->data = buf;
block->size = size;
}
return block;
}
/* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
Bytes at or beyond BUF_END will not be read. Returns NULL if the
terminator is not found or if the string is empty. *PTR is
incremented over the bytes scanned, including the terminator. */
static char *
read_string (bfd_byte **ptr,
bfd_byte *buf_end)
{
bfd_byte *buf = *ptr;
bfd_byte *str = buf;
while (buf < buf_end)
if (*buf++ == 0)
{
if (str == buf - 1)
break;
*ptr = buf;
return (char *) str;
}
*ptr = buf;
return NULL;
}
/* Reads an offset from *PTR and then locates the string at this offset
inside the debug string section. Returns a pointer to the string.
Increments *PTR by the number of bytes read for the offset. This
value is set even if the function fails. Bytes at or beyond
BUF_END will not be read. Returns NULL if there was a problem, or
if the string is empty. Does not check for NUL termination of the
string. */
static char *
read_indirect_string (struct comp_unit *unit,
bfd_byte **ptr,
bfd_byte *buf_end)
{
uint64_t offset;
struct dwarf2_debug *stash = unit->stash;
struct dwarf2_debug_file *file = unit->file;
char *str;
if (unit->offset_size > (size_t) (buf_end - *ptr))
{
*ptr = buf_end;
return NULL;
}
if (unit->offset_size == 4)
offset = read_4_bytes (unit->abfd, ptr, buf_end);
else
offset = read_8_bytes (unit->abfd, ptr, buf_end);
if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
file->syms, offset,
&file->dwarf_str_buffer, &file->dwarf_str_size))
return NULL;
str = (char *) file->dwarf_str_buffer + offset;
if (*str == '\0')
return NULL;
return str;
}
/* Like read_indirect_string but from .debug_line_str section. */
static char *
read_indirect_line_string (struct comp_unit *unit,
bfd_byte **ptr,
bfd_byte *buf_end)
{
uint64_t offset;
struct dwarf2_debug *stash = unit->stash;
struct dwarf2_debug_file *file = unit->file;
char *str;
if (unit->offset_size > (size_t) (buf_end - *ptr))
{
*ptr = buf_end;
return NULL;
}
if (unit->offset_size == 4)
offset = read_4_bytes (unit->abfd, ptr, buf_end);
else
offset = read_8_bytes (unit->abfd, ptr, buf_end);
if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
file->syms, offset,
&file->dwarf_line_str_buffer,
&file->dwarf_line_str_size))
return NULL;
str = (char *) file->dwarf_line_str_buffer + offset;
if (*str == '\0')
return NULL;
return str;
}
/* Like read_indirect_string but uses a .debug_str located in
an alternate file pointed to by the .gnu_debugaltlink section.
Used to impement DW_FORM_GNU_strp_alt. */
static char *
read_alt_indirect_string (struct comp_unit *unit,
bfd_byte **ptr,
bfd_byte *buf_end)
{
uint64_t offset;
struct dwarf2_debug *stash = unit->stash;
char *str;
if (unit->offset_size > (size_t) (buf_end - *ptr))
{
*ptr = buf_end;
return NULL;
}
if (unit->offset_size == 4)
offset = read_4_bytes (unit->abfd, ptr, buf_end);
else
offset = read_8_bytes (unit->abfd, ptr, buf_end);
if (stash->alt.bfd_ptr == NULL)
{
bfd *debug_bfd;
char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
if (debug_filename == NULL)
return NULL;
debug_bfd = bfd_openr (debug_filename, NULL);
free (debug_filename);
if (debug_bfd == NULL)
/* FIXME: Should we report our failure to follow the debuglink ? */
return NULL;
if (!bfd_check_format (debug_bfd, bfd_object))
{
bfd_close (debug_bfd);