dl-tls.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Thread-local storage handling in the ELF dynamic linker.
  4. *
  5. * Copyright (C) 2005 by Steven J. Hill <sjhill@realitydiluted.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. The name of the above contributors may not be
  13. * used to endorse or promote products derived from this software
  14. * without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <tls.h>
  29. #include <dl-tls.h>
  30. #include <ldsodefs.h>
  31. void *(*_dl_calloc_function) (size_t __nmemb, size_t __size) = NULL;
  32. void *(*_dl_realloc_function) (void *__ptr, size_t __size) = NULL;
  33. void *(*_dl_memalign_function) (size_t __boundary, size_t __size) = NULL;
  34. void (*_dl_free_function) (void *__ptr);
  35. void *_dl_memalign (size_t __boundary, size_t __size);
  36. struct link_map *_dl_update_slotinfo (unsigned long int req_modid);
  37. /* Round up N to the nearest multiple of P, where P is a power of 2
  38. --- without using libgcc division routines. */
  39. #define roundup_pow2(n, p) (((n) + (p) - 1) & ~((p) - 1))
  40. void *
  41. _dl_calloc (size_t __nmemb, size_t __size)
  42. {
  43. void *result;
  44. size_t size = (__size * __nmemb);
  45. if (_dl_calloc_function)
  46. return (*_dl_calloc_function) (__nmemb, __size);
  47. if ((result = _dl_malloc(size)) != NULL) {
  48. _dl_memset(result, 0, size);
  49. }
  50. return result;
  51. }
  52. void *
  53. _dl_realloc (void * __ptr, size_t __size)
  54. {
  55. if (_dl_realloc_function)
  56. return (*_dl_realloc_function) (__ptr, __size);
  57. _dl_debug_early("NOT IMPLEMENTED PROPERLY!!!\n");
  58. return NULL;
  59. }
  60. /* The __tls_get_addr function has two basic forms which differ in the
  61. arguments. The IA-64 form takes two parameters, the module ID and
  62. offset. The form used, among others, on IA-32 takes a reference to
  63. a special structure which contain the same information. The second
  64. form seems to be more often used (in the moment) so we default to
  65. it. Users of the IA-64 form have to provide adequate definitions
  66. of the following macros. */
  67. #ifndef GET_ADDR_ARGS
  68. # define GET_ADDR_ARGS tls_index *ti
  69. #endif
  70. #ifndef GET_ADDR_MODULE
  71. # define GET_ADDR_MODULE ti->ti_module
  72. #endif
  73. #ifndef GET_ADDR_OFFSET
  74. # define GET_ADDR_OFFSET ti->ti_offset
  75. #endif
  76. /*
  77. * Amount of excess space to allocate in the static TLS area
  78. * to allow dynamic loading of modules defining IE-model TLS data.
  79. */
  80. #define TLS_STATIC_SURPLUS 64 + DL_NNS * 100
  81. /* Value used for dtv entries for which the allocation is delayed. */
  82. #define TLS_DTV_UNALLOCATED ((void *) -1l)
  83. /*
  84. * We are trying to perform a static TLS relocation in MAP, but it was
  85. * dynamically loaded. This can only work if there is enough surplus in
  86. * the static TLS area already allocated for each running thread. If this
  87. * object's TLS segment is too big to fit, we fail. If it fits,
  88. * we set MAP->l_tls_offset and return.
  89. */
  90. int
  91. internal_function
  92. _dl_try_allocate_static_tls (struct link_map* map)
  93. {
  94. /* If the alignment requirements are too high fail. */
  95. if (map->l_tls_align > _dl_tls_static_align)
  96. {
  97. fail:
  98. return -1;
  99. }
  100. # ifdef TLS_TCB_AT_TP
  101. size_t freebytes;
  102. size_t n;
  103. size_t blsize;
  104. freebytes = _dl_tls_static_size - _dl_tls_static_used - TLS_TCB_SIZE;
  105. blsize = map->l_tls_blocksize + map->l_tls_firstbyte_offset;
  106. if (freebytes < blsize)
  107. goto fail;
  108. n = (freebytes - blsize) & ~(map->l_tls_align - 1);
  109. size_t offset = _dl_tls_static_used + (freebytes - n
  110. - map->l_tls_firstbyte_offset);
  111. map->l_tls_offset = _dl_tls_static_used = offset;
  112. # elif defined(TLS_DTV_AT_TP)
  113. size_t used;
  114. size_t check;
  115. size_t offset = roundup_pow2 (_dl_tls_static_used, map->l_tls_align);
  116. used = offset + map->l_tls_blocksize;
  117. check = used;
  118. /* dl_tls_static_used includes the TCB at the beginning. */
  119. if (check > _dl_tls_static_size)
  120. goto fail;
  121. map->l_tls_offset = offset;
  122. _dl_tls_static_used = used;
  123. # else
  124. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  125. # endif
  126. /*
  127. * If the object is not yet relocated we cannot initialize the
  128. * static TLS region. Delay it.
  129. */
  130. if (((struct elf_resolve *) map)->init_flag & RELOCS_DONE)
  131. {
  132. #ifdef SHARED
  133. /*
  134. * Update the slot information data for at least the generation of
  135. * the DSO we are allocating data for.
  136. */
  137. if (__builtin_expect (THREAD_DTV()[0].counter != _dl_tls_generation, 0))
  138. (void) _dl_update_slotinfo (map->l_tls_modid);
  139. #endif
  140. _dl_init_static_tls (map);
  141. }
  142. else
  143. map->l_need_tls_init = 1;
  144. return 0;
  145. }
  146. /*
  147. * This function intentionally does not return any value but signals error
  148. * directly, as static TLS should be rare and code handling it should
  149. * not be inlined as much as possible.
  150. */
  151. void
  152. internal_function __attribute_noinline__
  153. _dl_allocate_static_tls (struct link_map *map)
  154. {
  155. if (_dl_try_allocate_static_tls (map)) {
  156. _dl_dprintf(2, "cannot allocate memory in static TLS block");
  157. _dl_exit(30);
  158. }
  159. }
  160. #ifdef SHARED
  161. /* Initialize static TLS area and DTV for current (only) thread.
  162. libpthread implementations should provide their own hook
  163. to handle all threads. */
  164. void
  165. attribute_hidden __attribute_noinline__
  166. _dl_nothread_init_static_tls (struct link_map *map)
  167. {
  168. # ifdef TLS_TCB_AT_TP
  169. void *dest = (char *) THREAD_SELF - map->l_tls_offset;
  170. # elif defined(TLS_DTV_AT_TP)
  171. void *dest = (char *) THREAD_SELF + map->l_tls_offset + TLS_PRE_TCB_SIZE;
  172. # else
  173. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  174. # endif
  175. /* Fill in the DTV slot so that a later LD/GD access will find it. */
  176. dtv_t *dtv = THREAD_DTV ();
  177. if (!(map->l_tls_modid <= dtv[-1].counter)) {
  178. _dl_dprintf(2, "map->l_tls_modid <= dtv[-1].counter FAILED!\n");
  179. _dl_exit(30);
  180. }
  181. dtv[map->l_tls_modid].pointer.val = dest;
  182. dtv[map->l_tls_modid].pointer.is_static = true;
  183. /* Initialize the memory. */
  184. _dl_memcpy(dest, map->l_tls_initimage, map->l_tls_initimage_size);
  185. _dl_memset((dest + map->l_tls_initimage_size), '\0',
  186. map->l_tls_blocksize - map->l_tls_initimage_size);
  187. }
  188. #endif
  189. /* Taken from glibc/sysdeps/generic/dl-tls.c */
  190. static void
  191. oom (void)
  192. {
  193. _dl_debug_early("cannot allocate thread-local memory: ABORT\n");
  194. _dl_exit(30);
  195. }
  196. size_t
  197. internal_function
  198. _dl_next_tls_modid (void)
  199. {
  200. size_t result;
  201. if (__builtin_expect (_dl_tls_dtv_gaps, false))
  202. {
  203. size_t disp = 0;
  204. struct dtv_slotinfo_list *runp = _dl_tls_dtv_slotinfo_list;
  205. /* Note that this branch will never be executed during program
  206. start since there are no gaps at that time. Therefore it
  207. does not matter that the dl_tls_dtv_slotinfo is not allocated
  208. yet when the function is called for the first times.
  209. NB: the offset +1 is due to the fact that DTV[0] is used
  210. for something else. */
  211. result = _dl_tls_static_nelem + 1;
  212. if (result <= _dl_tls_max_dtv_idx)
  213. do
  214. {
  215. while (result - disp < runp->len)
  216. {
  217. if (runp->slotinfo[result - disp].map == NULL)
  218. break;
  219. ++result;
  220. _dl_assert (result <= _dl_tls_max_dtv_idx + 1);
  221. }
  222. if (result - disp < runp->len)
  223. break;
  224. disp += runp->len;
  225. }
  226. while ((runp = runp->next) != NULL);
  227. if (result > _dl_tls_max_dtv_idx)
  228. {
  229. /* The new index must indeed be exactly one higher than the
  230. previous high. */
  231. _dl_assert (result == _dl_tls_max_dtv_idx + 1);
  232. /* There is no gap anymore. */
  233. _dl_tls_dtv_gaps = false;
  234. goto nogaps;
  235. }
  236. }
  237. else
  238. {
  239. /* No gaps, allocate a new entry. */
  240. nogaps:
  241. result = ++_dl_tls_max_dtv_idx;
  242. }
  243. return result;
  244. }
  245. void
  246. internal_function
  247. _dl_determine_tlsoffset (void)
  248. {
  249. size_t max_align = TLS_TCB_ALIGN;
  250. size_t freetop = 0;
  251. size_t freebottom = 0;
  252. /* The first element of the dtv slot info list is allocated. */
  253. _dl_assert (_dl_tls_dtv_slotinfo_list != NULL);
  254. /* There is at this point only one element in the
  255. dl_tls_dtv_slotinfo_list list. */
  256. _dl_assert (_dl_tls_dtv_slotinfo_list->next == NULL);
  257. struct dtv_slotinfo *slotinfo = _dl_tls_dtv_slotinfo_list->slotinfo;
  258. /* Determining the offset of the various parts of the static TLS
  259. block has several dependencies. In addition we have to work
  260. around bugs in some toolchains.
  261. Each TLS block from the objects available at link time has a size
  262. and an alignment requirement. The GNU ld computes the alignment
  263. requirements for the data at the positions *in the file*, though.
  264. I.e, it is not simply possible to allocate a block with the size
  265. of the TLS program header entry. The data is layed out assuming
  266. that the first byte of the TLS block fulfills
  267. p_vaddr mod p_align == &TLS_BLOCK mod p_align
  268. This means we have to add artificial padding at the beginning of
  269. the TLS block. These bytes are never used for the TLS data in
  270. this module but the first byte allocated must be aligned
  271. according to mod p_align == 0 so that the first byte of the TLS
  272. block is aligned according to p_vaddr mod p_align. This is ugly
  273. and the linker can help by computing the offsets in the TLS block
  274. assuming the first byte of the TLS block is aligned according to
  275. p_align.
  276. The extra space which might be allocated before the first byte of
  277. the TLS block need not go unused. The code below tries to use
  278. that memory for the next TLS block. This can work if the total
  279. memory requirement for the next TLS block is smaller than the
  280. gap. */
  281. # ifdef TLS_TCB_AT_TP
  282. /* We simply start with zero. */
  283. size_t cnt, offset = 0;
  284. for (cnt = 1; slotinfo[cnt].map != NULL; ++cnt)
  285. {
  286. _dl_assert (cnt < _dl_tls_dtv_slotinfo_list->len);
  287. size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
  288. & (slotinfo[cnt].map->l_tls_align - 1));
  289. size_t off;
  290. max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
  291. if (freebottom - freetop >= slotinfo[cnt].map->l_tls_blocksize)
  292. {
  293. off = roundup_pow2 (freetop + slotinfo[cnt].map->l_tls_blocksize
  294. - firstbyte, slotinfo[cnt].map->l_tls_align)
  295. + firstbyte;
  296. if (off <= freebottom)
  297. {
  298. freetop = off;
  299. /* XXX For some architectures we perhaps should store the
  300. negative offset. */
  301. slotinfo[cnt].map->l_tls_offset = off;
  302. continue;
  303. }
  304. }
  305. off = roundup_pow2 (offset + slotinfo[cnt].map->l_tls_blocksize
  306. - firstbyte, slotinfo[cnt].map->l_tls_align)
  307. + firstbyte;
  308. if (off > offset + slotinfo[cnt].map->l_tls_blocksize
  309. + (freebottom - freetop))
  310. {
  311. freetop = offset;
  312. freebottom = off - slotinfo[cnt].map->l_tls_blocksize;
  313. }
  314. offset = off;
  315. /* XXX For some architectures we perhaps should store the
  316. negative offset. */
  317. slotinfo[cnt].map->l_tls_offset = off;
  318. }
  319. _dl_tls_static_used = offset;
  320. _dl_tls_static_size = (roundup_pow2 (offset + TLS_STATIC_SURPLUS, max_align)
  321. + TLS_TCB_SIZE);
  322. # elif defined(TLS_DTV_AT_TP)
  323. /* The TLS blocks start right after the TCB. */
  324. size_t offset = TLS_TCB_SIZE;
  325. size_t cnt;
  326. for (cnt = 1; slotinfo[cnt].map != NULL; ++cnt)
  327. {
  328. _dl_assert (cnt < _dl_tls_dtv_slotinfo_list->len);
  329. size_t firstbyte = (-slotinfo[cnt].map->l_tls_firstbyte_offset
  330. & (slotinfo[cnt].map->l_tls_align - 1));
  331. size_t off;
  332. max_align = MAX (max_align, slotinfo[cnt].map->l_tls_align);
  333. if (slotinfo[cnt].map->l_tls_blocksize <= freetop - freebottom)
  334. {
  335. off = roundup_pow2 (freebottom, slotinfo[cnt].map->l_tls_align);
  336. if (off - freebottom < firstbyte)
  337. off += slotinfo[cnt].map->l_tls_align;
  338. if (off + slotinfo[cnt].map->l_tls_blocksize - firstbyte <= freetop)
  339. {
  340. slotinfo[cnt].map->l_tls_offset = off - firstbyte;
  341. freebottom = (off + slotinfo[cnt].map->l_tls_blocksize
  342. - firstbyte);
  343. continue;
  344. }
  345. }
  346. off = roundup_pow2 (offset, slotinfo[cnt].map->l_tls_align);
  347. if (off - offset < firstbyte)
  348. off += slotinfo[cnt].map->l_tls_align;
  349. slotinfo[cnt].map->l_tls_offset = off - firstbyte;
  350. if (off - firstbyte - offset > freetop - freebottom)
  351. {
  352. freebottom = offset;
  353. freetop = off - firstbyte;
  354. }
  355. offset = off + slotinfo[cnt].map->l_tls_blocksize - firstbyte;
  356. }
  357. _dl_tls_static_used = offset;
  358. _dl_tls_static_size = roundup_pow2 (offset + TLS_STATIC_SURPLUS,
  359. TLS_TCB_ALIGN);
  360. # else
  361. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  362. # endif
  363. /* The alignment requirement for the static TLS block. */
  364. _dl_tls_static_align = max_align;
  365. }
  366. /* This is called only when the data structure setup was skipped at startup,
  367. when there was no need for it then. Now we have dynamically loaded
  368. something needing TLS, or libpthread needs it. */
  369. rtld_hidden_proto(_dl_tls_setup)
  370. int
  371. internal_function
  372. _dl_tls_setup (void)
  373. {
  374. _dl_assert (_dl_tls_dtv_slotinfo_list == NULL);
  375. _dl_assert (_dl_tls_max_dtv_idx == 0);
  376. const size_t nelem = 2 + TLS_SLOTINFO_SURPLUS;
  377. _dl_tls_dtv_slotinfo_list
  378. = _dl_calloc (1, (sizeof (struct dtv_slotinfo_list)
  379. + nelem * sizeof (struct dtv_slotinfo)));
  380. if (_dl_tls_dtv_slotinfo_list == NULL)
  381. return -1;
  382. _dl_tls_dtv_slotinfo_list->len = nelem;
  383. /* Number of elements in the static TLS block. It can't be zero
  384. because of various assumptions. The one element is null. */
  385. _dl_tls_static_nelem = _dl_tls_max_dtv_idx = 1;
  386. /* This initializes more variables for us. */
  387. _dl_determine_tlsoffset ();
  388. return 0;
  389. }
  390. rtld_hidden_def (_dl_tls_setup)
  391. static void *
  392. internal_function
  393. allocate_dtv (void *result)
  394. {
  395. dtv_t *dtv;
  396. size_t dtv_length;
  397. /* We allocate a few more elements in the dtv than are needed for the
  398. initial set of modules. This should avoid in most cases expansions
  399. of the dtv. */
  400. dtv_length = _dl_tls_max_dtv_idx + DTV_SURPLUS;
  401. dtv = _dl_calloc (dtv_length + 2, sizeof (dtv_t));
  402. if (dtv != NULL)
  403. {
  404. /* This is the initial length of the dtv. */
  405. dtv[0].counter = dtv_length;
  406. /* The rest of the dtv (including the generation counter) is
  407. Initialize with zero to indicate nothing there. */
  408. /* Add the dtv to the thread data structures. */
  409. INSTALL_DTV (result, dtv);
  410. }
  411. else
  412. result = NULL;
  413. return result;
  414. }
  415. /* Get size and alignment requirements of the static TLS block. */
  416. void
  417. internal_function
  418. _dl_get_tls_static_info (size_t *sizep, size_t *alignp)
  419. {
  420. *sizep = _dl_tls_static_size;
  421. *alignp = _dl_tls_static_align;
  422. }
  423. void *
  424. internal_function
  425. _dl_allocate_tls_storage (void)
  426. {
  427. void *result;
  428. size_t size = _dl_tls_static_size;
  429. # if defined(TLS_DTV_AT_TP)
  430. /* Memory layout is:
  431. [ TLS_PRE_TCB_SIZE ] [ TLS_TCB_SIZE ] [ TLS blocks ]
  432. ^ This should be returned. */
  433. size += (TLS_PRE_TCB_SIZE + _dl_tls_static_align - 1)
  434. & ~(_dl_tls_static_align - 1);
  435. # endif
  436. /* Allocate a correctly aligned chunk of memory. */
  437. result = _dl_memalign (_dl_tls_static_align, size);
  438. if (__builtin_expect (result != NULL, 1))
  439. {
  440. /* Allocate the DTV. */
  441. void *allocated = result;
  442. # ifdef TLS_TCB_AT_TP
  443. /* The TCB follows the TLS blocks. */
  444. result = (char *) result + size - TLS_TCB_SIZE;
  445. /* Clear the TCB data structure. We can't ask the caller (i.e.
  446. libpthread) to do it, because we will initialize the DTV et al. */
  447. _dl_memset (result, '\0', TLS_TCB_SIZE);
  448. # elif defined(TLS_DTV_AT_TP)
  449. result = (char *) result + size - _dl_tls_static_size;
  450. /* Clear the TCB data structure and TLS_PRE_TCB_SIZE bytes before it.
  451. We can't ask the caller (i.e. libpthread) to do it, because we will
  452. initialize the DTV et al. */
  453. _dl_memset ((char *) result - TLS_PRE_TCB_SIZE, '\0',
  454. TLS_PRE_TCB_SIZE + TLS_TCB_SIZE);
  455. # endif
  456. result = allocate_dtv (result);
  457. if (result == NULL)
  458. _dl_free (allocated);
  459. }
  460. return result;
  461. }
  462. void *
  463. internal_function
  464. _dl_allocate_tls_init (void *result)
  465. {
  466. if (result == NULL)
  467. /* The memory allocation failed. */
  468. return NULL;
  469. dtv_t *dtv = GET_DTV (result);
  470. struct dtv_slotinfo_list *listp;
  471. size_t total = 0;
  472. size_t maxgen = 0;
  473. /* We have to prepare the dtv for all currently loaded modules using
  474. TLS. For those which are dynamically loaded we add the values
  475. indicating deferred allocation. */
  476. listp = _dl_tls_dtv_slotinfo_list;
  477. while (1)
  478. {
  479. size_t cnt;
  480. for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
  481. {
  482. struct link_map *map;
  483. void *dest;
  484. /* Check for the total number of used slots. */
  485. if (total + cnt > _dl_tls_max_dtv_idx)
  486. break;
  487. map = listp->slotinfo[cnt].map;
  488. if (map == NULL)
  489. /* Unused entry. */
  490. continue;
  491. /* Keep track of the maximum generation number. This might
  492. not be the generation counter. */
  493. maxgen = MAX (maxgen, listp->slotinfo[cnt].gen);
  494. if (map->l_tls_offset == NO_TLS_OFFSET)
  495. {
  496. /* For dynamically loaded modules we simply store
  497. the value indicating deferred allocation. */
  498. dtv[map->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
  499. dtv[map->l_tls_modid].pointer.is_static = false;
  500. continue;
  501. }
  502. _dl_assert (map->l_tls_modid == cnt);
  503. _dl_assert (map->l_tls_blocksize >= map->l_tls_initimage_size);
  504. # ifdef TLS_TCB_AT_TP
  505. _dl_assert ((size_t) map->l_tls_offset >= map->l_tls_blocksize);
  506. dest = (char *) result - map->l_tls_offset;
  507. # elif defined(TLS_DTV_AT_TP)
  508. dest = (char *) result + map->l_tls_offset;
  509. # else
  510. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  511. # endif
  512. /* Copy the initialization image and clear the BSS part. */
  513. dtv[map->l_tls_modid].pointer.val = dest;
  514. dtv[map->l_tls_modid].pointer.is_static = true;
  515. _dl_memcpy(dest, map->l_tls_initimage, map->l_tls_initimage_size);
  516. _dl_memset((dest + map->l_tls_initimage_size), '\0',
  517. map->l_tls_blocksize - map->l_tls_initimage_size);
  518. }
  519. total += cnt;
  520. if (total >= _dl_tls_max_dtv_idx)
  521. break;
  522. listp = listp->next;
  523. _dl_assert (listp != NULL);
  524. }
  525. /* The DTV version is up-to-date now. */
  526. dtv[0].counter = maxgen;
  527. return result;
  528. }
  529. void *
  530. internal_function
  531. _dl_allocate_tls (void *mem)
  532. {
  533. return _dl_allocate_tls_init (mem == NULL
  534. ? _dl_allocate_tls_storage ()
  535. : allocate_dtv (mem));
  536. }
  537. void
  538. internal_function
  539. _dl_deallocate_tls (void *tcb, bool dealloc_tcb)
  540. {
  541. dtv_t *dtv = GET_DTV (tcb);
  542. size_t cnt;
  543. /* We need to free the memory allocated for non-static TLS. */
  544. for (cnt = 0; cnt < dtv[-1].counter; ++cnt)
  545. if (! dtv[1 + cnt].pointer.is_static
  546. && dtv[1 + cnt].pointer.val != TLS_DTV_UNALLOCATED)
  547. _dl_free (dtv[1 + cnt].pointer.val);
  548. /* The array starts with dtv[-1]. */
  549. if (dtv != _dl_initial_dtv)
  550. _dl_free (dtv - 1);
  551. if (dealloc_tcb)
  552. {
  553. # ifdef TLS_TCB_AT_TP
  554. /* The TCB follows the TLS blocks. Back up to free the whole block. */
  555. tcb -= _dl_tls_static_size - TLS_TCB_SIZE;
  556. # elif defined(TLS_DTV_AT_TP)
  557. /* Back up the TLS_PRE_TCB_SIZE bytes. */
  558. tcb -= (TLS_PRE_TCB_SIZE + _dl_tls_static_align - 1)
  559. & ~(_dl_tls_static_align - 1);
  560. # endif
  561. _dl_free (tcb);
  562. }
  563. }
  564. static void *
  565. allocate_and_init (struct link_map *map)
  566. {
  567. void *newp;
  568. newp = _dl_memalign (map->l_tls_align, map->l_tls_blocksize);
  569. if (newp == NULL)
  570. {
  571. _dl_dprintf(2, "%s:%d: Out of memory!!!\n", __func__, __LINE__);
  572. _dl_exit(1);
  573. }
  574. /* Initialize the memory. */
  575. _dl_memcpy (newp, map->l_tls_initimage, map->l_tls_initimage_size);
  576. _dl_memset ((newp + map->l_tls_initimage_size), '\0',
  577. map->l_tls_blocksize - map->l_tls_initimage_size);
  578. return newp;
  579. }
  580. struct link_map *
  581. _dl_update_slotinfo (unsigned long int req_modid)
  582. {
  583. struct link_map *the_map = NULL;
  584. dtv_t *dtv = THREAD_DTV ();
  585. /* The global dl_tls_dtv_slotinfo array contains for each module
  586. index the generation counter current when the entry was created.
  587. This array never shrinks so that all module indices which were
  588. valid at some time can be used to access it. Before the first
  589. use of a new module index in this function the array was extended
  590. appropriately. Access also does not have to be guarded against
  591. modifications of the array. It is assumed that pointer-size
  592. values can be read atomically even in SMP environments. It is
  593. possible that other threads at the same time dynamically load
  594. code and therefore add to the slotinfo list. This is a problem
  595. since we must not pick up any information about incomplete work.
  596. The solution to this is to ignore all dtv slots which were
  597. created after the one we are currently interested. We know that
  598. dynamic loading for this module is completed and this is the last
  599. load operation we know finished. */
  600. unsigned long int idx = req_modid;
  601. struct dtv_slotinfo_list *listp = _dl_tls_dtv_slotinfo_list;
  602. _dl_debug_early ("Updating slotinfo for module %d\n", req_modid);
  603. while (idx >= listp->len)
  604. {
  605. idx -= listp->len;
  606. listp = listp->next;
  607. }
  608. if (dtv[0].counter < listp->slotinfo[idx].gen)
  609. {
  610. /* The generation counter for the slot is higher than what the
  611. current dtv implements. We have to update the whole dtv but
  612. only those entries with a generation counter <= the one for
  613. the entry we need. */
  614. size_t new_gen = listp->slotinfo[idx].gen;
  615. size_t total = 0;
  616. /* We have to look through the entire dtv slotinfo list. */
  617. listp = _dl_tls_dtv_slotinfo_list;
  618. do
  619. {
  620. size_t cnt;
  621. for (cnt = total == 0 ? 1 : 0; cnt < listp->len; ++cnt)
  622. {
  623. size_t gen = listp->slotinfo[cnt].gen;
  624. if (gen > new_gen)
  625. /* This is a slot for a generation younger than the
  626. one we are handling now. It might be incompletely
  627. set up so ignore it. */
  628. continue;
  629. /* If the entry is older than the current dtv layout we
  630. know we don't have to handle it. */
  631. if (gen <= dtv[0].counter)
  632. continue;
  633. /* If there is no map this means the entry is empty. */
  634. struct link_map *map = listp->slotinfo[cnt].map;
  635. if (map == NULL)
  636. {
  637. /* If this modid was used at some point the memory
  638. might still be allocated. */
  639. if (! dtv[total + cnt].pointer.is_static
  640. && dtv[total + cnt].pointer.val != TLS_DTV_UNALLOCATED)
  641. {
  642. _dl_free (dtv[total + cnt].pointer.val);
  643. dtv[total + cnt].pointer.val = TLS_DTV_UNALLOCATED;
  644. }
  645. continue;
  646. }
  647. /* Check whether the current dtv array is large enough. */
  648. size_t modid = map->l_tls_modid;
  649. _dl_assert (total + cnt == modid);
  650. if (dtv[-1].counter < modid)
  651. {
  652. /* Reallocate the dtv. */
  653. dtv_t *newp;
  654. size_t newsize = _dl_tls_max_dtv_idx + DTV_SURPLUS;
  655. size_t oldsize = dtv[-1].counter;
  656. _dl_assert (map->l_tls_modid <= newsize);
  657. if (dtv == _dl_initial_dtv)
  658. {
  659. /* This is the initial dtv that was allocated
  660. during rtld startup using the dl-minimal.c
  661. malloc instead of the real malloc. We can't
  662. free it, we have to abandon the old storage. */
  663. newp = _dl_malloc ((2 + newsize) * sizeof (dtv_t));
  664. if (newp == NULL)
  665. oom ();
  666. _dl_memcpy (newp, &dtv[-1], oldsize * sizeof (dtv_t));
  667. }
  668. else
  669. {
  670. newp = _dl_realloc (&dtv[-1],
  671. (2 + newsize) * sizeof (dtv_t));
  672. if (newp == NULL)
  673. oom ();
  674. }
  675. newp[0].counter = newsize;
  676. /* Clear the newly allocated part. */
  677. _dl_memset (newp + 2 + oldsize, '\0',
  678. (newsize - oldsize) * sizeof (dtv_t));
  679. /* Point dtv to the generation counter. */
  680. dtv = &newp[1];
  681. /* Install this new dtv in the thread data
  682. structures. */
  683. INSTALL_NEW_DTV (dtv);
  684. }
  685. /* If there is currently memory allocate for this
  686. dtv entry free it. */
  687. /* XXX Ideally we will at some point create a memory
  688. pool. */
  689. if (! dtv[modid].pointer.is_static
  690. && dtv[modid].pointer.val != TLS_DTV_UNALLOCATED)
  691. /* Note that free is called for NULL is well. We
  692. deallocate even if it is this dtv entry we are
  693. supposed to load. The reason is that we call
  694. memalign and not malloc. */
  695. _dl_free (dtv[modid].pointer.val);
  696. /* This module is loaded dynamically- We defer memory
  697. allocation. */
  698. dtv[modid].pointer.is_static = false;
  699. dtv[modid].pointer.val = TLS_DTV_UNALLOCATED;
  700. if (modid == req_modid)
  701. the_map = map;
  702. }
  703. total += listp->len;
  704. }
  705. while ((listp = listp->next) != NULL);
  706. /* This will be the new maximum generation counter. */
  707. dtv[0].counter = new_gen;
  708. }
  709. return the_map;
  710. }
  711. /* The generic dynamic and local dynamic model cannot be used in
  712. statically linked applications. */
  713. void *
  714. __tls_get_addr (GET_ADDR_ARGS)
  715. {
  716. dtv_t *dtv = THREAD_DTV ();
  717. struct link_map *the_map = NULL;
  718. void *p;
  719. if (__builtin_expect (dtv[0].counter != _dl_tls_generation, 0))
  720. {
  721. the_map = _dl_update_slotinfo (GET_ADDR_MODULE);
  722. dtv = THREAD_DTV ();
  723. }
  724. p = dtv[GET_ADDR_MODULE].pointer.val;
  725. if (__builtin_expect (p == TLS_DTV_UNALLOCATED, 0))
  726. {
  727. /* The allocation was deferred. Do it now. */
  728. if (the_map == NULL)
  729. {
  730. /* Find the link map for this module. */
  731. size_t idx = GET_ADDR_MODULE;
  732. struct dtv_slotinfo_list *listp = _dl_tls_dtv_slotinfo_list;
  733. while (idx >= listp->len)
  734. {
  735. idx -= listp->len;
  736. listp = listp->next;
  737. }
  738. the_map = listp->slotinfo[idx].map;
  739. }
  740. p = dtv[GET_ADDR_MODULE].pointer.val = allocate_and_init (the_map);
  741. dtv[GET_ADDR_MODULE].pointer.is_static = false;
  742. }
  743. return (char *) p + GET_ADDR_OFFSET;
  744. }
  745. void
  746. _dl_add_to_slotinfo (struct link_map *l)
  747. {
  748. /* Now that we know the object is loaded successfully add
  749. modules containing TLS data to the dtv info table. We
  750. might have to increase its size. */
  751. struct dtv_slotinfo_list *listp;
  752. struct dtv_slotinfo_list *prevp;
  753. size_t idx = l->l_tls_modid;
  754. _dl_debug_early("Adding to slotinfo for %s\n", l->l_name);
  755. /* Find the place in the dtv slotinfo list. */
  756. listp = _dl_tls_dtv_slotinfo_list;
  757. prevp = NULL; /* Needed to shut up gcc. */
  758. do
  759. {
  760. /* Does it fit in the array of this list element? */
  761. if (idx < listp->len)
  762. break;
  763. idx -= listp->len;
  764. prevp = listp;
  765. listp = listp->next;
  766. }
  767. while (listp != NULL);
  768. if (listp == NULL)
  769. {
  770. /* When we come here it means we have to add a new element
  771. to the slotinfo list. And the new module must be in
  772. the first slot. */
  773. _dl_assert (idx == 0);
  774. listp = prevp->next = (struct dtv_slotinfo_list *)
  775. _dl_malloc (sizeof (struct dtv_slotinfo_list)
  776. + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
  777. if (listp == NULL)
  778. {
  779. /* We ran out of memory. We will simply fail this
  780. call but don't undo anything we did so far. The
  781. application will crash or be terminated anyway very
  782. soon. */
  783. /* We have to do this since some entries in the dtv
  784. slotinfo array might already point to this
  785. generation. */
  786. ++_dl_tls_generation;
  787. _dl_dprintf(2, "cannot create TLS data structures: ABORT\n");
  788. _dl_exit (127);
  789. }
  790. listp->len = TLS_SLOTINFO_SURPLUS;
  791. listp->next = NULL;
  792. _dl_memset (listp->slotinfo, '\0',
  793. TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
  794. }
  795. /* Add the information into the slotinfo data structure. */
  796. listp->slotinfo[idx].map = l;
  797. listp->slotinfo[idx].gen = _dl_tls_generation + 1;
  798. /* ??? ideally this would be done once per call to dlopen. However there's
  799. no easy way to indicate whether a library used TLS, so do it here
  800. instead. */
  801. /* Bump the TLS generation number. */
  802. _dl_tls_generation++;
  803. }
  804. /* Taken from glibc/elf/rtld.c */
  805. static bool tls_init_tp_called;
  806. /* _dl_error_catch_tsd points to this for the single-threaded case.
  807. It's reset by the thread library for multithreaded programs. */
  808. void ** __attribute__ ((const))
  809. _dl_initial_error_catch_tsd (void)
  810. {
  811. static
  812. #if 0 /* def ARCH_NEEDS_BOOTSTRAP_RELOCS */
  813. /* If we have to do bootstrap relocs anyway we might as well */
  814. __thread
  815. # endif
  816. void *__tsd_data;
  817. return &__tsd_data;
  818. }
  819. #ifdef SHARED
  820. void*
  821. internal_function
  822. init_tls (void);
  823. rtld_hidden_proto(init_tls)
  824. void *
  825. internal_function
  826. init_tls (void)
  827. {
  828. /* Number of elements in the static TLS block. */
  829. _dl_tls_static_nelem = _dl_tls_max_dtv_idx;
  830. /* Do not do this twice. The audit interface might have required
  831. the DTV interfaces to be set up early. */
  832. if (_dl_initial_dtv != NULL)
  833. return NULL;
  834. /* Allocate the array which contains the information about the
  835. dtv slots. We allocate a few entries more than needed to
  836. avoid the need for reallocation. */
  837. size_t nelem = _dl_tls_max_dtv_idx + 1 + TLS_SLOTINFO_SURPLUS;
  838. /* Allocate. */
  839. _dl_assert (_dl_tls_dtv_slotinfo_list == NULL);
  840. _dl_tls_dtv_slotinfo_list = (struct dtv_slotinfo_list *)
  841. _dl_calloc (sizeof (struct dtv_slotinfo_list)
  842. + nelem * sizeof (struct dtv_slotinfo), 1);
  843. /* No need to check the return value. If memory allocation failed
  844. the program would have been terminated. */
  845. struct dtv_slotinfo *slotinfo = _dl_tls_dtv_slotinfo_list->slotinfo;
  846. _dl_tls_dtv_slotinfo_list->len = nelem;
  847. _dl_tls_dtv_slotinfo_list->next = NULL;
  848. /* Fill in the information from the loaded modules. No namespace
  849. but the base one can be filled at this time. */
  850. int i = 0;
  851. struct link_map *l;
  852. for (l = (struct link_map *) _dl_loaded_modules; l != NULL; l = l->l_next)
  853. if (l->l_tls_blocksize != 0)
  854. {
  855. /* This is a module with TLS data. Store the map reference.
  856. The generation counter is zero. */
  857. /* Skeep slot[0]: it will be never used */
  858. slotinfo[++i].map = l;
  859. }
  860. _dl_assert (i == _dl_tls_max_dtv_idx);
  861. /* Compute the TLS offsets for the various blocks. */
  862. _dl_determine_tlsoffset ();
  863. /* Construct the static TLS block and the dtv for the initial
  864. thread. For some platforms this will include allocating memory
  865. for the thread descriptor. The memory for the TLS block will
  866. never be freed. It should be allocated accordingly. The dtv
  867. array can be changed if dynamic loading requires it. */
  868. void *tcbp = _dl_allocate_tls_storage ();
  869. if (tcbp == NULL) {
  870. _dl_debug_early("\ncannot allocate TLS data structures for initial thread");
  871. _dl_exit(30);
  872. }
  873. /* Store for detection of the special case by __tls_get_addr
  874. so it knows not to pass this dtv to the normal realloc. */
  875. _dl_initial_dtv = GET_DTV (tcbp);
  876. /* And finally install it for the main thread. If ld.so itself uses
  877. TLS we know the thread pointer was initialized earlier. */
  878. const char *lossage = (char *)TLS_INIT_TP (tcbp, USE___THREAD);
  879. if(__builtin_expect (lossage != NULL, 0)) {
  880. _dl_debug_early("cannot set up thread-local storage: %s\n", lossage);
  881. _dl_exit(30);
  882. }
  883. tls_init_tp_called = true;
  884. return tcbp;
  885. }
  886. rtld_hidden_def (init_tls)
  887. #endif