125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Iterate through modules.
225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Copyright (C) 2005, 2008 Red Hat, Inc.
325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   This file is part of Red Hat elfutils.
425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is free software; you can redistribute it and/or modify
625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   it under the terms of the GNU General Public License as published by the
725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Free Software Foundation; version 2 of the License.
825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is distributed in the hope that it will be useful, but
1025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   WITHOUT ANY WARRANTY; without even the implied warranty of
1125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   General Public License for more details.
1325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
1425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   You should have received a copy of the GNU General Public License along
1525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   with Red Hat elfutils; if not, write to the Free Software Foundation,
1625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
1725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
1825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   In addition, as a special exception, Red Hat, Inc. gives You the
1925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   additional right to link the code of Red Hat elfutils with code licensed
2025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   under any Open Source Initiative certified open source license
2125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   (http://www.opensource.org/licenses/index.php) which requires the
2225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   distribution of source code with any binary distribution and to
2325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   distribute linked combinations of the two.  Non-GPL Code permitted under
2425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   this exception must only link to the code of Red Hat elfutils through
2525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   those well defined interfaces identified in the file named EXCEPTION
2625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   found in the source code files (the "Approved Interfaces").  The files
2725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   of Non-GPL Code may instantiate templates or use macros or inline
2825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   functions from the Approved Interfaces without causing the resulting
2925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   work to be covered by the GNU General Public License.  Only Red Hat,
3025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Inc. may make changes or additions to the list of Approved Interfaces.
3125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat's grant of this exception is conditioned upon your not adding
3225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   any new exceptions.  If you wish to add a new Approved Interface or
3325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   exception, please contact Red Hat.  You must obey the GNU General Public
3425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   License in all respects for all of the Red Hat elfutils code and other
3525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   code used in conjunction with Red Hat elfutils except the Non-GPL Code
3625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   covered by this exception.  If you modify this file, you may extend this
3725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   exception to your version of the file, but you are not obligated to do
3825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   so.  If you do not wish to provide this exception without modification,
3925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   you must delete this exception statement from your version and license
4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   this file solely under the GPL without exception.
4125b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is an included package of the Open Invention Network.
4325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   An included package of the Open Invention Network is a package for which
4425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Open Invention Network licensees cross-license their patents.  No patent
4525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   license is granted, either expressly or impliedly, by designation as an
4625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   included package.  Should you wish to participate in the Open Invention
4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Network licensing program, please visit www.openinventionnetwork.com
4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   <http://www.openinventionnetwork.com>.  */
4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5025b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "libdwflP.h"
5125b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5225b3c049e70834cf33790a28643ab058b507b35cBen Chengptrdiff_t
5325b3c049e70834cf33790a28643ab058b507b35cBen Chengdwfl_getmodules (Dwfl *dwfl,
5425b3c049e70834cf33790a28643ab058b507b35cBen Cheng		 int (*callback) (Dwfl_Module *, void **,
5525b3c049e70834cf33790a28643ab058b507b35cBen Cheng				  const char *, Dwarf_Addr, void *),
5625b3c049e70834cf33790a28643ab058b507b35cBen Cheng		 void *arg,
5725b3c049e70834cf33790a28643ab058b507b35cBen Cheng		 ptrdiff_t offset)
5825b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
5925b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (dwfl == NULL)
6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng    return -1;
6125b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* We iterate through the linked list when it's all we have.
6325b3c049e70834cf33790a28643ab058b507b35cBen Cheng     But continuing from an offset is slow that way.  So when
6425b3c049e70834cf33790a28643ab058b507b35cBen Cheng     DWFL->lookup_module is populated, we can instead keep our
6525b3c049e70834cf33790a28643ab058b507b35cBen Cheng     place by jumping directly into the array.  Since the actions
6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng     of a callback could cause it to get populated, we must
6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng     choose the style of place-holder when we return an offset,
6825b3c049e70834cf33790a28643ab058b507b35cBen Cheng     and we encode the choice in the low bits of that value.  */
6925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Dwfl_Module *m = dwfl->modulelist;
7125b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if ((offset & 3) == 1)
7325b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
7425b3c049e70834cf33790a28643ab058b507b35cBen Cheng      offset >>= 2;
7525b3c049e70834cf33790a28643ab058b507b35cBen Cheng      for (ptrdiff_t pos = 0; pos < offset; ++pos)
7625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	if (m == NULL)
7725b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  return -1;
7825b3c049e70834cf33790a28643ab058b507b35cBen Cheng	else
7925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  m = m->next;
8025b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
8125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  else if (((offset & 3) == 2) && likely (dwfl->lookup_module != NULL))
8225b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
8325b3c049e70834cf33790a28643ab058b507b35cBen Cheng      offset >>= 2;
8425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8525b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if ((size_t) offset - 1 == dwfl->lookup_elts)
8625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	return 0;
8725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8825b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (unlikely ((size_t) offset - 1 > dwfl->lookup_elts))
8925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	return -1;
9025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
9125b3c049e70834cf33790a28643ab058b507b35cBen Cheng      m = dwfl->lookup_module[offset - 1];
9225b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (unlikely (m == NULL))
9325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	return -1;
9425b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
9525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  else if (offset != 0)
9625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
9725b3c049e70834cf33790a28643ab058b507b35cBen Cheng      __libdwfl_seterrno (DWFL_E_BADSTROFF);
9825b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return -1;
9925b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
10025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
10125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  while (m != NULL)
10225b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
10325b3c049e70834cf33790a28643ab058b507b35cBen Cheng      int ok = (*callback) (MODCB_ARGS (m), arg);
10425b3c049e70834cf33790a28643ab058b507b35cBen Cheng      ++offset;
10525b3c049e70834cf33790a28643ab058b507b35cBen Cheng      m = m->next;
10625b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (ok != DWARF_CB_OK)
10725b3c049e70834cf33790a28643ab058b507b35cBen Cheng	return ((dwfl->lookup_module == NULL) ? ((offset << 2) | 1)
10825b3c049e70834cf33790a28643ab058b507b35cBen Cheng		: (((m == NULL ? (ptrdiff_t) dwfl->lookup_elts + 1
10925b3c049e70834cf33790a28643ab058b507b35cBen Cheng		     : m->segment + 1) << 2) | 2));
11025b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
11125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return 0;
11225b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
11325b3c049e70834cf33790a28643ab058b507b35cBen ChengINTDEF (dwfl_getmodules)
114