dl-tls.c 31 KB

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