Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / dwarf2 / abbrev.c
CommitLineData
3054dd54
TT
1/* DWARF 2 abbreviations
2
88b9d363 3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
3054dd54
TT
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27#include "defs.h"
82ca8957 28#include "dwarf2/read.h"
3054dd54
TT
29#include "dwarf2/abbrev.h"
30#include "dwarf2/leb.h"
31#include "bfd.h"
32
1d33d811
TT
33/* Hash function for an abbrev. */
34
35static hashval_t
36hash_abbrev (const void *item)
37{
38 const struct abbrev_info *info = (const struct abbrev_info *) item;
af0b2a3e
TT
39 /* Warning: if you change this next line, you must also update the
40 other code in this class using the _with_hash functions. */
1d33d811
TT
41 return info->number;
42}
43
44/* Comparison function for abbrevs. */
45
46static int
47eq_abbrev (const void *lhs, const void *rhs)
48{
49 const struct abbrev_info *l_info = (const struct abbrev_info *) lhs;
50 const struct abbrev_info *r_info = (const struct abbrev_info *) rhs;
51 return l_info->number == r_info->number;
52}
53
3054dd54
TT
54/* Abbreviation tables.
55
56 In DWARF version 2, the description of the debugging information is
57 stored in a separate .debug_abbrev section. Before we read any
58 dies from a section we read in all abbreviations and install them
59 in a hash table. */
60
1d33d811
TT
61abbrev_table::abbrev_table (sect_offset off)
62 : sect_off (off),
63 m_abbrevs (htab_create_alloc (20, hash_abbrev, eq_abbrev,
64 nullptr, xcalloc, xfree))
65{
66}
67
3054dd54
TT
68/* Add an abbreviation to the table. */
69
70void
af0b2a3e 71abbrev_table::add_abbrev (struct abbrev_info *abbrev)
3054dd54 72{
af0b2a3e
TT
73 void **slot = htab_find_slot_with_hash (m_abbrevs.get (), abbrev,
74 abbrev->number, INSERT);
1d33d811 75 *slot = abbrev;
3054dd54
TT
76}
77
3054dd54
TT
78/* Read in an abbrev table. */
79
80abbrev_table_up
606decb2 81abbrev_table::read (struct dwarf2_section_info *section,
86de1d91 82 sect_offset sect_off)
3054dd54
TT
83{
84 bfd *abfd = section->get_bfd_owner ();
85 const gdb_byte *abbrev_ptr;
86 struct abbrev_info *cur_abbrev;
3054dd54
TT
87
88 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
4444f407 89 struct obstack *obstack = &abbrev_table->m_abbrev_obstack;
3054dd54 90
606decb2
TT
91 /* Caller must ensure this. */
92 gdb_assert (section->readin);
3054dd54 93 abbrev_ptr = section->buffer + to_underlying (sect_off);
3054dd54 94
27012aba 95 while (true)
3054dd54 96 {
27012aba
TT
97 unsigned int bytes_read;
98 /* Loop until we reach an abbrev number of 0. */
99 unsigned int abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr,
100 &bytes_read);
101 if (abbrev_number == 0)
102 break;
103 abbrev_ptr += bytes_read;
104
4444f407
TT
105 /* Start without any attrs. */
106 obstack_blank (obstack, offsetof (abbrev_info, attrs));
107 cur_abbrev = (struct abbrev_info *) obstack_base (obstack);
3054dd54 108
4444f407 109 /* Read in abbrev header. */
3054dd54
TT
110 cur_abbrev->number = abbrev_number;
111 cur_abbrev->tag
4444f407
TT
112 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr,
113 &bytes_read);
3054dd54
TT
114 abbrev_ptr += bytes_read;
115 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
116 abbrev_ptr += 1;
117
4444f407
TT
118 /* Now read in declarations. */
119 int num_attrs = 0;
3054dd54
TT
120 for (;;)
121 {
4444f407 122 struct attr_abbrev cur_attr;
3054dd54 123
4444f407
TT
124 cur_attr.name
125 = (enum dwarf_attribute) read_unsigned_leb128 (abfd, abbrev_ptr,
126 &bytes_read);
3054dd54 127 abbrev_ptr += bytes_read;
4444f407
TT
128 cur_attr.form
129 = (enum dwarf_form) read_unsigned_leb128 (abfd, abbrev_ptr,
130 &bytes_read);
3054dd54 131 abbrev_ptr += bytes_read;
4444f407 132 if (cur_attr.form == DW_FORM_implicit_const)
3054dd54 133 {
4444f407
TT
134 cur_attr.implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
135 &bytes_read);
3054dd54
TT
136 abbrev_ptr += bytes_read;
137 }
138 else
4444f407 139 cur_attr.implicit_const = -1;
3054dd54 140
4444f407 141 if (cur_attr.name == 0)
3054dd54
TT
142 break;
143
4444f407
TT
144 ++num_attrs;
145 obstack_grow (obstack, &cur_attr, sizeof (cur_attr));
3054dd54
TT
146 }
147
4444f407
TT
148 cur_abbrev = (struct abbrev_info *) obstack_finish (obstack);
149 cur_abbrev->num_attrs = num_attrs;
af0b2a3e 150 abbrev_table->add_abbrev (cur_abbrev);
3054dd54
TT
151 }
152
153 return abbrev_table;
154}
This page took 0.182006 seconds and 4 git commands to generate.