1
2
3
4
5
6#include <elf.h>
7#include <errno.h>
8#include <fcntl.h>
9#include <link.h>
10#include <stdio.h>
11#include <sys/stat.h>
12#include <sys/types.h>
13#include <unistd.h>
14
15#include "utils.h"
16
17static char auxv[4096];
18
19void *get_auxv_entry(int type)
20{
21 ElfW(auxv_t) *p;
22 void *result;
23 ssize_t num;
24 int fd;
25
26 fd = open("/proc/self/auxv", O_RDONLY);
27 if (fd == -1) {
28 perror("open");
29 return NULL;
30 }
31
32 result = NULL;
33
34 num = read(fd, auxv, sizeof(auxv));
35 if (num < 0) {
36 perror("read");
37 goto out;
38 }
39
40 if (num > sizeof(auxv)) {
41 printf("Overflowed auxv buffer\n");
42 goto out;
43 }
44
45 p = (ElfW(auxv_t) *)auxv;
46
47 while (p->a_type != AT_NULL) {
48 if (p->a_type == type) {
49 result = (void *)p->a_un.a_val;
50 break;
51 }
52
53 p++;
54 }
55out:
56 close(fd);
57 return result;
58}
59