dl-vdso.c 9.8 KB

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