dl-hash.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Compute hash value for given string according to ELF standard.
  2. Copyright (C) 1995-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _DL_HASH_H
  16. #define _DL_HASH_H 1
  17. /* This is the hashing function specified by the ELF ABI. In the
  18. first five operations no overflow is possible so we optimized it a
  19. bit. */
  20. static unsigned int
  21. __attribute__ ((unused))
  22. _dl_elf_hash (const char *name_arg)
  23. {
  24. const unsigned char *name = (const unsigned char *) name_arg;
  25. unsigned long int hash = *name;
  26. if (hash != 0 && name[1] != '\0')
  27. {
  28. hash = (hash << 4) + name[1];
  29. if (name[2] != '\0')
  30. {
  31. hash = (hash << 4) + name[2];
  32. if (name[3] != '\0')
  33. {
  34. hash = (hash << 4) + name[3];
  35. if (name[4] != '\0')
  36. {
  37. hash = (hash << 4) + name[4];
  38. name += 5;
  39. while (*name != '\0')
  40. {
  41. unsigned long int hi;
  42. hash = (hash << 4) + *name++;
  43. hi = hash & 0xf0000000;
  44. /* The algorithm specified in the ELF ABI is as
  45. follows:
  46. if (hi != 0)
  47. hash ^= hi >> 24;
  48. hash &= ~hi;
  49. But the following is equivalent and a lot
  50. faster, especially on modern processors. */
  51. hash ^= hi >> 24;
  52. }
  53. /* Second part of the modified formula. This
  54. operation can be lifted outside the loop. */
  55. hash &= 0x0fffffff;
  56. }
  57. }
  58. }
  59. }
  60. return hash;
  61. }
  62. #endif /* dl-hash.h */