1441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project/* Compute hash value for given string according to ELF standard.
2cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Copyright (C) 2006 Red Hat, Inc.
3cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   This file is part of Red Hat elfutils.
4441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project   Written by Ulrich Drepper <drepper@redhat.com>, 1995.
5441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project
6cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat elfutils is free software; you can redistribute it and/or modify
7cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   it under the terms of the GNU General Public License as published by the
8cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Free Software Foundation; version 2 of the License.
9441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project
10cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat elfutils is distributed in the hope that it will be useful, but
11cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   WITHOUT ANY WARRANTY; without even the implied warranty of
12cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   General Public License for more details.
14441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project
15cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   You should have received a copy of the GNU General Public License along
16cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   with Red Hat elfutils; if not, write to the Free Software Foundation,
17cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
19cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   In addition, as a special exception, Red Hat, Inc. gives You the
20cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   additional right to link the code of Red Hat elfutils with code licensed
21cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   under an Open Source Initiative certified open source license
22cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   (http://www.opensource.org/licenses/index.php) and to distribute linked
23cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   combinations including the two.  Non-GPL Code permitted under this
24cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   exception must only link to the code of Red Hat elfutils through those
25cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   well defined interfaces identified in the file named EXCEPTION found in
26cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   the source code files (the "Approved Interfaces").  The files of Non-GPL
27cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Code may instantiate templates or use macros or inline functions from
28cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   the Approved Interfaces without causing the resulting work to be covered
29cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   by the GNU General Public License.  Only Red Hat, Inc. may make changes
30cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   or additions to the list of Approved Interfaces.  Red Hat's grant of
31cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   this exception is conditioned upon your not adding any new exceptions.
32cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   If you wish to add a new Approved Interface or exception, please contact
33cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat.  You must obey the GNU General Public License in all respects
34cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   for all of the Red Hat elfutils code and other code used in conjunction
35cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   with Red Hat elfutils except the Non-GPL Code covered by this exception.
36cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   If you modify this file, you may extend this exception to your version
37cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   of the file, but you are not obligated to do so.  If you do not wish to
38cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   provide this exception without modification, you must delete this
39cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   exception statement from your version and license this file solely under
40cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   the GPL without exception.
41cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
42cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat elfutils is an included package of the Open Invention Network.
43cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   An included package of the Open Invention Network is a package for which
44cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Open Invention Network licensees cross-license their patents.  No patent
45cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   license is granted, either expressly or impliedly, by designation as an
46cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   included package.  Should you wish to participate in the Open Invention
47cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Network licensing program, please visit www.openinventionnetwork.com
48cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   <http://www.openinventionnetwork.com>.  */
49441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project
50441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project#ifndef _DL_HASH_H
51441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project#define _DL_HASH_H	1
52441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project
53441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project
54441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project/* This is the hashing function specified by the ELF ABI.  In the
55441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project   first five operations no overflow is possible so we optimized it a
56441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project   bit.  */
57441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Projectstatic inline unsigned int
58441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project__attribute__ ((__pure__))
59441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project_dl_elf_hash (const char *name)
60441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project{
61441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project  const unsigned char *iname = (const unsigned char *) name;
62441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project  unsigned int hash = (unsigned int) *iname++;
63441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project  if (*iname != '\0')
64441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project    {
65441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project      hash = (hash << 4) + (unsigned int) *iname++;
66441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project      if (*iname != '\0')
67441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project	{
68441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project	  hash = (hash << 4) + (unsigned int) *iname++;
69441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project	  if (*iname != '\0')
70441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project	    {
71441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project	      hash = (hash << 4) + (unsigned int) *iname++;
72441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project	      if (*iname != '\0')
73441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project		{
74441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project		  hash = (hash << 4) + (unsigned int) *iname++;
75441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project		  while (*iname != '\0')
76441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project		    {
77441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project		      unsigned int hi;
78441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project		      hash = (hash << 4) + (unsigned int) *iname++;
79441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project		      hi = hash & 0xf0000000;
80441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project
81441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project		      /* The algorithm specified in the ELF ABI is as
82441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project			 follows:
83441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project
84441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project			 if (hi != 0)
85441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project			 hash ^= hi >> 24;
86441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project
87441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project			 hash &= ~hi;
88441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project
89441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project			 But the following is equivalent and a lot
90441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project			 faster, especially on modern processors.  */
91441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project
92441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project		      hash ^= hi;
93441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project		      hash ^= hi >> 24;
94441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project		    }
95441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project		}
96441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project	    }
97441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project	}
98441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project    }
99441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project  return hash;
100441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project}
101441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project
102441f72d43a9b550baa779fc82f70816da5f74f0eThe Android Open Source Project#endif /* dl-hash.h */
103