1 /* Get public symbol information.
2 Copyright (C) 2002, 2003, 2004, 2005, 2008 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
6 Red Hat elfutils is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 2 of the License.
10 Red Hat elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with Red Hat elfutils; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
19 In addition, as a special exception, Red Hat, Inc. gives You the
20 additional right to link the code of Red Hat elfutils with code licensed
21 under any Open Source Initiative certified open source license
22 (http://www.opensource.org/licenses/index.php) which requires the
23 distribution of source code with any binary distribution and to
24 distribute linked combinations of the two. Non-GPL Code permitted under
25 this exception must only link to the code of Red Hat elfutils through
26 those well defined interfaces identified in the file named EXCEPTION
27 found in the source code files (the "Approved Interfaces"). The files
28 of Non-GPL Code may instantiate templates or use macros or inline
29 functions from the Approved Interfaces without causing the resulting
30 work to be covered by the GNU General Public License. Only Red Hat,
31 Inc. may make changes or additions to the list of Approved Interfaces.
32 Red Hat's grant of this exception is conditioned upon your not adding
33 any new exceptions. If you wish to add a new Approved Interface or
34 exception, please contact Red Hat. You must obey the GNU General Public
35 License in all respects for all of the Red Hat elfutils code and other
36 code used in conjunction with Red Hat elfutils except the Non-GPL Code
37 covered by this exception. If you modify this file, you may extend this
38 exception to your version of the file, but you are not obligated to do
39 so. If you do not wish to provide this exception without modification,
40 you must delete this exception statement from your version and license
41 this file solely under the GPL without exception.
43 Red Hat elfutils is an included package of the Open Invention Network.
44 An included package of the Open Invention Network is a package for which
45 Open Invention Network licensees cross-license their patents. No patent
46 license is granted, either expressly or impliedly, by designation as an
47 included package. Should you wish to participate in the Open Invention
48 Network licensing program, please visit www.openinventionnetwork.com
49 <http://www.openinventionnetwork.com>. */
58 #include <sys/param.h>
65 get_offsets (Dwarf *dbg)
69 struct pubnames_s *mem = NULL;
70 const size_t entsize = sizeof (struct pubnames_s);
71 unsigned char *const startp = dbg->sectiondata[IDX_debug_pubnames]->d_buf;
72 unsigned char *readp = startp;
73 unsigned char *endp = readp + dbg->sectiondata[IDX_debug_pubnames]->d_size;
75 while (readp + 14 < endp)
77 /* If necessary, allocate more entries. */
80 allocated = MAX (10, 2 * allocated);
81 struct pubnames_s *newmem
82 = (struct pubnames_s *) realloc (mem, allocated * entsize);
85 __libdw_seterrno (DWARF_E_NOMEM);
94 /* Read the set header. */
96 Dwarf_Off len = read_4ubyte_unaligned_inc (dbg, readp);
97 if (len == DWARF3_LENGTH_64_BIT)
99 len = read_8ubyte_unaligned_inc (dbg, readp);
102 else if (unlikely (len >= DWARF3_LENGTH_MIN_ESCAPE_CODE
103 && len <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
105 __libdw_seterrno (DWARF_E_INVALID_DWARF);
109 /* Now we know the offset of the first offset/name pair. */
110 mem[cnt].set_start = readp + 2 + 2 * len_bytes - startp;
111 mem[cnt].address_len = len_bytes;
112 if (mem[cnt].set_start >= dbg->sectiondata[IDX_debug_pubnames]->d_size)
113 /* Something wrong, the first entry is beyond the end of
117 /* Read the version. It better be two for now. */
118 uint16_t version = read_2ubyte_unaligned (dbg, readp);
119 if (unlikely (version != 2))
121 __libdw_seterrno (DWARF_E_INVALID_VERSION);
125 /* Get the CU offset. */
126 if (__libdw_read_offset (dbg, IDX_debug_pubnames, readp + 2, len_bytes,
127 &mem[cnt].cu_offset, IDX_debug_info, 3))
128 /* Error has been already set in reader. */
131 /* Determine the size of the CU header. */
133 = ((unsigned char *) dbg->sectiondata[IDX_debug_info]->d_buf
134 + mem[cnt].cu_offset);
135 if (read_4ubyte_unaligned_noncvt (infop) == DWARF3_LENGTH_64_BIT)
136 mem[cnt].cu_header_size = 23;
138 mem[cnt].cu_header_size = 11;
142 /* Advance to the next set. */
148 __libdw_seterrno (DWARF_E_NO_ENTRY);
152 dbg->pubnames_sets = (struct pubnames_s *) realloc (mem, cnt * entsize);
153 dbg->pubnames_nsets = cnt;
160 dwarf_getpubnames (dbg, callback, arg, offset)
162 int (*callback) (Dwarf *, Dwarf_Global *, void *);
169 if (unlikely (offset < 0))
171 __libdw_seterrno (DWARF_E_INVALID_OFFSET);
175 /* Make sure it is a valid offset. */
176 if (unlikely (dbg->sectiondata[IDX_debug_pubnames] == NULL
178 >= dbg->sectiondata[IDX_debug_pubnames]->d_size)))
179 /* No (more) entry. */
182 /* If necessary read the set information. */
183 if (dbg->pubnames_nsets == 0 && unlikely (get_offsets (dbg) != 0))
186 /* Find the place where to start. */
191 offset = dbg->pubnames_sets[0].set_start;
195 for (cnt = 0; cnt + 1 < dbg->pubnames_nsets; ++cnt)
196 if ((Dwarf_Off) offset >= dbg->pubnames_sets[cnt].set_start)
198 assert ((Dwarf_Off) offset
199 < dbg->pubnames_sets[cnt + 1].set_start);
202 assert (cnt + 1 < dbg->pubnames_nsets);
205 unsigned char *startp
206 = (unsigned char *) dbg->sectiondata[IDX_debug_pubnames]->d_buf;
207 unsigned char *readp = startp + offset;
212 gl.cu_offset = (dbg->pubnames_sets[cnt].cu_offset
213 + dbg->pubnames_sets[cnt].cu_header_size);
217 /* READP points to the next offset/name pair. */
218 if (dbg->pubnames_sets[cnt].address_len == 4)
219 gl.die_offset = read_4ubyte_unaligned_inc (dbg, readp);
221 gl.die_offset = read_8ubyte_unaligned_inc (dbg, readp);
223 /* If the offset is zero we reached the end of the set. */
224 if (gl.die_offset == 0)
227 /* Add the CU offset. */
228 gl.die_offset += dbg->pubnames_sets[cnt].cu_offset;
230 gl.name = (char *) readp;
231 readp = (unsigned char *) rawmemchr (gl.name, '\0') + 1;
233 /* We found name and DIE offset. Report it. */
234 if (callback (dbg, &gl, arg) != DWARF_CB_OK)
236 /* The user wants us to stop. Return the offset of the
238 return readp - startp;
242 if (++cnt == dbg->pubnames_nsets)
243 /* This was the last set. */
246 startp = (unsigned char *) dbg->sectiondata[IDX_debug_pubnames]->d_buf;
247 readp = startp + dbg->pubnames_sets[cnt].set_start;
250 /* We are done. No more entries. */