dl-tls.c 25 KB

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