2
0

getauxval.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * QEMU access to the auxiliary vector
  3. *
  4. * Copyright (C) 2013 Red Hat, Inc
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #ifdef CONFIG_GETAUXVAL
  26. /* Don't inline this in qemu/osdep.h, because pulling in <sys/auxv.h> for
  27. the system declaration of getauxval pulls in the system <elf.h>, which
  28. conflicts with qemu's version. */
  29. #include <sys/auxv.h>
  30. unsigned long qemu_getauxval(unsigned long key)
  31. {
  32. return getauxval(key);
  33. }
  34. #elif defined(__linux__)
  35. #include "elf.h"
  36. /* Our elf.h doesn't contain Elf32_auxv_t and Elf64_auxv_t, which is ok because
  37. that just makes it easier to define it properly for the host here. */
  38. typedef struct {
  39. unsigned long a_type;
  40. unsigned long a_val;
  41. } ElfW_auxv_t;
  42. static const ElfW_auxv_t *auxv;
  43. static const ElfW_auxv_t *qemu_init_auxval(void)
  44. {
  45. ElfW_auxv_t *a;
  46. ssize_t size = 512, r, ofs;
  47. int fd;
  48. /* Allocate some initial storage. Make sure the first entry is set
  49. to end-of-list, so that we've got a valid list in case of error. */
  50. auxv = a = g_malloc(size);
  51. a[0].a_type = 0;
  52. a[0].a_val = 0;
  53. fd = open("/proc/self/auxv", O_RDONLY);
  54. if (fd < 0) {
  55. return a;
  56. }
  57. /* Read the first SIZE bytes. Hopefully, this covers everything. */
  58. r = read(fd, a, size);
  59. if (r == size) {
  60. /* Continue to expand until we do get a partial read. */
  61. do {
  62. ofs = size;
  63. size *= 2;
  64. auxv = a = g_realloc(a, size);
  65. r = read(fd, (char *)a + ofs, ofs);
  66. } while (r == ofs);
  67. }
  68. close(fd);
  69. return a;
  70. }
  71. unsigned long qemu_getauxval(unsigned long type)
  72. {
  73. const ElfW_auxv_t *a = auxv;
  74. if (unlikely(a == NULL)) {
  75. a = qemu_init_auxval();
  76. }
  77. for (; a->a_type != 0; a++) {
  78. if (a->a_type == type) {
  79. return a->a_val;
  80. }
  81. }
  82. return 0;
  83. }
  84. #else
  85. unsigned long qemu_getauxval(unsigned long type)
  86. {
  87. return 0;
  88. }
  89. #endif