| Michael Ellerman | fcb45ec | 2015-11-24 13:05:38 +1100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2013-2015, Michael Ellerman, IBM Corp. |
| 3 | * Licensed under GPLv2. |
| 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 | |
| 17 | static char auxv[4096]; |
| 18 | |
| 19 | void *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 | } |
| 55 | out: |
| 56 | close(fd); |
| 57 | return result; |
| 58 | } |