• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* ANDROID_CHANGE_BEGIN */
64 #include <AndroidFixup.h>
65 /* ANDROID_CHANGE_END */
66 
67 static int
get_offsets(Dwarf * dbg)68 get_offsets (Dwarf *dbg)
69 {
70   size_t allocated = 0;
71   size_t cnt = 0;
72   struct pubnames_s *mem = NULL;
73   const size_t entsize = sizeof (struct pubnames_s);
74   unsigned char *const startp = dbg->sectiondata[IDX_debug_pubnames]->d_buf;
75   unsigned char *readp = startp;
76   unsigned char *endp = readp + dbg->sectiondata[IDX_debug_pubnames]->d_size;
77 
78   while (readp + 14 < endp)
79     {
80       /* If necessary, allocate more entries.  */
81       if (cnt >= allocated)
82 	{
83 	  allocated = MAX (10, 2 * allocated);
84 	  struct pubnames_s *newmem
85 	    = (struct pubnames_s *) realloc (mem, allocated * entsize);
86 	  if (newmem == NULL)
87 	    {
88 	      __libdw_seterrno (DWARF_E_NOMEM);
89 	    err_return:
90 	      free (mem);
91 	      return -1;
92 	    }
93 
94 	  mem = newmem;
95 	}
96 
97       /* Read the set header.  */
98       int len_bytes = 4;
99       Dwarf_Off len = read_4ubyte_unaligned_inc (dbg, readp);
100       if (len == DWARF3_LENGTH_64_BIT)
101 	{
102 	  len = read_8ubyte_unaligned_inc (dbg, readp);
103 	  len_bytes = 8;
104 	}
105       else if (unlikely (len >= DWARF3_LENGTH_MIN_ESCAPE_CODE
106 			 && len <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
107 	{
108 	invalid_dwarf:
109 	  __libdw_seterrno (DWARF_E_INVALID_DWARF);
110 	  goto err_return;
111 	}
112 
113       /* Now we know the offset of the first offset/name pair.  */
114       mem[cnt].set_start = readp + 2 + 2 * len_bytes - startp;
115       mem[cnt].address_len = len_bytes;
116       if (mem[cnt].set_start >= dbg->sectiondata[IDX_debug_pubnames]->d_size)
117 	/* Something wrong, the first entry is beyond the end of
118 	   the section.  */
119 	break;
120 
121       /* Read the version.  It better be two for now.  */
122       uint16_t version = read_2ubyte_unaligned (dbg, readp);
123       if (unlikely (version != 2))
124 	{
125 	  __libdw_seterrno (DWARF_E_INVALID_VERSION);
126 	  goto err_return;
127 	}
128 
129       /* Get the CU offset.  */
130       if (len_bytes == 4)
131 	mem[cnt].cu_offset = read_4ubyte_unaligned (dbg, readp + 2);
132       else
133 	mem[cnt].cu_offset = read_8ubyte_unaligned (dbg, readp + 2);
134 
135       /* Determine the size of the CU header.  */
136       if (unlikely (dbg->sectiondata[IDX_debug_info] == NULL
137 		    || dbg->sectiondata[IDX_debug_info]->d_buf == NULL
138 		    || (mem[cnt].cu_offset + 3
139 			>= dbg->sectiondata[IDX_debug_info]->d_size)))
140 	goto invalid_dwarf;
141 
142       unsigned char *infop
143 	= ((unsigned char *) dbg->sectiondata[IDX_debug_info]->d_buf
144 	   + mem[cnt].cu_offset);
145       if (read_4ubyte_unaligned_noncvt (infop) == DWARF3_LENGTH_64_BIT)
146 	mem[cnt].cu_header_size = 23;
147       else
148 	mem[cnt].cu_header_size = 11;
149 
150       ++cnt;
151 
152       /* Advance to the next set.  */
153       readp += len;
154     }
155 
156   if (mem == NULL)
157     {
158       __libdw_seterrno (DWARF_E_NO_ENTRY);
159       return -1;
160     }
161 
162   dbg->pubnames_sets = (struct pubnames_s *) realloc (mem, cnt * entsize);
163   dbg->pubnames_nsets = cnt;
164 
165   return 0;
166 }
167 
168 
169 ptrdiff_t
dwarf_getpubnames(dbg,callback,arg,offset)170 dwarf_getpubnames (dbg, callback, arg, offset)
171      Dwarf *dbg;
172      int (*callback) (Dwarf *, Dwarf_Global *, void *);
173      void *arg;
174      ptrdiff_t offset;
175 {
176   if (dbg == NULL)
177     return -1l;
178 
179   if (unlikely (offset < 0))
180     {
181       __libdw_seterrno (DWARF_E_INVALID_OFFSET);
182       return -1l;
183     }
184 
185   /* Make sure it is a valid offset.  */
186   if (unlikely (dbg->sectiondata[IDX_debug_pubnames] == NULL
187 		|| ((size_t) offset
188 		    >= dbg->sectiondata[IDX_debug_pubnames]->d_size)))
189     /* No (more) entry.  */
190     return 0;
191 
192   /* If necessary read the set information.  */
193   if (dbg->pubnames_nsets == 0 && unlikely (get_offsets (dbg) != 0))
194     return -1l;
195 
196   /* Find the place where to start.  */
197   size_t cnt;
198   if (offset == 0)
199     {
200       cnt = 0;
201       offset = dbg->pubnames_sets[0].set_start;
202     }
203   else
204     {
205       for (cnt = 0; cnt + 1 < dbg->pubnames_nsets; ++cnt)
206 	if ((Dwarf_Off) offset >= dbg->pubnames_sets[cnt].set_start)
207 	  {
208 	    assert ((Dwarf_Off) offset
209 		    < dbg->pubnames_sets[cnt + 1].set_start);
210 	    break;
211 	  }
212       assert (cnt + 1 < dbg->pubnames_nsets);
213     }
214 
215   unsigned char *startp
216     = (unsigned char *) dbg->sectiondata[IDX_debug_pubnames]->d_buf;
217   unsigned char *readp = startp + offset;
218   while (1)
219     {
220       Dwarf_Global gl;
221 
222       gl.cu_offset = (dbg->pubnames_sets[cnt].cu_offset
223 		      + dbg->pubnames_sets[cnt].cu_header_size);
224 
225       while (1)
226 	{
227 	  /* READP points to the next offset/name pair.  */
228 	  if (dbg->pubnames_sets[cnt].address_len == 4)
229 	    gl.die_offset = read_4ubyte_unaligned_inc (dbg, readp);
230 	  else
231 	    gl.die_offset = read_8ubyte_unaligned_inc (dbg, readp);
232 
233 	  /* If the offset is zero we reached the end of the set.  */
234 	  if (gl.die_offset == 0)
235 	    break;
236 
237 	  /* Add the CU offset.  */
238 	  gl.die_offset += dbg->pubnames_sets[cnt].cu_offset;
239 
240 	  gl.name = (char *) readp;
241 	  readp = (unsigned char *) rawmemchr (gl.name, '\0') + 1;
242 
243 	  /* We found name and DIE offset.  Report it.  */
244 	  if (callback (dbg, &gl, arg) != DWARF_CB_OK)
245 	    {
246 	      /* The user wants us to stop.  Return the offset of the
247 		 next entry.  */
248 	      return readp - startp;
249 	    }
250 	}
251 
252       if (++cnt == dbg->pubnames_nsets)
253 	/* This was the last set.  */
254 	break;
255 
256       startp = (unsigned char *) dbg->sectiondata[IDX_debug_pubnames]->d_buf;
257       readp = startp + dbg->pubnames_sets[cnt].set_start;
258     }
259 
260   /* We are done.  No more entries.  */
261   return 0;
262 }
263