Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / addrmap.h
blobed52e3cd990dfa6cc5df7d646fbc9a3ff568103b
1 /* addrmap.h --- interface to address map data structure.
3 Copyright (C) 2007-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #ifndef ADDRMAP_H
21 #define ADDRMAP_H
23 #include "splay-tree.h"
24 #include "gdbsupport/function-view.h"
26 /* An address map is essentially a table mapping CORE_ADDRs onto GDB
27 data structures, like blocks, symtabs, partial symtabs, and so on.
28 An address map uses memory proportional to the number of
29 transitions in the map, where a CORE_ADDR N is mapped to one
30 object, and N+1 is mapped to a different object.
32 Address maps come in two flavors: fixed, and mutable. Mutable
33 address maps consume more memory, but can be changed and extended.
34 A fixed address map, once constructed (from a mutable address map),
35 can't be edited. */
37 /* The type of a function used to iterate over the map.
38 OBJ is NULL for unmapped regions. */
39 using addrmap_foreach_fn
40 = gdb::function_view<int (CORE_ADDR start_addr, void *obj)>;
41 using addrmap_foreach_const_fn
42 = gdb::function_view<int (CORE_ADDR start_addr, const void *obj)>;
44 /* The base class for addrmaps. */
45 struct addrmap
47 /* Return the object associated with ADDR in MAP. */
48 const void *find (CORE_ADDR addr) const
49 { return this->do_find (addr); }
51 void *find (CORE_ADDR addr)
52 { return this->do_find (addr); }
54 /* Relocate all the addresses in MAP by OFFSET. (This can be applied
55 to either mutable or immutable maps.) */
56 virtual void relocate (CORE_ADDR offset) = 0;
58 /* Call FN for every address in MAP, following an in-order traversal.
59 If FN ever returns a non-zero value, the iteration ceases
60 immediately, and the value is returned. Otherwise, this function
61 returns 0. */
62 int foreach (addrmap_foreach_const_fn fn) const
63 { return this->do_foreach (fn); }
65 int foreach (addrmap_foreach_fn fn)
66 { return this->do_foreach (fn); }
69 protected:
70 ~addrmap () = default;
72 private:
73 /* Worker for find, implemented by sub-classes. */
74 virtual void *do_find (CORE_ADDR addr) const = 0;
76 /* Worker for foreach, implemented by sub-classes. */
77 virtual int do_foreach (addrmap_foreach_fn fn) const = 0;
80 struct addrmap_mutable;
82 /* Fixed address maps. */
83 struct addrmap_fixed final : public addrmap,
84 public allocate_on_obstack<addrmap_fixed>
86 public:
88 addrmap_fixed (struct obstack *obstack, addrmap_mutable *mut);
89 DISABLE_COPY_AND_ASSIGN (addrmap_fixed);
91 void relocate (CORE_ADDR offset) override;
93 private:
94 void *do_find (CORE_ADDR addr) const override;
95 int do_foreach (addrmap_foreach_fn fn) const override;
97 /* A transition: a point in an address map where the value changes.
98 The map maps ADDR to VALUE, but if ADDR > 0, it maps ADDR-1 to
99 something else. */
100 struct addrmap_transition
102 CORE_ADDR addr;
103 void *value;
106 /* The number of transitions in TRANSITIONS. */
107 size_t num_transitions;
109 /* An array of transitions, sorted by address. For every point in
110 the map where either ADDR == 0 or ADDR is mapped to one value and
111 ADDR - 1 is mapped to something different, we have an entry here
112 containing ADDR and VALUE. (Note that this means we always have
113 an entry for address 0). */
114 struct addrmap_transition *transitions;
117 /* Mutable address maps. */
119 struct addrmap_mutable final : public addrmap
121 public:
123 addrmap_mutable ();
124 ~addrmap_mutable ();
125 DISABLE_COPY_AND_ASSIGN (addrmap_mutable);
127 /* In the mutable address map MAP, associate the addresses from START
128 to END_INCLUSIVE that are currently associated with NULL with OBJ
129 instead. Addresses mapped to an object other than NULL are left
130 unchanged.
132 As the name suggests, END_INCLUSIVE is also mapped to OBJ. This
133 convention is unusual, but it allows callers to accurately specify
134 ranges that abut the top of the address space, and ranges that
135 cover the entire address space.
137 This operation seems a bit complicated for a primitive: if it's
138 needed, why not just have a simpler primitive operation that sets a
139 range to a value, wiping out whatever was there before, and then
140 let the caller construct more complicated operations from that,
141 along with some others for traversal?
143 It turns out this is the mutation operation we want to use all the
144 time, at least for now. Our immediate use for address maps is to
145 represent lexical blocks whose address ranges are not contiguous.
146 We walk the tree of lexical blocks present in the debug info, and
147 only create 'struct block' objects after we've traversed all a
148 block's children. If a lexical block declares no local variables
149 (and isn't the lexical block for a function's body), we omit it
150 from GDB's data structures entirely.
152 However, this means that we don't decide to create a block (and
153 thus record it in the address map) until after we've traversed its
154 children. If we do decide to create the block, we do so at a time
155 when all its children have already been recorded in the map. So
156 this operation --- change only those addresses left unset --- is
157 actually the operation we want to use every time.
159 It seems simpler to let the code which operates on the
160 representation directly deal with the hair of implementing these
161 semantics than to provide an interface which allows it to be
162 implemented efficiently, but doesn't reveal too much of the
163 representation. */
164 void set_empty (CORE_ADDR start, CORE_ADDR end_inclusive,
165 void *obj);
166 void relocate (CORE_ADDR offset) override;
168 private:
169 void *do_find (CORE_ADDR addr) const override;
170 int do_foreach (addrmap_foreach_fn fn) const override;
172 /* A splay tree, with a node for each transition; there is a
173 transition at address T if T-1 and T map to different objects.
175 Any addresses below the first node map to NULL. (Unlike
176 fixed maps, we have no entry at (CORE_ADDR) 0; it doesn't
177 simplify enough.)
179 The last region is assumed to end at CORE_ADDR_MAX.
181 Since we can't know whether CORE_ADDR is larger or smaller than
182 splay_tree_key (unsigned long) --- I think both are possible,
183 given all combinations of 32- and 64-bit hosts and targets ---
184 our keys are pointers to CORE_ADDR values. Since the splay tree
185 library doesn't pass any closure pointer to the key free
186 function, we can't keep a freelist for keys. Since mutable
187 addrmaps are only used temporarily right now, we just leak keys
188 from deleted nodes; they'll be freed when the obstack is freed. */
189 splay_tree tree;
191 /* Various helper methods. */
192 splay_tree_key allocate_key (CORE_ADDR addr);
193 void force_transition (CORE_ADDR addr);
194 splay_tree_node splay_tree_lookup (CORE_ADDR addr) const;
195 splay_tree_node splay_tree_predecessor (CORE_ADDR addr) const;
196 splay_tree_node splay_tree_successor (CORE_ADDR addr);
197 void splay_tree_remove (CORE_ADDR addr);
198 void splay_tree_insert (CORE_ADDR key, void *value);
202 /* Dump the addrmap to OUTFILE. If PAYLOAD is non-NULL, only dump any
203 components that map to PAYLOAD. (If PAYLOAD is NULL, the entire
204 map is dumped.) */
205 void addrmap_dump (struct addrmap *map, struct ui_file *outfile,
206 void *payload);
208 #endif /* ADDRMAP_H */