• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Iterate through modules.
2    Copyright (C) 2005, 2008 Red Hat, Inc.
3    This file is part of elfutils.
4 
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of either
7 
8      * the GNU Lesser General Public License as published by the Free
9        Software Foundation; either version 3 of the License, or (at
10        your option) any later version
11 
12    or
13 
14      * the GNU General Public License as published by the Free
15        Software Foundation; either version 2 of the License, or (at
16        your option) any later version
17 
18    or both in parallel, as here.
19 
20    elfutils is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24 
25    You should have received copies of the GNU General Public License and
26    the GNU Lesser General Public License along with this program.  If
27    not, see <http://www.gnu.org/licenses/>.  */
28 
29 #include "libdwflP.h"
30 
31 ptrdiff_t
dwfl_getmodules(Dwfl * dwfl,int (* callback)(Dwfl_Module *,void **,const char *,Dwarf_Addr,void *),void * arg,ptrdiff_t offset)32 dwfl_getmodules (Dwfl *dwfl,
33 		 int (*callback) (Dwfl_Module *, void **,
34 				  const char *, Dwarf_Addr, void *),
35 		 void *arg,
36 		 ptrdiff_t offset)
37 {
38   if (dwfl == NULL)
39     return -1;
40 
41   /* We iterate through the linked list when it's all we have.
42      But continuing from an offset is slow that way.  So when
43      DWFL->lookup_module is populated, we can instead keep our
44      place by jumping directly into the array.  Since the actions
45      of a callback could cause it to get populated, we must
46      choose the style of place-holder when we return an offset,
47      and we encode the choice in the low bits of that value.  */
48 
49   Dwfl_Module *m = dwfl->modulelist;
50 
51   if ((offset & 3) == 1)
52     {
53       offset >>= 2;
54       for (ptrdiff_t pos = 0; pos < offset; ++pos)
55 	if (m == NULL)
56 	  return -1;
57 	else
58 	  m = m->next;
59     }
60   else if (((offset & 3) == 2) && likely (dwfl->lookup_module != NULL))
61     {
62       offset >>= 2;
63 
64       if ((size_t) offset - 1 == dwfl->lookup_elts)
65 	return 0;
66 
67       if (unlikely ((size_t) offset - 1 > dwfl->lookup_elts))
68 	return -1;
69 
70       m = dwfl->lookup_module[offset - 1];
71       if (unlikely (m == NULL))
72 	return -1;
73     }
74   else if (offset != 0)
75     {
76       __libdwfl_seterrno (DWFL_E_BADSTROFF);
77       return -1;
78     }
79 
80   while (m != NULL)
81     {
82       int ok = (*callback) (MODCB_ARGS (m), arg);
83       ++offset;
84       m = m->next;
85       if (ok != DWARF_CB_OK)
86 	return ((dwfl->lookup_module == NULL) ? ((offset << 2) | 1)
87 		: (((m == NULL ? (ptrdiff_t) dwfl->lookup_elts + 1
88 		     : m->segment + 1) << 2) | 2));
89     }
90   return 0;
91 }
92 INTDEF (dwfl_getmodules)
93