parse_vdso.c 6.3 KB

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