1 | #ifndef ELF_UTILS_H_ |
---|
2 | #define ELF_UTILS_H_ |
---|
3 | |
---|
4 | #include <elf.h> |
---|
5 | #include <stdlib.h> |
---|
6 | #include <sys/module.h> |
---|
7 | |
---|
8 | /** |
---|
9 | * elf_get_header - Returns a pointer to the ELF header structure. |
---|
10 | * @elf_image: pointer to the ELF file image in memory |
---|
11 | */ |
---|
12 | static inline Elf_Ehdr *elf_get_header(void *elf_image) { |
---|
13 | return (Elf_Ehdr*)elf_image; |
---|
14 | } |
---|
15 | |
---|
16 | /** |
---|
17 | * elf_get_pht - Returns a pointer to the first entry in the PHT. |
---|
18 | * @elf_image: pointer to the ELF file image in memory |
---|
19 | */ |
---|
20 | static inline Elf_Phdr *elf_get_pht(void *elf_image) { |
---|
21 | Elf_Ehdr *elf_hdr = elf_get_header(elf_image); |
---|
22 | |
---|
23 | return (Elf_Phdr*)((Elf_Off)elf_hdr + elf_hdr->e_phoff); |
---|
24 | } |
---|
25 | |
---|
26 | // |
---|
27 | /** |
---|
28 | * elf_get_ph - Returns the element with the given index in the PTH |
---|
29 | * @elf_image: pointer to the ELF file image in memory |
---|
30 | * @index: the index of the PHT entry to look for |
---|
31 | */ |
---|
32 | static inline Elf_Phdr *elf_get_ph(void *elf_image, int index) { |
---|
33 | Elf_Phdr *elf_pht = elf_get_pht(elf_image); |
---|
34 | Elf_Ehdr *elf_hdr = elf_get_header(elf_image); |
---|
35 | |
---|
36 | return (Elf_Phdr*)((Elf_Off)elf_pht + index * elf_hdr->e_phentsize); |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | * elf_hash - Returns the index in a SysV hash table for the symbol name. |
---|
41 | * @name: the name of the symbol to look for |
---|
42 | */ |
---|
43 | extern unsigned long elf_hash(const unsigned char *name); |
---|
44 | |
---|
45 | /** |
---|
46 | * elf_gnu_hash - Returns the index in a GNU hash table for the symbol name. |
---|
47 | * @name: the name of the symbol to look for |
---|
48 | */ |
---|
49 | extern unsigned long elf_gnu_hash(const unsigned char *name); |
---|
50 | |
---|
51 | /** |
---|
52 | * elf_malloc - Allocates memory to be used by ELF module contents. |
---|
53 | * @memptr: pointer to a variable to hold the address of the allocated block. |
---|
54 | * @alignment: alignment constraints of the block |
---|
55 | * @size: the required size of the block |
---|
56 | */ |
---|
57 | extern int elf_malloc(void **memptr, size_t alignment, size_t size); |
---|
58 | |
---|
59 | /** |
---|
60 | * elf_free - Releases memory previously allocated by elf_malloc. |
---|
61 | * @memptr: the address of the allocated block |
---|
62 | */ |
---|
63 | extern void elf_free(char *memptr); |
---|
64 | |
---|
65 | #endif /*ELF_UTILS_H_*/ |
---|