Home | History | Annotate | Line # | Download | only in ld.elf_so
      1  1.1  riastrad /*	$NetBSD: hash.c,v 1.1 2023/07/30 09:20:14 riastradh Exp $	 */
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 1996 John D. Polstra.
      5  1.1  riastrad  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6  1.1  riastrad  * Copyright 2002 Charles M. Hannum <root (at) ihack.net>
      7  1.1  riastrad  * All rights reserved.
      8  1.1  riastrad  *
      9  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     10  1.1  riastrad  * modification, are permitted provided that the following conditions
     11  1.1  riastrad  * are met:
     12  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     13  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     14  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     17  1.1  riastrad  * 3. All advertising materials mentioning features or use of this software
     18  1.1  riastrad  *    must display the following acknowledgement:
     19  1.1  riastrad  *      This product includes software developed by John Polstra.
     20  1.1  riastrad  * 4. The name of the author may not be used to endorse or promote products
     21  1.1  riastrad  *    derived from this software without specific prior written permission.
     22  1.1  riastrad  *
     23  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  1.1  riastrad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  1.1  riastrad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  1.1  riastrad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  1.1  riastrad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  1.1  riastrad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  1.1  riastrad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  1.1  riastrad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  1.1  riastrad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  1.1  riastrad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.1  riastrad  */
     34  1.1  riastrad 
     35  1.1  riastrad /*
     36  1.1  riastrad  * Dynamic linker for ELF.
     37  1.1  riastrad  *
     38  1.1  riastrad  * John Polstra <jdp (at) polstra.com>.
     39  1.1  riastrad  */
     40  1.1  riastrad 
     41  1.1  riastrad #include <sys/cdefs.h>
     42  1.1  riastrad #ifndef lint
     43  1.1  riastrad __RCSID("$NetBSD: hash.c,v 1.1 2023/07/30 09:20:14 riastradh Exp $");
     44  1.1  riastrad #endif /* not lint */
     45  1.1  riastrad 
     46  1.1  riastrad #include <stdint.h>
     47  1.1  riastrad 
     48  1.1  riastrad #include "hash.h"
     49  1.1  riastrad 
     50  1.1  riastrad /*
     51  1.1  riastrad  * SysV hash function for symbol table lookup.  It is a slightly optimized
     52  1.1  riastrad  * version of the hash specified by the System V ABI.
     53  1.1  riastrad  */
     54  1.1  riastrad Elf32_Word
     55  1.1  riastrad _rtld_sysv_hash(const char *name)
     56  1.1  riastrad {
     57  1.1  riastrad 	const unsigned char *p = (const unsigned char *) name;
     58  1.1  riastrad 	Elf32_Word h = 0;
     59  1.1  riastrad 
     60  1.1  riastrad 	while (__predict_true(*p != '\0')) {
     61  1.1  riastrad 		h = (h << 4) + *p++;
     62  1.1  riastrad 		h ^= (h >> 24) & 0xf0;
     63  1.1  riastrad 	}
     64  1.1  riastrad 	return (h & 0x0fffffff);
     65  1.1  riastrad }
     66  1.1  riastrad 
     67  1.1  riastrad /*
     68  1.1  riastrad  * Hash function for symbol table lookup.  Don't even think about changing
     69  1.1  riastrad  * this.  It is specified by the GNU toolchain ABI.
     70  1.1  riastrad  */
     71  1.1  riastrad Elf32_Word
     72  1.1  riastrad _rtld_gnu_hash(const char *name)
     73  1.1  riastrad {
     74  1.1  riastrad 	const unsigned char *p = (const unsigned char *) name;
     75  1.1  riastrad 	uint_fast32_t h = 5381;
     76  1.1  riastrad 	unsigned char c;
     77  1.1  riastrad 
     78  1.1  riastrad 	for (c = *p; c != '\0'; c = *++p)
     79  1.1  riastrad 		h = h * 33 + c;
     80  1.1  riastrad 	return (h & 0xffffffff);
     81  1.1  riastrad }
     82