utils.c 840 B

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