dl-support.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Support for dynamic linking code in static libc.
  3. * Copyright (C) 1996-2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  4. *
  5. * Partially based on GNU C Library (file: libc/elf/dl-support.c)
  6. *
  7. * Copyright (C) 2008 STMicroelectronics Ltd.
  8. * Author: Carmelo Amoroso <carmelo.amoroso@st.com>
  9. *
  10. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  11. *
  12. */
  13. #include <link.h>
  14. #include <ldso.h>
  15. #include <elf.h>
  16. #if defined(USE_TLS) && USE_TLS
  17. #include <assert.h>
  18. #include <tls.h>
  19. #include <ldsodefs.h>
  20. #include <string.h>
  21. #endif
  22. #include <bits/uClibc_page.h>
  23. #if defined(USE_TLS) && USE_TLS
  24. void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
  25. #endif
  26. ElfW(Phdr) *_dl_phdr;
  27. size_t _dl_phnum;
  28. size_t _dl_pagesize;
  29. ElfW(auxv_t) _dl_auxvt[AUX_MAX_AT_ID];
  30. ElfW(auxv_t) *_dl_auxv_start;
  31. void internal_function _dl_aux_init (ElfW(auxv_t) *av);
  32. void internal_function _dl_aux_init (ElfW(auxv_t) *av)
  33. {
  34. _dl_auxv_start = av;
  35. memset(_dl_auxvt, 0x00, sizeof(_dl_auxvt));
  36. for (; av->a_type != AT_NULL; av++)
  37. {
  38. if (av->a_type < AUX_MAX_AT_ID)
  39. _dl_auxvt[av->a_type] = *av;
  40. }
  41. /* Get the program headers base address from the aux vect */
  42. _dl_phdr = (ElfW(Phdr) *) _dl_auxvt[AT_PHDR].a_un.a_val;
  43. /* Get the number of program headers from the aux vect */
  44. _dl_phnum = (size_t) _dl_auxvt[AT_PHNUM].a_un.a_val;
  45. /* Get the pagesize from the aux vect */
  46. _dl_pagesize = (_dl_auxvt[AT_PAGESZ].a_un.a_val) ? (size_t) _dl_auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
  47. }
  48. #if defined(USE_TLS) && USE_TLS
  49. /* Initialize static TLS area and DTV for current (only) thread.
  50. libpthread implementations should provide their own hook
  51. to handle all threads. */
  52. void
  53. attribute_hidden
  54. _dl_nothread_init_static_tls (struct link_map *map)
  55. {
  56. # if defined(TLS_TCB_AT_TP)
  57. void *dest = (char *) THREAD_SELF - map->l_tls_offset;
  58. # elif defined(TLS_DTV_AT_TP)
  59. void *dest = (char *) THREAD_SELF + map->l_tls_offset + TLS_PRE_TCB_SIZE;
  60. # else
  61. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  62. # endif
  63. /* Fill in the DTV slot so that a later LD/GD access will find it. */
  64. dtv_t *dtv = THREAD_DTV ();
  65. assert (map->l_tls_modid <= dtv[-1].counter);
  66. dtv[map->l_tls_modid].pointer.val = dest;
  67. dtv[map->l_tls_modid].pointer.is_static = true;
  68. /* Initialize the memory. */
  69. memset (mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
  70. '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
  71. }
  72. #endif