aboutsummaryrefslogtreecommitdiffstats
path: root/com32/lib/sys/module/elfutils.c
blob: ce781a08162860ed7ad260b31ad7d24d7b502e58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdlib.h>
#include <errno.h>

#include <sys/elfutils.h>

unsigned long elf_hash(const unsigned char *name) {
	unsigned long h = 0;
	unsigned long g;

	while (*name) {
		h = (h << 4) + *name++;
		if ((g = h & 0xF0000000))
			h ^= g >> 24;

		h &= ~g;
	}

	return h;
}

unsigned long elf_gnu_hash(const unsigned char *name) {
	unsigned long h = 5381;
	unsigned char c;

	for (c = *name; c != '\0'; c = *++name) {
		h = h * 33 + c;
	}

	return h & 0xFFFFFFFF;
}

#ifdef ELF_NO_POSIX_MEMALIGN

struct memalign_info {
	void 	*start_addr;
	char	data[0];
};

int elf_malloc(void **memptr, size_t alignment, size_t size) {
	void *start_addr = NULL;
	struct memalign_info *info;

	if ((alignment & (alignment - 1)) != 0)
		return EINVAL;
	if (alignment % sizeof(void*) != 0)
		alignment = sizeof(void*);

	start_addr = malloc(size + (alignment > sizeof(struct memalign_info) ?
					alignment : sizeof(struct memalign_info)));

	if (start_addr == NULL)
		return ENOMEM;


	info = (struct memalign_info*)(start_addr -
			((unsigned long)start_addr % alignment) +
			alignment - sizeof(struct memalign_info));

	info->start_addr = start_addr;

	*memptr = info->data;

	return 0;
}

void elf_free(void *memptr) {
	struct memalign_info *info = (struct memalign_info*)(memptr -
			sizeof(struct memalign_info));

	free(info->start_addr);
}

#else

int elf_malloc(void **memptr, size_t alignment, size_t size) {
	if ((alignment & (alignment - 1)) != 0)
		return EINVAL;

	if (alignment % sizeof(void*) != 0)
		alignment = sizeof(void*);

	return posix_memalign(memptr, alignment, size);
}

void elf_free(void *memptr) {
	free(memptr);
}

#endif //ELF_NO_POSIX_MEMALIGN