dl-tls.c 25 KB

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