parse_vdso.c 6.9 KB

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