1 /* Find CU for given offset.
2 Copyright (C) 2003-2010 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2003.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
39 findcu_cb (const void *arg1, const void *arg2)
41 struct Dwarf_CU *cu1 = (struct Dwarf_CU *) arg1;
42 struct Dwarf_CU *cu2 = (struct Dwarf_CU *) arg2;
44 /* Find out which of the two arguments is the search value. It has
48 if (cu1->start < cu2->start)
50 if (cu1->start >= cu2->end)
55 if (cu2->start < cu1->start)
57 if (cu2->start >= cu1->end)
66 __libdw_intern_next_unit (dbg, debug_types)
70 Dwarf_Off *const offsetp
71 = debug_types ? &dbg->next_tu_offset : &dbg->next_cu_offset;
72 void **tree = debug_types ? &dbg->tu_tree : &dbg->cu_tree;
74 Dwarf_Off oldoff = *offsetp;
78 Dwarf_Off abbrev_offset;
79 uint64_t type_sig8 = 0;
80 Dwarf_Off type_offset = 0;
82 if (INTUSE(dwarf_next_unit) (dbg, oldoff, offsetp, NULL,
83 &version, &abbrev_offset,
84 &address_size, &offset_size,
85 debug_types ? &type_sig8 : NULL,
86 debug_types ? &type_offset : NULL) != 0)
87 /* No more entries. */
90 /* We only know how to handle the DWARF version 2 through 4 formats. */
91 if (unlikely (version < 2) || unlikely (version > 4))
93 __libdw_seterrno (DWARF_E_INVALID_DWARF);
97 /* Create an entry for this CU. */
98 struct Dwarf_CU *newp = libdw_typed_alloc (dbg, struct Dwarf_CU);
101 newp->start = oldoff;
102 newp->end = *offsetp;
103 newp->address_size = address_size;
104 newp->offset_size = offset_size;
105 newp->version = version;
106 newp->type_sig8 = type_sig8;
107 newp->type_offset = type_offset;
108 Dwarf_Abbrev_Hash_init (&newp->abbrev_hash, 41);
109 newp->orig_abbrev_offset = newp->last_abbrev_offset = abbrev_offset;
113 /* Add the new entry to the search tree. */
114 if (tsearch (newp, tree, findcu_cb) == NULL)
116 /* Something went wrong. Undo the operation. */
118 __libdw_seterrno (DWARF_E_NOMEM);
126 __libdw_findcu (dbg, start, debug_types)
131 void **tree = debug_types ? &dbg->tu_tree : &dbg->cu_tree;
132 Dwarf_Off *next_offset
133 = debug_types ? &dbg->next_tu_offset : &dbg->next_cu_offset;
135 /* Maybe we already know that CU. */
136 struct Dwarf_CU fake = { .start = start, .end = 0 };
137 struct Dwarf_CU **found = tfind (&fake, tree, findcu_cb);
141 if (start < *next_offset)
143 __libdw_seterrno (DWARF_E_INVALID_DWARF);
147 /* No. Then read more CUs. */
150 struct Dwarf_CU *newp = __libdw_intern_next_unit (dbg, debug_types);
154 /* Is this the one we are looking for? */
155 if (start < *next_offset)
156 // XXX Match exact offset.