dl-tls.c 25 KB

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