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.
5
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.
9
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.
14
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.
18
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.
42
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>.  */
50
51#ifdef HAVE_CONFIG_H
52# include <config.h>
53#endif
54
55#include <assert.h>
56#include <stdlib.h>
57#include <string.h>
58#include <sys/param.h>
59
60#include <libdwP.h>
61#include <dwarf.h>
62
63
64static int
65get_offsets (Dwarf *dbg)
66{
67  size_t allocated = 0;
68  size_t cnt = 0;
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;
74
75  while (readp + 14 < endp)
76    {
77      /* If necessary, allocate more entries.  */
78      if (cnt >= allocated)
79	{
80	  allocated = MAX (10, 2 * allocated);
81	  struct pubnames_s *newmem
82	    = (struct pubnames_s *) realloc (mem, allocated * entsize);
83	  if (newmem == NULL)
84	    {
85	      __libdw_seterrno (DWARF_E_NOMEM);
86	    err_return:
87	      free (mem);
88	      return -1;
89	    }
90
91	  mem = newmem;
92	}
93
94      /* Read the set header.  */
95      int len_bytes = 4;
96      Dwarf_Off len = read_4ubyte_unaligned_inc (dbg, readp);
97      if (len == DWARF3_LENGTH_64_BIT)
98	{
99	  len = read_8ubyte_unaligned_inc (dbg, readp);
100	  len_bytes = 8;
101	}
102      else if (unlikely (len >= DWARF3_LENGTH_MIN_ESCAPE_CODE
103			 && len <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
104	{
105	  __libdw_seterrno (DWARF_E_INVALID_DWARF);
106	  goto err_return;
107	}
108
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
114	   the section.  */
115	break;
116
117      /* Read the version.  It better be two for now.  */
118      uint16_t version = read_2ubyte_unaligned (dbg, readp);
119      if (unlikely (version != 2))
120	{
121	  __libdw_seterrno (DWARF_E_INVALID_VERSION);
122	  goto err_return;
123	}
124
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.  */
129	goto err_return;
130
131      /* Determine the size of the CU header.  */
132      unsigned char *infop
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;
137      else
138	mem[cnt].cu_header_size = 11;
139
140      ++cnt;
141
142      /* Advance to the next set.  */
143      readp += len;
144    }
145
146  if (mem == NULL)
147    {
148      __libdw_seterrno (DWARF_E_NO_ENTRY);
149      return -1;
150    }
151
152  dbg->pubnames_sets = (struct pubnames_s *) realloc (mem, cnt * entsize);
153  dbg->pubnames_nsets = cnt;
154
155  return 0;
156}
157
158
159ptrdiff_t
160dwarf_getpubnames (dbg, callback, arg, offset)
161     Dwarf *dbg;
162     int (*callback) (Dwarf *, Dwarf_Global *, void *);
163     void *arg;
164     ptrdiff_t offset;
165{
166  if (dbg == NULL)
167    return -1l;
168
169  if (unlikely (offset < 0))
170    {
171      __libdw_seterrno (DWARF_E_INVALID_OFFSET);
172      return -1l;
173    }
174
175  /* Make sure it is a valid offset.  */
176  if (unlikely (dbg->sectiondata[IDX_debug_pubnames] == NULL
177		|| ((size_t) offset
178		    >= dbg->sectiondata[IDX_debug_pubnames]->d_size)))
179    /* No (more) entry.  */
180    return 0;
181
182  /* If necessary read the set information.  */
183  if (dbg->pubnames_nsets == 0 && unlikely (get_offsets (dbg) != 0))
184    return -1l;
185
186  /* Find the place where to start.  */
187  size_t cnt;
188  if (offset == 0)
189    {
190      cnt = 0;
191      offset = dbg->pubnames_sets[0].set_start;
192    }
193  else
194    {
195      for (cnt = 0; cnt + 1 < dbg->pubnames_nsets; ++cnt)
196	if ((Dwarf_Off) offset >= dbg->pubnames_sets[cnt].set_start)
197	  {
198	    assert ((Dwarf_Off) offset
199		    < dbg->pubnames_sets[cnt + 1].set_start);
200	    break;
201	  }
202      assert (cnt + 1 < dbg->pubnames_nsets);
203    }
204
205  unsigned char *startp
206    = (unsigned char *) dbg->sectiondata[IDX_debug_pubnames]->d_buf;
207  unsigned char *readp = startp + offset;
208  while (1)
209    {
210      Dwarf_Global gl;
211
212      gl.cu_offset = (dbg->pubnames_sets[cnt].cu_offset
213		      + dbg->pubnames_sets[cnt].cu_header_size);
214
215      while (1)
216	{
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);
220	  else
221	    gl.die_offset = read_8ubyte_unaligned_inc (dbg, readp);
222
223	  /* If the offset is zero we reached the end of the set.  */
224	  if (gl.die_offset == 0)
225	    break;
226
227	  /* Add the CU offset.  */
228	  gl.die_offset += dbg->pubnames_sets[cnt].cu_offset;
229
230	  gl.name = (char *) readp;
231	  readp = (unsigned char *) rawmemchr (gl.name, '\0') + 1;
232
233	  /* We found name and DIE offset.  Report it.  */
234	  if (callback (dbg, &gl, arg) != DWARF_CB_OK)
235	    {
236	      /* The user wants us to stop.  Return the offset of the
237		 next entry.  */
238	      return readp - startp;
239	    }
240	}
241
242      if (++cnt == dbg->pubnames_nsets)
243	/* This was the last set.  */
244	break;
245
246      startp = (unsigned char *) dbg->sectiondata[IDX_debug_pubnames]->d_buf;
247      readp = startp + dbg->pubnames_sets[cnt].set_start;
248    }
249
250  /* We are done.  No more entries.  */
251  return 0;
252}
253