]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/addrmap.c
[gdb/tdep] Fix ARM_LINUX_JB_PC_EABI
[thirdparty/binutils-gdb.git] / gdb / addrmap.c
CommitLineData
801e3a5b
JB
1/* addrmap.c --- implementation of address map data structure.
2
1d506c26 3 Copyright (C) 2007-2024 Free Software Foundation, Inc.
801e3a5b
JB
4
5 This file is part of GDB.
6
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
2fff4d11 9 the Free Software Foundation; either version 3 of the License, or
801e3a5b
JB
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
2fff4d11 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
801e3a5b 19
e5dc0d5d 20#include "event-top.h"
bf31fd38 21#include "gdbsupport/gdb_obstack.h"
4de283e4 22#include "addrmap.h"
6a7ee001 23#include "gdbsupport/selftest.h"
801e3a5b 24
90421c56
SM
25/* Make sure splay trees can actually hold the values we want to
26 store in them. */
69f6730d
TT
27static_assert (sizeof (splay_tree_key) >= sizeof (CORE_ADDR *));
28static_assert (sizeof (splay_tree_value) >= sizeof (void *));
90421c56 29
801e3a5b
JB
30\f
31/* Fixed address maps. */
32
a692aa3f 33void *
5867ab87 34addrmap_fixed::do_find (CORE_ADDR addr) const
801e3a5b 35{
a692aa3f
TT
36 const struct addrmap_transition *bottom = &transitions[0];
37 const struct addrmap_transition *top = &transitions[num_transitions - 1];
801e3a5b
JB
38
39 while (bottom < top)
40 {
41 /* This needs to round towards top, or else when top = bottom +
dda83cd7
SM
42 1 (i.e., two entries are under consideration), then mid ==
43 bottom, and then we may not narrow the range when (mid->addr
44 < addr). */
bad9471a 45 const addrmap_transition *mid = top - (top - bottom) / 2;
801e3a5b
JB
46
47 if (mid->addr == addr)
dda83cd7
SM
48 {
49 bottom = mid;
50 break;
51 }
801e3a5b 52 else if (mid->addr < addr)
dda83cd7
SM
53 /* We don't eliminate mid itself here, since each transition
54 covers all subsequent addresses until the next. This is why
55 we must round up in computing the midpoint. */
56 bottom = mid;
801e3a5b 57 else
dda83cd7 58 top = mid - 1;
801e3a5b
JB
59 }
60
61 return bottom->value;
62}
63
64
a692aa3f
TT
65void
66addrmap_fixed::relocate (CORE_ADDR offset)
801e3a5b 67{
801e3a5b
JB
68 size_t i;
69
a692aa3f
TT
70 for (i = 0; i < num_transitions; i++)
71 transitions[i].addr += offset;
801e3a5b
JB
72}
73
74
a692aa3f 75int
5867ab87 76addrmap_fixed::do_foreach (addrmap_foreach_fn fn) const
855c153f 77{
855c153f
DE
78 size_t i;
79
a692aa3f 80 for (i = 0; i < num_transitions; i++)
855c153f 81 {
a692aa3f 82 int res = fn (transitions[i].addr, transitions[i].value);
855c153f
DE
83
84 if (res != 0)
85 return res;
86 }
87
88 return 0;
89}
90
91
801e3a5b
JB
92\f
93/* Mutable address maps. */
94
93b527ef 95/* Allocate a copy of CORE_ADDR. */
9d45ec63
TT
96splay_tree_key
97addrmap_mutable::allocate_key (CORE_ADDR addr)
801e3a5b 98{
93b527ef 99 CORE_ADDR *key = XNEW (CORE_ADDR);
801e3a5b 100
5b4ee69b 101 *key = addr;
801e3a5b
JB
102 return (splay_tree_key) key;
103}
104
105
106/* Type-correct wrappers for splay tree access. */
9d45ec63
TT
107splay_tree_node
108addrmap_mutable::splay_tree_lookup (CORE_ADDR addr) const
801e3a5b 109{
9d45ec63 110 return ::splay_tree_lookup (tree, (splay_tree_key) &addr);
801e3a5b
JB
111}
112
113
9d45ec63
TT
114splay_tree_node
115addrmap_mutable::splay_tree_predecessor (CORE_ADDR addr) const
801e3a5b 116{
9d45ec63 117 return ::splay_tree_predecessor (tree, (splay_tree_key) &addr);
801e3a5b
JB
118}
119
120
9d45ec63
TT
121splay_tree_node
122addrmap_mutable::splay_tree_successor (CORE_ADDR addr)
801e3a5b 123{
9d45ec63 124 return ::splay_tree_successor (tree, (splay_tree_key) &addr);
801e3a5b
JB
125}
126
127
9d45ec63
TT
128void
129addrmap_mutable::splay_tree_remove (CORE_ADDR addr)
cb446409 130{
9d45ec63 131 ::splay_tree_remove (tree, (splay_tree_key) &addr);
cb446409
JB
132}
133
134
801e3a5b
JB
135static CORE_ADDR
136addrmap_node_key (splay_tree_node node)
137{
138 return * (CORE_ADDR *) node->key;
139}
140
141
142static void *
143addrmap_node_value (splay_tree_node node)
144{
145 return (void *) node->value;
146}
147
148
149static void
150addrmap_node_set_value (splay_tree_node node, void *value)
151{
152 node->value = (splay_tree_value) value;
153}
154
155
9d45ec63
TT
156void
157addrmap_mutable::splay_tree_insert (CORE_ADDR key, void *value)
801e3a5b 158{
9d45ec63
TT
159 ::splay_tree_insert (tree,
160 allocate_key (key),
161 (splay_tree_value) value);
801e3a5b
JB
162}
163
164
165/* Without changing the mapping of any address, ensure that there is a
166 tree node at ADDR, even if it would represent a "transition" from
167 one value to the same value. */
9d45ec63
TT
168void
169addrmap_mutable::force_transition (CORE_ADDR addr)
801e3a5b 170{
9d45ec63 171 splay_tree_node n = splay_tree_lookup (addr);
801e3a5b
JB
172
173 if (! n)
174 {
9d45ec63
TT
175 n = splay_tree_predecessor (addr);
176 splay_tree_insert (addr, n ? addrmap_node_value (n) : NULL);
801e3a5b
JB
177 }
178}
179
180
a692aa3f
TT
181void
182addrmap_mutable::set_empty (CORE_ADDR start, CORE_ADDR end_inclusive,
183 void *obj)
801e3a5b 184{
801e3a5b
JB
185 splay_tree_node n, next;
186 void *prior_value;
187
188 /* If we're being asked to set all empty portions of the given
189 address range to empty, then probably the caller is confused.
190 (If that turns out to be useful in some cases, then we can change
191 this to simply return, since overriding NULL with NULL is a
192 no-op.) */
193 gdb_assert (obj);
194
195 /* We take a two-pass approach, for simplicity.
196 - Establish transitions where we think we might need them.
197 - First pass: change all NULL regions to OBJ.
198 - Second pass: remove any unnecessary transitions. */
199
200 /* Establish transitions at the start and end. */
9d45ec63 201 force_transition (start);
801e3a5b 202 if (end_inclusive < CORE_ADDR_MAX)
9d45ec63 203 force_transition (end_inclusive + 1);
801e3a5b
JB
204
205 /* Walk the area, changing all NULL regions to OBJ. */
9d45ec63 206 for (n = splay_tree_lookup (start), gdb_assert (n);
801e3a5b 207 n && addrmap_node_key (n) <= end_inclusive;
9d45ec63 208 n = splay_tree_successor (addrmap_node_key (n)))
801e3a5b
JB
209 {
210 if (! addrmap_node_value (n))
dda83cd7 211 addrmap_node_set_value (n, obj);
801e3a5b
JB
212 }
213
214 /* Walk the area again, removing transitions from any value to
215 itself. Be sure to visit both the transitions we forced
216 above. */
9d45ec63 217 n = splay_tree_predecessor (start);
801e3a5b 218 prior_value = n ? addrmap_node_value (n) : NULL;
9d45ec63 219 for (n = splay_tree_lookup (start), gdb_assert (n);
801e3a5b 220 n && (end_inclusive == CORE_ADDR_MAX
dda83cd7 221 || addrmap_node_key (n) <= end_inclusive + 1);
801e3a5b
JB
222 n = next)
223 {
9d45ec63 224 next = splay_tree_successor (addrmap_node_key (n));
801e3a5b 225 if (addrmap_node_value (n) == prior_value)
9d45ec63 226 splay_tree_remove (addrmap_node_key (n));
801e3a5b 227 else
dda83cd7 228 prior_value = addrmap_node_value (n);
801e3a5b
JB
229 }
230}
231
232
a692aa3f 233void *
5867ab87 234addrmap_mutable::do_find (CORE_ADDR addr) const
801e3a5b 235{
9d45ec63 236 splay_tree_node n = splay_tree_lookup (addr);
6a7ee001
TV
237 if (n != nullptr)
238 {
239 gdb_assert (addrmap_node_key (n) == addr);
240 return addrmap_node_value (n);
241 }
242
9d45ec63 243 n = splay_tree_predecessor (addr);
6a7ee001
TV
244 if (n != nullptr)
245 {
246 gdb_assert (addrmap_node_key (n) < addr);
247 return addrmap_node_value (n);
248 }
249
250 return nullptr;
801e3a5b
JB
251}
252
253
79ddf4a5
TT
254addrmap_fixed::addrmap_fixed (struct obstack *obstack,
255 const addrmap_mutable *mut)
801e3a5b 256{
5427f03f 257 size_t transition_count = 0;
801e3a5b
JB
258
259 /* Count the number of transitions in the tree. */
79ddf4a5 260 mut->foreach ([&] (CORE_ADDR start, const void *obj)
5427f03f
TT
261 {
262 ++transition_count;
263 return 0;
264 });
801e3a5b
JB
265
266 /* Include an extra entry for the transition at zero (which fixed
267 maps have, but mutable maps do not.) */
5427f03f 268 transition_count++;
801e3a5b 269
5427f03f
TT
270 num_transitions = 1;
271 transitions = XOBNEWVEC (obstack, struct addrmap_transition,
272 transition_count);
273 transitions[0].addr = 0;
274 transitions[0].value = NULL;
801e3a5b
JB
275
276 /* Copy all entries from the splay tree to the array, in order
277 of increasing address. */
79ddf4a5 278 mut->foreach ([&] (CORE_ADDR start, const void *obj)
5427f03f
TT
279 {
280 transitions[num_transitions].addr = start;
79ddf4a5 281 transitions[num_transitions].value = const_cast<void *> (obj);
5427f03f
TT
282 ++num_transitions;
283 return 0;
284 });
801e3a5b
JB
285
286 /* We should have filled the array. */
5427f03f
TT
287 gdb_assert (num_transitions == transition_count);
288}
289
801e3a5b 290
a692aa3f
TT
291void
292addrmap_mutable::relocate (CORE_ADDR offset)
801e3a5b
JB
293{
294 /* Not needed yet. */
f34652de 295 internal_error (_("addrmap_relocate is not implemented yet "
dda83cd7 296 "for mutable addrmaps"));
801e3a5b
JB
297}
298
299
855c153f
DE
300/* This is a splay_tree_foreach_fn. */
301
302static int
303addrmap_mutable_foreach_worker (splay_tree_node node, void *data)
304{
50a6759f 305 addrmap_foreach_fn *fn = (addrmap_foreach_fn *) data;
855c153f 306
50a6759f 307 return (*fn) (addrmap_node_key (node), addrmap_node_value (node));
855c153f
DE
308}
309
310
a692aa3f 311int
5867ab87 312addrmap_mutable::do_foreach (addrmap_foreach_fn fn) const
855c153f 313{
a692aa3f 314 return splay_tree_foreach (tree, addrmap_mutable_foreach_worker, &fn);
855c153f
DE
315}
316
317
801e3a5b
JB
318/* Compare keys as CORE_ADDR * values. */
319static int
320splay_compare_CORE_ADDR_ptr (splay_tree_key ak, splay_tree_key bk)
321{
322 CORE_ADDR a = * (CORE_ADDR *) ak;
323 CORE_ADDR b = * (CORE_ADDR *) bk;
324
325 /* We can't just return a-b here, because of over/underflow. */
326 if (a < b)
327 return -1;
328 else if (a == b)
329 return 0;
330 else
331 return 1;
332}
333
334
93b527ef
TT
335static void
336xfree_wrapper (splay_tree_key key)
337{
338 xfree ((void *) key);
339}
340
341addrmap_mutable::addrmap_mutable ()
342 : tree (splay_tree_new (splay_compare_CORE_ADDR_ptr, xfree_wrapper,
343 nullptr /* no delete value */))
344{
345}
346
347addrmap_mutable::~addrmap_mutable ()
801e3a5b 348{
79ddf4a5
TT
349 if (tree != nullptr)
350 splay_tree_delete (tree);
9d45ec63 351}
801e3a5b 352
801e3a5b 353
192786c7
TT
354/* See addrmap.h. */
355
356void
357addrmap_dump (struct addrmap *map, struct ui_file *outfile, void *payload)
358{
359 /* True if the previously printed addrmap entry was for PAYLOAD.
360 If so, we want to print the next one as well (since the next
361 addrmap entry defines the end of the range). */
362 bool previous_matched = false;
363
5867ab87 364 auto callback = [&] (CORE_ADDR start_addr, const void *obj)
192786c7
TT
365 {
366 QUIT;
367
368 bool matches = payload == nullptr || payload == obj;
369 const char *addr_str = nullptr;
370 if (matches)
371 addr_str = host_address_to_string (obj);
372 else if (previous_matched)
373 addr_str = "<ends here>";
374
375 if (matches || previous_matched)
6cb06a8c
TT
376 gdb_printf (outfile, " %s%s %s\n",
377 payload != nullptr ? " " : "",
378 core_addr_to_string (start_addr),
379 addr_str);
192786c7
TT
380
381 previous_matched = matches;
382
383 return 0;
384 };
385
769520b7 386 map->foreach (callback);
192786c7
TT
387}
388
6a7ee001
TV
389#if GDB_SELF_TEST
390namespace selftests {
391
392/* Convert P to CORE_ADDR. */
393
394static CORE_ADDR
395core_addr (void *p)
396{
397 return (CORE_ADDR)(uintptr_t)p;
398}
399
400/* Check that &ARRAY[LOW]..&ARRAY[HIGH] has VAL in MAP. */
401
402#define CHECK_ADDRMAP_FIND(MAP, ARRAY, LOW, HIGH, VAL) \
403 do \
404 { \
405 for (unsigned i = LOW; i <= HIGH; ++i) \
769520b7 406 SELF_CHECK (MAP->find (core_addr (&ARRAY[i])) == VAL); \
6a7ee001
TV
407 } \
408 while (0)
409
6a7ee001
TV
410/* Entry point for addrmap unit tests. */
411
412static void
413test_addrmap ()
414{
5b3ef0a5
TV
415 /* We'll verify using the addresses of the elements of this array. */
416 char array[20];
417
418 /* We'll verify using these values stored into the map. */
419 void *val1 = &array[1];
420 void *val2 = &array[2];
6a7ee001
TV
421
422 /* Create mutable addrmap. */
aa095373 423 auto_obstack temp_obstack;
6b62451a 424 auto map = std::make_unique<struct addrmap_mutable> ();
6a7ee001
TV
425 SELF_CHECK (map != nullptr);
426
427 /* Check initial state. */
428 CHECK_ADDRMAP_FIND (map, array, 0, 19, nullptr);
429
430 /* Insert address range into mutable addrmap. */
769520b7 431 map->set_empty (core_addr (&array[10]), core_addr (&array[12]), val1);
6a7ee001
TV
432 CHECK_ADDRMAP_FIND (map, array, 0, 9, nullptr);
433 CHECK_ADDRMAP_FIND (map, array, 10, 12, val1);
434 CHECK_ADDRMAP_FIND (map, array, 13, 19, nullptr);
435
436 /* Create corresponding fixed addrmap. */
d89120e9 437 struct addrmap *map2
aa095373 438 = new (&temp_obstack) addrmap_fixed (&temp_obstack, map.get ());
6a7ee001
TV
439 SELF_CHECK (map2 != nullptr);
440 CHECK_ADDRMAP_FIND (map2, array, 0, 9, nullptr);
441 CHECK_ADDRMAP_FIND (map2, array, 10, 12, val1);
442 CHECK_ADDRMAP_FIND (map2, array, 13, 19, nullptr);
443
444 /* Iterate over both addrmaps. */
5b3ef0a5
TV
445 auto callback = [&] (CORE_ADDR start_addr, void *obj)
446 {
447 if (start_addr == core_addr (nullptr))
448 SELF_CHECK (obj == nullptr);
449 else if (start_addr == core_addr (&array[10]))
450 SELF_CHECK (obj == val1);
451 else if (start_addr == core_addr (&array[13]))
452 SELF_CHECK (obj == nullptr);
453 else
454 SELF_CHECK (false);
455 return 0;
456 };
769520b7
TT
457 SELF_CHECK (map->foreach (callback) == 0);
458 SELF_CHECK (map2->foreach (callback) == 0);
6a7ee001
TV
459
460 /* Relocate fixed addrmap. */
769520b7 461 map2->relocate (1);
6a7ee001
TV
462 CHECK_ADDRMAP_FIND (map2, array, 0, 10, nullptr);
463 CHECK_ADDRMAP_FIND (map2, array, 11, 13, val1);
464 CHECK_ADDRMAP_FIND (map2, array, 14, 19, nullptr);
465
466 /* Insert partially overlapping address range into mutable addrmap. */
769520b7 467 map->set_empty (core_addr (&array[11]), core_addr (&array[13]), val2);
6a7ee001
TV
468 CHECK_ADDRMAP_FIND (map, array, 0, 9, nullptr);
469 CHECK_ADDRMAP_FIND (map, array, 10, 12, val1);
470 CHECK_ADDRMAP_FIND (map, array, 13, 13, val2);
471 CHECK_ADDRMAP_FIND (map, array, 14, 19, nullptr);
6a7ee001
TV
472}
473
474} // namespace selftests
fd986183 475#endif /* GDB_SELF_TEST */
6a7ee001
TV
476
477void _initialize_addrmap ();
478void
479_initialize_addrmap ()
480{
fd986183 481#if GDB_SELF_TEST
6a7ee001 482 selftests::register_test ("addrmap", selftests::test_addrmap);
6a7ee001 483#endif /* GDB_SELF_TEST */
fd986183 484}