dl-tls.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /* Thread-local storage handling in the ELF dynamic linker. Generic version.
  2. Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #if defined SHARED || defined NOT_IN_libc
  16. # error in buildsystem: This file is for libc.a
  17. #endif
  18. #include <libintl.h>
  19. #include <signal.h>
  20. #include <stdlib.h>
  21. #include <sys/param.h>
  22. #include <tls.h>
  23. #include <dl-tls.h>
  24. #include <ldsodefs.h>
  25. #include <dl-elf.h>
  26. #include <dl-hash.h>
  27. #include <assert.h>
  28. #include <link.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <stdio.h>
  32. #define _dl_malloc malloc
  33. #define _dl_memset memset
  34. #define _dl_mempcpy mempcpy
  35. #define _dl_dprintf fprintf
  36. #define _dl_debug_file stderr
  37. #define _dl_exit exit
  38. /* Amount of excess space to allocate in the static TLS area
  39. to allow dynamic loading of modules defining IE-model TLS data. */
  40. # define TLS_STATIC_SURPLUS 64 + DL_NNS * 100
  41. /* Value used for dtv entries for which the allocation is delayed. */
  42. # define TLS_DTV_UNALLOCATED ((void *) -1l)
  43. /* Out-of-memory handler. */
  44. # ifdef SHARED
  45. static void
  46. __attribute__ ((__noreturn__))
  47. oom (void)
  48. {
  49. do {
  50. _dl_dprintf (_dl_debug_file,
  51. "cannot allocate thread-local memory: ABORT\n");
  52. _dl_exit (127);
  53. } while (1);
  54. }
  55. # endif
  56. void *_dl_memalign(size_t alignment, size_t bytes);
  57. void *_dl_memalign(size_t alignment, size_t bytes)
  58. {
  59. return _dl_malloc(bytes);
  60. }
  61. /*
  62. * We are trying to perform a static TLS relocation in MAP, but it was
  63. * dynamically loaded. This can only work if there is enough surplus in
  64. * the static TLS area already allocated for each running thread. If this
  65. * object's TLS segment is too big to fit, we fail. If it fits,
  66. * we set MAP->l_tls_offset and return.
  67. * This function intentionally does not return any value but signals error
  68. * directly, as static TLS should be rare and code handling it should
  69. * not be inlined as much as possible.
  70. */
  71. void
  72. internal_function __attribute_noinline__
  73. _dl_allocate_static_tls (struct link_map *map)
  74. {
  75. /* If the alignment requirements are too high fail. */
  76. if (map->l_tls_align > _dl_tls_static_align)
  77. {
  78. fail:
  79. _dl_dprintf(_dl_debug_file, "cannot allocate memory in static TLS block");
  80. _dl_exit(30);
  81. }
  82. # if defined(TLS_TCB_AT_TP)
  83. size_t freebytes;
  84. size_t n;
  85. size_t blsize;
  86. freebytes = _dl_tls_static_size - _dl_tls_static_used - TLS_TCB_SIZE;
  87. blsize = map->l_tls_blocksize + map->l_tls_firstbyte_offset;
  88. if (freebytes < blsize)
  89. goto fail;
  90. n = (freebytes - blsize) / map->l_tls_align;
  91. size_t offset = _dl_tls_static_used + (freebytes - n * map->l_tls_align
  92. - map->l_tls_firstbyte_offset);
  93. map->l_tls_offset = _dl_tls_static_used = offset;
  94. # elif defined(TLS_DTV_AT_TP)
  95. size_t used;
  96. size_t check;
  97. size_t offset = roundup (_dl_tls_static_used, map->l_tls_align);
  98. used = offset + map->l_tls_blocksize;
  99. check = used;
  100. /* dl_tls_static_used includes the TCB at the beginning. */
  101. if (check > _dl_tls_static_size)
  102. goto fail;
  103. map->l_tls_offset = offset;
  104. _dl_tls_static_used = used;
  105. # else
  106. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  107. # endif
  108. /*
  109. * If the object is not yet relocated we cannot initialize the
  110. * static TLS region. Delay it.
  111. */
  112. if (((struct elf_resolve *) map)->init_flag & RELOCS_DONE)
  113. {
  114. #ifdef SHARED
  115. /*
  116. * Update the slot information data for at least the generation of
  117. * the DSO we are allocating data for.
  118. */
  119. if (__builtin_expect (THREAD_DTV()[0].counter != _dl_tls_generation, 0))
  120. (void) _dl_update_slotinfo (map->l_tls_modid);
  121. #endif
  122. _dl_init_static_tls (map);
  123. }
  124. else
  125. map->l_need_tls_init = 1;
  126. }
  127. size_t
  128. internal_function
  129. _dl_next_tls_modid (void)
  130. {
  131. size_t result;
  132. if (__builtin_expect (GL(dl_tls_dtv_gaps), false))
  133. {
  134. size_t disp = 0;
  135. struct dtv_slotinfo_list *runp = GL(dl_tls_dtv_slotinfo_list);
  136. /* Note that this branch will never be executed during program
  137. start since there are no gaps at that time. Therefore it
  138. does not matter that the dl_tls_dtv_slotinfo is not allocated
  139. yet when the function is called for the first times.
  140. NB: the offset +1 is due to the fact that DTV[0] is used
  141. for something else. */
  142. result = GL(dl_tls_static_nelem) + 1;
  143. if (result <= GL(dl_tls_max_dtv_idx))
  144. do
  145. {
  146. while (result - disp < runp->len)
  147. {
  148. if (runp->slotinfo[result - disp].map == NULL)
  149. break;
  150. ++result;
  151. assert (result <= GL(dl_tls_max_dtv_idx) + 1);
  152. }
  153. if (result - disp < runp->len)
  154. break;
  155. disp += runp->len;
  156. }
  157. while ((runp = runp->next) != NULL);
  158. if (result > GL(dl_tls_max_dtv_idx))
  159. {
  160. /* The new index must indeed be exactly one higher than the
  161. previous high. */
  162. assert (result == GL(dl_tls_max_dtv_idx) + 1);
  163. /* There is no gap anymore. */
  164. GL(dl_tls_dtv_gaps) = false;
  165. goto nogaps;
  166. }
  167. }
  168. else
  169. {
  170. /* No gaps, allocate a new entry. */
  171. nogaps:
  172. result = ++GL(dl_tls_max_dtv_idx);
  173. }
  174. return result;
  175. }
  176. # ifdef SHARED
  177. void
  178. internal_function
  179. _dl_determine_tlsoffset (void)
  180. {
  181. size_t max_align = TLS_TCB_ALIGN;
  182. size_t freetop = 0;
  183. size_t freebottom = 0;
  184. /* The first element of the dtv slot info list is allocated. */
  185. assert (GL(dl_tls_dtv_slotinfo_list) != NULL);
  186. /* There is at this point only one element in the
  187. dl_tls_dtv_slotinfo_list list. */
  188. assert (GL(dl_tls_dtv_slotinfo_list)->next == NULL);
  189. struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
  190. /* Determining the offset of the various parts of the static TLS
  191. block has several dependencies. In addition we have to work
  192. around bugs in some toolchains.
  193. Each TLS block from the objects available at link time has a size
  194. and an alignment requirement. The GNU ld computes the alignment
  195. requirements for the data at the positions *in the file*, though.
  196. I.e, it is not simply possible to allocate a block with the size
  197. of the TLS program header entry. The data is layed out assuming
  198. that the first byte of the TLS block fulfills
  199. p_vaddr mod p_align == &TLS_BLOCK mod p_align
  200. This means we have to add artificial padding at the beginning of
  201. the TLS block. These bytes are never used for the TLS data in
  202. this module but the first byte allocated must be aligned
  203. according to mod p_align == 0 so that the first byte of the TLS
  204. block is aligned according to p_vaddr mod p_align. This is ugly
  205. and the linker can help by computing the offsets in the TLS block
  206. assuming the first byte of the TLS block is aligned according to
  207. p_align.
  208. The extra space which might be allocated before the first byte of
  209. the TLS block need not go unused. The code below tries to use
  210. that memory for the next TLS block. This can work if the total
  211. memory requirement for the next TLS block is smaller than the
  212. gap. */
  213. # if defined(TLS_TCB_AT_TP)
  214. /* We simply start with zero. */
  215. size_t offset = 0;
  216. size_t cnt;
  217. for (cnt = 0; slotinfo[cnt].map != NULL; ++cnt)
  218. {
  219. assert (cnt < GL(dl_tls_dtv_slotinfo_list)->len);
  220. size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
  221. & (slotinfo[cnt].map->l_tls_align - 1));
  222. size_t off;
  223. max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
  224. if (freebottom - freetop >= slotinfo[cnt].map->l_tls_blocksize)
  225. {
  226. off = roundup (freetop + slotinfo[cnt].map->l_tls_blocksize
  227. - firstbyte, slotinfo[cnt].map->l_tls_align)
  228. + firstbyte;
  229. if (off <= freebottom)
  230. {
  231. freetop = off;
  232. /* XXX For some architectures we perhaps should store the
  233. negative offset. */
  234. slotinfo[cnt].map->l_tls_offset = off;
  235. continue;
  236. }
  237. }
  238. off = roundup (offset + slotinfo[cnt].map->l_tls_blocksize - firstbyte,
  239. slotinfo[cnt].map->l_tls_align) + firstbyte;
  240. if (off > offset + slotinfo[cnt].map->l_tls_blocksize
  241. + (freebottom - freetop))
  242. {
  243. freetop = offset;
  244. freebottom = off - slotinfo[cnt].map->l_tls_blocksize;
  245. }
  246. offset = off;
  247. /* XXX For some architectures we perhaps should store the
  248. negative offset. */
  249. slotinfo[cnt].map->l_tls_offset = off;
  250. }
  251. GL(dl_tls_static_used) = offset;
  252. GL(dl_tls_static_size) = (roundup (offset + TLS_STATIC_SURPLUS, max_align)
  253. + TLS_TCB_SIZE);
  254. # elif defined(TLS_DTV_AT_TP)
  255. /* The TLS blocks start right after the TCB. */
  256. size_t offset = TLS_TCB_SIZE;
  257. size_t cnt;
  258. for (cnt = 0; slotinfo[cnt].map != NULL; ++cnt)
  259. {
  260. assert (cnt < GL(dl_tls_dtv_slotinfo_list)->len);
  261. size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
  262. & (slotinfo[cnt].map->l_tls_align - 1));
  263. size_t off;
  264. max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
  265. if (slotinfo[cnt].map->l_tls_blocksize <= freetop - freebottom)
  266. {
  267. off = roundup (freebottom, slotinfo[cnt].map->l_tls_align);
  268. if (off - freebottom < firstbyte)
  269. off += slotinfo[cnt].map->l_tls_align;
  270. if (off + slotinfo[cnt].map->l_tls_blocksize - firstbyte <= freetop)
  271. {
  272. slotinfo[cnt].map->l_tls_offset = off - firstbyte;
  273. freebottom = (off + slotinfo[cnt].map->l_tls_blocksize
  274. - firstbyte);
  275. continue;
  276. }
  277. }
  278. off = roundup (offset, slotinfo[cnt].map->l_tls_align);
  279. if (off - offset < firstbyte)
  280. off += slotinfo[cnt].map->l_tls_align;
  281. slotinfo[cnt].map->l_tls_offset = off - firstbyte;
  282. if (off - firstbyte - offset > freetop - freebottom)
  283. {
  284. freebottom = offset;
  285. freetop = off - firstbyte;
  286. }
  287. offset = off + slotinfo[cnt].map->l_tls_blocksize - firstbyte;
  288. }
  289. GL(dl_tls_static_used) = offset;
  290. GL(dl_tls_static_size) = roundup (offset + TLS_STATIC_SURPLUS,
  291. TLS_TCB_ALIGN);
  292. # else
  293. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  294. # endif
  295. /* The alignment requirement for the static TLS block. */
  296. GL(dl_tls_static_align) = max_align;
  297. }
  298. /* This is called only when the data structure setup was skipped at startup,
  299. when there was no need for it then. Now we have dynamically loaded
  300. something needing TLS, or libpthread needs it. */
  301. int
  302. internal_function
  303. _dl_tls_setup (void)
  304. {
  305. assert (GL(dl_tls_dtv_slotinfo_list) == NULL);
  306. assert (GL(dl_tls_max_dtv_idx) == 0);
  307. const size_t nelem = 2 + TLS_SLOTINFO_SURPLUS;
  308. GL(dl_tls_dtv_slotinfo_list)
  309. = calloc (1, (sizeof (struct dtv_slotinfo_list)
  310. + nelem * sizeof (struct dtv_slotinfo)));
  311. if (GL(dl_tls_dtv_slotinfo_list) == NULL)
  312. return -1;
  313. GL(dl_tls_dtv_slotinfo_list)->len = nelem;
  314. /* Number of elements in the static TLS block. It can't be zero
  315. because of various assumptions. The one element is null. */
  316. GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx) = 1;
  317. /* This initializes more variables for us. */
  318. _dl_determine_tlsoffset ();
  319. return 0;
  320. }
  321. # endif
  322. static void *
  323. internal_function
  324. allocate_dtv (void *result)
  325. {
  326. dtv_t *dtv;
  327. size_t dtv_length;
  328. /* We allocate a few more elements in the dtv than are needed for the
  329. initial set of modules. This should avoid in most cases expansions
  330. of the dtv. */
  331. dtv_length = GL(dl_tls_max_dtv_idx) + DTV_SURPLUS;
  332. dtv = calloc (dtv_length + 2, sizeof (dtv_t));
  333. if (dtv != NULL)
  334. {
  335. /* This is the initial length of the dtv. */
  336. dtv[0].counter = dtv_length;
  337. /* The rest of the dtv (including the generation counter) is
  338. Initialize with zero to indicate nothing there. */
  339. /* Add the dtv to the thread data structures. */
  340. INSTALL_DTV (result, dtv);
  341. }
  342. else
  343. result = NULL;
  344. return result;
  345. }
  346. /* Get size and alignment requirements of the static TLS block. */
  347. void
  348. internal_function
  349. _dl_get_tls_static_info (size_t *sizep, size_t *alignp)
  350. {
  351. *sizep = GL(dl_tls_static_size);
  352. *alignp = GL(dl_tls_static_align);
  353. }
  354. void *
  355. internal_function
  356. _dl_allocate_tls_storage (void)
  357. {
  358. void *result;
  359. size_t size = GL(dl_tls_static_size);
  360. # if defined(TLS_DTV_AT_TP)
  361. /* Memory layout is:
  362. [ TLS_PRE_TCB_SIZE ] [ TLS_TCB_SIZE ] [ TLS blocks ]
  363. ^ This should be returned. */
  364. size += (TLS_PRE_TCB_SIZE + GL(dl_tls_static_align) - 1)
  365. & ~(GL(dl_tls_static_align) - 1);
  366. # endif
  367. /* Allocate a correctly aligned chunk of memory. */
  368. result = _dl_memalign (GL(dl_tls_static_align), size);
  369. if (__builtin_expect (result != NULL, 1))
  370. {
  371. /* Allocate the DTV. */
  372. void *allocated = result;
  373. # if defined(TLS_TCB_AT_TP)
  374. /* The TCB follows the TLS blocks. */
  375. result = (char *) result + size - TLS_TCB_SIZE;
  376. /* Clear the TCB data structure. We can't ask the caller (i.e.
  377. libpthread) to do it, because we will initialize the DTV et al. */
  378. _dl_memset (result, '\0', TLS_TCB_SIZE);
  379. # elif defined(TLS_DTV_AT_TP)
  380. result = (char *) result + size - GL(dl_tls_static_size);
  381. /* Clear the TCB data structure and TLS_PRE_TCB_SIZE bytes before it.
  382. We can't ask the caller (i.e. libpthread) to do it, because we will
  383. initialize the DTV et al. */
  384. _dl_memset ((char *) result - TLS_PRE_TCB_SIZE, '\0',
  385. TLS_PRE_TCB_SIZE + TLS_TCB_SIZE);
  386. # endif
  387. result = allocate_dtv (result);
  388. if (result == NULL)
  389. free (allocated);
  390. }
  391. return result;
  392. }
  393. void *
  394. internal_function
  395. _dl_allocate_tls_init (void *result)
  396. {
  397. if (result == NULL)
  398. /* The memory allocation failed. */
  399. return NULL;
  400. dtv_t *dtv = GET_DTV (result);
  401. struct dtv_slotinfo_list *listp;
  402. size_t total = 0;
  403. size_t maxgen = 0;
  404. /* We have to prepare the dtv for all currently loaded modules using
  405. TLS. For those which are dynamically loaded we add the values
  406. indicating deferred allocation. */
  407. listp = GL(dl_tls_dtv_slotinfo_list);
  408. while (1)
  409. {
  410. size_t cnt;
  411. for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
  412. {
  413. struct link_map *map;
  414. void *dest;
  415. /* Check for the total number of used slots. */
  416. if (total + cnt > GL(dl_tls_max_dtv_idx))
  417. break;
  418. map = listp->slotinfo[cnt].map;
  419. if (map == NULL)
  420. /* Unused entry. */
  421. continue;
  422. /* Keep track of the maximum generation number. This might
  423. not be the generation counter. */
  424. maxgen = MAX (maxgen, listp->slotinfo[cnt].gen);
  425. if (map->l_tls_offset == NO_TLS_OFFSET)
  426. {
  427. /* For dynamically loaded modules we simply store
  428. the value indicating deferred allocation. */
  429. dtv[map->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
  430. dtv[map->l_tls_modid].pointer.is_static = false;
  431. continue;
  432. }
  433. assert (map->l_tls_modid == cnt);
  434. assert (map->l_tls_blocksize >= map->l_tls_initimage_size);
  435. # if defined(TLS_TCB_AT_TP)
  436. assert ((size_t) map->l_tls_offset >= map->l_tls_blocksize);
  437. dest = (char *) result - map->l_tls_offset;
  438. # elif defined(TLS_DTV_AT_TP)
  439. dest = (char *) result + map->l_tls_offset;
  440. # else
  441. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  442. # endif
  443. /* Copy the initialization image and clear the BSS part. */
  444. dtv[map->l_tls_modid].pointer.val = dest;
  445. dtv[map->l_tls_modid].pointer.is_static = true;
  446. _dl_memset (_dl_mempcpy (dest, map->l_tls_initimage,
  447. map->l_tls_initimage_size), '\0',
  448. map->l_tls_blocksize - map->l_tls_initimage_size);
  449. }
  450. total += cnt;
  451. if (total >= GL(dl_tls_max_dtv_idx))
  452. break;
  453. listp = listp->next;
  454. assert (listp != NULL);
  455. }
  456. /* The DTV version is up-to-date now. */
  457. dtv[0].counter = maxgen;
  458. return result;
  459. }
  460. void *
  461. internal_function
  462. _dl_allocate_tls (void *mem)
  463. {
  464. return _dl_allocate_tls_init (mem == NULL
  465. ? _dl_allocate_tls_storage ()
  466. : allocate_dtv (mem));
  467. }
  468. void
  469. internal_function
  470. _dl_deallocate_tls (void *tcb, bool dealloc_tcb)
  471. {
  472. dtv_t *dtv = GET_DTV (tcb);
  473. size_t cnt;
  474. /* We need to free the memory allocated for non-static TLS. */
  475. for (cnt = 0; cnt < dtv[-1].counter; ++cnt)
  476. if (! dtv[1 + cnt].pointer.is_static
  477. && dtv[1 + cnt].pointer.val != TLS_DTV_UNALLOCATED)
  478. free (dtv[1 + cnt].pointer.val);
  479. /* The array starts with dtv[-1]. */
  480. #ifdef SHARED
  481. if (dtv != GL(dl_initial_dtv))
  482. #endif
  483. free (dtv - 1);
  484. if (dealloc_tcb)
  485. {
  486. # if defined(TLS_TCB_AT_TP)
  487. /* The TCB follows the TLS blocks. Back up to free the whole block. */
  488. tcb -= GL(dl_tls_static_size) - TLS_TCB_SIZE;
  489. # elif defined(TLS_DTV_AT_TP)
  490. /* Back up the TLS_PRE_TCB_SIZE bytes. */
  491. tcb -= (TLS_PRE_TCB_SIZE + GL(dl_tls_static_align) - 1)
  492. & ~(GL(dl_tls_static_align) - 1);
  493. # endif
  494. free (tcb);
  495. }
  496. }
  497. # ifdef SHARED
  498. /* The __tls_get_addr function has two basic forms which differ in the
  499. arguments. The IA-64 form takes two parameters, the module ID and
  500. offset. The form used, among others, on IA-32 takes a reference to
  501. a special structure which contain the same information. The second
  502. form seems to be more often used (in the moment) so we default to
  503. it. Users of the IA-64 form have to provide adequate definitions
  504. of the following macros. */
  505. # ifndef GET_ADDR_ARGS
  506. # define GET_ADDR_ARGS tls_index *ti
  507. # endif
  508. # ifndef GET_ADDR_MODULE
  509. # define GET_ADDR_MODULE ti->ti_module
  510. # endif
  511. # ifndef GET_ADDR_OFFSET
  512. # define GET_ADDR_OFFSET ti->ti_offset
  513. # endif
  514. static void *
  515. allocate_and_init (struct link_map *map)
  516. {
  517. void *newp;
  518. newp = _dl_memalign (map->l_tls_align, map->l_tls_blocksize);
  519. if (newp == NULL)
  520. oom ();
  521. /* Initialize the memory. */
  522. _dl_memset (_dl_mempcpy (newp, map->l_tls_initimage, map->l_tls_initimage_size),
  523. '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
  524. return newp;
  525. }
  526. struct link_map *
  527. _dl_update_slotinfo (unsigned long int req_modid)
  528. {
  529. struct link_map *the_map = NULL;
  530. dtv_t *dtv = THREAD_DTV ();
  531. /* The global dl_tls_dtv_slotinfo array contains for each module
  532. index the generation counter current when the entry was created.
  533. This array never shrinks so that all module indices which were
  534. valid at some time can be used to access it. Before the first
  535. use of a new module index in this function the array was extended
  536. appropriately. Access also does not have to be guarded against
  537. modifications of the array. It is assumed that pointer-size
  538. values can be read atomically even in SMP environments. It is
  539. possible that other threads at the same time dynamically load
  540. code and therefore add to the slotinfo list. This is a problem
  541. since we must not pick up any information about incomplete work.
  542. The solution to this is to ignore all dtv slots which were
  543. created after the one we are currently interested. We know that
  544. dynamic loading for this module is completed and this is the last
  545. load operation we know finished. */
  546. unsigned long int idx = req_modid;
  547. struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
  548. while (idx >= listp->len)
  549. {
  550. idx -= listp->len;
  551. listp = listp->next;
  552. }
  553. if (dtv[0].counter < listp->slotinfo[idx].gen)
  554. {
  555. /* The generation counter for the slot is higher than what the
  556. current dtv implements. We have to update the whole dtv but
  557. only those entries with a generation counter <= the one for
  558. the entry we need. */
  559. size_t new_gen = listp->slotinfo[idx].gen;
  560. size_t total = 0;
  561. /* We have to look through the entire dtv slotinfo list. */
  562. listp = GL(dl_tls_dtv_slotinfo_list);
  563. do
  564. {
  565. size_t cnt;
  566. for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
  567. {
  568. size_t gen = listp->slotinfo[cnt].gen;
  569. if (gen > new_gen)
  570. /* This is a slot for a generation younger than the
  571. one we are handling now. It might be incompletely
  572. set up so ignore it. */
  573. continue;
  574. /* If the entry is older than the current dtv layout we
  575. know we don't have to handle it. */
  576. if (gen <= dtv[0].counter)
  577. continue;
  578. /* If there is no map this means the entry is empty. */
  579. struct link_map *map = listp->slotinfo[cnt].map;
  580. if (map == NULL)
  581. {
  582. /* If this modid was used at some point the memory
  583. might still be allocated. */
  584. if (! dtv[total + cnt].pointer.is_static
  585. && dtv[total + cnt].pointer.val != TLS_DTV_UNALLOCATED)
  586. {
  587. free (dtv[total + cnt].pointer.val);
  588. dtv[total + cnt].pointer.val = TLS_DTV_UNALLOCATED;
  589. }
  590. continue;
  591. }
  592. /* Check whether the current dtv array is large enough. */
  593. size_t modid = map->l_tls_modid;
  594. assert (total + cnt == modid);
  595. if (dtv[-1].counter < modid)
  596. {
  597. /* Reallocate the dtv. */
  598. dtv_t *newp;
  599. size_t newsize = GL(dl_tls_max_dtv_idx) + DTV_SURPLUS;
  600. size_t oldsize = dtv[-1].counter;
  601. assert (map->l_tls_modid <= newsize);
  602. if (dtv == GL(dl_initial_dtv))
  603. {
  604. /* This is the initial dtv that was allocated
  605. during rtld startup using the dl-minimal.c
  606. malloc instead of the real malloc. We can't
  607. free it, we have to abandon the old storage. */
  608. newp = malloc ((2 + newsize) * sizeof (dtv_t));
  609. if (newp == NULL)
  610. oom ();
  611. _dl_memcpy (newp, &dtv[-1], oldsize * sizeof (dtv_t));
  612. }
  613. else
  614. {
  615. newp = realloc (&dtv[-1],
  616. (2 + newsize) * sizeof (dtv_t));
  617. if (newp == NULL)
  618. oom ();
  619. }
  620. newp[0].counter = newsize;
  621. /* Clear the newly allocated part. */
  622. _dl_memset (newp + 2 + oldsize, '\0',
  623. (newsize - oldsize) * sizeof (dtv_t));
  624. /* Point dtv to the generation counter. */
  625. dtv = &newp[1];
  626. /* Install this new dtv in the thread data
  627. structures. */
  628. INSTALL_NEW_DTV (dtv);
  629. }
  630. /* If there is currently memory allocate for this
  631. dtv entry free it. */
  632. /* XXX Ideally we will at some point create a memory
  633. pool. */
  634. if (! dtv[modid].pointer.is_static
  635. && dtv[modid].pointer.val != TLS_DTV_UNALLOCATED)
  636. /* Note that free is called for NULL is well. We
  637. deallocate even if it is this dtv entry we are
  638. supposed to load. The reason is that we call
  639. memalign and not malloc. */
  640. free (dtv[modid].pointer.val);
  641. /* This module is loaded dynamically- We defer memory
  642. allocation. */
  643. dtv[modid].pointer.is_static = false;
  644. dtv[modid].pointer.val = TLS_DTV_UNALLOCATED;
  645. if (modid == req_modid)
  646. the_map = map;
  647. }
  648. total += listp->len;
  649. }
  650. while ((listp = listp->next) != NULL);
  651. /* This will be the new maximum generation counter. */
  652. dtv[0].counter = new_gen;
  653. }
  654. return the_map;
  655. }
  656. /* The generic dynamic and local dynamic model cannot be used in
  657. statically linked applications. */
  658. void *
  659. __tls_get_addr (GET_ADDR_ARGS)
  660. {
  661. dtv_t *dtv = THREAD_DTV ();
  662. struct link_map *the_map = NULL;
  663. void *p;
  664. if (__builtin_expect (dtv[0].counter != GL(dl_tls_generation), 0))
  665. the_map = _dl_update_slotinfo (GET_ADDR_MODULE);
  666. p = dtv[GET_ADDR_MODULE].pointer.val;
  667. if (__builtin_expect (p == TLS_DTV_UNALLOCATED, 0))
  668. {
  669. /* The allocation was deferred. Do it now. */
  670. if (the_map == NULL)
  671. {
  672. /* Find the link map for this module. */
  673. size_t idx = GET_ADDR_MODULE;
  674. struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
  675. while (idx >= listp->len)
  676. {
  677. idx -= listp->len;
  678. listp = listp->next;
  679. }
  680. the_map = listp->slotinfo[idx].map;
  681. }
  682. p = dtv[GET_ADDR_MODULE].pointer.val = allocate_and_init (the_map);
  683. dtv[GET_ADDR_MODULE].pointer.is_static = false;
  684. }
  685. return (char *) p + GET_ADDR_OFFSET;
  686. }
  687. # endif
  688. void _dl_add_to_slotinfo (struct link_map *l);
  689. void
  690. _dl_add_to_slotinfo (struct link_map *l)
  691. {
  692. /* Now that we know the object is loaded successfully add
  693. modules containing TLS data to the dtv info table. We
  694. might have to increase its size. */
  695. struct dtv_slotinfo_list *listp;
  696. struct dtv_slotinfo_list *prevp;
  697. size_t idx = l->l_tls_modid;
  698. /* Find the place in the dtv slotinfo list. */
  699. listp = GL(dl_tls_dtv_slotinfo_list);
  700. prevp = NULL; /* Needed to shut up gcc. */
  701. do
  702. {
  703. /* Does it fit in the array of this list element? */
  704. if (idx < listp->len)
  705. break;
  706. idx -= listp->len;
  707. prevp = listp;
  708. listp = listp->next;
  709. }
  710. while (listp != NULL);
  711. if (listp == NULL)
  712. {
  713. /* When we come here it means we have to add a new element
  714. to the slotinfo list. And the new module must be in
  715. the first slot. */
  716. assert (idx == 0);
  717. listp = prevp->next = (struct dtv_slotinfo_list *)
  718. malloc (sizeof (struct dtv_slotinfo_list)
  719. + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
  720. if (listp == NULL)
  721. {
  722. /* We ran out of memory. We will simply fail this
  723. call but don't undo anything we did so far. The
  724. application will crash or be terminated anyway very
  725. soon. */
  726. /* We have to do this since some entries in the dtv
  727. slotinfo array might already point to this
  728. generation. */
  729. ++GL(dl_tls_generation);
  730. _dl_dprintf (_dl_debug_file,
  731. "cannot create TLS data structures: ABORT\n");
  732. _dl_exit (127);
  733. }
  734. listp->len = TLS_SLOTINFO_SURPLUS;
  735. listp->next = NULL;
  736. _dl_memset (listp->slotinfo, '\0',
  737. TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
  738. }
  739. /* Add the information into the slotinfo data structure. */
  740. listp->slotinfo[idx].map = l;
  741. listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
  742. }