parse_vdso.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * parse_vdso.c: Linux reference vDSO parser
  3. * Written by Andrew Lutomirski, 2011.
  4. *
  5. * This code is meant to be linked in to various programs that run on Linux.
  6. * As such, it is available with as few restrictions as possible. This file
  7. * is licensed under the Creative Commons Zero License, version 1.0,
  8. * available at http://creativecommons.org/publicdomain/zero/1.0/legalcode
  9. *
  10. * The vDSO is a regular ELF DSO that the kernel maps into user space when
  11. * it starts a program. It works equally well in statically and dynamically
  12. * linked binaries.
  13. *
  14. * This code is tested on x86_64. In principle it should work on any 64-bit
  15. * architecture that has a vDSO.
  16. */
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <elf.h>
  21. /*
  22. * To use this vDSO parser, first call one of the vdso_init_* functions.
  23. * If you've already parsed auxv, then pass the value of AT_SYSINFO_EHDR
  24. * to vdso_init_from_sysinfo_ehdr. Otherwise pass auxv to vdso_init_from_auxv.
  25. * Then call vdso_sym for each symbol you want. For example, to look up
  26. * gettimeofday on x86_64, use:
  27. *
  28. * <some pointer> = vdso_sym("LINUX_2.6", "gettimeofday");
  29. * or
  30. * <some pointer> = vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
  31. *
  32. * vdso_sym will return 0 if the symbol doesn't exist or if the init function
  33. * failed or was not called. vdso_sym is a little slow, so its return value
  34. * should be cached.
  35. *
  36. * vdso_sym is threadsafe; the init functions are not.
  37. *
  38. * These are the prototypes:
  39. */
  40. extern void vdso_init_from_auxv(void *auxv);
  41. extern void vdso_init_from_sysinfo_ehdr(uintptr_t base);
  42. extern void *vdso_sym(const char *version, const char *name);
  43. /* And here's the code. */
  44. #ifndef __x86_64__
  45. # error Not yet ported to non-x86_64 architectures
  46. #endif
  47. static struct vdso_info
  48. {
  49. bool valid;
  50. /* Load information */
  51. uintptr_t load_addr;
  52. uintptr_t load_offset; /* load_addr - recorded vaddr */
  53. /* Symbol table */
  54. Elf64_Sym *symtab;
  55. const char *symstrings;
  56. Elf64_Word *bucket, *chain;
  57. Elf64_Word nbucket, nchain;
  58. /* Version table */
  59. Elf64_Versym *versym;
  60. Elf64_Verdef *verdef;
  61. } vdso_info;
  62. /* Straight from the ELF specification. */
  63. static unsigned long elf_hash(const unsigned char *name)
  64. {
  65. unsigned long h = 0, g;
  66. while (*name)
  67. {
  68. h = (h << 4) + *name++;
  69. if (g = h & 0xf0000000)
  70. h ^= g >> 24;
  71. h &= ~g;
  72. }
  73. return h;
  74. }
  75. void vdso_init_from_sysinfo_ehdr(uintptr_t base)
  76. {
  77. size_t i;
  78. bool found_vaddr = false;
  79. vdso_info.valid = false;
  80. vdso_info.load_addr = base;
  81. Elf64_Ehdr *hdr = (Elf64_Ehdr*)base;
  82. Elf64_Phdr *pt = (Elf64_Phdr*)(vdso_info.load_addr + hdr->e_phoff);
  83. Elf64_Dyn *dyn = 0;
  84. /*
  85. * We need two things from the segment table: the load offset
  86. * and the dynamic table.
  87. */
  88. for (i = 0; i < hdr->e_phnum; i++)
  89. {
  90. if (pt[i].p_type == PT_LOAD && !found_vaddr) {
  91. found_vaddr = true;
  92. vdso_info.load_offset = base
  93. + (uintptr_t)pt[i].p_offset
  94. - (uintptr_t)pt[i].p_vaddr;
  95. } else if (pt[i].p_type == PT_DYNAMIC) {
  96. dyn = (Elf64_Dyn*)(base + pt[i].p_offset);
  97. }
  98. }
  99. if (!found_vaddr || !dyn)
  100. return; /* Failed */
  101. /*
  102. * Fish out the useful bits of the dynamic table.
  103. */
  104. Elf64_Word *hash = 0;
  105. vdso_info.symstrings = 0;
  106. vdso_info.symtab = 0;
  107. vdso_info.versym = 0;
  108. vdso_info.verdef = 0;
  109. for (i = 0; dyn[i].d_tag != DT_NULL; i++) {
  110. switch (dyn[i].d_tag) {
  111. case DT_STRTAB:
  112. vdso_info.symstrings = (const char *)
  113. ((uintptr_t)dyn[i].d_un.d_ptr
  114. + vdso_info.load_offset);
  115. break;
  116. case DT_SYMTAB:
  117. vdso_info.symtab = (Elf64_Sym *)
  118. ((uintptr_t)dyn[i].d_un.d_ptr
  119. + vdso_info.load_offset);
  120. break;
  121. case DT_HASH:
  122. hash = (Elf64_Word *)
  123. ((uintptr_t)dyn[i].d_un.d_ptr
  124. + vdso_info.load_offset);
  125. break;
  126. case DT_VERSYM:
  127. vdso_info.versym = (Elf64_Versym *)
  128. ((uintptr_t)dyn[i].d_un.d_ptr
  129. + vdso_info.load_offset);
  130. break;
  131. case DT_VERDEF:
  132. vdso_info.verdef = (Elf64_Verdef *)
  133. ((uintptr_t)dyn[i].d_un.d_ptr
  134. + vdso_info.load_offset);
  135. break;
  136. }
  137. }
  138. if (!vdso_info.symstrings || !vdso_info.symtab || !hash)
  139. return; /* Failed */
  140. if (!vdso_info.verdef)
  141. vdso_info.versym = 0;
  142. /* Parse the hash table header. */
  143. vdso_info.nbucket = hash[0];
  144. vdso_info.nchain = hash[1];
  145. vdso_info.bucket = &hash[2];
  146. vdso_info.chain = &hash[vdso_info.nbucket + 2];
  147. /* That's all we need. */
  148. vdso_info.valid = true;
  149. }
  150. static bool vdso_match_version(Elf64_Versym ver,
  151. const char *name, Elf64_Word hash)
  152. {
  153. /*
  154. * This is a helper function to check if the version indexed by
  155. * ver matches name (which hashes to hash).
  156. *
  157. * The version definition table is a mess, and I don't know how
  158. * to do this in better than linear time without allocating memory
  159. * to build an index. I also don't know why the table has
  160. * variable size entries in the first place.
  161. *
  162. * For added fun, I can't find a comprehensible specification of how
  163. * to parse all the weird flags in the table.
  164. *
  165. * So I just parse the whole table every time.
  166. */
  167. /* First step: find the version definition */
  168. ver &= 0x7fff; /* Apparently bit 15 means "hidden" */
  169. Elf64_Verdef *def = vdso_info.verdef;
  170. while(true) {
  171. if ((def->vd_flags & VER_FLG_BASE) == 0
  172. && (def->vd_ndx & 0x7fff) == ver)
  173. break;
  174. if (def->vd_next == 0)
  175. return false; /* No definition. */
  176. def = (Elf64_Verdef *)((char *)def + def->vd_next);
  177. }
  178. /* Now figure out whether it matches. */
  179. Elf64_Verdaux *aux = (Elf64_Verdaux*)((char *)def + def->vd_aux);
  180. return def->vd_hash == hash
  181. && !strcmp(name, vdso_info.symstrings + aux->vda_name);
  182. }
  183. void *vdso_sym(const char *version, const char *name)
  184. {
  185. unsigned long ver_hash;
  186. if (!vdso_info.valid)
  187. return 0;
  188. ver_hash = elf_hash(version);
  189. Elf64_Word chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
  190. for (; chain != STN_UNDEF; chain = vdso_info.chain[chain]) {
  191. Elf64_Sym *sym = &vdso_info.symtab[chain];
  192. /* Check for a defined global or weak function w/ right name. */
  193. if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
  194. continue;
  195. if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL &&
  196. ELF64_ST_BIND(sym->st_info) != STB_WEAK)
  197. continue;
  198. if (sym->st_shndx == SHN_UNDEF)
  199. continue;
  200. if (strcmp(name, vdso_info.symstrings + sym->st_name))
  201. continue;
  202. /* Check symbol version. */
  203. if (vdso_info.versym
  204. && !vdso_match_version(vdso_info.versym[chain],
  205. version, ver_hash))
  206. continue;
  207. return (void *)(vdso_info.load_offset + sym->st_value);
  208. }
  209. return 0;
  210. }
  211. void vdso_init_from_auxv(void *auxv)
  212. {
  213. Elf64_auxv_t *elf_auxv = auxv;
  214. for (int i = 0; elf_auxv[i].a_type != AT_NULL; i++)
  215. {
  216. if (elf_auxv[i].a_type == AT_SYSINFO_EHDR) {
  217. vdso_init_from_sysinfo_ehdr(elf_auxv[i].a_un.a_val);
  218. return;
  219. }
  220. }
  221. vdso_info.valid = false;
  222. }