dl-tls.c 25 KB

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