dl-tls.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Thread-local storage handling in the ELF dynamic linker.
  4. *
  5. * Copyright (C) 2005 by Steven J. Hill <sjhill@realitydiluted.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. The name of the above contributors may not be
  13. * used to endorse or promote products derived from this software
  14. * without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <tls.h>
  29. #include <dl-tls.h>
  30. #include <ldsodefs.h>
  31. void *(*_dl_calloc_function) (size_t __nmemb, size_t __size) = NULL;
  32. void *(*_dl_realloc_function) (void *__ptr, size_t __size) = NULL;
  33. void *(*_dl_memalign_function) (size_t __boundary, size_t __size) = NULL;
  34. void (*_dl_free_function) (void *__ptr);
  35. void *_dl_memalign (size_t __boundary, size_t __size);
  36. struct link_map *_dl_update_slotinfo (unsigned long int req_modid);
  37. /* Round up N to the nearest multiple of P, where P is a power of 2
  38. --- without using libgcc division routines. */
  39. #define roundup_pow2(n, p) (((n) + (p) - 1) & ~((p) - 1))
  40. void *
  41. _dl_calloc (size_t __nmemb, size_t __size)
  42. {
  43. void *result;
  44. size_t size = (__size * __nmemb);
  45. if (_dl_calloc_function)
  46. return (*_dl_calloc_function) (__nmemb, __size);
  47. if ((result = _dl_malloc(size)) != NULL) {
  48. _dl_memset(result, 0, size);
  49. }
  50. return result;
  51. }
  52. void *
  53. _dl_realloc (void * __ptr, size_t __size)
  54. {
  55. if (_dl_realloc_function)
  56. return (*_dl_realloc_function) (__ptr, __size);
  57. _dl_debug_early("NOT IMPLEMENTED PROPERLY!!!\n");
  58. return NULL;
  59. }
  60. /* The __tls_get_addr function has two basic forms which differ in the
  61. arguments. The IA-64 form takes two parameters, the module ID and
  62. offset. The form used, among others, on IA-32 takes a reference to
  63. a special structure which contain the same information. The second
  64. form seems to be more often used (in the moment) so we default to
  65. it. Users of the IA-64 form have to provide adequate definitions
  66. of the following macros. */
  67. #ifndef GET_ADDR_ARGS
  68. # define GET_ADDR_ARGS tls_index *ti
  69. #endif
  70. #ifndef GET_ADDR_MODULE
  71. # define GET_ADDR_MODULE ti->ti_module
  72. #endif
  73. #ifndef GET_ADDR_OFFSET
  74. # define GET_ADDR_OFFSET ti->ti_offset
  75. #endif
  76. /*
  77. * Amount of excess space to allocate in the static TLS area
  78. * to allow dynamic loading of modules defining IE-model TLS data.
  79. */
  80. #define TLS_STATIC_SURPLUS 64 + DL_NNS * 100
  81. /* Value used for dtv entries for which the allocation is delayed. */
  82. #define TLS_DTV_UNALLOCATED ((void *) -1l)
  83. /*
  84. * We are trying to perform a static TLS relocation in MAP, but it was
  85. * dynamically loaded. This can only work if there is enough surplus in
  86. * the static TLS area already allocated for each running thread. If this
  87. * object's TLS segment is too big to fit, we fail. If it fits,
  88. * we set MAP->l_tls_offset and return.
  89. * This function intentionally does not return any value but signals error
  90. * directly, as static TLS should be rare and code handling it should
  91. * not be inlined as much as possible.
  92. */
  93. void
  94. internal_function __attribute_noinline__
  95. _dl_allocate_static_tls (struct link_map *map)
  96. {
  97. /* If the alignment requirements are too high fail. */
  98. if (map->l_tls_align > _dl_tls_static_align)
  99. {
  100. fail:
  101. _dl_dprintf(2, "cannot allocate memory in static TLS block");
  102. _dl_exit(30);
  103. }
  104. # ifdef TLS_TCB_AT_TP
  105. size_t freebytes;
  106. size_t n;
  107. size_t blsize;
  108. freebytes = _dl_tls_static_size - _dl_tls_static_used - TLS_TCB_SIZE;
  109. blsize = map->l_tls_blocksize + map->l_tls_firstbyte_offset;
  110. if (freebytes < blsize)
  111. goto fail;
  112. n = (freebytes - blsize) & ~(map->l_tls_align - 1);
  113. size_t offset = _dl_tls_static_used + (freebytes - n
  114. - map->l_tls_firstbyte_offset);
  115. map->l_tls_offset = _dl_tls_static_used = offset;
  116. # elif defined(TLS_DTV_AT_TP)
  117. size_t used;
  118. size_t check;
  119. size_t offset = roundup_pow2 (_dl_tls_static_used, map->l_tls_align);
  120. used = offset + map->l_tls_blocksize;
  121. check = used;
  122. /* dl_tls_static_used includes the TCB at the beginning. */
  123. if (check > _dl_tls_static_size)
  124. goto fail;
  125. map->l_tls_offset = offset;
  126. _dl_tls_static_used = used;
  127. # else
  128. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  129. # endif
  130. /*
  131. * If the object is not yet relocated we cannot initialize the
  132. * static TLS region. Delay it.
  133. */
  134. if (((struct elf_resolve *) map)->init_flag & RELOCS_DONE)
  135. {
  136. #ifdef SHARED
  137. /*
  138. * Update the slot information data for at least the generation of
  139. * the DSO we are allocating data for.
  140. */
  141. if (__builtin_expect (THREAD_DTV()[0].counter != _dl_tls_generation, 0))
  142. (void) _dl_update_slotinfo (map->l_tls_modid);
  143. #endif
  144. _dl_init_static_tls (map);
  145. }
  146. else
  147. map->l_need_tls_init = 1;
  148. }
  149. #ifdef SHARED
  150. /* Initialize static TLS area and DTV for current (only) thread.
  151. libpthread implementations should provide their own hook
  152. to handle all threads. */
  153. void
  154. attribute_hidden __attribute_noinline__
  155. _dl_nothread_init_static_tls (struct link_map *map)
  156. {
  157. # ifdef TLS_TCB_AT_TP
  158. void *dest = (char *) THREAD_SELF - map->l_tls_offset;
  159. # elif defined(TLS_DTV_AT_TP)
  160. void *dest = (char *) THREAD_SELF + map->l_tls_offset + TLS_PRE_TCB_SIZE;
  161. # else
  162. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  163. # endif
  164. /* Fill in the DTV slot so that a later LD/GD access will find it. */
  165. dtv_t *dtv = THREAD_DTV ();
  166. if (!(map->l_tls_modid <= dtv[-1].counter)) {
  167. _dl_dprintf(2, "map->l_tls_modid <= dtv[-1].counter FAILED!\n");
  168. _dl_exit(30);
  169. }
  170. dtv[map->l_tls_modid].pointer.val = dest;
  171. dtv[map->l_tls_modid].pointer.is_static = true;
  172. /* Initialize the memory. */
  173. _dl_memcpy(dest, map->l_tls_initimage, map->l_tls_initimage_size);
  174. _dl_memset((dest + map->l_tls_initimage_size), '\0',
  175. map->l_tls_blocksize - map->l_tls_initimage_size);
  176. }
  177. #endif
  178. /* Taken from glibc/sysdeps/generic/dl-tls.c */
  179. static void
  180. oom (void)
  181. {
  182. _dl_debug_early("cannot allocate thread-local memory: ABORT\n");
  183. _dl_exit(30);
  184. }
  185. size_t
  186. internal_function
  187. _dl_next_tls_modid (void)
  188. {
  189. size_t result;
  190. if (__builtin_expect (_dl_tls_dtv_gaps, false))
  191. {
  192. size_t disp = 0;
  193. struct dtv_slotinfo_list *runp = _dl_tls_dtv_slotinfo_list;
  194. /* Note that this branch will never be executed during program
  195. start since there are no gaps at that time. Therefore it
  196. does not matter that the dl_tls_dtv_slotinfo is not allocated
  197. yet when the function is called for the first times.
  198. NB: the offset +1 is due to the fact that DTV[0] is used
  199. for something else. */
  200. result = _dl_tls_static_nelem + 1;
  201. if (result <= _dl_tls_max_dtv_idx)
  202. do
  203. {
  204. while (result - disp < runp->len)
  205. {
  206. if (runp->slotinfo[result - disp].map == NULL)
  207. break;
  208. ++result;
  209. _dl_assert (result <= _dl_tls_max_dtv_idx + 1);
  210. }
  211. if (result - disp < runp->len)
  212. break;
  213. disp += runp->len;
  214. }
  215. while ((runp = runp->next) != NULL);
  216. if (result > _dl_tls_max_dtv_idx)
  217. {
  218. /* The new index must indeed be exactly one higher than the
  219. previous high. */
  220. _dl_assert (result == _dl_tls_max_dtv_idx + 1);
  221. /* There is no gap anymore. */
  222. _dl_tls_dtv_gaps = false;
  223. goto nogaps;
  224. }
  225. }
  226. else
  227. {
  228. /* No gaps, allocate a new entry. */
  229. nogaps:
  230. result = ++_dl_tls_max_dtv_idx;
  231. }
  232. return result;
  233. }
  234. void
  235. internal_function
  236. _dl_determine_tlsoffset (void)
  237. {
  238. size_t max_align = TLS_TCB_ALIGN;
  239. size_t freetop = 0;
  240. size_t freebottom = 0;
  241. /* The first element of the dtv slot info list is allocated. */
  242. _dl_assert (_dl_tls_dtv_slotinfo_list != NULL);
  243. /* There is at this point only one element in the
  244. dl_tls_dtv_slotinfo_list list. */
  245. _dl_assert (_dl_tls_dtv_slotinfo_list->next == NULL);
  246. struct dtv_slotinfo *slotinfo = _dl_tls_dtv_slotinfo_list->slotinfo;
  247. /* Determining the offset of the various parts of the static TLS
  248. block has several dependencies. In addition we have to work
  249. around bugs in some toolchains.
  250. Each TLS block from the objects available at link time has a size
  251. and an alignment requirement. The GNU ld computes the alignment
  252. requirements for the data at the positions *in the file*, though.
  253. I.e, it is not simply possible to allocate a block with the size
  254. of the TLS program header entry. The data is layed out assuming
  255. that the first byte of the TLS block fulfills
  256. p_vaddr mod p_align == &TLS_BLOCK mod p_align
  257. This means we have to add artificial padding at the beginning of
  258. the TLS block. These bytes are never used for the TLS data in
  259. this module but the first byte allocated must be aligned
  260. according to mod p_align == 0 so that the first byte of the TLS
  261. block is aligned according to p_vaddr mod p_align. This is ugly
  262. and the linker can help by computing the offsets in the TLS block
  263. assuming the first byte of the TLS block is aligned according to
  264. p_align.
  265. The extra space which might be allocated before the first byte of
  266. the TLS block need not go unused. The code below tries to use
  267. that memory for the next TLS block. This can work if the total
  268. memory requirement for the next TLS block is smaller than the
  269. gap. */
  270. # ifdef TLS_TCB_AT_TP
  271. /* We simply start with zero. */
  272. size_t cnt, offset = 0;
  273. for (cnt = 1; slotinfo[cnt].map != NULL; ++cnt)
  274. {
  275. _dl_assert (cnt < _dl_tls_dtv_slotinfo_list->len);
  276. size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
  277. & (slotinfo[cnt].map->l_tls_align - 1));
  278. size_t off;
  279. max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
  280. if (freebottom - freetop >= slotinfo[cnt].map->l_tls_blocksize)
  281. {
  282. off = roundup_pow2 (freetop + slotinfo[cnt].map->l_tls_blocksize
  283. - firstbyte, slotinfo[cnt].map->l_tls_align)
  284. + firstbyte;
  285. if (off <= freebottom)
  286. {
  287. freetop = off;
  288. /* XXX For some architectures we perhaps should store the
  289. negative offset. */
  290. slotinfo[cnt].map->l_tls_offset = off;
  291. continue;
  292. }
  293. }
  294. off = roundup_pow2 (offset + slotinfo[cnt].map->l_tls_blocksize
  295. - firstbyte, slotinfo[cnt].map->l_tls_align)
  296. + firstbyte;
  297. if (off > offset + slotinfo[cnt].map->l_tls_blocksize
  298. + (freebottom - freetop))
  299. {
  300. freetop = offset;
  301. freebottom = off - slotinfo[cnt].map->l_tls_blocksize;
  302. }
  303. offset = off;
  304. /* XXX For some architectures we perhaps should store the
  305. negative offset. */
  306. slotinfo[cnt].map->l_tls_offset = off;
  307. }
  308. _dl_tls_static_used = offset;
  309. _dl_tls_static_size = (roundup_pow2 (offset + TLS_STATIC_SURPLUS, max_align)
  310. + TLS_TCB_SIZE);
  311. # elif defined(TLS_DTV_AT_TP)
  312. /* The TLS blocks start right after the TCB. */
  313. size_t offset = TLS_TCB_SIZE;
  314. size_t cnt;
  315. for (cnt = 1; slotinfo[cnt].map != NULL; ++cnt)
  316. {
  317. _dl_assert (cnt < _dl_tls_dtv_slotinfo_list->len);
  318. size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
  319. & (slotinfo[cnt].map->l_tls_align - 1));
  320. size_t off;
  321. max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
  322. if (slotinfo[cnt].map->l_tls_blocksize <= freetop - freebottom)
  323. {
  324. off = roundup_pow2 (freebottom, slotinfo[cnt].map->l_tls_align);
  325. if (off - freebottom < firstbyte)
  326. off += slotinfo[cnt].map->l_tls_align;
  327. if (off + slotinfo[cnt].map->l_tls_blocksize - firstbyte <= freetop)
  328. {
  329. slotinfo[cnt].map->l_tls_offset = off - firstbyte;
  330. freebottom = (off + slotinfo[cnt].map->l_tls_blocksize
  331. - firstbyte);
  332. continue;
  333. }
  334. }
  335. off = roundup_pow2 (offset, slotinfo[cnt].map->l_tls_align);
  336. if (off - offset < firstbyte)
  337. off += slotinfo[cnt].map->l_tls_align;
  338. slotinfo[cnt].map->l_tls_offset = off - firstbyte;
  339. if (off - firstbyte - offset > freetop - freebottom)
  340. {
  341. freebottom = offset;
  342. freetop = off - firstbyte;
  343. }
  344. offset = off + slotinfo[cnt].map->l_tls_blocksize - firstbyte;
  345. }
  346. _dl_tls_static_used = offset;
  347. _dl_tls_static_size = roundup_pow2 (offset + TLS_STATIC_SURPLUS,
  348. TLS_TCB_ALIGN);
  349. # else
  350. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  351. # endif
  352. /* The alignment requirement for the static TLS block. */
  353. _dl_tls_static_align = max_align;
  354. }
  355. /* This is called only when the data structure setup was skipped at startup,
  356. when there was no need for it then. Now we have dynamically loaded
  357. something needing TLS, or libpthread needs it. */
  358. rtld_hidden_proto(_dl_tls_setup)
  359. int
  360. internal_function
  361. _dl_tls_setup (void)
  362. {
  363. _dl_assert (_dl_tls_dtv_slotinfo_list == NULL);
  364. _dl_assert (_dl_tls_max_dtv_idx == 0);
  365. const size_t nelem = 2 + TLS_SLOTINFO_SURPLUS;
  366. _dl_tls_dtv_slotinfo_list
  367. = _dl_calloc (1, (sizeof (struct dtv_slotinfo_list)
  368. + nelem * sizeof (struct dtv_slotinfo)));
  369. if (_dl_tls_dtv_slotinfo_list == NULL)
  370. return -1;
  371. _dl_tls_dtv_slotinfo_list->len = nelem;
  372. /* Number of elements in the static TLS block. It can't be zero
  373. because of various assumptions. The one element is null. */
  374. _dl_tls_static_nelem = _dl_tls_max_dtv_idx = 1;
  375. /* This initializes more variables for us. */
  376. _dl_determine_tlsoffset ();
  377. return 0;
  378. }
  379. rtld_hidden_def (_dl_tls_setup)
  380. static void *
  381. internal_function
  382. allocate_dtv (void *result)
  383. {
  384. dtv_t *dtv;
  385. size_t dtv_length;
  386. /* We allocate a few more elements in the dtv than are needed for the
  387. initial set of modules. This should avoid in most cases expansions
  388. of the dtv. */
  389. dtv_length = _dl_tls_max_dtv_idx + DTV_SURPLUS;
  390. dtv = _dl_calloc (dtv_length + 2, sizeof (dtv_t));
  391. if (dtv != NULL)
  392. {
  393. /* This is the initial length of the dtv. */
  394. dtv[0].counter = dtv_length;
  395. /* The rest of the dtv (including the generation counter) is
  396. Initialize with zero to indicate nothing there. */
  397. /* Add the dtv to the thread data structures. */
  398. INSTALL_DTV (result, dtv);
  399. }
  400. else
  401. result = NULL;
  402. return result;
  403. }
  404. /* Get size and alignment requirements of the static TLS block. */
  405. void
  406. internal_function
  407. _dl_get_tls_static_info (size_t *sizep, size_t *alignp)
  408. {
  409. *sizep = _dl_tls_static_size;
  410. *alignp = _dl_tls_static_align;
  411. }
  412. void *
  413. internal_function
  414. _dl_allocate_tls_storage (void)
  415. {
  416. void *result;
  417. size_t size = _dl_tls_static_size;
  418. # if defined(TLS_DTV_AT_TP)
  419. /* Memory layout is:
  420. [ TLS_PRE_TCB_SIZE ] [ TLS_TCB_SIZE ] [ TLS blocks ]
  421. ^ This should be returned. */
  422. size += (TLS_PRE_TCB_SIZE + _dl_tls_static_align - 1)
  423. & ~(_dl_tls_static_align - 1);
  424. # endif
  425. /* Allocate a correctly aligned chunk of memory. */
  426. result = _dl_memalign (_dl_tls_static_align, size);
  427. if (__builtin_expect (result != NULL, 1))
  428. {
  429. /* Allocate the DTV. */
  430. void *allocated = result;
  431. # ifdef TLS_TCB_AT_TP
  432. /* The TCB follows the TLS blocks. */
  433. result = (char *) result + size - TLS_TCB_SIZE;
  434. /* Clear the TCB data structure. We can't ask the caller (i.e.
  435. libpthread) to do it, because we will initialize the DTV et al. */
  436. _dl_memset (result, '\0', TLS_TCB_SIZE);
  437. # elif defined(TLS_DTV_AT_TP)
  438. result = (char *) result + size - _dl_tls_static_size;
  439. /* Clear the TCB data structure and TLS_PRE_TCB_SIZE bytes before it.
  440. We can't ask the caller (i.e. libpthread) to do it, because we will
  441. initialize the DTV et al. */
  442. _dl_memset ((char *) result - TLS_PRE_TCB_SIZE, '\0',
  443. TLS_PRE_TCB_SIZE + TLS_TCB_SIZE);
  444. # endif
  445. result = allocate_dtv (result);
  446. if (result == NULL)
  447. _dl_free (allocated);
  448. }
  449. return result;
  450. }
  451. void *
  452. internal_function
  453. _dl_allocate_tls_init (void *result)
  454. {
  455. if (result == NULL)
  456. /* The memory allocation failed. */
  457. return NULL;
  458. dtv_t *dtv = GET_DTV (result);
  459. struct dtv_slotinfo_list *listp;
  460. size_t total = 0;
  461. size_t maxgen = 0;
  462. /* We have to prepare the dtv for all currently loaded modules using
  463. TLS. For those which are dynamically loaded we add the values
  464. indicating deferred allocation. */
  465. listp = _dl_tls_dtv_slotinfo_list;
  466. while (1)
  467. {
  468. size_t cnt;
  469. for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
  470. {
  471. struct link_map *map;
  472. void *dest;
  473. /* Check for the total number of used slots. */
  474. if (total + cnt > _dl_tls_max_dtv_idx)
  475. break;
  476. map = listp->slotinfo[cnt].map;
  477. if (map == NULL)
  478. /* Unused entry. */
  479. continue;
  480. /* Keep track of the maximum generation number. This might
  481. not be the generation counter. */
  482. maxgen = MAX (maxgen, listp->slotinfo[cnt].gen);
  483. if (map->l_tls_offset == NO_TLS_OFFSET)
  484. {
  485. /* For dynamically loaded modules we simply store
  486. the value indicating deferred allocation. */
  487. dtv[map->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
  488. dtv[map->l_tls_modid].pointer.is_static = false;
  489. continue;
  490. }
  491. _dl_assert (map->l_tls_modid == cnt);
  492. _dl_assert (map->l_tls_blocksize >= map->l_tls_initimage_size);
  493. # ifdef TLS_TCB_AT_TP
  494. _dl_assert ((size_t) map->l_tls_offset >= map->l_tls_blocksize);
  495. dest = (char *) result - map->l_tls_offset;
  496. # elif defined(TLS_DTV_AT_TP)
  497. dest = (char *) result + map->l_tls_offset;
  498. # else
  499. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  500. # endif
  501. /* Copy the initialization image and clear the BSS part. */
  502. dtv[map->l_tls_modid].pointer.val = dest;
  503. dtv[map->l_tls_modid].pointer.is_static = true;
  504. _dl_memcpy(dest, map->l_tls_initimage, map->l_tls_initimage_size);
  505. _dl_memset((dest + map->l_tls_initimage_size), '\0',
  506. map->l_tls_blocksize - map->l_tls_initimage_size);
  507. }
  508. total += cnt;
  509. if (total >= _dl_tls_max_dtv_idx)
  510. break;
  511. listp = listp->next;
  512. _dl_assert (listp != NULL);
  513. }
  514. /* The DTV version is up-to-date now. */
  515. dtv[0].counter = maxgen;
  516. return result;
  517. }
  518. void *
  519. internal_function
  520. _dl_allocate_tls (void *mem)
  521. {
  522. return _dl_allocate_tls_init (mem == NULL
  523. ? _dl_allocate_tls_storage ()
  524. : allocate_dtv (mem));
  525. }
  526. void
  527. internal_function
  528. _dl_deallocate_tls (void *tcb, bool dealloc_tcb)
  529. {
  530. dtv_t *dtv = GET_DTV (tcb);
  531. size_t cnt;
  532. /* We need to free the memory allocated for non-static TLS. */
  533. for (cnt = 0; cnt < dtv[-1].counter; ++cnt)
  534. if (! dtv[1 + cnt].pointer.is_static
  535. && dtv[1 + cnt].pointer.val != TLS_DTV_UNALLOCATED)
  536. _dl_free (dtv[1 + cnt].pointer.val);
  537. /* The array starts with dtv[-1]. */
  538. if (dtv != _dl_initial_dtv)
  539. _dl_free (dtv - 1);
  540. if (dealloc_tcb)
  541. {
  542. # ifdef TLS_TCB_AT_TP
  543. /* The TCB follows the TLS blocks. Back up to free the whole block. */
  544. tcb -= _dl_tls_static_size - TLS_TCB_SIZE;
  545. # elif defined(TLS_DTV_AT_TP)
  546. /* Back up the TLS_PRE_TCB_SIZE bytes. */
  547. tcb -= (TLS_PRE_TCB_SIZE + _dl_tls_static_align - 1)
  548. & ~(_dl_tls_static_align - 1);
  549. # endif
  550. _dl_free (tcb);
  551. }
  552. }
  553. static void *
  554. allocate_and_init (struct link_map *map)
  555. {
  556. void *newp;
  557. newp = _dl_memalign (map->l_tls_align, map->l_tls_blocksize);
  558. if (newp == NULL)
  559. {
  560. _dl_dprintf(2, "%s:%d: Out of memory!!!\n", __FUNCTION__, __LINE__);
  561. _dl_exit(1);
  562. }
  563. /* Initialize the memory. */
  564. _dl_memcpy (newp, map->l_tls_initimage, map->l_tls_initimage_size);
  565. _dl_memset ((newp + map->l_tls_initimage_size), '\0',
  566. map->l_tls_blocksize - map->l_tls_initimage_size);
  567. return newp;
  568. }
  569. struct link_map *
  570. _dl_update_slotinfo (unsigned long int req_modid)
  571. {
  572. struct link_map *the_map = NULL;
  573. dtv_t *dtv = THREAD_DTV ();
  574. /* The global dl_tls_dtv_slotinfo array contains for each module
  575. index the generation counter current when the entry was created.
  576. This array never shrinks so that all module indices which were
  577. valid at some time can be used to access it. Before the first
  578. use of a new module index in this function the array was extended
  579. appropriately. Access also does not have to be guarded against
  580. modifications of the array. It is assumed that pointer-size
  581. values can be read atomically even in SMP environments. It is
  582. possible that other threads at the same time dynamically load
  583. code and therefore add to the slotinfo list. This is a problem
  584. since we must not pick up any information about incomplete work.
  585. The solution to this is to ignore all dtv slots which were
  586. created after the one we are currently interested. We know that
  587. dynamic loading for this module is completed and this is the last
  588. load operation we know finished. */
  589. unsigned long int idx = req_modid;
  590. struct dtv_slotinfo_list *listp = _dl_tls_dtv_slotinfo_list;
  591. _dl_debug_early ("Updating slotinfo for module %d\n", req_modid);
  592. while (idx >= listp->len)
  593. {
  594. idx -= listp->len;
  595. listp = listp->next;
  596. }
  597. if (dtv[0].counter < listp->slotinfo[idx].gen)
  598. {
  599. /* The generation counter for the slot is higher than what the
  600. current dtv implements. We have to update the whole dtv but
  601. only those entries with a generation counter <= the one for
  602. the entry we need. */
  603. size_t new_gen = listp->slotinfo[idx].gen;
  604. size_t total = 0;
  605. /* We have to look through the entire dtv slotinfo list. */
  606. listp = _dl_tls_dtv_slotinfo_list;
  607. do
  608. {
  609. size_t cnt;
  610. for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
  611. {
  612. size_t gen = listp->slotinfo[cnt].gen;
  613. if (gen > new_gen)
  614. /* This is a slot for a generation younger than the
  615. one we are handling now. It might be incompletely
  616. set up so ignore it. */
  617. continue;
  618. /* If the entry is older than the current dtv layout we
  619. know we don't have to handle it. */
  620. if (gen <= dtv[0].counter)
  621. continue;
  622. /* If there is no map this means the entry is empty. */
  623. struct link_map *map = listp->slotinfo[cnt].map;
  624. if (map == NULL)
  625. {
  626. /* If this modid was used at some point the memory
  627. might still be allocated. */
  628. if (! dtv[total + cnt].pointer.is_static
  629. && dtv[total + cnt].pointer.val != TLS_DTV_UNALLOCATED)
  630. {
  631. _dl_free (dtv[total + cnt].pointer.val);
  632. dtv[total + cnt].pointer.val = TLS_DTV_UNALLOCATED;
  633. }
  634. continue;
  635. }
  636. /* Check whether the current dtv array is large enough. */
  637. size_t modid = map->l_tls_modid;
  638. _dl_assert (total + cnt == modid);
  639. if (dtv[-1].counter < modid)
  640. {
  641. /* Reallocate the dtv. */
  642. dtv_t *newp;
  643. size_t newsize = _dl_tls_max_dtv_idx + DTV_SURPLUS;
  644. size_t oldsize = dtv[-1].counter;
  645. _dl_assert (map->l_tls_modid <= newsize);
  646. if (dtv == _dl_initial_dtv)
  647. {
  648. /* This is the initial dtv that was allocated
  649. during rtld startup using the dl-minimal.c
  650. malloc instead of the real malloc. We can't
  651. free it, we have to abandon the old storage. */
  652. newp = _dl_malloc ((2 + newsize) * sizeof (dtv_t));
  653. if (newp == NULL)
  654. oom ();
  655. _dl_memcpy (newp, &dtv[-1], oldsize * sizeof (dtv_t));
  656. }
  657. else
  658. {
  659. newp = _dl_realloc (&dtv[-1],
  660. (2 + newsize) * sizeof (dtv_t));
  661. if (newp == NULL)
  662. oom ();
  663. }
  664. newp[0].counter = newsize;
  665. /* Clear the newly allocated part. */
  666. _dl_memset (newp + 2 + oldsize, '\0',
  667. (newsize - oldsize) * sizeof (dtv_t));
  668. /* Point dtv to the generation counter. */
  669. dtv = &newp[1];
  670. /* Install this new dtv in the thread data
  671. structures. */
  672. INSTALL_NEW_DTV (dtv);
  673. }
  674. /* If there is currently memory allocate for this
  675. dtv entry free it. */
  676. /* XXX Ideally we will at some point create a memory
  677. pool. */
  678. if (! dtv[modid].pointer.is_static
  679. && dtv[modid].pointer.val != TLS_DTV_UNALLOCATED)
  680. /* Note that free is called for NULL is well. We
  681. deallocate even if it is this dtv entry we are
  682. supposed to load. The reason is that we call
  683. memalign and not malloc. */
  684. _dl_free (dtv[modid].pointer.val);
  685. /* This module is loaded dynamically- We defer memory
  686. allocation. */
  687. dtv[modid].pointer.is_static = false;
  688. dtv[modid].pointer.val = TLS_DTV_UNALLOCATED;
  689. if (modid == req_modid)
  690. the_map = map;
  691. }
  692. total += listp->len;
  693. }
  694. while ((listp = listp->next) != NULL);
  695. /* This will be the new maximum generation counter. */
  696. dtv[0].counter = new_gen;
  697. }
  698. return the_map;
  699. }
  700. /* The generic dynamic and local dynamic model cannot be used in
  701. statically linked applications. */
  702. void *
  703. __tls_get_addr (GET_ADDR_ARGS)
  704. {
  705. dtv_t *dtv = THREAD_DTV ();
  706. struct link_map *the_map = NULL;
  707. void *p;
  708. if (__builtin_expect (dtv[0].counter != _dl_tls_generation, 0))
  709. {
  710. the_map = _dl_update_slotinfo (GET_ADDR_MODULE);
  711. dtv = THREAD_DTV ();
  712. }
  713. p = dtv[GET_ADDR_MODULE].pointer.val;
  714. if (__builtin_expect (p == TLS_DTV_UNALLOCATED, 0))
  715. {
  716. /* The allocation was deferred. Do it now. */
  717. if (the_map == NULL)
  718. {
  719. /* Find the link map for this module. */
  720. size_t idx = GET_ADDR_MODULE;
  721. struct dtv_slotinfo_list *listp = _dl_tls_dtv_slotinfo_list;
  722. while (idx >= listp->len)
  723. {
  724. idx -= listp->len;
  725. listp = listp->next;
  726. }
  727. the_map = listp->slotinfo[idx].map;
  728. }
  729. p = dtv[GET_ADDR_MODULE].pointer.val = allocate_and_init (the_map);
  730. dtv[GET_ADDR_MODULE].pointer.is_static = false;
  731. }
  732. return (char *) p + GET_ADDR_OFFSET;
  733. }
  734. void
  735. _dl_add_to_slotinfo (struct link_map *l)
  736. {
  737. /* Now that we know the object is loaded successfully add
  738. modules containing TLS data to the dtv info table. We
  739. might have to increase its size. */
  740. struct dtv_slotinfo_list *listp;
  741. struct dtv_slotinfo_list *prevp;
  742. size_t idx = l->l_tls_modid;
  743. _dl_debug_early("Adding to slotinfo for %s\n", l->l_name);
  744. /* Find the place in the dtv slotinfo list. */
  745. listp = _dl_tls_dtv_slotinfo_list;
  746. prevp = NULL; /* Needed to shut up gcc. */
  747. do
  748. {
  749. /* Does it fit in the array of this list element? */
  750. if (idx < listp->len)
  751. break;
  752. idx -= listp->len;
  753. prevp = listp;
  754. listp = listp->next;
  755. }
  756. while (listp != NULL);
  757. if (listp == NULL)
  758. {
  759. /* When we come here it means we have to add a new element
  760. to the slotinfo list. And the new module must be in
  761. the first slot. */
  762. _dl_assert (idx == 0);
  763. listp = prevp->next = (struct dtv_slotinfo_list *)
  764. _dl_malloc (sizeof (struct dtv_slotinfo_list)
  765. + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
  766. if (listp == NULL)
  767. {
  768. /* We ran out of memory. We will simply fail this
  769. call but don't undo anything we did so far. The
  770. application will crash or be terminated anyway very
  771. soon. */
  772. /* We have to do this since some entries in the dtv
  773. slotinfo array might already point to this
  774. generation. */
  775. ++_dl_tls_generation;
  776. _dl_dprintf (_dl_debug_file,
  777. "cannot create TLS data structures: ABORT\n");
  778. _dl_exit (127);
  779. }
  780. listp->len = TLS_SLOTINFO_SURPLUS;
  781. listp->next = NULL;
  782. _dl_memset (listp->slotinfo, '\0',
  783. TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
  784. }
  785. /* Add the information into the slotinfo data structure. */
  786. listp->slotinfo[idx].map = l;
  787. listp->slotinfo[idx].gen = _dl_tls_generation + 1;
  788. /* ??? ideally this would be done once per call to dlopen. However there's
  789. no easy way to indicate whether a library used TLS, so do it here
  790. instead. */
  791. /* Bump the TLS generation number. */
  792. _dl_tls_generation++;
  793. }
  794. /* Taken from glibc/elf/rtld.c */
  795. static bool tls_init_tp_called;
  796. /* _dl_error_catch_tsd points to this for the single-threaded case.
  797. It's reset by the thread library for multithreaded programs. */
  798. void ** __attribute__ ((const))
  799. _dl_initial_error_catch_tsd (void)
  800. {
  801. static void *data;
  802. return &data;
  803. }
  804. #ifdef SHARED
  805. void*
  806. internal_function
  807. init_tls (void);
  808. rtld_hidden_proto(init_tls)
  809. void *
  810. internal_function
  811. init_tls (void)
  812. {
  813. /* Number of elements in the static TLS block. */
  814. _dl_tls_static_nelem = _dl_tls_max_dtv_idx;
  815. /* Do not do this twice. The audit interface might have required
  816. the DTV interfaces to be set up early. */
  817. if (_dl_initial_dtv != NULL)
  818. return NULL;
  819. /* Allocate the array which contains the information about the
  820. dtv slots. We allocate a few entries more than needed to
  821. avoid the need for reallocation. */
  822. size_t nelem = _dl_tls_max_dtv_idx + 1 + TLS_SLOTINFO_SURPLUS;
  823. /* Allocate. */
  824. _dl_assert (_dl_tls_dtv_slotinfo_list == NULL);
  825. _dl_tls_dtv_slotinfo_list = (struct dtv_slotinfo_list *)
  826. _dl_calloc (sizeof (struct dtv_slotinfo_list)
  827. + nelem * sizeof (struct dtv_slotinfo), 1);
  828. /* No need to check the return value. If memory allocation failed
  829. the program would have been terminated. */
  830. struct dtv_slotinfo *slotinfo = _dl_tls_dtv_slotinfo_list->slotinfo;
  831. _dl_tls_dtv_slotinfo_list->len = nelem;
  832. _dl_tls_dtv_slotinfo_list->next = NULL;
  833. /* Fill in the information from the loaded modules. No namespace
  834. but the base one can be filled at this time. */
  835. int i = 0;
  836. struct link_map *l;
  837. for (l = (struct link_map *) _dl_loaded_modules; l != NULL; l = l->l_next)
  838. if (l->l_tls_blocksize != 0)
  839. {
  840. /* This is a module with TLS data. Store the map reference.
  841. The generation counter is zero. */
  842. /* Skeep slot[0]: it will be never used */
  843. slotinfo[++i].map = l;
  844. }
  845. _dl_assert (i == _dl_tls_max_dtv_idx);
  846. /* Compute the TLS offsets for the various blocks. */
  847. _dl_determine_tlsoffset ();
  848. /* Construct the static TLS block and the dtv for the initial
  849. thread. For some platforms this will include allocating memory
  850. for the thread descriptor. The memory for the TLS block will
  851. never be freed. It should be allocated accordingly. The dtv
  852. array can be changed if dynamic loading requires it. */
  853. void *tcbp = _dl_allocate_tls_storage ();
  854. if (tcbp == NULL) {
  855. _dl_debug_early("\ncannot allocate TLS data structures for initial thread");
  856. _dl_exit(30);
  857. }
  858. /* Store for detection of the special case by __tls_get_addr
  859. so it knows not to pass this dtv to the normal realloc. */
  860. _dl_initial_dtv = GET_DTV (tcbp);
  861. /* And finally install it for the main thread. If ld.so itself uses
  862. TLS we know the thread pointer was initialized earlier. */
  863. const char *lossage = (char *)TLS_INIT_TP (tcbp, USE___THREAD);
  864. if(__builtin_expect (lossage != NULL, 0)) {
  865. _dl_debug_early("cannot set up thread-local storage: %s\n", lossage);
  866. _dl_exit(30);
  867. }
  868. tls_init_tp_called = true;
  869. return tcbp;
  870. }
  871. rtld_hidden_def (init_tls)
  872. #endif