dl-tls.c 25 KB

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