16e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)/* Validate an address and the relocatability of an offset from it.
26e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   Copyright (C) 2005 Red Hat, Inc.
36e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   This file is part of Red Hat elfutils.
46e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
56e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   Red Hat elfutils is free software; you can redistribute it and/or modify
66e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   it under the terms of the GNU General Public License as published by the
76e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   Free Software Foundation; version 2 of the License.
86e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
96e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   Red Hat elfutils is distributed in the hope that it will be useful, but
106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   WITHOUT ANY WARRANTY; without even the implied warranty of
116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   General Public License for more details.
136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   You should have received a copy of the GNU General Public License along
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   with Red Hat elfutils; if not, write to the Free Software Foundation,
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   In addition, as a special exception, Red Hat, Inc. gives You the
196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   additional right to link the code of Red Hat elfutils with code licensed
206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   under any Open Source Initiative certified open source license
216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   (http://www.opensource.org/licenses/index.php) which requires the
226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   distribution of source code with any binary distribution and to
236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   distribute linked combinations of the two.  Non-GPL Code permitted under
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   this exception must only link to the code of Red Hat elfutils through
256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   those well defined interfaces identified in the file named EXCEPTION
266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   found in the source code files (the "Approved Interfaces").  The files
276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   of Non-GPL Code may instantiate templates or use macros or inline
286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   functions from the Approved Interfaces without causing the resulting
296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   work to be covered by the GNU General Public License.  Only Red Hat,
306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   Inc. may make changes or additions to the list of Approved Interfaces.
316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   Red Hat's grant of this exception is conditioned upon your not adding
326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   any new exceptions.  If you wish to add a new Approved Interface or
336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   exception, please contact Red Hat.  You must obey the GNU General Public
346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   License in all respects for all of the Red Hat elfutils code and other
356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   code used in conjunction with Red Hat elfutils except the Non-GPL Code
366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   covered by this exception.  If you modify this file, you may extend this
376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   exception to your version of the file, but you are not obligated to do
386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   so.  If you do not wish to provide this exception without modification,
396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   you must delete this exception statement from your version and license
406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   this file solely under the GPL without exception.
416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   Red Hat elfutils is an included package of the Open Invention Network.
436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   An included package of the Open Invention Network is a package for which
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   Open Invention Network licensees cross-license their patents.  No patent
456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   license is granted, either expressly or impliedly, by designation as an
466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   included package.  Should you wish to participate in the Open Invention
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   Network licensing program, please visit www.openinventionnetwork.com
486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   <http://www.openinventionnetwork.com>.  */
496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "libdwflP.h"
516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)int
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)dwfl_validate_address (Dwfl *dwfl, Dwarf_Addr address, Dwarf_Sword offset)
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles){
556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  Dwfl_Module *mod = INTUSE(dwfl_addrmodule) (dwfl, address);
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (mod == NULL)
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return -1;
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  Dwarf_Addr relative = address;
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  int idx = INTUSE(dwfl_module_relocate_address) (mod, &relative);
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (idx < 0)
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return -1;
636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (offset != 0)
656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    {
666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      int offset_idx = -1;
676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      relative = address + offset;
686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      if (relative >= mod->low_addr && relative <= mod->high_addr)
696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)	{
706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)	  offset_idx = INTUSE(dwfl_module_relocate_address) (mod, &relative);
716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)	  if (offset_idx < 0)
726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)	    return -1;
736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)	}
746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      if (offset_idx != idx)
756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)	{
766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)	  __libdwfl_seterrno (DWFL_E_ADDR_OUTOFRANGE);
776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)	  return -1;
786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)	}
796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    }
806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return 0;
826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)