1#include <stdlib.h>
2#include <errno.h>
3
4#include "elfutils.h"
5
6unsigned long elf_hash(const unsigned char *name) {
7	unsigned long h = 0;
8	unsigned long g;
9
10	while (*name) {
11		h = (h << 4) + *name++;
12		if ((g = h & 0xF0000000))
13			h ^= g >> 24;
14
15		h &= ~g;
16	}
17
18	return h;
19}
20
21unsigned long elf_gnu_hash(const unsigned char *name) {
22	unsigned long h = 5381;
23	unsigned char c;
24
25	for (c = *name; c != '\0'; c = *++name) {
26		h = h * 33 + c;
27	}
28
29	return h & 0xFFFFFFFF;
30}
31
32#ifndef HAVE_ELF_POSIX_MEMALIGN
33
34struct memalign_info {
35	void 	*start_addr;
36	char	data[0];
37};
38
39int elf_malloc(void **memptr, size_t alignment, size_t size) {
40	char *start_addr = NULL;
41	struct memalign_info *info;
42
43	if ((alignment & (alignment - 1)) != 0)
44		return EINVAL;
45	if (alignment % sizeof(void*) != 0)
46		alignment = sizeof(void*);
47
48	start_addr = malloc(size + (alignment > sizeof(struct memalign_info) ?
49					alignment : sizeof(struct memalign_info)));
50
51	if (start_addr == NULL)
52		return ENOMEM;
53
54
55	info = (struct memalign_info*)(start_addr -
56			((unsigned long)start_addr % alignment) +
57			alignment - sizeof(struct memalign_info));
58
59	info->start_addr = start_addr;
60
61	*memptr = info->data;
62
63	return 0;
64}
65
66void elf_free(char *memptr) {
67	struct memalign_info *info = (struct memalign_info*)(memptr -
68			sizeof(struct memalign_info));
69
70	free(info->start_addr);
71}
72
73#else
74
75int elf_malloc(void **memptr, size_t alignment, size_t size) {
76	if ((alignment & (alignment - 1)) != 0)
77		return EINVAL;
78
79	if (alignment % sizeof(void*) != 0)
80		alignment = sizeof(void*);
81
82	return posix_memalign(memptr, alignment, size);
83}
84
85void elf_free(void *memptr) {
86	free(memptr);
87}
88
89#endif //HAVE_ELF_POSIX_MEMALIGN
90