dl-support.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 <elf.h>
  15. #if defined(USE_TLS) && USE_TLS
  16. #include <assert.h>
  17. #include <tls.h>
  18. #include <ldsodefs.h>
  19. #include <string.h>
  20. #endif
  21. #if defined(USE_TLS) && USE_TLS
  22. void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
  23. #endif
  24. ElfW(Phdr) *_dl_phdr;
  25. size_t _dl_phnum;
  26. size_t _dl_pagesize;
  27. void internal_function _dl_aux_init (ElfW(auxv_t) *av);
  28. void internal_function _dl_aux_init (ElfW(auxv_t) *av)
  29. {
  30. /* Get the program headers base address from the aux vect */
  31. _dl_phdr = (ElfW(Phdr) *) av[AT_PHDR].a_un.a_val;
  32. /* Get the number of program headers from the aux vect */
  33. _dl_phnum = (size_t) av[AT_PHNUM].a_un.a_val;
  34. /* Get the pagesize from the aux vect */
  35. _dl_pagesize = (av[AT_PAGESZ].a_un.a_val) ? (size_t) av[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
  36. }
  37. #if defined(USE_TLS) && USE_TLS
  38. /* Initialize static TLS area and DTV for current (only) thread.
  39. libpthread implementations should provide their own hook
  40. to handle all threads. */
  41. void
  42. attribute_hidden
  43. _dl_nothread_init_static_tls (struct link_map *map)
  44. {
  45. # if defined(TLS_TCB_AT_TP)
  46. void *dest = (char *) THREAD_SELF - map->l_tls_offset;
  47. # elif defined(TLS_DTV_AT_TP)
  48. void *dest = (char *) THREAD_SELF + map->l_tls_offset + TLS_PRE_TCB_SIZE;
  49. # else
  50. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  51. # endif
  52. /* Fill in the DTV slot so that a later LD/GD access will find it. */
  53. dtv_t *dtv = THREAD_DTV ();
  54. assert (map->l_tls_modid <= dtv[-1].counter);
  55. dtv[map->l_tls_modid].pointer.val = dest;
  56. dtv[map->l_tls_modid].pointer.is_static = true;
  57. /* Initialize the memory. */
  58. memset (mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
  59. '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
  60. }
  61. #endif