dl-vdso.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include <elf.h>
  2. #include <string.h>
  3. #include "sys/auxv.h"
  4. #define __ARCH_VDSO_GETTIMEOFDAY_NAME "__vdso_gettimeofday"
  5. #define __ARCH_VDSO_CLOCK_GETTIME_NAME "__vdso_clock_gettime"
  6. #if defined(__UCLIBC_USE_TIME64__)
  7. #define __ARCH_VDSO_CLOCK_GETTIME64_NAME "__vdso_clock_gettime64"
  8. #endif
  9. /* Maybe override default vDSO functions names by arch-specific */
  10. #include "ldso.h"
  11. #include "generated/autoconf.h"
  12. #ifndef ELF_BITS
  13. # if ULONG_MAX > 0xffffffffUL
  14. # define ELF_BITS 64
  15. # else
  16. # define ELF_BITS 32
  17. # endif
  18. #endif
  19. #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
  20. #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
  21. #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
  22. #ifndef __VDSO_SUPPORT__
  23. void load_vdso(void *sys_info_ehdr, char **envp ){
  24. #ifdef __SUPPORT_LD_DEBUG__
  25. if ( _dl_debug_vdso != 0 ){
  26. _dl_dprintf(2,"_dl_vdso support not enabled\n" );
  27. }
  28. #endif
  29. }
  30. #else
  31. void *_dl__vdso_gettimeofday = 0;
  32. void *_dl__vdso_clock_gettime = 0;
  33. #if defined(__UCLIBC_USE_TIME64__)
  34. void *_dl__vdso_clock_gettime64 = 0;
  35. #endif
  36. void *_get__dl__vdso_clock_gettime(void);
  37. void *_get__dl__vdso_clock_gettime(void)
  38. {
  39. return _dl__vdso_clock_gettime;
  40. }
  41. #if defined(__UCLIBC_USE_TIME64__)
  42. void *_get__dl__vdso_clock_gettime64(void);
  43. void *_get__dl__vdso_clock_gettime64(void)
  44. {
  45. return _dl__vdso_clock_gettime64;
  46. }
  47. #endif
  48. void *_get__dl__vdso_gettimeofday(void);
  49. void *_get__dl__vdso_gettimeofday(void)
  50. {
  51. return _dl__vdso_gettimeofday;
  52. }
  53. typedef struct {
  54. void* base_addr;
  55. ELF(Ehdr) *hdr;
  56. char* section_header_strtab;
  57. ELF(Sym) *dynsym_table;
  58. uint32_t dynsym_table_num;
  59. char* dynstr_table;
  60. uint16_t *versym_table;
  61. ELF(Verdef) *verdef_table;
  62. uint32_t verdef_num;
  63. ELF(Dyn) *dynamic_section;
  64. uint32_t dynamic_section_num;
  65. char* text_section;
  66. char* vers_strings[10];
  67. } elf_infos;
  68. /*
  69. * the raise() dummy function is needed because of divisons in this code
  70. * but keep it hidden in this object
  71. *
  72. * fixes link error with gcc 12 for arm
  73. */
  74. #pragma GCC visibility push(hidden)
  75. int raise(int sig){
  76. sig = sig;
  77. return 0;
  78. }
  79. #pragma GCC visibility pop
  80. static int vdso_check_elf_header( elf_infos* elf ){
  81. if ( 0 != _dl_memcmp( ELFMAG, elf->base_addr, 4 ) ){
  82. return 1;
  83. }
  84. if (elf->hdr->e_ident[EI_CLASS] != (ELF_BITS == 32 ? ELFCLASS32 : ELFCLASS64)) {
  85. _dl_dprintf(2,"vdso ELF Bits check error\n");
  86. return 1; /* Wrong ELF class -- check ELF_BITS */
  87. }
  88. return 0;
  89. }
  90. static ELF(Shdr) *vdso_get_sec_header( elf_infos* elf, int index ){
  91. return (ELF(Shdr) *) ( elf->base_addr + elf->hdr->e_shoff + ( index * sizeof( ELF(Shdr) )) );
  92. }
  93. void load_vdso(void *sys_info_ehdr, char **envp ){
  94. elf_infos vdso_infos;
  95. if ( sys_info_ehdr == 0 ){
  96. #ifdef __SUPPORT_LD_DEBUG__
  97. if ( _dl_debug_vdso != 0 ){
  98. _dl_dprintf(2,"_dl_vdso no vdso provied by kernel\n" );
  99. }
  100. #endif
  101. return;
  102. }
  103. char* _dl_vdso_disable = _dl_getenv("VDSO_DISABLE", envp);
  104. if ( _dl_vdso_disable != 0 ){
  105. #ifdef __SUPPORT_LD_DEBUG__
  106. if ( _dl_debug_vdso != 0 ){
  107. _dl_dprintf(2,"_dl_vdso vdso support disabled\n" );
  108. }
  109. #endif
  110. return;
  111. }
  112. _dl_memset( &vdso_infos, 0 , sizeof( elf_infos ) );
  113. vdso_infos.base_addr = (void*)sys_info_ehdr;
  114. vdso_infos.hdr = (ELF(Ehdr)*)vdso_infos.base_addr;
  115. if ( 0 != vdso_check_elf_header( &vdso_infos ) ){
  116. return;
  117. }
  118. ELF(Shdr) *sec_header = vdso_get_sec_header( &vdso_infos, vdso_infos.hdr->e_shstrndx);
  119. vdso_infos.section_header_strtab = ( vdso_infos.base_addr + sec_header->sh_offset );
  120. /*
  121. *
  122. * load ELF section headers
  123. *
  124. */
  125. for ( int i = 0 ; i < vdso_infos.hdr->e_shnum; i++ ){
  126. sec_header = vdso_get_sec_header( &vdso_infos, i );
  127. char* name = vdso_infos.section_header_strtab + sec_header->sh_name;
  128. if( ( SHT_DYNSYM == sec_header->sh_type ) && ( 0 == _dl_strcmp( ".dynsym",name ) ) ){
  129. vdso_infos.dynsym_table = ( vdso_infos.base_addr + sec_header->sh_offset );
  130. vdso_infos.dynsym_table_num = sec_header->sh_size / sec_header->sh_entsize ;
  131. continue;
  132. }
  133. if( ( SHT_STRTAB == sec_header->sh_type ) && ( 0 == _dl_strcmp( ".dynstr",name ) ) ){
  134. vdso_infos.dynstr_table = ( vdso_infos.base_addr + sec_header->sh_offset );
  135. continue;
  136. }
  137. if( ( SHT_GNU_versym == sec_header->sh_type ) && ( 0 == _dl_strcmp( ".gnu.version",name ) ) ){
  138. vdso_infos.versym_table = ( vdso_infos.base_addr + sec_header->sh_offset );
  139. continue;
  140. }
  141. if( ( SHT_GNU_verdef == sec_header->sh_type ) && ( 0 == _dl_strcmp( ".gnu.version_d",name ) ) ){
  142. vdso_infos.verdef_table = ( vdso_infos.base_addr + sec_header->sh_offset );
  143. continue;
  144. }
  145. if( ( SHT_DYNAMIC == sec_header->sh_type ) && ( 0 == _dl_strcmp( ".dynamic",name ) ) ){
  146. vdso_infos.dynamic_section = ( vdso_infos.base_addr + sec_header->sh_offset );
  147. vdso_infos.dynamic_section_num = sec_header->sh_size / sec_header->sh_entsize ;
  148. continue;
  149. }
  150. if( ( SHT_PROGBITS == sec_header->sh_type ) && ( 0 == _dl_strcmp( ".text",name ) ) ){
  151. vdso_infos.text_section = ( vdso_infos.base_addr + sec_header->sh_offset );
  152. continue;
  153. }
  154. }
  155. /*
  156. *
  157. * check section header -> dynamic table consistence
  158. *
  159. */
  160. for( int i = 0 ; i < vdso_infos.dynamic_section_num ; i++ ){
  161. ELF(Dyn) *dyn_sec = &vdso_infos.dynamic_section[i];
  162. if ( dyn_sec->d_tag == 0 ) continue;
  163. if ( dyn_sec->d_tag == DT_STRTAB ){
  164. char* strtab = ( vdso_infos.base_addr + dyn_sec->d_un.d_ptr );
  165. if ( strtab != (char*) vdso_infos.dynstr_table ){
  166. _dl_dprintf(2,"vdso elf DT_STRTAB check error\n");
  167. return;
  168. }
  169. continue;
  170. }
  171. if ( dyn_sec->d_tag == DT_SYMTAB ){
  172. char* symtab = ( vdso_infos.base_addr + dyn_sec->d_un.d_ptr );
  173. if ( symtab != (char*) vdso_infos.dynsym_table ){
  174. _dl_dprintf(2,"vdso elf DT_SYMTAB check error\n");
  175. return;
  176. }
  177. continue;
  178. }
  179. if ( dyn_sec->d_tag == DT_VERDEF ){
  180. Elf32_Verdef* verdef = ( vdso_infos.base_addr + dyn_sec->d_un.d_ptr );
  181. if ( verdef != (Elf32_Verdef*) vdso_infos.verdef_table ){
  182. _dl_dprintf(2,"vdso elf DT_VERDEF check error\n");
  183. return;
  184. }
  185. continue;
  186. }
  187. if ( dyn_sec->d_tag == DT_VERDEFNUM ){
  188. vdso_infos.verdef_num = dyn_sec->d_un.d_val;
  189. continue;
  190. }
  191. if ( dyn_sec->d_tag == DT_VERSYM ){
  192. uint16_t* versym = ( vdso_infos.base_addr + dyn_sec->d_un.d_ptr );
  193. if ( versym != vdso_infos.versym_table ){
  194. _dl_dprintf(2,"vdso elf DT_VERSYM check error\n");
  195. return;
  196. }
  197. continue;
  198. }
  199. }
  200. /*
  201. *
  202. * load vdso version definition strings
  203. *
  204. */
  205. ELF(Verdef) *vd = vdso_infos.verdef_table;
  206. for( int i = 0 ; i < vdso_infos.verdef_num ; i++ ){
  207. ELF(Verdaux) *vd_aux = (ELF(Verdaux) *)(( ( char*)vd ) + vd->vd_aux);
  208. vdso_infos.vers_strings[ vd->vd_ndx ] = vdso_infos.dynstr_table + vd_aux->vda_name;
  209. vd = ( ELF(Verdef) *)(( ( char*)vd ) + vd->vd_next);
  210. }
  211. /*
  212. *
  213. * load function from the vdso
  214. *
  215. */
  216. #ifdef __SUPPORT_LD_DEBUG__
  217. if ( _dl_debug_vdso != 0 ){
  218. int vdso_functions = 0;
  219. for( int i = 0 ; i < vdso_infos.dynsym_table_num ; i++ ){
  220. ELF(Sym)* sym = &vdso_infos.dynsym_table[i];
  221. if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
  222. continue;
  223. char* name = vdso_infos.dynstr_table + sym->st_name;
  224. if ( name[0] == 0 ){
  225. continue;
  226. }
  227. vdso_functions++;
  228. }
  229. _dl_dprintf(2,"_dl_vdso_load functions found : %d\n", vdso_functions );
  230. }
  231. #endif
  232. for( int i = 0 ; i < vdso_infos.dynsym_table_num ; i++ ){
  233. ELF(Sym)* sym = &vdso_infos.dynsym_table[i];
  234. if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
  235. continue;
  236. char* name = vdso_infos.dynstr_table + sym->st_name;
  237. void* func_addr = (void*)( vdso_infos.base_addr + sym->st_value );
  238. // the function name is patched to zero if the kernel has no timer which is
  239. // usable for the function
  240. if ( name[0] == 0 ){
  241. #ifdef __SUPPORT_LD_DEBUG__
  242. if ( _dl_debug_vdso != 0 ){
  243. _dl_dprintf(2," function at address %p disabled by the kernel\n", sym->st_value );
  244. }
  245. #endif
  246. continue;
  247. }
  248. if ( 0 == _dl_strcmp( name, __ARCH_VDSO_GETTIMEOFDAY_NAME ) ){
  249. _dl__vdso_gettimeofday = func_addr;
  250. #ifdef __SUPPORT_LD_DEBUG__
  251. if ( _dl_debug_vdso != 0 ){
  252. _dl_dprintf(2," %s at address %p\n", name, func_addr );
  253. }
  254. #endif
  255. continue;
  256. }
  257. if ( 0 == _dl_strcmp( name, __ARCH_VDSO_CLOCK_GETTIME_NAME ) ){
  258. _dl__vdso_clock_gettime = func_addr;
  259. #ifdef __SUPPORT_LD_DEBUG__
  260. if ( _dl_debug_vdso != 0 ){
  261. _dl_dprintf(2," %s at address %p\n", name, func_addr );
  262. }
  263. #endif
  264. continue;
  265. }
  266. #if defined(__UCLIBC_USE_TIME64__)
  267. if ( 0 == _dl_strcmp( name, __ARCH_VDSO_CLOCK_GETTIME64_NAME ) ){
  268. _dl__vdso_clock_gettime64 = func_addr;
  269. #ifdef __SUPPORT_LD_DEBUG__
  270. if ( _dl_debug_vdso != 0 ){
  271. _dl_dprintf(2," %s at address %p\n", name, func_addr );
  272. }
  273. #endif
  274. continue;
  275. }
  276. #endif /* defined(__UCLIBC_USE_TIME64__) */
  277. #ifdef __SUPPORT_LD_DEBUG__
  278. if ( _dl_debug_vdso != 0 ){
  279. _dl_dprintf(2," <%s> not handled\n", name );
  280. }
  281. #endif
  282. }
  283. }
  284. #endif // __VDSO_SUPPORT__