1 /* Get public symbol information.
2 Copyright (C) 2002, 2003, 2004, 2005, 2008 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
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/>. */
37 #include <sys/param.h>
44 get_offsets (Dwarf *dbg)
48 struct pubnames_s *mem = NULL;
49 const size_t entsize = sizeof (struct pubnames_s);
50 unsigned char *const startp = dbg->sectiondata[IDX_debug_pubnames]->d_buf;
51 unsigned char *readp = startp;
52 unsigned char *endp = readp + dbg->sectiondata[IDX_debug_pubnames]->d_size;
54 while (readp + 14 < endp)
56 /* If necessary, allocate more entries. */
59 allocated = MAX (10, 2 * allocated);
60 struct pubnames_s *newmem
61 = (struct pubnames_s *) realloc (mem, allocated * entsize);
64 __libdw_seterrno (DWARF_E_NOMEM);
73 /* Read the set header. */
75 Dwarf_Off len = read_4ubyte_unaligned_inc (dbg, readp);
76 if (len == DWARF3_LENGTH_64_BIT)
78 len = read_8ubyte_unaligned_inc (dbg, readp);
81 else if (unlikely (len >= DWARF3_LENGTH_MIN_ESCAPE_CODE
82 && len <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
84 __libdw_seterrno (DWARF_E_INVALID_DWARF);
88 /* Now we know the offset of the first offset/name pair. */
89 mem[cnt].set_start = readp + 2 + 2 * len_bytes - startp;
90 mem[cnt].address_len = len_bytes;
91 if (mem[cnt].set_start >= dbg->sectiondata[IDX_debug_pubnames]->d_size)
92 /* Something wrong, the first entry is beyond the end of
96 /* Read the version. It better be two for now. */
97 uint16_t version = read_2ubyte_unaligned (dbg, readp);
98 if (unlikely (version != 2))
100 __libdw_seterrno (DWARF_E_INVALID_VERSION);
104 /* Get the CU offset. */
105 if (__libdw_read_offset (dbg, dbg, IDX_debug_pubnames,
106 readp + 2, len_bytes,
107 &mem[cnt].cu_offset, IDX_debug_info, 3))
108 /* Error has been already set in reader. */
111 /* Determine the size of the CU header. */
113 = ((unsigned char *) dbg->sectiondata[IDX_debug_info]->d_buf
114 + mem[cnt].cu_offset);
115 if (read_4ubyte_unaligned_noncvt (infop) == DWARF3_LENGTH_64_BIT)
116 mem[cnt].cu_header_size = 23;
118 mem[cnt].cu_header_size = 11;
122 /* Advance to the next set. */
128 __libdw_seterrno (DWARF_E_NO_ENTRY);
132 dbg->pubnames_sets = (struct pubnames_s *) realloc (mem, cnt * entsize);
133 dbg->pubnames_nsets = cnt;
140 dwarf_getpubnames (dbg, callback, arg, offset)
142 int (*callback) (Dwarf *, Dwarf_Global *, void *);
149 if (unlikely (offset < 0))
151 __libdw_seterrno (DWARF_E_INVALID_OFFSET);
155 /* Make sure it is a valid offset. */
156 if (unlikely (dbg->sectiondata[IDX_debug_pubnames] == NULL
158 >= dbg->sectiondata[IDX_debug_pubnames]->d_size)))
159 /* No (more) entry. */
162 /* If necessary read the set information. */
163 if (dbg->pubnames_nsets == 0 && unlikely (get_offsets (dbg) != 0))
166 /* Find the place where to start. */
171 offset = dbg->pubnames_sets[0].set_start;
175 for (cnt = 0; cnt + 1 < dbg->pubnames_nsets; ++cnt)
176 if ((Dwarf_Off) offset >= dbg->pubnames_sets[cnt].set_start)
178 assert ((Dwarf_Off) offset
179 < dbg->pubnames_sets[cnt + 1].set_start);
182 assert (cnt + 1 < dbg->pubnames_nsets);
185 unsigned char *startp
186 = (unsigned char *) dbg->sectiondata[IDX_debug_pubnames]->d_buf;
187 unsigned char *readp = startp + offset;
192 gl.cu_offset = (dbg->pubnames_sets[cnt].cu_offset
193 + dbg->pubnames_sets[cnt].cu_header_size);
197 /* READP points to the next offset/name pair. */
198 if (dbg->pubnames_sets[cnt].address_len == 4)
199 gl.die_offset = read_4ubyte_unaligned_inc (dbg, readp);
201 gl.die_offset = read_8ubyte_unaligned_inc (dbg, readp);
203 /* If the offset is zero we reached the end of the set. */
204 if (gl.die_offset == 0)
207 /* Add the CU offset. */
208 gl.die_offset += dbg->pubnames_sets[cnt].cu_offset;
210 gl.name = (char *) readp;
211 readp = (unsigned char *) rawmemchr (gl.name, '\0') + 1;
213 /* We found name and DIE offset. Report it. */
214 if (callback (dbg, &gl, arg) != DWARF_CB_OK)
216 /* The user wants us to stop. Return the offset of the
218 return readp - startp;
222 if (++cnt == dbg->pubnames_nsets)
223 /* This was the last set. */
226 startp = (unsigned char *) dbg->sectiondata[IDX_debug_pubnames]->d_buf;
227 readp = startp + dbg->pubnames_sets[cnt].set_start;
230 /* We are done. No more entries. */